LKMG chardev.c question about register_chrdev function
In register_chrdev function we have to give the device name. Even though we create a device with another name with same major number it works correctly. So what is the significance开发者_JS百科 of giving name in that function.
I am a beginner to this :)
As per the man page for register_chrdev:
The name parameter is a short name for the device
and is displayed in the The /proc/devices list. It also
must exactly match the name passed to unregister_chrdev
function when releasing the functions.
So, the name isn't really used by the kernel at all except as a way for you to later identify the registration so you can undo it, and to have something sensible to call the driver in the /proc devices list.
The reason why having two seperate register_chrdev's with different names and the same major works is that modern Linux kernels allow for multiple drivers to register for the same major number, and basically share it. Presumably both drivers would get all calls for that major and have to decide based on minor number whether to take action or not. I'm not really sure of this, as all drivers I've ever worked on and most drives over all follow a 'one driver per major number' idiom
One last thing, rather than hardcoding a major number, its possible to just pass zero to register_chrdev and have the kernel pick a free major number for you and return it to you. This way you don't have to worry about stepping on other drivers' toes, but you do then need to have your userspace code check /proc/devices in order to mknod the /dev entry correctly.
精彩评论