blob: 72c22fde2b69379d2efde340f41721a67edff37f [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 Verkuil73f35412013-03-10 13:12:25 -030038#define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
39
Hans Verkuil35ea11f2008-07-20 08:12:02 -030040struct std_descr {
41 v4l2_std_id std;
42 const char *descr;
43};
44
45static const struct std_descr standards[] = {
46 { V4L2_STD_NTSC, "NTSC" },
47 { V4L2_STD_NTSC_M, "NTSC-M" },
48 { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
49 { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
50 { V4L2_STD_NTSC_443, "NTSC-443" },
51 { V4L2_STD_PAL, "PAL" },
52 { V4L2_STD_PAL_BG, "PAL-BG" },
53 { V4L2_STD_PAL_B, "PAL-B" },
54 { V4L2_STD_PAL_B1, "PAL-B1" },
55 { V4L2_STD_PAL_G, "PAL-G" },
56 { V4L2_STD_PAL_H, "PAL-H" },
57 { V4L2_STD_PAL_I, "PAL-I" },
58 { V4L2_STD_PAL_DK, "PAL-DK" },
59 { V4L2_STD_PAL_D, "PAL-D" },
60 { V4L2_STD_PAL_D1, "PAL-D1" },
61 { V4L2_STD_PAL_K, "PAL-K" },
62 { V4L2_STD_PAL_M, "PAL-M" },
63 { V4L2_STD_PAL_N, "PAL-N" },
64 { V4L2_STD_PAL_Nc, "PAL-Nc" },
65 { V4L2_STD_PAL_60, "PAL-60" },
66 { V4L2_STD_SECAM, "SECAM" },
67 { V4L2_STD_SECAM_B, "SECAM-B" },
68 { V4L2_STD_SECAM_G, "SECAM-G" },
69 { V4L2_STD_SECAM_H, "SECAM-H" },
70 { V4L2_STD_SECAM_DK, "SECAM-DK" },
71 { V4L2_STD_SECAM_D, "SECAM-D" },
72 { V4L2_STD_SECAM_K, "SECAM-K" },
73 { V4L2_STD_SECAM_K1, "SECAM-K1" },
74 { V4L2_STD_SECAM_L, "SECAM-L" },
75 { V4L2_STD_SECAM_LC, "SECAM-Lc" },
76 { 0, "Unknown" }
77};
78
79/* video4linux standard ID conversion to standard name
80 */
81const char *v4l2_norm_to_name(v4l2_std_id id)
82{
83 u32 myid = id;
84 int i;
85
86 /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
87 64 bit comparations. So, on that architecture, with some gcc
88 variants, compilation fails. Currently, the max value is 30bit wide.
89 */
90 BUG_ON(myid != id);
91
92 for (i = 0; standards[i].std; i++)
93 if (myid == standards[i].std)
94 break;
95 return standards[i].descr;
96}
97EXPORT_SYMBOL(v4l2_norm_to_name);
98
Trent Piepho51f0b8d52009-03-04 01:21:02 -030099/* Returns frame period for the given standard */
100void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
101{
102 if (id & V4L2_STD_525_60) {
103 frameperiod->numerator = 1001;
104 frameperiod->denominator = 30000;
105 } else {
106 frameperiod->numerator = 1;
107 frameperiod->denominator = 25;
108 }
109}
110EXPORT_SYMBOL(v4l2_video_std_frame_period);
111
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300112/* Fill in the fields of a v4l2_standard structure according to the
113 'id' and 'transmission' parameters. Returns negative on error. */
114int v4l2_video_std_construct(struct v4l2_standard *vs,
115 int id, const char *name)
116{
Trent Piepho51f0b8d52009-03-04 01:21:02 -0300117 vs->id = id;
118 v4l2_video_std_frame_period(id, &vs->frameperiod);
119 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300120 strlcpy(vs->name, name, sizeof(vs->name));
121 return 0;
122}
123EXPORT_SYMBOL(v4l2_video_std_construct);
124
125/* ----------------------------------------------------------------- */
126/* some arrays for pretty-printing debug messages of enum types */
127
128const char *v4l2_field_names[] = {
129 [V4L2_FIELD_ANY] = "any",
130 [V4L2_FIELD_NONE] = "none",
131 [V4L2_FIELD_TOP] = "top",
132 [V4L2_FIELD_BOTTOM] = "bottom",
133 [V4L2_FIELD_INTERLACED] = "interlaced",
134 [V4L2_FIELD_SEQ_TB] = "seq-tb",
135 [V4L2_FIELD_SEQ_BT] = "seq-bt",
136 [V4L2_FIELD_ALTERNATE] = "alternate",
137 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
138 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
139};
140EXPORT_SYMBOL(v4l2_field_names);
141
142const char *v4l2_type_names[] = {
143 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
144 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
145 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
146 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
147 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
148 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
149 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
150 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
Pawel Osciakf8f39142010-07-29 14:44:25 -0300151 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
152 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300153};
154EXPORT_SYMBOL(v4l2_type_names);
155
156static const char *v4l2_memory_names[] = {
157 [V4L2_MEMORY_MMAP] = "mmap",
158 [V4L2_MEMORY_USERPTR] = "userptr",
159 [V4L2_MEMORY_OVERLAY] = "overlay",
Sumit Semwal051c7782012-06-14 10:37:35 -0300160 [V4L2_MEMORY_DMABUF] = "dmabuf",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300161};
162
Hans Verkuild9246242012-10-02 02:47:59 -0300163#define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300164
165/* ------------------------------------------------------------------ */
166/* debug help functions */
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300167
Hans Verkuil59076122012-06-22 06:09:54 -0300168static void v4l_print_querycap(const void *arg, bool write_only)
169{
170 const struct v4l2_capability *p = arg;
171
Hans Verkuil27d5a872013-03-18 11:02:22 -0300172 pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, "
Hans Verkuil59076122012-06-22 06:09:54 -0300173 "capabilities=0x%08x, device_caps=0x%08x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300174 (int)sizeof(p->driver), p->driver,
175 (int)sizeof(p->card), p->card,
176 (int)sizeof(p->bus_info), p->bus_info,
Hans Verkuil59076122012-06-22 06:09:54 -0300177 p->version, p->capabilities, p->device_caps);
178}
179
180static void v4l_print_enuminput(const void *arg, bool write_only)
181{
182 const struct v4l2_input *p = arg;
183
Hans Verkuil27d5a872013-03-18 11:02:22 -0300184 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, "
Hans Verkuil59076122012-06-22 06:09:54 -0300185 "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300186 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
187 p->tuner, (unsigned long long)p->std, p->status,
188 p->capabilities);
Hans Verkuil59076122012-06-22 06:09:54 -0300189}
190
191static void v4l_print_enumoutput(const void *arg, bool write_only)
192{
193 const struct v4l2_output *p = arg;
194
Hans Verkuil27d5a872013-03-18 11:02:22 -0300195 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, "
Hans Verkuil59076122012-06-22 06:09:54 -0300196 "modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300197 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
198 p->modulator, (unsigned long long)p->std, p->capabilities);
Hans Verkuil59076122012-06-22 06:09:54 -0300199}
200
201static void v4l_print_audio(const void *arg, bool write_only)
202{
203 const struct v4l2_audio *p = arg;
204
205 if (write_only)
206 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
207 else
Hans Verkuil27d5a872013-03-18 11:02:22 -0300208 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
209 p->index, (int)sizeof(p->name), p->name,
210 p->capability, p->mode);
Hans Verkuil59076122012-06-22 06:09:54 -0300211}
212
213static void v4l_print_audioout(const void *arg, bool write_only)
214{
215 const struct v4l2_audioout *p = arg;
216
217 if (write_only)
218 pr_cont("index=%u\n", p->index);
219 else
Hans Verkuil27d5a872013-03-18 11:02:22 -0300220 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
221 p->index, (int)sizeof(p->name), p->name,
222 p->capability, p->mode);
Hans Verkuil59076122012-06-22 06:09:54 -0300223}
224
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300225static void v4l_print_fmtdesc(const void *arg, bool write_only)
226{
227 const struct v4l2_fmtdesc *p = arg;
228
Hans Verkuil27d5a872013-03-18 11:02:22 -0300229 pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%.*s'\n",
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300230 p->index, prt_names(p->type, v4l2_type_names),
231 p->flags, (p->pixelformat & 0xff),
232 (p->pixelformat >> 8) & 0xff,
233 (p->pixelformat >> 16) & 0xff,
234 (p->pixelformat >> 24) & 0xff,
Hans Verkuil27d5a872013-03-18 11:02:22 -0300235 (int)sizeof(p->description), p->description);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300236}
237
238static void v4l_print_format(const void *arg, bool write_only)
239{
240 const struct v4l2_format *p = arg;
241 const struct v4l2_pix_format *pix;
242 const struct v4l2_pix_format_mplane *mp;
243 const struct v4l2_vbi_format *vbi;
244 const struct v4l2_sliced_vbi_format *sliced;
245 const struct v4l2_window *win;
246 const struct v4l2_clip *clip;
247 unsigned i;
248
249 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
250 switch (p->type) {
251 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
252 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
253 pix = &p->fmt.pix;
254 pr_cont(", width=%u, height=%u, "
255 "pixelformat=%c%c%c%c, field=%s, "
256 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
257 pix->width, pix->height,
258 (pix->pixelformat & 0xff),
259 (pix->pixelformat >> 8) & 0xff,
260 (pix->pixelformat >> 16) & 0xff,
261 (pix->pixelformat >> 24) & 0xff,
262 prt_names(pix->field, v4l2_field_names),
263 pix->bytesperline, pix->sizeimage,
264 pix->colorspace);
265 break;
266 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
267 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
268 mp = &p->fmt.pix_mp;
269 pr_cont(", width=%u, height=%u, "
270 "format=%c%c%c%c, field=%s, "
271 "colorspace=%d, num_planes=%u\n",
272 mp->width, mp->height,
273 (mp->pixelformat & 0xff),
274 (mp->pixelformat >> 8) & 0xff,
275 (mp->pixelformat >> 16) & 0xff,
276 (mp->pixelformat >> 24) & 0xff,
277 prt_names(mp->field, v4l2_field_names),
278 mp->colorspace, mp->num_planes);
279 for (i = 0; i < mp->num_planes; i++)
280 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
281 mp->plane_fmt[i].bytesperline,
282 mp->plane_fmt[i].sizeimage);
283 break;
284 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
285 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
286 win = &p->fmt.win;
287 pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, "
288 "chromakey=0x%08x, bitmap=%p, "
289 "global_alpha=0x%02x\n",
290 win->w.width, win->w.height,
291 win->w.left, win->w.top,
292 prt_names(win->field, v4l2_field_names),
293 win->chromakey, win->bitmap, win->global_alpha);
294 clip = win->clips;
295 for (i = 0; i < win->clipcount; i++) {
296 printk(KERN_DEBUG "clip %u: wxh=%dx%d, x,y=%d,%d\n",
297 i, clip->c.width, clip->c.height,
298 clip->c.left, clip->c.top);
299 clip = clip->next;
300 }
301 break;
302 case V4L2_BUF_TYPE_VBI_CAPTURE:
303 case V4L2_BUF_TYPE_VBI_OUTPUT:
304 vbi = &p->fmt.vbi;
305 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
306 "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
307 vbi->sampling_rate, vbi->offset,
308 vbi->samples_per_line,
309 (vbi->sample_format & 0xff),
310 (vbi->sample_format >> 8) & 0xff,
311 (vbi->sample_format >> 16) & 0xff,
312 (vbi->sample_format >> 24) & 0xff,
313 vbi->start[0], vbi->start[1],
314 vbi->count[0], vbi->count[1]);
315 break;
316 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
317 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
318 sliced = &p->fmt.sliced;
319 pr_cont(", service_set=0x%08x, io_size=%d\n",
320 sliced->service_set, sliced->io_size);
321 for (i = 0; i < 24; i++)
322 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
323 sliced->service_lines[0][i],
324 sliced->service_lines[1][i]);
325 break;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300326 }
327}
328
329static void v4l_print_framebuffer(const void *arg, bool write_only)
330{
331 const struct v4l2_framebuffer *p = arg;
332
333 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
334 "height=%u, pixelformat=%c%c%c%c, "
335 "bytesperline=%u sizeimage=%u, colorspace=%d\n",
336 p->capability, p->flags, p->base,
337 p->fmt.width, p->fmt.height,
338 (p->fmt.pixelformat & 0xff),
339 (p->fmt.pixelformat >> 8) & 0xff,
340 (p->fmt.pixelformat >> 16) & 0xff,
341 (p->fmt.pixelformat >> 24) & 0xff,
342 p->fmt.bytesperline, p->fmt.sizeimage,
343 p->fmt.colorspace);
344}
345
Hans Verkuile325b242012-06-09 12:50:15 -0300346static void v4l_print_buftype(const void *arg, bool write_only)
347{
348 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
349}
350
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300351static void v4l_print_modulator(const void *arg, bool write_only)
352{
353 const struct v4l2_modulator *p = arg;
354
355 if (write_only)
356 pr_cont("index=%u, txsubchans=0x%x", p->index, p->txsubchans);
357 else
Hans Verkuil27d5a872013-03-18 11:02:22 -0300358 pr_cont("index=%u, name=%.*s, capability=0x%x, "
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300359 "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300360 p->index, (int)sizeof(p->name), p->name, p->capability,
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300361 p->rangelow, p->rangehigh, p->txsubchans);
362}
363
364static void v4l_print_tuner(const void *arg, bool write_only)
365{
366 const struct v4l2_tuner *p = arg;
367
368 if (write_only)
369 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
370 else
Hans Verkuil27d5a872013-03-18 11:02:22 -0300371 pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, "
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300372 "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
373 "rxsubchans=0x%x, audmode=%u\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300374 p->index, (int)sizeof(p->name), p->name, p->type,
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300375 p->capability, p->rangelow,
376 p->rangehigh, p->signal, p->afc,
377 p->rxsubchans, p->audmode);
378}
379
380static void v4l_print_frequency(const void *arg, bool write_only)
381{
382 const struct v4l2_frequency *p = arg;
383
384 pr_cont("tuner=%u, type=%u, frequency=%u\n",
385 p->tuner, p->type, p->frequency);
386}
387
388static void v4l_print_standard(const void *arg, bool write_only)
389{
390 const struct v4l2_standard *p = arg;
391
Hans Verkuil27d5a872013-03-18 11:02:22 -0300392 pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, "
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300393 "framelines=%u\n", p->index,
Hans Verkuil27d5a872013-03-18 11:02:22 -0300394 (unsigned long long)p->id, (int)sizeof(p->name), p->name,
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300395 p->frameperiod.numerator,
396 p->frameperiod.denominator,
397 p->framelines);
398}
399
400static void v4l_print_std(const void *arg, bool write_only)
401{
402 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
403}
404
405static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
406{
407 const struct v4l2_hw_freq_seek *p = arg;
408
Hans Verkuil5e7883172012-08-01 16:18:41 -0300409 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, "
410 "rangelow=%u, rangehigh=%u\n",
411 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
412 p->rangelow, p->rangehigh);
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300413}
414
Hans Verkuileb3728e2012-06-22 06:23:59 -0300415static void v4l_print_requestbuffers(const void *arg, bool write_only)
Hans Verkuil59076122012-06-22 06:09:54 -0300416{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300417 const struct v4l2_requestbuffers *p = arg;
418
419 pr_cont("count=%d, type=%s, memory=%s\n",
420 p->count,
421 prt_names(p->type, v4l2_type_names),
422 prt_names(p->memory, v4l2_memory_names));
Hans Verkuil59076122012-06-22 06:09:54 -0300423}
424
Hans Verkuileb3728e2012-06-22 06:23:59 -0300425static void v4l_print_buffer(const void *arg, bool write_only)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300426{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300427 const struct v4l2_buffer *p = arg;
428 const struct v4l2_timecode *tc = &p->timecode;
429 const struct v4l2_plane *plane;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300430 int i;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300431
Hans Verkuileb3728e2012-06-22 06:23:59 -0300432 pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
433 "flags=0x%08x, field=%s, sequence=%d, memory=%s",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300434 p->timestamp.tv_sec / 3600,
435 (int)(p->timestamp.tv_sec / 60) % 60,
436 (int)(p->timestamp.tv_sec % 60),
Alexander Beregalovb0459792008-09-03 16:47:38 -0300437 (long)p->timestamp.tv_usec,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300438 p->index,
439 prt_names(p->type, v4l2_type_names),
Hans Verkuileb3728e2012-06-22 06:23:59 -0300440 p->flags, prt_names(p->field, v4l2_field_names),
441 p->sequence, prt_names(p->memory, v4l2_memory_names));
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300442
443 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300444 pr_cont("\n");
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300445 for (i = 0; i < p->length; ++i) {
446 plane = &p->m.planes[i];
Hans Verkuileb3728e2012-06-22 06:23:59 -0300447 printk(KERN_DEBUG
448 "plane %d: bytesused=%d, data_offset=0x%08x "
449 "offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300450 i, plane->bytesused, plane->data_offset,
451 plane->m.userptr, plane->length);
452 }
453 } else {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300454 pr_cont("bytesused=%d, offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300455 p->bytesused, p->m.userptr, p->length);
456 }
457
Hans Verkuileb3728e2012-06-22 06:23:59 -0300458 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
459 "flags=0x%08x, frames=%d, userbits=0x%08x\n",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300460 tc->hours, tc->minutes, tc->seconds,
461 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
462}
463
Tomasz Stanislawskib799d092012-06-14 11:32:23 -0300464static void v4l_print_exportbuffer(const void *arg, bool write_only)
465{
466 const struct v4l2_exportbuffer *p = arg;
467
468 pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
469 p->fd, prt_names(p->type, v4l2_type_names),
470 p->index, p->plane, p->flags);
471}
472
Hans Verkuileb3728e2012-06-22 06:23:59 -0300473static void v4l_print_create_buffers(const void *arg, bool write_only)
474{
475 const struct v4l2_create_buffers *p = arg;
476
477 pr_cont("index=%d, count=%d, memory=%s, ",
478 p->index, p->count,
479 prt_names(p->memory, v4l2_memory_names));
480 v4l_print_format(&p->format, write_only);
481}
482
483static void v4l_print_streamparm(const void *arg, bool write_only)
484{
485 const struct v4l2_streamparm *p = arg;
486
487 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
488
489 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
490 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
491 const struct v4l2_captureparm *c = &p->parm.capture;
492
493 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
494 "extendedmode=%d, readbuffers=%d\n",
495 c->capability, c->capturemode,
496 c->timeperframe.numerator, c->timeperframe.denominator,
497 c->extendedmode, c->readbuffers);
498 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
499 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
500 const struct v4l2_outputparm *c = &p->parm.output;
501
502 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
503 "extendedmode=%d, writebuffers=%d\n",
504 c->capability, c->outputmode,
505 c->timeperframe.numerator, c->timeperframe.denominator,
506 c->extendedmode, c->writebuffers);
507 }
508}
509
Hans Verkuilefbceec2012-06-09 12:54:02 -0300510static void v4l_print_queryctrl(const void *arg, bool write_only)
511{
512 const struct v4l2_queryctrl *p = arg;
513
Hans Verkuil27d5a872013-03-18 11:02:22 -0300514 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, "
Hans Verkuilefbceec2012-06-09 12:54:02 -0300515 "step=%d, default=%d, flags=0x%08x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300516 p->id, p->type, (int)sizeof(p->name), p->name,
Hans Verkuilefbceec2012-06-09 12:54:02 -0300517 p->minimum, p->maximum,
518 p->step, p->default_value, p->flags);
519}
520
521static void v4l_print_querymenu(const void *arg, bool write_only)
522{
523 const struct v4l2_querymenu *p = arg;
524
525 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
526}
527
528static void v4l_print_control(const void *arg, bool write_only)
529{
530 const struct v4l2_control *p = arg;
531
532 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
533}
534
535static void v4l_print_ext_controls(const void *arg, bool write_only)
536{
537 const struct v4l2_ext_controls *p = arg;
538 int i;
539
540 pr_cont("class=0x%x, count=%d, error_idx=%d",
541 p->ctrl_class, p->count, p->error_idx);
542 for (i = 0; i < p->count; i++) {
543 if (p->controls[i].size)
544 pr_cont(", id/val=0x%x/0x%x",
545 p->controls[i].id, p->controls[i].value);
546 else
547 pr_cont(", id/size=0x%x/%u",
548 p->controls[i].id, p->controls[i].size);
549 }
550 pr_cont("\n");
551}
552
Hans Verkuild84f2d942012-06-09 11:57:46 -0300553static void v4l_print_cropcap(const void *arg, bool write_only)
554{
555 const struct v4l2_cropcap *p = arg;
556
557 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
558 "defrect wxh=%dx%d, x,y=%d,%d\n, "
559 "pixelaspect %d/%d\n",
560 prt_names(p->type, v4l2_type_names),
561 p->bounds.width, p->bounds.height,
562 p->bounds.left, p->bounds.top,
563 p->defrect.width, p->defrect.height,
564 p->defrect.left, p->defrect.top,
565 p->pixelaspect.numerator, p->pixelaspect.denominator);
566}
567
568static void v4l_print_crop(const void *arg, bool write_only)
569{
570 const struct v4l2_crop *p = arg;
571
572 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
573 prt_names(p->type, v4l2_type_names),
574 p->c.width, p->c.height,
575 p->c.left, p->c.top);
576}
577
578static void v4l_print_selection(const void *arg, bool write_only)
579{
580 const struct v4l2_selection *p = arg;
581
582 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
583 prt_names(p->type, v4l2_type_names),
584 p->target, p->flags,
585 p->r.width, p->r.height, p->r.left, p->r.top);
586}
587
Hans Verkuild806f2d2012-06-09 10:02:49 -0300588static void v4l_print_jpegcompression(const void *arg, bool write_only)
589{
590 const struct v4l2_jpegcompression *p = arg;
591
592 pr_cont("quality=%d, APPn=%d, APP_len=%d, "
593 "COM_len=%d, jpeg_markers=0x%x\n",
594 p->quality, p->APPn, p->APP_len,
595 p->COM_len, p->jpeg_markers);
596}
597
598static void v4l_print_enc_idx(const void *arg, bool write_only)
599{
600 const struct v4l2_enc_idx *p = arg;
601
602 pr_cont("entries=%d, entries_cap=%d\n",
603 p->entries, p->entries_cap);
604}
605
606static void v4l_print_encoder_cmd(const void *arg, bool write_only)
607{
608 const struct v4l2_encoder_cmd *p = arg;
609
610 pr_cont("cmd=%d, flags=0x%x\n",
611 p->cmd, p->flags);
612}
613
614static void v4l_print_decoder_cmd(const void *arg, bool write_only)
615{
616 const struct v4l2_decoder_cmd *p = arg;
617
618 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
619
620 if (p->cmd == V4L2_DEC_CMD_START)
621 pr_info("speed=%d, format=%u\n",
622 p->start.speed, p->start.format);
623 else if (p->cmd == V4L2_DEC_CMD_STOP)
624 pr_info("pts=%llu\n", p->stop.pts);
625}
626
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300627static void v4l_print_dbg_chip_ident(const void *arg, bool write_only)
628{
629 const struct v4l2_dbg_chip_ident *p = arg;
630
631 pr_cont("type=%u, ", p->match.type);
632 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
Hans Verkuil27d5a872013-03-18 11:02:22 -0300633 pr_cont("name=%.*s, ",
634 (int)sizeof(p->match.name), p->match.name);
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300635 else
636 pr_cont("addr=%u, ", p->match.addr);
637 pr_cont("chip_ident=%u, revision=0x%x\n",
638 p->ident, p->revision);
639}
640
641static void v4l_print_dbg_register(const void *arg, bool write_only)
642{
643 const struct v4l2_dbg_register *p = arg;
644
645 pr_cont("type=%u, ", p->match.type);
646 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
Hans Verkuil27d5a872013-03-18 11:02:22 -0300647 pr_cont("name=%.*s, ",
648 (int)sizeof(p->match.name), p->match.name);
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300649 else
650 pr_cont("addr=%u, ", p->match.addr);
651 pr_cont("reg=0x%llx, val=0x%llx\n",
652 p->reg, p->val);
653}
654
Hans Verkuilefc626b2012-06-09 10:09:07 -0300655static void v4l_print_dv_enum_presets(const void *arg, bool write_only)
Hans Verkuileb3728e2012-06-22 06:23:59 -0300656{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300657 const struct v4l2_dv_enum_preset *p = arg;
658
Hans Verkuil27d5a872013-03-18 11:02:22 -0300659 pr_cont("index=%u, preset=%u, name=%.*s, width=%u, height=%u\n",
660 p->index, p->preset,
661 (int)sizeof(p->name), p->name, p->width, p->height);
Hans Verkuileb3728e2012-06-22 06:23:59 -0300662}
663
Hans Verkuilefc626b2012-06-09 10:09:07 -0300664static void v4l_print_dv_preset(const void *arg, bool write_only)
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300665{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300666 const struct v4l2_dv_preset *p = arg;
667
668 pr_cont("preset=%u\n", p->preset);
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300669}
670
Hans Verkuilefc626b2012-06-09 10:09:07 -0300671static void v4l_print_dv_timings(const void *arg, bool write_only)
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300672{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300673 const struct v4l2_dv_timings *p = arg;
674
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300675 switch (p->type) {
676 case V4L2_DV_BT_656_1120:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300677 pr_cont("type=bt-656/1120, interlaced=%u, "
678 "pixelclock=%llu, "
679 "width=%u, height=%u, polarities=0x%x, "
680 "hfrontporch=%u, hsync=%u, "
681 "hbackporch=%u, vfrontporch=%u, "
682 "vsync=%u, vbackporch=%u, "
683 "il_vfrontporch=%u, il_vsync=%u, "
684 "il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300685 p->bt.interlaced, p->bt.pixelclock,
686 p->bt.width, p->bt.height,
687 p->bt.polarities, p->bt.hfrontporch,
688 p->bt.hsync, p->bt.hbackporch,
689 p->bt.vfrontporch, p->bt.vsync,
690 p->bt.vbackporch, p->bt.il_vfrontporch,
691 p->bt.il_vsync, p->bt.il_vbackporch,
692 p->bt.standards, p->bt.flags);
693 break;
694 default:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300695 pr_cont("type=%d\n", p->type);
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300696 break;
697 }
698}
699
Hans Verkuilefc626b2012-06-09 10:09:07 -0300700static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
701{
702 const struct v4l2_enum_dv_timings *p = arg;
703
704 pr_cont("index=%u, ", p->index);
705 v4l_print_dv_timings(&p->timings, write_only);
706}
707
708static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
709{
710 const struct v4l2_dv_timings_cap *p = arg;
711
712 switch (p->type) {
713 case V4L2_DV_BT_656_1120:
714 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
715 "pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
716 p->bt.min_width, p->bt.max_width,
717 p->bt.min_height, p->bt.max_height,
718 p->bt.min_pixelclock, p->bt.max_pixelclock,
719 p->bt.standards, p->bt.capabilities);
720 break;
721 default:
722 pr_cont("type=%u\n", p->type);
723 break;
724 }
725}
726
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300727static void v4l_print_frmsizeenum(const void *arg, bool write_only)
728{
729 const struct v4l2_frmsizeenum *p = arg;
730
731 pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
732 p->index,
733 (p->pixel_format & 0xff),
734 (p->pixel_format >> 8) & 0xff,
735 (p->pixel_format >> 16) & 0xff,
736 (p->pixel_format >> 24) & 0xff,
737 p->type);
738 switch (p->type) {
739 case V4L2_FRMSIZE_TYPE_DISCRETE:
740 pr_cont(" wxh=%ux%u\n",
741 p->discrete.width, p->discrete.height);
742 break;
743 case V4L2_FRMSIZE_TYPE_STEPWISE:
744 pr_cont(" min=%ux%u, max=%ux%u, step=%ux%u\n",
745 p->stepwise.min_width, p->stepwise.min_height,
746 p->stepwise.step_width, p->stepwise.step_height,
747 p->stepwise.max_width, p->stepwise.max_height);
748 break;
749 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
750 /* fall through */
751 default:
752 pr_cont("\n");
753 break;
754 }
755}
756
757static void v4l_print_frmivalenum(const void *arg, bool write_only)
758{
759 const struct v4l2_frmivalenum *p = arg;
760
761 pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
762 p->index,
763 (p->pixel_format & 0xff),
764 (p->pixel_format >> 8) & 0xff,
765 (p->pixel_format >> 16) & 0xff,
766 (p->pixel_format >> 24) & 0xff,
767 p->width, p->height, p->type);
768 switch (p->type) {
769 case V4L2_FRMIVAL_TYPE_DISCRETE:
770 pr_cont(" fps=%d/%d\n",
771 p->discrete.numerator,
772 p->discrete.denominator);
773 break;
774 case V4L2_FRMIVAL_TYPE_STEPWISE:
775 pr_cont(" min=%d/%d, max=%d/%d, step=%d/%d\n",
776 p->stepwise.min.numerator,
777 p->stepwise.min.denominator,
778 p->stepwise.max.numerator,
779 p->stepwise.max.denominator,
780 p->stepwise.step.numerator,
781 p->stepwise.step.denominator);
782 break;
783 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
784 /* fall through */
785 default:
786 pr_cont("\n");
787 break;
788 }
789}
790
791static void v4l_print_event(const void *arg, bool write_only)
792{
793 const struct v4l2_event *p = arg;
794 const struct v4l2_event_ctrl *c;
795
796 pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
797 "timestamp=%lu.%9.9lu\n",
798 p->type, p->pending, p->sequence, p->id,
799 p->timestamp.tv_sec, p->timestamp.tv_nsec);
800 switch (p->type) {
801 case V4L2_EVENT_VSYNC:
802 printk(KERN_DEBUG "field=%s\n",
803 prt_names(p->u.vsync.field, v4l2_field_names));
804 break;
805 case V4L2_EVENT_CTRL:
806 c = &p->u.ctrl;
807 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
808 c->changes, c->type);
809 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
810 pr_cont("value64=%lld, ", c->value64);
811 else
812 pr_cont("value=%d, ", c->value);
813 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d,"
814 " default_value=%d\n",
815 c->flags, c->minimum, c->maximum,
816 c->step, c->default_value);
817 break;
818 case V4L2_EVENT_FRAME_SYNC:
819 pr_cont("frame_sequence=%u\n",
820 p->u.frame_sync.frame_sequence);
821 break;
822 }
823}
824
825static void v4l_print_event_subscription(const void *arg, bool write_only)
826{
827 const struct v4l2_event_subscription *p = arg;
828
829 pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
830 p->type, p->id, p->flags);
831}
832
833static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
834{
835 const struct v4l2_sliced_vbi_cap *p = arg;
836 int i;
837
838 pr_cont("type=%s, service_set=0x%08x\n",
839 prt_names(p->type, v4l2_type_names), p->service_set);
840 for (i = 0; i < 24; i++)
841 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
842 p->service_lines[0][i],
843 p->service_lines[1][i]);
844}
845
Hans Verkuil82b655b2012-07-05 06:37:08 -0300846static void v4l_print_freq_band(const void *arg, bool write_only)
847{
848 const struct v4l2_frequency_band *p = arg;
849
850 pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
851 "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
852 p->tuner, p->type, p->index,
853 p->capability, p->rangelow,
854 p->rangehigh, p->modulation);
855}
856
Hans Verkuilefc626b2012-06-09 10:09:07 -0300857static void v4l_print_u32(const void *arg, bool write_only)
858{
859 pr_cont("value=%u\n", *(const u32 *)arg);
860}
861
862static void v4l_print_newline(const void *arg, bool write_only)
863{
864 pr_cont("\n");
865}
866
Hans Verkuilf18d8e02012-06-22 06:35:01 -0300867static void v4l_print_default(const void *arg, bool write_only)
868{
869 pr_cont("driver-specific ioctl\n");
870}
871
Hans Verkuilefbceec2012-06-09 12:54:02 -0300872static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300873{
874 __u32 i;
875
876 /* zero the reserved fields */
877 c->reserved[0] = c->reserved[1] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300878 for (i = 0; i < c->count; i++)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300879 c->controls[i].reserved2[0] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300880
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300881 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
882 when using extended controls.
883 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
884 is it allowed for backwards compatibility.
885 */
886 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
887 return 0;
888 /* Check that all controls are from the same control class. */
889 for (i = 0; i < c->count; i++) {
890 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
891 c->error_idx = i;
892 return 0;
893 }
894 }
895 return 1;
896}
897
Hans Verkuil4b202592012-09-14 07:03:35 -0300898static int check_fmt(struct file *file, enum v4l2_buf_type type)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300899{
Hans Verkuil4b202592012-09-14 07:03:35 -0300900 struct video_device *vfd = video_devdata(file);
901 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
902 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
903 bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
904 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
905 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
906
Hans Verkuila3998102008-07-21 02:57:38 -0300907 if (ops == NULL)
908 return -EINVAL;
909
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300910 switch (type) {
911 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300912 if (is_vid && is_rx &&
913 (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300914 return 0;
915 break;
916 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300917 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300918 return 0;
919 break;
920 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -0300921 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300922 return 0;
923 break;
924 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300925 if (is_vid && is_tx &&
926 (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300927 return 0;
928 break;
929 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300930 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300931 return 0;
932 break;
933 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -0300934 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300935 return 0;
936 break;
937 case V4L2_BUF_TYPE_VBI_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300938 if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300939 return 0;
940 break;
941 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300942 if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300943 return 0;
944 break;
945 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300946 if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300947 return 0;
948 break;
949 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300950 if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300951 return 0;
952 break;
Hans Verkuil633c98e2012-09-17 05:02:26 -0300953 default:
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300954 break;
955 }
956 return -EINVAL;
957}
958
Hans Verkuil59076122012-06-22 06:09:54 -0300959static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
960 struct file *file, void *fh, void *arg)
961{
962 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
963
964 cap->version = LINUX_VERSION_CODE;
965 return ops->vidioc_querycap(file, fh, cap);
966}
967
968static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
969 struct file *file, void *fh, void *arg)
970{
971 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
972}
973
974static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
975 struct file *file, void *fh, void *arg)
976{
977 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
978}
979
Hans Verkuild0547cb2012-06-09 09:27:10 -0300980static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
981 struct file *file, void *fh, void *arg)
982{
983 struct video_device *vfd;
984 u32 *p = arg;
985
986 if (ops->vidioc_g_priority)
987 return ops->vidioc_g_priority(file, fh, arg);
988 vfd = video_devdata(file);
989 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
990 return 0;
991}
992
993static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
994 struct file *file, void *fh, void *arg)
995{
996 struct video_device *vfd;
997 struct v4l2_fh *vfh;
998 u32 *p = arg;
999
1000 if (ops->vidioc_s_priority)
1001 return ops->vidioc_s_priority(file, fh, *p);
1002 vfd = video_devdata(file);
1003 vfh = file->private_data;
1004 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
1005}
1006
Hans Verkuil59076122012-06-22 06:09:54 -03001007static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1008 struct file *file, void *fh, void *arg)
1009{
Hans Verkuil73f35412013-03-10 13:12:25 -03001010 struct video_device *vfd = video_devdata(file);
Hans Verkuil59076122012-06-22 06:09:54 -03001011 struct v4l2_input *p = arg;
1012
1013 /*
Hans Verkuil1c4f3c92012-09-03 10:38:41 -03001014 * We set the flags for CAP_PRESETS, CAP_DV_TIMINGS &
Hans Verkuil59076122012-06-22 06:09:54 -03001015 * CAP_STD here based on ioctl handler provided by the
1016 * driver. If the driver doesn't support these
1017 * for a specific input, it must override these flags.
1018 */
Hans Verkuil73f35412013-03-10 13:12:25 -03001019 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
Hans Verkuil59076122012-06-22 06:09:54 -03001020 p->capabilities |= V4L2_IN_CAP_STD;
Hans Verkuil73f35412013-03-10 13:12:25 -03001021 if (is_valid_ioctl(vfd, VIDIOC_S_DV_PRESET))
Hans Verkuil59076122012-06-22 06:09:54 -03001022 p->capabilities |= V4L2_IN_CAP_PRESETS;
Hans Verkuil73f35412013-03-10 13:12:25 -03001023 if (is_valid_ioctl(vfd, VIDIOC_S_DV_TIMINGS))
Hans Verkuil1c4f3c92012-09-03 10:38:41 -03001024 p->capabilities |= V4L2_IN_CAP_DV_TIMINGS;
Hans Verkuil59076122012-06-22 06:09:54 -03001025
1026 return ops->vidioc_enum_input(file, fh, p);
1027}
1028
1029static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1030 struct file *file, void *fh, void *arg)
1031{
Hans Verkuil73f35412013-03-10 13:12:25 -03001032 struct video_device *vfd = video_devdata(file);
Hans Verkuil59076122012-06-22 06:09:54 -03001033 struct v4l2_output *p = arg;
1034
1035 /*
Hans Verkuil1c4f3c92012-09-03 10:38:41 -03001036 * We set the flags for CAP_PRESETS, CAP_DV_TIMINGS &
Hans Verkuil59076122012-06-22 06:09:54 -03001037 * CAP_STD here based on ioctl handler provided by the
1038 * driver. If the driver doesn't support these
1039 * for a specific output, it must override these flags.
1040 */
Hans Verkuil73f35412013-03-10 13:12:25 -03001041 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
Hans Verkuil59076122012-06-22 06:09:54 -03001042 p->capabilities |= V4L2_OUT_CAP_STD;
Hans Verkuil73f35412013-03-10 13:12:25 -03001043 if (is_valid_ioctl(vfd, VIDIOC_S_DV_PRESET))
Hans Verkuil59076122012-06-22 06:09:54 -03001044 p->capabilities |= V4L2_OUT_CAP_PRESETS;
Hans Verkuil73f35412013-03-10 13:12:25 -03001045 if (is_valid_ioctl(vfd, VIDIOC_S_DV_TIMINGS))
Hans Verkuil1c4f3c92012-09-03 10:38:41 -03001046 p->capabilities |= V4L2_OUT_CAP_DV_TIMINGS;
Hans Verkuil59076122012-06-22 06:09:54 -03001047
1048 return ops->vidioc_enum_output(file, fh, p);
1049}
1050
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001051static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1052 struct file *file, void *fh, void *arg)
1053{
1054 struct v4l2_fmtdesc *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001055 struct video_device *vfd = video_devdata(file);
1056 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1057 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001058
1059 switch (p->type) {
1060 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001061 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001062 break;
1063 return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1064 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001065 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001066 break;
1067 return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1068 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001069 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001070 break;
1071 return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1072 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001073 if (unlikely(!is_tx || !ops->vidioc_enum_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001074 break;
1075 return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1076 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001077 if (unlikely(!is_tx || !ops->vidioc_enum_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001078 break;
1079 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001080 }
1081 return -EINVAL;
1082}
1083
1084static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1085 struct file *file, void *fh, void *arg)
1086{
1087 struct v4l2_format *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001088 struct video_device *vfd = video_devdata(file);
1089 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1090 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1091 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001092
1093 switch (p->type) {
1094 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001095 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001096 break;
1097 return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1098 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001099 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001100 break;
1101 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1102 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001103 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001104 break;
1105 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001106 case V4L2_BUF_TYPE_VBI_CAPTURE:
1107 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_vbi_cap))
1108 break;
1109 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1110 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1111 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_cap))
1112 break;
1113 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001114 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001115 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001116 break;
1117 return ops->vidioc_g_fmt_vid_out(file, fh, arg);
1118 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001119 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001120 break;
1121 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1122 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001123 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001124 break;
1125 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001126 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001127 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001128 break;
1129 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001130 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001131 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001132 break;
1133 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001134 }
1135 return -EINVAL;
1136}
1137
1138static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1139 struct file *file, void *fh, void *arg)
1140{
1141 struct v4l2_format *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001142 struct video_device *vfd = video_devdata(file);
1143 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1144 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1145 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001146
1147 switch (p->type) {
1148 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001149 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001150 break;
1151 CLEAR_AFTER_FIELD(p, fmt.pix);
1152 return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1153 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001154 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001155 break;
1156 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1157 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1158 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001159 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001160 break;
1161 CLEAR_AFTER_FIELD(p, fmt.win);
1162 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001163 case V4L2_BUF_TYPE_VBI_CAPTURE:
1164 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_vbi_cap))
1165 break;
1166 CLEAR_AFTER_FIELD(p, fmt.vbi);
1167 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1168 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1169 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_cap))
1170 break;
1171 CLEAR_AFTER_FIELD(p, fmt.sliced);
1172 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001173 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001174 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001175 break;
1176 CLEAR_AFTER_FIELD(p, fmt.pix);
1177 return ops->vidioc_s_fmt_vid_out(file, fh, arg);
1178 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001179 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001180 break;
1181 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1182 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1183 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001184 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001185 break;
1186 CLEAR_AFTER_FIELD(p, fmt.win);
1187 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001188 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001189 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001190 break;
1191 CLEAR_AFTER_FIELD(p, fmt.vbi);
1192 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001193 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001194 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001195 break;
1196 CLEAR_AFTER_FIELD(p, fmt.sliced);
1197 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001198 }
1199 return -EINVAL;
1200}
1201
1202static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1203 struct file *file, void *fh, void *arg)
1204{
1205 struct v4l2_format *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001206 struct video_device *vfd = video_devdata(file);
1207 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1208 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1209 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001210
1211 switch (p->type) {
1212 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001213 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001214 break;
1215 CLEAR_AFTER_FIELD(p, fmt.pix);
1216 return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1217 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001218 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001219 break;
1220 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1221 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1222 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001223 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001224 break;
1225 CLEAR_AFTER_FIELD(p, fmt.win);
1226 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001227 case V4L2_BUF_TYPE_VBI_CAPTURE:
1228 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_vbi_cap))
1229 break;
1230 CLEAR_AFTER_FIELD(p, fmt.vbi);
1231 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1232 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1233 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_cap))
1234 break;
1235 CLEAR_AFTER_FIELD(p, fmt.sliced);
1236 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001237 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001238 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001239 break;
1240 CLEAR_AFTER_FIELD(p, fmt.pix);
1241 return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1242 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001243 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001244 break;
1245 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1246 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1247 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001248 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001249 break;
1250 CLEAR_AFTER_FIELD(p, fmt.win);
1251 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001252 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001253 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001254 break;
1255 CLEAR_AFTER_FIELD(p, fmt.vbi);
1256 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001257 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001258 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001259 break;
1260 CLEAR_AFTER_FIELD(p, fmt.sliced);
1261 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001262 }
1263 return -EINVAL;
1264}
1265
Hans Verkuile325b242012-06-09 12:50:15 -03001266static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1267 struct file *file, void *fh, void *arg)
1268{
1269 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1270}
1271
1272static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1273 struct file *file, void *fh, void *arg)
1274{
1275 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1276}
1277
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001278static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1279 struct file *file, void *fh, void *arg)
1280{
1281 struct video_device *vfd = video_devdata(file);
1282 struct v4l2_tuner *p = arg;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001283 int err;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001284
1285 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1286 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001287 err = ops->vidioc_g_tuner(file, fh, p);
1288 if (!err)
1289 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1290 return err;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001291}
1292
1293static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1294 struct file *file, void *fh, void *arg)
1295{
1296 struct video_device *vfd = video_devdata(file);
1297 struct v4l2_tuner *p = arg;
1298
1299 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1300 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1301 return ops->vidioc_s_tuner(file, fh, p);
1302}
1303
Hans Verkuil82b655b2012-07-05 06:37:08 -03001304static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1305 struct file *file, void *fh, void *arg)
1306{
1307 struct v4l2_modulator *p = arg;
1308 int err;
1309
1310 err = ops->vidioc_g_modulator(file, fh, p);
1311 if (!err)
1312 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1313 return err;
1314}
1315
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001316static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1317 struct file *file, void *fh, void *arg)
1318{
1319 struct video_device *vfd = video_devdata(file);
1320 struct v4l2_frequency *p = arg;
1321
1322 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1323 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1324 return ops->vidioc_g_frequency(file, fh, p);
1325}
1326
1327static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1328 struct file *file, void *fh, void *arg)
1329{
1330 struct video_device *vfd = video_devdata(file);
Hans Verkuilb530a442013-03-19 04:09:26 -03001331 const struct v4l2_frequency *p = arg;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001332 enum v4l2_tuner_type type;
1333
1334 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1335 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1336 if (p->type != type)
1337 return -EINVAL;
1338 return ops->vidioc_s_frequency(file, fh, p);
1339}
1340
1341static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1342 struct file *file, void *fh, void *arg)
1343{
1344 struct video_device *vfd = video_devdata(file);
1345 struct v4l2_standard *p = arg;
1346 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1347 unsigned int index = p->index, i, j = 0;
1348 const char *descr = "";
1349
Hans Verkuila5338192012-09-14 06:45:43 -03001350 /* Return -ENODATA if the tvnorms for the current input
1351 or output is 0, meaning that it doesn't support this API. */
1352 if (id == 0)
1353 return -ENODATA;
1354
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001355 /* Return norm array in a canonical way */
1356 for (i = 0; i <= index && id; i++) {
1357 /* last std value in the standards array is 0, so this
1358 while always ends there since (id & 0) == 0. */
1359 while ((id & standards[j].std) != standards[j].std)
1360 j++;
1361 curr_id = standards[j].std;
1362 descr = standards[j].descr;
1363 j++;
1364 if (curr_id == 0)
1365 break;
1366 if (curr_id != V4L2_STD_PAL &&
1367 curr_id != V4L2_STD_SECAM &&
1368 curr_id != V4L2_STD_NTSC)
1369 id &= ~curr_id;
1370 }
1371 if (i <= index)
1372 return -EINVAL;
1373
1374 v4l2_video_std_construct(p, curr_id, descr);
1375 return 0;
1376}
1377
1378static int v4l_g_std(const struct v4l2_ioctl_ops *ops,
1379 struct file *file, void *fh, void *arg)
1380{
1381 struct video_device *vfd = video_devdata(file);
1382 v4l2_std_id *id = arg;
1383
1384 /* Calls the specific handler */
1385 if (ops->vidioc_g_std)
1386 return ops->vidioc_g_std(file, fh, arg);
1387 if (vfd->current_norm) {
1388 *id = vfd->current_norm;
1389 return 0;
1390 }
1391 return -ENOTTY;
1392}
1393
1394static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1395 struct file *file, void *fh, void *arg)
1396{
1397 struct video_device *vfd = video_devdata(file);
Hans Verkuil314527a2013-03-15 06:10:40 -03001398 v4l2_std_id id = *(v4l2_std_id *)arg, norm;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001399 int ret;
1400
Hans Verkuil314527a2013-03-15 06:10:40 -03001401 norm = id & vfd->tvnorms;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001402 if (vfd->tvnorms && !norm) /* Check if std is supported */
1403 return -EINVAL;
1404
1405 /* Calls the specific handler */
Hans Verkuil314527a2013-03-15 06:10:40 -03001406 ret = ops->vidioc_s_std(file, fh, norm);
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001407
1408 /* Updates standard information */
1409 if (ret >= 0)
1410 vfd->current_norm = norm;
1411 return ret;
1412}
1413
1414static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1415 struct file *file, void *fh, void *arg)
1416{
1417 struct video_device *vfd = video_devdata(file);
1418 v4l2_std_id *p = arg;
1419
1420 /*
1421 * If nothing detected, it should return all supported
1422 * standard.
1423 * Drivers just need to mask the std argument, in order
1424 * to remove the standards that don't apply from the mask.
1425 * This means that tuners, audio and video decoders can join
1426 * their efforts to improve the standards detection.
1427 */
1428 *p = vfd->tvnorms;
1429 return ops->vidioc_querystd(file, fh, arg);
1430}
1431
1432static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1433 struct file *file, void *fh, void *arg)
1434{
1435 struct video_device *vfd = video_devdata(file);
1436 struct v4l2_hw_freq_seek *p = arg;
1437 enum v4l2_tuner_type type;
1438
1439 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1440 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1441 if (p->type != type)
1442 return -EINVAL;
1443 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1444}
1445
Hans Verkuil737c0972012-09-04 10:08:01 -03001446static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
1447 struct file *file, void *fh, void *arg)
1448{
1449 return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
1450}
1451
Hans Verkuileb3728e2012-06-22 06:23:59 -03001452static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1453 struct file *file, void *fh, void *arg)
1454{
1455 struct v4l2_requestbuffers *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001456 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001457
1458 if (ret)
1459 return ret;
1460
Hans Verkuil633c98e2012-09-17 05:02:26 -03001461 CLEAR_AFTER_FIELD(p, memory);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001462
1463 return ops->vidioc_reqbufs(file, fh, p);
1464}
1465
1466static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1467 struct file *file, void *fh, void *arg)
1468{
1469 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001470 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001471
1472 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1473}
1474
1475static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1476 struct file *file, void *fh, void *arg)
1477{
1478 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001479 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001480
1481 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1482}
1483
1484static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1485 struct file *file, void *fh, void *arg)
1486{
1487 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001488 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001489
1490 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1491}
1492
1493static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1494 struct file *file, void *fh, void *arg)
1495{
1496 struct v4l2_create_buffers *create = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001497 int ret = check_fmt(file, create->format.type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001498
1499 return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
1500}
1501
1502static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1503 struct file *file, void *fh, void *arg)
1504{
1505 struct v4l2_buffer *b = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001506 int ret = check_fmt(file, b->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001507
1508 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1509}
1510
1511static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1512 struct file *file, void *fh, void *arg)
1513{
1514 struct video_device *vfd = video_devdata(file);
1515 struct v4l2_streamparm *p = arg;
1516 v4l2_std_id std;
Hans Verkuil4b202592012-09-14 07:03:35 -03001517 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001518
1519 if (ret)
1520 return ret;
1521 if (ops->vidioc_g_parm)
1522 return ops->vidioc_g_parm(file, fh, p);
1523 std = vfd->current_norm;
1524 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1525 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1526 return -EINVAL;
1527 p->parm.capture.readbuffers = 2;
Hans Verkuil73f35412013-03-10 13:12:25 -03001528 if (is_valid_ioctl(vfd, VIDIOC_G_STD) && ops->vidioc_g_std)
Hans Verkuileb3728e2012-06-22 06:23:59 -03001529 ret = ops->vidioc_g_std(file, fh, &std);
1530 if (ret == 0)
1531 v4l2_video_std_frame_period(std,
1532 &p->parm.capture.timeperframe);
1533 return ret;
1534}
1535
1536static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1537 struct file *file, void *fh, void *arg)
1538{
1539 struct v4l2_streamparm *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001540 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001541
1542 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1543}
1544
Hans Verkuilefbceec2012-06-09 12:54:02 -03001545static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1546 struct file *file, void *fh, void *arg)
1547{
1548 struct video_device *vfd = video_devdata(file);
1549 struct v4l2_queryctrl *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001550 struct v4l2_fh *vfh =
1551 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001552
1553 if (vfh && vfh->ctrl_handler)
1554 return v4l2_queryctrl(vfh->ctrl_handler, p);
1555 if (vfd->ctrl_handler)
1556 return v4l2_queryctrl(vfd->ctrl_handler, p);
1557 if (ops->vidioc_queryctrl)
1558 return ops->vidioc_queryctrl(file, fh, p);
1559 return -ENOTTY;
1560}
1561
1562static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1563 struct file *file, void *fh, void *arg)
1564{
1565 struct video_device *vfd = video_devdata(file);
1566 struct v4l2_querymenu *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001567 struct v4l2_fh *vfh =
1568 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001569
1570 if (vfh && vfh->ctrl_handler)
1571 return v4l2_querymenu(vfh->ctrl_handler, p);
1572 if (vfd->ctrl_handler)
1573 return v4l2_querymenu(vfd->ctrl_handler, p);
1574 if (ops->vidioc_querymenu)
1575 return ops->vidioc_querymenu(file, fh, p);
1576 return -ENOTTY;
1577}
1578
1579static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1580 struct file *file, void *fh, void *arg)
1581{
1582 struct video_device *vfd = video_devdata(file);
1583 struct v4l2_control *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001584 struct v4l2_fh *vfh =
1585 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001586 struct v4l2_ext_controls ctrls;
1587 struct v4l2_ext_control ctrl;
1588
1589 if (vfh && vfh->ctrl_handler)
1590 return v4l2_g_ctrl(vfh->ctrl_handler, p);
1591 if (vfd->ctrl_handler)
1592 return v4l2_g_ctrl(vfd->ctrl_handler, p);
1593 if (ops->vidioc_g_ctrl)
1594 return ops->vidioc_g_ctrl(file, fh, p);
1595 if (ops->vidioc_g_ext_ctrls == NULL)
1596 return -ENOTTY;
1597
1598 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1599 ctrls.count = 1;
1600 ctrls.controls = &ctrl;
1601 ctrl.id = p->id;
1602 ctrl.value = p->value;
1603 if (check_ext_ctrls(&ctrls, 1)) {
1604 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1605
1606 if (ret == 0)
1607 p->value = ctrl.value;
1608 return ret;
1609 }
1610 return -EINVAL;
1611}
1612
1613static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1614 struct file *file, void *fh, void *arg)
1615{
1616 struct video_device *vfd = video_devdata(file);
1617 struct v4l2_control *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001618 struct v4l2_fh *vfh =
1619 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001620 struct v4l2_ext_controls ctrls;
1621 struct v4l2_ext_control ctrl;
1622
1623 if (vfh && vfh->ctrl_handler)
1624 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1625 if (vfd->ctrl_handler)
1626 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1627 if (ops->vidioc_s_ctrl)
1628 return ops->vidioc_s_ctrl(file, fh, p);
1629 if (ops->vidioc_s_ext_ctrls == NULL)
1630 return -ENOTTY;
1631
1632 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1633 ctrls.count = 1;
1634 ctrls.controls = &ctrl;
1635 ctrl.id = p->id;
1636 ctrl.value = p->value;
1637 if (check_ext_ctrls(&ctrls, 1))
1638 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1639 return -EINVAL;
1640}
1641
1642static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1643 struct file *file, void *fh, void *arg)
1644{
1645 struct video_device *vfd = video_devdata(file);
1646 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001647 struct v4l2_fh *vfh =
1648 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001649
1650 p->error_idx = p->count;
1651 if (vfh && vfh->ctrl_handler)
1652 return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1653 if (vfd->ctrl_handler)
1654 return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1655 if (ops->vidioc_g_ext_ctrls == NULL)
1656 return -ENOTTY;
1657 return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1658 -EINVAL;
1659}
1660
1661static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1662 struct file *file, void *fh, void *arg)
1663{
1664 struct video_device *vfd = video_devdata(file);
1665 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001666 struct v4l2_fh *vfh =
1667 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001668
1669 p->error_idx = p->count;
1670 if (vfh && vfh->ctrl_handler)
1671 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1672 if (vfd->ctrl_handler)
1673 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1674 if (ops->vidioc_s_ext_ctrls == NULL)
1675 return -ENOTTY;
1676 return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1677 -EINVAL;
1678}
1679
1680static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1681 struct file *file, void *fh, void *arg)
1682{
1683 struct video_device *vfd = video_devdata(file);
1684 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001685 struct v4l2_fh *vfh =
1686 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001687
1688 p->error_idx = p->count;
1689 if (vfh && vfh->ctrl_handler)
1690 return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1691 if (vfd->ctrl_handler)
1692 return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1693 if (ops->vidioc_try_ext_ctrls == NULL)
1694 return -ENOTTY;
1695 return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1696 -EINVAL;
1697}
1698
Hans Verkuild84f2d942012-06-09 11:57:46 -03001699static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1700 struct file *file, void *fh, void *arg)
1701{
1702 struct v4l2_crop *p = arg;
1703 struct v4l2_selection s = {
1704 .type = p->type,
1705 };
1706 int ret;
1707
1708 if (ops->vidioc_g_crop)
1709 return ops->vidioc_g_crop(file, fh, p);
1710 /* simulate capture crop using selection api */
1711
1712 /* crop means compose for output devices */
1713 if (V4L2_TYPE_IS_OUTPUT(p->type))
1714 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1715 else
1716 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1717
1718 ret = ops->vidioc_g_selection(file, fh, &s);
1719
1720 /* copying results to old structure on success */
1721 if (!ret)
1722 p->c = s.r;
1723 return ret;
1724}
1725
1726static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1727 struct file *file, void *fh, void *arg)
1728{
1729 struct v4l2_crop *p = arg;
1730 struct v4l2_selection s = {
1731 .type = p->type,
1732 .r = p->c,
1733 };
1734
1735 if (ops->vidioc_s_crop)
1736 return ops->vidioc_s_crop(file, fh, p);
1737 /* simulate capture crop using selection api */
1738
1739 /* crop means compose for output devices */
1740 if (V4L2_TYPE_IS_OUTPUT(p->type))
1741 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1742 else
1743 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1744
1745 return ops->vidioc_s_selection(file, fh, &s);
1746}
1747
1748static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1749 struct file *file, void *fh, void *arg)
1750{
1751 struct v4l2_cropcap *p = arg;
1752 struct v4l2_selection s = { .type = p->type };
1753 int ret;
1754
1755 if (ops->vidioc_cropcap)
1756 return ops->vidioc_cropcap(file, fh, p);
1757
1758 /* obtaining bounds */
1759 if (V4L2_TYPE_IS_OUTPUT(p->type))
1760 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1761 else
1762 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1763
1764 ret = ops->vidioc_g_selection(file, fh, &s);
1765 if (ret)
1766 return ret;
1767 p->bounds = s.r;
1768
1769 /* obtaining defrect */
1770 if (V4L2_TYPE_IS_OUTPUT(p->type))
1771 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1772 else
1773 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1774
1775 ret = ops->vidioc_g_selection(file, fh, &s);
1776 if (ret)
1777 return ret;
1778 p->defrect = s.r;
1779
1780 /* setting trivial pixelaspect */
1781 p->pixelaspect.numerator = 1;
1782 p->pixelaspect.denominator = 1;
1783 return 0;
1784}
1785
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001786static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1787 struct file *file, void *fh, void *arg)
1788{
1789 struct video_device *vfd = video_devdata(file);
1790 int ret;
1791
1792 if (vfd->v4l2_dev)
1793 pr_info("%s: ================= START STATUS =================\n",
1794 vfd->v4l2_dev->name);
1795 ret = ops->vidioc_log_status(file, fh);
1796 if (vfd->v4l2_dev)
1797 pr_info("%s: ================== END STATUS ==================\n",
1798 vfd->v4l2_dev->name);
1799 return ret;
1800}
1801
1802static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1803 struct file *file, void *fh, void *arg)
1804{
1805#ifdef CONFIG_VIDEO_ADV_DEBUG
1806 struct v4l2_dbg_register *p = arg;
1807
1808 if (!capable(CAP_SYS_ADMIN))
1809 return -EPERM;
1810 return ops->vidioc_g_register(file, fh, p);
1811#else
1812 return -ENOTTY;
1813#endif
1814}
1815
1816static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1817 struct file *file, void *fh, void *arg)
1818{
1819#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001820 const struct v4l2_dbg_register *p = arg;
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001821
1822 if (!capable(CAP_SYS_ADMIN))
1823 return -EPERM;
1824 return ops->vidioc_s_register(file, fh, p);
1825#else
1826 return -ENOTTY;
1827#endif
1828}
1829
1830static int v4l_dbg_g_chip_ident(const struct v4l2_ioctl_ops *ops,
1831 struct file *file, void *fh, void *arg)
1832{
1833 struct v4l2_dbg_chip_ident *p = arg;
1834
1835 p->ident = V4L2_IDENT_NONE;
1836 p->revision = 0;
1837 return ops->vidioc_g_chip_ident(file, fh, p);
1838}
1839
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001840static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
1841 struct file *file, void *fh, void *arg)
1842{
1843 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
1844}
1845
1846static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
1847 struct file *file, void *fh, void *arg)
1848{
1849 return ops->vidioc_subscribe_event(fh, arg);
1850}
1851
1852static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
1853 struct file *file, void *fh, void *arg)
1854{
1855 return ops->vidioc_unsubscribe_event(fh, arg);
1856}
1857
1858static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
1859 struct file *file, void *fh, void *arg)
1860{
1861 struct v4l2_sliced_vbi_cap *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001862 int ret = check_fmt(file, p->type);
1863
1864 if (ret)
1865 return ret;
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001866
1867 /* Clear up to type, everything after type is zeroed already */
1868 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1869
1870 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1871}
1872
Hans Verkuil82b655b2012-07-05 06:37:08 -03001873static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
1874 struct file *file, void *fh, void *arg)
1875{
1876 struct video_device *vfd = video_devdata(file);
1877 struct v4l2_frequency_band *p = arg;
1878 enum v4l2_tuner_type type;
1879 int err;
1880
1881 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1882 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1883
1884 if (type != p->type)
1885 return -EINVAL;
1886 if (ops->vidioc_enum_freq_bands)
1887 return ops->vidioc_enum_freq_bands(file, fh, p);
Hans Verkuil73f35412013-03-10 13:12:25 -03001888 if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
Hans Verkuil82b655b2012-07-05 06:37:08 -03001889 struct v4l2_tuner t = {
1890 .index = p->tuner,
1891 .type = type,
1892 };
1893
Hans Verkuilaa4d9b52012-08-01 15:52:46 -03001894 if (p->index)
1895 return -EINVAL;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001896 err = ops->vidioc_g_tuner(file, fh, &t);
1897 if (err)
1898 return err;
1899 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1900 p->rangelow = t.rangelow;
1901 p->rangehigh = t.rangehigh;
1902 p->modulation = (type == V4L2_TUNER_RADIO) ?
1903 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1904 return 0;
1905 }
Hans Verkuil73f35412013-03-10 13:12:25 -03001906 if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
Hans Verkuil82b655b2012-07-05 06:37:08 -03001907 struct v4l2_modulator m = {
1908 .index = p->tuner,
1909 };
1910
1911 if (type != V4L2_TUNER_RADIO)
1912 return -EINVAL;
Hans Verkuilaa4d9b52012-08-01 15:52:46 -03001913 if (p->index)
1914 return -EINVAL;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001915 err = ops->vidioc_g_modulator(file, fh, &m);
1916 if (err)
1917 return err;
1918 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1919 p->rangelow = m.rangelow;
1920 p->rangehigh = m.rangehigh;
1921 p->modulation = (type == V4L2_TUNER_RADIO) ?
1922 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
1923 return 0;
1924 }
1925 return -ENOTTY;
1926}
1927
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001928struct v4l2_ioctl_info {
1929 unsigned int ioctl;
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001930 u32 flags;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001931 const char * const name;
Hans Verkuil86748f32012-06-22 06:04:16 -03001932 union {
1933 u32 offset;
1934 int (*func)(const struct v4l2_ioctl_ops *ops,
1935 struct file *file, void *fh, void *p);
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001936 } u;
Hans Verkuil86748f32012-06-22 06:04:16 -03001937 void (*debug)(const void *arg, bool write_only);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001938};
1939
1940/* This control needs a priority check */
1941#define INFO_FL_PRIO (1 << 0)
1942/* This control can be valid if the filehandle passes a control handler. */
1943#define INFO_FL_CTRL (1 << 1)
Hans Verkuil86748f32012-06-22 06:04:16 -03001944/* This is a standard ioctl, no need for special code */
1945#define INFO_FL_STD (1 << 2)
1946/* This is ioctl has its own function */
1947#define INFO_FL_FUNC (1 << 3)
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001948/* Queuing ioctl */
1949#define INFO_FL_QUEUE (1 << 4)
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03001950/* Zero struct from after the field to the end */
1951#define INFO_FL_CLEAR(v4l2_struct, field) \
1952 ((offsetof(struct v4l2_struct, field) + \
1953 sizeof(((struct v4l2_struct *)0)->field)) << 16)
1954#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001955
Hans Verkuil86748f32012-06-22 06:04:16 -03001956#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
1957 [_IOC_NR(_ioctl)] = { \
1958 .ioctl = _ioctl, \
1959 .flags = _flags | INFO_FL_STD, \
1960 .name = #_ioctl, \
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001961 .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
Hans Verkuil86748f32012-06-22 06:04:16 -03001962 .debug = _debug, \
1963 }
1964
1965#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
1966 [_IOC_NR(_ioctl)] = { \
1967 .ioctl = _ioctl, \
1968 .flags = _flags | INFO_FL_FUNC, \
1969 .name = #_ioctl, \
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03001970 .u.func = _func, \
Hans Verkuil86748f32012-06-22 06:04:16 -03001971 .debug = _debug, \
1972 }
1973
Hans Verkuil4839e6c2012-06-04 05:23:40 -03001974static struct v4l2_ioctl_info v4l2_ioctls[] = {
Hans Verkuil59076122012-06-22 06:09:54 -03001975 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001976 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
1977 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
1978 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001979 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
1980 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 -03001981 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
1982 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
Hans Verkuil737c0972012-09-04 10:08:01 -03001983 IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03001984 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
Tomasz Stanislawskib799d092012-06-14 11:32:23 -03001985 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 -03001986 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
1987 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
1988 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
Hans Verkuileb3728e2012-06-22 06:23:59 -03001989 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
1990 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001991 IOCTL_INFO_FNC(VIDIOC_G_STD, v4l_g_std, v4l_print_std, 0),
1992 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
1993 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
Hans Verkuil59076122012-06-22 06:09:54 -03001994 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
Hans Verkuilefbceec2012-06-09 12:54:02 -03001995 IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
1996 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 -03001997 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
1998 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
Hans Verkuil59076122012-06-22 06:09:54 -03001999 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
2000 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
Hans Verkuilefbceec2012-06-09 12:54:02 -03002001 IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2002 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 -03002003 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
2004 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
2005 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
2006 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2007 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2008 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
2009 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
Hans Verkuil82b655b2012-07-05 06:37:08 -03002010 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 -03002011 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2012 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2013 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
Hans Verkuild84f2d942012-06-09 11:57:46 -03002014 IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2015 IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2016 IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2017 IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
2018 IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
Hans Verkuild806f2d2012-06-09 10:02:49 -03002019 IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
2020 IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03002021 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03002022 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
Hans Verkuil59076122012-06-22 06:09:54 -03002023 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2024 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
Hans Verkuild0547cb2012-06-09 09:27:10 -03002025 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2026 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002027 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 -03002028 IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
Hans Verkuilefbceec2012-06-09 12:54:02 -03002029 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2030 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 -03002031 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 -03002032 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2033 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 -03002034 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
2035 IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2036 IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2037 IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2038 IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
Hans Verkuilf16f77b2012-06-09 10:06:25 -03002039 IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2040 IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2041 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 -03002042 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 -03002043 IOCTL_INFO_STD(VIDIOC_ENUM_DV_PRESETS, vidioc_enum_dv_presets, v4l_print_dv_enum_presets, 0),
2044 IOCTL_INFO_STD(VIDIOC_S_DV_PRESET, vidioc_s_dv_preset, v4l_print_dv_preset, INFO_FL_PRIO),
2045 IOCTL_INFO_STD(VIDIOC_G_DV_PRESET, vidioc_g_dv_preset, v4l_print_dv_preset, 0),
2046 IOCTL_INFO_STD(VIDIOC_QUERY_DV_PRESET, vidioc_query_dv_preset, v4l_print_dv_preset, 0),
2047 IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
2048 IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002049 IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2050 IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2051 IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002052 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2053 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
Hans Verkuilefc626b2012-06-09 10:09:07 -03002054 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
2055 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuila1acb8f2012-07-11 09:15:06 -03002056 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 -03002057 IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002058};
2059#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2060
2061bool v4l2_is_known_ioctl(unsigned int cmd)
2062{
2063 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2064 return false;
2065 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2066}
2067
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002068struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
2069{
2070 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2071 return vdev->lock;
2072 if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
2073 return NULL;
2074 if (vdev->queue && vdev->queue->lock &&
2075 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2076 return vdev->queue->lock;
2077 return vdev->lock;
2078}
2079
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002080/* Common ioctl debug function. This function can be used by
2081 external ioctl messages as well as internal V4L ioctl */
Hans Verkuil4a085162012-06-22 06:38:06 -03002082void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002083{
Hans Verkuil86748f32012-06-22 06:04:16 -03002084 const char *dir, *type;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002085
Hans Verkuil4a085162012-06-22 06:38:06 -03002086 if (prefix)
2087 printk(KERN_DEBUG "%s: ", prefix);
2088
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002089 switch (_IOC_TYPE(cmd)) {
2090 case 'd':
2091 type = "v4l2_int";
2092 break;
2093 case 'V':
2094 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2095 type = "v4l2";
2096 break;
2097 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002098 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002099 return;
2100 default:
2101 type = "unknown";
Hans Verkuil86748f32012-06-22 06:04:16 -03002102 break;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002103 }
2104
2105 switch (_IOC_DIR(cmd)) {
2106 case _IOC_NONE: dir = "--"; break;
2107 case _IOC_READ: dir = "r-"; break;
2108 case _IOC_WRITE: dir = "-w"; break;
2109 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2110 default: dir = "*ERR*"; break;
2111 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002112 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002113 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2114}
2115EXPORT_SYMBOL(v4l_printk_ioctl);
2116
Hans Verkuil069b7472008-12-30 07:04:34 -03002117static long __video_do_ioctl(struct file *file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002118 unsigned int cmd, void *arg)
2119{
2120 struct video_device *vfd = video_devdata(file);
Hans Verkuila3998102008-07-21 02:57:38 -03002121 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
Hans Verkuil86748f32012-06-22 06:04:16 -03002122 bool write_only = false;
2123 struct v4l2_ioctl_info default_info;
2124 const struct v4l2_ioctl_info *info;
Hans Verkuild5fbf322008-10-18 13:39:53 -03002125 void *fh = file->private_data;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03002126 struct v4l2_fh *vfh = NULL;
Hans Verkuilb1a873a2011-03-22 10:14:07 -03002127 int use_fh_prio = 0;
Hans Verkuil80131fe02012-06-22 06:37:38 -03002128 int debug = vfd->debug;
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002129 long ret = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002130
Hans Verkuil6a717882010-04-06 15:56:08 -03002131 if (ops == NULL) {
Hans Verkuil4a085162012-06-22 06:38:06 -03002132 pr_warn("%s: has no ioctl_ops.\n",
2133 video_device_node_name(vfd));
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002134 return ret;
Hans Verkuil6a717882010-04-06 15:56:08 -03002135 }
2136
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002137 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) {
2138 vfh = file->private_data;
2139 use_fh_prio = test_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002140 }
2141
2142 if (v4l2_is_known_ioctl(cmd)) {
Hans Verkuil86748f32012-06-22 06:04:16 -03002143 info = &v4l2_ioctls[_IOC_NR(cmd)];
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002144
2145 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2146 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
Hans Verkuil86748f32012-06-22 06:04:16 -03002147 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002148
2149 if (use_fh_prio && (info->flags & INFO_FL_PRIO)) {
2150 ret = v4l2_prio_check(vfd->prio, vfh->prio);
2151 if (ret)
Hans Verkuil86748f32012-06-22 06:04:16 -03002152 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002153 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002154 } else {
2155 default_info.ioctl = cmd;
2156 default_info.flags = 0;
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002157 default_info.debug = v4l_print_default;
Hans Verkuil86748f32012-06-22 06:04:16 -03002158 info = &default_info;
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002159 }
2160
Hans Verkuil86748f32012-06-22 06:04:16 -03002161 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
Hans Verkuil86748f32012-06-22 06:04:16 -03002162 if (info->flags & INFO_FL_STD) {
2163 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2164 const void *p = vfd->ioctl_ops;
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002165 const vidioc_op *vidioc = p + info->u.offset;
Hans Verkuil86748f32012-06-22 06:04:16 -03002166
2167 ret = (*vidioc)(file, fh, arg);
Hans Verkuil86748f32012-06-22 06:04:16 -03002168 } else if (info->flags & INFO_FL_FUNC) {
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002169 ret = info->u.func(ops, file, fh, arg);
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002170 } else if (!ops->vidioc_default) {
2171 ret = -ENOTTY;
2172 } else {
2173 ret = ops->vidioc_default(file, fh,
2174 use_fh_prio ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2175 cmd, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002176 }
2177
Hans Verkuil86748f32012-06-22 06:04:16 -03002178done:
Hans Verkuil80131fe02012-06-22 06:37:38 -03002179 if (debug) {
Hans Verkuil4a085162012-06-22 06:38:06 -03002180 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03002181 if (ret < 0)
Hans Verkuil505d04b2013-03-14 07:40:45 -03002182 pr_cont(": error %ld", ret);
2183 if (debug == V4L2_DEBUG_IOCTL)
Hans Verkuil86748f32012-06-22 06:04:16 -03002184 pr_cont("\n");
Hans Verkuil86748f32012-06-22 06:04:16 -03002185 else if (_IOC_DIR(cmd) == _IOC_NONE)
2186 info->debug(arg, write_only);
2187 else {
2188 pr_cont(": ");
2189 info->debug(arg, write_only);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002190 }
2191 }
2192
2193 return ret;
2194}
2195
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002196static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2197 void * __user *user_ptr, void ***kernel_ptr)
2198{
2199 int ret = 0;
2200
2201 switch (cmd) {
Hans Verkuil96b1a702012-09-19 10:14:41 -03002202 case VIDIOC_PREPARE_BUF:
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002203 case VIDIOC_QUERYBUF:
2204 case VIDIOC_QBUF:
2205 case VIDIOC_DQBUF: {
2206 struct v4l2_buffer *buf = parg;
2207
2208 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2209 if (buf->length > VIDEO_MAX_PLANES) {
2210 ret = -EINVAL;
2211 break;
2212 }
2213 *user_ptr = (void __user *)buf->m.planes;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002214 *kernel_ptr = (void *)&buf->m.planes;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002215 *array_size = sizeof(struct v4l2_plane) * buf->length;
2216 ret = 1;
2217 }
2218 break;
2219 }
2220
Hans Verkuiled45ce22012-08-10 06:07:12 -03002221 case VIDIOC_SUBDEV_G_EDID:
2222 case VIDIOC_SUBDEV_S_EDID: {
2223 struct v4l2_subdev_edid *edid = parg;
2224
2225 if (edid->blocks) {
Hans Verkuil1b8b10c2012-10-02 02:47:58 -03002226 if (edid->blocks > 256) {
2227 ret = -EINVAL;
2228 break;
2229 }
Hans Verkuiled45ce22012-08-10 06:07:12 -03002230 *user_ptr = (void __user *)edid->edid;
2231 *kernel_ptr = (void *)&edid->edid;
2232 *array_size = edid->blocks * 128;
2233 ret = 1;
2234 }
2235 break;
2236 }
2237
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002238 case VIDIOC_S_EXT_CTRLS:
2239 case VIDIOC_G_EXT_CTRLS:
2240 case VIDIOC_TRY_EXT_CTRLS: {
2241 struct v4l2_ext_controls *ctrls = parg;
2242
2243 if (ctrls->count != 0) {
Dan Carpenter6c061082012-01-05 02:27:57 -03002244 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2245 ret = -EINVAL;
2246 break;
2247 }
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002248 *user_ptr = (void __user *)ctrls->controls;
Hans Petter Selasky2ef40372011-05-23 08:13:06 -03002249 *kernel_ptr = (void *)&ctrls->controls;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002250 *array_size = sizeof(struct v4l2_ext_control)
2251 * ctrls->count;
2252 ret = 1;
2253 }
2254 break;
2255 }
2256 }
2257
2258 return ret;
2259}
2260
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002261long
2262video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2263 v4l2_kioctl func)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002264{
2265 char sbuf[128];
2266 void *mbuf = NULL;
Hans Verkuil1d94aa32010-04-06 08:12:21 -03002267 void *parg = (void *)arg;
Hans Verkuil069b7472008-12-30 07:04:34 -03002268 long err = -EINVAL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002269 bool has_array_args;
2270 size_t array_size = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002271 void __user *user_ptr = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002272 void **kernel_ptr = NULL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002273
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002274 /* Copy arguments into temp kernel buffer */
Trent Piepho337f9d22009-03-04 01:21:02 -03002275 if (_IOC_DIR(cmd) != _IOC_NONE) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002276 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2277 parg = sbuf;
2278 } else {
2279 /* too big to allocate from stack */
2280 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2281 if (NULL == mbuf)
2282 return -ENOMEM;
2283 parg = mbuf;
2284 }
2285
2286 err = -EFAULT;
Trent Piepho19c96e42009-03-04 01:21:02 -03002287 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002288 unsigned int n = _IOC_SIZE(cmd);
2289
2290 /*
2291 * In some cases, only a few fields are used as input,
2292 * i.e. when the app sets "index" and then the driver
2293 * fills in the rest of the structure for the thing
2294 * with that index. We only need to copy up the first
2295 * non-input field.
2296 */
2297 if (v4l2_is_known_ioctl(cmd)) {
2298 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2299 if (flags & INFO_FL_CLEAR_MASK)
2300 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2301 }
Trent Piepho19c96e42009-03-04 01:21:02 -03002302
2303 if (copy_from_user(parg, (void __user *)arg, n))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002304 goto out;
Trent Piepho19c96e42009-03-04 01:21:02 -03002305
2306 /* zero out anything we don't copy from userspace */
2307 if (n < _IOC_SIZE(cmd))
2308 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
Trent Piepho337f9d22009-03-04 01:21:02 -03002309 } else {
2310 /* read-only ioctl */
2311 memset(parg, 0, _IOC_SIZE(cmd));
Trent Piepho19c96e42009-03-04 01:21:02 -03002312 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002313 }
2314
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002315 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2316 if (err < 0)
2317 goto out;
2318 has_array_args = err;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002319
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002320 if (has_array_args) {
2321 /*
2322 * When adding new types of array args, make sure that the
2323 * parent argument to ioctl (which contains the pointer to the
2324 * array) fits into sbuf (so that mbuf will still remain
2325 * unused up to here).
2326 */
2327 mbuf = kmalloc(array_size, GFP_KERNEL);
2328 err = -ENOMEM;
2329 if (NULL == mbuf)
2330 goto out_array_args;
2331 err = -EFAULT;
2332 if (copy_from_user(mbuf, user_ptr, array_size))
2333 goto out_array_args;
2334 *kernel_ptr = mbuf;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002335 }
2336
2337 /* Handles IOCTL */
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002338 err = func(file, cmd, parg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002339 if (err == -ENOIOCTLCMD)
Hans Verkuil02bbb812012-02-08 08:09:36 -03002340 err = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002341
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002342 if (has_array_args) {
2343 *kernel_ptr = user_ptr;
2344 if (copy_to_user(user_ptr, mbuf, array_size))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002345 err = -EFAULT;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002346 goto out_array_args;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002347 }
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002348 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2349 results that must be returned. */
2350 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002351 goto out;
2352
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002353out_array_args:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002354 /* Copy results into user buffer */
2355 switch (_IOC_DIR(cmd)) {
2356 case _IOC_READ:
2357 case (_IOC_WRITE | _IOC_READ):
2358 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2359 err = -EFAULT;
2360 break;
2361 }
2362
2363out:
2364 kfree(mbuf);
2365 return err;
2366}
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002367EXPORT_SYMBOL(video_usercopy);
2368
2369long video_ioctl2(struct file *file,
2370 unsigned int cmd, unsigned long arg)
2371{
2372 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2373}
Mauro Carvalho Chehab8a522c92008-10-21 11:58:39 -03002374EXPORT_SYMBOL(video_ioctl2);