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);
67 entity = media_get_entity_by_id(media, entity_id);
74 for (p = end + 1; isspace(*p); ++p);
76 pad = strtoul(p, &end, 10);
77 for (p = end; isspace(*p); ++p);
79 if (pad >= entity->info.pads)
82 for (p = end; isspace(*p); ++p);
85 return &entity->pads[pad];
88 static struct media_entity_link *parse_link(struct media_device *media, const char *p, char **endp)
90 struct media_entity_link *link;
91 struct media_entity_pad *source;
92 struct media_entity_pad *sink;
96 source = parse_pad(media, p, &end);
100 if (end[0] != '-' || end[1] != '>')
104 sink = parse_pad(media, p, &end);
110 for (i = 0; i < source->entity->info.links; i++) {
111 link = &source->entity->links[i];
113 if (link->source == source && link->sink == sink)
120 static int setup_link(struct media_device *media, const char *p, char **endp)
122 struct media_entity_link *link;
126 link = parse_link(media, p, &end);
128 printf("Unable to parse link\n");
134 printf("Unable to parse link flags\n");
138 flags = strtoul(p, &end, 10);
139 for (p = end; isspace(*p); p++);
141 printf("Unable to parse link flags\n");
145 for (; isspace(*p); p++);
148 printf("Setting up link %u:%u -> %u:%u [%u]\n",
149 link->source->entity->info.id, link->source->index,
150 link->sink->entity->info.id, link->sink->index,
153 return media_setup_link(media, link->source, link->sink, flags);
156 static int setup_links(struct media_device *media, const char *p)
162 ret = setup_link(media, p, &end);
167 } while (*end == ',');
169 return *end ? -EINVAL : 0;
172 /* -----------------------------------------------------------------------------
176 static int parse_format(struct v4l2_mbus_framefmt *format, const char *p, char **endp)
178 enum v4l2_mbus_pixelcode code;
179 unsigned int width, height;
182 for (; isspace(*p); ++p);
183 for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
185 code = string_to_pixelcode(p, end - p);
186 if (code == (enum v4l2_mbus_pixelcode)-1)
189 for (p = end; isspace(*p); ++p);
190 width = strtoul(p, &end, 10);
195 height = strtoul(p, &end, 10);
198 memset(format, 0, sizeof(*format));
199 format->width = width;
200 format->height = height;
206 static struct media_entity_pad *parse_pad_format(struct media_device *media,
207 struct v4l2_mbus_framefmt *format, const char *p, char **endp)
209 struct media_entity_pad *pad;
213 for (; isspace(*p); ++p);
215 pad = parse_pad(media, p, &end);
219 for (p = end; isspace(*p); ++p);
223 ret = parse_format(format, p, &end);
227 for (p = end; isspace(*p); p++);
235 static int set_format(struct media_entity_pad *pad, struct v4l2_mbus_framefmt *format)
239 printf("Setting up format %s %ux%u on pad %s/%u\n",
240 pixelcode_to_string(format->code), format->width, format->height,
241 pad->entity->info.name, pad->index);
243 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
244 V4L2_SUBDEV_FORMAT_ACTIVE);
246 printf("Unable to set format: %s (%d)\n", strerror(-ret), ret);
250 printf("Format set: %s %ux%u\n",
251 pixelcode_to_string(format->code), format->width, format->height);
256 static int setup_format(struct media_device *media, const char *p, char **endp)
258 struct v4l2_mbus_framefmt format;
259 struct media_entity_pad *pad;
264 pad = parse_pad_format(media, &format, p, &end);
266 printf("Unable to parse format\n");
270 ret = set_format(pad, &format);
274 /* If the pad is an output pad, automatically set the same format on
275 * the remote subdev input pads, if any.
277 if (pad->type == MEDIA_PAD_TYPE_OUTPUT) {
278 for (i = 0; i < pad->entity->info.links; ++i) {
279 struct media_entity_link *link = &pad->entity->links[i];
280 struct v4l2_mbus_framefmt remote_format;
282 if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
285 if (link->source == pad &&
286 link->sink->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV) {
287 remote_format = format;
288 set_format(link->sink, &remote_format);
297 static int setup_formats(struct media_device *media, const char *p)
303 ret = setup_format(media, p, &end);
305 printf("Unable to parse format\n");
310 } while (*end == ',');
312 return *end ? -EINVAL : 0;
315 int main(int argc, char **argv)
317 struct media_device *media;
319 if (parse_cmdline(argc, argv))
322 /* Open the media device and enumerate entities, pads and links. */
323 media = media_open(media_opts.devname, media_opts.verbose);
327 if (media_opts.entity) {
328 struct media_entity *entity;
330 entity = media_get_entity_by_name(media, media_opts.entity,
331 strlen(media_opts.entity));
333 printf("%s\n", entity->devname);
335 printf("Entity '%s' not found\n", media_opts.entity);
338 if (media_opts.print || media_opts.print_dot) {
339 media_print_topology(media, media_opts.print_dot);
343 if (media_opts.reset) {
344 printf("Resetting all links to inactive\n");
345 media_reset_links(media);
348 if (media_opts.links)
349 setup_links(media, media_opts.links);
351 if (media_opts.formats)
352 setup_formats(media, media_opts.formats);
354 if (media_opts.interactive) {
359 printf("Enter a link to modify or enter to stop\n");
360 if (fgets(buffer, sizeof buffer, stdin) == NULL)
363 if (buffer[0] == '\n')
366 setup_link(media, buffer, &end);