3 * Most of this source has been derived from the Linux USB
5 * (C) Copyright Linus Torvalds 1999
6 * (C) Copyright Johannes Erdfelt 1999-2001
7 * (C) Copyright Andreas Gal 1999
8 * (C) Copyright Gregory P. Smith 1999
9 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
10 * (C) Copyright Randy Dunlap 2000
11 * (C) Copyright David Brownell 2000 (kernel hotplug, usb_device_id)
12 * (C) Copyright Yggdrasil Computing, Inc. 2000
13 * (usb_device_id matching changes by Adam J. Richter)
16 * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
18 * See file CREDITS for list of people who contributed to this
21 * This program is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU General Public License as
23 * published by the Free Software Foundation; either version 2 of
24 * the License, or (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
38 /****************************************************************************
40 * Probes device for being a hub and configurate it
45 #include <asm/processor.h>
46 #include <asm/unaligned.h>
47 #include <linux/ctype.h>
48 #include <asm/byteorder.h>
49 #include <asm/unaligned.h>
53 #include <asm/4xx_pci.h>
58 #define USB_HUB_DEBUG 1
61 #define USB_HUB_DEBUG 0
64 #define USB_PRINTF(fmt, args...) debug_cond(USB_DEBUG, fmt, ##args)
65 #define USB_HUB_PRINTF(fmt, args...) debug_cond(USB_HUB_DEBUG, fmt, ##args)
67 #define USB_BUFSIZ 512
69 static struct usb_hub_device hub_dev[USB_MAX_HUB];
70 static int usb_hub_index;
73 static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
75 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
76 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
77 USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
80 static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
82 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
83 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
84 port, NULL, 0, USB_CNTL_TIMEOUT);
87 static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
89 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
90 USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
91 port, NULL, 0, USB_CNTL_TIMEOUT);
94 static int usb_get_hub_status(struct usb_device *dev, void *data)
96 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
97 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
98 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
101 static int usb_get_port_status(struct usb_device *dev, int port, void *data)
103 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
104 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
105 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
109 static void usb_hub_power_on(struct usb_hub_device *hub)
112 struct usb_device *dev;
113 unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
116 /* Enable power to the ports */
117 USB_HUB_PRINTF("enabling power on all ports\n");
118 for (i = 0; i < dev->maxchild; i++) {
119 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
120 USB_HUB_PRINTF("port %d returns %lX\n", i + 1, dev->status);
123 /* Wait at least 100 msec for power to become stable */
124 mdelay(max(pgood_delay, (unsigned)100));
127 void usb_hub_reset(void)
132 static struct usb_hub_device *usb_hub_allocate(void)
134 if (usb_hub_index < USB_MAX_HUB)
135 return &hub_dev[usb_hub_index++];
137 printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
143 static inline char *portspeed(int portstatus)
145 if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
147 else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
153 int hub_port_reset(struct usb_device *dev, int port,
154 unsigned short *portstat)
157 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
158 unsigned short portstatus, portchange;
160 USB_HUB_PRINTF("hub_port_reset: resetting port %d...\n", port);
161 for (tries = 0; tries < MAX_TRIES; tries++) {
163 usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
166 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
167 USB_HUB_PRINTF("get_port_status failed status %lX\n",
171 portstatus = le16_to_cpu(portsts->wPortStatus);
172 portchange = le16_to_cpu(portsts->wPortChange);
174 USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
175 portstatus, portchange,
176 portspeed(portstatus));
178 USB_HUB_PRINTF("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
179 " USB_PORT_STAT_ENABLE %d\n",
180 (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
181 (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
182 (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
184 if ((portchange & USB_PORT_STAT_C_CONNECTION) ||
185 !(portstatus & USB_PORT_STAT_CONNECTION))
188 if (portstatus & USB_PORT_STAT_ENABLE)
194 if (tries == MAX_TRIES) {
195 USB_HUB_PRINTF("Cannot enable port %i after %i retries, " \
196 "disabling port.\n", port + 1, MAX_TRIES);
197 USB_HUB_PRINTF("Maybe the USB cable is bad?\n");
201 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
202 *portstat = portstatus;
207 void usb_hub_port_connect_change(struct usb_device *dev, int port)
209 struct usb_device *usb;
210 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
211 unsigned short portstatus;
214 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
215 USB_HUB_PRINTF("get_port_status failed\n");
219 portstatus = le16_to_cpu(portsts->wPortStatus);
220 USB_HUB_PRINTF("portstatus %x, change %x, %s\n",
222 le16_to_cpu(portsts->wPortChange),
223 portspeed(portstatus));
225 /* Clear the connection change status */
226 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
228 /* Disconnect any existing devices under this port */
229 if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
230 (!(portstatus & USB_PORT_STAT_ENABLE))) || (dev->children[port])) {
231 USB_HUB_PRINTF("usb_disconnect(&hub->children[port]);\n");
232 /* Return now if nothing is connected */
233 if (!(portstatus & USB_PORT_STAT_CONNECTION))
239 if (hub_port_reset(dev, port, &portstatus) < 0) {
240 printf("cannot reset port %i!?\n", port + 1);
246 /* Allocate a new device struct for it */
247 usb = usb_alloc_new_device(dev->controller);
249 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
250 usb->speed = USB_SPEED_HIGH;
251 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
252 usb->speed = USB_SPEED_LOW;
254 usb->speed = USB_SPEED_FULL;
256 dev->children[port] = usb;
258 usb->portnr = port + 1;
259 /* Run it through the hoops (find a driver, etc) */
260 if (usb_new_device(usb)) {
261 /* Woops, disable the port */
263 dev->children[port] = NULL;
264 USB_HUB_PRINTF("hub: disabling port %d\n", port + 1);
265 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
270 static int usb_hub_configure(struct usb_device *dev)
273 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
274 unsigned char *bitmap;
275 short hubCharacteristics;
276 struct usb_hub_descriptor *descriptor;
277 struct usb_hub_device *hub;
279 struct usb_hub_status *hubsts;
282 /* "allocate" Hub device */
283 hub = usb_hub_allocate();
287 /* Get the the hub descriptor */
288 if (usb_get_hub_descriptor(dev, buffer, 4) < 0) {
289 USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
290 "descriptor, giving up %lX\n", dev->status);
293 descriptor = (struct usb_hub_descriptor *)buffer;
295 /* silence compiler warning if USB_BUFSIZ is > 256 [= sizeof(char)] */
296 i = descriptor->bLength;
297 if (i > USB_BUFSIZ) {
298 USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
299 "descriptor - too long: %d\n",
300 descriptor->bLength);
304 if (usb_get_hub_descriptor(dev, buffer, descriptor->bLength) < 0) {
305 USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
306 "descriptor 2nd giving up %lX\n", dev->status);
309 memcpy((unsigned char *)&hub->desc, buffer, descriptor->bLength);
310 /* adjust 16bit values */
311 put_unaligned(le16_to_cpu(get_unaligned(
312 &descriptor->wHubCharacteristics)),
313 &hub->desc.wHubCharacteristics);
315 bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
316 /* devices not removable by default */
317 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
318 bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
319 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
321 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
322 hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
324 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
325 hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
327 dev->maxchild = descriptor->bNbrPorts;
328 USB_HUB_PRINTF("%d ports detected\n", dev->maxchild);
330 hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
331 switch (hubCharacteristics & HUB_CHAR_LPSM) {
333 USB_HUB_PRINTF("ganged power switching\n");
336 USB_HUB_PRINTF("individual port power switching\n");
340 USB_HUB_PRINTF("unknown reserved power switching mode\n");
344 if (hubCharacteristics & HUB_CHAR_COMPOUND)
345 USB_HUB_PRINTF("part of a compound device\n");
347 USB_HUB_PRINTF("standalone hub\n");
349 switch (hubCharacteristics & HUB_CHAR_OCPM) {
351 USB_HUB_PRINTF("global over-current protection\n");
354 USB_HUB_PRINTF("individual port over-current protection\n");
358 USB_HUB_PRINTF("no over-current protection\n");
362 USB_HUB_PRINTF("power on to power good time: %dms\n",
363 descriptor->bPwrOn2PwrGood * 2);
364 USB_HUB_PRINTF("hub controller current requirement: %dmA\n",
365 descriptor->bHubContrCurrent);
367 for (i = 0; i < dev->maxchild; i++)
368 USB_HUB_PRINTF("port %d is%s removable\n", i + 1,
369 hub->desc.DeviceRemovable[(i + 1) / 8] & \
370 (1 << ((i + 1) % 8)) ? " not" : "");
372 if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
373 USB_HUB_PRINTF("usb_hub_configure: failed to get Status - " \
374 "too long: %d\n", descriptor->bLength);
378 if (usb_get_hub_status(dev, buffer) < 0) {
379 USB_HUB_PRINTF("usb_hub_configure: failed to get Status %lX\n",
385 hubsts = (struct usb_hub_status *)buffer;
387 USB_HUB_PRINTF("get_hub_status returned status %X, change %X\n",
388 le16_to_cpu(hubsts->wHubStatus),
389 le16_to_cpu(hubsts->wHubChange));
390 USB_HUB_PRINTF("local power source is %s\n",
391 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
392 "lost (inactive)" : "good");
393 USB_HUB_PRINTF("%sover-current condition exists\n",
394 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
396 usb_hub_power_on(hub);
398 for (i = 0; i < dev->maxchild; i++) {
399 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
400 unsigned short portstatus, portchange;
402 ulong start = get_timer(0);
405 * Wait for (whichever finishes first)
406 * - A maximum of 10 seconds
407 * This is a purely observational value driven by connecting
408 * a few broken pen drives and taking the max * 1.5 approach
409 * - connection_change and connection state to report same
413 ret = usb_get_port_status(dev, i + 1, portsts);
415 USB_HUB_PRINTF("get_port_status failed\n");
419 portstatus = le16_to_cpu(portsts->wPortStatus);
420 portchange = le16_to_cpu(portsts->wPortChange);
422 if ((portchange & USB_PORT_STAT_C_CONNECTION) ==
423 (portstatus & USB_PORT_STAT_CONNECTION))
427 } while (get_timer(start) < CONFIG_SYS_HZ * 10);
432 USB_HUB_PRINTF("Port %d Status %X Change %X\n",
433 i + 1, portstatus, portchange);
435 if (portchange & USB_PORT_STAT_C_CONNECTION) {
436 USB_HUB_PRINTF("port %d connection change\n", i + 1);
437 usb_hub_port_connect_change(dev, i);
439 if (portchange & USB_PORT_STAT_C_ENABLE) {
440 USB_HUB_PRINTF("port %d enable change, status %x\n",
442 usb_clear_port_feature(dev, i + 1,
443 USB_PORT_FEAT_C_ENABLE);
445 /* EM interference sometimes causes bad shielded USB
446 * devices to be shutdown by the hub, this hack enables
447 * them again. Works at least with mouse driver */
448 if (!(portstatus & USB_PORT_STAT_ENABLE) &&
449 (portstatus & USB_PORT_STAT_CONNECTION) &&
450 ((dev->children[i]))) {
451 USB_HUB_PRINTF("already running port %i " \
452 "disabled by hub (EMI?), " \
453 "re-enabling...\n", i + 1);
454 usb_hub_port_connect_change(dev, i);
457 if (portstatus & USB_PORT_STAT_SUSPEND) {
458 USB_HUB_PRINTF("port %d suspend change\n", i + 1);
459 usb_clear_port_feature(dev, i + 1,
460 USB_PORT_FEAT_SUSPEND);
463 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
464 USB_HUB_PRINTF("port %d over-current change\n", i + 1);
465 usb_clear_port_feature(dev, i + 1,
466 USB_PORT_FEAT_C_OVER_CURRENT);
467 usb_hub_power_on(hub);
470 if (portchange & USB_PORT_STAT_C_RESET) {
471 USB_HUB_PRINTF("port %d reset change\n", i + 1);
472 usb_clear_port_feature(dev, i + 1,
473 USB_PORT_FEAT_C_RESET);
475 } /* end for i all ports */
480 int usb_hub_probe(struct usb_device *dev, int ifnum)
482 struct usb_interface *iface;
483 struct usb_endpoint_descriptor *ep;
486 iface = &dev->config.if_desc[ifnum];
488 if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
490 /* Some hubs have a subclass of 1, which AFAICT according to the */
491 /* specs is not defined, but it works */
492 if ((iface->desc.bInterfaceSubClass != 0) &&
493 (iface->desc.bInterfaceSubClass != 1))
495 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
496 if (iface->desc.bNumEndpoints != 1)
498 ep = &iface->ep_desc[0];
499 /* Output endpoint? Curiousier and curiousier.. */
500 if (!(ep->bEndpointAddress & USB_DIR_IN))
502 /* If it's not an interrupt endpoint, we'd better punt! */
503 if ((ep->bmAttributes & 3) != 3)
506 USB_HUB_PRINTF("USB hub found\n");
507 ret = usb_hub_configure(dev);