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>
43 struct media_pad *media_entity_remote_source(struct media_pad *pad)
47 if (!(pad->flags & MEDIA_PAD_FL_SINK))
50 for (i = 0; i < pad->entity->num_links; ++i) {
51 struct media_link *link = &pad->entity->links[i];
53 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
56 if (link->sink == pad)
63 struct media_entity *media_get_entity_by_name(struct media_device *media,
64 const char *name, size_t length)
68 /* A match is impossible if the entity name is longer than the maximum
69 * size we can get from the kernel.
71 if (length >= FIELD_SIZEOF(struct media_entity_desc, name))
74 for (i = 0; i < media->entities_count; ++i) {
75 struct media_entity *entity = &media->entities[i];
77 if (strncmp(entity->info.name, name, length) == 0 &&
78 entity->info.name[length] == '\0')
85 struct media_entity *media_get_entity_by_id(struct media_device *media,
88 bool next = id & MEDIA_ENT_ID_FLAG_NEXT;
91 id &= ~MEDIA_ENT_ID_FLAG_NEXT;
93 for (i = 0; i < media->entities_count; ++i) {
94 struct media_entity *entity = &media->entities[i];
96 if ((entity->info.id == id && !next) ||
97 (entity->info.id > id && next))
104 int media_setup_link(struct media_device *media,
105 struct media_pad *source,
106 struct media_pad *sink,
109 struct media_link *link;
110 struct media_link_desc ulink;
114 for (i = 0; i < source->entity->num_links; i++) {
115 link = &source->entity->links[i];
117 if (link->source->entity == source->entity &&
118 link->source->index == source->index &&
119 link->sink->entity == sink->entity &&
120 link->sink->index == sink->index)
124 if (i == source->entity->num_links) {
125 media_dbg(media, "%s: Link not found\n", __func__);
130 ulink.source.entity = source->entity->info.id;
131 ulink.source.index = source->index;
132 ulink.source.flags = MEDIA_PAD_FL_SOURCE;
135 ulink.sink.entity = sink->entity->info.id;
136 ulink.sink.index = sink->index;
137 ulink.sink.flags = MEDIA_PAD_FL_SINK;
139 ulink.flags = flags | (link->flags & MEDIA_LNK_FL_IMMUTABLE);
141 ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
143 media_dbg(media, "%s: Unable to setup link (%s)\n",
144 __func__, strerror(errno));
148 link->flags = ulink.flags;
149 link->twin->flags = ulink.flags;
153 int media_reset_links(struct media_device *media)
158 for (i = 0; i < media->entities_count; ++i) {
159 struct media_entity *entity = &media->entities[i];
161 for (j = 0; j < entity->num_links; j++) {
162 struct media_link *link = &entity->links[j];
164 if (link->flags & MEDIA_LNK_FL_IMMUTABLE ||
165 link->source->entity != entity)
168 ret = media_setup_link(media, link->source, link->sink,
169 link->flags & ~MEDIA_LNK_FL_ENABLED);
178 static struct media_link *media_entity_add_link(struct media_entity *entity)
180 if (entity->num_links >= entity->max_links) {
181 struct media_link *links = entity->links;
182 unsigned int max_links = entity->max_links * 2;
185 links = realloc(links, max_links * sizeof *links);
189 for (i = 0; i < entity->num_links; ++i)
190 links[i].twin->twin = &links[i];
192 entity->max_links = max_links;
193 entity->links = links;
196 return &entity->links[entity->num_links++];
199 static int media_enum_links(struct media_device *media)
204 for (id = 1; id <= media->entities_count; id++) {
205 struct media_entity *entity = &media->entities[id - 1];
206 struct media_links_enum links;
209 links.entity = entity->info.id;
210 links.pads = calloc(entity->info.pads, sizeof(struct media_pad_desc));
211 links.links = calloc(entity->info.links, sizeof(struct media_link_desc));
213 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
215 "%s: Unable to enumerate pads and links (%s).\n",
216 __func__, strerror(errno));
222 for (i = 0; i < entity->info.pads; ++i) {
223 entity->pads[i].entity = entity;
224 entity->pads[i].index = links.pads[i].index;
225 entity->pads[i].flags = links.pads[i].flags;
228 for (i = 0; i < entity->info.links; ++i) {
229 struct media_link_desc *link = &links.links[i];
230 struct media_link *fwdlink;
231 struct media_link *backlink;
232 struct media_entity *source;
233 struct media_entity *sink;
235 source = media_get_entity_by_id(media, link->source.entity);
236 sink = media_get_entity_by_id(media, link->sink.entity);
238 if (source == NULL || sink == NULL) {
240 "WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
241 id, i, link->source.entity,
247 fwdlink = media_entity_add_link(source);
248 fwdlink->source = &source->pads[link->source.index];
249 fwdlink->sink = &sink->pads[link->sink.index];
250 fwdlink->flags = link->flags;
252 backlink = media_entity_add_link(sink);
253 backlink->source = &source->pads[link->source.index];
254 backlink->sink = &sink->pads[link->sink.index];
255 backlink->flags = link->flags;
257 fwdlink->twin = backlink;
258 backlink->twin = fwdlink;
273 static inline int media_udev_open(struct udev **udev)
281 static inline void media_udev_close(struct udev *udev)
287 static int media_get_devname_udev(struct udev *udev,
288 struct media_entity *entity)
290 struct udev_device *device;
298 devnum = makedev(entity->info.v4l.major, entity->info.v4l.minor);
299 media_dbg(entity->media, "looking up device: %u:%u\n",
300 major(devnum), minor(devnum));
301 device = udev_device_new_from_devnum(udev, 'c', devnum);
303 p = udev_device_get_devnode(device);
305 strncpy(entity->devname, p, sizeof(entity->devname));
306 entity->devname[sizeof(entity->devname) - 1] = '\0';
311 udev_device_unref(device);
316 #else /* HAVE_LIBUDEV */
320 static inline int media_udev_open(struct udev **udev) { return 0; }
322 static inline void media_udev_close(struct udev *udev) { }
324 static inline int media_get_devname_udev(struct udev *udev,
325 struct media_entity *entity)
330 #endif /* HAVE_LIBUDEV */
332 static int media_get_devname_sysfs(struct media_entity *entity)
341 sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
342 entity->info.v4l.minor);
343 ret = readlink(sysname, target, sizeof(target));
348 p = strrchr(target, '/');
352 sprintf(devname, "/dev/%s", p + 1);
353 ret = stat(devname, &devstat);
357 /* Sanity check: udev might have reordered the device nodes.
358 * Make sure the major/minor match. We should really use
361 if (major(devstat.st_rdev) == entity->info.v4l.major &&
362 minor(devstat.st_rdev) == entity->info.v4l.minor)
363 strcpy(entity->devname, devname);
368 static int media_enum_entities(struct media_device *media)
370 struct media_entity *entity;
376 ret = media_udev_open(&udev);
378 media_dbg(media, "Can't get udev context\n");
380 for (id = 0, ret = 0; ; id = entity->info.id) {
381 size = (media->entities_count + 1) * sizeof(*media->entities);
382 media->entities = realloc(media->entities, size);
384 entity = &media->entities[media->entities_count];
385 memset(entity, 0, sizeof(*entity));
387 entity->info.id = id | MEDIA_ENT_ID_FLAG_NEXT;
388 entity->media = media;
390 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
392 ret = errno != EINVAL ? -errno : 0;
396 /* Number of links (for outbound links) plus number of pads (for
397 * inbound links) is a good safe initial estimate of the total
400 entity->max_links = entity->info.pads + entity->info.links;
402 entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
403 entity->links = malloc(entity->max_links * sizeof(*entity->links));
404 if (entity->pads == NULL || entity->links == NULL) {
409 media->entities_count++;
411 /* Find the corresponding device name. */
412 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE &&
413 media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV)
416 /* Try to get the device name via udev */
417 if (!media_get_devname_udev(udev, entity))
420 /* Fall back to get the device name via sysfs */
421 media_get_devname_sysfs(entity);
424 media_udev_close(udev);
428 static void media_debug_default(void *ptr, ...)
432 void media_debug_set_handler(struct media_device *media,
433 void (*debug_handler)(void *, ...),
437 media->debug_handler = debug_handler;
438 media->debug_priv = debug_priv;
440 media->debug_handler = media_debug_default;
441 media->debug_priv = NULL;
445 struct media_device *media_open_debug(
446 const char *name, void (*debug_handler)(void *, ...),
449 struct media_device *media;
452 media = calloc(1, sizeof(*media));
456 media_debug_set_handler(media, debug_handler, debug_priv);
458 media_dbg(media, "Opening media device %s\n", name);
460 media->fd = open(name, O_RDWR);
463 media_dbg(media, "%s: Can't open media device %s\n",
468 ret = ioctl(media->fd, MEDIA_IOC_DEVICE_INFO, &media->info);
470 media_dbg(media, "%s: Unable to retrieve media device "
471 "information for device %s (%s)\n", __func__,
472 name, strerror(errno));
477 media_dbg(media, "Enumerating entities\n");
479 ret = media_enum_entities(media);
483 "%s: Unable to enumerate entities for device %s (%s)\n",
484 __func__, name, strerror(-ret));
489 media_dbg(media, "Found %u entities\n", media->entities_count);
490 media_dbg(media, "Enumerating pads and links\n");
492 ret = media_enum_links(media);
495 "%s: Unable to enumerate pads and linksfor device %s\n",
504 struct media_device *media_open(const char *name)
506 return media_open_debug(name, NULL, NULL);
509 void media_close(struct media_device *media)
516 for (i = 0; i < media->entities_count; ++i) {
517 struct media_entity *entity = &media->entities[i];
521 if (entity->fd != -1)
525 free(media->entities);
529 struct media_pad *media_parse_pad(struct media_device *media,
530 const char *p, char **endp)
532 unsigned int entity_id, pad;
533 struct media_entity *entity;
536 for (; isspace(*p); ++p);
539 for (end = (char *)p + 1; *end && *end != '"'; ++end);
541 media_dbg(media, "missing matching '\"'\n");
545 entity = media_get_entity_by_name(media, p + 1, end - p - 1);
546 if (entity == NULL) {
547 media_dbg(media, "no such entity \"%.*s\"\n", end - p - 1, p + 1);
553 entity_id = strtoul(p, &end, 10);
554 entity = media_get_entity_by_id(media, entity_id);
555 if (entity == NULL) {
556 media_dbg(media, "no such entity %d\n", entity_id);
560 for (; isspace(*end); ++end);
563 media_dbg(media, "Expected ':'\n", *end);
567 for (p = end + 1; isspace(*p); ++p);
569 pad = strtoul(p, &end, 10);
571 if (pad >= entity->info.pads) {
572 media_dbg(media, "No pad '%d' on entity \"%s\". Maximum pad number is %d\n",
573 pad, entity->info.name, entity->info.pads - 1);
577 for (p = end; isspace(*p); ++p);
581 return &entity->pads[pad];
584 struct media_link *media_parse_link(struct media_device *media,
585 const char *p, char **endp)
587 struct media_link *link;
588 struct media_pad *source;
589 struct media_pad *sink;
593 source = media_parse_pad(media, p, &end);
597 if (end[0] != '-' || end[1] != '>') {
598 media_dbg(media, "Expected '->'\n");
604 sink = media_parse_pad(media, p, &end);
610 for (i = 0; i < source->entity->num_links; i++) {
611 link = &source->entity->links[i];
613 if (link->source == source && link->sink == sink)
617 media_dbg(media, "No link between \"%s\":%d and \"%s\":%d\n",
618 source->entity->info.name, source->index,
619 sink->entity->info.name, sink->index);
623 int media_parse_setup_link(struct media_device *media,
624 const char *p, char **endp)
626 struct media_link *link;
630 link = media_parse_link(media, p, &end);
633 "%s: Unable to parse link\n", __func__);
639 media_dbg(media, "Unable to parse link flags: expected '['.\n");
643 flags = strtoul(p, &end, 10);
644 for (p = end; isspace(*p); p++);
646 media_dbg(media, "Unable to parse link flags: expected ']'.\n");
650 for (; isspace(*p); p++);
654 "Setting up link %u:%u -> %u:%u [%u]\n",
655 link->source->entity->info.id, link->source->index,
656 link->sink->entity->info.id, link->sink->index,
659 return media_setup_link(media, link->source, link->sink, flags);
662 int media_parse_setup_links(struct media_device *media, const char *p)
668 ret = media_parse_setup_link(media, p, &end);
673 } while (*end == ',');
675 return *end ? -EINVAL : 0;