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