Linux学习——LED字符设备驱动

1.应用程序的调用

应用层去调用一些接口函数时,会进入内核,驱动也是内核的一部分。以C库函数中的open函数为例,open函数的实现绘制行一条swi val指令,执行之后会引发异常:

内核的第一层:内核的系统调用接口。系统调用接口会根据发生异常的原因调用不同的异常处理函数,比如sys_open函数;

内核的第二层:虚拟文件系统。比如sys_open函数

内核的第三层:驱动。具体操作硬件的函数。sys_open函数会调用具体的硬件函数实现操作。

具体的框架:

2.LED驱动程序

①首先需要定义一个file_operations结构体变量,为了告诉内核有这个驱动

static struct file_operations first_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   first_drv_open, // 功能函数    
    .write  =	first_drv_write,// 功能函数	   
};

②然后把这个结构体变量告诉内核,即注册驱动程序,使用register_chrdev函数

register_chrdev(major, const char *name, const struct file_operation *fops); // 注册, 告诉内核

major是主设备号,是为了方便调用file_operations里面的相关成员函数。

注:在虚拟机上使用ls -l命令查看时开头的:

c表示字符设备驱动

d表示目录(文件夹)

-表示常规文件

第一个数字代表主设备号

③然后决定谁来调用,即设置驱动的入口

int major;
static int first_drv_init(void)
{
	major = register_chrdev(0, "first_drv", &first_drv_fops); // 自动分配主设备号

	firstdrv_class = class_create(THIS_MODULE, "firstdrv");

	firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */

	gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
	gpfdat = gpfcon + 1;

	return 0;
}

有First_init,就有Second_init,各有不同,因此需要一个函数指针方便调用

④使用宏module_init(first_drv_init):设置一个函数指针

module_init(first_drv_init);

⑤定义出口函数,用于卸载驱动,也一样需要使用宏来修饰

static void first_drv_exit(void)
{
	unregister_chrdev(major, "first_drv"); // 卸载

	class_device_unregister(firstdrv_class_dev);
	class_destroy(firstdrv_class);
	iounmap(gpfcon);
}
module_exit(first_drv_exit);

3.总结

一个应用程序,比如是/dev/xxx下的open,read,write,属性为C,主设备号是111,

打开之后:

①经过C库进入内核

②内核在字符设备的结构体数组里根据主设备号找到file_operations结构体并执行对应的功能函数

在驱动程序里实现,先写完功能函数,在定义该结构体变量,在入口函数里用register_chrdev函数将结构体放到该数组里

4.一些操作集锦

挂接nfs文件系统之后,虚拟机应该使用命令cd /mnt才能开始操作

注册驱动使用命令insmod xxx.ko

卸载驱动使用命令rmmod xxx.ko

之后使用命令mknod 目录 设备类型 主设备号 次设备号

查看哪些主设备号有空闲使用命令cat /proc/devices

执行测试程序时应该先使用命令chmod 主设备号+文件名,之后再使用命令 ./文件名

也可以选择自动创建目录/dev/xyz ,即在代码里面加入:

static struct class *firstdrv_class;
static struct class_device	*firstdrv_class_dev;

firstdrv_class = class_create(THIS_MODULE, "firstdrv"); /* 注册的是当前驱动 */

firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */

这个是因为之前创建根文件系统时在rcS命令下使用了:

echo /sbin/mdev > /proc/sys/kernel/hotplug就是热插拔的意思,一有驱动被注册或卸载时系统就会自动创建目录/* /dev/xyz */

5.用到的库函数

1.结构体file_operations

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) (struct file *, int, struct file_lock *);
	ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
	ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
	unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
	int (*check_flags)(int);
	int (*dir_notify)(struct file *filp, unsigned long arg);
	int (*flock) (struct file *, int, struct file_lock *);
	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
	ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
};

2.register_chrdev( )函数:用于注册驱动

int register_chrdev(unsigned int, const char *,
			   const struct file_operations *);

3.class_device_create( )函数:用于自动创建驱动目录

 struct class_device *class_device_create(struct class *cls,
						struct class_device *parent,
						dev_t devt,
						struct device *device,
						const char *fmt, ...)
					__attribute__((format(printf,5,6)));

4.unregister_chrdev( )函数:用于卸载驱动

int unregister_chrdev(unsigned int, const char *);

5.copy_from_user( )函数:用于取出用户程序传进来的值,是用户程序向内核传数据的方法

static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
{
	if (access_ok(VERIFY_READ, from, n))
		n = __copy_from_user(to, from, n);
	else /* security hole - plug it */
		memzero(to, n);
	return n;
}

6.ioremap( )函数:用于将虚拟地址映射为物理地址。

写单片机程序时直接对物理地址操作,但是写驱动是对虚拟地址操作,因此需要这个函数来实现地址映射。

#define ioremap(cookie,size)		__arm_ioremap(cookie, size, MT_DEVICE)
extern void __iomem * __arm_ioremap(unsigned long, size_t, unsigned int);

7.宏module_init/module_exit:用于修饰入口/出口函数

#define module_init(x)	__initcall(x);
#define module_exit(x)	__exitcall(x);

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值