3、ccess.h>/*定義設(shè)備的從設(shè)備號*/#defineMYDRIVER_MINOR174/*定義設(shè)備的相關(guān)數(shù)據(jù)結(jié)構(gòu)*/typedefstruct_MYDRIVER_DEV{spinlock_tdev_lock;wait_queue_head_toWait;intopen_count;}MYDRIVER_DEV,*PMYDRIVER_DEV;/*定義設(shè)備的狀態(tài)數(shù)據(jù)結(jié)構(gòu)*/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;/*定義設(shè)備open的接口函數(shù)*/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;/*打開使用次數(shù)累加*/#endifmydriver_dev.open_count++;if(mydriver_dev.open_co
6、unt==1){DBGPRINT(“mydriver_open:firstopen”);/*第一次打開設(shè)備,在這里可以放些設(shè)備初始化代碼*/}return0;}/*定義設(shè)備close的接口函數(shù)*/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”);/*設(shè)備徹底關(guān)閉,在這里可以放些使設(shè)備休眠,或者poweroff的代碼*/}#ifdefMODULEMOD_DEC_USE_COUNT;/*打開使用次數(shù)遞減*/#endifreturn0;}/*定義設(shè)備read的接口函數(shù)*/staticssize_t_mydriver_read(structfile*filp,char*buf,size_tsize,loff_t*offp){if(size>8192)size=8192;/*
8、copy_to_user()*//*copykernelspacetouserspace.*//*把數(shù)據(jù)從內(nèi)核復(fù)制到用戶空間的代碼,可根據(jù)實(shí)際添加*/returnsize;/*返回字節(jié)數(shù)*/}/*定義設(shè)備write的接口函數(shù)*/staticssize_t_mydriver_write(structfile*filp,constchar*buf,size_tsize,loff_t*offp){lock_kernel();DBGPRINT(“mydriver_write