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