1、实验五:简单字符设备驱动程序的设计 实验学时:4 实验类型:(设计) 一、实验目的 1. 理解设备驱动程序的处理过程; 2. 掌握Linux设备驱动程序开发的基本过程和设计方法; 3. 学会编写简单的字符设备驱动程序。 二、实验条件 Linux操作系统 gcc 三、实验原理及相关知识 设备驱动程序是I/O进程与设备控制器之间的通信程序。 驱动程序的功能: ⑴ 接收由设备独立性软件发来的命令和参数,并将命令中的抽象要求转换为具体的要求。 ⑵ 检查用户I/O请求的合法性,了解I/O设备的状态,传递有关参数,设置设备的工作方式。 ⑶ 发出I/O命令
2、 ⑷ 及时响应由控制器或通道发来的中断请求,并根据其中断类型调用相应的中断处理程序进行处理。 ⑸ 对于设置有通道的计算机系统,驱动程序还应能够根据用户的I/O请求,自动地构建通道程序。 设备驱动程序的处理过程: ⑴ 将抽象要求转换为具体要求 ⑵ 检查I/O设备请求的合法性 ⑶ 读出和检查设备的状态 ⑷ 传送必要的参数 ⑸ 工作方式的设置 ⑹ 启动I/O设备 Linux系统中,设备驱动程序是操作系统内核的重要组成部分,它与硬件设备之间建立了标准的抽象接口。通过这个接口,用户可以像处理普通文件一样,对硬件设备进行打开(open)、关闭(close)、读写(read/write
3、)等操作。 通常设备驱动程序接口是由结构file_operations结构体向系统说明的,它定义在include/linux/fs.h中。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
4、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 *, struc
5、t 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 *); i
6、nt (*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);
7、 ... }; ⑴ open 入口点: open函数负责打开设备、准备I/O。任何时候对设备文件进行打开操作,都会调用设备的open入口点。所以,open函数必须对将要进行的I/O操作做好必要的准备工作,如清除缓冲区等。如果设备是独占的。则open函数必须将设备标记成忙状态。 ⑵ close入口点 close函数负责关闭设备的操作,当最后一次使用设备完成后,调用close函数,关闭设备文件。独占设备必须标记为可再次使用。 close()函数作用是关闭打开的文件。 ⑶ read入口点 read函数负责从设备上读数据和命令,有缓冲区的I/O设备操作一般是从缓冲区里读数据。
8、 ⑷ write入口点 write函数负责往设备上写数据。对于有缓冲区的I/O设备操作,一般是把数据写入缓冲区里。对字符设备文件进行写操作将调用write函数。 ⑸ ioctl入口点 ioctl函数执行读、写之外的操作,主要实现对设备的控制。 四、实验步骤 (1) file_operations结构体设计 (2) 模块初始化、模块卸载函数实现 (3) 读写函数的实现 (4) 测试程序编写 (5) 驱动程序编译和加载 (6) 驱动程序测试 实验报告要求: (1) 驱动程序工作理说明 (2) 驱动程序中使用的数据结构及符号说明。 (3) 流程图。 (4) 源程序并
9、附上注释。
(5) 程序运行结果和分析。
五、思考题及其它
块设备驱动程序的设计。
附件
(1)驱动程序string.c
#include
10、0]="Hello,Wrold!"; //"string"设备的数组 static ssize_t string_read(struct file *filp, char *buf,size_t len,loff_t *loff) //读函数 { //将str[]中的内容从内核空间复制到用户空间 if (copy_to_user(buf, &str, sizeof(str))) { return -1; } return sizeof(str); } static ssize_t string_wri
11、te(struct file *filp,const char *buf,size_t len,loff_t *off) //写函数 { //将用户空间的数据复制到内核空间的str[]数组中 if(copy_from_user(&str,buf,sizeof(str))) { return -1; } return sizeof(str); } //初始化字符设备驱动的file_operations结构体 struct file_operations string_fops= {
12、 read: string_read, //将标准的读取函数指向对应于设备的具体函数 write: string_write, //将标准的写函数指向对应于设备的具体函数 }; static int __init string_init(void) { int ret; //注册设备驱动 ret = register_chrdev(MAJOR_NUM, "string", &string_fops); if (ret) { printk("string register failure"); //注册失败 }
13、 else { printk("string register success"); //注册成功 } return ret; } static void __exit string_exit(void) { int ret; //销毁设备驱动 ret = unregister_chrdev(MAJOR_NUM, "string"); if (ret) { printk("string unregister failure"); } else { pr
14、intk("string unregister success");
}
}
module_init(string_init);
module_exit(string_exit);
(2)测试程序test.c:
#include
15、/dev/string” fd = open("/dev/string", O_RDWR, S_IRUSR | S_IWUSR); //打开设备文件 if (fd != -1 ) //打开设备文件成功 { read(fd, &str, sizeof(str)); //初次读str[] printf("The string is :%s\n", str); printf("Please input the string to the sentence:\n"); scanf("%s
16、", &str); write(fd, &str, sizeof(str)); //写str[] read(fd, &str, sizeof(str)); //再次读str[] printf("The string is :%s\n", str); //关闭”/dev/string” close(fd); } else //打开设备文件失败 { printf("Device open failure\n"); } } (
17、3)调试说明 编译驱动程序string.c(如图1): 图1 编译驱动程序 模块加载(如图2) 图2 编译驱动程序模块加载 如果加载成功,就会在/proc/devices中看到该设备: 使用命令:cat /proc/devices查看(如图3) 图3 查看设备 可以看到多了“254 string”一行,说明设备驱动模块加载成功。 接着创建设备结点,用户进程就可以通过这个结点访问到字符数组的虚拟设备了: 创建设备结点可以使用如下命令:mknod /dev/string c 254
18、0(如图4) 图4 创建设备结点 至此设备驱动模块加载完成;为了验证驱动程序的正确性,下面进行测试。 首先编译测试文件test.c,使用命令 :gcc test.c –o test.o (如图 5) 图5 编译测试程序 编译完成后生成一个test.o 的文件,运行该文件: 使用命令:./test.o (如图 6) 图6 运行测试程序 可以看到初始化时str[]为”Hello,Wrold!”,所以读取的字符串为Hello,Wrold!。此时按照提示输入新数值:“First,test!”情况如下:(如图 7) 图7 运行测试程序 可以看到用户输入字符串到设备后,程序可以读取到该字符串,说明驱动程序编写成功。 当不需要该设备驱动程序时可以卸载该驱动,使用命令为:rmmod string(如图8 ) 图8 卸载驱动 此时驱动程序已被卸载,若再试图操作该设备则情况如下:(如图 9) 图9 测试驱动程序 可以看到提示“Device open failure”字样,这是因为驱动程序已被卸载,所以无法对设备进行操作,即打开设备文件失败。 8






