]>
Commit | Line | Data |
---|---|---|
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 | |
40 | DECLARE_GLOBAL_DATA_PTR; | |
23faf2bc MV |
41 | |
42 | #include <usb.h> | |
23faf2bc | 43 | |
23faf2bc MV |
44 | #define USB_BUFSIZ 512 |
45 | ||
f7f60100 SR |
46 | #define HUB_SHORT_RESET_TIME 20 |
47 | #define HUB_LONG_RESET_TIME 200 | |
48 | ||
c998da0d SR |
49 | #define PORT_OVERCURRENT_MAX_SCAN_COUNT 3 |
50 | ||
51 | struct usb_device_scan { | |
52 | struct usb_device *dev; /* USB hub device to scan */ | |
53 | struct usb_hub_device *hub; /* USB hub struct */ | |
54 | int port; /* USB port to scan */ | |
55 | struct list_head list; | |
56 | }; | |
57 | ||
054fe48e | 58 | /* TODO(sjg@chromium.org): Remove this when CONFIG_DM_USB is defined */ |
23faf2bc MV |
59 | static struct usb_hub_device hub_dev[USB_MAX_HUB]; |
60 | static int usb_hub_index; | |
c998da0d | 61 | static LIST_HEAD(usb_scan_list); |
23faf2bc | 62 | |
3615a996 DM |
63 | __weak void usb_hub_reset_devices(int port) |
64 | { | |
65 | return; | |
66 | } | |
23faf2bc | 67 | |
f3421196 BM |
68 | static inline bool usb_hub_is_superspeed(struct usb_device *hdev) |
69 | { | |
70 | return hdev->descriptor.bDeviceProtocol == 3; | |
71 | } | |
72 | ||
23faf2bc MV |
73 | static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size) |
74 | { | |
f3421196 BM |
75 | unsigned short dtype = USB_DT_HUB; |
76 | ||
77 | if (usb_hub_is_superspeed(dev)) | |
78 | dtype = USB_DT_SS_HUB; | |
79 | ||
23faf2bc MV |
80 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
81 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, | |
f3421196 | 82 | dtype << 8, 0, data, size, USB_CNTL_TIMEOUT); |
23faf2bc MV |
83 | } |
84 | ||
85 | static int usb_clear_port_feature(struct usb_device *dev, int port, int feature) | |
86 | { | |
87 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | |
88 | USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, | |
89 | port, NULL, 0, USB_CNTL_TIMEOUT); | |
90 | } | |
91 | ||
92 | static int usb_set_port_feature(struct usb_device *dev, int port, int feature) | |
93 | { | |
94 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | |
95 | USB_REQ_SET_FEATURE, USB_RT_PORT, feature, | |
96 | port, NULL, 0, USB_CNTL_TIMEOUT); | |
97 | } | |
98 | ||
99 | static int usb_get_hub_status(struct usb_device *dev, void *data) | |
100 | { | |
101 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
102 | USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, | |
103 | data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT); | |
104 | } | |
105 | ||
08f3bb0b | 106 | int usb_get_port_status(struct usb_device *dev, int port, void *data) |
23faf2bc MV |
107 | { |
108 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
109 | USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port, | |
53771a49 | 110 | data, sizeof(struct usb_port_status), USB_CNTL_TIMEOUT); |
23faf2bc MV |
111 | } |
112 | ||
113 | ||
114 | static void usb_hub_power_on(struct usb_hub_device *hub) | |
115 | { | |
116 | int i; | |
117 | struct usb_device *dev; | |
a1a28c6e | 118 | unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2; |
319418c0 | 119 | const char *env; |
23faf2bc MV |
120 | |
121 | dev = hub->pusb_dev; | |
0bf796f7 | 122 | |
ceb4972a | 123 | debug("enabling power on all ports\n"); |
0bf796f7 | 124 | for (i = 0; i < dev->maxchild; i++) { |
23faf2bc | 125 | usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER); |
ceb4972a | 126 | debug("port %d returns %lX\n", i + 1, dev->status); |
23faf2bc | 127 | } |
a1a28c6e | 128 | |
c998da0d SR |
129 | #ifdef CONFIG_SANDBOX |
130 | /* | |
131 | * Don't set timeout / delay values here. This results | |
132 | * in these values still being reset to 0. | |
133 | */ | |
134 | if (state_get_skip_delays()) | |
135 | return; | |
136 | #endif | |
137 | ||
0d437bca SW |
138 | /* |
139 | * Wait for power to become stable, | |
140 | * plus spec-defined max time for device to connect | |
319418c0 TH |
141 | * but allow this time to be increased via env variable as some |
142 | * devices break the spec and require longer warm-up times | |
0d437bca | 143 | */ |
319418c0 TH |
144 | env = getenv("usb_pgood_delay"); |
145 | if (env) | |
146 | pgood_delay = max(pgood_delay, | |
147 | (unsigned)simple_strtol(env, NULL, 0)); | |
148 | debug("pgood_delay=%dms\n", pgood_delay); | |
c998da0d SR |
149 | |
150 | /* | |
151 | * Do a minimum delay of the larger value of 100ms or pgood_delay | |
152 | * so that the power can stablize before the devices are queried | |
153 | */ | |
154 | hub->query_delay = get_timer(0) + max(100, (int)pgood_delay); | |
155 | ||
156 | /* | |
157 | * Record the power-on timeout here. The max. delay (timeout) | |
158 | * will be done based on this value in the USB port loop in | |
159 | * usb_hub_configure() later. | |
160 | */ | |
161 | hub->connect_timeout = hub->query_delay + 1000; | |
162 | debug("devnum=%d poweron: query_delay=%d connect_timeout=%d\n", | |
163 | dev->devnum, max(100, (int)pgood_delay), | |
164 | max(100, (int)pgood_delay) + 1000); | |
23faf2bc MV |
165 | } |
166 | ||
167 | void usb_hub_reset(void) | |
168 | { | |
169 | usb_hub_index = 0; | |
c998da0d SR |
170 | |
171 | /* Zero out global hub_dev in case its re-used again */ | |
172 | memset(hub_dev, 0, sizeof(hub_dev)); | |
23faf2bc MV |
173 | } |
174 | ||
175 | static struct usb_hub_device *usb_hub_allocate(void) | |
176 | { | |
177 | if (usb_hub_index < USB_MAX_HUB) | |
178 | return &hub_dev[usb_hub_index++]; | |
179 | ||
180 | printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB); | |
181 | return NULL; | |
182 | } | |
183 | ||
184 | #define MAX_TRIES 5 | |
185 | ||
186 | static inline char *portspeed(int portstatus) | |
187 | { | |
55f4b575 VG |
188 | char *speed_str; |
189 | ||
190 | switch (portstatus & USB_PORT_STAT_SPEED_MASK) { | |
191 | case USB_PORT_STAT_SUPER_SPEED: | |
192 | speed_str = "5 Gb/s"; | |
193 | break; | |
194 | case USB_PORT_STAT_HIGH_SPEED: | |
195 | speed_str = "480 Mb/s"; | |
196 | break; | |
197 | case USB_PORT_STAT_LOW_SPEED: | |
198 | speed_str = "1.5 Mb/s"; | |
199 | break; | |
200 | default: | |
201 | speed_str = "12 Mb/s"; | |
202 | break; | |
203 | } | |
204 | ||
205 | return speed_str; | |
23faf2bc MV |
206 | } |
207 | ||
862e75c0 | 208 | int legacy_hub_port_reset(struct usb_device *dev, int port, |
23faf2bc MV |
209 | unsigned short *portstat) |
210 | { | |
ad84a42f | 211 | int err, tries; |
f5766139 | 212 | ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); |
23faf2bc | 213 | unsigned short portstatus, portchange; |
f7f60100 | 214 | int delay = HUB_SHORT_RESET_TIME; /* start with short reset delay */ |
23faf2bc | 215 | |
054fe48e SG |
216 | #ifdef CONFIG_DM_USB |
217 | debug("%s: resetting '%s' port %d...\n", __func__, dev->dev->name, | |
218 | port + 1); | |
219 | #else | |
220 | debug("%s: resetting port %d...\n", __func__, port + 1); | |
221 | #endif | |
23faf2bc | 222 | for (tries = 0; tries < MAX_TRIES; tries++) { |
ad84a42f HG |
223 | err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET); |
224 | if (err < 0) | |
225 | return err; | |
23faf2bc | 226 | |
f7f60100 | 227 | mdelay(delay); |
23faf2bc | 228 | |
f5766139 | 229 | if (usb_get_port_status(dev, port + 1, portsts) < 0) { |
ceb4972a VG |
230 | debug("get_port_status failed status %lX\n", |
231 | dev->status); | |
23faf2bc MV |
232 | return -1; |
233 | } | |
f5766139 PS |
234 | portstatus = le16_to_cpu(portsts->wPortStatus); |
235 | portchange = le16_to_cpu(portsts->wPortChange); | |
23faf2bc | 236 | |
ceb4972a VG |
237 | debug("portstatus %x, change %x, %s\n", portstatus, portchange, |
238 | portspeed(portstatus)); | |
23faf2bc | 239 | |
ceb4972a VG |
240 | debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \ |
241 | " USB_PORT_STAT_ENABLE %d\n", | |
242 | (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0, | |
243 | (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0, | |
244 | (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0); | |
23faf2bc | 245 | |
74c0d756 SW |
246 | /* |
247 | * Perhaps we should check for the following here: | |
248 | * - C_CONNECTION hasn't been set. | |
249 | * - CONNECTION is still set. | |
250 | * | |
251 | * Doing so would ensure that the device is still connected | |
252 | * to the bus, and hasn't been unplugged or replaced while the | |
253 | * USB bus reset was going on. | |
254 | * | |
255 | * However, if we do that, then (at least) a San Disk Ultra | |
256 | * USB 3.0 16GB device fails to reset on (at least) an NVIDIA | |
257 | * Tegra Jetson TK1 board. For some reason, the device appears | |
258 | * to briefly drop off the bus when this second bus reset is | |
259 | * executed, yet if we retry this loop, it'll eventually come | |
260 | * back after another reset or two. | |
261 | */ | |
23faf2bc MV |
262 | |
263 | if (portstatus & USB_PORT_STAT_ENABLE) | |
264 | break; | |
265 | ||
f7f60100 SR |
266 | /* Switch to long reset delay for the next round */ |
267 | delay = HUB_LONG_RESET_TIME; | |
23faf2bc MV |
268 | } |
269 | ||
270 | if (tries == MAX_TRIES) { | |
ceb4972a VG |
271 | debug("Cannot enable port %i after %i retries, " \ |
272 | "disabling port.\n", port + 1, MAX_TRIES); | |
273 | debug("Maybe the USB cable is bad?\n"); | |
23faf2bc MV |
274 | return -1; |
275 | } | |
276 | ||
277 | usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET); | |
278 | *portstat = portstatus; | |
279 | return 0; | |
280 | } | |
281 | ||
054fe48e SG |
282 | #ifdef CONFIG_DM_USB |
283 | int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat) | |
284 | { | |
bcbe3d15 | 285 | struct usb_device *udev = dev_get_parent_priv(dev); |
054fe48e SG |
286 | |
287 | return legacy_hub_port_reset(udev, port, portstat); | |
288 | } | |
289 | #endif | |
23faf2bc | 290 | |
79b58887 | 291 | int usb_hub_port_connect_change(struct usb_device *dev, int port) |
23faf2bc | 292 | { |
f5766139 | 293 | ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); |
23faf2bc | 294 | unsigned short portstatus; |
79b58887 | 295 | int ret, speed; |
23faf2bc MV |
296 | |
297 | /* Check status */ | |
79b58887 SG |
298 | ret = usb_get_port_status(dev, port + 1, portsts); |
299 | if (ret < 0) { | |
ceb4972a | 300 | debug("get_port_status failed\n"); |
79b58887 | 301 | return ret; |
23faf2bc MV |
302 | } |
303 | ||
f5766139 | 304 | portstatus = le16_to_cpu(portsts->wPortStatus); |
ceb4972a VG |
305 | debug("portstatus %x, change %x, %s\n", |
306 | portstatus, | |
307 | le16_to_cpu(portsts->wPortChange), | |
308 | portspeed(portstatus)); | |
23faf2bc MV |
309 | |
310 | /* Clear the connection change status */ | |
311 | usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION); | |
312 | ||
313 | /* Disconnect any existing devices under this port */ | |
314 | if (((!(portstatus & USB_PORT_STAT_CONNECTION)) && | |
054fe48e SG |
315 | (!(portstatus & USB_PORT_STAT_ENABLE))) || |
316 | usb_device_has_child_on_port(dev, port)) { | |
ceb4972a | 317 | debug("usb_disconnect(&hub->children[port]);\n"); |
23faf2bc MV |
318 | /* Return now if nothing is connected */ |
319 | if (!(portstatus & USB_PORT_STAT_CONNECTION)) | |
79b58887 | 320 | return -ENOTCONN; |
23faf2bc | 321 | } |
23faf2bc MV |
322 | |
323 | /* Reset the port */ | |
862e75c0 | 324 | ret = legacy_hub_port_reset(dev, port, &portstatus); |
79b58887 | 325 | if (ret < 0) { |
45b9ea1d HG |
326 | if (ret != -ENXIO) |
327 | printf("cannot reset port %i!?\n", port + 1); | |
79b58887 | 328 | return ret; |
23faf2bc MV |
329 | } |
330 | ||
55f4b575 VG |
331 | switch (portstatus & USB_PORT_STAT_SPEED_MASK) { |
332 | case USB_PORT_STAT_SUPER_SPEED: | |
79b58887 | 333 | speed = USB_SPEED_SUPER; |
55f4b575 VG |
334 | break; |
335 | case USB_PORT_STAT_HIGH_SPEED: | |
79b58887 | 336 | speed = USB_SPEED_HIGH; |
55f4b575 VG |
337 | break; |
338 | case USB_PORT_STAT_LOW_SPEED: | |
79b58887 | 339 | speed = USB_SPEED_LOW; |
55f4b575 VG |
340 | break; |
341 | default: | |
79b58887 | 342 | speed = USB_SPEED_FULL; |
55f4b575 VG |
343 | break; |
344 | } | |
23faf2bc | 345 | |
054fe48e SG |
346 | #ifdef CONFIG_DM_USB |
347 | struct udevice *child; | |
348 | ||
349 | ret = usb_scan_device(dev->dev, port + 1, speed, &child); | |
350 | #else | |
351 | struct usb_device *usb; | |
352 | ||
79b58887 SG |
353 | ret = usb_alloc_new_device(dev->controller, &usb); |
354 | if (ret) { | |
355 | printf("cannot create new device: ret=%d", ret); | |
356 | return ret; | |
357 | } | |
358 | ||
23faf2bc | 359 | dev->children[port] = usb; |
79b58887 | 360 | usb->speed = speed; |
23faf2bc MV |
361 | usb->parent = dev; |
362 | usb->portnr = port + 1; | |
363 | /* Run it through the hoops (find a driver, etc) */ | |
79b58887 SG |
364 | ret = usb_new_device(usb); |
365 | if (ret < 0) { | |
23faf2bc | 366 | /* Woops, disable the port */ |
79b58887 | 367 | usb_free_device(dev->controller); |
359439d2 | 368 | dev->children[port] = NULL; |
054fe48e SG |
369 | } |
370 | #endif | |
371 | if (ret < 0) { | |
ceb4972a | 372 | debug("hub: disabling port %d\n", port + 1); |
23faf2bc MV |
373 | usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE); |
374 | } | |
79b58887 SG |
375 | |
376 | return ret; | |
23faf2bc MV |
377 | } |
378 | ||
c998da0d SR |
379 | static int usb_scan_port(struct usb_device_scan *usb_scan) |
380 | { | |
381 | ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); | |
382 | unsigned short portstatus; | |
383 | unsigned short portchange; | |
384 | struct usb_device *dev; | |
385 | struct usb_hub_device *hub; | |
386 | int ret = 0; | |
387 | int i; | |
388 | ||
389 | dev = usb_scan->dev; | |
390 | hub = usb_scan->hub; | |
391 | i = usb_scan->port; | |
392 | ||
393 | /* | |
394 | * Don't talk to the device before the query delay is expired. | |
395 | * This is needed for voltages to stabalize. | |
396 | */ | |
397 | if (get_timer(0) < hub->query_delay) | |
398 | return 0; | |
399 | ||
400 | ret = usb_get_port_status(dev, i + 1, portsts); | |
401 | if (ret < 0) { | |
402 | debug("get_port_status failed\n"); | |
403 | if (get_timer(0) >= hub->connect_timeout) { | |
404 | debug("devnum=%d port=%d: timeout\n", | |
405 | dev->devnum, i + 1); | |
406 | /* Remove this device from scanning list */ | |
407 | list_del(&usb_scan->list); | |
408 | free(usb_scan); | |
409 | return 0; | |
410 | } | |
d81db48d | 411 | return 0; |
c998da0d SR |
412 | } |
413 | ||
414 | portstatus = le16_to_cpu(portsts->wPortStatus); | |
415 | portchange = le16_to_cpu(portsts->wPortChange); | |
416 | debug("Port %d Status %X Change %X\n", i + 1, portstatus, portchange); | |
417 | ||
f7a9e5dd BM |
418 | /* |
419 | * No connection change happened, wait a bit more. | |
420 | * | |
421 | * For some situation, the hub reports no connection change but a | |
422 | * device is connected to the port (eg: CCS bit is set but CSC is not | |
423 | * in the PORTSC register of a root hub), ignore such case. | |
424 | */ | |
425 | if (!(portchange & USB_PORT_STAT_C_CONNECTION) && | |
426 | !(portstatus & USB_PORT_STAT_CONNECTION)) { | |
c998da0d SR |
427 | if (get_timer(0) >= hub->connect_timeout) { |
428 | debug("devnum=%d port=%d: timeout\n", | |
429 | dev->devnum, i + 1); | |
430 | /* Remove this device from scanning list */ | |
431 | list_del(&usb_scan->list); | |
432 | free(usb_scan); | |
433 | return 0; | |
434 | } | |
435 | return 0; | |
436 | } | |
437 | ||
c998da0d SR |
438 | /* A new USB device is ready at this point */ |
439 | debug("devnum=%d port=%d: USB dev found\n", dev->devnum, i + 1); | |
440 | ||
441 | usb_hub_port_connect_change(dev, i); | |
442 | ||
443 | if (portchange & USB_PORT_STAT_C_ENABLE) { | |
444 | debug("port %d enable change, status %x\n", i + 1, portstatus); | |
445 | usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE); | |
446 | /* | |
447 | * The following hack causes a ghost device problem | |
448 | * to Faraday EHCI | |
449 | */ | |
450 | #ifndef CONFIG_USB_EHCI_FARADAY | |
451 | /* | |
452 | * EM interference sometimes causes bad shielded USB | |
453 | * devices to be shutdown by the hub, this hack enables | |
454 | * them again. Works at least with mouse driver | |
455 | */ | |
456 | if (!(portstatus & USB_PORT_STAT_ENABLE) && | |
457 | (portstatus & USB_PORT_STAT_CONNECTION) && | |
458 | usb_device_has_child_on_port(dev, i)) { | |
459 | debug("already running port %i disabled by hub (EMI?), re-enabling...\n", | |
460 | i + 1); | |
461 | usb_hub_port_connect_change(dev, i); | |
462 | } | |
463 | #endif | |
464 | } | |
465 | ||
466 | if (portstatus & USB_PORT_STAT_SUSPEND) { | |
467 | debug("port %d suspend change\n", i + 1); | |
468 | usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_SUSPEND); | |
469 | } | |
470 | ||
471 | if (portchange & USB_PORT_STAT_C_OVERCURRENT) { | |
472 | debug("port %d over-current change\n", i + 1); | |
473 | usb_clear_port_feature(dev, i + 1, | |
474 | USB_PORT_FEAT_C_OVER_CURRENT); | |
475 | /* Only power-on this one port */ | |
476 | usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER); | |
477 | hub->overcurrent_count[i]++; | |
478 | ||
479 | /* | |
480 | * If the max-scan-count is not reached, return without removing | |
481 | * the device from scan-list. This will re-issue a new scan. | |
482 | */ | |
483 | if (hub->overcurrent_count[i] <= | |
484 | PORT_OVERCURRENT_MAX_SCAN_COUNT) | |
485 | return 0; | |
486 | ||
487 | /* Otherwise the device will get removed */ | |
eae4b2b6 | 488 | printf("Port %d over-current occurred %d times\n", i + 1, |
c998da0d SR |
489 | hub->overcurrent_count[i]); |
490 | } | |
491 | ||
492 | if (portchange & USB_PORT_STAT_C_RESET) { | |
493 | debug("port %d reset change\n", i + 1); | |
494 | usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET); | |
495 | } | |
496 | ||
497 | /* | |
498 | * We're done with this device, so let's remove this device from | |
499 | * scanning list | |
500 | */ | |
501 | list_del(&usb_scan->list); | |
502 | free(usb_scan); | |
503 | ||
504 | return 0; | |
505 | } | |
506 | ||
507 | static int usb_device_list_scan(void) | |
508 | { | |
509 | struct usb_device_scan *usb_scan; | |
510 | struct usb_device_scan *tmp; | |
511 | static int running; | |
512 | int ret = 0; | |
513 | ||
514 | /* Only run this loop once for each controller */ | |
515 | if (running) | |
516 | return 0; | |
517 | ||
518 | running = 1; | |
519 | ||
520 | while (1) { | |
521 | /* We're done, once the list is empty again */ | |
522 | if (list_empty(&usb_scan_list)) | |
523 | goto out; | |
524 | ||
525 | list_for_each_entry_safe(usb_scan, tmp, &usb_scan_list, list) { | |
526 | int ret; | |
527 | ||
528 | /* Scan this port */ | |
529 | ret = usb_scan_port(usb_scan); | |
530 | if (ret) | |
531 | goto out; | |
532 | } | |
533 | } | |
534 | ||
535 | out: | |
536 | /* | |
537 | * This USB controller has finished scanning all its connected | |
538 | * USB devices. Set "running" back to 0, so that other USB controllers | |
539 | * will scan their devices too. | |
540 | */ | |
541 | running = 0; | |
542 | ||
543 | return ret; | |
544 | } | |
23faf2bc MV |
545 | |
546 | static int usb_hub_configure(struct usb_device *dev) | |
547 | { | |
eaf3e613 | 548 | int i, length; |
f5766139 PS |
549 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ); |
550 | unsigned char *bitmap; | |
93ad908c | 551 | short hubCharacteristics; |
23faf2bc MV |
552 | struct usb_hub_descriptor *descriptor; |
553 | struct usb_hub_device *hub; | |
ceb4972a | 554 | __maybe_unused struct usb_hub_status *hubsts; |
361ad6af | 555 | int ret; |
23faf2bc MV |
556 | |
557 | /* "allocate" Hub device */ | |
558 | hub = usb_hub_allocate(); | |
559 | if (hub == NULL) | |
361ad6af | 560 | return -ENOMEM; |
23faf2bc MV |
561 | hub->pusb_dev = dev; |
562 | /* Get the the hub descriptor */ | |
361ad6af SG |
563 | ret = usb_get_hub_descriptor(dev, buffer, 4); |
564 | if (ret < 0) { | |
ceb4972a VG |
565 | debug("usb_hub_configure: failed to get hub " \ |
566 | "descriptor, giving up %lX\n", dev->status); | |
361ad6af | 567 | return ret; |
23faf2bc MV |
568 | } |
569 | descriptor = (struct usb_hub_descriptor *)buffer; | |
570 | ||
b4141195 MY |
571 | length = min_t(int, descriptor->bLength, |
572 | sizeof(struct usb_hub_descriptor)); | |
23faf2bc | 573 | |
361ad6af SG |
574 | ret = usb_get_hub_descriptor(dev, buffer, length); |
575 | if (ret < 0) { | |
ceb4972a VG |
576 | debug("usb_hub_configure: failed to get hub " \ |
577 | "descriptor 2nd giving up %lX\n", dev->status); | |
361ad6af | 578 | return ret; |
23faf2bc | 579 | } |
eaf3e613 | 580 | memcpy((unsigned char *)&hub->desc, buffer, length); |
23faf2bc | 581 | /* adjust 16bit values */ |
93ad908c LS |
582 | put_unaligned(le16_to_cpu(get_unaligned( |
583 | &descriptor->wHubCharacteristics)), | |
584 | &hub->desc.wHubCharacteristics); | |
23faf2bc MV |
585 | /* set the bitmap */ |
586 | bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0]; | |
587 | /* devices not removable by default */ | |
588 | memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); | |
589 | bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0]; | |
590 | memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */ | |
591 | ||
592 | for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++) | |
593 | hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i]; | |
594 | ||
595 | for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++) | |
596 | hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i]; | |
597 | ||
598 | dev->maxchild = descriptor->bNbrPorts; | |
ceb4972a | 599 | debug("%d ports detected\n", dev->maxchild); |
23faf2bc | 600 | |
93ad908c LS |
601 | hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics); |
602 | switch (hubCharacteristics & HUB_CHAR_LPSM) { | |
23faf2bc | 603 | case 0x00: |
ceb4972a | 604 | debug("ganged power switching\n"); |
23faf2bc MV |
605 | break; |
606 | case 0x01: | |
ceb4972a | 607 | debug("individual port power switching\n"); |
23faf2bc MV |
608 | break; |
609 | case 0x02: | |
610 | case 0x03: | |
ceb4972a | 611 | debug("unknown reserved power switching mode\n"); |
23faf2bc MV |
612 | break; |
613 | } | |
614 | ||
93ad908c | 615 | if (hubCharacteristics & HUB_CHAR_COMPOUND) |
ceb4972a | 616 | debug("part of a compound device\n"); |
23faf2bc | 617 | else |
ceb4972a | 618 | debug("standalone hub\n"); |
23faf2bc | 619 | |
93ad908c | 620 | switch (hubCharacteristics & HUB_CHAR_OCPM) { |
23faf2bc | 621 | case 0x00: |
ceb4972a | 622 | debug("global over-current protection\n"); |
23faf2bc MV |
623 | break; |
624 | case 0x08: | |
ceb4972a | 625 | debug("individual port over-current protection\n"); |
23faf2bc MV |
626 | break; |
627 | case 0x10: | |
628 | case 0x18: | |
ceb4972a | 629 | debug("no over-current protection\n"); |
23faf2bc MV |
630 | break; |
631 | } | |
632 | ||
ceb4972a VG |
633 | debug("power on to power good time: %dms\n", |
634 | descriptor->bPwrOn2PwrGood * 2); | |
635 | debug("hub controller current requirement: %dmA\n", | |
636 | descriptor->bHubContrCurrent); | |
23faf2bc MV |
637 | |
638 | for (i = 0; i < dev->maxchild; i++) | |
ceb4972a VG |
639 | debug("port %d is%s removable\n", i + 1, |
640 | hub->desc.DeviceRemovable[(i + 1) / 8] & \ | |
641 | (1 << ((i + 1) % 8)) ? " not" : ""); | |
23faf2bc MV |
642 | |
643 | if (sizeof(struct usb_hub_status) > USB_BUFSIZ) { | |
ceb4972a VG |
644 | debug("usb_hub_configure: failed to get Status - " \ |
645 | "too long: %d\n", descriptor->bLength); | |
361ad6af | 646 | return -EFBIG; |
23faf2bc MV |
647 | } |
648 | ||
361ad6af SG |
649 | ret = usb_get_hub_status(dev, buffer); |
650 | if (ret < 0) { | |
ceb4972a VG |
651 | debug("usb_hub_configure: failed to get Status %lX\n", |
652 | dev->status); | |
361ad6af | 653 | return ret; |
23faf2bc MV |
654 | } |
655 | ||
ceb4972a | 656 | #ifdef DEBUG |
23faf2bc MV |
657 | hubsts = (struct usb_hub_status *)buffer; |
658 | #endif | |
ceb4972a VG |
659 | |
660 | debug("get_hub_status returned status %X, change %X\n", | |
661 | le16_to_cpu(hubsts->wHubStatus), | |
662 | le16_to_cpu(hubsts->wHubChange)); | |
663 | debug("local power source is %s\n", | |
664 | (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \ | |
665 | "lost (inactive)" : "good"); | |
666 | debug("%sover-current condition exists\n", | |
667 | (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \ | |
668 | "" : "no "); | |
23faf2bc MV |
669 | usb_hub_power_on(hub); |
670 | ||
3615a996 DM |
671 | /* |
672 | * Reset any devices that may be in a bad state when applying | |
673 | * the power. This is a __weak function. Resetting of the devices | |
674 | * should occur in the board file of the device. | |
675 | */ | |
676 | for (i = 0; i < dev->maxchild; i++) | |
677 | usb_hub_reset_devices(i + 1); | |
678 | ||
c998da0d SR |
679 | /* |
680 | * Only add the connected USB devices, including potential hubs, | |
681 | * to a scanning list. This list will get scanned and devices that | |
682 | * are detected (either via port connected or via port timeout) | |
683 | * will get removed from this list. Scanning of the devices on this | |
684 | * list will continue until all devices are removed. | |
685 | */ | |
23faf2bc | 686 | for (i = 0; i < dev->maxchild; i++) { |
c998da0d | 687 | struct usb_device_scan *usb_scan; |
b6d7852c | 688 | |
c998da0d SR |
689 | usb_scan = calloc(1, sizeof(*usb_scan)); |
690 | if (!usb_scan) { | |
691 | printf("Can't allocate memory for USB device!\n"); | |
692 | return -ENOMEM; | |
23faf2bc | 693 | } |
c998da0d SR |
694 | usb_scan->dev = dev; |
695 | usb_scan->hub = hub; | |
696 | usb_scan->port = i; | |
697 | list_add_tail(&usb_scan->list, &usb_scan_list); | |
698 | } | |
23faf2bc | 699 | |
c998da0d SR |
700 | /* |
701 | * And now call the scanning code which loops over the generated list | |
702 | */ | |
703 | ret = usb_device_list_scan(); | |
23faf2bc | 704 | |
c998da0d | 705 | return ret; |
23faf2bc MV |
706 | } |
707 | ||
361ad6af | 708 | static int usb_hub_check(struct usb_device *dev, int ifnum) |
23faf2bc MV |
709 | { |
710 | struct usb_interface *iface; | |
361ad6af | 711 | struct usb_endpoint_descriptor *ep = NULL; |
23faf2bc MV |
712 | |
713 | iface = &dev->config.if_desc[ifnum]; | |
714 | /* Is it a hub? */ | |
715 | if (iface->desc.bInterfaceClass != USB_CLASS_HUB) | |
361ad6af | 716 | goto err; |
23faf2bc MV |
717 | /* Some hubs have a subclass of 1, which AFAICT according to the */ |
718 | /* specs is not defined, but it works */ | |
719 | if ((iface->desc.bInterfaceSubClass != 0) && | |
720 | (iface->desc.bInterfaceSubClass != 1)) | |
361ad6af | 721 | goto err; |
23faf2bc MV |
722 | /* Multiple endpoints? What kind of mutant ninja-hub is this? */ |
723 | if (iface->desc.bNumEndpoints != 1) | |
361ad6af | 724 | goto err; |
23faf2bc MV |
725 | ep = &iface->ep_desc[0]; |
726 | /* Output endpoint? Curiousier and curiousier.. */ | |
727 | if (!(ep->bEndpointAddress & USB_DIR_IN)) | |
361ad6af | 728 | goto err; |
23faf2bc MV |
729 | /* If it's not an interrupt endpoint, we'd better punt! */ |
730 | if ((ep->bmAttributes & 3) != 3) | |
361ad6af | 731 | goto err; |
23faf2bc | 732 | /* We found a hub */ |
ceb4972a | 733 | debug("USB hub found\n"); |
361ad6af SG |
734 | return 0; |
735 | ||
736 | err: | |
737 | debug("USB hub not found: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n", | |
738 | iface->desc.bInterfaceClass, iface->desc.bInterfaceSubClass, | |
739 | iface->desc.bNumEndpoints); | |
740 | if (ep) { | |
741 | debug(" bEndpointAddress=%#x, bmAttributes=%d", | |
742 | ep->bEndpointAddress, ep->bmAttributes); | |
743 | } | |
744 | ||
745 | return -ENOENT; | |
746 | } | |
747 | ||
748 | int usb_hub_probe(struct usb_device *dev, int ifnum) | |
749 | { | |
750 | int ret; | |
751 | ||
752 | ret = usb_hub_check(dev, ifnum); | |
753 | if (ret) | |
754 | return 0; | |
23faf2bc MV |
755 | ret = usb_hub_configure(dev); |
756 | return ret; | |
757 | } | |
054fe48e SG |
758 | |
759 | #ifdef CONFIG_DM_USB | |
760 | int usb_hub_scan(struct udevice *hub) | |
761 | { | |
bcbe3d15 | 762 | struct usb_device *udev = dev_get_parent_priv(hub); |
054fe48e SG |
763 | |
764 | return usb_hub_configure(udev); | |
765 | } | |
766 | ||
054fe48e SG |
767 | static int usb_hub_post_probe(struct udevice *dev) |
768 | { | |
769 | debug("%s\n", __func__); | |
770 | return usb_hub_scan(dev); | |
771 | } | |
772 | ||
773 | static const struct udevice_id usb_hub_ids[] = { | |
774 | { .compatible = "usb-hub" }, | |
775 | { } | |
776 | }; | |
777 | ||
778 | U_BOOT_DRIVER(usb_generic_hub) = { | |
779 | .name = "usb_hub", | |
780 | .id = UCLASS_USB_HUB, | |
781 | .of_match = usb_hub_ids, | |
782 | .flags = DM_FLAG_ALLOC_PRIV_DMA, | |
783 | }; | |
784 | ||
785 | UCLASS_DRIVER(usb_hub) = { | |
786 | .id = UCLASS_USB_HUB, | |
787 | .name = "usb_hub", | |
91195485 | 788 | .post_bind = dm_scan_fdt_dev, |
054fe48e SG |
789 | .post_probe = usb_hub_post_probe, |
790 | .child_pre_probe = usb_child_pre_probe, | |
791 | .per_child_auto_alloc_size = sizeof(struct usb_device), | |
792 | .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata), | |
793 | }; | |
794 | ||
795 | static const struct usb_device_id hub_id_table[] = { | |
796 | { | |
797 | .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, | |
798 | .bDeviceClass = USB_CLASS_HUB | |
799 | }, | |
800 | { } /* Terminating entry */ | |
801 | }; | |
802 | ||
abb59cff | 803 | U_BOOT_USB_DEVICE(usb_generic_hub, hub_id_table); |
054fe48e SG |
804 | |
805 | #endif |