V4L/DVB (10715): zoran: clean up some old V4L1 left-overs and remove the MAP_NR macro.
[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                 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                 ret = ops->vidioc_querybuf(file, fh, p);
973                 if (!ret)
974                         dbgbuf(cmd, vfd, p);
975                 break;
976         }
977         case VIDIOC_QBUF:
978         {
979                 struct v4l2_buffer *p = arg;
980
981                 if (!ops->vidioc_qbuf)
982                         break;
983                 ret = check_fmt(ops, p->type);
984                 if (ret)
985                         break;
986
987                 ret = ops->vidioc_qbuf(file, fh, p);
988                 if (!ret)
989                         dbgbuf(cmd, vfd, p);
990                 break;
991         }
992         case VIDIOC_DQBUF:
993         {
994                 struct v4l2_buffer *p = arg;
995
996                 if (!ops->vidioc_dqbuf)
997                         break;
998                 ret = check_fmt(ops, p->type);
999                 if (ret)
1000                         break;
1001
1002                 ret = ops->vidioc_dqbuf(file, fh, p);
1003                 if (!ret)
1004                         dbgbuf(cmd, vfd, p);
1005                 break;
1006         }
1007         case VIDIOC_OVERLAY:
1008         {
1009                 int *i = arg;
1010
1011                 if (!ops->vidioc_overlay)
1012                         break;
1013                 dbgarg(cmd, "value=%d\n", *i);
1014                 ret = ops->vidioc_overlay(file, fh, *i);
1015                 break;
1016         }
1017         case VIDIOC_G_FBUF:
1018         {
1019                 struct v4l2_framebuffer *p = arg;
1020
1021                 if (!ops->vidioc_g_fbuf)
1022                         break;
1023                 ret = ops->vidioc_g_fbuf(file, fh, arg);
1024                 if (!ret) {
1025                         dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
1026                                         p->capability, p->flags,
1027                                         (unsigned long)p->base);
1028                         v4l_print_pix_fmt(vfd, &p->fmt);
1029                 }
1030                 break;
1031         }
1032         case VIDIOC_S_FBUF:
1033         {
1034                 struct v4l2_framebuffer *p = arg;
1035
1036                 if (!ops->vidioc_s_fbuf)
1037                         break;
1038                 dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
1039                         p->capability, p->flags, (unsigned long)p->base);
1040                 v4l_print_pix_fmt(vfd, &p->fmt);
1041                 ret = ops->vidioc_s_fbuf(file, fh, arg);
1042                 break;
1043         }
1044         case VIDIOC_STREAMON:
1045         {
1046                 enum v4l2_buf_type i = *(int *)arg;
1047
1048                 if (!ops->vidioc_streamon)
1049                         break;
1050                 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
1051                 ret = ops->vidioc_streamon(file, fh, i);
1052                 break;
1053         }
1054         case VIDIOC_STREAMOFF:
1055         {
1056                 enum v4l2_buf_type i = *(int *)arg;
1057
1058                 if (!ops->vidioc_streamoff)
1059                         break;
1060                 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
1061                 ret = ops->vidioc_streamoff(file, fh, i);
1062                 break;
1063         }
1064         /* ---------- tv norms ---------- */
1065         case VIDIOC_ENUMSTD:
1066         {
1067                 struct v4l2_standard *p = arg;
1068                 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1069                 unsigned int index = p->index, i, j = 0;
1070                 const char *descr = "";
1071
1072                 /* Return norm array in a canonical way */
1073                 for (i = 0; i <= index && id; i++) {
1074                         /* last std value in the standards array is 0, so this
1075                            while always ends there since (id & 0) == 0. */
1076                         while ((id & standards[j].std) != standards[j].std)
1077                                 j++;
1078                         curr_id = standards[j].std;
1079                         descr = standards[j].descr;
1080                         j++;
1081                         if (curr_id == 0)
1082                                 break;
1083                         if (curr_id != V4L2_STD_PAL &&
1084                             curr_id != V4L2_STD_SECAM &&
1085                             curr_id != V4L2_STD_NTSC)
1086                                 id &= ~curr_id;
1087                 }
1088                 if (i <= index)
1089                         return -EINVAL;
1090
1091                 v4l2_video_std_construct(p, curr_id, descr);
1092                 p->index = index;
1093
1094                 dbgarg(cmd, "index=%d, id=0x%Lx, name=%s, fps=%d/%d, "
1095                                 "framelines=%d\n", p->index,
1096                                 (unsigned long long)p->id, p->name,
1097                                 p->frameperiod.numerator,
1098                                 p->frameperiod.denominator,
1099                                 p->framelines);
1100
1101                 ret = 0;
1102                 break;
1103         }
1104         case VIDIOC_G_STD:
1105         {
1106                 v4l2_std_id *id = arg;
1107
1108                 ret = 0;
1109                 /* Calls the specific handler */
1110                 if (ops->vidioc_g_std)
1111                         ret = ops->vidioc_g_std(file, fh, id);
1112                 else
1113                         *id = vfd->current_norm;
1114
1115                 if (!ret)
1116                         dbgarg(cmd, "std=0x%08Lx\n", (long long unsigned)*id);
1117                 break;
1118         }
1119         case VIDIOC_S_STD:
1120         {
1121                 v4l2_std_id *id = arg, norm;
1122
1123                 dbgarg(cmd, "std=%08Lx\n", (long long unsigned)*id);
1124
1125                 norm = (*id) & vfd->tvnorms;
1126                 if (vfd->tvnorms && !norm)      /* Check if std is supported */
1127                         break;
1128
1129                 /* Calls the specific handler */
1130                 if (ops->vidioc_s_std)
1131                         ret = ops->vidioc_s_std(file, fh, &norm);
1132                 else
1133                         ret = -EINVAL;
1134
1135                 /* Updates standard information */
1136                 if (ret >= 0)
1137                         vfd->current_norm = norm;
1138                 break;
1139         }
1140         case VIDIOC_QUERYSTD:
1141         {
1142                 v4l2_std_id *p = arg;
1143
1144                 if (!ops->vidioc_querystd)
1145                         break;
1146                 ret = ops->vidioc_querystd(file, fh, arg);
1147                 if (!ret)
1148                         dbgarg(cmd, "detected std=%08Lx\n",
1149                                                 (unsigned long long)*p);
1150                 break;
1151         }
1152         /* ------ input switching ---------- */
1153         /* FIXME: Inputs can be handled inside videodev2 */
1154         case VIDIOC_ENUMINPUT:
1155         {
1156                 struct v4l2_input *p = arg;
1157                 int i = p->index;
1158
1159                 if (!ops->vidioc_enum_input)
1160                         break;
1161                 memset(p, 0, sizeof(*p));
1162                 p->index = i;
1163
1164                 ret = ops->vidioc_enum_input(file, fh, p);
1165                 if (!ret)
1166                         dbgarg(cmd, "index=%d, name=%s, type=%d, "
1167                                 "audioset=%d, "
1168                                 "tuner=%d, std=%08Lx, status=%d\n",
1169                                 p->index, p->name, p->type, p->audioset,
1170                                 p->tuner,
1171                                 (unsigned long long)p->std,
1172                                 p->status);
1173                 break;
1174         }
1175         case VIDIOC_G_INPUT:
1176         {
1177                 unsigned int *i = arg;
1178
1179                 if (!ops->vidioc_g_input)
1180                         break;
1181                 ret = ops->vidioc_g_input(file, fh, i);
1182                 if (!ret)
1183                         dbgarg(cmd, "value=%d\n", *i);
1184                 break;
1185         }
1186         case VIDIOC_S_INPUT:
1187         {
1188                 unsigned int *i = arg;
1189
1190                 if (!ops->vidioc_s_input)
1191                         break;
1192                 dbgarg(cmd, "value=%d\n", *i);
1193                 ret = ops->vidioc_s_input(file, fh, *i);
1194                 break;
1195         }
1196
1197         /* ------ output switching ---------- */
1198         case VIDIOC_ENUMOUTPUT:
1199         {
1200                 struct v4l2_output *p = arg;
1201                 int i = p->index;
1202
1203                 if (!ops->vidioc_enum_output)
1204                         break;
1205                 memset(p, 0, sizeof(*p));
1206                 p->index = i;
1207
1208                 ret = ops->vidioc_enum_output(file, fh, p);
1209                 if (!ret)
1210                         dbgarg(cmd, "index=%d, name=%s, type=%d, "
1211                                 "audioset=0x%x, "
1212                                 "modulator=%d, std=0x%08Lx\n",
1213                                 p->index, p->name, p->type, p->audioset,
1214                                 p->modulator, (unsigned long long)p->std);
1215                 break;
1216         }
1217         case VIDIOC_G_OUTPUT:
1218         {
1219                 unsigned int *i = arg;
1220
1221                 if (!ops->vidioc_g_output)
1222                         break;
1223                 ret = ops->vidioc_g_output(file, fh, i);
1224                 if (!ret)
1225                         dbgarg(cmd, "value=%d\n", *i);
1226                 break;
1227         }
1228         case VIDIOC_S_OUTPUT:
1229         {
1230                 unsigned int *i = arg;
1231
1232                 if (!ops->vidioc_s_output)
1233                         break;
1234                 dbgarg(cmd, "value=%d\n", *i);
1235                 ret = ops->vidioc_s_output(file, fh, *i);
1236                 break;
1237         }
1238
1239         /* --- controls ---------------------------------------------- */
1240         case VIDIOC_QUERYCTRL:
1241         {
1242                 struct v4l2_queryctrl *p = arg;
1243
1244                 if (!ops->vidioc_queryctrl)
1245                         break;
1246                 ret = ops->vidioc_queryctrl(file, fh, p);
1247                 if (!ret)
1248                         dbgarg(cmd, "id=0x%x, type=%d, name=%s, min/max=%d/%d, "
1249                                         "step=%d, default=%d, flags=0x%08x\n",
1250                                         p->id, p->type, p->name,
1251                                         p->minimum, p->maximum,
1252                                         p->step, p->default_value, p->flags);
1253                 else
1254                         dbgarg(cmd, "id=0x%x\n", p->id);
1255                 break;
1256         }
1257         case VIDIOC_G_CTRL:
1258         {
1259                 struct v4l2_control *p = arg;
1260
1261                 if (ops->vidioc_g_ctrl)
1262                         ret = ops->vidioc_g_ctrl(file, fh, p);
1263                 else if (ops->vidioc_g_ext_ctrls) {
1264                         struct v4l2_ext_controls ctrls;
1265                         struct v4l2_ext_control ctrl;
1266
1267                         ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1268                         ctrls.count = 1;
1269                         ctrls.controls = &ctrl;
1270                         ctrl.id = p->id;
1271                         ctrl.value = p->value;
1272                         if (check_ext_ctrls(&ctrls, 1)) {
1273                                 ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1274                                 if (ret == 0)
1275                                         p->value = ctrl.value;
1276                         }
1277                 } else
1278                         break;
1279                 if (!ret)
1280                         dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1281                 else
1282                         dbgarg(cmd, "id=0x%x\n", p->id);
1283                 break;
1284         }
1285         case VIDIOC_S_CTRL:
1286         {
1287                 struct v4l2_control *p = arg;
1288                 struct v4l2_ext_controls ctrls;
1289                 struct v4l2_ext_control ctrl;
1290
1291                 if (!ops->vidioc_s_ctrl && !ops->vidioc_s_ext_ctrls)
1292                         break;
1293
1294                 dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1295
1296                 if (ops->vidioc_s_ctrl) {
1297                         ret = ops->vidioc_s_ctrl(file, fh, p);
1298                         break;
1299                 }
1300                 if (!ops->vidioc_s_ext_ctrls)
1301                         break;
1302
1303                 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1304                 ctrls.count = 1;
1305                 ctrls.controls = &ctrl;
1306                 ctrl.id = p->id;
1307                 ctrl.value = p->value;
1308                 if (check_ext_ctrls(&ctrls, 1))
1309                         ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1310                 break;
1311         }
1312         case VIDIOC_G_EXT_CTRLS:
1313         {
1314                 struct v4l2_ext_controls *p = arg;
1315
1316                 p->error_idx = p->count;
1317                 if (!ops->vidioc_g_ext_ctrls)
1318                         break;
1319                 if (check_ext_ctrls(p, 0))
1320                         ret = ops->vidioc_g_ext_ctrls(file, fh, p);
1321                 v4l_print_ext_ctrls(cmd, vfd, p, !ret);
1322                 break;
1323         }
1324         case VIDIOC_S_EXT_CTRLS:
1325         {
1326                 struct v4l2_ext_controls *p = arg;
1327
1328                 p->error_idx = p->count;
1329                 if (!ops->vidioc_s_ext_ctrls)
1330                         break;
1331                 v4l_print_ext_ctrls(cmd, vfd, p, 1);
1332                 if (check_ext_ctrls(p, 0))
1333                         ret = ops->vidioc_s_ext_ctrls(file, fh, p);
1334                 break;
1335         }
1336         case VIDIOC_TRY_EXT_CTRLS:
1337         {
1338                 struct v4l2_ext_controls *p = arg;
1339
1340                 p->error_idx = p->count;
1341                 if (!ops->vidioc_try_ext_ctrls)
1342                         break;
1343                 v4l_print_ext_ctrls(cmd, vfd, p, 1);
1344                 if (check_ext_ctrls(p, 0))
1345                         ret = ops->vidioc_try_ext_ctrls(file, fh, p);
1346                 break;
1347         }
1348         case VIDIOC_QUERYMENU:
1349         {
1350                 struct v4l2_querymenu *p = arg;
1351
1352                 if (!ops->vidioc_querymenu)
1353                         break;
1354                 ret = ops->vidioc_querymenu(file, fh, p);
1355                 if (!ret)
1356                         dbgarg(cmd, "id=0x%x, index=%d, name=%s\n",
1357                                 p->id, p->index, p->name);
1358                 else
1359                         dbgarg(cmd, "id=0x%x, index=%d\n",
1360                                 p->id, p->index);
1361                 break;
1362         }
1363         /* --- audio ---------------------------------------------- */
1364         case VIDIOC_ENUMAUDIO:
1365         {
1366                 struct v4l2_audio *p = arg;
1367
1368                 if (!ops->vidioc_enumaudio)
1369                         break;
1370                 ret = ops->vidioc_enumaudio(file, fh, p);
1371                 if (!ret)
1372                         dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1373                                         "mode=0x%x\n", p->index, p->name,
1374                                         p->capability, p->mode);
1375                 else
1376                         dbgarg(cmd, "index=%d\n", p->index);
1377                 break;
1378         }
1379         case VIDIOC_G_AUDIO:
1380         {
1381                 struct v4l2_audio *p = arg;
1382                 __u32 index = p->index;
1383
1384                 if (!ops->vidioc_g_audio)
1385                         break;
1386
1387                 memset(p, 0, sizeof(*p));
1388                 p->index = index;
1389                 ret = ops->vidioc_g_audio(file, fh, p);
1390                 if (!ret)
1391                         dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1392                                         "mode=0x%x\n", p->index,
1393                                         p->name, p->capability, p->mode);
1394                 else
1395                         dbgarg(cmd, "index=%d\n", p->index);
1396                 break;
1397         }
1398         case VIDIOC_S_AUDIO:
1399         {
1400                 struct v4l2_audio *p = arg;
1401
1402                 if (!ops->vidioc_s_audio)
1403                         break;
1404                 dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1405                                         "mode=0x%x\n", p->index, p->name,
1406                                         p->capability, p->mode);
1407                 ret = ops->vidioc_s_audio(file, fh, p);
1408                 break;
1409         }
1410         case VIDIOC_ENUMAUDOUT:
1411         {
1412                 struct v4l2_audioout *p = arg;
1413
1414                 if (!ops->vidioc_enumaudout)
1415                         break;
1416                 dbgarg(cmd, "Enum for index=%d\n", p->index);
1417                 ret = ops->vidioc_enumaudout(file, fh, p);
1418                 if (!ret)
1419                         dbgarg2("index=%d, name=%s, capability=%d, "
1420                                         "mode=%d\n", p->index, p->name,
1421                                         p->capability, p->mode);
1422                 break;
1423         }
1424         case VIDIOC_G_AUDOUT:
1425         {
1426                 struct v4l2_audioout *p = arg;
1427
1428                 if (!ops->vidioc_g_audout)
1429                         break;
1430                 dbgarg(cmd, "Enum for index=%d\n", p->index);
1431                 ret = ops->vidioc_g_audout(file, fh, p);
1432                 if (!ret)
1433                         dbgarg2("index=%d, name=%s, capability=%d, "
1434                                         "mode=%d\n", p->index, p->name,
1435                                         p->capability, p->mode);
1436                 break;
1437         }
1438         case VIDIOC_S_AUDOUT:
1439         {
1440                 struct v4l2_audioout *p = arg;
1441
1442                 if (!ops->vidioc_s_audout)
1443                         break;
1444                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1445                                         "mode=%d\n", p->index, p->name,
1446                                         p->capability, p->mode);
1447
1448                 ret = ops->vidioc_s_audout(file, fh, p);
1449                 break;
1450         }
1451         case VIDIOC_G_MODULATOR:
1452         {
1453                 struct v4l2_modulator *p = arg;
1454
1455                 if (!ops->vidioc_g_modulator)
1456                         break;
1457                 ret = ops->vidioc_g_modulator(file, fh, p);
1458                 if (!ret)
1459                         dbgarg(cmd, "index=%d, name=%s, "
1460                                         "capability=%d, rangelow=%d,"
1461                                         " rangehigh=%d, txsubchans=%d\n",
1462                                         p->index, p->name, p->capability,
1463                                         p->rangelow, p->rangehigh,
1464                                         p->txsubchans);
1465                 break;
1466         }
1467         case VIDIOC_S_MODULATOR:
1468         {
1469                 struct v4l2_modulator *p = arg;
1470
1471                 if (!ops->vidioc_s_modulator)
1472                         break;
1473                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1474                                 "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1475                                 p->index, p->name, p->capability, p->rangelow,
1476                                 p->rangehigh, p->txsubchans);
1477                         ret = ops->vidioc_s_modulator(file, fh, p);
1478                 break;
1479         }
1480         case VIDIOC_G_CROP:
1481         {
1482                 struct v4l2_crop *p = arg;
1483                 __u32 type;
1484
1485                 if (!ops->vidioc_g_crop)
1486                         break;
1487
1488                 type = p->type;
1489                 memset(p, 0, sizeof(*p));
1490                 p->type = type;
1491
1492                 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1493                 ret = ops->vidioc_g_crop(file, fh, p);
1494                 if (!ret)
1495                         dbgrect(vfd, "", &p->c);
1496                 break;
1497         }
1498         case VIDIOC_S_CROP:
1499         {
1500                 struct v4l2_crop *p = arg;
1501
1502                 if (!ops->vidioc_s_crop)
1503                         break;
1504                 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1505                 dbgrect(vfd, "", &p->c);
1506                 ret = ops->vidioc_s_crop(file, fh, p);
1507                 break;
1508         }
1509         case VIDIOC_CROPCAP:
1510         {
1511                 struct v4l2_cropcap *p = arg;
1512                 __u32 type;
1513
1514                 /*FIXME: Should also show v4l2_fract pixelaspect */
1515                 if (!ops->vidioc_cropcap)
1516                         break;
1517
1518                 type = p->type;
1519                 memset(p, 0, sizeof(*p));
1520                 p->type = type;
1521
1522                 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1523                 ret = ops->vidioc_cropcap(file, fh, p);
1524                 if (!ret) {
1525                         dbgrect(vfd, "bounds ", &p->bounds);
1526                         dbgrect(vfd, "defrect ", &p->defrect);
1527                 }
1528                 break;
1529         }
1530         case VIDIOC_G_JPEGCOMP:
1531         {
1532                 struct v4l2_jpegcompression *p = arg;
1533
1534                 if (!ops->vidioc_g_jpegcomp)
1535                         break;
1536
1537                 memset(p, 0, sizeof(*p));
1538
1539                 ret = ops->vidioc_g_jpegcomp(file, fh, p);
1540                 if (!ret)
1541                         dbgarg(cmd, "quality=%d, APPn=%d, "
1542                                         "APP_len=%d, COM_len=%d, "
1543                                         "jpeg_markers=%d\n",
1544                                         p->quality, p->APPn, p->APP_len,
1545                                         p->COM_len, p->jpeg_markers);
1546                 break;
1547         }
1548         case VIDIOC_S_JPEGCOMP:
1549         {
1550                 struct v4l2_jpegcompression *p = arg;
1551
1552                 if (!ops->vidioc_g_jpegcomp)
1553                         break;
1554                 dbgarg(cmd, "quality=%d, APPn=%d, APP_len=%d, "
1555                                         "COM_len=%d, jpeg_markers=%d\n",
1556                                         p->quality, p->APPn, p->APP_len,
1557                                         p->COM_len, p->jpeg_markers);
1558                         ret = ops->vidioc_s_jpegcomp(file, fh, p);
1559                 break;
1560         }
1561         case VIDIOC_G_ENC_INDEX:
1562         {
1563                 struct v4l2_enc_idx *p = arg;
1564
1565                 if (!ops->vidioc_g_enc_index)
1566                         break;
1567                 ret = ops->vidioc_g_enc_index(file, fh, p);
1568                 if (!ret)
1569                         dbgarg(cmd, "entries=%d, entries_cap=%d\n",
1570                                         p->entries, p->entries_cap);
1571                 break;
1572         }
1573         case VIDIOC_ENCODER_CMD:
1574         {
1575                 struct v4l2_encoder_cmd *p = arg;
1576
1577                 if (!ops->vidioc_encoder_cmd)
1578                         break;
1579                 memset(&p->raw, 0, sizeof(p->raw));
1580                 ret = ops->vidioc_encoder_cmd(file, fh, p);
1581                 if (!ret)
1582                         dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1583                 break;
1584         }
1585         case VIDIOC_TRY_ENCODER_CMD:
1586         {
1587                 struct v4l2_encoder_cmd *p = arg;
1588
1589                 if (!ops->vidioc_try_encoder_cmd)
1590                         break;
1591                 memset(&p->raw, 0, sizeof(p->raw));
1592                 ret = ops->vidioc_try_encoder_cmd(file, fh, p);
1593                 if (!ret)
1594                         dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1595                 break;
1596         }
1597         case VIDIOC_G_PARM:
1598         {
1599                 struct v4l2_streamparm *p = arg;
1600                 __u32 type = p->type;
1601
1602                 memset(p, 0, sizeof(*p));
1603                 p->type = type;
1604
1605                 if (ops->vidioc_g_parm) {
1606                         ret = ops->vidioc_g_parm(file, fh, p);
1607                 } else {
1608                         struct v4l2_standard s;
1609
1610                         if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1611                                 return -EINVAL;
1612
1613                         v4l2_video_std_construct(&s, vfd->current_norm,
1614                                                  v4l2_norm_to_name(vfd->current_norm));
1615
1616                         p->parm.capture.timeperframe = s.frameperiod;
1617                         ret = 0;
1618                 }
1619
1620                 dbgarg(cmd, "type=%d\n", p->type);
1621                 break;
1622         }
1623         case VIDIOC_S_PARM:
1624         {
1625                 struct v4l2_streamparm *p = arg;
1626
1627                 if (!ops->vidioc_s_parm)
1628                         break;
1629                 dbgarg(cmd, "type=%d\n", p->type);
1630                 ret = ops->vidioc_s_parm(file, fh, p);
1631                 break;
1632         }
1633         case VIDIOC_G_TUNER:
1634         {
1635                 struct v4l2_tuner *p = arg;
1636                 __u32 index = p->index;
1637
1638                 if (!ops->vidioc_g_tuner)
1639                         break;
1640
1641                 memset(p, 0, sizeof(*p));
1642                 p->index = index;
1643
1644                 ret = ops->vidioc_g_tuner(file, fh, p);
1645                 if (!ret)
1646                         dbgarg(cmd, "index=%d, name=%s, type=%d, "
1647                                         "capability=0x%x, rangelow=%d, "
1648                                         "rangehigh=%d, signal=%d, afc=%d, "
1649                                         "rxsubchans=0x%x, audmode=%d\n",
1650                                         p->index, p->name, p->type,
1651                                         p->capability, p->rangelow,
1652                                         p->rangehigh, p->signal, p->afc,
1653                                         p->rxsubchans, p->audmode);
1654                 break;
1655         }
1656         case VIDIOC_S_TUNER:
1657         {
1658                 struct v4l2_tuner *p = arg;
1659
1660                 if (!ops->vidioc_s_tuner)
1661                         break;
1662                 dbgarg(cmd, "index=%d, name=%s, type=%d, "
1663                                 "capability=0x%x, rangelow=%d, "
1664                                 "rangehigh=%d, signal=%d, afc=%d, "
1665                                 "rxsubchans=0x%x, audmode=%d\n",
1666                                 p->index, p->name, p->type,
1667                                 p->capability, p->rangelow,
1668                                 p->rangehigh, p->signal, p->afc,
1669                                 p->rxsubchans, p->audmode);
1670                 ret = ops->vidioc_s_tuner(file, fh, p);
1671                 break;
1672         }
1673         case VIDIOC_G_FREQUENCY:
1674         {
1675                 struct v4l2_frequency *p = arg;
1676
1677                 if (!ops->vidioc_g_frequency)
1678                         break;
1679
1680                 memset(p->reserved, 0, sizeof(p->reserved));
1681
1682                 ret = ops->vidioc_g_frequency(file, fh, p);
1683                 if (!ret)
1684                         dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1685                                         p->tuner, p->type, p->frequency);
1686                 break;
1687         }
1688         case VIDIOC_S_FREQUENCY:
1689         {
1690                 struct v4l2_frequency *p = arg;
1691
1692                 if (!ops->vidioc_s_frequency)
1693                         break;
1694                 dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1695                                 p->tuner, p->type, p->frequency);
1696                 ret = ops->vidioc_s_frequency(file, fh, p);
1697                 break;
1698         }
1699         case VIDIOC_G_SLICED_VBI_CAP:
1700         {
1701                 struct v4l2_sliced_vbi_cap *p = arg;
1702                 __u32 type = p->type;
1703
1704                 if (!ops->vidioc_g_sliced_vbi_cap)
1705                         break;
1706                 memset(p, 0, sizeof(*p));
1707                 p->type = type;
1708                 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1709                 ret = ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1710                 if (!ret)
1711                         dbgarg2("service_set=%d\n", p->service_set);
1712                 break;
1713         }
1714         case VIDIOC_LOG_STATUS:
1715         {
1716                 if (!ops->vidioc_log_status)
1717                         break;
1718                 ret = ops->vidioc_log_status(file, fh);
1719                 break;
1720         }
1721 #ifdef CONFIG_VIDEO_ADV_DEBUG
1722         case VIDIOC_DBG_G_REGISTER:
1723         {
1724                 struct v4l2_dbg_register *p = arg;
1725
1726                 if (!capable(CAP_SYS_ADMIN))
1727                         ret = -EPERM;
1728                 else if (ops->vidioc_g_register)
1729                         ret = ops->vidioc_g_register(file, fh, p);
1730                 break;
1731         }
1732         case VIDIOC_DBG_S_REGISTER:
1733         {
1734                 struct v4l2_dbg_register *p = arg;
1735
1736                 if (!capable(CAP_SYS_ADMIN))
1737                         ret = -EPERM;
1738                 else if (ops->vidioc_s_register)
1739                         ret = ops->vidioc_s_register(file, fh, p);
1740                 break;
1741         }
1742 #endif
1743         case VIDIOC_DBG_G_CHIP_IDENT:
1744         {
1745                 struct v4l2_dbg_chip_ident *p = arg;
1746
1747                 if (!ops->vidioc_g_chip_ident)
1748                         break;
1749                 p->ident = V4L2_IDENT_NONE;
1750                 p->revision = 0;
1751                 ret = ops->vidioc_g_chip_ident(file, fh, p);
1752                 if (!ret)
1753                         dbgarg(cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
1754                 break;
1755         }
1756         case VIDIOC_G_CHIP_IDENT_OLD:
1757                 printk(KERN_ERR "VIDIOC_G_CHIP_IDENT has been deprecated and will disappear in 2.6.30.\n");
1758                 printk(KERN_ERR "It is a debugging ioctl and must not be used in applications!\n");
1759                 return -EINVAL;
1760
1761         case VIDIOC_S_HW_FREQ_SEEK:
1762         {
1763                 struct v4l2_hw_freq_seek *p = arg;
1764
1765                 if (!ops->vidioc_s_hw_freq_seek)
1766                         break;
1767                 dbgarg(cmd,
1768                         "tuner=%d, type=%d, seek_upward=%d, wrap_around=%d\n",
1769                         p->tuner, p->type, p->seek_upward, p->wrap_around);
1770                 ret = ops->vidioc_s_hw_freq_seek(file, fh, p);
1771                 break;
1772         }
1773         case VIDIOC_ENUM_FRAMESIZES:
1774         {
1775                 struct v4l2_frmsizeenum *p = arg;
1776
1777                 if (!ops->vidioc_enum_framesizes)
1778                         break;
1779
1780                 memset(p, 0, sizeof(*p));
1781
1782                 ret = ops->vidioc_enum_framesizes(file, fh, p);
1783                 dbgarg(cmd,
1784                         "index=%d, pixelformat=%d, type=%d ",
1785                         p->index, p->pixel_format, p->type);
1786                 switch (p->type) {
1787                 case V4L2_FRMSIZE_TYPE_DISCRETE:
1788                         dbgarg2("width = %d, height=%d\n",
1789                                 p->discrete.width, p->discrete.height);
1790                         break;
1791                 case V4L2_FRMSIZE_TYPE_STEPWISE:
1792                         dbgarg2("min %dx%d, max %dx%d, step %dx%d\n",
1793                                 p->stepwise.min_width,  p->stepwise.min_height,
1794                                 p->stepwise.step_width, p->stepwise.step_height,
1795                                 p->stepwise.max_width,  p->stepwise.max_height);
1796                         break;
1797                 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
1798                         dbgarg2("continuous\n");
1799                         break;
1800                 default:
1801                         dbgarg2("- Unknown type!\n");
1802                 }
1803
1804                 break;
1805         }
1806         case VIDIOC_ENUM_FRAMEINTERVALS:
1807         {
1808                 struct v4l2_frmivalenum *p = arg;
1809
1810                 if (!ops->vidioc_enum_frameintervals)
1811                         break;
1812
1813                 memset(p, 0, sizeof(*p));
1814
1815                 ret = ops->vidioc_enum_frameintervals(file, fh, p);
1816                 dbgarg(cmd,
1817                         "index=%d, pixelformat=%d, width=%d, height=%d, type=%d ",
1818                         p->index, p->pixel_format,
1819                         p->width, p->height, p->type);
1820                 switch (p->type) {
1821                 case V4L2_FRMIVAL_TYPE_DISCRETE:
1822                         dbgarg2("fps=%d/%d\n",
1823                                 p->discrete.numerator,
1824                                 p->discrete.denominator);
1825                         break;
1826                 case V4L2_FRMIVAL_TYPE_STEPWISE:
1827                         dbgarg2("min=%d/%d, max=%d/%d, step=%d/%d\n",
1828                                 p->stepwise.min.numerator,
1829                                 p->stepwise.min.denominator,
1830                                 p->stepwise.max.numerator,
1831                                 p->stepwise.max.denominator,
1832                                 p->stepwise.step.numerator,
1833                                 p->stepwise.step.denominator);
1834                         break;
1835                 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1836                         dbgarg2("continuous\n");
1837                         break;
1838                 default:
1839                         dbgarg2("- Unknown type!\n");
1840                 }
1841                 break;
1842         }
1843
1844         default:
1845         {
1846                 if (!ops->vidioc_default)
1847                         break;
1848                 ret = ops->vidioc_default(file, fh, cmd, arg);
1849                 break;
1850         }
1851         } /* switch */
1852
1853         if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
1854                 if (ret < 0) {
1855                         v4l_print_ioctl(vfd->name, cmd);
1856                         printk(KERN_CONT " error %ld\n", ret);
1857                 }
1858         }
1859
1860         return ret;
1861 }
1862
1863 long video_ioctl2(struct file *file,
1864                unsigned int cmd, unsigned long arg)
1865 {
1866         char    sbuf[128];
1867         void    *mbuf = NULL;
1868         void    *parg = NULL;
1869         long    err  = -EINVAL;
1870         int     is_ext_ctrl;
1871         size_t  ctrls_size = 0;
1872         void __user *user_ptr = NULL;
1873
1874 #ifdef __OLD_VIDIOC_
1875         cmd = video_fix_command(cmd);
1876 #endif
1877         is_ext_ctrl = (cmd == VIDIOC_S_EXT_CTRLS || cmd == VIDIOC_G_EXT_CTRLS ||
1878                        cmd == VIDIOC_TRY_EXT_CTRLS);
1879
1880         /*  Copy arguments into temp kernel buffer  */
1881         switch (_IOC_DIR(cmd)) {
1882         case _IOC_NONE:
1883                 parg = NULL;
1884                 break;
1885         case _IOC_READ:
1886         case _IOC_WRITE:
1887         case (_IOC_WRITE | _IOC_READ):
1888                 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
1889                         parg = sbuf;
1890                 } else {
1891                         /* too big to allocate from stack */
1892                         mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
1893                         if (NULL == mbuf)
1894                                 return -ENOMEM;
1895                         parg = mbuf;
1896                 }
1897
1898                 err = -EFAULT;
1899                 if (_IOC_DIR(cmd) & _IOC_WRITE)
1900                         if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
1901                                 goto out;
1902                 break;
1903         }
1904
1905         if (is_ext_ctrl) {
1906                 struct v4l2_ext_controls *p = parg;
1907
1908                 /* In case of an error, tell the caller that it wasn't
1909                    a specific control that caused it. */
1910                 p->error_idx = p->count;
1911                 user_ptr = (void __user *)p->controls;
1912                 if (p->count) {
1913                         ctrls_size = sizeof(struct v4l2_ext_control) * p->count;
1914                         /* Note: v4l2_ext_controls fits in sbuf[] so mbuf is still NULL. */
1915                         mbuf = kmalloc(ctrls_size, GFP_KERNEL);
1916                         err = -ENOMEM;
1917                         if (NULL == mbuf)
1918                                 goto out_ext_ctrl;
1919                         err = -EFAULT;
1920                         if (copy_from_user(mbuf, user_ptr, ctrls_size))
1921                                 goto out_ext_ctrl;
1922                         p->controls = mbuf;
1923                 }
1924         }
1925
1926         /* Handles IOCTL */
1927         err = __video_do_ioctl(file, cmd, parg);
1928         if (err == -ENOIOCTLCMD)
1929                 err = -EINVAL;
1930         if (is_ext_ctrl) {
1931                 struct v4l2_ext_controls *p = parg;
1932
1933                 p->controls = (void *)user_ptr;
1934                 if (p->count && err == 0 && copy_to_user(user_ptr, mbuf, ctrls_size))
1935                         err = -EFAULT;
1936                 goto out_ext_ctrl;
1937         }
1938         if (err < 0)
1939                 goto out;
1940
1941 out_ext_ctrl:
1942         /*  Copy results into user buffer  */
1943         switch (_IOC_DIR(cmd)) {
1944         case _IOC_READ:
1945         case (_IOC_WRITE | _IOC_READ):
1946                 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1947                         err = -EFAULT;
1948                 break;
1949         }
1950
1951 out:
1952         kfree(mbuf);
1953         return err;
1954 }
1955 EXPORT_SYMBOL(video_ioctl2);