2 * Media controller test application
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>
38 enum v4l2_mbus_pixelcode code;
40 { "Y8", V4L2_MBUS_FMT_Y8_1X8},
41 { "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
42 { "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
43 { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
44 { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
47 const char *pixelcode_to_string(enum v4l2_mbus_pixelcode code)
51 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
52 if (mbus_formats[i].code == code)
53 return mbus_formats[i].name;
59 enum v4l2_mbus_pixelcode string_to_pixelcode(const char *string,
64 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
65 if (strncmp(mbus_formats[i].name, string, length) == 0)
69 if (i == ARRAY_SIZE(mbus_formats))
70 return (enum v4l2_mbus_pixelcode)-1;
72 return mbus_formats[i].code;
75 static int v4l2_subdev_open(struct media_entity *entity)
80 entity->fd = open(entity->devname, O_RDWR);
81 if (entity->fd == -1) {
82 printf("%s: Failed to open subdev device node %s\n", __func__,
90 int v4l2_subdev_get_format(struct media_entity *entity,
91 struct v4l2_mbus_framefmt *format, unsigned int pad,
92 enum v4l2_subdev_format_whence which)
94 struct v4l2_subdev_format fmt;
97 ret = v4l2_subdev_open(entity);
101 memset(&fmt, 0, sizeof(fmt));
105 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FMT, &fmt);
109 *format = fmt.format;
113 int v4l2_subdev_set_format(struct media_entity *entity,
114 struct v4l2_mbus_framefmt *format, unsigned int pad,
115 enum v4l2_subdev_format_whence which)
117 struct v4l2_subdev_format fmt;
120 ret = v4l2_subdev_open(entity);
124 memset(&fmt, 0, sizeof(fmt));
127 fmt.format = *format;
129 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FMT, &fmt);
133 *format = fmt.format;
137 int v4l2_subdev_get_crop(struct media_entity *entity, struct v4l2_rect *rect,
138 unsigned int pad, enum v4l2_subdev_format_whence which)
140 struct v4l2_subdev_crop crop;
143 ret = v4l2_subdev_open(entity);
147 memset(&crop, 0, sizeof(crop));
151 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_CROP, &crop);
159 int v4l2_subdev_set_crop(struct media_entity *entity, struct v4l2_rect *rect,
160 unsigned int pad, enum v4l2_subdev_format_whence which)
162 struct v4l2_subdev_crop crop;
165 ret = v4l2_subdev_open(entity);
169 memset(&crop, 0, sizeof(crop));
174 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CROP, &crop);
182 int v4l2_subdev_set_frame_interval(struct media_entity *entity,
183 struct v4l2_fract *interval)
185 struct v4l2_subdev_frame_interval ival;
188 ret = v4l2_subdev_open(entity);
192 memset(&ival, 0, sizeof(ival));
193 ival.interval = *interval;
195 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &ival);
199 *interval = ival.interval;
203 void v4l2_subdev_print_format(struct media_entity *entity,
204 unsigned int pad, enum v4l2_subdev_format_whence which)
206 struct v4l2_mbus_framefmt format;
207 struct v4l2_rect rect;
210 ret = v4l2_subdev_get_format(entity, &format, pad, which);
214 printf("[%s %ux%u", pixelcode_to_string(format.code),
215 format.width, format.height);
217 ret = v4l2_subdev_get_crop(entity, &rect, pad, which);
219 printf(" (%u,%u)/%ux%u", rect.left, rect.top,
220 rect.width, rect.height);