]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/v4l2-ioctl.c
V4L/DVB (10794): v4l2: Move code to zero querybuf output struct to v4l2_ioctl
[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 /* 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                 memset(p, 0, sizeof(*p));
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                 memset(cap, 0, sizeof(*cap));
687
688                 if (!ops->vidioc_querycap)
689                         break;
690
691                 ret = ops->vidioc_querycap(file, fh, cap);
692                 if (!ret)
693                         dbgarg(cmd, "driver=%s, card=%s, bus=%s, "
694                                         "version=0x%08x, "
695                                         "capabilities=0x%08x\n",
696                                         cap->driver, cap->card, cap->bus_info,
697                                         cap->version,
698                                         cap->capabilities);
699                 break;
700         }
701
702         /* --- priority ------------------------------------------ */
703         case VIDIOC_G_PRIORITY:
704         {
705                 enum v4l2_priority *p = arg;
706
707                 if (!ops->vidioc_g_priority)
708                         break;
709                 ret = ops->vidioc_g_priority(file, fh, p);
710                 if (!ret)
711                         dbgarg(cmd, "priority is %d\n", *p);
712                 break;
713         }
714         case VIDIOC_S_PRIORITY:
715         {
716                 enum v4l2_priority *p = arg;
717
718                 if (!ops->vidioc_s_priority)
719                         break;
720                 dbgarg(cmd, "setting priority to %d\n", *p);
721                 ret = ops->vidioc_s_priority(file, fh, *p);
722                 break;
723         }
724
725         /* --- capture ioctls ---------------------------------------- */
726         case VIDIOC_ENUM_FMT:
727         {
728                 struct v4l2_fmtdesc *f = arg;
729                 enum v4l2_buf_type type;
730                 unsigned int index;
731
732                 index = f->index;
733                 type  = f->type;
734                 memset(f, 0, sizeof(*f));
735                 f->index = index;
736                 f->type  = type;
737
738                 switch (type) {
739                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
740                         if (ops->vidioc_enum_fmt_vid_cap)
741                                 ret = ops->vidioc_enum_fmt_vid_cap(file, fh, f);
742                         break;
743                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
744                         if (ops->vidioc_enum_fmt_vid_overlay)
745                                 ret = ops->vidioc_enum_fmt_vid_overlay(file,
746                                         fh, f);
747                         break;
748                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
749                         if (ops->vidioc_enum_fmt_vid_out)
750                                 ret = ops->vidioc_enum_fmt_vid_out(file, fh, f);
751                         break;
752                 case V4L2_BUF_TYPE_PRIVATE:
753                         if (ops->vidioc_enum_fmt_type_private)
754                                 ret = ops->vidioc_enum_fmt_type_private(file,
755                                                                 fh, f);
756                         break;
757                 default:
758                         break;
759                 }
760                 if (!ret)
761                         dbgarg(cmd, "index=%d, type=%d, flags=%d, "
762                                 "pixelformat=%c%c%c%c, description='%s'\n",
763                                 f->index, f->type, f->flags,
764                                 (f->pixelformat & 0xff),
765                                 (f->pixelformat >>  8) & 0xff,
766                                 (f->pixelformat >> 16) & 0xff,
767                                 (f->pixelformat >> 24) & 0xff,
768                                 f->description);
769                 break;
770         }
771         case VIDIOC_G_FMT:
772         {
773                 struct v4l2_format *f = (struct v4l2_format *)arg;
774
775                 memset(f->fmt.raw_data, 0, sizeof(f->fmt.raw_data));
776
777                 /* FIXME: Should be one dump per type */
778                 dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
779
780                 switch (f->type) {
781                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
782                         if (ops->vidioc_g_fmt_vid_cap)
783                                 ret = ops->vidioc_g_fmt_vid_cap(file, fh, f);
784                         if (!ret)
785                                 v4l_print_pix_fmt(vfd, &f->fmt.pix);
786                         break;
787                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
788                         if (ops->vidioc_g_fmt_vid_overlay)
789                                 ret = ops->vidioc_g_fmt_vid_overlay(file,
790                                                                     fh, f);
791                         break;
792                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
793                         if (ops->vidioc_g_fmt_vid_out)
794                                 ret = ops->vidioc_g_fmt_vid_out(file, fh, f);
795                         if (!ret)
796                                 v4l_print_pix_fmt(vfd, &f->fmt.pix);
797                         break;
798                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
799                         if (ops->vidioc_g_fmt_vid_out_overlay)
800                                 ret = ops->vidioc_g_fmt_vid_out_overlay(file,
801                                        fh, f);
802                         break;
803                 case V4L2_BUF_TYPE_VBI_CAPTURE:
804                         if (ops->vidioc_g_fmt_vbi_cap)
805                                 ret = ops->vidioc_g_fmt_vbi_cap(file, fh, f);
806                         break;
807                 case V4L2_BUF_TYPE_VBI_OUTPUT:
808                         if (ops->vidioc_g_fmt_vbi_out)
809                                 ret = ops->vidioc_g_fmt_vbi_out(file, fh, f);
810                         break;
811                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
812                         if (ops->vidioc_g_fmt_sliced_vbi_cap)
813                                 ret = ops->vidioc_g_fmt_sliced_vbi_cap(file,
814                                                                         fh, f);
815                         break;
816                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
817                         if (ops->vidioc_g_fmt_sliced_vbi_out)
818                                 ret = ops->vidioc_g_fmt_sliced_vbi_out(file,
819                                                                         fh, f);
820                         break;
821                 case V4L2_BUF_TYPE_PRIVATE:
822                         if (ops->vidioc_g_fmt_type_private)
823                                 ret = ops->vidioc_g_fmt_type_private(file,
824                                                                 fh, f);
825                         break;
826                 }
827
828                 break;
829         }
830         case VIDIOC_S_FMT:
831         {
832                 struct v4l2_format *f = (struct v4l2_format *)arg;
833
834                 /* FIXME: Should be one dump per type */
835                 dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
836
837                 switch (f->type) {
838                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
839                         v4l_print_pix_fmt(vfd, &f->fmt.pix);
840                         if (ops->vidioc_s_fmt_vid_cap)
841                                 ret = ops->vidioc_s_fmt_vid_cap(file, fh, f);
842                         break;
843                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
844                         if (ops->vidioc_s_fmt_vid_overlay)
845                                 ret = ops->vidioc_s_fmt_vid_overlay(file,
846                                                                     fh, f);
847                         break;
848                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
849                         v4l_print_pix_fmt(vfd, &f->fmt.pix);
850                         if (ops->vidioc_s_fmt_vid_out)
851                                 ret = ops->vidioc_s_fmt_vid_out(file, fh, f);
852                         break;
853                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
854                         if (ops->vidioc_s_fmt_vid_out_overlay)
855                                 ret = ops->vidioc_s_fmt_vid_out_overlay(file,
856                                         fh, f);
857                         break;
858                 case V4L2_BUF_TYPE_VBI_CAPTURE:
859                         if (ops->vidioc_s_fmt_vbi_cap)
860                                 ret = ops->vidioc_s_fmt_vbi_cap(file, fh, f);
861                         break;
862                 case V4L2_BUF_TYPE_VBI_OUTPUT:
863                         if (ops->vidioc_s_fmt_vbi_out)
864                                 ret = ops->vidioc_s_fmt_vbi_out(file, fh, f);
865                         break;
866                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
867                         if (ops->vidioc_s_fmt_sliced_vbi_cap)
868                                 ret = ops->vidioc_s_fmt_sliced_vbi_cap(file,
869                                                                         fh, f);
870                         break;
871                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
872                         if (ops->vidioc_s_fmt_sliced_vbi_out)
873                                 ret = ops->vidioc_s_fmt_sliced_vbi_out(file,
874                                                                         fh, f);
875                         break;
876                 case V4L2_BUF_TYPE_PRIVATE:
877                         if (ops->vidioc_s_fmt_type_private)
878                                 ret = ops->vidioc_s_fmt_type_private(file,
879                                                                 fh, f);
880                         break;
881                 }
882                 break;
883         }
884         case VIDIOC_TRY_FMT:
885         {
886                 struct v4l2_format *f = (struct v4l2_format *)arg;
887
888                 /* FIXME: Should be one dump per type */
889                 dbgarg(cmd, "type=%s\n", prt_names(f->type,
890                                                 v4l2_type_names));
891                 switch (f->type) {
892                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
893                         if (ops->vidioc_try_fmt_vid_cap)
894                                 ret = ops->vidioc_try_fmt_vid_cap(file, fh, f);
895                         if (!ret)
896                                 v4l_print_pix_fmt(vfd, &f->fmt.pix);
897                         break;
898                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
899                         if (ops->vidioc_try_fmt_vid_overlay)
900                                 ret = ops->vidioc_try_fmt_vid_overlay(file,
901                                         fh, f);
902                         break;
903                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
904                         if (ops->vidioc_try_fmt_vid_out)
905                                 ret = ops->vidioc_try_fmt_vid_out(file, fh, f);
906                         if (!ret)
907                                 v4l_print_pix_fmt(vfd, &f->fmt.pix);
908                         break;
909                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
910                         if (ops->vidioc_try_fmt_vid_out_overlay)
911                                 ret = ops->vidioc_try_fmt_vid_out_overlay(file,
912                                        fh, f);
913                         break;
914                 case V4L2_BUF_TYPE_VBI_CAPTURE:
915                         if (ops->vidioc_try_fmt_vbi_cap)
916                                 ret = ops->vidioc_try_fmt_vbi_cap(file, fh, f);
917                         break;
918                 case V4L2_BUF_TYPE_VBI_OUTPUT:
919                         if (ops->vidioc_try_fmt_vbi_out)
920                                 ret = ops->vidioc_try_fmt_vbi_out(file, fh, f);
921                         break;
922                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
923                         if (ops->vidioc_try_fmt_sliced_vbi_cap)
924                                 ret = ops->vidioc_try_fmt_sliced_vbi_cap(file,
925                                                                 fh, f);
926                         break;
927                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
928                         if (ops->vidioc_try_fmt_sliced_vbi_out)
929                                 ret = ops->vidioc_try_fmt_sliced_vbi_out(file,
930                                                                 fh, f);
931                         break;
932                 case V4L2_BUF_TYPE_PRIVATE:
933                         if (ops->vidioc_try_fmt_type_private)
934                                 ret = ops->vidioc_try_fmt_type_private(file,
935                                                                 fh, f);
936                         break;
937                 }
938
939                 break;
940         }
941         /* FIXME: Those buf reqs could be handled here,
942            with some changes on videobuf to allow its header to be included at
943            videodev2.h or being merged at videodev2.
944          */
945         case VIDIOC_REQBUFS:
946         {
947                 struct v4l2_requestbuffers *p = arg;
948
949                 if (!ops->vidioc_reqbufs)
950                         break;
951                 ret = check_fmt(ops, p->type);
952                 if (ret)
953                         break;
954
955                 ret = ops->vidioc_reqbufs(file, fh, p);
956                 dbgarg(cmd, "count=%d, type=%s, memory=%s\n",
957                                 p->count,
958                                 prt_names(p->type, v4l2_type_names),
959                                 prt_names(p->memory, v4l2_memory_names));
960                 break;
961         }
962         case VIDIOC_QUERYBUF:
963         {
964                 struct v4l2_buffer *p = arg;
965
966                 if (!ops->vidioc_querybuf)
967                         break;
968                 ret = check_fmt(ops, p->type);
969                 if (ret)
970                         break;
971
972                 /* Zero out all fields starting with bytesysed, which is
973                  * everything but index and type.  */
974                 memset(0, &p->bytesused,
975                        sizeof(*p) - offsetof(typeof(*p), bytesused));
976
977                 ret = ops->vidioc_querybuf(file, fh, p);
978                 if (!ret)
979                         dbgbuf(cmd, vfd, p);
980                 break;
981         }
982         case VIDIOC_QBUF:
983         {
984                 struct v4l2_buffer *p = arg;
985
986                 if (!ops->vidioc_qbuf)
987                         break;
988                 ret = check_fmt(ops, p->type);
989                 if (ret)
990                         break;
991
992                 ret = ops->vidioc_qbuf(file, fh, p);
993                 if (!ret)
994                         dbgbuf(cmd, vfd, p);
995                 break;
996         }
997         case VIDIOC_DQBUF:
998         {
999                 struct v4l2_buffer *p = arg;
1000
1001                 if (!ops->vidioc_dqbuf)
1002                         break;
1003                 ret = check_fmt(ops, p->type);
1004                 if (ret)
1005                         break;
1006
1007                 ret = ops->vidioc_dqbuf(file, fh, p);
1008                 if (!ret)
1009                         dbgbuf(cmd, vfd, p);
1010                 break;
1011         }
1012         case VIDIOC_OVERLAY:
1013         {
1014                 int *i = arg;
1015
1016                 if (!ops->vidioc_overlay)
1017                         break;
1018                 dbgarg(cmd, "value=%d\n", *i);
1019                 ret = ops->vidioc_overlay(file, fh, *i);
1020                 break;
1021         }
1022         case VIDIOC_G_FBUF:
1023         {
1024                 struct v4l2_framebuffer *p = arg;
1025
1026                 if (!ops->vidioc_g_fbuf)
1027                         break;
1028                 ret = ops->vidioc_g_fbuf(file, fh, arg);
1029                 if (!ret) {
1030                         dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
1031                                         p->capability, p->flags,
1032                                         (unsigned long)p->base);
1033                         v4l_print_pix_fmt(vfd, &p->fmt);
1034                 }
1035                 break;
1036         }
1037         case VIDIOC_S_FBUF:
1038         {
1039                 struct v4l2_framebuffer *p = arg;
1040
1041                 if (!ops->vidioc_s_fbuf)
1042                         break;
1043                 dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
1044                         p->capability, p->flags, (unsigned long)p->base);
1045                 v4l_print_pix_fmt(vfd, &p->fmt);
1046                 ret = ops->vidioc_s_fbuf(file, fh, arg);
1047                 break;
1048         }
1049         case VIDIOC_STREAMON:
1050         {
1051                 enum v4l2_buf_type i = *(int *)arg;
1052
1053                 if (!ops->vidioc_streamon)
1054                         break;
1055                 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
1056                 ret = ops->vidioc_streamon(file, fh, i);
1057                 break;
1058         }
1059         case VIDIOC_STREAMOFF:
1060         {
1061                 enum v4l2_buf_type i = *(int *)arg;
1062
1063                 if (!ops->vidioc_streamoff)
1064                         break;
1065                 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
1066                 ret = ops->vidioc_streamoff(file, fh, i);
1067                 break;
1068         }
1069         /* ---------- tv norms ---------- */
1070         case VIDIOC_ENUMSTD:
1071         {
1072                 struct v4l2_standard *p = arg;
1073                 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1074                 unsigned int index = p->index, i, j = 0;
1075                 const char *descr = "";
1076
1077                 /* Return norm array in a canonical way */
1078                 for (i = 0; i <= index && id; i++) {
1079                         /* last std value in the standards array is 0, so this
1080                            while always ends there since (id & 0) == 0. */
1081                         while ((id & standards[j].std) != standards[j].std)
1082                                 j++;
1083                         curr_id = standards[j].std;
1084                         descr = standards[j].descr;
1085                         j++;
1086                         if (curr_id == 0)
1087                                 break;
1088                         if (curr_id != V4L2_STD_PAL &&
1089                             curr_id != V4L2_STD_SECAM &&
1090                             curr_id != V4L2_STD_NTSC)
1091                                 id &= ~curr_id;
1092                 }
1093                 if (i <= index)
1094                         return -EINVAL;
1095
1096                 v4l2_video_std_construct(p, curr_id, descr);
1097                 p->index = index;
1098
1099                 dbgarg(cmd, "index=%d, id=0x%Lx, name=%s, fps=%d/%d, "
1100                                 "framelines=%d\n", p->index,
1101                                 (unsigned long long)p->id, p->name,
1102                                 p->frameperiod.numerator,
1103                                 p->frameperiod.denominator,
1104                                 p->framelines);
1105
1106                 ret = 0;
1107                 break;
1108         }
1109         case VIDIOC_G_STD:
1110         {
1111                 v4l2_std_id *id = arg;
1112
1113                 ret = 0;
1114                 /* Calls the specific handler */
1115                 if (ops->vidioc_g_std)
1116                         ret = ops->vidioc_g_std(file, fh, id);
1117                 else
1118                         *id = vfd->current_norm;
1119
1120                 if (!ret)
1121                         dbgarg(cmd, "std=0x%08Lx\n", (long long unsigned)*id);
1122                 break;
1123         }
1124         case VIDIOC_S_STD:
1125         {
1126                 v4l2_std_id *id = arg, norm;
1127
1128                 dbgarg(cmd, "std=%08Lx\n", (long long unsigned)*id);
1129
1130                 norm = (*id) & vfd->tvnorms;
1131                 if (vfd->tvnorms && !norm)      /* Check if std is supported */
1132                         break;
1133
1134                 /* Calls the specific handler */
1135                 if (ops->vidioc_s_std)
1136                         ret = ops->vidioc_s_std(file, fh, &norm);
1137                 else
1138                         ret = -EINVAL;
1139
1140                 /* Updates standard information */
1141                 if (ret >= 0)
1142                         vfd->current_norm = norm;
1143                 break;
1144         }
1145         case VIDIOC_QUERYSTD:
1146         {
1147                 v4l2_std_id *p = arg;
1148
1149                 if (!ops->vidioc_querystd)
1150                         break;
1151                 ret = ops->vidioc_querystd(file, fh, arg);
1152                 if (!ret)
1153                         dbgarg(cmd, "detected std=%08Lx\n",
1154                                                 (unsigned long long)*p);
1155                 break;
1156         }
1157         /* ------ input switching ---------- */
1158         /* FIXME: Inputs can be handled inside videodev2 */
1159         case VIDIOC_ENUMINPUT:
1160         {
1161                 struct v4l2_input *p = arg;
1162                 int i = p->index;
1163
1164                 if (!ops->vidioc_enum_input)
1165                         break;
1166                 memset(p, 0, sizeof(*p));
1167                 p->index = i;
1168
1169                 ret = ops->vidioc_enum_input(file, fh, p);
1170                 if (!ret)
1171                         dbgarg(cmd, "index=%d, name=%s, type=%d, "
1172                                 "audioset=%d, "
1173                                 "tuner=%d, std=%08Lx, status=%d\n",
1174                                 p->index, p->name, p->type, p->audioset,
1175                                 p->tuner,
1176                                 (unsigned long long)p->std,
1177                                 p->status);
1178                 break;
1179         }
1180         case VIDIOC_G_INPUT:
1181         {
1182                 unsigned int *i = arg;
1183
1184                 if (!ops->vidioc_g_input)
1185                         break;
1186                 ret = ops->vidioc_g_input(file, fh, i);
1187                 if (!ret)
1188                         dbgarg(cmd, "value=%d\n", *i);
1189                 break;
1190         }
1191         case VIDIOC_S_INPUT:
1192         {
1193                 unsigned int *i = arg;
1194
1195                 if (!ops->vidioc_s_input)
1196                         break;
1197                 dbgarg(cmd, "value=%d\n", *i);
1198                 ret = ops->vidioc_s_input(file, fh, *i);
1199                 break;
1200         }
1201
1202         /* ------ output switching ---------- */
1203         case VIDIOC_ENUMOUTPUT:
1204         {
1205                 struct v4l2_output *p = arg;
1206                 int i = p->index;
1207
1208                 if (!ops->vidioc_enum_output)
1209                         break;
1210                 memset(p, 0, sizeof(*p));
1211                 p->index = i;
1212
1213                 ret = ops->vidioc_enum_output(file, fh, p);
1214                 if (!ret)
1215                         dbgarg(cmd, "index=%d, name=%s, type=%d, "
1216                                 "audioset=0x%x, "
1217                                 "modulator=%d, std=0x%08Lx\n",
1218                                 p->index, p->name, p->type, p->audioset,
1219                                 p->modulator, (unsigned long long)p->std);
1220                 break;
1221         }
1222         case VIDIOC_G_OUTPUT:
1223         {
1224                 unsigned int *i = arg;
1225
1226                 if (!ops->vidioc_g_output)
1227                         break;
1228                 ret = ops->vidioc_g_output(file, fh, i);
1229                 if (!ret)
1230                         dbgarg(cmd, "value=%d\n", *i);
1231                 break;
1232         }
1233         case VIDIOC_S_OUTPUT:
1234         {
1235                 unsigned int *i = arg;
1236
1237                 if (!ops->vidioc_s_output)
1238                         break;
1239                 dbgarg(cmd, "value=%d\n", *i);
1240                 ret = ops->vidioc_s_output(file, fh, *i);
1241                 break;
1242         }
1243
1244         /* --- controls ---------------------------------------------- */
1245         case VIDIOC_QUERYCTRL:
1246         {
1247                 struct v4l2_queryctrl *p = arg;
1248
1249                 if (!ops->vidioc_queryctrl)
1250                         break;
1251                 ret = ops->vidioc_queryctrl(file, fh, p);
1252                 if (!ret)
1253                         dbgarg(cmd, "id=0x%x, type=%d, name=%s, min/max=%d/%d, "
1254                                         "step=%d, default=%d, flags=0x%08x\n",
1255                                         p->id, p->type, p->name,
1256                                         p->minimum, p->maximum,
1257                                         p->step, p->default_value, p->flags);
1258                 else
1259                         dbgarg(cmd, "id=0x%x\n", p->id);
1260                 break;
1261         }
1262         case VIDIOC_G_CTRL:
1263         {
1264                 struct v4l2_control *p = arg;
1265
1266                 if (ops->vidioc_g_ctrl)
1267                         ret = ops->vidioc_g_ctrl(file, fh, p);
1268                 else if (ops->vidioc_g_ext_ctrls) {
1269                         struct v4l2_ext_controls ctrls;
1270                         struct v4l2_ext_control ctrl;
1271
1272                         ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1273                         ctrls.count = 1;
1274                         ctrls.controls = &ctrl;
1275                         ctrl.id = p->id;
1276                         ctrl.value = p->value;
1277                         if (check_ext_ctrls(&ctrls, 1)) {
1278                                 ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1279                                 if (ret == 0)
1280                                         p->value = ctrl.value;
1281                         }
1282                 } else
1283                         break;
1284                 if (!ret)
1285                         dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1286                 else
1287                         dbgarg(cmd, "id=0x%x\n", p->id);
1288                 break;
1289         }
1290         case VIDIOC_S_CTRL:
1291         {
1292                 struct v4l2_control *p = arg;
1293                 struct v4l2_ext_controls ctrls;
1294                 struct v4l2_ext_control ctrl;
1295
1296                 if (!ops->vidioc_s_ctrl && !ops->vidioc_s_ext_ctrls)
1297                         break;
1298
1299                 dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1300
1301                 if (ops->vidioc_s_ctrl) {
1302                         ret = ops->vidioc_s_ctrl(file, fh, p);
1303                         break;
1304                 }
1305                 if (!ops->vidioc_s_ext_ctrls)
1306                         break;
1307
1308                 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1309                 ctrls.count = 1;
1310                 ctrls.controls = &ctrl;
1311                 ctrl.id = p->id;
1312                 ctrl.value = p->value;
1313                 if (check_ext_ctrls(&ctrls, 1))
1314                         ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1315                 break;
1316         }
1317         case VIDIOC_G_EXT_CTRLS:
1318         {
1319                 struct v4l2_ext_controls *p = arg;
1320
1321                 p->error_idx = p->count;
1322                 if (!ops->vidioc_g_ext_ctrls)
1323                         break;
1324                 if (check_ext_ctrls(p, 0))
1325                         ret = ops->vidioc_g_ext_ctrls(file, fh, p);
1326                 v4l_print_ext_ctrls(cmd, vfd, p, !ret);
1327                 break;
1328         }
1329         case VIDIOC_S_EXT_CTRLS:
1330         {
1331                 struct v4l2_ext_controls *p = arg;
1332
1333                 p->error_idx = p->count;
1334                 if (!ops->vidioc_s_ext_ctrls)
1335                         break;
1336                 v4l_print_ext_ctrls(cmd, vfd, p, 1);
1337                 if (check_ext_ctrls(p, 0))
1338                         ret = ops->vidioc_s_ext_ctrls(file, fh, p);
1339                 break;
1340         }
1341         case VIDIOC_TRY_EXT_CTRLS:
1342         {
1343                 struct v4l2_ext_controls *p = arg;
1344
1345                 p->error_idx = p->count;
1346                 if (!ops->vidioc_try_ext_ctrls)
1347                         break;
1348                 v4l_print_ext_ctrls(cmd, vfd, p, 1);
1349                 if (check_ext_ctrls(p, 0))
1350                         ret = ops->vidioc_try_ext_ctrls(file, fh, p);
1351                 break;
1352         }
1353         case VIDIOC_QUERYMENU:
1354         {
1355                 struct v4l2_querymenu *p = arg;
1356
1357                 if (!ops->vidioc_querymenu)
1358                         break;
1359                 ret = ops->vidioc_querymenu(file, fh, p);
1360                 if (!ret)
1361                         dbgarg(cmd, "id=0x%x, index=%d, name=%s\n",
1362                                 p->id, p->index, p->name);
1363                 else
1364                         dbgarg(cmd, "id=0x%x, index=%d\n",
1365                                 p->id, p->index);
1366                 break;
1367         }
1368         /* --- audio ---------------------------------------------- */
1369         case VIDIOC_ENUMAUDIO:
1370         {
1371                 struct v4l2_audio *p = arg;
1372
1373                 if (!ops->vidioc_enumaudio)
1374                         break;
1375                 ret = ops->vidioc_enumaudio(file, fh, p);
1376                 if (!ret)
1377                         dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1378                                         "mode=0x%x\n", p->index, p->name,
1379                                         p->capability, p->mode);
1380                 else
1381                         dbgarg(cmd, "index=%d\n", p->index);
1382                 break;
1383         }
1384         case VIDIOC_G_AUDIO:
1385         {
1386                 struct v4l2_audio *p = arg;
1387                 __u32 index = p->index;
1388
1389                 if (!ops->vidioc_g_audio)
1390                         break;
1391
1392                 memset(p, 0, sizeof(*p));
1393                 p->index = index;
1394                 ret = ops->vidioc_g_audio(file, fh, p);
1395                 if (!ret)
1396                         dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1397                                         "mode=0x%x\n", p->index,
1398                                         p->name, p->capability, p->mode);
1399                 else
1400                         dbgarg(cmd, "index=%d\n", p->index);
1401                 break;
1402         }
1403         case VIDIOC_S_AUDIO:
1404         {
1405                 struct v4l2_audio *p = arg;
1406
1407                 if (!ops->vidioc_s_audio)
1408                         break;
1409                 dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1410                                         "mode=0x%x\n", p->index, p->name,
1411                                         p->capability, p->mode);
1412                 ret = ops->vidioc_s_audio(file, fh, p);
1413                 break;
1414         }
1415         case VIDIOC_ENUMAUDOUT:
1416         {
1417                 struct v4l2_audioout *p = arg;
1418
1419                 if (!ops->vidioc_enumaudout)
1420                         break;
1421                 dbgarg(cmd, "Enum for index=%d\n", p->index);
1422                 ret = ops->vidioc_enumaudout(file, fh, p);
1423                 if (!ret)
1424                         dbgarg2("index=%d, name=%s, capability=%d, "
1425                                         "mode=%d\n", p->index, p->name,
1426                                         p->capability, p->mode);
1427                 break;
1428         }
1429         case VIDIOC_G_AUDOUT:
1430         {
1431                 struct v4l2_audioout *p = arg;
1432
1433                 if (!ops->vidioc_g_audout)
1434                         break;
1435                 dbgarg(cmd, "Enum for index=%d\n", p->index);
1436                 ret = ops->vidioc_g_audout(file, fh, p);
1437                 if (!ret)
1438                         dbgarg2("index=%d, name=%s, capability=%d, "
1439                                         "mode=%d\n", p->index, p->name,
1440                                         p->capability, p->mode);
1441                 break;
1442         }
1443         case VIDIOC_S_AUDOUT:
1444         {
1445                 struct v4l2_audioout *p = arg;
1446
1447                 if (!ops->vidioc_s_audout)
1448                         break;
1449                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1450                                         "mode=%d\n", p->index, p->name,
1451                                         p->capability, p->mode);
1452
1453                 ret = ops->vidioc_s_audout(file, fh, p);
1454                 break;
1455         }
1456         case VIDIOC_G_MODULATOR:
1457         {
1458                 struct v4l2_modulator *p = arg;
1459
1460                 if (!ops->vidioc_g_modulator)
1461                         break;
1462                 ret = ops->vidioc_g_modulator(file, fh, p);
1463                 if (!ret)
1464                         dbgarg(cmd, "index=%d, name=%s, "
1465                                         "capability=%d, rangelow=%d,"
1466                                         " rangehigh=%d, txsubchans=%d\n",
1467                                         p->index, p->name, p->capability,
1468                                         p->rangelow, p->rangehigh,
1469                                         p->txsubchans);
1470                 break;
1471         }
1472         case VIDIOC_S_MODULATOR:
1473         {
1474                 struct v4l2_modulator *p = arg;
1475
1476                 if (!ops->vidioc_s_modulator)
1477                         break;
1478                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1479                                 "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1480                                 p->index, p->name, p->capability, p->rangelow,
1481                                 p->rangehigh, p->txsubchans);
1482                         ret = ops->vidioc_s_modulator(file, fh, p);
1483                 break;
1484         }
1485         case VIDIOC_G_CROP:
1486         {
1487                 struct v4l2_crop *p = arg;
1488                 __u32 type;
1489
1490                 if (!ops->vidioc_g_crop)
1491                         break;
1492
1493                 type = p->type;
1494                 memset(p, 0, sizeof(*p));
1495                 p->type = type;
1496
1497                 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1498                 ret = ops->vidioc_g_crop(file, fh, p);
1499                 if (!ret)
1500                         dbgrect(vfd, "", &p->c);
1501                 break;
1502         }
1503         case VIDIOC_S_CROP:
1504         {
1505                 struct v4l2_crop *p = arg;
1506
1507                 if (!ops->vidioc_s_crop)
1508                         break;
1509                 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1510                 dbgrect(vfd, "", &p->c);
1511                 ret = ops->vidioc_s_crop(file, fh, p);
1512                 break;
1513         }
1514         case VIDIOC_CROPCAP:
1515         {
1516                 struct v4l2_cropcap *p = arg;
1517                 __u32 type;
1518
1519                 /*FIXME: Should also show v4l2_fract pixelaspect */
1520                 if (!ops->vidioc_cropcap)
1521                         break;
1522
1523                 type = p->type;
1524                 memset(p, 0, sizeof(*p));
1525                 p->type = type;
1526
1527                 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1528                 ret = ops->vidioc_cropcap(file, fh, p);
1529                 if (!ret) {
1530                         dbgrect(vfd, "bounds ", &p->bounds);
1531                         dbgrect(vfd, "defrect ", &p->defrect);
1532                 }
1533                 break;
1534         }
1535         case VIDIOC_G_JPEGCOMP:
1536         {
1537                 struct v4l2_jpegcompression *p = arg;
1538
1539                 if (!ops->vidioc_g_jpegcomp)
1540                         break;
1541
1542                 memset(p, 0, sizeof(*p));
1543
1544                 ret = ops->vidioc_g_jpegcomp(file, fh, p);
1545                 if (!ret)
1546                         dbgarg(cmd, "quality=%d, APPn=%d, "
1547                                         "APP_len=%d, COM_len=%d, "
1548                                         "jpeg_markers=%d\n",
1549                                         p->quality, p->APPn, p->APP_len,
1550                                         p->COM_len, p->jpeg_markers);
1551                 break;
1552         }
1553         case VIDIOC_S_JPEGCOMP:
1554         {
1555                 struct v4l2_jpegcompression *p = arg;
1556
1557                 if (!ops->vidioc_g_jpegcomp)
1558                         break;
1559                 dbgarg(cmd, "quality=%d, APPn=%d, APP_len=%d, "
1560                                         "COM_len=%d, jpeg_markers=%d\n",
1561                                         p->quality, p->APPn, p->APP_len,
1562                                         p->COM_len, p->jpeg_markers);
1563                         ret = ops->vidioc_s_jpegcomp(file, fh, p);
1564                 break;
1565         }
1566         case VIDIOC_G_ENC_INDEX:
1567         {
1568                 struct v4l2_enc_idx *p = arg;
1569
1570                 if (!ops->vidioc_g_enc_index)
1571                         break;
1572                 ret = ops->vidioc_g_enc_index(file, fh, p);
1573                 if (!ret)
1574                         dbgarg(cmd, "entries=%d, entries_cap=%d\n",
1575                                         p->entries, p->entries_cap);
1576                 break;
1577         }
1578         case VIDIOC_ENCODER_CMD:
1579         {
1580                 struct v4l2_encoder_cmd *p = arg;
1581
1582                 if (!ops->vidioc_encoder_cmd)
1583                         break;
1584                 memset(&p->raw, 0, sizeof(p->raw));
1585                 ret = ops->vidioc_encoder_cmd(file, fh, p);
1586                 if (!ret)
1587                         dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1588                 break;
1589         }
1590         case VIDIOC_TRY_ENCODER_CMD:
1591         {
1592                 struct v4l2_encoder_cmd *p = arg;
1593
1594                 if (!ops->vidioc_try_encoder_cmd)
1595                         break;
1596                 memset(&p->raw, 0, sizeof(p->raw));
1597                 ret = ops->vidioc_try_encoder_cmd(file, fh, p);
1598                 if (!ret)
1599                         dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1600                 break;
1601         }
1602         case VIDIOC_G_PARM:
1603         {
1604                 struct v4l2_streamparm *p = arg;
1605                 __u32 type = p->type;
1606
1607                 memset(p, 0, sizeof(*p));
1608                 p->type = type;
1609
1610                 if (ops->vidioc_g_parm) {
1611                         ret = ops->vidioc_g_parm(file, fh, p);
1612                 } else {
1613                         struct v4l2_standard s;
1614
1615                         if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1616                                 return -EINVAL;
1617
1618                         v4l2_video_std_construct(&s, vfd->current_norm,
1619                                                  v4l2_norm_to_name(vfd->current_norm));
1620
1621                         p->parm.capture.timeperframe = s.frameperiod;
1622                         ret = 0;
1623                 }
1624
1625                 dbgarg(cmd, "type=%d\n", p->type);
1626                 break;
1627         }
1628         case VIDIOC_S_PARM:
1629         {
1630                 struct v4l2_streamparm *p = arg;
1631
1632                 if (!ops->vidioc_s_parm)
1633                         break;
1634                 dbgarg(cmd, "type=%d\n", p->type);
1635                 ret = ops->vidioc_s_parm(file, fh, p);
1636                 break;
1637         }
1638         case VIDIOC_G_TUNER:
1639         {
1640                 struct v4l2_tuner *p = arg;
1641                 __u32 index = p->index;
1642
1643                 if (!ops->vidioc_g_tuner)
1644                         break;
1645
1646                 memset(p, 0, sizeof(*p));
1647                 p->index = index;
1648
1649                 ret = ops->vidioc_g_tuner(file, fh, p);
1650                 if (!ret)
1651                         dbgarg(cmd, "index=%d, name=%s, type=%d, "
1652                                         "capability=0x%x, rangelow=%d, "
1653                                         "rangehigh=%d, signal=%d, afc=%d, "
1654                                         "rxsubchans=0x%x, audmode=%d\n",
1655                                         p->index, p->name, p->type,
1656                                         p->capability, p->rangelow,
1657                                         p->rangehigh, p->signal, p->afc,
1658                                         p->rxsubchans, p->audmode);
1659                 break;
1660         }
1661         case VIDIOC_S_TUNER:
1662         {
1663                 struct v4l2_tuner *p = arg;
1664
1665                 if (!ops->vidioc_s_tuner)
1666                         break;
1667                 dbgarg(cmd, "index=%d, name=%s, type=%d, "
1668                                 "capability=0x%x, rangelow=%d, "
1669                                 "rangehigh=%d, signal=%d, afc=%d, "
1670                                 "rxsubchans=0x%x, audmode=%d\n",
1671                                 p->index, p->name, p->type,
1672                                 p->capability, p->rangelow,
1673                                 p->rangehigh, p->signal, p->afc,
1674                                 p->rxsubchans, p->audmode);
1675                 ret = ops->vidioc_s_tuner(file, fh, p);
1676                 break;
1677         }
1678         case VIDIOC_G_FREQUENCY:
1679         {
1680                 struct v4l2_frequency *p = arg;
1681
1682                 if (!ops->vidioc_g_frequency)
1683                         break;
1684
1685                 memset(p->reserved, 0, sizeof(p->reserved));
1686
1687                 ret = ops->vidioc_g_frequency(file, fh, p);
1688                 if (!ret)
1689                         dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1690                                         p->tuner, p->type, p->frequency);
1691                 break;
1692         }
1693         case VIDIOC_S_FREQUENCY:
1694         {
1695                 struct v4l2_frequency *p = arg;
1696
1697                 if (!ops->vidioc_s_frequency)
1698                         break;
1699                 dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1700                                 p->tuner, p->type, p->frequency);
1701                 ret = ops->vidioc_s_frequency(file, fh, p);
1702                 break;
1703         }
1704         case VIDIOC_G_SLICED_VBI_CAP:
1705         {
1706                 struct v4l2_sliced_vbi_cap *p = arg;
1707                 __u32 type = p->type;
1708
1709                 if (!ops->vidioc_g_sliced_vbi_cap)
1710                         break;
1711                 memset(p, 0, sizeof(*p));
1712                 p->type = type;
1713                 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1714                 ret = ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1715                 if (!ret)
1716                         dbgarg2("service_set=%d\n", p->service_set);
1717                 break;
1718         }
1719         case VIDIOC_LOG_STATUS:
1720         {
1721                 if (!ops->vidioc_log_status)
1722                         break;
1723                 ret = ops->vidioc_log_status(file, fh);
1724                 break;
1725         }
1726 #ifdef CONFIG_VIDEO_ADV_DEBUG
1727         case VIDIOC_DBG_G_REGISTER:
1728         {
1729                 struct v4l2_dbg_register *p = arg;
1730
1731                 if (!capable(CAP_SYS_ADMIN))
1732                         ret = -EPERM;
1733                 else if (ops->vidioc_g_register)
1734                         ret = ops->vidioc_g_register(file, fh, p);
1735                 break;
1736         }
1737         case VIDIOC_DBG_S_REGISTER:
1738         {
1739                 struct v4l2_dbg_register *p = arg;
1740
1741                 if (!capable(CAP_SYS_ADMIN))
1742                         ret = -EPERM;
1743                 else if (ops->vidioc_s_register)
1744                         ret = ops->vidioc_s_register(file, fh, p);
1745                 break;
1746         }
1747 #endif
1748         case VIDIOC_DBG_G_CHIP_IDENT:
1749         {
1750                 struct v4l2_dbg_chip_ident *p = arg;
1751
1752                 if (!ops->vidioc_g_chip_ident)
1753                         break;
1754                 p->ident = V4L2_IDENT_NONE;
1755                 p->revision = 0;
1756                 ret = ops->vidioc_g_chip_ident(file, fh, p);
1757                 if (!ret)
1758                         dbgarg(cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
1759                 break;
1760         }
1761         case VIDIOC_G_CHIP_IDENT_OLD:
1762                 printk(KERN_ERR "VIDIOC_G_CHIP_IDENT has been deprecated and will disappear in 2.6.30.\n");
1763                 printk(KERN_ERR "It is a debugging ioctl and must not be used in applications!\n");
1764                 return -EINVAL;
1765
1766         case VIDIOC_S_HW_FREQ_SEEK:
1767         {
1768                 struct v4l2_hw_freq_seek *p = arg;
1769
1770                 if (!ops->vidioc_s_hw_freq_seek)
1771                         break;
1772                 dbgarg(cmd,
1773                         "tuner=%d, type=%d, seek_upward=%d, wrap_around=%d\n",
1774                         p->tuner, p->type, p->seek_upward, p->wrap_around);
1775                 ret = ops->vidioc_s_hw_freq_seek(file, fh, p);
1776                 break;
1777         }
1778         case VIDIOC_ENUM_FRAMESIZES:
1779         {
1780                 struct v4l2_frmsizeenum *p = arg;
1781
1782                 if (!ops->vidioc_enum_framesizes)
1783                         break;
1784
1785                 memset(p, 0, sizeof(*p));
1786
1787                 ret = ops->vidioc_enum_framesizes(file, fh, p);
1788                 dbgarg(cmd,
1789                         "index=%d, pixelformat=%d, type=%d ",
1790                         p->index, p->pixel_format, p->type);
1791                 switch (p->type) {
1792                 case V4L2_FRMSIZE_TYPE_DISCRETE:
1793                         dbgarg2("width = %d, height=%d\n",
1794                                 p->discrete.width, p->discrete.height);
1795                         break;
1796                 case V4L2_FRMSIZE_TYPE_STEPWISE:
1797                         dbgarg2("min %dx%d, max %dx%d, step %dx%d\n",
1798                                 p->stepwise.min_width,  p->stepwise.min_height,
1799                                 p->stepwise.step_width, p->stepwise.step_height,
1800                                 p->stepwise.max_width,  p->stepwise.max_height);
1801                         break;
1802                 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
1803                         dbgarg2("continuous\n");
1804                         break;
1805                 default:
1806                         dbgarg2("- Unknown type!\n");
1807                 }
1808
1809                 break;
1810         }
1811         case VIDIOC_ENUM_FRAMEINTERVALS:
1812         {
1813                 struct v4l2_frmivalenum *p = arg;
1814
1815                 if (!ops->vidioc_enum_frameintervals)
1816                         break;
1817
1818                 memset(p, 0, sizeof(*p));
1819
1820                 ret = ops->vidioc_enum_frameintervals(file, fh, p);
1821                 dbgarg(cmd,
1822                         "index=%d, pixelformat=%d, width=%d, height=%d, type=%d ",
1823                         p->index, p->pixel_format,
1824                         p->width, p->height, p->type);
1825                 switch (p->type) {
1826                 case V4L2_FRMIVAL_TYPE_DISCRETE:
1827                         dbgarg2("fps=%d/%d\n",
1828                                 p->discrete.numerator,
1829                                 p->discrete.denominator);
1830                         break;
1831                 case V4L2_FRMIVAL_TYPE_STEPWISE:
1832                         dbgarg2("min=%d/%d, max=%d/%d, step=%d/%d\n",
1833                                 p->stepwise.min.numerator,
1834                                 p->stepwise.min.denominator,
1835                                 p->stepwise.max.numerator,
1836                                 p->stepwise.max.denominator,
1837                                 p->stepwise.step.numerator,
1838                                 p->stepwise.step.denominator);
1839                         break;
1840                 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1841                         dbgarg2("continuous\n");
1842                         break;
1843                 default:
1844                         dbgarg2("- Unknown type!\n");
1845                 }
1846                 break;
1847         }
1848
1849         default:
1850         {
1851                 if (!ops->vidioc_default)
1852                         break;
1853                 ret = ops->vidioc_default(file, fh, cmd, arg);
1854                 break;
1855         }
1856         } /* switch */
1857
1858         if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
1859                 if (ret < 0) {
1860                         v4l_print_ioctl(vfd->name, cmd);
1861                         printk(KERN_CONT " error %ld\n", ret);
1862                 }
1863         }
1864
1865         return ret;
1866 }
1867
1868 long video_ioctl2(struct file *file,
1869                unsigned int cmd, unsigned long arg)
1870 {
1871         char    sbuf[128];
1872         void    *mbuf = NULL;
1873         void    *parg = NULL;
1874         long    err  = -EINVAL;
1875         int     is_ext_ctrl;
1876         size_t  ctrls_size = 0;
1877         void __user *user_ptr = NULL;
1878
1879 #ifdef __OLD_VIDIOC_
1880         cmd = video_fix_command(cmd);
1881 #endif
1882         is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
1883                        cmd == VIDIOC_TRY_EXT_CTRLS);
1884
1885         /*  Copy arguments into temp kernel buffer  */
1886         switch (_IOC_DIR(cmd)) {
1887         case _IOC_NONE:
1888                 parg = NULL;
1889                 break;
1890         case _IOC_READ:
1891         case _IOC_WRITE:
1892         case (_IOC_WRITE | _IOC_READ):
1893                 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
1894                         parg = sbuf;
1895                 } else {
1896                         /* too big to allocate from stack */
1897                         mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
1898                         if (NULL == mbuf)
1899                                 return -ENOMEM;
1900                         parg = mbuf;
1901                 }
1902
1903                 err = -EFAULT;
1904                 if (_IOC_DIR(cmd) & _IOC_WRITE)
1905                         if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
1906                                 goto out;
1907                 break;
1908         }
1909
1910         if (is_ext_ctrl) {
1911                 struct v4l2_ext_controls *p = parg;
1912
1913                 /* In case of an error, tell the caller that it wasn't
1914                    a specific control that caused it. */
1915                 p->error_idx = p->count;
1916                 user_ptr = (void __user *)p->controls;
1917                 if (p->count) {
1918                         ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
1919                         /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
1920                         mbuf = kmalloc(ctrls_size, GFP_KERNEL);
1921                         err = -ENOMEM;
1922                         if (NULL == mbuf)
1923                                 goto out_ext_ctrl;
1924                         err = -EFAULT;
1925                         if (copy_from_user(mbuf, user_ptr, ctrls_size))
1926                                 goto out_ext_ctrl;
1927                         p->controls = mbuf;
1928                 }
1929         }
1930
1931         /* Handles IOCTL */
1932         err = __video_do_ioctl(file, cmd, parg);
1933         if (err == -ENOIOCTLCMD)
1934                 err = -EINVAL;
1935         if (is_ext_ctrl) {
1936                 struct v4l2_ext_controls *p = parg;
1937
1938                 p->controls = (void *)user_ptr;
1939                 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
1940                         err = -EFAULT;
1941                 goto out_ext_ctrl;
1942         }
1943         if (err < 0)
1944                 goto out;
1945
1946 out_ext_ctrl:
1947         /*  Copy results into user buffer  */
1948         switch (_IOC_DIR(cmd)) {
1949         case _IOC_READ:
1950         case (_IOC_WRITE | _IOC_READ):
1951                 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1952                         err = -EFAULT;
1953                 break;
1954         }
1955
1956 out:
1957         kfree(mbuf);
1958         return err;
1959 }
1960 EXPORT_SYMBOL(video_ioctl2);