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>
43 /* -----------------------------------------------------------------------------
47 static struct media_entity_pad *parse_pad(struct media_device *media, const char *p, char **endp)
49 unsigned int entity_id, pad;
50 struct media_entity *entity;
53 for (; isspace(*p); ++p);
56 for (end = (char *)p + 1; *end && *end != '"'; ++end);
60 entity = media_get_entity_by_name(media, p + 1, end - p - 1);
64 for (++end; isspace(*end); ++end);
66 entity_id = strtoul(p, &end, 10) - 1;
67 if (entity_id >= media->entities_count)
70 entity = &media->entities[entity_id];
75 for (p = end + 1; isspace(*p); ++p);
77 pad = strtoul(p, &end, 10);
78 for (p = end; isspace(*p); ++p);
80 if (pad >= entity->info.pads)
83 for (p = end; isspace(*p); ++p);
86 return &entity->pads[pad];
89 static struct media_entity_link *parse_link(struct media_device *media, const char *p, char **endp)
91 struct media_entity_link *link;
92 struct media_entity_pad *source;
93 struct media_entity_pad *sink;
97 source = parse_pad(media, p, &end);
101 if (end[0] != '-' || end[1] != '>')
105 sink = parse_pad(media, p, &end);
111 for (i = 0; i < source->entity->info.links; i++) {
112 link = &source->entity->links[i];
114 if (link->source == source && link->sink == sink)
121 static int setup_link(struct media_device *media, const char *p, char **endp)
123 struct media_entity_link *link;
127 link = parse_link(media, p, &end);
129 printf("Unable to parse link\n");
135 printf("Unable to parse link flags\n");
139 flags = strtoul(p, &end, 10);
140 for (p = end; isspace(*p); p++);
142 printf("Unable to parse link flags\n");
146 for (; isspace(*p); p++);
149 printf("Setting up link %u:%u -> %u:%u [%u]\n",
150 link->source->entity->info.id, link->source->index,
151 link->sink->entity->info.id, link->sink->index,
154 return media_setup_link(media, link->source, link->sink, flags);
157 static int setup_links(struct media_device *media, const char *p)
163 ret = setup_link(media, p, &end);
168 } while (*end == ',');
170 return *end ? -EINVAL : 0;
173 /* -----------------------------------------------------------------------------
177 static int parse_format(struct v4l2_mbus_framefmt *format, const char *p, char **endp)
179 enum v4l2_mbus_pixelcode code;
180 unsigned int width, height;
183 for (; isspace(*p); ++p);
184 for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
186 code = string_to_pixelcode(p, end - p);
187 if (code == (enum v4l2_mbus_pixelcode)-1)
190 for (p = end; isspace(*p); ++p);
191 width = strtoul(p, &end, 10);
196 height = strtoul(p, &end, 10);
199 memset(format, 0, sizeof(*format));
200 format->width = width;
201 format->height = height;
207 static struct media_entity_pad *parse_pad_format(struct media_device *media,
208 struct v4l2_mbus_framefmt *format, const char *p, char **endp)
210 struct media_entity_pad *pad;
214 for (; isspace(*p); ++p);
216 pad = parse_pad(media, p, &end);
220 for (p = end; isspace(*p); ++p);
224 ret = parse_format(format, p, &end);
228 for (p = end; isspace(*p); p++);
236 static int set_format(struct media_entity_pad *pad, struct v4l2_mbus_framefmt *format)
240 printf("Setting up format %s %ux%u on pad %s/%u\n",
241 pixelcode_to_string(format->code), format->width, format->height,
242 pad->entity->info.name, pad->index);
244 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
245 V4L2_SUBDEV_FORMAT_ACTIVE);
247 printf("Unable to set format: %s (%d)\n", strerror(-ret), ret);
251 printf("Format set: %s %ux%u\n",
252 pixelcode_to_string(format->code), format->width, format->height);
257 static int setup_format(struct media_device *media, const char *p, char **endp)
259 struct v4l2_mbus_framefmt format;
260 struct media_entity_pad *pad;
265 pad = parse_pad_format(media, &format, p, &end);
267 printf("Unable to parse format\n");
271 ret = set_format(pad, &format);
275 /* If the pad is an output pad, automatically set the same format on
276 * the remote subdev input pads, if any.
278 if (pad->type == MEDIA_PAD_TYPE_OUTPUT) {
279 for (i = 0; i < pad->entity->info.links; ++i) {
280 struct media_entity_link *link = &pad->entity->links[i];
281 struct v4l2_mbus_framefmt remote_format;
283 if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
286 if (link->source == pad &&
287 link->sink->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV) {
288 remote_format = format;
289 set_format(link->sink, &remote_format);
298 static int setup_formats(struct media_device *media, const char *p)
304 ret = setup_format(media, p, &end);
306 printf("Unable to parse format\n");
311 } while (*end == ',');
313 return *end ? -EINVAL : 0;
316 int main(int argc, char **argv)
318 struct media_device *media;
320 if (parse_cmdline(argc, argv))
323 /* Open the media device and enumerate entities, pads and links. */
324 media = media_open(media_opts.devname, media_opts.verbose);
328 if (media_opts.entity) {
329 struct media_entity *entity;
331 entity = media_get_entity_by_name(media, media_opts.entity,
332 strlen(media_opts.entity));
334 printf("%s\n", entity->devname);
336 printf("Entity '%s' not found\n", media_opts.entity);
339 if (media_opts.print || media_opts.print_dot) {
340 media_print_topology(media, media_opts.print_dot);
344 if (media_opts.reset) {
345 printf("Resetting all links to inactive\n");
346 media_reset_links(media);
349 if (media_opts.links)
350 setup_links(media, media_opts.links);
352 if (media_opts.formats)
353 setup_formats(media, media_opts.formats);
355 if (media_opts.interactive) {
360 printf("Enter a link to modify or enter to stop\n");
361 if (fgets(buffer, sizeof buffer, stdin) == NULL)
364 if (buffer[0] == '\n')
367 setup_link(media, buffer, &end);