收藏 分销(赏)

简单字符设备驱动程序的设计.doc

上传人:a199****6536 文档编号:1654147 上传时间:2024-05-07 格式:DOC 页数:8 大小:396.50KB
下载 相关 举报
简单字符设备驱动程序的设计.doc_第1页
第1页 / 共8页
简单字符设备驱动程序的设计.doc_第2页
第2页 / 共8页
简单字符设备驱动程序的设计.doc_第3页
第3页 / 共8页
简单字符设备驱动程序的设计.doc_第4页
第4页 / 共8页
简单字符设备驱动程序的设计.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

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)等操作。通常设备驱动程序接口是由结构file_operations结构体向系统说明的,它定义在include/linux/fs.h中。f

3、ile_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 i

4、ovec *, 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

5、 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

6、) (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操作做好必要的准备工作,如清除缓冲区等。如果设备是独占的。则o

7、pen函数必须将设备标记成忙状态。 close入口点close函数负责关闭设备的操作,当最后一次使用设备完成后,调用close函数,关闭设备文件。独占设备必须标记为可再次使用。close()函数作用是关闭打开的文件。 read入口点read函数负责从设备上读数据和命令,有缓冲区的I/O设备操作一般是从缓冲区里读数据。 write入口点write函数负责往设备上写数据。对于有缓冲区的I/O设备操作,一般是把数据写入缓冲区里。对字符设备文件进行写操作将调用write函数。 ioctl入口点ioctl函数执行读、写之外的操作,主要实现对设备的控制。四、实验步骤 (1) file_operations

8、结构体设计(2) 模块初始化、模块卸载函数实现(3) 读写函数的实现(4) 测试程序编写(5) 驱动程序编译和加载(6) 驱动程序测试实验报告要求:(1) 驱动程序工作理说明(2) 驱动程序中使用的数据结构及符号说明。(3) 流程图。(4) 源程序并附上注释。(5) 程序运行结果和分析。五、思考题及其它块设备驱动程序的设计。附件(1)驱动程序string.c#include #include #include #include #include #include MODULE_LICENSE(GPL);#define MAJOR_NUM 254 /主设备号static char str20=H

9、ello,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) /写函数 /将用户空间

10、的数据复制到内核空间的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 = registe

11、r_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(s

12、tring unregister success); module_init(string_init);module_exit(string_exit);(2)测试程序test.c: #include #include #include #include /包括文件操作函数,如open()、close()、read()等main() int fd; char str20; /打开”/dev/string” fd = open(/dev/string, O_RDWR, S_IRUSR | S_IWUSR); /打开设备文件 if (fd != -1 ) /打开设备文件成功 read(fd, &s

13、tr, sizeof(str); /初次读str printf(The string is :%sn, 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 :%sn, str); /关闭”/dev/string” close(fd); else /打开设备文件失败 printf(Device open failuren

14、); (3)调试说明编译驱动程序string.c(如图1): 图1 编译驱动程序模块加载(如图2)图2 编译驱动程序模块加载如果加载成功,就会在/proc/devices中看到该设备:使用命令:cat /proc/devices查看(如图3)图3 查看设备可以看到多了“254 string”一行,说明设备驱动模块加载成功。接着创建设备结点,用户进程就可以通过这个结点访问到字符数组的虚拟设备了:创建设备结点可以使用如下命令:mknod /dev/string c 254 0(如图4)图4 创建设备结点至此设备驱动模块加载完成;为了验证驱动程序的正确性,下面进行测试。首先编译测试文件test.c,

15、使用命令 :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

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服