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