Chinaunix首页 | 论坛 | 博客
  • 博客访问: 651478
  • 博文数量: 329
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 693
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-05 23:37
个人简介

Do not panic!

文章存档

2021年(1)

2018年(3)

2017年(7)

2016年(98)

2015年(220)

我的朋友

分类: Android平台

2016-10-09 13:46:35

Video for Linux Two
V4L2的是V4L的第二个版本。原来的V4L被引入到Linux内核2.1.x的开发周期后期。
Video4Linux2修正了一些设计缺陷,并开始出现在2.5.X内核。
Video4Linux2驱动程序包括Video4Linux1应用的兼容模式,但实际上,支持是不完整的,并建议V4L2的设备使用V4L2的模式。

要想了解 V4l2 有几个重要的文档是必须要读的,
Documentation/video4linux目录下的V4L2-framework.txt和videobuf、V4L2的官方API文档V4L2 API Specification ,
drivers/media/video目录下的vivi.c(虚拟视频驱动程序 -此代码模拟一个真正的视频设备V4L2 API)。

V4l2可以支持多种设备,它可以有以下几种接口:
1. 视频采集接口(video capture interface):这种应用的设备可以是高频头或者摄像头.V4L2的最初设计就是应用于这种功能的.
2. 视频输出接口(video output interface):可以驱动计算机的外围视频图像设备--像可以输出电视信号格式的设备.
3. 直接传输视频接口(video overlay interface):它的主要工作是把从视频采集设备采集过来的信号直接输出到输出设备之上,而不用经过系统的CPU.
4. 视频间隔消隐信号接口(VBI interface):它可以使应用可以访问传输消隐期的视频信号.
5. 收音机接口(radio interface):可用来处理从AM或FM高频头设备接收来的音频流.

V4L2 驱动核心
V4L2 的驱动源码在 drivers/media/video目录下,主要核心代码有:
v4l2-dev.c                    //linux版本2视频捕捉接口,主要结构体 video_device 的注册
v4l2-common.c            //在Linux操作系统体系采用低级别的操作一套设备structures/vectors的通用视频设备接口。
                                 //此文件将替换videodev.c的文件配备常规的内核分配。
v4l2-device.c            //V4L2的设备支持。注册v4l2_device
v4l22-ioctl.c             //处理V4L2的ioctl命令的一个通用的框架。
v4l2-subdev.c          //v4l2子设备
v4l2-mem2mem.c  //内存到内存为Linux和videobuf视频设备的框架。设备的辅助函数,使用其源和目的地videobuf缓冲区。
头文件linux/videodev2.h、media/v4l2-common.h、media/v4l2-device.h、media/v4l2-ioctl.h、media/v4l2-dev.h、media/v4l2-ioctl.h等。

V4l2相关结构体
1.V4l2_device
  1. struct v4l2_device {
  2.     /* dev->driver_data points to this struct.
  3.      Note: dev might be NULL if there is no parent device
  4.      as is the case with e.g. ISA devices. */
  5.     struct device *dev;
  6.     /* used to keep track of the registered subdevs */
  7.     struct list_head subdevs;
  8.     /* lock this struct; can be used by the driver as well if this
  9.      struct is embedded into a larger struct. */
  10.     spinlock_t lock;
  11.     /* unique device name, by default the driver name + bus ID */
  12.     char name[V4L2_DEVICE_NAME_SIZE];
  13.     /* notify callback called by some sub-devices. */
  14.     void (*notify)(struct v4l2_subdev *sd,
  15.             unsigned int notification, void *arg);
  16. };
v4l2_device注册和注销

  1. int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)
  2. {
  3.     if (v4l2_dev == NULL)
  4.         return -EINVAL;

  5.     INIT_LIST_HEAD(&v4l2_dev->subdevs);
  6.     spin_lock_init(&v4l2_dev->lock);
  7.     v4l2_dev->dev = dev;
  8.     if (dev == NULL) {
  9.         /* If dev == NULL, then name must be filled in by the caller */
  10.         WARN_ON(!v4l2_dev->name[0]);
  11.         return 0;
  12.     }

  13.     /* Set name to driver name + device name if it is empty. */
  14.     if (!v4l2_dev->name[0])
  15.         snprintf(v4l2_dev->name, sizeof(v4l2_dev->name), "%s %s",
  16.             dev->driver->name, dev_name(dev));
  17.     if (dev_get_drvdata(dev))
  18.         v4l2_warn(v4l2_dev, "Non-NULL drvdata on register\n");
  19.     dev_set_drvdata(dev, v4l2_dev);
  20.     return 0;
  21. }
  22. EXPORT_SYMBOL_GPL(v4l2_device_register);
  1. void v4l2_device_unregister(struct v4l2_device *v4l2_dev)
  2. {
  3.     struct v4l2_subdev *sd, *next;

  4.     if (v4l2_dev == NULL)
  5.         return;
  6.     v4l2_device_disconnect(v4l2_dev);

  7.     /* Unregister subdevs */
  8.     list_for_each_entry_safe(sd, next, &v4l2_dev->subdevs, list) {
  9.         v4l2_device_unregister_subdev(sd);
  10. #if defined(CONFIG_I2C) || (defined(CONFIG_I2C_MODULE) && defined(MODULE))
  11.         if (sd->flags & V4L2_SUBDEV_FL_IS_I2C) {
  12.             struct i2c_client *client = v4l2_get_subdevdata(sd);

  13.             /* We need to unregister the i2c client explicitly.
  14.              We cannot rely on i2c_del_adapter to always
  15.              unregister clients for us, since if the i2c bus
  16.              is a platform bus, then it is never deleted. */
  17.             if (client)
  18.                 i2c_unregister_device(client);
  19.         }
  20. #endif
  21.     }
  22. }
  23. EXPORT_SYMBOL_GPL(v4l2_device_unregister);
2.video_device
  1. /*
  2.  * Newer version of video_device, handled by videodev2.c
  3.  *     This version moves redundant code from video device code to
  4.  *    the common handler
  5.  */

  6. struct video_device
  7. {
  8.     /* device ops */
  9.     const struct v4l2_file_operations *fops;

  10.     /* sysfs */
  11.     struct device dev;        /* v4l device */
  12.     struct cdev *cdev;        /* character device */

  13.     /* Set either parent or v4l2_dev if your driver uses v4l2_device */
  14.     struct device *parent;        /* device parent */
  15.     struct v4l2_device *v4l2_dev;    /* v4l2_device parent */

  16.     /* device info */
  17.     char name[32];
  18.     int vfl_type;
  19.     /* 'minor' is set to -1 if the registration failed */
  20.     int minor;
  21.     u16 num;
  22.     /* use bitops to set/clear/test flags */
  23.     unsigned long flags;
  24.     /* attribute to differentiate multiple indices on one physical device */
  25.     int index;

  26.     int debug;            /* Activates debug level*/

  27.     /* Video standard vars */
  28.     v4l2_std_id tvnorms;        /* Supported tv norms */
  29.     v4l2_std_id current_norm;    /* Current tvnorm */

  30.     /* callbacks */
  31.     void (*release)(struct video_device *vdev);

  32.     /* ioctl callbacks */
  33.     const struct v4l2_ioctl_ops *ioctl_ops;
  34. };
动态分配:
         struct video_device *vdev = video_device_alloc();

 结构体配置:
        fops:设置这个v4l2_file_operations结构,file_operations的一个子集。v4l2_dev: 设置这个v4l2_device父设备
        name:
        ioctl_ops:使用v4l2_ioctl_ops简化的IOCTL,然后设置v4l2_ioctl_ops结构。
        lock:如果你想要做的全部驱动程序锁定就保留为NULL。否则你给它一个指针指向一个mutex_lock结构体和任何v4l2_file_operations被调用之前核心应该释放释放锁。
        parent:一个硬件设备有多个PCI设备,都共享相同v4l2_device核心时,设置注册使用NULL v4l2_device作为父设备结构。
        flags:可选的。设置到V4L2_FL_USE_FH_PRIO如你想让框架处理VIDIOC_G/ S_PRIORITY的ioctl。这就需要您使用结构v4l2_fh。这个标志最终会消失,一旦所有的驱动程序使用的核心优先处理。但现在它必须明确设定。

如果使用v4l2_ioctl_ops,那么你应该设置。unlocked_ioctlvideo_ioctl2在v4l2_file_operations结构。

注册/注销 video_device:

  1. /**
  2.  *    video_register_device - register video4linux devices
  3.  *    @vdev: video device structure we want to register
  4.  *    @type: type of device to register
  5.  *    @nr: which device node number (0 == /dev/video0, 1 == /dev/video1, ...
  6.  * -1 == first free)
  7.  *    @warn_if_nr_in_use: warn if the desired device node number
  8.  *     was already in use and another number was chosen instead.
  9.  *
  10.  *    The registration code assigns minor numbers and device node numbers
  11.  *    based on the requested type and registers the new device node with
  12.  *    the kernel.
  13.  *    An error is returned if no free minor or device node number could be
  14.  *    found, or if the registration of the device node failed.
  15.  *
  16.  *    Zero is returned on success.
  17.  *
  18.  *    Valid types are
  19.  *
  20.  *    %VFL_TYPE_GRABBER - A frame grabber
  21.  *
  22.  *    %VFL_TYPE_VTX - A teletext device
  23.  *
  24.  *    %VFL_TYPE_VBI - Vertical blank data (undecoded)
  25.  *
  26.  *    %VFL_TYPE_RADIO - A radio card
  27.  */
  28. static int __video_register_device(struct video_device *vdev, int type, int nr,
  29.         int warn_if_nr_in_use)
  30. {
  31.     int i = 0;
  32.     int ret;
  33.     int minor_offset = 0;
  34.     int minor_cnt = VIDEO_NUM_DEVICES;
  35.     const char *name_base;
  36.     void *priv = video_get_drvdata(vdev);

  37.     /* A minor value of -1 marks this video device as never
  38.      having been registered */
  39.     vdev->minor = -1;

  40.     /* the release callback MUST be present */
  41.     WARN_ON(!vdev->release);
  42.     if (!vdev->release)
  43.         return -EINVAL;

  44.     /* Part 1: check device type */
  45.     switch (type) {
  46.     case VFL_TYPE_GRABBER:
  47.         name_base = "video";
  48.         break;
  49.     case VFL_TYPE_VTX:
  50.         name_base = "vtx";
  51.         break;
  52.     case VFL_TYPE_VBI:
  53.         name_base = "vbi";
  54.         break;
  55.     case VFL_TYPE_RADIO:
  56.         name_base = "radio";
  57.         break;
  58.     default:
  59.         printk(KERN_ERR "%s called with unknown type: %d\n",
  60.          __func__, type);
  61.         return -EINVAL;
  62.     }

  63.     vdev->vfl_type = type;
  64.     vdev->cdev = NULL;
  65.     if (vdev->v4l2_dev && vdev->v4l2_dev->dev)
  66.         vdev->parent = vdev->v4l2_dev->dev;

  67.     /* Part 2: find a free minor, device node number and device index. */
  68. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  69.     /* Keep the ranges for the first four types for historical
  70.      * reasons.
  71.      * Newer devices (not yet in place) should use the range
  72.      * of 128-191 and just pick the first free minor there
  73.      * (new style). */
  74.     switch (type) {
  75.     case VFL_TYPE_GRABBER:
  76.         minor_offset = 0;
  77.         minor_cnt = 64;
  78.         break;
  79.     case VFL_TYPE_RADIO:
  80.         minor_offset = 64;
  81.         minor_cnt = 64;
  82.         break;
  83.     case VFL_TYPE_VTX:
  84.         minor_offset = 192;
  85.         minor_cnt = 32;
  86.         break;
  87.     case VFL_TYPE_VBI:
  88.         minor_offset = 224;
  89.         minor_cnt = 32;
  90.         break;
  91.     default:
  92.         minor_offset = 128;
  93.         minor_cnt = 64;
  94.         break;
  95.     }
  96. #endif

  97.     /* Pick a device node number */
  98.     mutex_lock(&videodev_lock);
  99.     nr = devnode_find(vdev, nr == -1 ? 0 : nr, minor_cnt);
  100.     if (nr == minor_cnt)
  101.         nr = devnode_find(vdev, 0, minor_cnt);
  102.     if (nr == minor_cnt) {
  103.         printk(KERN_ERR "could not get a free device node number\n");
  104.         mutex_unlock(&videodev_lock);
  105.         return -ENFILE;
  106.     }
  107. #ifdef CONFIG_VIDEO_FIXED_MINOR_RANGES
  108.     /* 1-on-1 mapping of device node number to minor number */
  109.     i = nr;
  110. #else
  111.     /* The device node number and minor numbers are independent, so
  112.      we just find the first free minor number. */
  113.     for (i = 0; i < VIDEO_NUM_DEVICES; i++)
  114.         if (video_device[i] == NULL)
  115.             break;
  116.     if (i == VIDEO_NUM_DEVICES) {
  117.         mutex_unlock(&videodev_lock);
  118.         printk(KERN_ERR "could not get a free minor\n");
  119.         return -ENFILE;
  120.     }
  121. #endif
  122.     vdev->minor = i + minor_offset;
  123.     vdev->num = nr;
  124.     devnode_set(vdev);

  125.     /* Should not happen since we thought this minor was free */
  126.     WARN_ON(video_device[vdev->minor] != NULL);
  127.     vdev->index = get_index(vdev);
  128.     mutex_unlock(&videodev_lock);

  129.     /* Part 3: Initialize the character device */
  130.     vdev->cdev = cdev_alloc();
  131.     if (vdev->cdev == NULL) {
  132.         ret = -ENOMEM;
  133.         goto cleanup;
  134.     }
  135.     if (vdev->fops->unlocked_ioctl)
  136.         vdev->cdev->ops = &v4l2_unlocked_fops;
  137.     else
  138.         vdev->cdev->ops = &v4l2_fops;
  139.     vdev->cdev->owner = vdev->fops->owner;
  140.     ret = cdev_add(vdev->cdev, MKDEV(VIDEO_MAJOR, vdev->minor), 1);
  141.     if (ret < 0) {
  142.         printk(KERN_ERR "%s: cdev_add failed\n", __func__);
  143.         kfree(vdev->cdev);
  144.         vdev->cdev = NULL;
  145.         goto cleanup;
  146.     }

  147.     /* Part 4: register the device with sysfs */
  148.     memset(&vdev->dev, 0, sizeof(vdev->dev));
  149.     /* The memset above cleared the device's drvdata, so
  150.      put back the copy we made earlier. */
  151.     video_set_drvdata(vdev, priv);
  152.     vdev->dev.class = &video_class;
  153.     vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
  154.     if (vdev->parent)
  155.         vdev->dev.parent = vdev->parent;
  156.     dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
  157.     ret = device_register(&vdev->dev);
  158.     if (ret < 0) {
  159.         printk(KERN_ERR "%s: device_register failed\n", __func__);
  160.         goto cleanup;
  161.     }
  162.     /* Register the release callback that will be called when the last
  163.      reference to the device goes away. */
  164.     vdev->dev.release = v4l2_device_release;

  165.     if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
  166.         printk(KERN_WARNING "%s: requested %s%d, got %s%d\n",
  167.                 __func__, name_base, nr, name_base, vdev->num);

  168.     /* Part 5: Activate this minor. The char device can now be used. */
  169.     mutex_lock(&videodev_lock);
  170.     video_device[vdev->minor] = vdev;
  171.     mutex_unlock(&videodev_lock);
  172.     return 0;

  173. cleanup:
  174.     mutex_lock(&videodev_lock);
  175.     if (vdev->cdev)
  176.         cdev_del(vdev->cdev);
  177.     devnode_clear(vdev);
  178.     mutex_unlock(&videodev_lock);
  179.     /* Mark this video device as never having been registered. */
  180.     vdev->minor = -1;
  181.     return ret;
  182. }

  183. int video_register_device(struct video_device *vdev, int type, int nr)
  184. {
  185.     return __video_register_device(vdev, type, nr, 1);
  186. }
  187. EXPORT_SYMBOL(video_register_device);

  188. /**
  189.  *    video_unregister_device - unregister a video4linux device
  190.  *    @vdev: the device to unregister
  191.  *
  192.  *    This unregisters the passed device. Future open calls will
  193.  *    be met with errors.
  194.  */
  195. void video_unregister_device(struct video_device *vdev)
  196. {
  197.     /* Check if vdev was ever registered at all */
  198.     if (!vdev || vdev->minor < 0)
  199.         return;

  200.     mutex_lock(&videodev_lock);
  201.     set_bit(V4L2_FL_UNREGISTERED, &vdev->flags);
  202.     mutex_unlock(&videodev_lock);
  203.     device_unregister(&vdev->dev);
  204. }
  205. EXPORT_SYMBOL(video_unregister_device);
3.v4l2_subdev
每个子设备驱动程序必须有一个v4l2_subdev结构。这个结构可以独立简单的设备或者如果需要存储更多的状态信息它可能被嵌入在一个更大的结构。由于子设备可以做很多不同的东西,你不想结束一个巨大的OPS结构其中只有少数的OPS通常执行,函数指针进行排序按类别,每个类别都有其自己的OPS结构。顶层OPS结构包含的类别OPS结构,这可能是NULL如果在subdev驱动程序不支持任何从该类别指针。
  1. /* Each instance of a subdev driver should create this struct, either
  2.    stand-alone or embedded in a larger struct.
  3.  */
  4. struct v4l2_subdev {
  5.     struct list_head list;
  6.     struct module *owner;
  7.     u32 flags;
  8.     struct v4l2_device *v4l2_dev;
  9.     const struct v4l2_subdev_ops *ops;
  10.     /* name must be unique */
  11.     char name[V4L2_SUBDEV_NAME_SIZE];
  12.     /* can be used to group similar subdevs, value is driver-specific */
  13.     u32 grp_id;
  14.     /* pointer to private data */
  15.     void *priv;
  16. };
4.v4l2_buffer
  1. struct v4l2_buffer {
  2.     __u32            index;
  3.     enum v4l2_buf_type type;
  4.     __u32            bytesused;
  5.     __u32            flags;
  6.     enum v4l2_field        field;
  7.     struct timeval        timestamp;
  8.     struct v4l2_timecode    timecode;
  9.     __u32            sequence;

  10.     /* memory location */
  11.     enum v4l2_memory memory;
  12.     union {
  13.         __u32 offset;
  14.         unsigned long userptr;
  15.     } m;
  16.     __u32            length;
  17.     __u32            input;
  18.     __u32            reserved;
  19. };
V4L2核心API提供了一套标准方法的用于处理视频缓冲器(称为“videobuf”)。这些方法允许驱动程序以一致的方式来实现read(),mmap()和overlay()。目前使用的设备上的视频缓冲器,支持scatter/gather方法(videobuf-dma-SG),线性存取的DMA的(videobuf-DMA-contig),vmalloc分配的缓冲区,主要用于在USB驱动程序(DMA缓冲区的方法videobuf-vmalloc)。

videobuf层的功能为一种V4L2驱动和用户空间之间的粘合层。它可以处理存储视频帧缓冲区的分配和管理。有一组可用于执行许多标准的POSIX I / O系统调用的功能,包括read(),poll()的,happily,mmap()。另一套功能可以用来实现大部分的V4L2的ioctl()调用相关的流式I/ O的,包括缓冲区分配,排队和dequeueing,流控制。驱动作者使用videobuf规定了一些设计决定,但回收期在驱动器和一个V4L2的用户空间API的贯彻实施在减少代码的形式。

关于videobuf的层的更多信息,请参阅Documentation/video4linux/videobuf

V4l2驱动架构

所有的驱动程序有以下结构:
        1) 每个设备包含设备状态的实例结构。
        2) 子设备的初始化和命令方式(如果有).
        3) 创建V4L2的设备节点 (/dev/videoX, /dev/vbiX and /dev/radioX)和跟踪设备节点的具体数据。
        4)文件句柄特定的结构,包含每个文件句柄数据;
        5) 视频缓冲处理。

驱动源码分析

vivi.c 虚拟视频驱动程序----- 此代码模拟一个真正的视频设备V4L2 API (位于drivers/media/video目录下)

 入口:+int __init vivi_init(void)
                + vivi_create_instance(i) /*创建设备*//**/。
                        + 分配一个vivi_dev的结构体 /*它嵌套这结构体v4l2_device 和video_device*/
                        + v4l2_device_register(NULL, &dev->v4l2_dev);/*注册vivi_dev中的V4l2_device*/
                        + 初始化视频的DMA队列
                        + 初始化锁
                        + video_device_alloc(); 动态分配video_device结构体
                        + 构建一个video_device结构体 vivi_template 并赋给上面分配的video_device
                               static struct video_device vivi_template = {
                                         . name        = "vivi",
                                         .fops           = &vivi_fops,
                                         .ioctl_ops     = &vivi_ioctl_ops,
                                         .minor        = -1,
                                         .release    = video_device_release,

                                         .tvnorms              = V4L2_STD_525_60,
                                         .current_norm         = V4L2_STD_NTSC_M,
                                };
                      + video_set_drvdata(vfd, dev);设置驱动程序专有数据
                      + 所有控件设置为其默认值
                      + list_add_tail(&dev->vivi_devlist, &vivi_devlist);添加到设备列表
         + 构建 v4l2_file_operations 结构体vivi_fops 并实现.open .release .read .poll .mmap函数----- .ioctl 用标准的v4l2控制处理程序
         + 构建 v4l2_ioctl_ops结构体 vivi_ioctl_ops
                            static const struct v4l2_ioctl_ops vivi_ioctl_ops = {
                                       .vidioc_querycap      = vidioc_querycap,
                                       .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
                                       .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
                                       .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
                                       .vidioc_reqbufs       = vidioc_reqbufs,
                                       .vidioc_querybuf      = vidioc_querybuf,
                                       .vidioc_qbuf          = vidioc_qbuf,
                                       .vidioc_dqbuf         = vidioc_dqbuf,
                                       .vidioc_s_std         = vidioc_s_std,
                                       .vidioc_enum_input    = vidioc_enum_input,
                                       .vidioc_g_input       = vidioc_g_input,
                                       .vidioc_s_input       = vidioc_s_input,
                                       .vidioc_queryctrl     = vidioc_queryctrl,
                                       .vidioc_g_ctrl        = vidioc_g_ctrl,
                                       .vidioc_s_ctrl        = vidioc_s_ctrl,
                                       .vidioc_streamon      = vidioc_streamon,
                                       .vidioc_streamoff     = vidioc_streamoff,
                            #ifdef CONFIG_VIDEO_V4L1_COMPAT
                                      .vidiocgmbuf          = vidiocgmbuf,
                          #endif
                      };
          + int vivi_open(struct file *file)
                    + vivi_dev *dev = video_drvdata(file);  访问驱动程序专用数据
                    + 分配+初始化句柄(vivi_fh)数据
                    + 重置帧计数器
                    + videobuf_queue_vmalloc_init(); 初始化视频缓冲队列
                    + 开启一个新线程用于开始和暂停
          + 实现自定义的v4l2_ioctl_ops 函数

这里只是对V4L2的最基本知识
阅读(1015) | 评论(0) | 转发(0) |
0

上一篇:Linux音频设备驱动

下一篇:DBus 介绍

给主人留下些什么吧!~~