Merge tag 'usb-5.0-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
[sfrench/cifs-2.6.git] / drivers / media / v4l2-core / v4l2-ioctl.c
1 /*
2  * Video capture interface for Linux version 2
3  *
4  * A generic framework to process V4L2 ioctl commands.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  *
11  * Authors:     Alan Cox, <alan@lxorguk.ukuu.org.uk> (version 1)
12  *              Mauro Carvalho Chehab <mchehab@kernel.org> (version 2)
13  */
14
15 #include <linux/mm.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/version.h>
21
22 #include <linux/videodev2.h>
23
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-fh.h>
28 #include <media/v4l2-event.h>
29 #include <media/v4l2-device.h>
30 #include <media/videobuf2-v4l2.h>
31 #include <media/v4l2-mc.h>
32 #include <media/v4l2-mem2mem.h>
33
34 #include <trace/events/v4l2.h>
35
36 /* Zero out the end of the struct pointed to by p.  Everything after, but
37  * not including, the specified field is cleared. */
38 #define CLEAR_AFTER_FIELD(p, field) \
39         memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
40         0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
41
42 #define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
43
44 struct std_descr {
45         v4l2_std_id std;
46         const char *descr;
47 };
48
49 static const struct std_descr standards[] = {
50         { V4L2_STD_NTSC,        "NTSC"      },
51         { V4L2_STD_NTSC_M,      "NTSC-M"    },
52         { V4L2_STD_NTSC_M_JP,   "NTSC-M-JP" },
53         { V4L2_STD_NTSC_M_KR,   "NTSC-M-KR" },
54         { V4L2_STD_NTSC_443,    "NTSC-443"  },
55         { V4L2_STD_PAL,         "PAL"       },
56         { V4L2_STD_PAL_BG,      "PAL-BG"    },
57         { V4L2_STD_PAL_B,       "PAL-B"     },
58         { V4L2_STD_PAL_B1,      "PAL-B1"    },
59         { V4L2_STD_PAL_G,       "PAL-G"     },
60         { V4L2_STD_PAL_H,       "PAL-H"     },
61         { V4L2_STD_PAL_I,       "PAL-I"     },
62         { V4L2_STD_PAL_DK,      "PAL-DK"    },
63         { V4L2_STD_PAL_D,       "PAL-D"     },
64         { V4L2_STD_PAL_D1,      "PAL-D1"    },
65         { V4L2_STD_PAL_K,       "PAL-K"     },
66         { V4L2_STD_PAL_M,       "PAL-M"     },
67         { V4L2_STD_PAL_N,       "PAL-N"     },
68         { V4L2_STD_PAL_Nc,      "PAL-Nc"    },
69         { V4L2_STD_PAL_60,      "PAL-60"    },
70         { V4L2_STD_SECAM,       "SECAM"     },
71         { V4L2_STD_SECAM_B,     "SECAM-B"   },
72         { V4L2_STD_SECAM_G,     "SECAM-G"   },
73         { V4L2_STD_SECAM_H,     "SECAM-H"   },
74         { V4L2_STD_SECAM_DK,    "SECAM-DK"  },
75         { V4L2_STD_SECAM_D,     "SECAM-D"   },
76         { V4L2_STD_SECAM_K,     "SECAM-K"   },
77         { V4L2_STD_SECAM_K1,    "SECAM-K1"  },
78         { V4L2_STD_SECAM_L,     "SECAM-L"   },
79         { V4L2_STD_SECAM_LC,    "SECAM-Lc"  },
80         { 0,                    "Unknown"   }
81 };
82
83 /* video4linux standard ID conversion to standard name
84  */
85 const char *v4l2_norm_to_name(v4l2_std_id id)
86 {
87         u32 myid = id;
88         int i;
89
90         /* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
91            64 bit comparations. So, on that architecture, with some gcc
92            variants, compilation fails. Currently, the max value is 30bit wide.
93          */
94         BUG_ON(myid != id);
95
96         for (i = 0; standards[i].std; i++)
97                 if (myid == standards[i].std)
98                         break;
99         return standards[i].descr;
100 }
101 EXPORT_SYMBOL(v4l2_norm_to_name);
102
103 /* Returns frame period for the given standard */
104 void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
105 {
106         if (id & V4L2_STD_525_60) {
107                 frameperiod->numerator = 1001;
108                 frameperiod->denominator = 30000;
109         } else {
110                 frameperiod->numerator = 1;
111                 frameperiod->denominator = 25;
112         }
113 }
114 EXPORT_SYMBOL(v4l2_video_std_frame_period);
115
116 /* Fill in the fields of a v4l2_standard structure according to the
117    'id' and 'transmission' parameters.  Returns negative on error.  */
118 int v4l2_video_std_construct(struct v4l2_standard *vs,
119                              int id, const char *name)
120 {
121         vs->id = id;
122         v4l2_video_std_frame_period(id, &vs->frameperiod);
123         vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
124         strscpy(vs->name, name, sizeof(vs->name));
125         return 0;
126 }
127 EXPORT_SYMBOL(v4l2_video_std_construct);
128
129 /* Fill in the fields of a v4l2_standard structure according to the
130  * 'id' and 'vs->index' parameters. Returns negative on error. */
131 int v4l_video_std_enumstd(struct v4l2_standard *vs, v4l2_std_id id)
132 {
133         v4l2_std_id curr_id = 0;
134         unsigned int index = vs->index, i, j = 0;
135         const char *descr = "";
136
137         /* Return -ENODATA if the id for the current input
138            or output is 0, meaning that it doesn't support this API. */
139         if (id == 0)
140                 return -ENODATA;
141
142         /* Return norm array in a canonical way */
143         for (i = 0; i <= index && id; i++) {
144                 /* last std value in the standards array is 0, so this
145                    while always ends there since (id & 0) == 0. */
146                 while ((id & standards[j].std) != standards[j].std)
147                         j++;
148                 curr_id = standards[j].std;
149                 descr = standards[j].descr;
150                 j++;
151                 if (curr_id == 0)
152                         break;
153                 if (curr_id != V4L2_STD_PAL &&
154                                 curr_id != V4L2_STD_SECAM &&
155                                 curr_id != V4L2_STD_NTSC)
156                         id &= ~curr_id;
157         }
158         if (i <= index)
159                 return -EINVAL;
160
161         v4l2_video_std_construct(vs, curr_id, descr);
162         return 0;
163 }
164
165 /* ----------------------------------------------------------------- */
166 /* some arrays for pretty-printing debug messages of enum types      */
167
168 const char *v4l2_field_names[] = {
169         [V4L2_FIELD_ANY]        = "any",
170         [V4L2_FIELD_NONE]       = "none",
171         [V4L2_FIELD_TOP]        = "top",
172         [V4L2_FIELD_BOTTOM]     = "bottom",
173         [V4L2_FIELD_INTERLACED] = "interlaced",
174         [V4L2_FIELD_SEQ_TB]     = "seq-tb",
175         [V4L2_FIELD_SEQ_BT]     = "seq-bt",
176         [V4L2_FIELD_ALTERNATE]  = "alternate",
177         [V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
178         [V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
179 };
180 EXPORT_SYMBOL(v4l2_field_names);
181
182 const char *v4l2_type_names[] = {
183         [0]                                = "0",
184         [V4L2_BUF_TYPE_VIDEO_CAPTURE]      = "vid-cap",
185         [V4L2_BUF_TYPE_VIDEO_OVERLAY]      = "vid-overlay",
186         [V4L2_BUF_TYPE_VIDEO_OUTPUT]       = "vid-out",
187         [V4L2_BUF_TYPE_VBI_CAPTURE]        = "vbi-cap",
188         [V4L2_BUF_TYPE_VBI_OUTPUT]         = "vbi-out",
189         [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
190         [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT]  = "sliced-vbi-out",
191         [V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
192         [V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
193         [V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
194         [V4L2_BUF_TYPE_SDR_CAPTURE]        = "sdr-cap",
195         [V4L2_BUF_TYPE_SDR_OUTPUT]         = "sdr-out",
196         [V4L2_BUF_TYPE_META_CAPTURE]       = "meta-cap",
197         [V4L2_BUF_TYPE_META_OUTPUT]        = "meta-out",
198 };
199 EXPORT_SYMBOL(v4l2_type_names);
200
201 static const char *v4l2_memory_names[] = {
202         [V4L2_MEMORY_MMAP]    = "mmap",
203         [V4L2_MEMORY_USERPTR] = "userptr",
204         [V4L2_MEMORY_OVERLAY] = "overlay",
205         [V4L2_MEMORY_DMABUF] = "dmabuf",
206 };
207
208 #define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
209
210 /* ------------------------------------------------------------------ */
211 /* debug help functions                                               */
212
213 static void v4l_print_querycap(const void *arg, bool write_only)
214 {
215         const struct v4l2_capability *p = arg;
216
217         pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, capabilities=0x%08x, device_caps=0x%08x\n",
218                 (int)sizeof(p->driver), p->driver,
219                 (int)sizeof(p->card), p->card,
220                 (int)sizeof(p->bus_info), p->bus_info,
221                 p->version, p->capabilities, p->device_caps);
222 }
223
224 static void v4l_print_enuminput(const void *arg, bool write_only)
225 {
226         const struct v4l2_input *p = arg;
227
228         pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
229                 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
230                 p->tuner, (unsigned long long)p->std, p->status,
231                 p->capabilities);
232 }
233
234 static void v4l_print_enumoutput(const void *arg, bool write_only)
235 {
236         const struct v4l2_output *p = arg;
237
238         pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
239                 p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
240                 p->modulator, (unsigned long long)p->std, p->capabilities);
241 }
242
243 static void v4l_print_audio(const void *arg, bool write_only)
244 {
245         const struct v4l2_audio *p = arg;
246
247         if (write_only)
248                 pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
249         else
250                 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
251                         p->index, (int)sizeof(p->name), p->name,
252                         p->capability, p->mode);
253 }
254
255 static void v4l_print_audioout(const void *arg, bool write_only)
256 {
257         const struct v4l2_audioout *p = arg;
258
259         if (write_only)
260                 pr_cont("index=%u\n", p->index);
261         else
262                 pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
263                         p->index, (int)sizeof(p->name), p->name,
264                         p->capability, p->mode);
265 }
266
267 static void v4l_print_fmtdesc(const void *arg, bool write_only)
268 {
269         const struct v4l2_fmtdesc *p = arg;
270
271         pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%.*s'\n",
272                 p->index, prt_names(p->type, v4l2_type_names),
273                 p->flags, (p->pixelformat & 0xff),
274                 (p->pixelformat >>  8) & 0xff,
275                 (p->pixelformat >> 16) & 0xff,
276                 (p->pixelformat >> 24) & 0xff,
277                 (int)sizeof(p->description), p->description);
278 }
279
280 static void v4l_print_format(const void *arg, bool write_only)
281 {
282         const struct v4l2_format *p = arg;
283         const struct v4l2_pix_format *pix;
284         const struct v4l2_pix_format_mplane *mp;
285         const struct v4l2_vbi_format *vbi;
286         const struct v4l2_sliced_vbi_format *sliced;
287         const struct v4l2_window *win;
288         const struct v4l2_sdr_format *sdr;
289         const struct v4l2_meta_format *meta;
290         u32 planes;
291         unsigned i;
292
293         pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
294         switch (p->type) {
295         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
296         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
297                 pix = &p->fmt.pix;
298                 pr_cont(", width=%u, height=%u, pixelformat=%c%c%c%c, field=%s, bytesperline=%u, sizeimage=%u, colorspace=%d, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
299                         pix->width, pix->height,
300                         (pix->pixelformat & 0xff),
301                         (pix->pixelformat >>  8) & 0xff,
302                         (pix->pixelformat >> 16) & 0xff,
303                         (pix->pixelformat >> 24) & 0xff,
304                         prt_names(pix->field, v4l2_field_names),
305                         pix->bytesperline, pix->sizeimage,
306                         pix->colorspace, pix->flags, pix->ycbcr_enc,
307                         pix->quantization, pix->xfer_func);
308                 break;
309         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
310         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
311                 mp = &p->fmt.pix_mp;
312                 pr_cont(", width=%u, height=%u, format=%c%c%c%c, field=%s, colorspace=%d, num_planes=%u, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
313                         mp->width, mp->height,
314                         (mp->pixelformat & 0xff),
315                         (mp->pixelformat >>  8) & 0xff,
316                         (mp->pixelformat >> 16) & 0xff,
317                         (mp->pixelformat >> 24) & 0xff,
318                         prt_names(mp->field, v4l2_field_names),
319                         mp->colorspace, mp->num_planes, mp->flags,
320                         mp->ycbcr_enc, mp->quantization, mp->xfer_func);
321                 planes = min_t(u32, mp->num_planes, VIDEO_MAX_PLANES);
322                 for (i = 0; i < planes; i++)
323                         printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
324                                         mp->plane_fmt[i].bytesperline,
325                                         mp->plane_fmt[i].sizeimage);
326                 break;
327         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
328         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
329                 win = &p->fmt.win;
330                 /* Note: we can't print the clip list here since the clips
331                  * pointer is a userspace pointer, not a kernelspace
332                  * pointer. */
333                 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",
334                         win->w.width, win->w.height, win->w.left, win->w.top,
335                         prt_names(win->field, v4l2_field_names),
336                         win->chromakey, win->clipcount, win->clips,
337                         win->bitmap, win->global_alpha);
338                 break;
339         case V4L2_BUF_TYPE_VBI_CAPTURE:
340         case V4L2_BUF_TYPE_VBI_OUTPUT:
341                 vbi = &p->fmt.vbi;
342                 pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
343                         vbi->sampling_rate, vbi->offset,
344                         vbi->samples_per_line,
345                         (vbi->sample_format & 0xff),
346                         (vbi->sample_format >>  8) & 0xff,
347                         (vbi->sample_format >> 16) & 0xff,
348                         (vbi->sample_format >> 24) & 0xff,
349                         vbi->start[0], vbi->start[1],
350                         vbi->count[0], vbi->count[1]);
351                 break;
352         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
353         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
354                 sliced = &p->fmt.sliced;
355                 pr_cont(", service_set=0x%08x, io_size=%d\n",
356                                 sliced->service_set, sliced->io_size);
357                 for (i = 0; i < 24; i++)
358                         printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
359                                 sliced->service_lines[0][i],
360                                 sliced->service_lines[1][i]);
361                 break;
362         case V4L2_BUF_TYPE_SDR_CAPTURE:
363         case V4L2_BUF_TYPE_SDR_OUTPUT:
364                 sdr = &p->fmt.sdr;
365                 pr_cont(", pixelformat=%c%c%c%c\n",
366                         (sdr->pixelformat >>  0) & 0xff,
367                         (sdr->pixelformat >>  8) & 0xff,
368                         (sdr->pixelformat >> 16) & 0xff,
369                         (sdr->pixelformat >> 24) & 0xff);
370                 break;
371         case V4L2_BUF_TYPE_META_CAPTURE:
372         case V4L2_BUF_TYPE_META_OUTPUT:
373                 meta = &p->fmt.meta;
374                 pr_cont(", dataformat=%c%c%c%c, buffersize=%u\n",
375                         (meta->dataformat >>  0) & 0xff,
376                         (meta->dataformat >>  8) & 0xff,
377                         (meta->dataformat >> 16) & 0xff,
378                         (meta->dataformat >> 24) & 0xff,
379                         meta->buffersize);
380                 break;
381         }
382 }
383
384 static void v4l_print_framebuffer(const void *arg, bool write_only)
385 {
386         const struct v4l2_framebuffer *p = arg;
387
388         pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, height=%u, pixelformat=%c%c%c%c, bytesperline=%u, sizeimage=%u, colorspace=%d\n",
389                         p->capability, p->flags, p->base,
390                         p->fmt.width, p->fmt.height,
391                         (p->fmt.pixelformat & 0xff),
392                         (p->fmt.pixelformat >>  8) & 0xff,
393                         (p->fmt.pixelformat >> 16) & 0xff,
394                         (p->fmt.pixelformat >> 24) & 0xff,
395                         p->fmt.bytesperline, p->fmt.sizeimage,
396                         p->fmt.colorspace);
397 }
398
399 static void v4l_print_buftype(const void *arg, bool write_only)
400 {
401         pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
402 }
403
404 static void v4l_print_modulator(const void *arg, bool write_only)
405 {
406         const struct v4l2_modulator *p = arg;
407
408         if (write_only)
409                 pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
410         else
411                 pr_cont("index=%u, name=%.*s, capability=0x%x, rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
412                         p->index, (int)sizeof(p->name), p->name, p->capability,
413                         p->rangelow, p->rangehigh, p->txsubchans);
414 }
415
416 static void v4l_print_tuner(const void *arg, bool write_only)
417 {
418         const struct v4l2_tuner *p = arg;
419
420         if (write_only)
421                 pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
422         else
423                 pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, rangelow=%u, rangehigh=%u, signal=%u, afc=%d, rxsubchans=0x%x, audmode=%u\n",
424                         p->index, (int)sizeof(p->name), p->name, p->type,
425                         p->capability, p->rangelow,
426                         p->rangehigh, p->signal, p->afc,
427                         p->rxsubchans, p->audmode);
428 }
429
430 static void v4l_print_frequency(const void *arg, bool write_only)
431 {
432         const struct v4l2_frequency *p = arg;
433
434         pr_cont("tuner=%u, type=%u, frequency=%u\n",
435                                 p->tuner, p->type, p->frequency);
436 }
437
438 static void v4l_print_standard(const void *arg, bool write_only)
439 {
440         const struct v4l2_standard *p = arg;
441
442         pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, framelines=%u\n",
443                 p->index,
444                 (unsigned long long)p->id, (int)sizeof(p->name), p->name,
445                 p->frameperiod.numerator,
446                 p->frameperiod.denominator,
447                 p->framelines);
448 }
449
450 static void v4l_print_std(const void *arg, bool write_only)
451 {
452         pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
453 }
454
455 static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
456 {
457         const struct v4l2_hw_freq_seek *p = arg;
458
459         pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, rangelow=%u, rangehigh=%u\n",
460                 p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
461                 p->rangelow, p->rangehigh);
462 }
463
464 static void v4l_print_requestbuffers(const void *arg, bool write_only)
465 {
466         const struct v4l2_requestbuffers *p = arg;
467
468         pr_cont("count=%d, type=%s, memory=%s\n",
469                 p->count,
470                 prt_names(p->type, v4l2_type_names),
471                 prt_names(p->memory, v4l2_memory_names));
472 }
473
474 static void v4l_print_buffer(const void *arg, bool write_only)
475 {
476         const struct v4l2_buffer *p = arg;
477         const struct v4l2_timecode *tc = &p->timecode;
478         const struct v4l2_plane *plane;
479         int i;
480
481         pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, request_fd=%d, flags=0x%08x, field=%s, sequence=%d, memory=%s",
482                         p->timestamp.tv_sec / 3600,
483                         (int)(p->timestamp.tv_sec / 60) % 60,
484                         (int)(p->timestamp.tv_sec % 60),
485                         (long)p->timestamp.tv_usec,
486                         p->index,
487                         prt_names(p->type, v4l2_type_names), p->request_fd,
488                         p->flags, prt_names(p->field, v4l2_field_names),
489                         p->sequence, prt_names(p->memory, v4l2_memory_names));
490
491         if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
492                 pr_cont("\n");
493                 for (i = 0; i < p->length; ++i) {
494                         plane = &p->m.planes[i];
495                         printk(KERN_DEBUG
496                                 "plane %d: bytesused=%d, data_offset=0x%08x, offset/userptr=0x%lx, length=%d\n",
497                                 i, plane->bytesused, plane->data_offset,
498                                 plane->m.userptr, plane->length);
499                 }
500         } else {
501                 pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
502                         p->bytesused, p->m.userptr, p->length);
503         }
504
505         printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, flags=0x%08x, frames=%d, userbits=0x%08x\n",
506                         tc->hours, tc->minutes, tc->seconds,
507                         tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
508 }
509
510 static void v4l_print_exportbuffer(const void *arg, bool write_only)
511 {
512         const struct v4l2_exportbuffer *p = arg;
513
514         pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
515                 p->fd, prt_names(p->type, v4l2_type_names),
516                 p->index, p->plane, p->flags);
517 }
518
519 static void v4l_print_create_buffers(const void *arg, bool write_only)
520 {
521         const struct v4l2_create_buffers *p = arg;
522
523         pr_cont("index=%d, count=%d, memory=%s, ",
524                         p->index, p->count,
525                         prt_names(p->memory, v4l2_memory_names));
526         v4l_print_format(&p->format, write_only);
527 }
528
529 static void v4l_print_streamparm(const void *arg, bool write_only)
530 {
531         const struct v4l2_streamparm *p = arg;
532
533         pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
534
535         if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
536             p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
537                 const struct v4l2_captureparm *c = &p->parm.capture;
538
539                 pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, extendedmode=%d, readbuffers=%d\n",
540                         c->capability, c->capturemode,
541                         c->timeperframe.numerator, c->timeperframe.denominator,
542                         c->extendedmode, c->readbuffers);
543         } else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
544                    p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
545                 const struct v4l2_outputparm *c = &p->parm.output;
546
547                 pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, extendedmode=%d, writebuffers=%d\n",
548                         c->capability, c->outputmode,
549                         c->timeperframe.numerator, c->timeperframe.denominator,
550                         c->extendedmode, c->writebuffers);
551         } else {
552                 pr_cont("\n");
553         }
554 }
555
556 static void v4l_print_queryctrl(const void *arg, bool write_only)
557 {
558         const struct v4l2_queryctrl *p = arg;
559
560         pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, step=%d, default=%d, flags=0x%08x\n",
561                         p->id, p->type, (int)sizeof(p->name), p->name,
562                         p->minimum, p->maximum,
563                         p->step, p->default_value, p->flags);
564 }
565
566 static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
567 {
568         const struct v4l2_query_ext_ctrl *p = arg;
569
570         pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%lld/%lld, step=%lld, default=%lld, flags=0x%08x, elem_size=%u, elems=%u, nr_of_dims=%u, dims=%u,%u,%u,%u\n",
571                         p->id, p->type, (int)sizeof(p->name), p->name,
572                         p->minimum, p->maximum,
573                         p->step, p->default_value, p->flags,
574                         p->elem_size, p->elems, p->nr_of_dims,
575                         p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
576 }
577
578 static void v4l_print_querymenu(const void *arg, bool write_only)
579 {
580         const struct v4l2_querymenu *p = arg;
581
582         pr_cont("id=0x%x, index=%d\n", p->id, p->index);
583 }
584
585 static void v4l_print_control(const void *arg, bool write_only)
586 {
587         const struct v4l2_control *p = arg;
588
589         pr_cont("id=0x%x, value=%d\n", p->id, p->value);
590 }
591
592 static void v4l_print_ext_controls(const void *arg, bool write_only)
593 {
594         const struct v4l2_ext_controls *p = arg;
595         int i;
596
597         pr_cont("which=0x%x, count=%d, error_idx=%d, request_fd=%d",
598                         p->which, p->count, p->error_idx, p->request_fd);
599         for (i = 0; i < p->count; i++) {
600                 if (!p->controls[i].size)
601                         pr_cont(", id/val=0x%x/0x%x",
602                                 p->controls[i].id, p->controls[i].value);
603                 else
604                         pr_cont(", id/size=0x%x/%u",
605                                 p->controls[i].id, p->controls[i].size);
606         }
607         pr_cont("\n");
608 }
609
610 static void v4l_print_cropcap(const void *arg, bool write_only)
611 {
612         const struct v4l2_cropcap *p = arg;
613
614         pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, defrect wxh=%dx%d, x,y=%d,%d, pixelaspect %d/%d\n",
615                 prt_names(p->type, v4l2_type_names),
616                 p->bounds.width, p->bounds.height,
617                 p->bounds.left, p->bounds.top,
618                 p->defrect.width, p->defrect.height,
619                 p->defrect.left, p->defrect.top,
620                 p->pixelaspect.numerator, p->pixelaspect.denominator);
621 }
622
623 static void v4l_print_crop(const void *arg, bool write_only)
624 {
625         const struct v4l2_crop *p = arg;
626
627         pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
628                 prt_names(p->type, v4l2_type_names),
629                 p->c.width, p->c.height,
630                 p->c.left, p->c.top);
631 }
632
633 static void v4l_print_selection(const void *arg, bool write_only)
634 {
635         const struct v4l2_selection *p = arg;
636
637         pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
638                 prt_names(p->type, v4l2_type_names),
639                 p->target, p->flags,
640                 p->r.width, p->r.height, p->r.left, p->r.top);
641 }
642
643 static void v4l_print_jpegcompression(const void *arg, bool write_only)
644 {
645         const struct v4l2_jpegcompression *p = arg;
646
647         pr_cont("quality=%d, APPn=%d, APP_len=%d, COM_len=%d, jpeg_markers=0x%x\n",
648                 p->quality, p->APPn, p->APP_len,
649                 p->COM_len, p->jpeg_markers);
650 }
651
652 static void v4l_print_enc_idx(const void *arg, bool write_only)
653 {
654         const struct v4l2_enc_idx *p = arg;
655
656         pr_cont("entries=%d, entries_cap=%d\n",
657                         p->entries, p->entries_cap);
658 }
659
660 static void v4l_print_encoder_cmd(const void *arg, bool write_only)
661 {
662         const struct v4l2_encoder_cmd *p = arg;
663
664         pr_cont("cmd=%d, flags=0x%x\n",
665                         p->cmd, p->flags);
666 }
667
668 static void v4l_print_decoder_cmd(const void *arg, bool write_only)
669 {
670         const struct v4l2_decoder_cmd *p = arg;
671
672         pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
673
674         if (p->cmd == V4L2_DEC_CMD_START)
675                 pr_info("speed=%d, format=%u\n",
676                                 p->start.speed, p->start.format);
677         else if (p->cmd == V4L2_DEC_CMD_STOP)
678                 pr_info("pts=%llu\n", p->stop.pts);
679 }
680
681 static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
682 {
683         const struct v4l2_dbg_chip_info *p = arg;
684
685         pr_cont("type=%u, ", p->match.type);
686         if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
687                 pr_cont("name=%.*s, ",
688                                 (int)sizeof(p->match.name), p->match.name);
689         else
690                 pr_cont("addr=%u, ", p->match.addr);
691         pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
692 }
693
694 static void v4l_print_dbg_register(const void *arg, bool write_only)
695 {
696         const struct v4l2_dbg_register *p = arg;
697
698         pr_cont("type=%u, ", p->match.type);
699         if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
700                 pr_cont("name=%.*s, ",
701                                 (int)sizeof(p->match.name), p->match.name);
702         else
703                 pr_cont("addr=%u, ", p->match.addr);
704         pr_cont("reg=0x%llx, val=0x%llx\n",
705                         p->reg, p->val);
706 }
707
708 static void v4l_print_dv_timings(const void *arg, bool write_only)
709 {
710         const struct v4l2_dv_timings *p = arg;
711
712         switch (p->type) {
713         case V4L2_DV_BT_656_1120:
714                 pr_cont("type=bt-656/1120, interlaced=%u, pixelclock=%llu, width=%u, height=%u, polarities=0x%x, hfrontporch=%u, hsync=%u, hbackporch=%u, vfrontporch=%u, vsync=%u, vbackporch=%u, il_vfrontporch=%u, il_vsync=%u, il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
715                                 p->bt.interlaced, p->bt.pixelclock,
716                                 p->bt.width, p->bt.height,
717                                 p->bt.polarities, p->bt.hfrontporch,
718                                 p->bt.hsync, p->bt.hbackporch,
719                                 p->bt.vfrontporch, p->bt.vsync,
720                                 p->bt.vbackporch, p->bt.il_vfrontporch,
721                                 p->bt.il_vsync, p->bt.il_vbackporch,
722                                 p->bt.standards, p->bt.flags);
723                 break;
724         default:
725                 pr_cont("type=%d\n", p->type);
726                 break;
727         }
728 }
729
730 static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
731 {
732         const struct v4l2_enum_dv_timings *p = arg;
733
734         pr_cont("index=%u, ", p->index);
735         v4l_print_dv_timings(&p->timings, write_only);
736 }
737
738 static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
739 {
740         const struct v4l2_dv_timings_cap *p = arg;
741
742         switch (p->type) {
743         case V4L2_DV_BT_656_1120:
744                 pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
745                         p->bt.min_width, p->bt.max_width,
746                         p->bt.min_height, p->bt.max_height,
747                         p->bt.min_pixelclock, p->bt.max_pixelclock,
748                         p->bt.standards, p->bt.capabilities);
749                 break;
750         default:
751                 pr_cont("type=%u\n", p->type);
752                 break;
753         }
754 }
755
756 static void v4l_print_frmsizeenum(const void *arg, bool write_only)
757 {
758         const struct v4l2_frmsizeenum *p = arg;
759
760         pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
761                         p->index,
762                         (p->pixel_format & 0xff),
763                         (p->pixel_format >>  8) & 0xff,
764                         (p->pixel_format >> 16) & 0xff,
765                         (p->pixel_format >> 24) & 0xff,
766                         p->type);
767         switch (p->type) {
768         case V4L2_FRMSIZE_TYPE_DISCRETE:
769                 pr_cont(", wxh=%ux%u\n",
770                         p->discrete.width, p->discrete.height);
771                 break;
772         case V4L2_FRMSIZE_TYPE_STEPWISE:
773                 pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
774                                 p->stepwise.min_width,
775                                 p->stepwise.min_height,
776                                 p->stepwise.max_width,
777                                 p->stepwise.max_height,
778                                 p->stepwise.step_width,
779                                 p->stepwise.step_height);
780                 break;
781         case V4L2_FRMSIZE_TYPE_CONTINUOUS:
782                 /* fall through */
783         default:
784                 pr_cont("\n");
785                 break;
786         }
787 }
788
789 static void v4l_print_frmivalenum(const void *arg, bool write_only)
790 {
791         const struct v4l2_frmivalenum *p = arg;
792
793         pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
794                         p->index,
795                         (p->pixel_format & 0xff),
796                         (p->pixel_format >>  8) & 0xff,
797                         (p->pixel_format >> 16) & 0xff,
798                         (p->pixel_format >> 24) & 0xff,
799                         p->width, p->height, p->type);
800         switch (p->type) {
801         case V4L2_FRMIVAL_TYPE_DISCRETE:
802                 pr_cont(", fps=%d/%d\n",
803                                 p->discrete.numerator,
804                                 p->discrete.denominator);
805                 break;
806         case V4L2_FRMIVAL_TYPE_STEPWISE:
807                 pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
808                                 p->stepwise.min.numerator,
809                                 p->stepwise.min.denominator,
810                                 p->stepwise.max.numerator,
811                                 p->stepwise.max.denominator,
812                                 p->stepwise.step.numerator,
813                                 p->stepwise.step.denominator);
814                 break;
815         case V4L2_FRMIVAL_TYPE_CONTINUOUS:
816                 /* fall through */
817         default:
818                 pr_cont("\n");
819                 break;
820         }
821 }
822
823 static void v4l_print_event(const void *arg, bool write_only)
824 {
825         const struct v4l2_event *p = arg;
826         const struct v4l2_event_ctrl *c;
827
828         pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, timestamp=%lu.%9.9lu\n",
829                         p->type, p->pending, p->sequence, p->id,
830                         p->timestamp.tv_sec, p->timestamp.tv_nsec);
831         switch (p->type) {
832         case V4L2_EVENT_VSYNC:
833                 printk(KERN_DEBUG "field=%s\n",
834                         prt_names(p->u.vsync.field, v4l2_field_names));
835                 break;
836         case V4L2_EVENT_CTRL:
837                 c = &p->u.ctrl;
838                 printk(KERN_DEBUG "changes=0x%x, type=%u, ",
839                         c->changes, c->type);
840                 if (c->type == V4L2_CTRL_TYPE_INTEGER64)
841                         pr_cont("value64=%lld, ", c->value64);
842                 else
843                         pr_cont("value=%d, ", c->value);
844                 pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, default_value=%d\n",
845                         c->flags, c->minimum, c->maximum,
846                         c->step, c->default_value);
847                 break;
848         case V4L2_EVENT_FRAME_SYNC:
849                 pr_cont("frame_sequence=%u\n",
850                         p->u.frame_sync.frame_sequence);
851                 break;
852         }
853 }
854
855 static void v4l_print_event_subscription(const void *arg, bool write_only)
856 {
857         const struct v4l2_event_subscription *p = arg;
858
859         pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
860                         p->type, p->id, p->flags);
861 }
862
863 static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
864 {
865         const struct v4l2_sliced_vbi_cap *p = arg;
866         int i;
867
868         pr_cont("type=%s, service_set=0x%08x\n",
869                         prt_names(p->type, v4l2_type_names), p->service_set);
870         for (i = 0; i < 24; i++)
871                 printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
872                                 p->service_lines[0][i],
873                                 p->service_lines[1][i]);
874 }
875
876 static void v4l_print_freq_band(const void *arg, bool write_only)
877 {
878         const struct v4l2_frequency_band *p = arg;
879
880         pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, rangelow=%u, rangehigh=%u, modulation=0x%x\n",
881                         p->tuner, p->type, p->index,
882                         p->capability, p->rangelow,
883                         p->rangehigh, p->modulation);
884 }
885
886 static void v4l_print_edid(const void *arg, bool write_only)
887 {
888         const struct v4l2_edid *p = arg;
889
890         pr_cont("pad=%u, start_block=%u, blocks=%u\n",
891                 p->pad, p->start_block, p->blocks);
892 }
893
894 static void v4l_print_u32(const void *arg, bool write_only)
895 {
896         pr_cont("value=%u\n", *(const u32 *)arg);
897 }
898
899 static void v4l_print_newline(const void *arg, bool write_only)
900 {
901         pr_cont("\n");
902 }
903
904 static void v4l_print_default(const void *arg, bool write_only)
905 {
906         pr_cont("driver-specific ioctl\n");
907 }
908
909 static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
910 {
911         __u32 i;
912
913         /* zero the reserved fields */
914         c->reserved[0] = 0;
915         for (i = 0; i < c->count; i++)
916                 c->controls[i].reserved2[0] = 0;
917
918         /* V4L2_CID_PRIVATE_BASE cannot be used as control class
919            when using extended controls.
920            Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
921            is it allowed for backwards compatibility.
922          */
923         if (!allow_priv && c->which == V4L2_CID_PRIVATE_BASE)
924                 return 0;
925         if (!c->which)
926                 return 1;
927         /* Check that all controls are from the same control class. */
928         for (i = 0; i < c->count; i++) {
929                 if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) {
930                         c->error_idx = i;
931                         return 0;
932                 }
933         }
934         return 1;
935 }
936
937 static int check_fmt(struct file *file, enum v4l2_buf_type type)
938 {
939         struct video_device *vfd = video_devdata(file);
940         const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
941         bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
942         bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
943         bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
944         bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
945         bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
946         bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
947
948         if (ops == NULL)
949                 return -EINVAL;
950
951         switch (type) {
952         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
953                 if ((is_vid || is_tch) && is_rx &&
954                     (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
955                         return 0;
956                 break;
957         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
958                 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
959                         return 0;
960                 break;
961         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
962                 if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
963                         return 0;
964                 break;
965         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
966                 if (is_vid && is_tx &&
967                     (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
968                         return 0;
969                 break;
970         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
971                 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
972                         return 0;
973                 break;
974         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
975                 if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
976                         return 0;
977                 break;
978         case V4L2_BUF_TYPE_VBI_CAPTURE:
979                 if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
980                         return 0;
981                 break;
982         case V4L2_BUF_TYPE_VBI_OUTPUT:
983                 if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
984                         return 0;
985                 break;
986         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
987                 if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
988                         return 0;
989                 break;
990         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
991                 if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
992                         return 0;
993                 break;
994         case V4L2_BUF_TYPE_SDR_CAPTURE:
995                 if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
996                         return 0;
997                 break;
998         case V4L2_BUF_TYPE_SDR_OUTPUT:
999                 if (is_sdr && is_tx && ops->vidioc_g_fmt_sdr_out)
1000                         return 0;
1001                 break;
1002         case V4L2_BUF_TYPE_META_CAPTURE:
1003                 if (is_vid && is_rx && ops->vidioc_g_fmt_meta_cap)
1004                         return 0;
1005                 break;
1006         case V4L2_BUF_TYPE_META_OUTPUT:
1007                 if (is_vid && is_tx && ops->vidioc_g_fmt_meta_out)
1008                         return 0;
1009                 break;
1010         default:
1011                 break;
1012         }
1013         return -EINVAL;
1014 }
1015
1016 static void v4l_sanitize_format(struct v4l2_format *fmt)
1017 {
1018         unsigned int offset;
1019
1020         /*
1021          * The v4l2_pix_format structure has been extended with fields that were
1022          * not previously required to be set to zero by applications. The priv
1023          * field, when set to a magic value, indicates the the extended fields
1024          * are valid. Otherwise they will contain undefined values. To simplify
1025          * the API towards drivers zero the extended fields and set the priv
1026          * field to the magic value when the extended pixel format structure
1027          * isn't used by applications.
1028          */
1029
1030         if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1031             fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1032                 return;
1033
1034         if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
1035                 return;
1036
1037         fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1038
1039         offset = offsetof(struct v4l2_pix_format, priv)
1040                + sizeof(fmt->fmt.pix.priv);
1041         memset(((void *)&fmt->fmt.pix) + offset, 0,
1042                sizeof(fmt->fmt.pix) - offset);
1043 }
1044
1045 static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
1046                                 struct file *file, void *fh, void *arg)
1047 {
1048         struct v4l2_capability *cap = (struct v4l2_capability *)arg;
1049         struct video_device *vfd = video_devdata(file);
1050         int ret;
1051
1052         cap->version = LINUX_VERSION_CODE;
1053         cap->device_caps = vfd->device_caps;
1054         cap->capabilities = vfd->device_caps | V4L2_CAP_DEVICE_CAPS;
1055
1056         ret = ops->vidioc_querycap(file, fh, cap);
1057
1058         cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
1059         /*
1060          * Drivers MUST fill in device_caps, so check for this and
1061          * warn if it was forgotten.
1062          */
1063         WARN(!(cap->capabilities & V4L2_CAP_DEVICE_CAPS) ||
1064                 !cap->device_caps, "Bad caps for driver %s, %x %x",
1065                 cap->driver, cap->capabilities, cap->device_caps);
1066         cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
1067
1068         return ret;
1069 }
1070
1071 static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
1072                                 struct file *file, void *fh, void *arg)
1073 {
1074         struct video_device *vfd = video_devdata(file);
1075         int ret;
1076
1077         ret = v4l_enable_media_source(vfd);
1078         if (ret)
1079                 return ret;
1080         return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
1081 }
1082
1083 static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
1084                                 struct file *file, void *fh, void *arg)
1085 {
1086         return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
1087 }
1088
1089 static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
1090                                 struct file *file, void *fh, void *arg)
1091 {
1092         struct video_device *vfd;
1093         u32 *p = arg;
1094
1095         vfd = video_devdata(file);
1096         *p = v4l2_prio_max(vfd->prio);
1097         return 0;
1098 }
1099
1100 static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
1101                                 struct file *file, void *fh, void *arg)
1102 {
1103         struct video_device *vfd;
1104         struct v4l2_fh *vfh;
1105         u32 *p = arg;
1106
1107         vfd = video_devdata(file);
1108         if (!test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
1109                 return -ENOTTY;
1110         vfh = file->private_data;
1111         return v4l2_prio_change(vfd->prio, &vfh->prio, *p);
1112 }
1113
1114 static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1115                                 struct file *file, void *fh, void *arg)
1116 {
1117         struct video_device *vfd = video_devdata(file);
1118         struct v4l2_input *p = arg;
1119
1120         /*
1121          * We set the flags for CAP_DV_TIMINGS &
1122          * CAP_STD here based on ioctl handler provided by the
1123          * driver. If the driver doesn't support these
1124          * for a specific input, it must override these flags.
1125          */
1126         if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1127                 p->capabilities |= V4L2_IN_CAP_STD;
1128
1129         return ops->vidioc_enum_input(file, fh, p);
1130 }
1131
1132 static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1133                                 struct file *file, void *fh, void *arg)
1134 {
1135         struct video_device *vfd = video_devdata(file);
1136         struct v4l2_output *p = arg;
1137
1138         /*
1139          * We set the flags for CAP_DV_TIMINGS &
1140          * CAP_STD here based on ioctl handler provided by the
1141          * driver. If the driver doesn't support these
1142          * for a specific output, it must override these flags.
1143          */
1144         if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1145                 p->capabilities |= V4L2_OUT_CAP_STD;
1146
1147         return ops->vidioc_enum_output(file, fh, p);
1148 }
1149
1150 static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
1151 {
1152         const unsigned sz = sizeof(fmt->description);
1153         const char *descr = NULL;
1154         u32 flags = 0;
1155
1156         /*
1157          * We depart from the normal coding style here since the descriptions
1158          * should be aligned so it is easy to see which descriptions will be
1159          * longer than 31 characters (the max length for a description).
1160          * And frankly, this is easier to read anyway.
1161          *
1162          * Note that gcc will use O(log N) comparisons to find the right case.
1163          */
1164         switch (fmt->pixelformat) {
1165         /* Max description length mask: descr = "0123456789012345678901234567890" */
1166         case V4L2_PIX_FMT_RGB332:       descr = "8-bit RGB 3-3-2"; break;
1167         case V4L2_PIX_FMT_RGB444:       descr = "16-bit A/XRGB 4-4-4-4"; break;
1168         case V4L2_PIX_FMT_ARGB444:      descr = "16-bit ARGB 4-4-4-4"; break;
1169         case V4L2_PIX_FMT_XRGB444:      descr = "16-bit XRGB 4-4-4-4"; break;
1170         case V4L2_PIX_FMT_RGB555:       descr = "16-bit A/XRGB 1-5-5-5"; break;
1171         case V4L2_PIX_FMT_ARGB555:      descr = "16-bit ARGB 1-5-5-5"; break;
1172         case V4L2_PIX_FMT_XRGB555:      descr = "16-bit XRGB 1-5-5-5"; break;
1173         case V4L2_PIX_FMT_RGB565:       descr = "16-bit RGB 5-6-5"; break;
1174         case V4L2_PIX_FMT_RGB555X:      descr = "16-bit A/XRGB 1-5-5-5 BE"; break;
1175         case V4L2_PIX_FMT_ARGB555X:     descr = "16-bit ARGB 1-5-5-5 BE"; break;
1176         case V4L2_PIX_FMT_XRGB555X:     descr = "16-bit XRGB 1-5-5-5 BE"; break;
1177         case V4L2_PIX_FMT_RGB565X:      descr = "16-bit RGB 5-6-5 BE"; break;
1178         case V4L2_PIX_FMT_BGR666:       descr = "18-bit BGRX 6-6-6-14"; break;
1179         case V4L2_PIX_FMT_BGR24:        descr = "24-bit BGR 8-8-8"; break;
1180         case V4L2_PIX_FMT_RGB24:        descr = "24-bit RGB 8-8-8"; break;
1181         case V4L2_PIX_FMT_BGR32:        descr = "32-bit BGRA/X 8-8-8-8"; break;
1182         case V4L2_PIX_FMT_ABGR32:       descr = "32-bit BGRA 8-8-8-8"; break;
1183         case V4L2_PIX_FMT_XBGR32:       descr = "32-bit BGRX 8-8-8-8"; break;
1184         case V4L2_PIX_FMT_RGB32:        descr = "32-bit A/XRGB 8-8-8-8"; break;
1185         case V4L2_PIX_FMT_ARGB32:       descr = "32-bit ARGB 8-8-8-8"; break;
1186         case V4L2_PIX_FMT_XRGB32:       descr = "32-bit XRGB 8-8-8-8"; break;
1187         case V4L2_PIX_FMT_GREY:         descr = "8-bit Greyscale"; break;
1188         case V4L2_PIX_FMT_Y4:           descr = "4-bit Greyscale"; break;
1189         case V4L2_PIX_FMT_Y6:           descr = "6-bit Greyscale"; break;
1190         case V4L2_PIX_FMT_Y10:          descr = "10-bit Greyscale"; break;
1191         case V4L2_PIX_FMT_Y12:          descr = "12-bit Greyscale"; break;
1192         case V4L2_PIX_FMT_Y16:          descr = "16-bit Greyscale"; break;
1193         case V4L2_PIX_FMT_Y16_BE:       descr = "16-bit Greyscale BE"; break;
1194         case V4L2_PIX_FMT_Y10BPACK:     descr = "10-bit Greyscale (Packed)"; break;
1195         case V4L2_PIX_FMT_Y10P:         descr = "10-bit Greyscale (MIPI Packed)"; break;
1196         case V4L2_PIX_FMT_Y8I:          descr = "Interleaved 8-bit Greyscale"; break;
1197         case V4L2_PIX_FMT_Y12I:         descr = "Interleaved 12-bit Greyscale"; break;
1198         case V4L2_PIX_FMT_Z16:          descr = "16-bit Depth"; break;
1199         case V4L2_PIX_FMT_INZI:         descr = "Planar 10:16 Greyscale Depth"; break;
1200         case V4L2_PIX_FMT_CNF4:         descr = "4-bit Depth Confidence (Packed)"; break;
1201         case V4L2_PIX_FMT_PAL8:         descr = "8-bit Palette"; break;
1202         case V4L2_PIX_FMT_UV8:          descr = "8-bit Chrominance UV 4-4"; break;
1203         case V4L2_PIX_FMT_YVU410:       descr = "Planar YVU 4:1:0"; break;
1204         case V4L2_PIX_FMT_YVU420:       descr = "Planar YVU 4:2:0"; break;
1205         case V4L2_PIX_FMT_YUYV:         descr = "YUYV 4:2:2"; break;
1206         case V4L2_PIX_FMT_YYUV:         descr = "YYUV 4:2:2"; break;
1207         case V4L2_PIX_FMT_YVYU:         descr = "YVYU 4:2:2"; break;
1208         case V4L2_PIX_FMT_UYVY:         descr = "UYVY 4:2:2"; break;
1209         case V4L2_PIX_FMT_VYUY:         descr = "VYUY 4:2:2"; break;
1210         case V4L2_PIX_FMT_YUV422P:      descr = "Planar YUV 4:2:2"; break;
1211         case V4L2_PIX_FMT_YUV411P:      descr = "Planar YUV 4:1:1"; break;
1212         case V4L2_PIX_FMT_Y41P:         descr = "YUV 4:1:1 (Packed)"; break;
1213         case V4L2_PIX_FMT_YUV444:       descr = "16-bit A/XYUV 4-4-4-4"; break;
1214         case V4L2_PIX_FMT_YUV555:       descr = "16-bit A/XYUV 1-5-5-5"; break;
1215         case V4L2_PIX_FMT_YUV565:       descr = "16-bit YUV 5-6-5"; break;
1216         case V4L2_PIX_FMT_YUV32:        descr = "32-bit A/XYUV 8-8-8-8"; break;
1217         case V4L2_PIX_FMT_YUV410:       descr = "Planar YUV 4:1:0"; break;
1218         case V4L2_PIX_FMT_YUV420:       descr = "Planar YUV 4:2:0"; break;
1219         case V4L2_PIX_FMT_HI240:        descr = "8-bit Dithered RGB (BTTV)"; break;
1220         case V4L2_PIX_FMT_HM12:         descr = "YUV 4:2:0 (16x16 Macroblocks)"; break;
1221         case V4L2_PIX_FMT_M420:         descr = "YUV 4:2:0 (M420)"; break;
1222         case V4L2_PIX_FMT_NV12:         descr = "Y/CbCr 4:2:0"; break;
1223         case V4L2_PIX_FMT_NV21:         descr = "Y/CrCb 4:2:0"; break;
1224         case V4L2_PIX_FMT_NV16:         descr = "Y/CbCr 4:2:2"; break;
1225         case V4L2_PIX_FMT_NV61:         descr = "Y/CrCb 4:2:2"; break;
1226         case V4L2_PIX_FMT_NV24:         descr = "Y/CbCr 4:4:4"; break;
1227         case V4L2_PIX_FMT_NV42:         descr = "Y/CrCb 4:4:4"; break;
1228         case V4L2_PIX_FMT_NV12M:        descr = "Y/CbCr 4:2:0 (N-C)"; break;
1229         case V4L2_PIX_FMT_NV21M:        descr = "Y/CrCb 4:2:0 (N-C)"; break;
1230         case V4L2_PIX_FMT_NV16M:        descr = "Y/CbCr 4:2:2 (N-C)"; break;
1231         case V4L2_PIX_FMT_NV61M:        descr = "Y/CrCb 4:2:2 (N-C)"; break;
1232         case V4L2_PIX_FMT_NV12MT:       descr = "Y/CbCr 4:2:0 (64x32 MB, N-C)"; break;
1233         case V4L2_PIX_FMT_NV12MT_16X16: descr = "Y/CbCr 4:2:0 (16x16 MB, N-C)"; break;
1234         case V4L2_PIX_FMT_YUV420M:      descr = "Planar YUV 4:2:0 (N-C)"; break;
1235         case V4L2_PIX_FMT_YVU420M:      descr = "Planar YVU 4:2:0 (N-C)"; break;
1236         case V4L2_PIX_FMT_YUV422M:      descr = "Planar YUV 4:2:2 (N-C)"; break;
1237         case V4L2_PIX_FMT_YVU422M:      descr = "Planar YVU 4:2:2 (N-C)"; break;
1238         case V4L2_PIX_FMT_YUV444M:      descr = "Planar YUV 4:4:4 (N-C)"; break;
1239         case V4L2_PIX_FMT_YVU444M:      descr = "Planar YVU 4:4:4 (N-C)"; break;
1240         case V4L2_PIX_FMT_SBGGR8:       descr = "8-bit Bayer BGBG/GRGR"; break;
1241         case V4L2_PIX_FMT_SGBRG8:       descr = "8-bit Bayer GBGB/RGRG"; break;
1242         case V4L2_PIX_FMT_SGRBG8:       descr = "8-bit Bayer GRGR/BGBG"; break;
1243         case V4L2_PIX_FMT_SRGGB8:       descr = "8-bit Bayer RGRG/GBGB"; break;
1244         case V4L2_PIX_FMT_SBGGR10:      descr = "10-bit Bayer BGBG/GRGR"; break;
1245         case V4L2_PIX_FMT_SGBRG10:      descr = "10-bit Bayer GBGB/RGRG"; break;
1246         case V4L2_PIX_FMT_SGRBG10:      descr = "10-bit Bayer GRGR/BGBG"; break;
1247         case V4L2_PIX_FMT_SRGGB10:      descr = "10-bit Bayer RGRG/GBGB"; break;
1248         case V4L2_PIX_FMT_SBGGR10P:     descr = "10-bit Bayer BGBG/GRGR Packed"; break;
1249         case V4L2_PIX_FMT_SGBRG10P:     descr = "10-bit Bayer GBGB/RGRG Packed"; break;
1250         case V4L2_PIX_FMT_SGRBG10P:     descr = "10-bit Bayer GRGR/BGBG Packed"; break;
1251         case V4L2_PIX_FMT_SRGGB10P:     descr = "10-bit Bayer RGRG/GBGB Packed"; break;
1252         case V4L2_PIX_FMT_IPU3_SBGGR10: descr = "10-bit bayer BGGR IPU3 Packed"; break;
1253         case V4L2_PIX_FMT_IPU3_SGBRG10: descr = "10-bit bayer GBRG IPU3 Packed"; break;
1254         case V4L2_PIX_FMT_IPU3_SGRBG10: descr = "10-bit bayer GRBG IPU3 Packed"; break;
1255         case V4L2_PIX_FMT_IPU3_SRGGB10: descr = "10-bit bayer RGGB IPU3 Packed"; break;
1256         case V4L2_PIX_FMT_SBGGR10ALAW8: descr = "8-bit Bayer BGBG/GRGR (A-law)"; break;
1257         case V4L2_PIX_FMT_SGBRG10ALAW8: descr = "8-bit Bayer GBGB/RGRG (A-law)"; break;
1258         case V4L2_PIX_FMT_SGRBG10ALAW8: descr = "8-bit Bayer GRGR/BGBG (A-law)"; break;
1259         case V4L2_PIX_FMT_SRGGB10ALAW8: descr = "8-bit Bayer RGRG/GBGB (A-law)"; break;
1260         case V4L2_PIX_FMT_SBGGR10DPCM8: descr = "8-bit Bayer BGBG/GRGR (DPCM)"; break;
1261         case V4L2_PIX_FMT_SGBRG10DPCM8: descr = "8-bit Bayer GBGB/RGRG (DPCM)"; break;
1262         case V4L2_PIX_FMT_SGRBG10DPCM8: descr = "8-bit Bayer GRGR/BGBG (DPCM)"; break;
1263         case V4L2_PIX_FMT_SRGGB10DPCM8: descr = "8-bit Bayer RGRG/GBGB (DPCM)"; break;
1264         case V4L2_PIX_FMT_SBGGR12:      descr = "12-bit Bayer BGBG/GRGR"; break;
1265         case V4L2_PIX_FMT_SGBRG12:      descr = "12-bit Bayer GBGB/RGRG"; break;
1266         case V4L2_PIX_FMT_SGRBG12:      descr = "12-bit Bayer GRGR/BGBG"; break;
1267         case V4L2_PIX_FMT_SRGGB12:      descr = "12-bit Bayer RGRG/GBGB"; break;
1268         case V4L2_PIX_FMT_SBGGR12P:     descr = "12-bit Bayer BGBG/GRGR Packed"; break;
1269         case V4L2_PIX_FMT_SGBRG12P:     descr = "12-bit Bayer GBGB/RGRG Packed"; break;
1270         case V4L2_PIX_FMT_SGRBG12P:     descr = "12-bit Bayer GRGR/BGBG Packed"; break;
1271         case V4L2_PIX_FMT_SRGGB12P:     descr = "12-bit Bayer RGRG/GBGB Packed"; break;
1272         case V4L2_PIX_FMT_SBGGR14P:     descr = "14-bit Bayer BGBG/GRGR Packed"; break;
1273         case V4L2_PIX_FMT_SGBRG14P:     descr = "14-bit Bayer GBGB/RGRG Packed"; break;
1274         case V4L2_PIX_FMT_SGRBG14P:     descr = "14-bit Bayer GRGR/BGBG Packed"; break;
1275         case V4L2_PIX_FMT_SRGGB14P:     descr = "14-bit Bayer RGRG/GBGB Packed"; break;
1276         case V4L2_PIX_FMT_SBGGR16:      descr = "16-bit Bayer BGBG/GRGR"; break;
1277         case V4L2_PIX_FMT_SGBRG16:      descr = "16-bit Bayer GBGB/RGRG"; break;
1278         case V4L2_PIX_FMT_SGRBG16:      descr = "16-bit Bayer GRGR/BGBG"; break;
1279         case V4L2_PIX_FMT_SRGGB16:      descr = "16-bit Bayer RGRG/GBGB"; break;
1280         case V4L2_PIX_FMT_SN9C20X_I420: descr = "GSPCA SN9C20X I420"; break;
1281         case V4L2_PIX_FMT_SPCA501:      descr = "GSPCA SPCA501"; break;
1282         case V4L2_PIX_FMT_SPCA505:      descr = "GSPCA SPCA505"; break;
1283         case V4L2_PIX_FMT_SPCA508:      descr = "GSPCA SPCA508"; break;
1284         case V4L2_PIX_FMT_STV0680:      descr = "GSPCA STV0680"; break;
1285         case V4L2_PIX_FMT_TM6000:       descr = "A/V + VBI Mux Packet"; break;
1286         case V4L2_PIX_FMT_CIT_YYVYUY:   descr = "GSPCA CIT YYVYUY"; break;
1287         case V4L2_PIX_FMT_KONICA420:    descr = "GSPCA KONICA420"; break;
1288         case V4L2_PIX_FMT_HSV24:        descr = "24-bit HSV 8-8-8"; break;
1289         case V4L2_PIX_FMT_HSV32:        descr = "32-bit XHSV 8-8-8-8"; break;
1290         case V4L2_SDR_FMT_CU8:          descr = "Complex U8"; break;
1291         case V4L2_SDR_FMT_CU16LE:       descr = "Complex U16LE"; break;
1292         case V4L2_SDR_FMT_CS8:          descr = "Complex S8"; break;
1293         case V4L2_SDR_FMT_CS14LE:       descr = "Complex S14LE"; break;
1294         case V4L2_SDR_FMT_RU12LE:       descr = "Real U12LE"; break;
1295         case V4L2_SDR_FMT_PCU16BE:      descr = "Planar Complex U16BE"; break;
1296         case V4L2_SDR_FMT_PCU18BE:      descr = "Planar Complex U18BE"; break;
1297         case V4L2_SDR_FMT_PCU20BE:      descr = "Planar Complex U20BE"; break;
1298         case V4L2_TCH_FMT_DELTA_TD16:   descr = "16-bit signed deltas"; break;
1299         case V4L2_TCH_FMT_DELTA_TD08:   descr = "8-bit signed deltas"; break;
1300         case V4L2_TCH_FMT_TU16:         descr = "16-bit unsigned touch data"; break;
1301         case V4L2_TCH_FMT_TU08:         descr = "8-bit unsigned touch data"; break;
1302         case V4L2_META_FMT_VSP1_HGO:    descr = "R-Car VSP1 1-D Histogram"; break;
1303         case V4L2_META_FMT_VSP1_HGT:    descr = "R-Car VSP1 2-D Histogram"; break;
1304         case V4L2_META_FMT_UVC:         descr = "UVC payload header metadata"; break;
1305
1306         default:
1307                 /* Compressed formats */
1308                 flags = V4L2_FMT_FLAG_COMPRESSED;
1309                 switch (fmt->pixelformat) {
1310                 /* Max description length mask: descr = "0123456789012345678901234567890" */
1311                 case V4L2_PIX_FMT_MJPEG:        descr = "Motion-JPEG"; break;
1312                 case V4L2_PIX_FMT_JPEG:         descr = "JFIF JPEG"; break;
1313                 case V4L2_PIX_FMT_DV:           descr = "1394"; break;
1314                 case V4L2_PIX_FMT_MPEG:         descr = "MPEG-1/2/4"; break;
1315                 case V4L2_PIX_FMT_H264:         descr = "H.264"; break;
1316                 case V4L2_PIX_FMT_H264_NO_SC:   descr = "H.264 (No Start Codes)"; break;
1317                 case V4L2_PIX_FMT_H264_MVC:     descr = "H.264 MVC"; break;
1318                 case V4L2_PIX_FMT_H263:         descr = "H.263"; break;
1319                 case V4L2_PIX_FMT_MPEG1:        descr = "MPEG-1 ES"; break;
1320                 case V4L2_PIX_FMT_MPEG2:        descr = "MPEG-2 ES"; break;
1321                 case V4L2_PIX_FMT_MPEG2_SLICE:  descr = "MPEG-2 Parsed Slice Data"; break;
1322                 case V4L2_PIX_FMT_MPEG4:        descr = "MPEG-4 part 2 ES"; break;
1323                 case V4L2_PIX_FMT_XVID:         descr = "Xvid"; break;
1324                 case V4L2_PIX_FMT_VC1_ANNEX_G:  descr = "VC-1 (SMPTE 412M Annex G)"; break;
1325                 case V4L2_PIX_FMT_VC1_ANNEX_L:  descr = "VC-1 (SMPTE 412M Annex L)"; break;
1326                 case V4L2_PIX_FMT_VP8:          descr = "VP8"; break;
1327                 case V4L2_PIX_FMT_VP9:          descr = "VP9"; break;
1328                 case V4L2_PIX_FMT_HEVC:         descr = "HEVC"; break; /* aka H.265 */
1329                 case V4L2_PIX_FMT_FWHT:         descr = "FWHT"; break; /* used in vicodec */
1330                 case V4L2_PIX_FMT_CPIA1:        descr = "GSPCA CPiA YUV"; break;
1331                 case V4L2_PIX_FMT_WNVA:         descr = "WNVA"; break;
1332                 case V4L2_PIX_FMT_SN9C10X:      descr = "GSPCA SN9C10X"; break;
1333                 case V4L2_PIX_FMT_PWC1:         descr = "Raw Philips Webcam Type (Old)"; break;
1334                 case V4L2_PIX_FMT_PWC2:         descr = "Raw Philips Webcam Type (New)"; break;
1335                 case V4L2_PIX_FMT_ET61X251:     descr = "GSPCA ET61X251"; break;
1336                 case V4L2_PIX_FMT_SPCA561:      descr = "GSPCA SPCA561"; break;
1337                 case V4L2_PIX_FMT_PAC207:       descr = "GSPCA PAC207"; break;
1338                 case V4L2_PIX_FMT_MR97310A:     descr = "GSPCA MR97310A"; break;
1339                 case V4L2_PIX_FMT_JL2005BCD:    descr = "GSPCA JL2005BCD"; break;
1340                 case V4L2_PIX_FMT_SN9C2028:     descr = "GSPCA SN9C2028"; break;
1341                 case V4L2_PIX_FMT_SQ905C:       descr = "GSPCA SQ905C"; break;
1342                 case V4L2_PIX_FMT_PJPG:         descr = "GSPCA PJPG"; break;
1343                 case V4L2_PIX_FMT_OV511:        descr = "GSPCA OV511"; break;
1344                 case V4L2_PIX_FMT_OV518:        descr = "GSPCA OV518"; break;
1345                 case V4L2_PIX_FMT_JPGL:         descr = "JPEG Lite"; break;
1346                 case V4L2_PIX_FMT_SE401:        descr = "GSPCA SE401"; break;
1347                 case V4L2_PIX_FMT_S5C_UYVY_JPG: descr = "S5C73MX interleaved UYVY/JPEG"; break;
1348                 case V4L2_PIX_FMT_MT21C:        descr = "Mediatek Compressed Format"; break;
1349                 case V4L2_PIX_FMT_SUNXI_TILED_NV12: descr = "Sunxi Tiled NV12 Format"; break;
1350                 default:
1351                         if (fmt->description[0])
1352                                 return;
1353                         WARN(1, "Unknown pixelformat 0x%08x\n", fmt->pixelformat);
1354                         flags = 0;
1355                         snprintf(fmt->description, sz, "%c%c%c%c%s",
1356                                         (char)(fmt->pixelformat & 0x7f),
1357                                         (char)((fmt->pixelformat >> 8) & 0x7f),
1358                                         (char)((fmt->pixelformat >> 16) & 0x7f),
1359                                         (char)((fmt->pixelformat >> 24) & 0x7f),
1360                                         (fmt->pixelformat & (1 << 31)) ? "-BE" : "");
1361                         break;
1362                 }
1363         }
1364
1365         if (descr)
1366                 WARN_ON(strscpy(fmt->description, descr, sz) >= sz);
1367         fmt->flags = flags;
1368 }
1369
1370 static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1371                                 struct file *file, void *fh, void *arg)
1372 {
1373         struct v4l2_fmtdesc *p = arg;
1374         int ret = check_fmt(file, p->type);
1375
1376         if (ret)
1377                 return ret;
1378         ret = -EINVAL;
1379
1380         switch (p->type) {
1381         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1382                 if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
1383                         break;
1384                 ret = ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1385                 break;
1386         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1387                 if (unlikely(!ops->vidioc_enum_fmt_vid_cap_mplane))
1388                         break;
1389                 ret = ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1390                 break;
1391         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1392                 if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
1393                         break;
1394                 ret = ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1395                 break;
1396         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1397                 if (unlikely(!ops->vidioc_enum_fmt_vid_out))
1398                         break;
1399                 ret = ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1400                 break;
1401         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1402                 if (unlikely(!ops->vidioc_enum_fmt_vid_out_mplane))
1403                         break;
1404                 ret = ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
1405                 break;
1406         case V4L2_BUF_TYPE_SDR_CAPTURE:
1407                 if (unlikely(!ops->vidioc_enum_fmt_sdr_cap))
1408                         break;
1409                 ret = ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
1410                 break;
1411         case V4L2_BUF_TYPE_SDR_OUTPUT:
1412                 if (unlikely(!ops->vidioc_enum_fmt_sdr_out))
1413                         break;
1414                 ret = ops->vidioc_enum_fmt_sdr_out(file, fh, arg);
1415                 break;
1416         case V4L2_BUF_TYPE_META_CAPTURE:
1417                 if (unlikely(!ops->vidioc_enum_fmt_meta_cap))
1418                         break;
1419                 ret = ops->vidioc_enum_fmt_meta_cap(file, fh, arg);
1420                 break;
1421         case V4L2_BUF_TYPE_META_OUTPUT:
1422                 if (unlikely(!ops->vidioc_enum_fmt_meta_out))
1423                         break;
1424                 ret = ops->vidioc_enum_fmt_meta_out(file, fh, arg);
1425                 break;
1426         }
1427         if (ret == 0)
1428                 v4l_fill_fmtdesc(p);
1429         return ret;
1430 }
1431
1432 static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1433                                 struct file *file, void *fh, void *arg)
1434 {
1435         struct v4l2_format *p = arg;
1436         int ret = check_fmt(file, p->type);
1437
1438         if (ret)
1439                 return ret;
1440
1441         /*
1442          * fmt can't be cleared for these overlay types due to the 'clips'
1443          * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
1444          * Those are provided by the user. So handle these two overlay types
1445          * first, and then just do a simple memset for the other types.
1446          */
1447         switch (p->type) {
1448         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1449         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
1450                 struct v4l2_clip __user *clips = p->fmt.win.clips;
1451                 u32 clipcount = p->fmt.win.clipcount;
1452                 void __user *bitmap = p->fmt.win.bitmap;
1453
1454                 memset(&p->fmt, 0, sizeof(p->fmt));
1455                 p->fmt.win.clips = clips;
1456                 p->fmt.win.clipcount = clipcount;
1457                 p->fmt.win.bitmap = bitmap;
1458                 break;
1459         }
1460         default:
1461                 memset(&p->fmt, 0, sizeof(p->fmt));
1462                 break;
1463         }
1464
1465         switch (p->type) {
1466         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1467                 if (unlikely(!ops->vidioc_g_fmt_vid_cap))
1468                         break;
1469                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1470                 ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1471                 /* just in case the driver zeroed it again */
1472                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1473                 return ret;
1474         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1475                 return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1476         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1477                 return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1478         case V4L2_BUF_TYPE_VBI_CAPTURE:
1479                 return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1480         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1481                 return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1482         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1483                 if (unlikely(!ops->vidioc_g_fmt_vid_out))
1484                         break;
1485                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1486                 ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
1487                 /* just in case the driver zeroed it again */
1488                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1489                 return ret;
1490         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1491                 return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1492         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1493                 return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1494         case V4L2_BUF_TYPE_VBI_OUTPUT:
1495                 return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1496         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1497                 return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1498         case V4L2_BUF_TYPE_SDR_CAPTURE:
1499                 return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
1500         case V4L2_BUF_TYPE_SDR_OUTPUT:
1501                 return ops->vidioc_g_fmt_sdr_out(file, fh, arg);
1502         case V4L2_BUF_TYPE_META_CAPTURE:
1503                 return ops->vidioc_g_fmt_meta_cap(file, fh, arg);
1504         case V4L2_BUF_TYPE_META_OUTPUT:
1505                 return ops->vidioc_g_fmt_meta_out(file, fh, arg);
1506         }
1507         return -EINVAL;
1508 }
1509
1510 static void v4l_pix_format_touch(struct v4l2_pix_format *p)
1511 {
1512         /*
1513          * The v4l2_pix_format structure contains fields that make no sense for
1514          * touch. Set them to default values in this case.
1515          */
1516
1517         p->field = V4L2_FIELD_NONE;
1518         p->colorspace = V4L2_COLORSPACE_RAW;
1519         p->flags = 0;
1520         p->ycbcr_enc = 0;
1521         p->quantization = 0;
1522         p->xfer_func = 0;
1523 }
1524
1525 static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1526                                 struct file *file, void *fh, void *arg)
1527 {
1528         struct v4l2_format *p = arg;
1529         struct video_device *vfd = video_devdata(file);
1530         int ret = check_fmt(file, p->type);
1531         unsigned int i;
1532
1533         if (ret)
1534                 return ret;
1535
1536         ret = v4l_enable_media_source(vfd);
1537         if (ret)
1538                 return ret;
1539         v4l_sanitize_format(p);
1540
1541         switch (p->type) {
1542         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1543                 if (unlikely(!ops->vidioc_s_fmt_vid_cap))
1544                         break;
1545                 CLEAR_AFTER_FIELD(p, fmt.pix);
1546                 ret = ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1547                 /* just in case the driver zeroed it again */
1548                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1549                 if (vfd->vfl_type == VFL_TYPE_TOUCH)
1550                         v4l_pix_format_touch(&p->fmt.pix);
1551                 return ret;
1552         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1553                 if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
1554                         break;
1555                 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1556                 if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
1557                         break;
1558                 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1559                         CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1560                                           bytesperline);
1561                 return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1562         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1563                 if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
1564                         break;
1565                 CLEAR_AFTER_FIELD(p, fmt.win);
1566                 return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1567         case V4L2_BUF_TYPE_VBI_CAPTURE:
1568                 if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
1569                         break;
1570                 CLEAR_AFTER_FIELD(p, fmt.vbi);
1571                 return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1572         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1573                 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
1574                         break;
1575                 CLEAR_AFTER_FIELD(p, fmt.sliced);
1576                 return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1577         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1578                 if (unlikely(!ops->vidioc_s_fmt_vid_out))
1579                         break;
1580                 CLEAR_AFTER_FIELD(p, fmt.pix);
1581                 ret = ops->vidioc_s_fmt_vid_out(file, fh, arg);
1582                 /* just in case the driver zeroed it again */
1583                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1584                 return ret;
1585         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1586                 if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
1587                         break;
1588                 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1589                 if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
1590                         break;
1591                 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1592                         CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1593                                           bytesperline);
1594                 return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1595         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1596                 if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
1597                         break;
1598                 CLEAR_AFTER_FIELD(p, fmt.win);
1599                 return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1600         case V4L2_BUF_TYPE_VBI_OUTPUT:
1601                 if (unlikely(!ops->vidioc_s_fmt_vbi_out))
1602                         break;
1603                 CLEAR_AFTER_FIELD(p, fmt.vbi);
1604                 return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1605         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1606                 if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
1607                         break;
1608                 CLEAR_AFTER_FIELD(p, fmt.sliced);
1609                 return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
1610         case V4L2_BUF_TYPE_SDR_CAPTURE:
1611                 if (unlikely(!ops->vidioc_s_fmt_sdr_cap))
1612                         break;
1613                 CLEAR_AFTER_FIELD(p, fmt.sdr);
1614                 return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
1615         case V4L2_BUF_TYPE_SDR_OUTPUT:
1616                 if (unlikely(!ops->vidioc_s_fmt_sdr_out))
1617                         break;
1618                 CLEAR_AFTER_FIELD(p, fmt.sdr);
1619                 return ops->vidioc_s_fmt_sdr_out(file, fh, arg);
1620         case V4L2_BUF_TYPE_META_CAPTURE:
1621                 if (unlikely(!ops->vidioc_s_fmt_meta_cap))
1622                         break;
1623                 CLEAR_AFTER_FIELD(p, fmt.meta);
1624                 return ops->vidioc_s_fmt_meta_cap(file, fh, arg);
1625         case V4L2_BUF_TYPE_META_OUTPUT:
1626                 if (unlikely(!ops->vidioc_s_fmt_meta_out))
1627                         break;
1628                 CLEAR_AFTER_FIELD(p, fmt.meta);
1629                 return ops->vidioc_s_fmt_meta_out(file, fh, arg);
1630         }
1631         return -EINVAL;
1632 }
1633
1634 static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1635                                 struct file *file, void *fh, void *arg)
1636 {
1637         struct v4l2_format *p = arg;
1638         int ret = check_fmt(file, p->type);
1639         unsigned int i;
1640
1641         if (ret)
1642                 return ret;
1643
1644         v4l_sanitize_format(p);
1645
1646         switch (p->type) {
1647         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1648                 if (unlikely(!ops->vidioc_try_fmt_vid_cap))
1649                         break;
1650                 CLEAR_AFTER_FIELD(p, fmt.pix);
1651                 ret = ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1652                 /* just in case the driver zeroed it again */
1653                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1654                 return ret;
1655         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1656                 if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
1657                         break;
1658                 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1659                 if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
1660                         break;
1661                 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1662                         CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1663                                           bytesperline);
1664                 return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1665         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1666                 if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
1667                         break;
1668                 CLEAR_AFTER_FIELD(p, fmt.win);
1669                 return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1670         case V4L2_BUF_TYPE_VBI_CAPTURE:
1671                 if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
1672                         break;
1673                 CLEAR_AFTER_FIELD(p, fmt.vbi);
1674                 return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1675         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1676                 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
1677                         break;
1678                 CLEAR_AFTER_FIELD(p, fmt.sliced);
1679                 return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1680         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1681                 if (unlikely(!ops->vidioc_try_fmt_vid_out))
1682                         break;
1683                 CLEAR_AFTER_FIELD(p, fmt.pix);
1684                 ret = ops->vidioc_try_fmt_vid_out(file, fh, arg);
1685                 /* just in case the driver zeroed it again */
1686                 p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1687                 return ret;
1688         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1689                 if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
1690                         break;
1691                 CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1692                 if (p->fmt.pix_mp.num_planes > VIDEO_MAX_PLANES)
1693                         break;
1694                 for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1695                         CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1696                                           bytesperline);
1697                 return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1698         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1699                 if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
1700                         break;
1701                 CLEAR_AFTER_FIELD(p, fmt.win);
1702                 return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1703         case V4L2_BUF_TYPE_VBI_OUTPUT:
1704                 if (unlikely(!ops->vidioc_try_fmt_vbi_out))
1705                         break;
1706                 CLEAR_AFTER_FIELD(p, fmt.vbi);
1707                 return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1708         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1709                 if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
1710                         break;
1711                 CLEAR_AFTER_FIELD(p, fmt.sliced);
1712                 return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1713         case V4L2_BUF_TYPE_SDR_CAPTURE:
1714                 if (unlikely(!ops->vidioc_try_fmt_sdr_cap))
1715                         break;
1716                 CLEAR_AFTER_FIELD(p, fmt.sdr);
1717                 return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
1718         case V4L2_BUF_TYPE_SDR_OUTPUT:
1719                 if (unlikely(!ops->vidioc_try_fmt_sdr_out))
1720                         break;
1721                 CLEAR_AFTER_FIELD(p, fmt.sdr);
1722                 return ops->vidioc_try_fmt_sdr_out(file, fh, arg);
1723         case V4L2_BUF_TYPE_META_CAPTURE:
1724                 if (unlikely(!ops->vidioc_try_fmt_meta_cap))
1725                         break;
1726                 CLEAR_AFTER_FIELD(p, fmt.meta);
1727                 return ops->vidioc_try_fmt_meta_cap(file, fh, arg);
1728         case V4L2_BUF_TYPE_META_OUTPUT:
1729                 if (unlikely(!ops->vidioc_try_fmt_meta_out))
1730                         break;
1731                 CLEAR_AFTER_FIELD(p, fmt.meta);
1732                 return ops->vidioc_try_fmt_meta_out(file, fh, arg);
1733         }
1734         return -EINVAL;
1735 }
1736
1737 static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1738                                 struct file *file, void *fh, void *arg)
1739 {
1740         return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1741 }
1742
1743 static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1744                                 struct file *file, void *fh, void *arg)
1745 {
1746         return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1747 }
1748
1749 static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1750                                 struct file *file, void *fh, void *arg)
1751 {
1752         struct video_device *vfd = video_devdata(file);
1753         struct v4l2_tuner *p = arg;
1754         int err;
1755
1756         p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1757                         V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1758         err = ops->vidioc_g_tuner(file, fh, p);
1759         if (!err)
1760                 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1761         return err;
1762 }
1763
1764 static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1765                                 struct file *file, void *fh, void *arg)
1766 {
1767         struct video_device *vfd = video_devdata(file);
1768         struct v4l2_tuner *p = arg;
1769         int ret;
1770
1771         ret = v4l_enable_media_source(vfd);
1772         if (ret)
1773                 return ret;
1774         p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1775                         V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1776         return ops->vidioc_s_tuner(file, fh, p);
1777 }
1778
1779 static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1780                                 struct file *file, void *fh, void *arg)
1781 {
1782         struct video_device *vfd = video_devdata(file);
1783         struct v4l2_modulator *p = arg;
1784         int err;
1785
1786         if (vfd->vfl_type == VFL_TYPE_RADIO)
1787                 p->type = V4L2_TUNER_RADIO;
1788
1789         err = ops->vidioc_g_modulator(file, fh, p);
1790         if (!err)
1791                 p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1792         return err;
1793 }
1794
1795 static int v4l_s_modulator(const struct v4l2_ioctl_ops *ops,
1796                                 struct file *file, void *fh, void *arg)
1797 {
1798         struct video_device *vfd = video_devdata(file);
1799         struct v4l2_modulator *p = arg;
1800
1801         if (vfd->vfl_type == VFL_TYPE_RADIO)
1802                 p->type = V4L2_TUNER_RADIO;
1803
1804         return ops->vidioc_s_modulator(file, fh, p);
1805 }
1806
1807 static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1808                                 struct file *file, void *fh, void *arg)
1809 {
1810         struct video_device *vfd = video_devdata(file);
1811         struct v4l2_frequency *p = arg;
1812
1813         if (vfd->vfl_type == VFL_TYPE_SDR)
1814                 p->type = V4L2_TUNER_SDR;
1815         else
1816                 p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1817                                 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1818         return ops->vidioc_g_frequency(file, fh, p);
1819 }
1820
1821 static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1822                                 struct file *file, void *fh, void *arg)
1823 {
1824         struct video_device *vfd = video_devdata(file);
1825         const struct v4l2_frequency *p = arg;
1826         enum v4l2_tuner_type type;
1827         int ret;
1828
1829         ret = v4l_enable_media_source(vfd);
1830         if (ret)
1831                 return ret;
1832         if (vfd->vfl_type == VFL_TYPE_SDR) {
1833                 if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
1834                         return -EINVAL;
1835         } else {
1836                 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1837                                 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1838                 if (type != p->type)
1839                         return -EINVAL;
1840         }
1841         return ops->vidioc_s_frequency(file, fh, p);
1842 }
1843
1844 static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1845                                 struct file *file, void *fh, void *arg)
1846 {
1847         struct video_device *vfd = video_devdata(file);
1848         struct v4l2_standard *p = arg;
1849
1850         return v4l_video_std_enumstd(p, vfd->tvnorms);
1851 }
1852
1853 static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1854                                 struct file *file, void *fh, void *arg)
1855 {
1856         struct video_device *vfd = video_devdata(file);
1857         v4l2_std_id id = *(v4l2_std_id *)arg, norm;
1858         int ret;
1859
1860         ret = v4l_enable_media_source(vfd);
1861         if (ret)
1862                 return ret;
1863         norm = id & vfd->tvnorms;
1864         if (vfd->tvnorms && !norm)      /* Check if std is supported */
1865                 return -EINVAL;
1866
1867         /* Calls the specific handler */
1868         return ops->vidioc_s_std(file, fh, norm);
1869 }
1870
1871 static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1872                                 struct file *file, void *fh, void *arg)
1873 {
1874         struct video_device *vfd = video_devdata(file);
1875         v4l2_std_id *p = arg;
1876         int ret;
1877
1878         ret = v4l_enable_media_source(vfd);
1879         if (ret)
1880                 return ret;
1881         /*
1882          * If no signal is detected, then the driver should return
1883          * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
1884          * any standards that do not apply removed.
1885          *
1886          * This means that tuners, audio and video decoders can join
1887          * their efforts to improve the standards detection.
1888          */
1889         *p = vfd->tvnorms;
1890         return ops->vidioc_querystd(file, fh, arg);
1891 }
1892
1893 static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1894                                 struct file *file, void *fh, void *arg)
1895 {
1896         struct video_device *vfd = video_devdata(file);
1897         struct v4l2_hw_freq_seek *p = arg;
1898         enum v4l2_tuner_type type;
1899         int ret;
1900
1901         ret = v4l_enable_media_source(vfd);
1902         if (ret)
1903                 return ret;
1904         /* s_hw_freq_seek is not supported for SDR for now */
1905         if (vfd->vfl_type == VFL_TYPE_SDR)
1906                 return -EINVAL;
1907
1908         type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1909                 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1910         if (p->type != type)
1911                 return -EINVAL;
1912         return ops->vidioc_s_hw_freq_seek(file, fh, p);
1913 }
1914
1915 static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
1916                                 struct file *file, void *fh, void *arg)
1917 {
1918         return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
1919 }
1920
1921 static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1922                                 struct file *file, void *fh, void *arg)
1923 {
1924         struct v4l2_requestbuffers *p = arg;
1925         int ret = check_fmt(file, p->type);
1926
1927         if (ret)
1928                 return ret;
1929
1930         CLEAR_AFTER_FIELD(p, capabilities);
1931
1932         return ops->vidioc_reqbufs(file, fh, p);
1933 }
1934
1935 static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1936                                 struct file *file, void *fh, void *arg)
1937 {
1938         struct v4l2_buffer *p = arg;
1939         int ret = check_fmt(file, p->type);
1940
1941         return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1942 }
1943
1944 static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1945                                 struct file *file, void *fh, void *arg)
1946 {
1947         struct v4l2_buffer *p = arg;
1948         int ret = check_fmt(file, p->type);
1949
1950         return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1951 }
1952
1953 static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1954                                 struct file *file, void *fh, void *arg)
1955 {
1956         struct v4l2_buffer *p = arg;
1957         int ret = check_fmt(file, p->type);
1958
1959         return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1960 }
1961
1962 static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1963                                 struct file *file, void *fh, void *arg)
1964 {
1965         struct v4l2_create_buffers *create = arg;
1966         int ret = check_fmt(file, create->format.type);
1967
1968         if (ret)
1969                 return ret;
1970
1971         CLEAR_AFTER_FIELD(create, capabilities);
1972
1973         v4l_sanitize_format(&create->format);
1974
1975         ret = ops->vidioc_create_bufs(file, fh, create);
1976
1977         if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1978             create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1979                 create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1980
1981         return ret;
1982 }
1983
1984 static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1985                                 struct file *file, void *fh, void *arg)
1986 {
1987         struct v4l2_buffer *b = arg;
1988         int ret = check_fmt(file, b->type);
1989
1990         return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1991 }
1992
1993 static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1994                                 struct file *file, void *fh, void *arg)
1995 {
1996         struct v4l2_streamparm *p = arg;
1997         v4l2_std_id std;
1998         int ret = check_fmt(file, p->type);
1999
2000         if (ret)
2001                 return ret;
2002         if (ops->vidioc_g_parm)
2003                 return ops->vidioc_g_parm(file, fh, p);
2004         if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2005             p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2006                 return -EINVAL;
2007         p->parm.capture.readbuffers = 2;
2008         ret = ops->vidioc_g_std(file, fh, &std);
2009         if (ret == 0)
2010                 v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
2011         return ret;
2012 }
2013
2014 static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
2015                                 struct file *file, void *fh, void *arg)
2016 {
2017         struct v4l2_streamparm *p = arg;
2018         int ret = check_fmt(file, p->type);
2019
2020         if (ret)
2021                 return ret;
2022
2023         /* Note: extendedmode is never used in drivers */
2024         if (V4L2_TYPE_IS_OUTPUT(p->type)) {
2025                 memset(p->parm.output.reserved, 0,
2026                        sizeof(p->parm.output.reserved));
2027                 p->parm.output.extendedmode = 0;
2028                 p->parm.output.outputmode &= V4L2_MODE_HIGHQUALITY;
2029         } else {
2030                 memset(p->parm.capture.reserved, 0,
2031                        sizeof(p->parm.capture.reserved));
2032                 p->parm.capture.extendedmode = 0;
2033                 p->parm.capture.capturemode &= V4L2_MODE_HIGHQUALITY;
2034         }
2035         return ops->vidioc_s_parm(file, fh, p);
2036 }
2037
2038 static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
2039                                 struct file *file, void *fh, void *arg)
2040 {
2041         struct video_device *vfd = video_devdata(file);
2042         struct v4l2_queryctrl *p = arg;
2043         struct v4l2_fh *vfh =
2044                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2045
2046         if (vfh && vfh->ctrl_handler)
2047                 return v4l2_queryctrl(vfh->ctrl_handler, p);
2048         if (vfd->ctrl_handler)
2049                 return v4l2_queryctrl(vfd->ctrl_handler, p);
2050         if (ops->vidioc_queryctrl)
2051                 return ops->vidioc_queryctrl(file, fh, p);
2052         return -ENOTTY;
2053 }
2054
2055 static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
2056                                 struct file *file, void *fh, void *arg)
2057 {
2058         struct video_device *vfd = video_devdata(file);
2059         struct v4l2_query_ext_ctrl *p = arg;
2060         struct v4l2_fh *vfh =
2061                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2062
2063         if (vfh && vfh->ctrl_handler)
2064                 return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
2065         if (vfd->ctrl_handler)
2066                 return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
2067         if (ops->vidioc_query_ext_ctrl)
2068                 return ops->vidioc_query_ext_ctrl(file, fh, p);
2069         return -ENOTTY;
2070 }
2071
2072 static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
2073                                 struct file *file, void *fh, void *arg)
2074 {
2075         struct video_device *vfd = video_devdata(file);
2076         struct v4l2_querymenu *p = arg;
2077         struct v4l2_fh *vfh =
2078                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2079
2080         if (vfh && vfh->ctrl_handler)
2081                 return v4l2_querymenu(vfh->ctrl_handler, p);
2082         if (vfd->ctrl_handler)
2083                 return v4l2_querymenu(vfd->ctrl_handler, p);
2084         if (ops->vidioc_querymenu)
2085                 return ops->vidioc_querymenu(file, fh, p);
2086         return -ENOTTY;
2087 }
2088
2089 static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
2090                                 struct file *file, void *fh, void *arg)
2091 {
2092         struct video_device *vfd = video_devdata(file);
2093         struct v4l2_control *p = arg;
2094         struct v4l2_fh *vfh =
2095                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2096         struct v4l2_ext_controls ctrls;
2097         struct v4l2_ext_control ctrl;
2098
2099         if (vfh && vfh->ctrl_handler)
2100                 return v4l2_g_ctrl(vfh->ctrl_handler, p);
2101         if (vfd->ctrl_handler)
2102                 return v4l2_g_ctrl(vfd->ctrl_handler, p);
2103         if (ops->vidioc_g_ctrl)
2104                 return ops->vidioc_g_ctrl(file, fh, p);
2105         if (ops->vidioc_g_ext_ctrls == NULL)
2106                 return -ENOTTY;
2107
2108         ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2109         ctrls.count = 1;
2110         ctrls.controls = &ctrl;
2111         ctrl.id = p->id;
2112         ctrl.value = p->value;
2113         if (check_ext_ctrls(&ctrls, 1)) {
2114                 int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
2115
2116                 if (ret == 0)
2117                         p->value = ctrl.value;
2118                 return ret;
2119         }
2120         return -EINVAL;
2121 }
2122
2123 static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
2124                                 struct file *file, void *fh, void *arg)
2125 {
2126         struct video_device *vfd = video_devdata(file);
2127         struct v4l2_control *p = arg;
2128         struct v4l2_fh *vfh =
2129                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2130         struct v4l2_ext_controls ctrls;
2131         struct v4l2_ext_control ctrl;
2132
2133         if (vfh && vfh->ctrl_handler)
2134                 return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
2135         if (vfd->ctrl_handler)
2136                 return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
2137         if (ops->vidioc_s_ctrl)
2138                 return ops->vidioc_s_ctrl(file, fh, p);
2139         if (ops->vidioc_s_ext_ctrls == NULL)
2140                 return -ENOTTY;
2141
2142         ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2143         ctrls.count = 1;
2144         ctrls.controls = &ctrl;
2145         ctrl.id = p->id;
2146         ctrl.value = p->value;
2147         if (check_ext_ctrls(&ctrls, 1))
2148                 return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
2149         return -EINVAL;
2150 }
2151
2152 static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2153                                 struct file *file, void *fh, void *arg)
2154 {
2155         struct video_device *vfd = video_devdata(file);
2156         struct v4l2_ext_controls *p = arg;
2157         struct v4l2_fh *vfh =
2158                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2159
2160         p->error_idx = p->count;
2161         if (vfh && vfh->ctrl_handler)
2162                 return v4l2_g_ext_ctrls(vfh->ctrl_handler, vfd->v4l2_dev->mdev, p);
2163         if (vfd->ctrl_handler)
2164                 return v4l2_g_ext_ctrls(vfd->ctrl_handler, vfd->v4l2_dev->mdev, p);
2165         if (ops->vidioc_g_ext_ctrls == NULL)
2166                 return -ENOTTY;
2167         return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
2168                                         -EINVAL;
2169 }
2170
2171 static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2172                                 struct file *file, void *fh, void *arg)
2173 {
2174         struct video_device *vfd = video_devdata(file);
2175         struct v4l2_ext_controls *p = arg;
2176         struct v4l2_fh *vfh =
2177                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2178
2179         p->error_idx = p->count;
2180         if (vfh && vfh->ctrl_handler)
2181                 return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, vfd->v4l2_dev->mdev, p);
2182         if (vfd->ctrl_handler)
2183                 return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, vfd->v4l2_dev->mdev, p);
2184         if (ops->vidioc_s_ext_ctrls == NULL)
2185                 return -ENOTTY;
2186         return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
2187                                         -EINVAL;
2188 }
2189
2190 static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2191                                 struct file *file, void *fh, void *arg)
2192 {
2193         struct video_device *vfd = video_devdata(file);
2194         struct v4l2_ext_controls *p = arg;
2195         struct v4l2_fh *vfh =
2196                 test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2197
2198         p->error_idx = p->count;
2199         if (vfh && vfh->ctrl_handler)
2200                 return v4l2_try_ext_ctrls(vfh->ctrl_handler, vfd->v4l2_dev->mdev, p);
2201         if (vfd->ctrl_handler)
2202                 return v4l2_try_ext_ctrls(vfd->ctrl_handler, vfd->v4l2_dev->mdev, p);
2203         if (ops->vidioc_try_ext_ctrls == NULL)
2204                 return -ENOTTY;
2205         return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
2206                                         -EINVAL;
2207 }
2208
2209 /*
2210  * The selection API specified originally that the _MPLANE buffer types
2211  * shouldn't be used. The reasons for this are lost in the mists of time
2212  * (or just really crappy memories). Regardless, this is really annoying
2213  * for userspace. So to keep things simple we map _MPLANE buffer types
2214  * to their 'regular' counterparts before calling the driver. And we
2215  * restore it afterwards. This way applications can use either buffer
2216  * type and drivers don't need to check for both.
2217  */
2218 static int v4l_g_selection(const struct v4l2_ioctl_ops *ops,
2219                            struct file *file, void *fh, void *arg)
2220 {
2221         struct v4l2_selection *p = arg;
2222         u32 old_type = p->type;
2223         int ret;
2224
2225         if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2226                 p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2227         else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2228                 p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2229         ret = ops->vidioc_g_selection(file, fh, p);
2230         p->type = old_type;
2231         return ret;
2232 }
2233
2234 static int v4l_s_selection(const struct v4l2_ioctl_ops *ops,
2235                            struct file *file, void *fh, void *arg)
2236 {
2237         struct v4l2_selection *p = arg;
2238         u32 old_type = p->type;
2239         int ret;
2240
2241         if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2242                 p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2243         else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2244                 p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2245         ret = ops->vidioc_s_selection(file, fh, p);
2246         p->type = old_type;
2247         return ret;
2248 }
2249
2250 static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
2251                                 struct file *file, void *fh, void *arg)
2252 {
2253         struct video_device *vfd = video_devdata(file);
2254         struct v4l2_crop *p = arg;
2255         struct v4l2_selection s = {
2256                 .type = p->type,
2257         };
2258         int ret;
2259
2260         /* simulate capture crop using selection api */
2261
2262         /* crop means compose for output devices */
2263         if (V4L2_TYPE_IS_OUTPUT(p->type))
2264                 s.target = V4L2_SEL_TGT_COMPOSE;
2265         else
2266                 s.target = V4L2_SEL_TGT_CROP;
2267
2268         if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2269                 s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2270                         V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2271
2272         ret = v4l_g_selection(ops, file, fh, &s);
2273
2274         /* copying results to old structure on success */
2275         if (!ret)
2276                 p->c = s.r;
2277         return ret;
2278 }
2279
2280 static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
2281                                 struct file *file, void *fh, void *arg)
2282 {
2283         struct video_device *vfd = video_devdata(file);
2284         struct v4l2_crop *p = arg;
2285         struct v4l2_selection s = {
2286                 .type = p->type,
2287                 .r = p->c,
2288         };
2289
2290         /* simulate capture crop using selection api */
2291
2292         /* crop means compose for output devices */
2293         if (V4L2_TYPE_IS_OUTPUT(p->type))
2294                 s.target = V4L2_SEL_TGT_COMPOSE;
2295         else
2296                 s.target = V4L2_SEL_TGT_CROP;
2297
2298         if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2299                 s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2300                         V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2301
2302         return v4l_s_selection(ops, file, fh, &s);
2303 }
2304
2305 static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
2306                                 struct file *file, void *fh, void *arg)
2307 {
2308         struct video_device *vfd = video_devdata(file);
2309         struct v4l2_cropcap *p = arg;
2310         struct v4l2_selection s = { .type = p->type };
2311         int ret = 0;
2312
2313         /* setting trivial pixelaspect */
2314         p->pixelaspect.numerator = 1;
2315         p->pixelaspect.denominator = 1;
2316
2317         if (s.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2318                 s.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2319         else if (s.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2320                 s.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2321
2322         /*
2323          * The determine_valid_ioctls() call already should ensure
2324          * that this can never happen, but just in case...
2325          */
2326         if (WARN_ON(!ops->vidioc_g_selection))
2327                 return -ENOTTY;
2328
2329         if (ops->vidioc_g_pixelaspect)
2330                 ret = ops->vidioc_g_pixelaspect(file, fh, s.type,
2331                                                 &p->pixelaspect);
2332
2333         /*
2334          * Ignore ENOTTY or ENOIOCTLCMD error returns, just use the
2335          * square pixel aspect ratio in that case.
2336          */
2337         if (ret && ret != -ENOTTY && ret != -ENOIOCTLCMD)
2338                 return ret;
2339
2340         /* Use g_selection() to fill in the bounds and defrect rectangles */
2341
2342         /* obtaining bounds */
2343         if (V4L2_TYPE_IS_OUTPUT(p->type))
2344                 s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
2345         else
2346                 s.target = V4L2_SEL_TGT_CROP_BOUNDS;
2347
2348         if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2349                 s.target = s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS ?
2350                         V4L2_SEL_TGT_CROP_BOUNDS : V4L2_SEL_TGT_COMPOSE_BOUNDS;
2351
2352         ret = v4l_g_selection(ops, file, fh, &s);
2353         if (ret)
2354                 return ret;
2355         p->bounds = s.r;
2356
2357         /* obtaining defrect */
2358         if (s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS)
2359                 s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
2360         else
2361                 s.target = V4L2_SEL_TGT_CROP_DEFAULT;
2362
2363         ret = v4l_g_selection(ops, file, fh, &s);
2364         if (ret)
2365                 return ret;
2366         p->defrect = s.r;
2367
2368         return 0;
2369 }
2370
2371 static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
2372                                 struct file *file, void *fh, void *arg)
2373 {
2374         struct video_device *vfd = video_devdata(file);
2375         int ret;
2376
2377         if (vfd->v4l2_dev)
2378                 pr_info("%s: =================  START STATUS  =================\n",
2379                         vfd->v4l2_dev->name);
2380         ret = ops->vidioc_log_status(file, fh);
2381         if (vfd->v4l2_dev)
2382                 pr_info("%s: ==================  END STATUS  ==================\n",
2383                         vfd->v4l2_dev->name);
2384         return ret;
2385 }
2386
2387 static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
2388                                 struct file *file, void *fh, void *arg)
2389 {
2390 #ifdef CONFIG_VIDEO_ADV_DEBUG
2391         struct v4l2_dbg_register *p = arg;
2392         struct video_device *vfd = video_devdata(file);
2393         struct v4l2_subdev *sd;
2394         int idx = 0;
2395
2396         if (!capable(CAP_SYS_ADMIN))
2397                 return -EPERM;
2398         if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2399                 if (vfd->v4l2_dev == NULL)
2400                         return -EINVAL;
2401                 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2402                         if (p->match.addr == idx++)
2403                                 return v4l2_subdev_call(sd, core, g_register, p);
2404                 return -EINVAL;
2405         }
2406         if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2407             (ops->vidioc_g_chip_info || p->match.addr == 0))
2408                 return ops->vidioc_g_register(file, fh, p);
2409         return -EINVAL;
2410 #else
2411         return -ENOTTY;
2412 #endif
2413 }
2414
2415 static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
2416                                 struct file *file, void *fh, void *arg)
2417 {
2418 #ifdef CONFIG_VIDEO_ADV_DEBUG
2419         const struct v4l2_dbg_register *p = arg;
2420         struct video_device *vfd = video_devdata(file);
2421         struct v4l2_subdev *sd;
2422         int idx = 0;
2423
2424         if (!capable(CAP_SYS_ADMIN))
2425                 return -EPERM;
2426         if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2427                 if (vfd->v4l2_dev == NULL)
2428                         return -EINVAL;
2429                 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2430                         if (p->match.addr == idx++)
2431                                 return v4l2_subdev_call(sd, core, s_register, p);
2432                 return -EINVAL;
2433         }
2434         if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2435             (ops->vidioc_g_chip_info || p->match.addr == 0))
2436                 return ops->vidioc_s_register(file, fh, p);
2437         return -EINVAL;
2438 #else
2439         return -ENOTTY;
2440 #endif
2441 }
2442
2443 static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
2444                                 struct file *file, void *fh, void *arg)
2445 {
2446 #ifdef CONFIG_VIDEO_ADV_DEBUG
2447         struct video_device *vfd = video_devdata(file);
2448         struct v4l2_dbg_chip_info *p = arg;
2449         struct v4l2_subdev *sd;
2450         int idx = 0;
2451
2452         switch (p->match.type) {
2453         case V4L2_CHIP_MATCH_BRIDGE:
2454                 if (ops->vidioc_s_register)
2455                         p->flags |= V4L2_CHIP_FL_WRITABLE;
2456                 if (ops->vidioc_g_register)
2457                         p->flags |= V4L2_CHIP_FL_READABLE;
2458                 strscpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
2459                 if (ops->vidioc_g_chip_info)
2460                         return ops->vidioc_g_chip_info(file, fh, arg);
2461                 if (p->match.addr)
2462                         return -EINVAL;
2463                 return 0;
2464
2465         case V4L2_CHIP_MATCH_SUBDEV:
2466                 if (vfd->v4l2_dev == NULL)
2467                         break;
2468                 v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
2469                         if (p->match.addr != idx++)
2470                                 continue;
2471                         if (sd->ops->core && sd->ops->core->s_register)
2472                                 p->flags |= V4L2_CHIP_FL_WRITABLE;
2473                         if (sd->ops->core && sd->ops->core->g_register)
2474                                 p->flags |= V4L2_CHIP_FL_READABLE;
2475                         strscpy(p->name, sd->name, sizeof(p->name));
2476                         return 0;
2477                 }
2478                 break;
2479         }
2480         return -EINVAL;
2481 #else
2482         return -ENOTTY;
2483 #endif
2484 }
2485
2486 static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
2487                                 struct file *file, void *fh, void *arg)
2488 {
2489         return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
2490 }
2491
2492 static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
2493                                 struct file *file, void *fh, void *arg)
2494 {
2495         return ops->vidioc_subscribe_event(fh, arg);
2496 }
2497
2498 static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
2499                                 struct file *file, void *fh, void *arg)
2500 {
2501         return ops->vidioc_unsubscribe_event(fh, arg);
2502 }
2503
2504 static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
2505                                 struct file *file, void *fh, void *arg)
2506 {
2507         struct v4l2_sliced_vbi_cap *p = arg;
2508         int ret = check_fmt(file, p->type);
2509
2510         if (ret)
2511                 return ret;
2512
2513         /* Clear up to type, everything after type is zeroed already */
2514         memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
2515
2516         return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
2517 }
2518
2519 static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
2520                                 struct file *file, void *fh, void *arg)
2521 {
2522         struct video_device *vfd = video_devdata(file);
2523         struct v4l2_frequency_band *p = arg;
2524         enum v4l2_tuner_type type;
2525         int err;
2526
2527         if (vfd->vfl_type == VFL_TYPE_SDR) {
2528                 if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
2529                         return -EINVAL;
2530                 type = p->type;
2531         } else {
2532                 type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2533                                 V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2534                 if (type != p->type)
2535                         return -EINVAL;
2536         }
2537         if (ops->vidioc_enum_freq_bands) {
2538                 err = ops->vidioc_enum_freq_bands(file, fh, p);
2539                 if (err != -ENOTTY)
2540                         return err;
2541         }
2542         if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
2543                 struct v4l2_tuner t = {
2544                         .index = p->tuner,
2545                         .type = type,
2546                 };
2547
2548                 if (p->index)
2549                         return -EINVAL;
2550                 err = ops->vidioc_g_tuner(file, fh, &t);
2551                 if (err)
2552                         return err;
2553                 p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2554                 p->rangelow = t.rangelow;
2555                 p->rangehigh = t.rangehigh;
2556                 p->modulation = (type == V4L2_TUNER_RADIO) ?
2557                         V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2558                 return 0;
2559         }
2560         if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
2561                 struct v4l2_modulator m = {
2562                         .index = p->tuner,
2563                 };
2564
2565                 if (type != V4L2_TUNER_RADIO)
2566                         return -EINVAL;
2567                 if (p->index)
2568                         return -EINVAL;
2569                 err = ops->vidioc_g_modulator(file, fh, &m);
2570                 if (err)
2571                         return err;
2572                 p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2573                 p->rangelow = m.rangelow;
2574                 p->rangehigh = m.rangehigh;
2575                 p->modulation = (type == V4L2_TUNER_RADIO) ?
2576                         V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2577                 return 0;
2578         }
2579         return -ENOTTY;
2580 }
2581
2582 struct v4l2_ioctl_info {
2583         unsigned int ioctl;
2584         u32 flags;
2585         const char * const name;
2586         int (*func)(const struct v4l2_ioctl_ops *ops, struct file *file,
2587                     void *fh, void *p);
2588         void (*debug)(const void *arg, bool write_only);
2589 };
2590
2591 /* This control needs a priority check */
2592 #define INFO_FL_PRIO            (1 << 0)
2593 /* This control can be valid if the filehandle passes a control handler. */
2594 #define INFO_FL_CTRL            (1 << 1)
2595 /* Queuing ioctl */
2596 #define INFO_FL_QUEUE           (1 << 2)
2597 /* Always copy back result, even on error */
2598 #define INFO_FL_ALWAYS_COPY     (1 << 3)
2599 /* Zero struct from after the field to the end */
2600 #define INFO_FL_CLEAR(v4l2_struct, field)                       \
2601         ((offsetof(struct v4l2_struct, field) +                 \
2602           sizeof(((struct v4l2_struct *)0)->field)) << 16)
2603 #define INFO_FL_CLEAR_MASK      (_IOC_SIZEMASK << 16)
2604
2605 #define DEFINE_V4L_STUB_FUNC(_vidioc)                           \
2606         static int v4l_stub_ ## _vidioc(                        \
2607                         const struct v4l2_ioctl_ops *ops,       \
2608                         struct file *file, void *fh, void *p)   \
2609         {                                                       \
2610                 return ops->vidioc_ ## _vidioc(file, fh, p);    \
2611         }
2612
2613 #define IOCTL_INFO(_ioctl, _func, _debug, _flags)               \
2614         [_IOC_NR(_ioctl)] = {                                   \
2615                 .ioctl = _ioctl,                                \
2616                 .flags = _flags,                                \
2617                 .name = #_ioctl,                                \
2618                 .func = _func,                                  \
2619                 .debug = _debug,                                \
2620         }
2621
2622 DEFINE_V4L_STUB_FUNC(g_fbuf)
2623 DEFINE_V4L_STUB_FUNC(s_fbuf)
2624 DEFINE_V4L_STUB_FUNC(expbuf)
2625 DEFINE_V4L_STUB_FUNC(g_std)
2626 DEFINE_V4L_STUB_FUNC(g_audio)
2627 DEFINE_V4L_STUB_FUNC(s_audio)
2628 DEFINE_V4L_STUB_FUNC(g_input)
2629 DEFINE_V4L_STUB_FUNC(g_edid)
2630 DEFINE_V4L_STUB_FUNC(s_edid)
2631 DEFINE_V4L_STUB_FUNC(g_output)
2632 DEFINE_V4L_STUB_FUNC(g_audout)
2633 DEFINE_V4L_STUB_FUNC(s_audout)
2634 DEFINE_V4L_STUB_FUNC(g_jpegcomp)
2635 DEFINE_V4L_STUB_FUNC(s_jpegcomp)
2636 DEFINE_V4L_STUB_FUNC(enumaudio)
2637 DEFINE_V4L_STUB_FUNC(enumaudout)
2638 DEFINE_V4L_STUB_FUNC(enum_framesizes)
2639 DEFINE_V4L_STUB_FUNC(enum_frameintervals)
2640 DEFINE_V4L_STUB_FUNC(g_enc_index)
2641 DEFINE_V4L_STUB_FUNC(encoder_cmd)
2642 DEFINE_V4L_STUB_FUNC(try_encoder_cmd)
2643 DEFINE_V4L_STUB_FUNC(decoder_cmd)
2644 DEFINE_V4L_STUB_FUNC(try_decoder_cmd)
2645 DEFINE_V4L_STUB_FUNC(s_dv_timings)
2646 DEFINE_V4L_STUB_FUNC(g_dv_timings)
2647 DEFINE_V4L_STUB_FUNC(enum_dv_timings)
2648 DEFINE_V4L_STUB_FUNC(query_dv_timings)
2649 DEFINE_V4L_STUB_FUNC(dv_timings_cap)
2650
2651 static const struct v4l2_ioctl_info v4l2_ioctls[] = {
2652         IOCTL_INFO(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
2653         IOCTL_INFO(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
2654         IOCTL_INFO(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
2655         IOCTL_INFO(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
2656         IOCTL_INFO(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2657         IOCTL_INFO(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
2658         IOCTL_INFO(VIDIOC_G_FBUF, v4l_stub_g_fbuf, v4l_print_framebuffer, 0),
2659         IOCTL_INFO(VIDIOC_S_FBUF, v4l_stub_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
2660         IOCTL_INFO(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
2661         IOCTL_INFO(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
2662         IOCTL_INFO(VIDIOC_EXPBUF, v4l_stub_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
2663         IOCTL_INFO(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2664         IOCTL_INFO(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2665         IOCTL_INFO(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2666         IOCTL_INFO(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2667         IOCTL_INFO(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
2668         IOCTL_INFO(VIDIOC_G_STD, v4l_stub_g_std, v4l_print_std, 0),
2669         IOCTL_INFO(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2670         IOCTL_INFO(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
2671         IOCTL_INFO(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
2672         IOCTL_INFO(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2673         IOCTL_INFO(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
2674         IOCTL_INFO(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2675         IOCTL_INFO(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
2676         IOCTL_INFO(VIDIOC_G_AUDIO, v4l_stub_g_audio, v4l_print_audio, 0),
2677         IOCTL_INFO(VIDIOC_S_AUDIO, v4l_stub_s_audio, v4l_print_audio, INFO_FL_PRIO),
2678         IOCTL_INFO(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2679         IOCTL_INFO(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
2680         IOCTL_INFO(VIDIOC_G_INPUT, v4l_stub_g_input, v4l_print_u32, 0),
2681         IOCTL_INFO(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
2682         IOCTL_INFO(VIDIOC_G_EDID, v4l_stub_g_edid, v4l_print_edid, INFO_FL_ALWAYS_COPY),
2683         IOCTL_INFO(VIDIOC_S_EDID, v4l_stub_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_ALWAYS_COPY),
2684         IOCTL_INFO(VIDIOC_G_OUTPUT, v4l_stub_g_output, v4l_print_u32, 0),
2685         IOCTL_INFO(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2686         IOCTL_INFO(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2687         IOCTL_INFO(VIDIOC_G_AUDOUT, v4l_stub_g_audout, v4l_print_audioout, 0),
2688         IOCTL_INFO(VIDIOC_S_AUDOUT, v4l_stub_s_audout, v4l_print_audioout, INFO_FL_PRIO),
2689         IOCTL_INFO(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
2690         IOCTL_INFO(VIDIOC_S_MODULATOR, v4l_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2691         IOCTL_INFO(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2692         IOCTL_INFO(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
2693         IOCTL_INFO(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2694         IOCTL_INFO(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2695         IOCTL_INFO(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2696         IOCTL_INFO(VIDIOC_G_SELECTION, v4l_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
2697         IOCTL_INFO(VIDIOC_S_SELECTION, v4l_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
2698         IOCTL_INFO(VIDIOC_G_JPEGCOMP, v4l_stub_g_jpegcomp, v4l_print_jpegcompression, 0),
2699         IOCTL_INFO(VIDIOC_S_JPEGCOMP, v4l_stub_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
2700         IOCTL_INFO(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
2701         IOCTL_INFO(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
2702         IOCTL_INFO(VIDIOC_ENUMAUDIO, v4l_stub_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2703         IOCTL_INFO(VIDIOC_ENUMAUDOUT, v4l_stub_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
2704         IOCTL_INFO(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2705         IOCTL_INFO(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
2706         IOCTL_INFO(VIDIOC_G_SLICED_VBI_CAP, v4l_g_sliced_vbi_cap, v4l_print_sliced_vbi_cap, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
2707         IOCTL_INFO(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
2708         IOCTL_INFO(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2709         IOCTL_INFO(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
2710         IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2711         IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, v4l_stub_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2712         IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, v4l_stub_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
2713         IOCTL_INFO(VIDIOC_G_ENC_INDEX, v4l_stub_g_enc_index, v4l_print_enc_idx, 0),
2714         IOCTL_INFO(VIDIOC_ENCODER_CMD, v4l_stub_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2715         IOCTL_INFO(VIDIOC_TRY_ENCODER_CMD, v4l_stub_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2716         IOCTL_INFO(VIDIOC_DECODER_CMD, v4l_stub_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2717         IOCTL_INFO(VIDIOC_TRY_DECODER_CMD, v4l_stub_try_decoder_cmd, v4l_print_decoder_cmd, 0),
2718         IOCTL_INFO(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2719         IOCTL_INFO(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2720         IOCTL_INFO(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
2721         IOCTL_INFO(VIDIOC_S_DV_TIMINGS, v4l_stub_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_dv_timings, bt.flags)),
2722         IOCTL_INFO(VIDIOC_G_DV_TIMINGS, v4l_stub_g_dv_timings, v4l_print_dv_timings, 0),
2723         IOCTL_INFO(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2724         IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2725         IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
2726         IOCTL_INFO(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2727         IOCTL_INFO(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
2728         IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, v4l_stub_enum_dv_timings, v4l_print_enum_dv_timings, INFO_FL_CLEAR(v4l2_enum_dv_timings, pad)),
2729         IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, v4l_stub_query_dv_timings, v4l_print_dv_timings, INFO_FL_ALWAYS_COPY),
2730         IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, v4l_stub_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, pad)),
2731         IOCTL_INFO(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
2732         IOCTL_INFO(VIDIOC_DBG_G_CHIP_INFO, v4l_dbg_g_chip_info, v4l_print_dbg_chip_info, INFO_FL_CLEAR(v4l2_dbg_chip_info, match)),
2733         IOCTL_INFO(VIDIOC_QUERY_EXT_CTRL, v4l_query_ext_ctrl, v4l_print_query_ext_ctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_query_ext_ctrl, id)),
2734 };
2735 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2736
2737 static bool v4l2_is_known_ioctl(unsigned int cmd)
2738 {
2739         if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2740                 return false;
2741         return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2742 }
2743
2744 static struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev,
2745                                          struct v4l2_fh *vfh, unsigned int cmd,
2746                                          void *arg)
2747 {
2748         if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2749                 return vdev->lock;
2750 #if IS_ENABLED(CONFIG_V4L2_MEM2MEM_DEV)
2751         if (vfh && vfh->m2m_ctx &&
2752             (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE)) {
2753                 if (vfh->m2m_ctx->q_lock)
2754                         return vfh->m2m_ctx->q_lock;
2755         }
2756 #endif
2757         if (vdev->queue && vdev->queue->lock &&
2758                         (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2759                 return vdev->queue->lock;
2760         return vdev->lock;
2761 }
2762
2763 /* Common ioctl debug function. This function can be used by
2764    external ioctl messages as well as internal V4L ioctl */
2765 void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
2766 {
2767         const char *dir, *type;
2768
2769         if (prefix)
2770                 printk(KERN_DEBUG "%s: ", prefix);
2771
2772         switch (_IOC_TYPE(cmd)) {
2773         case 'd':
2774                 type = "v4l2_int";
2775                 break;
2776         case 'V':
2777                 if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2778                         type = "v4l2";
2779                         break;
2780                 }
2781                 pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
2782                 return;
2783         default:
2784                 type = "unknown";
2785                 break;
2786         }
2787
2788         switch (_IOC_DIR(cmd)) {
2789         case _IOC_NONE:              dir = "--"; break;
2790         case _IOC_READ:              dir = "r-"; break;
2791         case _IOC_WRITE:             dir = "-w"; break;
2792         case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2793         default:                     dir = "*ERR*"; break;
2794         }
2795         pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
2796                 type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2797 }
2798 EXPORT_SYMBOL(v4l_printk_ioctl);
2799
2800 static long __video_do_ioctl(struct file *file,
2801                 unsigned int cmd, void *arg)
2802 {
2803         struct video_device *vfd = video_devdata(file);
2804         struct mutex *req_queue_lock = NULL;
2805         struct mutex *lock; /* ioctl serialization mutex */
2806         const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
2807         bool write_only = false;
2808         struct v4l2_ioctl_info default_info;
2809         const struct v4l2_ioctl_info *info;
2810         void *fh = file->private_data;
2811         struct v4l2_fh *vfh = NULL;
2812         int dev_debug = vfd->dev_debug;
2813         long ret = -ENOTTY;
2814
2815         if (ops == NULL) {
2816                 pr_warn("%s: has no ioctl_ops.\n",
2817                                 video_device_node_name(vfd));
2818                 return ret;
2819         }
2820
2821         if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
2822                 vfh = file->private_data;
2823
2824         /*
2825          * We need to serialize streamon/off with queueing new requests.
2826          * These ioctls may trigger the cancellation of a streaming
2827          * operation, and that should not be mixed with queueing a new
2828          * request at the same time.
2829          */
2830         if (v4l2_device_supports_requests(vfd->v4l2_dev) &&
2831             (cmd == VIDIOC_STREAMON || cmd == VIDIOC_STREAMOFF)) {
2832                 req_queue_lock = &vfd->v4l2_dev->mdev->req_queue_mutex;
2833
2834                 if (mutex_lock_interruptible(req_queue_lock))
2835                         return -ERESTARTSYS;
2836         }
2837
2838         lock = v4l2_ioctl_get_lock(vfd, vfh, cmd, arg);
2839
2840         if (lock && mutex_lock_interruptible(lock)) {
2841                 if (req_queue_lock)
2842                         mutex_unlock(req_queue_lock);
2843                 return -ERESTARTSYS;
2844         }
2845
2846         if (!video_is_registered(vfd)) {
2847                 ret = -ENODEV;
2848                 goto unlock;
2849         }
2850
2851         if (v4l2_is_known_ioctl(cmd)) {
2852                 info = &v4l2_ioctls[_IOC_NR(cmd)];
2853
2854                 if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2855                     !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
2856                         goto done;
2857
2858                 if (vfh && (info->flags & INFO_FL_PRIO)) {
2859                         ret = v4l2_prio_check(vfd->prio, vfh->prio);
2860                         if (ret)
2861                                 goto done;
2862                 }
2863         } else {
2864                 default_info.ioctl = cmd;
2865                 default_info.flags = 0;
2866                 default_info.debug = v4l_print_default;
2867                 info = &default_info;
2868         }
2869
2870         write_only = _IOC_DIR(cmd) == _IOC_WRITE;
2871         if (info != &default_info) {
2872                 ret = info->func(ops, file, fh, arg);
2873         } else if (!ops->vidioc_default) {
2874                 ret = -ENOTTY;
2875         } else {
2876                 ret = ops->vidioc_default(file, fh,
2877                         vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2878                         cmd, arg);
2879         }
2880
2881 done:
2882         if (dev_debug & (V4L2_DEV_DEBUG_IOCTL | V4L2_DEV_DEBUG_IOCTL_ARG)) {
2883                 if (!(dev_debug & V4L2_DEV_DEBUG_STREAMING) &&
2884                     (cmd == VIDIOC_QBUF || cmd == VIDIOC_DQBUF))
2885                         goto unlock;
2886
2887                 v4l_printk_ioctl(video_device_node_name(vfd), cmd);
2888                 if (ret < 0)
2889                         pr_cont(": error %ld", ret);
2890                 if (!(dev_debug & V4L2_DEV_DEBUG_IOCTL_ARG))
2891                         pr_cont("\n");
2892                 else if (_IOC_DIR(cmd) == _IOC_NONE)
2893                         info->debug(arg, write_only);
2894                 else {
2895                         pr_cont(": ");
2896                         info->debug(arg, write_only);
2897                 }
2898         }
2899
2900 unlock:
2901         if (lock)
2902                 mutex_unlock(lock);
2903         if (req_queue_lock)
2904                 mutex_unlock(req_queue_lock);
2905         return ret;
2906 }
2907
2908 static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2909                             void __user **user_ptr, void ***kernel_ptr)
2910 {
2911         int ret = 0;
2912
2913         switch (cmd) {
2914         case VIDIOC_PREPARE_BUF:
2915         case VIDIOC_QUERYBUF:
2916         case VIDIOC_QBUF:
2917         case VIDIOC_DQBUF: {
2918                 struct v4l2_buffer *buf = parg;
2919
2920                 if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2921                         if (buf->length > VIDEO_MAX_PLANES) {
2922                                 ret = -EINVAL;
2923                                 break;
2924                         }
2925                         *user_ptr = (void __user *)buf->m.planes;
2926                         *kernel_ptr = (void **)&buf->m.planes;
2927                         *array_size = sizeof(struct v4l2_plane) * buf->length;
2928                         ret = 1;
2929                 }
2930                 break;
2931         }
2932
2933         case VIDIOC_G_EDID:
2934         case VIDIOC_S_EDID: {
2935                 struct v4l2_edid *edid = parg;
2936
2937                 if (edid->blocks) {
2938                         if (edid->blocks > 256) {
2939                                 ret = -EINVAL;
2940                                 break;
2941                         }
2942                         *user_ptr = (void __user *)edid->edid;
2943                         *kernel_ptr = (void **)&edid->edid;
2944                         *array_size = edid->blocks * 128;
2945                         ret = 1;
2946                 }
2947                 break;
2948         }
2949
2950         case VIDIOC_S_EXT_CTRLS:
2951         case VIDIOC_G_EXT_CTRLS:
2952         case VIDIOC_TRY_EXT_CTRLS: {
2953                 struct v4l2_ext_controls *ctrls = parg;
2954
2955                 if (ctrls->count != 0) {
2956                         if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2957                                 ret = -EINVAL;
2958                                 break;
2959                         }
2960                         *user_ptr = (void __user *)ctrls->controls;
2961                         *kernel_ptr = (void **)&ctrls->controls;
2962                         *array_size = sizeof(struct v4l2_ext_control)
2963                                     * ctrls->count;
2964                         ret = 1;
2965                 }
2966                 break;
2967         }
2968         }
2969
2970         return ret;
2971 }
2972
2973 long
2974 video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2975                v4l2_kioctl func)
2976 {
2977         char    sbuf[128];
2978         void    *mbuf = NULL;
2979         void    *parg = (void *)arg;
2980         long    err  = -EINVAL;
2981         bool    has_array_args;
2982         bool    always_copy = false;
2983         size_t  array_size = 0;
2984         void __user *user_ptr = NULL;
2985         void    **kernel_ptr = NULL;
2986         const size_t ioc_size = _IOC_SIZE(cmd);
2987
2988         /*  Copy arguments into temp kernel buffer  */
2989         if (_IOC_DIR(cmd) != _IOC_NONE) {
2990                 if (ioc_size <= sizeof(sbuf)) {
2991                         parg = sbuf;
2992                 } else {
2993                         /* too big to allocate from stack */
2994                         mbuf = kvmalloc(ioc_size, GFP_KERNEL);
2995                         if (NULL == mbuf)
2996                                 return -ENOMEM;
2997                         parg = mbuf;
2998                 }
2999
3000                 err = -EFAULT;
3001                 if (_IOC_DIR(cmd) & _IOC_WRITE) {
3002                         unsigned int n = ioc_size;
3003
3004                         /*
3005                          * In some cases, only a few fields are used as input,
3006                          * i.e. when the app sets "index" and then the driver
3007                          * fills in the rest of the structure for the thing
3008                          * with that index.  We only need to copy up the first
3009                          * non-input field.
3010                          */
3011                         if (v4l2_is_known_ioctl(cmd)) {
3012                                 u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
3013
3014                                 if (flags & INFO_FL_CLEAR_MASK)
3015                                         n = (flags & INFO_FL_CLEAR_MASK) >> 16;
3016                                 always_copy = flags & INFO_FL_ALWAYS_COPY;
3017                         }
3018
3019                         if (copy_from_user(parg, (void __user *)arg, n))
3020                                 goto out;
3021
3022                         /* zero out anything we don't copy from userspace */
3023                         if (n < ioc_size)
3024                                 memset((u8 *)parg + n, 0, ioc_size - n);
3025                 } else {
3026                         /* read-only ioctl */
3027                         memset(parg, 0, ioc_size);
3028                 }
3029         }
3030
3031         err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
3032         if (err < 0)
3033                 goto out;
3034         has_array_args = err;
3035
3036         if (has_array_args) {
3037                 /*
3038                  * When adding new types of array args, make sure that the
3039                  * parent argument to ioctl (which contains the pointer to the
3040                  * array) fits into sbuf (so that mbuf will still remain
3041                  * unused up to here).
3042                  */
3043                 mbuf = kvmalloc(array_size, GFP_KERNEL);
3044                 err = -ENOMEM;
3045                 if (NULL == mbuf)
3046                         goto out_array_args;
3047                 err = -EFAULT;
3048                 if (copy_from_user(mbuf, user_ptr, array_size))
3049                         goto out_array_args;
3050                 *kernel_ptr = mbuf;
3051         }
3052
3053         /* Handles IOCTL */
3054         err = func(file, cmd, parg);
3055         if (err == -ENOTTY || err == -ENOIOCTLCMD) {
3056                 err = -ENOTTY;
3057                 goto out;
3058         }
3059
3060         if (err == 0) {
3061                 if (cmd == VIDIOC_DQBUF)
3062                         trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
3063                 else if (cmd == VIDIOC_QBUF)
3064                         trace_v4l2_qbuf(video_devdata(file)->minor, parg);
3065         }
3066
3067         if (has_array_args) {
3068                 *kernel_ptr = (void __force *)user_ptr;
3069                 if (copy_to_user(user_ptr, mbuf, array_size))
3070                         err = -EFAULT;
3071                 goto out_array_args;
3072         }
3073         /*
3074          * Some ioctls can return an error, but still have valid
3075          * results that must be returned.
3076          */
3077         if (err < 0 && !always_copy)
3078                 goto out;
3079
3080 out_array_args:
3081         /*  Copy results into user buffer  */
3082         switch (_IOC_DIR(cmd)) {
3083         case _IOC_READ:
3084         case (_IOC_WRITE | _IOC_READ):
3085                 if (copy_to_user((void __user *)arg, parg, ioc_size))
3086                         err = -EFAULT;
3087                 break;
3088         }
3089
3090 out:
3091         kvfree(mbuf);
3092         return err;
3093 }
3094
3095 long video_ioctl2(struct file *file,
3096                unsigned int cmd, unsigned long arg)
3097 {
3098         return video_usercopy(file, cmd, arg, __video_do_ioctl);
3099 }
3100 EXPORT_SYMBOL(video_ioctl2);