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>
37 struct media_pad *media_entity_remote_source(struct media_pad *pad)
41 if (!(pad->flags & MEDIA_PAD_FL_SINK))
44 for (i = 0; i < pad->entity->num_links; ++i) {
45 struct media_link *link = &pad->entity->links[i];
47 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
50 if (link->sink == pad)
57 struct media_entity *media_get_entity_by_name(struct media_device *media,
58 const char *name, size_t length)
62 for (i = 0; i < media->entities_count; ++i) {
63 struct media_entity *entity = &media->entities[i];
65 if (strncmp(entity->info.name, name, length) == 0)
72 struct media_entity *media_get_entity_by_id(struct media_device *media,
77 for (i = 0; i < media->entities_count; ++i) {
78 struct media_entity *entity = &media->entities[i];
80 if (entity->info.id == id)
87 int media_setup_link(struct media_device *media,
88 struct media_pad *source,
89 struct media_pad *sink,
92 struct media_link *link;
93 struct media_link_desc ulink;
97 for (i = 0; i < source->entity->num_links; i++) {
98 link = &source->entity->links[i];
100 if (link->source->entity == source->entity &&
101 link->source->index == source->index &&
102 link->sink->entity == sink->entity &&
103 link->sink->index == sink->index)
107 if (i == source->entity->num_links) {
108 printf("%s: Link not found\n", __func__);
113 ulink.source.entity = source->entity->info.id;
114 ulink.source.index = source->index;
115 ulink.source.flags = MEDIA_PAD_FL_SOURCE;
118 ulink.sink.entity = sink->entity->info.id;
119 ulink.sink.index = sink->index;
120 ulink.sink.flags = MEDIA_PAD_FL_SINK;
122 ulink.flags = flags | (link->flags & MEDIA_LNK_FL_IMMUTABLE);
124 ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
126 printf("%s: Unable to setup link (%s)\n", __func__,
131 link->flags = ulink.flags;
132 link->twin->flags = ulink.flags;
136 int media_reset_links(struct media_device *media)
141 for (i = 0; i < media->entities_count; ++i) {
142 struct media_entity *entity = &media->entities[i];
144 for (j = 0; j < entity->num_links; j++) {
145 struct media_link *link = &entity->links[j];
147 if (link->flags & MEDIA_LNK_FL_IMMUTABLE ||
148 link->source->entity != entity)
151 ret = media_setup_link(media, link->source, link->sink,
152 link->flags & ~MEDIA_LNK_FL_ENABLED);
161 static struct media_link *media_entity_add_link(struct media_entity *entity)
163 if (entity->num_links >= entity->max_links) {
164 struct media_link *links = entity->links;
165 unsigned int max_links = entity->max_links * 2;
168 links = realloc(links, max_links * sizeof *links);
172 for (i = 0; i < entity->num_links; ++i)
173 links[i].twin->twin = &links[i];
175 entity->max_links = max_links;
176 entity->links = links;
179 return &entity->links[entity->num_links++];
182 static int media_enum_links(struct media_device *media)
187 for (id = 1; id <= media->entities_count; id++) {
188 struct media_entity *entity = &media->entities[id - 1];
189 struct media_links_enum links;
192 links.entity = entity->info.id;
193 links.pads = malloc(entity->info.pads * sizeof(struct media_pad_desc));
194 links.links = malloc(entity->info.links * sizeof(struct media_link_desc));
196 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
197 printf("%s: Unable to enumerate pads and links (%s).\n",
198 __func__, strerror(errno));
204 for (i = 0; i < entity->info.pads; ++i) {
205 entity->pads[i].entity = entity;
206 entity->pads[i].index = links.pads[i].index;
207 entity->pads[i].flags = links.pads[i].flags;
210 for (i = 0; i < entity->info.links; ++i) {
211 struct media_link_desc *link = &links.links[i];
212 struct media_link *fwdlink;
213 struct media_link *backlink;
214 struct media_entity *source;
215 struct media_entity *sink;
217 source = media_get_entity_by_id(media, link->source.entity);
218 sink = media_get_entity_by_id(media, link->sink.entity);
220 if (source == NULL || sink == NULL) {
221 printf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
222 id, i, link->source.entity, link->source.index,
223 link->sink.entity, link->sink.index);
226 fwdlink = media_entity_add_link(source);
227 fwdlink->source = &source->pads[link->source.index];
228 fwdlink->sink = &sink->pads[link->sink.index];
229 fwdlink->flags = link->flags;
231 backlink = media_entity_add_link(sink);
232 backlink->source = &source->pads[link->source.index];
233 backlink->sink = &sink->pads[link->sink.index];
234 backlink->flags = link->flags;
236 fwdlink->twin = backlink;
237 backlink->twin = fwdlink;
248 static int media_enum_entities(struct media_device *media)
250 struct media_entity *entity;
260 for (id = 0; ; id = entity->info.id) {
261 size = (media->entities_count + 1) * sizeof(*media->entities);
262 media->entities = realloc(media->entities, size);
264 entity = &media->entities[media->entities_count];
265 memset(entity, 0, sizeof(*entity));
267 entity->info.id = id | MEDIA_ENT_ID_FLAG_NEXT;
269 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
271 ret = errno != EINVAL ? -errno : 0;
275 /* Number of links (for outbound links) plus number of pads (for
276 * inbound links) is a good safe initial estimate of the total
279 entity->max_links = entity->info.pads + entity->info.links;
281 entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
282 entity->links = malloc(entity->max_links * sizeof(*entity->links));
283 if (entity->pads == NULL || entity->links == NULL) {
288 media->entities_count++;
290 /* Find the corresponding device name. */
291 if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE &&
292 media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV)
295 sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
296 entity->info.v4l.minor);
297 ret = readlink(sysname, target, sizeof(target));
302 p = strrchr(target, '/');
306 sprintf(devname, "/dev/%s", p + 1);
307 ret = stat(devname, &devstat);
311 /* Sanity check: udev might have reordered the device nodes.
312 * Make sure the major/minor match. We should really use
315 if (major(devstat.st_rdev) == entity->info.v4l.major &&
316 minor(devstat.st_rdev) == entity->info.v4l.minor)
317 strcpy(entity->devname, devname);
323 struct media_device *media_open(const char *name, int verbose)
325 struct media_device *media;
328 media = malloc(sizeof(*media));
330 printf("%s: unable to allocate memory\n", __func__);
333 memset(media, 0, sizeof(*media));
336 printf("Opening media device %s\n", name);
337 media->fd = open(name, O_RDWR);
340 printf("%s: Can't open media device %s\n", __func__, name);
345 printf("Enumerating entities\n");
347 ret = media_enum_entities(media);
349 printf("%s: Unable to enumerate entities for device %s (%s)\n",
350 __func__, name, strerror(-ret));
356 printf("Found %u entities\n", media->entities_count);
357 printf("Enumerating pads and links\n");
360 ret = media_enum_links(media);
362 printf("%s: Unable to enumerate pads and linksfor device %s\n",
371 void media_close(struct media_device *media)
378 for (i = 0; i < media->entities_count; ++i) {
379 struct media_entity *entity = &media->entities[i];
383 if (entity->fd != -1)
387 free(media->entities);