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