3、ccess.h>/*定義設備的從設備號*/#defineMYDRIVER_MINOR174/*定義設備的相關數據結構*/typedefstruct_MYDRIVER_DEV{spinlock_tdev_lock;wait_queue_head_toWait;intopen_count;}MYDRIVER_DEV,*PMYDRIVER_DEV;/*定義設備的狀態(tài)數據結構*/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;/*返回字節(jié)數*/}/*定義設備write的接口函數*/staticssize_t_mydriver_write(structfile*filp,constchar*buf,size_tsize,loff_t*offp){lock_kernel();DBGPRINT(“mydriver_write