Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 1 | /* |
Marek Vasut | 23faf2b | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 17 | * SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | 23faf2b | 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 | 054fe48 | 2015-03-25 12:22:10 -0600 | [diff] [blame] | 27 | #include <dm.h> |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 28 | #include <errno.h> |
Simon Glass | cf92e05 | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 29 | #include <memalign.h> |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 30 | #include <asm/processor.h> |
Lucas Stach | 93ad908 | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 31 | #include <asm/unaligned.h> |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 32 | #include <linux/ctype.h> |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 33 | #include <linux/list.h> |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 34 | #include <asm/byteorder.h> |
Simon Glass | 3884c98 | 2015-11-08 23:47:44 -0700 | [diff] [blame] | 35 | #ifdef CONFIG_SANDBOX |
| 36 | #include <asm/state.h> |
| 37 | #endif |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 38 | #include <asm/unaligned.h> |
Simon Glass | 054fe48 | 2015-03-25 12:22:10 -0600 | [diff] [blame] | 39 | #include <dm/root.h> |
| 40 | |
| 41 | DECLARE_GLOBAL_DATA_PTR; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 42 | |
| 43 | #include <usb.h> |
| 44 | #ifdef CONFIG_4xx |
| 45 | #include <asm/4xx_pci.h> |
| 46 | #endif |
| 47 | |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 48 | #define USB_BUFSIZ 512 |
| 49 | |
Stefan Roese | f7f6010 | 2016-03-15 13:59:12 +0100 | [diff] [blame] | 50 | #define HUB_SHORT_RESET_TIME 20 |
| 51 | #define HUB_LONG_RESET_TIME 200 |
| 52 | |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 53 | #define PORT_OVERCURRENT_MAX_SCAN_COUNT 3 |
| 54 | |
| 55 | struct usb_device_scan { |
| 56 | struct usb_device *dev; /* USB hub device to scan */ |
| 57 | struct usb_hub_device *hub; /* USB hub struct */ |
| 58 | int port; /* USB port to scan */ |
| 59 | struct list_head list; |
| 60 | }; |
| 61 | |
Simon Glass | 054fe48 | 2015-03-25 12:22:10 -0600 | [diff] [blame] | 62 | /* TODO(sjg@chromium.org): Remove this when CONFIG_DM_USB is defined */ |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 63 | static struct usb_hub_device hub_dev[USB_MAX_HUB]; |
| 64 | static int usb_hub_index; |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 65 | static LIST_HEAD(usb_scan_list); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 66 | |
Dan Murphy | 3615a99 | 2013-08-01 14:06:01 -0500 | [diff] [blame] | 67 | __weak void usb_hub_reset_devices(int port) |
| 68 | { |
| 69 | return; |
| 70 | } |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 71 | |
| 72 | static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size) |
| 73 | { |
| 74 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 75 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, |
| 76 | USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT); |
| 77 | } |
| 78 | |
| 79 | static int usb_clear_port_feature(struct usb_device *dev, int port, int feature) |
| 80 | { |
| 81 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 82 | USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, |
| 83 | port, NULL, 0, USB_CNTL_TIMEOUT); |
| 84 | } |
| 85 | |
| 86 | static int usb_set_port_feature(struct usb_device *dev, int port, int feature) |
| 87 | { |
| 88 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 89 | USB_REQ_SET_FEATURE, USB_RT_PORT, feature, |
| 90 | port, NULL, 0, USB_CNTL_TIMEOUT); |
| 91 | } |
| 92 | |
| 93 | static int usb_get_hub_status(struct usb_device *dev, void *data) |
| 94 | { |
| 95 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 96 | USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, |
| 97 | data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT); |
| 98 | } |
| 99 | |
Vincent Palatin | 08f3bb0 | 2015-05-04 11:30:54 -0600 | [diff] [blame] | 100 | int usb_get_port_status(struct usb_device *dev, int port, void *data) |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 101 | { |
| 102 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 103 | USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port, |
| 104 | data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static void usb_hub_power_on(struct usb_hub_device *hub) |
| 109 | { |
| 110 | int i; |
| 111 | struct usb_device *dev; |
Wolfgang Grandegger | a1a28c6 | 2011-12-21 00:01:09 +0000 | [diff] [blame] | 112 | unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2; |
Tim Harvey | 319418c | 2015-04-08 12:21:12 -0700 | [diff] [blame] | 113 | const char *env; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 114 | |
| 115 | dev = hub->pusb_dev; |
Vivek Gautam | 0bf796f | 2013-04-24 02:50:11 +0000 | [diff] [blame] | 116 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 117 | debug("enabling power on all ports\n"); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 118 | for (i = 0; i < dev->maxchild; i++) { |
| 119 | usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER); |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 120 | debug("port %d returns %lX\n", i + 1, dev->status); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 121 | } |
Wolfgang Grandegger | a1a28c6 | 2011-12-21 00:01:09 +0000 | [diff] [blame] | 122 | |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 123 | #ifdef CONFIG_SANDBOX |
| 124 | /* |
| 125 | * Don't set timeout / delay values here. This results |
| 126 | * in these values still being reset to 0. |
| 127 | */ |
| 128 | if (state_get_skip_delays()) |
| 129 | return; |
| 130 | #endif |
| 131 | |
Stephen Warren | 0d437bc | 2014-05-19 14:21:17 -0600 | [diff] [blame] | 132 | /* |
| 133 | * Wait for power to become stable, |
| 134 | * plus spec-defined max time for device to connect |
Tim Harvey | 319418c | 2015-04-08 12:21:12 -0700 | [diff] [blame] | 135 | * but allow this time to be increased via env variable as some |
| 136 | * devices break the spec and require longer warm-up times |
Stephen Warren | 0d437bc | 2014-05-19 14:21:17 -0600 | [diff] [blame] | 137 | */ |
Tim Harvey | 319418c | 2015-04-08 12:21:12 -0700 | [diff] [blame] | 138 | env = getenv("usb_pgood_delay"); |
| 139 | if (env) |
| 140 | pgood_delay = max(pgood_delay, |
| 141 | (unsigned)simple_strtol(env, NULL, 0)); |
| 142 | debug("pgood_delay=%dms\n", pgood_delay); |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * Do a minimum delay of the larger value of 100ms or pgood_delay |
| 146 | * so that the power can stablize before the devices are queried |
| 147 | */ |
| 148 | hub->query_delay = get_timer(0) + max(100, (int)pgood_delay); |
| 149 | |
| 150 | /* |
| 151 | * Record the power-on timeout here. The max. delay (timeout) |
| 152 | * will be done based on this value in the USB port loop in |
| 153 | * usb_hub_configure() later. |
| 154 | */ |
| 155 | hub->connect_timeout = hub->query_delay + 1000; |
| 156 | debug("devnum=%d poweron: query_delay=%d connect_timeout=%d\n", |
| 157 | dev->devnum, max(100, (int)pgood_delay), |
| 158 | max(100, (int)pgood_delay) + 1000); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void usb_hub_reset(void) |
| 162 | { |
| 163 | usb_hub_index = 0; |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 164 | |
| 165 | /* Zero out global hub_dev in case its re-used again */ |
| 166 | memset(hub_dev, 0, sizeof(hub_dev)); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static struct usb_hub_device *usb_hub_allocate(void) |
| 170 | { |
| 171 | if (usb_hub_index < USB_MAX_HUB) |
| 172 | return &hub_dev[usb_hub_index++]; |
| 173 | |
| 174 | printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB); |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | #define MAX_TRIES 5 |
| 179 | |
| 180 | static inline char *portspeed(int portstatus) |
| 181 | { |
Vivek Gautam | 55f4b57 | 2013-04-24 02:50:12 +0000 | [diff] [blame] | 182 | char *speed_str; |
| 183 | |
| 184 | switch (portstatus & USB_PORT_STAT_SPEED_MASK) { |
| 185 | case USB_PORT_STAT_SUPER_SPEED: |
| 186 | speed_str = "5 Gb/s"; |
| 187 | break; |
| 188 | case USB_PORT_STAT_HIGH_SPEED: |
| 189 | speed_str = "480 Mb/s"; |
| 190 | break; |
| 191 | case USB_PORT_STAT_LOW_SPEED: |
| 192 | speed_str = "1.5 Mb/s"; |
| 193 | break; |
| 194 | default: |
| 195 | speed_str = "12 Mb/s"; |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | return speed_str; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Simon Glass | 862e75c | 2015-03-25 12:22:04 -0600 | [diff] [blame] | 202 | int legacy_hub_port_reset(struct usb_device *dev, int port, |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 203 | unsigned short *portstat) |
| 204 | { |
Hans de Goede | ad84a42 | 2015-05-10 14:10:15 +0200 | [diff] [blame] | 205 | int err, tries; |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 206 | ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 207 | unsigned short portstatus, portchange; |
Stefan Roese | f7f6010 | 2016-03-15 13:59:12 +0100 | [diff] [blame] | 208 | int delay = HUB_SHORT_RESET_TIME; /* start with short reset delay */ |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 209 | |
Simon Glass | 054fe48 | 2015-03-25 12:22:10 -0600 | [diff] [blame] | 210 | #ifdef CONFIG_DM_USB |
| 211 | debug("%s: resetting '%s' port %d...\n", __func__, dev->dev->name, |
| 212 | port + 1); |
| 213 | #else |
| 214 | debug("%s: resetting port %d...\n", __func__, port + 1); |
| 215 | #endif |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 216 | for (tries = 0; tries < MAX_TRIES; tries++) { |
Hans de Goede | ad84a42 | 2015-05-10 14:10:15 +0200 | [diff] [blame] | 217 | err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET); |
| 218 | if (err < 0) |
| 219 | return err; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 220 | |
Stefan Roese | f7f6010 | 2016-03-15 13:59:12 +0100 | [diff] [blame] | 221 | mdelay(delay); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 222 | |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 223 | if (usb_get_port_status(dev, port + 1, portsts) < 0) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 224 | debug("get_port_status failed status %lX\n", |
| 225 | dev->status); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 226 | return -1; |
| 227 | } |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 228 | portstatus = le16_to_cpu(portsts->wPortStatus); |
| 229 | portchange = le16_to_cpu(portsts->wPortChange); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 230 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 231 | debug("portstatus %x, change %x, %s\n", portstatus, portchange, |
| 232 | portspeed(portstatus)); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 233 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 234 | debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \ |
| 235 | " USB_PORT_STAT_ENABLE %d\n", |
| 236 | (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0, |
| 237 | (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0, |
| 238 | (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 239 | |
Stephen Warren | 74c0d75 | 2014-08-07 17:07:59 -0600 | [diff] [blame] | 240 | /* |
| 241 | * Perhaps we should check for the following here: |
| 242 | * - C_CONNECTION hasn't been set. |
| 243 | * - CONNECTION is still set. |
| 244 | * |
| 245 | * Doing so would ensure that the device is still connected |
| 246 | * to the bus, and hasn't been unplugged or replaced while the |
| 247 | * USB bus reset was going on. |
| 248 | * |
| 249 | * However, if we do that, then (at least) a San Disk Ultra |
| 250 | * USB 3.0 16GB device fails to reset on (at least) an NVIDIA |
| 251 | * Tegra Jetson TK1 board. For some reason, the device appears |
| 252 | * to briefly drop off the bus when this second bus reset is |
| 253 | * executed, yet if we retry this loop, it'll eventually come |
| 254 | * back after another reset or two. |
| 255 | */ |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 256 | |
| 257 | if (portstatus & USB_PORT_STAT_ENABLE) |
| 258 | break; |
| 259 | |
Stefan Roese | f7f6010 | 2016-03-15 13:59:12 +0100 | [diff] [blame] | 260 | /* Switch to long reset delay for the next round */ |
| 261 | delay = HUB_LONG_RESET_TIME; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | if (tries == MAX_TRIES) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 265 | debug("Cannot enable port %i after %i retries, " \ |
| 266 | "disabling port.\n", port + 1, MAX_TRIES); |
| 267 | debug("Maybe the USB cable is bad?\n"); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 268 | return -1; |
| 269 | } |
| 270 | |
| 271 | usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET); |
| 272 | *portstat = portstatus; |
| 273 | return 0; |
| 274 | } |
| 275 | |
Simon Glass | 054fe48 | 2015-03-25 12:22:10 -0600 | [diff] [blame] | 276 | #ifdef CONFIG_DM_USB |
| 277 | int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat) |
| 278 | { |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 279 | struct usb_device *udev = dev_get_parent_priv(dev); |
Simon Glass | 054fe48 | 2015-03-25 12:22:10 -0600 | [diff] [blame] | 280 | |
| 281 | return legacy_hub_port_reset(udev, port, portstat); |
| 282 | } |
| 283 | #endif |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 284 | |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 285 | int usb_hub_port_connect_change(struct usb_device *dev, int port) |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 286 | { |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 287 | ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 288 | unsigned short portstatus; |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 289 | int ret, speed; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 290 | |
| 291 | /* Check status */ |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 292 | ret = usb_get_port_status(dev, port + 1, portsts); |
| 293 | if (ret < 0) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 294 | debug("get_port_status failed\n"); |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 295 | return ret; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 298 | portstatus = le16_to_cpu(portsts->wPortStatus); |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 299 | debug("portstatus %x, change %x, %s\n", |
| 300 | portstatus, |
| 301 | le16_to_cpu(portsts->wPortChange), |
| 302 | portspeed(portstatus)); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 303 | |
| 304 | /* Clear the connection change status */ |
| 305 | usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION); |
| 306 | |
| 307 | /* Disconnect any existing devices under this port */ |
| 308 | if (((!(portstatus & USB_PORT_STAT_CONNECTION)) && |
Simon Glass | 054fe48 | 2015-03-25 12:22:10 -0600 | [diff] [blame] | 309 | (!(portstatus & USB_PORT_STAT_ENABLE))) || |
| 310 | usb_device_has_child_on_port(dev, port)) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 311 | debug("usb_disconnect(&hub->children[port]);\n"); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 312 | /* Return now if nothing is connected */ |
| 313 | if (!(portstatus & USB_PORT_STAT_CONNECTION)) |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 314 | return -ENOTCONN; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 315 | } |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 316 | |
| 317 | /* Reset the port */ |
Simon Glass | 862e75c | 2015-03-25 12:22:04 -0600 | [diff] [blame] | 318 | ret = legacy_hub_port_reset(dev, port, &portstatus); |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 319 | if (ret < 0) { |
Hans de Goede | 45b9ea1 | 2015-05-10 14:10:16 +0200 | [diff] [blame] | 320 | if (ret != -ENXIO) |
| 321 | printf("cannot reset port %i!?\n", port + 1); |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 322 | return ret; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Vivek Gautam | 55f4b57 | 2013-04-24 02:50:12 +0000 | [diff] [blame] | 325 | switch (portstatus & USB_PORT_STAT_SPEED_MASK) { |
| 326 | case USB_PORT_STAT_SUPER_SPEED: |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 327 | speed = USB_SPEED_SUPER; |
Vivek Gautam | 55f4b57 | 2013-04-24 02:50:12 +0000 | [diff] [blame] | 328 | break; |
| 329 | case USB_PORT_STAT_HIGH_SPEED: |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 330 | speed = USB_SPEED_HIGH; |
Vivek Gautam | 55f4b57 | 2013-04-24 02:50:12 +0000 | [diff] [blame] | 331 | break; |
| 332 | case USB_PORT_STAT_LOW_SPEED: |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 333 | speed = USB_SPEED_LOW; |
Vivek Gautam | 55f4b57 | 2013-04-24 02:50:12 +0000 | [diff] [blame] | 334 | break; |
| 335 | default: |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 336 | speed = USB_SPEED_FULL; |
Vivek Gautam | 55f4b57 | 2013-04-24 02:50:12 +0000 | [diff] [blame] | 337 | break; |
| 338 | } |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 339 | |
Simon Glass | 054fe48 | 2015-03-25 12:22:10 -0600 | [diff] [blame] | 340 | #ifdef CONFIG_DM_USB |
| 341 | struct udevice *child; |
| 342 | |
| 343 | ret = usb_scan_device(dev->dev, port + 1, speed, &child); |
| 344 | #else |
| 345 | struct usb_device *usb; |
| 346 | |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 347 | ret = usb_alloc_new_device(dev->controller, &usb); |
| 348 | if (ret) { |
| 349 | printf("cannot create new device: ret=%d", ret); |
| 350 | return ret; |
| 351 | } |
| 352 | |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 353 | dev->children[port] = usb; |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 354 | usb->speed = speed; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 355 | usb->parent = dev; |
| 356 | usb->portnr = port + 1; |
| 357 | /* Run it through the hoops (find a driver, etc) */ |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 358 | ret = usb_new_device(usb); |
| 359 | if (ret < 0) { |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 360 | /* Woops, disable the port */ |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 361 | usb_free_device(dev->controller); |
Milind Choudhary | 359439d | 2012-12-12 17:55:28 -0800 | [diff] [blame] | 362 | dev->children[port] = NULL; |
Simon Glass | 054fe48 | 2015-03-25 12:22:10 -0600 | [diff] [blame] | 363 | } |
| 364 | #endif |
| 365 | if (ret < 0) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 366 | debug("hub: disabling port %d\n", port + 1); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 367 | usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE); |
| 368 | } |
Simon Glass | 79b5888 | 2015-03-25 12:22:01 -0600 | [diff] [blame] | 369 | |
| 370 | return ret; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 373 | static int usb_scan_port(struct usb_device_scan *usb_scan) |
| 374 | { |
| 375 | ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); |
| 376 | unsigned short portstatus; |
| 377 | unsigned short portchange; |
| 378 | struct usb_device *dev; |
| 379 | struct usb_hub_device *hub; |
| 380 | int ret = 0; |
| 381 | int i; |
| 382 | |
| 383 | dev = usb_scan->dev; |
| 384 | hub = usb_scan->hub; |
| 385 | i = usb_scan->port; |
| 386 | |
| 387 | /* |
| 388 | * Don't talk to the device before the query delay is expired. |
| 389 | * This is needed for voltages to stabalize. |
| 390 | */ |
| 391 | if (get_timer(0) < hub->query_delay) |
| 392 | return 0; |
| 393 | |
| 394 | ret = usb_get_port_status(dev, i + 1, portsts); |
| 395 | if (ret < 0) { |
| 396 | debug("get_port_status failed\n"); |
| 397 | if (get_timer(0) >= hub->connect_timeout) { |
| 398 | debug("devnum=%d port=%d: timeout\n", |
| 399 | dev->devnum, i + 1); |
| 400 | /* Remove this device from scanning list */ |
| 401 | list_del(&usb_scan->list); |
| 402 | free(usb_scan); |
| 403 | return 0; |
| 404 | } |
Marek Vasut | d81db48 | 2016-05-03 22:22:59 +0200 | [diff] [blame] | 405 | return 0; |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | portstatus = le16_to_cpu(portsts->wPortStatus); |
| 409 | portchange = le16_to_cpu(portsts->wPortChange); |
| 410 | debug("Port %d Status %X Change %X\n", i + 1, portstatus, portchange); |
| 411 | |
| 412 | /* No connection change happened, wait a bit more. */ |
| 413 | if (!(portchange & USB_PORT_STAT_C_CONNECTION)) { |
| 414 | if (get_timer(0) >= hub->connect_timeout) { |
| 415 | debug("devnum=%d port=%d: timeout\n", |
| 416 | dev->devnum, i + 1); |
| 417 | /* Remove this device from scanning list */ |
| 418 | list_del(&usb_scan->list); |
| 419 | free(usb_scan); |
| 420 | return 0; |
| 421 | } |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | /* Test if the connection came up, and if not exit */ |
| 426 | if (!(portstatus & USB_PORT_STAT_CONNECTION)) |
| 427 | return 0; |
| 428 | |
| 429 | /* A new USB device is ready at this point */ |
| 430 | debug("devnum=%d port=%d: USB dev found\n", dev->devnum, i + 1); |
| 431 | |
| 432 | usb_hub_port_connect_change(dev, i); |
| 433 | |
| 434 | if (portchange & USB_PORT_STAT_C_ENABLE) { |
| 435 | debug("port %d enable change, status %x\n", i + 1, portstatus); |
| 436 | usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE); |
| 437 | /* |
| 438 | * The following hack causes a ghost device problem |
| 439 | * to Faraday EHCI |
| 440 | */ |
| 441 | #ifndef CONFIG_USB_EHCI_FARADAY |
| 442 | /* |
| 443 | * EM interference sometimes causes bad shielded USB |
| 444 | * devices to be shutdown by the hub, this hack enables |
| 445 | * them again. Works at least with mouse driver |
| 446 | */ |
| 447 | if (!(portstatus & USB_PORT_STAT_ENABLE) && |
| 448 | (portstatus & USB_PORT_STAT_CONNECTION) && |
| 449 | usb_device_has_child_on_port(dev, i)) { |
| 450 | debug("already running port %i disabled by hub (EMI?), re-enabling...\n", |
| 451 | i + 1); |
| 452 | usb_hub_port_connect_change(dev, i); |
| 453 | } |
| 454 | #endif |
| 455 | } |
| 456 | |
| 457 | if (portstatus & USB_PORT_STAT_SUSPEND) { |
| 458 | debug("port %d suspend change\n", i + 1); |
| 459 | usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_SUSPEND); |
| 460 | } |
| 461 | |
| 462 | if (portchange & USB_PORT_STAT_C_OVERCURRENT) { |
| 463 | debug("port %d over-current change\n", i + 1); |
| 464 | usb_clear_port_feature(dev, i + 1, |
| 465 | USB_PORT_FEAT_C_OVER_CURRENT); |
| 466 | /* Only power-on this one port */ |
| 467 | usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER); |
| 468 | hub->overcurrent_count[i]++; |
| 469 | |
| 470 | /* |
| 471 | * If the max-scan-count is not reached, return without removing |
| 472 | * the device from scan-list. This will re-issue a new scan. |
| 473 | */ |
| 474 | if (hub->overcurrent_count[i] <= |
| 475 | PORT_OVERCURRENT_MAX_SCAN_COUNT) |
| 476 | return 0; |
| 477 | |
| 478 | /* Otherwise the device will get removed */ |
Vagrant Cascadian | eae4b2b | 2016-04-30 19:18:00 -0700 | [diff] [blame] | 479 | printf("Port %d over-current occurred %d times\n", i + 1, |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 480 | hub->overcurrent_count[i]); |
| 481 | } |
| 482 | |
| 483 | if (portchange & USB_PORT_STAT_C_RESET) { |
| 484 | debug("port %d reset change\n", i + 1); |
| 485 | usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET); |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * We're done with this device, so let's remove this device from |
| 490 | * scanning list |
| 491 | */ |
| 492 | list_del(&usb_scan->list); |
| 493 | free(usb_scan); |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static int usb_device_list_scan(void) |
| 499 | { |
| 500 | struct usb_device_scan *usb_scan; |
| 501 | struct usb_device_scan *tmp; |
| 502 | static int running; |
| 503 | int ret = 0; |
| 504 | |
| 505 | /* Only run this loop once for each controller */ |
| 506 | if (running) |
| 507 | return 0; |
| 508 | |
| 509 | running = 1; |
| 510 | |
| 511 | while (1) { |
| 512 | /* We're done, once the list is empty again */ |
| 513 | if (list_empty(&usb_scan_list)) |
| 514 | goto out; |
| 515 | |
| 516 | list_for_each_entry_safe(usb_scan, tmp, &usb_scan_list, list) { |
| 517 | int ret; |
| 518 | |
| 519 | /* Scan this port */ |
| 520 | ret = usb_scan_port(usb_scan); |
| 521 | if (ret) |
| 522 | goto out; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | out: |
| 527 | /* |
| 528 | * This USB controller has finished scanning all its connected |
| 529 | * USB devices. Set "running" back to 0, so that other USB controllers |
| 530 | * will scan their devices too. |
| 531 | */ |
| 532 | running = 0; |
| 533 | |
| 534 | return ret; |
| 535 | } |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 536 | |
| 537 | static int usb_hub_configure(struct usb_device *dev) |
| 538 | { |
Julius Werner | eaf3e61 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 539 | int i, length; |
Puneet Saxena | f576613 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 540 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ); |
| 541 | unsigned char *bitmap; |
Lucas Stach | 93ad908 | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 542 | short hubCharacteristics; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 543 | struct usb_hub_descriptor *descriptor; |
| 544 | struct usb_hub_device *hub; |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 545 | __maybe_unused struct usb_hub_status *hubsts; |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 546 | int ret; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 547 | |
| 548 | /* "allocate" Hub device */ |
| 549 | hub = usb_hub_allocate(); |
| 550 | if (hub == NULL) |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 551 | return -ENOMEM; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 552 | hub->pusb_dev = dev; |
| 553 | /* Get the the hub descriptor */ |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 554 | ret = usb_get_hub_descriptor(dev, buffer, 4); |
| 555 | if (ret < 0) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 556 | debug("usb_hub_configure: failed to get hub " \ |
| 557 | "descriptor, giving up %lX\n", dev->status); |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 558 | return ret; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 559 | } |
| 560 | descriptor = (struct usb_hub_descriptor *)buffer; |
| 561 | |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 562 | length = min_t(int, descriptor->bLength, |
| 563 | sizeof(struct usb_hub_descriptor)); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 564 | |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 565 | ret = usb_get_hub_descriptor(dev, buffer, length); |
| 566 | if (ret < 0) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 567 | debug("usb_hub_configure: failed to get hub " \ |
| 568 | "descriptor 2nd giving up %lX\n", dev->status); |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 569 | return ret; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 570 | } |
Julius Werner | eaf3e61 | 2013-07-19 13:12:08 -0700 | [diff] [blame] | 571 | memcpy((unsigned char *)&hub->desc, buffer, length); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 572 | /* adjust 16bit values */ |
Lucas Stach | 93ad908 | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 573 | put_unaligned(le16_to_cpu(get_unaligned( |
| 574 | &descriptor->wHubCharacteristics)), |
| 575 | &hub->desc.wHubCharacteristics); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 576 | /* set the bitmap */ |
| 577 | bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0]; |
| 578 | /* devices not removable by default */ |
| 579 | memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); |
| 580 | bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0]; |
| 581 | memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */ |
| 582 | |
| 583 | for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++) |
| 584 | hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i]; |
| 585 | |
| 586 | for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++) |
| 587 | hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i]; |
| 588 | |
| 589 | dev->maxchild = descriptor->bNbrPorts; |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 590 | debug("%d ports detected\n", dev->maxchild); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 591 | |
Lucas Stach | 93ad908 | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 592 | hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics); |
| 593 | switch (hubCharacteristics & HUB_CHAR_LPSM) { |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 594 | case 0x00: |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 595 | debug("ganged power switching\n"); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 596 | break; |
| 597 | case 0x01: |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 598 | debug("individual port power switching\n"); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 599 | break; |
| 600 | case 0x02: |
| 601 | case 0x03: |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 602 | debug("unknown reserved power switching mode\n"); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 603 | break; |
| 604 | } |
| 605 | |
Lucas Stach | 93ad908 | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 606 | if (hubCharacteristics & HUB_CHAR_COMPOUND) |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 607 | debug("part of a compound device\n"); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 608 | else |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 609 | debug("standalone hub\n"); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 610 | |
Lucas Stach | 93ad908 | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 611 | switch (hubCharacteristics & HUB_CHAR_OCPM) { |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 612 | case 0x00: |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 613 | debug("global over-current protection\n"); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 614 | break; |
| 615 | case 0x08: |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 616 | debug("individual port over-current protection\n"); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 617 | break; |
| 618 | case 0x10: |
| 619 | case 0x18: |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 620 | debug("no over-current protection\n"); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 621 | break; |
| 622 | } |
| 623 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 624 | debug("power on to power good time: %dms\n", |
| 625 | descriptor->bPwrOn2PwrGood * 2); |
| 626 | debug("hub controller current requirement: %dmA\n", |
| 627 | descriptor->bHubContrCurrent); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 628 | |
| 629 | for (i = 0; i < dev->maxchild; i++) |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 630 | debug("port %d is%s removable\n", i + 1, |
| 631 | hub->desc.DeviceRemovable[(i + 1) / 8] & \ |
| 632 | (1 << ((i + 1) % 8)) ? " not" : ""); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 633 | |
| 634 | if (sizeof(struct usb_hub_status) > USB_BUFSIZ) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 635 | debug("usb_hub_configure: failed to get Status - " \ |
| 636 | "too long: %d\n", descriptor->bLength); |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 637 | return -EFBIG; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 640 | ret = usb_get_hub_status(dev, buffer); |
| 641 | if (ret < 0) { |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 642 | debug("usb_hub_configure: failed to get Status %lX\n", |
| 643 | dev->status); |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 644 | return ret; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 647 | #ifdef DEBUG |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 648 | hubsts = (struct usb_hub_status *)buffer; |
| 649 | #endif |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 650 | |
| 651 | debug("get_hub_status returned status %X, change %X\n", |
| 652 | le16_to_cpu(hubsts->wHubStatus), |
| 653 | le16_to_cpu(hubsts->wHubChange)); |
| 654 | debug("local power source is %s\n", |
| 655 | (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \ |
| 656 | "lost (inactive)" : "good"); |
| 657 | debug("%sover-current condition exists\n", |
| 658 | (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \ |
| 659 | "" : "no "); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 660 | usb_hub_power_on(hub); |
| 661 | |
Dan Murphy | 3615a99 | 2013-08-01 14:06:01 -0500 | [diff] [blame] | 662 | /* |
| 663 | * Reset any devices that may be in a bad state when applying |
| 664 | * the power. This is a __weak function. Resetting of the devices |
| 665 | * should occur in the board file of the device. |
| 666 | */ |
| 667 | for (i = 0; i < dev->maxchild; i++) |
| 668 | usb_hub_reset_devices(i + 1); |
| 669 | |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 670 | /* |
| 671 | * Only add the connected USB devices, including potential hubs, |
| 672 | * to a scanning list. This list will get scanned and devices that |
| 673 | * are detected (either via port connected or via port timeout) |
| 674 | * will get removed from this list. Scanning of the devices on this |
| 675 | * list will continue until all devices are removed. |
| 676 | */ |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 677 | for (i = 0; i < dev->maxchild; i++) { |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 678 | struct usb_device_scan *usb_scan; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 679 | |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 680 | usb_scan = calloc(1, sizeof(*usb_scan)); |
| 681 | if (!usb_scan) { |
| 682 | printf("Can't allocate memory for USB device!\n"); |
| 683 | return -ENOMEM; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 684 | } |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 685 | usb_scan->dev = dev; |
| 686 | usb_scan->hub = hub; |
| 687 | usb_scan->port = i; |
| 688 | list_add_tail(&usb_scan->list, &usb_scan_list); |
| 689 | } |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 690 | |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 691 | /* |
| 692 | * And now call the scanning code which loops over the generated list |
| 693 | */ |
| 694 | ret = usb_device_list_scan(); |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 695 | |
Stefan Roese | c998da0 | 2016-03-15 13:59:15 +0100 | [diff] [blame] | 696 | return ret; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 699 | static int usb_hub_check(struct usb_device *dev, int ifnum) |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 700 | { |
| 701 | struct usb_interface *iface; |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 702 | struct usb_endpoint_descriptor *ep = NULL; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 703 | |
| 704 | iface = &dev->config.if_desc[ifnum]; |
| 705 | /* Is it a hub? */ |
| 706 | if (iface->desc.bInterfaceClass != USB_CLASS_HUB) |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 707 | goto err; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 708 | /* Some hubs have a subclass of 1, which AFAICT according to the */ |
| 709 | /* specs is not defined, but it works */ |
| 710 | if ((iface->desc.bInterfaceSubClass != 0) && |
| 711 | (iface->desc.bInterfaceSubClass != 1)) |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 712 | goto err; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 713 | /* Multiple endpoints? What kind of mutant ninja-hub is this? */ |
| 714 | if (iface->desc.bNumEndpoints != 1) |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 715 | goto err; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 716 | ep = &iface->ep_desc[0]; |
| 717 | /* Output endpoint? Curiousier and curiousier.. */ |
| 718 | if (!(ep->bEndpointAddress & USB_DIR_IN)) |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 719 | goto err; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 720 | /* If it's not an interrupt endpoint, we'd better punt! */ |
| 721 | if ((ep->bmAttributes & 3) != 3) |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 722 | goto err; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 723 | /* We found a hub */ |
Vivek Gautam | ceb4972 | 2013-04-12 16:34:33 +0530 | [diff] [blame] | 724 | debug("USB hub found\n"); |
Simon Glass | 361ad6a | 2015-03-25 12:22:09 -0600 | [diff] [blame] | 725 | return 0; |
| 726 | |
| 727 | err: |
| 728 | debug("USB hub not found: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n", |
| 729 | iface->desc.bInterfaceClass, iface->desc.bInterfaceSubClass, |
| 730 | iface->desc.bNumEndpoints); |
| 731 | if (ep) { |
| 732 | debug(" bEndpointAddress=%#x, bmAttributes=%d", |
| 733 | ep->bEndpointAddress, ep->bmAttributes); |
| 734 | } |
| 735 | |
| 736 | return -ENOENT; |
| 737 | } |
| 738 | |
| 739 | int usb_hub_probe(struct usb_device *dev, int ifnum) |
| 740 | { |
| 741 | int ret; |
| 742 | |
| 743 | ret = usb_hub_check(dev, ifnum); |
| 744 | if (ret) |
| 745 | return 0; |
Marek Vasut | 23faf2b | 2012-02-13 18:58:17 +0000 | [diff] [blame] | 746 | ret = usb_hub_configure(dev); |
| 747 | return ret; |
| 748 | } |
Simon Glass | 054fe48 | 2015-03-25 12:22:10 -0600 | [diff] [blame] | 749 | |
| 750 | #ifdef CONFIG_DM_USB |
| 751 | int usb_hub_scan(struct udevice *hub) |
| 752 | { |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 753 | struct usb_device *udev = dev_get_parent_priv(hub); |
Simon Glass | 054fe48 | 2015-03-25 12:22:10 -0600 | [diff] [blame] | 754 | |
| 755 | return usb_hub_configure(udev); |
| 756 | } |
| 757 | |
| 758 | static int usb_hub_post_bind(struct udevice *dev) |
| 759 | { |
| 760 | /* Scan the bus for devices */ |
| 761 | return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false); |
| 762 | } |
| 763 | |
| 764 | static int usb_hub_post_probe(struct udevice *dev) |
| 765 | { |
| 766 | debug("%s\n", __func__); |
| 767 | return usb_hub_scan(dev); |
| 768 | } |
| 769 | |
| 770 | static const struct udevice_id usb_hub_ids[] = { |
| 771 | { .compatible = "usb-hub" }, |
| 772 | { } |
| 773 | }; |
| 774 | |
| 775 | U_BOOT_DRIVER(usb_generic_hub) = { |
| 776 | .name = "usb_hub", |
| 777 | .id = UCLASS_USB_HUB, |
| 778 | .of_match = usb_hub_ids, |
| 779 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 780 | }; |
| 781 | |
| 782 | UCLASS_DRIVER(usb_hub) = { |
| 783 | .id = UCLASS_USB_HUB, |
| 784 | .name = "usb_hub", |
| 785 | .post_bind = usb_hub_post_bind, |
| 786 | .post_probe = usb_hub_post_probe, |
| 787 | .child_pre_probe = usb_child_pre_probe, |
| 788 | .per_child_auto_alloc_size = sizeof(struct usb_device), |
| 789 | .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata), |
| 790 | }; |
| 791 | |
| 792 | static const struct usb_device_id hub_id_table[] = { |
| 793 | { |
| 794 | .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, |
| 795 | .bDeviceClass = USB_CLASS_HUB |
| 796 | }, |
| 797 | { } /* Terminating entry */ |
| 798 | }; |
| 799 | |
Simon Glass | abb59cf | 2015-07-06 16:47:51 -0600 | [diff] [blame] | 800 | U_BOOT_USB_DEVICE(usb_generic_hub, hub_id_table); |
Simon Glass | 054fe48 | 2015-03-25 12:22:10 -0600 | [diff] [blame] | 801 | |
| 802 | #endif |