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