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