1bc39e1b34774c81752863d2c74e5e38af3f47b2
[media-ctl.git] / media.c
1 /*
2  * Media controller test application
3  *
4  * Copyright (C) 2010 Ideas on board SPRL <laurent.pinchart@ideasonboard.com>
5  *
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.
10  *
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.
15  *
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.,
18  */
19
20 #include <sys/ioctl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <errno.h>
30
31 #include <linux/videodev2.h>
32 #include <linux/media.h>
33
34 #include "media.h"
35 #include "subdev.h"
36 #include "tools.h"
37
38 static const char *media_entity_type_to_string(unsigned type)
39 {
40         static const struct {
41                 __u32 type;
42                 const char *name;
43         } types[] = {
44                 { MEDIA_ENTITY_TYPE_NODE, "Node" },
45                 { MEDIA_ENTITY_TYPE_SUBDEV, "V4L2 subdev" },
46         };
47
48         unsigned int i;
49
50         for (i = 0; i < ARRAY_SIZE(types); i++) {
51                 if (types[i].type == type)
52                         return types[i].name;
53         }
54
55         return "Unknown";
56 }
57
58 static const char *media_entity_subtype_to_string(unsigned type, unsigned subtype)
59 {
60         static const char *node_types[] = {
61                 "Unknown",
62                 "V4L",
63                 "FB",
64                 "ALSA",
65                 "DVB",
66         };
67         static const char *subdev_types[] = {
68                 "Unknown",
69                 "Video Decoder",
70                 "Video Encoder",
71                 "Miscellaneous",
72         };
73
74         switch (type) {
75         case MEDIA_ENTITY_TYPE_NODE:
76                 if (subtype > 4)
77                         subtype = 0;
78                 return node_types[subtype];
79
80         case MEDIA_ENTITY_TYPE_SUBDEV:
81                 if (subtype > 3)
82                         subtype = 0;
83                 return subdev_types[subtype];
84         default:
85                 return node_types[0];
86         }
87 }
88
89 static const char *media_pad_type_to_string(unsigned type)
90 {
91         static const struct {
92                 __u32 type;
93                 const char *name;
94         } types[] = {
95                 { MEDIA_PAD_TYPE_INPUT, "Input" },
96                 { MEDIA_PAD_TYPE_OUTPUT, "Output" },
97         };
98
99         unsigned int i;
100
101         for (i = 0; i < ARRAY_SIZE(types); i++) {
102                 if (types[i].type == type)
103                         return types[i].name;
104         }
105
106         return "Unknown";
107 }
108
109 /*
110  * media_entity_remote_pad -
111  */
112 struct media_entity_pad *media_entity_remote_pad(struct media_entity_pad *pad)
113 {
114         unsigned int i;
115
116         for (i = 0; i < pad->entity->info.links; ++i) {
117                 struct media_entity_link *link = &pad->entity->links[i];
118
119                 if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
120                         continue;
121
122                 if (link->source == pad)
123                         return link->sink;
124
125                 if (link->sink == pad)
126                         return link->source;
127         }
128
129         return NULL;
130 }
131
132 /*
133  * media_get_entity_by_name -
134  */
135 struct media_entity *media_get_entity_by_name(struct media_device *media,
136                                               const char *name, size_t length)
137 {
138         unsigned int i;
139
140         for (i = 0; i < media->entities_count; ++i) {
141                 struct media_entity *entity = &media->entities[i];
142
143                 if (strncmp(entity->info.name, name, length) == 0)
144                         return entity;
145         }
146
147         return NULL;
148 }
149
150 /*
151  * media_get_entity_by_id -
152  */
153 struct media_entity *media_get_entity_by_id(struct media_device *media,
154                                             __u32 id)
155 {
156         unsigned int i;
157
158         for (i = 0; i < media->entities_count; ++i) {
159                 struct media_entity *entity = &media->entities[i];
160
161                 if (entity->info.id == id)
162                         return entity;
163         }
164
165         return NULL;
166 }
167
168 /*
169  * media_setup_link -
170  */
171 int media_setup_link(struct media_device *media,
172                      struct media_entity_pad *source,
173                      struct media_entity_pad *sink,
174                      __u32 flags)
175 {
176         struct media_entity_link *link;
177         struct media_user_link ulink;
178         unsigned int i;
179         int ret;
180
181         for (i = 0; i < source->entity->info.links; i++) {
182                 link = &source->entity->links[i];
183
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)
188                         break;
189         }
190
191         if (i == source->entity->info.links) {
192                 printf("%s: Link not found\n", __func__);
193                 return -EINVAL;
194         }
195
196         /* source pad */
197         ulink.source.entity = source->entity->info.id;
198         ulink.source.index = source->index;
199         ulink.source.type = MEDIA_PAD_TYPE_OUTPUT;
200
201         /* sink pad */
202         ulink.sink.entity = sink->entity->info.id;
203         ulink.sink.index = sink->index;
204         ulink.sink.type = MEDIA_PAD_TYPE_INPUT;
205
206         ulink.flags = flags | (link->flags & MEDIA_LINK_FLAG_IMMUTABLE);
207
208         ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
209         if (ret < 0) {
210                 printf("%s: Unable to setup link (%s)\n", __func__,
211                         strerror(errno));
212                 return ret;
213         }
214
215         link->flags = flags;
216         return 0;
217 }
218
219 int media_reset_links(struct media_device *media)
220 {
221         unsigned int i, j;
222         int ret;
223
224         for (i = 0; i < media->entities_count; ++i) {
225                 struct media_entity *entity = &media->entities[i];
226
227                 for (j = 0; j < entity->info.links; j++) {
228                         struct media_entity_link *link = &entity->links[j];
229
230                         if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
231                                 continue;
232
233                         ret = media_setup_link(media, link->source, link->sink,
234                                                link->flags & ~MEDIA_LINK_FLAG_ACTIVE);
235                         if (ret < 0)
236                                 return ret;
237                 }
238         }
239
240         return 0;
241 }
242
243 static void media_print_topology_dot(struct media_device *media)
244 {
245         unsigned int i, j;
246
247         printf("digraph board {\n");
248         printf("\trankdir=TB\n");
249
250         for (i = 0; i < media->entities_count; ++i) {
251                 struct media_entity *entity = &media->entities[i];
252                 unsigned int npads;
253
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);
259                         break;
260
261                 case MEDIA_ENTITY_TYPE_SUBDEV:
262                         printf("\tn%08x [label=\"{{", entity->info.id);
263
264                         for (j = 0, npads = 0; j < entity->info.pads; ++j) {
265                                 if (entity->pads[j].type != MEDIA_PAD_TYPE_INPUT)
266                                         continue;
267
268                                 printf("%s<port%u> %u", npads ? " | " : "", j, j);
269                                 npads++;
270                         }
271
272                         printf("} | %s | {", entity->info.name);
273
274                         for (j = 0, npads = 0; j < entity->info.pads; ++j) {
275                                 if (entity->pads[j].type != MEDIA_PAD_TYPE_OUTPUT)
276                                         continue;
277
278                                 printf("%s<port%u> %u", npads ? " | " : "", j, j);
279                                 npads++;
280                         }
281
282                         printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n");
283                         break;
284
285                 default:
286                         continue;
287                 }
288
289                 for (j = 0; j < entity->info.links; j++) {
290                         struct media_entity_link *link = &entity->links[j];
291
292                         if (link->source->entity != entity)
293                                 continue;
294
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);
298                         printf(" -> ");
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);
302
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]");
307                         printf("\n");
308                 }
309         }
310
311         printf("}\n");
312 }
313
314 static void media_print_topology_text(struct media_device *media)
315 {
316         unsigned int i, j, k;
317         unsigned int padding;
318
319         printf("Device topology\n");
320
321         for (i = 0; i < media->entities_count; ++i) {
322                 struct media_entity *entity = &media->entities[i];
323
324                 padding = printf("- entity %u: ", entity->info.id);
325                 printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
326                         entity->info.pads, entity->info.pads > 1 ? "s" : "",
327                         entity->info.links, entity->info.links > 1 ? "s" : "");
328                 printf("%*ctype %s subtype %s\n", padding, ' ',
329                         media_entity_type_to_string(entity->info.type),
330                         media_entity_subtype_to_string(entity->info.type, entity->info.subtype));
331                 if (entity->devname[0])
332                         printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
333
334                 for (j = 0; j < entity->info.pads; j++) {
335                         struct media_entity_pad *pad = &entity->pads[j];
336
337                         printf("\tpad%u: %s ", j, media_pad_type_to_string(pad->type));
338
339                         if (entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV)
340                                 v4l2_subdev_print_format(entity, j, V4L2_SUBDEV_FORMAT_ACTIVE);
341
342                         printf("\n");
343
344                         for (k = 0; k < entity->info.links; k++) {
345                                 struct media_entity_link *link = &entity->links[k];
346
347                                 if (link->source->entity != entity ||
348                                     link->source->index != j)
349                                         continue;
350
351                                 printf("\t\t-> '%s':pad%u [",
352                                         link->sink->entity->info.name, link->sink->index);
353
354                                 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
355                                         printf("IMMUTABLE,");
356                                 if (link->flags & MEDIA_LINK_FLAG_ACTIVE)
357                                         printf("ACTIVE");
358
359                                 printf("]\n");
360                         }
361                 }
362                 printf("\n");
363         }
364 }
365
366 void media_print_topology(struct media_device *media, int dot)
367 {
368         if (dot)
369                 media_print_topology_dot(media);
370         else
371                 media_print_topology_text(media);
372 }
373
374 static int media_enum_links(struct media_device *media)
375 {
376         __u32 id;
377         int ret = 0;
378
379         for (id = 1; id <= media->entities_count; id++) {
380                 struct media_entity *entity = &media->entities[id - 1];
381                 struct media_user_links links;
382                 unsigned int i;
383
384                 links.entity = entity->info.id;
385                 links.pads = malloc(entity->info.pads * sizeof(struct media_user_pad));
386                 links.links = malloc(entity->info.links * sizeof(struct media_user_link));
387
388                 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
389                         printf("%s: Unable to enumerate pads and links (%s).\n",
390                                 __func__, strerror(errno));
391                         free(links.pads);
392                         free(links.links);
393                         return -errno;
394                 }
395
396                 for (i = 0; i < entity->info.pads; ++i) {
397                         entity->pads[i].entity = entity;
398                         entity->pads[i].type = links.pads[i].type;
399                         entity->pads[i].index = links.pads[i].index;
400                 }
401
402                 for (i = 0; i < entity->info.links; ++i) {
403                         struct media_user_link *link = &links.links[i];
404                         struct media_entity *source;
405                         struct media_entity *sink;
406
407                         source = media_get_entity_by_id(media, link->source.entity);
408                         sink = media_get_entity_by_id(media, link->sink.entity);
409
410                         if (source == NULL || sink == NULL) {
411                                 printf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
412                                         id, i, link->source.entity, link->source.index,
413                                         link->sink.entity, link->sink.index);
414                                 ret = -EINVAL;
415                         }
416
417                         entity->links[i].source = &source->pads[link->source.index];
418                         entity->links[i].sink = &sink->pads[link->sink.index];
419                         entity->links[i].flags = links.links[i].flags;
420                 }
421
422                 free(links.pads);
423                 free(links.links);
424         }
425
426         return ret;
427 }
428
429 static int media_enum_entities(struct media_device *media)
430 {
431         struct media_entity *entity;
432         struct stat devstat;
433         unsigned int size;
434         char devname[32];
435         char sysname[32];
436         char target[1024];
437         char *p;
438         __u32 id;
439         int ret;
440
441         for (id = 0; ; id = entity->info.id) {
442                 size = (media->entities_count + 1) * sizeof(*media->entities);
443                 media->entities = realloc(media->entities, size);
444
445                 entity = &media->entities[media->entities_count];
446                 memset(entity, 0, sizeof(*entity));
447                 entity->fd = -1;
448                 entity->info.id = id | MEDIA_ENTITY_ID_FLAG_NEXT;
449
450                 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
451                 if (ret < 0) {
452                         if (errno == EINVAL)
453                                 break;
454                         return -errno;
455                 }
456
457                 entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
458                 entity->links = malloc(entity->info.links * sizeof(*entity->links));
459                 if (entity->pads == NULL || entity->links == NULL)
460                         return -ENOMEM;
461
462                 media->entities_count++;
463
464                 /* Find the corresponding device name. */
465                 if ((entity->info.type != MEDIA_ENTITY_TYPE_NODE ||
466                      entity->info.type != MEDIA_NODE_TYPE_V4L) &&
467                     (entity->info.type != MEDIA_ENTITY_TYPE_SUBDEV))
468                         continue;
469
470                 sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
471                         entity->info.v4l.minor);
472                 ret = readlink(sysname, target, sizeof(target));
473                 if (ret < 0)
474                         continue;
475
476                 target[ret] = '\0';
477                 p = strrchr(target, '/');
478                 if (p == NULL)
479                         continue;
480
481                 sprintf(devname, "/dev/%s", p + 1);
482                 ret = stat(devname, &devstat);
483                 if (ret < 0)
484                         continue;
485
486                 /* Sanity check: udev might have reordered the device nodes.
487                  * Make sure the major/minor match. We should really use
488                  * libudev.
489                  */
490                 if (major(devstat.st_rdev) == entity->info.v4l.major &&
491                     minor(devstat.st_rdev) == entity->info.v4l.minor)
492                         strcpy(entity->devname, devname);
493         }
494
495         return 0;
496 }
497
498 /*
499  * media_open -
500  */
501 struct media_device *media_open(const char *name, int verbose)
502 {
503         struct media_device *media;
504         int ret;
505
506         media = malloc(sizeof(*media));
507         if (media == NULL) {
508                 printf("%s: unable to allocate memory\n", __func__);
509                 return NULL;
510         }
511         memset(media, 0, sizeof(*media));
512
513         if (verbose)
514                 printf("Opening media device %s\n", name);
515         media->fd = open(name, O_RDWR);
516         if (media->fd < 0) {
517                 media_close(media);
518                 printf("%s: Can't open media device %s\n", __func__, name);
519                 return NULL;
520         }
521
522         if (verbose)
523                 printf("Enumerating entities\n");
524
525         ret = media_enum_entities(media);
526         if (ret < 0) {
527                 printf("%s: Unable to enumerate entities for device %s (%s)\n",
528                         __func__, name, strerror(-ret));
529                 media_close(media);
530                 return NULL;
531         }
532
533         if (verbose) {
534                 printf("Found %u entities\n", media->entities_count);
535                 printf("Enumerating pads and links\n");
536         }
537
538         ret = media_enum_links(media);
539         if (ret < 0) {
540                 printf("%s: Unable to enumerate pads and linksfor device %s\n",
541                         __func__, name);
542                 media_close(media);
543                 return NULL;
544         }
545
546         return media;
547 }
548
549 /*
550  * media_close -
551  */
552 void media_close(struct media_device *media)
553 {
554         unsigned int i;
555
556         if (media->fd != -1)
557                 close(media->fd);
558
559         for (i = 0; i < media->entities_count; ++i) {
560                 struct media_entity *entity = &media->entities[i];
561
562                 free(entity->pads);
563                 free(entity->links);
564                 if (entity->fd != -1)
565                         close(entity->fd);
566         }
567
568         free(media->entities);
569         free(media);
570 }
571