2 The XHCI controller driver.
4 Copyright (c) 2011 - 2022, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
12 // Two arrays used to translate the XHCI port state (change)
13 // to the UEFI protocol's port state (change).
15 USB_PORT_STATE_MAP mUsbPortStateMap
[] = {
16 { XHC_PORTSC_CCS
, USB_PORT_STAT_CONNECTION
},
17 { XHC_PORTSC_PED
, USB_PORT_STAT_ENABLE
},
18 { XHC_PORTSC_OCA
, USB_PORT_STAT_OVERCURRENT
},
19 { XHC_PORTSC_RESET
, USB_PORT_STAT_RESET
}
22 USB_PORT_STATE_MAP mUsbPortChangeMap
[] = {
23 { XHC_PORTSC_CSC
, USB_PORT_STAT_C_CONNECTION
},
24 { XHC_PORTSC_PEC
, USB_PORT_STAT_C_ENABLE
},
25 { XHC_PORTSC_OCC
, USB_PORT_STAT_C_OVERCURRENT
},
26 { XHC_PORTSC_PRC
, USB_PORT_STAT_C_RESET
}
29 USB_CLEAR_PORT_MAP mUsbClearPortChangeMap
[] = {
30 { XHC_PORTSC_CSC
, EfiUsbPortConnectChange
},
31 { XHC_PORTSC_PEC
, EfiUsbPortEnableChange
},
32 { XHC_PORTSC_OCC
, EfiUsbPortOverCurrentChange
},
33 { XHC_PORTSC_PRC
, EfiUsbPortResetChange
}
36 USB_PORT_STATE_MAP mUsbHubPortStateMap
[] = {
37 { XHC_HUB_PORTSC_CCS
, USB_PORT_STAT_CONNECTION
},
38 { XHC_HUB_PORTSC_PED
, USB_PORT_STAT_ENABLE
},
39 { XHC_HUB_PORTSC_OCA
, USB_PORT_STAT_OVERCURRENT
},
40 { XHC_HUB_PORTSC_RESET
, USB_PORT_STAT_RESET
}
43 USB_PORT_STATE_MAP mUsbHubPortChangeMap
[] = {
44 { XHC_HUB_PORTSC_CSC
, USB_PORT_STAT_C_CONNECTION
},
45 { XHC_HUB_PORTSC_PEC
, USB_PORT_STAT_C_ENABLE
},
46 { XHC_HUB_PORTSC_OCC
, USB_PORT_STAT_C_OVERCURRENT
},
47 { XHC_HUB_PORTSC_PRC
, USB_PORT_STAT_C_RESET
}
50 USB_CLEAR_PORT_MAP mUsbHubClearPortChangeMap
[] = {
51 { XHC_HUB_PORTSC_CSC
, EfiUsbPortConnectChange
},
52 { XHC_HUB_PORTSC_PEC
, EfiUsbPortEnableChange
},
53 { XHC_HUB_PORTSC_OCC
, EfiUsbPortOverCurrentChange
},
54 { XHC_HUB_PORTSC_PRC
, EfiUsbPortResetChange
},
55 { XHC_HUB_PORTSC_BHRC
, Usb3PortBHPortResetChange
}
58 EFI_DRIVER_BINDING_PROTOCOL gXhciDriverBinding
= {
59 XhcDriverBindingSupported
,
60 XhcDriverBindingStart
,
68 // Template for Xhci's Usb2 Host Controller Protocol Instance.
70 EFI_USB2_HC_PROTOCOL gXhciUsb2HcTemplate
= {
77 XhcAsyncInterruptTransfer
,
78 XhcSyncInterruptTransfer
,
79 XhcIsochronousTransfer
,
80 XhcAsyncIsochronousTransfer
,
81 XhcGetRootHubPortStatus
,
82 XhcSetRootHubPortFeature
,
83 XhcClearRootHubPortFeature
,
89 Retrieves the capability of root hub ports.
91 @param This The EFI_USB2_HC_PROTOCOL instance.
92 @param MaxSpeed Max speed supported by the controller.
93 @param PortNumber Number of the root hub ports.
94 @param Is64BitCapable Whether the controller supports 64-bit memory
97 @retval EFI_SUCCESS Host controller capability were retrieved successfully.
98 @retval EFI_INVALID_PARAMETER Either of the three capability pointer is NULL.
104 IN EFI_USB2_HC_PROTOCOL
*This
,
106 OUT UINT8
*PortNumber
,
107 OUT UINT8
*Is64BitCapable
110 USB_XHCI_INSTANCE
*Xhc
;
113 if ((MaxSpeed
== NULL
) || (PortNumber
== NULL
) || (Is64BitCapable
== NULL
)) {
114 return EFI_INVALID_PARAMETER
;
117 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
119 Xhc
= XHC_FROM_THIS (This
);
120 *MaxSpeed
= EFI_USB_SPEED_SUPER
;
121 *PortNumber
= (UINT8
)(Xhc
->HcSParams1
.Data
.MaxPorts
);
122 *Is64BitCapable
= (UINT8
)Xhc
->Support64BitDma
;
123 DEBUG ((DEBUG_INFO
, "XhcGetCapability: %d ports, 64 bit %d\n", *PortNumber
, *Is64BitCapable
));
125 gBS
->RestoreTPL (OldTpl
);
131 Provides software reset for the USB host controller.
133 @param This This EFI_USB2_HC_PROTOCOL instance.
134 @param Attributes A bit mask of the reset operation to perform.
136 @retval EFI_SUCCESS The reset operation succeeded.
137 @retval EFI_INVALID_PARAMETER Attributes is not valid.
138 @retval EFI_UNSUPPOURTED The type of reset specified by Attributes is
139 not currently supported by the host controller.
140 @retval EFI_DEVICE_ERROR Host controller isn't halted to reset.
146 IN EFI_USB2_HC_PROTOCOL
*This
,
150 USB_XHCI_INSTANCE
*Xhc
;
154 Xhc
= XHC_FROM_THIS (This
);
156 if (Xhc
->DevicePath
!= NULL
) {
158 // Report Status Code to indicate reset happens
160 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
162 (EFI_IO_BUS_USB
| EFI_IOB_PC_RESET
),
167 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
169 switch (Attributes
) {
170 case EFI_USB_HC_RESET_GLOBAL
:
172 // Flow through, same behavior as Host Controller Reset
174 case EFI_USB_HC_RESET_HOST_CONTROLLER
:
175 if ((Xhc
->DebugCapSupOffset
!= 0xFFFFFFFF) && ((XhcReadExtCapReg (Xhc
, Xhc
->DebugCapSupOffset
) & 0xFF) == XHC_CAP_USB_DEBUG
) &&
176 ((XhcReadExtCapReg (Xhc
, Xhc
->DebugCapSupOffset
+ XHC_DC_DCCTRL
) & BIT0
) != 0))
178 Status
= EFI_SUCCESS
;
183 // Host Controller must be Halt when Reset it
185 if (!XhcIsHalt (Xhc
)) {
186 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
188 if (EFI_ERROR (Status
)) {
189 Status
= EFI_DEVICE_ERROR
;
194 Status
= XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
195 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
197 if (EFI_ERROR (Status
)) {
202 // Clean up the asynchronous transfers, currently only
203 // interrupt supports asynchronous operation.
205 XhciDelAllAsyncIntTransfers (Xhc
);
211 case EFI_USB_HC_RESET_GLOBAL_WITH_DEBUG
:
212 case EFI_USB_HC_RESET_HOST_WITH_DEBUG
:
213 Status
= EFI_UNSUPPORTED
;
217 Status
= EFI_INVALID_PARAMETER
;
221 DEBUG ((DEBUG_INFO
, "XhcReset: status %r\n", Status
));
222 gBS
->RestoreTPL (OldTpl
);
228 Retrieve the current state of the USB host controller.
230 @param This This EFI_USB2_HC_PROTOCOL instance.
231 @param State Variable to return the current host controller
234 @retval EFI_SUCCESS Host controller state was returned in State.
235 @retval EFI_INVALID_PARAMETER State is NULL.
236 @retval EFI_DEVICE_ERROR An error was encountered while attempting to
237 retrieve the host controller's current state.
243 IN EFI_USB2_HC_PROTOCOL
*This
,
244 OUT EFI_USB_HC_STATE
*State
247 USB_XHCI_INSTANCE
*Xhc
;
251 return EFI_INVALID_PARAMETER
;
254 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
256 Xhc
= XHC_FROM_THIS (This
);
258 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
259 *State
= EfiUsbHcStateHalt
;
261 *State
= EfiUsbHcStateOperational
;
264 DEBUG ((DEBUG_INFO
, "XhcGetState: current state %d\n", *State
));
265 gBS
->RestoreTPL (OldTpl
);
271 Sets the USB host controller to a specific state.
273 @param This This EFI_USB2_HC_PROTOCOL instance.
274 @param State The state of the host controller that will be set.
276 @retval EFI_SUCCESS The USB host controller was successfully placed
277 in the state specified by State.
278 @retval EFI_INVALID_PARAMETER State is invalid.
279 @retval EFI_DEVICE_ERROR Failed to set the state due to device error.
285 IN EFI_USB2_HC_PROTOCOL
*This
,
286 IN EFI_USB_HC_STATE State
289 USB_XHCI_INSTANCE
*Xhc
;
291 EFI_USB_HC_STATE CurState
;
294 Status
= XhcGetState (This
, &CurState
);
296 if (EFI_ERROR (Status
)) {
297 return EFI_DEVICE_ERROR
;
300 if (CurState
== State
) {
304 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
306 Xhc
= XHC_FROM_THIS (This
);
309 case EfiUsbHcStateHalt
:
310 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
313 case EfiUsbHcStateOperational
:
314 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HSE
)) {
315 Status
= EFI_DEVICE_ERROR
;
320 // Software must not write a one to this field unless the host controller
321 // is in the Halted state. Doing so will yield undefined results.
322 // refers to Spec[XHCI1.0-2.3.1]
324 if (!XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
325 Status
= EFI_DEVICE_ERROR
;
329 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
332 case EfiUsbHcStateSuspend
:
333 Status
= EFI_UNSUPPORTED
;
337 Status
= EFI_INVALID_PARAMETER
;
340 DEBUG ((DEBUG_INFO
, "XhcSetState: status %r\n", Status
));
341 gBS
->RestoreTPL (OldTpl
);
347 Retrieves the current status of a USB root hub port.
349 @param This This EFI_USB2_HC_PROTOCOL instance.
350 @param PortNumber The root hub port to retrieve the state from.
351 This value is zero-based.
352 @param PortStatus Variable to receive the port state.
354 @retval EFI_SUCCESS The status of the USB root hub port specified.
355 by PortNumber was returned in PortStatus.
356 @retval EFI_INVALID_PARAMETER PortNumber is invalid.
357 @retval EFI_DEVICE_ERROR Can't read register.
362 XhcGetRootHubPortStatus (
363 IN EFI_USB2_HC_PROTOCOL
*This
,
365 OUT EFI_USB_PORT_STATUS
*PortStatus
368 USB_XHCI_INSTANCE
*Xhc
;
376 USB_DEV_ROUTE ParentRouteChart
;
379 if (PortStatus
== NULL
) {
380 return EFI_INVALID_PARAMETER
;
383 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
385 Xhc
= XHC_FROM_THIS (This
);
386 Status
= EFI_SUCCESS
;
388 TotalPort
= Xhc
->HcSParams1
.Data
.MaxPorts
;
390 if (PortNumber
>= TotalPort
) {
391 Status
= EFI_INVALID_PARAMETER
;
395 Offset
= (UINT32
)(XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
396 PortStatus
->PortStatus
= 0;
397 PortStatus
->PortChangeStatus
= 0;
399 State
= XhcReadOpReg (Xhc
, Offset
);
401 PortSpeed
= (State
& XHC_PORTSC_PS
) >> 10;
404 // According to XHCI 1.1 spec November 2017,
405 // Section 7.2 xHCI Support Protocol Capability
408 PortStatus
->PortStatus
= XhcCheckUsbPortSpeedUsedPsic (Xhc
, PortSpeed
, PortNumber
);
409 // If no match found in ext cap reg, fall back to PORTSC
410 if (PortStatus
->PortStatus
== 0) {
412 // According to XHCI 1.1 spec November 2017,
413 // bit 10~13 of the root port status register identifies the speed of the attached device.
417 PortStatus
->PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
421 PortStatus
->PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
426 PortStatus
->PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
436 // Convert the XHCI port/port change state to UEFI status
438 MapSize
= sizeof (mUsbPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
440 for (Index
= 0; Index
< MapSize
; Index
++) {
441 if (XHC_BIT_IS_SET (State
, mUsbPortStateMap
[Index
].HwState
)) {
442 PortStatus
->PortStatus
= (UINT16
)(PortStatus
->PortStatus
| mUsbPortStateMap
[Index
].UefiState
);
447 // Bit5~8 reflects its current link state.
449 if ((State
& XHC_PORTSC_PLS
) >> 5 == 3) {
450 PortStatus
->PortStatus
|= USB_PORT_STAT_SUSPEND
;
453 MapSize
= sizeof (mUsbPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
455 for (Index
= 0; Index
< MapSize
; Index
++) {
456 if (XHC_BIT_IS_SET (State
, mUsbPortChangeMap
[Index
].HwState
)) {
457 PortStatus
->PortChangeStatus
= (UINT16
)(PortStatus
->PortChangeStatus
| mUsbPortChangeMap
[Index
].UefiState
);
461 MapSize
= sizeof (mUsbClearPortChangeMap
) / sizeof (USB_CLEAR_PORT_MAP
);
463 for (Index
= 0; Index
< MapSize
; Index
++) {
464 if (XHC_BIT_IS_SET (State
, mUsbClearPortChangeMap
[Index
].HwState
)) {
465 XhcClearRootHubPortFeature (This
, PortNumber
, (EFI_USB_PORT_FEATURE
)mUsbClearPortChangeMap
[Index
].Selector
);
470 // Poll the root port status register to enable/disable corresponding device slot if there is a device attached/detached.
471 // For those devices behind hub, we get its attach/detach event by hooking Get_Port_Status request at control transfer for those hub.
473 ParentRouteChart
.Dword
= 0;
474 Status
= XhcPollPortStatusChange (Xhc
, ParentRouteChart
, PortNumber
, PortStatus
);
477 // Force resetting the port by clearing the USB_PORT_STAT_C_RESET bit in PortChangeStatus
478 // when XhcPollPortStatusChange fails
480 if (EFI_ERROR (Status
)) {
481 PortStatus
->PortChangeStatus
&= ~(USB_PORT_STAT_C_RESET
);
482 Status
= EFI_SUCCESS
;
486 gBS
->RestoreTPL (OldTpl
);
491 Sets a feature for the specified root hub port.
493 @param This This EFI_USB2_HC_PROTOCOL instance.
494 @param PortNumber Root hub port to set.
495 @param PortFeature Feature to set.
497 @retval EFI_SUCCESS The feature specified by PortFeature was set.
498 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
499 @retval EFI_DEVICE_ERROR Can't read register.
504 XhcSetRootHubPortFeature (
505 IN EFI_USB2_HC_PROTOCOL
*This
,
507 IN EFI_USB_PORT_FEATURE PortFeature
510 USB_XHCI_INSTANCE
*Xhc
;
517 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
519 Xhc
= XHC_FROM_THIS (This
);
520 Status
= EFI_SUCCESS
;
522 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
524 if (PortNumber
>= TotalPort
) {
525 Status
= EFI_INVALID_PARAMETER
;
529 Offset
= (UINT32
)(XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
530 State
= XhcReadOpReg (Xhc
, Offset
);
533 // Mask off the port status change bits, these bits are
536 State
&= ~(BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
538 switch (PortFeature
) {
539 case EfiUsbPortEnable
:
541 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
542 // A port may be disabled by software writing a '1' to this flag.
544 Status
= EFI_SUCCESS
;
547 case EfiUsbPortSuspend
:
548 State
|= XHC_PORTSC_LWS
;
549 XhcWriteOpReg (Xhc
, Offset
, State
);
550 State
&= ~XHC_PORTSC_PLS
;
552 XhcWriteOpReg (Xhc
, Offset
, State
);
555 case EfiUsbPortReset
:
556 DEBUG ((DEBUG_INFO
, "XhcUsbPortReset!\n"));
558 // Make sure Host Controller not halt before reset it
560 if (XhcIsHalt (Xhc
)) {
561 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
563 if (EFI_ERROR (Status
)) {
564 DEBUG ((DEBUG_INFO
, "XhcSetRootHubPortFeature :failed to start HC - %r\n", Status
));
570 // 4.3.1 Resetting a Root Hub Port
571 // 1) Write the PORTSC register with the Port Reset (PR) bit set to '1'.
573 State
|= XHC_PORTSC_RESET
;
574 XhcWriteOpReg (Xhc
, Offset
, State
);
575 XhcWaitOpRegBit (Xhc
, Offset
, XHC_PORTSC_PRC
, TRUE
, XHC_GENERIC_TIMEOUT
);
578 case EfiUsbPortPower
:
580 // Not supported, ignore the operation
582 Status
= EFI_SUCCESS
;
585 case EfiUsbPortOwner
:
587 // XHCI root hub port don't has the owner bit, ignore the operation
589 Status
= EFI_SUCCESS
;
593 Status
= EFI_INVALID_PARAMETER
;
597 DEBUG ((DEBUG_INFO
, "XhcSetRootHubPortFeature: status %r\n", Status
));
598 gBS
->RestoreTPL (OldTpl
);
604 Clears a feature for the specified root hub port.
606 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
607 @param PortNumber Specifies the root hub port whose feature is
608 requested to be cleared.
609 @param PortFeature Indicates the feature selector associated with the
610 feature clear request.
612 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
613 for the USB root hub port specified by PortNumber.
614 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
615 @retval EFI_DEVICE_ERROR Can't read register.
620 XhcClearRootHubPortFeature (
621 IN EFI_USB2_HC_PROTOCOL
*This
,
623 IN EFI_USB_PORT_FEATURE PortFeature
626 USB_XHCI_INSTANCE
*Xhc
;
633 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
635 Xhc
= XHC_FROM_THIS (This
);
636 Status
= EFI_SUCCESS
;
638 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
640 if (PortNumber
>= TotalPort
) {
641 Status
= EFI_INVALID_PARAMETER
;
645 Offset
= XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
);
648 // Mask off the port status change bits, these bits are
651 State
= XhcReadOpReg (Xhc
, Offset
);
652 State
&= ~(BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
654 switch (PortFeature
) {
655 case EfiUsbPortEnable
:
657 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
658 // A port may be disabled by software writing a '1' to this flag.
660 State
|= XHC_PORTSC_PED
;
661 State
&= ~XHC_PORTSC_RESET
;
662 XhcWriteOpReg (Xhc
, Offset
, State
);
665 case EfiUsbPortSuspend
:
666 State
|= XHC_PORTSC_LWS
;
667 XhcWriteOpReg (Xhc
, Offset
, State
);
668 State
&= ~XHC_PORTSC_PLS
;
669 XhcWriteOpReg (Xhc
, Offset
, State
);
672 case EfiUsbPortReset
:
674 // PORTSC_RESET BIT(4) bit is RW1S attribute, which means Write-1-to-set status:
675 // Register bits indicate status when read, a clear bit may be set by
676 // writing a '1'. Writing a '0' to RW1S bits has no effect.
680 case EfiUsbPortOwner
:
682 // XHCI root hub port don't has the owner bit, ignore the operation
686 case EfiUsbPortConnectChange
:
688 // Clear connect status change
690 State
|= XHC_PORTSC_CSC
;
691 XhcWriteOpReg (Xhc
, Offset
, State
);
694 case EfiUsbPortEnableChange
:
696 // Clear enable status change
698 State
|= XHC_PORTSC_PEC
;
699 XhcWriteOpReg (Xhc
, Offset
, State
);
702 case EfiUsbPortOverCurrentChange
:
704 // Clear PortOverCurrent change
706 State
|= XHC_PORTSC_OCC
;
707 XhcWriteOpReg (Xhc
, Offset
, State
);
710 case EfiUsbPortResetChange
:
712 // Clear Port Reset change
714 State
|= XHC_PORTSC_PRC
;
715 XhcWriteOpReg (Xhc
, Offset
, State
);
718 case EfiUsbPortPower
:
719 case EfiUsbPortSuspendChange
:
721 // Not supported or not related operation
726 Status
= EFI_INVALID_PARAMETER
;
731 DEBUG ((DEBUG_INFO
, "XhcClearRootHubPortFeature: status %r\n", Status
));
732 gBS
->RestoreTPL (OldTpl
);
738 Submits a new transaction to a target USB device.
740 @param Xhc The XHCI Instance.
741 @param DeviceAddress The target device address.
742 @param EndPointAddress Endpoint number and its direction encoded in bit 7
743 @param DeviceSpeed Target device speed.
744 @param MaximumPacketLength Maximum packet size the default control transfer
745 endpoint is capable of sending or receiving.
746 @param Type The transaction type.
747 @param Request USB device request to send.
748 @param Data Data buffer to be transmitted or received from USB
750 @param DataLength The size (in bytes) of the data buffer.
751 @param Timeout Indicates the maximum timeout, in millisecond.
752 @param TransferResult Return the result of this control transfer.
754 @retval EFI_SUCCESS Transfer was completed successfully.
755 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
756 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
757 @retval EFI_TIMEOUT Transfer failed due to timeout.
758 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
762 IN USB_XHCI_INSTANCE
*Xhc
,
763 IN UINT8 DeviceAddress
,
764 IN UINT8 EndPointAddress
,
765 IN UINT8 DeviceSpeed
,
766 IN UINTN MaximumPacketLength
,
768 IN EFI_USB_DEVICE_REQUEST
*Request
,
770 IN OUT UINTN
*DataLength
,
772 OUT UINT32
*TransferResult
776 EFI_STATUS RecoveryStatus
;
779 ASSERT ((Type
== XHC_CTRL_TRANSFER
) || (Type
== XHC_BULK_TRANSFER
) || (Type
== XHC_INT_TRANSFER_SYNC
));
795 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: failed to create URB!\n", Type
));
796 return EFI_OUT_OF_RESOURCES
;
799 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
801 if (Status
== EFI_TIMEOUT
) {
803 // The transfer timed out. Abort the transfer by dequeueing of the TD.
805 RecoveryStatus
= XhcDequeueTrbFromEndpoint (Xhc
, Urb
);
806 if (RecoveryStatus
== EFI_ALREADY_STARTED
) {
808 // The URB is finished just before stopping endpoint.
809 // Change returning status from EFI_TIMEOUT to EFI_SUCCESS.
811 ASSERT (Urb
->Result
== EFI_USB_NOERROR
);
812 Status
= EFI_SUCCESS
;
813 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: pending URB is finished, Length = %d.\n", Type
, Urb
->Completed
));
814 } else if (EFI_ERROR (RecoveryStatus
)) {
815 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: XhcDequeueTrbFromEndpoint failed!\n", Type
));
819 *TransferResult
= Urb
->Result
;
820 *DataLength
= Urb
->Completed
;
822 if ((*TransferResult
== EFI_USB_ERR_STALL
) || (*TransferResult
== EFI_USB_ERR_BABBLE
)) {
823 ASSERT (Status
== EFI_DEVICE_ERROR
);
824 RecoveryStatus
= XhcRecoverHaltedEndpoint (Xhc
, Urb
);
825 if (EFI_ERROR (RecoveryStatus
)) {
826 DEBUG ((DEBUG_ERROR
, "XhcTransfer[Type=%d]: XhcRecoverHaltedEndpoint failed!\n", Type
));
830 Xhc
->PciIo
->Flush (Xhc
->PciIo
);
831 XhcFreeUrb (Xhc
, Urb
);
836 Submits control transfer to a target USB device.
838 @param This This EFI_USB2_HC_PROTOCOL instance.
839 @param DeviceAddress The target device address.
840 @param DeviceSpeed Target device speed.
841 @param MaximumPacketLength Maximum packet size the default control transfer
842 endpoint is capable of sending or receiving.
843 @param Request USB device request to send.
844 @param TransferDirection Specifies the data direction for the data stage
845 @param Data Data buffer to be transmitted or received from USB
847 @param DataLength The size (in bytes) of the data buffer.
848 @param Timeout Indicates the maximum timeout, in millisecond.
849 @param Translator Transaction translator to be used by this device.
850 @param TransferResult Return the result of this control transfer.
852 @retval EFI_SUCCESS Transfer was completed successfully.
853 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
854 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
855 @retval EFI_TIMEOUT Transfer failed due to timeout.
856 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
862 IN EFI_USB2_HC_PROTOCOL
*This
,
863 IN UINT8 DeviceAddress
,
864 IN UINT8 DeviceSpeed
,
865 IN UINTN MaximumPacketLength
,
866 IN EFI_USB_DEVICE_REQUEST
*Request
,
867 IN EFI_USB_DATA_DIRECTION TransferDirection
,
869 IN OUT UINTN
*DataLength
,
871 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
872 OUT UINT32
*TransferResult
875 USB_XHCI_INSTANCE
*Xhc
;
878 UINT8 DescriptorType
;
883 EFI_USB_HUB_DESCRIPTOR
*HubDesc
;
887 EFI_USB_PORT_STATUS PortStatus
;
889 EFI_USB_DEVICE_REQUEST ClearPortRequest
;
893 // Validate parameters
895 if ((Request
== NULL
) || (TransferResult
== NULL
)) {
896 return EFI_INVALID_PARAMETER
;
899 if ((TransferDirection
!= EfiUsbDataIn
) &&
900 (TransferDirection
!= EfiUsbDataOut
) &&
901 (TransferDirection
!= EfiUsbNoData
))
903 return EFI_INVALID_PARAMETER
;
906 if ((TransferDirection
== EfiUsbNoData
) &&
907 ((Data
!= NULL
) || (*DataLength
!= 0)))
909 return EFI_INVALID_PARAMETER
;
912 if ((TransferDirection
!= EfiUsbNoData
) &&
913 ((Data
== NULL
) || (*DataLength
== 0)))
915 return EFI_INVALID_PARAMETER
;
918 if ((MaximumPacketLength
!= 8) && (MaximumPacketLength
!= 16) &&
919 (MaximumPacketLength
!= 32) && (MaximumPacketLength
!= 64) &&
920 (MaximumPacketLength
!= 512)
923 return EFI_INVALID_PARAMETER
;
926 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) {
927 return EFI_INVALID_PARAMETER
;
930 if ((DeviceSpeed
== EFI_USB_SPEED_SUPER
) && (MaximumPacketLength
!= 512)) {
931 return EFI_INVALID_PARAMETER
;
934 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
936 Xhc
= XHC_FROM_THIS (This
);
938 Status
= EFI_DEVICE_ERROR
;
939 *TransferResult
= EFI_USB_ERR_SYSTEM
;
942 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
943 DEBUG ((DEBUG_ERROR
, "XhcControlTransfer: HC halted at entrance\n"));
948 // Check if the device is still enabled before every transaction.
950 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
956 // Hook the Set_Address request from UsbBus.
957 // According to XHCI 1.0 spec, the Set_Address request is replaced by XHCI's Address_Device cmd.
959 if ((Request
->Request
== USB_REQ_SET_ADDRESS
) &&
960 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
)))
963 // Reset the BusDevAddr field of all disabled entries in UsbDevContext array firstly.
964 // This way is used to clean the history to avoid using wrong device address by XhcAsyncInterruptTransfer().
966 for (Index
= 0; Index
< 255; Index
++) {
967 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
&&
968 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0) &&
969 (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== (UINT8
)Request
->Value
))
971 Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
= 0;
975 if (Xhc
->UsbDevContext
[SlotId
].XhciDevAddr
== 0) {
976 Status
= EFI_DEVICE_ERROR
;
981 // The actual device address has been assigned by XHCI during initializing the device slot.
982 // So we just need establish the mapping relationship between the device address requested from UsbBus
983 // and the actual device address assigned by XHCI. The the following invocations through EFI_USB2_HC_PROTOCOL interface
984 // can find out the actual device address by it.
986 Xhc
->UsbDevContext
[SlotId
].BusDevAddr
= (UINT8
)Request
->Value
;
987 Status
= EFI_SUCCESS
;
992 // Create a new URB, insert it into the asynchronous
993 // schedule list, then poll the execution status.
994 // Note that we encode the direction in address although default control
995 // endpoint is bidirectional. XhcCreateUrb expects this
996 // combination of Ep addr and its direction.
998 Endpoint
= (UINT8
)(0 | ((TransferDirection
== EfiUsbDataIn
) ? 0x80 : 0));
999 Status
= XhcTransfer (
1004 MaximumPacketLength
,
1013 if (EFI_ERROR (Status
)) {
1018 // Hook Get_Descriptor request from UsbBus as we need evaluate context and configure endpoint.
1019 // Hook Get_Status request form UsbBus as we need trace device attach/detach event happened at hub.
1020 // Hook Set_Config request from UsbBus as we need configure device endpoint.
1022 if ((Request
->Request
== USB_REQ_GET_DESCRIPTOR
) &&
1023 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
)) ||
1024 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_DEVICE
)))))
1026 DescriptorType
= (UINT8
)(Request
->Value
>> 8);
1027 if ((DescriptorType
== USB_DESC_TYPE_DEVICE
) && ((*DataLength
== sizeof (EFI_USB_DEVICE_DESCRIPTOR
)) || ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (*DataLength
== 8)))) {
1028 ASSERT (Data
!= NULL
);
1030 // Store a copy of device scriptor as hub device need this info to configure endpoint.
1032 CopyMem (&Xhc
->UsbDevContext
[SlotId
].DevDesc
, Data
, *DataLength
);
1033 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.BcdUSB
>= 0x0300) {
1035 // If it's a usb3.0 device, then its max packet size is a 2^n.
1037 MaxPacket0
= 1 << Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
1039 MaxPacket0
= Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
1042 Xhc
->UsbDevContext
[SlotId
].ConfDesc
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
* sizeof (EFI_USB_CONFIG_DESCRIPTOR
*));
1043 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1044 Status
= XhcEvaluateContext (Xhc
, SlotId
, MaxPacket0
);
1046 Status
= XhcEvaluateContext64 (Xhc
, SlotId
, MaxPacket0
);
1048 } else if (DescriptorType
== USB_DESC_TYPE_CONFIG
) {
1049 ASSERT (Data
!= NULL
);
1050 if (*DataLength
== ((UINT16
*)Data
)[1]) {
1052 // Get configuration value from request, Store the configuration descriptor for Configure_Endpoint cmd.
1054 Index
= (UINT8
)Request
->Value
;
1055 ASSERT (Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
);
1056 Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
] = AllocateZeroPool (*DataLength
);
1057 CopyMem (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
], Data
, *DataLength
);
1059 // Default to use AlternateSetting 0 for all interfaces.
1061 Xhc
->UsbDevContext
[SlotId
].ActiveAlternateSetting
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->NumInterfaces
* sizeof (UINT8
));
1063 } else if (((DescriptorType
== USB_DESC_TYPE_HUB
) ||
1064 (DescriptorType
== USB_DESC_TYPE_HUB_SUPER_SPEED
)) && (*DataLength
> 2))
1066 ASSERT (Data
!= NULL
);
1067 HubDesc
= (EFI_USB_HUB_DESCRIPTOR
*)Data
;
1068 ASSERT (HubDesc
->NumPorts
<= 15);
1070 // The bit 5,6 of HubCharacter field of Hub Descriptor is TTT.
1072 TTT
= (UINT8
)((HubDesc
->HubCharacter
& (BIT5
| BIT6
)) >> 5);
1073 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.DeviceProtocol
== 2) {
1075 // Don't support multi-TT feature for super speed hub now.
1078 DEBUG ((DEBUG_ERROR
, "XHCI: Don't support multi-TT feature for Hub now. (force to disable MTT)\n"));
1083 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1084 Status
= XhcConfigHubContext (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
1086 Status
= XhcConfigHubContext64 (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
1089 } else if ((Request
->Request
== USB_REQ_SET_CONFIG
) &&
1090 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
)))
1093 // Hook Set_Config request from UsbBus as we need configure device endpoint.
1095 for (Index
= 0; Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
; Index
++) {
1096 if (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->ConfigurationValue
== (UINT8
)Request
->Value
) {
1097 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1098 Status
= XhcSetConfigCmd (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
1100 Status
= XhcSetConfigCmd64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
1106 } else if ((Request
->Request
== USB_REQ_SET_INTERFACE
) &&
1107 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_INTERFACE
)))
1110 // Hook Set_Interface request from UsbBus as we need configure interface setting.
1111 // Request->Value indicates AlterlateSetting to set
1112 // Request->Index indicates Interface to set
1114 if (Xhc
->UsbDevContext
[SlotId
].ActiveAlternateSetting
[(UINT8
)Request
->Index
] != (UINT8
)Request
->Value
) {
1115 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1116 Status
= XhcSetInterface (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Xhc
->UsbDevContext
[SlotId
].ActiveConfiguration
- 1], Request
);
1118 Status
= XhcSetInterface64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Xhc
->UsbDevContext
[SlotId
].ActiveConfiguration
- 1], Request
);
1121 } else if ((Request
->Request
== USB_REQ_GET_STATUS
) &&
1122 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
)))
1124 ASSERT (Data
!= NULL
);
1126 // Hook Get_Status request from UsbBus to keep track of the port status change.
1128 State
= *(UINT32
*)Data
;
1129 PortStatus
.PortStatus
= 0;
1130 PortStatus
.PortChangeStatus
= 0;
1132 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
1134 // For super speed hub, its bit10~12 presents the attached device speed.
1136 if ((State
& XHC_PORTSC_PS
) >> 10 == 0) {
1137 PortStatus
.PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
1141 // For high or full/low speed hub, its bit9~10 presents the attached device speed.
1143 if (XHC_BIT_IS_SET (State
, BIT9
)) {
1144 PortStatus
.PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
1145 } else if (XHC_BIT_IS_SET (State
, BIT10
)) {
1146 PortStatus
.PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
1151 // Convert the XHCI port/port change state to UEFI status
1153 MapSize
= sizeof (mUsbHubPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
1154 for (Index
= 0; Index
< MapSize
; Index
++) {
1155 if (XHC_BIT_IS_SET (State
, mUsbHubPortStateMap
[Index
].HwState
)) {
1156 PortStatus
.PortStatus
= (UINT16
)(PortStatus
.PortStatus
| mUsbHubPortStateMap
[Index
].UefiState
);
1160 MapSize
= sizeof (mUsbHubPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
1161 for (Index
= 0; Index
< MapSize
; Index
++) {
1162 if (XHC_BIT_IS_SET (State
, mUsbHubPortChangeMap
[Index
].HwState
)) {
1163 PortStatus
.PortChangeStatus
= (UINT16
)(PortStatus
.PortChangeStatus
| mUsbHubPortChangeMap
[Index
].UefiState
);
1167 MapSize
= sizeof (mUsbHubClearPortChangeMap
) / sizeof (USB_CLEAR_PORT_MAP
);
1169 for (Index
= 0; Index
< MapSize
; Index
++) {
1170 if (XHC_BIT_IS_SET (State
, mUsbHubClearPortChangeMap
[Index
].HwState
)) {
1171 ZeroMem (&ClearPortRequest
, sizeof (EFI_USB_DEVICE_REQUEST
));
1172 ClearPortRequest
.RequestType
= USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
);
1173 ClearPortRequest
.Request
= (UINT8
)USB_REQ_CLEAR_FEATURE
;
1174 ClearPortRequest
.Value
= mUsbHubClearPortChangeMap
[Index
].Selector
;
1175 ClearPortRequest
.Index
= Request
->Index
;
1176 ClearPortRequest
.Length
= 0;
1178 XhcControlTransfer (
1182 MaximumPacketLength
,
1194 XhcPollPortStatusChange (Xhc
, Xhc
->UsbDevContext
[SlotId
].RouteString
, (UINT8
)Request
->Index
, &PortStatus
);
1196 *(UINT32
*)Data
= *(UINT32
*)&PortStatus
;
1200 if (EFI_ERROR (Status
)) {
1201 DEBUG ((DEBUG_ERROR
, "XhcControlTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1204 gBS
->RestoreTPL (OldTpl
);
1210 Submits bulk transfer to a bulk endpoint of a USB device.
1212 @param This This EFI_USB2_HC_PROTOCOL instance.
1213 @param DeviceAddress Target device address.
1214 @param EndPointAddress Endpoint number and its direction in bit 7.
1215 @param DeviceSpeed Device speed, Low speed device doesn't support bulk
1217 @param MaximumPacketLength Maximum packet size the endpoint is capable of
1218 sending or receiving.
1219 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1220 @param Data Array of pointers to the buffers of data to transmit
1221 from or receive into.
1222 @param DataLength The lenght of the data buffer.
1223 @param DataToggle On input, the initial data toggle for the transfer;
1224 On output, it is updated to to next data toggle to
1225 use of the subsequent bulk transfer.
1226 @param Timeout Indicates the maximum time, in millisecond, which
1227 the transfer is allowed to complete.
1228 @param Translator A pointr to the transaction translator data.
1229 @param TransferResult A pointer to the detailed result information of the
1232 @retval EFI_SUCCESS The transfer was completed successfully.
1233 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1234 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1235 @retval EFI_TIMEOUT The transfer failed due to timeout.
1236 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1242 IN EFI_USB2_HC_PROTOCOL
*This
,
1243 IN UINT8 DeviceAddress
,
1244 IN UINT8 EndPointAddress
,
1245 IN UINT8 DeviceSpeed
,
1246 IN UINTN MaximumPacketLength
,
1247 IN UINT8 DataBuffersNumber
,
1248 IN OUT VOID
*Data
[EFI_USB_MAX_BULK_BUFFER_NUM
],
1249 IN OUT UINTN
*DataLength
,
1250 IN OUT UINT8
*DataToggle
,
1252 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1253 OUT UINT32
*TransferResult
1256 USB_XHCI_INSTANCE
*Xhc
;
1260 UINTN DebugErrorLevel
;
1263 // Validate the parameters
1265 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1266 (Data
== NULL
) || (Data
[0] == NULL
) || (TransferResult
== NULL
))
1268 return EFI_INVALID_PARAMETER
;
1271 if ((*DataToggle
!= 0) && (*DataToggle
!= 1)) {
1272 return EFI_INVALID_PARAMETER
;
1275 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) ||
1276 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1277 ((EFI_USB_SPEED_HIGH
== DeviceSpeed
) && (MaximumPacketLength
> 512)) ||
1278 ((EFI_USB_SPEED_SUPER
== DeviceSpeed
) && (MaximumPacketLength
> 1024)))
1280 return EFI_INVALID_PARAMETER
;
1283 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1285 Xhc
= XHC_FROM_THIS (This
);
1287 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1288 Status
= EFI_DEVICE_ERROR
;
1290 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1291 DEBUG ((DEBUG_ERROR
, "XhcBulkTransfer: HC is halted\n"));
1296 // Check if the device is still enabled before every transaction.
1298 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1304 // Create a new URB, insert it into the asynchronous
1305 // schedule list, then poll the execution status.
1307 Status
= XhcTransfer (
1312 MaximumPacketLength
,
1322 if (EFI_ERROR (Status
)) {
1323 if (Status
== EFI_TIMEOUT
) {
1324 DebugErrorLevel
= DEBUG_VERBOSE
;
1326 DebugErrorLevel
= DEBUG_ERROR
;
1329 DEBUG ((DebugErrorLevel
, "XhcBulkTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1332 gBS
->RestoreTPL (OldTpl
);
1338 Submits an asynchronous interrupt transfer to an
1339 interrupt endpoint of a USB device.
1341 @param This This EFI_USB2_HC_PROTOCOL instance.
1342 @param DeviceAddress Target device address.
1343 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1344 @param DeviceSpeed Indicates device speed.
1345 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1346 @param IsNewTransfer If TRUE, to submit an new asynchronous interrupt
1347 transfer If FALSE, to remove the specified
1348 asynchronous interrupt.
1349 @param DataToggle On input, the initial data toggle to use; on output,
1350 it is updated to indicate the next data toggle.
1351 @param PollingInterval The he interval, in milliseconds, that the transfer
1353 @param DataLength The length of data to receive at the rate specified
1355 @param Translator Transaction translator to use.
1356 @param CallBackFunction Function to call at the rate specified by
1358 @param Context Context to CallBackFunction.
1360 @retval EFI_SUCCESS The request has been successfully submitted or canceled.
1361 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1362 @retval EFI_OUT_OF_RESOURCES The request failed due to a lack of resources.
1363 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1368 XhcAsyncInterruptTransfer (
1369 IN EFI_USB2_HC_PROTOCOL
*This
,
1370 IN UINT8 DeviceAddress
,
1371 IN UINT8 EndPointAddress
,
1372 IN UINT8 DeviceSpeed
,
1373 IN UINTN MaximumPacketLength
,
1374 IN BOOLEAN IsNewTransfer
,
1375 IN OUT UINT8
*DataToggle
,
1376 IN UINTN PollingInterval
,
1377 IN UINTN DataLength
,
1378 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1379 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction
,
1380 IN VOID
*Context OPTIONAL
1383 USB_XHCI_INSTANCE
*Xhc
;
1391 // Validate parameters
1393 if (!XHCI_IS_DATAIN (EndPointAddress
)) {
1394 return EFI_INVALID_PARAMETER
;
1397 if (IsNewTransfer
) {
1398 if (DataLength
== 0) {
1399 return EFI_INVALID_PARAMETER
;
1402 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1403 return EFI_INVALID_PARAMETER
;
1406 if ((PollingInterval
> 255) || (PollingInterval
< 1)) {
1407 return EFI_INVALID_PARAMETER
;
1411 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1413 Xhc
= XHC_FROM_THIS (This
);
1416 // Delete Async interrupt transfer request.
1418 if (!IsNewTransfer
) {
1420 // The delete request may happen after device is detached.
1422 for (Index
= 0; Index
< 255; Index
++) {
1423 if (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== DeviceAddress
) {
1429 Status
= EFI_INVALID_PARAMETER
;
1433 Status
= XhciDelAsyncIntTransfer (Xhc
, DeviceAddress
, EndPointAddress
);
1434 DEBUG ((DEBUG_INFO
, "XhcAsyncInterruptTransfer: remove old transfer for addr %d, Status = %r\n", DeviceAddress
, Status
));
1438 Status
= EFI_SUCCESS
;
1440 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1441 DEBUG ((DEBUG_ERROR
, "XhcAsyncInterruptTransfer: HC is halt\n"));
1442 Status
= EFI_DEVICE_ERROR
;
1447 // Check if the device is still enabled before every transaction.
1449 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1454 Urb
= XhciInsertAsyncIntTransfer (
1459 MaximumPacketLength
,
1465 Status
= EFI_OUT_OF_RESOURCES
;
1470 // Ring the doorbell
1472 Status
= RingIntTransferDoorBell (Xhc
, Urb
);
1475 Xhc
->PciIo
->Flush (Xhc
->PciIo
);
1476 gBS
->RestoreTPL (OldTpl
);
1482 Submits synchronous interrupt transfer to an interrupt endpoint
1485 @param This This EFI_USB2_HC_PROTOCOL instance.
1486 @param DeviceAddress Target device address.
1487 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1488 @param DeviceSpeed Indicates device speed.
1489 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1490 of sending or receiving.
1491 @param Data Buffer of data that will be transmitted to USB
1492 device or received from USB device.
1493 @param DataLength On input, the size, in bytes, of the data buffer; On
1494 output, the number of bytes transferred.
1495 @param DataToggle On input, the initial data toggle to use; on output,
1496 it is updated to indicate the next data toggle.
1497 @param Timeout Maximum time, in second, to complete.
1498 @param Translator Transaction translator to use.
1499 @param TransferResult Variable to receive the transfer result.
1501 @return EFI_SUCCESS The transfer was completed successfully.
1502 @return EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1503 @return EFI_INVALID_PARAMETER Some parameters are invalid.
1504 @return EFI_TIMEOUT The transfer failed due to timeout.
1505 @return EFI_DEVICE_ERROR The failed due to host controller or device error
1510 XhcSyncInterruptTransfer (
1511 IN EFI_USB2_HC_PROTOCOL
*This
,
1512 IN UINT8 DeviceAddress
,
1513 IN UINT8 EndPointAddress
,
1514 IN UINT8 DeviceSpeed
,
1515 IN UINTN MaximumPacketLength
,
1517 IN OUT UINTN
*DataLength
,
1518 IN OUT UINT8
*DataToggle
,
1520 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1521 OUT UINT32
*TransferResult
1524 USB_XHCI_INSTANCE
*Xhc
;
1530 // Validates parameters
1532 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1533 (Data
== NULL
) || (TransferResult
== NULL
))
1535 return EFI_INVALID_PARAMETER
;
1538 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1539 return EFI_INVALID_PARAMETER
;
1542 if (((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) ||
1543 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1544 ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) && (MaximumPacketLength
> 3072)))
1546 return EFI_INVALID_PARAMETER
;
1549 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1551 Xhc
= XHC_FROM_THIS (This
);
1553 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1554 Status
= EFI_DEVICE_ERROR
;
1556 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1557 DEBUG ((DEBUG_ERROR
, "EhcSyncInterruptTransfer: HC is halt\n"));
1562 // Check if the device is still enabled before every transaction.
1564 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1569 Status
= XhcTransfer (
1574 MaximumPacketLength
,
1575 XHC_INT_TRANSFER_SYNC
,
1584 if (EFI_ERROR (Status
)) {
1585 DEBUG ((DEBUG_ERROR
, "XhcSyncInterruptTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1588 gBS
->RestoreTPL (OldTpl
);
1594 Submits isochronous transfer to a target USB device.
1596 @param This This EFI_USB2_HC_PROTOCOL instance.
1597 @param DeviceAddress Target device address.
1598 @param EndPointAddress End point address with its direction.
1599 @param DeviceSpeed Device speed, Low speed device doesn't support this
1601 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1602 sending or receiving.
1603 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1604 @param Data Array of pointers to the buffers of data that will
1605 be transmitted to USB device or received from USB
1607 @param DataLength The size, in bytes, of the data buffer.
1608 @param Translator Transaction translator to use.
1609 @param TransferResult Variable to receive the transfer result.
1611 @return EFI_UNSUPPORTED Isochronous transfer is unsupported.
1616 XhcIsochronousTransfer (
1617 IN EFI_USB2_HC_PROTOCOL
*This
,
1618 IN UINT8 DeviceAddress
,
1619 IN UINT8 EndPointAddress
,
1620 IN UINT8 DeviceSpeed
,
1621 IN UINTN MaximumPacketLength
,
1622 IN UINT8 DataBuffersNumber
,
1623 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1624 IN UINTN DataLength
,
1625 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1626 OUT UINT32
*TransferResult
1629 return EFI_UNSUPPORTED
;
1633 Submits Async isochronous transfer to a target USB device.
1635 @param This This EFI_USB2_HC_PROTOCOL instance.
1636 @param DeviceAddress Target device address.
1637 @param EndPointAddress End point address with its direction.
1638 @param DeviceSpeed Device speed, Low speed device doesn't support this
1640 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1641 sending or receiving.
1642 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1643 @param Data Array of pointers to the buffers of data that will
1644 be transmitted to USB device or received from USB
1646 @param DataLength The size, in bytes, of the data buffer.
1647 @param Translator Transaction translator to use.
1648 @param IsochronousCallBack Function to be called when the transfer complete.
1649 @param Context Context passed to the call back function as
1652 @return EFI_UNSUPPORTED Isochronous transfer isn't supported.
1657 XhcAsyncIsochronousTransfer (
1658 IN EFI_USB2_HC_PROTOCOL
*This
,
1659 IN UINT8 DeviceAddress
,
1660 IN UINT8 EndPointAddress
,
1661 IN UINT8 DeviceSpeed
,
1662 IN UINTN MaximumPacketLength
,
1663 IN UINT8 DataBuffersNumber
,
1664 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1665 IN UINTN DataLength
,
1666 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1667 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
1671 return EFI_UNSUPPORTED
;
1675 Entry point for EFI drivers.
1677 @param ImageHandle EFI_HANDLE.
1678 @param SystemTable EFI_SYSTEM_TABLE.
1680 @retval EFI_SUCCESS Success.
1681 @retval Others Fail.
1686 XhcDriverEntryPoint (
1687 IN EFI_HANDLE ImageHandle
,
1688 IN EFI_SYSTEM_TABLE
*SystemTable
1691 return EfiLibInstallDriverBindingComponentName2 (
1694 &gXhciDriverBinding
,
1696 &gXhciComponentName
,
1697 &gXhciComponentName2
1702 Test to see if this driver supports ControllerHandle. Any
1703 ControllerHandle that has Usb2HcProtocol installed will
1706 @param This Protocol instance pointer.
1707 @param Controller Handle of device to test.
1708 @param RemainingDevicePath Not used.
1710 @return EFI_SUCCESS This driver supports this device.
1711 @return EFI_UNSUPPORTED This driver does not support this device.
1716 XhcDriverBindingSupported (
1717 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1718 IN EFI_HANDLE Controller
,
1719 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1723 EFI_PCI_IO_PROTOCOL
*PciIo
;
1724 USB_CLASSC UsbClassCReg
;
1727 // Test whether there is PCI IO Protocol attached on the controller handle.
1729 Status
= gBS
->OpenProtocol (
1731 &gEfiPciIoProtocolGuid
,
1733 This
->DriverBindingHandle
,
1735 EFI_OPEN_PROTOCOL_BY_DRIVER
1738 if (EFI_ERROR (Status
)) {
1739 return EFI_UNSUPPORTED
;
1742 Status
= PciIo
->Pci
.Read (
1745 PCI_CLASSCODE_OFFSET
,
1746 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1750 if (EFI_ERROR (Status
)) {
1751 Status
= EFI_UNSUPPORTED
;
1756 // Test whether the controller belongs to Xhci type
1758 if ((UsbClassCReg
.BaseCode
!= PCI_CLASS_SERIAL
) ||
1759 (UsbClassCReg
.SubClassCode
!= PCI_CLASS_SERIAL_USB
) ||
1760 (UsbClassCReg
.ProgInterface
!= PCI_IF_XHCI
))
1762 Status
= EFI_UNSUPPORTED
;
1766 gBS
->CloseProtocol (
1768 &gEfiPciIoProtocolGuid
,
1769 This
->DriverBindingHandle
,
1777 Create and initialize a USB_XHCI_INSTANCE structure.
1779 @param PciIo The PciIo on this device.
1780 @param DevicePath The device path of host controller.
1781 @param OriginalPciAttributes Original PCI attributes.
1783 @return The allocated and initialized USB_XHCI_INSTANCE structure if created,
1789 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1790 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1791 IN UINT64 OriginalPciAttributes
1794 USB_XHCI_INSTANCE
*Xhc
;
1798 UINT8 ReleaseNumber
;
1800 Xhc
= AllocateZeroPool (sizeof (USB_XHCI_INSTANCE
));
1807 // Initialize private data structure
1809 Xhc
->Signature
= XHCI_INSTANCE_SIG
;
1811 Xhc
->DevicePath
= DevicePath
;
1812 Xhc
->OriginalPciAttributes
= OriginalPciAttributes
;
1813 CopyMem (&Xhc
->Usb2Hc
, &gXhciUsb2HcTemplate
, sizeof (EFI_USB2_HC_PROTOCOL
));
1815 Status
= PciIo
->Pci
.Read (
1818 XHC_PCI_SBRN_OFFSET
,
1823 if (!EFI_ERROR (Status
)) {
1824 Xhc
->Usb2Hc
.MajorRevision
= (ReleaseNumber
& 0xF0) >> 4;
1825 Xhc
->Usb2Hc
.MinorRevision
= (ReleaseNumber
& 0x0F);
1828 InitializeListHead (&Xhc
->AsyncIntTransfers
);
1831 // Be caution that the Offset passed to XhcReadCapReg() should be Dword align
1833 Xhc
->CapLength
= XhcReadCapReg8 (Xhc
, XHC_CAPLENGTH_OFFSET
);
1834 Xhc
->HcSParams1
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS1_OFFSET
);
1835 Xhc
->HcSParams2
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS2_OFFSET
);
1836 Xhc
->HcCParams
.Dword
= XhcReadCapReg (Xhc
, XHC_HCCPARAMS_OFFSET
);
1837 Xhc
->DBOff
= XhcReadCapReg (Xhc
, XHC_DBOFF_OFFSET
);
1838 Xhc
->RTSOff
= XhcReadCapReg (Xhc
, XHC_RTSOFF_OFFSET
);
1841 // This PageSize field defines the page size supported by the xHC implementation.
1842 // This xHC supports a page size of 2^(n+12) if bit n is Set. For example,
1843 // if bit 0 is Set, the xHC supports 4k byte page sizes.
1845 PageSize
= XhcReadOpReg (Xhc
, XHC_PAGESIZE_OFFSET
);
1846 if ((PageSize
& (~XHC_PAGESIZE_MASK
)) != 0) {
1847 DEBUG ((DEBUG_ERROR
, "XhcCreateUsb3Hc: Reserved bits are not 0 for PageSize\n"));
1851 PageSize
&= XHC_PAGESIZE_MASK
;
1852 Xhc
->PageSize
= 1 << (HighBitSet32 (PageSize
) + 12);
1854 ExtCapReg
= (UINT16
)(Xhc
->HcCParams
.Data
.ExtCapReg
);
1855 Xhc
->ExtCapRegBase
= ExtCapReg
<< 2;
1856 Xhc
->UsbLegSupOffset
= XhcGetCapabilityAddr (Xhc
, XHC_CAP_USB_LEGACY
);
1857 Xhc
->DebugCapSupOffset
= XhcGetCapabilityAddr (Xhc
, XHC_CAP_USB_DEBUG
);
1858 Xhc
->Usb2SupOffset
= XhcGetSupportedProtocolCapabilityAddr (Xhc
, XHC_SUPPORTED_PROTOCOL_DW0_MAJOR_REVISION_USB2
);
1859 Xhc
->Usb3SupOffset
= XhcGetSupportedProtocolCapabilityAddr (Xhc
, XHC_SUPPORTED_PROTOCOL_DW0_MAJOR_REVISION_USB3
);
1861 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: Capability length 0x%x\n", Xhc
->CapLength
));
1862 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: HcSParams1 0x%x\n", Xhc
->HcSParams1
));
1863 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: HcSParams2 0x%x\n", Xhc
->HcSParams2
));
1864 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: HcCParams 0x%x\n", Xhc
->HcCParams
));
1865 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: DBOff 0x%x\n", Xhc
->DBOff
));
1866 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: RTSOff 0x%x\n", Xhc
->RTSOff
));
1867 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: UsbLegSupOffset 0x%x\n", Xhc
->UsbLegSupOffset
));
1868 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: DebugCapSupOffset 0x%x\n", Xhc
->DebugCapSupOffset
));
1869 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: Usb2SupOffset 0x%x\n", Xhc
->Usb2SupOffset
));
1870 DEBUG ((DEBUG_INFO
, "XhcCreateUsb3Hc: Usb3SupOffset 0x%x\n", Xhc
->Usb3SupOffset
));
1873 // Create AsyncRequest Polling Timer
1875 Status
= gBS
->CreateEvent (
1876 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1878 XhcMonitorAsyncRequests
,
1883 if (EFI_ERROR (Status
)) {
1895 One notified function to stop the Host Controller when gBS->ExitBootServices() called.
1897 @param Event Pointer to this event
1898 @param Context Event handler private data
1903 XhcExitBootService (
1909 USB_XHCI_INSTANCE
*Xhc
;
1910 EFI_PCI_IO_PROTOCOL
*PciIo
;
1912 Xhc
= (USB_XHCI_INSTANCE
*)Context
;
1916 // Stop AsyncRequest Polling timer then stop the XHCI driver
1917 // and uninstall the XHCI protocl.
1919 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
1920 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
1922 if (Xhc
->PollTimer
!= NULL
) {
1923 gBS
->CloseEvent (Xhc
->PollTimer
);
1926 XhcClearBiosOwnership (Xhc
);
1929 // Restore original PCI attributes
1933 EfiPciIoAttributeOperationSet
,
1934 Xhc
->OriginalPciAttributes
,
1940 Starting the Usb XHCI Driver.
1942 @param This Protocol instance pointer.
1943 @param Controller Handle of device to test.
1944 @param RemainingDevicePath Not used.
1946 @return EFI_SUCCESS supports this device.
1947 @return EFI_UNSUPPORTED do not support this device.
1948 @return EFI_DEVICE_ERROR cannot be started due to device Error.
1949 @return EFI_OUT_OF_RESOURCES cannot allocate resources.
1954 XhcDriverBindingStart (
1955 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1956 IN EFI_HANDLE Controller
,
1957 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1961 EFI_PCI_IO_PROTOCOL
*PciIo
;
1963 UINT64 OriginalPciAttributes
;
1964 BOOLEAN PciAttributesSaved
;
1965 USB_XHCI_INSTANCE
*Xhc
;
1966 EFI_DEVICE_PATH_PROTOCOL
*HcDevicePath
;
1969 // Open the PciIo Protocol, then enable the USB host controller
1971 Status
= gBS
->OpenProtocol (
1973 &gEfiPciIoProtocolGuid
,
1975 This
->DriverBindingHandle
,
1977 EFI_OPEN_PROTOCOL_BY_DRIVER
1980 if (EFI_ERROR (Status
)) {
1985 // Open Device Path Protocol for on USB host controller
1987 HcDevicePath
= NULL
;
1988 Status
= gBS
->OpenProtocol (
1990 &gEfiDevicePathProtocolGuid
,
1991 (VOID
**)&HcDevicePath
,
1992 This
->DriverBindingHandle
,
1994 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1997 PciAttributesSaved
= FALSE
;
1999 // Save original PCI attributes
2001 Status
= PciIo
->Attributes (
2003 EfiPciIoAttributeOperationGet
,
2005 &OriginalPciAttributes
2008 if (EFI_ERROR (Status
)) {
2012 PciAttributesSaved
= TRUE
;
2014 Status
= PciIo
->Attributes (
2016 EfiPciIoAttributeOperationSupported
,
2020 if (!EFI_ERROR (Status
)) {
2021 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
2022 Status
= PciIo
->Attributes (
2024 EfiPciIoAttributeOperationEnable
,
2030 if (EFI_ERROR (Status
)) {
2031 DEBUG ((DEBUG_ERROR
, "XhcDriverBindingStart: failed to enable controller\n"));
2036 // Create then install USB2_HC_PROTOCOL
2038 Xhc
= XhcCreateUsbHc (PciIo
, HcDevicePath
, OriginalPciAttributes
);
2041 DEBUG ((DEBUG_ERROR
, "XhcDriverBindingStart: failed to create USB2_HC\n"));
2042 return EFI_OUT_OF_RESOURCES
;
2046 // Enable 64-bit DMA support in the PCI layer if this controller
2049 if (Xhc
->HcCParams
.Data
.Ac64
!= 0) {
2050 Status
= PciIo
->Attributes (
2052 EfiPciIoAttributeOperationEnable
,
2053 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
2056 if (!EFI_ERROR (Status
)) {
2057 Xhc
->Support64BitDma
= TRUE
;
2061 "%a: failed to enable 64-bit DMA on 64-bit capable controller @ %p (%r)\n",
2069 XhcSetBiosOwnership (Xhc
);
2071 XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
2072 ASSERT (XhcIsHalt (Xhc
));
2075 // After Chip Hardware Reset wait until the Controller Not Ready (CNR) flag
2076 // in the USBSTS is '0' before writing any xHC Operational or Runtime registers.
2078 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
2081 // Initialize the schedule
2086 // Start the Host Controller
2088 XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2091 // Start the asynchronous interrupt monitor
2093 Status
= gBS
->SetTimer (Xhc
->PollTimer
, TimerPeriodic
, XHC_ASYNC_TIMER_INTERVAL
);
2094 if (EFI_ERROR (Status
)) {
2095 DEBUG ((DEBUG_ERROR
, "XhcDriverBindingStart: failed to start async interrupt monitor\n"));
2096 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2101 // Create event to stop the HC when exit boot service.
2103 Status
= gBS
->CreateEventEx (
2108 &gEfiEventExitBootServicesGuid
,
2109 &Xhc
->ExitBootServiceEvent
2111 if (EFI_ERROR (Status
)) {
2116 // Install the component name protocol, don't fail the start
2117 // because of something for display.
2121 gXhciComponentName
.SupportedLanguages
,
2122 &Xhc
->ControllerNameTable
,
2123 L
"eXtensible Host Controller (USB 3.0)",
2128 gXhciComponentName2
.SupportedLanguages
,
2129 &Xhc
->ControllerNameTable
,
2130 L
"eXtensible Host Controller (USB 3.0)",
2134 Status
= gBS
->InstallProtocolInterface (
2136 &gEfiUsb2HcProtocolGuid
,
2137 EFI_NATIVE_INTERFACE
,
2140 if (EFI_ERROR (Status
)) {
2141 DEBUG ((DEBUG_ERROR
, "XhcDriverBindingStart: failed to install USB2_HC Protocol\n"));
2145 DEBUG ((DEBUG_INFO
, "XhcDriverBindingStart: XHCI started for controller @ %x\n", Controller
));
2149 gBS
->CloseEvent (Xhc
->PollTimer
);
2154 if (PciAttributesSaved
) {
2156 // Restore original PCI attributes
2160 EfiPciIoAttributeOperationSet
,
2161 OriginalPciAttributes
,
2166 gBS
->CloseProtocol (
2168 &gEfiPciIoProtocolGuid
,
2169 This
->DriverBindingHandle
,
2177 Stop this driver on ControllerHandle. Support stopping any child handles
2178 created by this driver.
2180 @param This Protocol instance pointer.
2181 @param Controller Handle of device to stop driver on.
2182 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
2183 @param ChildHandleBuffer List of handles for the children we need to stop.
2185 @return EFI_SUCCESS Success.
2186 @return EFI_DEVICE_ERROR Fail.
2191 XhcDriverBindingStop (
2192 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
2193 IN EFI_HANDLE Controller
,
2194 IN UINTN NumberOfChildren
,
2195 IN EFI_HANDLE
*ChildHandleBuffer
2199 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
2200 EFI_PCI_IO_PROTOCOL
*PciIo
;
2201 USB_XHCI_INSTANCE
*Xhc
;
2205 // Test whether the Controller handler passed in is a valid
2206 // Usb controller handle that should be supported, if not,
2207 // return the error status directly
2209 Status
= gBS
->OpenProtocol (
2211 &gEfiUsb2HcProtocolGuid
,
2213 This
->DriverBindingHandle
,
2215 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2218 if (EFI_ERROR (Status
)) {
2222 Status
= gBS
->UninstallProtocolInterface (
2224 &gEfiUsb2HcProtocolGuid
,
2228 if (EFI_ERROR (Status
)) {
2232 Xhc
= XHC_FROM_THIS (Usb2Hc
);
2236 // Stop AsyncRequest Polling timer then stop the XHCI driver
2237 // and uninstall the XHCI protocl.
2239 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
2242 // Disable the device slots occupied by these devices on its downstream ports.
2243 // Entry 0 is reserved.
2245 for (Index
= 0; Index
< 255; Index
++) {
2246 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
||
2247 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0))
2252 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
2253 XhcDisableSlotCmd (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2255 XhcDisableSlotCmd64 (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2259 if (Xhc
->PollTimer
!= NULL
) {
2260 gBS
->CloseEvent (Xhc
->PollTimer
);
2263 if (Xhc
->ExitBootServiceEvent
!= NULL
) {
2264 gBS
->CloseEvent (Xhc
->ExitBootServiceEvent
);
2267 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2268 XhcClearBiosOwnership (Xhc
);
2269 XhciDelAllAsyncIntTransfers (Xhc
);
2272 if (Xhc
->ControllerNameTable
) {
2273 FreeUnicodeStringTable (Xhc
->ControllerNameTable
);
2277 // Restore original PCI attributes
2281 EfiPciIoAttributeOperationSet
,
2282 Xhc
->OriginalPciAttributes
,
2286 gBS
->CloseProtocol (
2288 &gEfiPciIoProtocolGuid
,
2289 This
->DriverBindingHandle
,