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