cdev_add 和 device_create 函数之间的区别?

Diffrences between cdev_add and device_create function?

我是 linux 设备驱动程序开发的新手。我不明白 cdev_add 实际上 do.I 查看了一些简单的字符设备驱动程序代码,我看到 cdev_add 和 device_create 函数一起使用。 例如:

/* Create device class, visible in /sys/class */
dummy_class = class_create(THIS_MODULE, "dummy_char_class");
if (IS_ERR(dummy_class)) {
    pr_err("Error creating dummy char class.\n");
    unregister_chrdev_region(MKDEV(major, 0), 1);
    return PTR_ERR(dummy_class);
}

/* Initialize the char device and tie a file_operations to it */
cdev_init(&dummy_cdev, &dummy_fops);
dummy_cdev.owner = THIS_MODULE;
/* Now make the device live for the users to access */
cdev_add(&dummy_cdev, devt, 1);

dummy_device = device_create(dummy_class,
                            NULL,   /* no parent device */
                            devt,    /* associated dev_t */
                            NULL,   /* no additional data */
                            "dummy_char");  /* device name */

cdev_add 和 device_create 在此代码中做了什么?

要使用字符驱动,首先要在系统中注册。然后你应该把它暴露给用户 space.

cdev_initcdev_add 函数执行字符设备注册。 cdev_add 将字符设备添加到系统中。当 cdev_add 函数成功完成时,设备处于活动状态,内核可以调用其操作。

为了从用户 space 访问此设备,您应该在 /dev 中创建一个设备节点。为此,您可以使用 class_create, then creating a device and registering it with sysfs using the device_create 函数创建一个虚拟设备 class。 device_create 将在 /dev 中创建一个设备文件。

阅读 Linux Device Drivers, Third Edition,第 3 章(字符驱动程序)以获得对该过程的良好描述(class_createdevice_create 未包含在本书中)。