虚拟内存设备驱动memdev及实例代码

时间:2021-07-31 03:36:47

驱动:

#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 <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#include <linux/device.h> #ifndef MEMDEV_MAJOR
#define MEMDEV_MAJOR 0 /*预设的mem的主设备号*/
#endif #ifndef MEMDEV_NR_DEVS
#define MEMDEV_NR_DEVS 2 /*设备数*/
#endif #ifndef MEMDEV_SIZE
#define MEMDEV_SIZE 4096
#endif #define DEVICE_NAME "memdev" /*udev自动添加设备节点*/
struct class *mem_class; static int mem_major = MEMDEV_MAJOR; module_param(mem_major, int, S_IRUGO); struct mem_dev
{
struct cdev cdev; /*cdev结构体*/
unsigned char mem[MEMDEV_SIZE]; /*全局内存*/
}; struct mem_dev *mem_devp; /*设备结构体指针*/ /*文件打开函数*/
int mem_open(struct inode *inode, struct file *filp)
{
struct mem_dev *dev; dev=container_of(inode->i_cdev,struct mem_dev,cdev);//通过结构体成员的指针找到对应结构体的指针 /*将设备描述结构指针赋值给文件私有数据指针*/
filp->private_data = dev; return ;
} /*文件释放函数*/
int mem_release(struct inode *inode, struct file *filp)
{
return ;
} /*读函数*/
static ssize_t mem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
{
unsigned long p = *ppos;
unsigned int count = size;
int ret = ;
struct mem_dev *dev = filp->private_data; /*获得设备结构体指针*/ /*判断读位置是否有效*/
if (p >= MEMDEV_SIZE)
return ;
if (count > MEMDEV_SIZE - p)
count = MEMDEV_SIZE - p; /*读数据到用户空间*/
if (copy_to_user(buf, (void*)(dev->mem+ p), count))
{
ret = - EFAULT;
}
else
{
*ppos += count;
ret = count; printk(KERN_INFO "read %d bytes(s) from postion %d\n", count, p);
} return ret;
} /*写函数*/
static ssize_t mem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
{
unsigned long p = *ppos;
unsigned int count = size;
int ret = ;
struct mem_dev *dev = filp->private_data; /*获得设备结构体指针*/ /*分析和获取有效的写长度*/
if (p >= MEMDEV_SIZE)
return ;
if (count > MEMDEV_SIZE - p)
count = MEMDEV_SIZE - p; /*从用户空间写入数据*/
if (copy_from_user(dev->mem + p, buf, count))
ret = - EFAULT;
else
{
*ppos += count;
ret = count; printk(KERN_INFO "written %d bytes(s) to postion %d\n", count, p);
} return ret;
} /* seek文件定位函数 */
static loff_t mem_llseek(struct file *filp, loff_t offset, int whence)
{
loff_t newpos; switch(whence) {
case : /* SEEK_SET */
newpos = offset;
break; case : /* SEEK_CUR */
newpos = filp->f_pos + offset;
break; case : /* SEEK_END */
newpos = MEMDEV_SIZE - + offset;
break; default: /* can't happen */
return -EINVAL;
}
if ((newpos<) || (newpos>MEMDEV_SIZE))
return -EINVAL; filp->f_pos = newpos;
return newpos; } /*文件操作结构体*/
static const struct file_operations mem_fops =
{
.owner = THIS_MODULE,
.llseek = mem_llseek,
.read = mem_read,
.write = mem_write,
.open = mem_open,
.release = mem_release,
}; /*初始化并注册cdev*/
static void mem_setup_cdev(struct mem_dev *dev, int index)
{
int err, devno = MKDEV(mem_major, index); cdev_init(&dev->cdev, &mem_fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &mem_fops;
err = cdev_add(&dev->cdev, devno, );
if (err)
printk(KERN_NOTICE "Error %d adding LED%d", err, index);
} /*设备驱动模块加载函数*/
static int memdev_init(void)
{
int result; dev_t devno = MKDEV(mem_major, ); /* 静态申请设备号*/
if (mem_major)
result = register_chrdev_region(devno, MEMDEV_NR_DEVS, DEVICE_NAME);
else /* 动态分配设备号 */
{
result = alloc_chrdev_region(&devno, , MEMDEV_NR_DEVS, DEVICE_NAME);
mem_major = MAJOR(devno);
} if (result < )
return result; /* 为设备描述结构分配内存*/
mem_devp = kmalloc(MEMDEV_NR_DEVS * sizeof(struct mem_dev), GFP_KERNEL);
if (!mem_devp) /*申请失败*/
{
result = - ENOMEM;
goto fail_malloc;
}
memset(mem_devp, , *sizeof(struct mem_dev)); /*为设备分配内存*/
mem_setup_cdev(&mem_devp[], );
mem_setup_cdev(&mem_devp[], ); /*udev机制可以自动添加设备节点,只需要添加xxx_class这个类,以及device_create()*/
mem_class = class_create(THIS_MODULE, "mem_class");/*在sys目录下创建xx_class这个类,/sys/class/~*/
device_create(mem_class, NULL, mem_devp[].cdev.dev, NULL, "memdev0");/*自动创建设备/dev/$DEVICE_NAME*/
device_create(mem_class, NULL, mem_devp[].cdev.dev, NULL, "memdev1");/*自动创建设备/dev/$DEVICE_NAME*/ printk("memdev driver installed, with major %d\n", mem_major);
printk("the device name is %s\n",DEVICE_NAME);
return ; fail_malloc:
unregister_chrdev_region(devno, ); return result;
} /*模块卸载函数*/
static void memdev_exit(void)
{ device_destroy(mem_class, mem_devp[].cdev.dev);
device_destroy(mem_class, mem_devp[].cdev.dev);
class_destroy(mem_class);
cdev_del(&(mem_devp[].cdev)); /*注销设备*/
cdev_del(&(mem_devp[].cdev)); kfree(mem_devp); /*释放设备结构体内存*/
unregister_chrdev_region(MKDEV(mem_major, ), ); /*释放设备号*/
printk("memdev driver uninstalled\n");
} MODULE_AUTHOR("mhb@SEU");
MODULE_LICENSE("GPL"); module_init(memdev_init);
module_exit(memdev_exit);

测试代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h> #include <string.h> int main()
{
FILE *fp0 = NULL;
char Buf[];
int result; /*初始化Buf*/
strcpy(Buf,"Mem is char dev!");
printf("BUF: %s\n",Buf); /*打开设备文件*/
fp0 = fopen("/dev/memdev0","r+");
if (fp0 == NULL)
{
perror("Open Memdev0 Error!\n");
return -;
} /*写入设备*/
result = fwrite(Buf, sizeof(Buf), , fp0);
if (result == -)
{
perror("fwrite Error!\n");
return -;
} /*重新定位文件位置(思考没有该指令,会有何后果)*/
fseek(fp0,,SEEK_SET); /*清除Buf*/
strcpy(Buf,"Buf is NULL!");
printf("BUF: %s\n",Buf); sleep();
/*读出设备*/
result = fread(Buf, sizeof(Buf), , fp0);
if (result == -)
{
perror("fread Error!\n");
return -;
} /*检测结果*/
printf("BUF: %s\n",Buf); return ; }