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_pad *media_entity_remote_source(struct media_pad *pad)
44 if (!(pad->flags & MEDIA_PAD_FLAG_INPUT))
47 for (i = 0; i < pad->entity->num_links; ++i) {
48 struct media_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_pad *source,
101 struct media_pad *sink,
104 struct media_link *link;
105 struct media_link_desc ulink;
109 for (i = 0; i < source->entity->num_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->num_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->num_links; j++) {
156 struct media_link *link = &entity->links[j];
158 if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE ||
159 link->source->entity != entity)
162 ret = media_setup_link(media, link->source, link->sink,
163 link->flags & ~MEDIA_LINK_FLAG_ACTIVE);
172 static struct media_link *media_entity_add_link(struct media_entity *entity)
174 if (entity->num_links >= entity->max_links) {
175 struct media_link *links = entity->links;
176 unsigned int max_links = entity->max_links * 2;
179 links = realloc(links, max_links * sizeof *links);
183 for (i = 0; i < entity->num_links; ++i)
184 links[i].twin->twin = &links[i];
186 entity->max_links = max_links;
187 entity->links = links;
190 return &entity->links[entity->num_links++];
193 static int media_enum_links(struct media_device *media)
198 for (id = 1; id <= media->entities_count; id++) {
199 struct media_entity *entity = &media->entities[id - 1];
200 struct media_links_enum links;
203 links.entity = entity->info.id;
204 links.pads = malloc(entity->info.pads * sizeof(struct media_pad_desc));
205 links.links = malloc(entity->info.links * sizeof(struct media_link_desc));
207 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
208 printf("%s: Unable to enumerate pads and links (%s).\n",
209 __func__, strerror(errno));
215 for (i = 0; i < entity->info.pads; ++i) {
216 entity->pads[i].entity = entity;
217 entity->pads[i].index = links.pads[i].index;
218 entity->pads[i].flags = links.pads[i].flags;
221 for (i = 0; i < entity->info.links; ++i) {
222 struct media_link_desc *link = &links.links[i];
223 struct media_link *fwdlink;
224 struct media_link *backlink;
225 struct media_entity *source;
226 struct media_entity *sink;
228 source = media_get_entity_by_id(media, link->source.entity);
229 sink = media_get_entity_by_id(media, link->sink.entity);
231 if (source == NULL || sink == NULL) {
232 printf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
233 id, i, link->source.entity, link->source.index,
234 link->sink.entity, link->sink.index);
237 fwdlink = media_entity_add_link(source);
238 fwdlink->source = &source->pads[link->source.index];
239 fwdlink->sink = &sink->pads[link->sink.index];
240 fwdlink->flags = links.links[i].flags;
242 backlink = media_entity_add_link(sink);
243 backlink->source = &source->pads[link->source.index];
244 backlink->sink = &sink->pads[link->sink.index];
245 backlink->flags = links.links[i].flags;
247 fwdlink->twin = backlink;
248 backlink->twin = fwdlink;
259 static int media_enum_entities(struct media_device *media)
261 struct media_entity *entity;
271 for (id = 0; ; id = entity->info.id) {
272 size = (media->entities_count + 1) * sizeof(*media->entities);
273 media->entities = realloc(media->entities, size);
275 entity = &media->entities[media->entities_count];
276 memset(entity, 0, sizeof(*entity));
278 entity->info.id = id | MEDIA_ENTITY_ID_FLAG_NEXT;
280 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
287 /* Number of links (for outbound links) plus number of pads (for
288 * inbound links) is a good safe initial estimate of the total
291 entity->max_links = entity->info.pads + entity->info.links;
293 entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
294 entity->links = malloc(entity->max_links * sizeof(*entity->links));
295 if (entity->pads == NULL || entity->links == NULL)
298 media->entities_count++;
300 /* Find the corresponding device name. */
301 if (media_entity_type(entity) != MEDIA_ENTITY_TYPE_NODE &&
302 media_entity_type(entity) != MEDIA_ENTITY_TYPE_SUBDEV)
305 sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
306 entity->info.v4l.minor);
307 ret = readlink(sysname, target, sizeof(target));
312 p = strrchr(target, '/');
316 sprintf(devname, "/dev/%s", p + 1);
317 ret = stat(devname, &devstat);
321 /* Sanity check: udev might have reordered the device nodes.
322 * Make sure the major/minor match. We should really use
325 if (major(devstat.st_rdev) == entity->info.v4l.major &&
326 minor(devstat.st_rdev) == entity->info.v4l.minor)
327 strcpy(entity->devname, devname);
336 struct media_device *media_open(const char *name, int verbose)
338 struct media_device *media;
341 media = malloc(sizeof(*media));
343 printf("%s: unable to allocate memory\n", __func__);
346 memset(media, 0, sizeof(*media));
349 printf("Opening media device %s\n", name);
350 media->fd = open(name, O_RDWR);
353 printf("%s: Can't open media device %s\n", __func__, name);
358 printf("Enumerating entities\n");
360 ret = media_enum_entities(media);
362 printf("%s: Unable to enumerate entities for device %s (%s)\n",
363 __func__, name, strerror(-ret));
369 printf("Found %u entities\n", media->entities_count);
370 printf("Enumerating pads and links\n");
373 ret = media_enum_links(media);
375 printf("%s: Unable to enumerate pads and linksfor device %s\n",
387 void media_close(struct media_device *media)
394 for (i = 0; i < media->entities_count; ++i) {
395 struct media_entity *entity = &media->entities[i];
399 if (entity->fd != -1)
403 free(media->entities);