driver.txt 7.9 KB

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