blob: ff9cd50eab7c4f7a335572f0e226f53bb285c04c [file] [log] [blame]
Marek Vasut6da5af32012-02-13 18:58:17 +00001/*
Marek Vasut6da5af32012-02-13 18:58:17 +00002 * 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 Denkd79de1d2013-07-08 09:37:19 +020017 * SPDX-License-Identifier: GPL-2.0+
Marek Vasut6da5af32012-02-13 18:58:17 +000018 */
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 Glass6ab43b62015-03-25 12:22:10 -060027#include <dm.h>
Simon Glassd1461132015-03-25 12:22:01 -060028#include <errno.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060029#include <memalign.h>
Marek Vasut6da5af32012-02-13 18:58:17 +000030#include <asm/processor.h>
Lucas Stach835e11e2012-09-06 08:00:13 +020031#include <asm/unaligned.h>
Marek Vasut6da5af32012-02-13 18:58:17 +000032#include <linux/ctype.h>
Stefan Roeseed4823a2016-03-15 13:59:15 +010033#include <linux/list.h>
Marek Vasut6da5af32012-02-13 18:58:17 +000034#include <asm/byteorder.h>
Simon Glass2d7c4992015-11-08 23:47:44 -070035#ifdef CONFIG_SANDBOX
36#include <asm/state.h>
37#endif
Marek Vasut6da5af32012-02-13 18:58:17 +000038#include <asm/unaligned.h>
Simon Glass6ab43b62015-03-25 12:22:10 -060039
40DECLARE_GLOBAL_DATA_PTR;
Marek Vasut6da5af32012-02-13 18:58:17 +000041
42#include <usb.h>
43#ifdef CONFIG_4xx
44#include <asm/4xx_pci.h>
45#endif
46
Marek Vasut6da5af32012-02-13 18:58:17 +000047#define USB_BUFSIZ 512
48
Stefan Roese3678cb52016-03-15 13:59:12 +010049#define HUB_SHORT_RESET_TIME 20
50#define HUB_LONG_RESET_TIME 200
51
Stefan Roeseed4823a2016-03-15 13:59:15 +010052#define PORT_OVERCURRENT_MAX_SCAN_COUNT 3
53
54struct usb_device_scan {
55 struct usb_device *dev; /* USB hub device to scan */
56 struct usb_hub_device *hub; /* USB hub struct */
57 int port; /* USB port to scan */
58 struct list_head list;
59};
60
Simon Glass6ab43b62015-03-25 12:22:10 -060061/* TODO(sjg@chromium.org): Remove this when CONFIG_DM_USB is defined */
Marek Vasut6da5af32012-02-13 18:58:17 +000062static struct usb_hub_device hub_dev[USB_MAX_HUB];
63static int usb_hub_index;
Stefan Roeseed4823a2016-03-15 13:59:15 +010064static LIST_HEAD(usb_scan_list);
Marek Vasut6da5af32012-02-13 18:58:17 +000065
Dan Murphy2666bb12013-08-01 14:06:01 -050066__weak void usb_hub_reset_devices(int port)
67{
68 return;
69}
Marek Vasut6da5af32012-02-13 18:58:17 +000070
71static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
72{
73 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
74 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
75 USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
76}
77
78static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
79{
80 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
81 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
82 port, NULL, 0, USB_CNTL_TIMEOUT);
83}
84
85static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
86{
87 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
88 USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
89 port, NULL, 0, USB_CNTL_TIMEOUT);
90}
91
92static int usb_get_hub_status(struct usb_device *dev, void *data)
93{
94 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
95 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
96 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
97}
98
Vincent Palatin0489ee32015-05-04 11:30:54 -060099int usb_get_port_status(struct usb_device *dev, int port, void *data)
Marek Vasut6da5af32012-02-13 18:58:17 +0000100{
101 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
102 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
103 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
104}
105
106
107static void usb_hub_power_on(struct usb_hub_device *hub)
108{
109 int i;
110 struct usb_device *dev;
Wolfgang Grandegger9bb46e02011-12-21 00:01:09 +0000111 unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
Tim Harveyacda1d52015-04-08 12:21:12 -0700112 const char *env;
Marek Vasut6da5af32012-02-13 18:58:17 +0000113
114 dev = hub->pusb_dev;
Vivek Gautamc9b13d62013-04-24 02:50:11 +0000115
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530116 debug("enabling power on all ports\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000117 for (i = 0; i < dev->maxchild; i++) {
118 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530119 debug("port %d returns %lX\n", i + 1, dev->status);
Marek Vasut6da5af32012-02-13 18:58:17 +0000120 }
Wolfgang Grandegger9bb46e02011-12-21 00:01:09 +0000121
Stefan Roeseed4823a2016-03-15 13:59:15 +0100122#ifdef CONFIG_SANDBOX
123 /*
124 * Don't set timeout / delay values here. This results
125 * in these values still being reset to 0.
126 */
127 if (state_get_skip_delays())
128 return;
129#endif
130
Stephen Warren62da17c2014-05-19 14:21:17 -0600131 /*
132 * Wait for power to become stable,
133 * plus spec-defined max time for device to connect
Tim Harveyacda1d52015-04-08 12:21:12 -0700134 * but allow this time to be increased via env variable as some
135 * devices break the spec and require longer warm-up times
Stephen Warren62da17c2014-05-19 14:21:17 -0600136 */
Tim Harveyacda1d52015-04-08 12:21:12 -0700137 env = getenv("usb_pgood_delay");
138 if (env)
139 pgood_delay = max(pgood_delay,
140 (unsigned)simple_strtol(env, NULL, 0));
141 debug("pgood_delay=%dms\n", pgood_delay);
Stefan Roeseed4823a2016-03-15 13:59:15 +0100142
143 /*
144 * Do a minimum delay of the larger value of 100ms or pgood_delay
145 * so that the power can stablize before the devices are queried
146 */
147 hub->query_delay = get_timer(0) + max(100, (int)pgood_delay);
148
149 /*
150 * Record the power-on timeout here. The max. delay (timeout)
151 * will be done based on this value in the USB port loop in
152 * usb_hub_configure() later.
153 */
154 hub->connect_timeout = hub->query_delay + 1000;
155 debug("devnum=%d poweron: query_delay=%d connect_timeout=%d\n",
156 dev->devnum, max(100, (int)pgood_delay),
157 max(100, (int)pgood_delay) + 1000);
Marek Vasut6da5af32012-02-13 18:58:17 +0000158}
159
160void usb_hub_reset(void)
161{
162 usb_hub_index = 0;
Stefan Roeseed4823a2016-03-15 13:59:15 +0100163
164 /* Zero out global hub_dev in case its re-used again */
165 memset(hub_dev, 0, sizeof(hub_dev));
Marek Vasut6da5af32012-02-13 18:58:17 +0000166}
167
168static struct usb_hub_device *usb_hub_allocate(void)
169{
170 if (usb_hub_index < USB_MAX_HUB)
171 return &hub_dev[usb_hub_index++];
172
173 printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
174 return NULL;
175}
176
177#define MAX_TRIES 5
178
179static inline char *portspeed(int portstatus)
180{
Vivek Gautam86c35be2013-04-24 02:50:12 +0000181 char *speed_str;
182
183 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
184 case USB_PORT_STAT_SUPER_SPEED:
185 speed_str = "5 Gb/s";
186 break;
187 case USB_PORT_STAT_HIGH_SPEED:
188 speed_str = "480 Mb/s";
189 break;
190 case USB_PORT_STAT_LOW_SPEED:
191 speed_str = "1.5 Mb/s";
192 break;
193 default:
194 speed_str = "12 Mb/s";
195 break;
196 }
197
198 return speed_str;
Marek Vasut6da5af32012-02-13 18:58:17 +0000199}
200
Simon Glassc093d6d2015-03-25 12:22:04 -0600201int legacy_hub_port_reset(struct usb_device *dev, int port,
Marek Vasut6da5af32012-02-13 18:58:17 +0000202 unsigned short *portstat)
203{
Hans de Goede26177522015-05-10 14:10:15 +0200204 int err, tries;
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530205 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut6da5af32012-02-13 18:58:17 +0000206 unsigned short portstatus, portchange;
Stefan Roese3678cb52016-03-15 13:59:12 +0100207 int delay = HUB_SHORT_RESET_TIME; /* start with short reset delay */
Marek Vasut6da5af32012-02-13 18:58:17 +0000208
Simon Glass6ab43b62015-03-25 12:22:10 -0600209#ifdef CONFIG_DM_USB
210 debug("%s: resetting '%s' port %d...\n", __func__, dev->dev->name,
211 port + 1);
212#else
213 debug("%s: resetting port %d...\n", __func__, port + 1);
214#endif
Marek Vasut6da5af32012-02-13 18:58:17 +0000215 for (tries = 0; tries < MAX_TRIES; tries++) {
Hans de Goede26177522015-05-10 14:10:15 +0200216 err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
217 if (err < 0)
218 return err;
Marek Vasut6da5af32012-02-13 18:58:17 +0000219
Stefan Roese3678cb52016-03-15 13:59:12 +0100220 mdelay(delay);
Marek Vasut6da5af32012-02-13 18:58:17 +0000221
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530222 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530223 debug("get_port_status failed status %lX\n",
224 dev->status);
Marek Vasut6da5af32012-02-13 18:58:17 +0000225 return -1;
226 }
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530227 portstatus = le16_to_cpu(portsts->wPortStatus);
228 portchange = le16_to_cpu(portsts->wPortChange);
Marek Vasut6da5af32012-02-13 18:58:17 +0000229
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530230 debug("portstatus %x, change %x, %s\n", portstatus, portchange,
231 portspeed(portstatus));
Marek Vasut6da5af32012-02-13 18:58:17 +0000232
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530233 debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
234 " USB_PORT_STAT_ENABLE %d\n",
235 (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
236 (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
237 (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
Marek Vasut6da5af32012-02-13 18:58:17 +0000238
Stephen Warren3a8e4fa2014-08-07 17:07:59 -0600239 /*
240 * Perhaps we should check for the following here:
241 * - C_CONNECTION hasn't been set.
242 * - CONNECTION is still set.
243 *
244 * Doing so would ensure that the device is still connected
245 * to the bus, and hasn't been unplugged or replaced while the
246 * USB bus reset was going on.
247 *
248 * However, if we do that, then (at least) a San Disk Ultra
249 * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
250 * Tegra Jetson TK1 board. For some reason, the device appears
251 * to briefly drop off the bus when this second bus reset is
252 * executed, yet if we retry this loop, it'll eventually come
253 * back after another reset or two.
254 */
Marek Vasut6da5af32012-02-13 18:58:17 +0000255
256 if (portstatus & USB_PORT_STAT_ENABLE)
257 break;
258
Stefan Roese3678cb52016-03-15 13:59:12 +0100259 /* Switch to long reset delay for the next round */
260 delay = HUB_LONG_RESET_TIME;
Marek Vasut6da5af32012-02-13 18:58:17 +0000261 }
262
263 if (tries == MAX_TRIES) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530264 debug("Cannot enable port %i after %i retries, " \
265 "disabling port.\n", port + 1, MAX_TRIES);
266 debug("Maybe the USB cable is bad?\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000267 return -1;
268 }
269
270 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
271 *portstat = portstatus;
272 return 0;
273}
274
Simon Glass6ab43b62015-03-25 12:22:10 -0600275#ifdef CONFIG_DM_USB
276int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat)
277{
Simon Glassde44acf2015-09-28 23:32:01 -0600278 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glass6ab43b62015-03-25 12:22:10 -0600279
280 return legacy_hub_port_reset(udev, port, portstat);
281}
282#endif
Marek Vasut6da5af32012-02-13 18:58:17 +0000283
Simon Glassd1461132015-03-25 12:22:01 -0600284int usb_hub_port_connect_change(struct usb_device *dev, int port)
Marek Vasut6da5af32012-02-13 18:58:17 +0000285{
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530286 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
Marek Vasut6da5af32012-02-13 18:58:17 +0000287 unsigned short portstatus;
Simon Glassd1461132015-03-25 12:22:01 -0600288 int ret, speed;
Marek Vasut6da5af32012-02-13 18:58:17 +0000289
290 /* Check status */
Simon Glassd1461132015-03-25 12:22:01 -0600291 ret = usb_get_port_status(dev, port + 1, portsts);
292 if (ret < 0) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530293 debug("get_port_status failed\n");
Simon Glassd1461132015-03-25 12:22:01 -0600294 return ret;
Marek Vasut6da5af32012-02-13 18:58:17 +0000295 }
296
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530297 portstatus = le16_to_cpu(portsts->wPortStatus);
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530298 debug("portstatus %x, change %x, %s\n",
299 portstatus,
300 le16_to_cpu(portsts->wPortChange),
301 portspeed(portstatus));
Marek Vasut6da5af32012-02-13 18:58:17 +0000302
303 /* Clear the connection change status */
304 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
305
306 /* Disconnect any existing devices under this port */
307 if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
Simon Glass6ab43b62015-03-25 12:22:10 -0600308 (!(portstatus & USB_PORT_STAT_ENABLE))) ||
309 usb_device_has_child_on_port(dev, port)) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530310 debug("usb_disconnect(&hub->children[port]);\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000311 /* Return now if nothing is connected */
312 if (!(portstatus & USB_PORT_STAT_CONNECTION))
Simon Glassd1461132015-03-25 12:22:01 -0600313 return -ENOTCONN;
Marek Vasut6da5af32012-02-13 18:58:17 +0000314 }
Marek Vasut6da5af32012-02-13 18:58:17 +0000315
316 /* Reset the port */
Simon Glassc093d6d2015-03-25 12:22:04 -0600317 ret = legacy_hub_port_reset(dev, port, &portstatus);
Simon Glassd1461132015-03-25 12:22:01 -0600318 if (ret < 0) {
Hans de Goede63f34ca2015-05-10 14:10:16 +0200319 if (ret != -ENXIO)
320 printf("cannot reset port %i!?\n", port + 1);
Simon Glassd1461132015-03-25 12:22:01 -0600321 return ret;
Marek Vasut6da5af32012-02-13 18:58:17 +0000322 }
323
Vivek Gautam86c35be2013-04-24 02:50:12 +0000324 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
325 case USB_PORT_STAT_SUPER_SPEED:
Simon Glassd1461132015-03-25 12:22:01 -0600326 speed = USB_SPEED_SUPER;
Vivek Gautam86c35be2013-04-24 02:50:12 +0000327 break;
328 case USB_PORT_STAT_HIGH_SPEED:
Simon Glassd1461132015-03-25 12:22:01 -0600329 speed = USB_SPEED_HIGH;
Vivek Gautam86c35be2013-04-24 02:50:12 +0000330 break;
331 case USB_PORT_STAT_LOW_SPEED:
Simon Glassd1461132015-03-25 12:22:01 -0600332 speed = USB_SPEED_LOW;
Vivek Gautam86c35be2013-04-24 02:50:12 +0000333 break;
334 default:
Simon Glassd1461132015-03-25 12:22:01 -0600335 speed = USB_SPEED_FULL;
Vivek Gautam86c35be2013-04-24 02:50:12 +0000336 break;
337 }
Marek Vasut6da5af32012-02-13 18:58:17 +0000338
Simon Glass6ab43b62015-03-25 12:22:10 -0600339#ifdef CONFIG_DM_USB
340 struct udevice *child;
341
342 ret = usb_scan_device(dev->dev, port + 1, speed, &child);
343#else
344 struct usb_device *usb;
345
Simon Glassd1461132015-03-25 12:22:01 -0600346 ret = usb_alloc_new_device(dev->controller, &usb);
347 if (ret) {
348 printf("cannot create new device: ret=%d", ret);
349 return ret;
350 }
351
Marek Vasut6da5af32012-02-13 18:58:17 +0000352 dev->children[port] = usb;
Simon Glassd1461132015-03-25 12:22:01 -0600353 usb->speed = speed;
Marek Vasut6da5af32012-02-13 18:58:17 +0000354 usb->parent = dev;
355 usb->portnr = port + 1;
356 /* Run it through the hoops (find a driver, etc) */
Simon Glassd1461132015-03-25 12:22:01 -0600357 ret = usb_new_device(usb);
358 if (ret < 0) {
Marek Vasut6da5af32012-02-13 18:58:17 +0000359 /* Woops, disable the port */
Simon Glassd1461132015-03-25 12:22:01 -0600360 usb_free_device(dev->controller);
Milind Choudharyebb24782012-12-12 17:55:28 -0800361 dev->children[port] = NULL;
Simon Glass6ab43b62015-03-25 12:22:10 -0600362 }
363#endif
364 if (ret < 0) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530365 debug("hub: disabling port %d\n", port + 1);
Marek Vasut6da5af32012-02-13 18:58:17 +0000366 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
367 }
Simon Glassd1461132015-03-25 12:22:01 -0600368
369 return ret;
Marek Vasut6da5af32012-02-13 18:58:17 +0000370}
371
Stefan Roeseed4823a2016-03-15 13:59:15 +0100372static int usb_scan_port(struct usb_device_scan *usb_scan)
373{
374 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
375 unsigned short portstatus;
376 unsigned short portchange;
377 struct usb_device *dev;
378 struct usb_hub_device *hub;
379 int ret = 0;
380 int i;
381
382 dev = usb_scan->dev;
383 hub = usb_scan->hub;
384 i = usb_scan->port;
385
386 /*
387 * Don't talk to the device before the query delay is expired.
388 * This is needed for voltages to stabalize.
389 */
390 if (get_timer(0) < hub->query_delay)
391 return 0;
392
393 ret = usb_get_port_status(dev, i + 1, portsts);
394 if (ret < 0) {
395 debug("get_port_status failed\n");
396 if (get_timer(0) >= hub->connect_timeout) {
397 debug("devnum=%d port=%d: timeout\n",
398 dev->devnum, i + 1);
399 /* Remove this device from scanning list */
400 list_del(&usb_scan->list);
401 free(usb_scan);
402 return 0;
403 }
Marek Vasut614d9dd2016-05-03 22:22:59 +0200404 return 0;
Stefan Roeseed4823a2016-03-15 13:59:15 +0100405 }
406
407 portstatus = le16_to_cpu(portsts->wPortStatus);
408 portchange = le16_to_cpu(portsts->wPortChange);
409 debug("Port %d Status %X Change %X\n", i + 1, portstatus, portchange);
410
411 /* No connection change happened, wait a bit more. */
412 if (!(portchange & USB_PORT_STAT_C_CONNECTION)) {
413 if (get_timer(0) >= hub->connect_timeout) {
414 debug("devnum=%d port=%d: timeout\n",
415 dev->devnum, i + 1);
416 /* Remove this device from scanning list */
417 list_del(&usb_scan->list);
418 free(usb_scan);
419 return 0;
420 }
421 return 0;
422 }
423
424 /* Test if the connection came up, and if not exit */
425 if (!(portstatus & USB_PORT_STAT_CONNECTION))
426 return 0;
427
428 /* A new USB device is ready at this point */
429 debug("devnum=%d port=%d: USB dev found\n", dev->devnum, i + 1);
430
431 usb_hub_port_connect_change(dev, i);
432
433 if (portchange & USB_PORT_STAT_C_ENABLE) {
434 debug("port %d enable change, status %x\n", i + 1, portstatus);
435 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
436 /*
437 * The following hack causes a ghost device problem
438 * to Faraday EHCI
439 */
440#ifndef CONFIG_USB_EHCI_FARADAY
441 /*
442 * EM interference sometimes causes bad shielded USB
443 * devices to be shutdown by the hub, this hack enables
444 * them again. Works at least with mouse driver
445 */
446 if (!(portstatus & USB_PORT_STAT_ENABLE) &&
447 (portstatus & USB_PORT_STAT_CONNECTION) &&
448 usb_device_has_child_on_port(dev, i)) {
449 debug("already running port %i disabled by hub (EMI?), re-enabling...\n",
450 i + 1);
451 usb_hub_port_connect_change(dev, i);
452 }
453#endif
454 }
455
456 if (portstatus & USB_PORT_STAT_SUSPEND) {
457 debug("port %d suspend change\n", i + 1);
458 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_SUSPEND);
459 }
460
461 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
462 debug("port %d over-current change\n", i + 1);
463 usb_clear_port_feature(dev, i + 1,
464 USB_PORT_FEAT_C_OVER_CURRENT);
465 /* Only power-on this one port */
466 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
467 hub->overcurrent_count[i]++;
468
469 /*
470 * If the max-scan-count is not reached, return without removing
471 * the device from scan-list. This will re-issue a new scan.
472 */
473 if (hub->overcurrent_count[i] <=
474 PORT_OVERCURRENT_MAX_SCAN_COUNT)
475 return 0;
476
477 /* Otherwise the device will get removed */
Vagrant Cascadianedfdb992016-04-30 19:18:00 -0700478 printf("Port %d over-current occurred %d times\n", i + 1,
Stefan Roeseed4823a2016-03-15 13:59:15 +0100479 hub->overcurrent_count[i]);
480 }
481
482 if (portchange & USB_PORT_STAT_C_RESET) {
483 debug("port %d reset change\n", i + 1);
484 usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
485 }
486
487 /*
488 * We're done with this device, so let's remove this device from
489 * scanning list
490 */
491 list_del(&usb_scan->list);
492 free(usb_scan);
493
494 return 0;
495}
496
497static int usb_device_list_scan(void)
498{
499 struct usb_device_scan *usb_scan;
500 struct usb_device_scan *tmp;
501 static int running;
502 int ret = 0;
503
504 /* Only run this loop once for each controller */
505 if (running)
506 return 0;
507
508 running = 1;
509
510 while (1) {
511 /* We're done, once the list is empty again */
512 if (list_empty(&usb_scan_list))
513 goto out;
514
515 list_for_each_entry_safe(usb_scan, tmp, &usb_scan_list, list) {
516 int ret;
517
518 /* Scan this port */
519 ret = usb_scan_port(usb_scan);
520 if (ret)
521 goto out;
522 }
523 }
524
525out:
526 /*
527 * This USB controller has finished scanning all its connected
528 * USB devices. Set "running" back to 0, so that other USB controllers
529 * will scan their devices too.
530 */
531 running = 0;
532
533 return ret;
534}
Marek Vasut6da5af32012-02-13 18:58:17 +0000535
536static int usb_hub_configure(struct usb_device *dev)
537{
Julius Wernerfc4ec492013-07-19 13:12:08 -0700538 int i, length;
Puneet Saxena6c9bb602012-04-03 14:56:06 +0530539 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
540 unsigned char *bitmap;
Lucas Stach835e11e2012-09-06 08:00:13 +0200541 short hubCharacteristics;
Marek Vasut6da5af32012-02-13 18:58:17 +0000542 struct usb_hub_descriptor *descriptor;
543 struct usb_hub_device *hub;
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530544 __maybe_unused struct usb_hub_status *hubsts;
Simon Glassf977c182015-03-25 12:22:09 -0600545 int ret;
Marek Vasut6da5af32012-02-13 18:58:17 +0000546
547 /* "allocate" Hub device */
548 hub = usb_hub_allocate();
549 if (hub == NULL)
Simon Glassf977c182015-03-25 12:22:09 -0600550 return -ENOMEM;
Marek Vasut6da5af32012-02-13 18:58:17 +0000551 hub->pusb_dev = dev;
552 /* Get the the hub descriptor */
Simon Glassf977c182015-03-25 12:22:09 -0600553 ret = usb_get_hub_descriptor(dev, buffer, 4);
554 if (ret < 0) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530555 debug("usb_hub_configure: failed to get hub " \
556 "descriptor, giving up %lX\n", dev->status);
Simon Glassf977c182015-03-25 12:22:09 -0600557 return ret;
Marek Vasut6da5af32012-02-13 18:58:17 +0000558 }
559 descriptor = (struct usb_hub_descriptor *)buffer;
560
Masahiro Yamadadb204642014-11-07 03:03:31 +0900561 length = min_t(int, descriptor->bLength,
562 sizeof(struct usb_hub_descriptor));
Marek Vasut6da5af32012-02-13 18:58:17 +0000563
Simon Glassf977c182015-03-25 12:22:09 -0600564 ret = usb_get_hub_descriptor(dev, buffer, length);
565 if (ret < 0) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530566 debug("usb_hub_configure: failed to get hub " \
567 "descriptor 2nd giving up %lX\n", dev->status);
Simon Glassf977c182015-03-25 12:22:09 -0600568 return ret;
Marek Vasut6da5af32012-02-13 18:58:17 +0000569 }
Julius Wernerfc4ec492013-07-19 13:12:08 -0700570 memcpy((unsigned char *)&hub->desc, buffer, length);
Marek Vasut6da5af32012-02-13 18:58:17 +0000571 /* adjust 16bit values */
Lucas Stach835e11e2012-09-06 08:00:13 +0200572 put_unaligned(le16_to_cpu(get_unaligned(
573 &descriptor->wHubCharacteristics)),
574 &hub->desc.wHubCharacteristics);
Marek Vasut6da5af32012-02-13 18:58:17 +0000575 /* set the bitmap */
576 bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
577 /* devices not removable by default */
578 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
579 bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
580 memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
581
582 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
583 hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
584
585 for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
586 hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
587
588 dev->maxchild = descriptor->bNbrPorts;
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530589 debug("%d ports detected\n", dev->maxchild);
Marek Vasut6da5af32012-02-13 18:58:17 +0000590
Lucas Stach835e11e2012-09-06 08:00:13 +0200591 hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
592 switch (hubCharacteristics & HUB_CHAR_LPSM) {
Marek Vasut6da5af32012-02-13 18:58:17 +0000593 case 0x00:
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530594 debug("ganged power switching\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000595 break;
596 case 0x01:
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530597 debug("individual port power switching\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000598 break;
599 case 0x02:
600 case 0x03:
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530601 debug("unknown reserved power switching mode\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000602 break;
603 }
604
Lucas Stach835e11e2012-09-06 08:00:13 +0200605 if (hubCharacteristics & HUB_CHAR_COMPOUND)
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530606 debug("part of a compound device\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000607 else
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530608 debug("standalone hub\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000609
Lucas Stach835e11e2012-09-06 08:00:13 +0200610 switch (hubCharacteristics & HUB_CHAR_OCPM) {
Marek Vasut6da5af32012-02-13 18:58:17 +0000611 case 0x00:
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530612 debug("global over-current protection\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000613 break;
614 case 0x08:
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530615 debug("individual port over-current protection\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000616 break;
617 case 0x10:
618 case 0x18:
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530619 debug("no over-current protection\n");
Marek Vasut6da5af32012-02-13 18:58:17 +0000620 break;
621 }
622
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530623 debug("power on to power good time: %dms\n",
624 descriptor->bPwrOn2PwrGood * 2);
625 debug("hub controller current requirement: %dmA\n",
626 descriptor->bHubContrCurrent);
Marek Vasut6da5af32012-02-13 18:58:17 +0000627
628 for (i = 0; i < dev->maxchild; i++)
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530629 debug("port %d is%s removable\n", i + 1,
630 hub->desc.DeviceRemovable[(i + 1) / 8] & \
631 (1 << ((i + 1) % 8)) ? " not" : "");
Marek Vasut6da5af32012-02-13 18:58:17 +0000632
633 if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530634 debug("usb_hub_configure: failed to get Status - " \
635 "too long: %d\n", descriptor->bLength);
Simon Glassf977c182015-03-25 12:22:09 -0600636 return -EFBIG;
Marek Vasut6da5af32012-02-13 18:58:17 +0000637 }
638
Simon Glassf977c182015-03-25 12:22:09 -0600639 ret = usb_get_hub_status(dev, buffer);
640 if (ret < 0) {
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530641 debug("usb_hub_configure: failed to get Status %lX\n",
642 dev->status);
Simon Glassf977c182015-03-25 12:22:09 -0600643 return ret;
Marek Vasut6da5af32012-02-13 18:58:17 +0000644 }
645
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530646#ifdef DEBUG
Marek Vasut6da5af32012-02-13 18:58:17 +0000647 hubsts = (struct usb_hub_status *)buffer;
648#endif
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530649
650 debug("get_hub_status returned status %X, change %X\n",
651 le16_to_cpu(hubsts->wHubStatus),
652 le16_to_cpu(hubsts->wHubChange));
653 debug("local power source is %s\n",
654 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
655 "lost (inactive)" : "good");
656 debug("%sover-current condition exists\n",
657 (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
658 "" : "no ");
Marek Vasut6da5af32012-02-13 18:58:17 +0000659 usb_hub_power_on(hub);
660
Dan Murphy2666bb12013-08-01 14:06:01 -0500661 /*
662 * Reset any devices that may be in a bad state when applying
663 * the power. This is a __weak function. Resetting of the devices
664 * should occur in the board file of the device.
665 */
666 for (i = 0; i < dev->maxchild; i++)
667 usb_hub_reset_devices(i + 1);
668
Stefan Roeseed4823a2016-03-15 13:59:15 +0100669 /*
670 * Only add the connected USB devices, including potential hubs,
671 * to a scanning list. This list will get scanned and devices that
672 * are detected (either via port connected or via port timeout)
673 * will get removed from this list. Scanning of the devices on this
674 * list will continue until all devices are removed.
675 */
Marek Vasut6da5af32012-02-13 18:58:17 +0000676 for (i = 0; i < dev->maxchild; i++) {
Stefan Roeseed4823a2016-03-15 13:59:15 +0100677 struct usb_device_scan *usb_scan;
Marek Vasut6da5af32012-02-13 18:58:17 +0000678
Stefan Roeseed4823a2016-03-15 13:59:15 +0100679 usb_scan = calloc(1, sizeof(*usb_scan));
680 if (!usb_scan) {
681 printf("Can't allocate memory for USB device!\n");
682 return -ENOMEM;
Marek Vasut6da5af32012-02-13 18:58:17 +0000683 }
Stefan Roeseed4823a2016-03-15 13:59:15 +0100684 usb_scan->dev = dev;
685 usb_scan->hub = hub;
686 usb_scan->port = i;
687 list_add_tail(&usb_scan->list, &usb_scan_list);
688 }
Marek Vasut6da5af32012-02-13 18:58:17 +0000689
Stefan Roeseed4823a2016-03-15 13:59:15 +0100690 /*
691 * And now call the scanning code which loops over the generated list
692 */
693 ret = usb_device_list_scan();
Marek Vasut6da5af32012-02-13 18:58:17 +0000694
Stefan Roeseed4823a2016-03-15 13:59:15 +0100695 return ret;
Marek Vasut6da5af32012-02-13 18:58:17 +0000696}
697
Simon Glassf977c182015-03-25 12:22:09 -0600698static int usb_hub_check(struct usb_device *dev, int ifnum)
Marek Vasut6da5af32012-02-13 18:58:17 +0000699{
700 struct usb_interface *iface;
Simon Glassf977c182015-03-25 12:22:09 -0600701 struct usb_endpoint_descriptor *ep = NULL;
Marek Vasut6da5af32012-02-13 18:58:17 +0000702
703 iface = &dev->config.if_desc[ifnum];
704 /* Is it a hub? */
705 if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
Simon Glassf977c182015-03-25 12:22:09 -0600706 goto err;
Marek Vasut6da5af32012-02-13 18:58:17 +0000707 /* Some hubs have a subclass of 1, which AFAICT according to the */
708 /* specs is not defined, but it works */
709 if ((iface->desc.bInterfaceSubClass != 0) &&
710 (iface->desc.bInterfaceSubClass != 1))
Simon Glassf977c182015-03-25 12:22:09 -0600711 goto err;
Marek Vasut6da5af32012-02-13 18:58:17 +0000712 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
713 if (iface->desc.bNumEndpoints != 1)
Simon Glassf977c182015-03-25 12:22:09 -0600714 goto err;
Marek Vasut6da5af32012-02-13 18:58:17 +0000715 ep = &iface->ep_desc[0];
716 /* Output endpoint? Curiousier and curiousier.. */
717 if (!(ep->bEndpointAddress & USB_DIR_IN))
Simon Glassf977c182015-03-25 12:22:09 -0600718 goto err;
Marek Vasut6da5af32012-02-13 18:58:17 +0000719 /* If it's not an interrupt endpoint, we'd better punt! */
720 if ((ep->bmAttributes & 3) != 3)
Simon Glassf977c182015-03-25 12:22:09 -0600721 goto err;
Marek Vasut6da5af32012-02-13 18:58:17 +0000722 /* We found a hub */
Vivek Gautamf94c95d2013-04-12 16:34:33 +0530723 debug("USB hub found\n");
Simon Glassf977c182015-03-25 12:22:09 -0600724 return 0;
725
726err:
727 debug("USB hub not found: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n",
728 iface->desc.bInterfaceClass, iface->desc.bInterfaceSubClass,
729 iface->desc.bNumEndpoints);
730 if (ep) {
731 debug(" bEndpointAddress=%#x, bmAttributes=%d",
732 ep->bEndpointAddress, ep->bmAttributes);
733 }
734
735 return -ENOENT;
736}
737
738int usb_hub_probe(struct usb_device *dev, int ifnum)
739{
740 int ret;
741
742 ret = usb_hub_check(dev, ifnum);
743 if (ret)
744 return 0;
Marek Vasut6da5af32012-02-13 18:58:17 +0000745 ret = usb_hub_configure(dev);
746 return ret;
747}
Simon Glass6ab43b62015-03-25 12:22:10 -0600748
749#ifdef CONFIG_DM_USB
750int usb_hub_scan(struct udevice *hub)
751{
Simon Glassde44acf2015-09-28 23:32:01 -0600752 struct usb_device *udev = dev_get_parent_priv(hub);
Simon Glass6ab43b62015-03-25 12:22:10 -0600753
754 return usb_hub_configure(udev);
755}
756
Simon Glass6ab43b62015-03-25 12:22:10 -0600757static int usb_hub_post_probe(struct udevice *dev)
758{
759 debug("%s\n", __func__);
760 return usb_hub_scan(dev);
761}
762
763static const struct udevice_id usb_hub_ids[] = {
764 { .compatible = "usb-hub" },
765 { }
766};
767
768U_BOOT_DRIVER(usb_generic_hub) = {
769 .name = "usb_hub",
770 .id = UCLASS_USB_HUB,
771 .of_match = usb_hub_ids,
772 .flags = DM_FLAG_ALLOC_PRIV_DMA,
773};
774
775UCLASS_DRIVER(usb_hub) = {
776 .id = UCLASS_USB_HUB,
777 .name = "usb_hub",
Simon Glass18230342016-07-05 17:10:10 -0600778 .post_bind = dm_scan_fdt_dev,
Simon Glass6ab43b62015-03-25 12:22:10 -0600779 .post_probe = usb_hub_post_probe,
780 .child_pre_probe = usb_child_pre_probe,
781 .per_child_auto_alloc_size = sizeof(struct usb_device),
782 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
783};
784
785static const struct usb_device_id hub_id_table[] = {
786 {
787 .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
788 .bDeviceClass = USB_CLASS_HUB
789 },
790 { } /* Terminating entry */
791};
792
Simon Glass46952fb2015-07-06 16:47:51 -0600793U_BOOT_USB_DEVICE(usb_generic_hub, hub_id_table);
Simon Glass6ab43b62015-03-25 12:22:10 -0600794
795#endif