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