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