[media] v4l: add support for selection api
[linux-2.6.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/slab.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/version.h>
20
21 #include <linux/videodev2.h>
22
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-fh.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-chip-ident.h>
30
31 #define dbgarg(cmd, fmt, arg...) \
32                 do {                                                    \
33                     if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {            \
34                         printk(KERN_DEBUG "%s: ",  vfd->name);          \
35                         v4l_printk_ioctl(cmd);                          \
36                         printk(" " fmt,  ## arg);                       \
37                     }                                                   \
38                 } while (0)
39
40 #define dbgarg2(fmt, arg...) \
41                 do {                                                    \
42                     if (vfd->debug & V4L2_DEBUG_IOCTL_ARG)              \
43                         printk(KERN_DEBUG "%s: " fmt, vfd->name, ## arg);\
44                 } while (0)
45
46 #define dbgarg3(fmt, arg...) \
47                 do {                                                    \
48                     if (vfd->debug & V4L2_DEBUG_IOCTL_ARG)              \
49                         printk(KERN_CONT "%s: " fmt, vfd->name, ## arg);\
50                 } while (0)
51
52 /* Zero out the end of the struct pointed to by p.  Everything after, but
53  * not including, the specified field is cleared. */
54 #define CLEAR_AFTER_FIELD(p, field) \
55         memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
56         0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
57
58 #define have_fmt_ops(foo) (                                             \
59         ops->vidioc_##foo##_fmt_vid_cap ||                              \
60         ops->vidioc_##foo##_fmt_vid_out ||                              \
61         ops->vidioc_##foo##_fmt_vid_cap_mplane ||                       \
62         ops->vidioc_##foo##_fmt_vid_out_mplane ||                       \
63         ops->vidioc_##foo##_fmt_vid_overlay ||                          \
64         ops->vidioc_##foo##_fmt_vbi_cap ||                              \
65         ops->vidioc_##foo##_fmt_vid_out_overlay ||                      \
66         ops->vidioc_##foo##_fmt_vbi_out ||                              \
67         ops->vidioc_##foo##_fmt_sliced_vbi_cap ||                       \
68         ops->vidioc_##foo##_fmt_sliced_vbi_out ||                       \
69         ops->vidioc_##foo##_fmt_type_private)
70
71 struct std_descr {
72         v4l2_std_id std;
73         const char *descr;
74 };
75
76 static const struct std_descr standards[] = {
77         { V4L2_STD_NTSC,        "NTSC"      },
78         { V4L2_STD_NTSC_M,      "NTSC-M"    },
79         { V4L2_STD_NTSC_M_JP,   "NTSC-M-JP" },
80         { V4L2_STD_NTSC_M_KR,   "NTSC-M-KR" },
81         { V4L2_STD_NTSC_443,    "NTSC-443"  },
82         { V4L2_STD_PAL,         "PAL"       },
83         { V4L2_STD_PAL_BG,      "PAL-BG"    },
84         { V4L2_STD_PAL_B,       "PAL-B"     },
85         { V4L2_STD_PAL_B1,      "PAL-B1"    },
86         { V4L2_STD_PAL_G,       "PAL-G"     },
87         { V4L2_STD_PAL_H,       "PAL-H"     },
88         { V4L2_STD_PAL_I,       "PAL-I"     },
89         { V4L2_STD_PAL_DK,      "PAL-DK"    },
90         { V4L2_STD_PAL_D,       "PAL-D"     },
91         { V4L2_STD_PAL_D1,      "PAL-D1"    },
92         { V4L2_STD_PAL_K,       "PAL-K"     },
93         { V4L2_STD_PAL_M,       "PAL-M"     },
94         { V4L2_STD_PAL_N,       "PAL-N"     },
95         { V4L2_STD_PAL_Nc,      "PAL-Nc"    },
96         { V4L2_STD_PAL_60,      "PAL-60"    },
97         { V4L2_STD_SECAM,       "SECAM"     },
98         { V4L2_STD_SECAM_B,     "SECAM-B"   },
99         { V4L2_STD_SECAM_G,     "SECAM-G"   },
100         { V4L2_STD_SECAM_H,     "SECAM-H"   },
101         { V4L2_STD_SECAM_DK,    "SECAM-DK"  },
102         { V4L2_STD_SECAM_D,     "SECAM-D"   },
103         { V4L2_STD_SECAM_K,     "SECAM-K"   },
104         { V4L2_STD_SECAM_K1,    "SECAM-K1"  },
105         { V4L2_STD_SECAM_L,     "SECAM-L"   },
106         { V4L2_STD_SECAM_LC,    "SECAM-Lc"  },
107         { 0,                    "Unknown"   }
108 };
109
110 /* video4linux standard ID conversion to standard name
111  */
112 const char *v4l2_norm_to_name(v4l2_std_id id)
113 {
114         u32 myid = id;
115         int i;
116
117         /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
118            64 bit comparations. So, on that architecture, with some gcc
119            variants, compilation fails. Currently, the max value is 30bit wide.
120          */
121         BUG_ON(myid != id);
122
123         for (i = 0; standards[i].std; i++)
124                 if (myid == standards[i].std)
125                         break;
126         return standards[i].descr;
127 }
128 EXPORT_SYMBOL(v4l2_norm_to_name);
129
130 /* Returns frame period for the given standard */
131 void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
132 {
133         if (id & V4L2_STD_525_60) {
134                 frameperiod->numerator = 1001;
135                 frameperiod->denominator = 30000;
136         } else {
137                 frameperiod->numerator = 1;
138                 frameperiod->denominator = 25;
139         }
140 }
141 EXPORT_SYMBOL(v4l2_video_std_frame_period);
142
143 /* Fill in the fields of a v4l2_standard structure according to the
144    'id' and 'transmission' parameters.  Returns negative on error.  */
145 int v4l2_video_std_construct(struct v4l2_standard *vs,
146                              int id, const char *name)
147 {
148         vs->id = id;
149         v4l2_video_std_frame_period(id, &vs->frameperiod);
150         vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
151         strlcpy(vs->name, name, sizeof(vs->name));
152         return 0;
153 }
154 EXPORT_SYMBOL(v4l2_video_std_construct);
155
156 /* ----------------------------------------------------------------- */
157 /* some arrays for pretty-printing debug messages of enum types      */
158
159 const char *v4l2_field_names[] = {
160         [V4L2_FIELD_ANY]        = "any",
161         [V4L2_FIELD_NONE]       = "none",
162         [V4L2_FIELD_TOP]        = "top",
163         [V4L2_FIELD_BOTTOM]     = "bottom",
164         [V4L2_FIELD_INTERLACED] = "interlaced",
165         [V4L2_FIELD_SEQ_TB]     = "seq-tb",
166         [V4L2_FIELD_SEQ_BT]     = "seq-bt",
167         [V4L2_FIELD_ALTERNATE]  = "alternate",
168         [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
169         [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
170 };
171 EXPORT_SYMBOL(v4l2_field_names);
172
173 const char *v4l2_type_names[] = {
174         [V4L2_BUF_TYPE_VIDEO_CAPTURE]      = "vid-cap",
175         [V4L2_BUF_TYPE_VIDEO_OVERLAY]      = "vid-overlay",
176         [V4L2_BUF_TYPE_VIDEO_OUTPUT]       = "vid-out",
177         [V4L2_BUF_TYPE_VBI_CAPTURE]        = "vbi-cap",
178         [V4L2_BUF_TYPE_VBI_OUTPUT]         = "vbi-out",
179         [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
180         [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT]  = "sliced-vbi-out",
181         [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
182         [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
183         [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
184 };
185 EXPORT_SYMBOL(v4l2_type_names);
186
187 static const char *v4l2_memory_names[] = {
188         [V4L2_MEMORY_MMAP]    = "mmap",
189         [V4L2_MEMORY_USERPTR] = "userptr",
190         [V4L2_MEMORY_OVERLAY] = "overlay",
191 };
192
193 #define prt_names(a, arr) ((((a) >= 0) && ((a) < ARRAY_SIZE(arr))) ? \
194                            arr[a] : "unknown")
195
196 /* ------------------------------------------------------------------ */
197 /* debug help functions                                               */
198 static const char *v4l2_ioctls[] = {
199         [_IOC_NR(VIDIOC_QUERYCAP)]         = "VIDIOC_QUERYCAP",
200         [_IOC_NR(VIDIOC_RESERVED)]         = "VIDIOC_RESERVED",
201         [_IOC_NR(VIDIOC_ENUM_FMT)]         = "VIDIOC_ENUM_FMT",
202         [_IOC_NR(VIDIOC_G_FMT)]            = "VIDIOC_G_FMT",
203         [_IOC_NR(VIDIOC_S_FMT)]            = "VIDIOC_S_FMT",
204         [_IOC_NR(VIDIOC_REQBUFS)]          = "VIDIOC_REQBUFS",
205         [_IOC_NR(VIDIOC_QUERYBUF)]         = "VIDIOC_QUERYBUF",
206         [_IOC_NR(VIDIOC_G_FBUF)]           = "VIDIOC_G_FBUF",
207         [_IOC_NR(VIDIOC_S_FBUF)]           = "VIDIOC_S_FBUF",
208         [_IOC_NR(VIDIOC_OVERLAY)]          = "VIDIOC_OVERLAY",
209         [_IOC_NR(VIDIOC_QBUF)]             = "VIDIOC_QBUF",
210         [_IOC_NR(VIDIOC_DQBUF)]            = "VIDIOC_DQBUF",
211         [_IOC_NR(VIDIOC_STREAMON)]         = "VIDIOC_STREAMON",
212         [_IOC_NR(VIDIOC_STREAMOFF)]        = "VIDIOC_STREAMOFF",
213         [_IOC_NR(VIDIOC_G_PARM)]           = "VIDIOC_G_PARM",
214         [_IOC_NR(VIDIOC_S_PARM)]           = "VIDIOC_S_PARM",
215         [_IOC_NR(VIDIOC_G_STD)]            = "VIDIOC_G_STD",
216         [_IOC_NR(VIDIOC_S_STD)]            = "VIDIOC_S_STD",
217         [_IOC_NR(VIDIOC_ENUMSTD)]          = "VIDIOC_ENUMSTD",
218         [_IOC_NR(VIDIOC_ENUMINPUT)]        = "VIDIOC_ENUMINPUT",
219         [_IOC_NR(VIDIOC_G_CTRL)]           = "VIDIOC_G_CTRL",
220         [_IOC_NR(VIDIOC_S_CTRL)]           = "VIDIOC_S_CTRL",
221         [_IOC_NR(VIDIOC_G_TUNER)]          = "VIDIOC_G_TUNER",
222         [_IOC_NR(VIDIOC_S_TUNER)]          = "VIDIOC_S_TUNER",
223         [_IOC_NR(VIDIOC_G_AUDIO)]          = "VIDIOC_G_AUDIO",
224         [_IOC_NR(VIDIOC_S_AUDIO)]          = "VIDIOC_S_AUDIO",
225         [_IOC_NR(VIDIOC_QUERYCTRL)]        = "VIDIOC_QUERYCTRL",
226         [_IOC_NR(VIDIOC_QUERYMENU)]        = "VIDIOC_QUERYMENU",
227         [_IOC_NR(VIDIOC_G_INPUT)]          = "VIDIOC_G_INPUT",
228         [_IOC_NR(VIDIOC_S_INPUT)]          = "VIDIOC_S_INPUT",
229         [_IOC_NR(VIDIOC_G_OUTPUT)]         = "VIDIOC_G_OUTPUT",
230         [_IOC_NR(VIDIOC_S_OUTPUT)]         = "VIDIOC_S_OUTPUT",
231         [_IOC_NR(VIDIOC_ENUMOUTPUT)]       = "VIDIOC_ENUMOUTPUT",
232         [_IOC_NR(VIDIOC_G_AUDOUT)]         = "VIDIOC_G_AUDOUT",
233         [_IOC_NR(VIDIOC_S_AUDOUT)]         = "VIDIOC_S_AUDOUT",
234         [_IOC_NR(VIDIOC_G_MODULATOR)]      = "VIDIOC_G_MODULATOR",
235         [_IOC_NR(VIDIOC_S_MODULATOR)]      = "VIDIOC_S_MODULATOR",
236         [_IOC_NR(VIDIOC_G_FREQUENCY)]      = "VIDIOC_G_FREQUENCY",
237         [_IOC_NR(VIDIOC_S_FREQUENCY)]      = "VIDIOC_S_FREQUENCY",
238         [_IOC_NR(VIDIOC_CROPCAP)]          = "VIDIOC_CROPCAP",
239         [_IOC_NR(VIDIOC_G_CROP)]           = "VIDIOC_G_CROP",
240         [_IOC_NR(VIDIOC_S_CROP)]           = "VIDIOC_S_CROP",
241         [_IOC_NR(VIDIOC_G_SELECTION)]      = "VIDIOC_G_SELECTION",
242         [_IOC_NR(VIDIOC_S_SELECTION)]      = "VIDIOC_S_SELECTION",
243         [_IOC_NR(VIDIOC_G_JPEGCOMP)]       = "VIDIOC_G_JPEGCOMP",
244         [_IOC_NR(VIDIOC_S_JPEGCOMP)]       = "VIDIOC_S_JPEGCOMP",
245         [_IOC_NR(VIDIOC_QUERYSTD)]         = "VIDIOC_QUERYSTD",
246         [_IOC_NR(VIDIOC_TRY_FMT)]          = "VIDIOC_TRY_FMT",
247         [_IOC_NR(VIDIOC_ENUMAUDIO)]        = "VIDIOC_ENUMAUDIO",
248         [_IOC_NR(VIDIOC_ENUMAUDOUT)]       = "VIDIOC_ENUMAUDOUT",
249         [_IOC_NR(VIDIOC_G_PRIORITY)]       = "VIDIOC_G_PRIORITY",
250         [_IOC_NR(VIDIOC_S_PRIORITY)]       = "VIDIOC_S_PRIORITY",
251         [_IOC_NR(VIDIOC_G_SLICED_VBI_CAP)] = "VIDIOC_G_SLICED_VBI_CAP",
252         [_IOC_NR(VIDIOC_LOG_STATUS)]       = "VIDIOC_LOG_STATUS",
253         [_IOC_NR(VIDIOC_G_EXT_CTRLS)]      = "VIDIOC_G_EXT_CTRLS",
254         [_IOC_NR(VIDIOC_S_EXT_CTRLS)]      = "VIDIOC_S_EXT_CTRLS",
255         [_IOC_NR(VIDIOC_TRY_EXT_CTRLS)]    = "VIDIOC_TRY_EXT_CTRLS",
256 #if 1
257         [_IOC_NR(VIDIOC_ENUM_FRAMESIZES)]  = "VIDIOC_ENUM_FRAMESIZES",
258         [_IOC_NR(VIDIOC_ENUM_FRAMEINTERVALS)] = "VIDIOC_ENUM_FRAMEINTERVALS",
259         [_IOC_NR(VIDIOC_G_ENC_INDEX)]      = "VIDIOC_G_ENC_INDEX",
260         [_IOC_NR(VIDIOC_ENCODER_CMD)]      = "VIDIOC_ENCODER_CMD",
261         [_IOC_NR(VIDIOC_TRY_ENCODER_CMD)]  = "VIDIOC_TRY_ENCODER_CMD",
262
263         [_IOC_NR(VIDIOC_DBG_S_REGISTER)]   = "VIDIOC_DBG_S_REGISTER",
264         [_IOC_NR(VIDIOC_DBG_G_REGISTER)]   = "VIDIOC_DBG_G_REGISTER",
265
266         [_IOC_NR(VIDIOC_DBG_G_CHIP_IDENT)] = "VIDIOC_DBG_G_CHIP_IDENT",
267         [_IOC_NR(VIDIOC_S_HW_FREQ_SEEK)]   = "VIDIOC_S_HW_FREQ_SEEK",
268 #endif
269         [_IOC_NR(VIDIOC_ENUM_DV_PRESETS)]  = "VIDIOC_ENUM_DV_PRESETS",
270         [_IOC_NR(VIDIOC_S_DV_PRESET)]      = "VIDIOC_S_DV_PRESET",
271         [_IOC_NR(VIDIOC_G_DV_PRESET)]      = "VIDIOC_G_DV_PRESET",
272         [_IOC_NR(VIDIOC_QUERY_DV_PRESET)]  = "VIDIOC_QUERY_DV_PRESET",
273         [_IOC_NR(VIDIOC_S_DV_TIMINGS)]     = "VIDIOC_S_DV_TIMINGS",
274         [_IOC_NR(VIDIOC_G_DV_TIMINGS)]     = "VIDIOC_G_DV_TIMINGS",
275         [_IOC_NR(VIDIOC_DQEVENT)]          = "VIDIOC_DQEVENT",
276         [_IOC_NR(VIDIOC_SUBSCRIBE_EVENT)]  = "VIDIOC_SUBSCRIBE_EVENT",
277         [_IOC_NR(VIDIOC_UNSUBSCRIBE_EVENT)] = "VIDIOC_UNSUBSCRIBE_EVENT",
278         [_IOC_NR(VIDIOC_CREATE_BUFS)]      = "VIDIOC_CREATE_BUFS",
279         [_IOC_NR(VIDIOC_PREPARE_BUF)]      = "VIDIOC_PREPARE_BUF",
280 };
281 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
282
283 /* Common ioctl debug function. This function can be used by
284    external ioctl messages as well as internal V4L ioctl */
285 void v4l_printk_ioctl(unsigned int cmd)
286 {
287         char *dir, *type;
288
289         switch (_IOC_TYPE(cmd)) {
290         case 'd':
291                 type = "v4l2_int";
292                 break;
293         case 'V':
294                 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
295                         type = "v4l2";
296                         break;
297                 }
298                 printk("%s", v4l2_ioctls[_IOC_NR(cmd)]);
299                 return;
300         default:
301                 type = "unknown";
302         }
303
304         switch (_IOC_DIR(cmd)) {
305         case _IOC_NONE:              dir = "--"; break;
306         case _IOC_READ:              dir = "r-"; break;
307         case _IOC_WRITE:             dir = "-w"; break;
308         case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
309         default:                     dir = "*ERR*"; break;
310         }
311         printk("%s ioctl '%c', dir=%s, #%d (0x%08x)",
312                 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
313 }
314 EXPORT_SYMBOL(v4l_printk_ioctl);
315
316 static void dbgbuf(unsigned int cmd, struct video_device *vfd,
317                                         struct v4l2_buffer *p)
318 {
319         struct v4l2_timecode *tc = &p->timecode;
320         struct v4l2_plane *plane;
321         int i;
322
323         dbgarg(cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
324                 "flags=0x%08d, field=%0d, sequence=%d, memory=%s\n",
325                         p->timestamp.tv_sec / 3600,
326                         (int)(p->timestamp.tv_sec / 60) % 60,
327                         (int)(p->timestamp.tv_sec % 60),
328                         (long)p->timestamp.tv_usec,
329                         p->index,
330                         prt_names(p->type, v4l2_type_names),
331                         p->flags, p->field, p->sequence,
332                         prt_names(p->memory, v4l2_memory_names));
333
334         if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
335                 for (i = 0; i < p->length; ++i) {
336                         plane = &p->m.planes[i];
337                         dbgarg2("plane %d: bytesused=%d, data_offset=0x%08x "
338                                 "offset/userptr=0x%08lx, length=%d\n",
339                                 i, plane->bytesused, plane->data_offset,
340                                 plane->m.userptr, plane->length);
341                 }
342         } else {
343                 dbgarg2("bytesused=%d, offset/userptr=0x%08lx, length=%d\n",
344                         p->bytesused, p->m.userptr, p->length);
345         }
346
347         dbgarg2("timecode=%02d:%02d:%02d type=%d, "
348                 "flags=0x%08d, frames=%d, userbits=0x%08x\n",
349                         tc->hours, tc->minutes, tc->seconds,
350                         tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
351 }
352
353 static inline void dbgrect(struct video_device *vfd, char *s,
354                                                         struct v4l2_rect *r)
355 {
356         dbgarg2("%sRect start at %dx%d, size=%dx%d\n", s, r->left, r->top,
357                                                 r->width, r->height);
358 };
359
360 static inline void v4l_print_pix_fmt(struct video_device *vfd,
361                                                 struct v4l2_pix_format *fmt)
362 {
363         dbgarg2("width=%d, height=%d, format=%c%c%c%c, field=%s, "
364                 "bytesperline=%d sizeimage=%d, colorspace=%d\n",
365                 fmt->width, fmt->height,
366                 (fmt->pixelformat & 0xff),
367                 (fmt->pixelformat >>  8) & 0xff,
368                 (fmt->pixelformat >> 16) & 0xff,
369                 (fmt->pixelformat >> 24) & 0xff,
370                 prt_names(fmt->field, v4l2_field_names),
371                 fmt->bytesperline, fmt->sizeimage, fmt->colorspace);
372 };
373
374 static inline void v4l_print_pix_fmt_mplane(struct video_device *vfd,
375                                             struct v4l2_pix_format_mplane *fmt)
376 {
377         int i;
378
379         dbgarg2("width=%d, height=%d, format=%c%c%c%c, field=%s, "
380                 "colorspace=%d, num_planes=%d\n",
381                 fmt->width, fmt->height,
382                 (fmt->pixelformat & 0xff),
383                 (fmt->pixelformat >>  8) & 0xff,
384                 (fmt->pixelformat >> 16) & 0xff,
385                 (fmt->pixelformat >> 24) & 0xff,
386                 prt_names(fmt->field, v4l2_field_names),
387                 fmt->colorspace, fmt->num_planes);
388
389         for (i = 0; i < fmt->num_planes; ++i)
390                 dbgarg2("plane %d: bytesperline=%d sizeimage=%d\n", i,
391                         fmt->plane_fmt[i].bytesperline,
392                         fmt->plane_fmt[i].sizeimage);
393 }
394
395 static inline void v4l_print_ext_ctrls(unsigned int cmd,
396         struct video_device *vfd, struct v4l2_ext_controls *c, int show_vals)
397 {
398         __u32 i;
399
400         if (!(vfd->debug & V4L2_DEBUG_IOCTL_ARG))
401                 return;
402         dbgarg(cmd, "");
403         printk(KERN_CONT "class=0x%x", c->ctrl_class);
404         for (i = 0; i < c->count; i++) {
405                 if (show_vals && !c->controls[i].size)
406                         printk(KERN_CONT " id/val=0x%x/0x%x",
407                                 c->controls[i].id, c->controls[i].value);
408                 else
409                         printk(KERN_CONT " id=0x%x,size=%u",
410                                 c->controls[i].id, c->controls[i].size);
411         }
412         printk(KERN_CONT "\n");
413 };
414
415 static inline int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
416 {
417         __u32 i;
418
419         /* zero the reserved fields */
420         c->reserved[0] = c->reserved[1] = 0;
421         for (i = 0; i < c->count; i++)
422                 c->controls[i].reserved2[0] = 0;
423
424         /* V4L2_CID_PRIVATE_BASE cannot be used as control class
425            when using extended controls.
426            Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
427            is it allowed for backwards compatibility.
428          */
429         if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
430                 return 0;
431         /* Check that all controls are from the same control class. */
432         for (i = 0; i < c->count; i++) {
433                 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
434                         c->error_idx = i;
435                         return 0;
436                 }
437         }
438         return 1;
439 }
440
441 static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
442 {
443         if (ops == NULL)
444                 return -EINVAL;
445
446         switch (type) {
447         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
448                 if (ops->vidioc_g_fmt_vid_cap ||
449                                 ops->vidioc_g_fmt_vid_cap_mplane)
450                         return 0;
451                 break;
452         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
453                 if (ops->vidioc_g_fmt_vid_cap_mplane)
454                         return 0;
455                 break;
456         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
457                 if (ops->vidioc_g_fmt_vid_overlay)
458                         return 0;
459                 break;
460         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
461                 if (ops->vidioc_g_fmt_vid_out ||
462                                 ops->vidioc_g_fmt_vid_out_mplane)
463                         return 0;
464                 break;
465         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
466                 if (ops->vidioc_g_fmt_vid_out_mplane)
467                         return 0;
468                 break;
469         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
470                 if (ops->vidioc_g_fmt_vid_out_overlay)
471                         return 0;
472                 break;
473         case V4L2_BUF_TYPE_VBI_CAPTURE:
474                 if (ops->vidioc_g_fmt_vbi_cap)
475                         return 0;
476                 break;
477         case V4L2_BUF_TYPE_VBI_OUTPUT:
478                 if (ops->vidioc_g_fmt_vbi_out)
479                         return 0;
480                 break;
481         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
482                 if (ops->vidioc_g_fmt_sliced_vbi_cap)
483                         return 0;
484                 break;
485         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
486                 if (ops->vidioc_g_fmt_sliced_vbi_out)
487                         return 0;
488                 break;
489         case V4L2_BUF_TYPE_PRIVATE:
490                 if (ops->vidioc_g_fmt_type_private)
491                         return 0;
492                 break;
493         }
494         return -EINVAL;
495 }
496
497 static long __video_do_ioctl(struct file *file,
498                 unsigned int cmd, void *arg)
499 {
500         struct video_device *vfd = video_devdata(file);
501         const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
502         void *fh = file->private_data;
503         struct v4l2_fh *vfh = NULL;
504         int use_fh_prio = 0;
505         long ret_prio = 0;
506         long ret = -ENOTTY;
507
508         if (ops == NULL) {
509                 printk(KERN_WARNING "videodev: \"%s\" has no ioctl_ops.\n",
510                                 vfd->name);
511                 return ret;
512         }
513
514         if ((vfd->debug & V4L2_DEBUG_IOCTL) &&
515                                 !(vfd->debug & V4L2_DEBUG_IOCTL_ARG)) {
516                 v4l_print_ioctl(vfd->name, cmd);
517                 printk(KERN_CONT "\n");
518         }
519
520         if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
521                 vfh = file->private_data;
522                 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
523         }
524
525         if (use_fh_prio)
526                 ret_prio = v4l2_prio_check(vfd->prio, vfh->prio);
527
528         switch (cmd) {
529
530         /* --- capabilities ------------------------------------------ */
531         case VIDIOC_QUERYCAP:
532         {
533                 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
534
535                 if (!ops->vidioc_querycap)
536                         break;
537
538                 cap->version = LINUX_VERSION_CODE;
539                 ret = ops->vidioc_querycap(file, fh, cap);
540                 if (!ret)
541                         dbgarg(cmd, "driver=%s, card=%s, bus=%s, "
542                                         "version=0x%08x, "
543                                         "capabilities=0x%08x\n",
544                                         cap->driver, cap->card, cap->bus_info,
545                                         cap->version,
546                                         cap->capabilities);
547                 break;
548         }
549
550         /* --- priority ------------------------------------------ */
551         case VIDIOC_G_PRIORITY:
552         {
553                 enum v4l2_priority *p = arg;
554
555                 if (ops->vidioc_g_priority) {
556                         ret = ops->vidioc_g_priority(file, fh, p);
557                 } else if (use_fh_prio) {
558                         *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
559                         ret = 0;
560                 }
561                 if (!ret)
562                         dbgarg(cmd, "priority is %d\n", *p);
563                 break;
564         }
565         case VIDIOC_S_PRIORITY:
566         {
567                 enum v4l2_priority *p = arg;
568
569                 if (!ops->vidioc_s_priority && !use_fh_prio)
570                         break;
571                 dbgarg(cmd, "setting priority to %d\n", *p);
572                 if (ops->vidioc_s_priority)
573                         ret = ops->vidioc_s_priority(file, fh, *p);
574                 else
575                         ret = ret_prio ? ret_prio :
576                                 v4l2_prio_change(&vfd->v4l2_dev->prio,
577                                                         &vfh->prio, *p);
578                 break;
579         }
580
581         /* --- capture ioctls ---------------------------------------- */
582         case VIDIOC_ENUM_FMT:
583         {
584                 struct v4l2_fmtdesc *f = arg;
585
586                 switch (f->type) {
587                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
588                         if (likely(ops->vidioc_enum_fmt_vid_cap))
589                                 ret = ops->vidioc_enum_fmt_vid_cap(file, fh, f);
590                         break;
591                 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
592                         if (likely(ops->vidioc_enum_fmt_vid_cap_mplane))
593                                 ret = ops->vidioc_enum_fmt_vid_cap_mplane(file,
594                                                                         fh, f);
595                         break;
596                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
597                         if (likely(ops->vidioc_enum_fmt_vid_overlay))
598                                 ret = ops->vidioc_enum_fmt_vid_overlay(file,
599                                         fh, f);
600                         break;
601                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
602                         if (likely(ops->vidioc_enum_fmt_vid_out))
603                                 ret = ops->vidioc_enum_fmt_vid_out(file, fh, f);
604                         break;
605                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
606                         if (likely(ops->vidioc_enum_fmt_vid_out_mplane))
607                                 ret = ops->vidioc_enum_fmt_vid_out_mplane(file,
608                                                                         fh, f);
609                         break;
610                 case V4L2_BUF_TYPE_PRIVATE:
611                         if (likely(ops->vidioc_enum_fmt_type_private))
612                                 ret = ops->vidioc_enum_fmt_type_private(file,
613                                                                 fh, f);
614                         break;
615                 default:
616                         break;
617                 }
618                 if (likely (!ret))
619                         dbgarg(cmd, "index=%d, type=%d, flags=%d, "
620                                 "pixelformat=%c%c%c%c, description='%s'\n",
621                                 f->index, f->type, f->flags,
622                                 (f->pixelformat & 0xff),
623                                 (f->pixelformat >>  8) & 0xff,
624                                 (f->pixelformat >> 16) & 0xff,
625                                 (f->pixelformat >> 24) & 0xff,
626                                 f->description);
627                 else if (ret == -ENOTTY &&
628                          (ops->vidioc_enum_fmt_vid_cap ||
629                           ops->vidioc_enum_fmt_vid_out ||
630                           ops->vidioc_enum_fmt_vid_cap_mplane ||
631                           ops->vidioc_enum_fmt_vid_out_mplane ||
632                           ops->vidioc_enum_fmt_vid_overlay ||
633                           ops->vidioc_enum_fmt_type_private))
634                         ret = -EINVAL;
635                 break;
636         }
637         case VIDIOC_G_FMT:
638         {
639                 struct v4l2_format *f = (struct v4l2_format *)arg;
640
641                 /* FIXME: Should be one dump per type */
642                 dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
643
644                 switch (f->type) {
645                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
646                         if (ops->vidioc_g_fmt_vid_cap)
647                                 ret = ops->vidioc_g_fmt_vid_cap(file, fh, f);
648                         if (!ret)
649                                 v4l_print_pix_fmt(vfd, &f->fmt.pix);
650                         break;
651                 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
652                         if (ops->vidioc_g_fmt_vid_cap_mplane)
653                                 ret = ops->vidioc_g_fmt_vid_cap_mplane(file,
654                                                                         fh, f);
655                         if (!ret)
656                                 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
657                         break;
658                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
659                         if (likely(ops->vidioc_g_fmt_vid_overlay))
660                                 ret = ops->vidioc_g_fmt_vid_overlay(file,
661                                                                     fh, f);
662                         break;
663                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
664                         if (ops->vidioc_g_fmt_vid_out)
665                                 ret = ops->vidioc_g_fmt_vid_out(file, fh, f);
666                         if (!ret)
667                                 v4l_print_pix_fmt(vfd, &f->fmt.pix);
668                         break;
669                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
670                         if (ops->vidioc_g_fmt_vid_out_mplane)
671                                 ret = ops->vidioc_g_fmt_vid_out_mplane(file,
672                                                                         fh, f);
673                         if (!ret)
674                                 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
675                         break;
676                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
677                         if (likely(ops->vidioc_g_fmt_vid_out_overlay))
678                                 ret = ops->vidioc_g_fmt_vid_out_overlay(file,
679                                        fh, f);
680                         break;
681                 case V4L2_BUF_TYPE_VBI_CAPTURE:
682                         if (likely(ops->vidioc_g_fmt_vbi_cap))
683                                 ret = ops->vidioc_g_fmt_vbi_cap(file, fh, f);
684                         break;
685                 case V4L2_BUF_TYPE_VBI_OUTPUT:
686                         if (likely(ops->vidioc_g_fmt_vbi_out))
687                                 ret = ops->vidioc_g_fmt_vbi_out(file, fh, f);
688                         break;
689                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
690                         if (likely(ops->vidioc_g_fmt_sliced_vbi_cap))
691                                 ret = ops->vidioc_g_fmt_sliced_vbi_cap(file,
692                                                                         fh, f);
693                         break;
694                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
695                         if (likely(ops->vidioc_g_fmt_sliced_vbi_out))
696                                 ret = ops->vidioc_g_fmt_sliced_vbi_out(file,
697                                                                         fh, f);
698                         break;
699                 case V4L2_BUF_TYPE_PRIVATE:
700                         if (likely(ops->vidioc_g_fmt_type_private))
701                                 ret = ops->vidioc_g_fmt_type_private(file,
702                                                                 fh, f);
703                         break;
704                 }
705                 if (unlikely(ret == -ENOTTY && have_fmt_ops(g)))
706                         ret = -EINVAL;
707
708                 break;
709         }
710         case VIDIOC_S_FMT:
711         {
712                 struct v4l2_format *f = (struct v4l2_format *)arg;
713
714                 if (!have_fmt_ops(s))
715                         break;
716                 if (ret_prio) {
717                         ret = ret_prio;
718                         break;
719                 }
720                 ret = -EINVAL;
721
722                 /* FIXME: Should be one dump per type */
723                 dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
724
725                 switch (f->type) {
726                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
727                         CLEAR_AFTER_FIELD(f, fmt.pix);
728                         v4l_print_pix_fmt(vfd, &f->fmt.pix);
729                         if (ops->vidioc_s_fmt_vid_cap)
730                                 ret = ops->vidioc_s_fmt_vid_cap(file, fh, f);
731                         break;
732                 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
733                         CLEAR_AFTER_FIELD(f, fmt.pix_mp);
734                         v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
735                         if (ops->vidioc_s_fmt_vid_cap_mplane)
736                                 ret = ops->vidioc_s_fmt_vid_cap_mplane(file,
737                                                                         fh, f);
738                         break;
739                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
740                         CLEAR_AFTER_FIELD(f, fmt.win);
741                         if (ops->vidioc_s_fmt_vid_overlay)
742                                 ret = ops->vidioc_s_fmt_vid_overlay(file,
743                                                                     fh, f);
744                         break;
745                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
746                         CLEAR_AFTER_FIELD(f, fmt.pix);
747                         v4l_print_pix_fmt(vfd, &f->fmt.pix);
748                         if (ops->vidioc_s_fmt_vid_out)
749                                 ret = ops->vidioc_s_fmt_vid_out(file, fh, f);
750                         break;
751                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
752                         CLEAR_AFTER_FIELD(f, fmt.pix_mp);
753                         v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
754                         if (ops->vidioc_s_fmt_vid_out_mplane)
755                                 ret = ops->vidioc_s_fmt_vid_out_mplane(file,
756                                                                         fh, f);
757                         break;
758                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
759                         CLEAR_AFTER_FIELD(f, fmt.win);
760                         if (ops->vidioc_s_fmt_vid_out_overlay)
761                                 ret = ops->vidioc_s_fmt_vid_out_overlay(file,
762                                         fh, f);
763                         break;
764                 case V4L2_BUF_TYPE_VBI_CAPTURE:
765                         CLEAR_AFTER_FIELD(f, fmt.vbi);
766                         if (likely(ops->vidioc_s_fmt_vbi_cap))
767                                 ret = ops->vidioc_s_fmt_vbi_cap(file, fh, f);
768                         break;
769                 case V4L2_BUF_TYPE_VBI_OUTPUT:
770                         CLEAR_AFTER_FIELD(f, fmt.vbi);
771                         if (likely(ops->vidioc_s_fmt_vbi_out))
772                                 ret = ops->vidioc_s_fmt_vbi_out(file, fh, f);
773                         break;
774                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
775                         CLEAR_AFTER_FIELD(f, fmt.sliced);
776                         if (likely(ops->vidioc_s_fmt_sliced_vbi_cap))
777                                 ret = ops->vidioc_s_fmt_sliced_vbi_cap(file,
778                                                                         fh, f);
779                         break;
780                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
781                         CLEAR_AFTER_FIELD(f, fmt.sliced);
782                         if (likely(ops->vidioc_s_fmt_sliced_vbi_out))
783                                 ret = ops->vidioc_s_fmt_sliced_vbi_out(file,
784                                                                         fh, f);
785
786                         break;
787                 case V4L2_BUF_TYPE_PRIVATE:
788                         /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
789                         if (likely(ops->vidioc_s_fmt_type_private))
790                                 ret = ops->vidioc_s_fmt_type_private(file,
791                                                                 fh, f);
792                         break;
793                 }
794                 break;
795         }
796         case VIDIOC_TRY_FMT:
797         {
798                 struct v4l2_format *f = (struct v4l2_format *)arg;
799
800                 /* FIXME: Should be one dump per type */
801                 dbgarg(cmd, "type=%s\n", prt_names(f->type,
802                                                 v4l2_type_names));
803                 switch (f->type) {
804                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
805                         CLEAR_AFTER_FIELD(f, fmt.pix);
806                         if (ops->vidioc_try_fmt_vid_cap)
807                                 ret = ops->vidioc_try_fmt_vid_cap(file, fh, f);
808                         if (!ret)
809                                 v4l_print_pix_fmt(vfd, &f->fmt.pix);
810                         break;
811                 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
812                         CLEAR_AFTER_FIELD(f, fmt.pix_mp);
813                         if (ops->vidioc_try_fmt_vid_cap_mplane)
814                                 ret = ops->vidioc_try_fmt_vid_cap_mplane(file,
815                                                                          fh, f);
816                         if (!ret)
817                                 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
818                         break;
819                 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
820                         CLEAR_AFTER_FIELD(f, fmt.win);
821                         if (likely(ops->vidioc_try_fmt_vid_overlay))
822                                 ret = ops->vidioc_try_fmt_vid_overlay(file,
823                                         fh, f);
824                         break;
825                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
826                         CLEAR_AFTER_FIELD(f, fmt.pix);
827                         if (ops->vidioc_try_fmt_vid_out)
828                                 ret = ops->vidioc_try_fmt_vid_out(file, fh, f);
829                         if (!ret)
830                                 v4l_print_pix_fmt(vfd, &f->fmt.pix);
831                         break;
832                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
833                         CLEAR_AFTER_FIELD(f, fmt.pix_mp);
834                         if (ops->vidioc_try_fmt_vid_out_mplane)
835                                 ret = ops->vidioc_try_fmt_vid_out_mplane(file,
836                                                                          fh, f);
837                         if (!ret)
838                                 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
839                         break;
840                 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
841                         CLEAR_AFTER_FIELD(f, fmt.win);
842                         if (likely(ops->vidioc_try_fmt_vid_out_overlay))
843                                 ret = ops->vidioc_try_fmt_vid_out_overlay(file,
844                                        fh, f);
845                         break;
846                 case V4L2_BUF_TYPE_VBI_CAPTURE:
847                         CLEAR_AFTER_FIELD(f, fmt.vbi);
848                         if (likely(ops->vidioc_try_fmt_vbi_cap))
849                                 ret = ops->vidioc_try_fmt_vbi_cap(file, fh, f);
850                         break;
851                 case V4L2_BUF_TYPE_VBI_OUTPUT:
852                         CLEAR_AFTER_FIELD(f, fmt.vbi);
853                         if (likely(ops->vidioc_try_fmt_vbi_out))
854                                 ret = ops->vidioc_try_fmt_vbi_out(file, fh, f);
855                         break;
856                 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
857                         CLEAR_AFTER_FIELD(f, fmt.sliced);
858                         if (likely(ops->vidioc_try_fmt_sliced_vbi_cap))
859                                 ret = ops->vidioc_try_fmt_sliced_vbi_cap(file,
860                                                                 fh, f);
861                         break;
862                 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
863                         CLEAR_AFTER_FIELD(f, fmt.sliced);
864                         if (likely(ops->vidioc_try_fmt_sliced_vbi_out))
865                                 ret = ops->vidioc_try_fmt_sliced_vbi_out(file,
866                                                                 fh, f);
867                         break;
868                 case V4L2_BUF_TYPE_PRIVATE:
869                         /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
870                         if (likely(ops->vidioc_try_fmt_type_private))
871                                 ret = ops->vidioc_try_fmt_type_private(file,
872                                                                 fh, f);
873                         break;
874                 }
875                 if (unlikely(ret == -ENOTTY && have_fmt_ops(try)))
876                         ret = -EINVAL;
877                 break;
878         }
879         /* FIXME: Those buf reqs could be handled here,
880            with some changes on videobuf to allow its header to be included at
881            videodev2.h or being merged at videodev2.
882          */
883         case VIDIOC_REQBUFS:
884         {
885                 struct v4l2_requestbuffers *p = arg;
886
887                 if (!ops->vidioc_reqbufs)
888                         break;
889                 if (ret_prio) {
890                         ret = ret_prio;
891                         break;
892                 }
893                 ret = check_fmt(ops, p->type);
894                 if (ret)
895                         break;
896
897                 if (p->type < V4L2_BUF_TYPE_PRIVATE)
898                         CLEAR_AFTER_FIELD(p, memory);
899
900                 ret = ops->vidioc_reqbufs(file, fh, p);
901                 dbgarg(cmd, "count=%d, type=%s, memory=%s\n",
902                                 p->count,
903                                 prt_names(p->type, v4l2_type_names),
904                                 prt_names(p->memory, v4l2_memory_names));
905                 break;
906         }
907         case VIDIOC_QUERYBUF:
908         {
909                 struct v4l2_buffer *p = arg;
910
911                 if (!ops->vidioc_querybuf)
912                         break;
913                 ret = check_fmt(ops, p->type);
914                 if (ret)
915                         break;
916
917                 ret = ops->vidioc_querybuf(file, fh, p);
918                 if (!ret)
919                         dbgbuf(cmd, vfd, p);
920                 break;
921         }
922         case VIDIOC_QBUF:
923         {
924                 struct v4l2_buffer *p = arg;
925
926                 if (!ops->vidioc_qbuf)
927                         break;
928                 ret = check_fmt(ops, p->type);
929                 if (ret)
930                         break;
931
932                 ret = ops->vidioc_qbuf(file, fh, p);
933                 if (!ret)
934                         dbgbuf(cmd, vfd, p);
935                 break;
936         }
937         case VIDIOC_DQBUF:
938         {
939                 struct v4l2_buffer *p = arg;
940
941                 if (!ops->vidioc_dqbuf)
942                         break;
943                 ret = check_fmt(ops, p->type);
944                 if (ret)
945                         break;
946
947                 ret = ops->vidioc_dqbuf(file, fh, p);
948                 if (!ret)
949                         dbgbuf(cmd, vfd, p);
950                 break;
951         }
952         case VIDIOC_OVERLAY:
953         {
954                 int *i = arg;
955
956                 if (!ops->vidioc_overlay)
957                         break;
958                 if (ret_prio) {
959                         ret = ret_prio;
960                         break;
961                 }
962                 dbgarg(cmd, "value=%d\n", *i);
963                 ret = ops->vidioc_overlay(file, fh, *i);
964                 break;
965         }
966         case VIDIOC_G_FBUF:
967         {
968                 struct v4l2_framebuffer *p = arg;
969
970                 if (!ops->vidioc_g_fbuf)
971                         break;
972                 ret = ops->vidioc_g_fbuf(file, fh, arg);
973                 if (!ret) {
974                         dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
975                                         p->capability, p->flags,
976                                         (unsigned long)p->base);
977                         v4l_print_pix_fmt(vfd, &p->fmt);
978                 }
979                 break;
980         }
981         case VIDIOC_S_FBUF:
982         {
983                 struct v4l2_framebuffer *p = arg;
984
985                 if (!ops->vidioc_s_fbuf)
986                         break;
987                 if (ret_prio) {
988                         ret = ret_prio;
989                         break;
990                 }
991                 dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
992                         p->capability, p->flags, (unsigned long)p->base);
993                 v4l_print_pix_fmt(vfd, &p->fmt);
994                 ret = ops->vidioc_s_fbuf(file, fh, arg);
995                 break;
996         }
997         case VIDIOC_STREAMON:
998         {
999                 enum v4l2_buf_type i = *(int *)arg;
1000
1001                 if (!ops->vidioc_streamon)
1002                         break;
1003                 if (ret_prio) {
1004                         ret = ret_prio;
1005                         break;
1006                 }
1007                 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
1008                 ret = ops->vidioc_streamon(file, fh, i);
1009                 break;
1010         }
1011         case VIDIOC_STREAMOFF:
1012         {
1013                 enum v4l2_buf_type i = *(int *)arg;
1014
1015                 if (!ops->vidioc_streamoff)
1016                         break;
1017                 if (ret_prio) {
1018                         ret = ret_prio;
1019                         break;
1020                 }
1021                 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
1022                 ret = ops->vidioc_streamoff(file, fh, i);
1023                 break;
1024         }
1025         /* ---------- tv norms ---------- */
1026         case VIDIOC_ENUMSTD:
1027         {
1028                 struct v4l2_standard *p = arg;
1029                 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1030                 unsigned int index = p->index, i, j = 0;
1031                 const char *descr = "";
1032
1033                 if (id == 0)
1034                         break;
1035                 ret = -EINVAL;
1036
1037                 /* Return norm array in a canonical way */
1038                 for (i = 0; i <= index && id; i++) {
1039                         /* last std value in the standards array is 0, so this
1040                            while always ends there since (id & 0) == 0. */
1041                         while ((id & standards[j].std) != standards[j].std)
1042                                 j++;
1043                         curr_id = standards[j].std;
1044                         descr = standards[j].descr;
1045                         j++;
1046                         if (curr_id == 0)
1047                                 break;
1048                         if (curr_id != V4L2_STD_PAL &&
1049                             curr_id != V4L2_STD_SECAM &&
1050                             curr_id != V4L2_STD_NTSC)
1051                                 id &= ~curr_id;
1052                 }
1053                 if (i <= index)
1054                         break;
1055
1056                 v4l2_video_std_construct(p, curr_id, descr);
1057
1058                 dbgarg(cmd, "index=%d, id=0x%Lx, name=%s, fps=%d/%d, "
1059                                 "framelines=%d\n", p->index,
1060                                 (unsigned long long)p->id, p->name,
1061                                 p->frameperiod.numerator,
1062                                 p->frameperiod.denominator,
1063                                 p->framelines);
1064
1065                 ret = 0;
1066                 break;
1067         }
1068         case VIDIOC_G_STD:
1069         {
1070                 v4l2_std_id *id = arg;
1071
1072                 /* Calls the specific handler */
1073                 if (ops->vidioc_g_std)
1074                         ret = ops->vidioc_g_std(file, fh, id);
1075                 else if (vfd->current_norm) {
1076                         ret = 0;
1077                         *id = vfd->current_norm;
1078                 }
1079
1080                 if (likely(!ret))
1081                         dbgarg(cmd, "std=0x%08Lx\n", (long long unsigned)*id);
1082                 break;
1083         }
1084         case VIDIOC_S_STD:
1085         {
1086                 v4l2_std_id *id = arg, norm;
1087
1088                 dbgarg(cmd, "std=%08Lx\n", (long long unsigned)*id);
1089
1090                 if (!ops->vidioc_s_std)
1091                         break;
1092
1093                 if (ret_prio) {
1094                         ret = ret_prio;
1095                         break;
1096                 }
1097                 ret = -EINVAL;
1098                 norm = (*id) & vfd->tvnorms;
1099                 if (vfd->tvnorms && !norm)      /* Check if std is supported */
1100                         break;
1101
1102                 /* Calls the specific handler */
1103                 ret = ops->vidioc_s_std(file, fh, &norm);
1104
1105                 /* Updates standard information */
1106                 if (ret >= 0)
1107                         vfd->current_norm = norm;
1108                 break;
1109         }
1110         case VIDIOC_QUERYSTD:
1111         {
1112                 v4l2_std_id *p = arg;
1113
1114                 if (!ops->vidioc_querystd)
1115                         break;
1116                 /*
1117                  * If nothing detected, it should return all supported
1118                  * Drivers just need to mask the std argument, in order
1119                  * to remove the standards that don't apply from the mask.
1120                  * This means that tuners, audio and video decoders can join
1121                  * their efforts to improve the standards detection
1122                  */
1123                 *p = vfd->tvnorms;
1124                 ret = ops->vidioc_querystd(file, fh, arg);
1125                 if (!ret)
1126                         dbgarg(cmd, "detected std=%08Lx\n",
1127                                                 (unsigned long long)*p);
1128                 break;
1129         }
1130         /* ------ input switching ---------- */
1131         /* FIXME: Inputs can be handled inside videodev2 */
1132         case VIDIOC_ENUMINPUT:
1133         {
1134                 struct v4l2_input *p = arg;
1135
1136                 /*
1137                  * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
1138                  * CAP_STD here based on ioctl handler provided by the
1139                  * driver. If the driver doesn't support these
1140                  * for a specific input, it must override these flags.
1141                  */
1142                 if (ops->vidioc_s_std)
1143                         p->capabilities |= V4L2_IN_CAP_STD;
1144                 if (ops->vidioc_s_dv_preset)
1145                         p->capabilities |= V4L2_IN_CAP_PRESETS;
1146                 if (ops->vidioc_s_dv_timings)
1147                         p->capabilities |= V4L2_IN_CAP_CUSTOM_TIMINGS;
1148
1149                 if (!ops->vidioc_enum_input)
1150                         break;
1151
1152                 ret = ops->vidioc_enum_input(file, fh, p);
1153                 if (!ret)
1154                         dbgarg(cmd, "index=%d, name=%s, type=%d, "
1155                                 "audioset=%d, "
1156                                 "tuner=%d, std=%08Lx, status=%d\n",
1157                                 p->index, p->name, p->type, p->audioset,
1158                                 p->tuner,
1159                                 (unsigned long long)p->std,
1160                                 p->status);
1161                 break;
1162         }
1163         case VIDIOC_G_INPUT:
1164         {
1165                 unsigned int *i = arg;
1166
1167                 if (!ops->vidioc_g_input)
1168                         break;
1169                 ret = ops->vidioc_g_input(file, fh, i);
1170                 if (!ret)
1171                         dbgarg(cmd, "value=%d\n", *i);
1172                 break;
1173         }
1174         case VIDIOC_S_INPUT:
1175         {
1176                 unsigned int *i = arg;
1177
1178                 if (!ops->vidioc_s_input)
1179                         break;
1180                 if (ret_prio) {
1181                         ret = ret_prio;
1182                         break;
1183                 }
1184                 dbgarg(cmd, "value=%d\n", *i);
1185                 ret = ops->vidioc_s_input(file, fh, *i);
1186                 break;
1187         }
1188
1189         /* ------ output switching ---------- */
1190         case VIDIOC_ENUMOUTPUT:
1191         {
1192                 struct v4l2_output *p = arg;
1193
1194                 if (!ops->vidioc_enum_output)
1195                         break;
1196
1197                 /*
1198                  * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
1199                  * CAP_STD here based on ioctl handler provided by the
1200                  * driver. If the driver doesn't support these
1201                  * for a specific output, it must override these flags.
1202                  */
1203                 if (ops->vidioc_s_std)
1204                         p->capabilities |= V4L2_OUT_CAP_STD;
1205                 if (ops->vidioc_s_dv_preset)
1206                         p->capabilities |= V4L2_OUT_CAP_PRESETS;
1207                 if (ops->vidioc_s_dv_timings)
1208                         p->capabilities |= V4L2_OUT_CAP_CUSTOM_TIMINGS;
1209
1210                 ret = ops->vidioc_enum_output(file, fh, p);
1211                 if (!ret)
1212                         dbgarg(cmd, "index=%d, name=%s, type=%d, "
1213                                 "audioset=0x%x, "
1214                                 "modulator=%d, std=0x%08Lx\n",
1215                                 p->index, p->name, p->type, p->audioset,
1216                                 p->modulator, (unsigned long long)p->std);
1217                 break;
1218         }
1219         case VIDIOC_G_OUTPUT:
1220         {
1221                 unsigned int *i = arg;
1222
1223                 if (!ops->vidioc_g_output)
1224                         break;
1225                 ret = ops->vidioc_g_output(file, fh, i);
1226                 if (!ret)
1227                         dbgarg(cmd, "value=%d\n", *i);
1228                 break;
1229         }
1230         case VIDIOC_S_OUTPUT:
1231         {
1232                 unsigned int *i = arg;
1233
1234                 if (!ops->vidioc_s_output)
1235                         break;
1236                 if (ret_prio) {
1237                         ret = ret_prio;
1238                         break;
1239                 }
1240                 dbgarg(cmd, "value=%d\n", *i);
1241                 ret = ops->vidioc_s_output(file, fh, *i);
1242                 break;
1243         }
1244
1245         /* --- controls ---------------------------------------------- */
1246         case VIDIOC_QUERYCTRL:
1247         {
1248                 struct v4l2_queryctrl *p = arg;
1249
1250                 if (vfh && vfh->ctrl_handler)
1251                         ret = v4l2_queryctrl(vfh->ctrl_handler, p);
1252                 else if (vfd->ctrl_handler)
1253                         ret = v4l2_queryctrl(vfd->ctrl_handler, p);
1254                 else if (ops->vidioc_queryctrl)
1255                         ret = ops->vidioc_queryctrl(file, fh, p);
1256                 else
1257                         break;
1258                 if (!ret)
1259                         dbgarg(cmd, "id=0x%x, type=%d, name=%s, min/max=%d/%d, "
1260                                         "step=%d, default=%d, flags=0x%08x\n",
1261                                         p->id, p->type, p->name,
1262                                         p->minimum, p->maximum,
1263                                         p->step, p->default_value, p->flags);
1264                 else
1265                         dbgarg(cmd, "id=0x%x\n", p->id);
1266                 break;
1267         }
1268         case VIDIOC_G_CTRL:
1269         {
1270                 struct v4l2_control *p = arg;
1271
1272                 if (vfh && vfh->ctrl_handler)
1273                         ret = v4l2_g_ctrl(vfh->ctrl_handler, p);
1274                 else if (vfd->ctrl_handler)
1275                         ret = v4l2_g_ctrl(vfd->ctrl_handler, p);
1276                 else if (ops->vidioc_g_ctrl)
1277                         ret = ops->vidioc_g_ctrl(file, fh, p);
1278                 else if (ops->vidioc_g_ext_ctrls) {
1279                         struct v4l2_ext_controls ctrls;
1280                         struct v4l2_ext_control ctrl;
1281
1282                         ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1283                         ctrls.count = 1;
1284                         ctrls.controls = &ctrl;
1285                         ctrl.id = p->id;
1286                         ctrl.value = p->value;
1287                         if (check_ext_ctrls(&ctrls, 1)) {
1288                                 ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1289                                 if (ret == 0)
1290                                         p->value = ctrl.value;
1291                         }
1292                 } else
1293                         break;
1294                 if (!ret)
1295                         dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1296                 else
1297                         dbgarg(cmd, "id=0x%x\n", p->id);
1298                 break;
1299         }
1300         case VIDIOC_S_CTRL:
1301         {
1302                 struct v4l2_control *p = arg;
1303                 struct v4l2_ext_controls ctrls;
1304                 struct v4l2_ext_control ctrl;
1305
1306                 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
1307                         !ops->vidioc_s_ctrl && !ops->vidioc_s_ext_ctrls)
1308                         break;
1309                 if (ret_prio) {
1310                         ret = ret_prio;
1311                         break;
1312                 }
1313
1314                 dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1315
1316                 if (vfh && vfh->ctrl_handler) {
1317                         ret = v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1318                         break;
1319                 }
1320                 if (vfd->ctrl_handler) {
1321                         ret = v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1322                         break;
1323                 }
1324                 if (ops->vidioc_s_ctrl) {
1325                         ret = ops->vidioc_s_ctrl(file, fh, p);
1326                         break;
1327                 }
1328                 if (!ops->vidioc_s_ext_ctrls)
1329                         break;
1330
1331                 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1332                 ctrls.count = 1;
1333                 ctrls.controls = &ctrl;
1334                 ctrl.id = p->id;
1335                 ctrl.value = p->value;
1336                 if (check_ext_ctrls(&ctrls, 1))
1337                         ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1338                 else
1339                         ret = -EINVAL;
1340                 break;
1341         }
1342         case VIDIOC_G_EXT_CTRLS:
1343         {
1344                 struct v4l2_ext_controls *p = arg;
1345
1346                 p->error_idx = p->count;
1347                 if (vfh && vfh->ctrl_handler)
1348                         ret = v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1349                 else if (vfd->ctrl_handler)
1350                         ret = v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1351                 else if (ops->vidioc_g_ext_ctrls)
1352                         ret = check_ext_ctrls(p, 0) ?
1353                                 ops->vidioc_g_ext_ctrls(file, fh, p) :
1354                                 -EINVAL;
1355                 else
1356                         break;
1357                 v4l_print_ext_ctrls(cmd, vfd, p, !ret);
1358                 break;
1359         }
1360         case VIDIOC_S_EXT_CTRLS:
1361         {
1362                 struct v4l2_ext_controls *p = arg;
1363
1364                 p->error_idx = p->count;
1365                 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
1366                                 !ops->vidioc_s_ext_ctrls)
1367                         break;
1368                 if (ret_prio) {
1369                         ret = ret_prio;
1370                         break;
1371                 }
1372                 v4l_print_ext_ctrls(cmd, vfd, p, 1);
1373                 if (vfh && vfh->ctrl_handler)
1374                         ret = v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1375                 else if (vfd->ctrl_handler)
1376                         ret = v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1377                 else if (check_ext_ctrls(p, 0))
1378                         ret = ops->vidioc_s_ext_ctrls(file, fh, p);
1379                 else
1380                         ret = -EINVAL;
1381                 break;
1382         }
1383         case VIDIOC_TRY_EXT_CTRLS:
1384         {
1385                 struct v4l2_ext_controls *p = arg;
1386
1387                 p->error_idx = p->count;
1388                 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
1389                                 !ops->vidioc_try_ext_ctrls)
1390                         break;
1391                 v4l_print_ext_ctrls(cmd, vfd, p, 1);
1392                 if (vfh && vfh->ctrl_handler)
1393                         ret = v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1394                 else if (vfd->ctrl_handler)
1395                         ret = v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1396                 else if (check_ext_ctrls(p, 0))
1397                         ret = ops->vidioc_try_ext_ctrls(file, fh, p);
1398                 else
1399                         ret = -EINVAL;
1400                 break;
1401         }
1402         case VIDIOC_QUERYMENU:
1403         {
1404                 struct v4l2_querymenu *p = arg;
1405
1406                 if (vfh && vfh->ctrl_handler)
1407                         ret = v4l2_querymenu(vfh->ctrl_handler, p);
1408                 else if (vfd->ctrl_handler)
1409                         ret = v4l2_querymenu(vfd->ctrl_handler, p);
1410                 else if (ops->vidioc_querymenu)
1411                         ret = ops->vidioc_querymenu(file, fh, p);
1412                 else
1413                         break;
1414                 if (!ret)
1415                         dbgarg(cmd, "id=0x%x, index=%d, name=%s\n",
1416                                 p->id, p->index, p->name);
1417                 else
1418                         dbgarg(cmd, "id=0x%x, index=%d\n",
1419                                 p->id, p->index);
1420                 break;
1421         }
1422         /* --- audio ---------------------------------------------- */
1423         case VIDIOC_ENUMAUDIO:
1424         {
1425                 struct v4l2_audio *p = arg;
1426
1427                 if (!ops->vidioc_enumaudio)
1428                         break;
1429                 ret = ops->vidioc_enumaudio(file, fh, p);
1430                 if (!ret)
1431                         dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1432                                         "mode=0x%x\n", p->index, p->name,
1433                                         p->capability, p->mode);
1434                 else
1435                         dbgarg(cmd, "index=%d\n", p->index);
1436                 break;
1437         }
1438         case VIDIOC_G_AUDIO:
1439         {
1440                 struct v4l2_audio *p = arg;
1441
1442                 if (!ops->vidioc_g_audio)
1443                         break;
1444
1445                 ret = ops->vidioc_g_audio(file, fh, p);
1446                 if (!ret)
1447                         dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1448                                         "mode=0x%x\n", p->index,
1449                                         p->name, p->capability, p->mode);
1450                 else
1451                         dbgarg(cmd, "index=%d\n", p->index);
1452                 break;
1453         }
1454         case VIDIOC_S_AUDIO:
1455         {
1456                 struct v4l2_audio *p = arg;
1457
1458                 if (!ops->vidioc_s_audio)
1459                         break;
1460                 if (ret_prio) {
1461                         ret = ret_prio;
1462                         break;
1463                 }
1464                 dbgarg(cmd, "index=%d, name=%s, capability=0x%x, "
1465                                         "mode=0x%x\n", p->index, p->name,
1466                                         p->capability, p->mode);
1467                 ret = ops->vidioc_s_audio(file, fh, p);
1468                 break;
1469         }
1470         case VIDIOC_ENUMAUDOUT:
1471         {
1472                 struct v4l2_audioout *p = arg;
1473
1474                 if (!ops->vidioc_enumaudout)
1475                         break;
1476                 dbgarg(cmd, "Enum for index=%d\n", p->index);
1477                 ret = ops->vidioc_enumaudout(file, fh, p);
1478                 if (!ret)
1479                         dbgarg2("index=%d, name=%s, capability=%d, "
1480                                         "mode=%d\n", p->index, p->name,
1481                                         p->capability, p->mode);
1482                 break;
1483         }
1484         case VIDIOC_G_AUDOUT:
1485         {
1486                 struct v4l2_audioout *p = arg;
1487
1488                 if (!ops->vidioc_g_audout)
1489                         break;
1490
1491                 ret = ops->vidioc_g_audout(file, fh, p);
1492                 if (!ret)
1493                         dbgarg2("index=%d, name=%s, capability=%d, "
1494                                         "mode=%d\n", p->index, p->name,
1495                                         p->capability, p->mode);
1496                 break;
1497         }
1498         case VIDIOC_S_AUDOUT:
1499         {
1500                 struct v4l2_audioout *p = arg;
1501
1502                 if (!ops->vidioc_s_audout)
1503                         break;
1504                 if (ret_prio) {
1505                         ret = ret_prio;
1506                         break;
1507                 }
1508                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1509                                         "mode=%d\n", p->index, p->name,
1510                                         p->capability, p->mode);
1511
1512                 ret = ops->vidioc_s_audout(file, fh, p);
1513                 break;
1514         }
1515         case VIDIOC_G_MODULATOR:
1516         {
1517                 struct v4l2_modulator *p = arg;
1518
1519                 if (!ops->vidioc_g_modulator)
1520                         break;
1521                 ret = ops->vidioc_g_modulator(file, fh, p);
1522                 if (!ret)
1523                         dbgarg(cmd, "index=%d, name=%s, "
1524                                         "capability=%d, rangelow=%d,"
1525                                         " rangehigh=%d, txsubchans=%d\n",
1526                                         p->index, p->name, p->capability,
1527                                         p->rangelow, p->rangehigh,
1528                                         p->txsubchans);
1529                 break;
1530         }
1531         case VIDIOC_S_MODULATOR:
1532         {
1533                 struct v4l2_modulator *p = arg;
1534
1535                 if (!ops->vidioc_s_modulator)
1536                         break;
1537                 if (ret_prio) {
1538                         ret = ret_prio;
1539                         break;
1540                 }
1541                 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1542                                 "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1543                                 p->index, p->name, p->capability, p->rangelow,
1544                                 p->rangehigh, p->txsubchans);
1545                         ret = ops->vidioc_s_modulator(file, fh, p);
1546                 break;
1547         }
1548         case VIDIOC_G_CROP:
1549         {
1550                 struct v4l2_crop *p = arg;
1551
1552                 if (!ops->vidioc_g_crop)
1553                         break;
1554
1555                 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1556                 ret = ops->vidioc_g_crop(file, fh, p);
1557                 if (!ret)
1558                         dbgrect(vfd, "", &p->c);
1559                 break;
1560         }
1561         case VIDIOC_S_CROP:
1562         {
1563                 struct v4l2_crop *p = arg;
1564
1565                 if (!ops->vidioc_s_crop)
1566                         break;
1567                 if (ret_prio) {
1568                         ret = ret_prio;
1569                         break;
1570                 }
1571                 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1572                 dbgrect(vfd, "", &p->c);
1573                 ret = ops->vidioc_s_crop(file, fh, p);
1574                 break;
1575         }
1576         case VIDIOC_G_SELECTION:
1577         {
1578                 struct v4l2_selection *p = arg;
1579
1580                 if (!ops->vidioc_g_selection)
1581                         break;
1582
1583                 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1584
1585                 ret = ops->vidioc_g_selection(file, fh, p);
1586                 if (!ret)
1587                         dbgrect(vfd, "", &p->r);
1588                 break;
1589         }
1590         case VIDIOC_S_SELECTION:
1591         {
1592                 struct v4l2_selection *p = arg;
1593
1594                 if (!ops->vidioc_s_selection)
1595                         break;
1596
1597                 if (ret_prio) {
1598                         ret = ret_prio;
1599                         break;
1600                 }
1601
1602                 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1603                 dbgrect(vfd, "", &p->r);
1604
1605                 ret = ops->vidioc_s_selection(file, fh, p);
1606                 break;
1607         }
1608         case VIDIOC_CROPCAP:
1609         {
1610                 struct v4l2_cropcap *p = arg;
1611
1612                 /*FIXME: Should also show v4l2_fract pixelaspect */
1613                 if (!ops->vidioc_cropcap)
1614                         break;
1615
1616                 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1617                 ret = ops->vidioc_cropcap(file, fh, p);
1618                 if (!ret) {
1619                         dbgrect(vfd, "bounds ", &p->bounds);
1620                         dbgrect(vfd, "defrect ", &p->defrect);
1621                 }
1622                 break;
1623         }
1624         case VIDIOC_G_JPEGCOMP:
1625         {
1626                 struct v4l2_jpegcompression *p = arg;
1627
1628                 if (!ops->vidioc_g_jpegcomp)
1629                         break;
1630
1631                 ret = ops->vidioc_g_jpegcomp(file, fh, p);
1632                 if (!ret)
1633                         dbgarg(cmd, "quality=%d, APPn=%d, "
1634                                         "APP_len=%d, COM_len=%d, "
1635                                         "jpeg_markers=%d\n",
1636                                         p->quality, p->APPn, p->APP_len,
1637                                         p->COM_len, p->jpeg_markers);
1638                 break;
1639         }
1640         case VIDIOC_S_JPEGCOMP:
1641         {
1642                 struct v4l2_jpegcompression *p = arg;
1643
1644                 if (!ops->vidioc_g_jpegcomp)
1645                         break;
1646                 if (ret_prio) {
1647                         ret = ret_prio;
1648                         break;
1649                 }
1650                 dbgarg(cmd, "quality=%d, APPn=%d, APP_len=%d, "
1651                                         "COM_len=%d, jpeg_markers=%d\n",
1652                                         p->quality, p->APPn, p->APP_len,
1653                                         p->COM_len, p->jpeg_markers);
1654                 ret = ops->vidioc_s_jpegcomp(file, fh, p);
1655                 break;
1656         }
1657         case VIDIOC_G_ENC_INDEX:
1658         {
1659                 struct v4l2_enc_idx *p = arg;
1660
1661                 if (!ops->vidioc_g_enc_index)
1662                         break;
1663                 ret = ops->vidioc_g_enc_index(file, fh, p);
1664                 if (!ret)
1665                         dbgarg(cmd, "entries=%d, entries_cap=%d\n",
1666                                         p->entries, p->entries_cap);
1667                 break;
1668         }
1669         case VIDIOC_ENCODER_CMD:
1670         {
1671                 struct v4l2_encoder_cmd *p = arg;
1672
1673                 if (!ops->vidioc_encoder_cmd)
1674                         break;
1675                 if (ret_prio) {
1676                         ret = ret_prio;
1677                         break;
1678                 }
1679                 ret = ops->vidioc_encoder_cmd(file, fh, p);
1680                 if (!ret)
1681                         dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1682                 break;
1683         }
1684         case VIDIOC_TRY_ENCODER_CMD:
1685         {
1686                 struct v4l2_encoder_cmd *p = arg;
1687
1688                 if (!ops->vidioc_try_encoder_cmd)
1689                         break;
1690                 ret = ops->vidioc_try_encoder_cmd(file, fh, p);
1691                 if (!ret)
1692                         dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1693                 break;
1694         }
1695         case VIDIOC_G_PARM:
1696         {
1697                 struct v4l2_streamparm *p = arg;
1698
1699                 if (!ops->vidioc_g_parm && !vfd->current_norm)
1700                         break;
1701                 if (ops->vidioc_g_parm) {
1702                         ret = check_fmt(ops, p->type);
1703                         if (ret)
1704                                 break;
1705                         ret = ops->vidioc_g_parm(file, fh, p);
1706                 } else {
1707                         v4l2_std_id std = vfd->current_norm;
1708
1709                         ret = -EINVAL;
1710                         if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1711                                 break;
1712
1713                         ret = 0;
1714                         if (ops->vidioc_g_std)
1715                                 ret = ops->vidioc_g_std(file, fh, &std);
1716                         if (ret == 0)
1717                                 v4l2_video_std_frame_period(std,
1718                                                     &p->parm.capture.timeperframe);
1719                 }
1720
1721                 dbgarg(cmd, "type=%d\n", p->type);
1722                 break;
1723         }
1724         case VIDIOC_S_PARM:
1725         {
1726                 struct v4l2_streamparm *p = arg;
1727
1728                 if (!ops->vidioc_s_parm)
1729                         break;
1730                 if (ret_prio) {
1731                         ret = ret_prio;
1732                         break;
1733                 }
1734                 ret = check_fmt(ops, p->type);
1735                 if (ret)
1736                         break;
1737
1738                 dbgarg(cmd, "type=%d\n", p->type);
1739                 ret = ops->vidioc_s_parm(file, fh, p);
1740                 break;
1741         }
1742         case VIDIOC_G_TUNER:
1743         {
1744                 struct v4l2_tuner *p = arg;
1745
1746                 if (!ops->vidioc_g_tuner)
1747                         break;
1748
1749                 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1750                         V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1751                 ret = ops->vidioc_g_tuner(file, fh, p);
1752                 if (!ret)
1753                         dbgarg(cmd, "index=%d, name=%s, type=%d, "
1754                                         "capability=0x%x, rangelow=%d, "
1755                                         "rangehigh=%d, signal=%d, afc=%d, "
1756                                         "rxsubchans=0x%x, audmode=%d\n",
1757                                         p->index, p->name, p->type,
1758                                         p->capability, p->rangelow,
1759                                         p->rangehigh, p->signal, p->afc,
1760                                         p->rxsubchans, p->audmode);
1761                 break;
1762         }
1763         case VIDIOC_S_TUNER:
1764         {
1765                 struct v4l2_tuner *p = arg;
1766
1767                 if (!ops->vidioc_s_tuner)
1768                         break;
1769                 if (ret_prio) {
1770                         ret = ret_prio;
1771                         break;
1772                 }
1773                 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1774                         V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1775                 dbgarg(cmd, "index=%d, name=%s, type=%d, "
1776                                 "capability=0x%x, rangelow=%d, "
1777                                 "rangehigh=%d, signal=%d, afc=%d, "
1778                                 "rxsubchans=0x%x, audmode=%d\n",
1779                                 p->index, p->name, p->type,
1780                                 p->capability, p->rangelow,
1781                                 p->rangehigh, p->signal, p->afc,
1782                                 p->rxsubchans, p->audmode);
1783                 ret = ops->vidioc_s_tuner(file, fh, p);
1784                 break;
1785         }
1786         case VIDIOC_G_FREQUENCY:
1787         {
1788                 struct v4l2_frequency *p = arg;
1789
1790                 if (!ops->vidioc_g_frequency)
1791                         break;
1792
1793                 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1794                         V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1795                 ret = ops->vidioc_g_frequency(file, fh, p);
1796                 if (!ret)
1797                         dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1798                                         p->tuner, p->type, p->frequency);
1799                 break;
1800         }
1801         case VIDIOC_S_FREQUENCY:
1802         {
1803                 struct v4l2_frequency *p = arg;
1804
1805                 if (!ops->vidioc_s_frequency)
1806                         break;
1807                 if (ret_prio) {
1808                         ret = ret_prio;
1809                         break;
1810                 }
1811                 dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1812                                 p->tuner, p->type, p->frequency);
1813                 ret = ops->vidioc_s_frequency(file, fh, p);
1814                 break;
1815         }
1816         case VIDIOC_G_SLICED_VBI_CAP:
1817         {
1818                 struct v4l2_sliced_vbi_cap *p = arg;
1819
1820                 if (!ops->vidioc_g_sliced_vbi_cap)
1821                         break;
1822
1823                 /* Clear up to type, everything after type is zerod already */
1824                 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1825
1826                 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1827                 ret = ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1828                 if (!ret)
1829                         dbgarg2("service_set=%d\n", p->service_set);
1830                 break;
1831         }
1832         case VIDIOC_LOG_STATUS:
1833         {
1834                 if (!ops->vidioc_log_status)
1835                         break;
1836                 ret = ops->vidioc_log_status(file, fh);
1837                 break;
1838         }
1839 #ifdef CONFIG_VIDEO_ADV_DEBUG
1840         case VIDIOC_DBG_G_REGISTER:
1841         {
1842                 struct v4l2_dbg_register *p = arg;
1843
1844                 if (ops->vidioc_g_register) {
1845                         if (!capable(CAP_SYS_ADMIN))
1846                                 ret = -EPERM;
1847                         else
1848                                 ret = ops->vidioc_g_register(file, fh, p);
1849                 }
1850                 break;
1851         }
1852         case VIDIOC_DBG_S_REGISTER:
1853         {
1854                 struct v4l2_dbg_register *p = arg;
1855
1856                 if (ops->vidioc_s_register) {
1857                         if (!capable(CAP_SYS_ADMIN))
1858                                 ret = -EPERM;
1859                         else
1860                                 ret = ops->vidioc_s_register(file, fh, p);
1861                 }
1862                 break;
1863         }
1864 #endif
1865         case VIDIOC_DBG_G_CHIP_IDENT:
1866         {
1867                 struct v4l2_dbg_chip_ident *p = arg;
1868
1869                 if (!ops->vidioc_g_chip_ident)
1870                         break;
1871                 p->ident = V4L2_IDENT_NONE;
1872                 p->revision = 0;
1873                 ret = ops->vidioc_g_chip_ident(file, fh, p);
1874                 if (!ret)
1875                         dbgarg(cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
1876                 break;
1877         }
1878         case VIDIOC_S_HW_FREQ_SEEK:
1879         {
1880                 struct v4l2_hw_freq_seek *p = arg;
1881                 enum v4l2_tuner_type type;
1882
1883                 if (!ops->vidioc_s_hw_freq_seek)
1884                         break;
1885                 if (ret_prio) {
1886                         ret = ret_prio;
1887                         break;
1888                 }
1889                 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1890                         V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1891                 dbgarg(cmd,
1892                         "tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u\n",
1893                         p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing);
1894                 if (p->type != type)
1895                         ret = -EINVAL;
1896                 else
1897                         ret = ops->vidioc_s_hw_freq_seek(file, fh, p);
1898                 break;
1899         }
1900         case VIDIOC_ENUM_FRAMESIZES:
1901         {
1902                 struct v4l2_frmsizeenum *p = arg;
1903
1904                 if (!ops->vidioc_enum_framesizes)
1905                         break;
1906
1907                 ret = ops->vidioc_enum_framesizes(file, fh, p);
1908                 dbgarg(cmd,
1909                         "index=%d, pixelformat=%c%c%c%c, type=%d ",
1910                         p->index,
1911                         (p->pixel_format & 0xff),
1912                         (p->pixel_format >>  8) & 0xff,
1913                         (p->pixel_format >> 16) & 0xff,
1914                         (p->pixel_format >> 24) & 0xff,
1915                         p->type);
1916                 switch (p->type) {
1917                 case V4L2_FRMSIZE_TYPE_DISCRETE:
1918                         dbgarg3("width = %d, height=%d\n",
1919                                 p->discrete.width, p->discrete.height);
1920                         break;
1921                 case V4L2_FRMSIZE_TYPE_STEPWISE:
1922                         dbgarg3("min %dx%d, max %dx%d, step %dx%d\n",
1923                                 p->stepwise.min_width,  p->stepwise.min_height,
1924                                 p->stepwise.step_width, p->stepwise.step_height,
1925                                 p->stepwise.max_width,  p->stepwise.max_height);
1926                         break;
1927                 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
1928                         dbgarg3("continuous\n");
1929                         break;
1930                 default:
1931                         dbgarg3("- Unknown type!\n");
1932                 }
1933
1934                 break;
1935         }
1936         case VIDIOC_ENUM_FRAMEINTERVALS:
1937         {
1938                 struct v4l2_frmivalenum *p = arg;
1939
1940                 if (!ops->vidioc_enum_frameintervals)
1941                         break;
1942
1943                 ret = ops->vidioc_enum_frameintervals(file, fh, p);
1944                 dbgarg(cmd,
1945                         "index=%d, pixelformat=%d, width=%d, height=%d, type=%d ",
1946                         p->index, p->pixel_format,
1947                         p->width, p->height, p->type);
1948                 switch (p->type) {
1949                 case V4L2_FRMIVAL_TYPE_DISCRETE:
1950                         dbgarg2("fps=%d/%d\n",
1951                                 p->discrete.numerator,
1952                                 p->discrete.denominator);
1953                         break;
1954                 case V4L2_FRMIVAL_TYPE_STEPWISE:
1955                         dbgarg2("min=%d/%d, max=%d/%d, step=%d/%d\n",
1956                                 p->stepwise.min.numerator,
1957                                 p->stepwise.min.denominator,
1958                                 p->stepwise.max.numerator,
1959                                 p->stepwise.max.denominator,
1960                                 p->stepwise.step.numerator,
1961                                 p->stepwise.step.denominator);
1962                         break;
1963                 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1964                         dbgarg2("continuous\n");
1965                         break;
1966                 default:
1967                         dbgarg2("- Unknown type!\n");
1968                 }
1969                 break;
1970         }
1971         case VIDIOC_ENUM_DV_PRESETS:
1972         {
1973                 struct v4l2_dv_enum_preset *p = arg;
1974
1975                 if (!ops->vidioc_enum_dv_presets)
1976                         break;
1977
1978                 ret = ops->vidioc_enum_dv_presets(file, fh, p);
1979                 if (!ret)
1980                         dbgarg(cmd,
1981                                 "index=%d, preset=%d, name=%s, width=%d,"
1982                                 " height=%d ",
1983                                 p->index, p->preset, p->name, p->width,
1984                                 p->height);
1985                 break;
1986         }
1987         case VIDIOC_S_DV_PRESET:
1988         {
1989                 struct v4l2_dv_preset *p = arg;
1990
1991                 if (!ops->vidioc_s_dv_preset)
1992                         break;
1993                 if (ret_prio) {
1994                         ret = ret_prio;
1995                         break;
1996                 }
1997
1998                 dbgarg(cmd, "preset=%d\n", p->preset);
1999                 ret = ops->vidioc_s_dv_preset(file, fh, p);
2000                 break;
2001         }
2002         case VIDIOC_G_DV_PRESET:
2003         {
2004                 struct v4l2_dv_preset *p = arg;
2005
2006                 if (!ops->vidioc_g_dv_preset)
2007                         break;
2008
2009                 ret = ops->vidioc_g_dv_preset(file, fh, p);
2010                 if (!ret)
2011                         dbgarg(cmd, "preset=%d\n", p->preset);
2012                 break;
2013         }
2014         case VIDIOC_QUERY_DV_PRESET:
2015         {
2016                 struct v4l2_dv_preset *p = arg;
2017
2018                 if (!ops->vidioc_query_dv_preset)
2019                         break;
2020
2021                 ret = ops->vidioc_query_dv_preset(file, fh, p);
2022                 if (!ret)
2023                         dbgarg(cmd, "preset=%d\n", p->preset);
2024                 break;
2025         }
2026         case VIDIOC_S_DV_TIMINGS:
2027         {
2028                 struct v4l2_dv_timings *p = arg;
2029
2030                 if (!ops->vidioc_s_dv_timings)
2031                         break;
2032                 if (ret_prio) {
2033                         ret = ret_prio;
2034                         break;
2035                 }
2036
2037                 switch (p->type) {
2038                 case V4L2_DV_BT_656_1120:
2039                         dbgarg2("bt-656/1120:interlaced=%d, pixelclock=%lld,"
2040                                 " width=%d, height=%d, polarities=%x,"
2041                                 " hfrontporch=%d, hsync=%d, hbackporch=%d,"
2042                                 " vfrontporch=%d, vsync=%d, vbackporch=%d,"
2043                                 " il_vfrontporch=%d, il_vsync=%d,"
2044                                 " il_vbackporch=%d\n",
2045                                 p->bt.interlaced, p->bt.pixelclock,
2046                                 p->bt.width, p->bt.height, p->bt.polarities,
2047                                 p->bt.hfrontporch, p->bt.hsync,
2048                                 p->bt.hbackporch, p->bt.vfrontporch,
2049                                 p->bt.vsync, p->bt.vbackporch,
2050                                 p->bt.il_vfrontporch, p->bt.il_vsync,
2051                                 p->bt.il_vbackporch);
2052                         ret = ops->vidioc_s_dv_timings(file, fh, p);
2053                         break;
2054                 default:
2055                         dbgarg2("Unknown type %d!\n", p->type);
2056                         break;
2057                 }
2058                 break;
2059         }
2060         case VIDIOC_G_DV_TIMINGS:
2061         {
2062                 struct v4l2_dv_timings *p = arg;
2063
2064                 if (!ops->vidioc_g_dv_timings)
2065                         break;
2066
2067                 ret = ops->vidioc_g_dv_timings(file, fh, p);
2068                 if (!ret) {
2069                         switch (p->type) {
2070                         case V4L2_DV_BT_656_1120:
2071                                 dbgarg2("bt-656/1120:interlaced=%d,"
2072                                         " pixelclock=%lld,"
2073                                         " width=%d, height=%d, polarities=%x,"
2074                                         " hfrontporch=%d, hsync=%d,"
2075                                         " hbackporch=%d, vfrontporch=%d,"
2076                                         " vsync=%d, vbackporch=%d,"
2077                                         " il_vfrontporch=%d, il_vsync=%d,"
2078                                         " il_vbackporch=%d\n",
2079                                         p->bt.interlaced, p->bt.pixelclock,
2080                                         p->bt.width, p->bt.height,
2081                                         p->bt.polarities, p->bt.hfrontporch,
2082                                         p->bt.hsync, p->bt.hbackporch,
2083                                         p->bt.vfrontporch, p->bt.vsync,
2084                                         p->bt.vbackporch, p->bt.il_vfrontporch,
2085                                         p->bt.il_vsync, p->bt.il_vbackporch);
2086                                 break;
2087                         default:
2088                                 dbgarg2("Unknown type %d!\n", p->type);
2089                                 break;
2090                         }
2091                 }
2092                 break;
2093         }
2094         case VIDIOC_DQEVENT:
2095         {
2096                 struct v4l2_event *ev = arg;
2097
2098                 if (!ops->vidioc_subscribe_event)
2099                         break;
2100
2101                 ret = v4l2_event_dequeue(fh, ev, file->f_flags & O_NONBLOCK);
2102                 if (ret < 0) {
2103                         dbgarg(cmd, "no pending events?");
2104                         break;
2105                 }
2106                 dbgarg(cmd,
2107                        "pending=%d, type=0x%8.8x, sequence=%d, "
2108                        "timestamp=%lu.%9.9lu ",
2109                        ev->pending, ev->type, ev->sequence,
2110                        ev->timestamp.tv_sec, ev->timestamp.tv_nsec);
2111                 break;
2112         }
2113         case VIDIOC_SUBSCRIBE_EVENT:
2114         {
2115                 struct v4l2_event_subscription *sub = arg;
2116
2117                 if (!ops->vidioc_subscribe_event)
2118                         break;
2119
2120                 ret = ops->vidioc_subscribe_event(fh, sub);
2121                 if (ret < 0) {
2122                         dbgarg(cmd, "failed, ret=%ld", ret);
2123                         break;
2124                 }
2125                 dbgarg(cmd, "type=0x%8.8x", sub->type);
2126                 break;
2127         }
2128         case VIDIOC_UNSUBSCRIBE_EVENT:
2129         {
2130                 struct v4l2_event_subscription *sub = arg;
2131
2132                 if (!ops->vidioc_unsubscribe_event)
2133                         break;
2134
2135                 ret = ops->vidioc_unsubscribe_event(fh, sub);
2136                 if (ret < 0) {
2137                         dbgarg(cmd, "failed, ret=%ld", ret);
2138                         break;
2139                 }
2140                 dbgarg(cmd, "type=0x%8.8x", sub->type);
2141                 break;
2142         }
2143         case VIDIOC_CREATE_BUFS:
2144         {
2145                 struct v4l2_create_buffers *create = arg;
2146
2147                 if (!ops->vidioc_create_bufs)
2148                         break;
2149                 if (ret_prio) {
2150                         ret = ret_prio;
2151                         break;
2152                 }
2153                 ret = check_fmt(ops, create->format.type);
2154                 if (ret)
2155                         break;
2156
2157                 ret = ops->vidioc_create_bufs(file, fh, create);
2158
2159                 dbgarg(cmd, "count=%d @ %d\n", create->count, create->index);
2160                 break;
2161         }
2162         case VIDIOC_PREPARE_BUF:
2163         {
2164                 struct v4l2_buffer *b = arg;
2165
2166                 if (!ops->vidioc_prepare_buf)
2167                         break;
2168                 ret = check_fmt(ops, b->type);
2169                 if (ret)
2170                         break;
2171
2172                 ret = ops->vidioc_prepare_buf(file, fh, b);
2173
2174                 dbgarg(cmd, "index=%d", b->index);
2175                 break;
2176         }
2177         default:
2178                 if (!ops->vidioc_default)
2179                         break;
2180                 ret = ops->vidioc_default(file, fh, ret_prio >= 0, cmd, arg);
2181                 break;
2182         } /* switch */
2183
2184         if (vfd->debug & V4L2_DEBUG_IOCTL_ARG) {
2185                 if (ret < 0) {
2186                         v4l_print_ioctl(vfd->name, cmd);
2187                         printk(KERN_CONT " error %ld\n", ret);
2188                 }
2189         }
2190
2191         return ret;
2192 }
2193
2194 /* In some cases, only a few fields are used as input, i.e. when the app sets
2195  * "index" and then the driver fills in the rest of the structure for the thing
2196  * with that index.  We only need to copy up the first non-input field.  */
2197 static unsigned long cmd_input_size(unsigned int cmd)
2198 {
2199         /* Size of structure up to and including 'field' */
2200 #define CMDINSIZE(cmd, type, field)                             \
2201         case VIDIOC_##cmd:                                      \
2202                 return offsetof(struct v4l2_##type, field) +    \
2203                         sizeof(((struct v4l2_##type *)0)->field);
2204
2205         switch (cmd) {
2206                 CMDINSIZE(ENUM_FMT,             fmtdesc,        type);
2207                 CMDINSIZE(G_FMT,                format,         type);
2208                 CMDINSIZE(QUERYBUF,             buffer,         length);
2209                 CMDINSIZE(G_PARM,               streamparm,     type);
2210                 CMDINSIZE(ENUMSTD,              standard,       index);
2211                 CMDINSIZE(ENUMINPUT,            input,          index);
2212                 CMDINSIZE(G_CTRL,               control,        id);
2213                 CMDINSIZE(G_TUNER,              tuner,          index);
2214                 CMDINSIZE(QUERYCTRL,            queryctrl,      id);
2215                 CMDINSIZE(QUERYMENU,            querymenu,      index);
2216                 CMDINSIZE(ENUMOUTPUT,           output,         index);
2217                 CMDINSIZE(G_MODULATOR,          modulator,      index);
2218                 CMDINSIZE(G_FREQUENCY,          frequency,      tuner);
2219                 CMDINSIZE(CROPCAP,              cropcap,        type);
2220                 CMDINSIZE(G_CROP,               crop,           type);
2221                 CMDINSIZE(ENUMAUDIO,            audio,          index);
2222                 CMDINSIZE(ENUMAUDOUT,           audioout,       index);
2223                 CMDINSIZE(ENCODER_CMD,          encoder_cmd,    flags);
2224                 CMDINSIZE(TRY_ENCODER_CMD,      encoder_cmd,    flags);
2225                 CMDINSIZE(G_SLICED_VBI_CAP,     sliced_vbi_cap, type);
2226                 CMDINSIZE(ENUM_FRAMESIZES,      frmsizeenum,    pixel_format);
2227                 CMDINSIZE(ENUM_FRAMEINTERVALS,  frmivalenum,    height);
2228         default:
2229                 return _IOC_SIZE(cmd);
2230         }
2231 }
2232
2233 static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2234                             void * __user *user_ptr, void ***kernel_ptr)
2235 {
2236         int ret = 0;
2237
2238         switch (cmd) {
2239         case VIDIOC_QUERYBUF:
2240         case VIDIOC_QBUF:
2241         case VIDIOC_DQBUF: {
2242                 struct v4l2_buffer *buf = parg;
2243
2244                 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2245                         if (buf->length > VIDEO_MAX_PLANES) {
2246                                 ret = -EINVAL;
2247                                 break;
2248                         }
2249                         *user_ptr = (void __user *)buf->m.planes;
2250                         *kernel_ptr = (void *)&buf->m.planes;
2251                         *array_size = sizeof(struct v4l2_plane) * buf->length;
2252                         ret = 1;
2253                 }
2254                 break;
2255         }
2256
2257         case VIDIOC_S_EXT_CTRLS:
2258         case VIDIOC_G_EXT_CTRLS:
2259         case VIDIOC_TRY_EXT_CTRLS: {
2260                 struct v4l2_ext_controls *ctrls = parg;
2261
2262                 if (ctrls->count != 0) {
2263                         if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2264                                 ret = -EINVAL;
2265                                 break;
2266                         }
2267                         *user_ptr = (void __user *)ctrls->controls;
2268                         *kernel_ptr = (void *)&ctrls->controls;
2269                         *array_size = sizeof(struct v4l2_ext_control)
2270                                     * ctrls->count;
2271                         ret = 1;
2272                 }
2273                 break;
2274         }
2275         }
2276
2277         return ret;
2278 }
2279
2280 long
2281 video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2282                v4l2_kioctl func)
2283 {
2284         char    sbuf[128];
2285         void    *mbuf = NULL;
2286         void    *parg = (void *)arg;
2287         long    err  = -EINVAL;
2288         bool    has_array_args;
2289         size_t  array_size = 0;
2290         void __user *user_ptr = NULL;
2291         void    **kernel_ptr = NULL;
2292
2293         /*  Copy arguments into temp kernel buffer  */
2294         if (_IOC_DIR(cmd) != _IOC_NONE) {
2295                 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2296                         parg = sbuf;
2297                 } else {
2298                         /* too big to allocate from stack */
2299                         mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2300                         if (NULL == mbuf)
2301                                 return -ENOMEM;
2302                         parg = mbuf;
2303                 }
2304
2305                 err = -EFAULT;
2306                 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2307                         unsigned long n = cmd_input_size(cmd);
2308
2309                         if (copy_from_user(parg, (void __user *)arg, n))
2310                                 goto out;
2311
2312                         /* zero out anything we don't copy from userspace */
2313                         if (n < _IOC_SIZE(cmd))
2314                                 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
2315                 } else {
2316                         /* read-only ioctl */
2317                         memset(parg, 0, _IOC_SIZE(cmd));
2318                 }
2319         }
2320
2321         err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2322         if (err < 0)
2323                 goto out;
2324         has_array_args = err;
2325
2326         if (has_array_args) {
2327                 /*
2328                  * When adding new types of array args, make sure that the
2329                  * parent argument to ioctl (which contains the pointer to the
2330                  * array) fits into sbuf (so that mbuf will still remain
2331                  * unused up to here).
2332                  */
2333                 mbuf = kmalloc(array_size, GFP_KERNEL);
2334                 err = -ENOMEM;
2335                 if (NULL == mbuf)
2336                         goto out_array_args;
2337                 err = -EFAULT;
2338                 if (copy_from_user(mbuf, user_ptr, array_size))
2339                         goto out_array_args;
2340                 *kernel_ptr = mbuf;
2341         }
2342
2343         /* Handles IOCTL */
2344         err = func(file, cmd, parg);
2345         if (err == -ENOIOCTLCMD)
2346                 err = -EINVAL;
2347
2348         if (has_array_args) {
2349                 *kernel_ptr = user_ptr;
2350                 if (copy_to_user(user_ptr, mbuf, array_size))
2351                         err = -EFAULT;
2352                 goto out_array_args;
2353         }
2354         if (err < 0)
2355                 goto out;
2356
2357 out_array_args:
2358         /*  Copy results into user buffer  */
2359         switch (_IOC_DIR(cmd)) {
2360         case _IOC_READ:
2361         case (_IOC_WRITE | _IOC_READ):
2362                 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2363                         err = -EFAULT;
2364                 break;
2365         }
2366
2367 out:
2368         kfree(mbuf);
2369         return err;
2370 }
2371 EXPORT_SYMBOL(video_usercopy);
2372
2373 long video_ioctl2(struct file *file,
2374                unsigned int cmd, unsigned long arg)
2375 {
2376         return video_usercopy(file, cmd, arg, __video_do_ioctl);
2377 }
2378 EXPORT_SYMBOL(video_ioctl2);