2 * Media controller interface library
4 * Copyright (C) 2010-2011 Ideas on board SPRL
6 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/ioctl.h>
26 #include <sys/types.h>
37 #include <linux/media.h>
38 #include <linux/videodev2.h>
41 #include "mediactl-priv.h"
44 /* -----------------------------------------------------------------------------
48 struct media_pad *media_entity_remote_source(struct media_pad *pad)
52 if (!(pad->flags & MEDIA_PAD_FL_SINK))
55 for (i = 0; i < pad->entity->num_links; ++i) {
56 struct media_link *link = &pad->entity->links[i];
58 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
61 if (link->sink == pad)
68 struct media_entity *media_get_entity_by_name(struct media_device *media,
69 const char *name, size_t length)
73 /* A match is impossible if the entity name is longer than the maximum
74 * size we can get from the kernel.
76 if (length >= FIELD_SIZEOF(struct media_entity_desc, name))
79 for (i = 0; i < media->entities_count; ++i) {
80 struct media_entity *entity = &media->entities[i];
82 if (strncmp(entity->info.name, name, length) == 0 &&
83 entity->info.name[length] == '\0')
90 struct media_entity *media_get_entity_by_id(struct media_device *media,
93 bool next = id & MEDIA_ENT_ID_FLAG_NEXT;
96 id &= ~MEDIA_ENT_ID_FLAG_NEXT;
98 for (i = 0; i < media->entities_count; ++i) {
99 struct media_entity *entity = &media->entities[i];
101 if ((entity->info.id == id && !next) ||
102 (entity->info.id > id && next))
109 unsigned int media_get_entities_count(struct media_device *media)
111 return media->entities_count;
114 struct media_entity *media_get_entity(struct media_device *media, unsigned int index)
116 if (index >= media->entities_count)
119 return &media->entities[index];
122 const struct media_pad *media_entity_get_pad(struct media_entity *entity, unsigned int index)
124 if (index >= entity->info.pads)
127 return &entity->pads[index];
130 unsigned int media_entity_get_links_count(struct media_entity *entity)
132 return entity->num_links;
135 const struct media_link *media_entity_get_link(struct media_entity *entity, unsigned int index)
137 if (index >= entity->num_links)
140 return &entity->links[index];
143 const char *media_entity_get_devname(struct media_entity *entity)
145 return entity->devname[0] ? entity->devname : NULL;
148 struct media_entity *media_get_default_entity(struct media_device *media,
152 case MEDIA_ENT_T_DEVNODE_V4L:
153 return media->def.v4l;
154 case MEDIA_ENT_T_DEVNODE_FB:
155 return media->def.fb;
156 case MEDIA_ENT_T_DEVNODE_ALSA:
157 return media->def.alsa;
158 case MEDIA_ENT_T_DEVNODE_DVB:
159 return media->def.dvb;
163 const struct media_device_info *media_get_info(struct media_device *media)
168 const char *media_get_devnode(struct media_device *media)
170 return media->devnode;
173 const struct media_entity_desc *media_entity_get_info(struct media_entity *entity)
175 return &entity->info;
178 /* -----------------------------------------------------------------------------
182 static int media_device_open(struct media_device *media)
189 media_dbg(media, "Opening media device %s\n", media->devnode);
191 media->fd = open(media->devnode, O_RDWR);
194 media_dbg(media, "%s: Can't open media device %s\n",
195 __func__, media->devnode);
202 static void media_device_close(struct media_device *media)
204 if (media->fd != -1) {
210 /* -----------------------------------------------------------------------------
214 int media_setup_link(struct media_device *media,
215 struct media_pad *source,
216 struct media_pad *sink,
219 struct media_link *link;
220 struct media_link_desc ulink;
224 ret = media_device_open(media);
228 for (i = 0; i < source->entity->num_links; i++) {
229 link = &source->entity->links[i];
231 if (link->source->entity == source->entity &&
232 link->source->index == source->index &&
233 link->sink->entity == sink->entity &&
234 link->sink->index == sink->index)
238 if (i == source->entity->num_links) {
239 media_dbg(media, "%s: Link not found\n", __func__);
245 ulink.source.entity = source->entity->info.id;
246 ulink.source.index = source->index;
247 ulink.source.flags = MEDIA_PAD_FL_SOURCE;
250 ulink.sink.entity = sink->entity->info.id;
251 ulink.sink.index = sink->index;
252 ulink.sink.flags = MEDIA_PAD_FL_SINK;
254 ulink.flags = flags | (link->flags & MEDIA_LNK_FL_IMMUTABLE);
256 ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
259 media_dbg(media, "%s: Unable to setup link (%s)\n",
260 __func__, strerror(errno));
264 link->flags = ulink.flags;
265 link->twin->flags = ulink.flags;
270 media_device_close(media);
274 int media_reset_links(struct media_device *media)
279 for (i = 0; i < media->entities_count; ++i) {
280 struct media_entity *entity = &media->entities[i];
282 for (j = 0; j < entity->num_links; j++) {
283 struct media_link *link = &entity->links[j];
285 if (link->flags & MEDIA_LNK_FL_IMMUTABLE ||
286 link->source->entity != entity)
289 ret = media_setup_link(media, link->source, link->sink,
290 link->flags & ~MEDIA_LNK_FL_ENABLED);
299 /* -----------------------------------------------------------------------------
300 * Entities, pads and links enumeration
303 static struct media_link *media_entity_add_link(struct media_entity *entity)
305 if (entity->num_links >= entity->max_links) {
306 struct media_link *links = entity->links;
307 unsigned int max_links = entity->max_links * 2;
310 links = realloc(links, max_links * sizeof *links);
314 for (i = 0; i < entity->num_links; ++i)
315 links[i].twin->twin = &links[i];
317 entity->max_links = max_links;
318 entity->links = links;
321 return &entity->links[entity->num_links++];
324 static int media_enum_links(struct media_device *media)
329 for (id = 1; id <= media->entities_count; id++) {
330 struct media_entity *entity = &media->entities[id - 1];
331 struct media_links_enum links;
334 links.entity = entity->info.id;
335 links.pads = calloc(entity->info.pads, sizeof(struct media_pad_desc));
336 links.links = calloc(entity->info.links, sizeof(struct media_link_desc));
338 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
341 "%s: Unable to enumerate pads and links (%s).\n",
342 __func__, strerror(errno));
348 for (i = 0; i < entity->info.pads; ++i) {
349 entity->pads[i].entity = entity;
350 entity->pads[i].index = links.pads[i].index;
351 entity->pads[i].flags = links.pads[i].flags;
354 for (i = 0; i < entity->info.links; ++i) {
355 struct media_link_desc *link = &links.links[i];
356 struct media_link *fwdlink;
357 struct media_link *backlink;
358 struct media_entity *source;
359 struct media_entity *sink;
361 source = media_get_entity_by_id(media, link->source.entity);
362 sink = media_get_entity_by_id(media, link->sink.entity);
364 if (source == NULL || sink == NULL) {
366 "WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
367 id, i, link->source.entity,
373 fwdlink = media_entity_add_link(source);
374 fwdlink->source = &source->pads[link->source.index];
375 fwdlink->sink = &sink->pads[link->sink.index];
376 fwdlink->flags = link->flags;
378 backlink = media_entity_add_link(sink);
379 backlink->source = &source->pads[link->source.index];
380 backlink->sink = &sink->pads[link->sink.index];
381 backlink->flags = link->flags;
383 fwdlink->twin = backlink;
384 backlink->twin = fwdlink;
399 static inline int media_udev_open(struct udev **udev)
407 static inline void media_udev_close(struct udev *udev)
413 static int media_get_devname_udev(struct udev *udev,
414 struct media_entity *entity)
416 struct udev_device *device;
424 devnum = makedev(entity->info.v4l.major, entity->info.v4l.minor);
425 media_dbg(entity->media, "looking up device: %u:%u\n",
426 major(devnum), minor(devnum));
427 device = udev_device_new_from_devnum(udev, 'c', devnum);
429 p = udev_device_get_devnode(device);
431 strncpy(entity->devname, p, sizeof(entity->devname));
432 entity->devname[sizeof(entity->devname) - 1] = '\0';
437 udev_device_unref(device);
442 #else /* HAVE_LIBUDEV */
446 static inline int media_udev_open(struct udev **udev) { return 0; }
448 static inline void media_udev_close(struct udev *udev) { }
450 static inline int media_get_devname_udev(struct udev *udev,
451 struct media_entity *entity)
456 #endif /* HAVE_LIBUDEV */
458 static int media_get_devname_sysfs(struct media_entity *entity)
467 sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
468 entity->info.v4l.minor);
469 ret = readlink(sysname, target, sizeof(target));
474 p = strrchr(target, '/');
478 sprintf(devname, "/dev/%s", p + 1);
479 ret = stat(devname, &devstat);
483 /* Sanity check: udev might have reordered the device nodes.
484 * Make sure the major/minor match. We should really use
487 if (major(devstat.st_rdev) == entity->info.v4l.major &&
488 minor(devstat.st_rdev) == entity->info.v4l.minor)
489 strcpy(entity->devname, devname);
494 static int media_enum_entities(struct media_device *media)
496 struct media_entity *entity;
502 ret = media_udev_open(&udev);
504 media_dbg(media, "Can't get udev context\n");
506 for (id = 0, ret = 0; ; id = entity->info.id) {
507 size = (media->entities_count + 1) * sizeof(*media->entities);
508 media->entities = realloc(media->entities, size);
510 entity = &media->entities[media->entities_count];
511 memset(entity, 0, sizeof(*entity));
513 entity->info.id = id | MEDIA_ENT_ID_FLAG_NEXT;
514 entity->media = media;
516 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
518 ret = errno != EINVAL ? -errno : 0;
522 /* Number of links (for outbound links) plus number of pads (for
523 * inbound links) is a good safe initial estimate of the total
526 entity->max_links = entity->info.pads + entity->info.links;
528 entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
529 entity->links = malloc(entity->max_links * sizeof(*entity->links));
530 if (entity->pads == NULL || entity->links == NULL) {
535 media->entities_count++;
537 if (entity->info.flags & MEDIA_ENT_FL_DEFAULT) {
538 switch (entity->info.type) {
539 case MEDIA_ENT_T_DEVNODE_V4L:
540 media->def.v4l = entity;
542 case MEDIA_ENT_T_DEVNODE_FB:
543 media->def.fb = entity;
545 case MEDIA_ENT_T_DEVNODE_ALSA:
546 media->def.alsa = entity;
548 case MEDIA_ENT_T_DEVNODE_DVB:
549 media->def.dvb = entity;
554 /* Find the corresponding device name. */
555 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE &&
556 media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV)
559 /* Try to get the device name via udev */
560 if (!media_get_devname_udev(udev, entity))
563 /* Fall back to get the device name via sysfs */
564 media_get_devname_sysfs(entity);
567 media_udev_close(udev);
571 int media_device_enumerate(struct media_device *media)
578 ret = media_device_open(media);
582 ret = ioctl(media->fd, MEDIA_IOC_DEVICE_INFO, &media->info);
585 media_dbg(media, "%s: Unable to retrieve media device "
586 "information for device %s (%s)\n", __func__,
587 media->devnode, strerror(errno));
591 media_dbg(media, "Enumerating entities\n");
593 ret = media_enum_entities(media);
596 "%s: Unable to enumerate entities for device %s (%s)\n",
597 __func__, media->devnode, strerror(-ret));
601 media_dbg(media, "Found %u entities\n", media->entities_count);
602 media_dbg(media, "Enumerating pads and links\n");
604 ret = media_enum_links(media);
607 "%s: Unable to enumerate pads and linksfor device %s\n",
608 __func__, media->devnode);
615 media_device_close(media);
619 /* -----------------------------------------------------------------------------
623 static void media_debug_default(void *ptr, ...)
627 void media_debug_set_handler(struct media_device *media,
628 void (*debug_handler)(void *, ...),
632 media->debug_handler = debug_handler;
633 media->debug_priv = debug_priv;
635 media->debug_handler = media_debug_default;
636 media->debug_priv = NULL;
640 struct media_device *media_device_new(const char *devnode)
642 struct media_device *media;
645 media = calloc(1, sizeof(*media));
652 media_debug_set_handler(media, NULL, NULL);
654 media->devnode = strdup(devnode);
655 if (media->devnode == NULL) {
656 media_device_unref(media);
663 struct media_device *media_device_ref(struct media_device *media)
669 void media_device_unref(struct media_device *media)
674 if (media->refcount > 0)
677 for (i = 0; i < media->entities_count; ++i) {
678 struct media_entity *entity = &media->entities[i];
682 if (entity->fd != -1)
686 free(media->entities);
687 free(media->devnode);
691 /* -----------------------------------------------------------------------------
695 struct media_pad *media_parse_pad(struct media_device *media,
696 const char *p, char **endp)
698 unsigned int entity_id, pad;
699 struct media_entity *entity;
702 /* endp can be NULL. To avoid spreading NULL checks across the function,
703 * set endp to &end in that case.
708 for (; isspace(*p); ++p);
710 if (*p == '"' || *p == '\'') {
711 for (end = (char *)p + 1; *end && *end != '"' && *end != '\''; ++end);
712 if (*end != '"' && *end != '\'') {
713 media_dbg(media, "missing matching '\"'\n");
718 entity = media_get_entity_by_name(media, p + 1, end - p - 1);
719 if (entity == NULL) {
720 media_dbg(media, "no such entity \"%.*s\"\n", end - p - 1, p + 1);
721 *endp = (char *)p + 1;
727 entity_id = strtoul(p, &end, 10);
728 entity = media_get_entity_by_id(media, entity_id);
729 if (entity == NULL) {
730 media_dbg(media, "no such entity %d\n", entity_id);
735 for (; isspace(*end); ++end);
738 media_dbg(media, "Expected ':'\n", *end);
743 for (p = end + 1; isspace(*p); ++p);
745 pad = strtoul(p, &end, 10);
747 if (pad >= entity->info.pads) {
748 media_dbg(media, "No pad '%d' on entity \"%s\". Maximum pad number is %d\n",
749 pad, entity->info.name, entity->info.pads - 1);
754 for (p = end; isspace(*p); ++p);
757 return &entity->pads[pad];
760 struct media_link *media_parse_link(struct media_device *media,
761 const char *p, char **endp)
763 struct media_link *link;
764 struct media_pad *source;
765 struct media_pad *sink;
769 source = media_parse_pad(media, p, &end);
770 if (source == NULL) {
775 if (end[0] != '-' || end[1] != '>') {
777 media_dbg(media, "Expected '->'\n");
783 sink = media_parse_pad(media, p, &end);
791 for (i = 0; i < source->entity->num_links; i++) {
792 link = &source->entity->links[i];
794 if (link->source == source && link->sink == sink)
798 media_dbg(media, "No link between \"%s\":%d and \"%s\":%d\n",
799 source->entity->info.name, source->index,
800 sink->entity->info.name, sink->index);
804 int media_parse_setup_link(struct media_device *media,
805 const char *p, char **endp)
807 struct media_link *link;
811 link = media_parse_link(media, p, &end);
814 "%s: Unable to parse link\n", __func__);
821 media_dbg(media, "Unable to parse link flags: expected '['.\n");
822 *endp = (char *)p - 1;
826 flags = strtoul(p, &end, 10);
827 for (p = end; isspace(*p); p++);
829 media_dbg(media, "Unable to parse link flags: expected ']'.\n");
830 *endp = (char *)p - 1;
834 for (; isspace(*p); p++);
838 "Setting up link %u:%u -> %u:%u [%u]\n",
839 link->source->entity->info.id, link->source->index,
840 link->sink->entity->info.id, link->sink->index,
843 return media_setup_link(media, link->source, link->sink, flags);
846 void media_print_streampos(struct media_device *media, const char *p,
858 media_dbg(media, "\n");
859 media_dbg(media, " %s\n", p);
860 media_dbg(media, " %*s\n", pos, "^");
863 int media_parse_setup_links(struct media_device *media, const char *p)
869 ret = media_parse_setup_link(media, p, &end);
871 media_print_streampos(media, p, end);
876 } while (*end == ',');
878 return *end ? -EINVAL : 0;