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 /* -----------------------------------------------------------------------------
50 enum v4l2_mbus_pixelcode code;
52 { "Y8", V4L2_MBUS_FMT_Y8_1X8},
53 { "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
54 { "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
55 { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
56 { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
59 static const char *pixelcode_to_string(enum v4l2_mbus_pixelcode code)
63 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
64 if (mbus_formats[i].code == code)
65 return mbus_formats[i].name;
71 static enum v4l2_mbus_pixelcode string_to_pixelcode(const char *string,
76 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
77 if (strncmp(mbus_formats[i].name, string, length) == 0)
81 if (i == ARRAY_SIZE(mbus_formats))
82 return (enum v4l2_mbus_pixelcode)-1;
84 return mbus_formats[i].code;
87 static void v4l2_subdev_print_format(struct media_entity *entity,
88 unsigned int pad, enum v4l2_subdev_format_whence which)
90 struct v4l2_mbus_framefmt format;
91 struct v4l2_rect rect;
94 ret = v4l2_subdev_get_format(entity, &format, pad, which);
98 printf("[%s %ux%u", pixelcode_to_string(format.code),
99 format.width, format.height);
101 ret = v4l2_subdev_get_crop(entity, &rect, pad, which);
103 printf(" (%u,%u)/%ux%u", rect.left, rect.top,
104 rect.width, rect.height);
108 static const char *media_entity_type_to_string(unsigned type)
110 static const struct {
114 { MEDIA_ENTITY_TYPE_NODE, "Node" },
115 { MEDIA_ENTITY_TYPE_SUBDEV, "V4L2 subdev" },
120 type &= MEDIA_ENTITY_TYPE_MASK;
122 for (i = 0; i < ARRAY_SIZE(types); i++) {
123 if (types[i].type == type)
124 return types[i].name;
130 static const char *media_entity_subtype_to_string(unsigned type)
132 static const char *node_types[] = {
139 static const char *subdev_types[] = {
146 unsigned int subtype = type & MEDIA_ENTITY_SUBTYPE_MASK;
148 switch (type & MEDIA_ENTITY_TYPE_MASK) {
149 case MEDIA_ENTITY_TYPE_NODE:
150 if (subtype >= ARRAY_SIZE(node_types))
152 return node_types[subtype];
154 case MEDIA_ENTITY_TYPE_SUBDEV:
155 if (subtype >= ARRAY_SIZE(subdev_types))
157 return subdev_types[subtype];
159 return node_types[0];
163 static const char *media_pad_type_to_string(unsigned flag)
165 static const struct {
169 { MEDIA_PAD_FLAG_INPUT, "Input" },
170 { MEDIA_PAD_FLAG_OUTPUT, "Output" },
175 for (i = 0; i < ARRAY_SIZE(flags); i++) {
176 if (flags[i].flag & flag)
177 return flags[i].name;
183 static void media_print_topology_dot(struct media_device *media)
187 printf("digraph board {\n");
188 printf("\trankdir=TB\n");
190 for (i = 0; i < media->entities_count; ++i) {
191 struct media_entity *entity = &media->entities[i];
194 switch (media_entity_type(entity)) {
195 case MEDIA_ENTITY_TYPE_NODE:
196 printf("\tn%08x [label=\"%s\\n%s\", shape=box, style=filled, "
197 "fillcolor=yellow]\n",
198 entity->info.id, entity->info.name, entity->devname);
201 case MEDIA_ENTITY_TYPE_SUBDEV:
202 printf("\tn%08x [label=\"{{", entity->info.id);
204 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
205 if (!(entity->pads[j].flags & MEDIA_PAD_FLAG_INPUT))
208 printf("%s<port%u> %u", npads ? " | " : "", j, j);
212 printf("} | %s", entity->info.name);
214 printf("\\n%s", entity->devname);
217 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
218 if (!(entity->pads[j].flags & MEDIA_PAD_FLAG_OUTPUT))
221 printf("%s<port%u> %u", npads ? " | " : "", j, j);
225 printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n");
232 for (j = 0; j < entity->num_links; j++) {
233 struct media_entity_link *link = &entity->links[j];
235 if (link->source->entity != entity)
238 printf("\tn%08x", link->source->entity->info.id);
239 if (media_entity_type(link->source->entity) == MEDIA_ENTITY_TYPE_SUBDEV)
240 printf(":port%u", link->source->index);
242 printf("n%08x", link->sink->entity->info.id);
243 if (media_entity_type(link->sink->entity) == MEDIA_ENTITY_TYPE_SUBDEV)
244 printf(":port%u", link->sink->index);
246 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
247 printf(" [style=bold]");
248 else if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
249 printf(" [style=dashed]");
257 static void media_print_topology_text(struct media_device *media)
259 unsigned int i, j, k;
260 unsigned int padding;
262 printf("Device topology\n");
264 for (i = 0; i < media->entities_count; ++i) {
265 struct media_entity *entity = &media->entities[i];
267 padding = printf("- entity %u: ", entity->info.id);
268 printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
269 entity->info.pads, entity->info.pads > 1 ? "s" : "",
270 entity->info.links, entity->info.links > 1 ? "s" : "");
271 printf("%*ctype %s subtype %s\n", padding, ' ',
272 media_entity_type_to_string(entity->info.type),
273 media_entity_subtype_to_string(entity->info.type));
274 if (entity->devname[0])
275 printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
277 for (j = 0; j < entity->info.pads; j++) {
278 struct media_entity_pad *pad = &entity->pads[j];
280 printf("\tpad%u: %s ", j, media_pad_type_to_string(pad->flags));
282 if (media_entity_type(entity) == MEDIA_ENTITY_TYPE_SUBDEV)
283 v4l2_subdev_print_format(entity, j, V4L2_SUBDEV_FORMAT_ACTIVE);
287 for (k = 0; k < entity->num_links; k++) {
288 struct media_entity_link *link = &entity->links[k];
290 if (link->source->entity != entity ||
291 link->source->index != j)
294 printf("\t\t-> '%s':pad%u [",
295 link->sink->entity->info.name, link->sink->index);
297 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
298 printf("IMMUTABLE,");
299 if (link->flags & MEDIA_LINK_FLAG_ACTIVE)
309 void media_print_topology(struct media_device *media, int dot)
312 media_print_topology_dot(media);
314 media_print_topology_text(media);
317 /* -----------------------------------------------------------------------------
321 static struct media_entity_pad *parse_pad(struct media_device *media, const char *p, char **endp)
323 unsigned int entity_id, pad;
324 struct media_entity *entity;
327 for (; isspace(*p); ++p);
330 for (end = (char *)p + 1; *end && *end != '"'; ++end);
334 entity = media_get_entity_by_name(media, p + 1, end - p - 1);
340 entity_id = strtoul(p, &end, 10);
341 entity = media_get_entity_by_id(media, entity_id);
345 for (; isspace(*end); ++end);
349 for (p = end + 1; isspace(*p); ++p);
351 pad = strtoul(p, &end, 10);
352 for (p = end; isspace(*p); ++p);
354 if (pad >= entity->info.pads)
357 for (p = end; isspace(*p); ++p);
361 return &entity->pads[pad];
364 static struct media_entity_link *parse_link(struct media_device *media, const char *p, char **endp)
366 struct media_entity_link *link;
367 struct media_entity_pad *source;
368 struct media_entity_pad *sink;
372 source = parse_pad(media, p, &end);
376 if (end[0] != '-' || end[1] != '>')
380 sink = parse_pad(media, p, &end);
386 for (i = 0; i < source->entity->num_links; i++) {
387 link = &source->entity->links[i];
389 if (link->source == source && link->sink == sink)
396 static int setup_link(struct media_device *media, const char *p, char **endp)
398 struct media_entity_link *link;
402 link = parse_link(media, p, &end);
404 printf("Unable to parse link\n");
410 printf("Unable to parse link flags\n");
414 flags = strtoul(p, &end, 10);
415 for (p = end; isspace(*p); p++);
417 printf("Unable to parse link flags\n");
421 for (; isspace(*p); p++);
424 printf("Setting up link %u:%u -> %u:%u [%u]\n",
425 link->source->entity->info.id, link->source->index,
426 link->sink->entity->info.id, link->sink->index,
429 return media_setup_link(media, link->source, link->sink, flags);
432 static int setup_links(struct media_device *media, const char *p)
438 ret = setup_link(media, p, &end);
443 } while (*end == ',');
445 return *end ? -EINVAL : 0;
448 /* -----------------------------------------------------------------------------
452 static int parse_format(struct v4l2_mbus_framefmt *format, const char *p, char **endp)
454 enum v4l2_mbus_pixelcode code;
455 unsigned int width, height;
458 for (; isspace(*p); ++p);
459 for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
461 code = string_to_pixelcode(p, end - p);
462 if (code == (enum v4l2_mbus_pixelcode)-1)
465 for (p = end; isspace(*p); ++p);
466 width = strtoul(p, &end, 10);
471 height = strtoul(p, &end, 10);
474 memset(format, 0, sizeof(*format));
475 format->width = width;
476 format->height = height;
482 static int parse_crop(struct v4l2_rect *crop, const char *p, char **endp)
489 crop->left = strtoul(p, &end, 10);
494 crop->top = strtoul(p, &end, 10);
501 crop->width = strtoul(p, &end, 10);
506 crop->height = strtoul(p, &end, 10);
512 static int parse_frame_interval(struct v4l2_fract *interval, const char *p, char **endp)
516 for (; isspace(*p); ++p);
518 interval->numerator = strtoul(p, &end, 10);
520 for (p = end; isspace(*p); ++p);
524 for (; isspace(*p); ++p);
525 interval->denominator = strtoul(p, &end, 10);
531 static struct media_entity_pad *parse_pad_format(struct media_device *media,
532 struct v4l2_mbus_framefmt *format, struct v4l2_rect *crop,
533 struct v4l2_fract *interval, const char *p, char **endp)
535 struct media_entity_pad *pad;
539 for (; isspace(*p); ++p);
541 pad = parse_pad(media, p, &end);
545 for (p = end; isspace(*p); ++p);
549 for (; isspace(*p); ++p);
552 ret = parse_format(format, p, &end);
556 for (p = end; isspace(*p); p++);
560 ret = parse_crop(crop, p, &end);
564 for (p = end; isspace(*p); p++);
568 ret = parse_frame_interval(interval, ++p, &end);
572 for (p = end; isspace(*p); p++);
578 *endp = (char *)p + 1;
582 static int set_format(struct media_entity_pad *pad, struct v4l2_mbus_framefmt *format)
586 if (format->width == 0 || format->height == 0)
589 printf("Setting up format %s %ux%u on pad %s/%u\n",
590 pixelcode_to_string(format->code), format->width, format->height,
591 pad->entity->info.name, pad->index);
593 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
594 V4L2_SUBDEV_FORMAT_ACTIVE);
596 printf("Unable to set format: %s (%d)\n", strerror(-ret), ret);
600 printf("Format set: %s %ux%u\n",
601 pixelcode_to_string(format->code), format->width, format->height);
606 static int set_crop(struct media_entity_pad *pad, struct v4l2_rect *crop)
610 if (crop->left == -1 || crop->top == -1)
613 printf("Setting up crop rectangle (%u,%u)/%ux%u on pad %s/%u\n",
614 crop->left, crop->top, crop->width, crop->height,
615 pad->entity->info.name, pad->index);
617 ret = v4l2_subdev_set_crop(pad->entity, crop, pad->index,
618 V4L2_SUBDEV_FORMAT_ACTIVE);
620 printf("Unable to set crop rectangle: %s (%d)\n", strerror(-ret), ret);
624 printf("Crop rectangle set: (%u,%u)/%ux%u\n",
625 crop->left, crop->top, crop->width, crop->height);
630 static int set_frame_interval(struct media_entity *entity, struct v4l2_fract *interval)
634 if (interval->numerator == 0)
637 printf("Setting up frame interval %u/%u on entity %s\n",
638 interval->numerator, interval->denominator, entity->info.name);
640 ret = v4l2_subdev_set_frame_interval(entity, interval);
642 printf("Unable to set frame interval: %s (%d)", strerror(-ret), ret);
646 printf("Frame interval set: %u/%u\n",
647 interval->numerator, interval->denominator);
653 static int setup_format(struct media_device *media, const char *p, char **endp)
655 struct v4l2_mbus_framefmt format = { 0, 0, 0 };
656 struct media_entity_pad *pad;
657 struct v4l2_rect crop = { -1, -1, -1, -1 };
658 struct v4l2_fract interval = { 0, 0 };
663 pad = parse_pad_format(media, &format, &crop, &interval, p, &end);
665 printf("Unable to parse format\n");
669 if (pad->flags & MEDIA_PAD_FLAG_OUTPUT) {
670 ret = set_crop(pad, &crop);
675 ret = set_format(pad, &format);
679 if (pad->flags & MEDIA_PAD_FLAG_INPUT) {
680 ret = set_crop(pad, &crop);
685 ret = set_frame_interval(pad->entity, &interval);
690 /* If the pad is an output pad, automatically set the same format on
691 * the remote subdev input pads, if any.
693 if (pad->flags & MEDIA_PAD_FLAG_OUTPUT) {
694 for (i = 0; i < pad->entity->num_links; ++i) {
695 struct media_entity_link *link = &pad->entity->links[i];
696 struct v4l2_mbus_framefmt remote_format;
698 if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
701 if (link->source == pad &&
702 link->sink->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV) {
703 remote_format = format;
704 set_format(link->sink, &remote_format);
713 static int setup_formats(struct media_device *media, const char *p)
719 ret = setup_format(media, p, &end);
724 } while (*end == ',');
726 return *end ? -EINVAL : 0;
729 int main(int argc, char **argv)
731 struct media_device *media;
734 if (parse_cmdline(argc, argv))
737 /* Open the media device and enumerate entities, pads and links. */
738 media = media_open(media_opts.devname, media_opts.verbose);
742 if (media_opts.entity) {
743 struct media_entity *entity;
745 entity = media_get_entity_by_name(media, media_opts.entity,
746 strlen(media_opts.entity));
747 if (entity == NULL) {
748 printf("Entity '%s' not found\n", media_opts.entity);
752 printf("%s\n", entity->devname);
755 if (media_opts.pad) {
756 struct media_entity_pad *pad;
758 pad = parse_pad(media, media_opts.pad, NULL);
760 printf("Pad '%s' not found\n", media_opts.pad);
764 v4l2_subdev_print_format(pad->entity, pad->index,
765 V4L2_SUBDEV_FORMAT_ACTIVE);
769 if (media_opts.print || media_opts.print_dot) {
770 media_print_topology(media, media_opts.print_dot);
774 if (media_opts.reset) {
775 printf("Resetting all links to inactive\n");
776 media_reset_links(media);
779 if (media_opts.links)
780 setup_links(media, media_opts.links);
782 if (media_opts.formats)
783 setup_formats(media, media_opts.formats);
785 if (media_opts.interactive) {
790 printf("Enter a link to modify or enter to stop\n");
791 if (fgets(buffer, sizeof buffer, stdin) == NULL)
794 if (buffer[0] == '\n')
797 setup_link(media, buffer, &end);
807 return ret ? EXIT_FAILURE : EXIT_SUCCESS;