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