函数原型:
#define class_create(owner, name)\
({\
static struct lock_class_key __key;\
__class_create(owner, name, &__key);\
})
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;}
void device_destroy(struct class *class, dev_t devt){ struct device *dev;
dev = class_find_device(class, NULL, &devt, __match_devt); if (dev) { put_device(dev); device_unregister(dev); }}
void class_destroy(struct class *cls){if ((cls == NULL) || (IS_ERR(cls)))return;class_unregister(cls);}
实例:
在__init函数中,添加以下代码:
my_class= class_create(THIS_MODULE, "my_class");
if(IS_ERR(my_class))
{
printk("Err: failed increating class.\n");
return -1;
}
device_create( my_class, NULL, devno,NULL, DEV_NAME);//DEV_NAME=设备名;
在__exit函数中,添加以下代码(在unregister_chrdev_region前):
device_destroy(virtual_disk_class, MKDEV(VirtualDisk_major, 0));
class_destroy(virtual_disk_class);