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, int verbose)
275 struct udev_device *device;
283 devnum = makedev(entity->info.v4l.major, entity->info.v4l.minor);
285 media_dbg(entity->media, "looking up device: %u:%u\n",
286 major(devnum), minor(devnum));
287 device = udev_device_new_from_devnum(udev, 'c', devnum);
289 p = udev_device_get_devnode(device);
291 strncpy(entity->devname, p, sizeof(entity->devname));
292 entity->devname[sizeof(entity->devname) - 1] = '\0';
297 udev_device_unref(device);
302 #else /* HAVE_LIBUDEV */
306 static inline int media_udev_open(struct udev **udev) { return 0; }
308 static inline void media_udev_close(struct udev *udev) { }
310 static inline int media_get_devname_udev(struct udev *udev,
311 struct media_entity *entity, int verbose)
316 #endif /* HAVE_LIBUDEV */
318 static int media_get_devname_sysfs(struct media_entity *entity)
327 sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
328 entity->info.v4l.minor);
329 ret = readlink(sysname, target, sizeof(target));
334 p = strrchr(target, '/');
338 sprintf(devname, "/dev/%s", p + 1);
339 ret = stat(devname, &devstat);
343 /* Sanity check: udev might have reordered the device nodes.
344 * Make sure the major/minor match. We should really use
347 if (major(devstat.st_rdev) == entity->info.v4l.major &&
348 minor(devstat.st_rdev) == entity->info.v4l.minor)
349 strcpy(entity->devname, devname);
354 static int media_enum_entities(struct media_device *media, int verbose)
356 struct media_entity *entity;
362 ret = media_udev_open(&udev);
364 media_dbg(media, "Can't get udev context\n");
366 for (id = 0, ret = 0; ; id = entity->info.id) {
367 size = (media->entities_count + 1) * sizeof(*media->entities);
368 media->entities = realloc(media->entities, size);
370 entity = &media->entities[media->entities_count];
371 memset(entity, 0, sizeof(*entity));
373 entity->info.id = id | MEDIA_ENT_ID_FLAG_NEXT;
374 entity->media = media;
376 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
378 ret = errno != EINVAL ? -errno : 0;
382 /* Number of links (for outbound links) plus number of pads (for
383 * inbound links) is a good safe initial estimate of the total
386 entity->max_links = entity->info.pads + entity->info.links;
388 entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
389 entity->links = malloc(entity->max_links * sizeof(*entity->links));
390 if (entity->pads == NULL || entity->links == NULL) {
395 media->entities_count++;
397 /* Find the corresponding device name. */
398 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE &&
399 media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV)
402 /* Try to get the device name via udev */
403 if (!media_get_devname_udev(udev, entity, verbose))
406 /* Fall back to get the device name via sysfs */
407 media_get_devname_sysfs(entity);
410 media_udev_close(udev);
414 static void media_debug_default(void *ptr, ...)
418 void media_debug_set_handler(struct media_device *media,
419 void (*debug_handler)(void *, ...),
423 media->debug_handler = debug_handler;
424 media->debug_priv = debug_priv;
426 media->debug_handler = media_debug_default;
427 media->debug_priv = NULL;
431 struct media_device *media_open_debug(
432 const char *name, int verbose, void (*debug_handler)(void *, ...),
435 struct media_device *media;
438 media = calloc(1, sizeof(*media));
442 media_debug_set_handler(media, debug_handler, debug_priv);
444 media_dbg(media, "Opening media device %s\n", name);
446 media->fd = open(name, O_RDWR);
449 media_dbg(media, "%s: Can't open media device %s\n",
454 media_dbg(media, "Enumerating entities\n");
456 ret = media_enum_entities(media, verbose);
460 "%s: Unable to enumerate entities for device %s (%s)\n",
461 __func__, name, strerror(-ret));
466 media_dbg(media, "Found %u entities\n", media->entities_count);
467 media_dbg(media, "Enumerating pads and links\n");
469 ret = media_enum_links(media);
472 "%s: Unable to enumerate pads and linksfor device %s\n",
481 struct media_device *media_open(const char *name, int verbose)
483 return media_open_debug(name, verbose, NULL, NULL);
486 void media_close(struct media_device *media)
493 for (i = 0; i < media->entities_count; ++i) {
494 struct media_entity *entity = &media->entities[i];
498 if (entity->fd != -1)
502 free(media->entities);
506 struct media_pad *media_parse_pad(struct media_device *media,
507 const char *p, char **endp)
509 unsigned int entity_id, pad;
510 struct media_entity *entity;
513 for (; isspace(*p); ++p);
516 for (end = (char *)p + 1; *end && *end != '"'; ++end);
520 entity = media_get_entity_by_name(media, p + 1, end - p - 1);
526 entity_id = strtoul(p, &end, 10);
527 entity = media_get_entity_by_id(media, entity_id);
531 for (; isspace(*end); ++end);
535 for (p = end + 1; isspace(*p); ++p);
537 pad = strtoul(p, &end, 10);
538 for (p = end; isspace(*p); ++p);
540 if (pad >= entity->info.pads)
543 for (p = end; isspace(*p); ++p);
547 return &entity->pads[pad];
550 struct media_link *media_parse_link(struct media_device *media,
551 const char *p, char **endp)
553 struct media_link *link;
554 struct media_pad *source;
555 struct media_pad *sink;
559 source = media_parse_pad(media, p, &end);
563 if (end[0] != '-' || end[1] != '>')
567 sink = media_parse_pad(media, p, &end);
573 for (i = 0; i < source->entity->num_links; i++) {
574 link = &source->entity->links[i];
576 if (link->source == source && link->sink == sink)
583 int media_parse_setup_link(struct media_device *media,
584 const char *p, char **endp)
586 struct media_link *link;
590 link = media_parse_link(media, p, &end);
593 "%s: Unable to parse link\n", __func__);
599 media_dbg(media, "Unable to parse link flags\n");
603 flags = strtoul(p, &end, 10);
604 for (p = end; isspace(*p); p++);
606 media_dbg(media, "Unable to parse link flags\n");
610 for (; isspace(*p); p++);
614 "Setting up link %u:%u -> %u:%u [%u]\n",
615 link->source->entity->info.id, link->source->index,
616 link->sink->entity->info.id, link->sink->index,
619 return media_setup_link(media, link->source, link->sink, flags);
622 int media_parse_setup_links(struct media_device *media, const char *p)
628 ret = media_parse_setup_link(media, p, &end);
633 } while (*end == ',');
635 return *end ? -EINVAL : 0;