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 { "Y10", V4L2_MBUS_FMT_Y10_1X10 },
54 { "Y12", V4L2_MBUS_FMT_Y12_1X12 },
55 { "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
56 { "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
57 { "SBGGR8", V4L2_MBUS_FMT_SBGGR8_1X8 },
58 { "SGBRG8", V4L2_MBUS_FMT_SGBRG8_1X8 },
59 { "SGRBG8", V4L2_MBUS_FMT_SGRBG8_1X8 },
60 { "SRGGB8", V4L2_MBUS_FMT_SRGGB8_1X8 },
61 { "SBGGR10", V4L2_MBUS_FMT_SBGGR10_1X10 },
62 { "SGBRG10", V4L2_MBUS_FMT_SGBRG10_1X10 },
63 { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
64 { "SRGGB10", V4L2_MBUS_FMT_SRGGB10_1X10 },
65 { "SBGGR10_DPCM8", V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8 },
66 { "SGBRG10_DPCM8", V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8 },
67 { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
68 { "SRGGB10_DPCM8", V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8 },
69 { "SBGGR12", V4L2_MBUS_FMT_SBGGR12_1X12 },
70 { "SGBRG12", V4L2_MBUS_FMT_SGBRG12_1X12 },
71 { "SGRBG12", V4L2_MBUS_FMT_SGRBG12_1X12 },
72 { "SRGGB12", V4L2_MBUS_FMT_SRGGB12_1X12 },
75 static const char *pixelcode_to_string(enum v4l2_mbus_pixelcode code)
79 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
80 if (mbus_formats[i].code == code)
81 return mbus_formats[i].name;
87 static enum v4l2_mbus_pixelcode string_to_pixelcode(const char *string,
92 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
93 if (strncmp(mbus_formats[i].name, string, length) == 0)
97 if (i == ARRAY_SIZE(mbus_formats))
98 return (enum v4l2_mbus_pixelcode)-1;
100 return mbus_formats[i].code;
103 static void v4l2_subdev_print_format(struct media_entity *entity,
104 unsigned int pad, enum v4l2_subdev_format_whence which)
106 struct v4l2_mbus_framefmt format;
107 struct v4l2_rect rect;
110 ret = v4l2_subdev_get_format(entity, &format, pad, which);
114 printf("[%s %ux%u", pixelcode_to_string(format.code),
115 format.width, format.height);
117 ret = v4l2_subdev_get_crop(entity, &rect, pad, which);
119 printf(" (%u,%u)/%ux%u", rect.left, rect.top,
120 rect.width, rect.height);
124 static const char *media_entity_type_to_string(unsigned type)
126 static const struct {
130 { MEDIA_ENT_T_DEVNODE, "Node" },
131 { MEDIA_ENT_T_V4L2_SUBDEV, "V4L2 subdev" },
136 type &= MEDIA_ENT_TYPE_MASK;
138 for (i = 0; i < ARRAY_SIZE(types); i++) {
139 if (types[i].type == type)
140 return types[i].name;
146 static const char *media_entity_subtype_to_string(unsigned type)
148 static const char *node_types[] = {
155 static const char *subdev_types[] = {
162 unsigned int subtype = type & MEDIA_ENT_SUBTYPE_MASK;
164 switch (type & MEDIA_ENT_TYPE_MASK) {
165 case MEDIA_ENT_T_DEVNODE:
166 if (subtype >= ARRAY_SIZE(node_types))
168 return node_types[subtype];
170 case MEDIA_ENT_T_V4L2_SUBDEV:
171 if (subtype >= ARRAY_SIZE(subdev_types))
173 return subdev_types[subtype];
175 return node_types[0];
179 static const char *media_pad_type_to_string(unsigned flag)
181 static const struct {
185 { MEDIA_PAD_FL_SINK, "Input" },
186 { MEDIA_PAD_FL_SOURCE, "Output" },
191 for (i = 0; i < ARRAY_SIZE(flags); i++) {
192 if (flags[i].flag & flag)
193 return flags[i].name;
199 static void media_print_topology_dot(struct media_device *media)
203 printf("digraph board {\n");
204 printf("\trankdir=TB\n");
206 for (i = 0; i < media->entities_count; ++i) {
207 struct media_entity *entity = &media->entities[i];
210 switch (media_entity_type(entity)) {
211 case MEDIA_ENT_T_DEVNODE:
212 printf("\tn%08x [label=\"%s\\n%s\", shape=box, style=filled, "
213 "fillcolor=yellow]\n",
214 entity->info.id, entity->info.name, entity->devname);
217 case MEDIA_ENT_T_V4L2_SUBDEV:
218 printf("\tn%08x [label=\"{{", entity->info.id);
220 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
221 if (!(entity->pads[j].flags & MEDIA_PAD_FL_SINK))
224 printf("%s<port%u> %u", npads ? " | " : "", j, j);
228 printf("} | %s", entity->info.name);
230 printf("\\n%s", entity->devname);
233 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
234 if (!(entity->pads[j].flags & MEDIA_PAD_FL_SOURCE))
237 printf("%s<port%u> %u", npads ? " | " : "", j, j);
241 printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n");
248 for (j = 0; j < entity->num_links; j++) {
249 struct media_link *link = &entity->links[j];
251 if (link->source->entity != entity)
254 printf("\tn%08x", link->source->entity->info.id);
255 if (media_entity_type(link->source->entity) == MEDIA_ENT_T_V4L2_SUBDEV)
256 printf(":port%u", link->source->index);
258 printf("n%08x", link->sink->entity->info.id);
259 if (media_entity_type(link->sink->entity) == MEDIA_ENT_T_V4L2_SUBDEV)
260 printf(":port%u", link->sink->index);
262 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
263 printf(" [style=bold]");
264 else if (!(link->flags & MEDIA_LNK_FL_ENABLED))
265 printf(" [style=dashed]");
273 static void media_print_topology_text(struct media_device *media)
275 unsigned int i, j, k;
276 unsigned int padding;
278 printf("Device topology\n");
280 for (i = 0; i < media->entities_count; ++i) {
281 struct media_entity *entity = &media->entities[i];
283 padding = printf("- entity %u: ", entity->info.id);
284 printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
285 entity->info.pads, entity->info.pads > 1 ? "s" : "",
286 entity->num_links, entity->num_links > 1 ? "s" : "");
287 printf("%*ctype %s subtype %s\n", padding, ' ',
288 media_entity_type_to_string(entity->info.type),
289 media_entity_subtype_to_string(entity->info.type));
290 if (entity->devname[0])
291 printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
293 for (j = 0; j < entity->info.pads; j++) {
294 struct media_pad *pad = &entity->pads[j];
296 printf("\tpad%u: %s ", j, media_pad_type_to_string(pad->flags));
298 if (media_entity_type(entity) == MEDIA_ENT_T_V4L2_SUBDEV)
299 v4l2_subdev_print_format(entity, j, V4L2_SUBDEV_FORMAT_ACTIVE);
303 for (k = 0; k < entity->num_links; k++) {
304 struct media_link *link = &entity->links[k];
305 struct media_pad *source = link->source;
306 struct media_pad *sink = link->sink;
308 if (source->entity == entity && source->index == j)
309 printf("\t\t-> '%s':pad%u [",
310 sink->entity->info.name, sink->index);
311 else if (sink->entity == entity && sink->index == j)
312 printf("\t\t<- '%s':pad%u [",
313 source->entity->info.name, source->index);
317 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
318 printf("IMMUTABLE,");
319 if (link->flags & MEDIA_LNK_FL_ENABLED)
329 void media_print_topology(struct media_device *media, int dot)
332 media_print_topology_dot(media);
334 media_print_topology_text(media);
337 /* -----------------------------------------------------------------------------
341 static struct media_pad *parse_pad(struct media_device *media, const char *p, char **endp)
343 unsigned int entity_id, pad;
344 struct media_entity *entity;
347 for (; isspace(*p); ++p);
350 for (end = (char *)p + 1; *end && *end != '"'; ++end);
354 entity = media_get_entity_by_name(media, p + 1, end - p - 1);
360 entity_id = strtoul(p, &end, 10);
361 entity = media_get_entity_by_id(media, entity_id);
365 for (; isspace(*end); ++end);
369 for (p = end + 1; isspace(*p); ++p);
371 pad = strtoul(p, &end, 10);
372 for (p = end; isspace(*p); ++p);
374 if (pad >= entity->info.pads)
377 for (p = end; isspace(*p); ++p);
381 return &entity->pads[pad];
384 static struct media_link *parse_link(struct media_device *media, const char *p, char **endp)
386 struct media_link *link;
387 struct media_pad *source;
388 struct media_pad *sink;
392 source = parse_pad(media, p, &end);
396 if (end[0] != '-' || end[1] != '>')
400 sink = parse_pad(media, p, &end);
406 for (i = 0; i < source->entity->num_links; i++) {
407 link = &source->entity->links[i];
409 if (link->source == source && link->sink == sink)
416 static int setup_link(struct media_device *media, const char *p, char **endp)
418 struct media_link *link;
422 link = parse_link(media, p, &end);
424 printf("Unable to parse link\n");
430 printf("Unable to parse link flags\n");
434 flags = strtoul(p, &end, 10);
435 for (p = end; isspace(*p); p++);
437 printf("Unable to parse link flags\n");
441 for (; isspace(*p); p++);
444 printf("Setting up link %u:%u -> %u:%u [%u]\n",
445 link->source->entity->info.id, link->source->index,
446 link->sink->entity->info.id, link->sink->index,
449 return media_setup_link(media, link->source, link->sink, flags);
452 static int setup_links(struct media_device *media, const char *p)
458 ret = setup_link(media, p, &end);
463 } while (*end == ',');
465 return *end ? -EINVAL : 0;
468 /* -----------------------------------------------------------------------------
472 static int parse_format(struct v4l2_mbus_framefmt *format, const char *p, char **endp)
474 enum v4l2_mbus_pixelcode code;
475 unsigned int width, height;
478 for (; isspace(*p); ++p);
479 for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
481 code = string_to_pixelcode(p, end - p);
482 if (code == (enum v4l2_mbus_pixelcode)-1)
485 for (p = end; isspace(*p); ++p);
486 width = strtoul(p, &end, 10);
491 height = strtoul(p, &end, 10);
494 memset(format, 0, sizeof(*format));
495 format->width = width;
496 format->height = height;
502 static int parse_crop(struct v4l2_rect *crop, const char *p, char **endp)
509 crop->left = strtoul(p, &end, 10);
514 crop->top = strtoul(p, &end, 10);
521 crop->width = strtoul(p, &end, 10);
526 crop->height = strtoul(p, &end, 10);
532 static int parse_frame_interval(struct v4l2_fract *interval, const char *p, char **endp)
536 for (; isspace(*p); ++p);
538 interval->numerator = strtoul(p, &end, 10);
540 for (p = end; isspace(*p); ++p);
544 for (; isspace(*p); ++p);
545 interval->denominator = strtoul(p, &end, 10);
551 static struct media_pad *parse_pad_format(struct media_device *media,
552 struct v4l2_mbus_framefmt *format, struct v4l2_rect *crop,
553 struct v4l2_fract *interval, const char *p, char **endp)
555 struct media_pad *pad;
559 for (; isspace(*p); ++p);
561 pad = parse_pad(media, p, &end);
565 for (p = end; isspace(*p); ++p);
569 for (; isspace(*p); ++p);
572 ret = parse_format(format, p, &end);
576 for (p = end; isspace(*p); p++);
580 ret = parse_crop(crop, p, &end);
584 for (p = end; isspace(*p); p++);
588 ret = parse_frame_interval(interval, ++p, &end);
592 for (p = end; isspace(*p); p++);
598 *endp = (char *)p + 1;
602 static int set_format(struct media_pad *pad, struct v4l2_mbus_framefmt *format)
606 if (format->width == 0 || format->height == 0)
609 printf("Setting up format %s %ux%u on pad %s/%u\n",
610 pixelcode_to_string(format->code), format->width, format->height,
611 pad->entity->info.name, pad->index);
613 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
614 V4L2_SUBDEV_FORMAT_ACTIVE);
616 printf("Unable to set format: %s (%d)\n", strerror(-ret), ret);
620 printf("Format set: %s %ux%u\n",
621 pixelcode_to_string(format->code), format->width, format->height);
626 static int set_crop(struct media_pad *pad, struct v4l2_rect *crop)
630 if (crop->left == -1 || crop->top == -1)
633 printf("Setting up crop rectangle (%u,%u)/%ux%u on pad %s/%u\n",
634 crop->left, crop->top, crop->width, crop->height,
635 pad->entity->info.name, pad->index);
637 ret = v4l2_subdev_set_crop(pad->entity, crop, pad->index,
638 V4L2_SUBDEV_FORMAT_ACTIVE);
640 printf("Unable to set crop rectangle: %s (%d)\n", strerror(-ret), ret);
644 printf("Crop rectangle set: (%u,%u)/%ux%u\n",
645 crop->left, crop->top, crop->width, crop->height);
650 static int set_frame_interval(struct media_entity *entity, struct v4l2_fract *interval)
654 if (interval->numerator == 0)
657 printf("Setting up frame interval %u/%u on entity %s\n",
658 interval->numerator, interval->denominator, entity->info.name);
660 ret = v4l2_subdev_set_frame_interval(entity, interval);
662 printf("Unable to set frame interval: %s (%d)", strerror(-ret), ret);
666 printf("Frame interval set: %u/%u\n",
667 interval->numerator, interval->denominator);
673 static int setup_format(struct media_device *media, const char *p, char **endp)
675 struct v4l2_mbus_framefmt format = { 0, 0, 0 };
676 struct media_pad *pad;
677 struct v4l2_rect crop = { -1, -1, -1, -1 };
678 struct v4l2_fract interval = { 0, 0 };
683 pad = parse_pad_format(media, &format, &crop, &interval, p, &end);
685 printf("Unable to parse format\n");
689 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
690 ret = set_crop(pad, &crop);
695 ret = set_format(pad, &format);
699 if (pad->flags & MEDIA_PAD_FL_SINK) {
700 ret = set_crop(pad, &crop);
705 ret = set_frame_interval(pad->entity, &interval);
710 /* If the pad is an output pad, automatically set the same format on
711 * the remote subdev input pads, if any.
713 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
714 for (i = 0; i < pad->entity->num_links; ++i) {
715 struct media_link *link = &pad->entity->links[i];
716 struct v4l2_mbus_framefmt remote_format;
718 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
721 if (link->source == pad &&
722 link->sink->entity->info.type == MEDIA_ENT_T_V4L2_SUBDEV) {
723 remote_format = format;
724 set_format(link->sink, &remote_format);
733 static int setup_formats(struct media_device *media, const char *p)
739 ret = setup_format(media, p, &end);
744 } while (*end == ',');
746 return *end ? -EINVAL : 0;
749 int main(int argc, char **argv)
751 struct media_device *media;
754 if (parse_cmdline(argc, argv))
757 /* Open the media device and enumerate entities, pads and links. */
758 media = media_open(media_opts.devname, media_opts.verbose);
762 if (media_opts.entity) {
763 struct media_entity *entity;
765 entity = media_get_entity_by_name(media, media_opts.entity,
766 strlen(media_opts.entity));
767 if (entity == NULL) {
768 printf("Entity '%s' not found\n", media_opts.entity);
772 printf("%s\n", entity->devname);
775 if (media_opts.pad) {
776 struct media_pad *pad;
778 pad = parse_pad(media, media_opts.pad, NULL);
780 printf("Pad '%s' not found\n", media_opts.pad);
784 v4l2_subdev_print_format(pad->entity, pad->index,
785 V4L2_SUBDEV_FORMAT_ACTIVE);
789 if (media_opts.print || media_opts.print_dot) {
790 media_print_topology(media, media_opts.print_dot);
794 if (media_opts.reset) {
795 printf("Resetting all links to inactive\n");
796 media_reset_links(media);
799 if (media_opts.links)
800 setup_links(media, media_opts.links);
802 if (media_opts.formats)
803 setup_formats(media, media_opts.formats);
805 if (media_opts.interactive) {
810 printf("Enter a link to modify or enter to stop\n");
811 if (fgets(buffer, sizeof buffer, stdin) == NULL)
814 if (buffer[0] == '\n')
817 setup_link(media, buffer, &end);
827 return ret ? EXIT_FAILURE : EXIT_SUCCESS;