Linux字符设备驱动自动创建设备节点

Linux可使用udev、mdev的机制来自动创建设备节点。

在驱动层面,我们需要首先调用class_create创建一个class类,然后在class类下,调用device_create来创建一个class_device,即类下面创建类的设备。(此方法仅适用于2.6以上的内核)。

内核中定义的struct class结构体,顾名思义,一个struct class结构体类型变量对应一个类,内核同时提供了class_create(…)函数,可以用它来创建一个类,这个类存放于sysfs下面,一旦创建好了这个类,再调用 device_create(…)函数来在/dev目录下创建相应的设备节点。这样,加载模块的时候,用户空间中的udev或者mdev会自动响应 device_create(…)函数,去/sysfs下寻找对应的类从而创建设备节点。

具体步骤如下:

1、定义class和device

static struct class *drv_class;
static struct device *drv_device;

2、创建class和device(在入口函数中)

drv_class = class_create(THIS_MODULE, "drv"); //创建类
drv_device = device_create(drv_class, NULL, MKDEV(major, 0), NULL, "xxx"); //在类下创建设备,供用户空间的应用程序使用

3、卸载类和设备(在出口函数中)

device_unregister(drv_device);  //卸载类下的设备
class_destroy(drv_class); //卸载类

以下是参考的驱动源代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include  //class create

#define SECOND_MAJOR    232

static int second_major = SECOND_MAJOR;
module_param(second_major, int, S_IRUGO);

//定义类和设备
static struct class* seconddrv_class;
static struct device* seconddrv_device;

struct second_dev
{
    struct cdev cdev;
    atomic_t counter;
    //定义内核定时器
    struct timer_list s_timer;
};

static struct second_dev* second_devp;
static void second_timer_handler(unsigned long arg)
{
    mod_timer(&second_devp->s_timer, jiffies + HZ);
    atomic_inc(&second_devp->counter);

    printk(KERN_INFO "current jiffies is %ld\n", jiffies);
}

static int second_open(struct inode* inode, struct file* filp)
{
    init_timer(&second_devp->s_timer);
    second_devp->s_timer.function = &second_timer_handler;
    second_devp->s_timer.expires = jiffies + HZ;

    add_timer(&second_devp->s_timer);

    atomic_set(&second_devp->counter, 0);

    return 0;
}

static int second_release(struct inode* inode, struct file* filp)
{
    del_timer(&second_devp->s_timer);

    return 0;
}

static ssize_t second_read(struct file* filp, char __user* buf, size_t count, loff_t* ppos)
{
    int counter;

    counter = atomic_read(&second_devp->counter);

    if(put_user(counter, (int*)buf))
        return -EFAULT;
    else
        return sizeof(unsigned int);
}

static const struct file_operations second_fops = 
{
    .owner = THIS_MODULE,
    .open = second_open,
    .release = second_release,
    .read = second_read,
};

static void second_setup_cdev(struct second_dev* dev, int index)
{
    int err, devno = MKDEV(second_major, index);

    cdev_init(&dev->cdev, &second_fops);
    dev->cdev.owner = THIS_MODULE;
    err = cdev_add(&dev->cdev, devno, 1);
    if(err)
        printk(KERN_ERR "Failed to add second device\n");
}

static int __init second_init(void)
{
    int ret;
    dev_t devno = MKDEV(second_major, 0);

    if(second_major)
        ret = register_chrdev_region(devno, 1, "second");
    else
    {
        ret = alloc_chrdev_region(&devno, 0, 1, "second");
        second_major = MAJOR(devno);
    }

    if(ret < 0)
        return ret;

    second_devp = kzalloc(sizeof(*second_devp),GFP_KERNEL);
    if(!second_devp)
    {
        ret = -ENOMEM;
        goto fail_malloc;
    }

    second_setup_cdev (second_devp, 0);

    //创建seconddrv类
    seconddrv_class = class_create(THIS_MODULE, "seconddrv");

    //在seconddrv类下创建second设备,供应用程序打开设备
    seconddrv_device = device_create(seconddrv_class, NULL, MKDEV(second_major, 0), NULL, "second");

    return 0;

    fail_malloc:
        unregister_chrdev_region(devno, 1);
        return ret;
}
module_init(second_init);

static void __exit second_exit(void)
{
    cdev_del(&second_devp->cdev);
    kfree(second_devp);
    unregister_chrdev_region(MKDEV(second_major, 0), 1);

    //卸载类下的设备
    device_unregister(seconddrv_device);
    //卸载类
    class_destroy(seconddrv_class);
}
module_exit(second_exit);

MODULE_AUTHOR("were0415");
MODULE_LICENSE("GPL v2");

你可能感兴趣的:(Linux字符设备驱动自动创建设备节点)