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 media_dbg(entity->media,
44 "%s: Failed to open subdev device node %s\n", __func__,
52 void v4l2_subdev_close(struct media_entity *entity)
58 int v4l2_subdev_get_format(struct media_entity *entity,
59 struct v4l2_mbus_framefmt *format, unsigned int pad,
60 enum v4l2_subdev_format_whence which)
62 struct v4l2_subdev_format fmt;
65 ret = v4l2_subdev_open(entity);
69 memset(&fmt, 0, sizeof(fmt));
73 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FMT, &fmt);
81 int v4l2_subdev_set_format(struct media_entity *entity,
82 struct v4l2_mbus_framefmt *format, unsigned int pad,
83 enum v4l2_subdev_format_whence which)
85 struct v4l2_subdev_format fmt;
88 ret = v4l2_subdev_open(entity);
92 memset(&fmt, 0, sizeof(fmt));
97 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FMT, &fmt);
101 *format = fmt.format;
105 int v4l2_subdev_get_crop(struct media_entity *entity, struct v4l2_rect *rect,
106 unsigned int pad, enum v4l2_subdev_format_whence which)
108 struct v4l2_subdev_crop crop;
111 ret = v4l2_subdev_open(entity);
115 memset(&crop, 0, sizeof(crop));
119 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_CROP, &crop);
127 int v4l2_subdev_set_crop(struct media_entity *entity, struct v4l2_rect *rect,
128 unsigned int pad, enum v4l2_subdev_format_whence which)
130 struct v4l2_subdev_crop crop;
133 ret = v4l2_subdev_open(entity);
137 memset(&crop, 0, sizeof(crop));
142 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CROP, &crop);
150 int v4l2_subdev_get_frame_interval(struct media_entity *entity,
151 struct v4l2_fract *interval)
153 struct v4l2_subdev_frame_interval ival;
156 ret = v4l2_subdev_open(entity);
160 memset(&ival, 0, sizeof(ival));
162 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FRAME_INTERVAL, &ival);
166 *interval = ival.interval;
170 int v4l2_subdev_set_frame_interval(struct media_entity *entity,
171 struct v4l2_fract *interval)
173 struct v4l2_subdev_frame_interval ival;
176 ret = v4l2_subdev_open(entity);
180 memset(&ival, 0, sizeof(ival));
181 ival.interval = *interval;
183 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &ival);
187 *interval = ival.interval;
191 static int v4l2_subdev_parse_format(struct v4l2_mbus_framefmt *format,
192 const char *p, char **endp)
194 enum v4l2_mbus_pixelcode code;
195 unsigned int width, height;
198 for (; isspace(*p); ++p);
199 for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
201 code = v4l2_subdev_string_to_pixelcode(p, end - p);
202 if (code == (enum v4l2_mbus_pixelcode)-1)
205 for (p = end; isspace(*p); ++p);
206 width = strtoul(p, &end, 10);
211 height = strtoul(p, &end, 10);
214 memset(format, 0, sizeof(*format));
215 format->width = width;
216 format->height = height;
222 static int v4l2_subdev_parse_crop(
223 struct v4l2_rect *crop, const char *p, char **endp)
230 crop->left = strtoul(p, &end, 10);
235 crop->top = strtoul(p, &end, 10);
242 crop->width = strtoul(p, &end, 10);
247 crop->height = strtoul(p, &end, 10);
253 static int v4l2_subdev_parse_frame_interval(struct v4l2_fract *interval,
254 const char *p, char **endp)
258 for (; isspace(*p); ++p);
260 interval->numerator = strtoul(p, &end, 10);
262 for (p = end; isspace(*p); ++p);
266 for (; isspace(*p); ++p);
267 interval->denominator = strtoul(p, &end, 10);
273 static struct media_pad *v4l2_subdev_parse_pad_format(
274 struct media_device *media, struct v4l2_mbus_framefmt *format,
275 struct v4l2_rect *crop, struct v4l2_fract *interval, const char *p,
278 struct media_pad *pad;
282 for (; isspace(*p); ++p);
284 pad = media_parse_pad(media, p, &end);
288 for (p = end; isspace(*p); ++p);
292 for (; isspace(*p); ++p);
295 ret = v4l2_subdev_parse_format(format, p, &end);
299 for (p = end; isspace(*p); p++);
303 ret = v4l2_subdev_parse_crop(crop, p, &end);
307 for (p = end; isspace(*p); p++);
311 ret = v4l2_subdev_parse_frame_interval(interval, ++p, &end);
315 for (p = end; isspace(*p); p++);
321 *endp = (char *)p + 1;
325 static int set_format(struct media_pad *pad,
326 struct v4l2_mbus_framefmt *format)
330 if (format->width == 0 || format->height == 0)
333 media_dbg(pad->entity->media,
334 "Setting up format %s %ux%u on pad %s/%u\n",
335 v4l2_subdev_pixelcode_to_string(format->code),
336 format->width, format->height,
337 pad->entity->info.name, pad->index);
339 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
340 V4L2_SUBDEV_FORMAT_ACTIVE);
342 media_dbg(pad->entity->media,
343 "Unable to set format: %s (%d)\n",
344 strerror(-ret), ret);
348 media_dbg(pad->entity->media,
349 "Format set: %s %ux%u\n",
350 v4l2_subdev_pixelcode_to_string(format->code),
351 format->width, format->height);
356 static int set_crop(struct media_pad *pad, struct v4l2_rect *crop)
360 if (crop->left == -1 || crop->top == -1)
363 media_dbg(pad->entity->media,
364 "Setting up crop rectangle (%u,%u)/%ux%u on pad %s/%u\n",
365 crop->left, crop->top, crop->width, crop->height,
366 pad->entity->info.name, pad->index);
368 ret = v4l2_subdev_set_crop(pad->entity, crop, pad->index,
369 V4L2_SUBDEV_FORMAT_ACTIVE);
371 media_dbg(pad->entity->media,
372 "Unable to set crop rectangle: %s (%d)\n",
373 strerror(-ret), ret);
377 media_dbg(pad->entity->media,
378 "Crop rectangle set: (%u,%u)/%ux%u\n",
379 crop->left, crop->top, crop->width, crop->height);
384 static int set_frame_interval(struct media_entity *entity,
385 struct v4l2_fract *interval)
389 if (interval->numerator == 0)
392 media_dbg(entity->media,
393 "Setting up frame interval %u/%u on entity %s\n",
394 interval->numerator, interval->denominator,
397 ret = v4l2_subdev_set_frame_interval(entity, interval);
399 media_dbg(entity->media,
400 "Unable to set frame interval: %s (%d)",
401 strerror(-ret), ret);
405 media_dbg(entity->media, "Frame interval set: %u/%u\n",
406 interval->numerator, interval->denominator);
412 static int v4l2_subdev_parse_setup_format(struct media_device *media,
413 const char *p, char **endp)
415 struct v4l2_mbus_framefmt format = { 0, 0, 0 };
416 struct media_pad *pad;
417 struct v4l2_rect crop = { -1, -1, -1, -1 };
418 struct v4l2_fract interval = { 0, 0 };
423 pad = v4l2_subdev_parse_pad_format(media, &format, &crop, &interval,
426 media_dbg(media, "Unable to parse format\n");
430 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
431 ret = set_crop(pad, &crop);
436 ret = set_format(pad, &format);
440 if (pad->flags & MEDIA_PAD_FL_SINK) {
441 ret = set_crop(pad, &crop);
446 ret = set_frame_interval(pad->entity, &interval);
451 /* If the pad is an output pad, automatically set the same format on
452 * the remote subdev input pads, if any.
454 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
455 for (i = 0; i < pad->entity->num_links; ++i) {
456 struct media_link *link = &pad->entity->links[i];
457 struct v4l2_mbus_framefmt remote_format;
459 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
462 if (link->source == pad &&
463 link->sink->entity->info.type == MEDIA_ENT_T_V4L2_SUBDEV) {
464 remote_format = format;
465 set_format(link->sink, &remote_format);
474 int v4l2_subdev_parse_setup_formats(struct media_device *media, const char *p)
480 ret = v4l2_subdev_parse_setup_format(media, p, &end);
485 } while (*end == ',');
487 return *end ? -EINVAL : 0;
492 enum v4l2_mbus_pixelcode code;
494 { "Y8", V4L2_MBUS_FMT_Y8_1X8},
495 { "Y10", V4L2_MBUS_FMT_Y10_1X10 },
496 { "Y12", V4L2_MBUS_FMT_Y12_1X12 },
497 { "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
498 { "YUYV2X8", V4L2_MBUS_FMT_YUYV8_2X8 },
499 { "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
500 { "UYVY2X8", V4L2_MBUS_FMT_UYVY8_2X8 },
501 { "SBGGR8", V4L2_MBUS_FMT_SBGGR8_1X8 },
502 { "SGBRG8", V4L2_MBUS_FMT_SGBRG8_1X8 },
503 { "SGRBG8", V4L2_MBUS_FMT_SGRBG8_1X8 },
504 { "SRGGB8", V4L2_MBUS_FMT_SRGGB8_1X8 },
505 { "SBGGR10", V4L2_MBUS_FMT_SBGGR10_1X10 },
506 { "SGBRG10", V4L2_MBUS_FMT_SGBRG10_1X10 },
507 { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
508 { "SRGGB10", V4L2_MBUS_FMT_SRGGB10_1X10 },
509 { "SBGGR10_DPCM8", V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8 },
510 { "SGBRG10_DPCM8", V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8 },
511 { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
512 { "SRGGB10_DPCM8", V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8 },
513 { "SBGGR12", V4L2_MBUS_FMT_SBGGR12_1X12 },
514 { "SGBRG12", V4L2_MBUS_FMT_SGBRG12_1X12 },
515 { "SGRBG12", V4L2_MBUS_FMT_SGRBG12_1X12 },
516 { "SRGGB12", V4L2_MBUS_FMT_SRGGB12_1X12 },
519 const char *v4l2_subdev_pixelcode_to_string(enum v4l2_mbus_pixelcode code)
523 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
524 if (mbus_formats[i].code == code)
525 return mbus_formats[i].name;
531 enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string,
536 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
537 if (strncmp(mbus_formats[i].name, string, length) == 0)
541 if (i == ARRAY_SIZE(mbus_formats))
542 return (enum v4l2_mbus_pixelcode)-1;
544 return mbus_formats[i].code;