驱动函数的基本框架

本文介绍了驱动程序与应用层之间的联系,从应用层角度出发,解析了如何通过设备文件与内核交互。内容包括驱动程序如何通过file_operation结构体注册并实现读写等操作,以及简易驱动程序的基本步骤,如open、write、read函数的编写,驱动注册和卸载。此外,还提到了测试程序的重要性。
摘要由CSDN通过智能技术生成

1 、驱动程序和应用层的联系

1.1 从应用层看

1)首先APP斗调用Open(“dev/xxx”,O_REWR)打开设备文件后,会得到此设备文件的属性,知道属性中的“设备类型”和“主设备号”。
2)然后VFS层通过“设备类型”(如字符设备类型)去内核中的“chrdev”这个数组。在通过APP得到的“主设备号”以此为索引从内核的"chrdev"数组中知道相应的“file_operation”结构。这个结构是驱动程序“register_chrdev”注册到内核的,这样索引找到它。这个结构中有相应的一些成员函数(如read,write等),这些成员函数就对应硬件的读写操作等。
file_operation的结构体如下:

struct file_operations {
   
	struct module *owner;
	loff_t (*llseek) (struct file *, loff_t, int);
	ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
	ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
	ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
	ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
	int (*readdir) (struct file *, void *, filldir_t);
	unsigned int (*poll) (struct file *, struct poll_table_struct *);
	int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
	long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
	int (*mmap) (struct file *, struct vm_area_struct *);
	int (*open) (struct inode *, struct file *);
	int (*flush) (struct file *, fl_owner_t id);
	int (*release) (struct inode *, struct file *);
	int (*fsync) (struct file *, struct dentry *, int datasync);
	int (*aio_fsync) (struct kiocb *, int datasync);
	int (*fasync) (int, struct file *, int);
	int (*lock) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值