PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Documentation/driver-api/driver-model/driver.rst

https://github.com/kvaneesh/linux
ReStructuredText | 286 lines | 213 code | 73 blank | 0 comment | 0 complexity | 2b2078e520b0c94eb6e2381ea6c82a46 MD5 | raw file
  1. ==============
  2. Device Drivers
  3. ==============
  4. See the kerneldoc for the struct device_driver.
  5. Allocation
  6. ~~~~~~~~~~
  7. Device drivers are statically allocated structures. Though there may
  8. be multiple devices in a system that a driver supports, struct
  9. device_driver represents the driver as a whole (not a particular
  10. device instance).
  11. Initialization
  12. ~~~~~~~~~~~~~~
  13. The driver must initialize at least the name and bus fields. It should
  14. also initialize the devclass field (when it arrives), so it may obtain
  15. the proper linkage internally. It should also initialize as many of
  16. the callbacks as possible, though each is optional.
  17. Declaration
  18. ~~~~~~~~~~~
  19. As stated above, struct device_driver objects are statically
  20. allocated. Below is an example declaration of the eepro100
  21. driver. This declaration is hypothetical only; it relies on the driver
  22. being converted completely to the new model::
  23. static struct device_driver eepro100_driver = {
  24. .name = "eepro100",
  25. .bus = &pci_bus_type,
  26. .probe = eepro100_probe,
  27. .remove = eepro100_remove,
  28. .suspend = eepro100_suspend,
  29. .resume = eepro100_resume,
  30. };
  31. Most drivers will not be able to be converted completely to the new
  32. model because the bus they belong to has a bus-specific structure with
  33. bus-specific fields that cannot be generalized.
  34. The most common example of this are device ID structures. A driver
  35. typically defines an array of device IDs that it supports. The format
  36. of these structures and the semantics for comparing device IDs are
  37. completely bus-specific. Defining them as bus-specific entities would
  38. sacrifice type-safety, so we keep bus-specific structures around.
  39. Bus-specific drivers should include a generic struct device_driver in
  40. the definition of the bus-specific driver. Like this::
  41. struct pci_driver {
  42. const struct pci_device_id *id_table;
  43. struct device_driver driver;
  44. };
  45. A definition that included bus-specific fields would look like
  46. (using the eepro100 driver again)::
  47. static struct pci_driver eepro100_driver = {
  48. .id_table = eepro100_pci_tbl,
  49. .driver = {
  50. .name = "eepro100",
  51. .bus = &pci_bus_type,
  52. .probe = eepro100_probe,
  53. .remove = eepro100_remove,
  54. .suspend = eepro100_suspend,
  55. .resume = eepro100_resume,
  56. },
  57. };
  58. Some may find the syntax of embedded struct initialization awkward or
  59. even a bit ugly. So far, it's the best way we've found to do what we want...
  60. Registration
  61. ~~~~~~~~~~~~
  62. ::
  63. int driver_register(struct device_driver *drv);
  64. The driver registers the structure on startup. For drivers that have
  65. no bus-specific fields (i.e. don't have a bus-specific driver
  66. structure), they would use driver_register and pass a pointer to their
  67. struct device_driver object.
  68. Most drivers, however, will have a bus-specific structure and will
  69. need to register with the bus using something like pci_driver_register.
  70. It is important that drivers register their driver structure as early as
  71. possible. Registration with the core initializes several fields in the
  72. struct device_driver object, including the reference count and the
  73. lock. These fields are assumed to be valid at all times and may be
  74. used by the device model core or the bus driver.
  75. Transition Bus Drivers
  76. ~~~~~~~~~~~~~~~~~~~~~~
  77. By defining wrapper functions, the transition to the new model can be
  78. made easier. Drivers can ignore the generic structure altogether and
  79. let the bus wrapper fill in the fields. For the callbacks, the bus can
  80. define generic callbacks that forward the call to the bus-specific
  81. callbacks of the drivers.
  82. This solution is intended to be only temporary. In order to get class
  83. information in the driver, the drivers must be modified anyway. Since
  84. converting drivers to the new model should reduce some infrastructural
  85. complexity and code size, it is recommended that they are converted as
  86. class information is added.
  87. Access
  88. ~~~~~~
  89. Once the object has been registered, it may access the common fields of
  90. the object, like the lock and the list of devices::
  91. int driver_for_each_dev(struct device_driver *drv, void *data,
  92. int (*callback)(struct device *dev, void *data));
  93. The devices field is a list of all the devices that have been bound to
  94. the driver. The LDM core provides a helper function to operate on all
  95. the devices a driver controls. This helper locks the driver on each
  96. node access, and does proper reference counting on each device as it
  97. accesses it.
  98. sysfs
  99. ~~~~~
  100. When a driver is registered, a sysfs directory is created in its
  101. bus's directory. In this directory, the driver can export an interface
  102. to userspace to control operation of the driver on a global basis;
  103. e.g. toggling debugging output in the driver.
  104. A future feature of this directory will be a 'devices' directory. This
  105. directory will contain symlinks to the directories of devices it
  106. supports.
  107. Callbacks
  108. ~~~~~~~~~
  109. ::
  110. int (*probe) (struct device *dev);
  111. The probe() entry is called in task context, with the bus's rwsem locked
  112. and the driver partially bound to the device. Drivers commonly use
  113. container_of() to convert "dev" to a bus-specific type, both in probe()
  114. and other routines. That type often provides device resource data, such
  115. as pci_dev.resource[] or platform_device.resources, which is used in
  116. addition to dev->platform_data to initialize the driver.
  117. This callback holds the driver-specific logic to bind the driver to a
  118. given device. That includes verifying that the device is present, that
  119. it's a version the driver can handle, that driver data structures can
  120. be allocated and initialized, and that any hardware can be initialized.
  121. Drivers often store a pointer to their state with dev_set_drvdata().
  122. When the driver has successfully bound itself to that device, then probe()
  123. returns zero and the driver model code will finish its part of binding
  124. the driver to that device.
  125. A driver's probe() may return a negative errno value to indicate that
  126. the driver did not bind to this device, in which case it should have
  127. released all resources it allocated.
  128. Optionally, probe() may return -EPROBE_DEFER if the driver depends on
  129. resources that are not yet available (e.g., supplied by a driver that
  130. hasn't initialized yet). The driver core will put the device onto the
  131. deferred probe list and will try to call it again later. If a driver
  132. must defer, it should return -EPROBE_DEFER as early as possible to
  133. reduce the amount of time spent on setup work that will need to be
  134. unwound and reexecuted at a later time.
  135. .. warning::
  136. -EPROBE_DEFER must not be returned if probe() has already created
  137. child devices, even if those child devices are removed again
  138. in a cleanup path. If -EPROBE_DEFER is returned after a child
  139. device has been registered, it may result in an infinite loop of
  140. .probe() calls to the same driver.
  141. ::
  142. void (*sync_state) (struct device *dev);
  143. sync_state is called only once for a device. It's called when all the consumer
  144. devices of the device have successfully probed. The list of consumers of the
  145. device is obtained by looking at the device links connecting that device to its
  146. consumer devices.
  147. The first attempt to call sync_state() is made during late_initcall_sync() to
  148. give firmware and drivers time to link devices to each other. During the first
  149. attempt at calling sync_state(), if all the consumers of the device at that
  150. point in time have already probed successfully, sync_state() is called right
  151. away. If there are no consumers of the device during the first attempt, that
  152. too is considered as "all consumers of the device have probed" and sync_state()
  153. is called right away.
  154. If during the first attempt at calling sync_state() for a device, there are
  155. still consumers that haven't probed successfully, the sync_state() call is
  156. postponed and reattempted in the future only when one or more consumers of the
  157. device probe successfully. If during the reattempt, the driver core finds that
  158. there are one or more consumers of the device that haven't probed yet, then
  159. sync_state() call is postponed again.
  160. A typical use case for sync_state() is to have the kernel cleanly take over
  161. management of devices from the bootloader. For example, if a device is left on
  162. and at a particular hardware configuration by the bootloader, the device's
  163. driver might need to keep the device in the boot configuration until all the
  164. consumers of the device have probed. Once all the consumers of the device have
  165. probed, the device's driver can synchronize the hardware state of the device to
  166. match the aggregated software state requested by all the consumers. Hence the
  167. name sync_state().
  168. While obvious examples of resources that can benefit from sync_state() include
  169. resources such as regulator, sync_state() can also be useful for complex
  170. resources like IOMMUs. For example, IOMMUs with multiple consumers (devices
  171. whose addresses are remapped by the IOMMU) might need to keep their mappings
  172. fixed at (or additive to) the boot configuration until all its consumers have
  173. probed.
  174. While the typical use case for sync_state() is to have the kernel cleanly take
  175. over management of devices from the bootloader, the usage of sync_state() is
  176. not restricted to that. Use it whenever it makes sense to take an action after
  177. all the consumers of a device have probed::
  178. int (*remove) (struct device *dev);
  179. remove is called to unbind a driver from a device. This may be
  180. called if a device is physically removed from the system, if the
  181. driver module is being unloaded, during a reboot sequence, or
  182. in other cases.
  183. It is up to the driver to determine if the device is present or
  184. not. It should free any resources allocated specifically for the
  185. device; i.e. anything in the device's driver_data field.
  186. If the device is still present, it should quiesce the device and place
  187. it into a supported low-power state.
  188. ::
  189. int (*suspend) (struct device *dev, pm_message_t state);
  190. suspend is called to put the device in a low power state.
  191. ::
  192. int (*resume) (struct device *dev);
  193. Resume is used to bring a device back from a low power state.
  194. Attributes
  195. ~~~~~~~~~~
  196. ::
  197. struct driver_attribute {
  198. struct attribute attr;
  199. ssize_t (*show)(struct device_driver *driver, char *buf);
  200. ssize_t (*store)(struct device_driver *, const char *buf, size_t count);
  201. };
  202. Device drivers can export attributes via their sysfs directories.
  203. Drivers can declare attributes using a DRIVER_ATTR_RW and DRIVER_ATTR_RO
  204. macro that works identically to the DEVICE_ATTR_RW and DEVICE_ATTR_RO
  205. macros.
  206. Example::
  207. DRIVER_ATTR_RW(debug);
  208. This is equivalent to declaring::
  209. struct driver_attribute driver_attr_debug;
  210. This can then be used to add and remove the attribute from the
  211. driver's directory using::
  212. int driver_create_file(struct device_driver *, const struct driver_attribute *);
  213. void driver_remove_file(struct device_driver *, const struct driver_attribute *);