3、no.h>#include#include#defineTEST_DEVICE_NAME"test_dev"#defineBUFF_SZ1024/*全局變量*/staticstructcdevtest_dev;unsignedintmajor=0;staticchar*data=NULL;/*函數(shù)聲明*/staticssize_ttest_read(structfile*file,char*buf,size_tcount,loff_t*f_pos);staticssize_ttest_write(structfile*f
4、ile,constchar*buffer,size_tcount,loff_t*f_pos);staticinttest_open(structinode*inode,structfile*file);staticinttest_release(structinode*inode,structfile*file);/*讀函數(shù)*/staticssize_ttest_read(structfile*file,char*buf,size_tcount,loff_t*f_pos){intlen;if(count<0){return-EINVAL;}len=strlen(data);co
5、unt=(len>count)?count:len;if(copy_to_user(buf,data,count)){return-EFAULT;}returncount;}/*寫函數(shù)*/staticssize_ttest_write(structfile*file,constchar*buffer,size_tcount,loff_t*f_pos){if(count<0){return-EINVAL;}memset(data,0,BUFF_SZ);count=(BUFF_SZ>count)?count:BUFF_SZ;if(copy_from_user(data,buffer
6、,count)){return-EFAULT;}returncount;}/*打開函數(shù)*/staticinttest_open(structinode*inode,structfile*file){printk("Thisisopenoperation");data=(char*)kmalloc(sizeof(char)*BUFF_SZ,GFP_KERNEL);if(!data){return-ENOMEM;}memset(data,0,BUFF_SZ);return0;}/*關(guān)閉函數(shù)*/staticinttest_release(structinode*inode,str
7、uctfile*file){printk("Thisisreleaseoperation");if(data){kfree(data);data=NULL;}return0;}staticvoidtest_setup_cdev(structcdev*dev,intminor,structfile_operations*fops){interr,devno=MKDEV(major,minor);cdev_init(dev,fops);dev->owner=THIS_MODULE;dev->