资源描述:
《添加简单的字符设备教程.docx》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、编写简单的字符设备的驱动由于用户进程是通过设备文件同硬件打交道,对设备文件的操作方式不外乎就是一些系统调用,如open,read,write,close....而这些系统调用通过定义structfile_operations结构体和设备驱动程序相关联。所以,编写设备驱动程序的主要工作就是编写子函数,并填充file_operations的各个域。具体步骤如下:① 随便在一个目录下,新建一个目录叫mydev,再在此目录下新建三个文件分别叫Makefilemydev.cmain.c。其中Makefile是编译文件,mydev.c是我们编写的字符设备驱动程序,main.c则是用于
2、测试字符设备驱动的测试程序。三个文件的源程序代码详见附录2.② 打开终端使用su命令,切换到superuser身份,然后写位到mydev目录下。③ 输入make命令,编译驱动程序chardev.c。④ 输入gccmain.c命令,编译测试程序。⑤ 输入insmodmydev.ko加载驱动模块。⑥ 输入gedit/proc/devices查看mydev驱动模块的主设备号。⑦ 输入mknod/dev/mydevc2500在/dev/目录下,创建设备mydev。其中,c代码字符驱动,250是驱动主设备号,0是次设备号。次设备号一般都为0。⑧ 输入./a.out运行测试程序。⑨
3、测试程序退出后,输入rmmodmydev卸载驱动模块,并输入rm-rf/dev/mydev删除/dev/目录下的mydev设备。其安装过程和运行结果如图3-1~图3-3所示。图3-1查看驱动模块主设备号图3-2创建mydev源代码:Makefileobj-m:=mydev.oKDIR:=/lib/modules/$(shelluname-r)/buildSRCPWD:=$(shellpwd)all:make-C$(KDIR)M=$(SRCPWD)modulesclean:rm-rfchardev.omydev.c#include#inclu
4、de#include#include#includestaticintmydevread(structfile*filp,char__user*buffer,size_t,loff_t*);staticintmydevopen(structinode*,structfile*);staticintmydevwrite(structfile*filp,constchar__user*buffer,size_t,loff_t*);staticintmydevrel
5、ease(structinode*,structfile*);staticintmajor;staticcharbuf[100]="Mydevisworking!";staticconststructfile_operationsfile_ops={.read=mydevread,.write=mydevwrite,.open=mydevopen,.release=mydevrelease};staticint__initmydev_init(void){intresult;major=0;result=register_chrdev(major,"mydev",&fil
6、e_ops);if(result<0){printk("registermydevfailed!");returnresult;}if(major==0)major=result;return0;}staticintmydevopen(structinode*inode,structfile*file){try_module_get(THIS_MODULE);printk("mydevopencalled!");return0;}staticintmydevrelease(structinode*inode,structfile*file){module_put(
7、THIS_MODULE);printk("mydevcleancalled!");return0;}staticintmydevread(structfile*filp,char__user*buffer,size_tlength,loff_t*offset){intrd;rd=copy_to_user(buffer,&buf,length);if(rd)returnlength;return-1;}staticintmydevwrite(structfile*filp,constchar__user*buffer,siz