2 The XHCI controller driver.
4 Copyright (c) 2011 - 2018, 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.1 spec November 2017,
407 // bit 10~13 of the root port status register identifies the speed of the attached device.
409 switch ((State
& XHC_PORTSC_PS
) >> 10) {
411 PortStatus
->PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
415 PortStatus
->PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
420 PortStatus
->PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
428 // Convert the XHCI port/port change state to UEFI status
430 MapSize
= sizeof (mUsbPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
432 for (Index
= 0; Index
< MapSize
; Index
++) {
433 if (XHC_BIT_IS_SET (State
, mUsbPortStateMap
[Index
].HwState
)) {
434 PortStatus
->PortStatus
= (UINT16
) (PortStatus
->PortStatus
| mUsbPortStateMap
[Index
].UefiState
);
438 // Bit5~8 reflects its current link state.
440 if ((State
& XHC_PORTSC_PLS
) >> 5 == 3) {
441 PortStatus
->PortStatus
|= USB_PORT_STAT_SUSPEND
;
444 MapSize
= sizeof (mUsbPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
446 for (Index
= 0; Index
< MapSize
; Index
++) {
447 if (XHC_BIT_IS_SET (State
, mUsbPortChangeMap
[Index
].HwState
)) {
448 PortStatus
->PortChangeStatus
= (UINT16
) (PortStatus
->PortChangeStatus
| mUsbPortChangeMap
[Index
].UefiState
);
452 MapSize
= sizeof (mUsbClearPortChangeMap
) / sizeof (USB_CLEAR_PORT_MAP
);
454 for (Index
= 0; Index
< MapSize
; Index
++) {
455 if (XHC_BIT_IS_SET (State
, mUsbClearPortChangeMap
[Index
].HwState
)) {
456 XhcClearRootHubPortFeature (This
, PortNumber
, (EFI_USB_PORT_FEATURE
)mUsbClearPortChangeMap
[Index
].Selector
);
461 // Poll the root port status register to enable/disable corresponding device slot if there is a device attached/detached.
462 // For those devices behind hub, we get its attach/detach event by hooking Get_Port_Status request at control transfer for those hub.
464 ParentRouteChart
.Dword
= 0;
465 XhcPollPortStatusChange (Xhc
, ParentRouteChart
, PortNumber
, PortStatus
);
468 gBS
->RestoreTPL (OldTpl
);
474 Sets a feature for the specified root hub port.
476 @param This This EFI_USB2_HC_PROTOCOL instance.
477 @param PortNumber Root hub port to set.
478 @param PortFeature Feature to set.
480 @retval EFI_SUCCESS The feature specified by PortFeature was set.
481 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
482 @retval EFI_DEVICE_ERROR Can't read register.
487 XhcSetRootHubPortFeature (
488 IN EFI_USB2_HC_PROTOCOL
*This
,
490 IN EFI_USB_PORT_FEATURE PortFeature
493 USB_XHCI_INSTANCE
*Xhc
;
500 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
502 Xhc
= XHC_FROM_THIS (This
);
503 Status
= EFI_SUCCESS
;
505 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
507 if (PortNumber
>= TotalPort
) {
508 Status
= EFI_INVALID_PARAMETER
;
512 Offset
= (UINT32
) (XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
513 State
= XhcReadOpReg (Xhc
, Offset
);
516 // Mask off the port status change bits, these bits are
519 State
&= ~ (BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
521 switch (PortFeature
) {
522 case EfiUsbPortEnable
:
524 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
525 // A port may be disabled by software writing a '1' to this flag.
527 Status
= EFI_SUCCESS
;
530 case EfiUsbPortSuspend
:
531 State
|= XHC_PORTSC_LWS
;
532 XhcWriteOpReg (Xhc
, Offset
, State
);
533 State
&= ~XHC_PORTSC_PLS
;
535 XhcWriteOpReg (Xhc
, Offset
, State
);
538 case EfiUsbPortReset
:
539 DEBUG ((EFI_D_INFO
, "XhcUsbPortReset!\n"));
541 // Make sure Host Controller not halt before reset it
543 if (XhcIsHalt (Xhc
)) {
544 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
546 if (EFI_ERROR (Status
)) {
547 DEBUG ((EFI_D_INFO
, "XhcSetRootHubPortFeature :failed to start HC - %r\n", Status
));
553 // 4.3.1 Resetting a Root Hub Port
554 // 1) Write the PORTSC register with the Port Reset (PR) bit set to '1'.
556 State
|= XHC_PORTSC_RESET
;
557 XhcWriteOpReg (Xhc
, Offset
, State
);
558 XhcWaitOpRegBit(Xhc
, Offset
, XHC_PORTSC_PRC
, TRUE
, XHC_GENERIC_TIMEOUT
);
561 case EfiUsbPortPower
:
563 // Not supported, ignore the operation
565 Status
= EFI_SUCCESS
;
568 case EfiUsbPortOwner
:
570 // XHCI root hub port don't has the owner bit, ignore the operation
572 Status
= EFI_SUCCESS
;
576 Status
= EFI_INVALID_PARAMETER
;
580 DEBUG ((EFI_D_INFO
, "XhcSetRootHubPortFeature: status %r\n", Status
));
581 gBS
->RestoreTPL (OldTpl
);
588 Clears a feature for the specified root hub port.
590 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
591 @param PortNumber Specifies the root hub port whose feature is
592 requested to be cleared.
593 @param PortFeature Indicates the feature selector associated with the
594 feature clear request.
596 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
597 for the USB root hub port specified by PortNumber.
598 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
599 @retval EFI_DEVICE_ERROR Can't read register.
604 XhcClearRootHubPortFeature (
605 IN EFI_USB2_HC_PROTOCOL
*This
,
607 IN EFI_USB_PORT_FEATURE PortFeature
610 USB_XHCI_INSTANCE
*Xhc
;
617 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
619 Xhc
= XHC_FROM_THIS (This
);
620 Status
= EFI_SUCCESS
;
622 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
624 if (PortNumber
>= TotalPort
) {
625 Status
= EFI_INVALID_PARAMETER
;
629 Offset
= XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
);
632 // Mask off the port status change bits, these bits are
635 State
= XhcReadOpReg (Xhc
, Offset
);
636 State
&= ~ (BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
638 switch (PortFeature
) {
639 case EfiUsbPortEnable
:
641 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
642 // A port may be disabled by software writing a '1' to this flag.
644 State
|= XHC_PORTSC_PED
;
645 State
&= ~XHC_PORTSC_RESET
;
646 XhcWriteOpReg (Xhc
, Offset
, State
);
649 case EfiUsbPortSuspend
:
650 State
|= XHC_PORTSC_LWS
;
651 XhcWriteOpReg (Xhc
, Offset
, State
);
652 State
&= ~XHC_PORTSC_PLS
;
653 XhcWriteOpReg (Xhc
, Offset
, State
);
656 case EfiUsbPortReset
:
658 // PORTSC_RESET BIT(4) bit is RW1S attribute, which means Write-1-to-set status:
659 // Register bits indicate status when read, a clear bit may be set by
660 // writing a '1'. Writing a '0' to RW1S bits has no effect.
664 case EfiUsbPortOwner
:
666 // XHCI root hub port don't has the owner bit, ignore the operation
670 case EfiUsbPortConnectChange
:
672 // Clear connect status change
674 State
|= XHC_PORTSC_CSC
;
675 XhcWriteOpReg (Xhc
, Offset
, State
);
678 case EfiUsbPortEnableChange
:
680 // Clear enable status change
682 State
|= XHC_PORTSC_PEC
;
683 XhcWriteOpReg (Xhc
, Offset
, State
);
686 case EfiUsbPortOverCurrentChange
:
688 // Clear PortOverCurrent change
690 State
|= XHC_PORTSC_OCC
;
691 XhcWriteOpReg (Xhc
, Offset
, State
);
694 case EfiUsbPortResetChange
:
696 // Clear Port Reset change
698 State
|= XHC_PORTSC_PRC
;
699 XhcWriteOpReg (Xhc
, Offset
, State
);
702 case EfiUsbPortPower
:
703 case EfiUsbPortSuspendChange
:
705 // Not supported or not related operation
710 Status
= EFI_INVALID_PARAMETER
;
715 DEBUG ((EFI_D_INFO
, "XhcClearRootHubPortFeature: status %r\n", Status
));
716 gBS
->RestoreTPL (OldTpl
);
722 Submits a new transaction to a target USB device.
724 @param Xhc The XHCI Instance.
725 @param DeviceAddress The target device address.
726 @param EndPointAddress Endpoint number and its direction encoded in bit 7
727 @param DeviceSpeed Target device speed.
728 @param MaximumPacketLength Maximum packet size the default control transfer
729 endpoint is capable of sending or receiving.
730 @param Type The transaction type.
731 @param Request USB device request to send.
732 @param Data Data buffer to be transmitted or received from USB
734 @param DataLength The size (in bytes) of the data buffer.
735 @param Timeout Indicates the maximum timeout, in millisecond.
736 @param TransferResult Return the result of this control transfer.
738 @retval EFI_SUCCESS Transfer was completed successfully.
739 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
740 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
741 @retval EFI_TIMEOUT Transfer failed due to timeout.
742 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
746 IN USB_XHCI_INSTANCE
*Xhc
,
747 IN UINT8 DeviceAddress
,
748 IN UINT8 EndPointAddress
,
749 IN UINT8 DeviceSpeed
,
750 IN UINTN MaximumPacketLength
,
752 IN EFI_USB_DEVICE_REQUEST
*Request
,
754 IN OUT UINTN
*DataLength
,
756 OUT UINT32
*TransferResult
760 EFI_STATUS RecoveryStatus
;
763 ASSERT ((Type
== XHC_CTRL_TRANSFER
) || (Type
== XHC_BULK_TRANSFER
) || (Type
== XHC_INT_TRANSFER_SYNC
));
779 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: failed to create URB!\n", Type
));
780 return EFI_OUT_OF_RESOURCES
;
783 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
785 if (Status
== EFI_TIMEOUT
) {
787 // The transfer timed out. Abort the transfer by dequeueing of the TD.
789 RecoveryStatus
= XhcDequeueTrbFromEndpoint(Xhc
, Urb
);
790 if (RecoveryStatus
== EFI_ALREADY_STARTED
) {
792 // The URB is finished just before stopping endpoint.
793 // Change returning status from EFI_TIMEOUT to EFI_SUCCESS.
795 ASSERT (Urb
->Result
== EFI_USB_NOERROR
);
796 Status
= EFI_SUCCESS
;
797 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: pending URB is finished, Length = %d.\n", Type
, Urb
->Completed
));
798 } else if (EFI_ERROR(RecoveryStatus
)) {
799 DEBUG((DEBUG_ERROR
, "XhcTransfer[Type=%d]: XhcDequeueTrbFromEndpoint failed!\n", Type
));
803 *TransferResult
= Urb
->Result
;
804 *DataLength
= Urb
->Completed
;
806 if ((*TransferResult
== EFI_USB_ERR_STALL
) || (*TransferResult
== EFI_USB_ERR_BABBLE
)) {
807 ASSERT (Status
== EFI_DEVICE_ERROR
);
808 RecoveryStatus
= XhcRecoverHaltedEndpoint(Xhc
, Urb
);
809 if (EFI_ERROR (RecoveryStatus
)) {
810 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: XhcRecoverHaltedEndpoint failed!\n", Type
));
814 Xhc
->PciIo
->Flush (Xhc
->PciIo
);
815 XhcFreeUrb (Xhc
, Urb
);
820 Submits control transfer to a target USB device.
822 @param This This EFI_USB2_HC_PROTOCOL instance.
823 @param DeviceAddress The target device address.
824 @param DeviceSpeed Target device speed.
825 @param MaximumPacketLength Maximum packet size the default control transfer
826 endpoint is capable of sending or receiving.
827 @param Request USB device request to send.
828 @param TransferDirection Specifies the data direction for the data stage
829 @param Data Data buffer to be transmitted or received from USB
831 @param DataLength The size (in bytes) of the data buffer.
832 @param Timeout Indicates the maximum timeout, in millisecond.
833 @param Translator Transaction translator to be used by this device.
834 @param TransferResult Return the result of this control transfer.
836 @retval EFI_SUCCESS Transfer was completed successfully.
837 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
838 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
839 @retval EFI_TIMEOUT Transfer failed due to timeout.
840 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
846 IN EFI_USB2_HC_PROTOCOL
*This
,
847 IN UINT8 DeviceAddress
,
848 IN UINT8 DeviceSpeed
,
849 IN UINTN MaximumPacketLength
,
850 IN EFI_USB_DEVICE_REQUEST
*Request
,
851 IN EFI_USB_DATA_DIRECTION TransferDirection
,
853 IN OUT UINTN
*DataLength
,
855 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
856 OUT UINT32
*TransferResult
859 USB_XHCI_INSTANCE
*Xhc
;
862 UINT8 DescriptorType
;
867 EFI_USB_HUB_DESCRIPTOR
*HubDesc
;
871 EFI_USB_PORT_STATUS PortStatus
;
873 EFI_USB_DEVICE_REQUEST ClearPortRequest
;
877 // Validate parameters
879 if ((Request
== NULL
) || (TransferResult
== NULL
)) {
880 return EFI_INVALID_PARAMETER
;
883 if ((TransferDirection
!= EfiUsbDataIn
) &&
884 (TransferDirection
!= EfiUsbDataOut
) &&
885 (TransferDirection
!= EfiUsbNoData
)) {
886 return EFI_INVALID_PARAMETER
;
889 if ((TransferDirection
== EfiUsbNoData
) &&
890 ((Data
!= NULL
) || (*DataLength
!= 0))) {
891 return EFI_INVALID_PARAMETER
;
894 if ((TransferDirection
!= EfiUsbNoData
) &&
895 ((Data
== NULL
) || (*DataLength
== 0))) {
896 return EFI_INVALID_PARAMETER
;
899 if ((MaximumPacketLength
!= 8) && (MaximumPacketLength
!= 16) &&
900 (MaximumPacketLength
!= 32) && (MaximumPacketLength
!= 64) &&
901 (MaximumPacketLength
!= 512)
903 return EFI_INVALID_PARAMETER
;
906 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) {
907 return EFI_INVALID_PARAMETER
;
910 if ((DeviceSpeed
== EFI_USB_SPEED_SUPER
) && (MaximumPacketLength
!= 512)) {
911 return EFI_INVALID_PARAMETER
;
914 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
916 Xhc
= XHC_FROM_THIS (This
);
918 Status
= EFI_DEVICE_ERROR
;
919 *TransferResult
= EFI_USB_ERR_SYSTEM
;
922 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
923 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: HC halted at entrance\n"));
928 // Check if the device is still enabled before every transaction.
930 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
936 // Hook the Set_Address request from UsbBus.
937 // According to XHCI 1.0 spec, the Set_Address request is replaced by XHCI's Address_Device cmd.
939 if ((Request
->Request
== USB_REQ_SET_ADDRESS
) &&
940 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
942 // Reset the BusDevAddr field of all disabled entries in UsbDevContext array firstly.
943 // This way is used to clean the history to avoid using wrong device address by XhcAsyncInterruptTransfer().
945 for (Index
= 0; Index
< 255; Index
++) {
946 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
&&
947 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0) &&
948 (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== (UINT8
)Request
->Value
)) {
949 Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
= 0;
953 if (Xhc
->UsbDevContext
[SlotId
].XhciDevAddr
== 0) {
954 Status
= EFI_DEVICE_ERROR
;
958 // The actual device address has been assigned by XHCI during initializing the device slot.
959 // So we just need establish the mapping relationship between the device address requested from UsbBus
960 // and the actual device address assigned by XHCI. The the following invocations through EFI_USB2_HC_PROTOCOL interface
961 // can find out the actual device address by it.
963 Xhc
->UsbDevContext
[SlotId
].BusDevAddr
= (UINT8
)Request
->Value
;
964 Status
= EFI_SUCCESS
;
969 // Create a new URB, insert it into the asynchronous
970 // schedule list, then poll the execution status.
971 // Note that we encode the direction in address although default control
972 // endpoint is bidirectional. XhcCreateUrb expects this
973 // combination of Ep addr and its direction.
975 Endpoint
= (UINT8
) (0 | ((TransferDirection
== EfiUsbDataIn
) ? 0x80 : 0));
976 Status
= XhcTransfer (
990 if (EFI_ERROR (Status
)) {
995 // Hook Get_Descriptor request from UsbBus as we need evaluate context and configure endpoint.
996 // Hook Get_Status request form UsbBus as we need trace device attach/detach event happened at hub.
997 // Hook Set_Config request from UsbBus as we need configure device endpoint.
999 if ((Request
->Request
== USB_REQ_GET_DESCRIPTOR
) &&
1000 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
)) ||
1001 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_DEVICE
))))) {
1002 DescriptorType
= (UINT8
)(Request
->Value
>> 8);
1003 if ((DescriptorType
== USB_DESC_TYPE_DEVICE
) && ((*DataLength
== sizeof (EFI_USB_DEVICE_DESCRIPTOR
)) || ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (*DataLength
== 8)))) {
1004 ASSERT (Data
!= NULL
);
1006 // Store a copy of device scriptor as hub device need this info to configure endpoint.
1008 CopyMem (&Xhc
->UsbDevContext
[SlotId
].DevDesc
, Data
, *DataLength
);
1009 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.BcdUSB
>= 0x0300) {
1011 // If it's a usb3.0 device, then its max packet size is a 2^n.
1013 MaxPacket0
= 1 << Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
1015 MaxPacket0
= Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
1017 Xhc
->UsbDevContext
[SlotId
].ConfDesc
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
* sizeof (EFI_USB_CONFIG_DESCRIPTOR
*));
1018 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1019 Status
= XhcEvaluateContext (Xhc
, SlotId
, MaxPacket0
);
1021 Status
= XhcEvaluateContext64 (Xhc
, SlotId
, MaxPacket0
);
1023 } else if (DescriptorType
== USB_DESC_TYPE_CONFIG
) {
1024 ASSERT (Data
!= NULL
);
1025 if (*DataLength
== ((UINT16
*)Data
)[1]) {
1027 // Get configuration value from request, Store the configuration descriptor for Configure_Endpoint cmd.
1029 Index
= (UINT8
)Request
->Value
;
1030 ASSERT (Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
);
1031 Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
] = AllocateZeroPool(*DataLength
);
1032 CopyMem (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
], Data
, *DataLength
);
1034 // Default to use AlternateSetting 0 for all interfaces.
1036 Xhc
->UsbDevContext
[SlotId
].ActiveAlternateSetting
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->NumInterfaces
* sizeof (UINT8
));
1038 } else if (((DescriptorType
== USB_DESC_TYPE_HUB
) ||
1039 (DescriptorType
== USB_DESC_TYPE_HUB_SUPER_SPEED
)) && (*DataLength
> 2)) {
1040 ASSERT (Data
!= NULL
);
1041 HubDesc
= (EFI_USB_HUB_DESCRIPTOR
*)Data
;
1042 ASSERT (HubDesc
->NumPorts
<= 15);
1044 // The bit 5,6 of HubCharacter field of Hub Descriptor is TTT.
1046 TTT
= (UINT8
)((HubDesc
->HubCharacter
& (BIT5
| BIT6
)) >> 5);
1047 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.DeviceProtocol
== 2) {
1049 // Don't support multi-TT feature for super speed hub now.
1052 DEBUG ((EFI_D_ERROR
, "XHCI: Don't support multi-TT feature for Hub now. (force to disable MTT)\n"));
1057 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1058 Status
= XhcConfigHubContext (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
1060 Status
= XhcConfigHubContext64 (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
1063 } else if ((Request
->Request
== USB_REQ_SET_CONFIG
) &&
1064 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
1066 // Hook Set_Config request from UsbBus as we need configure device endpoint.
1068 for (Index
= 0; Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
; Index
++) {
1069 if (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->ConfigurationValue
== (UINT8
)Request
->Value
) {
1070 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1071 Status
= XhcSetConfigCmd (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
1073 Status
= XhcSetConfigCmd64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
1078 } else if ((Request
->Request
== USB_REQ_SET_INTERFACE
) &&
1079 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_INTERFACE
))) {
1081 // Hook Set_Interface request from UsbBus as we need configure interface setting.
1082 // Request->Value indicates AlterlateSetting to set
1083 // Request->Index indicates Interface to set
1085 if (Xhc
->UsbDevContext
[SlotId
].ActiveAlternateSetting
[(UINT8
) Request
->Index
] != (UINT8
) Request
->Value
) {
1086 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1087 Status
= XhcSetInterface (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Xhc
->UsbDevContext
[SlotId
].ActiveConfiguration
- 1], Request
);
1089 Status
= XhcSetInterface64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Xhc
->UsbDevContext
[SlotId
].ActiveConfiguration
- 1], Request
);
1092 } else if ((Request
->Request
== USB_REQ_GET_STATUS
) &&
1093 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
))) {
1094 ASSERT (Data
!= NULL
);
1096 // Hook Get_Status request from UsbBus to keep track of the port status change.
1098 State
= *(UINT32
*)Data
;
1099 PortStatus
.PortStatus
= 0;
1100 PortStatus
.PortChangeStatus
= 0;
1102 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
1104 // For super speed hub, its bit10~12 presents the attached device speed.
1106 if ((State
& XHC_PORTSC_PS
) >> 10 == 0) {
1107 PortStatus
.PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
1111 // For high or full/low speed hub, its bit9~10 presents the attached device speed.
1113 if (XHC_BIT_IS_SET (State
, BIT9
)) {
1114 PortStatus
.PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
1115 } else if (XHC_BIT_IS_SET (State
, BIT10
)) {
1116 PortStatus
.PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
1121 // Convert the XHCI port/port change state to UEFI status
1123 MapSize
= sizeof (mUsbHubPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
1124 for (Index
= 0; Index
< MapSize
; Index
++) {
1125 if (XHC_BIT_IS_SET (State
, mUsbHubPortStateMap
[Index
].HwState
)) {
1126 PortStatus
.PortStatus
= (UINT16
) (PortStatus
.PortStatus
| mUsbHubPortStateMap
[Index
].UefiState
);
1130 MapSize
= sizeof (mUsbHubPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
1131 for (Index
= 0; Index
< MapSize
; Index
++) {
1132 if (XHC_BIT_IS_SET (State
, mUsbHubPortChangeMap
[Index
].HwState
)) {
1133 PortStatus
.PortChangeStatus
= (UINT16
) (PortStatus
.PortChangeStatus
| mUsbHubPortChangeMap
[Index
].UefiState
);
1137 MapSize
= sizeof (mUsbHubClearPortChangeMap
) / sizeof (USB_CLEAR_PORT_MAP
);
1139 for (Index
= 0; Index
< MapSize
; Index
++) {
1140 if (XHC_BIT_IS_SET (State
, mUsbHubClearPortChangeMap
[Index
].HwState
)) {
1141 ZeroMem (&ClearPortRequest
, sizeof (EFI_USB_DEVICE_REQUEST
));
1142 ClearPortRequest
.RequestType
= USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
);
1143 ClearPortRequest
.Request
= (UINT8
) USB_REQ_CLEAR_FEATURE
;
1144 ClearPortRequest
.Value
= mUsbHubClearPortChangeMap
[Index
].Selector
;
1145 ClearPortRequest
.Index
= Request
->Index
;
1146 ClearPortRequest
.Length
= 0;
1148 XhcControlTransfer (
1152 MaximumPacketLength
,
1164 XhcPollPortStatusChange (Xhc
, Xhc
->UsbDevContext
[SlotId
].RouteString
, (UINT8
)Request
->Index
, &PortStatus
);
1166 *(UINT32
*)Data
= *(UINT32
*)&PortStatus
;
1170 if (EFI_ERROR (Status
)) {
1171 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1174 gBS
->RestoreTPL (OldTpl
);
1181 Submits bulk transfer to a bulk endpoint of a USB device.
1183 @param This This EFI_USB2_HC_PROTOCOL instance.
1184 @param DeviceAddress Target device address.
1185 @param EndPointAddress Endpoint number and its direction in bit 7.
1186 @param DeviceSpeed Device speed, Low speed device doesn't support bulk
1188 @param MaximumPacketLength Maximum packet size the endpoint is capable of
1189 sending or receiving.
1190 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1191 @param Data Array of pointers to the buffers of data to transmit
1192 from or receive into.
1193 @param DataLength The lenght of the data buffer.
1194 @param DataToggle On input, the initial data toggle for the transfer;
1195 On output, it is updated to to next data toggle to
1196 use of the subsequent bulk transfer.
1197 @param Timeout Indicates the maximum time, in millisecond, which
1198 the transfer is allowed to complete.
1199 @param Translator A pointr to the transaction translator data.
1200 @param TransferResult A pointer to the detailed result information of the
1203 @retval EFI_SUCCESS The transfer was completed successfully.
1204 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1205 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1206 @retval EFI_TIMEOUT The transfer failed due to timeout.
1207 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1213 IN EFI_USB2_HC_PROTOCOL
*This
,
1214 IN UINT8 DeviceAddress
,
1215 IN UINT8 EndPointAddress
,
1216 IN UINT8 DeviceSpeed
,
1217 IN UINTN MaximumPacketLength
,
1218 IN UINT8 DataBuffersNumber
,
1219 IN OUT VOID
*Data
[EFI_USB_MAX_BULK_BUFFER_NUM
],
1220 IN OUT UINTN
*DataLength
,
1221 IN OUT UINT8
*DataToggle
,
1223 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1224 OUT UINT32
*TransferResult
1227 USB_XHCI_INSTANCE
*Xhc
;
1233 // Validate the parameters
1235 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1236 (Data
== NULL
) || (Data
[0] == NULL
) || (TransferResult
== NULL
)) {
1237 return EFI_INVALID_PARAMETER
;
1240 if ((*DataToggle
!= 0) && (*DataToggle
!= 1)) {
1241 return EFI_INVALID_PARAMETER
;
1244 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) ||
1245 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1246 ((EFI_USB_SPEED_HIGH
== DeviceSpeed
) && (MaximumPacketLength
> 512)) ||
1247 ((EFI_USB_SPEED_SUPER
== DeviceSpeed
) && (MaximumPacketLength
> 1024))) {
1248 return EFI_INVALID_PARAMETER
;
1251 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1253 Xhc
= XHC_FROM_THIS (This
);
1255 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1256 Status
= EFI_DEVICE_ERROR
;
1258 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1259 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: HC is halted\n"));
1264 // Check if the device is still enabled before every transaction.
1266 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1272 // Create a new URB, insert it into the asynchronous
1273 // schedule list, then poll the execution status.
1275 Status
= XhcTransfer (
1280 MaximumPacketLength
,
1290 if (EFI_ERROR (Status
)) {
1291 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1293 gBS
->RestoreTPL (OldTpl
);
1299 Submits an asynchronous interrupt transfer to an
1300 interrupt endpoint of a USB device.
1302 @param This This EFI_USB2_HC_PROTOCOL instance.
1303 @param DeviceAddress Target device address.
1304 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1305 @param DeviceSpeed Indicates device speed.
1306 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1307 @param IsNewTransfer If TRUE, to submit an new asynchronous interrupt
1308 transfer If FALSE, to remove the specified
1309 asynchronous interrupt.
1310 @param DataToggle On input, the initial data toggle to use; on output,
1311 it is updated to indicate the next data toggle.
1312 @param PollingInterval The he interval, in milliseconds, that the transfer
1314 @param DataLength The length of data to receive at the rate specified
1316 @param Translator Transaction translator to use.
1317 @param CallBackFunction Function to call at the rate specified by
1319 @param Context Context to CallBackFunction.
1321 @retval EFI_SUCCESS The request has been successfully submitted or canceled.
1322 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1323 @retval EFI_OUT_OF_RESOURCES The request failed due to a lack of resources.
1324 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1329 XhcAsyncInterruptTransfer (
1330 IN EFI_USB2_HC_PROTOCOL
*This
,
1331 IN UINT8 DeviceAddress
,
1332 IN UINT8 EndPointAddress
,
1333 IN UINT8 DeviceSpeed
,
1334 IN UINTN MaximumPacketLength
,
1335 IN BOOLEAN IsNewTransfer
,
1336 IN OUT UINT8
*DataToggle
,
1337 IN UINTN PollingInterval
,
1338 IN UINTN DataLength
,
1339 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1340 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction
,
1341 IN VOID
*Context OPTIONAL
1344 USB_XHCI_INSTANCE
*Xhc
;
1353 // Validate parameters
1355 if (!XHCI_IS_DATAIN (EndPointAddress
)) {
1356 return EFI_INVALID_PARAMETER
;
1359 if (IsNewTransfer
) {
1360 if (DataLength
== 0) {
1361 return EFI_INVALID_PARAMETER
;
1364 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1365 return EFI_INVALID_PARAMETER
;
1368 if ((PollingInterval
> 255) || (PollingInterval
< 1)) {
1369 return EFI_INVALID_PARAMETER
;
1373 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1375 Xhc
= XHC_FROM_THIS (This
);
1378 // Delete Async interrupt transfer request.
1380 if (!IsNewTransfer
) {
1382 // The delete request may happen after device is detached.
1384 for (Index
= 0; Index
< 255; Index
++) {
1385 if (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== DeviceAddress
) {
1391 Status
= EFI_INVALID_PARAMETER
;
1395 Status
= XhciDelAsyncIntTransfer (Xhc
, DeviceAddress
, EndPointAddress
);
1396 DEBUG ((EFI_D_INFO
, "XhcAsyncInterruptTransfer: remove old transfer for addr %d, Status = %r\n", DeviceAddress
, Status
));
1400 Status
= EFI_SUCCESS
;
1402 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1403 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: HC is halt\n"));
1404 Status
= EFI_DEVICE_ERROR
;
1409 // Check if the device is still enabled before every transaction.
1411 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1416 Data
= AllocateZeroPool (DataLength
);
1419 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: failed to allocate buffer\n"));
1420 Status
= EFI_OUT_OF_RESOURCES
;
1424 Urb
= XhcCreateUrb (
1429 MaximumPacketLength
,
1430 XHC_INT_TRANSFER_ASYNC
,
1439 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: failed to create URB\n"));
1441 Status
= EFI_OUT_OF_RESOURCES
;
1445 InsertHeadList (&Xhc
->AsyncIntTransfers
, &Urb
->UrbList
);
1447 // Ring the doorbell
1449 Status
= RingIntTransferDoorBell (Xhc
, Urb
);
1452 Xhc
->PciIo
->Flush (Xhc
->PciIo
);
1453 gBS
->RestoreTPL (OldTpl
);
1460 Submits synchronous interrupt transfer to an interrupt endpoint
1463 @param This This EFI_USB2_HC_PROTOCOL instance.
1464 @param DeviceAddress Target device address.
1465 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1466 @param DeviceSpeed Indicates device speed.
1467 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1468 of sending or receiving.
1469 @param Data Buffer of data that will be transmitted to USB
1470 device or received from USB device.
1471 @param DataLength On input, the size, in bytes, of the data buffer; On
1472 output, the number of bytes transferred.
1473 @param DataToggle On input, the initial data toggle to use; on output,
1474 it is updated to indicate the next data toggle.
1475 @param Timeout Maximum time, in second, to complete.
1476 @param Translator Transaction translator to use.
1477 @param TransferResult Variable to receive the transfer result.
1479 @return EFI_SUCCESS The transfer was completed successfully.
1480 @return EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1481 @return EFI_INVALID_PARAMETER Some parameters are invalid.
1482 @return EFI_TIMEOUT The transfer failed due to timeout.
1483 @return EFI_DEVICE_ERROR The failed due to host controller or device error
1488 XhcSyncInterruptTransfer (
1489 IN EFI_USB2_HC_PROTOCOL
*This
,
1490 IN UINT8 DeviceAddress
,
1491 IN UINT8 EndPointAddress
,
1492 IN UINT8 DeviceSpeed
,
1493 IN UINTN MaximumPacketLength
,
1495 IN OUT UINTN
*DataLength
,
1496 IN OUT UINT8
*DataToggle
,
1498 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1499 OUT UINT32
*TransferResult
1502 USB_XHCI_INSTANCE
*Xhc
;
1508 // Validates parameters
1510 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1511 (Data
== NULL
) || (TransferResult
== NULL
)) {
1512 return EFI_INVALID_PARAMETER
;
1515 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1516 return EFI_INVALID_PARAMETER
;
1519 if (((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) ||
1520 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1521 ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) && (MaximumPacketLength
> 3072))) {
1522 return EFI_INVALID_PARAMETER
;
1525 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1527 Xhc
= XHC_FROM_THIS (This
);
1529 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1530 Status
= EFI_DEVICE_ERROR
;
1532 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1533 DEBUG ((EFI_D_ERROR
, "EhcSyncInterruptTransfer: HC is halt\n"));
1538 // Check if the device is still enabled before every transaction.
1540 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1545 Status
= XhcTransfer (
1550 MaximumPacketLength
,
1551 XHC_INT_TRANSFER_SYNC
,
1560 if (EFI_ERROR (Status
)) {
1561 DEBUG ((EFI_D_ERROR
, "XhcSyncInterruptTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1563 gBS
->RestoreTPL (OldTpl
);
1570 Submits isochronous transfer to a target USB device.
1572 @param This This EFI_USB2_HC_PROTOCOL instance.
1573 @param DeviceAddress Target device address.
1574 @param EndPointAddress End point address with its direction.
1575 @param DeviceSpeed Device speed, Low speed device doesn't support this
1577 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1578 sending or receiving.
1579 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1580 @param Data Array of pointers to the buffers of data that will
1581 be transmitted to USB device or received from USB
1583 @param DataLength The size, in bytes, of the data buffer.
1584 @param Translator Transaction translator to use.
1585 @param TransferResult Variable to receive the transfer result.
1587 @return EFI_UNSUPPORTED Isochronous transfer is unsupported.
1592 XhcIsochronousTransfer (
1593 IN EFI_USB2_HC_PROTOCOL
*This
,
1594 IN UINT8 DeviceAddress
,
1595 IN UINT8 EndPointAddress
,
1596 IN UINT8 DeviceSpeed
,
1597 IN UINTN MaximumPacketLength
,
1598 IN UINT8 DataBuffersNumber
,
1599 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1600 IN UINTN DataLength
,
1601 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1602 OUT UINT32
*TransferResult
1605 return EFI_UNSUPPORTED
;
1610 Submits Async isochronous transfer to a target USB device.
1612 @param This This EFI_USB2_HC_PROTOCOL instance.
1613 @param DeviceAddress Target device address.
1614 @param EndPointAddress End point address with its direction.
1615 @param DeviceSpeed Device speed, Low speed device doesn't support this
1617 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1618 sending or receiving.
1619 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1620 @param Data Array of pointers to the buffers of data that will
1621 be transmitted to USB device or received from USB
1623 @param DataLength The size, in bytes, of the data buffer.
1624 @param Translator Transaction translator to use.
1625 @param IsochronousCallBack Function to be called when the transfer complete.
1626 @param Context Context passed to the call back function as
1629 @return EFI_UNSUPPORTED Isochronous transfer isn't supported.
1634 XhcAsyncIsochronousTransfer (
1635 IN EFI_USB2_HC_PROTOCOL
*This
,
1636 IN UINT8 DeviceAddress
,
1637 IN UINT8 EndPointAddress
,
1638 IN UINT8 DeviceSpeed
,
1639 IN UINTN MaximumPacketLength
,
1640 IN UINT8 DataBuffersNumber
,
1641 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1642 IN UINTN DataLength
,
1643 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1644 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
1648 return EFI_UNSUPPORTED
;
1652 Entry point for EFI drivers.
1654 @param ImageHandle EFI_HANDLE.
1655 @param SystemTable EFI_SYSTEM_TABLE.
1657 @retval EFI_SUCCESS Success.
1658 @retval Others Fail.
1663 XhcDriverEntryPoint (
1664 IN EFI_HANDLE ImageHandle
,
1665 IN EFI_SYSTEM_TABLE
*SystemTable
1668 return EfiLibInstallDriverBindingComponentName2 (
1671 &gXhciDriverBinding
,
1673 &gXhciComponentName
,
1674 &gXhciComponentName2
1680 Test to see if this driver supports ControllerHandle. Any
1681 ControllerHandle that has Usb2HcProtocol installed will
1684 @param This Protocol instance pointer.
1685 @param Controller Handle of device to test.
1686 @param RemainingDevicePath Not used.
1688 @return EFI_SUCCESS This driver supports this device.
1689 @return EFI_UNSUPPORTED This driver does not support this device.
1694 XhcDriverBindingSupported (
1695 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1696 IN EFI_HANDLE Controller
,
1697 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1701 EFI_PCI_IO_PROTOCOL
*PciIo
;
1702 USB_CLASSC UsbClassCReg
;
1705 // Test whether there is PCI IO Protocol attached on the controller handle.
1707 Status
= gBS
->OpenProtocol (
1709 &gEfiPciIoProtocolGuid
,
1711 This
->DriverBindingHandle
,
1713 EFI_OPEN_PROTOCOL_BY_DRIVER
1716 if (EFI_ERROR (Status
)) {
1717 return EFI_UNSUPPORTED
;
1720 Status
= PciIo
->Pci
.Read (
1723 PCI_CLASSCODE_OFFSET
,
1724 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1728 if (EFI_ERROR (Status
)) {
1729 Status
= EFI_UNSUPPORTED
;
1734 // Test whether the controller belongs to Xhci type
1736 if ((UsbClassCReg
.BaseCode
!= PCI_CLASS_SERIAL
) ||
1737 (UsbClassCReg
.SubClassCode
!= PCI_CLASS_SERIAL_USB
) ||
1738 (UsbClassCReg
.ProgInterface
!= PCI_IF_XHCI
)) {
1739 Status
= EFI_UNSUPPORTED
;
1743 gBS
->CloseProtocol (
1745 &gEfiPciIoProtocolGuid
,
1746 This
->DriverBindingHandle
,
1754 Create and initialize a USB_XHCI_INSTANCE structure.
1756 @param PciIo The PciIo on this device.
1757 @param DevicePath The device path of host controller.
1758 @param OriginalPciAttributes Original PCI attributes.
1760 @return The allocated and initialized USB_XHCI_INSTANCE structure if created,
1766 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1767 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1768 IN UINT64 OriginalPciAttributes
1771 USB_XHCI_INSTANCE
*Xhc
;
1775 UINT8 ReleaseNumber
;
1777 Xhc
= AllocateZeroPool (sizeof (USB_XHCI_INSTANCE
));
1784 // Initialize private data structure
1786 Xhc
->Signature
= XHCI_INSTANCE_SIG
;
1788 Xhc
->DevicePath
= DevicePath
;
1789 Xhc
->OriginalPciAttributes
= OriginalPciAttributes
;
1790 CopyMem (&Xhc
->Usb2Hc
, &gXhciUsb2HcTemplate
, sizeof (EFI_USB2_HC_PROTOCOL
));
1792 Status
= PciIo
->Pci
.Read (
1795 XHC_PCI_SBRN_OFFSET
,
1800 if (!EFI_ERROR (Status
)) {
1801 Xhc
->Usb2Hc
.MajorRevision
= (ReleaseNumber
& 0xF0) >> 4;
1802 Xhc
->Usb2Hc
.MinorRevision
= (ReleaseNumber
& 0x0F);
1805 InitializeListHead (&Xhc
->AsyncIntTransfers
);
1808 // Be caution that the Offset passed to XhcReadCapReg() should be Dword align
1810 Xhc
->CapLength
= XhcReadCapReg8 (Xhc
, XHC_CAPLENGTH_OFFSET
);
1811 Xhc
->HcSParams1
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS1_OFFSET
);
1812 Xhc
->HcSParams2
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS2_OFFSET
);
1813 Xhc
->HcCParams
.Dword
= XhcReadCapReg (Xhc
, XHC_HCCPARAMS_OFFSET
);
1814 Xhc
->DBOff
= XhcReadCapReg (Xhc
, XHC_DBOFF_OFFSET
);
1815 Xhc
->RTSOff
= XhcReadCapReg (Xhc
, XHC_RTSOFF_OFFSET
);
1818 // This PageSize field defines the page size supported by the xHC implementation.
1819 // This xHC supports a page size of 2^(n+12) if bit n is Set. For example,
1820 // if bit 0 is Set, the xHC supports 4k byte page sizes.
1822 PageSize
= XhcReadOpReg(Xhc
, XHC_PAGESIZE_OFFSET
) & XHC_PAGESIZE_MASK
;
1823 Xhc
->PageSize
= 1 << (HighBitSet32(PageSize
) + 12);
1825 ExtCapReg
= (UINT16
) (Xhc
->HcCParams
.Data
.ExtCapReg
);
1826 Xhc
->ExtCapRegBase
= ExtCapReg
<< 2;
1827 Xhc
->UsbLegSupOffset
= XhcGetCapabilityAddr (Xhc
, XHC_CAP_USB_LEGACY
);
1828 Xhc
->DebugCapSupOffset
= XhcGetCapabilityAddr (Xhc
, XHC_CAP_USB_DEBUG
);
1830 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: Capability length 0x%x\n", Xhc
->CapLength
));
1831 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcSParams1 0x%x\n", Xhc
->HcSParams1
));
1832 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcSParams2 0x%x\n", Xhc
->HcSParams2
));
1833 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcCParams 0x%x\n", Xhc
->HcCParams
));
1834 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: DBOff 0x%x\n", Xhc
->DBOff
));
1835 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: RTSOff 0x%x\n", Xhc
->RTSOff
));
1836 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: UsbLegSupOffset 0x%x\n", Xhc
->UsbLegSupOffset
));
1837 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: DebugCapSupOffset 0x%x\n", Xhc
->DebugCapSupOffset
));
1840 // Create AsyncRequest Polling Timer
1842 Status
= gBS
->CreateEvent (
1843 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1845 XhcMonitorAsyncRequests
,
1850 if (EFI_ERROR (Status
)) {
1862 One notified function to stop the Host Controller when gBS->ExitBootServices() called.
1864 @param Event Pointer to this event
1865 @param Context Event handler private data
1870 XhcExitBootService (
1876 USB_XHCI_INSTANCE
*Xhc
;
1877 EFI_PCI_IO_PROTOCOL
*PciIo
;
1879 Xhc
= (USB_XHCI_INSTANCE
*) Context
;
1883 // Stop AsyncRequest Polling timer then stop the XHCI driver
1884 // and uninstall the XHCI protocl.
1886 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
1887 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
1889 if (Xhc
->PollTimer
!= NULL
) {
1890 gBS
->CloseEvent (Xhc
->PollTimer
);
1893 XhcClearBiosOwnership (Xhc
);
1896 // Restore original PCI attributes
1900 EfiPciIoAttributeOperationSet
,
1901 Xhc
->OriginalPciAttributes
,
1907 Starting the Usb XHCI Driver.
1909 @param This Protocol instance pointer.
1910 @param Controller Handle of device to test.
1911 @param RemainingDevicePath Not used.
1913 @return EFI_SUCCESS supports this device.
1914 @return EFI_UNSUPPORTED do not support this device.
1915 @return EFI_DEVICE_ERROR cannot be started due to device Error.
1916 @return EFI_OUT_OF_RESOURCES cannot allocate resources.
1921 XhcDriverBindingStart (
1922 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1923 IN EFI_HANDLE Controller
,
1924 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1928 EFI_PCI_IO_PROTOCOL
*PciIo
;
1930 UINT64 OriginalPciAttributes
;
1931 BOOLEAN PciAttributesSaved
;
1932 USB_XHCI_INSTANCE
*Xhc
;
1933 EFI_DEVICE_PATH_PROTOCOL
*HcDevicePath
;
1936 // Open the PciIo Protocol, then enable the USB host controller
1938 Status
= gBS
->OpenProtocol (
1940 &gEfiPciIoProtocolGuid
,
1942 This
->DriverBindingHandle
,
1944 EFI_OPEN_PROTOCOL_BY_DRIVER
1947 if (EFI_ERROR (Status
)) {
1952 // Open Device Path Protocol for on USB host controller
1954 HcDevicePath
= NULL
;
1955 Status
= gBS
->OpenProtocol (
1957 &gEfiDevicePathProtocolGuid
,
1958 (VOID
**) &HcDevicePath
,
1959 This
->DriverBindingHandle
,
1961 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1964 PciAttributesSaved
= FALSE
;
1966 // Save original PCI attributes
1968 Status
= PciIo
->Attributes (
1970 EfiPciIoAttributeOperationGet
,
1972 &OriginalPciAttributes
1975 if (EFI_ERROR (Status
)) {
1978 PciAttributesSaved
= TRUE
;
1980 Status
= PciIo
->Attributes (
1982 EfiPciIoAttributeOperationSupported
,
1986 if (!EFI_ERROR (Status
)) {
1987 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
1988 Status
= PciIo
->Attributes (
1990 EfiPciIoAttributeOperationEnable
,
1996 if (EFI_ERROR (Status
)) {
1997 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to enable controller\n"));
2002 // Create then install USB2_HC_PROTOCOL
2004 Xhc
= XhcCreateUsbHc (PciIo
, HcDevicePath
, OriginalPciAttributes
);
2007 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to create USB2_HC\n"));
2008 return EFI_OUT_OF_RESOURCES
;
2012 // Enable 64-bit DMA support in the PCI layer if this controller
2015 if (Xhc
->HcCParams
.Data
.Ac64
!= 0) {
2016 Status
= PciIo
->Attributes (
2018 EfiPciIoAttributeOperationEnable
,
2019 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
2022 if (!EFI_ERROR (Status
)) {
2023 Xhc
->Support64BitDma
= TRUE
;
2026 "%a: failed to enable 64-bit DMA on 64-bit capable controller @ %p (%r)\n",
2027 __FUNCTION__
, Controller
, Status
));
2031 XhcSetBiosOwnership (Xhc
);
2033 XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
2034 ASSERT (XhcIsHalt (Xhc
));
2037 // After Chip Hardware Reset wait until the Controller Not Ready (CNR) flag
2038 // in the USBSTS is '0' before writing any xHC Operational or Runtime registers.
2040 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
2043 // Initialize the schedule
2048 // Start the Host Controller
2050 XhcRunHC(Xhc
, XHC_GENERIC_TIMEOUT
);
2053 // Start the asynchronous interrupt monitor
2055 Status
= gBS
->SetTimer (Xhc
->PollTimer
, TimerPeriodic
, XHC_ASYNC_TIMER_INTERVAL
);
2056 if (EFI_ERROR (Status
)) {
2057 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to start async interrupt monitor\n"));
2058 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2063 // Create event to stop the HC when exit boot service.
2065 Status
= gBS
->CreateEventEx (
2070 &gEfiEventExitBootServicesGuid
,
2071 &Xhc
->ExitBootServiceEvent
2073 if (EFI_ERROR (Status
)) {
2078 // Install the component name protocol, don't fail the start
2079 // because of something for display.
2083 gXhciComponentName
.SupportedLanguages
,
2084 &Xhc
->ControllerNameTable
,
2085 L
"eXtensible Host Controller (USB 3.0)",
2090 gXhciComponentName2
.SupportedLanguages
,
2091 &Xhc
->ControllerNameTable
,
2092 L
"eXtensible Host Controller (USB 3.0)",
2096 Status
= gBS
->InstallProtocolInterface (
2098 &gEfiUsb2HcProtocolGuid
,
2099 EFI_NATIVE_INTERFACE
,
2102 if (EFI_ERROR (Status
)) {
2103 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to install USB2_HC Protocol\n"));
2107 DEBUG ((EFI_D_INFO
, "XhcDriverBindingStart: XHCI started for controller @ %x\n", Controller
));
2111 gBS
->CloseEvent (Xhc
->PollTimer
);
2116 if (PciAttributesSaved
) {
2118 // Restore original PCI attributes
2122 EfiPciIoAttributeOperationSet
,
2123 OriginalPciAttributes
,
2128 gBS
->CloseProtocol (
2130 &gEfiPciIoProtocolGuid
,
2131 This
->DriverBindingHandle
,
2140 Stop this driver on ControllerHandle. Support stopping any child handles
2141 created by this driver.
2143 @param This Protocol instance pointer.
2144 @param Controller Handle of device to stop driver on.
2145 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
2146 @param ChildHandleBuffer List of handles for the children we need to stop.
2148 @return EFI_SUCCESS Success.
2149 @return EFI_DEVICE_ERROR Fail.
2154 XhcDriverBindingStop (
2155 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
2156 IN EFI_HANDLE Controller
,
2157 IN UINTN NumberOfChildren
,
2158 IN EFI_HANDLE
*ChildHandleBuffer
2162 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
2163 EFI_PCI_IO_PROTOCOL
*PciIo
;
2164 USB_XHCI_INSTANCE
*Xhc
;
2168 // Test whether the Controller handler passed in is a valid
2169 // Usb controller handle that should be supported, if not,
2170 // return the error status directly
2172 Status
= gBS
->OpenProtocol (
2174 &gEfiUsb2HcProtocolGuid
,
2176 This
->DriverBindingHandle
,
2178 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2181 if (EFI_ERROR (Status
)) {
2185 Status
= gBS
->UninstallProtocolInterface (
2187 &gEfiUsb2HcProtocolGuid
,
2191 if (EFI_ERROR (Status
)) {
2195 Xhc
= XHC_FROM_THIS (Usb2Hc
);
2199 // Stop AsyncRequest Polling timer then stop the XHCI driver
2200 // and uninstall the XHCI protocl.
2202 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
2205 // Disable the device slots occupied by these devices on its downstream ports.
2206 // Entry 0 is reserved.
2208 for (Index
= 0; Index
< 255; Index
++) {
2209 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
||
2210 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0)) {
2213 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
2214 XhcDisableSlotCmd (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2216 XhcDisableSlotCmd64 (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2220 if (Xhc
->PollTimer
!= NULL
) {
2221 gBS
->CloseEvent (Xhc
->PollTimer
);
2224 if (Xhc
->ExitBootServiceEvent
!= NULL
) {
2225 gBS
->CloseEvent (Xhc
->ExitBootServiceEvent
);
2228 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2229 XhcClearBiosOwnership (Xhc
);
2230 XhciDelAllAsyncIntTransfers (Xhc
);
2233 if (Xhc
->ControllerNameTable
) {
2234 FreeUnicodeStringTable (Xhc
->ControllerNameTable
);
2238 // Restore original PCI attributes
2242 EfiPciIoAttributeOperationSet
,
2243 Xhc
->OriginalPciAttributes
,
2247 gBS
->CloseProtocol (
2249 &gEfiPciIoProtocolGuid
,
2250 This
->DriverBindingHandle
,