您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 设备管理--Linux设备驱动程序安装
集美大学计算机工程学院实验报告课程名称:操作系统班级:xxx实验成绩:指导教师:姓名:xxx实验项目名称:设备管理——Linux设备驱动程序安装学号:xxxx上机实践日期:xxx实验项目编号:组号:上机实践时间:2学时一、目的(本次实验所涉及并要求掌握的知识点)1.认识Linux的设备的种类和设备工作方式;2.理解设备驱动程序的工作原理;3.掌握设备驱动程序的编写规范,能编写并安装简单的设备驱动程序。二、实验内容与设计思想(设计思路、主要数据结构、主要代码结构、主要代码段分析、电路图)实验内容:在Linux系统中,编写一个简单的字符型设备驱动程序模块,设备具有独占特性,可执行读和写操作,相关系统调用为open,close,read,write,open和close分别相当于请求和释放设备,read和write内容保存在设备模块内的缓冲区中。设备模块可动态注册和卸载,并建立与之对应的特殊文件/dev/mydev。实验设计:1.按照要求编写设备驱动模块,同时编写一个测试程序2.分别对其编译,注意编译时的项3.设备模块加载4.创建特殊文件5.分析执行结果6.设备模块卸载三、实验使用环境(本次实验所使用的平台和相关软件)Linux四、实验步骤和调试过程(实验步骤、测试数据设计、测试结果分析)///////////////////////////////////////////////////mydev.c#ifndef__KERNEL__#define__KERNEL__#endif#ifndefMODULE#defineMODULE#endif#define__NO_VERSION__#includelinux/kernel.h#includelinux/module.h#includelinux/version.h#includelinux/config.h#ifCONFIG_MODVERSIONS==1#defineMODVERSIONS#includelinux/modversions.h#endif#includelinux/fs.h#includelinux/wrapper.h#includelinux/types.h#includeasm/segment.h#ifndefKERNEL_VERSION#defineKERNEL_VERSION(a,b,c)((a)*65536+(b)*256+(c))#endif/*Conditionalcompilation.LINUX_VERSION_CODEis*thecode(asperKERNEL_VERSION)ofthisversion.*/#ifLINUX_VERSION_CODEKERNEL_VERSION(2,2,0)#includeasm/uaccess.h/*forput_user*/#endif#defineSUCCESS0#defineDEVICE_NAMEkueng_char_dev#defineBUF_LEN50staticintDevice_Open=0;staticcharMessage[BUF_LEN];staticintMajor;staticintmydev_open(structinode*inode,structfile*file){if(Device_Open)return-EBUSY;Device_Open=1;MOD_INC_USE_COUNT;//模块使用者数加1,非0不能卸载return0;}staticintmydev_release(structinode*inode,structfile*file){Device_Open=0;MOD_DEC_USE_COUNT;//模块使用者数减1return0;}staticssize_tmydev_read(structfile*file,char*buffer,size_tlength,loff_t*f_pos){intbytes_read=0;//确认访问用户内存空间合法性if(verify_area(VERIFY_WRITE,buffer,length)==-EFAULT)return-EFAULT;//由用户空间到系统空间复制bytes_read=copy_to_user(buffer,Message,length);returnbytes_read;}staticssize_tmydev_write(structfile*file,constchar*buffer,size_tlength,loff_t*f_pos){intlen=BUF_LENlength?BUF_LEN:length;//确认访问用户内存空间合法性if(verify_area(VERIFY_READ,buffer,length)==-EFAULT)return–EFAULT;//由用户空间到系统空间复制copy_from_user(Message,buffer,len);returnlength;}structfile_operationsFops={release:mydev_release,open:mydev_open,read:mydev_read,write:mydev_write};intinit_module(void){//设备注册Major=register_chrdev(0,DEVICE_NAME,&Fops);if(Major0){printk(Registeringcharacterdevicefailedwith%d\n,Major);returnMajor;}printk(RegistrationsuccesswithMajordevicenumber%d\n,Major);return0;}voidcleanup_module(void){intret;//设备注销ret=unregister_chrdev(Major,DEVICE_NAME);if(ret0)printk(Errorinunregister_chrdev:%d\n,ret);}MODULE_LICENSE(GPL);MODULE_AUTHOR(KUENG);///////////////////////////////////////////////////////test.c#includestdio.h#includesys/types.h#includesys/stat.h#includefcntl.h#includestring.hmain(){inttestdev;inti;charbuf[50]=peartodev!;printf(programtestisrunning!\n);testdev=open(/dev/mydev,O_RDWR);if(testdev==-1){printf(can'topenfile\n);exit(0);}//向设备写入peartodev!write(testdev,buf,50);printf(write\%s\\n,buf,50);//更改buf内容为appletodev!strcpy(buf,appletodev!);printf(bufferischangedto\%s\\n,buf,50);//由设备读出内容,比较与buf不同read(testdev,buf,50);printf(readfromdevis\%s\\n,buf);//释放设备close(testdev);}运行结果:
本文标题:设备管理--Linux设备驱动程序安装
链接地址:https://www.777doc.com/doc-6450104 .html