linux设备驱动模版

linux设备驱动模版

ID:43515586

大小:46.00 KB

页数:5页

时间:2019-10-09

linux设备驱动模版_第1页
linux设备驱动模版_第2页
linux设备驱动模版_第3页
linux设备驱动模版_第4页
linux设备驱动模版_第5页
资源描述:

《linux设备驱动模版》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库

1、设备驱动模版代码例程Mydriver.c#include#include#include#include#include#include#include#include#include#include

2、/spinlock.h>#include#include#include#include#include#include#include#include#include#include#include

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

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。