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