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