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