Device number:
1, self-allocation of the initiative
Major = Register_chrdev (0, "First_drv", &first_sdv_fops);//Register
When you register your device with the device number 0, the kernel will voluntarily assign a master device number to return.
2. Manual designation
Find the main device numbers under the Proc/devices file. Assuming it's not being used, we can take it and use it.
Application Device Node:
We will use open this function to take a device, then open ("dev/xxx") dev/xxx This device node how to come?
1, with mknod/dev/xxx C main equipment number device number
2. Create your own initiative:
Tips:
Mdev: Creating device nodes based on system information
Method:
Define two variables:
static struct class *firstdrv_class;
static struct class_devices *firstdrv_class_dev;
In the entry function, add:
Firstdrv_class = Class_create (This_module, "first_drv"), if (Is_err (Firstdrv_class)) return Ptr_err (Firstdrv_class); Firstdrv_class_dev = Device_create (Firstdrv_class,null,mkdev (major,0), NULL, "Wq_device"), if (Unlikely (IS_ERR ( Firstdrv_class_dev))) return Ptr_err (Firstdrv_class_dev);
In the Exit function, add:
Device_unregister (Firstdrv_class_dev); Class_destroy (Firstdrv_class);
This will generate a wq_device device in/dev. And will find a wq_device device under the/sys/class/first_drv folder,
There will be a dev file in/sys/class/first_drv/wq_device with the content: "252:0"
Mdev will create device nodes based on this information.
Why does Mdev create device nodes based on this information when the Sys folder is changed?
The reason is that in the RCS file in our/etc/init.d/folder there is something like the following:
Echo/sbin/mdev >/proc/sys/kernel/hotplug
Once the kernel is loaded or unloaded, the application indicated by/proc/sys/kernel/hotplug is invoked.
Linux drive device number and create device node