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