我是linux设备驱动程序开发的新手。我不明白cdev_add到底在做什么,我看了一些简单的char设备驱动程序代码,我看到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在这段代码中做了什么?
发布于 2018-05-18 15:52:56
要使用字符驱动程序,首先应该在系统中注册它。然后,您应该将它公开给用户空间。
cdev_init和cdev_add函数执行字符设备注册。添加将字符设备添加到系统中。当cdev_add函数成功完成时,设备处于活动状态,内核可以调用其操作。
为了从用户空间访问该设备,您应该在/dev中创建一个设备节点。为此,您可以使用创建创建一个虚拟设备类,然后创建一个设备并使用创建函数向sysfs注册它。device_create将在/dev中创建一个设备文件。
请阅读Linux设备驱动程序,第三版,第3章(Char驱动程序),以获得对流程的良好描述(class_create和device_create未在书中介绍)。
https://stackoverflow.com/questions/50377327
复制相似问题