设为首页 加入收藏

TOP

V4L2 driver -整体架构(一)
2019-08-27 07:39:12 】 浏览:82
Tags:V4L2 driver 整体 架构

我的uvc开源地址:gitee-uvc

  • 字符设备驱动程序核心:V4L2本身就是一个字符设备,具有字符设备所有的特性,暴露接口给用户空间。
  • V4L2 驱动核心:主要是构建一个内核中标准视频设备驱动的框架,为视频操作提供统一的接口函数。
  • 平台V4L2设备驱动:在V4L2框架下,根据平台自身的特性实现与平台相关的V4L2驱动部分,包括注册video_device和v4l2_dev。
  • 具体的sensor驱动:主要上电、提供工作时钟、视频图像裁剪、流IO开启等,实现各种设备控制方法供上层调用并注册v4l2_subdev。

1 从字符设备开始:

熟悉v4l2用户空间编程的都知道, v4l2编程主要是调用一系列的ioctl函数去对v4l2设备进行打开, 关闭, 查询, 设置等操作. v4l2设备是一个字符设备, 而且其驱动的主要工作就是实现各种各样的ioctl.

v4l2的整体框架如下图所示:

V4L2 :video for linux version 2 ,是 linux 里一套标准的视频驱动。本文来分析一下它的核心框架。

在v4l2的核心中对这个file_operations的实现如下:

static const struct file_operations v4l2_fops = {
    .owner = THIS_MODULE,
    .read = v4l2_read,
    .write = v4l2_write,
    .open = v4l2_open,
    .get_unmapped_area = v4l2_get_unmapped_area,
    .mmap = v4l2_mmap,
    .unlocked_ioctl = v4l2_ioctl,
#ifdef CONFIG_COMPAT
    .compat_ioctl = v4l2_compat_ioctl32,
#endif
    .release = v4l2_release,
    .poll = v4l2_poll,
    .llseek = no_llseek,
};

这个v4l2_fops函数最终绑定在一个cdev上, 并注册到系统中。

v4l2_open为例(代码在kernel\drivers\media\v4l2-core中):

/* Override for the open function */
static int v4l2_open(struct inode *inode, struct file *filp)
{
    struct video_device *vdev;
    int ret = 0;

    /* Check if the video device is available */
    mutex_lock(&videodev_lock);
    vdev = video_devdata(filp);
    /* return ENODEV if the video device has already been removed. */
    if (vdev == NULL || !video_is_registered(vdev)) {
        mutex_unlock(&videodev_lock);
        return -ENODEV;
    }
    /* and increase the device refcount */
    video_get(vdev);
    mutex_unlock(&videodev_lock);
    if (vdev->fops->open) {
        if (video_is_registered(vdev))
            //这里就是调用了file_operations的open函数
            ret = vdev->fops->open(filp);
        else
            ret = -ENODEV;
    }

    if (vdev->debug)
        printk(KERN_DEBUG "%s: open (%d)\n",
            video_device_node_name(vdev), ret);
    /* decrease the refcount in case of an error */
    if (ret)
        video_put(vdev);
    return ret;
}

2. video_device结构体:

video_device结构体用于在/dev目录下生成设备节点文件,把操作设备的接口暴露给用户空间。

我们是使用video_device来操作的,看看video_device这个结构体:

struct video_device
{
#if defined(CONFIG_MEDIA_CONTROLLER)
    struct media_entity entity;
#endif
    /* device ops */
    const struct v4l2_file_operations *fops;

    /* sysfs */
    struct device dev;      /* v4l device */
    struct cdev *cdev;      /* character device */

    /* Set either parent or v4l2_dev if your driver uses v4l2_device */
    struct device *parent;      /* device parent */
    struct v4l2_device *v4l2_dev;   /* v4l2_device parent */

    /* Control handler associated with this device node. May be NULL. */
    struct v4l2_ctrl_handler *ctrl_handler;

    /* vb2_queue associated with this device node. May be NULL. */
    struct vb2_queue *queue;

    /* Priority state. If NULL, then v4l2_dev->prio will be used. */
    struct v4l2_prio_state *prio;

    /* device info */
    char name[32];
    int vfl_type;   /* device type */
    int vfl_dir;    /* receiver, transmitter or m2m */
    /* 'minor' is set to -1 if the registration failed */
    int minor;
    u16 num;
    /* use bitops to set/clear/test flags */
    unsigned long flags;
    /* attribute to differentiate multiple indices on one physical device */
    int index;

    /* V4L2 file handles */
    spinlock_t      fh_lock; /* Lock for all v4l2_fhs */
    struct list_head    fh_list; /* List of struct v4l2_fh */

    int debug;          /* Activates debug level*/

    /* Video standard vars */
    v4l2_std_id tvnorms;        /* Supported tv norms */
    v4l2_std_id current_norm;   /* Current tvnorm */

    /* callbacks */
    void (*release)(struct video_device *vdev);

    /
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇嵌入式程序员应知道的0x10个基本.. 下一篇物联网设备网络接入方式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目