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_ENT_T_DEVNODE, "Node" },
115 { MEDIA_ENT_T_V4L2_SUBDEV, "V4L2 subdev" },
120 type &= MEDIA_ENT_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_ENT_SUBTYPE_MASK;
148 switch (type & MEDIA_ENT_TYPE_MASK) {
149 case MEDIA_ENT_T_DEVNODE:
150 if (subtype >= ARRAY_SIZE(node_types))
152 return node_types[subtype];
154 case MEDIA_ENT_T_V4L2_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_FL_SINK, "Input" },
170 { MEDIA_PAD_FL_SOURCE, "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_ENT_T_DEVNODE:
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_ENT_T_V4L2_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_FL_SINK))
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_FL_SOURCE))
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_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_ENT_T_V4L2_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_ENT_T_V4L2_SUBDEV)
244 printf(":port%u", link->sink->index);
246 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
247 printf(" [style=bold]");
248 else if (!(link->flags & MEDIA_LNK_FL_ENABLED))
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->num_links, entity->num_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_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_ENT_T_V4L2_SUBDEV)
283 v4l2_subdev_print_format(entity, j, V4L2_SUBDEV_FORMAT_ACTIVE);
287 for (k = 0; k < entity->num_links; k++) {
288 struct media_link *link = &entity->links[k];
289 struct media_pad *source = link->source;
290 struct media_pad *sink = link->sink;
292 if (source->entity == entity && source->index == j)
293 printf("\t\t-> '%s':pad%u [",
294 sink->entity->info.name, sink->index);
295 else if (sink->entity == entity && sink->index == j)
296 printf("\t\t<- '%s':pad%u [",
297 source->entity->info.name, source->index);
301 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
302 printf("IMMUTABLE,");
303 if (link->flags & MEDIA_LNK_FL_ENABLED)
313 void media_print_topology(struct media_device *media, int dot)
316 media_print_topology_dot(media);
318 media_print_topology_text(media);
321 /* -----------------------------------------------------------------------------
325 static struct media_pad *parse_pad(struct media_device *media, const char *p, char **endp)
327 unsigned int entity_id, pad;
328 struct media_entity *entity;
331 for (; isspace(*p); ++p);
334 for (end = (char *)p + 1; *end && *end != '"'; ++end);
338 entity = media_get_entity_by_name(media, p + 1, end - p - 1);
344 entity_id = strtoul(p, &end, 10);
345 entity = media_get_entity_by_id(media, entity_id);
349 for (; isspace(*end); ++end);
353 for (p = end + 1; isspace(*p); ++p);
355 pad = strtoul(p, &end, 10);
356 for (p = end; isspace(*p); ++p);
358 if (pad >= entity->info.pads)
361 for (p = end; isspace(*p); ++p);
365 return &entity->pads[pad];
368 static struct media_link *parse_link(struct media_device *media, const char *p, char **endp)
370 struct media_link *link;
371 struct media_pad *source;
372 struct media_pad *sink;
376 source = parse_pad(media, p, &end);
380 if (end[0] != '-' || end[1] != '>')
384 sink = parse_pad(media, p, &end);
390 for (i = 0; i < source->entity->num_links; i++) {
391 link = &source->entity->links[i];
393 if (link->source == source && link->sink == sink)
400 static int setup_link(struct media_device *media, const char *p, char **endp)
402 struct media_link *link;
406 link = parse_link(media, p, &end);
408 printf("Unable to parse link\n");
414 printf("Unable to parse link flags\n");
418 flags = strtoul(p, &end, 10);
419 for (p = end; isspace(*p); p++);
421 printf("Unable to parse link flags\n");
425 for (; isspace(*p); p++);
428 printf("Setting up link %u:%u -> %u:%u [%u]\n",
429 link->source->entity->info.id, link->source->index,
430 link->sink->entity->info.id, link->sink->index,
433 return media_setup_link(media, link->source, link->sink, flags);
436 static int setup_links(struct media_device *media, const char *p)
442 ret = setup_link(media, p, &end);
447 } while (*end == ',');
449 return *end ? -EINVAL : 0;
452 /* -----------------------------------------------------------------------------
456 static int parse_format(struct v4l2_mbus_framefmt *format, const char *p, char **endp)
458 enum v4l2_mbus_pixelcode code;
459 unsigned int width, height;
462 for (; isspace(*p); ++p);
463 for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
465 code = string_to_pixelcode(p, end - p);
466 if (code == (enum v4l2_mbus_pixelcode)-1)
469 for (p = end; isspace(*p); ++p);
470 width = strtoul(p, &end, 10);
475 height = strtoul(p, &end, 10);
478 memset(format, 0, sizeof(*format));
479 format->width = width;
480 format->height = height;
486 static int parse_crop(struct v4l2_rect *crop, const char *p, char **endp)
493 crop->left = strtoul(p, &end, 10);
498 crop->top = strtoul(p, &end, 10);
505 crop->width = strtoul(p, &end, 10);
510 crop->height = strtoul(p, &end, 10);
516 static int parse_frame_interval(struct v4l2_fract *interval, const char *p, char **endp)
520 for (; isspace(*p); ++p);
522 interval->numerator = strtoul(p, &end, 10);
524 for (p = end; isspace(*p); ++p);
528 for (; isspace(*p); ++p);
529 interval->denominator = strtoul(p, &end, 10);
535 static struct media_pad *parse_pad_format(struct media_device *media,
536 struct v4l2_mbus_framefmt *format, struct v4l2_rect *crop,
537 struct v4l2_fract *interval, const char *p, char **endp)
539 struct media_pad *pad;
543 for (; isspace(*p); ++p);
545 pad = parse_pad(media, p, &end);
549 for (p = end; isspace(*p); ++p);
553 for (; isspace(*p); ++p);
556 ret = parse_format(format, p, &end);
560 for (p = end; isspace(*p); p++);
564 ret = parse_crop(crop, p, &end);
568 for (p = end; isspace(*p); p++);
572 ret = parse_frame_interval(interval, ++p, &end);
576 for (p = end; isspace(*p); p++);
582 *endp = (char *)p + 1;
586 static int set_format(struct media_pad *pad, struct v4l2_mbus_framefmt *format)
590 if (format->width == 0 || format->height == 0)
593 printf("Setting up format %s %ux%u on pad %s/%u\n",
594 pixelcode_to_string(format->code), format->width, format->height,
595 pad->entity->info.name, pad->index);
597 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
598 V4L2_SUBDEV_FORMAT_ACTIVE);
600 printf("Unable to set format: %s (%d)\n", strerror(-ret), ret);
604 printf("Format set: %s %ux%u\n",
605 pixelcode_to_string(format->code), format->width, format->height);
610 static int set_crop(struct media_pad *pad, struct v4l2_rect *crop)
614 if (crop->left == -1 || crop->top == -1)
617 printf("Setting up crop rectangle (%u,%u)/%ux%u on pad %s/%u\n",
618 crop->left, crop->top, crop->width, crop->height,
619 pad->entity->info.name, pad->index);
621 ret = v4l2_subdev_set_crop(pad->entity, crop, pad->index,
622 V4L2_SUBDEV_FORMAT_ACTIVE);
624 printf("Unable to set crop rectangle: %s (%d)\n", strerror(-ret), ret);
628 printf("Crop rectangle set: (%u,%u)/%ux%u\n",
629 crop->left, crop->top, crop->width, crop->height);
634 static int set_frame_interval(struct media_entity *entity, struct v4l2_fract *interval)
638 if (interval->numerator == 0)
641 printf("Setting up frame interval %u/%u on entity %s\n",
642 interval->numerator, interval->denominator, entity->info.name);
644 ret = v4l2_subdev_set_frame_interval(entity, interval);
646 printf("Unable to set frame interval: %s (%d)", strerror(-ret), ret);
650 printf("Frame interval set: %u/%u\n",
651 interval->numerator, interval->denominator);
657 static int setup_format(struct media_device *media, const char *p, char **endp)
659 struct v4l2_mbus_framefmt format = { 0, 0, 0 };
660 struct media_pad *pad;
661 struct v4l2_rect crop = { -1, -1, -1, -1 };
662 struct v4l2_fract interval = { 0, 0 };
667 pad = parse_pad_format(media, &format, &crop, &interval, p, &end);
669 printf("Unable to parse format\n");
673 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
674 ret = set_crop(pad, &crop);
679 ret = set_format(pad, &format);
683 if (pad->flags & MEDIA_PAD_FL_SINK) {
684 ret = set_crop(pad, &crop);
689 ret = set_frame_interval(pad->entity, &interval);
694 /* If the pad is an output pad, automatically set the same format on
695 * the remote subdev input pads, if any.
697 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
698 for (i = 0; i < pad->entity->num_links; ++i) {
699 struct media_link *link = &pad->entity->links[i];
700 struct v4l2_mbus_framefmt remote_format;
702 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
705 if (link->source == pad &&
706 link->sink->entity->info.type == MEDIA_ENT_T_V4L2_SUBDEV) {
707 remote_format = format;
708 set_format(link->sink, &remote_format);
717 static int setup_formats(struct media_device *media, const char *p)
723 ret = setup_format(media, p, &end);
728 } while (*end == ',');
730 return *end ? -EINVAL : 0;
733 int main(int argc, char **argv)
735 struct media_device *media;
738 if (parse_cmdline(argc, argv))
741 /* Open the media device and enumerate entities, pads and links. */
742 media = media_open(media_opts.devname, media_opts.verbose);
746 if (media_opts.entity) {
747 struct media_entity *entity;
749 entity = media_get_entity_by_name(media, media_opts.entity,
750 strlen(media_opts.entity));
751 if (entity == NULL) {
752 printf("Entity '%s' not found\n", media_opts.entity);
756 printf("%s\n", entity->devname);
759 if (media_opts.pad) {
760 struct media_pad *pad;
762 pad = parse_pad(media, media_opts.pad, NULL);
764 printf("Pad '%s' not found\n", media_opts.pad);
768 v4l2_subdev_print_format(pad->entity, pad->index,
769 V4L2_SUBDEV_FORMAT_ACTIVE);
773 if (media_opts.print || media_opts.print_dot) {
774 media_print_topology(media, media_opts.print_dot);
778 if (media_opts.reset) {
779 printf("Resetting all links to inactive\n");
780 media_reset_links(media);
783 if (media_opts.links)
784 setup_links(media, media_opts.links);
786 if (media_opts.formats)
787 setup_formats(media, media_opts.formats);
789 if (media_opts.interactive) {
794 printf("Enter a link to modify or enter to stop\n");
795 if (fgets(buffer, sizeof buffer, stdin) == NULL)
798 if (buffer[0] == '\n')
801 setup_link(media, buffer, &end);
811 return ret ? EXIT_FAILURE : EXIT_SUCCESS;