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.,
21 #include <sys/ioctl.h>
33 #include <linux/types.h>
34 #include <linux/media.h>
35 #include <linux/v4l2-mediabus.h>
36 #include <linux/v4l2-subdev.h>
37 #include <linux/videodev2.h>
44 /* -----------------------------------------------------------------------------
48 static struct media_entity_pad *parse_pad(struct media_device *media, const char *p, char **endp)
50 unsigned int entity_id, pad;
51 struct media_entity *entity;
54 for (; isspace(*p); ++p);
57 for (end = (char *)p + 1; *end && *end != '"'; ++end);
61 entity = media_get_entity_by_name(media, p + 1, end - p - 1);
65 for (++end; isspace(*end); ++end);
67 entity_id = strtoul(p, &end, 10) - 1;
68 if (entity_id >= media->entities_count)
71 entity = &media->entities[entity_id];
76 for (p = end + 1; isspace(*p); ++p);
78 pad = strtoul(p, &end, 10);
79 for (p = end; isspace(*p); ++p);
81 if (pad >= entity->info.pads)
84 for (p = end; isspace(*p); ++p);
87 return &entity->pads[pad];
90 static struct media_entity_link *parse_link(struct media_device *media, const char *p, char **endp)
92 struct media_entity_link *link;
93 struct media_entity_pad *source;
94 struct media_entity_pad *sink;
98 source = parse_pad(media, p, &end);
102 if (end[0] != '-' || end[1] != '>')
106 sink = parse_pad(media, p, &end);
112 for (i = 0; i < source->entity->info.links; i++) {
113 link = &source->entity->links[i];
115 if (link->source == source && link->sink == sink)
122 static int setup_link(struct media_device *media, const char *p, char **endp)
124 struct media_entity_link *link;
128 link = parse_link(media, p, &end);
130 printf("Unable to parse link\n");
136 printf("Unable to parse link flags\n");
140 flags = strtoul(p, &end, 10);
141 for (p = end; isspace(*p); p++);
143 printf("Unable to parse link flags\n");
147 for (; isspace(*p); p++);
150 printf("Setting up link %u:%u -> %u:%u [%u]\n",
151 link->source->entity->info.id, link->source->index,
152 link->sink->entity->info.id, link->sink->index,
155 return media_setup_link(media, link->source, link->sink, flags);
158 static int setup_links(struct media_device *media, const char *p)
164 ret = setup_link(media, p, &end);
169 } while (*end == ',');
171 return *end ? -EINVAL : 0;
174 /* -----------------------------------------------------------------------------
180 enum v4l2_mbus_pixelcode code;
182 { "YUYV", V4L2_MBUS_FMT_YUYV16_1X16 },
183 { "UYVY", V4L2_MBUS_FMT_UYVY16_1X16 },
184 { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
185 { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
188 static const char *pixelcode_to_string(enum v4l2_mbus_pixelcode code)
192 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
193 if (mbus_formats[i].code == code)
194 return mbus_formats[i].name;
200 static int parse_format(struct v4l2_mbus_framefmt *format, const char *p, char **endp)
202 unsigned int width, height;
206 for (; isspace(*p); ++p);
207 for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
209 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
210 if (strncmp(mbus_formats[i].name, p, end - p) == 0)
214 if (i == ARRAY_SIZE(mbus_formats))
217 for (p = end; isspace(*p); ++p);
218 width = strtoul(p, &end, 10);
223 height = strtoul(p, &end, 10);
226 memset(format, 0, sizeof(*format));
227 format->width = width;
228 format->height = height;
229 format->code = mbus_formats[i].code;
234 static struct media_entity_pad *parse_pad_format(struct media_device *media,
235 struct v4l2_mbus_framefmt *format, const char *p, char **endp)
237 struct media_entity_pad *pad;
241 for (; isspace(*p); ++p);
243 pad = parse_pad(media, p, &end);
247 for (p = end; isspace(*p); ++p);
251 ret = parse_format(format, p, &end);
255 for (p = end; isspace(*p); p++);
263 static int set_format(struct media_entity_pad *pad, struct v4l2_mbus_framefmt *format)
267 printf("Setting up format %s %ux%u on pad %s/%u\n",
268 pixelcode_to_string(format->code), format->width, format->height,
269 pad->entity->info.name, pad->index);
271 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
272 V4L2_SUBDEV_FORMAT_ACTIVE);
274 printf("Unable to set format: %s(%u)", strerror(ret), ret);
278 printf("Format set: %s %ux%u\n",
279 pixelcode_to_string(format->code), format->width, format->height);
284 static int setup_format(struct media_device *media, const char *p, char **endp)
286 struct v4l2_mbus_framefmt format;
287 struct media_entity_pad *pad;
292 pad = parse_pad_format(media, &format, p, &end);
294 printf("Unable to parse format\n");
298 ret = set_format(pad, &format);
302 /* If the pad is an output pad, automatically set the same format on
303 * the remote inputs, if any.
305 if (pad->type == MEDIA_PAD_TYPE_OUTPUT) {
306 for (i = 0; i < pad->entity->info.links; ++i) {
307 struct media_entity_link *link = &pad->entity->links[i];
308 struct v4l2_mbus_framefmt remote_format;
310 if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
313 if (link->source == pad) {
314 remote_format = format;
315 set_format(link->sink, &remote_format);
324 static int setup_formats(struct media_device *media, const char *p)
330 ret = setup_format(media, p, &end);
332 printf("Unable to parse format\n");
337 } while (*end == ',');
339 return *end ? -EINVAL : 0;
342 int main(int argc, char **argv)
344 struct media_device *media;
346 if (parse_cmdline(argc, argv))
349 /* Open the media device and enumerate entities, pads and links. */
350 media = media_open(media_opts.devname, media_opts.verbose);
354 if (media_opts.entity) {
355 struct media_entity *entity;
357 entity = media_get_entity_by_name(media, media_opts.entity,
358 strlen(media_opts.entity));
360 printf("%s\n", entity->devname);
362 printf("Entity '%s' not found\n", media_opts.entity);
365 if (media_opts.print) {
366 media_print_topology(media);
370 if (media_opts.reset) {
371 printf("Resetting all links to inactive\n");
372 media_reset_links(media);
375 if (media_opts.links)
376 setup_links(media, media_opts.links);
378 if (media_opts.formats)
379 setup_formats(media, media_opts.formats);
381 if (media_opts.interactive) {
386 printf("Enter a link to modify or enter to stop\n");
387 if (fgets(buffer, sizeof buffer, stdin) == NULL)
390 if (buffer[0] == '\n')
393 setup_link(media, buffer, &end);