driver.txt 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. Device Drivers
  2. See the kerneldoc for the struct device_driver.
  3. Allocation
  4. ~~~~~~~~~~
  5. Device drivers are statically allocated structures. Though there may
  6. be multiple devices in a system that a driver supports, struct
  7. device_driver represents the driver as a whole (not a particular
  8. device instance).
  9. Initialization
  10. ~~~~~~~~~~~~~~
  11. The driver must initialize at least the name and bus fields. It should
  12. also initialize the devclass field (when it arrives), so it may obtain
  13. the proper linkage internally. It should also initialize as many of
  14. the callbacks as possible, though each is optional.
  15. Declaration
  16. ~~~~~~~~~~~
  17. As stated above, struct device_driver objects are statically
  18. allocated. Below is an example declaration of the eepro100
  19. driver. This declaration is hypothetical only; it relies on the driver
  20. being converted completely to the new model.
  21. static struct device_driver eepro100_driver = {
  22. .name = "eepro100",
  23. .bus = &pci_bus_type,
  24. .probe = eepro100_probe,
  25. .remove = eepro100_remove,
  26. .suspend = eepro100_suspend,
  27. .resume = eepro100_resume,
  28. };
  29. Most drivers will not be able to be converted completely to the new
  30. model because the bus they belong to has a bus-specific structure with
  31. bus-specific fields that cannot be generalized.
  32. The most common example of this are device ID structures. A driver
  33. typically defines an array of device IDs that it supports. The format
  34. of these structures and the semantics for comparing device IDs are
  35. completely bus-specific. Defining them as bus-specific entities would
  36. sacrifice type-safety, so we keep bus-specific structures around.
  37. Bus-specific drivers should include a generic struct device_driver in
  38. the definition of the bus-specific driver. Like this:
  39. struct pci_driver {
  40. const struct pci_device_id *id_table;
  41. struct device_driver driver;
  42. };
  43. A definition that included bus-specific fields would look like
  44. (using the eepro100 driver again):
  45. static struct pci_driver eepro100_driver = {
  46. .id_table = eepro100_pci_tbl,
  47. .driver = {
  48. .name = "eepro100",
  49. .bus = &pci_bus_type,
  50. .probe = eepro100_probe,
  51. .remove = eepro100_remove,
  52. .suspend = eepro100_suspend,
  53. .resume = eepro100_resume,
  54. },
  55. };
  56. Some may find the syntax of embedded struct initialization awkward or
  57. even a bit ugly. So far, it's the best way we've found to do what we want...
  58. Registration
  59. ~~~~~~~~~~~~
  60. int driver_register(struct device_driver * drv);
  61. The driver registers the structure on startup. For drivers that have
  62. no bus-specific fields (i.e. don't have a bus-specific driver
  63. structure), they would use driver_register and pass a pointer to their
  64. struct device_driver object.
  65. Most drivers, however, will have a bus-specific structure and will
  66. need to register with the bus using something like pci_driver_register.
  67. It is important that drivers register their driver structure as early as
  68. possible. Registration with the core initializes several fields in the
  69. struct device_driver object, including the reference count and the
  70. lock. These fields are assumed to be valid at all times and may be
  71. used by the device model core or the bus driver.
  72. Transition Bus Drivers
  73. ~~~~~~~~~~~~~~~~~~~~~~
  74. By defining wrapper functions, the transition to the new model can be
  75. made easier. Drivers can ignore the generic structure altogether and
  76. let the bus wrapper fill in the fields. For the callbacks, the bus can
  77. define generic callbacks that forward the call to the bus-specific
  78. callbacks of the drivers.
  79. This solution is intended to be only temporary. In order to get class
  80. information in the driver, the drivers must be modified anyway. Since
  81. converting drivers to the new model should reduce some infrastructural
  82. complexity and code size, it is recommended that they are converted as
  83. class information is added.
  84. Access
  85. ~~~~~~
  86. Once the object has been registered, it may access the common fields of
  87. the object, like the lock and the list of devices.
  88. int driver_for_each_dev(struct device_driver * drv, void * data,
  89. int (*callback)(struct device * dev, void * data));
  90. The devices field is a list of all the devices that have been bound to
  91. the driver. The LDM core provides a helper function to operate on all
  92. the devices a driver controls. This helper locks the driver on each
  93. node access, and does proper reference counting on each device as it
  94. accesses it.
  95. sysfs
  96. ~~~~~
  97. When a driver is registered, a sysfs directory is created in its
  98. bus's directory. In this directory, the driver can export an interface
  99. to userspace to control operation of the driver on a global basis;
  100. e.g. toggling debugging output in the driver.
  101. A future feature of this directory will be a 'devices' directory. This
  102. directory will contain symlinks to the directories of devices it
  103. supports.
  104. Callbacks
  105. ~~~~~~~~~
  106. int (*probe) (struct device * dev);
  107. The probe() entry is called in task context, with the bus's rwsem locked
  108. and the driver partially bound to the device. Drivers commonly use
  109. container_of() to convert "dev" to a bus-specific type, both in probe()
  110. and other routines. That type often provides device resource data, such
  111. as pci_dev.resource[] or platform_device.resources, which is used in
  112. addition to dev->platform_data to initialize the driver.
  113. This callback holds the driver-specific logic to bind the driver to a
  114. given device. That includes verifying that the device is present, that
  115. it's a version the driver can handle, that driver data structures can
  116. be allocated and initialized, and that any hardware can be initialized.
  117. Drivers often store a pointer to their state with dev_set_drvdata().
  118. When the driver has successfully bound itself to that device, then probe()
  119. returns zero and the driver model code will finish its part of binding
  120. the driver to that device.
  121. A driver's probe() may return a negative errno value to indicate that
  122. the driver did not bind to this device, in which case it should have
  123. released all resources it allocated.
  124. int (*remove) (struct device * dev);
  125. remove is called to unbind a driver from a device. This may be
  126. called if a device is physically removed from the system, if the
  127. driver module is being unloaded, during a reboot sequence, or
  128. in other cases.
  129. It is up to the driver to determine if the device is present or
  130. not. It should free any resources allocated specifically for the
  131. device; i.e. anything in the device's driver_data field.
  132. If the device is still present, it should quiesce the device and place
  133. it into a supported low-power state.
  134. int (*suspend) (struct device * dev, pm_message_t state);
  135. suspend is called to put the device in a low power state.
  136. int (*resume) (struct device * dev);
  137. Resume is used to bring a device back from a low power state.
  138. Attributes
  139. ~~~~~~~~~~
  140. struct driver_attribute {
  141. struct attribute attr;
  142. ssize_t (*show)(struct device_driver *driver, char *buf);
  143. ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
  144. };
  145. Device drivers can export attributes via their sysfs directories.
  146. Drivers can declare attributes using a DRIVER_ATTR macro that works
  147. identically to the DEVICE_ATTR macro.
  148. Example:
  149. DRIVER_ATTR(debug,0644,show_debug,store_debug);
  150. This is equivalent to declaring:
  151. struct driver_attribute driver_attr_debug;
  152. This can then be used to add and remove the attribute from the
  153. driver's directory using:
  154. int driver_create_file(struct device_driver *, const struct driver_attribute *);
  155. void driver_remove_file(struct device_driver *, const struct driver_attribute *);