v4l2-ioctl.c revision e6bee3685e732df82f48698254a36754cf15f0b0
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_sdr_format *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_query_ext_ctrl(const void *arg, bool write_only)
529{
530	const struct v4l2_query_ext_ctrl *p = arg;
531
532	pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%lld/%lld, "
533		"step=%lld, default=%lld, flags=0x%08x, elem_size=%u, elems=%u, "
534		"nr_of_dims=%u, dims=%u,%u,%u,%u,%u,%u,%u,%u\n",
535			p->id, p->type, (int)sizeof(p->name), p->name,
536			p->minimum, p->maximum,
537			p->step, p->default_value, p->flags,
538			p->elem_size, p->elems, p->nr_of_dims,
539			p->dims[0], p->dims[1], p->dims[2], p->dims[3],
540			p->dims[4], p->dims[5], p->dims[6], p->dims[7]);
541}
542
543static void v4l_print_querymenu(const void *arg, bool write_only)
544{
545	const struct v4l2_querymenu *p = arg;
546
547	pr_cont("id=0x%x, index=%d\n", p->id, p->index);
548}
549
550static void v4l_print_control(const void *arg, bool write_only)
551{
552	const struct v4l2_control *p = arg;
553
554	pr_cont("id=0x%x, value=%d\n", p->id, p->value);
555}
556
557static void v4l_print_ext_controls(const void *arg, bool write_only)
558{
559	const struct v4l2_ext_controls *p = arg;
560	int i;
561
562	pr_cont("class=0x%x, count=%d, error_idx=%d",
563			p->ctrl_class, p->count, p->error_idx);
564	for (i = 0; i < p->count; i++) {
565		if (p->controls[i].size)
566			pr_cont(", id/val=0x%x/0x%x",
567				p->controls[i].id, p->controls[i].value);
568		else
569			pr_cont(", id/size=0x%x/%u",
570				p->controls[i].id, p->controls[i].size);
571	}
572	pr_cont("\n");
573}
574
575static void v4l_print_cropcap(const void *arg, bool write_only)
576{
577	const struct v4l2_cropcap *p = arg;
578
579	pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, "
580		"defrect wxh=%dx%d, x,y=%d,%d, "
581		"pixelaspect %d/%d\n",
582		prt_names(p->type, v4l2_type_names),
583		p->bounds.width, p->bounds.height,
584		p->bounds.left, p->bounds.top,
585		p->defrect.width, p->defrect.height,
586		p->defrect.left, p->defrect.top,
587		p->pixelaspect.numerator, p->pixelaspect.denominator);
588}
589
590static void v4l_print_crop(const void *arg, bool write_only)
591{
592	const struct v4l2_crop *p = arg;
593
594	pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
595		prt_names(p->type, v4l2_type_names),
596		p->c.width, p->c.height,
597		p->c.left, p->c.top);
598}
599
600static void v4l_print_selection(const void *arg, bool write_only)
601{
602	const struct v4l2_selection *p = arg;
603
604	pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
605		prt_names(p->type, v4l2_type_names),
606		p->target, p->flags,
607		p->r.width, p->r.height, p->r.left, p->r.top);
608}
609
610static void v4l_print_jpegcompression(const void *arg, bool write_only)
611{
612	const struct v4l2_jpegcompression *p = arg;
613
614	pr_cont("quality=%d, APPn=%d, APP_len=%d, "
615		"COM_len=%d, jpeg_markers=0x%x\n",
616		p->quality, p->APPn, p->APP_len,
617		p->COM_len, p->jpeg_markers);
618}
619
620static void v4l_print_enc_idx(const void *arg, bool write_only)
621{
622	const struct v4l2_enc_idx *p = arg;
623
624	pr_cont("entries=%d, entries_cap=%d\n",
625			p->entries, p->entries_cap);
626}
627
628static void v4l_print_encoder_cmd(const void *arg, bool write_only)
629{
630	const struct v4l2_encoder_cmd *p = arg;
631
632	pr_cont("cmd=%d, flags=0x%x\n",
633			p->cmd, p->flags);
634}
635
636static void v4l_print_decoder_cmd(const void *arg, bool write_only)
637{
638	const struct v4l2_decoder_cmd *p = arg;
639
640	pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
641
642	if (p->cmd == V4L2_DEC_CMD_START)
643		pr_info("speed=%d, format=%u\n",
644				p->start.speed, p->start.format);
645	else if (p->cmd == V4L2_DEC_CMD_STOP)
646		pr_info("pts=%llu\n", p->stop.pts);
647}
648
649static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
650{
651	const struct v4l2_dbg_chip_info *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("name=%.*s\n", (int)sizeof(p->name), p->name);
660}
661
662static void v4l_print_dbg_register(const void *arg, bool write_only)
663{
664	const struct v4l2_dbg_register *p = arg;
665
666	pr_cont("type=%u, ", p->match.type);
667	if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
668		pr_cont("name=%.*s, ",
669				(int)sizeof(p->match.name), p->match.name);
670	else
671		pr_cont("addr=%u, ", p->match.addr);
672	pr_cont("reg=0x%llx, val=0x%llx\n",
673			p->reg, p->val);
674}
675
676static void v4l_print_dv_timings(const void *arg, bool write_only)
677{
678	const struct v4l2_dv_timings *p = arg;
679
680	switch (p->type) {
681	case V4L2_DV_BT_656_1120:
682		pr_cont("type=bt-656/1120, interlaced=%u, "
683			"pixelclock=%llu, "
684			"width=%u, height=%u, polarities=0x%x, "
685			"hfrontporch=%u, hsync=%u, "
686			"hbackporch=%u, vfrontporch=%u, "
687			"vsync=%u, vbackporch=%u, "
688			"il_vfrontporch=%u, il_vsync=%u, "
689			"il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
690				p->bt.interlaced, p->bt.pixelclock,
691				p->bt.width, p->bt.height,
692				p->bt.polarities, p->bt.hfrontporch,
693				p->bt.hsync, p->bt.hbackporch,
694				p->bt.vfrontporch, p->bt.vsync,
695				p->bt.vbackporch, p->bt.il_vfrontporch,
696				p->bt.il_vsync, p->bt.il_vbackporch,
697				p->bt.standards, p->bt.flags);
698		break;
699	default:
700		pr_cont("type=%d\n", p->type);
701		break;
702	}
703}
704
705static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
706{
707	const struct v4l2_enum_dv_timings *p = arg;
708
709	pr_cont("index=%u, ", p->index);
710	v4l_print_dv_timings(&p->timings, write_only);
711}
712
713static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
714{
715	const struct v4l2_dv_timings_cap *p = arg;
716
717	switch (p->type) {
718	case V4L2_DV_BT_656_1120:
719		pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, "
720			"pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
721			p->bt.min_width, p->bt.max_width,
722			p->bt.min_height, p->bt.max_height,
723			p->bt.min_pixelclock, p->bt.max_pixelclock,
724			p->bt.standards, p->bt.capabilities);
725		break;
726	default:
727		pr_cont("type=%u\n", p->type);
728		break;
729	}
730}
731
732static void v4l_print_frmsizeenum(const void *arg, bool write_only)
733{
734	const struct v4l2_frmsizeenum *p = arg;
735
736	pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
737			p->index,
738			(p->pixel_format & 0xff),
739			(p->pixel_format >>  8) & 0xff,
740			(p->pixel_format >> 16) & 0xff,
741			(p->pixel_format >> 24) & 0xff,
742			p->type);
743	switch (p->type) {
744	case V4L2_FRMSIZE_TYPE_DISCRETE:
745		pr_cont(", wxh=%ux%u\n",
746			p->discrete.width, p->discrete.height);
747		break;
748	case V4L2_FRMSIZE_TYPE_STEPWISE:
749		pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
750				p->stepwise.min_width,  p->stepwise.min_height,
751				p->stepwise.step_width, p->stepwise.step_height,
752				p->stepwise.max_width,  p->stepwise.max_height);
753		break;
754	case V4L2_FRMSIZE_TYPE_CONTINUOUS:
755		/* fall through */
756	default:
757		pr_cont("\n");
758		break;
759	}
760}
761
762static void v4l_print_frmivalenum(const void *arg, bool write_only)
763{
764	const struct v4l2_frmivalenum *p = arg;
765
766	pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
767			p->index,
768			(p->pixel_format & 0xff),
769			(p->pixel_format >>  8) & 0xff,
770			(p->pixel_format >> 16) & 0xff,
771			(p->pixel_format >> 24) & 0xff,
772			p->width, p->height, p->type);
773	switch (p->type) {
774	case V4L2_FRMIVAL_TYPE_DISCRETE:
775		pr_cont(", fps=%d/%d\n",
776				p->discrete.numerator,
777				p->discrete.denominator);
778		break;
779	case V4L2_FRMIVAL_TYPE_STEPWISE:
780		pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
781				p->stepwise.min.numerator,
782				p->stepwise.min.denominator,
783				p->stepwise.max.numerator,
784				p->stepwise.max.denominator,
785				p->stepwise.step.numerator,
786				p->stepwise.step.denominator);
787		break;
788	case V4L2_FRMIVAL_TYPE_CONTINUOUS:
789		/* fall through */
790	default:
791		pr_cont("\n");
792		break;
793	}
794}
795
796static void v4l_print_event(const void *arg, bool write_only)
797{
798	const struct v4l2_event *p = arg;
799	const struct v4l2_event_ctrl *c;
800
801	pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, "
802		"timestamp=%lu.%9.9lu\n",
803			p->type, p->pending, p->sequence, p->id,
804			p->timestamp.tv_sec, p->timestamp.tv_nsec);
805	switch (p->type) {
806	case V4L2_EVENT_VSYNC:
807		printk(KERN_DEBUG "field=%s\n",
808			prt_names(p->u.vsync.field, v4l2_field_names));
809		break;
810	case V4L2_EVENT_CTRL:
811		c = &p->u.ctrl;
812		printk(KERN_DEBUG "changes=0x%x, type=%u, ",
813			c->changes, c->type);
814		if (c->type == V4L2_CTRL_TYPE_INTEGER64)
815			pr_cont("value64=%lld, ", c->value64);
816		else
817			pr_cont("value=%d, ", c->value);
818		pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, "
819			"default_value=%d\n",
820			c->flags, c->minimum, c->maximum,
821			c->step, c->default_value);
822		break;
823	case V4L2_EVENT_FRAME_SYNC:
824		pr_cont("frame_sequence=%u\n",
825			p->u.frame_sync.frame_sequence);
826		break;
827	}
828}
829
830static void v4l_print_event_subscription(const void *arg, bool write_only)
831{
832	const struct v4l2_event_subscription *p = arg;
833
834	pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
835			p->type, p->id, p->flags);
836}
837
838static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
839{
840	const struct v4l2_sliced_vbi_cap *p = arg;
841	int i;
842
843	pr_cont("type=%s, service_set=0x%08x\n",
844			prt_names(p->type, v4l2_type_names), p->service_set);
845	for (i = 0; i < 24; i++)
846		printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
847				p->service_lines[0][i],
848				p->service_lines[1][i]);
849}
850
851static void v4l_print_freq_band(const void *arg, bool write_only)
852{
853	const struct v4l2_frequency_band *p = arg;
854
855	pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, "
856		"rangelow=%u, rangehigh=%u, modulation=0x%x\n",
857			p->tuner, p->type, p->index,
858			p->capability, p->rangelow,
859			p->rangehigh, p->modulation);
860}
861
862static void v4l_print_edid(const void *arg, bool write_only)
863{
864	const struct v4l2_edid *p = arg;
865
866	pr_cont("pad=%u, start_block=%u, blocks=%u\n",
867		p->pad, p->start_block, p->blocks);
868}
869
870static void v4l_print_u32(const void *arg, bool write_only)
871{
872	pr_cont("value=%u\n", *(const u32 *)arg);
873}
874
875static void v4l_print_newline(const void *arg, bool write_only)
876{
877	pr_cont("\n");
878}
879
880static void v4l_print_default(const void *arg, bool write_only)
881{
882	pr_cont("driver-specific ioctl\n");
883}
884
885static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
886{
887	__u32 i;
888
889	/* zero the reserved fields */
890	c->reserved[0] = c->reserved[1] = 0;
891	for (i = 0; i < c->count; i++)
892		c->controls[i].reserved2[0] = 0;
893
894	/* V4L2_CID_PRIVATE_BASE cannot be used as control class
895	   when using extended controls.
896	   Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
897	   is it allowed for backwards compatibility.
898	 */
899	if (!allow_priv && c->ctrl_class == V4L2_CID_PRIVATE_BASE)
900		return 0;
901	/* Check that all controls are from the same control class. */
902	for (i = 0; i < c->count; i++) {
903		if (V4L2_CTRL_ID2CLASS(c->controls[i].id) != c->ctrl_class) {
904			c->error_idx = i;
905			return 0;
906		}
907	}
908	return 1;
909}
910
911static int check_fmt(struct file *file, enum v4l2_buf_type type)
912{
913	struct video_device *vfd = video_devdata(file);
914	const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
915	bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
916	bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
917	bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
918	bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
919	bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
920
921	if (ops == NULL)
922		return -EINVAL;
923
924	switch (type) {
925	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
926		if (is_vid && is_rx &&
927		    (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
928			return 0;
929		break;
930	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
931		if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
932			return 0;
933		break;
934	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
935		if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
936			return 0;
937		break;
938	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
939		if (is_vid && is_tx &&
940		    (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
941			return 0;
942		break;
943	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
944		if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
945			return 0;
946		break;
947	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
948		if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
949			return 0;
950		break;
951	case V4L2_BUF_TYPE_VBI_CAPTURE:
952		if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
953			return 0;
954		break;
955	case V4L2_BUF_TYPE_VBI_OUTPUT:
956		if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
957			return 0;
958		break;
959	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
960		if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
961			return 0;
962		break;
963	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
964		if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
965			return 0;
966		break;
967	case V4L2_BUF_TYPE_SDR_CAPTURE:
968		if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
969			return 0;
970		break;
971	default:
972		break;
973	}
974	return -EINVAL;
975}
976
977static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
978				struct file *file, void *fh, void *arg)
979{
980	struct v4l2_capability *cap = (struct v4l2_capability *)arg;
981
982	cap->version = LINUX_VERSION_CODE;
983	return ops->vidioc_querycap(file, fh, cap);
984}
985
986static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
987				struct file *file, void *fh, void *arg)
988{
989	return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
990}
991
992static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
993				struct file *file, void *fh, void *arg)
994{
995	return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
996}
997
998static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
999				struct file *file, void *fh, void *arg)
1000{
1001	struct video_device *vfd;
1002	u32 *p = arg;
1003
1004	if (ops->vidioc_g_priority)
1005		return ops->vidioc_g_priority(file, fh, arg);
1006	vfd = video_devdata(file);
1007	*p = v4l2_prio_max(&vfd->v4l2_dev->prio);
1008	return 0;
1009}
1010
1011static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
1012				struct file *file, void *fh, void *arg)
1013{
1014	struct video_device *vfd;
1015	struct v4l2_fh *vfh;
1016	u32 *p = arg;
1017
1018	if (ops->vidioc_s_priority)
1019		return ops->vidioc_s_priority(file, fh, *p);
1020	vfd = video_devdata(file);
1021	vfh = file->private_data;
1022	return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
1023}
1024
1025static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1026				struct file *file, void *fh, void *arg)
1027{
1028	struct video_device *vfd = video_devdata(file);
1029	struct v4l2_input *p = arg;
1030
1031	/*
1032	 * We set the flags for CAP_DV_TIMINGS &
1033	 * CAP_STD here based on ioctl handler provided by the
1034	 * driver. If the driver doesn't support these
1035	 * for a specific input, it must override these flags.
1036	 */
1037	if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1038		p->capabilities |= V4L2_IN_CAP_STD;
1039
1040	return ops->vidioc_enum_input(file, fh, p);
1041}
1042
1043static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1044				struct file *file, void *fh, void *arg)
1045{
1046	struct video_device *vfd = video_devdata(file);
1047	struct v4l2_output *p = arg;
1048
1049	/*
1050	 * We set the flags for CAP_DV_TIMINGS &
1051	 * CAP_STD here based on ioctl handler provided by the
1052	 * driver. If the driver doesn't support these
1053	 * for a specific output, it must override these flags.
1054	 */
1055	if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1056		p->capabilities |= V4L2_OUT_CAP_STD;
1057
1058	return ops->vidioc_enum_output(file, fh, p);
1059}
1060
1061static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1062				struct file *file, void *fh, void *arg)
1063{
1064	struct v4l2_fmtdesc *p = arg;
1065	struct video_device *vfd = video_devdata(file);
1066	bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1067	bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1068
1069	switch (p->type) {
1070	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1071		if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_cap))
1072			break;
1073		return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1074	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1075		if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_cap_mplane))
1076			break;
1077		return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1078	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1079		if (unlikely(!is_rx || !ops->vidioc_enum_fmt_vid_overlay))
1080			break;
1081		return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1082	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1083		if (unlikely(!is_tx || !ops->vidioc_enum_fmt_vid_out))
1084			break;
1085		return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1086	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1087		if (unlikely(!is_tx || !ops->vidioc_enum_fmt_vid_out_mplane))
1088			break;
1089		return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
1090	case V4L2_BUF_TYPE_SDR_CAPTURE:
1091		if (unlikely(!is_rx || !ops->vidioc_enum_fmt_sdr_cap))
1092			break;
1093		return ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
1094	}
1095	return -EINVAL;
1096}
1097
1098static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1099				struct file *file, void *fh, void *arg)
1100{
1101	struct v4l2_format *p = arg;
1102	struct video_device *vfd = video_devdata(file);
1103	bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1104	bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
1105	bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1106	bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1107
1108	switch (p->type) {
1109	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1110		if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap))
1111			break;
1112		return ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1113	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1114		if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap_mplane))
1115			break;
1116		return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1117	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1118		if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_overlay))
1119			break;
1120		return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1121	case V4L2_BUF_TYPE_VBI_CAPTURE:
1122		if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_vbi_cap))
1123			break;
1124		return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1125	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1126		if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_cap))
1127			break;
1128		return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1129	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1130		if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out))
1131			break;
1132		return ops->vidioc_g_fmt_vid_out(file, fh, arg);
1133	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1134		if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_mplane))
1135			break;
1136		return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1137	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1138		if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_overlay))
1139			break;
1140		return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1141	case V4L2_BUF_TYPE_VBI_OUTPUT:
1142		if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_vbi_out))
1143			break;
1144		return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1145	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1146		if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_out))
1147			break;
1148		return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1149	case V4L2_BUF_TYPE_SDR_CAPTURE:
1150		if (unlikely(!is_rx || !is_sdr || !ops->vidioc_g_fmt_sdr_cap))
1151			break;
1152		return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
1153	}
1154	return -EINVAL;
1155}
1156
1157static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1158				struct file *file, void *fh, void *arg)
1159{
1160	struct v4l2_format *p = arg;
1161	struct video_device *vfd = video_devdata(file);
1162	bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1163	bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
1164	bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1165	bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1166
1167	switch (p->type) {
1168	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1169		if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap))
1170			break;
1171		CLEAR_AFTER_FIELD(p, fmt.pix);
1172		return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1173	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1174		if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap_mplane))
1175			break;
1176		CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1177		return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1178	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1179		if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_overlay))
1180			break;
1181		CLEAR_AFTER_FIELD(p, fmt.win);
1182		return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1183	case V4L2_BUF_TYPE_VBI_CAPTURE:
1184		if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_vbi_cap))
1185			break;
1186		CLEAR_AFTER_FIELD(p, fmt.vbi);
1187		return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1188	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1189		if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_cap))
1190			break;
1191		CLEAR_AFTER_FIELD(p, fmt.sliced);
1192		return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1193	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1194		if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out))
1195			break;
1196		CLEAR_AFTER_FIELD(p, fmt.pix);
1197		return ops->vidioc_s_fmt_vid_out(file, fh, arg);
1198	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1199		if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_mplane))
1200			break;
1201		CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1202		return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1203	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1204		if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_overlay))
1205			break;
1206		CLEAR_AFTER_FIELD(p, fmt.win);
1207		return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1208	case V4L2_BUF_TYPE_VBI_OUTPUT:
1209		if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_vbi_out))
1210			break;
1211		CLEAR_AFTER_FIELD(p, fmt.vbi);
1212		return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1213	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1214		if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_out))
1215			break;
1216		CLEAR_AFTER_FIELD(p, fmt.sliced);
1217		return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
1218	case V4L2_BUF_TYPE_SDR_CAPTURE:
1219		if (unlikely(!is_rx || !is_sdr || !ops->vidioc_s_fmt_sdr_cap))
1220			break;
1221		CLEAR_AFTER_FIELD(p, fmt.sdr);
1222		return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
1223	}
1224	return -EINVAL;
1225}
1226
1227static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1228				struct file *file, void *fh, void *arg)
1229{
1230	struct v4l2_format *p = arg;
1231	struct video_device *vfd = video_devdata(file);
1232	bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1233	bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
1234	bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1235	bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1236
1237	switch (p->type) {
1238	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1239		if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap))
1240			break;
1241		CLEAR_AFTER_FIELD(p, fmt.pix);
1242		return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1243	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1244		if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap_mplane))
1245			break;
1246		CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1247		return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1248	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1249		if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_overlay))
1250			break;
1251		CLEAR_AFTER_FIELD(p, fmt.win);
1252		return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1253	case V4L2_BUF_TYPE_VBI_CAPTURE:
1254		if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_vbi_cap))
1255			break;
1256		CLEAR_AFTER_FIELD(p, fmt.vbi);
1257		return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1258	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1259		if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_cap))
1260			break;
1261		CLEAR_AFTER_FIELD(p, fmt.sliced);
1262		return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1263	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1264		if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out))
1265			break;
1266		CLEAR_AFTER_FIELD(p, fmt.pix);
1267		return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1268	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1269		if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_mplane))
1270			break;
1271		CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1272		return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1273	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1274		if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_overlay))
1275			break;
1276		CLEAR_AFTER_FIELD(p, fmt.win);
1277		return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1278	case V4L2_BUF_TYPE_VBI_OUTPUT:
1279		if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_vbi_out))
1280			break;
1281		CLEAR_AFTER_FIELD(p, fmt.vbi);
1282		return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1283	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1284		if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_out))
1285			break;
1286		CLEAR_AFTER_FIELD(p, fmt.sliced);
1287		return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1288	case V4L2_BUF_TYPE_SDR_CAPTURE:
1289		if (unlikely(!is_rx || !is_sdr || !ops->vidioc_try_fmt_sdr_cap))
1290			break;
1291		CLEAR_AFTER_FIELD(p, fmt.sdr);
1292		return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
1293	}
1294	return -EINVAL;
1295}
1296
1297static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1298				struct file *file, void *fh, void *arg)
1299{
1300	return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1301}
1302
1303static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1304				struct file *file, void *fh, void *arg)
1305{
1306	return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1307}
1308
1309static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1310				struct file *file, void *fh, void *arg)
1311{
1312	struct video_device *vfd = video_devdata(file);
1313	struct v4l2_tuner *p = arg;
1314	int err;
1315
1316	p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1317			V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1318	err = ops->vidioc_g_tuner(file, fh, p);
1319	if (!err)
1320		p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1321	return err;
1322}
1323
1324static int v4l_s_tuner(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_tuner *p = arg;
1329
1330	p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1331			V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1332	return ops->vidioc_s_tuner(file, fh, p);
1333}
1334
1335static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1336				struct file *file, void *fh, void *arg)
1337{
1338	struct v4l2_modulator *p = arg;
1339	int err;
1340
1341	err = ops->vidioc_g_modulator(file, fh, p);
1342	if (!err)
1343		p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1344	return err;
1345}
1346
1347static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1348				struct file *file, void *fh, void *arg)
1349{
1350	struct video_device *vfd = video_devdata(file);
1351	struct v4l2_frequency *p = arg;
1352
1353	if (vfd->vfl_type == VFL_TYPE_SDR)
1354		p->type = V4L2_TUNER_ADC;
1355	else
1356		p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1357				V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1358	return ops->vidioc_g_frequency(file, fh, p);
1359}
1360
1361static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1362				struct file *file, void *fh, void *arg)
1363{
1364	struct video_device *vfd = video_devdata(file);
1365	const struct v4l2_frequency *p = arg;
1366	enum v4l2_tuner_type type;
1367
1368	if (vfd->vfl_type == VFL_TYPE_SDR) {
1369		if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
1370			return -EINVAL;
1371	} else {
1372		type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1373				V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1374		if (type != p->type)
1375			return -EINVAL;
1376	}
1377	return ops->vidioc_s_frequency(file, fh, p);
1378}
1379
1380static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1381				struct file *file, void *fh, void *arg)
1382{
1383	struct video_device *vfd = video_devdata(file);
1384	struct v4l2_standard *p = arg;
1385	v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1386	unsigned int index = p->index, i, j = 0;
1387	const char *descr = "";
1388
1389	/* Return -ENODATA if the tvnorms for the current input
1390	   or output is 0, meaning that it doesn't support this API. */
1391	if (id == 0)
1392		return -ENODATA;
1393
1394	/* Return norm array in a canonical way */
1395	for (i = 0; i <= index && id; i++) {
1396		/* last std value in the standards array is 0, so this
1397		   while always ends there since (id & 0) == 0. */
1398		while ((id & standards[j].std) != standards[j].std)
1399			j++;
1400		curr_id = standards[j].std;
1401		descr = standards[j].descr;
1402		j++;
1403		if (curr_id == 0)
1404			break;
1405		if (curr_id != V4L2_STD_PAL &&
1406				curr_id != V4L2_STD_SECAM &&
1407				curr_id != V4L2_STD_NTSC)
1408			id &= ~curr_id;
1409	}
1410	if (i <= index)
1411		return -EINVAL;
1412
1413	v4l2_video_std_construct(p, curr_id, descr);
1414	return 0;
1415}
1416
1417static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1418				struct file *file, void *fh, void *arg)
1419{
1420	struct video_device *vfd = video_devdata(file);
1421	v4l2_std_id id = *(v4l2_std_id *)arg, norm;
1422
1423	norm = id & vfd->tvnorms;
1424	if (vfd->tvnorms && !norm)	/* Check if std is supported */
1425		return -EINVAL;
1426
1427	/* Calls the specific handler */
1428	return ops->vidioc_s_std(file, fh, norm);
1429}
1430
1431static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1432				struct file *file, void *fh, void *arg)
1433{
1434	struct video_device *vfd = video_devdata(file);
1435	v4l2_std_id *p = arg;
1436
1437	/*
1438	 * If no signal is detected, then the driver should return
1439	 * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
1440	 * any standards that do not apply removed.
1441	 *
1442	 * This means that tuners, audio and video decoders can join
1443	 * their efforts to improve the standards detection.
1444	 */
1445	*p = vfd->tvnorms;
1446	return ops->vidioc_querystd(file, fh, arg);
1447}
1448
1449static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1450				struct file *file, void *fh, void *arg)
1451{
1452	struct video_device *vfd = video_devdata(file);
1453	struct v4l2_hw_freq_seek *p = arg;
1454	enum v4l2_tuner_type type;
1455
1456	/* s_hw_freq_seek is not supported for SDR for now */
1457	if (vfd->vfl_type == VFL_TYPE_SDR)
1458		return -EINVAL;
1459
1460	type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1461		V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1462	if (p->type != type)
1463		return -EINVAL;
1464	return ops->vidioc_s_hw_freq_seek(file, fh, p);
1465}
1466
1467static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
1468				struct file *file, void *fh, void *arg)
1469{
1470	return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
1471}
1472
1473static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1474				struct file *file, void *fh, void *arg)
1475{
1476	struct v4l2_requestbuffers *p = arg;
1477	int ret = check_fmt(file, p->type);
1478
1479	if (ret)
1480		return ret;
1481
1482	CLEAR_AFTER_FIELD(p, memory);
1483
1484	return ops->vidioc_reqbufs(file, fh, p);
1485}
1486
1487static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1488				struct file *file, void *fh, void *arg)
1489{
1490	struct v4l2_buffer *p = arg;
1491	int ret = check_fmt(file, p->type);
1492
1493	return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1494}
1495
1496static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1497				struct file *file, void *fh, void *arg)
1498{
1499	struct v4l2_buffer *p = arg;
1500	int ret = check_fmt(file, p->type);
1501
1502	return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1503}
1504
1505static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1506				struct file *file, void *fh, void *arg)
1507{
1508	struct v4l2_buffer *p = arg;
1509	int ret = check_fmt(file, p->type);
1510
1511	return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1512}
1513
1514static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1515				struct file *file, void *fh, void *arg)
1516{
1517	struct v4l2_create_buffers *create = arg;
1518	int ret = check_fmt(file, create->format.type);
1519
1520	return ret ? ret : ops->vidioc_create_bufs(file, fh, create);
1521}
1522
1523static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1524				struct file *file, void *fh, void *arg)
1525{
1526	struct v4l2_buffer *b = arg;
1527	int ret = check_fmt(file, b->type);
1528
1529	return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1530}
1531
1532static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1533				struct file *file, void *fh, void *arg)
1534{
1535	struct v4l2_streamparm *p = arg;
1536	v4l2_std_id std;
1537	int ret = check_fmt(file, p->type);
1538
1539	if (ret)
1540		return ret;
1541	if (ops->vidioc_g_parm)
1542		return ops->vidioc_g_parm(file, fh, p);
1543	if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1544	    p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1545		return -EINVAL;
1546	p->parm.capture.readbuffers = 2;
1547	ret = ops->vidioc_g_std(file, fh, &std);
1548	if (ret == 0)
1549		v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
1550	return ret;
1551}
1552
1553static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1554				struct file *file, void *fh, void *arg)
1555{
1556	struct v4l2_streamparm *p = arg;
1557	int ret = check_fmt(file, p->type);
1558
1559	return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1560}
1561
1562static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1563				struct file *file, void *fh, void *arg)
1564{
1565	struct video_device *vfd = video_devdata(file);
1566	struct v4l2_queryctrl *p = arg;
1567	struct v4l2_fh *vfh =
1568		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1569
1570	if (vfh && vfh->ctrl_handler)
1571		return v4l2_queryctrl(vfh->ctrl_handler, p);
1572	if (vfd->ctrl_handler)
1573		return v4l2_queryctrl(vfd->ctrl_handler, p);
1574	if (ops->vidioc_queryctrl)
1575		return ops->vidioc_queryctrl(file, fh, p);
1576	return -ENOTTY;
1577}
1578
1579static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
1580				struct file *file, void *fh, void *arg)
1581{
1582	struct video_device *vfd = video_devdata(file);
1583	struct v4l2_query_ext_ctrl *p = arg;
1584	struct v4l2_fh *vfh =
1585		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1586
1587	if (vfh && vfh->ctrl_handler)
1588		return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
1589	if (vfd->ctrl_handler)
1590		return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
1591	if (ops->vidioc_query_ext_ctrl)
1592		return ops->vidioc_query_ext_ctrl(file, fh, p);
1593	return -ENOTTY;
1594}
1595
1596static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
1597				struct file *file, void *fh, void *arg)
1598{
1599	struct video_device *vfd = video_devdata(file);
1600	struct v4l2_querymenu *p = arg;
1601	struct v4l2_fh *vfh =
1602		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1603
1604	if (vfh && vfh->ctrl_handler)
1605		return v4l2_querymenu(vfh->ctrl_handler, p);
1606	if (vfd->ctrl_handler)
1607		return v4l2_querymenu(vfd->ctrl_handler, p);
1608	if (ops->vidioc_querymenu)
1609		return ops->vidioc_querymenu(file, fh, p);
1610	return -ENOTTY;
1611}
1612
1613static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1614				struct file *file, void *fh, void *arg)
1615{
1616	struct video_device *vfd = video_devdata(file);
1617	struct v4l2_control *p = arg;
1618	struct v4l2_fh *vfh =
1619		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1620	struct v4l2_ext_controls ctrls;
1621	struct v4l2_ext_control ctrl;
1622
1623	if (vfh && vfh->ctrl_handler)
1624		return v4l2_g_ctrl(vfh->ctrl_handler, p);
1625	if (vfd->ctrl_handler)
1626		return v4l2_g_ctrl(vfd->ctrl_handler, p);
1627	if (ops->vidioc_g_ctrl)
1628		return ops->vidioc_g_ctrl(file, fh, p);
1629	if (ops->vidioc_g_ext_ctrls == NULL)
1630		return -ENOTTY;
1631
1632	ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1633	ctrls.count = 1;
1634	ctrls.controls = &ctrl;
1635	ctrl.id = p->id;
1636	ctrl.value = p->value;
1637	if (check_ext_ctrls(&ctrls, 1)) {
1638		int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1639
1640		if (ret == 0)
1641			p->value = ctrl.value;
1642		return ret;
1643	}
1644	return -EINVAL;
1645}
1646
1647static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1648				struct file *file, void *fh, void *arg)
1649{
1650	struct video_device *vfd = video_devdata(file);
1651	struct v4l2_control *p = arg;
1652	struct v4l2_fh *vfh =
1653		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1654	struct v4l2_ext_controls ctrls;
1655	struct v4l2_ext_control ctrl;
1656
1657	if (vfh && vfh->ctrl_handler)
1658		return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1659	if (vfd->ctrl_handler)
1660		return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1661	if (ops->vidioc_s_ctrl)
1662		return ops->vidioc_s_ctrl(file, fh, p);
1663	if (ops->vidioc_s_ext_ctrls == NULL)
1664		return -ENOTTY;
1665
1666	ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1667	ctrls.count = 1;
1668	ctrls.controls = &ctrl;
1669	ctrl.id = p->id;
1670	ctrl.value = p->value;
1671	if (check_ext_ctrls(&ctrls, 1))
1672		return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1673	return -EINVAL;
1674}
1675
1676static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1677				struct file *file, void *fh, void *arg)
1678{
1679	struct video_device *vfd = video_devdata(file);
1680	struct v4l2_ext_controls *p = arg;
1681	struct v4l2_fh *vfh =
1682		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1683
1684	p->error_idx = p->count;
1685	if (vfh && vfh->ctrl_handler)
1686		return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1687	if (vfd->ctrl_handler)
1688		return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1689	if (ops->vidioc_g_ext_ctrls == NULL)
1690		return -ENOTTY;
1691	return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1692					-EINVAL;
1693}
1694
1695static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1696				struct file *file, void *fh, void *arg)
1697{
1698	struct video_device *vfd = video_devdata(file);
1699	struct v4l2_ext_controls *p = arg;
1700	struct v4l2_fh *vfh =
1701		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1702
1703	p->error_idx = p->count;
1704	if (vfh && vfh->ctrl_handler)
1705		return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1706	if (vfd->ctrl_handler)
1707		return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1708	if (ops->vidioc_s_ext_ctrls == NULL)
1709		return -ENOTTY;
1710	return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1711					-EINVAL;
1712}
1713
1714static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1715				struct file *file, void *fh, void *arg)
1716{
1717	struct video_device *vfd = video_devdata(file);
1718	struct v4l2_ext_controls *p = arg;
1719	struct v4l2_fh *vfh =
1720		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1721
1722	p->error_idx = p->count;
1723	if (vfh && vfh->ctrl_handler)
1724		return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1725	if (vfd->ctrl_handler)
1726		return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1727	if (ops->vidioc_try_ext_ctrls == NULL)
1728		return -ENOTTY;
1729	return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1730					-EINVAL;
1731}
1732
1733static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1734				struct file *file, void *fh, void *arg)
1735{
1736	struct v4l2_crop *p = arg;
1737	struct v4l2_selection s = {
1738		.type = p->type,
1739	};
1740	int ret;
1741
1742	if (ops->vidioc_g_crop)
1743		return ops->vidioc_g_crop(file, fh, p);
1744	/* simulate capture crop using selection api */
1745
1746	/* crop means compose for output devices */
1747	if (V4L2_TYPE_IS_OUTPUT(p->type))
1748		s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1749	else
1750		s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1751
1752	ret = ops->vidioc_g_selection(file, fh, &s);
1753
1754	/* copying results to old structure on success */
1755	if (!ret)
1756		p->c = s.r;
1757	return ret;
1758}
1759
1760static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1761				struct file *file, void *fh, void *arg)
1762{
1763	struct v4l2_crop *p = arg;
1764	struct v4l2_selection s = {
1765		.type = p->type,
1766		.r = p->c,
1767	};
1768
1769	if (ops->vidioc_s_crop)
1770		return ops->vidioc_s_crop(file, fh, p);
1771	/* simulate capture crop using selection api */
1772
1773	/* crop means compose for output devices */
1774	if (V4L2_TYPE_IS_OUTPUT(p->type))
1775		s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1776	else
1777		s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1778
1779	return ops->vidioc_s_selection(file, fh, &s);
1780}
1781
1782static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1783				struct file *file, void *fh, void *arg)
1784{
1785	struct v4l2_cropcap *p = arg;
1786	struct v4l2_selection s = { .type = p->type };
1787	int ret;
1788
1789	if (ops->vidioc_cropcap)
1790		return ops->vidioc_cropcap(file, fh, p);
1791
1792	/* obtaining bounds */
1793	if (V4L2_TYPE_IS_OUTPUT(p->type))
1794		s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1795	else
1796		s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1797
1798	ret = ops->vidioc_g_selection(file, fh, &s);
1799	if (ret)
1800		return ret;
1801	p->bounds = s.r;
1802
1803	/* obtaining defrect */
1804	if (V4L2_TYPE_IS_OUTPUT(p->type))
1805		s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1806	else
1807		s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1808
1809	ret = ops->vidioc_g_selection(file, fh, &s);
1810	if (ret)
1811		return ret;
1812	p->defrect = s.r;
1813
1814	/* setting trivial pixelaspect */
1815	p->pixelaspect.numerator = 1;
1816	p->pixelaspect.denominator = 1;
1817	return 0;
1818}
1819
1820static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1821				struct file *file, void *fh, void *arg)
1822{
1823	struct video_device *vfd = video_devdata(file);
1824	int ret;
1825
1826	if (vfd->v4l2_dev)
1827		pr_info("%s: =================  START STATUS  =================\n",
1828			vfd->v4l2_dev->name);
1829	ret = ops->vidioc_log_status(file, fh);
1830	if (vfd->v4l2_dev)
1831		pr_info("%s: ==================  END STATUS  ==================\n",
1832			vfd->v4l2_dev->name);
1833	return ret;
1834}
1835
1836static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1837				struct file *file, void *fh, void *arg)
1838{
1839#ifdef CONFIG_VIDEO_ADV_DEBUG
1840	struct v4l2_dbg_register *p = arg;
1841	struct video_device *vfd = video_devdata(file);
1842	struct v4l2_subdev *sd;
1843	int idx = 0;
1844
1845	if (!capable(CAP_SYS_ADMIN))
1846		return -EPERM;
1847	if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
1848		if (vfd->v4l2_dev == NULL)
1849			return -EINVAL;
1850		v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1851			if (p->match.addr == idx++)
1852				return v4l2_subdev_call(sd, core, g_register, p);
1853		return -EINVAL;
1854	}
1855	if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1856	    (ops->vidioc_g_chip_info || p->match.addr == 0))
1857		return ops->vidioc_g_register(file, fh, p);
1858	return -EINVAL;
1859#else
1860	return -ENOTTY;
1861#endif
1862}
1863
1864static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1865				struct file *file, void *fh, void *arg)
1866{
1867#ifdef CONFIG_VIDEO_ADV_DEBUG
1868	const struct v4l2_dbg_register *p = arg;
1869	struct video_device *vfd = video_devdata(file);
1870	struct v4l2_subdev *sd;
1871	int idx = 0;
1872
1873	if (!capable(CAP_SYS_ADMIN))
1874		return -EPERM;
1875	if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
1876		if (vfd->v4l2_dev == NULL)
1877			return -EINVAL;
1878		v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1879			if (p->match.addr == idx++)
1880				return v4l2_subdev_call(sd, core, s_register, p);
1881		return -EINVAL;
1882	}
1883	if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1884	    (ops->vidioc_g_chip_info || p->match.addr == 0))
1885		return ops->vidioc_s_register(file, fh, p);
1886	return -EINVAL;
1887#else
1888	return -ENOTTY;
1889#endif
1890}
1891
1892static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
1893				struct file *file, void *fh, void *arg)
1894{
1895#ifdef CONFIG_VIDEO_ADV_DEBUG
1896	struct video_device *vfd = video_devdata(file);
1897	struct v4l2_dbg_chip_info *p = arg;
1898	struct v4l2_subdev *sd;
1899	int idx = 0;
1900
1901	switch (p->match.type) {
1902	case V4L2_CHIP_MATCH_BRIDGE:
1903		if (ops->vidioc_s_register)
1904			p->flags |= V4L2_CHIP_FL_WRITABLE;
1905		if (ops->vidioc_g_register)
1906			p->flags |= V4L2_CHIP_FL_READABLE;
1907		strlcpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
1908		if (ops->vidioc_g_chip_info)
1909			return ops->vidioc_g_chip_info(file, fh, arg);
1910		if (p->match.addr)
1911			return -EINVAL;
1912		return 0;
1913
1914	case V4L2_CHIP_MATCH_SUBDEV:
1915		if (vfd->v4l2_dev == NULL)
1916			break;
1917		v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
1918			if (p->match.addr != idx++)
1919				continue;
1920			if (sd->ops->core && sd->ops->core->s_register)
1921				p->flags |= V4L2_CHIP_FL_WRITABLE;
1922			if (sd->ops->core && sd->ops->core->g_register)
1923				p->flags |= V4L2_CHIP_FL_READABLE;
1924			strlcpy(p->name, sd->name, sizeof(p->name));
1925			return 0;
1926		}
1927		break;
1928	}
1929	return -EINVAL;
1930#else
1931	return -ENOTTY;
1932#endif
1933}
1934
1935static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
1936				struct file *file, void *fh, void *arg)
1937{
1938	return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
1939}
1940
1941static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
1942				struct file *file, void *fh, void *arg)
1943{
1944	return ops->vidioc_subscribe_event(fh, arg);
1945}
1946
1947static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
1948				struct file *file, void *fh, void *arg)
1949{
1950	return ops->vidioc_unsubscribe_event(fh, arg);
1951}
1952
1953static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
1954				struct file *file, void *fh, void *arg)
1955{
1956	struct v4l2_sliced_vbi_cap *p = arg;
1957	int ret = check_fmt(file, p->type);
1958
1959	if (ret)
1960		return ret;
1961
1962	/* Clear up to type, everything after type is zeroed already */
1963	memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
1964
1965	return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
1966}
1967
1968static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
1969				struct file *file, void *fh, void *arg)
1970{
1971	struct video_device *vfd = video_devdata(file);
1972	struct v4l2_frequency_band *p = arg;
1973	enum v4l2_tuner_type type;
1974	int err;
1975
1976	if (vfd->vfl_type == VFL_TYPE_SDR) {
1977		if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
1978			return -EINVAL;
1979		type = p->type;
1980	} else {
1981		type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1982				V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1983		if (type != p->type)
1984			return -EINVAL;
1985	}
1986	if (ops->vidioc_enum_freq_bands)
1987		return ops->vidioc_enum_freq_bands(file, fh, p);
1988	if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
1989		struct v4l2_tuner t = {
1990			.index = p->tuner,
1991			.type = type,
1992		};
1993
1994		if (p->index)
1995			return -EINVAL;
1996		err = ops->vidioc_g_tuner(file, fh, &t);
1997		if (err)
1998			return err;
1999		p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2000		p->rangelow = t.rangelow;
2001		p->rangehigh = t.rangehigh;
2002		p->modulation = (type == V4L2_TUNER_RADIO) ?
2003			V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2004		return 0;
2005	}
2006	if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
2007		struct v4l2_modulator m = {
2008			.index = p->tuner,
2009		};
2010
2011		if (type != V4L2_TUNER_RADIO)
2012			return -EINVAL;
2013		if (p->index)
2014			return -EINVAL;
2015		err = ops->vidioc_g_modulator(file, fh, &m);
2016		if (err)
2017			return err;
2018		p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2019		p->rangelow = m.rangelow;
2020		p->rangehigh = m.rangehigh;
2021		p->modulation = (type == V4L2_TUNER_RADIO) ?
2022			V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2023		return 0;
2024	}
2025	return -ENOTTY;
2026}
2027
2028struct v4l2_ioctl_info {
2029	unsigned int ioctl;
2030	u32 flags;
2031	const char * const name;
2032	union {
2033		u32 offset;
2034		int (*func)(const struct v4l2_ioctl_ops *ops,
2035				struct file *file, void *fh, void *p);
2036	} u;
2037	void (*debug)(const void *arg, bool write_only);
2038};
2039
2040/* This control needs a priority check */
2041#define INFO_FL_PRIO	(1 << 0)
2042/* This control can be valid if the filehandle passes a control handler. */
2043#define INFO_FL_CTRL	(1 << 1)
2044/* This is a standard ioctl, no need for special code */
2045#define INFO_FL_STD	(1 << 2)
2046/* This is ioctl has its own function */
2047#define INFO_FL_FUNC	(1 << 3)
2048/* Queuing ioctl */
2049#define INFO_FL_QUEUE	(1 << 4)
2050/* Zero struct from after the field to the end */
2051#define INFO_FL_CLEAR(v4l2_struct, field)			\
2052	((offsetof(struct v4l2_struct, field) +			\
2053	  sizeof(((struct v4l2_struct *)0)->field)) << 16)
2054#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
2055
2056#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags)			\
2057	[_IOC_NR(_ioctl)] = {						\
2058		.ioctl = _ioctl,					\
2059		.flags = _flags | INFO_FL_STD,				\
2060		.name = #_ioctl,					\
2061		.u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc),	\
2062		.debug = _debug,					\
2063	}
2064
2065#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags)			\
2066	[_IOC_NR(_ioctl)] = {						\
2067		.ioctl = _ioctl,					\
2068		.flags = _flags | INFO_FL_FUNC,				\
2069		.name = #_ioctl,					\
2070		.u.func = _func,					\
2071		.debug = _debug,					\
2072	}
2073
2074static struct v4l2_ioctl_info v4l2_ioctls[] = {
2075	IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
2076	IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
2077	IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
2078	IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
2079	IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2080	IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
2081	IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
2082	IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
2083	IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
2084	IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
2085	IOCTL_INFO_STD(VIDIOC_EXPBUF, vidioc_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
2086	IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2087	IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2088	IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2089	IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2090	IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
2091	IOCTL_INFO_STD(VIDIOC_G_STD, vidioc_g_std, v4l_print_std, 0),
2092	IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2093	IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
2094	IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
2095	IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2096	IOCTL_INFO_FNC(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
2097	IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2098	IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
2099	IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
2100	IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
2101	IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2102	IOCTL_INFO_FNC(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
2103	IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
2104	IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
2105	IOCTL_INFO_STD(VIDIOC_G_EDID, vidioc_g_edid, v4l_print_edid, INFO_FL_CLEAR(v4l2_edid, edid)),
2106	IOCTL_INFO_STD(VIDIOC_S_EDID, vidioc_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_edid, edid)),
2107	IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
2108	IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2109	IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2110	IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
2111	IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
2112	IOCTL_INFO_FNC(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
2113	IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2114	IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2115	IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
2116	IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2117	IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2118	IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2119	IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
2120	IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
2121	IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
2122	IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
2123	IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
2124	IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
2125	IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2126	IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
2127	IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2128	IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
2129	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)),
2130	IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
2131	IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2132	IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
2133	IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2134	IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2135	IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
2136	IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
2137	IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2138	IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2139	IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2140	IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
2141	IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2142	IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2143	IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
2144	IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
2145	IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
2146	IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2147	IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2148	IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
2149	IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2150	IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
2151	IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
2152	IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
2153	IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)),
2154	IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
2155	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)),
2156	IOCTL_INFO_FNC(VIDIOC_QUERY_EXT_CTRL, v4l_query_ext_ctrl, v4l_print_query_ext_ctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_query_ext_ctrl, id)),
2157};
2158#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2159
2160bool v4l2_is_known_ioctl(unsigned int cmd)
2161{
2162	if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2163		return false;
2164	return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2165}
2166
2167struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
2168{
2169	if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2170		return vdev->lock;
2171	if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
2172		return NULL;
2173	if (vdev->queue && vdev->queue->lock &&
2174			(v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2175		return vdev->queue->lock;
2176	return vdev->lock;
2177}
2178
2179/* Common ioctl debug function. This function can be used by
2180   external ioctl messages as well as internal V4L ioctl */
2181void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
2182{
2183	const char *dir, *type;
2184
2185	if (prefix)
2186		printk(KERN_DEBUG "%s: ", prefix);
2187
2188	switch (_IOC_TYPE(cmd)) {
2189	case 'd':
2190		type = "v4l2_int";
2191		break;
2192	case 'V':
2193		if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2194			type = "v4l2";
2195			break;
2196		}
2197		pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
2198		return;
2199	default:
2200		type = "unknown";
2201		break;
2202	}
2203
2204	switch (_IOC_DIR(cmd)) {
2205	case _IOC_NONE:              dir = "--"; break;
2206	case _IOC_READ:              dir = "r-"; break;
2207	case _IOC_WRITE:             dir = "-w"; break;
2208	case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2209	default:                     dir = "*ERR*"; break;
2210	}
2211	pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
2212		type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2213}
2214EXPORT_SYMBOL(v4l_printk_ioctl);
2215
2216static long __video_do_ioctl(struct file *file,
2217		unsigned int cmd, void *arg)
2218{
2219	struct video_device *vfd = video_devdata(file);
2220	const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
2221	bool write_only = false;
2222	struct v4l2_ioctl_info default_info;
2223	const struct v4l2_ioctl_info *info;
2224	void *fh = file->private_data;
2225	struct v4l2_fh *vfh = NULL;
2226	int debug = vfd->debug;
2227	long ret = -ENOTTY;
2228
2229	if (ops == NULL) {
2230		pr_warn("%s: has no ioctl_ops.\n",
2231				video_device_node_name(vfd));
2232		return ret;
2233	}
2234
2235	if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
2236		vfh = file->private_data;
2237
2238	if (v4l2_is_known_ioctl(cmd)) {
2239		info = &v4l2_ioctls[_IOC_NR(cmd)];
2240
2241	        if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2242		    !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
2243			goto done;
2244
2245		if (vfh && (info->flags & INFO_FL_PRIO)) {
2246			ret = v4l2_prio_check(vfd->prio, vfh->prio);
2247			if (ret)
2248				goto done;
2249		}
2250	} else {
2251		default_info.ioctl = cmd;
2252		default_info.flags = 0;
2253		default_info.debug = v4l_print_default;
2254		info = &default_info;
2255	}
2256
2257	write_only = _IOC_DIR(cmd) == _IOC_WRITE;
2258	if (info->flags & INFO_FL_STD) {
2259		typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2260		const void *p = vfd->ioctl_ops;
2261		const vidioc_op *vidioc = p + info->u.offset;
2262
2263		ret = (*vidioc)(file, fh, arg);
2264	} else if (info->flags & INFO_FL_FUNC) {
2265		ret = info->u.func(ops, file, fh, arg);
2266	} else if (!ops->vidioc_default) {
2267		ret = -ENOTTY;
2268	} else {
2269		ret = ops->vidioc_default(file, fh,
2270			vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2271			cmd, arg);
2272	}
2273
2274done:
2275	if (debug) {
2276		v4l_printk_ioctl(video_device_node_name(vfd), cmd);
2277		if (ret < 0)
2278			pr_cont(": error %ld", ret);
2279		if (debug == V4L2_DEBUG_IOCTL)
2280			pr_cont("\n");
2281		else if (_IOC_DIR(cmd) == _IOC_NONE)
2282			info->debug(arg, write_only);
2283		else {
2284			pr_cont(": ");
2285			info->debug(arg, write_only);
2286		}
2287	}
2288
2289	return ret;
2290}
2291
2292static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2293			    void __user **user_ptr, void ***kernel_ptr)
2294{
2295	int ret = 0;
2296
2297	switch (cmd) {
2298	case VIDIOC_PREPARE_BUF:
2299	case VIDIOC_QUERYBUF:
2300	case VIDIOC_QBUF:
2301	case VIDIOC_DQBUF: {
2302		struct v4l2_buffer *buf = parg;
2303
2304		if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2305			if (buf->length > VIDEO_MAX_PLANES) {
2306				ret = -EINVAL;
2307				break;
2308			}
2309			*user_ptr = (void __user *)buf->m.planes;
2310			*kernel_ptr = (void **)&buf->m.planes;
2311			*array_size = sizeof(struct v4l2_plane) * buf->length;
2312			ret = 1;
2313		}
2314		break;
2315	}
2316
2317	case VIDIOC_G_EDID:
2318	case VIDIOC_S_EDID: {
2319		struct v4l2_edid *edid = parg;
2320
2321		if (edid->blocks) {
2322			if (edid->blocks > 256) {
2323				ret = -EINVAL;
2324				break;
2325			}
2326			*user_ptr = (void __user *)edid->edid;
2327			*kernel_ptr = (void **)&edid->edid;
2328			*array_size = edid->blocks * 128;
2329			ret = 1;
2330		}
2331		break;
2332	}
2333
2334	case VIDIOC_S_EXT_CTRLS:
2335	case VIDIOC_G_EXT_CTRLS:
2336	case VIDIOC_TRY_EXT_CTRLS: {
2337		struct v4l2_ext_controls *ctrls = parg;
2338
2339		if (ctrls->count != 0) {
2340			if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2341				ret = -EINVAL;
2342				break;
2343			}
2344			*user_ptr = (void __user *)ctrls->controls;
2345			*kernel_ptr = (void **)&ctrls->controls;
2346			*array_size = sizeof(struct v4l2_ext_control)
2347				    * ctrls->count;
2348			ret = 1;
2349		}
2350		break;
2351	}
2352	}
2353
2354	return ret;
2355}
2356
2357long
2358video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2359	       v4l2_kioctl func)
2360{
2361	char	sbuf[128];
2362	void    *mbuf = NULL;
2363	void	*parg = (void *)arg;
2364	long	err  = -EINVAL;
2365	bool	has_array_args;
2366	size_t  array_size = 0;
2367	void __user *user_ptr = NULL;
2368	void	**kernel_ptr = NULL;
2369
2370	/*  Copy arguments into temp kernel buffer  */
2371	if (_IOC_DIR(cmd) != _IOC_NONE) {
2372		if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2373			parg = sbuf;
2374		} else {
2375			/* too big to allocate from stack */
2376			mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2377			if (NULL == mbuf)
2378				return -ENOMEM;
2379			parg = mbuf;
2380		}
2381
2382		err = -EFAULT;
2383		if (_IOC_DIR(cmd) & _IOC_WRITE) {
2384			unsigned int n = _IOC_SIZE(cmd);
2385
2386			/*
2387			 * In some cases, only a few fields are used as input,
2388			 * i.e. when the app sets "index" and then the driver
2389			 * fills in the rest of the structure for the thing
2390			 * with that index.  We only need to copy up the first
2391			 * non-input field.
2392			 */
2393			if (v4l2_is_known_ioctl(cmd)) {
2394				u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2395				if (flags & INFO_FL_CLEAR_MASK)
2396					n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2397			}
2398
2399			if (copy_from_user(parg, (void __user *)arg, n))
2400				goto out;
2401
2402			/* zero out anything we don't copy from userspace */
2403			if (n < _IOC_SIZE(cmd))
2404				memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
2405		} else {
2406			/* read-only ioctl */
2407			memset(parg, 0, _IOC_SIZE(cmd));
2408		}
2409	}
2410
2411	err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2412	if (err < 0)
2413		goto out;
2414	has_array_args = err;
2415
2416	if (has_array_args) {
2417		/*
2418		 * When adding new types of array args, make sure that the
2419		 * parent argument to ioctl (which contains the pointer to the
2420		 * array) fits into sbuf (so that mbuf will still remain
2421		 * unused up to here).
2422		 */
2423		mbuf = kmalloc(array_size, GFP_KERNEL);
2424		err = -ENOMEM;
2425		if (NULL == mbuf)
2426			goto out_array_args;
2427		err = -EFAULT;
2428		if (copy_from_user(mbuf, user_ptr, array_size))
2429			goto out_array_args;
2430		*kernel_ptr = mbuf;
2431	}
2432
2433	/* Handles IOCTL */
2434	err = func(file, cmd, parg);
2435	if (err == -ENOIOCTLCMD)
2436		err = -ENOTTY;
2437	if (err == 0) {
2438		if (cmd == VIDIOC_DQBUF)
2439			trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
2440		else if (cmd == VIDIOC_QBUF)
2441			trace_v4l2_qbuf(video_devdata(file)->minor, parg);
2442	}
2443
2444	if (has_array_args) {
2445		*kernel_ptr = (void __force *)user_ptr;
2446		if (copy_to_user(user_ptr, mbuf, array_size))
2447			err = -EFAULT;
2448		goto out_array_args;
2449	}
2450	/* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2451	   results that must be returned. */
2452	if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
2453		goto out;
2454
2455out_array_args:
2456	/*  Copy results into user buffer  */
2457	switch (_IOC_DIR(cmd)) {
2458	case _IOC_READ:
2459	case (_IOC_WRITE | _IOC_READ):
2460		if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2461			err = -EFAULT;
2462		break;
2463	}
2464
2465out:
2466	kfree(mbuf);
2467	return err;
2468}
2469EXPORT_SYMBOL(video_usercopy);
2470
2471long video_ioctl2(struct file *file,
2472	       unsigned int cmd, unsigned long arg)
2473{
2474	return video_usercopy(file, cmd, arg, __video_do_ioctl);
2475}
2476EXPORT_SYMBOL(video_ioctl2);
2477