v4l2 编程接口(二) — driver

本文深入探讨了V4L2驱动的复杂性,涉及如何处理多IC设备,以及在/dev目录下创建的多种设备节点。介绍了video_device结构和v4l2_subdev结构在驱动中的作用,强调了v4l2_device作为设备实例的重要性,以及v4l2_subdev如何作为子设备实例进行操作。文章详细阐述了video_device的file_operations接口,包括open、read、poll、mmap和ioctl,并讨论了v4l2_subdev结构及其ops域在子设备控制中的角色。
摘要由CSDN通过智能技术生成

V4L2 驱动随着硬件的变化也越来越复杂,现在大部分设备有里面包含了多个IC, 在/dev目录下不仅要建立 V4L2 的节点,而且还需要建立如:DVB、ALSA、FB、I2C、input等设备节点。事实上 V4L2 驱动需要支持音频/视频的混音/编码/解码等IC所以比其他驱动都要复杂很多,通常这些IC通过 i2c 总线连接到主板,这些设备都统称为sub-devices。在很长的一段时间里 V4L2 被限制只能在 video_device 结构体里面创建,并且用video_buf 控制视频缓存,这意味着所有的驱动创建自己的实例都将连接到自己的sub-devices,这些工作通常很复杂并经常引起错误,许多常见的代码因为缺乏一个框架而无法重构。因此这个框架建立起了基本的机制,所有的驱动都需要和这个框架结合以便共用其中的函数。因此 V4L2  框架作了相应的优化:它有一个 v4l2_device 结构作为设备实例,一个v4l2_subdev结构作为子设备实例,video_device 结构包含了v4l2_device 节点,以后将会有一个 v4l2_fh 的结构作为与文件句柄的实例。每个设备都采用 v4l2_device 结构来表示。非常简单的设备都可以申请这个结构,但通常会将这个结构嵌入一个更大的结构中。

1、 video_device

在 v4l2 中用 struct video_device 代表一个视频设备,该结构说明如下:

struct video_device
{
	/* 设备操作合集 */
	const struct v4l2_file_operations *fops;

	/* sysfs节点 */
	struct device dev;		/* v4l device */
	struct cdev *cdev;		/* 字符设备节点 */

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

	/* 设备信息 */
	char name[32];
	int vfl_type;
	/* 如果注册失败 minor 将被设置为 -1 */
	int minor;
	u16 num;
	/* 需要用位操作 flags */
	unsigned long flags;
	/* attribute to differentiate multiple indices on one physical device */
	int index;

	int debug;			/* Activates debug level*/

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

	/* 释放设备的回调函数 */
	void (*release)(struct video_device *vdev);

	/* ioctl 回调函数 */
	const struct v4l2_ioctl_ops *ioctl_ops;
};
其中
struct cdev {
	struct kobject kobj;    /* 内核对象 */
	struct module *owner;
	const struct file_operations *ops;  /* 设备操作合集 */
	struct list_head list;
	dev_t dev;
	unsigned int count;
};

struct v4l2_file_operations {
	struct module *owner;
	ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);        /* 读数据 */
	ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); /* 写数据 */
	unsigned int (*poll) (struct file *, struct poll_table_struct *);        /* 同步操作 */
	long (*ioctl) (struct file *, unsigned int, unsigned long);              /* 特殊命令 */
	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
	unsigned long (*get_unmapped_area) (struct file *, unsigned long,
				unsigned long, unsigned long, unsigned long);
	int (*mmap) (struct file *, struct vm_area_struct *);                    /* 内存映射 */
	int (*open) (struct file *);                          
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值