driver.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. Device Drivers
  2. struct device_driver {
  3. char * name;
  4. struct bus_type * bus;
  5. rwlock_t lock;
  6. atomic_t refcount;
  7. list_t bus_list;
  8. list_t devices;
  9. struct driver_dir_entry dir;
  10. int (*probe) (struct device * dev);
  11. int (*remove) (struct device * dev);
  12. int (*suspend) (struct device * dev, pm_message_t state, u32 level);
  13. int (*resume) (struct device * dev, u32 level);
  14. void (*release) (struct device_driver * drv);
  15. };
  16. Allocation
  17. ~~~~~~~~~~
  18. Device drivers are statically allocated structures. Though there may
  19. be multiple devices in a system that a driver supports, struct
  20. device_driver represents the driver as a whole (not a particular
  21. device instance).
  22. Initialization
  23. ~~~~~~~~~~~~~~
  24. The driver must initialize at least the name and bus fields. It should
  25. also initialize the devclass field (when it arrives), so it may obtain
  26. the proper linkage internally. It should also initialize as many of
  27. the callbacks as possible, though each is optional.
  28. Declaration
  29. ~~~~~~~~~~~
  30. As stated above, struct device_driver objects are statically
  31. allocated. Below is an example declaration of the eepro100
  32. driver. This declaration is hypothetical only; it relies on the driver
  33. being converted completely to the new model.
  34. static struct device_driver eepro100_driver = {
  35. .name = "eepro100",
  36. .bus = &pci_bus_type,
  37. .devclass = &ethernet_devclass, /* when it's implemented */
  38. .probe = eepro100_probe,
  39. .remove = eepro100_remove,
  40. .suspend = eepro100_suspend,
  41. .resume = eepro100_resume,
  42. };
  43. Most drivers will not be able to be converted completely to the new
  44. model because the bus they belong to has a bus-specific structure with
  45. bus-specific fields that cannot be generalized.
  46. The most common example of this are device ID structures. A driver
  47. typically defines an array of device IDs that it supports. The format
  48. of these structures and the semantics for comparing device IDs are
  49. completely bus-specific. Defining them as bus-specific entities would
  50. sacrifice type-safety, so we keep bus-specific structures around.
  51. Bus-specific drivers should include a generic struct device_driver in
  52. the definition of the bus-specific driver. Like this:
  53. struct pci_driver {
  54. const struct pci_device_id *id_table;
  55. struct device_driver driver;
  56. };
  57. A definition that included bus-specific fields would look like
  58. (using the eepro100 driver again):
  59. static struct pci_driver eepro100_driver = {
  60. .id_table = eepro100_pci_tbl,
  61. .driver = {
  62. .name = "eepro100",
  63. .bus = &pci_bus_type,
  64. .devclass = &ethernet_devclass, /* when it's implemented */
  65. .probe = eepro100_probe,
  66. .remove = eepro100_remove,
  67. .suspend = eepro100_suspend,
  68. .resume = eepro100_resume,
  69. },
  70. };
  71. Some may find the syntax of embedded struct initialization awkward or
  72. even a bit ugly. So far, it's the best way we've found to do what we want...
  73. Registration
  74. ~~~~~~~~~~~~
  75. int driver_register(struct device_driver * drv);
  76. The driver registers the structure on startup. For drivers that have
  77. no bus-specific fields (i.e. don't have a bus-specific driver
  78. structure), they would use driver_register and pass a pointer to their
  79. struct device_driver object.
  80. Most drivers, however, will have a bus-specific structure and will
  81. need to register with the bus using something like pci_driver_register.
  82. It is important that drivers register their driver structure as early as
  83. possible. Registration with the core initializes several fields in the
  84. struct device_driver object, including the reference count and the
  85. lock. These fields are assumed to be valid at all times and may be
  86. used by the device model core or the bus driver.
  87. Transition Bus Drivers
  88. ~~~~~~~~~~~~~~~~~~~~~~
  89. By defining wrapper functions, the transition to the new model can be
  90. made easier. Drivers can ignore the generic structure altogether and
  91. let the bus wrapper fill in the fields. For the callbacks, the bus can
  92. define generic callbacks that forward the call to the bus-specific
  93. callbacks of the drivers.
  94. This solution is intended to be only temporary. In order to get class
  95. information in the driver, the drivers must be modified anyway. Since
  96. converting drivers to the new model should reduce some infrastructural
  97. complexity and code size, it is recommended that they are converted as
  98. class information is added.
  99. Access
  100. ~~~~~~
  101. Once the object has been registered, it may access the common fields of
  102. the object, like the lock and the list of devices.
  103. int driver_for_each_dev(struct device_driver * drv, void * data,
  104. int (*callback)(struct device * dev, void * data));
  105. The devices field is a list of all the devices that have been bound to
  106. the driver. The LDM core provides a helper function to operate on all
  107. the devices a driver controls. This helper locks the driver on each
  108. node access, and does proper reference counting on each device as it
  109. accesses it.
  110. sysfs
  111. ~~~~~
  112. When a driver is registered, a sysfs directory is created in its
  113. bus's directory. In this directory, the driver can export an interface
  114. to userspace to control operation of the driver on a global basis;
  115. e.g. toggling debugging output in the driver.
  116. A future feature of this directory will be a 'devices' directory. This
  117. directory will contain symlinks to the directories of devices it
  118. supports.
  119. Callbacks
  120. ~~~~~~~~~
  121. int (*probe) (struct device * dev);
  122. probe is called to verify the existence of a certain type of
  123. hardware. This is called during the driver binding process, after the
  124. bus has verified that the device ID of a device matches one of the
  125. device IDs supported by the driver.
  126. This callback only verifies that there actually is supported hardware
  127. present. It may allocate a driver-specific structure, but it should
  128. not do any initialization of the hardware itself. The device-specific
  129. structure may be stored in the device's driver_data field.
  130. int (*init) (struct device * dev);
  131. init is called during the binding stage. It is called after probe has
  132. successfully returned and the device has been registered with its
  133. class. It is responsible for initializing the hardware.
  134. int (*remove) (struct device * dev);
  135. remove is called to dissociate a driver with a device. This may be
  136. called if a device is physically removed from the system, if the
  137. driver module is being unloaded, or during a reboot sequence.
  138. It is up to the driver to determine if the device is present or
  139. not. It should free any resources allocated specifically for the
  140. device; i.e. anything in the device's driver_data field.
  141. If the device is still present, it should quiesce the device and place
  142. it into a supported low-power state.
  143. int (*suspend) (struct device * dev, pm_message_t state, u32 level);
  144. suspend is called to put the device in a low power state. There are
  145. several stages to successfully suspending a device, which is denoted in
  146. the @level parameter. Breaking the suspend transition into several
  147. stages affords the platform flexibility in performing device power
  148. management based on the requirements of the system and the
  149. user-defined policy.
  150. SUSPEND_NOTIFY notifies the device that a suspend transition is about
  151. to happen. This happens on system power state transitions to verify
  152. that all devices can successfully suspend.
  153. A driver may choose to fail on this call, which should cause the
  154. entire suspend transition to fail. A driver should fail only if it
  155. knows that the device will not be able to be resumed properly when the
  156. system wakes up again. It could also fail if it somehow determines it
  157. is in the middle of an operation too important to stop.
  158. SUSPEND_DISABLE tells the device to stop I/O transactions. When it
  159. stops transactions, or what it should do with unfinished transactions
  160. is a policy of the driver. After this call, the driver should not
  161. accept any other I/O requests.
  162. SUSPEND_SAVE_STATE tells the device to save the context of the
  163. hardware. This includes any bus-specific hardware state and
  164. device-specific hardware state. A pointer to this saved state can be
  165. stored in the device's saved_state field.
  166. SUSPEND_POWER_DOWN tells the driver to place the device in the low
  167. power state requested.
  168. Whether suspend is called with a given level is a policy of the
  169. platform. Some levels may be omitted; drivers must not assume the
  170. reception of any level. However, all levels must be called in the
  171. order above; i.e. notification will always come before disabling;
  172. disabling the device will come before suspending the device.
  173. All calls are made with interrupts enabled, except for the
  174. SUSPEND_POWER_DOWN level.
  175. int (*resume) (struct device * dev, u32 level);
  176. Resume is used to bring a device back from a low power state. Like the
  177. suspend transition, it happens in several stages.
  178. RESUME_POWER_ON tells the driver to set the power state to the state
  179. before the suspend call (The device could have already been in a low
  180. power state before the suspend call to put in a lower power state).
  181. RESUME_RESTORE_STATE tells the driver to restore the state saved by
  182. the SUSPEND_SAVE_STATE suspend call.
  183. RESUME_ENABLE tells the driver to start accepting I/O transactions
  184. again. Depending on driver policy, the device may already have pending
  185. I/O requests.
  186. RESUME_POWER_ON is called with interrupts disabled. The other resume
  187. levels are called with interrupts enabled.
  188. As with the various suspend stages, the driver must not assume that
  189. any other resume calls have been or will be made. Each call should be
  190. self-contained and not dependent on any external state.
  191. Attributes
  192. ~~~~~~~~~~
  193. struct driver_attribute {
  194. struct attribute attr;
  195. ssize_t (*show)(struct device_driver *, char * buf, size_t count, loff_t off);
  196. ssize_t (*store)(struct device_driver *, const char * buf, size_t count, loff_t off);
  197. };
  198. Device drivers can export attributes via their sysfs directories.
  199. Drivers can declare attributes using a DRIVER_ATTR macro that works
  200. identically to the DEVICE_ATTR macro.
  201. Example:
  202. DRIVER_ATTR(debug,0644,show_debug,store_debug);
  203. This is equivalent to declaring:
  204. struct driver_attribute driver_attr_debug;
  205. This can then be used to add and remove the attribute from the
  206. driver's directory using:
  207. int driver_create_file(struct device_driver *, struct driver_attribute *);
  208. void driver_remove_file(struct device_driver *, struct driver_attribute *);