资源描述:
《字符型设备驱动模板》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、字符型设备驱动模板#defineDEVICE_NAME"demo"#definedemo_MAJOR254#definedemo_MINOR0staticssize_tdemo_write(structfile*filp,constchar*buffer,size_tcount){chardrv_buf[];copy_from_user(drv_buf,buffer,count);…}staticssize_tdemo_read(structfile*filp,char*buffer,size_tcount,loff_t*ppos){chardrv_buf[];copy_
2、to_user(buffer,drv_buf,count);…}staticintdemo_ioctl(structinode*inode,structfile*file,unsignedintcmd,unsignedlongarg){}staticintdemo_open(structinode*inode,structfile*file){MOD_INC_USE_COUNT;printk("deviceopensucess!");return0;}staticintdemo_release(structinode*inode,structfile*filp){MO
3、D_DEC_USE_COUNT;printk("devicerelease");return0;}staticstructfile_operationsdemo_fops={owner:THIS_MODULE,write:demo_write,read:demo_read,ioctl:demo_ioctl,open:demo_open,release:demo_release,};#ifdefCONFIG_DEVFS_FSstaticdevfs_handle_tdevfs_demo_dir,devfs_demo_handle;#endifstaticint__init
4、demo_init(void){intresult;#ifdefCONFIG_DEVFS_FSdevfs_demo_dir=devfs_mk_dir(NULL,"demo",NULL);devfs_demo_handle=devfs_register(devfs_demo_dir,"0",DEVFS_FL_DEFAULT,demo_MAJOR,demo_MINOR,S_IFCHR
5、S_IRUSR
6、S_IWUSR,&demo_fops,NULL);#elseSET_MODULE_OWNER(&demo_fops);result=register_chrdev(demo_MA
7、JOR,"demo",&demo_fops);if(result<0)returnresult;if(demo_MAJOR==0)demo_MAJOR=result;/*动态分配主设备号*/#endifprintk(DEVICE_NAME"initialized");return0;}staticvoid__exitdemo_exit(void){#ifdefCONFIG_DEVFS_FSdevfs_unregister(devfs_demo_handle);#elseunregister_chrdev(demo_MAJOR,"demo");#endifprintk(
8、DEVICE_NAME"unloaded");}module_init(demo_init);module_exit(demo_exit);