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>
34 #include <asm/unaligned.h>
37 DECLARE_GLOBAL_DATA_PTR;
41 #include <asm/4xx_pci.h>
44 #define USB_BUFSIZ 512
46 /* TODO(sjg@chromium.org): Remove this when CONFIG_DM_USB is defined */
47 static struct usb_hub_device hub_dev[USB_MAX_HUB];
48 static int usb_hub_index;
50 __weak void usb_hub_reset_devices(int port)
55 static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
57 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
58 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
59 USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
62 static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
64 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
65 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
66 port, NULL, 0, USB_CNTL_TIMEOUT);
69 static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
71 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
72 USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
73 port, NULL, 0, USB_CNTL_TIMEOUT);
76 static int usb_get_hub_status(struct usb_device *dev, void *data)
78 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
79 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
80 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
83 int usb_get_port_status(struct usb_device *dev, int port, void *data)
85 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
86 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
87 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
91 static void usb_hub_power_on(struct usb_hub_device *hub)
94 struct usb_device *dev;
95 unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
100 debug("enabling power on all ports\n");
101 for (i = 0; i < dev->maxchild; i++) {
102 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
103 debug("port %d returns %lX\n", i + 1, dev->status);
107 * Wait for power to become stable,
108 * plus spec-defined max time for device to connect
109 * but allow this time to be increased via env variable as some
110 * devices break the spec and require longer warm-up times
112 env = getenv("usb_pgood_delay");
114 pgood_delay = max(pgood_delay,
115 (unsigned)simple_strtol(env, NULL, 0));
116 debug("pgood_delay=%dms\n", pgood_delay);
117 mdelay(pgood_delay + 1000);
120 void usb_hub_reset(void)
125 static struct usb_hub_device *usb_hub_allocate(void)
127 if (usb_hub_index < USB_MAX_HUB)
128 return &hub_dev[usb_hub_index++];
130 printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
136 static inline char *portspeed(int portstatus)
140 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
141 case USB_PORT_STAT_SUPER_SPEED:
142 speed_str = "5 Gb/s";
144 case USB_PORT_STAT_HIGH_SPEED:
145 speed_str = "480 Mb/s";
147 case USB_PORT_STAT_LOW_SPEED:
148 speed_str = "1.5 Mb/s";
151 speed_str = "12 Mb/s";
158 int legacy_hub_port_reset(struct usb_device *dev, int port,
159 unsigned short *portstat)
162 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
163 unsigned short portstatus, portchange;
166 debug("%s: resetting '%s' port %d...\n", __func__, dev->dev->name,
169 debug("%s: resetting port %d...\n", __func__, port + 1);
171 for (tries = 0; tries < MAX_TRIES; tries++) {
172 err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
178 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
179 debug("get_port_status failed status %lX\n",
183 portstatus = le16_to_cpu(portsts->wPortStatus);
184 portchange = le16_to_cpu(portsts->wPortChange);
186 debug("portstatus %x, change %x, %s\n", portstatus, portchange,
187 portspeed(portstatus));
189 debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
190 " USB_PORT_STAT_ENABLE %d\n",
191 (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
192 (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
193 (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
196 * Perhaps we should check for the following here:
197 * - C_CONNECTION hasn't been set.
198 * - CONNECTION is still set.
200 * Doing so would ensure that the device is still connected
201 * to the bus, and hasn't been unplugged or replaced while the
202 * USB bus reset was going on.
204 * However, if we do that, then (at least) a San Disk Ultra
205 * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
206 * Tegra Jetson TK1 board. For some reason, the device appears
207 * to briefly drop off the bus when this second bus reset is
208 * executed, yet if we retry this loop, it'll eventually come
209 * back after another reset or two.
212 if (portstatus & USB_PORT_STAT_ENABLE)
218 if (tries == MAX_TRIES) {
219 debug("Cannot enable port %i after %i retries, " \
220 "disabling port.\n", port + 1, MAX_TRIES);
221 debug("Maybe the USB cable is bad?\n");
225 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
226 *portstat = portstatus;
231 int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat)
233 struct usb_device *udev = dev_get_parentdata(dev);
235 return legacy_hub_port_reset(udev, port, portstat);
239 int usb_hub_port_connect_change(struct usb_device *dev, int port)
241 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
242 unsigned short portstatus;
246 ret = usb_get_port_status(dev, port + 1, portsts);
248 debug("get_port_status failed\n");
252 portstatus = le16_to_cpu(portsts->wPortStatus);
253 debug("portstatus %x, change %x, %s\n",
255 le16_to_cpu(portsts->wPortChange),
256 portspeed(portstatus));
258 /* Clear the connection change status */
259 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
261 /* Disconnect any existing devices under this port */
262 if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
263 (!(portstatus & USB_PORT_STAT_ENABLE))) ||
264 usb_device_has_child_on_port(dev, port)) {
265 debug("usb_disconnect(&hub->children[port]);\n");
266 /* Return now if nothing is connected */
267 if (!(portstatus & USB_PORT_STAT_CONNECTION))
273 ret = legacy_hub_port_reset(dev, port, &portstatus);
276 printf("cannot reset port %i!?\n", port + 1);
282 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
283 case USB_PORT_STAT_SUPER_SPEED:
284 speed = USB_SPEED_SUPER;
286 case USB_PORT_STAT_HIGH_SPEED:
287 speed = USB_SPEED_HIGH;
289 case USB_PORT_STAT_LOW_SPEED:
290 speed = USB_SPEED_LOW;
293 speed = USB_SPEED_FULL;
298 struct udevice *child;
300 ret = usb_scan_device(dev->dev, port + 1, speed, &child);
302 struct usb_device *usb;
304 ret = usb_alloc_new_device(dev->controller, &usb);
306 printf("cannot create new device: ret=%d", ret);
310 dev->children[port] = usb;
313 usb->portnr = port + 1;
314 /* Run it through the hoops (find a driver, etc) */
315 ret = usb_new_device(usb);
317 /* Woops, disable the port */
318 usb_free_device(dev->controller);
319 dev->children[port] = NULL;
323 debug("hub: disabling port %d\n", port + 1);
324 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
331 static int usb_hub_configure(struct usb_device *dev)
334 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
335 unsigned char *bitmap;
336 short hubCharacteristics;
337 struct usb_hub_descriptor *descriptor;
338 struct usb_hub_device *hub;
339 __maybe_unused struct usb_hub_status *hubsts;
342 /* "allocate" Hub device */
343 hub = usb_hub_allocate();
347 /* Get the the hub descriptor */
348 ret = usb_get_hub_descriptor(dev, buffer, 4);
350 debug("usb_hub_configure: failed to get hub " \
351 "descriptor, giving up %lX\n", dev->status);
354 descriptor = (struct usb_hub_descriptor *)buffer;
356 length = min_t(int, descriptor->bLength,
357 sizeof(struct usb_hub_descriptor));
359 ret = usb_get_hub_descriptor(dev, buffer, length);
361 debug("usb_hub_configure: failed to get hub " \
362 "descriptor 2nd giving up %lX\n", dev->status);
365 memcpy((unsigned char *)&hub->desc, buffer, length);
366 /* adjust 16bit values */
367 put_unaligned(le16_to_cpu(get_unaligned(
368 &descriptor->wHubCharacteristics)),
369 &hub->desc.wHubCharacteristics);
371 bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
372 /* devices not removable by default */
373 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
374 bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
375 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
377 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
378 hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
380 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
381 hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
383 dev->maxchild = descriptor->bNbrPorts;
384 debug("%d ports detected\n", dev->maxchild);
386 hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
387 switch (hubCharacteristics & HUB_CHAR_LPSM) {
389 debug("ganged power switching\n");
392 debug("individual port power switching\n");
396 debug("unknown reserved power switching mode\n");
400 if (hubCharacteristics & HUB_CHAR_COMPOUND)
401 debug("part of a compound device\n");
403 debug("standalone hub\n");
405 switch (hubCharacteristics & HUB_CHAR_OCPM) {
407 debug("global over-current protection\n");
410 debug("individual port over-current protection\n");
414 debug("no over-current protection\n");
418 debug("power on to power good time: %dms\n",
419 descriptor->bPwrOn2PwrGood * 2);
420 debug("hub controller current requirement: %dmA\n",
421 descriptor->bHubContrCurrent);
423 for (i = 0; i < dev->maxchild; i++)
424 debug("port %d is%s removable\n", i + 1,
425 hub->desc.DeviceRemovable[(i + 1) / 8] & \
426 (1 << ((i + 1) % 8)) ? " not" : "");
428 if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
429 debug("usb_hub_configure: failed to get Status - " \
430 "too long: %d\n", descriptor->bLength);
434 ret = usb_get_hub_status(dev, buffer);
436 debug("usb_hub_configure: failed to get Status %lX\n",
442 hubsts = (struct usb_hub_status *)buffer;
445 debug("get_hub_status returned status %X, change %X\n",
446 le16_to_cpu(hubsts->wHubStatus),
447 le16_to_cpu(hubsts->wHubChange));
448 debug("local power source is %s\n",
449 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
450 "lost (inactive)" : "good");
451 debug("%sover-current condition exists\n",
452 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
454 usb_hub_power_on(hub);
457 * Reset any devices that may be in a bad state when applying
458 * the power. This is a __weak function. Resetting of the devices
459 * should occur in the board file of the device.
461 for (i = 0; i < dev->maxchild; i++)
462 usb_hub_reset_devices(i + 1);
464 for (i = 0; i < dev->maxchild; i++) {
465 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
466 unsigned short portstatus, portchange;
468 ulong start = get_timer(0);
471 debug("\n\nScanning '%s' port %d\n", dev->dev->name, i + 1);
473 debug("\n\nScanning port %d\n", i + 1);
476 * Wait for (whichever finishes first)
477 * - A maximum of 10 seconds
478 * This is a purely observational value driven by connecting
479 * a few broken pen drives and taking the max * 1.5 approach
480 * - connection_change and connection state to report same
484 ret = usb_get_port_status(dev, i + 1, portsts);
486 debug("get_port_status failed\n");
490 portstatus = le16_to_cpu(portsts->wPortStatus);
491 portchange = le16_to_cpu(portsts->wPortChange);
493 /* No connection change happened, wait a bit more. */
494 if (!(portchange & USB_PORT_STAT_C_CONNECTION))
497 /* Test if the connection came up, and if so, exit. */
498 if (portstatus & USB_PORT_STAT_CONNECTION)
501 } while (get_timer(start) < CONFIG_SYS_HZ * 1);
506 debug("Port %d Status %X Change %X\n",
507 i + 1, portstatus, portchange);
509 if (portchange & USB_PORT_STAT_C_CONNECTION) {
510 debug("port %d connection change\n", i + 1);
511 usb_hub_port_connect_change(dev, i);
513 if (portchange & USB_PORT_STAT_C_ENABLE) {
514 debug("port %d enable change, status %x\n",
516 usb_clear_port_feature(dev, i + 1,
517 USB_PORT_FEAT_C_ENABLE);
519 * The following hack causes a ghost device problem
522 #ifndef CONFIG_USB_EHCI_FARADAY
523 /* EM interference sometimes causes bad shielded USB
524 * devices to be shutdown by the hub, this hack enables
525 * them again. Works at least with mouse driver */
526 if (!(portstatus & USB_PORT_STAT_ENABLE) &&
527 (portstatus & USB_PORT_STAT_CONNECTION) &&
528 usb_device_has_child_on_port(dev, i)) {
529 debug("already running port %i " \
530 "disabled by hub (EMI?), " \
531 "re-enabling...\n", i + 1);
532 usb_hub_port_connect_change(dev, i);
536 if (portstatus & USB_PORT_STAT_SUSPEND) {
537 debug("port %d suspend change\n", i + 1);
538 usb_clear_port_feature(dev, i + 1,
539 USB_PORT_FEAT_SUSPEND);
542 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
543 debug("port %d over-current change\n", i + 1);
544 usb_clear_port_feature(dev, i + 1,
545 USB_PORT_FEAT_C_OVER_CURRENT);
546 usb_hub_power_on(hub);
549 if (portchange & USB_PORT_STAT_C_RESET) {
550 debug("port %d reset change\n", i + 1);
551 usb_clear_port_feature(dev, i + 1,
552 USB_PORT_FEAT_C_RESET);
554 } /* end for i all ports */
559 static int usb_hub_check(struct usb_device *dev, int ifnum)
561 struct usb_interface *iface;
562 struct usb_endpoint_descriptor *ep = NULL;
564 iface = &dev->config.if_desc[ifnum];
566 if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
568 /* Some hubs have a subclass of 1, which AFAICT according to the */
569 /* specs is not defined, but it works */
570 if ((iface->desc.bInterfaceSubClass != 0) &&
571 (iface->desc.bInterfaceSubClass != 1))
573 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
574 if (iface->desc.bNumEndpoints != 1)
576 ep = &iface->ep_desc[0];
577 /* Output endpoint? Curiousier and curiousier.. */
578 if (!(ep->bEndpointAddress & USB_DIR_IN))
580 /* If it's not an interrupt endpoint, we'd better punt! */
581 if ((ep->bmAttributes & 3) != 3)
584 debug("USB hub found\n");
588 debug("USB hub not found: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n",
589 iface->desc.bInterfaceClass, iface->desc.bInterfaceSubClass,
590 iface->desc.bNumEndpoints);
592 debug(" bEndpointAddress=%#x, bmAttributes=%d",
593 ep->bEndpointAddress, ep->bmAttributes);
599 int usb_hub_probe(struct usb_device *dev, int ifnum)
603 ret = usb_hub_check(dev, ifnum);
606 ret = usb_hub_configure(dev);
611 int usb_hub_scan(struct udevice *hub)
613 struct usb_device *udev = dev_get_parentdata(hub);
615 return usb_hub_configure(udev);
618 static int usb_hub_post_bind(struct udevice *dev)
620 /* Scan the bus for devices */
621 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
624 static int usb_hub_post_probe(struct udevice *dev)
626 debug("%s\n", __func__);
627 return usb_hub_scan(dev);
630 static const struct udevice_id usb_hub_ids[] = {
631 { .compatible = "usb-hub" },
635 U_BOOT_DRIVER(usb_generic_hub) = {
637 .id = UCLASS_USB_HUB,
638 .of_match = usb_hub_ids,
639 .flags = DM_FLAG_ALLOC_PRIV_DMA,
642 UCLASS_DRIVER(usb_hub) = {
643 .id = UCLASS_USB_HUB,
645 .post_bind = usb_hub_post_bind,
646 .post_probe = usb_hub_post_probe,
647 .child_pre_probe = usb_child_pre_probe,
648 .per_child_auto_alloc_size = sizeof(struct usb_device),
649 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
652 static const struct usb_device_id hub_id_table[] = {
654 .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
655 .bDeviceClass = USB_CLASS_HUB
657 { } /* Terminating entry */
660 U_BOOT_USB_DEVICE(usb_generic_hub, hub_id_table);