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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/ioctl.h>
34 #include <linux/types.h>
35 #include <linux/videodev2.h>
36 /* some historical constants */
37 #ifndef V4L2_CID_HCENTER
38 #define V4L2_CID_HCENTER (V4L2_CID_BASE+22)
40 #ifndef V4L2_CID_VCENTER
41 #define V4L2_CID_VCENTER (V4L2_CID_BASE+23)
43 #ifndef V4L2_CID_BAND_STOP_FILTER
44 #define V4L2_CID_BAND_STOP_FILTER (V4L2_CID_BASE+33)
47 #include "xlat/v4l2_device_capabilities_flags.h"
48 #include "xlat/v4l2_buf_types.h"
49 #include "xlat/v4l2_buf_flags.h"
50 #include "xlat/v4l2_framesize_types.h"
51 #include "xlat/v4l2_frameinterval_types.h"
52 #include "xlat/v4l2_fields.h"
53 #include "xlat/v4l2_colorspaces.h"
54 #include "xlat/v4l2_format_description_flags.h"
55 #include "xlat/v4l2_memories.h"
56 #include "xlat/v4l2_control_ids.h"
57 #include "xlat/v4l2_control_types.h"
58 #include "xlat/v4l2_control_flags.h"
59 #include "xlat/v4l2_control_classes.h"
60 #include "xlat/v4l2_streaming_capabilities.h"
61 #include "xlat/v4l2_capture_modes.h"
62 #include "xlat/v4l2_input_types.h"
64 #define FMT_FRACT "%u/%u"
65 #define ARGS_FRACT(x) ((x).numerator), ((x).denominator)
67 #define FMT_RECT "{left=%i, top=%i, width=%i, height=%i}"
68 #define ARGS_RECT(x) (x).left, (x).top, (x).width, (x).height
70 static void print_pixelformat(uint32_t fourcc)
74 unsigned char cc[sizeof(uint32_t)];
85 tprints("v4l2_fourcc(");
86 for (i = 0; i < sizeof(u.cc); ++i) {
87 unsigned int c = u.cc[i];
92 (c >= '0' && c <= '9') ||
93 (c >= 'A' && c <= 'Z') ||
94 (c >= 'a' && c <= 'z')) {
106 "0123456789abcdef"[c >> 4],
107 "0123456789abcdef"[c & 0xf],
117 static void print_v4l2_format_fmt(const struct v4l2_format *f)
121 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
122 case V4L2_BUF_TYPE_VIDEO_OUTPUT: {
123 const struct v4l2_pix_format *pix = &f->fmt.pix;
125 tprintf("pix={width=%u, height=%u, pixelformat=",
126 pix->width, pix->height);
127 print_pixelformat(pix->pixelformat);
129 printxval(v4l2_fields, pix->field, "V4L2_FIELD_???");
130 tprintf(", bytesperline=%u, sizeimage=%u, colorspace=",
131 pix->bytesperline, pix->sizeimage);
132 printxval(v4l2_colorspaces, pix->colorspace,
133 "V4L2_COLORSPACE_???");
137 #if HAVE_DECL_V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
138 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
139 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: {
140 const struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
143 tprintf("pix_mp={width=%u, height=%u, pixelformat=",
144 pix_mp->width, pix_mp->height);
145 print_pixelformat(pix_mp->pixelformat);
147 printxval(v4l2_fields, pix_mp->field, "V4L2_FIELD_???");
148 tprints(", colorspace=");
149 printxval(v4l2_colorspaces, pix_mp->colorspace,
150 "V4L2_COLORSPACE_???");
151 tprints("plane_fmt=[");
152 max = pix_mp->num_planes;
153 if (max > VIDEO_MAX_PLANES)
154 max = VIDEO_MAX_PLANES;
155 for (i = 0; i < max; i++) {
158 tprintf("{sizeimage=%u, bytesperline=%u}",
159 pix_mp->plane_fmt[i].sizeimage,
160 pix_mp->plane_fmt[i].bytesperline);
162 tprintf("], num_planes=%u}", (unsigned) pix_mp->num_planes);
167 /* TODO: Complete this switch statement */
168 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
169 #if HAVE_DECL_V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
170 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
172 tprints("win={???}");
175 case V4L2_BUF_TYPE_VBI_CAPTURE:
176 case V4L2_BUF_TYPE_VBI_OUTPUT:
177 tprints("vbi={???}");
180 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
181 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
182 tprints("sliced={???}");
192 v4l2_ioctl(struct tcb *tcp, const unsigned int code, long arg)
198 case VIDIOC_QUERYCAP: /* decode on exit */ {
199 struct v4l2_capability caps;
201 if (entering(tcp) || syserror(tcp) || umove(tcp, arg, &caps) < 0)
203 tprints(", {driver=");
204 print_quoted_string((const char *) caps.driver,
205 sizeof(caps.driver), QUOTE_0_TERMINATED);
207 print_quoted_string((const char *) caps.card,
208 sizeof(caps.card), QUOTE_0_TERMINATED);
209 tprints(", bus_info=");
210 print_quoted_string((const char *) caps.bus_info,
211 sizeof(caps.bus_info), QUOTE_0_TERMINATED);
212 tprintf(", version=%u.%u.%u, capabilities=",
213 (caps.version >> 16) & 0xFF,
214 (caps.version >> 8) & 0xFF,
215 caps.version & 0xFF);
216 printflags(v4l2_device_capabilities_flags, caps.capabilities,
218 #ifdef V4L2_CAP_DEVICE_CAPS
219 tprints(", device_caps=");
220 printflags(v4l2_device_capabilities_flags, caps.device_caps,
227 #ifdef VIDIOC_ENUM_FRAMESIZES
228 case VIDIOC_ENUM_FRAMESIZES: /* decode on exit */ {
229 struct v4l2_frmsizeenum s;
231 if (entering(tcp) || umove(tcp, arg, &s) < 0)
233 tprintf(", {index=%u, pixel_format=", s.index);
234 print_pixelformat(s.pixel_format);
236 if (!syserror(tcp)) {
238 printxval(v4l2_framesize_types, s.type, "V4L2_FRMSIZE_TYPE_???");
240 case V4L2_FRMSIZE_TYPE_DISCRETE:
241 tprintf(", discrete={width=%u, height=%u}",
242 s.discrete.width, s.discrete.height);
244 case V4L2_FRMSIZE_TYPE_STEPWISE:
245 tprintf(", stepwise={min_width=%u, max_width=%u, "
246 "step_width=%u, min_height=%u, max_height=%u, "
248 s.stepwise.min_width, s.stepwise.max_width,
249 s.stepwise.step_width, s.stepwise.min_height,
250 s.stepwise.max_height, s.stepwise.step_height);
257 #endif /* VIDIOC_ENUM_FRAMESIZES */
261 case VIDIOC_TRY_FMT: {
262 struct v4l2_format f;
264 if (umove(tcp, arg, &f) < 0)
268 printxval(v4l2_buf_types, f.type, "V4L2_BUF_TYPE_???");
270 if ((entering(tcp) && code != VIDIOC_G_FMT)
271 || (exiting(tcp) && !syserror(tcp))) {
272 tprints(exiting(tcp) && code != VIDIOC_G_FMT ? " => " : ", ");
273 print_v4l2_format_fmt(&f);
280 case VIDIOC_ENUM_FMT: {
281 struct v4l2_fmtdesc f;
283 if (entering(tcp) || umove(tcp, arg, &f) < 0)
286 tprintf(", {index=%u", f.index);
287 if (!syserror(tcp)) {
289 printxval(v4l2_buf_types, f.type, "V4L2_BUF_TYPE_???");
291 printflags(v4l2_format_description_flags, f.flags,
292 "V4L2_FMT_FLAG_???");
293 tprints(", description=");
294 print_quoted_string((const char *) f.description,
295 sizeof(f.description),
297 tprints(", pixelformat=");
298 print_pixelformat(f.pixelformat);
305 case VIDIOC_S_PARM: {
306 struct v4l2_streamparm s;
308 if (entering(tcp) && code == VIDIOC_G_PARM)
310 if (exiting(tcp) && syserror(tcp))
311 return code == VIDIOC_S_PARM;
312 if (umove(tcp, arg, &s) < 0)
316 printxval(v4l2_buf_types, s.type, "V4L2_BUF_TYPE_???");
319 tprints(exiting(tcp) && code == VIDIOC_S_PARM ? " => {" : ", {");
320 if (s.type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
321 struct v4l2_captureparm *cap = &s.parm.capture;
323 tprints("capability=");
324 printflags(v4l2_streaming_capabilities,
325 cap->capability, "V4L2_CAP_???");
327 tprints(", capturemode=");
328 printflags(v4l2_capture_modes,
329 cap->capturemode, "V4L2_MODE_???");
331 tprintf(", timeperframe=" FMT_FRACT,
332 ARGS_FRACT(cap->timeperframe));
334 tprintf(", extendedmode=%u, readbuffers=%u",
345 case VIDIOC_QUERYCTRL: {
346 struct v4l2_queryctrl c;
348 if (umove(tcp, arg, &c) < 0)
350 /* 'id' field must be printed :
352 * on exit if !syserror(tcp) && V4L2_CTRL_FLAG_NEXT_CTRL was set
355 || (exiting(tcp) && tcp->auxstr && !syserror(tcp))) {
356 tprints(exiting(tcp) ? " => " : ", {id=");
357 #ifdef V4L2_CTRL_FLAG_NEXT_CTRL
358 tcp->auxstr = (c.id & V4L2_CTRL_FLAG_NEXT_CTRL) ? "" : NULL;
360 tprints("V4L2_CTRL_FLAG_NEXT_CTRL|");
361 c.id &= ~V4L2_CTRL_FLAG_NEXT_CTRL;
364 printxval(v4l2_control_ids, c.id, "V4L2_CID_???");
367 if (!syserror(tcp)) {
369 printxval(v4l2_control_types, c.type,
370 "V4L2_CTRL_TYPE_???");
372 print_quoted_string((const char *) c.name,
375 tprintf(", minimum=%i, maximum=%i, step=%i, "
376 "default_value=%i, flags=",
377 c.minimum, c.maximum,
378 c.step, c.default_value);
379 printflags(v4l2_control_flags, c.flags,
380 "V4L2_CTRL_FLAG_???");
388 case VIDIOC_S_CTRL: {
389 struct v4l2_control c;
391 if (entering(tcp) || umove(tcp, arg, &c) < 0)
394 printxval(v4l2_control_ids, c.id, "V4L2_CID_???");
395 if (!syserror(tcp) || code != VIDIOC_G_CTRL)
396 tprintf(", value=%i", c.value);
401 #ifdef VIDIOC_S_EXT_CTRLS
402 case VIDIOC_S_EXT_CTRLS:
403 case VIDIOC_TRY_EXT_CTRLS:
404 case VIDIOC_G_EXT_CTRLS: {
405 struct v4l2_ext_controls c;
407 bool must_print_values;
409 if (entering(tcp) && code == VIDIOC_G_EXT_CTRLS)
411 if (exiting(tcp) && syserror(tcp) && code != VIDIOC_G_EXT_CTRLS)
413 must_print_values = ((entering(tcp) && code != VIDIOC_G_EXT_CTRLS)
414 || (exiting(tcp) && !syserror(tcp)));
415 if (umove(tcp, arg, &c) < 0)
417 tprints(code != VIDIOC_G_EXT_CTRLS && exiting(tcp) ? " => " : ", ");
418 tprints("{ctrl_class=");
419 printxval(v4l2_control_classes, c.ctrl_class,
420 "V4L2_CTRL_CLASS_???");
421 tprintf(", count=%u", c.count);
422 if (exiting(tcp) && syserror(tcp))
423 tprintf(", error_idx=%u", c.error_idx);
424 tprints(", controls=[");
425 for (n = 0; n < c.count; ++n) {
426 struct v4l2_ext_control ctrl;
430 if (umove(tcp, (long) (c.controls + n), &ctrl) < 0)
432 if (abbrev(tcp) && n == 2) {
437 printxval(v4l2_control_ids, ctrl.id, "V4L2_CID_???");
438 # if HAVE_DECL_V4L2_CTRL_TYPE_STRING
439 tprintf(", size=%u", ctrl.size);
441 if (must_print_values) {
442 tprints(", string=");
443 printstr(tcp, (long) ctrl.string, ctrl.size);
448 if (must_print_values) {
449 tprintf(", value=%i, value64=%lld", ctrl.value,
450 (long long) ctrl.value64);
458 #endif /* VIDIOC_S_EXT_CTRLS */
460 case VIDIOC_ENUMSTD: {
461 struct v4l2_standard s;
463 if (umove(tcp, arg, &s) < 0)
466 tprintf(", {index=%i", s.index);
468 if (!syserror(tcp)) {
470 print_quoted_string((const char *) s.name,
473 tprintf(", frameperiod=" FMT_FRACT, ARGS_FRACT(s.frameperiod));
474 tprintf(", framelines=%i", s.framelines);
485 if (code == VIDIOC_G_STD && exiting(tcp) && syserror(tcp))
487 if (umove(tcp, arg, &s) < 0)
489 if ((code == VIDIOC_S_STD) == entering(tcp))
490 tprintf(", std=%#llx", (unsigned long long) s);
494 case VIDIOC_ENUMINPUT: {
497 if (entering(tcp) || umove(tcp, arg, &i) < 0)
499 tprintf(", {index=%i", i.index);
500 if (!syserror(tcp)) {
502 print_quoted_string((const char *) i.name,
503 sizeof(i.name), QUOTE_0_TERMINATED);
505 printxval(v4l2_input_types, i.type,
506 "V4L2_INPUT_TYPE_???");
513 case VIDIOC_S_INPUT: {
516 if (entering(tcp) || syserror(tcp) || umove(tcp, arg, &index) < 0)
519 tprintf(", index=%i", index);
523 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
524 case VIDIOC_ENUM_FRAMEINTERVALS: {
525 struct v4l2_frmivalenum f;
527 if (entering(tcp) || umove(tcp, arg, &f) < 0)
529 tprintf(", {index=%i, pixel_format=", f.index);
530 print_pixelformat(f.pixel_format);
531 tprintf(", width=%u, height=%u", f.width, f.height);
532 if (!syserror(tcp)) {
534 printxval(v4l2_frameinterval_types, f.type,
535 "V4L2_FRMIVAL_TYPE_???");
537 case V4L2_FRMIVAL_TYPE_DISCRETE:
538 tprintf(", discrete=" FMT_FRACT,
539 ARGS_FRACT(f.discrete));
541 case V4L2_FRMIVAL_TYPE_STEPWISE:
542 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
543 tprintf(", stepwise={min=" FMT_FRACT ", max="
544 FMT_FRACT ", step=" FMT_FRACT "}",
545 ARGS_FRACT(f.stepwise.min),
546 ARGS_FRACT(f.stepwise.max),
547 ARGS_FRACT(f.stepwise.step));
554 #endif /* VIDIOC_ENUM_FRAMEINTERVALS */
556 case VIDIOC_CROPCAP: {
557 struct v4l2_cropcap c;
559 if (entering(tcp) || umove(tcp, arg, &c) < 0)
562 printxval(v4l2_buf_types, c.type, "V4L2_BUF_TYPE_???");
564 tprintf(", bounds=" FMT_RECT ", defrect=" FMT_RECT ", "
565 "pixelaspect=" FMT_FRACT, ARGS_RECT(c.bounds),
566 ARGS_RECT(c.defrect), ARGS_FRACT(c.pixelaspect));
571 case VIDIOC_S_CROP: {
574 if (exiting(tcp) || umove(tcp, arg, &c) < 0)
577 printxval(v4l2_buf_types, c.type, "V4L2_BUF_TYPE_???");
578 tprintf(", c=" FMT_RECT "}", ARGS_RECT(c.c));
583 case VIDIOC_S_FBUF: {
584 struct v4l2_framebuffer b;
586 if (syserror(tcp) && code == VIDIOC_G_FBUF)
588 if (entering(tcp) || umove(tcp, arg, &b) < 0)
590 tprintf(", {capability=%x, flags=%x, base=%p}",
591 b.capability, b.flags, b.base);
595 #ifdef VIDIOC_CREATE_BUFS
596 case VIDIOC_CREATE_BUFS: {
597 struct v4l2_create_buffers b;
599 if (exiting(tcp) && syserror(tcp))
601 if (umove(tcp, arg, &b) < 0)
604 tprintf(", {count=%u, memory=", b.count);
605 printxval(v4l2_memories, b.memory, "V4L2_MEMORY_???");
606 tprints(", format={type=");
607 printxval(v4l2_buf_types, b.format.type, "V4L2_BUF_TYPE_???");
609 print_v4l2_format_fmt(&b.format);
613 static const char fmt[] = "{index=%u, count=%u}";
614 static char outstr[sizeof(fmt) + sizeof(int) * 6];
616 sprintf(outstr, fmt, b.index, b.count);
617 tcp->auxstr = outstr;
621 #endif /* VIDIOC_CREATE_BUFS */
623 case VIDIOC_REQBUFS: {
624 struct v4l2_requestbuffers reqbufs;
626 if (umove(tcp, arg, &reqbufs) < 0)
629 tprintf(", {count=%u, type=", reqbufs.count);
630 printxval(v4l2_buf_types, reqbufs.type, "V4L2_BUF_TYPE_???");
631 tprints(", memory=");
632 printxval(v4l2_memories, reqbufs.memory, "V4L2_MEMORY_???");
635 } else if (syserror(tcp))
638 static char outstr[sizeof("{count=}") + sizeof(int) * 3];
640 sprintf(outstr, "{count=%u}", reqbufs.count);
641 tcp->auxstr = outstr;
646 case VIDIOC_QUERYBUF:
649 struct v4l2_buffer b;
651 if (umove(tcp, arg, &b) < 0)
655 printxval(v4l2_buf_types, b.type, "V4L2_BUF_TYPE_???");
656 if (code != VIDIOC_DQBUF)
657 tprintf(", index=%u", b.index);
659 if (!syserror(tcp)) {
660 if (code == VIDIOC_DQBUF)
661 tprintf(", index=%u", b.index);
662 tprints(", memory=");
663 printxval(v4l2_memories, b.memory, "V4L2_MEMORY_???");
665 if (b.memory == V4L2_MEMORY_MMAP) {
666 tprintf(", m.offset=%#x", b.m.offset);
667 } else if (b.memory == V4L2_MEMORY_USERPTR) {
668 tprintf(", m.userptr=%#lx", b.m.userptr);
671 tprintf(", length=%u, bytesused=%u, flags=",
672 b.length, b.bytesused);
673 printflags(v4l2_buf_flags, b.flags, "V4L2_BUF_FLAG_???");
674 if (code == VIDIOC_DQBUF)
675 tprintf(", timestamp = {%ju.%06ju}",
676 (uintmax_t)b.timestamp.tv_sec,
677 (uintmax_t)b.timestamp.tv_usec);
685 case VIDIOC_STREAMON:
686 case VIDIOC_STREAMOFF: {
689 if (umove(tcp, arg, &type) < 0)
693 printxval(v4l2_buf_types, type, "V4L2_BUF_TYPE_???");
698 default: /* decode on exit */