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