2 The XHCI controller driver.
4 Copyright (c) 2011 - 2022, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
12 // Two arrays used to translate the XHCI port state (change)
13 // to the UEFI protocol's port state (change).
15 USB_PORT_STATE_MAP mUsbPortStateMap
[] = {
16 { XHC_PORTSC_CCS
, USB_PORT_STAT_CONNECTION
},
17 { XHC_PORTSC_PED
, USB_PORT_STAT_ENABLE
},
18 { XHC_PORTSC_OCA
, USB_PORT_STAT_OVERCURRENT
},
19 { XHC_PORTSC_RESET
, USB_PORT_STAT_RESET
}
22 USB_PORT_STATE_MAP mUsbPortChangeMap
[] = {
23 { XHC_PORTSC_CSC
, USB_PORT_STAT_C_CONNECTION
},
24 { XHC_PORTSC_PEC
, USB_PORT_STAT_C_ENABLE
},
25 { XHC_PORTSC_OCC
, USB_PORT_STAT_C_OVERCURRENT
},
26 { XHC_PORTSC_PRC
, USB_PORT_STAT_C_RESET
}
29 USB_CLEAR_PORT_MAP mUsbClearPortChangeMap
[] = {
30 { XHC_PORTSC_CSC
, EfiUsbPortConnectChange
},
31 { XHC_PORTSC_PEC
, EfiUsbPortEnableChange
},
32 { XHC_PORTSC_OCC
, EfiUsbPortOverCurrentChange
},
33 { XHC_PORTSC_PRC
, EfiUsbPortResetChange
}
36 USB_PORT_STATE_MAP mUsbHubPortStateMap
[] = {
37 { XHC_HUB_PORTSC_CCS
, USB_PORT_STAT_CONNECTION
},
38 { XHC_HUB_PORTSC_PED
, USB_PORT_STAT_ENABLE
},
39 { XHC_HUB_PORTSC_OCA
, USB_PORT_STAT_OVERCURRENT
},
40 { XHC_HUB_PORTSC_RESET
, USB_PORT_STAT_RESET
}
43 USB_PORT_STATE_MAP mUsbHubPortChangeMap
[] = {
44 { XHC_HUB_PORTSC_CSC
, USB_PORT_STAT_C_CONNECTION
},
45 { XHC_HUB_PORTSC_PEC
, USB_PORT_STAT_C_ENABLE
},
46 { XHC_HUB_PORTSC_OCC
, USB_PORT_STAT_C_OVERCURRENT
},
47 { XHC_HUB_PORTSC_PRC
, USB_PORT_STAT_C_RESET
}
50 USB_CLEAR_PORT_MAP mUsbHubClearPortChangeMap
[] = {
51 { XHC_HUB_PORTSC_CSC
, EfiUsbPortConnectChange
},
52 { XHC_HUB_PORTSC_PEC
, EfiUsbPortEnableChange
},
53 { XHC_HUB_PORTSC_OCC
, EfiUsbPortOverCurrentChange
},
54 { XHC_HUB_PORTSC_PRC
, EfiUsbPortResetChange
},
55 { XHC_HUB_PORTSC_BHRC
, Usb3PortBHPortResetChange
}
58 EFI_DRIVER_BINDING_PROTOCOL gXhciDriverBinding
= {
59 XhcDriverBindingSupported
,
60 XhcDriverBindingStart
,
68 // Template for Xhci's Usb2 Host Controller Protocol Instance.
70 EFI_USB2_HC_PROTOCOL gXhciUsb2HcTemplate
= {
77 XhcAsyncInterruptTransfer
,
78 XhcSyncInterruptTransfer
,
79 XhcIsochronousTransfer
,
80 XhcAsyncIsochronousTransfer
,
81 XhcGetRootHubPortStatus
,
82 XhcSetRootHubPortFeature
,
83 XhcClearRootHubPortFeature
,
89 Retrieves the capability of root hub ports.
91 @param This The EFI_USB2_HC_PROTOCOL instance.
92 @param MaxSpeed Max speed supported by the controller.
93 @param PortNumber Number of the root hub ports.
94 @param Is64BitCapable Whether the controller supports 64-bit memory
97 @retval EFI_SUCCESS Host controller capability were retrieved successfully.
98 @retval EFI_INVALID_PARAMETER Either of the three capability pointer is NULL.
104 IN EFI_USB2_HC_PROTOCOL
*This
,
106 OUT UINT8
*PortNumber
,
107 OUT UINT8
*Is64BitCapable
110 USB_XHCI_INSTANCE
*Xhc
;
113 if ((MaxSpeed
== NULL
) || (PortNumber
== NULL
) || (Is64BitCapable
== NULL
)) {
114 return EFI_INVALID_PARAMETER
;
117 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
119 Xhc
= XHC_FROM_THIS (This
);
120 *MaxSpeed
= EFI_USB_SPEED_SUPER
;
121 *PortNumber
= (UINT8
)(Xhc
->HcSParams1
.Data
.MaxPorts
);
122 *Is64BitCapable
= (UINT8
)Xhc
->Support64BitDma
;
123 DEBUG ((DEBUG_INFO
, "XhcGetCapability: %d ports, 64 bit %d\n", *PortNumber
, *Is64BitCapable
));
125 gBS
->RestoreTPL (OldTpl
);
131 Provides software reset for the USB host controller.
133 @param This This EFI_USB2_HC_PROTOCOL instance.
134 @param Attributes A bit mask of the reset operation to perform.
136 @retval EFI_SUCCESS The reset operation succeeded.
137 @retval EFI_INVALID_PARAMETER Attributes is not valid.
138 @retval EFI_UNSUPPOURTED The type of reset specified by Attributes is
139 not currently supported by the host controller.
140 @retval EFI_DEVICE_ERROR Host controller isn't halted to reset.
146 IN EFI_USB2_HC_PROTOCOL
*This
,
150 USB_XHCI_INSTANCE
*Xhc
;
154 Xhc
= XHC_FROM_THIS (This
);
156 if (Xhc
->DevicePath
!= NULL
) {
158 // Report Status Code to indicate reset happens
160 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
162 (EFI_IO_BUS_USB
| EFI_IOB_PC_RESET
),
167 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
169 switch (Attributes
) {
170 case EFI_USB_HC_RESET_GLOBAL
:
172 // Flow through, same behavior as Host Controller Reset
174 case EFI_USB_HC_RESET_HOST_CONTROLLER
:
175 if ((Xhc
->DebugCapSupOffset
!= 0xFFFFFFFF) && ((XhcReadExtCapReg (Xhc
, Xhc
->DebugCapSupOffset
) & 0xFF) == XHC_CAP_USB_DEBUG
) &&
176 ((XhcReadExtCapReg (Xhc
, Xhc
->DebugCapSupOffset
+ XHC_DC_DCCTRL
) & BIT0
) != 0))
178 Status
= EFI_SUCCESS
;
183 // Host Controller must be Halt when Reset it
185 if (!XhcIsHalt (Xhc
)) {
186 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
188 if (EFI_ERROR (Status
)) {
189 Status
= EFI_DEVICE_ERROR
;
194 Status
= XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
195 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
197 if (EFI_ERROR (Status
)) {
202 // Clean up the asynchronous transfers, currently only
203 // interrupt supports asynchronous operation.
205 XhciDelAllAsyncIntTransfers (Xhc
);
211 case EFI_USB_HC_RESET_GLOBAL_WITH_DEBUG
:
212 case EFI_USB_HC_RESET_HOST_WITH_DEBUG
:
213 Status
= EFI_UNSUPPORTED
;
217 Status
= EFI_INVALID_PARAMETER
;
221 DEBUG ((DEBUG_INFO
, "XhcReset: status %r\n", Status
));
222 gBS
->RestoreTPL (OldTpl
);
228 Retrieve the current state of the USB host controller.
230 @param This This EFI_USB2_HC_PROTOCOL instance.
231 @param State Variable to return the current host controller
234 @retval EFI_SUCCESS Host controller state was returned in State.
235 @retval EFI_INVALID_PARAMETER State is NULL.
236 @retval EFI_DEVICE_ERROR An error was encountered while attempting to
237 retrieve the host controller's current state.
243 IN EFI_USB2_HC_PROTOCOL
*This
,
244 OUT EFI_USB_HC_STATE
*State
247 USB_XHCI_INSTANCE
*Xhc
;
251 return EFI_INVALID_PARAMETER
;
254 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
256 Xhc
= XHC_FROM_THIS (This
);
258 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
259 *State
= EfiUsbHcStateHalt
;
261 *State
= EfiUsbHcStateOperational
;
264 DEBUG ((DEBUG_INFO
, "XhcGetState: current state %d\n", *State
));
265 gBS
->RestoreTPL (OldTpl
);
271 Sets the USB host controller to a specific state.
273 @param This This EFI_USB2_HC_PROTOCOL instance.
274 @param State The state of the host controller that will be set.
276 @retval EFI_SUCCESS The USB host controller was successfully placed
277 in the state specified by State.
278 @retval EFI_INVALID_PARAMETER State is invalid.
279 @retval EFI_DEVICE_ERROR Failed to set the state due to device error.
285 IN EFI_USB2_HC_PROTOCOL
*This
,
286 IN EFI_USB_HC_STATE State
289 USB_XHCI_INSTANCE
*Xhc
;
291 EFI_USB_HC_STATE CurState
;
294 Status
= XhcGetState (This
, &CurState
);
296 if (EFI_ERROR (Status
)) {
297 return EFI_DEVICE_ERROR
;
300 if (CurState
== State
) {
304 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
306 Xhc
= XHC_FROM_THIS (This
);
309 case EfiUsbHcStateHalt
:
310 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
313 case EfiUsbHcStateOperational
:
314 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HSE
)) {
315 Status
= EFI_DEVICE_ERROR
;
320 // Software must not write a one to this field unless the host controller
321 // is in the Halted state. Doing so will yield undefined results.
322 // refers to Spec[XHCI1.0-2.3.1]
324 if (!XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
325 Status
= EFI_DEVICE_ERROR
;
329 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
332 case EfiUsbHcStateSuspend
:
333 Status
= EFI_UNSUPPORTED
;
337 Status
= EFI_INVALID_PARAMETER
;
340 DEBUG ((DEBUG_INFO
, "XhcSetState: status %r\n", Status
));
341 gBS
->RestoreTPL (OldTpl
);
347 Retrieves the current status of a USB root hub port.
349 @param This This EFI_USB2_HC_PROTOCOL instance.
350 @param PortNumber The root hub port to retrieve the state from.
351 This value is zero-based.
352 @param PortStatus Variable to receive the port state.
354 @retval EFI_SUCCESS The status of the USB root hub port specified.
355 by PortNumber was returned in PortStatus.
356 @retval EFI_INVALID_PARAMETER PortNumber is invalid.
357 @retval EFI_DEVICE_ERROR Can't read register.
362 XhcGetRootHubPortStatus (
363 IN EFI_USB2_HC_PROTOCOL
*This
,
365 OUT EFI_USB_PORT_STATUS
*PortStatus
368 USB_XHCI_INSTANCE
*Xhc
;
376 USB_DEV_ROUTE ParentRouteChart
;
379 if (PortStatus
== NULL
) {
380 return EFI_INVALID_PARAMETER
;
383 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
385 Xhc
= XHC_FROM_THIS (This
);
386 Status
= EFI_SUCCESS
;
388 TotalPort
= Xhc
->HcSParams1
.Data
.MaxPorts
;
390 if (PortNumber
>= TotalPort
) {
391 Status
= EFI_INVALID_PARAMETER
;
395 Offset
= (UINT32
)(XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
396 PortStatus
->PortStatus
= 0;
397 PortStatus
->PortChangeStatus
= 0;
399 State
= XhcReadOpReg (Xhc
, Offset
);
401 PortSpeed
= (State
& XHC_PORTSC_PS
) >> 10;
404 // According to XHCI 1.1 spec November 2017,
405 // Section 7.2 xHCI Support Protocol Capability
408 PortStatus
->PortStatus
= XhcCheckUsbPortSpeedUsedPsic (Xhc
, PortSpeed
, PortNumber
);
409 // If no match found in ext cap reg, fall back to PORTSC
410 if (PortStatus
->PortStatus
== 0) {
412 // According to XHCI 1.1 spec November 2017,
413 // bit 10~13 of the root port status register identifies the speed of the attached device.
417 PortStatus
->PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
421 PortStatus
->PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
426 PortStatus
->PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
436 // Convert the XHCI port/port change state to UEFI status
438 MapSize
= sizeof (mUsbPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
440 for (Index
= 0; Index
< MapSize
; Index
++) {
441 if (XHC_BIT_IS_SET (State
, mUsbPortStateMap
[Index
].HwState
)) {
442 PortStatus
->PortStatus
= (UINT16
)(PortStatus
->PortStatus
| mUsbPortStateMap
[Index
].UefiState
);
447 // Bit5~8 reflects its current link state.
449 if ((State
& XHC_PORTSC_PLS
) >> 5 == 3) {
450 PortStatus
->PortStatus
|= USB_PORT_STAT_SUSPEND
;
453 MapSize
= sizeof (mUsbPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
455 for (Index
= 0; Index
< MapSize
; Index
++) {
456 if (XHC_BIT_IS_SET (State
, mUsbPortChangeMap
[Index
].HwState
)) {
457 PortStatus
->PortChangeStatus
= (UINT16
)(PortStatus
->PortChangeStatus
| mUsbPortChangeMap
[Index
].UefiState
);
461 MapSize
= sizeof (mUsbClearPortChangeMap
) / sizeof (USB_CLEAR_PORT_MAP
);
463 for (Index
= 0; Index
< MapSize
; Index
++) {
464 if (XHC_BIT_IS_SET (State
, mUsbClearPortChangeMap
[Index
].HwState
)) {
465 XhcClearRootHubPortFeature (This
, PortNumber
, (EFI_USB_PORT_FEATURE
)mUsbClearPortChangeMap
[Index
].Selector
);
470 // Poll the root port status register to enable/disable corresponding device slot if there is a device attached/detached.
471 // For those devices behind hub, we get its attach/detach event by hooking Get_Port_Status request at control transfer for those hub.
473 ParentRouteChart
.Dword
= 0;
474 XhcPollPortStatusChange (Xhc
, ParentRouteChart
, PortNumber
, PortStatus
);
477 gBS
->RestoreTPL (OldTpl
);
482 Sets a feature for the specified root hub port.
484 @param This This EFI_USB2_HC_PROTOCOL instance.
485 @param PortNumber Root hub port to set.
486 @param PortFeature Feature to set.
488 @retval EFI_SUCCESS The feature specified by PortFeature was set.
489 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
490 @retval EFI_DEVICE_ERROR Can't read register.
495 XhcSetRootHubPortFeature (
496 IN EFI_USB2_HC_PROTOCOL
*This
,
498 IN EFI_USB_PORT_FEATURE PortFeature
501 USB_XHCI_INSTANCE
*Xhc
;
508 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
510 Xhc
= XHC_FROM_THIS (This
);
511 Status
= EFI_SUCCESS
;
513 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
515 if (PortNumber
>= TotalPort
) {
516 Status
= EFI_INVALID_PARAMETER
;
520 Offset
= (UINT32
)(XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
521 State
= XhcReadOpReg (Xhc
, Offset
);
524 // Mask off the port status change bits, these bits are
527 State
&= ~(BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
529 switch (PortFeature
) {
530 case EfiUsbPortEnable
:
532 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
533 // A port may be disabled by software writing a '1' to this flag.
535 Status
= EFI_SUCCESS
;
538 case EfiUsbPortSuspend
:
539 State
|= XHC_PORTSC_LWS
;
540 XhcWriteOpReg (Xhc
, Offset
, State
);
541 State
&= ~XHC_PORTSC_PLS
;
543 XhcWriteOpReg (Xhc
, Offset
, State
);
546 case EfiUsbPortReset
:
547 DEBUG ((DEBUG_INFO
, "XhcUsbPortReset!\n"));
549 // Make sure Host Controller not halt before reset it
551 if (XhcIsHalt (Xhc
)) {
552 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
554 if (EFI_ERROR (Status
)) {
555 DEBUG ((DEBUG_INFO
, "XhcSetRootHubPortFeature :failed to start HC - %r\n", Status
));
561 // 4.3.1 Resetting a Root Hub Port
562 // 1) Write the PORTSC register with the Port Reset (PR) bit set to '1'.
564 State
|= XHC_PORTSC_RESET
;
565 XhcWriteOpReg (Xhc
, Offset
, State
);
566 XhcWaitOpRegBit (Xhc
, Offset
, XHC_PORTSC_PRC
, TRUE
, XHC_GENERIC_TIMEOUT
);
569 case EfiUsbPortPower
:
571 // Not supported, ignore the operation
573 Status
= EFI_SUCCESS
;
576 case EfiUsbPortOwner
:
578 // XHCI root hub port don't has the owner bit, ignore the operation
580 Status
= EFI_SUCCESS
;
584 Status
= EFI_INVALID_PARAMETER
;
588 DEBUG ((DEBUG_INFO
, "XhcSetRootHubPortFeature: status %r\n", Status
));
589 gBS
->RestoreTPL (OldTpl
);
595 Clears a feature for the specified root hub port.
597 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
598 @param PortNumber Specifies the root hub port whose feature is
599 requested to be cleared.
600 @param PortFeature Indicates the feature selector associated with the
601 feature clear request.
603 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
604 for the USB root hub port specified by PortNumber.
605 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
606 @retval EFI_DEVICE_ERROR Can't read register.
611 XhcClearRootHubPortFeature (
612 IN EFI_USB2_HC_PROTOCOL
*This
,
614 IN EFI_USB_PORT_FEATURE PortFeature
617 USB_XHCI_INSTANCE
*Xhc
;
624 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
626 Xhc
= XHC_FROM_THIS (This
);
627 Status
= EFI_SUCCESS
;
629 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
631 if (PortNumber
>= TotalPort
) {
632 Status
= EFI_INVALID_PARAMETER
;
636 Offset
= XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
);
639 // Mask off the port status change bits, these bits are
642 State
= XhcReadOpReg (Xhc
, Offset
);
643 State
&= ~(BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
645 switch (PortFeature
) {
646 case EfiUsbPortEnable
:
648 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
649 // A port may be disabled by software writing a '1' to this flag.
651 State
|= XHC_PORTSC_PED
;
652 State
&= ~XHC_PORTSC_RESET
;
653 XhcWriteOpReg (Xhc
, Offset
, State
);
656 case EfiUsbPortSuspend
:
657 State
|= XHC_PORTSC_LWS
;
658 XhcWriteOpReg (Xhc
, Offset
, State
);
659 State
&= ~XHC_PORTSC_PLS
;
660 XhcWriteOpReg (Xhc
, Offset
, State
);
663 case EfiUsbPortReset
:
665 // PORTSC_RESET BIT(4) bit is RW1S attribute, which means Write-1-to-set status:
666 // Register bits indicate status when read, a clear bit may be set by
667 // writing a '1'. Writing a '0' to RW1S bits has no effect.
671 case EfiUsbPortOwner
:
673 // XHCI root hub port don't has the owner bit, ignore the operation
677 case EfiUsbPortConnectChange
:
679 // Clear connect status change
681 State
|= XHC_PORTSC_CSC
;
682 XhcWriteOpReg (Xhc
, Offset
, State
);
685 case EfiUsbPortEnableChange
:
687 // Clear enable status change
689 State
|= XHC_PORTSC_PEC
;
690 XhcWriteOpReg (Xhc
, Offset
, State
);
693 case EfiUsbPortOverCurrentChange
:
695 // Clear PortOverCurrent change
697 State
|= XHC_PORTSC_OCC
;
698 XhcWriteOpReg (Xhc
, Offset
, State
);
701 case EfiUsbPortResetChange
:
703 // Clear Port Reset change
705 State
|= XHC_PORTSC_PRC
;
706 XhcWriteOpReg (Xhc
, Offset
, State
);
709 case EfiUsbPortPower
:
710 case EfiUsbPortSuspendChange
:
712 // Not supported or not related operation
717 Status
= EFI_INVALID_PARAMETER
;
722 DEBUG ((DEBUG_INFO
, "XhcClearRootHubPortFeature: status %r\n", Status
));
723 gBS
->RestoreTPL (OldTpl
);
729 Submits a new transaction to a target USB device.
731 @param Xhc The XHCI Instance.
732 @param DeviceAddress The target device address.
733 @param EndPointAddress Endpoint number and its direction encoded in bit 7
734 @param DeviceSpeed Target device speed.
735 @param MaximumPacketLength Maximum packet size the default control transfer
736 endpoint is capable of sending or receiving.
737 @param Type The transaction type.
738 @param Request USB device request to send.
739 @param Data Data buffer to be transmitted or received from USB
741 @param DataLength The size (in bytes) of the data buffer.
742 @param Timeout Indicates the maximum timeout, in millisecond.
743 @param TransferResult Return the result of this control transfer.
745 @retval EFI_SUCCESS Transfer was completed successfully.
746 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
747 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
748 @retval EFI_TIMEOUT Transfer failed due to timeout.
749 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
753 IN USB_XHCI_INSTANCE
*Xhc
,
754 IN UINT8 DeviceAddress
,
755 IN UINT8 EndPointAddress
,
756 IN UINT8 DeviceSpeed
,
757 IN UINTN MaximumPacketLength
,
759 IN EFI_USB_DEVICE_REQUEST
*Request
,
761 IN OUT UINTN
*DataLength
,
763 OUT UINT32
*TransferResult
767 EFI_STATUS RecoveryStatus
;
770 ASSERT ((Type
== XHC_CTRL_TRANSFER
) || (Type
== XHC_BULK_TRANSFER
) || (Type
== XHC_INT_TRANSFER_SYNC
));
786 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: failed to create URB!\n", Type
));
787 return EFI_OUT_OF_RESOURCES
;
790 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
792 if (Status
== EFI_TIMEOUT
) {
794 // The transfer timed out. Abort the transfer by dequeueing of the TD.
796 RecoveryStatus
= XhcDequeueTrbFromEndpoint (Xhc
, Urb
);
797 if (RecoveryStatus
== EFI_ALREADY_STARTED
) {
799 // The URB is finished just before stopping endpoint.
800 // Change returning status from EFI_TIMEOUT to EFI_SUCCESS.
802 ASSERT (Urb
->Result
== EFI_USB_NOERROR
);
803 Status
= EFI_SUCCESS
;
804 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: pending URB is finished, Length = %d.\n", Type
, Urb
->Completed
));
805 } else if (EFI_ERROR (RecoveryStatus
)) {
806 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: XhcDequeueTrbFromEndpoint failed!\n", Type
));
810 *TransferResult
= Urb
->Result
;
811 *DataLength
= Urb
->Completed
;
813 if ((*TransferResult
== EFI_USB_ERR_STALL
) || (*TransferResult
== EFI_USB_ERR_BABBLE
)) {
814 ASSERT (Status
== EFI_DEVICE_ERROR
);
815 RecoveryStatus
= XhcRecoverHaltedEndpoint (Xhc
, Urb
);
816 if (EFI_ERROR (RecoveryStatus
)) {
817 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: XhcRecoverHaltedEndpoint failed!\n", Type
));
821 Xhc
->PciIo
->Flush (Xhc
->PciIo
);
822 XhcFreeUrb (Xhc
, Urb
);
827 Submits control transfer to a target USB device.
829 @param This This EFI_USB2_HC_PROTOCOL instance.
830 @param DeviceAddress The target device address.
831 @param DeviceSpeed Target device speed.
832 @param MaximumPacketLength Maximum packet size the default control transfer
833 endpoint is capable of sending or receiving.
834 @param Request USB device request to send.
835 @param TransferDirection Specifies the data direction for the data stage
836 @param Data Data buffer to be transmitted or received from USB
838 @param DataLength The size (in bytes) of the data buffer.
839 @param Timeout Indicates the maximum timeout, in millisecond.
840 @param Translator Transaction translator to be used by this device.
841 @param TransferResult Return the result of this control transfer.
843 @retval EFI_SUCCESS Transfer was completed successfully.
844 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
845 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
846 @retval EFI_TIMEOUT Transfer failed due to timeout.
847 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
853 IN EFI_USB2_HC_PROTOCOL
*This
,
854 IN UINT8 DeviceAddress
,
855 IN UINT8 DeviceSpeed
,
856 IN UINTN MaximumPacketLength
,
857 IN EFI_USB_DEVICE_REQUEST
*Request
,
858 IN EFI_USB_DATA_DIRECTION TransferDirection
,
860 IN OUT UINTN
*DataLength
,
862 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
863 OUT UINT32
*TransferResult
866 USB_XHCI_INSTANCE
*Xhc
;
869 UINT8 DescriptorType
;
874 EFI_USB_HUB_DESCRIPTOR
*HubDesc
;
878 EFI_USB_PORT_STATUS PortStatus
;
880 EFI_USB_DEVICE_REQUEST ClearPortRequest
;
884 // Validate parameters
886 if ((Request
== NULL
) || (TransferResult
== NULL
)) {
887 return EFI_INVALID_PARAMETER
;
890 if ((TransferDirection
!= EfiUsbDataIn
) &&
891 (TransferDirection
!= EfiUsbDataOut
) &&
892 (TransferDirection
!= EfiUsbNoData
))
894 return EFI_INVALID_PARAMETER
;
897 if ((TransferDirection
== EfiUsbNoData
) &&
898 ((Data
!= NULL
) || (*DataLength
!= 0)))
900 return EFI_INVALID_PARAMETER
;
903 if ((TransferDirection
!= EfiUsbNoData
) &&
904 ((Data
== NULL
) || (*DataLength
== 0)))
906 return EFI_INVALID_PARAMETER
;
909 if ((MaximumPacketLength
!= 8) && (MaximumPacketLength
!= 16) &&
910 (MaximumPacketLength
!= 32) && (MaximumPacketLength
!= 64) &&
911 (MaximumPacketLength
!= 512)
914 return EFI_INVALID_PARAMETER
;
917 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) {
918 return EFI_INVALID_PARAMETER
;
921 if ((DeviceSpeed
== EFI_USB_SPEED_SUPER
) && (MaximumPacketLength
!= 512)) {
922 return EFI_INVALID_PARAMETER
;
925 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
927 Xhc
= XHC_FROM_THIS (This
);
929 Status
= EFI_DEVICE_ERROR
;
930 *TransferResult
= EFI_USB_ERR_SYSTEM
;
933 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
934 DEBUG ((DEBUG_ERROR
, "XhcControlTransfer: HC halted at entrance\n"));
939 // Check if the device is still enabled before every transaction.
941 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
947 // Hook the Set_Address request from UsbBus.
948 // According to XHCI 1.0 spec, the Set_Address request is replaced by XHCI's Address_Device cmd.
950 if ((Request
->Request
== USB_REQ_SET_ADDRESS
) &&
951 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
)))
954 // Reset the BusDevAddr field of all disabled entries in UsbDevContext array firstly.
955 // This way is used to clean the history to avoid using wrong device address by XhcAsyncInterruptTransfer().
957 for (Index
= 0; Index
< 255; Index
++) {
958 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
&&
959 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0) &&
960 (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== (UINT8
)Request
->Value
))
962 Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
= 0;
966 if (Xhc
->UsbDevContext
[SlotId
].XhciDevAddr
== 0) {
967 Status
= EFI_DEVICE_ERROR
;
972 // The actual device address has been assigned by XHCI during initializing the device slot.
973 // So we just need establish the mapping relationship between the device address requested from UsbBus
974 // and the actual device address assigned by XHCI. The the following invocations through EFI_USB2_HC_PROTOCOL interface
975 // can find out the actual device address by it.
977 Xhc
->UsbDevContext
[SlotId
].BusDevAddr
= (UINT8
)Request
->Value
;
978 Status
= EFI_SUCCESS
;
983 // Create a new URB, insert it into the asynchronous
984 // schedule list, then poll the execution status.
985 // Note that we encode the direction in address although default control
986 // endpoint is bidirectional. XhcCreateUrb expects this
987 // combination of Ep addr and its direction.
989 Endpoint
= (UINT8
)(0 | ((TransferDirection
== EfiUsbDataIn
) ? 0x80 : 0));
990 Status
= XhcTransfer (
1004 if (EFI_ERROR (Status
)) {
1009 // Hook Get_Descriptor request from UsbBus as we need evaluate context and configure endpoint.
1010 // Hook Get_Status request form UsbBus as we need trace device attach/detach event happened at hub.
1011 // Hook Set_Config request from UsbBus as we need configure device endpoint.
1013 if ((Request
->Request
== USB_REQ_GET_DESCRIPTOR
) &&
1014 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
)) ||
1015 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_DEVICE
)))))
1017 DescriptorType
= (UINT8
)(Request
->Value
>> 8);
1018 if ((DescriptorType
== USB_DESC_TYPE_DEVICE
) && ((*DataLength
== sizeof (EFI_USB_DEVICE_DESCRIPTOR
)) || ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (*DataLength
== 8)))) {
1019 ASSERT (Data
!= NULL
);
1021 // Store a copy of device scriptor as hub device need this info to configure endpoint.
1023 CopyMem (&Xhc
->UsbDevContext
[SlotId
].DevDesc
, Data
, *DataLength
);
1024 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.BcdUSB
>= 0x0300) {
1026 // If it's a usb3.0 device, then its max packet size is a 2^n.
1028 MaxPacket0
= 1 << Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
1030 MaxPacket0
= Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
1033 Xhc
->UsbDevContext
[SlotId
].ConfDesc
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
* sizeof (EFI_USB_CONFIG_DESCRIPTOR
*));
1034 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1035 Status
= XhcEvaluateContext (Xhc
, SlotId
, MaxPacket0
);
1037 Status
= XhcEvaluateContext64 (Xhc
, SlotId
, MaxPacket0
);
1039 } else if (DescriptorType
== USB_DESC_TYPE_CONFIG
) {
1040 ASSERT (Data
!= NULL
);
1041 if (*DataLength
== ((UINT16
*)Data
)[1]) {
1043 // Get configuration value from request, Store the configuration descriptor for Configure_Endpoint cmd.
1045 Index
= (UINT8
)Request
->Value
;
1046 ASSERT (Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
);
1047 Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
] = AllocateZeroPool (*DataLength
);
1048 CopyMem (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
], Data
, *DataLength
);
1050 // Default to use AlternateSetting 0 for all interfaces.
1052 Xhc
->UsbDevContext
[SlotId
].ActiveAlternateSetting
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->NumInterfaces
* sizeof (UINT8
));
1054 } else if (((DescriptorType
== USB_DESC_TYPE_HUB
) ||
1055 (DescriptorType
== USB_DESC_TYPE_HUB_SUPER_SPEED
)) && (*DataLength
> 2))
1057 ASSERT (Data
!= NULL
);
1058 HubDesc
= (EFI_USB_HUB_DESCRIPTOR
*)Data
;
1059 ASSERT (HubDesc
->NumPorts
<= 15);
1061 // The bit 5,6 of HubCharacter field of Hub Descriptor is TTT.
1063 TTT
= (UINT8
)((HubDesc
->HubCharacter
& (BIT5
| BIT6
)) >> 5);
1064 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.DeviceProtocol
== 2) {
1066 // Don't support multi-TT feature for super speed hub now.
1069 DEBUG ((DEBUG_ERROR
, "XHCI: Don't support multi-TT feature for Hub now. (force to disable MTT)\n"));
1074 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1075 Status
= XhcConfigHubContext (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
1077 Status
= XhcConfigHubContext64 (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
1080 } else if ((Request
->Request
== USB_REQ_SET_CONFIG
) &&
1081 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
)))
1084 // Hook Set_Config request from UsbBus as we need configure device endpoint.
1086 for (Index
= 0; Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
; Index
++) {
1087 if (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->ConfigurationValue
== (UINT8
)Request
->Value
) {
1088 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1089 Status
= XhcSetConfigCmd (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
1091 Status
= XhcSetConfigCmd64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
1097 } else if ((Request
->Request
== USB_REQ_SET_INTERFACE
) &&
1098 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_INTERFACE
)))
1101 // Hook Set_Interface request from UsbBus as we need configure interface setting.
1102 // Request->Value indicates AlterlateSetting to set
1103 // Request->Index indicates Interface to set
1105 if (Xhc
->UsbDevContext
[SlotId
].ActiveAlternateSetting
[(UINT8
)Request
->Index
] != (UINT8
)Request
->Value
) {
1106 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1107 Status
= XhcSetInterface (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Xhc
->UsbDevContext
[SlotId
].ActiveConfiguration
- 1], Request
);
1109 Status
= XhcSetInterface64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Xhc
->UsbDevContext
[SlotId
].ActiveConfiguration
- 1], Request
);
1112 } else if ((Request
->Request
== USB_REQ_GET_STATUS
) &&
1113 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
)))
1115 ASSERT (Data
!= NULL
);
1117 // Hook Get_Status request from UsbBus to keep track of the port status change.
1119 State
= *(UINT32
*)Data
;
1120 PortStatus
.PortStatus
= 0;
1121 PortStatus
.PortChangeStatus
= 0;
1123 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
1125 // For super speed hub, its bit10~12 presents the attached device speed.
1127 if ((State
& XHC_PORTSC_PS
) >> 10 == 0) {
1128 PortStatus
.PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
1132 // For high or full/low speed hub, its bit9~10 presents the attached device speed.
1134 if (XHC_BIT_IS_SET (State
, BIT9
)) {
1135 PortStatus
.PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
1136 } else if (XHC_BIT_IS_SET (State
, BIT10
)) {
1137 PortStatus
.PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
1142 // Convert the XHCI port/port change state to UEFI status
1144 MapSize
= sizeof (mUsbHubPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
1145 for (Index
= 0; Index
< MapSize
; Index
++) {
1146 if (XHC_BIT_IS_SET (State
, mUsbHubPortStateMap
[Index
].HwState
)) {
1147 PortStatus
.PortStatus
= (UINT16
)(PortStatus
.PortStatus
| mUsbHubPortStateMap
[Index
].UefiState
);
1151 MapSize
= sizeof (mUsbHubPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
1152 for (Index
= 0; Index
< MapSize
; Index
++) {
1153 if (XHC_BIT_IS_SET (State
, mUsbHubPortChangeMap
[Index
].HwState
)) {
1154 PortStatus
.PortChangeStatus
= (UINT16
)(PortStatus
.PortChangeStatus
| mUsbHubPortChangeMap
[Index
].UefiState
);
1158 MapSize
= sizeof (mUsbHubClearPortChangeMap
) / sizeof (USB_CLEAR_PORT_MAP
);
1160 for (Index
= 0; Index
< MapSize
; Index
++) {
1161 if (XHC_BIT_IS_SET (State
, mUsbHubClearPortChangeMap
[Index
].HwState
)) {
1162 ZeroMem (&ClearPortRequest
, sizeof (EFI_USB_DEVICE_REQUEST
));
1163 ClearPortRequest
.RequestType
= USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
);
1164 ClearPortRequest
.Request
= (UINT8
)USB_REQ_CLEAR_FEATURE
;
1165 ClearPortRequest
.Value
= mUsbHubClearPortChangeMap
[Index
].Selector
;
1166 ClearPortRequest
.Index
= Request
->Index
;
1167 ClearPortRequest
.Length
= 0;
1169 XhcControlTransfer (
1173 MaximumPacketLength
,
1185 XhcPollPortStatusChange (Xhc
, Xhc
->UsbDevContext
[SlotId
].RouteString
, (UINT8
)Request
->Index
, &PortStatus
);
1187 *(UINT32
*)Data
= *(UINT32
*)&PortStatus
;
1191 if (EFI_ERROR (Status
)) {
1192 DEBUG ((DEBUG_ERROR
, "XhcControlTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1195 gBS
->RestoreTPL (OldTpl
);
1201 Submits bulk transfer to a bulk endpoint of a USB device.
1203 @param This This EFI_USB2_HC_PROTOCOL instance.
1204 @param DeviceAddress Target device address.
1205 @param EndPointAddress Endpoint number and its direction in bit 7.
1206 @param DeviceSpeed Device speed, Low speed device doesn't support bulk
1208 @param MaximumPacketLength Maximum packet size the endpoint is capable of
1209 sending or receiving.
1210 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1211 @param Data Array of pointers to the buffers of data to transmit
1212 from or receive into.
1213 @param DataLength The lenght of the data buffer.
1214 @param DataToggle On input, the initial data toggle for the transfer;
1215 On output, it is updated to to next data toggle to
1216 use of the subsequent bulk transfer.
1217 @param Timeout Indicates the maximum time, in millisecond, which
1218 the transfer is allowed to complete.
1219 @param Translator A pointr to the transaction translator data.
1220 @param TransferResult A pointer to the detailed result information of the
1223 @retval EFI_SUCCESS The transfer was completed successfully.
1224 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1225 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1226 @retval EFI_TIMEOUT The transfer failed due to timeout.
1227 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1233 IN EFI_USB2_HC_PROTOCOL
*This
,
1234 IN UINT8 DeviceAddress
,
1235 IN UINT8 EndPointAddress
,
1236 IN UINT8 DeviceSpeed
,
1237 IN UINTN MaximumPacketLength
,
1238 IN UINT8 DataBuffersNumber
,
1239 IN OUT VOID
*Data
[EFI_USB_MAX_BULK_BUFFER_NUM
],
1240 IN OUT UINTN
*DataLength
,
1241 IN OUT UINT8
*DataToggle
,
1243 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1244 OUT UINT32
*TransferResult
1247 USB_XHCI_INSTANCE
*Xhc
;
1251 UINTN DebugErrorLevel
;
1254 // Validate the parameters
1256 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1257 (Data
== NULL
) || (Data
[0] == NULL
) || (TransferResult
== NULL
))
1259 return EFI_INVALID_PARAMETER
;
1262 if ((*DataToggle
!= 0) && (*DataToggle
!= 1)) {
1263 return EFI_INVALID_PARAMETER
;
1266 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) ||
1267 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1268 ((EFI_USB_SPEED_HIGH
== DeviceSpeed
) && (MaximumPacketLength
> 512)) ||
1269 ((EFI_USB_SPEED_SUPER
== DeviceSpeed
) && (MaximumPacketLength
> 1024)))
1271 return EFI_INVALID_PARAMETER
;
1274 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1276 Xhc
= XHC_FROM_THIS (This
);
1278 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1279 Status
= EFI_DEVICE_ERROR
;
1281 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1282 DEBUG ((DEBUG_ERROR
, "XhcBulkTransfer: HC is halted\n"));
1287 // Check if the device is still enabled before every transaction.
1289 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1295 // Create a new URB, insert it into the asynchronous
1296 // schedule list, then poll the execution status.
1298 Status
= XhcTransfer (
1303 MaximumPacketLength
,
1313 if (EFI_ERROR (Status
)) {
1314 if (Status
== EFI_TIMEOUT
) {
1315 DebugErrorLevel
= DEBUG_VERBOSE
;
1317 DebugErrorLevel
= DEBUG_ERROR
;
1320 DEBUG ((DebugErrorLevel
, "XhcBulkTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1323 gBS
->RestoreTPL (OldTpl
);
1329 Submits an asynchronous interrupt transfer to an
1330 interrupt endpoint of a USB device.
1332 @param This This EFI_USB2_HC_PROTOCOL instance.
1333 @param DeviceAddress Target device address.
1334 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1335 @param DeviceSpeed Indicates device speed.
1336 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1337 @param IsNewTransfer If TRUE, to submit an new asynchronous interrupt
1338 transfer If FALSE, to remove the specified
1339 asynchronous interrupt.
1340 @param DataToggle On input, the initial data toggle to use; on output,
1341 it is updated to indicate the next data toggle.
1342 @param PollingInterval The he interval, in milliseconds, that the transfer
1344 @param DataLength The length of data to receive at the rate specified
1346 @param Translator Transaction translator to use.
1347 @param CallBackFunction Function to call at the rate specified by
1349 @param Context Context to CallBackFunction.
1351 @retval EFI_SUCCESS The request has been successfully submitted or canceled.
1352 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1353 @retval EFI_OUT_OF_RESOURCES The request failed due to a lack of resources.
1354 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1359 XhcAsyncInterruptTransfer (
1360 IN EFI_USB2_HC_PROTOCOL
*This
,
1361 IN UINT8 DeviceAddress
,
1362 IN UINT8 EndPointAddress
,
1363 IN UINT8 DeviceSpeed
,
1364 IN UINTN MaximumPacketLength
,
1365 IN BOOLEAN IsNewTransfer
,
1366 IN OUT UINT8
*DataToggle
,
1367 IN UINTN PollingInterval
,
1368 IN UINTN DataLength
,
1369 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1370 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction
,
1371 IN VOID
*Context OPTIONAL
1374 USB_XHCI_INSTANCE
*Xhc
;
1382 // Validate parameters
1384 if (!XHCI_IS_DATAIN (EndPointAddress
)) {
1385 return EFI_INVALID_PARAMETER
;
1388 if (IsNewTransfer
) {
1389 if (DataLength
== 0) {
1390 return EFI_INVALID_PARAMETER
;
1393 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1394 return EFI_INVALID_PARAMETER
;
1397 if ((PollingInterval
> 255) || (PollingInterval
< 1)) {
1398 return EFI_INVALID_PARAMETER
;
1402 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1404 Xhc
= XHC_FROM_THIS (This
);
1407 // Delete Async interrupt transfer request.
1409 if (!IsNewTransfer
) {
1411 // The delete request may happen after device is detached.
1413 for (Index
= 0; Index
< 255; Index
++) {
1414 if (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== DeviceAddress
) {
1420 Status
= EFI_INVALID_PARAMETER
;
1424 Status
= XhciDelAsyncIntTransfer (Xhc
, DeviceAddress
, EndPointAddress
);
1425 DEBUG ((DEBUG_INFO
, "XhcAsyncInterruptTransfer: remove old transfer for addr %d, Status = %r\n", DeviceAddress
, Status
));
1429 Status
= EFI_SUCCESS
;
1431 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1432 DEBUG ((DEBUG_ERROR
, "XhcAsyncInterruptTransfer: HC is halt\n"));
1433 Status
= EFI_DEVICE_ERROR
;
1438 // Check if the device is still enabled before every transaction.
1440 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1445 Urb
= XhciInsertAsyncIntTransfer (
1450 MaximumPacketLength
,
1456 Status
= EFI_OUT_OF_RESOURCES
;
1461 // Ring the doorbell
1463 Status
= RingIntTransferDoorBell (Xhc
, Urb
);
1466 Xhc
->PciIo
->Flush (Xhc
->PciIo
);
1467 gBS
->RestoreTPL (OldTpl
);
1473 Submits synchronous interrupt transfer to an interrupt endpoint
1476 @param This This EFI_USB2_HC_PROTOCOL instance.
1477 @param DeviceAddress Target device address.
1478 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1479 @param DeviceSpeed Indicates device speed.
1480 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1481 of sending or receiving.
1482 @param Data Buffer of data that will be transmitted to USB
1483 device or received from USB device.
1484 @param DataLength On input, the size, in bytes, of the data buffer; On
1485 output, the number of bytes transferred.
1486 @param DataToggle On input, the initial data toggle to use; on output,
1487 it is updated to indicate the next data toggle.
1488 @param Timeout Maximum time, in second, to complete.
1489 @param Translator Transaction translator to use.
1490 @param TransferResult Variable to receive the transfer result.
1492 @return EFI_SUCCESS The transfer was completed successfully.
1493 @return EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1494 @return EFI_INVALID_PARAMETER Some parameters are invalid.
1495 @return EFI_TIMEOUT The transfer failed due to timeout.
1496 @return EFI_DEVICE_ERROR The failed due to host controller or device error
1501 XhcSyncInterruptTransfer (
1502 IN EFI_USB2_HC_PROTOCOL
*This
,
1503 IN UINT8 DeviceAddress
,
1504 IN UINT8 EndPointAddress
,
1505 IN UINT8 DeviceSpeed
,
1506 IN UINTN MaximumPacketLength
,
1508 IN OUT UINTN
*DataLength
,
1509 IN OUT UINT8
*DataToggle
,
1511 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1512 OUT UINT32
*TransferResult
1515 USB_XHCI_INSTANCE
*Xhc
;
1521 // Validates parameters
1523 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1524 (Data
== NULL
) || (TransferResult
== NULL
))
1526 return EFI_INVALID_PARAMETER
;
1529 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1530 return EFI_INVALID_PARAMETER
;
1533 if (((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) ||
1534 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1535 ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) && (MaximumPacketLength
> 3072)))
1537 return EFI_INVALID_PARAMETER
;
1540 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1542 Xhc
= XHC_FROM_THIS (This
);
1544 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1545 Status
= EFI_DEVICE_ERROR
;
1547 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1548 DEBUG ((DEBUG_ERROR
, "EhcSyncInterruptTransfer: HC is halt\n"));
1553 // Check if the device is still enabled before every transaction.
1555 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1560 Status
= XhcTransfer (
1565 MaximumPacketLength
,
1566 XHC_INT_TRANSFER_SYNC
,
1575 if (EFI_ERROR (Status
)) {
1576 DEBUG ((DEBUG_ERROR
, "XhcSyncInterruptTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1579 gBS
->RestoreTPL (OldTpl
);
1585 Submits isochronous transfer to a target USB device.
1587 @param This This EFI_USB2_HC_PROTOCOL instance.
1588 @param DeviceAddress Target device address.
1589 @param EndPointAddress End point address with its direction.
1590 @param DeviceSpeed Device speed, Low speed device doesn't support this
1592 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1593 sending or receiving.
1594 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1595 @param Data Array of pointers to the buffers of data that will
1596 be transmitted to USB device or received from USB
1598 @param DataLength The size, in bytes, of the data buffer.
1599 @param Translator Transaction translator to use.
1600 @param TransferResult Variable to receive the transfer result.
1602 @return EFI_UNSUPPORTED Isochronous transfer is unsupported.
1607 XhcIsochronousTransfer (
1608 IN EFI_USB2_HC_PROTOCOL
*This
,
1609 IN UINT8 DeviceAddress
,
1610 IN UINT8 EndPointAddress
,
1611 IN UINT8 DeviceSpeed
,
1612 IN UINTN MaximumPacketLength
,
1613 IN UINT8 DataBuffersNumber
,
1614 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1615 IN UINTN DataLength
,
1616 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1617 OUT UINT32
*TransferResult
1620 return EFI_UNSUPPORTED
;
1624 Submits Async isochronous transfer to a target USB device.
1626 @param This This EFI_USB2_HC_PROTOCOL instance.
1627 @param DeviceAddress Target device address.
1628 @param EndPointAddress End point address with its direction.
1629 @param DeviceSpeed Device speed, Low speed device doesn't support this
1631 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1632 sending or receiving.
1633 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1634 @param Data Array of pointers to the buffers of data that will
1635 be transmitted to USB device or received from USB
1637 @param DataLength The size, in bytes, of the data buffer.
1638 @param Translator Transaction translator to use.
1639 @param IsochronousCallBack Function to be called when the transfer complete.
1640 @param Context Context passed to the call back function as
1643 @return EFI_UNSUPPORTED Isochronous transfer isn't supported.
1648 XhcAsyncIsochronousTransfer (
1649 IN EFI_USB2_HC_PROTOCOL
*This
,
1650 IN UINT8 DeviceAddress
,
1651 IN UINT8 EndPointAddress
,
1652 IN UINT8 DeviceSpeed
,
1653 IN UINTN MaximumPacketLength
,
1654 IN UINT8 DataBuffersNumber
,
1655 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1656 IN UINTN DataLength
,
1657 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1658 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
1662 return EFI_UNSUPPORTED
;
1666 Entry point for EFI drivers.
1668 @param ImageHandle EFI_HANDLE.
1669 @param SystemTable EFI_SYSTEM_TABLE.
1671 @retval EFI_SUCCESS Success.
1672 @retval Others Fail.
1677 XhcDriverEntryPoint (
1678 IN EFI_HANDLE ImageHandle
,
1679 IN EFI_SYSTEM_TABLE
*SystemTable
1682 return EfiLibInstallDriverBindingComponentName2 (
1685 &gXhciDriverBinding
,
1687 &gXhciComponentName
,
1688 &gXhciComponentName2
1693 Test to see if this driver supports ControllerHandle. Any
1694 ControllerHandle that has Usb2HcProtocol installed will
1697 @param This Protocol instance pointer.
1698 @param Controller Handle of device to test.
1699 @param RemainingDevicePath Not used.
1701 @return EFI_SUCCESS This driver supports this device.
1702 @return EFI_UNSUPPORTED This driver does not support this device.
1707 XhcDriverBindingSupported (
1708 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1709 IN EFI_HANDLE Controller
,
1710 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1714 EFI_PCI_IO_PROTOCOL
*PciIo
;
1715 USB_CLASSC UsbClassCReg
;
1718 // Test whether there is PCI IO Protocol attached on the controller handle.
1720 Status
= gBS
->OpenProtocol (
1722 &gEfiPciIoProtocolGuid
,
1724 This
->DriverBindingHandle
,
1726 EFI_OPEN_PROTOCOL_BY_DRIVER
1729 if (EFI_ERROR (Status
)) {
1730 return EFI_UNSUPPORTED
;
1733 Status
= PciIo
->Pci
.Read (
1736 PCI_CLASSCODE_OFFSET
,
1737 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1741 if (EFI_ERROR (Status
)) {
1742 Status
= EFI_UNSUPPORTED
;
1747 // Test whether the controller belongs to Xhci type
1749 if ((UsbClassCReg
.BaseCode
!= PCI_CLASS_SERIAL
) ||
1750 (UsbClassCReg
.SubClassCode
!= PCI_CLASS_SERIAL_USB
) ||
1751 (UsbClassCReg
.ProgInterface
!= PCI_IF_XHCI
))
1753 Status
= EFI_UNSUPPORTED
;
1757 gBS
->CloseProtocol (
1759 &gEfiPciIoProtocolGuid
,
1760 This
->DriverBindingHandle
,
1768 Create and initialize a USB_XHCI_INSTANCE structure.
1770 @param PciIo The PciIo on this device.
1771 @param DevicePath The device path of host controller.
1772 @param OriginalPciAttributes Original PCI attributes.
1774 @return The allocated and initialized USB_XHCI_INSTANCE structure if created,
1780 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1781 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1782 IN UINT64 OriginalPciAttributes
1785 USB_XHCI_INSTANCE
*Xhc
;
1789 UINT8 ReleaseNumber
;
1791 Xhc
= AllocateZeroPool (sizeof (USB_XHCI_INSTANCE
));
1798 // Initialize private data structure
1800 Xhc
->Signature
= XHCI_INSTANCE_SIG
;
1802 Xhc
->DevicePath
= DevicePath
;
1803 Xhc
->OriginalPciAttributes
= OriginalPciAttributes
;
1804 CopyMem (&Xhc
->Usb2Hc
, &gXhciUsb2HcTemplate
, sizeof (EFI_USB2_HC_PROTOCOL
));
1806 Status
= PciIo
->Pci
.Read (
1809 XHC_PCI_SBRN_OFFSET
,
1814 if (!EFI_ERROR (Status
)) {
1815 Xhc
->Usb2Hc
.MajorRevision
= (ReleaseNumber
& 0xF0) >> 4;
1816 Xhc
->Usb2Hc
.MinorRevision
= (ReleaseNumber
& 0x0F);
1819 InitializeListHead (&Xhc
->AsyncIntTransfers
);
1822 // Be caution that the Offset passed to XhcReadCapReg() should be Dword align
1824 Xhc
->CapLength
= XhcReadCapReg8 (Xhc
, XHC_CAPLENGTH_OFFSET
);
1825 Xhc
->HcSParams1
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS1_OFFSET
);
1826 Xhc
->HcSParams2
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS2_OFFSET
);
1827 Xhc
->HcCParams
.Dword
= XhcReadCapReg (Xhc
, XHC_HCCPARAMS_OFFSET
);
1828 Xhc
->DBOff
= XhcReadCapReg (Xhc
, XHC_DBOFF_OFFSET
);
1829 Xhc
->RTSOff
= XhcReadCapReg (Xhc
, XHC_RTSOFF_OFFSET
);
1832 // This PageSize field defines the page size supported by the xHC implementation.
1833 // This xHC supports a page size of 2^(n+12) if bit n is Set. For example,
1834 // if bit 0 is Set, the xHC supports 4k byte page sizes.
1836 PageSize
= XhcReadOpReg (Xhc
, XHC_PAGESIZE_OFFSET
);
1837 if ((PageSize
& (~XHC_PAGESIZE_MASK
)) != 0) {
1838 DEBUG ((DEBUG_ERROR
, "XhcCreateUsb3Hc: Reserved bits are not 0 for PageSize\n"));
1842 PageSize
&= XHC_PAGESIZE_MASK
;
1843 Xhc
->PageSize
= 1 << (HighBitSet32 (PageSize
) + 12);
1845 ExtCapReg
= (UINT16
)(Xhc
->HcCParams
.Data
.ExtCapReg
);
1846 Xhc
->ExtCapRegBase
= ExtCapReg
<< 2;
1847 Xhc
->UsbLegSupOffset
= XhcGetCapabilityAddr (Xhc
, XHC_CAP_USB_LEGACY
);
1848 Xhc
->DebugCapSupOffset
= XhcGetCapabilityAddr (Xhc
, XHC_CAP_USB_DEBUG
);
1849 Xhc
->Usb2SupOffset
= XhcGetSupportedProtocolCapabilityAddr (Xhc
, XHC_SUPPORTED_PROTOCOL_DW0_MAJOR_REVISION_USB2
);
1850 Xhc
->Usb3SupOffset
= XhcGetSupportedProtocolCapabilityAddr (Xhc
, XHC_SUPPORTED_PROTOCOL_DW0_MAJOR_REVISION_USB3
);
1852 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: Capability length 0x%x\n", Xhc
->CapLength
));
1853 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: HcSParams1 0x%x\n", Xhc
->HcSParams1
));
1854 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: HcSParams2 0x%x\n", Xhc
->HcSParams2
));
1855 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: HcCParams 0x%x\n", Xhc
->HcCParams
));
1856 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: DBOff 0x%x\n", Xhc
->DBOff
));
1857 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: RTSOff 0x%x\n", Xhc
->RTSOff
));
1858 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: UsbLegSupOffset 0x%x\n", Xhc
->UsbLegSupOffset
));
1859 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: DebugCapSupOffset 0x%x\n", Xhc
->DebugCapSupOffset
));
1860 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: Usb2SupOffset 0x%x\n", Xhc
->Usb2SupOffset
));
1861 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: Usb3SupOffset 0x%x\n", Xhc
->Usb3SupOffset
));
1864 // Create AsyncRequest Polling Timer
1866 Status
= gBS
->CreateEvent (
1867 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1869 XhcMonitorAsyncRequests
,
1874 if (EFI_ERROR (Status
)) {
1886 One notified function to stop the Host Controller when gBS->ExitBootServices() called.
1888 @param Event Pointer to this event
1889 @param Context Event handler private data
1894 XhcExitBootService (
1900 USB_XHCI_INSTANCE
*Xhc
;
1901 EFI_PCI_IO_PROTOCOL
*PciIo
;
1903 Xhc
= (USB_XHCI_INSTANCE
*)Context
;
1907 // Stop AsyncRequest Polling timer then stop the XHCI driver
1908 // and uninstall the XHCI protocl.
1910 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
1911 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
1913 if (Xhc
->PollTimer
!= NULL
) {
1914 gBS
->CloseEvent (Xhc
->PollTimer
);
1917 XhcClearBiosOwnership (Xhc
);
1920 // Restore original PCI attributes
1924 EfiPciIoAttributeOperationSet
,
1925 Xhc
->OriginalPciAttributes
,
1931 Starting the Usb XHCI Driver.
1933 @param This Protocol instance pointer.
1934 @param Controller Handle of device to test.
1935 @param RemainingDevicePath Not used.
1937 @return EFI_SUCCESS supports this device.
1938 @return EFI_UNSUPPORTED do not support this device.
1939 @return EFI_DEVICE_ERROR cannot be started due to device Error.
1940 @return EFI_OUT_OF_RESOURCES cannot allocate resources.
1945 XhcDriverBindingStart (
1946 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1947 IN EFI_HANDLE Controller
,
1948 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1952 EFI_PCI_IO_PROTOCOL
*PciIo
;
1954 UINT64 OriginalPciAttributes
;
1955 BOOLEAN PciAttributesSaved
;
1956 USB_XHCI_INSTANCE
*Xhc
;
1957 EFI_DEVICE_PATH_PROTOCOL
*HcDevicePath
;
1960 // Open the PciIo Protocol, then enable the USB host controller
1962 Status
= gBS
->OpenProtocol (
1964 &gEfiPciIoProtocolGuid
,
1966 This
->DriverBindingHandle
,
1968 EFI_OPEN_PROTOCOL_BY_DRIVER
1971 if (EFI_ERROR (Status
)) {
1976 // Open Device Path Protocol for on USB host controller
1978 HcDevicePath
= NULL
;
1979 Status
= gBS
->OpenProtocol (
1981 &gEfiDevicePathProtocolGuid
,
1982 (VOID
**)&HcDevicePath
,
1983 This
->DriverBindingHandle
,
1985 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1988 PciAttributesSaved
= FALSE
;
1990 // Save original PCI attributes
1992 Status
= PciIo
->Attributes (
1994 EfiPciIoAttributeOperationGet
,
1996 &OriginalPciAttributes
1999 if (EFI_ERROR (Status
)) {
2003 PciAttributesSaved
= TRUE
;
2005 Status
= PciIo
->Attributes (
2007 EfiPciIoAttributeOperationSupported
,
2011 if (!EFI_ERROR (Status
)) {
2012 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
2013 Status
= PciIo
->Attributes (
2015 EfiPciIoAttributeOperationEnable
,
2021 if (EFI_ERROR (Status
)) {
2022 DEBUG ((DEBUG_ERROR
, "XhcDriverBindingStart: failed to enable controller\n"));
2027 // Create then install USB2_HC_PROTOCOL
2029 Xhc
= XhcCreateUsbHc (PciIo
, HcDevicePath
, OriginalPciAttributes
);
2032 DEBUG ((DEBUG_ERROR
, "XhcDriverBindingStart: failed to create USB2_HC\n"));
2033 return EFI_OUT_OF_RESOURCES
;
2037 // Enable 64-bit DMA support in the PCI layer if this controller
2040 if (Xhc
->HcCParams
.Data
.Ac64
!= 0) {
2041 Status
= PciIo
->Attributes (
2043 EfiPciIoAttributeOperationEnable
,
2044 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
2047 if (!EFI_ERROR (Status
)) {
2048 Xhc
->Support64BitDma
= TRUE
;
2052 "%a: failed to enable 64-bit DMA on 64-bit capable controller @ %p (%r)\n",
2060 XhcSetBiosOwnership (Xhc
);
2062 XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
2063 ASSERT (XhcIsHalt (Xhc
));
2066 // After Chip Hardware Reset wait until the Controller Not Ready (CNR) flag
2067 // in the USBSTS is '0' before writing any xHC Operational or Runtime registers.
2069 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
2072 // Initialize the schedule
2077 // Start the Host Controller
2079 XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2082 // Start the asynchronous interrupt monitor
2084 Status
= gBS
->SetTimer (Xhc
->PollTimer
, TimerPeriodic
, XHC_ASYNC_TIMER_INTERVAL
);
2085 if (EFI_ERROR (Status
)) {
2086 DEBUG ((DEBUG_ERROR
, "XhcDriverBindingStart: failed to start async interrupt monitor\n"));
2087 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2092 // Create event to stop the HC when exit boot service.
2094 Status
= gBS
->CreateEventEx (
2099 &gEfiEventExitBootServicesGuid
,
2100 &Xhc
->ExitBootServiceEvent
2102 if (EFI_ERROR (Status
)) {
2107 // Install the component name protocol, don't fail the start
2108 // because of something for display.
2112 gXhciComponentName
.SupportedLanguages
,
2113 &Xhc
->ControllerNameTable
,
2114 L
"eXtensible Host Controller (USB 3.0)",
2119 gXhciComponentName2
.SupportedLanguages
,
2120 &Xhc
->ControllerNameTable
,
2121 L
"eXtensible Host Controller (USB 3.0)",
2125 Status
= gBS
->InstallProtocolInterface (
2127 &gEfiUsb2HcProtocolGuid
,
2128 EFI_NATIVE_INTERFACE
,
2131 if (EFI_ERROR (Status
)) {
2132 DEBUG ((DEBUG_ERROR
, "XhcDriverBindingStart: failed to install USB2_HC Protocol\n"));
2136 DEBUG ((DEBUG_INFO
, "XhcDriverBindingStart: XHCI started for controller @ %x\n", Controller
));
2140 gBS
->CloseEvent (Xhc
->PollTimer
);
2145 if (PciAttributesSaved
) {
2147 // Restore original PCI attributes
2151 EfiPciIoAttributeOperationSet
,
2152 OriginalPciAttributes
,
2157 gBS
->CloseProtocol (
2159 &gEfiPciIoProtocolGuid
,
2160 This
->DriverBindingHandle
,
2168 Stop this driver on ControllerHandle. Support stopping any child handles
2169 created by this driver.
2171 @param This Protocol instance pointer.
2172 @param Controller Handle of device to stop driver on.
2173 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
2174 @param ChildHandleBuffer List of handles for the children we need to stop.
2176 @return EFI_SUCCESS Success.
2177 @return EFI_DEVICE_ERROR Fail.
2182 XhcDriverBindingStop (
2183 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
2184 IN EFI_HANDLE Controller
,
2185 IN UINTN NumberOfChildren
,
2186 IN EFI_HANDLE
*ChildHandleBuffer
2190 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
2191 EFI_PCI_IO_PROTOCOL
*PciIo
;
2192 USB_XHCI_INSTANCE
*Xhc
;
2196 // Test whether the Controller handler passed in is a valid
2197 // Usb controller handle that should be supported, if not,
2198 // return the error status directly
2200 Status
= gBS
->OpenProtocol (
2202 &gEfiUsb2HcProtocolGuid
,
2204 This
->DriverBindingHandle
,
2206 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2209 if (EFI_ERROR (Status
)) {
2213 Status
= gBS
->UninstallProtocolInterface (
2215 &gEfiUsb2HcProtocolGuid
,
2219 if (EFI_ERROR (Status
)) {
2223 Xhc
= XHC_FROM_THIS (Usb2Hc
);
2227 // Stop AsyncRequest Polling timer then stop the XHCI driver
2228 // and uninstall the XHCI protocl.
2230 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
2233 // Disable the device slots occupied by these devices on its downstream ports.
2234 // Entry 0 is reserved.
2236 for (Index
= 0; Index
< 255; Index
++) {
2237 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
||
2238 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0))
2243 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
2244 XhcDisableSlotCmd (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2246 XhcDisableSlotCmd64 (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2250 if (Xhc
->PollTimer
!= NULL
) {
2251 gBS
->CloseEvent (Xhc
->PollTimer
);
2254 if (Xhc
->ExitBootServiceEvent
!= NULL
) {
2255 gBS
->CloseEvent (Xhc
->ExitBootServiceEvent
);
2258 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2259 XhcClearBiosOwnership (Xhc
);
2260 XhciDelAllAsyncIntTransfers (Xhc
);
2263 if (Xhc
->ControllerNameTable
) {
2264 FreeUnicodeStringTable (Xhc
->ControllerNameTable
);
2268 // Restore original PCI attributes
2272 EfiPciIoAttributeOperationSet
,
2273 Xhc
->OriginalPciAttributes
,
2277 gBS
->CloseProtocol (
2279 &gEfiPciIoProtocolGuid
,
2280 This
->DriverBindingHandle
,