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 */
44 #ifdef KERNEL_HEADER_MODIFICATION
45 #include "../kernel_header/compiler.h"
46 #include "../kernel_header/media.h"
48 #include <linux/compiler.h>
49 #include <linux/media.h>
53 #include <linux/kdev_t.h>
54 #include <linux/types.h>
56 #include "exynos_v4l2.h"
58 //#define LOG_NDEBUG 0
59 #define LOG_TAG "libexynosv4l2-mc"
60 #ifndef SLP_PLATFORM /* build env */
61 #include <utils/Log.h>
63 #include "Exynos_OSAL_Log.h"
66 static inline unsigned int __media_entity_type(struct media_entity *entity)
68 return entity->info.type & MEDIA_ENT_TYPE_MASK;
71 static void __media_debug_default(void *ptr, ...)
74 va_start(argptr, ptr);
75 vprintf((const char*)ptr, argptr);
79 static void __media_debug_set_handler(
80 struct media_device *media,
81 void (*debug_handler)(void *, ...),
85 media->debug_handler = debug_handler;
86 media->debug_priv = debug_priv;
88 media->debug_handler = __media_debug_default;
89 media->debug_priv = NULL;
93 static struct media_link *__media_entity_add_link(struct media_entity *entity)
95 if (entity->num_links >= entity->max_links) {
96 struct media_link *links = entity->links;
97 unsigned int max_links = entity->max_links * 2;
100 links = (struct media_link*)realloc(links, max_links * sizeof *links);
104 for (i = 0; i < entity->num_links; ++i)
105 links[i].twin->twin = &links[i];
107 entity->max_links = max_links;
108 entity->links = links;
111 return &entity->links[entity->num_links++];
115 static int __media_enum_links(struct media_device *media)
117 ALOGD("%s: start", __func__);
121 for (id = 1; id <= media->entities_count; id++) {
122 struct media_entity *entity = &media->entities[id - 1];
123 struct media_links_enum links;
126 links.entity = entity->info.id;
127 links.pads = (struct media_pad_desc*)malloc(entity->info.pads * sizeof(struct media_pad_desc));
128 links.links = (struct media_link_desc*)malloc(entity->info.links * sizeof(struct media_link_desc));
130 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
131 ALOGE("Unable to enumerate pads and links (%s)", strerror(errno));
137 for (i = 0; i < entity->info.pads; ++i) {
138 entity->pads[i].entity = entity;
139 entity->pads[i].index = links.pads[i].index;
140 entity->pads[i].flags = links.pads[i].flags;
143 for (i = 0; i < entity->info.links; ++i) {
144 struct media_link_desc *link = &links.links[i];
145 struct media_link *fwdlink;
146 struct media_link *backlink;
147 struct media_entity *source;
148 struct media_entity *sink;
150 source = exynos_media_get_entity_by_id(media, link->source.entity);
151 sink = exynos_media_get_entity_by_id(media, link->sink.entity);
152 if (source == NULL || sink == NULL) {
153 ALOGE("WARNING entity %u link %u from %u/%u to %u/%u is invalid!",
154 id, i, link->source.entity,
160 fwdlink = __media_entity_add_link(source);
161 fwdlink->source = &source->pads[link->source.index];
162 fwdlink->sink = &sink->pads[link->sink.index];
163 fwdlink->flags = link->flags;
165 backlink = __media_entity_add_link(sink);
166 backlink->source = &source->pads[link->source.index];
167 backlink->sink = &sink->pads[link->sink.index];
168 backlink->flags = link->flags;
170 fwdlink->twin = backlink;
171 backlink->twin = fwdlink;
181 static int __media_get_devname_sysfs(struct media_entity *entity)
183 //struct stat devstat;
190 sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
191 entity->info.v4l.minor);
193 ret = readlink(sysname, target, sizeof(target));
198 p = strrchr(target, '/');
202 sprintf(devname, "/tmp/%s", p + 1);
204 ret = mknod(devname, 0666 | S_IFCHR, MKDEV(81, entity->info.v4l.minor));
205 strcpy(entity->devname, devname);
210 static int __media_get_media_fd(const char *filename, struct media_device *media)
217 ALOGD("%s: %s", __func__, filename);
219 media->fd = open(filename, O_RDWR, 0);
221 ALOGE("Open sysfs media device failed, media->fd: %d", media->fd);
225 ALOGD("%s: media->fd: %d", __func__, media->fd);
231 static int __media_enum_entities(struct media_device *media)
233 struct media_entity *entity, *temp_entity;
238 temp_entity = entity = (struct media_entity*)calloc(1, sizeof(struct media_entity));
239 for (id = 0, ret = 0; ; id = entity->info.id) {
240 size = (media->entities_count + 1) * sizeof(*media->entities);
241 media->entities = (struct media_entity*)realloc(media->entities, size);
243 entity = &media->entities[media->entities_count];
244 memset(entity, 0, sizeof(*entity));
246 entity->info.id = id | MEDIA_ENT_ID_FLAG_NEXT;
247 entity->media = media;
249 ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
252 ret = errno != EINVAL ? -errno : 0;
256 /* Number of links (for outbound links) plus number of pads (for
257 * inbound links) is a good safe initial estimate of the total
260 entity->max_links = entity->info.pads + entity->info.links;
262 entity->pads = (struct media_pad*)malloc(entity->info.pads * sizeof(*entity->pads));
263 entity->links = (struct media_link*)malloc(entity->max_links * sizeof(*entity->links));
264 if (entity->pads == NULL || entity->links == NULL) {
269 media->entities_count++;
271 /* Find the corresponding device name. */
272 if (__media_entity_type(entity) != MEDIA_ENT_T_DEVNODE &&
273 __media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV)
276 /* Fall back to get the device name via sysfs */
277 __media_get_devname_sysfs(entity);
279 ALOGE("media_get_devname failed");
286 static struct media_device *__media_open_debug(
287 const char *filename,
288 void (*debug_handler)(void *, ...),
291 struct media_device *media;
294 media = (struct media_device *)calloc(1, sizeof(struct media_device));
296 ALOGE("media: %p", media);
300 __media_debug_set_handler(media, debug_handler, debug_priv);
302 ALOGD("%s: Opening media device %s", __func__, filename);
303 ALOGD("%s: media: %p", __func__, media);
305 media->fd = __media_get_media_fd(filename, media);
307 exynos_media_close(media);
308 ALOGE("failed __media_get_media_fd %s", filename);
312 ALOGD("%s: media->fd: %d", __func__, media->fd);
313 ret = __media_enum_entities(media);
316 ALOGE("Unable to enumerate entities for device %s (%s)", filename, strerror(-ret));
317 exynos_media_close(media);
321 ALOGD("%s: Found %u entities", __func__, media->entities_count);
322 ALOGD("%s: Enumerating pads and links", __func__);
324 ret = __media_enum_links(media);
326 ALOGE("Unable to enumerate pads and links for device %s", filename);
327 exynos_media_close(media);
335 * @brief Open a media device.
336 * @param filename - name (including path) of the device node.
338 * Open the media device referenced by @a filename and enumerate entities, pads and
341 * @return A pointer to a newly allocated media_device structure instance on
342 * success and NULL on failure. The returned pointer must be freed with
343 * exynos_media_close when the device isn't needed anymore.
345 struct media_device *exynos_media_open(const char *filename)
347 return __media_open_debug(filename, (void (*)(void *, ...))fprintf, stdout);
351 * @brief Close a media device.
352 * @param media - device instance.
354 * Close the @a media device instance and free allocated resources. Access to the
355 * device instance is forbidden after this function returns.
357 void exynos_media_close(struct media_device *media)
364 for (i = 0; i < media->entities_count; ++i) {
365 struct media_entity *entity = &media->entities[i];
369 if (entity->fd != -1)
373 free(media->entities);
378 * @brief Locate the pad at the other end of a link.
379 * @param pad - sink pad at one end of the link.
381 * Locate the source pad connected to @a pad through an enabled link. As only one
382 * link connected to a sink pad can be enabled at a time, the connected source
383 * pad is guaranteed to be unique.
385 * @return A pointer to the connected source pad, or NULL if all links connected
386 * to @a pad are disabled. Return NULL also if @a pad is not a sink pad.
388 struct media_pad *exynos_media_entity_remote_source(struct media_pad *pad)
392 if (!(pad->flags & MEDIA_PAD_FL_SINK))
395 for (i = 0; i < pad->entity->num_links; ++i) {
396 struct media_link *link = &pad->entity->links[i];
398 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
401 if (link->sink == pad)
409 * @brief Find an entity by its name.
410 * @param media - media device.
411 * @param name - entity name.
412 * @param length - size of @a name.
414 * Search for an entity with a name equal to @a name.
416 * @return A pointer to the entity if found, or NULL otherwise.
418 struct media_entity *exynos_media_get_entity_by_name(struct media_device *media,
419 const char *name, size_t length)
422 struct media_entity *entity;
424 for (i = 0; i < media->entities_count; ++i) {
425 entity = &media->entities[i];
427 if (strncmp(entity->info.name, name, length) == 0)
435 * @brief Find an entity by its ID.
436 * @param media - media device.
437 * @param id - entity ID.
439 * Search for an entity with an ID equal to @a id.
441 * @return A pointer to the entity if found, or NULL otherwise.
443 struct media_entity *exynos_media_get_entity_by_id(struct media_device *media,
448 for (i = 0; i < media->entities_count; ++i) {
449 struct media_entity *entity = &media->entities[i];
451 if (entity->info.id == id)
459 * @brief Configure a link.
460 * @param media - media device.
461 * @param source - source pad at the link origin.
462 * @param sink - sink pad at the link target.
463 * @param flags - configuration flags.
465 * Locate the link between @a source and @a sink, and configure it by applying
468 * Only the MEDIA_LINK_FLAG_ENABLED flag is writable.
470 * @return 0 on success, -1 on failure:
471 * -ENOENT: link not found
472 * - other error codes returned by MEDIA_IOC_SETUP_LINK
474 int exynos_media_setup_link(struct media_device *media,
475 struct media_pad *source,
476 struct media_pad *sink,
479 struct media_link *link;
480 struct media_link_desc ulink;
484 for (i = 0; i < source->entity->num_links; i++) {
485 link = &source->entity->links[i];
487 if (link->source->entity == source->entity &&
488 link->source->index == source->index &&
489 link->sink->entity == sink->entity &&
490 link->sink->index == sink->index)
494 if (i == source->entity->num_links) {
495 ALOGE("Link not found");
500 ulink.source.entity = source->entity->info.id;
501 ulink.source.index = source->index;
502 ulink.source.flags = MEDIA_PAD_FL_SOURCE;
505 ulink.sink.entity = sink->entity->info.id;
506 ulink.sink.index = sink->index;
507 ulink.sink.flags = MEDIA_PAD_FL_SINK;
509 ulink.flags = flags | (link->flags & MEDIA_LNK_FL_IMMUTABLE);
511 ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
513 ALOGE("Unable to setup link (%s)", strerror(errno));
517 link->flags = ulink.flags;
518 link->twin->flags = ulink.flags;
523 * @brief Reset all links to the disabled state.
524 * @param media - media device.
526 * Disable all links in the media device. This function is usually used after
527 * opening a media device to reset all links to a known state.
529 * @return 0 on success, or a negative error code on failure.
531 int exynos_media_reset_links(struct media_device *media)
536 for (i = 0; i < media->entities_count; ++i) {
537 struct media_entity *entity = &media->entities[i];
539 for (j = 0; j < entity->num_links; j++) {
540 struct media_link *link = &entity->links[j];
542 if (link->flags & MEDIA_LNK_FL_IMMUTABLE ||
543 link->source->entity != entity)
546 ret = exynos_media_setup_link(media, link->source, link->sink,
547 link->flags & ~MEDIA_LNK_FL_ENABLED);
560 static inline int __media_udev_open(struct udev **udev)
568 static inline void __media_udev_close(struct udev *udev)
574 static int __media_get_devname_udev(struct udev *udev,
575 struct media_entity *entity)
577 struct udev_device *device;
585 devnum = makedev(entity->info.v4l.major, entity->info.v4l.minor);
586 ALOGE("looking up device: %u:%u",
587 major(devnum), minor(devnum));
588 device = udev_device_new_from_devnum(udev, 'c', devnum);
590 p = udev_device_get_devnode(device);
592 strncpy(entity->devname, p, sizeof(entity->devname));
593 entity->devname[sizeof(entity->devname) - 1] = '\0';
598 udev_device_unref(device);
603 #else /* HAVE_LIBUDEV */
607 static inline int __media_udev_open(struct udev **udev) { return 0; }
609 static inline void __media_udev_close(struct udev *udev) { }
611 static inline int __media_get_devname_udev(struct udev *udev,
612 struct media_entity *entity)
617 #endif /* HAVE_LIBUDEV */
620 * @brief Parse string to a pad on the media device.
621 * @param media - media device.
622 * @param p - input string
623 * @param endp - pointer to string where parsing ended
625 * Parse NULL terminated string describing a pad and return its struct
626 * media_pad instance.
628 * @return Pointer to struct media_pad on success, NULL on failure.
630 struct media_pad *exynos_media_parse_pad(struct media_device *media,
631 const char *p, char **endp)
633 unsigned int entity_id, pad;
634 struct media_entity *entity;
637 for (; isspace(*p); ++p);
640 for (end = (char *)p + 1; *end && *end != '"'; ++end);
644 entity = exynos_media_get_entity_by_name(media, p + 1, end - p - 1);
650 entity_id = strtoul(p, &end, 10);
651 entity = exynos_media_get_entity_by_id(media, entity_id);
655 for (; isspace(*end); ++end);
659 for (p = end + 1; isspace(*p); ++p);
661 pad = strtoul(p, &end, 10);
662 for (p = end; isspace(*p); ++p);
664 if (pad >= entity->info.pads)
667 for (p = end; isspace(*p); ++p);
671 return &entity->pads[pad];
675 * @brief Parse string to a link on the media device.
676 * @param media - media device.
677 * @param p - input string
678 * @param endp - pointer to p where parsing ended
680 * Parse NULL terminated string p describing a link and return its struct
681 * media_link instance.
683 * @return Pointer to struct media_link on success, NULL on failure.
685 struct media_link *exynos_media_parse_link(
686 struct media_device *media,
690 struct media_link *link;
691 struct media_pad *source;
692 struct media_pad *sink;
696 source = exynos_media_parse_pad(media, p, &end);
700 if (end[0] != '-' || end[1] != '>')
704 sink = exynos_media_parse_pad(media, p, &end);
710 for (i = 0; i < source->entity->num_links; i++) {
711 link = &source->entity->links[i];
713 if (link->source == source && link->sink == sink)
721 * @brief Parse string to a link on the media device and set it up.
722 * @param media - media device.
723 * @param p - input string
725 * Parse NULL terminated string p describing a link and its configuration
726 * and configure the link.
728 * @return 0 on success, or a negative error code on failure.
730 int exynos_media_parse_setup_link(
731 struct media_device *media,
735 struct media_link *link;
739 link = exynos_media_parse_link(media, p, &end);
741 ALOGE("Unable to parse link");
747 ALOGE("Unable to parse link flags");
751 flags = strtoul(p, &end, 10);
752 for (p = end; isspace(*p); p++);
754 ALOGE("Unable to parse link flags");
758 for (; isspace(*p); p++);
761 ALOGD("%s: Setting up link %u:%u -> %u:%u [%u]", __func__,
762 link->source->entity->info.id, link->source->index,
763 link->sink->entity->info.id, link->sink->index,
766 return exynos_media_setup_link(media, link->source, link->sink, flags);
770 * @brief Parse string to link(s) on the media device and set it up.
771 * @param media - media device.
772 * @param p - input string
774 * Parse NULL terminated string p describing link(s) separated by
775 * commas (,) and configure the link(s).
777 * @return 0 on success, or a negative error code on failure.
779 int exynos_media_parse_setup_links(struct media_device *media, const char *p)
785 ret = exynos_media_parse_setup_link(media, p, &end);
790 } while (*end == ',');
792 return *end ? -EINVAL : 0;