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>
33 #include <linux/v4l2-subdev.h>
36 #include "v4l2subdev.h"
39 int v4l2_subdev_open(struct media_entity *entity)
44 entity->fd = open(entity->devname, O_RDWR);
45 if (entity->fd == -1) {
46 media_dbg(entity->media,
47 "%s: Failed to open subdev device node %s\n", __func__,
55 void v4l2_subdev_close(struct media_entity *entity)
61 int v4l2_subdev_get_format(struct media_entity *entity,
62 struct v4l2_mbus_framefmt *format, unsigned int pad,
63 enum v4l2_subdev_format_whence which)
65 struct v4l2_subdev_format fmt;
68 ret = v4l2_subdev_open(entity);
72 memset(&fmt, 0, sizeof(fmt));
76 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FMT, &fmt);
84 int v4l2_subdev_set_format(struct media_entity *entity,
85 struct v4l2_mbus_framefmt *format, unsigned int pad,
86 enum v4l2_subdev_format_whence which)
88 struct v4l2_subdev_format fmt;
91 ret = v4l2_subdev_open(entity);
95 memset(&fmt, 0, sizeof(fmt));
100 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FMT, &fmt);
104 *format = fmt.format;
108 int v4l2_subdev_get_selection(struct media_entity *entity,
109 struct v4l2_rect *rect, unsigned int pad, unsigned int target,
110 enum v4l2_subdev_format_whence which)
113 struct v4l2_subdev_selection sel;
114 struct v4l2_subdev_crop crop;
118 ret = v4l2_subdev_open(entity);
122 memset(&u.sel, 0, sizeof(u.sel));
124 u.sel.target = target;
127 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_SELECTION, &u.sel);
132 if (errno != ENOTTY || target != V4L2_SEL_TGT_CROP)
135 memset(&u.crop, 0, sizeof(u.crop));
137 u.crop.which = which;
139 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_CROP, &u.crop);
147 int v4l2_subdev_set_selection(struct media_entity *entity,
148 struct v4l2_rect *rect, unsigned int pad, unsigned int target,
149 enum v4l2_subdev_format_whence which)
152 struct v4l2_subdev_selection sel;
153 struct v4l2_subdev_crop crop;
157 ret = v4l2_subdev_open(entity);
161 memset(&u.sel, 0, sizeof(u.sel));
163 u.sel.target = target;
167 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_SELECTION, &u.sel);
172 if (errno != ENOTTY || target != V4L2_SEL_TGT_CROP)
175 memset(&u.crop, 0, sizeof(u.crop));
177 u.crop.which = which;
180 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CROP, &u.crop);
188 int v4l2_subdev_get_frame_interval(struct media_entity *entity,
189 struct v4l2_fract *interval)
191 struct v4l2_subdev_frame_interval ival;
194 ret = v4l2_subdev_open(entity);
198 memset(&ival, 0, sizeof(ival));
200 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FRAME_INTERVAL, &ival);
204 *interval = ival.interval;
208 int v4l2_subdev_set_frame_interval(struct media_entity *entity,
209 struct v4l2_fract *interval)
211 struct v4l2_subdev_frame_interval ival;
214 ret = v4l2_subdev_open(entity);
218 memset(&ival, 0, sizeof(ival));
219 ival.interval = *interval;
221 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &ival);
225 *interval = ival.interval;
229 static int v4l2_subdev_parse_format(struct v4l2_mbus_framefmt *format,
230 const char *p, char **endp)
232 enum v4l2_mbus_pixelcode code;
233 unsigned int width, height;
237 * Compatibility with the old syntax: consider space as valid
238 * separator between the media bus pixel code and the size.
240 for (; isspace(*p); ++p);
241 for (end = (char *)p;
242 *end != '/' && *end != ' ' && *end != '\0'; ++end);
244 code = v4l2_subdev_string_to_pixelcode(p, end - p);
245 if (code == (enum v4l2_mbus_pixelcode)-1)
249 width = strtoul(p, &end, 10);
254 height = strtoul(p, &end, 10);
257 memset(format, 0, sizeof(*format));
258 format->width = width;
259 format->height = height;
265 static int v4l2_subdev_parse_rectangle(
266 struct v4l2_rect *r, const char *p, char **endp)
273 r->left = strtoul(p, &end, 10);
278 r->top = strtoul(p, &end, 10);
285 r->width = strtoul(p, &end, 10);
290 r->height = strtoul(p, &end, 10);
296 static int v4l2_subdev_parse_frame_interval(struct v4l2_fract *interval,
297 const char *p, char **endp)
301 for (; isspace(*p); ++p);
303 interval->numerator = strtoul(p, &end, 10);
305 for (p = end; isspace(*p); ++p);
309 for (; isspace(*p); ++p);
310 interval->denominator = strtoul(p, &end, 10);
317 * The debate over whether this function should be named icanhasstr() instead
318 * has been strong and heated. If you feel like this would be an important
319 * change, patches are welcome (or not).
321 static bool strhazit(const char *str, const char **p)
323 int len = strlen(str);
325 if (strncmp(str, *p, len))
328 for (*p += len; isspace(**p); ++*p);
332 static struct media_pad *v4l2_subdev_parse_pad_format(
333 struct media_device *media, struct v4l2_mbus_framefmt *format,
334 struct v4l2_rect *crop, struct v4l2_rect *compose,
335 struct v4l2_fract *interval, const char *p, char **endp)
337 struct media_pad *pad;
342 for (; isspace(*p); ++p);
344 pad = media_parse_pad(media, p, &end);
348 for (p = end; isspace(*p); ++p);
352 for (first = true; ; first = false) {
353 for (; isspace(*p); p++);
356 * Backward compatibility: if the first property starts with an
357 * uppercase later, process it as a format description.
359 if (strhazit("fmt:", &p) || (first && isupper(*p))) {
360 ret = v4l2_subdev_parse_format(format, p, &end);
369 * Backward compatibility: crop rectangles can be specified
370 * implicitly without the 'crop:' property name.
372 if (strhazit("crop:", &p) || *p == '(') {
373 ret = v4l2_subdev_parse_rectangle(crop, p, &end);
381 if (strhazit("compose:", &p)) {
382 ret = v4l2_subdev_parse_rectangle(compose, p, &end);
386 for (p = end; isspace(*p); p++);
391 ret = v4l2_subdev_parse_frame_interval(interval, ++p, &end);
405 *endp = (char *)p + 1;
409 static int set_format(struct media_pad *pad,
410 struct v4l2_mbus_framefmt *format)
414 if (format->width == 0 || format->height == 0)
417 media_dbg(pad->entity->media,
418 "Setting up format %s %ux%u on pad %s/%u\n",
419 v4l2_subdev_pixelcode_to_string(format->code),
420 format->width, format->height,
421 pad->entity->info.name, pad->index);
423 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
424 V4L2_SUBDEV_FORMAT_ACTIVE);
426 media_dbg(pad->entity->media,
427 "Unable to set format: %s (%d)\n",
428 strerror(-ret), ret);
432 media_dbg(pad->entity->media,
433 "Format set: %s %ux%u\n",
434 v4l2_subdev_pixelcode_to_string(format->code),
435 format->width, format->height);
440 static int set_selection(struct media_pad *pad, unsigned int target,
441 struct v4l2_rect *rect)
445 if (rect->left == -1 || rect->top == -1)
448 media_dbg(pad->entity->media,
449 "Setting up selection target %u rectangle (%u,%u)/%ux%u on pad %s/%u\n",
450 target, rect->left, rect->top, rect->width, rect->height,
451 pad->entity->info.name, pad->index);
453 ret = v4l2_subdev_set_selection(pad->entity, rect, pad->index,
454 target, V4L2_SUBDEV_FORMAT_ACTIVE);
456 media_dbg(pad->entity->media,
457 "Unable to set selection rectangle: %s (%d)\n",
458 strerror(-ret), ret);
462 media_dbg(pad->entity->media,
463 "Selection rectangle set: (%u,%u)/%ux%u\n",
464 rect->left, rect->top, rect->width, rect->height);
469 static int set_frame_interval(struct media_entity *entity,
470 struct v4l2_fract *interval)
474 if (interval->numerator == 0)
477 media_dbg(entity->media,
478 "Setting up frame interval %u/%u on entity %s\n",
479 interval->numerator, interval->denominator,
482 ret = v4l2_subdev_set_frame_interval(entity, interval);
484 media_dbg(entity->media,
485 "Unable to set frame interval: %s (%d)",
486 strerror(-ret), ret);
490 media_dbg(entity->media, "Frame interval set: %u/%u\n",
491 interval->numerator, interval->denominator);
497 static int v4l2_subdev_parse_setup_format(struct media_device *media,
498 const char *p, char **endp)
500 struct v4l2_mbus_framefmt format = { 0, 0, 0 };
501 struct media_pad *pad;
502 struct v4l2_rect crop = { -1, -1, -1, -1 };
503 struct v4l2_rect compose = crop;
504 struct v4l2_fract interval = { 0, 0 };
509 pad = v4l2_subdev_parse_pad_format(media, &format, &crop, &compose,
512 media_dbg(media, "Unable to parse format\n");
516 if (pad->flags & MEDIA_PAD_FL_SINK) {
517 ret = set_format(pad, &format);
522 ret = set_selection(pad, V4L2_SEL_TGT_CROP, &crop);
526 ret = set_selection(pad, V4L2_SEL_TGT_COMPOSE, &compose);
530 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
531 ret = set_format(pad, &format);
536 ret = set_frame_interval(pad->entity, &interval);
541 /* If the pad is an output pad, automatically set the same format on
542 * the remote subdev input pads, if any.
544 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
545 for (i = 0; i < pad->entity->num_links; ++i) {
546 struct media_link *link = &pad->entity->links[i];
547 struct v4l2_mbus_framefmt remote_format;
549 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
552 if (link->source == pad &&
553 link->sink->entity->info.type == MEDIA_ENT_T_V4L2_SUBDEV) {
554 remote_format = format;
555 set_format(link->sink, &remote_format);
564 int v4l2_subdev_parse_setup_formats(struct media_device *media, const char *p)
570 ret = v4l2_subdev_parse_setup_format(media, p, &end);
575 } while (*end == ',');
577 return *end ? -EINVAL : 0;
582 enum v4l2_mbus_pixelcode code;
584 { "Y8", V4L2_MBUS_FMT_Y8_1X8},
585 { "Y10", V4L2_MBUS_FMT_Y10_1X10 },
586 { "Y12", V4L2_MBUS_FMT_Y12_1X12 },
587 { "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
588 { "YUYV1_5X8", V4L2_MBUS_FMT_YUYV8_1_5X8 },
589 { "YUYV2X8", V4L2_MBUS_FMT_YUYV8_2X8 },
590 { "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
591 { "UYVY1_5X8", V4L2_MBUS_FMT_UYVY8_1_5X8 },
592 { "UYVY2X8", V4L2_MBUS_FMT_UYVY8_2X8 },
593 { "SBGGR8", V4L2_MBUS_FMT_SBGGR8_1X8 },
594 { "SGBRG8", V4L2_MBUS_FMT_SGBRG8_1X8 },
595 { "SGRBG8", V4L2_MBUS_FMT_SGRBG8_1X8 },
596 { "SRGGB8", V4L2_MBUS_FMT_SRGGB8_1X8 },
597 { "SBGGR10", V4L2_MBUS_FMT_SBGGR10_1X10 },
598 { "SGBRG10", V4L2_MBUS_FMT_SGBRG10_1X10 },
599 { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
600 { "SRGGB10", V4L2_MBUS_FMT_SRGGB10_1X10 },
601 { "SBGGR10_DPCM8", V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8 },
602 { "SGBRG10_DPCM8", V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8 },
603 { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
604 { "SRGGB10_DPCM8", V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8 },
605 { "SBGGR12", V4L2_MBUS_FMT_SBGGR12_1X12 },
606 { "SGBRG12", V4L2_MBUS_FMT_SGBRG12_1X12 },
607 { "SGRBG12", V4L2_MBUS_FMT_SGRBG12_1X12 },
608 { "SRGGB12", V4L2_MBUS_FMT_SRGGB12_1X12 },
611 const char *v4l2_subdev_pixelcode_to_string(enum v4l2_mbus_pixelcode code)
615 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
616 if (mbus_formats[i].code == code)
617 return mbus_formats[i].name;
623 enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string,
628 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
629 if (strncmp(mbus_formats[i].name, string, length) == 0)
633 if (i == ARRAY_SIZE(mbus_formats))
634 return (enum v4l2_mbus_pixelcode)-1;
636 return mbus_formats[i].code;