xref: /OK3568_Linux_fs/u-boot/doc/driver-model/usb-info.txt (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593SmuzhiyunHow USB works with driver model
2*4882a593Smuzhiyun===============================
3*4882a593Smuzhiyun
4*4882a593SmuzhiyunIntroduction
5*4882a593Smuzhiyun------------
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunDriver model USB support makes use of existing features but changes how
8*4882a593Smuzhiyundrivers are found. This document provides some information intended to help
9*4882a593Smuzhiyununderstand how things work with USB in U-Boot when driver model is enabled.
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun
12*4882a593SmuzhiyunEnabling driver model for USB
13*4882a593Smuzhiyun-----------------------------
14*4882a593Smuzhiyun
15*4882a593SmuzhiyunA new CONFIG_DM_USB option is provided to enable driver model for USB. This
16*4882a593Smuzhiyuncauses the USB uclass to be included, and drops the equivalent code in
17*4882a593Smuzhiyunusb.c. In particular the usb_init() function is then implemented by the
18*4882a593Smuzhiyunuclass.
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun
21*4882a593SmuzhiyunSupport for EHCI and XHCI
22*4882a593Smuzhiyun-------------------------
23*4882a593Smuzhiyun
24*4882a593SmuzhiyunSo far OHCI is not supported. Both EHCI and XHCI drivers should be declared
25*4882a593Smuzhiyunas drivers in the USB uclass. For example:
26*4882a593Smuzhiyun
27*4882a593Smuzhiyunstatic const struct udevice_id ehci_usb_ids[] = {
28*4882a593Smuzhiyun	{ .compatible = "nvidia,tegra20-ehci", .data = USB_CTLR_T20 },
29*4882a593Smuzhiyun	{ .compatible = "nvidia,tegra30-ehci", .data = USB_CTLR_T30 },
30*4882a593Smuzhiyun	{ .compatible = "nvidia,tegra114-ehci", .data = USB_CTLR_T114 },
31*4882a593Smuzhiyun	{ }
32*4882a593Smuzhiyun};
33*4882a593Smuzhiyun
34*4882a593SmuzhiyunU_BOOT_DRIVER(usb_ehci) = {
35*4882a593Smuzhiyun	.name	= "ehci_tegra",
36*4882a593Smuzhiyun	.id	= UCLASS_USB,
37*4882a593Smuzhiyun	.of_match = ehci_usb_ids,
38*4882a593Smuzhiyun	.ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
39*4882a593Smuzhiyun	.probe = tegra_ehci_usb_probe,
40*4882a593Smuzhiyun	.remove = tegra_ehci_usb_remove,
41*4882a593Smuzhiyun	.ops	= &ehci_usb_ops,
42*4882a593Smuzhiyun	.platdata_auto_alloc_size = sizeof(struct usb_platdata),
43*4882a593Smuzhiyun	.priv_auto_alloc_size = sizeof(struct fdt_usb),
44*4882a593Smuzhiyun	.flags	= DM_FLAG_ALLOC_PRIV_DMA,
45*4882a593Smuzhiyun};
46*4882a593Smuzhiyun
47*4882a593SmuzhiyunHere ehci_usb_ids is used to list the controllers that the driver supports.
48*4882a593SmuzhiyunEach has its own data value. Controllers must be in the UCLASS_USB uclass.
49*4882a593Smuzhiyun
50*4882a593SmuzhiyunThe ofdata_to_platdata() method allows the controller driver to grab any
51*4882a593Smuzhiyunnecessary settings from the device tree.
52*4882a593Smuzhiyun
53*4882a593SmuzhiyunThe ops here are ehci_usb_ops. All EHCI drivers will use these same ops in
54*4882a593Smuzhiyunmost cases, since they are all EHCI-compatible. For EHCI there are also some
55*4882a593Smuzhiyunspecial operations that can be overridden when calling ehci_register().
56*4882a593Smuzhiyun
57*4882a593SmuzhiyunThe driver can use priv_auto_alloc_size to set the size of its private data.
58*4882a593SmuzhiyunThis can hold run-time information needed by the driver for operation. It
59*4882a593Smuzhiyunexists when the device is probed (not when it is bound) and is removed when
60*4882a593Smuzhiyunthe driver is removed.
61*4882a593Smuzhiyun
62*4882a593SmuzhiyunNote that usb_platdata is currently only used to deal with setting up a bus
63*4882a593Smuzhiyunin USB device mode (OTG operation). It can be omitted if that is not
64*4882a593Smuzhiyunsupported.
65*4882a593Smuzhiyun
66*4882a593SmuzhiyunThe driver's probe() method should do the basic controller init and then
67*4882a593Smuzhiyuncall ehci_register() to register itself as an EHCI device. It should call
68*4882a593Smuzhiyunehci_deregister() in the remove() method. Registering a new EHCI device
69*4882a593Smuzhiyundoes not by itself cause the bus to be scanned.
70*4882a593Smuzhiyun
71*4882a593SmuzhiyunThe old ehci_hcd_init() function is no-longer used. Nor is it necessary to
72*4882a593Smuzhiyunset up the USB controllers from board init code. When 'usb start' is used,
73*4882a593Smuzhiyuneach controller will be probed and its bus scanned.
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunXHCI works in a similar way.
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun
78*4882a593SmuzhiyunData structures
79*4882a593Smuzhiyun---------------
80*4882a593Smuzhiyun
81*4882a593SmuzhiyunThe following primary data structures are in use:
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun- struct usb_device
84*4882a593Smuzhiyun	This holds information about a device on the bus. All devices have
85*4882a593Smuzhiyun	this structure, even the root hub. The controller itself does not
86*4882a593Smuzhiyun	have this structure. You can access it for a device 'dev' with
87*4882a593Smuzhiyun	dev_get_parent_priv(dev). It matches the old structure except that the
88*4882a593Smuzhiyun	parent and child information is not present (since driver model
89*4882a593Smuzhiyun	handles that). Once the device is set up, you can find the device
90*4882a593Smuzhiyun	descriptor and current configuration descriptor in this structure.
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun- struct usb_platdata
93*4882a593Smuzhiyun	This holds platform data for a controller. So far this is only used
94*4882a593Smuzhiyun	as a work-around for controllers which can act as USB devices in OTG
95*4882a593Smuzhiyun	mode, since the gadget framework does not use driver model.
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun- struct usb_dev_platdata
98*4882a593Smuzhiyun	This holds platform data for a device. You can access it for a
99*4882a593Smuzhiyun	device 'dev' with dev_get_parent_platdata(dev). It holds the device
100*4882a593Smuzhiyun	address and speed - anything that can be determined before the device
101*4882a593Smuzhiyun	driver is actually set up. When probing the bus this structure is
102*4882a593Smuzhiyun	used to provide essential information to the device driver.
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun- struct usb_bus_priv
105*4882a593Smuzhiyun	This is private information for each controller, maintained by the
106*4882a593Smuzhiyun	controller uclass. It is mostly used to keep track of the next
107*4882a593Smuzhiyun	device address to use.
108*4882a593Smuzhiyun
109*4882a593SmuzhiyunOf these, only struct usb_device was used prior to driver model.
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun
112*4882a593SmuzhiyunUSB buses
113*4882a593Smuzhiyun---------
114*4882a593Smuzhiyun
115*4882a593SmuzhiyunGiven a controller, you know the bus - it is the one attached to the
116*4882a593Smuzhiyuncontroller. Each controller handles exactly one bus. Every controller has a
117*4882a593Smuzhiyunroot hub attached to it. This hub, which is itself a USB device, can provide
118*4882a593Smuzhiyunone or more 'ports' to which additional devices can be attached. It is
119*4882a593Smuzhiyunpossible to power up a hub and find out which of its ports have devices
120*4882a593Smuzhiyunattached.
121*4882a593Smuzhiyun
122*4882a593SmuzhiyunDevices are given addresses starting at 1. The root hub is always address 1,
123*4882a593Smuzhiyunand from there the devices are numbered in sequence. The USB uclass takes
124*4882a593Smuzhiyuncare of this numbering automatically during enumeration.
125*4882a593Smuzhiyun
126*4882a593SmuzhiyunUSB devices are enumerated by finding a device on a particular hub, and
127*4882a593Smuzhiyunsetting its address to the next available address. The USB bus stretches out
128*4882a593Smuzhiyunin a tree structure, potentially with multiple hubs each with several ports
129*4882a593Smuzhiyunand perhaps other hubs. Some hubs will have their own power since otherwise
130*4882a593Smuzhiyunthe 5V 500mA power supplied by the controller will not be sufficient to run
131*4882a593Smuzhiyunvery many devices.
132*4882a593Smuzhiyun
133*4882a593SmuzhiyunEnumeration in U-Boot takes a long time since devices are probed one at a
134*4882a593Smuzhiyuntime, and each is given sufficient time to wake up and announce itself. The
135*4882a593Smuzhiyuntimeouts are set for the slowest device.
136*4882a593Smuzhiyun
137*4882a593SmuzhiyunUp to 127 devices can be on each bus. USB has four bus speeds: low
138*4882a593Smuzhiyun(1.5Mbps), full (12Mbps), high (480Mbps) which is only available with USB2
139*4882a593Smuzhiyunand newer (EHCI), and super (5Gbps) which is only available with USB3 and
140*4882a593Smuzhiyunnewer (XHCI). If you connect a super-speed device to a high-speed hub, you
141*4882a593Smuzhiyunwill only get high-speed.
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun
144*4882a593SmuzhiyunUSB operations
145*4882a593Smuzhiyun--------------
146*4882a593Smuzhiyun
147*4882a593SmuzhiyunAs before driver model, messages can be sent using submit_bulk_msg() and the
148*4882a593Smuzhiyunlike. These are now implemented by the USB uclass and route through the
149*4882a593Smuzhiyuncontroller drivers. Note that messages are not sent to the driver of the
150*4882a593Smuzhiyundevice itself - i.e. they don't pass down the stack to the controller.
151*4882a593SmuzhiyunU-Boot simply finds the controller to which the device is attached, and sends
152*4882a593Smuzhiyunthe message there with an appropriate 'pipe' value so it can be addressed
153*4882a593Smuzhiyunproperly. Having said that, the USB device which should receive the message
154*4882a593Smuzhiyunis passed in to the driver methods, for use by sandbox. This design decision
155*4882a593Smuzhiyunis open for review and the code impact of changing it is small since the
156*4882a593Smuzhiyunmethods are typically implemented by the EHCI and XHCI stacks.
157*4882a593Smuzhiyun
158*4882a593SmuzhiyunController drivers (in UCLASS_USB) themselves provide methods for sending
159*4882a593Smuzhiyuneach message type. For XHCI an additional alloc_device() method is provided
160*4882a593Smuzhiyunsince XHCI needs to allocate a device context before it can even read the
161*4882a593Smuzhiyundevice's descriptor.
162*4882a593Smuzhiyun
163*4882a593SmuzhiyunThese methods use a 'pipe' which is a collection of bit fields used to
164*4882a593Smuzhiyundescribe the type of message, direction of transfer and the intended
165*4882a593Smuzhiyunrecipient (device number).
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun
168*4882a593SmuzhiyunUSB Devices
169*4882a593Smuzhiyun-----------
170*4882a593Smuzhiyun
171*4882a593SmuzhiyunUSB devices are found using a simple algorithm which works through the
172*4882a593Smuzhiyunavailable hubs in a depth-first search. Devices can be in any uclass, but
173*4882a593Smuzhiyunare attached to a parent hub (or controller in the case of the root hub) and
174*4882a593Smuzhiyunso have parent data attached to them (this is struct usb_device).
175*4882a593Smuzhiyun
176*4882a593SmuzhiyunBy the time the device's probe() method is called, it is enumerated and is
177*4882a593Smuzhiyunready to talk to the host.
178*4882a593Smuzhiyun
179*4882a593SmuzhiyunThe enumeration process needs to work out which driver to attach to each USB
180*4882a593Smuzhiyundevice. It does this by examining the device class, interface class, vendor
181*4882a593SmuzhiyunID, product ID, etc. See struct usb_driver_entry for how drivers are matched
182*4882a593Smuzhiyunwith USB devices - you can use the USB_DEVICE() macro to declare a USB
183*4882a593Smuzhiyundriver. For example, usb_storage.c defines a USB_DEVICE() to handle storage
184*4882a593Smuzhiyundevices, and it will be used for all USB devices which match.
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun
188*4882a593SmuzhiyunTechnical details on enumeration flow
189*4882a593Smuzhiyun-------------------------------------
190*4882a593Smuzhiyun
191*4882a593SmuzhiyunIt is useful to understand precisely how a USB bus is enumerating to avoid
192*4882a593Smuzhiyunconfusion when dealing with USB devices.
193*4882a593Smuzhiyun
194*4882a593SmuzhiyunDevice initialisation happens roughly like this:
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun- At some point the 'usb start' command is run
197*4882a593Smuzhiyun- This calls usb_init() which works through each controller in turn
198*4882a593Smuzhiyun- The controller is probed(). This does no enumeration.
199*4882a593Smuzhiyun- Then usb_scan_bus() is called. This calls usb_scan_device() to scan the
200*4882a593Smuzhiyun(only) device that is attached to the controller - a root hub
201*4882a593Smuzhiyun- usb_scan_device() sets up a fake struct usb_device and calls
202*4882a593Smuzhiyunusb_setup_device(), passing the port number to be scanned, in this case port
203*4882a593Smuzhiyun0
204*4882a593Smuzhiyun- usb_setup_device() first calls usb_prepare_device() to set the device
205*4882a593Smuzhiyunaddress, then usb_select_config() to select the first configuration
206*4882a593Smuzhiyun- at this point the device is enumerated but we do not have a real struct
207*4882a593Smuzhiyunudevice for it. But we do have the descriptor in struct usb_device so we can
208*4882a593Smuzhiyunuse this to figure out what driver to use
209*4882a593Smuzhiyun- back in usb_scan_device(), we call usb_find_child() to try to find an
210*4882a593Smuzhiyunexisting device which matches the one we just found on the bus. This can
211*4882a593Smuzhiyunhappen if the device is mentioned in the device tree, or if we previously
212*4882a593Smuzhiyunscanned the bus and so the device was created before
213*4882a593Smuzhiyun- if usb_find_child() does not find an existing device, we call
214*4882a593Smuzhiyunusb_find_and_bind_driver() which tries to bind one
215*4882a593Smuzhiyun- usb_find_and_bind_driver() searches all available USB drivers (declared
216*4882a593Smuzhiyunwith USB_DEVICE()). If it finds a match it binds that driver to create a new
217*4882a593Smuzhiyundevice.
218*4882a593Smuzhiyun- If it does not, it binds a generic driver. A generic driver is good enough
219*4882a593Smuzhiyunto allow access to the device (sending it packets, etc.) but all
220*4882a593Smuzhiyunfunctionality will need to be implemented outside the driver model.
221*4882a593Smuzhiyun- in any case, when usb_find_child() and/or usb_find_and_bind_driver() are
222*4882a593Smuzhiyundone, we have a device with the correct uclass. At this point we want to
223*4882a593Smuzhiyunprobe the device
224*4882a593Smuzhiyun- first we store basic information about the new device (address, port,
225*4882a593Smuzhiyunspeed) in its parent platform data. We cannot store it its private data
226*4882a593Smuzhiyunsince that will not exist until the device is probed.
227*4882a593Smuzhiyun- then we call device_probe() which probes the device
228*4882a593Smuzhiyun- the first probe step is actually the USB controller's (or USB hubs's)
229*4882a593Smuzhiyunchild_pre_probe() method. This gets called before anything else and is
230*4882a593Smuzhiyunintended to set up a child device ready to be used with its parent bus. For
231*4882a593SmuzhiyunUSB this calls usb_child_pre_probe() which grabs the information that was
232*4882a593Smuzhiyunstored in the parent platform data and stores it in the parent private data
233*4882a593Smuzhiyun(which is struct usb_device, a real one this time). It then calls
234*4882a593Smuzhiyunusb_select_config() again to make sure that everything about the device is
235*4882a593Smuzhiyunset up
236*4882a593Smuzhiyun- note that we have called usb_select_config() twice. This is inefficient
237*4882a593Smuzhiyunbut the alternative is to store additional information in the platform data.
238*4882a593SmuzhiyunThe time taken is minimal and this way is simpler
239*4882a593Smuzhiyun- at this point the device is set up and ready for use so far as the USB
240*4882a593Smuzhiyunsubsystem is concerned
241*4882a593Smuzhiyun- the device's probe() method is then called. It can send messages and do
242*4882a593Smuzhiyunwhatever else it wants to make the device work.
243*4882a593Smuzhiyun
244*4882a593SmuzhiyunNote that the first device is always a root hub, and this must be scanned to
245*4882a593Smuzhiyunfind any devices. The above steps will have created a hub (UCLASS_USB_HUB),
246*4882a593Smuzhiyungiven it address 1 and set the configuration.
247*4882a593Smuzhiyun
248*4882a593SmuzhiyunFor hubs, the hub uclass has a post_probe() method. This means that after
249*4882a593Smuzhiyunany hub is probed, the uclass gets to do some processing. In this case
250*4882a593Smuzhiyunusb_hub_post_probe() is called, and the following steps take place:
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun- usb_hub_post_probe() calls usb_hub_scan() to scan the hub, which in turn
253*4882a593Smuzhiyuncalls usb_hub_configure()
254*4882a593Smuzhiyun- hub power is enabled
255*4882a593Smuzhiyun- we loop through each port on the hub, performing the same steps for each
256*4882a593Smuzhiyun- first, check if there is a device present. This happens in
257*4882a593Smuzhiyunusb_hub_port_connect_change(). If so, then usb_scan_device() is called to
258*4882a593Smuzhiyunscan the device, passing the appropriate port number.
259*4882a593Smuzhiyun- you will recognise usb_scan_device() from the steps above. It sets up the
260*4882a593Smuzhiyundevice ready for use. If it is a hub, it will scan that hub before it
261*4882a593Smuzhiyuncontinues here (recursively, depth-first)
262*4882a593Smuzhiyun- once all hub ports are scanned in this way, the hub is ready for use and
263*4882a593Smuzhiyunall of its downstream devices also
264*4882a593Smuzhiyun- additional controllers are scanned in the same way
265*4882a593Smuzhiyun
266*4882a593SmuzhiyunThe above method has some nice properties:
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun- the bus enumeration happens by virtue of driver model's natural device flow
269*4882a593Smuzhiyun- most logic is in the USB controller and hub uclasses; the actual device
270*4882a593Smuzhiyundrivers do not need to know they are on a USB bus, at least so far as
271*4882a593Smuzhiyunenumeration goes
272*4882a593Smuzhiyun- hub scanning happens automatically after a hub is probed
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun
275*4882a593SmuzhiyunHubs
276*4882a593Smuzhiyun----
277*4882a593Smuzhiyun
278*4882a593SmuzhiyunUSB hubs are scanned as in the section above. While hubs have their own
279*4882a593Smuzhiyunuclass, they share some common elements with controllers:
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun- they both attach private data to their children (struct usb_device,
282*4882a593Smuzhiyunaccessible for a child with dev_get_parent_priv(child))
283*4882a593Smuzhiyun- they both use usb_child_pre_probe() to set up their children as proper USB
284*4882a593Smuzhiyundevices
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun
287*4882a593SmuzhiyunExample - Mass Storage
288*4882a593Smuzhiyun----------------------
289*4882a593Smuzhiyun
290*4882a593SmuzhiyunAs an example of a USB device driver, see usb_storage.c. It uses its own
291*4882a593Smuzhiyunuclass and declares itself as follows:
292*4882a593Smuzhiyun
293*4882a593SmuzhiyunU_BOOT_DRIVER(usb_mass_storage) = {
294*4882a593Smuzhiyun	.name	= "usb_mass_storage",
295*4882a593Smuzhiyun	.id	= UCLASS_MASS_STORAGE,
296*4882a593Smuzhiyun	.of_match = usb_mass_storage_ids,
297*4882a593Smuzhiyun	.probe = usb_mass_storage_probe,
298*4882a593Smuzhiyun};
299*4882a593Smuzhiyun
300*4882a593Smuzhiyunstatic const struct usb_device_id mass_storage_id_table[] = {
301*4882a593Smuzhiyun    { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
302*4882a593Smuzhiyun      .bInterfaceClass = USB_CLASS_MASS_STORAGE},
303*4882a593Smuzhiyun    { }						/* Terminating entry */
304*4882a593Smuzhiyun};
305*4882a593Smuzhiyun
306*4882a593SmuzhiyunUSB_DEVICE(usb_mass_storage, mass_storage_id_table);
307*4882a593Smuzhiyun
308*4882a593SmuzhiyunThe USB_DEVICE() macro attaches the given table of matching information to
309*4882a593Smuzhiyunthe given driver. Note that the driver is declared in U_BOOT_DRIVER() as
310*4882a593Smuzhiyun'usb_mass_storage' and this must match the first parameter of USB_DEVICE.
311*4882a593Smuzhiyun
312*4882a593SmuzhiyunWhen usb_find_and_bind_driver() is called on a USB device with the
313*4882a593SmuzhiyunbInterfaceClass value of USB_CLASS_MASS_STORAGE, it will automatically find
314*4882a593Smuzhiyunthis driver and use it.
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun
317*4882a593SmuzhiyunCounter-example: USB Ethernet
318*4882a593Smuzhiyun-----------------------------
319*4882a593Smuzhiyun
320*4882a593SmuzhiyunAs an example of the old way of doing things, see usb_ether.c. When the bus
321*4882a593Smuzhiyunis scanned, all Ethernet devices will be created as generic USB devices (in
322*4882a593Smuzhiyunuclass UCLASS_USB_DEV_GENERIC). Then, when the scan is completed,
323*4882a593Smuzhiyunusb_host_eth_scan() will be called. This looks through all the devices on
324*4882a593Smuzhiyuneach bus and manually figures out which are Ethernet devices in the ways of
325*4882a593Smuzhiyunyore.
326*4882a593Smuzhiyun
327*4882a593SmuzhiyunIn fact, usb_ether should be moved to driver model. Each USB Ethernet driver
328*4882a593Smuzhiyun(e.g drivers/usb/eth/asix.c) should include a USB_DEVICE() declaration, so
329*4882a593Smuzhiyunthat it will be found as part of normal USB enumeration. Then, instead of a
330*4882a593Smuzhiyungeneric USB driver, a real (driver-model-aware) driver will be used. Since
331*4882a593SmuzhiyunEthernet now supports driver model, this should be fairly easy to achieve,
332*4882a593Smuzhiyunand then usb_ether.c and the usb_host_eth_scan() will melt away.
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun
335*4882a593SmuzhiyunSandbox
336*4882a593Smuzhiyun-------
337*4882a593Smuzhiyun
338*4882a593SmuzhiyunAll driver model uclasses must have tests and USB is no exception. To
339*4882a593Smuzhiyunachieve this, a sandbox USB controller is provided. This can make use of
340*4882a593Smuzhiyunemulation drivers which pretend to be USB devices. Emulations are provided
341*4882a593Smuzhiyunfor a hub and a flash stick. These are enough to create a pretend USB bus
342*4882a593Smuzhiyun(defined by the sandbox device tree sandbox.dts) which can be scanned and
343*4882a593Smuzhiyunused.
344*4882a593Smuzhiyun
345*4882a593SmuzhiyunTests in test/dm/usb.c make use of this feature. It allows much of the USB
346*4882a593Smuzhiyunstack to be tested without real hardware being needed.
347*4882a593Smuzhiyun
348*4882a593SmuzhiyunHere is an example device tree fragment:
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun	usb@1 {
351*4882a593Smuzhiyun		compatible = "sandbox,usb";
352*4882a593Smuzhiyun		hub {
353*4882a593Smuzhiyun			compatible = "usb-hub";
354*4882a593Smuzhiyun			usb,device-class = <USB_CLASS_HUB>;
355*4882a593Smuzhiyun			hub-emul {
356*4882a593Smuzhiyun				compatible = "sandbox,usb-hub";
357*4882a593Smuzhiyun				#address-cells = <1>;
358*4882a593Smuzhiyun				#size-cells = <0>;
359*4882a593Smuzhiyun				flash-stick {
360*4882a593Smuzhiyun					reg = <0>;
361*4882a593Smuzhiyun					compatible = "sandbox,usb-flash";
362*4882a593Smuzhiyun					sandbox,filepath = "flash.bin";
363*4882a593Smuzhiyun				};
364*4882a593Smuzhiyun			};
365*4882a593Smuzhiyun		};
366*4882a593Smuzhiyun	};
367*4882a593Smuzhiyun
368*4882a593SmuzhiyunThis defines a single controller, containing a root hub (which is required).
369*4882a593SmuzhiyunThe hub is emulated by a hub emulator, and the emulated hub has a single
370*4882a593Smuzhiyunflash stick to emulate on one of its ports.
371*4882a593Smuzhiyun
372*4882a593SmuzhiyunWhen 'usb start' is used, the following 'dm tree' output will be available:
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun usb         [ + ]    `-- usb@1
375*4882a593Smuzhiyun usb_hub     [ + ]        `-- hub
376*4882a593Smuzhiyun usb_emul    [ + ]            |-- hub-emul
377*4882a593Smuzhiyun usb_emul    [ + ]            |   `-- flash-stick
378*4882a593Smuzhiyun usb_mass_st [ + ]            `-- usb_mass_storage
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun
381*4882a593SmuzhiyunThis may look confusing. Most of it mirrors the device tree, but the
382*4882a593Smuzhiyun'usb_mass_storage' device is not in the device tree. This is created by
383*4882a593Smuzhiyunusb_find_and_bind_driver() based on the USB_DRIVER in usb_storage.c. While
384*4882a593Smuzhiyun'flash-stick' is the emulation device, 'usb_mass_storage' is the real U-Boot
385*4882a593SmuzhiyunUSB device driver that talks to it.
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun
388*4882a593SmuzhiyunFuture work
389*4882a593Smuzhiyun-----------
390*4882a593Smuzhiyun
391*4882a593SmuzhiyunIt is pretty uncommon to have a large USB bus with lots of hubs on an
392*4882a593Smuzhiyunembedded system. In fact anything other than a root hub is uncommon. Still
393*4882a593Smuzhiyunit would be possible to speed up enumeration in two ways:
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun- breadth-first search would allow devices to be reset and probed in
396*4882a593Smuzhiyunparallel to some extent
397*4882a593Smuzhiyun- enumeration could be lazy, in the sense that we could enumerate just the
398*4882a593Smuzhiyunroot hub at first, then only progress to the next 'level' when a device is
399*4882a593Smuzhiyunused that we cannot find. This could be made easier if the devices were
400*4882a593Smuzhiyunstatically declared in the device tree (which is acceptable for production
401*4882a593Smuzhiyunboards where the same, known, things are on each bus).
402*4882a593Smuzhiyun
403*4882a593SmuzhiyunBut in common cases the current algorithm is sufficient.
404*4882a593Smuzhiyun
405*4882a593SmuzhiyunOther things that need doing:
406*4882a593Smuzhiyun- Convert usb_ether to use driver model as described above
407*4882a593Smuzhiyun- Test that keyboards work (and convert to driver model)
408*4882a593Smuzhiyun- Move the USB gadget framework to driver model
409*4882a593Smuzhiyun- Implement OHCI in driver model
410*4882a593Smuzhiyun- Implement USB PHYs in driver model
411*4882a593Smuzhiyun- Work out a clever way to provide lazy init for USB devices
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun--
414*4882a593SmuzhiyunSimon Glass <sjg@chromium.org>
415*4882a593Smuzhiyun23-Mar-15
416