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