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