2 * Media controller test application
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>
40 #define dprintf(...) printf(__VA_ARGS__)
45 struct media_pad *media_entity_remote_source(struct media_pad *pad)
49 if (!(pad->flags & MEDIA_PAD_FL_SINK))
52 for (i = 0; i < pad->entity->num_links; ++i) {
53 struct media_link *link = &pad->entity->links[i];
55 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
58 if (link->sink == pad)
65 struct media_entity *media_get_entity_by_name(struct media_device *media,
66 const char *name, size_t length)
70 for (i = 0; i < media->entities_count; ++i) {
71 struct media_entity *entity = &media->entities[i];
73 if (strncmp(entity->info.name, name, length) == 0)
80 struct media_entity *media_get_entity_by_id(struct media_device *media,
85 for (i = 0; i < media->entities_count; ++i) {
86 struct media_entity *entity = &media->entities[i];
88 if (entity->info.id == id)
95 int media_setup_link(struct media_device *media,
96 struct media_pad *source,
97 struct media_pad *sink,
100 struct media_link *link;
101 struct media_link_desc ulink;
105 for (i = 0; i < source->entity->num_links; i++) {
106 link = &source->entity->links[i];
108 if (link->source->entity == source->entity &&
109 link->source->index == source->index &&
110 link->sink->entity == sink->entity &&
111 link->sink->index == sink->index)
115 if (i == source->entity->num_links) {
116 dprintf("%s: Link not found\n", __func__);
121 ulink.source.entity = source->entity->info.id;
122 ulink.source.index = source->index;
123 ulink.source.flags = MEDIA_PAD_FL_SOURCE;
126 ulink.sink.entity = sink->entity->info.id;
127 ulink.sink.index = sink->index;
128 ulink.sink.flags = MEDIA_PAD_FL_SINK;
130 ulink.flags = flags | (link->flags & MEDIA_LNK_FL_IMMUTABLE);
132 ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
134 dprintf("%s: Unable to setup link (%s)\n", __func__,
139 link->flags = ulink.flags;
140 link->twin->flags = ulink.flags;
144 int media_reset_links(struct media_device *media)
149 for (i = 0; i < media->entities_count; ++i) {
150 struct media_entity *entity = &media->entities[i];
152 for (j = 0; j < entity->num_links; j++) {
153 struct media_link *link = &entity->links[j];
155 if (link->flags & MEDIA_LNK_FL_IMMUTABLE ||
156 link->source->entity != entity)
159 ret = media_setup_link(media, link->source, link->sink,
160 link->flags & ~MEDIA_LNK_FL_ENABLED);
169 static struct media_link *media_entity_add_link(struct media_entity *entity)
171 if (entity->num_links >= entity->max_links) {
172 struct media_link *links = entity->links;
173 unsigned int max_links = entity->max_links * 2;
176 links = realloc(links, max_links * sizeof *links);
180 for (i = 0; i < entity->num_links; ++i)
181 links[i].twin->twin = &links[i];
183 entity->max_links = max_links;
184 entity->links = links;
187 return &entity->links[entity->num_links++];
190 static int media_enum_links(struct media_device *media)
195 for (id = 1; id <= media->entities_count; id++) {
196 struct media_entity *entity = &media->entities[id - 1];
197 struct media_links_enum links;
200 links.entity = entity->info.id;
201 links.pads = malloc(entity->info.pads * sizeof(struct media_pad_desc));
202 links.links = malloc(entity->info.links * sizeof(struct media_link_desc));
204 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
205 dprintf("%s: Unable to enumerate pads and links (%s).\n",
206 __func__, strerror(errno));
212 for (i = 0; i < entity->info.pads; ++i) {
213 entity->pads[i].entity = entity;
214 entity->pads[i].index = links.pads[i].index;
215 entity->pads[i].flags = links.pads[i].flags;
218 for (i = 0; i < entity->info.links; ++i) {
219 struct media_link_desc *link = &links.links[i];
220 struct media_link *fwdlink;
221 struct media_link *backlink;
222 struct media_entity *source;
223 struct media_entity *sink;
225 source = media_get_entity_by_id(media, link->source.entity);
226 sink = media_get_entity_by_id(media, link->sink.entity);
228 if (source == NULL || sink == NULL) {
229 dprintf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
230 id, i, link->source.entity, link->source.index,
231 link->sink.entity, link->sink.index);
234 fwdlink = media_entity_add_link(source);
235 fwdlink->source = &source->pads[link->source.index];
236 fwdlink->sink = &sink->pads[link->sink.index];
237 fwdlink->flags = link->flags;
239 backlink = media_entity_add_link(sink);
240 backlink->source = &source->pads[link->source.index];
241 backlink->sink = &sink->pads[link->sink.index];
242 backlink->flags = link->flags;
244 fwdlink->twin = backlink;
245 backlink->twin = fwdlink;
260 static inline int media_udev_open(struct udev **udev)
268 static inline void media_udev_close(struct udev *udev)
274 static int media_get_devname_udev(struct udev *udev,
275 struct media_entity *entity, int verbose)
277 struct udev_device *device;
285 devnum = makedev(entity->info.v4l.major, entity->info.v4l.minor);
287 printf("looking up device: %u:%u\n", major(devnum), minor(devnum));
288 device = udev_device_new_from_devnum(udev, 'c', devnum);
290 p = udev_device_get_devnode(device);
292 strncpy(entity->devname, p, sizeof(entity->devname));
293 entity->devname[sizeof(entity->devname) - 1] = '\0';
298 udev_device_unref(device);
303 #else /* HAVE_LIBUDEV */
307 static inline int media_udev_open(struct udev **udev) { return 0; }
309 static inline void media_udev_close(struct udev *udev) { }
311 static inline int media_get_devname_udev(struct udev *udev,
312 struct media_entity *entity, int verbose)
317 #endif /* HAVE_LIBUDEV */
319 static int media_get_devname_sysfs(struct media_entity *entity)
328 sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
329 entity->info.v4l.minor);
330 ret = readlink(sysname, target, sizeof(target));
335 p = strrchr(target, '/');
339 sprintf(devname, "/dev/%s", p + 1);
340 ret = stat(devname, &devstat);
344 /* Sanity check: udev might have reordered the device nodes.
345 * Make sure the major/minor match. We should really use
348 if (major(devstat.st_rdev) == entity->info.v4l.major &&
349 minor(devstat.st_rdev) == entity->info.v4l.minor)
350 strcpy(entity->devname, devname);
355 static int media_enum_entities(struct media_device *media, int verbose)
357 struct media_entity *entity;
363 ret = media_udev_open(&udev);
365 printf("%s: Can't get udev context\n", __func__);
367 for (id = 0, ret = 0; ; id = entity->info.id) {
368 size = (media->entities_count + 1) * sizeof(*media->entities);
369 media->entities = realloc(media->entities, size);
371 entity = &media->entities[media->entities_count];
372 memset(entity, 0, sizeof(*entity));
374 entity->info.id = id | MEDIA_ENT_ID_FLAG_NEXT;
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 struct media_device *media_open(const char *name, int verbose)
416 struct media_device *media;
419 media = calloc(1, sizeof(*media));
421 dprintf("%s: unable to allocate memory\n", __func__);
426 dprintf("Opening media device %s\n", name);
428 media->fd = open(name, O_RDWR);
431 dprintf("%s: Can't open media device %s\n", __func__, name);
436 dprintf("Enumerating entities\n");
438 ret = media_enum_entities(media, verbose);
441 dprintf("%s: Unable to enumerate entities for device %s (%s)\n",
442 __func__, name, strerror(-ret));
448 dprintf("Found %u entities\n", media->entities_count);
449 dprintf("Enumerating pads and links\n");
452 ret = media_enum_links(media);
454 dprintf("%s: Unable to enumerate pads and linksfor device %s\n",
463 void media_close(struct media_device *media)
470 for (i = 0; i < media->entities_count; ++i) {
471 struct media_entity *entity = &media->entities[i];
475 if (entity->fd != -1)
479 free(media->entities);
483 struct media_pad *media_parse_pad(struct media_device *media,
484 const char *p, char **endp)
486 unsigned int entity_id, pad;
487 struct media_entity *entity;
490 for (; isspace(*p); ++p);
493 for (end = (char *)p + 1; *end && *end != '"'; ++end);
497 entity = media_get_entity_by_name(media, p + 1, end - p - 1);
503 entity_id = strtoul(p, &end, 10);
504 entity = media_get_entity_by_id(media, entity_id);
508 for (; isspace(*end); ++end);
512 for (p = end + 1; isspace(*p); ++p);
514 pad = strtoul(p, &end, 10);
515 for (p = end; isspace(*p); ++p);
517 if (pad >= entity->info.pads)
520 for (p = end; isspace(*p); ++p);
524 return &entity->pads[pad];
527 struct media_link *media_parse_link(struct media_device *media,
528 const char *p, char **endp)
530 struct media_link *link;
531 struct media_pad *source;
532 struct media_pad *sink;
536 source = media_parse_pad(media, p, &end);
540 if (end[0] != '-' || end[1] != '>')
544 sink = media_parse_pad(media, p, &end);
550 for (i = 0; i < source->entity->num_links; i++) {
551 link = &source->entity->links[i];
553 if (link->source == source && link->sink == sink)
560 int media_parse_setup_link(struct media_device *media,
561 const char *p, char **endp)
563 struct media_link *link;
567 link = media_parse_link(media, p, &end);
569 dprintf("Unable to parse link\n");
575 dprintf("Unable to parse link flags\n");
579 flags = strtoul(p, &end, 10);
580 for (p = end; isspace(*p); p++);
582 dprintf("Unable to parse link flags\n");
586 for (; isspace(*p); p++);
589 dprintf("Setting up link %u:%u -> %u:%u [%u]\n",
590 link->source->entity->info.id, link->source->index,
591 link->sink->entity->info.id, link->sink->index,
594 return media_setup_link(media, link->source, link->sink, flags);
597 int media_parse_setup_links(struct media_device *media, const char *p)
603 ret = media_parse_setup_link(media, p, &end);
608 } while (*end == ',');
610 return *end ? -EINVAL : 0;