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