对于想入门Linux设备驱动程序的人来说,字符设备驱动常常是最简单的了解和实践的最佳选择linux伊甸园论坛,这儿就实现一个简单的字符驱动,以易于你们了解字符设备驱动程序。
字符设备驱动程序
这儿就我实现的字符设备驱动程序的源代码来讲解,这只是最简单的实现,以便你们了解linux开发培训,因而并没有涉及到锁,休眠等操作,不过在实际的字符设备驱动程序中,这种操作也是很常见的。
char.c源代码
#include
#include
#include
#include
#include
#include
#include
#define MAX_SIZE 0x1000
#define MEM_CLEAR 0x1
static struct class *class;
static struct device *dev;
struct chardev_dev //定义设备
{
struct cdev cdev;
unsigned char mem[MAX_SIZE];
};
struct chardev_dev *devp;
dev_t devno; //设备号
static int chardev_open(struct inode *inode, struct file *filp) //open函数
{
filp->private_data = devp;
return 0;
}
static int chardev_release(struct inode *inode, struct file *filp) //release函数
{
return 0;
}
static long chardev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) //ioctl函数
{
struct chardev_dev *dev = filp->private_data;
switch (cmd)
{
case MEM_CLEAR:
memset(dev->mem, 0, MAX_SIZE);
printk(KERN_INFO "chardev is set to zeron");
break;
default:
return -EINVAL;
}
return 0;
}
static ssize_t chardev_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) //read函数
{
unsigned long p = *ppos;
unsigned int count = size;
int ret = 0;
struct chardev_dev *dev = filp->private_data;
if (p >= MAX_SIZE)
return -EINVAL;
if (count > MAX_SIZE - p)
count = MAX_SIZE - p;
if (copy_to_user(buf, dev->mem + p, count)) //从内核空间得到数据
ret = -EINVAL;
else
{
*ppos += count;
ret = count;
printk(KERN_INFO "read %u byte(s) from %lun", count, p);
}
return ret;
}
static ssize_t chardev_write(struct file *filp, const char __user *buf, size_t size, //write函数
loff_t *ppos)
{
unsigned long p = *ppos;
unsigned int count = size;
int ret = 0;
struct chardev_dev *dev = filp->private_data;
if (p >= MAX_SIZE)
return -EINVAL;
if (count > MAX_SIZE - p)
count = MAX_SIZE - p;
if (copy_from_user(dev->mem + p, buf, count)) //从用户空间得到写入的数据
ret = -EINVAL;
else
{
*ppos += count;
ret = count;
printk(KERN_INFO "write %u byte(s) from %lun", count, p);
}
return ret;
}
static loff_t chardev_llseek(struct file *filp, loff_t offset, int orig) //llseek函数
{
loff_t ret = 0;
switch (orig) //判断文件指针的位置,确定从何开始读写
{
case 0:
if (offset < 0)
{
ret = -EINVAL;
break;
}
if (offset > MAX_SIZE)
{
ret = -EINVAL;
break;
}
filp->f_pos = 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 const struct file_operations chardev_fops = //字符设备操作函数定义
{
.owner = THIS_MODULE,
.llseek = chardev_llseek,
.read = chardev_read,
.write = chardev_write,
.unlocked_ioctl = chardev_ioctl,
.open = chardev_open,
.release = chardev_release,
};
static char *chardev_devnode(struct device *dev, umode_t *mode)
{
if (mode)
*mode = 0666;
return NULL;
}
static int __init chardev_init(void) //初始化,入口函数
{
int ret;
int err;
ret = alloc_chrdev_region(&devno, 0, 1, "chardev"); //动态申请设备号
if (ret < 0)
return ret;
devp = kzalloc(sizeof(struct chardev_dev), GFP_KERNEL); //分配内存空间
if (!devp)
{
ret = -ENOMEM;
goto fail_malloc;
}
class = class_create(NULL, "chardev"); //创建类节点
if (IS_ERR(class))
{
ret = PTR_ERR(class);
printk(KERN_ERR "class create error %dn", ret);
goto fail_malloc;
}
class->devnode = chardev_devnode; //创建设备节点
dev = device_create(class, NULL, devno, NULL, "chardev");
if (IS_ERR(class))
{
ret = PTR_ERR(dev);
printk(KERN_ERR "device create error %dn", ret);
goto bad_device_create;
}
cdev_init(&devp->cdev, &chardev_fops); //绑定操作函数的结构体
devp->cdev.owner = THIS_MODULE;
err = cdev_add(&devp->cdev, devno, 1); //调用cdev_add函数将cdev结构体注册到内核
if (err)
printk(KERN_NOTICE "Error %d adding chardev", err);
return 0;
bad_device_create:
class_destroy(class);
fail_malloc:
unregister_chrdev_region(devno, 1);
return ret;
}
static void __exit chardev_exit(void) //注销
{
device_destroy(class, devno);
class_destroy(class);
cdev_del(&devp->cdev);
kfree(devp);
unregister_chrdev_region(devno, 1);
}
module_init(chardev_init); //加载模块
module_exit(chardev_exit); //退出模块
MODULE_AUTHOR("lql");
MODULE_LICENSE("GPL");
Makefile文件
ifneq ($(KERNELRELEASE),)
# call from kernel build system
obj-m := char.o
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
注意哦,记得make然后把模块加载进去linux 设备驱动程序,不然的话没有疗效的。
用户空间操作
既然在内核空间实现了字符设备驱动,这么想要测试疗效的话linux 设备驱动程序,可以写两个读写函数来测试:
read函数
#include
#include
#include
#include
#include
#include
int main()
{
int fd,i;
char msg[101];
fd= open("/dev/chardev",O_RDWR,S_IRUSR|S_IWUSR);
if(fd!=-1)
{
for(i=0;i<101;i++)
msg[i]='';
read(fd,msg,100);
printf("%sn",msg);
}
else
{
printf("device open failure,%dn",fd);
}
return 0;
}
write函数
#include
#include
#include
#include
#include
#include
int main()
{
int fd;
char msg[100];
fd= open("/dev/chardev",O_RDWR,S_IRUSR|S_IWUSR);
if(fd!=-1)
{
while(1)
{
printf("Please input the globar:(input quit go out)n");
scanf("%s",msg);
if(strcmp(msg,"quit")==0)
{
close(fd);
break;
}
write(fd,msg,strlen(msg));
}
}
else
{
printf("device open failuren");
}
return 0;
}
这两个c文件正常编译输出,就可以得到对应的实现结果。