blob: ad13d7cc44559e69898e2dc60ef4af0ccce0b947 [file] [log] [blame]
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*!
* \file exynos_subdev.c
* \brief source file for libv4l2
* \author Jinsung Yang (jsgood.yang@samsung.com)
* \author Sangwoo Park (sw5771.park@samsung.com)
* \date 2012/01/17
*
* <b>Revision History: </b>
* - 2012/01/17: Jinsung Yang (jsgood.yang@samsung.com) \n
* Initial version
*
*/
#include <stdio.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include "exynos_v4l2.h"
//#define LOG_NDEBUG 0
#define LOG_TAG "libexynosv4l2-subdev"
#include <utils/Log.h>
#include <string.h>
#define SUBDEV_MAX 191
static int __subdev_open(const char *filename, int oflag, va_list ap)
{
mode_t mode = 0;
int fd;
if (oflag & O_CREAT)
mode = va_arg(ap, int);
fd = open(filename, oflag, mode);
return fd;
}
int exynos_subdev_open(const char *filename, int oflag, ...)
{
va_list ap;
int fd;
va_start(ap, oflag);
fd = __subdev_open(filename, oflag, ap);
va_end(ap);
return fd;
}
int exynos_subdev_get_node_num(const char *devname, int oflag, ...)
{
bool found = false;
int ret = -1;
struct stat s;
FILE *stream_fd;
char filename[64], name[64];
int i = 0;
do {
if (i > (SUBDEV_MAX - 128))
break;
/* video device node */
snprintf(filename, sizeof(filename), "/dev/v4l-subdev%d", i);
/* if the node is video device */
if ((lstat(filename, &s) == 0) && S_ISCHR(s.st_mode) &&
((int)((unsigned short)(s.st_rdev) >> 8) == 81)) {
ALOGD("try node: %s", filename);
/* open sysfs entry */
snprintf(filename, sizeof(filename), "/sys/class/video4linux/v4l-subdev%d/name", i);
if (S_ISLNK(s.st_mode)) {
ALOGE("symbolic link detected");
return -1;
}
stream_fd = fopen(filename, "r");
if (stream_fd == NULL) {
ALOGE("failed to open sysfs entry for subdev");
continue; /* try next */
}
/* read sysfs entry for device name */
char *p = fgets(name, sizeof(name), stream_fd);
fclose(stream_fd);
/* check read size */
if (p == NULL) {
ALOGE("failed to read sysfs entry for subdev");
} else {
/* matched */
if (strncmp(name, devname, strlen(devname)) == 0) {
ALOGI("node found for device %s: /dev/v4l-subdev%d", devname, i);
found = true;
break;
}
}
}
i++;
} while (found == false);
if (found)
ret = i;
else
ALOGE("no subdev device found");
return ret;
}
int exynos_subdev_open_devname(const char *devname, int oflag, ...)
{
bool found = false;
int fd = -1;
struct stat s;
va_list ap;
FILE *stream_fd;
char filename[64], name[64];
int i = 0;
do {
if (i > (SUBDEV_MAX - 128))
break;
/* video device node */
snprintf(filename, sizeof(filename), "/dev/v4l-subdev%d", i);
/* if the node is video device */
if ((lstat(filename, &s) == 0) && S_ISCHR(s.st_mode) &&
((int)((unsigned short)(s.st_rdev) >> 8) == 81)) {
ALOGD("try node: %s", filename);
/* open sysfs entry */
snprintf(filename, sizeof(filename), "/sys/class/video4linux/v4l-subdev%d/name", i);
if (S_ISLNK(s.st_mode)) {
ALOGE("symbolic link detected");
return -1;
}
stream_fd = fopen(filename, "r");
if (stream_fd == NULL) {
ALOGE("failed to open sysfs entry for subdev");
continue; /* try next */
}
/* read sysfs entry for device name */
char *p = fgets(name, sizeof(name), stream_fd);
fclose(stream_fd);
/* check read size */
if (p == NULL) {
ALOGE("failed to read sysfs entry for subdev");
} else {
/* matched */
if (strncmp(name, devname, strlen(devname)) == 0) {
ALOGI("node found for device %s: /dev/v4l-subdev%d", devname, i);
found = true;
break;
}
}
}
i++;
} while (found == false);
if (found) {
snprintf(filename, sizeof(filename), "/dev/v4l-subdev%d", i);
va_start(ap, oflag);
fd = __subdev_open(filename, oflag, ap);
va_end(ap);
if (fd > 0)
ALOGI("open subdev device %s", filename);
else
ALOGE("failed to open subdev device %s", filename);
} else {
ALOGE("no subdev device found");
}
return fd;
}
int exynos_subdev_close(int fd)
{
int ret = -1;
if (fd < 0)
ALOGE("%s: invalid fd: %d", __func__, fd);
else
ret = close(fd);
return ret;
}
/**
* @brief enum frame size on a pad.
* @return 0 on success, or a negative error code on failure.
*/
int exynos_subdev_enum_frame_size(int fd, struct v4l2_subdev_frame_size_enum *frame_size_enum)
{
int ret = -1;
if (fd < 0) {
ALOGE("%s: invalid fd: %d", __func__, fd);
return ret;
}
if (!frame_size_enum) {
ALOGE("%s: frame_size_enum is NULL", __func__);
return ret;
}
ret = ioctl(fd, VIDIOC_SUBDEV_ENUM_FRAME_SIZE, frame_size_enum);
if (ret) {
ALOGE("failed to ioctl: VIDIOC_SUBDEV_ENUM_FRAME_SIZE");
return ret;
}
return ret;
}
/**
* @brief Retrieve the format on a pad.
* @return 0 on success, or a negative error code on failure.
*/
int exynos_subdev_g_fmt(int fd, struct v4l2_subdev_format *fmt)
{
int ret = -1;
if (fd < 0) {
ALOGE("%s: invalid fd: %d", __func__, fd);
return ret;
}
if (!fmt) {
ALOGE("%s: fmt is NULL", __func__);
return ret;
}
ret = ioctl(fd, VIDIOC_SUBDEV_G_FMT, fmt);
if (ret) {
ALOGE("failed to ioctl: VIDIOC_SUBDEV_G_FMT");
return ret;
}
return ret;
}
/**
* @brief Set the format on a pad.
* @return 0 on success, or a negative error code on failure.
*/
int exynos_subdev_s_fmt(int fd, struct v4l2_subdev_format *fmt)
{
int ret = -1;
if (fd < 0) {
ALOGE("%s: invalid fd: %d", __func__, fd);
return ret;
}
if (!fmt) {
ALOGE("%s: fmt is NULL", __func__);
return ret;
}
ret = ioctl(fd, VIDIOC_SUBDEV_S_FMT, fmt);
if (ret) {
ALOGE("failed to ioctl: VIDIOC_SUBDEV_S_FMT");
return ret;
}
return ret;
}
/**
* @brief Retrieve the crop rectangle on a pad.
* @return 0 on success, or a negative error code on failure.
*/
int exynos_subdev_g_crop(int fd, struct v4l2_subdev_crop *crop)
{
int ret = -1;
if (fd < 0) {
ALOGE("%s: invalid fd: %d", __func__, fd);
return ret;
}
if (!crop) {
ALOGE("%s: crop is NULL", __func__);
return ret;
}
ret = ioctl(fd, VIDIOC_SUBDEV_G_CROP, crop);
if (ret) {
ALOGE("failed to ioctl: VIDIOC_SUBDEV_G_CROP");
return ret;
}
return ret;
}
/**
* @brief Set the crop rectangle on a pad.
* @return 0 on success, or a negative error code on failure.
*/
int exynos_subdev_s_crop(int fd, struct v4l2_subdev_crop *crop)
{
int ret = -1;
if (fd < 0) {
ALOGE("%s: invalid fd: %d", __func__, fd);
return ret;
}
if (!crop) {
ALOGE("%s: crop is NULL", __func__);
return ret;
}
ret = ioctl(fd, VIDIOC_SUBDEV_S_CROP, crop);
if (ret) {
ALOGE("failed to ioctl: VIDIOC_SUBDEV_S_CROP");
return ret;
}
return ret;
}
/**
* @brief Retrieve the frame interval on a sub-device.
* @return 0 on success, or a negative error code on failure.
*/
int exynos_subdev_enum_frame_interval(int fd, struct v4l2_subdev_frame_interval_enum *frame_internval_enum)
{
int ret = -1;
if (fd < 0) {
ALOGE("%s: invalid fd: %d", __func__, fd);
return ret;
}
if (!frame_internval_enum) {
ALOGE("%s: frame_internval_enum is NULL", __func__);
return ret;
}
ret = ioctl(fd, VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL, frame_internval_enum);
if (ret) {
ALOGE("failed to ioctl: VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL");
return ret;
}
return ret;
}
/**
* @brief Retrieve the frame interval on a sub-device.
* @return 0 on success, or a negative error code on failure.
*/
int exynos_subdev_g_frame_interval(int fd, struct v4l2_subdev_frame_interval *frame_internval)
{
int ret = -1;
if (fd < 0) {
ALOGE("%s: invalid fd: %d", __func__, fd);
return ret;
}
if (!frame_internval) {
ALOGE("%s: frame_internval is NULL", __func__);
return ret;
}
ret = ioctl(fd, VIDIOC_SUBDEV_G_FRAME_INTERVAL, frame_internval);
if (ret) {
ALOGE("failed to ioctl: VIDIOC_SUBDEV_G_FRAME_INTERVAL");
return ret;
}
return ret;
}
/**
* @brief Set the frame interval on a sub-device.
* @return 0 on success, or a negative error code on failure.
*/
int exynos_subdev_s_frame_interval(int fd, struct v4l2_subdev_frame_interval *frame_internval)
{
int ret = -1;
if (fd < 0) {
ALOGE("%s: invalid fd: %d", __func__, fd);
return ret;
}
if (!frame_internval) {
ALOGE("%s: frame_internval is NULL", __func__);
return ret;
}
ret = ioctl(fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, frame_internval);
if (ret) {
ALOGE("failed to ioctl: VIDIOC_SUBDEV_S_FRAME_INTERVAL");
return ret;
}
return ret;
}
/**
* @brief enum mbus code
* @return 0 on success, or a negative error code on failure.
*/
int exynos_subdev_enum_mbus_code(int fd, struct v4l2_subdev_mbus_code_enum *mbus_code_enum)
{
int ret = -1;
if (fd < 0) {
ALOGE("%s: invalid fd: %d", __func__, fd);
return ret;
}
if (!mbus_code_enum) {
ALOGE("%s: mbus_code_enum is NULL", __func__);
return ret;
}
ret = ioctl(fd, VIDIOC_SUBDEV_ENUM_MBUS_CODE, mbus_code_enum);
if (ret) {
ALOGE("failed to ioctl: VIDIOC_SUBDEV_ENUM_MBUS_CODE");
return ret;
}
return ret;
}