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