2 * Most of this source has been derived from the Linux USB
4 * (C) Copyright Linus Torvalds 1999
5 * (C) Copyright Johannes Erdfelt 1999-2001
6 * (C) Copyright Andreas Gal 1999
7 * (C) Copyright Gregory P. Smith 1999
8 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
9 * (C) Copyright Randy Dunlap 2000
10 * (C) Copyright David Brownell 2000 (kernel hotplug, usb_device_id)
11 * (C) Copyright Yggdrasil Computing, Inc. 2000
12 * (usb_device_id matching changes by Adam J. Richter)
15 * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
17 * SPDX-License-Identifier: GPL-2.0+
20 /****************************************************************************
22 * Probes device for being a hub and configurate it
30 #include <asm/processor.h>
31 #include <asm/unaligned.h>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <asm/byteorder.h>
36 #include <asm/state.h>
38 #include <asm/unaligned.h>
40 DECLARE_GLOBAL_DATA_PTR;
44 #define USB_BUFSIZ 512
46 #define HUB_SHORT_RESET_TIME 20
47 #define HUB_LONG_RESET_TIME 200
49 #define PORT_OVERCURRENT_MAX_SCAN_COUNT 3
51 struct usb_device_scan {
52 struct usb_device *dev; /* USB hub device to scan */
53 struct usb_hub_device *hub; /* USB hub struct */
54 int port; /* USB port to scan */
55 struct list_head list;
58 /* TODO(sjg@chromium.org): Remove this when CONFIG_DM_USB is defined */
59 static struct usb_hub_device hub_dev[USB_MAX_HUB];
60 static int usb_hub_index;
61 static LIST_HEAD(usb_scan_list);
63 __weak void usb_hub_reset_devices(int port)
68 static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
70 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
71 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
72 USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
75 static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
77 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
78 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
79 port, NULL, 0, USB_CNTL_TIMEOUT);
82 static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
84 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
85 USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
86 port, NULL, 0, USB_CNTL_TIMEOUT);
89 static int usb_get_hub_status(struct usb_device *dev, void *data)
91 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
92 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
93 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
96 int usb_get_port_status(struct usb_device *dev, int port, void *data)
98 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
99 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
100 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
104 static void usb_hub_power_on(struct usb_hub_device *hub)
107 struct usb_device *dev;
108 unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
113 debug("enabling power on all ports\n");
114 for (i = 0; i < dev->maxchild; i++) {
115 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
116 debug("port %d returns %lX\n", i + 1, dev->status);
119 #ifdef CONFIG_SANDBOX
121 * Don't set timeout / delay values here. This results
122 * in these values still being reset to 0.
124 if (state_get_skip_delays())
129 * Wait for power to become stable,
130 * plus spec-defined max time for device to connect
131 * but allow this time to be increased via env variable as some
132 * devices break the spec and require longer warm-up times
134 env = getenv("usb_pgood_delay");
136 pgood_delay = max(pgood_delay,
137 (unsigned)simple_strtol(env, NULL, 0));
138 debug("pgood_delay=%dms\n", pgood_delay);
141 * Do a minimum delay of the larger value of 100ms or pgood_delay
142 * so that the power can stablize before the devices are queried
144 hub->query_delay = get_timer(0) + max(100, (int)pgood_delay);
147 * Record the power-on timeout here. The max. delay (timeout)
148 * will be done based on this value in the USB port loop in
149 * usb_hub_configure() later.
151 hub->connect_timeout = hub->query_delay + 1000;
152 debug("devnum=%d poweron: query_delay=%d connect_timeout=%d\n",
153 dev->devnum, max(100, (int)pgood_delay),
154 max(100, (int)pgood_delay) + 1000);
157 void usb_hub_reset(void)
161 /* Zero out global hub_dev in case its re-used again */
162 memset(hub_dev, 0, sizeof(hub_dev));
165 static struct usb_hub_device *usb_hub_allocate(void)
167 if (usb_hub_index < USB_MAX_HUB)
168 return &hub_dev[usb_hub_index++];
170 printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
176 static inline char *portspeed(int portstatus)
180 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
181 case USB_PORT_STAT_SUPER_SPEED:
182 speed_str = "5 Gb/s";
184 case USB_PORT_STAT_HIGH_SPEED:
185 speed_str = "480 Mb/s";
187 case USB_PORT_STAT_LOW_SPEED:
188 speed_str = "1.5 Mb/s";
191 speed_str = "12 Mb/s";
198 int legacy_hub_port_reset(struct usb_device *dev, int port,
199 unsigned short *portstat)
202 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
203 unsigned short portstatus, portchange;
204 int delay = HUB_SHORT_RESET_TIME; /* start with short reset delay */
207 debug("%s: resetting '%s' port %d...\n", __func__, dev->dev->name,
210 debug("%s: resetting port %d...\n", __func__, port + 1);
212 for (tries = 0; tries < MAX_TRIES; tries++) {
213 err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
219 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
220 debug("get_port_status failed status %lX\n",
224 portstatus = le16_to_cpu(portsts->wPortStatus);
225 portchange = le16_to_cpu(portsts->wPortChange);
227 debug("portstatus %x, change %x, %s\n", portstatus, portchange,
228 portspeed(portstatus));
230 debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
231 " USB_PORT_STAT_ENABLE %d\n",
232 (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
233 (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
234 (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
237 * Perhaps we should check for the following here:
238 * - C_CONNECTION hasn't been set.
239 * - CONNECTION is still set.
241 * Doing so would ensure that the device is still connected
242 * to the bus, and hasn't been unplugged or replaced while the
243 * USB bus reset was going on.
245 * However, if we do that, then (at least) a San Disk Ultra
246 * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
247 * Tegra Jetson TK1 board. For some reason, the device appears
248 * to briefly drop off the bus when this second bus reset is
249 * executed, yet if we retry this loop, it'll eventually come
250 * back after another reset or two.
253 if (portstatus & USB_PORT_STAT_ENABLE)
256 /* Switch to long reset delay for the next round */
257 delay = HUB_LONG_RESET_TIME;
260 if (tries == MAX_TRIES) {
261 debug("Cannot enable port %i after %i retries, " \
262 "disabling port.\n", port + 1, MAX_TRIES);
263 debug("Maybe the USB cable is bad?\n");
267 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
268 *portstat = portstatus;
273 int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat)
275 struct usb_device *udev = dev_get_parent_priv(dev);
277 return legacy_hub_port_reset(udev, port, portstat);
281 int usb_hub_port_connect_change(struct usb_device *dev, int port)
283 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
284 unsigned short portstatus;
288 ret = usb_get_port_status(dev, port + 1, portsts);
290 debug("get_port_status failed\n");
294 portstatus = le16_to_cpu(portsts->wPortStatus);
295 debug("portstatus %x, change %x, %s\n",
297 le16_to_cpu(portsts->wPortChange),
298 portspeed(portstatus));
300 /* Clear the connection change status */
301 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
303 /* Disconnect any existing devices under this port */
304 if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
305 (!(portstatus & USB_PORT_STAT_ENABLE))) ||
306 usb_device_has_child_on_port(dev, port)) {
307 debug("usb_disconnect(&hub->children[port]);\n");
308 /* Return now if nothing is connected */
309 if (!(portstatus & USB_PORT_STAT_CONNECTION))
314 ret = legacy_hub_port_reset(dev, port, &portstatus);
317 printf("cannot reset port %i!?\n", port + 1);
321 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
322 case USB_PORT_STAT_SUPER_SPEED:
323 speed = USB_SPEED_SUPER;
325 case USB_PORT_STAT_HIGH_SPEED:
326 speed = USB_SPEED_HIGH;
328 case USB_PORT_STAT_LOW_SPEED:
329 speed = USB_SPEED_LOW;
332 speed = USB_SPEED_FULL;
337 struct udevice *child;
339 ret = usb_scan_device(dev->dev, port + 1, speed, &child);
341 struct usb_device *usb;
343 ret = usb_alloc_new_device(dev->controller, &usb);
345 printf("cannot create new device: ret=%d", ret);
349 dev->children[port] = usb;
352 usb->portnr = port + 1;
353 /* Run it through the hoops (find a driver, etc) */
354 ret = usb_new_device(usb);
356 /* Woops, disable the port */
357 usb_free_device(dev->controller);
358 dev->children[port] = NULL;
362 debug("hub: disabling port %d\n", port + 1);
363 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
369 static int usb_scan_port(struct usb_device_scan *usb_scan)
371 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
372 unsigned short portstatus;
373 unsigned short portchange;
374 struct usb_device *dev;
375 struct usb_hub_device *hub;
384 * Don't talk to the device before the query delay is expired.
385 * This is needed for voltages to stabalize.
387 if (get_timer(0) < hub->query_delay)
390 ret = usb_get_port_status(dev, i + 1, portsts);
392 debug("get_port_status failed\n");
393 if (get_timer(0) >= hub->connect_timeout) {
394 debug("devnum=%d port=%d: timeout\n",
396 /* Remove this device from scanning list */
397 list_del(&usb_scan->list);
404 portstatus = le16_to_cpu(portsts->wPortStatus);
405 portchange = le16_to_cpu(portsts->wPortChange);
406 debug("Port %d Status %X Change %X\n", i + 1, portstatus, portchange);
408 /* No connection change happened, wait a bit more. */
409 if (!(portchange & USB_PORT_STAT_C_CONNECTION)) {
410 if (get_timer(0) >= hub->connect_timeout) {
411 debug("devnum=%d port=%d: timeout\n",
413 /* Remove this device from scanning list */
414 list_del(&usb_scan->list);
421 /* Test if the connection came up, and if not exit */
422 if (!(portstatus & USB_PORT_STAT_CONNECTION))
425 /* A new USB device is ready at this point */
426 debug("devnum=%d port=%d: USB dev found\n", dev->devnum, i + 1);
428 usb_hub_port_connect_change(dev, i);
430 if (portchange & USB_PORT_STAT_C_ENABLE) {
431 debug("port %d enable change, status %x\n", i + 1, portstatus);
432 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
434 * The following hack causes a ghost device problem
437 #ifndef CONFIG_USB_EHCI_FARADAY
439 * EM interference sometimes causes bad shielded USB
440 * devices to be shutdown by the hub, this hack enables
441 * them again. Works at least with mouse driver
443 if (!(portstatus & USB_PORT_STAT_ENABLE) &&
444 (portstatus & USB_PORT_STAT_CONNECTION) &&
445 usb_device_has_child_on_port(dev, i)) {
446 debug("already running port %i disabled by hub (EMI?), re-enabling...\n",
448 usb_hub_port_connect_change(dev, i);
453 if (portstatus & USB_PORT_STAT_SUSPEND) {
454 debug("port %d suspend change\n", i + 1);
455 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_SUSPEND);
458 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
459 debug("port %d over-current change\n", i + 1);
460 usb_clear_port_feature(dev, i + 1,
461 USB_PORT_FEAT_C_OVER_CURRENT);
462 /* Only power-on this one port */
463 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
464 hub->overcurrent_count[i]++;
467 * If the max-scan-count is not reached, return without removing
468 * the device from scan-list. This will re-issue a new scan.
470 if (hub->overcurrent_count[i] <=
471 PORT_OVERCURRENT_MAX_SCAN_COUNT)
474 /* Otherwise the device will get removed */
475 printf("Port %d over-current occurred %d times\n", i + 1,
476 hub->overcurrent_count[i]);
479 if (portchange & USB_PORT_STAT_C_RESET) {
480 debug("port %d reset change\n", i + 1);
481 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
485 * We're done with this device, so let's remove this device from
488 list_del(&usb_scan->list);
494 static int usb_device_list_scan(void)
496 struct usb_device_scan *usb_scan;
497 struct usb_device_scan *tmp;
501 /* Only run this loop once for each controller */
508 /* We're done, once the list is empty again */
509 if (list_empty(&usb_scan_list))
512 list_for_each_entry_safe(usb_scan, tmp, &usb_scan_list, list) {
516 ret = usb_scan_port(usb_scan);
524 * This USB controller has finished scanning all its connected
525 * USB devices. Set "running" back to 0, so that other USB controllers
526 * will scan their devices too.
533 static int usb_hub_configure(struct usb_device *dev)
536 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
537 unsigned char *bitmap;
538 short hubCharacteristics;
539 struct usb_hub_descriptor *descriptor;
540 struct usb_hub_device *hub;
541 __maybe_unused struct usb_hub_status *hubsts;
544 /* "allocate" Hub device */
545 hub = usb_hub_allocate();
549 /* Get the the hub descriptor */
550 ret = usb_get_hub_descriptor(dev, buffer, 4);
552 debug("usb_hub_configure: failed to get hub " \
553 "descriptor, giving up %lX\n", dev->status);
556 descriptor = (struct usb_hub_descriptor *)buffer;
558 length = min_t(int, descriptor->bLength,
559 sizeof(struct usb_hub_descriptor));
561 ret = usb_get_hub_descriptor(dev, buffer, length);
563 debug("usb_hub_configure: failed to get hub " \
564 "descriptor 2nd giving up %lX\n", dev->status);
567 memcpy((unsigned char *)&hub->desc, buffer, length);
568 /* adjust 16bit values */
569 put_unaligned(le16_to_cpu(get_unaligned(
570 &descriptor->wHubCharacteristics)),
571 &hub->desc.wHubCharacteristics);
573 bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
574 /* devices not removable by default */
575 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
576 bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
577 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
579 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
580 hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
582 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
583 hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
585 dev->maxchild = descriptor->bNbrPorts;
586 debug("%d ports detected\n", dev->maxchild);
588 hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
589 switch (hubCharacteristics & HUB_CHAR_LPSM) {
591 debug("ganged power switching\n");
594 debug("individual port power switching\n");
598 debug("unknown reserved power switching mode\n");
602 if (hubCharacteristics & HUB_CHAR_COMPOUND)
603 debug("part of a compound device\n");
605 debug("standalone hub\n");
607 switch (hubCharacteristics & HUB_CHAR_OCPM) {
609 debug("global over-current protection\n");
612 debug("individual port over-current protection\n");
616 debug("no over-current protection\n");
620 debug("power on to power good time: %dms\n",
621 descriptor->bPwrOn2PwrGood * 2);
622 debug("hub controller current requirement: %dmA\n",
623 descriptor->bHubContrCurrent);
625 for (i = 0; i < dev->maxchild; i++)
626 debug("port %d is%s removable\n", i + 1,
627 hub->desc.DeviceRemovable[(i + 1) / 8] & \
628 (1 << ((i + 1) % 8)) ? " not" : "");
630 if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
631 debug("usb_hub_configure: failed to get Status - " \
632 "too long: %d\n", descriptor->bLength);
636 ret = usb_get_hub_status(dev, buffer);
638 debug("usb_hub_configure: failed to get Status %lX\n",
644 hubsts = (struct usb_hub_status *)buffer;
647 debug("get_hub_status returned status %X, change %X\n",
648 le16_to_cpu(hubsts->wHubStatus),
649 le16_to_cpu(hubsts->wHubChange));
650 debug("local power source is %s\n",
651 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
652 "lost (inactive)" : "good");
653 debug("%sover-current condition exists\n",
654 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
656 usb_hub_power_on(hub);
659 * Reset any devices that may be in a bad state when applying
660 * the power. This is a __weak function. Resetting of the devices
661 * should occur in the board file of the device.
663 for (i = 0; i < dev->maxchild; i++)
664 usb_hub_reset_devices(i + 1);
667 * Only add the connected USB devices, including potential hubs,
668 * to a scanning list. This list will get scanned and devices that
669 * are detected (either via port connected or via port timeout)
670 * will get removed from this list. Scanning of the devices on this
671 * list will continue until all devices are removed.
673 for (i = 0; i < dev->maxchild; i++) {
674 struct usb_device_scan *usb_scan;
676 usb_scan = calloc(1, sizeof(*usb_scan));
678 printf("Can't allocate memory for USB device!\n");
684 list_add_tail(&usb_scan->list, &usb_scan_list);
688 * And now call the scanning code which loops over the generated list
690 ret = usb_device_list_scan();
695 static int usb_hub_check(struct usb_device *dev, int ifnum)
697 struct usb_interface *iface;
698 struct usb_endpoint_descriptor *ep = NULL;
700 iface = &dev->config.if_desc[ifnum];
702 if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
704 /* Some hubs have a subclass of 1, which AFAICT according to the */
705 /* specs is not defined, but it works */
706 if ((iface->desc.bInterfaceSubClass != 0) &&
707 (iface->desc.bInterfaceSubClass != 1))
709 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
710 if (iface->desc.bNumEndpoints != 1)
712 ep = &iface->ep_desc[0];
713 /* Output endpoint? Curiousier and curiousier.. */
714 if (!(ep->bEndpointAddress & USB_DIR_IN))
716 /* If it's not an interrupt endpoint, we'd better punt! */
717 if ((ep->bmAttributes & 3) != 3)
720 debug("USB hub found\n");
724 debug("USB hub not found: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n",
725 iface->desc.bInterfaceClass, iface->desc.bInterfaceSubClass,
726 iface->desc.bNumEndpoints);
728 debug(" bEndpointAddress=%#x, bmAttributes=%d",
729 ep->bEndpointAddress, ep->bmAttributes);
735 int usb_hub_probe(struct usb_device *dev, int ifnum)
739 ret = usb_hub_check(dev, ifnum);
742 ret = usb_hub_configure(dev);
747 int usb_hub_scan(struct udevice *hub)
749 struct usb_device *udev = dev_get_parent_priv(hub);
751 return usb_hub_configure(udev);
754 static int usb_hub_post_probe(struct udevice *dev)
756 debug("%s\n", __func__);
757 return usb_hub_scan(dev);
760 static const struct udevice_id usb_hub_ids[] = {
761 { .compatible = "usb-hub" },
765 U_BOOT_DRIVER(usb_generic_hub) = {
767 .id = UCLASS_USB_HUB,
768 .of_match = usb_hub_ids,
769 .flags = DM_FLAG_ALLOC_PRIV_DMA,
772 UCLASS_DRIVER(usb_hub) = {
773 .id = UCLASS_USB_HUB,
775 .post_bind = dm_scan_fdt_dev,
776 .post_probe = usb_hub_post_probe,
777 .child_pre_probe = usb_child_pre_probe,
778 .per_child_auto_alloc_size = sizeof(struct usb_device),
779 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
782 static const struct usb_device_id hub_id_table[] = {
784 .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
785 .bDeviceClass = USB_CLASS_HUB
787 { } /* Terminating entry */
790 U_BOOT_USB_DEVICE(usb_generic_hub, hub_id_table);