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>
36 #include <linux/media.h>
37 #include <linux/videodev2.h>
42 struct media_pad *media_entity_remote_source(struct media_pad *pad)
46 if (!(pad->flags & MEDIA_PAD_FL_SINK))
49 for (i = 0; i < pad->entity->num_links; ++i) {
50 struct media_link *link = &pad->entity->links[i];
52 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
55 if (link->sink == pad)
62 struct media_entity *media_get_entity_by_name(struct media_device *media,
63 const char *name, size_t length)
67 /* A match is impossible if the entity name is longer than the maximum
68 * size we can get from the kernel.
70 if (length >= FIELD_SIZEOF(struct media_entity_desc, name))
73 for (i = 0; i < media->entities_count; ++i) {
74 struct media_entity *entity = &media->entities[i];
76 if (strncmp(entity->info.name, name, length) == 0 &&
77 entity->info.name[length] == '\0')
84 struct media_entity *media_get_entity_by_id(struct media_device *media,
87 bool next = id & MEDIA_ENT_ID_FLAG_NEXT;
90 id &= ~MEDIA_ENT_ID_FLAG_NEXT;
92 for (i = 0; i < media->entities_count; ++i) {
93 struct media_entity *entity = &media->entities[i];
95 if ((entity->info.id == id && !next) ||
96 (entity->info.id > id && next))
103 int media_setup_link(struct media_device *media,
104 struct media_pad *source,
105 struct media_pad *sink,
108 struct media_link *link;
109 struct media_link_desc ulink;
113 for (i = 0; i < source->entity->num_links; i++) {
114 link = &source->entity->links[i];
116 if (link->source->entity == source->entity &&
117 link->source->index == source->index &&
118 link->sink->entity == sink->entity &&
119 link->sink->index == sink->index)
123 if (i == source->entity->num_links) {
124 media_dbg(media, "%s: Link not found\n", __func__);
129 ulink.source.entity = source->entity->info.id;
130 ulink.source.index = source->index;
131 ulink.source.flags = MEDIA_PAD_FL_SOURCE;
134 ulink.sink.entity = sink->entity->info.id;
135 ulink.sink.index = sink->index;
136 ulink.sink.flags = MEDIA_PAD_FL_SINK;
138 ulink.flags = flags | (link->flags & MEDIA_LNK_FL_IMMUTABLE);
140 ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
142 media_dbg(media, "%s: Unable to setup link (%s)\n",
143 __func__, strerror(errno));
147 link->flags = ulink.flags;
148 link->twin->flags = ulink.flags;
152 int media_reset_links(struct media_device *media)
157 for (i = 0; i < media->entities_count; ++i) {
158 struct media_entity *entity = &media->entities[i];
160 for (j = 0; j < entity->num_links; j++) {
161 struct media_link *link = &entity->links[j];
163 if (link->flags & MEDIA_LNK_FL_IMMUTABLE ||
164 link->source->entity != entity)
167 ret = media_setup_link(media, link->source, link->sink,
168 link->flags & ~MEDIA_LNK_FL_ENABLED);
177 static struct media_link *media_entity_add_link(struct media_entity *entity)
179 if (entity->num_links >= entity->max_links) {
180 struct media_link *links = entity->links;
181 unsigned int max_links = entity->max_links * 2;
184 links = realloc(links, max_links * sizeof *links);
188 for (i = 0; i < entity->num_links; ++i)
189 links[i].twin->twin = &links[i];
191 entity->max_links = max_links;
192 entity->links = links;
195 return &entity->links[entity->num_links++];
198 static int media_enum_links(struct media_device *media)
203 for (id = 1; id <= media->entities_count; id++) {
204 struct media_entity *entity = &media->entities[id - 1];
205 struct media_links_enum links;
208 links.entity = entity->info.id;
209 links.pads = calloc(entity->info.pads, sizeof(struct media_pad_desc));
210 links.links = calloc(entity->info.links, sizeof(struct media_link_desc));
212 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
214 "%s: Unable to enumerate pads and links (%s).\n",
215 __func__, strerror(errno));
221 for (i = 0; i < entity->info.pads; ++i) {
222 entity->pads[i].entity = entity;
223 entity->pads[i].index = links.pads[i].index;
224 entity->pads[i].flags = links.pads[i].flags;
227 for (i = 0; i < entity->info.links; ++i) {
228 struct media_link_desc *link = &links.links[i];
229 struct media_link *fwdlink;
230 struct media_link *backlink;
231 struct media_entity *source;
232 struct media_entity *sink;
234 source = media_get_entity_by_id(media, link->source.entity);
235 sink = media_get_entity_by_id(media, link->sink.entity);
237 if (source == NULL || sink == NULL) {
239 "WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
240 id, i, link->source.entity,
246 fwdlink = media_entity_add_link(source);
247 fwdlink->source = &source->pads[link->source.index];
248 fwdlink->sink = &sink->pads[link->sink.index];
249 fwdlink->flags = link->flags;
251 backlink = media_entity_add_link(sink);
252 backlink->source = &source->pads[link->source.index];
253 backlink->sink = &sink->pads[link->sink.index];
254 backlink->flags = link->flags;
256 fwdlink->twin = backlink;
257 backlink->twin = fwdlink;
272 static inline int media_udev_open(struct udev **udev)
280 static inline void media_udev_close(struct udev *udev)
286 static int media_get_devname_udev(struct udev *udev,
287 struct media_entity *entity)
289 struct udev_device *device;
297 devnum = makedev(entity->info.v4l.major, entity->info.v4l.minor);
298 media_dbg(entity->media, "looking up device: %u:%u\n",
299 major(devnum), minor(devnum));
300 device = udev_device_new_from_devnum(udev, 'c', devnum);
302 p = udev_device_get_devnode(device);
304 strncpy(entity->devname, p, sizeof(entity->devname));
305 entity->devname[sizeof(entity->devname) - 1] = '\0';
310 udev_device_unref(device);
315 #else /* HAVE_LIBUDEV */
319 static inline int media_udev_open(struct udev **udev) { return 0; }
321 static inline void media_udev_close(struct udev *udev) { }
323 static inline int media_get_devname_udev(struct udev *udev,
324 struct media_entity *entity)
329 #endif /* HAVE_LIBUDEV */
331 static int media_get_devname_sysfs(struct media_entity *entity)
340 sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
341 entity->info.v4l.minor);
342 ret = readlink(sysname, target, sizeof(target));
347 p = strrchr(target, '/');
351 sprintf(devname, "/dev/%s", p + 1);
352 ret = stat(devname, &devstat);
356 /* Sanity check: udev might have reordered the device nodes.
357 * Make sure the major/minor match. We should really use
360 if (major(devstat.st_rdev) == entity->info.v4l.major &&
361 minor(devstat.st_rdev) == entity->info.v4l.minor)
362 strcpy(entity->devname, devname);
367 static int media_enum_entities(struct media_device *media)
369 struct media_entity *entity;
375 ret = media_udev_open(&udev);
377 media_dbg(media, "Can't get udev context\n");
379 for (id = 0, ret = 0; ; id = entity->info.id) {
380 size = (media->entities_count + 1) * sizeof(*media->entities);
381 media->entities = realloc(media->entities, size);
383 entity = &media->entities[media->entities_count];
384 memset(entity, 0, sizeof(*entity));
386 entity->info.id = id | MEDIA_ENT_ID_FLAG_NEXT;
387 entity->media = media;
389 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
391 ret = errno != EINVAL ? -errno : 0;
395 /* Number of links (for outbound links) plus number of pads (for
396 * inbound links) is a good safe initial estimate of the total
399 entity->max_links = entity->info.pads + entity->info.links;
401 entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
402 entity->links = malloc(entity->max_links * sizeof(*entity->links));
403 if (entity->pads == NULL || entity->links == NULL) {
408 media->entities_count++;
410 /* Find the corresponding device name. */
411 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE &&
412 media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV)
415 /* Try to get the device name via udev */
416 if (!media_get_devname_udev(udev, entity))
419 /* Fall back to get the device name via sysfs */
420 media_get_devname_sysfs(entity);
423 media_udev_close(udev);
427 static void media_debug_default(void *ptr, ...)
431 void media_debug_set_handler(struct media_device *media,
432 void (*debug_handler)(void *, ...),
436 media->debug_handler = debug_handler;
437 media->debug_priv = debug_priv;
439 media->debug_handler = media_debug_default;
440 media->debug_priv = NULL;
444 struct media_device *media_open_debug(
445 const char *name, void (*debug_handler)(void *, ...),
448 struct media_device *media;
451 media = calloc(1, sizeof(*media));
455 media_debug_set_handler(media, debug_handler, debug_priv);
457 media_dbg(media, "Opening media device %s\n", name);
459 media->fd = open(name, O_RDWR);
462 media_dbg(media, "%s: Can't open media device %s\n",
467 ret = ioctl(media->fd, MEDIA_IOC_DEVICE_INFO, &media->info);
469 media_dbg(media, "%s: Unable to retrieve media device "
470 "information for device %s (%s)\n", __func__,
471 name, strerror(errno));
476 media_dbg(media, "Enumerating entities\n");
478 ret = media_enum_entities(media);
482 "%s: Unable to enumerate entities for device %s (%s)\n",
483 __func__, name, strerror(-ret));
488 media_dbg(media, "Found %u entities\n", media->entities_count);
489 media_dbg(media, "Enumerating pads and links\n");
491 ret = media_enum_links(media);
494 "%s: Unable to enumerate pads and linksfor device %s\n",
503 struct media_device *media_open(const char *name)
505 return media_open_debug(name, NULL, NULL);
508 void media_close(struct media_device *media)
515 for (i = 0; i < media->entities_count; ++i) {
516 struct media_entity *entity = &media->entities[i];
520 if (entity->fd != -1)
524 free(media->entities);
528 struct media_pad *media_parse_pad(struct media_device *media,
529 const char *p, char **endp)
531 unsigned int entity_id, pad;
532 struct media_entity *entity;
535 for (; isspace(*p); ++p);
538 for (end = (char *)p + 1; *end && *end != '"'; ++end);
542 entity = media_get_entity_by_name(media, p + 1, end - p - 1);
548 entity_id = strtoul(p, &end, 10);
549 entity = media_get_entity_by_id(media, entity_id);
553 for (; isspace(*end); ++end);
557 for (p = end + 1; isspace(*p); ++p);
559 pad = strtoul(p, &end, 10);
560 for (p = end; isspace(*p); ++p);
562 if (pad >= entity->info.pads)
565 for (p = end; isspace(*p); ++p);
569 return &entity->pads[pad];
572 struct media_link *media_parse_link(struct media_device *media,
573 const char *p, char **endp)
575 struct media_link *link;
576 struct media_pad *source;
577 struct media_pad *sink;
581 source = media_parse_pad(media, p, &end);
585 if (end[0] != '-' || end[1] != '>')
589 sink = media_parse_pad(media, p, &end);
595 for (i = 0; i < source->entity->num_links; i++) {
596 link = &source->entity->links[i];
598 if (link->source == source && link->sink == sink)
605 int media_parse_setup_link(struct media_device *media,
606 const char *p, char **endp)
608 struct media_link *link;
612 link = media_parse_link(media, p, &end);
615 "%s: Unable to parse link\n", __func__);
621 media_dbg(media, "Unable to parse link flags\n");
625 flags = strtoul(p, &end, 10);
626 for (p = end; isspace(*p); p++);
628 media_dbg(media, "Unable to parse link flags\n");
632 for (; isspace(*p); p++);
636 "Setting up link %u:%u -> %u:%u [%u]\n",
637 link->source->entity->info.id, link->source->index,
638 link->sink->entity->info.id, link->sink->index,
641 return media_setup_link(media, link->source, link->sink, flags);
644 int media_parse_setup_links(struct media_device *media, const char *p)
650 ret = media_parse_setup_link(media, p, &end);
655 } while (*end == ',');
657 return *end ? -EINVAL : 0;