blob: 29c751d86061d400b0faf9a164d616457c92ee05 [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 Verkuil5a5adf62012-06-22 07:29:35 -030030#include <media/videobuf2-core.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030031
Lucas De Marchi25985ed2011-03-30 22:57:33 -030032/* Zero out the end of the struct pointed to by p. Everything after, but
Trent Piepho7ecc0cf2009-04-30 21:03:34 -030033 * not including, the specified field is cleared. */
34#define CLEAR_AFTER_FIELD(p, field) \
35 memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
36 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
37
Hans Verkuil35ea11f2008-07-20 08:12:02 -030038struct std_descr {
39 v4l2_std_id std;
40 const char *descr;
41};
42
43static const struct std_descr standards[] = {
44 { V4L2_STD_NTSC, "NTSC" },
45 { V4L2_STD_NTSC_M, "NTSC-M" },
46 { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
47 { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
48 { V4L2_STD_NTSC_443, "NTSC-443" },
49 { V4L2_STD_PAL, "PAL" },
50 { V4L2_STD_PAL_BG, "PAL-BG" },
51 { V4L2_STD_PAL_B, "PAL-B" },
52 { V4L2_STD_PAL_B1, "PAL-B1" },
53 { V4L2_STD_PAL_G, "PAL-G" },
54 { V4L2_STD_PAL_H, "PAL-H" },
55 { V4L2_STD_PAL_I, "PAL-I" },
56 { V4L2_STD_PAL_DK, "PAL-DK" },
57 { V4L2_STD_PAL_D, "PAL-D" },
58 { V4L2_STD_PAL_D1, "PAL-D1" },
59 { V4L2_STD_PAL_K, "PAL-K" },
60 { V4L2_STD_PAL_M, "PAL-M" },
61 { V4L2_STD_PAL_N, "PAL-N" },
62 { V4L2_STD_PAL_Nc, "PAL-Nc" },
63 { V4L2_STD_PAL_60, "PAL-60" },
64 { V4L2_STD_SECAM, "SECAM" },
65 { V4L2_STD_SECAM_B, "SECAM-B" },
66 { V4L2_STD_SECAM_G, "SECAM-G" },
67 { V4L2_STD_SECAM_H, "SECAM-H" },
68 { V4L2_STD_SECAM_DK, "SECAM-DK" },
69 { V4L2_STD_SECAM_D, "SECAM-D" },
70 { V4L2_STD_SECAM_K, "SECAM-K" },
71 { V4L2_STD_SECAM_K1, "SECAM-K1" },
72 { V4L2_STD_SECAM_L, "SECAM-L" },
73 { V4L2_STD_SECAM_LC, "SECAM-Lc" },
74 { 0, "Unknown" }
75};
76
77/* video4linux standard ID conversion to standard name
78 */
79const char *v4l2_norm_to_name(v4l2_std_id id)
80{
81 u32 myid = id;
82 int i;
83
84 /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
85 64 bit comparations. So, on that architecture, with some gcc
86 variants, compilation fails. Currently, the max value is 30bit wide.
87 */
88 BUG_ON(myid != id);
89
90 for (i = 0; standards[i].std; i++)
91 if (myid == standards[i].std)
92 break;
93 return standards[i].descr;
94}
95EXPORT_SYMBOL(v4l2_norm_to_name);
96
Trent Piepho51f0b8d52009-03-04 01:21:02 -030097/* Returns frame period for the given standard */
98void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
99{
100 if (id & V4L2_STD_525_60) {
101 frameperiod->numerator = 1001;
102 frameperiod->denominator = 30000;
103 } else {
104 frameperiod->numerator = 1;
105 frameperiod->denominator = 25;
106 }
107}
108EXPORT_SYMBOL(v4l2_video_std_frame_period);
109
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300110/* Fill in the fields of a v4l2_standard structure according to the
111 'id' and 'transmission' parameters. Returns negative on error. */
112int v4l2_video_std_construct(struct v4l2_standard *vs,
113 int id, const char *name)
114{
Trent Piepho51f0b8d52009-03-04 01:21:02 -0300115 vs->id = id;
116 v4l2_video_std_frame_period(id, &vs->frameperiod);
117 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300118 strlcpy(vs->name, name, sizeof(vs->name));
119 return 0;
120}
121EXPORT_SYMBOL(v4l2_video_std_construct);
122
123/* ----------------------------------------------------------------- */
124/* some arrays for pretty-printing debug messages of enum types */
125
126const char *v4l2_field_names[] = {
127 [V4L2_FIELD_ANY] = "any",
128 [V4L2_FIELD_NONE] = "none",
129 [V4L2_FIELD_TOP] = "top",
130 [V4L2_FIELD_BOTTOM] = "bottom",
131 [V4L2_FIELD_INTERLACED] = "interlaced",
132 [V4L2_FIELD_SEQ_TB] = "seq-tb",
133 [V4L2_FIELD_SEQ_BT] = "seq-bt",
134 [V4L2_FIELD_ALTERNATE] = "alternate",
135 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
136 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
137};
138EXPORT_SYMBOL(v4l2_field_names);
139
140const char *v4l2_type_names[] = {
141 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
142 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
143 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
144 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
145 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
146 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
147 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
148 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
Pawel Osciakf8f39142010-07-29 14:44:25 -0300149 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
150 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300151};
152EXPORT_SYMBOL(v4l2_type_names);
153
154static const char *v4l2_memory_names[] = {
155 [V4L2_MEMORY_MMAP] = "mmap",
156 [V4L2_MEMORY_USERPTR] = "userptr",
157 [V4L2_MEMORY_OVERLAY] = "overlay",
Sumit Semwal051c7782012-06-14 10:37:35 -0300158 [V4L2_MEMORY_DMABUF] = "dmabuf",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300159};
160
Hans Verkuild9246242012-10-02 02:47:59 -0300161#define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300162
163/* ------------------------------------------------------------------ */
164/* debug help functions */
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300165
Hans Verkuil59076122012-06-22 06:09:54 -0300166static void v4l_print_querycap(const void *arg, bool write_only)
167{
168 const struct v4l2_capability *p = arg;
169
Hans Verkuil27d5a872013-03-18 11:02:22 -0300170 pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, "
Hans Verkuil59076122012-06-22 06:09:54 -0300171 "capabilities=0x%08x, device_caps=0x%08x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300172 (int)sizeof(p->driver), p->driver,
173 (int)sizeof(p->card), p->card,
174 (int)sizeof(p->bus_info), p->bus_info,
Hans Verkuil59076122012-06-22 06:09:54 -0300175 p->version, p->capabilities, p->device_caps);
176}
177
178static void v4l_print_enuminput(const void *arg, bool write_only)
179{
180 const struct v4l2_input *p = arg;
181
Hans Verkuil27d5a872013-03-18 11:02:22 -0300182 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, "
Hans Verkuil59076122012-06-22 06:09:54 -0300183 "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300184 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
185 p->tuner, (unsigned long long)p->std, p->status,
186 p->capabilities);
Hans Verkuil59076122012-06-22 06:09:54 -0300187}
188
189static void v4l_print_enumoutput(const void *arg, bool write_only)
190{
191 const struct v4l2_output *p = arg;
192
Hans Verkuil27d5a872013-03-18 11:02:22 -0300193 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, "
Hans Verkuil59076122012-06-22 06:09:54 -0300194 "modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300195 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
196 p->modulator, (unsigned long long)p->std, p->capabilities);
Hans Verkuil59076122012-06-22 06:09:54 -0300197}
198
199static void v4l_print_audio(const void *arg, bool write_only)
200{
201 const struct v4l2_audio *p = arg;
202
203 if (write_only)
204 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
205 else
Hans Verkuil27d5a872013-03-18 11:02:22 -0300206 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
207 p->index, (int)sizeof(p->name), p->name,
208 p->capability, p->mode);
Hans Verkuil59076122012-06-22 06:09:54 -0300209}
210
211static void v4l_print_audioout(const void *arg, bool write_only)
212{
213 const struct v4l2_audioout *p = arg;
214
215 if (write_only)
216 pr_cont("index=%u\n", p->index);
217 else
Hans Verkuil27d5a872013-03-18 11:02:22 -0300218 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
219 p->index, (int)sizeof(p->name), p->name,
220 p->capability, p->mode);
Hans Verkuil59076122012-06-22 06:09:54 -0300221}
222
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300223static void v4l_print_fmtdesc(const void *arg, bool write_only)
224{
225 const struct v4l2_fmtdesc *p = arg;
226
Hans Verkuil27d5a872013-03-18 11:02:22 -0300227 pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%.*s'\n",
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300228 p->index, prt_names(p->type, v4l2_type_names),
229 p->flags, (p->pixelformat & 0xff),
230 (p->pixelformat >> 8) & 0xff,
231 (p->pixelformat >> 16) & 0xff,
232 (p->pixelformat >> 24) & 0xff,
Hans Verkuil27d5a872013-03-18 11:02:22 -0300233 (int)sizeof(p->description), p->description);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300234}
235
236static void v4l_print_format(const void *arg, bool write_only)
237{
238 const struct v4l2_format *p = arg;
239 const struct v4l2_pix_format *pix;
240 const struct v4l2_pix_format_mplane *mp;
241 const struct v4l2_vbi_format *vbi;
242 const struct v4l2_sliced_vbi_format *sliced;
243 const struct v4l2_window *win;
244 const struct v4l2_clip *clip;
245 unsigned i;
246
247 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
248 switch (p->type) {
249 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
250 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
251 pix = &p->fmt.pix;
252 pr_cont(", width=%u, height=%u, "
253 "pixelformat=%c%c%c%c, field=%s, "
254 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
255 pix->width, pix->height,
256 (pix->pixelformat & 0xff),
257 (pix->pixelformat >> 8) & 0xff,
258 (pix->pixelformat >> 16) & 0xff,
259 (pix->pixelformat >> 24) & 0xff,
260 prt_names(pix->field, v4l2_field_names),
261 pix->bytesperline, pix->sizeimage,
262 pix->colorspace);
263 break;
264 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
265 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
266 mp = &p->fmt.pix_mp;
267 pr_cont(", width=%u, height=%u, "
268 "format=%c%c%c%c, field=%s, "
269 "colorspace=%d, num_planes=%u\n",
270 mp->width, mp->height,
271 (mp->pixelformat & 0xff),
272 (mp->pixelformat >> 8) & 0xff,
273 (mp->pixelformat >> 16) & 0xff,
274 (mp->pixelformat >> 24) & 0xff,
275 prt_names(mp->field, v4l2_field_names),
276 mp->colorspace, mp->num_planes);
277 for (i = 0; i < mp->num_planes; i++)
278 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
279 mp->plane_fmt[i].bytesperline,
280 mp->plane_fmt[i].sizeimage);
281 break;
282 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
283 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
284 win = &p->fmt.win;
285 pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, "
286 "chromakey=0x%08x, bitmap=%p, "
287 "global_alpha=0x%02x\n",
288 win->w.width, win->w.height,
289 win->w.left, win->w.top,
290 prt_names(win->field, v4l2_field_names),
291 win->chromakey, win->bitmap, win->global_alpha);
292 clip = win->clips;
293 for (i = 0; i < win->clipcount; i++) {
294 printk(KERN_DEBUG "clip %u: wxh=%dx%d, x,y=%d,%d\n",
295 i, clip->c.width, clip->c.height,
296 clip->c.left, clip->c.top);
297 clip = clip->next;
298 }
299 break;
300 case V4L2_BUF_TYPE_VBI_CAPTURE:
301 case V4L2_BUF_TYPE_VBI_OUTPUT:
302 vbi = &p->fmt.vbi;
303 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
304 "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
305 vbi->sampling_rate, vbi->offset,
306 vbi->samples_per_line,
307 (vbi->sample_format & 0xff),
308 (vbi->sample_format >> 8) & 0xff,
309 (vbi->sample_format >> 16) & 0xff,
310 (vbi->sample_format >> 24) & 0xff,
311 vbi->start[0], vbi->start[1],
312 vbi->count[0], vbi->count[1]);
313 break;
314 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
315 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
316 sliced = &p->fmt.sliced;
317 pr_cont(", service_set=0x%08x, io_size=%d\n",
318 sliced->service_set, sliced->io_size);
319 for (i = 0; i < 24; i++)
320 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
321 sliced->service_lines[0][i],
322 sliced->service_lines[1][i]);
323 break;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300324 }
325}
326
327static void v4l_print_framebuffer(const void *arg, bool write_only)
328{
329 const struct v4l2_framebuffer *p = arg;
330
331 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
332 "height=%u, pixelformat=%c%c%c%c, "
333 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
334 p->capability, p->flags, p->base,
335 p->fmt.width, p->fmt.height,
336 (p->fmt.pixelformat & 0xff),
337 (p->fmt.pixelformat >> 8) & 0xff,
338 (p->fmt.pixelformat >> 16) & 0xff,
339 (p->fmt.pixelformat >> 24) & 0xff,
340 p->fmt.bytesperline, p->fmt.sizeimage,
341 p->fmt.colorspace);
342}
343
Hans Verkuile325b242012-06-09 12:50:15 -0300344static void v4l_print_buftype(const void *arg, bool write_only)
345{
346 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
347}
348
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300349static void v4l_print_modulator(const void *arg, bool write_only)
350{
351 const struct v4l2_modulator *p = arg;
352
353 if (write_only)
354 pr_cont("index=%u, txsubchans=0x%x", p->index, p->txsubchans);
355 else
Hans Verkuil27d5a872013-03-18 11:02:22 -0300356 pr_cont("index=%u, name=%.*s, capability=0x%x, "
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300357 "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300358 p->index, (int)sizeof(p->name), p->name, p->capability,
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300359 p->rangelow, p->rangehigh, p->txsubchans);
360}
361
362static void v4l_print_tuner(const void *arg, bool write_only)
363{
364 const struct v4l2_tuner *p = arg;
365
366 if (write_only)
367 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
368 else
Hans Verkuil27d5a872013-03-18 11:02:22 -0300369 pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, "
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300370 "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
371 "rxsubchans=0x%x, audmode=%u\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300372 p->index, (int)sizeof(p->name), p->name, p->type,
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300373 p->capability, p->rangelow,
374 p->rangehigh, p->signal, p->afc,
375 p->rxsubchans, p->audmode);
376}
377
378static void v4l_print_frequency(const void *arg, bool write_only)
379{
380 const struct v4l2_frequency *p = arg;
381
382 pr_cont("tuner=%u, type=%u, frequency=%u\n",
383 p->tuner, p->type, p->frequency);
384}
385
386static void v4l_print_standard(const void *arg, bool write_only)
387{
388 const struct v4l2_standard *p = arg;
389
Hans Verkuil27d5a872013-03-18 11:02:22 -0300390 pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, "
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300391 "framelines=%u\n", p->index,
Hans Verkuil27d5a872013-03-18 11:02:22 -0300392 (unsigned long long)p->id, (int)sizeof(p->name), p->name,
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300393 p->frameperiod.numerator,
394 p->frameperiod.denominator,
395 p->framelines);
396}
397
398static void v4l_print_std(const void *arg, bool write_only)
399{
400 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
401}
402
403static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
404{
405 const struct v4l2_hw_freq_seek *p = arg;
406
Hans Verkuil5e7883172012-08-01 16:18:41 -0300407 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, "
408 "rangelow=%u, rangehigh=%u\n",
409 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
410 p->rangelow, p->rangehigh);
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300411}
412
Hans Verkuileb3728e2012-06-22 06:23:59 -0300413static void v4l_print_requestbuffers(const void *arg, bool write_only)
Hans Verkuil59076122012-06-22 06:09:54 -0300414{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300415 const struct v4l2_requestbuffers *p = arg;
416
417 pr_cont("count=%d, type=%s, memory=%s\n",
418 p->count,
419 prt_names(p->type, v4l2_type_names),
420 prt_names(p->memory, v4l2_memory_names));
Hans Verkuil59076122012-06-22 06:09:54 -0300421}
422
Hans Verkuileb3728e2012-06-22 06:23:59 -0300423static void v4l_print_buffer(const void *arg, bool write_only)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300424{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300425 const struct v4l2_buffer *p = arg;
426 const struct v4l2_timecode *tc = &p->timecode;
427 const struct v4l2_plane *plane;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300428 int i;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300429
Hans Verkuileb3728e2012-06-22 06:23:59 -0300430 pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
431 "flags=0x%08x, field=%s, sequence=%d, memory=%s",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300432 p->timestamp.tv_sec / 3600,
433 (int)(p->timestamp.tv_sec / 60) % 60,
434 (int)(p->timestamp.tv_sec % 60),
Alexander Beregalovb0459792008-09-03 16:47:38 -0300435 (long)p->timestamp.tv_usec,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300436 p->index,
437 prt_names(p->type, v4l2_type_names),
Hans Verkuileb3728e2012-06-22 06:23:59 -0300438 p->flags, prt_names(p->field, v4l2_field_names),
439 p->sequence, prt_names(p->memory, v4l2_memory_names));
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300440
441 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300442 pr_cont("\n");
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300443 for (i = 0; i < p->length; ++i) {
444 plane = &p->m.planes[i];
Hans Verkuileb3728e2012-06-22 06:23:59 -0300445 printk(KERN_DEBUG
446 "plane %d: bytesused=%d, data_offset=0x%08x "
447 "offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300448 i, plane->bytesused, plane->data_offset,
449 plane->m.userptr, plane->length);
450 }
451 } else {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300452 pr_cont("bytesused=%d, offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300453 p->bytesused, p->m.userptr, p->length);
454 }
455
Hans Verkuileb3728e2012-06-22 06:23:59 -0300456 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
457 "flags=0x%08x, frames=%d, userbits=0x%08x\n",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300458 tc->hours, tc->minutes, tc->seconds,
459 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
460}
461
Tomasz Stanislawskib799d092012-06-14 11:32:23 -0300462static void v4l_print_exportbuffer(const void *arg, bool write_only)
463{
464 const struct v4l2_exportbuffer *p = arg;
465
466 pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
467 p->fd, prt_names(p->type, v4l2_type_names),
468 p->index, p->plane, p->flags);
469}
470
Hans Verkuileb3728e2012-06-22 06:23:59 -0300471static void v4l_print_create_buffers(const void *arg, bool write_only)
472{
473 const struct v4l2_create_buffers *p = arg;
474
475 pr_cont("index=%d, count=%d, memory=%s, ",
476 p->index, p->count,
477 prt_names(p->memory, v4l2_memory_names));
478 v4l_print_format(&p->format, write_only);
479}
480
481static void v4l_print_streamparm(const void *arg, bool write_only)
482{
483 const struct v4l2_streamparm *p = arg;
484
485 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
486
487 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
488 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
489 const struct v4l2_captureparm *c = &p->parm.capture;
490
491 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
492 "extendedmode=%d, readbuffers=%d\n",
493 c->capability, c->capturemode,
494 c->timeperframe.numerator, c->timeperframe.denominator,
495 c->extendedmode, c->readbuffers);
496 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
497 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
498 const struct v4l2_outputparm *c = &p->parm.output;
499
500 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
501 "extendedmode=%d, writebuffers=%d\n",
502 c->capability, c->outputmode,
503 c->timeperframe.numerator, c->timeperframe.denominator,
504 c->extendedmode, c->writebuffers);
505 }
506}
507
Hans Verkuilefbceec2012-06-09 12:54:02 -0300508static void v4l_print_queryctrl(const void *arg, bool write_only)
509{
510 const struct v4l2_queryctrl *p = arg;
511
Hans Verkuil27d5a872013-03-18 11:02:22 -0300512 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, "
Hans Verkuilefbceec2012-06-09 12:54:02 -0300513 "step=%d, default=%d, flags=0x%08x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300514 p->id, p->type, (int)sizeof(p->name), p->name,
Hans Verkuilefbceec2012-06-09 12:54:02 -0300515 p->minimum, p->maximum,
516 p->step, p->default_value, p->flags);
517}
518
519static void v4l_print_querymenu(const void *arg, bool write_only)
520{
521 const struct v4l2_querymenu *p = arg;
522
523 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
524}
525
526static void v4l_print_control(const void *arg, bool write_only)
527{
528 const struct v4l2_control *p = arg;
529
530 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
531}
532
533static void v4l_print_ext_controls(const void *arg, bool write_only)
534{
535 const struct v4l2_ext_controls *p = arg;
536 int i;
537
538 pr_cont("class=0x%x, count=%d, error_idx=%d",
539 p->ctrl_class, p->count, p->error_idx);
540 for (i = 0; i < p->count; i++) {
541 if (p->controls[i].size)
542 pr_cont(", id/val=0x%x/0x%x",
543 p->controls[i].id, p->controls[i].value);
544 else
545 pr_cont(", id/size=0x%x/%u",
546 p->controls[i].id, p->controls[i].size);
547 }
548 pr_cont("\n");
549}
550
Hans Verkuild84f2d942012-06-09 11:57:46 -0300551static void v4l_print_cropcap(const void *arg, bool write_only)
552{
553 const struct v4l2_cropcap *p = arg;
554
555 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
556 "defrect wxh=%dx%d, x,y=%d,%d\n, "
557 "pixelaspect %d/%d\n",
558 prt_names(p->type, v4l2_type_names),
559 p->bounds.width, p->bounds.height,
560 p->bounds.left, p->bounds.top,
561 p->defrect.width, p->defrect.height,
562 p->defrect.left, p->defrect.top,
563 p->pixelaspect.numerator, p->pixelaspect.denominator);
564}
565
566static void v4l_print_crop(const void *arg, bool write_only)
567{
568 const struct v4l2_crop *p = arg;
569
570 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
571 prt_names(p->type, v4l2_type_names),
572 p->c.width, p->c.height,
573 p->c.left, p->c.top);
574}
575
576static void v4l_print_selection(const void *arg, bool write_only)
577{
578 const struct v4l2_selection *p = arg;
579
580 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
581 prt_names(p->type, v4l2_type_names),
582 p->target, p->flags,
583 p->r.width, p->r.height, p->r.left, p->r.top);
584}
585
Hans Verkuild806f2d2012-06-09 10:02:49 -0300586static void v4l_print_jpegcompression(const void *arg, bool write_only)
587{
588 const struct v4l2_jpegcompression *p = arg;
589
590 pr_cont("quality=%d, APPn=%d, APP_len=%d, "
591 "COM_len=%d, jpeg_markers=0x%x\n",
592 p->quality, p->APPn, p->APP_len,
593 p->COM_len, p->jpeg_markers);
594}
595
596static void v4l_print_enc_idx(const void *arg, bool write_only)
597{
598 const struct v4l2_enc_idx *p = arg;
599
600 pr_cont("entries=%d, entries_cap=%d\n",
601 p->entries, p->entries_cap);
602}
603
604static void v4l_print_encoder_cmd(const void *arg, bool write_only)
605{
606 const struct v4l2_encoder_cmd *p = arg;
607
608 pr_cont("cmd=%d, flags=0x%x\n",
609 p->cmd, p->flags);
610}
611
612static void v4l_print_decoder_cmd(const void *arg, bool write_only)
613{
614 const struct v4l2_decoder_cmd *p = arg;
615
616 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
617
618 if (p->cmd == V4L2_DEC_CMD_START)
619 pr_info("speed=%d, format=%u\n",
620 p->start.speed, p->start.format);
621 else if (p->cmd == V4L2_DEC_CMD_STOP)
622 pr_info("pts=%llu\n", p->stop.pts);
623}
624
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300625static void v4l_print_dbg_chip_ident(const void *arg, bool write_only)
626{
627 const struct v4l2_dbg_chip_ident *p = arg;
628
629 pr_cont("type=%u, ", p->match.type);
630 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
Hans Verkuil27d5a872013-03-18 11:02:22 -0300631 pr_cont("name=%.*s, ",
632 (int)sizeof(p->match.name), p->match.name);
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300633 else
634 pr_cont("addr=%u, ", p->match.addr);
635 pr_cont("chip_ident=%u, revision=0x%x\n",
636 p->ident, p->revision);
637}
638
639static void v4l_print_dbg_register(const void *arg, bool write_only)
640{
641 const struct v4l2_dbg_register *p = arg;
642
643 pr_cont("type=%u, ", p->match.type);
644 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
Hans Verkuil27d5a872013-03-18 11:02:22 -0300645 pr_cont("name=%.*s, ",
646 (int)sizeof(p->match.name), p->match.name);
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300647 else
648 pr_cont("addr=%u, ", p->match.addr);
649 pr_cont("reg=0x%llx, val=0x%llx\n",
650 p->reg, p->val);
651}
652
Hans Verkuilefc626b2012-06-09 10:09:07 -0300653static void v4l_print_dv_enum_presets(const void *arg, bool write_only)
Hans Verkuileb3728e2012-06-22 06:23:59 -0300654{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300655 const struct v4l2_dv_enum_preset *p = arg;
656
Hans Verkuil27d5a872013-03-18 11:02:22 -0300657 pr_cont("index=%u, preset=%u, name=%.*s, width=%u, height=%u\n",
658 p->index, p->preset,
659 (int)sizeof(p->name), p->name, p->width, p->height);
Hans Verkuileb3728e2012-06-22 06:23:59 -0300660}
661
Hans Verkuilefc626b2012-06-09 10:09:07 -0300662static void v4l_print_dv_preset(const void *arg, bool write_only)
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300663{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300664 const struct v4l2_dv_preset *p = arg;
665
666 pr_cont("preset=%u\n", p->preset);
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300667}
668
Hans Verkuilefc626b2012-06-09 10:09:07 -0300669static void v4l_print_dv_timings(const void *arg, bool write_only)
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300670{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300671 const struct v4l2_dv_timings *p = arg;
672
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300673 switch (p->type) {
674 case V4L2_DV_BT_656_1120:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300675 pr_cont("type=bt-656/1120, interlaced=%u, "
676 "pixelclock=%llu, "
677 "width=%u, height=%u, polarities=0x%x, "
678 "hfrontporch=%u, hsync=%u, "
679 "hbackporch=%u, vfrontporch=%u, "
680 "vsync=%u, vbackporch=%u, "
681 "il_vfrontporch=%u, il_vsync=%u, "
682 "il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300683 p->bt.interlaced, p->bt.pixelclock,
684 p->bt.width, p->bt.height,
685 p->bt.polarities, p->bt.hfrontporch,
686 p->bt.hsync, p->bt.hbackporch,
687 p->bt.vfrontporch, p->bt.vsync,
688 p->bt.vbackporch, p->bt.il_vfrontporch,
689 p->bt.il_vsync, p->bt.il_vbackporch,
690 p->bt.standards, p->bt.flags);
691 break;
692 default:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300693 pr_cont("type=%d\n", p->type);
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300694 break;
695 }
696}
697
Hans Verkuilefc626b2012-06-09 10:09:07 -0300698static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
699{
700 const struct v4l2_enum_dv_timings *p = arg;
701
702 pr_cont("index=%u, ", p->index);
703 v4l_print_dv_timings(&p->timings, write_only);
704}
705
706static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
707{
708 const struct v4l2_dv_timings_cap *p = arg;
709
710 switch (p->type) {
711 case V4L2_DV_BT_656_1120:
712 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
713 "pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
714 p->bt.min_width, p->bt.max_width,
715 p->bt.min_height, p->bt.max_height,
716 p->bt.min_pixelclock, p->bt.max_pixelclock,
717 p->bt.standards, p->bt.capabilities);
718 break;
719 default:
720 pr_cont("type=%u\n", p->type);
721 break;
722 }
723}
724
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300725static void v4l_print_frmsizeenum(const void *arg, bool write_only)
726{
727 const struct v4l2_frmsizeenum *p = arg;
728
729 pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
730 p->index,
731 (p->pixel_format & 0xff),
732 (p->pixel_format >> 8) & 0xff,
733 (p->pixel_format >> 16) & 0xff,
734 (p->pixel_format >> 24) & 0xff,
735 p->type);
736 switch (p->type) {
737 case V4L2_FRMSIZE_TYPE_DISCRETE:
738 pr_cont(" wxh=%ux%u\n",
739 p->discrete.width, p->discrete.height);
740 break;
741 case V4L2_FRMSIZE_TYPE_STEPWISE:
742 pr_cont(" min=%ux%u, max=%ux%u, step=%ux%u\n",
743 p->stepwise.min_width, p->stepwise.min_height,
744 p->stepwise.step_width, p->stepwise.step_height,
745 p->stepwise.max_width, p->stepwise.max_height);
746 break;
747 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
748 /* fall through */
749 default:
750 pr_cont("\n");
751 break;
752 }
753}
754
755static void v4l_print_frmivalenum(const void *arg, bool write_only)
756{
757 const struct v4l2_frmivalenum *p = arg;
758
759 pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
760 p->index,
761 (p->pixel_format & 0xff),
762 (p->pixel_format >> 8) & 0xff,
763 (p->pixel_format >> 16) & 0xff,
764 (p->pixel_format >> 24) & 0xff,
765 p->width, p->height, p->type);
766 switch (p->type) {
767 case V4L2_FRMIVAL_TYPE_DISCRETE:
768 pr_cont(" fps=%d/%d\n",
769 p->discrete.numerator,
770 p->discrete.denominator);
771 break;
772 case V4L2_FRMIVAL_TYPE_STEPWISE:
773 pr_cont(" min=%d/%d, max=%d/%d, step=%d/%d\n",
774 p->stepwise.min.numerator,
775 p->stepwise.min.denominator,
776 p->stepwise.max.numerator,
777 p->stepwise.max.denominator,
778 p->stepwise.step.numerator,
779 p->stepwise.step.denominator);
780 break;
781 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
782 /* fall through */
783 default:
784 pr_cont("\n");
785 break;
786 }
787}
788
789static void v4l_print_event(const void *arg, bool write_only)
790{
791 const struct v4l2_event *p = arg;
792 const struct v4l2_event_ctrl *c;
793
794 pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
795 "timestamp=%lu.%9.9lu\n",
796 p->type, p->pending, p->sequence, p->id,
797 p->timestamp.tv_sec, p->timestamp.tv_nsec);
798 switch (p->type) {
799 case V4L2_EVENT_VSYNC:
800 printk(KERN_DEBUG "field=%s\n",
801 prt_names(p->u.vsync.field, v4l2_field_names));
802 break;
803 case V4L2_EVENT_CTRL:
804 c = &p->u.ctrl;
805 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
806 c->changes, c->type);
807 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
808 pr_cont("value64=%lld, ", c->value64);
809 else
810 pr_cont("value=%d, ", c->value);
811 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d,"
812 " default_value=%d\n",
813 c->flags, c->minimum, c->maximum,
814 c->step, c->default_value);
815 break;
816 case V4L2_EVENT_FRAME_SYNC:
817 pr_cont("frame_sequence=%u\n",
818 p->u.frame_sync.frame_sequence);
819 break;
820 }
821}
822
823static void v4l_print_event_subscription(const void *arg, bool write_only)
824{
825 const struct v4l2_event_subscription *p = arg;
826
827 pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
828 p->type, p->id, p->flags);
829}
830
831static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
832{
833 const struct v4l2_sliced_vbi_cap *p = arg;
834 int i;
835
836 pr_cont("type=%s, service_set=0x%08x\n",
837 prt_names(p->type, v4l2_type_names), p->service_set);
838 for (i = 0; i < 24; i++)
839 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
840 p->service_lines[0][i],
841 p->service_lines[1][i]);
842}
843
Hans Verkuil82b655b2012-07-05 06:37:08 -0300844static void v4l_print_freq_band(const void *arg, bool write_only)
845{
846 const struct v4l2_frequency_band *p = arg;
847
848 pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
849 "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
850 p->tuner, p->type, p->index,
851 p->capability, p->rangelow,
852 p->rangehigh, p->modulation);
853}
854
Hans Verkuilefc626b2012-06-09 10:09:07 -0300855static void v4l_print_u32(const void *arg, bool write_only)
856{
857 pr_cont("value=%u\n", *(const u32 *)arg);
858}
859
860static void v4l_print_newline(const void *arg, bool write_only)
861{
862 pr_cont("\n");
863}
864
Hans Verkuilf18d8e02012-06-22 06:35:01 -0300865static void v4l_print_default(const void *arg, bool write_only)
866{
867 pr_cont("driver-specific ioctl\n");
868}
869
Hans Verkuilefbceec2012-06-09 12:54:02 -0300870static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300871{
872 __u32 i;
873
874 /* zero the reserved fields */
875 c->reserved[0] = c->reserved[1] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300876 for (i = 0; i < c->count; i++)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300877 c->controls[i].reserved2[0] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300878
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300879 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
880 when using extended controls.
881 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
882 is it allowed for backwards compatibility.
883 */
884 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
885 return 0;
886 /* Check that all controls are from the same control class. */
887 for (i = 0; i < c->count; i++) {
888 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
889 c->error_idx = i;
890 return 0;
891 }
892 }
893 return 1;
894}
895
Hans Verkuil4b202592012-09-14 07:03:35 -0300896static int check_fmt(struct file *file, enum v4l2_buf_type type)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300897{
Hans Verkuil4b202592012-09-14 07:03:35 -0300898 struct video_device *vfd = video_devdata(file);
899 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
900 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
901 bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
902 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
903 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
904
Hans Verkuila3998102008-07-21 02:57:38 -0300905 if (ops == NULL)
906 return -EINVAL;
907
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300908 switch (type) {
909 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300910 if (is_vid && is_rx &&
911 (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300912 return 0;
913 break;
914 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300915 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300916 return 0;
917 break;
918 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -0300919 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300920 return 0;
921 break;
922 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300923 if (is_vid && is_tx &&
924 (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300925 return 0;
926 break;
927 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300928 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300929 return 0;
930 break;
931 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -0300932 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300933 return 0;
934 break;
935 case V4L2_BUF_TYPE_VBI_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300936 if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300937 return 0;
938 break;
939 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300940 if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300941 return 0;
942 break;
943 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300944 if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300945 return 0;
946 break;
947 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300948 if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300949 return 0;
950 break;
Hans Verkuil633c98e2012-09-17 05:02:26 -0300951 default:
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300952 break;
953 }
954 return -EINVAL;
955}
956
Hans Verkuil59076122012-06-22 06:09:54 -0300957static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
958 struct file *file, void *fh, void *arg)
959{
960 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
961
962 cap->version = LINUX_VERSION_CODE;
963 return ops->vidioc_querycap(file, fh, cap);
964}
965
966static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
967 struct file *file, void *fh, void *arg)
968{
969 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
970}
971
972static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
973 struct file *file, void *fh, void *arg)
974{
975 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
976}
977
Hans Verkuild0547cb2012-06-09 09:27:10 -0300978static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
979 struct file *file, void *fh, void *arg)
980{
981 struct video_device *vfd;
982 u32 *p = arg;
983
984 if (ops->vidioc_g_priority)
985 return ops->vidioc_g_priority(file, fh, arg);
986 vfd = video_devdata(file);
987 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
988 return 0;
989}
990
991static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
992 struct file *file, void *fh, void *arg)
993{
994 struct video_device *vfd;
995 struct v4l2_fh *vfh;
996 u32 *p = arg;
997
998 if (ops->vidioc_s_priority)
999 return ops->vidioc_s_priority(file, fh, *p);
1000 vfd = video_devdata(file);
1001 vfh = file->private_data;
1002 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
1003}
1004
Hans Verkuil59076122012-06-22 06:09:54 -03001005static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1006 struct file *file, void *fh, void *arg)
1007{
1008 struct v4l2_input *p = arg;
1009
1010 /*
Hans Verkuil1c4f3c982012-09-03 10:38:41 -03001011 * We set the flags for CAP_PRESETS, CAP_DV_TIMINGS &
Hans Verkuil59076122012-06-22 06:09:54 -03001012 * CAP_STD here based on ioctl handler provided by the
1013 * driver. If the driver doesn't support these
1014 * for a specific input, it must override these flags.
1015 */
1016 if (ops->vidioc_s_std)
1017 p->capabilities |= V4L2_IN_CAP_STD;
1018 if (ops->vidioc_s_dv_preset)
1019 p->capabilities |= V4L2_IN_CAP_PRESETS;
1020 if (ops->vidioc_s_dv_timings)
Hans Verkuil1c4f3c982012-09-03 10:38:41 -03001021 p->capabilities |= V4L2_IN_CAP_DV_TIMINGS;
Hans Verkuil59076122012-06-22 06:09:54 -03001022
1023 return ops->vidioc_enum_input(file, fh, p);
1024}
1025
1026static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1027 struct file *file, void *fh, void *arg)
1028{
1029 struct v4l2_output *p = arg;
1030
1031 /*
Hans Verkuil1c4f3c982012-09-03 10:38:41 -03001032 * We set the flags for CAP_PRESETS, CAP_DV_TIMINGS &
Hans Verkuil59076122012-06-22 06:09:54 -03001033 * CAP_STD here based on ioctl handler provided by the
1034 * driver. If the driver doesn't support these
1035 * for a specific output, it must override these flags.
1036 */
1037 if (ops->vidioc_s_std)
1038 p->capabilities |= V4L2_OUT_CAP_STD;
1039 if (ops->vidioc_s_dv_preset)
1040 p->capabilities |= V4L2_OUT_CAP_PRESETS;
1041 if (ops->vidioc_s_dv_timings)
Hans Verkuil1c4f3c982012-09-03 10:38:41 -03001042 p->capabilities |= V4L2_OUT_CAP_DV_TIMINGS;
Hans Verkuil59076122012-06-22 06:09:54 -03001043
1044 return ops->vidioc_enum_output(file, fh, p);
1045}
1046
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001047static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1048 struct file *file, void *fh, void *arg)
1049{
1050 struct v4l2_fmtdesc *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001051 struct video_device *vfd = video_devdata(file);
1052 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1053 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001054
1055 switch (p->type) {
1056 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001057 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001058 break;
1059 return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1060 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001061 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001062 break;
1063 return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1064 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001065 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001066 break;
1067 return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1068 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001069 if (unlikely(!is_tx || !ops->vidioc_enum_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001070 break;
1071 return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1072 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001073 if (unlikely(!is_tx || !ops->vidioc_enum_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001074 break;
1075 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001076 }
1077 return -EINVAL;
1078}
1079
1080static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1081 struct file *file, void *fh, void *arg)
1082{
1083 struct v4l2_format *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001084 struct video_device *vfd = video_devdata(file);
1085 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1086 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1087 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001088
1089 switch (p->type) {
1090 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001091 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001092 break;
1093 return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1094 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001095 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001096 break;
1097 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1098 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001099 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001100 break;
1101 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001102 case V4L2_BUF_TYPE_VBI_CAPTURE:
1103 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_vbi_cap))
1104 break;
1105 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1106 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1107 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_cap))
1108 break;
1109 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001110 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001111 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001112 break;
1113 return ops->vidioc_g_fmt_vid_out(file, fh, arg);
1114 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001115 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001116 break;
1117 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1118 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001119 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001120 break;
1121 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001122 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001123 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001124 break;
1125 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001126 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001127 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001128 break;
1129 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001130 }
1131 return -EINVAL;
1132}
1133
1134static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1135 struct file *file, void *fh, void *arg)
1136{
1137 struct v4l2_format *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001138 struct video_device *vfd = video_devdata(file);
1139 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1140 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1141 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001142
1143 switch (p->type) {
1144 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001145 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001146 break;
1147 CLEAR_AFTER_FIELD(p, fmt.pix);
1148 return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1149 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001150 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001151 break;
1152 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1153 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1154 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001155 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001156 break;
1157 CLEAR_AFTER_FIELD(p, fmt.win);
1158 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001159 case V4L2_BUF_TYPE_VBI_CAPTURE:
1160 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_vbi_cap))
1161 break;
1162 CLEAR_AFTER_FIELD(p, fmt.vbi);
1163 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1164 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1165 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_cap))
1166 break;
1167 CLEAR_AFTER_FIELD(p, fmt.sliced);
1168 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001169 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001170 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001171 break;
1172 CLEAR_AFTER_FIELD(p, fmt.pix);
1173 return ops->vidioc_s_fmt_vid_out(file, fh, arg);
1174 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001175 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001176 break;
1177 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1178 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1179 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001180 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001181 break;
1182 CLEAR_AFTER_FIELD(p, fmt.win);
1183 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001184 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001185 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001186 break;
1187 CLEAR_AFTER_FIELD(p, fmt.vbi);
1188 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001189 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001190 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001191 break;
1192 CLEAR_AFTER_FIELD(p, fmt.sliced);
1193 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001194 }
1195 return -EINVAL;
1196}
1197
1198static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1199 struct file *file, void *fh, void *arg)
1200{
1201 struct v4l2_format *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001202 struct video_device *vfd = video_devdata(file);
1203 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1204 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1205 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001206
1207 switch (p->type) {
1208 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001209 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001210 break;
1211 CLEAR_AFTER_FIELD(p, fmt.pix);
1212 return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1213 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001214 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001215 break;
1216 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1217 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1218 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001219 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001220 break;
1221 CLEAR_AFTER_FIELD(p, fmt.win);
1222 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001223 case V4L2_BUF_TYPE_VBI_CAPTURE:
1224 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_vbi_cap))
1225 break;
1226 CLEAR_AFTER_FIELD(p, fmt.vbi);
1227 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1228 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1229 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_cap))
1230 break;
1231 CLEAR_AFTER_FIELD(p, fmt.sliced);
1232 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001233 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001234 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001235 break;
1236 CLEAR_AFTER_FIELD(p, fmt.pix);
1237 return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1238 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001239 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001240 break;
1241 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1242 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1243 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001244 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001245 break;
1246 CLEAR_AFTER_FIELD(p, fmt.win);
1247 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001248 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001249 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001250 break;
1251 CLEAR_AFTER_FIELD(p, fmt.vbi);
1252 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001253 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001254 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001255 break;
1256 CLEAR_AFTER_FIELD(p, fmt.sliced);
1257 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001258 }
1259 return -EINVAL;
1260}
1261
Hans Verkuile325b242012-06-09 12:50:15 -03001262static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1263 struct file *file, void *fh, void *arg)
1264{
1265 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1266}
1267
1268static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1269 struct file *file, void *fh, void *arg)
1270{
1271 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1272}
1273
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001274static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1275 struct file *file, void *fh, void *arg)
1276{
1277 struct video_device *vfd = video_devdata(file);
1278 struct v4l2_tuner *p = arg;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001279 int err;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001280
1281 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1282 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001283 err = ops->vidioc_g_tuner(file, fh, p);
1284 if (!err)
1285 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1286 return err;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001287}
1288
1289static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1290 struct file *file, void *fh, void *arg)
1291{
1292 struct video_device *vfd = video_devdata(file);
1293 struct v4l2_tuner *p = arg;
1294
1295 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1296 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1297 return ops->vidioc_s_tuner(file, fh, p);
1298}
1299
Hans Verkuil82b655b2012-07-05 06:37:08 -03001300static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1301 struct file *file, void *fh, void *arg)
1302{
1303 struct v4l2_modulator *p = arg;
1304 int err;
1305
1306 err = ops->vidioc_g_modulator(file, fh, p);
1307 if (!err)
1308 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1309 return err;
1310}
1311
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001312static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1313 struct file *file, void *fh, void *arg)
1314{
1315 struct video_device *vfd = video_devdata(file);
1316 struct v4l2_frequency *p = arg;
1317
1318 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1319 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1320 return ops->vidioc_g_frequency(file, fh, p);
1321}
1322
1323static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1324 struct file *file, void *fh, void *arg)
1325{
1326 struct video_device *vfd = video_devdata(file);
Hans Verkuilb530a442013-03-19 04:09:26 -03001327 const struct v4l2_frequency *p = arg;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001328 enum v4l2_tuner_type type;
1329
1330 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1331 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1332 if (p->type != type)
1333 return -EINVAL;
1334 return ops->vidioc_s_frequency(file, fh, p);
1335}
1336
1337static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1338 struct file *file, void *fh, void *arg)
1339{
1340 struct video_device *vfd = video_devdata(file);
1341 struct v4l2_standard *p = arg;
1342 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1343 unsigned int index = p->index, i, j = 0;
1344 const char *descr = "";
1345
Hans Verkuila5338192012-09-14 06:45:43 -03001346 /* Return -ENODATA if the tvnorms for the current input
1347 or output is 0, meaning that it doesn't support this API. */
1348 if (id == 0)
1349 return -ENODATA;
1350
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001351 /* Return norm array in a canonical way */
1352 for (i = 0; i <= index && id; i++) {
1353 /* last std value in the standards array is 0, so this
1354 while always ends there since (id & 0) == 0. */
1355 while ((id & standards[j].std) != standards[j].std)
1356 j++;
1357 curr_id = standards[j].std;
1358 descr = standards[j].descr;
1359 j++;
1360 if (curr_id == 0)
1361 break;
1362 if (curr_id != V4L2_STD_PAL &&
1363 curr_id != V4L2_STD_SECAM &&
1364 curr_id != V4L2_STD_NTSC)
1365 id &= ~curr_id;
1366 }
1367 if (i <= index)
1368 return -EINVAL;
1369
1370 v4l2_video_std_construct(p, curr_id, descr);
1371 return 0;
1372}
1373
1374static int v4l_g_std(const struct v4l2_ioctl_ops *ops,
1375 struct file *file, void *fh, void *arg)
1376{
1377 struct video_device *vfd = video_devdata(file);
1378 v4l2_std_id *id = arg;
1379
1380 /* Calls the specific handler */
1381 if (ops->vidioc_g_std)
1382 return ops->vidioc_g_std(file, fh, arg);
1383 if (vfd->current_norm) {
1384 *id = vfd->current_norm;
1385 return 0;
1386 }
1387 return -ENOTTY;
1388}
1389
1390static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1391 struct file *file, void *fh, void *arg)
1392{
1393 struct video_device *vfd = video_devdata(file);
Hans Verkuil314527a2013-03-15 06:10:40 -03001394 v4l2_std_id id = *(v4l2_std_id *)arg, norm;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001395 int ret;
1396
Hans Verkuil314527a2013-03-15 06:10:40 -03001397 norm = id & vfd->tvnorms;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001398 if (vfd->tvnorms && !norm) /* Check if std is supported */
1399 return -EINVAL;
1400
1401 /* Calls the specific handler */
Hans Verkuil314527a2013-03-15 06:10:40 -03001402 ret = ops->vidioc_s_std(file, fh, norm);
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001403
1404 /* Updates standard information */
1405 if (ret >= 0)
1406 vfd->current_norm = norm;
1407 return ret;
1408}
1409
1410static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1411 struct file *file, void *fh, void *arg)
1412{
1413 struct video_device *vfd = video_devdata(file);
1414 v4l2_std_id *p = arg;
1415
1416 /*
1417 * If nothing detected, it should return all supported
1418 * standard.
1419 * Drivers just need to mask the std argument, in order
1420 * to remove the standards that don't apply from the mask.
1421 * This means that tuners, audio and video decoders can join
1422 * their efforts to improve the standards detection.
1423 */
1424 *p = vfd->tvnorms;
1425 return ops->vidioc_querystd(file, fh, arg);
1426}
1427
1428static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1429 struct file *file, void *fh, void *arg)
1430{
1431 struct video_device *vfd = video_devdata(file);
1432 struct v4l2_hw_freq_seek *p = arg;
1433 enum v4l2_tuner_type type;
1434
1435 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1436 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1437 if (p->type != type)
1438 return -EINVAL;
1439 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1440}
1441
Hans Verkuil737c0972012-09-04 10:08:01 -03001442static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
1443 struct file *file, void *fh, void *arg)
1444{
1445 return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
1446}
1447
Hans Verkuileb3728e2012-06-22 06:23:59 -03001448static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1449 struct file *file, void *fh, void *arg)
1450{
1451 struct v4l2_requestbuffers *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001452 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001453
1454 if (ret)
1455 return ret;
1456
Hans Verkuil633c98e2012-09-17 05:02:26 -03001457 CLEAR_AFTER_FIELD(p, memory);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001458
1459 return ops->vidioc_reqbufs(file, fh, p);
1460}
1461
1462static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1463 struct file *file, void *fh, void *arg)
1464{
1465 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001466 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001467
1468 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1469}
1470
1471static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1472 struct file *file, void *fh, void *arg)
1473{
1474 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001475 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001476
1477 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1478}
1479
1480static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1481 struct file *file, void *fh, void *arg)
1482{
1483 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001484 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001485
1486 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1487}
1488
1489static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1490 struct file *file, void *fh, void *arg)
1491{
1492 struct v4l2_create_buffers *create = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001493 int ret = check_fmt(file, create->format.type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001494
1495 return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
1496}
1497
1498static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1499 struct file *file, void *fh, void *arg)
1500{
1501 struct v4l2_buffer *b = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001502 int ret = check_fmt(file, b->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001503
1504 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1505}
1506
1507static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1508 struct file *file, void *fh, void *arg)
1509{
1510 struct video_device *vfd = video_devdata(file);
1511 struct v4l2_streamparm *p = arg;
1512 v4l2_std_id std;
Hans Verkuil4b202592012-09-14 07:03:35 -03001513 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001514
1515 if (ret)
1516 return ret;
1517 if (ops->vidioc_g_parm)
1518 return ops->vidioc_g_parm(file, fh, p);
1519 std = vfd->current_norm;
1520 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1521 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1522 return -EINVAL;
1523 p->parm.capture.readbuffers = 2;
1524 if (ops->vidioc_g_std)
1525 ret = ops->vidioc_g_std(file, fh, &std);
1526 if (ret == 0)
1527 v4l2_video_std_frame_period(std,
1528 &p->parm.capture.timeperframe);
1529 return ret;
1530}
1531
1532static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1533 struct file *file, void *fh, void *arg)
1534{
1535 struct v4l2_streamparm *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001536 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001537
1538 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1539}
1540
Hans Verkuilefbceec2012-06-09 12:54:02 -03001541static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1542 struct file *file, void *fh, void *arg)
1543{
1544 struct video_device *vfd = video_devdata(file);
1545 struct v4l2_queryctrl *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001546 struct v4l2_fh *vfh =
1547 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001548
1549 if (vfh && vfh->ctrl_handler)
1550 return v4l2_queryctrl(vfh->ctrl_handler, p);
1551 if (vfd->ctrl_handler)
1552 return v4l2_queryctrl(vfd->ctrl_handler, p);
1553 if (ops->vidioc_queryctrl)
1554 return ops->vidioc_queryctrl(file, fh, p);
1555 return -ENOTTY;
1556}
1557
1558static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1559 struct file *file, void *fh, void *arg)
1560{
1561 struct video_device *vfd = video_devdata(file);
1562 struct v4l2_querymenu *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001563 struct v4l2_fh *vfh =
1564 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001565
1566 if (vfh && vfh->ctrl_handler)
1567 return v4l2_querymenu(vfh->ctrl_handler, p);
1568 if (vfd->ctrl_handler)
1569 return v4l2_querymenu(vfd->ctrl_handler, p);
1570 if (ops->vidioc_querymenu)
1571 return ops->vidioc_querymenu(file, fh, p);
1572 return -ENOTTY;
1573}
1574
1575static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1576 struct file *file, void *fh, void *arg)
1577{
1578 struct video_device *vfd = video_devdata(file);
1579 struct v4l2_control *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001580 struct v4l2_fh *vfh =
1581 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001582 struct v4l2_ext_controls ctrls;
1583 struct v4l2_ext_control ctrl;
1584
1585 if (vfh && vfh->ctrl_handler)
1586 return v4l2_g_ctrl(vfh->ctrl_handler, p);
1587 if (vfd->ctrl_handler)
1588 return v4l2_g_ctrl(vfd->ctrl_handler, p);
1589 if (ops->vidioc_g_ctrl)
1590 return ops->vidioc_g_ctrl(file, fh, p);
1591 if (ops->vidioc_g_ext_ctrls == NULL)
1592 return -ENOTTY;
1593
1594 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1595 ctrls.count = 1;
1596 ctrls.controls = &ctrl;
1597 ctrl.id = p->id;
1598 ctrl.value = p->value;
1599 if (check_ext_ctrls(&ctrls, 1)) {
1600 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1601
1602 if (ret == 0)
1603 p->value = ctrl.value;
1604 return ret;
1605 }
1606 return -EINVAL;
1607}
1608
1609static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1610 struct file *file, void *fh, void *arg)
1611{
1612 struct video_device *vfd = video_devdata(file);
1613 struct v4l2_control *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001614 struct v4l2_fh *vfh =
1615 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001616 struct v4l2_ext_controls ctrls;
1617 struct v4l2_ext_control ctrl;
1618
1619 if (vfh && vfh->ctrl_handler)
1620 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1621 if (vfd->ctrl_handler)
1622 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1623 if (ops->vidioc_s_ctrl)
1624 return ops->vidioc_s_ctrl(file, fh, p);
1625 if (ops->vidioc_s_ext_ctrls == NULL)
1626 return -ENOTTY;
1627
1628 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1629 ctrls.count = 1;
1630 ctrls.controls = &ctrl;
1631 ctrl.id = p->id;
1632 ctrl.value = p->value;
1633 if (check_ext_ctrls(&ctrls, 1))
1634 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1635 return -EINVAL;
1636}
1637
1638static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1639 struct file *file, void *fh, void *arg)
1640{
1641 struct video_device *vfd = video_devdata(file);
1642 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001643 struct v4l2_fh *vfh =
1644 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001645
1646 p->error_idx = p->count;
1647 if (vfh && vfh->ctrl_handler)
1648 return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1649 if (vfd->ctrl_handler)
1650 return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1651 if (ops->vidioc_g_ext_ctrls == NULL)
1652 return -ENOTTY;
1653 return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1654 -EINVAL;
1655}
1656
1657static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1658 struct file *file, void *fh, void *arg)
1659{
1660 struct video_device *vfd = video_devdata(file);
1661 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001662 struct v4l2_fh *vfh =
1663 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001664
1665 p->error_idx = p->count;
1666 if (vfh && vfh->ctrl_handler)
1667 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1668 if (vfd->ctrl_handler)
1669 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1670 if (ops->vidioc_s_ext_ctrls == NULL)
1671 return -ENOTTY;
1672 return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1673 -EINVAL;
1674}
1675
1676static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1677 struct file *file, void *fh, void *arg)
1678{
1679 struct video_device *vfd = video_devdata(file);
1680 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001681 struct v4l2_fh *vfh =
1682 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001683
1684 p->error_idx = p->count;
1685 if (vfh && vfh->ctrl_handler)
1686 return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1687 if (vfd->ctrl_handler)
1688 return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1689 if (ops->vidioc_try_ext_ctrls == NULL)
1690 return -ENOTTY;
1691 return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1692 -EINVAL;
1693}
1694
Hans Verkuild84f2d942012-06-09 11:57:46 -03001695static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1696 struct file *file, void *fh, void *arg)
1697{
1698 struct v4l2_crop *p = arg;
1699 struct v4l2_selection s = {
1700 .type = p->type,
1701 };
1702 int ret;
1703
1704 if (ops->vidioc_g_crop)
1705 return ops->vidioc_g_crop(file, fh, p);
1706 /* simulate capture crop using selection api */
1707
1708 /* crop means compose for output devices */
1709 if (V4L2_TYPE_IS_OUTPUT(p->type))
1710 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1711 else
1712 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1713
1714 ret = ops->vidioc_g_selection(file, fh, &s);
1715
1716 /* copying results to old structure on success */
1717 if (!ret)
1718 p->c = s.r;
1719 return ret;
1720}
1721
1722static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1723 struct file *file, void *fh, void *arg)
1724{
1725 struct v4l2_crop *p = arg;
1726 struct v4l2_selection s = {
1727 .type = p->type,
1728 .r = p->c,
1729 };
1730
1731 if (ops->vidioc_s_crop)
1732 return ops->vidioc_s_crop(file, fh, p);
1733 /* simulate capture crop using selection api */
1734
1735 /* crop means compose for output devices */
1736 if (V4L2_TYPE_IS_OUTPUT(p->type))
1737 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1738 else
1739 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1740
1741 return ops->vidioc_s_selection(file, fh, &s);
1742}
1743
1744static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1745 struct file *file, void *fh, void *arg)
1746{
1747 struct v4l2_cropcap *p = arg;
1748 struct v4l2_selection s = { .type = p->type };
1749 int ret;
1750
1751 if (ops->vidioc_cropcap)
1752 return ops->vidioc_cropcap(file, fh, p);
1753
1754 /* obtaining bounds */
1755 if (V4L2_TYPE_IS_OUTPUT(p->type))
1756 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1757 else
1758 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1759
1760 ret = ops->vidioc_g_selection(file, fh, &s);
1761 if (ret)
1762 return ret;
1763 p->bounds = s.r;
1764
1765 /* obtaining defrect */
1766 if (V4L2_TYPE_IS_OUTPUT(p->type))
1767 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1768 else
1769 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1770
1771 ret = ops->vidioc_g_selection(file, fh, &s);
1772 if (ret)
1773 return ret;
1774 p->defrect = s.r;
1775
1776 /* setting trivial pixelaspect */
1777 p->pixelaspect.numerator = 1;
1778 p->pixelaspect.denominator = 1;
1779 return 0;
1780}
1781
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001782static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1783 struct file *file, void *fh, void *arg)
1784{
1785 struct video_device *vfd = video_devdata(file);
1786 int ret;
1787
1788 if (vfd->v4l2_dev)
1789 pr_info("%s: ================= START STATUS =================\n",
1790 vfd->v4l2_dev->name);
1791 ret = ops->vidioc_log_status(file, fh);
1792 if (vfd->v4l2_dev)
1793 pr_info("%s: ================== END STATUS ==================\n",
1794 vfd->v4l2_dev->name);
1795 return ret;
1796}
1797
1798static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1799 struct file *file, void *fh, void *arg)
1800{
1801#ifdef CONFIG_VIDEO_ADV_DEBUG
1802 struct v4l2_dbg_register *p = arg;
1803
1804 if (!capable(CAP_SYS_ADMIN))
1805 return -EPERM;
1806 return ops->vidioc_g_register(file, fh, p);
1807#else
1808 return -ENOTTY;
1809#endif
1810}
1811
1812static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1813 struct file *file, void *fh, void *arg)
1814{
1815#ifdef CONFIG_VIDEO_ADV_DEBUG
1816 struct v4l2_dbg_register *p = arg;
1817
1818 if (!capable(CAP_SYS_ADMIN))
1819 return -EPERM;
1820 return ops->vidioc_s_register(file, fh, p);
1821#else
1822 return -ENOTTY;
1823#endif
1824}
1825
1826static int v4l_dbg_g_chip_ident(const struct v4l2_ioctl_ops *ops,
1827 struct file *file, void *fh, void *arg)
1828{
1829 struct v4l2_dbg_chip_ident *p = arg;
1830
1831 p->ident = V4L2_IDENT_NONE;
1832 p->revision = 0;
1833 return ops->vidioc_g_chip_ident(file, fh, p);
1834}
1835
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001836static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
1837 struct file *file, void *fh, void *arg)
1838{
1839 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
1840}
1841
1842static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
1843 struct file *file, void *fh, void *arg)
1844{
1845 return ops->vidioc_subscribe_event(fh, arg);
1846}
1847
1848static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
1849 struct file *file, void *fh, void *arg)
1850{
1851 return ops->vidioc_unsubscribe_event(fh, arg);
1852}
1853
1854static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
1855 struct file *file, void *fh, void *arg)
1856{
1857 struct v4l2_sliced_vbi_cap *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001858 int ret = check_fmt(file, p->type);
1859
1860 if (ret)
1861 return ret;
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001862
1863 /* Clear up to type, everything after type is zeroed already */
1864 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1865
1866 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1867}
1868
Hans Verkuil82b655b2012-07-05 06:37:08 -03001869static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
1870 struct file *file, void *fh, void *arg)
1871{
1872 struct video_device *vfd = video_devdata(file);
1873 struct v4l2_frequency_band *p = arg;
1874 enum v4l2_tuner_type type;
1875 int err;
1876
1877 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1878 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1879
1880 if (type != p->type)
1881 return -EINVAL;
1882 if (ops->vidioc_enum_freq_bands)
1883 return ops->vidioc_enum_freq_bands(file, fh, p);
1884 if (ops->vidioc_g_tuner) {
1885 struct v4l2_tuner t = {
1886 .index = p->tuner,
1887 .type = type,
1888 };
1889
Hans Verkuilaa4d9b52012-08-01 15:52:46 -03001890 if (p->index)
1891 return -EINVAL;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001892 err = ops->vidioc_g_tuner(file, fh, &t);
1893 if (err)
1894 return err;
1895 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1896 p->rangelow = t.rangelow;
1897 p->rangehigh = t.rangehigh;
1898 p->modulation = (type == V4L2_TUNER_RADIO) ?
1899 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1900 return 0;
1901 }
1902 if (ops->vidioc_g_modulator) {
1903 struct v4l2_modulator m = {
1904 .index = p->tuner,
1905 };
1906
1907 if (type != V4L2_TUNER_RADIO)
1908 return -EINVAL;
Hans Verkuilaa4d9b52012-08-01 15:52:46 -03001909 if (p->index)
1910 return -EINVAL;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001911 err = ops->vidioc_g_modulator(file, fh, &m);
1912 if (err)
1913 return err;
1914 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1915 p->rangelow = m.rangelow;
1916 p->rangehigh = m.rangehigh;
1917 p->modulation = (type == V4L2_TUNER_RADIO) ?
1918 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1919 return 0;
1920 }
1921 return -ENOTTY;
1922}
1923
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001924struct v4l2_ioctl_info {
1925 unsigned int ioctl;
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001926 u32 flags;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001927 const char * const name;
Hans Verkuil86748f32012-06-22 06:04:16 -03001928 union {
1929 u32 offset;
1930 int (*func)(const struct v4l2_ioctl_ops *ops,
1931 struct file *file, void *fh, void *p);
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001932 } u;
Hans Verkuil86748f32012-06-22 06:04:16 -03001933 void (*debug)(const void *arg, bool write_only);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001934};
1935
1936/* This control needs a priority check */
1937#define INFO_FL_PRIO (1 << 0)
1938/* This control can be valid if the filehandle passes a control handler. */
1939#define INFO_FL_CTRL (1 << 1)
Hans Verkuil86748f32012-06-22 06:04:16 -03001940/* This is a standard ioctl, no need for special code */
1941#define INFO_FL_STD (1 << 2)
1942/* This is ioctl has its own function */
1943#define INFO_FL_FUNC (1 << 3)
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001944/* Queuing ioctl */
1945#define INFO_FL_QUEUE (1 << 4)
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001946/* Zero struct from after the field to the end */
1947#define INFO_FL_CLEAR(v4l2_struct, field) \
1948 ((offsetof(struct v4l2_struct, field) + \
1949 sizeof(((struct v4l2_struct *)0)->field)) << 16)
1950#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001951
Hans Verkuil86748f32012-06-22 06:04:16 -03001952#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
1953 [_IOC_NR(_ioctl)] = { \
1954 .ioctl = _ioctl, \
1955 .flags = _flags | INFO_FL_STD, \
1956 .name = #_ioctl, \
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001957 .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
Hans Verkuil86748f32012-06-22 06:04:16 -03001958 .debug = _debug, \
1959 }
1960
1961#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
1962 [_IOC_NR(_ioctl)] = { \
1963 .ioctl = _ioctl, \
1964 .flags = _flags | INFO_FL_FUNC, \
1965 .name = #_ioctl, \
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001966 .u.func = _func, \
Hans Verkuil86748f32012-06-22 06:04:16 -03001967 .debug = _debug, \
1968 }
1969
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001970static struct v4l2_ioctl_info v4l2_ioctls[] = {
Hans Verkuil59076122012-06-22 06:09:54 -03001971 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001972 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
1973 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
1974 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001975 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
1976 IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001977 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
1978 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
Hans Verkuil737c0972012-09-04 10:08:01 -03001979 IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001980 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
Tomasz Stanislawskib799d092012-06-14 11:32:23 -03001981 IOCTL_INFO_STD(VIDIOC_EXPBUF, vidioc_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001982 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
1983 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
1984 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001985 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
1986 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001987 IOCTL_INFO_FNC(VIDIOC_G_STD, v4l_g_std, v4l_print_std, 0),
1988 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
1989 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
Hans Verkuil59076122012-06-22 06:09:54 -03001990 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001991 IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
1992 IOCTL_INFO_FNC(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001993 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
1994 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
Hans Verkuil59076122012-06-22 06:09:54 -03001995 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
1996 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001997 IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
1998 IOCTL_INFO_FNC(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
Hans Verkuil59076122012-06-22 06:09:54 -03001999 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
2000 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
2001 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
2002 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2003 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2004 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
2005 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
Hans Verkuil82b655b2012-07-05 06:37:08 -03002006 IOCTL_INFO_FNC(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03002007 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2008 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2009 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
Hans Verkuild84f2d942012-06-09 11:57:46 -03002010 IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2011 IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2012 IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2013 IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
2014 IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
Hans Verkuild806f2d2012-06-09 10:02:49 -03002015 IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
2016 IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03002017 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03002018 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
Hans Verkuil59076122012-06-22 06:09:54 -03002019 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2020 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
Hans Verkuild0547cb2012-06-09 09:27:10 -03002021 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2022 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002023 IOCTL_INFO_FNC(VIDIOC_G_SLICED_VBI_CAP, v4l_g_sliced_vbi_cap, v4l_print_sliced_vbi_cap, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
Hans Verkuilf16f77b2012-06-09 10:06:25 -03002024 IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
Hans Verkuilefbceec2012-06-09 12:54:02 -03002025 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2026 IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
Hans Verkuil9bc31632012-07-18 09:34:59 -03002027 IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002028 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2029 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
Hans Verkuild806f2d2012-06-09 10:02:49 -03002030 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
2031 IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2032 IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2033 IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2034 IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
Hans Verkuilf16f77b2012-06-09 10:06:25 -03002035 IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2036 IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2037 IOCTL_INFO_FNC(VIDIOC_DBG_G_CHIP_IDENT, v4l_dbg_g_chip_ident, v4l_print_dbg_chip_ident, 0),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03002038 IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
Hans Verkuilefc626b2012-06-09 10:09:07 -03002039 IOCTL_INFO_STD(VIDIOC_ENUM_DV_PRESETS, vidioc_enum_dv_presets, v4l_print_dv_enum_presets, 0),
2040 IOCTL_INFO_STD(VIDIOC_S_DV_PRESET, vidioc_s_dv_preset, v4l_print_dv_preset, INFO_FL_PRIO),
2041 IOCTL_INFO_STD(VIDIOC_G_DV_PRESET, vidioc_g_dv_preset, v4l_print_dv_preset, 0),
2042 IOCTL_INFO_STD(VIDIOC_QUERY_DV_PRESET, vidioc_query_dv_preset, v4l_print_dv_preset, 0),
2043 IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
2044 IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002045 IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2046 IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2047 IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002048 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2049 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
Hans Verkuilefc626b2012-06-09 10:09:07 -03002050 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
2051 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuila1acb8f2012-07-11 09:15:06 -03002052 IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)),
Hans Verkuil82b655b2012-07-05 06:37:08 -03002053 IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002054};
2055#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2056
2057bool v4l2_is_known_ioctl(unsigned int cmd)
2058{
2059 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2060 return false;
2061 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2062}
2063
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002064struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
2065{
2066 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2067 return vdev->lock;
2068 if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
2069 return NULL;
2070 if (vdev->queue && vdev->queue->lock &&
2071 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2072 return vdev->queue->lock;
2073 return vdev->lock;
2074}
2075
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002076/* Common ioctl debug function. This function can be used by
2077 external ioctl messages as well as internal V4L ioctl */
Hans Verkuil4a085162012-06-22 06:38:06 -03002078void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002079{
Hans Verkuil86748f32012-06-22 06:04:16 -03002080 const char *dir, *type;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002081
Hans Verkuil4a085162012-06-22 06:38:06 -03002082 if (prefix)
2083 printk(KERN_DEBUG "%s: ", prefix);
2084
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002085 switch (_IOC_TYPE(cmd)) {
2086 case 'd':
2087 type = "v4l2_int";
2088 break;
2089 case 'V':
2090 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2091 type = "v4l2";
2092 break;
2093 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002094 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002095 return;
2096 default:
2097 type = "unknown";
Hans Verkuil86748f32012-06-22 06:04:16 -03002098 break;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002099 }
2100
2101 switch (_IOC_DIR(cmd)) {
2102 case _IOC_NONE: dir = "--"; break;
2103 case _IOC_READ: dir = "r-"; break;
2104 case _IOC_WRITE: dir = "-w"; break;
2105 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2106 default: dir = "*ERR*"; break;
2107 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002108 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002109 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2110}
2111EXPORT_SYMBOL(v4l_printk_ioctl);
2112
Hans Verkuil069b7472008-12-30 07:04:34 -03002113static long __video_do_ioctl(struct file *file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002114 unsigned int cmd, void *arg)
2115{
2116 struct video_device *vfd = video_devdata(file);
Hans Verkuila3998102008-07-21 02:57:38 -03002117 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
Hans Verkuil86748f32012-06-22 06:04:16 -03002118 bool write_only = false;
2119 struct v4l2_ioctl_info default_info;
2120 const struct v4l2_ioctl_info *info;
Hans Verkuild5fbf322008-10-18 13:39:53 -03002121 void *fh = file->private_data;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03002122 struct v4l2_fh *vfh = NULL;
Hans Verkuilb1a873a2011-03-22 10:14:07 -03002123 int use_fh_prio = 0;
Hans Verkuil80131fe02012-06-22 06:37:38 -03002124 int debug = vfd->debug;
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002125 long ret = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002126
Hans Verkuil6a717882010-04-06 15:56:08 -03002127 if (ops == NULL) {
Hans Verkuil4a085162012-06-22 06:38:06 -03002128 pr_warn("%s: has no ioctl_ops.\n",
2129 video_device_node_name(vfd));
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002130 return ret;
Hans Verkuil6a717882010-04-06 15:56:08 -03002131 }
2132
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002133 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2134 vfh = file->private_data;
2135 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002136 }
2137
2138 if (v4l2_is_known_ioctl(cmd)) {
Hans Verkuil86748f32012-06-22 06:04:16 -03002139 info = &v4l2_ioctls[_IOC_NR(cmd)];
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002140
2141 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2142 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
Hans Verkuil86748f32012-06-22 06:04:16 -03002143 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002144
2145 if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
2146 ret = v4l2_prio_check(vfd->prio, vfh->prio);
2147 if (ret)
Hans Verkuil86748f32012-06-22 06:04:16 -03002148 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002149 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002150 } else {
2151 default_info.ioctl = cmd;
2152 default_info.flags = 0;
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002153 default_info.debug = v4l_print_default;
Hans Verkuil86748f32012-06-22 06:04:16 -03002154 info = &default_info;
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002155 }
2156
Hans Verkuil86748f32012-06-22 06:04:16 -03002157 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
Hans Verkuil80131fe02012-06-22 06:37:38 -03002158 if (write_only && debug > V4L2_DEBUG_IOCTL) {
Hans Verkuil4a085162012-06-22 06:38:06 -03002159 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03002160 pr_cont(": ");
2161 info->debug(arg, write_only);
2162 }
2163 if (info->flags & INFO_FL_STD) {
2164 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2165 const void *p = vfd->ioctl_ops;
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002166 const vidioc_op *vidioc = p + info->u.offset;
Hans Verkuil86748f32012-06-22 06:04:16 -03002167
2168 ret = (*vidioc)(file, fh, arg);
Hans Verkuil86748f32012-06-22 06:04:16 -03002169 } else if (info->flags & INFO_FL_FUNC) {
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002170 ret = info->u.func(ops, file, fh, arg);
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002171 } else if (!ops->vidioc_default) {
2172 ret = -ENOTTY;
2173 } else {
2174 ret = ops->vidioc_default(file, fh,
2175 use_fh_prio ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2176 cmd, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002177 }
2178
Hans Verkuil86748f32012-06-22 06:04:16 -03002179done:
Hans Verkuil80131fe02012-06-22 06:37:38 -03002180 if (debug) {
2181 if (write_only && debug > V4L2_DEBUG_IOCTL) {
Hans Verkuil86748f32012-06-22 06:04:16 -03002182 if (ret < 0)
2183 printk(KERN_DEBUG "%s: error %ld\n",
2184 video_device_node_name(vfd), ret);
2185 return ret;
2186 }
Hans Verkuil4a085162012-06-22 06:38:06 -03002187 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03002188 if (ret < 0)
2189 pr_cont(": error %ld\n", ret);
Hans Verkuil80131fe02012-06-22 06:37:38 -03002190 else if (debug == V4L2_DEBUG_IOCTL)
Hans Verkuil86748f32012-06-22 06:04:16 -03002191 pr_cont("\n");
Hans Verkuil86748f32012-06-22 06:04:16 -03002192 else if (_IOC_DIR(cmd) == _IOC_NONE)
2193 info->debug(arg, write_only);
2194 else {
2195 pr_cont(": ");
2196 info->debug(arg, write_only);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002197 }
2198 }
2199
2200 return ret;
2201}
2202
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002203static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2204 void * __user *user_ptr, void ***kernel_ptr)
2205{
2206 int ret = 0;
2207
2208 switch (cmd) {
Hans Verkuil96b1a702012-09-19 10:14:41 -03002209 case VIDIOC_PREPARE_BUF:
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002210 case VIDIOC_QUERYBUF:
2211 case VIDIOC_QBUF:
2212 case VIDIOC_DQBUF: {
2213 struct v4l2_buffer *buf = parg;
2214
2215 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2216 if (buf->length > VIDEO_MAX_PLANES) {
2217 ret = -EINVAL;
2218 break;
2219 }
2220 *user_ptr = (void __user *)buf->m.planes;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002221 *kernel_ptr = (void *)&buf->m.planes;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002222 *array_size = sizeof(struct v4l2_plane) * buf->length;
2223 ret = 1;
2224 }
2225 break;
2226 }
2227
Hans Verkuiled45ce22012-08-10 06:07:12 -03002228 case VIDIOC_SUBDEV_G_EDID:
2229 case VIDIOC_SUBDEV_S_EDID: {
2230 struct v4l2_subdev_edid *edid = parg;
2231
2232 if (edid->blocks) {
Hans Verkuil1b8b10c2012-10-02 02:47:58 -03002233 if (edid->blocks > 256) {
2234 ret = -EINVAL;
2235 break;
2236 }
Hans Verkuiled45ce22012-08-10 06:07:12 -03002237 *user_ptr = (void __user *)edid->edid;
2238 *kernel_ptr = (void *)&edid->edid;
2239 *array_size = edid->blocks * 128;
2240 ret = 1;
2241 }
2242 break;
2243 }
2244
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002245 case VIDIOC_S_EXT_CTRLS:
2246 case VIDIOC_G_EXT_CTRLS:
2247 case VIDIOC_TRY_EXT_CTRLS: {
2248 struct v4l2_ext_controls *ctrls = parg;
2249
2250 if (ctrls->count != 0) {
Dan Carpenter6c061082012-01-05 02:27:57 -03002251 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2252 ret = -EINVAL;
2253 break;
2254 }
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002255 *user_ptr = (void __user *)ctrls->controls;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002256 *kernel_ptr = (void *)&ctrls->controls;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002257 *array_size = sizeof(struct v4l2_ext_control)
2258 * ctrls->count;
2259 ret = 1;
2260 }
2261 break;
2262 }
2263 }
2264
2265 return ret;
2266}
2267
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002268long
2269video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2270 v4l2_kioctl func)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002271{
2272 char sbuf[128];
2273 void *mbuf = NULL;
Hans Verkuil1d94aa32010-04-06 08:12:21 -03002274 void *parg = (void *)arg;
Hans Verkuil069b7472008-12-30 07:04:34 -03002275 long err = -EINVAL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002276 bool has_array_args;
2277 size_t array_size = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002278 void __user *user_ptr = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002279 void **kernel_ptr = NULL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002280
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002281 /* Copy arguments into temp kernel buffer */
Trent Piepho337f9d22009-03-04 01:21:02 -03002282 if (_IOC_DIR(cmd) != _IOC_NONE) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002283 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2284 parg = sbuf;
2285 } else {
2286 /* too big to allocate from stack */
2287 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2288 if (NULL == mbuf)
2289 return -ENOMEM;
2290 parg = mbuf;
2291 }
2292
2293 err = -EFAULT;
Trent Piepho19c96e42009-03-04 01:21:02 -03002294 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002295 unsigned int n = _IOC_SIZE(cmd);
2296
2297 /*
2298 * In some cases, only a few fields are used as input,
2299 * i.e. when the app sets "index" and then the driver
2300 * fills in the rest of the structure for the thing
2301 * with that index. We only need to copy up the first
2302 * non-input field.
2303 */
2304 if (v4l2_is_known_ioctl(cmd)) {
2305 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2306 if (flags & INFO_FL_CLEAR_MASK)
2307 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2308 }
Trent Piepho19c96e42009-03-04 01:21:02 -03002309
2310 if (copy_from_user(parg, (void __user *)arg, n))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002311 goto out;
Trent Piepho19c96e42009-03-04 01:21:02 -03002312
2313 /* zero out anything we don't copy from userspace */
2314 if (n < _IOC_SIZE(cmd))
2315 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
Trent Piepho337f9d22009-03-04 01:21:02 -03002316 } else {
2317 /* read-only ioctl */
2318 memset(parg, 0, _IOC_SIZE(cmd));
Trent Piepho19c96e42009-03-04 01:21:02 -03002319 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002320 }
2321
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002322 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2323 if (err < 0)
2324 goto out;
2325 has_array_args = err;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002326
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002327 if (has_array_args) {
2328 /*
2329 * When adding new types of array args, make sure that the
2330 * parent argument to ioctl (which contains the pointer to the
2331 * array) fits into sbuf (so that mbuf will still remain
2332 * unused up to here).
2333 */
2334 mbuf = kmalloc(array_size, GFP_KERNEL);
2335 err = -ENOMEM;
2336 if (NULL == mbuf)
2337 goto out_array_args;
2338 err = -EFAULT;
2339 if (copy_from_user(mbuf, user_ptr, array_size))
2340 goto out_array_args;
2341 *kernel_ptr = mbuf;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002342 }
2343
2344 /* Handles IOCTL */
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002345 err = func(file, cmd, parg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002346 if (err == -ENOIOCTLCMD)
Hans Verkuil02bbb812012-02-08 08:09:36 -03002347 err = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002348
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002349 if (has_array_args) {
2350 *kernel_ptr = user_ptr;
2351 if (copy_to_user(user_ptr, mbuf, array_size))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002352 err = -EFAULT;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002353 goto out_array_args;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002354 }
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002355 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2356 results that must be returned. */
2357 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002358 goto out;
2359
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002360out_array_args:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002361 /* Copy results into user buffer */
2362 switch (_IOC_DIR(cmd)) {
2363 case _IOC_READ:
2364 case (_IOC_WRITE | _IOC_READ):
2365 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2366 err = -EFAULT;
2367 break;
2368 }
2369
2370out:
2371 kfree(mbuf);
2372 return err;
2373}
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002374EXPORT_SYMBOL(video_usercopy);
2375
2376long video_ioctl2(struct file *file,
2377 unsigned int cmd, unsigned long arg)
2378{
2379 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2380}
Mauro Carvalho Chehab8a522c92008-10-21 11:58:39 -03002381EXPORT_SYMBOL(video_ioctl2);