【Linux-驅動】在sysfs下創建對應的class節點---class_create


在編寫簡單字符設備驅動的時候,可以使用宏class_create在sysfs下創建對應的class節點,便於用戶管理設備:

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

函數 __class_create:

/**
 * class_create - create a struct class structure
 * @owner: 這個class所屬的模塊
 * @name:  class的名稱一般為設備名
 * @key:   對應的(struct lock_class_key)結構體指針
 */
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);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM