2 * V4L2 subdev interface library
4 * Copyright (C) 2010 Ideas on board SPRL <laurent.pinchart@ideasonboard.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
20 #include <sys/ioctl.h>
22 #include <sys/types.h>
30 #include <linux/v4l2-subdev.h>
33 #include "v4l2subdev.h"
36 int v4l2_subdev_open(struct media_entity *entity)
41 entity->fd = open(entity->devname, O_RDWR);
42 if (entity->fd == -1) {
43 printf("%s: Failed to open subdev device node %s\n", __func__,
51 void v4l2_subdev_close(struct media_entity *entity)
57 int v4l2_subdev_get_format(struct media_entity *entity,
58 struct v4l2_mbus_framefmt *format, unsigned int pad,
59 enum v4l2_subdev_format_whence which)
61 struct v4l2_subdev_format fmt;
64 ret = v4l2_subdev_open(entity);
68 memset(&fmt, 0, sizeof(fmt));
72 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FMT, &fmt);
80 int v4l2_subdev_set_format(struct media_entity *entity,
81 struct v4l2_mbus_framefmt *format, unsigned int pad,
82 enum v4l2_subdev_format_whence which)
84 struct v4l2_subdev_format fmt;
87 ret = v4l2_subdev_open(entity);
91 memset(&fmt, 0, sizeof(fmt));
96 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FMT, &fmt);
100 *format = fmt.format;
104 int v4l2_subdev_get_crop(struct media_entity *entity, struct v4l2_rect *rect,
105 unsigned int pad, enum v4l2_subdev_format_whence which)
107 struct v4l2_subdev_crop crop;
110 ret = v4l2_subdev_open(entity);
114 memset(&crop, 0, sizeof(crop));
118 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_CROP, &crop);
126 int v4l2_subdev_set_crop(struct media_entity *entity, struct v4l2_rect *rect,
127 unsigned int pad, enum v4l2_subdev_format_whence which)
129 struct v4l2_subdev_crop crop;
132 ret = v4l2_subdev_open(entity);
136 memset(&crop, 0, sizeof(crop));
141 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CROP, &crop);
149 int v4l2_subdev_get_frame_interval(struct media_entity *entity,
150 struct v4l2_fract *interval)
152 struct v4l2_subdev_frame_interval ival;
155 ret = v4l2_subdev_open(entity);
159 memset(&ival, 0, sizeof(ival));
161 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FRAME_INTERVAL, &ival);
165 *interval = ival.interval;
169 int v4l2_subdev_set_frame_interval(struct media_entity *entity,
170 struct v4l2_fract *interval)
172 struct v4l2_subdev_frame_interval ival;
175 ret = v4l2_subdev_open(entity);
179 memset(&ival, 0, sizeof(ival));
180 ival.interval = *interval;
182 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &ival);
186 *interval = ival.interval;
190 static int v4l2_subdev_parse_format(struct v4l2_mbus_framefmt *format,
191 const char *p, char **endp)
193 enum v4l2_mbus_pixelcode code;
194 unsigned int width, height;
197 for (; isspace(*p); ++p);
198 for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
200 code = v4l2_subdev_string_to_pixelcode(p, end - p);
201 if (code == (enum v4l2_mbus_pixelcode)-1)
204 for (p = end; isspace(*p); ++p);
205 width = strtoul(p, &end, 10);
210 height = strtoul(p, &end, 10);
213 memset(format, 0, sizeof(*format));
214 format->width = width;
215 format->height = height;
221 static int v4l2_subdev_parse_crop(
222 struct v4l2_rect *crop, const char *p, char **endp)
229 crop->left = strtoul(p, &end, 10);
234 crop->top = strtoul(p, &end, 10);
241 crop->width = strtoul(p, &end, 10);
246 crop->height = strtoul(p, &end, 10);
252 static int v4l2_subdev_parse_frame_interval(struct v4l2_fract *interval,
253 const char *p, char **endp)
257 for (; isspace(*p); ++p);
259 interval->numerator = strtoul(p, &end, 10);
261 for (p = end; isspace(*p); ++p);
265 for (; isspace(*p); ++p);
266 interval->denominator = strtoul(p, &end, 10);
272 static struct media_pad *v4l2_subdev_parse_pad_format(
273 struct media_device *media, struct v4l2_mbus_framefmt *format,
274 struct v4l2_rect *crop, struct v4l2_fract *interval, const char *p,
277 struct media_pad *pad;
281 for (; isspace(*p); ++p);
283 pad = media_parse_pad(media, p, &end);
287 for (p = end; isspace(*p); ++p);
291 for (; isspace(*p); ++p);
294 ret = v4l2_subdev_parse_format(format, p, &end);
298 for (p = end; isspace(*p); p++);
302 ret = v4l2_subdev_parse_crop(crop, p, &end);
306 for (p = end; isspace(*p); p++);
310 ret = v4l2_subdev_parse_frame_interval(interval, ++p, &end);
314 for (p = end; isspace(*p); p++);
320 *endp = (char *)p + 1;
324 static int set_format(struct media_pad *pad,
325 struct v4l2_mbus_framefmt *format)
329 if (format->width == 0 || format->height == 0)
332 printf("Setting up format %s %ux%u on pad %s/%u\n",
333 v4l2_subdev_pixelcode_to_string(format->code),
334 format->width, format->height,
335 pad->entity->info.name, pad->index);
337 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
338 V4L2_SUBDEV_FORMAT_ACTIVE);
340 printf("Unable to set format: %s (%d)\n", strerror(-ret), ret);
344 printf("Format set: %s %ux%u\n",
345 v4l2_subdev_pixelcode_to_string(format->code),
346 format->width, format->height);
351 static int set_crop(struct media_pad *pad, struct v4l2_rect *crop)
355 if (crop->left == -1 || crop->top == -1)
358 printf("Setting up crop rectangle (%u,%u)/%ux%u on pad %s/%u\n",
359 crop->left, crop->top, crop->width, crop->height,
360 pad->entity->info.name, pad->index);
362 ret = v4l2_subdev_set_crop(pad->entity, crop, pad->index,
363 V4L2_SUBDEV_FORMAT_ACTIVE);
365 printf("Unable to set crop rectangle: %s (%d)\n", strerror(-ret), ret);
369 printf("Crop rectangle set: (%u,%u)/%ux%u\n",
370 crop->left, crop->top, crop->width, crop->height);
375 static int set_frame_interval(struct media_entity *entity,
376 struct v4l2_fract *interval)
380 if (interval->numerator == 0)
383 printf("Setting up frame interval %u/%u on entity %s\n",
384 interval->numerator, interval->denominator, entity->info.name);
386 ret = v4l2_subdev_set_frame_interval(entity, interval);
388 printf("Unable to set frame interval: %s (%d)", strerror(-ret), ret);
392 printf("Frame interval set: %u/%u\n",
393 interval->numerator, interval->denominator);
399 static int v4l2_subdev_parse_setup_format(struct media_device *media,
400 const char *p, char **endp)
402 struct v4l2_mbus_framefmt format = { 0, 0, 0 };
403 struct media_pad *pad;
404 struct v4l2_rect crop = { -1, -1, -1, -1 };
405 struct v4l2_fract interval = { 0, 0 };
410 pad = v4l2_subdev_parse_pad_format(media, &format, &crop, &interval,
413 printf("Unable to parse format\n");
417 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
418 ret = set_crop(pad, &crop);
423 ret = set_format(pad, &format);
427 if (pad->flags & MEDIA_PAD_FL_SINK) {
428 ret = set_crop(pad, &crop);
433 ret = set_frame_interval(pad->entity, &interval);
438 /* If the pad is an output pad, automatically set the same format on
439 * the remote subdev input pads, if any.
441 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
442 for (i = 0; i < pad->entity->num_links; ++i) {
443 struct media_link *link = &pad->entity->links[i];
444 struct v4l2_mbus_framefmt remote_format;
446 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
449 if (link->source == pad &&
450 link->sink->entity->info.type == MEDIA_ENT_T_V4L2_SUBDEV) {
451 remote_format = format;
452 set_format(link->sink, &remote_format);
461 int v4l2_subdev_parse_setup_formats(struct media_device *media, const char *p)
467 ret = v4l2_subdev_parse_setup_format(media, p, &end);
472 } while (*end == ',');
474 return *end ? -EINVAL : 0;
479 enum v4l2_mbus_pixelcode code;
481 { "Y8", V4L2_MBUS_FMT_Y8_1X8},
482 { "Y10", V4L2_MBUS_FMT_Y10_1X10 },
483 { "Y12", V4L2_MBUS_FMT_Y12_1X12 },
484 { "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
485 { "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
486 { "SBGGR8", V4L2_MBUS_FMT_SBGGR8_1X8 },
487 { "SGBRG8", V4L2_MBUS_FMT_SGBRG8_1X8 },
488 { "SGRBG8", V4L2_MBUS_FMT_SGRBG8_1X8 },
489 { "SRGGB8", V4L2_MBUS_FMT_SRGGB8_1X8 },
490 { "SBGGR10", V4L2_MBUS_FMT_SBGGR10_1X10 },
491 { "SGBRG10", V4L2_MBUS_FMT_SGBRG10_1X10 },
492 { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
493 { "SRGGB10", V4L2_MBUS_FMT_SRGGB10_1X10 },
494 { "SBGGR10_DPCM8", V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8 },
495 { "SGBRG10_DPCM8", V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8 },
496 { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
497 { "SRGGB10_DPCM8", V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8 },
498 { "SBGGR12", V4L2_MBUS_FMT_SBGGR12_1X12 },
499 { "SGBRG12", V4L2_MBUS_FMT_SGBRG12_1X12 },
500 { "SGRBG12", V4L2_MBUS_FMT_SGRBG12_1X12 },
501 { "SRGGB12", V4L2_MBUS_FMT_SRGGB12_1X12 },
504 const char *v4l2_subdev_pixelcode_to_string(enum v4l2_mbus_pixelcode code)
508 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
509 if (mbus_formats[i].code == code)
510 return mbus_formats[i].name;
516 enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string,
521 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
522 if (strncmp(mbus_formats[i].name, string, length) == 0)
526 if (i == ARRAY_SIZE(mbus_formats))
527 return (enum v4l2_mbus_pixelcode)-1;
529 return mbus_formats[i].code;