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);
405 * Backward compatibility: crop rectangles can be specified
406 * implicitly without the 'crop:' property name.
408 if (strhazit("crop:", &p) || *p == '(') {
409 ret = v4l2_subdev_parse_rectangle(media, crop, p, &end);
419 if (strhazit("compose:", &p)) {
420 ret = v4l2_subdev_parse_rectangle(media, compose, p, &end);
426 for (p = end; isspace(*p); p++);
431 ret = v4l2_subdev_parse_frame_interval(media, interval, ++p, &end);
445 media_dbg(media, "Expected ']'\n");
450 *endp = (char *)p + 1;
454 static int set_format(struct media_pad *pad,
455 struct v4l2_mbus_framefmt *format)
459 if (format->width == 0 || format->height == 0)
462 media_dbg(pad->entity->media,
463 "Setting up format %s %ux%u on pad %s/%u\n",
464 v4l2_subdev_pixelcode_to_string(format->code),
465 format->width, format->height,
466 pad->entity->info.name, pad->index);
468 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
469 V4L2_SUBDEV_FORMAT_ACTIVE);
471 media_dbg(pad->entity->media,
472 "Unable to set format: %s (%d)\n",
473 strerror(-ret), ret);
477 media_dbg(pad->entity->media,
478 "Format set: %s %ux%u\n",
479 v4l2_subdev_pixelcode_to_string(format->code),
480 format->width, format->height);
485 static int set_selection(struct media_pad *pad, unsigned int target,
486 struct v4l2_rect *rect)
490 if (rect->left == -1 || rect->top == -1)
493 media_dbg(pad->entity->media,
494 "Setting up selection target %u rectangle (%u,%u)/%ux%u on pad %s/%u\n",
495 target, rect->left, rect->top, rect->width, rect->height,
496 pad->entity->info.name, pad->index);
498 ret = v4l2_subdev_set_selection(pad->entity, rect, pad->index,
499 target, V4L2_SUBDEV_FORMAT_ACTIVE);
501 media_dbg(pad->entity->media,
502 "Unable to set selection rectangle: %s (%d)\n",
503 strerror(-ret), ret);
507 media_dbg(pad->entity->media,
508 "Selection rectangle set: (%u,%u)/%ux%u\n",
509 rect->left, rect->top, rect->width, rect->height);
514 static int set_frame_interval(struct media_entity *entity,
515 struct v4l2_fract *interval)
519 if (interval->numerator == 0)
522 media_dbg(entity->media,
523 "Setting up frame interval %u/%u on entity %s\n",
524 interval->numerator, interval->denominator,
527 ret = v4l2_subdev_set_frame_interval(entity, interval);
529 media_dbg(entity->media,
530 "Unable to set frame interval: %s (%d)",
531 strerror(-ret), ret);
535 media_dbg(entity->media, "Frame interval set: %u/%u\n",
536 interval->numerator, interval->denominator);
542 static int v4l2_subdev_parse_setup_format(struct media_device *media,
543 const char *p, char **endp)
545 struct v4l2_mbus_framefmt format = { 0, 0, 0 };
546 struct media_pad *pad;
547 struct v4l2_rect crop = { -1, -1, -1, -1 };
548 struct v4l2_rect compose = crop;
549 struct v4l2_fract interval = { 0, 0 };
554 pad = v4l2_subdev_parse_pad_format(media, &format, &crop, &compose,
557 media_print_streampos(media, p, end);
558 media_dbg(media, "Unable to parse format\n");
562 if (pad->flags & MEDIA_PAD_FL_SINK) {
563 ret = set_format(pad, &format);
568 ret = set_selection(pad, V4L2_SEL_TGT_CROP, &crop);
572 ret = set_selection(pad, V4L2_SEL_TGT_COMPOSE, &compose);
576 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
577 ret = set_format(pad, &format);
582 ret = set_frame_interval(pad->entity, &interval);
587 /* If the pad is an output pad, automatically set the same format on
588 * the remote subdev input pads, if any.
590 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
591 for (i = 0; i < pad->entity->num_links; ++i) {
592 struct media_link *link = &pad->entity->links[i];
593 struct v4l2_mbus_framefmt remote_format;
595 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
598 if (link->source == pad &&
599 link->sink->entity->info.type == MEDIA_ENT_T_V4L2_SUBDEV) {
600 remote_format = format;
601 set_format(link->sink, &remote_format);
610 int v4l2_subdev_parse_setup_formats(struct media_device *media, const char *p)
616 ret = v4l2_subdev_parse_setup_format(media, p, &end);
621 } while (*end == ',');
623 return *end ? -EINVAL : 0;
628 enum v4l2_mbus_pixelcode code;
630 { "Y8", V4L2_MBUS_FMT_Y8_1X8},
631 { "Y10", V4L2_MBUS_FMT_Y10_1X10 },
632 { "Y12", V4L2_MBUS_FMT_Y12_1X12 },
633 { "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
634 { "YUYV1_5X8", V4L2_MBUS_FMT_YUYV8_1_5X8 },
635 { "YUYV2X8", V4L2_MBUS_FMT_YUYV8_2X8 },
636 { "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
637 { "UYVY1_5X8", V4L2_MBUS_FMT_UYVY8_1_5X8 },
638 { "UYVY2X8", V4L2_MBUS_FMT_UYVY8_2X8 },
639 { "SBGGR8", V4L2_MBUS_FMT_SBGGR8_1X8 },
640 { "SGBRG8", V4L2_MBUS_FMT_SGBRG8_1X8 },
641 { "SGRBG8", V4L2_MBUS_FMT_SGRBG8_1X8 },
642 { "SRGGB8", V4L2_MBUS_FMT_SRGGB8_1X8 },
643 { "SBGGR10", V4L2_MBUS_FMT_SBGGR10_1X10 },
644 { "SGBRG10", V4L2_MBUS_FMT_SGBRG10_1X10 },
645 { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
646 { "SRGGB10", V4L2_MBUS_FMT_SRGGB10_1X10 },
647 { "SBGGR10_DPCM8", V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8 },
648 { "SGBRG10_DPCM8", V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8 },
649 { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
650 { "SRGGB10_DPCM8", V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8 },
651 { "SBGGR12", V4L2_MBUS_FMT_SBGGR12_1X12 },
652 { "SGBRG12", V4L2_MBUS_FMT_SGBRG12_1X12 },
653 { "SGRBG12", V4L2_MBUS_FMT_SGRBG12_1X12 },
654 { "SRGGB12", V4L2_MBUS_FMT_SRGGB12_1X12 },
657 const char *v4l2_subdev_pixelcode_to_string(enum v4l2_mbus_pixelcode code)
661 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
662 if (mbus_formats[i].code == code)
663 return mbus_formats[i].name;
669 enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string,
674 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
675 if (strncmp(mbus_formats[i].name, string, length) == 0)
679 if (i == ARRAY_SIZE(mbus_formats))
680 return (enum v4l2_mbus_pixelcode)-1;
682 return mbus_formats[i].code;