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