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
29 #include <asm/processor.h>
30 #include <asm/unaligned.h>
31 #include <linux/ctype.h>
32 #include <asm/byteorder.h>
33 #include <asm/unaligned.h>
36 DECLARE_GLOBAL_DATA_PTR
;
40 #include <asm/4xx_pci.h>
43 #define USB_BUFSIZ 512
45 /* TODO(sjg@chromium.org): Remove this when CONFIG_DM_USB is defined */
46 static struct usb_hub_device hub_dev
[USB_MAX_HUB
];
47 static int usb_hub_index
;
49 __weak
void usb_hub_reset_devices(int port
)
54 static int usb_get_hub_descriptor(struct usb_device
*dev
, void *data
, int size
)
56 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
57 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
| USB_RT_HUB
,
58 USB_DT_HUB
<< 8, 0, data
, size
, USB_CNTL_TIMEOUT
);
61 static int usb_clear_port_feature(struct usb_device
*dev
, int port
, int feature
)
63 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
64 USB_REQ_CLEAR_FEATURE
, USB_RT_PORT
, feature
,
65 port
, NULL
, 0, USB_CNTL_TIMEOUT
);
68 static int usb_set_port_feature(struct usb_device
*dev
, int port
, int feature
)
70 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
71 USB_REQ_SET_FEATURE
, USB_RT_PORT
, feature
,
72 port
, NULL
, 0, USB_CNTL_TIMEOUT
);
75 static int usb_get_hub_status(struct usb_device
*dev
, void *data
)
77 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
78 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_HUB
, 0, 0,
79 data
, sizeof(struct usb_hub_status
), USB_CNTL_TIMEOUT
);
82 int usb_get_port_status(struct usb_device
*dev
, int port
, void *data
)
84 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
85 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RT_PORT
, 0, port
,
86 data
, sizeof(struct usb_hub_status
), USB_CNTL_TIMEOUT
);
90 static void usb_hub_power_on(struct usb_hub_device
*hub
)
93 struct usb_device
*dev
;
94 unsigned pgood_delay
= hub
->desc
.bPwrOn2PwrGood
* 2;
99 debug("enabling power on all ports\n");
100 for (i
= 0; i
< dev
->maxchild
; i
++) {
101 usb_set_port_feature(dev
, i
+ 1, USB_PORT_FEAT_POWER
);
102 debug("port %d returns %lX\n", i
+ 1, dev
->status
);
106 * Wait for power to become stable,
107 * plus spec-defined max time for device to connect
108 * but allow this time to be increased via env variable as some
109 * devices break the spec and require longer warm-up times
111 env
= getenv("usb_pgood_delay");
113 pgood_delay
= max(pgood_delay
,
114 (unsigned)simple_strtol(env
, NULL
, 0));
115 debug("pgood_delay=%dms\n", pgood_delay
);
116 mdelay(pgood_delay
+ 1000);
119 void usb_hub_reset(void)
124 static struct usb_hub_device
*usb_hub_allocate(void)
126 if (usb_hub_index
< USB_MAX_HUB
)
127 return &hub_dev
[usb_hub_index
++];
129 printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB
);
135 static inline char *portspeed(int portstatus
)
139 switch (portstatus
& USB_PORT_STAT_SPEED_MASK
) {
140 case USB_PORT_STAT_SUPER_SPEED
:
141 speed_str
= "5 Gb/s";
143 case USB_PORT_STAT_HIGH_SPEED
:
144 speed_str
= "480 Mb/s";
146 case USB_PORT_STAT_LOW_SPEED
:
147 speed_str
= "1.5 Mb/s";
150 speed_str
= "12 Mb/s";
157 int legacy_hub_port_reset(struct usb_device
*dev
, int port
,
158 unsigned short *portstat
)
161 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status
, portsts
, 1);
162 unsigned short portstatus
, portchange
;
165 debug("%s: resetting '%s' port %d...\n", __func__
, dev
->dev
->name
,
168 debug("%s: resetting port %d...\n", __func__
, port
+ 1);
170 for (tries
= 0; tries
< MAX_TRIES
; tries
++) {
171 err
= usb_set_port_feature(dev
, port
+ 1, USB_PORT_FEAT_RESET
);
177 if (usb_get_port_status(dev
, port
+ 1, portsts
) < 0) {
178 debug("get_port_status failed status %lX\n",
182 portstatus
= le16_to_cpu(portsts
->wPortStatus
);
183 portchange
= le16_to_cpu(portsts
->wPortChange
);
185 debug("portstatus %x, change %x, %s\n", portstatus
, portchange
,
186 portspeed(portstatus
));
188 debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
189 " USB_PORT_STAT_ENABLE %d\n",
190 (portchange
& USB_PORT_STAT_C_CONNECTION
) ? 1 : 0,
191 (portstatus
& USB_PORT_STAT_CONNECTION
) ? 1 : 0,
192 (portstatus
& USB_PORT_STAT_ENABLE
) ? 1 : 0);
195 * Perhaps we should check for the following here:
196 * - C_CONNECTION hasn't been set.
197 * - CONNECTION is still set.
199 * Doing so would ensure that the device is still connected
200 * to the bus, and hasn't been unplugged or replaced while the
201 * USB bus reset was going on.
203 * However, if we do that, then (at least) a San Disk Ultra
204 * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
205 * Tegra Jetson TK1 board. For some reason, the device appears
206 * to briefly drop off the bus when this second bus reset is
207 * executed, yet if we retry this loop, it'll eventually come
208 * back after another reset or two.
211 if (portstatus
& USB_PORT_STAT_ENABLE
)
217 if (tries
== MAX_TRIES
) {
218 debug("Cannot enable port %i after %i retries, " \
219 "disabling port.\n", port
+ 1, MAX_TRIES
);
220 debug("Maybe the USB cable is bad?\n");
224 usb_clear_port_feature(dev
, port
+ 1, USB_PORT_FEAT_C_RESET
);
225 *portstat
= portstatus
;
230 int hub_port_reset(struct udevice
*dev
, int port
, unsigned short *portstat
)
232 struct usb_device
*udev
= dev_get_parentdata(dev
);
234 return legacy_hub_port_reset(udev
, port
, portstat
);
238 int usb_hub_port_connect_change(struct usb_device
*dev
, int port
)
240 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status
, portsts
, 1);
241 unsigned short portstatus
;
245 ret
= usb_get_port_status(dev
, port
+ 1, portsts
);
247 debug("get_port_status failed\n");
251 portstatus
= le16_to_cpu(portsts
->wPortStatus
);
252 debug("portstatus %x, change %x, %s\n",
254 le16_to_cpu(portsts
->wPortChange
),
255 portspeed(portstatus
));
257 /* Clear the connection change status */
258 usb_clear_port_feature(dev
, port
+ 1, USB_PORT_FEAT_C_CONNECTION
);
260 /* Disconnect any existing devices under this port */
261 if (((!(portstatus
& USB_PORT_STAT_CONNECTION
)) &&
262 (!(portstatus
& USB_PORT_STAT_ENABLE
))) ||
263 usb_device_has_child_on_port(dev
, port
)) {
264 debug("usb_disconnect(&hub->children[port]);\n");
265 /* Return now if nothing is connected */
266 if (!(portstatus
& USB_PORT_STAT_CONNECTION
))
272 ret
= legacy_hub_port_reset(dev
, port
, &portstatus
);
275 printf("cannot reset port %i!?\n", port
+ 1);
281 switch (portstatus
& USB_PORT_STAT_SPEED_MASK
) {
282 case USB_PORT_STAT_SUPER_SPEED
:
283 speed
= USB_SPEED_SUPER
;
285 case USB_PORT_STAT_HIGH_SPEED
:
286 speed
= USB_SPEED_HIGH
;
288 case USB_PORT_STAT_LOW_SPEED
:
289 speed
= USB_SPEED_LOW
;
292 speed
= USB_SPEED_FULL
;
297 struct udevice
*child
;
299 ret
= usb_scan_device(dev
->dev
, port
+ 1, speed
, &child
);
301 struct usb_device
*usb
;
303 ret
= usb_alloc_new_device(dev
->controller
, &usb
);
305 printf("cannot create new device: ret=%d", ret
);
309 dev
->children
[port
] = usb
;
312 usb
->portnr
= port
+ 1;
313 /* Run it through the hoops (find a driver, etc) */
314 ret
= usb_new_device(usb
);
316 /* Woops, disable the port */
317 usb_free_device(dev
->controller
);
318 dev
->children
[port
] = NULL
;
322 debug("hub: disabling port %d\n", port
+ 1);
323 usb_clear_port_feature(dev
, port
+ 1, USB_PORT_FEAT_ENABLE
);
330 static int usb_hub_configure(struct usb_device
*dev
)
333 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer
, USB_BUFSIZ
);
334 unsigned char *bitmap
;
335 short hubCharacteristics
;
336 struct usb_hub_descriptor
*descriptor
;
337 struct usb_hub_device
*hub
;
338 __maybe_unused
struct usb_hub_status
*hubsts
;
341 /* "allocate" Hub device */
342 hub
= usb_hub_allocate();
346 /* Get the the hub descriptor */
347 ret
= usb_get_hub_descriptor(dev
, buffer
, 4);
349 debug("usb_hub_configure: failed to get hub " \
350 "descriptor, giving up %lX\n", dev
->status
);
353 descriptor
= (struct usb_hub_descriptor
*)buffer
;
355 length
= min_t(int, descriptor
->bLength
,
356 sizeof(struct usb_hub_descriptor
));
358 ret
= usb_get_hub_descriptor(dev
, buffer
, length
);
360 debug("usb_hub_configure: failed to get hub " \
361 "descriptor 2nd giving up %lX\n", dev
->status
);
364 memcpy((unsigned char *)&hub
->desc
, buffer
, length
);
365 /* adjust 16bit values */
366 put_unaligned(le16_to_cpu(get_unaligned(
367 &descriptor
->wHubCharacteristics
)),
368 &hub
->desc
.wHubCharacteristics
);
370 bitmap
= (unsigned char *)&hub
->desc
.DeviceRemovable
[0];
371 /* devices not removable by default */
372 memset(bitmap
, 0xff, (USB_MAXCHILDREN
+1+7)/8);
373 bitmap
= (unsigned char *)&hub
->desc
.PortPowerCtrlMask
[0];
374 memset(bitmap
, 0xff, (USB_MAXCHILDREN
+1+7)/8); /* PowerMask = 1B */
376 for (i
= 0; i
< ((hub
->desc
.bNbrPorts
+ 1 + 7)/8); i
++)
377 hub
->desc
.DeviceRemovable
[i
] = descriptor
->DeviceRemovable
[i
];
379 for (i
= 0; i
< ((hub
->desc
.bNbrPorts
+ 1 + 7)/8); i
++)
380 hub
->desc
.PortPowerCtrlMask
[i
] = descriptor
->PortPowerCtrlMask
[i
];
382 dev
->maxchild
= descriptor
->bNbrPorts
;
383 debug("%d ports detected\n", dev
->maxchild
);
385 hubCharacteristics
= get_unaligned(&hub
->desc
.wHubCharacteristics
);
386 switch (hubCharacteristics
& HUB_CHAR_LPSM
) {
388 debug("ganged power switching\n");
391 debug("individual port power switching\n");
395 debug("unknown reserved power switching mode\n");
399 if (hubCharacteristics
& HUB_CHAR_COMPOUND
)
400 debug("part of a compound device\n");
402 debug("standalone hub\n");
404 switch (hubCharacteristics
& HUB_CHAR_OCPM
) {
406 debug("global over-current protection\n");
409 debug("individual port over-current protection\n");
413 debug("no over-current protection\n");
417 debug("power on to power good time: %dms\n",
418 descriptor
->bPwrOn2PwrGood
* 2);
419 debug("hub controller current requirement: %dmA\n",
420 descriptor
->bHubContrCurrent
);
422 for (i
= 0; i
< dev
->maxchild
; i
++)
423 debug("port %d is%s removable\n", i
+ 1,
424 hub
->desc
.DeviceRemovable
[(i
+ 1) / 8] & \
425 (1 << ((i
+ 1) % 8)) ? " not" : "");
427 if (sizeof(struct usb_hub_status
) > USB_BUFSIZ
) {
428 debug("usb_hub_configure: failed to get Status - " \
429 "too long: %d\n", descriptor
->bLength
);
433 ret
= usb_get_hub_status(dev
, buffer
);
435 debug("usb_hub_configure: failed to get Status %lX\n",
441 hubsts
= (struct usb_hub_status
*)buffer
;
444 debug("get_hub_status returned status %X, change %X\n",
445 le16_to_cpu(hubsts
->wHubStatus
),
446 le16_to_cpu(hubsts
->wHubChange
));
447 debug("local power source is %s\n",
448 (le16_to_cpu(hubsts
->wHubStatus
) & HUB_STATUS_LOCAL_POWER
) ? \
449 "lost (inactive)" : "good");
450 debug("%sover-current condition exists\n",
451 (le16_to_cpu(hubsts
->wHubStatus
) & HUB_STATUS_OVERCURRENT
) ? \
453 usb_hub_power_on(hub
);
456 * Reset any devices that may be in a bad state when applying
457 * the power. This is a __weak function. Resetting of the devices
458 * should occur in the board file of the device.
460 for (i
= 0; i
< dev
->maxchild
; i
++)
461 usb_hub_reset_devices(i
+ 1);
463 for (i
= 0; i
< dev
->maxchild
; i
++) {
464 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status
, portsts
, 1);
465 unsigned short portstatus
, portchange
;
467 ulong start
= get_timer(0);
470 debug("\n\nScanning '%s' port %d\n", dev
->dev
->name
, i
+ 1);
472 debug("\n\nScanning port %d\n", i
+ 1);
475 * Wait for (whichever finishes first)
476 * - A maximum of 10 seconds
477 * This is a purely observational value driven by connecting
478 * a few broken pen drives and taking the max * 1.5 approach
479 * - connection_change and connection state to report same
483 ret
= usb_get_port_status(dev
, i
+ 1, portsts
);
485 debug("get_port_status failed\n");
489 portstatus
= le16_to_cpu(portsts
->wPortStatus
);
490 portchange
= le16_to_cpu(portsts
->wPortChange
);
492 if ((portchange
& USB_PORT_STAT_C_CONNECTION
) ==
493 (portstatus
& USB_PORT_STAT_CONNECTION
))
496 } while (get_timer(start
) < CONFIG_SYS_HZ
* 10);
501 debug("Port %d Status %X Change %X\n",
502 i
+ 1, portstatus
, portchange
);
504 if (portchange
& USB_PORT_STAT_C_CONNECTION
) {
505 debug("port %d connection change\n", i
+ 1);
506 usb_hub_port_connect_change(dev
, i
);
508 if (portchange
& USB_PORT_STAT_C_ENABLE
) {
509 debug("port %d enable change, status %x\n",
511 usb_clear_port_feature(dev
, i
+ 1,
512 USB_PORT_FEAT_C_ENABLE
);
514 * The following hack causes a ghost device problem
517 #ifndef CONFIG_USB_EHCI_FARADAY
518 /* EM interference sometimes causes bad shielded USB
519 * devices to be shutdown by the hub, this hack enables
520 * them again. Works at least with mouse driver */
521 if (!(portstatus
& USB_PORT_STAT_ENABLE
) &&
522 (portstatus
& USB_PORT_STAT_CONNECTION
) &&
523 usb_device_has_child_on_port(dev
, i
)) {
524 debug("already running port %i " \
525 "disabled by hub (EMI?), " \
526 "re-enabling...\n", i
+ 1);
527 usb_hub_port_connect_change(dev
, i
);
531 if (portstatus
& USB_PORT_STAT_SUSPEND
) {
532 debug("port %d suspend change\n", i
+ 1);
533 usb_clear_port_feature(dev
, i
+ 1,
534 USB_PORT_FEAT_SUSPEND
);
537 if (portchange
& USB_PORT_STAT_C_OVERCURRENT
) {
538 debug("port %d over-current change\n", i
+ 1);
539 usb_clear_port_feature(dev
, i
+ 1,
540 USB_PORT_FEAT_C_OVER_CURRENT
);
541 usb_hub_power_on(hub
);
544 if (portchange
& USB_PORT_STAT_C_RESET
) {
545 debug("port %d reset change\n", i
+ 1);
546 usb_clear_port_feature(dev
, i
+ 1,
547 USB_PORT_FEAT_C_RESET
);
549 } /* end for i all ports */
554 static int usb_hub_check(struct usb_device
*dev
, int ifnum
)
556 struct usb_interface
*iface
;
557 struct usb_endpoint_descriptor
*ep
= NULL
;
559 iface
= &dev
->config
.if_desc
[ifnum
];
561 if (iface
->desc
.bInterfaceClass
!= USB_CLASS_HUB
)
563 /* Some hubs have a subclass of 1, which AFAICT according to the */
564 /* specs is not defined, but it works */
565 if ((iface
->desc
.bInterfaceSubClass
!= 0) &&
566 (iface
->desc
.bInterfaceSubClass
!= 1))
568 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
569 if (iface
->desc
.bNumEndpoints
!= 1)
571 ep
= &iface
->ep_desc
[0];
572 /* Output endpoint? Curiousier and curiousier.. */
573 if (!(ep
->bEndpointAddress
& USB_DIR_IN
))
575 /* If it's not an interrupt endpoint, we'd better punt! */
576 if ((ep
->bmAttributes
& 3) != 3)
579 debug("USB hub found\n");
583 debug("USB hub not found: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n",
584 iface
->desc
.bInterfaceClass
, iface
->desc
.bInterfaceSubClass
,
585 iface
->desc
.bNumEndpoints
);
587 debug(" bEndpointAddress=%#x, bmAttributes=%d",
588 ep
->bEndpointAddress
, ep
->bmAttributes
);
594 int usb_hub_probe(struct usb_device
*dev
, int ifnum
)
598 ret
= usb_hub_check(dev
, ifnum
);
601 ret
= usb_hub_configure(dev
);
606 int usb_hub_scan(struct udevice
*hub
)
608 struct usb_device
*udev
= dev_get_parentdata(hub
);
610 return usb_hub_configure(udev
);
613 static int usb_hub_post_bind(struct udevice
*dev
)
615 /* Scan the bus for devices */
616 return dm_scan_fdt_node(dev
, gd
->fdt_blob
, dev
->of_offset
, false);
619 static int usb_hub_post_probe(struct udevice
*dev
)
621 debug("%s\n", __func__
);
622 return usb_hub_scan(dev
);
625 static const struct udevice_id usb_hub_ids
[] = {
626 { .compatible
= "usb-hub" },
630 U_BOOT_DRIVER(usb_generic_hub
) = {
632 .id
= UCLASS_USB_HUB
,
633 .of_match
= usb_hub_ids
,
634 .flags
= DM_FLAG_ALLOC_PRIV_DMA
,
637 UCLASS_DRIVER(usb_hub
) = {
638 .id
= UCLASS_USB_HUB
,
640 .post_bind
= usb_hub_post_bind
,
641 .post_probe
= usb_hub_post_probe
,
642 .child_pre_probe
= usb_child_pre_probe
,
643 .per_child_auto_alloc_size
= sizeof(struct usb_device
),
644 .per_child_platdata_auto_alloc_size
= sizeof(struct usb_dev_platdata
),
647 static const struct usb_device_id hub_id_table
[] = {
649 .match_flags
= USB_DEVICE_ID_MATCH_DEV_CLASS
,
650 .bDeviceClass
= USB_CLASS_HUB
652 { } /* Terminating entry */
655 USB_DEVICE(usb_generic_hub
, hub_id_table
);