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