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