您的位置:首页 > 其它

基于Ubuntu16.04 的globalmem驱动测试

2017-12-14 14:34 309 查看

基于Ubuntu16.04 的globalmem驱动测试

网上大多数都是基于2.6的内核,自己懒得替换内核,基于最新内核需要做一些修改。代码参考基本来自网上,自己做一个综合。

一.首先检查当前内核版本



二,安装驱动

1.实验代码

#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>
//高版本替换 system.h
#include <asm/switch_to.h>
#include <asm/uaccess.h>
////////增加此头文件//////////
#include <linux/slab.h>
////////////////////////////
#define GLOBALMEM_SIZE  0x1000  /*全局内存最大4K字节*/
#define MEM_CLEAR 0x1  /*清0全局内存*/
#define GLOBALMEM_MAJOR 666    /*预设的globalmem的主设备号*/
static int globalmem_major = GLOBALMEM_MAJOR;
/*globalmem设备结构体*/
struct globalmem_dev
{
struct cdev cdev; /*cdev结构体*/
unsigned char mem[GLOBALMEM_SIZE]; /*全局内存*/
};

struct globalmem_dev *globalmem_devp; /*设备结构体指针*/
/*文件打开函数*/
int globalmem_open(struct inode *inode, struct file *filp)
{
/*将设备结构体指针赋值给文件私有数据指针*/
filp->private_data = globalmem_devp;
return 0;
}
/*文件释放函数*/
int globalmem_release(struct inode *inode, struct file *filp)
{
return 0;
}

/* ioctl设备控制函数 */
static int globalmem_ioctl(struct inode *inodep, struct file *filp, unsigned
int cmd, unsigned long arg)
{
struct globalmem_dev *dev = filp->private_data;/*获得设备结构体指针*/

switch (cmd)
{
case MEM_CLEAR:
memset(dev->mem, 0, GLOBALMEM_SIZE);
printk(KERN_INFO "globalmem is set to zero/n");
break;

default:
return  - EINVAL;
}
return 0;
}

/*读函数*/
static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size,
loff_t *ppos)
{
unsigned long p =  *ppos;
unsigned int count = size;
int ret = 0;
struct globalmem_dev *dev = filp->private_data; /*获得设备结构体指针*/

/*分析和获取有效的写长度*/
if (p > GLOBALMEM_SIZE)
return count ?  - ENXIO: 0;
if (count > GLOBALMEM_SIZE - p)
count = GLOBALMEM_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 %d/n", count, p);
}

return ret;
}

/*写函数*/
static ssize_t globalmem_write(struct file *filp, const char __user *buf,
size_t size, loff_t *ppos)
{
unsigned long p =  *ppos;
unsigned int count = size;
int ret = 0;
struct globalmem_dev *dev = filp->private_data; /*获得设备结构体指针*/

/*分析和获取有效的写长度*/
if (p > GLOBALMEM_SIZE)
return count ?  - ENXIO: 0;
if (count > GLOBALMEM_SIZE - p)
count = GLOBALMEM_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) from %d/n", count, p);
}

return ret;
}

/* seek文件定位函数 */
static loff_t globalmem_llseek(struct file *filp, loff_t offset, int orig)
{
loff_t ret = 0;
switch (orig)
{
case 0:   /*相对文件开始位置偏移*/
if (offset < 0)
{
ret =  - EINVAL;
break;
}
if ((unsigned int)offset > GLOBALMEM_SIZE)
{
ret =  - EINVAL;
break;
}
filp->f_pos = (unsigned int)offset;
ret = filp->f_pos;
break;
case 1:   /*相对文件当前位置偏移*/
if ((filp->f_pos + offset) > GLOBALMEM_SIZE)
{
ret =  - EINVAL;
break;
}
if ((filp->f_pos + offset) < 0)
{
ret =  - EINVAL;
break;
}
filp->f_pos += offset;
ret = filp->f_pos;
break;
default:
ret =  - EINVAL;
break;
}
return ret;
}

/*文件操作结构体*/
static const struct file_operations globalmem_fops =
{
.owner = THIS_MODULE,
.llseek = globalmem_llseek,
.read = globalmem_read,
.write = globalmem_write,
//.ioctl 改为 .compat_ioctl
.compat_ioctl = globalmem_ioctl,
.open = globalmem_open,
.release = globalmem_release,
};

/*初始化并注册cdev*/
static void globalmem_setup_cdev(struct globalmem_dev *dev, int index)
{
int err, devno = MKDEV(globalmem_major, index);

cdev_init(&dev->cdev, &globalmem_fops);
dev->cdev.owner = THIS_MODULE;
dev->cdev.ops = &globalmem_fops;
err = cdev_add(&dev->cdev, devno, 1);
if (err)
printk(KERN_NOTICE "Error %d adding LED%d", err, index);
}

/*设备驱动模块加载函数*/
int globalmem_init(void)
{
int result;
dev_t devno = MKDEV(globalmem_major, 0);

/* 申请设备号*/
if (globalmem_major)
result = register_chrdev_region(devno, 1, "globalmem");
else  /* 动态申请设备号 */
{
result = alloc_chrdev_region(&devno, 0, 1, "globalmem");
globalmem_major = MAJOR(devno);
}
if (result < 0)
return result;

/* 动态申请设备结构体的内存*/
globalmem_devp = kmalloc(sizeof(struct globalmem_dev), GFP_KERNEL);
if (!globalmem_devp)    /*申请失败*/
{
result =  - ENOMEM;
goto fail_malloc;
}
memset(globalmem_devp, 0, sizeof(struct globalmem_dev));

globalmem_setup_cdev(globalmem_devp, 0);
return 0;

fail_malloc: unregister_chrdev_region(devno, 1);
return result;
}

/*模块卸载函数*/
void globalmem_exit(void)
{
cdev_del(&globalmem_devp->cdev);   /*注销cdev*/
kfree(globalmem_devp);     /*释放设备结构体内存*/
unregister_chrdev_region(MKDEV(globalmem_major, 0), 1); /*释放设备号*/
}

MODULE_AUTHOR("CK OS test");
MODULE_LICENSE("Dual BSD/GPL");

module_param(globalmem_major, int, S_IRUGO);

module_init(globalmem_init);
module_exit(globalmem_exit);


2.Makefile

ifneq ($(KERNELRELEASE),)
obj-m:= globalmem.o
else
KERNELDIR =/lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm ‐rf *.o *~ core .depend .*.cmd *.ko *.mod.c
endif


执行make

sudo insmod globalmem.ko

若插入错误 insmod: can’t insert ‘memdev.ko’: Device or resource busy

检查设备号是否被占用自己重新编译一下。

cat /proc/devices 是否有已经用了 GLOBALMEM_MAJOR



3.创建设备节点

sudo mknode /dev/globalmem c 666 0

修改读写权限

sudo chmod 666 /dev/globalmem

4.设备测试



三,驱动测试

实验代码

#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
main()
{
char str[1024],str2[1024];
int fd,stop;
int ret;
fd=open("/dev/globalmem",O_RDWR,S_IRUSR|S_IWUSR);
printf("Dev number %d\n",fd);
if(fd != -1)
{
printf("Please input the string written to globalmem:\n");
scanf("%s",str);
printf("The str is %s\n",str);
ioctl(fd,1,NULL);
ret=write(fd,str,strlen(str));
printf("The ret is %d\n",ret);
lseek(fd,0,SEEK_SET);
scanf("%d",&stop);
read(fd,str2,100);
printf(" The globalmem is %s\n",str2);
close(fd);
}else{
printf("Device open failure\n");
}
}


运行截图



四.总结

内核编译替换需要注意

驱动文件里的相关函数要对应相应的内核版本

编译的时候,注意makefile文件,设计环境依赖和链接

插入模块的时候,注意插入权限,设备号是否重复,读写权限

资源全打包

http://download.csdn.net/download/qq_33728573/10158398
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息