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.,
20 #include <sys/ioctl.h>
22 #include <sys/types.h>
31 #include <linux/videodev2.h>
32 #include <linux/media.h>
38 * media_entity_remote_pad -
40 struct media_entity_pad *media_entity_remote_source(struct media_entity_pad *pad)
44 if (!(pad->flags & MEDIA_PAD_FLAG_INPUT))
47 for (i = 0; i < pad->entity->info.links; ++i) {
48 struct media_entity_link *link = &pad->entity->links[i];
50 if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
53 if (link->sink == pad)
61 * media_get_entity_by_name -
63 struct media_entity *media_get_entity_by_name(struct media_device *media,
64 const char *name, size_t length)
68 for (i = 0; i < media->entities_count; ++i) {
69 struct media_entity *entity = &media->entities[i];
71 if (strncmp(entity->info.name, name, length) == 0)
79 * media_get_entity_by_id -
81 struct media_entity *media_get_entity_by_id(struct media_device *media,
86 for (i = 0; i < media->entities_count; ++i) {
87 struct media_entity *entity = &media->entities[i];
89 if (entity->info.id == id)
99 int media_setup_link(struct media_device *media,
100 struct media_entity_pad *source,
101 struct media_entity_pad *sink,
104 struct media_entity_link *link;
105 struct media_link_desc ulink;
109 for (i = 0; i < source->entity->info.links; i++) {
110 link = &source->entity->links[i];
112 if (link->source->entity == source->entity &&
113 link->source->index == source->index &&
114 link->sink->entity == sink->entity &&
115 link->sink->index == sink->index)
119 if (i == source->entity->info.links) {
120 printf("%s: Link not found\n", __func__);
125 ulink.source.entity = source->entity->info.id;
126 ulink.source.index = source->index;
127 ulink.source.flags = MEDIA_PAD_FLAG_OUTPUT;
130 ulink.sink.entity = sink->entity->info.id;
131 ulink.sink.index = sink->index;
132 ulink.sink.flags = MEDIA_PAD_FLAG_INPUT;
134 ulink.flags = flags | (link->flags & MEDIA_LINK_FLAG_IMMUTABLE);
136 ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
138 printf("%s: Unable to setup link (%s)\n", __func__,
147 int media_reset_links(struct media_device *media)
152 for (i = 0; i < media->entities_count; ++i) {
153 struct media_entity *entity = &media->entities[i];
155 for (j = 0; j < entity->info.links; j++) {
156 struct media_entity_link *link = &entity->links[j];
158 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
161 ret = media_setup_link(media, link->source, link->sink,
162 link->flags & ~MEDIA_LINK_FLAG_ACTIVE);
171 static int media_enum_links(struct media_device *media)
176 for (id = 1; id <= media->entities_count; id++) {
177 struct media_entity *entity = &media->entities[id - 1];
178 struct media_links_enum links;
181 links.entity = entity->info.id;
182 links.pads = malloc(entity->info.pads * sizeof(struct media_pad_desc));
183 links.links = malloc(entity->info.links * sizeof(struct media_link_desc));
185 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
186 printf("%s: Unable to enumerate pads and links (%s).\n",
187 __func__, strerror(errno));
193 for (i = 0; i < entity->info.pads; ++i) {
194 entity->pads[i].entity = entity;
195 entity->pads[i].index = links.pads[i].index;
196 entity->pads[i].flags = links.pads[i].flags;
199 for (i = 0; i < entity->info.links; ++i) {
200 struct media_link_desc *link = &links.links[i];
201 struct media_entity *source;
202 struct media_entity *sink;
204 source = media_get_entity_by_id(media, link->source.entity);
205 sink = media_get_entity_by_id(media, link->sink.entity);
207 if (source == NULL || sink == NULL) {
208 printf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
209 id, i, link->source.entity, link->source.index,
210 link->sink.entity, link->sink.index);
213 entity->links[i].source = &source->pads[link->source.index];
214 entity->links[i].sink = &sink->pads[link->sink.index];
215 entity->links[i].flags = links.links[i].flags;
226 static int media_enum_entities(struct media_device *media)
228 struct media_entity *entity;
238 for (id = 0; ; id = entity->info.id) {
239 size = (media->entities_count + 1) * sizeof(*media->entities);
240 media->entities = realloc(media->entities, size);
242 entity = &media->entities[media->entities_count];
243 memset(entity, 0, sizeof(*entity));
245 entity->info.id = id | MEDIA_ENTITY_ID_FLAG_NEXT;
247 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
254 entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
255 entity->links = malloc(entity->info.links * sizeof(*entity->links));
256 if (entity->pads == NULL || entity->links == NULL)
259 media->entities_count++;
261 /* Find the corresponding device name. */
262 if (media_entity_type(entity) != MEDIA_ENTITY_TYPE_NODE &&
263 media_entity_type(entity) != MEDIA_ENTITY_TYPE_SUBDEV)
266 sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
267 entity->info.v4l.minor);
268 ret = readlink(sysname, target, sizeof(target));
273 p = strrchr(target, '/');
277 sprintf(devname, "/dev/%s", p + 1);
278 ret = stat(devname, &devstat);
282 /* Sanity check: udev might have reordered the device nodes.
283 * Make sure the major/minor match. We should really use
286 if (major(devstat.st_rdev) == entity->info.v4l.major &&
287 minor(devstat.st_rdev) == entity->info.v4l.minor)
288 strcpy(entity->devname, devname);
297 struct media_device *media_open(const char *name, int verbose)
299 struct media_device *media;
302 media = malloc(sizeof(*media));
304 printf("%s: unable to allocate memory\n", __func__);
307 memset(media, 0, sizeof(*media));
310 printf("Opening media device %s\n", name);
311 media->fd = open(name, O_RDWR);
314 printf("%s: Can't open media device %s\n", __func__, name);
319 printf("Enumerating entities\n");
321 ret = media_enum_entities(media);
323 printf("%s: Unable to enumerate entities for device %s (%s)\n",
324 __func__, name, strerror(-ret));
330 printf("Found %u entities\n", media->entities_count);
331 printf("Enumerating pads and links\n");
334 ret = media_enum_links(media);
336 printf("%s: Unable to enumerate pads and linksfor device %s\n",
348 void media_close(struct media_device *media)
355 for (i = 0; i < media->entities_count; ++i) {
356 struct media_entity *entity = &media->entities[i];
360 if (entity->fd != -1)
364 free(media->entities);