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>
32 #include <linux/v4l2-subdev.h>
35 #include "v4l2subdev.h"
38 int v4l2_subdev_open(struct media_entity *entity)
43 entity->fd = open(entity->devname, O_RDWR);
44 if (entity->fd == -1) {
45 media_dbg(entity->media,
46 "%s: Failed to open subdev device node %s\n", __func__,
54 void v4l2_subdev_close(struct media_entity *entity)
60 int v4l2_subdev_get_format(struct media_entity *entity,
61 struct v4l2_mbus_framefmt *format, unsigned int pad,
62 enum v4l2_subdev_format_whence which)
64 struct v4l2_subdev_format fmt;
67 ret = v4l2_subdev_open(entity);
71 memset(&fmt, 0, sizeof(fmt));
75 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FMT, &fmt);
83 int v4l2_subdev_set_format(struct media_entity *entity,
84 struct v4l2_mbus_framefmt *format, unsigned int pad,
85 enum v4l2_subdev_format_whence which)
87 struct v4l2_subdev_format fmt;
90 ret = v4l2_subdev_open(entity);
94 memset(&fmt, 0, sizeof(fmt));
99 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FMT, &fmt);
103 *format = fmt.format;
107 int v4l2_subdev_get_selection(struct media_entity *entity,
108 struct v4l2_rect *rect, unsigned int pad, unsigned int target,
109 enum v4l2_subdev_format_whence which)
112 struct v4l2_subdev_selection sel;
113 struct v4l2_subdev_crop crop;
117 ret = v4l2_subdev_open(entity);
121 memset(&u.sel, 0, sizeof(u.sel));
123 u.sel.target = target;
126 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_SELECTION, &u.sel);
131 if (errno != ENOTTY || target != V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL)
134 memset(&u.crop, 0, sizeof(u.crop));
136 u.crop.which = which;
138 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_CROP, &u.crop);
146 int v4l2_subdev_set_selection(struct media_entity *entity,
147 struct v4l2_rect *rect, unsigned int pad, unsigned int target,
148 enum v4l2_subdev_format_whence which)
151 struct v4l2_subdev_selection sel;
152 struct v4l2_subdev_crop crop;
156 ret = v4l2_subdev_open(entity);
160 memset(&u.sel, 0, sizeof(u.sel));
162 u.sel.target = target;
166 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_SELECTION, &u.sel);
171 if (errno != ENOTTY || target != V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL)
174 memset(&u.crop, 0, sizeof(u.crop));
176 u.crop.which = which;
179 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CROP, &u.crop);
187 int v4l2_subdev_get_frame_interval(struct media_entity *entity,
188 struct v4l2_fract *interval)
190 struct v4l2_subdev_frame_interval ival;
193 ret = v4l2_subdev_open(entity);
197 memset(&ival, 0, sizeof(ival));
199 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FRAME_INTERVAL, &ival);
203 *interval = ival.interval;
207 int v4l2_subdev_set_frame_interval(struct media_entity *entity,
208 struct v4l2_fract *interval)
210 struct v4l2_subdev_frame_interval ival;
213 ret = v4l2_subdev_open(entity);
217 memset(&ival, 0, sizeof(ival));
218 ival.interval = *interval;
220 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &ival);
224 *interval = ival.interval;
228 static int v4l2_subdev_parse_format(struct v4l2_mbus_framefmt *format,
229 const char *p, char **endp)
231 enum v4l2_mbus_pixelcode code;
232 unsigned int width, height;
235 for (; isspace(*p); ++p);
236 for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
238 code = v4l2_subdev_string_to_pixelcode(p, end - p);
239 if (code == (enum v4l2_mbus_pixelcode)-1)
242 for (p = end; isspace(*p); ++p);
243 width = strtoul(p, &end, 10);
248 height = strtoul(p, &end, 10);
251 memset(format, 0, sizeof(*format));
252 format->width = width;
253 format->height = height;
259 static int v4l2_subdev_parse_crop(
260 struct v4l2_rect *crop, const char *p, char **endp)
267 crop->left = strtoul(p, &end, 10);
272 crop->top = strtoul(p, &end, 10);
279 crop->width = strtoul(p, &end, 10);
284 crop->height = strtoul(p, &end, 10);
290 static int v4l2_subdev_parse_frame_interval(struct v4l2_fract *interval,
291 const char *p, char **endp)
295 for (; isspace(*p); ++p);
297 interval->numerator = strtoul(p, &end, 10);
299 for (p = end; isspace(*p); ++p);
303 for (; isspace(*p); ++p);
304 interval->denominator = strtoul(p, &end, 10);
310 static struct media_pad *v4l2_subdev_parse_pad_format(
311 struct media_device *media, struct v4l2_mbus_framefmt *format,
312 struct v4l2_rect *crop, struct v4l2_fract *interval, const char *p,
315 struct media_pad *pad;
319 for (; isspace(*p); ++p);
321 pad = media_parse_pad(media, p, &end);
325 for (p = end; isspace(*p); ++p);
329 for (; isspace(*p); ++p);
332 ret = v4l2_subdev_parse_format(format, p, &end);
336 for (p = end; isspace(*p); p++);
340 ret = v4l2_subdev_parse_crop(crop, p, &end);
344 for (p = end; isspace(*p); p++);
348 ret = v4l2_subdev_parse_frame_interval(interval, ++p, &end);
352 for (p = end; isspace(*p); p++);
358 *endp = (char *)p + 1;
362 static int set_format(struct media_pad *pad,
363 struct v4l2_mbus_framefmt *format)
367 if (format->width == 0 || format->height == 0)
370 media_dbg(pad->entity->media,
371 "Setting up format %s %ux%u on pad %s/%u\n",
372 v4l2_subdev_pixelcode_to_string(format->code),
373 format->width, format->height,
374 pad->entity->info.name, pad->index);
376 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
377 V4L2_SUBDEV_FORMAT_ACTIVE);
379 media_dbg(pad->entity->media,
380 "Unable to set format: %s (%d)\n",
381 strerror(-ret), ret);
385 media_dbg(pad->entity->media,
386 "Format set: %s %ux%u\n",
387 v4l2_subdev_pixelcode_to_string(format->code),
388 format->width, format->height);
393 static int set_selection(struct media_pad *pad, unsigned int target,
394 struct v4l2_rect *rect)
398 if (rect->left == -1 || rect->top == -1)
401 media_dbg(pad->entity->media,
402 "Setting up selection target %u rectangle (%u,%u)/%ux%u on pad %s/%u\n",
403 target, rect->left, rect->top, rect->width, rect->height,
404 pad->entity->info.name, pad->index);
406 ret = v4l2_subdev_set_selection(pad->entity, rect, pad->index,
407 target, V4L2_SUBDEV_FORMAT_ACTIVE);
409 media_dbg(pad->entity->media,
410 "Unable to set selection rectangle: %s (%d)\n",
411 strerror(-ret), ret);
415 media_dbg(pad->entity->media,
416 "Selection rectangle set: (%u,%u)/%ux%u\n",
417 rect->left, rect->top, rect->width, rect->height);
422 static int set_frame_interval(struct media_entity *entity,
423 struct v4l2_fract *interval)
427 if (interval->numerator == 0)
430 media_dbg(entity->media,
431 "Setting up frame interval %u/%u on entity %s\n",
432 interval->numerator, interval->denominator,
435 ret = v4l2_subdev_set_frame_interval(entity, interval);
437 media_dbg(entity->media,
438 "Unable to set frame interval: %s (%d)",
439 strerror(-ret), ret);
443 media_dbg(entity->media, "Frame interval set: %u/%u\n",
444 interval->numerator, interval->denominator);
450 static int v4l2_subdev_parse_setup_format(struct media_device *media,
451 const char *p, char **endp)
453 struct v4l2_mbus_framefmt format = { 0, 0, 0 };
454 struct media_pad *pad;
455 struct v4l2_rect crop = { -1, -1, -1, -1 };
456 struct v4l2_fract interval = { 0, 0 };
461 pad = v4l2_subdev_parse_pad_format(media, &format, &crop, &interval,
464 media_dbg(media, "Unable to parse format\n");
468 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
469 ret = set_selection(pad, V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL, &crop);
474 ret = set_format(pad, &format);
478 if (pad->flags & MEDIA_PAD_FL_SINK) {
479 ret = set_selection(pad, V4L2_SUBDEV_SEL_TGT_CROP_ACTUAL, &crop);
484 ret = set_frame_interval(pad->entity, &interval);
489 /* If the pad is an output pad, automatically set the same format on
490 * the remote subdev input pads, if any.
492 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
493 for (i = 0; i < pad->entity->num_links; ++i) {
494 struct media_link *link = &pad->entity->links[i];
495 struct v4l2_mbus_framefmt remote_format;
497 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
500 if (link->source == pad &&
501 link->sink->entity->info.type == MEDIA_ENT_T_V4L2_SUBDEV) {
502 remote_format = format;
503 set_format(link->sink, &remote_format);
512 int v4l2_subdev_parse_setup_formats(struct media_device *media, const char *p)
518 ret = v4l2_subdev_parse_setup_format(media, p, &end);
523 } while (*end == ',');
525 return *end ? -EINVAL : 0;
530 enum v4l2_mbus_pixelcode code;
532 { "Y8", V4L2_MBUS_FMT_Y8_1X8},
533 { "Y10", V4L2_MBUS_FMT_Y10_1X10 },
534 { "Y12", V4L2_MBUS_FMT_Y12_1X12 },
535 { "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
536 { "YUYV1_5X8", V4L2_MBUS_FMT_YUYV8_1_5X8 },
537 { "YUYV2X8", V4L2_MBUS_FMT_YUYV8_2X8 },
538 { "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
539 { "UYVY1_5X8", V4L2_MBUS_FMT_UYVY8_1_5X8 },
540 { "UYVY2X8", V4L2_MBUS_FMT_UYVY8_2X8 },
541 { "SBGGR8", V4L2_MBUS_FMT_SBGGR8_1X8 },
542 { "SGBRG8", V4L2_MBUS_FMT_SGBRG8_1X8 },
543 { "SGRBG8", V4L2_MBUS_FMT_SGRBG8_1X8 },
544 { "SRGGB8", V4L2_MBUS_FMT_SRGGB8_1X8 },
545 { "SBGGR10", V4L2_MBUS_FMT_SBGGR10_1X10 },
546 { "SGBRG10", V4L2_MBUS_FMT_SGBRG10_1X10 },
547 { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
548 { "SRGGB10", V4L2_MBUS_FMT_SRGGB10_1X10 },
549 { "SBGGR10_DPCM8", V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8 },
550 { "SGBRG10_DPCM8", V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8 },
551 { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
552 { "SRGGB10_DPCM8", V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8 },
553 { "SBGGR12", V4L2_MBUS_FMT_SBGGR12_1X12 },
554 { "SGBRG12", V4L2_MBUS_FMT_SGBRG12_1X12 },
555 { "SGRBG12", V4L2_MBUS_FMT_SGRBG12_1X12 },
556 { "SRGGB12", V4L2_MBUS_FMT_SRGGB12_1X12 },
559 const char *v4l2_subdev_pixelcode_to_string(enum v4l2_mbus_pixelcode code)
563 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
564 if (mbus_formats[i].code == code)
565 return mbus_formats[i].name;
571 enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string,
576 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
577 if (strncmp(mbus_formats[i].name, string, length) == 0)
581 if (i == ARRAY_SIZE(mbus_formats))
582 return (enum v4l2_mbus_pixelcode)-1;
584 return mbus_formats[i].code;