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 <asm/byteorder.h>
35 #include <asm/state.h>
37 #include <asm/unaligned.h>
40 DECLARE_GLOBAL_DATA_PTR;
44 #include <asm/4xx_pci.h>
47 #define USB_BUFSIZ 512
49 #define HUB_SHORT_RESET_TIME 20
50 #define HUB_LONG_RESET_TIME 200
52 /* TODO(sjg@chromium.org): Remove this when CONFIG_DM_USB is defined */
53 static struct usb_hub_device hub_dev[USB_MAX_HUB];
54 static int usb_hub_index;
56 __weak void usb_hub_reset_devices(int port)
61 static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
63 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
64 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
65 USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
68 static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
70 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
71 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
72 port, NULL, 0, USB_CNTL_TIMEOUT);
75 static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
77 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
78 USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
79 port, NULL, 0, USB_CNTL_TIMEOUT);
82 static int usb_get_hub_status(struct usb_device *dev, void *data)
84 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
85 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
86 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
89 int usb_get_port_status(struct usb_device *dev, int port, void *data)
91 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
92 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
93 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
97 static void usb_hub_power_on(struct usb_hub_device *hub)
100 struct usb_device *dev;
101 unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
106 debug("enabling power on all ports\n");
107 for (i = 0; i < dev->maxchild; i++) {
108 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
109 debug("port %d returns %lX\n", i + 1, dev->status);
113 * Wait for power to become stable,
114 * plus spec-defined max time for device to connect
115 * but allow this time to be increased via env variable as some
116 * devices break the spec and require longer warm-up times
118 env = getenv("usb_pgood_delay");
120 pgood_delay = max(pgood_delay,
121 (unsigned)simple_strtol(env, NULL, 0));
122 debug("pgood_delay=%dms\n", pgood_delay);
123 mdelay(pgood_delay + 1000);
126 void usb_hub_reset(void)
131 static struct usb_hub_device *usb_hub_allocate(void)
133 if (usb_hub_index < USB_MAX_HUB)
134 return &hub_dev[usb_hub_index++];
136 printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
142 static inline char *portspeed(int portstatus)
146 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
147 case USB_PORT_STAT_SUPER_SPEED:
148 speed_str = "5 Gb/s";
150 case USB_PORT_STAT_HIGH_SPEED:
151 speed_str = "480 Mb/s";
153 case USB_PORT_STAT_LOW_SPEED:
154 speed_str = "1.5 Mb/s";
157 speed_str = "12 Mb/s";
164 int legacy_hub_port_reset(struct usb_device *dev, int port,
165 unsigned short *portstat)
168 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
169 unsigned short portstatus, portchange;
170 int delay = HUB_SHORT_RESET_TIME; /* start with short reset delay */
173 debug("%s: resetting '%s' port %d...\n", __func__, dev->dev->name,
176 debug("%s: resetting port %d...\n", __func__, port + 1);
178 for (tries = 0; tries < MAX_TRIES; tries++) {
179 err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
185 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
186 debug("get_port_status failed status %lX\n",
190 portstatus = le16_to_cpu(portsts->wPortStatus);
191 portchange = le16_to_cpu(portsts->wPortChange);
193 debug("portstatus %x, change %x, %s\n", portstatus, portchange,
194 portspeed(portstatus));
196 debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
197 " USB_PORT_STAT_ENABLE %d\n",
198 (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
199 (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
200 (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
203 * Perhaps we should check for the following here:
204 * - C_CONNECTION hasn't been set.
205 * - CONNECTION is still set.
207 * Doing so would ensure that the device is still connected
208 * to the bus, and hasn't been unplugged or replaced while the
209 * USB bus reset was going on.
211 * However, if we do that, then (at least) a San Disk Ultra
212 * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
213 * Tegra Jetson TK1 board. For some reason, the device appears
214 * to briefly drop off the bus when this second bus reset is
215 * executed, yet if we retry this loop, it'll eventually come
216 * back after another reset or two.
219 if (portstatus & USB_PORT_STAT_ENABLE)
222 /* Switch to long reset delay for the next round */
223 delay = HUB_LONG_RESET_TIME;
226 if (tries == MAX_TRIES) {
227 debug("Cannot enable port %i after %i retries, " \
228 "disabling port.\n", port + 1, MAX_TRIES);
229 debug("Maybe the USB cable is bad?\n");
233 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
234 *portstat = portstatus;
239 int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat)
241 struct usb_device *udev = dev_get_parent_priv(dev);
243 return legacy_hub_port_reset(udev, port, portstat);
247 int usb_hub_port_connect_change(struct usb_device *dev, int port)
249 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
250 unsigned short portstatus;
254 ret = usb_get_port_status(dev, port + 1, portsts);
256 debug("get_port_status failed\n");
260 portstatus = le16_to_cpu(portsts->wPortStatus);
261 debug("portstatus %x, change %x, %s\n",
263 le16_to_cpu(portsts->wPortChange),
264 portspeed(portstatus));
266 /* Clear the connection change status */
267 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
269 /* Disconnect any existing devices under this port */
270 if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
271 (!(portstatus & USB_PORT_STAT_ENABLE))) ||
272 usb_device_has_child_on_port(dev, port)) {
273 debug("usb_disconnect(&hub->children[port]);\n");
274 /* Return now if nothing is connected */
275 if (!(portstatus & USB_PORT_STAT_CONNECTION))
281 ret = legacy_hub_port_reset(dev, port, &portstatus);
284 printf("cannot reset port %i!?\n", port + 1);
290 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
291 case USB_PORT_STAT_SUPER_SPEED:
292 speed = USB_SPEED_SUPER;
294 case USB_PORT_STAT_HIGH_SPEED:
295 speed = USB_SPEED_HIGH;
297 case USB_PORT_STAT_LOW_SPEED:
298 speed = USB_SPEED_LOW;
301 speed = USB_SPEED_FULL;
306 struct udevice *child;
308 ret = usb_scan_device(dev->dev, port + 1, speed, &child);
310 struct usb_device *usb;
312 ret = usb_alloc_new_device(dev->controller, &usb);
314 printf("cannot create new device: ret=%d", ret);
318 dev->children[port] = usb;
321 usb->portnr = port + 1;
322 /* Run it through the hoops (find a driver, etc) */
323 ret = usb_new_device(usb);
325 /* Woops, disable the port */
326 usb_free_device(dev->controller);
327 dev->children[port] = NULL;
331 debug("hub: disabling port %d\n", port + 1);
332 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
339 static int usb_hub_configure(struct usb_device *dev)
342 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
343 unsigned char *bitmap;
344 short hubCharacteristics;
345 struct usb_hub_descriptor *descriptor;
346 struct usb_hub_device *hub;
347 __maybe_unused struct usb_hub_status *hubsts;
350 /* "allocate" Hub device */
351 hub = usb_hub_allocate();
355 /* Get the the hub descriptor */
356 ret = usb_get_hub_descriptor(dev, buffer, 4);
358 debug("usb_hub_configure: failed to get hub " \
359 "descriptor, giving up %lX\n", dev->status);
362 descriptor = (struct usb_hub_descriptor *)buffer;
364 length = min_t(int, descriptor->bLength,
365 sizeof(struct usb_hub_descriptor));
367 ret = usb_get_hub_descriptor(dev, buffer, length);
369 debug("usb_hub_configure: failed to get hub " \
370 "descriptor 2nd giving up %lX\n", dev->status);
373 memcpy((unsigned char *)&hub->desc, buffer, length);
374 /* adjust 16bit values */
375 put_unaligned(le16_to_cpu(get_unaligned(
376 &descriptor->wHubCharacteristics)),
377 &hub->desc.wHubCharacteristics);
379 bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
380 /* devices not removable by default */
381 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
382 bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
383 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
385 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
386 hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
388 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
389 hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
391 dev->maxchild = descriptor->bNbrPorts;
392 debug("%d ports detected\n", dev->maxchild);
394 hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
395 switch (hubCharacteristics & HUB_CHAR_LPSM) {
397 debug("ganged power switching\n");
400 debug("individual port power switching\n");
404 debug("unknown reserved power switching mode\n");
408 if (hubCharacteristics & HUB_CHAR_COMPOUND)
409 debug("part of a compound device\n");
411 debug("standalone hub\n");
413 switch (hubCharacteristics & HUB_CHAR_OCPM) {
415 debug("global over-current protection\n");
418 debug("individual port over-current protection\n");
422 debug("no over-current protection\n");
426 debug("power on to power good time: %dms\n",
427 descriptor->bPwrOn2PwrGood * 2);
428 debug("hub controller current requirement: %dmA\n",
429 descriptor->bHubContrCurrent);
431 for (i = 0; i < dev->maxchild; i++)
432 debug("port %d is%s removable\n", i + 1,
433 hub->desc.DeviceRemovable[(i + 1) / 8] & \
434 (1 << ((i + 1) % 8)) ? " not" : "");
436 if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
437 debug("usb_hub_configure: failed to get Status - " \
438 "too long: %d\n", descriptor->bLength);
442 ret = usb_get_hub_status(dev, buffer);
444 debug("usb_hub_configure: failed to get Status %lX\n",
450 hubsts = (struct usb_hub_status *)buffer;
453 debug("get_hub_status returned status %X, change %X\n",
454 le16_to_cpu(hubsts->wHubStatus),
455 le16_to_cpu(hubsts->wHubChange));
456 debug("local power source is %s\n",
457 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
458 "lost (inactive)" : "good");
459 debug("%sover-current condition exists\n",
460 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
462 usb_hub_power_on(hub);
465 * Reset any devices that may be in a bad state when applying
466 * the power. This is a __weak function. Resetting of the devices
467 * should occur in the board file of the device.
469 for (i = 0; i < dev->maxchild; i++)
470 usb_hub_reset_devices(i + 1);
472 for (i = 0; i < dev->maxchild; i++) {
473 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
474 unsigned short portstatus, portchange;
476 ulong start = get_timer(0);
477 uint delay = CONFIG_SYS_HZ;
479 #ifdef CONFIG_SANDBOX
480 if (state_get_skip_delays())
484 debug("\n\nScanning '%s' port %d\n", dev->dev->name, i + 1);
486 debug("\n\nScanning port %d\n", i + 1);
489 * Wait for (whichever finishes first)
490 * - A maximum of 10 seconds
491 * This is a purely observational value driven by connecting
492 * a few broken pen drives and taking the max * 1.5 approach
493 * - connection_change and connection state to report same
497 ret = usb_get_port_status(dev, i + 1, portsts);
499 debug("get_port_status failed\n");
503 portstatus = le16_to_cpu(portsts->wPortStatus);
504 portchange = le16_to_cpu(portsts->wPortChange);
506 /* No connection change happened, wait a bit more. */
507 if (!(portchange & USB_PORT_STAT_C_CONNECTION))
510 /* Test if the connection came up, and if so, exit. */
511 if (portstatus & USB_PORT_STAT_CONNECTION)
514 } while (get_timer(start) < delay);
519 debug("Port %d Status %X Change %X\n",
520 i + 1, portstatus, portchange);
522 if (portchange & USB_PORT_STAT_C_CONNECTION) {
523 debug("port %d connection change\n", i + 1);
524 usb_hub_port_connect_change(dev, i);
526 if (portchange & USB_PORT_STAT_C_ENABLE) {
527 debug("port %d enable change, status %x\n",
529 usb_clear_port_feature(dev, i + 1,
530 USB_PORT_FEAT_C_ENABLE);
532 * The following hack causes a ghost device problem
535 #ifndef CONFIG_USB_EHCI_FARADAY
536 /* EM interference sometimes causes bad shielded USB
537 * devices to be shutdown by the hub, this hack enables
538 * them again. Works at least with mouse driver */
539 if (!(portstatus & USB_PORT_STAT_ENABLE) &&
540 (portstatus & USB_PORT_STAT_CONNECTION) &&
541 usb_device_has_child_on_port(dev, i)) {
542 debug("already running port %i " \
543 "disabled by hub (EMI?), " \
544 "re-enabling...\n", i + 1);
545 usb_hub_port_connect_change(dev, i);
549 if (portstatus & USB_PORT_STAT_SUSPEND) {
550 debug("port %d suspend change\n", i + 1);
551 usb_clear_port_feature(dev, i + 1,
552 USB_PORT_FEAT_SUSPEND);
555 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
556 debug("port %d over-current change\n", i + 1);
557 usb_clear_port_feature(dev, i + 1,
558 USB_PORT_FEAT_C_OVER_CURRENT);
559 usb_hub_power_on(hub);
562 if (portchange & USB_PORT_STAT_C_RESET) {
563 debug("port %d reset change\n", i + 1);
564 usb_clear_port_feature(dev, i + 1,
565 USB_PORT_FEAT_C_RESET);
567 } /* end for i all ports */
572 static int usb_hub_check(struct usb_device *dev, int ifnum)
574 struct usb_interface *iface;
575 struct usb_endpoint_descriptor *ep = NULL;
577 iface = &dev->config.if_desc[ifnum];
579 if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
581 /* Some hubs have a subclass of 1, which AFAICT according to the */
582 /* specs is not defined, but it works */
583 if ((iface->desc.bInterfaceSubClass != 0) &&
584 (iface->desc.bInterfaceSubClass != 1))
586 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
587 if (iface->desc.bNumEndpoints != 1)
589 ep = &iface->ep_desc[0];
590 /* Output endpoint? Curiousier and curiousier.. */
591 if (!(ep->bEndpointAddress & USB_DIR_IN))
593 /* If it's not an interrupt endpoint, we'd better punt! */
594 if ((ep->bmAttributes & 3) != 3)
597 debug("USB hub found\n");
601 debug("USB hub not found: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n",
602 iface->desc.bInterfaceClass, iface->desc.bInterfaceSubClass,
603 iface->desc.bNumEndpoints);
605 debug(" bEndpointAddress=%#x, bmAttributes=%d",
606 ep->bEndpointAddress, ep->bmAttributes);
612 int usb_hub_probe(struct usb_device *dev, int ifnum)
616 ret = usb_hub_check(dev, ifnum);
619 ret = usb_hub_configure(dev);
624 int usb_hub_scan(struct udevice *hub)
626 struct usb_device *udev = dev_get_parent_priv(hub);
628 return usb_hub_configure(udev);
631 static int usb_hub_post_bind(struct udevice *dev)
633 /* Scan the bus for devices */
634 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
637 static int usb_hub_post_probe(struct udevice *dev)
639 debug("%s\n", __func__);
640 return usb_hub_scan(dev);
643 static const struct udevice_id usb_hub_ids[] = {
644 { .compatible = "usb-hub" },
648 U_BOOT_DRIVER(usb_generic_hub) = {
650 .id = UCLASS_USB_HUB,
651 .of_match = usb_hub_ids,
652 .flags = DM_FLAG_ALLOC_PRIV_DMA,
655 UCLASS_DRIVER(usb_hub) = {
656 .id = UCLASS_USB_HUB,
658 .post_bind = usb_hub_post_bind,
659 .post_probe = usb_hub_post_probe,
660 .child_pre_probe = usb_child_pre_probe,
661 .per_child_auto_alloc_size = sizeof(struct usb_device),
662 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
665 static const struct usb_device_id hub_id_table[] = {
667 .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
668 .bDeviceClass = USB_CLASS_HUB
670 { } /* Terminating entry */
673 U_BOOT_USB_DEVICE(usb_generic_hub, hub_id_table);