]>
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 MV |
32 | #include <linux/ctype.h> |
33 | #include <asm/byteorder.h> | |
34 | #include <asm/unaligned.h> | |
054fe48e SG |
35 | #include <dm/root.h> |
36 | ||
37 | DECLARE_GLOBAL_DATA_PTR; | |
23faf2bc MV |
38 | |
39 | #include <usb.h> | |
40 | #ifdef CONFIG_4xx | |
41 | #include <asm/4xx_pci.h> | |
42 | #endif | |
43 | ||
23faf2bc MV |
44 | #define USB_BUFSIZ 512 |
45 | ||
054fe48e | 46 | /* TODO([email protected]): Remove this when CONFIG_DM_USB is defined */ |
23faf2bc MV |
47 | static struct usb_hub_device hub_dev[USB_MAX_HUB]; |
48 | static int usb_hub_index; | |
49 | ||
3615a996 DM |
50 | __weak void usb_hub_reset_devices(int port) |
51 | { | |
52 | return; | |
53 | } | |
23faf2bc MV |
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 | ||
08f3bb0b | 83 | int usb_get_port_status(struct usb_device *dev, int port, void *data) |
23faf2bc MV |
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; | |
a1a28c6e | 95 | unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2; |
319418c0 | 96 | const char *env; |
23faf2bc MV |
97 | |
98 | dev = hub->pusb_dev; | |
0bf796f7 | 99 | |
ceb4972a | 100 | debug("enabling power on all ports\n"); |
0bf796f7 | 101 | for (i = 0; i < dev->maxchild; i++) { |
23faf2bc | 102 | usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER); |
ceb4972a | 103 | debug("port %d returns %lX\n", i + 1, dev->status); |
23faf2bc | 104 | } |
a1a28c6e | 105 | |
0d437bca SW |
106 | /* |
107 | * Wait for power to become stable, | |
108 | * plus spec-defined max time for device to connect | |
319418c0 TH |
109 | * but allow this time to be increased via env variable as some |
110 | * devices break the spec and require longer warm-up times | |
0d437bca | 111 | */ |
319418c0 TH |
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); | |
77b83e6d | 117 | mdelay(pgood_delay + 1000); |
23faf2bc MV |
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 | { | |
55f4b575 VG |
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; | |
23faf2bc MV |
156 | } |
157 | ||
862e75c0 | 158 | int legacy_hub_port_reset(struct usb_device *dev, int port, |
23faf2bc MV |
159 | unsigned short *portstat) |
160 | { | |
ad84a42f | 161 | int err, tries; |
f5766139 | 162 | ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); |
23faf2bc MV |
163 | unsigned short portstatus, portchange; |
164 | ||
054fe48e SG |
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 | |
23faf2bc | 171 | for (tries = 0; tries < MAX_TRIES; tries++) { |
ad84a42f HG |
172 | err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET); |
173 | if (err < 0) | |
174 | return err; | |
23faf2bc | 175 | |
5b84dd67 | 176 | mdelay(200); |
23faf2bc | 177 | |
f5766139 | 178 | if (usb_get_port_status(dev, port + 1, portsts) < 0) { |
ceb4972a VG |
179 | debug("get_port_status failed status %lX\n", |
180 | dev->status); | |
23faf2bc MV |
181 | return -1; |
182 | } | |
f5766139 PS |
183 | portstatus = le16_to_cpu(portsts->wPortStatus); |
184 | portchange = le16_to_cpu(portsts->wPortChange); | |
23faf2bc | 185 | |
ceb4972a VG |
186 | debug("portstatus %x, change %x, %s\n", portstatus, portchange, |
187 | portspeed(portstatus)); | |
23faf2bc | 188 | |
ceb4972a VG |
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); | |
23faf2bc | 194 | |
74c0d756 SW |
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 | */ | |
23faf2bc MV |
211 | |
212 | if (portstatus & USB_PORT_STAT_ENABLE) | |
213 | break; | |
214 | ||
5b84dd67 | 215 | mdelay(200); |
23faf2bc MV |
216 | } |
217 | ||
218 | if (tries == MAX_TRIES) { | |
ceb4972a VG |
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"); | |
23faf2bc MV |
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 | ||
054fe48e SG |
230 | #ifdef CONFIG_DM_USB |
231 | int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat) | |
232 | { | |
bcbe3d15 | 233 | struct usb_device *udev = dev_get_parent_priv(dev); |
054fe48e SG |
234 | |
235 | return legacy_hub_port_reset(udev, port, portstat); | |
236 | } | |
237 | #endif | |
23faf2bc | 238 | |
79b58887 | 239 | int usb_hub_port_connect_change(struct usb_device *dev, int port) |
23faf2bc | 240 | { |
f5766139 | 241 | ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); |
23faf2bc | 242 | unsigned short portstatus; |
79b58887 | 243 | int ret, speed; |
23faf2bc MV |
244 | |
245 | /* Check status */ | |
79b58887 SG |
246 | ret = usb_get_port_status(dev, port + 1, portsts); |
247 | if (ret < 0) { | |
ceb4972a | 248 | debug("get_port_status failed\n"); |
79b58887 | 249 | return ret; |
23faf2bc MV |
250 | } |
251 | ||
f5766139 | 252 | portstatus = le16_to_cpu(portsts->wPortStatus); |
ceb4972a VG |
253 | debug("portstatus %x, change %x, %s\n", |
254 | portstatus, | |
255 | le16_to_cpu(portsts->wPortChange), | |
256 | portspeed(portstatus)); | |
23faf2bc MV |
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)) && | |
054fe48e SG |
263 | (!(portstatus & USB_PORT_STAT_ENABLE))) || |
264 | usb_device_has_child_on_port(dev, port)) { | |
ceb4972a | 265 | debug("usb_disconnect(&hub->children[port]);\n"); |
23faf2bc MV |
266 | /* Return now if nothing is connected */ |
267 | if (!(portstatus & USB_PORT_STAT_CONNECTION)) | |
79b58887 | 268 | return -ENOTCONN; |
23faf2bc | 269 | } |
5b84dd67 | 270 | mdelay(200); |
23faf2bc MV |
271 | |
272 | /* Reset the port */ | |
862e75c0 | 273 | ret = legacy_hub_port_reset(dev, port, &portstatus); |
79b58887 | 274 | if (ret < 0) { |
45b9ea1d HG |
275 | if (ret != -ENXIO) |
276 | printf("cannot reset port %i!?\n", port + 1); | |
79b58887 | 277 | return ret; |
23faf2bc MV |
278 | } |
279 | ||
5b84dd67 | 280 | mdelay(200); |
23faf2bc | 281 | |
55f4b575 VG |
282 | switch (portstatus & USB_PORT_STAT_SPEED_MASK) { |
283 | case USB_PORT_STAT_SUPER_SPEED: | |
79b58887 | 284 | speed = USB_SPEED_SUPER; |
55f4b575 VG |
285 | break; |
286 | case USB_PORT_STAT_HIGH_SPEED: | |
79b58887 | 287 | speed = USB_SPEED_HIGH; |
55f4b575 VG |
288 | break; |
289 | case USB_PORT_STAT_LOW_SPEED: | |
79b58887 | 290 | speed = USB_SPEED_LOW; |
55f4b575 VG |
291 | break; |
292 | default: | |
79b58887 | 293 | speed = USB_SPEED_FULL; |
55f4b575 VG |
294 | break; |
295 | } | |
23faf2bc | 296 | |
054fe48e SG |
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 | ||
79b58887 SG |
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 | ||
23faf2bc | 310 | dev->children[port] = usb; |
79b58887 | 311 | usb->speed = speed; |
23faf2bc MV |
312 | usb->parent = dev; |
313 | usb->portnr = port + 1; | |
314 | /* Run it through the hoops (find a driver, etc) */ | |
79b58887 SG |
315 | ret = usb_new_device(usb); |
316 | if (ret < 0) { | |
23faf2bc | 317 | /* Woops, disable the port */ |
79b58887 | 318 | usb_free_device(dev->controller); |
359439d2 | 319 | dev->children[port] = NULL; |
054fe48e SG |
320 | } |
321 | #endif | |
322 | if (ret < 0) { | |
ceb4972a | 323 | debug("hub: disabling port %d\n", port + 1); |
23faf2bc MV |
324 | usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE); |
325 | } | |
79b58887 SG |
326 | |
327 | return ret; | |
23faf2bc MV |
328 | } |
329 | ||
330 | ||
331 | static int usb_hub_configure(struct usb_device *dev) | |
332 | { | |
eaf3e613 | 333 | int i, length; |
f5766139 PS |
334 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ); |
335 | unsigned char *bitmap; | |
93ad908c | 336 | short hubCharacteristics; |
23faf2bc MV |
337 | struct usb_hub_descriptor *descriptor; |
338 | struct usb_hub_device *hub; | |
ceb4972a | 339 | __maybe_unused struct usb_hub_status *hubsts; |
361ad6af | 340 | int ret; |
23faf2bc MV |
341 | |
342 | /* "allocate" Hub device */ | |
343 | hub = usb_hub_allocate(); | |
344 | if (hub == NULL) | |
361ad6af | 345 | return -ENOMEM; |
23faf2bc MV |
346 | hub->pusb_dev = dev; |
347 | /* Get the the hub descriptor */ | |
361ad6af SG |
348 | ret = usb_get_hub_descriptor(dev, buffer, 4); |
349 | if (ret < 0) { | |
ceb4972a VG |
350 | debug("usb_hub_configure: failed to get hub " \ |
351 | "descriptor, giving up %lX\n", dev->status); | |
361ad6af | 352 | return ret; |
23faf2bc MV |
353 | } |
354 | descriptor = (struct usb_hub_descriptor *)buffer; | |
355 | ||
b4141195 MY |
356 | length = min_t(int, descriptor->bLength, |
357 | sizeof(struct usb_hub_descriptor)); | |
23faf2bc | 358 | |
361ad6af SG |
359 | ret = usb_get_hub_descriptor(dev, buffer, length); |
360 | if (ret < 0) { | |
ceb4972a VG |
361 | debug("usb_hub_configure: failed to get hub " \ |
362 | "descriptor 2nd giving up %lX\n", dev->status); | |
361ad6af | 363 | return ret; |
23faf2bc | 364 | } |
eaf3e613 | 365 | memcpy((unsigned char *)&hub->desc, buffer, length); |
23faf2bc | 366 | /* adjust 16bit values */ |
93ad908c LS |
367 | put_unaligned(le16_to_cpu(get_unaligned( |
368 | &descriptor->wHubCharacteristics)), | |
369 | &hub->desc.wHubCharacteristics); | |
23faf2bc MV |
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; | |
ceb4972a | 384 | debug("%d ports detected\n", dev->maxchild); |
23faf2bc | 385 | |
93ad908c LS |
386 | hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics); |
387 | switch (hubCharacteristics & HUB_CHAR_LPSM) { | |
23faf2bc | 388 | case 0x00: |
ceb4972a | 389 | debug("ganged power switching\n"); |
23faf2bc MV |
390 | break; |
391 | case 0x01: | |
ceb4972a | 392 | debug("individual port power switching\n"); |
23faf2bc MV |
393 | break; |
394 | case 0x02: | |
395 | case 0x03: | |
ceb4972a | 396 | debug("unknown reserved power switching mode\n"); |
23faf2bc MV |
397 | break; |
398 | } | |
399 | ||
93ad908c | 400 | if (hubCharacteristics & HUB_CHAR_COMPOUND) |
ceb4972a | 401 | debug("part of a compound device\n"); |
23faf2bc | 402 | else |
ceb4972a | 403 | debug("standalone hub\n"); |
23faf2bc | 404 | |
93ad908c | 405 | switch (hubCharacteristics & HUB_CHAR_OCPM) { |
23faf2bc | 406 | case 0x00: |
ceb4972a | 407 | debug("global over-current protection\n"); |
23faf2bc MV |
408 | break; |
409 | case 0x08: | |
ceb4972a | 410 | debug("individual port over-current protection\n"); |
23faf2bc MV |
411 | break; |
412 | case 0x10: | |
413 | case 0x18: | |
ceb4972a | 414 | debug("no over-current protection\n"); |
23faf2bc MV |
415 | break; |
416 | } | |
417 | ||
ceb4972a VG |
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); | |
23faf2bc MV |
422 | |
423 | for (i = 0; i < dev->maxchild; i++) | |
ceb4972a VG |
424 | debug("port %d is%s removable\n", i + 1, |
425 | hub->desc.DeviceRemovable[(i + 1) / 8] & \ | |
426 | (1 << ((i + 1) % 8)) ? " not" : ""); | |
23faf2bc MV |
427 | |
428 | if (sizeof(struct usb_hub_status) > USB_BUFSIZ) { | |
ceb4972a VG |
429 | debug("usb_hub_configure: failed to get Status - " \ |
430 | "too long: %d\n", descriptor->bLength); | |
361ad6af | 431 | return -EFBIG; |
23faf2bc MV |
432 | } |
433 | ||
361ad6af SG |
434 | ret = usb_get_hub_status(dev, buffer); |
435 | if (ret < 0) { | |
ceb4972a VG |
436 | debug("usb_hub_configure: failed to get Status %lX\n", |
437 | dev->status); | |
361ad6af | 438 | return ret; |
23faf2bc MV |
439 | } |
440 | ||
ceb4972a | 441 | #ifdef DEBUG |
23faf2bc MV |
442 | hubsts = (struct usb_hub_status *)buffer; |
443 | #endif | |
ceb4972a VG |
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 "); | |
23faf2bc MV |
454 | usb_hub_power_on(hub); |
455 | ||
3615a996 DM |
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 | ||
23faf2bc | 464 | for (i = 0; i < dev->maxchild; i++) { |
f5766139 | 465 | ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1); |
23faf2bc | 466 | unsigned short portstatus, portchange; |
b6d7852c VK |
467 | int ret; |
468 | ulong start = get_timer(0); | |
469 | ||
054fe48e SG |
470 | #ifdef CONFIG_DM_USB |
471 | debug("\n\nScanning '%s' port %d\n", dev->dev->name, i + 1); | |
472 | #else | |
361ad6af | 473 | debug("\n\nScanning port %d\n", i + 1); |
054fe48e | 474 | #endif |
b6d7852c VK |
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) { | |
ceb4972a | 486 | debug("get_port_status failed\n"); |
b6d7852c VK |
487 | break; |
488 | } | |
489 | ||
490 | portstatus = le16_to_cpu(portsts->wPortStatus); | |
491 | portchange = le16_to_cpu(portsts->wPortChange); | |
492 | ||
dcc7dbc7 MV |
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) | |
b6d7852c VK |
499 | break; |
500 | ||
dcc7dbc7 | 501 | } while (get_timer(start) < CONFIG_SYS_HZ * 1); |
b6d7852c VK |
502 | |
503 | if (ret < 0) | |
23faf2bc | 504 | continue; |
23faf2bc | 505 | |
ceb4972a VG |
506 | debug("Port %d Status %X Change %X\n", |
507 | i + 1, portstatus, portchange); | |
23faf2bc MV |
508 | |
509 | if (portchange & USB_PORT_STAT_C_CONNECTION) { | |
ceb4972a | 510 | debug("port %d connection change\n", i + 1); |
23faf2bc MV |
511 | usb_hub_port_connect_change(dev, i); |
512 | } | |
513 | if (portchange & USB_PORT_STAT_C_ENABLE) { | |
ceb4972a VG |
514 | debug("port %d enable change, status %x\n", |
515 | i + 1, portstatus); | |
23faf2bc MV |
516 | usb_clear_port_feature(dev, i + 1, |
517 | USB_PORT_FEAT_C_ENABLE); | |
e82a316d KJS |
518 | /* |
519 | * The following hack causes a ghost device problem | |
520 | * to Faraday EHCI | |
521 | */ | |
522 | #ifndef CONFIG_USB_EHCI_FARADAY | |
23faf2bc MV |
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) && | |
054fe48e | 528 | usb_device_has_child_on_port(dev, i)) { |
ceb4972a VG |
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); | |
23faf2bc | 533 | } |
e82a316d | 534 | #endif |
23faf2bc MV |
535 | } |
536 | if (portstatus & USB_PORT_STAT_SUSPEND) { | |
ceb4972a | 537 | debug("port %d suspend change\n", i + 1); |
23faf2bc MV |
538 | usb_clear_port_feature(dev, i + 1, |
539 | USB_PORT_FEAT_SUSPEND); | |
540 | } | |
541 | ||
542 | if (portchange & USB_PORT_STAT_C_OVERCURRENT) { | |
ceb4972a | 543 | debug("port %d over-current change\n", i + 1); |
23faf2bc MV |
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) { | |
ceb4972a | 550 | debug("port %d reset change\n", i + 1); |
23faf2bc MV |
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 | ||
361ad6af | 559 | static int usb_hub_check(struct usb_device *dev, int ifnum) |
23faf2bc MV |
560 | { |
561 | struct usb_interface *iface; | |
361ad6af | 562 | struct usb_endpoint_descriptor *ep = NULL; |
23faf2bc MV |
563 | |
564 | iface = &dev->config.if_desc[ifnum]; | |
565 | /* Is it a hub? */ | |
566 | if (iface->desc.bInterfaceClass != USB_CLASS_HUB) | |
361ad6af | 567 | goto err; |
23faf2bc MV |
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)) | |
361ad6af | 572 | goto err; |
23faf2bc MV |
573 | /* Multiple endpoints? What kind of mutant ninja-hub is this? */ |
574 | if (iface->desc.bNumEndpoints != 1) | |
361ad6af | 575 | goto err; |
23faf2bc MV |
576 | ep = &iface->ep_desc[0]; |
577 | /* Output endpoint? Curiousier and curiousier.. */ | |
578 | if (!(ep->bEndpointAddress & USB_DIR_IN)) | |
361ad6af | 579 | goto err; |
23faf2bc MV |
580 | /* If it's not an interrupt endpoint, we'd better punt! */ |
581 | if ((ep->bmAttributes & 3) != 3) | |
361ad6af | 582 | goto err; |
23faf2bc | 583 | /* We found a hub */ |
ceb4972a | 584 | debug("USB hub found\n"); |
361ad6af SG |
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; | |
23faf2bc MV |
606 | ret = usb_hub_configure(dev); |
607 | return ret; | |
608 | } | |
054fe48e SG |
609 | |
610 | #ifdef CONFIG_DM_USB | |
611 | int usb_hub_scan(struct udevice *hub) | |
612 | { | |
bcbe3d15 | 613 | struct usb_device *udev = dev_get_parent_priv(hub); |
054fe48e SG |
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 | ||
abb59cff | 660 | U_BOOT_USB_DEVICE(usb_generic_hub, hub_id_table); |
054fe48e SG |
661 | |
662 | #endif |