Linux驱动模块自动创建设备节点

在宋宝华《Linux设备驱动开发详解》中我们能找到如下描述:

devfs与udev的另一个显著区别在于:采用devfs,当一个并不存在的/dev节点被打开的时候,devfs能自动加载对应的驱动,而udev则不能。这是因为 udev的设计者认为Linux应该在设备被发现的时候加载驱动模块,而不是当它被访问的时候。

udev 完全在用户态工作,利用设备加入或移除时内核所发送的热插拔事件(hotplug event)来工作。在热插拔时,设备的详细信息会由内核输出到位于/sys的sysfs文件系统。udev的设备命名策略、权限控制和事件处理都是在用户态下完成的,它利用sysfs中的信息来进行创建设备文件节点等工作。 

Linux内核为我们提供了一组函数,可以用来在模块加载的时候自动在/dev目录下创建相应设备节点,并在卸载模块时删除该节点,当然前提条件是用户空间移植了udev。

在驱动初始化的代码里调用class_create(…)为该设备创建一个class,再为每个设备调用device_create(…)( 在2.6较早的内核中用class_device_create)创建对应的设备。

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

struct class和device_create(…) 都定义在/include/linux/device.h中,使用的时候一定要包含这个头文件,否则编译器会报错。

在Linux2.6.32.2内核头文件include/linux/device.h中:

/*
 * device classes
 */
struct class {
const char *name;
struct module *owner;


struct class_attribute*class_attrs;
struct device_attribute*dev_attrs;
struct kobject*dev_kobj;


int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
char *(*devnode)(struct device *dev, mode_t *mode);


void (*class_release)(struct class *class);
void (*dev_release)(struct device *dev);


int (*suspend)(struct device *dev, pm_message_t state);
int (*resume)(struct device *dev);


const struct dev_pm_ops *pm;


struct class_private *p;
};


#define class_create(owner, name) \
({ \
static struct lock_class_key __key;\
__class_create(owner, name, &__key);\
})


/**
 * class_create - create a struct class structure
 * @owner: pointer to the module that is to "own" this struct class
 * @name: pointer to a string for the name of this class.
 * @key: the lock_class_key for this class; used by mutex lock debugging
 *
 * This is used to create a struct class pointer that can then be used
 * in calls to device_create().
 *
 * Note, the pointer created here is to be destroyed when finished by
 * making a call to class_destroy().
 */

struct class *__class_create(struct module *owner, const char *name,
    struct lock_class_key *key)
{
struct class *cls;
int retval;


cls = kzalloc(sizeof(*cls), GFP_KERNEL);
if (!cls) {
retval = -ENOMEM;
goto error;
}


cls->name = name;
cls->owner = owner;
cls->class_release = class_create_release;


retval = __class_register(cls, key);
if (retval)
goto error;


return cls;


error:
kfree(cls);
return ERR_PTR(retval);
}

第一个参数指定类的所有者是哪个模块,第二个参数指定类名。

class_destroy(struct class *cls)函数,用于在模块卸载时删除类。

/**
 * device_create - creates a device and registers it with sysfs
 * @class: pointer to the struct class that this device should be registered to
 * @parent: pointer to the parent struct device of this new device, if any
 * @devt: the dev_t for the char device to be added
 * @drvdata: the data to be added to the device for callbacks
 * @fmt: string for the device's name
 *
 * This function can be used by char device classes.  A struct device
 * will be created in sysfs, registered to the specified class.
 *
 * A "dev" file will be created, showing the dev_t for the device, if
 * the dev_t is not 0,0.
 * If a pointer to a parent struct device is passed in, the newly created
 * struct device will be a child of that device in sysfs.
 * The pointer to the struct device will be returned from the call.
 * Any further sysfs files that might be required can be created using this
 * pointer.
 *
 * Note: the struct class passed to this function must have previously
 * been created with a call to class_create().
 */
struct device *device_create(struct class *class, struct device *parent,
    dev_t devt, void *drvdata, const char *fmt, ...)
{
va_list vargs;
struct device *dev;


va_start(vargs, fmt);
dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
va_end(vargs);
return dev;

}

第一个参数指定所要创建的设备所从属的类,第二个参数是这个设备的父设备,如果没有就指定为NULL,第三个参数是设备号,第四个参数是void类型的指针,代表回调函数的输入参数,第五个参数是设备名字符串

函数 device_unregister( struct device *dev);用于在模块卸载是删除设备,在卸载函数中要先删除设备,再删除类,顺序不能颠倒

例:

#define DEVICE_NAME  "fpga_dma"
static int dev_major = 0;
static struct class *fpga_class;
static struct cdev fpgaDevice;


static int  dev_init(void)
{
    int result;
    int err;
    dev_t dev = MKDEV(dev_major, 0);


    if (dev_major)
        result = register_chrdev_region(dev, 1, DEVICE_NAME);
    else {
        result = alloc_chrdev_region(&dev, 0, 1, DEVICE_NAME);
        dev_major = MAJOR(dev);
    }
    if (result < 0)
    {
        printk("unable to get major %d\n", dev_major);
        return result;
    }
    printk("get major is %d\n", dev_major);
    if (dev_major == 0)
        dev_major = result;


    cdev_init(&fpgaDevice, &fpga_dma_fops); 
    fpgaDevice.owner = THIS_MODULE;
    fpgaDevice.ops = &fpga_dma_fops;
    err = cdev_add(&fpgaDevice, dev, 1);
    if (err)
        printk("error %d add fpga ", err);


    fpga_class = class_create(THIS_MODULE, DEVICE_NAME);
    if (IS_ERR(fpga_class))
    {
        printk("Err:failed in creating class.\n");
        return -1;
    }
    class_device_create(fpga_class, NULL, MKDEV(dev_major, 0), NULL, "%s", DEVICE_NAME);
    
    printk("MPC8377core FPGA_GPIO_driver installed OK\n");


    return 0;
}

这样,模块加载后,就能在/dev目录下找到设备节点了。

你可能感兴趣的:(Linux驱动开发基础)