xref: /linux/drivers/media/v4l2-core/v4l2-ioctl.c (revision ec63e2a4897075e427c121d863bd89c44578094f)
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@kernel.org> (version 2)
13  */
14 
15 #include <linux/mm.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/version.h>
21 
22 #include <linux/videodev2.h>
23 
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-fh.h>
28 #include <media/v4l2-event.h>
29 #include <media/v4l2-device.h>
30 #include <media/videobuf2-v4l2.h>
31 #include <media/v4l2-mc.h>
32 #include <media/v4l2-mem2mem.h>
33 
34 #include <trace/events/v4l2.h>
35 
36 /* Zero out the end of the struct pointed to by p.  Everything after, but
37  * not including, the specified field is cleared. */
38 #define CLEAR_AFTER_FIELD(p, field) \
39 	memset((u8 *)(p) + offsetof(typeof(*(p)), field) + sizeof((p)->field), \
40 	0, sizeof(*(p)) - offsetof(typeof(*(p)), field) - sizeof((p)->field))
41 
42 #define is_valid_ioctl(vfd, cmd) test_bit(_IOC_NR(cmd), (vfd)->valid_ioctls)
43 
44 struct std_descr {
45 	v4l2_std_id std;
46 	const char *descr;
47 };
48 
49 static const struct std_descr standards[] = {
50 	{ V4L2_STD_NTSC,	"NTSC"      },
51 	{ V4L2_STD_NTSC_M,	"NTSC-M"    },
52 	{ V4L2_STD_NTSC_M_JP,	"NTSC-M-JP" },
53 	{ V4L2_STD_NTSC_M_KR,	"NTSC-M-KR" },
54 	{ V4L2_STD_NTSC_443,	"NTSC-443"  },
55 	{ V4L2_STD_PAL,		"PAL"       },
56 	{ V4L2_STD_PAL_BG,	"PAL-BG"    },
57 	{ V4L2_STD_PAL_B,	"PAL-B"     },
58 	{ V4L2_STD_PAL_B1,	"PAL-B1"    },
59 	{ V4L2_STD_PAL_G,	"PAL-G"     },
60 	{ V4L2_STD_PAL_H,	"PAL-H"     },
61 	{ V4L2_STD_PAL_I,	"PAL-I"     },
62 	{ V4L2_STD_PAL_DK,	"PAL-DK"    },
63 	{ V4L2_STD_PAL_D,	"PAL-D"     },
64 	{ V4L2_STD_PAL_D1,	"PAL-D1"    },
65 	{ V4L2_STD_PAL_K,	"PAL-K"     },
66 	{ V4L2_STD_PAL_M,	"PAL-M"     },
67 	{ V4L2_STD_PAL_N,	"PAL-N"     },
68 	{ V4L2_STD_PAL_Nc,	"PAL-Nc"    },
69 	{ V4L2_STD_PAL_60,	"PAL-60"    },
70 	{ V4L2_STD_SECAM,	"SECAM"     },
71 	{ V4L2_STD_SECAM_B,	"SECAM-B"   },
72 	{ V4L2_STD_SECAM_G,	"SECAM-G"   },
73 	{ V4L2_STD_SECAM_H,	"SECAM-H"   },
74 	{ V4L2_STD_SECAM_DK,	"SECAM-DK"  },
75 	{ V4L2_STD_SECAM_D,	"SECAM-D"   },
76 	{ V4L2_STD_SECAM_K,	"SECAM-K"   },
77 	{ V4L2_STD_SECAM_K1,	"SECAM-K1"  },
78 	{ V4L2_STD_SECAM_L,	"SECAM-L"   },
79 	{ V4L2_STD_SECAM_LC,	"SECAM-Lc"  },
80 	{ 0,			"Unknown"   }
81 };
82 
83 /* video4linux standard ID conversion to standard name
84  */
85 const char *v4l2_norm_to_name(v4l2_std_id id)
86 {
87 	u32 myid = id;
88 	int i;
89 
90 	/* HACK: ppc32 architecture doesn't have __ucmpdi2 function to handle
91 	   64 bit comparisons. So, on that architecture, with some gcc
92 	   variants, compilation fails. Currently, the max value is 30bit wide.
93 	 */
94 	BUG_ON(myid != id);
95 
96 	for (i = 0; standards[i].std; i++)
97 		if (myid == standards[i].std)
98 			break;
99 	return standards[i].descr;
100 }
101 EXPORT_SYMBOL(v4l2_norm_to_name);
102 
103 /* Returns frame period for the given standard */
104 void v4l2_video_std_frame_period(int id, struct v4l2_fract *frameperiod)
105 {
106 	if (id & V4L2_STD_525_60) {
107 		frameperiod->numerator = 1001;
108 		frameperiod->denominator = 30000;
109 	} else {
110 		frameperiod->numerator = 1;
111 		frameperiod->denominator = 25;
112 	}
113 }
114 EXPORT_SYMBOL(v4l2_video_std_frame_period);
115 
116 /* Fill in the fields of a v4l2_standard structure according to the
117    'id' and 'transmission' parameters.  Returns negative on error.  */
118 int v4l2_video_std_construct(struct v4l2_standard *vs,
119 			     int id, const char *name)
120 {
121 	vs->id = id;
122 	v4l2_video_std_frame_period(id, &vs->frameperiod);
123 	vs->framelines = (id & V4L2_STD_525_60) ? 525 : 625;
124 	strscpy(vs->name, name, sizeof(vs->name));
125 	return 0;
126 }
127 EXPORT_SYMBOL(v4l2_video_std_construct);
128 
129 /* Fill in the fields of a v4l2_standard structure according to the
130  * 'id' and 'vs->index' parameters. Returns negative on error. */
131 int v4l_video_std_enumstd(struct v4l2_standard *vs, v4l2_std_id id)
132 {
133 	v4l2_std_id curr_id = 0;
134 	unsigned int index = vs->index, i, j = 0;
135 	const char *descr = "";
136 
137 	/* Return -ENODATA if the id for the current input
138 	   or output is 0, meaning that it doesn't support this API. */
139 	if (id == 0)
140 		return -ENODATA;
141 
142 	/* Return norm array in a canonical way */
143 	for (i = 0; i <= index && id; i++) {
144 		/* last std value in the standards array is 0, so this
145 		   while always ends there since (id & 0) == 0. */
146 		while ((id & standards[j].std) != standards[j].std)
147 			j++;
148 		curr_id = standards[j].std;
149 		descr = standards[j].descr;
150 		j++;
151 		if (curr_id == 0)
152 			break;
153 		if (curr_id != V4L2_STD_PAL &&
154 				curr_id != V4L2_STD_SECAM &&
155 				curr_id != V4L2_STD_NTSC)
156 			id &= ~curr_id;
157 	}
158 	if (i <= index)
159 		return -EINVAL;
160 
161 	v4l2_video_std_construct(vs, curr_id, descr);
162 	return 0;
163 }
164 
165 /* ----------------------------------------------------------------- */
166 /* some arrays for pretty-printing debug messages of enum types      */
167 
168 const char *v4l2_field_names[] = {
169 	[V4L2_FIELD_ANY]        = "any",
170 	[V4L2_FIELD_NONE]       = "none",
171 	[V4L2_FIELD_TOP]        = "top",
172 	[V4L2_FIELD_BOTTOM]     = "bottom",
173 	[V4L2_FIELD_INTERLACED] = "interlaced",
174 	[V4L2_FIELD_SEQ_TB]     = "seq-tb",
175 	[V4L2_FIELD_SEQ_BT]     = "seq-bt",
176 	[V4L2_FIELD_ALTERNATE]  = "alternate",
177 	[V4L2_FIELD_INTERLACED_TB] = "interlaced-tb",
178 	[V4L2_FIELD_INTERLACED_BT] = "interlaced-bt",
179 };
180 EXPORT_SYMBOL(v4l2_field_names);
181 
182 const char *v4l2_type_names[] = {
183 	[0]				   = "0",
184 	[V4L2_BUF_TYPE_VIDEO_CAPTURE]      = "vid-cap",
185 	[V4L2_BUF_TYPE_VIDEO_OVERLAY]      = "vid-overlay",
186 	[V4L2_BUF_TYPE_VIDEO_OUTPUT]       = "vid-out",
187 	[V4L2_BUF_TYPE_VBI_CAPTURE]        = "vbi-cap",
188 	[V4L2_BUF_TYPE_VBI_OUTPUT]         = "vbi-out",
189 	[V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
190 	[V4L2_BUF_TYPE_SLICED_VBI_OUTPUT]  = "sliced-vbi-out",
191 	[V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY] = "vid-out-overlay",
192 	[V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE] = "vid-cap-mplane",
193 	[V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE] = "vid-out-mplane",
194 	[V4L2_BUF_TYPE_SDR_CAPTURE]        = "sdr-cap",
195 	[V4L2_BUF_TYPE_SDR_OUTPUT]         = "sdr-out",
196 	[V4L2_BUF_TYPE_META_CAPTURE]       = "meta-cap",
197 	[V4L2_BUF_TYPE_META_OUTPUT]	   = "meta-out",
198 };
199 EXPORT_SYMBOL(v4l2_type_names);
200 
201 static const char *v4l2_memory_names[] = {
202 	[V4L2_MEMORY_MMAP]    = "mmap",
203 	[V4L2_MEMORY_USERPTR] = "userptr",
204 	[V4L2_MEMORY_OVERLAY] = "overlay",
205 	[V4L2_MEMORY_DMABUF] = "dmabuf",
206 };
207 
208 #define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
209 
210 /* ------------------------------------------------------------------ */
211 /* debug help functions                                               */
212 
213 static void v4l_print_querycap(const void *arg, bool write_only)
214 {
215 	const struct v4l2_capability *p = arg;
216 
217 	pr_cont("driver=%.*s, card=%.*s, bus=%.*s, version=0x%08x, capabilities=0x%08x, device_caps=0x%08x\n",
218 		(int)sizeof(p->driver), p->driver,
219 		(int)sizeof(p->card), p->card,
220 		(int)sizeof(p->bus_info), p->bus_info,
221 		p->version, p->capabilities, p->device_caps);
222 }
223 
224 static void v4l_print_enuminput(const void *arg, bool write_only)
225 {
226 	const struct v4l2_input *p = arg;
227 
228 	pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, tuner=%u, std=0x%08Lx, status=0x%x, capabilities=0x%x\n",
229 		p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
230 		p->tuner, (unsigned long long)p->std, p->status,
231 		p->capabilities);
232 }
233 
234 static void v4l_print_enumoutput(const void *arg, bool write_only)
235 {
236 	const struct v4l2_output *p = arg;
237 
238 	pr_cont("index=%u, name=%.*s, type=%u, audioset=0x%x, modulator=%u, std=0x%08Lx, capabilities=0x%x\n",
239 		p->index, (int)sizeof(p->name), p->name, p->type, p->audioset,
240 		p->modulator, (unsigned long long)p->std, p->capabilities);
241 }
242 
243 static void v4l_print_audio(const void *arg, bool write_only)
244 {
245 	const struct v4l2_audio *p = arg;
246 
247 	if (write_only)
248 		pr_cont("index=%u, mode=0x%x\n", p->index, p->mode);
249 	else
250 		pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
251 			p->index, (int)sizeof(p->name), p->name,
252 			p->capability, p->mode);
253 }
254 
255 static void v4l_print_audioout(const void *arg, bool write_only)
256 {
257 	const struct v4l2_audioout *p = arg;
258 
259 	if (write_only)
260 		pr_cont("index=%u\n", p->index);
261 	else
262 		pr_cont("index=%u, name=%.*s, capability=0x%x, mode=0x%x\n",
263 			p->index, (int)sizeof(p->name), p->name,
264 			p->capability, p->mode);
265 }
266 
267 static void v4l_print_fmtdesc(const void *arg, bool write_only)
268 {
269 	const struct v4l2_fmtdesc *p = arg;
270 
271 	pr_cont("index=%u, type=%s, flags=0x%x, pixelformat=%c%c%c%c, description='%.*s'\n",
272 		p->index, prt_names(p->type, v4l2_type_names),
273 		p->flags, (p->pixelformat & 0xff),
274 		(p->pixelformat >>  8) & 0xff,
275 		(p->pixelformat >> 16) & 0xff,
276 		(p->pixelformat >> 24) & 0xff,
277 		(int)sizeof(p->description), p->description);
278 }
279 
280 static void v4l_print_format(const void *arg, bool write_only)
281 {
282 	const struct v4l2_format *p = arg;
283 	const struct v4l2_pix_format *pix;
284 	const struct v4l2_pix_format_mplane *mp;
285 	const struct v4l2_vbi_format *vbi;
286 	const struct v4l2_sliced_vbi_format *sliced;
287 	const struct v4l2_window *win;
288 	const struct v4l2_sdr_format *sdr;
289 	const struct v4l2_meta_format *meta;
290 	u32 planes;
291 	unsigned i;
292 
293 	pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
294 	switch (p->type) {
295 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
296 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
297 		pix = &p->fmt.pix;
298 		pr_cont(", width=%u, height=%u, pixelformat=%c%c%c%c, field=%s, bytesperline=%u, sizeimage=%u, colorspace=%d, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
299 			pix->width, pix->height,
300 			(pix->pixelformat & 0xff),
301 			(pix->pixelformat >>  8) & 0xff,
302 			(pix->pixelformat >> 16) & 0xff,
303 			(pix->pixelformat >> 24) & 0xff,
304 			prt_names(pix->field, v4l2_field_names),
305 			pix->bytesperline, pix->sizeimage,
306 			pix->colorspace, pix->flags, pix->ycbcr_enc,
307 			pix->quantization, pix->xfer_func);
308 		break;
309 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
310 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
311 		mp = &p->fmt.pix_mp;
312 		pr_cont(", width=%u, height=%u, format=%c%c%c%c, field=%s, colorspace=%d, num_planes=%u, flags=0x%x, ycbcr_enc=%u, quantization=%u, xfer_func=%u\n",
313 			mp->width, mp->height,
314 			(mp->pixelformat & 0xff),
315 			(mp->pixelformat >>  8) & 0xff,
316 			(mp->pixelformat >> 16) & 0xff,
317 			(mp->pixelformat >> 24) & 0xff,
318 			prt_names(mp->field, v4l2_field_names),
319 			mp->colorspace, mp->num_planes, mp->flags,
320 			mp->ycbcr_enc, mp->quantization, mp->xfer_func);
321 		planes = min_t(u32, mp->num_planes, VIDEO_MAX_PLANES);
322 		for (i = 0; i < planes; i++)
323 			printk(KERN_DEBUG "plane %u: bytesperline=%u sizeimage=%u\n", i,
324 					mp->plane_fmt[i].bytesperline,
325 					mp->plane_fmt[i].sizeimage);
326 		break;
327 	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
328 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
329 		win = &p->fmt.win;
330 		/* Note: we can't print the clip list here since the clips
331 		 * pointer is a userspace pointer, not a kernelspace
332 		 * pointer. */
333 		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",
334 			win->w.width, win->w.height, win->w.left, win->w.top,
335 			prt_names(win->field, v4l2_field_names),
336 			win->chromakey, win->clipcount, win->clips,
337 			win->bitmap, win->global_alpha);
338 		break;
339 	case V4L2_BUF_TYPE_VBI_CAPTURE:
340 	case V4L2_BUF_TYPE_VBI_OUTPUT:
341 		vbi = &p->fmt.vbi;
342 		pr_cont(", sampling_rate=%u, offset=%u, samples_per_line=%u, sample_format=%c%c%c%c, start=%u,%u, count=%u,%u\n",
343 			vbi->sampling_rate, vbi->offset,
344 			vbi->samples_per_line,
345 			(vbi->sample_format & 0xff),
346 			(vbi->sample_format >>  8) & 0xff,
347 			(vbi->sample_format >> 16) & 0xff,
348 			(vbi->sample_format >> 24) & 0xff,
349 			vbi->start[0], vbi->start[1],
350 			vbi->count[0], vbi->count[1]);
351 		break;
352 	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
353 	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
354 		sliced = &p->fmt.sliced;
355 		pr_cont(", service_set=0x%08x, io_size=%d\n",
356 				sliced->service_set, sliced->io_size);
357 		for (i = 0; i < 24; i++)
358 			printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
359 				sliced->service_lines[0][i],
360 				sliced->service_lines[1][i]);
361 		break;
362 	case V4L2_BUF_TYPE_SDR_CAPTURE:
363 	case V4L2_BUF_TYPE_SDR_OUTPUT:
364 		sdr = &p->fmt.sdr;
365 		pr_cont(", pixelformat=%c%c%c%c\n",
366 			(sdr->pixelformat >>  0) & 0xff,
367 			(sdr->pixelformat >>  8) & 0xff,
368 			(sdr->pixelformat >> 16) & 0xff,
369 			(sdr->pixelformat >> 24) & 0xff);
370 		break;
371 	case V4L2_BUF_TYPE_META_CAPTURE:
372 	case V4L2_BUF_TYPE_META_OUTPUT:
373 		meta = &p->fmt.meta;
374 		pr_cont(", dataformat=%c%c%c%c, buffersize=%u\n",
375 			(meta->dataformat >>  0) & 0xff,
376 			(meta->dataformat >>  8) & 0xff,
377 			(meta->dataformat >> 16) & 0xff,
378 			(meta->dataformat >> 24) & 0xff,
379 			meta->buffersize);
380 		break;
381 	}
382 }
383 
384 static void v4l_print_framebuffer(const void *arg, bool write_only)
385 {
386 	const struct v4l2_framebuffer *p = arg;
387 
388 	pr_cont("capability=0x%x, flags=0x%x, base=0x%p, width=%u, height=%u, pixelformat=%c%c%c%c, bytesperline=%u, sizeimage=%u, colorspace=%d\n",
389 			p->capability, p->flags, p->base,
390 			p->fmt.width, p->fmt.height,
391 			(p->fmt.pixelformat & 0xff),
392 			(p->fmt.pixelformat >>  8) & 0xff,
393 			(p->fmt.pixelformat >> 16) & 0xff,
394 			(p->fmt.pixelformat >> 24) & 0xff,
395 			p->fmt.bytesperline, p->fmt.sizeimage,
396 			p->fmt.colorspace);
397 }
398 
399 static void v4l_print_buftype(const void *arg, bool write_only)
400 {
401 	pr_cont("type=%s\n", prt_names(*(u32 *)arg, v4l2_type_names));
402 }
403 
404 static void v4l_print_modulator(const void *arg, bool write_only)
405 {
406 	const struct v4l2_modulator *p = arg;
407 
408 	if (write_only)
409 		pr_cont("index=%u, txsubchans=0x%x\n", p->index, p->txsubchans);
410 	else
411 		pr_cont("index=%u, name=%.*s, capability=0x%x, rangelow=%u, rangehigh=%u, txsubchans=0x%x\n",
412 			p->index, (int)sizeof(p->name), p->name, p->capability,
413 			p->rangelow, p->rangehigh, p->txsubchans);
414 }
415 
416 static void v4l_print_tuner(const void *arg, bool write_only)
417 {
418 	const struct v4l2_tuner *p = arg;
419 
420 	if (write_only)
421 		pr_cont("index=%u, audmode=%u\n", p->index, p->audmode);
422 	else
423 		pr_cont("index=%u, name=%.*s, type=%u, capability=0x%x, rangelow=%u, rangehigh=%u, signal=%u, afc=%d, rxsubchans=0x%x, audmode=%u\n",
424 			p->index, (int)sizeof(p->name), p->name, p->type,
425 			p->capability, p->rangelow,
426 			p->rangehigh, p->signal, p->afc,
427 			p->rxsubchans, p->audmode);
428 }
429 
430 static void v4l_print_frequency(const void *arg, bool write_only)
431 {
432 	const struct v4l2_frequency *p = arg;
433 
434 	pr_cont("tuner=%u, type=%u, frequency=%u\n",
435 				p->tuner, p->type, p->frequency);
436 }
437 
438 static void v4l_print_standard(const void *arg, bool write_only)
439 {
440 	const struct v4l2_standard *p = arg;
441 
442 	pr_cont("index=%u, id=0x%Lx, name=%.*s, fps=%u/%u, framelines=%u\n",
443 		p->index,
444 		(unsigned long long)p->id, (int)sizeof(p->name), p->name,
445 		p->frameperiod.numerator,
446 		p->frameperiod.denominator,
447 		p->framelines);
448 }
449 
450 static void v4l_print_std(const void *arg, bool write_only)
451 {
452 	pr_cont("std=0x%08Lx\n", *(const long long unsigned *)arg);
453 }
454 
455 static void v4l_print_hw_freq_seek(const void *arg, bool write_only)
456 {
457 	const struct v4l2_hw_freq_seek *p = arg;
458 
459 	pr_cont("tuner=%u, type=%u, seek_upward=%u, wrap_around=%u, spacing=%u, rangelow=%u, rangehigh=%u\n",
460 		p->tuner, p->type, p->seek_upward, p->wrap_around, p->spacing,
461 		p->rangelow, p->rangehigh);
462 }
463 
464 static void v4l_print_requestbuffers(const void *arg, bool write_only)
465 {
466 	const struct v4l2_requestbuffers *p = arg;
467 
468 	pr_cont("count=%d, type=%s, memory=%s\n",
469 		p->count,
470 		prt_names(p->type, v4l2_type_names),
471 		prt_names(p->memory, v4l2_memory_names));
472 }
473 
474 static void v4l_print_buffer(const void *arg, bool write_only)
475 {
476 	const struct v4l2_buffer *p = arg;
477 	const struct v4l2_timecode *tc = &p->timecode;
478 	const struct v4l2_plane *plane;
479 	int i;
480 
481 	pr_cont("%02ld:%02d:%02d.%08ld index=%d, type=%s, request_fd=%d, flags=0x%08x, field=%s, sequence=%d, memory=%s",
482 			p->timestamp.tv_sec / 3600,
483 			(int)(p->timestamp.tv_sec / 60) % 60,
484 			(int)(p->timestamp.tv_sec % 60),
485 			(long)p->timestamp.tv_usec,
486 			p->index,
487 			prt_names(p->type, v4l2_type_names), p->request_fd,
488 			p->flags, prt_names(p->field, v4l2_field_names),
489 			p->sequence, prt_names(p->memory, v4l2_memory_names));
490 
491 	if (V4L2_TYPE_IS_MULTIPLANAR(p->type) && p->m.planes) {
492 		pr_cont("\n");
493 		for (i = 0; i < p->length; ++i) {
494 			plane = &p->m.planes[i];
495 			printk(KERN_DEBUG
496 				"plane %d: bytesused=%d, data_offset=0x%08x, offset/userptr=0x%lx, length=%d\n",
497 				i, plane->bytesused, plane->data_offset,
498 				plane->m.userptr, plane->length);
499 		}
500 	} else {
501 		pr_cont(", bytesused=%d, offset/userptr=0x%lx, length=%d\n",
502 			p->bytesused, p->m.userptr, p->length);
503 	}
504 
505 	printk(KERN_DEBUG "timecode=%02d:%02d:%02d type=%d, flags=0x%08x, frames=%d, userbits=0x%08x\n",
506 			tc->hours, tc->minutes, tc->seconds,
507 			tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
508 }
509 
510 static void v4l_print_exportbuffer(const void *arg, bool write_only)
511 {
512 	const struct v4l2_exportbuffer *p = arg;
513 
514 	pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
515 		p->fd, prt_names(p->type, v4l2_type_names),
516 		p->index, p->plane, p->flags);
517 }
518 
519 static void v4l_print_create_buffers(const void *arg, bool write_only)
520 {
521 	const struct v4l2_create_buffers *p = arg;
522 
523 	pr_cont("index=%d, count=%d, memory=%s, ",
524 			p->index, p->count,
525 			prt_names(p->memory, v4l2_memory_names));
526 	v4l_print_format(&p->format, write_only);
527 }
528 
529 static void v4l_print_streamparm(const void *arg, bool write_only)
530 {
531 	const struct v4l2_streamparm *p = arg;
532 
533 	pr_cont("type=%s", prt_names(p->type, v4l2_type_names));
534 
535 	if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
536 	    p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
537 		const struct v4l2_captureparm *c = &p->parm.capture;
538 
539 		pr_cont(", capability=0x%x, capturemode=0x%x, timeperframe=%d/%d, extendedmode=%d, readbuffers=%d\n",
540 			c->capability, c->capturemode,
541 			c->timeperframe.numerator, c->timeperframe.denominator,
542 			c->extendedmode, c->readbuffers);
543 	} else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
544 		   p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
545 		const struct v4l2_outputparm *c = &p->parm.output;
546 
547 		pr_cont(", capability=0x%x, outputmode=0x%x, timeperframe=%d/%d, extendedmode=%d, writebuffers=%d\n",
548 			c->capability, c->outputmode,
549 			c->timeperframe.numerator, c->timeperframe.denominator,
550 			c->extendedmode, c->writebuffers);
551 	} else {
552 		pr_cont("\n");
553 	}
554 }
555 
556 static void v4l_print_queryctrl(const void *arg, bool write_only)
557 {
558 	const struct v4l2_queryctrl *p = arg;
559 
560 	pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%d/%d, step=%d, default=%d, flags=0x%08x\n",
561 			p->id, p->type, (int)sizeof(p->name), p->name,
562 			p->minimum, p->maximum,
563 			p->step, p->default_value, p->flags);
564 }
565 
566 static void v4l_print_query_ext_ctrl(const void *arg, bool write_only)
567 {
568 	const struct v4l2_query_ext_ctrl *p = arg;
569 
570 	pr_cont("id=0x%x, type=%d, name=%.*s, min/max=%lld/%lld, step=%lld, default=%lld, flags=0x%08x, elem_size=%u, elems=%u, nr_of_dims=%u, dims=%u,%u,%u,%u\n",
571 			p->id, p->type, (int)sizeof(p->name), p->name,
572 			p->minimum, p->maximum,
573 			p->step, p->default_value, p->flags,
574 			p->elem_size, p->elems, p->nr_of_dims,
575 			p->dims[0], p->dims[1], p->dims[2], p->dims[3]);
576 }
577 
578 static void v4l_print_querymenu(const void *arg, bool write_only)
579 {
580 	const struct v4l2_querymenu *p = arg;
581 
582 	pr_cont("id=0x%x, index=%d\n", p->id, p->index);
583 }
584 
585 static void v4l_print_control(const void *arg, bool write_only)
586 {
587 	const struct v4l2_control *p = arg;
588 
589 	pr_cont("id=0x%x, value=%d\n", p->id, p->value);
590 }
591 
592 static void v4l_print_ext_controls(const void *arg, bool write_only)
593 {
594 	const struct v4l2_ext_controls *p = arg;
595 	int i;
596 
597 	pr_cont("which=0x%x, count=%d, error_idx=%d, request_fd=%d",
598 			p->which, p->count, p->error_idx, p->request_fd);
599 	for (i = 0; i < p->count; i++) {
600 		if (!p->controls[i].size)
601 			pr_cont(", id/val=0x%x/0x%x",
602 				p->controls[i].id, p->controls[i].value);
603 		else
604 			pr_cont(", id/size=0x%x/%u",
605 				p->controls[i].id, p->controls[i].size);
606 	}
607 	pr_cont("\n");
608 }
609 
610 static void v4l_print_cropcap(const void *arg, bool write_only)
611 {
612 	const struct v4l2_cropcap *p = arg;
613 
614 	pr_cont("type=%s, bounds wxh=%dx%d, x,y=%d,%d, defrect wxh=%dx%d, x,y=%d,%d, pixelaspect %d/%d\n",
615 		prt_names(p->type, v4l2_type_names),
616 		p->bounds.width, p->bounds.height,
617 		p->bounds.left, p->bounds.top,
618 		p->defrect.width, p->defrect.height,
619 		p->defrect.left, p->defrect.top,
620 		p->pixelaspect.numerator, p->pixelaspect.denominator);
621 }
622 
623 static void v4l_print_crop(const void *arg, bool write_only)
624 {
625 	const struct v4l2_crop *p = arg;
626 
627 	pr_cont("type=%s, wxh=%dx%d, x,y=%d,%d\n",
628 		prt_names(p->type, v4l2_type_names),
629 		p->c.width, p->c.height,
630 		p->c.left, p->c.top);
631 }
632 
633 static void v4l_print_selection(const void *arg, bool write_only)
634 {
635 	const struct v4l2_selection *p = arg;
636 
637 	pr_cont("type=%s, target=%d, flags=0x%x, wxh=%dx%d, x,y=%d,%d\n",
638 		prt_names(p->type, v4l2_type_names),
639 		p->target, p->flags,
640 		p->r.width, p->r.height, p->r.left, p->r.top);
641 }
642 
643 static void v4l_print_jpegcompression(const void *arg, bool write_only)
644 {
645 	const struct v4l2_jpegcompression *p = arg;
646 
647 	pr_cont("quality=%d, APPn=%d, APP_len=%d, COM_len=%d, jpeg_markers=0x%x\n",
648 		p->quality, p->APPn, p->APP_len,
649 		p->COM_len, p->jpeg_markers);
650 }
651 
652 static void v4l_print_enc_idx(const void *arg, bool write_only)
653 {
654 	const struct v4l2_enc_idx *p = arg;
655 
656 	pr_cont("entries=%d, entries_cap=%d\n",
657 			p->entries, p->entries_cap);
658 }
659 
660 static void v4l_print_encoder_cmd(const void *arg, bool write_only)
661 {
662 	const struct v4l2_encoder_cmd *p = arg;
663 
664 	pr_cont("cmd=%d, flags=0x%x\n",
665 			p->cmd, p->flags);
666 }
667 
668 static void v4l_print_decoder_cmd(const void *arg, bool write_only)
669 {
670 	const struct v4l2_decoder_cmd *p = arg;
671 
672 	pr_cont("cmd=%d, flags=0x%x\n", p->cmd, p->flags);
673 
674 	if (p->cmd == V4L2_DEC_CMD_START)
675 		pr_info("speed=%d, format=%u\n",
676 				p->start.speed, p->start.format);
677 	else if (p->cmd == V4L2_DEC_CMD_STOP)
678 		pr_info("pts=%llu\n", p->stop.pts);
679 }
680 
681 static void v4l_print_dbg_chip_info(const void *arg, bool write_only)
682 {
683 	const struct v4l2_dbg_chip_info *p = arg;
684 
685 	pr_cont("type=%u, ", p->match.type);
686 	if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
687 		pr_cont("name=%.*s, ",
688 				(int)sizeof(p->match.name), p->match.name);
689 	else
690 		pr_cont("addr=%u, ", p->match.addr);
691 	pr_cont("name=%.*s\n", (int)sizeof(p->name), p->name);
692 }
693 
694 static void v4l_print_dbg_register(const void *arg, bool write_only)
695 {
696 	const struct v4l2_dbg_register *p = arg;
697 
698 	pr_cont("type=%u, ", p->match.type);
699 	if (p->match.type == V4L2_CHIP_MATCH_I2C_DRIVER)
700 		pr_cont("name=%.*s, ",
701 				(int)sizeof(p->match.name), p->match.name);
702 	else
703 		pr_cont("addr=%u, ", p->match.addr);
704 	pr_cont("reg=0x%llx, val=0x%llx\n",
705 			p->reg, p->val);
706 }
707 
708 static void v4l_print_dv_timings(const void *arg, bool write_only)
709 {
710 	const struct v4l2_dv_timings *p = arg;
711 
712 	switch (p->type) {
713 	case V4L2_DV_BT_656_1120:
714 		pr_cont("type=bt-656/1120, interlaced=%u, pixelclock=%llu, width=%u, height=%u, polarities=0x%x, hfrontporch=%u, hsync=%u, hbackporch=%u, vfrontporch=%u, vsync=%u, vbackporch=%u, il_vfrontporch=%u, il_vsync=%u, il_vbackporch=%u, standards=0x%x, flags=0x%x\n",
715 				p->bt.interlaced, p->bt.pixelclock,
716 				p->bt.width, p->bt.height,
717 				p->bt.polarities, p->bt.hfrontporch,
718 				p->bt.hsync, p->bt.hbackporch,
719 				p->bt.vfrontporch, p->bt.vsync,
720 				p->bt.vbackporch, p->bt.il_vfrontporch,
721 				p->bt.il_vsync, p->bt.il_vbackporch,
722 				p->bt.standards, p->bt.flags);
723 		break;
724 	default:
725 		pr_cont("type=%d\n", p->type);
726 		break;
727 	}
728 }
729 
730 static void v4l_print_enum_dv_timings(const void *arg, bool write_only)
731 {
732 	const struct v4l2_enum_dv_timings *p = arg;
733 
734 	pr_cont("index=%u, ", p->index);
735 	v4l_print_dv_timings(&p->timings, write_only);
736 }
737 
738 static void v4l_print_dv_timings_cap(const void *arg, bool write_only)
739 {
740 	const struct v4l2_dv_timings_cap *p = arg;
741 
742 	switch (p->type) {
743 	case V4L2_DV_BT_656_1120:
744 		pr_cont("type=bt-656/1120, width=%u-%u, height=%u-%u, pixelclock=%llu-%llu, standards=0x%x, capabilities=0x%x\n",
745 			p->bt.min_width, p->bt.max_width,
746 			p->bt.min_height, p->bt.max_height,
747 			p->bt.min_pixelclock, p->bt.max_pixelclock,
748 			p->bt.standards, p->bt.capabilities);
749 		break;
750 	default:
751 		pr_cont("type=%u\n", p->type);
752 		break;
753 	}
754 }
755 
756 static void v4l_print_frmsizeenum(const void *arg, bool write_only)
757 {
758 	const struct v4l2_frmsizeenum *p = arg;
759 
760 	pr_cont("index=%u, pixelformat=%c%c%c%c, type=%u",
761 			p->index,
762 			(p->pixel_format & 0xff),
763 			(p->pixel_format >>  8) & 0xff,
764 			(p->pixel_format >> 16) & 0xff,
765 			(p->pixel_format >> 24) & 0xff,
766 			p->type);
767 	switch (p->type) {
768 	case V4L2_FRMSIZE_TYPE_DISCRETE:
769 		pr_cont(", wxh=%ux%u\n",
770 			p->discrete.width, p->discrete.height);
771 		break;
772 	case V4L2_FRMSIZE_TYPE_STEPWISE:
773 		pr_cont(", min=%ux%u, max=%ux%u, step=%ux%u\n",
774 				p->stepwise.min_width,
775 				p->stepwise.min_height,
776 				p->stepwise.max_width,
777 				p->stepwise.max_height,
778 				p->stepwise.step_width,
779 				p->stepwise.step_height);
780 		break;
781 	case V4L2_FRMSIZE_TYPE_CONTINUOUS:
782 		/* fall through */
783 	default:
784 		pr_cont("\n");
785 		break;
786 	}
787 }
788 
789 static void v4l_print_frmivalenum(const void *arg, bool write_only)
790 {
791 	const struct v4l2_frmivalenum *p = arg;
792 
793 	pr_cont("index=%u, pixelformat=%c%c%c%c, wxh=%ux%u, type=%u",
794 			p->index,
795 			(p->pixel_format & 0xff),
796 			(p->pixel_format >>  8) & 0xff,
797 			(p->pixel_format >> 16) & 0xff,
798 			(p->pixel_format >> 24) & 0xff,
799 			p->width, p->height, p->type);
800 	switch (p->type) {
801 	case V4L2_FRMIVAL_TYPE_DISCRETE:
802 		pr_cont(", fps=%d/%d\n",
803 				p->discrete.numerator,
804 				p->discrete.denominator);
805 		break;
806 	case V4L2_FRMIVAL_TYPE_STEPWISE:
807 		pr_cont(", min=%d/%d, max=%d/%d, step=%d/%d\n",
808 				p->stepwise.min.numerator,
809 				p->stepwise.min.denominator,
810 				p->stepwise.max.numerator,
811 				p->stepwise.max.denominator,
812 				p->stepwise.step.numerator,
813 				p->stepwise.step.denominator);
814 		break;
815 	case V4L2_FRMIVAL_TYPE_CONTINUOUS:
816 		/* fall through */
817 	default:
818 		pr_cont("\n");
819 		break;
820 	}
821 }
822 
823 static void v4l_print_event(const void *arg, bool write_only)
824 {
825 	const struct v4l2_event *p = arg;
826 	const struct v4l2_event_ctrl *c;
827 
828 	pr_cont("type=0x%x, pending=%u, sequence=%u, id=%u, timestamp=%lu.%9.9lu\n",
829 			p->type, p->pending, p->sequence, p->id,
830 			p->timestamp.tv_sec, p->timestamp.tv_nsec);
831 	switch (p->type) {
832 	case V4L2_EVENT_VSYNC:
833 		printk(KERN_DEBUG "field=%s\n",
834 			prt_names(p->u.vsync.field, v4l2_field_names));
835 		break;
836 	case V4L2_EVENT_CTRL:
837 		c = &p->u.ctrl;
838 		printk(KERN_DEBUG "changes=0x%x, type=%u, ",
839 			c->changes, c->type);
840 		if (c->type == V4L2_CTRL_TYPE_INTEGER64)
841 			pr_cont("value64=%lld, ", c->value64);
842 		else
843 			pr_cont("value=%d, ", c->value);
844 		pr_cont("flags=0x%x, minimum=%d, maximum=%d, step=%d, default_value=%d\n",
845 			c->flags, c->minimum, c->maximum,
846 			c->step, c->default_value);
847 		break;
848 	case V4L2_EVENT_FRAME_SYNC:
849 		pr_cont("frame_sequence=%u\n",
850 			p->u.frame_sync.frame_sequence);
851 		break;
852 	}
853 }
854 
855 static void v4l_print_event_subscription(const void *arg, bool write_only)
856 {
857 	const struct v4l2_event_subscription *p = arg;
858 
859 	pr_cont("type=0x%x, id=0x%x, flags=0x%x\n",
860 			p->type, p->id, p->flags);
861 }
862 
863 static void v4l_print_sliced_vbi_cap(const void *arg, bool write_only)
864 {
865 	const struct v4l2_sliced_vbi_cap *p = arg;
866 	int i;
867 
868 	pr_cont("type=%s, service_set=0x%08x\n",
869 			prt_names(p->type, v4l2_type_names), p->service_set);
870 	for (i = 0; i < 24; i++)
871 		printk(KERN_DEBUG "line[%02u]=0x%04x, 0x%04x\n", i,
872 				p->service_lines[0][i],
873 				p->service_lines[1][i]);
874 }
875 
876 static void v4l_print_freq_band(const void *arg, bool write_only)
877 {
878 	const struct v4l2_frequency_band *p = arg;
879 
880 	pr_cont("tuner=%u, type=%u, index=%u, capability=0x%x, rangelow=%u, rangehigh=%u, modulation=0x%x\n",
881 			p->tuner, p->type, p->index,
882 			p->capability, p->rangelow,
883 			p->rangehigh, p->modulation);
884 }
885 
886 static void v4l_print_edid(const void *arg, bool write_only)
887 {
888 	const struct v4l2_edid *p = arg;
889 
890 	pr_cont("pad=%u, start_block=%u, blocks=%u\n",
891 		p->pad, p->start_block, p->blocks);
892 }
893 
894 static void v4l_print_u32(const void *arg, bool write_only)
895 {
896 	pr_cont("value=%u\n", *(const u32 *)arg);
897 }
898 
899 static void v4l_print_newline(const void *arg, bool write_only)
900 {
901 	pr_cont("\n");
902 }
903 
904 static void v4l_print_default(const void *arg, bool write_only)
905 {
906 	pr_cont("driver-specific ioctl\n");
907 }
908 
909 static int check_ext_ctrls(struct v4l2_ext_controls *c, int allow_priv)
910 {
911 	__u32 i;
912 
913 	/* zero the reserved fields */
914 	c->reserved[0] = 0;
915 	for (i = 0; i < c->count; i++)
916 		c->controls[i].reserved2[0] = 0;
917 
918 	/* V4L2_CID_PRIVATE_BASE cannot be used as control class
919 	   when using extended controls.
920 	   Only when passed in through VIDIOC_G_CTRL and VIDIOC_S_CTRL
921 	   is it allowed for backwards compatibility.
922 	 */
923 	if (!allow_priv && c->which == V4L2_CID_PRIVATE_BASE)
924 		return 0;
925 	if (!c->which)
926 		return 1;
927 	/* Check that all controls are from the same control class. */
928 	for (i = 0; i < c->count; i++) {
929 		if (V4L2_CTRL_ID2WHICH(c->controls[i].id) != c->which) {
930 			c->error_idx = i;
931 			return 0;
932 		}
933 	}
934 	return 1;
935 }
936 
937 static int check_fmt(struct file *file, enum v4l2_buf_type type)
938 {
939 	struct video_device *vfd = video_devdata(file);
940 	const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
941 	bool is_vid = vfd->vfl_type == VFL_TYPE_GRABBER;
942 	bool is_vbi = vfd->vfl_type == VFL_TYPE_VBI;
943 	bool is_sdr = vfd->vfl_type == VFL_TYPE_SDR;
944 	bool is_tch = vfd->vfl_type == VFL_TYPE_TOUCH;
945 	bool is_rx = vfd->vfl_dir != VFL_DIR_TX;
946 	bool is_tx = vfd->vfl_dir != VFL_DIR_RX;
947 
948 	if (ops == NULL)
949 		return -EINVAL;
950 
951 	switch (type) {
952 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
953 		if ((is_vid || is_tch) && is_rx &&
954 		    (ops->vidioc_g_fmt_vid_cap || ops->vidioc_g_fmt_vid_cap_mplane))
955 			return 0;
956 		break;
957 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
958 		if (is_vid && is_rx && ops->vidioc_g_fmt_vid_cap_mplane)
959 			return 0;
960 		break;
961 	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
962 		if (is_vid && is_rx && ops->vidioc_g_fmt_vid_overlay)
963 			return 0;
964 		break;
965 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
966 		if (is_vid && is_tx &&
967 		    (ops->vidioc_g_fmt_vid_out || ops->vidioc_g_fmt_vid_out_mplane))
968 			return 0;
969 		break;
970 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
971 		if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_mplane)
972 			return 0;
973 		break;
974 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
975 		if (is_vid && is_tx && ops->vidioc_g_fmt_vid_out_overlay)
976 			return 0;
977 		break;
978 	case V4L2_BUF_TYPE_VBI_CAPTURE:
979 		if (is_vbi && is_rx && ops->vidioc_g_fmt_vbi_cap)
980 			return 0;
981 		break;
982 	case V4L2_BUF_TYPE_VBI_OUTPUT:
983 		if (is_vbi && is_tx && ops->vidioc_g_fmt_vbi_out)
984 			return 0;
985 		break;
986 	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
987 		if (is_vbi && is_rx && ops->vidioc_g_fmt_sliced_vbi_cap)
988 			return 0;
989 		break;
990 	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
991 		if (is_vbi && is_tx && ops->vidioc_g_fmt_sliced_vbi_out)
992 			return 0;
993 		break;
994 	case V4L2_BUF_TYPE_SDR_CAPTURE:
995 		if (is_sdr && is_rx && ops->vidioc_g_fmt_sdr_cap)
996 			return 0;
997 		break;
998 	case V4L2_BUF_TYPE_SDR_OUTPUT:
999 		if (is_sdr && is_tx && ops->vidioc_g_fmt_sdr_out)
1000 			return 0;
1001 		break;
1002 	case V4L2_BUF_TYPE_META_CAPTURE:
1003 		if (is_vid && is_rx && ops->vidioc_g_fmt_meta_cap)
1004 			return 0;
1005 		break;
1006 	case V4L2_BUF_TYPE_META_OUTPUT:
1007 		if (is_vid && is_tx && ops->vidioc_g_fmt_meta_out)
1008 			return 0;
1009 		break;
1010 	default:
1011 		break;
1012 	}
1013 	return -EINVAL;
1014 }
1015 
1016 static void v4l_sanitize_format(struct v4l2_format *fmt)
1017 {
1018 	unsigned int offset;
1019 
1020 	/* Make sure num_planes is not bogus */
1021 	if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
1022 	    fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1023 		fmt->fmt.pix_mp.num_planes = min_t(u32, fmt->fmt.pix_mp.num_planes,
1024 					       VIDEO_MAX_PLANES);
1025 
1026 	/*
1027 	 * The v4l2_pix_format structure has been extended with fields that were
1028 	 * not previously required to be set to zero by applications. The priv
1029 	 * field, when set to a magic value, indicates the the extended fields
1030 	 * are valid. Otherwise they will contain undefined values. To simplify
1031 	 * the API towards drivers zero the extended fields and set the priv
1032 	 * field to the magic value when the extended pixel format structure
1033 	 * isn't used by applications.
1034 	 */
1035 
1036 	if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1037 	    fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1038 		return;
1039 
1040 	if (fmt->fmt.pix.priv == V4L2_PIX_FMT_PRIV_MAGIC)
1041 		return;
1042 
1043 	fmt->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1044 
1045 	offset = offsetof(struct v4l2_pix_format, priv)
1046 	       + sizeof(fmt->fmt.pix.priv);
1047 	memset(((void *)&fmt->fmt.pix) + offset, 0,
1048 	       sizeof(fmt->fmt.pix) - offset);
1049 }
1050 
1051 static int v4l_querycap(const struct v4l2_ioctl_ops *ops,
1052 				struct file *file, void *fh, void *arg)
1053 {
1054 	struct v4l2_capability *cap = (struct v4l2_capability *)arg;
1055 	struct video_device *vfd = video_devdata(file);
1056 	int ret;
1057 
1058 	cap->version = LINUX_VERSION_CODE;
1059 	cap->device_caps = vfd->device_caps;
1060 	cap->capabilities = vfd->device_caps | V4L2_CAP_DEVICE_CAPS;
1061 
1062 	ret = ops->vidioc_querycap(file, fh, cap);
1063 
1064 	cap->capabilities |= V4L2_CAP_EXT_PIX_FORMAT;
1065 	/*
1066 	 * Drivers MUST fill in device_caps, so check for this and
1067 	 * warn if it was forgotten.
1068 	 */
1069 	WARN(!(cap->capabilities & V4L2_CAP_DEVICE_CAPS) ||
1070 		!cap->device_caps, "Bad caps for driver %s, %x %x",
1071 		cap->driver, cap->capabilities, cap->device_caps);
1072 	cap->device_caps |= V4L2_CAP_EXT_PIX_FORMAT;
1073 
1074 	return ret;
1075 }
1076 
1077 static int v4l_s_input(const struct v4l2_ioctl_ops *ops,
1078 				struct file *file, void *fh, void *arg)
1079 {
1080 	struct video_device *vfd = video_devdata(file);
1081 	int ret;
1082 
1083 	ret = v4l_enable_media_source(vfd);
1084 	if (ret)
1085 		return ret;
1086 	return ops->vidioc_s_input(file, fh, *(unsigned int *)arg);
1087 }
1088 
1089 static int v4l_s_output(const struct v4l2_ioctl_ops *ops,
1090 				struct file *file, void *fh, void *arg)
1091 {
1092 	return ops->vidioc_s_output(file, fh, *(unsigned int *)arg);
1093 }
1094 
1095 static int v4l_g_priority(const struct v4l2_ioctl_ops *ops,
1096 				struct file *file, void *fh, void *arg)
1097 {
1098 	struct video_device *vfd;
1099 	u32 *p = arg;
1100 
1101 	vfd = video_devdata(file);
1102 	*p = v4l2_prio_max(vfd->prio);
1103 	return 0;
1104 }
1105 
1106 static int v4l_s_priority(const struct v4l2_ioctl_ops *ops,
1107 				struct file *file, void *fh, void *arg)
1108 {
1109 	struct video_device *vfd;
1110 	struct v4l2_fh *vfh;
1111 	u32 *p = arg;
1112 
1113 	vfd = video_devdata(file);
1114 	if (!test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
1115 		return -ENOTTY;
1116 	vfh = file->private_data;
1117 	return v4l2_prio_change(vfd->prio, &vfh->prio, *p);
1118 }
1119 
1120 static int v4l_enuminput(const struct v4l2_ioctl_ops *ops,
1121 				struct file *file, void *fh, void *arg)
1122 {
1123 	struct video_device *vfd = video_devdata(file);
1124 	struct v4l2_input *p = arg;
1125 
1126 	/*
1127 	 * We set the flags for CAP_DV_TIMINGS &
1128 	 * CAP_STD here based on ioctl handler provided by the
1129 	 * driver. If the driver doesn't support these
1130 	 * for a specific input, it must override these flags.
1131 	 */
1132 	if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1133 		p->capabilities |= V4L2_IN_CAP_STD;
1134 
1135 	return ops->vidioc_enum_input(file, fh, p);
1136 }
1137 
1138 static int v4l_enumoutput(const struct v4l2_ioctl_ops *ops,
1139 				struct file *file, void *fh, void *arg)
1140 {
1141 	struct video_device *vfd = video_devdata(file);
1142 	struct v4l2_output *p = arg;
1143 
1144 	/*
1145 	 * We set the flags for CAP_DV_TIMINGS &
1146 	 * CAP_STD here based on ioctl handler provided by the
1147 	 * driver. If the driver doesn't support these
1148 	 * for a specific output, it must override these flags.
1149 	 */
1150 	if (is_valid_ioctl(vfd, VIDIOC_S_STD))
1151 		p->capabilities |= V4L2_OUT_CAP_STD;
1152 
1153 	return ops->vidioc_enum_output(file, fh, p);
1154 }
1155 
1156 static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
1157 {
1158 	const unsigned sz = sizeof(fmt->description);
1159 	const char *descr = NULL;
1160 	u32 flags = 0;
1161 
1162 	/*
1163 	 * We depart from the normal coding style here since the descriptions
1164 	 * should be aligned so it is easy to see which descriptions will be
1165 	 * longer than 31 characters (the max length for a description).
1166 	 * And frankly, this is easier to read anyway.
1167 	 *
1168 	 * Note that gcc will use O(log N) comparisons to find the right case.
1169 	 */
1170 	switch (fmt->pixelformat) {
1171 	/* Max description length mask:	descr = "0123456789012345678901234567890" */
1172 	case V4L2_PIX_FMT_RGB332:	descr = "8-bit RGB 3-3-2"; break;
1173 	case V4L2_PIX_FMT_RGB444:	descr = "16-bit A/XRGB 4-4-4-4"; break;
1174 	case V4L2_PIX_FMT_ARGB444:	descr = "16-bit ARGB 4-4-4-4"; break;
1175 	case V4L2_PIX_FMT_XRGB444:	descr = "16-bit XRGB 4-4-4-4"; break;
1176 	case V4L2_PIX_FMT_RGB555:	descr = "16-bit A/XRGB 1-5-5-5"; break;
1177 	case V4L2_PIX_FMT_ARGB555:	descr = "16-bit ARGB 1-5-5-5"; break;
1178 	case V4L2_PIX_FMT_XRGB555:	descr = "16-bit XRGB 1-5-5-5"; break;
1179 	case V4L2_PIX_FMT_RGB565:	descr = "16-bit RGB 5-6-5"; break;
1180 	case V4L2_PIX_FMT_RGB555X:	descr = "16-bit A/XRGB 1-5-5-5 BE"; break;
1181 	case V4L2_PIX_FMT_ARGB555X:	descr = "16-bit ARGB 1-5-5-5 BE"; break;
1182 	case V4L2_PIX_FMT_XRGB555X:	descr = "16-bit XRGB 1-5-5-5 BE"; break;
1183 	case V4L2_PIX_FMT_RGB565X:	descr = "16-bit RGB 5-6-5 BE"; break;
1184 	case V4L2_PIX_FMT_BGR666:	descr = "18-bit BGRX 6-6-6-14"; break;
1185 	case V4L2_PIX_FMT_BGR24:	descr = "24-bit BGR 8-8-8"; break;
1186 	case V4L2_PIX_FMT_RGB24:	descr = "24-bit RGB 8-8-8"; break;
1187 	case V4L2_PIX_FMT_BGR32:	descr = "32-bit BGRA/X 8-8-8-8"; break;
1188 	case V4L2_PIX_FMT_ABGR32:	descr = "32-bit BGRA 8-8-8-8"; break;
1189 	case V4L2_PIX_FMT_XBGR32:	descr = "32-bit BGRX 8-8-8-8"; break;
1190 	case V4L2_PIX_FMT_RGB32:	descr = "32-bit A/XRGB 8-8-8-8"; break;
1191 	case V4L2_PIX_FMT_ARGB32:	descr = "32-bit ARGB 8-8-8-8"; break;
1192 	case V4L2_PIX_FMT_XRGB32:	descr = "32-bit XRGB 8-8-8-8"; break;
1193 	case V4L2_PIX_FMT_GREY:		descr = "8-bit Greyscale"; break;
1194 	case V4L2_PIX_FMT_Y4:		descr = "4-bit Greyscale"; break;
1195 	case V4L2_PIX_FMT_Y6:		descr = "6-bit Greyscale"; break;
1196 	case V4L2_PIX_FMT_Y10:		descr = "10-bit Greyscale"; break;
1197 	case V4L2_PIX_FMT_Y12:		descr = "12-bit Greyscale"; break;
1198 	case V4L2_PIX_FMT_Y16:		descr = "16-bit Greyscale"; break;
1199 	case V4L2_PIX_FMT_Y16_BE:	descr = "16-bit Greyscale BE"; break;
1200 	case V4L2_PIX_FMT_Y10BPACK:	descr = "10-bit Greyscale (Packed)"; break;
1201 	case V4L2_PIX_FMT_Y10P:		descr = "10-bit Greyscale (MIPI Packed)"; break;
1202 	case V4L2_PIX_FMT_Y8I:		descr = "Interleaved 8-bit Greyscale"; break;
1203 	case V4L2_PIX_FMT_Y12I:		descr = "Interleaved 12-bit Greyscale"; break;
1204 	case V4L2_PIX_FMT_Z16:		descr = "16-bit Depth"; break;
1205 	case V4L2_PIX_FMT_INZI:		descr = "Planar 10:16 Greyscale Depth"; break;
1206 	case V4L2_PIX_FMT_CNF4:		descr = "4-bit Depth Confidence (Packed)"; break;
1207 	case V4L2_PIX_FMT_PAL8:		descr = "8-bit Palette"; break;
1208 	case V4L2_PIX_FMT_UV8:		descr = "8-bit Chrominance UV 4-4"; break;
1209 	case V4L2_PIX_FMT_YVU410:	descr = "Planar YVU 4:1:0"; break;
1210 	case V4L2_PIX_FMT_YVU420:	descr = "Planar YVU 4:2:0"; break;
1211 	case V4L2_PIX_FMT_YUYV:		descr = "YUYV 4:2:2"; break;
1212 	case V4L2_PIX_FMT_YYUV:		descr = "YYUV 4:2:2"; break;
1213 	case V4L2_PIX_FMT_YVYU:		descr = "YVYU 4:2:2"; break;
1214 	case V4L2_PIX_FMT_UYVY:		descr = "UYVY 4:2:2"; break;
1215 	case V4L2_PIX_FMT_VYUY:		descr = "VYUY 4:2:2"; break;
1216 	case V4L2_PIX_FMT_YUV422P:	descr = "Planar YUV 4:2:2"; break;
1217 	case V4L2_PIX_FMT_YUV411P:	descr = "Planar YUV 4:1:1"; break;
1218 	case V4L2_PIX_FMT_Y41P:		descr = "YUV 4:1:1 (Packed)"; break;
1219 	case V4L2_PIX_FMT_YUV444:	descr = "16-bit A/XYUV 4-4-4-4"; break;
1220 	case V4L2_PIX_FMT_YUV555:	descr = "16-bit A/XYUV 1-5-5-5"; break;
1221 	case V4L2_PIX_FMT_YUV565:	descr = "16-bit YUV 5-6-5"; break;
1222 	case V4L2_PIX_FMT_YUV32:	descr = "32-bit A/XYUV 8-8-8-8"; break;
1223 	case V4L2_PIX_FMT_AYUV32:	descr = "32-bit AYUV 8-8-8-8"; break;
1224 	case V4L2_PIX_FMT_XYUV32:	descr = "32-bit XYUV 8-8-8-8"; break;
1225 	case V4L2_PIX_FMT_VUYA32:	descr = "32-bit VUYA 8-8-8-8"; break;
1226 	case V4L2_PIX_FMT_VUYX32:	descr = "32-bit VUYX 8-8-8-8"; break;
1227 	case V4L2_PIX_FMT_YUV410:	descr = "Planar YUV 4:1:0"; break;
1228 	case V4L2_PIX_FMT_YUV420:	descr = "Planar YUV 4:2:0"; break;
1229 	case V4L2_PIX_FMT_HI240:	descr = "8-bit Dithered RGB (BTTV)"; break;
1230 	case V4L2_PIX_FMT_HM12:		descr = "YUV 4:2:0 (16x16 Macroblocks)"; break;
1231 	case V4L2_PIX_FMT_M420:		descr = "YUV 4:2:0 (M420)"; break;
1232 	case V4L2_PIX_FMT_NV12:		descr = "Y/CbCr 4:2:0"; break;
1233 	case V4L2_PIX_FMT_NV21:		descr = "Y/CrCb 4:2:0"; break;
1234 	case V4L2_PIX_FMT_NV16:		descr = "Y/CbCr 4:2:2"; break;
1235 	case V4L2_PIX_FMT_NV61:		descr = "Y/CrCb 4:2:2"; break;
1236 	case V4L2_PIX_FMT_NV24:		descr = "Y/CbCr 4:4:4"; break;
1237 	case V4L2_PIX_FMT_NV42:		descr = "Y/CrCb 4:4:4"; break;
1238 	case V4L2_PIX_FMT_NV12M:	descr = "Y/CbCr 4:2:0 (N-C)"; break;
1239 	case V4L2_PIX_FMT_NV21M:	descr = "Y/CrCb 4:2:0 (N-C)"; break;
1240 	case V4L2_PIX_FMT_NV16M:	descr = "Y/CbCr 4:2:2 (N-C)"; break;
1241 	case V4L2_PIX_FMT_NV61M:	descr = "Y/CrCb 4:2:2 (N-C)"; break;
1242 	case V4L2_PIX_FMT_NV12MT:	descr = "Y/CbCr 4:2:0 (64x32 MB, N-C)"; break;
1243 	case V4L2_PIX_FMT_NV12MT_16X16:	descr = "Y/CbCr 4:2:0 (16x16 MB, N-C)"; break;
1244 	case V4L2_PIX_FMT_YUV420M:	descr = "Planar YUV 4:2:0 (N-C)"; break;
1245 	case V4L2_PIX_FMT_YVU420M:	descr = "Planar YVU 4:2:0 (N-C)"; break;
1246 	case V4L2_PIX_FMT_YUV422M:	descr = "Planar YUV 4:2:2 (N-C)"; break;
1247 	case V4L2_PIX_FMT_YVU422M:	descr = "Planar YVU 4:2:2 (N-C)"; break;
1248 	case V4L2_PIX_FMT_YUV444M:	descr = "Planar YUV 4:4:4 (N-C)"; break;
1249 	case V4L2_PIX_FMT_YVU444M:	descr = "Planar YVU 4:4:4 (N-C)"; break;
1250 	case V4L2_PIX_FMT_SBGGR8:	descr = "8-bit Bayer BGBG/GRGR"; break;
1251 	case V4L2_PIX_FMT_SGBRG8:	descr = "8-bit Bayer GBGB/RGRG"; break;
1252 	case V4L2_PIX_FMT_SGRBG8:	descr = "8-bit Bayer GRGR/BGBG"; break;
1253 	case V4L2_PIX_FMT_SRGGB8:	descr = "8-bit Bayer RGRG/GBGB"; break;
1254 	case V4L2_PIX_FMT_SBGGR10:	descr = "10-bit Bayer BGBG/GRGR"; break;
1255 	case V4L2_PIX_FMT_SGBRG10:	descr = "10-bit Bayer GBGB/RGRG"; break;
1256 	case V4L2_PIX_FMT_SGRBG10:	descr = "10-bit Bayer GRGR/BGBG"; break;
1257 	case V4L2_PIX_FMT_SRGGB10:	descr = "10-bit Bayer RGRG/GBGB"; break;
1258 	case V4L2_PIX_FMT_SBGGR10P:	descr = "10-bit Bayer BGBG/GRGR Packed"; break;
1259 	case V4L2_PIX_FMT_SGBRG10P:	descr = "10-bit Bayer GBGB/RGRG Packed"; break;
1260 	case V4L2_PIX_FMT_SGRBG10P:	descr = "10-bit Bayer GRGR/BGBG Packed"; break;
1261 	case V4L2_PIX_FMT_SRGGB10P:	descr = "10-bit Bayer RGRG/GBGB Packed"; break;
1262 	case V4L2_PIX_FMT_IPU3_SBGGR10: descr = "10-bit bayer BGGR IPU3 Packed"; break;
1263 	case V4L2_PIX_FMT_IPU3_SGBRG10: descr = "10-bit bayer GBRG IPU3 Packed"; break;
1264 	case V4L2_PIX_FMT_IPU3_SGRBG10: descr = "10-bit bayer GRBG IPU3 Packed"; break;
1265 	case V4L2_PIX_FMT_IPU3_SRGGB10: descr = "10-bit bayer RGGB IPU3 Packed"; break;
1266 	case V4L2_PIX_FMT_SBGGR10ALAW8:	descr = "8-bit Bayer BGBG/GRGR (A-law)"; break;
1267 	case V4L2_PIX_FMT_SGBRG10ALAW8:	descr = "8-bit Bayer GBGB/RGRG (A-law)"; break;
1268 	case V4L2_PIX_FMT_SGRBG10ALAW8:	descr = "8-bit Bayer GRGR/BGBG (A-law)"; break;
1269 	case V4L2_PIX_FMT_SRGGB10ALAW8:	descr = "8-bit Bayer RGRG/GBGB (A-law)"; break;
1270 	case V4L2_PIX_FMT_SBGGR10DPCM8:	descr = "8-bit Bayer BGBG/GRGR (DPCM)"; break;
1271 	case V4L2_PIX_FMT_SGBRG10DPCM8:	descr = "8-bit Bayer GBGB/RGRG (DPCM)"; break;
1272 	case V4L2_PIX_FMT_SGRBG10DPCM8:	descr = "8-bit Bayer GRGR/BGBG (DPCM)"; break;
1273 	case V4L2_PIX_FMT_SRGGB10DPCM8:	descr = "8-bit Bayer RGRG/GBGB (DPCM)"; break;
1274 	case V4L2_PIX_FMT_SBGGR12:	descr = "12-bit Bayer BGBG/GRGR"; break;
1275 	case V4L2_PIX_FMT_SGBRG12:	descr = "12-bit Bayer GBGB/RGRG"; break;
1276 	case V4L2_PIX_FMT_SGRBG12:	descr = "12-bit Bayer GRGR/BGBG"; break;
1277 	case V4L2_PIX_FMT_SRGGB12:	descr = "12-bit Bayer RGRG/GBGB"; break;
1278 	case V4L2_PIX_FMT_SBGGR12P:	descr = "12-bit Bayer BGBG/GRGR Packed"; break;
1279 	case V4L2_PIX_FMT_SGBRG12P:	descr = "12-bit Bayer GBGB/RGRG Packed"; break;
1280 	case V4L2_PIX_FMT_SGRBG12P:	descr = "12-bit Bayer GRGR/BGBG Packed"; break;
1281 	case V4L2_PIX_FMT_SRGGB12P:	descr = "12-bit Bayer RGRG/GBGB Packed"; break;
1282 	case V4L2_PIX_FMT_SBGGR14P:	descr = "14-bit Bayer BGBG/GRGR Packed"; break;
1283 	case V4L2_PIX_FMT_SGBRG14P:	descr = "14-bit Bayer GBGB/RGRG Packed"; break;
1284 	case V4L2_PIX_FMT_SGRBG14P:	descr = "14-bit Bayer GRGR/BGBG Packed"; break;
1285 	case V4L2_PIX_FMT_SRGGB14P:	descr = "14-bit Bayer RGRG/GBGB Packed"; break;
1286 	case V4L2_PIX_FMT_SBGGR16:	descr = "16-bit Bayer BGBG/GRGR"; break;
1287 	case V4L2_PIX_FMT_SGBRG16:	descr = "16-bit Bayer GBGB/RGRG"; break;
1288 	case V4L2_PIX_FMT_SGRBG16:	descr = "16-bit Bayer GRGR/BGBG"; break;
1289 	case V4L2_PIX_FMT_SRGGB16:	descr = "16-bit Bayer RGRG/GBGB"; break;
1290 	case V4L2_PIX_FMT_SN9C20X_I420:	descr = "GSPCA SN9C20X I420"; break;
1291 	case V4L2_PIX_FMT_SPCA501:	descr = "GSPCA SPCA501"; break;
1292 	case V4L2_PIX_FMT_SPCA505:	descr = "GSPCA SPCA505"; break;
1293 	case V4L2_PIX_FMT_SPCA508:	descr = "GSPCA SPCA508"; break;
1294 	case V4L2_PIX_FMT_STV0680:	descr = "GSPCA STV0680"; break;
1295 	case V4L2_PIX_FMT_TM6000:	descr = "A/V + VBI Mux Packet"; break;
1296 	case V4L2_PIX_FMT_CIT_YYVYUY:	descr = "GSPCA CIT YYVYUY"; break;
1297 	case V4L2_PIX_FMT_KONICA420:	descr = "GSPCA KONICA420"; break;
1298 	case V4L2_PIX_FMT_HSV24:	descr = "24-bit HSV 8-8-8"; break;
1299 	case V4L2_PIX_FMT_HSV32:	descr = "32-bit XHSV 8-8-8-8"; break;
1300 	case V4L2_SDR_FMT_CU8:		descr = "Complex U8"; break;
1301 	case V4L2_SDR_FMT_CU16LE:	descr = "Complex U16LE"; break;
1302 	case V4L2_SDR_FMT_CS8:		descr = "Complex S8"; break;
1303 	case V4L2_SDR_FMT_CS14LE:	descr = "Complex S14LE"; break;
1304 	case V4L2_SDR_FMT_RU12LE:	descr = "Real U12LE"; break;
1305 	case V4L2_SDR_FMT_PCU16BE:	descr = "Planar Complex U16BE"; break;
1306 	case V4L2_SDR_FMT_PCU18BE:	descr = "Planar Complex U18BE"; break;
1307 	case V4L2_SDR_FMT_PCU20BE:	descr = "Planar Complex U20BE"; break;
1308 	case V4L2_TCH_FMT_DELTA_TD16:	descr = "16-bit signed deltas"; break;
1309 	case V4L2_TCH_FMT_DELTA_TD08:	descr = "8-bit signed deltas"; break;
1310 	case V4L2_TCH_FMT_TU16:		descr = "16-bit unsigned touch data"; break;
1311 	case V4L2_TCH_FMT_TU08:		descr = "8-bit unsigned touch data"; break;
1312 	case V4L2_META_FMT_VSP1_HGO:	descr = "R-Car VSP1 1-D Histogram"; break;
1313 	case V4L2_META_FMT_VSP1_HGT:	descr = "R-Car VSP1 2-D Histogram"; break;
1314 	case V4L2_META_FMT_UVC:		descr = "UVC payload header metadata"; break;
1315 
1316 	default:
1317 		/* Compressed formats */
1318 		flags = V4L2_FMT_FLAG_COMPRESSED;
1319 		switch (fmt->pixelformat) {
1320 		/* Max description length mask:	descr = "0123456789012345678901234567890" */
1321 		case V4L2_PIX_FMT_MJPEG:	descr = "Motion-JPEG"; break;
1322 		case V4L2_PIX_FMT_JPEG:		descr = "JFIF JPEG"; break;
1323 		case V4L2_PIX_FMT_DV:		descr = "1394"; break;
1324 		case V4L2_PIX_FMT_MPEG:		descr = "MPEG-1/2/4"; break;
1325 		case V4L2_PIX_FMT_H264:		descr = "H.264"; break;
1326 		case V4L2_PIX_FMT_H264_NO_SC:	descr = "H.264 (No Start Codes)"; break;
1327 		case V4L2_PIX_FMT_H264_MVC:	descr = "H.264 MVC"; break;
1328 		case V4L2_PIX_FMT_H263:		descr = "H.263"; break;
1329 		case V4L2_PIX_FMT_MPEG1:	descr = "MPEG-1 ES"; break;
1330 		case V4L2_PIX_FMT_MPEG2:	descr = "MPEG-2 ES"; break;
1331 		case V4L2_PIX_FMT_MPEG2_SLICE:	descr = "MPEG-2 Parsed Slice Data"; break;
1332 		case V4L2_PIX_FMT_MPEG4:	descr = "MPEG-4 part 2 ES"; break;
1333 		case V4L2_PIX_FMT_XVID:		descr = "Xvid"; break;
1334 		case V4L2_PIX_FMT_VC1_ANNEX_G:	descr = "VC-1 (SMPTE 412M Annex G)"; break;
1335 		case V4L2_PIX_FMT_VC1_ANNEX_L:	descr = "VC-1 (SMPTE 412M Annex L)"; break;
1336 		case V4L2_PIX_FMT_VP8:		descr = "VP8"; break;
1337 		case V4L2_PIX_FMT_VP9:		descr = "VP9"; break;
1338 		case V4L2_PIX_FMT_HEVC:		descr = "HEVC"; break; /* aka H.265 */
1339 		case V4L2_PIX_FMT_FWHT:		descr = "FWHT"; break; /* used in vicodec */
1340 		case V4L2_PIX_FMT_CPIA1:	descr = "GSPCA CPiA YUV"; break;
1341 		case V4L2_PIX_FMT_WNVA:		descr = "WNVA"; break;
1342 		case V4L2_PIX_FMT_SN9C10X:	descr = "GSPCA SN9C10X"; break;
1343 		case V4L2_PIX_FMT_PWC1:		descr = "Raw Philips Webcam Type (Old)"; break;
1344 		case V4L2_PIX_FMT_PWC2:		descr = "Raw Philips Webcam Type (New)"; break;
1345 		case V4L2_PIX_FMT_ET61X251:	descr = "GSPCA ET61X251"; break;
1346 		case V4L2_PIX_FMT_SPCA561:	descr = "GSPCA SPCA561"; break;
1347 		case V4L2_PIX_FMT_PAC207:	descr = "GSPCA PAC207"; break;
1348 		case V4L2_PIX_FMT_MR97310A:	descr = "GSPCA MR97310A"; break;
1349 		case V4L2_PIX_FMT_JL2005BCD:	descr = "GSPCA JL2005BCD"; break;
1350 		case V4L2_PIX_FMT_SN9C2028:	descr = "GSPCA SN9C2028"; break;
1351 		case V4L2_PIX_FMT_SQ905C:	descr = "GSPCA SQ905C"; break;
1352 		case V4L2_PIX_FMT_PJPG:		descr = "GSPCA PJPG"; break;
1353 		case V4L2_PIX_FMT_OV511:	descr = "GSPCA OV511"; break;
1354 		case V4L2_PIX_FMT_OV518:	descr = "GSPCA OV518"; break;
1355 		case V4L2_PIX_FMT_JPGL:		descr = "JPEG Lite"; break;
1356 		case V4L2_PIX_FMT_SE401:	descr = "GSPCA SE401"; break;
1357 		case V4L2_PIX_FMT_S5C_UYVY_JPG:	descr = "S5C73MX interleaved UYVY/JPEG"; break;
1358 		case V4L2_PIX_FMT_MT21C:	descr = "Mediatek Compressed Format"; break;
1359 		case V4L2_PIX_FMT_SUNXI_TILED_NV12: descr = "Sunxi Tiled NV12 Format"; break;
1360 		default:
1361 			if (fmt->description[0])
1362 				return;
1363 			WARN(1, "Unknown pixelformat 0x%08x\n", fmt->pixelformat);
1364 			flags = 0;
1365 			snprintf(fmt->description, sz, "%c%c%c%c%s",
1366 					(char)(fmt->pixelformat & 0x7f),
1367 					(char)((fmt->pixelformat >> 8) & 0x7f),
1368 					(char)((fmt->pixelformat >> 16) & 0x7f),
1369 					(char)((fmt->pixelformat >> 24) & 0x7f),
1370 					(fmt->pixelformat & (1 << 31)) ? "-BE" : "");
1371 			break;
1372 		}
1373 	}
1374 
1375 	if (descr)
1376 		WARN_ON(strscpy(fmt->description, descr, sz) >= sz);
1377 	fmt->flags = flags;
1378 }
1379 
1380 static int v4l_enum_fmt(const struct v4l2_ioctl_ops *ops,
1381 				struct file *file, void *fh, void *arg)
1382 {
1383 	struct v4l2_fmtdesc *p = arg;
1384 	int ret = check_fmt(file, p->type);
1385 
1386 	if (ret)
1387 		return ret;
1388 	ret = -EINVAL;
1389 
1390 	switch (p->type) {
1391 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1392 		if (unlikely(!ops->vidioc_enum_fmt_vid_cap))
1393 			break;
1394 		ret = ops->vidioc_enum_fmt_vid_cap(file, fh, arg);
1395 		break;
1396 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1397 		if (unlikely(!ops->vidioc_enum_fmt_vid_cap_mplane))
1398 			break;
1399 		ret = ops->vidioc_enum_fmt_vid_cap_mplane(file, fh, arg);
1400 		break;
1401 	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1402 		if (unlikely(!ops->vidioc_enum_fmt_vid_overlay))
1403 			break;
1404 		ret = ops->vidioc_enum_fmt_vid_overlay(file, fh, arg);
1405 		break;
1406 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1407 		if (unlikely(!ops->vidioc_enum_fmt_vid_out))
1408 			break;
1409 		ret = ops->vidioc_enum_fmt_vid_out(file, fh, arg);
1410 		break;
1411 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1412 		if (unlikely(!ops->vidioc_enum_fmt_vid_out_mplane))
1413 			break;
1414 		ret = ops->vidioc_enum_fmt_vid_out_mplane(file, fh, arg);
1415 		break;
1416 	case V4L2_BUF_TYPE_SDR_CAPTURE:
1417 		if (unlikely(!ops->vidioc_enum_fmt_sdr_cap))
1418 			break;
1419 		ret = ops->vidioc_enum_fmt_sdr_cap(file, fh, arg);
1420 		break;
1421 	case V4L2_BUF_TYPE_SDR_OUTPUT:
1422 		if (unlikely(!ops->vidioc_enum_fmt_sdr_out))
1423 			break;
1424 		ret = ops->vidioc_enum_fmt_sdr_out(file, fh, arg);
1425 		break;
1426 	case V4L2_BUF_TYPE_META_CAPTURE:
1427 		if (unlikely(!ops->vidioc_enum_fmt_meta_cap))
1428 			break;
1429 		ret = ops->vidioc_enum_fmt_meta_cap(file, fh, arg);
1430 		break;
1431 	case V4L2_BUF_TYPE_META_OUTPUT:
1432 		if (unlikely(!ops->vidioc_enum_fmt_meta_out))
1433 			break;
1434 		ret = ops->vidioc_enum_fmt_meta_out(file, fh, arg);
1435 		break;
1436 	}
1437 	if (ret == 0)
1438 		v4l_fill_fmtdesc(p);
1439 	return ret;
1440 }
1441 
1442 static int v4l_g_fmt(const struct v4l2_ioctl_ops *ops,
1443 				struct file *file, void *fh, void *arg)
1444 {
1445 	struct v4l2_format *p = arg;
1446 	int ret = check_fmt(file, p->type);
1447 
1448 	if (ret)
1449 		return ret;
1450 
1451 	/*
1452 	 * fmt can't be cleared for these overlay types due to the 'clips'
1453 	 * 'clipcount' and 'bitmap' pointers in struct v4l2_window.
1454 	 * Those are provided by the user. So handle these two overlay types
1455 	 * first, and then just do a simple memset for the other types.
1456 	 */
1457 	switch (p->type) {
1458 	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1459 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY: {
1460 		struct v4l2_clip __user *clips = p->fmt.win.clips;
1461 		u32 clipcount = p->fmt.win.clipcount;
1462 		void __user *bitmap = p->fmt.win.bitmap;
1463 
1464 		memset(&p->fmt, 0, sizeof(p->fmt));
1465 		p->fmt.win.clips = clips;
1466 		p->fmt.win.clipcount = clipcount;
1467 		p->fmt.win.bitmap = bitmap;
1468 		break;
1469 	}
1470 	default:
1471 		memset(&p->fmt, 0, sizeof(p->fmt));
1472 		break;
1473 	}
1474 
1475 	switch (p->type) {
1476 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1477 		if (unlikely(!ops->vidioc_g_fmt_vid_cap))
1478 			break;
1479 		p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1480 		ret = ops->vidioc_g_fmt_vid_cap(file, fh, arg);
1481 		/* just in case the driver zeroed it again */
1482 		p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1483 		return ret;
1484 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1485 		return ops->vidioc_g_fmt_vid_cap_mplane(file, fh, arg);
1486 	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1487 		return ops->vidioc_g_fmt_vid_overlay(file, fh, arg);
1488 	case V4L2_BUF_TYPE_VBI_CAPTURE:
1489 		return ops->vidioc_g_fmt_vbi_cap(file, fh, arg);
1490 	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1491 		return ops->vidioc_g_fmt_sliced_vbi_cap(file, fh, arg);
1492 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1493 		if (unlikely(!ops->vidioc_g_fmt_vid_out))
1494 			break;
1495 		p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1496 		ret = ops->vidioc_g_fmt_vid_out(file, fh, arg);
1497 		/* just in case the driver zeroed it again */
1498 		p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1499 		return ret;
1500 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1501 		return ops->vidioc_g_fmt_vid_out_mplane(file, fh, arg);
1502 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1503 		return ops->vidioc_g_fmt_vid_out_overlay(file, fh, arg);
1504 	case V4L2_BUF_TYPE_VBI_OUTPUT:
1505 		return ops->vidioc_g_fmt_vbi_out(file, fh, arg);
1506 	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1507 		return ops->vidioc_g_fmt_sliced_vbi_out(file, fh, arg);
1508 	case V4L2_BUF_TYPE_SDR_CAPTURE:
1509 		return ops->vidioc_g_fmt_sdr_cap(file, fh, arg);
1510 	case V4L2_BUF_TYPE_SDR_OUTPUT:
1511 		return ops->vidioc_g_fmt_sdr_out(file, fh, arg);
1512 	case V4L2_BUF_TYPE_META_CAPTURE:
1513 		return ops->vidioc_g_fmt_meta_cap(file, fh, arg);
1514 	case V4L2_BUF_TYPE_META_OUTPUT:
1515 		return ops->vidioc_g_fmt_meta_out(file, fh, arg);
1516 	}
1517 	return -EINVAL;
1518 }
1519 
1520 static void v4l_pix_format_touch(struct v4l2_pix_format *p)
1521 {
1522 	/*
1523 	 * The v4l2_pix_format structure contains fields that make no sense for
1524 	 * touch. Set them to default values in this case.
1525 	 */
1526 
1527 	p->field = V4L2_FIELD_NONE;
1528 	p->colorspace = V4L2_COLORSPACE_RAW;
1529 	p->flags = 0;
1530 	p->ycbcr_enc = 0;
1531 	p->quantization = 0;
1532 	p->xfer_func = 0;
1533 }
1534 
1535 static int v4l_s_fmt(const struct v4l2_ioctl_ops *ops,
1536 				struct file *file, void *fh, void *arg)
1537 {
1538 	struct v4l2_format *p = arg;
1539 	struct video_device *vfd = video_devdata(file);
1540 	int ret = check_fmt(file, p->type);
1541 	unsigned int i;
1542 
1543 	if (ret)
1544 		return ret;
1545 
1546 	ret = v4l_enable_media_source(vfd);
1547 	if (ret)
1548 		return ret;
1549 	v4l_sanitize_format(p);
1550 
1551 	switch (p->type) {
1552 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1553 		if (unlikely(!ops->vidioc_s_fmt_vid_cap))
1554 			break;
1555 		CLEAR_AFTER_FIELD(p, fmt.pix);
1556 		ret = ops->vidioc_s_fmt_vid_cap(file, fh, arg);
1557 		/* just in case the driver zeroed it again */
1558 		p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1559 		if (vfd->vfl_type == VFL_TYPE_TOUCH)
1560 			v4l_pix_format_touch(&p->fmt.pix);
1561 		return ret;
1562 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1563 		if (unlikely(!ops->vidioc_s_fmt_vid_cap_mplane))
1564 			break;
1565 		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1566 		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1567 			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1568 					  bytesperline);
1569 		return ops->vidioc_s_fmt_vid_cap_mplane(file, fh, arg);
1570 	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1571 		if (unlikely(!ops->vidioc_s_fmt_vid_overlay))
1572 			break;
1573 		CLEAR_AFTER_FIELD(p, fmt.win);
1574 		return ops->vidioc_s_fmt_vid_overlay(file, fh, arg);
1575 	case V4L2_BUF_TYPE_VBI_CAPTURE:
1576 		if (unlikely(!ops->vidioc_s_fmt_vbi_cap))
1577 			break;
1578 		CLEAR_AFTER_FIELD(p, fmt.vbi);
1579 		return ops->vidioc_s_fmt_vbi_cap(file, fh, arg);
1580 	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1581 		if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_cap))
1582 			break;
1583 		CLEAR_AFTER_FIELD(p, fmt.sliced);
1584 		return ops->vidioc_s_fmt_sliced_vbi_cap(file, fh, arg);
1585 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1586 		if (unlikely(!ops->vidioc_s_fmt_vid_out))
1587 			break;
1588 		CLEAR_AFTER_FIELD(p, fmt.pix);
1589 		ret = ops->vidioc_s_fmt_vid_out(file, fh, arg);
1590 		/* just in case the driver zeroed it again */
1591 		p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1592 		return ret;
1593 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1594 		if (unlikely(!ops->vidioc_s_fmt_vid_out_mplane))
1595 			break;
1596 		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1597 		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1598 			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1599 					  bytesperline);
1600 		return ops->vidioc_s_fmt_vid_out_mplane(file, fh, arg);
1601 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1602 		if (unlikely(!ops->vidioc_s_fmt_vid_out_overlay))
1603 			break;
1604 		CLEAR_AFTER_FIELD(p, fmt.win);
1605 		return ops->vidioc_s_fmt_vid_out_overlay(file, fh, arg);
1606 	case V4L2_BUF_TYPE_VBI_OUTPUT:
1607 		if (unlikely(!ops->vidioc_s_fmt_vbi_out))
1608 			break;
1609 		CLEAR_AFTER_FIELD(p, fmt.vbi);
1610 		return ops->vidioc_s_fmt_vbi_out(file, fh, arg);
1611 	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1612 		if (unlikely(!ops->vidioc_s_fmt_sliced_vbi_out))
1613 			break;
1614 		CLEAR_AFTER_FIELD(p, fmt.sliced);
1615 		return ops->vidioc_s_fmt_sliced_vbi_out(file, fh, arg);
1616 	case V4L2_BUF_TYPE_SDR_CAPTURE:
1617 		if (unlikely(!ops->vidioc_s_fmt_sdr_cap))
1618 			break;
1619 		CLEAR_AFTER_FIELD(p, fmt.sdr);
1620 		return ops->vidioc_s_fmt_sdr_cap(file, fh, arg);
1621 	case V4L2_BUF_TYPE_SDR_OUTPUT:
1622 		if (unlikely(!ops->vidioc_s_fmt_sdr_out))
1623 			break;
1624 		CLEAR_AFTER_FIELD(p, fmt.sdr);
1625 		return ops->vidioc_s_fmt_sdr_out(file, fh, arg);
1626 	case V4L2_BUF_TYPE_META_CAPTURE:
1627 		if (unlikely(!ops->vidioc_s_fmt_meta_cap))
1628 			break;
1629 		CLEAR_AFTER_FIELD(p, fmt.meta);
1630 		return ops->vidioc_s_fmt_meta_cap(file, fh, arg);
1631 	case V4L2_BUF_TYPE_META_OUTPUT:
1632 		if (unlikely(!ops->vidioc_s_fmt_meta_out))
1633 			break;
1634 		CLEAR_AFTER_FIELD(p, fmt.meta);
1635 		return ops->vidioc_s_fmt_meta_out(file, fh, arg);
1636 	}
1637 	return -EINVAL;
1638 }
1639 
1640 static int v4l_try_fmt(const struct v4l2_ioctl_ops *ops,
1641 				struct file *file, void *fh, void *arg)
1642 {
1643 	struct v4l2_format *p = arg;
1644 	int ret = check_fmt(file, p->type);
1645 	unsigned int i;
1646 
1647 	if (ret)
1648 		return ret;
1649 
1650 	v4l_sanitize_format(p);
1651 
1652 	switch (p->type) {
1653 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1654 		if (unlikely(!ops->vidioc_try_fmt_vid_cap))
1655 			break;
1656 		CLEAR_AFTER_FIELD(p, fmt.pix);
1657 		ret = ops->vidioc_try_fmt_vid_cap(file, fh, arg);
1658 		/* just in case the driver zeroed it again */
1659 		p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1660 		return ret;
1661 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1662 		if (unlikely(!ops->vidioc_try_fmt_vid_cap_mplane))
1663 			break;
1664 		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1665 		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1666 			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1667 					  bytesperline);
1668 		return ops->vidioc_try_fmt_vid_cap_mplane(file, fh, arg);
1669 	case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1670 		if (unlikely(!ops->vidioc_try_fmt_vid_overlay))
1671 			break;
1672 		CLEAR_AFTER_FIELD(p, fmt.win);
1673 		return ops->vidioc_try_fmt_vid_overlay(file, fh, arg);
1674 	case V4L2_BUF_TYPE_VBI_CAPTURE:
1675 		if (unlikely(!ops->vidioc_try_fmt_vbi_cap))
1676 			break;
1677 		CLEAR_AFTER_FIELD(p, fmt.vbi);
1678 		return ops->vidioc_try_fmt_vbi_cap(file, fh, arg);
1679 	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
1680 		if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_cap))
1681 			break;
1682 		CLEAR_AFTER_FIELD(p, fmt.sliced);
1683 		return ops->vidioc_try_fmt_sliced_vbi_cap(file, fh, arg);
1684 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1685 		if (unlikely(!ops->vidioc_try_fmt_vid_out))
1686 			break;
1687 		CLEAR_AFTER_FIELD(p, fmt.pix);
1688 		ret = ops->vidioc_try_fmt_vid_out(file, fh, arg);
1689 		/* just in case the driver zeroed it again */
1690 		p->fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1691 		return ret;
1692 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1693 		if (unlikely(!ops->vidioc_try_fmt_vid_out_mplane))
1694 			break;
1695 		CLEAR_AFTER_FIELD(p, fmt.pix_mp.xfer_func);
1696 		for (i = 0; i < p->fmt.pix_mp.num_planes; i++)
1697 			CLEAR_AFTER_FIELD(&p->fmt.pix_mp.plane_fmt[i],
1698 					  bytesperline);
1699 		return ops->vidioc_try_fmt_vid_out_mplane(file, fh, arg);
1700 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
1701 		if (unlikely(!ops->vidioc_try_fmt_vid_out_overlay))
1702 			break;
1703 		CLEAR_AFTER_FIELD(p, fmt.win);
1704 		return ops->vidioc_try_fmt_vid_out_overlay(file, fh, arg);
1705 	case V4L2_BUF_TYPE_VBI_OUTPUT:
1706 		if (unlikely(!ops->vidioc_try_fmt_vbi_out))
1707 			break;
1708 		CLEAR_AFTER_FIELD(p, fmt.vbi);
1709 		return ops->vidioc_try_fmt_vbi_out(file, fh, arg);
1710 	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
1711 		if (unlikely(!ops->vidioc_try_fmt_sliced_vbi_out))
1712 			break;
1713 		CLEAR_AFTER_FIELD(p, fmt.sliced);
1714 		return ops->vidioc_try_fmt_sliced_vbi_out(file, fh, arg);
1715 	case V4L2_BUF_TYPE_SDR_CAPTURE:
1716 		if (unlikely(!ops->vidioc_try_fmt_sdr_cap))
1717 			break;
1718 		CLEAR_AFTER_FIELD(p, fmt.sdr);
1719 		return ops->vidioc_try_fmt_sdr_cap(file, fh, arg);
1720 	case V4L2_BUF_TYPE_SDR_OUTPUT:
1721 		if (unlikely(!ops->vidioc_try_fmt_sdr_out))
1722 			break;
1723 		CLEAR_AFTER_FIELD(p, fmt.sdr);
1724 		return ops->vidioc_try_fmt_sdr_out(file, fh, arg);
1725 	case V4L2_BUF_TYPE_META_CAPTURE:
1726 		if (unlikely(!ops->vidioc_try_fmt_meta_cap))
1727 			break;
1728 		CLEAR_AFTER_FIELD(p, fmt.meta);
1729 		return ops->vidioc_try_fmt_meta_cap(file, fh, arg);
1730 	case V4L2_BUF_TYPE_META_OUTPUT:
1731 		if (unlikely(!ops->vidioc_try_fmt_meta_out))
1732 			break;
1733 		CLEAR_AFTER_FIELD(p, fmt.meta);
1734 		return ops->vidioc_try_fmt_meta_out(file, fh, arg);
1735 	}
1736 	return -EINVAL;
1737 }
1738 
1739 static int v4l_streamon(const struct v4l2_ioctl_ops *ops,
1740 				struct file *file, void *fh, void *arg)
1741 {
1742 	return ops->vidioc_streamon(file, fh, *(unsigned int *)arg);
1743 }
1744 
1745 static int v4l_streamoff(const struct v4l2_ioctl_ops *ops,
1746 				struct file *file, void *fh, void *arg)
1747 {
1748 	return ops->vidioc_streamoff(file, fh, *(unsigned int *)arg);
1749 }
1750 
1751 static int v4l_g_tuner(const struct v4l2_ioctl_ops *ops,
1752 				struct file *file, void *fh, void *arg)
1753 {
1754 	struct video_device *vfd = video_devdata(file);
1755 	struct v4l2_tuner *p = arg;
1756 	int err;
1757 
1758 	p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1759 			V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1760 	err = ops->vidioc_g_tuner(file, fh, p);
1761 	if (!err)
1762 		p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1763 	return err;
1764 }
1765 
1766 static int v4l_s_tuner(const struct v4l2_ioctl_ops *ops,
1767 				struct file *file, void *fh, void *arg)
1768 {
1769 	struct video_device *vfd = video_devdata(file);
1770 	struct v4l2_tuner *p = arg;
1771 	int ret;
1772 
1773 	ret = v4l_enable_media_source(vfd);
1774 	if (ret)
1775 		return ret;
1776 	p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1777 			V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1778 	return ops->vidioc_s_tuner(file, fh, p);
1779 }
1780 
1781 static int v4l_g_modulator(const struct v4l2_ioctl_ops *ops,
1782 				struct file *file, void *fh, void *arg)
1783 {
1784 	struct video_device *vfd = video_devdata(file);
1785 	struct v4l2_modulator *p = arg;
1786 	int err;
1787 
1788 	if (vfd->vfl_type == VFL_TYPE_RADIO)
1789 		p->type = V4L2_TUNER_RADIO;
1790 
1791 	err = ops->vidioc_g_modulator(file, fh, p);
1792 	if (!err)
1793 		p->capability |= V4L2_TUNER_CAP_FREQ_BANDS;
1794 	return err;
1795 }
1796 
1797 static int v4l_s_modulator(const struct v4l2_ioctl_ops *ops,
1798 				struct file *file, void *fh, void *arg)
1799 {
1800 	struct video_device *vfd = video_devdata(file);
1801 	struct v4l2_modulator *p = arg;
1802 
1803 	if (vfd->vfl_type == VFL_TYPE_RADIO)
1804 		p->type = V4L2_TUNER_RADIO;
1805 
1806 	return ops->vidioc_s_modulator(file, fh, p);
1807 }
1808 
1809 static int v4l_g_frequency(const struct v4l2_ioctl_ops *ops,
1810 				struct file *file, void *fh, void *arg)
1811 {
1812 	struct video_device *vfd = video_devdata(file);
1813 	struct v4l2_frequency *p = arg;
1814 
1815 	if (vfd->vfl_type == VFL_TYPE_SDR)
1816 		p->type = V4L2_TUNER_SDR;
1817 	else
1818 		p->type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1819 				V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1820 	return ops->vidioc_g_frequency(file, fh, p);
1821 }
1822 
1823 static int v4l_s_frequency(const struct v4l2_ioctl_ops *ops,
1824 				struct file *file, void *fh, void *arg)
1825 {
1826 	struct video_device *vfd = video_devdata(file);
1827 	const struct v4l2_frequency *p = arg;
1828 	enum v4l2_tuner_type type;
1829 	int ret;
1830 
1831 	ret = v4l_enable_media_source(vfd);
1832 	if (ret)
1833 		return ret;
1834 	if (vfd->vfl_type == VFL_TYPE_SDR) {
1835 		if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
1836 			return -EINVAL;
1837 	} else {
1838 		type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1839 				V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1840 		if (type != p->type)
1841 			return -EINVAL;
1842 	}
1843 	return ops->vidioc_s_frequency(file, fh, p);
1844 }
1845 
1846 static int v4l_enumstd(const struct v4l2_ioctl_ops *ops,
1847 				struct file *file, void *fh, void *arg)
1848 {
1849 	struct video_device *vfd = video_devdata(file);
1850 	struct v4l2_standard *p = arg;
1851 
1852 	return v4l_video_std_enumstd(p, vfd->tvnorms);
1853 }
1854 
1855 static int v4l_s_std(const struct v4l2_ioctl_ops *ops,
1856 				struct file *file, void *fh, void *arg)
1857 {
1858 	struct video_device *vfd = video_devdata(file);
1859 	v4l2_std_id id = *(v4l2_std_id *)arg, norm;
1860 	int ret;
1861 
1862 	ret = v4l_enable_media_source(vfd);
1863 	if (ret)
1864 		return ret;
1865 	norm = id & vfd->tvnorms;
1866 	if (vfd->tvnorms && !norm)	/* Check if std is supported */
1867 		return -EINVAL;
1868 
1869 	/* Calls the specific handler */
1870 	return ops->vidioc_s_std(file, fh, norm);
1871 }
1872 
1873 static int v4l_querystd(const struct v4l2_ioctl_ops *ops,
1874 				struct file *file, void *fh, void *arg)
1875 {
1876 	struct video_device *vfd = video_devdata(file);
1877 	v4l2_std_id *p = arg;
1878 	int ret;
1879 
1880 	ret = v4l_enable_media_source(vfd);
1881 	if (ret)
1882 		return ret;
1883 	/*
1884 	 * If no signal is detected, then the driver should return
1885 	 * V4L2_STD_UNKNOWN. Otherwise it should return tvnorms with
1886 	 * any standards that do not apply removed.
1887 	 *
1888 	 * This means that tuners, audio and video decoders can join
1889 	 * their efforts to improve the standards detection.
1890 	 */
1891 	*p = vfd->tvnorms;
1892 	return ops->vidioc_querystd(file, fh, arg);
1893 }
1894 
1895 static int v4l_s_hw_freq_seek(const struct v4l2_ioctl_ops *ops,
1896 				struct file *file, void *fh, void *arg)
1897 {
1898 	struct video_device *vfd = video_devdata(file);
1899 	struct v4l2_hw_freq_seek *p = arg;
1900 	enum v4l2_tuner_type type;
1901 	int ret;
1902 
1903 	ret = v4l_enable_media_source(vfd);
1904 	if (ret)
1905 		return ret;
1906 	/* s_hw_freq_seek is not supported for SDR for now */
1907 	if (vfd->vfl_type == VFL_TYPE_SDR)
1908 		return -EINVAL;
1909 
1910 	type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
1911 		V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1912 	if (p->type != type)
1913 		return -EINVAL;
1914 	return ops->vidioc_s_hw_freq_seek(file, fh, p);
1915 }
1916 
1917 static int v4l_overlay(const struct v4l2_ioctl_ops *ops,
1918 				struct file *file, void *fh, void *arg)
1919 {
1920 	return ops->vidioc_overlay(file, fh, *(unsigned int *)arg);
1921 }
1922 
1923 static int v4l_reqbufs(const struct v4l2_ioctl_ops *ops,
1924 				struct file *file, void *fh, void *arg)
1925 {
1926 	struct v4l2_requestbuffers *p = arg;
1927 	int ret = check_fmt(file, p->type);
1928 
1929 	if (ret)
1930 		return ret;
1931 
1932 	CLEAR_AFTER_FIELD(p, capabilities);
1933 
1934 	return ops->vidioc_reqbufs(file, fh, p);
1935 }
1936 
1937 static int v4l_querybuf(const struct v4l2_ioctl_ops *ops,
1938 				struct file *file, void *fh, void *arg)
1939 {
1940 	struct v4l2_buffer *p = arg;
1941 	int ret = check_fmt(file, p->type);
1942 
1943 	return ret ? ret : ops->vidioc_querybuf(file, fh, p);
1944 }
1945 
1946 static int v4l_qbuf(const struct v4l2_ioctl_ops *ops,
1947 				struct file *file, void *fh, void *arg)
1948 {
1949 	struct v4l2_buffer *p = arg;
1950 	int ret = check_fmt(file, p->type);
1951 
1952 	return ret ? ret : ops->vidioc_qbuf(file, fh, p);
1953 }
1954 
1955 static int v4l_dqbuf(const struct v4l2_ioctl_ops *ops,
1956 				struct file *file, void *fh, void *arg)
1957 {
1958 	struct v4l2_buffer *p = arg;
1959 	int ret = check_fmt(file, p->type);
1960 
1961 	return ret ? ret : ops->vidioc_dqbuf(file, fh, p);
1962 }
1963 
1964 static int v4l_create_bufs(const struct v4l2_ioctl_ops *ops,
1965 				struct file *file, void *fh, void *arg)
1966 {
1967 	struct v4l2_create_buffers *create = arg;
1968 	int ret = check_fmt(file, create->format.type);
1969 
1970 	if (ret)
1971 		return ret;
1972 
1973 	CLEAR_AFTER_FIELD(create, capabilities);
1974 
1975 	v4l_sanitize_format(&create->format);
1976 
1977 	ret = ops->vidioc_create_bufs(file, fh, create);
1978 
1979 	if (create->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1980 	    create->format.type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
1981 		create->format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
1982 
1983 	return ret;
1984 }
1985 
1986 static int v4l_prepare_buf(const struct v4l2_ioctl_ops *ops,
1987 				struct file *file, void *fh, void *arg)
1988 {
1989 	struct v4l2_buffer *b = arg;
1990 	int ret = check_fmt(file, b->type);
1991 
1992 	return ret ? ret : ops->vidioc_prepare_buf(file, fh, b);
1993 }
1994 
1995 static int v4l_g_parm(const struct v4l2_ioctl_ops *ops,
1996 				struct file *file, void *fh, void *arg)
1997 {
1998 	struct v4l2_streamparm *p = arg;
1999 	v4l2_std_id std;
2000 	int ret = check_fmt(file, p->type);
2001 
2002 	if (ret)
2003 		return ret;
2004 	if (ops->vidioc_g_parm)
2005 		return ops->vidioc_g_parm(file, fh, p);
2006 	if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
2007 	    p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2008 		return -EINVAL;
2009 	p->parm.capture.readbuffers = 2;
2010 	ret = ops->vidioc_g_std(file, fh, &std);
2011 	if (ret == 0)
2012 		v4l2_video_std_frame_period(std, &p->parm.capture.timeperframe);
2013 	return ret;
2014 }
2015 
2016 static int v4l_s_parm(const struct v4l2_ioctl_ops *ops,
2017 				struct file *file, void *fh, void *arg)
2018 {
2019 	struct v4l2_streamparm *p = arg;
2020 	int ret = check_fmt(file, p->type);
2021 
2022 	if (ret)
2023 		return ret;
2024 
2025 	/* Note: extendedmode is never used in drivers */
2026 	if (V4L2_TYPE_IS_OUTPUT(p->type)) {
2027 		memset(p->parm.output.reserved, 0,
2028 		       sizeof(p->parm.output.reserved));
2029 		p->parm.output.extendedmode = 0;
2030 		p->parm.output.outputmode &= V4L2_MODE_HIGHQUALITY;
2031 	} else {
2032 		memset(p->parm.capture.reserved, 0,
2033 		       sizeof(p->parm.capture.reserved));
2034 		p->parm.capture.extendedmode = 0;
2035 		p->parm.capture.capturemode &= V4L2_MODE_HIGHQUALITY;
2036 	}
2037 	return ops->vidioc_s_parm(file, fh, p);
2038 }
2039 
2040 static int v4l_queryctrl(const struct v4l2_ioctl_ops *ops,
2041 				struct file *file, void *fh, void *arg)
2042 {
2043 	struct video_device *vfd = video_devdata(file);
2044 	struct v4l2_queryctrl *p = arg;
2045 	struct v4l2_fh *vfh =
2046 		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2047 
2048 	if (vfh && vfh->ctrl_handler)
2049 		return v4l2_queryctrl(vfh->ctrl_handler, p);
2050 	if (vfd->ctrl_handler)
2051 		return v4l2_queryctrl(vfd->ctrl_handler, p);
2052 	if (ops->vidioc_queryctrl)
2053 		return ops->vidioc_queryctrl(file, fh, p);
2054 	return -ENOTTY;
2055 }
2056 
2057 static int v4l_query_ext_ctrl(const struct v4l2_ioctl_ops *ops,
2058 				struct file *file, void *fh, void *arg)
2059 {
2060 	struct video_device *vfd = video_devdata(file);
2061 	struct v4l2_query_ext_ctrl *p = arg;
2062 	struct v4l2_fh *vfh =
2063 		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2064 
2065 	if (vfh && vfh->ctrl_handler)
2066 		return v4l2_query_ext_ctrl(vfh->ctrl_handler, p);
2067 	if (vfd->ctrl_handler)
2068 		return v4l2_query_ext_ctrl(vfd->ctrl_handler, p);
2069 	if (ops->vidioc_query_ext_ctrl)
2070 		return ops->vidioc_query_ext_ctrl(file, fh, p);
2071 	return -ENOTTY;
2072 }
2073 
2074 static int v4l_querymenu(const struct v4l2_ioctl_ops *ops,
2075 				struct file *file, void *fh, void *arg)
2076 {
2077 	struct video_device *vfd = video_devdata(file);
2078 	struct v4l2_querymenu *p = arg;
2079 	struct v4l2_fh *vfh =
2080 		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2081 
2082 	if (vfh && vfh->ctrl_handler)
2083 		return v4l2_querymenu(vfh->ctrl_handler, p);
2084 	if (vfd->ctrl_handler)
2085 		return v4l2_querymenu(vfd->ctrl_handler, p);
2086 	if (ops->vidioc_querymenu)
2087 		return ops->vidioc_querymenu(file, fh, p);
2088 	return -ENOTTY;
2089 }
2090 
2091 static int v4l_g_ctrl(const struct v4l2_ioctl_ops *ops,
2092 				struct file *file, void *fh, void *arg)
2093 {
2094 	struct video_device *vfd = video_devdata(file);
2095 	struct v4l2_control *p = arg;
2096 	struct v4l2_fh *vfh =
2097 		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2098 	struct v4l2_ext_controls ctrls;
2099 	struct v4l2_ext_control ctrl;
2100 
2101 	if (vfh && vfh->ctrl_handler)
2102 		return v4l2_g_ctrl(vfh->ctrl_handler, p);
2103 	if (vfd->ctrl_handler)
2104 		return v4l2_g_ctrl(vfd->ctrl_handler, p);
2105 	if (ops->vidioc_g_ctrl)
2106 		return ops->vidioc_g_ctrl(file, fh, p);
2107 	if (ops->vidioc_g_ext_ctrls == NULL)
2108 		return -ENOTTY;
2109 
2110 	ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2111 	ctrls.count = 1;
2112 	ctrls.controls = &ctrl;
2113 	ctrl.id = p->id;
2114 	ctrl.value = p->value;
2115 	if (check_ext_ctrls(&ctrls, 1)) {
2116 		int ret = ops->vidioc_g_ext_ctrls(file, fh, &ctrls);
2117 
2118 		if (ret == 0)
2119 			p->value = ctrl.value;
2120 		return ret;
2121 	}
2122 	return -EINVAL;
2123 }
2124 
2125 static int v4l_s_ctrl(const struct v4l2_ioctl_ops *ops,
2126 				struct file *file, void *fh, void *arg)
2127 {
2128 	struct video_device *vfd = video_devdata(file);
2129 	struct v4l2_control *p = arg;
2130 	struct v4l2_fh *vfh =
2131 		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2132 	struct v4l2_ext_controls ctrls;
2133 	struct v4l2_ext_control ctrl;
2134 
2135 	if (vfh && vfh->ctrl_handler)
2136 		return v4l2_s_ctrl(vfh, vfh->ctrl_handler, p);
2137 	if (vfd->ctrl_handler)
2138 		return v4l2_s_ctrl(NULL, vfd->ctrl_handler, p);
2139 	if (ops->vidioc_s_ctrl)
2140 		return ops->vidioc_s_ctrl(file, fh, p);
2141 	if (ops->vidioc_s_ext_ctrls == NULL)
2142 		return -ENOTTY;
2143 
2144 	ctrls.which = V4L2_CTRL_ID2WHICH(p->id);
2145 	ctrls.count = 1;
2146 	ctrls.controls = &ctrl;
2147 	ctrl.id = p->id;
2148 	ctrl.value = p->value;
2149 	if (check_ext_ctrls(&ctrls, 1))
2150 		return ops->vidioc_s_ext_ctrls(file, fh, &ctrls);
2151 	return -EINVAL;
2152 }
2153 
2154 static int v4l_g_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2155 				struct file *file, void *fh, void *arg)
2156 {
2157 	struct video_device *vfd = video_devdata(file);
2158 	struct v4l2_ext_controls *p = arg;
2159 	struct v4l2_fh *vfh =
2160 		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2161 
2162 	p->error_idx = p->count;
2163 	if (vfh && vfh->ctrl_handler)
2164 		return v4l2_g_ext_ctrls(vfh->ctrl_handler, vfd->v4l2_dev->mdev, p);
2165 	if (vfd->ctrl_handler)
2166 		return v4l2_g_ext_ctrls(vfd->ctrl_handler, vfd->v4l2_dev->mdev, p);
2167 	if (ops->vidioc_g_ext_ctrls == NULL)
2168 		return -ENOTTY;
2169 	return check_ext_ctrls(p, 0) ? ops->vidioc_g_ext_ctrls(file, fh, p) :
2170 					-EINVAL;
2171 }
2172 
2173 static int v4l_s_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2174 				struct file *file, void *fh, void *arg)
2175 {
2176 	struct video_device *vfd = video_devdata(file);
2177 	struct v4l2_ext_controls *p = arg;
2178 	struct v4l2_fh *vfh =
2179 		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2180 
2181 	p->error_idx = p->count;
2182 	if (vfh && vfh->ctrl_handler)
2183 		return v4l2_s_ext_ctrls(vfh, vfh->ctrl_handler, vfd->v4l2_dev->mdev, p);
2184 	if (vfd->ctrl_handler)
2185 		return v4l2_s_ext_ctrls(NULL, vfd->ctrl_handler, vfd->v4l2_dev->mdev, p);
2186 	if (ops->vidioc_s_ext_ctrls == NULL)
2187 		return -ENOTTY;
2188 	return check_ext_ctrls(p, 0) ? ops->vidioc_s_ext_ctrls(file, fh, p) :
2189 					-EINVAL;
2190 }
2191 
2192 static int v4l_try_ext_ctrls(const struct v4l2_ioctl_ops *ops,
2193 				struct file *file, void *fh, void *arg)
2194 {
2195 	struct video_device *vfd = video_devdata(file);
2196 	struct v4l2_ext_controls *p = arg;
2197 	struct v4l2_fh *vfh =
2198 		test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags) ? fh : NULL;
2199 
2200 	p->error_idx = p->count;
2201 	if (vfh && vfh->ctrl_handler)
2202 		return v4l2_try_ext_ctrls(vfh->ctrl_handler, vfd->v4l2_dev->mdev, p);
2203 	if (vfd->ctrl_handler)
2204 		return v4l2_try_ext_ctrls(vfd->ctrl_handler, vfd->v4l2_dev->mdev, p);
2205 	if (ops->vidioc_try_ext_ctrls == NULL)
2206 		return -ENOTTY;
2207 	return check_ext_ctrls(p, 0) ? ops->vidioc_try_ext_ctrls(file, fh, p) :
2208 					-EINVAL;
2209 }
2210 
2211 /*
2212  * The selection API specified originally that the _MPLANE buffer types
2213  * shouldn't be used. The reasons for this are lost in the mists of time
2214  * (or just really crappy memories). Regardless, this is really annoying
2215  * for userspace. So to keep things simple we map _MPLANE buffer types
2216  * to their 'regular' counterparts before calling the driver. And we
2217  * restore it afterwards. This way applications can use either buffer
2218  * type and drivers don't need to check for both.
2219  */
2220 static int v4l_g_selection(const struct v4l2_ioctl_ops *ops,
2221 			   struct file *file, void *fh, void *arg)
2222 {
2223 	struct v4l2_selection *p = arg;
2224 	u32 old_type = p->type;
2225 	int ret;
2226 
2227 	if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2228 		p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2229 	else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2230 		p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2231 	ret = ops->vidioc_g_selection(file, fh, p);
2232 	p->type = old_type;
2233 	return ret;
2234 }
2235 
2236 static int v4l_s_selection(const struct v4l2_ioctl_ops *ops,
2237 			   struct file *file, void *fh, void *arg)
2238 {
2239 	struct v4l2_selection *p = arg;
2240 	u32 old_type = p->type;
2241 	int ret;
2242 
2243 	if (p->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2244 		p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2245 	else if (p->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2246 		p->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2247 	ret = ops->vidioc_s_selection(file, fh, p);
2248 	p->type = old_type;
2249 	return ret;
2250 }
2251 
2252 static int v4l_g_crop(const struct v4l2_ioctl_ops *ops,
2253 				struct file *file, void *fh, void *arg)
2254 {
2255 	struct video_device *vfd = video_devdata(file);
2256 	struct v4l2_crop *p = arg;
2257 	struct v4l2_selection s = {
2258 		.type = p->type,
2259 	};
2260 	int ret;
2261 
2262 	/* simulate capture crop using selection api */
2263 
2264 	/* crop means compose for output devices */
2265 	if (V4L2_TYPE_IS_OUTPUT(p->type))
2266 		s.target = V4L2_SEL_TGT_COMPOSE;
2267 	else
2268 		s.target = V4L2_SEL_TGT_CROP;
2269 
2270 	if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2271 		s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2272 			V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2273 
2274 	ret = v4l_g_selection(ops, file, fh, &s);
2275 
2276 	/* copying results to old structure on success */
2277 	if (!ret)
2278 		p->c = s.r;
2279 	return ret;
2280 }
2281 
2282 static int v4l_s_crop(const struct v4l2_ioctl_ops *ops,
2283 				struct file *file, void *fh, void *arg)
2284 {
2285 	struct video_device *vfd = video_devdata(file);
2286 	struct v4l2_crop *p = arg;
2287 	struct v4l2_selection s = {
2288 		.type = p->type,
2289 		.r = p->c,
2290 	};
2291 
2292 	/* simulate capture crop using selection api */
2293 
2294 	/* crop means compose for output devices */
2295 	if (V4L2_TYPE_IS_OUTPUT(p->type))
2296 		s.target = V4L2_SEL_TGT_COMPOSE;
2297 	else
2298 		s.target = V4L2_SEL_TGT_CROP;
2299 
2300 	if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2301 		s.target = s.target == V4L2_SEL_TGT_COMPOSE ?
2302 			V4L2_SEL_TGT_CROP : V4L2_SEL_TGT_COMPOSE;
2303 
2304 	return v4l_s_selection(ops, file, fh, &s);
2305 }
2306 
2307 static int v4l_cropcap(const struct v4l2_ioctl_ops *ops,
2308 				struct file *file, void *fh, void *arg)
2309 {
2310 	struct video_device *vfd = video_devdata(file);
2311 	struct v4l2_cropcap *p = arg;
2312 	struct v4l2_selection s = { .type = p->type };
2313 	int ret = 0;
2314 
2315 	/* setting trivial pixelaspect */
2316 	p->pixelaspect.numerator = 1;
2317 	p->pixelaspect.denominator = 1;
2318 
2319 	if (s.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2320 		s.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2321 	else if (s.type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2322 		s.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2323 
2324 	/*
2325 	 * The determine_valid_ioctls() call already should ensure
2326 	 * that this can never happen, but just in case...
2327 	 */
2328 	if (WARN_ON(!ops->vidioc_g_selection))
2329 		return -ENOTTY;
2330 
2331 	if (ops->vidioc_g_pixelaspect)
2332 		ret = ops->vidioc_g_pixelaspect(file, fh, s.type,
2333 						&p->pixelaspect);
2334 
2335 	/*
2336 	 * Ignore ENOTTY or ENOIOCTLCMD error returns, just use the
2337 	 * square pixel aspect ratio in that case.
2338 	 */
2339 	if (ret && ret != -ENOTTY && ret != -ENOIOCTLCMD)
2340 		return ret;
2341 
2342 	/* Use g_selection() to fill in the bounds and defrect rectangles */
2343 
2344 	/* obtaining bounds */
2345 	if (V4L2_TYPE_IS_OUTPUT(p->type))
2346 		s.target = V4L2_SEL_TGT_COMPOSE_BOUNDS;
2347 	else
2348 		s.target = V4L2_SEL_TGT_CROP_BOUNDS;
2349 
2350 	if (test_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags))
2351 		s.target = s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS ?
2352 			V4L2_SEL_TGT_CROP_BOUNDS : V4L2_SEL_TGT_COMPOSE_BOUNDS;
2353 
2354 	ret = v4l_g_selection(ops, file, fh, &s);
2355 	if (ret)
2356 		return ret;
2357 	p->bounds = s.r;
2358 
2359 	/* obtaining defrect */
2360 	if (s.target == V4L2_SEL_TGT_COMPOSE_BOUNDS)
2361 		s.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
2362 	else
2363 		s.target = V4L2_SEL_TGT_CROP_DEFAULT;
2364 
2365 	ret = v4l_g_selection(ops, file, fh, &s);
2366 	if (ret)
2367 		return ret;
2368 	p->defrect = s.r;
2369 
2370 	return 0;
2371 }
2372 
2373 static int v4l_log_status(const struct v4l2_ioctl_ops *ops,
2374 				struct file *file, void *fh, void *arg)
2375 {
2376 	struct video_device *vfd = video_devdata(file);
2377 	int ret;
2378 
2379 	if (vfd->v4l2_dev)
2380 		pr_info("%s: =================  START STATUS  =================\n",
2381 			vfd->v4l2_dev->name);
2382 	ret = ops->vidioc_log_status(file, fh);
2383 	if (vfd->v4l2_dev)
2384 		pr_info("%s: ==================  END STATUS  ==================\n",
2385 			vfd->v4l2_dev->name);
2386 	return ret;
2387 }
2388 
2389 static int v4l_dbg_g_register(const struct v4l2_ioctl_ops *ops,
2390 				struct file *file, void *fh, void *arg)
2391 {
2392 #ifdef CONFIG_VIDEO_ADV_DEBUG
2393 	struct v4l2_dbg_register *p = arg;
2394 	struct video_device *vfd = video_devdata(file);
2395 	struct v4l2_subdev *sd;
2396 	int idx = 0;
2397 
2398 	if (!capable(CAP_SYS_ADMIN))
2399 		return -EPERM;
2400 	if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2401 		if (vfd->v4l2_dev == NULL)
2402 			return -EINVAL;
2403 		v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2404 			if (p->match.addr == idx++)
2405 				return v4l2_subdev_call(sd, core, g_register, p);
2406 		return -EINVAL;
2407 	}
2408 	if (ops->vidioc_g_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2409 	    (ops->vidioc_g_chip_info || p->match.addr == 0))
2410 		return ops->vidioc_g_register(file, fh, p);
2411 	return -EINVAL;
2412 #else
2413 	return -ENOTTY;
2414 #endif
2415 }
2416 
2417 static int v4l_dbg_s_register(const struct v4l2_ioctl_ops *ops,
2418 				struct file *file, void *fh, void *arg)
2419 {
2420 #ifdef CONFIG_VIDEO_ADV_DEBUG
2421 	const struct v4l2_dbg_register *p = arg;
2422 	struct video_device *vfd = video_devdata(file);
2423 	struct v4l2_subdev *sd;
2424 	int idx = 0;
2425 
2426 	if (!capable(CAP_SYS_ADMIN))
2427 		return -EPERM;
2428 	if (p->match.type == V4L2_CHIP_MATCH_SUBDEV) {
2429 		if (vfd->v4l2_dev == NULL)
2430 			return -EINVAL;
2431 		v4l2_device_for_each_subdev(sd, vfd->v4l2_dev)
2432 			if (p->match.addr == idx++)
2433 				return v4l2_subdev_call(sd, core, s_register, p);
2434 		return -EINVAL;
2435 	}
2436 	if (ops->vidioc_s_register && p->match.type == V4L2_CHIP_MATCH_BRIDGE &&
2437 	    (ops->vidioc_g_chip_info || p->match.addr == 0))
2438 		return ops->vidioc_s_register(file, fh, p);
2439 	return -EINVAL;
2440 #else
2441 	return -ENOTTY;
2442 #endif
2443 }
2444 
2445 static int v4l_dbg_g_chip_info(const struct v4l2_ioctl_ops *ops,
2446 				struct file *file, void *fh, void *arg)
2447 {
2448 #ifdef CONFIG_VIDEO_ADV_DEBUG
2449 	struct video_device *vfd = video_devdata(file);
2450 	struct v4l2_dbg_chip_info *p = arg;
2451 	struct v4l2_subdev *sd;
2452 	int idx = 0;
2453 
2454 	switch (p->match.type) {
2455 	case V4L2_CHIP_MATCH_BRIDGE:
2456 		if (ops->vidioc_s_register)
2457 			p->flags |= V4L2_CHIP_FL_WRITABLE;
2458 		if (ops->vidioc_g_register)
2459 			p->flags |= V4L2_CHIP_FL_READABLE;
2460 		strscpy(p->name, vfd->v4l2_dev->name, sizeof(p->name));
2461 		if (ops->vidioc_g_chip_info)
2462 			return ops->vidioc_g_chip_info(file, fh, arg);
2463 		if (p->match.addr)
2464 			return -EINVAL;
2465 		return 0;
2466 
2467 	case V4L2_CHIP_MATCH_SUBDEV:
2468 		if (vfd->v4l2_dev == NULL)
2469 			break;
2470 		v4l2_device_for_each_subdev(sd, vfd->v4l2_dev) {
2471 			if (p->match.addr != idx++)
2472 				continue;
2473 			if (sd->ops->core && sd->ops->core->s_register)
2474 				p->flags |= V4L2_CHIP_FL_WRITABLE;
2475 			if (sd->ops->core && sd->ops->core->g_register)
2476 				p->flags |= V4L2_CHIP_FL_READABLE;
2477 			strscpy(p->name, sd->name, sizeof(p->name));
2478 			return 0;
2479 		}
2480 		break;
2481 	}
2482 	return -EINVAL;
2483 #else
2484 	return -ENOTTY;
2485 #endif
2486 }
2487 
2488 static int v4l_dqevent(const struct v4l2_ioctl_ops *ops,
2489 				struct file *file, void *fh, void *arg)
2490 {
2491 	return v4l2_event_dequeue(fh, arg, file->f_flags & O_NONBLOCK);
2492 }
2493 
2494 static int v4l_subscribe_event(const struct v4l2_ioctl_ops *ops,
2495 				struct file *file, void *fh, void *arg)
2496 {
2497 	return ops->vidioc_subscribe_event(fh, arg);
2498 }
2499 
2500 static int v4l_unsubscribe_event(const struct v4l2_ioctl_ops *ops,
2501 				struct file *file, void *fh, void *arg)
2502 {
2503 	return ops->vidioc_unsubscribe_event(fh, arg);
2504 }
2505 
2506 static int v4l_g_sliced_vbi_cap(const struct v4l2_ioctl_ops *ops,
2507 				struct file *file, void *fh, void *arg)
2508 {
2509 	struct v4l2_sliced_vbi_cap *p = arg;
2510 	int ret = check_fmt(file, p->type);
2511 
2512 	if (ret)
2513 		return ret;
2514 
2515 	/* Clear up to type, everything after type is zeroed already */
2516 	memset(p, 0, offsetof(struct v4l2_sliced_vbi_cap, type));
2517 
2518 	return ops->vidioc_g_sliced_vbi_cap(file, fh, p);
2519 }
2520 
2521 static int v4l_enum_freq_bands(const struct v4l2_ioctl_ops *ops,
2522 				struct file *file, void *fh, void *arg)
2523 {
2524 	struct video_device *vfd = video_devdata(file);
2525 	struct v4l2_frequency_band *p = arg;
2526 	enum v4l2_tuner_type type;
2527 	int err;
2528 
2529 	if (vfd->vfl_type == VFL_TYPE_SDR) {
2530 		if (p->type != V4L2_TUNER_SDR && p->type != V4L2_TUNER_RF)
2531 			return -EINVAL;
2532 		type = p->type;
2533 	} else {
2534 		type = (vfd->vfl_type == VFL_TYPE_RADIO) ?
2535 				V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
2536 		if (type != p->type)
2537 			return -EINVAL;
2538 	}
2539 	if (ops->vidioc_enum_freq_bands) {
2540 		err = ops->vidioc_enum_freq_bands(file, fh, p);
2541 		if (err != -ENOTTY)
2542 			return err;
2543 	}
2544 	if (is_valid_ioctl(vfd, VIDIOC_G_TUNER)) {
2545 		struct v4l2_tuner t = {
2546 			.index = p->tuner,
2547 			.type = type,
2548 		};
2549 
2550 		if (p->index)
2551 			return -EINVAL;
2552 		err = ops->vidioc_g_tuner(file, fh, &t);
2553 		if (err)
2554 			return err;
2555 		p->capability = t.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2556 		p->rangelow = t.rangelow;
2557 		p->rangehigh = t.rangehigh;
2558 		p->modulation = (type == V4L2_TUNER_RADIO) ?
2559 			V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2560 		return 0;
2561 	}
2562 	if (is_valid_ioctl(vfd, VIDIOC_G_MODULATOR)) {
2563 		struct v4l2_modulator m = {
2564 			.index = p->tuner,
2565 		};
2566 
2567 		if (type != V4L2_TUNER_RADIO)
2568 			return -EINVAL;
2569 		if (p->index)
2570 			return -EINVAL;
2571 		err = ops->vidioc_g_modulator(file, fh, &m);
2572 		if (err)
2573 			return err;
2574 		p->capability = m.capability | V4L2_TUNER_CAP_FREQ_BANDS;
2575 		p->rangelow = m.rangelow;
2576 		p->rangehigh = m.rangehigh;
2577 		p->modulation = (type == V4L2_TUNER_RADIO) ?
2578 			V4L2_BAND_MODULATION_FM : V4L2_BAND_MODULATION_VSB;
2579 		return 0;
2580 	}
2581 	return -ENOTTY;
2582 }
2583 
2584 struct v4l2_ioctl_info {
2585 	unsigned int ioctl;
2586 	u32 flags;
2587 	const char * const name;
2588 	int (*func)(const struct v4l2_ioctl_ops *ops, struct file *file,
2589 		    void *fh, void *p);
2590 	void (*debug)(const void *arg, bool write_only);
2591 };
2592 
2593 /* This control needs a priority check */
2594 #define INFO_FL_PRIO		(1 << 0)
2595 /* This control can be valid if the filehandle passes a control handler. */
2596 #define INFO_FL_CTRL		(1 << 1)
2597 /* Queuing ioctl */
2598 #define INFO_FL_QUEUE		(1 << 2)
2599 /* Always copy back result, even on error */
2600 #define INFO_FL_ALWAYS_COPY	(1 << 3)
2601 /* Zero struct from after the field to the end */
2602 #define INFO_FL_CLEAR(v4l2_struct, field)			\
2603 	((offsetof(struct v4l2_struct, field) +			\
2604 	  sizeof(((struct v4l2_struct *)0)->field)) << 16)
2605 #define INFO_FL_CLEAR_MASK	(_IOC_SIZEMASK << 16)
2606 
2607 #define DEFINE_V4L_STUB_FUNC(_vidioc)				\
2608 	static int v4l_stub_ ## _vidioc(			\
2609 			const struct v4l2_ioctl_ops *ops,	\
2610 			struct file *file, void *fh, void *p)	\
2611 	{							\
2612 		return ops->vidioc_ ## _vidioc(file, fh, p);	\
2613 	}
2614 
2615 #define IOCTL_INFO(_ioctl, _func, _debug, _flags)		\
2616 	[_IOC_NR(_ioctl)] = {					\
2617 		.ioctl = _ioctl,				\
2618 		.flags = _flags,				\
2619 		.name = #_ioctl,				\
2620 		.func = _func,					\
2621 		.debug = _debug,				\
2622 	}
2623 
2624 DEFINE_V4L_STUB_FUNC(g_fbuf)
2625 DEFINE_V4L_STUB_FUNC(s_fbuf)
2626 DEFINE_V4L_STUB_FUNC(expbuf)
2627 DEFINE_V4L_STUB_FUNC(g_std)
2628 DEFINE_V4L_STUB_FUNC(g_audio)
2629 DEFINE_V4L_STUB_FUNC(s_audio)
2630 DEFINE_V4L_STUB_FUNC(g_input)
2631 DEFINE_V4L_STUB_FUNC(g_edid)
2632 DEFINE_V4L_STUB_FUNC(s_edid)
2633 DEFINE_V4L_STUB_FUNC(g_output)
2634 DEFINE_V4L_STUB_FUNC(g_audout)
2635 DEFINE_V4L_STUB_FUNC(s_audout)
2636 DEFINE_V4L_STUB_FUNC(g_jpegcomp)
2637 DEFINE_V4L_STUB_FUNC(s_jpegcomp)
2638 DEFINE_V4L_STUB_FUNC(enumaudio)
2639 DEFINE_V4L_STUB_FUNC(enumaudout)
2640 DEFINE_V4L_STUB_FUNC(enum_framesizes)
2641 DEFINE_V4L_STUB_FUNC(enum_frameintervals)
2642 DEFINE_V4L_STUB_FUNC(g_enc_index)
2643 DEFINE_V4L_STUB_FUNC(encoder_cmd)
2644 DEFINE_V4L_STUB_FUNC(try_encoder_cmd)
2645 DEFINE_V4L_STUB_FUNC(decoder_cmd)
2646 DEFINE_V4L_STUB_FUNC(try_decoder_cmd)
2647 DEFINE_V4L_STUB_FUNC(s_dv_timings)
2648 DEFINE_V4L_STUB_FUNC(g_dv_timings)
2649 DEFINE_V4L_STUB_FUNC(enum_dv_timings)
2650 DEFINE_V4L_STUB_FUNC(query_dv_timings)
2651 DEFINE_V4L_STUB_FUNC(dv_timings_cap)
2652 
2653 static const struct v4l2_ioctl_info v4l2_ioctls[] = {
2654 	IOCTL_INFO(VIDIOC_QUERYCAP, v4l_querycap, v4l_print_querycap, 0),
2655 	IOCTL_INFO(VIDIOC_ENUM_FMT, v4l_enum_fmt, v4l_print_fmtdesc, INFO_FL_CLEAR(v4l2_fmtdesc, type)),
2656 	IOCTL_INFO(VIDIOC_G_FMT, v4l_g_fmt, v4l_print_format, 0),
2657 	IOCTL_INFO(VIDIOC_S_FMT, v4l_s_fmt, v4l_print_format, INFO_FL_PRIO),
2658 	IOCTL_INFO(VIDIOC_REQBUFS, v4l_reqbufs, v4l_print_requestbuffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2659 	IOCTL_INFO(VIDIOC_QUERYBUF, v4l_querybuf, v4l_print_buffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_buffer, length)),
2660 	IOCTL_INFO(VIDIOC_G_FBUF, v4l_stub_g_fbuf, v4l_print_framebuffer, 0),
2661 	IOCTL_INFO(VIDIOC_S_FBUF, v4l_stub_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
2662 	IOCTL_INFO(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
2663 	IOCTL_INFO(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
2664 	IOCTL_INFO(VIDIOC_EXPBUF, v4l_stub_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
2665 	IOCTL_INFO(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
2666 	IOCTL_INFO(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2667 	IOCTL_INFO(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
2668 	IOCTL_INFO(VIDIOC_G_PARM, v4l_g_parm, v4l_print_streamparm, INFO_FL_CLEAR(v4l2_streamparm, type)),
2669 	IOCTL_INFO(VIDIOC_S_PARM, v4l_s_parm, v4l_print_streamparm, INFO_FL_PRIO),
2670 	IOCTL_INFO(VIDIOC_G_STD, v4l_stub_g_std, v4l_print_std, 0),
2671 	IOCTL_INFO(VIDIOC_S_STD, v4l_s_std, v4l_print_std, INFO_FL_PRIO),
2672 	IOCTL_INFO(VIDIOC_ENUMSTD, v4l_enumstd, v4l_print_standard, INFO_FL_CLEAR(v4l2_standard, index)),
2673 	IOCTL_INFO(VIDIOC_ENUMINPUT, v4l_enuminput, v4l_print_enuminput, INFO_FL_CLEAR(v4l2_input, index)),
2674 	IOCTL_INFO(VIDIOC_G_CTRL, v4l_g_ctrl, v4l_print_control, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_control, id)),
2675 	IOCTL_INFO(VIDIOC_S_CTRL, v4l_s_ctrl, v4l_print_control, INFO_FL_PRIO | INFO_FL_CTRL),
2676 	IOCTL_INFO(VIDIOC_G_TUNER, v4l_g_tuner, v4l_print_tuner, INFO_FL_CLEAR(v4l2_tuner, index)),
2677 	IOCTL_INFO(VIDIOC_S_TUNER, v4l_s_tuner, v4l_print_tuner, INFO_FL_PRIO),
2678 	IOCTL_INFO(VIDIOC_G_AUDIO, v4l_stub_g_audio, v4l_print_audio, 0),
2679 	IOCTL_INFO(VIDIOC_S_AUDIO, v4l_stub_s_audio, v4l_print_audio, INFO_FL_PRIO),
2680 	IOCTL_INFO(VIDIOC_QUERYCTRL, v4l_queryctrl, v4l_print_queryctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_queryctrl, id)),
2681 	IOCTL_INFO(VIDIOC_QUERYMENU, v4l_querymenu, v4l_print_querymenu, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_querymenu, index)),
2682 	IOCTL_INFO(VIDIOC_G_INPUT, v4l_stub_g_input, v4l_print_u32, 0),
2683 	IOCTL_INFO(VIDIOC_S_INPUT, v4l_s_input, v4l_print_u32, INFO_FL_PRIO),
2684 	IOCTL_INFO(VIDIOC_G_EDID, v4l_stub_g_edid, v4l_print_edid, INFO_FL_ALWAYS_COPY),
2685 	IOCTL_INFO(VIDIOC_S_EDID, v4l_stub_s_edid, v4l_print_edid, INFO_FL_PRIO | INFO_FL_ALWAYS_COPY),
2686 	IOCTL_INFO(VIDIOC_G_OUTPUT, v4l_stub_g_output, v4l_print_u32, 0),
2687 	IOCTL_INFO(VIDIOC_S_OUTPUT, v4l_s_output, v4l_print_u32, INFO_FL_PRIO),
2688 	IOCTL_INFO(VIDIOC_ENUMOUTPUT, v4l_enumoutput, v4l_print_enumoutput, INFO_FL_CLEAR(v4l2_output, index)),
2689 	IOCTL_INFO(VIDIOC_G_AUDOUT, v4l_stub_g_audout, v4l_print_audioout, 0),
2690 	IOCTL_INFO(VIDIOC_S_AUDOUT, v4l_stub_s_audout, v4l_print_audioout, INFO_FL_PRIO),
2691 	IOCTL_INFO(VIDIOC_G_MODULATOR, v4l_g_modulator, v4l_print_modulator, INFO_FL_CLEAR(v4l2_modulator, index)),
2692 	IOCTL_INFO(VIDIOC_S_MODULATOR, v4l_s_modulator, v4l_print_modulator, INFO_FL_PRIO),
2693 	IOCTL_INFO(VIDIOC_G_FREQUENCY, v4l_g_frequency, v4l_print_frequency, INFO_FL_CLEAR(v4l2_frequency, tuner)),
2694 	IOCTL_INFO(VIDIOC_S_FREQUENCY, v4l_s_frequency, v4l_print_frequency, INFO_FL_PRIO),
2695 	IOCTL_INFO(VIDIOC_CROPCAP, v4l_cropcap, v4l_print_cropcap, INFO_FL_CLEAR(v4l2_cropcap, type)),
2696 	IOCTL_INFO(VIDIOC_G_CROP, v4l_g_crop, v4l_print_crop, INFO_FL_CLEAR(v4l2_crop, type)),
2697 	IOCTL_INFO(VIDIOC_S_CROP, v4l_s_crop, v4l_print_crop, INFO_FL_PRIO),
2698 	IOCTL_INFO(VIDIOC_G_SELECTION, v4l_g_selection, v4l_print_selection, INFO_FL_CLEAR(v4l2_selection, r)),
2699 	IOCTL_INFO(VIDIOC_S_SELECTION, v4l_s_selection, v4l_print_selection, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_selection, r)),
2700 	IOCTL_INFO(VIDIOC_G_JPEGCOMP, v4l_stub_g_jpegcomp, v4l_print_jpegcompression, 0),
2701 	IOCTL_INFO(VIDIOC_S_JPEGCOMP, v4l_stub_s_jpegcomp, v4l_print_jpegcompression, INFO_FL_PRIO),
2702 	IOCTL_INFO(VIDIOC_QUERYSTD, v4l_querystd, v4l_print_std, 0),
2703 	IOCTL_INFO(VIDIOC_TRY_FMT, v4l_try_fmt, v4l_print_format, 0),
2704 	IOCTL_INFO(VIDIOC_ENUMAUDIO, v4l_stub_enumaudio, v4l_print_audio, INFO_FL_CLEAR(v4l2_audio, index)),
2705 	IOCTL_INFO(VIDIOC_ENUMAUDOUT, v4l_stub_enumaudout, v4l_print_audioout, INFO_FL_CLEAR(v4l2_audioout, index)),
2706 	IOCTL_INFO(VIDIOC_G_PRIORITY, v4l_g_priority, v4l_print_u32, 0),
2707 	IOCTL_INFO(VIDIOC_S_PRIORITY, v4l_s_priority, v4l_print_u32, INFO_FL_PRIO),
2708 	IOCTL_INFO(VIDIOC_G_SLICED_VBI_CAP, v4l_g_sliced_vbi_cap, v4l_print_sliced_vbi_cap, INFO_FL_CLEAR(v4l2_sliced_vbi_cap, type)),
2709 	IOCTL_INFO(VIDIOC_LOG_STATUS, v4l_log_status, v4l_print_newline, 0),
2710 	IOCTL_INFO(VIDIOC_G_EXT_CTRLS, v4l_g_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2711 	IOCTL_INFO(VIDIOC_S_EXT_CTRLS, v4l_s_ext_ctrls, v4l_print_ext_controls, INFO_FL_PRIO | INFO_FL_CTRL),
2712 	IOCTL_INFO(VIDIOC_TRY_EXT_CTRLS, v4l_try_ext_ctrls, v4l_print_ext_controls, INFO_FL_CTRL),
2713 	IOCTL_INFO(VIDIOC_ENUM_FRAMESIZES, v4l_stub_enum_framesizes, v4l_print_frmsizeenum, INFO_FL_CLEAR(v4l2_frmsizeenum, pixel_format)),
2714 	IOCTL_INFO(VIDIOC_ENUM_FRAMEINTERVALS, v4l_stub_enum_frameintervals, v4l_print_frmivalenum, INFO_FL_CLEAR(v4l2_frmivalenum, height)),
2715 	IOCTL_INFO(VIDIOC_G_ENC_INDEX, v4l_stub_g_enc_index, v4l_print_enc_idx, 0),
2716 	IOCTL_INFO(VIDIOC_ENCODER_CMD, v4l_stub_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2717 	IOCTL_INFO(VIDIOC_TRY_ENCODER_CMD, v4l_stub_try_encoder_cmd, v4l_print_encoder_cmd, INFO_FL_CLEAR(v4l2_encoder_cmd, flags)),
2718 	IOCTL_INFO(VIDIOC_DECODER_CMD, v4l_stub_decoder_cmd, v4l_print_decoder_cmd, INFO_FL_PRIO),
2719 	IOCTL_INFO(VIDIOC_TRY_DECODER_CMD, v4l_stub_try_decoder_cmd, v4l_print_decoder_cmd, 0),
2720 	IOCTL_INFO(VIDIOC_DBG_S_REGISTER, v4l_dbg_s_register, v4l_print_dbg_register, 0),
2721 	IOCTL_INFO(VIDIOC_DBG_G_REGISTER, v4l_dbg_g_register, v4l_print_dbg_register, 0),
2722 	IOCTL_INFO(VIDIOC_S_HW_FREQ_SEEK, v4l_s_hw_freq_seek, v4l_print_hw_freq_seek, INFO_FL_PRIO),
2723 	IOCTL_INFO(VIDIOC_S_DV_TIMINGS, v4l_stub_s_dv_timings, v4l_print_dv_timings, INFO_FL_PRIO | INFO_FL_CLEAR(v4l2_dv_timings, bt.flags)),
2724 	IOCTL_INFO(VIDIOC_G_DV_TIMINGS, v4l_stub_g_dv_timings, v4l_print_dv_timings, 0),
2725 	IOCTL_INFO(VIDIOC_DQEVENT, v4l_dqevent, v4l_print_event, 0),
2726 	IOCTL_INFO(VIDIOC_SUBSCRIBE_EVENT, v4l_subscribe_event, v4l_print_event_subscription, 0),
2727 	IOCTL_INFO(VIDIOC_UNSUBSCRIBE_EVENT, v4l_unsubscribe_event, v4l_print_event_subscription, 0),
2728 	IOCTL_INFO(VIDIOC_CREATE_BUFS, v4l_create_bufs, v4l_print_create_buffers, INFO_FL_PRIO | INFO_FL_QUEUE),
2729 	IOCTL_INFO(VIDIOC_PREPARE_BUF, v4l_prepare_buf, v4l_print_buffer, INFO_FL_QUEUE),
2730 	IOCTL_INFO(VIDIOC_ENUM_DV_TIMINGS, v4l_stub_enum_dv_timings, v4l_print_enum_dv_timings, INFO_FL_CLEAR(v4l2_enum_dv_timings, pad)),
2731 	IOCTL_INFO(VIDIOC_QUERY_DV_TIMINGS, v4l_stub_query_dv_timings, v4l_print_dv_timings, INFO_FL_ALWAYS_COPY),
2732 	IOCTL_INFO(VIDIOC_DV_TIMINGS_CAP, v4l_stub_dv_timings_cap, v4l_print_dv_timings_cap, INFO_FL_CLEAR(v4l2_dv_timings_cap, pad)),
2733 	IOCTL_INFO(VIDIOC_ENUM_FREQ_BANDS, v4l_enum_freq_bands, v4l_print_freq_band, 0),
2734 	IOCTL_INFO(VIDIOC_DBG_G_CHIP_INFO, v4l_dbg_g_chip_info, v4l_print_dbg_chip_info, INFO_FL_CLEAR(v4l2_dbg_chip_info, match)),
2735 	IOCTL_INFO(VIDIOC_QUERY_EXT_CTRL, v4l_query_ext_ctrl, v4l_print_query_ext_ctrl, INFO_FL_CTRL | INFO_FL_CLEAR(v4l2_query_ext_ctrl, id)),
2736 };
2737 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
2738 
2739 static bool v4l2_is_known_ioctl(unsigned int cmd)
2740 {
2741 	if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2742 		return false;
2743 	return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
2744 }
2745 
2746 static struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev,
2747 					 struct v4l2_fh *vfh, unsigned int cmd,
2748 					 void *arg)
2749 {
2750 	if (_IOC_NR(cmd) >= V4L2_IOCTLS)
2751 		return vdev->lock;
2752 #if IS_ENABLED(CONFIG_V4L2_MEM2MEM_DEV)
2753 	if (vfh && vfh->m2m_ctx &&
2754 	    (v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE)) {
2755 		if (vfh->m2m_ctx->q_lock)
2756 			return vfh->m2m_ctx->q_lock;
2757 	}
2758 #endif
2759 	if (vdev->queue && vdev->queue->lock &&
2760 			(v4l2_ioctls[_IOC_NR(cmd)].flags & INFO_FL_QUEUE))
2761 		return vdev->queue->lock;
2762 	return vdev->lock;
2763 }
2764 
2765 /* Common ioctl debug function. This function can be used by
2766    external ioctl messages as well as internal V4L ioctl */
2767 void v4l_printk_ioctl(const char *prefix, unsigned int cmd)
2768 {
2769 	const char *dir, *type;
2770 
2771 	if (prefix)
2772 		printk(KERN_DEBUG "%s: ", prefix);
2773 
2774 	switch (_IOC_TYPE(cmd)) {
2775 	case 'd':
2776 		type = "v4l2_int";
2777 		break;
2778 	case 'V':
2779 		if (_IOC_NR(cmd) >= V4L2_IOCTLS) {
2780 			type = "v4l2";
2781 			break;
2782 		}
2783 		pr_cont("%s", v4l2_ioctls[_IOC_NR(cmd)].name);
2784 		return;
2785 	default:
2786 		type = "unknown";
2787 		break;
2788 	}
2789 
2790 	switch (_IOC_DIR(cmd)) {
2791 	case _IOC_NONE:              dir = "--"; break;
2792 	case _IOC_READ:              dir = "r-"; break;
2793 	case _IOC_WRITE:             dir = "-w"; break;
2794 	case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
2795 	default:                     dir = "*ERR*"; break;
2796 	}
2797 	pr_cont("%s ioctl '%c', dir=%s, #%d (0x%08x)",
2798 		type, _IOC_TYPE(cmd), dir, _IOC_NR(cmd), cmd);
2799 }
2800 EXPORT_SYMBOL(v4l_printk_ioctl);
2801 
2802 static long __video_do_ioctl(struct file *file,
2803 		unsigned int cmd, void *arg)
2804 {
2805 	struct video_device *vfd = video_devdata(file);
2806 	struct mutex *req_queue_lock = NULL;
2807 	struct mutex *lock; /* ioctl serialization mutex */
2808 	const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
2809 	bool write_only = false;
2810 	struct v4l2_ioctl_info default_info;
2811 	const struct v4l2_ioctl_info *info;
2812 	void *fh = file->private_data;
2813 	struct v4l2_fh *vfh = NULL;
2814 	int dev_debug = vfd->dev_debug;
2815 	long ret = -ENOTTY;
2816 
2817 	if (ops == NULL) {
2818 		pr_warn("%s: has no ioctl_ops.\n",
2819 				video_device_node_name(vfd));
2820 		return ret;
2821 	}
2822 
2823 	if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
2824 		vfh = file->private_data;
2825 
2826 	/*
2827 	 * We need to serialize streamon/off with queueing new requests.
2828 	 * These ioctls may trigger the cancellation of a streaming
2829 	 * operation, and that should not be mixed with queueing a new
2830 	 * request at the same time.
2831 	 */
2832 	if (v4l2_device_supports_requests(vfd->v4l2_dev) &&
2833 	    (cmd == VIDIOC_STREAMON || cmd == VIDIOC_STREAMOFF)) {
2834 		req_queue_lock = &vfd->v4l2_dev->mdev->req_queue_mutex;
2835 
2836 		if (mutex_lock_interruptible(req_queue_lock))
2837 			return -ERESTARTSYS;
2838 	}
2839 
2840 	lock = v4l2_ioctl_get_lock(vfd, vfh, cmd, arg);
2841 
2842 	if (lock && mutex_lock_interruptible(lock)) {
2843 		if (req_queue_lock)
2844 			mutex_unlock(req_queue_lock);
2845 		return -ERESTARTSYS;
2846 	}
2847 
2848 	if (!video_is_registered(vfd)) {
2849 		ret = -ENODEV;
2850 		goto unlock;
2851 	}
2852 
2853 	if (v4l2_is_known_ioctl(cmd)) {
2854 		info = &v4l2_ioctls[_IOC_NR(cmd)];
2855 
2856 		if (!test_bit(_IOC_NR(cmd), vfd->valid_ioctls) &&
2857 		    !((info->flags & INFO_FL_CTRL) && vfh && vfh->ctrl_handler))
2858 			goto done;
2859 
2860 		if (vfh && (info->flags & INFO_FL_PRIO)) {
2861 			ret = v4l2_prio_check(vfd->prio, vfh->prio);
2862 			if (ret)
2863 				goto done;
2864 		}
2865 	} else {
2866 		default_info.ioctl = cmd;
2867 		default_info.flags = 0;
2868 		default_info.debug = v4l_print_default;
2869 		info = &default_info;
2870 	}
2871 
2872 	write_only = _IOC_DIR(cmd) == _IOC_WRITE;
2873 	if (info != &default_info) {
2874 		ret = info->func(ops, file, fh, arg);
2875 	} else if (!ops->vidioc_default) {
2876 		ret = -ENOTTY;
2877 	} else {
2878 		ret = ops->vidioc_default(file, fh,
2879 			vfh ? v4l2_prio_check(vfd->prio, vfh->prio) >= 0 : 0,
2880 			cmd, arg);
2881 	}
2882 
2883 done:
2884 	if (dev_debug & (V4L2_DEV_DEBUG_IOCTL | V4L2_DEV_DEBUG_IOCTL_ARG)) {
2885 		if (!(dev_debug & V4L2_DEV_DEBUG_STREAMING) &&
2886 		    (cmd == VIDIOC_QBUF || cmd == VIDIOC_DQBUF))
2887 			goto unlock;
2888 
2889 		v4l_printk_ioctl(video_device_node_name(vfd), cmd);
2890 		if (ret < 0)
2891 			pr_cont(": error %ld", ret);
2892 		if (!(dev_debug & V4L2_DEV_DEBUG_IOCTL_ARG))
2893 			pr_cont("\n");
2894 		else if (_IOC_DIR(cmd) == _IOC_NONE)
2895 			info->debug(arg, write_only);
2896 		else {
2897 			pr_cont(": ");
2898 			info->debug(arg, write_only);
2899 		}
2900 	}
2901 
2902 unlock:
2903 	if (lock)
2904 		mutex_unlock(lock);
2905 	if (req_queue_lock)
2906 		mutex_unlock(req_queue_lock);
2907 	return ret;
2908 }
2909 
2910 static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
2911 			    void __user **user_ptr, void ***kernel_ptr)
2912 {
2913 	int ret = 0;
2914 
2915 	switch (cmd) {
2916 	case VIDIOC_PREPARE_BUF:
2917 	case VIDIOC_QUERYBUF:
2918 	case VIDIOC_QBUF:
2919 	case VIDIOC_DQBUF: {
2920 		struct v4l2_buffer *buf = parg;
2921 
2922 		if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
2923 			if (buf->length > VIDEO_MAX_PLANES) {
2924 				ret = -EINVAL;
2925 				break;
2926 			}
2927 			*user_ptr = (void __user *)buf->m.planes;
2928 			*kernel_ptr = (void **)&buf->m.planes;
2929 			*array_size = sizeof(struct v4l2_plane) * buf->length;
2930 			ret = 1;
2931 		}
2932 		break;
2933 	}
2934 
2935 	case VIDIOC_G_EDID:
2936 	case VIDIOC_S_EDID: {
2937 		struct v4l2_edid *edid = parg;
2938 
2939 		if (edid->blocks) {
2940 			if (edid->blocks > 256) {
2941 				ret = -EINVAL;
2942 				break;
2943 			}
2944 			*user_ptr = (void __user *)edid->edid;
2945 			*kernel_ptr = (void **)&edid->edid;
2946 			*array_size = edid->blocks * 128;
2947 			ret = 1;
2948 		}
2949 		break;
2950 	}
2951 
2952 	case VIDIOC_S_EXT_CTRLS:
2953 	case VIDIOC_G_EXT_CTRLS:
2954 	case VIDIOC_TRY_EXT_CTRLS: {
2955 		struct v4l2_ext_controls *ctrls = parg;
2956 
2957 		if (ctrls->count != 0) {
2958 			if (ctrls->count > V4L2_CID_MAX_CTRLS) {
2959 				ret = -EINVAL;
2960 				break;
2961 			}
2962 			*user_ptr = (void __user *)ctrls->controls;
2963 			*kernel_ptr = (void **)&ctrls->controls;
2964 			*array_size = sizeof(struct v4l2_ext_control)
2965 				    * ctrls->count;
2966 			ret = 1;
2967 		}
2968 		break;
2969 	}
2970 	}
2971 
2972 	return ret;
2973 }
2974 
2975 long
2976 video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
2977 	       v4l2_kioctl func)
2978 {
2979 	char	sbuf[128];
2980 	void    *mbuf = NULL;
2981 	void	*parg = (void *)arg;
2982 	long	err  = -EINVAL;
2983 	bool	has_array_args;
2984 	bool	always_copy = false;
2985 	size_t  array_size = 0;
2986 	void __user *user_ptr = NULL;
2987 	void	**kernel_ptr = NULL;
2988 	const size_t ioc_size = _IOC_SIZE(cmd);
2989 
2990 	/*  Copy arguments into temp kernel buffer  */
2991 	if (_IOC_DIR(cmd) != _IOC_NONE) {
2992 		if (ioc_size <= sizeof(sbuf)) {
2993 			parg = sbuf;
2994 		} else {
2995 			/* too big to allocate from stack */
2996 			mbuf = kvmalloc(ioc_size, GFP_KERNEL);
2997 			if (NULL == mbuf)
2998 				return -ENOMEM;
2999 			parg = mbuf;
3000 		}
3001 
3002 		err = -EFAULT;
3003 		if (_IOC_DIR(cmd) & _IOC_WRITE) {
3004 			unsigned int n = ioc_size;
3005 
3006 			/*
3007 			 * In some cases, only a few fields are used as input,
3008 			 * i.e. when the app sets "index" and then the driver
3009 			 * fills in the rest of the structure for the thing
3010 			 * with that index.  We only need to copy up the first
3011 			 * non-input field.
3012 			 */
3013 			if (v4l2_is_known_ioctl(cmd)) {
3014 				u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;
3015 
3016 				if (flags & INFO_FL_CLEAR_MASK)
3017 					n = (flags & INFO_FL_CLEAR_MASK) >> 16;
3018 				always_copy = flags & INFO_FL_ALWAYS_COPY;
3019 			}
3020 
3021 			if (copy_from_user(parg, (void __user *)arg, n))
3022 				goto out;
3023 
3024 			/* zero out anything we don't copy from userspace */
3025 			if (n < ioc_size)
3026 				memset((u8 *)parg + n, 0, ioc_size - n);
3027 		} else {
3028 			/* read-only ioctl */
3029 			memset(parg, 0, ioc_size);
3030 		}
3031 	}
3032 
3033 	err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
3034 	if (err < 0)
3035 		goto out;
3036 	has_array_args = err;
3037 
3038 	if (has_array_args) {
3039 		/*
3040 		 * When adding new types of array args, make sure that the
3041 		 * parent argument to ioctl (which contains the pointer to the
3042 		 * array) fits into sbuf (so that mbuf will still remain
3043 		 * unused up to here).
3044 		 */
3045 		mbuf = kvmalloc(array_size, GFP_KERNEL);
3046 		err = -ENOMEM;
3047 		if (NULL == mbuf)
3048 			goto out_array_args;
3049 		err = -EFAULT;
3050 		if (copy_from_user(mbuf, user_ptr, array_size))
3051 			goto out_array_args;
3052 		*kernel_ptr = mbuf;
3053 	}
3054 
3055 	/* Handles IOCTL */
3056 	err = func(file, cmd, parg);
3057 	if (err == -ENOTTY || err == -ENOIOCTLCMD) {
3058 		err = -ENOTTY;
3059 		goto out;
3060 	}
3061 
3062 	if (err == 0) {
3063 		if (cmd == VIDIOC_DQBUF)
3064 			trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
3065 		else if (cmd == VIDIOC_QBUF)
3066 			trace_v4l2_qbuf(video_devdata(file)->minor, parg);
3067 	}
3068 
3069 	if (has_array_args) {
3070 		*kernel_ptr = (void __force *)user_ptr;
3071 		if (copy_to_user(user_ptr, mbuf, array_size))
3072 			err = -EFAULT;
3073 		goto out_array_args;
3074 	}
3075 	/*
3076 	 * Some ioctls can return an error, but still have valid
3077 	 * results that must be returned.
3078 	 */
3079 	if (err < 0 && !always_copy)
3080 		goto out;
3081 
3082 out_array_args:
3083 	/*  Copy results into user buffer  */
3084 	switch (_IOC_DIR(cmd)) {
3085 	case _IOC_READ:
3086 	case (_IOC_WRITE | _IOC_READ):
3087 		if (copy_to_user((void __user *)arg, parg, ioc_size))
3088 			err = -EFAULT;
3089 		break;
3090 	}
3091 
3092 out:
3093 	kvfree(mbuf);
3094 	return err;
3095 }
3096 
3097 long video_ioctl2(struct file *file,
3098 	       unsigned int cmd, unsigned long arg)
3099 {
3100 	return video_usercopy(file, cmd, arg, __video_do_ioctl);
3101 }
3102 EXPORT_SYMBOL(video_ioctl2);
3103