binding.rst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ==============
  2. Driver Binding
  3. ==============
  4. Driver binding is the process of associating a device with a device
  5. driver that can control it. Bus drivers have typically handled this
  6. because there have been bus-specific structures to represent the
  7. devices and the drivers. With generic device and device driver
  8. structures, most of the binding can take place using common code.
  9. Bus
  10. ~~~
  11. The bus type structure contains a list of all devices that are on that bus
  12. type in the system. When device_register is called for a device, it is
  13. inserted into the end of this list. The bus object also contains a
  14. list of all drivers of that bus type. When driver_register is called
  15. for a driver, it is inserted at the end of this list. These are the
  16. two events which trigger driver binding.
  17. device_register
  18. ~~~~~~~~~~~~~~~
  19. When a new device is added, the bus's list of drivers is iterated over
  20. to find one that supports it. In order to determine that, the device
  21. ID of the device must match one of the device IDs that the driver
  22. supports. The format and semantics for comparing IDs is bus-specific.
  23. Instead of trying to derive a complex state machine and matching
  24. algorithm, it is up to the bus driver to provide a callback to compare
  25. a device against the IDs of a driver. The bus returns 1 if a match was
  26. found; 0 otherwise.
  27. int match(struct device * dev, struct device_driver * drv);
  28. If a match is found, the device's driver field is set to the driver
  29. and the driver's probe callback is called. This gives the driver a
  30. chance to verify that it really does support the hardware, and that
  31. it's in a working state.
  32. Device Class
  33. ~~~~~~~~~~~~
  34. Upon the successful completion of probe, the device is registered with
  35. the class to which it belongs. Device drivers belong to one and only one
  36. class, and that is set in the driver's devclass field.
  37. devclass_add_device is called to enumerate the device within the class
  38. and actually register it with the class, which happens with the
  39. class's register_dev callback.
  40. Driver
  41. ~~~~~~
  42. When a driver is attached to a device, the driver's probe() function is
  43. called. Within probe(), the driver initializes the device and allocates
  44. and initializes per-device data structures. This per-device state is
  45. associated with the device object for as long as the driver remains bound
  46. to it. Conceptually, this per-device data together with the binding to
  47. the device can be thought of as an instance of the driver.
  48. sysfs
  49. ~~~~~
  50. A symlink is created in the bus's 'devices' directory that points to
  51. the device's directory in the physical hierarchy.
  52. A symlink is created in the driver's 'devices' directory that points
  53. to the device's directory in the physical hierarchy.
  54. A directory for the device is created in the class's directory. A
  55. symlink is created in that directory that points to the device's
  56. physical location in the sysfs tree.
  57. A symlink can be created (though this isn't done yet) in the device's
  58. physical directory to either its class directory, or the class's
  59. top-level directory. One can also be created to point to its driver's
  60. directory also.
  61. driver_register
  62. ~~~~~~~~~~~~~~~
  63. The process is almost identical for when a new driver is added.
  64. The bus's list of devices is iterated over to find a match. Devices
  65. that already have a driver are skipped. All the devices are iterated
  66. over, to bind as many devices as possible to the driver.
  67. Removal
  68. ~~~~~~~
  69. When a device is removed, the reference count for it will eventually
  70. go to 0. When it does, the remove callback of the driver is called. It
  71. is removed from the driver's list of devices and the reference count
  72. of the driver is decremented. All symlinks between the two are removed.
  73. When a driver is removed, the list of devices that it supports is
  74. iterated over, and the driver's remove callback is called for each
  75. one. The device is removed from that list and the symlinks removed.
  76. Driver Override
  77. ~~~~~~~~~~~~~~~
  78. Userspace may override the standard matching by writing a driver name to
  79. a device's ``driver_override`` sysfs attribute. When set, only a driver
  80. whose name matches the override will be considered during binding. This
  81. bypasses all bus-specific matching (OF, ACPI, ID tables, etc.).
  82. The override may be cleared by writing an empty string, which returns
  83. the device to standard matching rules. Writing to ``driver_override``
  84. does not automatically unbind the device from its current driver or
  85. make any attempt to load the specified driver.
  86. Buses opt into this mechanism by setting the ``driver_override`` flag in
  87. their ``struct bus_type``::
  88. const struct bus_type example_bus_type = {
  89. ...
  90. .driver_override = true,
  91. };
  92. When the flag is set, the driver core automatically creates the
  93. ``driver_override`` sysfs attribute for every device on that bus.
  94. The bus's ``match()`` callback should check the override before performing
  95. its own matching, using ``device_match_driver_override()``::
  96. static int example_match(struct device *dev, const struct device_driver *drv)
  97. {
  98. int ret;
  99. ret = device_match_driver_override(dev, drv);
  100. if (ret >= 0)
  101. return ret;
  102. /* Fall through to bus-specific matching... */
  103. }
  104. ``device_match_driver_override()`` returns > 0 if the override matches
  105. the given driver, 0 if the override is set but does not match, or < 0 if
  106. no override is set at all.
  107. Additional helpers are available:
  108. - ``device_set_driver_override()`` - set or clear the override from kernel code.
  109. - ``device_has_driver_override()`` - check whether an override is set.