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>
39 #include "v4l2subdev.h"
41 int v4l2_subdev_open(struct media_entity *entity)
46 entity->fd = open(entity->devname, O_RDWR);
47 if (entity->fd == -1) {
48 media_dbg(entity->media,
49 "%s: Failed to open subdev device node %s\n", __func__,
57 void v4l2_subdev_close(struct media_entity *entity)
63 int v4l2_subdev_get_format(struct media_entity *entity,
64 struct v4l2_mbus_framefmt *format, unsigned int pad,
65 enum v4l2_subdev_format_whence which)
67 struct v4l2_subdev_format fmt;
70 ret = v4l2_subdev_open(entity);
74 memset(&fmt, 0, sizeof(fmt));
78 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FMT, &fmt);
86 int v4l2_subdev_set_format(struct media_entity *entity,
87 struct v4l2_mbus_framefmt *format, unsigned int pad,
88 enum v4l2_subdev_format_whence which)
90 struct v4l2_subdev_format fmt;
93 ret = v4l2_subdev_open(entity);
97 memset(&fmt, 0, sizeof(fmt));
100 fmt.format = *format;
102 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FMT, &fmt);
106 *format = fmt.format;
110 int v4l2_subdev_get_selection(struct media_entity *entity,
111 struct v4l2_rect *rect, unsigned int pad, unsigned int target,
112 enum v4l2_subdev_format_whence which)
115 struct v4l2_subdev_selection sel;
116 struct v4l2_subdev_crop crop;
120 ret = v4l2_subdev_open(entity);
124 memset(&u.sel, 0, sizeof(u.sel));
126 u.sel.target = target;
129 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_SELECTION, &u.sel);
134 if (errno != ENOTTY || target != V4L2_SEL_TGT_CROP)
137 memset(&u.crop, 0, sizeof(u.crop));
139 u.crop.which = which;
141 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_CROP, &u.crop);
149 int v4l2_subdev_set_selection(struct media_entity *entity,
150 struct v4l2_rect *rect, unsigned int pad, unsigned int target,
151 enum v4l2_subdev_format_whence which)
154 struct v4l2_subdev_selection sel;
155 struct v4l2_subdev_crop crop;
159 ret = v4l2_subdev_open(entity);
163 memset(&u.sel, 0, sizeof(u.sel));
165 u.sel.target = target;
169 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_SELECTION, &u.sel);
174 if (errno != ENOTTY || target != V4L2_SEL_TGT_CROP)
177 memset(&u.crop, 0, sizeof(u.crop));
179 u.crop.which = which;
182 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CROP, &u.crop);
190 int v4l2_subdev_get_frame_interval(struct media_entity *entity,
191 struct v4l2_fract *interval)
193 struct v4l2_subdev_frame_interval ival;
196 ret = v4l2_subdev_open(entity);
200 memset(&ival, 0, sizeof(ival));
202 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FRAME_INTERVAL, &ival);
206 *interval = ival.interval;
210 int v4l2_subdev_set_frame_interval(struct media_entity *entity,
211 struct v4l2_fract *interval)
213 struct v4l2_subdev_frame_interval ival;
216 ret = v4l2_subdev_open(entity);
220 memset(&ival, 0, sizeof(ival));
221 ival.interval = *interval;
223 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &ival);
227 *interval = ival.interval;
231 static int v4l2_subdev_parse_format(struct media_device *media,
232 struct v4l2_mbus_framefmt *format,
233 const char *p, char **endp)
235 enum v4l2_mbus_pixelcode code;
236 unsigned int width, height;
240 * Compatibility with the old syntax: consider space as valid
241 * separator between the media bus pixel code and the size.
243 for (; isspace(*p); ++p);
244 for (end = (char *)p;
245 *end != '/' && *end != ' ' && *end != '\0'; ++end);
247 code = v4l2_subdev_string_to_pixelcode(p, end - p);
248 if (code == (enum v4l2_mbus_pixelcode)-1) {
249 media_dbg(media, "Invalid pixel code '%.*s'\n", end - p, p);
254 width = strtoul(p, &end, 10);
256 media_dbg(media, "Expected 'x'\n");
261 height = strtoul(p, &end, 10);
264 memset(format, 0, sizeof(*format));
265 format->width = width;
266 format->height = height;
272 static int v4l2_subdev_parse_rectangle(struct media_device *media,
273 struct v4l2_rect *r, const char *p,
279 media_dbg(media, "Expected '('\n");
280 *endp = (char *)p - 1;
284 r->left = strtoul(p, &end, 10);
286 media_dbg(media, "Expected ','\n");
292 r->top = strtoul(p, &end, 10);
294 media_dbg(media, "Expected ')'\n");
299 media_dbg(media, "Expected '/'\n");
305 r->width = strtoul(p, &end, 10);
307 media_dbg(media, "Expected 'x'\n");
313 r->height = strtoul(p, &end, 10);
319 static int v4l2_subdev_parse_frame_interval(struct media_device *media,
320 struct v4l2_fract *interval,
321 const char *p, char **endp)
325 for (; isspace(*p); ++p);
327 interval->numerator = strtoul(p, &end, 10);
329 for (p = end; isspace(*p); ++p);
331 media_dbg(media, "Expected '/'\n");
332 *endp = (char *)p - 1;
336 for (; isspace(*p); ++p);
337 interval->denominator = strtoul(p, &end, 10);
344 * The debate over whether this function should be named icanhasstr() instead
345 * has been strong and heated. If you feel like this would be an important
346 * change, patches are welcome (or not).
348 static bool strhazit(const char *str, const char **p)
350 int len = strlen(str);
352 if (strncmp(str, *p, len))
355 for (*p += len; isspace(**p); ++*p);
359 static struct media_pad *v4l2_subdev_parse_pad_format(
360 struct media_device *media, struct v4l2_mbus_framefmt *format,
361 struct v4l2_rect *crop, struct v4l2_rect *compose,
362 struct v4l2_fract *interval, const char *p, char **endp)
364 struct media_pad *pad;
369 for (; isspace(*p); ++p);
371 pad = media_parse_pad(media, p, &end);
377 for (p = end; isspace(*p); ++p);
379 media_dbg(media, "Expected '['\n");
380 *endp = (char *)p - 1;
384 for (first = true; ; first = false) {
385 for (; isspace(*p); p++);
388 * Backward compatibility: if the first property starts with an
389 * uppercase later, process it as a format description.
391 if (strhazit("fmt:", &p) || (first && isupper(*p))) {
392 ret = v4l2_subdev_parse_format(media, format, p, &end);
403 * Backward compatibility: crop rectangles can be specified
404 * implicitly without the 'crop:' property name.
406 if (strhazit("crop:", &p) || *p == '(') {
407 ret = v4l2_subdev_parse_rectangle(media, crop, p, &end);
417 if (strhazit("compose:", &p)) {
418 ret = v4l2_subdev_parse_rectangle(media, compose, p, &end);
424 for (p = end; isspace(*p); p++);
429 ret = v4l2_subdev_parse_frame_interval(media, interval, ++p, &end);
443 media_dbg(media, "Expected ']'\n");
448 *endp = (char *)p + 1;
452 static int set_format(struct media_pad *pad,
453 struct v4l2_mbus_framefmt *format)
457 if (format->width == 0 || format->height == 0)
460 media_dbg(pad->entity->media,
461 "Setting up format %s %ux%u on pad %s/%u\n",
462 v4l2_subdev_pixelcode_to_string(format->code),
463 format->width, format->height,
464 pad->entity->info.name, pad->index);
466 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
467 V4L2_SUBDEV_FORMAT_ACTIVE);
469 media_dbg(pad->entity->media,
470 "Unable to set format: %s (%d)\n",
471 strerror(-ret), ret);
475 media_dbg(pad->entity->media,
476 "Format set: %s %ux%u\n",
477 v4l2_subdev_pixelcode_to_string(format->code),
478 format->width, format->height);
483 static int set_selection(struct media_pad *pad, unsigned int target,
484 struct v4l2_rect *rect)
488 if (rect->left == -1 || rect->top == -1)
491 media_dbg(pad->entity->media,
492 "Setting up selection target %u rectangle (%u,%u)/%ux%u on pad %s/%u\n",
493 target, rect->left, rect->top, rect->width, rect->height,
494 pad->entity->info.name, pad->index);
496 ret = v4l2_subdev_set_selection(pad->entity, rect, pad->index,
497 target, V4L2_SUBDEV_FORMAT_ACTIVE);
499 media_dbg(pad->entity->media,
500 "Unable to set selection rectangle: %s (%d)\n",
501 strerror(-ret), ret);
505 media_dbg(pad->entity->media,
506 "Selection rectangle set: (%u,%u)/%ux%u\n",
507 rect->left, rect->top, rect->width, rect->height);
512 static int set_frame_interval(struct media_entity *entity,
513 struct v4l2_fract *interval)
517 if (interval->numerator == 0)
520 media_dbg(entity->media,
521 "Setting up frame interval %u/%u on entity %s\n",
522 interval->numerator, interval->denominator,
525 ret = v4l2_subdev_set_frame_interval(entity, interval);
527 media_dbg(entity->media,
528 "Unable to set frame interval: %s (%d)",
529 strerror(-ret), ret);
533 media_dbg(entity->media, "Frame interval set: %u/%u\n",
534 interval->numerator, interval->denominator);
540 static int v4l2_subdev_parse_setup_format(struct media_device *media,
541 const char *p, char **endp)
543 struct v4l2_mbus_framefmt format = { 0, 0, 0 };
544 struct media_pad *pad;
545 struct v4l2_rect crop = { -1, -1, -1, -1 };
546 struct v4l2_rect compose = crop;
547 struct v4l2_fract interval = { 0, 0 };
552 pad = v4l2_subdev_parse_pad_format(media, &format, &crop, &compose,
555 media_print_streampos(media, p, end);
556 media_dbg(media, "Unable to parse format\n");
560 if (pad->flags & MEDIA_PAD_FL_SINK) {
561 ret = set_format(pad, &format);
566 ret = set_selection(pad, V4L2_SEL_TGT_CROP, &crop);
570 ret = set_selection(pad, V4L2_SEL_TGT_COMPOSE, &compose);
574 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
575 ret = set_format(pad, &format);
580 ret = set_frame_interval(pad->entity, &interval);
585 /* If the pad is an output pad, automatically set the same format on
586 * the remote subdev input pads, if any.
588 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
589 for (i = 0; i < pad->entity->num_links; ++i) {
590 struct media_link *link = &pad->entity->links[i];
591 struct v4l2_mbus_framefmt remote_format;
593 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
596 if (link->source == pad &&
597 link->sink->entity->info.type == MEDIA_ENT_T_V4L2_SUBDEV) {
598 remote_format = format;
599 set_format(link->sink, &remote_format);
608 int v4l2_subdev_parse_setup_formats(struct media_device *media, const char *p)
614 ret = v4l2_subdev_parse_setup_format(media, p, &end);
619 } while (*end == ',');
621 return *end ? -EINVAL : 0;
626 enum v4l2_mbus_pixelcode code;
628 { "Y8", V4L2_MBUS_FMT_Y8_1X8},
629 { "Y10", V4L2_MBUS_FMT_Y10_1X10 },
630 { "Y12", V4L2_MBUS_FMT_Y12_1X12 },
631 { "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
632 { "YUYV1_5X8", V4L2_MBUS_FMT_YUYV8_1_5X8 },
633 { "YUYV2X8", V4L2_MBUS_FMT_YUYV8_2X8 },
634 { "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
635 { "UYVY1_5X8", V4L2_MBUS_FMT_UYVY8_1_5X8 },
636 { "UYVY2X8", V4L2_MBUS_FMT_UYVY8_2X8 },
637 { "SBGGR8", V4L2_MBUS_FMT_SBGGR8_1X8 },
638 { "SGBRG8", V4L2_MBUS_FMT_SGBRG8_1X8 },
639 { "SGRBG8", V4L2_MBUS_FMT_SGRBG8_1X8 },
640 { "SRGGB8", V4L2_MBUS_FMT_SRGGB8_1X8 },
641 { "SBGGR10", V4L2_MBUS_FMT_SBGGR10_1X10 },
642 { "SGBRG10", V4L2_MBUS_FMT_SGBRG10_1X10 },
643 { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
644 { "SRGGB10", V4L2_MBUS_FMT_SRGGB10_1X10 },
645 { "SBGGR10_DPCM8", V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8 },
646 { "SGBRG10_DPCM8", V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8 },
647 { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
648 { "SRGGB10_DPCM8", V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8 },
649 { "SBGGR12", V4L2_MBUS_FMT_SBGGR12_1X12 },
650 { "SGBRG12", V4L2_MBUS_FMT_SGBRG12_1X12 },
651 { "SGRBG12", V4L2_MBUS_FMT_SGRBG12_1X12 },
652 { "SRGGB12", V4L2_MBUS_FMT_SRGGB12_1X12 },
655 const char *v4l2_subdev_pixelcode_to_string(enum v4l2_mbus_pixelcode code)
659 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
660 if (mbus_formats[i].code == code)
661 return mbus_formats[i].name;
667 enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string,
672 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
673 if (strncmp(mbus_formats[i].name, string, length) == 0)
677 if (i == ARRAY_SIZE(mbus_formats))
678 return (enum v4l2_mbus_pixelcode)-1;
680 return mbus_formats[i].code;