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