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