2 The XHCI controller driver.
4 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 // Two arrays used to translate the XHCI port state (change)
19 // to the UEFI protocol's port state (change).
21 USB_PORT_STATE_MAP mUsbPortStateMap
[] = {
22 {XHC_PORTSC_CCS
, USB_PORT_STAT_CONNECTION
},
23 {XHC_PORTSC_PED
, USB_PORT_STAT_ENABLE
},
24 {XHC_PORTSC_OCA
, USB_PORT_STAT_OVERCURRENT
},
25 {XHC_PORTSC_RESET
, USB_PORT_STAT_RESET
}
28 USB_PORT_STATE_MAP mUsbPortChangeMap
[] = {
29 {XHC_PORTSC_CSC
, USB_PORT_STAT_C_CONNECTION
},
30 {XHC_PORTSC_PEC
, USB_PORT_STAT_C_ENABLE
},
31 {XHC_PORTSC_OCC
, USB_PORT_STAT_C_OVERCURRENT
},
32 {XHC_PORTSC_PRC
, USB_PORT_STAT_C_RESET
}
35 USB_PORT_STATE_MAP mUsbHubPortStateMap
[] = {
36 {XHC_HUB_PORTSC_CCS
, USB_PORT_STAT_CONNECTION
},
37 {XHC_HUB_PORTSC_PED
, USB_PORT_STAT_ENABLE
},
38 {XHC_HUB_PORTSC_OCA
, USB_PORT_STAT_OVERCURRENT
},
39 {XHC_HUB_PORTSC_RESET
, USB_PORT_STAT_RESET
}
42 USB_PORT_STATE_MAP mUsbHubPortChangeMap
[] = {
43 {XHC_HUB_PORTSC_CSC
, USB_PORT_STAT_C_CONNECTION
},
44 {XHC_HUB_PORTSC_PEC
, USB_PORT_STAT_C_ENABLE
},
45 {XHC_HUB_PORTSC_OCC
, USB_PORT_STAT_C_OVERCURRENT
},
46 {XHC_HUB_PORTSC_PRC
, USB_PORT_STAT_C_RESET
}
49 EFI_DRIVER_BINDING_PROTOCOL gXhciDriverBinding
= {
50 XhcDriverBindingSupported
,
51 XhcDriverBindingStart
,
59 // Template for Xhci's Usb2 Host Controller Protocol Instance.
61 EFI_USB2_HC_PROTOCOL gXhciUsb2HcTemplate
= {
68 XhcAsyncInterruptTransfer
,
69 XhcSyncInterruptTransfer
,
70 XhcIsochronousTransfer
,
71 XhcAsyncIsochronousTransfer
,
72 XhcGetRootHubPortStatus
,
73 XhcSetRootHubPortFeature
,
74 XhcClearRootHubPortFeature
,
80 Retrieves the capability of root hub ports.
82 @param This The EFI_USB2_HC_PROTOCOL instance.
83 @param MaxSpeed Max speed supported by the controller.
84 @param PortNumber Number of the root hub ports.
85 @param Is64BitCapable Whether the controller supports 64-bit memory
88 @retval EFI_SUCCESS Host controller capability were retrieved successfully.
89 @retval EFI_INVALID_PARAMETER Either of the three capability pointer is NULL.
95 IN EFI_USB2_HC_PROTOCOL
*This
,
97 OUT UINT8
*PortNumber
,
98 OUT UINT8
*Is64BitCapable
101 USB_XHCI_INSTANCE
*Xhc
;
104 if ((MaxSpeed
== NULL
) || (PortNumber
== NULL
) || (Is64BitCapable
== NULL
)) {
105 return EFI_INVALID_PARAMETER
;
108 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
110 Xhc
= XHC_FROM_THIS (This
);
111 *MaxSpeed
= EFI_USB_SPEED_SUPER
;
112 *PortNumber
= (UINT8
) (Xhc
->HcSParams1
.Data
.MaxPorts
);
113 *Is64BitCapable
= (UINT8
) (Xhc
->HcCParams
.Data
.Ac64
);
114 DEBUG ((EFI_D_INFO
, "XhcGetCapability: %d ports, 64 bit %d\n", *PortNumber
, *Is64BitCapable
));
116 gBS
->RestoreTPL (OldTpl
);
123 Provides software reset for the USB host controller.
125 @param This This EFI_USB2_HC_PROTOCOL instance.
126 @param Attributes A bit mask of the reset operation to perform.
128 @retval EFI_SUCCESS The reset operation succeeded.
129 @retval EFI_INVALID_PARAMETER Attributes is not valid.
130 @retval EFI_UNSUPPOURTED The type of reset specified by Attributes is
131 not currently supported by the host controller.
132 @retval EFI_DEVICE_ERROR Host controller isn't halted to reset.
138 IN EFI_USB2_HC_PROTOCOL
*This
,
142 USB_XHCI_INSTANCE
*Xhc
;
146 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
148 Xhc
= XHC_FROM_THIS (This
);
150 switch (Attributes
) {
151 case EFI_USB_HC_RESET_GLOBAL
:
153 // Flow through, same behavior as Host Controller Reset
155 case EFI_USB_HC_RESET_HOST_CONTROLLER
:
157 // Host Controller must be Halt when Reset it
159 if (!XhcIsHalt (Xhc
)) {
160 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
162 if (EFI_ERROR (Status
)) {
163 Status
= EFI_DEVICE_ERROR
;
168 Status
= XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
169 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
171 if (EFI_ERROR (Status
)) {
175 // Clean up the asynchronous transfers, currently only
176 // interrupt supports asynchronous operation.
178 XhciDelAllAsyncIntTransfers (Xhc
);
184 case EFI_USB_HC_RESET_GLOBAL_WITH_DEBUG
:
185 case EFI_USB_HC_RESET_HOST_WITH_DEBUG
:
186 Status
= EFI_UNSUPPORTED
;
190 Status
= EFI_INVALID_PARAMETER
;
194 DEBUG ((EFI_D_INFO
, "XhcReset: status %r\n", Status
));
195 gBS
->RestoreTPL (OldTpl
);
202 Retrieve the current state of the USB host controller.
204 @param This This EFI_USB2_HC_PROTOCOL instance.
205 @param State Variable to return the current host controller
208 @retval EFI_SUCCESS Host controller state was returned in State.
209 @retval EFI_INVALID_PARAMETER State is NULL.
210 @retval EFI_DEVICE_ERROR An error was encountered while attempting to
211 retrieve the host controller's current state.
217 IN EFI_USB2_HC_PROTOCOL
*This
,
218 OUT EFI_USB_HC_STATE
*State
221 USB_XHCI_INSTANCE
*Xhc
;
225 return EFI_INVALID_PARAMETER
;
228 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
230 Xhc
= XHC_FROM_THIS (This
);
232 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
233 *State
= EfiUsbHcStateHalt
;
235 *State
= EfiUsbHcStateOperational
;
238 DEBUG ((EFI_D_INFO
, "XhcGetState: current state %d\n", *State
));
239 gBS
->RestoreTPL (OldTpl
);
245 Sets the USB host controller to a specific state.
247 @param This This EFI_USB2_HC_PROTOCOL instance.
248 @param State The state of the host controller that will be set.
250 @retval EFI_SUCCESS The USB host controller was successfully placed
251 in the state specified by State.
252 @retval EFI_INVALID_PARAMETER State is invalid.
253 @retval EFI_DEVICE_ERROR Failed to set the state due to device error.
259 IN EFI_USB2_HC_PROTOCOL
*This
,
260 IN EFI_USB_HC_STATE State
263 USB_XHCI_INSTANCE
*Xhc
;
265 EFI_USB_HC_STATE CurState
;
268 Status
= XhcGetState (This
, &CurState
);
270 if (EFI_ERROR (Status
)) {
271 return EFI_DEVICE_ERROR
;
274 if (CurState
== State
) {
278 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
280 Xhc
= XHC_FROM_THIS (This
);
283 case EfiUsbHcStateHalt
:
284 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
287 case EfiUsbHcStateOperational
:
288 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HSE
)) {
289 Status
= EFI_DEVICE_ERROR
;
294 // Software must not write a one to this field unless the host controller
295 // is in the Halted state. Doing so will yield undefined results.
296 // refers to Spec[XHCI1.0-2.3.1]
298 if (!XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
299 Status
= EFI_DEVICE_ERROR
;
303 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
306 case EfiUsbHcStateSuspend
:
307 Status
= EFI_UNSUPPORTED
;
311 Status
= EFI_INVALID_PARAMETER
;
314 DEBUG ((EFI_D_INFO
, "XhcSetState: status %r\n", Status
));
315 gBS
->RestoreTPL (OldTpl
);
321 Retrieves the current status of a USB root hub port.
323 @param This This EFI_USB2_HC_PROTOCOL instance.
324 @param PortNumber The root hub port to retrieve the state from.
325 This value is zero-based.
326 @param PortStatus Variable to receive the port state.
328 @retval EFI_SUCCESS The status of the USB root hub port specified.
329 by PortNumber was returned in PortStatus.
330 @retval EFI_INVALID_PARAMETER PortNumber is invalid.
331 @retval EFI_DEVICE_ERROR Can't read register.
336 XhcGetRootHubPortStatus (
337 IN EFI_USB2_HC_PROTOCOL
*This
,
339 OUT EFI_USB_PORT_STATUS
*PortStatus
342 USB_XHCI_INSTANCE
*Xhc
;
349 USB_DEV_ROUTE ParentRouteChart
;
352 if (PortStatus
== NULL
) {
353 return EFI_INVALID_PARAMETER
;
356 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
358 Xhc
= XHC_FROM_THIS (This
);
359 Status
= EFI_SUCCESS
;
361 TotalPort
= Xhc
->HcSParams1
.Data
.MaxPorts
;
363 if (PortNumber
>= TotalPort
) {
364 Status
= EFI_INVALID_PARAMETER
;
368 Offset
= (UINT32
) (XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
369 PortStatus
->PortStatus
= 0;
370 PortStatus
->PortChangeStatus
= 0;
372 State
= XhcReadOpReg (Xhc
, Offset
);
375 // According to XHCI 1.0 spec, bit 10~13 of the root port status register identifies the speed of the attached device.
377 switch ((State
& XHC_PORTSC_PS
) >> 10) {
379 PortStatus
->PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
383 PortStatus
->PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
387 PortStatus
->PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
395 // Convert the XHCI port/port change state to UEFI status
397 MapSize
= sizeof (mUsbPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
399 for (Index
= 0; Index
< MapSize
; Index
++) {
400 if (XHC_BIT_IS_SET (State
, mUsbPortStateMap
[Index
].HwState
)) {
401 PortStatus
->PortStatus
= (UINT16
) (PortStatus
->PortStatus
| mUsbPortStateMap
[Index
].UefiState
);
405 // Bit5~8 reflects its current link state.
407 if ((State
& XHC_PORTSC_PLS
) >> 5 == 3) {
408 PortStatus
->PortStatus
|= USB_PORT_STAT_SUSPEND
;
411 MapSize
= sizeof (mUsbPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
413 for (Index
= 0; Index
< MapSize
; Index
++) {
414 if (XHC_BIT_IS_SET (State
, mUsbPortChangeMap
[Index
].HwState
)) {
415 PortStatus
->PortChangeStatus
= (UINT16
) (PortStatus
->PortChangeStatus
| mUsbPortChangeMap
[Index
].UefiState
);
420 // Poll the root port status register to enable/disable corresponding device slot if there is a device attached/detached.
421 // For those devices behind hub, we get its attach/detach event by hooking Get_Port_Status request at control transfer for those hub.
423 ParentRouteChart
.Dword
= 0;
424 XhcPollPortStatusChange (Xhc
, ParentRouteChart
, PortNumber
, PortStatus
);
427 gBS
->RestoreTPL (OldTpl
);
433 Sets a feature for the specified root hub port.
435 @param This This EFI_USB2_HC_PROTOCOL instance.
436 @param PortNumber Root hub port to set.
437 @param PortFeature Feature to set.
439 @retval EFI_SUCCESS The feature specified by PortFeature was set.
440 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
441 @retval EFI_DEVICE_ERROR Can't read register.
446 XhcSetRootHubPortFeature (
447 IN EFI_USB2_HC_PROTOCOL
*This
,
449 IN EFI_USB_PORT_FEATURE PortFeature
452 USB_XHCI_INSTANCE
*Xhc
;
457 USB_DEV_ROUTE RouteChart
;
461 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
463 Xhc
= XHC_FROM_THIS (This
);
464 Status
= EFI_SUCCESS
;
466 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
468 if (PortNumber
>= TotalPort
) {
469 Status
= EFI_INVALID_PARAMETER
;
473 Offset
= (UINT32
) (XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
474 State
= XhcReadOpReg (Xhc
, Offset
);
477 // Mask off the port status change bits, these bits are
480 State
&= ~ (BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
482 switch (PortFeature
) {
483 case EfiUsbPortEnable
:
485 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
486 // A port may be disabled by software writing a '1' to this flag.
488 Status
= EFI_SUCCESS
;
491 case EfiUsbPortSuspend
:
492 State
|= XHC_PORTSC_LWS
;
493 XhcWriteOpReg (Xhc
, Offset
, State
);
494 State
&= ~XHC_PORTSC_PLS
;
496 XhcWriteOpReg (Xhc
, Offset
, State
);
499 case EfiUsbPortReset
:
500 DEBUG ((EFI_D_INFO
, "XhcUsbPortReset!\n"));
502 // Make sure Host Controller not halt before reset it
504 if (XhcIsHalt (Xhc
)) {
505 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
507 if (EFI_ERROR (Status
)) {
508 DEBUG ((EFI_D_INFO
, "XhcSetRootHubPortFeature :failed to start HC - %r\n", Status
));
513 RouteChart
.Route
.RouteString
= 0;
514 RouteChart
.Route
.RootPortNum
= PortNumber
+ 1;
515 RouteChart
.Route
.TierNum
= 1;
517 // If the port reset operation happens after the usb super speed device is enabled,
518 // The subsequent configuration, such as getting device descriptor, will fail.
519 // So here a workaround is introduced to skip the reset operation if the device is enabled.
521 SlotId
= XhcRouteStringToSlotId (Xhc
, RouteChart
);
524 // 4.3.1 Resetting a Root Hub Port
525 // 1) Write the PORTSC register with the Port Reset (PR) bit set to '1'.
527 State
|= XHC_PORTSC_RESET
;
528 XhcWriteOpReg (Xhc
, Offset
, State
);
529 XhcWaitOpRegBit(Xhc
, Offset
, XHC_PORTSC_PRC
, TRUE
, XHC_GENERIC_TIMEOUT
);
533 case EfiUsbPortPower
:
535 // Not supported, ignore the operation
537 Status
= EFI_SUCCESS
;
540 case EfiUsbPortOwner
:
542 // XHCI root hub port don't has the owner bit, ignore the operation
544 Status
= EFI_SUCCESS
;
548 Status
= EFI_INVALID_PARAMETER
;
552 DEBUG ((EFI_D_INFO
, "XhcSetRootHubPortFeature: status %r\n", Status
));
553 gBS
->RestoreTPL (OldTpl
);
560 Clears a feature for the specified root hub port.
562 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
563 @param PortNumber Specifies the root hub port whose feature is
564 requested to be cleared.
565 @param PortFeature Indicates the feature selector associated with the
566 feature clear request.
568 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
569 for the USB root hub port specified by PortNumber.
570 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
571 @retval EFI_DEVICE_ERROR Can't read register.
576 XhcClearRootHubPortFeature (
577 IN EFI_USB2_HC_PROTOCOL
*This
,
579 IN EFI_USB_PORT_FEATURE PortFeature
582 USB_XHCI_INSTANCE
*Xhc
;
589 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
591 Xhc
= XHC_FROM_THIS (This
);
592 Status
= EFI_SUCCESS
;
594 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
596 if (PortNumber
>= TotalPort
) {
597 Status
= EFI_INVALID_PARAMETER
;
601 Offset
= XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
);
604 // Mask off the port status change bits, these bits are
607 State
= XhcReadOpReg (Xhc
, Offset
);
608 State
&= ~ (BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
610 switch (PortFeature
) {
611 case EfiUsbPortEnable
:
613 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
614 // A port may be disabled by software writing a '1' to this flag.
616 State
|= XHC_PORTSC_PED
;
617 State
&= ~XHC_PORTSC_RESET
;
618 XhcWriteOpReg (Xhc
, Offset
, State
);
621 case EfiUsbPortSuspend
:
622 State
|= XHC_PORTSC_LWS
;
623 XhcWriteOpReg (Xhc
, Offset
, State
);
624 State
&= ~XHC_PORTSC_PLS
;
625 XhcWriteOpReg (Xhc
, Offset
, State
);
628 case EfiUsbPortReset
:
630 // PORTSC_RESET BIT(4) bit is RW1S attribute, which means Write-1-to-set status:
631 // Register bits indicate status when read, a clear bit may be set by
632 // writing a '1'. Writing a '0' to RW1S bits has no effect.
636 case EfiUsbPortOwner
:
638 // XHCI root hub port don't has the owner bit, ignore the operation
642 case EfiUsbPortConnectChange
:
644 // Clear connect status change
646 State
|= XHC_PORTSC_CSC
;
647 XhcWriteOpReg (Xhc
, Offset
, State
);
650 case EfiUsbPortEnableChange
:
652 // Clear enable status change
654 State
|= XHC_PORTSC_PEC
;
655 XhcWriteOpReg (Xhc
, Offset
, State
);
658 case EfiUsbPortOverCurrentChange
:
660 // Clear PortOverCurrent change
662 State
|= XHC_PORTSC_OCC
;
663 XhcWriteOpReg (Xhc
, Offset
, State
);
666 case EfiUsbPortResetChange
:
668 // Clear Port Reset change
670 State
|= XHC_PORTSC_PRC
;
671 XhcWriteOpReg (Xhc
, Offset
, State
);
674 case EfiUsbPortPower
:
675 case EfiUsbPortSuspendChange
:
677 // Not supported or not related operation
682 Status
= EFI_INVALID_PARAMETER
;
687 DEBUG ((EFI_D_INFO
, "XhcClearRootHubPortFeature: status %r\n", Status
));
688 gBS
->RestoreTPL (OldTpl
);
695 Submits control transfer to a target USB device.
697 @param This This EFI_USB2_HC_PROTOCOL instance.
698 @param DeviceAddress The target device address.
699 @param DeviceSpeed Target device speed.
700 @param MaximumPacketLength Maximum packet size the default control transfer
701 endpoint is capable of sending or receiving.
702 @param Request USB device request to send.
703 @param TransferDirection Specifies the data direction for the data stage
704 @param Data Data buffer to be transmitted or received from USB
706 @param DataLength The size (in bytes) of the data buffer.
707 @param Timeout Indicates the maximum timeout, in millisecond.
708 @param Translator Transaction translator to be used by this device.
709 @param TransferResult Return the result of this control transfer.
711 @retval EFI_SUCCESS Transfer was completed successfully.
712 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
713 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
714 @retval EFI_TIMEOUT Transfer failed due to timeout.
715 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
721 IN EFI_USB2_HC_PROTOCOL
*This
,
722 IN UINT8 DeviceAddress
,
723 IN UINT8 DeviceSpeed
,
724 IN UINTN MaximumPacketLength
,
725 IN EFI_USB_DEVICE_REQUEST
*Request
,
726 IN EFI_USB_DATA_DIRECTION TransferDirection
,
728 IN OUT UINTN
*DataLength
,
730 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
731 OUT UINT32
*TransferResult
734 USB_XHCI_INSTANCE
*Xhc
;
738 UINT8 DescriptorType
;
743 EFI_USB_HUB_DESCRIPTOR
*HubDesc
;
746 EFI_STATUS RecoveryStatus
;
748 EFI_USB_PORT_STATUS PortStatus
;
752 // Validate parameters
754 if ((Request
== NULL
) || (TransferResult
== NULL
)) {
755 return EFI_INVALID_PARAMETER
;
758 if ((TransferDirection
!= EfiUsbDataIn
) &&
759 (TransferDirection
!= EfiUsbDataOut
) &&
760 (TransferDirection
!= EfiUsbNoData
)) {
761 return EFI_INVALID_PARAMETER
;
764 if ((TransferDirection
== EfiUsbNoData
) &&
765 ((Data
!= NULL
) || (*DataLength
!= 0))) {
766 return EFI_INVALID_PARAMETER
;
769 if ((TransferDirection
!= EfiUsbNoData
) &&
770 ((Data
== NULL
) || (*DataLength
== 0))) {
771 return EFI_INVALID_PARAMETER
;
774 if ((MaximumPacketLength
!= 8) && (MaximumPacketLength
!= 16) &&
775 (MaximumPacketLength
!= 32) && (MaximumPacketLength
!= 64) &&
776 (MaximumPacketLength
!= 512)
778 return EFI_INVALID_PARAMETER
;
781 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) {
782 return EFI_INVALID_PARAMETER
;
785 if ((DeviceSpeed
== EFI_USB_SPEED_SUPER
) && (MaximumPacketLength
!= 512)) {
786 return EFI_INVALID_PARAMETER
;
789 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
791 Xhc
= XHC_FROM_THIS (This
);
793 Status
= EFI_DEVICE_ERROR
;
794 *TransferResult
= EFI_USB_ERR_SYSTEM
;
796 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
797 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: HC halted at entrance\n"));
802 // Check if the device is still enabled before every transaction.
804 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
810 // Hook the Set_Address request from UsbBus.
811 // According to XHCI 1.0 spec, the Set_Address request is replaced by XHCI's Address_Device cmd.
813 if ((Request
->Request
== USB_REQ_SET_ADDRESS
) &&
814 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
816 // Reset the BusDevAddr field of all disabled entries in UsbDevContext array firstly.
817 // This way is used to clean the history to avoid using wrong device address by XhcAsyncInterruptTransfer().
819 for (Index
= 0; Index
< 255; Index
++) {
820 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
&&
821 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0) &&
822 (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== (UINT8
)Request
->Value
)) {
823 Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
= 0;
827 // The actual device address has been assigned by XHCI during initializing the device slot.
828 // So we just need establish the mapping relationship between the device address requested from UsbBus
829 // and the actual device address assigned by XHCI. The the following invocations through EFI_USB2_HC_PROTOCOL interface
830 // can find out the actual device address by it.
832 Xhc
->UsbDevContext
[SlotId
].BusDevAddr
= (UINT8
)Request
->Value
;
833 Status
= EFI_SUCCESS
;
838 // If the port reset operation happens after the usb super speed device is enabled,
839 // The subsequent configuration, such as getting device descriptor, will fail.
840 // So here a workaround is introduced to skip the reset operation if the device is enabled.
842 if ((Request
->Request
== USB_REQ_SET_FEATURE
) &&
843 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
)) &&
844 (Request
->Value
== EfiUsbPortReset
)) {
845 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
846 Status
= EFI_SUCCESS
;
852 // Create a new URB, insert it into the asynchronous
853 // schedule list, then poll the execution status.
854 // Note that we encode the direction in address although default control
855 // endpoint is bidirectional. XhcCreateUrb expects this
856 // combination of Ep addr and its direction.
858 Endpoint
= (UINT8
) (0 | ((TransferDirection
== EfiUsbDataIn
) ? 0x80 : 0));
874 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: failed to create URB"));
875 Status
= EFI_OUT_OF_RESOURCES
;
879 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
882 // Get the status from URB. The result is updated in XhcCheckUrbResult
883 // which is called by XhcExecTransfer
885 *TransferResult
= Urb
->Result
;
886 *DataLength
= Urb
->Completed
;
888 if (*TransferResult
== EFI_USB_NOERROR
) {
889 Status
= EFI_SUCCESS
;
890 } else if (*TransferResult
== EFI_USB_ERR_STALL
) {
891 RecoveryStatus
= XhcRecoverHaltedEndpoint(Xhc
, Urb
);
892 ASSERT_EFI_ERROR (RecoveryStatus
);
893 Status
= EFI_DEVICE_ERROR
;
900 // Hook Get_Descriptor request from UsbBus as we need evaluate context and configure endpoint.
901 // Hook Get_Status request form UsbBus as we need trace device attach/detach event happened at hub.
902 // Hook Set_Config request from UsbBus as we need configure device endpoint.
904 if ((Request
->Request
== USB_REQ_GET_DESCRIPTOR
) &&
905 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
)) ||
906 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_DEVICE
))))) {
907 DescriptorType
= (UINT8
)(Request
->Value
>> 8);
908 if ((DescriptorType
== USB_DESC_TYPE_DEVICE
) && (*DataLength
== sizeof (EFI_USB_DEVICE_DESCRIPTOR
))) {
909 ASSERT (Data
!= NULL
);
911 // Store a copy of device scriptor as hub device need this info to configure endpoint.
913 CopyMem (&Xhc
->UsbDevContext
[SlotId
].DevDesc
, Data
, *DataLength
);
914 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.BcdUSB
== 0x0300) {
916 // If it's a usb3.0 device, then its max packet size is a 2^n.
918 MaxPacket0
= 1 << Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
920 MaxPacket0
= Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
922 Xhc
->UsbDevContext
[SlotId
].ConfDesc
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
* sizeof (EFI_USB_CONFIG_DESCRIPTOR
*));
923 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
924 Status
= XhcEvaluateContext (Xhc
, SlotId
, MaxPacket0
);
926 Status
= XhcEvaluateContext64 (Xhc
, SlotId
, MaxPacket0
);
928 ASSERT_EFI_ERROR (Status
);
929 } else if (DescriptorType
== USB_DESC_TYPE_CONFIG
) {
930 ASSERT (Data
!= NULL
);
931 if (*DataLength
== ((UINT16
*)Data
)[1]) {
933 // Get configuration value from request, Store the configuration descriptor for Configure_Endpoint cmd.
935 Index
= (UINT8
)Request
->Value
;
936 ASSERT (Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
);
937 Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
] = AllocateZeroPool(*DataLength
);
938 CopyMem (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
], Data
, *DataLength
);
940 } else if (((DescriptorType
== USB_DESC_TYPE_HUB
) ||
941 (DescriptorType
== USB_DESC_TYPE_HUB_SUPER_SPEED
)) && (*DataLength
> 2)) {
942 ASSERT (Data
!= NULL
);
943 HubDesc
= (EFI_USB_HUB_DESCRIPTOR
*)Data
;
944 ASSERT (HubDesc
->NumPorts
<= 15);
946 // The bit 5,6 of HubCharacter field of Hub Descriptor is TTT.
948 TTT
= (UINT8
)((HubDesc
->HubCharacter
& (BIT5
| BIT6
)) >> 5);
949 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.DeviceProtocol
== 2) {
951 // Don't support multi-TT feature for super speed hub now.
954 DEBUG ((EFI_D_ERROR
, "XHCI: Don't support multi-TT feature for Hub now. (force to disable MTT)\n"));
959 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
960 Status
= XhcConfigHubContext (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
962 Status
= XhcConfigHubContext64 (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
964 ASSERT_EFI_ERROR (Status
);
966 } else if ((Request
->Request
== USB_REQ_SET_CONFIG
) &&
967 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
969 // Hook Set_Config request from UsbBus as we need configure device endpoint.
971 for (Index
= 0; Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
; Index
++) {
972 if (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->ConfigurationValue
== (UINT8
)Request
->Value
) {
973 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
974 Status
= XhcSetConfigCmd (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
976 Status
= XhcSetConfigCmd64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
978 ASSERT_EFI_ERROR (Status
);
982 } else if ((Request
->Request
== USB_REQ_GET_STATUS
) &&
983 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
))) {
984 ASSERT (Data
!= NULL
);
986 // Hook Get_Status request from UsbBus to keep track of the port status change.
988 State
= *(UINT32
*)Data
;
989 PortStatus
.PortStatus
= 0;
990 PortStatus
.PortChangeStatus
= 0;
992 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
994 // For super speed hub, its bit10~12 presents the attached device speed.
996 if ((State
& XHC_PORTSC_PS
) >> 10 == 0) {
997 PortStatus
.PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
999 } else if (DeviceSpeed
== EFI_USB_SPEED_HIGH
) {
1001 // For high speed hub, its bit9~10 presents the attached device speed.
1003 if (XHC_BIT_IS_SET (State
, BIT9
)) {
1004 PortStatus
.PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
1005 } else if (XHC_BIT_IS_SET (State
, BIT10
)) {
1006 PortStatus
.PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
1013 // Convert the XHCI port/port change state to UEFI status
1015 MapSize
= sizeof (mUsbHubPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
1016 for (Index
= 0; Index
< MapSize
; Index
++) {
1017 if (XHC_BIT_IS_SET (State
, mUsbHubPortStateMap
[Index
].HwState
)) {
1018 PortStatus
.PortStatus
= (UINT16
) (PortStatus
.PortStatus
| mUsbHubPortStateMap
[Index
].UefiState
);
1022 MapSize
= sizeof (mUsbHubPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
1023 for (Index
= 0; Index
< MapSize
; Index
++) {
1024 if (XHC_BIT_IS_SET (State
, mUsbHubPortChangeMap
[Index
].HwState
)) {
1025 PortStatus
.PortChangeStatus
= (UINT16
) (PortStatus
.PortChangeStatus
| mUsbHubPortChangeMap
[Index
].UefiState
);
1029 XhcPollPortStatusChange (Xhc
, Xhc
->UsbDevContext
[SlotId
].RouteString
, (UINT8
)Request
->Index
, &PortStatus
);
1031 *(UINT32
*)Data
= *(UINT32
*)&PortStatus
;
1039 if (EFI_ERROR (Status
)) {
1040 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1043 gBS
->RestoreTPL (OldTpl
);
1050 Submits bulk transfer to a bulk endpoint of a USB device.
1052 @param This This EFI_USB2_HC_PROTOCOL instance.
1053 @param DeviceAddress Target device address.
1054 @param EndPointAddress Endpoint number and its direction in bit 7.
1055 @param DeviceSpeed Device speed, Low speed device doesn't support bulk
1057 @param MaximumPacketLength Maximum packet size the endpoint is capable of
1058 sending or receiving.
1059 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1060 @param Data Array of pointers to the buffers of data to transmit
1061 from or receive into.
1062 @param DataLength The lenght of the data buffer.
1063 @param DataToggle On input, the initial data toggle for the transfer;
1064 On output, it is updated to to next data toggle to
1065 use of the subsequent bulk transfer.
1066 @param Timeout Indicates the maximum time, in millisecond, which
1067 the transfer is allowed to complete.
1068 @param Translator A pointr to the transaction translator data.
1069 @param TransferResult A pointer to the detailed result information of the
1072 @retval EFI_SUCCESS The transfer was completed successfully.
1073 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1074 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1075 @retval EFI_TIMEOUT The transfer failed due to timeout.
1076 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1082 IN EFI_USB2_HC_PROTOCOL
*This
,
1083 IN UINT8 DeviceAddress
,
1084 IN UINT8 EndPointAddress
,
1085 IN UINT8 DeviceSpeed
,
1086 IN UINTN MaximumPacketLength
,
1087 IN UINT8 DataBuffersNumber
,
1088 IN OUT VOID
*Data
[EFI_USB_MAX_BULK_BUFFER_NUM
],
1089 IN OUT UINTN
*DataLength
,
1090 IN OUT UINT8
*DataToggle
,
1092 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1093 OUT UINT32
*TransferResult
1096 USB_XHCI_INSTANCE
*Xhc
;
1100 EFI_STATUS RecoveryStatus
;
1104 // Validate the parameters
1106 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1107 (Data
== NULL
) || (Data
[0] == NULL
) || (TransferResult
== NULL
)) {
1108 return EFI_INVALID_PARAMETER
;
1111 if ((*DataToggle
!= 0) && (*DataToggle
!= 1)) {
1112 return EFI_INVALID_PARAMETER
;
1115 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) ||
1116 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1117 ((EFI_USB_SPEED_HIGH
== DeviceSpeed
) && (MaximumPacketLength
> 512)) ||
1118 ((EFI_USB_SPEED_SUPER
== DeviceSpeed
) && (MaximumPacketLength
> 1024))) {
1119 return EFI_INVALID_PARAMETER
;
1122 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1124 Xhc
= XHC_FROM_THIS (This
);
1126 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1127 Status
= EFI_DEVICE_ERROR
;
1129 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1130 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: HC is halted\n"));
1135 // Check if the device is still enabled before every transaction.
1137 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1143 // Create a new URB, insert it into the asynchronous
1144 // schedule list, then poll the execution status.
1146 Urb
= XhcCreateUrb (
1151 MaximumPacketLength
,
1161 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: failed to create URB\n"));
1162 Status
= EFI_OUT_OF_RESOURCES
;
1166 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
1168 *TransferResult
= Urb
->Result
;
1169 *DataLength
= Urb
->Completed
;
1171 if (*TransferResult
== EFI_USB_NOERROR
) {
1172 Status
= EFI_SUCCESS
;
1173 } else if (*TransferResult
== EFI_USB_ERR_STALL
) {
1174 RecoveryStatus
= XhcRecoverHaltedEndpoint(Xhc
, Urb
);
1175 ASSERT_EFI_ERROR (RecoveryStatus
);
1176 Status
= EFI_DEVICE_ERROR
;
1183 if (EFI_ERROR (Status
)) {
1184 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1186 gBS
->RestoreTPL (OldTpl
);
1192 Submits an asynchronous interrupt transfer to an
1193 interrupt endpoint of a USB device.
1195 @param This This EFI_USB2_HC_PROTOCOL instance.
1196 @param DeviceAddress Target device address.
1197 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1198 @param DeviceSpeed Indicates device speed.
1199 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1200 @param IsNewTransfer If TRUE, to submit an new asynchronous interrupt
1201 transfer If FALSE, to remove the specified
1202 asynchronous interrupt.
1203 @param DataToggle On input, the initial data toggle to use; on output,
1204 it is updated to indicate the next data toggle.
1205 @param PollingInterval The he interval, in milliseconds, that the transfer
1207 @param DataLength The length of data to receive at the rate specified
1209 @param Translator Transaction translator to use.
1210 @param CallBackFunction Function to call at the rate specified by
1212 @param Context Context to CallBackFunction.
1214 @retval EFI_SUCCESS The request has been successfully submitted or canceled.
1215 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1216 @retval EFI_OUT_OF_RESOURCES The request failed due to a lack of resources.
1217 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1222 XhcAsyncInterruptTransfer (
1223 IN EFI_USB2_HC_PROTOCOL
*This
,
1224 IN UINT8 DeviceAddress
,
1225 IN UINT8 EndPointAddress
,
1226 IN UINT8 DeviceSpeed
,
1227 IN UINTN MaximumPacketLength
,
1228 IN BOOLEAN IsNewTransfer
,
1229 IN OUT UINT8
*DataToggle
,
1230 IN UINTN PollingInterval
,
1231 IN UINTN DataLength
,
1232 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1233 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction
,
1234 IN VOID
*Context OPTIONAL
1237 USB_XHCI_INSTANCE
*Xhc
;
1246 // Validate parameters
1248 if (!XHCI_IS_DATAIN (EndPointAddress
)) {
1249 return EFI_INVALID_PARAMETER
;
1252 if (IsNewTransfer
) {
1253 if (DataLength
== 0) {
1254 return EFI_INVALID_PARAMETER
;
1257 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1258 return EFI_INVALID_PARAMETER
;
1261 if ((PollingInterval
> 255) || (PollingInterval
< 1)) {
1262 return EFI_INVALID_PARAMETER
;
1266 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1268 Xhc
= XHC_FROM_THIS (This
);
1271 // Delete Async interrupt transfer request.
1273 if (!IsNewTransfer
) {
1275 // The delete request may happen after device is detached.
1277 for (Index
= 0; Index
< 255; Index
++) {
1278 if (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== DeviceAddress
) {
1284 Status
= EFI_INVALID_PARAMETER
;
1288 Status
= XhciDelAsyncIntTransfer (Xhc
, DeviceAddress
, EndPointAddress
);
1289 DEBUG ((EFI_D_INFO
, "XhcAsyncInterruptTransfer: remove old transfer for addr %d, Status = %r\n", DeviceAddress
, Status
));
1293 Status
= EFI_SUCCESS
;
1295 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1296 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: HC is halt\n"));
1297 Status
= EFI_DEVICE_ERROR
;
1302 // Check if the device is still enabled before every transaction.
1304 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1309 Data
= AllocateZeroPool (DataLength
);
1312 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: failed to allocate buffer\n"));
1313 Status
= EFI_OUT_OF_RESOURCES
;
1317 Urb
= XhcCreateUrb (
1322 MaximumPacketLength
,
1323 XHC_INT_TRANSFER_ASYNC
,
1332 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: failed to create URB\n"));
1334 Status
= EFI_OUT_OF_RESOURCES
;
1338 InsertHeadList (&Xhc
->AsyncIntTransfers
, &Urb
->UrbList
);
1340 // Ring the doorbell
1342 Status
= RingIntTransferDoorBell (Xhc
, Urb
);
1345 gBS
->RestoreTPL (OldTpl
);
1352 Submits synchronous interrupt transfer to an interrupt endpoint
1355 @param This This EFI_USB2_HC_PROTOCOL instance.
1356 @param DeviceAddress Target device address.
1357 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1358 @param DeviceSpeed Indicates device speed.
1359 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1360 of sending or receiving.
1361 @param Data Buffer of data that will be transmitted to USB
1362 device or received from USB device.
1363 @param DataLength On input, the size, in bytes, of the data buffer; On
1364 output, the number of bytes transferred.
1365 @param DataToggle On input, the initial data toggle to use; on output,
1366 it is updated to indicate the next data toggle.
1367 @param Timeout Maximum time, in second, to complete.
1368 @param Translator Transaction translator to use.
1369 @param TransferResult Variable to receive the transfer result.
1371 @return EFI_SUCCESS The transfer was completed successfully.
1372 @return EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1373 @return EFI_INVALID_PARAMETER Some parameters are invalid.
1374 @return EFI_TIMEOUT The transfer failed due to timeout.
1375 @return EFI_DEVICE_ERROR The failed due to host controller or device error
1380 XhcSyncInterruptTransfer (
1381 IN EFI_USB2_HC_PROTOCOL
*This
,
1382 IN UINT8 DeviceAddress
,
1383 IN UINT8 EndPointAddress
,
1384 IN UINT8 DeviceSpeed
,
1385 IN UINTN MaximumPacketLength
,
1387 IN OUT UINTN
*DataLength
,
1388 IN OUT UINT8
*DataToggle
,
1390 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1391 OUT UINT32
*TransferResult
1394 USB_XHCI_INSTANCE
*Xhc
;
1398 EFI_STATUS RecoveryStatus
;
1402 // Validates parameters
1404 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1405 (Data
== NULL
) || (TransferResult
== NULL
)) {
1406 return EFI_INVALID_PARAMETER
;
1409 if (!XHCI_IS_DATAIN (EndPointAddress
)) {
1410 return EFI_INVALID_PARAMETER
;
1413 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1414 return EFI_INVALID_PARAMETER
;
1417 if (((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) ||
1418 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1419 ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) && (MaximumPacketLength
> 3072))) {
1420 return EFI_INVALID_PARAMETER
;
1423 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1425 Xhc
= XHC_FROM_THIS (This
);
1427 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1428 Status
= EFI_DEVICE_ERROR
;
1430 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1431 DEBUG ((EFI_D_ERROR
, "EhcSyncInterruptTransfer: HC is halt\n"));
1436 // Check if the device is still enabled before every transaction.
1438 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1443 Urb
= XhcCreateUrb (
1448 MaximumPacketLength
,
1449 XHC_INT_TRANSFER_SYNC
,
1458 DEBUG ((EFI_D_ERROR
, "XhcSyncInterruptTransfer: failed to create URB\n"));
1459 Status
= EFI_OUT_OF_RESOURCES
;
1463 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
1465 *TransferResult
= Urb
->Result
;
1466 *DataLength
= Urb
->Completed
;
1468 if (*TransferResult
== EFI_USB_NOERROR
) {
1469 Status
= EFI_SUCCESS
;
1470 } else if (*TransferResult
== EFI_USB_ERR_STALL
) {
1471 RecoveryStatus
= XhcRecoverHaltedEndpoint(Xhc
, Urb
);
1472 ASSERT_EFI_ERROR (RecoveryStatus
);
1473 Status
= EFI_DEVICE_ERROR
;
1479 if (EFI_ERROR (Status
)) {
1480 DEBUG ((EFI_D_ERROR
, "XhcSyncInterruptTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1482 gBS
->RestoreTPL (OldTpl
);
1489 Submits isochronous transfer to a target USB device.
1491 @param This This EFI_USB2_HC_PROTOCOL instance.
1492 @param DeviceAddress Target device address.
1493 @param EndPointAddress End point address with its direction.
1494 @param DeviceSpeed Device speed, Low speed device doesn't support this
1496 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1497 sending or receiving.
1498 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1499 @param Data Array of pointers to the buffers of data that will
1500 be transmitted to USB device or received from USB
1502 @param DataLength The size, in bytes, of the data buffer.
1503 @param Translator Transaction translator to use.
1504 @param TransferResult Variable to receive the transfer result.
1506 @return EFI_UNSUPPORTED Isochronous transfer is unsupported.
1511 XhcIsochronousTransfer (
1512 IN EFI_USB2_HC_PROTOCOL
*This
,
1513 IN UINT8 DeviceAddress
,
1514 IN UINT8 EndPointAddress
,
1515 IN UINT8 DeviceSpeed
,
1516 IN UINTN MaximumPacketLength
,
1517 IN UINT8 DataBuffersNumber
,
1518 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1519 IN UINTN DataLength
,
1520 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1521 OUT UINT32
*TransferResult
1524 return EFI_UNSUPPORTED
;
1529 Submits Async isochronous transfer to a target USB device.
1531 @param This This EFI_USB2_HC_PROTOCOL instance.
1532 @param DeviceAddress Target device address.
1533 @param EndPointAddress End point address with its direction.
1534 @param DeviceSpeed Device speed, Low speed device doesn't support this
1536 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1537 sending or receiving.
1538 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1539 @param Data Array of pointers to the buffers of data that will
1540 be transmitted to USB device or received from USB
1542 @param DataLength The size, in bytes, of the data buffer.
1543 @param Translator Transaction translator to use.
1544 @param IsochronousCallBack Function to be called when the transfer complete.
1545 @param Context Context passed to the call back function as
1548 @return EFI_UNSUPPORTED Isochronous transfer isn't supported.
1553 XhcAsyncIsochronousTransfer (
1554 IN EFI_USB2_HC_PROTOCOL
*This
,
1555 IN UINT8 DeviceAddress
,
1556 IN UINT8 EndPointAddress
,
1557 IN UINT8 DeviceSpeed
,
1558 IN UINTN MaximumPacketLength
,
1559 IN UINT8 DataBuffersNumber
,
1560 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1561 IN UINTN DataLength
,
1562 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1563 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
1567 return EFI_UNSUPPORTED
;
1571 Entry point for EFI drivers.
1573 @param ImageHandle EFI_HANDLE.
1574 @param SystemTable EFI_SYSTEM_TABLE.
1576 @retval EFI_SUCCESS Success.
1577 @retval Others Fail.
1582 XhcDriverEntryPoint (
1583 IN EFI_HANDLE ImageHandle
,
1584 IN EFI_SYSTEM_TABLE
*SystemTable
1587 return EfiLibInstallDriverBindingComponentName2 (
1590 &gXhciDriverBinding
,
1592 &gXhciComponentName
,
1593 &gXhciComponentName2
1599 Test to see if this driver supports ControllerHandle. Any
1600 ControllerHandle that has Usb2HcProtocol installed will
1603 @param This Protocol instance pointer.
1604 @param Controller Handle of device to test.
1605 @param RemainingDevicePath Not used.
1607 @return EFI_SUCCESS This driver supports this device.
1608 @return EFI_UNSUPPORTED This driver does not support this device.
1613 XhcDriverBindingSupported (
1614 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1615 IN EFI_HANDLE Controller
,
1616 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1620 EFI_PCI_IO_PROTOCOL
*PciIo
;
1621 USB_CLASSC UsbClassCReg
;
1624 // Test whether there is PCI IO Protocol attached on the controller handle.
1626 Status
= gBS
->OpenProtocol (
1628 &gEfiPciIoProtocolGuid
,
1630 This
->DriverBindingHandle
,
1632 EFI_OPEN_PROTOCOL_BY_DRIVER
1635 if (EFI_ERROR (Status
)) {
1636 return EFI_UNSUPPORTED
;
1639 Status
= PciIo
->Pci
.Read (
1642 PCI_CLASSCODE_OFFSET
,
1643 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1647 if (EFI_ERROR (Status
)) {
1648 Status
= EFI_UNSUPPORTED
;
1653 // Test whether the controller belongs to Xhci type
1655 if ((UsbClassCReg
.BaseCode
!= PCI_CLASS_SERIAL
) ||
1656 (UsbClassCReg
.SubClassCode
!= PCI_CLASS_SERIAL_USB
) ||
1657 (UsbClassCReg
.ProgInterface
!= PCI_IF_XHCI
)) {
1658 Status
= EFI_UNSUPPORTED
;
1662 gBS
->CloseProtocol (
1664 &gEfiPciIoProtocolGuid
,
1665 This
->DriverBindingHandle
,
1673 Create and initialize a USB_XHCI_INSTANCE structure.
1675 @param PciIo The PciIo on this device.
1676 @param OriginalPciAttributes Original PCI attributes.
1678 @return The allocated and initialized USB_XHCI_INSTANCE structure if created,
1684 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1685 IN UINT64 OriginalPciAttributes
1688 USB_XHCI_INSTANCE
*Xhc
;
1693 Xhc
= AllocateZeroPool (sizeof (USB_XHCI_INSTANCE
));
1700 // Initialize private data structure
1702 Xhc
->Signature
= XHCI_INSTANCE_SIG
;
1704 Xhc
->OriginalPciAttributes
= OriginalPciAttributes
;
1705 CopyMem (&Xhc
->Usb2Hc
, &gXhciUsb2HcTemplate
, sizeof (EFI_USB2_HC_PROTOCOL
));
1707 InitializeListHead (&Xhc
->AsyncIntTransfers
);
1710 // Be caution that the Offset passed to XhcReadCapReg() should be Dword align
1712 Xhc
->CapLength
= XhcReadCapReg8 (Xhc
, XHC_CAPLENGTH_OFFSET
);
1713 Xhc
->HcSParams1
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS1_OFFSET
);
1714 Xhc
->HcSParams2
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS2_OFFSET
);
1715 Xhc
->HcCParams
.Dword
= XhcReadCapReg (Xhc
, XHC_HCCPARAMS_OFFSET
);
1716 Xhc
->DBOff
= XhcReadCapReg (Xhc
, XHC_DBOFF_OFFSET
);
1717 Xhc
->RTSOff
= XhcReadCapReg (Xhc
, XHC_RTSOFF_OFFSET
);
1720 // This PageSize field defines the page size supported by the xHC implementation.
1721 // This xHC supports a page size of 2^(n+12) if bit n is Set. For example,
1722 // if bit 0 is Set, the xHC supports 4k byte page sizes.
1724 PageSize
= XhcReadOpReg(Xhc
, XHC_PAGESIZE_OFFSET
) & XHC_PAGESIZE_MASK
;
1725 Xhc
->PageSize
= 1 << (HighBitSet32(PageSize
) + 12);
1727 ExtCapReg
= (UINT16
) (Xhc
->HcCParams
.Data
.ExtCapReg
);
1728 Xhc
->ExtCapRegBase
= ExtCapReg
<< 2;
1729 Xhc
->UsbLegSupOffset
= XhcGetLegSupCapAddr (Xhc
);
1731 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: Capability length 0x%x\n", Xhc
->CapLength
));
1732 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcSParams1 0x%x\n", Xhc
->HcSParams1
));
1733 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcSParams2 0x%x\n", Xhc
->HcSParams2
));
1734 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcCParams 0x%x\n", Xhc
->HcCParams
));
1735 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: DBOff 0x%x\n", Xhc
->DBOff
));
1736 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: RTSOff 0x%x\n", Xhc
->RTSOff
));
1737 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: UsbLegSupOffset 0x%x\n", Xhc
->UsbLegSupOffset
));
1740 // Create AsyncRequest Polling Timer
1742 Status
= gBS
->CreateEvent (
1743 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1745 XhcMonitorAsyncRequests
,
1750 if (EFI_ERROR (Status
)) {
1762 One notified function to stop the Host Controller when gBS->ExitBootServices() called.
1764 @param Event Pointer to this event
1765 @param Context Event hanlder private data
1770 XhcExitBootService (
1776 USB_XHCI_INSTANCE
*Xhc
;
1777 EFI_PCI_IO_PROTOCOL
*PciIo
;
1779 Xhc
= (USB_XHCI_INSTANCE
*) Context
;
1783 // Stop AsyncRequest Polling timer then stop the XHCI driver
1784 // and uninstall the XHCI protocl.
1786 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
1787 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
1789 if (Xhc
->PollTimer
!= NULL
) {
1790 gBS
->CloseEvent (Xhc
->PollTimer
);
1794 // Restore original PCI attributes
1798 EfiPciIoAttributeOperationSet
,
1799 Xhc
->OriginalPciAttributes
,
1803 XhcClearBiosOwnership (Xhc
);
1807 Starting the Usb XHCI Driver.
1809 @param This Protocol instance pointer.
1810 @param Controller Handle of device to test.
1811 @param RemainingDevicePath Not used.
1813 @return EFI_SUCCESS supports this device.
1814 @return EFI_UNSUPPORTED do not support this device.
1815 @return EFI_DEVICE_ERROR cannot be started due to device Error.
1816 @return EFI_OUT_OF_RESOURCES cannot allocate resources.
1821 XhcDriverBindingStart (
1822 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1823 IN EFI_HANDLE Controller
,
1824 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1828 EFI_PCI_IO_PROTOCOL
*PciIo
;
1830 UINT64 OriginalPciAttributes
;
1831 BOOLEAN PciAttributesSaved
;
1832 USB_XHCI_INSTANCE
*Xhc
;
1835 // Open the PciIo Protocol, then enable the USB host controller
1837 Status
= gBS
->OpenProtocol (
1839 &gEfiPciIoProtocolGuid
,
1841 This
->DriverBindingHandle
,
1843 EFI_OPEN_PROTOCOL_BY_DRIVER
1846 if (EFI_ERROR (Status
)) {
1850 PciAttributesSaved
= FALSE
;
1852 // Save original PCI attributes
1854 Status
= PciIo
->Attributes (
1856 EfiPciIoAttributeOperationGet
,
1858 &OriginalPciAttributes
1861 if (EFI_ERROR (Status
)) {
1864 PciAttributesSaved
= TRUE
;
1866 Status
= PciIo
->Attributes (
1868 EfiPciIoAttributeOperationSupported
,
1872 if (!EFI_ERROR (Status
)) {
1873 Supports
&= EFI_PCI_DEVICE_ENABLE
;
1874 Status
= PciIo
->Attributes (
1876 EfiPciIoAttributeOperationEnable
,
1882 if (EFI_ERROR (Status
)) {
1883 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to enable controller\n"));
1888 // Create then install USB2_HC_PROTOCOL
1890 Xhc
= XhcCreateUsbHc (PciIo
, OriginalPciAttributes
);
1893 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to create USB2_HC\n"));
1894 return EFI_OUT_OF_RESOURCES
;
1897 XhcSetBiosOwnership (Xhc
);
1899 XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
1900 ASSERT (XhcIsHalt (Xhc
));
1903 // After Chip Hardware Reset wait until the Controller Not Ready (CNR) flag
1904 // in the USBSTS is '0' before writing any xHC Operational or Runtime registers.
1906 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
1909 // Initialize the schedule
1914 // Start the Host Controller
1916 XhcRunHC(Xhc
, XHC_GENERIC_TIMEOUT
);
1919 // Start the asynchronous interrupt monitor
1921 Status
= gBS
->SetTimer (Xhc
->PollTimer
, TimerPeriodic
, XHC_ASYNC_TIMER_INTERVAL
);
1922 if (EFI_ERROR (Status
)) {
1923 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to start async interrupt monitor\n"));
1924 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
1929 // Create event to stop the HC when exit boot service.
1931 Status
= gBS
->CreateEventEx (
1936 &gEfiEventExitBootServicesGuid
,
1937 &Xhc
->ExitBootServiceEvent
1939 if (EFI_ERROR (Status
)) {
1944 // Install the component name protocol, don't fail the start
1945 // because of something for display.
1949 gXhciComponentName
.SupportedLanguages
,
1950 &Xhc
->ControllerNameTable
,
1951 L
"eXtensible Host Controller (USB 3.0)",
1956 gXhciComponentName2
.SupportedLanguages
,
1957 &Xhc
->ControllerNameTable
,
1958 L
"eXtensible Host Controller (USB 3.0)",
1962 Status
= gBS
->InstallProtocolInterface (
1964 &gEfiUsb2HcProtocolGuid
,
1965 EFI_NATIVE_INTERFACE
,
1968 if (EFI_ERROR (Status
)) {
1969 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to install USB2_HC Protocol\n"));
1973 DEBUG ((EFI_D_INFO
, "XhcDriverBindingStart: XHCI started for controller @ %x\n", Controller
));
1977 gBS
->CloseEvent (Xhc
->PollTimer
);
1982 if (PciAttributesSaved
) {
1984 // Restore original PCI attributes
1988 EfiPciIoAttributeOperationSet
,
1989 OriginalPciAttributes
,
1994 gBS
->CloseProtocol (
1996 &gEfiPciIoProtocolGuid
,
1997 This
->DriverBindingHandle
,
2006 Stop this driver on ControllerHandle. Support stoping any child handles
2007 created by this driver.
2009 @param This Protocol instance pointer.
2010 @param Controller Handle of device to stop driver on.
2011 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
2012 @param ChildHandleBuffer List of handles for the children we need to stop.
2014 @return EFI_SUCCESS Success.
2015 @return EFI_DEVICE_ERROR Fail.
2020 XhcDriverBindingStop (
2021 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
2022 IN EFI_HANDLE Controller
,
2023 IN UINTN NumberOfChildren
,
2024 IN EFI_HANDLE
*ChildHandleBuffer
2028 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
2029 EFI_PCI_IO_PROTOCOL
*PciIo
;
2030 USB_XHCI_INSTANCE
*Xhc
;
2034 // Test whether the Controller handler passed in is a valid
2035 // Usb controller handle that should be supported, if not,
2036 // return the error status directly
2038 Status
= gBS
->OpenProtocol (
2040 &gEfiUsb2HcProtocolGuid
,
2042 This
->DriverBindingHandle
,
2044 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2047 if (EFI_ERROR (Status
)) {
2051 Xhc
= XHC_FROM_THIS (Usb2Hc
);
2055 // Stop AsyncRequest Polling timer then stop the XHCI driver
2056 // and uninstall the XHCI protocl.
2058 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
2061 // Disable the device slots occupied by these devices on its downstream ports.
2062 // Entry 0 is reserved.
2064 for (Index
= 0; Index
< 255; Index
++) {
2065 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
||
2066 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0)) {
2069 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
2070 XhcDisableSlotCmd (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2072 XhcDisableSlotCmd64 (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2076 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2077 XhcClearBiosOwnership (Xhc
);
2079 Status
= gBS
->UninstallProtocolInterface (
2081 &gEfiUsb2HcProtocolGuid
,
2085 if (EFI_ERROR (Status
)) {
2089 if (Xhc
->PollTimer
!= NULL
) {
2090 gBS
->CloseEvent (Xhc
->PollTimer
);
2093 if (Xhc
->ExitBootServiceEvent
!= NULL
) {
2094 gBS
->CloseEvent (Xhc
->ExitBootServiceEvent
);
2097 XhciDelAllAsyncIntTransfers (Xhc
);
2100 if (Xhc
->ControllerNameTable
) {
2101 FreeUnicodeStringTable (Xhc
->ControllerNameTable
);
2105 // Restore original PCI attributes
2109 EfiPciIoAttributeOperationSet
,
2110 Xhc
->OriginalPciAttributes
,
2114 gBS
->CloseProtocol (
2116 &gEfiPciIoProtocolGuid
,
2117 This
->DriverBindingHandle
,