blob: 96bc117f66b2a9a0f75cf76ac4d272f187bde645 [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, "
Hans Verkuil560dde22013-06-03 04:35:22 -0300259 "bytesperline=%u, sizeimage=%u, colorspace=%d\n",
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300260 pix->width, pix->height,
261 (pix->pixelformat & 0xff),
262 (pix->pixelformat >> 8) & 0xff,
263 (pix->pixelformat >> 16) & 0xff,
264 (pix->pixelformat >> 24) & 0xff,
265 prt_names(pix->field, v4l2_field_names),
266 pix->bytesperline, pix->sizeimage,
267 pix->colorspace);
268 break;
269 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
270 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
271 mp = &p->fmt.pix_mp;
272 pr_cont(", width=%u, height=%u, "
273 "format=%c%c%c%c, field=%s, "
274 "colorspace=%d, num_planes=%u\n",
275 mp->width, mp->height,
276 (mp->pixelformat & 0xff),
277 (mp->pixelformat >> 8) & 0xff,
278 (mp->pixelformat >> 16) & 0xff,
279 (mp->pixelformat >> 24) & 0xff,
280 prt_names(mp->field, v4l2_field_names),
281 mp->colorspace, mp->num_planes);
282 for (i = 0; i < mp->num_planes; i++)
283 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
284 mp->plane_fmt[i].bytesperline,
285 mp->plane_fmt[i].sizeimage);
286 break;
287 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
288 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
289 win = &p->fmt.win;
Hans Verkuil560dde22013-06-03 04:35:22 -0300290 /* Note: we can't print the clip list here since the clips
291 * pointer is a userspace pointer, not a kernelspace
292 * pointer. */
293 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",
294 win->w.width, win->w.height, win->w.left, win->w.top,
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300295 prt_names(win->field, v4l2_field_names),
Hans Verkuil560dde22013-06-03 04:35:22 -0300296 win->chromakey, win->clipcount, win->clips,
297 win->bitmap, win->global_alpha);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300298 break;
299 case V4L2_BUF_TYPE_VBI_CAPTURE:
300 case V4L2_BUF_TYPE_VBI_OUTPUT:
301 vbi = &p->fmt.vbi;
302 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
303 "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
304 vbi->sampling_rate, vbi->offset,
305 vbi->samples_per_line,
306 (vbi->sample_format & 0xff),
307 (vbi->sample_format >> 8) & 0xff,
308 (vbi->sample_format >> 16) & 0xff,
309 (vbi->sample_format >> 24) & 0xff,
310 vbi->start[0], vbi->start[1],
311 vbi->count[0], vbi->count[1]);
312 break;
313 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
314 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
315 sliced = &p->fmt.sliced;
316 pr_cont(", service_set=0x%08x, io_size=%d\n",
317 sliced->service_set, sliced->io_size);
318 for (i = 0; i < 24; i++)
319 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
320 sliced->service_lines[0][i],
321 sliced->service_lines[1][i]);
322 break;
Antti Palosaari582c52c2013-12-12 13:44:14 -0300323 case V4L2_BUF_TYPE_SDR_CAPTURE:
324 sdr = &p->fmt.sdr;
325 pr_cont(", pixelformat=%c%c%c%c\n",
326 (sdr->pixelformat >> 0) & 0xff,
327 (sdr->pixelformat >> 8) & 0xff,
328 (sdr->pixelformat >> 16) & 0xff,
329 (sdr->pixelformat >> 24) & 0xff);
330 break;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300331 }
332}
333
334static void v4l_print_framebuffer(const void *arg, bool write_only)
335{
336 const struct v4l2_framebuffer *p = arg;
337
338 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
339 "height=%u, pixelformat=%c%c%c%c, "
Hans Verkuil560dde22013-06-03 04:35:22 -0300340 "bytesperline=%u, sizeimage=%u, colorspace=%d\n",
Hans Verkuilbe6c64e2012-06-22 06:12:57 -0300341 p->capability, p->flags, p->base,
342 p->fmt.width, p->fmt.height,
343 (p->fmt.pixelformat & 0xff),
344 (p->fmt.pixelformat >> 8) & 0xff,
345 (p->fmt.pixelformat >> 16) & 0xff,
346 (p->fmt.pixelformat >> 24) & 0xff,
347 p->fmt.bytesperline, p->fmt.sizeimage,
348 p->fmt.colorspace);
349}
350
Hans Verkuile325b242012-06-09 12:50:15 -0300351static void v4l_print_buftype(const void *arg, bool write_only)
352{
353 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
354}
355
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300356static void v4l_print_modulator(const void *arg, bool write_only)
357{
358 const struct v4l2_modulator *p = arg;
359
360 if (write_only)
Hans Verkuil560dde22013-06-03 04:35:22 -0300361 pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300362 else
Hans Verkuil27d5a872013-03-18 11:02:22 -0300363 pr_cont("index=%u, name=%.*s, capability=0x%x, "
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300364 "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300365 p->index, (int)sizeof(p->name), p->name, p->capability,
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300366 p->rangelow, p->rangehigh, p->txsubchans);
367}
368
369static void v4l_print_tuner(const void *arg, bool write_only)
370{
371 const struct v4l2_tuner *p = arg;
372
373 if (write_only)
374 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
375 else
Hans Verkuil27d5a872013-03-18 11:02:22 -0300376 pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, "
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300377 "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
378 "rxsubchans=0x%x, audmode=%u\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300379 p->index, (int)sizeof(p->name), p->name, p->type,
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300380 p->capability, p->rangelow,
381 p->rangehigh, p->signal, p->afc,
382 p->rxsubchans, p->audmode);
383}
384
385static void v4l_print_frequency(const void *arg, bool write_only)
386{
387 const struct v4l2_frequency *p = arg;
388
389 pr_cont("tuner=%u, type=%u, frequency=%u\n",
390 p->tuner, p->type, p->frequency);
391}
392
393static void v4l_print_standard(const void *arg, bool write_only)
394{
395 const struct v4l2_standard *p = arg;
396
Hans Verkuil27d5a872013-03-18 11:02:22 -0300397 pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, "
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300398 "framelines=%u\n", p->index,
Hans Verkuil27d5a872013-03-18 11:02:22 -0300399 (unsigned long long)p->id, (int)sizeof(p->name), p->name,
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300400 p->frameperiod.numerator,
401 p->frameperiod.denominator,
402 p->framelines);
403}
404
405static void v4l_print_std(const void *arg, bool write_only)
406{
407 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
408}
409
410static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
411{
412 const struct v4l2_hw_freq_seek *p = arg;
413
Hans Verkuil5e7883172012-08-01 16:18:41 -0300414 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, "
415 "rangelow=%u, rangehigh=%u\n",
416 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
417 p->rangelow, p->rangehigh);
Hans Verkuil4b1e2e42012-06-22 06:17:48 -0300418}
419
Hans Verkuileb3728e2012-06-22 06:23:59 -0300420static void v4l_print_requestbuffers(const void *arg, bool write_only)
Hans Verkuil59076122012-06-22 06:09:54 -0300421{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300422 const struct v4l2_requestbuffers *p = arg;
423
424 pr_cont("count=%d, type=%s, memory=%s\n",
425 p->count,
426 prt_names(p->type, v4l2_type_names),
427 prt_names(p->memory, v4l2_memory_names));
Hans Verkuil59076122012-06-22 06:09:54 -0300428}
429
Hans Verkuileb3728e2012-06-22 06:23:59 -0300430static void v4l_print_buffer(const void *arg, bool write_only)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300431{
Hans Verkuileb3728e2012-06-22 06:23:59 -0300432 const struct v4l2_buffer *p = arg;
433 const struct v4l2_timecode *tc = &p->timecode;
434 const struct v4l2_plane *plane;
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300435 int i;
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300436
Hans Verkuileb3728e2012-06-22 06:23:59 -0300437 pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
438 "flags=0x%08x, field=%s, sequence=%d, memory=%s",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300439 p->timestamp.tv_sec / 3600,
440 (int)(p->timestamp.tv_sec / 60) % 60,
441 (int)(p->timestamp.tv_sec % 60),
Alexander Beregalovb0459792008-09-03 16:47:38 -0300442 (long)p->timestamp.tv_usec,
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300443 p->index,
444 prt_names(p->type, v4l2_type_names),
Hans Verkuileb3728e2012-06-22 06:23:59 -0300445 p->flags, prt_names(p->field, v4l2_field_names),
446 p->sequence, prt_names(p->memory, v4l2_memory_names));
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300447
448 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
Hans Verkuileb3728e2012-06-22 06:23:59 -0300449 pr_cont("\n");
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300450 for (i = 0; i < p->length; ++i) {
451 plane = &p->m.planes[i];
Hans Verkuileb3728e2012-06-22 06:23:59 -0300452 printk(KERN_DEBUG
Hans Verkuil560dde22013-06-03 04:35:22 -0300453 "plane %d: bytesused=%d, data_offset=0x%08x, "
Hans Verkuileb3728e2012-06-22 06:23:59 -0300454 "offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300455 i, plane->bytesused, plane->data_offset,
456 plane->m.userptr, plane->length);
457 }
458 } else {
Hans Verkuil560dde22013-06-03 04:35:22 -0300459 pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300460 p->bytesused, p->m.userptr, p->length);
461 }
462
Hans Verkuileb3728e2012-06-22 06:23:59 -0300463 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
464 "flags=0x%08x, frames=%d, userbits=0x%08x\n",
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300465 tc->hours, tc->minutes, tc->seconds,
466 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
467}
468
Tomasz Stanislawskib799d09a2012-06-14 11:32:23 -0300469static void v4l_print_exportbuffer(const void *arg, bool write_only)
470{
471 const struct v4l2_exportbuffer *p = arg;
472
473 pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
474 p->fd, prt_names(p->type, v4l2_type_names),
475 p->index, p->plane, p->flags);
476}
477
Hans Verkuileb3728e2012-06-22 06:23:59 -0300478static void v4l_print_create_buffers(const void *arg, bool write_only)
479{
480 const struct v4l2_create_buffers *p = arg;
481
482 pr_cont("index=%d, count=%d, memory=%s, ",
483 p->index, p->count,
484 prt_names(p->memory, v4l2_memory_names));
485 v4l_print_format(&p->format, write_only);
486}
487
488static void v4l_print_streamparm(const void *arg, bool write_only)
489{
490 const struct v4l2_streamparm *p = arg;
491
492 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
493
494 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
495 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
496 const struct v4l2_captureparm *c = &p->parm.capture;
497
498 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
499 "extendedmode=%d, readbuffers=%d\n",
500 c->capability, c->capturemode,
501 c->timeperframe.numerator, c->timeperframe.denominator,
502 c->extendedmode, c->readbuffers);
503 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
504 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
505 const struct v4l2_outputparm *c = &p->parm.output;
506
507 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
508 "extendedmode=%d, writebuffers=%d\n",
509 c->capability, c->outputmode,
510 c->timeperframe.numerator, c->timeperframe.denominator,
511 c->extendedmode, c->writebuffers);
Hans Verkuil560dde22013-06-03 04:35:22 -0300512 } else {
513 pr_cont("\n");
Hans Verkuileb3728e2012-06-22 06:23:59 -0300514 }
515}
516
Hans Verkuilefbceec2012-06-09 12:54:02 -0300517static void v4l_print_queryctrl(const void *arg, bool write_only)
518{
519 const struct v4l2_queryctrl *p = arg;
520
Hans Verkuil27d5a872013-03-18 11:02:22 -0300521 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, "
Hans Verkuilefbceec2012-06-09 12:54:02 -0300522 "step=%d, default=%d, flags=0x%08x\n",
Hans Verkuil27d5a872013-03-18 11:02:22 -0300523 p->id, p->type, (int)sizeof(p->name), p->name,
Hans Verkuilefbceec2012-06-09 12:54:02 -0300524 p->minimum, p->maximum,
525 p->step, p->default_value, p->flags);
526}
527
Hans Verkuile6bee362014-06-10 04:22:06 -0300528static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
529{
530 const struct v4l2_query_ext_ctrl *p = arg;
531
532 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%lld/%lld, "
533 "step=%lld, default=%lld, flags=0x%08x, elem_size=%u, elems=%u, "
Hans Verkuil01760772014-04-27 03:22:17 -0300534 "nr_of_dims=%u, dims=%u,%u,%u,%u\n",
Hans Verkuile6bee362014-06-10 04:22:06 -0300535 p->id, p->type, (int)sizeof(p->name), p->name,
536 p->minimum, p->maximum,
537 p->step, p->default_value, p->flags,
538 p->elem_size, p->elems, p->nr_of_dims,
Hans Verkuil01760772014-04-27 03:22:17 -0300539 p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
Hans Verkuile6bee362014-06-10 04:22:06 -0300540}
541
Hans Verkuilefbceec2012-06-09 12:54:02 -0300542static void v4l_print_querymenu(const void *arg, bool write_only)
543{
544 const struct v4l2_querymenu *p = arg;
545
546 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
547}
548
549static void v4l_print_control(const void *arg, bool write_only)
550{
551 const struct v4l2_control *p = arg;
552
553 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
554}
555
556static void v4l_print_ext_controls(const void *arg, bool write_only)
557{
558 const struct v4l2_ext_controls *p = arg;
559 int i;
560
561 pr_cont("class=0x%x, count=%d, error_idx=%d",
562 p->ctrl_class, p->count, p->error_idx);
563 for (i = 0; i < p->count; i++) {
564 if (p->controls[i].size)
565 pr_cont(", id/val=0x%x/0x%x",
566 p->controls[i].id, p->controls[i].value);
567 else
568 pr_cont(", id/size=0x%x/%u",
569 p->controls[i].id, p->controls[i].size);
570 }
571 pr_cont("\n");
572}
573
Hans Verkuild84f2d942012-06-09 11:57:46 -0300574static void v4l_print_cropcap(const void *arg, bool write_only)
575{
576 const struct v4l2_cropcap *p = arg;
577
578 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
Hans Verkuila112fba2014-05-09 09:26:04 -0300579 "defrect wxh=%dx%d, x,y=%d,%d, "
Hans Verkuild84f2d942012-06-09 11:57:46 -0300580 "pixelaspect %d/%d\n",
581 prt_names(p->type, v4l2_type_names),
582 p->bounds.width, p->bounds.height,
583 p->bounds.left, p->bounds.top,
584 p->defrect.width, p->defrect.height,
585 p->defrect.left, p->defrect.top,
586 p->pixelaspect.numerator, p->pixelaspect.denominator);
587}
588
589static void v4l_print_crop(const void *arg, bool write_only)
590{
591 const struct v4l2_crop *p = arg;
592
593 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
594 prt_names(p->type, v4l2_type_names),
595 p->c.width, p->c.height,
596 p->c.left, p->c.top);
597}
598
599static void v4l_print_selection(const void *arg, bool write_only)
600{
601 const struct v4l2_selection *p = arg;
602
603 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
604 prt_names(p->type, v4l2_type_names),
605 p->target, p->flags,
606 p->r.width, p->r.height, p->r.left, p->r.top);
607}
608
Hans Verkuild806f2d2012-06-09 10:02:49 -0300609static void v4l_print_jpegcompression(const void *arg, bool write_only)
610{
611 const struct v4l2_jpegcompression *p = arg;
612
613 pr_cont("quality=%d, APPn=%d, APP_len=%d, "
614 "COM_len=%d, jpeg_markers=0x%x\n",
615 p->quality, p->APPn, p->APP_len,
616 p->COM_len, p->jpeg_markers);
617}
618
619static void v4l_print_enc_idx(const void *arg, bool write_only)
620{
621 const struct v4l2_enc_idx *p = arg;
622
623 pr_cont("entries=%d, entries_cap=%d\n",
624 p->entries, p->entries_cap);
625}
626
627static void v4l_print_encoder_cmd(const void *arg, bool write_only)
628{
629 const struct v4l2_encoder_cmd *p = arg;
630
631 pr_cont("cmd=%d, flags=0x%x\n",
632 p->cmd, p->flags);
633}
634
635static void v4l_print_decoder_cmd(const void *arg, bool write_only)
636{
637 const struct v4l2_decoder_cmd *p = arg;
638
639 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
640
641 if (p->cmd == V4L2_DEC_CMD_START)
642 pr_info("speed=%d, format=%u\n",
643 p->start.speed, p->start.format);
644 else if (p->cmd == V4L2_DEC_CMD_STOP)
645 pr_info("pts=%llu\n", p->stop.pts);
646}
647
Hans Verkuil96b03d22013-04-06 06:16:58 -0300648static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
Hans Verkuil79b0c642013-03-18 12:16:34 -0300649{
Hans Verkuil96b03d22013-04-06 06:16:58 -0300650 const struct v4l2_dbg_chip_info *p = arg;
Hans Verkuil79b0c642013-03-18 12:16:34 -0300651
652 pr_cont("type=%u, ", p->match.type);
Hans Verkuil3eef2512013-04-03 04:08:19 -0300653 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
Hans Verkuil79b0c642013-03-18 12:16:34 -0300654 pr_cont("name=%.*s, ",
655 (int)sizeof(p->match.name), p->match.name);
656 else
657 pr_cont("addr=%u, ", p->match.addr);
658 pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
659}
660
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300661static void v4l_print_dbg_register(const void *arg, bool write_only)
662{
663 const struct v4l2_dbg_register *p = arg;
664
665 pr_cont("type=%u, ", p->match.type);
Hans Verkuil3eef2512013-04-03 04:08:19 -0300666 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
Hans Verkuil27d5a872013-03-18 11:02:22 -0300667 pr_cont("name=%.*s, ",
668 (int)sizeof(p->match.name), p->match.name);
Hans Verkuilf16f77b2012-06-09 10:06:25 -0300669 else
670 pr_cont("addr=%u, ", p->match.addr);
671 pr_cont("reg=0x%llx, val=0x%llx\n",
672 p->reg, p->val);
673}
674
Hans Verkuilefc626b2012-06-09 10:09:07 -0300675static void v4l_print_dv_timings(const void *arg, bool write_only)
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300676{
Hans Verkuilefc626b2012-06-09 10:09:07 -0300677 const struct v4l2_dv_timings *p = arg;
678
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300679 switch (p->type) {
680 case V4L2_DV_BT_656_1120:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300681 pr_cont("type=bt-656/1120, interlaced=%u, "
682 "pixelclock=%llu, "
683 "width=%u, height=%u, polarities=0x%x, "
684 "hfrontporch=%u, hsync=%u, "
685 "hbackporch=%u, vfrontporch=%u, "
686 "vsync=%u, vbackporch=%u, "
687 "il_vfrontporch=%u, il_vsync=%u, "
688 "il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300689 p->bt.interlaced, p->bt.pixelclock,
690 p->bt.width, p->bt.height,
691 p->bt.polarities, p->bt.hfrontporch,
692 p->bt.hsync, p->bt.hbackporch,
693 p->bt.vfrontporch, p->bt.vsync,
694 p->bt.vbackporch, p->bt.il_vfrontporch,
695 p->bt.il_vsync, p->bt.il_vbackporch,
696 p->bt.standards, p->bt.flags);
697 break;
698 default:
Hans Verkuilefc626b2012-06-09 10:09:07 -0300699 pr_cont("type=%d\n", p->type);
Hans Verkuil5d7758e2012-05-15 08:06:44 -0300700 break;
701 }
702}
703
Hans Verkuilefc626b2012-06-09 10:09:07 -0300704static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
705{
706 const struct v4l2_enum_dv_timings *p = arg;
707
708 pr_cont("index=%u, ", p->index);
709 v4l_print_dv_timings(&p->timings, write_only);
710}
711
712static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
713{
714 const struct v4l2_dv_timings_cap *p = arg;
715
716 switch (p->type) {
717 case V4L2_DV_BT_656_1120:
718 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
719 "pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
720 p->bt.min_width, p->bt.max_width,
721 p->bt.min_height, p->bt.max_height,
722 p->bt.min_pixelclock, p->bt.max_pixelclock,
723 p->bt.standards, p->bt.capabilities);
724 break;
725 default:
726 pr_cont("type=%u\n", p->type);
727 break;
728 }
729}
730
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300731static void v4l_print_frmsizeenum(const void *arg, bool write_only)
732{
733 const struct v4l2_frmsizeenum *p = arg;
734
735 pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
736 p->index,
737 (p->pixel_format & 0xff),
738 (p->pixel_format >> 8) & 0xff,
739 (p->pixel_format >> 16) & 0xff,
740 (p->pixel_format >> 24) & 0xff,
741 p->type);
742 switch (p->type) {
743 case V4L2_FRMSIZE_TYPE_DISCRETE:
Hans Verkuil560dde22013-06-03 04:35:22 -0300744 pr_cont(", wxh=%ux%u\n",
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300745 p->discrete.width, p->discrete.height);
746 break;
747 case V4L2_FRMSIZE_TYPE_STEPWISE:
Hans Verkuil560dde22013-06-03 04:35:22 -0300748 pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300749 p->stepwise.min_width, p->stepwise.min_height,
750 p->stepwise.step_width, p->stepwise.step_height,
751 p->stepwise.max_width, p->stepwise.max_height);
752 break;
753 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
754 /* fall through */
755 default:
756 pr_cont("\n");
757 break;
758 }
759}
760
761static void v4l_print_frmivalenum(const void *arg, bool write_only)
762{
763 const struct v4l2_frmivalenum *p = arg;
764
765 pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
766 p->index,
767 (p->pixel_format & 0xff),
768 (p->pixel_format >> 8) & 0xff,
769 (p->pixel_format >> 16) & 0xff,
770 (p->pixel_format >> 24) & 0xff,
771 p->width, p->height, p->type);
772 switch (p->type) {
773 case V4L2_FRMIVAL_TYPE_DISCRETE:
Hans Verkuil560dde22013-06-03 04:35:22 -0300774 pr_cont(", fps=%d/%d\n",
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300775 p->discrete.numerator,
776 p->discrete.denominator);
777 break;
778 case V4L2_FRMIVAL_TYPE_STEPWISE:
Hans Verkuil560dde22013-06-03 04:35:22 -0300779 pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300780 p->stepwise.min.numerator,
781 p->stepwise.min.denominator,
782 p->stepwise.max.numerator,
783 p->stepwise.max.denominator,
784 p->stepwise.step.numerator,
785 p->stepwise.step.denominator);
786 break;
787 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
788 /* fall through */
789 default:
790 pr_cont("\n");
791 break;
792 }
793}
794
795static void v4l_print_event(const void *arg, bool write_only)
796{
797 const struct v4l2_event *p = arg;
798 const struct v4l2_event_ctrl *c;
799
800 pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
801 "timestamp=%lu.%9.9lu\n",
802 p->type, p->pending, p->sequence, p->id,
803 p->timestamp.tv_sec, p->timestamp.tv_nsec);
804 switch (p->type) {
805 case V4L2_EVENT_VSYNC:
806 printk(KERN_DEBUG "field=%s\n",
807 prt_names(p->u.vsync.field, v4l2_field_names));
808 break;
809 case V4L2_EVENT_CTRL:
810 c = &p->u.ctrl;
811 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
812 c->changes, c->type);
813 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
814 pr_cont("value64=%lld, ", c->value64);
815 else
816 pr_cont("value=%d, ", c->value);
Hans Verkuil560dde22013-06-03 04:35:22 -0300817 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, "
818 "default_value=%d\n",
Hans Verkuil458aa4a2012-06-09 12:55:52 -0300819 c->flags, c->minimum, c->maximum,
820 c->step, c->default_value);
821 break;
822 case V4L2_EVENT_FRAME_SYNC:
823 pr_cont("frame_sequence=%u\n",
824 p->u.frame_sync.frame_sequence);
825 break;
826 }
827}
828
829static void v4l_print_event_subscription(const void *arg, bool write_only)
830{
831 const struct v4l2_event_subscription *p = arg;
832
833 pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
834 p->type, p->id, p->flags);
835}
836
837static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
838{
839 const struct v4l2_sliced_vbi_cap *p = arg;
840 int i;
841
842 pr_cont("type=%s, service_set=0x%08x\n",
843 prt_names(p->type, v4l2_type_names), p->service_set);
844 for (i = 0; i < 24; i++)
845 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
846 p->service_lines[0][i],
847 p->service_lines[1][i]);
848}
849
Hans Verkuil82b655b2012-07-05 06:37:08 -0300850static void v4l_print_freq_band(const void *arg, bool write_only)
851{
852 const struct v4l2_frequency_band *p = arg;
853
854 pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
Hans Verkuil560dde22013-06-03 04:35:22 -0300855 "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
Hans Verkuil82b655b2012-07-05 06:37:08 -0300856 p->tuner, p->type, p->index,
857 p->capability, p->rangelow,
858 p->rangehigh, p->modulation);
859}
860
Hans Verkuildd519bb2014-03-07 07:18:37 -0300861static void v4l_print_edid(const void *arg, bool write_only)
862{
863 const struct v4l2_edid *p = arg;
864
865 pr_cont("pad=%u, start_block=%u, blocks=%u\n",
866 p->pad, p->start_block, p->blocks);
867}
868
Hans Verkuilefc626b2012-06-09 10:09:07 -0300869static void v4l_print_u32(const void *arg, bool write_only)
870{
871 pr_cont("value=%u\n", *(const u32 *)arg);
872}
873
874static void v4l_print_newline(const void *arg, bool write_only)
875{
876 pr_cont("\n");
877}
878
Hans Verkuilf18d8e02012-06-22 06:35:01 -0300879static void v4l_print_default(const void *arg, bool write_only)
880{
881 pr_cont("driver-specific ioctl\n");
882}
883
Hans Verkuilefbceec2012-06-09 12:54:02 -0300884static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300885{
886 __u32 i;
887
888 /* zero the reserved fields */
889 c->reserved[0] = c->reserved[1] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300890 for (i = 0; i < c->count; i++)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300891 c->controls[i].reserved2[0] = 0;
Hans Verkuil6b5a9492009-08-11 18:47:18 -0300892
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300893 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
894 when using extended controls.
895 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
896 is it allowed for backwards compatibility.
897 */
898 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
899 return 0;
900 /* Check that all controls are from the same control class. */
901 for (i = 0; i < c->count; i++) {
902 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
903 c->error_idx = i;
904 return 0;
905 }
906 }
907 return 1;
908}
909
Hans Verkuil4b202592012-09-14 07:03:35 -0300910static int check_fmt(struct file *file, enum v4l2_buf_type type)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300911{
Hans Verkuil4b202592012-09-14 07:03:35 -0300912 struct video_device *vfd = video_devdata(file);
913 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
914 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
915 bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
Antti Palosaari582c52c2013-12-12 13:44:14 -0300916 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
Hans Verkuil4b202592012-09-14 07:03:35 -0300917 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
918 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
919
Hans Verkuila3998102008-07-21 02:57:38 -0300920 if (ops == NULL)
921 return -EINVAL;
922
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300923 switch (type) {
924 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300925 if (is_vid && is_rx &&
926 (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300927 return 0;
928 break;
929 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300930 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300931 return 0;
932 break;
933 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -0300934 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300935 return 0;
936 break;
937 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300938 if (is_vid && is_tx &&
939 (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
Pawel Osciakd14e6d72010-12-23 04:15:27 -0300940 return 0;
941 break;
942 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300943 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300944 return 0;
945 break;
946 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -0300947 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300948 return 0;
949 break;
950 case V4L2_BUF_TYPE_VBI_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300951 if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300952 return 0;
953 break;
954 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300955 if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300956 return 0;
957 break;
958 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -0300959 if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300960 return 0;
961 break;
962 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -0300963 if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300964 return 0;
965 break;
Antti Palosaari582c52c2013-12-12 13:44:14 -0300966 case V4L2_BUF_TYPE_SDR_CAPTURE:
967 if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
968 return 0;
969 break;
Hans Verkuil633c98e2012-09-17 05:02:26 -0300970 default:
Hans Verkuil35ea11f2008-07-20 08:12:02 -0300971 break;
972 }
973 return -EINVAL;
974}
975
Hans Verkuil59076122012-06-22 06:09:54 -0300976static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
977 struct file *file, void *fh, void *arg)
978{
979 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
980
981 cap->version = LINUX_VERSION_CODE;
982 return ops->vidioc_querycap(file, fh, cap);
983}
984
985static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
986 struct file *file, void *fh, void *arg)
987{
988 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
989}
990
991static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
992 struct file *file, void *fh, void *arg)
993{
994 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
995}
996
Hans Verkuild0547cb2012-06-09 09:27:10 -0300997static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
998 struct file *file, void *fh, void *arg)
999{
1000 struct video_device *vfd;
1001 u32 *p = arg;
1002
1003 if (ops->vidioc_g_priority)
1004 return ops->vidioc_g_priority(file, fh, arg);
1005 vfd = video_devdata(file);
1006 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
1007 return 0;
1008}
1009
1010static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
1011 struct file *file, void *fh, void *arg)
1012{
1013 struct video_device *vfd;
1014 struct v4l2_fh *vfh;
1015 u32 *p = arg;
1016
1017 if (ops->vidioc_s_priority)
1018 return ops->vidioc_s_priority(file, fh, *p);
1019 vfd = video_devdata(file);
1020 vfh = file->private_data;
1021 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
1022}
1023
Hans Verkuil59076122012-06-22 06:09:54 -03001024static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1025 struct file *file, void *fh, void *arg)
1026{
Hans Verkuil73f35412013-03-10 13:12:25 -03001027 struct video_device *vfd = video_devdata(file);
Hans Verkuil59076122012-06-22 06:09:54 -03001028 struct v4l2_input *p = arg;
1029
1030 /*
Hans Verkuil02fa6282013-02-15 15:21:06 -03001031 * We set the flags for CAP_DV_TIMINGS &
Hans Verkuil59076122012-06-22 06:09:54 -03001032 * CAP_STD here based on ioctl handler provided by the
1033 * driver. If the driver doesn't support these
1034 * for a specific input, it must override these flags.
1035 */
Hans Verkuil73f35412013-03-10 13:12:25 -03001036 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
Hans Verkuil59076122012-06-22 06:09:54 -03001037 p->capabilities |= V4L2_IN_CAP_STD;
Hans Verkuil59076122012-06-22 06:09:54 -03001038
1039 return ops->vidioc_enum_input(file, fh, p);
1040}
1041
1042static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1043 struct file *file, void *fh, void *arg)
1044{
Hans Verkuil73f35412013-03-10 13:12:25 -03001045 struct video_device *vfd = video_devdata(file);
Hans Verkuil59076122012-06-22 06:09:54 -03001046 struct v4l2_output *p = arg;
1047
1048 /*
Hans Verkuil02fa6282013-02-15 15:21:06 -03001049 * We set the flags for CAP_DV_TIMINGS &
Hans Verkuil59076122012-06-22 06:09:54 -03001050 * CAP_STD here based on ioctl handler provided by the
1051 * driver. If the driver doesn't support these
1052 * for a specific output, it must override these flags.
1053 */
Hans Verkuil73f35412013-03-10 13:12:25 -03001054 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
Hans Verkuil59076122012-06-22 06:09:54 -03001055 p->capabilities |= V4L2_OUT_CAP_STD;
Hans Verkuil59076122012-06-22 06:09:54 -03001056
1057 return ops->vidioc_enum_output(file, fh, p);
1058}
1059
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001060static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1061 struct file *file, void *fh, void *arg)
1062{
1063 struct v4l2_fmtdesc *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001064 struct video_device *vfd = video_devdata(file);
1065 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1066 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001067
1068 switch (p->type) {
1069 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001070 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001071 break;
1072 return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1073 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001074 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001075 break;
1076 return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1077 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001078 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001079 break;
1080 return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1081 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001082 if (unlikely(!is_tx || !ops->vidioc_enum_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001083 break;
1084 return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1085 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001086 if (unlikely(!is_tx || !ops->vidioc_enum_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001087 break;
1088 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
Antti Palosaari582c52c2013-12-12 13:44:14 -03001089 case V4L2_BUF_TYPE_SDR_CAPTURE:
1090 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_sdr_cap))
1091 break;
1092 return ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001093 }
1094 return -EINVAL;
1095}
1096
1097static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1098 struct file *file, void *fh, void *arg)
1099{
1100 struct v4l2_format *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001101 struct video_device *vfd = video_devdata(file);
1102 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
Antti Palosaari582c52c2013-12-12 13:44:14 -03001103 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 Verkuil4b202592012-09-14 07:03:35 -03001109 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001110 break;
1111 return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1112 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001113 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001114 break;
1115 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1116 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001117 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001118 break;
1119 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001120 case V4L2_BUF_TYPE_VBI_CAPTURE:
1121 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_vbi_cap))
1122 break;
1123 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1124 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1125 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_cap))
1126 break;
1127 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001128 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001129 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001130 break;
1131 return ops->vidioc_g_fmt_vid_out(file, fh, arg);
1132 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001133 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001134 break;
1135 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1136 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001137 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001138 break;
1139 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001140 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001141 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001142 break;
1143 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001144 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001145 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001146 break;
1147 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
Antti Palosaari582c52c2013-12-12 13:44:14 -03001148 case V4L2_BUF_TYPE_SDR_CAPTURE:
1149 if (unlikely(!is_rx || !is_sdr || !ops->vidioc_g_fmt_sdr_cap))
1150 break;
1151 return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001152 }
1153 return -EINVAL;
1154}
1155
1156static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1157 struct file *file, void *fh, void *arg)
1158{
1159 struct v4l2_format *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001160 struct video_device *vfd = video_devdata(file);
1161 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
Antti Palosaari582c52c2013-12-12 13:44:14 -03001162 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
Hans Verkuil4b202592012-09-14 07:03:35 -03001163 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1164 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001165
1166 switch (p->type) {
1167 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001168 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001169 break;
1170 CLEAR_AFTER_FIELD(p, fmt.pix);
1171 return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1172 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001173 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001174 break;
1175 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1176 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1177 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001178 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001179 break;
1180 CLEAR_AFTER_FIELD(p, fmt.win);
1181 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001182 case V4L2_BUF_TYPE_VBI_CAPTURE:
1183 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_vbi_cap))
1184 break;
1185 CLEAR_AFTER_FIELD(p, fmt.vbi);
1186 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1187 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1188 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_cap))
1189 break;
1190 CLEAR_AFTER_FIELD(p, fmt.sliced);
1191 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001192 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001193 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001194 break;
1195 CLEAR_AFTER_FIELD(p, fmt.pix);
1196 return ops->vidioc_s_fmt_vid_out(file, fh, arg);
1197 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001198 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001199 break;
1200 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1201 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1202 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001203 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001204 break;
1205 CLEAR_AFTER_FIELD(p, fmt.win);
1206 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001207 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001208 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001209 break;
1210 CLEAR_AFTER_FIELD(p, fmt.vbi);
1211 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001212 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001213 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001214 break;
1215 CLEAR_AFTER_FIELD(p, fmt.sliced);
1216 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
Antti Palosaari582c52c2013-12-12 13:44:14 -03001217 case V4L2_BUF_TYPE_SDR_CAPTURE:
1218 if (unlikely(!is_rx || !is_sdr || !ops->vidioc_s_fmt_sdr_cap))
1219 break;
1220 CLEAR_AFTER_FIELD(p, fmt.sdr);
1221 return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001222 }
1223 return -EINVAL;
1224}
1225
1226static int v4l_try_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
1236 switch (p->type) {
1237 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001238 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001239 break;
1240 CLEAR_AFTER_FIELD(p, fmt.pix);
1241 return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1242 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001243 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001244 break;
1245 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1246 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1247 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001248 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001249 break;
1250 CLEAR_AFTER_FIELD(p, fmt.win);
1251 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
Hans Verkuil4b202592012-09-14 07:03:35 -03001252 case V4L2_BUF_TYPE_VBI_CAPTURE:
1253 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_vbi_cap))
1254 break;
1255 CLEAR_AFTER_FIELD(p, fmt.vbi);
1256 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1257 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1258 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_cap))
1259 break;
1260 CLEAR_AFTER_FIELD(p, fmt.sliced);
1261 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001262 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001263 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001264 break;
1265 CLEAR_AFTER_FIELD(p, fmt.pix);
1266 return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1267 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
Hans Verkuil4b202592012-09-14 07:03:35 -03001268 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_mplane))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001269 break;
1270 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1271 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1272 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
Hans Verkuil4b202592012-09-14 07:03:35 -03001273 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_overlay))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001274 break;
1275 CLEAR_AFTER_FIELD(p, fmt.win);
1276 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001277 case V4L2_BUF_TYPE_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001278 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001279 break;
1280 CLEAR_AFTER_FIELD(p, fmt.vbi);
1281 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001282 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
Hans Verkuil4b202592012-09-14 07:03:35 -03001283 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_out))
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001284 break;
1285 CLEAR_AFTER_FIELD(p, fmt.sliced);
1286 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
Antti Palosaari582c52c2013-12-12 13:44:14 -03001287 case V4L2_BUF_TYPE_SDR_CAPTURE:
1288 if (unlikely(!is_rx || !is_sdr || !ops->vidioc_try_fmt_sdr_cap))
1289 break;
1290 CLEAR_AFTER_FIELD(p, fmt.sdr);
1291 return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03001292 }
1293 return -EINVAL;
1294}
1295
Hans Verkuile325b242012-06-09 12:50:15 -03001296static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1297 struct file *file, void *fh, void *arg)
1298{
1299 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1300}
1301
1302static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1303 struct file *file, void *fh, void *arg)
1304{
1305 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1306}
1307
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001308static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1309 struct file *file, void *fh, void *arg)
1310{
1311 struct video_device *vfd = video_devdata(file);
1312 struct v4l2_tuner *p = arg;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001313 int err;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001314
1315 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1316 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001317 err = ops->vidioc_g_tuner(file, fh, p);
1318 if (!err)
1319 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1320 return err;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001321}
1322
1323static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1324 struct file *file, void *fh, void *arg)
1325{
1326 struct video_device *vfd = video_devdata(file);
1327 struct v4l2_tuner *p = arg;
1328
1329 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1330 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1331 return ops->vidioc_s_tuner(file, fh, p);
1332}
1333
Hans Verkuil82b655b2012-07-05 06:37:08 -03001334static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1335 struct file *file, void *fh, void *arg)
1336{
1337 struct v4l2_modulator *p = arg;
1338 int err;
1339
1340 err = ops->vidioc_g_modulator(file, fh, p);
1341 if (!err)
1342 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1343 return err;
1344}
1345
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001346static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1347 struct file *file, void *fh, void *arg)
1348{
1349 struct video_device *vfd = video_devdata(file);
1350 struct v4l2_frequency *p = arg;
1351
Antti Palosaari84099a22013-12-11 20:24:02 -03001352 if (vfd->vfl_type == VFL_TYPE_SDR)
1353 p->type = V4L2_TUNER_ADC;
1354 else
1355 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1356 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001357 return ops->vidioc_g_frequency(file, fh, p);
1358}
1359
1360static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1361 struct file *file, void *fh, void *arg)
1362{
1363 struct video_device *vfd = video_devdata(file);
Hans Verkuilb530a442013-03-19 04:09:26 -03001364 const struct v4l2_frequency *p = arg;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001365 enum v4l2_tuner_type type;
1366
Antti Palosaari84099a22013-12-11 20:24:02 -03001367 if (vfd->vfl_type == VFL_TYPE_SDR) {
1368 if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
1369 return -EINVAL;
1370 } else {
1371 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1372 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1373 if (type != p->type)
1374 return -EINVAL;
1375 }
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001376 return ops->vidioc_s_frequency(file, fh, p);
1377}
1378
1379static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1380 struct file *file, void *fh, void *arg)
1381{
1382 struct video_device *vfd = video_devdata(file);
1383 struct v4l2_standard *p = arg;
1384 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1385 unsigned int index = p->index, i, j = 0;
1386 const char *descr = "";
1387
Hans Verkuila5338192012-09-14 06:45:43 -03001388 /* Return -ENODATA if the tvnorms for the current input
1389 or output is 0, meaning that it doesn't support this API. */
1390 if (id == 0)
1391 return -ENODATA;
1392
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001393 /* Return norm array in a canonical way */
1394 for (i = 0; i <= index && id; i++) {
1395 /* last std value in the standards array is 0, so this
1396 while always ends there since (id & 0) == 0. */
1397 while ((id & standards[j].std) != standards[j].std)
1398 j++;
1399 curr_id = standards[j].std;
1400 descr = standards[j].descr;
1401 j++;
1402 if (curr_id == 0)
1403 break;
1404 if (curr_id != V4L2_STD_PAL &&
1405 curr_id != V4L2_STD_SECAM &&
1406 curr_id != V4L2_STD_NTSC)
1407 id &= ~curr_id;
1408 }
1409 if (i <= index)
1410 return -EINVAL;
1411
1412 v4l2_video_std_construct(p, curr_id, descr);
1413 return 0;
1414}
1415
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001416static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1417 struct file *file, void *fh, void *arg)
1418{
1419 struct video_device *vfd = video_devdata(file);
Hans Verkuil314527a2013-03-15 06:10:40 -03001420 v4l2_std_id id = *(v4l2_std_id *)arg, norm;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001421
Hans Verkuil314527a2013-03-15 06:10:40 -03001422 norm = id & vfd->tvnorms;
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001423 if (vfd->tvnorms && !norm) /* Check if std is supported */
1424 return -EINVAL;
1425
1426 /* Calls the specific handler */
Hans Verkuilca371572013-06-03 05:36:50 -03001427 return ops->vidioc_s_std(file, fh, norm);
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001428}
1429
1430static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1431 struct file *file, void *fh, void *arg)
1432{
1433 struct video_device *vfd = video_devdata(file);
1434 v4l2_std_id *p = arg;
1435
1436 /*
Hans Verkuil1a2c6862013-05-29 10:19:04 -03001437 * If no signal is detected, then the driver should return
1438 * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
1439 * any standards that do not apply removed.
1440 *
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001441 * This means that tuners, audio and video decoders can join
1442 * their efforts to improve the standards detection.
1443 */
1444 *p = vfd->tvnorms;
1445 return ops->vidioc_querystd(file, fh, arg);
1446}
1447
1448static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1449 struct file *file, void *fh, void *arg)
1450{
1451 struct video_device *vfd = video_devdata(file);
1452 struct v4l2_hw_freq_seek *p = arg;
1453 enum v4l2_tuner_type type;
1454
Antti Palosaari84099a22013-12-11 20:24:02 -03001455 /* s_hw_freq_seek is not supported for SDR for now */
1456 if (vfd->vfl_type == VFL_TYPE_SDR)
1457 return -EINVAL;
1458
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03001459 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1460 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1461 if (p->type != type)
1462 return -EINVAL;
1463 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1464}
1465
Hans Verkuil737c0972012-09-04 10:08:01 -03001466static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
1467 struct file *file, void *fh, void *arg)
1468{
1469 return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
1470}
1471
Hans Verkuileb3728e2012-06-22 06:23:59 -03001472static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1473 struct file *file, void *fh, void *arg)
1474{
1475 struct v4l2_requestbuffers *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001476 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001477
1478 if (ret)
1479 return ret;
1480
Hans Verkuil633c98e2012-09-17 05:02:26 -03001481 CLEAR_AFTER_FIELD(p, memory);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001482
1483 return ops->vidioc_reqbufs(file, fh, p);
1484}
1485
1486static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1487 struct file *file, void *fh, void *arg)
1488{
1489 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001490 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001491
1492 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1493}
1494
1495static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1496 struct file *file, void *fh, void *arg)
1497{
1498 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001499 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001500
1501 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1502}
1503
1504static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1505 struct file *file, void *fh, void *arg)
1506{
1507 struct v4l2_buffer *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001508 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001509
1510 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1511}
1512
1513static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1514 struct file *file, void *fh, void *arg)
1515{
1516 struct v4l2_create_buffers *create = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001517 int ret = check_fmt(file, create->format.type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001518
1519 return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
1520}
1521
1522static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1523 struct file *file, void *fh, void *arg)
1524{
1525 struct v4l2_buffer *b = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001526 int ret = check_fmt(file, b->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001527
1528 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1529}
1530
1531static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1532 struct file *file, void *fh, void *arg)
1533{
Hans Verkuileb3728e2012-06-22 06:23:59 -03001534 struct v4l2_streamparm *p = arg;
1535 v4l2_std_id std;
Hans Verkuil4b202592012-09-14 07:03:35 -03001536 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001537
1538 if (ret)
1539 return ret;
1540 if (ops->vidioc_g_parm)
1541 return ops->vidioc_g_parm(file, fh, p);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001542 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1543 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1544 return -EINVAL;
1545 p->parm.capture.readbuffers = 2;
Hans Verkuilca371572013-06-03 05:36:50 -03001546 ret = ops->vidioc_g_std(file, fh, &std);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001547 if (ret == 0)
Hans Verkuilca371572013-06-03 05:36:50 -03001548 v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001549 return ret;
1550}
1551
1552static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1553 struct file *file, void *fh, void *arg)
1554{
1555 struct v4l2_streamparm *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001556 int ret = check_fmt(file, p->type);
Hans Verkuileb3728e2012-06-22 06:23:59 -03001557
1558 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1559}
1560
Hans Verkuilefbceec2012-06-09 12:54:02 -03001561static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1562 struct file *file, void *fh, void *arg)
1563{
1564 struct video_device *vfd = video_devdata(file);
1565 struct v4l2_queryctrl *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001566 struct v4l2_fh *vfh =
1567 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001568
1569 if (vfh && vfh->ctrl_handler)
1570 return v4l2_queryctrl(vfh->ctrl_handler, p);
1571 if (vfd->ctrl_handler)
1572 return v4l2_queryctrl(vfd->ctrl_handler, p);
1573 if (ops->vidioc_queryctrl)
1574 return ops->vidioc_queryctrl(file, fh, p);
1575 return -ENOTTY;
1576}
1577
Hans Verkuile6bee362014-06-10 04:22:06 -03001578static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
1579 struct file *file, void *fh, void *arg)
1580{
1581 struct video_device *vfd = video_devdata(file);
1582 struct v4l2_query_ext_ctrl *p = arg;
1583 struct v4l2_fh *vfh =
1584 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1585
1586 if (vfh && vfh->ctrl_handler)
1587 return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
1588 if (vfd->ctrl_handler)
1589 return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
1590 if (ops->vidioc_query_ext_ctrl)
1591 return ops->vidioc_query_ext_ctrl(file, fh, p);
1592 return -ENOTTY;
1593}
1594
Hans Verkuilefbceec2012-06-09 12:54:02 -03001595static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1596 struct file *file, void *fh, void *arg)
1597{
1598 struct video_device *vfd = video_devdata(file);
1599 struct v4l2_querymenu *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001600 struct v4l2_fh *vfh =
1601 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001602
1603 if (vfh && vfh->ctrl_handler)
1604 return v4l2_querymenu(vfh->ctrl_handler, p);
1605 if (vfd->ctrl_handler)
1606 return v4l2_querymenu(vfd->ctrl_handler, p);
1607 if (ops->vidioc_querymenu)
1608 return ops->vidioc_querymenu(file, fh, p);
1609 return -ENOTTY;
1610}
1611
1612static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1613 struct file *file, void *fh, void *arg)
1614{
1615 struct video_device *vfd = video_devdata(file);
1616 struct v4l2_control *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001617 struct v4l2_fh *vfh =
1618 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001619 struct v4l2_ext_controls ctrls;
1620 struct v4l2_ext_control ctrl;
1621
1622 if (vfh && vfh->ctrl_handler)
1623 return v4l2_g_ctrl(vfh->ctrl_handler, p);
1624 if (vfd->ctrl_handler)
1625 return v4l2_g_ctrl(vfd->ctrl_handler, p);
1626 if (ops->vidioc_g_ctrl)
1627 return ops->vidioc_g_ctrl(file, fh, p);
1628 if (ops->vidioc_g_ext_ctrls == NULL)
1629 return -ENOTTY;
1630
1631 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1632 ctrls.count = 1;
1633 ctrls.controls = &ctrl;
1634 ctrl.id = p->id;
1635 ctrl.value = p->value;
1636 if (check_ext_ctrls(&ctrls, 1)) {
1637 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1638
1639 if (ret == 0)
1640 p->value = ctrl.value;
1641 return ret;
1642 }
1643 return -EINVAL;
1644}
1645
1646static int v4l_s_ctrl(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_control *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 struct v4l2_ext_controls ctrls;
1654 struct v4l2_ext_control ctrl;
1655
1656 if (vfh && vfh->ctrl_handler)
1657 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1658 if (vfd->ctrl_handler)
1659 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1660 if (ops->vidioc_s_ctrl)
1661 return ops->vidioc_s_ctrl(file, fh, p);
1662 if (ops->vidioc_s_ext_ctrls == NULL)
1663 return -ENOTTY;
1664
1665 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1666 ctrls.count = 1;
1667 ctrls.controls = &ctrl;
1668 ctrl.id = p->id;
1669 ctrl.value = p->value;
1670 if (check_ext_ctrls(&ctrls, 1))
1671 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1672 return -EINVAL;
1673}
1674
1675static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1676 struct file *file, void *fh, void *arg)
1677{
1678 struct video_device *vfd = video_devdata(file);
1679 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001680 struct v4l2_fh *vfh =
1681 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001682
1683 p->error_idx = p->count;
1684 if (vfh && vfh->ctrl_handler)
1685 return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1686 if (vfd->ctrl_handler)
1687 return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1688 if (ops->vidioc_g_ext_ctrls == NULL)
1689 return -ENOTTY;
1690 return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1691 -EINVAL;
1692}
1693
1694static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1695 struct file *file, void *fh, void *arg)
1696{
1697 struct video_device *vfd = video_devdata(file);
1698 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001699 struct v4l2_fh *vfh =
1700 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001701
1702 p->error_idx = p->count;
1703 if (vfh && vfh->ctrl_handler)
1704 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1705 if (vfd->ctrl_handler)
1706 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1707 if (ops->vidioc_s_ext_ctrls == NULL)
1708 return -ENOTTY;
1709 return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1710 -EINVAL;
1711}
1712
1713static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1714 struct file *file, void *fh, void *arg)
1715{
1716 struct video_device *vfd = video_devdata(file);
1717 struct v4l2_ext_controls *p = arg;
Hans Verkuil7a3ed2d2012-07-07 16:11:11 -03001718 struct v4l2_fh *vfh =
1719 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
Hans Verkuilefbceec2012-06-09 12:54:02 -03001720
1721 p->error_idx = p->count;
1722 if (vfh && vfh->ctrl_handler)
1723 return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1724 if (vfd->ctrl_handler)
1725 return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1726 if (ops->vidioc_try_ext_ctrls == NULL)
1727 return -ENOTTY;
1728 return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1729 -EINVAL;
1730}
1731
Hans Verkuild84f2d942012-06-09 11:57:46 -03001732static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1733 struct file *file, void *fh, void *arg)
1734{
1735 struct v4l2_crop *p = arg;
1736 struct v4l2_selection s = {
1737 .type = p->type,
1738 };
1739 int ret;
1740
1741 if (ops->vidioc_g_crop)
1742 return ops->vidioc_g_crop(file, fh, p);
1743 /* simulate capture crop using selection api */
1744
1745 /* crop means compose for output devices */
1746 if (V4L2_TYPE_IS_OUTPUT(p->type))
1747 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1748 else
1749 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1750
1751 ret = ops->vidioc_g_selection(file, fh, &s);
1752
1753 /* copying results to old structure on success */
1754 if (!ret)
1755 p->c = s.r;
1756 return ret;
1757}
1758
1759static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1760 struct file *file, void *fh, void *arg)
1761{
1762 struct v4l2_crop *p = arg;
1763 struct v4l2_selection s = {
1764 .type = p->type,
1765 .r = p->c,
1766 };
1767
1768 if (ops->vidioc_s_crop)
1769 return ops->vidioc_s_crop(file, fh, p);
1770 /* simulate capture crop using selection api */
1771
1772 /* crop means compose for output devices */
1773 if (V4L2_TYPE_IS_OUTPUT(p->type))
1774 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1775 else
1776 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1777
1778 return ops->vidioc_s_selection(file, fh, &s);
1779}
1780
1781static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1782 struct file *file, void *fh, void *arg)
1783{
1784 struct v4l2_cropcap *p = arg;
1785 struct v4l2_selection s = { .type = p->type };
1786 int ret;
1787
1788 if (ops->vidioc_cropcap)
1789 return ops->vidioc_cropcap(file, fh, p);
1790
1791 /* obtaining bounds */
1792 if (V4L2_TYPE_IS_OUTPUT(p->type))
1793 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1794 else
1795 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1796
1797 ret = ops->vidioc_g_selection(file, fh, &s);
1798 if (ret)
1799 return ret;
1800 p->bounds = s.r;
1801
1802 /* obtaining defrect */
1803 if (V4L2_TYPE_IS_OUTPUT(p->type))
1804 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1805 else
1806 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1807
1808 ret = ops->vidioc_g_selection(file, fh, &s);
1809 if (ret)
1810 return ret;
1811 p->defrect = s.r;
1812
1813 /* setting trivial pixelaspect */
1814 p->pixelaspect.numerator = 1;
1815 p->pixelaspect.denominator = 1;
1816 return 0;
1817}
1818
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001819static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1820 struct file *file, void *fh, void *arg)
1821{
1822 struct video_device *vfd = video_devdata(file);
1823 int ret;
1824
1825 if (vfd->v4l2_dev)
1826 pr_info("%s: ================= START STATUS =================\n",
1827 vfd->v4l2_dev->name);
1828 ret = ops->vidioc_log_status(file, fh);
1829 if (vfd->v4l2_dev)
1830 pr_info("%s: ================== END STATUS ==================\n",
1831 vfd->v4l2_dev->name);
1832 return ret;
1833}
1834
1835static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1836 struct file *file, void *fh, void *arg)
1837{
1838#ifdef CONFIG_VIDEO_ADV_DEBUG
1839 struct v4l2_dbg_register *p = arg;
Hans Verkuil79b0c642013-03-18 12:16:34 -03001840 struct video_device *vfd = video_devdata(file);
1841 struct v4l2_subdev *sd;
1842 int idx = 0;
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001843
1844 if (!capable(CAP_SYS_ADMIN))
1845 return -EPERM;
Hans Verkuil3eef2512013-04-03 04:08:19 -03001846 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
Hans Verkuil79b0c642013-03-18 12:16:34 -03001847 if (vfd->v4l2_dev == NULL)
1848 return -EINVAL;
Hans Verkuil3eef2512013-04-03 04:08:19 -03001849 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1850 if (p->match.addr == idx++)
Hans Verkuil79b0c642013-03-18 12:16:34 -03001851 return v4l2_subdev_call(sd, core, g_register, p);
Hans Verkuil79b0c642013-03-18 12:16:34 -03001852 return -EINVAL;
1853 }
Hans Verkuil191b79b2013-05-29 06:59:34 -03001854 if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1855 (ops->vidioc_g_chip_info || p->match.addr == 0))
Hans Verkuil79b0c642013-03-18 12:16:34 -03001856 return ops->vidioc_g_register(file, fh, p);
1857 return -EINVAL;
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001858#else
1859 return -ENOTTY;
1860#endif
1861}
1862
1863static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1864 struct file *file, void *fh, void *arg)
1865{
1866#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001867 const struct v4l2_dbg_register *p = arg;
Hans Verkuil79b0c642013-03-18 12:16:34 -03001868 struct video_device *vfd = video_devdata(file);
1869 struct v4l2_subdev *sd;
1870 int idx = 0;
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001871
1872 if (!capable(CAP_SYS_ADMIN))
1873 return -EPERM;
Hans Verkuil3eef2512013-04-03 04:08:19 -03001874 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
Hans Verkuil79b0c642013-03-18 12:16:34 -03001875 if (vfd->v4l2_dev == NULL)
1876 return -EINVAL;
Hans Verkuil3eef2512013-04-03 04:08:19 -03001877 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1878 if (p->match.addr == idx++)
Hans Verkuil79b0c642013-03-18 12:16:34 -03001879 return v4l2_subdev_call(sd, core, s_register, p);
Hans Verkuil79b0c642013-03-18 12:16:34 -03001880 return -EINVAL;
1881 }
Hans Verkuil191b79b2013-05-29 06:59:34 -03001882 if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1883 (ops->vidioc_g_chip_info || p->match.addr == 0))
Hans Verkuil79b0c642013-03-18 12:16:34 -03001884 return ops->vidioc_s_register(file, fh, p);
1885 return -EINVAL;
Hans Verkuilf16f77b2012-06-09 10:06:25 -03001886#else
1887 return -ENOTTY;
1888#endif
1889}
1890
Hans Verkuil96b03d22013-04-06 06:16:58 -03001891static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
Hans Verkuil79b0c642013-03-18 12:16:34 -03001892 struct file *file, void *fh, void *arg)
1893{
Hans Verkuilcd634f12013-03-27 08:04:23 -03001894#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil79b0c642013-03-18 12:16:34 -03001895 struct video_device *vfd = video_devdata(file);
Hans Verkuil96b03d22013-04-06 06:16:58 -03001896 struct v4l2_dbg_chip_info *p = arg;
Hans Verkuil79b0c642013-03-18 12:16:34 -03001897 struct v4l2_subdev *sd;
1898 int idx = 0;
1899
1900 switch (p->match.type) {
1901 case V4L2_CHIP_MATCH_BRIDGE:
Hans Verkuil79b0c642013-03-18 12:16:34 -03001902 if (ops->vidioc_s_register)
1903 p->flags |= V4L2_CHIP_FL_WRITABLE;
1904 if (ops->vidioc_g_register)
1905 p->flags |= V4L2_CHIP_FL_READABLE;
Hans Verkuil1c1d86a2013-06-12 11:15:12 -03001906 strlcpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
Hans Verkuil96b03d22013-04-06 06:16:58 -03001907 if (ops->vidioc_g_chip_info)
1908 return ops->vidioc_g_chip_info(file, fh, arg);
Hans Verkuil0f0fe4b2013-04-06 06:06:13 -03001909 if (p->match.addr)
1910 return -EINVAL;
Hans Verkuil79b0c642013-03-18 12:16:34 -03001911 return 0;
1912
Hans Verkuil3eef2512013-04-03 04:08:19 -03001913 case V4L2_CHIP_MATCH_SUBDEV:
Hans Verkuil79b0c642013-03-18 12:16:34 -03001914 if (vfd->v4l2_dev == NULL)
1915 break;
1916 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
Hans Verkuil3eef2512013-04-03 04:08:19 -03001917 if (p->match.addr != idx++)
1918 continue;
1919 if (sd->ops->core && sd->ops->core->s_register)
1920 p->flags |= V4L2_CHIP_FL_WRITABLE;
1921 if (sd->ops->core && sd->ops->core->g_register)
1922 p->flags |= V4L2_CHIP_FL_READABLE;
1923 strlcpy(p->name, sd->name, sizeof(p->name));
1924 return 0;
Hans Verkuil79b0c642013-03-18 12:16:34 -03001925 }
1926 break;
1927 }
1928 return -EINVAL;
Hans Verkuilcd634f12013-03-27 08:04:23 -03001929#else
1930 return -ENOTTY;
1931#endif
Hans Verkuil79b0c642013-03-18 12:16:34 -03001932}
1933
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001934static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
1935 struct file *file, void *fh, void *arg)
1936{
1937 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
1938}
1939
1940static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
1941 struct file *file, void *fh, void *arg)
1942{
1943 return ops->vidioc_subscribe_event(fh, arg);
1944}
1945
1946static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
1947 struct file *file, void *fh, void *arg)
1948{
1949 return ops->vidioc_unsubscribe_event(fh, arg);
1950}
1951
1952static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
1953 struct file *file, void *fh, void *arg)
1954{
1955 struct v4l2_sliced_vbi_cap *p = arg;
Hans Verkuil4b202592012-09-14 07:03:35 -03001956 int ret = check_fmt(file, p->type);
1957
1958 if (ret)
1959 return ret;
Hans Verkuil458aa4a2012-06-09 12:55:52 -03001960
1961 /* Clear up to type, everything after type is zeroed already */
1962 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1963
1964 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1965}
1966
Hans Verkuil82b655b2012-07-05 06:37:08 -03001967static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
1968 struct file *file, void *fh, void *arg)
1969{
1970 struct video_device *vfd = video_devdata(file);
1971 struct v4l2_frequency_band *p = arg;
1972 enum v4l2_tuner_type type;
1973 int err;
1974
Antti Palosaari84099a22013-12-11 20:24:02 -03001975 if (vfd->vfl_type == VFL_TYPE_SDR) {
1976 if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
1977 return -EINVAL;
1978 type = p->type;
1979 } else {
1980 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1981 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1982 if (type != p->type)
1983 return -EINVAL;
1984 }
Hans Verkuil82b655b2012-07-05 06:37:08 -03001985 if (ops->vidioc_enum_freq_bands)
1986 return ops->vidioc_enum_freq_bands(file, fh, p);
Hans Verkuil73f35412013-03-10 13:12:25 -03001987 if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
Hans Verkuil82b655b2012-07-05 06:37:08 -03001988 struct v4l2_tuner t = {
1989 .index = p->tuner,
1990 .type = type,
1991 };
1992
Hans Verkuilaa4d9b52012-08-01 15:52:46 -03001993 if (p->index)
1994 return -EINVAL;
Hans Verkuil82b655b2012-07-05 06:37:08 -03001995 err = ops->vidioc_g_tuner(file, fh, &t);
1996 if (err)
1997 return err;
1998 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
1999 p->rangelow = t.rangelow;
2000 p->rangehigh = t.rangehigh;
2001 p->modulation = (type == V4L2_TUNER_RADIO) ?
2002 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2003 return 0;
2004 }
Hans Verkuil73f35412013-03-10 13:12:25 -03002005 if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
Hans Verkuil82b655b2012-07-05 06:37:08 -03002006 struct v4l2_modulator m = {
2007 .index = p->tuner,
2008 };
2009
2010 if (type != V4L2_TUNER_RADIO)
2011 return -EINVAL;
Hans Verkuilaa4d9b52012-08-01 15:52:46 -03002012 if (p->index)
2013 return -EINVAL;
Hans Verkuil82b655b2012-07-05 06:37:08 -03002014 err = ops->vidioc_g_modulator(file, fh, &m);
2015 if (err)
2016 return err;
2017 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2018 p->rangelow = m.rangelow;
2019 p->rangehigh = m.rangehigh;
2020 p->modulation = (type == V4L2_TUNER_RADIO) ?
2021 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2022 return 0;
2023 }
2024 return -ENOTTY;
2025}
2026
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002027struct v4l2_ioctl_info {
2028 unsigned int ioctl;
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002029 u32 flags;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002030 const char * const name;
Hans Verkuil86748f32012-06-22 06:04:16 -03002031 union {
2032 u32 offset;
2033 int (*func)(const struct v4l2_ioctl_ops *ops,
2034 struct file *file, void *fh, void *p);
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002035 } u;
Hans Verkuil86748f32012-06-22 06:04:16 -03002036 void (*debug)(const void *arg, bool write_only);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002037};
2038
2039/* This control needs a priority check */
2040#define INFO_FL_PRIO (1 << 0)
2041/* This control can be valid if the filehandle passes a control handler. */
2042#define INFO_FL_CTRL (1 << 1)
Hans Verkuil86748f32012-06-22 06:04:16 -03002043/* This is a standard ioctl, no need for special code */
2044#define INFO_FL_STD (1 << 2)
2045/* This is ioctl has its own function */
2046#define INFO_FL_FUNC (1 << 3)
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002047/* Queuing ioctl */
2048#define INFO_FL_QUEUE (1 << 4)
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002049/* Zero struct from after the field to the end */
2050#define INFO_FL_CLEAR(v4l2_struct, field) \
2051 ((offsetof(struct v4l2_struct, field) + \
2052 sizeof(((struct v4l2_struct *)0)->field)) << 16)
2053#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002054
Hans Verkuil86748f32012-06-22 06:04:16 -03002055#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
2056 [_IOC_NR(_ioctl)] = { \
2057 .ioctl = _ioctl, \
2058 .flags = _flags | INFO_FL_STD, \
2059 .name = #_ioctl, \
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002060 .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
Hans Verkuil86748f32012-06-22 06:04:16 -03002061 .debug = _debug, \
2062 }
2063
2064#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
2065 [_IOC_NR(_ioctl)] = { \
2066 .ioctl = _ioctl, \
2067 .flags = _flags | INFO_FL_FUNC, \
2068 .name = #_ioctl, \
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002069 .u.func = _func, \
Hans Verkuil86748f32012-06-22 06:04:16 -03002070 .debug = _debug, \
2071 }
2072
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002073static struct v4l2_ioctl_info v4l2_ioctls[] = {
Hans Verkuil59076122012-06-22 06:09:54 -03002074 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03002075 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
2076 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
2077 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002078 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2079 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 -03002080 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
2081 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
Hans Verkuil737c0972012-09-04 10:08:01 -03002082 IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002083 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
Tomasz Stanislawskib799d09a2012-06-14 11:32:23 -03002084 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 -03002085 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2086 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2087 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
Hans Verkuileb3728e2012-06-22 06:23:59 -03002088 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2089 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
Hans Verkuilca371572013-06-03 05:36:50 -03002090 IOCTL_INFO_STD(VIDIOC_G_STD, vidioc_g_std, v4l_print_std, 0),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03002091 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2092 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
Hans Verkuil59076122012-06-22 06:09:54 -03002093 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
Hans Verkuilefbceec2012-06-09 12:54:02 -03002094 IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2095 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 -03002096 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2097 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
Hans Verkuil59076122012-06-22 06:09:54 -03002098 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
2099 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
Hans Verkuilefbceec2012-06-09 12:54:02 -03002100 IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2101 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 -03002102 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
2103 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuildd519bb2014-03-07 07:18:37 -03002104 IOCTL_INFO_STD(VIDIOC_G_EDID, vidioc_g_edid, v4l_print_edid, INFO_FL_CLEAR(v4l2_edid, edid)),
2105 IOCTL_INFO_STD(VIDIOC_S_EDID, vidioc_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_edid, edid)),
Hans Verkuil59076122012-06-22 06:09:54 -03002106 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
2107 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2108 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2109 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
2110 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
Hans Verkuil82b655b2012-07-05 06:37:08 -03002111 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 -03002112 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2113 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2114 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
Hans Verkuild84f2d942012-06-09 11:57:46 -03002115 IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2116 IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2117 IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2118 IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
2119 IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
Hans Verkuild806f2d2012-06-09 10:02:49 -03002120 IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
2121 IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03002122 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
Hans Verkuilbe6c64e2012-06-22 06:12:57 -03002123 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
Hans Verkuil59076122012-06-22 06:09:54 -03002124 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2125 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
Hans Verkuild0547cb2012-06-09 09:27:10 -03002126 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2127 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002128 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 -03002129 IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
Hans Verkuilefbceec2012-06-09 12:54:02 -03002130 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2131 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 -03002132 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 -03002133 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2134 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 -03002135 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
2136 IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2137 IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2138 IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2139 IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
Hans Verkuilf16f77b2012-06-09 10:06:25 -03002140 IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2141 IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
Hans Verkuil4b1e2e42012-06-22 06:17:48 -03002142 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 -03002143 IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
2144 IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuil458aa4a2012-06-09 12:55:52 -03002145 IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2146 IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2147 IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002148 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2149 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
Hans Verkuilefc626b2012-06-09 10:09:07 -03002150 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
2151 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
Hans Verkuila1acb8f2012-07-11 09:15:06 -03002152 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 -03002153 IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
Hans Verkuil96b03d22013-04-06 06:16:58 -03002154 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 -03002155 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 -03002156};
2157#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2158
2159bool v4l2_is_known_ioctl(unsigned int cmd)
2160{
2161 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2162 return false;
2163 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2164}
2165
Hans Verkuil5a5adf62012-06-22 07:29:35 -03002166struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
2167{
2168 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2169 return vdev->lock;
2170 if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
2171 return NULL;
2172 if (vdev->queue && vdev->queue->lock &&
2173 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2174 return vdev->queue->lock;
2175 return vdev->lock;
2176}
2177
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002178/* Common ioctl debug function. This function can be used by
2179 external ioctl messages as well as internal V4L ioctl */
Hans Verkuil4a085162012-06-22 06:38:06 -03002180void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002181{
Hans Verkuil86748f32012-06-22 06:04:16 -03002182 const char *dir, *type;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002183
Hans Verkuil4a085162012-06-22 06:38:06 -03002184 if (prefix)
2185 printk(KERN_DEBUG "%s: ", prefix);
2186
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002187 switch (_IOC_TYPE(cmd)) {
2188 case 'd':
2189 type = "v4l2_int";
2190 break;
2191 case 'V':
2192 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2193 type = "v4l2";
2194 break;
2195 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002196 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002197 return;
2198 default:
2199 type = "unknown";
Hans Verkuil86748f32012-06-22 06:04:16 -03002200 break;
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002201 }
2202
2203 switch (_IOC_DIR(cmd)) {
2204 case _IOC_NONE: dir = "--"; break;
2205 case _IOC_READ: dir = "r-"; break;
2206 case _IOC_WRITE: dir = "-w"; break;
2207 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2208 default: dir = "*ERR*"; break;
2209 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002210 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
Hans Verkuil4839e6c2012-06-04 05:23:40 -03002211 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2212}
2213EXPORT_SYMBOL(v4l_printk_ioctl);
2214
Hans Verkuil069b7472008-12-30 07:04:34 -03002215static long __video_do_ioctl(struct file *file,
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002216 unsigned int cmd, void *arg)
2217{
2218 struct video_device *vfd = video_devdata(file);
Hans Verkuila3998102008-07-21 02:57:38 -03002219 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
Hans Verkuil86748f32012-06-22 06:04:16 -03002220 bool write_only = false;
2221 struct v4l2_ioctl_info default_info;
2222 const struct v4l2_ioctl_info *info;
Hans Verkuild5fbf322008-10-18 13:39:53 -03002223 void *fh = file->private_data;
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03002224 struct v4l2_fh *vfh = NULL;
Hans Verkuil80131fe02012-06-22 06:37:38 -03002225 int debug = vfd->debug;
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002226 long ret = -ENOTTY;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002227
Hans Verkuil6a717882010-04-06 15:56:08 -03002228 if (ops == NULL) {
Hans Verkuil4a085162012-06-22 06:38:06 -03002229 pr_warn("%s: has no ioctl_ops.\n",
2230 video_device_node_name(vfd));
Mauro Carvalho Chehab9190d192011-07-06 14:08:08 -03002231 return ret;
Hans Verkuil6a717882010-04-06 15:56:08 -03002232 }
2233
Ramakrishnan Muthukrishnanb7284bb2014-06-19 14:22:57 -03002234 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002235 vfh = file->private_data;
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002236
2237 if (v4l2_is_known_ioctl(cmd)) {
Hans Verkuil86748f32012-06-22 06:04:16 -03002238 info = &v4l2_ioctls[_IOC_NR(cmd)];
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002239
2240 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2241 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
Hans Verkuil86748f32012-06-22 06:04:16 -03002242 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002243
Ramakrishnan Muthukrishnanb7284bb2014-06-19 14:22:57 -03002244 if (vfh && (info->flags & INFO_FL_PRIO)) {
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002245 ret = v4l2_prio_check(vfd->prio, vfh->prio);
2246 if (ret)
Hans Verkuil86748f32012-06-22 06:04:16 -03002247 goto done;
Hans Verkuil4b902fe2012-05-10 05:40:50 -03002248 }
Hans Verkuil86748f32012-06-22 06:04:16 -03002249 } else {
2250 default_info.ioctl = cmd;
2251 default_info.flags = 0;
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002252 default_info.debug = v4l_print_default;
Hans Verkuil86748f32012-06-22 06:04:16 -03002253 info = &default_info;
Hans Verkuil48ea0be2012-05-10 05:36:00 -03002254 }
2255
Hans Verkuil86748f32012-06-22 06:04:16 -03002256 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
Hans Verkuil86748f32012-06-22 06:04:16 -03002257 if (info->flags & INFO_FL_STD) {
2258 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2259 const void *p = vfd->ioctl_ops;
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002260 const vidioc_op *vidioc = p + info->u.offset;
Hans Verkuil86748f32012-06-22 06:04:16 -03002261
2262 ret = (*vidioc)(file, fh, arg);
Hans Verkuil86748f32012-06-22 06:04:16 -03002263 } else if (info->flags & INFO_FL_FUNC) {
Hans Verkuil26ddcbc2012-07-12 12:06:24 -03002264 ret = info->u.func(ops, file, fh, arg);
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002265 } else if (!ops->vidioc_default) {
2266 ret = -ENOTTY;
2267 } else {
2268 ret = ops->vidioc_default(file, fh,
Ramakrishnan Muthukrishnanb7284bb2014-06-19 14:22:57 -03002269 vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
Hans Verkuilf18d8e02012-06-22 06:35:01 -03002270 cmd, arg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002271 }
2272
Hans Verkuil86748f32012-06-22 06:04:16 -03002273done:
Hans Verkuil80131fe02012-06-22 06:37:38 -03002274 if (debug) {
Hans Verkuil4a085162012-06-22 06:38:06 -03002275 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
Hans Verkuil86748f32012-06-22 06:04:16 -03002276 if (ret < 0)
Hans Verkuil505d04b2013-03-14 07:40:45 -03002277 pr_cont(": error %ld", ret);
2278 if (debug == V4L2_DEBUG_IOCTL)
Hans Verkuil86748f32012-06-22 06:04:16 -03002279 pr_cont("\n");
Hans Verkuil86748f32012-06-22 06:04:16 -03002280 else if (_IOC_DIR(cmd) == _IOC_NONE)
2281 info->debug(arg, write_only);
2282 else {
2283 pr_cont(": ");
2284 info->debug(arg, write_only);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002285 }
2286 }
2287
2288 return ret;
2289}
2290
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002291static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
Hans Verkuilba2d35c12014-03-17 09:54:23 -03002292 void __user **user_ptr, void ***kernel_ptr)
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002293{
2294 int ret = 0;
2295
2296 switch (cmd) {
Hans Verkuil96b1a702012-09-19 10:14:41 -03002297 case VIDIOC_PREPARE_BUF:
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002298 case VIDIOC_QUERYBUF:
2299 case VIDIOC_QBUF:
2300 case VIDIOC_DQBUF: {
2301 struct v4l2_buffer *buf = parg;
2302
2303 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2304 if (buf->length > VIDEO_MAX_PLANES) {
2305 ret = -EINVAL;
2306 break;
2307 }
2308 *user_ptr = (void __user *)buf->m.planes;
Hans Verkuilba2d35c12014-03-17 09:54:23 -03002309 *kernel_ptr = (void **)&buf->m.planes;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002310 *array_size = sizeof(struct v4l2_plane) * buf->length;
2311 ret = 1;
2312 }
2313 break;
2314 }
2315
Hans Verkuildd519bb2014-03-07 07:18:37 -03002316 case VIDIOC_G_EDID:
2317 case VIDIOC_S_EDID: {
2318 struct v4l2_edid *edid = parg;
Hans Verkuiled45ce22012-08-10 06:07:12 -03002319
2320 if (edid->blocks) {
Hans Verkuil1b8b10c2012-10-02 02:47:58 -03002321 if (edid->blocks > 256) {
2322 ret = -EINVAL;
2323 break;
2324 }
Hans Verkuiled45ce22012-08-10 06:07:12 -03002325 *user_ptr = (void __user *)edid->edid;
Hans Verkuilba2d35c12014-03-17 09:54:23 -03002326 *kernel_ptr = (void **)&edid->edid;
Hans Verkuiled45ce22012-08-10 06:07:12 -03002327 *array_size = edid->blocks * 128;
2328 ret = 1;
2329 }
2330 break;
2331 }
2332
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002333 case VIDIOC_S_EXT_CTRLS:
2334 case VIDIOC_G_EXT_CTRLS:
2335 case VIDIOC_TRY_EXT_CTRLS: {
2336 struct v4l2_ext_controls *ctrls = parg;
2337
2338 if (ctrls->count != 0) {
Dan Carpenter6c061082012-01-05 02:27:57 -03002339 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2340 ret = -EINVAL;
2341 break;
2342 }
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002343 *user_ptr = (void __user *)ctrls->controls;
Hans Verkuilba2d35c12014-03-17 09:54:23 -03002344 *kernel_ptr = (void **)&ctrls->controls;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002345 *array_size = sizeof(struct v4l2_ext_control)
2346 * ctrls->count;
2347 ret = 1;
2348 }
2349 break;
2350 }
2351 }
2352
2353 return ret;
2354}
2355
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002356long
2357video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2358 v4l2_kioctl func)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002359{
2360 char sbuf[128];
2361 void *mbuf = NULL;
Hans Verkuil1d94aa32010-04-06 08:12:21 -03002362 void *parg = (void *)arg;
Hans Verkuil069b7472008-12-30 07:04:34 -03002363 long err = -EINVAL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002364 bool has_array_args;
2365 size_t array_size = 0;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002366 void __user *user_ptr = NULL;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002367 void **kernel_ptr = NULL;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002368
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002369 /* Copy arguments into temp kernel buffer */
Trent Piepho337f9d22009-03-04 01:21:02 -03002370 if (_IOC_DIR(cmd) != _IOC_NONE) {
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002371 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2372 parg = sbuf;
2373 } else {
2374 /* too big to allocate from stack */
2375 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2376 if (NULL == mbuf)
2377 return -ENOMEM;
2378 parg = mbuf;
2379 }
2380
2381 err = -EFAULT;
Trent Piepho19c96e42009-03-04 01:21:02 -03002382 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Hans Verkuil27cd2ab2012-06-09 08:55:31 -03002383 unsigned int n = _IOC_SIZE(cmd);
2384
2385 /*
2386 * In some cases, only a few fields are used as input,
2387 * i.e. when the app sets "index" and then the driver
2388 * fills in the rest of the structure for the thing
2389 * with that index. We only need to copy up the first
2390 * non-input field.
2391 */
2392 if (v4l2_is_known_ioctl(cmd)) {
2393 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2394 if (flags & INFO_FL_CLEAR_MASK)
2395 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2396 }
Trent Piepho19c96e42009-03-04 01:21:02 -03002397
2398 if (copy_from_user(parg, (void __user *)arg, n))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002399 goto out;
Trent Piepho19c96e42009-03-04 01:21:02 -03002400
2401 /* zero out anything we don't copy from userspace */
2402 if (n < _IOC_SIZE(cmd))
2403 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
Trent Piepho337f9d22009-03-04 01:21:02 -03002404 } else {
2405 /* read-only ioctl */
2406 memset(parg, 0, _IOC_SIZE(cmd));
Trent Piepho19c96e42009-03-04 01:21:02 -03002407 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002408 }
2409
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002410 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2411 if (err < 0)
2412 goto out;
2413 has_array_args = err;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002414
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002415 if (has_array_args) {
2416 /*
2417 * When adding new types of array args, make sure that the
2418 * parent argument to ioctl (which contains the pointer to the
2419 * array) fits into sbuf (so that mbuf will still remain
2420 * unused up to here).
2421 */
2422 mbuf = kmalloc(array_size, GFP_KERNEL);
2423 err = -ENOMEM;
2424 if (NULL == mbuf)
2425 goto out_array_args;
2426 err = -EFAULT;
2427 if (copy_from_user(mbuf, user_ptr, array_size))
2428 goto out_array_args;
2429 *kernel_ptr = mbuf;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002430 }
2431
2432 /* Handles IOCTL */
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002433 err = func(file, cmd, parg);
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002434 if (err == -ENOIOCTLCMD)
Hans Verkuil02bbb812012-02-08 08:09:36 -03002435 err = -ENOTTY;
Hans Verkuilaa32f4c2013-12-16 05:45:37 -03002436 if (err == 0) {
2437 if (cmd == VIDIOC_DQBUF)
2438 trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
2439 else if (cmd == VIDIOC_QBUF)
2440 trace_v4l2_qbuf(video_devdata(file)->minor, parg);
2441 }
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002442
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002443 if (has_array_args) {
Hans Verkuilba2d35c12014-03-17 09:54:23 -03002444 *kernel_ptr = (void __force *)user_ptr;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002445 if (copy_to_user(user_ptr, mbuf, array_size))
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002446 err = -EFAULT;
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002447 goto out_array_args;
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002448 }
Hans Verkuil5d7758e2012-05-15 08:06:44 -03002449 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2450 results that must be returned. */
2451 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002452 goto out;
2453
Pawel Osciakd14e6d72010-12-23 04:15:27 -03002454out_array_args:
Hans Verkuil35ea11f2008-07-20 08:12:02 -03002455 /* Copy results into user buffer */
2456 switch (_IOC_DIR(cmd)) {
2457 case _IOC_READ:
2458 case (_IOC_WRITE | _IOC_READ):
2459 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2460 err = -EFAULT;
2461 break;
2462 }
2463
2464out:
2465 kfree(mbuf);
2466 return err;
2467}
Laurent Pinchartfc0a8072010-07-12 11:09:41 -03002468EXPORT_SYMBOL(video_usercopy);
2469
2470long video_ioctl2(struct file *file,
2471 unsigned int cmd, unsigned long arg)
2472{
2473 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2474}
Mauro Carvalho Chehab8a522c92008-10-21 11:58:39 -03002475EXPORT_SYMBOL(video_ioctl2);