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