2 The XHCI controller driver.
4 Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 // Two arrays used to translate the XHCI port state (change)
19 // to the UEFI protocol's port state (change).
21 USB_PORT_STATE_MAP mUsbPortStateMap
[] = {
22 {XHC_PORTSC_CCS
, USB_PORT_STAT_CONNECTION
},
23 {XHC_PORTSC_PED
, USB_PORT_STAT_ENABLE
},
24 {XHC_PORTSC_OCA
, USB_PORT_STAT_OVERCURRENT
},
25 {XHC_PORTSC_RESET
, USB_PORT_STAT_RESET
}
28 USB_PORT_STATE_MAP mUsbPortChangeMap
[] = {
29 {XHC_PORTSC_CSC
, USB_PORT_STAT_C_CONNECTION
},
30 {XHC_PORTSC_PEC
, USB_PORT_STAT_C_ENABLE
},
31 {XHC_PORTSC_OCC
, USB_PORT_STAT_C_OVERCURRENT
},
32 {XHC_PORTSC_PRC
, USB_PORT_STAT_C_RESET
}
35 USB_CLEAR_PORT_MAP mUsbClearPortChangeMap
[] = {
36 {XHC_PORTSC_CSC
, EfiUsbPortConnectChange
},
37 {XHC_PORTSC_PEC
, EfiUsbPortEnableChange
},
38 {XHC_PORTSC_OCC
, EfiUsbPortOverCurrentChange
},
39 {XHC_PORTSC_PRC
, EfiUsbPortResetChange
}
42 USB_PORT_STATE_MAP mUsbHubPortStateMap
[] = {
43 {XHC_HUB_PORTSC_CCS
, USB_PORT_STAT_CONNECTION
},
44 {XHC_HUB_PORTSC_PED
, USB_PORT_STAT_ENABLE
},
45 {XHC_HUB_PORTSC_OCA
, USB_PORT_STAT_OVERCURRENT
},
46 {XHC_HUB_PORTSC_RESET
, USB_PORT_STAT_RESET
}
49 USB_PORT_STATE_MAP mUsbHubPortChangeMap
[] = {
50 {XHC_HUB_PORTSC_CSC
, USB_PORT_STAT_C_CONNECTION
},
51 {XHC_HUB_PORTSC_PEC
, USB_PORT_STAT_C_ENABLE
},
52 {XHC_HUB_PORTSC_OCC
, USB_PORT_STAT_C_OVERCURRENT
},
53 {XHC_HUB_PORTSC_PRC
, USB_PORT_STAT_C_RESET
}
56 USB_CLEAR_PORT_MAP mUsbHubClearPortChangeMap
[] = {
57 {XHC_HUB_PORTSC_CSC
, EfiUsbPortConnectChange
},
58 {XHC_HUB_PORTSC_PEC
, EfiUsbPortEnableChange
},
59 {XHC_HUB_PORTSC_OCC
, EfiUsbPortOverCurrentChange
},
60 {XHC_HUB_PORTSC_PRC
, EfiUsbPortResetChange
},
61 {XHC_HUB_PORTSC_BHRC
, Usb3PortBHPortResetChange
}
64 EFI_DRIVER_BINDING_PROTOCOL gXhciDriverBinding
= {
65 XhcDriverBindingSupported
,
66 XhcDriverBindingStart
,
74 // Template for Xhci's Usb2 Host Controller Protocol Instance.
76 EFI_USB2_HC_PROTOCOL gXhciUsb2HcTemplate
= {
83 XhcAsyncInterruptTransfer
,
84 XhcSyncInterruptTransfer
,
85 XhcIsochronousTransfer
,
86 XhcAsyncIsochronousTransfer
,
87 XhcGetRootHubPortStatus
,
88 XhcSetRootHubPortFeature
,
89 XhcClearRootHubPortFeature
,
95 Retrieves the capability of root hub ports.
97 @param This The EFI_USB2_HC_PROTOCOL instance.
98 @param MaxSpeed Max speed supported by the controller.
99 @param PortNumber Number of the root hub ports.
100 @param Is64BitCapable Whether the controller supports 64-bit memory
103 @retval EFI_SUCCESS Host controller capability were retrieved successfully.
104 @retval EFI_INVALID_PARAMETER Either of the three capability pointer is NULL.
110 IN EFI_USB2_HC_PROTOCOL
*This
,
112 OUT UINT8
*PortNumber
,
113 OUT UINT8
*Is64BitCapable
116 USB_XHCI_INSTANCE
*Xhc
;
119 if ((MaxSpeed
== NULL
) || (PortNumber
== NULL
) || (Is64BitCapable
== NULL
)) {
120 return EFI_INVALID_PARAMETER
;
123 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
125 Xhc
= XHC_FROM_THIS (This
);
126 *MaxSpeed
= EFI_USB_SPEED_SUPER
;
127 *PortNumber
= (UINT8
) (Xhc
->HcSParams1
.Data
.MaxPorts
);
128 *Is64BitCapable
= (UINT8
) Xhc
->Support64BitDma
;
129 DEBUG ((EFI_D_INFO
, "XhcGetCapability: %d ports, 64 bit %d\n", *PortNumber
, *Is64BitCapable
));
131 gBS
->RestoreTPL (OldTpl
);
138 Provides software reset for the USB host controller.
140 @param This This EFI_USB2_HC_PROTOCOL instance.
141 @param Attributes A bit mask of the reset operation to perform.
143 @retval EFI_SUCCESS The reset operation succeeded.
144 @retval EFI_INVALID_PARAMETER Attributes is not valid.
145 @retval EFI_UNSUPPOURTED The type of reset specified by Attributes is
146 not currently supported by the host controller.
147 @retval EFI_DEVICE_ERROR Host controller isn't halted to reset.
153 IN EFI_USB2_HC_PROTOCOL
*This
,
157 USB_XHCI_INSTANCE
*Xhc
;
161 Xhc
= XHC_FROM_THIS (This
);
163 if (Xhc
->DevicePath
!= NULL
) {
165 // Report Status Code to indicate reset happens
167 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
169 (EFI_IO_BUS_USB
| EFI_IOB_PC_RESET
),
174 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
176 switch (Attributes
) {
177 case EFI_USB_HC_RESET_GLOBAL
:
179 // Flow through, same behavior as Host Controller Reset
181 case EFI_USB_HC_RESET_HOST_CONTROLLER
:
182 if ((Xhc
->DebugCapSupOffset
!= 0xFFFFFFFF) && ((XhcReadExtCapReg (Xhc
, Xhc
->DebugCapSupOffset
) & 0xFF) == XHC_CAP_USB_DEBUG
) &&
183 ((XhcReadExtCapReg (Xhc
, Xhc
->DebugCapSupOffset
+ XHC_DC_DCCTRL
) & BIT0
) != 0)) {
184 Status
= EFI_SUCCESS
;
188 // Host Controller must be Halt when Reset it
190 if (!XhcIsHalt (Xhc
)) {
191 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
193 if (EFI_ERROR (Status
)) {
194 Status
= EFI_DEVICE_ERROR
;
199 Status
= XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
200 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
202 if (EFI_ERROR (Status
)) {
206 // Clean up the asynchronous transfers, currently only
207 // interrupt supports asynchronous operation.
209 XhciDelAllAsyncIntTransfers (Xhc
);
215 case EFI_USB_HC_RESET_GLOBAL_WITH_DEBUG
:
216 case EFI_USB_HC_RESET_HOST_WITH_DEBUG
:
217 Status
= EFI_UNSUPPORTED
;
221 Status
= EFI_INVALID_PARAMETER
;
225 DEBUG ((EFI_D_INFO
, "XhcReset: status %r\n", Status
));
226 gBS
->RestoreTPL (OldTpl
);
233 Retrieve the current state of the USB host controller.
235 @param This This EFI_USB2_HC_PROTOCOL instance.
236 @param State Variable to return the current host controller
239 @retval EFI_SUCCESS Host controller state was returned in State.
240 @retval EFI_INVALID_PARAMETER State is NULL.
241 @retval EFI_DEVICE_ERROR An error was encountered while attempting to
242 retrieve the host controller's current state.
248 IN EFI_USB2_HC_PROTOCOL
*This
,
249 OUT EFI_USB_HC_STATE
*State
252 USB_XHCI_INSTANCE
*Xhc
;
256 return EFI_INVALID_PARAMETER
;
259 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
261 Xhc
= XHC_FROM_THIS (This
);
263 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
264 *State
= EfiUsbHcStateHalt
;
266 *State
= EfiUsbHcStateOperational
;
269 DEBUG ((EFI_D_INFO
, "XhcGetState: current state %d\n", *State
));
270 gBS
->RestoreTPL (OldTpl
);
276 Sets the USB host controller to a specific state.
278 @param This This EFI_USB2_HC_PROTOCOL instance.
279 @param State The state of the host controller that will be set.
281 @retval EFI_SUCCESS The USB host controller was successfully placed
282 in the state specified by State.
283 @retval EFI_INVALID_PARAMETER State is invalid.
284 @retval EFI_DEVICE_ERROR Failed to set the state due to device error.
290 IN EFI_USB2_HC_PROTOCOL
*This
,
291 IN EFI_USB_HC_STATE State
294 USB_XHCI_INSTANCE
*Xhc
;
296 EFI_USB_HC_STATE CurState
;
299 Status
= XhcGetState (This
, &CurState
);
301 if (EFI_ERROR (Status
)) {
302 return EFI_DEVICE_ERROR
;
305 if (CurState
== State
) {
309 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
311 Xhc
= XHC_FROM_THIS (This
);
314 case EfiUsbHcStateHalt
:
315 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
318 case EfiUsbHcStateOperational
:
319 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HSE
)) {
320 Status
= EFI_DEVICE_ERROR
;
325 // Software must not write a one to this field unless the host controller
326 // is in the Halted state. Doing so will yield undefined results.
327 // refers to Spec[XHCI1.0-2.3.1]
329 if (!XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
330 Status
= EFI_DEVICE_ERROR
;
334 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
337 case EfiUsbHcStateSuspend
:
338 Status
= EFI_UNSUPPORTED
;
342 Status
= EFI_INVALID_PARAMETER
;
345 DEBUG ((EFI_D_INFO
, "XhcSetState: status %r\n", Status
));
346 gBS
->RestoreTPL (OldTpl
);
352 Retrieves the current status of a USB root hub port.
354 @param This This EFI_USB2_HC_PROTOCOL instance.
355 @param PortNumber The root hub port to retrieve the state from.
356 This value is zero-based.
357 @param PortStatus Variable to receive the port state.
359 @retval EFI_SUCCESS The status of the USB root hub port specified.
360 by PortNumber was returned in PortStatus.
361 @retval EFI_INVALID_PARAMETER PortNumber is invalid.
362 @retval EFI_DEVICE_ERROR Can't read register.
367 XhcGetRootHubPortStatus (
368 IN EFI_USB2_HC_PROTOCOL
*This
,
370 OUT EFI_USB_PORT_STATUS
*PortStatus
373 USB_XHCI_INSTANCE
*Xhc
;
380 USB_DEV_ROUTE ParentRouteChart
;
383 if (PortStatus
== NULL
) {
384 return EFI_INVALID_PARAMETER
;
387 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
389 Xhc
= XHC_FROM_THIS (This
);
390 Status
= EFI_SUCCESS
;
392 TotalPort
= Xhc
->HcSParams1
.Data
.MaxPorts
;
394 if (PortNumber
>= TotalPort
) {
395 Status
= EFI_INVALID_PARAMETER
;
399 Offset
= (UINT32
) (XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
400 PortStatus
->PortStatus
= 0;
401 PortStatus
->PortChangeStatus
= 0;
403 State
= XhcReadOpReg (Xhc
, Offset
);
406 // According to XHCI 1.0 spec, bit 10~13 of the root port status register identifies the speed of the attached device.
408 switch ((State
& XHC_PORTSC_PS
) >> 10) {
410 PortStatus
->PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
414 PortStatus
->PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
418 PortStatus
->PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
426 // Convert the XHCI port/port change state to UEFI status
428 MapSize
= sizeof (mUsbPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
430 for (Index
= 0; Index
< MapSize
; Index
++) {
431 if (XHC_BIT_IS_SET (State
, mUsbPortStateMap
[Index
].HwState
)) {
432 PortStatus
->PortStatus
= (UINT16
) (PortStatus
->PortStatus
| mUsbPortStateMap
[Index
].UefiState
);
436 // Bit5~8 reflects its current link state.
438 if ((State
& XHC_PORTSC_PLS
) >> 5 == 3) {
439 PortStatus
->PortStatus
|= USB_PORT_STAT_SUSPEND
;
442 MapSize
= sizeof (mUsbPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
444 for (Index
= 0; Index
< MapSize
; Index
++) {
445 if (XHC_BIT_IS_SET (State
, mUsbPortChangeMap
[Index
].HwState
)) {
446 PortStatus
->PortChangeStatus
= (UINT16
) (PortStatus
->PortChangeStatus
| mUsbPortChangeMap
[Index
].UefiState
);
450 MapSize
= sizeof (mUsbClearPortChangeMap
) / sizeof (USB_CLEAR_PORT_MAP
);
452 for (Index
= 0; Index
< MapSize
; Index
++) {
453 if (XHC_BIT_IS_SET (State
, mUsbClearPortChangeMap
[Index
].HwState
)) {
454 XhcClearRootHubPortFeature (This
, PortNumber
, (EFI_USB_PORT_FEATURE
)mUsbClearPortChangeMap
[Index
].Selector
);
459 // Poll the root port status register to enable/disable corresponding device slot if there is a device attached/detached.
460 // For those devices behind hub, we get its attach/detach event by hooking Get_Port_Status request at control transfer for those hub.
462 ParentRouteChart
.Dword
= 0;
463 XhcPollPortStatusChange (Xhc
, ParentRouteChart
, PortNumber
, PortStatus
);
466 gBS
->RestoreTPL (OldTpl
);
472 Sets a feature for the specified root hub port.
474 @param This This EFI_USB2_HC_PROTOCOL instance.
475 @param PortNumber Root hub port to set.
476 @param PortFeature Feature to set.
478 @retval EFI_SUCCESS The feature specified by PortFeature was set.
479 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
480 @retval EFI_DEVICE_ERROR Can't read register.
485 XhcSetRootHubPortFeature (
486 IN EFI_USB2_HC_PROTOCOL
*This
,
488 IN EFI_USB_PORT_FEATURE PortFeature
491 USB_XHCI_INSTANCE
*Xhc
;
498 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
500 Xhc
= XHC_FROM_THIS (This
);
501 Status
= EFI_SUCCESS
;
503 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
505 if (PortNumber
>= TotalPort
) {
506 Status
= EFI_INVALID_PARAMETER
;
510 Offset
= (UINT32
) (XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
511 State
= XhcReadOpReg (Xhc
, Offset
);
514 // Mask off the port status change bits, these bits are
517 State
&= ~ (BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
519 switch (PortFeature
) {
520 case EfiUsbPortEnable
:
522 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
523 // A port may be disabled by software writing a '1' to this flag.
525 Status
= EFI_SUCCESS
;
528 case EfiUsbPortSuspend
:
529 State
|= XHC_PORTSC_LWS
;
530 XhcWriteOpReg (Xhc
, Offset
, State
);
531 State
&= ~XHC_PORTSC_PLS
;
533 XhcWriteOpReg (Xhc
, Offset
, State
);
536 case EfiUsbPortReset
:
537 DEBUG ((EFI_D_INFO
, "XhcUsbPortReset!\n"));
539 // Make sure Host Controller not halt before reset it
541 if (XhcIsHalt (Xhc
)) {
542 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
544 if (EFI_ERROR (Status
)) {
545 DEBUG ((EFI_D_INFO
, "XhcSetRootHubPortFeature :failed to start HC - %r\n", Status
));
551 // 4.3.1 Resetting a Root Hub Port
552 // 1) Write the PORTSC register with the Port Reset (PR) bit set to '1'.
554 State
|= XHC_PORTSC_RESET
;
555 XhcWriteOpReg (Xhc
, Offset
, State
);
556 XhcWaitOpRegBit(Xhc
, Offset
, XHC_PORTSC_PRC
, TRUE
, XHC_GENERIC_TIMEOUT
);
559 case EfiUsbPortPower
:
561 // Not supported, ignore the operation
563 Status
= EFI_SUCCESS
;
566 case EfiUsbPortOwner
:
568 // XHCI root hub port don't has the owner bit, ignore the operation
570 Status
= EFI_SUCCESS
;
574 Status
= EFI_INVALID_PARAMETER
;
578 DEBUG ((EFI_D_INFO
, "XhcSetRootHubPortFeature: status %r\n", Status
));
579 gBS
->RestoreTPL (OldTpl
);
586 Clears a feature for the specified root hub port.
588 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
589 @param PortNumber Specifies the root hub port whose feature is
590 requested to be cleared.
591 @param PortFeature Indicates the feature selector associated with the
592 feature clear request.
594 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
595 for the USB root hub port specified by PortNumber.
596 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
597 @retval EFI_DEVICE_ERROR Can't read register.
602 XhcClearRootHubPortFeature (
603 IN EFI_USB2_HC_PROTOCOL
*This
,
605 IN EFI_USB_PORT_FEATURE PortFeature
608 USB_XHCI_INSTANCE
*Xhc
;
615 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
617 Xhc
= XHC_FROM_THIS (This
);
618 Status
= EFI_SUCCESS
;
620 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
622 if (PortNumber
>= TotalPort
) {
623 Status
= EFI_INVALID_PARAMETER
;
627 Offset
= XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
);
630 // Mask off the port status change bits, these bits are
633 State
= XhcReadOpReg (Xhc
, Offset
);
634 State
&= ~ (BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
636 switch (PortFeature
) {
637 case EfiUsbPortEnable
:
639 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
640 // A port may be disabled by software writing a '1' to this flag.
642 State
|= XHC_PORTSC_PED
;
643 State
&= ~XHC_PORTSC_RESET
;
644 XhcWriteOpReg (Xhc
, Offset
, State
);
647 case EfiUsbPortSuspend
:
648 State
|= XHC_PORTSC_LWS
;
649 XhcWriteOpReg (Xhc
, Offset
, State
);
650 State
&= ~XHC_PORTSC_PLS
;
651 XhcWriteOpReg (Xhc
, Offset
, State
);
654 case EfiUsbPortReset
:
656 // PORTSC_RESET BIT(4) bit is RW1S attribute, which means Write-1-to-set status:
657 // Register bits indicate status when read, a clear bit may be set by
658 // writing a '1'. Writing a '0' to RW1S bits has no effect.
662 case EfiUsbPortOwner
:
664 // XHCI root hub port don't has the owner bit, ignore the operation
668 case EfiUsbPortConnectChange
:
670 // Clear connect status change
672 State
|= XHC_PORTSC_CSC
;
673 XhcWriteOpReg (Xhc
, Offset
, State
);
676 case EfiUsbPortEnableChange
:
678 // Clear enable status change
680 State
|= XHC_PORTSC_PEC
;
681 XhcWriteOpReg (Xhc
, Offset
, State
);
684 case EfiUsbPortOverCurrentChange
:
686 // Clear PortOverCurrent change
688 State
|= XHC_PORTSC_OCC
;
689 XhcWriteOpReg (Xhc
, Offset
, State
);
692 case EfiUsbPortResetChange
:
694 // Clear Port Reset change
696 State
|= XHC_PORTSC_PRC
;
697 XhcWriteOpReg (Xhc
, Offset
, State
);
700 case EfiUsbPortPower
:
701 case EfiUsbPortSuspendChange
:
703 // Not supported or not related operation
708 Status
= EFI_INVALID_PARAMETER
;
713 DEBUG ((EFI_D_INFO
, "XhcClearRootHubPortFeature: status %r\n", Status
));
714 gBS
->RestoreTPL (OldTpl
);
720 Submits a new transaction to a target USB device.
722 @param Xhc The XHCI Instance.
723 @param DeviceAddress The target device address.
724 @param EndPointAddress Endpoint number and its direction encoded in bit 7
725 @param DeviceSpeed Target device speed.
726 @param MaximumPacketLength Maximum packet size the default control transfer
727 endpoint is capable of sending or receiving.
728 @param Type The transaction type.
729 @param Request USB device request to send.
730 @param Data Data buffer to be transmitted or received from USB
732 @param DataLength The size (in bytes) of the data buffer.
733 @param Timeout Indicates the maximum timeout, in millisecond.
734 @param TransferResult Return the result of this control transfer.
736 @retval EFI_SUCCESS Transfer was completed successfully.
737 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
738 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
739 @retval EFI_TIMEOUT Transfer failed due to timeout.
740 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
744 IN USB_XHCI_INSTANCE
*Xhc
,
745 IN UINT8 DeviceAddress
,
746 IN UINT8 EndPointAddress
,
747 IN UINT8 DeviceSpeed
,
748 IN UINTN MaximumPacketLength
,
750 IN EFI_USB_DEVICE_REQUEST
*Request
,
752 IN OUT UINTN
*DataLength
,
754 OUT UINT32
*TransferResult
758 EFI_STATUS RecoveryStatus
;
761 ASSERT ((Type
== XHC_CTRL_TRANSFER
) || (Type
== XHC_BULK_TRANSFER
) || (Type
== XHC_INT_TRANSFER_SYNC
));
777 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: failed to create URB!\n", Type
));
778 return EFI_OUT_OF_RESOURCES
;
781 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
783 *TransferResult
= Urb
->Result
;
784 *DataLength
= Urb
->Completed
;
786 if (Status
== EFI_TIMEOUT
) {
788 // The transfer timed out. Abort the transfer by dequeueing of the TD.
790 RecoveryStatus
= XhcDequeueTrbFromEndpoint (Xhc
, Urb
);
791 if (EFI_ERROR (RecoveryStatus
)) {
792 DEBUG((DEBUG_ERROR
, "XhcTransfer[Type=%d]: XhcDequeueTrbFromEndpoint failed\n", Type
));
795 if (*TransferResult
== EFI_USB_NOERROR
) {
796 Status
= EFI_SUCCESS
;
797 } else if (*TransferResult
== EFI_USB_ERR_STALL
) {
798 RecoveryStatus
= XhcRecoverHaltedEndpoint (Xhc
, Urb
);
799 if (EFI_ERROR (RecoveryStatus
)) {
800 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: XhcRecoverHaltedEndpoint failed\n", Type
));
802 Status
= EFI_DEVICE_ERROR
;
806 Xhc
->PciIo
->Flush (Xhc
->PciIo
);
807 XhcFreeUrb (Xhc
, Urb
);
812 Submits control transfer to a target USB device.
814 @param This This EFI_USB2_HC_PROTOCOL instance.
815 @param DeviceAddress The target device address.
816 @param DeviceSpeed Target device speed.
817 @param MaximumPacketLength Maximum packet size the default control transfer
818 endpoint is capable of sending or receiving.
819 @param Request USB device request to send.
820 @param TransferDirection Specifies the data direction for the data stage
821 @param Data Data buffer to be transmitted or received from USB
823 @param DataLength The size (in bytes) of the data buffer.
824 @param Timeout Indicates the maximum timeout, in millisecond.
825 @param Translator Transaction translator to be used by this device.
826 @param TransferResult Return the result of this control transfer.
828 @retval EFI_SUCCESS Transfer was completed successfully.
829 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
830 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
831 @retval EFI_TIMEOUT Transfer failed due to timeout.
832 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
838 IN EFI_USB2_HC_PROTOCOL
*This
,
839 IN UINT8 DeviceAddress
,
840 IN UINT8 DeviceSpeed
,
841 IN UINTN MaximumPacketLength
,
842 IN EFI_USB_DEVICE_REQUEST
*Request
,
843 IN EFI_USB_DATA_DIRECTION TransferDirection
,
845 IN OUT UINTN
*DataLength
,
847 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
848 OUT UINT32
*TransferResult
851 USB_XHCI_INSTANCE
*Xhc
;
854 UINT8 DescriptorType
;
859 EFI_USB_HUB_DESCRIPTOR
*HubDesc
;
863 EFI_USB_PORT_STATUS PortStatus
;
865 EFI_USB_DEVICE_REQUEST ClearPortRequest
;
869 // Validate parameters
871 if ((Request
== NULL
) || (TransferResult
== NULL
)) {
872 return EFI_INVALID_PARAMETER
;
875 if ((TransferDirection
!= EfiUsbDataIn
) &&
876 (TransferDirection
!= EfiUsbDataOut
) &&
877 (TransferDirection
!= EfiUsbNoData
)) {
878 return EFI_INVALID_PARAMETER
;
881 if ((TransferDirection
== EfiUsbNoData
) &&
882 ((Data
!= NULL
) || (*DataLength
!= 0))) {
883 return EFI_INVALID_PARAMETER
;
886 if ((TransferDirection
!= EfiUsbNoData
) &&
887 ((Data
== NULL
) || (*DataLength
== 0))) {
888 return EFI_INVALID_PARAMETER
;
891 if ((MaximumPacketLength
!= 8) && (MaximumPacketLength
!= 16) &&
892 (MaximumPacketLength
!= 32) && (MaximumPacketLength
!= 64) &&
893 (MaximumPacketLength
!= 512)
895 return EFI_INVALID_PARAMETER
;
898 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) {
899 return EFI_INVALID_PARAMETER
;
902 if ((DeviceSpeed
== EFI_USB_SPEED_SUPER
) && (MaximumPacketLength
!= 512)) {
903 return EFI_INVALID_PARAMETER
;
906 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
908 Xhc
= XHC_FROM_THIS (This
);
910 Status
= EFI_DEVICE_ERROR
;
911 *TransferResult
= EFI_USB_ERR_SYSTEM
;
914 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
915 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: HC halted at entrance\n"));
920 // Check if the device is still enabled before every transaction.
922 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
928 // Hook the Set_Address request from UsbBus.
929 // According to XHCI 1.0 spec, the Set_Address request is replaced by XHCI's Address_Device cmd.
931 if ((Request
->Request
== USB_REQ_SET_ADDRESS
) &&
932 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
934 // Reset the BusDevAddr field of all disabled entries in UsbDevContext array firstly.
935 // This way is used to clean the history to avoid using wrong device address by XhcAsyncInterruptTransfer().
937 for (Index
= 0; Index
< 255; Index
++) {
938 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
&&
939 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0) &&
940 (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== (UINT8
)Request
->Value
)) {
941 Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
= 0;
945 if (Xhc
->UsbDevContext
[SlotId
].XhciDevAddr
== 0) {
946 Status
= EFI_DEVICE_ERROR
;
950 // The actual device address has been assigned by XHCI during initializing the device slot.
951 // So we just need establish the mapping relationship between the device address requested from UsbBus
952 // and the actual device address assigned by XHCI. The the following invocations through EFI_USB2_HC_PROTOCOL interface
953 // can find out the actual device address by it.
955 Xhc
->UsbDevContext
[SlotId
].BusDevAddr
= (UINT8
)Request
->Value
;
956 Status
= EFI_SUCCESS
;
961 // Create a new URB, insert it into the asynchronous
962 // schedule list, then poll the execution status.
963 // Note that we encode the direction in address although default control
964 // endpoint is bidirectional. XhcCreateUrb expects this
965 // combination of Ep addr and its direction.
967 Endpoint
= (UINT8
) (0 | ((TransferDirection
== EfiUsbDataIn
) ? 0x80 : 0));
968 Status
= XhcTransfer (
982 if (EFI_ERROR (Status
)) {
987 // Hook Get_Descriptor request from UsbBus as we need evaluate context and configure endpoint.
988 // Hook Get_Status request form UsbBus as we need trace device attach/detach event happened at hub.
989 // Hook Set_Config request from UsbBus as we need configure device endpoint.
991 if ((Request
->Request
== USB_REQ_GET_DESCRIPTOR
) &&
992 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
)) ||
993 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_DEVICE
))))) {
994 DescriptorType
= (UINT8
)(Request
->Value
>> 8);
995 if ((DescriptorType
== USB_DESC_TYPE_DEVICE
) && ((*DataLength
== sizeof (EFI_USB_DEVICE_DESCRIPTOR
)) || ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (*DataLength
== 8)))) {
996 ASSERT (Data
!= NULL
);
998 // Store a copy of device scriptor as hub device need this info to configure endpoint.
1000 CopyMem (&Xhc
->UsbDevContext
[SlotId
].DevDesc
, Data
, *DataLength
);
1001 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.BcdUSB
>= 0x0300) {
1003 // If it's a usb3.0 device, then its max packet size is a 2^n.
1005 MaxPacket0
= 1 << Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
1007 MaxPacket0
= Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
1009 Xhc
->UsbDevContext
[SlotId
].ConfDesc
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
* sizeof (EFI_USB_CONFIG_DESCRIPTOR
*));
1010 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1011 Status
= XhcEvaluateContext (Xhc
, SlotId
, MaxPacket0
);
1013 Status
= XhcEvaluateContext64 (Xhc
, SlotId
, MaxPacket0
);
1015 } else if (DescriptorType
== USB_DESC_TYPE_CONFIG
) {
1016 ASSERT (Data
!= NULL
);
1017 if (*DataLength
== ((UINT16
*)Data
)[1]) {
1019 // Get configuration value from request, Store the configuration descriptor for Configure_Endpoint cmd.
1021 Index
= (UINT8
)Request
->Value
;
1022 ASSERT (Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
);
1023 Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
] = AllocateZeroPool(*DataLength
);
1024 CopyMem (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
], Data
, *DataLength
);
1026 // Default to use AlternateSetting 0 for all interfaces.
1028 Xhc
->UsbDevContext
[SlotId
].ActiveAlternateSetting
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->NumInterfaces
* sizeof (UINT8
));
1030 } else if (((DescriptorType
== USB_DESC_TYPE_HUB
) ||
1031 (DescriptorType
== USB_DESC_TYPE_HUB_SUPER_SPEED
)) && (*DataLength
> 2)) {
1032 ASSERT (Data
!= NULL
);
1033 HubDesc
= (EFI_USB_HUB_DESCRIPTOR
*)Data
;
1034 ASSERT (HubDesc
->NumPorts
<= 15);
1036 // The bit 5,6 of HubCharacter field of Hub Descriptor is TTT.
1038 TTT
= (UINT8
)((HubDesc
->HubCharacter
& (BIT5
| BIT6
)) >> 5);
1039 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.DeviceProtocol
== 2) {
1041 // Don't support multi-TT feature for super speed hub now.
1044 DEBUG ((EFI_D_ERROR
, "XHCI: Don't support multi-TT feature for Hub now. (force to disable MTT)\n"));
1049 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1050 Status
= XhcConfigHubContext (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
1052 Status
= XhcConfigHubContext64 (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
1055 } else if ((Request
->Request
== USB_REQ_SET_CONFIG
) &&
1056 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
1058 // Hook Set_Config request from UsbBus as we need configure device endpoint.
1060 for (Index
= 0; Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
; Index
++) {
1061 if (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->ConfigurationValue
== (UINT8
)Request
->Value
) {
1062 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1063 Status
= XhcSetConfigCmd (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
1065 Status
= XhcSetConfigCmd64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
1070 } else if ((Request
->Request
== USB_REQ_SET_INTERFACE
) &&
1071 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_INTERFACE
))) {
1073 // Hook Set_Interface request from UsbBus as we need configure interface setting.
1074 // Request->Value indicates AlterlateSetting to set
1075 // Request->Index indicates Interface to set
1077 if (Xhc
->UsbDevContext
[SlotId
].ActiveAlternateSetting
[(UINT8
) Request
->Index
] != (UINT8
) Request
->Value
) {
1078 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1079 Status
= XhcSetInterface (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Xhc
->UsbDevContext
[SlotId
].ActiveConfiguration
- 1], Request
);
1081 Status
= XhcSetInterface64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Xhc
->UsbDevContext
[SlotId
].ActiveConfiguration
- 1], Request
);
1084 } else if ((Request
->Request
== USB_REQ_GET_STATUS
) &&
1085 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
))) {
1086 ASSERT (Data
!= NULL
);
1088 // Hook Get_Status request from UsbBus to keep track of the port status change.
1090 State
= *(UINT32
*)Data
;
1091 PortStatus
.PortStatus
= 0;
1092 PortStatus
.PortChangeStatus
= 0;
1094 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
1096 // For super speed hub, its bit10~12 presents the attached device speed.
1098 if ((State
& XHC_PORTSC_PS
) >> 10 == 0) {
1099 PortStatus
.PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
1103 // For high or full/low speed hub, its bit9~10 presents the attached device speed.
1105 if (XHC_BIT_IS_SET (State
, BIT9
)) {
1106 PortStatus
.PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
1107 } else if (XHC_BIT_IS_SET (State
, BIT10
)) {
1108 PortStatus
.PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
1113 // Convert the XHCI port/port change state to UEFI status
1115 MapSize
= sizeof (mUsbHubPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
1116 for (Index
= 0; Index
< MapSize
; Index
++) {
1117 if (XHC_BIT_IS_SET (State
, mUsbHubPortStateMap
[Index
].HwState
)) {
1118 PortStatus
.PortStatus
= (UINT16
) (PortStatus
.PortStatus
| mUsbHubPortStateMap
[Index
].UefiState
);
1122 MapSize
= sizeof (mUsbHubPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
1123 for (Index
= 0; Index
< MapSize
; Index
++) {
1124 if (XHC_BIT_IS_SET (State
, mUsbHubPortChangeMap
[Index
].HwState
)) {
1125 PortStatus
.PortChangeStatus
= (UINT16
) (PortStatus
.PortChangeStatus
| mUsbHubPortChangeMap
[Index
].UefiState
);
1129 MapSize
= sizeof (mUsbHubClearPortChangeMap
) / sizeof (USB_CLEAR_PORT_MAP
);
1131 for (Index
= 0; Index
< MapSize
; Index
++) {
1132 if (XHC_BIT_IS_SET (State
, mUsbHubClearPortChangeMap
[Index
].HwState
)) {
1133 ZeroMem (&ClearPortRequest
, sizeof (EFI_USB_DEVICE_REQUEST
));
1134 ClearPortRequest
.RequestType
= USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
);
1135 ClearPortRequest
.Request
= (UINT8
) USB_REQ_CLEAR_FEATURE
;
1136 ClearPortRequest
.Value
= mUsbHubClearPortChangeMap
[Index
].Selector
;
1137 ClearPortRequest
.Index
= Request
->Index
;
1138 ClearPortRequest
.Length
= 0;
1140 XhcControlTransfer (
1144 MaximumPacketLength
,
1156 XhcPollPortStatusChange (Xhc
, Xhc
->UsbDevContext
[SlotId
].RouteString
, (UINT8
)Request
->Index
, &PortStatus
);
1158 *(UINT32
*)Data
= *(UINT32
*)&PortStatus
;
1162 if (EFI_ERROR (Status
)) {
1163 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1166 gBS
->RestoreTPL (OldTpl
);
1173 Submits bulk transfer to a bulk endpoint of a USB device.
1175 @param This This EFI_USB2_HC_PROTOCOL instance.
1176 @param DeviceAddress Target device address.
1177 @param EndPointAddress Endpoint number and its direction in bit 7.
1178 @param DeviceSpeed Device speed, Low speed device doesn't support bulk
1180 @param MaximumPacketLength Maximum packet size the endpoint is capable of
1181 sending or receiving.
1182 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1183 @param Data Array of pointers to the buffers of data to transmit
1184 from or receive into.
1185 @param DataLength The lenght of the data buffer.
1186 @param DataToggle On input, the initial data toggle for the transfer;
1187 On output, it is updated to to next data toggle to
1188 use of the subsequent bulk transfer.
1189 @param Timeout Indicates the maximum time, in millisecond, which
1190 the transfer is allowed to complete.
1191 @param Translator A pointr to the transaction translator data.
1192 @param TransferResult A pointer to the detailed result information of the
1195 @retval EFI_SUCCESS The transfer was completed successfully.
1196 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1197 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1198 @retval EFI_TIMEOUT The transfer failed due to timeout.
1199 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1205 IN EFI_USB2_HC_PROTOCOL
*This
,
1206 IN UINT8 DeviceAddress
,
1207 IN UINT8 EndPointAddress
,
1208 IN UINT8 DeviceSpeed
,
1209 IN UINTN MaximumPacketLength
,
1210 IN UINT8 DataBuffersNumber
,
1211 IN OUT VOID
*Data
[EFI_USB_MAX_BULK_BUFFER_NUM
],
1212 IN OUT UINTN
*DataLength
,
1213 IN OUT UINT8
*DataToggle
,
1215 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1216 OUT UINT32
*TransferResult
1219 USB_XHCI_INSTANCE
*Xhc
;
1225 // Validate the parameters
1227 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1228 (Data
== NULL
) || (Data
[0] == NULL
) || (TransferResult
== NULL
)) {
1229 return EFI_INVALID_PARAMETER
;
1232 if ((*DataToggle
!= 0) && (*DataToggle
!= 1)) {
1233 return EFI_INVALID_PARAMETER
;
1236 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) ||
1237 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1238 ((EFI_USB_SPEED_HIGH
== DeviceSpeed
) && (MaximumPacketLength
> 512)) ||
1239 ((EFI_USB_SPEED_SUPER
== DeviceSpeed
) && (MaximumPacketLength
> 1024))) {
1240 return EFI_INVALID_PARAMETER
;
1243 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1245 Xhc
= XHC_FROM_THIS (This
);
1247 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1248 Status
= EFI_DEVICE_ERROR
;
1250 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1251 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: HC is halted\n"));
1256 // Check if the device is still enabled before every transaction.
1258 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1264 // Create a new URB, insert it into the asynchronous
1265 // schedule list, then poll the execution status.
1267 Status
= XhcTransfer (
1272 MaximumPacketLength
,
1282 if (EFI_ERROR (Status
)) {
1283 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1285 gBS
->RestoreTPL (OldTpl
);
1291 Submits an asynchronous interrupt transfer to an
1292 interrupt endpoint of a USB device.
1294 @param This This EFI_USB2_HC_PROTOCOL instance.
1295 @param DeviceAddress Target device address.
1296 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1297 @param DeviceSpeed Indicates device speed.
1298 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1299 @param IsNewTransfer If TRUE, to submit an new asynchronous interrupt
1300 transfer If FALSE, to remove the specified
1301 asynchronous interrupt.
1302 @param DataToggle On input, the initial data toggle to use; on output,
1303 it is updated to indicate the next data toggle.
1304 @param PollingInterval The he interval, in milliseconds, that the transfer
1306 @param DataLength The length of data to receive at the rate specified
1308 @param Translator Transaction translator to use.
1309 @param CallBackFunction Function to call at the rate specified by
1311 @param Context Context to CallBackFunction.
1313 @retval EFI_SUCCESS The request has been successfully submitted or canceled.
1314 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1315 @retval EFI_OUT_OF_RESOURCES The request failed due to a lack of resources.
1316 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1321 XhcAsyncInterruptTransfer (
1322 IN EFI_USB2_HC_PROTOCOL
*This
,
1323 IN UINT8 DeviceAddress
,
1324 IN UINT8 EndPointAddress
,
1325 IN UINT8 DeviceSpeed
,
1326 IN UINTN MaximumPacketLength
,
1327 IN BOOLEAN IsNewTransfer
,
1328 IN OUT UINT8
*DataToggle
,
1329 IN UINTN PollingInterval
,
1330 IN UINTN DataLength
,
1331 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1332 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction
,
1333 IN VOID
*Context OPTIONAL
1336 USB_XHCI_INSTANCE
*Xhc
;
1345 // Validate parameters
1347 if (!XHCI_IS_DATAIN (EndPointAddress
)) {
1348 return EFI_INVALID_PARAMETER
;
1351 if (IsNewTransfer
) {
1352 if (DataLength
== 0) {
1353 return EFI_INVALID_PARAMETER
;
1356 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1357 return EFI_INVALID_PARAMETER
;
1360 if ((PollingInterval
> 255) || (PollingInterval
< 1)) {
1361 return EFI_INVALID_PARAMETER
;
1365 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1367 Xhc
= XHC_FROM_THIS (This
);
1370 // Delete Async interrupt transfer request.
1372 if (!IsNewTransfer
) {
1374 // The delete request may happen after device is detached.
1376 for (Index
= 0; Index
< 255; Index
++) {
1377 if (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== DeviceAddress
) {
1383 Status
= EFI_INVALID_PARAMETER
;
1387 Status
= XhciDelAsyncIntTransfer (Xhc
, DeviceAddress
, EndPointAddress
);
1388 DEBUG ((EFI_D_INFO
, "XhcAsyncInterruptTransfer: remove old transfer for addr %d, Status = %r\n", DeviceAddress
, Status
));
1392 Status
= EFI_SUCCESS
;
1394 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1395 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: HC is halt\n"));
1396 Status
= EFI_DEVICE_ERROR
;
1401 // Check if the device is still enabled before every transaction.
1403 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1408 Data
= AllocateZeroPool (DataLength
);
1411 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: failed to allocate buffer\n"));
1412 Status
= EFI_OUT_OF_RESOURCES
;
1416 Urb
= XhcCreateUrb (
1421 MaximumPacketLength
,
1422 XHC_INT_TRANSFER_ASYNC
,
1431 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: failed to create URB\n"));
1433 Status
= EFI_OUT_OF_RESOURCES
;
1437 InsertHeadList (&Xhc
->AsyncIntTransfers
, &Urb
->UrbList
);
1439 // Ring the doorbell
1441 Status
= RingIntTransferDoorBell (Xhc
, Urb
);
1444 Xhc
->PciIo
->Flush (Xhc
->PciIo
);
1445 gBS
->RestoreTPL (OldTpl
);
1452 Submits synchronous interrupt transfer to an interrupt endpoint
1455 @param This This EFI_USB2_HC_PROTOCOL instance.
1456 @param DeviceAddress Target device address.
1457 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1458 @param DeviceSpeed Indicates device speed.
1459 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1460 of sending or receiving.
1461 @param Data Buffer of data that will be transmitted to USB
1462 device or received from USB device.
1463 @param DataLength On input, the size, in bytes, of the data buffer; On
1464 output, the number of bytes transferred.
1465 @param DataToggle On input, the initial data toggle to use; on output,
1466 it is updated to indicate the next data toggle.
1467 @param Timeout Maximum time, in second, to complete.
1468 @param Translator Transaction translator to use.
1469 @param TransferResult Variable to receive the transfer result.
1471 @return EFI_SUCCESS The transfer was completed successfully.
1472 @return EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1473 @return EFI_INVALID_PARAMETER Some parameters are invalid.
1474 @return EFI_TIMEOUT The transfer failed due to timeout.
1475 @return EFI_DEVICE_ERROR The failed due to host controller or device error
1480 XhcSyncInterruptTransfer (
1481 IN EFI_USB2_HC_PROTOCOL
*This
,
1482 IN UINT8 DeviceAddress
,
1483 IN UINT8 EndPointAddress
,
1484 IN UINT8 DeviceSpeed
,
1485 IN UINTN MaximumPacketLength
,
1487 IN OUT UINTN
*DataLength
,
1488 IN OUT UINT8
*DataToggle
,
1490 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1491 OUT UINT32
*TransferResult
1494 USB_XHCI_INSTANCE
*Xhc
;
1500 // Validates parameters
1502 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1503 (Data
== NULL
) || (TransferResult
== NULL
)) {
1504 return EFI_INVALID_PARAMETER
;
1507 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1508 return EFI_INVALID_PARAMETER
;
1511 if (((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) ||
1512 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1513 ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) && (MaximumPacketLength
> 3072))) {
1514 return EFI_INVALID_PARAMETER
;
1517 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1519 Xhc
= XHC_FROM_THIS (This
);
1521 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1522 Status
= EFI_DEVICE_ERROR
;
1524 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1525 DEBUG ((EFI_D_ERROR
, "EhcSyncInterruptTransfer: HC is halt\n"));
1530 // Check if the device is still enabled before every transaction.
1532 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1537 Status
= XhcTransfer (
1542 MaximumPacketLength
,
1543 XHC_INT_TRANSFER_SYNC
,
1552 if (EFI_ERROR (Status
)) {
1553 DEBUG ((EFI_D_ERROR
, "XhcSyncInterruptTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1555 gBS
->RestoreTPL (OldTpl
);
1562 Submits isochronous transfer to a target USB device.
1564 @param This This EFI_USB2_HC_PROTOCOL instance.
1565 @param DeviceAddress Target device address.
1566 @param EndPointAddress End point address with its direction.
1567 @param DeviceSpeed Device speed, Low speed device doesn't support this
1569 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1570 sending or receiving.
1571 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1572 @param Data Array of pointers to the buffers of data that will
1573 be transmitted to USB device or received from USB
1575 @param DataLength The size, in bytes, of the data buffer.
1576 @param Translator Transaction translator to use.
1577 @param TransferResult Variable to receive the transfer result.
1579 @return EFI_UNSUPPORTED Isochronous transfer is unsupported.
1584 XhcIsochronousTransfer (
1585 IN EFI_USB2_HC_PROTOCOL
*This
,
1586 IN UINT8 DeviceAddress
,
1587 IN UINT8 EndPointAddress
,
1588 IN UINT8 DeviceSpeed
,
1589 IN UINTN MaximumPacketLength
,
1590 IN UINT8 DataBuffersNumber
,
1591 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1592 IN UINTN DataLength
,
1593 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1594 OUT UINT32
*TransferResult
1597 return EFI_UNSUPPORTED
;
1602 Submits Async isochronous transfer to a target USB device.
1604 @param This This EFI_USB2_HC_PROTOCOL instance.
1605 @param DeviceAddress Target device address.
1606 @param EndPointAddress End point address with its direction.
1607 @param DeviceSpeed Device speed, Low speed device doesn't support this
1609 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1610 sending or receiving.
1611 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1612 @param Data Array of pointers to the buffers of data that will
1613 be transmitted to USB device or received from USB
1615 @param DataLength The size, in bytes, of the data buffer.
1616 @param Translator Transaction translator to use.
1617 @param IsochronousCallBack Function to be called when the transfer complete.
1618 @param Context Context passed to the call back function as
1621 @return EFI_UNSUPPORTED Isochronous transfer isn't supported.
1626 XhcAsyncIsochronousTransfer (
1627 IN EFI_USB2_HC_PROTOCOL
*This
,
1628 IN UINT8 DeviceAddress
,
1629 IN UINT8 EndPointAddress
,
1630 IN UINT8 DeviceSpeed
,
1631 IN UINTN MaximumPacketLength
,
1632 IN UINT8 DataBuffersNumber
,
1633 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1634 IN UINTN DataLength
,
1635 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1636 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
1640 return EFI_UNSUPPORTED
;
1644 Entry point for EFI drivers.
1646 @param ImageHandle EFI_HANDLE.
1647 @param SystemTable EFI_SYSTEM_TABLE.
1649 @retval EFI_SUCCESS Success.
1650 @retval Others Fail.
1655 XhcDriverEntryPoint (
1656 IN EFI_HANDLE ImageHandle
,
1657 IN EFI_SYSTEM_TABLE
*SystemTable
1660 return EfiLibInstallDriverBindingComponentName2 (
1663 &gXhciDriverBinding
,
1665 &gXhciComponentName
,
1666 &gXhciComponentName2
1672 Test to see if this driver supports ControllerHandle. Any
1673 ControllerHandle that has Usb2HcProtocol installed will
1676 @param This Protocol instance pointer.
1677 @param Controller Handle of device to test.
1678 @param RemainingDevicePath Not used.
1680 @return EFI_SUCCESS This driver supports this device.
1681 @return EFI_UNSUPPORTED This driver does not support this device.
1686 XhcDriverBindingSupported (
1687 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1688 IN EFI_HANDLE Controller
,
1689 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1693 EFI_PCI_IO_PROTOCOL
*PciIo
;
1694 USB_CLASSC UsbClassCReg
;
1697 // Test whether there is PCI IO Protocol attached on the controller handle.
1699 Status
= gBS
->OpenProtocol (
1701 &gEfiPciIoProtocolGuid
,
1703 This
->DriverBindingHandle
,
1705 EFI_OPEN_PROTOCOL_BY_DRIVER
1708 if (EFI_ERROR (Status
)) {
1709 return EFI_UNSUPPORTED
;
1712 Status
= PciIo
->Pci
.Read (
1715 PCI_CLASSCODE_OFFSET
,
1716 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1720 if (EFI_ERROR (Status
)) {
1721 Status
= EFI_UNSUPPORTED
;
1726 // Test whether the controller belongs to Xhci type
1728 if ((UsbClassCReg
.BaseCode
!= PCI_CLASS_SERIAL
) ||
1729 (UsbClassCReg
.SubClassCode
!= PCI_CLASS_SERIAL_USB
) ||
1730 (UsbClassCReg
.ProgInterface
!= PCI_IF_XHCI
)) {
1731 Status
= EFI_UNSUPPORTED
;
1735 gBS
->CloseProtocol (
1737 &gEfiPciIoProtocolGuid
,
1738 This
->DriverBindingHandle
,
1746 Create and initialize a USB_XHCI_INSTANCE structure.
1748 @param PciIo The PciIo on this device.
1749 @param DevicePath The device path of host controller.
1750 @param OriginalPciAttributes Original PCI attributes.
1752 @return The allocated and initialized USB_XHCI_INSTANCE structure if created,
1758 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1759 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1760 IN UINT64 OriginalPciAttributes
1763 USB_XHCI_INSTANCE
*Xhc
;
1768 Xhc
= AllocateZeroPool (sizeof (USB_XHCI_INSTANCE
));
1775 // Initialize private data structure
1777 Xhc
->Signature
= XHCI_INSTANCE_SIG
;
1779 Xhc
->DevicePath
= DevicePath
;
1780 Xhc
->OriginalPciAttributes
= OriginalPciAttributes
;
1781 CopyMem (&Xhc
->Usb2Hc
, &gXhciUsb2HcTemplate
, sizeof (EFI_USB2_HC_PROTOCOL
));
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
,