Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 1 | /* |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 2 | * Most of this source has been derived from the Linux USB |
| 3 | * project: |
| 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) |
| 13 | * |
| 14 | * Adapted for U-Boot: |
| 15 | * (C) Copyright 2001 Denis Peter, MPL AG Switzerland |
| 16 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 17 | * SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /**************************************************************************** |
| 21 | * HUB "Driver" |
| 22 | * Probes device for being a hub and configurate it |
| 23 | */ |
| 24 | |
| 25 | #include <common.h> |
| 26 | #include <command.h> |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 27 | #include <errno.h> |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 28 | #include <asm/processor.h> |
Lucas Stach | 835e11e | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 29 | #include <asm/unaligned.h> |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 30 | #include <linux/ctype.h> |
| 31 | #include <asm/byteorder.h> |
| 32 | #include <asm/unaligned.h> |
| 33 | |
| 34 | #include <usb.h> |
| 35 | #ifdef CONFIG_4xx |
| 36 | #include <asm/4xx_pci.h> |
| 37 | #endif |
| 38 | |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 39 | #define USB_BUFSIZ 512 |
| 40 | |
| 41 | static struct usb_hub_device hub_dev[USB_MAX_HUB]; |
| 42 | static int usb_hub_index; |
| 43 | |
Dan Murphy | 2666bb1 | 2013-08-01 14:06:01 -0500 | [diff] [blame] | 44 | __weak void usb_hub_reset_devices(int port) |
| 45 | { |
| 46 | return; |
| 47 | } |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 48 | |
| 49 | static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size) |
| 50 | { |
| 51 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 52 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, |
| 53 | USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT); |
| 54 | } |
| 55 | |
| 56 | static int usb_clear_port_feature(struct usb_device *dev, int port, int feature) |
| 57 | { |
| 58 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 59 | USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, |
| 60 | port, NULL, 0, USB_CNTL_TIMEOUT); |
| 61 | } |
| 62 | |
| 63 | static int usb_set_port_feature(struct usb_device *dev, int port, int feature) |
| 64 | { |
| 65 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 66 | USB_REQ_SET_FEATURE, USB_RT_PORT, feature, |
| 67 | port, NULL, 0, USB_CNTL_TIMEOUT); |
| 68 | } |
| 69 | |
| 70 | static int usb_get_hub_status(struct usb_device *dev, void *data) |
| 71 | { |
| 72 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 73 | USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, |
| 74 | data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT); |
| 75 | } |
| 76 | |
| 77 | static int usb_get_port_status(struct usb_device *dev, int port, void *data) |
| 78 | { |
| 79 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 80 | USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port, |
| 81 | data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | static void usb_hub_power_on(struct usb_hub_device *hub) |
| 86 | { |
| 87 | int i; |
| 88 | struct usb_device *dev; |
Wolfgang Grandegger | 9bb46e0 | 2011-12-21 00:01:09 +0000 | [diff] [blame] | 89 | unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2; |
Tim Harvey | acda1d5 | 2015-04-08 12:21:12 -0700 | [diff] [blame] | 90 | const char *env; |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 91 | |
| 92 | dev = hub->pusb_dev; |
Vivek Gautam | c9b13d6 | 2013-04-24 02:50:11 +0000 | [diff] [blame] | 93 | |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 94 | debug("enabling power on all ports\n"); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 95 | for (i = 0; i < dev->maxchild; i++) { |
| 96 | usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER); |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 97 | debug("port %d returns %lX\n", i + 1, dev->status); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 98 | } |
Wolfgang Grandegger | 9bb46e0 | 2011-12-21 00:01:09 +0000 | [diff] [blame] | 99 | |
Stephen Warren | 62da17c | 2014-05-19 14:21:17 -0600 | [diff] [blame] | 100 | /* |
| 101 | * Wait for power to become stable, |
| 102 | * plus spec-defined max time for device to connect |
Tim Harvey | acda1d5 | 2015-04-08 12:21:12 -0700 | [diff] [blame] | 103 | * but allow this time to be increased via env variable as some |
| 104 | * devices break the spec and require longer warm-up times |
Stephen Warren | 62da17c | 2014-05-19 14:21:17 -0600 | [diff] [blame] | 105 | */ |
Tim Harvey | acda1d5 | 2015-04-08 12:21:12 -0700 | [diff] [blame] | 106 | env = getenv("usb_pgood_delay"); |
| 107 | if (env) |
| 108 | pgood_delay = max(pgood_delay, |
| 109 | (unsigned)simple_strtol(env, NULL, 0)); |
| 110 | debug("pgood_delay=%dms\n", pgood_delay); |
Stephen Warren | 683ecd8 | 2014-05-19 14:21:18 -0600 | [diff] [blame] | 111 | mdelay(pgood_delay + 1000); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void usb_hub_reset(void) |
| 115 | { |
| 116 | usb_hub_index = 0; |
| 117 | } |
| 118 | |
| 119 | static struct usb_hub_device *usb_hub_allocate(void) |
| 120 | { |
| 121 | if (usb_hub_index < USB_MAX_HUB) |
| 122 | return &hub_dev[usb_hub_index++]; |
| 123 | |
| 124 | printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB); |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | #define MAX_TRIES 5 |
| 129 | |
| 130 | static inline char *portspeed(int portstatus) |
| 131 | { |
Vivek Gautam | 86c35be | 2013-04-24 02:50:12 +0000 | [diff] [blame] | 132 | char *speed_str; |
| 133 | |
| 134 | switch (portstatus & USB_PORT_STAT_SPEED_MASK) { |
| 135 | case USB_PORT_STAT_SUPER_SPEED: |
| 136 | speed_str = "5 Gb/s"; |
| 137 | break; |
| 138 | case USB_PORT_STAT_HIGH_SPEED: |
| 139 | speed_str = "480 Mb/s"; |
| 140 | break; |
| 141 | case USB_PORT_STAT_LOW_SPEED: |
| 142 | speed_str = "1.5 Mb/s"; |
| 143 | break; |
| 144 | default: |
| 145 | speed_str = "12 Mb/s"; |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | return speed_str; |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Simon Glass | c093d6d | 2015-03-25 12:22:04 -0600 | [diff] [blame^] | 152 | int legacy_hub_port_reset(struct usb_device *dev, int port, |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 153 | unsigned short *portstat) |
| 154 | { |
| 155 | int tries; |
Puneet Saxena | 6c9bb60 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 156 | ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 157 | unsigned short portstatus, portchange; |
| 158 | |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 159 | debug("hub_port_reset: resetting port %d...\n", port); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 160 | for (tries = 0; tries < MAX_TRIES; tries++) { |
| 161 | |
| 162 | usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET); |
Mike Frysinger | 60ce19a | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 163 | mdelay(200); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 164 | |
Puneet Saxena | 6c9bb60 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 165 | if (usb_get_port_status(dev, port + 1, portsts) < 0) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 166 | debug("get_port_status failed status %lX\n", |
| 167 | dev->status); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 168 | return -1; |
| 169 | } |
Puneet Saxena | 6c9bb60 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 170 | portstatus = le16_to_cpu(portsts->wPortStatus); |
| 171 | portchange = le16_to_cpu(portsts->wPortChange); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 172 | |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 173 | debug("portstatus %x, change %x, %s\n", portstatus, portchange, |
| 174 | portspeed(portstatus)); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 175 | |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 176 | debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \ |
| 177 | " USB_PORT_STAT_ENABLE %d\n", |
| 178 | (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0, |
| 179 | (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0, |
| 180 | (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 181 | |
Stephen Warren | 3a8e4fa | 2014-08-07 17:07:59 -0600 | [diff] [blame] | 182 | /* |
| 183 | * Perhaps we should check for the following here: |
| 184 | * - C_CONNECTION hasn't been set. |
| 185 | * - CONNECTION is still set. |
| 186 | * |
| 187 | * Doing so would ensure that the device is still connected |
| 188 | * to the bus, and hasn't been unplugged or replaced while the |
| 189 | * USB bus reset was going on. |
| 190 | * |
| 191 | * However, if we do that, then (at least) a San Disk Ultra |
| 192 | * USB 3.0 16GB device fails to reset on (at least) an NVIDIA |
| 193 | * Tegra Jetson TK1 board. For some reason, the device appears |
| 194 | * to briefly drop off the bus when this second bus reset is |
| 195 | * executed, yet if we retry this loop, it'll eventually come |
| 196 | * back after another reset or two. |
| 197 | */ |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 198 | |
| 199 | if (portstatus & USB_PORT_STAT_ENABLE) |
| 200 | break; |
| 201 | |
Mike Frysinger | 60ce19a | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 202 | mdelay(200); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | if (tries == MAX_TRIES) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 206 | debug("Cannot enable port %i after %i retries, " \ |
| 207 | "disabling port.\n", port + 1, MAX_TRIES); |
| 208 | debug("Maybe the USB cable is bad?\n"); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET); |
| 213 | *portstat = portstatus; |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 218 | int usb_hub_port_connect_change(struct usb_device *dev, int port) |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 219 | { |
| 220 | struct usb_device *usb; |
Puneet Saxena | 6c9bb60 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 221 | ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 222 | unsigned short portstatus; |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 223 | int ret, speed; |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 224 | |
| 225 | /* Check status */ |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 226 | ret = usb_get_port_status(dev, port + 1, portsts); |
| 227 | if (ret < 0) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 228 | debug("get_port_status failed\n"); |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 229 | return ret; |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Puneet Saxena | 6c9bb60 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 232 | portstatus = le16_to_cpu(portsts->wPortStatus); |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 233 | debug("portstatus %x, change %x, %s\n", |
| 234 | portstatus, |
| 235 | le16_to_cpu(portsts->wPortChange), |
| 236 | portspeed(portstatus)); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 237 | |
| 238 | /* Clear the connection change status */ |
| 239 | usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION); |
| 240 | |
| 241 | /* Disconnect any existing devices under this port */ |
| 242 | if (((!(portstatus & USB_PORT_STAT_CONNECTION)) && |
| 243 | (!(portstatus & USB_PORT_STAT_ENABLE))) || (dev->children[port])) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 244 | debug("usb_disconnect(&hub->children[port]);\n"); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 245 | /* Return now if nothing is connected */ |
| 246 | if (!(portstatus & USB_PORT_STAT_CONNECTION)) |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 247 | return -ENOTCONN; |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 248 | } |
Mike Frysinger | 60ce19a | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 249 | mdelay(200); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 250 | |
| 251 | /* Reset the port */ |
Simon Glass | c093d6d | 2015-03-25 12:22:04 -0600 | [diff] [blame^] | 252 | ret = legacy_hub_port_reset(dev, port, &portstatus); |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 253 | if (ret < 0) { |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 254 | printf("cannot reset port %i!?\n", port + 1); |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 255 | return ret; |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Mike Frysinger | 60ce19a | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 258 | mdelay(200); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 259 | |
Vivek Gautam | 86c35be | 2013-04-24 02:50:12 +0000 | [diff] [blame] | 260 | switch (portstatus & USB_PORT_STAT_SPEED_MASK) { |
| 261 | case USB_PORT_STAT_SUPER_SPEED: |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 262 | speed = USB_SPEED_SUPER; |
Vivek Gautam | 86c35be | 2013-04-24 02:50:12 +0000 | [diff] [blame] | 263 | break; |
| 264 | case USB_PORT_STAT_HIGH_SPEED: |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 265 | speed = USB_SPEED_HIGH; |
Vivek Gautam | 86c35be | 2013-04-24 02:50:12 +0000 | [diff] [blame] | 266 | break; |
| 267 | case USB_PORT_STAT_LOW_SPEED: |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 268 | speed = USB_SPEED_LOW; |
Vivek Gautam | 86c35be | 2013-04-24 02:50:12 +0000 | [diff] [blame] | 269 | break; |
| 270 | default: |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 271 | speed = USB_SPEED_FULL; |
Vivek Gautam | 86c35be | 2013-04-24 02:50:12 +0000 | [diff] [blame] | 272 | break; |
| 273 | } |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 274 | |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 275 | ret = usb_alloc_new_device(dev->controller, &usb); |
| 276 | if (ret) { |
| 277 | printf("cannot create new device: ret=%d", ret); |
| 278 | return ret; |
| 279 | } |
| 280 | |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 281 | dev->children[port] = usb; |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 282 | usb->speed = speed; |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 283 | usb->parent = dev; |
| 284 | usb->portnr = port + 1; |
| 285 | /* Run it through the hoops (find a driver, etc) */ |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 286 | ret = usb_new_device(usb); |
| 287 | if (ret < 0) { |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 288 | /* Woops, disable the port */ |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 289 | usb_free_device(dev->controller); |
Milind Choudhary | ebb2478 | 2012-12-12 17:55:28 -0800 | [diff] [blame] | 290 | dev->children[port] = NULL; |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 291 | debug("hub: disabling port %d\n", port + 1); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 292 | usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE); |
| 293 | } |
Simon Glass | d146113 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 294 | |
| 295 | return ret; |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | |
| 299 | static int usb_hub_configure(struct usb_device *dev) |
| 300 | { |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 301 | int i, length; |
Puneet Saxena | 6c9bb60 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 302 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ); |
| 303 | unsigned char *bitmap; |
Lucas Stach | 835e11e | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 304 | short hubCharacteristics; |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 305 | struct usb_hub_descriptor *descriptor; |
| 306 | struct usb_hub_device *hub; |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 307 | __maybe_unused struct usb_hub_status *hubsts; |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 308 | |
| 309 | /* "allocate" Hub device */ |
| 310 | hub = usb_hub_allocate(); |
| 311 | if (hub == NULL) |
| 312 | return -1; |
| 313 | hub->pusb_dev = dev; |
| 314 | /* Get the the hub descriptor */ |
| 315 | if (usb_get_hub_descriptor(dev, buffer, 4) < 0) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 316 | debug("usb_hub_configure: failed to get hub " \ |
| 317 | "descriptor, giving up %lX\n", dev->status); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 318 | return -1; |
| 319 | } |
| 320 | descriptor = (struct usb_hub_descriptor *)buffer; |
| 321 | |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 322 | length = min_t(int, descriptor->bLength, |
| 323 | sizeof(struct usb_hub_descriptor)); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 324 | |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 325 | if (usb_get_hub_descriptor(dev, buffer, length) < 0) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 326 | debug("usb_hub_configure: failed to get hub " \ |
| 327 | "descriptor 2nd giving up %lX\n", dev->status); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 328 | return -1; |
| 329 | } |
Julius Werner | fc4ec49 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 330 | memcpy((unsigned char *)&hub->desc, buffer, length); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 331 | /* adjust 16bit values */ |
Lucas Stach | 835e11e | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 332 | put_unaligned(le16_to_cpu(get_unaligned( |
| 333 | &descriptor->wHubCharacteristics)), |
| 334 | &hub->desc.wHubCharacteristics); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 335 | /* set the bitmap */ |
| 336 | bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0]; |
| 337 | /* devices not removable by default */ |
| 338 | memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); |
| 339 | bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0]; |
| 340 | memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */ |
| 341 | |
| 342 | for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++) |
| 343 | hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i]; |
| 344 | |
| 345 | for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++) |
| 346 | hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i]; |
| 347 | |
| 348 | dev->maxchild = descriptor->bNbrPorts; |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 349 | debug("%d ports detected\n", dev->maxchild); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 350 | |
Lucas Stach | 835e11e | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 351 | hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics); |
| 352 | switch (hubCharacteristics & HUB_CHAR_LPSM) { |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 353 | case 0x00: |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 354 | debug("ganged power switching\n"); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 355 | break; |
| 356 | case 0x01: |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 357 | debug("individual port power switching\n"); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 358 | break; |
| 359 | case 0x02: |
| 360 | case 0x03: |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 361 | debug("unknown reserved power switching mode\n"); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 362 | break; |
| 363 | } |
| 364 | |
Lucas Stach | 835e11e | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 365 | if (hubCharacteristics & HUB_CHAR_COMPOUND) |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 366 | debug("part of a compound device\n"); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 367 | else |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 368 | debug("standalone hub\n"); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 369 | |
Lucas Stach | 835e11e | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 370 | switch (hubCharacteristics & HUB_CHAR_OCPM) { |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 371 | case 0x00: |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 372 | debug("global over-current protection\n"); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 373 | break; |
| 374 | case 0x08: |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 375 | debug("individual port over-current protection\n"); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 376 | break; |
| 377 | case 0x10: |
| 378 | case 0x18: |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 379 | debug("no over-current protection\n"); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 380 | break; |
| 381 | } |
| 382 | |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 383 | debug("power on to power good time: %dms\n", |
| 384 | descriptor->bPwrOn2PwrGood * 2); |
| 385 | debug("hub controller current requirement: %dmA\n", |
| 386 | descriptor->bHubContrCurrent); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 387 | |
| 388 | for (i = 0; i < dev->maxchild; i++) |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 389 | debug("port %d is%s removable\n", i + 1, |
| 390 | hub->desc.DeviceRemovable[(i + 1) / 8] & \ |
| 391 | (1 << ((i + 1) % 8)) ? " not" : ""); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 392 | |
| 393 | if (sizeof(struct usb_hub_status) > USB_BUFSIZ) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 394 | debug("usb_hub_configure: failed to get Status - " \ |
| 395 | "too long: %d\n", descriptor->bLength); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 396 | return -1; |
| 397 | } |
| 398 | |
| 399 | if (usb_get_hub_status(dev, buffer) < 0) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 400 | debug("usb_hub_configure: failed to get Status %lX\n", |
| 401 | dev->status); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 402 | return -1; |
| 403 | } |
| 404 | |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 405 | #ifdef DEBUG |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 406 | hubsts = (struct usb_hub_status *)buffer; |
| 407 | #endif |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 408 | |
| 409 | debug("get_hub_status returned status %X, change %X\n", |
| 410 | le16_to_cpu(hubsts->wHubStatus), |
| 411 | le16_to_cpu(hubsts->wHubChange)); |
| 412 | debug("local power source is %s\n", |
| 413 | (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \ |
| 414 | "lost (inactive)" : "good"); |
| 415 | debug("%sover-current condition exists\n", |
| 416 | (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \ |
| 417 | "" : "no "); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 418 | usb_hub_power_on(hub); |
| 419 | |
Dan Murphy | 2666bb1 | 2013-08-01 14:06:01 -0500 | [diff] [blame] | 420 | /* |
| 421 | * Reset any devices that may be in a bad state when applying |
| 422 | * the power. This is a __weak function. Resetting of the devices |
| 423 | * should occur in the board file of the device. |
| 424 | */ |
| 425 | for (i = 0; i < dev->maxchild; i++) |
| 426 | usb_hub_reset_devices(i + 1); |
| 427 | |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 428 | for (i = 0; i < dev->maxchild; i++) { |
Puneet Saxena | 6c9bb60 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 429 | ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 430 | unsigned short portstatus, portchange; |
Vipin Kumar | a261282 | 2012-12-13 16:25:53 +0530 | [diff] [blame] | 431 | int ret; |
| 432 | ulong start = get_timer(0); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 433 | |
Vipin Kumar | a261282 | 2012-12-13 16:25:53 +0530 | [diff] [blame] | 434 | /* |
| 435 | * Wait for (whichever finishes first) |
| 436 | * - A maximum of 10 seconds |
| 437 | * This is a purely observational value driven by connecting |
| 438 | * a few broken pen drives and taking the max * 1.5 approach |
| 439 | * - connection_change and connection state to report same |
| 440 | * state |
| 441 | */ |
| 442 | do { |
| 443 | ret = usb_get_port_status(dev, i + 1, portsts); |
| 444 | if (ret < 0) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 445 | debug("get_port_status failed\n"); |
Vipin Kumar | a261282 | 2012-12-13 16:25:53 +0530 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | |
| 449 | portstatus = le16_to_cpu(portsts->wPortStatus); |
| 450 | portchange = le16_to_cpu(portsts->wPortChange); |
| 451 | |
| 452 | if ((portchange & USB_PORT_STAT_C_CONNECTION) == |
| 453 | (portstatus & USB_PORT_STAT_CONNECTION)) |
| 454 | break; |
| 455 | |
Vipin Kumar | a261282 | 2012-12-13 16:25:53 +0530 | [diff] [blame] | 456 | } while (get_timer(start) < CONFIG_SYS_HZ * 10); |
| 457 | |
| 458 | if (ret < 0) |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 459 | continue; |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 460 | |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 461 | debug("Port %d Status %X Change %X\n", |
| 462 | i + 1, portstatus, portchange); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 463 | |
| 464 | if (portchange & USB_PORT_STAT_C_CONNECTION) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 465 | debug("port %d connection change\n", i + 1); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 466 | usb_hub_port_connect_change(dev, i); |
| 467 | } |
| 468 | if (portchange & USB_PORT_STAT_C_ENABLE) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 469 | debug("port %d enable change, status %x\n", |
| 470 | i + 1, portstatus); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 471 | usb_clear_port_feature(dev, i + 1, |
| 472 | USB_PORT_FEAT_C_ENABLE); |
Kuo-Jung Su | b5d59de | 2013-05-15 15:29:23 +0800 | [diff] [blame] | 473 | /* |
| 474 | * The following hack causes a ghost device problem |
| 475 | * to Faraday EHCI |
| 476 | */ |
| 477 | #ifndef CONFIG_USB_EHCI_FARADAY |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 478 | /* EM interference sometimes causes bad shielded USB |
| 479 | * devices to be shutdown by the hub, this hack enables |
| 480 | * them again. Works at least with mouse driver */ |
| 481 | if (!(portstatus & USB_PORT_STAT_ENABLE) && |
| 482 | (portstatus & USB_PORT_STAT_CONNECTION) && |
| 483 | ((dev->children[i]))) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 484 | debug("already running port %i " \ |
| 485 | "disabled by hub (EMI?), " \ |
| 486 | "re-enabling...\n", i + 1); |
| 487 | usb_hub_port_connect_change(dev, i); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 488 | } |
Kuo-Jung Su | b5d59de | 2013-05-15 15:29:23 +0800 | [diff] [blame] | 489 | #endif |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 490 | } |
| 491 | if (portstatus & USB_PORT_STAT_SUSPEND) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 492 | debug("port %d suspend change\n", i + 1); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 493 | usb_clear_port_feature(dev, i + 1, |
| 494 | USB_PORT_FEAT_SUSPEND); |
| 495 | } |
| 496 | |
| 497 | if (portchange & USB_PORT_STAT_C_OVERCURRENT) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 498 | debug("port %d over-current change\n", i + 1); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 499 | usb_clear_port_feature(dev, i + 1, |
| 500 | USB_PORT_FEAT_C_OVER_CURRENT); |
| 501 | usb_hub_power_on(hub); |
| 502 | } |
| 503 | |
| 504 | if (portchange & USB_PORT_STAT_C_RESET) { |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 505 | debug("port %d reset change\n", i + 1); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 506 | usb_clear_port_feature(dev, i + 1, |
| 507 | USB_PORT_FEAT_C_RESET); |
| 508 | } |
| 509 | } /* end for i all ports */ |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | int usb_hub_probe(struct usb_device *dev, int ifnum) |
| 515 | { |
| 516 | struct usb_interface *iface; |
| 517 | struct usb_endpoint_descriptor *ep; |
| 518 | int ret; |
| 519 | |
| 520 | iface = &dev->config.if_desc[ifnum]; |
| 521 | /* Is it a hub? */ |
| 522 | if (iface->desc.bInterfaceClass != USB_CLASS_HUB) |
| 523 | return 0; |
| 524 | /* Some hubs have a subclass of 1, which AFAICT according to the */ |
| 525 | /* specs is not defined, but it works */ |
| 526 | if ((iface->desc.bInterfaceSubClass != 0) && |
| 527 | (iface->desc.bInterfaceSubClass != 1)) |
| 528 | return 0; |
| 529 | /* Multiple endpoints? What kind of mutant ninja-hub is this? */ |
| 530 | if (iface->desc.bNumEndpoints != 1) |
| 531 | return 0; |
| 532 | ep = &iface->ep_desc[0]; |
| 533 | /* Output endpoint? Curiousier and curiousier.. */ |
| 534 | if (!(ep->bEndpointAddress & USB_DIR_IN)) |
| 535 | return 0; |
| 536 | /* If it's not an interrupt endpoint, we'd better punt! */ |
| 537 | if ((ep->bmAttributes & 3) != 3) |
| 538 | return 0; |
| 539 | /* We found a hub */ |
Vivek Gautam | f94c95d | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 540 | debug("USB hub found\n"); |
Marek Vasut | 6da5af3 | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 541 | ret = usb_hub_configure(dev); |
| 542 | return ret; |
| 543 | } |