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.,
20 #include <sys/ioctl.h>
22 #include <sys/types.h>
31 #include <linux/videodev2.h>
32 #include <linux/media.h>
38 static unsigned int media_entity_type(struct media_entity *entity)
40 return entity->info.type & MEDIA_ENTITY_TYPE_MASK;
43 static const char *media_entity_type_to_string(unsigned type)
49 { MEDIA_ENTITY_TYPE_NODE, "Node" },
50 { MEDIA_ENTITY_TYPE_SUBDEV, "V4L2 subdev" },
55 type &= MEDIA_ENTITY_TYPE_MASK;
57 for (i = 0; i < ARRAY_SIZE(types); i++) {
58 if (types[i].type == type)
65 static const char *media_entity_subtype_to_string(unsigned type)
67 static const char *node_types[] = {
74 static const char *subdev_types[] = {
81 unsigned int subtype = type & MEDIA_ENTITY_SUBTYPE_MASK;
83 switch (type & MEDIA_ENTITY_TYPE_MASK) {
84 case MEDIA_ENTITY_TYPE_NODE:
85 if (subtype >= ARRAY_SIZE(node_types))
87 return node_types[subtype];
89 case MEDIA_ENTITY_TYPE_SUBDEV:
90 if (subtype >= ARRAY_SIZE(subdev_types))
92 return subdev_types[subtype];
98 static const char *media_pad_type_to_string(unsigned flag)
100 static const struct {
104 { MEDIA_PAD_FLAG_INPUT, "Input" },
105 { MEDIA_PAD_FLAG_OUTPUT, "Output" },
110 for (i = 0; i < ARRAY_SIZE(flags); i++) {
111 if (flags[i].flag & flag)
112 return flags[i].name;
119 * media_entity_remote_pad -
121 struct media_entity_pad *media_entity_remote_pad(struct media_entity_pad *pad)
125 for (i = 0; i < pad->entity->info.links; ++i) {
126 struct media_entity_link *link = &pad->entity->links[i];
128 if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
131 if (link->source == pad)
134 if (link->sink == pad)
142 * media_get_entity_by_name -
144 struct media_entity *media_get_entity_by_name(struct media_device *media,
145 const char *name, size_t length)
149 for (i = 0; i < media->entities_count; ++i) {
150 struct media_entity *entity = &media->entities[i];
152 if (strncmp(entity->info.name, name, length) == 0)
160 * media_get_entity_by_id -
162 struct media_entity *media_get_entity_by_id(struct media_device *media,
167 for (i = 0; i < media->entities_count; ++i) {
168 struct media_entity *entity = &media->entities[i];
170 if (entity->info.id == id)
180 int media_setup_link(struct media_device *media,
181 struct media_entity_pad *source,
182 struct media_entity_pad *sink,
185 struct media_entity_link *link;
186 struct media_link_desc ulink;
190 for (i = 0; i < source->entity->info.links; i++) {
191 link = &source->entity->links[i];
193 if (link->source->entity == source->entity &&
194 link->source->index == source->index &&
195 link->sink->entity == sink->entity &&
196 link->sink->index == sink->index)
200 if (i == source->entity->info.links) {
201 printf("%s: Link not found\n", __func__);
206 ulink.source.entity = source->entity->info.id;
207 ulink.source.index = source->index;
208 ulink.source.flags = MEDIA_PAD_FLAG_OUTPUT;
211 ulink.sink.entity = sink->entity->info.id;
212 ulink.sink.index = sink->index;
213 ulink.sink.flags = MEDIA_PAD_FLAG_INPUT;
215 ulink.flags = flags | (link->flags & MEDIA_LINK_FLAG_IMMUTABLE);
217 ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
219 printf("%s: Unable to setup link (%s)\n", __func__,
228 int media_reset_links(struct media_device *media)
233 for (i = 0; i < media->entities_count; ++i) {
234 struct media_entity *entity = &media->entities[i];
236 for (j = 0; j < entity->info.links; j++) {
237 struct media_entity_link *link = &entity->links[j];
239 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
242 ret = media_setup_link(media, link->source, link->sink,
243 link->flags & ~MEDIA_LINK_FLAG_ACTIVE);
252 static void media_print_topology_dot(struct media_device *media)
256 printf("digraph board {\n");
257 printf("\trankdir=TB\n");
259 for (i = 0; i < media->entities_count; ++i) {
260 struct media_entity *entity = &media->entities[i];
263 switch (media_entity_type(entity)) {
264 case MEDIA_ENTITY_TYPE_NODE:
265 printf("\tn%08x [label=\"%s\\n%s\", shape=box, style=filled, "
266 "fillcolor=yellow]\n",
267 entity->info.id, entity->info.name, entity->devname);
270 case MEDIA_ENTITY_TYPE_SUBDEV:
271 printf("\tn%08x [label=\"{{", entity->info.id);
273 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
274 if (!(entity->pads[j].flags & MEDIA_PAD_FLAG_INPUT))
277 printf("%s<port%u> %u", npads ? " | " : "", j, j);
281 printf("} | %s", entity->info.name);
283 printf("\\n%s", entity->devname);
286 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
287 if (!(entity->pads[j].flags & MEDIA_PAD_FLAG_OUTPUT))
290 printf("%s<port%u> %u", npads ? " | " : "", j, j);
294 printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n");
301 for (j = 0; j < entity->info.links; j++) {
302 struct media_entity_link *link = &entity->links[j];
304 if (link->source->entity != entity)
307 printf("\tn%08x", link->source->entity->info.id);
308 if (media_entity_type(link->source->entity) == MEDIA_ENTITY_TYPE_SUBDEV)
309 printf(":port%u", link->source->index);
311 printf("n%08x", link->sink->entity->info.id);
312 if (media_entity_type(link->sink->entity) == MEDIA_ENTITY_TYPE_SUBDEV)
313 printf(":port%u", link->sink->index);
315 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
316 printf(" [style=bold]");
317 else if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
318 printf(" [style=dashed]");
326 static void media_print_topology_text(struct media_device *media)
328 unsigned int i, j, k;
329 unsigned int padding;
331 printf("Device topology\n");
333 for (i = 0; i < media->entities_count; ++i) {
334 struct media_entity *entity = &media->entities[i];
336 padding = printf("- entity %u: ", entity->info.id);
337 printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
338 entity->info.pads, entity->info.pads > 1 ? "s" : "",
339 entity->info.links, entity->info.links > 1 ? "s" : "");
340 printf("%*ctype %s subtype %s\n", padding, ' ',
341 media_entity_type_to_string(entity->info.type),
342 media_entity_subtype_to_string(entity->info.type));
343 if (entity->devname[0])
344 printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
346 for (j = 0; j < entity->info.pads; j++) {
347 struct media_entity_pad *pad = &entity->pads[j];
349 printf("\tpad%u: %s ", j, media_pad_type_to_string(pad->flags));
351 if (media_entity_type(entity) == MEDIA_ENTITY_TYPE_SUBDEV)
352 v4l2_subdev_print_format(entity, j, V4L2_SUBDEV_FORMAT_ACTIVE);
356 for (k = 0; k < entity->info.links; k++) {
357 struct media_entity_link *link = &entity->links[k];
359 if (link->source->entity != entity ||
360 link->source->index != j)
363 printf("\t\t-> '%s':pad%u [",
364 link->sink->entity->info.name, link->sink->index);
366 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
367 printf("IMMUTABLE,");
368 if (link->flags & MEDIA_LINK_FLAG_ACTIVE)
378 void media_print_topology(struct media_device *media, int dot)
381 media_print_topology_dot(media);
383 media_print_topology_text(media);
386 static int media_enum_links(struct media_device *media)
391 for (id = 1; id <= media->entities_count; id++) {
392 struct media_entity *entity = &media->entities[id - 1];
393 struct media_links_enum links;
396 links.entity = entity->info.id;
397 links.pads = malloc(entity->info.pads * sizeof(struct media_pad_desc));
398 links.links = malloc(entity->info.links * sizeof(struct media_link_desc));
400 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
401 printf("%s: Unable to enumerate pads and links (%s).\n",
402 __func__, strerror(errno));
408 for (i = 0; i < entity->info.pads; ++i) {
409 entity->pads[i].entity = entity;
410 entity->pads[i].index = links.pads[i].index;
411 entity->pads[i].flags = links.pads[i].flags;
414 for (i = 0; i < entity->info.links; ++i) {
415 struct media_link_desc *link = &links.links[i];
416 struct media_entity *source;
417 struct media_entity *sink;
419 source = media_get_entity_by_id(media, link->source.entity);
420 sink = media_get_entity_by_id(media, link->sink.entity);
422 if (source == NULL || sink == NULL) {
423 printf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
424 id, i, link->source.entity, link->source.index,
425 link->sink.entity, link->sink.index);
429 entity->links[i].source = &source->pads[link->source.index];
430 entity->links[i].sink = &sink->pads[link->sink.index];
431 entity->links[i].flags = links.links[i].flags;
441 static int media_enum_entities(struct media_device *media)
443 struct media_entity *entity;
453 for (id = 0; ; id = entity->info.id) {
454 size = (media->entities_count + 1) * sizeof(*media->entities);
455 media->entities = realloc(media->entities, size);
457 entity = &media->entities[media->entities_count];
458 memset(entity, 0, sizeof(*entity));
460 entity->info.id = id | MEDIA_ENTITY_ID_FLAG_NEXT;
462 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
469 entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
470 entity->links = malloc(entity->info.links * sizeof(*entity->links));
471 if (entity->pads == NULL || entity->links == NULL)
474 media->entities_count++;
476 /* Find the corresponding device name. */
477 if (media_entity_type(entity) != MEDIA_ENTITY_TYPE_NODE &&
478 media_entity_type(entity) != MEDIA_ENTITY_TYPE_SUBDEV)
481 sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
482 entity->info.v4l.minor);
483 ret = readlink(sysname, target, sizeof(target));
488 p = strrchr(target, '/');
492 sprintf(devname, "/dev/%s", p + 1);
493 ret = stat(devname, &devstat);
497 /* Sanity check: udev might have reordered the device nodes.
498 * Make sure the major/minor match. We should really use
501 if (major(devstat.st_rdev) == entity->info.v4l.major &&
502 minor(devstat.st_rdev) == entity->info.v4l.minor)
503 strcpy(entity->devname, devname);
512 struct media_device *media_open(const char *name, int verbose)
514 struct media_device *media;
517 media = malloc(sizeof(*media));
519 printf("%s: unable to allocate memory\n", __func__);
522 memset(media, 0, sizeof(*media));
525 printf("Opening media device %s\n", name);
526 media->fd = open(name, O_RDWR);
529 printf("%s: Can't open media device %s\n", __func__, name);
534 printf("Enumerating entities\n");
536 ret = media_enum_entities(media);
538 printf("%s: Unable to enumerate entities for device %s (%s)\n",
539 __func__, name, strerror(-ret));
545 printf("Found %u entities\n", media->entities_count);
546 printf("Enumerating pads and links\n");
549 ret = media_enum_links(media);
551 printf("%s: Unable to enumerate pads and linksfor device %s\n",
563 void media_close(struct media_device *media)
570 for (i = 0; i < media->entities_count; ++i) {
571 struct media_entity *entity = &media->entities[i];
575 if (entity->fd != -1)
579 free(media->entities);