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 for (i = 0; i < ARRAY_SIZE(types); i++) {
51 if (types[i].type == type)
58 static const char *media_entity_subtype_to_string(unsigned type, unsigned subtype)
60 static const char *node_types[] = {
67 static const char *subdev_types[] = {
75 case MEDIA_ENTITY_TYPE_NODE:
78 return node_types[subtype];
80 case MEDIA_ENTITY_TYPE_SUBDEV:
83 return subdev_types[subtype];
89 static const char *media_pad_type_to_string(unsigned type)
95 { MEDIA_PAD_TYPE_INPUT, "Input" },
96 { MEDIA_PAD_TYPE_OUTPUT, "Output" },
101 for (i = 0; i < ARRAY_SIZE(types); i++) {
102 if (types[i].type == type)
103 return types[i].name;
110 * media_entity_remote_pad -
112 struct media_entity_pad *media_entity_remote_pad(struct media_entity_pad *pad)
116 for (i = 0; i < pad->entity->info.links; ++i) {
117 struct media_entity_link *link = &pad->entity->links[i];
119 if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
122 if (link->source == pad)
125 if (link->sink == pad)
133 * media_entity_by_name -
135 struct media_entity *media_get_entity_by_name(struct media_device *media,
136 const char *name, size_t length)
140 for (i = 0; i < media->entities_count; ++i) {
141 struct media_entity *entity = &media->entities[i];
143 if (strncmp(entity->info.name, name, length) == 0)
153 int media_setup_link(struct media_device *media,
154 struct media_entity_pad *source,
155 struct media_entity_pad *sink,
158 struct media_entity_link *link;
159 struct media_user_link ulink;
163 for (i = 0; i < source->entity->info.links; i++) {
164 link = &source->entity->links[i];
166 if (link->source->entity == source->entity &&
167 link->source->index == source->index &&
168 link->sink->entity == sink->entity &&
169 link->sink->index == sink->index)
173 if (i == source->entity->info.links) {
174 printf("%s: Link not found\n", __func__);
179 ulink.source.entity = source->entity->info.id;
180 ulink.source.index = source->index;
181 ulink.source.type = MEDIA_PAD_TYPE_OUTPUT;
184 ulink.sink.entity = sink->entity->info.id;
185 ulink.sink.index = sink->index;
186 ulink.sink.type = MEDIA_PAD_TYPE_INPUT;
188 ulink.flags = flags | (link->flags & MEDIA_LINK_FLAG_IMMUTABLE);
190 ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
192 printf("%s: Unable to setup link (%s)\n", __func__,
201 int media_reset_links(struct media_device *media)
206 for (i = 0; i < media->entities_count; ++i) {
207 struct media_entity *entity = &media->entities[i];
209 for (j = 0; j < entity->info.links; j++) {
210 struct media_entity_link *link = &entity->links[j];
212 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
215 ret = media_setup_link(media, link->source, link->sink,
216 link->flags & ~MEDIA_LINK_FLAG_ACTIVE);
225 static void media_print_topology_dot(struct media_device *media)
229 printf("digraph board {\n");
230 printf("\trankdir=TB\n");
232 for (i = 0; i < media->entities_count; ++i) {
233 struct media_entity *entity = &media->entities[i];
236 switch (entity->info.type) {
237 case MEDIA_ENTITY_TYPE_NODE:
238 printf("\tn%08x [label=\"%s\", shape=box, style=filled, "
239 "fillcolor=yellow]\n",
240 entity->info.id, entity->info.name);
243 case MEDIA_ENTITY_TYPE_SUBDEV:
244 printf("\tn%08x [label=\"{{", entity->info.id);
246 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
247 if (entity->pads[j].type != MEDIA_PAD_TYPE_INPUT)
250 printf("%s<port%u> %u", npads ? " | " : "", j, j);
254 printf("} | %s | {", entity->info.name);
256 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
257 if (entity->pads[j].type != MEDIA_PAD_TYPE_OUTPUT)
260 printf("%s<port%u> %u", npads ? " | " : "", j, j);
264 printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n");
271 for (j = 0; j < entity->info.links; j++) {
272 struct media_entity_link *link = &entity->links[j];
274 if (link->source->entity != entity)
277 printf("\tn%08x", link->source->entity->info.id);
278 if (link->source->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV)
279 printf(":port%u", link->source->index);
281 printf("n%08x", link->sink->entity->info.id);
282 if (link->sink->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV)
283 printf(":port%u", link->sink->index);
285 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
286 printf(" [style=bold]");
287 else if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
288 printf(" [style=dashed]");
296 static void media_print_topology_text(struct media_device *media)
298 unsigned int i, j, k;
299 unsigned int padding;
302 printf("Device topology\n");
304 for (i = 0; i < media->entities_count; ++i) {
305 struct media_entity *entity = &media->entities[i];
307 padding = printf("- entity %u: ", entity->info.id);
308 printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
309 entity->info.pads, entity->info.pads > 1 ? "s" : "",
310 entity->info.links, entity->info.links > 1 ? "s" : "");
311 printf("%*ctype %s subtype %s\n", padding, ' ',
312 media_entity_type_to_string(entity->info.type),
313 media_entity_subtype_to_string(entity->info.type, entity->info.subtype));
315 printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
317 for (j = 0; j < entity->info.pads; j++) {
318 struct media_entity_pad *pad = &entity->pads[j];
319 struct v4l2_mbus_framefmt format;
321 printf("\tpad%u: %s", j, media_pad_type_to_string(pad->type));
323 if (entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV) {
324 ret = v4l2_subdev_get_format(entity, &format, j,
325 V4L2_SUBDEV_FORMAT_ACTIVE);
327 printf(" [%s %ux%u]", pixelcode_to_string(format.code),
328 format.width, format.height);
333 for (k = 0; k < entity->info.links; k++) {
334 struct media_entity_link *link = &entity->links[k];
336 if (link->source->entity != entity ||
337 link->source->index != j)
340 printf("\t\t-> '%s':pad%u [",
341 link->sink->entity->info.name, link->sink->index);
343 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
344 printf("IMMUTABLE,");
345 if (link->flags & MEDIA_LINK_FLAG_ACTIVE)
355 void media_print_topology(struct media_device *media, int dot)
358 media_print_topology_dot(media);
360 media_print_topology_text(media);
363 static int media_enum_links(struct media_device *media)
368 for (id = 1; id <= media->entities_count; id++) {
369 struct media_entity *entity = &media->entities[id - 1];
370 struct media_user_links links;
373 links.entity = entity->info.id;
374 links.pads = malloc(entity->info.pads * sizeof(struct media_user_pad));
375 links.links = malloc(entity->info.links * sizeof(struct media_user_link));
377 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
378 printf("%s: Unable to enumerate pads and links (%s).\n",
379 __func__, strerror(errno));
385 for (i = 0; i < entity->info.pads; ++i) {
386 entity->pads[i].entity = entity;
387 entity->pads[i].type = links.pads[i].type;
388 entity->pads[i].index = links.pads[i].index;
391 for (i = 0; i < entity->info.links; ++i) {
392 struct media_user_link *link = &links.links[i];
393 struct media_entity *source;
394 struct media_entity *sink;
396 if (link->source.entity > media->entities_count ||
397 link->sink.entity > media->entities_count) {
398 printf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
399 id, i, link->source.entity, link->source.index,
400 link->sink.entity, link->sink.index);
404 source = &media->entities[link->source.entity - 1];
405 sink = &media->entities[link->sink.entity - 1];
406 entity->links[i].source = &source->pads[link->source.index];
407 entity->links[i].sink = &sink->pads[link->sink.index];
408 entity->links[i].flags = links.links[i].flags;
418 static int media_enum_entities(struct media_device *media)
420 struct media_entity *entity;
427 for (id = 1; ; id++) {
429 realloc(media->entities, id * sizeof(*media->entities));
430 entity = &media->entities[id - 1];
431 memset(entity, 0, sizeof(*entity));
433 entity->info.id = id;
435 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
442 entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
443 entity->links = malloc(entity->info.links * sizeof(*entity->links));
444 if (entity->pads == NULL || entity->links == NULL)
447 media->entities_count++;
449 /* Find the corresponding device name. */
450 if ((entity->info.type != MEDIA_ENTITY_TYPE_NODE ||
451 entity->info.type != MEDIA_NODE_TYPE_V4L) &&
452 (entity->info.type != MEDIA_ENTITY_TYPE_SUBDEV))
455 for (i = 0; i < 256; ++i) {
456 if (entity->info.type == MEDIA_ENTITY_TYPE_NODE)
457 sprintf(devname, "/dev/video%u", i);
459 sprintf(devname, "/dev/subdev%u", i);
461 ret = stat(devname, &devstat);
465 if (major(devstat.st_rdev) == entity->info.v4l.major &&
466 minor(devstat.st_rdev) == entity->info.v4l.minor) {
467 strcpy(entity->devname, devname);
479 struct media_device *media_open(const char *name, int verbose)
481 struct media_device *media;
484 media = malloc(sizeof(*media));
486 printf("%s: unable to allocate memory\n", __func__);
489 memset(media, 0, sizeof(*media));
492 printf("Opening media device %s\n", name);
493 media->fd = open(name, O_RDWR);
496 printf("%s: Can't open media device %s\n", __func__, name);
501 printf("Enumerating entities\n");
503 ret = media_enum_entities(media);
505 printf("%s: Unable to enumerate entities for device %s (%s)\n",
506 __func__, name, strerror(-ret));
512 printf("Found %u entities\n", media->entities_count);
513 printf("Enumerating pads and links\n");
516 ret = media_enum_links(media);
518 printf("%s: Unable to enumerate pads and linksfor device %s\n",
530 void media_close(struct media_device *media)
537 for (i = 0; i < media->entities_count; ++i) {
538 struct media_entity *entity = &media->entities[i];
542 if (entity->fd != -1)
546 free(media->entities);