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