一、概述
一个字符型驱动的demo程序,主要功能:申请一块内存,把它当作设备,进行读写操作。它涵盖了编写驱动的主要函数和流程,适合入门驱动人员参考。该程序已经验证通过。
二、源码
有主要3个程序:
Makefile用来编译,my_driver.c驱动程序,test.c是测试程序。
.
├── Makefile
├── my_driver.c
└── test
└── test.c
三、测试
依据上述文件格式排列,将Makefile和my_driver存放到同一个文件夹,
输入命令:
make
这样就已经把驱动加载到了Linux内核中,通过/proc/device查看设备:
输入命令:
cat /proc/device
加载设备文件结点,输入命令
进入带有test.c文件的文件夹,利用gcc编译这个文件:
gcc test.c
四、程序
my_driver.c
#include <linux/module.h>
#include <linux/syscalls.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#define GLOBAL_MAJOR 233 //major device number
#define MAX_SIZE 10
static int my_major = GLOBAL_MAJOR;
struct my_dev
{
struct cdev cdev;
unsigned char mem[MAX_SIZE]; // my memory device
};
struct my_dev *my_devp;
int my_open(struct inode *inode,struct file *filp)
{
filp->private_data = my_devp;
printk(KERN_INFO"my driver open\n");
return 0;
}
int my_release(struct inode *inode,struct file *filp)
{
printk(KERN_INFO"my driver close\n");
return 0;
}
static ssize_t my_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 my_dev *dev = filp->private_data;
if(p >= MAX_SIZE)
return count?-ENXIO:0;
if(count > MAX_SIZE-p)
count = MAX_SIZE-p;
if(copy_to_user(buf,(void*)dev->mem+p,count)){
ret = -EFAULT;
}
else{
*ppos+=count;
ret=count;
}
return ret;
}
static ssize_t my_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 my_dev *dev = filp->private_data;
if(p >= MAX_SIZE)
return count?-ENXIO:0;
if(count > MAX_SIZE-p)
count = MAX_SIZE-p;
memset(dev->mem,0,MAX_SIZE);
if(copy_from_user(dev->mem+p,buf,count)){
ret=-EFAULT;
}
return ret;
}
static loff_t my_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 > MAX_SIZE){
ret = -EINVAL;
break;
}
filp->f_pos = (unsigned int)offset;
ret = filp->f_pos;
break;
case 1:
if((filp->f_pos+offset) < 0){
ret = -EINVAL;
break;
}
if((filp->f_pos+offset) > MAX_SIZE){
ret = -EINVAL;
break;
}
filp->f_pos += offset;
ret = filp->f_pos;
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
static void hello(void)
{
memcpy(&my_devp->mem,"hello",5);
}
static void clear(void)
{
memset(&my_devp->mem,0,MAX_SIZE);
}
static int my_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
{
struct my_dev *dev = filp->private_data;
memset(dev->mem,0,MAX_SIZE);
switch(cmd){
case 0:
hello();
break;
case 1:
clear();
break;
default:
break;
}
return 0;
}
static const struct file_operations my_fops=
{
.owner = THIS_MODULE,
.llseek = my_llseek,
.read = my_read,
.write = my_write,
.unlocked_ioctl = my_ioctl,
.open = my_open,
.release = my_release,
};
static void my_setup_cdev(struct my_dev *dev,int index)
{
int err;
dev_t devno = MKDEV(my_major,index);//设置设备号
cdev_init(&dev->cdev,&my_fops); //初始化
dev->cdev.owner=THIS_MODULE;
dev->cdev.ops=&my_fops;
err=cdev_add(&dev->cdev,devno,1);
if(err)
printk(KERN_NOTICE "Error %d adding %d\n",err,index);
}
static int __init test_init(void)
{
int ret;
dev_t devno=MKDEV(my_major,0);
printk(KERN_INFO "module my_test init\n");
if(my_major)
ret=register_chrdev_region(devno,1,"mytest");
else{
ret=alloc_chrdev_region(&devno,0,1,"mytest");
my_major=MAJOR(devno);
}
if(ret<0)
return ret;
my_devp=kmalloc(sizeof(struct my_dev),GFP_KERNEL);
if(!my_devp){
ret=-ENOMEM;
goto fail_malloc;
}
memset(my_devp,0,sizeof(struct my_dev));
my_setup_cdev(my_devp,0);
return 0;
fail_malloc:
unregister_chrdev_region(devno,1);
return ret;
}
static void __exit test_exit(void)
{
cdev_del(&my_devp->cdev);
kfree(my_devp);
unregister_chrdev_region(MKDEV(my_major,0),1);
printk(KERN_INFO "module my_test exit\n");
}
module_param(my_major,int,S_IRUGO);
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
Makefile:
KONAME = my_driver.o
IFTEST=$(shell lsmod | grep my_driver | wc -l)
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD :=$(shell pwd)
obj-m:=$(KONAME)
module:
make -C $(KERNELDIR) M=$(PWD)
test_install :
ifeq ($(IFTEST),1)
rmmod my_driver
insmod my_driver.ko
else
insmod my_driver.ko
endif
install: test_install
.PHONY:install
clean:
rm *.o *.mod.c modules.order Module.symvers .*.cmd *.ko
rm -rf .tmp_versions
test.c
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#define HELLO 0
#define CLEAR 1
void show(char *buf)
{
int i;
for(i = 0;i < 6;i++){
printf("%c",buf[i]);
}
printf("\n");
}
int main()
{
int dev_fd;
char *buf = (char*)malloc(10);
memset(buf,0,10);
dev_fd=open("/dev/my_test",O_RDWR|O_NONBLOCK);
if(dev_fd==-1){
perror("cann't open file /dev/aes_test:");
exit(-1);
}
write(dev_fd,"hahaha",6);
lseek(dev_fd,0,SEEK_SET);
read(dev_fd,buf,6);
printf("the read buf is: \n");
show(buf);
lseek(dev_fd,0,SEEK_SET);
ioctl(dev_fd,HELLO,0);
read(dev_fd,buf,6);
printf("after HELLO the read buf is: \n");
show(buf);
ioctl(dev_fd,CLEAR,0);
lseek(dev_fd,0,SEEK_SET);
read(dev_fd,buf,6);
printf("after CLEAR the read buf is: \n");
show(buf);
return 0;
}