实现原理:
分步注册原因:
由于使用register_chrdev()会一次性申请256的空间,但实际使用的并没有这么多,造成资源浪费,所以在实际开发中,大多使用分步注册的方式
分步注册流程:
1、定义字符设备驱动对象
入口函数:
2、给字符设备驱动对象申请空间
3、初始化字符设备驱动对象
4、申请设备号(静态申请、动态申请)
5、注册字符设备驱动对象到内核中
出口函数:
6、注销字符设备驱动对象
代码:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
//定义字符设备驱动指针
struct cdev *cdev;
//存放设备号
dev_t devno;
//目录信息
struct class *cls;
//设备节点信息
struct device *dev;
char kbuf[128]={0};
#if 1
unsigned int major = 0; //动态申请设备号
#else
unsigned int major = 500; //静态申请设备号
#endif
unsigned int minor = 0;
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *off)
{
int ret;
//判断kbuf的大小,如果<size,把size的数值修改为kbuf的大小
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user filed\n");
return -EIO;//拷贝失败返回错误码
}
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *off)
{
int ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;//拷贝失败返回错误码
}
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//定义操作方法结构体变量并初始化
struct file_operations fops=
{
.open=mycdev_open,
.read=mycdev_read,
.write=mycdev_write,
.release=mycdev_close,
};
static int __init mycdev_init(void)
{
int ret,i;
//字符设备驱动申请空间
cdev = cdev_alloc();
if(cdev == NULL)
{
printk("字符设备驱动申请空间失败...\n");
ret = -ENOMEM;
goto ERR1;
}
//字符设备驱动初始化
cdev_init(cdev, &fops);
//申请设备号
if(major == 0) //动态申请
{
ret = alloc_chrdev_region(&devno, minor, 3, "myled");
if(ret)
{
printk("动态申请设备号失败...\n");
goto ERR2;
}
major = MAJOR(devno);
minor = MINOR(devno);
}
else if(major > 0) //静态申请
{
ret = register_chrdev_region(MKDEV(major, minor), 3, "myled");
if(ret)
{
printk("静态申请设备号失败...\n");
goto ERR2;
}
}
//注册字符设备驱动到内核
ret = cdev_add(cdev, MKDEV(major, minor), 3);
if(ret)
{
printk("注册字符设备驱动到内核失败...\n");
goto ERR3;
}
//向上提交目录信息
cls = class_create(THIS_MODULE, "mycdev");
if(IS_ERR(cls))
{
printk("向上提交目录信息失败\n");
goto ERR4;
}
printk("向上提交目录信息成功\n");
//向上提交设备节点信息
for(i=0; i<3; i++)
{
dev = device_create(cls, NULL, MKDEV(major,i), NULL, "mycdev%d", i);
if(IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
goto ERR5;
}
}
printk("向上提交设备节点信息成功\n");
return 0;
ERR5:
for(--i; i>=0; i--)
{
device_destroy(cls, MKDEV(major,i));
}
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major, minor), 3);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit mycdev_exit(void)
{
int i;
//释放空间并注销字符设备驱动
for(i=0; i<3; i++)
{
device_destroy(cls, MKDEV(major,i));
}
class_destroy(cls);
cdev_del(cdev);
unregister_chrdev_region(MKDEV(major, minor), 3);
kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");