Shorten media structures names
[media-ctl.git] / media.c
diff --git a/media.c b/media.c
index 9974ae1..5508775 100644 (file)
--- a/media.c
+++ b/media.c
 #include <linux/media.h>
 
 #include "media.h"
-#include "subdev.h"
 #include "tools.h"
 
-static const char *media_entity_type_to_string(unsigned type)
-{
-       static const struct {
-               __u32 type;
-               const char *name;
-       } types[] = {
-               { MEDIA_ENTITY_TYPE_NODE, "Node" },
-               { MEDIA_ENTITY_TYPE_SUBDEV, "V4L2 subdev" },
-       };
-
-       unsigned int i;
-
-       type &= MEDIA_ENTITY_TYPE_MASK;
-
-       for (i = 0; i < ARRAY_SIZE(types); i++) {
-               if (types[i].type == type)
-                       return types[i].name;
-       }
-
-       return "Unknown";
-}
-
-static const char *media_entity_subtype_to_string(unsigned type)
-{
-       static const char *node_types[] = {
-               "Unknown",
-               "V4L",
-               "FB",
-               "ALSA",
-               "DVB",
-       };
-       static const char *subdev_types[] = {
-               "Unknown",
-               "Sensor",
-               "Flash",
-               "Lens",
-       };
-
-       unsigned int subtype = type & MEDIA_ENTITY_SUBTYPE_MASK;
-
-       switch (type & MEDIA_ENTITY_TYPE_MASK) {
-       case MEDIA_ENTITY_TYPE_NODE:
-               if (subtype >= ARRAY_SIZE(node_types))
-                       subtype = 0;
-               return node_types[subtype];
-
-       case MEDIA_ENTITY_TYPE_SUBDEV:
-               if (subtype >= ARRAY_SIZE(subdev_types))
-                       subtype = 0;
-               return subdev_types[subtype];
-       default:
-               return node_types[0];
-       }
-}
-
-static const char *media_pad_type_to_string(unsigned flag)
-{
-       static const struct {
-               __u32 flag;
-               const char *name;
-       } flags[] = {
-               { MEDIA_PAD_FLAG_INPUT, "Input" },
-               { MEDIA_PAD_FLAG_OUTPUT, "Output" },
-       };
-
-       unsigned int i;
-
-       for (i = 0; i < ARRAY_SIZE(flags); i++) {
-               if (flags[i].flag & flag)
-                       return flags[i].name;
-       }
-
-       return "Unknown";
-}
-
 /*
  * media_entity_remote_pad -
  */
-struct media_entity_pad *media_entity_remote_pad(struct media_entity_pad *pad)
+struct media_pad *media_entity_remote_source(struct media_pad *pad)
 {
        unsigned int i;
 
-       for (i = 0; i < pad->entity->info.links; ++i) {
-               struct media_entity_link *link = &pad->entity->links[i];
+       if (!(pad->flags & MEDIA_PAD_FLAG_INPUT))
+               return NULL;
+
+       for (i = 0; i < pad->entity->num_links; ++i) {
+               struct media_link *link = &pad->entity->links[i];
 
                if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
                        continue;
 
-               if (link->source == pad)
-                       return link->sink;
-
                if (link->sink == pad)
                        return link->source;
        }
@@ -173,16 +97,16 @@ struct media_entity *media_get_entity_by_id(struct media_device *media,
  * media_setup_link -
  */
 int media_setup_link(struct media_device *media,
-                    struct media_entity_pad *source,
-                    struct media_entity_pad *sink,
+                    struct media_pad *source,
+                    struct media_pad *sink,
                     __u32 flags)
 {
-       struct media_entity_link *link;
+       struct media_link *link;
        struct media_link_desc ulink;
        unsigned int i;
        int ret;
 
-       for (i = 0; i < source->entity->info.links; i++) {
+       for (i = 0; i < source->entity->num_links; i++) {
                link = &source->entity->links[i];
 
                if (link->source->entity == source->entity &&
@@ -192,7 +116,7 @@ int media_setup_link(struct media_device *media,
                        break;
        }
 
-       if (i == source->entity->info.links) {
+       if (i == source->entity->num_links) {
                printf("%s: Link not found\n", __func__);
                return -EINVAL;
        }
@@ -228,10 +152,11 @@ int media_reset_links(struct media_device *media)
        for (i = 0; i < media->entities_count; ++i) {
                struct media_entity *entity = &media->entities[i];
 
-               for (j = 0; j < entity->info.links; j++) {
-                       struct media_entity_link *link = &entity->links[j];
+               for (j = 0; j < entity->num_links; j++) {
+                       struct media_link *link = &entity->links[j];
 
-                       if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
+                       if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE ||
+                           link->source->entity != entity)
                                continue;
 
                        ret = media_setup_link(media, link->source, link->sink,
@@ -244,138 +169,25 @@ int media_reset_links(struct media_device *media)
        return 0;
 }
 
-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 (media_entity_type(entity)) {
-               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].flags & MEDIA_PAD_FLAG_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].flags & MEDIA_PAD_FLAG_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 (media_entity_type(link->source->entity) == MEDIA_ENTITY_TYPE_SUBDEV)
-                               printf(":port%u", link->source->index);
-                       printf(" -> ");
-                       printf("n%08x", link->sink->entity->info.id);
-                       if (media_entity_type(link->sink->entity) == 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)
+static struct media_link *media_entity_add_link(struct media_entity *entity)
 {
-       unsigned int i, j, k;
-       unsigned int padding;
-
-       printf("Device topology\n");
-
-       for (i = 0; i < media->entities_count; ++i) {
-               struct media_entity *entity = &media->entities[i];
-
-               padding = printf("- entity %u: ", entity->info.id);
-               printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
-                       entity->info.pads, entity->info.pads > 1 ? "s" : "",
-                       entity->info.links, entity->info.links > 1 ? "s" : "");
-               printf("%*ctype %s subtype %s\n", padding, ' ',
-                       media_entity_type_to_string(entity->info.type),
-                       media_entity_subtype_to_string(entity->info.type));
-               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->flags));
-
-                       if (media_entity_type(entity) == 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];
-
-                               if (link->source->entity != entity ||
-                                   link->source->index != j)
-                                       continue;
+       if (entity->num_links >= entity->max_links) {
+               struct media_link *links = entity->links;
+               unsigned int max_links = entity->max_links * 2;
+               unsigned int i;
 
-                               printf("\t\t-> '%s':pad%u [",
-                                       link->sink->entity->info.name, link->sink->index);
+               links = realloc(links, max_links * sizeof *links);
+               if (links == NULL)
+                       return NULL;
 
-                               if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
-                                       printf("IMMUTABLE,");
-                               if (link->flags & MEDIA_LINK_FLAG_ACTIVE)
-                                       printf("ACTIVE");
+               for (i = 0; i < entity->num_links; ++i)
+                       links[i].twin->twin = &links[i];
 
-                               printf("]\n");
-                       }
-               }
-               printf("\n");
+               entity->max_links = max_links;
+               entity->links = links;
        }
-}
 
-void media_print_topology(struct media_device *media, int dot)
-{
-       if (dot)
-               media_print_topology_dot(media);
-       else
-               media_print_topology_text(media);
+       return &entity->links[entity->num_links++];
 }
 
 static int media_enum_links(struct media_device *media)
@@ -408,6 +220,8 @@ static int media_enum_links(struct media_device *media)
 
                for (i = 0; i < entity->info.links; ++i) {
                        struct media_link_desc *link = &links.links[i];
+                       struct media_link *fwdlink;
+                       struct media_link *backlink;
                        struct media_entity *source;
                        struct media_entity *sink;
 
@@ -420,9 +234,18 @@ static int media_enum_links(struct media_device *media)
                                        link->sink.entity, link->sink.index);
                                ret = -EINVAL;
                        } else {
-                               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;
+                               fwdlink = media_entity_add_link(source);
+                               fwdlink->source = &source->pads[link->source.index];
+                               fwdlink->sink = &sink->pads[link->sink.index];
+                               fwdlink->flags = links.links[i].flags;
+
+                               backlink = media_entity_add_link(sink);
+                               backlink->source = &source->pads[link->source.index];
+                               backlink->sink = &sink->pads[link->sink.index];
+                               backlink->flags = links.links[i].flags;
+
+                               fwdlink->twin = backlink;
+                               backlink->twin = fwdlink;
                        }
                }
 
@@ -461,8 +284,14 @@ static int media_enum_entities(struct media_device *media)
                        return -errno;
                }
 
+               /* Number of links (for outbound links) plus number of pads (for
+                * inbound links) is a good safe initial estimate of the total
+                * number of links.
+                */
+               entity->max_links = entity->info.pads + entity->info.links;
+
                entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
-               entity->links = malloc(entity->info.links * sizeof(*entity->links));
+               entity->links = malloc(entity->max_links * sizeof(*entity->links));
                if (entity->pads == NULL || entity->links == NULL)
                        return -ENOMEM;