usb-info.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. How USB works with driver model
  2. ===============================
  3. Introduction
  4. ------------
  5. Driver model USB support makes use of existing features but changes how
  6. drivers are found. This document provides some information intended to help
  7. understand how things work with USB in U-Boot when driver model is enabled.
  8. Enabling driver model for USB
  9. -----------------------------
  10. A new CONFIG_DM_USB option is provided to enable driver model for USB. This
  11. causes the USB uclass to be included, and drops the equivalent code in
  12. usb.c. In particular the usb_init() function is then implemented by the
  13. uclass.
  14. Support for EHCI and XHCI
  15. -------------------------
  16. So far OHCI is not supported. Both EHCI and XHCI drivers should be declared
  17. as drivers in the USB uclass. For example:
  18. static const struct udevice_id ehci_usb_ids[] = {
  19. { .compatible = "nvidia,tegra20-ehci", .data = USB_CTLR_T20 },
  20. { .compatible = "nvidia,tegra30-ehci", .data = USB_CTLR_T30 },
  21. { .compatible = "nvidia,tegra114-ehci", .data = USB_CTLR_T114 },
  22. { }
  23. };
  24. U_BOOT_DRIVER(usb_ehci) = {
  25. .name = "ehci_tegra",
  26. .id = UCLASS_USB,
  27. .of_match = ehci_usb_ids,
  28. .ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
  29. .probe = tegra_ehci_usb_probe,
  30. .remove = tegra_ehci_usb_remove,
  31. .ops = &ehci_usb_ops,
  32. .platdata_auto_alloc_size = sizeof(struct usb_platdata),
  33. .priv_auto_alloc_size = sizeof(struct fdt_usb),
  34. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  35. };
  36. Here ehci_usb_ids is used to list the controllers that the driver supports.
  37. Each has its own data value. Controllers must be in the UCLASS_USB uclass.
  38. The ofdata_to_platdata() method allows the controller driver to grab any
  39. necessary settings from the device tree.
  40. The ops here are ehci_usb_ops. All EHCI drivers will use these same ops in
  41. most cases, since they are all EHCI-compatible. For EHCI there are also some
  42. special operations that can be overridden when calling ehci_register().
  43. The driver can use priv_auto_alloc_size to set the size of its private data.
  44. This can hold run-time information needed by the driver for operation. It
  45. exists when the device is probed (not when it is bound) and is removed when
  46. the driver is removed.
  47. Note that usb_platdata is currently only used to deal with setting up a bus
  48. in USB device mode (OTG operation). It can be omitted if that is not
  49. supported.
  50. The driver's probe() method should do the basic controller init and then
  51. call ehci_register() to register itself as an EHCI device. It should call
  52. ehci_deregister() in the remove() method. Registering a new EHCI device
  53. does not by itself cause the bus to be scanned.
  54. The old ehci_hcd_init() function is no-longer used. Nor is it necessary to
  55. set up the USB controllers from board init code. When 'usb start' is used,
  56. each controller will be probed and its bus scanned.
  57. XHCI works in a similar way.
  58. Data structures
  59. ---------------
  60. The following primary data structures are in use:
  61. - struct usb_device
  62. This holds information about a device on the bus. All devices have
  63. this structure, even the root hub. The controller itself does not
  64. have this structure. You can access it for a device 'dev' with
  65. dev_get_parent_priv(dev). It matches the old structure except that the
  66. parent and child information is not present (since driver model
  67. handles that). Once the device is set up, you can find the device
  68. descriptor and current configuration descriptor in this structure.
  69. - struct usb_platdata
  70. This holds platform data for a controller. So far this is only used
  71. as a work-around for controllers which can act as USB devices in OTG
  72. mode, since the gadget framework does not use driver model.
  73. - struct usb_dev_platdata
  74. This holds platform data for a device. You can access it for a
  75. device 'dev' with dev_get_parent_platdata(dev). It holds the device
  76. address and speed - anything that can be determined before the device
  77. driver is actually set up. When probing the bus this structure is
  78. used to provide essential information to the device driver.
  79. - struct usb_bus_priv
  80. This is private information for each controller, maintained by the
  81. controller uclass. It is mostly used to keep track of the next
  82. device address to use.
  83. Of these, only struct usb_device was used prior to driver model.
  84. USB buses
  85. ---------
  86. Given a controller, you know the bus - it is the one attached to the
  87. controller. Each controller handles exactly one bus. Every controller has a
  88. root hub attached to it. This hub, which is itself a USB device, can provide
  89. one or more 'ports' to which additional devices can be attached. It is
  90. possible to power up a hub and find out which of its ports have devices
  91. attached.
  92. Devices are given addresses starting at 1. The root hub is always address 1,
  93. and from there the devices are numbered in sequence. The USB uclass takes
  94. care of this numbering automatically during enumeration.
  95. USB devices are enumerated by finding a device on a particular hub, and
  96. setting its address to the next available address. The USB bus stretches out
  97. in a tree structure, potentially with multiple hubs each with several ports
  98. and perhaps other hubs. Some hubs will have their own power since otherwise
  99. the 5V 500mA power supplied by the controller will not be sufficient to run
  100. very many devices.
  101. Enumeration in U-Boot takes a long time since devices are probed one at a
  102. time, and each is given sufficient time to wake up and announce itself. The
  103. timeouts are set for the slowest device.
  104. Up to 127 devices can be on each bus. USB has four bus speeds: low
  105. (1.5Mbps), full (12Mbps), high (480Mbps) which is only available with USB2
  106. and newer (EHCI), and super (5Gbps) which is only available with USB3 and
  107. newer (XHCI). If you connect a super-speed device to a high-speed hub, you
  108. will only get high-speed.
  109. USB operations
  110. --------------
  111. As before driver model, messages can be sent using submit_bulk_msg() and the
  112. like. These are now implemented by the USB uclass and route through the
  113. controller drivers. Note that messages are not sent to the driver of the
  114. device itself - i.e. they don't pass down the stack to the controller.
  115. U-Boot simply finds the controller to which the device is attached, and sends
  116. the message there with an appropriate 'pipe' value so it can be addressed
  117. properly. Having said that, the USB device which should receive the message
  118. is passed in to the driver methods, for use by sandbox. This design decision
  119. is open for review and the code impact of changing it is small since the
  120. methods are typically implemented by the EHCI and XHCI stacks.
  121. Controller drivers (in UCLASS_USB) themselves provide methods for sending
  122. each message type. For XHCI an additional alloc_device() method is provided
  123. since XHCI needs to allocate a device context before it can even read the
  124. device's descriptor.
  125. These methods use a 'pipe' which is a collection of bit fields used to
  126. describe the type of message, direction of transfer and the intended
  127. recipient (device number).
  128. USB Devices
  129. -----------
  130. USB devices are found using a simple algorithm which works through the
  131. available hubs in a depth-first search. Devices can be in any uclass, but
  132. are attached to a parent hub (or controller in the case of the root hub) and
  133. so have parent data attached to them (this is struct usb_device).
  134. By the time the device's probe() method is called, it is enumerated and is
  135. ready to talk to the host.
  136. The enumeration process needs to work out which driver to attach to each USB
  137. device. It does this by examining the device class, interface class, vendor
  138. ID, product ID, etc. See struct usb_driver_entry for how drivers are matched
  139. with USB devices - you can use the USB_DEVICE() macro to declare a USB
  140. driver. For example, usb_storage.c defines a USB_DEVICE() to handle storage
  141. devices, and it will be used for all USB devices which match.
  142. Technical details on enumeration flow
  143. -------------------------------------
  144. It is useful to understand precisely how a USB bus is enumerating to avoid
  145. confusion when dealing with USB devices.
  146. Device initialisation happens roughly like this:
  147. - At some point the 'usb start' command is run
  148. - This calls usb_init() which works through each controller in turn
  149. - The controller is probed(). This does no enumeration.
  150. - Then usb_scan_bus() is called. This calls usb_scan_device() to scan the
  151. (only) device that is attached to the controller - a root hub
  152. - usb_scan_device() sets up a fake struct usb_device and calls
  153. usb_setup_device(), passing the port number to be scanned, in this case port
  154. 0
  155. - usb_setup_device() first calls usb_prepare_device() to set the device
  156. address, then usb_select_config() to select the first configuration
  157. - at this point the device is enumerated but we do not have a real struct
  158. udevice for it. But we do have the descriptor in struct usb_device so we can
  159. use this to figure out what driver to use
  160. - back in usb_scan_device(), we call usb_find_child() to try to find an
  161. existing device which matches the one we just found on the bus. This can
  162. happen if the device is mentioned in the device tree, or if we previously
  163. scanned the bus and so the device was created before
  164. - if usb_find_child() does not find an existing device, we call
  165. usb_find_and_bind_driver() which tries to bind one
  166. - usb_find_and_bind_driver() searches all available USB drivers (declared
  167. with USB_DEVICE()). If it finds a match it binds that driver to create a new
  168. device.
  169. - If it does not, it binds a generic driver. A generic driver is good enough
  170. to allow access to the device (sending it packets, etc.) but all
  171. functionality will need to be implemented outside the driver model.
  172. - in any case, when usb_find_child() and/or usb_find_and_bind_driver() are
  173. done, we have a device with the correct uclass. At this point we want to
  174. probe the device
  175. - first we store basic information about the new device (address, port,
  176. speed) in its parent platform data. We cannot store it its private data
  177. since that will not exist until the device is probed.
  178. - then we call device_probe() which probes the device
  179. - the first probe step is actually the USB controller's (or USB hubs's)
  180. child_pre_probe() method. This gets called before anything else and is
  181. intended to set up a child device ready to be used with its parent bus. For
  182. USB this calls usb_child_pre_probe() which grabs the information that was
  183. stored in the parent platform data and stores it in the parent private data
  184. (which is struct usb_device, a real one this time). It then calls
  185. usb_select_config() again to make sure that everything about the device is
  186. set up
  187. - note that we have called usb_select_config() twice. This is inefficient
  188. but the alternative is to store additional information in the platform data.
  189. The time taken is minimal and this way is simpler
  190. - at this point the device is set up and ready for use so far as the USB
  191. subsystem is concerned
  192. - the device's probe() method is then called. It can send messages and do
  193. whatever else it wants to make the device work.
  194. Note that the first device is always a root hub, and this must be scanned to
  195. find any devices. The above steps will have created a hub (UCLASS_USB_HUB),
  196. given it address 1 and set the configuration.
  197. For hubs, the hub uclass has a post_probe() method. This means that after
  198. any hub is probed, the uclass gets to do some processing. In this case
  199. usb_hub_post_probe() is called, and the following steps take place:
  200. - usb_hub_post_probe() calls usb_hub_scan() to scan the hub, which in turn
  201. calls usb_hub_configure()
  202. - hub power is enabled
  203. - we loop through each port on the hub, performing the same steps for each
  204. - first, check if there is a device present. This happens in
  205. usb_hub_port_connect_change(). If so, then usb_scan_device() is called to
  206. scan the device, passing the appropriate port number.
  207. - you will recognise usb_scan_device() from the steps above. It sets up the
  208. device ready for use. If it is a hub, it will scan that hub before it
  209. continues here (recursively, depth-first)
  210. - once all hub ports are scanned in this way, the hub is ready for use and
  211. all of its downstream devices also
  212. - additional controllers are scanned in the same way
  213. The above method has some nice properties:
  214. - the bus enumeration happens by virtue of driver model's natural device flow
  215. - most logic is in the USB controller and hub uclasses; the actual device
  216. drivers do not need to know they are on a USB bus, at least so far as
  217. enumeration goes
  218. - hub scanning happens automatically after a hub is probed
  219. Hubs
  220. ----
  221. USB hubs are scanned as in the section above. While hubs have their own
  222. uclass, they share some common elements with controllers:
  223. - they both attach private data to their children (struct usb_device,
  224. accessible for a child with dev_get_parent_priv(child))
  225. - they both use usb_child_pre_probe() to set up their children as proper USB
  226. devices
  227. Example - Mass Storage
  228. ----------------------
  229. As an example of a USB device driver, see usb_storage.c. It uses its own
  230. uclass and declares itself as follows:
  231. U_BOOT_DRIVER(usb_mass_storage) = {
  232. .name = "usb_mass_storage",
  233. .id = UCLASS_MASS_STORAGE,
  234. .of_match = usb_mass_storage_ids,
  235. .probe = usb_mass_storage_probe,
  236. };
  237. static const struct usb_device_id mass_storage_id_table[] = {
  238. { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  239. .bInterfaceClass = USB_CLASS_MASS_STORAGE},
  240. { } /* Terminating entry */
  241. };
  242. USB_DEVICE(usb_mass_storage, mass_storage_id_table);
  243. The USB_DEVICE() macro attaches the given table of matching information to
  244. the given driver. Note that the driver is declared in U_BOOT_DRIVER() as
  245. 'usb_mass_storage' and this must match the first parameter of USB_DEVICE.
  246. When usb_find_and_bind_driver() is called on a USB device with the
  247. bInterfaceClass value of USB_CLASS_MASS_STORAGE, it will automatically find
  248. this driver and use it.
  249. Counter-example: USB Ethernet
  250. -----------------------------
  251. As an example of the old way of doing things, see usb_ether.c. When the bus
  252. is scanned, all Ethernet devices will be created as generic USB devices (in
  253. uclass UCLASS_USB_DEV_GENERIC). Then, when the scan is completed,
  254. usb_host_eth_scan() will be called. This looks through all the devices on
  255. each bus and manually figures out which are Ethernet devices in the ways of
  256. yore.
  257. In fact, usb_ether should be moved to driver model. Each USB Ethernet driver
  258. (e.g drivers/usb/eth/asix.c) should include a USB_DEVICE() declaration, so
  259. that it will be found as part of normal USB enumeration. Then, instead of a
  260. generic USB driver, a real (driver-model-aware) driver will be used. Since
  261. Ethernet now supports driver model, this should be fairly easy to achieve,
  262. and then usb_ether.c and the usb_host_eth_scan() will melt away.
  263. Sandbox
  264. -------
  265. All driver model uclasses must have tests and USB is no exception. To
  266. achieve this, a sandbox USB controller is provided. This can make use of
  267. emulation drivers which pretend to be USB devices. Emulations are provided
  268. for a hub and a flash stick. These are enough to create a pretend USB bus
  269. (defined by the sandbox device tree sandbox.dts) which can be scanned and
  270. used.
  271. Tests in test/dm/usb.c make use of this feature. It allows much of the USB
  272. stack to be tested without real hardware being needed.
  273. Here is an example device tree fragment:
  274. usb@1 {
  275. compatible = "sandbox,usb";
  276. hub {
  277. compatible = "usb-hub";
  278. usb,device-class = <USB_CLASS_HUB>;
  279. hub-emul {
  280. compatible = "sandbox,usb-hub";
  281. #address-cells = <1>;
  282. #size-cells = <0>;
  283. flash-stick {
  284. reg = <0>;
  285. compatible = "sandbox,usb-flash";
  286. sandbox,filepath = "flash.bin";
  287. };
  288. };
  289. };
  290. };
  291. This defines a single controller, containing a root hub (which is required).
  292. The hub is emulated by a hub emulator, and the emulated hub has a single
  293. flash stick to emulate on one of its ports.
  294. When 'usb start' is used, the following 'dm tree' output will be available:
  295. usb [ + ] `-- usb@1
  296. usb_hub [ + ] `-- hub
  297. usb_emul [ + ] |-- hub-emul
  298. usb_emul [ + ] | `-- flash-stick
  299. usb_mass_st [ + ] `-- usb_mass_storage
  300. This may look confusing. Most of it mirrors the device tree, but the
  301. 'usb_mass_storage' device is not in the device tree. This is created by
  302. usb_find_and_bind_driver() based on the USB_DRIVER in usb_storage.c. While
  303. 'flash-stick' is the emulation device, 'usb_mass_storage' is the real U-Boot
  304. USB device driver that talks to it.
  305. Future work
  306. -----------
  307. It is pretty uncommon to have a large USB bus with lots of hubs on an
  308. embedded system. In fact anything other than a root hub is uncommon. Still
  309. it would be possible to speed up enumeration in two ways:
  310. - breadth-first search would allow devices to be reset and probed in
  311. parallel to some extent
  312. - enumeration could be lazy, in the sense that we could enumerate just the
  313. root hub at first, then only progress to the next 'level' when a device is
  314. used that we cannot find. This could be made easier if the devices were
  315. statically declared in the device tree (which is acceptable for production
  316. boards where the same, known, things are on each bus).
  317. But in common cases the current algorithm is sufficient.
  318. Other things that need doing:
  319. - Convert usb_ether to use driver model as described above
  320. - Test that keyboards work (and convert to driver model)
  321. - Move the USB gadget framework to driver model
  322. - Implement OHCI in driver model
  323. - Implement USB PHYs in driver model
  324. - Work out a clever way to provide lazy init for USB devices
  325. --
  326. Simon Glass <sjg@chromium.org>
  327. 23-Mar-15