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 v4l2_mbus_framefmt *format,
232 const char *p, char **endp)
234 enum v4l2_mbus_pixelcode code;
235 unsigned int width, height;
239 * Compatibility with the old syntax: consider space as valid
240 * separator between the media bus pixel code and the size.
242 for (; isspace(*p); ++p);
243 for (end = (char *)p;
244 *end != '/' && *end != ' ' && *end != '\0'; ++end);
246 code = v4l2_subdev_string_to_pixelcode(p, end - p);
247 if (code == (enum v4l2_mbus_pixelcode)-1)
251 width = strtoul(p, &end, 10);
256 height = strtoul(p, &end, 10);
259 memset(format, 0, sizeof(*format));
260 format->width = width;
261 format->height = height;
267 static int v4l2_subdev_parse_rectangle(
268 struct v4l2_rect *r, const char *p, char **endp)
275 r->left = strtoul(p, &end, 10);
280 r->top = strtoul(p, &end, 10);
287 r->width = strtoul(p, &end, 10);
292 r->height = strtoul(p, &end, 10);
298 static int v4l2_subdev_parse_frame_interval(struct v4l2_fract *interval,
299 const char *p, char **endp)
303 for (; isspace(*p); ++p);
305 interval->numerator = strtoul(p, &end, 10);
307 for (p = end; isspace(*p); ++p);
311 for (; isspace(*p); ++p);
312 interval->denominator = strtoul(p, &end, 10);
319 * The debate over whether this function should be named icanhasstr() instead
320 * has been strong and heated. If you feel like this would be an important
321 * change, patches are welcome (or not).
323 static bool strhazit(const char *str, const char **p)
325 int len = strlen(str);
327 if (strncmp(str, *p, len))
330 for (*p += len; isspace(**p); ++*p);
334 static struct media_pad *v4l2_subdev_parse_pad_format(
335 struct media_device *media, struct v4l2_mbus_framefmt *format,
336 struct v4l2_rect *crop, struct v4l2_rect *compose,
337 struct v4l2_fract *interval, const char *p, char **endp)
339 struct media_pad *pad;
344 for (; isspace(*p); ++p);
346 pad = media_parse_pad(media, p, &end);
350 for (p = end; isspace(*p); ++p);
354 for (first = true; ; first = false) {
355 for (; isspace(*p); p++);
358 * Backward compatibility: if the first property starts with an
359 * uppercase later, process it as a format description.
361 if (strhazit("fmt:", &p) || (first && isupper(*p))) {
362 ret = v4l2_subdev_parse_format(format, p, &end);
371 * Backward compatibility: crop rectangles can be specified
372 * implicitly without the 'crop:' property name.
374 if (strhazit("crop:", &p) || *p == '(') {
375 ret = v4l2_subdev_parse_rectangle(crop, p, &end);
383 if (strhazit("compose:", &p)) {
384 ret = v4l2_subdev_parse_rectangle(compose, p, &end);
388 for (p = end; isspace(*p); p++);
393 ret = v4l2_subdev_parse_frame_interval(interval, ++p, &end);
407 *endp = (char *)p + 1;
411 static int set_format(struct media_pad *pad,
412 struct v4l2_mbus_framefmt *format)
416 if (format->width == 0 || format->height == 0)
419 media_dbg(pad->entity->media,
420 "Setting up format %s %ux%u on pad %s/%u\n",
421 v4l2_subdev_pixelcode_to_string(format->code),
422 format->width, format->height,
423 pad->entity->info.name, pad->index);
425 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
426 V4L2_SUBDEV_FORMAT_ACTIVE);
428 media_dbg(pad->entity->media,
429 "Unable to set format: %s (%d)\n",
430 strerror(-ret), ret);
434 media_dbg(pad->entity->media,
435 "Format set: %s %ux%u\n",
436 v4l2_subdev_pixelcode_to_string(format->code),
437 format->width, format->height);
442 static int set_selection(struct media_pad *pad, unsigned int target,
443 struct v4l2_rect *rect)
447 if (rect->left == -1 || rect->top == -1)
450 media_dbg(pad->entity->media,
451 "Setting up selection target %u rectangle (%u,%u)/%ux%u on pad %s/%u\n",
452 target, rect->left, rect->top, rect->width, rect->height,
453 pad->entity->info.name, pad->index);
455 ret = v4l2_subdev_set_selection(pad->entity, rect, pad->index,
456 target, V4L2_SUBDEV_FORMAT_ACTIVE);
458 media_dbg(pad->entity->media,
459 "Unable to set selection rectangle: %s (%d)\n",
460 strerror(-ret), ret);
464 media_dbg(pad->entity->media,
465 "Selection rectangle set: (%u,%u)/%ux%u\n",
466 rect->left, rect->top, rect->width, rect->height);
471 static int set_frame_interval(struct media_entity *entity,
472 struct v4l2_fract *interval)
476 if (interval->numerator == 0)
479 media_dbg(entity->media,
480 "Setting up frame interval %u/%u on entity %s\n",
481 interval->numerator, interval->denominator,
484 ret = v4l2_subdev_set_frame_interval(entity, interval);
486 media_dbg(entity->media,
487 "Unable to set frame interval: %s (%d)",
488 strerror(-ret), ret);
492 media_dbg(entity->media, "Frame interval set: %u/%u\n",
493 interval->numerator, interval->denominator);
499 static int v4l2_subdev_parse_setup_format(struct media_device *media,
500 const char *p, char **endp)
502 struct v4l2_mbus_framefmt format = { 0, 0, 0 };
503 struct media_pad *pad;
504 struct v4l2_rect crop = { -1, -1, -1, -1 };
505 struct v4l2_rect compose = crop;
506 struct v4l2_fract interval = { 0, 0 };
511 pad = v4l2_subdev_parse_pad_format(media, &format, &crop, &compose,
514 media_dbg(media, "Unable to parse format\n");
518 if (pad->flags & MEDIA_PAD_FL_SINK) {
519 ret = set_format(pad, &format);
524 ret = set_selection(pad, V4L2_SEL_TGT_CROP, &crop);
528 ret = set_selection(pad, V4L2_SEL_TGT_COMPOSE, &compose);
532 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
533 ret = set_format(pad, &format);
538 ret = set_frame_interval(pad->entity, &interval);
543 /* If the pad is an output pad, automatically set the same format on
544 * the remote subdev input pads, if any.
546 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
547 for (i = 0; i < pad->entity->num_links; ++i) {
548 struct media_link *link = &pad->entity->links[i];
549 struct v4l2_mbus_framefmt remote_format;
551 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
554 if (link->source == pad &&
555 link->sink->entity->info.type == MEDIA_ENT_T_V4L2_SUBDEV) {
556 remote_format = format;
557 set_format(link->sink, &remote_format);
566 int v4l2_subdev_parse_setup_formats(struct media_device *media, const char *p)
572 ret = v4l2_subdev_parse_setup_format(media, p, &end);
577 } while (*end == ',');
579 return *end ? -EINVAL : 0;
584 enum v4l2_mbus_pixelcode code;
586 { "Y8", V4L2_MBUS_FMT_Y8_1X8},
587 { "Y10", V4L2_MBUS_FMT_Y10_1X10 },
588 { "Y12", V4L2_MBUS_FMT_Y12_1X12 },
589 { "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
590 { "YUYV1_5X8", V4L2_MBUS_FMT_YUYV8_1_5X8 },
591 { "YUYV2X8", V4L2_MBUS_FMT_YUYV8_2X8 },
592 { "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
593 { "UYVY1_5X8", V4L2_MBUS_FMT_UYVY8_1_5X8 },
594 { "UYVY2X8", V4L2_MBUS_FMT_UYVY8_2X8 },
595 { "SBGGR8", V4L2_MBUS_FMT_SBGGR8_1X8 },
596 { "SGBRG8", V4L2_MBUS_FMT_SGBRG8_1X8 },
597 { "SGRBG8", V4L2_MBUS_FMT_SGRBG8_1X8 },
598 { "SRGGB8", V4L2_MBUS_FMT_SRGGB8_1X8 },
599 { "SBGGR10", V4L2_MBUS_FMT_SBGGR10_1X10 },
600 { "SGBRG10", V4L2_MBUS_FMT_SGBRG10_1X10 },
601 { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
602 { "SRGGB10", V4L2_MBUS_FMT_SRGGB10_1X10 },
603 { "SBGGR10_DPCM8", V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8 },
604 { "SGBRG10_DPCM8", V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8 },
605 { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
606 { "SRGGB10_DPCM8", V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8 },
607 { "SBGGR12", V4L2_MBUS_FMT_SBGGR12_1X12 },
608 { "SGBRG12", V4L2_MBUS_FMT_SGBRG12_1X12 },
609 { "SGRBG12", V4L2_MBUS_FMT_SGRBG12_1X12 },
610 { "SRGGB12", V4L2_MBUS_FMT_SRGGB12_1X12 },
613 const char *v4l2_subdev_pixelcode_to_string(enum v4l2_mbus_pixelcode code)
617 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
618 if (mbus_formats[i].code == code)
619 return mbus_formats[i].name;
625 enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string,
630 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
631 if (strncmp(mbus_formats[i].name, string, length) == 0)
635 if (i == ARRAY_SIZE(mbus_formats))
636 return (enum v4l2_mbus_pixelcode)-1;
638 return mbus_formats[i].code;