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
38 #include <sys/types.h>
39 #include <sys/ioctl.h>
41 #ifndef TIZEN_FEATURE_E3250 /* build env */
44 #include <linux/media.h>
46 #include <linux/kdev_t.h>
47 #include <linux/types.h>
49 #include "exynos_v4l2.h"
51 //#define LOG_NDEBUG 0
52 #define LOG_TAG "libexynosv4l2-mc"
53 #ifndef TIZEN_FEATURE_E3250 /* build env */
54 #include <utils/Log.h>
56 #include "Exynos_OSAL_Log.h"
59 static inline unsigned int __media_entity_type(struct media_entity *entity)
61 return entity->info.type & MEDIA_ENT_TYPE_MASK;
64 static void __media_debug_default(void *ptr, ...)
67 va_start(argptr, ptr);
68 vprintf((const char*)ptr, argptr);
72 static void __media_debug_set_handler(
73 struct media_device *media,
74 void (*debug_handler)(void *, ...),
78 media->debug_handler = debug_handler;
79 media->debug_priv = debug_priv;
81 media->debug_handler = __media_debug_default;
82 media->debug_priv = NULL;
86 static struct media_link *__media_entity_add_link(struct media_entity *entity)
88 if (entity->num_links >= entity->max_links) {
89 struct media_link *links = entity->links;
90 unsigned int max_links = entity->max_links * 2;
93 links = (struct media_link*)realloc(links, max_links * sizeof *links);
97 for (i = 0; i < entity->num_links; ++i)
98 links[i].twin->twin = &links[i];
100 entity->max_links = max_links;
101 entity->links = links;
104 return &entity->links[entity->num_links++];
108 static int __media_enum_links(struct media_device *media)
110 ALOGD("%s: start", __func__);
114 for (id = 1; id <= media->entities_count; id++) {
115 struct media_entity *entity = &media->entities[id - 1];
116 struct media_links_enum links;
119 links.entity = entity->info.id;
120 links.pads = (struct media_pad_desc*)malloc(entity->info.pads * sizeof(struct media_pad_desc));
121 links.links = (struct media_link_desc*)malloc(entity->info.links * sizeof(struct media_link_desc));
123 if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
124 ALOGE("Unable to enumerate pads and links (%s)", strerror(errno));
130 for (i = 0; i < entity->info.pads; ++i) {
131 entity->pads[i].entity = entity;
132 entity->pads[i].index = links.pads[i].index;
133 entity->pads[i].flags = links.pads[i].flags;
136 for (i = 0; i < entity->info.links; ++i) {
137 struct media_link_desc *link = &links.links[i];
138 struct media_link *fwdlink;
139 struct media_link *backlink;
140 struct media_entity *source;
141 struct media_entity *sink;
143 source = exynos_media_get_entity_by_id(media, link->source.entity);
144 sink = exynos_media_get_entity_by_id(media, link->sink.entity);
145 if (source == NULL || sink == NULL) {
146 ALOGE("WARNING entity %u link %u from %u/%u to %u/%u is invalid!",
147 id, i, link->source.entity,
153 fwdlink = __media_entity_add_link(source);
154 fwdlink->source = &source->pads[link->source.index];
155 fwdlink->sink = &sink->pads[link->sink.index];
156 fwdlink->flags = link->flags;
158 backlink = __media_entity_add_link(sink);
159 backlink->source = &source->pads[link->source.index];
160 backlink->sink = &sink->pads[link->sink.index];
161 backlink->flags = link->flags;
163 fwdlink->twin = backlink;
164 backlink->twin = fwdlink;
174 static int __media_get_devname_sysfs(struct media_entity *entity)
176 //struct stat devstat;
183 snprintf(sysname, sizeof(sysname), "/sys/dev/char/%u:%u", entity->info.v4l.major,
184 entity->info.v4l.minor);
186 ret = readlink(sysname, target, sizeof(target));
187 if (ret < 0 || ret >= (int)sizeof(target))
191 p = strrchr(target, '/');
195 snprintf(devname, sizeof(devname), "/tmp/%s", p + 1);
197 ret = mknod(devname, 0666 | S_IFCHR, MKDEV(81, entity->info.v4l.minor));
198 strncpy(entity->devname, devname, sizeof(devname) - 1);
203 static int __media_get_media_fd(const char *filename, struct media_device *media)
205 ALOGD("%s: %s", __func__, filename);
207 media->fd = open(filename, O_RDWR, 0);
209 ALOGE("Open sysfs media device failed, media->fd: %d", media->fd);
213 ALOGD("%s: media->fd: %d", __func__, media->fd);
219 static int __media_enum_entities(struct media_device *media)
221 struct media_entity *entity, *temp_entity;
226 temp_entity = entity = (struct media_entity*)calloc(1, sizeof(struct media_entity));
227 for (id = 0, ret = 0; ; id = entity->info.id) {
228 size = (media->entities_count + 1) * sizeof(*media->entities);
229 media->entities = (struct media_entity*)realloc(media->entities, size);
230 if (media->entities == NULL) {
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;