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>
37 static const char *media_entity_type_to_string(unsigned type)
43 { MEDIA_ENTITY_TYPE_NODE, "Node" },
44 { MEDIA_ENTITY_TYPE_SUBDEV, "V4L2 subdev" },
49 for (i = 0; i < ARRAY_SIZE(types); i++) {
50 if (types[i].type == type)
57 static const char *media_entity_subtype_to_string(unsigned type, unsigned subtype)
59 static const char *node_types[] = {
66 static const char *subdev_types[] = {
74 case MEDIA_ENTITY_TYPE_NODE:
77 return node_types[subtype];
79 case MEDIA_ENTITY_TYPE_SUBDEV:
82 return subdev_types[subtype];
89 * media_entity_remote_pad -
91 struct media_entity_pad *media_entity_remote_pad(struct media_entity_pad *pad)
95 for (i = 0; i < pad->entity->info.links; ++i) {
96 struct media_entity_link *link = &pad->entity->links[i];
98 if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
101 if (link->source == pad)
104 if (link->sink == pad)
112 * media_entity_by_name -
114 struct media_entity *media_get_entity_by_name(struct media_device *media,
115 const char *name, size_t length)
119 for (i = 0; i < media->entities_count; ++i) {
120 struct media_entity *entity = &media->entities[i];
122 if (strncmp(entity->info.name, name, length) == 0)
132 int media_setup_link(struct media_device *media,
133 struct media_entity_pad *source,
134 struct media_entity_pad *sink,
137 struct media_entity_link *link;
138 struct media_user_link ulink;
142 for (i = 0; i < source->entity->info.links; i++) {
143 link = &source->entity->links[i];
145 if (link->source->entity == source->entity &&
146 link->source->index == source->index &&
147 link->sink->entity == sink->entity &&
148 link->sink->index == sink->index)
152 if (i == source->entity->info.links) {
153 printf("%s: Link not found\n", __func__);
158 ulink.source.entity = source->entity->info.id;
159 ulink.source.index = source->index;
160 ulink.source.type = MEDIA_PAD_TYPE_OUTPUT;
163 ulink.sink.entity = sink->entity->info.id;
164 ulink.sink.index = sink->index;
165 ulink.sink.type = MEDIA_PAD_TYPE_INPUT;
167 ulink.flags = flags | (link->flags & MEDIA_LINK_FLAG_IMMUTABLE);
169 ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
171 printf("%s: Unable to setup link (%s)\n", __func__,
180 int media_reset_links(struct media_device *media)
185 for (i = 0; i < media->entities_count; ++i) {
186 struct media_entity *entity = &media->entities[i];
188 for (j = 0; j < entity->info.links; j++) {
189 struct media_entity_link *link = &entity->links[j];
191 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
194 ret = media_setup_link(media, link->source, link->sink,
195 link->flags & ~MEDIA_LINK_FLAG_ACTIVE);
204 static void media_print_topology_dot(struct media_device *media)
208 printf("digraph board {\n");
209 printf("\trankdir=TB\n");
211 for (i = 0; i < media->entities_count; ++i) {
212 struct media_entity *entity = &media->entities[i];
215 switch (entity->info.type) {
216 case MEDIA_ENTITY_TYPE_NODE:
217 printf("\tn%08x [label=\"%s\", shape=box, style=filled, "
218 "fillcolor=yellow]\n",
219 entity->info.id, entity->info.name);
222 case MEDIA_ENTITY_TYPE_SUBDEV:
223 printf("\tn%08x [label=\"{{", entity->info.id);
225 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
226 if (entity->pads[j].type != MEDIA_PAD_TYPE_INPUT)
229 printf("%s<port%u> %u", npads ? " | " : "", j, j);
233 printf("} | %s | {", entity->info.name);
235 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
236 if (entity->pads[j].type != MEDIA_PAD_TYPE_OUTPUT)
239 printf("%s<port%u> %u", npads ? " | " : "", j, j);
243 printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n");
250 for (j = 0; j < entity->info.links; j++) {
251 struct media_entity_link *link = &entity->links[j];
253 if (link->source->entity != entity)
256 printf("\tn%08x", link->source->entity->info.id);
257 if (link->source->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV)
258 printf(":port%u", link->source->index);
260 printf("n%08x", link->sink->entity->info.id);
261 if (link->sink->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV)
262 printf(":port%u", link->sink->index);
264 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
265 printf(" [style=bold]");
266 else if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
267 printf(" [style=dashed]");
275 static void media_print_topology_text(struct media_device *media)
277 unsigned int i, j, k;
278 unsigned int padding;
280 printf("Device topology\n");
282 for (i = 0; i < media->entities_count; ++i) {
283 struct media_entity *entity = &media->entities[i];
285 padding = printf("- entity %u: ", entity->info.id);
286 printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
287 entity->info.pads, entity->info.pads > 1 ? "s" : "",
288 entity->info.links, entity->info.links > 1 ? "s" : "");
289 printf("%*ctype %s subtype %s\n", padding, ' ',
290 media_entity_type_to_string(entity->info.type),
291 media_entity_subtype_to_string(entity->info.type, entity->info.subtype));
293 printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
295 for (j = 0; j < entity->info.pads; j++) {
296 for (k = 0; k < entity->info.links; k++) {
297 struct media_entity_link *link = &entity->links[k];
299 if (link->source->entity != entity ||
300 link->source->index != j)
303 printf("\tpad%u -> '%s':pad%u [", link->source->index,
304 link->sink->entity->info.name, link->sink->index);
306 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
307 printf("IMMUTABLE,");
308 if (link->flags & MEDIA_LINK_FLAG_ACTIVE)
318 void media_print_topology(struct media_device *media, int dot)
321 media_print_topology_dot(media);
323 media_print_topology_text(media);
326 static int media_enum_links(struct media_device *media)
331 for (id = 1; id <= media->entities_count; id++) {
332 struct media_entity *entity = &media->entities[id - 1];
333 struct media_user_links links;
336 links.entity = entity->info.id;
337 links.pads = malloc(entity->info.pads * sizeof(struct media_user_pad));
338 links.links = malloc(entity->info.links * sizeof(struct media_user_link));
340 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
341 printf("%s: Unable to enumerate pads and links (%s).\n",
342 __func__, strerror(errno));
348 for (i = 0; i < entity->info.pads; ++i) {
349 entity->pads[i].entity = entity;
350 entity->pads[i].type = links.pads[i].type;
351 entity->pads[i].index = links.pads[i].index;
354 for (i = 0; i < entity->info.links; ++i) {
355 struct media_user_link *link = &links.links[i];
356 struct media_entity *source;
357 struct media_entity *sink;
359 if (link->source.entity > media->entities_count ||
360 link->sink.entity > media->entities_count) {
361 printf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
362 id, i, link->source.entity, link->source.index,
363 link->sink.entity, link->sink.index);
367 source = &media->entities[link->source.entity - 1];
368 sink = &media->entities[link->sink.entity - 1];
369 entity->links[i].source = &source->pads[link->source.index];
370 entity->links[i].sink = &sink->pads[link->sink.index];
371 entity->links[i].flags = links.links[i].flags;
381 static int media_enum_entities(struct media_device *media)
383 struct media_entity *entity;
390 for (id = 1; ; id++) {
392 realloc(media->entities, id * sizeof(*media->entities));
393 entity = &media->entities[id - 1];
394 memset(entity, 0, sizeof(*entity));
396 entity->info.id = id;
398 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
405 entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
406 entity->links = malloc(entity->info.links * sizeof(*entity->links));
407 if (entity->pads == NULL || entity->links == NULL)
410 media->entities_count++;
412 /* Find the corresponding device name. */
413 if ((entity->info.type != MEDIA_ENTITY_TYPE_NODE ||
414 entity->info.type != MEDIA_NODE_TYPE_V4L) &&
415 (entity->info.type != MEDIA_ENTITY_TYPE_SUBDEV))
418 for (i = 0; i < 256; ++i) {
419 if (entity->info.type == MEDIA_ENTITY_TYPE_NODE)
420 sprintf(devname, "/dev/video%u", i);
422 sprintf(devname, "/dev/subdev%u", i);
424 ret = stat(devname, &devstat);
428 if (major(devstat.st_rdev) == entity->info.v4l.major &&
429 minor(devstat.st_rdev) == entity->info.v4l.minor) {
430 strcpy(entity->devname, devname);
442 struct media_device *media_open(const char *name, int verbose)
444 struct media_device *media;
447 media = malloc(sizeof(*media));
449 printf("%s: unable to allocate memory\n", __func__);
452 memset(media, 0, sizeof(*media));
455 printf("Opening media device %s\n", name);
456 media->fd = open(name, O_RDWR);
459 printf("%s: Can't open media device %s\n", __func__, name);
464 printf("Enumerating entities\n");
466 ret = media_enum_entities(media);
468 printf("%s: Unable to enumerate entities for device %s (%s)\n",
469 __func__, name, strerror(ret));
475 printf("Found %u entities\n", media->entities_count);
476 printf("Enumerating pads and links\n");
479 ret = media_enum_links(media);
481 printf("%s: Unable to enumerate pads and linksfor device %s\n",
493 void media_close(struct media_device *media)
500 for (i = 0; i < media->entities_count; ++i) {
501 struct media_entity *entity = &media->entities[i];
505 if (entity->fd != -1)
509 free(media->entities);