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