字符驱动
-------------------------------------------------------------
核心 file_operations 结构体
--------------------------------------------------------------
注册字符设备方法
步骤:1.申请设备号
2.设置cdev 结构体
3.初始化 cdev结构体
4.将cdev 结构体添加到链表中
申请设备号:
方法1: int register_chrdev_region(dev_t from, unsigned count, const char *name)
方法2. int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,const char *name)
方法1中,需要先设置设备号,在不能动态的申请设备号,可以申请用一个主设备下的count个从设备
方法2中,动态的申请设备号,需要提供次设备的base ,可以连续申请count个从设备
申请 cdev 结构体
cdev 结构体可以使用全局变量,可以使用使用指针
使用指针就需要使用cdev_alloc 进行申请
设置cdedv 结构体
void cdev_init(struct cdev *cdev, const struct file_operations *fops)
cdev_init --->将file_opertions 结构体赋值给cdev 的ops
其实也可以进行手工的设置
cdev 添加到链表中
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
----------------------------------------------------------------------------------------
注销:
从链表中删除cdev
void cdev_del(struct cdev *p)
-------------------------------------------------------------------------------------
字符设备的注册还可以使用一个简化的版本 register_chrdev
int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops)
如果major 为0 的话,则动态申请主设备号。
//撤销注册字符设备
int unregister_chrdev(unsigned int major, const char *name)
------------------------------------------------------------------
字符设备申请的主设备号,会在 proc/devices 显示已经申请的主设备号。
次设备号,是如何体现出来的呢?次设备号,是通过/dev/xxx 即dev下的设备体现出来的。
----------------------------------------------------------------------------------------------------
在字符设备驱动中,以上的操作仅仅完成了设备号的申请,并没有涉及到生成设备,
如果不自动生成设备,就需要手动的使用mknod命令来创建这个设置。
自动创建设备:
自动创建设备的前提是创建class类。只有在创建了class类后。自动创建设备
类的创建:struct class *class_create(struct module *owner, const char *name)
功能:create a struct class structure
参数:owner : THIS_MODULE
name: pointer to a string for the name of this class.
返回值:
返回值需要使用 IS_ERR判断
if(IS_ERR(cls))
{
PTR_ERR(cls);
}
这样的处理方式
类的注销: void class_destroy(struct class *cls)
以上的操作也仅仅是创建了类。并没有进行设备的创建
设备的创建:
struct device *device_create(struct class *class, struct device *parent, dev_t devt, const char *fmt, ...)
功能:creates a device and registers it with sysfs
参数:class :创建的类
parent: pointer to the parent struct device of this new device, if any
devt:设备号。devt=MKDEV(major,min)
fmt: 这里就可以创建多个次设备 ( eg:"dev%d", i)
返回值:返回值的判断也需要和class_create一样进行判断
设备的注销:
void device_destroy(struct class *class, dev_t devt)
//如果有多个次设备的注销,需要使用MKDEV(major,min)生成devt。并循环调用 device_destroy
----------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
static struct cdev * pdev = NULL;
#define DEVNAME "demo"
static int major = 0;
static int minor = 0;
static int count = 3;
#define KMAX 1024
static char kbuf[KMAX];
static int counter = 0;
static struct class *cls = NULL;
//打开设备
//int open(const char *pathname, int flags);
static int demo_open(struct inode *inode, struct file *filp)
{
printk("%s:%d, pid = %d\n", __func__, __LINE__, current->pid);
printk("%s:%d, %d\n", __func__, MAJOR(inode->i_rdev), MINOR(inode->i_rdev));
return 0;
}
//关闭设备
//int close(int fd)
static int demo_release(struct inode *inode, struct file *filp)
{
printk("%s:%d, pid = %d\n", __func__, __LINE__, current->pid);
printk("%s:%d, %d\n", __func__, MAJOR(inode->i_rdev), MINOR(inode->i_rdev));
return 0;
}
//读设备
//ssize_t read(int fd, void *buf, size_t count);
static ssize_t demo_read(struct file *filp, char __user *buffer, size_t size, loff_t *offset)
{
printk("%s:%d, pid = %d\n", __func__, __LINE__, current->pid);
printk("%s:%p, %d\n", __func__, buffer, size);
if(0 == counter){
return -EAGAIN;
}
if(counter < size){
size = counter;
}
if(size == copy_to_user(buffer, kbuf, size)){
return -EAGAIN;
}
counter = 0;
return size;
}
//写设备
static ssize_t demo_write(struct file *filp, const char __user *buffer, size_t size, loff_t *offset)
{
printk("%s:%d, pid = %d\n", __func__, __LINE__, current->pid);
printk("%s:%p, %d\n", __func__, buffer, size);
if(size > KMAX){
return -ENOMEM;
}
if(size == copy_from_user(kbuf, buffer, size)){
return -EAGAIN;
}
counter = size;
return size;
}
static struct file_operations misc = {
.owner = THIS_MODULE,
.open = demo_open,
.release= demo_release,
.read = demo_read,
.write = demo_write,
};
static int main_open(struct inode *inode, struct file *filp)
{
printk("%s:%d, pid = %d\n", __func__, __LINE__, current->pid);
printk("%s:%d, %d\n", __func__, MAJOR(inode->i_rdev), MINOR(inode->i_rdev));
filp->f_op = &misc;
return filp->f_op->open(inode, filp);
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = main_open,
};
static int __init demo_init(void)
{
dev_t dev;
int ret, i;
struct device *device = NULL;
printk("%s:%d\n", __func__, __LINE__);
//1.alloc obj
pdev = cdev_alloc();
if(NULL == pdev){
printk("cdev_alloc fail.\n");
return -ENOMEM;
}
//2.init obj
cdev_init(pdev, &fops);
ret = alloc_chrdev_region(&dev, minor, count, DEVNAME);
if(ret){
printk("alloc_chrdev_region fail.\n");
goto ERR_STEP;
}
major = MAJOR(dev);
//3. register obj
ret = cdev_add(pdev, dev, count);
if(ret){
printk("cdev_add fail.\n");
goto ERR_STEP1;
}
cls = class_create(THIS_MODULE, DEVNAME);
if (IS_ERR(cls)) {
ret = PTR_ERR(cls);
goto ERR_STEP1;
}
for(i = minor; i < count+minor; i++){
device = device_create(cls, NULL, MKDEV(major, i), NULL, "%s%d", DEVNAME, i);
if (IS_ERR(device)) {
ret = PTR_ERR(device);
goto ERR_STEP2;
}
}
return 0; //0-成功;失败:负数,绝对值是错误码,(被保存到应用进程的errno)
ERR_STEP2:
for(i--; i >= minor; i--){
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
ERR_STEP1:
unregister_chrdev_region(dev, count);
ERR_STEP:
cdev_del(pdev);
return ret;
}
static void __exit demo_exit(void)
{
int i;
printk("%s:%d\n", __func__, __LINE__);
//1. free resorce
unregister_chrdev_region(MKDEV(major, minor), count);
for(i = minor; i < count+minor; i++){
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
//2.unregister
cdev_del(pdev);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");