实验四的第3个就直接先上代码了,有空了再补上来
3)编写Linux驱动程序并编程应用程序测试。
◆提示1:参考任务1
◆提示2:至少实现xx_open,xx_write,xx_read等函数
◆提示3:功能:
- 内核分配一定长度的缓冲区,比如64字节。
- xx_write()写进去若干字符,注意维护写入位置。下次继续写的话 ,接着该位置往后写,直到缓冲区末尾。要返回实际写入字数。
- xx_read()读出若干字符串,注意维护读出位置。下次继续读的话 ,接着该位置往后读,直到缓冲区末尾。要返回实际读回字数。
- 扩展:
缓冲区设置为循环缓冲区?
如何避免写覆盖,避免读重复?
Makefile
obj-m += test3.o
#generate the path
CURRENT_PATH:=$(shell pwd)
#the current kernel version number
LINUX_KERNEL:=$(shell uname -r)
#the absolute path--根据获取的内核版本拼装绝对路径
LINUX_KERNEL_PATH:=/usr/src/linux-$(LINUX_KERNEL)
#complie object
all:
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
#clean
clean:
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean
设备驱动的代码
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <asm/io.h>
#define VIRTUALDISK_SIZE 255
#define MEM_CLEAR 0x1
#define PORT1_SET 0x2
#define PORT2_SET 0x3
#define VIRTUALDISK_MAJOR 210
#define BUFFER_SIZE 64
static dev_t devno;//创建设备号
static int VirtualDisk_major = VIRTUALDISK_MAJOR;//主设备号
static struct cdev cdev;//构建字符设备结构
unsigned char *device_buffer;
static int W=0,R=0;
static int VirtualDisk_open(struct inode *inode, struct file *filp)
{
printk("cdev open\n");
return 0;
}
static int VirtualDisk_release(struct inode *inode, struct file *filp)
{
printk("cdev release\n");
return 0;
}
static ssize_t VirtualDisk_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
{
unsigned long p = *ppos;
unsigned int count = size;
unsigned char