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