After we load the device driver into the kernel, the corresponding device node file will not be generated in the /dev directory. The application program reads and writes the device, and the essence is to read and write the corresponding device node file under /dev.
Therefore, we need to automatically create device nodes when loading into the driver. This needs to be realized by relying on mdev, which is a simplified version of udev, which can detect the status of hardware devices in the system, so as to create or delete device files.
Table of contents
2. Creation/removal of device nodes
1. Class creation/destruction
1. Create a class
A device can have its own type. For example, touch, mouse, and keyboard all belong to input devices. This is the most typical class. You can see other similar classes in /sys/class.
The class structure is used in the kernel to represent a class, and the prototype used to create a class is as follows (essentially a macro)
/*
* @description: 创建一个类
* @param - owner
* @param - name
*/
#define class_create(owner, name) \
({ \
static struct lock_class_key __key; \
__class_create(owner, name, &__key); \
})
extern struct class * __must_check __class_create(struct module *owner,
const char *name,
struct lock_class_key *key);
owner: generally THIS_MODULE
name: The name of the class. After the call is successful, the corresponding class directory will be generated in the /sys/class directory
Return value: Returns a pointer to a class structure
static struct class* _class;
_class = class_create(THIS_MODULE, CHRDEVBASE_NAME);
if (IS_ERR(_class)) // 判断指针是否有效,IS_ERR 在 #include <linux/err.h> 中定义
{
return -1;
}
2. Destruction class
Destroying a class will remove the class directory from /sys/class, the prototype is as follows. The parameter cls represents the class to be destroyed
void class_destroy(struct class *cls);
2. Creation/removal of device nodes
1. Create a device node
Creating a device node will generate a device directory under the corresponding class directory, and will also generate the corresponding device node file under the /dev directory.
The device structure is used in the kernel to represent the device node, and the prototype used to create the device is as follows:
struct device *device_create(struct class *class,
struct device *parent,
dev_t devt,
void *drvdata,
const char *fmt, ...)
class: the class to which the device node belongs
parent: Indicates the parent device, generally NULL, representing no parent device
devt: device number
drvdata: the data required by the device, usually NULL
fmt: device name, if fmt=xxx is set, the device file /dev/xxx will be generated
Return value: the device node pointer of the creation number
2. Remove the device node
When uninstalling the driver, you need to delete the created device, and the corresponding device file under /dev/xxx will also be deleted. The function prototype is as follows:
void device_destroy(struct class *class, dev_t devt);
class: the class to which the device node belongs
devt: device number
Reference article: