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 const char *media_entity_type_to_string(unsigned type)
44 { MEDIA_ENTITY_TYPE_NODE, "Node" },
45 { MEDIA_ENTITY_TYPE_SUBDEV, "V4L2 subdev" },
50 type &= MEDIA_ENTITY_TYPE_MASK;
52 for (i = 0; i < ARRAY_SIZE(types); i++) {
53 if (types[i].type == type)
60 static const char *media_entity_subtype_to_string(unsigned type)
62 static const char *node_types[] = {
69 static const char *subdev_types[] = {
76 unsigned int subtype = type & MEDIA_ENTITY_SUBTYPE_MASK;
78 switch (type & MEDIA_ENTITY_TYPE_MASK) {
79 case MEDIA_ENTITY_TYPE_NODE:
80 if (subtype >= ARRAY_SIZE(node_types))
82 return node_types[subtype];
84 case MEDIA_ENTITY_TYPE_SUBDEV:
85 if (subtype >= ARRAY_SIZE(subdev_types))
87 return subdev_types[subtype];
93 static const char *media_pad_type_to_string(unsigned flag)
99 { MEDIA_PAD_FLAG_INPUT, "Input" },
100 { MEDIA_PAD_FLAG_OUTPUT, "Output" },
105 for (i = 0; i < ARRAY_SIZE(flags); i++) {
106 if (flags[i].flag & flag)
107 return flags[i].name;
114 * media_entity_remote_pad -
116 struct media_entity_pad *media_entity_remote_pad(struct media_entity_pad *pad)
120 for (i = 0; i < pad->entity->info.links; ++i) {
121 struct media_entity_link *link = &pad->entity->links[i];
123 if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
126 if (link->source == pad)
129 if (link->sink == pad)
137 * media_get_entity_by_name -
139 struct media_entity *media_get_entity_by_name(struct media_device *media,
140 const char *name, size_t length)
144 for (i = 0; i < media->entities_count; ++i) {
145 struct media_entity *entity = &media->entities[i];
147 if (strncmp(entity->info.name, name, length) == 0)
155 * media_get_entity_by_id -
157 struct media_entity *media_get_entity_by_id(struct media_device *media,
162 for (i = 0; i < media->entities_count; ++i) {
163 struct media_entity *entity = &media->entities[i];
165 if (entity->info.id == id)
175 int media_setup_link(struct media_device *media,
176 struct media_entity_pad *source,
177 struct media_entity_pad *sink,
180 struct media_entity_link *link;
181 struct media_link_desc ulink;
185 for (i = 0; i < source->entity->info.links; i++) {
186 link = &source->entity->links[i];
188 if (link->source->entity == source->entity &&
189 link->source->index == source->index &&
190 link->sink->entity == sink->entity &&
191 link->sink->index == sink->index)
195 if (i == source->entity->info.links) {
196 printf("%s: Link not found\n", __func__);
201 ulink.source.entity = source->entity->info.id;
202 ulink.source.index = source->index;
203 ulink.source.flags = MEDIA_PAD_FLAG_OUTPUT;
206 ulink.sink.entity = sink->entity->info.id;
207 ulink.sink.index = sink->index;
208 ulink.sink.flags = MEDIA_PAD_FLAG_INPUT;
210 ulink.flags = flags | (link->flags & MEDIA_LINK_FLAG_IMMUTABLE);
212 ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
214 printf("%s: Unable to setup link (%s)\n", __func__,
223 int media_reset_links(struct media_device *media)
228 for (i = 0; i < media->entities_count; ++i) {
229 struct media_entity *entity = &media->entities[i];
231 for (j = 0; j < entity->info.links; j++) {
232 struct media_entity_link *link = &entity->links[j];
234 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
237 ret = media_setup_link(media, link->source, link->sink,
238 link->flags & ~MEDIA_LINK_FLAG_ACTIVE);
247 static void media_print_topology_dot(struct media_device *media)
251 printf("digraph board {\n");
252 printf("\trankdir=TB\n");
254 for (i = 0; i < media->entities_count; ++i) {
255 struct media_entity *entity = &media->entities[i];
258 switch (media_entity_type(entity)) {
259 case MEDIA_ENTITY_TYPE_NODE:
260 printf("\tn%08x [label=\"%s\\n%s\", shape=box, style=filled, "
261 "fillcolor=yellow]\n",
262 entity->info.id, entity->info.name, entity->devname);
265 case MEDIA_ENTITY_TYPE_SUBDEV:
266 printf("\tn%08x [label=\"{{", entity->info.id);
268 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
269 if (!(entity->pads[j].flags & MEDIA_PAD_FLAG_INPUT))
272 printf("%s<port%u> %u", npads ? " | " : "", j, j);
276 printf("} | %s", entity->info.name);
278 printf("\\n%s", entity->devname);
281 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
282 if (!(entity->pads[j].flags & MEDIA_PAD_FLAG_OUTPUT))
285 printf("%s<port%u> %u", npads ? " | " : "", j, j);
289 printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n");
296 for (j = 0; j < entity->info.links; j++) {
297 struct media_entity_link *link = &entity->links[j];
299 if (link->source->entity != entity)
302 printf("\tn%08x", link->source->entity->info.id);
303 if (media_entity_type(link->source->entity) == MEDIA_ENTITY_TYPE_SUBDEV)
304 printf(":port%u", link->source->index);
306 printf("n%08x", link->sink->entity->info.id);
307 if (media_entity_type(link->sink->entity) == MEDIA_ENTITY_TYPE_SUBDEV)
308 printf(":port%u", link->sink->index);
310 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
311 printf(" [style=bold]");
312 else if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
313 printf(" [style=dashed]");
321 static void media_print_topology_text(struct media_device *media)
323 unsigned int i, j, k;
324 unsigned int padding;
326 printf("Device topology\n");
328 for (i = 0; i < media->entities_count; ++i) {
329 struct media_entity *entity = &media->entities[i];
331 padding = printf("- entity %u: ", entity->info.id);
332 printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
333 entity->info.pads, entity->info.pads > 1 ? "s" : "",
334 entity->info.links, entity->info.links > 1 ? "s" : "");
335 printf("%*ctype %s subtype %s\n", padding, ' ',
336 media_entity_type_to_string(entity->info.type),
337 media_entity_subtype_to_string(entity->info.type));
338 if (entity->devname[0])
339 printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
341 for (j = 0; j < entity->info.pads; j++) {
342 struct media_entity_pad *pad = &entity->pads[j];
344 printf("\tpad%u: %s ", j, media_pad_type_to_string(pad->flags));
346 if (media_entity_type(entity) == MEDIA_ENTITY_TYPE_SUBDEV)
347 v4l2_subdev_print_format(entity, j, V4L2_SUBDEV_FORMAT_ACTIVE);
351 for (k = 0; k < entity->info.links; k++) {
352 struct media_entity_link *link = &entity->links[k];
354 if (link->source->entity != entity ||
355 link->source->index != j)
358 printf("\t\t-> '%s':pad%u [",
359 link->sink->entity->info.name, link->sink->index);
361 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
362 printf("IMMUTABLE,");
363 if (link->flags & MEDIA_LINK_FLAG_ACTIVE)
373 void media_print_topology(struct media_device *media, int dot)
376 media_print_topology_dot(media);
378 media_print_topology_text(media);
381 static int media_enum_links(struct media_device *media)
386 for (id = 1; id <= media->entities_count; id++) {
387 struct media_entity *entity = &media->entities[id - 1];
388 struct media_links_enum links;
391 links.entity = entity->info.id;
392 links.pads = malloc(entity->info.pads * sizeof(struct media_pad_desc));
393 links.links = malloc(entity->info.links * sizeof(struct media_link_desc));
395 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
396 printf("%s: Unable to enumerate pads and links (%s).\n",
397 __func__, strerror(errno));
403 for (i = 0; i < entity->info.pads; ++i) {
404 entity->pads[i].entity = entity;
405 entity->pads[i].index = links.pads[i].index;
406 entity->pads[i].flags = links.pads[i].flags;
409 for (i = 0; i < entity->info.links; ++i) {
410 struct media_link_desc *link = &links.links[i];
411 struct media_entity *source;
412 struct media_entity *sink;
414 source = media_get_entity_by_id(media, link->source.entity);
415 sink = media_get_entity_by_id(media, link->sink.entity);
417 if (source == NULL || sink == NULL) {
418 printf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
419 id, i, link->source.entity, link->source.index,
420 link->sink.entity, link->sink.index);
423 entity->links[i].source = &source->pads[link->source.index];
424 entity->links[i].sink = &sink->pads[link->sink.index];
425 entity->links[i].flags = links.links[i].flags;
436 static int media_enum_entities(struct media_device *media)
438 struct media_entity *entity;
448 for (id = 0; ; id = entity->info.id) {
449 size = (media->entities_count + 1) * sizeof(*media->entities);
450 media->entities = realloc(media->entities, size);
452 entity = &media->entities[media->entities_count];
453 memset(entity, 0, sizeof(*entity));
455 entity->info.id = id | MEDIA_ENTITY_ID_FLAG_NEXT;
457 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
464 entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
465 entity->links = malloc(entity->info.links * sizeof(*entity->links));
466 if (entity->pads == NULL || entity->links == NULL)
469 media->entities_count++;
471 /* Find the corresponding device name. */
472 if (media_entity_type(entity) != MEDIA_ENTITY_TYPE_NODE &&
473 media_entity_type(entity) != MEDIA_ENTITY_TYPE_SUBDEV)
476 sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
477 entity->info.v4l.minor);
478 ret = readlink(sysname, target, sizeof(target));
483 p = strrchr(target, '/');
487 sprintf(devname, "/dev/%s", p + 1);
488 ret = stat(devname, &devstat);
492 /* Sanity check: udev might have reordered the device nodes.
493 * Make sure the major/minor match. We should really use
496 if (major(devstat.st_rdev) == entity->info.v4l.major &&
497 minor(devstat.st_rdev) == entity->info.v4l.minor)
498 strcpy(entity->devname, devname);
507 struct media_device *media_open(const char *name, int verbose)
509 struct media_device *media;
512 media = malloc(sizeof(*media));
514 printf("%s: unable to allocate memory\n", __func__);
517 memset(media, 0, sizeof(*media));
520 printf("Opening media device %s\n", name);
521 media->fd = open(name, O_RDWR);
524 printf("%s: Can't open media device %s\n", __func__, name);
529 printf("Enumerating entities\n");
531 ret = media_enum_entities(media);
533 printf("%s: Unable to enumerate entities for device %s (%s)\n",
534 __func__, name, strerror(-ret));
540 printf("Found %u entities\n", media->entities_count);
541 printf("Enumerating pads and links\n");
544 ret = media_enum_links(media);
546 printf("%s: Unable to enumerate pads and linksfor device %s\n",
558 void media_close(struct media_device *media)
565 for (i = 0; i < media->entities_count; ++i) {
566 struct media_entity *entity = &media->entities[i];
570 if (entity->fd != -1)
574 free(media->entities);