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_crop(struct media_entity *entity, struct v4l2_rect *rect,
108 unsigned int pad, enum v4l2_subdev_format_whence which)
110 struct v4l2_subdev_crop crop;
113 ret = v4l2_subdev_open(entity);
117 memset(&crop, 0, sizeof(crop));
121 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_CROP, &crop);
129 int v4l2_subdev_set_crop(struct media_entity *entity, struct v4l2_rect *rect,
130 unsigned int pad, enum v4l2_subdev_format_whence which)
132 struct v4l2_subdev_crop crop;
135 ret = v4l2_subdev_open(entity);
139 memset(&crop, 0, sizeof(crop));
144 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CROP, &crop);
152 int v4l2_subdev_get_frame_interval(struct media_entity *entity,
153 struct v4l2_fract *interval)
155 struct v4l2_subdev_frame_interval ival;
158 ret = v4l2_subdev_open(entity);
162 memset(&ival, 0, sizeof(ival));
164 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FRAME_INTERVAL, &ival);
168 *interval = ival.interval;
172 int v4l2_subdev_set_frame_interval(struct media_entity *entity,
173 struct v4l2_fract *interval)
175 struct v4l2_subdev_frame_interval ival;
178 ret = v4l2_subdev_open(entity);
182 memset(&ival, 0, sizeof(ival));
183 ival.interval = *interval;
185 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &ival);
189 *interval = ival.interval;
193 static int v4l2_subdev_parse_format(struct v4l2_mbus_framefmt *format,
194 const char *p, char **endp)
196 enum v4l2_mbus_pixelcode code;
197 unsigned int width, height;
200 for (; isspace(*p); ++p);
201 for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
203 code = v4l2_subdev_string_to_pixelcode(p, end - p);
204 if (code == (enum v4l2_mbus_pixelcode)-1)
207 for (p = end; isspace(*p); ++p);
208 width = strtoul(p, &end, 10);
213 height = strtoul(p, &end, 10);
216 memset(format, 0, sizeof(*format));
217 format->width = width;
218 format->height = height;
224 static int v4l2_subdev_parse_crop(
225 struct v4l2_rect *crop, const char *p, char **endp)
232 crop->left = strtoul(p, &end, 10);
237 crop->top = strtoul(p, &end, 10);
244 crop->width = strtoul(p, &end, 10);
249 crop->height = strtoul(p, &end, 10);
255 static int v4l2_subdev_parse_frame_interval(struct v4l2_fract *interval,
256 const char *p, char **endp)
260 for (; isspace(*p); ++p);
262 interval->numerator = strtoul(p, &end, 10);
264 for (p = end; isspace(*p); ++p);
268 for (; isspace(*p); ++p);
269 interval->denominator = strtoul(p, &end, 10);
275 static struct media_pad *v4l2_subdev_parse_pad_format(
276 struct media_device *media, struct v4l2_mbus_framefmt *format,
277 struct v4l2_rect *crop, struct v4l2_fract *interval, const char *p,
280 struct media_pad *pad;
284 for (; isspace(*p); ++p);
286 pad = media_parse_pad(media, p, &end);
290 for (p = end; isspace(*p); ++p);
294 for (; isspace(*p); ++p);
297 ret = v4l2_subdev_parse_format(format, p, &end);
301 for (p = end; isspace(*p); p++);
305 ret = v4l2_subdev_parse_crop(crop, p, &end);
309 for (p = end; isspace(*p); p++);
313 ret = v4l2_subdev_parse_frame_interval(interval, ++p, &end);
317 for (p = end; isspace(*p); p++);
323 *endp = (char *)p + 1;
327 static int set_format(struct media_pad *pad,
328 struct v4l2_mbus_framefmt *format)
332 if (format->width == 0 || format->height == 0)
335 media_dbg(pad->entity->media,
336 "Setting up format %s %ux%u on pad %s/%u\n",
337 v4l2_subdev_pixelcode_to_string(format->code),
338 format->width, format->height,
339 pad->entity->info.name, pad->index);
341 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
342 V4L2_SUBDEV_FORMAT_ACTIVE);
344 media_dbg(pad->entity->media,
345 "Unable to set format: %s (%d)\n",
346 strerror(-ret), ret);
350 media_dbg(pad->entity->media,
351 "Format set: %s %ux%u\n",
352 v4l2_subdev_pixelcode_to_string(format->code),
353 format->width, format->height);
358 static int set_crop(struct media_pad *pad, struct v4l2_rect *crop)
362 if (crop->left == -1 || crop->top == -1)
365 media_dbg(pad->entity->media,
366 "Setting up crop rectangle (%u,%u)/%ux%u on pad %s/%u\n",
367 crop->left, crop->top, crop->width, crop->height,
368 pad->entity->info.name, pad->index);
370 ret = v4l2_subdev_set_crop(pad->entity, crop, pad->index,
371 V4L2_SUBDEV_FORMAT_ACTIVE);
373 media_dbg(pad->entity->media,
374 "Unable to set crop rectangle: %s (%d)\n",
375 strerror(-ret), ret);
379 media_dbg(pad->entity->media,
380 "Crop rectangle set: (%u,%u)/%ux%u\n",
381 crop->left, crop->top, crop->width, crop->height);
386 static int set_frame_interval(struct media_entity *entity,
387 struct v4l2_fract *interval)
391 if (interval->numerator == 0)
394 media_dbg(entity->media,
395 "Setting up frame interval %u/%u on entity %s\n",
396 interval->numerator, interval->denominator,
399 ret = v4l2_subdev_set_frame_interval(entity, interval);
401 media_dbg(entity->media,
402 "Unable to set frame interval: %s (%d)",
403 strerror(-ret), ret);
407 media_dbg(entity->media, "Frame interval set: %u/%u\n",
408 interval->numerator, interval->denominator);
414 static int v4l2_subdev_parse_setup_format(struct media_device *media,
415 const char *p, char **endp)
417 struct v4l2_mbus_framefmt format = { 0, 0, 0 };
418 struct media_pad *pad;
419 struct v4l2_rect crop = { -1, -1, -1, -1 };
420 struct v4l2_fract interval = { 0, 0 };
425 pad = v4l2_subdev_parse_pad_format(media, &format, &crop, &interval,
428 media_dbg(media, "Unable to parse format\n");
432 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
433 ret = set_crop(pad, &crop);
438 ret = set_format(pad, &format);
442 if (pad->flags & MEDIA_PAD_FL_SINK) {
443 ret = set_crop(pad, &crop);
448 ret = set_frame_interval(pad->entity, &interval);
453 /* If the pad is an output pad, automatically set the same format on
454 * the remote subdev input pads, if any.
456 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
457 for (i = 0; i < pad->entity->num_links; ++i) {
458 struct media_link *link = &pad->entity->links[i];
459 struct v4l2_mbus_framefmt remote_format;
461 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
464 if (link->source == pad &&
465 link->sink->entity->info.type == MEDIA_ENT_T_V4L2_SUBDEV) {
466 remote_format = format;
467 set_format(link->sink, &remote_format);
476 int v4l2_subdev_parse_setup_formats(struct media_device *media, const char *p)
482 ret = v4l2_subdev_parse_setup_format(media, p, &end);
487 } while (*end == ',');
489 return *end ? -EINVAL : 0;
494 enum v4l2_mbus_pixelcode code;
496 { "Y8", V4L2_MBUS_FMT_Y8_1X8},
497 { "Y10", V4L2_MBUS_FMT_Y10_1X10 },
498 { "Y12", V4L2_MBUS_FMT_Y12_1X12 },
499 { "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
500 { "YUYV1_5X8", V4L2_MBUS_FMT_YUYV8_1_5X8 },
501 { "YUYV2X8", V4L2_MBUS_FMT_YUYV8_2X8 },
502 { "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
503 { "UYVY1_5X8", V4L2_MBUS_FMT_UYVY8_1_5X8 },
504 { "UYVY2X8", V4L2_MBUS_FMT_UYVY8_2X8 },
505 { "SBGGR8", V4L2_MBUS_FMT_SBGGR8_1X8 },
506 { "SGBRG8", V4L2_MBUS_FMT_SGBRG8_1X8 },
507 { "SGRBG8", V4L2_MBUS_FMT_SGRBG8_1X8 },
508 { "SRGGB8", V4L2_MBUS_FMT_SRGGB8_1X8 },
509 { "SBGGR10", V4L2_MBUS_FMT_SBGGR10_1X10 },
510 { "SGBRG10", V4L2_MBUS_FMT_SGBRG10_1X10 },
511 { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
512 { "SRGGB10", V4L2_MBUS_FMT_SRGGB10_1X10 },
513 { "SBGGR10_DPCM8", V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8 },
514 { "SGBRG10_DPCM8", V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8 },
515 { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
516 { "SRGGB10_DPCM8", V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8 },
517 { "SBGGR12", V4L2_MBUS_FMT_SBGGR12_1X12 },
518 { "SGBRG12", V4L2_MBUS_FMT_SGBRG12_1X12 },
519 { "SGRBG12", V4L2_MBUS_FMT_SGRBG12_1X12 },
520 { "SRGGB12", V4L2_MBUS_FMT_SRGGB12_1X12 },
523 const char *v4l2_subdev_pixelcode_to_string(enum v4l2_mbus_pixelcode code)
527 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
528 if (mbus_formats[i].code == code)
529 return mbus_formats[i].name;
535 enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string,
540 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
541 if (strncmp(mbus_formats[i].name, string, length) == 0)
545 if (i == ARRAY_SIZE(mbus_formats))
546 return (enum v4l2_mbus_pixelcode)-1;
548 return mbus_formats[i].code;