1 /*
2  * Copyright (c) 2014 Philippe De Muyter <phdm@macqel.be>
3  * Copyright (c) 2014 William Manley <will@williammanley.net>
4  * Copyright (c) 2011 Peter Zotov <whitequark@whitequark.org>
5  * Copyright (c) 2014-2018 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "defs.h"
32 
33 #include <stdint.h>
34 #include <linux/ioctl.h>
35 #include <linux/types.h>
36 #include <linux/videodev2.h>
37 
38 #include DEF_MPERS_TYPE(struct_v4l2_buffer)
39 #include DEF_MPERS_TYPE(struct_v4l2_clip)
40 #ifdef VIDIOC_CREATE_BUFS
41 # include DEF_MPERS_TYPE(struct_v4l2_create_buffers)
42 #endif
43 #include DEF_MPERS_TYPE(struct_v4l2_ext_control)
44 #include DEF_MPERS_TYPE(struct_v4l2_ext_controls)
45 #include DEF_MPERS_TYPE(struct_v4l2_format)
46 #include DEF_MPERS_TYPE(struct_v4l2_framebuffer)
47 #include DEF_MPERS_TYPE(struct_v4l2_input)
48 #include DEF_MPERS_TYPE(struct_v4l2_standard)
49 
50 typedef struct v4l2_buffer struct_v4l2_buffer;
51 typedef struct v4l2_clip struct_v4l2_clip;
52 #ifdef VIDIOC_CREATE_BUFS
53 typedef struct v4l2_create_buffers struct_v4l2_create_buffers;
54 #endif
55 typedef struct v4l2_ext_control struct_v4l2_ext_control;
56 typedef struct v4l2_ext_controls struct_v4l2_ext_controls;
57 typedef struct v4l2_format struct_v4l2_format;
58 typedef struct v4l2_framebuffer struct_v4l2_framebuffer;
59 typedef struct v4l2_input struct_v4l2_input;
60 typedef struct v4l2_standard struct_v4l2_standard;
61 
62 #include MPERS_DEFS
63 
64 #include "print_fields.h"
65 #include "xstring.h"
66 
67 /* some historical constants */
68 #ifndef V4L2_CID_HCENTER
69 #define V4L2_CID_HCENTER (V4L2_CID_BASE+22)
70 #endif
71 #ifndef V4L2_CID_VCENTER
72 #define V4L2_CID_VCENTER (V4L2_CID_BASE+23)
73 #endif
74 #ifndef V4L2_CID_BAND_STOP_FILTER
75 #define V4L2_CID_BAND_STOP_FILTER (V4L2_CID_BASE+33)
76 #endif
77 
78 #define FMT_FRACT "%u/%u"
79 #define ARGS_FRACT(x) ((x).numerator), ((x).denominator)
80 
81 #define FMT_RECT "{left=%d, top=%d, width=%u, height=%u}"
82 #define ARGS_RECT(x) (x).left, (x).top, (x).width, (x).height
83 
84 static void
print_pixelformat(uint32_t fourcc)85 print_pixelformat(uint32_t fourcc)
86 {
87 	const union {
88 		uint32_t pixelformat;
89 		unsigned char cc[sizeof(uint32_t)];
90 	} u = {
91 #if WORDS_BIGENDIAN
92 		.cc = {
93 			(unsigned char) (fourcc >> 24),
94 			(unsigned char) (fourcc >> 16),
95 			(unsigned char) (fourcc >> 8),
96 			(unsigned char) fourcc
97 		}
98 #else
99 		.pixelformat = fourcc
100 #endif
101 	};
102 	unsigned int i;
103 
104 	tprints("v4l2_fourcc(");
105 	for (i = 0; i < sizeof(u.cc); ++i) {
106 		unsigned char c = u.cc[i];
107 
108 		if (i)
109 			tprints(", ");
110 		if (c == '\'' || c == '\\') {
111 			char sym[] = {
112 				'\'',
113 				'\\',
114 				c,
115 				'\'',
116 				'\0'
117 			};
118 			tprints(sym);
119 		} else if (c >= ' ' && c <= 0x7e) {
120 			char sym[] = {
121 				'\'',
122 				c,
123 				'\'',
124 				'\0'
125 			};
126 			tprints(sym);
127 		} else {
128 			char hex[] = {
129 				'\'',
130 				'\\',
131 				'x',
132 				"0123456789abcdef"[c >> 4],
133 				"0123456789abcdef"[c & 0xf],
134 				'\'',
135 				'\0'
136 			};
137 			tprints(hex);
138 		}
139 	}
140 	tprints(")");
141 }
142 
143 #include "xlat/v4l2_device_capabilities_flags.h"
144 
145 static int
print_v4l2_capability(struct tcb * const tcp,const kernel_ulong_t arg)146 print_v4l2_capability(struct tcb *const tcp, const kernel_ulong_t arg)
147 {
148 	struct v4l2_capability caps;
149 
150 	if (entering(tcp))
151 		return 0;
152 	tprints(", ");
153 	if (umove_or_printaddr(tcp, arg, &caps))
154 		return RVAL_IOCTL_DECODED;
155 	PRINT_FIELD_CSTRING("{", caps, driver);
156 	PRINT_FIELD_CSTRING(", ", caps, card);
157 	PRINT_FIELD_CSTRING(", ", caps, bus_info);
158 	tprintf(", version=%u.%u.%u, capabilities=",
159 		(caps.version >> 16) & 0xFF,
160 		(caps.version >> 8) & 0xFF,
161 		caps.version & 0xFF);
162 	printflags(v4l2_device_capabilities_flags, caps.capabilities,
163 		   "V4L2_CAP_???");
164 #ifdef V4L2_CAP_DEVICE_CAPS
165 	tprints(", device_caps=");
166 	printflags(v4l2_device_capabilities_flags, caps.device_caps,
167 		   "V4L2_CAP_???");
168 #endif
169 	tprints("}");
170 	return RVAL_IOCTL_DECODED;
171 }
172 
173 #include "xlat/v4l2_buf_types.h"
174 #include "xlat/v4l2_format_description_flags.h"
175 
176 static int
print_v4l2_fmtdesc(struct tcb * const tcp,const kernel_ulong_t arg)177 print_v4l2_fmtdesc(struct tcb *const tcp, const kernel_ulong_t arg)
178 {
179 	struct v4l2_fmtdesc f;
180 
181 	if (entering(tcp)) {
182 		tprints(", ");
183 		if (umove_or_printaddr(tcp, arg, &f))
184 			return RVAL_IOCTL_DECODED;
185 		tprintf("{index=%u, type=", f.index);
186 		printxval(v4l2_buf_types, f.type, "V4L2_BUF_TYPE_???");
187 		return 0;
188 	}
189 
190 	if (!syserror(tcp) && !umove(tcp, arg, &f)) {
191 		tprints(", flags=");
192 		printflags(v4l2_format_description_flags, f.flags,
193 			   "V4L2_FMT_FLAG_???");
194 		PRINT_FIELD_CSTRING(", ", f, description);
195 		tprints(", pixelformat=");
196 		print_pixelformat(f.pixelformat);
197 	}
198 	tprints("}");
199 	return RVAL_IOCTL_DECODED;
200 }
201 
202 #include "xlat/v4l2_fields.h"
203 #include "xlat/v4l2_colorspaces.h"
204 #include "xlat/v4l2_vbi_flags.h"
205 #include "xlat/v4l2_sliced_flags.h"
206 
207 static bool
print_v4l2_clip(struct tcb * tcp,void * elem_buf,size_t elem_size,void * data)208 print_v4l2_clip(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
209 {
210 	const struct_v4l2_clip *p = elem_buf;
211 	tprintf(FMT_RECT, ARGS_RECT(p->c));
212 	return true;
213 }
214 
215 static bool
print_v4l2_format_fmt(struct tcb * const tcp,const char * prefix,const struct_v4l2_format * f)216 print_v4l2_format_fmt(struct tcb *const tcp, const char *prefix,
217 		      const struct_v4l2_format *f)
218 {
219 	bool ret = true;
220 	switch (f->type) {
221 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
222 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
223 		tprints(prefix);
224 		tprintf("fmt.pix={width=%u, height=%u, pixelformat=",
225 			f->fmt.pix.width, f->fmt.pix.height);
226 		print_pixelformat(f->fmt.pix.pixelformat);
227 		tprints(", field=");
228 		printxval(v4l2_fields, f->fmt.pix.field, "V4L2_FIELD_???");
229 		tprintf(", bytesperline=%u, sizeimage=%u, colorspace=",
230 			f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
231 		printxval(v4l2_colorspaces, f->fmt.pix.colorspace,
232 			  "V4L2_COLORSPACE_???");
233 		tprints("}");
234 		break;
235 #if HAVE_DECL_V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
236 	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
237 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: {
238 		unsigned int i, max;
239 
240 		tprints(prefix);
241 		tprintf("fmt.pix_mp={width=%u, height=%u, pixelformat=",
242 			f->fmt.pix_mp.width, f->fmt.pix_mp.height);
243 		print_pixelformat(f->fmt.pix_mp.pixelformat);
244 		tprints(", field=");
245 		printxval(v4l2_fields, f->fmt.pix_mp.field, "V4L2_FIELD_???");
246 		tprints(", colorspace=");
247 		printxval(v4l2_colorspaces, f->fmt.pix_mp.colorspace,
248 			  "V4L2_COLORSPACE_???");
249 		tprints(", plane_fmt=[");
250 		max = f->fmt.pix_mp.num_planes;
251 		if (max > VIDEO_MAX_PLANES)
252 			max = VIDEO_MAX_PLANES;
253 		for (i = 0; i < max; i++) {
254 			if (i > 0)
255 				tprints(", ");
256 			tprintf("{sizeimage=%u, bytesperline=%u}",
257 				f->fmt.pix_mp.plane_fmt[i].sizeimage,
258 				f->fmt.pix_mp.plane_fmt[i].bytesperline);
259 		}
260 		tprintf("], num_planes=%u}",
261 			(unsigned) f->fmt.pix_mp.num_planes);
262 		break;
263 	}
264 #endif
265 	/* OUTPUT_OVERLAY since Linux v2.6.22-rc1~1118^2~179 */
266 #if HAVE_DECL_V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
267 	case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
268 #endif
269 	case V4L2_BUF_TYPE_VIDEO_OVERLAY: {
270 		struct_v4l2_clip clip;
271 		tprints(prefix);
272 		tprintf("fmt.win={left=%d, top=%d, width=%u, height=%u, field=",
273 			ARGS_RECT(f->fmt.win.w));
274 		printxval(v4l2_fields, f->fmt.win.field, "V4L2_FIELD_???");
275 		tprintf(", chromakey=%#x, clips=", f->fmt.win.chromakey);
276 		ret = print_array(tcp, ptr_to_kulong(f->fmt.win.clips),
277 				  f->fmt.win.clipcount, &clip, sizeof(clip),
278 				  umoven_or_printaddr, print_v4l2_clip, 0);
279 		tprintf(", clipcount=%u, bitmap=", f->fmt.win.clipcount);
280 		printaddr(ptr_to_kulong(f->fmt.win.bitmap));
281 #ifdef HAVE_STRUCT_V4L2_WINDOW_GLOBAL_ALPHA
282 		tprintf(", global_alpha=%#x", f->fmt.win.global_alpha);
283 #endif
284 		tprints("}");
285 		break;
286 	}
287 	case V4L2_BUF_TYPE_VBI_CAPTURE:
288 	case V4L2_BUF_TYPE_VBI_OUTPUT:
289 		tprints(prefix);
290 		tprintf("fmt.vbi={sampling_rate=%u, offset=%u, "
291 			"samples_per_line=%u, sample_format=",
292 			f->fmt.vbi.sampling_rate, f->fmt.vbi.offset,
293 			f->fmt.vbi.samples_per_line);
294 		print_pixelformat(f->fmt.vbi.sample_format);
295 		tprintf(", start=[%u, %u], count=[%u, %u], ",
296 			f->fmt.vbi.start[0], f->fmt.vbi.start[1],
297 			f->fmt.vbi.count[0], f->fmt.vbi.count[1]);
298 		tprints("flags=");
299 		printxval(v4l2_vbi_flags, f->fmt.vbi.flags, "V4L2_VBI_???");
300 		tprints("}");
301 		break;
302 	/* both since Linux v2.6.14-rc2~64 */
303 #if HAVE_DECL_V4L2_BUF_TYPE_SLICED_VBI_CAPTURE
304 	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
305 	case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT: {
306 		unsigned int i, j;
307 
308 		tprints(prefix);
309 		tprints("fmt.sliced={service_set=");
310 		printxval(v4l2_sliced_flags, f->fmt.sliced.service_set,
311 			"V4L2_SLICED_???");
312 		tprintf(", io_size=%u, service_lines=[",
313 			f->fmt.sliced.io_size);
314 		for (i = 0; i < ARRAY_SIZE(f->fmt.sliced.service_lines); i++) {
315 			if (i > 0)
316 				tprints(", ");
317 			tprints("[");
318 			for (j = 0;
319 			     j < ARRAY_SIZE(f->fmt.sliced.service_lines[0]);
320 			     j++) {
321 				if (j > 0)
322 					tprints(", ");
323 				tprintf("%#x",
324 					f->fmt.sliced.service_lines[i][j]);
325 			}
326 			tprints("]");
327 		}
328 		tprints("]}");
329 		break;
330 	}
331 #endif
332 	/* since Linux v4.4-rc1~118^2~14 */
333 #if HAVE_DECL_V4L2_BUF_TYPE_SDR_OUTPUT
334 	case V4L2_BUF_TYPE_SDR_OUTPUT:
335 #endif
336 	/* since Linux v3.15-rc1~85^2~213 */
337 #if HAVE_DECL_V4L2_BUF_TYPE_SDR_CAPTURE
338 	case V4L2_BUF_TYPE_SDR_CAPTURE:
339 		tprints(prefix);
340 		tprints("fmt.sdr={pixelformat=");
341 		print_pixelformat(f->fmt.sdr.pixelformat);
342 #ifdef HAVE_STRUCT_V4L2_SDR_FORMAT_BUFFERSIZE
343 		tprintf(", buffersize=%u",
344 			f->fmt.sdr.buffersize);
345 #endif
346 		tprints("}");
347 		break;
348 #endif
349 	}
350 	return ret;
351 }
352 
353 static int
print_v4l2_format(struct tcb * const tcp,const kernel_ulong_t arg,const bool is_get)354 print_v4l2_format(struct tcb *const tcp, const kernel_ulong_t arg,
355 		  const bool is_get)
356 {
357 	struct_v4l2_format f;
358 
359 	if (entering(tcp)) {
360 		tprints(", ");
361 		if (umove_or_printaddr(tcp, arg, &f))
362 			return RVAL_IOCTL_DECODED;
363 		tprints("{type=");
364 		printxval(v4l2_buf_types, f.type, "V4L2_BUF_TYPE_???");
365 		if (is_get)
366 			return 0;
367 		if (!print_v4l2_format_fmt(tcp, ", ", &f)) {
368 			tprints("}");
369 			return RVAL_IOCTL_DECODED;
370 		}
371 
372 		return 0;
373 	}
374 
375 	if (!syserror(tcp) && !umove(tcp, arg, &f))
376 		print_v4l2_format_fmt(tcp, is_get ? ", " : " => ", &f);
377 
378 	tprints("}");
379 
380 	return RVAL_IOCTL_DECODED;
381 }
382 
383 #include "xlat/v4l2_memories.h"
384 
385 static int
print_v4l2_requestbuffers(struct tcb * const tcp,const kernel_ulong_t arg)386 print_v4l2_requestbuffers(struct tcb *const tcp, const kernel_ulong_t arg)
387 {
388 	struct v4l2_requestbuffers reqbufs;
389 
390 	if (entering(tcp)) {
391 		tprints(", ");
392 
393 		if (umove_or_printaddr(tcp, arg, &reqbufs))
394 			return RVAL_IOCTL_DECODED;
395 
396 		tprintf("{type=");
397 		printxval(v4l2_buf_types, reqbufs.type, "V4L2_BUF_TYPE_???");
398 		tprints(", memory=");
399 		printxval(v4l2_memories, reqbufs.memory, "V4L2_MEMORY_???");
400 		tprintf(", count=%u", reqbufs.count);
401 
402 		return 0;
403 	}
404 
405 	if (!syserror(tcp)) {
406 		tprints(" => ");
407 
408 		if (!umove(tcp, arg, &reqbufs))
409 			tprintf("%u", reqbufs.count);
410 		else
411 			tprints("???");
412 	}
413 
414 	tprints("}");
415 
416 	return RVAL_IOCTL_DECODED;
417 }
418 
419 #include "xlat/v4l2_buf_flags.h"
420 
421 static int
print_v4l2_buffer(struct tcb * const tcp,const unsigned int code,const kernel_ulong_t arg)422 print_v4l2_buffer(struct tcb *const tcp, const unsigned int code,
423 		  const kernel_ulong_t arg)
424 {
425 	struct_v4l2_buffer b;
426 
427 	if (entering(tcp)) {
428 		tprints(", ");
429 		if (umove_or_printaddr(tcp, arg, &b))
430 			return RVAL_IOCTL_DECODED;
431 		tprints("{type=");
432 		printxval(v4l2_buf_types, b.type, "V4L2_BUF_TYPE_???");
433 		if (code != VIDIOC_DQBUF)
434 			tprintf(", index=%u", b.index);
435 
436 		return 0;
437 	}
438 
439 	if (!syserror(tcp) && !umove(tcp, arg, &b)) {
440 		if (code == VIDIOC_DQBUF)
441 			tprintf(", index=%u", b.index);
442 		tprints(", memory=");
443 		printxval(v4l2_memories, b.memory, "V4L2_MEMORY_???");
444 
445 		if (b.memory == V4L2_MEMORY_MMAP) {
446 			tprintf(", m.offset=%#x", b.m.offset);
447 		} else if (b.memory == V4L2_MEMORY_USERPTR) {
448 			tprints(", m.userptr=");
449 			printaddr(b.m.userptr);
450 		}
451 
452 		tprintf(", length=%u, bytesused=%u, flags=",
453 			b.length, b.bytesused);
454 		printflags(v4l2_buf_flags, b.flags, "V4L2_BUF_FLAG_???");
455 		if (code == VIDIOC_DQBUF) {
456 			tprints(", timestamp = ");
457 			MPERS_FUNC_NAME(print_struct_timeval)(&b.timestamp);
458 		}
459 		tprints(", ...");
460 	}
461 
462 	tprints("}");
463 
464 	return RVAL_IOCTL_DECODED;
465 }
466 
467 static int
print_v4l2_framebuffer(struct tcb * const tcp,const kernel_ulong_t arg)468 print_v4l2_framebuffer(struct tcb *const tcp, const kernel_ulong_t arg)
469 {
470 	struct_v4l2_framebuffer b;
471 
472 	tprints(", ");
473 	if (!umove_or_printaddr(tcp, arg, &b)) {
474 		tprintf("{capability=%#x, flags=%#x, base=",
475 			b.capability, b.flags);
476 		printaddr(ptr_to_kulong(b.base));
477 		tprints("}");
478 	}
479 
480 	return RVAL_IOCTL_DECODED;
481 }
482 
483 static int
print_v4l2_buf_type(struct tcb * const tcp,const kernel_ulong_t arg)484 print_v4l2_buf_type(struct tcb *const tcp, const kernel_ulong_t arg)
485 {
486 	int type;
487 
488 	tprints(", ");
489 	if (!umove_or_printaddr(tcp, arg, &type)) {
490 		tprints("[");
491 		printxval(v4l2_buf_types, type, "V4L2_BUF_TYPE_???");
492 		tprints("]");
493 	}
494 	return RVAL_IOCTL_DECODED;
495 }
496 
497 #include "xlat/v4l2_streaming_capabilities.h"
498 #include "xlat/v4l2_capture_modes.h"
499 
500 static int
print_v4l2_streamparm(struct tcb * const tcp,const kernel_ulong_t arg,const bool is_get)501 print_v4l2_streamparm(struct tcb *const tcp, const kernel_ulong_t arg,
502 		      const bool is_get)
503 {
504 	struct v4l2_streamparm s;
505 
506 	if (entering(tcp)) {
507 		tprints(", ");
508 		if (umove_or_printaddr(tcp, arg, &s))
509 			return RVAL_IOCTL_DECODED;
510 		tprints("{type=");
511 		printxval(v4l2_buf_types, s.type, "V4L2_BUF_TYPE_???");
512 		switch (s.type) {
513 			case V4L2_BUF_TYPE_VIDEO_CAPTURE:
514 			case V4L2_BUF_TYPE_VIDEO_OUTPUT:
515 				if (is_get)
516 					return 0;
517 				tprints(", ");
518 				break;
519 			default:
520 				tprints("}");
521 				return RVAL_IOCTL_DECODED;
522 		}
523 	} else {
524 		if (syserror(tcp) || umove(tcp, arg, &s) < 0) {
525 			tprints("}");
526 			return RVAL_IOCTL_DECODED;
527 		}
528 		tprints(is_get ? ", " : " => ");
529 	}
530 
531 	if (s.type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
532 		tprints("parm.capture={capability=");
533 		printflags(v4l2_streaming_capabilities,
534 			   s.parm.capture.capability, "V4L2_CAP_???");
535 
536 		tprints(", capturemode=");
537 		printflags(v4l2_capture_modes,
538 			   s.parm.capture.capturemode, "V4L2_MODE_???");
539 
540 		tprintf(", timeperframe=" FMT_FRACT,
541 			ARGS_FRACT(s.parm.capture.timeperframe));
542 
543 		tprintf(", extendedmode=%u, readbuffers=%u}",
544 			s.parm.capture.extendedmode,
545 			s.parm.capture.readbuffers);
546 	} else {
547 		tprints("parm.output={capability=");
548 		printflags(v4l2_streaming_capabilities,
549 			   s.parm.output.capability, "V4L2_CAP_???");
550 
551 		tprintf(", outputmode=%u", s.parm.output.outputmode);
552 
553 		tprintf(", timeperframe=" FMT_FRACT,
554 			ARGS_FRACT(s.parm.output.timeperframe));
555 
556 		tprintf(", extendedmode=%u, writebuffers=%u}",
557 			s.parm.output.extendedmode,
558 			s.parm.output.writebuffers);
559 	}
560 	if (entering(tcp)) {
561 		return 0;
562 	} else {
563 		tprints("}");
564 		return RVAL_IOCTL_DECODED;
565 	}
566 }
567 
568 static int
print_v4l2_standard(struct tcb * const tcp,const kernel_ulong_t arg)569 print_v4l2_standard(struct tcb *const tcp, const kernel_ulong_t arg)
570 {
571 	struct_v4l2_standard s;
572 
573 	if (entering(tcp)) {
574 		tprints(", ");
575 		if (umove_or_printaddr(tcp, arg, &s))
576 			return RVAL_IOCTL_DECODED;
577 		tprintf("{index=%u", s.index);
578 
579 		return 0;
580 	}
581 
582 	if (!syserror(tcp) && !umove(tcp, arg, &s)) {
583 		PRINT_FIELD_CSTRING(", ", s, name);
584 		tprintf(", frameperiod=" FMT_FRACT,
585 			ARGS_FRACT(s.frameperiod));
586 		tprintf(", framelines=%d", s.framelines);
587 	}
588 
589 	tprints("}");
590 
591 	return RVAL_IOCTL_DECODED;
592 }
593 
594 #include "xlat/v4l2_input_types.h"
595 
596 static int
print_v4l2_input(struct tcb * const tcp,const kernel_ulong_t arg)597 print_v4l2_input(struct tcb *const tcp, const kernel_ulong_t arg)
598 {
599 	struct_v4l2_input i;
600 
601 	if (entering(tcp)) {
602 		tprints(", ");
603 		if (umove_or_printaddr(tcp, arg, &i))
604 			return RVAL_IOCTL_DECODED;
605 		tprintf("{index=%u", i.index);
606 
607 		return 0;
608 	}
609 
610 	if (!syserror(tcp) && !umove(tcp, arg, &i)) {
611 		PRINT_FIELD_CSTRING(", ", i, name);
612 		tprints(", type=");
613 		printxval(v4l2_input_types, i.type, "V4L2_INPUT_TYPE_???");
614 	}
615 
616 	tprints("}");
617 
618 	return RVAL_IOCTL_DECODED;
619 }
620 
621 #include "xlat/v4l2_control_ids.h"
622 
623 static int
print_v4l2_control(struct tcb * const tcp,const kernel_ulong_t arg,const bool is_get)624 print_v4l2_control(struct tcb *const tcp, const kernel_ulong_t arg,
625 		   const bool is_get)
626 {
627 	struct v4l2_control c;
628 
629 	if (entering(tcp)) {
630 		tprints(", ");
631 		if (umove_or_printaddr(tcp, arg, &c))
632 			return RVAL_IOCTL_DECODED;
633 		tprints("{id=");
634 		printxval(v4l2_control_ids, c.id, "V4L2_CID_???");
635 		if (!is_get)
636 			tprintf(", value=%d", c.value);
637 		return 0;
638 	}
639 
640 	if (!syserror(tcp) && !umove(tcp, arg, &c)) {
641 		tprints(is_get ? ", " : " => ");
642 		tprintf("value=%d", c.value);
643 	}
644 
645 	tprints("}");
646 
647 	return RVAL_IOCTL_DECODED;
648 }
649 
650 #include "xlat/v4l2_tuner_types.h"
651 #include "xlat/v4l2_tuner_capabilities.h"
652 #include "xlat/v4l2_tuner_rxsubchanses.h"
653 #include "xlat/v4l2_tuner_audmodes.h"
654 
655 static int
print_v4l2_tuner(struct tcb * const tcp,const kernel_ulong_t arg,const bool is_get)656 print_v4l2_tuner(struct tcb *const tcp, const kernel_ulong_t arg,
657 		 const bool is_get)
658 {
659 	struct v4l2_tuner c;
660 	if (entering(tcp)) {
661 		tprints(", ");
662 		if (umove_or_printaddr(tcp, arg, &c))
663 			return RVAL_IOCTL_DECODED;
664 		tprintf("{index=%u", c.index);
665 		if (is_get)
666 			return 0;
667 		tprints(", ");
668 	} else {
669 		if (syserror(tcp) || umove(tcp, arg, &c) < 0) {
670 			tprints("}");
671 			return RVAL_IOCTL_DECODED;
672 		}
673 		tprints(is_get ? ", " : " => ");
674 	}
675 
676 	PRINT_FIELD_CSTRING("", c, name);
677 	tprints(", type=");
678 	printxval(v4l2_tuner_types, c.type, "V4L2_TUNER_TYPE_???");
679 	tprints(", capability=");
680 	printxval(v4l2_tuner_capabilities, c.capability,
681 		  "V4L2_TUNER_CAP_???");
682 	tprintf(", rangelow=%u, rangehigh=%u, rxsubchans=",
683 		c.rangelow, c.rangehigh);
684 	printxval(v4l2_tuner_rxsubchanses, c.rxsubchans,
685 		  "V4L2_TUNER_SUB_???");
686 	tprints(", audmode=");
687 	printxval(v4l2_tuner_audmodes, c.audmode,
688 		  "V4L2_TUNER_MODE_???");
689 	tprintf(", signal=%d, afc=%d", c.signal, c.afc);
690 
691 	if (entering(tcp)) {
692 		return 0;
693 	} else {
694 		tprints("}");
695 		return RVAL_IOCTL_DECODED;
696 	}
697 }
698 
699 #include "xlat/v4l2_control_types.h"
700 #include "xlat/v4l2_control_flags.h"
701 
702 static int
print_v4l2_queryctrl(struct tcb * const tcp,const kernel_ulong_t arg)703 print_v4l2_queryctrl(struct tcb *const tcp, const kernel_ulong_t arg)
704 {
705 	struct v4l2_queryctrl c;
706 
707 	if (entering(tcp)) {
708 		tprints(", ");
709 		if (umove_or_printaddr(tcp, arg, &c))
710 			return RVAL_IOCTL_DECODED;
711 		tprints("{id=");
712 	} else {
713 		if (syserror(tcp) || umove(tcp, arg, &c) < 0) {
714 			tprints("}");
715 			return RVAL_IOCTL_DECODED;
716 		}
717 		if (get_tcb_priv_ulong(tcp))
718 			tprints(" => ");
719 	}
720 
721 	if (entering(tcp) || get_tcb_priv_ulong(tcp)) {
722 #ifdef V4L2_CTRL_FLAG_NEXT_CTRL
723 		const unsigned long next = c.id & V4L2_CTRL_FLAG_NEXT_CTRL;
724 		set_tcb_priv_ulong(tcp, next);
725 		if (next) {
726 			tprints("V4L2_CTRL_FLAG_NEXT_CTRL|");
727 			c.id &= ~V4L2_CTRL_FLAG_NEXT_CTRL;
728 		}
729 #endif
730 		printxval(v4l2_control_ids, c.id, "V4L2_CID_???");
731 	}
732 
733 	if (exiting(tcp)) {
734 		tprints(", type=");
735 		printxval(v4l2_control_types, c.type, "V4L2_CTRL_TYPE_???");
736 		PRINT_FIELD_CSTRING(", ", c, name);
737 		tprintf(", minimum=%d, maximum=%d, step=%d"
738 			", default_value=%d, flags=",
739 			c.minimum, c.maximum, c.step, c.default_value);
740 		printflags(v4l2_control_flags, c.flags, "V4L2_CTRL_FLAG_???");
741 		tprints("}");
742 	}
743 	return entering(tcp) ? 0 : RVAL_IOCTL_DECODED;
744 }
745 
746 static int
print_v4l2_cropcap(struct tcb * const tcp,const kernel_ulong_t arg)747 print_v4l2_cropcap(struct tcb *const tcp, const kernel_ulong_t arg)
748 {
749 	struct v4l2_cropcap c;
750 
751 	if (entering(tcp)) {
752 		tprints(", ");
753 		if (umove_or_printaddr(tcp, arg, &c))
754 			return RVAL_IOCTL_DECODED;
755 		tprints("{type=");
756 		printxval(v4l2_buf_types, c.type, "V4L2_BUF_TYPE_???");
757 
758 		return 0;
759 	}
760 
761 	if (!syserror(tcp) && !umove(tcp, arg, &c)) {
762 		tprintf(", bounds=" FMT_RECT
763 			", defrect=" FMT_RECT
764 			", pixelaspect=" FMT_FRACT,
765 			ARGS_RECT(c.bounds),
766 			ARGS_RECT(c.defrect),
767 			ARGS_FRACT(c.pixelaspect));
768 	}
769 
770 	tprints("}");
771 
772 	return RVAL_IOCTL_DECODED;
773 }
774 
775 static int
print_v4l2_crop(struct tcb * const tcp,const kernel_ulong_t arg,const bool is_get)776 print_v4l2_crop(struct tcb *const tcp, const kernel_ulong_t arg,
777 		const bool is_get)
778 {
779 	struct v4l2_crop c;
780 
781 	if (entering(tcp)) {
782 		tprints(", ");
783 		if (umove_or_printaddr(tcp, arg, &c))
784 			return RVAL_IOCTL_DECODED;
785 		tprints("{type=");
786 		printxval(v4l2_buf_types, c.type, "V4L2_BUF_TYPE_???");
787 		if (is_get)
788 			return 0;
789 		tprintf(", c=" FMT_RECT, ARGS_RECT(c.c));
790 	} else {
791 		if (!syserror(tcp) && !umove(tcp, arg, &c))
792 			tprintf(", c=" FMT_RECT, ARGS_RECT(c.c));
793 	}
794 
795 	tprints("}");
796 
797 	return RVAL_IOCTL_DECODED;
798 }
799 
800 #ifdef VIDIOC_S_EXT_CTRLS
801 static bool
print_v4l2_ext_control(struct tcb * tcp,void * elem_buf,size_t elem_size,void * data)802 print_v4l2_ext_control(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
803 {
804 	const struct_v4l2_ext_control *p = elem_buf;
805 
806 	tprints("{id=");
807 	printxval(v4l2_control_ids, p->id, "V4L2_CID_???");
808 # if HAVE_DECL_V4L2_CTRL_TYPE_STRING
809 	tprintf(", size=%u", p->size);
810 	if (p->size > 0) {
811 		tprints(", string=");
812 		printstrn(tcp, ptr_to_kulong(p->string), p->size);
813 	} else
814 # endif
815 	tprintf(", value=%d, value64=%" PRId64, p->value, (int64_t) p->value64);
816 	tprints("}");
817 
818 	return true;
819 }
820 
821 #include "xlat/v4l2_control_classes.h"
822 
823 static int
print_v4l2_ext_controls(struct tcb * const tcp,const kernel_ulong_t arg,const bool is_get)824 print_v4l2_ext_controls(struct tcb *const tcp, const kernel_ulong_t arg,
825 			const bool is_get)
826 {
827 	struct_v4l2_ext_controls c;
828 
829 	if (entering(tcp)) {
830 		tprints(", ");
831 		if (umove_or_printaddr(tcp, arg, &c))
832 			return RVAL_IOCTL_DECODED;
833 		tprints("{ctrl_class=");
834 		printxval(v4l2_control_classes, c.ctrl_class,
835 			  "V4L2_CTRL_CLASS_???");
836 		tprintf(", count=%u", c.count);
837 		if (!c.count) {
838 			tprints("}");
839 			return RVAL_IOCTL_DECODED;
840 		}
841 		if (is_get)
842 			return 0;
843 		tprints(", ");
844 	} else {
845 		if (umove(tcp, arg, &c) < 0) {
846 			tprints("}");
847 			return RVAL_IOCTL_DECODED;
848 		}
849 		tprints(is_get ? ", " : " => ");
850 	}
851 
852 	tprints("controls=");
853 	struct_v4l2_ext_control ctrl;
854 	bool fail = !print_array(tcp, ptr_to_kulong(c.controls), c.count,
855 				 &ctrl, sizeof(ctrl),
856 				 umoven_or_printaddr_ignore_syserror,
857 				 print_v4l2_ext_control, 0);
858 
859 	if (exiting(tcp) && syserror(tcp))
860 		tprintf(", error_idx=%u", c.error_idx);
861 
862 	if (exiting(tcp) || fail) {
863 		tprints("}");
864 		return RVAL_IOCTL_DECODED;
865 	}
866 
867 	/* entering */
868 	return 0;
869 }
870 #endif /* VIDIOC_S_EXT_CTRLS */
871 
872 #ifdef VIDIOC_ENUM_FRAMESIZES
873 # include "xlat/v4l2_framesize_types.h"
874 
875 static int
print_v4l2_frmsizeenum(struct tcb * const tcp,const kernel_ulong_t arg)876 print_v4l2_frmsizeenum(struct tcb *const tcp, const kernel_ulong_t arg)
877 {
878 	struct v4l2_frmsizeenum s;
879 
880 	if (entering(tcp)) {
881 		tprints(", ");
882 		if (umove_or_printaddr(tcp, arg, &s))
883 			return RVAL_IOCTL_DECODED;
884 		tprintf("{index=%u, pixel_format=", s.index);
885 		print_pixelformat(s.pixel_format);
886 		return 0;
887 	}
888 
889 	if (!syserror(tcp) && !umove(tcp, arg, &s)) {
890 		tprints(", type=");
891 		printxval(v4l2_framesize_types, s.type, "V4L2_FRMSIZE_TYPE_???");
892 		switch (s.type) {
893 		case V4L2_FRMSIZE_TYPE_DISCRETE:
894 			tprintf(", discrete={width=%u, height=%u}",
895 				s.discrete.width, s.discrete.height);
896 			break;
897 		case V4L2_FRMSIZE_TYPE_STEPWISE:
898 			tprintf(", stepwise={min_width=%u, max_width=%u, "
899 				"step_width=%u, min_height=%u, max_height=%u, "
900 				"step_height=%u}",
901 				s.stepwise.min_width, s.stepwise.max_width,
902 				s.stepwise.step_width, s.stepwise.min_height,
903 				s.stepwise.max_height, s.stepwise.step_height);
904 			break;
905 		}
906 	}
907 	tprints("}");
908 	return RVAL_IOCTL_DECODED;
909 }
910 #endif /* VIDIOC_ENUM_FRAMESIZES */
911 
912 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
913 # include "xlat/v4l2_frameinterval_types.h"
914 
915 static int
print_v4l2_frmivalenum(struct tcb * const tcp,const kernel_ulong_t arg)916 print_v4l2_frmivalenum(struct tcb *const tcp, const kernel_ulong_t arg)
917 {
918 	struct v4l2_frmivalenum f;
919 
920 	if (entering(tcp)) {
921 		tprints(", ");
922 		if (umove_or_printaddr(tcp, arg, &f))
923 			return RVAL_IOCTL_DECODED;
924 		tprintf("{index=%u, pixel_format=", f.index);
925 		print_pixelformat(f.pixel_format);
926 		tprintf(", width=%u, height=%u", f.width, f.height);
927 		return 0;
928 	}
929 
930 	if (!syserror(tcp) && !umove(tcp, arg, &f)) {
931 		tprints(", type=");
932 		printxval(v4l2_frameinterval_types, f.type,
933 			  "V4L2_FRMIVAL_TYPE_???");
934 		switch (f.type) {
935 		case V4L2_FRMIVAL_TYPE_DISCRETE:
936 			tprintf(", discrete=" FMT_FRACT,
937 				ARGS_FRACT(f.discrete));
938 			break;
939 		case V4L2_FRMIVAL_TYPE_STEPWISE:
940 		case V4L2_FRMSIZE_TYPE_CONTINUOUS:
941 			tprintf(", stepwise={min=" FMT_FRACT ", max="
942 				FMT_FRACT ", step=" FMT_FRACT "}",
943 				ARGS_FRACT(f.stepwise.min),
944 				ARGS_FRACT(f.stepwise.max),
945 				ARGS_FRACT(f.stepwise.step));
946 			break;
947 		}
948 	}
949 
950 	tprints("}");
951 
952 	return RVAL_IOCTL_DECODED;
953 }
954 #endif /* VIDIOC_ENUM_FRAMEINTERVALS */
955 
956 #ifdef VIDIOC_CREATE_BUFS
957 static int
print_v4l2_create_buffers(struct tcb * const tcp,const kernel_ulong_t arg)958 print_v4l2_create_buffers(struct tcb *const tcp, const kernel_ulong_t arg)
959 {
960 	static const char fmt[] = "{index=%u, count=%u}";
961 	static char outstr[sizeof(fmt) + sizeof(int) * 6];
962 
963 	struct_v4l2_create_buffers b;
964 
965 	if (entering(tcp)) {
966 		tprints(", ");
967 		if (umove_or_printaddr(tcp, arg, &b))
968 			return RVAL_IOCTL_DECODED;
969 		tprintf("{count=%u, memory=", b.count);
970 		printxval(v4l2_memories, b.memory, "V4L2_MEMORY_???");
971 		tprints(", format={type=");
972 		printxval(v4l2_buf_types, b.format.type,
973 			  "V4L2_BUF_TYPE_???");
974 		print_v4l2_format_fmt(tcp, ", ",
975 				      (struct_v4l2_format *) &b.format);
976 		tprints("}}");
977 		return 0;
978 	}
979 
980 	if (syserror(tcp) || umove(tcp, arg, &b))
981 		return RVAL_IOCTL_DECODED;
982 
983 	xsprintf(outstr, fmt, b.index, b.count);
984 	tcp->auxstr = outstr;
985 
986 	return RVAL_IOCTL_DECODED | RVAL_STR;
987 }
988 #endif /* VIDIOC_CREATE_BUFS */
989 
MPERS_PRINTER_DECL(int,v4l2_ioctl,struct tcb * const tcp,const unsigned int code,const kernel_ulong_t arg)990 MPERS_PRINTER_DECL(int, v4l2_ioctl, struct tcb *const tcp,
991 		   const unsigned int code, const kernel_ulong_t arg)
992 {
993 	if (!verbose(tcp))
994 		return RVAL_DECODED;
995 
996 	switch (code) {
997 	case VIDIOC_QUERYCAP: /* R */
998 		return print_v4l2_capability(tcp, arg);
999 
1000 	case VIDIOC_ENUM_FMT: /* RW */
1001 		return print_v4l2_fmtdesc(tcp, arg);
1002 
1003 	case VIDIOC_G_FMT: /* RW */
1004 	case VIDIOC_S_FMT: /* RW */
1005 	case VIDIOC_TRY_FMT: /* RW */
1006 		return print_v4l2_format(tcp, arg, code == VIDIOC_G_FMT);
1007 
1008 	case VIDIOC_REQBUFS: /* RW */
1009 		return print_v4l2_requestbuffers(tcp, arg);
1010 
1011 	case VIDIOC_QUERYBUF: /* RW */
1012 	case VIDIOC_QBUF: /* RW */
1013 	case VIDIOC_DQBUF: /* RW */
1014 		return print_v4l2_buffer(tcp, code, arg);
1015 
1016 	case VIDIOC_G_FBUF: /* R */
1017 		if (entering(tcp))
1018 			return 0;
1019 		/* fall through */
1020 	case VIDIOC_S_FBUF: /* W */
1021 		return print_v4l2_framebuffer(tcp, arg);
1022 
1023 	case VIDIOC_STREAMON: /* W */
1024 	case VIDIOC_STREAMOFF: /* W */
1025 		return print_v4l2_buf_type(tcp, arg);
1026 
1027 	case VIDIOC_G_PARM: /* RW */
1028 	case VIDIOC_S_PARM: /* RW */
1029 		return print_v4l2_streamparm(tcp, arg, code == VIDIOC_G_PARM);
1030 
1031 	case VIDIOC_G_STD: /* R */
1032 		if (entering(tcp))
1033 			return 0;
1034 		/* fall through */
1035 	case VIDIOC_S_STD: /* W */
1036 		tprints(", ");
1037 		printnum_int64(tcp, arg, "%#" PRIx64);
1038 		break;
1039 
1040 	case VIDIOC_ENUMSTD: /* RW */
1041 		return print_v4l2_standard(tcp, arg);
1042 
1043 	case VIDIOC_ENUMINPUT: /* RW */
1044 		return print_v4l2_input(tcp, arg);
1045 
1046 	case VIDIOC_G_CTRL: /* RW */
1047 	case VIDIOC_S_CTRL: /* RW */
1048 		return print_v4l2_control(tcp, arg, code == VIDIOC_G_CTRL);
1049 
1050 	case VIDIOC_G_TUNER: /* RW */
1051 	case VIDIOC_S_TUNER: /* RW */
1052 		return print_v4l2_tuner(tcp, arg, code == VIDIOC_G_TUNER);
1053 
1054 	case VIDIOC_QUERYCTRL: /* RW */
1055 		return print_v4l2_queryctrl(tcp, arg);
1056 
1057 	case VIDIOC_G_INPUT: /* R */
1058 		if (entering(tcp))
1059 			return 0;
1060 		/* fall through */
1061 	case VIDIOC_S_INPUT: /* RW */
1062 		tprints(", ");
1063 		printnum_int(tcp, arg, "%u");
1064 		break;
1065 
1066 	case VIDIOC_CROPCAP: /* RW */
1067 		return print_v4l2_cropcap(tcp, arg);
1068 
1069 	case VIDIOC_G_CROP: /* RW */
1070 	case VIDIOC_S_CROP: /* W */
1071 		return print_v4l2_crop(tcp, arg, code == VIDIOC_G_CROP);
1072 
1073 #ifdef VIDIOC_S_EXT_CTRLS
1074 	case VIDIOC_S_EXT_CTRLS: /* RW */
1075 	case VIDIOC_TRY_EXT_CTRLS: /* RW */
1076 	case VIDIOC_G_EXT_CTRLS: /* RW */
1077 		return print_v4l2_ext_controls(tcp, arg,
1078 					       code == VIDIOC_G_EXT_CTRLS);
1079 #endif /* VIDIOC_S_EXT_CTRLS */
1080 
1081 #ifdef VIDIOC_ENUM_FRAMESIZES
1082 	case VIDIOC_ENUM_FRAMESIZES: /* RW */
1083 		return print_v4l2_frmsizeenum(tcp, arg);
1084 #endif /* VIDIOC_ENUM_FRAMESIZES */
1085 
1086 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1087 	case VIDIOC_ENUM_FRAMEINTERVALS: /* RW */
1088 		return print_v4l2_frmivalenum(tcp, arg);
1089 #endif /* VIDIOC_ENUM_FRAMEINTERVALS */
1090 
1091 #ifdef VIDIOC_CREATE_BUFS
1092 	case VIDIOC_CREATE_BUFS: /* RW */
1093 		return print_v4l2_create_buffers(tcp, arg);
1094 #endif /* VIDIOC_CREATE_BUFS */
1095 
1096 	default:
1097 		return RVAL_DECODED;
1098 	}
1099 
1100 	return RVAL_IOCTL_DECODED;
1101 }
1102