V4L/DVB (7813): Fix compilation, when V4L1_COMPAT is disabled
[linux-2.6] / drivers / media / video / videodev.c
1 /*
2  * Video capture interface for Linux version 2
3  *
4  *      A generic video device interface for the LINUX operating system
5  *      using a set of device structures/vectors for low level operations.
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  *
12  * Authors:     Alan Cox, <alan@redhat.com> (version 1)
13  *              Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
14  *
15  * Fixes:       20000516  Claudio Matsuoka <claudio@conectiva.com>
16  *              - Added procfs support
17  */
18
19 #define dbgarg(cmd, fmt, arg...) \
20                 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {                \
21                         printk(KERN_DEBUG "%s: ",  vfd->name);          \
22                         v4l_printk_ioctl(cmd);                          \
23                         printk(" " fmt,  ## arg);                       \
24                 }
25
26 #define dbgarg2(fmt, arg...) \
27                 if (vfd->debug & V4L2_DEBUG_IOCTL_ARG)                  \
28                         printk(KERN_DEBUG "%s: " fmt, vfd->name, ## arg);
29
30 #include <linux/module.h>
31 #include <linux/types.h>
32 #include <linux/kernel.h>
33 #include <linux/mm.h>
34 #include <linux/string.h>
35 #include <linux/errno.h>
36 #include <linux/init.h>
37 #include <linux/kmod.h>
38 #include <linux/slab.h>
39 #include <asm/uaccess.h>
40 #include <asm/system.h>
41
42 #define __OLD_VIDIOC_ /* To allow fixing old calls*/
43 #include <linux/videodev2.h>
44
45 #ifdef CONFIG_VIDEO_V4L1
46 #include <linux/videodev.h>
47 #endif
48 #include <media/v4l2-common.h>
49 #include <linux/video_decoder.h>
50
51 #define VIDEO_NUM_DEVICES       256
52 #define VIDEO_NAME              "video4linux"
53
54 /* video4linux standard ID conversion to standard name
55  */
56 char *v4l2_norm_to_name(v4l2_std_id id)
57 {
58         char *name;
59         u32 myid = id;
60
61         /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
62            64 bit comparations. So, on that architecture, with some gcc
63            variants, compilation fails. Currently, the max value is 30bit wide.
64          */
65         BUG_ON(myid != id);
66
67         switch (myid) {
68         case V4L2_STD_PAL:
69                 name = "PAL";
70                 break;
71         case V4L2_STD_PAL_BG:
72                 name = "PAL-BG";
73                 break;
74         case V4L2_STD_PAL_DK:
75                 name = "PAL-DK";
76                 break;
77         case V4L2_STD_PAL_B:
78                 name = "PAL-B";
79                 break;
80         case V4L2_STD_PAL_B1:
81                 name = "PAL-B1";
82                 break;
83         case V4L2_STD_PAL_G:
84                 name = "PAL-G";
85                 break;
86         case V4L2_STD_PAL_H:
87                 name = "PAL-H";
88                 break;
89         case V4L2_STD_PAL_I:
90                 name = "PAL-I";
91                 break;
92         case V4L2_STD_PAL_D:
93                 name = "PAL-D";
94                 break;
95         case V4L2_STD_PAL_D1:
96                 name = "PAL-D1";
97                 break;
98         case V4L2_STD_PAL_K:
99                 name = "PAL-K";
100                 break;
101         case V4L2_STD_PAL_M:
102                 name = "PAL-M";
103                 break;
104         case V4L2_STD_PAL_N:
105                 name = "PAL-N";
106                 break;
107         case V4L2_STD_PAL_Nc:
108                 name = "PAL-Nc";
109                 break;
110         case V4L2_STD_PAL_60:
111                 name = "PAL-60";
112                 break;
113         case V4L2_STD_NTSC:
114                 name = "NTSC";
115                 break;
116         case V4L2_STD_NTSC_M:
117                 name = "NTSC-M";
118                 break;
119         case V4L2_STD_NTSC_M_JP:
120                 name = "NTSC-M-JP";
121                 break;
122         case V4L2_STD_NTSC_443:
123                 name = "NTSC-443";
124                 break;
125         case V4L2_STD_NTSC_M_KR:
126                 name = "NTSC-M-KR";
127                 break;
128         case V4L2_STD_SECAM:
129                 name = "SECAM";
130                 break;
131         case V4L2_STD_SECAM_DK:
132                 name = "SECAM-DK";
133                 break;
134         case V4L2_STD_SECAM_B:
135                 name = "SECAM-B";
136                 break;
137         case V4L2_STD_SECAM_D:
138                 name = "SECAM-D";
139                 break;
140         case V4L2_STD_SECAM_G:
141                 name = "SECAM-G";
142                 break;
143         case V4L2_STD_SECAM_H:
144                 name = "SECAM-H";
145                 break;
146         case V4L2_STD_SECAM_K:
147                 name = "SECAM-K";
148                 break;
149         case V4L2_STD_SECAM_K1:
150                 name = "SECAM-K1";
151                 break;
152         case V4L2_STD_SECAM_L:
153                 name = "SECAM-L";
154                 break;
155         case V4L2_STD_SECAM_LC:
156                 name = "SECAM-LC";
157                 break;
158         default:
159                 name = "Unknown";
160                 break;
161         }
162
163         return name;
164 }
165 EXPORT_SYMBOL(v4l2_norm_to_name);
166
167 /* Fill in the fields of a v4l2_standard structure according to the
168    'id' and 'transmission' parameters.  Returns negative on error.  */
169 int v4l2_video_std_construct(struct v4l2_standard *vs,
170                              int id, char *name)
171 {
172         u32 index = vs->index;
173
174         memset(vs, 0, sizeof(struct v4l2_standard));
175         vs->index = index;
176         vs->id    = id;
177         if (id & V4L2_STD_525_60) {
178                 vs->frameperiod.numerator = 1001;
179                 vs->frameperiod.denominator = 30000;
180                 vs->framelines = 525;
181         } else {
182                 vs->frameperiod.numerator = 1;
183                 vs->frameperiod.denominator = 25;
184                 vs->framelines = 625;
185         }
186         strlcpy(vs->name, name, sizeof(vs->name));
187         return 0;
188 }
189 EXPORT_SYMBOL(v4l2_video_std_construct);
190
191 /* ----------------------------------------------------------------- */
192 /* some arrays for pretty-printing debug messages of enum types      */
193
194 char *v4l2_field_names[] = {
195         [V4L2_FIELD_ANY]        = "any",
196         [V4L2_FIELD_NONE]       = "none",
197         [V4L2_FIELD_TOP]        = "top",
198         [V4L2_FIELD_BOTTOM]     = "bottom",
199         [V4L2_FIELD_INTERLACED] = "interlaced",
200         [V4L2_FIELD_SEQ_TB]     = "seq-tb",
201         [V4L2_FIELD_SEQ_BT]     = "seq-bt",
202         [V4L2_FIELD_ALTERNATE]  = "alternate",
203         [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
204         [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
205 };
206 EXPORT_SYMBOL(v4l2_field_names);
207
208 char *v4l2_type_names[] = {
209         [V4L2_BUF_TYPE_VIDEO_CAPTURE]      = "video-cap",
210         [V4L2_BUF_TYPE_VIDEO_OVERLAY]      = "video-over",
211         [V4L2_BUF_TYPE_VIDEO_OUTPUT]       = "video-out",
212         [V4L2_BUF_TYPE_VBI_CAPTURE]        = "vbi-cap",
213         [V4L2_BUF_TYPE_VBI_OUTPUT]         = "vbi-out",
214         [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
215         [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT]  = "sliced-vbi-out",
216         [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "video-out-over",
217 };
218 EXPORT_SYMBOL(v4l2_type_names);
219
220 static char *v4l2_memory_names[] = {
221         [V4L2_MEMORY_MMAP]    = "mmap",
222         [V4L2_MEMORY_USERPTR] = "userptr",
223         [V4L2_MEMORY_OVERLAY] = "overlay",
224 };
225
226 #define prt_names(a, arr) ((((a) >= 0) && ((a) < ARRAY_SIZE(arr))) ? \
227                            arr[a] : "unknown")
228
229 /* ------------------------------------------------------------------ */
230 /* debug help functions                                               */
231
232 #ifdef CONFIG_VIDEO_V4L1_COMPAT
233 static const char *v4l1_ioctls[] = {
234         [_IOC_NR(VIDIOCGCAP)]       = "VIDIOCGCAP",
235         [_IOC_NR(VIDIOCGCHAN)]      = "VIDIOCGCHAN",
236         [_IOC_NR(VIDIOCSCHAN)]      = "VIDIOCSCHAN",
237         [_IOC_NR(VIDIOCGTUNER)]     = "VIDIOCGTUNER",
238         [_IOC_NR(VIDIOCSTUNER)]     = "VIDIOCSTUNER",
239         [_IOC_NR(VIDIOCGPICT)]      = "VIDIOCGPICT",
240         [_IOC_NR(VIDIOCSPICT)]      = "VIDIOCSPICT",
241         [_IOC_NR(VIDIOCCAPTURE)]    = "VIDIOCCAPTURE",
242         [_IOC_NR(VIDIOCGWIN)]       = "VIDIOCGWIN",
243         [_IOC_NR(VIDIOCSWIN)]       = "VIDIOCSWIN",
244         [_IOC_NR(VIDIOCGFBUF)]      = "VIDIOCGFBUF",
245         [_IOC_NR(VIDIOCSFBUF)]      = "VIDIOCSFBUF",
246         [_IOC_NR(VIDIOCKEY)]        = "VIDIOCKEY",
247         [_IOC_NR(VIDIOCGFREQ)]      = "VIDIOCGFREQ",
248         [_IOC_NR(VIDIOCSFREQ)]      = "VIDIOCSFREQ",
249         [_IOC_NR(VIDIOCGAUDIO)]     = "VIDIOCGAUDIO",
250         [_IOC_NR(VIDIOCSAUDIO)]     = "VIDIOCSAUDIO",
251         [_IOC_NR(VIDIOCSYNC)]       = "VIDIOCSYNC",
252         [_IOC_NR(VIDIOCMCAPTURE)]   = "VIDIOCMCAPTURE",
253         [_IOC_NR(VIDIOCGMBUF)]      = "VIDIOCGMBUF",
254         [_IOC_NR(VIDIOCGUNIT)]      = "VIDIOCGUNIT",
255         [_IOC_NR(VIDIOCGCAPTURE)]   = "VIDIOCGCAPTURE",
256         [_IOC_NR(VIDIOCSCAPTURE)]   = "VIDIOCSCAPTURE",
257         [_IOC_NR(VIDIOCSPLAYMODE)]  = "VIDIOCSPLAYMODE",
258         [_IOC_NR(VIDIOCSWRITEMODE)] = "VIDIOCSWRITEMODE",
259         [_IOC_NR(VIDIOCGPLAYINFO)]  = "VIDIOCGPLAYINFO",
260         [_IOC_NR(VIDIOCSMICROCODE)] = "VIDIOCSMICROCODE",
261         [_IOC_NR(VIDIOCGVBIFMT)]    = "VIDIOCGVBIFMT",
262         [_IOC_NR(VIDIOCSVBIFMT)]    = "VIDIOCSVBIFMT"
263 };
264 #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
265 #endif
266
267 static const char *v4l2_ioctls[] = {
268         [_IOC_NR(VIDIOC_QUERYCAP)]         = "VIDIOC_QUERYCAP",
269         [_IOC_NR(VIDIOC_RESERVED)]         = "VIDIOC_RESERVED",
270         [_IOC_NR(VIDIOC_ENUM_FMT)]         = "VIDIOC_ENUM_FMT",
271         [_IOC_NR(VIDIOC_G_FMT)]            = "VIDIOC_G_FMT",
272         [_IOC_NR(VIDIOC_S_FMT)]            = "VIDIOC_S_FMT",
273         [_IOC_NR(VIDIOC_REQBUFS)]          = "VIDIOC_REQBUFS",
274         [_IOC_NR(VIDIOC_QUERYBUF)]         = "VIDIOC_QUERYBUF",
275         [_IOC_NR(VIDIOC_G_FBUF)]           = "VIDIOC_G_FBUF",
276         [_IOC_NR(VIDIOC_S_FBUF)]           = "VIDIOC_S_FBUF",
277         [_IOC_NR(VIDIOC_OVERLAY)]          = "VIDIOC_OVERLAY",
278         [_IOC_NR(VIDIOC_QBUF)]             = "VIDIOC_QBUF",
279         [_IOC_NR(VIDIOC_DQBUF)]            = "VIDIOC_DQBUF",
280         [_IOC_NR(VIDIOC_STREAMON)]         = "VIDIOC_STREAMON",
281         [_IOC_NR(VIDIOC_STREAMOFF)]        = "VIDIOC_STREAMOFF",
282         [_IOC_NR(VIDIOC_G_PARM)]           = "VIDIOC_G_PARM",
283         [_IOC_NR(VIDIOC_S_PARM)]           = "VIDIOC_S_PARM",
284         [_IOC_NR(VIDIOC_G_STD)]            = "VIDIOC_G_STD",
285         [_IOC_NR(VIDIOC_S_STD)]            = "VIDIOC_S_STD",
286         [_IOC_NR(VIDIOC_ENUMSTD)]          = "VIDIOC_ENUMSTD",
287         [_IOC_NR(VIDIOC_ENUMINPUT)]        = "VIDIOC_ENUMINPUT",
288         [_IOC_NR(VIDIOC_G_CTRL)]           = "VIDIOC_G_CTRL",
289         [_IOC_NR(VIDIOC_S_CTRL)]           = "VIDIOC_S_CTRL",
290         [_IOC_NR(VIDIOC_G_TUNER)]          = "VIDIOC_G_TUNER",
291         [_IOC_NR(VIDIOC_S_TUNER)]          = "VIDIOC_S_TUNER",
292         [_IOC_NR(VIDIOC_G_AUDIO)]          = "VIDIOC_G_AUDIO",
293         [_IOC_NR(VIDIOC_S_AUDIO)]          = "VIDIOC_S_AUDIO",
294         [_IOC_NR(VIDIOC_QUERYCTRL)]        = "VIDIOC_QUERYCTRL",
295         [_IOC_NR(VIDIOC_QUERYMENU)]        = "VIDIOC_QUERYMENU",
296         [_IOC_NR(VIDIOC_G_INPUT)]          = "VIDIOC_G_INPUT",
297         [_IOC_NR(VIDIOC_S_INPUT)]          = "VIDIOC_S_INPUT",
298         [_IOC_NR(VIDIOC_G_OUTPUT)]         = "VIDIOC_G_OUTPUT",
299         [_IOC_NR(VIDIOC_S_OUTPUT)]         = "VIDIOC_S_OUTPUT",
300         [_IOC_NR(VIDIOC_ENUMOUTPUT)]       = "VIDIOC_ENUMOUTPUT",
301         [_IOC_NR(VIDIOC_G_AUDOUT)]         = "VIDIOC_G_AUDOUT",
302         [_IOC_NR(VIDIOC_S_AUDOUT)]         = "VIDIOC_S_AUDOUT",
303         [_IOC_NR(VIDIOC_G_MODULATOR)]      = "VIDIOC_G_MODULATOR",
304         [_IOC_NR(VIDIOC_S_MODULATOR)]      = "VIDIOC_S_MODULATOR",
305         [_IOC_NR(VIDIOC_G_FREQUENCY)]      = "VIDIOC_G_FREQUENCY",
306         [_IOC_NR(VIDIOC_S_FREQUENCY)]      = "VIDIOC_S_FREQUENCY",
307         [_IOC_NR(VIDIOC_CROPCAP)]          = "VIDIOC_CROPCAP",
308         [_IOC_NR(VIDIOC_G_CROP)]           = "VIDIOC_G_CROP",
309         [_IOC_NR(VIDIOC_S_CROP)]           = "VIDIOC_S_CROP",
310         [_IOC_NR(VIDIOC_G_JPEGCOMP)]       = "VIDIOC_G_JPEGCOMP",
311         [_IOC_NR(VIDIOC_S_JPEGCOMP)]       = "VIDIOC_S_JPEGCOMP",
312         [_IOC_NR(VIDIOC_QUERYSTD)]         = "VIDIOC_QUERYSTD",
313         [_IOC_NR(VIDIOC_TRY_FMT)]          = "VIDIOC_TRY_FMT",
314         [_IOC_NR(VIDIOC_ENUMAUDIO)]        = "VIDIOC_ENUMAUDIO",
315         [_IOC_NR(VIDIOC_ENUMAUDOUT)]       = "VIDIOC_ENUMAUDOUT",
316         [_IOC_NR(VIDIOC_G_PRIORITY)]       = "VIDIOC_G_PRIORITY",
317         [_IOC_NR(VIDIOC_S_PRIORITY)]       = "VIDIOC_S_PRIORITY",
318         [_IOC_NR(VIDIOC_G_SLICED_VBI_CAP)] = "VIDIOC_G_SLICED_VBI_CAP",
319         [_IOC_NR(VIDIOC_LOG_STATUS)]       = "VIDIOC_LOG_STATUS",
320         [_IOC_NR(VIDIOC_G_EXT_CTRLS)]      = "VIDIOC_G_EXT_CTRLS",
321         [_IOC_NR(VIDIOC_S_EXT_CTRLS)]      = "VIDIOC_S_EXT_CTRLS",
322         [_IOC_NR(VIDIOC_TRY_EXT_CTRLS)]    = "VIDIOC_TRY_EXT_CTRLS",
323 #if 1
324         [_IOC_NR(VIDIOC_ENUM_FRAMESIZES)]  = "VIDIOC_ENUM_FRAMESIZES",
325         [_IOC_NR(VIDIOC_ENUM_FRAMEINTERVALS)] = "VIDIOC_ENUM_FRAMEINTERVALS",
326         [_IOC_NR(VIDIOC_G_ENC_INDEX)]      = "VIDIOC_G_ENC_INDEX",
327         [_IOC_NR(VIDIOC_ENCODER_CMD)]      = "VIDIOC_ENCODER_CMD",
328         [_IOC_NR(VIDIOC_TRY_ENCODER_CMD)]  = "VIDIOC_TRY_ENCODER_CMD",
329
330         [_IOC_NR(VIDIOC_DBG_S_REGISTER)]   = "VIDIOC_DBG_S_REGISTER",
331         [_IOC_NR(VIDIOC_DBG_G_REGISTER)]   = "VIDIOC_DBG_G_REGISTER",
332
333         [_IOC_NR(VIDIOC_G_CHIP_IDENT)]     = "VIDIOC_G_CHIP_IDENT",
334 #endif
335 };
336 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
337
338 static const char *v4l2_int_ioctls[] = {
339 #ifdef CONFIG_VIDEO_V4L1_COMPAT
340         [_IOC_NR(DECODER_GET_CAPABILITIES)]    = "DECODER_GET_CAPABILITIES",
341         [_IOC_NR(DECODER_GET_STATUS)]          = "DECODER_GET_STATUS",
342         [_IOC_NR(DECODER_SET_NORM)]            = "DECODER_SET_NORM",
343         [_IOC_NR(DECODER_SET_INPUT)]           = "DECODER_SET_INPUT",
344         [_IOC_NR(DECODER_SET_OUTPUT)]          = "DECODER_SET_OUTPUT",
345         [_IOC_NR(DECODER_ENABLE_OUTPUT)]       = "DECODER_ENABLE_OUTPUT",
346         [_IOC_NR(DECODER_SET_PICTURE)]         = "DECODER_SET_PICTURE",
347         [_IOC_NR(DECODER_SET_GPIO)]            = "DECODER_SET_GPIO",
348         [_IOC_NR(DECODER_INIT)]                = "DECODER_INIT",
349         [_IOC_NR(DECODER_SET_VBI_BYPASS)]      = "DECODER_SET_VBI_BYPASS",
350         [_IOC_NR(DECODER_DUMP)]                = "DECODER_DUMP",
351 #endif
352         [_IOC_NR(AUDC_SET_RADIO)]              = "AUDC_SET_RADIO",
353
354         [_IOC_NR(TUNER_SET_TYPE_ADDR)]         = "TUNER_SET_TYPE_ADDR",
355         [_IOC_NR(TUNER_SET_STANDBY)]           = "TUNER_SET_STANDBY",
356         [_IOC_NR(TUNER_SET_CONFIG)]            = "TUNER_SET_CONFIG",
357
358         [_IOC_NR(VIDIOC_INT_S_TUNER_MODE)]     = "VIDIOC_INT_S_TUNER_MODE",
359         [_IOC_NR(VIDIOC_INT_RESET)]            = "VIDIOC_INT_RESET",
360         [_IOC_NR(VIDIOC_INT_AUDIO_CLOCK_FREQ)] = "VIDIOC_INT_AUDIO_CLOCK_FREQ",
361         [_IOC_NR(VIDIOC_INT_DECODE_VBI_LINE)]  = "VIDIOC_INT_DECODE_VBI_LINE",
362         [_IOC_NR(VIDIOC_INT_S_VBI_DATA)]       = "VIDIOC_INT_S_VBI_DATA",
363         [_IOC_NR(VIDIOC_INT_G_VBI_DATA)]       = "VIDIOC_INT_G_VBI_DATA",
364         [_IOC_NR(VIDIOC_INT_I2S_CLOCK_FREQ)]   = "VIDIOC_INT_I2S_CLOCK_FREQ",
365         [_IOC_NR(VIDIOC_INT_S_STANDBY)]        = "VIDIOC_INT_S_STANDBY",
366         [_IOC_NR(VIDIOC_INT_S_AUDIO_ROUTING)]  = "VIDIOC_INT_S_AUDIO_ROUTING",
367         [_IOC_NR(VIDIOC_INT_G_AUDIO_ROUTING)]  = "VIDIOC_INT_G_AUDIO_ROUTING",
368         [_IOC_NR(VIDIOC_INT_S_VIDEO_ROUTING)]  = "VIDIOC_INT_S_VIDEO_ROUTING",
369         [_IOC_NR(VIDIOC_INT_G_VIDEO_ROUTING)]  = "VIDIOC_INT_G_VIDEO_ROUTING",
370         [_IOC_NR(VIDIOC_INT_S_CRYSTAL_FREQ)]   = "VIDIOC_INT_S_CRYSTAL_FREQ",
371         [_IOC_NR(VIDIOC_INT_INIT)]             = "VIDIOC_INT_INIT",
372         [_IOC_NR(VIDIOC_INT_G_STD_OUTPUT)]     = "VIDIOC_INT_G_STD_OUTPUT",
373         [_IOC_NR(VIDIOC_INT_S_STD_OUTPUT)]     = "VIDIOC_INT_S_STD_OUTPUT",
374 };
375 #define V4L2_INT_IOCTLS ARRAY_SIZE(v4l2_int_ioctls)
376
377 /* Common ioctl debug function. This function can be used by
378    external ioctl messages as well as internal V4L ioctl */
379 void v4l_printk_ioctl(unsigned int cmd)
380 {
381         char *dir, *type;
382
383         switch (_IOC_TYPE(cmd)) {
384         case 'd':
385                 if (_IOC_NR(cmd) >= V4L2_INT_IOCTLS) {
386                         type = "v4l2_int";
387                         break;
388                 }
389                 printk("%s", v4l2_int_ioctls[_IOC_NR(cmd)]);
390                 return;
391 #ifdef CONFIG_VIDEO_V4L1_COMPAT
392         case 'v':
393                 if (_IOC_NR(cmd) >= V4L1_IOCTLS) {
394                         type = "v4l1";
395                         break;
396                 }
397                 printk("%s", v4l1_ioctls[_IOC_NR(cmd)]);
398                 return;
399 #endif
400         case 'V':
401                 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
402                         type = "v4l2";
403                         break;
404                 }
405                 printk("%s", v4l2_ioctls[_IOC_NR(cmd)]);
406                 return;
407         default:
408                 type = "unknown";
409         }
410
411         switch (_IOC_DIR(cmd)) {
412         case _IOC_NONE:              dir = "--"; break;
413         case _IOC_READ:              dir = "r-"; break;
414         case _IOC_WRITE:             dir = "-w"; break;
415         case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
416         default:                     dir = "*ERR*"; break;
417         }
418         printk("%s ioctl '%c', dir=%s, #%d (0x%08x)",
419                 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
420 }
421 EXPORT_SYMBOL(v4l_printk_ioctl);
422
423 /*
424  *      sysfs stuff
425  */
426
427 static ssize_t show_name(struct device *cd,
428                          struct device_attribute *attr, char *buf)
429 {
430         struct video_device *vfd = container_of(cd, struct video_device,
431                                                 class_dev);
432         return sprintf(buf, "%.*s\n", (int)sizeof(vfd->name), vfd->name);
433 }
434
435 struct video_device *video_device_alloc(void)
436 {
437         struct video_device *vfd;
438
439         vfd = kzalloc(sizeof(*vfd),GFP_KERNEL);
440         return vfd;
441 }
442 EXPORT_SYMBOL(video_device_alloc);
443
444 void video_device_release(struct video_device *vfd)
445 {
446         kfree(vfd);
447 }
448 EXPORT_SYMBOL(video_device_release);
449
450 static void video_release(struct device *cd)
451 {
452         struct video_device *vfd = container_of(cd, struct video_device,
453                                                                 class_dev);
454
455 #if 1
456         /* needed until all drivers are fixed */
457         if (!vfd->release)
458                 return;
459 #endif
460         vfd->release(vfd);
461 }
462
463 static struct device_attribute video_device_attrs[] = {
464         __ATTR(name, S_IRUGO, show_name, NULL),
465         __ATTR_NULL
466 };
467
468 static struct class video_class = {
469         .name    = VIDEO_NAME,
470         .dev_attrs = video_device_attrs,
471         .dev_release = video_release,
472 };
473
474 /*
475  *      Active devices
476  */
477
478 static struct video_device *video_device[VIDEO_NUM_DEVICES];
479 static DEFINE_MUTEX(videodev_lock);
480
481 struct video_device* video_devdata(struct file *file)
482 {
483         return video_device[iminor(file->f_path.dentry->d_inode)];
484 }
485 EXPORT_SYMBOL(video_devdata);
486
487 /*
488  *      Open a video device - FIXME: Obsoleted
489  */
490 static int video_open(struct inode *inode, struct file *file)
491 {
492         unsigned int minor = iminor(inode);
493         int err = 0;
494         struct video_device *vfl;
495         const struct file_operations *old_fops;
496
497         if(minor>=VIDEO_NUM_DEVICES)
498                 return -ENODEV;
499         mutex_lock(&videodev_lock);
500         vfl=video_device[minor];
501         if(vfl==NULL) {
502                 mutex_unlock(&videodev_lock);
503                 request_module("char-major-%d-%d", VIDEO_MAJOR, minor);
504                 mutex_lock(&videodev_lock);
505                 vfl=video_device[minor];
506                 if (vfl==NULL) {
507                         mutex_unlock(&videodev_lock);
508                         return -ENODEV;
509                 }
510         }
511         old_fops = file->f_op;
512         file->f_op = fops_get(vfl->fops);
513         if(file->f_op->open)
514                 err = file->f_op->open(inode,file);
515         if (err) {
516                 fops_put(file->f_op);
517                 file->f_op = fops_get(old_fops);
518         }
519         fops_put(old_fops);
520         mutex_unlock(&videodev_lock);
521         return err;
522 }
523
524 /*
525  * helper function -- handles userspace copying for ioctl arguments
526  */
527
528 #ifdef __OLD_VIDIOC_
529 static unsigned int
530 video_fix_command(unsigned int cmd)
531 {
532         switch (cmd) {
533         case VIDIOC_OVERLAY_OLD:
534                 cmd = VIDIOC_OVERLAY;
535                 break;
536         case VIDIOC_S_PARM_OLD:
537                 cmd = VIDIOC_S_PARM;
538                 break;
539         case VIDIOC_S_CTRL_OLD:
540                 cmd = VIDIOC_S_CTRL;
541                 break;
542         case VIDIOC_G_AUDIO_OLD:
543                 cmd = VIDIOC_G_AUDIO;
544                 break;
545         case VIDIOC_G_AUDOUT_OLD:
546                 cmd = VIDIOC_G_AUDOUT;
547                 break;
548         case VIDIOC_CROPCAP_OLD:
549                 cmd = VIDIOC_CROPCAP;
550                 break;
551         }
552         return cmd;
553 }
554 #endif
555
556 /*
557  * Obsolete usercopy function - Should be removed soon
558  */
559 int
560 video_usercopy(struct inode *inode, struct file *file,
561                unsigned int cmd, unsigned long arg,
562                int (*func)(struct inode *inode, struct file *file,
563                            unsigned int cmd, void *arg))
564 {
565         char    sbuf[128];
566         void    *mbuf = NULL;
567         void    *parg = NULL;
568         int     err  = -EINVAL;
569         int     is_ext_ctrl;
570         size_t  ctrls_size = 0;
571         void __user *user_ptr = NULL;
572
573 #ifdef __OLD_VIDIOC_
574         cmd = video_fix_command(cmd);
575 #endif
576         is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
577                        cmd == VIDIOC_TRY_EXT_CTRLS);
578
579         /*  Copy arguments into temp kernel buffer  */
580         switch (_IOC_DIR(cmd)) {
581         case _IOC_NONE:
582                 parg = NULL;
583                 break;
584         case _IOC_READ:
585         case _IOC_WRITE:
586         case (_IOC_WRITE | _IOC_READ):
587                 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
588                         parg = sbuf;
589                 } else {
590                         /* too big to allocate from stack */
591                         mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
592                         if (NULL == mbuf)
593                                 return -ENOMEM;
594                         parg = mbuf;
595                 }
596
597                 err = -EFAULT;
598                 if (_IOC_DIR(cmd) & _IOC_WRITE)
599                         if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
600                                 goto out;
601                 break;
602         }
603         if (is_ext_ctrl) {
604                 struct v4l2_ext_controls *p = parg;
605
606                 /* In case of an error, tell the caller that it wasn't
607                    a specific control that caused it. */
608                 p->error_idx = p->count;
609                 user_ptr = (void __user *)p->controls;
610                 if (p->count) {
611                         ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
612                         /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
613                         mbuf = kmalloc(ctrls_size, GFP_KERNEL);
614                         err = -ENOMEM;
615                         if (NULL == mbuf)
616                                 goto out_ext_ctrl;
617                         err = -EFAULT;
618                         if (copy_from_user(mbuf, user_ptr, ctrls_size))
619                                 goto out_ext_ctrl;
620                         p->controls = mbuf;
621                 }
622         }
623
624         /* call driver */
625         err = func(inode, file, cmd, parg);
626         if (err == -ENOIOCTLCMD)
627                 err = -EINVAL;
628         if (is_ext_ctrl) {
629                 struct v4l2_ext_controls *p = parg;
630
631                 p->controls = (void *)user_ptr;
632                 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
633                         err = -EFAULT;
634                 goto out_ext_ctrl;
635         }
636         if (err < 0)
637                 goto out;
638
639 out_ext_ctrl:
640         /*  Copy results into user buffer  */
641         switch (_IOC_DIR(cmd))
642         {
643         case _IOC_READ:
644         case (_IOC_WRITE | _IOC_READ):
645                 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
646                         err = -EFAULT;
647                 break;
648         }
649
650 out:
651         kfree(mbuf);
652         return err;
653 }
654 EXPORT_SYMBOL(video_usercopy);
655
656 /*
657  * open/release helper functions -- handle exclusive opens
658  * Should be removed soon
659  */
660 int video_exclusive_open(struct inode *inode, struct file *file)
661 {
662         struct  video_device *vfl = video_devdata(file);
663         int retval = 0;
664
665         mutex_lock(&vfl->lock);
666         if (vfl->users) {
667                 retval = -EBUSY;
668         } else {
669                 vfl->users++;
670         }
671         mutex_unlock(&vfl->lock);
672         return retval;
673 }
674 EXPORT_SYMBOL(video_exclusive_open);
675
676 int video_exclusive_release(struct inode *inode, struct file *file)
677 {
678         struct  video_device *vfl = video_devdata(file);
679
680         vfl->users--;
681         return 0;
682 }
683 EXPORT_SYMBOL(video_exclusive_release);
684
685 static void dbgbuf(unsigned int cmd, struct video_device *vfd,
686                                         struct v4l2_buffer *p)
687 {
688         struct v4l2_timecode *tc=&p->timecode;
689
690         dbgarg (cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
691                 "bytesused=%d, flags=0x%08d, "
692                 "field=%0d, sequence=%d, memory=%s, offset/userptr=0x%08lx, length=%d\n",
693                         (p->timestamp.tv_sec/3600),
694                         (int)(p->timestamp.tv_sec/60)%60,
695                         (int)(p->timestamp.tv_sec%60),
696                         p->timestamp.tv_usec,
697                         p->index,
698                         prt_names(p->type, v4l2_type_names),
699                         p->bytesused, p->flags,
700                         p->field, p->sequence,
701                         prt_names(p->memory, v4l2_memory_names),
702                         p->m.userptr, p->length);
703         dbgarg2 ("timecode= %02d:%02d:%02d type=%d, "
704                 "flags=0x%08d, frames=%d, userbits=0x%08x\n",
705                         tc->hours,tc->minutes,tc->seconds,
706                         tc->type, tc->flags, tc->frames, *(__u32 *) tc->userbits);
707 }
708
709 static inline void dbgrect(struct video_device *vfd, char *s,
710                                                         struct v4l2_rect *r)
711 {
712         dbgarg2 ("%sRect start at %dx%d, size= %dx%d\n", s, r->left, r->top,
713                                                 r->width, r->height);
714 };
715
716 static inline void v4l_print_pix_fmt (struct video_device *vfd,
717                                                 struct v4l2_pix_format *fmt)
718 {
719         dbgarg2 ("width=%d, height=%d, format=%c%c%c%c, field=%s, "
720                 "bytesperline=%d sizeimage=%d, colorspace=%d\n",
721                 fmt->width,fmt->height,
722                 (fmt->pixelformat & 0xff),
723                 (fmt->pixelformat >>  8) & 0xff,
724                 (fmt->pixelformat >> 16) & 0xff,
725                 (fmt->pixelformat >> 24) & 0xff,
726                 prt_names(fmt->field, v4l2_field_names),
727                 fmt->bytesperline, fmt->sizeimage, fmt->colorspace);
728 };
729
730
731 static int check_fmt (struct video_device *vfd, enum v4l2_buf_type type)
732 {
733         switch (type) {
734         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
735                 if (vfd->vidioc_try_fmt_cap)
736                         return (0);
737                 break;
738         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
739                 if (vfd->vidioc_try_fmt_overlay)
740                         return (0);
741                 break;
742         case V4L2_BUF_TYPE_VBI_CAPTURE:
743                 if (vfd->vidioc_try_fmt_vbi)
744                         return (0);
745                 break;
746         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
747                 if (vfd->vidioc_try_fmt_vbi_output)
748                         return (0);
749                 break;
750         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
751                 if (vfd->vidioc_try_fmt_vbi_capture)
752                         return (0);
753                 break;
754         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
755                 if (vfd->vidioc_try_fmt_video_output)
756                         return (0);
757                 break;
758         case V4L2_BUF_TYPE_VBI_OUTPUT:
759                 if (vfd->vidioc_try_fmt_vbi_output)
760                         return (0);
761                 break;
762         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
763                 if (vfd->vidioc_try_fmt_output_overlay)
764                         return (0);
765                 break;
766         case V4L2_BUF_TYPE_PRIVATE:
767                 if (vfd->vidioc_try_fmt_type_private)
768                         return (0);
769                 break;
770         }
771         return (-EINVAL);
772 }
773
774 static int __video_do_ioctl(struct inode *inode, struct file *file,
775                 unsigned int cmd, void *arg)
776 {
777         struct video_device *vfd = video_devdata(file);
778         void                 *fh = file->private_data;
779         int                  ret = -EINVAL;
780
781         if ( (vfd->debug & V4L2_DEBUG_IOCTL) &&
782                                 !(vfd->debug & V4L2_DEBUG_IOCTL_ARG)) {
783                 v4l_print_ioctl(vfd->name, cmd);
784                 printk("\n");
785         }
786
787 #ifdef CONFIG_VIDEO_V4L1_COMPAT
788         /***********************************************************
789          Handles calls to the obsoleted V4L1 API
790          Due to the nature of VIDIOCGMBUF, each driver that supports
791          V4L1 should implement its own handler for this ioctl.
792          ***********************************************************/
793
794         /* --- streaming capture ------------------------------------- */
795         if (cmd == VIDIOCGMBUF) {
796                 struct video_mbuf *p=arg;
797
798                 memset(p, 0, sizeof(*p));
799
800                 if (!vfd->vidiocgmbuf)
801                         return ret;
802                 ret=vfd->vidiocgmbuf(file, fh, p);
803                 if (!ret)
804                         dbgarg (cmd, "size=%d, frames=%d, offsets=0x%08lx\n",
805                                                 p->size, p->frames,
806                                                 (unsigned long)p->offsets);
807                 return ret;
808         }
809
810         /********************************************************
811          All other V4L1 calls are handled by v4l1_compat module.
812          Those calls will be translated into V4L2 calls, and
813          __video_do_ioctl will be called again, with one or more
814          V4L2 ioctls.
815          ********************************************************/
816         if (_IOC_TYPE(cmd)=='v')
817                 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
818                                                 __video_do_ioctl);
819 #endif
820
821         switch(cmd) {
822         /* --- capabilities ------------------------------------------ */
823         case VIDIOC_QUERYCAP:
824         {
825                 struct v4l2_capability *cap = (struct v4l2_capability*)arg;
826                 memset(cap, 0, sizeof(*cap));
827
828                 if (!vfd->vidioc_querycap)
829                         break;
830
831                 ret=vfd->vidioc_querycap(file, fh, cap);
832                 if (!ret)
833                         dbgarg (cmd, "driver=%s, card=%s, bus=%s, "
834                                         "version=0x%08x, "
835                                         "capabilities=0x%08x\n",
836                                         cap->driver,cap->card,cap->bus_info,
837                                         cap->version,
838                                         cap->capabilities);
839                 break;
840         }
841
842         /* --- priority ------------------------------------------ */
843         case VIDIOC_G_PRIORITY:
844         {
845                 enum v4l2_priority *p=arg;
846
847                 if (!vfd->vidioc_g_priority)
848                         break;
849                 ret=vfd->vidioc_g_priority(file, fh, p);
850                 if (!ret)
851                         dbgarg(cmd, "priority is %d\n", *p);
852                 break;
853         }
854         case VIDIOC_S_PRIORITY:
855         {
856                 enum v4l2_priority *p=arg;
857
858                 if (!vfd->vidioc_s_priority)
859                         break;
860                 dbgarg(cmd, "setting priority to %d\n", *p);
861                 ret=vfd->vidioc_s_priority(file, fh, *p);
862                 break;
863         }
864
865         /* --- capture ioctls ---------------------------------------- */
866         case VIDIOC_ENUM_FMT:
867         {
868                 struct v4l2_fmtdesc *f = arg;
869                 enum v4l2_buf_type type;
870                 unsigned int index;
871
872                 index = f->index;
873                 type  = f->type;
874                 memset(f,0,sizeof(*f));
875                 f->index = index;
876                 f->type  = type;
877
878                 switch (type) {
879                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
880                         if (vfd->vidioc_enum_fmt_cap)
881                                 ret=vfd->vidioc_enum_fmt_cap(file, fh, f);
882                         break;
883                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
884                         if (vfd->vidioc_enum_fmt_overlay)
885                                 ret=vfd->vidioc_enum_fmt_overlay(file, fh, f);
886                         break;
887                 case V4L2_BUF_TYPE_VBI_CAPTURE:
888                         if (vfd->vidioc_enum_fmt_vbi)
889                                 ret=vfd->vidioc_enum_fmt_vbi(file, fh, f);
890                         break;
891                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
892                         if (vfd->vidioc_enum_fmt_vbi_output)
893                                 ret=vfd->vidioc_enum_fmt_vbi_output(file,
894                                                                 fh, f);
895                         break;
896                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
897                         if (vfd->vidioc_enum_fmt_vbi_capture)
898                                 ret=vfd->vidioc_enum_fmt_vbi_capture(file,
899                                                                 fh, f);
900                         break;
901                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
902                         if (vfd->vidioc_enum_fmt_video_output)
903                                 ret=vfd->vidioc_enum_fmt_video_output(file,
904                                                                 fh, f);
905                         break;
906                 case V4L2_BUF_TYPE_VBI_OUTPUT:
907                         if (vfd->vidioc_enum_fmt_vbi_output)
908                                 ret=vfd->vidioc_enum_fmt_vbi_output(file,
909                                                                 fh, f);
910                         break;
911                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
912                         if (vfd->vidioc_enum_fmt_output_overlay)
913                                 ret=vfd->vidioc_enum_fmt_output_overlay(file, fh, f);
914                         break;
915                 case V4L2_BUF_TYPE_PRIVATE:
916                         if (vfd->vidioc_enum_fmt_type_private)
917                                 ret=vfd->vidioc_enum_fmt_type_private(file,
918                                                                 fh, f);
919                         break;
920                 }
921                 if (!ret)
922                         dbgarg (cmd, "index=%d, type=%d, flags=%d, "
923                                         "pixelformat=%c%c%c%c, description='%s'\n",
924                                         f->index, f->type, f->flags,
925                                         (f->pixelformat & 0xff),
926                                         (f->pixelformat >>  8) & 0xff,
927                                         (f->pixelformat >> 16) & 0xff,
928                                         (f->pixelformat >> 24) & 0xff,
929                                         f->description);
930                 break;
931         }
932         case VIDIOC_G_FMT:
933         {
934                 struct v4l2_format *f = (struct v4l2_format *)arg;
935                 enum v4l2_buf_type type=f->type;
936
937                 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
938                 f->type=type;
939
940                 /* FIXME: Should be one dump per type */
941                 dbgarg (cmd, "type=%s\n", prt_names(type,
942                                         v4l2_type_names));
943
944                 switch (type) {
945                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
946                         if (vfd->vidioc_g_fmt_cap)
947                                 ret=vfd->vidioc_g_fmt_cap(file, fh, f);
948                         if (!ret)
949                                 v4l_print_pix_fmt(vfd,&f->fmt.pix);
950                         break;
951                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
952                         if (vfd->vidioc_g_fmt_overlay)
953                                 ret=vfd->vidioc_g_fmt_overlay(file, fh, f);
954                         break;
955                 case V4L2_BUF_TYPE_VBI_CAPTURE:
956                         if (vfd->vidioc_g_fmt_vbi)
957                                 ret=vfd->vidioc_g_fmt_vbi(file, fh, f);
958                         break;
959                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
960                         if (vfd->vidioc_g_fmt_vbi_output)
961                                 ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
962                         break;
963                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
964                         if (vfd->vidioc_g_fmt_vbi_capture)
965                                 ret=vfd->vidioc_g_fmt_vbi_capture(file, fh, f);
966                         break;
967                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
968                         if (vfd->vidioc_g_fmt_video_output)
969                                 ret=vfd->vidioc_g_fmt_video_output(file,
970                                                                 fh, f);
971                         break;
972                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
973                         if (vfd->vidioc_g_fmt_output_overlay)
974                                 ret=vfd->vidioc_g_fmt_output_overlay(file, fh, f);
975                         break;
976                 case V4L2_BUF_TYPE_VBI_OUTPUT:
977                         if (vfd->vidioc_g_fmt_vbi_output)
978                                 ret=vfd->vidioc_g_fmt_vbi_output(file, fh, f);
979                         break;
980                 case V4L2_BUF_TYPE_PRIVATE:
981                         if (vfd->vidioc_g_fmt_type_private)
982                                 ret=vfd->vidioc_g_fmt_type_private(file,
983                                                                 fh, f);
984                         break;
985                 }
986
987                 break;
988         }
989         case VIDIOC_S_FMT:
990         {
991                 struct v4l2_format *f = (struct v4l2_format *)arg;
992
993                 /* FIXME: Should be one dump per type */
994                 dbgarg (cmd, "type=%s\n", prt_names(f->type,
995                                         v4l2_type_names));
996
997                 switch (f->type) {
998                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
999                         v4l_print_pix_fmt(vfd,&f->fmt.pix);
1000                         if (vfd->vidioc_s_fmt_cap)
1001                                 ret=vfd->vidioc_s_fmt_cap(file, fh, f);
1002                         break;
1003                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1004                         if (vfd->vidioc_s_fmt_overlay)
1005                                 ret=vfd->vidioc_s_fmt_overlay(file, fh, f);
1006                         break;
1007                 case V4L2_BUF_TYPE_VBI_CAPTURE:
1008                         if (vfd->vidioc_s_fmt_vbi)
1009                                 ret=vfd->vidioc_s_fmt_vbi(file, fh, f);
1010                         break;
1011                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1012                         if (vfd->vidioc_s_fmt_vbi_output)
1013                                 ret=vfd->vidioc_s_fmt_vbi_output(file, fh, f);
1014                         break;
1015                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1016                         if (vfd->vidioc_s_fmt_vbi_capture)
1017                                 ret=vfd->vidioc_s_fmt_vbi_capture(file, fh, f);
1018                         break;
1019                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1020                         if (vfd->vidioc_s_fmt_video_output)
1021                                 ret=vfd->vidioc_s_fmt_video_output(file,
1022                                                                 fh, f);
1023                         break;
1024                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1025                         if (vfd->vidioc_s_fmt_output_overlay)
1026                                 ret=vfd->vidioc_s_fmt_output_overlay(file, fh, f);
1027                         break;
1028                 case V4L2_BUF_TYPE_VBI_OUTPUT:
1029                         if (vfd->vidioc_s_fmt_vbi_output)
1030                                 ret=vfd->vidioc_s_fmt_vbi_output(file,
1031                                                                 fh, f);
1032                         break;
1033                 case V4L2_BUF_TYPE_PRIVATE:
1034                         if (vfd->vidioc_s_fmt_type_private)
1035                                 ret=vfd->vidioc_s_fmt_type_private(file,
1036                                                                 fh, f);
1037                         break;
1038                 }
1039                 break;
1040         }
1041         case VIDIOC_TRY_FMT:
1042         {
1043                 struct v4l2_format *f = (struct v4l2_format *)arg;
1044
1045                 /* FIXME: Should be one dump per type */
1046                 dbgarg (cmd, "type=%s\n", prt_names(f->type,
1047                                                 v4l2_type_names));
1048                 switch (f->type) {
1049                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1050                         if (vfd->vidioc_try_fmt_cap)
1051                                 ret=vfd->vidioc_try_fmt_cap(file, fh, f);
1052                         if (!ret)
1053                                 v4l_print_pix_fmt(vfd,&f->fmt.pix);
1054                         break;
1055                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1056                         if (vfd->vidioc_try_fmt_overlay)
1057                                 ret=vfd->vidioc_try_fmt_overlay(file, fh, f);
1058                         break;
1059                 case V4L2_BUF_TYPE_VBI_CAPTURE:
1060                         if (vfd->vidioc_try_fmt_vbi)
1061                                 ret=vfd->vidioc_try_fmt_vbi(file, fh, f);
1062                         break;
1063                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1064                         if (vfd->vidioc_try_fmt_vbi_output)
1065                                 ret=vfd->vidioc_try_fmt_vbi_output(file,
1066                                                                 fh, f);
1067                         break;
1068                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1069                         if (vfd->vidioc_try_fmt_vbi_capture)
1070                                 ret=vfd->vidioc_try_fmt_vbi_capture(file,
1071                                                                 fh, f);
1072                         break;
1073                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1074                         if (vfd->vidioc_try_fmt_video_output)
1075                                 ret=vfd->vidioc_try_fmt_video_output(file,
1076                                                                 fh, f);
1077                         break;
1078                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1079                         if (vfd->vidioc_try_fmt_output_overlay)
1080                                 ret=vfd->vidioc_try_fmt_output_overlay(file, fh, f);
1081                         break;
1082                 case V4L2_BUF_TYPE_VBI_OUTPUT:
1083                         if (vfd->vidioc_try_fmt_vbi_output)
1084                                 ret=vfd->vidioc_try_fmt_vbi_output(file,
1085                                                                 fh, f);
1086                         break;
1087                 case V4L2_BUF_TYPE_PRIVATE:
1088                         if (vfd->vidioc_try_fmt_type_private)
1089                                 ret=vfd->vidioc_try_fmt_type_private(file,
1090                                                                 fh, f);
1091                         break;
1092                 }
1093
1094                 break;
1095         }
1096         /* FIXME: Those buf reqs could be handled here,
1097            with some changes on videobuf to allow its header to be included at
1098            videodev2.h or being merged at videodev2.
1099          */
1100         case VIDIOC_REQBUFS:
1101         {
1102                 struct v4l2_requestbuffers *p=arg;
1103
1104                 if (!vfd->vidioc_reqbufs)
1105                         break;
1106                 ret = check_fmt (vfd, p->type);
1107                 if (ret)
1108                         break;
1109
1110                 ret=vfd->vidioc_reqbufs(file, fh, p);
1111                 dbgarg (cmd, "count=%d, type=%s, memory=%s\n",
1112                                 p->count,
1113                                 prt_names(p->type, v4l2_type_names),
1114                                 prt_names(p->memory, v4l2_memory_names));
1115                 break;
1116         }
1117         case VIDIOC_QUERYBUF:
1118         {
1119                 struct v4l2_buffer *p=arg;
1120
1121                 if (!vfd->vidioc_querybuf)
1122                         break;
1123                 ret = check_fmt (vfd, p->type);
1124                 if (ret)
1125                         break;
1126
1127                 ret=vfd->vidioc_querybuf(file, fh, p);
1128                 if (!ret)
1129                         dbgbuf(cmd,vfd,p);
1130                 break;
1131         }
1132         case VIDIOC_QBUF:
1133         {
1134                 struct v4l2_buffer *p=arg;
1135
1136                 if (!vfd->vidioc_qbuf)
1137                         break;
1138                 ret = check_fmt (vfd, p->type);
1139                 if (ret)
1140                         break;
1141
1142                 ret=vfd->vidioc_qbuf(file, fh, p);
1143                 if (!ret)
1144                         dbgbuf(cmd,vfd,p);
1145                 break;
1146         }
1147         case VIDIOC_DQBUF:
1148         {
1149                 struct v4l2_buffer *p=arg;
1150                 if (!vfd->vidioc_dqbuf)
1151                         break;
1152                 ret = check_fmt (vfd, p->type);
1153                 if (ret)
1154                         break;
1155
1156                 ret=vfd->vidioc_dqbuf(file, fh, p);
1157                 if (!ret)
1158                         dbgbuf(cmd,vfd,p);
1159                 break;
1160         }
1161         case VIDIOC_OVERLAY:
1162         {
1163                 int *i = arg;
1164
1165                 if (!vfd->vidioc_overlay)
1166                         break;
1167                 dbgarg (cmd, "value=%d\n",*i);
1168                 ret=vfd->vidioc_overlay(file, fh, *i);
1169                 break;
1170         }
1171         case VIDIOC_G_FBUF:
1172         {
1173                 struct v4l2_framebuffer *p=arg;
1174                 if (!vfd->vidioc_g_fbuf)
1175                         break;
1176                 ret=vfd->vidioc_g_fbuf(file, fh, arg);
1177                 if (!ret) {
1178                         dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
1179                                         p->capability,p->flags,
1180                                         (unsigned long)p->base);
1181                         v4l_print_pix_fmt (vfd, &p->fmt);
1182                 }
1183                 break;
1184         }
1185         case VIDIOC_S_FBUF:
1186         {
1187                 struct v4l2_framebuffer *p=arg;
1188                 if (!vfd->vidioc_s_fbuf)
1189                         break;
1190
1191                 dbgarg (cmd, "capability=%d, flags=%d, base=0x%08lx\n",
1192                                 p->capability,p->flags,(unsigned long)p->base);
1193                 v4l_print_pix_fmt (vfd, &p->fmt);
1194                 ret=vfd->vidioc_s_fbuf(file, fh, arg);
1195
1196                 break;
1197         }
1198         case VIDIOC_STREAMON:
1199         {
1200                 enum v4l2_buf_type i = *(int *)arg;
1201                 if (!vfd->vidioc_streamon)
1202                         break;
1203                 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
1204                 ret=vfd->vidioc_streamon(file, fh,i);
1205                 break;
1206         }
1207         case VIDIOC_STREAMOFF:
1208         {
1209                 enum v4l2_buf_type i = *(int *)arg;
1210
1211                 if (!vfd->vidioc_streamoff)
1212                         break;
1213                 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
1214                 ret=vfd->vidioc_streamoff(file, fh, i);
1215                 break;
1216         }
1217         /* ---------- tv norms ---------- */
1218         case VIDIOC_ENUMSTD:
1219         {
1220                 struct v4l2_standard *p = arg;
1221                 v4l2_std_id id = vfd->tvnorms,curr_id=0;
1222                 unsigned int index = p->index,i;
1223
1224                 if (index<0) {
1225                         ret=-EINVAL;
1226                         break;
1227                 }
1228
1229                 /* Return norm array on a canonical way */
1230                 for (i=0;i<= index && id; i++) {
1231                         if ( (id & V4L2_STD_PAL) == V4L2_STD_PAL) {
1232                                 curr_id = V4L2_STD_PAL;
1233                         } else if ( (id & V4L2_STD_PAL_BG) == V4L2_STD_PAL_BG) {
1234                                 curr_id = V4L2_STD_PAL_BG;
1235                         } else if ( (id & V4L2_STD_PAL_DK) == V4L2_STD_PAL_DK) {
1236                                 curr_id = V4L2_STD_PAL_DK;
1237                         } else if ( (id & V4L2_STD_PAL_B) == V4L2_STD_PAL_B) {
1238                                 curr_id = V4L2_STD_PAL_B;
1239                         } else if ( (id & V4L2_STD_PAL_B1) == V4L2_STD_PAL_B1) {
1240                                 curr_id = V4L2_STD_PAL_B1;
1241                         } else if ( (id & V4L2_STD_PAL_G) == V4L2_STD_PAL_G) {
1242                                 curr_id = V4L2_STD_PAL_G;
1243                         } else if ( (id & V4L2_STD_PAL_H) == V4L2_STD_PAL_H) {
1244                                 curr_id = V4L2_STD_PAL_H;
1245                         } else if ( (id & V4L2_STD_PAL_I) == V4L2_STD_PAL_I) {
1246                                 curr_id = V4L2_STD_PAL_I;
1247                         } else if ( (id & V4L2_STD_PAL_D) == V4L2_STD_PAL_D) {
1248                                 curr_id = V4L2_STD_PAL_D;
1249                         } else if ( (id & V4L2_STD_PAL_D1) == V4L2_STD_PAL_D1) {
1250                                 curr_id = V4L2_STD_PAL_D1;
1251                         } else if ( (id & V4L2_STD_PAL_K) == V4L2_STD_PAL_K) {
1252                                 curr_id = V4L2_STD_PAL_K;
1253                         } else if ( (id & V4L2_STD_PAL_M) == V4L2_STD_PAL_M) {
1254                                 curr_id = V4L2_STD_PAL_M;
1255                         } else if ( (id & V4L2_STD_PAL_N) == V4L2_STD_PAL_N) {
1256                                 curr_id = V4L2_STD_PAL_N;
1257                         } else if ( (id & V4L2_STD_PAL_Nc) == V4L2_STD_PAL_Nc) {
1258                                 curr_id = V4L2_STD_PAL_Nc;
1259                         } else if ( (id & V4L2_STD_PAL_60) == V4L2_STD_PAL_60) {
1260                                 curr_id = V4L2_STD_PAL_60;
1261                         } else if ( (id & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
1262                                 curr_id = V4L2_STD_NTSC;
1263                         } else if ( (id & V4L2_STD_NTSC_M) == V4L2_STD_NTSC_M) {
1264                                 curr_id = V4L2_STD_NTSC_M;
1265                         } else if ( (id & V4L2_STD_NTSC_M_JP) == V4L2_STD_NTSC_M_JP) {
1266                                 curr_id = V4L2_STD_NTSC_M_JP;
1267                         } else if ( (id & V4L2_STD_NTSC_443) == V4L2_STD_NTSC_443) {
1268                                 curr_id = V4L2_STD_NTSC_443;
1269                         } else if ( (id & V4L2_STD_NTSC_M_KR) == V4L2_STD_NTSC_M_KR) {
1270                                 curr_id = V4L2_STD_NTSC_M_KR;
1271                         } else if ( (id & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
1272                                 curr_id = V4L2_STD_SECAM;
1273                         } else if ( (id & V4L2_STD_SECAM_DK) == V4L2_STD_SECAM_DK) {
1274                                 curr_id = V4L2_STD_SECAM_DK;
1275                         } else if ( (id & V4L2_STD_SECAM_B) == V4L2_STD_SECAM_B) {
1276                                 curr_id = V4L2_STD_SECAM_B;
1277                         } else if ( (id & V4L2_STD_SECAM_D) == V4L2_STD_SECAM_D) {
1278                                 curr_id = V4L2_STD_SECAM_D;
1279                         } else if ( (id & V4L2_STD_SECAM_G) == V4L2_STD_SECAM_G) {
1280                                 curr_id = V4L2_STD_SECAM_G;
1281                         } else if ( (id & V4L2_STD_SECAM_H) == V4L2_STD_SECAM_H) {
1282                                 curr_id = V4L2_STD_SECAM_H;
1283                         } else if ( (id & V4L2_STD_SECAM_K) == V4L2_STD_SECAM_K) {
1284                                 curr_id = V4L2_STD_SECAM_K;
1285                         } else if ( (id & V4L2_STD_SECAM_K1) == V4L2_STD_SECAM_K1) {
1286                                 curr_id = V4L2_STD_SECAM_K1;
1287                         } else if ( (id & V4L2_STD_SECAM_L) == V4L2_STD_SECAM_L) {
1288                                 curr_id = V4L2_STD_SECAM_L;
1289                         } else if ( (id & V4L2_STD_SECAM_LC) == V4L2_STD_SECAM_LC) {
1290                                 curr_id = V4L2_STD_SECAM_LC;
1291                         } else {
1292                                 break;
1293                         }
1294                         id &= ~curr_id;
1295                 }
1296                 if (i<=index)
1297                         return -EINVAL;
1298
1299                 v4l2_video_std_construct(p, curr_id,v4l2_norm_to_name(curr_id));
1300                 p->index = index;
1301
1302                 dbgarg (cmd, "index=%d, id=%Ld, name=%s, fps=%d/%d, "
1303                                 "framelines=%d\n", p->index,
1304                                 (unsigned long long)p->id, p->name,
1305                                 p->frameperiod.numerator,
1306                                 p->frameperiod.denominator,
1307                                 p->framelines);
1308
1309                 ret=0;
1310                 break;
1311         }
1312         case VIDIOC_G_STD:
1313         {
1314                 v4l2_std_id *id = arg;
1315
1316                 *id = vfd->current_norm;
1317
1318                 dbgarg (cmd, "value=%08Lx\n", (long long unsigned) *id);
1319
1320                 ret=0;
1321                 break;
1322         }
1323         case VIDIOC_S_STD:
1324         {
1325                 v4l2_std_id *id = arg,norm;
1326
1327                 dbgarg (cmd, "value=%08Lx\n", (long long unsigned) *id);
1328
1329                 norm = (*id) & vfd->tvnorms;
1330                 if ( vfd->tvnorms && !norm)     /* Check if std is supported */
1331                         break;
1332
1333                 /* Calls the specific handler */
1334                 if (vfd->vidioc_s_std)
1335                         ret=vfd->vidioc_s_std(file, fh, &norm);
1336                 else
1337                         ret=-EINVAL;
1338
1339                 /* Updates standard information */
1340                 if (ret>=0)
1341                         vfd->current_norm=norm;
1342
1343                 break;
1344         }
1345         case VIDIOC_QUERYSTD:
1346         {
1347                 v4l2_std_id *p=arg;
1348
1349                 if (!vfd->vidioc_querystd)
1350                         break;
1351                 ret=vfd->vidioc_querystd(file, fh, arg);
1352                 if (!ret)
1353                         dbgarg (cmd, "detected std=%08Lx\n",
1354                                                 (unsigned long long)*p);
1355                 break;
1356         }
1357         /* ------ input switching ---------- */
1358         /* FIXME: Inputs can be handled inside videodev2 */
1359         case VIDIOC_ENUMINPUT:
1360         {
1361                 struct v4l2_input *p=arg;
1362                 int i=p->index;
1363
1364                 if (!vfd->vidioc_enum_input)
1365                         break;
1366                 memset(p, 0, sizeof(*p));
1367                 p->index=i;
1368
1369                 ret=vfd->vidioc_enum_input(file, fh, p);
1370                 if (!ret)
1371                         dbgarg (cmd, "index=%d, name=%s, type=%d, "
1372                                         "audioset=%d, "
1373                                         "tuner=%d, std=%08Lx, status=%d\n",
1374                                         p->index,p->name,p->type,p->audioset,
1375                                         p->tuner,
1376                                         (unsigned long long)p->std,
1377                                         p->status);
1378                 break;
1379         }
1380         case VIDIOC_G_INPUT:
1381         {
1382                 unsigned int *i = arg;
1383
1384                 if (!vfd->vidioc_g_input)
1385                         break;
1386                 ret=vfd->vidioc_g_input(file, fh, i);
1387                 if (!ret)
1388                         dbgarg (cmd, "value=%d\n",*i);
1389                 break;
1390         }
1391         case VIDIOC_S_INPUT:
1392         {
1393                 unsigned int *i = arg;
1394
1395                 if (!vfd->vidioc_s_input)
1396                         break;
1397                 dbgarg (cmd, "value=%d\n",*i);
1398                 ret=vfd->vidioc_s_input(file, fh, *i);
1399                 break;
1400         }
1401
1402         /* ------ output switching ---------- */
1403         case VIDIOC_G_OUTPUT:
1404         {
1405                 unsigned int *i = arg;
1406
1407                 if (!vfd->vidioc_g_output)
1408                         break;
1409                 ret=vfd->vidioc_g_output(file, fh, i);
1410                 if (!ret)
1411                         dbgarg (cmd, "value=%d\n",*i);
1412                 break;
1413         }
1414         case VIDIOC_S_OUTPUT:
1415         {
1416                 unsigned int *i = arg;
1417
1418                 if (!vfd->vidioc_s_output)
1419                         break;
1420                 dbgarg (cmd, "value=%d\n",*i);
1421                 ret=vfd->vidioc_s_output(file, fh, *i);
1422                 break;
1423         }
1424
1425         /* --- controls ---------------------------------------------- */
1426         case VIDIOC_QUERYCTRL:
1427         {
1428                 struct v4l2_queryctrl *p=arg;
1429
1430                 if (!vfd->vidioc_queryctrl)
1431                         break;
1432                 ret=vfd->vidioc_queryctrl(file, fh, p);
1433
1434                 if (!ret)
1435                         dbgarg (cmd, "id=%d, type=%d, name=%s, "
1436                                         "min/max=%d/%d,"
1437                                         " step=%d, default=%d, flags=0x%08x\n",
1438                                         p->id,p->type,p->name,p->minimum,
1439                                         p->maximum,p->step,p->default_value,
1440                                         p->flags);
1441                 break;
1442         }
1443         case VIDIOC_G_CTRL:
1444         {
1445                 struct v4l2_control *p = arg;
1446
1447                 if (!vfd->vidioc_g_ctrl)
1448                         break;
1449                 dbgarg(cmd, "Enum for index=%d\n", p->id);
1450
1451                 ret=vfd->vidioc_g_ctrl(file, fh, p);
1452                 if (!ret)
1453                         dbgarg2 ( "id=%d, value=%d\n", p->id, p->value);
1454                 break;
1455         }
1456         case VIDIOC_S_CTRL:
1457         {
1458                 struct v4l2_control *p = arg;
1459
1460                 if (!vfd->vidioc_s_ctrl)
1461                         break;
1462                 dbgarg (cmd, "id=%d, value=%d\n", p->id, p->value);
1463
1464                 ret=vfd->vidioc_s_ctrl(file, fh, p);
1465                 break;
1466         }
1467         case VIDIOC_G_EXT_CTRLS:
1468         {
1469                 struct v4l2_ext_controls *p = arg;
1470
1471                 if (vfd->vidioc_g_ext_ctrls) {
1472                         dbgarg(cmd, "count=%d\n", p->count);
1473
1474                         ret=vfd->vidioc_g_ext_ctrls(file, fh, p);
1475                 }
1476                 break;
1477         }
1478         case VIDIOC_S_EXT_CTRLS:
1479         {
1480                 struct v4l2_ext_controls *p = arg;
1481
1482                 if (vfd->vidioc_s_ext_ctrls) {
1483                         dbgarg(cmd, "count=%d\n", p->count);
1484
1485                         ret=vfd->vidioc_s_ext_ctrls(file, fh, p);
1486                 }
1487                 break;
1488         }
1489         case VIDIOC_TRY_EXT_CTRLS:
1490         {
1491                 struct v4l2_ext_controls *p = arg;
1492
1493                 if (vfd->vidioc_try_ext_ctrls) {
1494                         dbgarg(cmd, "count=%d\n", p->count);
1495
1496                         ret=vfd->vidioc_try_ext_ctrls(file, fh, p);
1497                 }
1498                 break;
1499         }
1500         case VIDIOC_QUERYMENU:
1501         {
1502                 struct v4l2_querymenu *p=arg;
1503                 if (!vfd->vidioc_querymenu)
1504                         break;
1505                 ret=vfd->vidioc_querymenu(file, fh, p);
1506                 if (!ret)
1507                         dbgarg (cmd, "id=%d, index=%d, name=%s\n",
1508                                                 p->id,p->index,p->name);
1509                 break;
1510         }
1511         /* --- audio ---------------------------------------------- */
1512         case VIDIOC_ENUMAUDIO:
1513         {
1514                 struct v4l2_audio *p=arg;
1515
1516                 if (!vfd->vidioc_enumaudio)
1517                         break;
1518                 dbgarg(cmd, "Enum for index=%d\n", p->index);
1519                 ret=vfd->vidioc_enumaudio(file, fh, p);
1520                 if (!ret)
1521                         dbgarg2("index=%d, name=%s, capability=%d, "
1522                                         "mode=%d\n",p->index,p->name,
1523                                         p->capability, p->mode);
1524                 break;
1525         }
1526         case VIDIOC_G_AUDIO:
1527         {
1528                 struct v4l2_audio *p=arg;
1529                 __u32 index=p->index;
1530
1531                 if (!vfd->vidioc_g_audio)
1532                         break;
1533
1534                 memset(p,0,sizeof(*p));
1535                 p->index=index;
1536                 dbgarg(cmd, "Get for index=%d\n", p->index);
1537                 ret=vfd->vidioc_g_audio(file, fh, p);
1538                 if (!ret)
1539                         dbgarg2("index=%d, name=%s, capability=%d, "
1540                                         "mode=%d\n",p->index,
1541                                         p->name,p->capability, p->mode);
1542                 break;
1543         }
1544         case VIDIOC_S_AUDIO:
1545         {
1546                 struct v4l2_audio *p=arg;
1547
1548                 if (!vfd->vidioc_s_audio)
1549                         break;
1550                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1551                                         "mode=%d\n", p->index, p->name,
1552                                         p->capability, p->mode);
1553                 ret=vfd->vidioc_s_audio(file, fh, p);
1554                 break;
1555         }
1556         case VIDIOC_ENUMAUDOUT:
1557         {
1558                 struct v4l2_audioout *p=arg;
1559
1560                 if (!vfd->vidioc_enumaudout)
1561                         break;
1562                 dbgarg(cmd, "Enum for index=%d\n", p->index);
1563                 ret=vfd->vidioc_enumaudout(file, fh, p);
1564                 if (!ret)
1565                         dbgarg2("index=%d, name=%s, capability=%d, "
1566                                         "mode=%d\n", p->index, p->name,
1567                                         p->capability,p->mode);
1568                 break;
1569         }
1570         case VIDIOC_G_AUDOUT:
1571         {
1572                 struct v4l2_audioout *p=arg;
1573
1574                 if (!vfd->vidioc_g_audout)
1575                         break;
1576                 dbgarg(cmd, "Enum for index=%d\n", p->index);
1577                 ret=vfd->vidioc_g_audout(file, fh, p);
1578                 if (!ret)
1579                         dbgarg2("index=%d, name=%s, capability=%d, "
1580                                         "mode=%d\n", p->index, p->name,
1581                                         p->capability,p->mode);
1582                 break;
1583         }
1584         case VIDIOC_S_AUDOUT:
1585         {
1586                 struct v4l2_audioout *p=arg;
1587
1588                 if (!vfd->vidioc_s_audout)
1589                         break;
1590                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1591                                         "mode=%d\n", p->index, p->name,
1592                                         p->capability,p->mode);
1593
1594                 ret=vfd->vidioc_s_audout(file, fh, p);
1595                 break;
1596         }
1597         case VIDIOC_G_MODULATOR:
1598         {
1599                 struct v4l2_modulator *p=arg;
1600                 if (!vfd->vidioc_g_modulator)
1601                         break;
1602                 ret=vfd->vidioc_g_modulator(file, fh, p);
1603                 if (!ret)
1604                         dbgarg(cmd, "index=%d, name=%s, "
1605                                         "capability=%d, rangelow=%d,"
1606                                         " rangehigh=%d, txsubchans=%d\n",
1607                                         p->index, p->name,p->capability,
1608                                         p->rangelow, p->rangehigh,
1609                                         p->txsubchans);
1610                 break;
1611         }
1612         case VIDIOC_S_MODULATOR:
1613         {
1614                 struct v4l2_modulator *p=arg;
1615                 if (!vfd->vidioc_s_modulator)
1616                         break;
1617                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1618                                 "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1619                                 p->index, p->name,p->capability,p->rangelow,
1620                                 p->rangehigh,p->txsubchans);
1621                         ret=vfd->vidioc_s_modulator(file, fh, p);
1622                 break;
1623         }
1624         case VIDIOC_G_CROP:
1625         {
1626                 struct v4l2_crop *p=arg;
1627                 if (!vfd->vidioc_g_crop)
1628                         break;
1629                 ret=vfd->vidioc_g_crop(file, fh, p);
1630                 if (!ret) {
1631                         dbgarg(cmd, "type=%d\n", p->type);
1632                         dbgrect(vfd, "", &p->c);
1633                 }
1634                 break;
1635         }
1636         case VIDIOC_S_CROP:
1637         {
1638                 struct v4l2_crop *p=arg;
1639                 if (!vfd->vidioc_s_crop)
1640                         break;
1641                 dbgarg(cmd, "type=%d\n", p->type);
1642                 dbgrect(vfd, "", &p->c);
1643                 ret=vfd->vidioc_s_crop(file, fh, p);
1644                 break;
1645         }
1646         case VIDIOC_CROPCAP:
1647         {
1648                 struct v4l2_cropcap *p=arg;
1649                 /*FIXME: Should also show v4l2_fract pixelaspect */
1650                 if (!vfd->vidioc_cropcap)
1651                         break;
1652                 dbgarg(cmd, "type=%d\n", p->type);
1653                 dbgrect(vfd, "bounds ", &p->bounds);
1654                 dbgrect(vfd, "defrect ", &p->defrect);
1655                 ret=vfd->vidioc_cropcap(file, fh, p);
1656                 break;
1657         }
1658         case VIDIOC_G_JPEGCOMP:
1659         {
1660                 struct v4l2_jpegcompression *p=arg;
1661                 if (!vfd->vidioc_g_jpegcomp)
1662                         break;
1663                 ret=vfd->vidioc_g_jpegcomp(file, fh, p);
1664                 if (!ret)
1665                         dbgarg (cmd, "quality=%d, APPn=%d, "
1666                                                 "APP_len=%d, COM_len=%d, "
1667                                                 "jpeg_markers=%d\n",
1668                                                 p->quality,p->APPn,p->APP_len,
1669                                                 p->COM_len,p->jpeg_markers);
1670                 break;
1671         }
1672         case VIDIOC_S_JPEGCOMP:
1673         {
1674                 struct v4l2_jpegcompression *p=arg;
1675                 if (!vfd->vidioc_g_jpegcomp)
1676                         break;
1677                 dbgarg (cmd, "quality=%d, APPn=%d, APP_len=%d, "
1678                                         "COM_len=%d, jpeg_markers=%d\n",
1679                                         p->quality,p->APPn,p->APP_len,
1680                                         p->COM_len,p->jpeg_markers);
1681                         ret=vfd->vidioc_s_jpegcomp(file, fh, p);
1682                 break;
1683         }
1684         case VIDIOC_G_ENC_INDEX:
1685         {
1686                 struct v4l2_enc_idx *p=arg;
1687
1688                 if (!vfd->vidioc_g_enc_index)
1689                         break;
1690                 ret=vfd->vidioc_g_enc_index(file, fh, p);
1691                 if (!ret)
1692                         dbgarg (cmd, "entries=%d, entries_cap=%d\n",
1693                                         p->entries,p->entries_cap);
1694                 break;
1695         }
1696         case VIDIOC_ENCODER_CMD:
1697         {
1698                 struct v4l2_encoder_cmd *p=arg;
1699
1700                 if (!vfd->vidioc_encoder_cmd)
1701                         break;
1702                 ret=vfd->vidioc_encoder_cmd(file, fh, p);
1703                 if (!ret)
1704                         dbgarg (cmd, "cmd=%d, flags=%d\n",
1705                                         p->cmd,p->flags);
1706                 break;
1707         }
1708         case VIDIOC_TRY_ENCODER_CMD:
1709         {
1710                 struct v4l2_encoder_cmd *p=arg;
1711
1712                 if (!vfd->vidioc_try_encoder_cmd)
1713                         break;
1714                 ret=vfd->vidioc_try_encoder_cmd(file, fh, p);
1715                 if (!ret)
1716                         dbgarg (cmd, "cmd=%d, flags=%d\n",
1717                                         p->cmd,p->flags);
1718                 break;
1719         }
1720         case VIDIOC_G_PARM:
1721         {
1722                 struct v4l2_streamparm *p=arg;
1723                 __u32 type=p->type;
1724
1725                 memset(p,0,sizeof(*p));
1726                 p->type=type;
1727
1728                 if (vfd->vidioc_g_parm) {
1729                         ret=vfd->vidioc_g_parm(file, fh, p);
1730                 } else {
1731                         struct v4l2_standard s;
1732
1733                         if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1734                                 return -EINVAL;
1735
1736                         v4l2_video_std_construct(&s, vfd->current_norm,
1737                                                  v4l2_norm_to_name(vfd->current_norm));
1738
1739                         p->parm.capture.timeperframe = s.frameperiod;
1740                         ret=0;
1741                 }
1742
1743                 dbgarg (cmd, "type=%d\n", p->type);
1744                 break;
1745         }
1746         case VIDIOC_S_PARM:
1747         {
1748                 struct v4l2_streamparm *p=arg;
1749                 if (!vfd->vidioc_s_parm)
1750                         break;
1751                 dbgarg (cmd, "type=%d\n", p->type);
1752                 ret=vfd->vidioc_s_parm(file, fh, p);
1753                 break;
1754         }
1755         case VIDIOC_G_TUNER:
1756         {
1757                 struct v4l2_tuner *p=arg;
1758                 __u32 index=p->index;
1759
1760                 if (!vfd->vidioc_g_tuner)
1761                         break;
1762
1763                 memset(p,0,sizeof(*p));
1764                 p->index=index;
1765
1766                 ret=vfd->vidioc_g_tuner(file, fh, p);
1767                 if (!ret)
1768                         dbgarg (cmd, "index=%d, name=%s, type=%d, "
1769                                         "capability=%d, rangelow=%d, "
1770                                         "rangehigh=%d, signal=%d, afc=%d, "
1771                                         "rxsubchans=%d, audmode=%d\n",
1772                                         p->index, p->name, p->type,
1773                                         p->capability, p->rangelow,
1774                                         p->rangehigh, p->rxsubchans,
1775                                         p->audmode, p->signal, p->afc);
1776                 break;
1777         }
1778         case VIDIOC_S_TUNER:
1779         {
1780                 struct v4l2_tuner *p=arg;
1781                 if (!vfd->vidioc_s_tuner)
1782                         break;
1783                 dbgarg (cmd, "index=%d, name=%s, type=%d, "
1784                                 "capability=%d, rangelow=%d, rangehigh=%d, "
1785                                 "signal=%d, afc=%d, rxsubchans=%d, "
1786                                 "audmode=%d\n",p->index, p->name, p->type,
1787                                 p->capability, p->rangelow,p->rangehigh,
1788                                 p->rxsubchans, p->audmode, p->signal,
1789                                 p->afc);
1790                 ret=vfd->vidioc_s_tuner(file, fh, p);
1791                 break;
1792         }
1793         case VIDIOC_G_FREQUENCY:
1794         {
1795                 struct v4l2_frequency *p=arg;
1796                 if (!vfd->vidioc_g_frequency)
1797                         break;
1798
1799                 memset(p,0,sizeof(*p));
1800
1801                 ret=vfd->vidioc_g_frequency(file, fh, p);
1802                 if (!ret)
1803                         dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1804                                                 p->tuner,p->type,p->frequency);
1805                 break;
1806         }
1807         case VIDIOC_S_FREQUENCY:
1808         {
1809                 struct v4l2_frequency *p=arg;
1810                 if (!vfd->vidioc_s_frequency)
1811                         break;
1812                 dbgarg (cmd, "tuner=%d, type=%d, frequency=%d\n",
1813                                 p->tuner,p->type,p->frequency);
1814                 ret=vfd->vidioc_s_frequency(file, fh, p);
1815                 break;
1816         }
1817         case VIDIOC_G_SLICED_VBI_CAP:
1818         {
1819                 struct v4l2_sliced_vbi_cap *p=arg;
1820                 if (!vfd->vidioc_g_sliced_vbi_cap)
1821                         break;
1822                 ret=vfd->vidioc_g_sliced_vbi_cap(file, fh, p);
1823                 if (!ret)
1824                         dbgarg (cmd, "service_set=%d\n", p->service_set);
1825                 break;
1826         }
1827         case VIDIOC_LOG_STATUS:
1828         {
1829                 if (!vfd->vidioc_log_status)
1830                         break;
1831                 ret=vfd->vidioc_log_status(file, fh);
1832                 break;
1833         }
1834 #ifdef CONFIG_VIDEO_ADV_DEBUG
1835         case VIDIOC_DBG_G_REGISTER:
1836         {
1837                 struct v4l2_register *p=arg;
1838                 if (!capable(CAP_SYS_ADMIN))
1839                         ret=-EPERM;
1840                 else if (vfd->vidioc_g_register)
1841                         ret=vfd->vidioc_g_register(file, fh, p);
1842                 break;
1843         }
1844         case VIDIOC_DBG_S_REGISTER:
1845         {
1846                 struct v4l2_register *p=arg;
1847                 if (!capable(CAP_SYS_ADMIN))
1848                         ret=-EPERM;
1849                 else if (vfd->vidioc_s_register)
1850                         ret=vfd->vidioc_s_register(file, fh, p);
1851                 break;
1852         }
1853 #endif
1854         case VIDIOC_G_CHIP_IDENT:
1855         {
1856                 struct v4l2_chip_ident *p=arg;
1857                 if (!vfd->vidioc_g_chip_ident)
1858                         break;
1859                 ret=vfd->vidioc_g_chip_ident(file, fh, p);
1860                 if (!ret)
1861                         dbgarg (cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
1862                 break;
1863         }
1864         default:
1865         {
1866                 if (!vfd->vidioc_default)
1867                         break;
1868                 ret = vfd->vidioc_default(file, fh, cmd, arg);
1869                 break;
1870         }
1871         } /* switch */
1872
1873         if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
1874                 if (ret<0) {
1875                         printk("%s: err: on ", vfd->name);
1876                         v4l_print_ioctl(vfd->name, cmd);
1877                         printk("\n");
1878                 }
1879         }
1880
1881         return ret;
1882 }
1883
1884 int video_ioctl2 (struct inode *inode, struct file *file,
1885                unsigned int cmd, unsigned long arg)
1886 {
1887         char    sbuf[128];
1888         void    *mbuf = NULL;
1889         void    *parg = NULL;
1890         int     err  = -EINVAL;
1891         int     is_ext_ctrl;
1892         size_t  ctrls_size = 0;
1893         void __user *user_ptr = NULL;
1894
1895 #ifdef __OLD_VIDIOC_
1896         cmd = video_fix_command(cmd);
1897 #endif
1898         is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
1899                        cmd == VIDIOC_TRY_EXT_CTRLS);
1900
1901         /*  Copy arguments into temp kernel buffer  */
1902         switch (_IOC_DIR(cmd)) {
1903         case _IOC_NONE:
1904                 parg = NULL;
1905                 break;
1906         case _IOC_READ:
1907         case _IOC_WRITE:
1908         case (_IOC_WRITE | _IOC_READ):
1909                 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
1910                         parg = sbuf;
1911                 } else {
1912                         /* too big to allocate from stack */
1913                         mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
1914                         if (NULL == mbuf)
1915                                 return -ENOMEM;
1916                         parg = mbuf;
1917                 }
1918
1919                 err = -EFAULT;
1920                 if (_IOC_DIR(cmd) & _IOC_WRITE)
1921                         if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
1922                                 goto out;
1923                 break;
1924         }
1925
1926         if (is_ext_ctrl) {
1927                 struct v4l2_ext_controls *p = parg;
1928
1929                 /* In case of an error, tell the caller that it wasn't
1930                    a specific control that caused it. */
1931                 p->error_idx = p->count;
1932                 user_ptr = (void __user *)p->controls;
1933                 if (p->count) {
1934                         ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
1935                         /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
1936                         mbuf = kmalloc(ctrls_size, GFP_KERNEL);
1937                         err = -ENOMEM;
1938                         if (NULL == mbuf)
1939                                 goto out_ext_ctrl;
1940                         err = -EFAULT;
1941                         if (copy_from_user(mbuf, user_ptr, ctrls_size))
1942                                 goto out_ext_ctrl;
1943                         p->controls = mbuf;
1944                 }
1945         }
1946
1947         /* Handles IOCTL */
1948         err = __video_do_ioctl(inode, file, cmd, parg);
1949         if (err == -ENOIOCTLCMD)
1950                 err = -EINVAL;
1951         if (is_ext_ctrl) {
1952                 struct v4l2_ext_controls *p = parg;
1953
1954                 p->controls = (void *)user_ptr;
1955                 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
1956                         err = -EFAULT;
1957                 goto out_ext_ctrl;
1958         }
1959         if (err < 0)
1960                 goto out;
1961
1962 out_ext_ctrl:
1963         /*  Copy results into user buffer  */
1964         switch (_IOC_DIR(cmd))
1965         {
1966         case _IOC_READ:
1967         case (_IOC_WRITE | _IOC_READ):
1968                 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1969                         err = -EFAULT;
1970                 break;
1971         }
1972
1973 out:
1974         kfree(mbuf);
1975         return err;
1976 }
1977 EXPORT_SYMBOL(video_ioctl2);
1978
1979 static const struct file_operations video_fops;
1980
1981 /**
1982  *      video_register_device - register video4linux devices
1983  *      @vfd:  video device structure we want to register
1984  *      @type: type of device to register
1985  *      @nr:   which device number (0 == /dev/video0, 1 == /dev/video1, ...
1986  *             -1 == first free)
1987  *
1988  *      The registration code assigns minor numbers based on the type
1989  *      requested. -ENFILE is returned in all the device slots for this
1990  *      category are full. If not then the minor field is set and the
1991  *      driver initialize function is called (if non %NULL).
1992  *
1993  *      Zero is returned on success.
1994  *
1995  *      Valid types are
1996  *
1997  *      %VFL_TYPE_GRABBER - A frame grabber
1998  *
1999  *      %VFL_TYPE_VTX - A teletext device
2000  *
2001  *      %VFL_TYPE_VBI - Vertical blank data (undecoded)
2002  *
2003  *      %VFL_TYPE_RADIO - A radio card
2004  */
2005
2006 int video_register_device(struct video_device *vfd, int type, int nr)
2007 {
2008         int i=0;
2009         int base;
2010         int end;
2011         int ret;
2012         char *name_base;
2013
2014         switch(type)
2015         {
2016                 case VFL_TYPE_GRABBER:
2017                         base=MINOR_VFL_TYPE_GRABBER_MIN;
2018                         end=MINOR_VFL_TYPE_GRABBER_MAX+1;
2019                         name_base = "video";
2020                         break;
2021                 case VFL_TYPE_VTX:
2022                         base=MINOR_VFL_TYPE_VTX_MIN;
2023                         end=MINOR_VFL_TYPE_VTX_MAX+1;
2024                         name_base = "vtx";
2025                         break;
2026                 case VFL_TYPE_VBI:
2027                         base=MINOR_VFL_TYPE_VBI_MIN;
2028                         end=MINOR_VFL_TYPE_VBI_MAX+1;
2029                         name_base = "vbi";
2030                         break;
2031                 case VFL_TYPE_RADIO:
2032                         base=MINOR_VFL_TYPE_RADIO_MIN;
2033                         end=MINOR_VFL_TYPE_RADIO_MAX+1;
2034                         name_base = "radio";
2035                         break;
2036                 default:
2037                         printk(KERN_ERR "%s called with unknown type: %d\n",
2038                                __func__, type);
2039                         return -1;
2040         }
2041
2042         /* pick a minor number */
2043         mutex_lock(&videodev_lock);
2044         if (nr >= 0  &&  nr < end-base) {
2045                 /* use the one the driver asked for */
2046                 i = base+nr;
2047                 if (NULL != video_device[i]) {
2048                         mutex_unlock(&videodev_lock);
2049                         return -ENFILE;
2050                 }
2051         } else {
2052                 /* use first free */
2053                 for(i=base;i<end;i++)
2054                         if (NULL == video_device[i])
2055                                 break;
2056                 if (i == end) {
2057                         mutex_unlock(&videodev_lock);
2058                         return -ENFILE;
2059                 }
2060         }
2061         video_device[i]=vfd;
2062         vfd->minor=i;
2063         mutex_unlock(&videodev_lock);
2064         mutex_init(&vfd->lock);
2065
2066         /* sysfs class */
2067         memset(&vfd->class_dev, 0x00, sizeof(vfd->class_dev));
2068         if (vfd->dev)
2069                 vfd->class_dev.parent = vfd->dev;
2070         vfd->class_dev.class       = &video_class;
2071         vfd->class_dev.devt        = MKDEV(VIDEO_MAJOR, vfd->minor);
2072         sprintf(vfd->class_dev.bus_id, "%s%d", name_base, i - base);
2073         ret = device_register(&vfd->class_dev);
2074         if (ret < 0) {
2075                 printk(KERN_ERR "%s: device_register failed\n",
2076                        __func__);
2077                 goto fail_minor;
2078         }
2079
2080 #if 1
2081         /* needed until all drivers are fixed */
2082         if (!vfd->release)
2083                 printk(KERN_WARNING "videodev: \"%s\" has no release callback. "
2084                        "Please fix your driver for proper sysfs support, see "
2085                        "http://lwn.net/Articles/36850/\n", vfd->name);
2086 #endif
2087         return 0;
2088
2089 fail_minor:
2090         mutex_lock(&videodev_lock);
2091         video_device[vfd->minor] = NULL;
2092         vfd->minor = -1;
2093         mutex_unlock(&videodev_lock);
2094         return ret;
2095 }
2096 EXPORT_SYMBOL(video_register_device);
2097
2098 /**
2099  *      video_unregister_device - unregister a video4linux device
2100  *      @vfd: the device to unregister
2101  *
2102  *      This unregisters the passed device and deassigns the minor
2103  *      number. Future open calls will be met with errors.
2104  */
2105
2106 void video_unregister_device(struct video_device *vfd)
2107 {
2108         mutex_lock(&videodev_lock);
2109         if(video_device[vfd->minor]!=vfd)
2110                 panic("videodev: bad unregister");
2111
2112         video_device[vfd->minor]=NULL;
2113         device_unregister(&vfd->class_dev);
2114         mutex_unlock(&videodev_lock);
2115 }
2116 EXPORT_SYMBOL(video_unregister_device);
2117
2118 /*
2119  * Video fs operations
2120  */
2121 static const struct file_operations video_fops=
2122 {
2123         .owner          = THIS_MODULE,
2124         .llseek         = no_llseek,
2125         .open           = video_open,
2126 };
2127
2128 /*
2129  *      Initialise video for linux
2130  */
2131
2132 static int __init videodev_init(void)
2133 {
2134         int ret;
2135
2136         printk(KERN_INFO "Linux video capture interface: v2.00\n");
2137         if (register_chrdev(VIDEO_MAJOR, VIDEO_NAME, &video_fops)) {
2138                 printk(KERN_WARNING "video_dev: unable to get major %d\n", VIDEO_MAJOR);
2139                 return -EIO;
2140         }
2141
2142         ret = class_register(&video_class);
2143         if (ret < 0) {
2144                 unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
2145                 printk(KERN_WARNING "video_dev: class_register failed\n");
2146                 return -EIO;
2147         }
2148
2149         return 0;
2150 }
2151
2152 static void __exit videodev_exit(void)
2153 {
2154         class_unregister(&video_class);
2155         unregister_chrdev(VIDEO_MAJOR, VIDEO_NAME);
2156 }
2157
2158 module_init(videodev_init)
2159 module_exit(videodev_exit)
2160
2161 MODULE_AUTHOR("Alan Cox, Mauro Carvalho Chehab <mchehab@infradead.org>");
2162 MODULE_DESCRIPTION("Device registrar for Video4Linux drivers v2");
2163 MODULE_LICENSE("GPL");
2164
2165
2166 /*
2167  * Local variables:
2168  * c-basic-offset: 8
2169  * End:
2170  */