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