[media] imon: fix sparse warnings
[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, "
c96fd46a
LP
259 "bytesperline=%u, sizeimage=%u, colorspace=%d, "
260 "flags %u\n",
be6c64e4
HV
261 pix->width, pix->height,
262 (pix->pixelformat & 0xff),
263 (pix->pixelformat >> 8) & 0xff,
264 (pix->pixelformat >> 16) & 0xff,
265 (pix->pixelformat >> 24) & 0xff,
266 prt_names(pix->field, v4l2_field_names),
267 pix->bytesperline, pix->sizeimage,
c96fd46a 268 pix->colorspace, pix->flags);
be6c64e4
HV
269 break;
270 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
271 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
272 mp = &p->fmt.pix_mp;
273 pr_cont(", width=%u, height=%u, "
274 "format=%c%c%c%c, field=%s, "
275 "colorspace=%d, num_planes=%u\n",
276 mp->width, mp->height,
277 (mp->pixelformat & 0xff),
278 (mp->pixelformat >> 8) & 0xff,
279 (mp->pixelformat >> 16) & 0xff,
280 (mp->pixelformat >> 24) & 0xff,
281 prt_names(mp->field, v4l2_field_names),
282 mp->colorspace, mp->num_planes);
283 for (i = 0; i < mp->num_planes; i++)
284 printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
285 mp->plane_fmt[i].bytesperline,
286 mp->plane_fmt[i].sizeimage);
287 break;
288 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
289 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
290 win = &p->fmt.win;
560dde24
HV
291 /* Note: we can't print the clip list here since the clips
292 * pointer is a userspace pointer, not a kernelspace
293 * pointer. */
294 pr_cont(", wxh=%dx%d, x,y=%d,%d, field=%s, chromakey=0x%08x, clipcount=%u, clips=%p, bitmap=%p, global_alpha=0x%02x\n",
295 win->w.width, win->w.height, win->w.left, win->w.top,
be6c64e4 296 prt_names(win->field, v4l2_field_names),
560dde24
HV
297 win->chromakey, win->clipcount, win->clips,
298 win->bitmap, win->global_alpha);
be6c64e4
HV
299 break;
300 case V4L2_BUF_TYPE_VBI_CAPTURE:
301 case V4L2_BUF_TYPE_VBI_OUTPUT:
302 vbi = &p->fmt.vbi;
303 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
304 "sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
305 vbi->sampling_rate, vbi->offset,
306 vbi->samples_per_line,
307 (vbi->sample_format & 0xff),
308 (vbi->sample_format >> 8) & 0xff,
309 (vbi->sample_format >> 16) & 0xff,
310 (vbi->sample_format >> 24) & 0xff,
311 vbi->start[0], vbi->start[1],
312 vbi->count[0], vbi->count[1]);
313 break;
314 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
315 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
316 sliced = &p->fmt.sliced;
317 pr_cont(", service_set=0x%08x, io_size=%d\n",
318 sliced->service_set, sliced->io_size);
319 for (i = 0; i < 24; i++)
320 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
321 sliced->service_lines[0][i],
322 sliced->service_lines[1][i]);
323 break;
582c52cb
AP
324 case V4L2_BUF_TYPE_SDR_CAPTURE:
325 sdr = &p->fmt.sdr;
326 pr_cont(", pixelformat=%c%c%c%c\n",
327 (sdr->pixelformat >> 0) & 0xff,
328 (sdr->pixelformat >> 8) & 0xff,
329 (sdr->pixelformat >> 16) & 0xff,
330 (sdr->pixelformat >> 24) & 0xff);
331 break;
be6c64e4
HV
332 }
333}
334
335static void v4l_print_framebuffer(const void *arg, bool write_only)
336{
337 const struct v4l2_framebuffer *p = arg;
338
339 pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
340 "height=%u, pixelformat=%c%c%c%c, "
560dde24 341 "bytesperline=%u, sizeimage=%u, colorspace=%d\n",
be6c64e4
HV
342 p->capability, p->flags, p->base,
343 p->fmt.width, p->fmt.height,
344 (p->fmt.pixelformat & 0xff),
345 (p->fmt.pixelformat >> 8) & 0xff,
346 (p->fmt.pixelformat >> 16) & 0xff,
347 (p->fmt.pixelformat >> 24) & 0xff,
348 p->fmt.bytesperline, p->fmt.sizeimage,
349 p->fmt.colorspace);
350}
351
e325b244
HV
352static void v4l_print_buftype(const void *arg, bool write_only)
353{
354 pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
355}
356
4b1e2e4d
HV
357static void v4l_print_modulator(const void *arg, bool write_only)
358{
359 const struct v4l2_modulator *p = arg;
360
361 if (write_only)
560dde24 362 pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
4b1e2e4d 363 else
27d5a87c 364 pr_cont("index=%u, name=%.*s, capability=0x%x, "
4b1e2e4d 365 "rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
27d5a87c 366 p->index, (int)sizeof(p->name), p->name, p->capability,
4b1e2e4d
HV
367 p->rangelow, p->rangehigh, p->txsubchans);
368}
369
370static void v4l_print_tuner(const void *arg, bool write_only)
371{
372 const struct v4l2_tuner *p = arg;
373
374 if (write_only)
375 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
376 else
27d5a87c 377 pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, "
4b1e2e4d
HV
378 "rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
379 "rxsubchans=0x%x, audmode=%u\n",
27d5a87c 380 p->index, (int)sizeof(p->name), p->name, p->type,
4b1e2e4d
HV
381 p->capability, p->rangelow,
382 p->rangehigh, p->signal, p->afc,
383 p->rxsubchans, p->audmode);
384}
385
386static void v4l_print_frequency(const void *arg, bool write_only)
387{
388 const struct v4l2_frequency *p = arg;
389
390 pr_cont("tuner=%u, type=%u, frequency=%u\n",
391 p->tuner, p->type, p->frequency);
392}
393
394static void v4l_print_standard(const void *arg, bool write_only)
395{
396 const struct v4l2_standard *p = arg;
397
27d5a87c 398 pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, "
4b1e2e4d 399 "framelines=%u\n", p->index,
27d5a87c 400 (unsigned long long)p->id, (int)sizeof(p->name), p->name,
4b1e2e4d
HV
401 p->frameperiod.numerator,
402 p->frameperiod.denominator,
403 p->framelines);
404}
405
406static void v4l_print_std(const void *arg, bool write_only)
407{
408 pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
409}
410
411static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
412{
413 const struct v4l2_hw_freq_seek *p = arg;
414
5e788317
HV
415 pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, "
416 "rangelow=%u, rangehigh=%u\n",
417 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
418 p->rangelow, p->rangehigh);
4b1e2e4d
HV
419}
420
eb3728ed 421static void v4l_print_requestbuffers(const void *arg, bool write_only)
59076122 422{
eb3728ed
HV
423 const struct v4l2_requestbuffers *p = arg;
424
425 pr_cont("count=%d, type=%s, memory=%s\n",
426 p->count,
427 prt_names(p->type, v4l2_type_names),
428 prt_names(p->memory, v4l2_memory_names));
59076122
HV
429}
430
eb3728ed 431static void v4l_print_buffer(const void *arg, bool write_only)
35ea11ff 432{
eb3728ed
HV
433 const struct v4l2_buffer *p = arg;
434 const struct v4l2_timecode *tc = &p->timecode;
435 const struct v4l2_plane *plane;
d14e6d76 436 int i;
35ea11ff 437
eb3728ed
HV
438 pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
439 "flags=0x%08x, field=%s, sequence=%d, memory=%s",
35ea11ff
HV
440 p->timestamp.tv_sec / 3600,
441 (int)(p->timestamp.tv_sec / 60) % 60,
442 (int)(p->timestamp.tv_sec % 60),
b045979d 443 (long)p->timestamp.tv_usec,
35ea11ff
HV
444 p->index,
445 prt_names(p->type, v4l2_type_names),
eb3728ed
HV
446 p->flags, prt_names(p->field, v4l2_field_names),
447 p->sequence, prt_names(p->memory, v4l2_memory_names));
d14e6d76
PO
448
449 if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
eb3728ed 450 pr_cont("\n");
d14e6d76
PO
451 for (i = 0; i < p->length; ++i) {
452 plane = &p->m.planes[i];
eb3728ed 453 printk(KERN_DEBUG
560dde24 454 "plane %d: bytesused=%d, data_offset=0x%08x, "
eb3728ed 455 "offset/userptr=0x%lx, length=%d\n",
d14e6d76
PO
456 i, plane->bytesused, plane->data_offset,
457 plane->m.userptr, plane->length);
458 }
459 } else {
560dde24 460 pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
d14e6d76
PO
461 p->bytesused, p->m.userptr, p->length);
462 }
463
eb3728ed
HV
464 printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
465 "flags=0x%08x, frames=%d, userbits=0x%08x\n",
35ea11ff
HV
466 tc->hours, tc->minutes, tc->seconds,
467 tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
468}
469
b799d09a
TS
470static void v4l_print_exportbuffer(const void *arg, bool write_only)
471{
472 const struct v4l2_exportbuffer *p = arg;
473
474 pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
475 p->fd, prt_names(p->type, v4l2_type_names),
476 p->index, p->plane, p->flags);
477}
478
eb3728ed
HV
479static void v4l_print_create_buffers(const void *arg, bool write_only)
480{
481 const struct v4l2_create_buffers *p = arg;
482
483 pr_cont("index=%d, count=%d, memory=%s, ",
484 p->index, p->count,
485 prt_names(p->memory, v4l2_memory_names));
486 v4l_print_format(&p->format, write_only);
487}
488
489static void v4l_print_streamparm(const void *arg, bool write_only)
490{
491 const struct v4l2_streamparm *p = arg;
492
493 pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
494
495 if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
496 p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
497 const struct v4l2_captureparm *c = &p->parm.capture;
498
499 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
500 "extendedmode=%d, readbuffers=%d\n",
501 c->capability, c->capturemode,
502 c->timeperframe.numerator, c->timeperframe.denominator,
503 c->extendedmode, c->readbuffers);
504 } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
505 p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
506 const struct v4l2_outputparm *c = &p->parm.output;
507
508 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
509 "extendedmode=%d, writebuffers=%d\n",
510 c->capability, c->outputmode,
511 c->timeperframe.numerator, c->timeperframe.denominator,
512 c->extendedmode, c->writebuffers);
560dde24
HV
513 } else {
514 pr_cont("\n");
eb3728ed
HV
515 }
516}
517
efbceecd
HV
518static void v4l_print_queryctrl(const void *arg, bool write_only)
519{
520 const struct v4l2_queryctrl *p = arg;
521
27d5a87c 522 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, "
efbceecd 523 "step=%d, default=%d, flags=0x%08x\n",
27d5a87c 524 p->id, p->type, (int)sizeof(p->name), p->name,
efbceecd
HV
525 p->minimum, p->maximum,
526 p->step, p->default_value, p->flags);
527}
528
e6bee368
HV
529static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
530{
531 const struct v4l2_query_ext_ctrl *p = arg;
532
533 pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%lld/%lld, "
534 "step=%lld, default=%lld, flags=0x%08x, elem_size=%u, elems=%u, "
0176077a 535 "nr_of_dims=%u, dims=%u,%u,%u,%u\n",
e6bee368
HV
536 p->id, p->type, (int)sizeof(p->name), p->name,
537 p->minimum, p->maximum,
538 p->step, p->default_value, p->flags,
539 p->elem_size, p->elems, p->nr_of_dims,
0176077a 540 p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
e6bee368
HV
541}
542
efbceecd
HV
543static void v4l_print_querymenu(const void *arg, bool write_only)
544{
545 const struct v4l2_querymenu *p = arg;
546
547 pr_cont("id=0x%x, index=%d\n", p->id, p->index);
548}
549
550static void v4l_print_control(const void *arg, bool write_only)
551{
552 const struct v4l2_control *p = arg;
553
554 pr_cont("id=0x%x, value=%d\n", p->id, p->value);
555}
556
557static void v4l_print_ext_controls(const void *arg, bool write_only)
558{
559 const struct v4l2_ext_controls *p = arg;
560 int i;
561
562 pr_cont("class=0x%x, count=%d, error_idx=%d",
563 p->ctrl_class, p->count, p->error_idx);
564 for (i = 0; i < p->count; i++) {
565 if (p->controls[i].size)
566 pr_cont(", id/val=0x%x/0x%x",
567 p->controls[i].id, p->controls[i].value);
568 else
569 pr_cont(", id/size=0x%x/%u",
570 p->controls[i].id, p->controls[i].size);
571 }
572 pr_cont("\n");
573}
574
d84f2d94 575static void v4l_print_cropcap(const void *arg, bool write_only)
eb3728ed 576{
d84f2d94
HV
577 const struct v4l2_cropcap *p = arg;
578
579 pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
a112fbaf 580 "defrect wxh=%dx%d, x,y=%d,%d, "
d84f2d94
HV
581 "pixelaspect %d/%d\n",
582 prt_names(p->type, v4l2_type_names),
583 p->bounds.width, p->bounds.height,
584 p->bounds.left, p->bounds.top,
585 p->defrect.width, p->defrect.height,
586 p->defrect.left, p->defrect.top,
587 p->pixelaspect.numerator, p->pixelaspect.denominator);
eb3728ed
HV
588}
589
d84f2d94 590static void v4l_print_crop(const void *arg, bool write_only)
35ea11ff 591{
d84f2d94
HV
592 const struct v4l2_crop *p = arg;
593
594 pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
595 prt_names(p->type, v4l2_type_names),
596 p->c.width, p->c.height,
597 p->c.left, p->c.top);
598}
599
600static void v4l_print_selection(const void *arg, bool write_only)
601{
602 const struct v4l2_selection *p = arg;
603
604 pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
605 prt_names(p->type, v4l2_type_names),
606 p->target, p->flags,
607 p->r.width, p->r.height, p->r.left, p->r.top);
608}
609
d806f2df
HV
610static void v4l_print_jpegcompression(const void *arg, bool write_only)
611{
612 const struct v4l2_jpegcompression *p = arg;
613
614 pr_cont("quality=%d, APPn=%d, APP_len=%d, "
615 "COM_len=%d, jpeg_markers=0x%x\n",
616 p->quality, p->APPn, p->APP_len,
617 p->COM_len, p->jpeg_markers);
618}
619
620static void v4l_print_enc_idx(const void *arg, bool write_only)
621{
622 const struct v4l2_enc_idx *p = arg;
623
624 pr_cont("entries=%d, entries_cap=%d\n",
625 p->entries, p->entries_cap);
626}
627
628static void v4l_print_encoder_cmd(const void *arg, bool write_only)
629{
630 const struct v4l2_encoder_cmd *p = arg;
631
632 pr_cont("cmd=%d, flags=0x%x\n",
633 p->cmd, p->flags);
634}
635
636static void v4l_print_decoder_cmd(const void *arg, bool write_only)
637{
638 const struct v4l2_decoder_cmd *p = arg;
639
640 pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
641
642 if (p->cmd == V4L2_DEC_CMD_START)
643 pr_info("speed=%d, format=%u\n",
644 p->start.speed, p->start.format);
645 else if (p->cmd == V4L2_DEC_CMD_STOP)
646 pr_info("pts=%llu\n", p->stop.pts);
647}
648
96b03d2a 649static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
79b0c640 650{
96b03d2a 651 const struct v4l2_dbg_chip_info *p = arg;
79b0c640
HV
652
653 pr_cont("type=%u, ", p->match.type);
3eef2510 654 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
79b0c640
HV
655 pr_cont("name=%.*s, ",
656 (int)sizeof(p->match.name), p->match.name);
657 else
658 pr_cont("addr=%u, ", p->match.addr);
659 pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
660}
661
f16f77b0
HV
662static void v4l_print_dbg_register(const void *arg, bool write_only)
663{
664 const struct v4l2_dbg_register *p = arg;
665
666 pr_cont("type=%u, ", p->match.type);
3eef2510 667 if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
27d5a87c
HV
668 pr_cont("name=%.*s, ",
669 (int)sizeof(p->match.name), p->match.name);
f16f77b0
HV
670 else
671 pr_cont("addr=%u, ", p->match.addr);
672 pr_cont("reg=0x%llx, val=0x%llx\n",
673 p->reg, p->val);
674}
675
efc626b0 676static void v4l_print_dv_timings(const void *arg, bool write_only)
5d7758ee 677{
efc626b0
HV
678 const struct v4l2_dv_timings *p = arg;
679
5d7758ee
HV
680 switch (p->type) {
681 case V4L2_DV_BT_656_1120:
efc626b0
HV
682 pr_cont("type=bt-656/1120, interlaced=%u, "
683 "pixelclock=%llu, "
684 "width=%u, height=%u, polarities=0x%x, "
685 "hfrontporch=%u, hsync=%u, "
686 "hbackporch=%u, vfrontporch=%u, "
687 "vsync=%u, vbackporch=%u, "
688 "il_vfrontporch=%u, il_vsync=%u, "
689 "il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
5d7758ee
HV
690 p->bt.interlaced, p->bt.pixelclock,
691 p->bt.width, p->bt.height,
692 p->bt.polarities, p->bt.hfrontporch,
693 p->bt.hsync, p->bt.hbackporch,
694 p->bt.vfrontporch, p->bt.vsync,
695 p->bt.vbackporch, p->bt.il_vfrontporch,
696 p->bt.il_vsync, p->bt.il_vbackporch,
697 p->bt.standards, p->bt.flags);
698 break;
699 default:
efc626b0 700 pr_cont("type=%d\n", p->type);
5d7758ee
HV
701 break;
702 }
703}
704
efc626b0
HV
705static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
706{
707 const struct v4l2_enum_dv_timings *p = arg;
708
709 pr_cont("index=%u, ", p->index);
710 v4l_print_dv_timings(&p->timings, write_only);
711}
712
713static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
714{
715 const struct v4l2_dv_timings_cap *p = arg;
716
717 switch (p->type) {
718 case V4L2_DV_BT_656_1120:
719 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
720 "pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
721 p->bt.min_width, p->bt.max_width,
722 p->bt.min_height, p->bt.max_height,
723 p->bt.min_pixelclock, p->bt.max_pixelclock,
724 p->bt.standards, p->bt.capabilities);
725 break;
726 default:
727 pr_cont("type=%u\n", p->type);
728 break;
729 }
730}
731
458aa4a6
HV
732static void v4l_print_frmsizeenum(const void *arg, bool write_only)
733{
734 const struct v4l2_frmsizeenum *p = arg;
735
736 pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
737 p->index,
738 (p->pixel_format & 0xff),
739 (p->pixel_format >> 8) & 0xff,
740 (p->pixel_format >> 16) & 0xff,
741 (p->pixel_format >> 24) & 0xff,
742 p->type);
743 switch (p->type) {
744 case V4L2_FRMSIZE_TYPE_DISCRETE:
560dde24 745 pr_cont(", wxh=%ux%u\n",
458aa4a6
HV
746 p->discrete.width, p->discrete.height);
747 break;
748 case V4L2_FRMSIZE_TYPE_STEPWISE:
560dde24 749 pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
458aa4a6
HV
750 p->stepwise.min_width, p->stepwise.min_height,
751 p->stepwise.step_width, p->stepwise.step_height,
752 p->stepwise.max_width, p->stepwise.max_height);
753 break;
754 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
755 /* fall through */
756 default:
757 pr_cont("\n");
758 break;
759 }
760}
761
762static void v4l_print_frmivalenum(const void *arg, bool write_only)
763{
764 const struct v4l2_frmivalenum *p = arg;
765
766 pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
767 p->index,
768 (p->pixel_format & 0xff),
769 (p->pixel_format >> 8) & 0xff,
770 (p->pixel_format >> 16) & 0xff,
771 (p->pixel_format >> 24) & 0xff,
772 p->width, p->height, p->type);
773 switch (p->type) {
774 case V4L2_FRMIVAL_TYPE_DISCRETE:
560dde24 775 pr_cont(", fps=%d/%d\n",
458aa4a6
HV
776 p->discrete.numerator,
777 p->discrete.denominator);
778 break;
779 case V4L2_FRMIVAL_TYPE_STEPWISE:
560dde24 780 pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
458aa4a6
HV
781 p->stepwise.min.numerator,
782 p->stepwise.min.denominator,
783 p->stepwise.max.numerator,
784 p->stepwise.max.denominator,
785 p->stepwise.step.numerator,
786 p->stepwise.step.denominator);
787 break;
788 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
789 /* fall through */
790 default:
791 pr_cont("\n");
792 break;
793 }
794}
795
796static void v4l_print_event(const void *arg, bool write_only)
797{
798 const struct v4l2_event *p = arg;
799 const struct v4l2_event_ctrl *c;
800
801 pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
802 "timestamp=%lu.%9.9lu\n",
803 p->type, p->pending, p->sequence, p->id,
804 p->timestamp.tv_sec, p->timestamp.tv_nsec);
805 switch (p->type) {
806 case V4L2_EVENT_VSYNC:
807 printk(KERN_DEBUG "field=%s\n",
808 prt_names(p->u.vsync.field, v4l2_field_names));
809 break;
810 case V4L2_EVENT_CTRL:
811 c = &p->u.ctrl;
812 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
813 c->changes, c->type);
814 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
815 pr_cont("value64=%lld, ", c->value64);
816 else
817 pr_cont("value=%d, ", c->value);
560dde24
HV
818 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, "
819 "default_value=%d\n",
458aa4a6
HV
820 c->flags, c->minimum, c->maximum,
821 c->step, c->default_value);
822 break;
823 case V4L2_EVENT_FRAME_SYNC:
824 pr_cont("frame_sequence=%u\n",
825 p->u.frame_sync.frame_sequence);
826 break;
827 }
828}
829
830static void v4l_print_event_subscription(const void *arg, bool write_only)
831{
832 const struct v4l2_event_subscription *p = arg;
833
834 pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
835 p->type, p->id, p->flags);
836}
837
838static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
839{
840 const struct v4l2_sliced_vbi_cap *p = arg;
841 int i;
842
843 pr_cont("type=%s, service_set=0x%08x\n",
844 prt_names(p->type, v4l2_type_names), p->service_set);
845 for (i = 0; i < 24; i++)
846 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
847 p->service_lines[0][i],
848 p->service_lines[1][i]);
849}
850
82b655bf
HV
851static void v4l_print_freq_band(const void *arg, bool write_only)
852{
853 const struct v4l2_frequency_band *p = arg;
854
855 pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
560dde24 856 "rangelow=%u, rangehigh=%u, modulation=0x%x\n",
82b655bf
HV
857 p->tuner, p->type, p->index,
858 p->capability, p->rangelow,
859 p->rangehigh, p->modulation);
860}
861
dd519bb3
HV
862static void v4l_print_edid(const void *arg, bool write_only)
863{
864 const struct v4l2_edid *p = arg;
865
866 pr_cont("pad=%u, start_block=%u, blocks=%u\n",
867 p->pad, p->start_block, p->blocks);
868}
869
efc626b0
HV
870static void v4l_print_u32(const void *arg, bool write_only)
871{
872 pr_cont("value=%u\n", *(const u32 *)arg);
873}
874
875static void v4l_print_newline(const void *arg, bool write_only)
876{
877 pr_cont("\n");
878}
879
f18d8e07
HV
880static void v4l_print_default(const void *arg, bool write_only)
881{
882 pr_cont("driver-specific ioctl\n");
883}
884
efbceecd 885static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
35ea11ff
HV
886{
887 __u32 i;
888
889 /* zero the reserved fields */
890 c->reserved[0] = c->reserved[1] = 0;
6b5a9492 891 for (i = 0; i < c->count; i++)
35ea11ff 892 c->controls[i].reserved2[0] = 0;
6b5a9492 893
35ea11ff
HV
894 /* V4L2_CID_PRIVATE_BASE cannot be used as control class
895 when using extended controls.
896 Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
897 is it allowed for backwards compatibility.
898 */
899 if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
900 return 0;
901 /* Check that all controls are from the same control class. */
902 for (i = 0; i < c->count; i++) {
903 if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
904 c->error_idx = i;
905 return 0;
906 }
907 }
908 return 1;
909}
910
4b20259f 911static int check_fmt(struct file *file, enum v4l2_buf_type type)
35ea11ff 912{
4b20259f
HV
913 struct video_device *vfd = video_devdata(file);
914 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
915 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
916 bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
582c52cb 917 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
4b20259f
HV
918 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
919 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
920
a399810c
HV
921 if (ops == NULL)
922 return -EINVAL;
923
35ea11ff
HV
924 switch (type) {
925 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4b20259f
HV
926 if (is_vid && is_rx &&
927 (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
d14e6d76
PO
928 return 0;
929 break;
930 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
4b20259f 931 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
35ea11ff
HV
932 return 0;
933 break;
934 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4b20259f 935 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
35ea11ff
HV
936 return 0;
937 break;
938 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4b20259f
HV
939 if (is_vid && is_tx &&
940 (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
d14e6d76
PO
941 return 0;
942 break;
943 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
4b20259f 944 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
35ea11ff
HV
945 return 0;
946 break;
947 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
4b20259f 948 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
35ea11ff
HV
949 return 0;
950 break;
951 case V4L2_BUF_TYPE_VBI_CAPTURE:
4b20259f 952 if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
35ea11ff
HV
953 return 0;
954 break;
955 case V4L2_BUF_TYPE_VBI_OUTPUT:
4b20259f 956 if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
35ea11ff
HV
957 return 0;
958 break;
959 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
4b20259f 960 if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
35ea11ff
HV
961 return 0;
962 break;
963 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
4b20259f 964 if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
35ea11ff
HV
965 return 0;
966 break;
582c52cb
AP
967 case V4L2_BUF_TYPE_SDR_CAPTURE:
968 if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
969 return 0;
970 break;
633c98e5 971 default:
35ea11ff
HV
972 break;
973 }
974 return -EINVAL;
975}
976
d52e2381
LP
977static void v4l_sanitize_format(struct v4l2_format *fmt)
978{
979 unsigned int offset;
980
981 /*
982 * The v4l2_pix_format structure has been extended with fields that were
983 * not previously required to be set to zero by applications. The priv
984 * field, when set to a magic value, indicates the the extended fields
985 * are valid. Otherwise they will contain undefined values. To simplify
986 * the API towards drivers zero the extended fields and set the priv
987 * field to the magic value when the extended pixel format structure
988 * isn't used by applications.
989 */
990
991 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
992 fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
993 return;
994
995 if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
996 return;
997
998 fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
999
1000 offset = offsetof(struct v4l2_pix_format, priv)
1001 + sizeof(fmt->fmt.pix.priv);
1002 memset(((void *)&fmt->fmt.pix) + offset, 0,
1003 sizeof(fmt->fmt.pix) - offset);
1004}
1005
59076122
HV
1006static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
1007 struct file *file, void *fh, void *arg)
1008{
1009 struct v4l2_capability *cap = (struct v4l2_capability *)arg;
d52e2381 1010 int ret;
59076122
HV
1011
1012 cap->version = LINUX_VERSION_CODE;
d52e2381
LP
1013
1014 ret = ops->vidioc_querycap(file, fh, cap);
1015
1016 cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
796a2bd2 1017 cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
d52e2381
LP
1018
1019 return ret;
59076122
HV
1020}
1021
1022static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
1023 struct file *file, void *fh, void *arg)
1024{
1025 return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
1026}
1027
1028static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
1029 struct file *file, void *fh, void *arg)
1030{
1031 return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
1032}
1033
d0547cba
HV
1034static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
1035 struct file *file, void *fh, void *arg)
1036{
1037 struct video_device *vfd;
1038 u32 *p = arg;
1039
1040 if (ops->vidioc_g_priority)
1041 return ops->vidioc_g_priority(file, fh, arg);
1042 vfd = video_devdata(file);
1043 *p = v4l2_prio_max(&vfd->v4l2_dev->prio);
1044 return 0;
1045}
1046
1047static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
1048 struct file *file, void *fh, void *arg)
1049{
1050 struct video_device *vfd;
1051 struct v4l2_fh *vfh;
1052 u32 *p = arg;
1053
1054 if (ops->vidioc_s_priority)
1055 return ops->vidioc_s_priority(file, fh, *p);
1056 vfd = video_devdata(file);
1057 vfh = file->private_data;
1058 return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
1059}
1060
59076122
HV
1061static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1062 struct file *file, void *fh, void *arg)
1063{
73f35418 1064 struct video_device *vfd = video_devdata(file);
59076122
HV
1065 struct v4l2_input *p = arg;
1066
1067 /*
02fa6282 1068 * We set the flags for CAP_DV_TIMINGS &
59076122
HV
1069 * CAP_STD here based on ioctl handler provided by the
1070 * driver. If the driver doesn't support these
1071 * for a specific input, it must override these flags.
1072 */
73f35418 1073 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
59076122 1074 p->capabilities |= V4L2_IN_CAP_STD;
59076122
HV
1075
1076 return ops->vidioc_enum_input(file, fh, p);
1077}
1078
1079static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1080 struct file *file, void *fh, void *arg)
1081{
73f35418 1082 struct video_device *vfd = video_devdata(file);
59076122
HV
1083 struct v4l2_output *p = arg;
1084
1085 /*
02fa6282 1086 * We set the flags for CAP_DV_TIMINGS &
59076122
HV
1087 * CAP_STD here based on ioctl handler provided by the
1088 * driver. If the driver doesn't support these
1089 * for a specific output, it must override these flags.
1090 */
73f35418 1091 if (is_valid_ioctl(vfd, VIDIOC_S_STD))
59076122 1092 p->capabilities |= V4L2_OUT_CAP_STD;
59076122
HV
1093
1094 return ops->vidioc_enum_output(file, fh, p);
1095}
1096
be6c64e4
HV
1097static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1098 struct file *file, void *fh, void *arg)
1099{
1100 struct v4l2_fmtdesc *p = arg;
4b20259f 1101 struct video_device *vfd = video_devdata(file);
ce71bbc9
HV
1102 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1103 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
4b20259f
HV
1104 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1105 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
be6c64e4
HV
1106
1107 switch (p->type) {
1108 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
ce71bbc9 1109 if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_cap))
be6c64e4
HV
1110 break;
1111 return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1112 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
ce71bbc9 1113 if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_cap_mplane))
be6c64e4
HV
1114 break;
1115 return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1116 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
ce71bbc9 1117 if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_overlay))
be6c64e4
HV
1118 break;
1119 return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1120 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
ce71bbc9 1121 if (unlikely(!is_tx || !is_vid || !ops->vidioc_enum_fmt_vid_out))
be6c64e4
HV
1122 break;
1123 return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1124 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
ce71bbc9 1125 if (unlikely(!is_tx || !is_vid || !ops->vidioc_enum_fmt_vid_out_mplane))
be6c64e4
HV
1126 break;
1127 return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
582c52cb 1128 case V4L2_BUF_TYPE_SDR_CAPTURE:
ce71bbc9 1129 if (unlikely(!is_rx || !is_sdr || !ops->vidioc_enum_fmt_sdr_cap))
582c52cb
AP
1130 break;
1131 return ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
be6c64e4
HV
1132 }
1133 return -EINVAL;
1134}
1135
1136static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1137 struct file *file, void *fh, void *arg)
1138{
1139 struct v4l2_format *p = arg;
4b20259f
HV
1140 struct video_device *vfd = video_devdata(file);
1141 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
582c52cb 1142 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
4b20259f
HV
1143 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1144 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
d52e2381
LP
1145 int ret;
1146
e5ce558a
HV
1147 /*
1148 * fmt can't be cleared for these overlay types due to the 'clips'
1149 * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
1150 * Those are provided by the user. So handle these two overlay types
1151 * first, and then just do a simple memset for the other types.
1152 */
1153 switch (p->type) {
1154 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1155 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
1156 struct v4l2_clip *clips = p->fmt.win.clips;
1157 u32 clipcount = p->fmt.win.clipcount;
1158 void *bitmap = p->fmt.win.bitmap;
1159
1160 memset(&p->fmt, 0, sizeof(p->fmt));
1161 p->fmt.win.clips = clips;
1162 p->fmt.win.clipcount = clipcount;
1163 p->fmt.win.bitmap = bitmap;
1164 break;
1165 }
1166 default:
1167 memset(&p->fmt, 0, sizeof(p->fmt));
1168 break;
1169 }
1170
be6c64e4
HV
1171 switch (p->type) {
1172 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4b20259f 1173 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap))
be6c64e4 1174 break;
48f2650a 1175 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
d52e2381 1176 ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
48f2650a 1177 /* just in case the driver zeroed it again */
d52e2381
LP
1178 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1179 return ret;
be6c64e4 1180 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
4b20259f 1181 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap_mplane))
be6c64e4
HV
1182 break;
1183 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1184 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4b20259f 1185 if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_overlay))
be6c64e4
HV
1186 break;
1187 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
4b20259f
HV
1188 case V4L2_BUF_TYPE_VBI_CAPTURE:
1189 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_vbi_cap))
1190 break;
1191 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1192 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1193 if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_cap))
1194 break;
1195 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
be6c64e4 1196 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4b20259f 1197 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out))
be6c64e4 1198 break;
48f2650a 1199 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
d52e2381 1200 ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
48f2650a 1201 /* just in case the driver zeroed it again */
d52e2381
LP
1202 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1203 return ret;
be6c64e4 1204 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
4b20259f 1205 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_mplane))
be6c64e4
HV
1206 break;
1207 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1208 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
4b20259f 1209 if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_overlay))
be6c64e4
HV
1210 break;
1211 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
be6c64e4 1212 case V4L2_BUF_TYPE_VBI_OUTPUT:
4b20259f 1213 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_vbi_out))
be6c64e4
HV
1214 break;
1215 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
be6c64e4 1216 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
4b20259f 1217 if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_out))
be6c64e4
HV
1218 break;
1219 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
582c52cb
AP
1220 case V4L2_BUF_TYPE_SDR_CAPTURE:
1221 if (unlikely(!is_rx || !is_sdr || !ops->vidioc_g_fmt_sdr_cap))
1222 break;
1223 return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
be6c64e4
HV
1224 }
1225 return -EINVAL;
1226}
1227
1228static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1229 struct file *file, void *fh, void *arg)
1230{
1231 struct v4l2_format *p = arg;
4b20259f
HV
1232 struct video_device *vfd = video_devdata(file);
1233 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
582c52cb 1234 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
4b20259f
HV
1235 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1236 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
48f2650a 1237 int ret;
be6c64e4 1238
d52e2381
LP
1239 v4l_sanitize_format(p);
1240
be6c64e4
HV
1241 switch (p->type) {
1242 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4b20259f 1243 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap))
be6c64e4
HV
1244 break;
1245 CLEAR_AFTER_FIELD(p, fmt.pix);
48f2650a
HV
1246 ret = ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1247 /* just in case the driver zeroed it again */
1248 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1249 return ret;
be6c64e4 1250 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
4b20259f 1251 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap_mplane))
be6c64e4
HV
1252 break;
1253 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1254 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1255 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4b20259f 1256 if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_overlay))
be6c64e4
HV
1257 break;
1258 CLEAR_AFTER_FIELD(p, fmt.win);
1259 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
4b20259f
HV
1260 case V4L2_BUF_TYPE_VBI_CAPTURE:
1261 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_vbi_cap))
1262 break;
1263 CLEAR_AFTER_FIELD(p, fmt.vbi);
1264 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1265 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1266 if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_cap))
1267 break;
1268 CLEAR_AFTER_FIELD(p, fmt.sliced);
1269 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
be6c64e4 1270 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4b20259f 1271 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out))
be6c64e4
HV
1272 break;
1273 CLEAR_AFTER_FIELD(p, fmt.pix);
48f2650a
HV
1274 ret = ops->vidioc_s_fmt_vid_out(file, fh, arg);
1275 /* just in case the driver zeroed it again */
1276 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1277 return ret;
be6c64e4 1278 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
4b20259f 1279 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_mplane))
be6c64e4
HV
1280 break;
1281 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1282 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1283 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
4b20259f 1284 if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_overlay))
be6c64e4
HV
1285 break;
1286 CLEAR_AFTER_FIELD(p, fmt.win);
1287 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
be6c64e4 1288 case V4L2_BUF_TYPE_VBI_OUTPUT:
4b20259f 1289 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_vbi_out))
be6c64e4
HV
1290 break;
1291 CLEAR_AFTER_FIELD(p, fmt.vbi);
1292 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
be6c64e4 1293 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
4b20259f 1294 if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_out))
be6c64e4
HV
1295 break;
1296 CLEAR_AFTER_FIELD(p, fmt.sliced);
1297 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
582c52cb
AP
1298 case V4L2_BUF_TYPE_SDR_CAPTURE:
1299 if (unlikely(!is_rx || !is_sdr || !ops->vidioc_s_fmt_sdr_cap))
1300 break;
1301 CLEAR_AFTER_FIELD(p, fmt.sdr);
1302 return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
be6c64e4
HV
1303 }
1304 return -EINVAL;
1305}
1306
1307static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1308 struct file *file, void *fh, void *arg)
1309{
1310 struct v4l2_format *p = arg;
4b20259f
HV
1311 struct video_device *vfd = video_devdata(file);
1312 bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
582c52cb 1313 bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
4b20259f
HV
1314 bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1315 bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
48f2650a 1316 int ret;
be6c64e4 1317
d52e2381
LP
1318 v4l_sanitize_format(p);
1319
be6c64e4
HV
1320 switch (p->type) {
1321 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
4b20259f 1322 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap))
be6c64e4
HV
1323 break;
1324 CLEAR_AFTER_FIELD(p, fmt.pix);
48f2650a
HV
1325 ret = ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1326 /* just in case the driver zeroed it again */
1327 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1328 return ret;
be6c64e4 1329 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
4b20259f 1330 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap_mplane))
be6c64e4
HV
1331 break;
1332 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1333 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1334 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
4b20259f 1335 if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_overlay))
be6c64e4
HV
1336 break;
1337 CLEAR_AFTER_FIELD(p, fmt.win);
1338 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
4b20259f
HV
1339 case V4L2_BUF_TYPE_VBI_CAPTURE:
1340 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_vbi_cap))
1341 break;
1342 CLEAR_AFTER_FIELD(p, fmt.vbi);
1343 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1344 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1345 if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_cap))
1346 break;
1347 CLEAR_AFTER_FIELD(p, fmt.sliced);
1348 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
be6c64e4 1349 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
4b20259f 1350 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out))
be6c64e4
HV
1351 break;
1352 CLEAR_AFTER_FIELD(p, fmt.pix);
48f2650a
HV
1353 ret = ops->vidioc_try_fmt_vid_out(file, fh, arg);
1354 /* just in case the driver zeroed it again */
1355 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1356 return ret;
be6c64e4 1357 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
4b20259f 1358 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_mplane))
be6c64e4
HV
1359 break;
1360 CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1361 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1362 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
4b20259f 1363 if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_overlay))
be6c64e4
HV
1364 break;
1365 CLEAR_AFTER_FIELD(p, fmt.win);
1366 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
be6c64e4 1367 case V4L2_BUF_TYPE_VBI_OUTPUT:
4b20259f 1368 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_vbi_out))
be6c64e4
HV
1369 break;
1370 CLEAR_AFTER_FIELD(p, fmt.vbi);
1371 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
be6c64e4 1372 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
4b20259f 1373 if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_out))
be6c64e4
HV
1374 break;
1375 CLEAR_AFTER_FIELD(p, fmt.sliced);
1376 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
582c52cb
AP
1377 case V4L2_BUF_TYPE_SDR_CAPTURE:
1378 if (unlikely(!is_rx || !is_sdr || !ops->vidioc_try_fmt_sdr_cap))
1379 break;
1380 CLEAR_AFTER_FIELD(p, fmt.sdr);
1381 return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
be6c64e4
HV
1382 }
1383 return -EINVAL;
1384}
1385
e325b244
HV
1386static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1387 struct file *file, void *fh, void *arg)
1388{
1389 return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1390}
1391
1392static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1393 struct file *file, void *fh, void *arg)
1394{
1395 return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1396}
1397
4b1e2e4d
HV
1398static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1399 struct file *file, void *fh, void *arg)
1400{
1401 struct video_device *vfd = video_devdata(file);
1402 struct v4l2_tuner *p = arg;
82b655bf 1403 int err;
4b1e2e4d
HV
1404
1405 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1406 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
82b655bf
HV
1407 err = ops->vidioc_g_tuner(file, fh, p);
1408 if (!err)
1409 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1410 return err;
4b1e2e4d
HV
1411}
1412
1413static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1414 struct file *file, void *fh, void *arg)
1415{
1416 struct video_device *vfd = video_devdata(file);
1417 struct v4l2_tuner *p = arg;
1418
1419 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1420 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1421 return ops->vidioc_s_tuner(file, fh, p);
1422}
1423
82b655bf
HV
1424static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1425 struct file *file, void *fh, void *arg)
1426{
1427 struct v4l2_modulator *p = arg;
1428 int err;
1429
1430 err = ops->vidioc_g_modulator(file, fh, p);
1431 if (!err)
1432 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1433 return err;
1434}
1435
4b1e2e4d
HV
1436static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1437 struct file *file, void *fh, void *arg)
1438{
1439 struct video_device *vfd = video_devdata(file);
1440 struct v4l2_frequency *p = arg;
1441
84099a28
AP
1442 if (vfd->vfl_type == VFL_TYPE_SDR)
1443 p->type = V4L2_TUNER_ADC;
1444 else
1445 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1446 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
4b1e2e4d
HV
1447 return ops->vidioc_g_frequency(file, fh, p);
1448}
1449
1450static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1451 struct file *file, void *fh, void *arg)
1452{
1453 struct video_device *vfd = video_devdata(file);
b530a447 1454 const struct v4l2_frequency *p = arg;
4b1e2e4d
HV
1455 enum v4l2_tuner_type type;
1456
84099a28
AP
1457 if (vfd->vfl_type == VFL_TYPE_SDR) {
1458 if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
1459 return -EINVAL;
1460 } else {
1461 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1462 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1463 if (type != p->type)
1464 return -EINVAL;
1465 }
4b1e2e4d
HV
1466 return ops->vidioc_s_frequency(file, fh, p);
1467}
1468
1469static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1470 struct file *file, void *fh, void *arg)
1471{
1472 struct video_device *vfd = video_devdata(file);
1473 struct v4l2_standard *p = arg;
1474 v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1475 unsigned int index = p->index, i, j = 0;
1476 const char *descr = "";
1477
a5338190
HV
1478 /* Return -ENODATA if the tvnorms for the current input
1479 or output is 0, meaning that it doesn't support this API. */
1480 if (id == 0)
1481 return -ENODATA;
1482
4b1e2e4d
HV
1483 /* Return norm array in a canonical way */
1484 for (i = 0; i <= index && id; i++) {
1485 /* last std value in the standards array is 0, so this
1486 while always ends there since (id & 0) == 0. */
1487 while ((id & standards[j].std) != standards[j].std)
1488 j++;
1489 curr_id = standards[j].std;
1490 descr = standards[j].descr;
1491 j++;
1492 if (curr_id == 0)
1493 break;
1494 if (curr_id != V4L2_STD_PAL &&
1495 curr_id != V4L2_STD_SECAM &&
1496 curr_id != V4L2_STD_NTSC)
1497 id &= ~curr_id;
1498 }
1499 if (i <= index)
1500 return -EINVAL;
1501
1502 v4l2_video_std_construct(p, curr_id, descr);
1503 return 0;
1504}
1505
4b1e2e4d
HV
1506static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1507 struct file *file, void *fh, void *arg)
1508{
1509 struct video_device *vfd = video_devdata(file);
314527ac 1510 v4l2_std_id id = *(v4l2_std_id *)arg, norm;
4b1e2e4d 1511
314527ac 1512 norm = id & vfd->tvnorms;
4b1e2e4d
HV
1513 if (vfd->tvnorms && !norm) /* Check if std is supported */
1514 return -EINVAL;
1515
1516 /* Calls the specific handler */
ca371575 1517 return ops->vidioc_s_std(file, fh, norm);
4b1e2e4d
HV
1518}
1519
1520static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1521 struct file *file, void *fh, void *arg)
1522{
1523 struct video_device *vfd = video_devdata(file);
1524 v4l2_std_id *p = arg;
1525
1526 /*
1a2c6866
HV
1527 * If no signal is detected, then the driver should return
1528 * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
1529 * any standards that do not apply removed.
1530 *
4b1e2e4d
HV
1531 * This means that tuners, audio and video decoders can join
1532 * their efforts to improve the standards detection.
1533 */
1534 *p = vfd->tvnorms;
1535 return ops->vidioc_querystd(file, fh, arg);
1536}
1537
1538static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1539 struct file *file, void *fh, void *arg)
1540{
1541 struct video_device *vfd = video_devdata(file);
1542 struct v4l2_hw_freq_seek *p = arg;
1543 enum v4l2_tuner_type type;
1544
84099a28
AP
1545 /* s_hw_freq_seek is not supported for SDR for now */
1546 if (vfd->vfl_type == VFL_TYPE_SDR)
1547 return -EINVAL;
1548
4b1e2e4d
HV
1549 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1550 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1551 if (p->type != type)
1552 return -EINVAL;
1553 return ops->vidioc_s_hw_freq_seek(file, fh, p);
1554}
1555
737c097b
HV
1556static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
1557 struct file *file, void *fh, void *arg)
1558{
1559 return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
1560}
1561
eb3728ed
HV
1562static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1563 struct file *file, void *fh, void *arg)
1564{
1565 struct v4l2_requestbuffers *p = arg;
4b20259f 1566 int ret = check_fmt(file, p->type);
eb3728ed
HV
1567
1568 if (ret)
1569 return ret;
1570
633c98e5 1571 CLEAR_AFTER_FIELD(p, memory);
eb3728ed
HV
1572
1573 return ops->vidioc_reqbufs(file, fh, p);
1574}
1575
1576static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1577 struct file *file, void *fh, void *arg)
1578{
1579 struct v4l2_buffer *p = arg;
4b20259f 1580 int ret = check_fmt(file, p->type);
eb3728ed
HV
1581
1582 return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1583}
1584
1585static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1586 struct file *file, void *fh, void *arg)
1587{
1588 struct v4l2_buffer *p = arg;
4b20259f 1589 int ret = check_fmt(file, p->type);
eb3728ed
HV
1590
1591 return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1592}
1593
1594static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1595 struct file *file, void *fh, void *arg)
1596{
1597 struct v4l2_buffer *p = arg;
4b20259f 1598 int ret = check_fmt(file, p->type);
eb3728ed
HV
1599
1600 return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1601}
1602
1603static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1604 struct file *file, void *fh, void *arg)
1605{
1606 struct v4l2_create_buffers *create = arg;
4b20259f 1607 int ret = check_fmt(file, create->format.type);
eb3728ed 1608
d52e2381
LP
1609 if (ret)
1610 return ret;
1611
1612 v4l_sanitize_format(&create->format);
1613
1614 ret = ops->vidioc_create_bufs(file, fh, create);
1615
1616 if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1617 create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1618 create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1619
1620 return ret;
eb3728ed
HV
1621}
1622
1623static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1624 struct file *file, void *fh, void *arg)
1625{
1626 struct v4l2_buffer *b = arg;
4b20259f 1627 int ret = check_fmt(file, b->type);
eb3728ed
HV
1628
1629 return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1630}
1631
1632static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1633 struct file *file, void *fh, void *arg)
1634{
eb3728ed
HV
1635 struct v4l2_streamparm *p = arg;
1636 v4l2_std_id std;
4b20259f 1637 int ret = check_fmt(file, p->type);
eb3728ed
HV
1638
1639 if (ret)
1640 return ret;
1641 if (ops->vidioc_g_parm)
1642 return ops->vidioc_g_parm(file, fh, p);
eb3728ed
HV
1643 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1644 p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1645 return -EINVAL;
1646 p->parm.capture.readbuffers = 2;
ca371575 1647 ret = ops->vidioc_g_std(file, fh, &std);
eb3728ed 1648 if (ret == 0)
ca371575 1649 v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
eb3728ed
HV
1650 return ret;
1651}
1652
1653static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1654 struct file *file, void *fh, void *arg)
1655{
1656 struct v4l2_streamparm *p = arg;
4b20259f 1657 int ret = check_fmt(file, p->type);
eb3728ed
HV
1658
1659 return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1660}
1661
efbceecd
HV
1662static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1663 struct file *file, void *fh, void *arg)
1664{
1665 struct video_device *vfd = video_devdata(file);
1666 struct v4l2_queryctrl *p = arg;
7a3ed2d9
HV
1667 struct v4l2_fh *vfh =
1668 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
efbceecd
HV
1669
1670 if (vfh && vfh->ctrl_handler)
1671 return v4l2_queryctrl(vfh->ctrl_handler, p);
1672 if (vfd->ctrl_handler)
1673 return v4l2_queryctrl(vfd->ctrl_handler, p);
1674 if (ops->vidioc_queryctrl)
1675 return ops->vidioc_queryctrl(file, fh, p);
1676 return -ENOTTY;
1677}
1678
e6bee368
HV
1679static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
1680 struct file *file, void *fh, void *arg)
1681{
1682 struct video_device *vfd = video_devdata(file);
1683 struct v4l2_query_ext_ctrl *p = arg;
1684 struct v4l2_fh *vfh =
1685 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1686
1687 if (vfh && vfh->ctrl_handler)
1688 return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
1689 if (vfd->ctrl_handler)
1690 return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
1691 if (ops->vidioc_query_ext_ctrl)
1692 return ops->vidioc_query_ext_ctrl(file, fh, p);
1693 return -ENOTTY;
1694}
1695
efbceecd
HV
1696static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1697 struct file *file, void *fh, void *arg)
1698{
1699 struct video_device *vfd = video_devdata(file);
1700 struct v4l2_querymenu *p = arg;
7a3ed2d9
HV
1701 struct v4l2_fh *vfh =
1702 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
efbceecd
HV
1703
1704 if (vfh && vfh->ctrl_handler)
1705 return v4l2_querymenu(vfh->ctrl_handler, p);
1706 if (vfd->ctrl_handler)
1707 return v4l2_querymenu(vfd->ctrl_handler, p);
1708 if (ops->vidioc_querymenu)
1709 return ops->vidioc_querymenu(file, fh, p);
1710 return -ENOTTY;
1711}
1712
1713static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1714 struct file *file, void *fh, void *arg)
1715{
1716 struct video_device *vfd = video_devdata(file);
1717 struct v4l2_control *p = arg;
7a3ed2d9
HV
1718 struct v4l2_fh *vfh =
1719 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
efbceecd
HV
1720 struct v4l2_ext_controls ctrls;
1721 struct v4l2_ext_control ctrl;
1722
1723 if (vfh && vfh->ctrl_handler)
1724 return v4l2_g_ctrl(vfh->ctrl_handler, p);
1725 if (vfd->ctrl_handler)
1726 return v4l2_g_ctrl(vfd->ctrl_handler, p);
1727 if (ops->vidioc_g_ctrl)
1728 return ops->vidioc_g_ctrl(file, fh, p);
1729 if (ops->vidioc_g_ext_ctrls == NULL)
1730 return -ENOTTY;
1731
1732 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1733 ctrls.count = 1;
1734 ctrls.controls = &ctrl;
1735 ctrl.id = p->id;
1736 ctrl.value = p->value;
1737 if (check_ext_ctrls(&ctrls, 1)) {
1738 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1739
1740 if (ret == 0)
1741 p->value = ctrl.value;
1742 return ret;
1743 }
1744 return -EINVAL;
1745}
1746
1747static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1748 struct file *file, void *fh, void *arg)
1749{
1750 struct video_device *vfd = video_devdata(file);
1751 struct v4l2_control *p = arg;
7a3ed2d9
HV
1752 struct v4l2_fh *vfh =
1753 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
efbceecd
HV
1754 struct v4l2_ext_controls ctrls;
1755 struct v4l2_ext_control ctrl;
1756
1757 if (vfh && vfh->ctrl_handler)
1758 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1759 if (vfd->ctrl_handler)
1760 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1761 if (ops->vidioc_s_ctrl)
1762 return ops->vidioc_s_ctrl(file, fh, p);
1763 if (ops->vidioc_s_ext_ctrls == NULL)
1764 return -ENOTTY;
1765
1766 ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1767 ctrls.count = 1;
1768 ctrls.controls = &ctrl;
1769 ctrl.id = p->id;
1770 ctrl.value = p->value;
1771 if (check_ext_ctrls(&ctrls, 1))
1772 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1773 return -EINVAL;
1774}
1775
1776static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1777 struct file *file, void *fh, void *arg)
1778{
1779 struct video_device *vfd = video_devdata(file);
1780 struct v4l2_ext_controls *p = arg;
7a3ed2d9
HV
1781 struct v4l2_fh *vfh =
1782 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
efbceecd
HV
1783
1784 p->error_idx = p->count;
1785 if (vfh && vfh->ctrl_handler)
1786 return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1787 if (vfd->ctrl_handler)
1788 return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1789 if (ops->vidioc_g_ext_ctrls == NULL)
1790 return -ENOTTY;
1791 return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1792 -EINVAL;
1793}
1794
1795static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1796 struct file *file, void *fh, void *arg)
1797{
1798 struct video_device *vfd = video_devdata(file);
1799 struct v4l2_ext_controls *p = arg;
7a3ed2d9
HV
1800 struct v4l2_fh *vfh =
1801 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
efbceecd
HV
1802
1803 p->error_idx = p->count;
1804 if (vfh && vfh->ctrl_handler)
1805 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1806 if (vfd->ctrl_handler)
1807 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1808 if (ops->vidioc_s_ext_ctrls == NULL)
1809 return -ENOTTY;
1810 return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1811 -EINVAL;
1812}
1813
1814static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1815 struct file *file, void *fh, void *arg)
1816{
1817 struct video_device *vfd = video_devdata(file);
1818 struct v4l2_ext_controls *p = arg;
7a3ed2d9
HV
1819 struct v4l2_fh *vfh =
1820 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
efbceecd
HV
1821
1822 p->error_idx = p->count;
1823 if (vfh && vfh->ctrl_handler)
1824 return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1825 if (vfd->ctrl_handler)
1826 return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1827 if (ops->vidioc_try_ext_ctrls == NULL)
1828 return -ENOTTY;
1829 return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1830 -EINVAL;
1831}
1832
d84f2d94
HV
1833static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1834 struct file *file, void *fh, void *arg)
1835{
1836 struct v4l2_crop *p = arg;
1837 struct v4l2_selection s = {
1838 .type = p->type,
1839 };
1840 int ret;
1841
1842 if (ops->vidioc_g_crop)
1843 return ops->vidioc_g_crop(file, fh, p);
1844 /* simulate capture crop using selection api */
1845
1846 /* crop means compose for output devices */
1847 if (V4L2_TYPE_IS_OUTPUT(p->type))
1848 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1849 else
1850 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1851
1852 ret = ops->vidioc_g_selection(file, fh, &s);
1853
1854 /* copying results to old structure on success */
1855 if (!ret)
1856 p->c = s.r;
1857 return ret;
1858}
1859
1860static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1861 struct file *file, void *fh, void *arg)
1862{
1863 struct v4l2_crop *p = arg;
1864 struct v4l2_selection s = {
1865 .type = p->type,
1866 .r = p->c,
1867 };
1868
1869 if (ops->vidioc_s_crop)
1870 return ops->vidioc_s_crop(file, fh, p);
1871 /* simulate capture crop using selection api */
1872
1873 /* crop means compose for output devices */
1874 if (V4L2_TYPE_IS_OUTPUT(p->type))
1875 s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1876 else
1877 s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1878
1879 return ops->vidioc_s_selection(file, fh, &s);
1880}
1881
1882static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1883 struct file *file, void *fh, void *arg)
1884{
1885 struct v4l2_cropcap *p = arg;
d84f2d94 1886
9409945c
HV
1887 if (ops->vidioc_g_selection) {
1888 struct v4l2_selection s = { .type = p->type };
1889 int ret;
d84f2d94 1890
9409945c
HV
1891 /* obtaining bounds */
1892 if (V4L2_TYPE_IS_OUTPUT(p->type))
1893 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1894 else
1895 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
d84f2d94 1896
9409945c
HV
1897 ret = ops->vidioc_g_selection(file, fh, &s);
1898 if (ret)
1899 return ret;
1900 p->bounds = s.r;
d84f2d94 1901
9409945c
HV
1902 /* obtaining defrect */
1903 if (V4L2_TYPE_IS_OUTPUT(p->type))
1904 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1905 else
1906 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
d84f2d94 1907
9409945c
HV
1908 ret = ops->vidioc_g_selection(file, fh, &s);
1909 if (ret)
1910 return ret;
1911 p->defrect = s.r;
1912 }
d84f2d94
HV
1913
1914 /* setting trivial pixelaspect */
1915 p->pixelaspect.numerator = 1;
1916 p->pixelaspect.denominator = 1;
9409945c
HV
1917
1918 if (ops->vidioc_cropcap)
1919 return ops->vidioc_cropcap(file, fh, p);
1920
d84f2d94
HV
1921 return 0;
1922}
1923
f16f77b0
HV
1924static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1925 struct file *file, void *fh, void *arg)
1926{
1927 struct video_device *vfd = video_devdata(file);
1928 int ret;
1929
1930 if (vfd->v4l2_dev)
1931 pr_info("%s: ================= START STATUS =================\n",
1932 vfd->v4l2_dev->name);
1933 ret = ops->vidioc_log_status(file, fh);
1934 if (vfd->v4l2_dev)
1935 pr_info("%s: ================== END STATUS ==================\n",
1936 vfd->v4l2_dev->name);
1937 return ret;
1938}
1939
1940static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1941 struct file *file, void *fh, void *arg)
1942{
1943#ifdef CONFIG_VIDEO_ADV_DEBUG
1944 struct v4l2_dbg_register *p = arg;
79b0c640
HV
1945 struct video_device *vfd = video_devdata(file);
1946 struct v4l2_subdev *sd;
1947 int idx = 0;
f16f77b0
HV
1948
1949 if (!capable(CAP_SYS_ADMIN))
1950 return -EPERM;
3eef2510 1951 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
79b0c640
HV
1952 if (vfd->v4l2_dev == NULL)
1953 return -EINVAL;
3eef2510
HV
1954 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1955 if (p->match.addr == idx++)
79b0c640 1956 return v4l2_subdev_call(sd, core, g_register, p);
79b0c640
HV
1957 return -EINVAL;
1958 }
191b79b0
HV
1959 if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1960 (ops->vidioc_g_chip_info || p->match.addr == 0))
79b0c640
HV
1961 return ops->vidioc_g_register(file, fh, p);
1962 return -EINVAL;
f16f77b0
HV
1963#else
1964 return -ENOTTY;
1965#endif
1966}
1967
1968static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1969 struct file *file, void *fh, void *arg)
1970{
1971#ifdef CONFIG_VIDEO_ADV_DEBUG
977ba3b1 1972 const struct v4l2_dbg_register *p = arg;
79b0c640
HV
1973 struct video_device *vfd = video_devdata(file);
1974 struct v4l2_subdev *sd;
1975 int idx = 0;
f16f77b0
HV
1976
1977 if (!capable(CAP_SYS_ADMIN))
1978 return -EPERM;
3eef2510 1979 if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
79b0c640
HV
1980 if (vfd->v4l2_dev == NULL)
1981 return -EINVAL;
3eef2510
HV
1982 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1983 if (p->match.addr == idx++)
79b0c640 1984 return v4l2_subdev_call(sd, core, s_register, p);
79b0c640
HV
1985 return -EINVAL;
1986 }
191b79b0
HV
1987 if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1988 (ops->vidioc_g_chip_info || p->match.addr == 0))
79b0c640
HV
1989 return ops->vidioc_s_register(file, fh, p);
1990 return -EINVAL;
f16f77b0
HV
1991#else
1992 return -ENOTTY;
1993#endif
1994}
1995
96b03d2a 1996static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
79b0c640
HV
1997 struct file *file, void *fh, void *arg)
1998{
cd634f1b 1999#ifdef CONFIG_VIDEO_ADV_DEBUG
79b0c640 2000 struct video_device *vfd = video_devdata(file);
96b03d2a 2001 struct v4l2_dbg_chip_info *p = arg;
79b0c640
HV
2002 struct v4l2_subdev *sd;
2003 int idx = 0;
2004
2005 switch (p->match.type) {
2006 case V4L2_CHIP_MATCH_BRIDGE:
79b0c640
HV
2007 if (ops->vidioc_s_register)
2008 p->flags |= V4L2_CHIP_FL_WRITABLE;
2009 if (ops->vidioc_g_register)
2010 p->flags |= V4L2_CHIP_FL_READABLE;
1c1d86a1 2011 strlcpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
96b03d2a
HV
2012 if (ops->vidioc_g_chip_info)
2013 return ops->vidioc_g_chip_info(file, fh, arg);
0f0fe4b9
HV
2014 if (p->match.addr)
2015 return -EINVAL;
79b0c640
HV
2016 return 0;
2017
3eef2510 2018 case V4L2_CHIP_MATCH_SUBDEV:
79b0c640
HV
2019 if (vfd->v4l2_dev == NULL)
2020 break;
2021 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
3eef2510
HV
2022 if (p->match.addr != idx++)
2023 continue;
2024 if (sd->ops->core && sd->ops->core->s_register)
2025 p->flags |= V4L2_CHIP_FL_WRITABLE;
2026 if (sd->ops->core && sd->ops->core->g_register)
2027 p->flags |= V4L2_CHIP_FL_READABLE;
2028 strlcpy(p->name, sd->name, sizeof(p->name));
2029 return 0;
79b0c640
HV
2030 }
2031 break;
2032 }
2033 return -EINVAL;
cd634f1b
HV
2034#else
2035 return -ENOTTY;
2036#endif
79b0c640
HV
2037}
2038
458aa4a6
HV
2039static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
2040 struct file *file, void *fh, void *arg)
2041{
2042 return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
2043}
2044
2045static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
2046 struct file *file, void *fh, void *arg)
2047{
2048 return ops->vidioc_subscribe_event(fh, arg);
2049}
2050
2051static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
2052 struct file *file, void *fh, void *arg)
2053{
2054 return ops->vidioc_unsubscribe_event(fh, arg);
2055}
2056
2057static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
2058 struct file *file, void *fh, void *arg)
2059{
2060 struct v4l2_sliced_vbi_cap *p = arg;
4b20259f
HV
2061 int ret = check_fmt(file, p->type);
2062
2063 if (ret)
2064 return ret;
458aa4a6
HV
2065
2066 /* Clear up to type, everything after type is zeroed already */
2067 memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
2068
2069 return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
2070}
2071
82b655bf
HV
2072static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
2073 struct file *file, void *fh, void *arg)
2074{
2075 struct video_device *vfd = video_devdata(file);
2076 struct v4l2_frequency_band *p = arg;
2077 enum v4l2_tuner_type type;
2078 int err;
2079
84099a28
AP
2080 if (vfd->vfl_type == VFL_TYPE_SDR) {
2081 if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
2082 return -EINVAL;
2083 type = p->type;
2084 } else {
2085 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2086 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2087 if (type != p->type)
2088 return -EINVAL;
2089 }
a7f404af
HV
2090 if (ops->vidioc_enum_freq_bands) {
2091 err = ops->vidioc_enum_freq_bands(file, fh, p);
2092 if (err != -ENOTTY)
2093 return err;
2094 }
73f35418 2095 if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
82b655bf
HV
2096 struct v4l2_tuner t = {
2097 .index = p->tuner,
2098 .type = type,
2099 };
2100
aa4d9b53
HV
2101 if (p->index)
2102 return -EINVAL;
82b655bf
HV
2103 err = ops->vidioc_g_tuner(file, fh, &t);
2104 if (err)
2105 return err;
2106 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2107 p->rangelow = t.rangelow;
2108 p->rangehigh = t.rangehigh;
2109 p->modulation = (type == V4L2_TUNER_RADIO) ?
2110 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2111 return 0;
2112 }
73f35418 2113 if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
82b655bf
HV
2114 struct v4l2_modulator m = {
2115 .index = p->tuner,
2116 };
2117
2118 if (type != V4L2_TUNER_RADIO)
2119 return -EINVAL;
aa4d9b53
HV
2120 if (p->index)
2121 return -EINVAL;
82b655bf
HV
2122 err = ops->vidioc_g_modulator(file, fh, &m);
2123 if (err)
2124 return err;
2125 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2126 p->rangelow = m.rangelow;
2127 p->rangehigh = m.rangehigh;
2128 p->modulation = (type == V4L2_TUNER_RADIO) ?
2129 V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2130 return 0;
2131 }
2132 return -ENOTTY;
2133}
2134
4839e6c2
HV
2135struct v4l2_ioctl_info {
2136 unsigned int ioctl;
27cd2aba 2137 u32 flags;
4839e6c2 2138 const char * const name;
86748f35
HV
2139 union {
2140 u32 offset;
2141 int (*func)(const struct v4l2_ioctl_ops *ops,
2142 struct file *file, void *fh, void *p);
26ddcbcc 2143 } u;
86748f35 2144 void (*debug)(const void *arg, bool write_only);
4839e6c2
HV
2145};
2146
2147/* This control needs a priority check */
2148#define INFO_FL_PRIO (1 << 0)
2149/* This control can be valid if the filehandle passes a control handler. */
2150#define INFO_FL_CTRL (1 << 1)
86748f35
HV
2151/* This is a standard ioctl, no need for special code */
2152#define INFO_FL_STD (1 << 2)
2153/* This is ioctl has its own function */
2154#define INFO_FL_FUNC (1 << 3)
5a5adf6b
HV
2155/* Queuing ioctl */
2156#define INFO_FL_QUEUE (1 << 4)
27cd2aba
HV
2157/* Zero struct from after the field to the end */
2158#define INFO_FL_CLEAR(v4l2_struct, field) \
2159 ((offsetof(struct v4l2_struct, field) + \
2160 sizeof(((struct v4l2_struct *)0)->field)) << 16)
2161#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
4839e6c2 2162
86748f35
HV
2163#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags) \
2164 [_IOC_NR(_ioctl)] = { \
2165 .ioctl = _ioctl, \
2166 .flags = _flags | INFO_FL_STD, \
2167 .name = #_ioctl, \
26ddcbcc 2168 .u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc), \
86748f35
HV
2169 .debug = _debug, \
2170 }
2171
2172#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags) \
2173 [_IOC_NR(_ioctl)] = { \
2174 .ioctl = _ioctl, \
2175 .flags = _flags | INFO_FL_FUNC, \
2176 .name = #_ioctl, \
26ddcbcc 2177 .u.func = _func, \
86748f35
HV
2178 .debug = _debug, \
2179 }
2180
4839e6c2 2181static struct v4l2_ioctl_info v4l2_ioctls[] = {
59076122 2182 IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
be6c64e4 2183 IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
e5ce558a 2184 IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
be6c64e4 2185 IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
5a5adf6b
HV
2186 IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2187 IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
be6c64e4
HV
2188 IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
2189 IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
737c097b 2190 IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
5a5adf6b 2191 IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
b799d09a 2192 IOCTL_INFO_STD(VIDIOC_EXPBUF, vidioc_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
5a5adf6b
HV
2193 IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2194 IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2195 IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
eb3728ed
HV
2196 IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2197 IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
ca371575 2198 IOCTL_INFO_STD(VIDIOC_G_STD, vidioc_g_std, v4l_print_std, 0),
4b1e2e4d
HV
2199 IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2200 IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
59076122 2201 IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
efbceecd
HV
2202 IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2203 IOCTL_INFO_FNC(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
4b1e2e4d
HV
2204 IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2205 IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
59076122
HV
2206 IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
2207 IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
efbceecd
HV
2208 IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2209 IOCTL_INFO_FNC(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
59076122
HV
2210 IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
2211 IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
f9402a94
HV
2212 IOCTL_INFO_STD(VIDIOC_G_EDID, vidioc_g_edid, v4l_print_edid, 0),
2213 IOCTL_INFO_STD(VIDIOC_S_EDID, vidioc_s_edid, v4l_print_edid, INFO_FL_PRIO),
59076122
HV
2214 IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
2215 IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2216 IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2217 IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
2218 IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
82b655bf 2219 IOCTL_INFO_FNC(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
4b1e2e4d
HV
2220 IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2221 IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2222 IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
d84f2d94
HV
2223 IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2224 IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2225 IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
865c4642
HV
2226 IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
2227 IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
d806f2df
HV
2228 IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
2229 IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
4b1e2e4d 2230 IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
be6c64e4 2231 IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
59076122
HV
2232 IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2233 IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
d0547cba
HV
2234 IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2235 IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
458aa4a6 2236 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 2237 IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
efbceecd
HV
2238 IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2239 IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
9bc31633 2240 IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
458aa4a6
HV
2241 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2242 IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
d806f2df
HV
2243 IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
2244 IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2245 IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2246 IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2247 IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
f16f77b0
HV
2248 IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2249 IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
4b1e2e4d 2250 IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
efc626b0
HV
2251 IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
2252 IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
458aa4a6
HV
2253 IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2254 IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2255 IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
5a5adf6b
HV
2256 IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2257 IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
efc626b0
HV
2258 IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
2259 IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
a1acb8f9 2260 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 2261 IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
96b03d2a 2262 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 2263 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
2264};
2265#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2266
2267bool v4l2_is_known_ioctl(unsigned int cmd)
2268{
2269 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2270 return false;
2271 return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2272}
2273
5a5adf6b
HV
2274struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
2275{
2276 if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2277 return vdev->lock;
2278 if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
2279 return NULL;
2280 if (vdev->queue && vdev->queue->lock &&
2281 (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2282 return vdev->queue->lock;
2283 return vdev->lock;
2284}
2285
4839e6c2
HV
2286/* Common ioctl debug function. This function can be used by
2287 external ioctl messages as well as internal V4L ioctl */
4a085168 2288void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
4839e6c2 2289{
86748f35 2290 const char *dir, *type;
4839e6c2 2291
4a085168
HV
2292 if (prefix)
2293 printk(KERN_DEBUG "%s: ", prefix);
2294
4839e6c2
HV
2295 switch (_IOC_TYPE(cmd)) {
2296 case 'd':
2297 type = "v4l2_int";
2298 break;
2299 case 'V':
2300 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2301 type = "v4l2";
2302 break;
2303 }
86748f35 2304 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
4839e6c2
HV
2305 return;
2306 default:
2307 type = "unknown";
86748f35 2308 break;
4839e6c2
HV
2309 }
2310
2311 switch (_IOC_DIR(cmd)) {
2312 case _IOC_NONE: dir = "--"; break;
2313 case _IOC_READ: dir = "r-"; break;
2314 case _IOC_WRITE: dir = "-w"; break;
2315 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2316 default: dir = "*ERR*"; break;
2317 }
86748f35 2318 pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
4839e6c2
HV
2319 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2320}
2321EXPORT_SYMBOL(v4l_printk_ioctl);
2322
069b7479 2323static long __video_do_ioctl(struct file *file,
35ea11ff
HV
2324 unsigned int cmd, void *arg)
2325{
2326 struct video_device *vfd = video_devdata(file);
a399810c 2327 const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
86748f35
HV
2328 bool write_only = false;
2329 struct v4l2_ioctl_info default_info;
2330 const struct v4l2_ioctl_info *info;
d5fbf32f 2331 void *fh = file->private_data;
99cd47bc 2332 struct v4l2_fh *vfh = NULL;
80131fe0 2333 int debug = vfd->debug;
9190d191 2334 long ret = -ENOTTY;
35ea11ff 2335
6a717883 2336 if (ops == NULL) {
4a085168
HV
2337 pr_warn("%s: has no ioctl_ops.\n",
2338 video_device_node_name(vfd));
9190d191 2339 return ret;
6a717883
HV
2340 }
2341
b7284bb0 2342 if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
99cd47bc
HV
2343 vfh = file->private_data;
2344
48ea0be0 2345 if (v4l2_is_known_ioctl(cmd)) {
86748f35 2346 info = &v4l2_ioctls[_IOC_NR(cmd)];
48ea0be0
HV
2347
2348 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2349 !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
86748f35 2350 goto done;
4b902fec 2351
b7284bb0 2352 if (vfh && (info->flags & INFO_FL_PRIO)) {
4b902fec
HV
2353 ret = v4l2_prio_check(vfd->prio, vfh->prio);
2354 if (ret)
86748f35 2355 goto done;
4b902fec 2356 }
86748f35
HV
2357 } else {
2358 default_info.ioctl = cmd;
2359 default_info.flags = 0;
f18d8e07 2360 default_info.debug = v4l_print_default;
86748f35 2361 info = &default_info;
48ea0be0
HV
2362 }
2363
86748f35 2364 write_only = _IOC_DIR(cmd) == _IOC_WRITE;
86748f35
HV
2365 if (info->flags & INFO_FL_STD) {
2366 typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2367 const void *p = vfd->ioctl_ops;
26ddcbcc 2368 const vidioc_op *vidioc = p + info->u.offset;
86748f35
HV
2369
2370 ret = (*vidioc)(file, fh, arg);
86748f35 2371 } else if (info->flags & INFO_FL_FUNC) {
26ddcbcc 2372 ret = info->u.func(ops, file, fh, arg);
f18d8e07
HV
2373 } else if (!ops->vidioc_default) {
2374 ret = -ENOTTY;
2375 } else {
2376 ret = ops->vidioc_default(file, fh,
b7284bb0 2377 vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
f18d8e07 2378 cmd, arg);
48ea0be0 2379 }
99cd47bc 2380
86748f35 2381done:
80131fe0 2382 if (debug) {
4a085168 2383 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
86748f35 2384 if (ret < 0)
505d04bd
HV
2385 pr_cont(": error %ld", ret);
2386 if (debug == V4L2_DEBUG_IOCTL)
86748f35 2387 pr_cont("\n");
86748f35
HV
2388 else if (_IOC_DIR(cmd) == _IOC_NONE)
2389 info->debug(arg, write_only);
2390 else {
2391 pr_cont(": ");
2392 info->debug(arg, write_only);
35ea11ff
HV
2393 }
2394 }
2395
2396 return ret;
2397}
2398
d14e6d76 2399static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
ba2d35c1 2400 void __user **user_ptr, void ***kernel_ptr)
d14e6d76
PO
2401{
2402 int ret = 0;
2403
2404 switch (cmd) {
96b1a702 2405 case VIDIOC_PREPARE_BUF:
d14e6d76
PO
2406 case VIDIOC_QUERYBUF:
2407 case VIDIOC_QBUF:
2408 case VIDIOC_DQBUF: {
2409 struct v4l2_buffer *buf = parg;
2410
2411 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2412 if (buf->length > VIDEO_MAX_PLANES) {
2413 ret = -EINVAL;
2414 break;
2415 }
2416 *user_ptr = (void __user *)buf->m.planes;
ba2d35c1 2417 *kernel_ptr = (void **)&buf->m.planes;
d14e6d76
PO
2418 *array_size = sizeof(struct v4l2_plane) * buf->length;
2419 ret = 1;
2420 }
2421 break;
2422 }
2423
dd519bb3
HV
2424 case VIDIOC_G_EDID:
2425 case VIDIOC_S_EDID: {
2426 struct v4l2_edid *edid = parg;
ed45ce2c
HV
2427
2428 if (edid->blocks) {
1b8b10cc
HV
2429 if (edid->blocks > 256) {
2430 ret = -EINVAL;
2431 break;
2432 }
ed45ce2c 2433 *user_ptr = (void __user *)edid->edid;
ba2d35c1 2434 *kernel_ptr = (void **)&edid->edid;
ed45ce2c
HV
2435 *array_size = edid->blocks * 128;
2436 ret = 1;
2437 }
2438 break;
2439 }
2440
d14e6d76
PO
2441 case VIDIOC_S_EXT_CTRLS:
2442 case VIDIOC_G_EXT_CTRLS:
2443 case VIDIOC_TRY_EXT_CTRLS: {
2444 struct v4l2_ext_controls *ctrls = parg;
2445
2446 if (ctrls->count != 0) {
6c06108b
DC
2447 if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2448 ret = -EINVAL;
2449 break;
2450 }
d14e6d76 2451 *user_ptr = (void __user *)ctrls->controls;
ba2d35c1 2452 *kernel_ptr = (void **)&ctrls->controls;
d14e6d76
PO
2453 *array_size = sizeof(struct v4l2_ext_control)
2454 * ctrls->count;
2455 ret = 1;
2456 }
2457 break;
2458 }
2459 }
2460
2461 return ret;
2462}
2463
fc0a8079
LP
2464long
2465video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2466 v4l2_kioctl func)
35ea11ff
HV
2467{
2468 char sbuf[128];
2469 void *mbuf = NULL;
1d94aa36 2470 void *parg = (void *)arg;
069b7479 2471 long err = -EINVAL;
d14e6d76
PO
2472 bool has_array_args;
2473 size_t array_size = 0;
35ea11ff 2474 void __user *user_ptr = NULL;
d14e6d76 2475 void **kernel_ptr = NULL;
35ea11ff 2476
35ea11ff 2477 /* Copy arguments into temp kernel buffer */
337f9d20 2478 if (_IOC_DIR(cmd) != _IOC_NONE) {
35ea11ff
HV
2479 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2480 parg = sbuf;
2481 } else {
2482 /* too big to allocate from stack */
2483 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2484 if (NULL == mbuf)
2485 return -ENOMEM;
2486 parg = mbuf;
2487 }
2488
2489 err = -EFAULT;
19c96e4b 2490 if (_IOC_DIR(cmd) & _IOC_WRITE) {
27cd2aba
HV
2491 unsigned int n = _IOC_SIZE(cmd);
2492
2493 /*
2494 * In some cases, only a few fields are used as input,
2495 * i.e. when the app sets "index" and then the driver
2496 * fills in the rest of the structure for the thing
2497 * with that index. We only need to copy up the first
2498 * non-input field.
2499 */
2500 if (v4l2_is_known_ioctl(cmd)) {
2501 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2502 if (flags & INFO_FL_CLEAR_MASK)
2503 n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2504 }
19c96e4b
TP
2505
2506 if (copy_from_user(parg, (void __user *)arg, n))
35ea11ff 2507 goto out;
19c96e4b
TP
2508
2509 /* zero out anything we don't copy from userspace */
2510 if (n < _IOC_SIZE(cmd))
2511 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
337f9d20
TP
2512 } else {
2513 /* read-only ioctl */
2514 memset(parg, 0, _IOC_SIZE(cmd));
19c96e4b 2515 }
35ea11ff
HV
2516 }
2517
d14e6d76
PO
2518 err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2519 if (err < 0)
2520 goto out;
2521 has_array_args = err;
35ea11ff 2522
d14e6d76
PO
2523 if (has_array_args) {
2524 /*
2525 * When adding new types of array args, make sure that the
2526 * parent argument to ioctl (which contains the pointer to the
2527 * array) fits into sbuf (so that mbuf will still remain
2528 * unused up to here).
2529 */
2530 mbuf = kmalloc(array_size, GFP_KERNEL);
2531 err = -ENOMEM;
2532 if (NULL == mbuf)
2533 goto out_array_args;
2534 err = -EFAULT;
2535 if (copy_from_user(mbuf, user_ptr, array_size))
2536 goto out_array_args;
2537 *kernel_ptr = mbuf;
35ea11ff
HV
2538 }
2539
2540 /* Handles IOCTL */
fc0a8079 2541 err = func(file, cmd, parg);
35ea11ff 2542 if (err == -ENOIOCTLCMD)
02bbb814 2543 err = -ENOTTY;
aa32f4c0
HV
2544 if (err == 0) {
2545 if (cmd == VIDIOC_DQBUF)
2546 trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
2547 else if (cmd == VIDIOC_QBUF)
2548 trace_v4l2_qbuf(video_devdata(file)->minor, parg);
2549 }
35ea11ff 2550
d14e6d76 2551 if (has_array_args) {
ba2d35c1 2552 *kernel_ptr = (void __force *)user_ptr;
d14e6d76 2553 if (copy_to_user(user_ptr, mbuf, array_size))
35ea11ff 2554 err = -EFAULT;
d14e6d76 2555 goto out_array_args;
35ea11ff 2556 }
5d7758ee
HV
2557 /* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2558 results that must be returned. */
2559 if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
35ea11ff
HV
2560 goto out;
2561
d14e6d76 2562out_array_args:
35ea11ff
HV
2563 /* Copy results into user buffer */
2564 switch (_IOC_DIR(cmd)) {
2565 case _IOC_READ:
2566 case (_IOC_WRITE | _IOC_READ):
2567 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2568 err = -EFAULT;
2569 break;
2570 }
2571
2572out:
2573 kfree(mbuf);
2574 return err;
2575}
fc0a8079
LP
2576EXPORT_SYMBOL(video_usercopy);
2577
2578long video_ioctl2(struct file *file,
2579 unsigned int cmd, unsigned long arg)
2580{
2581 return video_usercopy(file, cmd, arg, __video_do_ioctl);
2582}
8a522c91 2583EXPORT_SYMBOL(video_ioctl2);
This page took 0.632447 seconds and 5 git commands to generate.