已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
内核及设备驱动实验报告1、 实验目的1.学习Linux操作系统下内核程序的编写和应用。2.学习可编程接口芯片的编程控制方法。2、 实验内容与要求1.完成一个内核模块的编写,实现内核模块的正确加载和卸载。2.建立一个虚拟的字符设备驱动程序,至少要包含读、写功能,为用户程序提供内核空间与用户空间的数据交换,方案及实现过程自定。3.在上面的基础上完成8253与自编的应用程序结合,实现特定的功能。3、 实验记录与分析程序清单:1. 驱动程序 #include #include #include #include int init_module(void);void cleanup_module(void);static int device_open(struct inode *, struct file *);static int device_release(struct inode *, struct file *);static ssize_t device_read(struct file *, char *, size_t, loff_t *);static ssize_t device_write(struct file *, const char *, size_t, loff_t *);#define SUCCESS 0#define DEVICE_NAME chardev #define BUF_LEN 80 static int Major; static int Device_Open = 0; static char msgBUF_LEN; static char *msg_Ptr;static struct file_operations fops = .read = device_read, .write = device_write, .open = device_open, .release = device_release;int init_module(void) Major = register_chrdev(0, DEVICE_NAME, &fops); if (Major 0) printk (Registering the character device failed with %dn, Major); return Major; printk(I was assigned major number %d. To talk ton, Major); printk(the driver, create a dev file withn); printk(mknod /dev/hello c %d 0.n, Major); printk(Try various minor numbers. Try to cat and echo ton); printk(the device file.n); printk(Remove the device file and module when done.n); return 0;void cleanup_module(void) int ret = unregister_chrdev(Major, DEVICE_NAME); if (ret i_rdev);printk(Major number=%dn, MAJOR(inode-i_rdev); Device_Open+; sprintf(msg,I have already told you %d times Hello world!n, counter+); msg_Ptr = msg; return SUCCESS;static int device_release(struct inode *inode, struct file *file) Device_Open -; return 0;static ssize_t device_read(struct file *filp,char *buffer, size_t length, loff_t *offset) int bytes_read = 0; if (*msg_Ptr = 0) return 0; while (length & *msg_Ptr) put_user(*(msg_Ptr+), buffer+); length-; bytes_read+; return bytes_read;static ssize_t device_write(struct file *filp,const char *buff,size_t len, loff_t *off) printk (Sorry, this operation isnt supported.n); return -EINVAL;2. Makefile文件 KSRC = /lib/modules/uname -r/build PWD = $(shell pwd) obj-m = chardev.o default: make -C $(KSRC) SUBDIRS=$(PWD) modules clean: $(MAKE) -C $(KSRC) SUBDIRS=$(PWD) clean执行make指令后生成chrdev.ko内核文件 Insmod加载内核后,lsmod查看内核可以看到大小为6900的chardev,使用次数为0通过mknod指令创建设备文件通过ls -l /dev查看设备文件3应用程序 通过运行下面程序访问设备chrdev#include #include int main() int fd, n; char buff80; fd = open(./dev/chrdev, O_RDONLY); if(fd 0) printf(Open File Failedn); return -1; printf(Open File Successedn); n = read(fd, buff, 40); buffn=0; printf(%sn, buff); close(fd); return 0;8253应用程序1驱动程序#include#include#include#include#include#include#include#define TIMERIOMAGIC 250 #define INIT _IOW(TIMERIOMAGIC,0,int)#define DAT _IOW(TIMERIOMAGIC,1,int)#define START _IOW(TIMERIOMAGIC,2,int)#define STOP _IOW(TIMERIOMAGIC,3,int)#define MAJOR 240#define NAME 8253static int tyue_open(struct inode *,struct file *);static int tyue_ioctl(struct inode *,struct file *,unsigned int,unsigned long);static int tyue_close(struct inode *,struct file *);static int Device_open=0;static struct file_operations fops= .open= tyue_open, .ioctl= tyue_ioctl, .release= tyue_close,;int init_module()int ret;ret=register_chrdev(MAJOR,NAME,&fops);return 0;void cleanup_module(void) unregister_chrdev(TIMER_MAJOR,TIMER_NAME); static int tyue_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg) unsigned int freq=0; switch(cmd) case INIT: outb_p(0xb6,0x43); break; case START: outb_p(0x03,0x61); break; case DAT: freq=(unsigned int)arg; outb_p(char)(freq&0x00ff),0x42); outb_p(char)(freq8),0x42); break; case STOP: outb_p(0x00,0x61); break; default: printk(INVALID COMMANDS!n); return 0;static int tyue_open(struct inode *inode,struct file *file)if(Device_open)return -EBUSY;Device_open+;return 0;static int tyue_close(struct inode *inode,struct file *file)Device_open-;outb_p(0,0x61);return 0;2 应用程序2.应用程序#include #include #include #include #include #define INIT _IOW(TIMERIOMAGIC,0,int)#define DAT _IOW(TIMERIOMAGIC,1,int)#define START _IOW(TIMERIOMAGIC,2,int)#define STOP _IOW(TIMERIOMAGIC,3,int)freq=0x237b,0x1f9f,0x1c2c,0x1a90,0x17b7,0x1521,0x12d1;int main()fd=open(8253,O_RDONLY); ioctl(fd,SINIT,0); int i; printf(vioce:1,2,3,4,5,6,7n); printf(stop :Ctrl+cn);for(i=0;i10000;i+) char b; b = getchar(); switch(b) case 1: ioctl(fd,DATA,freq0);break; case 2: ioctl(fd,DATA,freq1);break; case 3: ioctl(fd,DATA,freq2);break; case 4: ioctl(fd,DATA,freq3
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030年中国A环降解物市场深度分析及发展前景研究预测报告
- 2025汉中中考三国文化冲刺卷(语文+历史)
- 商场消防安全规定
- 2025山东德州市武城县招聘城市社区工作者30人备考题库及一套完整答案详解
- 2026中国农业发展银行校园招聘备考题库及答案详解(夺冠系列)
- 2025内蒙古巴彦淖尔市临河区招聘社区工作者80人备考题库附答案详解(精练)
- 2025浦发银行图木舒克支行招聘备考题库(4人)有完整答案详解
- 2025广东深圳市南山区总工会招聘工会社会工作者16人备考题库附答案详解(预热题)
- 辽宁农商银行2026年校园招聘备考题库含答案详解(b卷)
- 2026福建省面向中国矿业大学(北京)选调生选拔工作备考题库及答案详解(必刷)
- 中药分析学(中国药科大学) 知到智慧树网课答案
- 2022年中国-东盟博览会秘书处招聘考试试题及答案
- DZ∕T 0283-2015 地面沉降调查与监测规范(正式版)
- 2024-2030中国半导体旋转接头市场现状研究分析与发展前景预测报告
- 《涂刷类墙面》课件
- 幼儿园冬季流感预防知识教育
- WB/T 1131-2023汽车零部件入厂物流质损判定及处理规范
- 医院药房停电应急预案
- 慢性胃炎与消化性溃疡
- 植物生长与温度环境调控-温度与植物生长发育
- 石油建设项目可行性研究投资估算编制规定
评论
0/150
提交评论