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