Int register_chrdev (unsigned int major, const char * Name, Struct file_operations * FoPs );Among them, Major is the master device number applied to the system for the device driver. If it is 0, the system dynamically allocates a master device number for the driver. Name is the device name. Fops is the preceding description of each call's entry point. If this function returns 0, the operation is successful. -Einval indicates that the requested primary device number is invalid. Generally, the primary device number is greater than the maximum device number allowed by the system. Return-ebusy indicates that the requested master device number is being used by other device drivers. If the master device number is successfully allocated dynamically, this function returns the assigned master device number. If register_chrdev is successful, the device name will appear in the/proc/devices file. After a device driver is successfully registered with the system (after register_chrdev () is successfully called), you can use the mknod command to map the device to a special file, when other programs use this device, they only need to perform operations on this special file. |
After a Linux kernel version of 2.6, devfs no longer exists, and udev becomes a replacement for devfs. Compared with devfs, udev has many advantages, so it won't be long-winded here. Note that udev is the application layer, so do not try to find it in the Kernel configuration options; it is very easy to add udev support, take the character Device Driver written by the author as an example. In the driver initialization code, call class_create to create a class for the device and then call class_device_create to create the corresponding device for each device. The general usage is as follows:
Struct class * myclass = class_create (this_module, "my_device_driver ");
Class_device_create (myclass, null, mkdev (major_num, 0), null, "my_device ");
When such a module is loaded,Udev daemon will automatically create the my_device file under/dev.
Class_create ()
-------------------------------------------------
Linux-2.6.22/include/Linux/device. h
Struct class * class_create (struct module * owner, const char * name)
Class_create-create a struct Class Structure
@ Owner: pointer to the module that is to "own" This struct class
@ Name: pointer to a string for the name of this class.
Create a class directory under/sys/class/
Class_device_create ()
-------------------------------------------------
Linux-2.6.22/include/Linux/device. h
Struct class_device * class_device_create (struct
Class * CLs,
Struct class_device * parent,
Dev_t devt,
Struct device * device,
Const char * FMT ,...)
Class_device_create-creates a Class Device and registers it with sysfs
@ CLS: pointer to the struct class that this device shoshould be registered.
@ Parent: pointer to the parent struct class_device of this new device, if any.
@ Devt: The dev_t for the char device to be added.
@ Device: a pointer to a struct device that is assiociated with this class device.
@ FMT: string for the class device's name