简单的Linux字符设备驱动程序框架


前言

本文基于S3C2440开发板。

一、认识file_operations结构体

file_operations是字符设备驱动程序中最重要的数据结构。它是一个结构体,这个结构体的内容包含很多东西,整体由函数指针构成,这里需要特别关注的就是 read write open 这三个函数指针,是整个驱动程序的核心。

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 *);
        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 *, loff_t, loff_t, 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 (*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 (*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);
        int (*setlease)(struct file *, long, struct file_lock **);
        long (*fallocate)(struct file *file, int mode, loff_t offset,loff_t len);
        int (*show_fdinfo)(struct seq_file *m, struct file *f);
};

二、认识register_chrdev注册函数

这是一个函数,int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops)术语为“注册驱动程序”,就是告诉内核新增加一个驱动,这个驱动的名字为(*name),然后内核就按照major指定的位置,将file_operations结构的数据注册进内核。

三、open、read、write

/* static: 这里必须需要用到satic这个关键词来修修饰S3C2440_drv_open这个函数。
* 参数2 :struct inode 表示一个磁盘上的具体文件,inode 为文件节点,这个节点只有一个,无论用户打开多少个文件,都只是对应着一个inode结构。
* 参数2 :struct file 表示一个打开的文件,只要打开一个文件,就对应着一个file结构体,file结构体通常用来追踪文件在运行时的状态信息。 
* 返回值 : 文件的操作句柄,用于后续的读写。
*/
static int S3C2440_drv_open(struct inode *inode, struct file *file)
{
	//open核心代码,一般都是对一些硬件的初始化
}
/* static: 这里必须需要用到satic这个关键词来修修饰S3C2440_drv_read这个函数。
* 参数1 :为进行读取信息的目标文件,是open返回的操作句柄。 
* 参数2 :为对应放置信息的缓冲区(即用户空间内存地址)。
* 参数3 :参数size为要读取的信息长度。
* 参数3 :为读的位置相对于文件开头的偏移,在读取信息后,这个指针一般都会移动,移动的值为要读取信息的长度值。
* 返回值 : 返回非负值代表读取成功。
*/
static ssize_t S3C2440_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
	//read核心代码,一般都是对一些硬件数据寄存器,状态寄存器的读取
}
/* static: 这里必须需要用到satic这个关键词来修修饰first_drv_write这个函数。
* 参数1 :为进行读取信息的目标文件,是open返回的操作句柄。 
* 参数2 :buffer为要写入文件的信息缓冲区。
* 参数3 :count为要写入信息的长度。
* 参数4 :ppos为当前的偏移位置,这个值通常是用来判断写文件是否越界。
* 返回值 : 返回非负值代表写入成功。
*/
static ssize_t s3c2440_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	//write核心代码,一般都是对一些输出引脚进行输出高低电平
}

四、对结构体file_operations的填充

static struct file_operations s3c2440_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   s3c2440_drv_open,     
	.write	=	s3c2440_drv_write,	
	.read   =   s3c2440_drv_read,   
};

五、驱动的安装与卸载

①使用register_chrdev注册函数
register_chrdev(0, "first_drv", &s3c2440_drv_fops ); // 注册, 告诉内核

②安装初始化函数:

  • 通过注册函数可以将我们的驱动程序注册进内核,这时候,我们用命令 cat /proc/devices 就可以看到我们的主设备号和注册的名字,但怎么使用这些驱动程序呢,这时候就需要通过这个驱动程序的主设备号去创建一个设备节点,命令是" mknod 名字 c 主设备号 次设备号 " ,每次都需要我们进行手动的去建立设备节点,这样子很麻烦,这时候我们就需要用到busybox的mdev机制,
    echo /sbin/mdev >/proc/sys/kernel/hotplug mdev -s,在注册进内核的时候顺便建立设备节点,就需要用到下面这两行代码
  • firstdrv_class = class_create(THIS_MODULE, "firstdrv"); 在sys目录下创建firstdrv设备类
  • firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xxx");建立一个名字为xxx的设备节点。
    参数1: 指定所要创建的设备所从属的类,也就是class_create的返回值;
    参数2 :是这个设备的父设备,如果没有就指定为NUL;
    参数3 : 是主设备号。就是注册函数随机产生的一个空的序列号。
    参数4 : 是将* device对应的逻辑设备(class_ device)添加到*ls所代表的设备类中。
    参数5 : 设备名称。
    参数6 : 是次设备号。
static int s3c2440_drv_init(void)
{
	major = register_chrdev(0, "first_drv", &s3c2440_drv_fops ); // 注册, 告诉内核

	firstdrv_class = class_create(THIS_MODULE, "firstdrv");

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

	return 0;
}

使用insmod xxx.ko 命令安装驱动的时候,就会调用这个函数,但是这只是一个普通的c语言函数,如何让内核知道这个函数呢,这时候就需要用到下面这个宏来修饰一下:
module_init(s3c2440_drv_init);

有初始化函数,那么也有卸载函数,对于的是rmmod xxx.ko 这个命令

static void s3c2440_drv_exit(void)
{
	unregister_chrdev(major, "first_drv"); // 卸载
	class_device_unregister(firstdrv_class_dev);
	class_destroy(firstdrv_class);
}

这也是一个普通的函数,要让内核知道这个函数也需要用下面这个宏修饰一下:
module_exit(s3c2440_drv_exit);

六、驱动程序头文件

查看内核文件,找到驱动程序的目录,找到相关类型的驱动程序,拷贝其头文件

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>

七、代码整合

first_drv.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>

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

static int S3C2440_drv_open(struct inode *inode, struct file *file)
{
	//open核心代码,一般都是对一些硬件的初始化
}
static ssize_t S3C2440_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
	//read核心代码,一般都是对一些硬件数据寄存器,状态寄存器的读取
}
static ssize_t s3c2440_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
	//write核心代码,一般都是对一些输出引脚进行输出高低电平
}
static struct file_operations first_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   first_drv_open,     
	.write	=	first_drv_write,	   
};


int major;
static int s3c2440_drv_init(void)
{
	major = register_chrdev(0, "first_drv", &s3c2440_drv_fops ); // 注册, 告诉内核

	firstdrv_class = class_create(THIS_MODULE, "firstdrv");

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

	return 0;
}
static void s3c2440_drv_exit(void)
{
	unregister_chrdev(major, "first_drv"); // 卸载
	class_device_unregister(firstdrv_class_dev);
	class_destroy(firstdrv_class);
}

module_init(s3c2440_drv_init);
module_exit(s3c2440_drv_exit);
MODULE_LICENSE("GPL");


makefile文件:

KERN_DIR = /work/system/linux-2.6.22.6

all:
	make -C $(KERN_DIR) M=`pwd` modules 

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order

obj-m	+= first_drv.o

  • /work/system/linux-2.6.22.6 编译过的内核目录

八、驱动程序的测试


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

/* firstdrvtest on
  * firstdrvtest off
  */
int main(int argc, char **argv)
{
	int fd;
	int val = 1;
	fd = open("/dev/xxx", O_RDWR);
	write(fd, &val, 4);
	read(fd, &val, sizeof(val));
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值