Search code examples
linuxembeddeddevice-driver

How to associate the device in /dev/ with the actual driver


I am trying to understand how device driver works in linux.

  1. I have a device node as follows (Major number 89, device name i2c-0)

     crw-r--r--    1 0        0         89,   0 Sep 29 01:36 /dev/i2c-0
    
  2. I have the i2c driver with the name i2c.ko and I will do insmod i2c.ko during start up.

  3. And in the driver , following function will be called during initialization:

    register_chrdev(89, "i2c", &i2chtv_fops)<0    // not "i2c-0"
    

My question is : When user call open("/dev/i2c-0", O_RDWR),how the kernel knows which driver to use ? I noticed the device name is i2c-0 but registered device name is i2c. Is it because they are use the same major number that the kernel can use the correct driver?


Solution

  • Yes, major numbers select the driver and minor numbers select "units" (whatever that may be; for the console driver, it's the different screens).

    The -0 you see is the "unit" (in case you have more than a single i2c bus in your system).