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