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