nuttx/drivers/video/v4l2_core.c
shizhenghui 255090d594 Refactor nuttx v4l2
Adjust the v4l2 framework to support both capture and v4l2m2m,
and can easily add other v4l2 features.

Signed-off-by: shizhenghui <shizhenghui@xiaomi.com>
2024-03-13 09:51:08 +08:00

534 lines
13 KiB
C

/****************************************************************************
* drivers/video/v4l2_core.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <debug.h>
#include <errno.h>
#include <nuttx/kmalloc.h>
#include <nuttx/video/video.h>
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Character driver methods. */
static int v4l2_open(FAR struct file *filep);
static int v4l2_close(FAR struct file *filep);
static ssize_t v4l2_read(FAR struct file *filep,
FAR char *buffer, size_t buflen);
static ssize_t v4l2_write(FAR struct file *filep,
FAR const char *buffer, size_t buflen);
static int v4l2_ioctl(FAR struct file *filep,
int cmd, unsigned long arg);
static int v4l2_mmap(FAR struct file *filep,
FAR struct mm_map_entry_s *map);
static int v4l2_poll(FAR struct file *filep,
FAR struct pollfd *fds, bool setup);
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
static int v4l2_unlink(FAR struct inode *inode);
#endif
/****************************************************************************
* Public Data
****************************************************************************/
static const struct file_operations g_v4l2_fops =
{
v4l2_open, /* open */
v4l2_close, /* close */
v4l2_read, /* read */
v4l2_write, /* write */
NULL, /* seek */
v4l2_ioctl, /* ioctl */
v4l2_mmap, /* mmap */
NULL, /* truncate */
v4l2_poll, /* poll */
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
v4l2_unlink, /* unlink */
#endif
};
/****************************************************************************
* Private Functions
****************************************************************************/
static int v4l2_open(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct v4l2_s *v4l2 = inode->i_private;
if (v4l2->fops->open != NULL)
{
return v4l2->fops->open(filep);
}
return -ENOTSUP;
}
static int v4l2_close(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct v4l2_s *v4l2 = inode->i_private;
if (v4l2->fops->close != NULL)
{
return v4l2->fops->close(filep);
}
return -ENOTSUP;
}
static ssize_t v4l2_read(FAR struct file *filep,
FAR char *buffer, size_t buflen)
{
FAR struct inode *inode = filep->f_inode;
FAR struct v4l2_s *v4l2 = inode->i_private;
if (v4l2->fops->read != NULL)
{
return v4l2->fops->read(filep, buffer, buflen);
}
return -ENOTSUP;
}
static ssize_t v4l2_write(FAR struct file *filep,
FAR const char *buffer, size_t buflen)
{
FAR struct inode *inode = filep->f_inode;
FAR struct v4l2_s *v4l2 = inode->i_private;
if (v4l2->fops->write != NULL)
{
return v4l2->fops->write(filep, buffer, buflen);
}
return -ENOTSUP;
}
static int v4l2_ioctl(FAR struct file *filep,
int cmd, unsigned long arg)
{
FAR struct inode *inode = filep->f_inode;
FAR struct v4l2_s *v4l2 = inode->i_private;
switch (cmd)
{
case VIDIOC_QUERYCAP:
if (v4l2->vops->querycap == NULL)
{
break;
}
return v4l2->vops->querycap(v4l2,
(FAR struct v4l2_capability *)arg);
case VIDIOC_G_INPUT:
if (v4l2->vops->g_input == NULL)
{
break;
}
return v4l2->vops->g_input((FAR int *)arg);
case VIDIOC_ENUMINPUT:
if (v4l2->vops->enum_input == NULL)
{
break;
}
return v4l2->vops->enum_input(v4l2,
(FAR struct v4l2_input *)arg);
case VIDIOC_REQBUFS:
if (v4l2->vops->reqbufs == NULL)
{
break;
}
return v4l2->vops->reqbufs(v4l2,
(FAR struct v4l2_requestbuffers *)arg);
case VIDIOC_QUERYBUF:
if (v4l2->vops->querybuf == NULL)
{
break;
}
return v4l2->vops->querybuf(v4l2,
(FAR struct v4l2_buffer *)arg);
case VIDIOC_QBUF:
if (v4l2->vops->qbuf == NULL)
{
break;
}
return v4l2->vops->qbuf(v4l2,
(FAR struct v4l2_buffer *)arg);
break;
case VIDIOC_DQBUF:
if (v4l2->vops->dqbuf == NULL)
{
break;
}
return v4l2->vops->dqbuf(v4l2,
(FAR struct v4l2_buffer *)arg,
filep->f_oflags);
case VIDIOC_CANCEL_DQBUF:
if (v4l2->vops->cancel_dqbuf == NULL)
{
break;
}
return v4l2->vops->cancel_dqbuf(v4l2,
(FAR enum v4l2_buf_type)arg);
case VIDIOC_STREAMON:
if (v4l2->vops->streamon == NULL)
{
break;
}
return v4l2->vops->streamon(v4l2,
(FAR enum v4l2_buf_type *)arg);
case VIDIOC_STREAMOFF:
if (v4l2->vops->streamoff == NULL)
{
break;
}
return v4l2->vops->streamoff(v4l2,
(FAR enum v4l2_buf_type *)arg);
case VIDIOC_DO_HALFPUSH:
if (v4l2->vops->do_halfpush == NULL)
{
break;
}
return v4l2->vops->do_halfpush(v4l2, arg);
case VIDIOC_TAKEPICT_START:
if (v4l2->vops->takepict_start == NULL)
{
break;
}
return v4l2->vops->takepict_start(v4l2, (int32_t)arg);
case VIDIOC_TAKEPICT_STOP:
if (v4l2->vops->takepict_stop == NULL)
{
break;
}
return v4l2->vops->takepict_stop(v4l2, arg);
case VIDIOC_S_SELECTION:
if (v4l2->vops->s_selection == NULL)
{
break;
}
return v4l2->vops->s_selection(v4l2,
(FAR struct v4l2_selection *)arg);
case VIDIOC_G_SELECTION:
if (v4l2->vops->g_selection == NULL)
{
break;
}
return v4l2->vops->g_selection(v4l2,
(FAR struct v4l2_selection *)arg);
case VIDIOC_TRY_FMT:
if (v4l2->vops->try_fmt == NULL)
{
break;
}
return v4l2->vops->try_fmt(v4l2,
(FAR struct v4l2_format *)arg);
case VIDIOC_G_FMT:
if (v4l2->vops->g_fmt == NULL)
{
break;
}
return v4l2->vops->g_fmt(v4l2,
(FAR struct v4l2_format *)arg);
case VIDIOC_S_FMT:
if (v4l2->vops->s_fmt == NULL)
{
break;
}
return v4l2->vops->s_fmt(v4l2,
(FAR struct v4l2_format *)arg);
case VIDIOC_S_PARM:
if (v4l2->vops->s_parm == NULL)
{
break;
}
return v4l2->vops->s_parm(v4l2,
(FAR struct v4l2_streamparm *)arg);
case VIDIOC_G_PARM:
if (v4l2->vops->g_parm == NULL)
{
break;
}
return v4l2->vops->g_parm(v4l2,
(FAR struct v4l2_streamparm *)arg);
case VIDIOC_QUERYCTRL:
if (v4l2->vops->queryctrl == NULL)
{
break;
}
return v4l2->vops->queryctrl(v4l2,
(FAR struct v4l2_queryctrl *)arg);
case VIDIOC_QUERY_EXT_CTRL:
if (v4l2->vops->query_ext_ctrl == NULL)
{
break;
}
return v4l2->vops->query_ext_ctrl(v4l2,
(FAR struct v4l2_query_ext_ctrl *)arg);
case VIDIOC_QUERYMENU:
if (v4l2->vops->querymenu == NULL)
{
break;
}
return v4l2->vops->querymenu(v4l2,
(FAR struct v4l2_querymenu *)arg);
case VIDIOC_G_CTRL:
if (v4l2->vops->g_ctrl == NULL)
{
break;
}
return v4l2->vops->g_ctrl(v4l2,
(FAR struct v4l2_control *)arg);
case VIDIOC_S_CTRL:
if (v4l2->vops->s_ctrl == NULL)
{
break;
}
return v4l2->vops->s_ctrl(v4l2,
(FAR struct v4l2_control *)arg);
case VIDIOC_G_EXT_CTRLS:
if (v4l2->vops->g_ext_ctrls == NULL)
{
break;
}
return v4l2->vops->g_ext_ctrls(v4l2,
(FAR struct v4l2_ext_controls *)arg);
case VIDIOC_S_EXT_CTRLS:
if (v4l2->vops->s_ext_ctrls == NULL)
{
break;
}
return v4l2->vops->s_ext_ctrls(v4l2,
(FAR struct v4l2_ext_controls *)arg);
case VIDIOC_G_STD:
break;
case VIDIOC_S_STD:
break;
case V4SIOC_QUERY_EXT_CTRL_SCENE:
if (v4l2->vops->query_ext_ctrl_scene == NULL)
{
break;
}
return v4l2->vops->query_ext_ctrl_scene(v4l2,
(FAR struct v4s_query_ext_ctrl_scene *)arg);
case V4SIOC_QUERYMENU_SCENE:
if (v4l2->vops->querymenu_scene == NULL)
{
break;
}
return v4l2->vops->querymenu_scene(v4l2,
(FAR struct v4s_querymenu_scene *)arg);
case V4SIOC_G_EXT_CTRLS_SCENE:
if (v4l2->vops->g_ext_ctrls_scene == NULL)
{
break;
}
return v4l2->vops->g_ext_ctrls_scene(v4l2,
(FAR struct v4s_ext_controls_scene *)arg);
case V4SIOC_S_EXT_CTRLS_SCENE:
if (v4l2->vops->s_ext_ctrls_scene == NULL)
{
break;
}
return v4l2->vops->s_ext_ctrls_scene(v4l2,
(FAR struct v4s_ext_controls_scene *)arg);
case VIDIOC_ENUM_FMT:
if (v4l2->vops->enum_fmt == NULL)
{
break;
}
return v4l2->vops->enum_fmt(v4l2,
(FAR struct v4l2_fmtdesc *)arg);
break;
case VIDIOC_ENUM_FRAMEINTERVALS:
if (v4l2->vops->enum_frminterval == NULL)
{
break;
}
return v4l2->vops->enum_frminterval(v4l2,
(FAR struct v4l2_frmivalenum *)arg);
break;
case VIDIOC_ENUM_FRAMESIZES:
if (v4l2->vops->enum_frmsize == NULL)
{
break;
}
return v4l2->vops->enum_frmsize(v4l2,
(FAR struct v4l2_frmsizeenum *)arg);
break;
default:
verr("Unrecognized cmd: %d\n", cmd);
break;
}
return -ENOTTY;
}
static int v4l2_mmap(FAR struct file *filep,
FAR struct mm_map_entry_s *map)
{
FAR struct inode *inode = filep->f_inode;
FAR struct v4l2_s *v4l2 = inode->i_private;
if (v4l2->fops->mmap != NULL)
{
return v4l2->fops->mmap(filep, map);
}
return -ENOTSUP;
}
static int v4l2_poll(FAR struct file *filep,
FAR struct pollfd *fds, bool setup)
{
FAR struct inode *inode = filep->f_inode;
FAR struct v4l2_s *v4l2 = inode->i_private;
if (v4l2->fops->poll != NULL)
{
return v4l2->fops->poll(filep, fds, setup);
}
return -ENOTSUP;
}
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
static int v4l2_unlink(FAR struct inode *inode)
{
FAR struct v4l2_s *v4l2 = inode->i_private;
if (v4l2->fops->unlink != NULL)
{
return v4l2->fops->unlink(inode);
}
return -ENOTSUP;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
int video_register(FAR const char *devpath, FAR struct v4l2_s *v4l2)
{
int ret;
/* Input devpath Error Check */
if (devpath[0] != '/')
{
return -EINVAL;
}
/* Register the character driver */
ret = register_driver(devpath, &g_v4l2_fops, 0666, v4l2);
if (ret < 0)
{
verr("Failed to register driver: %d\n", ret);
return ret;
}
return OK;
}
int video_unregister(FAR const char *devpath)
{
return unregister_driver(devpath);
}