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