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