blob: 7556678846f90e1006556235c70c22f8e658d1f4 [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
473static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
474 struct file *file, void *fh, void *arg)
475{
476 struct v4l2_input *p = arg;
477
478 /*
479 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
480 * CAP_STD here based on ioctl handler provided by the
481 * driver. If the driver doesn't support these
482 * for a specific input, it must override these flags.
483 */
484 if (ops->vidioc_s_std)
485 p->capabilities |= V4L2_IN_CAP_STD;
486 if (ops->vidioc_s_dv_preset)
487 p->capabilities |= V4L2_IN_CAP_PRESETS;
488 if (ops->vidioc_s_dv_timings)
489 p->capabilities |= V4L2_IN_CAP_CUSTOM_TIMINGS;
490
491 return ops->vidioc_enum_input(file, fh, p);
492}
493
494static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
495 struct file *file, void *fh, void *arg)
496{
497 struct v4l2_output *p = arg;
498
499 /*
500 * We set the flags for CAP_PRESETS, CAP_CUSTOM_TIMINGS &
501 * CAP_STD here based on ioctl handler provided by the
502 * driver. If the driver doesn't support these
503 * for a specific output, it must override these flags.
504 */
505 if (ops->vidioc_s_std)
506 p->capabilities |= V4L2_OUT_CAP_STD;
507 if (ops->vidioc_s_dv_preset)
508 p->capabilities |= V4L2_OUT_CAP_PRESETS;
509 if (ops->vidioc_s_dv_timings)
510 p->capabilities |= V4L2_OUT_CAP_CUSTOM_TIMINGS;
511
512 return ops->vidioc_enum_output(file, fh, p);
513}
514
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300515struct v4l2_ioctl_info {
516 unsigned int ioctl;
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300517 u32 flags;
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300518 const char * const name;
Hans Verkuil86748f32012-06-22 06:04:16 -0300519 union {
520 u32 offset;
521 int (*func)(const struct v4l2_ioctl_ops *ops,
522 struct file *file, void *fh, void *p);
523 };
524 void (*debug)(const void *arg, bool write_only);
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300525};
526
527/* This control needs a priority check */
528#define INFO_FL_PRIO (1 << 0)
529/* This control can be valid if the filehandle passes a control handler. */
530#define INFO_FL_CTRL (1 << 1)
Hans Verkuil86748f32012-06-22 06:04:16 -0300531/* This is a standard ioctl, no need for special code */
532#define INFO_FL_STD (1 << 2)
533/* This is ioctl has its own function */
534#define INFO_FL_FUNC (1 << 3)
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300535/* Zero struct from after the field to the end */
536#define INFO_FL_CLEAR(v4l2_struct, field) \
537 ((offsetof(struct v4l2_struct, field) + \
538 sizeof(((struct v4l2_struct *)0)->field)) << 16)
539#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300540
541#define IOCTL_INFO(_ioctl, _flags) [_IOC_NR(_ioctl)] = { \
542 .ioctl = _ioctl, \
543 .flags = _flags, \
544 .name = #_ioctl, \
545}
546
Hans Verkuil86748f32012-06-22 06:04:16 -0300547#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
548 [_IOC_NR(_ioctl)] = { \
549 .ioctl = _ioctl, \
550 .flags = _flags | INFO_FL_STD, \
551 .name = #_ioctl, \
552 .offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
553 .debug = _debug, \
554 }
555
556#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
557 [_IOC_NR(_ioctl)] = { \
558 .ioctl = _ioctl, \
559 .flags = _flags | INFO_FL_FUNC, \
560 .name = #_ioctl, \
561 .func = _func, \
562 .debug = _debug, \
563 }
564
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300565static struct v4l2_ioctl_info v4l2_ioctls[] = {
Hans Verkuil59076122012-06-22 06:09:54 -0300566 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300567 IOCTL_INFO(VIDIOC_ENUM_FMT, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
568 IOCTL_INFO(VIDIOC_G_FMT, INFO_FL_CLEAR(v4l2_format, type)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300569 IOCTL_INFO(VIDIOC_S_FMT, INFO_FL_PRIO),
570 IOCTL_INFO(VIDIOC_REQBUFS, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300571 IOCTL_INFO(VIDIOC_QUERYBUF, INFO_FL_CLEAR(v4l2_buffer, length)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300572 IOCTL_INFO(VIDIOC_G_FBUF, 0),
573 IOCTL_INFO(VIDIOC_S_FBUF, INFO_FL_PRIO),
574 IOCTL_INFO(VIDIOC_OVERLAY, INFO_FL_PRIO),
575 IOCTL_INFO(VIDIOC_QBUF, 0),
576 IOCTL_INFO(VIDIOC_DQBUF, 0),
577 IOCTL_INFO(VIDIOC_STREAMON, INFO_FL_PRIO),
578 IOCTL_INFO(VIDIOC_STREAMOFF, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300579 IOCTL_INFO(VIDIOC_G_PARM, INFO_FL_CLEAR(v4l2_streamparm, type)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300580 IOCTL_INFO(VIDIOC_S_PARM, INFO_FL_PRIO),
581 IOCTL_INFO(VIDIOC_G_STD, 0),
582 IOCTL_INFO(VIDIOC_S_STD, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300583 IOCTL_INFO(VIDIOC_ENUMSTD, INFO_FL_CLEAR(v4l2_standard, index)),
Hans Verkuil59076122012-06-22 06:09:54 -0300584 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300585 IOCTL_INFO(VIDIOC_G_CTRL, INFO_FL_CTRL),
586 IOCTL_INFO(VIDIOC_S_CTRL, INFO_FL_PRIO | INFO_FL_CTRL),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300587 IOCTL_INFO(VIDIOC_G_TUNER, INFO_FL_CLEAR(v4l2_tuner, index)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300588 IOCTL_INFO(VIDIOC_S_TUNER, INFO_FL_PRIO),
Hans Verkuil59076122012-06-22 06:09:54 -0300589 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
590 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300591 IOCTL_INFO(VIDIOC_QUERYCTRL, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
592 IOCTL_INFO(VIDIOC_QUERYMENU, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
Hans Verkuil59076122012-06-22 06:09:54 -0300593 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
594 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
595 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
596 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
597 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
598 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
599 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300600 IOCTL_INFO(VIDIOC_G_MODULATOR, INFO_FL_CLEAR(v4l2_modulator, index)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300601 IOCTL_INFO(VIDIOC_S_MODULATOR, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300602 IOCTL_INFO(VIDIOC_G_FREQUENCY, INFO_FL_CLEAR(v4l2_frequency, tuner)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300603 IOCTL_INFO(VIDIOC_S_FREQUENCY, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300604 IOCTL_INFO(VIDIOC_CROPCAP, INFO_FL_CLEAR(v4l2_cropcap, type)),
605 IOCTL_INFO(VIDIOC_G_CROP, INFO_FL_CLEAR(v4l2_crop, type)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300606 IOCTL_INFO(VIDIOC_S_CROP, INFO_FL_PRIO),
607 IOCTL_INFO(VIDIOC_G_SELECTION, 0),
608 IOCTL_INFO(VIDIOC_S_SELECTION, INFO_FL_PRIO),
609 IOCTL_INFO(VIDIOC_G_JPEGCOMP, 0),
610 IOCTL_INFO(VIDIOC_S_JPEGCOMP, INFO_FL_PRIO),
611 IOCTL_INFO(VIDIOC_QUERYSTD, 0),
612 IOCTL_INFO(VIDIOC_TRY_FMT, 0),
Hans Verkuil59076122012-06-22 06:09:54 -0300613 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
614 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300615 IOCTL_INFO(VIDIOC_G_PRIORITY, 0),
616 IOCTL_INFO(VIDIOC_S_PRIORITY, INFO_FL_PRIO),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300617 IOCTL_INFO(VIDIOC_G_SLICED_VBI_CAP, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300618 IOCTL_INFO(VIDIOC_LOG_STATUS, 0),
619 IOCTL_INFO(VIDIOC_G_EXT_CTRLS, INFO_FL_CTRL),
620 IOCTL_INFO(VIDIOC_S_EXT_CTRLS, INFO_FL_PRIO | INFO_FL_CTRL),
621 IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, 0),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300622 IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
623 IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300624 IOCTL_INFO(VIDIOC_G_ENC_INDEX, 0),
Hans Verkuil27cd2ab2012-06-09 08:55:31 -0300625 IOCTL_INFO(VIDIOC_ENCODER_CMD, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
626 IOCTL_INFO(VIDIOC_TRY_ENCODER_CMD, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300627 IOCTL_INFO(VIDIOC_DECODER_CMD, INFO_FL_PRIO),
628 IOCTL_INFO(VIDIOC_TRY_DECODER_CMD, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300629 IOCTL_INFO(VIDIOC_DBG_S_REGISTER, 0),
630 IOCTL_INFO(VIDIOC_DBG_G_REGISTER, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300631 IOCTL_INFO(VIDIOC_DBG_G_CHIP_IDENT, 0),
632 IOCTL_INFO(VIDIOC_S_HW_FREQ_SEEK, INFO_FL_PRIO),
633 IOCTL_INFO(VIDIOC_ENUM_DV_PRESETS, 0),
634 IOCTL_INFO(VIDIOC_S_DV_PRESET, INFO_FL_PRIO),
635 IOCTL_INFO(VIDIOC_G_DV_PRESET, 0),
636 IOCTL_INFO(VIDIOC_QUERY_DV_PRESET, 0),
637 IOCTL_INFO(VIDIOC_S_DV_TIMINGS, INFO_FL_PRIO),
638 IOCTL_INFO(VIDIOC_G_DV_TIMINGS, 0),
639 IOCTL_INFO(VIDIOC_DQEVENT, 0),
640 IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, 0),
641 IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, 0),
642 IOCTL_INFO(VIDIOC_CREATE_BUFS, INFO_FL_PRIO),
643 IOCTL_INFO(VIDIOC_PREPARE_BUF, 0),
644 IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, 0),
645 IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, 0),
646 IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, 0),
647};
648#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
649
650bool v4l2_is_known_ioctl(unsigned int cmd)
651{
652 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
653 return false;
654 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
655}
656
657/* Common ioctl debug function. This function can be used by
658 external ioctl messages as well as internal V4L ioctl */
659void v4l_printk_ioctl(unsigned int cmd)
660{
Hans Verkuil86748f32012-06-22 06:04:16 -0300661 const char *dir, *type;
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300662
663 switch (_IOC_TYPE(cmd)) {
664 case 'd':
665 type = "v4l2_int";
666 break;
667 case 'V':
668 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
669 type = "v4l2";
670 break;
671 }
Hans Verkuil86748f32012-06-22 06:04:16 -0300672 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300673 return;
674 default:
675 type = "unknown";
Hans Verkuil86748f32012-06-22 06:04:16 -0300676 break;
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300677 }
678
679 switch (_IOC_DIR(cmd)) {
680 case _IOC_NONE: dir = "--"; break;
681 case _IOC_READ: dir = "r-"; break;
682 case _IOC_WRITE: dir = "-w"; break;
683 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
684 default: dir = "*ERR*"; break;
685 }
Hans Verkuil86748f32012-06-22 06:04:16 -0300686 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
Hans Verkuil4839e6c2012-06-04 05:23:40 -0300687 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
688}
689EXPORT_SYMBOL(v4l_printk_ioctl);
690
Hans Verkuil069b7472008-12-30 07:04:34 -0300691static long __video_do_ioctl(struct file *file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300692 unsigned int cmd, void *arg)
693{
694 struct video_device *vfd = video_devdata(file);
Hans Verkuila3998102008-07-21 02:57:38 -0300695 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
Hans Verkuil86748f32012-06-22 06:04:16 -0300696 bool write_only = false;
697 struct v4l2_ioctl_info default_info;
698 const struct v4l2_ioctl_info *info;
Hans Verkuild5fbf322008-10-18 13:39:53 -0300699 void *fh = file->private_data;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300700 struct v4l2_fh *vfh = NULL;
Hans Verkuilb1a873a2011-03-22 10:14:07 -0300701 int use_fh_prio = 0;
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -0300702 long ret = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300703
Hans Verkuil6a717882010-04-06 15:56:08 -0300704 if (ops == NULL) {
705 printk(KERN_WARNING "videodev: \"%s\" has no ioctl_ops.\n",
706 vfd->name);
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -0300707 return ret;
Hans Verkuil6a717882010-04-06 15:56:08 -0300708 }
709
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300710 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
711 vfh = file->private_data;
712 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300713 }
714
715 if (v4l2_is_known_ioctl(cmd)) {
Hans Verkuil86748f32012-06-22 06:04:16 -0300716 info = &v4l2_ioctls[_IOC_NR(cmd)];
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300717
718 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
719 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
Hans Verkuil86748f32012-06-22 06:04:16 -0300720 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -0300721
722 if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
723 ret = v4l2_prio_check(vfd->prio, vfh->prio);
724 if (ret)
Hans Verkuil86748f32012-06-22 06:04:16 -0300725 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -0300726 }
Hans Verkuil86748f32012-06-22 06:04:16 -0300727 } else {
728 default_info.ioctl = cmd;
729 default_info.flags = 0;
730 default_info.debug = NULL;
731 info = &default_info;
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300732 }
733
Hans Verkuil86748f32012-06-22 06:04:16 -0300734 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
735 if (info->debug && write_only && vfd->debug > V4L2_DEBUG_IOCTL) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300736 v4l_print_ioctl(vfd->name, cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -0300737 pr_cont(": ");
738 info->debug(arg, write_only);
739 }
740 if (info->flags & INFO_FL_STD) {
741 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
742 const void *p = vfd->ioctl_ops;
743 const vidioc_op *vidioc = p + info->offset;
744
745 ret = (*vidioc)(file, fh, arg);
746 goto done;
747 } else if (info->flags & INFO_FL_FUNC) {
748 ret = info->func(ops, file, fh, arg);
749 goto done;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300750 }
751
Hans Verkuil6a717882010-04-06 15:56:08 -0300752 switch (cmd) {
Hans Verkuila3998102008-07-21 02:57:38 -0300753
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300754 /* --- priority ------------------------------------------ */
755 case VIDIOC_G_PRIORITY:
756 {
757 enum v4l2_priority *p = arg;
758
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300759 if (ops->vidioc_g_priority) {
760 ret = ops->vidioc_g_priority(file, fh, p);
Hans Verkuilb1a873a2011-03-22 10:14:07 -0300761 } else if (use_fh_prio) {
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300762 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
763 ret = 0;
764 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300765 if (!ret)
766 dbgarg(cmd, "priority is %d\n", *p);
767 break;
768 }
769 case VIDIOC_S_PRIORITY:
770 {
771 enum v4l2_priority *p = arg;
772
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300773 dbgarg(cmd, "setting priority to %d\n", *p);
Hans Verkuil99cd47bc2011-03-11 19:00:56 -0300774 if (ops->vidioc_s_priority)
775 ret = ops->vidioc_s_priority(file, fh, *p);
776 else
Hans Verkuil4b902fe2012-05-10 05:40:50 -0300777 ret = v4l2_prio_change(&vfd->v4l2_dev->prio,
Hans Verkuil93d5a302011-08-09 09:32:06 -0300778 &vfh->prio, *p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300779 break;
780 }
781
782 /* --- capture ioctls ---------------------------------------- */
783 case VIDIOC_ENUM_FMT:
784 {
785 struct v4l2_fmtdesc *f = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300786
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300787 ret = -EINVAL;
Trent Piepho19c96e42009-03-04 01:21:02 -0300788 switch (f->type) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300789 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300790 if (likely(ops->vidioc_enum_fmt_vid_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300791 ret = ops->vidioc_enum_fmt_vid_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300792 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300793 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300794 if (likely(ops->vidioc_enum_fmt_vid_cap_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300795 ret = ops->vidioc_enum_fmt_vid_cap_mplane(file,
796 fh, f);
797 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300798 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300799 if (likely(ops->vidioc_enum_fmt_vid_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -0300800 ret = ops->vidioc_enum_fmt_vid_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300801 fh, f);
802 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300803 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300804 if (likely(ops->vidioc_enum_fmt_vid_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300805 ret = ops->vidioc_enum_fmt_vid_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300806 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300807 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300808 if (likely(ops->vidioc_enum_fmt_vid_out_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300809 ret = ops->vidioc_enum_fmt_vid_out_mplane(file,
810 fh, f);
811 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300812 case V4L2_BUF_TYPE_PRIVATE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300813 if (likely(ops->vidioc_enum_fmt_type_private))
Hans Verkuila3998102008-07-21 02:57:38 -0300814 ret = ops->vidioc_enum_fmt_type_private(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300815 fh, f);
816 break;
817 default:
818 break;
819 }
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300820 if (likely(!ret))
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300821 dbgarg(cmd, "index=%d, type=%d, flags=%d, "
822 "pixelformat=%c%c%c%c, description='%s'\n",
823 f->index, f->type, f->flags,
824 (f->pixelformat & 0xff),
825 (f->pixelformat >> 8) & 0xff,
826 (f->pixelformat >> 16) & 0xff,
827 (f->pixelformat >> 24) & 0xff,
828 f->description);
829 break;
830 }
831 case VIDIOC_G_FMT:
832 {
833 struct v4l2_format *f = (struct v4l2_format *)arg;
834
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300835 /* FIXME: Should be one dump per type */
836 dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
837
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300838 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300839 switch (f->type) {
840 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300841 if (ops->vidioc_g_fmt_vid_cap)
Hans Verkuila3998102008-07-21 02:57:38 -0300842 ret = ops->vidioc_g_fmt_vid_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300843 if (!ret)
844 v4l_print_pix_fmt(vfd, &f->fmt.pix);
845 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300846 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300847 if (ops->vidioc_g_fmt_vid_cap_mplane)
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300848 ret = ops->vidioc_g_fmt_vid_cap_mplane(file,
849 fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300850 if (!ret)
851 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
852 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300853 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300854 if (likely(ops->vidioc_g_fmt_vid_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -0300855 ret = ops->vidioc_g_fmt_vid_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300856 fh, f);
857 break;
858 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300859 if (ops->vidioc_g_fmt_vid_out)
Hans Verkuila3998102008-07-21 02:57:38 -0300860 ret = ops->vidioc_g_fmt_vid_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300861 if (!ret)
862 v4l_print_pix_fmt(vfd, &f->fmt.pix);
863 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300864 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300865 if (ops->vidioc_g_fmt_vid_out_mplane)
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300866 ret = ops->vidioc_g_fmt_vid_out_mplane(file,
867 fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300868 if (!ret)
869 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
870 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300871 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300872 if (likely(ops->vidioc_g_fmt_vid_out_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -0300873 ret = ops->vidioc_g_fmt_vid_out_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300874 fh, f);
875 break;
876 case V4L2_BUF_TYPE_VBI_CAPTURE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300877 if (likely(ops->vidioc_g_fmt_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300878 ret = ops->vidioc_g_fmt_vbi_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300879 break;
880 case V4L2_BUF_TYPE_VBI_OUTPUT:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300881 if (likely(ops->vidioc_g_fmt_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300882 ret = ops->vidioc_g_fmt_vbi_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300883 break;
884 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300885 if (likely(ops->vidioc_g_fmt_sliced_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300886 ret = ops->vidioc_g_fmt_sliced_vbi_cap(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300887 fh, f);
888 break;
889 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300890 if (likely(ops->vidioc_g_fmt_sliced_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300891 ret = ops->vidioc_g_fmt_sliced_vbi_out(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300892 fh, f);
893 break;
894 case V4L2_BUF_TYPE_PRIVATE:
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300895 if (likely(ops->vidioc_g_fmt_type_private))
Hans Verkuila3998102008-07-21 02:57:38 -0300896 ret = ops->vidioc_g_fmt_type_private(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300897 fh, f);
898 break;
899 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300900 break;
901 }
902 case VIDIOC_S_FMT:
903 {
904 struct v4l2_format *f = (struct v4l2_format *)arg;
905
Hans Verkuil93d5a302011-08-09 09:32:06 -0300906 ret = -EINVAL;
907
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300908 /* FIXME: Should be one dump per type */
909 dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
910
911 switch (f->type) {
912 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300913 CLEAR_AFTER_FIELD(f, fmt.pix);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300914 v4l_print_pix_fmt(vfd, &f->fmt.pix);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300915 if (ops->vidioc_s_fmt_vid_cap)
Hans Verkuila3998102008-07-21 02:57:38 -0300916 ret = ops->vidioc_s_fmt_vid_cap(file, fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300917 break;
918 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
919 CLEAR_AFTER_FIELD(f, fmt.pix_mp);
920 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300921 if (ops->vidioc_s_fmt_vid_cap_mplane)
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300922 ret = ops->vidioc_s_fmt_vid_cap_mplane(file,
923 fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300924 break;
925 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300926 CLEAR_AFTER_FIELD(f, fmt.win);
Hans Verkuila3998102008-07-21 02:57:38 -0300927 if (ops->vidioc_s_fmt_vid_overlay)
928 ret = ops->vidioc_s_fmt_vid_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300929 fh, f);
930 break;
931 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300932 CLEAR_AFTER_FIELD(f, fmt.pix);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300933 v4l_print_pix_fmt(vfd, &f->fmt.pix);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300934 if (ops->vidioc_s_fmt_vid_out)
Hans Verkuila3998102008-07-21 02:57:38 -0300935 ret = ops->vidioc_s_fmt_vid_out(file, fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300936 break;
937 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
938 CLEAR_AFTER_FIELD(f, fmt.pix_mp);
939 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300940 if (ops->vidioc_s_fmt_vid_out_mplane)
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300941 ret = ops->vidioc_s_fmt_vid_out_mplane(file,
942 fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300943 break;
944 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300945 CLEAR_AFTER_FIELD(f, fmt.win);
Hans Verkuila3998102008-07-21 02:57:38 -0300946 if (ops->vidioc_s_fmt_vid_out_overlay)
947 ret = ops->vidioc_s_fmt_vid_out_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300948 fh, f);
949 break;
950 case V4L2_BUF_TYPE_VBI_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300951 CLEAR_AFTER_FIELD(f, fmt.vbi);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300952 if (likely(ops->vidioc_s_fmt_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300953 ret = ops->vidioc_s_fmt_vbi_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300954 break;
955 case V4L2_BUF_TYPE_VBI_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300956 CLEAR_AFTER_FIELD(f, fmt.vbi);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300957 if (likely(ops->vidioc_s_fmt_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300958 ret = ops->vidioc_s_fmt_vbi_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300959 break;
960 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300961 CLEAR_AFTER_FIELD(f, fmt.sliced);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300962 if (likely(ops->vidioc_s_fmt_sliced_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -0300963 ret = ops->vidioc_s_fmt_sliced_vbi_cap(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300964 fh, f);
965 break;
966 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300967 CLEAR_AFTER_FIELD(f, fmt.sliced);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300968 if (likely(ops->vidioc_s_fmt_sliced_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -0300969 ret = ops->vidioc_s_fmt_sliced_vbi_out(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300970 fh, f);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300971
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300972 break;
973 case V4L2_BUF_TYPE_PRIVATE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300974 /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -0300975 if (likely(ops->vidioc_s_fmt_type_private))
Hans Verkuila3998102008-07-21 02:57:38 -0300976 ret = ops->vidioc_s_fmt_type_private(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300977 fh, f);
978 break;
979 }
980 break;
981 }
982 case VIDIOC_TRY_FMT:
983 {
984 struct v4l2_format *f = (struct v4l2_format *)arg;
985
986 /* FIXME: Should be one dump per type */
987 dbgarg(cmd, "type=%s\n", prt_names(f->type,
988 v4l2_type_names));
Hans Verkuil48ea0be2012-05-10 05:36:00 -0300989 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300990 switch (f->type) {
991 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -0300992 CLEAR_AFTER_FIELD(f, fmt.pix);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -0300993 if (ops->vidioc_try_fmt_vid_cap)
Hans Verkuila3998102008-07-21 02:57:38 -0300994 ret = ops->vidioc_try_fmt_vid_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300995 if (!ret)
996 v4l_print_pix_fmt(vfd, &f->fmt.pix);
997 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300998 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
999 CLEAR_AFTER_FIELD(f, fmt.pix_mp);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -03001000 if (ops->vidioc_try_fmt_vid_cap_mplane)
Pawel Osciakd14e6d72010-12-23 04:15:27 -03001001 ret = ops->vidioc_try_fmt_vid_cap_mplane(file,
1002 fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -03001003 if (!ret)
1004 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
1005 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001006 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001007 CLEAR_AFTER_FIELD(f, fmt.win);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001008 if (likely(ops->vidioc_try_fmt_vid_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -03001009 ret = ops->vidioc_try_fmt_vid_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001010 fh, f);
1011 break;
1012 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001013 CLEAR_AFTER_FIELD(f, fmt.pix);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -03001014 if (ops->vidioc_try_fmt_vid_out)
Hans Verkuila3998102008-07-21 02:57:38 -03001015 ret = ops->vidioc_try_fmt_vid_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001016 if (!ret)
1017 v4l_print_pix_fmt(vfd, &f->fmt.pix);
1018 break;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03001019 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1020 CLEAR_AFTER_FIELD(f, fmt.pix_mp);
Tomasz Stanislawski1d0c86c2011-07-01 06:25:46 -03001021 if (ops->vidioc_try_fmt_vid_out_mplane)
Pawel Osciakd14e6d72010-12-23 04:15:27 -03001022 ret = ops->vidioc_try_fmt_vid_out_mplane(file,
1023 fh, f);
Pawel Osciakd14e6d72010-12-23 04:15:27 -03001024 if (!ret)
1025 v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
1026 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001027 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001028 CLEAR_AFTER_FIELD(f, fmt.win);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001029 if (likely(ops->vidioc_try_fmt_vid_out_overlay))
Hans Verkuila3998102008-07-21 02:57:38 -03001030 ret = ops->vidioc_try_fmt_vid_out_overlay(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001031 fh, f);
1032 break;
1033 case V4L2_BUF_TYPE_VBI_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001034 CLEAR_AFTER_FIELD(f, fmt.vbi);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001035 if (likely(ops->vidioc_try_fmt_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -03001036 ret = ops->vidioc_try_fmt_vbi_cap(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001037 break;
1038 case V4L2_BUF_TYPE_VBI_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001039 CLEAR_AFTER_FIELD(f, fmt.vbi);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001040 if (likely(ops->vidioc_try_fmt_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -03001041 ret = ops->vidioc_try_fmt_vbi_out(file, fh, f);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001042 break;
1043 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001044 CLEAR_AFTER_FIELD(f, fmt.sliced);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001045 if (likely(ops->vidioc_try_fmt_sliced_vbi_cap))
Hans Verkuila3998102008-07-21 02:57:38 -03001046 ret = ops->vidioc_try_fmt_sliced_vbi_cap(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001047 fh, f);
1048 break;
1049 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001050 CLEAR_AFTER_FIELD(f, fmt.sliced);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001051 if (likely(ops->vidioc_try_fmt_sliced_vbi_out))
Hans Verkuila3998102008-07-21 02:57:38 -03001052 ret = ops->vidioc_try_fmt_sliced_vbi_out(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001053 fh, f);
1054 break;
1055 case V4L2_BUF_TYPE_PRIVATE:
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001056 /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001057 if (likely(ops->vidioc_try_fmt_type_private))
Hans Verkuila3998102008-07-21 02:57:38 -03001058 ret = ops->vidioc_try_fmt_type_private(file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001059 fh, f);
1060 break;
1061 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001062 break;
1063 }
1064 /* FIXME: Those buf reqs could be handled here,
1065 with some changes on videobuf to allow its header to be included at
1066 videodev2.h or being merged at videodev2.
1067 */
1068 case VIDIOC_REQBUFS:
1069 {
1070 struct v4l2_requestbuffers *p = arg;
1071
Hans Verkuila3998102008-07-21 02:57:38 -03001072 ret = check_fmt(ops, p->type);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001073 if (ret)
1074 break;
1075
Trent Piepho7ecc0cf2009-04-30 21:03:34 -03001076 if (p->type < V4L2_BUF_TYPE_PRIVATE)
1077 CLEAR_AFTER_FIELD(p, memory);
1078
Hans Verkuila3998102008-07-21 02:57:38 -03001079 ret = ops->vidioc_reqbufs(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001080 dbgarg(cmd, "count=%d, type=%s, memory=%s\n",
1081 p->count,
1082 prt_names(p->type, v4l2_type_names),
1083 prt_names(p->memory, v4l2_memory_names));
1084 break;
1085 }
1086 case VIDIOC_QUERYBUF:
1087 {
1088 struct v4l2_buffer *p = arg;
1089
Hans Verkuila3998102008-07-21 02:57:38 -03001090 ret = check_fmt(ops, p->type);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001091 if (ret)
1092 break;
1093
Hans Verkuila3998102008-07-21 02:57:38 -03001094 ret = ops->vidioc_querybuf(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001095 if (!ret)
1096 dbgbuf(cmd, vfd, p);
1097 break;
1098 }
1099 case VIDIOC_QBUF:
1100 {
1101 struct v4l2_buffer *p = arg;
1102
Hans Verkuila3998102008-07-21 02:57:38 -03001103 ret = check_fmt(ops, p->type);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001104 if (ret)
1105 break;
1106
Hans Verkuila3998102008-07-21 02:57:38 -03001107 ret = ops->vidioc_qbuf(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001108 if (!ret)
1109 dbgbuf(cmd, vfd, p);
1110 break;
1111 }
1112 case VIDIOC_DQBUF:
1113 {
1114 struct v4l2_buffer *p = arg;
1115
Hans Verkuila3998102008-07-21 02:57:38 -03001116 ret = check_fmt(ops, p->type);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001117 if (ret)
1118 break;
1119
Hans Verkuila3998102008-07-21 02:57:38 -03001120 ret = ops->vidioc_dqbuf(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001121 if (!ret)
1122 dbgbuf(cmd, vfd, p);
1123 break;
1124 }
1125 case VIDIOC_OVERLAY:
1126 {
1127 int *i = arg;
1128
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001129 dbgarg(cmd, "value=%d\n", *i);
Hans Verkuila3998102008-07-21 02:57:38 -03001130 ret = ops->vidioc_overlay(file, fh, *i);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001131 break;
1132 }
1133 case VIDIOC_G_FBUF:
1134 {
1135 struct v4l2_framebuffer *p = arg;
1136
Hans Verkuila3998102008-07-21 02:57:38 -03001137 ret = ops->vidioc_g_fbuf(file, fh, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001138 if (!ret) {
1139 dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
1140 p->capability, p->flags,
1141 (unsigned long)p->base);
1142 v4l_print_pix_fmt(vfd, &p->fmt);
1143 }
1144 break;
1145 }
1146 case VIDIOC_S_FBUF:
1147 {
1148 struct v4l2_framebuffer *p = arg;
1149
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001150 dbgarg(cmd, "capability=0x%x, flags=%d, base=0x%08lx\n",
1151 p->capability, p->flags, (unsigned long)p->base);
1152 v4l_print_pix_fmt(vfd, &p->fmt);
Hans Verkuila3998102008-07-21 02:57:38 -03001153 ret = ops->vidioc_s_fbuf(file, fh, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001154 break;
1155 }
1156 case VIDIOC_STREAMON:
1157 {
1158 enum v4l2_buf_type i = *(int *)arg;
1159
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001160 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
Hans Verkuila3998102008-07-21 02:57:38 -03001161 ret = ops->vidioc_streamon(file, fh, i);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001162 break;
1163 }
1164 case VIDIOC_STREAMOFF:
1165 {
1166 enum v4l2_buf_type i = *(int *)arg;
1167
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001168 dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
Hans Verkuila3998102008-07-21 02:57:38 -03001169 ret = ops->vidioc_streamoff(file, fh, i);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001170 break;
1171 }
1172 /* ---------- tv norms ---------- */
1173 case VIDIOC_ENUMSTD:
1174 {
1175 struct v4l2_standard *p = arg;
1176 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1177 unsigned int index = p->index, i, j = 0;
1178 const char *descr = "";
1179
Hans Verkuil93d5a302011-08-09 09:32:06 -03001180 if (id == 0)
1181 break;
1182 ret = -EINVAL;
1183
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001184 /* Return norm array in a canonical way */
1185 for (i = 0; i <= index && id; i++) {
1186 /* last std value in the standards array is 0, so this
1187 while always ends there since (id & 0) == 0. */
1188 while ((id & standards[j].std) != standards[j].std)
1189 j++;
1190 curr_id = standards[j].std;
1191 descr = standards[j].descr;
1192 j++;
1193 if (curr_id == 0)
1194 break;
1195 if (curr_id != V4L2_STD_PAL &&
1196 curr_id != V4L2_STD_SECAM &&
1197 curr_id != V4L2_STD_NTSC)
1198 id &= ~curr_id;
1199 }
1200 if (i <= index)
Hans Verkuil3f5e1822010-04-06 08:14:11 -03001201 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001202
1203 v4l2_video_std_construct(p, curr_id, descr);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001204
1205 dbgarg(cmd, "index=%d, id=0x%Lx, name=%s, fps=%d/%d, "
1206 "framelines=%d\n", p->index,
1207 (unsigned long long)p->id, p->name,
1208 p->frameperiod.numerator,
1209 p->frameperiod.denominator,
1210 p->framelines);
1211
1212 ret = 0;
1213 break;
1214 }
1215 case VIDIOC_G_STD:
1216 {
1217 v4l2_std_id *id = arg;
1218
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001219 /* Calls the specific handler */
Hans Verkuila3998102008-07-21 02:57:38 -03001220 if (ops->vidioc_g_std)
1221 ret = ops->vidioc_g_std(file, fh, id);
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001222 else if (vfd->current_norm) {
1223 ret = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001224 *id = vfd->current_norm;
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001225 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001226
Mauro Carvalho Chehaba5f2db52011-07-31 09:37:56 -03001227 if (likely(!ret))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001228 dbgarg(cmd, "std=0x%08Lx\n", (long long unsigned)*id);
1229 break;
1230 }
1231 case VIDIOC_S_STD:
1232 {
1233 v4l2_std_id *id = arg, norm;
1234
1235 dbgarg(cmd, "std=%08Lx\n", (long long unsigned)*id);
1236
Hans Verkuil93d5a302011-08-09 09:32:06 -03001237 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001238 norm = (*id) & vfd->tvnorms;
1239 if (vfd->tvnorms && !norm) /* Check if std is supported */
1240 break;
1241
1242 /* Calls the specific handler */
Hans Verkuil93d5a302011-08-09 09:32:06 -03001243 ret = ops->vidioc_s_std(file, fh, &norm);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001244
1245 /* Updates standard information */
1246 if (ret >= 0)
1247 vfd->current_norm = norm;
1248 break;
1249 }
1250 case VIDIOC_QUERYSTD:
1251 {
1252 v4l2_std_id *p = arg;
1253
Mauro Carvalho Chehab8715e6c2011-10-04 09:32:23 -03001254 /*
1255 * If nothing detected, it should return all supported
1256 * Drivers just need to mask the std argument, in order
1257 * to remove the standards that don't apply from the mask.
1258 * This means that tuners, audio and video decoders can join
1259 * their efforts to improve the standards detection
1260 */
1261 *p = vfd->tvnorms;
Hans Verkuila3998102008-07-21 02:57:38 -03001262 ret = ops->vidioc_querystd(file, fh, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001263 if (!ret)
1264 dbgarg(cmd, "detected std=%08Lx\n",
1265 (unsigned long long)*p);
1266 break;
1267 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001268
1269 /* --- controls ---------------------------------------------- */
1270 case VIDIOC_QUERYCTRL:
1271 {
1272 struct v4l2_queryctrl *p = arg;
1273
Hans Verkuil2d28b682011-03-12 08:54:43 -03001274 if (vfh && vfh->ctrl_handler)
1275 ret = v4l2_queryctrl(vfh->ctrl_handler, p);
1276 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001277 ret = v4l2_queryctrl(vfd->ctrl_handler, p);
1278 else if (ops->vidioc_queryctrl)
1279 ret = ops->vidioc_queryctrl(file, fh, p);
1280 else
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001281 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001282 if (!ret)
1283 dbgarg(cmd, "id=0x%x, type=%d, name=%s, min/max=%d/%d, "
1284 "step=%d, default=%d, flags=0x%08x\n",
1285 p->id, p->type, p->name,
1286 p->minimum, p->maximum,
1287 p->step, p->default_value, p->flags);
1288 else
1289 dbgarg(cmd, "id=0x%x\n", p->id);
1290 break;
1291 }
1292 case VIDIOC_G_CTRL:
1293 {
1294 struct v4l2_control *p = arg;
1295
Hans Verkuil2d28b682011-03-12 08:54:43 -03001296 if (vfh && vfh->ctrl_handler)
1297 ret = v4l2_g_ctrl(vfh->ctrl_handler, p);
1298 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001299 ret = v4l2_g_ctrl(vfd->ctrl_handler, p);
1300 else if (ops->vidioc_g_ctrl)
Hans Verkuila3998102008-07-21 02:57:38 -03001301 ret = ops->vidioc_g_ctrl(file, fh, p);
1302 else if (ops->vidioc_g_ext_ctrls) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001303 struct v4l2_ext_controls ctrls;
1304 struct v4l2_ext_control ctrl;
1305
1306 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1307 ctrls.count = 1;
1308 ctrls.controls = &ctrl;
1309 ctrl.id = p->id;
1310 ctrl.value = p->value;
1311 if (check_ext_ctrls(&ctrls, 1)) {
Hans Verkuila3998102008-07-21 02:57:38 -03001312 ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001313 if (ret == 0)
1314 p->value = ctrl.value;
1315 }
1316 } else
1317 break;
1318 if (!ret)
1319 dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1320 else
1321 dbgarg(cmd, "id=0x%x\n", p->id);
1322 break;
1323 }
1324 case VIDIOC_S_CTRL:
1325 {
1326 struct v4l2_control *p = arg;
1327 struct v4l2_ext_controls ctrls;
1328 struct v4l2_ext_control ctrl;
1329
Hans Verkuil2d28b682011-03-12 08:54:43 -03001330 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001331 !ops->vidioc_s_ctrl && !ops->vidioc_s_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001332 break;
1333
1334 dbgarg(cmd, "id=0x%x, value=%d\n", p->id, p->value);
1335
Hans Verkuil2d28b682011-03-12 08:54:43 -03001336 if (vfh && vfh->ctrl_handler) {
Hans Verkuilab892ba2011-06-07 06:47:18 -03001337 ret = v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001338 break;
1339 }
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001340 if (vfd->ctrl_handler) {
Hans Verkuilab892ba2011-06-07 06:47:18 -03001341 ret = v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001342 break;
1343 }
Hans Verkuila3998102008-07-21 02:57:38 -03001344 if (ops->vidioc_s_ctrl) {
1345 ret = ops->vidioc_s_ctrl(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001346 break;
1347 }
Hans Verkuila3998102008-07-21 02:57:38 -03001348 if (!ops->vidioc_s_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001349 break;
1350
1351 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1352 ctrls.count = 1;
1353 ctrls.controls = &ctrl;
1354 ctrl.id = p->id;
1355 ctrl.value = p->value;
1356 if (check_ext_ctrls(&ctrls, 1))
Hans Verkuila3998102008-07-21 02:57:38 -03001357 ret = ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001358 else
1359 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001360 break;
1361 }
1362 case VIDIOC_G_EXT_CTRLS:
1363 {
1364 struct v4l2_ext_controls *p = arg;
1365
1366 p->error_idx = p->count;
Hans Verkuil2d28b682011-03-12 08:54:43 -03001367 if (vfh && vfh->ctrl_handler)
1368 ret = v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1369 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001370 ret = v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001371 else if (ops->vidioc_g_ext_ctrls)
1372 ret = check_ext_ctrls(p, 0) ?
1373 ops->vidioc_g_ext_ctrls(file, fh, p) :
1374 -EINVAL;
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001375 else
1376 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001377 v4l_print_ext_ctrls(cmd, vfd, p, !ret);
1378 break;
1379 }
1380 case VIDIOC_S_EXT_CTRLS:
1381 {
1382 struct v4l2_ext_controls *p = arg;
1383
1384 p->error_idx = p->count;
Hans Verkuil2d28b682011-03-12 08:54:43 -03001385 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
1386 !ops->vidioc_s_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001387 break;
1388 v4l_print_ext_ctrls(cmd, vfd, p, 1);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001389 if (vfh && vfh->ctrl_handler)
Hans Verkuilab892ba2011-06-07 06:47:18 -03001390 ret = v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001391 else if (vfd->ctrl_handler)
Hans Verkuilab892ba2011-06-07 06:47:18 -03001392 ret = v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001393 else if (check_ext_ctrls(p, 0))
Hans Verkuila3998102008-07-21 02:57:38 -03001394 ret = ops->vidioc_s_ext_ctrls(file, fh, p);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001395 else
1396 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001397 break;
1398 }
1399 case VIDIOC_TRY_EXT_CTRLS:
1400 {
1401 struct v4l2_ext_controls *p = arg;
1402
1403 p->error_idx = p->count;
Hans Verkuil2d28b682011-03-12 08:54:43 -03001404 if (!(vfh && vfh->ctrl_handler) && !vfd->ctrl_handler &&
1405 !ops->vidioc_try_ext_ctrls)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001406 break;
1407 v4l_print_ext_ctrls(cmd, vfd, p, 1);
Hans Verkuil2d28b682011-03-12 08:54:43 -03001408 if (vfh && vfh->ctrl_handler)
1409 ret = v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1410 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001411 ret = v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1412 else if (check_ext_ctrls(p, 0))
Hans Verkuila3998102008-07-21 02:57:38 -03001413 ret = ops->vidioc_try_ext_ctrls(file, fh, p);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001414 else
1415 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001416 break;
1417 }
1418 case VIDIOC_QUERYMENU:
1419 {
1420 struct v4l2_querymenu *p = arg;
1421
Hans Verkuil2d28b682011-03-12 08:54:43 -03001422 if (vfh && vfh->ctrl_handler)
1423 ret = v4l2_querymenu(vfh->ctrl_handler, p);
1424 else if (vfd->ctrl_handler)
Hans Verkuil11bbc1c2010-05-16 09:24:06 -03001425 ret = v4l2_querymenu(vfd->ctrl_handler, p);
1426 else if (ops->vidioc_querymenu)
1427 ret = ops->vidioc_querymenu(file, fh, p);
1428 else
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001429 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001430 if (!ret)
1431 dbgarg(cmd, "id=0x%x, index=%d, name=%s\n",
1432 p->id, p->index, p->name);
1433 else
1434 dbgarg(cmd, "id=0x%x, index=%d\n",
1435 p->id, p->index);
1436 break;
1437 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001438 case VIDIOC_G_MODULATOR:
1439 {
1440 struct v4l2_modulator *p = arg;
1441
Hans Verkuila3998102008-07-21 02:57:38 -03001442 ret = ops->vidioc_g_modulator(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001443 if (!ret)
1444 dbgarg(cmd, "index=%d, name=%s, "
1445 "capability=%d, rangelow=%d,"
1446 " rangehigh=%d, txsubchans=%d\n",
1447 p->index, p->name, p->capability,
1448 p->rangelow, p->rangehigh,
1449 p->txsubchans);
1450 break;
1451 }
1452 case VIDIOC_S_MODULATOR:
1453 {
1454 struct v4l2_modulator *p = arg;
1455
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001456 dbgarg(cmd, "index=%d, name=%s, capability=%d, "
1457 "rangelow=%d, rangehigh=%d, txsubchans=%d\n",
1458 p->index, p->name, p->capability, p->rangelow,
1459 p->rangehigh, p->txsubchans);
Hans Verkuila3998102008-07-21 02:57:38 -03001460 ret = ops->vidioc_s_modulator(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001461 break;
1462 }
1463 case VIDIOC_G_CROP:
1464 {
1465 struct v4l2_crop *p = arg;
1466
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001467 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Tomasz Stanislawski992efeff2011-03-22 14:04:47 -03001468
1469 if (ops->vidioc_g_crop) {
1470 ret = ops->vidioc_g_crop(file, fh, p);
1471 } else {
1472 /* simulate capture crop using selection api */
1473 struct v4l2_selection s = {
1474 .type = p->type,
1475 };
1476
1477 /* crop means compose for output devices */
1478 if (V4L2_TYPE_IS_OUTPUT(p->type))
1479 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1480 else
1481 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1482
1483 ret = ops->vidioc_g_selection(file, fh, &s);
1484
1485 /* copying results to old structure on success */
1486 if (!ret)
1487 p->c = s.r;
1488 }
1489
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001490 if (!ret)
1491 dbgrect(vfd, "", &p->c);
1492 break;
1493 }
1494 case VIDIOC_S_CROP:
1495 {
1496 struct v4l2_crop *p = arg;
1497
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001498 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1499 dbgrect(vfd, "", &p->c);
Tomasz Stanislawski992efeff2011-03-22 14:04:47 -03001500
1501 if (ops->vidioc_s_crop) {
1502 ret = ops->vidioc_s_crop(file, fh, p);
1503 } else {
1504 /* simulate capture crop using selection api */
1505 struct v4l2_selection s = {
1506 .type = p->type,
1507 .r = p->c,
1508 };
1509
1510 /* crop means compose for output devices */
1511 if (V4L2_TYPE_IS_OUTPUT(p->type))
1512 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1513 else
1514 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1515
1516 ret = ops->vidioc_s_selection(file, fh, &s);
1517 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001518 break;
1519 }
Tomasz Stanislawski0e8caac2011-08-10 10:37:47 -03001520 case VIDIOC_G_SELECTION:
1521 {
1522 struct v4l2_selection *p = arg;
1523
Tomasz Stanislawski0e8caac2011-08-10 10:37:47 -03001524 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1525
1526 ret = ops->vidioc_g_selection(file, fh, p);
1527 if (!ret)
1528 dbgrect(vfd, "", &p->r);
1529 break;
1530 }
1531 case VIDIOC_S_SELECTION:
1532 {
1533 struct v4l2_selection *p = arg;
1534
Tomasz Stanislawski0e8caac2011-08-10 10:37:47 -03001535
1536 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
1537 dbgrect(vfd, "", &p->r);
1538
1539 ret = ops->vidioc_s_selection(file, fh, p);
1540 break;
1541 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001542 case VIDIOC_CROPCAP:
1543 {
1544 struct v4l2_cropcap *p = arg;
1545
1546 /*FIXME: Should also show v4l2_fract pixelaspect */
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001547 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Tomasz Stanislawski992efeff2011-03-22 14:04:47 -03001548 if (ops->vidioc_cropcap) {
1549 ret = ops->vidioc_cropcap(file, fh, p);
1550 } else {
1551 struct v4l2_selection s = { .type = p->type };
1552
1553 /* obtaining bounds */
1554 if (V4L2_TYPE_IS_OUTPUT(p->type))
1555 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1556 else
1557 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1558
1559 ret = ops->vidioc_g_selection(file, fh, &s);
1560 if (ret)
1561 break;
1562 p->bounds = s.r;
1563
1564 /* obtaining defrect */
1565 if (V4L2_TYPE_IS_OUTPUT(p->type))
1566 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1567 else
1568 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1569
1570 ret = ops->vidioc_g_selection(file, fh, &s);
1571 if (ret)
1572 break;
1573 p->defrect = s.r;
1574
1575 /* setting trivial pixelaspect */
1576 p->pixelaspect.numerator = 1;
1577 p->pixelaspect.denominator = 1;
1578 }
1579
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001580 if (!ret) {
1581 dbgrect(vfd, "bounds ", &p->bounds);
1582 dbgrect(vfd, "defrect ", &p->defrect);
1583 }
1584 break;
1585 }
1586 case VIDIOC_G_JPEGCOMP:
1587 {
1588 struct v4l2_jpegcompression *p = arg;
1589
Hans Verkuila3998102008-07-21 02:57:38 -03001590 ret = ops->vidioc_g_jpegcomp(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001591 if (!ret)
1592 dbgarg(cmd, "quality=%d, APPn=%d, "
1593 "APP_len=%d, COM_len=%d, "
1594 "jpeg_markers=%d\n",
1595 p->quality, p->APPn, p->APP_len,
1596 p->COM_len, p->jpeg_markers);
1597 break;
1598 }
1599 case VIDIOC_S_JPEGCOMP:
1600 {
1601 struct v4l2_jpegcompression *p = arg;
1602
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001603 dbgarg(cmd, "quality=%d, APPn=%d, APP_len=%d, "
1604 "COM_len=%d, jpeg_markers=%d\n",
1605 p->quality, p->APPn, p->APP_len,
1606 p->COM_len, p->jpeg_markers);
Hans Verkuil93d5a302011-08-09 09:32:06 -03001607 ret = ops->vidioc_s_jpegcomp(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001608 break;
1609 }
1610 case VIDIOC_G_ENC_INDEX:
1611 {
1612 struct v4l2_enc_idx *p = arg;
1613
Hans Verkuila3998102008-07-21 02:57:38 -03001614 ret = ops->vidioc_g_enc_index(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001615 if (!ret)
1616 dbgarg(cmd, "entries=%d, entries_cap=%d\n",
1617 p->entries, p->entries_cap);
1618 break;
1619 }
1620 case VIDIOC_ENCODER_CMD:
1621 {
1622 struct v4l2_encoder_cmd *p = arg;
1623
Hans Verkuila3998102008-07-21 02:57:38 -03001624 ret = ops->vidioc_encoder_cmd(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001625 if (!ret)
1626 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1627 break;
1628 }
1629 case VIDIOC_TRY_ENCODER_CMD:
1630 {
1631 struct v4l2_encoder_cmd *p = arg;
1632
Hans Verkuila3998102008-07-21 02:57:38 -03001633 ret = ops->vidioc_try_encoder_cmd(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001634 if (!ret)
1635 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1636 break;
1637 }
Hans Verkuila45c0ad2011-11-24 09:53:43 -03001638 case VIDIOC_DECODER_CMD:
1639 {
1640 struct v4l2_decoder_cmd *p = arg;
1641
Hans Verkuila45c0ad2011-11-24 09:53:43 -03001642 ret = ops->vidioc_decoder_cmd(file, fh, p);
1643 if (!ret)
1644 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1645 break;
1646 }
1647 case VIDIOC_TRY_DECODER_CMD:
1648 {
1649 struct v4l2_decoder_cmd *p = arg;
1650
Hans Verkuila45c0ad2011-11-24 09:53:43 -03001651 ret = ops->vidioc_try_decoder_cmd(file, fh, p);
1652 if (!ret)
1653 dbgarg(cmd, "cmd=%d, flags=%x\n", p->cmd, p->flags);
1654 break;
1655 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001656 case VIDIOC_G_PARM:
1657 {
1658 struct v4l2_streamparm *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001659
Hans Verkuila3998102008-07-21 02:57:38 -03001660 if (ops->vidioc_g_parm) {
Trent Piepho34796bc2009-03-28 22:25:35 -03001661 ret = check_fmt(ops, p->type);
1662 if (ret)
1663 break;
Hans Verkuila3998102008-07-21 02:57:38 -03001664 ret = ops->vidioc_g_parm(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001665 } else {
Hans Verkuil9bedc7f2009-08-07 07:28:16 -03001666 v4l2_std_id std = vfd->current_norm;
1667
Hans Verkuil93d5a302011-08-09 09:32:06 -03001668 ret = -EINVAL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001669 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Hans Verkuil3f5e1822010-04-06 08:14:11 -03001670 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001671
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001672 ret = 0;
Hans Verkuil0c142c82012-05-25 11:51:31 -03001673 p->parm.capture.readbuffers = 2;
Hans Verkuil9bedc7f2009-08-07 07:28:16 -03001674 if (ops->vidioc_g_std)
1675 ret = ops->vidioc_g_std(file, fh, &std);
Hans Verkuil9bedc7f2009-08-07 07:28:16 -03001676 if (ret == 0)
1677 v4l2_video_std_frame_period(std,
1678 &p->parm.capture.timeperframe);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001679 }
1680
1681 dbgarg(cmd, "type=%d\n", p->type);
1682 break;
1683 }
1684 case VIDIOC_S_PARM:
1685 {
1686 struct v4l2_streamparm *p = arg;
1687
Trent Piepho34796bc2009-03-28 22:25:35 -03001688 ret = check_fmt(ops, p->type);
1689 if (ret)
1690 break;
1691
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001692 dbgarg(cmd, "type=%d\n", p->type);
Hans Verkuila3998102008-07-21 02:57:38 -03001693 ret = ops->vidioc_s_parm(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001694 break;
1695 }
1696 case VIDIOC_G_TUNER:
1697 {
1698 struct v4l2_tuner *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001699
Hans Verkuil227690d2011-06-12 06:36:41 -03001700 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1701 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuila3998102008-07-21 02:57:38 -03001702 ret = ops->vidioc_g_tuner(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001703 if (!ret)
1704 dbgarg(cmd, "index=%d, name=%s, type=%d, "
1705 "capability=0x%x, rangelow=%d, "
1706 "rangehigh=%d, signal=%d, afc=%d, "
1707 "rxsubchans=0x%x, audmode=%d\n",
1708 p->index, p->name, p->type,
1709 p->capability, p->rangelow,
1710 p->rangehigh, p->signal, p->afc,
1711 p->rxsubchans, p->audmode);
1712 break;
1713 }
1714 case VIDIOC_S_TUNER:
1715 {
1716 struct v4l2_tuner *p = arg;
1717
Hans Verkuil227690d2011-06-12 06:36:41 -03001718 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1719 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001720 dbgarg(cmd, "index=%d, name=%s, type=%d, "
1721 "capability=0x%x, rangelow=%d, "
1722 "rangehigh=%d, signal=%d, afc=%d, "
1723 "rxsubchans=0x%x, audmode=%d\n",
1724 p->index, p->name, p->type,
1725 p->capability, p->rangelow,
1726 p->rangehigh, p->signal, p->afc,
1727 p->rxsubchans, p->audmode);
Hans Verkuila3998102008-07-21 02:57:38 -03001728 ret = ops->vidioc_s_tuner(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001729 break;
1730 }
1731 case VIDIOC_G_FREQUENCY:
1732 {
1733 struct v4l2_frequency *p = arg;
1734
Hans Verkuil227690d2011-06-12 06:36:41 -03001735 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1736 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuila3998102008-07-21 02:57:38 -03001737 ret = ops->vidioc_g_frequency(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001738 if (!ret)
1739 dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1740 p->tuner, p->type, p->frequency);
1741 break;
1742 }
1743 case VIDIOC_S_FREQUENCY:
1744 {
1745 struct v4l2_frequency *p = arg;
Hans Verkuilaa07eec2011-10-11 05:12:52 -03001746 enum v4l2_tuner_type type;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001747
Hans Verkuilaa07eec2011-10-11 05:12:52 -03001748 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1749 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001750 dbgarg(cmd, "tuner=%d, type=%d, frequency=%d\n",
1751 p->tuner, p->type, p->frequency);
Hans Verkuilaa07eec2011-10-11 05:12:52 -03001752 if (p->type != type)
1753 ret = -EINVAL;
1754 else
1755 ret = ops->vidioc_s_frequency(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001756 break;
1757 }
1758 case VIDIOC_G_SLICED_VBI_CAP:
1759 {
1760 struct v4l2_sliced_vbi_cap *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001761
Trent Piepho19c96e42009-03-04 01:21:02 -03001762 /* Clear up to type, everything after type is zerod already */
1763 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1764
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001765 dbgarg(cmd, "type=%s\n", prt_names(p->type, v4l2_type_names));
Hans Verkuila3998102008-07-21 02:57:38 -03001766 ret = ops->vidioc_g_sliced_vbi_cap(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001767 if (!ret)
1768 dbgarg2("service_set=%d\n", p->service_set);
1769 break;
1770 }
1771 case VIDIOC_LOG_STATUS:
1772 {
Hans Verkuile2ecb252012-02-02 08:20:53 -03001773 if (vfd->v4l2_dev)
1774 pr_info("%s: ================= START STATUS =================\n",
1775 vfd->v4l2_dev->name);
Hans Verkuila3998102008-07-21 02:57:38 -03001776 ret = ops->vidioc_log_status(file, fh);
Hans Verkuile2ecb252012-02-02 08:20:53 -03001777 if (vfd->v4l2_dev)
1778 pr_info("%s: ================== END STATUS ==================\n",
1779 vfd->v4l2_dev->name);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001780 break;
1781 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001782 case VIDIOC_DBG_G_REGISTER:
1783 {
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001784#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001785 struct v4l2_dbg_register *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001786
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001787 if (!capable(CAP_SYS_ADMIN))
1788 ret = -EPERM;
1789 else
1790 ret = ops->vidioc_g_register(file, fh, p);
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001791#endif
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001792 break;
1793 }
1794 case VIDIOC_DBG_S_REGISTER:
1795 {
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001796#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001797 struct v4l2_dbg_register *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001798
Hans Verkuil48ea0be2012-05-10 05:36:00 -03001799 if (!capable(CAP_SYS_ADMIN))
1800 ret = -EPERM;
1801 else
1802 ret = ops->vidioc_s_register(file, fh, p);
Hans Verkuil8ab75e32012-05-10 02:51:31 -03001803#endif
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001804 break;
1805 }
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001806 case VIDIOC_DBG_G_CHIP_IDENT:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001807 {
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001808 struct v4l2_dbg_chip_ident *p = arg;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001809
Hans Verkuil80b36e02009-02-07 11:00:02 -03001810 p->ident = V4L2_IDENT_NONE;
1811 p->revision = 0;
Hans Verkuila3998102008-07-21 02:57:38 -03001812 ret = ops->vidioc_g_chip_ident(file, fh, p);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001813 if (!ret)
1814 dbgarg(cmd, "chip_ident=%u, revision=0x%x\n", p->ident, p->revision);
1815 break;
1816 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001817 case VIDIOC_S_HW_FREQ_SEEK:
1818 {
1819 struct v4l2_hw_freq_seek *p = arg;
Hans Verkuila6cf90a2011-06-14 04:03:26 -03001820 enum v4l2_tuner_type type;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001821
Hans Verkuila6cf90a2011-06-14 04:03:26 -03001822 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1823 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03001824 dbgarg(cmd,
Hans Verkuila6cf90a2011-06-14 04:03:26 -03001825 "tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u\n",
1826 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing);
1827 if (p->type != type)
1828 ret = -EINVAL;
1829 else
1830 ret = ops->vidioc_s_hw_freq_seek(file, fh, p);
Hans Verkuila3998102008-07-21 02:57:38 -03001831 break;
1832 }
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001833 case VIDIOC_ENUM_FRAMESIZES:
1834 {
1835 struct v4l2_frmsizeenum *p = arg;
1836
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001837 ret = ops->vidioc_enum_framesizes(file, fh, p);
1838 dbgarg(cmd,
Mauro Carvalho Chehabd1afe422009-06-21 22:37:12 -03001839 "index=%d, pixelformat=%c%c%c%c, type=%d ",
1840 p->index,
1841 (p->pixel_format & 0xff),
1842 (p->pixel_format >> 8) & 0xff,
1843 (p->pixel_format >> 16) & 0xff,
1844 (p->pixel_format >> 24) & 0xff,
1845 p->type);
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001846 switch (p->type) {
1847 case V4L2_FRMSIZE_TYPE_DISCRETE:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001848 dbgarg3("width = %d, height=%d\n",
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001849 p->discrete.width, p->discrete.height);
1850 break;
1851 case V4L2_FRMSIZE_TYPE_STEPWISE:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001852 dbgarg3("min %dx%d, max %dx%d, step %dx%d\n",
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001853 p->stepwise.min_width, p->stepwise.min_height,
1854 p->stepwise.step_width, p->stepwise.step_height,
1855 p->stepwise.max_width, p->stepwise.max_height);
1856 break;
1857 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001858 dbgarg3("continuous\n");
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001859 break;
1860 default:
Mauro Carvalho Chehabd33fbcb2009-07-02 17:07:32 -03001861 dbgarg3("- Unknown type!\n");
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001862 }
1863
1864 break;
1865 }
1866 case VIDIOC_ENUM_FRAMEINTERVALS:
1867 {
1868 struct v4l2_frmivalenum *p = arg;
1869
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001870 ret = ops->vidioc_enum_frameintervals(file, fh, p);
1871 dbgarg(cmd,
1872 "index=%d, pixelformat=%d, width=%d, height=%d, type=%d ",
1873 p->index, p->pixel_format,
1874 p->width, p->height, p->type);
1875 switch (p->type) {
1876 case V4L2_FRMIVAL_TYPE_DISCRETE:
1877 dbgarg2("fps=%d/%d\n",
1878 p->discrete.numerator,
1879 p->discrete.denominator);
1880 break;
1881 case V4L2_FRMIVAL_TYPE_STEPWISE:
Mauro Carvalho Chehab19585782008-11-12 01:03:02 -03001882 dbgarg2("min=%d/%d, max=%d/%d, step=%d/%d\n",
1883 p->stepwise.min.numerator,
1884 p->stepwise.min.denominator,
1885 p->stepwise.max.numerator,
1886 p->stepwise.max.denominator,
1887 p->stepwise.step.numerator,
1888 p->stepwise.step.denominator);
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03001889 break;
1890 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1891 dbgarg2("continuous\n");
1892 break;
1893 default:
1894 dbgarg2("- Unknown type!\n");
1895 }
1896 break;
1897 }
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001898 case VIDIOC_ENUM_DV_PRESETS:
1899 {
1900 struct v4l2_dv_enum_preset *p = arg;
1901
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001902 ret = ops->vidioc_enum_dv_presets(file, fh, p);
1903 if (!ret)
1904 dbgarg(cmd,
1905 "index=%d, preset=%d, name=%s, width=%d,"
1906 " height=%d ",
1907 p->index, p->preset, p->name, p->width,
1908 p->height);
1909 break;
1910 }
1911 case VIDIOC_S_DV_PRESET:
1912 {
1913 struct v4l2_dv_preset *p = arg;
1914
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001915 dbgarg(cmd, "preset=%d\n", p->preset);
1916 ret = ops->vidioc_s_dv_preset(file, fh, p);
1917 break;
1918 }
1919 case VIDIOC_G_DV_PRESET:
1920 {
1921 struct v4l2_dv_preset *p = arg;
1922
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001923 ret = ops->vidioc_g_dv_preset(file, fh, p);
1924 if (!ret)
1925 dbgarg(cmd, "preset=%d\n", p->preset);
1926 break;
1927 }
1928 case VIDIOC_QUERY_DV_PRESET:
1929 {
1930 struct v4l2_dv_preset *p = arg;
1931
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001932 ret = ops->vidioc_query_dv_preset(file, fh, p);
1933 if (!ret)
1934 dbgarg(cmd, "preset=%d\n", p->preset);
1935 break;
1936 }
1937 case VIDIOC_S_DV_TIMINGS:
1938 {
1939 struct v4l2_dv_timings *p = arg;
1940
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001941 dbgtimings(vfd, p);
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001942 switch (p->type) {
1943 case V4L2_DV_BT_656_1120:
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001944 ret = ops->vidioc_s_dv_timings(file, fh, p);
1945 break;
1946 default:
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001947 ret = -EINVAL;
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001948 break;
1949 }
1950 break;
1951 }
1952 case VIDIOC_G_DV_TIMINGS:
1953 {
1954 struct v4l2_dv_timings *p = arg;
1955
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001956 ret = ops->vidioc_g_dv_timings(file, fh, p);
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001957 if (!ret)
1958 dbgtimings(vfd, p);
1959 break;
1960 }
1961 case VIDIOC_ENUM_DV_TIMINGS:
1962 {
1963 struct v4l2_enum_dv_timings *p = arg;
1964
1965 if (!ops->vidioc_enum_dv_timings)
1966 break;
1967
1968 ret = ops->vidioc_enum_dv_timings(file, fh, p);
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03001969 if (!ret) {
Hans Verkuil5d7758e2012-05-15 08:06:44 -03001970 dbgarg(cmd, "index=%d: ", p->index);
1971 dbgtimings(vfd, &p->timings);
1972 }
1973 break;
1974 }
1975 case VIDIOC_QUERY_DV_TIMINGS:
1976 {
1977 struct v4l2_dv_timings *p = arg;
1978
1979 if (!ops->vidioc_query_dv_timings)
1980 break;
1981
1982 ret = ops->vidioc_query_dv_timings(file, fh, p);
1983 if (!ret)
1984 dbgtimings(vfd, p);
1985 break;
1986 }
1987 case VIDIOC_DV_TIMINGS_CAP:
1988 {
1989 struct v4l2_dv_timings_cap *p = arg;
1990
1991 if (!ops->vidioc_dv_timings_cap)
1992 break;
1993
1994 ret = ops->vidioc_dv_timings_cap(file, fh, p);
1995 if (ret)
1996 break;
1997 switch (p->type) {
1998 case V4L2_DV_BT_656_1120:
1999 dbgarg(cmd,
2000 "type=%d, width=%u-%u, height=%u-%u, "
2001 "pixelclock=%llu-%llu, standards=%x, capabilities=%x ",
2002 p->type,
2003 p->bt.min_width, p->bt.max_width,
2004 p->bt.min_height, p->bt.max_height,
2005 p->bt.min_pixelclock, p->bt.max_pixelclock,
2006 p->bt.standards, p->bt.capabilities);
2007 break;
2008 default:
2009 dbgarg(cmd, "unknown type ");
2010 break;
Muralidharan Karicherib6456c02009-11-19 12:00:31 -03002011 }
2012 break;
2013 }
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002014 case VIDIOC_DQEVENT:
2015 {
2016 struct v4l2_event *ev = arg;
Mauro Carvalho Chehab74d83fa2008-11-11 21:13:47 -03002017
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002018 ret = v4l2_event_dequeue(fh, ev, file->f_flags & O_NONBLOCK);
2019 if (ret < 0) {
2020 dbgarg(cmd, "no pending events?");
2021 break;
2022 }
2023 dbgarg(cmd,
2024 "pending=%d, type=0x%8.8x, sequence=%d, "
2025 "timestamp=%lu.%9.9lu ",
2026 ev->pending, ev->type, ev->sequence,
2027 ev->timestamp.tv_sec, ev->timestamp.tv_nsec);
2028 break;
2029 }
2030 case VIDIOC_SUBSCRIBE_EVENT:
2031 {
2032 struct v4l2_event_subscription *sub = arg;
2033
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002034 ret = ops->vidioc_subscribe_event(fh, sub);
2035 if (ret < 0) {
2036 dbgarg(cmd, "failed, ret=%ld", ret);
2037 break;
2038 }
2039 dbgarg(cmd, "type=0x%8.8x", sub->type);
2040 break;
2041 }
2042 case VIDIOC_UNSUBSCRIBE_EVENT:
2043 {
2044 struct v4l2_event_subscription *sub = arg;
2045
Sakari Ailusd3d7c962010-03-27 11:02:10 -03002046 ret = ops->vidioc_unsubscribe_event(fh, sub);
2047 if (ret < 0) {
2048 dbgarg(cmd, "failed, ret=%ld", ret);
2049 break;
2050 }
2051 dbgarg(cmd, "type=0x%8.8x", sub->type);
2052 break;
2053 }
Guennadi Liakhovetski21501582011-09-28 11:34:06 -03002054 case VIDIOC_CREATE_BUFS:
2055 {
2056 struct v4l2_create_buffers *create = arg;
2057
Guennadi Liakhovetski21501582011-09-28 11:34:06 -03002058 ret = check_fmt(ops, create->format.type);
2059 if (ret)
2060 break;
2061
2062 ret = ops->vidioc_create_bufs(file, fh, create);
2063
2064 dbgarg(cmd, "count=%d @ %d\n", create->count, create->index);
2065 break;
2066 }
2067 case VIDIOC_PREPARE_BUF:
2068 {
2069 struct v4l2_buffer *b = arg;
2070
Guennadi Liakhovetski21501582011-09-28 11:34:06 -03002071 ret = check_fmt(ops, b->type);
2072 if (ret)
2073 break;
2074
2075 ret = ops->vidioc_prepare_buf(file, fh, b);
2076
2077 dbgarg(cmd, "index=%d", b->index);
2078 break;
2079 }
Hans Verkuila3998102008-07-21 02:57:38 -03002080 default:
Hans Verkuila3998102008-07-21 02:57:38 -03002081 if (!ops->vidioc_default)
2082 break;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002083 ret = ops->vidioc_default(file, fh, use_fh_prio ?
2084 v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2085 cmd, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002086 break;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002087 } /* switch */
2088
Hans Verkuil86748f32012-06-22 06:04:16 -03002089done:
2090 if (vfd->debug) {
2091 if (write_only && vfd->debug > V4L2_DEBUG_IOCTL) {
2092 if (ret < 0)
2093 printk(KERN_DEBUG "%s: error %ld\n",
2094 video_device_node_name(vfd), ret);
2095 return ret;
2096 }
2097 v4l_print_ioctl(vfd->name, cmd);
2098 if (ret < 0)
2099 pr_cont(": error %ld\n", ret);
2100 else if (vfd->debug == V4L2_DEBUG_IOCTL)
2101 pr_cont("\n");
2102 else if (!info->debug)
2103 return ret;
2104 else if (_IOC_DIR(cmd) == _IOC_NONE)
2105 info->debug(arg, write_only);
2106 else {
2107 pr_cont(": ");
2108 info->debug(arg, write_only);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002109 }
2110 }
2111
2112 return ret;
2113}
2114
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002115static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2116 void * __user *user_ptr, void ***kernel_ptr)
2117{
2118 int ret = 0;
2119
2120 switch (cmd) {
2121 case VIDIOC_QUERYBUF:
2122 case VIDIOC_QBUF:
2123 case VIDIOC_DQBUF: {
2124 struct v4l2_buffer *buf = parg;
2125
2126 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2127 if (buf->length > VIDEO_MAX_PLANES) {
2128 ret = -EINVAL;
2129 break;
2130 }
2131 *user_ptr = (void __user *)buf->m.planes;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002132 *kernel_ptr = (void *)&buf->m.planes;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002133 *array_size = sizeof(struct v4l2_plane) * buf->length;
2134 ret = 1;
2135 }
2136 break;
2137 }
2138
2139 case VIDIOC_S_EXT_CTRLS:
2140 case VIDIOC_G_EXT_CTRLS:
2141 case VIDIOC_TRY_EXT_CTRLS: {
2142 struct v4l2_ext_controls *ctrls = parg;
2143
2144 if (ctrls->count != 0) {
Dan Carpenter6c061082012-01-05 02:27:57 -03002145 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2146 ret = -EINVAL;
2147 break;
2148 }
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002149 *user_ptr = (void __user *)ctrls->controls;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002150 *kernel_ptr = (void *)&ctrls->controls;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002151 *array_size = sizeof(struct v4l2_ext_control)
2152 * ctrls->count;
2153 ret = 1;
2154 }
2155 break;
2156 }
2157 }
2158
2159 return ret;
2160}
2161
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002162long
2163video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2164 v4l2_kioctl func)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002165{
2166 char sbuf[128];
2167 void *mbuf = NULL;
Hans Verkuil1d94aa32010-04-06 08:12:21 -03002168 void *parg = (void *)arg;
Hans Verkuil069b7472008-12-30 07:04:34 -03002169 long err = -EINVAL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002170 bool has_array_args;
2171 size_t array_size = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002172 void __user *user_ptr = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002173 void **kernel_ptr = NULL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002174
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002175 /* Copy arguments into temp kernel buffer */
Trent Piepho337f9d22009-03-04 01:21:02 -03002176 if (_IOC_DIR(cmd) != _IOC_NONE) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002177 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2178 parg = sbuf;
2179 } else {
2180 /* too big to allocate from stack */
2181 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2182 if (NULL == mbuf)
2183 return -ENOMEM;
2184 parg = mbuf;
2185 }
2186
2187 err = -EFAULT;
Trent Piepho19c96e42009-03-04 01:21:02 -03002188 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002189 unsigned int n = _IOC_SIZE(cmd);
2190
2191 /*
2192 * In some cases, only a few fields are used as input,
2193 * i.e. when the app sets "index" and then the driver
2194 * fills in the rest of the structure for the thing
2195 * with that index. We only need to copy up the first
2196 * non-input field.
2197 */
2198 if (v4l2_is_known_ioctl(cmd)) {
2199 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2200 if (flags & INFO_FL_CLEAR_MASK)
2201 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2202 }
Trent Piepho19c96e42009-03-04 01:21:02 -03002203
2204 if (copy_from_user(parg, (void __user *)arg, n))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002205 goto out;
Trent Piepho19c96e42009-03-04 01:21:02 -03002206
2207 /* zero out anything we don't copy from userspace */
2208 if (n < _IOC_SIZE(cmd))
2209 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
Trent Piepho337f9d22009-03-04 01:21:02 -03002210 } else {
2211 /* read-only ioctl */
2212 memset(parg, 0, _IOC_SIZE(cmd));
Trent Piepho19c96e42009-03-04 01:21:02 -03002213 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002214 }
2215
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002216 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2217 if (err < 0)
2218 goto out;
2219 has_array_args = err;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002220
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002221 if (has_array_args) {
2222 /*
2223 * When adding new types of array args, make sure that the
2224 * parent argument to ioctl (which contains the pointer to the
2225 * array) fits into sbuf (so that mbuf will still remain
2226 * unused up to here).
2227 */
2228 mbuf = kmalloc(array_size, GFP_KERNEL);
2229 err = -ENOMEM;
2230 if (NULL == mbuf)
2231 goto out_array_args;
2232 err = -EFAULT;
2233 if (copy_from_user(mbuf, user_ptr, array_size))
2234 goto out_array_args;
2235 *kernel_ptr = mbuf;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002236 }
2237
2238 /* Handles IOCTL */
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002239 err = func(file, cmd, parg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002240 if (err == -ENOIOCTLCMD)
Hans Verkuil02bbb812012-02-08 08:09:36 -03002241 err = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002242
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002243 if (has_array_args) {
2244 *kernel_ptr = user_ptr;
2245 if (copy_to_user(user_ptr, mbuf, array_size))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002246 err = -EFAULT;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002247 goto out_array_args;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002248 }
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002249 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2250 results that must be returned. */
2251 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002252 goto out;
2253
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002254out_array_args:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002255 /* Copy results into user buffer */
2256 switch (_IOC_DIR(cmd)) {
2257 case _IOC_READ:
2258 case (_IOC_WRITE | _IOC_READ):
2259 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2260 err = -EFAULT;
2261 break;
2262 }
2263
2264out:
2265 kfree(mbuf);
2266 return err;
2267}
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002268EXPORT_SYMBOL(video_usercopy);
2269
2270long video_ioctl2(struct file *file,
2271 unsigned int cmd, unsigned long arg)
2272{
2273 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2274}
Mauro Carvalho Chehab8a522c92008-10-21 11:58:39 -03002275EXPORT_SYMBOL(video_ioctl2);