]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/usb_hub.c
spl: mmc: raw: Try to load u-boot if Linux image is not found
[people/ms/u-boot.git] / common / usb_hub.c
CommitLineData
23faf2bc 1/*
23faf2bc
MV
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 *
1a459660 17 * SPDX-License-Identifier: GPL-2.0+
23faf2bc
MV
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>
054fe48e 27#include <dm.h>
79b58887 28#include <errno.h>
cf92e05c 29#include <memalign.h>
23faf2bc 30#include <asm/processor.h>
93ad908c 31#include <asm/unaligned.h>
23faf2bc 32#include <linux/ctype.h>
c998da0d 33#include <linux/list.h>
23faf2bc 34#include <asm/byteorder.h>
3884c98c
SG
35#ifdef CONFIG_SANDBOX
36#include <asm/state.h>
37#endif
23faf2bc 38#include <asm/unaligned.h>
054fe48e
SG
39#include <dm/root.h>
40
41DECLARE_GLOBAL_DATA_PTR;
23faf2bc
MV
42
43#include <usb.h>
44#ifdef CONFIG_4xx
45#include <asm/4xx_pci.h>
46#endif
47
23faf2bc
MV
48#define USB_BUFSIZ 512
49
f7f60100
SR
50#define HUB_SHORT_RESET_TIME 20
51#define HUB_LONG_RESET_TIME 200
52
c998da0d
SR
53#define PORT_OVERCURRENT_MAX_SCAN_COUNT 3
54
55struct usb_device_scan {
56 struct usb_device *dev; /* USB hub device to scan */
57 struct usb_hub_device *hub; /* USB hub struct */
58 int port; /* USB port to scan */
59 struct list_head list;
60};
61
054fe48e 62/* TODO(sjg@chromium.org): Remove this when CONFIG_DM_USB is defined */
23faf2bc
MV
63static struct usb_hub_device hub_dev[USB_MAX_HUB];
64static int usb_hub_index;
c998da0d 65static LIST_HEAD(usb_scan_list);
23faf2bc 66
3615a996
DM
67__weak void usb_hub_reset_devices(int port)
68{
69 return;
70}
23faf2bc
MV
71
72static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
73{
74 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
75 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
76 USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
77}
78
79static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
80{
81 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
82 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
83 port, NULL, 0, USB_CNTL_TIMEOUT);
84}
85
86static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
87{
88 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
89 USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
90 port, NULL, 0, USB_CNTL_TIMEOUT);
91}
92
93static int usb_get_hub_status(struct usb_device *dev, void *data)
94{
95 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
96 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
97 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
98}
99
08f3bb0b 100int usb_get_port_status(struct usb_device *dev, int port, void *data)
23faf2bc
MV
101{
102 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
103 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
104 data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
105}
106
107
108static void usb_hub_power_on(struct usb_hub_device *hub)
109{
110 int i;
111 struct usb_device *dev;
a1a28c6e 112 unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
319418c0 113 const char *env;
23faf2bc
MV
114
115 dev = hub->pusb_dev;
0bf796f7 116
ceb4972a 117 debug("enabling power on all ports\n");
0bf796f7 118 for (i = 0; i < dev->maxchild; i++) {
23faf2bc 119 usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
ceb4972a 120 debug("port %d returns %lX\n", i + 1, dev->status);
23faf2bc 121 }
a1a28c6e 122
c998da0d
SR
123#ifdef CONFIG_SANDBOX
124 /*
125 * Don't set timeout / delay values here. This results
126 * in these values still being reset to 0.
127 */
128 if (state_get_skip_delays())
129 return;
130#endif
131
0d437bca
SW
132 /*
133 * Wait for power to become stable,
134 * plus spec-defined max time for device to connect
319418c0
TH
135 * but allow this time to be increased via env variable as some
136 * devices break the spec and require longer warm-up times
0d437bca 137 */
319418c0
TH
138 env = getenv("usb_pgood_delay");
139 if (env)
140 pgood_delay = max(pgood_delay,
141 (unsigned)simple_strtol(env, NULL, 0));
142 debug("pgood_delay=%dms\n", pgood_delay);
c998da0d
SR
143
144 /*
145 * Do a minimum delay of the larger value of 100ms or pgood_delay
146 * so that the power can stablize before the devices are queried
147 */
148 hub->query_delay = get_timer(0) + max(100, (int)pgood_delay);
149
150 /*
151 * Record the power-on timeout here. The max. delay (timeout)
152 * will be done based on this value in the USB port loop in
153 * usb_hub_configure() later.
154 */
155 hub->connect_timeout = hub->query_delay + 1000;
156 debug("devnum=%d poweron: query_delay=%d connect_timeout=%d\n",
157 dev->devnum, max(100, (int)pgood_delay),
158 max(100, (int)pgood_delay) + 1000);
23faf2bc
MV
159}
160
161void usb_hub_reset(void)
162{
163 usb_hub_index = 0;
c998da0d
SR
164
165 /* Zero out global hub_dev in case its re-used again */
166 memset(hub_dev, 0, sizeof(hub_dev));
23faf2bc
MV
167}
168
169static struct usb_hub_device *usb_hub_allocate(void)
170{
171 if (usb_hub_index < USB_MAX_HUB)
172 return &hub_dev[usb_hub_index++];
173
174 printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
175 return NULL;
176}
177
178#define MAX_TRIES 5
179
180static inline char *portspeed(int portstatus)
181{
55f4b575
VG
182 char *speed_str;
183
184 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
185 case USB_PORT_STAT_SUPER_SPEED:
186 speed_str = "5 Gb/s";
187 break;
188 case USB_PORT_STAT_HIGH_SPEED:
189 speed_str = "480 Mb/s";
190 break;
191 case USB_PORT_STAT_LOW_SPEED:
192 speed_str = "1.5 Mb/s";
193 break;
194 default:
195 speed_str = "12 Mb/s";
196 break;
197 }
198
199 return speed_str;
23faf2bc
MV
200}
201
862e75c0 202int legacy_hub_port_reset(struct usb_device *dev, int port,
23faf2bc
MV
203 unsigned short *portstat)
204{
ad84a42f 205 int err, tries;
f5766139 206 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
23faf2bc 207 unsigned short portstatus, portchange;
f7f60100 208 int delay = HUB_SHORT_RESET_TIME; /* start with short reset delay */
23faf2bc 209
054fe48e
SG
210#ifdef CONFIG_DM_USB
211 debug("%s: resetting '%s' port %d...\n", __func__, dev->dev->name,
212 port + 1);
213#else
214 debug("%s: resetting port %d...\n", __func__, port + 1);
215#endif
23faf2bc 216 for (tries = 0; tries < MAX_TRIES; tries++) {
ad84a42f
HG
217 err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
218 if (err < 0)
219 return err;
23faf2bc 220
f7f60100 221 mdelay(delay);
23faf2bc 222
f5766139 223 if (usb_get_port_status(dev, port + 1, portsts) < 0) {
ceb4972a
VG
224 debug("get_port_status failed status %lX\n",
225 dev->status);
23faf2bc
MV
226 return -1;
227 }
f5766139
PS
228 portstatus = le16_to_cpu(portsts->wPortStatus);
229 portchange = le16_to_cpu(portsts->wPortChange);
23faf2bc 230
ceb4972a
VG
231 debug("portstatus %x, change %x, %s\n", portstatus, portchange,
232 portspeed(portstatus));
23faf2bc 233
ceb4972a
VG
234 debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
235 " USB_PORT_STAT_ENABLE %d\n",
236 (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
237 (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
238 (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
23faf2bc 239
74c0d756
SW
240 /*
241 * Perhaps we should check for the following here:
242 * - C_CONNECTION hasn't been set.
243 * - CONNECTION is still set.
244 *
245 * Doing so would ensure that the device is still connected
246 * to the bus, and hasn't been unplugged or replaced while the
247 * USB bus reset was going on.
248 *
249 * However, if we do that, then (at least) a San Disk Ultra
250 * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
251 * Tegra Jetson TK1 board. For some reason, the device appears
252 * to briefly drop off the bus when this second bus reset is
253 * executed, yet if we retry this loop, it'll eventually come
254 * back after another reset or two.
255 */
23faf2bc
MV
256
257 if (portstatus & USB_PORT_STAT_ENABLE)
258 break;
259
f7f60100
SR
260 /* Switch to long reset delay for the next round */
261 delay = HUB_LONG_RESET_TIME;
23faf2bc
MV
262 }
263
264 if (tries == MAX_TRIES) {
ceb4972a
VG
265 debug("Cannot enable port %i after %i retries, " \
266 "disabling port.\n", port + 1, MAX_TRIES);
267 debug("Maybe the USB cable is bad?\n");
23faf2bc
MV
268 return -1;
269 }
270
271 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
272 *portstat = portstatus;
273 return 0;
274}
275
054fe48e
SG
276#ifdef CONFIG_DM_USB
277int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat)
278{
bcbe3d15 279 struct usb_device *udev = dev_get_parent_priv(dev);
054fe48e
SG
280
281 return legacy_hub_port_reset(udev, port, portstat);
282}
283#endif
23faf2bc 284
79b58887 285int usb_hub_port_connect_change(struct usb_device *dev, int port)
23faf2bc 286{
f5766139 287 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
23faf2bc 288 unsigned short portstatus;
79b58887 289 int ret, speed;
23faf2bc
MV
290
291 /* Check status */
79b58887
SG
292 ret = usb_get_port_status(dev, port + 1, portsts);
293 if (ret < 0) {
ceb4972a 294 debug("get_port_status failed\n");
79b58887 295 return ret;
23faf2bc
MV
296 }
297
f5766139 298 portstatus = le16_to_cpu(portsts->wPortStatus);
ceb4972a
VG
299 debug("portstatus %x, change %x, %s\n",
300 portstatus,
301 le16_to_cpu(portsts->wPortChange),
302 portspeed(portstatus));
23faf2bc
MV
303
304 /* Clear the connection change status */
305 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
306
307 /* Disconnect any existing devices under this port */
308 if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
054fe48e
SG
309 (!(portstatus & USB_PORT_STAT_ENABLE))) ||
310 usb_device_has_child_on_port(dev, port)) {
ceb4972a 311 debug("usb_disconnect(&hub->children[port]);\n");
23faf2bc
MV
312 /* Return now if nothing is connected */
313 if (!(portstatus & USB_PORT_STAT_CONNECTION))
79b58887 314 return -ENOTCONN;
23faf2bc 315 }
23faf2bc
MV
316
317 /* Reset the port */
862e75c0 318 ret = legacy_hub_port_reset(dev, port, &portstatus);
79b58887 319 if (ret < 0) {
45b9ea1d
HG
320 if (ret != -ENXIO)
321 printf("cannot reset port %i!?\n", port + 1);
79b58887 322 return ret;
23faf2bc
MV
323 }
324
55f4b575
VG
325 switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
326 case USB_PORT_STAT_SUPER_SPEED:
79b58887 327 speed = USB_SPEED_SUPER;
55f4b575
VG
328 break;
329 case USB_PORT_STAT_HIGH_SPEED:
79b58887 330 speed = USB_SPEED_HIGH;
55f4b575
VG
331 break;
332 case USB_PORT_STAT_LOW_SPEED:
79b58887 333 speed = USB_SPEED_LOW;
55f4b575
VG
334 break;
335 default:
79b58887 336 speed = USB_SPEED_FULL;
55f4b575
VG
337 break;
338 }
23faf2bc 339
054fe48e
SG
340#ifdef CONFIG_DM_USB
341 struct udevice *child;
342
343 ret = usb_scan_device(dev->dev, port + 1, speed, &child);
344#else
345 struct usb_device *usb;
346
79b58887
SG
347 ret = usb_alloc_new_device(dev->controller, &usb);
348 if (ret) {
349 printf("cannot create new device: ret=%d", ret);
350 return ret;
351 }
352
23faf2bc 353 dev->children[port] = usb;
79b58887 354 usb->speed = speed;
23faf2bc
MV
355 usb->parent = dev;
356 usb->portnr = port + 1;
357 /* Run it through the hoops (find a driver, etc) */
79b58887
SG
358 ret = usb_new_device(usb);
359 if (ret < 0) {
23faf2bc 360 /* Woops, disable the port */
79b58887 361 usb_free_device(dev->controller);
359439d2 362 dev->children[port] = NULL;
054fe48e
SG
363 }
364#endif
365 if (ret < 0) {
ceb4972a 366 debug("hub: disabling port %d\n", port + 1);
23faf2bc
MV
367 usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
368 }
79b58887
SG
369
370 return ret;
23faf2bc
MV
371}
372
c998da0d
SR
373static int usb_scan_port(struct usb_device_scan *usb_scan)
374{
375 ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
376 unsigned short portstatus;
377 unsigned short portchange;
378 struct usb_device *dev;
379 struct usb_hub_device *hub;
380 int ret = 0;
381 int i;
382
383 dev = usb_scan->dev;
384 hub = usb_scan->hub;
385 i = usb_scan->port;
386
387 /*
388 * Don't talk to the device before the query delay is expired.
389 * This is needed for voltages to stabalize.
390 */
391 if (get_timer(0) < hub->query_delay)
392 return 0;
393
394 ret = usb_get_port_status(dev, i + 1, portsts);
395 if (ret < 0) {
396 debug("get_port_status failed\n");
397 if (get_timer(0) >= hub->connect_timeout) {
398 debug("devnum=%d port=%d: timeout\n",
399 dev->devnum, i + 1);
400 /* Remove this device from scanning list */
401 list_del(&usb_scan->list);
402 free(usb_scan);
403 return 0;
404 }
405 }
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 */
478 printf("Port %d over-current occured %d times\n", i + 1,
479 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}
23faf2bc
MV
535
536static int usb_hub_configure(struct usb_device *dev)
537{
eaf3e613 538 int i, length;
f5766139
PS
539 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
540 unsigned char *bitmap;
93ad908c 541 short hubCharacteristics;
23faf2bc
MV
542 struct usb_hub_descriptor *descriptor;
543 struct usb_hub_device *hub;
ceb4972a 544 __maybe_unused struct usb_hub_status *hubsts;
361ad6af 545 int ret;
23faf2bc
MV
546
547 /* "allocate" Hub device */
548 hub = usb_hub_allocate();
549 if (hub == NULL)
361ad6af 550 return -ENOMEM;
23faf2bc
MV
551 hub->pusb_dev = dev;
552 /* Get the the hub descriptor */
361ad6af
SG
553 ret = usb_get_hub_descriptor(dev, buffer, 4);
554 if (ret < 0) {
ceb4972a
VG
555 debug("usb_hub_configure: failed to get hub " \
556 "descriptor, giving up %lX\n", dev->status);
361ad6af 557 return ret;
23faf2bc
MV
558 }
559 descriptor = (struct usb_hub_descriptor *)buffer;
560
b4141195
MY
561 length = min_t(int, descriptor->bLength,
562 sizeof(struct usb_hub_descriptor));
23faf2bc 563
361ad6af
SG
564 ret = usb_get_hub_descriptor(dev, buffer, length);
565 if (ret < 0) {
ceb4972a
VG
566 debug("usb_hub_configure: failed to get hub " \
567 "descriptor 2nd giving up %lX\n", dev->status);
361ad6af 568 return ret;
23faf2bc 569 }
eaf3e613 570 memcpy((unsigned char *)&hub->desc, buffer, length);
23faf2bc 571 /* adjust 16bit values */
93ad908c
LS
572 put_unaligned(le16_to_cpu(get_unaligned(
573 &descriptor->wHubCharacteristics)),
574 &hub->desc.wHubCharacteristics);
23faf2bc
MV
575 /* 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;
ceb4972a 589 debug("%d ports detected\n", dev->maxchild);
23faf2bc 590
93ad908c
LS
591 hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
592 switch (hubCharacteristics & HUB_CHAR_LPSM) {
23faf2bc 593 case 0x00:
ceb4972a 594 debug("ganged power switching\n");
23faf2bc
MV
595 break;
596 case 0x01:
ceb4972a 597 debug("individual port power switching\n");
23faf2bc
MV
598 break;
599 case 0x02:
600 case 0x03:
ceb4972a 601 debug("unknown reserved power switching mode\n");
23faf2bc
MV
602 break;
603 }
604
93ad908c 605 if (hubCharacteristics & HUB_CHAR_COMPOUND)
ceb4972a 606 debug("part of a compound device\n");
23faf2bc 607 else
ceb4972a 608 debug("standalone hub\n");
23faf2bc 609
93ad908c 610 switch (hubCharacteristics & HUB_CHAR_OCPM) {
23faf2bc 611 case 0x00:
ceb4972a 612 debug("global over-current protection\n");
23faf2bc
MV
613 break;
614 case 0x08:
ceb4972a 615 debug("individual port over-current protection\n");
23faf2bc
MV
616 break;
617 case 0x10:
618 case 0x18:
ceb4972a 619 debug("no over-current protection\n");
23faf2bc
MV
620 break;
621 }
622
ceb4972a
VG
623 debug("power on to power good time: %dms\n",
624 descriptor->bPwrOn2PwrGood * 2);
625 debug("hub controller current requirement: %dmA\n",
626 descriptor->bHubContrCurrent);
23faf2bc
MV
627
628 for (i = 0; i < dev->maxchild; i++)
ceb4972a
VG
629 debug("port %d is%s removable\n", i + 1,
630 hub->desc.DeviceRemovable[(i + 1) / 8] & \
631 (1 << ((i + 1) % 8)) ? " not" : "");
23faf2bc
MV
632
633 if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
ceb4972a
VG
634 debug("usb_hub_configure: failed to get Status - " \
635 "too long: %d\n", descriptor->bLength);
361ad6af 636 return -EFBIG;
23faf2bc
MV
637 }
638
361ad6af
SG
639 ret = usb_get_hub_status(dev, buffer);
640 if (ret < 0) {
ceb4972a
VG
641 debug("usb_hub_configure: failed to get Status %lX\n",
642 dev->status);
361ad6af 643 return ret;
23faf2bc
MV
644 }
645
ceb4972a 646#ifdef DEBUG
23faf2bc
MV
647 hubsts = (struct usb_hub_status *)buffer;
648#endif
ceb4972a
VG
649
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 ");
23faf2bc
MV
659 usb_hub_power_on(hub);
660
3615a996
DM
661 /*
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
c998da0d
SR
669 /*
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 */
23faf2bc 676 for (i = 0; i < dev->maxchild; i++) {
c998da0d 677 struct usb_device_scan *usb_scan;
b6d7852c 678
c998da0d
SR
679 usb_scan = calloc(1, sizeof(*usb_scan));
680 if (!usb_scan) {
681 printf("Can't allocate memory for USB device!\n");
682 return -ENOMEM;
23faf2bc 683 }
c998da0d
SR
684 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 }
23faf2bc 689
c998da0d
SR
690 /*
691 * And now call the scanning code which loops over the generated list
692 */
693 ret = usb_device_list_scan();
23faf2bc 694
c998da0d 695 return ret;
23faf2bc
MV
696}
697
361ad6af 698static int usb_hub_check(struct usb_device *dev, int ifnum)
23faf2bc
MV
699{
700 struct usb_interface *iface;
361ad6af 701 struct usb_endpoint_descriptor *ep = NULL;
23faf2bc
MV
702
703 iface = &dev->config.if_desc[ifnum];
704 /* Is it a hub? */
705 if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
361ad6af 706 goto err;
23faf2bc
MV
707 /* 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))
361ad6af 711 goto err;
23faf2bc
MV
712 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
713 if (iface->desc.bNumEndpoints != 1)
361ad6af 714 goto err;
23faf2bc
MV
715 ep = &iface->ep_desc[0];
716 /* Output endpoint? Curiousier and curiousier.. */
717 if (!(ep->bEndpointAddress & USB_DIR_IN))
361ad6af 718 goto err;
23faf2bc
MV
719 /* If it's not an interrupt endpoint, we'd better punt! */
720 if ((ep->bmAttributes & 3) != 3)
361ad6af 721 goto err;
23faf2bc 722 /* We found a hub */
ceb4972a 723 debug("USB hub found\n");
361ad6af
SG
724 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;
23faf2bc
MV
745 ret = usb_hub_configure(dev);
746 return ret;
747}
054fe48e
SG
748
749#ifdef CONFIG_DM_USB
750int usb_hub_scan(struct udevice *hub)
751{
bcbe3d15 752 struct usb_device *udev = dev_get_parent_priv(hub);
054fe48e
SG
753
754 return usb_hub_configure(udev);
755}
756
757static int usb_hub_post_bind(struct udevice *dev)
758{
759 /* Scan the bus for devices */
760 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
761}
762
763static int usb_hub_post_probe(struct udevice *dev)
764{
765 debug("%s\n", __func__);
766 return usb_hub_scan(dev);
767}
768
769static const struct udevice_id usb_hub_ids[] = {
770 { .compatible = "usb-hub" },
771 { }
772};
773
774U_BOOT_DRIVER(usb_generic_hub) = {
775 .name = "usb_hub",
776 .id = UCLASS_USB_HUB,
777 .of_match = usb_hub_ids,
778 .flags = DM_FLAG_ALLOC_PRIV_DMA,
779};
780
781UCLASS_DRIVER(usb_hub) = {
782 .id = UCLASS_USB_HUB,
783 .name = "usb_hub",
784 .post_bind = usb_hub_post_bind,
785 .post_probe = usb_hub_post_probe,
786 .child_pre_probe = usb_child_pre_probe,
787 .per_child_auto_alloc_size = sizeof(struct usb_device),
788 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
789};
790
791static const struct usb_device_id hub_id_table[] = {
792 {
793 .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
794 .bDeviceClass = USB_CLASS_HUB
795 },
796 { } /* Terminating entry */
797};
798
abb59cff 799U_BOOT_USB_DEVICE(usb_generic_hub, hub_id_table);
054fe48e
SG
800
801#endif