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 int parse_crop(struct v4l2_rect *crop, const char *p, char **endp)
210 for (; isspace(*p); ++p);
212 crop->left = strtoul(p, &end, 10);
217 crop->top = strtoul(p, &end, 10);
222 crop->width = strtoul(p, &end, 10);
227 crop->height = strtoul(p, &end, 10);
233 static struct media_entity_pad *parse_pad_format(struct media_device *media,
234 struct v4l2_mbus_framefmt *format, struct v4l2_rect *crop,
235 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++);
257 ret = parse_crop(crop, p, &end);
261 for (p = end; isspace(*p); p++);
267 *endp = (char *)p + 1;
271 static int set_format(struct media_entity_pad *pad, struct v4l2_mbus_framefmt *format)
275 printf("Setting up format %s %ux%u on pad %s/%u\n",
276 pixelcode_to_string(format->code), format->width, format->height,
277 pad->entity->info.name, pad->index);
279 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
280 V4L2_SUBDEV_FORMAT_ACTIVE);
282 printf("Unable to set format: %s (%d)\n", strerror(-ret), ret);
286 printf("Format set: %s %ux%u\n",
287 pixelcode_to_string(format->code), format->width, format->height);
292 static int set_crop(struct media_entity_pad *pad, struct v4l2_rect *crop)
296 printf("Setting up crop rectangle %u,%u/%ux%u on pad %s/%u\n",
297 crop->left, crop->top, crop->width, crop->height,
298 pad->entity->info.name, pad->index);
300 ret = v4l2_subdev_set_crop(pad->entity, crop, pad->index,
301 V4L2_SUBDEV_FORMAT_ACTIVE);
303 printf("Unable to set crop rectangle: %s (%d)", strerror(-ret), ret);
307 printf("Crop rectangle set: %u,%u/%ux%u\n",
308 crop->left, crop->top, crop->width, crop->height);
313 static int setup_format(struct media_device *media, const char *p, char **endp)
315 struct v4l2_mbus_framefmt format;
316 struct media_entity_pad *pad;
317 struct v4l2_rect crop = { -1, -1, -1, -1 };
322 pad = parse_pad_format(media, &format, &crop, p, &end);
324 printf("Unable to parse format\n");
328 ret = set_format(pad, &format);
330 printf("Unable to set format\n");
334 if (crop.left != -1 && crop.top != -1) {
335 ret = set_crop(pad, &crop);
337 printf("Unable to set crop rectangle\n");
342 /* If the pad is an output pad, automatically set the same format on
343 * the remote subdev input pads, if any.
345 if (pad->type == MEDIA_PAD_TYPE_OUTPUT) {
346 for (i = 0; i < pad->entity->info.links; ++i) {
347 struct media_entity_link *link = &pad->entity->links[i];
348 struct v4l2_mbus_framefmt remote_format;
350 if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
353 if (link->source == pad &&
354 link->sink->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV) {
355 remote_format = format;
356 set_format(link->sink, &remote_format);
365 static int setup_formats(struct media_device *media, const char *p)
371 ret = setup_format(media, p, &end);
376 } while (*end == ',');
378 return *end ? -EINVAL : 0;
381 int main(int argc, char **argv)
383 struct media_device *media;
385 if (parse_cmdline(argc, argv))
388 /* Open the media device and enumerate entities, pads and links. */
389 media = media_open(media_opts.devname, media_opts.verbose);
393 if (media_opts.entity) {
394 struct media_entity *entity;
396 entity = media_get_entity_by_name(media, media_opts.entity,
397 strlen(media_opts.entity));
399 printf("%s\n", entity->devname);
401 printf("Entity '%s' not found\n", media_opts.entity);
404 if (media_opts.print || media_opts.print_dot) {
405 media_print_topology(media, media_opts.print_dot);
409 if (media_opts.reset) {
410 printf("Resetting all links to inactive\n");
411 media_reset_links(media);
414 if (media_opts.links)
415 setup_links(media, media_opts.links);
417 if (media_opts.formats)
418 setup_formats(media, media_opts.formats);
420 if (media_opts.interactive) {
425 printf("Enter a link to modify or enter to stop\n");
426 if (fgets(buffer, sizeof buffer, stdin) == NULL)
429 if (buffer[0] == '\n')
432 setup_link(media, buffer, &end);