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