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