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