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