porting.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. Porting Drivers to the New Driver Model
  2. Patrick Mochel
  3. 7 January 2003
  4. Overview
  5. Please refer to Documentation/driver-model/*.txt for definitions of
  6. various driver types and concepts.
  7. Most of the work of porting devices drivers to the new model happens
  8. at the bus driver layer. This was intentional, to minimize the
  9. negative effect on kernel drivers, and to allow a gradual transition
  10. of bus drivers.
  11. In a nutshell, the driver model consists of a set of objects that can
  12. be embedded in larger, bus-specific objects. Fields in these generic
  13. objects can replace fields in the bus-specific objects.
  14. The generic objects must be registered with the driver model core. By
  15. doing so, they will exported via the sysfs filesystem. sysfs can be
  16. mounted by doing
  17. # mount -t sysfs sysfs /sys
  18. The Process
  19. Step 0: Read include/linux/device.h for object and function definitions.
  20. Step 1: Registering the bus driver.
  21. - Define a struct bus_type for the bus driver.
  22. struct bus_type pci_bus_type = {
  23. .name = "pci",
  24. };
  25. - Register the bus type.
  26. This should be done in the initialization function for the bus type,
  27. which is usually the module_init(), or equivalent, function.
  28. static int __init pci_driver_init(void)
  29. {
  30. return bus_register(&pci_bus_type);
  31. }
  32. subsys_initcall(pci_driver_init);
  33. The bus type may be unregistered (if the bus driver may be compiled
  34. as a module) by doing:
  35. bus_unregister(&pci_bus_type);
  36. - Export the bus type for others to use.
  37. Other code may wish to reference the bus type, so declare it in a
  38. shared header file and export the symbol.
  39. From include/linux/pci.h:
  40. extern struct bus_type pci_bus_type;
  41. From file the above code appears in:
  42. EXPORT_SYMBOL(pci_bus_type);
  43. - This will cause the bus to show up in /sys/bus/pci/ with two
  44. subdirectories: 'devices' and 'drivers'.
  45. # tree -d /sys/bus/pci/
  46. /sys/bus/pci/
  47. |-- devices
  48. `-- drivers
  49. Step 2: Registering Devices.
  50. struct device represents a single device. It mainly contains metadata
  51. describing the relationship the device has to other entities.
  52. - Embed a struct device in the bus-specific device type.
  53. struct pci_dev {
  54. ...
  55. struct device dev; /* Generic device interface */
  56. ...
  57. };
  58. It is recommended that the generic device not be the first item in
  59. the struct to discourage programmers from doing mindless casts
  60. between the object types. Instead macros, or inline functions,
  61. should be created to convert from the generic object type.
  62. #define to_pci_dev(n) container_of(n, struct pci_dev, dev)
  63. or
  64. static inline struct pci_dev * to_pci_dev(struct kobject * kobj)
  65. {
  66. return container_of(n, struct pci_dev, dev);
  67. }
  68. This allows the compiler to verify type-safety of the operations
  69. that are performed (which is Good).
  70. - Initialize the device on registration.
  71. When devices are discovered or registered with the bus type, the
  72. bus driver should initialize the generic device. The most important
  73. things to initialize are the bus_id, parent, and bus fields.
  74. The bus_id is an ASCII string that contains the device's address on
  75. the bus. The format of this string is bus-specific. This is
  76. necessary for representing devices in sysfs.
  77. parent is the physical parent of the device. It is important that
  78. the bus driver sets this field correctly.
  79. The driver model maintains an ordered list of devices that it uses
  80. for power management. This list must be in order to guarantee that
  81. devices are shutdown before their physical parents, and vice versa.
  82. The order of this list is determined by the parent of registered
  83. devices.
  84. Also, the location of the device's sysfs directory depends on a
  85. device's parent. sysfs exports a directory structure that mirrors
  86. the device hierarchy. Accurately setting the parent guarantees that
  87. sysfs will accurately represent the hierarchy.
  88. The device's bus field is a pointer to the bus type the device
  89. belongs to. This should be set to the bus_type that was declared
  90. and initialized before.
  91. Optionally, the bus driver may set the device's name and release
  92. fields.
  93. The name field is an ASCII string describing the device, like
  94. "ATI Technologies Inc Radeon QD"
  95. The release field is a callback that the driver model core calls
  96. when the device has been removed, and all references to it have
  97. been released. More on this in a moment.
  98. - Register the device.
  99. Once the generic device has been initialized, it can be registered
  100. with the driver model core by doing:
  101. device_register(&dev->dev);
  102. It can later be unregistered by doing:
  103. device_unregister(&dev->dev);
  104. This should happen on buses that support hotpluggable devices.
  105. If a bus driver unregisters a device, it should not immediately free
  106. it. It should instead wait for the driver model core to call the
  107. device's release method, then free the bus-specific object.
  108. (There may be other code that is currently referencing the device
  109. structure, and it would be rude to free the device while that is
  110. happening).
  111. When the device is registered, a directory in sysfs is created.
  112. The PCI tree in sysfs looks like:
  113. /sys/devices/pci0/
  114. |-- 00:00.0
  115. |-- 00:01.0
  116. | `-- 01:00.0
  117. |-- 00:02.0
  118. | `-- 02:1f.0
  119. | `-- 03:00.0
  120. |-- 00:1e.0
  121. | `-- 04:04.0
  122. |-- 00:1f.0
  123. |-- 00:1f.1
  124. | |-- ide0
  125. | | |-- 0.0
  126. | | `-- 0.1
  127. | `-- ide1
  128. | `-- 1.0
  129. |-- 00:1f.2
  130. |-- 00:1f.3
  131. `-- 00:1f.5
  132. Also, symlinks are created in the bus's 'devices' directory
  133. that point to the device's directory in the physical hierarchy.
  134. /sys/bus/pci/devices/
  135. |-- 00:00.0 -> ../../../devices/pci0/00:00.0
  136. |-- 00:01.0 -> ../../../devices/pci0/00:01.0
  137. |-- 00:02.0 -> ../../../devices/pci0/00:02.0
  138. |-- 00:1e.0 -> ../../../devices/pci0/00:1e.0
  139. |-- 00:1f.0 -> ../../../devices/pci0/00:1f.0
  140. |-- 00:1f.1 -> ../../../devices/pci0/00:1f.1
  141. |-- 00:1f.2 -> ../../../devices/pci0/00:1f.2
  142. |-- 00:1f.3 -> ../../../devices/pci0/00:1f.3
  143. |-- 00:1f.5 -> ../../../devices/pci0/00:1f.5
  144. |-- 01:00.0 -> ../../../devices/pci0/00:01.0/01:00.0
  145. |-- 02:1f.0 -> ../../../devices/pci0/00:02.0/02:1f.0
  146. |-- 03:00.0 -> ../../../devices/pci0/00:02.0/02:1f.0/03:00.0
  147. `-- 04:04.0 -> ../../../devices/pci0/00:1e.0/04:04.0
  148. Step 3: Registering Drivers.
  149. struct device_driver is a simple driver structure that contains a set
  150. of operations that the driver model core may call.
  151. - Embed a struct device_driver in the bus-specific driver.
  152. Just like with devices, do something like:
  153. struct pci_driver {
  154. ...
  155. struct device_driver driver;
  156. };
  157. - Initialize the generic driver structure.
  158. When the driver registers with the bus (e.g. doing pci_register_driver()),
  159. initialize the necessary fields of the driver: the name and bus
  160. fields.
  161. - Register the driver.
  162. After the generic driver has been initialized, call
  163. driver_register(&drv->driver);
  164. to register the driver with the core.
  165. When the driver is unregistered from the bus, unregister it from the
  166. core by doing:
  167. driver_unregister(&drv->driver);
  168. Note that this will block until all references to the driver have
  169. gone away. Normally, there will not be any.
  170. - Sysfs representation.
  171. Drivers are exported via sysfs in their bus's 'driver's directory.
  172. For example:
  173. /sys/bus/pci/drivers/
  174. |-- 3c59x
  175. |-- Ensoniq AudioPCI
  176. |-- agpgart-amdk7
  177. |-- e100
  178. `-- serial
  179. Step 4: Define Generic Methods for Drivers.
  180. struct device_driver defines a set of operations that the driver model
  181. core calls. Most of these operations are probably similar to
  182. operations the bus already defines for drivers, but taking different
  183. parameters.
  184. It would be difficult and tedious to force every driver on a bus to
  185. simultaneously convert their drivers to generic format. Instead, the
  186. bus driver should define single instances of the generic methods that
  187. forward call to the bus-specific drivers. For instance:
  188. static int pci_device_remove(struct device * dev)
  189. {
  190. struct pci_dev * pci_dev = to_pci_dev(dev);
  191. struct pci_driver * drv = pci_dev->driver;
  192. if (drv) {
  193. if (drv->remove)
  194. drv->remove(pci_dev);
  195. pci_dev->driver = NULL;
  196. }
  197. return 0;
  198. }
  199. The generic driver should be initialized with these methods before it
  200. is registered.
  201. /* initialize common driver fields */
  202. drv->driver.name = drv->name;
  203. drv->driver.bus = &pci_bus_type;
  204. drv->driver.probe = pci_device_probe;
  205. drv->driver.resume = pci_device_resume;
  206. drv->driver.suspend = pci_device_suspend;
  207. drv->driver.remove = pci_device_remove;
  208. /* register with core */
  209. driver_register(&drv->driver);
  210. Ideally, the bus should only initialize the fields if they are not
  211. already set. This allows the drivers to implement their own generic
  212. methods.
  213. Step 5: Support generic driver binding.
  214. The model assumes that a device or driver can be dynamically
  215. registered with the bus at any time. When registration happens,
  216. devices must be bound to a driver, or drivers must be bound to all
  217. devices that it supports.
  218. A driver typically contains a list of device IDs that it supports. The
  219. bus driver compares these IDs to the IDs of devices registered with it.
  220. The format of the device IDs, and the semantics for comparing them are
  221. bus-specific, so the generic model does attempt to generalize them.
  222. Instead, a bus may supply a method in struct bus_type that does the
  223. comparison:
  224. int (*match)(struct device * dev, struct device_driver * drv);
  225. match should return positive value if the driver supports the device,
  226. and zero otherwise. It may also return error code (for example
  227. -EPROBE_DEFER) if determining that given driver supports the device is
  228. not possible.
  229. When a device is registered, the bus's list of drivers is iterated
  230. over. bus->match() is called for each one until a match is found.
  231. When a driver is registered, the bus's list of devices is iterated
  232. over. bus->match() is called for each device that is not already
  233. claimed by a driver.
  234. When a device is successfully bound to a driver, device->driver is
  235. set, the device is added to a per-driver list of devices, and a
  236. symlink is created in the driver's sysfs directory that points to the
  237. device's physical directory:
  238. /sys/bus/pci/drivers/
  239. |-- 3c59x
  240. | `-- 00:0b.0 -> ../../../../devices/pci0/00:0b.0
  241. |-- Ensoniq AudioPCI
  242. |-- agpgart-amdk7
  243. | `-- 00:00.0 -> ../../../../devices/pci0/00:00.0
  244. |-- e100
  245. | `-- 00:0c.0 -> ../../../../devices/pci0/00:0c.0
  246. `-- serial
  247. This driver binding should replace the existing driver binding
  248. mechanism the bus currently uses.
  249. Step 6: Supply a hotplug callback.
  250. Whenever a device is registered with the driver model core, the
  251. userspace program /sbin/hotplug is called to notify userspace.
  252. Users can define actions to perform when a device is inserted or
  253. removed.
  254. The driver model core passes several arguments to userspace via
  255. environment variables, including
  256. - ACTION: set to 'add' or 'remove'
  257. - DEVPATH: set to the device's physical path in sysfs.
  258. A bus driver may also supply additional parameters for userspace to
  259. consume. To do this, a bus must implement the 'hotplug' method in
  260. struct bus_type:
  261. int (*hotplug) (struct device *dev, char **envp,
  262. int num_envp, char *buffer, int buffer_size);
  263. This is called immediately before /sbin/hotplug is executed.
  264. Step 7: Cleaning up the bus driver.
  265. The generic bus, device, and driver structures provide several fields
  266. that can replace those defined privately to the bus driver.
  267. - Device list.
  268. struct bus_type contains a list of all devices registered with the bus
  269. type. This includes all devices on all instances of that bus type.
  270. An internal list that the bus uses may be removed, in favor of using
  271. this one.
  272. The core provides an iterator to access these devices.
  273. int bus_for_each_dev(struct bus_type * bus, struct device * start,
  274. void * data, int (*fn)(struct device *, void *));
  275. - Driver list.
  276. struct bus_type also contains a list of all drivers registered with
  277. it. An internal list of drivers that the bus driver maintains may
  278. be removed in favor of using the generic one.
  279. The drivers may be iterated over, like devices:
  280. int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
  281. void * data, int (*fn)(struct device_driver *, void *));
  282. Please see drivers/base/bus.c for more information.
  283. - rwsem
  284. struct bus_type contains an rwsem that protects all core accesses to
  285. the device and driver lists. This can be used by the bus driver
  286. internally, and should be used when accessing the device or driver
  287. lists the bus maintains.
  288. - Device and driver fields.
  289. Some of the fields in struct device and struct device_driver duplicate
  290. fields in the bus-specific representations of these objects. Feel free
  291. to remove the bus-specific ones and favor the generic ones. Note
  292. though, that this will likely mean fixing up all the drivers that
  293. reference the bus-specific fields (though those should all be 1-line
  294. changes).