[media] v4l: Support extending the v4l2_pix_format structure
[deliverable/linux.git] / drivers / media / v4l2-core / v4l2-ioctl.c
CommitLineData
35ea11ff
HV
1/*
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 *
d9b01449 11 * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
35ea11ff
HV
12 * Mauro Carvalho Chehab <mchehab@infradead.org> (version 2)
13 */
14
15#include <linux/module.h>
5a0e3ad6 16#include <linux/slab.h>
35ea11ff
HV
17#include <linux/types.h>
18#include <linux/kernel.h>
ae6db515 19#include <linux/version.h>
35ea11ff 20
35ea11ff
HV
21#include <linux/videodev2.h>
22
35ea11ff
HV
23#include <media/v4l2-common.h>
24#include <media/v4l2-ioctl.h>
11bbc1ca 25#include <media/v4l2-ctrls.h>
d3d7c963
SA
26#include <media/v4l2-fh.h>
27#include <media/v4l2-event.h>
99cd47bc 28#include <media/v4l2-device.h>
5a5adf6b 29#include <media/videobuf2-core.h>
35ea11ff 30
aa32f4c0
HV
31#define CREATE_TRACE_POINTS
32#include <trace/events/v4l2.h>
33
25985edc 34/* Zero out the end of the struct pointed to by p. Everything after, but
7ecc0cf9
TP
35 * 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
73f35418
HV
40#define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
41
35ea11ff
HV
42struct 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
51f0b8d5
TP
101/* 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
35ea11ff
HV
114/* 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{
51f0b8d5
TP
119 vs->id = id;
120 v4l2_video_std_frame_period(id, &vs->frameperiod);
121 vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
35ea11ff
HV
122 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",
f8f3914c
PO
153 [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
154 [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
6f3073b8 155 [V4L2_BUF_TYPE_SDR_CAPTURE] = "sdr-cap",
35ea11ff
HV
156};
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",
051c7788 163 [V4L2_MEMORY_DMABUF] = "dmabuf",
35ea11ff
HV
164};
165
d9246240 166#define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
35ea11ff
HV
167
168/* ------------------------------------------------------------------ */
169/* debug help functions */
8ab75e3e 170
59076122
HV
171static void v4l_print_querycap(const void *arg, bool write_only)
172{
173 const struct v4l2_capability *p = arg;
174
27d5a87c 175 pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, "
59076122 176 "capabilities=0x%08x, device_caps=0x%08x\n",
27d5a87c
HV
177 (int)sizeof(p->driver), p->driver,
178 (int)sizeof(p->card), p->card,
179 (int)sizeof(p->bus_info), p->bus_info,
59076122
HV
180 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
27d5a87c 187 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, "
59076122 188 "std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
27d5a87c
HV
189 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);
59076122
HV
192}
193
194static void v4l_print_enumoutput(const void *arg, bool write_only)
195{
196 const struct v4l2_output *p = arg;
197
27d5a87c 198 pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, "
59076122 199 "modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
27d5a87c
HV
200 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
201 p->modulator, (unsigned long long)p->std, p->capabilities);
59076122
HV
202}
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
27d5a87c
HV
211 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);
59076122
HV
214}
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
27d5a87c
HV
223 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);
59076122
HV
226}
227
be6c64e4
HV
228static void v4l_print_fmtdesc(const void *arg, bool write_only)
229{
230 const struct v4l2_fmtdesc *p = arg;
231
27d5a87c 232 pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%.*s'\n",
be6c64e4
HV
233 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,
27d5a87c 238 (int)sizeof(p->description), p->description);
be6c64e4
HV
239}
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;
87185c95 249 const struct v4l2_sdr_format *sdr;
be6c64e4
HV
250 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, "
560dde24 259 "bytesperline=%u, sizeimage=%u, colorspace=%d\n",
be6c64e4
HV
260 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;
560dde24
HV
290 /* 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,
be6c64e4 295 prt_names(win->field, v4l2_field_names),
560dde24
HV
296 win->chromakey, win->clipcount, win->clips,
297 win->bitmap, win->global_alpha);
be6c64e4
HV
298 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;
582c52cb
AP
323 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;
be6c64e4
HV
331 }
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, "
560dde24 340 "bytesperline=%u, sizeimage=%u, colorspace=%d\n",
be6c64e4
HV
341 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
e325b244
HV
351static 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
4b1e2e4d
HV
356static void v4l_print_modulator(const void *arg, bool write_only)
357{
358 const struct v4l2_modulator *p = arg;
359
360 if (write_only)
560dde24 361 pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
4b1e2e4d 362 else
27d5a87c 363 pr_cont("index=%u, name=%.*s, capability=0x%x, "
4b1e2e4d 364 "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
27d5a87c 365 p->index, (int)sizeof(p->name), p->name, p->capability,
4b1e2e4d
HV
366 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
27d5a87c 376 pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, "
4b1e2e4d
HV
377 "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
378 "rxsubchans=0x%x, audmode=%u\n",
27d5a87c 379 p->index, (int)sizeof(p->name), p->name, p->type,
4b1e2e4d
HV
380 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
27d5a87c 397 pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, "
4b1e2e4d 398 "framelines=%u\n", p->index,
27d5a87c 399 (unsigned long long)p->id, (int)sizeof(p->name), p->name,
4b1e2e4d
HV
400 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
5e788317
HV
414 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);
4b1e2e4d
HV
418}
419
eb3728ed 420static void v4l_print_requestbuffers(const void *arg, bool write_only)
59076122 421{
eb3728ed
HV
422 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));
59076122
HV
428}
429
eb3728ed 430static void v4l_print_buffer(const void *arg, bool write_only)
35ea11ff 431{
eb3728ed
HV
432 const struct v4l2_buffer *p = arg;
433 const struct v4l2_timecode *tc = &p->timecode;
434 const struct v4l2_plane *plane;
d14e6d76 435 int i;
35ea11ff 436
eb3728ed
HV
437 pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
438 "flags=0x%08x, field=%s, sequence=%d, memory=%s",
35ea11ff
HV
439 p->timestamp.tv_sec / 3600,
440 (int)(p->timestamp.tv_sec / 60) % 60,
441 (int)(p->timestamp.tv_sec % 60),
b045979d 442 (long)p->timestamp.tv_usec,
35ea11ff
HV
443 p->index,
444 prt_names(p->type, v4l2_type_names),
eb3728ed
HV
445 p->flags, prt_names(p->field, v4l2_field_names),
446 p->sequence, prt_names(p->memory, v4l2_memory_names));
d14e6d76
PO
447
448 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
eb3728ed 449 pr_cont("\n");
d14e6d76
PO
450 for (i = 0; i < p->length; ++i) {
451 plane = &p->m.planes[i];
eb3728ed 452 printk(KERN_DEBUG
560dde24 453 "plane %d: bytesused=%d, data_offset=0x%08x, "
eb3728ed 454 "offset/userptr=0x%lx, length=%d\n",
d14e6d76
PO
455 i, plane->bytesused, plane->data_offset,
456 plane->m.userptr, plane->length);
457 }
458 } else {
560dde24 459 pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
d14e6d76
PO
460 p->bytesused, p->m.userptr, p->length);
461 }
462
eb3728ed
HV
463 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
464 "flags=0x%08x, frames=%d, userbits=0x%08x\n",
35ea11ff
HV
465 tc->hours, tc->minutes, tc->seconds,
466 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
467}
468
b799d09a
TS
469static 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
eb3728ed
HV
478static 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);
560dde24
HV
512 } else {
513 pr_cont("\n");
eb3728ed
HV
514 }
515}
516
efbceecd
HV
517static void v4l_print_queryctrl(const void *arg, bool write_only)
518{
519 const struct v4l2_queryctrl *p = arg;
520
27d5a87c 521 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, "
efbceecd 522 "step=%d, default=%d, flags=0x%08x\n",
27d5a87c 523 p->id, p->type, (int)sizeof(p->name), p->name,
efbceecd
HV
524 p->minimum, p->maximum,
525 p->step, p->default_value, p->flags);
526}
527
e6bee368
HV
528static 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, "
0176077a 534 "nr_of_dims=%u, dims=%u,%u,%u,%u\n",
e6bee368
HV
535 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,
0176077a 539 p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
e6bee368
HV
540}
541
efbceecd
HV
542static 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
d84f2d94 574static void v4l_print_cropcap(const void *arg, bool write_only)
eb3728ed 575{
d84f2d94
HV
576 const struct v4l2_cropcap *p = arg;
577
578 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
a112fbaf 579 "defrect wxh=%dx%d, x,y=%d,%d, "
d84f2d94
HV
580 "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);
eb3728ed
HV
587}
588
d84f2d94 589static void v4l_print_crop(const void *arg, bool write_only)
35ea11ff 590{
d84f2d94
HV
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
d806f2df
HV
609static 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
96b03d2a 648static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
79b0c640 649{
96b03d2a 650 const struct v4l2_dbg_chip_info *p = arg;
79b0c640
HV
651
652 pr_cont("type=%u, ", p->match.type);
3eef2510 653 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
79b0c640
HV
654 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
f16f77b0
HV
661static 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);
3eef2510 666 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
27d5a87c
HV
667 pr_cont("name=%.*s, ",
668 (int)sizeof(p->match.name), p->match.name);
f16f77b0
HV
669 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
efc626b0 675static void v4l_print_dv_timings(const void *arg, bool write_only)
5d7758ee 676{
efc626b0
HV
677 const struct v4l2_dv_timings *p = arg;
678
5d7758ee
HV
679 switch (p->type) {
680 case V4L2_DV_BT_656_1120:
efc626b0
HV
681 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",
5d7758ee
HV
689 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:
efc626b0 699 pr_cont("type=%d\n", p->type);
5d7758ee
HV
700 break;
701 }
702}
703
efc626b0
HV
704static 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
458aa4a6
HV
731static 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:
560dde24 744 pr_cont(", wxh=%ux%u\n",
458aa4a6
HV
745 p->discrete.width, p->discrete.height);
746 break;
747 case V4L2_FRMSIZE_TYPE_STEPWISE:
560dde24 748 pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
458aa4a6
HV
749 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:
560dde24 774 pr_cont(", fps=%d/%d\n",
458aa4a6
HV
775 p->discrete.numerator,
776 p->discrete.denominator);
777 break;
778 case V4L2_FRMIVAL_TYPE_STEPWISE:
560dde24 779 pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
458aa4a6
HV
780 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);
560dde24
HV
817 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, "
818 "default_value=%d\n",
458aa4a6
HV
819 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
82b655bf
HV
850static 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, "
560dde24 855 "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
82b655bf
HV
856 p->tuner, p->type, p->index,
857 p->capability, p->rangelow,
858 p->rangehigh, p->modulation);
859}
860
dd519bb3
HV
861static 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
efc626b0
HV
869static 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
f18d8e07
HV
879static void v4l_print_default(const void *arg, bool write_only)
880{
881 pr_cont("driver-specific ioctl\n");
882}
883
efbceecd 884static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
35ea11ff
HV
885{
886 __u32 i;
887
888 /* zero the reserved fields */
889 c->reserved[0] = c->reserved[1] = 0;
6b5a9492 890 for (i = 0; i < c->count; i++)
35ea11ff 891 c->controls[i].reserved2[0] = 0;
6b5a9492 892
35ea11ff
HV
893 /* 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
4b20259f 910static int check_fmt(struct file *file, enum v4l2_buf_type type)
35ea11ff 911{
4b20259f
HV
912 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;
582c52cb 916 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
4b20259f
HV
917 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
918 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
919
a399810c
HV
920 if (ops == NULL)
921 return -EINVAL;
922
35ea11ff
HV
923 switch (type) {
924 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4b20259f
HV
925 if (is_vid && is_rx &&
926 (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
d14e6d76
PO
927 return 0;
928 break;
929 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
4b20259f 930 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
35ea11ff
HV
931 return 0;
932 break;
933 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4b20259f 934 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
35ea11ff
HV
935 return 0;
936 break;
937 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4b20259f
HV
938 if (is_vid && is_tx &&
939 (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
d14e6d76
PO
940 return 0;
941 break;
942 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
4b20259f 943 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
35ea11ff
HV
944 return 0;
945 break;
946 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
4b20259f 947 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
35ea11ff
HV
948 return 0;
949 break;
950 case V4L2_BUF_TYPE_VBI_CAPTURE:
4b20259f 951 if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
35ea11ff
HV
952 return 0;
953 break;
954 case V4L2_BUF_TYPE_VBI_OUTPUT:
4b20259f 955 if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
35ea11ff
HV
956 return 0;
957 break;
958 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
4b20259f 959 if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
35ea11ff
HV
960 return 0;
961 break;
962 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
4b20259f 963 if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
35ea11ff
HV
964 return 0;
965 break;
582c52cb
AP
966 case V4L2_BUF_TYPE_SDR_CAPTURE:
967 if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
968 return 0;
969 break;
633c98e5 970 default:
35ea11ff
HV
971 break;
972 }
973 return -EINVAL;
974}
975
d52e2381
LP
976static void v4l_sanitize_format(struct v4l2_format *fmt)
977{
978 unsigned int offset;
979
980 /*
981 * The v4l2_pix_format structure has been extended with fields that were
982 * not previously required to be set to zero by applications. The priv
983 * field, when set to a magic value, indicates the the extended fields
984 * are valid. Otherwise they will contain undefined values. To simplify
985 * the API towards drivers zero the extended fields and set the priv
986 * field to the magic value when the extended pixel format structure
987 * isn't used by applications.
988 */
989
990 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
991 fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
992 return;
993
994 if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
995 return;
996
997 fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
998
999 offset = offsetof(struct v4l2_pix_format, priv)
1000 + sizeof(fmt->fmt.pix.priv);
1001 memset(((void *)&fmt->fmt.pix) + offset, 0,
1002 sizeof(fmt->fmt.pix) - offset);
1003}
1004
59076122
HV
1005static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
1006 struct file *file, void *fh, void *arg)
1007{
1008 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
d52e2381 1009 int ret;
59076122
HV
1010
1011 cap->version = LINUX_VERSION_CODE;
d52e2381
LP
1012
1013 ret = ops->vidioc_querycap(file, fh, cap);
1014
1015 cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
1016
1017 return ret;
59076122
HV
1018}
1019
1020static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
1021 struct file *file, void *fh, void *arg)
1022{
1023 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
1024}
1025
1026static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
1027 struct file *file, void *fh, void *arg)
1028{
1029 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
1030}
1031
d0547cba
HV
1032static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
1033 struct file *file, void *fh, void *arg)
1034{
1035 struct video_device *vfd;
1036 u32 *p = arg;
1037
1038 if (ops->vidioc_g_priority)
1039 return ops->vidioc_g_priority(file, fh, arg);
1040 vfd = video_devdata(file);
1041 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
1042 return 0;
1043}
1044
1045static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
1046 struct file *file, void *fh, void *arg)
1047{
1048 struct video_device *vfd;
1049 struct v4l2_fh *vfh;
1050 u32 *p = arg;
1051
1052 if (ops->vidioc_s_priority)
1053 return ops->vidioc_s_priority(file, fh, *p);
1054 vfd = video_devdata(file);
1055 vfh = file->private_data;
1056 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
1057}
1058
59076122
HV
1059static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1060 struct file *file, void *fh, void *arg)
1061{
73f35418 1062 struct video_device *vfd = video_devdata(file);
59076122
HV
1063 struct v4l2_input *p = arg;
1064
1065 /*
02fa6282 1066 * We set the flags for CAP_DV_TIMINGS &
59076122
HV
1067 * CAP_STD here based on ioctl handler provided by the
1068 * driver. If the driver doesn't support these
1069 * for a specific input, it must override these flags.
1070 */
73f35418 1071 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
59076122 1072 p->capabilities |= V4L2_IN_CAP_STD;
59076122
HV
1073
1074 return ops->vidioc_enum_input(file, fh, p);
1075}
1076
1077static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1078 struct file *file, void *fh, void *arg)
1079{
73f35418 1080 struct video_device *vfd = video_devdata(file);
59076122
HV
1081 struct v4l2_output *p = arg;
1082
1083 /*
02fa6282 1084 * We set the flags for CAP_DV_TIMINGS &
59076122
HV
1085 * CAP_STD here based on ioctl handler provided by the
1086 * driver. If the driver doesn't support these
1087 * for a specific output, it must override these flags.
1088 */
73f35418 1089 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
59076122 1090 p->capabilities |= V4L2_OUT_CAP_STD;
59076122
HV
1091
1092 return ops->vidioc_enum_output(file, fh, p);
1093}
1094
be6c64e4
HV
1095static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1096 struct file *file, void *fh, void *arg)
1097{
1098 struct v4l2_fmtdesc *p = arg;
4b20259f
HV
1099 struct video_device *vfd = video_devdata(file);
1100 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1101 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
be6c64e4
HV
1102
1103 switch (p->type) {
1104 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4b20259f 1105 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_cap))
be6c64e4
HV
1106 break;
1107 return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1108 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
4b20259f 1109 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_cap_mplane))
be6c64e4
HV
1110 break;
1111 return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1112 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4b20259f 1113 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_overlay))
be6c64e4
HV
1114 break;
1115 return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1116 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4b20259f 1117 if (unlikely(!is_tx || !ops->vidioc_enum_fmt_vid_out))
be6c64e4
HV
1118 break;
1119 return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1120 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
4b20259f 1121 if (unlikely(!is_tx || !ops->vidioc_enum_fmt_vid_out_mplane))
be6c64e4
HV
1122 break;
1123 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
582c52cb
AP
1124 case V4L2_BUF_TYPE_SDR_CAPTURE:
1125 if (unlikely(!is_rx || !ops->vidioc_enum_fmt_sdr_cap))
1126 break;
1127 return ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
be6c64e4
HV
1128 }
1129 return -EINVAL;
1130}
1131
1132static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1133 struct file *file, void *fh, void *arg)
1134{
1135 struct v4l2_format *p = arg;
4b20259f
HV
1136 struct video_device *vfd = video_devdata(file);
1137 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
582c52cb 1138 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
4b20259f
HV
1139 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1140 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
d52e2381
LP
1141 int ret;
1142
1143 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
be6c64e4
HV
1144
1145 switch (p->type) {
1146 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4b20259f 1147 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap))
be6c64e4 1148 break;
d52e2381
LP
1149 ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1150 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1151 return ret;
be6c64e4 1152 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
4b20259f 1153 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap_mplane))
be6c64e4
HV
1154 break;
1155 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1156 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4b20259f 1157 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_overlay))
be6c64e4
HV
1158 break;
1159 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
4b20259f
HV
1160 case V4L2_BUF_TYPE_VBI_CAPTURE:
1161 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_vbi_cap))
1162 break;
1163 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1164 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1165 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_cap))
1166 break;
1167 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
be6c64e4 1168 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4b20259f 1169 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out))
be6c64e4 1170 break;
d52e2381
LP
1171 ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
1172 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1173 return ret;
be6c64e4 1174 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
4b20259f 1175 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_mplane))
be6c64e4
HV
1176 break;
1177 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1178 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
4b20259f 1179 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_overlay))
be6c64e4
HV
1180 break;
1181 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
be6c64e4 1182 case V4L2_BUF_TYPE_VBI_OUTPUT:
4b20259f 1183 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_vbi_out))
be6c64e4
HV
1184 break;
1185 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
be6c64e4 1186 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
4b20259f 1187 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_out))
be6c64e4
HV
1188 break;
1189 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
582c52cb
AP
1190 case V4L2_BUF_TYPE_SDR_CAPTURE:
1191 if (unlikely(!is_rx || !is_sdr || !ops->vidioc_g_fmt_sdr_cap))
1192 break;
1193 return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
be6c64e4
HV
1194 }
1195 return -EINVAL;
1196}
1197
1198static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1199 struct file *file, void *fh, void *arg)
1200{
1201 struct v4l2_format *p = arg;
4b20259f
HV
1202 struct video_device *vfd = video_devdata(file);
1203 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
582c52cb 1204 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
4b20259f
HV
1205 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1206 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
be6c64e4 1207
d52e2381
LP
1208 v4l_sanitize_format(p);
1209
be6c64e4
HV
1210 switch (p->type) {
1211 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4b20259f 1212 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap))
be6c64e4
HV
1213 break;
1214 CLEAR_AFTER_FIELD(p, fmt.pix);
1215 return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1216 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
4b20259f 1217 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap_mplane))
be6c64e4
HV
1218 break;
1219 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1220 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1221 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4b20259f 1222 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_overlay))
be6c64e4
HV
1223 break;
1224 CLEAR_AFTER_FIELD(p, fmt.win);
1225 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
4b20259f
HV
1226 case V4L2_BUF_TYPE_VBI_CAPTURE:
1227 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_vbi_cap))
1228 break;
1229 CLEAR_AFTER_FIELD(p, fmt.vbi);
1230 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1231 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1232 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_cap))
1233 break;
1234 CLEAR_AFTER_FIELD(p, fmt.sliced);
1235 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
be6c64e4 1236 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4b20259f 1237 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out))
be6c64e4
HV
1238 break;
1239 CLEAR_AFTER_FIELD(p, fmt.pix);
1240 return ops->vidioc_s_fmt_vid_out(file, fh, arg);
1241 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
4b20259f 1242 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_mplane))
be6c64e4
HV
1243 break;
1244 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1245 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1246 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
4b20259f 1247 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_overlay))
be6c64e4
HV
1248 break;
1249 CLEAR_AFTER_FIELD(p, fmt.win);
1250 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
be6c64e4 1251 case V4L2_BUF_TYPE_VBI_OUTPUT:
4b20259f 1252 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_vbi_out))
be6c64e4
HV
1253 break;
1254 CLEAR_AFTER_FIELD(p, fmt.vbi);
1255 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
be6c64e4 1256 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
4b20259f 1257 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_out))
be6c64e4
HV
1258 break;
1259 CLEAR_AFTER_FIELD(p, fmt.sliced);
1260 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
582c52cb
AP
1261 case V4L2_BUF_TYPE_SDR_CAPTURE:
1262 if (unlikely(!is_rx || !is_sdr || !ops->vidioc_s_fmt_sdr_cap))
1263 break;
1264 CLEAR_AFTER_FIELD(p, fmt.sdr);
1265 return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
be6c64e4
HV
1266 }
1267 return -EINVAL;
1268}
1269
1270static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1271 struct file *file, void *fh, void *arg)
1272{
1273 struct v4l2_format *p = arg;
4b20259f
HV
1274 struct video_device *vfd = video_devdata(file);
1275 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
582c52cb 1276 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
4b20259f
HV
1277 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1278 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
be6c64e4 1279
d52e2381
LP
1280 v4l_sanitize_format(p);
1281
be6c64e4
HV
1282 switch (p->type) {
1283 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4b20259f 1284 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap))
be6c64e4
HV
1285 break;
1286 CLEAR_AFTER_FIELD(p, fmt.pix);
1287 return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1288 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
4b20259f 1289 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap_mplane))
be6c64e4
HV
1290 break;
1291 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1292 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1293 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4b20259f 1294 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_overlay))
be6c64e4
HV
1295 break;
1296 CLEAR_AFTER_FIELD(p, fmt.win);
1297 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
4b20259f
HV
1298 case V4L2_BUF_TYPE_VBI_CAPTURE:
1299 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_vbi_cap))
1300 break;
1301 CLEAR_AFTER_FIELD(p, fmt.vbi);
1302 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1303 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1304 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_cap))
1305 break;
1306 CLEAR_AFTER_FIELD(p, fmt.sliced);
1307 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
be6c64e4 1308 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4b20259f 1309 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out))
be6c64e4
HV
1310 break;
1311 CLEAR_AFTER_FIELD(p, fmt.pix);
1312 return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1313 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
4b20259f 1314 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_mplane))
be6c64e4
HV
1315 break;
1316 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1317 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1318 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
4b20259f 1319 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_overlay))
be6c64e4
HV
1320 break;
1321 CLEAR_AFTER_FIELD(p, fmt.win);
1322 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
be6c64e4 1323 case V4L2_BUF_TYPE_VBI_OUTPUT:
4b20259f 1324 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_vbi_out))
be6c64e4
HV
1325 break;
1326 CLEAR_AFTER_FIELD(p, fmt.vbi);
1327 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
be6c64e4 1328 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
4b20259f 1329 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_out))
be6c64e4
HV
1330 break;
1331 CLEAR_AFTER_FIELD(p, fmt.sliced);
1332 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
582c52cb
AP
1333 case V4L2_BUF_TYPE_SDR_CAPTURE:
1334 if (unlikely(!is_rx || !is_sdr || !ops->vidioc_try_fmt_sdr_cap))
1335 break;
1336 CLEAR_AFTER_FIELD(p, fmt.sdr);
1337 return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
be6c64e4
HV
1338 }
1339 return -EINVAL;
1340}
1341
e325b244
HV
1342static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1343 struct file *file, void *fh, void *arg)
1344{
1345 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1346}
1347
1348static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1349 struct file *file, void *fh, void *arg)
1350{
1351 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1352}
1353
4b1e2e4d
HV
1354static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1355 struct file *file, void *fh, void *arg)
1356{
1357 struct video_device *vfd = video_devdata(file);
1358 struct v4l2_tuner *p = arg;
82b655bf 1359 int err;
4b1e2e4d
HV
1360
1361 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1362 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
82b655bf
HV
1363 err = ops->vidioc_g_tuner(file, fh, p);
1364 if (!err)
1365 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1366 return err;
4b1e2e4d
HV
1367}
1368
1369static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1370 struct file *file, void *fh, void *arg)
1371{
1372 struct video_device *vfd = video_devdata(file);
1373 struct v4l2_tuner *p = arg;
1374
1375 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1376 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1377 return ops->vidioc_s_tuner(file, fh, p);
1378}
1379
82b655bf
HV
1380static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1381 struct file *file, void *fh, void *arg)
1382{
1383 struct v4l2_modulator *p = arg;
1384 int err;
1385
1386 err = ops->vidioc_g_modulator(file, fh, p);
1387 if (!err)
1388 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1389 return err;
1390}
1391
4b1e2e4d
HV
1392static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1393 struct file *file, void *fh, void *arg)
1394{
1395 struct video_device *vfd = video_devdata(file);
1396 struct v4l2_frequency *p = arg;
1397
84099a28
AP
1398 if (vfd->vfl_type == VFL_TYPE_SDR)
1399 p->type = V4L2_TUNER_ADC;
1400 else
1401 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1402 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
4b1e2e4d
HV
1403 return ops->vidioc_g_frequency(file, fh, p);
1404}
1405
1406static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1407 struct file *file, void *fh, void *arg)
1408{
1409 struct video_device *vfd = video_devdata(file);
b530a447 1410 const struct v4l2_frequency *p = arg;
4b1e2e4d
HV
1411 enum v4l2_tuner_type type;
1412
84099a28
AP
1413 if (vfd->vfl_type == VFL_TYPE_SDR) {
1414 if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
1415 return -EINVAL;
1416 } else {
1417 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1418 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1419 if (type != p->type)
1420 return -EINVAL;
1421 }
4b1e2e4d
HV
1422 return ops->vidioc_s_frequency(file, fh, p);
1423}
1424
1425static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1426 struct file *file, void *fh, void *arg)
1427{
1428 struct video_device *vfd = video_devdata(file);
1429 struct v4l2_standard *p = arg;
1430 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1431 unsigned int index = p->index, i, j = 0;
1432 const char *descr = "";
1433
a5338190
HV
1434 /* Return -ENODATA if the tvnorms for the current input
1435 or output is 0, meaning that it doesn't support this API. */
1436 if (id == 0)
1437 return -ENODATA;
1438
4b1e2e4d
HV
1439 /* Return norm array in a canonical way */
1440 for (i = 0; i <= index && id; i++) {
1441 /* last std value in the standards array is 0, so this
1442 while always ends there since (id & 0) == 0. */
1443 while ((id & standards[j].std) != standards[j].std)
1444 j++;
1445 curr_id = standards[j].std;
1446 descr = standards[j].descr;
1447 j++;
1448 if (curr_id == 0)
1449 break;
1450 if (curr_id != V4L2_STD_PAL &&
1451 curr_id != V4L2_STD_SECAM &&
1452 curr_id != V4L2_STD_NTSC)
1453 id &= ~curr_id;
1454 }
1455 if (i <= index)
1456 return -EINVAL;
1457
1458 v4l2_video_std_construct(p, curr_id, descr);
1459 return 0;
1460}
1461
4b1e2e4d
HV
1462static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1463 struct file *file, void *fh, void *arg)
1464{
1465 struct video_device *vfd = video_devdata(file);
314527ac 1466 v4l2_std_id id = *(v4l2_std_id *)arg, norm;
4b1e2e4d 1467
314527ac 1468 norm = id & vfd->tvnorms;
4b1e2e4d
HV
1469 if (vfd->tvnorms && !norm) /* Check if std is supported */
1470 return -EINVAL;
1471
1472 /* Calls the specific handler */
ca371575 1473 return ops->vidioc_s_std(file, fh, norm);
4b1e2e4d
HV
1474}
1475
1476static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1477 struct file *file, void *fh, void *arg)
1478{
1479 struct video_device *vfd = video_devdata(file);
1480 v4l2_std_id *p = arg;
1481
1482 /*
1a2c6866
HV
1483 * If no signal is detected, then the driver should return
1484 * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
1485 * any standards that do not apply removed.
1486 *
4b1e2e4d
HV
1487 * This means that tuners, audio and video decoders can join
1488 * their efforts to improve the standards detection.
1489 */
1490 *p = vfd->tvnorms;
1491 return ops->vidioc_querystd(file, fh, arg);
1492}
1493
1494static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1495 struct file *file, void *fh, void *arg)
1496{
1497 struct video_device *vfd = video_devdata(file);
1498 struct v4l2_hw_freq_seek *p = arg;
1499 enum v4l2_tuner_type type;
1500
84099a28
AP
1501 /* s_hw_freq_seek is not supported for SDR for now */
1502 if (vfd->vfl_type == VFL_TYPE_SDR)
1503 return -EINVAL;
1504
4b1e2e4d
HV
1505 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1506 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1507 if (p->type != type)
1508 return -EINVAL;
1509 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1510}
1511
737c097b
HV
1512static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
1513 struct file *file, void *fh, void *arg)
1514{
1515 return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
1516}
1517
eb3728ed
HV
1518static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1519 struct file *file, void *fh, void *arg)
1520{
1521 struct v4l2_requestbuffers *p = arg;
4b20259f 1522 int ret = check_fmt(file, p->type);
eb3728ed
HV
1523
1524 if (ret)
1525 return ret;
1526
633c98e5 1527 CLEAR_AFTER_FIELD(p, memory);
eb3728ed
HV
1528
1529 return ops->vidioc_reqbufs(file, fh, p);
1530}
1531
1532static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1533 struct file *file, void *fh, void *arg)
1534{
1535 struct v4l2_buffer *p = arg;
4b20259f 1536 int ret = check_fmt(file, p->type);
eb3728ed
HV
1537
1538 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1539}
1540
1541static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1542 struct file *file, void *fh, void *arg)
1543{
1544 struct v4l2_buffer *p = arg;
4b20259f 1545 int ret = check_fmt(file, p->type);
eb3728ed
HV
1546
1547 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1548}
1549
1550static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1551 struct file *file, void *fh, void *arg)
1552{
1553 struct v4l2_buffer *p = arg;
4b20259f 1554 int ret = check_fmt(file, p->type);
eb3728ed
HV
1555
1556 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1557}
1558
1559static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1560 struct file *file, void *fh, void *arg)
1561{
1562 struct v4l2_create_buffers *create = arg;
4b20259f 1563 int ret = check_fmt(file, create->format.type);
eb3728ed 1564
d52e2381
LP
1565 if (ret)
1566 return ret;
1567
1568 v4l_sanitize_format(&create->format);
1569
1570 ret = ops->vidioc_create_bufs(file, fh, create);
1571
1572 if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1573 create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1574 create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1575
1576 return ret;
eb3728ed
HV
1577}
1578
1579static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1580 struct file *file, void *fh, void *arg)
1581{
1582 struct v4l2_buffer *b = arg;
4b20259f 1583 int ret = check_fmt(file, b->type);
eb3728ed
HV
1584
1585 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1586}
1587
1588static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1589 struct file *file, void *fh, void *arg)
1590{
eb3728ed
HV
1591 struct v4l2_streamparm *p = arg;
1592 v4l2_std_id std;
4b20259f 1593 int ret = check_fmt(file, p->type);
eb3728ed
HV
1594
1595 if (ret)
1596 return ret;
1597 if (ops->vidioc_g_parm)
1598 return ops->vidioc_g_parm(file, fh, p);
eb3728ed
HV
1599 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1600 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1601 return -EINVAL;
1602 p->parm.capture.readbuffers = 2;
ca371575 1603 ret = ops->vidioc_g_std(file, fh, &std);
eb3728ed 1604 if (ret == 0)
ca371575 1605 v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
eb3728ed
HV
1606 return ret;
1607}
1608
1609static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1610 struct file *file, void *fh, void *arg)
1611{
1612 struct v4l2_streamparm *p = arg;
4b20259f 1613 int ret = check_fmt(file, p->type);
eb3728ed
HV
1614
1615 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1616}
1617
efbceecd
HV
1618static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1619 struct file *file, void *fh, void *arg)
1620{
1621 struct video_device *vfd = video_devdata(file);
1622 struct v4l2_queryctrl *p = arg;
7a3ed2d9
HV
1623 struct v4l2_fh *vfh =
1624 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
efbceecd
HV
1625
1626 if (vfh && vfh->ctrl_handler)
1627 return v4l2_queryctrl(vfh->ctrl_handler, p);
1628 if (vfd->ctrl_handler)
1629 return v4l2_queryctrl(vfd->ctrl_handler, p);
1630 if (ops->vidioc_queryctrl)
1631 return ops->vidioc_queryctrl(file, fh, p);
1632 return -ENOTTY;
1633}
1634
e6bee368
HV
1635static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
1636 struct file *file, void *fh, void *arg)
1637{
1638 struct video_device *vfd = video_devdata(file);
1639 struct v4l2_query_ext_ctrl *p = arg;
1640 struct v4l2_fh *vfh =
1641 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1642
1643 if (vfh && vfh->ctrl_handler)
1644 return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
1645 if (vfd->ctrl_handler)
1646 return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
1647 if (ops->vidioc_query_ext_ctrl)
1648 return ops->vidioc_query_ext_ctrl(file, fh, p);
1649 return -ENOTTY;
1650}
1651
efbceecd
HV
1652static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1653 struct file *file, void *fh, void *arg)
1654{
1655 struct video_device *vfd = video_devdata(file);
1656 struct v4l2_querymenu *p = arg;
7a3ed2d9
HV
1657 struct v4l2_fh *vfh =
1658 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
efbceecd
HV
1659
1660 if (vfh && vfh->ctrl_handler)
1661 return v4l2_querymenu(vfh->ctrl_handler, p);
1662 if (vfd->ctrl_handler)
1663 return v4l2_querymenu(vfd->ctrl_handler, p);
1664 if (ops->vidioc_querymenu)
1665 return ops->vidioc_querymenu(file, fh, p);
1666 return -ENOTTY;
1667}
1668
1669static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1670 struct file *file, void *fh, void *arg)
1671{
1672 struct video_device *vfd = video_devdata(file);
1673 struct v4l2_control *p = arg;
7a3ed2d9
HV
1674 struct v4l2_fh *vfh =
1675 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
efbceecd
HV
1676 struct v4l2_ext_controls ctrls;
1677 struct v4l2_ext_control ctrl;
1678
1679 if (vfh && vfh->ctrl_handler)
1680 return v4l2_g_ctrl(vfh->ctrl_handler, p);
1681 if (vfd->ctrl_handler)
1682 return v4l2_g_ctrl(vfd->ctrl_handler, p);
1683 if (ops->vidioc_g_ctrl)
1684 return ops->vidioc_g_ctrl(file, fh, p);
1685 if (ops->vidioc_g_ext_ctrls == NULL)
1686 return -ENOTTY;
1687
1688 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1689 ctrls.count = 1;
1690 ctrls.controls = &ctrl;
1691 ctrl.id = p->id;
1692 ctrl.value = p->value;
1693 if (check_ext_ctrls(&ctrls, 1)) {
1694 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1695
1696 if (ret == 0)
1697 p->value = ctrl.value;
1698 return ret;
1699 }
1700 return -EINVAL;
1701}
1702
1703static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1704 struct file *file, void *fh, void *arg)
1705{
1706 struct video_device *vfd = video_devdata(file);
1707 struct v4l2_control *p = arg;
7a3ed2d9
HV
1708 struct v4l2_fh *vfh =
1709 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
efbceecd
HV
1710 struct v4l2_ext_controls ctrls;
1711 struct v4l2_ext_control ctrl;
1712
1713 if (vfh && vfh->ctrl_handler)
1714 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1715 if (vfd->ctrl_handler)
1716 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1717 if (ops->vidioc_s_ctrl)
1718 return ops->vidioc_s_ctrl(file, fh, p);
1719 if (ops->vidioc_s_ext_ctrls == NULL)
1720 return -ENOTTY;
1721
1722 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1723 ctrls.count = 1;
1724 ctrls.controls = &ctrl;
1725 ctrl.id = p->id;
1726 ctrl.value = p->value;
1727 if (check_ext_ctrls(&ctrls, 1))
1728 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1729 return -EINVAL;
1730}
1731
1732static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1733 struct file *file, void *fh, void *arg)
1734{
1735 struct video_device *vfd = video_devdata(file);
1736 struct v4l2_ext_controls *p = arg;
7a3ed2d9
HV
1737 struct v4l2_fh *vfh =
1738 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
efbceecd
HV
1739
1740 p->error_idx = p->count;
1741 if (vfh && vfh->ctrl_handler)
1742 return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1743 if (vfd->ctrl_handler)
1744 return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1745 if (ops->vidioc_g_ext_ctrls == NULL)
1746 return -ENOTTY;
1747 return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1748 -EINVAL;
1749}
1750
1751static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1752 struct file *file, void *fh, void *arg)
1753{
1754 struct video_device *vfd = video_devdata(file);
1755 struct v4l2_ext_controls *p = arg;
7a3ed2d9
HV
1756 struct v4l2_fh *vfh =
1757 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
efbceecd
HV
1758
1759 p->error_idx = p->count;
1760 if (vfh && vfh->ctrl_handler)
1761 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1762 if (vfd->ctrl_handler)
1763 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1764 if (ops->vidioc_s_ext_ctrls == NULL)
1765 return -ENOTTY;
1766 return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1767 -EINVAL;
1768}
1769
1770static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1771 struct file *file, void *fh, void *arg)
1772{
1773 struct video_device *vfd = video_devdata(file);
1774 struct v4l2_ext_controls *p = arg;
7a3ed2d9
HV
1775 struct v4l2_fh *vfh =
1776 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
efbceecd
HV
1777
1778 p->error_idx = p->count;
1779 if (vfh && vfh->ctrl_handler)
1780 return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1781 if (vfd->ctrl_handler)
1782 return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1783 if (ops->vidioc_try_ext_ctrls == NULL)
1784 return -ENOTTY;
1785 return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1786 -EINVAL;
1787}
1788
d84f2d94
HV
1789static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1790 struct file *file, void *fh, void *arg)
1791{
1792 struct v4l2_crop *p = arg;
1793 struct v4l2_selection s = {
1794 .type = p->type,
1795 };
1796 int ret;
1797
1798 if (ops->vidioc_g_crop)
1799 return ops->vidioc_g_crop(file, fh, p);
1800 /* simulate capture crop using selection api */
1801
1802 /* crop means compose for output devices */
1803 if (V4L2_TYPE_IS_OUTPUT(p->type))
1804 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1805 else
1806 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1807
1808 ret = ops->vidioc_g_selection(file, fh, &s);
1809
1810 /* copying results to old structure on success */
1811 if (!ret)
1812 p->c = s.r;
1813 return ret;
1814}
1815
1816static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1817 struct file *file, void *fh, void *arg)
1818{
1819 struct v4l2_crop *p = arg;
1820 struct v4l2_selection s = {
1821 .type = p->type,
1822 .r = p->c,
1823 };
1824
1825 if (ops->vidioc_s_crop)
1826 return ops->vidioc_s_crop(file, fh, p);
1827 /* simulate capture crop using selection api */
1828
1829 /* crop means compose for output devices */
1830 if (V4L2_TYPE_IS_OUTPUT(p->type))
1831 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1832 else
1833 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1834
1835 return ops->vidioc_s_selection(file, fh, &s);
1836}
1837
1838static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1839 struct file *file, void *fh, void *arg)
1840{
1841 struct v4l2_cropcap *p = arg;
1842 struct v4l2_selection s = { .type = p->type };
1843 int ret;
1844
1845 if (ops->vidioc_cropcap)
1846 return ops->vidioc_cropcap(file, fh, p);
1847
1848 /* obtaining bounds */
1849 if (V4L2_TYPE_IS_OUTPUT(p->type))
1850 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1851 else
1852 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1853
1854 ret = ops->vidioc_g_selection(file, fh, &s);
1855 if (ret)
1856 return ret;
1857 p->bounds = s.r;
1858
1859 /* obtaining defrect */
1860 if (V4L2_TYPE_IS_OUTPUT(p->type))
1861 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1862 else
1863 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1864
1865 ret = ops->vidioc_g_selection(file, fh, &s);
1866 if (ret)
1867 return ret;
1868 p->defrect = s.r;
1869
1870 /* setting trivial pixelaspect */
1871 p->pixelaspect.numerator = 1;
1872 p->pixelaspect.denominator = 1;
1873 return 0;
1874}
1875
f16f77b0
HV
1876static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1877 struct file *file, void *fh, void *arg)
1878{
1879 struct video_device *vfd = video_devdata(file);
1880 int ret;
1881
1882 if (vfd->v4l2_dev)
1883 pr_info("%s: ================= START STATUS =================\n",
1884 vfd->v4l2_dev->name);
1885 ret = ops->vidioc_log_status(file, fh);
1886 if (vfd->v4l2_dev)
1887 pr_info("%s: ================== END STATUS ==================\n",
1888 vfd->v4l2_dev->name);
1889 return ret;
1890}
1891
1892static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1893 struct file *file, void *fh, void *arg)
1894{
1895#ifdef CONFIG_VIDEO_ADV_DEBUG
1896 struct v4l2_dbg_register *p = arg;
79b0c640
HV
1897 struct video_device *vfd = video_devdata(file);
1898 struct v4l2_subdev *sd;
1899 int idx = 0;
f16f77b0
HV
1900
1901 if (!capable(CAP_SYS_ADMIN))
1902 return -EPERM;
3eef2510 1903 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
79b0c640
HV
1904 if (vfd->v4l2_dev == NULL)
1905 return -EINVAL;
3eef2510
HV
1906 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1907 if (p->match.addr == idx++)
79b0c640 1908 return v4l2_subdev_call(sd, core, g_register, p);
79b0c640
HV
1909 return -EINVAL;
1910 }
191b79b0
HV
1911 if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1912 (ops->vidioc_g_chip_info || p->match.addr == 0))
79b0c640
HV
1913 return ops->vidioc_g_register(file, fh, p);
1914 return -EINVAL;
f16f77b0
HV
1915#else
1916 return -ENOTTY;
1917#endif
1918}
1919
1920static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1921 struct file *file, void *fh, void *arg)
1922{
1923#ifdef CONFIG_VIDEO_ADV_DEBUG
977ba3b1 1924 const struct v4l2_dbg_register *p = arg;
79b0c640
HV
1925 struct video_device *vfd = video_devdata(file);
1926 struct v4l2_subdev *sd;
1927 int idx = 0;
f16f77b0
HV
1928
1929 if (!capable(CAP_SYS_ADMIN))
1930 return -EPERM;
3eef2510 1931 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
79b0c640
HV
1932 if (vfd->v4l2_dev == NULL)
1933 return -EINVAL;
3eef2510
HV
1934 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1935 if (p->match.addr == idx++)
79b0c640 1936 return v4l2_subdev_call(sd, core, s_register, p);
79b0c640
HV
1937 return -EINVAL;
1938 }
191b79b0
HV
1939 if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1940 (ops->vidioc_g_chip_info || p->match.addr == 0))
79b0c640
HV
1941 return ops->vidioc_s_register(file, fh, p);
1942 return -EINVAL;
f16f77b0
HV
1943#else
1944 return -ENOTTY;
1945#endif
1946}
1947
96b03d2a 1948static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
79b0c640
HV
1949 struct file *file, void *fh, void *arg)
1950{
cd634f1b 1951#ifdef CONFIG_VIDEO_ADV_DEBUG
79b0c640 1952 struct video_device *vfd = video_devdata(file);
96b03d2a 1953 struct v4l2_dbg_chip_info *p = arg;
79b0c640
HV
1954 struct v4l2_subdev *sd;
1955 int idx = 0;
1956
1957 switch (p->match.type) {
1958 case V4L2_CHIP_MATCH_BRIDGE:
79b0c640
HV
1959 if (ops->vidioc_s_register)
1960 p->flags |= V4L2_CHIP_FL_WRITABLE;
1961 if (ops->vidioc_g_register)
1962 p->flags |= V4L2_CHIP_FL_READABLE;
1c1d86a1 1963 strlcpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
96b03d2a
HV
1964 if (ops->vidioc_g_chip_info)
1965 return ops->vidioc_g_chip_info(file, fh, arg);
0f0fe4b9
HV
1966 if (p->match.addr)
1967 return -EINVAL;
79b0c640
HV
1968 return 0;
1969
3eef2510 1970 case V4L2_CHIP_MATCH_SUBDEV:
79b0c640
HV
1971 if (vfd->v4l2_dev == NULL)
1972 break;
1973 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
3eef2510
HV
1974 if (p->match.addr != idx++)
1975 continue;
1976 if (sd->ops->core && sd->ops->core->s_register)
1977 p->flags |= V4L2_CHIP_FL_WRITABLE;
1978 if (sd->ops->core && sd->ops->core->g_register)
1979 p->flags |= V4L2_CHIP_FL_READABLE;
1980 strlcpy(p->name, sd->name, sizeof(p->name));
1981 return 0;
79b0c640
HV
1982 }
1983 break;
1984 }
1985 return -EINVAL;
cd634f1b
HV
1986#else
1987 return -ENOTTY;
1988#endif
79b0c640
HV
1989}
1990
458aa4a6
HV
1991static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
1992 struct file *file, void *fh, void *arg)
1993{
1994 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
1995}
1996
1997static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
1998 struct file *file, void *fh, void *arg)
1999{
2000 return ops->vidioc_subscribe_event(fh, arg);
2001}
2002
2003static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
2004 struct file *file, void *fh, void *arg)
2005{
2006 return ops->vidioc_unsubscribe_event(fh, arg);
2007}
2008
2009static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
2010 struct file *file, void *fh, void *arg)
2011{
2012 struct v4l2_sliced_vbi_cap *p = arg;
4b20259f
HV
2013 int ret = check_fmt(file, p->type);
2014
2015 if (ret)
2016 return ret;
458aa4a6
HV
2017
2018 /* Clear up to type, everything after type is zeroed already */
2019 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
2020
2021 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
2022}
2023
82b655bf
HV
2024static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
2025 struct file *file, void *fh, void *arg)
2026{
2027 struct video_device *vfd = video_devdata(file);
2028 struct v4l2_frequency_band *p = arg;
2029 enum v4l2_tuner_type type;
2030 int err;
2031
84099a28
AP
2032 if (vfd->vfl_type == VFL_TYPE_SDR) {
2033 if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
2034 return -EINVAL;
2035 type = p->type;
2036 } else {
2037 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2038 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2039 if (type != p->type)
2040 return -EINVAL;
2041 }
82b655bf
HV
2042 if (ops->vidioc_enum_freq_bands)
2043 return ops->vidioc_enum_freq_bands(file, fh, p);
73f35418 2044 if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
82b655bf
HV
2045 struct v4l2_tuner t = {
2046 .index = p->tuner,
2047 .type = type,
2048 };
2049
aa4d9b53
HV
2050 if (p->index)
2051 return -EINVAL;
82b655bf
HV
2052 err = ops->vidioc_g_tuner(file, fh, &t);
2053 if (err)
2054 return err;
2055 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2056 p->rangelow = t.rangelow;
2057 p->rangehigh = t.rangehigh;
2058 p->modulation = (type == V4L2_TUNER_RADIO) ?
2059 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2060 return 0;
2061 }
73f35418 2062 if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
82b655bf
HV
2063 struct v4l2_modulator m = {
2064 .index = p->tuner,
2065 };
2066
2067 if (type != V4L2_TUNER_RADIO)
2068 return -EINVAL;
aa4d9b53
HV
2069 if (p->index)
2070 return -EINVAL;
82b655bf
HV
2071 err = ops->vidioc_g_modulator(file, fh, &m);
2072 if (err)
2073 return err;
2074 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2075 p->rangelow = m.rangelow;
2076 p->rangehigh = m.rangehigh;
2077 p->modulation = (type == V4L2_TUNER_RADIO) ?
2078 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2079 return 0;
2080 }
2081 return -ENOTTY;
2082}
2083
4839e6c2
HV
2084struct v4l2_ioctl_info {
2085 unsigned int ioctl;
27cd2aba 2086 u32 flags;
4839e6c2 2087 const char * const name;
86748f35
HV
2088 union {
2089 u32 offset;
2090 int (*func)(const struct v4l2_ioctl_ops *ops,
2091 struct file *file, void *fh, void *p);
26ddcbcc 2092 } u;
86748f35 2093 void (*debug)(const void *arg, bool write_only);
4839e6c2
HV
2094};
2095
2096/* This control needs a priority check */
2097#define INFO_FL_PRIO (1 << 0)
2098/* This control can be valid if the filehandle passes a control handler. */
2099#define INFO_FL_CTRL (1 << 1)
86748f35
HV
2100/* This is a standard ioctl, no need for special code */
2101#define INFO_FL_STD (1 << 2)
2102/* This is ioctl has its own function */
2103#define INFO_FL_FUNC (1 << 3)
5a5adf6b
HV
2104/* Queuing ioctl */
2105#define INFO_FL_QUEUE (1 << 4)
27cd2aba
HV
2106/* Zero struct from after the field to the end */
2107#define INFO_FL_CLEAR(v4l2_struct, field) \
2108 ((offsetof(struct v4l2_struct, field) + \
2109 sizeof(((struct v4l2_struct *)0)->field)) << 16)
2110#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
4839e6c2 2111
86748f35
HV
2112#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
2113 [_IOC_NR(_ioctl)] = { \
2114 .ioctl = _ioctl, \
2115 .flags = _flags | INFO_FL_STD, \
2116 .name = #_ioctl, \
26ddcbcc 2117 .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
86748f35
HV
2118 .debug = _debug, \
2119 }
2120
2121#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
2122 [_IOC_NR(_ioctl)] = { \
2123 .ioctl = _ioctl, \
2124 .flags = _flags | INFO_FL_FUNC, \
2125 .name = #_ioctl, \
26ddcbcc 2126 .u.func = _func, \
86748f35
HV
2127 .debug = _debug, \
2128 }
2129
4839e6c2 2130static struct v4l2_ioctl_info v4l2_ioctls[] = {
59076122 2131 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
be6c64e4
HV
2132 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
2133 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
2134 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
5a5adf6b
HV
2135 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2136 IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
be6c64e4
HV
2137 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
2138 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
737c097b 2139 IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
5a5adf6b 2140 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
b799d09a 2141 IOCTL_INFO_STD(VIDIOC_EXPBUF, vidioc_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
5a5adf6b
HV
2142 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2143 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2144 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
eb3728ed
HV
2145 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2146 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
ca371575 2147 IOCTL_INFO_STD(VIDIOC_G_STD, vidioc_g_std, v4l_print_std, 0),
4b1e2e4d
HV
2148 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2149 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
59076122 2150 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
efbceecd
HV
2151 IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2152 IOCTL_INFO_FNC(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
4b1e2e4d
HV
2153 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2154 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
59076122
HV
2155 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
2156 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
efbceecd
HV
2157 IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2158 IOCTL_INFO_FNC(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
59076122
HV
2159 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
2160 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
dd519bb3
HV
2161 IOCTL_INFO_STD(VIDIOC_G_EDID, vidioc_g_edid, v4l_print_edid, INFO_FL_CLEAR(v4l2_edid, edid)),
2162 IOCTL_INFO_STD(VIDIOC_S_EDID, vidioc_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_edid, edid)),
59076122
HV
2163 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
2164 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2165 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2166 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
2167 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
82b655bf 2168 IOCTL_INFO_FNC(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
4b1e2e4d
HV
2169 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2170 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2171 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
d84f2d94
HV
2172 IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2173 IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2174 IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2175 IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
2176 IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
d806f2df
HV
2177 IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
2178 IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
4b1e2e4d 2179 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
be6c64e4 2180 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
59076122
HV
2181 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2182 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
d0547cba
HV
2183 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2184 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
458aa4a6 2185 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)),
f16f77b0 2186 IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
efbceecd
HV
2187 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2188 IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
9bc31633 2189 IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
458aa4a6
HV
2190 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2191 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
d806f2df
HV
2192 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
2193 IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2194 IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2195 IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2196 IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
f16f77b0
HV
2197 IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2198 IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
4b1e2e4d 2199 IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
efc626b0
HV
2200 IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
2201 IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
458aa4a6
HV
2202 IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2203 IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2204 IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
5a5adf6b
HV
2205 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2206 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
efc626b0
HV
2207 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
2208 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
a1acb8f9 2209 IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)),
82b655bf 2210 IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
96b03d2a 2211 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)),
e6bee368 2212 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)),
4839e6c2
HV
2213};
2214#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2215
2216bool v4l2_is_known_ioctl(unsigned int cmd)
2217{
2218 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2219 return false;
2220 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2221}
2222
5a5adf6b
HV
2223struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
2224{
2225 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2226 return vdev->lock;
2227 if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
2228 return NULL;
2229 if (vdev->queue && vdev->queue->lock &&
2230 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2231 return vdev->queue->lock;
2232 return vdev->lock;
2233}
2234
4839e6c2
HV
2235/* Common ioctl debug function. This function can be used by
2236 external ioctl messages as well as internal V4L ioctl */
4a085168 2237void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
4839e6c2 2238{
86748f35 2239 const char *dir, *type;
4839e6c2 2240
4a085168
HV
2241 if (prefix)
2242 printk(KERN_DEBUG "%s: ", prefix);
2243
4839e6c2
HV
2244 switch (_IOC_TYPE(cmd)) {
2245 case 'd':
2246 type = "v4l2_int";
2247 break;
2248 case 'V':
2249 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2250 type = "v4l2";
2251 break;
2252 }
86748f35 2253 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
4839e6c2
HV
2254 return;
2255 default:
2256 type = "unknown";
86748f35 2257 break;
4839e6c2
HV
2258 }
2259
2260 switch (_IOC_DIR(cmd)) {
2261 case _IOC_NONE: dir = "--"; break;
2262 case _IOC_READ: dir = "r-"; break;
2263 case _IOC_WRITE: dir = "-w"; break;
2264 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2265 default: dir = "*ERR*"; break;
2266 }
86748f35 2267 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
4839e6c2
HV
2268 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2269}
2270EXPORT_SYMBOL(v4l_printk_ioctl);
2271
069b7479 2272static long __video_do_ioctl(struct file *file,
35ea11ff
HV
2273 unsigned int cmd, void *arg)
2274{
2275 struct video_device *vfd = video_devdata(file);
a399810c 2276 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
86748f35
HV
2277 bool write_only = false;
2278 struct v4l2_ioctl_info default_info;
2279 const struct v4l2_ioctl_info *info;
d5fbf32f 2280 void *fh = file->private_data;
99cd47bc 2281 struct v4l2_fh *vfh = NULL;
80131fe0 2282 int debug = vfd->debug;
9190d191 2283 long ret = -ENOTTY;
35ea11ff 2284
6a717883 2285 if (ops == NULL) {
4a085168
HV
2286 pr_warn("%s: has no ioctl_ops.\n",
2287 video_device_node_name(vfd));
9190d191 2288 return ret;
6a717883
HV
2289 }
2290
b7284bb0 2291 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
99cd47bc
HV
2292 vfh = file->private_data;
2293
48ea0be0 2294 if (v4l2_is_known_ioctl(cmd)) {
86748f35 2295 info = &v4l2_ioctls[_IOC_NR(cmd)];
48ea0be0
HV
2296
2297 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2298 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
86748f35 2299 goto done;
4b902fec 2300
b7284bb0 2301 if (vfh && (info->flags & INFO_FL_PRIO)) {
4b902fec
HV
2302 ret = v4l2_prio_check(vfd->prio, vfh->prio);
2303 if (ret)
86748f35 2304 goto done;
4b902fec 2305 }
86748f35
HV
2306 } else {
2307 default_info.ioctl = cmd;
2308 default_info.flags = 0;
f18d8e07 2309 default_info.debug = v4l_print_default;
86748f35 2310 info = &default_info;
48ea0be0
HV
2311 }
2312
86748f35 2313 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
86748f35
HV
2314 if (info->flags & INFO_FL_STD) {
2315 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2316 const void *p = vfd->ioctl_ops;
26ddcbcc 2317 const vidioc_op *vidioc = p + info->u.offset;
86748f35
HV
2318
2319 ret = (*vidioc)(file, fh, arg);
86748f35 2320 } else if (info->flags & INFO_FL_FUNC) {
26ddcbcc 2321 ret = info->u.func(ops, file, fh, arg);
f18d8e07
HV
2322 } else if (!ops->vidioc_default) {
2323 ret = -ENOTTY;
2324 } else {
2325 ret = ops->vidioc_default(file, fh,
b7284bb0 2326 vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
f18d8e07 2327 cmd, arg);
48ea0be0 2328 }
99cd47bc 2329
86748f35 2330done:
80131fe0 2331 if (debug) {
4a085168 2332 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
86748f35 2333 if (ret < 0)
505d04bd
HV
2334 pr_cont(": error %ld", ret);
2335 if (debug == V4L2_DEBUG_IOCTL)
86748f35 2336 pr_cont("\n");
86748f35
HV
2337 else if (_IOC_DIR(cmd) == _IOC_NONE)
2338 info->debug(arg, write_only);
2339 else {
2340 pr_cont(": ");
2341 info->debug(arg, write_only);
35ea11ff
HV
2342 }
2343 }
2344
2345 return ret;
2346}
2347
d14e6d76 2348static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
ba2d35c1 2349 void __user **user_ptr, void ***kernel_ptr)
d14e6d76
PO
2350{
2351 int ret = 0;
2352
2353 switch (cmd) {
96b1a702 2354 case VIDIOC_PREPARE_BUF:
d14e6d76
PO
2355 case VIDIOC_QUERYBUF:
2356 case VIDIOC_QBUF:
2357 case VIDIOC_DQBUF: {
2358 struct v4l2_buffer *buf = parg;
2359
2360 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2361 if (buf->length > VIDEO_MAX_PLANES) {
2362 ret = -EINVAL;
2363 break;
2364 }
2365 *user_ptr = (void __user *)buf->m.planes;
ba2d35c1 2366 *kernel_ptr = (void **)&buf->m.planes;
d14e6d76
PO
2367 *array_size = sizeof(struct v4l2_plane) * buf->length;
2368 ret = 1;
2369 }
2370 break;
2371 }
2372
dd519bb3
HV
2373 case VIDIOC_G_EDID:
2374 case VIDIOC_S_EDID: {
2375 struct v4l2_edid *edid = parg;
ed45ce2c
HV
2376
2377 if (edid->blocks) {
1b8b10cc
HV
2378 if (edid->blocks > 256) {
2379 ret = -EINVAL;
2380 break;
2381 }
ed45ce2c 2382 *user_ptr = (void __user *)edid->edid;
ba2d35c1 2383 *kernel_ptr = (void **)&edid->edid;
ed45ce2c
HV
2384 *array_size = edid->blocks * 128;
2385 ret = 1;
2386 }
2387 break;
2388 }
2389
d14e6d76
PO
2390 case VIDIOC_S_EXT_CTRLS:
2391 case VIDIOC_G_EXT_CTRLS:
2392 case VIDIOC_TRY_EXT_CTRLS: {
2393 struct v4l2_ext_controls *ctrls = parg;
2394
2395 if (ctrls->count != 0) {
6c06108b
DC
2396 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2397 ret = -EINVAL;
2398 break;
2399 }
d14e6d76 2400 *user_ptr = (void __user *)ctrls->controls;
ba2d35c1 2401 *kernel_ptr = (void **)&ctrls->controls;
d14e6d76
PO
2402 *array_size = sizeof(struct v4l2_ext_control)
2403 * ctrls->count;
2404 ret = 1;
2405 }
2406 break;
2407 }
2408 }
2409
2410 return ret;
2411}
2412
fc0a8079
LP
2413long
2414video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2415 v4l2_kioctl func)
35ea11ff
HV
2416{
2417 char sbuf[128];
2418 void *mbuf = NULL;
1d94aa36 2419 void *parg = (void *)arg;
069b7479 2420 long err = -EINVAL;
d14e6d76
PO
2421 bool has_array_args;
2422 size_t array_size = 0;
35ea11ff 2423 void __user *user_ptr = NULL;
d14e6d76 2424 void **kernel_ptr = NULL;
35ea11ff 2425
35ea11ff 2426 /* Copy arguments into temp kernel buffer */
337f9d20 2427 if (_IOC_DIR(cmd) != _IOC_NONE) {
35ea11ff
HV
2428 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2429 parg = sbuf;
2430 } else {
2431 /* too big to allocate from stack */
2432 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2433 if (NULL == mbuf)
2434 return -ENOMEM;
2435 parg = mbuf;
2436 }
2437
2438 err = -EFAULT;
19c96e4b 2439 if (_IOC_DIR(cmd) & _IOC_WRITE) {
27cd2aba
HV
2440 unsigned int n = _IOC_SIZE(cmd);
2441
2442 /*
2443 * In some cases, only a few fields are used as input,
2444 * i.e. when the app sets "index" and then the driver
2445 * fills in the rest of the structure for the thing
2446 * with that index. We only need to copy up the first
2447 * non-input field.
2448 */
2449 if (v4l2_is_known_ioctl(cmd)) {
2450 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2451 if (flags & INFO_FL_CLEAR_MASK)
2452 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2453 }
19c96e4b
TP
2454
2455 if (copy_from_user(parg, (void __user *)arg, n))
35ea11ff 2456 goto out;
19c96e4b
TP
2457
2458 /* zero out anything we don't copy from userspace */
2459 if (n < _IOC_SIZE(cmd))
2460 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
337f9d20
TP
2461 } else {
2462 /* read-only ioctl */
2463 memset(parg, 0, _IOC_SIZE(cmd));
19c96e4b 2464 }
35ea11ff
HV
2465 }
2466
d14e6d76
PO
2467 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2468 if (err < 0)
2469 goto out;
2470 has_array_args = err;
35ea11ff 2471
d14e6d76
PO
2472 if (has_array_args) {
2473 /*
2474 * When adding new types of array args, make sure that the
2475 * parent argument to ioctl (which contains the pointer to the
2476 * array) fits into sbuf (so that mbuf will still remain
2477 * unused up to here).
2478 */
2479 mbuf = kmalloc(array_size, GFP_KERNEL);
2480 err = -ENOMEM;
2481 if (NULL == mbuf)
2482 goto out_array_args;
2483 err = -EFAULT;
2484 if (copy_from_user(mbuf, user_ptr, array_size))
2485 goto out_array_args;
2486 *kernel_ptr = mbuf;
35ea11ff
HV
2487 }
2488
2489 /* Handles IOCTL */
fc0a8079 2490 err = func(file, cmd, parg);
35ea11ff 2491 if (err == -ENOIOCTLCMD)
02bbb814 2492 err = -ENOTTY;
aa32f4c0
HV
2493 if (err == 0) {
2494 if (cmd == VIDIOC_DQBUF)
2495 trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
2496 else if (cmd == VIDIOC_QBUF)
2497 trace_v4l2_qbuf(video_devdata(file)->minor, parg);
2498 }
35ea11ff 2499
d14e6d76 2500 if (has_array_args) {
ba2d35c1 2501 *kernel_ptr = (void __force *)user_ptr;
d14e6d76 2502 if (copy_to_user(user_ptr, mbuf, array_size))
35ea11ff 2503 err = -EFAULT;
d14e6d76 2504 goto out_array_args;
35ea11ff 2505 }
5d7758ee
HV
2506 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2507 results that must be returned. */
2508 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
35ea11ff
HV
2509 goto out;
2510
d14e6d76 2511out_array_args:
35ea11ff
HV
2512 /* Copy results into user buffer */
2513 switch (_IOC_DIR(cmd)) {
2514 case _IOC_READ:
2515 case (_IOC_WRITE | _IOC_READ):
2516 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2517 err = -EFAULT;
2518 break;
2519 }
2520
2521out:
2522 kfree(mbuf);
2523 return err;
2524}
fc0a8079
LP
2525EXPORT_SYMBOL(video_usercopy);
2526
2527long video_ioctl2(struct file *file,
2528 unsigned int cmd, unsigned long arg)
2529{
2530 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2531}
8a522c91 2532EXPORT_SYMBOL(video_ioctl2);
This page took 0.928994 seconds and 5 git commands to generate.