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