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