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