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