您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > linux驱动程序实例
嵌入式系统程序设计大连理工大学软件学院嵌入式系统工程系赖晓晨Linux驱动程序实例rh9驱动程序实例(2.4)ubuntu驱动程序实例(2.6)嵌入式设备linux驱动实例(2.4)★★★★★一、rh9驱动程序实例下面的驱动程序虚拟了一个字符设备驱动程序,运行于RedHatLinux9功能:在内核空间定义一个数据,然后通过测试程序调用驱动,来读取及修改这个数据【例5-21】程序文件driver.cMakefiletest.c/**************************************************linux字符设备驱动程序,运行于rh9linux*赖晓晨*************************************************/#includelinux/module.h#includelinux/init.h#includelinux/fs.h#includeasm/uaccess.hMODULE_LICENSE(GPL);#defineMAJOR_NUM254#defineEFALUT1staticssize_tmy_read(structfile*filp,char*buf,ssize_tsize,loff_t*off);staticssize_tmy_write(structfile*flip,constchar*buf,ssize_tsize,loff_t*off);structfile_operationsmy_ops={read:my_read,write:my_write,};staticintglobal_var=0;/**************************************************linux字符设备驱动程序,运行于rh9linux*赖晓晨*************************************************/#includelinux/module.h#includelinux/init.h#includelinux/fs.h#includeasm/uaccess.hMODULE_LICENSE(GPL);#defineMAJOR_NUM254#defineEFALUT1staticssize_tmy_read(structfile*filp,char*buf,ssize_tsize,loff_t*off);staticssize_tmy_write(structfile*flip,constchar*buf,ssize_tsize,loff_t*off);structfile_operationsmy_ops={read:my_read,write:my_write,};staticintglobal_var=0;必要的头文件/**************************************************linux字符设备驱动程序,运行于rh9linux*赖晓晨*************************************************/#includelinux/module.h#includelinux/init.h#includelinux/fs.h#includeasm/uaccess.hMODULE_LICENSE(GPL);#defineMAJOR_NUM254#defineEFALUT1staticssize_tmy_read(structfile*filp,char*buf,ssize_tsize,loff_t*off);staticssize_tmy_write(structfile*flip,constchar*buf,ssize_tsize,loff_t*off);structfile_operationsmy_ops={read:my_read,write:my_write,};staticintglobal_var=0;遵守GPL协议/**************************************************linux字符设备驱动程序,运行于rh9linux*赖晓晨*************************************************/#includelinux/module.h#includelinux/init.h#includelinux/fs.h#includeasm/uaccess.hMODULE_LICENSE(GPL);#defineMAJOR_NUM254#defineEFALUT1staticssize_tmy_read(structfile*filp,char*buf,ssize_tsize,loff_t*off);staticssize_tmy_write(structfile*flip,constchar*buf,ssize_tsize,loff_t*off);structfile_operationsmy_ops={read:my_read,write:my_write,};staticintglobal_var=0;主设备号/**************************************************linux字符设备驱动程序,运行于rh9linux*赖晓晨*************************************************/#includelinux/module.h#includelinux/init.h#includelinux/fs.h#includeasm/uaccess.hMODULE_LICENSE(GPL);#defineMAJOR_NUM254#defineEFALUT1staticssize_tmy_read(structfile*filp,char*buf,ssize_tsize,loff_t*off);staticssize_tmy_write(structfile*flip,constchar*buf,ssize_tsize,loff_t*off);structfile_operationsmy_ops={read:my_read,write:my_write,};staticintglobal_var=0;定义宏表示错误/**************************************************linux字符设备驱动程序,运行于rh9linux*赖晓晨*************************************************/#includelinux/module.h#includelinux/init.h#includelinux/fs.h#includeasm/uaccess.hMODULE_LICENSE(GPL);#defineMAJOR_NUM254#defineEFALUT1staticssize_tmy_read(structfile*filp,char*buf,ssize_tsize,loff_t*off);staticssize_tmy_write(structfile*flip,constchar*buf,ssize_tsize,loff_t*off);structfile_operationsmy_ops={read:my_read,write:my_write,};staticintglobal_var=0;读函数:从flip指定的文件中读size个字符到buf中/**************************************************linux字符设备驱动程序,运行于rh9linux*赖晓晨*************************************************/#includelinux/module.h#includelinux/init.h#includelinux/fs.h#includeasm/uaccess.hMODULE_LICENSE(GPL);#defineMAJOR_NUM254#defineEFALUT1staticssize_tmy_read(structfile*filp,char*buf,ssize_tsize,loff_t*off);staticssize_tmy_write(structfile*flip,constchar*buf,ssize_tsize,loff_t*off);structfile_operationsmy_ops={read:my_read,write:my_write,};staticintglobal_var=0;写函数:把buf中的size个字符写到flip文件中/**************************************************linux字符设备驱动程序,运行于rh9linux*赖晓晨*************************************************/#includelinux/module.h#includelinux/init.h#includelinux/fs.h#includeasm/uaccess.hMODULE_LICENSE(GPL);#defineMAJOR_NUM254#defineEFALUT1staticssize_tmy_read(structfile*filp,char*buf,ssize_tsize,loff_t*off);staticssize_tmy_write(structfile*flip,constchar*buf,ssize_tsize,loff_t*off);structfile_operationsmy_ops={read:my_read,write:my_write,};staticintglobal_var=0;函数指针设置/**************************************************linux字符设备驱动程序,运行于rh9linux*赖晓晨*************************************************/#includelinux/module.h#includelinux/init.h#includelinux/fs.h#includeasm/uaccess.hMODULE_LICENSE(GPL);#defineMAJOR_NUM254#defineEFALUT1staticssize_tmy_read(structfile*filp,char*buf,ssize_tsize,loff_t*off);staticssize_tmy_write(structfile*flip,constchar*buf,ssize_tsize,loff_t*off);structfile_operationsmy_ops={read:my_read,write:my_write,};staticintglobal_var=0;驱动保存的内核数据staticint__initmy_init(void){intret;ret=register_chrdev(MAJOR_NUM,mydev,&my_ops);if(ret)printk(mydevregisterfailer!\n);elseprintk(mydevregistersuccess!\n);returnret;}staticvoid__exitmy_exit(vo
本文标题:linux驱动程序实例
链接地址:https://www.777doc.com/doc-1903575 .html