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>
41 #include "v4l2subdev.h"
44 /* -----------------------------------------------------------------------------
48 static void v4l2_subdev_print_format(struct media_entity *entity,
49 unsigned int pad, enum v4l2_subdev_format_whence which)
51 struct v4l2_mbus_framefmt format;
52 struct v4l2_rect rect;
55 ret = v4l2_subdev_get_format(entity, &format, pad, which);
59 printf("[%s %ux%u", v4l2_subdev_pixelcode_to_string(format.code),
60 format.width, format.height);
62 ret = v4l2_subdev_get_crop(entity, &rect, pad, which);
64 printf(" (%u,%u)/%ux%u", rect.left, rect.top,
65 rect.width, rect.height);
69 static const char *media_entity_type_to_string(unsigned type)
75 { MEDIA_ENT_T_DEVNODE, "Node" },
76 { MEDIA_ENT_T_V4L2_SUBDEV, "V4L2 subdev" },
81 type &= MEDIA_ENT_TYPE_MASK;
83 for (i = 0; i < ARRAY_SIZE(types); i++) {
84 if (types[i].type == type)
91 static const char *media_entity_subtype_to_string(unsigned type)
93 static const char *node_types[] = {
100 static const char *subdev_types[] = {
107 unsigned int subtype = type & MEDIA_ENT_SUBTYPE_MASK;
109 switch (type & MEDIA_ENT_TYPE_MASK) {
110 case MEDIA_ENT_T_DEVNODE:
111 if (subtype >= ARRAY_SIZE(node_types))
113 return node_types[subtype];
115 case MEDIA_ENT_T_V4L2_SUBDEV:
116 if (subtype >= ARRAY_SIZE(subdev_types))
118 return subdev_types[subtype];
120 return node_types[0];
124 static const char *media_pad_type_to_string(unsigned flag)
126 static const struct {
130 { MEDIA_PAD_FL_SINK, "Input" },
131 { MEDIA_PAD_FL_SOURCE, "Output" },
136 for (i = 0; i < ARRAY_SIZE(flags); i++) {
137 if (flags[i].flag & flag)
138 return flags[i].name;
144 static void media_print_topology_dot(struct media_device *media)
148 printf("digraph board {\n");
149 printf("\trankdir=TB\n");
151 for (i = 0; i < media->entities_count; ++i) {
152 struct media_entity *entity = &media->entities[i];
155 switch (media_entity_type(entity)) {
156 case MEDIA_ENT_T_DEVNODE:
157 printf("\tn%08x [label=\"%s\\n%s\", shape=box, style=filled, "
158 "fillcolor=yellow]\n",
159 entity->info.id, entity->info.name, entity->devname);
162 case MEDIA_ENT_T_V4L2_SUBDEV:
163 printf("\tn%08x [label=\"{{", entity->info.id);
165 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
166 if (!(entity->pads[j].flags & MEDIA_PAD_FL_SINK))
169 printf("%s<port%u> %u", npads ? " | " : "", j, j);
173 printf("} | %s", entity->info.name);
175 printf("\\n%s", entity->devname);
178 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
179 if (!(entity->pads[j].flags & MEDIA_PAD_FL_SOURCE))
182 printf("%s<port%u> %u", npads ? " | " : "", j, j);
186 printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n");
193 for (j = 0; j < entity->num_links; j++) {
194 struct media_link *link = &entity->links[j];
196 if (link->source->entity != entity)
199 printf("\tn%08x", link->source->entity->info.id);
200 if (media_entity_type(link->source->entity) == MEDIA_ENT_T_V4L2_SUBDEV)
201 printf(":port%u", link->source->index);
203 printf("n%08x", link->sink->entity->info.id);
204 if (media_entity_type(link->sink->entity) == MEDIA_ENT_T_V4L2_SUBDEV)
205 printf(":port%u", link->sink->index);
207 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
208 printf(" [style=bold]");
209 else if (!(link->flags & MEDIA_LNK_FL_ENABLED))
210 printf(" [style=dashed]");
218 static void media_print_topology_text(struct media_device *media)
220 unsigned int i, j, k;
221 unsigned int padding;
223 printf("Device topology\n");
225 for (i = 0; i < media->entities_count; ++i) {
226 struct media_entity *entity = &media->entities[i];
228 padding = printf("- entity %u: ", entity->info.id);
229 printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
230 entity->info.pads, entity->info.pads > 1 ? "s" : "",
231 entity->num_links, entity->num_links > 1 ? "s" : "");
232 printf("%*ctype %s subtype %s\n", padding, ' ',
233 media_entity_type_to_string(entity->info.type),
234 media_entity_subtype_to_string(entity->info.type));
235 if (entity->devname[0])
236 printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
238 for (j = 0; j < entity->info.pads; j++) {
239 struct media_pad *pad = &entity->pads[j];
241 printf("\tpad%u: %s ", j, media_pad_type_to_string(pad->flags));
243 if (media_entity_type(entity) == MEDIA_ENT_T_V4L2_SUBDEV)
244 v4l2_subdev_print_format(entity, j, V4L2_SUBDEV_FORMAT_ACTIVE);
248 for (k = 0; k < entity->num_links; k++) {
249 struct media_link *link = &entity->links[k];
250 struct media_pad *source = link->source;
251 struct media_pad *sink = link->sink;
253 if (source->entity == entity && source->index == j)
254 printf("\t\t-> '%s':pad%u [",
255 sink->entity->info.name, sink->index);
256 else if (sink->entity == entity && sink->index == j)
257 printf("\t\t<- '%s':pad%u [",
258 source->entity->info.name, source->index);
262 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
263 printf("IMMUTABLE,");
264 if (link->flags & MEDIA_LNK_FL_ENABLED)
274 void media_print_topology(struct media_device *media, int dot)
277 media_print_topology_dot(media);
279 media_print_topology_text(media);
282 int main(int argc, char **argv)
284 struct media_device *media;
287 if (parse_cmdline(argc, argv))
290 /* Open the media device and enumerate entities, pads and links. */
291 media = media_open_debug(media_opts.devname, media_opts.verbose,
292 (void (*)(void *, ...))fprintf, stdout);
296 if (media_opts.entity) {
297 struct media_entity *entity;
299 entity = media_get_entity_by_name(media, media_opts.entity,
300 strlen(media_opts.entity));
301 if (entity == NULL) {
302 printf("Entity '%s' not found\n", media_opts.entity);
306 printf("%s\n", entity->devname);
309 if (media_opts.pad) {
310 struct media_pad *pad;
312 pad = media_parse_pad(media, media_opts.pad, NULL);
314 printf("Pad '%s' not found\n", media_opts.pad);
318 v4l2_subdev_print_format(pad->entity, pad->index,
319 V4L2_SUBDEV_FORMAT_ACTIVE);
323 if (media_opts.print || media_opts.print_dot) {
324 media_print_topology(media, media_opts.print_dot);
328 if (media_opts.reset) {
329 printf("Resetting all links to inactive\n");
330 media_reset_links(media);
333 if (media_opts.links)
334 media_parse_setup_links(media, media_opts.links);
336 if (media_opts.formats)
337 v4l2_subdev_parse_setup_formats(media, media_opts.formats);
339 if (media_opts.interactive) {
344 printf("Enter a link to modify or enter to stop\n");
345 if (fgets(buffer, sizeof buffer, stdin) == NULL)
348 if (buffer[0] == '\n')
351 media_parse_setup_link(media, buffer, &end);
361 return ret ? EXIT_FAILURE : EXIT_SUCCESS;