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