欢迎来到天天文库
浏览记录
ID:43515586
大小:46.00 KB
页数:5页
时间:2019-10-09
《linux设备驱动模版》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、设备驱动模版代码例程Mydriver.c#include#include#include#include#include#include#include#include#include#include2、/spinlock.h>#include#include#include#include#include#include#include#include#include#include#include3、ccess.h>/*定义设备的从设备号*/#defineMYDRIVER_MINOR174/*定义设备的相关数据结构*/typedefstruct_MYDRIVER_DEV{spinlock_tdev_lock;wait_queue_head_toWait;intopen_count;}MYDRIVER_DEV,*PMYDRIVER_DEV;/*定义设备的状态数据结构*/typedefstruct_MYDRIVER_DEV_STATS{unsignedlongrx_intrs;unsignedlon4、grx_errors;unsignedlongrx_blocks;unsignedlongrx_dropped;unsignedlongrx_intrs;unsignedlongrx_errors;unsignedlongrx_missed;unsignedlongrx_blocks;unsignedlongrx_dropped;}MYDRIVER_DEV_STATS,*MYDRIVER_DEV_STATS;unsignedintIntInit=0;/*定义设备open的接口函数*/staticin5、tmydriver_open(structinode*inode,structfile*filp){intminor;DBGPRINT(“mydriver_open”);minor=MINOR(inode->i_rdev);if(minor!=MYDRIVER_MINOR){return-ENODEV;}#ifdefMODULEMOD_INC_USE_COUNT;/*打开使用次数累加*/#endifmydriver_dev.open_count++;if(mydriver_dev.open_co6、unt==1){DBGPRINT(“mydriver_open:firstopen”);/*第一次打开设备,在这里可以放些设备初始化代码*/}return0;}/*定义设备close的接口函数*/staticintmydriver_release(structinode*inode,structfile*filp){DBGPRINT(“mydriver_release”);mydriver_dev.open_count--;if(mydriver_dev.open_count==0){DBG7、PRINT(“mydriver_release:lastclose”);/*设备彻底关闭,在这里可以放些使设备休眠,或者poweroff的代码*/}#ifdefMODULEMOD_DEC_USE_COUNT;/*打开使用次数递减*/#endifreturn0;}/*定义设备read的接口函数*/staticssize_t_mydriver_read(structfile*filp,char*buf,size_tsize,loff_t*offp){if(size>8192)size=8192;/*8、copy_to_user()*//*copykernelspacetouserspace.*//*把数据从内核复制到用户空间的代码,可根据实际添加*/returnsize;/*返回字节数*/}/*定义设备write的接口函数*/staticssize_t_mydriver_write(structfile*filp,constchar*buf,size_tsize,loff_t*offp){lock_kernel();DBGPRINT(“mydriver_write
2、/spinlock.h>#include#include#include#include#include#include#include#include#include#include#include3、ccess.h>/*定义设备的从设备号*/#defineMYDRIVER_MINOR174/*定义设备的相关数据结构*/typedefstruct_MYDRIVER_DEV{spinlock_tdev_lock;wait_queue_head_toWait;intopen_count;}MYDRIVER_DEV,*PMYDRIVER_DEV;/*定义设备的状态数据结构*/typedefstruct_MYDRIVER_DEV_STATS{unsignedlongrx_intrs;unsignedlon4、grx_errors;unsignedlongrx_blocks;unsignedlongrx_dropped;unsignedlongrx_intrs;unsignedlongrx_errors;unsignedlongrx_missed;unsignedlongrx_blocks;unsignedlongrx_dropped;}MYDRIVER_DEV_STATS,*MYDRIVER_DEV_STATS;unsignedintIntInit=0;/*定义设备open的接口函数*/staticin5、tmydriver_open(structinode*inode,structfile*filp){intminor;DBGPRINT(“mydriver_open”);minor=MINOR(inode->i_rdev);if(minor!=MYDRIVER_MINOR){return-ENODEV;}#ifdefMODULEMOD_INC_USE_COUNT;/*打开使用次数累加*/#endifmydriver_dev.open_count++;if(mydriver_dev.open_co6、unt==1){DBGPRINT(“mydriver_open:firstopen”);/*第一次打开设备,在这里可以放些设备初始化代码*/}return0;}/*定义设备close的接口函数*/staticintmydriver_release(structinode*inode,structfile*filp){DBGPRINT(“mydriver_release”);mydriver_dev.open_count--;if(mydriver_dev.open_count==0){DBG7、PRINT(“mydriver_release:lastclose”);/*设备彻底关闭,在这里可以放些使设备休眠,或者poweroff的代码*/}#ifdefMODULEMOD_DEC_USE_COUNT;/*打开使用次数递减*/#endifreturn0;}/*定义设备read的接口函数*/staticssize_t_mydriver_read(structfile*filp,char*buf,size_tsize,loff_t*offp){if(size>8192)size=8192;/*8、copy_to_user()*//*copykernelspacetouserspace.*//*把数据从内核复制到用户空间的代码,可根据实际添加*/returnsize;/*返回字节数*/}/*定义设备write的接口函数*/staticssize_t_mydriver_write(structfile*filp,constchar*buf,size_tsize,loff_t*offp){lock_kernel();DBGPRINT(“mydriver_write
3、ccess.h>/*定义设备的从设备号*/#defineMYDRIVER_MINOR174/*定义设备的相关数据结构*/typedefstruct_MYDRIVER_DEV{spinlock_tdev_lock;wait_queue_head_toWait;intopen_count;}MYDRIVER_DEV,*PMYDRIVER_DEV;/*定义设备的状态数据结构*/typedefstruct_MYDRIVER_DEV_STATS{unsignedlongrx_intrs;unsignedlon
4、grx_errors;unsignedlongrx_blocks;unsignedlongrx_dropped;unsignedlongrx_intrs;unsignedlongrx_errors;unsignedlongrx_missed;unsignedlongrx_blocks;unsignedlongrx_dropped;}MYDRIVER_DEV_STATS,*MYDRIVER_DEV_STATS;unsignedintIntInit=0;/*定义设备open的接口函数*/staticin
5、tmydriver_open(structinode*inode,structfile*filp){intminor;DBGPRINT(“mydriver_open”);minor=MINOR(inode->i_rdev);if(minor!=MYDRIVER_MINOR){return-ENODEV;}#ifdefMODULEMOD_INC_USE_COUNT;/*打开使用次数累加*/#endifmydriver_dev.open_count++;if(mydriver_dev.open_co
6、unt==1){DBGPRINT(“mydriver_open:firstopen”);/*第一次打开设备,在这里可以放些设备初始化代码*/}return0;}/*定义设备close的接口函数*/staticintmydriver_release(structinode*inode,structfile*filp){DBGPRINT(“mydriver_release”);mydriver_dev.open_count--;if(mydriver_dev.open_count==0){DBG
7、PRINT(“mydriver_release:lastclose”);/*设备彻底关闭,在这里可以放些使设备休眠,或者poweroff的代码*/}#ifdefMODULEMOD_DEC_USE_COUNT;/*打开使用次数递减*/#endifreturn0;}/*定义设备read的接口函数*/staticssize_t_mydriver_read(structfile*filp,char*buf,size_tsize,loff_t*offp){if(size>8192)size=8192;/*
8、copy_to_user()*//*copykernelspacetouserspace.*//*把数据从内核复制到用户空间的代码,可根据实际添加*/returnsize;/*返回字节数*/}/*定义设备write的接口函数*/staticssize_t_mydriver_write(structfile*filp,constchar*buf,size_tsize,loff_t*offp){lock_kernel();DBGPRINT(“mydriver_write
此文档下载收益归作者所有