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