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