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_get_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)
151 * media_get_entity_by_id -
153 struct media_entity *media_get_entity_by_id(struct media_device *media,
158 for (i = 0; i < media->entities_count; ++i) {
159 struct media_entity *entity = &media->entities[i];
161 if (entity->info.id == id)
171 int media_setup_link(struct media_device *media,
172 struct media_entity_pad *source,
173 struct media_entity_pad *sink,
176 struct media_entity_link *link;
177 struct media_user_link ulink;
181 for (i = 0; i < source->entity->info.links; i++) {
182 link = &source->entity->links[i];
184 if (link->source->entity == source->entity &&
185 link->source->index == source->index &&
186 link->sink->entity == sink->entity &&
187 link->sink->index == sink->index)
191 if (i == source->entity->info.links) {
192 printf("%s: Link not found\n", __func__);
197 ulink.source.entity = source->entity->info.id;
198 ulink.source.index = source->index;
199 ulink.source.type = MEDIA_PAD_TYPE_OUTPUT;
202 ulink.sink.entity = sink->entity->info.id;
203 ulink.sink.index = sink->index;
204 ulink.sink.type = MEDIA_PAD_TYPE_INPUT;
206 ulink.flags = flags | (link->flags & MEDIA_LINK_FLAG_IMMUTABLE);
208 ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
210 printf("%s: Unable to setup link (%s)\n", __func__,
219 int media_reset_links(struct media_device *media)
224 for (i = 0; i < media->entities_count; ++i) {
225 struct media_entity *entity = &media->entities[i];
227 for (j = 0; j < entity->info.links; j++) {
228 struct media_entity_link *link = &entity->links[j];
230 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
233 ret = media_setup_link(media, link->source, link->sink,
234 link->flags & ~MEDIA_LINK_FLAG_ACTIVE);
243 static void media_print_topology_dot(struct media_device *media)
247 printf("digraph board {\n");
248 printf("\trankdir=TB\n");
250 for (i = 0; i < media->entities_count; ++i) {
251 struct media_entity *entity = &media->entities[i];
254 switch (entity->info.type) {
255 case MEDIA_ENTITY_TYPE_NODE:
256 printf("\tn%08x [label=\"%s\\n%s\", shape=box, style=filled, "
257 "fillcolor=yellow]\n",
258 entity->info.id, entity->info.name, entity->devname);
261 case MEDIA_ENTITY_TYPE_SUBDEV:
262 printf("\tn%08x [label=\"{{", entity->info.id);
264 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
265 if (entity->pads[j].type != MEDIA_PAD_TYPE_INPUT)
268 printf("%s<port%u> %u", npads ? " | " : "", j, j);
272 printf("} | %s | {", entity->info.name);
274 for (j = 0, npads = 0; j < entity->info.pads; ++j) {
275 if (entity->pads[j].type != MEDIA_PAD_TYPE_OUTPUT)
278 printf("%s<port%u> %u", npads ? " | " : "", j, j);
282 printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n");
289 for (j = 0; j < entity->info.links; j++) {
290 struct media_entity_link *link = &entity->links[j];
292 if (link->source->entity != entity)
295 printf("\tn%08x", link->source->entity->info.id);
296 if (link->source->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV)
297 printf(":port%u", link->source->index);
299 printf("n%08x", link->sink->entity->info.id);
300 if (link->sink->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV)
301 printf(":port%u", link->sink->index);
303 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
304 printf(" [style=bold]");
305 else if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
306 printf(" [style=dashed]");
314 static void media_print_topology_text(struct media_device *media)
316 unsigned int i, j, k;
317 unsigned int padding;
320 printf("Device topology\n");
322 for (i = 0; i < media->entities_count; ++i) {
323 struct media_entity *entity = &media->entities[i];
325 padding = printf("- entity %u: ", entity->info.id);
326 printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
327 entity->info.pads, entity->info.pads > 1 ? "s" : "",
328 entity->info.links, entity->info.links > 1 ? "s" : "");
329 printf("%*ctype %s subtype %s\n", padding, ' ',
330 media_entity_type_to_string(entity->info.type),
331 media_entity_subtype_to_string(entity->info.type, entity->info.subtype));
332 if (entity->devname[0])
333 printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
335 for (j = 0; j < entity->info.pads; j++) {
336 struct media_entity_pad *pad = &entity->pads[j];
337 struct v4l2_mbus_framefmt format;
338 struct v4l2_rect rect;
340 printf("\tpad%u: %s", j, media_pad_type_to_string(pad->type));
342 if (entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV) {
343 ret = v4l2_subdev_get_format(entity, &format, j,
344 V4L2_SUBDEV_FORMAT_ACTIVE);
346 printf(" [%s %ux%u", pixelcode_to_string(format.code),
347 format.width, format.height);
349 ret = v4l2_subdev_get_crop(entity, &rect, j,
350 V4L2_SUBDEV_FORMAT_ACTIVE);
352 printf(" (%u,%u)/%ux%u", rect.left, rect.top,
353 rect.width, rect.height);
360 for (k = 0; k < entity->info.links; k++) {
361 struct media_entity_link *link = &entity->links[k];
363 if (link->source->entity != entity ||
364 link->source->index != j)
367 printf("\t\t-> '%s':pad%u [",
368 link->sink->entity->info.name, link->sink->index);
370 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
371 printf("IMMUTABLE,");
372 if (link->flags & MEDIA_LINK_FLAG_ACTIVE)
382 void media_print_topology(struct media_device *media, int dot)
385 media_print_topology_dot(media);
387 media_print_topology_text(media);
390 static int media_enum_links(struct media_device *media)
395 for (id = 1; id <= media->entities_count; id++) {
396 struct media_entity *entity = &media->entities[id - 1];
397 struct media_user_links links;
400 links.entity = entity->info.id;
401 links.pads = malloc(entity->info.pads * sizeof(struct media_user_pad));
402 links.links = malloc(entity->info.links * sizeof(struct media_user_link));
404 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
405 printf("%s: Unable to enumerate pads and links (%s).\n",
406 __func__, strerror(errno));
412 for (i = 0; i < entity->info.pads; ++i) {
413 entity->pads[i].entity = entity;
414 entity->pads[i].type = links.pads[i].type;
415 entity->pads[i].index = links.pads[i].index;
418 for (i = 0; i < entity->info.links; ++i) {
419 struct media_user_link *link = &links.links[i];
420 struct media_entity *source;
421 struct media_entity *sink;
423 source = media_get_entity_by_id(media, link->source.entity);
424 sink = media_get_entity_by_id(media, link->sink.entity);
426 if (source == NULL || sink == NULL) {
427 printf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
428 id, i, link->source.entity, link->source.index,
429 link->sink.entity, link->sink.index);
433 entity->links[i].source = &source->pads[link->source.index];
434 entity->links[i].sink = &sink->pads[link->sink.index];
435 entity->links[i].flags = links.links[i].flags;
445 static int media_enum_entities(struct media_device *media)
447 struct media_entity *entity;
455 for (id = 0; ; id = entity->info.id) {
456 size = (media->entities_count + 1) * sizeof(*media->entities);
457 media->entities = realloc(media->entities, size);
459 entity = &media->entities[media->entities_count];
460 memset(entity, 0, sizeof(*entity));
462 entity->info.id = id | MEDIA_ENTITY_ID_FLAG_NEXT;
464 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
471 entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
472 entity->links = malloc(entity->info.links * sizeof(*entity->links));
473 if (entity->pads == NULL || entity->links == NULL)
476 media->entities_count++;
478 /* Find the corresponding device name. */
479 if ((entity->info.type != MEDIA_ENTITY_TYPE_NODE ||
480 entity->info.type != MEDIA_NODE_TYPE_V4L) &&
481 (entity->info.type != MEDIA_ENTITY_TYPE_SUBDEV))
484 for (i = 0; i < 256; ++i) {
485 if (entity->info.type == MEDIA_ENTITY_TYPE_NODE)
486 sprintf(devname, "/dev/video%u", i);
488 sprintf(devname, "/dev/subdev%u", i);
490 ret = stat(devname, &devstat);
494 if (major(devstat.st_rdev) == entity->info.v4l.major &&
495 minor(devstat.st_rdev) == entity->info.v4l.minor) {
496 strcpy(entity->devname, devname);
501 id = entity->info.id;
510 struct media_device *media_open(const char *name, int verbose)
512 struct media_device *media;
515 media = malloc(sizeof(*media));
517 printf("%s: unable to allocate memory\n", __func__);
520 memset(media, 0, sizeof(*media));
523 printf("Opening media device %s\n", name);
524 media->fd = open(name, O_RDWR);
527 printf("%s: Can't open media device %s\n", __func__, name);
532 printf("Enumerating entities\n");
534 ret = media_enum_entities(media);
536 printf("%s: Unable to enumerate entities for device %s (%s)\n",
537 __func__, name, strerror(-ret));
543 printf("Found %u entities\n", media->entities_count);
544 printf("Enumerating pads and links\n");
547 ret = media_enum_links(media);
549 printf("%s: Unable to enumerate pads and linksfor device %s\n",
561 void media_close(struct media_device *media)
568 for (i = 0; i < media->entities_count; ++i) {
569 struct media_entity *entity = &media->entities[i];
573 if (entity->fd != -1)
577 free(media->entities);