linux2.6.35動態創建節點的變化總結

       最近做linux驅動,遇到了個問題,由於linux版本變化太快和資料更新慢,有些函數發生變化,其中udev動態創建設備節點的函數也發生變化

          在linux2.6某個版本之後 devfs不復存在,udev成爲devfs的替代,可以用udev在/dev/下動態生成設備文件,其中2.6之後不同版本創建函數也不同,

           在linux2.6.13之前的是

class_simple_create 創建 class

class_simple_destroy 銷燬 class

calss_simple_device_add 創建device

calss_simple_device_remove 銷燬device

  在linux2.6.13之後的是

class_create 創建 class

class_destroy 銷燬 class

calss_device_create 創建device

calss_device_destroy 銷燬device

  在linux2.6.35之中的是

class_create 創建 class

device_create 創建device

class_destroy 銷燬 class

device_destroy 銷燬device

上述這些都是在linux中的device.h中定義的,所以頭文件要包括#include <linux/device.h>

其大致用法如下

struct class *myclass ;
        class_create(THIS_MODULE, “my_device_driver”);
        device_create(myclass, NULL, MKDEV(major_num, minor_num), NULL, “my_device”);

在linux中的定義如下

Class結構體

在2.6.35內核版本中,struct class定義在頭文件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 kobj_ns_type_operations *ns_type;
                const void *(*namespace)(struct device *dev);
                const struct dev_pm_ops *pm;
                struct class_private *p;
                };
class_create(…)在/drivers/base/class.c中實現: 
        /**
        * 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.
        ;*
        * This is used to create a struct class pointer that can then be used
        * in calls to device_create().
        * 
http://www.jybase.net
        * 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.c中,還定義了class_destroy(…)函數,用於在模塊卸載時刪除類,起到相反的作用。

device_create(…)函數在/drivers/base/core.c中實現: 
        /**
        * 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
        * @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,第三個參數是設備號,第四個參數是設備名稱,第五個參數是從設備號。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章