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/videodev2.h>
35 /* some historical constants */
36 #ifndef V4L2_CID_HCENTER
37 #define V4L2_CID_HCENTER (V4L2_CID_BASE+22)
39 #ifndef V4L2_CID_VCENTER
40 #define V4L2_CID_VCENTER (V4L2_CID_BASE+23)
42 #ifndef V4L2_CID_BAND_STOP_FILTER
43 #define V4L2_CID_BAND_STOP_FILTER (V4L2_CID_BASE+33)
46 #include "xlat/v4l2_device_capabilities_flags.h"
47 #include "xlat/v4l2_buf_types.h"
48 #include "xlat/v4l2_buf_flags.h"
49 #include "xlat/v4l2_framesize_types.h"
50 #include "xlat/v4l2_frameinterval_types.h"
51 #include "xlat/v4l2_fields.h"
52 #include "xlat/v4l2_colorspaces.h"
53 #include "xlat/v4l2_format_description_flags.h"
54 #include "xlat/v4l2_memories.h"
55 #include "xlat/v4l2_control_ids.h"
56 #include "xlat/v4l2_control_types.h"
57 #include "xlat/v4l2_control_flags.h"
58 #include "xlat/v4l2_control_classes.h"
59 #include "xlat/v4l2_streaming_capabilities.h"
60 #include "xlat/v4l2_capture_modes.h"
61 #include "xlat/v4l2_input_types.h"
63 #define FMT_FRACT "%u/%u"
64 #define ARGS_FRACT(x) ((x).numerator), ((x).denominator)
66 #define FMT_RECT "{left=%i, top=%i, width=%i, height=%i}"
67 #define ARGS_RECT(x) (x).left, (x).top, (x).width, (x).height
69 static void print_pixelformat(uint32_t fourcc)
73 unsigned char cc[sizeof(uint32_t)];
84 tprints("v4l2_fourcc(");
85 for (i = 0; i < sizeof(u.cc); ++i) {
86 unsigned int c = u.cc[i];
91 (c >= '0' && c <= '9') ||
92 (c >= 'A' && c <= 'Z') ||
93 (c >= 'a' && c <= 'z')) {
105 "0123456789abcdef"[c >> 4],
106 "0123456789abcdef"[c & 0xf],
116 static void print_v4l2_format_fmt(const struct v4l2_format *f)
120 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
121 case V4L2_BUF_TYPE_VIDEO_OUTPUT: {
122 const struct v4l2_pix_format *pix = &f->fmt.pix;
124 tprintf("pix={width=%u, height=%u, pixelformat=",
125 pix->width, pix->height);
126 print_pixelformat(pix->pixelformat);
128 printxval(v4l2_fields, pix->field, "V4L2_FIELD_???");
129 tprintf(", bytesperline=%u, sizeimage=%u, colorspace=",
130 pix->bytesperline, pix->sizeimage);
131 printxval(v4l2_colorspaces, pix->colorspace,
132 "V4L2_COLORSPACE_???");
136 #if HAVE_DECL_V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
137 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
138 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: {
139 const struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
142 tprintf("pix_mp={width=%u, height=%u, pixelformat=",
143 pix_mp->width, pix_mp->height);
144 print_pixelformat(pix_mp->pixelformat);
146 printxval(v4l2_fields, pix_mp->field, "V4L2_FIELD_???");
147 tprints(", colorspace=");
148 printxval(v4l2_colorspaces, pix_mp->colorspace,
149 "V4L2_COLORSPACE_???");
150 tprints("plane_fmt=[");
151 max = pix_mp->num_planes;
152 if (max > VIDEO_MAX_PLANES)
153 max = VIDEO_MAX_PLANES;
154 for (i = 0; i < max; i++) {
157 tprintf("{sizeimage=%u, bytesperline=%u}",
158 pix_mp->plane_fmt[i].sizeimage,
159 pix_mp->plane_fmt[i].bytesperline);
161 tprintf("], num_planes=%u}", (unsigned) pix_mp->num_planes);
166 /* TODO: Complete this switch statement */
167 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
168 #if HAVE_DECL_V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY
169 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
171 tprints("win={???}");
174 case V4L2_BUF_TYPE_VBI_CAPTURE:
175 case V4L2_BUF_TYPE_VBI_OUTPUT:
176 tprints("vbi={???}");
179 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
180 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
181 tprints("sliced={???}");
191 v4l2_ioctl(struct tcb *tcp, const unsigned int code, long arg)
197 case VIDIOC_QUERYCAP: /* decode on exit */ {
198 struct v4l2_capability caps;
200 if (entering(tcp) || syserror(tcp) || umove(tcp, arg, &caps) < 0)
202 tprints(", {driver=");
203 print_quoted_string((const char *) caps.driver,
204 sizeof(caps.driver), QUOTE_0_TERMINATED);
206 print_quoted_string((const char *) caps.card,
207 sizeof(caps.card), QUOTE_0_TERMINATED);
208 tprints(", bus_info=");
209 print_quoted_string((const char *) caps.bus_info,
210 sizeof(caps.bus_info), QUOTE_0_TERMINATED);
211 tprintf(", version=%u.%u.%u, capabilities=",
212 (caps.version >> 16) & 0xFF,
213 (caps.version >> 8) & 0xFF,
214 caps.version & 0xFF);
215 printflags(v4l2_device_capabilities_flags, caps.capabilities,
217 #ifdef V4L2_CAP_DEVICE_CAPS
218 tprints(", device_caps=");
219 printflags(v4l2_device_capabilities_flags, caps.device_caps,
226 #ifdef VIDIOC_ENUM_FRAMESIZES
227 case VIDIOC_ENUM_FRAMESIZES: /* decode on exit */ {
228 struct v4l2_frmsizeenum s;
230 if (entering(tcp) || umove(tcp, arg, &s) < 0)
232 tprintf(", {index=%u, pixel_format=", s.index);
233 print_pixelformat(s.pixel_format);
235 if (!syserror(tcp)) {
237 printxval(v4l2_framesize_types, s.type, "V4L2_FRMSIZE_TYPE_???");
239 case V4L2_FRMSIZE_TYPE_DISCRETE:
240 tprintf(", discrete={width=%u, height=%u}",
241 s.discrete.width, s.discrete.height);
243 case V4L2_FRMSIZE_TYPE_STEPWISE:
244 tprintf(", stepwise={min_width=%u, max_width=%u, "
245 "step_width=%u, min_height=%u, max_height=%u, "
247 s.stepwise.min_width, s.stepwise.max_width,
248 s.stepwise.step_width, s.stepwise.min_height,
249 s.stepwise.max_height, s.stepwise.step_height);
256 #endif /* VIDIOC_ENUM_FRAMESIZES */
260 case VIDIOC_TRY_FMT: {
261 struct v4l2_format f;
263 if (umove(tcp, arg, &f) < 0)
267 printxval(v4l2_buf_types, f.type, "V4L2_BUF_TYPE_???");
269 if ((entering(tcp) && code != VIDIOC_G_FMT)
270 || (exiting(tcp) && !syserror(tcp))) {
271 tprints(exiting(tcp) && code != VIDIOC_G_FMT ? " => " : ", ");
272 print_v4l2_format_fmt(&f);
279 case VIDIOC_ENUM_FMT: {
280 struct v4l2_fmtdesc f;
282 if (entering(tcp) || umove(tcp, arg, &f) < 0)
285 tprintf(", {index=%u", f.index);
286 if (!syserror(tcp)) {
288 printxval(v4l2_buf_types, f.type, "V4L2_BUF_TYPE_???");
290 printflags(v4l2_format_description_flags, f.flags,
291 "V4L2_FMT_FLAG_???");
292 tprints(", description=");
293 print_quoted_string((const char *) f.description,
294 sizeof(f.description),
296 tprints(", pixelformat=");
297 print_pixelformat(f.pixelformat);
304 case VIDIOC_S_PARM: {
305 struct v4l2_streamparm s;
307 if (entering(tcp) && code == VIDIOC_G_PARM)
309 if (exiting(tcp) && syserror(tcp))
310 return code == VIDIOC_S_PARM;
311 if (umove(tcp, arg, &s) < 0)
315 printxval(v4l2_buf_types, s.type, "V4L2_BUF_TYPE_???");
318 tprints(exiting(tcp) && code == VIDIOC_S_PARM ? " => {" : ", {");
319 if (s.type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
320 struct v4l2_captureparm *cap = &s.parm.capture;
322 tprints("capability=");
323 printflags(v4l2_streaming_capabilities,
324 cap->capability, "V4L2_CAP_???");
326 tprints(", capturemode=");
327 printflags(v4l2_capture_modes,
328 cap->capturemode, "V4L2_MODE_???");
330 tprintf(", timeperframe=" FMT_FRACT,
331 ARGS_FRACT(cap->timeperframe));
333 tprintf(", extendedmode=%u, readbuffers=%u",
344 case VIDIOC_QUERYCTRL: {
345 struct v4l2_queryctrl c;
347 if (umove(tcp, arg, &c) < 0)
349 /* 'id' field must be printed :
351 * on exit if !syserror(tcp) && V4L2_CTRL_FLAG_NEXT_CTRL was set
354 || (exiting(tcp) && tcp->auxstr && !syserror(tcp))) {
355 tprints(exiting(tcp) ? " => " : ", {id=");
356 tcp->auxstr = (c.id & V4L2_CTRL_FLAG_NEXT_CTRL) ? "" : NULL;
358 tprints("V4L2_CTRL_FLAG_NEXT_CTRL|");
359 c.id &= ~V4L2_CTRL_FLAG_NEXT_CTRL;
361 printxval(v4l2_control_ids, c.id, "V4L2_CID_???");
364 if (!syserror(tcp)) {
366 printxval(v4l2_control_types, c.type,
367 "V4L2_CTRL_TYPE_???");
369 print_quoted_string((const char *) c.name,
372 tprintf(", minimum=%i, maximum=%i, step=%i, "
373 "default_value=%i, flags=",
374 c.minimum, c.maximum,
375 c.step, c.default_value);
376 printflags(v4l2_control_flags, c.flags,
377 "V4L2_CTRL_FLAG_???");
385 case VIDIOC_S_CTRL: {
386 struct v4l2_control c;
388 if (entering(tcp) || umove(tcp, arg, &c) < 0)
391 printxval(v4l2_control_ids, c.id, "V4L2_CID_???");
392 if (!syserror(tcp) || code != VIDIOC_G_CTRL)
393 tprintf(", value=%i", c.value);
398 case VIDIOC_S_EXT_CTRLS:
399 case VIDIOC_TRY_EXT_CTRLS:
400 case VIDIOC_G_EXT_CTRLS: {
401 struct v4l2_ext_controls c;
403 bool must_print_values;
405 if (entering(tcp) && code == VIDIOC_G_EXT_CTRLS)
407 if (exiting(tcp) && syserror(tcp) && code != VIDIOC_G_EXT_CTRLS)
409 must_print_values = ((entering(tcp) && code != VIDIOC_G_EXT_CTRLS)
410 || (exiting(tcp) && !syserror(tcp)));
411 if (umove(tcp, arg, &c) < 0)
413 tprints(code != VIDIOC_G_EXT_CTRLS && exiting(tcp) ? " => " : ", ");
414 tprints("{ctrl_class=");
415 printxval(v4l2_control_classes, c.ctrl_class,
416 "V4L2_CTRL_CLASS_???");
417 tprintf(", count=%u", c.count);
418 if (exiting(tcp) && syserror(tcp))
419 tprintf(", error_idx=%u", c.error_idx);
420 tprints(", controls=[");
421 for (n = 0; n < c.count; ++n) {
422 struct v4l2_ext_control ctrl;
426 if (umove(tcp, (long) (c.controls + n), &ctrl) < 0)
428 if (abbrev(tcp) && n == 2) {
433 printxval(v4l2_control_ids, ctrl.id, "V4L2_CID_???");
434 #if HAVE_DECL_V4L2_CTRL_TYPE_STRING
435 tprintf(", size=%u", ctrl.size);
437 if (must_print_values) {
438 tprints(", string=");
439 printstr(tcp, (long) ctrl.string, ctrl.size);
444 if (must_print_values) {
445 tprintf(", value=%i, value64=%lli", ctrl.value,
455 case VIDIOC_ENUMSTD: {
456 struct v4l2_standard s;
458 if (umove(tcp, arg, &s) < 0)
461 tprintf(", {index=%i", s.index);
463 if (!syserror(tcp)) {
465 print_quoted_string((const char *) s.name,
468 tprintf(", frameperiod=" FMT_FRACT, ARGS_FRACT(s.frameperiod));
469 tprintf(", framelines=%i", s.framelines);
480 if (code == VIDIOC_G_STD && exiting(tcp) && syserror(tcp))
482 if (umove(tcp, arg, &s) < 0)
484 if ((code == VIDIOC_S_STD) == entering(tcp))
485 tprintf(", std=%#llx", s);
489 case VIDIOC_ENUMINPUT: {
492 if (entering(tcp) || umove(tcp, arg, &i) < 0)
494 tprintf(", {index=%i", i.index);
495 if (!syserror(tcp)) {
497 print_quoted_string((const char *) i.name,
498 sizeof(i.name), QUOTE_0_TERMINATED);
500 printxval(v4l2_input_types, i.type,
501 "V4L2_INPUT_TYPE_???");
508 case VIDIOC_S_INPUT: {
511 if (entering(tcp) || syserror(tcp) || umove(tcp, arg, &index) < 0)
514 tprintf(", index=%i", index);
518 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
519 case VIDIOC_ENUM_FRAMEINTERVALS: {
520 struct v4l2_frmivalenum f;
522 if (entering(tcp) || umove(tcp, arg, &f) < 0)
524 tprintf(", {index=%i, pixel_format=", f.index);
525 print_pixelformat(f.pixel_format);
526 tprintf(", width=%u, height=%u", f.width, f.height);
527 if (!syserror(tcp)) {
529 printxval(v4l2_frameinterval_types, f.type,
530 "V4L2_FRMIVAL_TYPE_???");
532 case V4L2_FRMIVAL_TYPE_DISCRETE:
533 tprintf(", discrete=" FMT_FRACT,
534 ARGS_FRACT(f.discrete));
536 case V4L2_FRMIVAL_TYPE_STEPWISE:
537 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
538 tprintf(", stepwise={min=" FMT_FRACT ", max="
539 FMT_FRACT ", step=" FMT_FRACT "}",
540 ARGS_FRACT(f.stepwise.min),
541 ARGS_FRACT(f.stepwise.max),
542 ARGS_FRACT(f.stepwise.step));
549 #endif /* VIDIOC_ENUM_FRAMEINTERVALS */
551 case VIDIOC_CROPCAP: {
552 struct v4l2_cropcap c;
554 if (entering(tcp) || umove(tcp, arg, &c) < 0)
557 printxval(v4l2_buf_types, c.type, "V4L2_BUF_TYPE_???");
560 tprintf(", bounds=" FMT_RECT ", defrect=" FMT_RECT ", "
561 "pixelaspect=" FMT_FRACT, ARGS_RECT(c.bounds),
562 ARGS_RECT(c.defrect), ARGS_FRACT(c.pixelaspect));
567 case VIDIOC_S_FBUF: {
568 struct v4l2_framebuffer b;
570 if (syserror(tcp) && code == VIDIOC_G_FBUF)
572 if (entering(tcp) || umove(tcp, arg, &b) < 0)
574 tprintf(", {capability=%x, flags=%x, base=%p}",
575 b.capability, b.flags, b.base);
579 case VIDIOC_REQBUFS: {
580 struct v4l2_requestbuffers reqbufs;
582 if (umove(tcp, arg, &reqbufs) < 0)
585 tprintf(", {count=%u, type=", reqbufs.count);
586 printxval(v4l2_buf_types, reqbufs.type, "V4L2_BUF_TYPE_???");
587 tprints(", memory=");
588 printxval(v4l2_memories, reqbufs.memory, "V4L2_MEMORY_???");
591 } else if (syserror(tcp))
594 static char outstr[sizeof("{count=}") + sizeof(int) * 3];
596 sprintf(outstr, "{count=%u}", reqbufs.count);
597 tcp->auxstr = outstr;
602 case VIDIOC_QUERYBUF:
605 struct v4l2_buffer b;
607 if (umove(tcp, arg, &b) < 0)
611 printxval(v4l2_buf_types, b.type, "V4L2_BUF_TYPE_???");
612 if (code != VIDIOC_DQBUF)
613 tprintf(", index=%u", b.index);
615 if (!syserror(tcp)) {
616 if (code == VIDIOC_DQBUF)
617 tprintf(", index=%u", b.index);
618 tprints(", memory=");
619 printxval(v4l2_memories, b.memory, "V4L2_MEMORY_???");
621 if (b.memory == V4L2_MEMORY_MMAP) {
622 tprintf(", m.offset=%#x", b.m.offset);
623 } else if (b.memory == V4L2_MEMORY_USERPTR) {
624 tprintf(", m.userptr=%#lx", b.m.userptr);
627 tprintf(", length=%u, bytesused=%u, flags=",
628 b.length, b.bytesused);
629 printflags(v4l2_buf_flags, b.flags, "V4L2_BUF_FLAG_???");
630 if (code == VIDIOC_DQBUF)
631 tprintf(", timestamp = {%lu.%06lu}",
633 b.timestamp.tv_usec);
641 case VIDIOC_STREAMON:
642 case VIDIOC_STREAMOFF: {
645 if (umove(tcp, arg, &type) < 0)
649 printxval(v4l2_buf_types, type, "V4L2_BUF_TYPE_???");
654 default: /* decode on exit */