v4l2-ioctl.c revision a7f404af8a435b940055a04be19a3304da93a76d
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, "
260			"flags %u\n",
261			pix->width, pix->height,
262			(pix->pixelformat & 0xff),
263			(pix->pixelformat >>  8) & 0xff,
264			(pix->pixelformat >> 16) & 0xff,
265			(pix->pixelformat >> 24) & 0xff,
266			prt_names(pix->field, v4l2_field_names),
267			pix->bytesperline, pix->sizeimage,
268			pix->colorspace, pix->flags);
269		break;
270	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
271	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
272		mp = &p->fmt.pix_mp;
273		pr_cont(", width=%u, height=%u, "
274			"format=%c%c%c%c, field=%s, "
275			"colorspace=%d, num_planes=%u\n",
276			mp->width, mp->height,
277			(mp->pixelformat & 0xff),
278			(mp->pixelformat >>  8) & 0xff,
279			(mp->pixelformat >> 16) & 0xff,
280			(mp->pixelformat >> 24) & 0xff,
281			prt_names(mp->field, v4l2_field_names),
282			mp->colorspace, mp->num_planes);
283		for (i = 0; i < mp->num_planes; i++)
284			printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
285					mp->plane_fmt[i].bytesperline,
286					mp->plane_fmt[i].sizeimage);
287		break;
288	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
289	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
290		win = &p->fmt.win;
291		/* Note: we can't print the clip list here since the clips
292		 * pointer is a userspace pointer, not a kernelspace
293		 * pointer. */
294		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",
295			win->w.width, win->w.height, win->w.left, win->w.top,
296			prt_names(win->field, v4l2_field_names),
297			win->chromakey, win->clipcount, win->clips,
298			win->bitmap, win->global_alpha);
299		break;
300	case V4L2_BUF_TYPE_VBI_CAPTURE:
301	case V4L2_BUF_TYPE_VBI_OUTPUT:
302		vbi = &p->fmt.vbi;
303		pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, "
304			"sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
305			vbi->sampling_rate, vbi->offset,
306			vbi->samples_per_line,
307			(vbi->sample_format & 0xff),
308			(vbi->sample_format >>  8) & 0xff,
309			(vbi->sample_format >> 16) & 0xff,
310			(vbi->sample_format >> 24) & 0xff,
311			vbi->start[0], vbi->start[1],
312			vbi->count[0], vbi->count[1]);
313		break;
314	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
315	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
316		sliced = &p->fmt.sliced;
317		pr_cont(", service_set=0x%08x, io_size=%d\n",
318				sliced->service_set, sliced->io_size);
319		for (i = 0; i < 24; i++)
320			printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
321				sliced->service_lines[0][i],
322				sliced->service_lines[1][i]);
323		break;
324	case V4L2_BUF_TYPE_SDR_CAPTURE:
325		sdr = &p->fmt.sdr;
326		pr_cont(", pixelformat=%c%c%c%c\n",
327			(sdr->pixelformat >>  0) & 0xff,
328			(sdr->pixelformat >>  8) & 0xff,
329			(sdr->pixelformat >> 16) & 0xff,
330			(sdr->pixelformat >> 24) & 0xff);
331		break;
332	}
333}
334
335static void v4l_print_framebuffer(const void *arg, bool write_only)
336{
337	const struct v4l2_framebuffer *p = arg;
338
339	pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, "
340		"height=%u, pixelformat=%c%c%c%c, "
341		"bytesperline=%u, sizeimage=%u, colorspace=%d\n",
342			p->capability, p->flags, p->base,
343			p->fmt.width, p->fmt.height,
344			(p->fmt.pixelformat & 0xff),
345			(p->fmt.pixelformat >>  8) & 0xff,
346			(p->fmt.pixelformat >> 16) & 0xff,
347			(p->fmt.pixelformat >> 24) & 0xff,
348			p->fmt.bytesperline, p->fmt.sizeimage,
349			p->fmt.colorspace);
350}
351
352static void v4l_print_buftype(const void *arg, bool write_only)
353{
354	pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
355}
356
357static void v4l_print_modulator(const void *arg, bool write_only)
358{
359	const struct v4l2_modulator *p = arg;
360
361	if (write_only)
362		pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
363	else
364		pr_cont("index=%u, name=%.*s, capability=0x%x, "
365			"rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
366			p->index, (int)sizeof(p->name), p->name, p->capability,
367			p->rangelow, p->rangehigh, p->txsubchans);
368}
369
370static void v4l_print_tuner(const void *arg, bool write_only)
371{
372	const struct v4l2_tuner *p = arg;
373
374	if (write_only)
375		pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
376	else
377		pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, "
378			"rangelow=%u, rangehigh=%u, signal=%u, afc=%d, "
379			"rxsubchans=0x%x, audmode=%u\n",
380			p->index, (int)sizeof(p->name), p->name, p->type,
381			p->capability, p->rangelow,
382			p->rangehigh, p->signal, p->afc,
383			p->rxsubchans, p->audmode);
384}
385
386static void v4l_print_frequency(const void *arg, bool write_only)
387{
388	const struct v4l2_frequency *p = arg;
389
390	pr_cont("tuner=%u, type=%u, frequency=%u\n",
391				p->tuner, p->type, p->frequency);
392}
393
394static void v4l_print_standard(const void *arg, bool write_only)
395{
396	const struct v4l2_standard *p = arg;
397
398	pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, "
399		"framelines=%u\n", p->index,
400		(unsigned long long)p->id, (int)sizeof(p->name), p->name,
401		p->frameperiod.numerator,
402		p->frameperiod.denominator,
403		p->framelines);
404}
405
406static void v4l_print_std(const void *arg, bool write_only)
407{
408	pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
409}
410
411static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
412{
413	const struct v4l2_hw_freq_seek *p = arg;
414
415	pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, "
416		"rangelow=%u, rangehigh=%u\n",
417		p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
418		p->rangelow, p->rangehigh);
419}
420
421static void v4l_print_requestbuffers(const void *arg, bool write_only)
422{
423	const struct v4l2_requestbuffers *p = arg;
424
425	pr_cont("count=%d, type=%s, memory=%s\n",
426		p->count,
427		prt_names(p->type, v4l2_type_names),
428		prt_names(p->memory, v4l2_memory_names));
429}
430
431static void v4l_print_buffer(const void *arg, bool write_only)
432{
433	const struct v4l2_buffer *p = arg;
434	const struct v4l2_timecode *tc = &p->timecode;
435	const struct v4l2_plane *plane;
436	int i;
437
438	pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, "
439		"flags=0x%08x, field=%s, sequence=%d, memory=%s",
440			p->timestamp.tv_sec / 3600,
441			(int)(p->timestamp.tv_sec / 60) % 60,
442			(int)(p->timestamp.tv_sec % 60),
443			(long)p->timestamp.tv_usec,
444			p->index,
445			prt_names(p->type, v4l2_type_names),
446			p->flags, prt_names(p->field, v4l2_field_names),
447			p->sequence, prt_names(p->memory, v4l2_memory_names));
448
449	if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
450		pr_cont("\n");
451		for (i = 0; i < p->length; ++i) {
452			plane = &p->m.planes[i];
453			printk(KERN_DEBUG
454				"plane %d: bytesused=%d, data_offset=0x%08x, "
455				"offset/userptr=0x%lx, length=%d\n",
456				i, plane->bytesused, plane->data_offset,
457				plane->m.userptr, plane->length);
458		}
459	} else {
460		pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
461			p->bytesused, p->m.userptr, p->length);
462	}
463
464	printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, "
465		"flags=0x%08x, frames=%d, userbits=0x%08x\n",
466			tc->hours, tc->minutes, tc->seconds,
467			tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
468}
469
470static void v4l_print_exportbuffer(const void *arg, bool write_only)
471{
472	const struct v4l2_exportbuffer *p = arg;
473
474	pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
475		p->fd, prt_names(p->type, v4l2_type_names),
476		p->index, p->plane, p->flags);
477}
478
479static void v4l_print_create_buffers(const void *arg, bool write_only)
480{
481	const struct v4l2_create_buffers *p = arg;
482
483	pr_cont("index=%d, count=%d, memory=%s, ",
484			p->index, p->count,
485			prt_names(p->memory, v4l2_memory_names));
486	v4l_print_format(&p->format, write_only);
487}
488
489static void v4l_print_streamparm(const void *arg, bool write_only)
490{
491	const struct v4l2_streamparm *p = arg;
492
493	pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
494
495	if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
496	    p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
497		const struct v4l2_captureparm *c = &p->parm.capture;
498
499		pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, "
500			"extendedmode=%d, readbuffers=%d\n",
501			c->capability, c->capturemode,
502			c->timeperframe.numerator, c->timeperframe.denominator,
503			c->extendedmode, c->readbuffers);
504	} else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
505		   p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
506		const struct v4l2_outputparm *c = &p->parm.output;
507
508		pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, "
509			"extendedmode=%d, writebuffers=%d\n",
510			c->capability, c->outputmode,
511			c->timeperframe.numerator, c->timeperframe.denominator,
512			c->extendedmode, c->writebuffers);
513	} else {
514		pr_cont("\n");
515	}
516}
517
518static void v4l_print_queryctrl(const void *arg, bool write_only)
519{
520	const struct v4l2_queryctrl *p = arg;
521
522	pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, "
523		"step=%d, default=%d, flags=0x%08x\n",
524			p->id, p->type, (int)sizeof(p->name), p->name,
525			p->minimum, p->maximum,
526			p->step, p->default_value, p->flags);
527}
528
529static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
530{
531	const struct v4l2_query_ext_ctrl *p = arg;
532
533	pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%lld/%lld, "
534		"step=%lld, default=%lld, flags=0x%08x, elem_size=%u, elems=%u, "
535		"nr_of_dims=%u, dims=%u,%u,%u,%u\n",
536			p->id, p->type, (int)sizeof(p->name), p->name,
537			p->minimum, p->maximum,
538			p->step, p->default_value, p->flags,
539			p->elem_size, p->elems, p->nr_of_dims,
540			p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
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 void v4l_sanitize_format(struct v4l2_format *fmt)
978{
979	unsigned int offset;
980
981	/*
982	 * The v4l2_pix_format structure has been extended with fields that were
983	 * not previously required to be set to zero by applications. The priv
984	 * field, when set to a magic value, indicates the the extended fields
985	 * are valid. Otherwise they will contain undefined values. To simplify
986	 * the API towards drivers zero the extended fields and set the priv
987	 * field to the magic value when the extended pixel format structure
988	 * isn't used by applications.
989	 */
990
991	if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
992	    fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
993		return;
994
995	if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
996		return;
997
998	fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
999
1000	offset = offsetof(struct v4l2_pix_format, priv)
1001	       + sizeof(fmt->fmt.pix.priv);
1002	memset(((void *)&fmt->fmt.pix) + offset, 0,
1003	       sizeof(fmt->fmt.pix) - offset);
1004}
1005
1006static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
1007				struct file *file, void *fh, void *arg)
1008{
1009	struct v4l2_capability *cap = (struct v4l2_capability *)arg;
1010	int ret;
1011
1012	cap->version = LINUX_VERSION_CODE;
1013
1014	ret = ops->vidioc_querycap(file, fh, cap);
1015
1016	cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
1017
1018	return ret;
1019}
1020
1021static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
1022				struct file *file, void *fh, void *arg)
1023{
1024	return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
1025}
1026
1027static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
1028				struct file *file, void *fh, void *arg)
1029{
1030	return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
1031}
1032
1033static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
1034				struct file *file, void *fh, void *arg)
1035{
1036	struct video_device *vfd;
1037	u32 *p = arg;
1038
1039	if (ops->vidioc_g_priority)
1040		return ops->vidioc_g_priority(file, fh, arg);
1041	vfd = video_devdata(file);
1042	*p = v4l2_prio_max(&vfd->v4l2_dev->prio);
1043	return 0;
1044}
1045
1046static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
1047				struct file *file, void *fh, void *arg)
1048{
1049	struct video_device *vfd;
1050	struct v4l2_fh *vfh;
1051	u32 *p = arg;
1052
1053	if (ops->vidioc_s_priority)
1054		return ops->vidioc_s_priority(file, fh, *p);
1055	vfd = video_devdata(file);
1056	vfh = file->private_data;
1057	return v4l2_prio_change(&vfd->v4l2_dev->prio, &vfh->prio, *p);
1058}
1059
1060static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1061				struct file *file, void *fh, void *arg)
1062{
1063	struct video_device *vfd = video_devdata(file);
1064	struct v4l2_input *p = arg;
1065
1066	/*
1067	 * We set the flags for CAP_DV_TIMINGS &
1068	 * CAP_STD here based on ioctl handler provided by the
1069	 * driver. If the driver doesn't support these
1070	 * for a specific input, it must override these flags.
1071	 */
1072	if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1073		p->capabilities |= V4L2_IN_CAP_STD;
1074
1075	return ops->vidioc_enum_input(file, fh, p);
1076}
1077
1078static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1079				struct file *file, void *fh, void *arg)
1080{
1081	struct video_device *vfd = video_devdata(file);
1082	struct v4l2_output *p = arg;
1083
1084	/*
1085	 * We set the flags for CAP_DV_TIMINGS &
1086	 * CAP_STD here based on ioctl handler provided by the
1087	 * driver. If the driver doesn't support these
1088	 * for a specific output, it must override these flags.
1089	 */
1090	if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1091		p->capabilities |= V4L2_OUT_CAP_STD;
1092
1093	return ops->vidioc_enum_output(file, fh, p);
1094}
1095
1096static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1097				struct file *file, void *fh, void *arg)
1098{
1099	struct v4l2_fmtdesc *p = arg;
1100	struct video_device *vfd = video_devdata(file);
1101	bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1102	bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
1103	bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1104	bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1105
1106	switch (p->type) {
1107	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1108		if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_cap))
1109			break;
1110		return ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1111	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1112		if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_cap_mplane))
1113			break;
1114		return ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1115	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1116		if (unlikely(!is_rx || !is_vid || !ops->vidioc_enum_fmt_vid_overlay))
1117			break;
1118		return ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1119	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1120		if (unlikely(!is_tx || !is_vid || !ops->vidioc_enum_fmt_vid_out))
1121			break;
1122		return ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1123	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1124		if (unlikely(!is_tx || !is_vid || !ops->vidioc_enum_fmt_vid_out_mplane))
1125			break;
1126		return ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
1127	case V4L2_BUF_TYPE_SDR_CAPTURE:
1128		if (unlikely(!is_rx || !is_sdr || !ops->vidioc_enum_fmt_sdr_cap))
1129			break;
1130		return ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
1131	}
1132	return -EINVAL;
1133}
1134
1135static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1136				struct file *file, void *fh, void *arg)
1137{
1138	struct v4l2_format *p = arg;
1139	struct video_device *vfd = video_devdata(file);
1140	bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1141	bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
1142	bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1143	bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1144	int ret;
1145
1146	p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1147
1148	switch (p->type) {
1149	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1150		if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap))
1151			break;
1152		ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1153		p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1154		return ret;
1155	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1156		if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_cap_mplane))
1157			break;
1158		return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1159	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1160		if (unlikely(!is_rx || !is_vid || !ops->vidioc_g_fmt_vid_overlay))
1161			break;
1162		return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1163	case V4L2_BUF_TYPE_VBI_CAPTURE:
1164		if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_vbi_cap))
1165			break;
1166		return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1167	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1168		if (unlikely(!is_rx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_cap))
1169			break;
1170		return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1171	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1172		if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out))
1173			break;
1174		ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
1175		p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1176		return ret;
1177	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1178		if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_mplane))
1179			break;
1180		return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1181	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1182		if (unlikely(!is_tx || !is_vid || !ops->vidioc_g_fmt_vid_out_overlay))
1183			break;
1184		return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1185	case V4L2_BUF_TYPE_VBI_OUTPUT:
1186		if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_vbi_out))
1187			break;
1188		return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1189	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1190		if (unlikely(!is_tx || is_vid || !ops->vidioc_g_fmt_sliced_vbi_out))
1191			break;
1192		return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1193	case V4L2_BUF_TYPE_SDR_CAPTURE:
1194		if (unlikely(!is_rx || !is_sdr || !ops->vidioc_g_fmt_sdr_cap))
1195			break;
1196		return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
1197	}
1198	return -EINVAL;
1199}
1200
1201static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1202				struct file *file, void *fh, void *arg)
1203{
1204	struct v4l2_format *p = arg;
1205	struct video_device *vfd = video_devdata(file);
1206	bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1207	bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
1208	bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1209	bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1210
1211	v4l_sanitize_format(p);
1212
1213	switch (p->type) {
1214	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1215		if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap))
1216			break;
1217		CLEAR_AFTER_FIELD(p, fmt.pix);
1218		return ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1219	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1220		if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_cap_mplane))
1221			break;
1222		CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1223		return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1224	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1225		if (unlikely(!is_rx || !is_vid || !ops->vidioc_s_fmt_vid_overlay))
1226			break;
1227		CLEAR_AFTER_FIELD(p, fmt.win);
1228		return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1229	case V4L2_BUF_TYPE_VBI_CAPTURE:
1230		if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_vbi_cap))
1231			break;
1232		CLEAR_AFTER_FIELD(p, fmt.vbi);
1233		return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1234	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1235		if (unlikely(!is_rx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_cap))
1236			break;
1237		CLEAR_AFTER_FIELD(p, fmt.sliced);
1238		return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1239	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1240		if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out))
1241			break;
1242		CLEAR_AFTER_FIELD(p, fmt.pix);
1243		return ops->vidioc_s_fmt_vid_out(file, fh, arg);
1244	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1245		if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_mplane))
1246			break;
1247		CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1248		return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1249	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1250		if (unlikely(!is_tx || !is_vid || !ops->vidioc_s_fmt_vid_out_overlay))
1251			break;
1252		CLEAR_AFTER_FIELD(p, fmt.win);
1253		return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1254	case V4L2_BUF_TYPE_VBI_OUTPUT:
1255		if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_vbi_out))
1256			break;
1257		CLEAR_AFTER_FIELD(p, fmt.vbi);
1258		return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1259	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1260		if (unlikely(!is_tx || is_vid || !ops->vidioc_s_fmt_sliced_vbi_out))
1261			break;
1262		CLEAR_AFTER_FIELD(p, fmt.sliced);
1263		return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
1264	case V4L2_BUF_TYPE_SDR_CAPTURE:
1265		if (unlikely(!is_rx || !is_sdr || !ops->vidioc_s_fmt_sdr_cap))
1266			break;
1267		CLEAR_AFTER_FIELD(p, fmt.sdr);
1268		return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
1269	}
1270	return -EINVAL;
1271}
1272
1273static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1274				struct file *file, void *fh, void *arg)
1275{
1276	struct v4l2_format *p = arg;
1277	struct video_device *vfd = video_devdata(file);
1278	bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
1279	bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
1280	bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
1281	bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
1282
1283	v4l_sanitize_format(p);
1284
1285	switch (p->type) {
1286	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1287		if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap))
1288			break;
1289		CLEAR_AFTER_FIELD(p, fmt.pix);
1290		return ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1291	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1292		if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_cap_mplane))
1293			break;
1294		CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1295		return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1296	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1297		if (unlikely(!is_rx || !is_vid || !ops->vidioc_try_fmt_vid_overlay))
1298			break;
1299		CLEAR_AFTER_FIELD(p, fmt.win);
1300		return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1301	case V4L2_BUF_TYPE_VBI_CAPTURE:
1302		if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_vbi_cap))
1303			break;
1304		CLEAR_AFTER_FIELD(p, fmt.vbi);
1305		return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1306	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1307		if (unlikely(!is_rx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_cap))
1308			break;
1309		CLEAR_AFTER_FIELD(p, fmt.sliced);
1310		return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1311	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1312		if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out))
1313			break;
1314		CLEAR_AFTER_FIELD(p, fmt.pix);
1315		return ops->vidioc_try_fmt_vid_out(file, fh, arg);
1316	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1317		if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_mplane))
1318			break;
1319		CLEAR_AFTER_FIELD(p, fmt.pix_mp);
1320		return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1321	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1322		if (unlikely(!is_tx || !is_vid || !ops->vidioc_try_fmt_vid_out_overlay))
1323			break;
1324		CLEAR_AFTER_FIELD(p, fmt.win);
1325		return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1326	case V4L2_BUF_TYPE_VBI_OUTPUT:
1327		if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_vbi_out))
1328			break;
1329		CLEAR_AFTER_FIELD(p, fmt.vbi);
1330		return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1331	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1332		if (unlikely(!is_tx || is_vid || !ops->vidioc_try_fmt_sliced_vbi_out))
1333			break;
1334		CLEAR_AFTER_FIELD(p, fmt.sliced);
1335		return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1336	case V4L2_BUF_TYPE_SDR_CAPTURE:
1337		if (unlikely(!is_rx || !is_sdr || !ops->vidioc_try_fmt_sdr_cap))
1338			break;
1339		CLEAR_AFTER_FIELD(p, fmt.sdr);
1340		return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
1341	}
1342	return -EINVAL;
1343}
1344
1345static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1346				struct file *file, void *fh, void *arg)
1347{
1348	return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1349}
1350
1351static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1352				struct file *file, void *fh, void *arg)
1353{
1354	return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1355}
1356
1357static int v4l_g_tuner(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_tuner *p = arg;
1362	int err;
1363
1364	p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1365			V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1366	err = ops->vidioc_g_tuner(file, fh, p);
1367	if (!err)
1368		p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1369	return err;
1370}
1371
1372static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1373				struct file *file, void *fh, void *arg)
1374{
1375	struct video_device *vfd = video_devdata(file);
1376	struct v4l2_tuner *p = arg;
1377
1378	p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1379			V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1380	return ops->vidioc_s_tuner(file, fh, p);
1381}
1382
1383static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1384				struct file *file, void *fh, void *arg)
1385{
1386	struct v4l2_modulator *p = arg;
1387	int err;
1388
1389	err = ops->vidioc_g_modulator(file, fh, p);
1390	if (!err)
1391		p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1392	return err;
1393}
1394
1395static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1396				struct file *file, void *fh, void *arg)
1397{
1398	struct video_device *vfd = video_devdata(file);
1399	struct v4l2_frequency *p = arg;
1400
1401	if (vfd->vfl_type == VFL_TYPE_SDR)
1402		p->type = V4L2_TUNER_ADC;
1403	else
1404		p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1405				V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1406	return ops->vidioc_g_frequency(file, fh, p);
1407}
1408
1409static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1410				struct file *file, void *fh, void *arg)
1411{
1412	struct video_device *vfd = video_devdata(file);
1413	const struct v4l2_frequency *p = arg;
1414	enum v4l2_tuner_type type;
1415
1416	if (vfd->vfl_type == VFL_TYPE_SDR) {
1417		if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
1418			return -EINVAL;
1419	} else {
1420		type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1421				V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1422		if (type != p->type)
1423			return -EINVAL;
1424	}
1425	return ops->vidioc_s_frequency(file, fh, p);
1426}
1427
1428static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1429				struct file *file, void *fh, void *arg)
1430{
1431	struct video_device *vfd = video_devdata(file);
1432	struct v4l2_standard *p = arg;
1433	v4l2_std_id id = vfd->tvnorms, curr_id = 0;
1434	unsigned int index = p->index, i, j = 0;
1435	const char *descr = "";
1436
1437	/* Return -ENODATA if the tvnorms for the current input
1438	   or output is 0, meaning that it doesn't support this API. */
1439	if (id == 0)
1440		return -ENODATA;
1441
1442	/* Return norm array in a canonical way */
1443	for (i = 0; i <= index && id; i++) {
1444		/* last std value in the standards array is 0, so this
1445		   while always ends there since (id & 0) == 0. */
1446		while ((id & standards[j].std) != standards[j].std)
1447			j++;
1448		curr_id = standards[j].std;
1449		descr = standards[j].descr;
1450		j++;
1451		if (curr_id == 0)
1452			break;
1453		if (curr_id != V4L2_STD_PAL &&
1454				curr_id != V4L2_STD_SECAM &&
1455				curr_id != V4L2_STD_NTSC)
1456			id &= ~curr_id;
1457	}
1458	if (i <= index)
1459		return -EINVAL;
1460
1461	v4l2_video_std_construct(p, curr_id, descr);
1462	return 0;
1463}
1464
1465static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1466				struct file *file, void *fh, void *arg)
1467{
1468	struct video_device *vfd = video_devdata(file);
1469	v4l2_std_id id = *(v4l2_std_id *)arg, norm;
1470
1471	norm = id & vfd->tvnorms;
1472	if (vfd->tvnorms && !norm)	/* Check if std is supported */
1473		return -EINVAL;
1474
1475	/* Calls the specific handler */
1476	return ops->vidioc_s_std(file, fh, norm);
1477}
1478
1479static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1480				struct file *file, void *fh, void *arg)
1481{
1482	struct video_device *vfd = video_devdata(file);
1483	v4l2_std_id *p = arg;
1484
1485	/*
1486	 * If no signal is detected, then the driver should return
1487	 * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
1488	 * any standards that do not apply removed.
1489	 *
1490	 * This means that tuners, audio and video decoders can join
1491	 * their efforts to improve the standards detection.
1492	 */
1493	*p = vfd->tvnorms;
1494	return ops->vidioc_querystd(file, fh, arg);
1495}
1496
1497static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1498				struct file *file, void *fh, void *arg)
1499{
1500	struct video_device *vfd = video_devdata(file);
1501	struct v4l2_hw_freq_seek *p = arg;
1502	enum v4l2_tuner_type type;
1503
1504	/* s_hw_freq_seek is not supported for SDR for now */
1505	if (vfd->vfl_type == VFL_TYPE_SDR)
1506		return -EINVAL;
1507
1508	type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1509		V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1510	if (p->type != type)
1511		return -EINVAL;
1512	return ops->vidioc_s_hw_freq_seek(file, fh, p);
1513}
1514
1515static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
1516				struct file *file, void *fh, void *arg)
1517{
1518	return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
1519}
1520
1521static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1522				struct file *file, void *fh, void *arg)
1523{
1524	struct v4l2_requestbuffers *p = arg;
1525	int ret = check_fmt(file, p->type);
1526
1527	if (ret)
1528		return ret;
1529
1530	CLEAR_AFTER_FIELD(p, memory);
1531
1532	return ops->vidioc_reqbufs(file, fh, p);
1533}
1534
1535static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1536				struct file *file, void *fh, void *arg)
1537{
1538	struct v4l2_buffer *p = arg;
1539	int ret = check_fmt(file, p->type);
1540
1541	return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1542}
1543
1544static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1545				struct file *file, void *fh, void *arg)
1546{
1547	struct v4l2_buffer *p = arg;
1548	int ret = check_fmt(file, p->type);
1549
1550	return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1551}
1552
1553static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1554				struct file *file, void *fh, void *arg)
1555{
1556	struct v4l2_buffer *p = arg;
1557	int ret = check_fmt(file, p->type);
1558
1559	return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1560}
1561
1562static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1563				struct file *file, void *fh, void *arg)
1564{
1565	struct v4l2_create_buffers *create = arg;
1566	int ret = check_fmt(file, create->format.type);
1567
1568	if (ret)
1569		return ret;
1570
1571	v4l_sanitize_format(&create->format);
1572
1573	ret = ops->vidioc_create_bufs(file, fh, create);
1574
1575	if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1576	    create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1577		create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1578
1579	return ret;
1580}
1581
1582static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1583				struct file *file, void *fh, void *arg)
1584{
1585	struct v4l2_buffer *b = arg;
1586	int ret = check_fmt(file, b->type);
1587
1588	return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1589}
1590
1591static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1592				struct file *file, void *fh, void *arg)
1593{
1594	struct v4l2_streamparm *p = arg;
1595	v4l2_std_id std;
1596	int ret = check_fmt(file, p->type);
1597
1598	if (ret)
1599		return ret;
1600	if (ops->vidioc_g_parm)
1601		return ops->vidioc_g_parm(file, fh, p);
1602	if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1603	    p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1604		return -EINVAL;
1605	p->parm.capture.readbuffers = 2;
1606	ret = ops->vidioc_g_std(file, fh, &std);
1607	if (ret == 0)
1608		v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
1609	return ret;
1610}
1611
1612static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
1613				struct file *file, void *fh, void *arg)
1614{
1615	struct v4l2_streamparm *p = arg;
1616	int ret = check_fmt(file, p->type);
1617
1618	return ret ? ret : ops->vidioc_s_parm(file, fh, p);
1619}
1620
1621static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
1622				struct file *file, void *fh, void *arg)
1623{
1624	struct video_device *vfd = video_devdata(file);
1625	struct v4l2_queryctrl *p = arg;
1626	struct v4l2_fh *vfh =
1627		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1628
1629	if (vfh && vfh->ctrl_handler)
1630		return v4l2_queryctrl(vfh->ctrl_handler, p);
1631	if (vfd->ctrl_handler)
1632		return v4l2_queryctrl(vfd->ctrl_handler, p);
1633	if (ops->vidioc_queryctrl)
1634		return ops->vidioc_queryctrl(file, fh, p);
1635	return -ENOTTY;
1636}
1637
1638static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
1639				struct file *file, void *fh, void *arg)
1640{
1641	struct video_device *vfd = video_devdata(file);
1642	struct v4l2_query_ext_ctrl *p = arg;
1643	struct v4l2_fh *vfh =
1644		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1645
1646	if (vfh && vfh->ctrl_handler)
1647		return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
1648	if (vfd->ctrl_handler)
1649		return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
1650	if (ops->vidioc_query_ext_ctrl)
1651		return ops->vidioc_query_ext_ctrl(file, fh, p);
1652	return -ENOTTY;
1653}
1654
1655static int v4l_querymenu(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_querymenu *p = arg;
1660	struct v4l2_fh *vfh =
1661		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1662
1663	if (vfh && vfh->ctrl_handler)
1664		return v4l2_querymenu(vfh->ctrl_handler, p);
1665	if (vfd->ctrl_handler)
1666		return v4l2_querymenu(vfd->ctrl_handler, p);
1667	if (ops->vidioc_querymenu)
1668		return ops->vidioc_querymenu(file, fh, p);
1669	return -ENOTTY;
1670}
1671
1672static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
1673				struct file *file, void *fh, void *arg)
1674{
1675	struct video_device *vfd = video_devdata(file);
1676	struct v4l2_control *p = arg;
1677	struct v4l2_fh *vfh =
1678		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1679	struct v4l2_ext_controls ctrls;
1680	struct v4l2_ext_control ctrl;
1681
1682	if (vfh && vfh->ctrl_handler)
1683		return v4l2_g_ctrl(vfh->ctrl_handler, p);
1684	if (vfd->ctrl_handler)
1685		return v4l2_g_ctrl(vfd->ctrl_handler, p);
1686	if (ops->vidioc_g_ctrl)
1687		return ops->vidioc_g_ctrl(file, fh, p);
1688	if (ops->vidioc_g_ext_ctrls == NULL)
1689		return -ENOTTY;
1690
1691	ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1692	ctrls.count = 1;
1693	ctrls.controls = &ctrl;
1694	ctrl.id = p->id;
1695	ctrl.value = p->value;
1696	if (check_ext_ctrls(&ctrls, 1)) {
1697		int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
1698
1699		if (ret == 0)
1700			p->value = ctrl.value;
1701		return ret;
1702	}
1703	return -EINVAL;
1704}
1705
1706static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
1707				struct file *file, void *fh, void *arg)
1708{
1709	struct video_device *vfd = video_devdata(file);
1710	struct v4l2_control *p = arg;
1711	struct v4l2_fh *vfh =
1712		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1713	struct v4l2_ext_controls ctrls;
1714	struct v4l2_ext_control ctrl;
1715
1716	if (vfh && vfh->ctrl_handler)
1717		return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
1718	if (vfd->ctrl_handler)
1719		return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
1720	if (ops->vidioc_s_ctrl)
1721		return ops->vidioc_s_ctrl(file, fh, p);
1722	if (ops->vidioc_s_ext_ctrls == NULL)
1723		return -ENOTTY;
1724
1725	ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(p->id);
1726	ctrls.count = 1;
1727	ctrls.controls = &ctrl;
1728	ctrl.id = p->id;
1729	ctrl.value = p->value;
1730	if (check_ext_ctrls(&ctrls, 1))
1731		return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
1732	return -EINVAL;
1733}
1734
1735static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1736				struct file *file, void *fh, void *arg)
1737{
1738	struct video_device *vfd = video_devdata(file);
1739	struct v4l2_ext_controls *p = arg;
1740	struct v4l2_fh *vfh =
1741		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1742
1743	p->error_idx = p->count;
1744	if (vfh && vfh->ctrl_handler)
1745		return v4l2_g_ext_ctrls(vfh->ctrl_handler, p);
1746	if (vfd->ctrl_handler)
1747		return v4l2_g_ext_ctrls(vfd->ctrl_handler, p);
1748	if (ops->vidioc_g_ext_ctrls == NULL)
1749		return -ENOTTY;
1750	return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
1751					-EINVAL;
1752}
1753
1754static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1755				struct file *file, void *fh, void *arg)
1756{
1757	struct video_device *vfd = video_devdata(file);
1758	struct v4l2_ext_controls *p = arg;
1759	struct v4l2_fh *vfh =
1760		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1761
1762	p->error_idx = p->count;
1763	if (vfh && vfh->ctrl_handler)
1764		return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, p);
1765	if (vfd->ctrl_handler)
1766		return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, p);
1767	if (ops->vidioc_s_ext_ctrls == NULL)
1768		return -ENOTTY;
1769	return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
1770					-EINVAL;
1771}
1772
1773static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
1774				struct file *file, void *fh, void *arg)
1775{
1776	struct video_device *vfd = video_devdata(file);
1777	struct v4l2_ext_controls *p = arg;
1778	struct v4l2_fh *vfh =
1779		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
1780
1781	p->error_idx = p->count;
1782	if (vfh && vfh->ctrl_handler)
1783		return v4l2_try_ext_ctrls(vfh->ctrl_handler, p);
1784	if (vfd->ctrl_handler)
1785		return v4l2_try_ext_ctrls(vfd->ctrl_handler, p);
1786	if (ops->vidioc_try_ext_ctrls == NULL)
1787		return -ENOTTY;
1788	return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
1789					-EINVAL;
1790}
1791
1792static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
1793				struct file *file, void *fh, void *arg)
1794{
1795	struct v4l2_crop *p = arg;
1796	struct v4l2_selection s = {
1797		.type = p->type,
1798	};
1799	int ret;
1800
1801	if (ops->vidioc_g_crop)
1802		return ops->vidioc_g_crop(file, fh, p);
1803	/* simulate capture crop using selection api */
1804
1805	/* crop means compose for output devices */
1806	if (V4L2_TYPE_IS_OUTPUT(p->type))
1807		s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1808	else
1809		s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1810
1811	ret = ops->vidioc_g_selection(file, fh, &s);
1812
1813	/* copying results to old structure on success */
1814	if (!ret)
1815		p->c = s.r;
1816	return ret;
1817}
1818
1819static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
1820				struct file *file, void *fh, void *arg)
1821{
1822	struct v4l2_crop *p = arg;
1823	struct v4l2_selection s = {
1824		.type = p->type,
1825		.r = p->c,
1826	};
1827
1828	if (ops->vidioc_s_crop)
1829		return ops->vidioc_s_crop(file, fh, p);
1830	/* simulate capture crop using selection api */
1831
1832	/* crop means compose for output devices */
1833	if (V4L2_TYPE_IS_OUTPUT(p->type))
1834		s.target = V4L2_SEL_TGT_COMPOSE_ACTIVE;
1835	else
1836		s.target = V4L2_SEL_TGT_CROP_ACTIVE;
1837
1838	return ops->vidioc_s_selection(file, fh, &s);
1839}
1840
1841static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
1842				struct file *file, void *fh, void *arg)
1843{
1844	struct v4l2_cropcap *p = arg;
1845	struct v4l2_selection s = { .type = p->type };
1846	int ret;
1847
1848	if (ops->vidioc_cropcap)
1849		return ops->vidioc_cropcap(file, fh, p);
1850
1851	/* obtaining bounds */
1852	if (V4L2_TYPE_IS_OUTPUT(p->type))
1853		s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
1854	else
1855		s.target = V4L2_SEL_TGT_CROP_BOUNDS;
1856
1857	ret = ops->vidioc_g_selection(file, fh, &s);
1858	if (ret)
1859		return ret;
1860	p->bounds = s.r;
1861
1862	/* obtaining defrect */
1863	if (V4L2_TYPE_IS_OUTPUT(p->type))
1864		s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
1865	else
1866		s.target = V4L2_SEL_TGT_CROP_DEFAULT;
1867
1868	ret = ops->vidioc_g_selection(file, fh, &s);
1869	if (ret)
1870		return ret;
1871	p->defrect = s.r;
1872
1873	/* setting trivial pixelaspect */
1874	p->pixelaspect.numerator = 1;
1875	p->pixelaspect.denominator = 1;
1876	return 0;
1877}
1878
1879static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
1880				struct file *file, void *fh, void *arg)
1881{
1882	struct video_device *vfd = video_devdata(file);
1883	int ret;
1884
1885	if (vfd->v4l2_dev)
1886		pr_info("%s: =================  START STATUS  =================\n",
1887			vfd->v4l2_dev->name);
1888	ret = ops->vidioc_log_status(file, fh);
1889	if (vfd->v4l2_dev)
1890		pr_info("%s: ==================  END STATUS  ==================\n",
1891			vfd->v4l2_dev->name);
1892	return ret;
1893}
1894
1895static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
1896				struct file *file, void *fh, void *arg)
1897{
1898#ifdef CONFIG_VIDEO_ADV_DEBUG
1899	struct v4l2_dbg_register *p = arg;
1900	struct video_device *vfd = video_devdata(file);
1901	struct v4l2_subdev *sd;
1902	int idx = 0;
1903
1904	if (!capable(CAP_SYS_ADMIN))
1905		return -EPERM;
1906	if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
1907		if (vfd->v4l2_dev == NULL)
1908			return -EINVAL;
1909		v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1910			if (p->match.addr == idx++)
1911				return v4l2_subdev_call(sd, core, g_register, p);
1912		return -EINVAL;
1913	}
1914	if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1915	    (ops->vidioc_g_chip_info || p->match.addr == 0))
1916		return ops->vidioc_g_register(file, fh, p);
1917	return -EINVAL;
1918#else
1919	return -ENOTTY;
1920#endif
1921}
1922
1923static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
1924				struct file *file, void *fh, void *arg)
1925{
1926#ifdef CONFIG_VIDEO_ADV_DEBUG
1927	const struct v4l2_dbg_register *p = arg;
1928	struct video_device *vfd = video_devdata(file);
1929	struct v4l2_subdev *sd;
1930	int idx = 0;
1931
1932	if (!capable(CAP_SYS_ADMIN))
1933		return -EPERM;
1934	if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
1935		if (vfd->v4l2_dev == NULL)
1936			return -EINVAL;
1937		v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
1938			if (p->match.addr == idx++)
1939				return v4l2_subdev_call(sd, core, s_register, p);
1940		return -EINVAL;
1941	}
1942	if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
1943	    (ops->vidioc_g_chip_info || p->match.addr == 0))
1944		return ops->vidioc_s_register(file, fh, p);
1945	return -EINVAL;
1946#else
1947	return -ENOTTY;
1948#endif
1949}
1950
1951static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
1952				struct file *file, void *fh, void *arg)
1953{
1954#ifdef CONFIG_VIDEO_ADV_DEBUG
1955	struct video_device *vfd = video_devdata(file);
1956	struct v4l2_dbg_chip_info *p = arg;
1957	struct v4l2_subdev *sd;
1958	int idx = 0;
1959
1960	switch (p->match.type) {
1961	case V4L2_CHIP_MATCH_BRIDGE:
1962		if (ops->vidioc_s_register)
1963			p->flags |= V4L2_CHIP_FL_WRITABLE;
1964		if (ops->vidioc_g_register)
1965			p->flags |= V4L2_CHIP_FL_READABLE;
1966		strlcpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
1967		if (ops->vidioc_g_chip_info)
1968			return ops->vidioc_g_chip_info(file, fh, arg);
1969		if (p->match.addr)
1970			return -EINVAL;
1971		return 0;
1972
1973	case V4L2_CHIP_MATCH_SUBDEV:
1974		if (vfd->v4l2_dev == NULL)
1975			break;
1976		v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
1977			if (p->match.addr != idx++)
1978				continue;
1979			if (sd->ops->core && sd->ops->core->s_register)
1980				p->flags |= V4L2_CHIP_FL_WRITABLE;
1981			if (sd->ops->core && sd->ops->core->g_register)
1982				p->flags |= V4L2_CHIP_FL_READABLE;
1983			strlcpy(p->name, sd->name, sizeof(p->name));
1984			return 0;
1985		}
1986		break;
1987	}
1988	return -EINVAL;
1989#else
1990	return -ENOTTY;
1991#endif
1992}
1993
1994static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
1995				struct file *file, void *fh, void *arg)
1996{
1997	return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
1998}
1999
2000static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
2001				struct file *file, void *fh, void *arg)
2002{
2003	return ops->vidioc_subscribe_event(fh, arg);
2004}
2005
2006static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
2007				struct file *file, void *fh, void *arg)
2008{
2009	return ops->vidioc_unsubscribe_event(fh, arg);
2010}
2011
2012static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
2013				struct file *file, void *fh, void *arg)
2014{
2015	struct v4l2_sliced_vbi_cap *p = arg;
2016	int ret = check_fmt(file, p->type);
2017
2018	if (ret)
2019		return ret;
2020
2021	/* Clear up to type, everything after type is zeroed already */
2022	memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
2023
2024	return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
2025}
2026
2027static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
2028				struct file *file, void *fh, void *arg)
2029{
2030	struct video_device *vfd = video_devdata(file);
2031	struct v4l2_frequency_band *p = arg;
2032	enum v4l2_tuner_type type;
2033	int err;
2034
2035	if (vfd->vfl_type == VFL_TYPE_SDR) {
2036		if (p->type != V4L2_TUNER_ADC && p->type != V4L2_TUNER_RF)
2037			return -EINVAL;
2038		type = p->type;
2039	} else {
2040		type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2041				V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2042		if (type != p->type)
2043			return -EINVAL;
2044	}
2045	if (ops->vidioc_enum_freq_bands) {
2046		err = ops->vidioc_enum_freq_bands(file, fh, p);
2047		if (err != -ENOTTY)
2048			return err;
2049	}
2050	if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
2051		struct v4l2_tuner t = {
2052			.index = p->tuner,
2053			.type = type,
2054		};
2055
2056		if (p->index)
2057			return -EINVAL;
2058		err = ops->vidioc_g_tuner(file, fh, &t);
2059		if (err)
2060			return err;
2061		p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2062		p->rangelow = t.rangelow;
2063		p->rangehigh = t.rangehigh;
2064		p->modulation = (type == V4L2_TUNER_RADIO) ?
2065			V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2066		return 0;
2067	}
2068	if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
2069		struct v4l2_modulator m = {
2070			.index = p->tuner,
2071		};
2072
2073		if (type != V4L2_TUNER_RADIO)
2074			return -EINVAL;
2075		if (p->index)
2076			return -EINVAL;
2077		err = ops->vidioc_g_modulator(file, fh, &m);
2078		if (err)
2079			return err;
2080		p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2081		p->rangelow = m.rangelow;
2082		p->rangehigh = m.rangehigh;
2083		p->modulation = (type == V4L2_TUNER_RADIO) ?
2084			V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2085		return 0;
2086	}
2087	return -ENOTTY;
2088}
2089
2090struct v4l2_ioctl_info {
2091	unsigned int ioctl;
2092	u32 flags;
2093	const char * const name;
2094	union {
2095		u32 offset;
2096		int (*func)(const struct v4l2_ioctl_ops *ops,
2097				struct file *file, void *fh, void *p);
2098	} u;
2099	void (*debug)(const void *arg, bool write_only);
2100};
2101
2102/* This control needs a priority check */
2103#define INFO_FL_PRIO	(1 << 0)
2104/* This control can be valid if the filehandle passes a control handler. */
2105#define INFO_FL_CTRL	(1 << 1)
2106/* This is a standard ioctl, no need for special code */
2107#define INFO_FL_STD	(1 << 2)
2108/* This is ioctl has its own function */
2109#define INFO_FL_FUNC	(1 << 3)
2110/* Queuing ioctl */
2111#define INFO_FL_QUEUE	(1 << 4)
2112/* Zero struct from after the field to the end */
2113#define INFO_FL_CLEAR(v4l2_struct, field)			\
2114	((offsetof(struct v4l2_struct, field) +			\
2115	  sizeof(((struct v4l2_struct *)0)->field)) << 16)
2116#define INFO_FL_CLEAR_MASK (_IOC_SIZEMASK << 16)
2117
2118#define IOCTL_INFO_STD(_ioctl, _vidioc, _debug, _flags)			\
2119	[_IOC_NR(_ioctl)] = {						\
2120		.ioctl = _ioctl,					\
2121		.flags = _flags | INFO_FL_STD,				\
2122		.name = #_ioctl,					\
2123		.u.offset = offsetof(struct v4l2_ioctl_ops, _vidioc),	\
2124		.debug = _debug,					\
2125	}
2126
2127#define IOCTL_INFO_FNC(_ioctl, _func, _debug, _flags)			\
2128	[_IOC_NR(_ioctl)] = {						\
2129		.ioctl = _ioctl,					\
2130		.flags = _flags | INFO_FL_FUNC,				\
2131		.name = #_ioctl,					\
2132		.u.func = _func,					\
2133		.debug = _debug,					\
2134	}
2135
2136static struct v4l2_ioctl_info v4l2_ioctls[] = {
2137	IOCTL_INFO_FNC(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
2138	IOCTL_INFO_FNC(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
2139	IOCTL_INFO_FNC(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, INFO_FL_CLEAR(v4l2_format, type)),
2140	IOCTL_INFO_FNC(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
2141	IOCTL_INFO_FNC(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2142	IOCTL_INFO_FNC(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
2143	IOCTL_INFO_STD(VIDIOC_G_FBUF, vidioc_g_fbuf, v4l_print_framebuffer, 0),
2144	IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
2145	IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
2146	IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
2147	IOCTL_INFO_STD(VIDIOC_EXPBUF, vidioc_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
2148	IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2149	IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2150	IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2151	IOCTL_INFO_FNC(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2152	IOCTL_INFO_FNC(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
2153	IOCTL_INFO_STD(VIDIOC_G_STD, vidioc_g_std, v4l_print_std, 0),
2154	IOCTL_INFO_FNC(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2155	IOCTL_INFO_FNC(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
2156	IOCTL_INFO_FNC(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
2157	IOCTL_INFO_FNC(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2158	IOCTL_INFO_FNC(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
2159	IOCTL_INFO_FNC(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2160	IOCTL_INFO_FNC(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
2161	IOCTL_INFO_STD(VIDIOC_G_AUDIO, vidioc_g_audio, v4l_print_audio, 0),
2162	IOCTL_INFO_STD(VIDIOC_S_AUDIO, vidioc_s_audio, v4l_print_audio, INFO_FL_PRIO),
2163	IOCTL_INFO_FNC(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2164	IOCTL_INFO_FNC(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
2165	IOCTL_INFO_STD(VIDIOC_G_INPUT, vidioc_g_input, v4l_print_u32, 0),
2166	IOCTL_INFO_FNC(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
2167	IOCTL_INFO_STD(VIDIOC_G_EDID, vidioc_g_edid, v4l_print_edid, INFO_FL_CLEAR(v4l2_edid, edid)),
2168	IOCTL_INFO_STD(VIDIOC_S_EDID, vidioc_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_edid, edid)),
2169	IOCTL_INFO_STD(VIDIOC_G_OUTPUT, vidioc_g_output, v4l_print_u32, 0),
2170	IOCTL_INFO_FNC(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2171	IOCTL_INFO_FNC(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2172	IOCTL_INFO_STD(VIDIOC_G_AUDOUT, vidioc_g_audout, v4l_print_audioout, 0),
2173	IOCTL_INFO_STD(VIDIOC_S_AUDOUT, vidioc_s_audout, v4l_print_audioout, INFO_FL_PRIO),
2174	IOCTL_INFO_FNC(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
2175	IOCTL_INFO_STD(VIDIOC_S_MODULATOR, vidioc_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2176	IOCTL_INFO_FNC(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2177	IOCTL_INFO_FNC(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
2178	IOCTL_INFO_FNC(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2179	IOCTL_INFO_FNC(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2180	IOCTL_INFO_FNC(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2181	IOCTL_INFO_STD(VIDIOC_G_SELECTION, vidioc_g_selection, v4l_print_selection, 0),
2182	IOCTL_INFO_STD(VIDIOC_S_SELECTION, vidioc_s_selection, v4l_print_selection, INFO_FL_PRIO),
2183	IOCTL_INFO_STD(VIDIOC_G_JPEGCOMP, vidioc_g_jpegcomp, v4l_print_jpegcompression, 0),
2184	IOCTL_INFO_STD(VIDIOC_S_JPEGCOMP, vidioc_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
2185	IOCTL_INFO_FNC(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
2186	IOCTL_INFO_FNC(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
2187	IOCTL_INFO_STD(VIDIOC_ENUMAUDIO, vidioc_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2188	IOCTL_INFO_STD(VIDIOC_ENUMAUDOUT, vidioc_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
2189	IOCTL_INFO_FNC(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2190	IOCTL_INFO_FNC(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
2191	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)),
2192	IOCTL_INFO_FNC(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
2193	IOCTL_INFO_FNC(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2194	IOCTL_INFO_FNC(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
2195	IOCTL_INFO_FNC(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2196	IOCTL_INFO_STD(VIDIOC_ENUM_FRAMESIZES, vidioc_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2197	IOCTL_INFO_STD(VIDIOC_ENUM_FRAMEINTERVALS, vidioc_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
2198	IOCTL_INFO_STD(VIDIOC_G_ENC_INDEX, vidioc_g_enc_index, v4l_print_enc_idx, 0),
2199	IOCTL_INFO_STD(VIDIOC_ENCODER_CMD, vidioc_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2200	IOCTL_INFO_STD(VIDIOC_TRY_ENCODER_CMD, vidioc_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2201	IOCTL_INFO_STD(VIDIOC_DECODER_CMD, vidioc_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2202	IOCTL_INFO_STD(VIDIOC_TRY_DECODER_CMD, vidioc_try_decoder_cmd, v4l_print_decoder_cmd, 0),
2203	IOCTL_INFO_FNC(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2204	IOCTL_INFO_FNC(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2205	IOCTL_INFO_FNC(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
2206	IOCTL_INFO_STD(VIDIOC_S_DV_TIMINGS, vidioc_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO),
2207	IOCTL_INFO_STD(VIDIOC_G_DV_TIMINGS, vidioc_g_dv_timings, v4l_print_dv_timings, 0),
2208	IOCTL_INFO_FNC(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2209	IOCTL_INFO_FNC(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2210	IOCTL_INFO_FNC(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
2211	IOCTL_INFO_FNC(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2212	IOCTL_INFO_FNC(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
2213	IOCTL_INFO_STD(VIDIOC_ENUM_DV_TIMINGS, vidioc_enum_dv_timings, v4l_print_enum_dv_timings, 0),
2214	IOCTL_INFO_STD(VIDIOC_QUERY_DV_TIMINGS, vidioc_query_dv_timings, v4l_print_dv_timings, 0),
2215	IOCTL_INFO_STD(VIDIOC_DV_TIMINGS_CAP, vidioc_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, type)),
2216	IOCTL_INFO_FNC(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
2217	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)),
2218	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)),
2219};
2220#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2221
2222bool v4l2_is_known_ioctl(unsigned int cmd)
2223{
2224	if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2225		return false;
2226	return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2227}
2228
2229struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
2230{
2231	if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2232		return vdev->lock;
2233	if (test_bit(_IOC_NR(cmd), vdev->disable_locking))
2234		return NULL;
2235	if (vdev->queue && vdev->queue->lock &&
2236			(v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2237		return vdev->queue->lock;
2238	return vdev->lock;
2239}
2240
2241/* Common ioctl debug function. This function can be used by
2242   external ioctl messages as well as internal V4L ioctl */
2243void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
2244{
2245	const char *dir, *type;
2246
2247	if (prefix)
2248		printk(KERN_DEBUG "%s: ", prefix);
2249
2250	switch (_IOC_TYPE(cmd)) {
2251	case 'd':
2252		type = "v4l2_int";
2253		break;
2254	case 'V':
2255		if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2256			type = "v4l2";
2257			break;
2258		}
2259		pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
2260		return;
2261	default:
2262		type = "unknown";
2263		break;
2264	}
2265
2266	switch (_IOC_DIR(cmd)) {
2267	case _IOC_NONE:              dir = "--"; break;
2268	case _IOC_READ:              dir = "r-"; break;
2269	case _IOC_WRITE:             dir = "-w"; break;
2270	case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2271	default:                     dir = "*ERR*"; break;
2272	}
2273	pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
2274		type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2275}
2276EXPORT_SYMBOL(v4l_printk_ioctl);
2277
2278static long __video_do_ioctl(struct file *file,
2279		unsigned int cmd, void *arg)
2280{
2281	struct video_device *vfd = video_devdata(file);
2282	const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
2283	bool write_only = false;
2284	struct v4l2_ioctl_info default_info;
2285	const struct v4l2_ioctl_info *info;
2286	void *fh = file->private_data;
2287	struct v4l2_fh *vfh = NULL;
2288	int debug = vfd->debug;
2289	long ret = -ENOTTY;
2290
2291	if (ops == NULL) {
2292		pr_warn("%s: has no ioctl_ops.\n",
2293				video_device_node_name(vfd));
2294		return ret;
2295	}
2296
2297	if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
2298		vfh = file->private_data;
2299
2300	if (v4l2_is_known_ioctl(cmd)) {
2301		info = &v4l2_ioctls[_IOC_NR(cmd)];
2302
2303	        if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2304		    !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
2305			goto done;
2306
2307		if (vfh && (info->flags & INFO_FL_PRIO)) {
2308			ret = v4l2_prio_check(vfd->prio, vfh->prio);
2309			if (ret)
2310				goto done;
2311		}
2312	} else {
2313		default_info.ioctl = cmd;
2314		default_info.flags = 0;
2315		default_info.debug = v4l_print_default;
2316		info = &default_info;
2317	}
2318
2319	write_only = _IOC_DIR(cmd) == _IOC_WRITE;
2320	if (info->flags & INFO_FL_STD) {
2321		typedef int (*vidioc_op)(struct file *file, void *fh, void *p);
2322		const void *p = vfd->ioctl_ops;
2323		const vidioc_op *vidioc = p + info->u.offset;
2324
2325		ret = (*vidioc)(file, fh, arg);
2326	} else if (info->flags & INFO_FL_FUNC) {
2327		ret = info->u.func(ops, file, fh, arg);
2328	} else if (!ops->vidioc_default) {
2329		ret = -ENOTTY;
2330	} else {
2331		ret = ops->vidioc_default(file, fh,
2332			vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2333			cmd, arg);
2334	}
2335
2336done:
2337	if (debug) {
2338		v4l_printk_ioctl(video_device_node_name(vfd), cmd);
2339		if (ret < 0)
2340			pr_cont(": error %ld", ret);
2341		if (debug == V4L2_DEBUG_IOCTL)
2342			pr_cont("\n");
2343		else if (_IOC_DIR(cmd) == _IOC_NONE)
2344			info->debug(arg, write_only);
2345		else {
2346			pr_cont(": ");
2347			info->debug(arg, write_only);
2348		}
2349	}
2350
2351	return ret;
2352}
2353
2354static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2355			    void __user **user_ptr, void ***kernel_ptr)
2356{
2357	int ret = 0;
2358
2359	switch (cmd) {
2360	case VIDIOC_PREPARE_BUF:
2361	case VIDIOC_QUERYBUF:
2362	case VIDIOC_QBUF:
2363	case VIDIOC_DQBUF: {
2364		struct v4l2_buffer *buf = parg;
2365
2366		if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2367			if (buf->length > VIDEO_MAX_PLANES) {
2368				ret = -EINVAL;
2369				break;
2370			}
2371			*user_ptr = (void __user *)buf->m.planes;
2372			*kernel_ptr = (void **)&buf->m.planes;
2373			*array_size = sizeof(struct v4l2_plane) * buf->length;
2374			ret = 1;
2375		}
2376		break;
2377	}
2378
2379	case VIDIOC_G_EDID:
2380	case VIDIOC_S_EDID: {
2381		struct v4l2_edid *edid = parg;
2382
2383		if (edid->blocks) {
2384			if (edid->blocks > 256) {
2385				ret = -EINVAL;
2386				break;
2387			}
2388			*user_ptr = (void __user *)edid->edid;
2389			*kernel_ptr = (void **)&edid->edid;
2390			*array_size = edid->blocks * 128;
2391			ret = 1;
2392		}
2393		break;
2394	}
2395
2396	case VIDIOC_S_EXT_CTRLS:
2397	case VIDIOC_G_EXT_CTRLS:
2398	case VIDIOC_TRY_EXT_CTRLS: {
2399		struct v4l2_ext_controls *ctrls = parg;
2400
2401		if (ctrls->count != 0) {
2402			if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2403				ret = -EINVAL;
2404				break;
2405			}
2406			*user_ptr = (void __user *)ctrls->controls;
2407			*kernel_ptr = (void **)&ctrls->controls;
2408			*array_size = sizeof(struct v4l2_ext_control)
2409				    * ctrls->count;
2410			ret = 1;
2411		}
2412		break;
2413	}
2414	}
2415
2416	return ret;
2417}
2418
2419long
2420video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2421	       v4l2_kioctl func)
2422{
2423	char	sbuf[128];
2424	void    *mbuf = NULL;
2425	void	*parg = (void *)arg;
2426	long	err  = -EINVAL;
2427	bool	has_array_args;
2428	size_t  array_size = 0;
2429	void __user *user_ptr = NULL;
2430	void	**kernel_ptr = NULL;
2431
2432	/*  Copy arguments into temp kernel buffer  */
2433	if (_IOC_DIR(cmd) != _IOC_NONE) {
2434		if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
2435			parg = sbuf;
2436		} else {
2437			/* too big to allocate from stack */
2438			mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
2439			if (NULL == mbuf)
2440				return -ENOMEM;
2441			parg = mbuf;
2442		}
2443
2444		err = -EFAULT;
2445		if (_IOC_DIR(cmd) & _IOC_WRITE) {
2446			unsigned int n = _IOC_SIZE(cmd);
2447
2448			/*
2449			 * In some cases, only a few fields are used as input,
2450			 * i.e. when the app sets "index" and then the driver
2451			 * fills in the rest of the structure for the thing
2452			 * with that index.  We only need to copy up the first
2453			 * non-input field.
2454			 */
2455			if (v4l2_is_known_ioctl(cmd)) {
2456				u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
2457				if (flags & INFO_FL_CLEAR_MASK)
2458					n = (flags & INFO_FL_CLEAR_MASK) >> 16;
2459			}
2460
2461			if (copy_from_user(parg, (void __user *)arg, n))
2462				goto out;
2463
2464			/* zero out anything we don't copy from userspace */
2465			if (n < _IOC_SIZE(cmd))
2466				memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
2467		} else {
2468			/* read-only ioctl */
2469			memset(parg, 0, _IOC_SIZE(cmd));
2470		}
2471	}
2472
2473	err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
2474	if (err < 0)
2475		goto out;
2476	has_array_args = err;
2477
2478	if (has_array_args) {
2479		/*
2480		 * When adding new types of array args, make sure that the
2481		 * parent argument to ioctl (which contains the pointer to the
2482		 * array) fits into sbuf (so that mbuf will still remain
2483		 * unused up to here).
2484		 */
2485		mbuf = kmalloc(array_size, GFP_KERNEL);
2486		err = -ENOMEM;
2487		if (NULL == mbuf)
2488			goto out_array_args;
2489		err = -EFAULT;
2490		if (copy_from_user(mbuf, user_ptr, array_size))
2491			goto out_array_args;
2492		*kernel_ptr = mbuf;
2493	}
2494
2495	/* Handles IOCTL */
2496	err = func(file, cmd, parg);
2497	if (err == -ENOIOCTLCMD)
2498		err = -ENOTTY;
2499	if (err == 0) {
2500		if (cmd == VIDIOC_DQBUF)
2501			trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
2502		else if (cmd == VIDIOC_QBUF)
2503			trace_v4l2_qbuf(video_devdata(file)->minor, parg);
2504	}
2505
2506	if (has_array_args) {
2507		*kernel_ptr = (void __force *)user_ptr;
2508		if (copy_to_user(user_ptr, mbuf, array_size))
2509			err = -EFAULT;
2510		goto out_array_args;
2511	}
2512	/* VIDIOC_QUERY_DV_TIMINGS can return an error, but still have valid
2513	   results that must be returned. */
2514	if (err < 0 && cmd != VIDIOC_QUERY_DV_TIMINGS)
2515		goto out;
2516
2517out_array_args:
2518	/*  Copy results into user buffer  */
2519	switch (_IOC_DIR(cmd)) {
2520	case _IOC_READ:
2521	case (_IOC_WRITE | _IOC_READ):
2522		if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
2523			err = -EFAULT;
2524		break;
2525	}
2526
2527out:
2528	kfree(mbuf);
2529	return err;
2530}
2531EXPORT_SYMBOL(video_usercopy);
2532
2533long video_ioctl2(struct file *file,
2534	       unsigned int cmd, unsigned long arg)
2535{
2536	return video_usercopy(file, cmd, arg, __video_do_ioctl);
2537}
2538EXPORT_SYMBOL(video_ioctl2);
2539