实验五:简单字符设备驱动程序的设计实验学时:4实验类型:(设计)一、实验目的1. 理解设备驱动程序的处理过程;2. 掌握Linux设备驱动程序开发的基本过程和设计方法;3. 学会编写简单的字符设备驱动程序。
二、实验条件Linux操作系统gcc三、实验原理及相关知识设备驱动程序是I/O进程与设备控制器之间的通信程序。
驱动程序的功能:⑴接收由设备独立性软件发来的命令和参数,并将命令中的抽象要求转换为具体的要求。
⑵检查用户I/O请求的合法性,了解I/O设备的状态,传递有关参数,设置设备的工作方式。
⑶发出I/O命令。
⑷及时响应由控制器或通道发来的中断请求,并根据其中断类型调用相应的中断处理程序进行处理。
⑸对于设置有通道的计算机系统,驱动程序还应能够根据用户的I/O请求,自动地构建通道程序。
设备驱动程序的处理过程:⑴将抽象要求转换为具体要求⑵检查I/O设备请求的合法性⑶读出和检查设备的状态⑷传送必要的参数⑸工作方式的设置⑹启动I/O设备Linux系统中,设备驱动程序是操作系统内核的重要组成部分,它与硬件设备之间建立了标准的抽象接口。
通过这个接口,用户可以像处理普通文件一样,对硬通常设备驱动程序接口是由结构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 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);...};⑴open 入口点:open函数负责打开设备、准备I/O。
任何时候对设备文件进行打开操作,都会调用设备的open入口点。
所以,open函数必须对将要进行的I/O操作做好必要的准备工作,如清除缓冲区等。
如果设备是独占的。
则open函数必须将设备标记成忙状态。
⑵close入口点close函数负责关闭设备的操作,当最后一次使用设备完成后,调用close函数,关闭设备文件。
独占设备必须标记为可再次使用。
close()函数作用是关闭打开的文件。
⑶read入口点read函数负责从设备上读数据和命令,有缓冲区的I/O设备操作一般是从缓冲⑷write入口点write函数负责往设备上写数据。
对于有缓冲区的I/O设备操作,一般是把数据写入缓冲区里。
对字符设备文件进行写操作将调用write函数。
⑸ioctl入口点ioctl函数执行读、写之外的操作,主要实现对设备的控制。
四、实验步骤(1) file_operations结构体设计(2) 模块初始化、模块卸载函数实现(3) 读写函数的实现(4) 测试程序编写(5) 驱动程序编译和加载(6) 驱动程序测试实验报告要求:(1) 驱动程序工作理说明(2) 驱动程序中使用的数据结构及符号说明。
(3) 流程图。
(4) 源程序并附上注释。
(5) 程序运行结果和分析。
五、思考题及其它块设备驱动程序的设计。
附件(1)驱动程序string.c#include <linux/module.h>#include <linux/init.h>#include <linux/fs.h>#include <linux/types.h>#include <linux/kernel.h>#include <asm/uaccess.h>MODULE_LICENSE("GPL");#define MAJOR_NUM 254 //主设备号static char str[20]="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_write(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={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"); //注册失败}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{printk("string unregister success");}}module_init(string_init);module_exit(string_exit);(2)测试程序test.c:#include <sys/types.h>#include <sys/stat.h>#include <stdio.h>#include <fcntl.h> //包括文件操作函数,如open()、close()、read()等main(){int fd;char str[20];//打开”/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", &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");}}(3)调试说明编译驱动程序string.c(如图1):图1 编译驱动程序模块加载(如图2)图2 编译驱动程序模块加载如果加载成功,就会在/proc/devices中看到该设备:使用命令:cat /proc/devices查看(如图3)图3 查看设备可以看到多了“254 string”一行,说明设备驱动模块加载成功。