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