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