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
27 #include <asm/processor.h>
28 #include <asm/unaligned.h>
29 #include <linux/ctype.h>
30 #include <asm/byteorder.h>
31 #include <asm/unaligned.h>
35 #include <asm/4xx_pci.h>
38 #ifndef CONFIG_USB_HUB_MIN_POWER_ON_DELAY
39 #define CONFIG_USB_HUB_MIN_POWER_ON_DELAY 1000
42 #define USB_BUFSIZ 512
44 static struct usb_hub_device hub_dev[USB_MAX_HUB];
45 static int usb_hub_index;
47 __weak void usb_hub_reset_devices(int port)
52 static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
54 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
55 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
56 USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
59 static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
61 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
62 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
63 port, NULL, 0, USB_CNTL_TIMEOUT);
66 static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
68 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
69 USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
70 port, NULL, 0, USB_CNTL_TIMEOUT);
73 static int usb_get_hub_status(struct usb_device *dev, void *data)
75 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
76 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
77 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
80 static int usb_get_port_status(struct usb_device *dev, int port, void *data)
82 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
83 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
84 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
88 static void usb_hub_power_on(struct usb_hub_device *hub)
91 struct usb_device *dev;
92 unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
93 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
94 unsigned short portstatus;
100 * Enable power to the ports:
101 * Here we Power-cycle the ports: aka,
102 * turning them off and turning on again.
104 debug("enabling power on all ports\n");
105 for (i = 0; i < dev->maxchild; i++) {
106 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
107 debug("port %d returns %lX\n", i + 1, dev->status);
110 /* Wait at least 2*bPwrOn2PwrGood for PP to change */
113 for (i = 0; i < dev->maxchild; i++) {
114 ret = usb_get_port_status(dev, i + 1, portsts);
116 debug("port %d: get_port_status failed\n", i + 1);
121 * Check to confirm the state of Port Power:
122 * xHCI says "After modifying PP, s/w shall read
123 * PP and confirm that it has reached the desired state
124 * before modifying it again, undefined behavior may occur
125 * if this procedure is not followed".
126 * EHCI doesn't say anything like this, but no harm in keeping
129 portstatus = le16_to_cpu(portsts->wPortStatus);
130 if (portstatus & (USB_PORT_STAT_POWER << 1)) {
131 debug("port %d: Port power change failed\n", i + 1);
136 for (i = 0; i < dev->maxchild; i++) {
137 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
138 debug("port %d returns %lX\n", i + 1, dev->status);
142 * Wait for power to become stable,
143 * plus spec-defined max time for device to connect
145 mdelay(pgood_delay + CONFIG_USB_HUB_MIN_POWER_ON_DELAY);
148 void usb_hub_reset(void)
153 static struct usb_hub_device *usb_hub_allocate(void)
155 if (usb_hub_index < USB_MAX_HUB)
156 return &hub_dev[usb_hub_index++];
158 printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
164 static inline char *portspeed(int portstatus)
168 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
169 case USB_PORT_STAT_SUPER_SPEED:
170 speed_str = "5 Gb/s";
172 case USB_PORT_STAT_HIGH_SPEED:
173 speed_str = "480 Mb/s";
175 case USB_PORT_STAT_LOW_SPEED:
176 speed_str = "1.5 Mb/s";
179 speed_str = "12 Mb/s";
186 int hub_port_reset(struct usb_device *dev, int port,
187 unsigned short *portstat)
190 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
191 unsigned short portstatus, portchange;
193 debug("hub_port_reset: resetting port %d...\n", port);
194 for (tries = 0; tries < MAX_TRIES; tries++) {
196 usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
199 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
200 debug("get_port_status failed status %lX\n",
204 portstatus = le16_to_cpu(portsts->wPortStatus);
205 portchange = le16_to_cpu(portsts->wPortChange);
207 debug("portstatus %x, change %x, %s\n", portstatus, portchange,
208 portspeed(portstatus));
210 debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
211 " USB_PORT_STAT_ENABLE %d\n",
212 (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
213 (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
214 (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
216 if ((portchange & USB_PORT_STAT_C_CONNECTION) ||
217 !(portstatus & USB_PORT_STAT_CONNECTION))
220 if (portstatus & USB_PORT_STAT_ENABLE)
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 void usb_hub_port_connect_change(struct usb_device *dev, int port)
241 struct usb_device *usb;
242 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
243 unsigned short portstatus;
246 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
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))) || (dev->children[port])) {
263 debug("usb_disconnect(&hub->children[port]);\n");
264 /* Return now if nothing is connected */
265 if (!(portstatus & USB_PORT_STAT_CONNECTION))
271 if (hub_port_reset(dev, port, &portstatus) < 0) {
272 printf("cannot reset port %i!?\n", port + 1);
278 /* Allocate a new device struct for it */
279 usb = usb_alloc_new_device(dev->controller);
281 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
282 case USB_PORT_STAT_SUPER_SPEED:
283 usb->speed = USB_SPEED_SUPER;
285 case USB_PORT_STAT_HIGH_SPEED:
286 usb->speed = USB_SPEED_HIGH;
288 case USB_PORT_STAT_LOW_SPEED:
289 usb->speed = USB_SPEED_LOW;
292 usb->speed = USB_SPEED_FULL;
296 dev->children[port] = usb;
298 usb->portnr = port + 1;
299 /* Run it through the hoops (find a driver, etc) */
300 if (usb_new_device(usb)) {
301 /* Woops, disable the port */
303 dev->children[port] = NULL;
304 debug("hub: disabling port %d\n", port + 1);
305 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
310 static int usb_hub_configure(struct usb_device *dev)
313 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
314 unsigned char *bitmap;
315 short hubCharacteristics;
316 struct usb_hub_descriptor *descriptor;
317 struct usb_hub_device *hub;
318 __maybe_unused struct usb_hub_status *hubsts;
320 /* "allocate" Hub device */
321 hub = usb_hub_allocate();
325 /* Get the the hub descriptor */
326 if (usb_get_hub_descriptor(dev, buffer, 4) < 0) {
327 debug("usb_hub_configure: failed to get hub " \
328 "descriptor, giving up %lX\n", dev->status);
331 descriptor = (struct usb_hub_descriptor *)buffer;
333 length = min(descriptor->bLength, sizeof(struct usb_hub_descriptor));
335 if (usb_get_hub_descriptor(dev, buffer, length) < 0) {
336 debug("usb_hub_configure: failed to get hub " \
337 "descriptor 2nd giving up %lX\n", dev->status);
340 memcpy((unsigned char *)&hub->desc, buffer, length);
341 /* adjust 16bit values */
342 put_unaligned(le16_to_cpu(get_unaligned(
343 &descriptor->wHubCharacteristics)),
344 &hub->desc.wHubCharacteristics);
346 bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
347 /* devices not removable by default */
348 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
349 bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
350 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
352 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
353 hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
355 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
356 hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
358 dev->maxchild = descriptor->bNbrPorts;
359 debug("%d ports detected\n", dev->maxchild);
361 hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
362 switch (hubCharacteristics & HUB_CHAR_LPSM) {
364 debug("ganged power switching\n");
367 debug("individual port power switching\n");
371 debug("unknown reserved power switching mode\n");
375 if (hubCharacteristics & HUB_CHAR_COMPOUND)
376 debug("part of a compound device\n");
378 debug("standalone hub\n");
380 switch (hubCharacteristics & HUB_CHAR_OCPM) {
382 debug("global over-current protection\n");
385 debug("individual port over-current protection\n");
389 debug("no over-current protection\n");
393 debug("power on to power good time: %dms\n",
394 descriptor->bPwrOn2PwrGood * 2);
395 debug("hub controller current requirement: %dmA\n",
396 descriptor->bHubContrCurrent);
398 for (i = 0; i < dev->maxchild; i++)
399 debug("port %d is%s removable\n", i + 1,
400 hub->desc.DeviceRemovable[(i + 1) / 8] & \
401 (1 << ((i + 1) % 8)) ? " not" : "");
403 if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
404 debug("usb_hub_configure: failed to get Status - " \
405 "too long: %d\n", descriptor->bLength);
409 if (usb_get_hub_status(dev, buffer) < 0) {
410 debug("usb_hub_configure: failed to get Status %lX\n",
416 hubsts = (struct usb_hub_status *)buffer;
419 debug("get_hub_status returned status %X, change %X\n",
420 le16_to_cpu(hubsts->wHubStatus),
421 le16_to_cpu(hubsts->wHubChange));
422 debug("local power source is %s\n",
423 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
424 "lost (inactive)" : "good");
425 debug("%sover-current condition exists\n",
426 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
428 usb_hub_power_on(hub);
431 * Reset any devices that may be in a bad state when applying
432 * the power. This is a __weak function. Resetting of the devices
433 * should occur in the board file of the device.
435 for (i = 0; i < dev->maxchild; i++)
436 usb_hub_reset_devices(i + 1);
438 for (i = 0; i < dev->maxchild; i++) {
439 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
440 unsigned short portstatus, portchange;
442 ulong start = get_timer(0);
445 * Wait for (whichever finishes first)
446 * - A maximum of 10 seconds
447 * This is a purely observational value driven by connecting
448 * a few broken pen drives and taking the max * 1.5 approach
449 * - connection_change and connection state to report same
453 ret = usb_get_port_status(dev, i + 1, portsts);
455 debug("get_port_status failed\n");
459 portstatus = le16_to_cpu(portsts->wPortStatus);
460 portchange = le16_to_cpu(portsts->wPortChange);
462 if ((portchange & USB_PORT_STAT_C_CONNECTION) ==
463 (portstatus & USB_PORT_STAT_CONNECTION))
466 } while (get_timer(start) < CONFIG_SYS_HZ * 10);
471 debug("Port %d Status %X Change %X\n",
472 i + 1, portstatus, portchange);
474 if (portchange & USB_PORT_STAT_C_CONNECTION) {
475 debug("port %d connection change\n", i + 1);
476 usb_hub_port_connect_change(dev, i);
478 if (portchange & USB_PORT_STAT_C_ENABLE) {
479 debug("port %d enable change, status %x\n",
481 usb_clear_port_feature(dev, i + 1,
482 USB_PORT_FEAT_C_ENABLE);
484 * The following hack causes a ghost device problem
487 #ifndef CONFIG_USB_EHCI_FARADAY
488 /* EM interference sometimes causes bad shielded USB
489 * devices to be shutdown by the hub, this hack enables
490 * them again. Works at least with mouse driver */
491 if (!(portstatus & USB_PORT_STAT_ENABLE) &&
492 (portstatus & USB_PORT_STAT_CONNECTION) &&
493 ((dev->children[i]))) {
494 debug("already running port %i " \
495 "disabled by hub (EMI?), " \
496 "re-enabling...\n", i + 1);
497 usb_hub_port_connect_change(dev, i);
501 if (portstatus & USB_PORT_STAT_SUSPEND) {
502 debug("port %d suspend change\n", i + 1);
503 usb_clear_port_feature(dev, i + 1,
504 USB_PORT_FEAT_SUSPEND);
507 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
508 debug("port %d over-current change\n", i + 1);
509 usb_clear_port_feature(dev, i + 1,
510 USB_PORT_FEAT_C_OVER_CURRENT);
511 usb_hub_power_on(hub);
514 if (portchange & USB_PORT_STAT_C_RESET) {
515 debug("port %d reset change\n", i + 1);
516 usb_clear_port_feature(dev, i + 1,
517 USB_PORT_FEAT_C_RESET);
519 } /* end for i all ports */
524 int usb_hub_probe(struct usb_device *dev, int ifnum)
526 struct usb_interface *iface;
527 struct usb_endpoint_descriptor *ep;
530 iface = &dev->config.if_desc[ifnum];
532 if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
534 /* Some hubs have a subclass of 1, which AFAICT according to the */
535 /* specs is not defined, but it works */
536 if ((iface->desc.bInterfaceSubClass != 0) &&
537 (iface->desc.bInterfaceSubClass != 1))
539 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
540 if (iface->desc.bNumEndpoints != 1)
542 ep = &iface->ep_desc[0];
543 /* Output endpoint? Curiousier and curiousier.. */
544 if (!(ep->bEndpointAddress & USB_DIR_IN))
546 /* If it's not an interrupt endpoint, we'd better punt! */
547 if ((ep->bmAttributes & 3) != 3)
550 debug("USB hub found\n");
551 ret = usb_hub_configure(dev);