2 The XHCI controller driver.
4 Copyright (c) 2011, 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 EFI_DRIVER_BINDING_PROTOCOL gXhciDriverBinding
= {
36 XhcDriverBindingSupported
,
37 XhcDriverBindingStart
,
45 // Template for Xhci's Usb2 Host Controller Protocol Instance.
47 EFI_USB2_HC_PROTOCOL gXhciUsb2HcTemplate
= {
54 XhcAsyncInterruptTransfer
,
55 XhcSyncInterruptTransfer
,
56 XhcIsochronousTransfer
,
57 XhcAsyncIsochronousTransfer
,
58 XhcGetRootHubPortStatus
,
59 XhcSetRootHubPortFeature
,
60 XhcClearRootHubPortFeature
,
66 Retrieves the capability of root hub ports.
68 @param This The EFI_USB2_HC_PROTOCOL instance.
69 @param MaxSpeed Max speed supported by the controller.
70 @param PortNumber Number of the root hub ports.
71 @param Is64BitCapable Whether the controller supports 64-bit memory
74 @retval EFI_SUCCESS Host controller capability were retrieved successfully.
75 @retval EFI_INVALID_PARAMETER Either of the three capability pointer is NULL.
81 IN EFI_USB2_HC_PROTOCOL
*This
,
83 OUT UINT8
*PortNumber
,
84 OUT UINT8
*Is64BitCapable
87 USB_XHCI_INSTANCE
*Xhc
;
90 if ((MaxSpeed
== NULL
) || (PortNumber
== NULL
) || (Is64BitCapable
== NULL
)) {
91 return EFI_INVALID_PARAMETER
;
94 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
96 Xhc
= XHC_FROM_THIS (This
);
97 *MaxSpeed
= EFI_USB_SPEED_SUPER
;
98 *PortNumber
= (UINT8
) (Xhc
->HcSParams1
.Data
.MaxPorts
);
99 *Is64BitCapable
= (UINT8
) (Xhc
->HcCParams
.Data
.Ac64
);
100 DEBUG ((EFI_D_INFO
, "XhcGetCapability: %d ports, 64 bit %d\n", *PortNumber
, *Is64BitCapable
));
102 gBS
->RestoreTPL (OldTpl
);
109 Provides software reset for the USB host controller.
111 @param This This EFI_USB2_HC_PROTOCOL instance.
112 @param Attributes A bit mask of the reset operation to perform.
114 @retval EFI_SUCCESS The reset operation succeeded.
115 @retval EFI_INVALID_PARAMETER Attributes is not valid.
116 @retval EFI_UNSUPPOURTED The type of reset specified by Attributes is
117 not currently supported by the host controller.
118 @retval EFI_DEVICE_ERROR Host controller isn't halted to reset.
124 IN EFI_USB2_HC_PROTOCOL
*This
,
128 USB_XHCI_INSTANCE
*Xhc
;
132 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
134 Xhc
= XHC_FROM_THIS (This
);
136 switch (Attributes
) {
137 case EFI_USB_HC_RESET_GLOBAL
:
139 // Flow through, same behavior as Host Controller Reset
141 case EFI_USB_HC_RESET_HOST_CONTROLLER
:
143 // Host Controller must be Halt when Reset it
145 if (!XhcIsHalt (Xhc
)) {
146 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
148 if (EFI_ERROR (Status
)) {
149 Status
= EFI_DEVICE_ERROR
;
154 Status
= XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
155 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
157 if (EFI_ERROR (Status
)) {
161 // Clean up the asynchronous transfers, currently only
162 // interrupt supports asynchronous operation.
164 XhciDelAllAsyncIntTransfers (Xhc
);
170 case EFI_USB_HC_RESET_GLOBAL_WITH_DEBUG
:
171 case EFI_USB_HC_RESET_HOST_WITH_DEBUG
:
172 Status
= EFI_UNSUPPORTED
;
176 Status
= EFI_INVALID_PARAMETER
;
180 DEBUG ((EFI_D_INFO
, "XhcReset: status %r\n", Status
));
181 gBS
->RestoreTPL (OldTpl
);
188 Retrieve the current state of the USB host controller.
190 @param This This EFI_USB2_HC_PROTOCOL instance.
191 @param State Variable to return the current host controller
194 @retval EFI_SUCCESS Host controller state was returned in State.
195 @retval EFI_INVALID_PARAMETER State is NULL.
196 @retval EFI_DEVICE_ERROR An error was encountered while attempting to
197 retrieve the host controller's current state.
203 IN EFI_USB2_HC_PROTOCOL
*This
,
204 OUT EFI_USB_HC_STATE
*State
207 USB_XHCI_INSTANCE
*Xhc
;
211 return EFI_INVALID_PARAMETER
;
214 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
216 Xhc
= XHC_FROM_THIS (This
);
218 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
219 *State
= EfiUsbHcStateHalt
;
221 *State
= EfiUsbHcStateOperational
;
224 DEBUG ((EFI_D_INFO
, "XhcGetState: current state %d\n", *State
));
225 gBS
->RestoreTPL (OldTpl
);
231 Sets the USB host controller to a specific state.
233 @param This This EFI_USB2_HC_PROTOCOL instance.
234 @param State The state of the host controller that will be set.
236 @retval EFI_SUCCESS The USB host controller was successfully placed
237 in the state specified by State.
238 @retval EFI_INVALID_PARAMETER State is invalid.
239 @retval EFI_DEVICE_ERROR Failed to set the state due to device error.
245 IN EFI_USB2_HC_PROTOCOL
*This
,
246 IN EFI_USB_HC_STATE State
249 USB_XHCI_INSTANCE
*Xhc
;
251 EFI_USB_HC_STATE CurState
;
254 Status
= XhcGetState (This
, &CurState
);
256 if (EFI_ERROR (Status
)) {
257 return EFI_DEVICE_ERROR
;
260 if (CurState
== State
) {
264 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
266 Xhc
= XHC_FROM_THIS (This
);
269 case EfiUsbHcStateHalt
:
270 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
273 case EfiUsbHcStateOperational
:
274 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HSE
)) {
275 Status
= EFI_DEVICE_ERROR
;
280 // Software must not write a one to this field unless the host controller
281 // is in the Halted state. Doing so will yield undefined results.
282 // refers to Spec[XHCI1.0-2.3.1]
284 if (!XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
285 Status
= EFI_DEVICE_ERROR
;
289 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
292 case EfiUsbHcStateSuspend
:
293 Status
= EFI_UNSUPPORTED
;
297 Status
= EFI_INVALID_PARAMETER
;
300 DEBUG ((EFI_D_INFO
, "XhcSetState: status %r\n", Status
));
301 gBS
->RestoreTPL (OldTpl
);
307 Retrieves the current status of a USB root hub port.
309 @param This This EFI_USB2_HC_PROTOCOL instance.
310 @param PortNumber The root hub port to retrieve the state from.
311 This value is zero-based.
312 @param PortStatus Variable to receive the port state.
314 @retval EFI_SUCCESS The status of the USB root hub port specified.
315 by PortNumber was returned in PortStatus.
316 @retval EFI_INVALID_PARAMETER PortNumber is invalid.
317 @retval EFI_DEVICE_ERROR Can't read register.
322 XhcGetRootHubPortStatus (
323 IN EFI_USB2_HC_PROTOCOL
*This
,
325 OUT EFI_USB_PORT_STATUS
*PortStatus
328 USB_XHCI_INSTANCE
*Xhc
;
335 USB_DEV_ROUTE ParentRouteChart
;
338 if (PortStatus
== NULL
) {
339 return EFI_INVALID_PARAMETER
;
342 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
344 Xhc
= XHC_FROM_THIS (This
);
345 Status
= EFI_SUCCESS
;
347 TotalPort
= Xhc
->HcSParams1
.Data
.MaxPorts
;
349 if (PortNumber
>= TotalPort
) {
350 Status
= EFI_INVALID_PARAMETER
;
354 Offset
= (UINT32
) (XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
355 PortStatus
->PortStatus
= 0;
356 PortStatus
->PortChangeStatus
= 0;
358 State
= XhcReadOpReg (Xhc
, Offset
);
361 // According to XHCI 1.0 spec, bit 10~13 of the root port status register identifies the speed of the attached device.
363 switch ((State
& XHC_PORTSC_PS
) >> 10) {
365 PortStatus
->PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
369 PortStatus
->PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
373 PortStatus
->PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
381 // Convert the XHCI port/port change state to UEFI status
383 MapSize
= sizeof (mUsbPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
385 for (Index
= 0; Index
< MapSize
; Index
++) {
386 if (XHC_BIT_IS_SET (State
, mUsbPortStateMap
[Index
].HwState
)) {
387 PortStatus
->PortStatus
= (UINT16
) (PortStatus
->PortStatus
| mUsbPortStateMap
[Index
].UefiState
);
391 // Bit5~8 reflects its current link state.
393 if ((State
& XHC_PORTSC_PLS
) >> 5 == 3) {
394 PortStatus
->PortStatus
|= USB_PORT_STAT_SUSPEND
;
397 MapSize
= sizeof (mUsbPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
399 for (Index
= 0; Index
< MapSize
; Index
++) {
400 if (XHC_BIT_IS_SET (State
, mUsbPortChangeMap
[Index
].HwState
)) {
401 PortStatus
->PortChangeStatus
= (UINT16
) (PortStatus
->PortChangeStatus
| mUsbPortChangeMap
[Index
].UefiState
);
406 // Poll the root port status register to enable/disable corresponding device slot if there is a device attached/detached.
407 // For those devices behind hub, we get its attach/detach event by hooking Get_Port_Status request at control transfer for those hub.
409 ParentRouteChart
.Dword
= 0;
410 XhcPollPortStatusChange (Xhc
, ParentRouteChart
, PortNumber
, PortStatus
);
413 gBS
->RestoreTPL (OldTpl
);
419 Sets a feature for the specified root hub port.
421 @param This This EFI_USB2_HC_PROTOCOL instance.
422 @param PortNumber Root hub port to set.
423 @param PortFeature Feature to set.
425 @retval EFI_SUCCESS The feature specified by PortFeature was set.
426 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
427 @retval EFI_DEVICE_ERROR Can't read register.
432 XhcSetRootHubPortFeature (
433 IN EFI_USB2_HC_PROTOCOL
*This
,
435 IN EFI_USB_PORT_FEATURE PortFeature
438 USB_XHCI_INSTANCE
*Xhc
;
443 USB_DEV_ROUTE RouteChart
;
447 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
449 Xhc
= XHC_FROM_THIS (This
);
450 Status
= EFI_SUCCESS
;
452 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
454 if (PortNumber
>= TotalPort
) {
455 Status
= EFI_INVALID_PARAMETER
;
459 Offset
= (UINT32
) (XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
460 State
= XhcReadOpReg (Xhc
, Offset
);
463 // Mask off the port status change bits, these bits are
466 State
&= ~ (BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
468 switch (PortFeature
) {
469 case EfiUsbPortEnable
:
471 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
472 // A port may be disabled by software writing a '1' to this flag.
474 Status
= EFI_SUCCESS
;
477 case EfiUsbPortSuspend
:
478 State
|= XHC_PORTSC_LWS
;
479 XhcWriteOpReg (Xhc
, Offset
, State
);
480 State
&= ~XHC_PORTSC_PLS
;
482 XhcWriteOpReg (Xhc
, Offset
, State
);
485 case EfiUsbPortReset
:
486 DEBUG ((EFI_D_INFO
, "XhcUsbPortReset!\n"));
488 // Make sure Host Controller not halt before reset it
490 if (XhcIsHalt (Xhc
)) {
491 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
493 if (EFI_ERROR (Status
)) {
494 DEBUG ((EFI_D_INFO
, "XhcSetRootHubPortFeature :failed to start HC - %r\n", Status
));
499 RouteChart
.Route
.RouteString
= 0;
500 RouteChart
.Route
.RootPortNum
= PortNumber
+ 1;
501 RouteChart
.Route
.TierNum
= 1;
503 // If the port reset operation happens after the usb super speed device is enabled,
504 // The subsequent configuration, such as getting device descriptor, will fail.
505 // So here a workaround is introduced to skip the reset operation if the device is enabled.
507 SlotId
= XhcRouteStringToSlotId (Xhc
, RouteChart
);
510 // 4.3.1 Resetting a Root Hub Port
511 // 1) Write the PORTSC register with the Port Reset (PR) bit set to '1'.
513 State
|= XHC_PORTSC_RESET
;
514 XhcWriteOpReg (Xhc
, Offset
, State
);
515 XhcWaitOpRegBit(Xhc
, Offset
, XHC_PORTSC_PRC
, TRUE
, XHC_GENERIC_TIMEOUT
);
519 case EfiUsbPortPower
:
521 // Not supported, ignore the operation
523 Status
= EFI_SUCCESS
;
526 case EfiUsbPortOwner
:
528 // XHCI root hub port don't has the owner bit, ignore the operation
530 Status
= EFI_SUCCESS
;
534 Status
= EFI_INVALID_PARAMETER
;
538 DEBUG ((EFI_D_INFO
, "XhcSetRootHubPortFeature: status %r\n", Status
));
539 gBS
->RestoreTPL (OldTpl
);
546 Clears a feature for the specified root hub port.
548 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
549 @param PortNumber Specifies the root hub port whose feature is
550 requested to be cleared.
551 @param PortFeature Indicates the feature selector associated with the
552 feature clear request.
554 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
555 for the USB root hub port specified by PortNumber.
556 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
557 @retval EFI_DEVICE_ERROR Can't read register.
562 XhcClearRootHubPortFeature (
563 IN EFI_USB2_HC_PROTOCOL
*This
,
565 IN EFI_USB_PORT_FEATURE PortFeature
568 USB_XHCI_INSTANCE
*Xhc
;
575 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
577 Xhc
= XHC_FROM_THIS (This
);
578 Status
= EFI_SUCCESS
;
580 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
582 if (PortNumber
>= TotalPort
) {
583 Status
= EFI_INVALID_PARAMETER
;
587 Offset
= XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
);
590 // Mask off the port status change bits, these bits are
593 State
= XhcReadOpReg (Xhc
, Offset
);
594 State
&= ~ (BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
596 switch (PortFeature
) {
597 case EfiUsbPortEnable
:
599 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
600 // A port may be disabled by software writing a '1' to this flag.
602 State
|= XHC_PORTSC_PED
;
603 State
&= ~XHC_PORTSC_RESET
;
604 XhcWriteOpReg (Xhc
, Offset
, State
);
607 case EfiUsbPortSuspend
:
608 State
|= XHC_PORTSC_LWS
;
609 XhcWriteOpReg (Xhc
, Offset
, State
);
610 State
&= ~XHC_PORTSC_PLS
;
611 XhcWriteOpReg (Xhc
, Offset
, State
);
614 case EfiUsbPortReset
:
616 // PORTSC_RESET BIT(4) bit is RW1S attribute, which means Write-1-to-set status:
617 // Register bits indicate status when read, a clear bit may be set by
618 // writing a '1'. Writing a '0' to RW1S bits has no effect.
622 case EfiUsbPortOwner
:
624 // XHCI root hub port don't has the owner bit, ignore the operation
628 case EfiUsbPortConnectChange
:
630 // Clear connect status change
632 State
|= XHC_PORTSC_CSC
;
633 XhcWriteOpReg (Xhc
, Offset
, State
);
636 case EfiUsbPortEnableChange
:
638 // Clear enable status change
640 State
|= XHC_PORTSC_PEC
;
641 XhcWriteOpReg (Xhc
, Offset
, State
);
644 case EfiUsbPortOverCurrentChange
:
646 // Clear PortOverCurrent change
648 State
|= XHC_PORTSC_OCC
;
649 XhcWriteOpReg (Xhc
, Offset
, State
);
652 case EfiUsbPortResetChange
:
654 // Clear Port Reset change
656 State
|= XHC_PORTSC_PRC
;
657 XhcWriteOpReg (Xhc
, Offset
, State
);
660 case EfiUsbPortPower
:
661 case EfiUsbPortSuspendChange
:
663 // Not supported or not related operation
668 Status
= EFI_INVALID_PARAMETER
;
673 DEBUG ((EFI_D_INFO
, "XhcClearRootHubPortFeature: status %r\n", Status
));
674 gBS
->RestoreTPL (OldTpl
);
681 Submits control transfer to a target USB device.
683 @param This This EFI_USB2_HC_PROTOCOL instance.
684 @param DeviceAddress The target device address.
685 @param DeviceSpeed Target device speed.
686 @param MaximumPacketLength Maximum packet size the default control transfer
687 endpoint is capable of sending or receiving.
688 @param Request USB device request to send.
689 @param TransferDirection Specifies the data direction for the data stage
690 @param Data Data buffer to be transmitted or received from USB
692 @param DataLength The size (in bytes) of the data buffer.
693 @param Timeout Indicates the maximum timeout, in millisecond.
694 @param Translator Transaction translator to be used by this device.
695 @param TransferResult Return the result of this control transfer.
697 @retval EFI_SUCCESS Transfer was completed successfully.
698 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
699 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
700 @retval EFI_TIMEOUT Transfer failed due to timeout.
701 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
707 IN EFI_USB2_HC_PROTOCOL
*This
,
708 IN UINT8 DeviceAddress
,
709 IN UINT8 DeviceSpeed
,
710 IN UINTN MaximumPacketLength
,
711 IN EFI_USB_DEVICE_REQUEST
*Request
,
712 IN EFI_USB_DATA_DIRECTION TransferDirection
,
714 IN OUT UINTN
*DataLength
,
716 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
717 OUT UINT32
*TransferResult
720 USB_XHCI_INSTANCE
*Xhc
;
724 UINT8 DescriptorType
;
729 EFI_USB_HUB_DESCRIPTOR
*HubDesc
;
732 EFI_STATUS RecoveryStatus
;
734 EFI_USB_PORT_STATUS PortStatus
;
738 // Validate parameters
740 if ((Request
== NULL
) || (TransferResult
== NULL
)) {
741 return EFI_INVALID_PARAMETER
;
744 if ((TransferDirection
!= EfiUsbDataIn
) &&
745 (TransferDirection
!= EfiUsbDataOut
) &&
746 (TransferDirection
!= EfiUsbNoData
)) {
747 return EFI_INVALID_PARAMETER
;
750 if ((TransferDirection
== EfiUsbNoData
) &&
751 ((Data
!= NULL
) || (*DataLength
!= 0))) {
752 return EFI_INVALID_PARAMETER
;
755 if ((TransferDirection
!= EfiUsbNoData
) &&
756 ((Data
== NULL
) || (*DataLength
== 0))) {
757 return EFI_INVALID_PARAMETER
;
760 if ((MaximumPacketLength
!= 8) && (MaximumPacketLength
!= 16) &&
761 (MaximumPacketLength
!= 32) && (MaximumPacketLength
!= 64) &&
762 (MaximumPacketLength
!= 512)
764 return EFI_INVALID_PARAMETER
;
767 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) {
768 return EFI_INVALID_PARAMETER
;
771 if ((DeviceSpeed
== EFI_USB_SPEED_SUPER
) && (MaximumPacketLength
!= 512)) {
772 return EFI_INVALID_PARAMETER
;
775 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
777 Xhc
= XHC_FROM_THIS (This
);
779 Status
= EFI_DEVICE_ERROR
;
780 *TransferResult
= EFI_USB_ERR_SYSTEM
;
782 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
783 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: HC halted at entrance\n"));
788 // Check if the device is still enabled before every transaction.
790 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
796 // Hook the Set_Address request from UsbBus.
797 // According to XHCI 1.0 spec, the Set_Address request is replaced by XHCI's Address_Device cmd.
799 if ((Request
->Request
== USB_REQ_SET_ADDRESS
) &&
800 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
802 // Reset the BusDevAddr field of all disabled entries in UsbDevContext array firstly.
803 // This way is used to clean the history to avoid using wrong device address by XhcAsyncInterruptTransfer().
805 for (Index
= 0; Index
< 255; Index
++) {
806 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
&&
807 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0) &&
808 (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== (UINT8
)Request
->Value
)) {
809 Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
= 0;
813 // The actual device address has been assigned by XHCI during initializing the device slot.
814 // So we just need establish the mapping relationship between the device address requested from UsbBus
815 // and the actual device address assigned by XHCI. The the following invocations through EFI_USB2_HC_PROTOCOL interface
816 // can find out the actual device address by it.
818 Xhc
->UsbDevContext
[SlotId
].BusDevAddr
= (UINT8
)Request
->Value
;
819 Status
= EFI_SUCCESS
;
824 // If the port reset operation happens after the usb super speed device is enabled,
825 // The subsequent configuration, such as getting device descriptor, will fail.
826 // So here a workaround is introduced to skip the reset operation if the device is enabled.
828 if ((Request
->Request
== USB_REQ_SET_FEATURE
) &&
829 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
)) &&
830 (Request
->Value
== EfiUsbPortReset
)) {
831 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
832 Status
= EFI_SUCCESS
;
838 // Create a new URB, insert it into the asynchronous
839 // schedule list, then poll the execution status.
840 // Note that we encode the direction in address although default control
841 // endpoint is bidirectional. XhcCreateUrb expects this
842 // combination of Ep addr and its direction.
844 Endpoint
= (UINT8
) (0 | ((TransferDirection
== EfiUsbDataIn
) ? 0x80 : 0));
860 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: failed to create URB"));
861 Status
= EFI_OUT_OF_RESOURCES
;
864 ASSERT (Urb
->EvtRing
== &Xhc
->EventRing
);
865 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
868 // Get the status from URB. The result is updated in XhcCheckUrbResult
869 // which is called by XhcExecTransfer
871 *TransferResult
= Urb
->Result
;
872 *DataLength
= Urb
->Completed
;
874 if (*TransferResult
== EFI_USB_NOERROR
) {
875 Status
= EFI_SUCCESS
;
876 } else if ((*TransferResult
== EFI_USB_ERR_STALL
) ||
877 (*TransferResult
== EFI_USB_ERR_TIMEOUT
)) {
878 RecoveryStatus
= XhcRecoverHaltedEndpoint(Xhc
, Urb
);
879 ASSERT_EFI_ERROR (RecoveryStatus
);
884 // Hook Get_Descriptor request from UsbBus as we need evaluate context and configure endpoint.
885 // Hook Get_Status request form UsbBus as we need trace device attach/detach event happened at hub.
886 // Hook Set_Config request from UsbBus as we need configure device endpoint.
888 if ((Request
->Request
== USB_REQ_GET_DESCRIPTOR
) &&
889 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
890 DescriptorType
= (UINT8
)(Request
->Value
>> 8);
891 if ((DescriptorType
== USB_DESC_TYPE_DEVICE
) && (*DataLength
== sizeof (EFI_USB_DEVICE_DESCRIPTOR
))) {
892 ASSERT (Data
!= NULL
);
894 // Store a copy of device scriptor as hub device need this info to configure endpoint.
896 CopyMem (&Xhc
->UsbDevContext
[SlotId
].DevDesc
, Data
, *DataLength
);
897 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.BcdUSB
== 0x0300) {
899 // If it's a usb3.0 device, then its max packet size is a 2^n.
901 MaxPacket0
= 1 << Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
903 MaxPacket0
= Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
905 Xhc
->UsbDevContext
[SlotId
].ConfDesc
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
* sizeof (EFI_USB_CONFIG_DESCRIPTOR
*));
906 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
907 Status
= XhcEvaluateContext (Xhc
, SlotId
, MaxPacket0
);
909 Status
= XhcEvaluateContext64 (Xhc
, SlotId
, MaxPacket0
);
911 ASSERT_EFI_ERROR (Status
);
912 } else if (DescriptorType
== USB_DESC_TYPE_CONFIG
) {
913 ASSERT (Data
!= NULL
);
914 if (*DataLength
== ((UINT16
*)Data
)[1]) {
916 // Get configuration value from request, Store the configuration descriptor for Configure_Endpoint cmd.
918 Index
= (UINT8
)Request
->Value
;
919 ASSERT (Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
);
920 Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
] = AllocateZeroPool(*DataLength
);
921 CopyMem (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
], Data
, *DataLength
);
923 } else if ((DescriptorType
== USB_DESC_TYPE_HUB
) ||
924 (DescriptorType
== USB_DESC_TYPE_HUB_SUPER_SPEED
)) {
925 ASSERT (Data
!= NULL
);
926 HubDesc
= (EFI_USB_HUB_DESCRIPTOR
*)Data
;
928 // The bit 5,6 of HubCharacter field of Hub Descriptor is TTT.
930 TTT
= (UINT8
)((HubDesc
->HubCharacter
& (BIT5
| BIT6
)) >> 5);
931 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.DeviceProtocol
== 2) {
933 // Don't support multi-TT feature for super speed hub now.
941 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
942 Status
= XhcConfigHubContext (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
944 Status
= XhcConfigHubContext64 (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
946 ASSERT_EFI_ERROR (Status
);
948 } else if ((Request
->Request
== USB_REQ_SET_CONFIG
) &&
949 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
951 // Hook Set_Config request from UsbBus as we need configure device endpoint.
953 for (Index
= 0; Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
; Index
++) {
954 if (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->ConfigurationValue
== (UINT8
)Request
->Value
) {
955 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
956 Status
= XhcSetConfigCmd (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
958 Status
= XhcSetConfigCmd64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
960 ASSERT_EFI_ERROR (Status
);
964 } else if ((Request
->Request
== USB_REQ_GET_STATUS
) &&
965 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
))) {
966 ASSERT (Data
!= NULL
);
968 // Hook Get_Status request from UsbBus to keep track of the port status change.
970 State
= *(UINT32
*)Data
;
971 PortStatus
.PortStatus
= 0;
972 PortStatus
.PortChangeStatus
= 0;
974 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
976 // For super speed hub, its bit10~12 presents the attached device speed.
978 if ((State
& XHC_PORTSC_PS
) >> 10 == 0) {
979 PortStatus
.PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
981 } else if (DeviceSpeed
== EFI_USB_SPEED_HIGH
) {
983 // For high speed hub, its bit9~10 presents the attached device speed.
985 if (XHC_BIT_IS_SET (State
, BIT9
)) {
986 PortStatus
.PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
987 } else if (XHC_BIT_IS_SET (State
, BIT10
)) {
988 PortStatus
.PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
995 // Convert the XHCI port/port change state to UEFI status
997 MapSize
= sizeof (mUsbPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
998 for (Index
= 0; Index
< MapSize
; Index
++) {
999 if (XHC_BIT_IS_SET (State
, mUsbPortStateMap
[Index
].HwState
)) {
1000 PortStatus
.PortStatus
= (UINT16
) (PortStatus
.PortStatus
| mUsbPortStateMap
[Index
].UefiState
);
1003 MapSize
= sizeof (mUsbPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
1005 for (Index
= 0; Index
< MapSize
; Index
++) {
1006 if (XHC_BIT_IS_SET (State
, mUsbPortChangeMap
[Index
].HwState
)) {
1007 PortStatus
.PortChangeStatus
= (UINT16
) (PortStatus
.PortChangeStatus
| mUsbPortChangeMap
[Index
].UefiState
);
1011 XhcPollPortStatusChange (Xhc
, Xhc
->UsbDevContext
[SlotId
].RouteString
, (UINT8
)Request
->Index
, &PortStatus
);
1019 if (EFI_ERROR (Status
)) {
1020 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1023 gBS
->RestoreTPL (OldTpl
);
1030 Submits bulk transfer to a bulk endpoint of a USB device.
1032 @param This This EFI_USB2_HC_PROTOCOL instance.
1033 @param DeviceAddress Target device address.
1034 @param EndPointAddress Endpoint number and its direction in bit 7.
1035 @param DeviceSpeed Device speed, Low speed device doesn't support bulk
1037 @param MaximumPacketLength Maximum packet size the endpoint is capable of
1038 sending or receiving.
1039 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1040 @param Data Array of pointers to the buffers of data to transmit
1041 from or receive into.
1042 @param DataLength The lenght of the data buffer.
1043 @param DataToggle On input, the initial data toggle for the transfer;
1044 On output, it is updated to to next data toggle to
1045 use of the subsequent bulk transfer.
1046 @param Timeout Indicates the maximum time, in millisecond, which
1047 the transfer is allowed to complete.
1048 @param Translator A pointr to the transaction translator data.
1049 @param TransferResult A pointer to the detailed result information of the
1052 @retval EFI_SUCCESS The transfer was completed successfully.
1053 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1054 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1055 @retval EFI_TIMEOUT The transfer failed due to timeout.
1056 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1062 IN EFI_USB2_HC_PROTOCOL
*This
,
1063 IN UINT8 DeviceAddress
,
1064 IN UINT8 EndPointAddress
,
1065 IN UINT8 DeviceSpeed
,
1066 IN UINTN MaximumPacketLength
,
1067 IN UINT8 DataBuffersNumber
,
1068 IN OUT VOID
*Data
[EFI_USB_MAX_BULK_BUFFER_NUM
],
1069 IN OUT UINTN
*DataLength
,
1070 IN OUT UINT8
*DataToggle
,
1072 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1073 OUT UINT32
*TransferResult
1076 USB_XHCI_INSTANCE
*Xhc
;
1080 EFI_STATUS RecoveryStatus
;
1084 // Validate the parameters
1086 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1087 (Data
== NULL
) || (Data
[0] == NULL
) || (TransferResult
== NULL
)) {
1088 return EFI_INVALID_PARAMETER
;
1091 if ((*DataToggle
!= 0) && (*DataToggle
!= 1)) {
1092 return EFI_INVALID_PARAMETER
;
1095 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) ||
1096 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1097 ((EFI_USB_SPEED_HIGH
== DeviceSpeed
) && (MaximumPacketLength
> 512)) ||
1098 ((EFI_USB_SPEED_SUPER
== DeviceSpeed
) && (MaximumPacketLength
> 1024))) {
1099 return EFI_INVALID_PARAMETER
;
1102 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1104 Xhc
= XHC_FROM_THIS (This
);
1106 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1107 Status
= EFI_DEVICE_ERROR
;
1109 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1110 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: HC is halted\n"));
1115 // Check if the device is still enabled before every transaction.
1117 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1123 // Create a new URB, insert it into the asynchronous
1124 // schedule list, then poll the execution status.
1126 Urb
= XhcCreateUrb (
1131 MaximumPacketLength
,
1141 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: failed to create URB\n"));
1142 Status
= EFI_OUT_OF_RESOURCES
;
1146 ASSERT (Urb
->EvtRing
== &Xhc
->EventRing
);
1148 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
1150 *TransferResult
= Urb
->Result
;
1151 *DataLength
= Urb
->Completed
;
1153 if (*TransferResult
== EFI_USB_NOERROR
) {
1154 Status
= EFI_SUCCESS
;
1155 } else if ((*TransferResult
== EFI_USB_ERR_STALL
) ||
1156 (*TransferResult
== EFI_USB_ERR_TIMEOUT
)) {
1157 RecoveryStatus
= XhcRecoverHaltedEndpoint(Xhc
, Urb
);
1158 ASSERT_EFI_ERROR (RecoveryStatus
);
1165 if (EFI_ERROR (Status
)) {
1166 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1168 gBS
->RestoreTPL (OldTpl
);
1174 Submits an asynchronous interrupt transfer to an
1175 interrupt endpoint of a USB device.
1177 @param This This EFI_USB2_HC_PROTOCOL instance.
1178 @param DeviceAddress Target device address.
1179 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1180 @param DeviceSpeed Indicates device speed.
1181 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1182 @param IsNewTransfer If TRUE, to submit an new asynchronous interrupt
1183 transfer If FALSE, to remove the specified
1184 asynchronous interrupt.
1185 @param DataToggle On input, the initial data toggle to use; on output,
1186 it is updated to indicate the next data toggle.
1187 @param PollingInterval The he interval, in milliseconds, that the transfer
1189 @param DataLength The length of data to receive at the rate specified
1191 @param Translator Transaction translator to use.
1192 @param CallBackFunction Function to call at the rate specified by
1194 @param Context Context to CallBackFunction.
1196 @retval EFI_SUCCESS The request has been successfully submitted or canceled.
1197 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1198 @retval EFI_OUT_OF_RESOURCES The request failed due to a lack of resources.
1199 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1204 XhcAsyncInterruptTransfer (
1205 IN EFI_USB2_HC_PROTOCOL
*This
,
1206 IN UINT8 DeviceAddress
,
1207 IN UINT8 EndPointAddress
,
1208 IN UINT8 DeviceSpeed
,
1209 IN UINTN MaximumPacketLength
,
1210 IN BOOLEAN IsNewTransfer
,
1211 IN OUT UINT8
*DataToggle
,
1212 IN UINTN PollingInterval
,
1213 IN UINTN DataLength
,
1214 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1215 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction
,
1216 IN VOID
*Context OPTIONAL
1219 USB_XHCI_INSTANCE
*Xhc
;
1228 // Validate parameters
1230 if (!XHCI_IS_DATAIN (EndPointAddress
)) {
1231 return EFI_INVALID_PARAMETER
;
1234 if (IsNewTransfer
) {
1235 if (DataLength
== 0) {
1236 return EFI_INVALID_PARAMETER
;
1239 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1240 return EFI_INVALID_PARAMETER
;
1243 if ((PollingInterval
> 255) || (PollingInterval
< 1)) {
1244 return EFI_INVALID_PARAMETER
;
1248 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1250 Xhc
= XHC_FROM_THIS (This
);
1253 // Delete Async interrupt transfer request.
1255 if (!IsNewTransfer
) {
1257 // The delete request may happen after device is detached.
1259 for (Index
= 0; Index
< 255; Index
++) {
1260 if (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== DeviceAddress
) {
1266 Status
= EFI_INVALID_PARAMETER
;
1270 Status
= XhciDelAsyncIntTransfer (Xhc
, DeviceAddress
, EndPointAddress
);
1271 DEBUG ((EFI_D_INFO
, "XhcAsyncInterruptTransfer: remove old transfer, Status = %r\n", Status
));
1275 Status
= EFI_SUCCESS
;
1277 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1278 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: HC is halt\n"));
1279 Status
= EFI_DEVICE_ERROR
;
1284 // Check if the device is still enabled before every transaction.
1286 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1291 Data
= AllocateZeroPool (DataLength
);
1294 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: failed to allocate buffer\n"));
1295 Status
= EFI_OUT_OF_RESOURCES
;
1299 Urb
= XhcCreateUrb (
1304 MaximumPacketLength
,
1305 XHC_INT_TRANSFER_ASYNC
,
1314 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: failed to create URB\n"));
1316 Status
= EFI_OUT_OF_RESOURCES
;
1320 ASSERT (Urb
->EvtRing
== &Xhc
->EventRing
);
1322 InsertHeadList (&Xhc
->AsyncIntTransfers
, &Urb
->UrbList
);
1324 // Ring the doorbell
1326 Status
= RingIntTransferDoorBell (Xhc
, Urb
);
1329 gBS
->RestoreTPL (OldTpl
);
1336 Submits synchronous interrupt transfer to an interrupt endpoint
1339 @param This This EFI_USB2_HC_PROTOCOL instance.
1340 @param DeviceAddress Target device address.
1341 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1342 @param DeviceSpeed Indicates device speed.
1343 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1344 of sending or receiving.
1345 @param Data Buffer of data that will be transmitted to USB
1346 device or received from USB device.
1347 @param DataLength On input, the size, in bytes, of the data buffer; On
1348 output, the number of bytes transferred.
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 Timeout Maximum time, in second, to complete.
1352 @param Translator Transaction translator to use.
1353 @param TransferResult Variable to receive the transfer result.
1355 @return EFI_SUCCESS The transfer was completed successfully.
1356 @return EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1357 @return EFI_INVALID_PARAMETER Some parameters are invalid.
1358 @return EFI_TIMEOUT The transfer failed due to timeout.
1359 @return EFI_DEVICE_ERROR The failed due to host controller or device error
1364 XhcSyncInterruptTransfer (
1365 IN EFI_USB2_HC_PROTOCOL
*This
,
1366 IN UINT8 DeviceAddress
,
1367 IN UINT8 EndPointAddress
,
1368 IN UINT8 DeviceSpeed
,
1369 IN UINTN MaximumPacketLength
,
1371 IN OUT UINTN
*DataLength
,
1372 IN OUT UINT8
*DataToggle
,
1374 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1375 OUT UINT32
*TransferResult
1378 USB_XHCI_INSTANCE
*Xhc
;
1382 EFI_STATUS RecoveryStatus
;
1386 // Validates parameters
1388 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1389 (Data
== NULL
) || (TransferResult
== NULL
)) {
1390 return EFI_INVALID_PARAMETER
;
1393 if (!XHCI_IS_DATAIN (EndPointAddress
)) {
1394 return EFI_INVALID_PARAMETER
;
1397 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1398 return EFI_INVALID_PARAMETER
;
1401 if (((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) ||
1402 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1403 ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) && (MaximumPacketLength
> 3072))) {
1404 return EFI_INVALID_PARAMETER
;
1407 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1409 Xhc
= XHC_FROM_THIS (This
);
1411 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1412 Status
= EFI_DEVICE_ERROR
;
1414 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1415 DEBUG ((EFI_D_ERROR
, "EhcSyncInterruptTransfer: HC is halt\n"));
1420 // Check if the device is still enabled before every transaction.
1422 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1427 Urb
= XhcCreateUrb (
1432 MaximumPacketLength
,
1433 XHC_INT_TRANSFER_SYNC
,
1442 DEBUG ((EFI_D_ERROR
, "XhcSyncInterruptTransfer: failed to create URB\n"));
1443 Status
= EFI_OUT_OF_RESOURCES
;
1447 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
1449 *TransferResult
= Urb
->Result
;
1450 *DataLength
= Urb
->Completed
;
1452 if (*TransferResult
== EFI_USB_NOERROR
) {
1453 Status
= EFI_SUCCESS
;
1454 } else if ((*TransferResult
== EFI_USB_ERR_STALL
) ||
1455 (*TransferResult
== EFI_USB_ERR_TIMEOUT
)) {
1456 RecoveryStatus
= XhcRecoverHaltedEndpoint(Xhc
, Urb
);
1457 ASSERT_EFI_ERROR (RecoveryStatus
);
1463 if (EFI_ERROR (Status
)) {
1464 DEBUG ((EFI_D_ERROR
, "XhcSyncInterruptTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1466 gBS
->RestoreTPL (OldTpl
);
1473 Submits isochronous transfer to a target USB device.
1475 @param This This EFI_USB2_HC_PROTOCOL instance.
1476 @param DeviceAddress Target device address.
1477 @param EndPointAddress End point address with its direction.
1478 @param DeviceSpeed Device speed, Low speed device doesn't support this
1480 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1481 sending or receiving.
1482 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1483 @param Data Array of pointers to the buffers of data that will
1484 be transmitted to USB device or received from USB
1486 @param DataLength The size, in bytes, of the data buffer.
1487 @param Translator Transaction translator to use.
1488 @param TransferResult Variable to receive the transfer result.
1490 @return EFI_UNSUPPORTED Isochronous transfer is unsupported.
1495 XhcIsochronousTransfer (
1496 IN EFI_USB2_HC_PROTOCOL
*This
,
1497 IN UINT8 DeviceAddress
,
1498 IN UINT8 EndPointAddress
,
1499 IN UINT8 DeviceSpeed
,
1500 IN UINTN MaximumPacketLength
,
1501 IN UINT8 DataBuffersNumber
,
1502 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1503 IN UINTN DataLength
,
1504 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1505 OUT UINT32
*TransferResult
1508 return EFI_UNSUPPORTED
;
1513 Submits Async isochronous transfer to a target USB device.
1515 @param This This EFI_USB2_HC_PROTOCOL instance.
1516 @param DeviceAddress Target device address.
1517 @param EndPointAddress End point address with its direction.
1518 @param DeviceSpeed Device speed, Low speed device doesn't support this
1520 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1521 sending or receiving.
1522 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1523 @param Data Array of pointers to the buffers of data that will
1524 be transmitted to USB device or received from USB
1526 @param DataLength The size, in bytes, of the data buffer.
1527 @param Translator Transaction translator to use.
1528 @param IsochronousCallBack Function to be called when the transfer complete.
1529 @param Context Context passed to the call back function as
1532 @return EFI_UNSUPPORTED Isochronous transfer isn't supported.
1537 XhcAsyncIsochronousTransfer (
1538 IN EFI_USB2_HC_PROTOCOL
*This
,
1539 IN UINT8 DeviceAddress
,
1540 IN UINT8 EndPointAddress
,
1541 IN UINT8 DeviceSpeed
,
1542 IN UINTN MaximumPacketLength
,
1543 IN UINT8 DataBuffersNumber
,
1544 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1545 IN UINTN DataLength
,
1546 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1547 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
1551 return EFI_UNSUPPORTED
;
1555 Entry point for EFI drivers.
1557 @param ImageHandle EFI_HANDLE.
1558 @param SystemTable EFI_SYSTEM_TABLE.
1560 @retval EFI_SUCCESS Success.
1561 @retval Others Fail.
1566 XhcDriverEntryPoint (
1567 IN EFI_HANDLE ImageHandle
,
1568 IN EFI_SYSTEM_TABLE
*SystemTable
1571 return EfiLibInstallDriverBindingComponentName2 (
1574 &gXhciDriverBinding
,
1576 &gXhciComponentName
,
1577 &gXhciComponentName2
1583 Test to see if this driver supports ControllerHandle. Any
1584 ControllerHandle that has Usb2HcProtocol installed will
1587 @param This Protocol instance pointer.
1588 @param Controller Handle of device to test.
1589 @param RemainingDevicePath Not used.
1591 @return EFI_SUCCESS This driver supports this device.
1592 @return EFI_UNSUPPORTED This driver does not support this device.
1597 XhcDriverBindingSupported (
1598 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1599 IN EFI_HANDLE Controller
,
1600 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1604 EFI_PCI_IO_PROTOCOL
*PciIo
;
1605 USB_CLASSC UsbClassCReg
;
1608 // Test whether there is PCI IO Protocol attached on the controller handle.
1610 Status
= gBS
->OpenProtocol (
1612 &gEfiPciIoProtocolGuid
,
1614 This
->DriverBindingHandle
,
1616 EFI_OPEN_PROTOCOL_BY_DRIVER
1619 if (EFI_ERROR (Status
)) {
1620 return EFI_UNSUPPORTED
;
1623 Status
= PciIo
->Pci
.Read (
1626 PCI_CLASSCODE_OFFSET
,
1627 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1631 if (EFI_ERROR (Status
)) {
1632 Status
= EFI_UNSUPPORTED
;
1637 // Test whether the controller belongs to Xhci type
1639 if ((UsbClassCReg
.BaseCode
!= PCI_CLASS_SERIAL
) ||
1640 (UsbClassCReg
.SubClassCode
!= PCI_CLASS_SERIAL_USB
) ||
1641 (UsbClassCReg
.ProgInterface
!= PCI_IF_XHCI
)) {
1642 Status
= EFI_UNSUPPORTED
;
1646 gBS
->CloseProtocol (
1648 &gEfiPciIoProtocolGuid
,
1649 This
->DriverBindingHandle
,
1657 Create and initialize a USB_XHCI_INSTANCE structure.
1659 @param PciIo The PciIo on this device.
1660 @param OriginalPciAttributes Original PCI attributes.
1662 @return The allocated and initialized USB_XHCI_INSTANCE structure if created,
1668 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1669 IN UINT64 OriginalPciAttributes
1672 USB_XHCI_INSTANCE
*Xhc
;
1677 Xhc
= AllocateZeroPool (sizeof (USB_XHCI_INSTANCE
));
1684 // Initialize private data structure
1686 Xhc
->Signature
= XHCI_INSTANCE_SIG
;
1688 Xhc
->OriginalPciAttributes
= OriginalPciAttributes
;
1689 CopyMem (&Xhc
->Usb2Hc
, &gXhciUsb2HcTemplate
, sizeof (EFI_USB2_HC_PROTOCOL
));
1691 InitializeListHead (&Xhc
->AsyncIntTransfers
);
1694 // Be caution that the Offset passed to XhcReadCapReg() should be Dword align
1696 Xhc
->CapLength
= XhcReadCapReg8 (Xhc
, XHC_CAPLENGTH_OFFSET
);
1697 Xhc
->HcSParams1
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS1_OFFSET
);
1698 Xhc
->HcSParams2
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS2_OFFSET
);
1699 Xhc
->HcCParams
.Dword
= XhcReadCapReg (Xhc
, XHC_HCCPARAMS_OFFSET
);
1700 Xhc
->DBOff
= XhcReadCapReg (Xhc
, XHC_DBOFF_OFFSET
);
1701 Xhc
->RTSOff
= XhcReadCapReg (Xhc
, XHC_RTSOFF_OFFSET
);
1704 // This PageSize field defines the page size supported by the xHC implementation.
1705 // This xHC supports a page size of 2^(n+12) if bit n is Set. For example,
1706 // if bit 0 is Set, the xHC supports 4k byte page sizes.
1708 PageSize
= XhcReadOpReg(Xhc
, XHC_PAGESIZE_OFFSET
) & XHC_PAGESIZE_MASK
;
1709 Xhc
->PageSize
= 1 << (HighBitSet32(PageSize
) + 12);
1711 ExtCapReg
= (UINT16
) (Xhc
->HcCParams
.Data
.ExtCapReg
);
1712 Xhc
->ExtCapRegBase
= ExtCapReg
<< 2;
1713 Xhc
->UsbLegSupOffset
= XhcGetLegSupCapAddr (Xhc
);
1715 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: Capability length 0x%x\n", Xhc
->CapLength
));
1716 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcSParams1 0x%x\n", Xhc
->HcSParams1
));
1717 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcSParams2 0x%x\n", Xhc
->HcSParams2
));
1718 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcCParams 0x%x\n", Xhc
->HcCParams
));
1719 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: DBOff 0x%x\n", Xhc
->DBOff
));
1720 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: RTSOff 0x%x\n", Xhc
->RTSOff
));
1721 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: UsbLegSupOffset 0x%x\n", Xhc
->UsbLegSupOffset
));
1724 // Create AsyncRequest Polling Timer
1726 Status
= gBS
->CreateEvent (
1727 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1729 XhcMonitorAsyncRequests
,
1734 if (EFI_ERROR (Status
)) {
1746 One notified function to stop the Host Controller when gBS->ExitBootServices() called.
1748 @param Event Pointer to this event
1749 @param Context Event hanlder private data
1754 XhcExitBootService (
1760 USB_XHCI_INSTANCE
*Xhc
;
1761 EFI_PCI_IO_PROTOCOL
*PciIo
;
1763 Xhc
= (USB_XHCI_INSTANCE
*) Context
;
1767 // Stop AsyncRequest Polling timer then stop the XHCI driver
1768 // and uninstall the XHCI protocl.
1770 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
1771 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
1773 if (Xhc
->PollTimer
!= NULL
) {
1774 gBS
->CloseEvent (Xhc
->PollTimer
);
1778 // Restore original PCI attributes
1782 EfiPciIoAttributeOperationSet
,
1783 Xhc
->OriginalPciAttributes
,
1787 XhcClearBiosOwnership (Xhc
);
1791 Starting the Usb XHCI Driver.
1793 @param This Protocol instance pointer.
1794 @param Controller Handle of device to test.
1795 @param RemainingDevicePath Not used.
1797 @return EFI_SUCCESS supports this device.
1798 @return EFI_UNSUPPORTED do not support this device.
1799 @return EFI_DEVICE_ERROR cannot be started due to device Error.
1800 @return EFI_OUT_OF_RESOURCES cannot allocate resources.
1805 XhcDriverBindingStart (
1806 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1807 IN EFI_HANDLE Controller
,
1808 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1812 EFI_PCI_IO_PROTOCOL
*PciIo
;
1814 UINT64 OriginalPciAttributes
;
1815 BOOLEAN PciAttributesSaved
;
1816 USB_XHCI_INSTANCE
*Xhc
;
1819 // Open the PciIo Protocol, then enable the USB host controller
1821 Status
= gBS
->OpenProtocol (
1823 &gEfiPciIoProtocolGuid
,
1825 This
->DriverBindingHandle
,
1827 EFI_OPEN_PROTOCOL_BY_DRIVER
1830 if (EFI_ERROR (Status
)) {
1834 PciAttributesSaved
= FALSE
;
1836 // Save original PCI attributes
1838 Status
= PciIo
->Attributes (
1840 EfiPciIoAttributeOperationGet
,
1842 &OriginalPciAttributes
1845 if (EFI_ERROR (Status
)) {
1848 PciAttributesSaved
= TRUE
;
1850 Status
= PciIo
->Attributes (
1852 EfiPciIoAttributeOperationSupported
,
1856 if (!EFI_ERROR (Status
)) {
1857 Supports
&= EFI_PCI_DEVICE_ENABLE
;
1858 Status
= PciIo
->Attributes (
1860 EfiPciIoAttributeOperationEnable
,
1866 if (EFI_ERROR (Status
)) {
1867 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to enable controller\n"));
1872 // Create then install USB2_HC_PROTOCOL
1874 Xhc
= XhcCreateUsbHc (PciIo
, OriginalPciAttributes
);
1877 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to create USB2_HC\n"));
1878 return EFI_OUT_OF_RESOURCES
;
1881 XhcSetBiosOwnership (Xhc
);
1883 XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
1884 ASSERT (XhcIsHalt (Xhc
));
1887 // After Chip Hardware Reset wait until the Controller Not Ready (CNR) flag
1888 // in the USBSTS is '0' before writing any xHC Operational or Runtime registers.
1890 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
1893 // Initialize the schedule
1898 // Start the Host Controller
1900 XhcRunHC(Xhc
, XHC_GENERIC_TIMEOUT
);
1903 // Start the asynchronous interrupt monitor
1905 Status
= gBS
->SetTimer (Xhc
->PollTimer
, TimerPeriodic
, XHC_ASYNC_TIMER_INTERVAL
);
1906 if (EFI_ERROR (Status
)) {
1907 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to start async interrupt monitor\n"));
1908 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
1913 // Create event to stop the HC when exit boot service.
1915 Status
= gBS
->CreateEventEx (
1920 &gEfiEventExitBootServicesGuid
,
1921 &Xhc
->ExitBootServiceEvent
1923 if (EFI_ERROR (Status
)) {
1928 // Install the component name protocol, don't fail the start
1929 // because of something for display.
1933 gXhciComponentName
.SupportedLanguages
,
1934 &Xhc
->ControllerNameTable
,
1935 L
"eXtensible Host Controller (USB 3.0)",
1940 gXhciComponentName2
.SupportedLanguages
,
1941 &Xhc
->ControllerNameTable
,
1942 L
"eXtensible Host Controller (USB 3.0)",
1946 Status
= gBS
->InstallProtocolInterface (
1948 &gEfiUsb2HcProtocolGuid
,
1949 EFI_NATIVE_INTERFACE
,
1952 if (EFI_ERROR (Status
)) {
1953 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to install USB2_HC Protocol\n"));
1957 DEBUG ((EFI_D_INFO
, "XhcDriverBindingStart: XHCI started for controller @ %x\n", Controller
));
1961 gBS
->CloseEvent (Xhc
->PollTimer
);
1966 if (PciAttributesSaved
) {
1968 // Restore original PCI attributes
1972 EfiPciIoAttributeOperationSet
,
1973 OriginalPciAttributes
,
1978 gBS
->CloseProtocol (
1980 &gEfiPciIoProtocolGuid
,
1981 This
->DriverBindingHandle
,
1990 Stop this driver on ControllerHandle. Support stoping any child handles
1991 created by this driver.
1993 @param This Protocol instance pointer.
1994 @param Controller Handle of device to stop driver on.
1995 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
1996 @param ChildHandleBuffer List of handles for the children we need to stop.
1998 @return EFI_SUCCESS Success.
1999 @return EFI_DEVICE_ERROR Fail.
2004 XhcDriverBindingStop (
2005 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
2006 IN EFI_HANDLE Controller
,
2007 IN UINTN NumberOfChildren
,
2008 IN EFI_HANDLE
*ChildHandleBuffer
2012 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
2013 EFI_PCI_IO_PROTOCOL
*PciIo
;
2014 USB_XHCI_INSTANCE
*Xhc
;
2018 // Test whether the Controller handler passed in is a valid
2019 // Usb controller handle that should be supported, if not,
2020 // return the error status directly
2022 Status
= gBS
->OpenProtocol (
2024 &gEfiUsb2HcProtocolGuid
,
2026 This
->DriverBindingHandle
,
2028 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2031 if (EFI_ERROR (Status
)) {
2035 Xhc
= XHC_FROM_THIS (Usb2Hc
);
2039 // Stop AsyncRequest Polling timer then stop the XHCI driver
2040 // and uninstall the XHCI protocl.
2042 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
2045 // Disable the device slots occupied by these devices on its downstream ports.
2046 // Entry 0 is reserved.
2048 for (Index
= 0; Index
< 255; Index
++) {
2049 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
||
2050 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0)) {
2053 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
2054 XhcDisableSlotCmd (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2056 XhcDisableSlotCmd64 (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2060 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2061 XhcClearBiosOwnership (Xhc
);
2063 Status
= gBS
->UninstallProtocolInterface (
2065 &gEfiUsb2HcProtocolGuid
,
2069 if (EFI_ERROR (Status
)) {
2073 if (Xhc
->PollTimer
!= NULL
) {
2074 gBS
->CloseEvent (Xhc
->PollTimer
);
2077 if (Xhc
->ExitBootServiceEvent
!= NULL
) {
2078 gBS
->CloseEvent (Xhc
->ExitBootServiceEvent
);
2081 XhciDelAllAsyncIntTransfers (Xhc
);
2084 if (Xhc
->ControllerNameTable
) {
2085 FreeUnicodeStringTable (Xhc
->ControllerNameTable
);
2089 // Restore original PCI attributes
2093 EfiPciIoAttributeOperationSet
,
2094 Xhc
->OriginalPciAttributes
,
2098 gBS
->CloseProtocol (
2100 &gEfiPciIoProtocolGuid
,
2101 This
->DriverBindingHandle
,