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