]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbHub.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusDxe / UsbHub.c
CommitLineData
e237e7ae 1/** @file\r
2\r
8616fc4c 3 Unified interface for RootHub and Hub.\r
4\r
d1102dba 5Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 6SPDX-License-Identifier: BSD-2-Clause-Patent\r
e237e7ae 7\r
e237e7ae 8**/\r
9\r
10#include "UsbBus.h"\r
11\r
aa79b0b3 12//\r
13// Array that maps the change bit to feature value which is\r
14// used to clear these change bit. USB HUB API will clear\r
15// these change bit automatically. For non-root hub, these\r
16// bits determine whether hub will report the port in changed\r
17// bit maps.\r
18//\r
92870c98 19USB_CHANGE_FEATURE_MAP mHubFeatureMap[] = {\r
1436aea4
MK
20 { USB_PORT_STAT_C_CONNECTION, EfiUsbPortConnectChange },\r
21 { USB_PORT_STAT_C_ENABLE, EfiUsbPortEnableChange },\r
22 { USB_PORT_STAT_C_SUSPEND, EfiUsbPortSuspendChange },\r
23 { USB_PORT_STAT_C_OVERCURRENT, EfiUsbPortOverCurrentChange },\r
24 { USB_PORT_STAT_C_RESET, EfiUsbPortResetChange }\r
aa79b0b3 25};\r
26\r
92870c98 27USB_CHANGE_FEATURE_MAP mRootHubFeatureMap[] = {\r
1436aea4
MK
28 { USB_PORT_STAT_C_CONNECTION, EfiUsbPortConnectChange },\r
29 { USB_PORT_STAT_C_ENABLE, EfiUsbPortEnableChange },\r
30 { USB_PORT_STAT_C_SUSPEND, EfiUsbPortSuspendChange },\r
31 { USB_PORT_STAT_C_OVERCURRENT, EfiUsbPortOverCurrentChange },\r
32 { USB_PORT_STAT_C_RESET, EfiUsbPortResetChange },\r
aa79b0b3 33};\r
34\r
e237e7ae 35//\r
36// USB hub class specific requests. Although USB hub\r
37// is related to an interface, these requests are sent\r
38// to the control endpoint of the device.\r
39//\r
1436aea4 40\r
92870c98 41/**\r
42 USB hub control transfer to set the hub depth.\r
43\r
44 @param HubDev The device of the hub.\r
45 @param Depth The depth to set.\r
46\r
47 @retval EFI_SUCCESS Depth of the hub is set.\r
48 @retval Others Failed to set the depth.\r
49\r
50**/\r
51EFI_STATUS\r
52UsbHubCtrlSetHubDepth (\r
1436aea4
MK
53 IN USB_DEVICE *HubDev,\r
54 IN UINT16 Depth\r
92870c98 55 )\r
56{\r
1436aea4 57 EFI_STATUS Status;\r
e237e7ae 58\r
92870c98 59 Status = UsbCtrlRequest (\r
60 HubDev,\r
61 EfiUsbNoData,\r
62 USB_REQ_TYPE_CLASS,\r
63 USB_HUB_TARGET_HUB,\r
64 USB_HUB_REQ_SET_DEPTH,\r
65 Depth,\r
66 0,\r
67 NULL,\r
68 0\r
69 );\r
70\r
71 return Status;\r
72}\r
e237e7ae 73\r
74/**\r
8616fc4c 75 USB hub control transfer to clear the hub feature.\r
e237e7ae 76\r
8616fc4c 77 @param HubDev The device of the hub.\r
78 @param Feature The feature to clear.\r
e237e7ae 79\r
8616fc4c 80 @retval EFI_SUCCESS Feature of the hub is cleared.\r
81 @retval Others Failed to clear the feature.\r
e237e7ae 82\r
83**/\r
e237e7ae 84EFI_STATUS\r
85UsbHubCtrlClearHubFeature (\r
1436aea4
MK
86 IN USB_DEVICE *HubDev,\r
87 IN UINT16 Feature\r
e237e7ae 88 )\r
89{\r
1436aea4 90 EFI_STATUS Status;\r
e237e7ae 91\r
92 Status = UsbCtrlRequest (\r
93 HubDev,\r
94 EfiUsbNoData,\r
95 USB_REQ_TYPE_CLASS,\r
96 USB_HUB_TARGET_HUB,\r
97 USB_HUB_REQ_CLEAR_FEATURE,\r
98 Feature,\r
99 0,\r
100 NULL,\r
101 0\r
102 );\r
103\r
104 return Status;\r
105}\r
106\r
e237e7ae 107/**\r
8616fc4c 108 Clear the feature of the device's port.\r
e237e7ae 109\r
8616fc4c 110 @param HubDev The hub device.\r
111 @param Port The port to clear feature.\r
112 @param Feature The feature to clear.\r
e237e7ae 113\r
114 @retval EFI_SUCCESS The feature of the port is cleared.\r
115 @retval Others Failed to clear the feature.\r
116\r
117**/\r
e237e7ae 118EFI_STATUS\r
119UsbHubCtrlClearPortFeature (\r
1436aea4
MK
120 IN USB_DEVICE *HubDev,\r
121 IN UINT8 Port,\r
122 IN UINT16 Feature\r
e237e7ae 123 )\r
124{\r
1436aea4 125 EFI_STATUS Status;\r
e237e7ae 126\r
127 //\r
128 // In USB bus, all the port index starts from 0. But HUB\r
129 // indexes its port from 1. So, port number is added one.\r
130 //\r
131 Status = UsbCtrlRequest (\r
132 HubDev,\r
133 EfiUsbNoData,\r
134 USB_REQ_TYPE_CLASS,\r
135 USB_HUB_TARGET_PORT,\r
136 USB_HUB_REQ_CLEAR_FEATURE,\r
137 Feature,\r
1436aea4 138 (UINT16)(Port + 1),\r
e237e7ae 139 NULL,\r
140 0\r
141 );\r
142\r
143 return Status;\r
144}\r
145\r
e237e7ae 146/**\r
147 Clear the transaction translate buffer if full/low\r
148 speed control/bulk transfer failed and the transfer\r
149 uses this hub as translator.Remember to clear the TT\r
150 buffer of transaction translator, not that of the\r
151 parent.\r
152\r
8616fc4c 153 @param HubDev The hub device.\r
154 @param Port The port of the hub.\r
155 @param DevAddr Address of the failed transaction.\r
156 @param EpNum The endpoint number of the failed transaction.\r
157 @param EpType The type of failed transaction.\r
e237e7ae 158\r
8616fc4c 159 @retval EFI_SUCCESS The TT buffer is cleared.\r
160 @retval Others Failed to clear the TT buffer.\r
e237e7ae 161\r
162**/\r
163EFI_STATUS\r
164UsbHubCtrlClearTTBuffer (\r
1436aea4
MK
165 IN USB_DEVICE *HubDev,\r
166 IN UINT8 Port,\r
167 IN UINT16 DevAddr,\r
168 IN UINT16 EpNum,\r
169 IN UINT16 EpType\r
e237e7ae 170 )\r
171{\r
1436aea4
MK
172 EFI_STATUS Status;\r
173 UINT16 Value;\r
e237e7ae 174\r
175 //\r
176 // Check USB2.0 spec page 424 for wValue's encoding\r
177 //\r
1436aea4
MK
178 Value = (UINT16)((EpNum & 0x0F) | (DevAddr << 4) |\r
179 ((EpType & 0x03) << 11) | ((EpNum & 0x80) << 15));\r
e237e7ae 180\r
181 Status = UsbCtrlRequest (\r
182 HubDev,\r
183 EfiUsbNoData,\r
184 USB_REQ_TYPE_CLASS,\r
185 USB_HUB_TARGET_PORT,\r
186 USB_HUB_REQ_CLEAR_TT,\r
187 Value,\r
1436aea4 188 (UINT16)(Port + 1),\r
e237e7ae 189 NULL,\r
190 0\r
191 );\r
192\r
193 return Status;\r
194}\r
195\r
92870c98 196/**\r
acebdf14 197 Usb hub control transfer to get the (super speed) hub descriptor.\r
e237e7ae 198\r
8616fc4c 199 @param HubDev The hub device.\r
200 @param Buf The buffer to hold the descriptor.\r
201 @param Len The length to retrieve.\r
e237e7ae 202\r
8616fc4c 203 @retval EFI_SUCCESS The hub descriptor is retrieved.\r
204 @retval Others Failed to retrieve the hub descriptor.\r
e237e7ae 205\r
206**/\r
e237e7ae 207EFI_STATUS\r
208UsbHubCtrlGetHubDesc (\r
1436aea4
MK
209 IN USB_DEVICE *HubDev,\r
210 OUT VOID *Buf,\r
211 IN UINTN Len\r
e237e7ae 212 )\r
213{\r
1436aea4
MK
214 EFI_STATUS Status;\r
215 UINT8 DescType;\r
acebdf14
SZ
216\r
217 DescType = (HubDev->Speed == EFI_USB_SPEED_SUPER) ?\r
218 USB_DESC_TYPE_HUB_SUPER_SPEED :\r
219 USB_DESC_TYPE_HUB;\r
e237e7ae 220\r
221 Status = UsbCtrlRequest (\r
222 HubDev,\r
223 EfiUsbDataIn,\r
224 USB_REQ_TYPE_CLASS,\r
225 USB_HUB_TARGET_HUB,\r
226 USB_HUB_REQ_GET_DESC,\r
1436aea4 227 (UINT16)(DescType << 8),\r
e237e7ae 228 0,\r
229 Buf,\r
230 Len\r
231 );\r
232\r
233 return Status;\r
234}\r
235\r
e237e7ae 236/**\r
8616fc4c 237 Usb hub control transfer to get the hub status.\r
e237e7ae 238\r
8616fc4c 239 @param HubDev The hub device.\r
240 @param State The variable to return the status.\r
e237e7ae 241\r
8616fc4c 242 @retval EFI_SUCCESS The hub status is returned in State.\r
243 @retval Others Failed to get the hub status.\r
e237e7ae 244\r
245**/\r
e237e7ae 246EFI_STATUS\r
247UsbHubCtrlGetHubStatus (\r
1436aea4
MK
248 IN USB_DEVICE *HubDev,\r
249 OUT UINT32 *State\r
e237e7ae 250 )\r
251{\r
1436aea4 252 EFI_STATUS Status;\r
e237e7ae 253\r
254 Status = UsbCtrlRequest (\r
255 HubDev,\r
256 EfiUsbDataIn,\r
257 USB_REQ_TYPE_CLASS,\r
258 USB_HUB_TARGET_HUB,\r
259 USB_HUB_REQ_GET_STATUS,\r
260 0,\r
261 0,\r
262 State,\r
263 4\r
264 );\r
265\r
266 return Status;\r
267}\r
268\r
e237e7ae 269/**\r
8616fc4c 270 Usb hub control transfer to get the port status.\r
e237e7ae 271\r
8616fc4c 272 @param HubDev The hub device.\r
273 @param Port The port of the hub.\r
274 @param State Variable to return the hub port state.\r
e237e7ae 275\r
8616fc4c 276 @retval EFI_SUCCESS The port state is returned in State.\r
d17371e8 277 @retval Others Failed to retrieve the port state.\r
e237e7ae 278\r
279**/\r
e237e7ae 280EFI_STATUS\r
281UsbHubCtrlGetPortStatus (\r
1436aea4
MK
282 IN USB_DEVICE *HubDev,\r
283 IN UINT8 Port,\r
284 OUT VOID *State\r
e237e7ae 285 )\r
286{\r
1436aea4 287 EFI_STATUS Status;\r
e237e7ae 288\r
289 //\r
290 // In USB bus, all the port index starts from 0. But HUB\r
291 // indexes its port from 1. So, port number is added one.\r
292 // No need to convert the hub bit to UEFI definition, they\r
293 // are the same\r
294 //\r
295 Status = UsbCtrlRequest (\r
296 HubDev,\r
297 EfiUsbDataIn,\r
298 USB_REQ_TYPE_CLASS,\r
299 USB_HUB_TARGET_PORT,\r
300 USB_HUB_REQ_GET_STATUS,\r
301 0,\r
1436aea4 302 (UINT16)(Port + 1),\r
e237e7ae 303 State,\r
304 4\r
305 );\r
306\r
307 return Status;\r
308}\r
309\r
e237e7ae 310/**\r
8616fc4c 311 Usb hub control transfer to set the port feature.\r
e237e7ae 312\r
8616fc4c 313 @param HubDev The Usb hub device.\r
314 @param Port The Usb port to set feature for.\r
315 @param Feature The feature to set.\r
e237e7ae 316\r
8616fc4c 317 @retval EFI_SUCCESS The feature is set for the port.\r
318 @retval Others Failed to set the feature.\r
e237e7ae 319\r
320**/\r
e237e7ae 321EFI_STATUS\r
322UsbHubCtrlSetPortFeature (\r
1436aea4
MK
323 IN USB_DEVICE *HubDev,\r
324 IN UINT8 Port,\r
325 IN UINT8 Feature\r
e237e7ae 326 )\r
327{\r
1436aea4 328 EFI_STATUS Status;\r
e237e7ae 329\r
330 //\r
331 // In USB bus, all the port index starts from 0. But HUB\r
332 // indexes its port from 1. So, port number is added one.\r
333 //\r
334 Status = UsbCtrlRequest (\r
335 HubDev,\r
336 EfiUsbNoData,\r
337 USB_REQ_TYPE_CLASS,\r
338 USB_HUB_TARGET_PORT,\r
339 USB_HUB_REQ_SET_FEATURE,\r
340 Feature,\r
1436aea4 341 (UINT16)(Port + 1),\r
e237e7ae 342 NULL,\r
343 0\r
344 );\r
345\r
346 return Status;\r
347}\r
348\r
e237e7ae 349/**\r
350 Read the whole usb hub descriptor. It is necessary\r
351 to do it in two steps because hub descriptor is of\r
8616fc4c 352 variable length.\r
e237e7ae 353\r
8616fc4c 354 @param HubDev The hub device.\r
355 @param HubDesc The variable to return the descriptor.\r
e237e7ae 356\r
8616fc4c 357 @retval EFI_SUCCESS The hub descriptor is read.\r
358 @retval Others Failed to read the hub descriptor.\r
e237e7ae 359\r
360**/\r
e237e7ae 361EFI_STATUS\r
362UsbHubReadDesc (\r
363 IN USB_DEVICE *HubDev,\r
364 OUT EFI_USB_HUB_DESCRIPTOR *HubDesc\r
365 )\r
366{\r
1436aea4 367 EFI_STATUS Status;\r
e237e7ae 368\r
acebdf14
SZ
369 //\r
370 // First get the hub descriptor length\r
371 //\r
372 Status = UsbHubCtrlGetHubDesc (HubDev, HubDesc, 2);\r
92870c98 373\r
acebdf14
SZ
374 if (EFI_ERROR (Status)) {\r
375 return Status;\r
92870c98 376 }\r
e237e7ae 377\r
acebdf14
SZ
378 //\r
379 // Get the whole hub descriptor\r
380 //\r
381 return UsbHubCtrlGetHubDesc (HubDev, HubDesc, HubDesc->Length);\r
e237e7ae 382}\r
383\r
e237e7ae 384/**\r
385 Ack the hub change bits. If these bits are not ACKed, Hub will\r
386 always return changed bit map from its interrupt endpoint.\r
387\r
8616fc4c 388 @param HubDev The hub device.\r
e237e7ae 389\r
8616fc4c 390 @retval EFI_SUCCESS The hub change status is ACKed.\r
391 @retval Others Failed to ACK the hub status.\r
e237e7ae 392\r
393**/\r
394EFI_STATUS\r
395UsbHubAckHubStatus (\r
1436aea4 396 IN USB_DEVICE *HubDev\r
e237e7ae 397 )\r
398{\r
1436aea4
MK
399 EFI_USB_PORT_STATUS HubState;\r
400 EFI_STATUS Status;\r
e237e7ae 401\r
1436aea4 402 Status = UsbHubCtrlGetHubStatus (HubDev, (UINT32 *)&HubState);\r
e237e7ae 403\r
404 if (EFI_ERROR (Status)) {\r
405 return Status;\r
406 }\r
407\r
408 if (USB_BIT_IS_SET (HubState.PortChangeStatus, USB_HUB_STAT_C_LOCAL_POWER)) {\r
409 UsbHubCtrlClearHubFeature (HubDev, USB_HUB_C_HUB_LOCAL_POWER);\r
410 }\r
411\r
412 if (USB_BIT_IS_SET (HubState.PortChangeStatus, USB_HUB_STAT_C_OVER_CURRENT)) {\r
413 UsbHubCtrlClearHubFeature (HubDev, USB_HUB_C_HUB_OVER_CURRENT);\r
414 }\r
415\r
416 return EFI_SUCCESS;\r
417}\r
418\r
e237e7ae 419/**\r
420 Test whether the interface is a hub interface.\r
421\r
8616fc4c 422 @param UsbIf The interface to test.\r
e237e7ae 423\r
8616fc4c 424 @retval TRUE The interface is a hub interface.\r
425 @retval FALSE The interface isn't a hub interface.\r
e237e7ae 426\r
427**/\r
428BOOLEAN\r
429UsbIsHubInterface (\r
1436aea4 430 IN USB_INTERFACE *UsbIf\r
e237e7ae 431 )\r
432{\r
433 EFI_USB_INTERFACE_DESCRIPTOR *Setting;\r
434\r
435 //\r
436 // If the hub is a high-speed hub with multiple TT,\r
437 // the hub will has a default setting of single TT.\r
438 //\r
439 Setting = &UsbIf->IfSetting->Desc;\r
440\r
441 if ((Setting->InterfaceClass == USB_HUB_CLASS_CODE) &&\r
1436aea4
MK
442 (Setting->InterfaceSubClass == USB_HUB_SUBCLASS_CODE))\r
443 {\r
e237e7ae 444 return TRUE;\r
445 }\r
446\r
447 return FALSE;\r
448}\r
449\r
e237e7ae 450/**\r
451 The callback function to the USB hub status change\r
452 interrupt endpoint. It is called periodically by\r
453 the underlying host controller.\r
454\r
8616fc4c 455 @param Data The data read.\r
456 @param DataLength The length of the data read.\r
457 @param Context The context.\r
458 @param Result The result of the last interrupt transfer.\r
e237e7ae 459\r
8616fc4c 460 @retval EFI_SUCCESS The process is OK.\r
461 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.\r
e237e7ae 462\r
463**/\r
e237e7ae 464EFI_STATUS\r
6d3ea23f 465EFIAPI\r
e237e7ae 466UsbOnHubInterrupt (\r
1436aea4
MK
467 IN VOID *Data,\r
468 IN UINTN DataLength,\r
469 IN VOID *Context,\r
470 IN UINT32 Result\r
e237e7ae 471 )\r
472{\r
1436aea4
MK
473 USB_INTERFACE *HubIf;\r
474 EFI_USB_IO_PROTOCOL *UsbIo;\r
475 EFI_USB_ENDPOINT_DESCRIPTOR *EpDesc;\r
476 EFI_STATUS Status;\r
e237e7ae 477\r
1436aea4
MK
478 HubIf = (USB_INTERFACE *)Context;\r
479 UsbIo = &(HubIf->UsbIo);\r
480 EpDesc = &(HubIf->HubEp->Desc);\r
e237e7ae 481\r
482 if (Result != EFI_USB_NOERROR) {\r
483 //\r
484 // If endpoint is stalled, clear the stall. Use UsbIo to access\r
485 // the control transfer so internal status are maintained.\r
486 //\r
487 if (USB_BIT_IS_SET (Result, EFI_USB_ERR_STALL)) {\r
488 UsbIoClearFeature (\r
489 UsbIo,\r
490 USB_TARGET_ENDPOINT,\r
491 USB_FEATURE_ENDPOINT_HALT,\r
492 EpDesc->EndpointAddress\r
493 );\r
494 }\r
495\r
496 //\r
497 // Delete and submit a new async interrupt\r
498 //\r
499 Status = UsbIo->UsbAsyncInterruptTransfer (\r
500 UsbIo,\r
501 EpDesc->EndpointAddress,\r
502 FALSE,\r
503 0,\r
504 0,\r
505 NULL,\r
506 NULL\r
507 );\r
508\r
509 if (EFI_ERROR (Status)) {\r
1436aea4 510 DEBUG ((DEBUG_ERROR, "UsbOnHubInterrupt: failed to remove async transfer - %r\n", Status));\r
e237e7ae 511 return Status;\r
512 }\r
513\r
514 Status = UsbIo->UsbAsyncInterruptTransfer (\r
515 UsbIo,\r
516 EpDesc->EndpointAddress,\r
517 TRUE,\r
518 USB_HUB_POLL_INTERVAL,\r
519 HubIf->NumOfPort / 8 + 1,\r
520 UsbOnHubInterrupt,\r
521 HubIf\r
522 );\r
523\r
524 if (EFI_ERROR (Status)) {\r
1436aea4 525 DEBUG ((DEBUG_ERROR, "UsbOnHubInterrupt: failed to submit new async transfer - %r\n", Status));\r
e237e7ae 526 }\r
527\r
528 return Status;\r
529 }\r
530\r
531 if ((DataLength == 0) || (Data == NULL)) {\r
532 return EFI_SUCCESS;\r
533 }\r
534\r
535 //\r
536 // OK, actually something is changed, save the change map\r
537 // then signal the HUB to do enumeration. This is a good\r
538 // practise since UsbOnHubInterrupt is called in the context\r
13a623cf 539 // of host controller's AsyncInterrupt monitor.\r
e237e7ae 540 //\r
541 HubIf->ChangeMap = AllocateZeroPool (DataLength);\r
542\r
543 if (HubIf->ChangeMap == NULL) {\r
544 return EFI_OUT_OF_RESOURCES;\r
545 }\r
546\r
547 CopyMem (HubIf->ChangeMap, Data, DataLength);\r
548 gBS->SignalEvent (HubIf->HubNotify);\r
549\r
550 return EFI_SUCCESS;\r
551}\r
552\r
e237e7ae 553/**\r
8616fc4c 554 Initialize the device for a non-root hub.\r
e237e7ae 555\r
8616fc4c 556 @param HubIf The USB hub interface.\r
e237e7ae 557\r
8616fc4c 558 @retval EFI_SUCCESS The hub is initialized.\r
559 @retval EFI_DEVICE_ERROR Failed to initialize the hub.\r
e237e7ae 560\r
561**/\r
e237e7ae 562EFI_STATUS\r
563UsbHubInit (\r
1436aea4 564 IN USB_INTERFACE *HubIf\r
e237e7ae 565 )\r
566{\r
acebdf14
SZ
567 UINT8 HubDescBuffer[256];\r
568 EFI_USB_HUB_DESCRIPTOR *HubDesc;\r
e237e7ae 569 USB_ENDPOINT_DESC *EpDesc;\r
570 USB_INTERFACE_SETTING *Setting;\r
571 EFI_USB_IO_PROTOCOL *UsbIo;\r
572 USB_DEVICE *HubDev;\r
573 EFI_STATUS Status;\r
574 UINT8 Index;\r
d17371e8 575 UINT8 NumEndpoints;\r
92870c98 576 UINT16 Depth;\r
e237e7ae 577\r
578 //\r
579 // Locate the interrupt endpoint for port change map\r
580 //\r
1436aea4
MK
581 HubIf->IsHub = FALSE;\r
582 Setting = HubIf->IfSetting;\r
583 HubDev = HubIf->Device;\r
584 EpDesc = NULL;\r
585 NumEndpoints = Setting->Desc.NumEndpoints;\r
e237e7ae 586\r
d17371e8 587 for (Index = 0; Index < NumEndpoints; Index++) {\r
e237e7ae 588 ASSERT ((Setting->Endpoints != NULL) && (Setting->Endpoints[Index] != NULL));\r
589\r
590 EpDesc = Setting->Endpoints[Index];\r
591\r
592 if (USB_BIT_IS_SET (EpDesc->Desc.EndpointAddress, USB_ENDPOINT_DIR_IN) &&\r
1436aea4
MK
593 (USB_ENDPOINT_TYPE (&EpDesc->Desc) == USB_ENDPOINT_INTERRUPT))\r
594 {\r
e237e7ae 595 break;\r
596 }\r
597 }\r
598\r
d17371e8 599 if (Index == NumEndpoints) {\r
1436aea4 600 DEBUG ((DEBUG_ERROR, "UsbHubInit: no interrupt endpoint found for hub %d\n", HubDev->Address));\r
e237e7ae 601 return EFI_DEVICE_ERROR;\r
602 }\r
603\r
acebdf14
SZ
604 //\r
605 // The length field of descriptor is UINT8 type, so the buffer\r
606 // with 256 bytes is enough to hold the descriptor data.\r
607 //\r
1436aea4
MK
608 HubDesc = (EFI_USB_HUB_DESCRIPTOR *)HubDescBuffer;\r
609 Status = UsbHubReadDesc (HubDev, HubDesc);\r
e237e7ae 610\r
611 if (EFI_ERROR (Status)) {\r
1436aea4 612 DEBUG ((DEBUG_ERROR, "UsbHubInit: failed to read HUB descriptor %r\n", Status));\r
e237e7ae 613 return Status;\r
614 }\r
615\r
acebdf14 616 HubIf->NumOfPort = HubDesc->NumPorts;\r
e237e7ae 617\r
1436aea4 618 DEBUG ((DEBUG_INFO, "UsbHubInit: hub %d has %d ports\n", HubDev->Address, HubIf->NumOfPort));\r
e237e7ae 619\r
92870c98 620 //\r
621 // OK, set IsHub to TRUE. Now usb bus can handle this device\r
13a623cf 622 // as a working HUB. If failed earlier, bus driver will not\r
92870c98 623 // recognize it as a hub. Other parts of the bus should be able\r
624 // to work.\r
625 //\r
626 HubIf->IsHub = TRUE;\r
627 HubIf->HubApi = &mUsbHubApi;\r
628 HubIf->HubEp = EpDesc;\r
629\r
630 if (HubIf->Device->Speed == EFI_USB_SPEED_SUPER) {\r
631 Depth = (UINT16)(HubIf->Device->Tier - 1);\r
87000d77 632 DEBUG ((DEBUG_INFO, "UsbHubInit: Set Hub Depth as 0x%x\n", Depth));\r
92870c98 633 UsbHubCtrlSetHubDepth (HubIf->Device, Depth);\r
d1102dba 634\r
acebdf14 635 for (Index = 0; Index < HubDesc->NumPorts; Index++) {\r
92870c98 636 UsbHubCtrlSetPortFeature (HubIf->Device, Index, USB_HUB_PORT_REMOTE_WAKE_MASK);\r
d1102dba 637 }\r
92870c98 638 } else {\r
639 //\r
640 // Feed power to all the hub ports. It should be ok\r
641 // for both gang/individual powered hubs.\r
642 //\r
acebdf14 643 for (Index = 0; Index < HubDesc->NumPorts; Index++) {\r
1436aea4 644 UsbHubCtrlSetPortFeature (HubIf->Device, Index, (EFI_USB_PORT_FEATURE)USB_HUB_PORT_POWER);\r
92870c98 645 }\r
646\r
c843ef67 647 //\r
648 // Update for the usb hub has no power on delay requirement\r
649 //\r
acebdf14
SZ
650 if (HubDesc->PwrOn2PwrGood > 0) {\r
651 gBS->Stall (HubDesc->PwrOn2PwrGood * USB_SET_PORT_POWER_STALL);\r
c843ef67 652 }\r
1436aea4 653\r
92870c98 654 UsbHubAckHubStatus (HubIf->Device);\r
655 }\r
656\r
e237e7ae 657 //\r
658 // Create an event to enumerate the hub's port. On\r
659 //\r
660 Status = gBS->CreateEvent (\r
661 EVT_NOTIFY_SIGNAL,\r
662 TPL_CALLBACK,\r
663 UsbHubEnumeration,\r
664 HubIf,\r
665 &HubIf->HubNotify\r
666 );\r
667\r
668 if (EFI_ERROR (Status)) {\r
1436aea4
MK
669 DEBUG ((\r
670 DEBUG_ERROR,\r
671 "UsbHubInit: failed to create signal for hub %d - %r\n",\r
672 HubDev->Address,\r
673 Status\r
674 ));\r
e237e7ae 675\r
676 return Status;\r
677 }\r
678\r
679 //\r
680 // Create AsyncInterrupt to query hub port change endpoint\r
681 // periodically. If the hub ports are changed, hub will return\r
682 // changed port map from the interrupt endpoint. The port map\r
683 // must be able to hold (HubIf->NumOfPort + 1) bits (one bit for\r
684 // host change status).\r
685 //\r
686 UsbIo = &HubIf->UsbIo;\r
687 Status = UsbIo->UsbAsyncInterruptTransfer (\r
688 UsbIo,\r
689 EpDesc->Desc.EndpointAddress,\r
690 TRUE,\r
691 USB_HUB_POLL_INTERVAL,\r
692 HubIf->NumOfPort / 8 + 1,\r
693 UsbOnHubInterrupt,\r
694 HubIf\r
695 );\r
696\r
697 if (EFI_ERROR (Status)) {\r
1436aea4
MK
698 DEBUG ((\r
699 DEBUG_ERROR,\r
700 "UsbHubInit: failed to queue interrupt transfer for hub %d - %r\n",\r
701 HubDev->Address,\r
702 Status\r
703 ));\r
e237e7ae 704\r
705 gBS->CloseEvent (HubIf->HubNotify);\r
706 HubIf->HubNotify = NULL;\r
707\r
708 return Status;\r
709 }\r
710\r
1436aea4 711 DEBUG ((DEBUG_INFO, "UsbHubInit: hub %d initialized\n", HubDev->Address));\r
e237e7ae 712 return Status;\r
713}\r
714\r
e237e7ae 715/**\r
716 Get the port status. This function is required to\r
717 ACK the port change bits although it will return\r
718 the port changes in PortState. Bus enumeration code\r
719 doesn't need to ACK the port change bits.\r
720\r
8616fc4c 721 @param HubIf The hub interface.\r
722 @param Port The port of the hub to get state.\r
723 @param PortState Variable to return the port state.\r
e237e7ae 724\r
8616fc4c 725 @retval EFI_SUCCESS The port status is successfully returned.\r
726 @retval Others Failed to return the status.\r
e237e7ae 727\r
728**/\r
e237e7ae 729EFI_STATUS\r
730UsbHubGetPortStatus (\r
1436aea4
MK
731 IN USB_INTERFACE *HubIf,\r
732 IN UINT8 Port,\r
733 OUT EFI_USB_PORT_STATUS *PortState\r
e237e7ae 734 )\r
735{\r
1436aea4 736 EFI_STATUS Status;\r
e237e7ae 737\r
1436aea4 738 Status = UsbHubCtrlGetPortStatus (HubIf->Device, Port, PortState);\r
e237e7ae 739\r
740 return Status;\r
741}\r
742\r
e237e7ae 743/**\r
744 Clear the port change status.\r
745\r
8616fc4c 746 @param HubIf The hub interface.\r
747 @param Port The hub port.\r
e237e7ae 748\r
e237e7ae 749**/\r
e237e7ae 750VOID\r
751UsbHubClearPortChange (\r
1436aea4
MK
752 IN USB_INTERFACE *HubIf,\r
753 IN UINT8 Port\r
e237e7ae 754 )\r
755{\r
756 EFI_USB_PORT_STATUS PortState;\r
757 USB_CHANGE_FEATURE_MAP *Map;\r
758 UINTN Index;\r
759 EFI_STATUS Status;\r
760\r
761 Status = UsbHubGetPortStatus (HubIf, Port, &PortState);\r
762\r
763 if (EFI_ERROR (Status)) {\r
764 return;\r
765 }\r
766\r
767 //\r
768 // OK, get the usb port status, now ACK the change bits.\r
769 // Don't return error when failed to clear the change bits.\r
770 // It may lead to extra port state report. USB bus should\r
771 // be able to handle this.\r
772 //\r
60a2d248 773 for (Index = 0; Index < ARRAY_SIZE (mHubFeatureMap); Index++) {\r
e237e7ae 774 Map = &mHubFeatureMap[Index];\r
775\r
776 if (USB_BIT_IS_SET (PortState.PortChangeStatus, Map->ChangedBit)) {\r
1436aea4 777 UsbHubCtrlClearPortFeature (HubIf->Device, Port, (UINT16)Map->Feature);\r
e237e7ae 778 }\r
779 }\r
780}\r
781\r
e237e7ae 782/**\r
8616fc4c 783 Function to set the port feature for non-root hub.\r
e237e7ae 784\r
8616fc4c 785 @param HubIf The hub interface.\r
786 @param Port The port of the hub.\r
787 @param Feature The feature of the port to set.\r
e237e7ae 788\r
8616fc4c 789 @retval EFI_SUCCESS The hub port feature is set.\r
790 @retval Others Failed to set the port feature.\r
e237e7ae 791\r
792**/\r
e237e7ae 793EFI_STATUS\r
794UsbHubSetPortFeature (\r
1436aea4
MK
795 IN USB_INTERFACE *HubIf,\r
796 IN UINT8 Port,\r
797 IN EFI_USB_PORT_FEATURE Feature\r
e237e7ae 798 )\r
799{\r
1436aea4 800 EFI_STATUS Status;\r
e237e7ae 801\r
1436aea4 802 Status = UsbHubCtrlSetPortFeature (HubIf->Device, Port, (UINT8)Feature);\r
e237e7ae 803\r
804 return Status;\r
805}\r
806\r
e237e7ae 807/**\r
8616fc4c 808 Interface function to clear the port feature for non-root hub.\r
e237e7ae 809\r
8616fc4c 810 @param HubIf The hub interface.\r
811 @param Port The port of the hub to clear feature for.\r
812 @param Feature The feature to clear.\r
e237e7ae 813\r
8616fc4c 814 @retval EFI_SUCCESS The port feature is cleared.\r
815 @retval Others Failed to clear the port feature.\r
e237e7ae 816\r
817**/\r
e237e7ae 818EFI_STATUS\r
819UsbHubClearPortFeature (\r
1436aea4
MK
820 IN USB_INTERFACE *HubIf,\r
821 IN UINT8 Port,\r
822 IN EFI_USB_PORT_FEATURE Feature\r
e237e7ae 823 )\r
824{\r
1436aea4 825 EFI_STATUS Status;\r
e237e7ae 826\r
1436aea4 827 Status = UsbHubCtrlClearPortFeature (HubIf->Device, Port, (UINT8)Feature);\r
e237e7ae 828\r
829 return Status;\r
830}\r
831\r
e237e7ae 832/**\r
d17371e8 833 Interface function to reset the port.\r
e237e7ae 834\r
8616fc4c 835 @param HubIf The hub interface.\r
836 @param Port The port to reset.\r
e237e7ae 837\r
8616fc4c 838 @retval EFI_SUCCESS The hub port is reset.\r
839 @retval EFI_TIMEOUT Failed to reset the port in time.\r
840 @retval Others Failed to reset the port.\r
e237e7ae 841\r
842**/\r
e237e7ae 843EFI_STATUS\r
844UsbHubResetPort (\r
1436aea4
MK
845 IN USB_INTERFACE *HubIf,\r
846 IN UINT8 Port\r
e237e7ae 847 )\r
848{\r
1436aea4
MK
849 EFI_USB_PORT_STATUS PortState;\r
850 UINTN Index;\r
851 EFI_STATUS Status;\r
e237e7ae 852\r
1436aea4 853 Status = UsbHubSetPortFeature (HubIf, Port, (EFI_USB_PORT_FEATURE)USB_HUB_PORT_RESET);\r
e237e7ae 854\r
855 if (EFI_ERROR (Status)) {\r
856 return Status;\r
857 }\r
858\r
859 //\r
71619ac2 860 // Drive the reset signal for worst 20ms. Check USB 2.0 Spec\r
e237e7ae 861 // section 7.1.7.5 for timing requirements.\r
862 //\r
41e8ff27 863 gBS->Stall (USB_SET_PORT_RESET_STALL);\r
e237e7ae 864\r
865 //\r
71619ac2 866 // Check USB_PORT_STAT_C_RESET bit to see if the resetting state is done.\r
e237e7ae 867 //\r
868 ZeroMem (&PortState, sizeof (EFI_USB_PORT_STATUS));\r
869\r
41e8ff27 870 for (Index = 0; Index < USB_WAIT_PORT_STS_CHANGE_LOOP; Index++) {\r
e237e7ae 871 Status = UsbHubGetPortStatus (HubIf, Port, &PortState);\r
872\r
127884c5
FT
873 if (EFI_ERROR (Status)) {\r
874 return Status;\r
875 }\r
876\r
e237e7ae 877 if (!EFI_ERROR (Status) &&\r
1436aea4
MK
878 USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_RESET))\r
879 {\r
71619ac2 880 gBS->Stall (USB_SET_PORT_RECOVERY_STALL);\r
e237e7ae 881 return EFI_SUCCESS;\r
882 }\r
883\r
41e8ff27 884 gBS->Stall (USB_WAIT_PORT_STS_CHANGE_STALL);\r
e237e7ae 885 }\r
886\r
887 return EFI_TIMEOUT;\r
888}\r
889\r
e237e7ae 890/**\r
8616fc4c 891 Release the hub's control of the interface.\r
e237e7ae 892\r
8616fc4c 893 @param HubIf The hub interface.\r
e237e7ae 894\r
8616fc4c 895 @retval EFI_SUCCESS The interface is release of hub control.\r
e237e7ae 896\r
897**/\r
e237e7ae 898EFI_STATUS\r
899UsbHubRelease (\r
1436aea4 900 IN USB_INTERFACE *HubIf\r
e237e7ae 901 )\r
902{\r
1436aea4
MK
903 EFI_USB_IO_PROTOCOL *UsbIo;\r
904 EFI_STATUS Status;\r
e237e7ae 905\r
906 UsbIo = &HubIf->UsbIo;\r
907 Status = UsbIo->UsbAsyncInterruptTransfer (\r
908 UsbIo,\r
909 HubIf->HubEp->Desc.EndpointAddress,\r
910 FALSE,\r
911 USB_HUB_POLL_INTERVAL,\r
912 0,\r
913 NULL,\r
914 0\r
915 );\r
916\r
917 if (EFI_ERROR (Status)) {\r
918 return Status;\r
919 }\r
920\r
921 gBS->CloseEvent (HubIf->HubNotify);\r
922\r
1436aea4
MK
923 HubIf->IsHub = FALSE;\r
924 HubIf->HubApi = NULL;\r
925 HubIf->HubEp = NULL;\r
926 HubIf->HubNotify = NULL;\r
e237e7ae 927\r
1436aea4 928 DEBUG ((DEBUG_INFO, "UsbHubRelease: hub device %d released\n", HubIf->Device->Address));\r
e237e7ae 929 return EFI_SUCCESS;\r
930}\r
931\r
e237e7ae 932/**\r
8616fc4c 933 Initialize the interface for root hub.\r
e237e7ae 934\r
8616fc4c 935 @param HubIf The root hub interface.\r
e237e7ae 936\r
d17371e8 937 @retval EFI_SUCCESS The interface is initialized for root hub.\r
8616fc4c 938 @retval Others Failed to initialize the hub.\r
e237e7ae 939\r
940**/\r
e237e7ae 941EFI_STATUS\r
942UsbRootHubInit (\r
1436aea4 943 IN USB_INTERFACE *HubIf\r
e237e7ae 944 )\r
945{\r
1436aea4
MK
946 EFI_STATUS Status;\r
947 UINT8 MaxSpeed;\r
948 UINT8 NumOfPort;\r
949 UINT8 Support64;\r
e237e7ae 950\r
951 Status = UsbHcGetCapability (HubIf->Device->Bus, &MaxSpeed, &NumOfPort, &Support64);\r
952\r
953 if (EFI_ERROR (Status)) {\r
954 return Status;\r
955 }\r
956\r
1436aea4
MK
957 DEBUG ((\r
958 DEBUG_INFO,\r
959 "UsbRootHubInit: root hub %p - max speed %d, %d ports\n",\r
960 HubIf,\r
961 MaxSpeed,\r
962 NumOfPort\r
963 ));\r
964\r
965 HubIf->IsHub = TRUE;\r
966 HubIf->HubApi = &mUsbRootHubApi;\r
967 HubIf->HubEp = NULL;\r
968 HubIf->MaxSpeed = MaxSpeed;\r
969 HubIf->NumOfPort = NumOfPort;\r
970 HubIf->HubNotify = NULL;\r
e237e7ae 971\r
972 //\r
973 // Create a timer to poll root hub ports periodically\r
974 //\r
975 Status = gBS->CreateEvent (\r
976 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
977 TPL_CALLBACK,\r
978 UsbRootHubEnumeration,\r
979 HubIf,\r
980 &HubIf->HubNotify\r
981 );\r
982\r
983 if (EFI_ERROR (Status)) {\r
984 return Status;\r
985 }\r
986\r
d2577026 987 //\r
988 // It should signal the event immediately here, or device detection\r
989 // by bus enumeration might be delayed by the timer interval.\r
990 //\r
991 gBS->SignalEvent (HubIf->HubNotify);\r
992\r
e237e7ae 993 Status = gBS->SetTimer (\r
994 HubIf->HubNotify,\r
995 TimerPeriodic,\r
996 USB_ROOTHUB_POLL_INTERVAL\r
997 );\r
998\r
999 if (EFI_ERROR (Status)) {\r
1000 gBS->CloseEvent (HubIf->HubNotify);\r
1001 }\r
1002\r
1003 return Status;\r
1004}\r
1005\r
e237e7ae 1006/**\r
1007 Get the port status. This function is required to\r
1008 ACK the port change bits although it will return\r
1009 the port changes in PortState. Bus enumeration code\r
1010 doesn't need to ACK the port change bits.\r
1011\r
8616fc4c 1012 @param HubIf The root hub interface.\r
1013 @param Port The root hub port to get the state.\r
1014 @param PortState Variable to return the port state.\r
e237e7ae 1015\r
8616fc4c 1016 @retval EFI_SUCCESS The port state is returned.\r
1017 @retval Others Failed to retrieve the port state.\r
e237e7ae 1018\r
1019**/\r
e237e7ae 1020EFI_STATUS\r
1021UsbRootHubGetPortStatus (\r
1436aea4
MK
1022 IN USB_INTERFACE *HubIf,\r
1023 IN UINT8 Port,\r
1024 OUT EFI_USB_PORT_STATUS *PortState\r
e237e7ae 1025 )\r
1026{\r
1436aea4
MK
1027 USB_BUS *Bus;\r
1028 EFI_STATUS Status;\r
e237e7ae 1029\r
1436aea4
MK
1030 Bus = HubIf->Device->Bus;\r
1031 Status = UsbHcGetRootHubPortStatus (Bus, Port, PortState);\r
e237e7ae 1032\r
1033 return Status;\r
1034}\r
1035\r
e237e7ae 1036/**\r
1037 Clear the port change status.\r
1038\r
8616fc4c 1039 @param HubIf The root hub interface.\r
1040 @param Port The root hub port.\r
e237e7ae 1041\r
e237e7ae 1042**/\r
e237e7ae 1043VOID\r
1044UsbRootHubClearPortChange (\r
1436aea4
MK
1045 IN USB_INTERFACE *HubIf,\r
1046 IN UINT8 Port\r
e237e7ae 1047 )\r
1048{\r
1049 EFI_USB_PORT_STATUS PortState;\r
1050 USB_CHANGE_FEATURE_MAP *Map;\r
1051 UINTN Index;\r
1052 EFI_STATUS Status;\r
1053\r
1054 Status = UsbRootHubGetPortStatus (HubIf, Port, &PortState);\r
1055\r
1056 if (EFI_ERROR (Status)) {\r
1057 return;\r
1058 }\r
1059\r
1060 //\r
1061 // OK, get the usb port status, now ACK the change bits.\r
1062 // Don't return error when failed to clear the change bits.\r
1063 // It may lead to extra port state report. USB bus should\r
1064 // be able to handle this.\r
1065 //\r
60a2d248 1066 for (Index = 0; Index < ARRAY_SIZE (mRootHubFeatureMap); Index++) {\r
e237e7ae 1067 Map = &mRootHubFeatureMap[Index];\r
1068\r
1069 if (USB_BIT_IS_SET (PortState.PortChangeStatus, Map->ChangedBit)) {\r
1436aea4 1070 UsbHcClearRootHubPortFeature (HubIf->Device->Bus, Port, (EFI_USB_PORT_FEATURE)Map->Feature);\r
e237e7ae 1071 }\r
1072 }\r
1073}\r
1074\r
e237e7ae 1075/**\r
8616fc4c 1076 Set the root hub port feature.\r
e237e7ae 1077\r
8616fc4c 1078 @param HubIf The Usb hub interface.\r
1079 @param Port The hub port.\r
1080 @param Feature The feature to set.\r
e237e7ae 1081\r
8616fc4c 1082 @retval EFI_SUCCESS The root hub port is set with the feature.\r
1083 @retval Others Failed to set the feature.\r
e237e7ae 1084\r
1085**/\r
e237e7ae 1086EFI_STATUS\r
1087UsbRootHubSetPortFeature (\r
1436aea4
MK
1088 IN USB_INTERFACE *HubIf,\r
1089 IN UINT8 Port,\r
1090 IN EFI_USB_PORT_FEATURE Feature\r
e237e7ae 1091 )\r
1092{\r
1436aea4 1093 EFI_STATUS Status;\r
e237e7ae 1094\r
1436aea4 1095 Status = UsbHcSetRootHubPortFeature (HubIf->Device->Bus, Port, Feature);\r
e237e7ae 1096\r
1097 return Status;\r
1098}\r
1099\r
e237e7ae 1100/**\r
8616fc4c 1101 Clear the root hub port feature.\r
e237e7ae 1102\r
8616fc4c 1103 @param HubIf The root hub interface.\r
1104 @param Port The root hub port.\r
1105 @param Feature The feature to clear.\r
e237e7ae 1106\r
8616fc4c 1107 @retval EFI_SUCCESS The root hub port is cleared of the feature.\r
1108 @retval Others Failed to clear the feature.\r
e237e7ae 1109\r
1110**/\r
e237e7ae 1111EFI_STATUS\r
1112UsbRootHubClearPortFeature (\r
1436aea4
MK
1113 IN USB_INTERFACE *HubIf,\r
1114 IN UINT8 Port,\r
1115 IN EFI_USB_PORT_FEATURE Feature\r
e237e7ae 1116 )\r
1117{\r
1436aea4 1118 EFI_STATUS Status;\r
e237e7ae 1119\r
1436aea4 1120 Status = UsbHcClearRootHubPortFeature (HubIf->Device->Bus, Port, Feature);\r
e237e7ae 1121\r
1122 return Status;\r
1123}\r
1124\r
e237e7ae 1125/**\r
d17371e8 1126 Interface function to reset the root hub port.\r
e237e7ae 1127\r
8616fc4c 1128 @param RootIf The root hub interface.\r
1129 @param Port The port to reset.\r
e237e7ae 1130\r
8616fc4c 1131 @retval EFI_SUCCESS The hub port is reset.\r
1132 @retval EFI_TIMEOUT Failed to reset the port in time.\r
1133 @retval EFI_NOT_FOUND The low/full speed device connected to high speed.\r
1134 root hub is released to the companion UHCI.\r
1135 @retval Others Failed to reset the port.\r
e237e7ae 1136\r
1137**/\r
e237e7ae 1138EFI_STATUS\r
1139UsbRootHubResetPort (\r
1436aea4
MK
1140 IN USB_INTERFACE *RootIf,\r
1141 IN UINT8 Port\r
e237e7ae 1142 )\r
1143{\r
1436aea4
MK
1144 USB_BUS *Bus;\r
1145 EFI_STATUS Status;\r
1146 EFI_USB_PORT_STATUS PortState;\r
1147 UINTN Index;\r
e237e7ae 1148\r
1149 //\r
1150 // Notice: although EHCI requires that ENABLED bit be cleared\r
1151 // when reset the port, we don't need to care that here. It\r
1152 // should be handled in the EHCI driver.\r
1153 //\r
1436aea4 1154 Bus = RootIf->Device->Bus;\r
c3f44a77 1155\r
1436aea4 1156 Status = UsbHcSetRootHubPortFeature (Bus, Port, EfiUsbPortReset);\r
e237e7ae 1157\r
1158 if (EFI_ERROR (Status)) {\r
1436aea4 1159 DEBUG ((DEBUG_ERROR, "UsbRootHubResetPort: failed to start reset on port %d\n", Port));\r
e237e7ae 1160 return Status;\r
1161 }\r
1162\r
1163 //\r
1164 // Drive the reset signal for at least 50ms. Check USB 2.0 Spec\r
1165 // section 7.1.7.5 for timing requirements.\r
1166 //\r
41e8ff27 1167 gBS->Stall (USB_SET_ROOT_PORT_RESET_STALL);\r
e237e7ae 1168\r
1169 Status = UsbHcClearRootHubPortFeature (Bus, Port, EfiUsbPortReset);\r
1170\r
1171 if (EFI_ERROR (Status)) {\r
1436aea4 1172 DEBUG ((DEBUG_ERROR, "UsbRootHubResetPort: failed to clear reset on port %d\n", Port));\r
e237e7ae 1173 return Status;\r
1174 }\r
1175\r
41e8ff27 1176 gBS->Stall (USB_CLR_ROOT_PORT_RESET_STALL);\r
e237e7ae 1177\r
1178 //\r
1179 // USB host controller won't clear the RESET bit until\r
1180 // reset is actually finished.\r
1181 //\r
1182 ZeroMem (&PortState, sizeof (EFI_USB_PORT_STATUS));\r
1183\r
41e8ff27 1184 for (Index = 0; Index < USB_WAIT_PORT_STS_CHANGE_LOOP; Index++) {\r
e237e7ae 1185 Status = UsbHcGetRootHubPortStatus (Bus, Port, &PortState);\r
1186\r
1187 if (EFI_ERROR (Status)) {\r
1188 return Status;\r
1189 }\r
1190\r
1191 if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_RESET)) {\r
1192 break;\r
1193 }\r
1194\r
41e8ff27 1195 gBS->Stall (USB_WAIT_PORT_STS_CHANGE_STALL);\r
e237e7ae 1196 }\r
1197\r
41e8ff27 1198 if (Index == USB_WAIT_PORT_STS_CHANGE_LOOP) {\r
87000d77 1199 DEBUG ((DEBUG_ERROR, "UsbRootHubResetPort: reset not finished in time on port %d\n", Port));\r
e237e7ae 1200 return EFI_TIMEOUT;\r
1201 }\r
1202\r
1203 if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_ENABLE)) {\r
1204 //\r
1205 // OK, the port is reset. If root hub is of high speed and\r
1206 // the device is of low/full speed, release the ownership to\r
1207 // companion UHCI. If root hub is of full speed, it won't\r
1208 // automatically enable the port, we need to enable it manually.\r
1209 //\r
1210 if (RootIf->MaxSpeed == EFI_USB_SPEED_HIGH) {\r
1436aea4 1211 DEBUG ((DEBUG_ERROR, "UsbRootHubResetPort: release low/full speed device (%d) to UHCI\n", Port));\r
e237e7ae 1212\r
1213 UsbRootHubSetPortFeature (RootIf, Port, EfiUsbPortOwner);\r
1214 return EFI_NOT_FOUND;\r
e237e7ae 1215 } else {\r
e237e7ae 1216 Status = UsbRootHubSetPortFeature (RootIf, Port, EfiUsbPortEnable);\r
1217\r
1218 if (EFI_ERROR (Status)) {\r
1436aea4 1219 DEBUG ((DEBUG_ERROR, "UsbRootHubResetPort: failed to enable port %d for UHCI\n", Port));\r
e237e7ae 1220 return Status;\r
1221 }\r
1222\r
41e8ff27 1223 gBS->Stall (USB_SET_ROOT_PORT_ENABLE_STALL);\r
e237e7ae 1224 }\r
1225 }\r
1226\r
1227 return EFI_SUCCESS;\r
1228}\r
1229\r
e237e7ae 1230/**\r
8616fc4c 1231 Release the root hub's control of the interface.\r
e237e7ae 1232\r
8616fc4c 1233 @param HubIf The root hub interface.\r
e237e7ae 1234\r
1235 @retval EFI_SUCCESS The root hub's control of the interface is\r
1236 released.\r
1237\r
1238**/\r
e237e7ae 1239EFI_STATUS\r
1240UsbRootHubRelease (\r
1436aea4 1241 IN USB_INTERFACE *HubIf\r
e237e7ae 1242 )\r
1243{\r
1436aea4 1244 DEBUG ((DEBUG_INFO, "UsbRootHubRelease: root hub released for hub %p\n", HubIf));\r
e237e7ae 1245\r
1246 gBS->SetTimer (HubIf->HubNotify, TimerCancel, USB_ROOTHUB_POLL_INTERVAL);\r
1247 gBS->CloseEvent (HubIf->HubNotify);\r
1248\r
1249 return EFI_SUCCESS;\r
1250}\r
1251\r
1436aea4 1252USB_HUB_API mUsbHubApi = {\r
e237e7ae 1253 UsbHubInit,\r
1254 UsbHubGetPortStatus,\r
1255 UsbHubClearPortChange,\r
1256 UsbHubSetPortFeature,\r
1257 UsbHubClearPortFeature,\r
1258 UsbHubResetPort,\r
1259 UsbHubRelease\r
1260};\r
1261\r
1436aea4 1262USB_HUB_API mUsbRootHubApi = {\r
e237e7ae 1263 UsbRootHubInit,\r
1264 UsbRootHubGetPortStatus,\r
1265 UsbRootHubClearPortChange,\r
1266 UsbRootHubSetPortFeature,\r
1267 UsbRootHubClearPortFeature,\r
1268 UsbRootHubResetPort,\r
1269 UsbRootHubRelease\r
1270};\r