15fd2733eSSimon GlassHow USB works with driver model 25fd2733eSSimon Glass=============================== 35fd2733eSSimon Glass 45fd2733eSSimon GlassIntroduction 55fd2733eSSimon Glass------------ 65fd2733eSSimon Glass 75fd2733eSSimon GlassDriver model USB support makes use of existing features but changes how 85fd2733eSSimon Glassdrivers are found. This document provides some information intended to help 95fd2733eSSimon Glassunderstand how things work with USB in U-Boot when driver model is enabled. 105fd2733eSSimon Glass 115fd2733eSSimon Glass 125fd2733eSSimon GlassEnabling driver model for USB 135fd2733eSSimon Glass----------------------------- 145fd2733eSSimon Glass 155fd2733eSSimon GlassA new CONFIG_DM_USB option is provided to enable driver model for USB. This 165fd2733eSSimon Glasscauses the USB uclass to be included, and drops the equivalent code in 175fd2733eSSimon Glassusb.c. In particular the usb_init() function is then implemented by the 185fd2733eSSimon Glassuclass. 195fd2733eSSimon Glass 205fd2733eSSimon Glass 215fd2733eSSimon GlassSupport for EHCI and XHCI 225fd2733eSSimon Glass------------------------- 235fd2733eSSimon Glass 245fd2733eSSimon GlassSo far OHCI is not supported. Both EHCI and XHCI drivers should be declared 255fd2733eSSimon Glassas drivers in the USB uclass. For example: 265fd2733eSSimon Glass 275fd2733eSSimon Glassstatic const struct udevice_id ehci_usb_ids[] = { 285fd2733eSSimon Glass { .compatible = "nvidia,tegra20-ehci", .data = USB_CTLR_T20 }, 295fd2733eSSimon Glass { .compatible = "nvidia,tegra30-ehci", .data = USB_CTLR_T30 }, 305fd2733eSSimon Glass { .compatible = "nvidia,tegra114-ehci", .data = USB_CTLR_T114 }, 315fd2733eSSimon Glass { } 325fd2733eSSimon Glass}; 335fd2733eSSimon Glass 345fd2733eSSimon GlassU_BOOT_DRIVER(usb_ehci) = { 355fd2733eSSimon Glass .name = "ehci_tegra", 365fd2733eSSimon Glass .id = UCLASS_USB, 375fd2733eSSimon Glass .of_match = ehci_usb_ids, 385fd2733eSSimon Glass .ofdata_to_platdata = ehci_usb_ofdata_to_platdata, 395fd2733eSSimon Glass .probe = tegra_ehci_usb_probe, 405fd2733eSSimon Glass .remove = tegra_ehci_usb_remove, 415fd2733eSSimon Glass .ops = &ehci_usb_ops, 425fd2733eSSimon Glass .platdata_auto_alloc_size = sizeof(struct usb_platdata), 435fd2733eSSimon Glass .priv_auto_alloc_size = sizeof(struct fdt_usb), 445fd2733eSSimon Glass .flags = DM_FLAG_ALLOC_PRIV_DMA, 455fd2733eSSimon Glass}; 465fd2733eSSimon Glass 475fd2733eSSimon GlassHere ehci_usb_ids is used to list the controllers that the driver supports. 485fd2733eSSimon GlassEach has its own data value. Controllers must be in the UCLASS_USB uclass. 495fd2733eSSimon Glass 505fd2733eSSimon GlassThe ofdata_to_platdata() method allows the controller driver to grab any 515fd2733eSSimon Glassnecessary settings from the device tree. 525fd2733eSSimon Glass 535fd2733eSSimon GlassThe ops here are ehci_usb_ops. All EHCI drivers will use these same ops in 545fd2733eSSimon Glassmost cases, since they are all EHCI-compatible. For EHCI there are also some 555fd2733eSSimon Glassspecial operations that can be overridden when calling ehci_register(). 565fd2733eSSimon Glass 575fd2733eSSimon GlassThe driver can use priv_auto_alloc_size to set the size of its private data. 585fd2733eSSimon GlassThis can hold run-time information needed by the driver for operation. It 595fd2733eSSimon Glassexists when the device is probed (not when it is bound) and is removed when 605fd2733eSSimon Glassthe driver is removed. 615fd2733eSSimon Glass 625fd2733eSSimon GlassNote that usb_platdata is currently only used to deal with setting up a bus 635fd2733eSSimon Glassin USB device mode (OTG operation). It can be omitted if that is not 645fd2733eSSimon Glasssupported. 655fd2733eSSimon Glass 665fd2733eSSimon GlassThe driver's probe() method should do the basic controller init and then 675fd2733eSSimon Glasscall ehci_register() to register itself as an EHCI device. It should call 685fd2733eSSimon Glassehci_deregister() in the remove() method. Registering a new EHCI device 695fd2733eSSimon Glassdoes not by itself cause the bus to be scanned. 705fd2733eSSimon Glass 715fd2733eSSimon GlassThe old ehci_hcd_init() function is no-longer used. Nor is it necessary to 725fd2733eSSimon Glassset up the USB controllers from board init code. When 'usb start' is used, 735fd2733eSSimon Glasseach controller will be probed and its bus scanned. 745fd2733eSSimon Glass 755fd2733eSSimon GlassXHCI works in a similar way. 765fd2733eSSimon Glass 775fd2733eSSimon Glass 785fd2733eSSimon GlassData structures 795fd2733eSSimon Glass--------------- 805fd2733eSSimon Glass 815fd2733eSSimon GlassThe following primary data structures are in use: 825fd2733eSSimon Glass 835fd2733eSSimon Glass- struct usb_device 845fd2733eSSimon Glass This holds information about a device on the bus. All devices have 855fd2733eSSimon Glass this structure, even the root hub. The controller itself does not 865fd2733eSSimon Glass have this structure. You can access it for a device 'dev' with 87*bcbe3d15SSimon Glass dev_get_parent_priv(dev). It matches the old structure except that the 885fd2733eSSimon Glass parent and child information is not present (since driver model 895fd2733eSSimon Glass handles that). Once the device is set up, you can find the device 905fd2733eSSimon Glass descriptor and current configuration descriptor in this structure. 915fd2733eSSimon Glass 925fd2733eSSimon Glass- struct usb_platdata 935fd2733eSSimon Glass This holds platform data for a controller. So far this is only used 945fd2733eSSimon Glass as a work-around for controllers which can act as USB devices in OTG 955fd2733eSSimon Glass mode, since the gadget framework does not use driver model. 965fd2733eSSimon Glass 975fd2733eSSimon Glass- struct usb_dev_platdata 985fd2733eSSimon Glass This holds platform data for a device. You can access it for a 995fd2733eSSimon Glass device 'dev' with dev_get_parent_platdata(dev). It holds the device 1005fd2733eSSimon Glass address and speed - anything that can be determined before the device 1015fd2733eSSimon Glass driver is actually set up. When probing the bus this structure is 1025fd2733eSSimon Glass used to provide essential information to the device driver. 1035fd2733eSSimon Glass 1045fd2733eSSimon Glass- struct usb_bus_priv 1055fd2733eSSimon Glass This is private information for each controller, maintained by the 1065fd2733eSSimon Glass controller uclass. It is mostly used to keep track of the next 1075fd2733eSSimon Glass device address to use. 1085fd2733eSSimon Glass 1095fd2733eSSimon GlassOf these, only struct usb_device was used prior to driver model. 1105fd2733eSSimon Glass 1115fd2733eSSimon Glass 1125fd2733eSSimon GlassUSB buses 1135fd2733eSSimon Glass--------- 1145fd2733eSSimon Glass 1155fd2733eSSimon GlassGiven a controller, you know the bus - it is the one attached to the 1165fd2733eSSimon Glasscontroller. Each controller handles exactly one bus. Every controller has a 1175fd2733eSSimon Glassroot hub attached to it. This hub, which is itself a USB device, can provide 1185fd2733eSSimon Glassone or more 'ports' to which additional devices can be attached. It is 1195fd2733eSSimon Glasspossible to power up a hub and find out which of its ports have devices 1205fd2733eSSimon Glassattached. 1215fd2733eSSimon Glass 1225fd2733eSSimon GlassDevices are given addresses starting at 1. The root hub is always address 1, 1235fd2733eSSimon Glassand from there the devices are numbered in sequence. The USB uclass takes 1245fd2733eSSimon Glasscare of this numbering automatically during enumeration. 1255fd2733eSSimon Glass 1265fd2733eSSimon GlassUSB devices are enumerated by finding a device on a particular hub, and 1275fd2733eSSimon Glasssetting its address to the next available address. The USB bus stretches out 1285fd2733eSSimon Glassin a tree structure, potentially with multiple hubs each with several ports 1295fd2733eSSimon Glassand perhaps other hubs. Some hubs will have their own power since otherwise 1305fd2733eSSimon Glassthe 5V 500mA power supplied by the controller will not be sufficient to run 1315fd2733eSSimon Glassvery many devices. 1325fd2733eSSimon Glass 1335fd2733eSSimon GlassEnumeration in U-Boot takes a long time since devices are probed one at a 1345fd2733eSSimon Glasstime, and each is given sufficient time to wake up and announce itself. The 1355fd2733eSSimon Glasstimeouts are set for the slowest device. 1365fd2733eSSimon Glass 1375fd2733eSSimon GlassUp to 127 devices can be on each bus. USB has four bus speeds: low 1385fd2733eSSimon Glass(1.5Mbps), full (12Mbps), high (480Mbps) which is only available with USB2 1395fd2733eSSimon Glassand newer (EHCI), and super (5Gbps) which is only available with USB3 and 1405fd2733eSSimon Glassnewer (XHCI). If you connect a super-speed device to a high-speed hub, you 1415fd2733eSSimon Glasswill only get high-speed. 1425fd2733eSSimon Glass 1435fd2733eSSimon Glass 1445fd2733eSSimon GlassUSB operations 1455fd2733eSSimon Glass-------------- 1465fd2733eSSimon Glass 1475fd2733eSSimon GlassAs before driver model, messages can be sent using submit_bulk_msg() and the 1485fd2733eSSimon Glasslike. These are now implemented by the USB uclass and route through the 1495fd2733eSSimon Glasscontroller drivers. Note that messages are not sent to the driver of the 1505fd2733eSSimon Glassdevice itself - i.e. they don't pass down the stack to the controller. 1515fd2733eSSimon GlassU-Boot simply finds the controller to which the device is attached, and sends 1525fd2733eSSimon Glassthe message there with an appropriate 'pipe' value so it can be addressed 1535fd2733eSSimon Glassproperly. Having said that, the USB device which should receive the message 1545fd2733eSSimon Glassis passed in to the driver methods, for use by sandbox. This design decision 1555fd2733eSSimon Glassis open for review and the code impact of changing it is small since the 1565fd2733eSSimon Glassmethods are typically implemented by the EHCI and XHCI stacks. 1575fd2733eSSimon Glass 1585fd2733eSSimon GlassController drivers (in UCLASS_USB) themselves provide methods for sending 1595fd2733eSSimon Glasseach message type. For XHCI an additional alloc_device() method is provided 1605fd2733eSSimon Glasssince XHCI needs to allocate a device context before it can even read the 1615fd2733eSSimon Glassdevice's descriptor. 1625fd2733eSSimon Glass 1635fd2733eSSimon GlassThese methods use a 'pipe' which is a collection of bit fields used to 1645fd2733eSSimon Glassdescribe the type of message, direction of transfer and the intended 1655fd2733eSSimon Glassrecipient (device number). 1665fd2733eSSimon Glass 1675fd2733eSSimon Glass 1685fd2733eSSimon GlassUSB Devices 1695fd2733eSSimon Glass----------- 1705fd2733eSSimon Glass 1715fd2733eSSimon GlassUSB devices are found using a simple algorithm which works through the 1725fd2733eSSimon Glassavailable hubs in a depth-first search. Devices can be in any uclass, but 1735fd2733eSSimon Glassare attached to a parent hub (or controller in the case of the root hub) and 1745fd2733eSSimon Glassso have parent data attached to them (this is struct usb_device). 1755fd2733eSSimon Glass 1765fd2733eSSimon GlassBy the time the device's probe() method is called, it is enumerated and is 1775fd2733eSSimon Glassready to talk to the host. 1785fd2733eSSimon Glass 1795fd2733eSSimon GlassThe enumeration process needs to work out which driver to attach to each USB 1805fd2733eSSimon Glassdevice. It does this by examining the device class, interface class, vendor 1815fd2733eSSimon GlassID, product ID, etc. See struct usb_driver_entry for how drivers are matched 1825fd2733eSSimon Glasswith USB devices - you can use the USB_DEVICE() macro to declare a USB 1835fd2733eSSimon Glassdriver. For example, usb_storage.c defines a USB_DEVICE() to handle storage 1845fd2733eSSimon Glassdevices, and it will be used for all USB devices which match. 1855fd2733eSSimon Glass 1865fd2733eSSimon Glass 1875fd2733eSSimon Glass 1885fd2733eSSimon GlassTechnical details on enumeration flow 1895fd2733eSSimon Glass------------------------------------- 1905fd2733eSSimon Glass 1915fd2733eSSimon GlassIt is useful to understand precisely how a USB bus is enumerating to avoid 1925fd2733eSSimon Glassconfusion when dealing with USB devices. 1935fd2733eSSimon Glass 1945fd2733eSSimon GlassDevice initialisation happens roughly like this: 1955fd2733eSSimon Glass 1965fd2733eSSimon Glass- At some point the 'usb start' command is run 1975fd2733eSSimon Glass- This calls usb_init() which works through each controller in turn 1985fd2733eSSimon Glass- The controller is probed(). This does no enumeration. 1995fd2733eSSimon Glass- Then usb_scan_bus() is called. This calls usb_scan_device() to scan the 2005fd2733eSSimon Glass(only) device that is attached to the controller - a root hub 2015fd2733eSSimon Glass- usb_scan_device() sets up a fake struct usb_device and calls 2025fd2733eSSimon Glassusb_setup_device(), passing the port number to be scanned, in this case port 2035fd2733eSSimon Glass0 2045fd2733eSSimon Glass- usb_setup_device() first calls usb_prepare_device() to set the device 2055fd2733eSSimon Glassaddress, then usb_select_config() to select the first configuration 2065fd2733eSSimon Glass- at this point the device is enumerated but we do not have a real struct 2075fd2733eSSimon Glassudevice for it. But we do have the descriptor in struct usb_device so we can 2085fd2733eSSimon Glassuse this to figure out what driver to use 2095fd2733eSSimon Glass- back in usb_scan_device(), we call usb_find_child() to try to find an 2105fd2733eSSimon Glassexisting device which matches the one we just found on the bus. This can 2115fd2733eSSimon Glasshappen if the device is mentioned in the device tree, or if we previously 2125fd2733eSSimon Glassscanned the bus and so the device was created before 2135fd2733eSSimon Glass- if usb_find_child() does not find an existing device, we call 2145fd2733eSSimon Glassusb_find_and_bind_driver() which tries to bind one 2155fd2733eSSimon Glass- usb_find_and_bind_driver() searches all available USB drivers (declared 2165fd2733eSSimon Glasswith USB_DEVICE()). If it finds a match it binds that driver to create a new 2175fd2733eSSimon Glassdevice. 2185fd2733eSSimon Glass- If it does not, it binds a generic driver. A generic driver is good enough 2195fd2733eSSimon Glassto allow access to the device (sending it packets, etc.) but all 2205fd2733eSSimon Glassfunctionality will need to be implemented outside the driver model. 2215fd2733eSSimon Glass- in any case, when usb_find_child() and/or usb_find_and_bind_driver() are 2225fd2733eSSimon Glassdone, we have a device with the correct uclass. At this point we want to 2235fd2733eSSimon Glassprobe the device 2245fd2733eSSimon Glass- first we store basic information about the new device (address, port, 2255fd2733eSSimon Glassspeed) in its parent platform data. We cannot store it its private data 2265fd2733eSSimon Glasssince that will not exist until the device is probed. 2275fd2733eSSimon Glass- then we call device_probe() which probes the device 2285fd2733eSSimon Glass- the first probe step is actually the USB controller's (or USB hubs's) 2295fd2733eSSimon Glasschild_pre_probe() method. This gets called before anything else and is 2305fd2733eSSimon Glassintended to set up a child device ready to be used with its parent bus. For 2315fd2733eSSimon GlassUSB this calls usb_child_pre_probe() which grabs the information that was 2325fd2733eSSimon Glassstored in the parent platform data and stores it in the parent private data 2335fd2733eSSimon Glass(which is struct usb_device, a real one this time). It then calls 2345fd2733eSSimon Glassusb_select_config() again to make sure that everything about the device is 2355fd2733eSSimon Glassset up 2365fd2733eSSimon Glass- note that we have called usb_select_config() twice. This is inefficient 2375fd2733eSSimon Glassbut the alternative is to store additional information in the platform data. 2385fd2733eSSimon GlassThe time taken is minimal and this way is simpler 2395fd2733eSSimon Glass- at this point the device is set up and ready for use so far as the USB 2405fd2733eSSimon Glasssubsystem is concerned 2415fd2733eSSimon Glass- the device's probe() method is then called. It can send messages and do 2425fd2733eSSimon Glasswhatever else it wants to make the device work. 2435fd2733eSSimon Glass 2445fd2733eSSimon GlassNote that the first device is always a root hub, and this must be scanned to 2455fd2733eSSimon Glassfind any devices. The above steps will have created a hub (UCLASS_USB_HUB), 2465fd2733eSSimon Glassgiven it address 1 and set the configuration. 2475fd2733eSSimon Glass 2485fd2733eSSimon GlassFor hubs, the hub uclass has a post_probe() method. This means that after 2495fd2733eSSimon Glassany hub is probed, the uclass gets to do some processing. In this case 2505fd2733eSSimon Glassusb_hub_post_probe() is called, and the following steps take place: 2515fd2733eSSimon Glass 2525fd2733eSSimon Glass- usb_hub_post_probe() calls usb_hub_scan() to scan the hub, which in turn 2535fd2733eSSimon Glasscalls usb_hub_configure() 2545fd2733eSSimon Glass- hub power is enabled 2555fd2733eSSimon Glass- we loop through each port on the hub, performing the same steps for each 2565fd2733eSSimon Glass- first, check if there is a device present. This happens in 2575fd2733eSSimon Glassusb_hub_port_connect_change(). If so, then usb_scan_device() is called to 2585fd2733eSSimon Glassscan the device, passing the appropriate port number. 2595fd2733eSSimon Glass- you will recognise usb_scan_device() from the steps above. It sets up the 2605fd2733eSSimon Glassdevice ready for use. If it is a hub, it will scan that hub before it 2615fd2733eSSimon Glasscontinues here (recursively, depth-first) 2625fd2733eSSimon Glass- once all hub ports are scanned in this way, the hub is ready for use and 2635fd2733eSSimon Glassall of its downstream devices also 2645fd2733eSSimon Glass- additional controllers are scanned in the same way 2655fd2733eSSimon Glass 2665fd2733eSSimon GlassThe above method has some nice properties: 2675fd2733eSSimon Glass 2685fd2733eSSimon Glass- the bus enumeration happens by virtue of driver model's natural device flow 2695fd2733eSSimon Glass- most logic is in the USB controller and hub uclasses; the actual device 2705fd2733eSSimon Glassdrivers do not need to know they are on a USB bus, at least so far as 2715fd2733eSSimon Glassenumeration goes 2725fd2733eSSimon Glass- hub scanning happens automatically after a hub is probed 2735fd2733eSSimon Glass 2745fd2733eSSimon Glass 2755fd2733eSSimon GlassHubs 2765fd2733eSSimon Glass---- 2775fd2733eSSimon Glass 2785fd2733eSSimon GlassUSB hubs are scanned as in the section above. While hubs have their own 2795fd2733eSSimon Glassuclass, they share some common elements with controllers: 2805fd2733eSSimon Glass 2815fd2733eSSimon Glass- they both attach private data to their children (struct usb_device, 282*bcbe3d15SSimon Glassaccessible for a child with dev_get_parent_priv(child)) 2835fd2733eSSimon Glass- they both use usb_child_pre_probe() to set up their children as proper USB 2845fd2733eSSimon Glassdevices 2855fd2733eSSimon Glass 2865fd2733eSSimon Glass 2875fd2733eSSimon GlassExample - Mass Storage 2885fd2733eSSimon Glass---------------------- 2895fd2733eSSimon Glass 2905fd2733eSSimon GlassAs an example of a USB device driver, see usb_storage.c. It uses its own 2915fd2733eSSimon Glassuclass and declares itself as follows: 2925fd2733eSSimon Glass 2935fd2733eSSimon GlassU_BOOT_DRIVER(usb_mass_storage) = { 2945fd2733eSSimon Glass .name = "usb_mass_storage", 2955fd2733eSSimon Glass .id = UCLASS_MASS_STORAGE, 2965fd2733eSSimon Glass .of_match = usb_mass_storage_ids, 2975fd2733eSSimon Glass .probe = usb_mass_storage_probe, 2985fd2733eSSimon Glass}; 2995fd2733eSSimon Glass 3005fd2733eSSimon Glassstatic const struct usb_device_id mass_storage_id_table[] = { 3015fd2733eSSimon Glass { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, 3025fd2733eSSimon Glass .bInterfaceClass = USB_CLASS_MASS_STORAGE}, 3035fd2733eSSimon Glass { } /* Terminating entry */ 3045fd2733eSSimon Glass}; 3055fd2733eSSimon Glass 3065fd2733eSSimon GlassUSB_DEVICE(usb_mass_storage, mass_storage_id_table); 3075fd2733eSSimon Glass 3085fd2733eSSimon GlassThe USB_DEVICE() macro attaches the given table of matching information to 3095fd2733eSSimon Glassthe given driver. Note that the driver is declared in U_BOOT_DRIVER() as 3105fd2733eSSimon Glass'usb_mass_storage' and this must match the first parameter of USB_DEVICE. 3115fd2733eSSimon Glass 3125fd2733eSSimon GlassWhen usb_find_and_bind_driver() is called on a USB device with the 3135fd2733eSSimon GlassbInterfaceClass value of USB_CLASS_MASS_STORAGE, it will automatically find 3145fd2733eSSimon Glassthis driver and use it. 3155fd2733eSSimon Glass 3165fd2733eSSimon Glass 3175fd2733eSSimon GlassCounter-example: USB Ethernet 3185fd2733eSSimon Glass----------------------------- 3195fd2733eSSimon Glass 3205fd2733eSSimon GlassAs an example of the old way of doing things, see usb_ether.c. When the bus 3215fd2733eSSimon Glassis scanned, all Ethernet devices will be created as generic USB devices (in 3225fd2733eSSimon Glassuclass UCLASS_USB_DEV_GENERIC). Then, when the scan is completed, 3235fd2733eSSimon Glassusb_host_eth_scan() will be called. This looks through all the devices on 3245fd2733eSSimon Glasseach bus and manually figures out which are Ethernet devices in the ways of 3255fd2733eSSimon Glassyore. 3265fd2733eSSimon Glass 3275fd2733eSSimon GlassIn fact, usb_ether should be moved to driver model. Each USB Ethernet driver 3285fd2733eSSimon Glass(e.g drivers/usb/eth/asix.c) should include a USB_DEVICE() declaration, so 3295fd2733eSSimon Glassthat it will be found as part of normal USB enumeration. Then, instead of a 3305fd2733eSSimon Glassgeneric USB driver, a real (driver-model-aware) driver will be used. Since 3315fd2733eSSimon GlassEthernet now supports driver model, this should be fairly easy to achieve, 3325fd2733eSSimon Glassand then usb_ether.c and the usb_host_eth_scan() will melt away. 3335fd2733eSSimon Glass 3345fd2733eSSimon Glass 3355fd2733eSSimon GlassSandbox 3365fd2733eSSimon Glass------- 3375fd2733eSSimon Glass 3385fd2733eSSimon GlassAll driver model uclasses must have tests and USB is no exception. To 3395fd2733eSSimon Glassachieve this, a sandbox USB controller is provided. This can make use of 3405fd2733eSSimon Glassemulation drivers which pretend to be USB devices. Emulations are provided 3415fd2733eSSimon Glassfor a hub and a flash stick. These are enough to create a pretend USB bus 3425fd2733eSSimon Glass(defined by the sandbox device tree sandbox.dts) which can be scanned and 3435fd2733eSSimon Glassused. 3445fd2733eSSimon Glass 3455fd2733eSSimon GlassTests in test/dm/usb.c make use of this feature. It allows much of the USB 3465fd2733eSSimon Glassstack to be tested without real hardware being needed. 3475fd2733eSSimon Glass 3485fd2733eSSimon GlassHere is an example device tree fragment: 3495fd2733eSSimon Glass 3505fd2733eSSimon Glass usb@1 { 3515fd2733eSSimon Glass compatible = "sandbox,usb"; 3525fd2733eSSimon Glass hub { 3535fd2733eSSimon Glass compatible = "usb-hub"; 3545fd2733eSSimon Glass usb,device-class = <USB_CLASS_HUB>; 3555fd2733eSSimon Glass hub-emul { 3565fd2733eSSimon Glass compatible = "sandbox,usb-hub"; 3575fd2733eSSimon Glass #address-cells = <1>; 3585fd2733eSSimon Glass #size-cells = <0>; 3595fd2733eSSimon Glass flash-stick { 3605fd2733eSSimon Glass reg = <0>; 3615fd2733eSSimon Glass compatible = "sandbox,usb-flash"; 3625fd2733eSSimon Glass sandbox,filepath = "flash.bin"; 3635fd2733eSSimon Glass }; 3645fd2733eSSimon Glass }; 3655fd2733eSSimon Glass }; 3665fd2733eSSimon Glass }; 3675fd2733eSSimon Glass 3685fd2733eSSimon GlassThis defines a single controller, containing a root hub (which is required). 3695fd2733eSSimon GlassThe hub is emulated by a hub emulator, and the emulated hub has a single 3705fd2733eSSimon Glassflash stick to emulate on one of its ports. 3715fd2733eSSimon Glass 3725fd2733eSSimon GlassWhen 'usb start' is used, the following 'dm tree' output will be available: 3735fd2733eSSimon Glass 3745fd2733eSSimon Glass usb [ + ] `-- usb@1 3755fd2733eSSimon Glass usb_hub [ + ] `-- hub 3765fd2733eSSimon Glass usb_emul [ + ] |-- hub-emul 3775fd2733eSSimon Glass usb_emul [ + ] | `-- flash-stick 3785fd2733eSSimon Glass usb_mass_st [ + ] `-- usb_mass_storage 3795fd2733eSSimon Glass 3805fd2733eSSimon Glass 3815fd2733eSSimon GlassThis may look confusing. Most of it mirrors the device tree, but the 3825fd2733eSSimon Glass'usb_mass_storage' device is not in the device tree. This is created by 3835fd2733eSSimon Glassusb_find_and_bind_driver() based on the USB_DRIVER in usb_storage.c. While 3845fd2733eSSimon Glass'flash-stick' is the emulation device, 'usb_mass_storage' is the real U-Boot 3855fd2733eSSimon GlassUSB device driver that talks to it. 3865fd2733eSSimon Glass 3875fd2733eSSimon Glass 3885fd2733eSSimon GlassFuture work 3895fd2733eSSimon Glass----------- 3905fd2733eSSimon Glass 3915fd2733eSSimon GlassIt is pretty uncommon to have a large USB bus with lots of hubs on an 3925fd2733eSSimon Glassembedded system. In fact anything other than a root hub is uncommon. Still 3935fd2733eSSimon Glassit would be possible to speed up enumeration in two ways: 3945fd2733eSSimon Glass 3955fd2733eSSimon Glass- breadth-first search would allow devices to be reset and probed in 3965fd2733eSSimon Glassparallel to some extent 3975fd2733eSSimon Glass- enumeration could be lazy, in the sense that we could enumerate just the 3985fd2733eSSimon Glassroot hub at first, then only progress to the next 'level' when a device is 3995fd2733eSSimon Glassused that we cannot find. This could be made easier if the devices were 4005fd2733eSSimon Glassstatically declared in the device tree (which is acceptable for production 4015fd2733eSSimon Glassboards where the same, known, things are on each bus). 4025fd2733eSSimon Glass 4035fd2733eSSimon GlassBut in common cases the current algorithm is sufficient. 4045fd2733eSSimon Glass 4055fd2733eSSimon GlassOther things that need doing: 4065fd2733eSSimon Glass- Convert usb_ether to use driver model as described above 4075fd2733eSSimon Glass- Test that keyboards work (and convert to driver model) 4085fd2733eSSimon Glass- Move the USB gadget framework to driver model 4095fd2733eSSimon Glass- Implement OHCI in driver model 4105fd2733eSSimon Glass- Implement USB PHYs in driver model 4115fd2733eSSimon Glass- Work out a clever way to provide lazy init for USB devices 4125fd2733eSSimon Glass 4135fd2733eSSimon Glass-- 4145fd2733eSSimon GlassSimon Glass <sjg@chromium.org> 4155fd2733eSSimon Glass23-Mar-15 416