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