2 * Media controller test application
4 * Copyright (C) 2010-2011 Ideas on board SPRL
6 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/ioctl.h>
36 #include <linux/media.h>
37 #include <linux/types.h>
38 #include <linux/v4l2-mediabus.h>
39 #include <linux/v4l2-subdev.h>
40 #include <linux/videodev2.h>
45 #include "v4l2subdev.h"
47 /* -----------------------------------------------------------------------------
51 static void v4l2_subdev_print_format(struct media_entity *entity,
52 unsigned int pad, enum v4l2_subdev_format_whence which)
54 struct v4l2_mbus_framefmt format;
55 struct v4l2_rect rect;
58 ret = v4l2_subdev_get_format(entity, &format, pad, which);
62 printf("\t\t[fmt:%s/%ux%u",
63 v4l2_subdev_pixelcode_to_string(format.code),
64 format.width, format.height);
66 ret = v4l2_subdev_get_selection(entity, &rect, pad,
67 V4L2_SEL_TGT_CROP_BOUNDS,
70 printf("\n\t\t crop.bounds:(%u,%u)/%ux%u", rect.left, rect.top,
71 rect.width, rect.height);
73 ret = v4l2_subdev_get_selection(entity, &rect, pad,
77 printf("\n\t\t crop:(%u,%u)/%ux%u", rect.left, rect.top,
78 rect.width, rect.height);
80 ret = v4l2_subdev_get_selection(entity, &rect, pad,
81 V4L2_SEL_TGT_COMPOSE_BOUNDS,
84 printf("\n\t\t compose.bounds:(%u,%u)/%ux%u",
85 rect.left, rect.top, rect.width, rect.height);
87 ret = v4l2_subdev_get_selection(entity, &rect, pad,
91 printf("\n\t\t compose:(%u,%u)/%ux%u",
92 rect.left, rect.top, rect.width, rect.height);
97 static const char *media_entity_type_to_string(unsigned type)
103 { MEDIA_ENT_T_DEVNODE, "Node" },
104 { MEDIA_ENT_T_V4L2_SUBDEV, "V4L2 subdev" },
109 type &= MEDIA_ENT_TYPE_MASK;
111 for (i = 0; i < ARRAY_SIZE(types); i++) {
112 if (types[i].type == type)
113 return types[i].name;
119 static const char *media_entity_subtype_to_string(unsigned type)
121 static const char *node_types[] = {
128 static const char *subdev_types[] = {
135 unsigned int subtype = type & MEDIA_ENT_SUBTYPE_MASK;
137 switch (type & MEDIA_ENT_TYPE_MASK) {
138 case MEDIA_ENT_T_DEVNODE:
139 if (subtype >= ARRAY_SIZE(node_types))
141 return node_types[subtype];
143 case MEDIA_ENT_T_V4L2_SUBDEV:
144 if (subtype >= ARRAY_SIZE(subdev_types))
146 return subdev_types[subtype];
148 return node_types[0];
152 static const char *media_pad_type_to_string(unsigned flag)
154 static const struct {
158 { MEDIA_PAD_FL_SINK, "Sink" },
159 { MEDIA_PAD_FL_SOURCE, "Source" },
164 for (i = 0; i < ARRAY_SIZE(flags); i++) {
165 if (flags[i].flag & flag)
166 return flags[i].name;
172 static void media_print_topology_dot(struct media_device *media)
176 printf("digraph board {\n");
177 printf("\trankdir=TB\n");
179 for (i = 0; i < media->entities_count; ++i) {
180 struct media_entity *entity = &media->entities[i];
183 switch (media_entity_type(entity)) {
184 case MEDIA_ENT_T_DEVNODE:
185 printf("\tn%08x [label=\"%s\\n%s\", shape=box, style=filled, "
186 "fillcolor=yellow]\n",
187 entity->info.id, entity->info.name, entity->devname);
190 case MEDIA_ENT_T_V4L2_SUBDEV:
191 printf("\tn%08x [label=\"{{", entity->info.id);
193 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
194 if (!(entity->pads[j].flags & MEDIA_PAD_FL_SINK))
197 printf("%s<port%u> %u", npads ? " | " : "", j, j);
201 printf("} | %s", entity->info.name);
203 printf("\\n%s", entity->devname);
206 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
207 if (!(entity->pads[j].flags & MEDIA_PAD_FL_SOURCE))
210 printf("%s<port%u> %u", npads ? " | " : "", j, j);
214 printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n");
221 for (j = 0; j < entity->num_links; j++) {
222 struct media_link *link = &entity->links[j];
224 if (link->source->entity != entity)
227 printf("\tn%08x", link->source->entity->info.id);
228 if (media_entity_type(link->source->entity) == MEDIA_ENT_T_V4L2_SUBDEV)
229 printf(":port%u", link->source->index);
231 printf("n%08x", link->sink->entity->info.id);
232 if (media_entity_type(link->sink->entity) == MEDIA_ENT_T_V4L2_SUBDEV)
233 printf(":port%u", link->sink->index);
235 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
236 printf(" [style=bold]");
237 else if (!(link->flags & MEDIA_LNK_FL_ENABLED))
238 printf(" [style=dashed]");
246 static void media_print_topology_text(struct media_device *media)
248 static const struct {
252 { MEDIA_LNK_FL_ENABLED, "ENABLED" },
253 { MEDIA_LNK_FL_IMMUTABLE, "IMMUTABLE" },
254 { MEDIA_LNK_FL_DYNAMIC, "DYNAMIC" },
257 unsigned int i, j, k;
258 unsigned int padding;
260 printf("Device topology\n");
262 for (i = 0; i < media->entities_count; ++i) {
263 struct media_entity *entity = &media->entities[i];
265 padding = printf("- entity %u: ", entity->info.id);
266 printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
267 entity->info.pads, entity->info.pads > 1 ? "s" : "",
268 entity->num_links, entity->num_links > 1 ? "s" : "");
269 printf("%*ctype %s subtype %s flags %x\n", padding, ' ',
270 media_entity_type_to_string(entity->info.type),
271 media_entity_subtype_to_string(entity->info.type),
273 if (entity->devname[0])
274 printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
276 for (j = 0; j < entity->info.pads; j++) {
277 struct media_pad *pad = &entity->pads[j];
279 printf("\tpad%u: %s\n", j, media_pad_type_to_string(pad->flags));
281 if (media_entity_type(entity) == MEDIA_ENT_T_V4L2_SUBDEV)
282 v4l2_subdev_print_format(entity, j, V4L2_SUBDEV_FORMAT_ACTIVE);
284 for (k = 0; k < entity->num_links; k++) {
285 struct media_link *link = &entity->links[k];
286 struct media_pad *source = link->source;
287 struct media_pad *sink = link->sink;
291 if (source->entity == entity && source->index == j)
292 printf("\t\t-> \"%s\":%u [",
293 sink->entity->info.name, sink->index);
294 else if (sink->entity == entity && sink->index == j)
295 printf("\t\t<- \"%s\":%u [",
296 source->entity->info.name, source->index);
300 for (i = 0; i < ARRAY_SIZE(link_flags); i++) {
301 if (!(link->flags & link_flags[i].flag))
305 printf("%s", link_flags[i].name);
316 void media_print_topology(struct media_device *media, int dot)
319 media_print_topology_dot(media);
321 media_print_topology_text(media);
324 int main(int argc, char **argv)
326 struct media_device *media;
329 if (parse_cmdline(argc, argv))
332 /* Open the media device and enumerate entities, pads and links. */
333 if (media_opts.verbose)
334 media = media_open_debug(
336 (void (*)(void *, ...))fprintf, stdout);
338 media = media_open(media_opts.devname);
340 printf("Failed to open %s\n", media_opts.devname);
344 if (media_opts.print) {
345 printf("Media controller API version %u.%u.%u\n\n",
346 (media->info.media_version << 16) & 0xff,
347 (media->info.media_version << 8) & 0xff,
348 (media->info.media_version << 0) & 0xff);
349 printf("Media device information\n"
350 "------------------------\n"
356 "driver version %u.%u.%u\n\n",
357 media->info.driver, media->info.model,
358 media->info.serial, media->info.bus_info,
359 media->info.hw_revision,
360 (media->info.driver_version << 16) & 0xff,
361 (media->info.driver_version << 8) & 0xff,
362 (media->info.driver_version << 0) & 0xff);
365 if (media_opts.entity) {
366 struct media_entity *entity;
368 entity = media_get_entity_by_name(media, media_opts.entity,
369 strlen(media_opts.entity));
370 if (entity == NULL) {
371 printf("Entity '%s' not found\n", media_opts.entity);
375 printf("%s\n", entity->devname);
378 if (media_opts.pad) {
379 struct media_pad *pad;
381 pad = media_parse_pad(media, media_opts.pad, NULL);
383 printf("Pad '%s' not found\n", media_opts.pad);
387 v4l2_subdev_print_format(pad->entity, pad->index,
388 V4L2_SUBDEV_FORMAT_ACTIVE);
391 if (media_opts.print || media_opts.print_dot) {
392 media_print_topology(media, media_opts.print_dot);
396 if (media_opts.reset) {
397 if (media_opts.verbose)
398 printf("Resetting all links to inactive\n");
399 ret = media_reset_links(media);
401 printf("Unable to reset links: %s (%d)\n",
402 strerror(-ret), -ret);
407 if (media_opts.links) {
408 ret = media_parse_setup_links(media, media_opts.links);
410 printf("Unable to parse link: %s (%d)\n",
411 strerror(-ret), -ret);
416 if (media_opts.formats) {
417 ret = v4l2_subdev_parse_setup_formats(media,
420 printf("Unable to parse format: %s (%d)\n",
421 strerror(-ret), -ret);
426 if (media_opts.interactive) {
431 printf("Enter a link to modify or enter to stop\n");
432 if (fgets(buffer, sizeof buffer, stdin) == NULL)
435 if (buffer[0] == '\n')
438 ret = media_parse_setup_link(media, buffer, &end);
440 printf("Unable to parse link: %s (%d)\n",
441 strerror(-ret), -ret);
451 return ret ? EXIT_FAILURE : EXIT_SUCCESS;