blob: 00ceedf16bb70a07c94ac442433a113916c738cc [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 Verkuil5a5adf62012-06-22 07:29:35 -030029#include <media/videobuf2-core.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030030
Hans Verkuilaa32f4c2013-12-16 05:45:37 -030031#define CREATE_TRACE_POINTS
32#include <trace/events/v4l2.h>
33
Lucas De Marchi25985ed2011-03-30 22:57:33 -030034/* Zero out the end of the struct pointed to by p. Everything after, but
Trent Piepho7ecc0cf2009-04-30 21:03:34 -030035 * not including, the specified field is cleared. */
36#define CLEAR_AFTER_FIELD(p, field) \
37 memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
38 0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
39
Hans Verkuil73f35412013-03-10 13:12:25 -030040#define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
41
Hans Verkuil35ea11f2008-07-20 08:12:02 -030042struct std_descr {
43 v4l2_std_id std;
44 const char *descr;
45};
46
47static const struct std_descr standards[] = {
48 { V4L2_STD_NTSC, "NTSC" },
49 { V4L2_STD_NTSC_M, "NTSC-M" },
50 { V4L2_STD_NTSC_M_JP, "NTSC-M-JP" },
51 { V4L2_STD_NTSC_M_KR, "NTSC-M-KR" },
52 { V4L2_STD_NTSC_443, "NTSC-443" },
53 { V4L2_STD_PAL, "PAL" },
54 { V4L2_STD_PAL_BG, "PAL-BG" },
55 { V4L2_STD_PAL_B, "PAL-B" },
56 { V4L2_STD_PAL_B1, "PAL-B1" },
57 { V4L2_STD_PAL_G, "PAL-G" },
58 { V4L2_STD_PAL_H, "PAL-H" },
59 { V4L2_STD_PAL_I, "PAL-I" },
60 { V4L2_STD_PAL_DK, "PAL-DK" },
61 { V4L2_STD_PAL_D, "PAL-D" },
62 { V4L2_STD_PAL_D1, "PAL-D1" },
63 { V4L2_STD_PAL_K, "PAL-K" },
64 { V4L2_STD_PAL_M, "PAL-M" },
65 { V4L2_STD_PAL_N, "PAL-N" },
66 { V4L2_STD_PAL_Nc, "PAL-Nc" },
67 { V4L2_STD_PAL_60, "PAL-60" },
68 { V4L2_STD_SECAM, "SECAM" },
69 { V4L2_STD_SECAM_B, "SECAM-B" },
70 { V4L2_STD_SECAM_G, "SECAM-G" },
71 { V4L2_STD_SECAM_H, "SECAM-H" },
72 { V4L2_STD_SECAM_DK, "SECAM-DK" },
73 { V4L2_STD_SECAM_D, "SECAM-D" },
74 { V4L2_STD_SECAM_K, "SECAM-K" },
75 { V4L2_STD_SECAM_K1, "SECAM-K1" },
76 { V4L2_STD_SECAM_L, "SECAM-L" },
77 { V4L2_STD_SECAM_LC, "SECAM-Lc" },
78 { 0, "Unknown" }
79};
80
81/* video4linux standard ID conversion to standard name
82 */
83const char *v4l2_norm_to_name(v4l2_std_id id)
84{
85 u32 myid = id;
86 int i;
87
88 /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
89 64 bit comparations. So, on that architecture, with some gcc
90 variants, compilation fails. Currently, the max value is 30bit wide.
91 */
92 BUG_ON(myid != id);
93
94 for (i = 0; standards[i].std; i++)
95 if (myid == standards[i].std)
96 break;
97 return standards[i].descr;
98}
99EXPORT_SYMBOL(v4l2_norm_to_name);
100
Trent Piepho51f0b8d52009-03-04 01:21:02 -0300101/* Returns frame period for the given standard */
102void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
103{
104 if (id & V4L2_STD_525_60) {
105 frameperiod->numerator = 1001;
106 frameperiod->denominator = 30000;
107 } else {
108 frameperiod->numerator = 1;
109 frameperiod->denominator = 25;
110 }
111}
112EXPORT_SYMBOL(v4l2_video_std_frame_period);
113
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300114/* Fill in the fields of a v4l2_standard structure according to the
115 'id' and 'transmission' parameters. Returns negative on error. */
116int v4l2_video_std_construct(struct v4l2_standard *vs,
117 int id, const char *name)
118{
Trent Piepho51f0b8d52009-03-04 01:21:02 -0300119 vs->id = id;
120 v4l2_video_std_frame_period(id, &vs->frameperiod);
121 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300122 strlcpy(vs->name, name, sizeof(vs->name));
123 return 0;
124}
125EXPORT_SYMBOL(v4l2_video_std_construct);
126
127/* ----------------------------------------------------------------- */
128/* some arrays for pretty-printing debug messages of enum types */
129
130const char *v4l2_field_names[] = {
131 [V4L2_FIELD_ANY] = "any",
132 [V4L2_FIELD_NONE] = "none",
133 [V4L2_FIELD_TOP] = "top",
134 [V4L2_FIELD_BOTTOM] = "bottom",
135 [V4L2_FIELD_INTERLACED] = "interlaced",
136 [V4L2_FIELD_SEQ_TB] = "seq-tb",
137 [V4L2_FIELD_SEQ_BT] = "seq-bt",
138 [V4L2_FIELD_ALTERNATE] = "alternate",
139 [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
140 [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
141};
142EXPORT_SYMBOL(v4l2_field_names);
143
144const char *v4l2_type_names[] = {
145 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "vid-cap",
146 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "vid-overlay",
147 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "vid-out",
148 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
149 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
150 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
151 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "sliced-vbi-out",
152 [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
Pawel Osciakf8f39142010-07-29 14:44:25 -0300153 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
154 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
Antti Palosaari6f3073b2013-12-12 13:34:30 -0300155 [V4L2_BUF_TYPE_SDR_CAPTURE] = "sdr-cap",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300156};
157EXPORT_SYMBOL(v4l2_type_names);
158
159static const char *v4l2_memory_names[] = {
160 [V4L2_MEMORY_MMAP] = "mmap",
161 [V4L2_MEMORY_USERPTR] = "userptr",
162 [V4L2_MEMORY_OVERLAY] = "overlay",
Sumit Semwal051c7782012-06-14 10:37:35 -0300163 [V4L2_MEMORY_DMABUF] = "dmabuf",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300164};
165
Hans Verkuild9246242012-10-02 02:47:59 -0300166#define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300167
168/* ------------------------------------------------------------------ */
169/* debug help functions */
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300170
Hans Verkuil59076122012-06-22 06:09:54 -0300171static void v4l_print_querycap(const void *arg, bool write_only)
172{
173 const struct v4l2_capability *p = arg;
174
Hans Verkuil27d5a872013-03-18 11:02:22 -0300175 pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, "
Hans Verkuil59076122012-06-22 06:09:54 -0300176 "capabilities=0x%08x, device_caps=0x%08x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300177 (int)sizeof(p->driver), p->driver,
178 (int)sizeof(p->card), p->card,
179 (int)sizeof(p->bus_info), p->bus_info,
Hans Verkuil59076122012-06-22 06:09:54 -0300180 p->version, p->capabilities, p->device_caps);
181}
182
183static void v4l_print_enuminput(const void *arg, bool write_only)
184{
185 const struct v4l2_input *p = arg;
186
Hans Verkuil27d5a872013-03-18 11:02:22 -0300187 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, "
Hans Verkuil59076122012-06-22 06:09:54 -0300188 "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300189 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
190 p->tuner, (unsigned long long)p->std, p->status,
191 p->capabilities);
Hans Verkuil59076122012-06-22 06:09:54 -0300192}
193
194static void v4l_print_enumoutput(const void *arg, bool write_only)
195{
196 const struct v4l2_output *p = arg;
197
Hans Verkuil27d5a872013-03-18 11:02:22 -0300198 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, "
Hans Verkuil59076122012-06-22 06:09:54 -0300199 "modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300200 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
201 p->modulator, (unsigned long long)p->std, p->capabilities);
Hans Verkuil59076122012-06-22 06:09:54 -0300202}
203
204static void v4l_print_audio(const void *arg, bool write_only)
205{
206 const struct v4l2_audio *p = arg;
207
208 if (write_only)
209 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
210 else
Hans Verkuil27d5a872013-03-18 11:02:22 -0300211 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
212 p->index, (int)sizeof(p->name), p->name,
213 p->capability, p->mode);
Hans Verkuil59076122012-06-22 06:09:54 -0300214}
215
216static void v4l_print_audioout(const void *arg, bool write_only)
217{
218 const struct v4l2_audioout *p = arg;
219
220 if (write_only)
221 pr_cont("index=%u\n", p->index);
222 else
Hans Verkuil27d5a872013-03-18 11:02:22 -0300223 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
224 p->index, (int)sizeof(p->name), p->name,
225 p->capability, p->mode);
Hans Verkuil59076122012-06-22 06:09:54 -0300226}
227
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300228static void v4l_print_fmtdesc(const void *arg, bool write_only)
229{
230 const struct v4l2_fmtdesc *p = arg;
231
Hans Verkuil27d5a872013-03-18 11:02:22 -0300232 pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%.*s'\n",
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300233 p->index, prt_names(p->type, v4l2_type_names),
234 p->flags, (p->pixelformat & 0xff),
235 (p->pixelformat >> 8) & 0xff,
236 (p->pixelformat >> 16) & 0xff,
237 (p->pixelformat >> 24) & 0xff,
Hans Verkuil27d5a872013-03-18 11:02:22 -0300238 (int)sizeof(p->description), p->description);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300239}
240
241static void v4l_print_format(const void *arg, bool write_only)
242{
243 const struct v4l2_format *p = arg;
244 const struct v4l2_pix_format *pix;
245 const struct v4l2_pix_format_mplane *mp;
246 const struct v4l2_vbi_format *vbi;
247 const struct v4l2_sliced_vbi_format *sliced;
248 const struct v4l2_window *win;
Antti Palosaari87185c92014-03-10 10:43:24 -0300249 const struct v4l2_sdr_format *sdr;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300250 unsigned i;
251
252 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
253 switch (p->type) {
254 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
255 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
256 pix = &p->fmt.pix;
257 pr_cont(", width=%u, height=%u, "
258 "pixelformat=%c%c%c%c, field=%s, "
Laurent Pinchartc96fd462014-05-27 10:12:43 -0300259 "bytesperline=%u, sizeimage=%u, colorspace=%d, "
260 "flags %u\n",
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300261 pix->width, pix->height,
262 (pix->pixelformat & 0xff),
263 (pix->pixelformat >> 8) & 0xff,
264 (pix->pixelformat >> 16) & 0xff,
265 (pix->pixelformat >> 24) & 0xff,
266 prt_names(pix->field, v4l2_field_names),
267 pix->bytesperline, pix->sizeimage,
Laurent Pinchartc96fd462014-05-27 10:12:43 -0300268 pix->colorspace, pix->flags);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300269 break;
270 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
271 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
272 mp = &p->fmt.pix_mp;
273 pr_cont(", width=%u, height=%u, "
274 "format=%c%c%c%c, field=%s, "
275 "colorspace=%d, num_planes=%u\n",
276 mp->width, mp->height,
277 (mp->pixelformat & 0xff),
278 (mp->pixelformat >> 8) & 0xff,
279 (mp->pixelformat >> 16) & 0xff,
280 (mp->pixelformat >> 24) & 0xff,
281 prt_names(mp->field, v4l2_field_names),
282 mp->colorspace, mp->num_planes);
283 for (i = 0; i < mp->num_planes; i++)
284 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
285 mp->plane_fmt[i].bytesperline,
286 mp->plane_fmt[i].sizeimage);
287 break;
288 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
289 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
290 win = &p->fmt.win;
Hans Verkuil560dde22013-06-03 04:35:22 -0300291 /* Note: we can't print the clip list here since the clips
292 * pointer is a userspace pointer, not a kernelspace
293 * pointer. */
294 pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, chromakey=0x%08x, clipcount=%u, clips=%p, bitmap=%p, global_alpha=0x%02x\n",
295 win->w.width, win->w.height, win->w.left, win->w.top,
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300296 prt_names(win->field, v4l2_field_names),
Hans Verkuil560dde22013-06-03 04:35:22 -0300297 win->chromakey, win->clipcount, win->clips,
298 win->bitmap, win->global_alpha);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300299 break;
300 case V4L2_BUF_TYPE_VBI_CAPTURE:
301 case V4L2_BUF_TYPE_VBI_OUTPUT:
302 vbi = &p->fmt.vbi;
303 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
304 "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
305 vbi->sampling_rate, vbi->offset,
306 vbi->samples_per_line,
307 (vbi->sample_format & 0xff),
308 (vbi->sample_format >> 8) & 0xff,
309 (vbi->sample_format >> 16) & 0xff,
310 (vbi->sample_format >> 24) & 0xff,
311 vbi->start[0], vbi->start[1],
312 vbi->count[0], vbi->count[1]);
313 break;
314 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
315 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
316 sliced = &p->fmt.sliced;
317 pr_cont(", service_set=0x%08x, io_size=%d\n",
318 sliced->service_set, sliced->io_size);
319 for (i = 0; i < 24; i++)
320 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
321 sliced->service_lines[0][i],
322 sliced->service_lines[1][i]);
323 break;
Antti Palosaari582c52c2013-12-12 13:44:14 -0300324 case V4L2_BUF_TYPE_SDR_CAPTURE:
325 sdr = &p->fmt.sdr;
326 pr_cont(", pixelformat=%c%c%c%c\n",
327 (sdr->pixelformat >> 0) & 0xff,
328 (sdr->pixelformat >> 8) & 0xff,
329 (sdr->pixelformat >> 16) & 0xff,
330 (sdr->pixelformat >> 24) & 0xff);
331 break;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300332 }
333}
334
335static void v4l_print_framebuffer(const void *arg, bool write_only)
336{
337 const struct v4l2_framebuffer *p = arg;
338
339 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
340 "height=%u, pixelformat=%c%c%c%c, "
Hans Verkuil560dde22013-06-03 04:35:22 -0300341 "bytesperline=%u, sizeimage=%u, colorspace=%d\n",
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300342 p->capability, p->flags, p->base,
343 p->fmt.width, p->fmt.height,
344 (p->fmt.pixelformat & 0xff),
345 (p->fmt.pixelformat >> 8) & 0xff,
346 (p->fmt.pixelformat >> 16) & 0xff,
347 (p->fmt.pixelformat >> 24) & 0xff,
348 p->fmt.bytesperline, p->fmt.sizeimage,
349 p->fmt.colorspace);
350}
351
Hans Verkuile325b242012-06-09 12:50:15 -0300352static void v4l_print_buftype(const void *arg, bool write_only)
353{
354 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
355}
356
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300357static void v4l_print_modulator(const void *arg, bool write_only)
358{
359 const struct v4l2_modulator *p = arg;
360
361 if (write_only)
Hans Verkuil560dde22013-06-03 04:35:22 -0300362 pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300363 else
Hans Verkuil27d5a872013-03-18 11:02:22 -0300364 pr_cont("index=%u, name=%.*s, capability=0x%x, "
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300365 "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300366 p->index, (int)sizeof(p->name), p->name, p->capability,
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300367 p->rangelow, p->rangehigh, p->txsubchans);
368}
369
370static void v4l_print_tuner(const void *arg, bool write_only)
371{
372 const struct v4l2_tuner *p = arg;
373
374 if (write_only)
375 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
376 else
Hans Verkuil27d5a872013-03-18 11:02:22 -0300377 pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, "
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300378 "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
379 "rxsubchans=0x%x, audmode=%u\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300380 p->index, (int)sizeof(p->name), p->name, p->type,
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300381 p->capability, p->rangelow,
382 p->rangehigh, p->signal, p->afc,
383 p->rxsubchans, p->audmode);
384}
385
386static void v4l_print_frequency(const void *arg, bool write_only)
387{
388 const struct v4l2_frequency *p = arg;
389
390 pr_cont("tuner=%u, type=%u, frequency=%u\n",
391 p->tuner, p->type, p->frequency);
392}
393
394static void v4l_print_standard(const void *arg, bool write_only)
395{
396 const struct v4l2_standard *p = arg;
397
Hans Verkuil27d5a872013-03-18 11:02:22 -0300398 pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, "
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300399 "framelines=%u\n", p->index,
Hans Verkuil27d5a872013-03-18 11:02:22 -0300400 (unsigned long long)p->id, (int)sizeof(p->name), p->name,
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300401 p->frameperiod.numerator,
402 p->frameperiod.denominator,
403 p->framelines);
404}
405
406static void v4l_print_std(const void *arg, bool write_only)
407{
408 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
409}
410
411static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
412{
413 const struct v4l2_hw_freq_seek *p = arg;
414
Hans Verkuil5e7883172012-08-01 16:18:41 -0300415 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, "
416 "rangelow=%u, rangehigh=%u\n",
417 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
418 p->rangelow, p->rangehigh);
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300419}
420
Hans Verkuileb3728e2012-06-22 06:23:59 -0300421static void v4l_print_requestbuffers(const void *arg, bool write_only)
Hans Verkuil59076122012-06-22 06:09:54 -0300422{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300423 const struct v4l2_requestbuffers *p = arg;
424
425 pr_cont("count=%d, type=%s, memory=%s\n",
426 p->count,
427 prt_names(p->type, v4l2_type_names),
428 prt_names(p->memory, v4l2_memory_names));
Hans Verkuil59076122012-06-22 06:09:54 -0300429}
430
Hans Verkuileb3728e2012-06-22 06:23:59 -0300431static void v4l_print_buffer(const void *arg, bool write_only)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300432{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300433 const struct v4l2_buffer *p = arg;
434 const struct v4l2_timecode *tc = &p->timecode;
435 const struct v4l2_plane *plane;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300436 int i;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300437
Hans Verkuileb3728e2012-06-22 06:23:59 -0300438 pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
439 "flags=0x%08x, field=%s, sequence=%d, memory=%s",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300440 p->timestamp.tv_sec / 3600,
441 (int)(p->timestamp.tv_sec / 60) % 60,
442 (int)(p->timestamp.tv_sec % 60),
Alexander Beregalovb0459792008-09-03 16:47:38 -0300443 (long)p->timestamp.tv_usec,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300444 p->index,
445 prt_names(p->type, v4l2_type_names),
Hans Verkuileb3728e2012-06-22 06:23:59 -0300446 p->flags, prt_names(p->field, v4l2_field_names),
447 p->sequence, prt_names(p->memory, v4l2_memory_names));
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300448
449 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300450 pr_cont("\n");
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300451 for (i = 0; i < p->length; ++i) {
452 plane = &p->m.planes[i];
Hans Verkuileb3728e2012-06-22 06:23:59 -0300453 printk(KERN_DEBUG
Hans Verkuil560dde22013-06-03 04:35:22 -0300454 "plane %d: bytesused=%d, data_offset=0x%08x, "
Hans Verkuileb3728e2012-06-22 06:23:59 -0300455 "offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300456 i, plane->bytesused, plane->data_offset,
457 plane->m.userptr, plane->length);
458 }
459 } else {
Hans Verkuil560dde22013-06-03 04:35:22 -0300460 pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300461 p->bytesused, p->m.userptr, p->length);
462 }
463
Hans Verkuileb3728e2012-06-22 06:23:59 -0300464 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
465 "flags=0x%08x, frames=%d, userbits=0x%08x\n",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300466 tc->hours, tc->minutes, tc->seconds,
467 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
468}
469
Tomasz Stanislawskib799d09a2012-06-14 11:32:23 -0300470static void v4l_print_exportbuffer(const void *arg, bool write_only)
471{
472 const struct v4l2_exportbuffer *p = arg;
473
474 pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
475 p->fd, prt_names(p->type, v4l2_type_names),
476 p->index, p->plane, p->flags);
477}
478
Hans Verkuileb3728e2012-06-22 06:23:59 -0300479static void v4l_print_create_buffers(const void *arg, bool write_only)
480{
481 const struct v4l2_create_buffers *p = arg;
482
483 pr_cont("index=%d, count=%d, memory=%s, ",
484 p->index, p->count,
485 prt_names(p->memory, v4l2_memory_names));
486 v4l_print_format(&p->format, write_only);
487}
488
489static void v4l_print_streamparm(const void *arg, bool write_only)
490{
491 const struct v4l2_streamparm *p = arg;
492
493 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
494
495 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
496 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
497 const struct v4l2_captureparm *c = &p->parm.capture;
498
499 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
500 "extendedmode=%d, readbuffers=%d\n",
501 c->capability, c->capturemode,
502 c->timeperframe.numerator, c->timeperframe.denominator,
503 c->extendedmode, c->readbuffers);
504 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
505 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
506 const struct v4l2_outputparm *c = &p->parm.output;
507
508 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
509 "extendedmode=%d, writebuffers=%d\n",
510 c->capability, c->outputmode,
511 c->timeperframe.numerator, c->timeperframe.denominator,
512 c->extendedmode, c->writebuffers);
Hans Verkuil560dde22013-06-03 04:35:22 -0300513 } else {
514 pr_cont("\n");
Hans Verkuileb3728e2012-06-22 06:23:59 -0300515 }
516}
517
Hans Verkuilefbceec2012-06-09 12:54:02 -0300518static void v4l_print_queryctrl(const void *arg, bool write_only)
519{
520 const struct v4l2_queryctrl *p = arg;
521
Hans Verkuil27d5a872013-03-18 11:02:22 -0300522 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, "
Hans Verkuilefbceec2012-06-09 12:54:02 -0300523 "step=%d, default=%d, flags=0x%08x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300524 p->id, p->type, (int)sizeof(p->name), p->name,
Hans Verkuilefbceec2012-06-09 12:54:02 -0300525 p->minimum, p->maximum,
526 p->step, p->default_value, p->flags);
527}
528
Hans Verkuile6bee362014-06-10 04:22:06 -0300529static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
530{
531 const struct v4l2_query_ext_ctrl *p = arg;
532
533 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%lld/%lld, "
534 "step=%lld, default=%lld, flags=0x%08x, elem_size=%u, elems=%u, "
Hans Verkuil01760772014-04-27 03:22:17 -0300535 "nr_of_dims=%u, dims=%u,%u,%u,%u\n",
Hans Verkuile6bee362014-06-10 04:22:06 -0300536 p->id, p->type, (int)sizeof(p->name), p->name,
537 p->minimum, p->maximum,
538 p->step, p->default_value, p->flags,
539 p->elem_size, p->elems, p->nr_of_dims,
Hans Verkuil01760772014-04-27 03:22:17 -0300540 p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
Hans Verkuile6bee362014-06-10 04:22:06 -0300541}
542
Hans Verkuilefbceec2012-06-09 12:54:02 -0300543static void v4l_print_querymenu(const void *arg, bool write_only)
544{
545 const struct v4l2_querymenu *p = arg;
546
547 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
548}
549
550static void v4l_print_control(const void *arg, bool write_only)
551{
552 const struct v4l2_control *p = arg;
553
554 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
555}
556
557static void v4l_print_ext_controls(const void *arg, bool write_only)
558{
559 const struct v4l2_ext_controls *p = arg;
560 int i;
561
562 pr_cont("class=0x%x, count=%d, error_idx=%d",
563 p->ctrl_class, p->count, p->error_idx);
564 for (i = 0; i < p->count; i++) {
565 if (p->controls[i].size)
566 pr_cont(", id/val=0x%x/0x%x",
567 p->controls[i].id, p->controls[i].value);
568 else
569 pr_cont(", id/size=0x%x/%u",
570 p->controls[i].id, p->controls[i].size);
571 }
572 pr_cont("\n");
573}
574
Hans Verkuild84f2d942012-06-09 11:57:46 -0300575static void v4l_print_cropcap(const void *arg, bool write_only)
576{
577 const struct v4l2_cropcap *p = arg;
578
579 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
Hans Verkuila112fba2014-05-09 09:26:04 -0300580 "defrect wxh=%dx%d, x,y=%d,%d, "
Hans Verkuild84f2d942012-06-09 11:57:46 -0300581 "pixelaspect %d/%d\n",
582 prt_names(p->type, v4l2_type_names),
583 p->bounds.width, p->bounds.height,
584 p->bounds.left, p->bounds.top,
585 p->defrect.width, p->defrect.height,
586 p->defrect.left, p->defrect.top,
587 p->pixelaspect.numerator, p->pixelaspect.denominator);
588}
589
590static void v4l_print_crop(const void *arg, bool write_only)
591{
592 const struct v4l2_crop *p = arg;
593
594 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
595 prt_names(p->type, v4l2_type_names),
596 p->c.width, p->c.height,
597 p->c.left, p->c.top);
598}
599
600static void v4l_print_selection(const void *arg, bool write_only)
601{
602 const struct v4l2_selection *p = arg;
603
604 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
605 prt_names(p->type, v4l2_type_names),
606 p->target, p->flags,
607 p->r.width, p->r.height, p->r.left, p->r.top);
608}
609
Hans Verkuild806f2d2012-06-09 10:02:49 -0300610static void v4l_print_jpegcompression(const void *arg, bool write_only)
611{
612 const struct v4l2_jpegcompression *p = arg;
613
614 pr_cont("quality=%d, APPn=%d, APP_len=%d, "
615 "COM_len=%d, jpeg_markers=0x%x\n",
616 p->quality, p->APPn, p->APP_len,
617 p->COM_len, p->jpeg_markers);
618}
619
620static void v4l_print_enc_idx(const void *arg, bool write_only)
621{
622 const struct v4l2_enc_idx *p = arg;
623
624 pr_cont("entries=%d, entries_cap=%d\n",
625 p->entries, p->entries_cap);
626}
627
628static void v4l_print_encoder_cmd(const void *arg, bool write_only)
629{
630 const struct v4l2_encoder_cmd *p = arg;
631
632 pr_cont("cmd=%d, flags=0x%x\n",
633 p->cmd, p->flags);
634}
635
636static void v4l_print_decoder_cmd(const void *arg, bool write_only)
637{
638 const struct v4l2_decoder_cmd *p = arg;
639
640 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
641
642 if (p->cmd == V4L2_DEC_CMD_START)
643 pr_info("speed=%d, format=%u\n",
644 p->start.speed, p->start.format);
645 else if (p->cmd == V4L2_DEC_CMD_STOP)
646 pr_info("pts=%llu\n", p->stop.pts);
647}
648
Hans Verkuil96b03d22013-04-06 06:16:58 -0300649static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
Hans Verkuil79b0c642013-03-18 12:16:34 -0300650{
Hans Verkuil96b03d22013-04-06 06:16:58 -0300651 const struct v4l2_dbg_chip_info *p = arg;
Hans Verkuil79b0c642013-03-18 12:16:34 -0300652
653 pr_cont("type=%u, ", p->match.type);
Hans Verkuil3eef2512013-04-03 04:08:19 -0300654 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
Hans Verkuil79b0c642013-03-18 12:16:34 -0300655 pr_cont("name=%.*s, ",
656 (int)sizeof(p->match.name), p->match.name);
657 else
658 pr_cont("addr=%u, ", p->match.addr);
659 pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
660}
661
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300662static void v4l_print_dbg_register(const void *arg, bool write_only)
663{
664 const struct v4l2_dbg_register *p = arg;
665
666 pr_cont("type=%u, ", p->match.type);
Hans Verkuil3eef2512013-04-03 04:08:19 -0300667 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
Hans Verkuil27d5a872013-03-18 11:02:22 -0300668 pr_cont("name=%.*s, ",
669 (int)sizeof(p->match.name), p->match.name);
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300670 else
671 pr_cont("addr=%u, ", p->match.addr);
672 pr_cont("reg=0x%llx, val=0x%llx\n",
673 p->reg, p->val);
674}
675
Hans Verkuilefc626b2012-06-09 10:09:07 -0300676static void v4l_print_dv_timings(const void *arg, bool write_only)
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300677{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300678 const struct v4l2_dv_timings *p = arg;
679
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300680 switch (p->type) {
681 case V4L2_DV_BT_656_1120:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300682 pr_cont("type=bt-656/1120, interlaced=%u, "
683 "pixelclock=%llu, "
684 "width=%u, height=%u, polarities=0x%x, "
685 "hfrontporch=%u, hsync=%u, "
686 "hbackporch=%u, vfrontporch=%u, "
687 "vsync=%u, vbackporch=%u, "
688 "il_vfrontporch=%u, il_vsync=%u, "
689 "il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300690 p->bt.interlaced, p->bt.pixelclock,
691 p->bt.width, p->bt.height,
692 p->bt.polarities, p->bt.hfrontporch,
693 p->bt.hsync, p->bt.hbackporch,
694 p->bt.vfrontporch, p->bt.vsync,
695 p->bt.vbackporch, p->bt.il_vfrontporch,
696 p->bt.il_vsync, p->bt.il_vbackporch,
697 p->bt.standards, p->bt.flags);
698 break;
699 default:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300700 pr_cont("type=%d\n", p->type);
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300701 break;
702 }
703}
704
Hans Verkuilefc626b2012-06-09 10:09:07 -0300705static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
706{
707 const struct v4l2_enum_dv_timings *p = arg;
708
709 pr_cont("index=%u, ", p->index);
710 v4l_print_dv_timings(&p->timings, write_only);
711}
712
713static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
714{
715 const struct v4l2_dv_timings_cap *p = arg;
716
717 switch (p->type) {
718 case V4L2_DV_BT_656_1120:
719 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
720 "pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
721 p->bt.min_width, p->bt.max_width,
722 p->bt.min_height, p->bt.max_height,
723 p->bt.min_pixelclock, p->bt.max_pixelclock,
724 p->bt.standards, p->bt.capabilities);
725 break;
726 default:
727 pr_cont("type=%u\n", p->type);
728 break;
729 }
730}
731
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300732static void v4l_print_frmsizeenum(const void *arg, bool write_only)
733{
734 const struct v4l2_frmsizeenum *p = arg;
735
736 pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
737 p->index,
738 (p->pixel_format & 0xff),
739 (p->pixel_format >> 8) & 0xff,
740 (p->pixel_format >> 16) & 0xff,
741 (p->pixel_format >> 24) & 0xff,
742 p->type);
743 switch (p->type) {
744 case V4L2_FRMSIZE_TYPE_DISCRETE:
Hans Verkuil560dde22013-06-03 04:35:22 -0300745 pr_cont(", wxh=%ux%u\n",
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300746 p->discrete.width, p->discrete.height);
747 break;
748 case V4L2_FRMSIZE_TYPE_STEPWISE:
Hans Verkuil560dde22013-06-03 04:35:22 -0300749 pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300750 p->stepwise.min_width, p->stepwise.min_height,
751 p->stepwise.step_width, p->stepwise.step_height,
752 p->stepwise.max_width, p->stepwise.max_height);
753 break;
754 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
755 /* fall through */
756 default:
757 pr_cont("\n");
758 break;
759 }
760}
761
762static void v4l_print_frmivalenum(const void *arg, bool write_only)
763{
764 const struct v4l2_frmivalenum *p = arg;
765
766 pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
767 p->index,
768 (p->pixel_format & 0xff),
769 (p->pixel_format >> 8) & 0xff,
770 (p->pixel_format >> 16) & 0xff,
771 (p->pixel_format >> 24) & 0xff,
772 p->width, p->height, p->type);
773 switch (p->type) {
774 case V4L2_FRMIVAL_TYPE_DISCRETE:
Hans Verkuil560dde22013-06-03 04:35:22 -0300775 pr_cont(", fps=%d/%d\n",
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300776 p->discrete.numerator,
777 p->discrete.denominator);
778 break;
779 case V4L2_FRMIVAL_TYPE_STEPWISE:
Hans Verkuil560dde22013-06-03 04:35:22 -0300780 pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300781 p->stepwise.min.numerator,
782 p->stepwise.min.denominator,
783 p->stepwise.max.numerator,
784 p->stepwise.max.denominator,
785 p->stepwise.step.numerator,
786 p->stepwise.step.denominator);
787 break;
788 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
789 /* fall through */
790 default:
791 pr_cont("\n");
792 break;
793 }
794}
795
796static void v4l_print_event(const void *arg, bool write_only)
797{
798 const struct v4l2_event *p = arg;
799 const struct v4l2_event_ctrl *c;
800
801 pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
802 "timestamp=%lu.%9.9lu\n",
803 p->type, p->pending, p->sequence, p->id,
804 p->timestamp.tv_sec, p->timestamp.tv_nsec);
805 switch (p->type) {
806 case V4L2_EVENT_VSYNC:
807 printk(KERN_DEBUG "field=%s\n",
808 prt_names(p->u.vsync.field, v4l2_field_names));
809 break;
810 case V4L2_EVENT_CTRL:
811 c = &p->u.ctrl;
812 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
813 c->changes, c->type);
814 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
815 pr_cont("value64=%lld, ", c->value64);
816 else
817 pr_cont("value=%d, ", c->value);
Hans Verkuil560dde22013-06-03 04:35:22 -0300818 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, "
819 "default_value=%d\n",
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300820 c->flags, c->minimum, c->maximum,
821 c->step, c->default_value);
822 break;
823 case V4L2_EVENT_FRAME_SYNC:
824 pr_cont("frame_sequence=%u\n",
825 p->u.frame_sync.frame_sequence);
826 break;
827 }
828}
829
830static void v4l_print_event_subscription(const void *arg, bool write_only)
831{
832 const struct v4l2_event_subscription *p = arg;
833
834 pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
835 p->type, p->id, p->flags);
836}
837
838static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
839{
840 const struct v4l2_sliced_vbi_cap *p = arg;
841 int i;
842
843 pr_cont("type=%s, service_set=0x%08x\n",
844 prt_names(p->type, v4l2_type_names), p->service_set);
845 for (i = 0; i < 24; i++)
846 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
847 p->service_lines[0][i],
848 p->service_lines[1][i]);
849}
850
Hans Verkuil82b655b2012-07-05 06:37:08 -0300851static void v4l_print_freq_band(const void *arg, bool write_only)
852{
853 const struct v4l2_frequency_band *p = arg;
854
855 pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
Hans Verkuil560dde22013-06-03 04:35:22 -0300856 "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
Hans Verkuil82b655b2012-07-05 06:37:08 -0300857 p->tuner, p->type, p->index,
858 p->capability, p->rangelow,
859 p->rangehigh, p->modulation);
860}
861
Hans Verkuildd519bb2014-03-07 07:18:37 -0300862static void v4l_print_edid(const void *arg, bool write_only)
863{
864 const struct v4l2_edid *p = arg;
865
866 pr_cont("pad=%u, start_block=%u, blocks=%u\n",
867 p->pad, p->start_block, p->blocks);
868}
869
Hans Verkuilefc626b2012-06-09 10:09:07 -0300870static void v4l_print_u32(const void *arg, bool write_only)
871{
872 pr_cont("value=%u\n", *(const u32 *)arg);
873}
874
875static void v4l_print_newline(const void *arg, bool write_only)
876{
877 pr_cont("\n");
878}
879
Hans Verkuilf18d8e02012-06-22 06:35:01 -0300880static void v4l_print_default(const void *arg, bool write_only)
881{
882 pr_cont("driver-specific ioctl\n");
883}
884
Hans Verkuilefbceec2012-06-09 12:54:02 -0300885static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300886{
887 __u32 i;
888
889 /* zero the reserved fields */
890 c->reserved[0] = c->reserved[1] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300891 for (i = 0; i < c->count; i++)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300892 c->controls[i].reserved2[0] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300893
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300894 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
895 when using extended controls.
896 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
897 is it allowed for backwards compatibility.
898 */
899 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
900 return 0;
901 /* Check that all controls are from the same control class. */
902 for (i = 0; i < c->count; i++) {
903 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
904 c->error_idx = i;
905 return 0;
906 }
907 }
908 return 1;
909}
910
Hans Verkuil4b202592012-09-14 07:03:35 -0300911static int check_fmt(struct file *file, enum v4l2_buf_type type)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300912{
Hans Verkuil4b202592012-09-14 07:03:35 -0300913 struct video_device *vfd = video_devdata(file);
914 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
915 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
916 bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
Antti Palosaari582c52c2013-12-12 13:44:14 -0300917 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
Hans Verkuil4b202592012-09-14 07:03:35 -0300918 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
919 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
920
Hans Verkuila3998102008-07-21 02:57:38 -0300921 if (ops == NULL)
922 return -EINVAL;
923
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300924 switch (type) {
925 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300926 if (is_vid && is_rx &&
927 (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300928 return 0;
929 break;
930 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300931 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300932 return 0;
933 break;
934 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -0300935 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300936 return 0;
937 break;
938 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300939 if (is_vid && is_tx &&
940 (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300941 return 0;
942 break;
943 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300944 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300945 return 0;
946 break;
947 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -0300948 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300949 return 0;
950 break;
951 case V4L2_BUF_TYPE_VBI_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300952 if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300953 return 0;
954 break;
955 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300956 if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300957 return 0;
958 break;
959 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300960 if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300961 return 0;
962 break;
963 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300964 if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300965 return 0;
966 break;
Antti Palosaari582c52c2013-12-12 13:44:14 -0300967 case V4L2_BUF_TYPE_SDR_CAPTURE:
968 if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
969 return 0;
970 break;
Hans Verkuil633c98e2012-09-17 05:02:26 -0300971 default:
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300972 break;
973 }
974 return -EINVAL;
975}
976
Laurent Pinchartd52e2382014-05-27 09:41:05 -0300977static void v4l_sanitize_format(struct v4l2_format *fmt)
978{
979 unsigned int offset;
980
981 /*
982 * The v4l2_pix_format structure has been extended with fields that were
983 * not previously required to be set to zero by applications. The priv
984 * field, when set to a magic value, indicates the the extended fields
985 * are valid. Otherwise they will contain undefined values. To simplify
986 * the API towards drivers zero the extended fields and set the priv
987 * field to the magic value when the extended pixel format structure
988 * isn't used by applications.
989 */
990
991 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
992 fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
993 return;
994
995 if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
996 return;
997
998 fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
999
1000 offset = offsetof(struct v4l2_pix_format, priv)
1001 + sizeof(fmt->fmt.pix.priv);
1002 memset(((void *)&fmt->fmt.pix) + offset, 0,
1003 sizeof(fmt->fmt.pix) - offset);
1004}
1005
Hans Verkuil59076122012-06-22 06:09:54 -03001006static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
1007 struct file *file, void *fh, void *arg)
1008{
1009 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
Laurent Pinchartd52e2382014-05-27 09:41:05 -03001010 int ret;
Hans Verkuil59076122012-06-22 06:09:54 -03001011
1012 cap->version = LINUX_VERSION_CODE;
Laurent Pinchartd52e2382014-05-27 09:41:05 -03001013
1014 ret = ops->vidioc_querycap(file, fh, cap);
1015
1016 cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
Hans Verkuil796a2bd2014-07-21 04:14:46 -03001017 cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
Laurent Pinchartd52e2382014-05-27 09:41:05 -03001018
1019 return ret;
Hans Verkuil59076122012-06-22 06:09:54 -03001020}
1021
1022static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
1023 struct file *file, void *fh, void *arg)
1024{
1025 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
1026}
1027
1028static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
1029 struct file *file, void *fh, void *arg)
1030{
1031 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
1032}
1033
Hans Verkuild0547cb2012-06-09 09:27:10 -03001034static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
1035 struct file *file, void *fh, void *arg)
1036{
1037 struct video_device *vfd;
1038 u32 *p = arg;
1039
1040 if (ops->vidioc_g_priority)
1041 return ops->vidioc_g_priority(file, fh, arg);
1042 vfd = video_devdata(file);
1043 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
1044 return 0;
1045}
1046
1047static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
1048 struct file *file, void *fh, void *arg)
1049{
1050 struct video_device *vfd;
1051 struct v4l2_fh *vfh;
1052 u32 *p = arg;
1053
1054 if (ops->vidioc_s_priority)
1055 return ops->vidioc_s_priority(file, fh, *p);
1056 vfd = video_devdata(file);
1057 vfh = file->private_data;
1058 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
1059}
1060
Hans Verkuil59076122012-06-22 06:09:54 -03001061static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1062 struct file *file, void *fh, void *arg)
1063{
Hans Verkuil73f35412013-03-10 13:12:25 -03001064 struct video_device *vfd = video_devdata(file);
Hans Verkuil59076122012-06-22 06:09:54 -03001065 struct v4l2_input *p = arg;
1066
1067 /*
Hans Verkuil02fa6282013-02-15 15:21:06 -03001068 * We set the flags for CAP_DV_TIMINGS &
Hans Verkuil59076122012-06-22 06:09:54 -03001069 * CAP_STD here based on ioctl handler provided by the
1070 * driver. If the driver doesn't support these
1071 * for a specific input, it must override these flags.
1072 */
Hans Verkuil73f35412013-03-10 13:12:25 -03001073 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
Hans Verkuil59076122012-06-22 06:09:54 -03001074 p->capabilities |= V4L2_IN_CAP_STD;
Hans Verkuil59076122012-06-22 06:09:54 -03001075
1076 return ops->vidioc_enum_input(file, fh, p);
1077}
1078
1079static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1080 struct file *file, void *fh, void *arg)
1081{
Hans Verkuil73f35412013-03-10 13:12:25 -03001082 struct video_device *vfd = video_devdata(file);
Hans Verkuil59076122012-06-22 06:09:54 -03001083 struct v4l2_output *p = arg;
1084
1085 /*
Hans Verkuil02fa6282013-02-15 15:21:06 -03001086 * We set the flags for CAP_DV_TIMINGS &
Hans Verkuil59076122012-06-22 06:09:54 -03001087 * CAP_STD here based on ioctl handler provided by the
1088 * driver. If the driver doesn't support these
1089 * for a specific output, it must override these flags.
1090 */
Hans Verkuil73f35412013-03-10 13:12:25 -03001091 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
Hans Verkuil59076122012-06-22 06:09:54 -03001092 p->capabilities |= V4L2_OUT_CAP_STD;
Hans Verkuil59076122012-06-22 06:09:54 -03001093
1094 return ops->vidioc_enum_output(file, fh, p);
1095}
1096
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001097static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1098 struct file *file, void *fh, void *arg)
1099{
1100 struct v4l2_fmtdesc *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001101 struct video_device *vfd = video_devdata(file);
Hans Verkuilce71bbc2014-07-12 07:54:26 -03001102 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1103 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
Hans Verkuil4b202592012-09-14 07:03:35 -03001104 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1105 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001106
1107 switch (p->type) {
1108 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuilce71bbc2014-07-12 07:54:26 -03001109 if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001110 break;
1111 return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1112 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuilce71bbc2014-07-12 07:54:26 -03001113 if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001114 break;
1115 return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1116 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuilce71bbc2014-07-12 07:54:26 -03001117 if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001118 break;
1119 return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1120 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuilce71bbc2014-07-12 07:54:26 -03001121 if (unlikely(!is_tx || !is_vid || !ops->vidioc_enum_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001122 break;
1123 return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1124 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuilce71bbc2014-07-12 07:54:26 -03001125 if (unlikely(!is_tx || !is_vid || !ops->vidioc_enum_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001126 break;
1127 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
Antti Palosaari582c52c2013-12-12 13:44:14 -03001128 case V4L2_BUF_TYPE_SDR_CAPTURE:
Hans Verkuilce71bbc2014-07-12 07:54:26 -03001129 if (unlikely(!is_rx || !is_sdr || !ops->vidioc_enum_fmt_sdr_cap))
Antti Palosaari582c52c2013-12-12 13:44:14 -03001130 break;
1131 return ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001132 }
1133 return -EINVAL;
1134}
1135
1136static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1137 struct file *file, void *fh, void *arg)
1138{
1139 struct v4l2_format *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001140 struct video_device *vfd = video_devdata(file);
1141 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
Antti Palosaari582c52c2013-12-12 13:44:14 -03001142 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
Hans Verkuil4b202592012-09-14 07:03:35 -03001143 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1144 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Laurent Pinchartd52e2382014-05-27 09:41:05 -03001145 int ret;
1146
1147 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001148
Hans Verkuile5ce5582014-07-17 18:45:45 -03001149 /*
1150 * fmt can't be cleared for these overlay types due to the 'clips'
1151 * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
1152 * Those are provided by the user. So handle these two overlay types
1153 * first, and then just do a simple memset for the other types.
1154 */
1155 switch (p->type) {
1156 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1157 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
1158 struct v4l2_clip *clips = p->fmt.win.clips;
1159 u32 clipcount = p->fmt.win.clipcount;
1160 void *bitmap = p->fmt.win.bitmap;
1161
1162 memset(&p->fmt, 0, sizeof(p->fmt));
1163 p->fmt.win.clips = clips;
1164 p->fmt.win.clipcount = clipcount;
1165 p->fmt.win.bitmap = bitmap;
1166 break;
1167 }
1168 default:
1169 memset(&p->fmt, 0, sizeof(p->fmt));
1170 break;
1171 }
1172
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001173 switch (p->type) {
1174 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001175 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001176 break;
Laurent Pinchartd52e2382014-05-27 09:41:05 -03001177 ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1178 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1179 return ret;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001180 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001181 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001182 break;
1183 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1184 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001185 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001186 break;
1187 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001188 case V4L2_BUF_TYPE_VBI_CAPTURE:
1189 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_vbi_cap))
1190 break;
1191 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1192 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1193 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_cap))
1194 break;
1195 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001196 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001197 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001198 break;
Laurent Pinchartd52e2382014-05-27 09:41:05 -03001199 ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
1200 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1201 return ret;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001202 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001203 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001204 break;
1205 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1206 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001207 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001208 break;
1209 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001210 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001211 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001212 break;
1213 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001214 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001215 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001216 break;
1217 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
Antti Palosaari582c52c2013-12-12 13:44:14 -03001218 case V4L2_BUF_TYPE_SDR_CAPTURE:
1219 if (unlikely(!is_rx || !is_sdr || !ops->vidioc_g_fmt_sdr_cap))
1220 break;
1221 return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001222 }
1223 return -EINVAL;
1224}
1225
1226static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1227 struct file *file, void *fh, void *arg)
1228{
1229 struct v4l2_format *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001230 struct video_device *vfd = video_devdata(file);
1231 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
Antti Palosaari582c52c2013-12-12 13:44:14 -03001232 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
Hans Verkuil4b202592012-09-14 07:03:35 -03001233 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1234 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001235
Laurent Pinchartd52e2382014-05-27 09:41:05 -03001236 v4l_sanitize_format(p);
1237
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001238 switch (p->type) {
1239 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001240 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001241 break;
1242 CLEAR_AFTER_FIELD(p, fmt.pix);
1243 return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1244 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001245 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001246 break;
1247 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1248 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1249 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001250 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001251 break;
1252 CLEAR_AFTER_FIELD(p, fmt.win);
1253 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001254 case V4L2_BUF_TYPE_VBI_CAPTURE:
1255 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_vbi_cap))
1256 break;
1257 CLEAR_AFTER_FIELD(p, fmt.vbi);
1258 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1259 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1260 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_cap))
1261 break;
1262 CLEAR_AFTER_FIELD(p, fmt.sliced);
1263 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001264 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001265 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001266 break;
1267 CLEAR_AFTER_FIELD(p, fmt.pix);
1268 return ops->vidioc_s_fmt_vid_out(file, fh, arg);
1269 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001270 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001271 break;
1272 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1273 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1274 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001275 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001276 break;
1277 CLEAR_AFTER_FIELD(p, fmt.win);
1278 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001279 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001280 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001281 break;
1282 CLEAR_AFTER_FIELD(p, fmt.vbi);
1283 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001284 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001285 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001286 break;
1287 CLEAR_AFTER_FIELD(p, fmt.sliced);
1288 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
Antti Palosaari582c52c2013-12-12 13:44:14 -03001289 case V4L2_BUF_TYPE_SDR_CAPTURE:
1290 if (unlikely(!is_rx || !is_sdr || !ops->vidioc_s_fmt_sdr_cap))
1291 break;
1292 CLEAR_AFTER_FIELD(p, fmt.sdr);
1293 return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001294 }
1295 return -EINVAL;
1296}
1297
1298static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1299 struct file *file, void *fh, void *arg)
1300{
1301 struct v4l2_format *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001302 struct video_device *vfd = video_devdata(file);
1303 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
Antti Palosaari582c52c2013-12-12 13:44:14 -03001304 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
Hans Verkuil4b202592012-09-14 07:03:35 -03001305 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1306 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001307
Laurent Pinchartd52e2382014-05-27 09:41:05 -03001308 v4l_sanitize_format(p);
1309
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001310 switch (p->type) {
1311 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001312 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001313 break;
1314 CLEAR_AFTER_FIELD(p, fmt.pix);
1315 return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1316 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001317 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001318 break;
1319 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1320 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1321 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001322 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001323 break;
1324 CLEAR_AFTER_FIELD(p, fmt.win);
1325 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001326 case V4L2_BUF_TYPE_VBI_CAPTURE:
1327 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_vbi_cap))
1328 break;
1329 CLEAR_AFTER_FIELD(p, fmt.vbi);
1330 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1331 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1332 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_cap))
1333 break;
1334 CLEAR_AFTER_FIELD(p, fmt.sliced);
1335 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001336 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001337 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001338 break;
1339 CLEAR_AFTER_FIELD(p, fmt.pix);
1340 return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1341 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001342 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001343 break;
1344 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1345 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1346 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001347 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001348 break;
1349 CLEAR_AFTER_FIELD(p, fmt.win);
1350 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001351 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001352 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001353 break;
1354 CLEAR_AFTER_FIELD(p, fmt.vbi);
1355 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001356 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001357 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001358 break;
1359 CLEAR_AFTER_FIELD(p, fmt.sliced);
1360 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
Antti Palosaari582c52c2013-12-12 13:44:14 -03001361 case V4L2_BUF_TYPE_SDR_CAPTURE:
1362 if (unlikely(!is_rx || !is_sdr || !ops->vidioc_try_fmt_sdr_cap))
1363 break;
1364 CLEAR_AFTER_FIELD(p, fmt.sdr);
1365 return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001366 }
1367 return -EINVAL;
1368}
1369
Hans Verkuile325b242012-06-09 12:50:15 -03001370static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1371 struct file *file, void *fh, void *arg)
1372{
1373 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1374}
1375
1376static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1377 struct file *file, void *fh, void *arg)
1378{
1379 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1380}
1381
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001382static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1383 struct file *file, void *fh, void *arg)
1384{
1385 struct video_device *vfd = video_devdata(file);
1386 struct v4l2_tuner *p = arg;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001387 int err;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001388
1389 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1390 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001391 err = ops->vidioc_g_tuner(file, fh, p);
1392 if (!err)
1393 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1394 return err;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001395}
1396
1397static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1398 struct file *file, void *fh, void *arg)
1399{
1400 struct video_device *vfd = video_devdata(file);
1401 struct v4l2_tuner *p = arg;
1402
1403 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1404 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1405 return ops->vidioc_s_tuner(file, fh, p);
1406}
1407
Hans Verkuil82b655b2012-07-05 06:37:08 -03001408static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1409 struct file *file, void *fh, void *arg)
1410{
1411 struct v4l2_modulator *p = arg;
1412 int err;
1413
1414 err = ops->vidioc_g_modulator(file, fh, p);
1415 if (!err)
1416 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1417 return err;
1418}
1419
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001420static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1421 struct file *file, void *fh, void *arg)
1422{
1423 struct video_device *vfd = video_devdata(file);
1424 struct v4l2_frequency *p = arg;
1425
Antti Palosaari84099a22013-12-11 20:24:02 -03001426 if (vfd->vfl_type == VFL_TYPE_SDR)
1427 p->type = V4L2_TUNER_ADC;
1428 else
1429 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1430 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001431 return ops->vidioc_g_frequency(file, fh, p);
1432}
1433
1434static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1435 struct file *file, void *fh, void *arg)
1436{
1437 struct video_device *vfd = video_devdata(file);
Hans Verkuilb530a442013-03-19 04:09:26 -03001438 const struct v4l2_frequency *p = arg;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001439 enum v4l2_tuner_type type;
1440
Antti Palosaari84099a22013-12-11 20:24:02 -03001441 if (vfd->vfl_type == VFL_TYPE_SDR) {
1442 if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
1443 return -EINVAL;
1444 } else {
1445 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1446 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1447 if (type != p->type)
1448 return -EINVAL;
1449 }
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001450 return ops->vidioc_s_frequency(file, fh, p);
1451}
1452
1453static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1454 struct file *file, void *fh, void *arg)
1455{
1456 struct video_device *vfd = video_devdata(file);
1457 struct v4l2_standard *p = arg;
1458 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1459 unsigned int index = p->index, i, j = 0;
1460 const char *descr = "";
1461
Hans Verkuila5338192012-09-14 06:45:43 -03001462 /* Return -ENODATA if the tvnorms for the current input
1463 or output is 0, meaning that it doesn't support this API. */
1464 if (id == 0)
1465 return -ENODATA;
1466
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001467 /* Return norm array in a canonical way */
1468 for (i = 0; i <= index && id; i++) {
1469 /* last std value in the standards array is 0, so this
1470 while always ends there since (id & 0) == 0. */
1471 while ((id & standards[j].std) != standards[j].std)
1472 j++;
1473 curr_id = standards[j].std;
1474 descr = standards[j].descr;
1475 j++;
1476 if (curr_id == 0)
1477 break;
1478 if (curr_id != V4L2_STD_PAL &&
1479 curr_id != V4L2_STD_SECAM &&
1480 curr_id != V4L2_STD_NTSC)
1481 id &= ~curr_id;
1482 }
1483 if (i <= index)
1484 return -EINVAL;
1485
1486 v4l2_video_std_construct(p, curr_id, descr);
1487 return 0;
1488}
1489
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001490static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1491 struct file *file, void *fh, void *arg)
1492{
1493 struct video_device *vfd = video_devdata(file);
Hans Verkuil314527a2013-03-15 06:10:40 -03001494 v4l2_std_id id = *(v4l2_std_id *)arg, norm;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001495
Hans Verkuil314527a2013-03-15 06:10:40 -03001496 norm = id & vfd->tvnorms;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001497 if (vfd->tvnorms && !norm) /* Check if std is supported */
1498 return -EINVAL;
1499
1500 /* Calls the specific handler */
Hans Verkuilca371572013-06-03 05:36:50 -03001501 return ops->vidioc_s_std(file, fh, norm);
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001502}
1503
1504static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1505 struct file *file, void *fh, void *arg)
1506{
1507 struct video_device *vfd = video_devdata(file);
1508 v4l2_std_id *p = arg;
1509
1510 /*
Hans Verkuil1a2c6862013-05-29 10:19:04 -03001511 * If no signal is detected, then the driver should return
1512 * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
1513 * any standards that do not apply removed.
1514 *
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001515 * This means that tuners, audio and video decoders can join
1516 * their efforts to improve the standards detection.
1517 */
1518 *p = vfd->tvnorms;
1519 return ops->vidioc_querystd(file, fh, arg);
1520}
1521
1522static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1523 struct file *file, void *fh, void *arg)
1524{
1525 struct video_device *vfd = video_devdata(file);
1526 struct v4l2_hw_freq_seek *p = arg;
1527 enum v4l2_tuner_type type;
1528
Antti Palosaari84099a22013-12-11 20:24:02 -03001529 /* s_hw_freq_seek is not supported for SDR for now */
1530 if (vfd->vfl_type == VFL_TYPE_SDR)
1531 return -EINVAL;
1532
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001533 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1534 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1535 if (p->type != type)
1536 return -EINVAL;
1537 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1538}
1539
Hans Verkuil737c0972012-09-04 10:08:01 -03001540static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
1541 struct file *file, void *fh, void *arg)
1542{
1543 return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
1544}
1545
Hans Verkuileb3728e2012-06-22 06:23:59 -03001546static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1547 struct file *file, void *fh, void *arg)
1548{
1549 struct v4l2_requestbuffers *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001550 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001551
1552 if (ret)
1553 return ret;
1554
Hans Verkuil633c98e2012-09-17 05:02:26 -03001555 CLEAR_AFTER_FIELD(p, memory);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001556
1557 return ops->vidioc_reqbufs(file, fh, p);
1558}
1559
1560static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1561 struct file *file, void *fh, void *arg)
1562{
1563 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001564 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001565
1566 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1567}
1568
1569static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1570 struct file *file, void *fh, void *arg)
1571{
1572 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001573 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001574
1575 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1576}
1577
1578static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1579 struct file *file, void *fh, void *arg)
1580{
1581 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001582 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001583
1584 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1585}
1586
1587static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1588 struct file *file, void *fh, void *arg)
1589{
1590 struct v4l2_create_buffers *create = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001591 int ret = check_fmt(file, create->format.type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001592
Laurent Pinchartd52e2382014-05-27 09:41:05 -03001593 if (ret)
1594 return ret;
1595
1596 v4l_sanitize_format(&create->format);
1597
1598 ret = ops->vidioc_create_bufs(file, fh, create);
1599
1600 if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1601 create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1602 create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1603
1604 return ret;
Hans Verkuileb3728e2012-06-22 06:23:59 -03001605}
1606
1607static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1608 struct file *file, void *fh, void *arg)
1609{
1610 struct v4l2_buffer *b = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001611 int ret = check_fmt(file, b->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001612
1613 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1614}
1615
1616static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1617 struct file *file, void *fh, void *arg)
1618{
Hans Verkuileb3728e2012-06-22 06:23:59 -03001619 struct v4l2_streamparm *p = arg;
1620 v4l2_std_id std;
Hans Verkuil4b202592012-09-14 07:03:35 -03001621 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001622
1623 if (ret)
1624 return ret;
1625 if (ops->vidioc_g_parm)
1626 return ops->vidioc_g_parm(file, fh, p);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001627 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1628 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1629 return -EINVAL;
1630 p->parm.capture.readbuffers = 2;
Hans Verkuilca371572013-06-03 05:36:50 -03001631 ret = ops->vidioc_g_std(file, fh, &std);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001632 if (ret == 0)
Hans Verkuilca371572013-06-03 05:36:50 -03001633 v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001634 return ret;
1635}
1636
1637static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1638 struct file *file, void *fh, void *arg)
1639{
1640 struct v4l2_streamparm *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001641 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001642
1643 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1644}
1645
Hans Verkuilefbceec2012-06-09 12:54:02 -03001646static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1647 struct file *file, void *fh, void *arg)
1648{
1649 struct video_device *vfd = video_devdata(file);
1650 struct v4l2_queryctrl *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001651 struct v4l2_fh *vfh =
1652 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001653
1654 if (vfh && vfh->ctrl_handler)
1655 return v4l2_queryctrl(vfh->ctrl_handler, p);
1656 if (vfd->ctrl_handler)
1657 return v4l2_queryctrl(vfd->ctrl_handler, p);
1658 if (ops->vidioc_queryctrl)
1659 return ops->vidioc_queryctrl(file, fh, p);
1660 return -ENOTTY;
1661}
1662
Hans Verkuile6bee362014-06-10 04:22:06 -03001663static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
1664 struct file *file, void *fh, void *arg)
1665{
1666 struct video_device *vfd = video_devdata(file);
1667 struct v4l2_query_ext_ctrl *p = arg;
1668 struct v4l2_fh *vfh =
1669 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1670
1671 if (vfh && vfh->ctrl_handler)
1672 return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
1673 if (vfd->ctrl_handler)
1674 return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
1675 if (ops->vidioc_query_ext_ctrl)
1676 return ops->vidioc_query_ext_ctrl(file, fh, p);
1677 return -ENOTTY;
1678}
1679
Hans Verkuilefbceec2012-06-09 12:54:02 -03001680static int v4l_querymenu(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_querymenu *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 if (vfh && vfh->ctrl_handler)
1689 return v4l2_querymenu(vfh->ctrl_handler, p);
1690 if (vfd->ctrl_handler)
1691 return v4l2_querymenu(vfd->ctrl_handler, p);
1692 if (ops->vidioc_querymenu)
1693 return ops->vidioc_querymenu(file, fh, p);
1694 return -ENOTTY;
1695}
1696
1697static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1698 struct file *file, void *fh, void *arg)
1699{
1700 struct video_device *vfd = video_devdata(file);
1701 struct v4l2_control *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001702 struct v4l2_fh *vfh =
1703 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001704 struct v4l2_ext_controls ctrls;
1705 struct v4l2_ext_control ctrl;
1706
1707 if (vfh && vfh->ctrl_handler)
1708 return v4l2_g_ctrl(vfh->ctrl_handler, p);
1709 if (vfd->ctrl_handler)
1710 return v4l2_g_ctrl(vfd->ctrl_handler, p);
1711 if (ops->vidioc_g_ctrl)
1712 return ops->vidioc_g_ctrl(file, fh, p);
1713 if (ops->vidioc_g_ext_ctrls == NULL)
1714 return -ENOTTY;
1715
1716 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1717 ctrls.count = 1;
1718 ctrls.controls = &ctrl;
1719 ctrl.id = p->id;
1720 ctrl.value = p->value;
1721 if (check_ext_ctrls(&ctrls, 1)) {
1722 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1723
1724 if (ret == 0)
1725 p->value = ctrl.value;
1726 return ret;
1727 }
1728 return -EINVAL;
1729}
1730
1731static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1732 struct file *file, void *fh, void *arg)
1733{
1734 struct video_device *vfd = video_devdata(file);
1735 struct v4l2_control *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001736 struct v4l2_fh *vfh =
1737 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001738 struct v4l2_ext_controls ctrls;
1739 struct v4l2_ext_control ctrl;
1740
1741 if (vfh && vfh->ctrl_handler)
1742 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1743 if (vfd->ctrl_handler)
1744 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1745 if (ops->vidioc_s_ctrl)
1746 return ops->vidioc_s_ctrl(file, fh, p);
1747 if (ops->vidioc_s_ext_ctrls == NULL)
1748 return -ENOTTY;
1749
1750 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1751 ctrls.count = 1;
1752 ctrls.controls = &ctrl;
1753 ctrl.id = p->id;
1754 ctrl.value = p->value;
1755 if (check_ext_ctrls(&ctrls, 1))
1756 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1757 return -EINVAL;
1758}
1759
1760static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1761 struct file *file, void *fh, void *arg)
1762{
1763 struct video_device *vfd = video_devdata(file);
1764 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001765 struct v4l2_fh *vfh =
1766 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001767
1768 p->error_idx = p->count;
1769 if (vfh && vfh->ctrl_handler)
1770 return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1771 if (vfd->ctrl_handler)
1772 return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1773 if (ops->vidioc_g_ext_ctrls == NULL)
1774 return -ENOTTY;
1775 return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1776 -EINVAL;
1777}
1778
1779static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1780 struct file *file, void *fh, void *arg)
1781{
1782 struct video_device *vfd = video_devdata(file);
1783 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001784 struct v4l2_fh *vfh =
1785 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001786
1787 p->error_idx = p->count;
1788 if (vfh && vfh->ctrl_handler)
1789 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1790 if (vfd->ctrl_handler)
1791 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1792 if (ops->vidioc_s_ext_ctrls == NULL)
1793 return -ENOTTY;
1794 return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1795 -EINVAL;
1796}
1797
1798static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1799 struct file *file, void *fh, void *arg)
1800{
1801 struct video_device *vfd = video_devdata(file);
1802 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001803 struct v4l2_fh *vfh =
1804 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001805
1806 p->error_idx = p->count;
1807 if (vfh && vfh->ctrl_handler)
1808 return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1809 if (vfd->ctrl_handler)
1810 return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1811 if (ops->vidioc_try_ext_ctrls == NULL)
1812 return -ENOTTY;
1813 return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1814 -EINVAL;
1815}
1816
Hans Verkuild84f2d942012-06-09 11:57:46 -03001817static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1818 struct file *file, void *fh, void *arg)
1819{
1820 struct v4l2_crop *p = arg;
1821 struct v4l2_selection s = {
1822 .type = p->type,
1823 };
1824 int ret;
1825
1826 if (ops->vidioc_g_crop)
1827 return ops->vidioc_g_crop(file, fh, p);
1828 /* simulate capture crop using selection api */
1829
1830 /* crop means compose for output devices */
1831 if (V4L2_TYPE_IS_OUTPUT(p->type))
1832 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1833 else
1834 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1835
1836 ret = ops->vidioc_g_selection(file, fh, &s);
1837
1838 /* copying results to old structure on success */
1839 if (!ret)
1840 p->c = s.r;
1841 return ret;
1842}
1843
1844static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1845 struct file *file, void *fh, void *arg)
1846{
1847 struct v4l2_crop *p = arg;
1848 struct v4l2_selection s = {
1849 .type = p->type,
1850 .r = p->c,
1851 };
1852
1853 if (ops->vidioc_s_crop)
1854 return ops->vidioc_s_crop(file, fh, p);
1855 /* simulate capture crop using selection api */
1856
1857 /* crop means compose for output devices */
1858 if (V4L2_TYPE_IS_OUTPUT(p->type))
1859 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1860 else
1861 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1862
1863 return ops->vidioc_s_selection(file, fh, &s);
1864}
1865
1866static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1867 struct file *file, void *fh, void *arg)
1868{
1869 struct v4l2_cropcap *p = arg;
Hans Verkuild84f2d942012-06-09 11:57:46 -03001870
Hans Verkuil94099452014-06-13 04:31:06 -03001871 if (ops->vidioc_g_selection) {
1872 struct v4l2_selection s = { .type = p->type };
1873 int ret;
Hans Verkuild84f2d942012-06-09 11:57:46 -03001874
Hans Verkuil94099452014-06-13 04:31:06 -03001875 /* obtaining bounds */
1876 if (V4L2_TYPE_IS_OUTPUT(p->type))
1877 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1878 else
1879 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
Hans Verkuild84f2d942012-06-09 11:57:46 -03001880
Hans Verkuil94099452014-06-13 04:31:06 -03001881 ret = ops->vidioc_g_selection(file, fh, &s);
1882 if (ret)
1883 return ret;
1884 p->bounds = s.r;
Hans Verkuild84f2d942012-06-09 11:57:46 -03001885
Hans Verkuil94099452014-06-13 04:31:06 -03001886 /* obtaining defrect */
1887 if (V4L2_TYPE_IS_OUTPUT(p->type))
1888 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1889 else
1890 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
Hans Verkuild84f2d942012-06-09 11:57:46 -03001891
Hans Verkuil94099452014-06-13 04:31:06 -03001892 ret = ops->vidioc_g_selection(file, fh, &s);
1893 if (ret)
1894 return ret;
1895 p->defrect = s.r;
1896 }
Hans Verkuild84f2d942012-06-09 11:57:46 -03001897
1898 /* setting trivial pixelaspect */
1899 p->pixelaspect.numerator = 1;
1900 p->pixelaspect.denominator = 1;
Hans Verkuil94099452014-06-13 04:31:06 -03001901
1902 if (ops->vidioc_cropcap)
1903 return ops->vidioc_cropcap(file, fh, p);
1904
Hans Verkuild84f2d942012-06-09 11:57:46 -03001905 return 0;
1906}
1907
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001908static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1909 struct file *file, void *fh, void *arg)
1910{
1911 struct video_device *vfd = video_devdata(file);
1912 int ret;
1913
1914 if (vfd->v4l2_dev)
1915 pr_info("%s: ================= START STATUS =================\n",
1916 vfd->v4l2_dev->name);
1917 ret = ops->vidioc_log_status(file, fh);
1918 if (vfd->v4l2_dev)
1919 pr_info("%s: ================== END STATUS ==================\n",
1920 vfd->v4l2_dev->name);
1921 return ret;
1922}
1923
1924static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1925 struct file *file, void *fh, void *arg)
1926{
1927#ifdef CONFIG_VIDEO_ADV_DEBUG
1928 struct v4l2_dbg_register *p = arg;
Hans Verkuil79b0c642013-03-18 12:16:34 -03001929 struct video_device *vfd = video_devdata(file);
1930 struct v4l2_subdev *sd;
1931 int idx = 0;
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001932
1933 if (!capable(CAP_SYS_ADMIN))
1934 return -EPERM;
Hans Verkuil3eef2512013-04-03 04:08:19 -03001935 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
Hans Verkuil79b0c642013-03-18 12:16:34 -03001936 if (vfd->v4l2_dev == NULL)
1937 return -EINVAL;
Hans Verkuil3eef2512013-04-03 04:08:19 -03001938 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1939 if (p->match.addr == idx++)
Hans Verkuil79b0c642013-03-18 12:16:34 -03001940 return v4l2_subdev_call(sd, core, g_register, p);
Hans Verkuil79b0c642013-03-18 12:16:34 -03001941 return -EINVAL;
1942 }
Hans Verkuil191b79b2013-05-29 06:59:34 -03001943 if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1944 (ops->vidioc_g_chip_info || p->match.addr == 0))
Hans Verkuil79b0c642013-03-18 12:16:34 -03001945 return ops->vidioc_g_register(file, fh, p);
1946 return -EINVAL;
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001947#else
1948 return -ENOTTY;
1949#endif
1950}
1951
1952static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1953 struct file *file, void *fh, void *arg)
1954{
1955#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001956 const struct v4l2_dbg_register *p = arg;
Hans Verkuil79b0c642013-03-18 12:16:34 -03001957 struct video_device *vfd = video_devdata(file);
1958 struct v4l2_subdev *sd;
1959 int idx = 0;
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001960
1961 if (!capable(CAP_SYS_ADMIN))
1962 return -EPERM;
Hans Verkuil3eef2512013-04-03 04:08:19 -03001963 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
Hans Verkuil79b0c642013-03-18 12:16:34 -03001964 if (vfd->v4l2_dev == NULL)
1965 return -EINVAL;
Hans Verkuil3eef2512013-04-03 04:08:19 -03001966 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1967 if (p->match.addr == idx++)
Hans Verkuil79b0c642013-03-18 12:16:34 -03001968 return v4l2_subdev_call(sd, core, s_register, p);
Hans Verkuil79b0c642013-03-18 12:16:34 -03001969 return -EINVAL;
1970 }
Hans Verkuil191b79b2013-05-29 06:59:34 -03001971 if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1972 (ops->vidioc_g_chip_info || p->match.addr == 0))
Hans Verkuil79b0c642013-03-18 12:16:34 -03001973 return ops->vidioc_s_register(file, fh, p);
1974 return -EINVAL;
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001975#else
1976 return -ENOTTY;
1977#endif
1978}
1979
Hans Verkuil96b03d22013-04-06 06:16:58 -03001980static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
Hans Verkuil79b0c642013-03-18 12:16:34 -03001981 struct file *file, void *fh, void *arg)
1982{
Hans Verkuilcd634f12013-03-27 08:04:23 -03001983#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil79b0c642013-03-18 12:16:34 -03001984 struct video_device *vfd = video_devdata(file);
Hans Verkuil96b03d22013-04-06 06:16:58 -03001985 struct v4l2_dbg_chip_info *p = arg;
Hans Verkuil79b0c642013-03-18 12:16:34 -03001986 struct v4l2_subdev *sd;
1987 int idx = 0;
1988
1989 switch (p->match.type) {
1990 case V4L2_CHIP_MATCH_BRIDGE:
Hans Verkuil79b0c642013-03-18 12:16:34 -03001991 if (ops->vidioc_s_register)
1992 p->flags |= V4L2_CHIP_FL_WRITABLE;
1993 if (ops->vidioc_g_register)
1994 p->flags |= V4L2_CHIP_FL_READABLE;
Hans Verkuil1c1d86a2013-06-12 11:15:12 -03001995 strlcpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
Hans Verkuil96b03d22013-04-06 06:16:58 -03001996 if (ops->vidioc_g_chip_info)
1997 return ops->vidioc_g_chip_info(file, fh, arg);
Hans Verkuil0f0fe4b2013-04-06 06:06:13 -03001998 if (p->match.addr)
1999 return -EINVAL;
Hans Verkuil79b0c642013-03-18 12:16:34 -03002000 return 0;
2001
Hans Verkuil3eef2512013-04-03 04:08:19 -03002002 case V4L2_CHIP_MATCH_SUBDEV:
Hans Verkuil79b0c642013-03-18 12:16:34 -03002003 if (vfd->v4l2_dev == NULL)
2004 break;
2005 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
Hans Verkuil3eef2512013-04-03 04:08:19 -03002006 if (p->match.addr != idx++)
2007 continue;
2008 if (sd->ops->core && sd->ops->core->s_register)
2009 p->flags |= V4L2_CHIP_FL_WRITABLE;
2010 if (sd->ops->core && sd->ops->core->g_register)
2011 p->flags |= V4L2_CHIP_FL_READABLE;
2012 strlcpy(p->name, sd->name, sizeof(p->name));
2013 return 0;
Hans Verkuil79b0c642013-03-18 12:16:34 -03002014 }
2015 break;
2016 }
2017 return -EINVAL;
Hans Verkuilcd634f12013-03-27 08:04:23 -03002018#else
2019 return -ENOTTY;
2020#endif
Hans Verkuil79b0c642013-03-18 12:16:34 -03002021}
2022
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002023static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
2024 struct file *file, void *fh, void *arg)
2025{
2026 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
2027}
2028
2029static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
2030 struct file *file, void *fh, void *arg)
2031{
2032 return ops->vidioc_subscribe_event(fh, arg);
2033}
2034
2035static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
2036 struct file *file, void *fh, void *arg)
2037{
2038 return ops->vidioc_unsubscribe_event(fh, arg);
2039}
2040
2041static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
2042 struct file *file, void *fh, void *arg)
2043{
2044 struct v4l2_sliced_vbi_cap *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03002045 int ret = check_fmt(file, p->type);
2046
2047 if (ret)
2048 return ret;
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002049
2050 /* Clear up to type, everything after type is zeroed already */
2051 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
2052
2053 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
2054}
2055
Hans Verkuil82b655b2012-07-05 06:37:08 -03002056static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
2057 struct file *file, void *fh, void *arg)
2058{
2059 struct video_device *vfd = video_devdata(file);
2060 struct v4l2_frequency_band *p = arg;
2061 enum v4l2_tuner_type type;
2062 int err;
2063
Antti Palosaari84099a22013-12-11 20:24:02 -03002064 if (vfd->vfl_type == VFL_TYPE_SDR) {
2065 if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
2066 return -EINVAL;
2067 type = p->type;
2068 } else {
2069 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2070 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2071 if (type != p->type)
2072 return -EINVAL;
2073 }
Hans Verkuila7f404a2014-07-11 07:01:39 -03002074 if (ops->vidioc_enum_freq_bands) {
2075 err = ops->vidioc_enum_freq_bands(file, fh, p);
2076 if (err != -ENOTTY)
2077 return err;
2078 }
Hans Verkuil73f35412013-03-10 13:12:25 -03002079 if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
Hans Verkuil82b655b2012-07-05 06:37:08 -03002080 struct v4l2_tuner t = {
2081 .index = p->tuner,
2082 .type = type,
2083 };
2084
Hans Verkuilaa4d9b52012-08-01 15:52:46 -03002085 if (p->index)
2086 return -EINVAL;
Hans Verkuil82b655b2012-07-05 06:37:08 -03002087 err = ops->vidioc_g_tuner(file, fh, &t);
2088 if (err)
2089 return err;
2090 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2091 p->rangelow = t.rangelow;
2092 p->rangehigh = t.rangehigh;
2093 p->modulation = (type == V4L2_TUNER_RADIO) ?
2094 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2095 return 0;
2096 }
Hans Verkuil73f35412013-03-10 13:12:25 -03002097 if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
Hans Verkuil82b655b2012-07-05 06:37:08 -03002098 struct v4l2_modulator m = {
2099 .index = p->tuner,
2100 };
2101
2102 if (type != V4L2_TUNER_RADIO)
2103 return -EINVAL;
Hans Verkuilaa4d9b52012-08-01 15:52:46 -03002104 if (p->index)
2105 return -EINVAL;
Hans Verkuil82b655b2012-07-05 06:37:08 -03002106 err = ops->vidioc_g_modulator(file, fh, &m);
2107 if (err)
2108 return err;
2109 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2110 p->rangelow = m.rangelow;
2111 p->rangehigh = m.rangehigh;
2112 p->modulation = (type == V4L2_TUNER_RADIO) ?
2113 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2114 return 0;
2115 }
2116 return -ENOTTY;
2117}
2118
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002119struct v4l2_ioctl_info {
2120 unsigned int ioctl;
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002121 u32 flags;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002122 const char * const name;
Hans Verkuil86748f32012-06-22 06:04:16 -03002123 union {
2124 u32 offset;
2125 int (*func)(const struct v4l2_ioctl_ops *ops,
2126 struct file *file, void *fh, void *p);
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002127 } u;
Hans Verkuil86748f32012-06-22 06:04:16 -03002128 void (*debug)(const void *arg, bool write_only);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002129};
2130
2131/* This control needs a priority check */
2132#define INFO_FL_PRIO (1 << 0)
2133/* This control can be valid if the filehandle passes a control handler. */
2134#define INFO_FL_CTRL (1 << 1)
Hans Verkuil86748f32012-06-22 06:04:16 -03002135/* This is a standard ioctl, no need for special code */
2136#define INFO_FL_STD (1 << 2)
2137/* This is ioctl has its own function */
2138#define INFO_FL_FUNC (1 << 3)
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002139/* Queuing ioctl */
2140#define INFO_FL_QUEUE (1 << 4)
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002141/* Zero struct from after the field to the end */
2142#define INFO_FL_CLEAR(v4l2_struct, field) \
2143 ((offsetof(struct v4l2_struct, field) + \
2144 sizeof(((struct v4l2_struct *)0)->field)) << 16)
2145#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002146
Hans Verkuil86748f32012-06-22 06:04:16 -03002147#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
2148 [_IOC_NR(_ioctl)] = { \
2149 .ioctl = _ioctl, \
2150 .flags = _flags | INFO_FL_STD, \
2151 .name = #_ioctl, \
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002152 .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
Hans Verkuil86748f32012-06-22 06:04:16 -03002153 .debug = _debug, \
2154 }
2155
2156#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
2157 [_IOC_NR(_ioctl)] = { \
2158 .ioctl = _ioctl, \
2159 .flags = _flags | INFO_FL_FUNC, \
2160 .name = #_ioctl, \
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002161 .u.func = _func, \
Hans Verkuil86748f32012-06-22 06:04:16 -03002162 .debug = _debug, \
2163 }
2164
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002165static struct v4l2_ioctl_info v4l2_ioctls[] = {
Hans Verkuil59076122012-06-22 06:09:54 -03002166 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03002167 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
Hans Verkuile5ce5582014-07-17 18:45:45 -03002168 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03002169 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002170 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2171 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 -03002172 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
2173 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
Hans Verkuil737c0972012-09-04 10:08:01 -03002174 IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002175 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
Tomasz Stanislawskib799d09a2012-06-14 11:32:23 -03002176 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 -03002177 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2178 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2179 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
Hans Verkuileb3728e2012-06-22 06:23:59 -03002180 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2181 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
Hans Verkuilca371572013-06-03 05:36:50 -03002182 IOCTL_INFO_STD(VIDIOC_G_STD, vidioc_g_std, v4l_print_std, 0),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03002183 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2184 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
Hans Verkuil59076122012-06-22 06:09:54 -03002185 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
Hans Verkuilefbceec2012-06-09 12:54:02 -03002186 IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2187 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 -03002188 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2189 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
Hans Verkuil59076122012-06-22 06:09:54 -03002190 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
2191 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
Hans Verkuilefbceec2012-06-09 12:54:02 -03002192 IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2193 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 -03002194 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
2195 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuilf9402a92014-03-14 11:52:38 -03002196 IOCTL_INFO_STD(VIDIOC_G_EDID, vidioc_g_edid, v4l_print_edid, 0),
2197 IOCTL_INFO_STD(VIDIOC_S_EDID, vidioc_s_edid, v4l_print_edid, INFO_FL_PRIO),
Hans Verkuil59076122012-06-22 06:09:54 -03002198 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
2199 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2200 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2201 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
2202 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
Hans Verkuil82b655b2012-07-05 06:37:08 -03002203 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 -03002204 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2205 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2206 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
Hans Verkuild84f2d942012-06-09 11:57:46 -03002207 IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2208 IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2209 IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
Hans Verkuil865c4642014-03-24 09:51:34 -03002210 IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
2211 IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
Hans Verkuild806f2d2012-06-09 10:02:49 -03002212 IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
2213 IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03002214 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03002215 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
Hans Verkuil59076122012-06-22 06:09:54 -03002216 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2217 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
Hans Verkuild0547cb2012-06-09 09:27:10 -03002218 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2219 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002220 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 -03002221 IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
Hans Verkuilefbceec2012-06-09 12:54:02 -03002222 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2223 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 -03002224 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 -03002225 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2226 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 -03002227 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
2228 IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2229 IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2230 IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2231 IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
Hans Verkuilf16f77b2012-06-09 10:06:25 -03002232 IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2233 IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03002234 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 -03002235 IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
2236 IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002237 IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2238 IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2239 IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002240 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2241 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
Hans Verkuilefc626b2012-06-09 10:09:07 -03002242 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
2243 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuila1acb8f2012-07-11 09:15:06 -03002244 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 -03002245 IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
Hans Verkuil96b03d22013-04-06 06:16:58 -03002246 IOCTL_INFO_FNC(VIDIOC_DBG_G_CHIP_INFO, v4l_dbg_g_chip_info, v4l_print_dbg_chip_info, INFO_FL_CLEAR(v4l2_dbg_chip_info, match)),
Hans Verkuile6bee362014-06-10 04:22:06 -03002247 IOCTL_INFO_FNC(VIDIOC_QUERY_EXT_CTRL, v4l_query_ext_ctrl, v4l_print_query_ext_ctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_query_ext_ctrl, id)),
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002248};
2249#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2250
2251bool v4l2_is_known_ioctl(unsigned int cmd)
2252{
2253 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2254 return false;
2255 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2256}
2257
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002258struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
2259{
2260 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2261 return vdev->lock;
2262 if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
2263 return NULL;
2264 if (vdev->queue && vdev->queue->lock &&
2265 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2266 return vdev->queue->lock;
2267 return vdev->lock;
2268}
2269
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002270/* Common ioctl debug function. This function can be used by
2271 external ioctl messages as well as internal V4L ioctl */
Hans Verkuil4a085162012-06-22 06:38:06 -03002272void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002273{
Hans Verkuil86748f32012-06-22 06:04:16 -03002274 const char *dir, *type;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002275
Hans Verkuil4a085162012-06-22 06:38:06 -03002276 if (prefix)
2277 printk(KERN_DEBUG "%s: ", prefix);
2278
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002279 switch (_IOC_TYPE(cmd)) {
2280 case 'd':
2281 type = "v4l2_int";
2282 break;
2283 case 'V':
2284 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2285 type = "v4l2";
2286 break;
2287 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002288 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002289 return;
2290 default:
2291 type = "unknown";
Hans Verkuil86748f32012-06-22 06:04:16 -03002292 break;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002293 }
2294
2295 switch (_IOC_DIR(cmd)) {
2296 case _IOC_NONE: dir = "--"; break;
2297 case _IOC_READ: dir = "r-"; break;
2298 case _IOC_WRITE: dir = "-w"; break;
2299 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2300 default: dir = "*ERR*"; break;
2301 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002302 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002303 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2304}
2305EXPORT_SYMBOL(v4l_printk_ioctl);
2306
Hans Verkuil069b7472008-12-30 07:04:34 -03002307static long __video_do_ioctl(struct file *file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002308 unsigned int cmd, void *arg)
2309{
2310 struct video_device *vfd = video_devdata(file);
Hans Verkuila3998102008-07-21 02:57:38 -03002311 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
Hans Verkuil86748f32012-06-22 06:04:16 -03002312 bool write_only = false;
2313 struct v4l2_ioctl_info default_info;
2314 const struct v4l2_ioctl_info *info;
Hans Verkuild5fbf322008-10-18 13:39:53 -03002315 void *fh = file->private_data;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03002316 struct v4l2_fh *vfh = NULL;
Hans Verkuil80131fe02012-06-22 06:37:38 -03002317 int debug = vfd->debug;
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002318 long ret = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002319
Hans Verkuil6a717882010-04-06 15:56:08 -03002320 if (ops == NULL) {
Hans Verkuil4a085162012-06-22 06:38:06 -03002321 pr_warn("%s: has no ioctl_ops.\n",
2322 video_device_node_name(vfd));
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002323 return ret;
Hans Verkuil6a717882010-04-06 15:56:08 -03002324 }
2325
Ramakrishnan Muthukrishnanb7284bb2014-06-19 14:22:57 -03002326 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002327 vfh = file->private_data;
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002328
2329 if (v4l2_is_known_ioctl(cmd)) {
Hans Verkuil86748f32012-06-22 06:04:16 -03002330 info = &v4l2_ioctls[_IOC_NR(cmd)];
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002331
2332 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2333 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
Hans Verkuil86748f32012-06-22 06:04:16 -03002334 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002335
Ramakrishnan Muthukrishnanb7284bb2014-06-19 14:22:57 -03002336 if (vfh && (info->flags & INFO_FL_PRIO)) {
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002337 ret = v4l2_prio_check(vfd->prio, vfh->prio);
2338 if (ret)
Hans Verkuil86748f32012-06-22 06:04:16 -03002339 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002340 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002341 } else {
2342 default_info.ioctl = cmd;
2343 default_info.flags = 0;
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002344 default_info.debug = v4l_print_default;
Hans Verkuil86748f32012-06-22 06:04:16 -03002345 info = &default_info;
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002346 }
2347
Hans Verkuil86748f32012-06-22 06:04:16 -03002348 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
Hans Verkuil86748f32012-06-22 06:04:16 -03002349 if (info->flags & INFO_FL_STD) {
2350 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2351 const void *p = vfd->ioctl_ops;
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002352 const vidioc_op *vidioc = p + info->u.offset;
Hans Verkuil86748f32012-06-22 06:04:16 -03002353
2354 ret = (*vidioc)(file, fh, arg);
Hans Verkuil86748f32012-06-22 06:04:16 -03002355 } else if (info->flags & INFO_FL_FUNC) {
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002356 ret = info->u.func(ops, file, fh, arg);
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002357 } else if (!ops->vidioc_default) {
2358 ret = -ENOTTY;
2359 } else {
2360 ret = ops->vidioc_default(file, fh,
Ramakrishnan Muthukrishnanb7284bb2014-06-19 14:22:57 -03002361 vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002362 cmd, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002363 }
2364
Hans Verkuil86748f32012-06-22 06:04:16 -03002365done:
Hans Verkuil80131fe02012-06-22 06:37:38 -03002366 if (debug) {
Hans Verkuil4a085162012-06-22 06:38:06 -03002367 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03002368 if (ret < 0)
Hans Verkuil505d04b2013-03-14 07:40:45 -03002369 pr_cont(": error %ld", ret);
2370 if (debug == V4L2_DEBUG_IOCTL)
Hans Verkuil86748f32012-06-22 06:04:16 -03002371 pr_cont("\n");
Hans Verkuil86748f32012-06-22 06:04:16 -03002372 else if (_IOC_DIR(cmd) == _IOC_NONE)
2373 info->debug(arg, write_only);
2374 else {
2375 pr_cont(": ");
2376 info->debug(arg, write_only);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002377 }
2378 }
2379
2380 return ret;
2381}
2382
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002383static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
Hans Verkuilba2d35c12014-03-17 09:54:23 -03002384 void __user **user_ptr, void ***kernel_ptr)
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002385{
2386 int ret = 0;
2387
2388 switch (cmd) {
Hans Verkuil96b1a702012-09-19 10:14:41 -03002389 case VIDIOC_PREPARE_BUF:
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002390 case VIDIOC_QUERYBUF:
2391 case VIDIOC_QBUF:
2392 case VIDIOC_DQBUF: {
2393 struct v4l2_buffer *buf = parg;
2394
2395 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2396 if (buf->length > VIDEO_MAX_PLANES) {
2397 ret = -EINVAL;
2398 break;
2399 }
2400 *user_ptr = (void __user *)buf->m.planes;
Hans Verkuilba2d35c12014-03-17 09:54:23 -03002401 *kernel_ptr = (void **)&buf->m.planes;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002402 *array_size = sizeof(struct v4l2_plane) * buf->length;
2403 ret = 1;
2404 }
2405 break;
2406 }
2407
Hans Verkuildd519bb2014-03-07 07:18:37 -03002408 case VIDIOC_G_EDID:
2409 case VIDIOC_S_EDID: {
2410 struct v4l2_edid *edid = parg;
Hans Verkuiled45ce22012-08-10 06:07:12 -03002411
2412 if (edid->blocks) {
Hans Verkuil1b8b10c2012-10-02 02:47:58 -03002413 if (edid->blocks > 256) {
2414 ret = -EINVAL;
2415 break;
2416 }
Hans Verkuiled45ce22012-08-10 06:07:12 -03002417 *user_ptr = (void __user *)edid->edid;
Hans Verkuilba2d35c12014-03-17 09:54:23 -03002418 *kernel_ptr = (void **)&edid->edid;
Hans Verkuiled45ce22012-08-10 06:07:12 -03002419 *array_size = edid->blocks * 128;
2420 ret = 1;
2421 }
2422 break;
2423 }
2424
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002425 case VIDIOC_S_EXT_CTRLS:
2426 case VIDIOC_G_EXT_CTRLS:
2427 case VIDIOC_TRY_EXT_CTRLS: {
2428 struct v4l2_ext_controls *ctrls = parg;
2429
2430 if (ctrls->count != 0) {
Dan Carpenter6c061082012-01-05 02:27:57 -03002431 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2432 ret = -EINVAL;
2433 break;
2434 }
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002435 *user_ptr = (void __user *)ctrls->controls;
Hans Verkuilba2d35c12014-03-17 09:54:23 -03002436 *kernel_ptr = (void **)&ctrls->controls;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002437 *array_size = sizeof(struct v4l2_ext_control)
2438 * ctrls->count;
2439 ret = 1;
2440 }
2441 break;
2442 }
2443 }
2444
2445 return ret;
2446}
2447
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002448long
2449video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2450 v4l2_kioctl func)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002451{
2452 char sbuf[128];
2453 void *mbuf = NULL;
Hans Verkuil1d94aa32010-04-06 08:12:21 -03002454 void *parg = (void *)arg;
Hans Verkuil069b7472008-12-30 07:04:34 -03002455 long err = -EINVAL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002456 bool has_array_args;
2457 size_t array_size = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002458 void __user *user_ptr = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002459 void **kernel_ptr = NULL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002460
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002461 /* Copy arguments into temp kernel buffer */
Trent Piepho337f9d22009-03-04 01:21:02 -03002462 if (_IOC_DIR(cmd) != _IOC_NONE) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002463 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2464 parg = sbuf;
2465 } else {
2466 /* too big to allocate from stack */
2467 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2468 if (NULL == mbuf)
2469 return -ENOMEM;
2470 parg = mbuf;
2471 }
2472
2473 err = -EFAULT;
Trent Piepho19c96e42009-03-04 01:21:02 -03002474 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002475 unsigned int n = _IOC_SIZE(cmd);
2476
2477 /*
2478 * In some cases, only a few fields are used as input,
2479 * i.e. when the app sets "index" and then the driver
2480 * fills in the rest of the structure for the thing
2481 * with that index. We only need to copy up the first
2482 * non-input field.
2483 */
2484 if (v4l2_is_known_ioctl(cmd)) {
2485 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2486 if (flags & INFO_FL_CLEAR_MASK)
2487 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2488 }
Trent Piepho19c96e42009-03-04 01:21:02 -03002489
2490 if (copy_from_user(parg, (void __user *)arg, n))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002491 goto out;
Trent Piepho19c96e42009-03-04 01:21:02 -03002492
2493 /* zero out anything we don't copy from userspace */
2494 if (n < _IOC_SIZE(cmd))
2495 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
Trent Piepho337f9d22009-03-04 01:21:02 -03002496 } else {
2497 /* read-only ioctl */
2498 memset(parg, 0, _IOC_SIZE(cmd));
Trent Piepho19c96e42009-03-04 01:21:02 -03002499 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002500 }
2501
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002502 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2503 if (err < 0)
2504 goto out;
2505 has_array_args = err;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002506
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002507 if (has_array_args) {
2508 /*
2509 * When adding new types of array args, make sure that the
2510 * parent argument to ioctl (which contains the pointer to the
2511 * array) fits into sbuf (so that mbuf will still remain
2512 * unused up to here).
2513 */
2514 mbuf = kmalloc(array_size, GFP_KERNEL);
2515 err = -ENOMEM;
2516 if (NULL == mbuf)
2517 goto out_array_args;
2518 err = -EFAULT;
2519 if (copy_from_user(mbuf, user_ptr, array_size))
2520 goto out_array_args;
2521 *kernel_ptr = mbuf;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002522 }
2523
2524 /* Handles IOCTL */
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002525 err = func(file, cmd, parg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002526 if (err == -ENOIOCTLCMD)
Hans Verkuil02bbb812012-02-08 08:09:36 -03002527 err = -ENOTTY;
Hans Verkuilaa32f4c2013-12-16 05:45:37 -03002528 if (err == 0) {
2529 if (cmd == VIDIOC_DQBUF)
2530 trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
2531 else if (cmd == VIDIOC_QBUF)
2532 trace_v4l2_qbuf(video_devdata(file)->minor, parg);
2533 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002534
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002535 if (has_array_args) {
Hans Verkuilba2d35c12014-03-17 09:54:23 -03002536 *kernel_ptr = (void __force *)user_ptr;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002537 if (copy_to_user(user_ptr, mbuf, array_size))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002538 err = -EFAULT;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002539 goto out_array_args;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002540 }
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002541 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2542 results that must be returned. */
2543 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002544 goto out;
2545
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002546out_array_args:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002547 /* Copy results into user buffer */
2548 switch (_IOC_DIR(cmd)) {
2549 case _IOC_READ:
2550 case (_IOC_WRITE | _IOC_READ):
2551 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2552 err = -EFAULT;
2553 break;
2554 }
2555
2556out:
2557 kfree(mbuf);
2558 return err;
2559}
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002560EXPORT_SYMBOL(video_usercopy);
2561
2562long video_ioctl2(struct file *file,
2563 unsigned int cmd, unsigned long arg)
2564{
2565 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2566}
Mauro Carvalho Chehab8a522c92008-10-21 11:58:39 -03002567EXPORT_SYMBOL(video_ioctl2);