2 The XHCI controller driver.
4 Copyright (c) 2011 - 2018, 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 ((EFI_D_INFO
, "XhcGetCapability: %d ports, 64 bit %d\n", *PortNumber
, *Is64BitCapable
));
125 gBS
->RestoreTPL (OldTpl
);
132 Provides software reset for the USB host controller.
134 @param This This EFI_USB2_HC_PROTOCOL instance.
135 @param Attributes A bit mask of the reset operation to perform.
137 @retval EFI_SUCCESS The reset operation succeeded.
138 @retval EFI_INVALID_PARAMETER Attributes is not valid.
139 @retval EFI_UNSUPPOURTED The type of reset specified by Attributes is
140 not currently supported by the host controller.
141 @retval EFI_DEVICE_ERROR Host controller isn't halted to reset.
147 IN EFI_USB2_HC_PROTOCOL
*This
,
151 USB_XHCI_INSTANCE
*Xhc
;
155 Xhc
= XHC_FROM_THIS (This
);
157 if (Xhc
->DevicePath
!= NULL
) {
159 // Report Status Code to indicate reset happens
161 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
163 (EFI_IO_BUS_USB
| EFI_IOB_PC_RESET
),
168 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
170 switch (Attributes
) {
171 case EFI_USB_HC_RESET_GLOBAL
:
173 // Flow through, same behavior as Host Controller Reset
175 case EFI_USB_HC_RESET_HOST_CONTROLLER
:
176 if ((Xhc
->DebugCapSupOffset
!= 0xFFFFFFFF) && ((XhcReadExtCapReg (Xhc
, Xhc
->DebugCapSupOffset
) & 0xFF) == XHC_CAP_USB_DEBUG
) &&
177 ((XhcReadExtCapReg (Xhc
, Xhc
->DebugCapSupOffset
+ XHC_DC_DCCTRL
) & BIT0
) != 0)) {
178 Status
= EFI_SUCCESS
;
182 // Host Controller must be Halt when Reset it
184 if (!XhcIsHalt (Xhc
)) {
185 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
187 if (EFI_ERROR (Status
)) {
188 Status
= EFI_DEVICE_ERROR
;
193 Status
= XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
194 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
196 if (EFI_ERROR (Status
)) {
200 // Clean up the asynchronous transfers, currently only
201 // interrupt supports asynchronous operation.
203 XhciDelAllAsyncIntTransfers (Xhc
);
209 case EFI_USB_HC_RESET_GLOBAL_WITH_DEBUG
:
210 case EFI_USB_HC_RESET_HOST_WITH_DEBUG
:
211 Status
= EFI_UNSUPPORTED
;
215 Status
= EFI_INVALID_PARAMETER
;
219 DEBUG ((EFI_D_INFO
, "XhcReset: status %r\n", Status
));
220 gBS
->RestoreTPL (OldTpl
);
227 Retrieve the current state of the USB host controller.
229 @param This This EFI_USB2_HC_PROTOCOL instance.
230 @param State Variable to return the current host controller
233 @retval EFI_SUCCESS Host controller state was returned in State.
234 @retval EFI_INVALID_PARAMETER State is NULL.
235 @retval EFI_DEVICE_ERROR An error was encountered while attempting to
236 retrieve the host controller's current state.
242 IN EFI_USB2_HC_PROTOCOL
*This
,
243 OUT EFI_USB_HC_STATE
*State
246 USB_XHCI_INSTANCE
*Xhc
;
250 return EFI_INVALID_PARAMETER
;
253 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
255 Xhc
= XHC_FROM_THIS (This
);
257 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
258 *State
= EfiUsbHcStateHalt
;
260 *State
= EfiUsbHcStateOperational
;
263 DEBUG ((EFI_D_INFO
, "XhcGetState: current state %d\n", *State
));
264 gBS
->RestoreTPL (OldTpl
);
270 Sets the USB host controller to a specific state.
272 @param This This EFI_USB2_HC_PROTOCOL instance.
273 @param State The state of the host controller that will be set.
275 @retval EFI_SUCCESS The USB host controller was successfully placed
276 in the state specified by State.
277 @retval EFI_INVALID_PARAMETER State is invalid.
278 @retval EFI_DEVICE_ERROR Failed to set the state due to device error.
284 IN EFI_USB2_HC_PROTOCOL
*This
,
285 IN EFI_USB_HC_STATE State
288 USB_XHCI_INSTANCE
*Xhc
;
290 EFI_USB_HC_STATE CurState
;
293 Status
= XhcGetState (This
, &CurState
);
295 if (EFI_ERROR (Status
)) {
296 return EFI_DEVICE_ERROR
;
299 if (CurState
== State
) {
303 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
305 Xhc
= XHC_FROM_THIS (This
);
308 case EfiUsbHcStateHalt
:
309 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
312 case EfiUsbHcStateOperational
:
313 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HSE
)) {
314 Status
= EFI_DEVICE_ERROR
;
319 // Software must not write a one to this field unless the host controller
320 // is in the Halted state. Doing so will yield undefined results.
321 // refers to Spec[XHCI1.0-2.3.1]
323 if (!XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
324 Status
= EFI_DEVICE_ERROR
;
328 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
331 case EfiUsbHcStateSuspend
:
332 Status
= EFI_UNSUPPORTED
;
336 Status
= EFI_INVALID_PARAMETER
;
339 DEBUG ((EFI_D_INFO
, "XhcSetState: status %r\n", Status
));
340 gBS
->RestoreTPL (OldTpl
);
346 Retrieves the current status of a USB root hub port.
348 @param This This EFI_USB2_HC_PROTOCOL instance.
349 @param PortNumber The root hub port to retrieve the state from.
350 This value is zero-based.
351 @param PortStatus Variable to receive the port state.
353 @retval EFI_SUCCESS The status of the USB root hub port specified.
354 by PortNumber was returned in PortStatus.
355 @retval EFI_INVALID_PARAMETER PortNumber is invalid.
356 @retval EFI_DEVICE_ERROR Can't read register.
361 XhcGetRootHubPortStatus (
362 IN EFI_USB2_HC_PROTOCOL
*This
,
364 OUT EFI_USB_PORT_STATUS
*PortStatus
367 USB_XHCI_INSTANCE
*Xhc
;
374 USB_DEV_ROUTE ParentRouteChart
;
377 if (PortStatus
== NULL
) {
378 return EFI_INVALID_PARAMETER
;
381 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
383 Xhc
= XHC_FROM_THIS (This
);
384 Status
= EFI_SUCCESS
;
386 TotalPort
= Xhc
->HcSParams1
.Data
.MaxPorts
;
388 if (PortNumber
>= TotalPort
) {
389 Status
= EFI_INVALID_PARAMETER
;
393 Offset
= (UINT32
) (XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
394 PortStatus
->PortStatus
= 0;
395 PortStatus
->PortChangeStatus
= 0;
397 State
= XhcReadOpReg (Xhc
, Offset
);
400 // According to XHCI 1.1 spec November 2017,
401 // bit 10~13 of the root port status register identifies the speed of the attached device.
403 switch ((State
& XHC_PORTSC_PS
) >> 10) {
405 PortStatus
->PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
409 PortStatus
->PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
414 PortStatus
->PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
422 // Convert the XHCI port/port change state to UEFI status
424 MapSize
= sizeof (mUsbPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
426 for (Index
= 0; Index
< MapSize
; Index
++) {
427 if (XHC_BIT_IS_SET (State
, mUsbPortStateMap
[Index
].HwState
)) {
428 PortStatus
->PortStatus
= (UINT16
) (PortStatus
->PortStatus
| mUsbPortStateMap
[Index
].UefiState
);
432 // Bit5~8 reflects its current link state.
434 if ((State
& XHC_PORTSC_PLS
) >> 5 == 3) {
435 PortStatus
->PortStatus
|= USB_PORT_STAT_SUSPEND
;
438 MapSize
= sizeof (mUsbPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
440 for (Index
= 0; Index
< MapSize
; Index
++) {
441 if (XHC_BIT_IS_SET (State
, mUsbPortChangeMap
[Index
].HwState
)) {
442 PortStatus
->PortChangeStatus
= (UINT16
) (PortStatus
->PortChangeStatus
| mUsbPortChangeMap
[Index
].UefiState
);
446 MapSize
= sizeof (mUsbClearPortChangeMap
) / sizeof (USB_CLEAR_PORT_MAP
);
448 for (Index
= 0; Index
< MapSize
; Index
++) {
449 if (XHC_BIT_IS_SET (State
, mUsbClearPortChangeMap
[Index
].HwState
)) {
450 XhcClearRootHubPortFeature (This
, PortNumber
, (EFI_USB_PORT_FEATURE
)mUsbClearPortChangeMap
[Index
].Selector
);
455 // Poll the root port status register to enable/disable corresponding device slot if there is a device attached/detached.
456 // For those devices behind hub, we get its attach/detach event by hooking Get_Port_Status request at control transfer for those hub.
458 ParentRouteChart
.Dword
= 0;
459 XhcPollPortStatusChange (Xhc
, ParentRouteChart
, PortNumber
, PortStatus
);
462 gBS
->RestoreTPL (OldTpl
);
468 Sets a feature for the specified root hub port.
470 @param This This EFI_USB2_HC_PROTOCOL instance.
471 @param PortNumber Root hub port to set.
472 @param PortFeature Feature to set.
474 @retval EFI_SUCCESS The feature specified by PortFeature was set.
475 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
476 @retval EFI_DEVICE_ERROR Can't read register.
481 XhcSetRootHubPortFeature (
482 IN EFI_USB2_HC_PROTOCOL
*This
,
484 IN EFI_USB_PORT_FEATURE PortFeature
487 USB_XHCI_INSTANCE
*Xhc
;
494 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
496 Xhc
= XHC_FROM_THIS (This
);
497 Status
= EFI_SUCCESS
;
499 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
501 if (PortNumber
>= TotalPort
) {
502 Status
= EFI_INVALID_PARAMETER
;
506 Offset
= (UINT32
) (XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
507 State
= XhcReadOpReg (Xhc
, Offset
);
510 // Mask off the port status change bits, these bits are
513 State
&= ~ (BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
515 switch (PortFeature
) {
516 case EfiUsbPortEnable
:
518 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
519 // A port may be disabled by software writing a '1' to this flag.
521 Status
= EFI_SUCCESS
;
524 case EfiUsbPortSuspend
:
525 State
|= XHC_PORTSC_LWS
;
526 XhcWriteOpReg (Xhc
, Offset
, State
);
527 State
&= ~XHC_PORTSC_PLS
;
529 XhcWriteOpReg (Xhc
, Offset
, State
);
532 case EfiUsbPortReset
:
533 DEBUG ((EFI_D_INFO
, "XhcUsbPortReset!\n"));
535 // Make sure Host Controller not halt before reset it
537 if (XhcIsHalt (Xhc
)) {
538 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
540 if (EFI_ERROR (Status
)) {
541 DEBUG ((EFI_D_INFO
, "XhcSetRootHubPortFeature :failed to start HC - %r\n", Status
));
547 // 4.3.1 Resetting a Root Hub Port
548 // 1) Write the PORTSC register with the Port Reset (PR) bit set to '1'.
550 State
|= XHC_PORTSC_RESET
;
551 XhcWriteOpReg (Xhc
, Offset
, State
);
552 XhcWaitOpRegBit(Xhc
, Offset
, XHC_PORTSC_PRC
, TRUE
, XHC_GENERIC_TIMEOUT
);
555 case EfiUsbPortPower
:
557 // Not supported, ignore the operation
559 Status
= EFI_SUCCESS
;
562 case EfiUsbPortOwner
:
564 // XHCI root hub port don't has the owner bit, ignore the operation
566 Status
= EFI_SUCCESS
;
570 Status
= EFI_INVALID_PARAMETER
;
574 DEBUG ((EFI_D_INFO
, "XhcSetRootHubPortFeature: status %r\n", Status
));
575 gBS
->RestoreTPL (OldTpl
);
582 Clears a feature for the specified root hub port.
584 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
585 @param PortNumber Specifies the root hub port whose feature is
586 requested to be cleared.
587 @param PortFeature Indicates the feature selector associated with the
588 feature clear request.
590 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
591 for the USB root hub port specified by PortNumber.
592 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
593 @retval EFI_DEVICE_ERROR Can't read register.
598 XhcClearRootHubPortFeature (
599 IN EFI_USB2_HC_PROTOCOL
*This
,
601 IN EFI_USB_PORT_FEATURE PortFeature
604 USB_XHCI_INSTANCE
*Xhc
;
611 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
613 Xhc
= XHC_FROM_THIS (This
);
614 Status
= EFI_SUCCESS
;
616 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
618 if (PortNumber
>= TotalPort
) {
619 Status
= EFI_INVALID_PARAMETER
;
623 Offset
= XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
);
626 // Mask off the port status change bits, these bits are
629 State
= XhcReadOpReg (Xhc
, Offset
);
630 State
&= ~ (BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
632 switch (PortFeature
) {
633 case EfiUsbPortEnable
:
635 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
636 // A port may be disabled by software writing a '1' to this flag.
638 State
|= XHC_PORTSC_PED
;
639 State
&= ~XHC_PORTSC_RESET
;
640 XhcWriteOpReg (Xhc
, Offset
, State
);
643 case EfiUsbPortSuspend
:
644 State
|= XHC_PORTSC_LWS
;
645 XhcWriteOpReg (Xhc
, Offset
, State
);
646 State
&= ~XHC_PORTSC_PLS
;
647 XhcWriteOpReg (Xhc
, Offset
, State
);
650 case EfiUsbPortReset
:
652 // PORTSC_RESET BIT(4) bit is RW1S attribute, which means Write-1-to-set status:
653 // Register bits indicate status when read, a clear bit may be set by
654 // writing a '1'. Writing a '0' to RW1S bits has no effect.
658 case EfiUsbPortOwner
:
660 // XHCI root hub port don't has the owner bit, ignore the operation
664 case EfiUsbPortConnectChange
:
666 // Clear connect status change
668 State
|= XHC_PORTSC_CSC
;
669 XhcWriteOpReg (Xhc
, Offset
, State
);
672 case EfiUsbPortEnableChange
:
674 // Clear enable status change
676 State
|= XHC_PORTSC_PEC
;
677 XhcWriteOpReg (Xhc
, Offset
, State
);
680 case EfiUsbPortOverCurrentChange
:
682 // Clear PortOverCurrent change
684 State
|= XHC_PORTSC_OCC
;
685 XhcWriteOpReg (Xhc
, Offset
, State
);
688 case EfiUsbPortResetChange
:
690 // Clear Port Reset change
692 State
|= XHC_PORTSC_PRC
;
693 XhcWriteOpReg (Xhc
, Offset
, State
);
696 case EfiUsbPortPower
:
697 case EfiUsbPortSuspendChange
:
699 // Not supported or not related operation
704 Status
= EFI_INVALID_PARAMETER
;
709 DEBUG ((EFI_D_INFO
, "XhcClearRootHubPortFeature: status %r\n", Status
));
710 gBS
->RestoreTPL (OldTpl
);
716 Submits a new transaction to a target USB device.
718 @param Xhc The XHCI Instance.
719 @param DeviceAddress The target device address.
720 @param EndPointAddress Endpoint number and its direction encoded in bit 7
721 @param DeviceSpeed Target device speed.
722 @param MaximumPacketLength Maximum packet size the default control transfer
723 endpoint is capable of sending or receiving.
724 @param Type The transaction type.
725 @param Request USB device request to send.
726 @param Data Data buffer to be transmitted or received from USB
728 @param DataLength The size (in bytes) of the data buffer.
729 @param Timeout Indicates the maximum timeout, in millisecond.
730 @param TransferResult Return the result of this control transfer.
732 @retval EFI_SUCCESS Transfer was completed successfully.
733 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
734 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
735 @retval EFI_TIMEOUT Transfer failed due to timeout.
736 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
740 IN USB_XHCI_INSTANCE
*Xhc
,
741 IN UINT8 DeviceAddress
,
742 IN UINT8 EndPointAddress
,
743 IN UINT8 DeviceSpeed
,
744 IN UINTN MaximumPacketLength
,
746 IN EFI_USB_DEVICE_REQUEST
*Request
,
748 IN OUT UINTN
*DataLength
,
750 OUT UINT32
*TransferResult
754 EFI_STATUS RecoveryStatus
;
757 ASSERT ((Type
== XHC_CTRL_TRANSFER
) || (Type
== XHC_BULK_TRANSFER
) || (Type
== XHC_INT_TRANSFER_SYNC
));
773 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: failed to create URB!\n", Type
));
774 return EFI_OUT_OF_RESOURCES
;
777 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
779 if (Status
== EFI_TIMEOUT
) {
781 // The transfer timed out. Abort the transfer by dequeueing of the TD.
783 RecoveryStatus
= XhcDequeueTrbFromEndpoint(Xhc
, Urb
);
784 if (RecoveryStatus
== EFI_ALREADY_STARTED
) {
786 // The URB is finished just before stopping endpoint.
787 // Change returning status from EFI_TIMEOUT to EFI_SUCCESS.
789 ASSERT (Urb
->Result
== EFI_USB_NOERROR
);
790 Status
= EFI_SUCCESS
;
791 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: pending URB is finished, Length = %d.\n", Type
, Urb
->Completed
));
792 } else if (EFI_ERROR(RecoveryStatus
)) {
793 DEBUG((DEBUG_ERROR
, "XhcTransfer[Type=%d]: XhcDequeueTrbFromEndpoint failed!\n", Type
));
797 *TransferResult
= Urb
->Result
;
798 *DataLength
= Urb
->Completed
;
800 if ((*TransferResult
== EFI_USB_ERR_STALL
) || (*TransferResult
== EFI_USB_ERR_BABBLE
)) {
801 ASSERT (Status
== EFI_DEVICE_ERROR
);
802 RecoveryStatus
= XhcRecoverHaltedEndpoint(Xhc
, Urb
);
803 if (EFI_ERROR (RecoveryStatus
)) {
804 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: XhcRecoverHaltedEndpoint failed!\n", Type
));
808 Xhc
->PciIo
->Flush (Xhc
->PciIo
);
809 XhcFreeUrb (Xhc
, Urb
);
814 Submits control transfer to a target USB device.
816 @param This This EFI_USB2_HC_PROTOCOL instance.
817 @param DeviceAddress The target device address.
818 @param DeviceSpeed Target device speed.
819 @param MaximumPacketLength Maximum packet size the default control transfer
820 endpoint is capable of sending or receiving.
821 @param Request USB device request to send.
822 @param TransferDirection Specifies the data direction for the data stage
823 @param Data Data buffer to be transmitted or received from USB
825 @param DataLength The size (in bytes) of the data buffer.
826 @param Timeout Indicates the maximum timeout, in millisecond.
827 @param Translator Transaction translator to be used by this device.
828 @param TransferResult Return the result of this control transfer.
830 @retval EFI_SUCCESS Transfer was completed successfully.
831 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
832 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
833 @retval EFI_TIMEOUT Transfer failed due to timeout.
834 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
840 IN EFI_USB2_HC_PROTOCOL
*This
,
841 IN UINT8 DeviceAddress
,
842 IN UINT8 DeviceSpeed
,
843 IN UINTN MaximumPacketLength
,
844 IN EFI_USB_DEVICE_REQUEST
*Request
,
845 IN EFI_USB_DATA_DIRECTION TransferDirection
,
847 IN OUT UINTN
*DataLength
,
849 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
850 OUT UINT32
*TransferResult
853 USB_XHCI_INSTANCE
*Xhc
;
856 UINT8 DescriptorType
;
861 EFI_USB_HUB_DESCRIPTOR
*HubDesc
;
865 EFI_USB_PORT_STATUS PortStatus
;
867 EFI_USB_DEVICE_REQUEST ClearPortRequest
;
871 // Validate parameters
873 if ((Request
== NULL
) || (TransferResult
== NULL
)) {
874 return EFI_INVALID_PARAMETER
;
877 if ((TransferDirection
!= EfiUsbDataIn
) &&
878 (TransferDirection
!= EfiUsbDataOut
) &&
879 (TransferDirection
!= EfiUsbNoData
)) {
880 return EFI_INVALID_PARAMETER
;
883 if ((TransferDirection
== EfiUsbNoData
) &&
884 ((Data
!= NULL
) || (*DataLength
!= 0))) {
885 return EFI_INVALID_PARAMETER
;
888 if ((TransferDirection
!= EfiUsbNoData
) &&
889 ((Data
== NULL
) || (*DataLength
== 0))) {
890 return EFI_INVALID_PARAMETER
;
893 if ((MaximumPacketLength
!= 8) && (MaximumPacketLength
!= 16) &&
894 (MaximumPacketLength
!= 32) && (MaximumPacketLength
!= 64) &&
895 (MaximumPacketLength
!= 512)
897 return EFI_INVALID_PARAMETER
;
900 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) {
901 return EFI_INVALID_PARAMETER
;
904 if ((DeviceSpeed
== EFI_USB_SPEED_SUPER
) && (MaximumPacketLength
!= 512)) {
905 return EFI_INVALID_PARAMETER
;
908 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
910 Xhc
= XHC_FROM_THIS (This
);
912 Status
= EFI_DEVICE_ERROR
;
913 *TransferResult
= EFI_USB_ERR_SYSTEM
;
916 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
917 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: HC halted at entrance\n"));
922 // Check if the device is still enabled before every transaction.
924 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
930 // Hook the Set_Address request from UsbBus.
931 // According to XHCI 1.0 spec, the Set_Address request is replaced by XHCI's Address_Device cmd.
933 if ((Request
->Request
== USB_REQ_SET_ADDRESS
) &&
934 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
936 // Reset the BusDevAddr field of all disabled entries in UsbDevContext array firstly.
937 // This way is used to clean the history to avoid using wrong device address by XhcAsyncInterruptTransfer().
939 for (Index
= 0; Index
< 255; Index
++) {
940 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
&&
941 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0) &&
942 (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== (UINT8
)Request
->Value
)) {
943 Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
= 0;
947 if (Xhc
->UsbDevContext
[SlotId
].XhciDevAddr
== 0) {
948 Status
= EFI_DEVICE_ERROR
;
952 // The actual device address has been assigned by XHCI during initializing the device slot.
953 // So we just need establish the mapping relationship between the device address requested from UsbBus
954 // and the actual device address assigned by XHCI. The the following invocations through EFI_USB2_HC_PROTOCOL interface
955 // can find out the actual device address by it.
957 Xhc
->UsbDevContext
[SlotId
].BusDevAddr
= (UINT8
)Request
->Value
;
958 Status
= EFI_SUCCESS
;
963 // Create a new URB, insert it into the asynchronous
964 // schedule list, then poll the execution status.
965 // Note that we encode the direction in address although default control
966 // endpoint is bidirectional. XhcCreateUrb expects this
967 // combination of Ep addr and its direction.
969 Endpoint
= (UINT8
) (0 | ((TransferDirection
== EfiUsbDataIn
) ? 0x80 : 0));
970 Status
= XhcTransfer (
984 if (EFI_ERROR (Status
)) {
989 // Hook Get_Descriptor request from UsbBus as we need evaluate context and configure endpoint.
990 // Hook Get_Status request form UsbBus as we need trace device attach/detach event happened at hub.
991 // Hook Set_Config request from UsbBus as we need configure device endpoint.
993 if ((Request
->Request
== USB_REQ_GET_DESCRIPTOR
) &&
994 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
)) ||
995 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_DEVICE
))))) {
996 DescriptorType
= (UINT8
)(Request
->Value
>> 8);
997 if ((DescriptorType
== USB_DESC_TYPE_DEVICE
) && ((*DataLength
== sizeof (EFI_USB_DEVICE_DESCRIPTOR
)) || ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (*DataLength
== 8)))) {
998 ASSERT (Data
!= NULL
);
1000 // Store a copy of device scriptor as hub device need this info to configure endpoint.
1002 CopyMem (&Xhc
->UsbDevContext
[SlotId
].DevDesc
, Data
, *DataLength
);
1003 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.BcdUSB
>= 0x0300) {
1005 // If it's a usb3.0 device, then its max packet size is a 2^n.
1007 MaxPacket0
= 1 << Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
1009 MaxPacket0
= Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
1011 Xhc
->UsbDevContext
[SlotId
].ConfDesc
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
* sizeof (EFI_USB_CONFIG_DESCRIPTOR
*));
1012 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1013 Status
= XhcEvaluateContext (Xhc
, SlotId
, MaxPacket0
);
1015 Status
= XhcEvaluateContext64 (Xhc
, SlotId
, MaxPacket0
);
1017 } else if (DescriptorType
== USB_DESC_TYPE_CONFIG
) {
1018 ASSERT (Data
!= NULL
);
1019 if (*DataLength
== ((UINT16
*)Data
)[1]) {
1021 // Get configuration value from request, Store the configuration descriptor for Configure_Endpoint cmd.
1023 Index
= (UINT8
)Request
->Value
;
1024 ASSERT (Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
);
1025 Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
] = AllocateZeroPool(*DataLength
);
1026 CopyMem (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
], Data
, *DataLength
);
1028 // Default to use AlternateSetting 0 for all interfaces.
1030 Xhc
->UsbDevContext
[SlotId
].ActiveAlternateSetting
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->NumInterfaces
* sizeof (UINT8
));
1032 } else if (((DescriptorType
== USB_DESC_TYPE_HUB
) ||
1033 (DescriptorType
== USB_DESC_TYPE_HUB_SUPER_SPEED
)) && (*DataLength
> 2)) {
1034 ASSERT (Data
!= NULL
);
1035 HubDesc
= (EFI_USB_HUB_DESCRIPTOR
*)Data
;
1036 ASSERT (HubDesc
->NumPorts
<= 15);
1038 // The bit 5,6 of HubCharacter field of Hub Descriptor is TTT.
1040 TTT
= (UINT8
)((HubDesc
->HubCharacter
& (BIT5
| BIT6
)) >> 5);
1041 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.DeviceProtocol
== 2) {
1043 // Don't support multi-TT feature for super speed hub now.
1046 DEBUG ((EFI_D_ERROR
, "XHCI: Don't support multi-TT feature for Hub now. (force to disable MTT)\n"));
1051 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1052 Status
= XhcConfigHubContext (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
1054 Status
= XhcConfigHubContext64 (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
1057 } else if ((Request
->Request
== USB_REQ_SET_CONFIG
) &&
1058 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
1060 // Hook Set_Config request from UsbBus as we need configure device endpoint.
1062 for (Index
= 0; Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
; Index
++) {
1063 if (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->ConfigurationValue
== (UINT8
)Request
->Value
) {
1064 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1065 Status
= XhcSetConfigCmd (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
1067 Status
= XhcSetConfigCmd64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
1072 } else if ((Request
->Request
== USB_REQ_SET_INTERFACE
) &&
1073 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_INTERFACE
))) {
1075 // Hook Set_Interface request from UsbBus as we need configure interface setting.
1076 // Request->Value indicates AlterlateSetting to set
1077 // Request->Index indicates Interface to set
1079 if (Xhc
->UsbDevContext
[SlotId
].ActiveAlternateSetting
[(UINT8
) Request
->Index
] != (UINT8
) Request
->Value
) {
1080 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1081 Status
= XhcSetInterface (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Xhc
->UsbDevContext
[SlotId
].ActiveConfiguration
- 1], Request
);
1083 Status
= XhcSetInterface64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Xhc
->UsbDevContext
[SlotId
].ActiveConfiguration
- 1], Request
);
1086 } else if ((Request
->Request
== USB_REQ_GET_STATUS
) &&
1087 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
))) {
1088 ASSERT (Data
!= NULL
);
1090 // Hook Get_Status request from UsbBus to keep track of the port status change.
1092 State
= *(UINT32
*)Data
;
1093 PortStatus
.PortStatus
= 0;
1094 PortStatus
.PortChangeStatus
= 0;
1096 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
1098 // For super speed hub, its bit10~12 presents the attached device speed.
1100 if ((State
& XHC_PORTSC_PS
) >> 10 == 0) {
1101 PortStatus
.PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
1105 // For high or full/low speed hub, its bit9~10 presents the attached device speed.
1107 if (XHC_BIT_IS_SET (State
, BIT9
)) {
1108 PortStatus
.PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
1109 } else if (XHC_BIT_IS_SET (State
, BIT10
)) {
1110 PortStatus
.PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
1115 // Convert the XHCI port/port change state to UEFI status
1117 MapSize
= sizeof (mUsbHubPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
1118 for (Index
= 0; Index
< MapSize
; Index
++) {
1119 if (XHC_BIT_IS_SET (State
, mUsbHubPortStateMap
[Index
].HwState
)) {
1120 PortStatus
.PortStatus
= (UINT16
) (PortStatus
.PortStatus
| mUsbHubPortStateMap
[Index
].UefiState
);
1124 MapSize
= sizeof (mUsbHubPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
1125 for (Index
= 0; Index
< MapSize
; Index
++) {
1126 if (XHC_BIT_IS_SET (State
, mUsbHubPortChangeMap
[Index
].HwState
)) {
1127 PortStatus
.PortChangeStatus
= (UINT16
) (PortStatus
.PortChangeStatus
| mUsbHubPortChangeMap
[Index
].UefiState
);
1131 MapSize
= sizeof (mUsbHubClearPortChangeMap
) / sizeof (USB_CLEAR_PORT_MAP
);
1133 for (Index
= 0; Index
< MapSize
; Index
++) {
1134 if (XHC_BIT_IS_SET (State
, mUsbHubClearPortChangeMap
[Index
].HwState
)) {
1135 ZeroMem (&ClearPortRequest
, sizeof (EFI_USB_DEVICE_REQUEST
));
1136 ClearPortRequest
.RequestType
= USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
);
1137 ClearPortRequest
.Request
= (UINT8
) USB_REQ_CLEAR_FEATURE
;
1138 ClearPortRequest
.Value
= mUsbHubClearPortChangeMap
[Index
].Selector
;
1139 ClearPortRequest
.Index
= Request
->Index
;
1140 ClearPortRequest
.Length
= 0;
1142 XhcControlTransfer (
1146 MaximumPacketLength
,
1158 XhcPollPortStatusChange (Xhc
, Xhc
->UsbDevContext
[SlotId
].RouteString
, (UINT8
)Request
->Index
, &PortStatus
);
1160 *(UINT32
*)Data
= *(UINT32
*)&PortStatus
;
1164 if (EFI_ERROR (Status
)) {
1165 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1168 gBS
->RestoreTPL (OldTpl
);
1175 Submits bulk transfer to a bulk endpoint of a USB device.
1177 @param This This EFI_USB2_HC_PROTOCOL instance.
1178 @param DeviceAddress Target device address.
1179 @param EndPointAddress Endpoint number and its direction in bit 7.
1180 @param DeviceSpeed Device speed, Low speed device doesn't support bulk
1182 @param MaximumPacketLength Maximum packet size the endpoint is capable of
1183 sending or receiving.
1184 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1185 @param Data Array of pointers to the buffers of data to transmit
1186 from or receive into.
1187 @param DataLength The lenght of the data buffer.
1188 @param DataToggle On input, the initial data toggle for the transfer;
1189 On output, it is updated to to next data toggle to
1190 use of the subsequent bulk transfer.
1191 @param Timeout Indicates the maximum time, in millisecond, which
1192 the transfer is allowed to complete.
1193 @param Translator A pointr to the transaction translator data.
1194 @param TransferResult A pointer to the detailed result information of the
1197 @retval EFI_SUCCESS The transfer was completed successfully.
1198 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1199 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1200 @retval EFI_TIMEOUT The transfer failed due to timeout.
1201 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1207 IN EFI_USB2_HC_PROTOCOL
*This
,
1208 IN UINT8 DeviceAddress
,
1209 IN UINT8 EndPointAddress
,
1210 IN UINT8 DeviceSpeed
,
1211 IN UINTN MaximumPacketLength
,
1212 IN UINT8 DataBuffersNumber
,
1213 IN OUT VOID
*Data
[EFI_USB_MAX_BULK_BUFFER_NUM
],
1214 IN OUT UINTN
*DataLength
,
1215 IN OUT UINT8
*DataToggle
,
1217 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1218 OUT UINT32
*TransferResult
1221 USB_XHCI_INSTANCE
*Xhc
;
1227 // Validate the parameters
1229 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1230 (Data
== NULL
) || (Data
[0] == NULL
) || (TransferResult
== NULL
)) {
1231 return EFI_INVALID_PARAMETER
;
1234 if ((*DataToggle
!= 0) && (*DataToggle
!= 1)) {
1235 return EFI_INVALID_PARAMETER
;
1238 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) ||
1239 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1240 ((EFI_USB_SPEED_HIGH
== DeviceSpeed
) && (MaximumPacketLength
> 512)) ||
1241 ((EFI_USB_SPEED_SUPER
== DeviceSpeed
) && (MaximumPacketLength
> 1024))) {
1242 return EFI_INVALID_PARAMETER
;
1245 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1247 Xhc
= XHC_FROM_THIS (This
);
1249 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1250 Status
= EFI_DEVICE_ERROR
;
1252 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1253 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: HC is halted\n"));
1258 // Check if the device is still enabled before every transaction.
1260 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1266 // Create a new URB, insert it into the asynchronous
1267 // schedule list, then poll the execution status.
1269 Status
= XhcTransfer (
1274 MaximumPacketLength
,
1284 if (EFI_ERROR (Status
)) {
1285 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1287 gBS
->RestoreTPL (OldTpl
);
1293 Submits an asynchronous interrupt transfer to an
1294 interrupt endpoint of a USB device.
1296 @param This This EFI_USB2_HC_PROTOCOL instance.
1297 @param DeviceAddress Target device address.
1298 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1299 @param DeviceSpeed Indicates device speed.
1300 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1301 @param IsNewTransfer If TRUE, to submit an new asynchronous interrupt
1302 transfer If FALSE, to remove the specified
1303 asynchronous interrupt.
1304 @param DataToggle On input, the initial data toggle to use; on output,
1305 it is updated to indicate the next data toggle.
1306 @param PollingInterval The he interval, in milliseconds, that the transfer
1308 @param DataLength The length of data to receive at the rate specified
1310 @param Translator Transaction translator to use.
1311 @param CallBackFunction Function to call at the rate specified by
1313 @param Context Context to CallBackFunction.
1315 @retval EFI_SUCCESS The request has been successfully submitted or canceled.
1316 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1317 @retval EFI_OUT_OF_RESOURCES The request failed due to a lack of resources.
1318 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1323 XhcAsyncInterruptTransfer (
1324 IN EFI_USB2_HC_PROTOCOL
*This
,
1325 IN UINT8 DeviceAddress
,
1326 IN UINT8 EndPointAddress
,
1327 IN UINT8 DeviceSpeed
,
1328 IN UINTN MaximumPacketLength
,
1329 IN BOOLEAN IsNewTransfer
,
1330 IN OUT UINT8
*DataToggle
,
1331 IN UINTN PollingInterval
,
1332 IN UINTN DataLength
,
1333 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1334 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction
,
1335 IN VOID
*Context OPTIONAL
1338 USB_XHCI_INSTANCE
*Xhc
;
1346 // Validate parameters
1348 if (!XHCI_IS_DATAIN (EndPointAddress
)) {
1349 return EFI_INVALID_PARAMETER
;
1352 if (IsNewTransfer
) {
1353 if (DataLength
== 0) {
1354 return EFI_INVALID_PARAMETER
;
1357 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1358 return EFI_INVALID_PARAMETER
;
1361 if ((PollingInterval
> 255) || (PollingInterval
< 1)) {
1362 return EFI_INVALID_PARAMETER
;
1366 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1368 Xhc
= XHC_FROM_THIS (This
);
1371 // Delete Async interrupt transfer request.
1373 if (!IsNewTransfer
) {
1375 // The delete request may happen after device is detached.
1377 for (Index
= 0; Index
< 255; Index
++) {
1378 if (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== DeviceAddress
) {
1384 Status
= EFI_INVALID_PARAMETER
;
1388 Status
= XhciDelAsyncIntTransfer (Xhc
, DeviceAddress
, EndPointAddress
);
1389 DEBUG ((EFI_D_INFO
, "XhcAsyncInterruptTransfer: remove old transfer for addr %d, Status = %r\n", DeviceAddress
, Status
));
1393 Status
= EFI_SUCCESS
;
1395 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1396 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: HC is halt\n"));
1397 Status
= EFI_DEVICE_ERROR
;
1402 // Check if the device is still enabled before every transaction.
1404 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1409 Urb
= XhciInsertAsyncIntTransfer (
1414 MaximumPacketLength
,
1420 Status
= EFI_OUT_OF_RESOURCES
;
1425 // Ring the doorbell
1427 Status
= RingIntTransferDoorBell (Xhc
, Urb
);
1430 Xhc
->PciIo
->Flush (Xhc
->PciIo
);
1431 gBS
->RestoreTPL (OldTpl
);
1438 Submits synchronous interrupt transfer to an interrupt endpoint
1441 @param This This EFI_USB2_HC_PROTOCOL instance.
1442 @param DeviceAddress Target device address.
1443 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1444 @param DeviceSpeed Indicates device speed.
1445 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1446 of sending or receiving.
1447 @param Data Buffer of data that will be transmitted to USB
1448 device or received from USB device.
1449 @param DataLength On input, the size, in bytes, of the data buffer; On
1450 output, the number of bytes transferred.
1451 @param DataToggle On input, the initial data toggle to use; on output,
1452 it is updated to indicate the next data toggle.
1453 @param Timeout Maximum time, in second, to complete.
1454 @param Translator Transaction translator to use.
1455 @param TransferResult Variable to receive the transfer result.
1457 @return EFI_SUCCESS The transfer was completed successfully.
1458 @return EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1459 @return EFI_INVALID_PARAMETER Some parameters are invalid.
1460 @return EFI_TIMEOUT The transfer failed due to timeout.
1461 @return EFI_DEVICE_ERROR The failed due to host controller or device error
1466 XhcSyncInterruptTransfer (
1467 IN EFI_USB2_HC_PROTOCOL
*This
,
1468 IN UINT8 DeviceAddress
,
1469 IN UINT8 EndPointAddress
,
1470 IN UINT8 DeviceSpeed
,
1471 IN UINTN MaximumPacketLength
,
1473 IN OUT UINTN
*DataLength
,
1474 IN OUT UINT8
*DataToggle
,
1476 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1477 OUT UINT32
*TransferResult
1480 USB_XHCI_INSTANCE
*Xhc
;
1486 // Validates parameters
1488 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1489 (Data
== NULL
) || (TransferResult
== NULL
)) {
1490 return EFI_INVALID_PARAMETER
;
1493 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1494 return EFI_INVALID_PARAMETER
;
1497 if (((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) ||
1498 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1499 ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) && (MaximumPacketLength
> 3072))) {
1500 return EFI_INVALID_PARAMETER
;
1503 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1505 Xhc
= XHC_FROM_THIS (This
);
1507 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1508 Status
= EFI_DEVICE_ERROR
;
1510 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1511 DEBUG ((EFI_D_ERROR
, "EhcSyncInterruptTransfer: HC is halt\n"));
1516 // Check if the device is still enabled before every transaction.
1518 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1523 Status
= XhcTransfer (
1528 MaximumPacketLength
,
1529 XHC_INT_TRANSFER_SYNC
,
1538 if (EFI_ERROR (Status
)) {
1539 DEBUG ((EFI_D_ERROR
, "XhcSyncInterruptTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1541 gBS
->RestoreTPL (OldTpl
);
1548 Submits isochronous transfer to a target USB device.
1550 @param This This EFI_USB2_HC_PROTOCOL instance.
1551 @param DeviceAddress Target device address.
1552 @param EndPointAddress End point address with its direction.
1553 @param DeviceSpeed Device speed, Low speed device doesn't support this
1555 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1556 sending or receiving.
1557 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1558 @param Data Array of pointers to the buffers of data that will
1559 be transmitted to USB device or received from USB
1561 @param DataLength The size, in bytes, of the data buffer.
1562 @param Translator Transaction translator to use.
1563 @param TransferResult Variable to receive the transfer result.
1565 @return EFI_UNSUPPORTED Isochronous transfer is unsupported.
1570 XhcIsochronousTransfer (
1571 IN EFI_USB2_HC_PROTOCOL
*This
,
1572 IN UINT8 DeviceAddress
,
1573 IN UINT8 EndPointAddress
,
1574 IN UINT8 DeviceSpeed
,
1575 IN UINTN MaximumPacketLength
,
1576 IN UINT8 DataBuffersNumber
,
1577 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1578 IN UINTN DataLength
,
1579 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1580 OUT UINT32
*TransferResult
1583 return EFI_UNSUPPORTED
;
1588 Submits Async isochronous transfer to a target USB device.
1590 @param This This EFI_USB2_HC_PROTOCOL instance.
1591 @param DeviceAddress Target device address.
1592 @param EndPointAddress End point address with its direction.
1593 @param DeviceSpeed Device speed, Low speed device doesn't support this
1595 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1596 sending or receiving.
1597 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1598 @param Data Array of pointers to the buffers of data that will
1599 be transmitted to USB device or received from USB
1601 @param DataLength The size, in bytes, of the data buffer.
1602 @param Translator Transaction translator to use.
1603 @param IsochronousCallBack Function to be called when the transfer complete.
1604 @param Context Context passed to the call back function as
1607 @return EFI_UNSUPPORTED Isochronous transfer isn't supported.
1612 XhcAsyncIsochronousTransfer (
1613 IN EFI_USB2_HC_PROTOCOL
*This
,
1614 IN UINT8 DeviceAddress
,
1615 IN UINT8 EndPointAddress
,
1616 IN UINT8 DeviceSpeed
,
1617 IN UINTN MaximumPacketLength
,
1618 IN UINT8 DataBuffersNumber
,
1619 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1620 IN UINTN DataLength
,
1621 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1622 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
1626 return EFI_UNSUPPORTED
;
1630 Entry point for EFI drivers.
1632 @param ImageHandle EFI_HANDLE.
1633 @param SystemTable EFI_SYSTEM_TABLE.
1635 @retval EFI_SUCCESS Success.
1636 @retval Others Fail.
1641 XhcDriverEntryPoint (
1642 IN EFI_HANDLE ImageHandle
,
1643 IN EFI_SYSTEM_TABLE
*SystemTable
1646 return EfiLibInstallDriverBindingComponentName2 (
1649 &gXhciDriverBinding
,
1651 &gXhciComponentName
,
1652 &gXhciComponentName2
1658 Test to see if this driver supports ControllerHandle. Any
1659 ControllerHandle that has Usb2HcProtocol installed will
1662 @param This Protocol instance pointer.
1663 @param Controller Handle of device to test.
1664 @param RemainingDevicePath Not used.
1666 @return EFI_SUCCESS This driver supports this device.
1667 @return EFI_UNSUPPORTED This driver does not support this device.
1672 XhcDriverBindingSupported (
1673 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1674 IN EFI_HANDLE Controller
,
1675 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1679 EFI_PCI_IO_PROTOCOL
*PciIo
;
1680 USB_CLASSC UsbClassCReg
;
1683 // Test whether there is PCI IO Protocol attached on the controller handle.
1685 Status
= gBS
->OpenProtocol (
1687 &gEfiPciIoProtocolGuid
,
1689 This
->DriverBindingHandle
,
1691 EFI_OPEN_PROTOCOL_BY_DRIVER
1694 if (EFI_ERROR (Status
)) {
1695 return EFI_UNSUPPORTED
;
1698 Status
= PciIo
->Pci
.Read (
1701 PCI_CLASSCODE_OFFSET
,
1702 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1706 if (EFI_ERROR (Status
)) {
1707 Status
= EFI_UNSUPPORTED
;
1712 // Test whether the controller belongs to Xhci type
1714 if ((UsbClassCReg
.BaseCode
!= PCI_CLASS_SERIAL
) ||
1715 (UsbClassCReg
.SubClassCode
!= PCI_CLASS_SERIAL_USB
) ||
1716 (UsbClassCReg
.ProgInterface
!= PCI_IF_XHCI
)) {
1717 Status
= EFI_UNSUPPORTED
;
1721 gBS
->CloseProtocol (
1723 &gEfiPciIoProtocolGuid
,
1724 This
->DriverBindingHandle
,
1732 Create and initialize a USB_XHCI_INSTANCE structure.
1734 @param PciIo The PciIo on this device.
1735 @param DevicePath The device path of host controller.
1736 @param OriginalPciAttributes Original PCI attributes.
1738 @return The allocated and initialized USB_XHCI_INSTANCE structure if created,
1744 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1745 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1746 IN UINT64 OriginalPciAttributes
1749 USB_XHCI_INSTANCE
*Xhc
;
1753 UINT8 ReleaseNumber
;
1755 Xhc
= AllocateZeroPool (sizeof (USB_XHCI_INSTANCE
));
1762 // Initialize private data structure
1764 Xhc
->Signature
= XHCI_INSTANCE_SIG
;
1766 Xhc
->DevicePath
= DevicePath
;
1767 Xhc
->OriginalPciAttributes
= OriginalPciAttributes
;
1768 CopyMem (&Xhc
->Usb2Hc
, &gXhciUsb2HcTemplate
, sizeof (EFI_USB2_HC_PROTOCOL
));
1770 Status
= PciIo
->Pci
.Read (
1773 XHC_PCI_SBRN_OFFSET
,
1778 if (!EFI_ERROR (Status
)) {
1779 Xhc
->Usb2Hc
.MajorRevision
= (ReleaseNumber
& 0xF0) >> 4;
1780 Xhc
->Usb2Hc
.MinorRevision
= (ReleaseNumber
& 0x0F);
1783 InitializeListHead (&Xhc
->AsyncIntTransfers
);
1786 // Be caution that the Offset passed to XhcReadCapReg() should be Dword align
1788 Xhc
->CapLength
= XhcReadCapReg8 (Xhc
, XHC_CAPLENGTH_OFFSET
);
1789 Xhc
->HcSParams1
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS1_OFFSET
);
1790 Xhc
->HcSParams2
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS2_OFFSET
);
1791 Xhc
->HcCParams
.Dword
= XhcReadCapReg (Xhc
, XHC_HCCPARAMS_OFFSET
);
1792 Xhc
->DBOff
= XhcReadCapReg (Xhc
, XHC_DBOFF_OFFSET
);
1793 Xhc
->RTSOff
= XhcReadCapReg (Xhc
, XHC_RTSOFF_OFFSET
);
1796 // This PageSize field defines the page size supported by the xHC implementation.
1797 // This xHC supports a page size of 2^(n+12) if bit n is Set. For example,
1798 // if bit 0 is Set, the xHC supports 4k byte page sizes.
1800 PageSize
= XhcReadOpReg(Xhc
, XHC_PAGESIZE_OFFSET
) & XHC_PAGESIZE_MASK
;
1801 Xhc
->PageSize
= 1 << (HighBitSet32(PageSize
) + 12);
1803 ExtCapReg
= (UINT16
) (Xhc
->HcCParams
.Data
.ExtCapReg
);
1804 Xhc
->ExtCapRegBase
= ExtCapReg
<< 2;
1805 Xhc
->UsbLegSupOffset
= XhcGetCapabilityAddr (Xhc
, XHC_CAP_USB_LEGACY
);
1806 Xhc
->DebugCapSupOffset
= XhcGetCapabilityAddr (Xhc
, XHC_CAP_USB_DEBUG
);
1808 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: Capability length 0x%x\n", Xhc
->CapLength
));
1809 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcSParams1 0x%x\n", Xhc
->HcSParams1
));
1810 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcSParams2 0x%x\n", Xhc
->HcSParams2
));
1811 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcCParams 0x%x\n", Xhc
->HcCParams
));
1812 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: DBOff 0x%x\n", Xhc
->DBOff
));
1813 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: RTSOff 0x%x\n", Xhc
->RTSOff
));
1814 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: UsbLegSupOffset 0x%x\n", Xhc
->UsbLegSupOffset
));
1815 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: DebugCapSupOffset 0x%x\n", Xhc
->DebugCapSupOffset
));
1818 // Create AsyncRequest Polling Timer
1820 Status
= gBS
->CreateEvent (
1821 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1823 XhcMonitorAsyncRequests
,
1828 if (EFI_ERROR (Status
)) {
1840 One notified function to stop the Host Controller when gBS->ExitBootServices() called.
1842 @param Event Pointer to this event
1843 @param Context Event handler private data
1848 XhcExitBootService (
1854 USB_XHCI_INSTANCE
*Xhc
;
1855 EFI_PCI_IO_PROTOCOL
*PciIo
;
1857 Xhc
= (USB_XHCI_INSTANCE
*) Context
;
1861 // Stop AsyncRequest Polling timer then stop the XHCI driver
1862 // and uninstall the XHCI protocl.
1864 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
1865 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
1867 if (Xhc
->PollTimer
!= NULL
) {
1868 gBS
->CloseEvent (Xhc
->PollTimer
);
1871 XhcClearBiosOwnership (Xhc
);
1874 // Restore original PCI attributes
1878 EfiPciIoAttributeOperationSet
,
1879 Xhc
->OriginalPciAttributes
,
1885 Starting the Usb XHCI Driver.
1887 @param This Protocol instance pointer.
1888 @param Controller Handle of device to test.
1889 @param RemainingDevicePath Not used.
1891 @return EFI_SUCCESS supports this device.
1892 @return EFI_UNSUPPORTED do not support this device.
1893 @return EFI_DEVICE_ERROR cannot be started due to device Error.
1894 @return EFI_OUT_OF_RESOURCES cannot allocate resources.
1899 XhcDriverBindingStart (
1900 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1901 IN EFI_HANDLE Controller
,
1902 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1906 EFI_PCI_IO_PROTOCOL
*PciIo
;
1908 UINT64 OriginalPciAttributes
;
1909 BOOLEAN PciAttributesSaved
;
1910 USB_XHCI_INSTANCE
*Xhc
;
1911 EFI_DEVICE_PATH_PROTOCOL
*HcDevicePath
;
1914 // Open the PciIo Protocol, then enable the USB host controller
1916 Status
= gBS
->OpenProtocol (
1918 &gEfiPciIoProtocolGuid
,
1920 This
->DriverBindingHandle
,
1922 EFI_OPEN_PROTOCOL_BY_DRIVER
1925 if (EFI_ERROR (Status
)) {
1930 // Open Device Path Protocol for on USB host controller
1932 HcDevicePath
= NULL
;
1933 Status
= gBS
->OpenProtocol (
1935 &gEfiDevicePathProtocolGuid
,
1936 (VOID
**) &HcDevicePath
,
1937 This
->DriverBindingHandle
,
1939 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1942 PciAttributesSaved
= FALSE
;
1944 // Save original PCI attributes
1946 Status
= PciIo
->Attributes (
1948 EfiPciIoAttributeOperationGet
,
1950 &OriginalPciAttributes
1953 if (EFI_ERROR (Status
)) {
1956 PciAttributesSaved
= TRUE
;
1958 Status
= PciIo
->Attributes (
1960 EfiPciIoAttributeOperationSupported
,
1964 if (!EFI_ERROR (Status
)) {
1965 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
1966 Status
= PciIo
->Attributes (
1968 EfiPciIoAttributeOperationEnable
,
1974 if (EFI_ERROR (Status
)) {
1975 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to enable controller\n"));
1980 // Create then install USB2_HC_PROTOCOL
1982 Xhc
= XhcCreateUsbHc (PciIo
, HcDevicePath
, OriginalPciAttributes
);
1985 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to create USB2_HC\n"));
1986 return EFI_OUT_OF_RESOURCES
;
1990 // Enable 64-bit DMA support in the PCI layer if this controller
1993 if (Xhc
->HcCParams
.Data
.Ac64
!= 0) {
1994 Status
= PciIo
->Attributes (
1996 EfiPciIoAttributeOperationEnable
,
1997 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
2000 if (!EFI_ERROR (Status
)) {
2001 Xhc
->Support64BitDma
= TRUE
;
2004 "%a: failed to enable 64-bit DMA on 64-bit capable controller @ %p (%r)\n",
2005 __FUNCTION__
, Controller
, Status
));
2009 XhcSetBiosOwnership (Xhc
);
2011 XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
2012 ASSERT (XhcIsHalt (Xhc
));
2015 // After Chip Hardware Reset wait until the Controller Not Ready (CNR) flag
2016 // in the USBSTS is '0' before writing any xHC Operational or Runtime registers.
2018 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
2021 // Initialize the schedule
2026 // Start the Host Controller
2028 XhcRunHC(Xhc
, XHC_GENERIC_TIMEOUT
);
2031 // Start the asynchronous interrupt monitor
2033 Status
= gBS
->SetTimer (Xhc
->PollTimer
, TimerPeriodic
, XHC_ASYNC_TIMER_INTERVAL
);
2034 if (EFI_ERROR (Status
)) {
2035 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to start async interrupt monitor\n"));
2036 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2041 // Create event to stop the HC when exit boot service.
2043 Status
= gBS
->CreateEventEx (
2048 &gEfiEventExitBootServicesGuid
,
2049 &Xhc
->ExitBootServiceEvent
2051 if (EFI_ERROR (Status
)) {
2056 // Install the component name protocol, don't fail the start
2057 // because of something for display.
2061 gXhciComponentName
.SupportedLanguages
,
2062 &Xhc
->ControllerNameTable
,
2063 L
"eXtensible Host Controller (USB 3.0)",
2068 gXhciComponentName2
.SupportedLanguages
,
2069 &Xhc
->ControllerNameTable
,
2070 L
"eXtensible Host Controller (USB 3.0)",
2074 Status
= gBS
->InstallProtocolInterface (
2076 &gEfiUsb2HcProtocolGuid
,
2077 EFI_NATIVE_INTERFACE
,
2080 if (EFI_ERROR (Status
)) {
2081 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to install USB2_HC Protocol\n"));
2085 DEBUG ((EFI_D_INFO
, "XhcDriverBindingStart: XHCI started for controller @ %x\n", Controller
));
2089 gBS
->CloseEvent (Xhc
->PollTimer
);
2094 if (PciAttributesSaved
) {
2096 // Restore original PCI attributes
2100 EfiPciIoAttributeOperationSet
,
2101 OriginalPciAttributes
,
2106 gBS
->CloseProtocol (
2108 &gEfiPciIoProtocolGuid
,
2109 This
->DriverBindingHandle
,
2118 Stop this driver on ControllerHandle. Support stopping any child handles
2119 created by this driver.
2121 @param This Protocol instance pointer.
2122 @param Controller Handle of device to stop driver on.
2123 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
2124 @param ChildHandleBuffer List of handles for the children we need to stop.
2126 @return EFI_SUCCESS Success.
2127 @return EFI_DEVICE_ERROR Fail.
2132 XhcDriverBindingStop (
2133 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
2134 IN EFI_HANDLE Controller
,
2135 IN UINTN NumberOfChildren
,
2136 IN EFI_HANDLE
*ChildHandleBuffer
2140 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
2141 EFI_PCI_IO_PROTOCOL
*PciIo
;
2142 USB_XHCI_INSTANCE
*Xhc
;
2146 // Test whether the Controller handler passed in is a valid
2147 // Usb controller handle that should be supported, if not,
2148 // return the error status directly
2150 Status
= gBS
->OpenProtocol (
2152 &gEfiUsb2HcProtocolGuid
,
2154 This
->DriverBindingHandle
,
2156 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2159 if (EFI_ERROR (Status
)) {
2163 Status
= gBS
->UninstallProtocolInterface (
2165 &gEfiUsb2HcProtocolGuid
,
2169 if (EFI_ERROR (Status
)) {
2173 Xhc
= XHC_FROM_THIS (Usb2Hc
);
2177 // Stop AsyncRequest Polling timer then stop the XHCI driver
2178 // and uninstall the XHCI protocl.
2180 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
2183 // Disable the device slots occupied by these devices on its downstream ports.
2184 // Entry 0 is reserved.
2186 for (Index
= 0; Index
< 255; Index
++) {
2187 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
||
2188 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0)) {
2191 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
2192 XhcDisableSlotCmd (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2194 XhcDisableSlotCmd64 (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2198 if (Xhc
->PollTimer
!= NULL
) {
2199 gBS
->CloseEvent (Xhc
->PollTimer
);
2202 if (Xhc
->ExitBootServiceEvent
!= NULL
) {
2203 gBS
->CloseEvent (Xhc
->ExitBootServiceEvent
);
2206 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2207 XhcClearBiosOwnership (Xhc
);
2208 XhciDelAllAsyncIntTransfers (Xhc
);
2211 if (Xhc
->ControllerNameTable
) {
2212 FreeUnicodeStringTable (Xhc
->ControllerNameTable
);
2216 // Restore original PCI attributes
2220 EfiPciIoAttributeOperationSet
,
2221 Xhc
->OriginalPciAttributes
,
2225 gBS
->CloseProtocol (
2227 &gEfiPciIoProtocolGuid
,
2228 This
->DriverBindingHandle
,