2 * Copyright 2012 Samsung Electronics S.LSI Co. LTD
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * \brief source file for libexynosv4l2
20 * \author Jinsung Yang (jsgood.yang@samsung.com)
21 * \author Sangwoo Park (sw5771.park@samsung.com)
24 * <b>Revision History: </b>
25 * - 2012/01/17: Jinsung Yang (jsgood.yang@samsung.com) \n
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
40 #ifndef SLP_PLATFORM /* build env */
43 #include <linux/media.h>
45 #include <linux/kdev_t.h>
46 #include <linux/types.h>
48 #include "exynos_v4l2.h"
50 //#define LOG_NDEBUG 0
51 #define LOG_TAG "libexynosv4l2-mc"
52 #ifndef SLP_PLATFORM /* build env */
53 #include <utils/Log.h>
55 #include "Exynos_OSAL_Log.h"
58 static inline unsigned int __media_entity_type(struct media_entity *entity)
60 return entity->info.type & MEDIA_ENT_TYPE_MASK;
63 static void __media_debug_default(void *ptr, ...)
66 va_start(argptr, ptr);
67 vprintf((const char*)ptr, argptr);
71 static void __media_debug_set_handler(
72 struct media_device *media,
73 void (*debug_handler)(void *, ...),
77 media->debug_handler = debug_handler;
78 media->debug_priv = debug_priv;
80 media->debug_handler = __media_debug_default;
81 media->debug_priv = NULL;
85 static struct media_link *__media_entity_add_link(struct media_entity *entity)
87 if (entity->num_links >= entity->max_links) {
88 struct media_link *links = entity->links;
89 unsigned int max_links = entity->max_links * 2;
92 links = (struct media_link*)realloc(links, max_links * sizeof *links);
96 for (i = 0; i < entity->num_links; ++i)
97 links[i].twin->twin = &links[i];
99 entity->max_links = max_links;
100 entity->links = links;
103 return &entity->links[entity->num_links++];
107 static int __media_enum_links(struct media_device *media)
109 ALOGD("%s: start", __func__);
113 for (id = 1; id <= media->entities_count; id++) {
114 struct media_entity *entity = &media->entities[id - 1];
115 struct media_links_enum links;
118 links.entity = entity->info.id;
119 links.pads = (struct media_pad_desc*)malloc(entity->info.pads * sizeof(struct media_pad_desc));
120 links.links = (struct media_link_desc*)malloc(entity->info.links * sizeof(struct media_link_desc));
122 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
123 ALOGE("Unable to enumerate pads and links (%s)", strerror(errno));
129 for (i = 0; i < entity->info.pads; ++i) {
130 entity->pads[i].entity = entity;
131 entity->pads[i].index = links.pads[i].index;
132 entity->pads[i].flags = links.pads[i].flags;
135 for (i = 0; i < entity->info.links; ++i) {
136 struct media_link_desc *link = &links.links[i];
137 struct media_link *fwdlink;
138 struct media_link *backlink;
139 struct media_entity *source;
140 struct media_entity *sink;
142 source = exynos_media_get_entity_by_id(media, link->source.entity);
143 sink = exynos_media_get_entity_by_id(media, link->sink.entity);
144 if (source == NULL || sink == NULL) {
145 ALOGE("WARNING entity %u link %u from %u/%u to %u/%u is invalid!",
146 id, i, link->source.entity,
152 fwdlink = __media_entity_add_link(source);
153 fwdlink->source = &source->pads[link->source.index];
154 fwdlink->sink = &sink->pads[link->sink.index];
155 fwdlink->flags = link->flags;
157 backlink = __media_entity_add_link(sink);
158 backlink->source = &source->pads[link->source.index];
159 backlink->sink = &sink->pads[link->sink.index];
160 backlink->flags = link->flags;
162 fwdlink->twin = backlink;
163 backlink->twin = fwdlink;
173 static int __media_get_devname_sysfs(struct media_entity *entity)
175 //struct stat devstat;
182 sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
183 entity->info.v4l.minor);
185 ret = readlink(sysname, target, sizeof(target));
190 p = strrchr(target, '/');
194 sprintf(devname, "/tmp/%s", p + 1);
196 ret = mknod(devname, 0666 | S_IFCHR, MKDEV(81, entity->info.v4l.minor));
197 strcpy(entity->devname, devname);
202 static int __media_get_media_fd(const char *filename, struct media_device *media)
209 ALOGD("%s: %s", __func__, filename);
211 media->fd = open(filename, O_RDWR, 0);
213 ALOGE("Open sysfs media device failed, media->fd: %d", media->fd);
217 ALOGD("%s: media->fd: %d", __func__, media->fd);
223 static int __media_enum_entities(struct media_device *media)
225 struct media_entity *entity, *temp_entity;
230 temp_entity = entity = (struct media_entity*)calloc(1, sizeof(struct media_entity));
231 for (id = 0, ret = 0; ; id = entity->info.id) {
232 size = (media->entities_count + 1) * sizeof(*media->entities);
233 media->entities = (struct media_entity*)realloc(media->entities, size);
235 entity = &media->entities[media->entities_count];
236 memset(entity, 0, sizeof(*entity));
238 entity->info.id = id | MEDIA_ENT_ID_FLAG_NEXT;
239 entity->media = media;
241 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
244 ret = errno != EINVAL ? -errno : 0;
248 /* Number of links (for outbound links) plus number of pads (for
249 * inbound links) is a good safe initial estimate of the total
252 entity->max_links = entity->info.pads + entity->info.links;
254 entity->pads = (struct media_pad*)malloc(entity->info.pads * sizeof(*entity->pads));
255 entity->links = (struct media_link*)malloc(entity->max_links * sizeof(*entity->links));
256 if (entity->pads == NULL || entity->links == NULL) {
261 media->entities_count++;
263 /* Find the corresponding device name. */
264 if (__media_entity_type(entity) != MEDIA_ENT_T_DEVNODE &&
265 __media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV)
268 /* Fall back to get the device name via sysfs */
269 __media_get_devname_sysfs(entity);
271 ALOGE("media_get_devname failed");
278 static struct media_device *__media_open_debug(
279 const char *filename,
280 void (*debug_handler)(void *, ...),
283 struct media_device *media;
286 media = (struct media_device *)calloc(1, sizeof(struct media_device));
288 ALOGE("media: %p", media);
292 __media_debug_set_handler(media, debug_handler, debug_priv);
294 ALOGD("%s: Opening media device %s", __func__, filename);
295 ALOGD("%s: media: %p", __func__, media);
297 media->fd = __media_get_media_fd(filename, media);
299 exynos_media_close(media);
300 ALOGE("failed __media_get_media_fd %s", filename);
304 ALOGD("%s: media->fd: %d", __func__, media->fd);
305 ret = __media_enum_entities(media);
308 ALOGE("Unable to enumerate entities for device %s (%s)", filename, strerror(-ret));
309 exynos_media_close(media);
313 ALOGD("%s: Found %u entities", __func__, media->entities_count);
314 ALOGD("%s: Enumerating pads and links", __func__);
316 ret = __media_enum_links(media);
318 ALOGE("Unable to enumerate pads and links for device %s", filename);
319 exynos_media_close(media);
327 * @brief Open a media device.
328 * @param filename - name (including path) of the device node.
330 * Open the media device referenced by @a filename and enumerate entities, pads and
333 * @return A pointer to a newly allocated media_device structure instance on
334 * success and NULL on failure. The returned pointer must be freed with
335 * exynos_media_close when the device isn't needed anymore.
337 struct media_device *exynos_media_open(const char *filename)
339 return __media_open_debug(filename, (void (*)(void *, ...))fprintf, stdout);
343 * @brief Close a media device.
344 * @param media - device instance.
346 * Close the @a media device instance and free allocated resources. Access to the
347 * device instance is forbidden after this function returns.
349 void exynos_media_close(struct media_device *media)
356 for (i = 0; i < media->entities_count; ++i) {
357 struct media_entity *entity = &media->entities[i];
361 if (entity->fd != -1)
365 free(media->entities);
370 * @brief Locate the pad at the other end of a link.
371 * @param pad - sink pad at one end of the link.
373 * Locate the source pad connected to @a pad through an enabled link. As only one
374 * link connected to a sink pad can be enabled at a time, the connected source
375 * pad is guaranteed to be unique.
377 * @return A pointer to the connected source pad, or NULL if all links connected
378 * to @a pad are disabled. Return NULL also if @a pad is not a sink pad.
380 struct media_pad *exynos_media_entity_remote_source(struct media_pad *pad)
384 if (!(pad->flags & MEDIA_PAD_FL_SINK))
387 for (i = 0; i < pad->entity->num_links; ++i) {
388 struct media_link *link = &pad->entity->links[i];
390 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
393 if (link->sink == pad)
401 * @brief Find an entity by its name.
402 * @param media - media device.
403 * @param name - entity name.
404 * @param length - size of @a name.
406 * Search for an entity with a name equal to @a name.
408 * @return A pointer to the entity if found, or NULL otherwise.
410 struct media_entity *exynos_media_get_entity_by_name(struct media_device *media,
411 const char *name, size_t length)
414 struct media_entity *entity;
416 for (i = 0; i < media->entities_count; ++i) {
417 entity = &media->entities[i];
419 if (strncmp(entity->info.name, name, length) == 0)
427 * @brief Find an entity by its ID.
428 * @param media - media device.
429 * @param id - entity ID.
431 * Search for an entity with an ID equal to @a id.
433 * @return A pointer to the entity if found, or NULL otherwise.
435 struct media_entity *exynos_media_get_entity_by_id(struct media_device *media,
440 for (i = 0; i < media->entities_count; ++i) {
441 struct media_entity *entity = &media->entities[i];
443 if (entity->info.id == id)
451 * @brief Configure a link.
452 * @param media - media device.
453 * @param source - source pad at the link origin.
454 * @param sink - sink pad at the link target.
455 * @param flags - configuration flags.
457 * Locate the link between @a source and @a sink, and configure it by applying
460 * Only the MEDIA_LINK_FLAG_ENABLED flag is writable.
462 * @return 0 on success, -1 on failure:
463 * -ENOENT: link not found
464 * - other error codes returned by MEDIA_IOC_SETUP_LINK
466 int exynos_media_setup_link(struct media_device *media,
467 struct media_pad *source,
468 struct media_pad *sink,
471 struct media_link *link;
472 struct media_link_desc ulink;
476 for (i = 0; i < source->entity->num_links; i++) {
477 link = &source->entity->links[i];
479 if (link->source->entity == source->entity &&
480 link->source->index == source->index &&
481 link->sink->entity == sink->entity &&
482 link->sink->index == sink->index)
486 if (i == source->entity->num_links) {
487 ALOGE("Link not found");
492 ulink.source.entity = source->entity->info.id;
493 ulink.source.index = source->index;
494 ulink.source.flags = MEDIA_PAD_FL_SOURCE;
497 ulink.sink.entity = sink->entity->info.id;
498 ulink.sink.index = sink->index;
499 ulink.sink.flags = MEDIA_PAD_FL_SINK;
501 ulink.flags = flags | (link->flags & MEDIA_LNK_FL_IMMUTABLE);
503 ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
505 ALOGE("Unable to setup link (%s)", strerror(errno));
509 link->flags = ulink.flags;
510 link->twin->flags = ulink.flags;
515 * @brief Reset all links to the disabled state.
516 * @param media - media device.
518 * Disable all links in the media device. This function is usually used after
519 * opening a media device to reset all links to a known state.
521 * @return 0 on success, or a negative error code on failure.
523 int exynos_media_reset_links(struct media_device *media)
528 for (i = 0; i < media->entities_count; ++i) {
529 struct media_entity *entity = &media->entities[i];
531 for (j = 0; j < entity->num_links; j++) {
532 struct media_link *link = &entity->links[j];
534 if (link->flags & MEDIA_LNK_FL_IMMUTABLE ||
535 link->source->entity != entity)
538 ret = exynos_media_setup_link(media, link->source, link->sink,
539 link->flags & ~MEDIA_LNK_FL_ENABLED);
552 static inline int __media_udev_open(struct udev **udev)
560 static inline void __media_udev_close(struct udev *udev)
566 static int __media_get_devname_udev(struct udev *udev,
567 struct media_entity *entity)
569 struct udev_device *device;
577 devnum = makedev(entity->info.v4l.major, entity->info.v4l.minor);
578 ALOGE("looking up device: %u:%u",
579 major(devnum), minor(devnum));
580 device = udev_device_new_from_devnum(udev, 'c', devnum);
582 p = udev_device_get_devnode(device);
584 strncpy(entity->devname, p, sizeof(entity->devname));
585 entity->devname[sizeof(entity->devname) - 1] = '\0';
590 udev_device_unref(device);
595 #else /* HAVE_LIBUDEV */
599 static inline int __media_udev_open(struct udev **udev) { return 0; }
601 static inline void __media_udev_close(struct udev *udev) { }
603 static inline int __media_get_devname_udev(struct udev *udev,
604 struct media_entity *entity)
609 #endif /* HAVE_LIBUDEV */
612 * @brief Parse string to a pad on the media device.
613 * @param media - media device.
614 * @param p - input string
615 * @param endp - pointer to string where parsing ended
617 * Parse NULL terminated string describing a pad and return its struct
618 * media_pad instance.
620 * @return Pointer to struct media_pad on success, NULL on failure.
622 struct media_pad *exynos_media_parse_pad(struct media_device *media,
623 const char *p, char **endp)
625 unsigned int entity_id, pad;
626 struct media_entity *entity;
629 for (; isspace(*p); ++p);
632 for (end = (char *)p + 1; *end && *end != '"'; ++end);
636 entity = exynos_media_get_entity_by_name(media, p + 1, end - p - 1);
642 entity_id = strtoul(p, &end, 10);
643 entity = exynos_media_get_entity_by_id(media, entity_id);
647 for (; isspace(*end); ++end);
651 for (p = end + 1; isspace(*p); ++p);
653 pad = strtoul(p, &end, 10);
654 for (p = end; isspace(*p); ++p);
656 if (pad >= entity->info.pads)
659 for (p = end; isspace(*p); ++p);
663 return &entity->pads[pad];
667 * @brief Parse string to a link on the media device.
668 * @param media - media device.
669 * @param p - input string
670 * @param endp - pointer to p where parsing ended
672 * Parse NULL terminated string p describing a link and return its struct
673 * media_link instance.
675 * @return Pointer to struct media_link on success, NULL on failure.
677 struct media_link *exynos_media_parse_link(
678 struct media_device *media,
682 struct media_link *link;
683 struct media_pad *source;
684 struct media_pad *sink;
688 source = exynos_media_parse_pad(media, p, &end);
692 if (end[0] != '-' || end[1] != '>')
696 sink = exynos_media_parse_pad(media, p, &end);
702 for (i = 0; i < source->entity->num_links; i++) {
703 link = &source->entity->links[i];
705 if (link->source == source && link->sink == sink)
713 * @brief Parse string to a link on the media device and set it up.
714 * @param media - media device.
715 * @param p - input string
717 * Parse NULL terminated string p describing a link and its configuration
718 * and configure the link.
720 * @return 0 on success, or a negative error code on failure.
722 int exynos_media_parse_setup_link(
723 struct media_device *media,
727 struct media_link *link;
731 link = exynos_media_parse_link(media, p, &end);
733 ALOGE("Unable to parse link");
739 ALOGE("Unable to parse link flags");
743 flags = strtoul(p, &end, 10);
744 for (p = end; isspace(*p); p++);
746 ALOGE("Unable to parse link flags");
750 for (; isspace(*p); p++);
753 ALOGD("%s: Setting up link %u:%u -> %u:%u [%u]", __func__,
754 link->source->entity->info.id, link->source->index,
755 link->sink->entity->info.id, link->sink->index,
758 return exynos_media_setup_link(media, link->source, link->sink, flags);
762 * @brief Parse string to link(s) on the media device and set it up.
763 * @param media - media device.
764 * @param p - input string
766 * Parse NULL terminated string p describing link(s) separated by
767 * commas (,) and configure the link(s).
769 * @return 0 on success, or a negative error code on failure.
771 int exynos_media_parse_setup_links(struct media_device *media, const char *p)
777 ret = exynos_media_parse_setup_link(media, p, &end);
782 } while (*end == ',');
784 return *end ? -EINVAL : 0;