v4l2驱动1-linux-3.0.8的v4l2_dev.h分析

看一下drivers/media/video下的Makefile你会发现:

videodev-objs:=v4l2-dev.o v4l2-ioctl.o v4l2-device.o v4l2-fh.o \

                        v4l2-event.o v4l2-ctrls.o v4l2-subdev.o

这是生成videodev.ko必要的文件。网上有很多讲解v4l2驱动编写的文章。推荐网址:

这是主要说用户使用:

http://linuxtv.org/downloads/v4l-dvb-apis/

这是说驱动编写:

http://lwn.net/Articles/203924/

对应的中文翻译(用户的不是很全)

http://download.csdn.net/detail/xxxxxlllllxl/7076235

我的博客将从Makefile中涉及的文件对应的头文件去分析:先从v4l2_dev.h开始

/*
 *
 *	V 4 L 2   D R I V E R   H E L P E R   A P I
 *
 * Moved from videodev2.h//从videodev2.h中移出来的
 *
 *	Some commonly needed functions for drivers (v4l2-common.o module)
 */
#ifndef _V4L2_DEV_H
#define _V4L2_DEV_H

#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/mutex.h>
#include <linux/videodev2.h>

#include <media/media-entity.h>

#define VIDEO_MAJOR	81

//下面不同的宏生成的设备名不一样
#define VFL_TYPE_GRABBER	0 //"video"
#define VFL_TYPE_VBI		1 //"vbi"
#define VFL_TYPE_RADIO		2 //"radio"
#define VFL_TYPE_SUBDEV		3 //"v4l-subdev"
#define VFL_TYPE_MAX		4

struct v4l2_ioctl_callbacks;
struct video_device;
struct v4l2_device;
struct v4l2_ctrl_handler;

/* Flag to mark the video_device struct as registered.
   Drivers can clear this flag if they want to block all future
   device access. It is cleared by video_unregister_device. */
#define V4L2_FL_REGISTERED	(0)//标记设备是否被注册
/* file->private_data points to struct v4l2_fh */
#define V4L2_FL_USES_V4L2_FH	(1) //使用文件操作
/* Use the prio field of v4l2_fh for core priority checking */
#define V4L2_FL_USE_FH_PRIO	(2) //文件操作中使用优先级检查

/* Priority helper functions */ //优先级帮助功能

struct v4l2_prio_state {
	atomic_t prios[4];
};
/*
enum v4l2_priority {
	V4L2_PRIORITY_UNSET       = 0,  //不初始化
	V4L2_PRIORITY_BACKGROUND  = 1,  //背景
	V4L2_PRIORITY_INTERACTIVE = 2,  //互动
	V4L2_PRIORITY_RECORD      = 3,  //记录
	V4L2_PRIORITY_DEFAULT     = V4L2_PRIORITY_INTERACTIVE,
};
*/
//优先级是为了Multiple Opens操作,当可以支持设备同事被多个线程打开时
//需要用这优先级去区分那个操作更重要,用那个优先级,那个优先级几上面
//的prios[4]就加一,不用就减一。
void v4l2_prio_init(struct v4l2_prio_state *global); //初始化,就是global清零
int v4l2_prio_change(struct v4l2_prio_state *global, enum v4l2_priority *local,
		     enum v4l2_priority new); //改变优先级,对应prios改变,local会等于new
void v4l2_prio_open(struct v4l2_prio_state *global, enum v4l2_priority *local);//打开,对应的prios[*local]加一
void v4l2_prio_close(struct v4l2_prio_state *global, enum v4l2_priority local);//关闭,对应的prios[local]减一
enum v4l2_priority v4l2_prio_max(struct v4l2_prio_state *global);//当前已打开的最大优先级。
int v4l2_prio_check(struct v4l2_prio_state *global, enum v4l2_priority local);//检查local显示优先级是否是当前打开的最大的优先级


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 *);
	int (*release) (struct file *);
};
/*
对应上面的ops我要说几点:
1. get_unmapped_area,这个最终赋给file_operations对应的函数。
   这是系统sys_mmap2使用的函数,会判断虚拟内存空间中是否有足
   够的空间的工作。
2. read/write读流
   使用read和write方法,每一帧都要通过I/O 操作,在用户和内核
   空间之间拷贝数据。设备cap要或上V4L2_CAP_READWRITE
3. ioctl和unlocked_ioctl
   这两个都通过file_operations中的unlocked_ioctl调用,v4l2的
   unlocked_ioctl会优先调用,ioctl可以认为是序列化的ioctl,会
   上锁等待上一个ioctl完成,如果在驱动睡眠下会很糟糕。
4. ioctl读流
   在设备cap上要或上V4L2_CAP_STREAMING,让用户与内核空间之间
   交换缓冲区指针,这些缓冲区将被映射到用户地址空间,使帧的零
   拷贝成为可能。
   “用户与内核空间之间交换缓冲区指针”我们要理解的是缓冲区是
   在用户空间还是内核空间。对于内核空间我们通过mmap()方法映射。
   对于用户空间就不需要mmap(),此方法比较难用。
*/

/*
 * Newer version of video_device, handled by videodev2.c
 * 	This version moves redundant code from video device code to
 *	the common handler
 */

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 */  //这个dev主要是为了sysfs用的
	struct cdev *cdev;		/* character device */ //这个cdev就是我们生成对应的video字符设备

	/* 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;//这是多video设备控制用的,等说到v4l2_ctrls.h再细说

	/* Priority state. If NULL, then v4l2_dev->prio will be used. */
	struct v4l2_prio_state *prio;//上面说的优先级

	/* device info */
	char name[32]; //设备名,这个设备名是在/sysfs下显示用的
	int vfl_type; //对应上面的VFL_*
	/* ‘minor‘ is set to -1 if the registration failed */
	int minor;
	u16 num;//记录注册的video设备节点号
	/* use bitops to set/clear/test flags */
	unsigned long flags;//主要对应上面的V4L2_FL_*
	/* attribute to differentiate multiple indices on one physical device */
	int index;//通过get_index()获取一个可用设备索引。

	/* V4L2 file handles *///文件操作。v4l2_fh.h再说
	spinlock_t		fh_lock; /* Lock for all v4l2_fhs */
	struct list_head	fh_list; /* List of struct v4l2_fh */

	int debug;			/* Activates debug level*///V4L2_DEBUG_*,不同的赋值,打印出的信息数量不一样

	/* Video standard vars */ //视频标准规范,对应V4L2_STD_*,如我们熟悉的PAL、NTSC。
	v4l2_std_id tvnorms;		/* Supported tv norms */  //支持的标准
	v4l2_std_id current_norm;	/* Current tvnorm */ //当前使用的标准

	/* callbacks */
	void (*release)(struct video_device *vdev);//这个必须有,很多驱动使用video_device_release。

	/* ioctl callbacks */ //v4l2-ioctl.h我们再慢慢说它
	const struct v4l2_ioctl_ops *ioctl_ops;

	/* serialization lock */
	struct mutex *lock;
};

#define media_entity_to_video_device(__e) 	container_of(__e, struct video_device, entity)
/* dev to video-device */
#define to_video_device(cd) container_of(cd, struct video_device, dev)

//这个注册主要说一下warn_if_nr_in_use,就是如果你指定一个nr,但是驱动会使用另一个代替时会打印警告
int __must_check __video_register_device(struct video_device *vdev, int type,
		int nr, int warn_if_nr_in_use, struct module *owner);

/* Register video devices. Note that if video_register_device fails,
   the release() callback of the video_device structure is *not* called, so
   the caller is responsible for freeing any data. Usually that means that
   you call video_device_release() on failure. */
static inline int __must_check video_register_device(struct video_device *vdev,
		int type, int nr)
{
	return __video_register_device(vdev, type, nr, 1, vdev->fops->owner);
}

/* Same as video_register_device, but no warning is issued if the desired
   device node number was already in use. */
static inline int __must_check video_register_device_no_warn(
		struct video_device *vdev, int type, int nr)
{
	return __video_register_device(vdev, type, nr, 0, vdev->fops->owner);
}

/* Unregister video devices. Will do nothing if vdev == NULL or
   video_is_registered() returns false. */
void video_unregister_device(struct video_device *vdev);

/* helper functions to alloc/release struct video_device, the
   latter can also be used for video_device->release(). */
struct video_device * __must_check video_device_alloc(void); //分配video_device结构体

/* this release function frees the vdev pointer */
void video_device_release(struct video_device *vdev); //释放

/* this release function does nothing, use when the video_device is a
   static global struct. Note that having a static video_device is
   a dubious construction at best. */
void video_device_release_empty(struct video_device *vdev);//空函数

/* helper functions to access driver private data. */
static inline void *video_get_drvdata(struct video_device *vdev) //获取私有数据
{
	return dev_get_drvdata(&vdev->dev);
}

static inline void video_set_drvdata(struct video_device *vdev, void *data)//设置私有数据
{
	dev_set_drvdata(&vdev->dev, data);
}

struct video_device *video_devdata(struct file *file);//根据file得到对应的video_device

/* Combine video_get_drvdata and video_devdata as this is
   used very often. */
static inline void *video_drvdata(struct file *file) //结合上面看
{
	return video_get_drvdata(video_devdata(file));
}

static inline const char *video_device_node_name(struct video_device *vdev)
{
	return dev_name(&vdev->dev);
}

static inline int video_is_registered(struct video_device *vdev)//是否已注册
{
	return test_bit(V4L2_FL_REGISTERED, &vdev->flags);
}

#endif /* _V4L2_DEV_H */



v4l2驱动1-linux-3.0.8的v4l2_dev.h分析,布布扣,bubuko.com

v4l2驱动1-linux-3.0.8的v4l2_dev.h分析

上一篇:Linux网络编程服务器模型选择之并发服务器(上)


下一篇:2018届C++B组省赛真题 明码