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