ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / video / zoran_procfs.c
1 /*
2  * Zoran zr36057/zr36067 PCI controller driver, for the
3  * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4  * Media Labs LML33/LML33R10.
5  *
6  * This part handles the procFS entries (/proc/ZORAN[%d])
7  * 
8  * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9  *
10  * Currently maintained by:
11  *   Ronald Bultje    <rbultje@ronald.bitfreak.net>
12  *   Laurent Pinchart <laurent.pinchart@skynet.be>
13  *   Mailinglist      <mjpeg-users@lists.sf.net>
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  */
29
30 #include <linux/config.h>
31 #include <linux/types.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/vmalloc.h>
35
36 #include <linux/proc_fs.h>
37 #include <linux/pci.h>
38 #include <linux/i2c.h>
39 #include <linux/i2c-algo-bit.h>
40 #include <linux/videodev.h>
41 #include <linux/spinlock.h>
42 #include <linux/sem.h>
43
44 #include <linux/ctype.h>
45 #include <asm/io.h>
46
47 #include "videocodec.h"
48 #include "zoran.h"
49 #include "zoran_procfs.h"
50
51 extern int *zr_debug;
52
53 #define dprintk(num, format, args...) \
54         do { \
55                 if (*zr_debug >= num) \
56                         printk(format, ##args); \
57         } while (0)
58
59 #ifdef CONFIG_PROC_FS
60 struct procfs_params_zr36067 {
61         char *name;
62         short reg;
63         u32 mask;
64         short bit;
65 };
66
67 static const struct procfs_params_zr36067 zr67[] = {
68         {"HSPol", 0x000, 1, 30},
69         {"HStart", 0x000, 0x3ff, 10},
70         {"HEnd", 0x000, 0x3ff, 0},
71
72         {"VSPol", 0x004, 1, 30},
73         {"VStart", 0x004, 0x3ff, 10},
74         {"VEnd", 0x004, 0x3ff, 0},
75
76         {"ExtFl", 0x008, 1, 26},
77         {"TopField", 0x008, 1, 25},
78         {"VCLKPol", 0x008, 1, 24},
79         {"DupFld", 0x008, 1, 20},
80         {"LittleEndian", 0x008, 1, 0},
81
82         {"HsyncStart", 0x10c, 0xffff, 16},
83         {"LineTot", 0x10c, 0xffff, 0},
84
85         {"NAX", 0x110, 0xffff, 16},
86         {"PAX", 0x110, 0xffff, 0},
87
88         {"NAY", 0x114, 0xffff, 16},
89         {"PAY", 0x114, 0xffff, 0},
90
91         /* {"",,,}, */
92
93         {NULL, 0, 0, 0},
94 };
95
96 struct procfs_io {
97         char *buffer;
98         char *end;
99         int neof;
100         int count;
101         int count_current;
102 };
103
104 static void
105 setparam (struct zoran *zr,
106           char         *name,
107           char         *sval)
108 {
109         int i = 0, reg0, reg, val;
110
111         while (zr67[i].name != NULL) {
112                 if (!strncmp(name, zr67[i].name, strlen(zr67[i].name))) {
113                         reg = reg0 = btread(zr67[i].reg);
114                         reg &= ~(zr67[i].mask << zr67[i].bit);
115                         if (!isdigit(sval[0]))
116                                 break;
117                         val = simple_strtoul(sval, NULL, 0);
118                         if ((val & ~zr67[i].mask))
119                                 break;
120                         reg |= (val & zr67[i].mask) << zr67[i].bit;
121                         dprintk(4,
122                                 KERN_INFO
123                                 "%s: setparam: setting ZR36067 register 0x%03x: 0x%08x=>0x%08x %s=%d\n",
124                                 ZR_DEVNAME(zr), zr67[i].reg, reg0, reg,
125                                 zr67[i].name, val);
126                         btwrite(reg, zr67[i].reg);
127                         break;
128                 }
129                 i++;
130         }
131 }
132
133 static int
134 print_procfs (struct procfs_io *io,
135               const char       *fmt,
136               ...)
137 {
138         va_list args;
139         int i;
140
141         if (io->buffer >= io->end) {
142                 io->neof++;
143                 return 0;
144         }
145         if (io->count > io->count_current++)
146                 return 0;
147         va_start(args, fmt);
148         i = vsprintf(io->buffer, fmt, args);
149         io->buffer += i;
150         va_end(args);
151         return i;
152 }
153
154 static void
155 zoran_procfs_output (struct procfs_io *io,
156                      void             *data)
157 {
158         int i;
159         struct zoran *zr;
160         zr = (struct zoran *) data;
161
162         print_procfs(io, "ZR36067 registers:");
163         for (i = 0; i < 0x130; i += 4) {
164                 if (!(i % 16)) {
165                         print_procfs(io, "\n%03X", i);
166                 };
167                 print_procfs(io, " %08X ", btread(i));
168         };
169         print_procfs(io, "\n");
170 }
171
172 static int
173 zoran_read_proc (char  *buffer,
174                  char **start,
175                  off_t  offset,
176                  int    size,
177                  int   *eof,
178                  void  *data)
179 {
180         struct procfs_io io;
181         int nbytes;
182
183         io.buffer = buffer;
184         io.end = buffer + size - 128;   // Just to make it a little bit safer
185         io.count = offset;
186         io.count_current = 0;
187         io.neof = 0;
188         zoran_procfs_output(&io, data);
189         *start = (char *) (io.count_current - io.count);
190         nbytes = (int) (io.buffer - buffer);
191         *eof = !io.neof;
192         return nbytes;
193
194         return 0;
195 }
196
197 static int
198 zoran_write_proc (struct file   *file,
199                   const char    *buffer,
200                   unsigned long  count,
201                   void          *data)
202 {
203         char *string, *sp;
204         char *line, *ldelim, *varname, *svar, *tdelim;
205         struct zoran *zr;
206
207         if (count > 32768)      /* Stupidity filter */
208                 return -EINVAL;
209
210         zr = (struct zoran *) data;
211
212         string = sp = vmalloc(count + 1);
213         if (!string) {
214                 dprintk(1,
215                         KERN_ERR
216                         "%s: write_proc: can not allocate memory\n",
217                         ZR_DEVNAME(zr));
218                 return -ENOMEM;
219         }
220         if (copy_from_user(string, buffer, count)) {
221                 vfree (string);
222                 return -EFAULT;
223         }
224         string[count] = 0;
225         dprintk(4, KERN_INFO "%s: write_proc: name=%s count=%lu data=%x\n",
226                 ZR_DEVNAME(zr), file->f_dentry->d_name.name, count, (int) data);
227         ldelim = " \t\n";
228         tdelim = "=";
229         line = strpbrk(sp, ldelim);
230         while (line) {
231                 *line = 0;
232                 svar = strpbrk(sp, tdelim);
233                 if (svar) {
234                         *svar = 0;
235                         varname = sp;
236                         svar++;
237                         setparam(zr, varname, svar);
238                 }
239                 sp = line + 1;
240                 line = strpbrk(sp, ldelim);
241         }
242         vfree(string);
243
244         return count;
245 }
246 #endif
247
248 int
249 zoran_proc_init (struct zoran *zr)
250 {
251 #ifdef CONFIG_PROC_FS
252         char name[8];
253
254         snprintf(name, 7, "zoran%d", zr->id);
255         if ((zr->zoran_proc = create_proc_entry(name, 0, 0))) {
256                 zr->zoran_proc->read_proc = zoran_read_proc;
257                 zr->zoran_proc->write_proc = zoran_write_proc;
258                 zr->zoran_proc->data = zr;
259                 zr->zoran_proc->owner = THIS_MODULE;
260                 dprintk(2,
261                         KERN_INFO
262                         "%s: procfs entry /proc/%s allocated. data=%p\n",
263                         ZR_DEVNAME(zr), name, zr->zoran_proc->data);
264         } else {
265                 dprintk(1, KERN_ERR "%s: Unable to initialise /proc/%s\n",
266                         ZR_DEVNAME(zr), name);
267                 return 1;
268         }
269 #endif
270         return 0;
271 }
272
273 void
274 zoran_proc_cleanup (struct zoran *zr)
275 {
276 #ifdef CONFIG_PROC_FS
277         char name[8];
278
279         snprintf(name, 7, "zoran%d", zr->id);
280         if (zr->zoran_proc) {
281                 remove_proc_entry(name, 0);
282         }
283         zr->zoran_proc = NULL;
284 #endif
285 }