2 * Media controller interface library
4 * Copyright (C) 2010 Ideas on board SPRL <laurent.pinchart@ideasonboard.com>
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.
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.
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.,
22 #include <sys/ioctl.h>
24 #include <sys/types.h>
33 #include <linux/videodev2.h>
34 #include <linux/media.h>
39 struct media_pad *media_entity_remote_source(struct media_pad *pad)
43 if (!(pad->flags & MEDIA_PAD_FL_SINK))
46 for (i = 0; i < pad->entity->num_links; ++i) {
47 struct media_link *link = &pad->entity->links[i];
49 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
52 if (link->sink == pad)
59 struct media_entity *media_get_entity_by_name(struct media_device *media,
60 const char *name, size_t length)
64 for (i = 0; i < media->entities_count; ++i) {
65 struct media_entity *entity = &media->entities[i];
67 if (strncmp(entity->info.name, name, length) == 0)
74 struct media_entity *media_get_entity_by_id(struct media_device *media,
79 for (i = 0; i < media->entities_count; ++i) {
80 struct media_entity *entity = &media->entities[i];
82 if (entity->info.id == id)
89 int media_setup_link(struct media_device *media,
90 struct media_pad *source,
91 struct media_pad *sink,
94 struct media_link *link;
95 struct media_link_desc ulink;
99 for (i = 0; i < source->entity->num_links; i++) {
100 link = &source->entity->links[i];
102 if (link->source->entity == source->entity &&
103 link->source->index == source->index &&
104 link->sink->entity == sink->entity &&
105 link->sink->index == sink->index)
109 if (i == source->entity->num_links) {
110 media_dbg(media, "%s: Link not found\n", __func__);
115 ulink.source.entity = source->entity->info.id;
116 ulink.source.index = source->index;
117 ulink.source.flags = MEDIA_PAD_FL_SOURCE;
120 ulink.sink.entity = sink->entity->info.id;
121 ulink.sink.index = sink->index;
122 ulink.sink.flags = MEDIA_PAD_FL_SINK;
124 ulink.flags = flags | (link->flags & MEDIA_LNK_FL_IMMUTABLE);
126 ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
128 media_dbg(media, "%s: Unable to setup link (%s)\n",
129 __func__, strerror(errno));
133 link->flags = ulink.flags;
134 link->twin->flags = ulink.flags;
138 int media_reset_links(struct media_device *media)
143 for (i = 0; i < media->entities_count; ++i) {
144 struct media_entity *entity = &media->entities[i];
146 for (j = 0; j < entity->num_links; j++) {
147 struct media_link *link = &entity->links[j];
149 if (link->flags & MEDIA_LNK_FL_IMMUTABLE ||
150 link->source->entity != entity)
153 ret = media_setup_link(media, link->source, link->sink,
154 link->flags & ~MEDIA_LNK_FL_ENABLED);
163 static struct media_link *media_entity_add_link(struct media_entity *entity)
165 if (entity->num_links >= entity->max_links) {
166 struct media_link *links = entity->links;
167 unsigned int max_links = entity->max_links * 2;
170 links = realloc(links, max_links * sizeof *links);
174 for (i = 0; i < entity->num_links; ++i)
175 links[i].twin->twin = &links[i];
177 entity->max_links = max_links;
178 entity->links = links;
181 return &entity->links[entity->num_links++];
184 static int media_enum_links(struct media_device *media)
189 for (id = 1; id <= media->entities_count; id++) {
190 struct media_entity *entity = &media->entities[id - 1];
191 struct media_links_enum links;
194 links.entity = entity->info.id;
195 links.pads = malloc(entity->info.pads * sizeof(struct media_pad_desc));
196 links.links = malloc(entity->info.links * sizeof(struct media_link_desc));
198 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
200 "%s: Unable to enumerate pads and links (%s).\n",
201 __func__, strerror(errno));
207 for (i = 0; i < entity->info.pads; ++i) {
208 entity->pads[i].entity = entity;
209 entity->pads[i].index = links.pads[i].index;
210 entity->pads[i].flags = links.pads[i].flags;
213 for (i = 0; i < entity->info.links; ++i) {
214 struct media_link_desc *link = &links.links[i];
215 struct media_link *fwdlink;
216 struct media_link *backlink;
217 struct media_entity *source;
218 struct media_entity *sink;
220 source = media_get_entity_by_id(media, link->source.entity);
221 sink = media_get_entity_by_id(media, link->sink.entity);
223 if (source == NULL || sink == NULL) {
225 "WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
226 id, i, link->source.entity,
232 fwdlink = media_entity_add_link(source);
233 fwdlink->source = &source->pads[link->source.index];
234 fwdlink->sink = &sink->pads[link->sink.index];
235 fwdlink->flags = link->flags;
237 backlink = media_entity_add_link(sink);
238 backlink->source = &source->pads[link->source.index];
239 backlink->sink = &sink->pads[link->sink.index];
240 backlink->flags = link->flags;
242 fwdlink->twin = backlink;
243 backlink->twin = fwdlink;
258 static inline int media_udev_open(struct udev **udev)
266 static inline void media_udev_close(struct udev *udev)
272 static int media_get_devname_udev(struct udev *udev,
273 struct media_entity *entity)
275 struct udev_device *device;
283 devnum = makedev(entity->info.v4l.major, entity->info.v4l.minor);
284 media_dbg(entity->media, "looking up device: %u:%u\n",
285 major(devnum), minor(devnum));
286 device = udev_device_new_from_devnum(udev, 'c', devnum);
288 p = udev_device_get_devnode(device);
290 strncpy(entity->devname, p, sizeof(entity->devname));
291 entity->devname[sizeof(entity->devname) - 1] = '\0';
296 udev_device_unref(device);
301 #else /* HAVE_LIBUDEV */
305 static inline int media_udev_open(struct udev **udev) { return 0; }
307 static inline void media_udev_close(struct udev *udev) { }
309 static inline int media_get_devname_udev(struct udev *udev,
310 struct media_entity *entity)
315 #endif /* HAVE_LIBUDEV */
317 static int media_get_devname_sysfs(struct media_entity *entity)
326 sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
327 entity->info.v4l.minor);
328 ret = readlink(sysname, target, sizeof(target));
333 p = strrchr(target, '/');
337 sprintf(devname, "/dev/%s", p + 1);
338 ret = stat(devname, &devstat);
342 /* Sanity check: udev might have reordered the device nodes.
343 * Make sure the major/minor match. We should really use
346 if (major(devstat.st_rdev) == entity->info.v4l.major &&
347 minor(devstat.st_rdev) == entity->info.v4l.minor)
348 strcpy(entity->devname, devname);
353 static int media_enum_entities(struct media_device *media)
355 struct media_entity *entity;
361 ret = media_udev_open(&udev);
363 media_dbg(media, "Can't get udev context\n");
365 for (id = 0, ret = 0; ; id = entity->info.id) {
366 size = (media->entities_count + 1) * sizeof(*media->entities);
367 media->entities = realloc(media->entities, size);
369 entity = &media->entities[media->entities_count];
370 memset(entity, 0, sizeof(*entity));
372 entity->info.id = id | MEDIA_ENT_ID_FLAG_NEXT;
373 entity->media = media;
375 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
377 ret = errno != EINVAL ? -errno : 0;
381 /* Number of links (for outbound links) plus number of pads (for
382 * inbound links) is a good safe initial estimate of the total
385 entity->max_links = entity->info.pads + entity->info.links;
387 entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
388 entity->links = malloc(entity->max_links * sizeof(*entity->links));
389 if (entity->pads == NULL || entity->links == NULL) {
394 media->entities_count++;
396 /* Find the corresponding device name. */
397 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE &&
398 media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV)
401 /* Try to get the device name via udev */
402 if (!media_get_devname_udev(udev, entity))
405 /* Fall back to get the device name via sysfs */
406 media_get_devname_sysfs(entity);
409 media_udev_close(udev);
413 static void media_debug_default(void *ptr, ...)
417 void media_debug_set_handler(struct media_device *media,
418 void (*debug_handler)(void *, ...),
422 media->debug_handler = debug_handler;
423 media->debug_priv = debug_priv;
425 media->debug_handler = media_debug_default;
426 media->debug_priv = NULL;
430 struct media_device *media_open_debug(
431 const char *name, void (*debug_handler)(void *, ...),
434 struct media_device *media;
437 media = calloc(1, sizeof(*media));
441 media_debug_set_handler(media, debug_handler, debug_priv);
443 media_dbg(media, "Opening media device %s\n", name);
445 media->fd = open(name, O_RDWR);
448 media_dbg(media, "%s: Can't open media device %s\n",
453 media_dbg(media, "Enumerating entities\n");
455 ret = media_enum_entities(media);
459 "%s: Unable to enumerate entities for device %s (%s)\n",
460 __func__, name, strerror(-ret));
465 media_dbg(media, "Found %u entities\n", media->entities_count);
466 media_dbg(media, "Enumerating pads and links\n");
468 ret = media_enum_links(media);
471 "%s: Unable to enumerate pads and linksfor device %s\n",
480 struct media_device *media_open(const char *name)
482 return media_open_debug(name, NULL, NULL);
485 void media_close(struct media_device *media)
492 for (i = 0; i < media->entities_count; ++i) {
493 struct media_entity *entity = &media->entities[i];
497 if (entity->fd != -1)
501 free(media->entities);
505 struct media_pad *media_parse_pad(struct media_device *media,
506 const char *p, char **endp)
508 unsigned int entity_id, pad;
509 struct media_entity *entity;
512 for (; isspace(*p); ++p);
515 for (end = (char *)p + 1; *end && *end != '"'; ++end);
519 entity = media_get_entity_by_name(media, p + 1, end - p - 1);
525 entity_id = strtoul(p, &end, 10);
526 entity = media_get_entity_by_id(media, entity_id);
530 for (; isspace(*end); ++end);
534 for (p = end + 1; isspace(*p); ++p);
536 pad = strtoul(p, &end, 10);
537 for (p = end; isspace(*p); ++p);
539 if (pad >= entity->info.pads)
542 for (p = end; isspace(*p); ++p);
546 return &entity->pads[pad];
549 struct media_link *media_parse_link(struct media_device *media,
550 const char *p, char **endp)
552 struct media_link *link;
553 struct media_pad *source;
554 struct media_pad *sink;
558 source = media_parse_pad(media, p, &end);
562 if (end[0] != '-' || end[1] != '>')
566 sink = media_parse_pad(media, p, &end);
572 for (i = 0; i < source->entity->num_links; i++) {
573 link = &source->entity->links[i];
575 if (link->source == source && link->sink == sink)
582 int media_parse_setup_link(struct media_device *media,
583 const char *p, char **endp)
585 struct media_link *link;
589 link = media_parse_link(media, p, &end);
592 "%s: Unable to parse link\n", __func__);
598 media_dbg(media, "Unable to parse link flags\n");
602 flags = strtoul(p, &end, 10);
603 for (p = end; isspace(*p); p++);
605 media_dbg(media, "Unable to parse link flags\n");
609 for (; isspace(*p); p++);
613 "Setting up link %u:%u -> %u:%u [%u]\n",
614 link->source->entity->info.id, link->source->index,
615 link->sink->entity->info.id, link->sink->index,
618 return media_setup_link(media, link->source, link->sink, flags);
621 int media_parse_setup_links(struct media_device *media, const char *p)
627 ret = media_parse_setup_link(media, p, &end);
632 } while (*end == ',');
634 return *end ? -EINVAL : 0;