Print subdev device node names in dot diagrams
[media-ctl.git] / media.c
diff --git a/media.c b/media.c
index 81c730c..f384026 100644 (file)
--- a/media.c
+++ b/media.c
@@ -32,6 +32,7 @@
 #include <linux/media.h>
 
 #include "media.h"
+#include "subdev.h"
 #include "tools.h"
 
 static const char *media_entity_type_to_string(unsigned type)
@@ -85,6 +86,26 @@ static const char *media_entity_subtype_to_string(unsigned type, unsigned subtyp
        }
 }
 
+static const char *media_pad_type_to_string(unsigned type)
+{
+       static const struct {
+               __u32 type;
+               const char *name;
+       } types[] = {
+               { MEDIA_PAD_TYPE_INPUT, "Input" },
+               { MEDIA_PAD_TYPE_OUTPUT, "Output" },
+       };
+
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_SIZE(types); i++) {
+               if (types[i].type == type)
+                       return types[i].name;
+       }
+
+       return "Unknown";
+}
+
 /*
  * media_entity_remote_pad -
  */
@@ -109,7 +130,7 @@ struct media_entity_pad *media_entity_remote_pad(struct media_entity_pad *pad)
 }
 
 /*
- * media_entity_by_name -
+ * media_get_entity_by_name -
  */
 struct media_entity *media_get_entity_by_name(struct media_device *media,
                                              const char *name, size_t length)
@@ -126,6 +147,24 @@ struct media_entity *media_get_entity_by_name(struct media_device *media,
        return NULL;
 }
 
+/*
+ * media_get_entity_by_id -
+ */
+struct media_entity *media_get_entity_by_id(struct media_device *media,
+                                           __u32 id)
+{
+       unsigned int i;
+
+       for (i = 0; i < media->entities_count; ++i) {
+               struct media_entity *entity = &media->entities[i];
+
+               if (entity->info.id == id)
+                       return entity;
+       }
+
+       return NULL;
+}
+
 /*
  * media_setup_link -
  */
@@ -201,7 +240,81 @@ int media_reset_links(struct media_device *media)
        return 0;
 }
 
-void media_print_topology(struct media_device *media)
+static void media_print_topology_dot(struct media_device *media)
+{
+       unsigned int i, j;
+
+       printf("digraph board {\n");
+       printf("\trankdir=TB\n");
+
+       for (i = 0; i < media->entities_count; ++i) {
+               struct media_entity *entity = &media->entities[i];
+               unsigned int npads;
+
+               switch (entity->info.type) {
+               case MEDIA_ENTITY_TYPE_NODE:
+                       printf("\tn%08x [label=\"%s\\n%s\", shape=box, style=filled, "
+                              "fillcolor=yellow]\n",
+                              entity->info.id, entity->info.name, entity->devname);
+                       break;
+
+               case MEDIA_ENTITY_TYPE_SUBDEV:
+                       printf("\tn%08x [label=\"{{", entity->info.id);
+
+                       for (j = 0, npads = 0; j < entity->info.pads; ++j) {
+                               if (entity->pads[j].type != MEDIA_PAD_TYPE_INPUT)
+                                       continue;
+
+                               printf("%s<port%u> %u", npads ? " | " : "", j, j);
+                               npads++;
+                       }
+
+                       printf("} | %s", entity->info.name);
+                       if (entity->devname)
+                               printf("\\n%s", entity->devname);
+                       printf(" | {");
+
+                       for (j = 0, npads = 0; j < entity->info.pads; ++j) {
+                               if (entity->pads[j].type != MEDIA_PAD_TYPE_OUTPUT)
+                                       continue;
+
+                               printf("%s<port%u> %u", npads ? " | " : "", j, j);
+                               npads++;
+                       }
+
+                       printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n");
+                       break;
+
+               default:
+                       continue;
+               }
+
+               for (j = 0; j < entity->info.links; j++) {
+                       struct media_entity_link *link = &entity->links[j];
+
+                       if (link->source->entity != entity)
+                               continue;
+
+                       printf("\tn%08x", link->source->entity->info.id);
+                       if (link->source->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV)
+                               printf(":port%u", link->source->index);
+                       printf(" -> ");
+                       printf("n%08x", link->sink->entity->info.id);
+                       if (link->sink->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV)
+                               printf(":port%u", link->sink->index);
+
+                       if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
+                               printf(" [style=bold]");
+                       else if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
+                               printf(" [style=dashed]");
+                       printf("\n");
+               }
+       }
+
+       printf("}\n");
+}
+
+static void media_print_topology_text(struct media_device *media)
 {
        unsigned int i, j, k;
        unsigned int padding;
@@ -218,10 +331,19 @@ void media_print_topology(struct media_device *media)
                printf("%*ctype %s subtype %s\n", padding, ' ',
                        media_entity_type_to_string(entity->info.type),
                        media_entity_subtype_to_string(entity->info.type, entity->info.subtype));
-               if (entity->devname)
+               if (entity->devname[0])
                        printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
 
                for (j = 0; j < entity->info.pads; j++) {
+                       struct media_entity_pad *pad = &entity->pads[j];
+
+                       printf("\tpad%u: %s ", j, media_pad_type_to_string(pad->type));
+
+                       if (entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV)
+                               v4l2_subdev_print_format(entity, j, V4L2_SUBDEV_FORMAT_ACTIVE);
+
+                       printf("\n");
+
                        for (k = 0; k < entity->info.links; k++) {
                                struct media_entity_link *link = &entity->links[k];
 
@@ -229,7 +351,7 @@ void media_print_topology(struct media_device *media)
                                    link->source->index != j)
                                        continue;
 
-                               printf("\tpad%u -> '%s':pad%u [", link->source->index,
+                               printf("\t\t-> '%s':pad%u [",
                                        link->sink->entity->info.name, link->sink->index);
 
                                if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
@@ -244,6 +366,14 @@ void media_print_topology(struct media_device *media)
        }
 }
 
+void media_print_topology(struct media_device *media, int dot)
+{
+       if (dot)
+               media_print_topology_dot(media);
+       else
+               media_print_topology_text(media);
+}
+
 static int media_enum_links(struct media_device *media)
 {
        __u32 id;
@@ -277,16 +407,16 @@ static int media_enum_links(struct media_device *media)
                        struct media_entity *source;
                        struct media_entity *sink;
 
-                       if (link->source.entity > media->entities_count ||
-                           link->sink.entity > media->entities_count) {
+                       source = media_get_entity_by_id(media, link->source.entity);
+                       sink = media_get_entity_by_id(media, link->sink.entity);
+
+                       if (source == NULL || sink == NULL) {
                                printf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
                                        id, i, link->source.entity, link->source.index,
                                        link->sink.entity, link->sink.index);
                                ret = -EINVAL;
                        }
 
-                       source = &media->entities[link->source.entity - 1];
-                       sink = &media->entities[link->sink.entity - 1];
                        entity->links[i].source = &source->pads[link->source.index];
                        entity->links[i].sink = &sink->pads[link->sink.index];
                        entity->links[i].flags = links.links[i].flags;
@@ -303,18 +433,22 @@ static int media_enum_entities(struct media_device *media)
 {
        struct media_entity *entity;
        struct stat devstat;
+       unsigned int size;
        char devname[32];
-       unsigned int i;
+       char sysname[32];
+       char target[1024];
+       char *p;
        __u32 id;
        int ret;
 
-       for (id = 1; ; id++) {
-               media->entities =
-                       realloc(media->entities, id * sizeof(*media->entities));
-               entity = &media->entities[id - 1];
+       for (id = 0; ; id = entity->info.id) {
+               size = (media->entities_count + 1) * sizeof(*media->entities);
+               media->entities = realloc(media->entities, size);
+
+               entity = &media->entities[media->entities_count];
                memset(entity, 0, sizeof(*entity));
                entity->fd = -1;
-               entity->info.id = id;
+               entity->info.id = id | MEDIA_ENTITY_ID_FLAG_NEXT;
 
                ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
                if (ret < 0) {
@@ -336,22 +470,29 @@ static int media_enum_entities(struct media_device *media)
                    (entity->info.type != MEDIA_ENTITY_TYPE_SUBDEV))
                        continue;
 
-               for (i = 0; i < 256; ++i) {
-                       if (entity->info.type == MEDIA_ENTITY_TYPE_NODE)
-                               sprintf(devname, "/dev/video%u", i);
-                       else
-                               sprintf(devname, "/dev/subdev%u", i);
+               sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
+                       entity->info.v4l.minor);
+               ret = readlink(sysname, target, sizeof(target));
+               if (ret < 0)
+                       continue;
 
-                       ret = stat(devname, &devstat);
-                       if (ret < 0)
-                               continue;
+               target[ret] = '\0';
+               p = strrchr(target, '/');
+               if (p == NULL)
+                       continue;
 
-                       if (major(devstat.st_rdev) == entity->info.v4l.major &&
-                           minor(devstat.st_rdev) == entity->info.v4l.minor) {
-                               strcpy(entity->devname, devname);
-                               break;
-                       }
-               }
+               sprintf(devname, "/dev/%s", p + 1);
+               ret = stat(devname, &devstat);
+               if (ret < 0)
+                       continue;
+
+               /* Sanity check: udev might have reordered the device nodes.
+                * Make sure the major/minor match. We should really use
+                * libudev.
+                */
+               if (major(devstat.st_rdev) == entity->info.v4l.major &&
+                   minor(devstat.st_rdev) == entity->info.v4l.minor)
+                       strcpy(entity->devname, devname);
        }
 
        return 0;
@@ -387,7 +528,7 @@ struct media_device *media_open(const char *name, int verbose)
        ret = media_enum_entities(media);
        if (ret < 0) {
                printf("%s: Unable to enumerate entities for device %s (%s)\n",
-                       __func__, name, strerror(ret));
+                       __func__, name, strerror(-ret));
                media_close(media);
                return NULL;
        }