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);
66 entity_id = strtoul(p, &end, 10);
67 entity = media_get_entity_by_id(media, entity_id);
71 for (; isspace(*end); ++end);
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);
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 /* -----------------------------------------------------------------------------
178 static int parse_format(struct v4l2_mbus_framefmt *format, const char *p, char **endp)
180 enum v4l2_mbus_pixelcode code;
181 unsigned int width, height;
184 for (; isspace(*p); ++p);
185 for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
187 code = string_to_pixelcode(p, end - p);
188 if (code == (enum v4l2_mbus_pixelcode)-1)
191 for (p = end; isspace(*p); ++p);
192 width = strtoul(p, &end, 10);
197 height = strtoul(p, &end, 10);
200 memset(format, 0, sizeof(*format));
201 format->width = width;
202 format->height = height;
208 static int parse_crop(struct v4l2_rect *crop, const char *p, char **endp)
215 crop->left = strtoul(p, &end, 10);
220 crop->top = strtoul(p, &end, 10);
227 crop->width = strtoul(p, &end, 10);
232 crop->height = strtoul(p, &end, 10);
238 static int parse_frame_interval(struct v4l2_fract *interval, const char *p, char **endp)
242 for (; isspace(*p); ++p);
244 interval->numerator = strtoul(p, &end, 10);
246 for (p = end; isspace(*p); ++p);
250 for (; isspace(*p); ++p);
251 interval->denominator = strtoul(p, &end, 10);
257 static struct media_entity_pad *parse_pad_format(struct media_device *media,
258 struct v4l2_mbus_framefmt *format, struct v4l2_rect *crop,
259 struct v4l2_fract *interval, const char *p, char **endp)
261 struct media_entity_pad *pad;
265 for (; isspace(*p); ++p);
267 pad = parse_pad(media, p, &end);
271 for (p = end; isspace(*p); ++p);
275 ret = parse_format(format, p, &end);
279 for (p = end; isspace(*p); p++);
281 ret = parse_crop(crop, p, &end);
285 for (p = end; isspace(*p); p++);
289 ret = parse_frame_interval(interval, ++p, &end);
293 for (p = end; isspace(*p); p++);
299 *endp = (char *)p + 1;
303 static int set_format(struct media_entity_pad *pad, struct v4l2_mbus_framefmt *format)
307 printf("Setting up format %s %ux%u on pad %s/%u\n",
308 pixelcode_to_string(format->code), format->width, format->height,
309 pad->entity->info.name, pad->index);
311 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
312 V4L2_SUBDEV_FORMAT_ACTIVE);
314 printf("Unable to set format: %s (%d)\n", strerror(-ret), ret);
318 printf("Format set: %s %ux%u\n",
319 pixelcode_to_string(format->code), format->width, format->height);
324 static int set_crop(struct media_entity_pad *pad, struct v4l2_rect *crop)
328 if (crop->left == -1 || crop->top == -1)
331 printf("Setting up crop rectangle %u,%u/%ux%u on pad %s/%u\n",
332 crop->left, crop->top, crop->width, crop->height,
333 pad->entity->info.name, pad->index);
335 ret = v4l2_subdev_set_crop(pad->entity, crop, pad->index,
336 V4L2_SUBDEV_FORMAT_ACTIVE);
338 printf("Unable to set crop rectangle: %s (%d)", strerror(-ret), ret);
342 printf("Crop rectangle set: %u,%u/%ux%u\n",
343 crop->left, crop->top, crop->width, crop->height);
348 static int set_frame_interval(struct media_entity *entity, struct v4l2_fract *interval)
352 printf("Setting up frame interval %u/%u on entity %s\n",
353 interval->numerator, interval->denominator, entity->info.name);
355 ret = v4l2_subdev_set_frame_interval(entity, interval);
357 printf("Unable to set frame interval: %s (%d)", strerror(-ret), ret);
361 printf("Frame interval set: %u/%u\n",
362 interval->numerator, interval->denominator);
368 static int setup_format(struct media_device *media, const char *p, char **endp)
370 struct v4l2_mbus_framefmt format;
371 struct media_entity_pad *pad;
372 struct v4l2_rect crop = { -1, -1, -1, -1 };
373 struct v4l2_fract interval = { 0, 0 };
378 pad = parse_pad_format(media, &format, &crop, &interval, p, &end);
380 printf("Unable to parse format\n");
384 if (pad->type == MEDIA_PAD_TYPE_OUTPUT) {
385 ret = set_crop(pad, &crop);
390 ret = set_format(pad, &format);
394 if (pad->type == MEDIA_PAD_TYPE_INPUT) {
395 ret = set_crop(pad, &crop);
400 if (interval.numerator != 0) {
401 ret = set_frame_interval(pad->entity, &interval);
403 printf("Unable to set frame interval\n");
408 /* If the pad is an output pad, automatically set the same format on
409 * the remote subdev input pads, if any.
411 if (pad->type == MEDIA_PAD_TYPE_OUTPUT) {
412 for (i = 0; i < pad->entity->info.links; ++i) {
413 struct media_entity_link *link = &pad->entity->links[i];
414 struct v4l2_mbus_framefmt remote_format;
416 if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
419 if (link->source == pad &&
420 link->sink->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV) {
421 remote_format = format;
422 set_format(link->sink, &remote_format);
431 static int setup_formats(struct media_device *media, const char *p)
437 ret = setup_format(media, p, &end);
442 } while (*end == ',');
444 return *end ? -EINVAL : 0;
447 int main(int argc, char **argv)
449 struct media_device *media;
452 if (parse_cmdline(argc, argv))
455 /* Open the media device and enumerate entities, pads and links. */
456 media = media_open(media_opts.devname, media_opts.verbose);
460 if (media_opts.entity) {
461 struct media_entity *entity;
463 entity = media_get_entity_by_name(media, media_opts.entity,
464 strlen(media_opts.entity));
465 if (entity == NULL) {
466 printf("Entity '%s' not found\n", media_opts.entity);
470 printf("%s\n", entity->devname);
473 if (media_opts.pad) {
474 struct media_entity_pad *pad;
476 pad = parse_pad(media, media_opts.pad, NULL);
478 printf("Pad '%s' not found\n", media_opts.pad);
482 v4l2_subdev_print_format(pad->entity, pad->index,
483 V4L2_SUBDEV_FORMAT_ACTIVE);
487 if (media_opts.print || media_opts.print_dot) {
488 media_print_topology(media, media_opts.print_dot);
492 if (media_opts.reset) {
493 printf("Resetting all links to inactive\n");
494 media_reset_links(media);
497 if (media_opts.links)
498 setup_links(media, media_opts.links);
500 if (media_opts.formats)
501 setup_formats(media, media_opts.formats);
503 if (media_opts.interactive) {
508 printf("Enter a link to modify or enter to stop\n");
509 if (fgets(buffer, sizeof buffer, stdin) == NULL)
512 if (buffer[0] == '\n')
515 setup_link(media, buffer, &end);
525 return ret ? EXIT_FAILURE : EXIT_SUCCESS;