blob: 4029d12f984d49055f136563e409daf431a0e0b4 [file] [log] [blame]
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001/*
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 *
Alan Coxd9b01442008-10-27 15:13:47 -030011 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
Hans Verkuil35ea11f2008-07-20 08:12:02 -030012 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
13 */
14
15#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030017#include <linux/types.h>
18#include <linux/kernel.h>
Mauro Carvalho Chehabae6db512011-06-24 13:14:14 -030019#include <linux/version.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030020
Hans Verkuil35ea11f2008-07-20 08:12:02 -030021#include <linux/videodev2.h>
22
Hans Verkuil35ea11f2008-07-20 08:12:02 -030023#include <media/v4l2-common.h>
24#include <media/v4l2-ioctl.h>
Hans Verkuil11bbc1c2010-05-16 09:24:06 -030025#include <media/v4l2-ctrls.h>
Sakari Ailusd3d7c962010-03-27 11:02:10 -030026#include <media/v4l2-fh.h>
27#include <media/v4l2-event.h>
Hans Verkuil99cd47bc2011-03-11 19:00:56 -030028#include <media/v4l2-device.h>
Hans Verkuil80b36e02009-02-07 11:00:02 -030029#include <media/v4l2-chip-ident.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030030
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
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -030046#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
Lucas De Marchi25985ed2011-03-30 22:57:33 -030052/* Zero out the end of the struct pointed to by p. Everything after, but
Trent Piepho7ecc0cf2009-04-30 21:03:34 -030053 * 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
Hans Verkuil35ea11f2008-07-20 08:12:02 -030058struct 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
Trent Piepho51f0b8d52009-03-04 01:21:02 -0300117/* 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
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300130/* 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{
Trent Piepho51f0b8d52009-03-04 01:21:02 -0300135 vs->id = id;
136 v4l2_video_std_frame_period(id, &vs->frameperiod);
137 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300138 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",
Pawel Osciakf8f39142010-07-29 14:44:25 -0300169 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
170 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300171};
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 */
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300185
Hans Verkuil59076122012-06-22 06:09:54 -0300186static void v4l_print_querycap(const void *arg, bool write_only)
187{
188 const struct v4l2_capability *p = arg;
189
190 pr_cont("driver=%s, card=%s, bus=%s, version=0x%08x, "
191 "capabilities=0x%08x, device_caps=0x%08x\n",
192 p->driver, p->card, p->bus_info,
193 p->version, p->capabilities, p->device_caps);
194}
195
196static void v4l_print_enuminput(const void *arg, bool write_only)
197{
198 const struct v4l2_input *p = arg;
199
200 pr_cont("index=%u, name=%s, type=%u, audioset=0x%x, tuner=%u, "
201 "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
202 p->index, p->name, p->type, p->audioset, p->tuner,
203 (unsigned long long)p->std, p->status, p->capabilities);
204}
205
206static void v4l_print_enumoutput(const void *arg, bool write_only)
207{
208 const struct v4l2_output *p = arg;
209
210 pr_cont("index=%u, name=%s, type=%u, audioset=0x%x, "
211 "modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
212 p->index, p->name, p->type, p->audioset, p->modulator,
213 (unsigned long long)p->std, p->capabilities);
214}
215
216static void v4l_print_audio(const void *arg, bool write_only)
217{
218 const struct v4l2_audio *p = arg;
219
220 if (write_only)
221 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
222 else
223 pr_cont("index=%u, name=%s, capability=0x%x, mode=0x%x\n",
224 p->index, p->name, p->capability, p->mode);
225}
226
227static void v4l_print_audioout(const void *arg, bool write_only)
228{
229 const struct v4l2_audioout *p = arg;
230
231 if (write_only)
232 pr_cont("index=%u\n", p->index);
233 else
234 pr_cont("index=%u, name=%s, capability=0x%x, mode=0x%x\n",
235 p->index, p->name, p->capability, p->mode);
236}
237
238static void v4l_print_u32(const void *arg, bool write_only)
239{
240 pr_cont("value=%u\n", *(const u32 *)arg);
241}
242
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300243static void dbgbuf(unsigned int cmd, struct video_device *vfd,
244 struct v4l2_buffer *p)
245{
246 struct v4l2_timecode *tc = &p->timecode;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300247 struct v4l2_plane *plane;
248 int i;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300249
250 dbgarg(cmd, "%02ld:%02d:%02d.%08ld index=%d, type=%s, "
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300251 "flags=0x%08d, field=%0d, sequence=%d, memory=%s\n",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300252 p->timestamp.tv_sec / 3600,
253 (int)(p->timestamp.tv_sec / 60) % 60,
254 (int)(p->timestamp.tv_sec % 60),
Alexander Beregalovb0459792008-09-03 16:47:38 -0300255 (long)p->timestamp.tv_usec,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300256 p->index,
257 prt_names(p->type, v4l2_type_names),
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300258 p->flags, p->field, p->sequence,
259 prt_names(p->memory, v4l2_memory_names));
260
261 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
262 for (i = 0; i < p->length; ++i) {
263 plane = &p->m.planes[i];
264 dbgarg2("plane %d: bytesused=%d, data_offset=0x%08x "
265 "offset/userptr=0x%08lx, length=%d\n",
266 i, plane->bytesused, plane->data_offset,
267 plane->m.userptr, plane->length);
268 }
269 } else {
270 dbgarg2("bytesused=%d, offset/userptr=0x%08lx, length=%d\n",
271 p->bytesused, p->m.userptr, p->length);
272 }
273
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300274 dbgarg2("timecode=%02d:%02d:%02d type=%d, "
275 "flags=0x%08d, frames=%d, userbits=0x%08x\n",
276 tc->hours, tc->minutes, tc->seconds,
277 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
278}
279
280static inline void dbgrect(struct video_device *vfd, char *s,
281 struct v4l2_rect *r)
282{
283 dbgarg2("%sRect start at %dx%d, size=%dx%d\n", s, r->left, r->top,
284 r->width, r->height);
285};
286
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300287static void dbgtimings(struct video_device *vfd,
288 const struct v4l2_dv_timings *p)
289{
290 switch (p->type) {
291 case V4L2_DV_BT_656_1120:
292 dbgarg2("bt-656/1120:interlaced=%d,"
293 " pixelclock=%lld,"
294 " width=%d, height=%d, polarities=%x,"
295 " hfrontporch=%d, hsync=%d,"
296 " hbackporch=%d, vfrontporch=%d,"
297 " vsync=%d, vbackporch=%d,"
298 " il_vfrontporch=%d, il_vsync=%d,"
299 " il_vbackporch=%d, standards=%x, flags=%x\n",
300 p->bt.interlaced, p->bt.pixelclock,
301 p->bt.width, p->bt.height,
302 p->bt.polarities, p->bt.hfrontporch,
303 p->bt.hsync, p->bt.hbackporch,
304 p->bt.vfrontporch, p->bt.vsync,
305 p->bt.vbackporch, p->bt.il_vfrontporch,
306 p->bt.il_vsync, p->bt.il_vbackporch,
307 p->bt.standards, p->bt.flags);
308 break;
309 default:
310 dbgarg2("Unknown type %d!\n", p->type);
311 break;
312 }
313}
314
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300315static inline void v4l_print_pix_fmt(struct video_device *vfd,
316 struct v4l2_pix_format *fmt)
317{
318 dbgarg2("width=%d, height=%d, format=%c%c%c%c, field=%s, "
319 "bytesperline=%d sizeimage=%d, colorspace=%d\n",
320 fmt->width, fmt->height,
321 (fmt->pixelformat & 0xff),
322 (fmt->pixelformat >> 8) & 0xff,
323 (fmt->pixelformat >> 16) & 0xff,
324 (fmt->pixelformat >> 24) & 0xff,
325 prt_names(fmt->field, v4l2_field_names),
326 fmt->bytesperline, fmt->sizeimage, fmt->colorspace);
327};
328
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300329static inline void v4l_print_pix_fmt_mplane(struct video_device *vfd,
330 struct v4l2_pix_format_mplane *fmt)
331{
332 int i;
333
334 dbgarg2("width=%d, height=%d, format=%c%c%c%c, field=%s, "
335 "colorspace=%d, num_planes=%d\n",
336 fmt->width, fmt->height,
337 (fmt->pixelformat & 0xff),
338 (fmt->pixelformat >> 8) & 0xff,
339 (fmt->pixelformat >> 16) & 0xff,
340 (fmt->pixelformat >> 24) & 0xff,
341 prt_names(fmt->field, v4l2_field_names),
342 fmt->colorspace, fmt->num_planes);
343
344 for (i = 0; i < fmt->num_planes; ++i)
345 dbgarg2("plane %d: bytesperline=%d sizeimage=%d\n", i,
346 fmt->plane_fmt[i].bytesperline,
347 fmt->plane_fmt[i].sizeimage);
348}
349
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300350static inline void v4l_print_ext_ctrls(unsigned int cmd,
351 struct video_device *vfd, struct v4l2_ext_controls *c, int show_vals)
352{
353 __u32 i;
354
355 if (!(vfd->debug & V4L2_DEBUG_IOCTL_ARG))
356 return;
357 dbgarg(cmd, "");
358 printk(KERN_CONT "class=0x%x", c->ctrl_class);
359 for (i = 0; i < c->count; i++) {
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300360 if (show_vals && !c->controls[i].size)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300361 printk(KERN_CONT " id/val=0x%x/0x%x",
362 c->controls[i].id, c->controls[i].value);
363 else
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300364 printk(KERN_CONT " id=0x%x,size=%u",
365 c->controls[i].id, c->controls[i].size);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300366 }
367 printk(KERN_CONT "\n");
368};
369
370static inline int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
371{
372 __u32 i;
373
374 /* zero the reserved fields */
375 c->reserved[0] = c->reserved[1] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300376 for (i = 0; i < c->count; i++)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300377 c->controls[i].reserved2[0] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300378
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300379 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
380 when using extended controls.
381 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
382 is it allowed for backwards compatibility.
383 */
384 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
385 return 0;
386 /* Check that all controls are from the same control class. */
387 for (i = 0; i < c->count; i++) {
388 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
389 c->error_idx = i;
390 return 0;
391 }
392 }
393 return 1;
394}
395
Hans Verkuila3998102008-07-21 02:57:38 -0300396static int check_fmt(const struct v4l2_ioctl_ops *ops, enum v4l2_buf_type type)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300397{
Hans Verkuila3998102008-07-21 02:57:38 -0300398 if (ops == NULL)
399 return -EINVAL;
400
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300401 switch (type) {
402 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300403 if (ops->vidioc_g_fmt_vid_cap ||
404 ops->vidioc_g_fmt_vid_cap_mplane)
405 return 0;
406 break;
407 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
408 if (ops->vidioc_g_fmt_vid_cap_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300409 return 0;
410 break;
411 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300412 if (ops->vidioc_g_fmt_vid_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300413 return 0;
414 break;
415 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300416 if (ops->vidioc_g_fmt_vid_out ||
417 ops->vidioc_g_fmt_vid_out_mplane)
418 return 0;
419 break;
420 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
421 if (ops->vidioc_g_fmt_vid_out_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300422 return 0;
423 break;
424 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Trent Piepho1175d612009-04-30 21:03:34 -0300425 if (ops->vidioc_g_fmt_vid_out_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300426 return 0;
427 break;
428 case V4L2_BUF_TYPE_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300429 if (ops->vidioc_g_fmt_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300430 return 0;
431 break;
432 case V4L2_BUF_TYPE_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300433 if (ops->vidioc_g_fmt_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300434 return 0;
435 break;
436 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Trent Piepho1175d612009-04-30 21:03:34 -0300437 if (ops->vidioc_g_fmt_sliced_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300438 return 0;
439 break;
440 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Trent Piepho1175d612009-04-30 21:03:34 -0300441 if (ops->vidioc_g_fmt_sliced_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300442 return 0;
443 break;
444 case V4L2_BUF_TYPE_PRIVATE:
Trent Piepho1175d612009-04-30 21:03:34 -0300445 if (ops->vidioc_g_fmt_type_private)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300446 return 0;
447 break;
448 }
449 return -EINVAL;
450}
451
Hans Verkuil59076122012-06-22 06:09:54 -0300452static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
453 struct file *file, void *fh, void *arg)
454{
455 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
456
457 cap->version = LINUX_VERSION_CODE;
458 return ops->vidioc_querycap(file, fh, cap);
459}
460
461static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
462 struct file *file, void *fh, void *arg)
463{
464 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
465}
466
467static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
468 struct file *file, void *fh, void *arg)
469{
470 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
471}
472
Hans Verkuild0547cb2012-06-09 09:27:10 -0300473static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
474 struct file *file, void *fh, void *arg)
475{
476 struct video_device *vfd;
477 u32 *p = arg;
478
479 if (ops->vidioc_g_priority)
480 return ops->vidioc_g_priority(file, fh, arg);
481 vfd = video_devdata(file);
482 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
483 return 0;
484}
485
486static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
487 struct file *file, void *fh, void *arg)
488{
489 struct video_device *vfd;
490 struct v4l2_fh *vfh;
491 u32 *p = arg;
492
493 if (ops->vidioc_s_priority)
494 return ops->vidioc_s_priority(file, fh, *p);
495 vfd = video_devdata(file);
496 vfh = file->private_data;
497 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
498}
499
Hans Verkuil59076122012-06-22 06:09:54 -0300500static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
501 struct file *file, void *fh, void *arg)
502{
503 struct v4l2_input *p = arg;
504
505 /*
506 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
507 * CAP_STD here based on ioctl handler provided by the
508 * driver. If the driver doesn't support these
509 * for a specific input, it must override these flags.
510 */
511 if (ops->vidioc_s_std)
512 p->capabilities |= V4L2_IN_CAP_STD;
513 if (ops->vidioc_s_dv_preset)
514 p->capabilities |= V4L2_IN_CAP_PRESETS;
515 if (ops->vidioc_s_dv_timings)
516 p->capabilities |= V4L2_IN_CAP_CUSTOM_TIMINGS;
517
518 return ops->vidioc_enum_input(file, fh, p);
519}
520
521static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
522 struct file *file, void *fh, void *arg)
523{
524 struct v4l2_output *p = arg;
525
526 /*
527 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
528 * CAP_STD here based on ioctl handler provided by the
529 * driver. If the driver doesn't support these
530 * for a specific output, it must override these flags.
531 */
532 if (ops->vidioc_s_std)
533 p->capabilities |= V4L2_OUT_CAP_STD;
534 if (ops->vidioc_s_dv_preset)
535 p->capabilities |= V4L2_OUT_CAP_PRESETS;
536 if (ops->vidioc_s_dv_timings)
537 p->capabilities |= V4L2_OUT_CAP_CUSTOM_TIMINGS;
538
539 return ops->vidioc_enum_output(file, fh, p);
540}
541
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300542struct v4l2_ioctl_info {
543 unsigned int ioctl;
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300544 u32 flags;
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300545 const char * const name;
Hans Verkuil86748f32012-06-22 06:04:16 -0300546 union {
547 u32 offset;
548 int (*func)(const struct v4l2_ioctl_ops *ops,
549 struct file *file, void *fh, void *p);
550 };
551 void (*debug)(const void *arg, bool write_only);
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300552};
553
554/* This control needs a priority check */
555#define INFO_FL_PRIO (1 << 0)
556/* This control can be valid if the filehandle passes a control handler. */
557#define INFO_FL_CTRL (1 << 1)
Hans Verkuil86748f32012-06-22 06:04:16 -0300558/* This is a standard ioctl, no need for special code */
559#define INFO_FL_STD (1 << 2)
560/* This is ioctl has its own function */
561#define INFO_FL_FUNC (1 << 3)
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300562/* Zero struct from after the field to the end */
563#define INFO_FL_CLEAR(v4l2_struct, field) \
564 ((offsetof(struct v4l2_struct, field) + \
565 sizeof(((struct v4l2_struct *)0)->field)) << 16)
566#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300567
568#define IOCTL_INFO(_ioctl, _flags) [_IOC_NR(_ioctl)] = { \
569 .ioctl = _ioctl, \
570 .flags = _flags, \
571 .name = #_ioctl, \
572}
573
Hans Verkuil86748f32012-06-22 06:04:16 -0300574#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
575 [_IOC_NR(_ioctl)] = { \
576 .ioctl = _ioctl, \
577 .flags = _flags | INFO_FL_STD, \
578 .name = #_ioctl, \
579 .offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
580 .debug = _debug, \
581 }
582
583#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
584 [_IOC_NR(_ioctl)] = { \
585 .ioctl = _ioctl, \
586 .flags = _flags | INFO_FL_FUNC, \
587 .name = #_ioctl, \
588 .func = _func, \
589 .debug = _debug, \
590 }
591
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300592static struct v4l2_ioctl_info v4l2_ioctls[] = {
Hans Verkuil59076122012-06-22 06:09:54 -0300593 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300594 IOCTL_INFO(VIDIOC_ENUM_FMT, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
595 IOCTL_INFO(VIDIOC_G_FMT, INFO_FL_CLEAR(v4l2_format, type)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300596 IOCTL_INFO(VIDIOC_S_FMT, INFO_FL_PRIO),
597 IOCTL_INFO(VIDIOC_REQBUFS, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300598 IOCTL_INFO(VIDIOC_QUERYBUF, INFO_FL_CLEAR(v4l2_buffer, length)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300599 IOCTL_INFO(VIDIOC_G_FBUF, 0),
600 IOCTL_INFO(VIDIOC_S_FBUF, INFO_FL_PRIO),
601 IOCTL_INFO(VIDIOC_OVERLAY, INFO_FL_PRIO),
602 IOCTL_INFO(VIDIOC_QBUF, 0),
603 IOCTL_INFO(VIDIOC_DQBUF, 0),
604 IOCTL_INFO(VIDIOC_STREAMON, INFO_FL_PRIO),
605 IOCTL_INFO(VIDIOC_STREAMOFF, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300606 IOCTL_INFO(VIDIOC_G_PARM, INFO_FL_CLEAR(v4l2_streamparm, type)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300607 IOCTL_INFO(VIDIOC_S_PARM, INFO_FL_PRIO),
608 IOCTL_INFO(VIDIOC_G_STD, 0),
609 IOCTL_INFO(VIDIOC_S_STD, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300610 IOCTL_INFO(VIDIOC_ENUMSTD, INFO_FL_CLEAR(v4l2_standard, index)),
Hans Verkuil59076122012-06-22 06:09:54 -0300611 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300612 IOCTL_INFO(VIDIOC_G_CTRL, INFO_FL_CTRL),
613 IOCTL_INFO(VIDIOC_S_CTRL, INFO_FL_PRIO | INFO_FL_CTRL),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300614 IOCTL_INFO(VIDIOC_G_TUNER, INFO_FL_CLEAR(v4l2_tuner, index)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300615 IOCTL_INFO(VIDIOC_S_TUNER, INFO_FL_PRIO),
Hans Verkuil59076122012-06-22 06:09:54 -0300616 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
617 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300618 IOCTL_INFO(VIDIOC_QUERYCTRL, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
619 IOCTL_INFO(VIDIOC_QUERYMENU, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
Hans Verkuil59076122012-06-22 06:09:54 -0300620 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
621 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
622 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
623 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
624 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
625 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
626 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300627 IOCTL_INFO(VIDIOC_G_MODULATOR, INFO_FL_CLEAR(v4l2_modulator, index)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300628 IOCTL_INFO(VIDIOC_S_MODULATOR, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300629 IOCTL_INFO(VIDIOC_G_FREQUENCY, INFO_FL_CLEAR(v4l2_frequency, tuner)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300630 IOCTL_INFO(VIDIOC_S_FREQUENCY, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300631 IOCTL_INFO(VIDIOC_CROPCAP, INFO_FL_CLEAR(v4l2_cropcap, type)),
632 IOCTL_INFO(VIDIOC_G_CROP, INFO_FL_CLEAR(v4l2_crop, type)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300633 IOCTL_INFO(VIDIOC_S_CROP, INFO_FL_PRIO),
634 IOCTL_INFO(VIDIOC_G_SELECTION, 0),
635 IOCTL_INFO(VIDIOC_S_SELECTION, INFO_FL_PRIO),
636 IOCTL_INFO(VIDIOC_G_JPEGCOMP, 0),
637 IOCTL_INFO(VIDIOC_S_JPEGCOMP, INFO_FL_PRIO),
638 IOCTL_INFO(VIDIOC_QUERYSTD, 0),
639 IOCTL_INFO(VIDIOC_TRY_FMT, 0),
Hans Verkuil59076122012-06-22 06:09:54 -0300640 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
641 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
Hans Verkuild0547cb2012-06-09 09:27:10 -0300642 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
643 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300644 IOCTL_INFO(VIDIOC_G_SLICED_VBI_CAP, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300645 IOCTL_INFO(VIDIOC_LOG_STATUS, 0),
646 IOCTL_INFO(VIDIOC_G_EXT_CTRLS, INFO_FL_CTRL),
647 IOCTL_INFO(VIDIOC_S_EXT_CTRLS, INFO_FL_PRIO | INFO_FL_CTRL),
648 IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, 0),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300649 IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
650 IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300651 IOCTL_INFO(VIDIOC_G_ENC_INDEX, 0),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300652 IOCTL_INFO(VIDIOC_ENCODER_CMD, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
653 IOCTL_INFO(VIDIOC_TRY_ENCODER_CMD, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300654 IOCTL_INFO(VIDIOC_DECODER_CMD, INFO_FL_PRIO),
655 IOCTL_INFO(VIDIOC_TRY_DECODER_CMD, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300656 IOCTL_INFO(VIDIOC_DBG_S_REGISTER, 0),
657 IOCTL_INFO(VIDIOC_DBG_G_REGISTER, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300658 IOCTL_INFO(VIDIOC_DBG_G_CHIP_IDENT, 0),
659 IOCTL_INFO(VIDIOC_S_HW_FREQ_SEEK, INFO_FL_PRIO),
660 IOCTL_INFO(VIDIOC_ENUM_DV_PRESETS, 0),
661 IOCTL_INFO(VIDIOC_S_DV_PRESET, INFO_FL_PRIO),
662 IOCTL_INFO(VIDIOC_G_DV_PRESET, 0),
663 IOCTL_INFO(VIDIOC_QUERY_DV_PRESET, 0),
664 IOCTL_INFO(VIDIOC_S_DV_TIMINGS, INFO_FL_PRIO),
665 IOCTL_INFO(VIDIOC_G_DV_TIMINGS, 0),
666 IOCTL_INFO(VIDIOC_DQEVENT, 0),
667 IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, 0),
668 IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, 0),
669 IOCTL_INFO(VIDIOC_CREATE_BUFS, INFO_FL_PRIO),
670 IOCTL_INFO(VIDIOC_PREPARE_BUF, 0),
671 IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, 0),
672 IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, 0),
673 IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, 0),
674};
675#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
676
677bool v4l2_is_known_ioctl(unsigned int cmd)
678{
679 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
680 return false;
681 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
682}
683
684/* Common ioctl debug function. This function can be used by
685 external ioctl messages as well as internal V4L ioctl */
686void v4l_printk_ioctl(unsigned int cmd)
687{
Hans Verkuil86748f32012-06-22 06:04:16 -0300688 const char *dir, *type;
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300689
690 switch (_IOC_TYPE(cmd)) {
691 case 'd':
692 type = "v4l2_int";
693 break;
694 case 'V':
695 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
696 type = "v4l2";
697 break;
698 }
Hans Verkuil86748f32012-06-22 06:04:16 -0300699 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300700 return;
701 default:
702 type = "unknown";
Hans Verkuil86748f32012-06-22 06:04:16 -0300703 break;
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300704 }
705
706 switch (_IOC_DIR(cmd)) {
707 case _IOC_NONE: dir = "--"; break;
708 case _IOC_READ: dir = "r-"; break;
709 case _IOC_WRITE: dir = "-w"; break;
710 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
711 default: dir = "*ERR*"; break;
712 }
Hans Verkuil86748f32012-06-22 06:04:16 -0300713 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300714 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
715}
716EXPORT_SYMBOL(v4l_printk_ioctl);
717
Hans Verkuil069b7472008-12-30 07:04:34 -0300718static long __video_do_ioctl(struct file *file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300719 unsigned int cmd, void *arg)
720{
721 struct video_device *vfd = video_devdata(file);
Hans Verkuila3998102008-07-21 02:57:38 -0300722 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
Hans Verkuil86748f32012-06-22 06:04:16 -0300723 bool write_only = false;
724 struct v4l2_ioctl_info default_info;
725 const struct v4l2_ioctl_info *info;
Hans Verkuild5fbf322008-10-18 13:39:53 -0300726 void *fh = file->private_data;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300727 struct v4l2_fh *vfh = NULL;
Hans Verkuilb1a873a2011-03-22 10:14:07 -0300728 int use_fh_prio = 0;
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -0300729 long ret = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300730
Hans Verkuil6a717882010-04-06 15:56:08 -0300731 if (ops == NULL) {
732 printk(KERN_WARNING "videodev: \"%s\" has no ioctl_ops.\n",
733 vfd->name);
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -0300734 return ret;
Hans Verkuil6a717882010-04-06 15:56:08 -0300735 }
736
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300737 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
738 vfh = file->private_data;
739 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300740 }
741
742 if (v4l2_is_known_ioctl(cmd)) {
Hans Verkuil86748f32012-06-22 06:04:16 -0300743 info = &v4l2_ioctls[_IOC_NR(cmd)];
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300744
745 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
746 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
Hans Verkuil86748f32012-06-22 06:04:16 -0300747 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -0300748
749 if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
750 ret = v4l2_prio_check(vfd->prio, vfh->prio);
751 if (ret)
Hans Verkuil86748f32012-06-22 06:04:16 -0300752 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -0300753 }
Hans Verkuil86748f32012-06-22 06:04:16 -0300754 } else {
755 default_info.ioctl = cmd;
756 default_info.flags = 0;
757 default_info.debug = NULL;
758 info = &default_info;
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300759 }
760
Hans Verkuil86748f32012-06-22 06:04:16 -0300761 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
762 if (info->debug && write_only && vfd->debug > V4L2_DEBUG_IOCTL) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300763 v4l_print_ioctl(vfd->name, cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -0300764 pr_cont(": ");
765 info->debug(arg, write_only);
766 }
767 if (info->flags & INFO_FL_STD) {
768 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
769 const void *p = vfd->ioctl_ops;
770 const vidioc_op *vidioc = p + info->offset;
771
772 ret = (*vidioc)(file, fh, arg);
773 goto done;
774 } else if (info->flags & INFO_FL_FUNC) {
775 ret = info->func(ops, file, fh, arg);
776 goto done;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300777 }
778
Hans Verkuil6a717882010-04-06 15:56:08 -0300779 switch (cmd) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300780 /* --- capture ioctls ---------------------------------------- */
781 case VIDIOC_ENUM_FMT:
782 {
783 struct v4l2_fmtdesc *f = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300784
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300785 ret = -EINVAL;
Trent Piepho19c96e42009-03-04 01:21:02 -0300786 switch (f->type) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300787 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300788 if (likely(ops->vidioc_enum_fmt_vid_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300789 ret = ops->vidioc_enum_fmt_vid_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300790 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300791 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300792 if (likely(ops->vidioc_enum_fmt_vid_cap_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300793 ret = ops->vidioc_enum_fmt_vid_cap_mplane(file,
794 fh, f);
795 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300796 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300797 if (likely(ops->vidioc_enum_fmt_vid_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -0300798 ret = ops->vidioc_enum_fmt_vid_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300799 fh, f);
800 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300801 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300802 if (likely(ops->vidioc_enum_fmt_vid_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300803 ret = ops->vidioc_enum_fmt_vid_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300804 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300805 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300806 if (likely(ops->vidioc_enum_fmt_vid_out_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300807 ret = ops->vidioc_enum_fmt_vid_out_mplane(file,
808 fh, f);
809 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300810 case V4L2_BUF_TYPE_PRIVATE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300811 if (likely(ops->vidioc_enum_fmt_type_private))
Hans Verkuila3998102008-07-21 02:57:38 -0300812 ret = ops->vidioc_enum_fmt_type_private(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300813 fh, f);
814 break;
815 default:
816 break;
817 }
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300818 if (likely(!ret))
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300819 dbgarg(cmd, "index=%d, type=%d, flags=%d, "
820 "pixelformat=%c%c%c%c, description='%s'\n",
821 f->index, f->type, f->flags,
822 (f->pixelformat & 0xff),
823 (f->pixelformat >> 8) & 0xff,
824 (f->pixelformat >> 16) & 0xff,
825 (f->pixelformat >> 24) & 0xff,
826 f->description);
827 break;
828 }
829 case VIDIOC_G_FMT:
830 {
831 struct v4l2_format *f = (struct v4l2_format *)arg;
832
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300833 /* FIXME: Should be one dump per type */
834 dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
835
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300836 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300837 switch (f->type) {
838 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300839 if (ops->vidioc_g_fmt_vid_cap)
Hans Verkuila3998102008-07-21 02:57:38 -0300840 ret = ops->vidioc_g_fmt_vid_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300841 if (!ret)
842 v4l_print_pix_fmt(vfd, &f->fmt.pix);
843 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300844 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300845 if (ops->vidioc_g_fmt_vid_cap_mplane)
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300846 ret = ops->vidioc_g_fmt_vid_cap_mplane(file,
847 fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300848 if (!ret)
849 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
850 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300851 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300852 if (likely(ops->vidioc_g_fmt_vid_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -0300853 ret = ops->vidioc_g_fmt_vid_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300854 fh, f);
855 break;
856 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300857 if (ops->vidioc_g_fmt_vid_out)
Hans Verkuila3998102008-07-21 02:57:38 -0300858 ret = ops->vidioc_g_fmt_vid_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300859 if (!ret)
860 v4l_print_pix_fmt(vfd, &f->fmt.pix);
861 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300862 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300863 if (ops->vidioc_g_fmt_vid_out_mplane)
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300864 ret = ops->vidioc_g_fmt_vid_out_mplane(file,
865 fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300866 if (!ret)
867 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
868 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300869 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300870 if (likely(ops->vidioc_g_fmt_vid_out_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -0300871 ret = ops->vidioc_g_fmt_vid_out_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300872 fh, f);
873 break;
874 case V4L2_BUF_TYPE_VBI_CAPTURE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300875 if (likely(ops->vidioc_g_fmt_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300876 ret = ops->vidioc_g_fmt_vbi_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300877 break;
878 case V4L2_BUF_TYPE_VBI_OUTPUT:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300879 if (likely(ops->vidioc_g_fmt_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300880 ret = ops->vidioc_g_fmt_vbi_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300881 break;
882 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300883 if (likely(ops->vidioc_g_fmt_sliced_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300884 ret = ops->vidioc_g_fmt_sliced_vbi_cap(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300885 fh, f);
886 break;
887 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300888 if (likely(ops->vidioc_g_fmt_sliced_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300889 ret = ops->vidioc_g_fmt_sliced_vbi_out(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300890 fh, f);
891 break;
892 case V4L2_BUF_TYPE_PRIVATE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300893 if (likely(ops->vidioc_g_fmt_type_private))
Hans Verkuila3998102008-07-21 02:57:38 -0300894 ret = ops->vidioc_g_fmt_type_private(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300895 fh, f);
896 break;
897 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300898 break;
899 }
900 case VIDIOC_S_FMT:
901 {
902 struct v4l2_format *f = (struct v4l2_format *)arg;
903
Hans Verkuil93d5a302011-08-09 09:32:06 -0300904 ret = -EINVAL;
905
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300906 /* FIXME: Should be one dump per type */
907 dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
908
909 switch (f->type) {
910 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300911 CLEAR_AFTER_FIELD(f, fmt.pix);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300912 v4l_print_pix_fmt(vfd, &f->fmt.pix);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300913 if (ops->vidioc_s_fmt_vid_cap)
Hans Verkuila3998102008-07-21 02:57:38 -0300914 ret = ops->vidioc_s_fmt_vid_cap(file, fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300915 break;
916 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
917 CLEAR_AFTER_FIELD(f, fmt.pix_mp);
918 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300919 if (ops->vidioc_s_fmt_vid_cap_mplane)
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300920 ret = ops->vidioc_s_fmt_vid_cap_mplane(file,
921 fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300922 break;
923 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300924 CLEAR_AFTER_FIELD(f, fmt.win);
Hans Verkuila3998102008-07-21 02:57:38 -0300925 if (ops->vidioc_s_fmt_vid_overlay)
926 ret = ops->vidioc_s_fmt_vid_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300927 fh, f);
928 break;
929 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300930 CLEAR_AFTER_FIELD(f, fmt.pix);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300931 v4l_print_pix_fmt(vfd, &f->fmt.pix);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300932 if (ops->vidioc_s_fmt_vid_out)
Hans Verkuila3998102008-07-21 02:57:38 -0300933 ret = ops->vidioc_s_fmt_vid_out(file, fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300934 break;
935 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
936 CLEAR_AFTER_FIELD(f, fmt.pix_mp);
937 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300938 if (ops->vidioc_s_fmt_vid_out_mplane)
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300939 ret = ops->vidioc_s_fmt_vid_out_mplane(file,
940 fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300941 break;
942 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300943 CLEAR_AFTER_FIELD(f, fmt.win);
Hans Verkuila3998102008-07-21 02:57:38 -0300944 if (ops->vidioc_s_fmt_vid_out_overlay)
945 ret = ops->vidioc_s_fmt_vid_out_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300946 fh, f);
947 break;
948 case V4L2_BUF_TYPE_VBI_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300949 CLEAR_AFTER_FIELD(f, fmt.vbi);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300950 if (likely(ops->vidioc_s_fmt_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300951 ret = ops->vidioc_s_fmt_vbi_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300952 break;
953 case V4L2_BUF_TYPE_VBI_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300954 CLEAR_AFTER_FIELD(f, fmt.vbi);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300955 if (likely(ops->vidioc_s_fmt_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300956 ret = ops->vidioc_s_fmt_vbi_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300957 break;
958 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300959 CLEAR_AFTER_FIELD(f, fmt.sliced);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300960 if (likely(ops->vidioc_s_fmt_sliced_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300961 ret = ops->vidioc_s_fmt_sliced_vbi_cap(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300962 fh, f);
963 break;
964 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300965 CLEAR_AFTER_FIELD(f, fmt.sliced);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300966 if (likely(ops->vidioc_s_fmt_sliced_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300967 ret = ops->vidioc_s_fmt_sliced_vbi_out(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300968 fh, f);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300969
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300970 break;
971 case V4L2_BUF_TYPE_PRIVATE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300972 /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300973 if (likely(ops->vidioc_s_fmt_type_private))
Hans Verkuila3998102008-07-21 02:57:38 -0300974 ret = ops->vidioc_s_fmt_type_private(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300975 fh, f);
976 break;
977 }
978 break;
979 }
980 case VIDIOC_TRY_FMT:
981 {
982 struct v4l2_format *f = (struct v4l2_format *)arg;
983
984 /* FIXME: Should be one dump per type */
985 dbgarg(cmd, "type=%s\n", prt_names(f->type,
986 v4l2_type_names));
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300987 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300988 switch (f->type) {
989 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300990 CLEAR_AFTER_FIELD(f, fmt.pix);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300991 if (ops->vidioc_try_fmt_vid_cap)
Hans Verkuila3998102008-07-21 02:57:38 -0300992 ret = ops->vidioc_try_fmt_vid_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300993 if (!ret)
994 v4l_print_pix_fmt(vfd, &f->fmt.pix);
995 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300996 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
997 CLEAR_AFTER_FIELD(f, fmt.pix_mp);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300998 if (ops->vidioc_try_fmt_vid_cap_mplane)
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300999 ret = ops->vidioc_try_fmt_vid_cap_mplane(file,
1000 fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -03001001 if (!ret)
1002 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
1003 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001004 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001005 CLEAR_AFTER_FIELD(f, fmt.win);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001006 if (likely(ops->vidioc_try_fmt_vid_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -03001007 ret = ops->vidioc_try_fmt_vid_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001008 fh, f);
1009 break;
1010 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001011 CLEAR_AFTER_FIELD(f, fmt.pix);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -03001012 if (ops->vidioc_try_fmt_vid_out)
Hans Verkuila3998102008-07-21 02:57:38 -03001013 ret = ops->vidioc_try_fmt_vid_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001014 if (!ret)
1015 v4l_print_pix_fmt(vfd, &f->fmt.pix);
1016 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03001017 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1018 CLEAR_AFTER_FIELD(f, fmt.pix_mp);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -03001019 if (ops->vidioc_try_fmt_vid_out_mplane)
Pawel Osciakd14e6d72010-12-23 04:15:27 -03001020 ret = ops->vidioc_try_fmt_vid_out_mplane(file,
1021 fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -03001022 if (!ret)
1023 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
1024 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001025 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001026 CLEAR_AFTER_FIELD(f, fmt.win);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001027 if (likely(ops->vidioc_try_fmt_vid_out_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -03001028 ret = ops->vidioc_try_fmt_vid_out_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001029 fh, f);
1030 break;
1031 case V4L2_BUF_TYPE_VBI_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001032 CLEAR_AFTER_FIELD(f, fmt.vbi);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001033 if (likely(ops->vidioc_try_fmt_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -03001034 ret = ops->vidioc_try_fmt_vbi_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001035 break;
1036 case V4L2_BUF_TYPE_VBI_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001037 CLEAR_AFTER_FIELD(f, fmt.vbi);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001038 if (likely(ops->vidioc_try_fmt_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -03001039 ret = ops->vidioc_try_fmt_vbi_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001040 break;
1041 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001042 CLEAR_AFTER_FIELD(f, fmt.sliced);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001043 if (likely(ops->vidioc_try_fmt_sliced_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -03001044 ret = ops->vidioc_try_fmt_sliced_vbi_cap(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001045 fh, f);
1046 break;
1047 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001048 CLEAR_AFTER_FIELD(f, fmt.sliced);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001049 if (likely(ops->vidioc_try_fmt_sliced_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -03001050 ret = ops->vidioc_try_fmt_sliced_vbi_out(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001051 fh, f);
1052 break;
1053 case V4L2_BUF_TYPE_PRIVATE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001054 /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001055 if (likely(ops->vidioc_try_fmt_type_private))
Hans Verkuila3998102008-07-21 02:57:38 -03001056 ret = ops->vidioc_try_fmt_type_private(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001057 fh, f);
1058 break;
1059 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001060 break;
1061 }
1062 /* FIXME: Those buf reqs could be handled here,
1063 with some changes on videobuf to allow its header to be included at
1064 videodev2.h or being merged at videodev2.
1065 */
1066 case VIDIOC_REQBUFS:
1067 {
1068 struct v4l2_requestbuffers *p = arg;
1069
Hans Verkuila3998102008-07-21 02:57:38 -03001070 ret = check_fmt(ops, p->type);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001071 if (ret)
1072 break;
1073
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001074 if (p->type < V4L2_BUF_TYPE_PRIVATE)
1075 CLEAR_AFTER_FIELD(p, memory);
1076
Hans Verkuila3998102008-07-21 02:57:38 -03001077 ret = ops->vidioc_reqbufs(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001078 dbgarg(cmd, "count=%d, type=%s, memory=%s\n",
1079 p->count,
1080 prt_names(p->type, v4l2_type_names),
1081 prt_names(p->memory, v4l2_memory_names));
1082 break;
1083 }
1084 case VIDIOC_QUERYBUF:
1085 {
1086 struct v4l2_buffer *p = arg;
1087
Hans Verkuila3998102008-07-21 02:57:38 -03001088 ret = check_fmt(ops, p->type);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001089 if (ret)
1090 break;
1091
Hans Verkuila3998102008-07-21 02:57:38 -03001092 ret = ops->vidioc_querybuf(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001093 if (!ret)
1094 dbgbuf(cmd, vfd, p);
1095 break;
1096 }
1097 case VIDIOC_QBUF:
1098 {
1099 struct v4l2_buffer *p = arg;
1100
Hans Verkuila3998102008-07-21 02:57:38 -03001101 ret = check_fmt(ops, p->type);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001102 if (ret)
1103 break;
1104
Hans Verkuila3998102008-07-21 02:57:38 -03001105 ret = ops->vidioc_qbuf(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001106 if (!ret)
1107 dbgbuf(cmd, vfd, p);
1108 break;
1109 }
1110 case VIDIOC_DQBUF:
1111 {
1112 struct v4l2_buffer *p = arg;
1113
Hans Verkuila3998102008-07-21 02:57:38 -03001114 ret = check_fmt(ops, p->type);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001115 if (ret)
1116 break;
1117
Hans Verkuila3998102008-07-21 02:57:38 -03001118 ret = ops->vidioc_dqbuf(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001119 if (!ret)
1120 dbgbuf(cmd, vfd, p);
1121 break;
1122 }
1123 case VIDIOC_OVERLAY:
1124 {
1125 int *i = arg;
1126
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001127 dbgarg(cmd, "value=%d\n", *i);
Hans Verkuila3998102008-07-21 02:57:38 -03001128 ret = ops->vidioc_overlay(file, fh, *i);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001129 break;
1130 }
1131 case VIDIOC_G_FBUF:
1132 {
1133 struct v4l2_framebuffer *p = arg;
1134
Hans Verkuila3998102008-07-21 02:57:38 -03001135 ret = ops->vidioc_g_fbuf(file, fh, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001136 if (!ret) {
1137 dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
1138 p->capability, p->flags,
1139 (unsigned long)p->base);
1140 v4l_print_pix_fmt(vfd, &p->fmt);
1141 }
1142 break;
1143 }
1144 case VIDIOC_S_FBUF:
1145 {
1146 struct v4l2_framebuffer *p = arg;
1147
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001148 dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
1149 p->capability, p->flags, (unsigned long)p->base);
1150 v4l_print_pix_fmt(vfd, &p->fmt);
Hans Verkuila3998102008-07-21 02:57:38 -03001151 ret = ops->vidioc_s_fbuf(file, fh, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001152 break;
1153 }
1154 case VIDIOC_STREAMON:
1155 {
1156 enum v4l2_buf_type i = *(int *)arg;
1157
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001158 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
Hans Verkuila3998102008-07-21 02:57:38 -03001159 ret = ops->vidioc_streamon(file, fh, i);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001160 break;
1161 }
1162 case VIDIOC_STREAMOFF:
1163 {
1164 enum v4l2_buf_type i = *(int *)arg;
1165
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001166 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
Hans Verkuila3998102008-07-21 02:57:38 -03001167 ret = ops->vidioc_streamoff(file, fh, i);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001168 break;
1169 }
1170 /* ---------- tv norms ---------- */
1171 case VIDIOC_ENUMSTD:
1172 {
1173 struct v4l2_standard *p = arg;
1174 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1175 unsigned int index = p->index, i, j = 0;
1176 const char *descr = "";
1177
Hans Verkuil93d5a302011-08-09 09:32:06 -03001178 if (id == 0)
1179 break;
1180 ret = -EINVAL;
1181
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001182 /* Return norm array in a canonical way */
1183 for (i = 0; i <= index && id; i++) {
1184 /* last std value in the standards array is 0, so this
1185 while always ends there since (id & 0) == 0. */
1186 while ((id & standards[j].std) != standards[j].std)
1187 j++;
1188 curr_id = standards[j].std;
1189 descr = standards[j].descr;
1190 j++;
1191 if (curr_id == 0)
1192 break;
1193 if (curr_id != V4L2_STD_PAL &&
1194 curr_id != V4L2_STD_SECAM &&
1195 curr_id != V4L2_STD_NTSC)
1196 id &= ~curr_id;
1197 }
1198 if (i <= index)
Hans Verkuil3f5e1822010-04-06 08:14:11 -03001199 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001200
1201 v4l2_video_std_construct(p, curr_id, descr);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001202
1203 dbgarg(cmd, "index=%d, id=0x%Lx, name=%s, fps=%d/%d, "
1204 "framelines=%d\n", p->index,
1205 (unsigned long long)p->id, p->name,
1206 p->frameperiod.numerator,
1207 p->frameperiod.denominator,
1208 p->framelines);
1209
1210 ret = 0;
1211 break;
1212 }
1213 case VIDIOC_G_STD:
1214 {
1215 v4l2_std_id *id = arg;
1216
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001217 /* Calls the specific handler */
Hans Verkuila3998102008-07-21 02:57:38 -03001218 if (ops->vidioc_g_std)
1219 ret = ops->vidioc_g_std(file, fh, id);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001220 else if (vfd->current_norm) {
1221 ret = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001222 *id = vfd->current_norm;
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001223 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001224
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001225 if (likely(!ret))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001226 dbgarg(cmd, "std=0x%08Lx\n", (long long unsigned)*id);
1227 break;
1228 }
1229 case VIDIOC_S_STD:
1230 {
1231 v4l2_std_id *id = arg, norm;
1232
1233 dbgarg(cmd, "std=%08Lx\n", (long long unsigned)*id);
1234
Hans Verkuil93d5a302011-08-09 09:32:06 -03001235 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001236 norm = (*id) & vfd->tvnorms;
1237 if (vfd->tvnorms && !norm) /* Check if std is supported */
1238 break;
1239
1240 /* Calls the specific handler */
Hans Verkuil93d5a302011-08-09 09:32:06 -03001241 ret = ops->vidioc_s_std(file, fh, &norm);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001242
1243 /* Updates standard information */
1244 if (ret >= 0)
1245 vfd->current_norm = norm;
1246 break;
1247 }
1248 case VIDIOC_QUERYSTD:
1249 {
1250 v4l2_std_id *p = arg;
1251
Mauro Carvalho Chehab8715e6c2011-10-04 09:32:23 -03001252 /*
1253 * If nothing detected, it should return all supported
1254 * Drivers just need to mask the std argument, in order
1255 * to remove the standards that don't apply from the mask.
1256 * This means that tuners, audio and video decoders can join
1257 * their efforts to improve the standards detection
1258 */
1259 *p = vfd->tvnorms;
Hans Verkuila3998102008-07-21 02:57:38 -03001260 ret = ops->vidioc_querystd(file, fh, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001261 if (!ret)
1262 dbgarg(cmd, "detected std=%08Lx\n",
1263 (unsigned long long)*p);
1264 break;
1265 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001266
1267 /* --- controls ---------------------------------------------- */
1268 case VIDIOC_QUERYCTRL:
1269 {
1270 struct v4l2_queryctrl *p = arg;
1271
Hans Verkuil2d28b682011-03-12 08:54:43 -03001272 if (vfh && vfh->ctrl_handler)
1273 ret = v4l2_queryctrl(vfh->ctrl_handler, p);
1274 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001275 ret = v4l2_queryctrl(vfd->ctrl_handler, p);
1276 else if (ops->vidioc_queryctrl)
1277 ret = ops->vidioc_queryctrl(file, fh, p);
1278 else
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001279 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001280 if (!ret)
1281 dbgarg(cmd, "id=0x%x, type=%d, name=%s, min/max=%d/%d, "
1282 "step=%d, default=%d, flags=0x%08x\n",
1283 p->id, p->type, p->name,
1284 p->minimum, p->maximum,
1285 p->step, p->default_value, p->flags);
1286 else
1287 dbgarg(cmd, "id=0x%x\n", p->id);
1288 break;
1289 }
1290 case VIDIOC_G_CTRL:
1291 {
1292 struct v4l2_control *p = arg;
1293
Hans Verkuil2d28b682011-03-12 08:54:43 -03001294 if (vfh && vfh->ctrl_handler)
1295 ret = v4l2_g_ctrl(vfh->ctrl_handler, p);
1296 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001297 ret = v4l2_g_ctrl(vfd->ctrl_handler, p);
1298 else if (ops->vidioc_g_ctrl)
Hans Verkuila3998102008-07-21 02:57:38 -03001299 ret = ops->vidioc_g_ctrl(file, fh, p);
1300 else if (ops->vidioc_g_ext_ctrls) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001301 struct v4l2_ext_controls ctrls;
1302 struct v4l2_ext_control ctrl;
1303
1304 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1305 ctrls.count = 1;
1306 ctrls.controls = &ctrl;
1307 ctrl.id = p->id;
1308 ctrl.value = p->value;
1309 if (check_ext_ctrls(&ctrls, 1)) {
Hans Verkuila3998102008-07-21 02:57:38 -03001310 ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001311 if (ret == 0)
1312 p->value = ctrl.value;
1313 }
1314 } else
1315 break;
1316 if (!ret)
1317 dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1318 else
1319 dbgarg(cmd, "id=0x%x\n", p->id);
1320 break;
1321 }
1322 case VIDIOC_S_CTRL:
1323 {
1324 struct v4l2_control *p = arg;
1325 struct v4l2_ext_controls ctrls;
1326 struct v4l2_ext_control ctrl;
1327
Hans Verkuil2d28b682011-03-12 08:54:43 -03001328 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001329 !ops->vidioc_s_ctrl && !ops->vidioc_s_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001330 break;
1331
1332 dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1333
Hans Verkuil2d28b682011-03-12 08:54:43 -03001334 if (vfh && vfh->ctrl_handler) {
Hans Verkuilab892ba2011-06-07 06:47:18 -03001335 ret = v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001336 break;
1337 }
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001338 if (vfd->ctrl_handler) {
Hans Verkuilab892ba2011-06-07 06:47:18 -03001339 ret = v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001340 break;
1341 }
Hans Verkuila3998102008-07-21 02:57:38 -03001342 if (ops->vidioc_s_ctrl) {
1343 ret = ops->vidioc_s_ctrl(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001344 break;
1345 }
Hans Verkuila3998102008-07-21 02:57:38 -03001346 if (!ops->vidioc_s_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001347 break;
1348
1349 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1350 ctrls.count = 1;
1351 ctrls.controls = &ctrl;
1352 ctrl.id = p->id;
1353 ctrl.value = p->value;
1354 if (check_ext_ctrls(&ctrls, 1))
Hans Verkuila3998102008-07-21 02:57:38 -03001355 ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001356 else
1357 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001358 break;
1359 }
1360 case VIDIOC_G_EXT_CTRLS:
1361 {
1362 struct v4l2_ext_controls *p = arg;
1363
1364 p->error_idx = p->count;
Hans Verkuil2d28b682011-03-12 08:54:43 -03001365 if (vfh && vfh->ctrl_handler)
1366 ret = v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1367 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001368 ret = v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001369 else if (ops->vidioc_g_ext_ctrls)
1370 ret = check_ext_ctrls(p, 0) ?
1371 ops->vidioc_g_ext_ctrls(file, fh, p) :
1372 -EINVAL;
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001373 else
1374 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001375 v4l_print_ext_ctrls(cmd, vfd, p, !ret);
1376 break;
1377 }
1378 case VIDIOC_S_EXT_CTRLS:
1379 {
1380 struct v4l2_ext_controls *p = arg;
1381
1382 p->error_idx = p->count;
Hans Verkuil2d28b682011-03-12 08:54:43 -03001383 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
1384 !ops->vidioc_s_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001385 break;
1386 v4l_print_ext_ctrls(cmd, vfd, p, 1);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001387 if (vfh && vfh->ctrl_handler)
Hans Verkuilab892ba2011-06-07 06:47:18 -03001388 ret = v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001389 else if (vfd->ctrl_handler)
Hans Verkuilab892ba2011-06-07 06:47:18 -03001390 ret = v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001391 else if (check_ext_ctrls(p, 0))
Hans Verkuila3998102008-07-21 02:57:38 -03001392 ret = ops->vidioc_s_ext_ctrls(file, fh, p);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001393 else
1394 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001395 break;
1396 }
1397 case VIDIOC_TRY_EXT_CTRLS:
1398 {
1399 struct v4l2_ext_controls *p = arg;
1400
1401 p->error_idx = p->count;
Hans Verkuil2d28b682011-03-12 08:54:43 -03001402 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
1403 !ops->vidioc_try_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001404 break;
1405 v4l_print_ext_ctrls(cmd, vfd, p, 1);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001406 if (vfh && vfh->ctrl_handler)
1407 ret = v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1408 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001409 ret = v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1410 else if (check_ext_ctrls(p, 0))
Hans Verkuila3998102008-07-21 02:57:38 -03001411 ret = ops->vidioc_try_ext_ctrls(file, fh, p);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001412 else
1413 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001414 break;
1415 }
1416 case VIDIOC_QUERYMENU:
1417 {
1418 struct v4l2_querymenu *p = arg;
1419
Hans Verkuil2d28b682011-03-12 08:54:43 -03001420 if (vfh && vfh->ctrl_handler)
1421 ret = v4l2_querymenu(vfh->ctrl_handler, p);
1422 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001423 ret = v4l2_querymenu(vfd->ctrl_handler, p);
1424 else if (ops->vidioc_querymenu)
1425 ret = ops->vidioc_querymenu(file, fh, p);
1426 else
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001427 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001428 if (!ret)
1429 dbgarg(cmd, "id=0x%x, index=%d, name=%s\n",
1430 p->id, p->index, p->name);
1431 else
1432 dbgarg(cmd, "id=0x%x, index=%d\n",
1433 p->id, p->index);
1434 break;
1435 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001436 case VIDIOC_G_MODULATOR:
1437 {
1438 struct v4l2_modulator *p = arg;
1439
Hans Verkuila3998102008-07-21 02:57:38 -03001440 ret = ops->vidioc_g_modulator(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001441 if (!ret)
1442 dbgarg(cmd, "index=%d, name=%s, "
1443 "capability=%d, rangelow=%d,"
1444 " rangehigh=%d, txsubchans=%d\n",
1445 p->index, p->name, p->capability,
1446 p->rangelow, p->rangehigh,
1447 p->txsubchans);
1448 break;
1449 }
1450 case VIDIOC_S_MODULATOR:
1451 {
1452 struct v4l2_modulator *p = arg;
1453
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001454 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1455 "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1456 p->index, p->name, p->capability, p->rangelow,
1457 p->rangehigh, p->txsubchans);
Hans Verkuila3998102008-07-21 02:57:38 -03001458 ret = ops->vidioc_s_modulator(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001459 break;
1460 }
1461 case VIDIOC_G_CROP:
1462 {
1463 struct v4l2_crop *p = arg;
1464
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001465 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Tomasz Stanislawski992efeff2011-03-22 14:04:47 -03001466
1467 if (ops->vidioc_g_crop) {
1468 ret = ops->vidioc_g_crop(file, fh, p);
1469 } else {
1470 /* simulate capture crop using selection api */
1471 struct v4l2_selection s = {
1472 .type = p->type,
1473 };
1474
1475 /* crop means compose for output devices */
1476 if (V4L2_TYPE_IS_OUTPUT(p->type))
1477 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1478 else
1479 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1480
1481 ret = ops->vidioc_g_selection(file, fh, &s);
1482
1483 /* copying results to old structure on success */
1484 if (!ret)
1485 p->c = s.r;
1486 }
1487
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001488 if (!ret)
1489 dbgrect(vfd, "", &p->c);
1490 break;
1491 }
1492 case VIDIOC_S_CROP:
1493 {
1494 struct v4l2_crop *p = arg;
1495
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001496 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1497 dbgrect(vfd, "", &p->c);
Tomasz Stanislawski992efeff2011-03-22 14:04:47 -03001498
1499 if (ops->vidioc_s_crop) {
1500 ret = ops->vidioc_s_crop(file, fh, p);
1501 } else {
1502 /* simulate capture crop using selection api */
1503 struct v4l2_selection s = {
1504 .type = p->type,
1505 .r = p->c,
1506 };
1507
1508 /* crop means compose for output devices */
1509 if (V4L2_TYPE_IS_OUTPUT(p->type))
1510 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1511 else
1512 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1513
1514 ret = ops->vidioc_s_selection(file, fh, &s);
1515 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001516 break;
1517 }
Tomasz Stanislawski0e8caac2011-08-10 10:37:47 -03001518 case VIDIOC_G_SELECTION:
1519 {
1520 struct v4l2_selection *p = arg;
1521
Tomasz Stanislawski0e8caac2011-08-10 10:37:47 -03001522 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1523
1524 ret = ops->vidioc_g_selection(file, fh, p);
1525 if (!ret)
1526 dbgrect(vfd, "", &p->r);
1527 break;
1528 }
1529 case VIDIOC_S_SELECTION:
1530 {
1531 struct v4l2_selection *p = arg;
1532
Tomasz Stanislawski0e8caac2011-08-10 10:37:47 -03001533
1534 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1535 dbgrect(vfd, "", &p->r);
1536
1537 ret = ops->vidioc_s_selection(file, fh, p);
1538 break;
1539 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001540 case VIDIOC_CROPCAP:
1541 {
1542 struct v4l2_cropcap *p = arg;
1543
1544 /*FIXME: Should also show v4l2_fract pixelaspect */
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001545 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Tomasz Stanislawski992efeff2011-03-22 14:04:47 -03001546 if (ops->vidioc_cropcap) {
1547 ret = ops->vidioc_cropcap(file, fh, p);
1548 } else {
1549 struct v4l2_selection s = { .type = p->type };
1550
1551 /* obtaining bounds */
1552 if (V4L2_TYPE_IS_OUTPUT(p->type))
1553 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1554 else
1555 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1556
1557 ret = ops->vidioc_g_selection(file, fh, &s);
1558 if (ret)
1559 break;
1560 p->bounds = s.r;
1561
1562 /* obtaining defrect */
1563 if (V4L2_TYPE_IS_OUTPUT(p->type))
1564 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1565 else
1566 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1567
1568 ret = ops->vidioc_g_selection(file, fh, &s);
1569 if (ret)
1570 break;
1571 p->defrect = s.r;
1572
1573 /* setting trivial pixelaspect */
1574 p->pixelaspect.numerator = 1;
1575 p->pixelaspect.denominator = 1;
1576 }
1577
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001578 if (!ret) {
1579 dbgrect(vfd, "bounds ", &p->bounds);
1580 dbgrect(vfd, "defrect ", &p->defrect);
1581 }
1582 break;
1583 }
1584 case VIDIOC_G_JPEGCOMP:
1585 {
1586 struct v4l2_jpegcompression *p = arg;
1587
Hans Verkuila3998102008-07-21 02:57:38 -03001588 ret = ops->vidioc_g_jpegcomp(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001589 if (!ret)
1590 dbgarg(cmd, "quality=%d, APPn=%d, "
1591 "APP_len=%d, COM_len=%d, "
1592 "jpeg_markers=%d\n",
1593 p->quality, p->APPn, p->APP_len,
1594 p->COM_len, p->jpeg_markers);
1595 break;
1596 }
1597 case VIDIOC_S_JPEGCOMP:
1598 {
1599 struct v4l2_jpegcompression *p = arg;
1600
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001601 dbgarg(cmd, "quality=%d, APPn=%d, APP_len=%d, "
1602 "COM_len=%d, jpeg_markers=%d\n",
1603 p->quality, p->APPn, p->APP_len,
1604 p->COM_len, p->jpeg_markers);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001605 ret = ops->vidioc_s_jpegcomp(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001606 break;
1607 }
1608 case VIDIOC_G_ENC_INDEX:
1609 {
1610 struct v4l2_enc_idx *p = arg;
1611
Hans Verkuila3998102008-07-21 02:57:38 -03001612 ret = ops->vidioc_g_enc_index(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001613 if (!ret)
1614 dbgarg(cmd, "entries=%d, entries_cap=%d\n",
1615 p->entries, p->entries_cap);
1616 break;
1617 }
1618 case VIDIOC_ENCODER_CMD:
1619 {
1620 struct v4l2_encoder_cmd *p = arg;
1621
Hans Verkuila3998102008-07-21 02:57:38 -03001622 ret = ops->vidioc_encoder_cmd(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001623 if (!ret)
1624 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1625 break;
1626 }
1627 case VIDIOC_TRY_ENCODER_CMD:
1628 {
1629 struct v4l2_encoder_cmd *p = arg;
1630
Hans Verkuila3998102008-07-21 02:57:38 -03001631 ret = ops->vidioc_try_encoder_cmd(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001632 if (!ret)
1633 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1634 break;
1635 }
Hans Verkuila45c0ad2011-11-24 09:53:43 -03001636 case VIDIOC_DECODER_CMD:
1637 {
1638 struct v4l2_decoder_cmd *p = arg;
1639
Hans Verkuila45c0ad2011-11-24 09:53:43 -03001640 ret = ops->vidioc_decoder_cmd(file, fh, p);
1641 if (!ret)
1642 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1643 break;
1644 }
1645 case VIDIOC_TRY_DECODER_CMD:
1646 {
1647 struct v4l2_decoder_cmd *p = arg;
1648
Hans Verkuila45c0ad2011-11-24 09:53:43 -03001649 ret = ops->vidioc_try_decoder_cmd(file, fh, p);
1650 if (!ret)
1651 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1652 break;
1653 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001654 case VIDIOC_G_PARM:
1655 {
1656 struct v4l2_streamparm *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001657
Hans Verkuila3998102008-07-21 02:57:38 -03001658 if (ops->vidioc_g_parm) {
Trent Piepho34796bc2009-03-28 22:25:35 -03001659 ret = check_fmt(ops, p->type);
1660 if (ret)
1661 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001662 ret = ops->vidioc_g_parm(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001663 } else {
Hans Verkuil9bedc7f2009-08-07 07:28:16 -03001664 v4l2_std_id std = vfd->current_norm;
1665
Hans Verkuil93d5a302011-08-09 09:32:06 -03001666 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001667 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Hans Verkuil3f5e1822010-04-06 08:14:11 -03001668 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001669
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001670 ret = 0;
Hans Verkuil0c142c82012-05-25 11:51:31 -03001671 p->parm.capture.readbuffers = 2;
Hans Verkuil9bedc7f2009-08-07 07:28:16 -03001672 if (ops->vidioc_g_std)
1673 ret = ops->vidioc_g_std(file, fh, &std);
Hans Verkuil9bedc7f2009-08-07 07:28:16 -03001674 if (ret == 0)
1675 v4l2_video_std_frame_period(std,
1676 &p->parm.capture.timeperframe);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001677 }
1678
1679 dbgarg(cmd, "type=%d\n", p->type);
1680 break;
1681 }
1682 case VIDIOC_S_PARM:
1683 {
1684 struct v4l2_streamparm *p = arg;
1685
Trent Piepho34796bc2009-03-28 22:25:35 -03001686 ret = check_fmt(ops, p->type);
1687 if (ret)
1688 break;
1689
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001690 dbgarg(cmd, "type=%d\n", p->type);
Hans Verkuila3998102008-07-21 02:57:38 -03001691 ret = ops->vidioc_s_parm(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001692 break;
1693 }
1694 case VIDIOC_G_TUNER:
1695 {
1696 struct v4l2_tuner *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001697
Hans Verkuil227690d2011-06-12 06:36:41 -03001698 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1699 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuila3998102008-07-21 02:57:38 -03001700 ret = ops->vidioc_g_tuner(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001701 if (!ret)
1702 dbgarg(cmd, "index=%d, name=%s, type=%d, "
1703 "capability=0x%x, rangelow=%d, "
1704 "rangehigh=%d, signal=%d, afc=%d, "
1705 "rxsubchans=0x%x, audmode=%d\n",
1706 p->index, p->name, p->type,
1707 p->capability, p->rangelow,
1708 p->rangehigh, p->signal, p->afc,
1709 p->rxsubchans, p->audmode);
1710 break;
1711 }
1712 case VIDIOC_S_TUNER:
1713 {
1714 struct v4l2_tuner *p = arg;
1715
Hans Verkuil227690d2011-06-12 06:36:41 -03001716 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1717 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001718 dbgarg(cmd, "index=%d, name=%s, type=%d, "
1719 "capability=0x%x, rangelow=%d, "
1720 "rangehigh=%d, signal=%d, afc=%d, "
1721 "rxsubchans=0x%x, audmode=%d\n",
1722 p->index, p->name, p->type,
1723 p->capability, p->rangelow,
1724 p->rangehigh, p->signal, p->afc,
1725 p->rxsubchans, p->audmode);
Hans Verkuila3998102008-07-21 02:57:38 -03001726 ret = ops->vidioc_s_tuner(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001727 break;
1728 }
1729 case VIDIOC_G_FREQUENCY:
1730 {
1731 struct v4l2_frequency *p = arg;
1732
Hans Verkuil227690d2011-06-12 06:36:41 -03001733 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1734 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuila3998102008-07-21 02:57:38 -03001735 ret = ops->vidioc_g_frequency(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001736 if (!ret)
1737 dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1738 p->tuner, p->type, p->frequency);
1739 break;
1740 }
1741 case VIDIOC_S_FREQUENCY:
1742 {
1743 struct v4l2_frequency *p = arg;
Hans Verkuilaa07eec2011-10-11 05:12:52 -03001744 enum v4l2_tuner_type type;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001745
Hans Verkuilaa07eec2011-10-11 05:12:52 -03001746 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1747 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001748 dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1749 p->tuner, p->type, p->frequency);
Hans Verkuilaa07eec2011-10-11 05:12:52 -03001750 if (p->type != type)
1751 ret = -EINVAL;
1752 else
1753 ret = ops->vidioc_s_frequency(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001754 break;
1755 }
1756 case VIDIOC_G_SLICED_VBI_CAP:
1757 {
1758 struct v4l2_sliced_vbi_cap *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001759
Trent Piepho19c96e42009-03-04 01:21:02 -03001760 /* Clear up to type, everything after type is zerod already */
1761 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1762
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001763 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Hans Verkuila3998102008-07-21 02:57:38 -03001764 ret = ops->vidioc_g_sliced_vbi_cap(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001765 if (!ret)
1766 dbgarg2("service_set=%d\n", p->service_set);
1767 break;
1768 }
1769 case VIDIOC_LOG_STATUS:
1770 {
Hans Verkuile2ecb252012-02-02 08:20:53 -03001771 if (vfd->v4l2_dev)
1772 pr_info("%s: ================= START STATUS =================\n",
1773 vfd->v4l2_dev->name);
Hans Verkuila3998102008-07-21 02:57:38 -03001774 ret = ops->vidioc_log_status(file, fh);
Hans Verkuile2ecb252012-02-02 08:20:53 -03001775 if (vfd->v4l2_dev)
1776 pr_info("%s: ================== END STATUS ==================\n",
1777 vfd->v4l2_dev->name);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001778 break;
1779 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001780 case VIDIOC_DBG_G_REGISTER:
1781 {
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001782#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001783 struct v4l2_dbg_register *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001784
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001785 if (!capable(CAP_SYS_ADMIN))
1786 ret = -EPERM;
1787 else
1788 ret = ops->vidioc_g_register(file, fh, p);
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001789#endif
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001790 break;
1791 }
1792 case VIDIOC_DBG_S_REGISTER:
1793 {
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001794#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001795 struct v4l2_dbg_register *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001796
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001797 if (!capable(CAP_SYS_ADMIN))
1798 ret = -EPERM;
1799 else
1800 ret = ops->vidioc_s_register(file, fh, p);
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001801#endif
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001802 break;
1803 }
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001804 case VIDIOC_DBG_G_CHIP_IDENT:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001805 {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001806 struct v4l2_dbg_chip_ident *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001807
Hans Verkuil80b36e02009-02-07 11:00:02 -03001808 p->ident = V4L2_IDENT_NONE;
1809 p->revision = 0;
Hans Verkuila3998102008-07-21 02:57:38 -03001810 ret = ops->vidioc_g_chip_ident(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001811 if (!ret)
1812 dbgarg(cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
1813 break;
1814 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001815 case VIDIOC_S_HW_FREQ_SEEK:
1816 {
1817 struct v4l2_hw_freq_seek *p = arg;
Hans Verkuila6cf90a2011-06-14 04:03:26 -03001818 enum v4l2_tuner_type type;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001819
Hans Verkuila6cf90a2011-06-14 04:03:26 -03001820 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1821 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001822 dbgarg(cmd,
Hans Verkuila6cf90a2011-06-14 04:03:26 -03001823 "tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u\n",
1824 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing);
1825 if (p->type != type)
1826 ret = -EINVAL;
1827 else
1828 ret = ops->vidioc_s_hw_freq_seek(file, fh, p);
Hans Verkuila3998102008-07-21 02:57:38 -03001829 break;
1830 }
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001831 case VIDIOC_ENUM_FRAMESIZES:
1832 {
1833 struct v4l2_frmsizeenum *p = arg;
1834
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001835 ret = ops->vidioc_enum_framesizes(file, fh, p);
1836 dbgarg(cmd,
Mauro Carvalho Chehabd1afe422009-06-21 22:37:12 -03001837 "index=%d, pixelformat=%c%c%c%c, type=%d ",
1838 p->index,
1839 (p->pixel_format & 0xff),
1840 (p->pixel_format >> 8) & 0xff,
1841 (p->pixel_format >> 16) & 0xff,
1842 (p->pixel_format >> 24) & 0xff,
1843 p->type);
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001844 switch (p->type) {
1845 case V4L2_FRMSIZE_TYPE_DISCRETE:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001846 dbgarg3("width = %d, height=%d\n",
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001847 p->discrete.width, p->discrete.height);
1848 break;
1849 case V4L2_FRMSIZE_TYPE_STEPWISE:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001850 dbgarg3("min %dx%d, max %dx%d, step %dx%d\n",
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001851 p->stepwise.min_width, p->stepwise.min_height,
1852 p->stepwise.step_width, p->stepwise.step_height,
1853 p->stepwise.max_width, p->stepwise.max_height);
1854 break;
1855 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001856 dbgarg3("continuous\n");
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001857 break;
1858 default:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001859 dbgarg3("- Unknown type!\n");
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001860 }
1861
1862 break;
1863 }
1864 case VIDIOC_ENUM_FRAMEINTERVALS:
1865 {
1866 struct v4l2_frmivalenum *p = arg;
1867
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001868 ret = ops->vidioc_enum_frameintervals(file, fh, p);
1869 dbgarg(cmd,
1870 "index=%d, pixelformat=%d, width=%d, height=%d, type=%d ",
1871 p->index, p->pixel_format,
1872 p->width, p->height, p->type);
1873 switch (p->type) {
1874 case V4L2_FRMIVAL_TYPE_DISCRETE:
1875 dbgarg2("fps=%d/%d\n",
1876 p->discrete.numerator,
1877 p->discrete.denominator);
1878 break;
1879 case V4L2_FRMIVAL_TYPE_STEPWISE:
Mauro Carvalho Chehab19585782008-11-12 01:03:02 -03001880 dbgarg2("min=%d/%d, max=%d/%d, step=%d/%d\n",
1881 p->stepwise.min.numerator,
1882 p->stepwise.min.denominator,
1883 p->stepwise.max.numerator,
1884 p->stepwise.max.denominator,
1885 p->stepwise.step.numerator,
1886 p->stepwise.step.denominator);
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001887 break;
1888 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1889 dbgarg2("continuous\n");
1890 break;
1891 default:
1892 dbgarg2("- Unknown type!\n");
1893 }
1894 break;
1895 }
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001896 case VIDIOC_ENUM_DV_PRESETS:
1897 {
1898 struct v4l2_dv_enum_preset *p = arg;
1899
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001900 ret = ops->vidioc_enum_dv_presets(file, fh, p);
1901 if (!ret)
1902 dbgarg(cmd,
1903 "index=%d, preset=%d, name=%s, width=%d,"
1904 " height=%d ",
1905 p->index, p->preset, p->name, p->width,
1906 p->height);
1907 break;
1908 }
1909 case VIDIOC_S_DV_PRESET:
1910 {
1911 struct v4l2_dv_preset *p = arg;
1912
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001913 dbgarg(cmd, "preset=%d\n", p->preset);
1914 ret = ops->vidioc_s_dv_preset(file, fh, p);
1915 break;
1916 }
1917 case VIDIOC_G_DV_PRESET:
1918 {
1919 struct v4l2_dv_preset *p = arg;
1920
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001921 ret = ops->vidioc_g_dv_preset(file, fh, p);
1922 if (!ret)
1923 dbgarg(cmd, "preset=%d\n", p->preset);
1924 break;
1925 }
1926 case VIDIOC_QUERY_DV_PRESET:
1927 {
1928 struct v4l2_dv_preset *p = arg;
1929
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001930 ret = ops->vidioc_query_dv_preset(file, fh, p);
1931 if (!ret)
1932 dbgarg(cmd, "preset=%d\n", p->preset);
1933 break;
1934 }
1935 case VIDIOC_S_DV_TIMINGS:
1936 {
1937 struct v4l2_dv_timings *p = arg;
1938
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001939 dbgtimings(vfd, p);
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001940 switch (p->type) {
1941 case V4L2_DV_BT_656_1120:
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001942 ret = ops->vidioc_s_dv_timings(file, fh, p);
1943 break;
1944 default:
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001945 ret = -EINVAL;
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001946 break;
1947 }
1948 break;
1949 }
1950 case VIDIOC_G_DV_TIMINGS:
1951 {
1952 struct v4l2_dv_timings *p = arg;
1953
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001954 ret = ops->vidioc_g_dv_timings(file, fh, p);
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001955 if (!ret)
1956 dbgtimings(vfd, p);
1957 break;
1958 }
1959 case VIDIOC_ENUM_DV_TIMINGS:
1960 {
1961 struct v4l2_enum_dv_timings *p = arg;
1962
1963 if (!ops->vidioc_enum_dv_timings)
1964 break;
1965
1966 ret = ops->vidioc_enum_dv_timings(file, fh, p);
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001967 if (!ret) {
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001968 dbgarg(cmd, "index=%d: ", p->index);
1969 dbgtimings(vfd, &p->timings);
1970 }
1971 break;
1972 }
1973 case VIDIOC_QUERY_DV_TIMINGS:
1974 {
1975 struct v4l2_dv_timings *p = arg;
1976
1977 if (!ops->vidioc_query_dv_timings)
1978 break;
1979
1980 ret = ops->vidioc_query_dv_timings(file, fh, p);
1981 if (!ret)
1982 dbgtimings(vfd, p);
1983 break;
1984 }
1985 case VIDIOC_DV_TIMINGS_CAP:
1986 {
1987 struct v4l2_dv_timings_cap *p = arg;
1988
1989 if (!ops->vidioc_dv_timings_cap)
1990 break;
1991
1992 ret = ops->vidioc_dv_timings_cap(file, fh, p);
1993 if (ret)
1994 break;
1995 switch (p->type) {
1996 case V4L2_DV_BT_656_1120:
1997 dbgarg(cmd,
1998 "type=%d, width=%u-%u, height=%u-%u, "
1999 "pixelclock=%llu-%llu, standards=%x, capabilities=%x ",
2000 p->type,
2001 p->bt.min_width, p->bt.max_width,
2002 p->bt.min_height, p->bt.max_height,
2003 p->bt.min_pixelclock, p->bt.max_pixelclock,
2004 p->bt.standards, p->bt.capabilities);
2005 break;
2006 default:
2007 dbgarg(cmd, "unknown type ");
2008 break;
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03002009 }
2010 break;
2011 }
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002012 case VIDIOC_DQEVENT:
2013 {
2014 struct v4l2_event *ev = arg;
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03002015
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002016 ret = v4l2_event_dequeue(fh, ev, file->f_flags & O_NONBLOCK);
2017 if (ret < 0) {
2018 dbgarg(cmd, "no pending events?");
2019 break;
2020 }
2021 dbgarg(cmd,
2022 "pending=%d, type=0x%8.8x, sequence=%d, "
2023 "timestamp=%lu.%9.9lu ",
2024 ev->pending, ev->type, ev->sequence,
2025 ev->timestamp.tv_sec, ev->timestamp.tv_nsec);
2026 break;
2027 }
2028 case VIDIOC_SUBSCRIBE_EVENT:
2029 {
2030 struct v4l2_event_subscription *sub = arg;
2031
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002032 ret = ops->vidioc_subscribe_event(fh, sub);
2033 if (ret < 0) {
2034 dbgarg(cmd, "failed, ret=%ld", ret);
2035 break;
2036 }
2037 dbgarg(cmd, "type=0x%8.8x", sub->type);
2038 break;
2039 }
2040 case VIDIOC_UNSUBSCRIBE_EVENT:
2041 {
2042 struct v4l2_event_subscription *sub = arg;
2043
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002044 ret = ops->vidioc_unsubscribe_event(fh, sub);
2045 if (ret < 0) {
2046 dbgarg(cmd, "failed, ret=%ld", ret);
2047 break;
2048 }
2049 dbgarg(cmd, "type=0x%8.8x", sub->type);
2050 break;
2051 }
Guennadi Liakhovetski21501582011-09-28 11:34:06 -03002052 case VIDIOC_CREATE_BUFS:
2053 {
2054 struct v4l2_create_buffers *create = arg;
2055
Guennadi Liakhovetski21501582011-09-28 11:34:06 -03002056 ret = check_fmt(ops, create->format.type);
2057 if (ret)
2058 break;
2059
2060 ret = ops->vidioc_create_bufs(file, fh, create);
2061
2062 dbgarg(cmd, "count=%d @ %d\n", create->count, create->index);
2063 break;
2064 }
2065 case VIDIOC_PREPARE_BUF:
2066 {
2067 struct v4l2_buffer *b = arg;
2068
Guennadi Liakhovetski21501582011-09-28 11:34:06 -03002069 ret = check_fmt(ops, b->type);
2070 if (ret)
2071 break;
2072
2073 ret = ops->vidioc_prepare_buf(file, fh, b);
2074
2075 dbgarg(cmd, "index=%d", b->index);
2076 break;
2077 }
Hans Verkuila3998102008-07-21 02:57:38 -03002078 default:
Hans Verkuila3998102008-07-21 02:57:38 -03002079 if (!ops->vidioc_default)
2080 break;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002081 ret = ops->vidioc_default(file, fh, use_fh_prio ?
2082 v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2083 cmd, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002084 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002085 } /* switch */
2086
Hans Verkuil86748f32012-06-22 06:04:16 -03002087done:
2088 if (vfd->debug) {
2089 if (write_only && vfd->debug > V4L2_DEBUG_IOCTL) {
2090 if (ret < 0)
2091 printk(KERN_DEBUG "%s: error %ld\n",
2092 video_device_node_name(vfd), ret);
2093 return ret;
2094 }
2095 v4l_print_ioctl(vfd->name, cmd);
2096 if (ret < 0)
2097 pr_cont(": error %ld\n", ret);
2098 else if (vfd->debug == V4L2_DEBUG_IOCTL)
2099 pr_cont("\n");
2100 else if (!info->debug)
2101 return ret;
2102 else if (_IOC_DIR(cmd) == _IOC_NONE)
2103 info->debug(arg, write_only);
2104 else {
2105 pr_cont(": ");
2106 info->debug(arg, write_only);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002107 }
2108 }
2109
2110 return ret;
2111}
2112
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002113static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2114 void * __user *user_ptr, void ***kernel_ptr)
2115{
2116 int ret = 0;
2117
2118 switch (cmd) {
2119 case VIDIOC_QUERYBUF:
2120 case VIDIOC_QBUF:
2121 case VIDIOC_DQBUF: {
2122 struct v4l2_buffer *buf = parg;
2123
2124 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2125 if (buf->length > VIDEO_MAX_PLANES) {
2126 ret = -EINVAL;
2127 break;
2128 }
2129 *user_ptr = (void __user *)buf->m.planes;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002130 *kernel_ptr = (void *)&buf->m.planes;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002131 *array_size = sizeof(struct v4l2_plane) * buf->length;
2132 ret = 1;
2133 }
2134 break;
2135 }
2136
2137 case VIDIOC_S_EXT_CTRLS:
2138 case VIDIOC_G_EXT_CTRLS:
2139 case VIDIOC_TRY_EXT_CTRLS: {
2140 struct v4l2_ext_controls *ctrls = parg;
2141
2142 if (ctrls->count != 0) {
Dan Carpenter6c061082012-01-05 02:27:57 -03002143 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2144 ret = -EINVAL;
2145 break;
2146 }
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002147 *user_ptr = (void __user *)ctrls->controls;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002148 *kernel_ptr = (void *)&ctrls->controls;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002149 *array_size = sizeof(struct v4l2_ext_control)
2150 * ctrls->count;
2151 ret = 1;
2152 }
2153 break;
2154 }
2155 }
2156
2157 return ret;
2158}
2159
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002160long
2161video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2162 v4l2_kioctl func)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002163{
2164 char sbuf[128];
2165 void *mbuf = NULL;
Hans Verkuil1d94aa32010-04-06 08:12:21 -03002166 void *parg = (void *)arg;
Hans Verkuil069b7472008-12-30 07:04:34 -03002167 long err = -EINVAL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002168 bool has_array_args;
2169 size_t array_size = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002170 void __user *user_ptr = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002171 void **kernel_ptr = NULL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002172
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002173 /* Copy arguments into temp kernel buffer */
Trent Piepho337f9d22009-03-04 01:21:02 -03002174 if (_IOC_DIR(cmd) != _IOC_NONE) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002175 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2176 parg = sbuf;
2177 } else {
2178 /* too big to allocate from stack */
2179 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2180 if (NULL == mbuf)
2181 return -ENOMEM;
2182 parg = mbuf;
2183 }
2184
2185 err = -EFAULT;
Trent Piepho19c96e42009-03-04 01:21:02 -03002186 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002187 unsigned int n = _IOC_SIZE(cmd);
2188
2189 /*
2190 * In some cases, only a few fields are used as input,
2191 * i.e. when the app sets "index" and then the driver
2192 * fills in the rest of the structure for the thing
2193 * with that index. We only need to copy up the first
2194 * non-input field.
2195 */
2196 if (v4l2_is_known_ioctl(cmd)) {
2197 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2198 if (flags & INFO_FL_CLEAR_MASK)
2199 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2200 }
Trent Piepho19c96e42009-03-04 01:21:02 -03002201
2202 if (copy_from_user(parg, (void __user *)arg, n))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002203 goto out;
Trent Piepho19c96e42009-03-04 01:21:02 -03002204
2205 /* zero out anything we don't copy from userspace */
2206 if (n < _IOC_SIZE(cmd))
2207 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
Trent Piepho337f9d22009-03-04 01:21:02 -03002208 } else {
2209 /* read-only ioctl */
2210 memset(parg, 0, _IOC_SIZE(cmd));
Trent Piepho19c96e42009-03-04 01:21:02 -03002211 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002212 }
2213
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002214 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2215 if (err < 0)
2216 goto out;
2217 has_array_args = err;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002218
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002219 if (has_array_args) {
2220 /*
2221 * When adding new types of array args, make sure that the
2222 * parent argument to ioctl (which contains the pointer to the
2223 * array) fits into sbuf (so that mbuf will still remain
2224 * unused up to here).
2225 */
2226 mbuf = kmalloc(array_size, GFP_KERNEL);
2227 err = -ENOMEM;
2228 if (NULL == mbuf)
2229 goto out_array_args;
2230 err = -EFAULT;
2231 if (copy_from_user(mbuf, user_ptr, array_size))
2232 goto out_array_args;
2233 *kernel_ptr = mbuf;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002234 }
2235
2236 /* Handles IOCTL */
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002237 err = func(file, cmd, parg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002238 if (err == -ENOIOCTLCMD)
Hans Verkuil02bbb812012-02-08 08:09:36 -03002239 err = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002240
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002241 if (has_array_args) {
2242 *kernel_ptr = user_ptr;
2243 if (copy_to_user(user_ptr, mbuf, array_size))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002244 err = -EFAULT;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002245 goto out_array_args;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002246 }
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002247 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2248 results that must be returned. */
2249 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002250 goto out;
2251
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002252out_array_args:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002253 /* Copy results into user buffer */
2254 switch (_IOC_DIR(cmd)) {
2255 case _IOC_READ:
2256 case (_IOC_WRITE | _IOC_READ):
2257 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2258 err = -EFAULT;
2259 break;
2260 }
2261
2262out:
2263 kfree(mbuf);
2264 return err;
2265}
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002266EXPORT_SYMBOL(video_usercopy);
2267
2268long video_ioctl2(struct file *file,
2269 unsigned int cmd, unsigned long arg)
2270{
2271 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2272}
Mauro Carvalho Chehab8a522c92008-10-21 11:58:39 -03002273EXPORT_SYMBOL(video_ioctl2);