2 * V4L2 subdev interface library
4 * Copyright (C) 2010-2011 Ideas on board SPRL
6 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/ioctl.h>
24 #include <sys/types.h>
35 #include <linux/v4l2-subdev.h>
38 #include "mediactl-priv.h"
40 #include "v4l2subdev.h"
42 int v4l2_subdev_open(struct media_entity *entity)
47 entity->fd = open(entity->devname, O_RDWR);
48 if (entity->fd == -1) {
50 media_dbg(entity->media,
51 "%s: Failed to open subdev device node %s\n", __func__,
59 void v4l2_subdev_close(struct media_entity *entity)
65 int v4l2_subdev_get_format(struct media_entity *entity,
66 struct v4l2_mbus_framefmt *format, unsigned int pad,
67 enum v4l2_subdev_format_whence which)
69 struct v4l2_subdev_format fmt;
72 ret = v4l2_subdev_open(entity);
76 memset(&fmt, 0, sizeof(fmt));
80 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FMT, &fmt);
88 int v4l2_subdev_set_format(struct media_entity *entity,
89 struct v4l2_mbus_framefmt *format, unsigned int pad,
90 enum v4l2_subdev_format_whence which)
92 struct v4l2_subdev_format fmt;
95 ret = v4l2_subdev_open(entity);
99 memset(&fmt, 0, sizeof(fmt));
102 fmt.format = *format;
104 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FMT, &fmt);
108 *format = fmt.format;
112 int v4l2_subdev_get_selection(struct media_entity *entity,
113 struct v4l2_rect *rect, unsigned int pad, unsigned int target,
114 enum v4l2_subdev_format_whence which)
117 struct v4l2_subdev_selection sel;
118 struct v4l2_subdev_crop crop;
122 ret = v4l2_subdev_open(entity);
126 memset(&u.sel, 0, sizeof(u.sel));
128 u.sel.target = target;
131 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_SELECTION, &u.sel);
136 if (errno != ENOTTY || target != V4L2_SEL_TGT_CROP)
139 memset(&u.crop, 0, sizeof(u.crop));
141 u.crop.which = which;
143 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_CROP, &u.crop);
151 int v4l2_subdev_set_selection(struct media_entity *entity,
152 struct v4l2_rect *rect, unsigned int pad, unsigned int target,
153 enum v4l2_subdev_format_whence which)
156 struct v4l2_subdev_selection sel;
157 struct v4l2_subdev_crop crop;
161 ret = v4l2_subdev_open(entity);
165 memset(&u.sel, 0, sizeof(u.sel));
167 u.sel.target = target;
171 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_SELECTION, &u.sel);
176 if (errno != ENOTTY || target != V4L2_SEL_TGT_CROP)
179 memset(&u.crop, 0, sizeof(u.crop));
181 u.crop.which = which;
184 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CROP, &u.crop);
192 int v4l2_subdev_get_frame_interval(struct media_entity *entity,
193 struct v4l2_fract *interval)
195 struct v4l2_subdev_frame_interval ival;
198 ret = v4l2_subdev_open(entity);
202 memset(&ival, 0, sizeof(ival));
204 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FRAME_INTERVAL, &ival);
208 *interval = ival.interval;
212 int v4l2_subdev_set_frame_interval(struct media_entity *entity,
213 struct v4l2_fract *interval)
215 struct v4l2_subdev_frame_interval ival;
218 ret = v4l2_subdev_open(entity);
222 memset(&ival, 0, sizeof(ival));
223 ival.interval = *interval;
225 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &ival);
229 *interval = ival.interval;
233 static int v4l2_subdev_parse_format(struct media_device *media,
234 struct v4l2_mbus_framefmt *format,
235 const char *p, char **endp)
237 enum v4l2_mbus_pixelcode code;
238 unsigned int width, height;
242 * Compatibility with the old syntax: consider space as valid
243 * separator between the media bus pixel code and the size.
245 for (; isspace(*p); ++p);
246 for (end = (char *)p;
247 *end != '/' && *end != ' ' && *end != '\0'; ++end);
249 code = v4l2_subdev_string_to_pixelcode(p, end - p);
250 if (code == (enum v4l2_mbus_pixelcode)-1) {
251 media_dbg(media, "Invalid pixel code '%.*s'\n", end - p, p);
256 width = strtoul(p, &end, 10);
258 media_dbg(media, "Expected 'x'\n");
263 height = strtoul(p, &end, 10);
266 memset(format, 0, sizeof(*format));
267 format->width = width;
268 format->height = height;
274 static int v4l2_subdev_parse_rectangle(struct media_device *media,
275 struct v4l2_rect *r, const char *p,
281 media_dbg(media, "Expected '('\n");
282 *endp = (char *)p - 1;
286 r->left = strtoul(p, &end, 10);
288 media_dbg(media, "Expected ','\n");
294 r->top = strtoul(p, &end, 10);
296 media_dbg(media, "Expected ')'\n");
301 media_dbg(media, "Expected '/'\n");
307 r->width = strtoul(p, &end, 10);
309 media_dbg(media, "Expected 'x'\n");
315 r->height = strtoul(p, &end, 10);
321 static int v4l2_subdev_parse_frame_interval(struct media_device *media,
322 struct v4l2_fract *interval,
323 const char *p, char **endp)
327 for (; isspace(*p); ++p);
329 interval->numerator = strtoul(p, &end, 10);
331 for (p = end; isspace(*p); ++p);
333 media_dbg(media, "Expected '/'\n");
334 *endp = (char *)p - 1;
338 for (; isspace(*p); ++p);
339 interval->denominator = strtoul(p, &end, 10);
346 * The debate over whether this function should be named icanhasstr() instead
347 * has been strong and heated. If you feel like this would be an important
348 * change, patches are welcome (or not).
350 static bool strhazit(const char *str, const char **p)
352 int len = strlen(str);
354 if (strncmp(str, *p, len))
357 for (*p += len; isspace(**p); ++*p);
361 static struct media_pad *v4l2_subdev_parse_pad_format(
362 struct media_device *media, struct v4l2_mbus_framefmt *format,
363 struct v4l2_rect *crop, struct v4l2_rect *compose,
364 struct v4l2_fract *interval, const char *p, char **endp)
366 struct media_pad *pad;
371 for (; isspace(*p); ++p);
373 pad = media_parse_pad(media, p, &end);
379 for (p = end; isspace(*p); ++p);
381 media_dbg(media, "Expected '['\n");
382 *endp = (char *)p - 1;
386 for (first = true; ; first = false) {
387 for (; isspace(*p); p++);
390 * Backward compatibility: if the first property starts with an
391 * uppercase later, process it as a format description.
393 if (strhazit("fmt:", &p) || (first && isupper(*p))) {
394 ret = v4l2_subdev_parse_format(media, format, p, &end);
404 if (strhazit("field:", &p)) {
405 enum v4l2_field field;
407 for (end = (char *)p; isalpha(*end) || *end == '-'; ++end);
409 field = v4l2_subdev_string_to_field(p, end - p);
410 if (field == (enum v4l2_field)-1) {
411 media_dbg(media, "Invalid field value '%*s'\n", end - p, p);
416 format->field = field;
423 * Backward compatibility: crop rectangles can be specified
424 * implicitly without the 'crop:' property name.
426 if (strhazit("crop:", &p) || *p == '(') {
427 ret = v4l2_subdev_parse_rectangle(media, crop, p, &end);
437 if (strhazit("compose:", &p)) {
438 ret = v4l2_subdev_parse_rectangle(media, compose, p, &end);
444 for (p = end; isspace(*p); p++);
449 ret = v4l2_subdev_parse_frame_interval(media, interval, ++p, &end);
463 media_dbg(media, "Expected ']'\n");
468 *endp = (char *)p + 1;
472 static int set_format(struct media_pad *pad,
473 struct v4l2_mbus_framefmt *format)
477 if (format->width == 0 || format->height == 0)
480 media_dbg(pad->entity->media,
481 "Setting up format %s %ux%u on pad %s/%u\n",
482 v4l2_subdev_pixelcode_to_string(format->code),
483 format->width, format->height,
484 pad->entity->info.name, pad->index);
486 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
487 V4L2_SUBDEV_FORMAT_ACTIVE);
489 media_dbg(pad->entity->media,
490 "Unable to set format: %s (%d)\n",
491 strerror(-ret), ret);
495 media_dbg(pad->entity->media,
496 "Format set: %s %ux%u\n",
497 v4l2_subdev_pixelcode_to_string(format->code),
498 format->width, format->height);
503 static int set_selection(struct media_pad *pad, unsigned int target,
504 struct v4l2_rect *rect)
508 if (rect->left == -1 || rect->top == -1)
511 media_dbg(pad->entity->media,
512 "Setting up selection target %u rectangle (%u,%u)/%ux%u on pad %s/%u\n",
513 target, rect->left, rect->top, rect->width, rect->height,
514 pad->entity->info.name, pad->index);
516 ret = v4l2_subdev_set_selection(pad->entity, rect, pad->index,
517 target, V4L2_SUBDEV_FORMAT_ACTIVE);
519 media_dbg(pad->entity->media,
520 "Unable to set selection rectangle: %s (%d)\n",
521 strerror(-ret), ret);
525 media_dbg(pad->entity->media,
526 "Selection rectangle set: (%u,%u)/%ux%u\n",
527 rect->left, rect->top, rect->width, rect->height);
532 static int set_frame_interval(struct media_entity *entity,
533 struct v4l2_fract *interval)
537 if (interval->numerator == 0)
540 media_dbg(entity->media,
541 "Setting up frame interval %u/%u on entity %s\n",
542 interval->numerator, interval->denominator,
545 ret = v4l2_subdev_set_frame_interval(entity, interval);
547 media_dbg(entity->media,
548 "Unable to set frame interval: %s (%d)",
549 strerror(-ret), ret);
553 media_dbg(entity->media, "Frame interval set: %u/%u\n",
554 interval->numerator, interval->denominator);
560 static int v4l2_subdev_parse_setup_format(struct media_device *media,
561 const char *p, char **endp)
563 struct v4l2_mbus_framefmt format = { 0, 0, 0 };
564 struct media_pad *pad;
565 struct v4l2_rect crop = { -1, -1, -1, -1 };
566 struct v4l2_rect compose = crop;
567 struct v4l2_fract interval = { 0, 0 };
572 pad = v4l2_subdev_parse_pad_format(media, &format, &crop, &compose,
575 media_print_streampos(media, p, end);
576 media_dbg(media, "Unable to parse format\n");
580 if (pad->flags & MEDIA_PAD_FL_SINK) {
581 ret = set_format(pad, &format);
586 ret = set_selection(pad, V4L2_SEL_TGT_CROP, &crop);
590 ret = set_selection(pad, V4L2_SEL_TGT_COMPOSE, &compose);
594 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
595 ret = set_format(pad, &format);
600 ret = set_frame_interval(pad->entity, &interval);
605 /* If the pad is an output pad, automatically set the same format on
606 * the remote subdev input pads, if any.
608 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
609 for (i = 0; i < pad->entity->num_links; ++i) {
610 struct media_link *link = &pad->entity->links[i];
611 struct v4l2_mbus_framefmt remote_format;
613 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
616 if (link->source == pad &&
617 link->sink->entity->info.type == MEDIA_ENT_T_V4L2_SUBDEV) {
618 remote_format = format;
619 set_format(link->sink, &remote_format);
628 int v4l2_subdev_parse_setup_formats(struct media_device *media, const char *p)
634 ret = v4l2_subdev_parse_setup_format(media, p, &end);
639 } while (*end == ',');
641 return *end ? -EINVAL : 0;
646 enum v4l2_mbus_pixelcode code;
648 { "Y8", V4L2_MBUS_FMT_Y8_1X8},
649 { "Y10", V4L2_MBUS_FMT_Y10_1X10 },
650 { "Y12", V4L2_MBUS_FMT_Y12_1X12 },
651 { "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
652 { "YUYV1_5X8", V4L2_MBUS_FMT_YUYV8_1_5X8 },
653 { "YUYV2X8", V4L2_MBUS_FMT_YUYV8_2X8 },
654 { "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
655 { "UYVY1_5X8", V4L2_MBUS_FMT_UYVY8_1_5X8 },
656 { "UYVY2X8", V4L2_MBUS_FMT_UYVY8_2X8 },
657 { "SBGGR8", V4L2_MBUS_FMT_SBGGR8_1X8 },
658 { "SGBRG8", V4L2_MBUS_FMT_SGBRG8_1X8 },
659 { "SGRBG8", V4L2_MBUS_FMT_SGRBG8_1X8 },
660 { "SRGGB8", V4L2_MBUS_FMT_SRGGB8_1X8 },
661 { "SBGGR10", V4L2_MBUS_FMT_SBGGR10_1X10 },
662 { "SGBRG10", V4L2_MBUS_FMT_SGBRG10_1X10 },
663 { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
664 { "SRGGB10", V4L2_MBUS_FMT_SRGGB10_1X10 },
665 { "SBGGR10_DPCM8", V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8 },
666 { "SGBRG10_DPCM8", V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8 },
667 { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
668 { "SRGGB10_DPCM8", V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8 },
669 { "SBGGR12", V4L2_MBUS_FMT_SBGGR12_1X12 },
670 { "SGBRG12", V4L2_MBUS_FMT_SGBRG12_1X12 },
671 { "SGRBG12", V4L2_MBUS_FMT_SGRBG12_1X12 },
672 { "SRGGB12", V4L2_MBUS_FMT_SRGGB12_1X12 },
673 { "AYUV32", V4L2_MBUS_FMT_AYUV8_1X32 },
674 { "ARGB32", V4L2_MBUS_FMT_ARGB8888_1X32 },
677 const char *v4l2_subdev_pixelcode_to_string(enum v4l2_mbus_pixelcode code)
681 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
682 if (mbus_formats[i].code == code)
683 return mbus_formats[i].name;
689 enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string,
694 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
695 if (strncmp(mbus_formats[i].name, string, length) == 0)
699 if (i == ARRAY_SIZE(mbus_formats))
700 return (enum v4l2_mbus_pixelcode)-1;
702 return mbus_formats[i].code;
707 enum v4l2_field field;
709 { "any", V4L2_FIELD_ANY },
710 { "none", V4L2_FIELD_NONE },
711 { "top", V4L2_FIELD_TOP },
712 { "bottom", V4L2_FIELD_BOTTOM },
713 { "interlaced", V4L2_FIELD_INTERLACED },
714 { "seq-tb", V4L2_FIELD_SEQ_TB },
715 { "seq-bt", V4L2_FIELD_SEQ_BT },
716 { "alternate", V4L2_FIELD_ALTERNATE },
717 { "interlaced-tb", V4L2_FIELD_INTERLACED_TB },
718 { "interlaced-bt", V4L2_FIELD_INTERLACED_BT },
721 const char *v4l2_subdev_field_to_string(enum v4l2_field field)
725 for (i = 0; i < ARRAY_SIZE(fields); ++i) {
726 if (fields[i].field == field)
727 return fields[i].name;
733 enum v4l2_field v4l2_subdev_string_to_field(const char *string,
738 for (i = 0; i < ARRAY_SIZE(fields); ++i) {
739 if (strncmp(fields[i].name, string, length) == 0)
743 if (i == ARRAY_SIZE(fields))
744 return (enum v4l2_field)-1;
746 return fields[i].field;