binding.txt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. Driver Binding
  2. Driver binding is the process of associating a device with a device
  3. driver that can control it. Bus drivers have typically handled this
  4. because there have been bus-specific structures to represent the
  5. devices and the drivers. With generic device and device driver
  6. structures, most of the binding can take place using common code.
  7. Bus
  8. ~~~
  9. The bus type structure contains a list of all devices that are on that bus
  10. type in the system. When device_register is called for a device, it is
  11. inserted into the end of this list. The bus object also contains a
  12. list of all drivers of that bus type. When driver_register is called
  13. for a driver, it is inserted at the end of this list. These are the
  14. two events which trigger driver binding.
  15. device_register
  16. ~~~~~~~~~~~~~~~
  17. When a new device is added, the bus's list of drivers is iterated over
  18. to find one that supports it. In order to determine that, the device
  19. ID of the device must match one of the device IDs that the driver
  20. supports. The format and semantics for comparing IDs is bus-specific.
  21. Instead of trying to derive a complex state machine and matching
  22. algorithm, it is up to the bus driver to provide a callback to compare
  23. a device against the IDs of a driver. The bus returns 1 if a match was
  24. found; 0 otherwise.
  25. int match(struct device * dev, struct device_driver * drv);
  26. If a match is found, the device's driver field is set to the driver
  27. and the driver's probe callback is called. This gives the driver a
  28. chance to verify that it really does support the hardware, and that
  29. it's in a working state.
  30. Device Class
  31. ~~~~~~~~~~~~
  32. Upon the successful completion of probe, the device is registered with
  33. the class to which it belongs. Device drivers belong to one and only one
  34. class, and that is set in the driver's devclass field.
  35. devclass_add_device is called to enumerate the device within the class
  36. and actually register it with the class, which happens with the
  37. class's register_dev callback.
  38. NOTE: The device class structures and core routines to manipulate them
  39. are not in the mainline kernel, so the discussion is still a bit
  40. speculative.
  41. Driver
  42. ~~~~~~
  43. When a driver is attached to a device, the device is inserted into the
  44. driver's list of devices.
  45. sysfs
  46. ~~~~~
  47. A symlink is created in the bus's 'devices' directory that points to
  48. the device's directory in the physical hierarchy.
  49. A symlink is created in the driver's 'devices' directory that points
  50. to the device's directory in the physical hierarchy.
  51. A directory for the device is created in the class's directory. A
  52. symlink is created in that directory that points to the device's
  53. physical location in the sysfs tree.
  54. A symlink can be created (though this isn't done yet) in the device's
  55. physical directory to either its class directory, or the class's
  56. top-level directory. One can also be created to point to its driver's
  57. directory also.
  58. driver_register
  59. ~~~~~~~~~~~~~~~
  60. The process is almost identical for when a new driver is added.
  61. The bus's list of devices is iterated over to find a match. Devices
  62. that already have a driver are skipped. All the devices are iterated
  63. over, to bind as many devices as possible to the driver.
  64. Removal
  65. ~~~~~~~
  66. When a device is removed, the reference count for it will eventually
  67. go to 0. When it does, the remove callback of the driver is called. It
  68. is removed from the driver's list of devices and the reference count
  69. of the driver is decremented. All symlinks between the two are removed.
  70. When a driver is removed, the list of devices that it supports is
  71. iterated over, and the driver's remove callback is called for each
  72. one. The device is removed from that list and the symlinks removed.