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