在編寫簡單字符設備驅動的時候,可以使用宏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); }