porting.rst 13 KB

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