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