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