




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 民爆员工安全培训计划课件
- 民法总则课件精简
- 初中语文小考试卷及答案
- 采油厂招聘考试题及答案
- 新质生产力三高特征解读
- 民族风情绘画课件
- 新质生产力和人民之间的关系
- 新质生产力赋能房地产新模式
- 标准化等级评定讲解
- 民族民俗课件
- GB/T 45777-2025水泥中石膏掺量评估方法
- 高中数学双向细目表
- AI引擎:Prompt指令设计绿皮书
- 绿化日常工作流程范本
- 江西省专业技术职务任职评审表
- 物联网概述课件
- 中国旅游地理(第四版)中职PPT完整全套教学课件
- 园林机械完整版
- 几何模型“将军饮马”模型(将军饮马、将军遛马、将军造桥)(轴对称模型) 中考数学总复习必会几何模型剖析(全国通用)
- JJG 146-2011量块
- 小学数学思想方法(课件)
评论
0/150
提交评论