memdev.h文件示例代码如下:
#ifndef _MEMDEV_H
#define _MEMDEV_H
//采用静态分配设备号
#ifndef MEMDEV_MAJOR
#define MEMDEV_MAJOR 260
#endif
#ifndef MEMDEV_NR_DEVS
#define MEMDEV_NR_DEVS 2
#endif
#ifndef MEMEV_SIZE
#define MEMDEV_SIZE 4096
#endif
struct mem_dev{
char *data;
unsigned long size;
};
#endif
memdev.c文件示例代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "memdev.h"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Retacn Yue");
MODULE_DESCRIPTION("memdev module");
MODULE_ALIAS("memdev module");
static int mem_major=MEMDEV_MAJOR;
module_param(mem_major,int,S_IRUGO);
struct mem_dev *mem_devp;
struct cdev cdev;
/*初始化*/
int mem_open(struct inode *inode,struct file *filp){
struct mem_dev *dev;
int num=MINOR(inode->i_rdev);
if(num>MEMDEV_NR_DEVS){
return -ENODEV;
}
dev=&mem_devp[num];
filp->private_data=dev;
return 0;
}
/*关闭释主资源*/
int mem_release(struct inode *inode,struct file *flip){
return 0;
}
/*从字符设备读取数据*/
static ssize_t mem_read(struct file *filp,char __user *buff,size_t
size,loff_t *poss){
unsigned long p=*poss;
unsigned int count=size;
int ret=0;
struct mem_dev *dev=filp->private_data;
if(p>=MEMDEV_SIZE)
return 0;
if(count>MEMDEV_SIZE-p)
count=MEMDEV_SIZE-p;
if(copy_to_user(buff,(void*)(dev->data+p),count)){
ret=-EFAULT;
}else{
*poss+=count;
ret=count;
printk(KERN_INFO "read %d bytes form %lu\n",count,p);
}
return 0;
}
/*向字符设备发送数据*/
static ssize_t mem_write(struct file *filp,const char __user
*buf,size_t size,loff_t *poss){
unsigned long p=*poss;
unsigned int count=size;
int ret=0;
struct mem_dev *dev=filp->private_data;
if(p>=MEMDEV_SIZE)
return 0;
if(count>MEMDEV_SIZE-p)
count=MEMDEV_SIZE-p;
if(copy_from_user(dev->data+p,buf,count)){
ret=-EFAULT;
}else{
*poss+=count;
ret=count;
printk(KERN_INFO "write %d bytes from %lu\n",count,p);
}
return 0;
}
/*修改文件当前读写位置*/
static loff_t mem_llseek(struct file *filp,loff_t offset, int whence){
loff_t newpos;
switch(whence){
case 0:
newpos=offset;
break;
case 1:
newpos=filp->f_pos+offset;
break;
case 2:
newpos=MEMDEV_SIZE-1+offset;
break;
default:
return -EINVAL;
}
if((newpos<0)||(newpos>MEMDEV_SIZE))
return -EINVAL;
filp->f_pos&#61;newpos;
return 0;
}
/*应用程序在设备上调用的所有操作*/
static const struct file_operations mem_fops&#61;{
.owner&#61;THIS_MODULE,
.llseek&#61;mem_llseek,
.read&#61;mem_read,
.write&#61;mem_write,
.open&#61;mem_open,
.release&#61;mem_release
};
/*初始化方法 &#xff1a;分配设备号
字符设备注册
*/
static int memdev_init(void){
int result;
int i;
dev_t devno&#61;MKDEV(mem_major,0);
/*静态分配*/
if(mem_major){
result&#61;register_chrdev_region(devno,2,"memdev");
}
/*动态分配*/
else{
result&#61;alloc_chrdev_region(&devno,0,2,"memdev");
mem_major&#61;MAJOR(devno);
}
if(result<0)
return result;
/*初始化*/
cdev_init(&cdev,&mem_fops);
cdev.owner&#61;THIS_MODULE;
cdev.ops&#61;&mem_fops;
/*添加设备文件*/
cdev_add(&cdev,MKDEV(mem_major,0),MEMDEV_NR_DEVS);
mem_devp&#61;kmalloc(MEMDEV_NR_DEVS * sizeof(struct
mem_dev),GFP_KERNEL);
if(!mem_devp){
result&#61;-ENOMEM;
goto fail_malloc;
}
memset(mem_devp,0,MEMDEV_NR_DEVS * sizeof(struct mem_dev));
for(i&#61;0;i
mem_devp[i].data&#61;kmalloc(MEMDEV_SIZE,GFP_KERNEL);
memset(mem_devp[i].data,0,MEMDEV_SIZE);
}
return 0;
fail_malloc:
unregister_chrdev_region(devno,2);
return result;
}
static void memdev_exit(void){
cdev_del(&cdev);
kfree(mem_devp);
unregister_chrdev_region(MKDEV(mem_major,0),2);
}
module_init(memdev_init);
module_exit(memdev_exit);
makefile文件示例代码如下&#xff1a;
ifneq ($(KERNELRELEASE),)
obj-m:&#61;memdev.o
else
KERNELDIR:&#61;/opt/FriendlyARM/mini6410/linux/linux-2.6.38
PWD:&#61;$(shell pwd)
all:
make -C $(KERNELDIR) M&#61;$(PWD) modules ARCH&#61;arm
CROSS_COMPILE&#61;arm-linux-
clean:
rm -fr *.ko *.o *.mod.c *mod.o *.symvers
endif
编译&#xff1a;make
生成memdev.ko文件下载到开发板/lib/modules/
加载模块
[root&#64;FriendlyARM modules]# lnsmod memdev.ko
查看设备节点
[root&#64;FriendlyARM modules]# cat /proc/devices
创建设备文件
[root&#64;FriendlyARM modules]# cd /dev
[root&#64;FriendlyARM dev]# mknod memdev0 c 260 0
测试字符设备驱动
示例代码如下&#xff1a;
/*测试字符设备驱动*/
int main(){
int fd;
char buf[4096];
strcpy(buf,"this is a example of charactar devices driver");
printf("buf:%s\n",buf);
fd&#61;open("/dev/memdev0",O_RDWR);
if(fd&#61;&#61;-1){
printf("open memdev failed!\n");
return -1;
}
write(fd,buf,sizeof(buf));
lseek(fd,0,SEEK_SET);
strcpy(buf,"nothing");
read(fd,buf,sizeof(buf));
printf("buf:%s\n",buf);
return 0;
}
编译&#xff1a;
[root&#64;localhost memdev]# arm-linux-gcc -o memdevapp memdevapp.c
将生成的memdevapp下载到开发板&#xff0c;运行应用程序
[root&#64;FriendlyARM plg]# ./memdev/app
运行结果如下&#xff1a;
[root&#64;FriendlyARM plg]# ./memdevapp
buf:this is a example of charactar devices driverwrite 4096 bytes from
0
read 4096 bytes form 0
buf:this is a example of charactar devices driver