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