2 The XHCI controller driver.
4 Copyright (c) 2011 - 2013, 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 Xhc
= XHC_FROM_THIS (This
);
148 if (Xhc
->DevicePath
!= NULL
) {
150 // Report Status Code to indicate reset happens
152 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
154 (EFI_IO_BUS_USB
| EFI_IOB_PC_RESET
),
159 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
161 switch (Attributes
) {
162 case EFI_USB_HC_RESET_GLOBAL
:
164 // Flow through, same behavior as Host Controller Reset
166 case EFI_USB_HC_RESET_HOST_CONTROLLER
:
168 // Host Controller must be Halt when Reset it
170 if (!XhcIsHalt (Xhc
)) {
171 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
173 if (EFI_ERROR (Status
)) {
174 Status
= EFI_DEVICE_ERROR
;
179 Status
= XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
180 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
182 if (EFI_ERROR (Status
)) {
186 // Clean up the asynchronous transfers, currently only
187 // interrupt supports asynchronous operation.
189 XhciDelAllAsyncIntTransfers (Xhc
);
195 case EFI_USB_HC_RESET_GLOBAL_WITH_DEBUG
:
196 case EFI_USB_HC_RESET_HOST_WITH_DEBUG
:
197 Status
= EFI_UNSUPPORTED
;
201 Status
= EFI_INVALID_PARAMETER
;
205 DEBUG ((EFI_D_INFO
, "XhcReset: status %r\n", Status
));
206 gBS
->RestoreTPL (OldTpl
);
213 Retrieve the current state of the USB host controller.
215 @param This This EFI_USB2_HC_PROTOCOL instance.
216 @param State Variable to return the current host controller
219 @retval EFI_SUCCESS Host controller state was returned in State.
220 @retval EFI_INVALID_PARAMETER State is NULL.
221 @retval EFI_DEVICE_ERROR An error was encountered while attempting to
222 retrieve the host controller's current state.
228 IN EFI_USB2_HC_PROTOCOL
*This
,
229 OUT EFI_USB_HC_STATE
*State
232 USB_XHCI_INSTANCE
*Xhc
;
236 return EFI_INVALID_PARAMETER
;
239 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
241 Xhc
= XHC_FROM_THIS (This
);
243 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
244 *State
= EfiUsbHcStateHalt
;
246 *State
= EfiUsbHcStateOperational
;
249 DEBUG ((EFI_D_INFO
, "XhcGetState: current state %d\n", *State
));
250 gBS
->RestoreTPL (OldTpl
);
256 Sets the USB host controller to a specific state.
258 @param This This EFI_USB2_HC_PROTOCOL instance.
259 @param State The state of the host controller that will be set.
261 @retval EFI_SUCCESS The USB host controller was successfully placed
262 in the state specified by State.
263 @retval EFI_INVALID_PARAMETER State is invalid.
264 @retval EFI_DEVICE_ERROR Failed to set the state due to device error.
270 IN EFI_USB2_HC_PROTOCOL
*This
,
271 IN EFI_USB_HC_STATE State
274 USB_XHCI_INSTANCE
*Xhc
;
276 EFI_USB_HC_STATE CurState
;
279 Status
= XhcGetState (This
, &CurState
);
281 if (EFI_ERROR (Status
)) {
282 return EFI_DEVICE_ERROR
;
285 if (CurState
== State
) {
289 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
291 Xhc
= XHC_FROM_THIS (This
);
294 case EfiUsbHcStateHalt
:
295 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
298 case EfiUsbHcStateOperational
:
299 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HSE
)) {
300 Status
= EFI_DEVICE_ERROR
;
305 // Software must not write a one to this field unless the host controller
306 // is in the Halted state. Doing so will yield undefined results.
307 // refers to Spec[XHCI1.0-2.3.1]
309 if (!XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
310 Status
= EFI_DEVICE_ERROR
;
314 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
317 case EfiUsbHcStateSuspend
:
318 Status
= EFI_UNSUPPORTED
;
322 Status
= EFI_INVALID_PARAMETER
;
325 DEBUG ((EFI_D_INFO
, "XhcSetState: status %r\n", Status
));
326 gBS
->RestoreTPL (OldTpl
);
332 Retrieves the current status of a USB root hub port.
334 @param This This EFI_USB2_HC_PROTOCOL instance.
335 @param PortNumber The root hub port to retrieve the state from.
336 This value is zero-based.
337 @param PortStatus Variable to receive the port state.
339 @retval EFI_SUCCESS The status of the USB root hub port specified.
340 by PortNumber was returned in PortStatus.
341 @retval EFI_INVALID_PARAMETER PortNumber is invalid.
342 @retval EFI_DEVICE_ERROR Can't read register.
347 XhcGetRootHubPortStatus (
348 IN EFI_USB2_HC_PROTOCOL
*This
,
350 OUT EFI_USB_PORT_STATUS
*PortStatus
353 USB_XHCI_INSTANCE
*Xhc
;
360 USB_DEV_ROUTE ParentRouteChart
;
363 if (PortStatus
== NULL
) {
364 return EFI_INVALID_PARAMETER
;
367 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
369 Xhc
= XHC_FROM_THIS (This
);
370 Status
= EFI_SUCCESS
;
372 TotalPort
= Xhc
->HcSParams1
.Data
.MaxPorts
;
374 if (PortNumber
>= TotalPort
) {
375 Status
= EFI_INVALID_PARAMETER
;
379 Offset
= (UINT32
) (XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
380 PortStatus
->PortStatus
= 0;
381 PortStatus
->PortChangeStatus
= 0;
383 State
= XhcReadOpReg (Xhc
, Offset
);
386 // According to XHCI 1.0 spec, bit 10~13 of the root port status register identifies the speed of the attached device.
388 switch ((State
& XHC_PORTSC_PS
) >> 10) {
390 PortStatus
->PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
394 PortStatus
->PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
398 PortStatus
->PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
406 // Convert the XHCI port/port change state to UEFI status
408 MapSize
= sizeof (mUsbPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
410 for (Index
= 0; Index
< MapSize
; Index
++) {
411 if (XHC_BIT_IS_SET (State
, mUsbPortStateMap
[Index
].HwState
)) {
412 PortStatus
->PortStatus
= (UINT16
) (PortStatus
->PortStatus
| mUsbPortStateMap
[Index
].UefiState
);
416 // Bit5~8 reflects its current link state.
418 if ((State
& XHC_PORTSC_PLS
) >> 5 == 3) {
419 PortStatus
->PortStatus
|= USB_PORT_STAT_SUSPEND
;
422 MapSize
= sizeof (mUsbPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
424 for (Index
= 0; Index
< MapSize
; Index
++) {
425 if (XHC_BIT_IS_SET (State
, mUsbPortChangeMap
[Index
].HwState
)) {
426 PortStatus
->PortChangeStatus
= (UINT16
) (PortStatus
->PortChangeStatus
| mUsbPortChangeMap
[Index
].UefiState
);
431 // Poll the root port status register to enable/disable corresponding device slot if there is a device attached/detached.
432 // For those devices behind hub, we get its attach/detach event by hooking Get_Port_Status request at control transfer for those hub.
434 ParentRouteChart
.Dword
= 0;
435 XhcPollPortStatusChange (Xhc
, ParentRouteChart
, PortNumber
, PortStatus
);
438 gBS
->RestoreTPL (OldTpl
);
444 Sets a feature for the specified root hub port.
446 @param This This EFI_USB2_HC_PROTOCOL instance.
447 @param PortNumber Root hub port to set.
448 @param PortFeature Feature to set.
450 @retval EFI_SUCCESS The feature specified by PortFeature was set.
451 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
452 @retval EFI_DEVICE_ERROR Can't read register.
457 XhcSetRootHubPortFeature (
458 IN EFI_USB2_HC_PROTOCOL
*This
,
460 IN EFI_USB_PORT_FEATURE PortFeature
463 USB_XHCI_INSTANCE
*Xhc
;
468 USB_DEV_ROUTE RouteChart
;
472 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
474 Xhc
= XHC_FROM_THIS (This
);
475 Status
= EFI_SUCCESS
;
477 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
479 if (PortNumber
>= TotalPort
) {
480 Status
= EFI_INVALID_PARAMETER
;
484 Offset
= (UINT32
) (XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
485 State
= XhcReadOpReg (Xhc
, Offset
);
488 // Mask off the port status change bits, these bits are
491 State
&= ~ (BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
493 switch (PortFeature
) {
494 case EfiUsbPortEnable
:
496 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
497 // A port may be disabled by software writing a '1' to this flag.
499 Status
= EFI_SUCCESS
;
502 case EfiUsbPortSuspend
:
503 State
|= XHC_PORTSC_LWS
;
504 XhcWriteOpReg (Xhc
, Offset
, State
);
505 State
&= ~XHC_PORTSC_PLS
;
507 XhcWriteOpReg (Xhc
, Offset
, State
);
510 case EfiUsbPortReset
:
511 DEBUG ((EFI_D_INFO
, "XhcUsbPortReset!\n"));
513 // Make sure Host Controller not halt before reset it
515 if (XhcIsHalt (Xhc
)) {
516 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
518 if (EFI_ERROR (Status
)) {
519 DEBUG ((EFI_D_INFO
, "XhcSetRootHubPortFeature :failed to start HC - %r\n", Status
));
524 RouteChart
.Route
.RouteString
= 0;
525 RouteChart
.Route
.RootPortNum
= PortNumber
+ 1;
526 RouteChart
.Route
.TierNum
= 1;
528 // If the port reset operation happens after the usb super speed device is enabled,
529 // The subsequent configuration, such as getting device descriptor, will fail.
530 // So here a workaround is introduced to skip the reset operation if the device is enabled.
532 SlotId
= XhcRouteStringToSlotId (Xhc
, RouteChart
);
535 // 4.3.1 Resetting a Root Hub Port
536 // 1) Write the PORTSC register with the Port Reset (PR) bit set to '1'.
538 State
|= XHC_PORTSC_RESET
;
539 XhcWriteOpReg (Xhc
, Offset
, State
);
540 XhcWaitOpRegBit(Xhc
, Offset
, XHC_PORTSC_PRC
, TRUE
, XHC_GENERIC_TIMEOUT
);
544 case EfiUsbPortPower
:
546 // Not supported, ignore the operation
548 Status
= EFI_SUCCESS
;
551 case EfiUsbPortOwner
:
553 // XHCI root hub port don't has the owner bit, ignore the operation
555 Status
= EFI_SUCCESS
;
559 Status
= EFI_INVALID_PARAMETER
;
563 DEBUG ((EFI_D_INFO
, "XhcSetRootHubPortFeature: status %r\n", Status
));
564 gBS
->RestoreTPL (OldTpl
);
571 Clears a feature for the specified root hub port.
573 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
574 @param PortNumber Specifies the root hub port whose feature is
575 requested to be cleared.
576 @param PortFeature Indicates the feature selector associated with the
577 feature clear request.
579 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
580 for the USB root hub port specified by PortNumber.
581 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
582 @retval EFI_DEVICE_ERROR Can't read register.
587 XhcClearRootHubPortFeature (
588 IN EFI_USB2_HC_PROTOCOL
*This
,
590 IN EFI_USB_PORT_FEATURE PortFeature
593 USB_XHCI_INSTANCE
*Xhc
;
600 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
602 Xhc
= XHC_FROM_THIS (This
);
603 Status
= EFI_SUCCESS
;
605 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
607 if (PortNumber
>= TotalPort
) {
608 Status
= EFI_INVALID_PARAMETER
;
612 Offset
= XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
);
615 // Mask off the port status change bits, these bits are
618 State
= XhcReadOpReg (Xhc
, Offset
);
619 State
&= ~ (BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
621 switch (PortFeature
) {
622 case EfiUsbPortEnable
:
624 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
625 // A port may be disabled by software writing a '1' to this flag.
627 State
|= XHC_PORTSC_PED
;
628 State
&= ~XHC_PORTSC_RESET
;
629 XhcWriteOpReg (Xhc
, Offset
, State
);
632 case EfiUsbPortSuspend
:
633 State
|= XHC_PORTSC_LWS
;
634 XhcWriteOpReg (Xhc
, Offset
, State
);
635 State
&= ~XHC_PORTSC_PLS
;
636 XhcWriteOpReg (Xhc
, Offset
, State
);
639 case EfiUsbPortReset
:
641 // PORTSC_RESET BIT(4) bit is RW1S attribute, which means Write-1-to-set status:
642 // Register bits indicate status when read, a clear bit may be set by
643 // writing a '1'. Writing a '0' to RW1S bits has no effect.
647 case EfiUsbPortOwner
:
649 // XHCI root hub port don't has the owner bit, ignore the operation
653 case EfiUsbPortConnectChange
:
655 // Clear connect status change
657 State
|= XHC_PORTSC_CSC
;
658 XhcWriteOpReg (Xhc
, Offset
, State
);
661 case EfiUsbPortEnableChange
:
663 // Clear enable status change
665 State
|= XHC_PORTSC_PEC
;
666 XhcWriteOpReg (Xhc
, Offset
, State
);
669 case EfiUsbPortOverCurrentChange
:
671 // Clear PortOverCurrent change
673 State
|= XHC_PORTSC_OCC
;
674 XhcWriteOpReg (Xhc
, Offset
, State
);
677 case EfiUsbPortResetChange
:
679 // Clear Port Reset change
681 State
|= XHC_PORTSC_PRC
;
682 XhcWriteOpReg (Xhc
, Offset
, State
);
685 case EfiUsbPortPower
:
686 case EfiUsbPortSuspendChange
:
688 // Not supported or not related operation
693 Status
= EFI_INVALID_PARAMETER
;
698 DEBUG ((EFI_D_INFO
, "XhcClearRootHubPortFeature: status %r\n", Status
));
699 gBS
->RestoreTPL (OldTpl
);
706 Submits control transfer to a target USB device.
708 @param This This EFI_USB2_HC_PROTOCOL instance.
709 @param DeviceAddress The target device address.
710 @param DeviceSpeed Target device speed.
711 @param MaximumPacketLength Maximum packet size the default control transfer
712 endpoint is capable of sending or receiving.
713 @param Request USB device request to send.
714 @param TransferDirection Specifies the data direction for the data stage
715 @param Data Data buffer to be transmitted or received from USB
717 @param DataLength The size (in bytes) of the data buffer.
718 @param Timeout Indicates the maximum timeout, in millisecond.
719 @param Translator Transaction translator to be used by this device.
720 @param TransferResult Return the result of this control transfer.
722 @retval EFI_SUCCESS Transfer was completed successfully.
723 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
724 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
725 @retval EFI_TIMEOUT Transfer failed due to timeout.
726 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
732 IN EFI_USB2_HC_PROTOCOL
*This
,
733 IN UINT8 DeviceAddress
,
734 IN UINT8 DeviceSpeed
,
735 IN UINTN MaximumPacketLength
,
736 IN EFI_USB_DEVICE_REQUEST
*Request
,
737 IN EFI_USB_DATA_DIRECTION TransferDirection
,
739 IN OUT UINTN
*DataLength
,
741 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
742 OUT UINT32
*TransferResult
745 USB_XHCI_INSTANCE
*Xhc
;
749 UINT8 DescriptorType
;
754 EFI_USB_HUB_DESCRIPTOR
*HubDesc
;
757 EFI_STATUS RecoveryStatus
;
759 EFI_USB_PORT_STATUS PortStatus
;
763 // Validate parameters
765 if ((Request
== NULL
) || (TransferResult
== NULL
)) {
766 return EFI_INVALID_PARAMETER
;
769 if ((TransferDirection
!= EfiUsbDataIn
) &&
770 (TransferDirection
!= EfiUsbDataOut
) &&
771 (TransferDirection
!= EfiUsbNoData
)) {
772 return EFI_INVALID_PARAMETER
;
775 if ((TransferDirection
== EfiUsbNoData
) &&
776 ((Data
!= NULL
) || (*DataLength
!= 0))) {
777 return EFI_INVALID_PARAMETER
;
780 if ((TransferDirection
!= EfiUsbNoData
) &&
781 ((Data
== NULL
) || (*DataLength
== 0))) {
782 return EFI_INVALID_PARAMETER
;
785 if ((MaximumPacketLength
!= 8) && (MaximumPacketLength
!= 16) &&
786 (MaximumPacketLength
!= 32) && (MaximumPacketLength
!= 64) &&
787 (MaximumPacketLength
!= 512)
789 return EFI_INVALID_PARAMETER
;
792 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) {
793 return EFI_INVALID_PARAMETER
;
796 if ((DeviceSpeed
== EFI_USB_SPEED_SUPER
) && (MaximumPacketLength
!= 512)) {
797 return EFI_INVALID_PARAMETER
;
800 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
802 Xhc
= XHC_FROM_THIS (This
);
804 Status
= EFI_DEVICE_ERROR
;
805 *TransferResult
= EFI_USB_ERR_SYSTEM
;
807 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
808 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: HC halted at entrance\n"));
813 // Check if the device is still enabled before every transaction.
815 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
821 // Hook the Set_Address request from UsbBus.
822 // According to XHCI 1.0 spec, the Set_Address request is replaced by XHCI's Address_Device cmd.
824 if ((Request
->Request
== USB_REQ_SET_ADDRESS
) &&
825 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
827 // Reset the BusDevAddr field of all disabled entries in UsbDevContext array firstly.
828 // This way is used to clean the history to avoid using wrong device address by XhcAsyncInterruptTransfer().
830 for (Index
= 0; Index
< 255; Index
++) {
831 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
&&
832 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0) &&
833 (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== (UINT8
)Request
->Value
)) {
834 Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
= 0;
838 // The actual device address has been assigned by XHCI during initializing the device slot.
839 // So we just need establish the mapping relationship between the device address requested from UsbBus
840 // and the actual device address assigned by XHCI. The the following invocations through EFI_USB2_HC_PROTOCOL interface
841 // can find out the actual device address by it.
843 Xhc
->UsbDevContext
[SlotId
].BusDevAddr
= (UINT8
)Request
->Value
;
844 Status
= EFI_SUCCESS
;
849 // If the port reset operation happens after the usb super speed device is enabled,
850 // The subsequent configuration, such as getting device descriptor, will fail.
851 // So here a workaround is introduced to skip the reset operation if the device is enabled.
853 if ((Request
->Request
== USB_REQ_SET_FEATURE
) &&
854 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
)) &&
855 (Request
->Value
== EfiUsbPortReset
)) {
856 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
857 Status
= EFI_SUCCESS
;
863 // Create a new URB, insert it into the asynchronous
864 // schedule list, then poll the execution status.
865 // Note that we encode the direction in address although default control
866 // endpoint is bidirectional. XhcCreateUrb expects this
867 // combination of Ep addr and its direction.
869 Endpoint
= (UINT8
) (0 | ((TransferDirection
== EfiUsbDataIn
) ? 0x80 : 0));
885 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: failed to create URB"));
886 Status
= EFI_OUT_OF_RESOURCES
;
890 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
893 // Get the status from URB. The result is updated in XhcCheckUrbResult
894 // which is called by XhcExecTransfer
896 *TransferResult
= Urb
->Result
;
897 *DataLength
= Urb
->Completed
;
899 if (*TransferResult
== EFI_USB_NOERROR
) {
900 Status
= EFI_SUCCESS
;
901 } else if (*TransferResult
== EFI_USB_ERR_STALL
) {
902 RecoveryStatus
= XhcRecoverHaltedEndpoint(Xhc
, Urb
);
903 ASSERT_EFI_ERROR (RecoveryStatus
);
904 Status
= EFI_DEVICE_ERROR
;
911 // Hook Get_Descriptor request from UsbBus as we need evaluate context and configure endpoint.
912 // Hook Get_Status request form UsbBus as we need trace device attach/detach event happened at hub.
913 // Hook Set_Config request from UsbBus as we need configure device endpoint.
915 if ((Request
->Request
== USB_REQ_GET_DESCRIPTOR
) &&
916 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
)) ||
917 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_DEVICE
))))) {
918 DescriptorType
= (UINT8
)(Request
->Value
>> 8);
919 if ((DescriptorType
== USB_DESC_TYPE_DEVICE
) && ((*DataLength
== sizeof (EFI_USB_DEVICE_DESCRIPTOR
)) || ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (*DataLength
== 8)))) {
920 ASSERT (Data
!= NULL
);
922 // Store a copy of device scriptor as hub device need this info to configure endpoint.
924 CopyMem (&Xhc
->UsbDevContext
[SlotId
].DevDesc
, Data
, *DataLength
);
925 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.BcdUSB
== 0x0300) {
927 // If it's a usb3.0 device, then its max packet size is a 2^n.
929 MaxPacket0
= 1 << Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
931 MaxPacket0
= Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
933 Xhc
->UsbDevContext
[SlotId
].ConfDesc
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
* sizeof (EFI_USB_CONFIG_DESCRIPTOR
*));
934 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
935 Status
= XhcEvaluateContext (Xhc
, SlotId
, MaxPacket0
);
937 Status
= XhcEvaluateContext64 (Xhc
, SlotId
, MaxPacket0
);
939 ASSERT_EFI_ERROR (Status
);
940 } else if (DescriptorType
== USB_DESC_TYPE_CONFIG
) {
941 ASSERT (Data
!= NULL
);
942 if (*DataLength
== ((UINT16
*)Data
)[1]) {
944 // Get configuration value from request, Store the configuration descriptor for Configure_Endpoint cmd.
946 Index
= (UINT8
)Request
->Value
;
947 ASSERT (Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
);
948 Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
] = AllocateZeroPool(*DataLength
);
949 CopyMem (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
], Data
, *DataLength
);
951 } else if (((DescriptorType
== USB_DESC_TYPE_HUB
) ||
952 (DescriptorType
== USB_DESC_TYPE_HUB_SUPER_SPEED
)) && (*DataLength
> 2)) {
953 ASSERT (Data
!= NULL
);
954 HubDesc
= (EFI_USB_HUB_DESCRIPTOR
*)Data
;
955 ASSERT (HubDesc
->NumPorts
<= 15);
957 // The bit 5,6 of HubCharacter field of Hub Descriptor is TTT.
959 TTT
= (UINT8
)((HubDesc
->HubCharacter
& (BIT5
| BIT6
)) >> 5);
960 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.DeviceProtocol
== 2) {
962 // Don't support multi-TT feature for super speed hub now.
965 DEBUG ((EFI_D_ERROR
, "XHCI: Don't support multi-TT feature for Hub now. (force to disable MTT)\n"));
970 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
971 Status
= XhcConfigHubContext (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
973 Status
= XhcConfigHubContext64 (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
975 ASSERT_EFI_ERROR (Status
);
977 } else if ((Request
->Request
== USB_REQ_SET_CONFIG
) &&
978 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
980 // Hook Set_Config request from UsbBus as we need configure device endpoint.
982 for (Index
= 0; Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
; Index
++) {
983 if (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->ConfigurationValue
== (UINT8
)Request
->Value
) {
984 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
985 Status
= XhcSetConfigCmd (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
987 Status
= XhcSetConfigCmd64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
989 ASSERT_EFI_ERROR (Status
);
993 } else if ((Request
->Request
== USB_REQ_GET_STATUS
) &&
994 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
))) {
995 ASSERT (Data
!= NULL
);
997 // Hook Get_Status request from UsbBus to keep track of the port status change.
999 State
= *(UINT32
*)Data
;
1000 PortStatus
.PortStatus
= 0;
1001 PortStatus
.PortChangeStatus
= 0;
1003 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
1005 // For super speed hub, its bit10~12 presents the attached device speed.
1007 if ((State
& XHC_PORTSC_PS
) >> 10 == 0) {
1008 PortStatus
.PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
1012 // For high or full/low speed hub, its bit9~10 presents the attached device speed.
1014 if (XHC_BIT_IS_SET (State
, BIT9
)) {
1015 PortStatus
.PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
1016 } else if (XHC_BIT_IS_SET (State
, BIT10
)) {
1017 PortStatus
.PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
1022 // Convert the XHCI port/port change state to UEFI status
1024 MapSize
= sizeof (mUsbHubPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
1025 for (Index
= 0; Index
< MapSize
; Index
++) {
1026 if (XHC_BIT_IS_SET (State
, mUsbHubPortStateMap
[Index
].HwState
)) {
1027 PortStatus
.PortStatus
= (UINT16
) (PortStatus
.PortStatus
| mUsbHubPortStateMap
[Index
].UefiState
);
1031 MapSize
= sizeof (mUsbHubPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
1032 for (Index
= 0; Index
< MapSize
; Index
++) {
1033 if (XHC_BIT_IS_SET (State
, mUsbHubPortChangeMap
[Index
].HwState
)) {
1034 PortStatus
.PortChangeStatus
= (UINT16
) (PortStatus
.PortChangeStatus
| mUsbHubPortChangeMap
[Index
].UefiState
);
1038 XhcPollPortStatusChange (Xhc
, Xhc
->UsbDevContext
[SlotId
].RouteString
, (UINT8
)Request
->Index
, &PortStatus
);
1040 *(UINT32
*)Data
= *(UINT32
*)&PortStatus
;
1048 if (EFI_ERROR (Status
)) {
1049 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1052 gBS
->RestoreTPL (OldTpl
);
1059 Submits bulk transfer to a bulk endpoint of a USB device.
1061 @param This This EFI_USB2_HC_PROTOCOL instance.
1062 @param DeviceAddress Target device address.
1063 @param EndPointAddress Endpoint number and its direction in bit 7.
1064 @param DeviceSpeed Device speed, Low speed device doesn't support bulk
1066 @param MaximumPacketLength Maximum packet size the endpoint is capable of
1067 sending or receiving.
1068 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1069 @param Data Array of pointers to the buffers of data to transmit
1070 from or receive into.
1071 @param DataLength The lenght of the data buffer.
1072 @param DataToggle On input, the initial data toggle for the transfer;
1073 On output, it is updated to to next data toggle to
1074 use of the subsequent bulk transfer.
1075 @param Timeout Indicates the maximum time, in millisecond, which
1076 the transfer is allowed to complete.
1077 @param Translator A pointr to the transaction translator data.
1078 @param TransferResult A pointer to the detailed result information of the
1081 @retval EFI_SUCCESS The transfer was completed successfully.
1082 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1083 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1084 @retval EFI_TIMEOUT The transfer failed due to timeout.
1085 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1091 IN EFI_USB2_HC_PROTOCOL
*This
,
1092 IN UINT8 DeviceAddress
,
1093 IN UINT8 EndPointAddress
,
1094 IN UINT8 DeviceSpeed
,
1095 IN UINTN MaximumPacketLength
,
1096 IN UINT8 DataBuffersNumber
,
1097 IN OUT VOID
*Data
[EFI_USB_MAX_BULK_BUFFER_NUM
],
1098 IN OUT UINTN
*DataLength
,
1099 IN OUT UINT8
*DataToggle
,
1101 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1102 OUT UINT32
*TransferResult
1105 USB_XHCI_INSTANCE
*Xhc
;
1109 EFI_STATUS RecoveryStatus
;
1113 // Validate the parameters
1115 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1116 (Data
== NULL
) || (Data
[0] == NULL
) || (TransferResult
== NULL
)) {
1117 return EFI_INVALID_PARAMETER
;
1120 if ((*DataToggle
!= 0) && (*DataToggle
!= 1)) {
1121 return EFI_INVALID_PARAMETER
;
1124 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) ||
1125 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1126 ((EFI_USB_SPEED_HIGH
== DeviceSpeed
) && (MaximumPacketLength
> 512)) ||
1127 ((EFI_USB_SPEED_SUPER
== DeviceSpeed
) && (MaximumPacketLength
> 1024))) {
1128 return EFI_INVALID_PARAMETER
;
1131 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1133 Xhc
= XHC_FROM_THIS (This
);
1135 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1136 Status
= EFI_DEVICE_ERROR
;
1138 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1139 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: HC is halted\n"));
1144 // Check if the device is still enabled before every transaction.
1146 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1152 // Create a new URB, insert it into the asynchronous
1153 // schedule list, then poll the execution status.
1155 Urb
= XhcCreateUrb (
1160 MaximumPacketLength
,
1170 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: failed to create URB\n"));
1171 Status
= EFI_OUT_OF_RESOURCES
;
1175 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
1177 *TransferResult
= Urb
->Result
;
1178 *DataLength
= Urb
->Completed
;
1180 if (*TransferResult
== EFI_USB_NOERROR
) {
1181 Status
= EFI_SUCCESS
;
1182 } else if (*TransferResult
== EFI_USB_ERR_STALL
) {
1183 RecoveryStatus
= XhcRecoverHaltedEndpoint(Xhc
, Urb
);
1184 ASSERT_EFI_ERROR (RecoveryStatus
);
1185 Status
= EFI_DEVICE_ERROR
;
1192 if (EFI_ERROR (Status
)) {
1193 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1195 gBS
->RestoreTPL (OldTpl
);
1201 Submits an asynchronous interrupt transfer to an
1202 interrupt endpoint of a USB device.
1204 @param This This EFI_USB2_HC_PROTOCOL instance.
1205 @param DeviceAddress Target device address.
1206 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1207 @param DeviceSpeed Indicates device speed.
1208 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1209 @param IsNewTransfer If TRUE, to submit an new asynchronous interrupt
1210 transfer If FALSE, to remove the specified
1211 asynchronous interrupt.
1212 @param DataToggle On input, the initial data toggle to use; on output,
1213 it is updated to indicate the next data toggle.
1214 @param PollingInterval The he interval, in milliseconds, that the transfer
1216 @param DataLength The length of data to receive at the rate specified
1218 @param Translator Transaction translator to use.
1219 @param CallBackFunction Function to call at the rate specified by
1221 @param Context Context to CallBackFunction.
1223 @retval EFI_SUCCESS The request has been successfully submitted or canceled.
1224 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1225 @retval EFI_OUT_OF_RESOURCES The request failed due to a lack of resources.
1226 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1231 XhcAsyncInterruptTransfer (
1232 IN EFI_USB2_HC_PROTOCOL
*This
,
1233 IN UINT8 DeviceAddress
,
1234 IN UINT8 EndPointAddress
,
1235 IN UINT8 DeviceSpeed
,
1236 IN UINTN MaximumPacketLength
,
1237 IN BOOLEAN IsNewTransfer
,
1238 IN OUT UINT8
*DataToggle
,
1239 IN UINTN PollingInterval
,
1240 IN UINTN DataLength
,
1241 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1242 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction
,
1243 IN VOID
*Context OPTIONAL
1246 USB_XHCI_INSTANCE
*Xhc
;
1255 // Validate parameters
1257 if (!XHCI_IS_DATAIN (EndPointAddress
)) {
1258 return EFI_INVALID_PARAMETER
;
1261 if (IsNewTransfer
) {
1262 if (DataLength
== 0) {
1263 return EFI_INVALID_PARAMETER
;
1266 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1267 return EFI_INVALID_PARAMETER
;
1270 if ((PollingInterval
> 255) || (PollingInterval
< 1)) {
1271 return EFI_INVALID_PARAMETER
;
1275 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1277 Xhc
= XHC_FROM_THIS (This
);
1280 // Delete Async interrupt transfer request.
1282 if (!IsNewTransfer
) {
1284 // The delete request may happen after device is detached.
1286 for (Index
= 0; Index
< 255; Index
++) {
1287 if (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== DeviceAddress
) {
1293 Status
= EFI_INVALID_PARAMETER
;
1297 Status
= XhciDelAsyncIntTransfer (Xhc
, DeviceAddress
, EndPointAddress
);
1298 DEBUG ((EFI_D_INFO
, "XhcAsyncInterruptTransfer: remove old transfer for addr %d, Status = %r\n", DeviceAddress
, Status
));
1302 Status
= EFI_SUCCESS
;
1304 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1305 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: HC is halt\n"));
1306 Status
= EFI_DEVICE_ERROR
;
1311 // Check if the device is still enabled before every transaction.
1313 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1318 Data
= AllocateZeroPool (DataLength
);
1321 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: failed to allocate buffer\n"));
1322 Status
= EFI_OUT_OF_RESOURCES
;
1326 Urb
= XhcCreateUrb (
1331 MaximumPacketLength
,
1332 XHC_INT_TRANSFER_ASYNC
,
1341 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: failed to create URB\n"));
1343 Status
= EFI_OUT_OF_RESOURCES
;
1347 InsertHeadList (&Xhc
->AsyncIntTransfers
, &Urb
->UrbList
);
1349 // Ring the doorbell
1351 Status
= RingIntTransferDoorBell (Xhc
, Urb
);
1354 gBS
->RestoreTPL (OldTpl
);
1361 Submits synchronous interrupt transfer to an interrupt endpoint
1364 @param This This EFI_USB2_HC_PROTOCOL instance.
1365 @param DeviceAddress Target device address.
1366 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1367 @param DeviceSpeed Indicates device speed.
1368 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1369 of sending or receiving.
1370 @param Data Buffer of data that will be transmitted to USB
1371 device or received from USB device.
1372 @param DataLength On input, the size, in bytes, of the data buffer; On
1373 output, the number of bytes transferred.
1374 @param DataToggle On input, the initial data toggle to use; on output,
1375 it is updated to indicate the next data toggle.
1376 @param Timeout Maximum time, in second, to complete.
1377 @param Translator Transaction translator to use.
1378 @param TransferResult Variable to receive the transfer result.
1380 @return EFI_SUCCESS The transfer was completed successfully.
1381 @return EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1382 @return EFI_INVALID_PARAMETER Some parameters are invalid.
1383 @return EFI_TIMEOUT The transfer failed due to timeout.
1384 @return EFI_DEVICE_ERROR The failed due to host controller or device error
1389 XhcSyncInterruptTransfer (
1390 IN EFI_USB2_HC_PROTOCOL
*This
,
1391 IN UINT8 DeviceAddress
,
1392 IN UINT8 EndPointAddress
,
1393 IN UINT8 DeviceSpeed
,
1394 IN UINTN MaximumPacketLength
,
1396 IN OUT UINTN
*DataLength
,
1397 IN OUT UINT8
*DataToggle
,
1399 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1400 OUT UINT32
*TransferResult
1403 USB_XHCI_INSTANCE
*Xhc
;
1407 EFI_STATUS RecoveryStatus
;
1411 // Validates parameters
1413 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1414 (Data
== NULL
) || (TransferResult
== NULL
)) {
1415 return EFI_INVALID_PARAMETER
;
1418 if (!XHCI_IS_DATAIN (EndPointAddress
)) {
1419 return EFI_INVALID_PARAMETER
;
1422 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1423 return EFI_INVALID_PARAMETER
;
1426 if (((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) ||
1427 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1428 ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) && (MaximumPacketLength
> 3072))) {
1429 return EFI_INVALID_PARAMETER
;
1432 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1434 Xhc
= XHC_FROM_THIS (This
);
1436 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1437 Status
= EFI_DEVICE_ERROR
;
1439 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1440 DEBUG ((EFI_D_ERROR
, "EhcSyncInterruptTransfer: HC is halt\n"));
1445 // Check if the device is still enabled before every transaction.
1447 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1452 Urb
= XhcCreateUrb (
1457 MaximumPacketLength
,
1458 XHC_INT_TRANSFER_SYNC
,
1467 DEBUG ((EFI_D_ERROR
, "XhcSyncInterruptTransfer: failed to create URB\n"));
1468 Status
= EFI_OUT_OF_RESOURCES
;
1472 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
1474 *TransferResult
= Urb
->Result
;
1475 *DataLength
= Urb
->Completed
;
1477 if (*TransferResult
== EFI_USB_NOERROR
) {
1478 Status
= EFI_SUCCESS
;
1479 } else if (*TransferResult
== EFI_USB_ERR_STALL
) {
1480 RecoveryStatus
= XhcRecoverHaltedEndpoint(Xhc
, Urb
);
1481 ASSERT_EFI_ERROR (RecoveryStatus
);
1482 Status
= EFI_DEVICE_ERROR
;
1488 if (EFI_ERROR (Status
)) {
1489 DEBUG ((EFI_D_ERROR
, "XhcSyncInterruptTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1491 gBS
->RestoreTPL (OldTpl
);
1498 Submits isochronous transfer to a target USB device.
1500 @param This This EFI_USB2_HC_PROTOCOL instance.
1501 @param DeviceAddress Target device address.
1502 @param EndPointAddress End point address with its direction.
1503 @param DeviceSpeed Device speed, Low speed device doesn't support this
1505 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1506 sending or receiving.
1507 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1508 @param Data Array of pointers to the buffers of data that will
1509 be transmitted to USB device or received from USB
1511 @param DataLength The size, in bytes, of the data buffer.
1512 @param Translator Transaction translator to use.
1513 @param TransferResult Variable to receive the transfer result.
1515 @return EFI_UNSUPPORTED Isochronous transfer is unsupported.
1520 XhcIsochronousTransfer (
1521 IN EFI_USB2_HC_PROTOCOL
*This
,
1522 IN UINT8 DeviceAddress
,
1523 IN UINT8 EndPointAddress
,
1524 IN UINT8 DeviceSpeed
,
1525 IN UINTN MaximumPacketLength
,
1526 IN UINT8 DataBuffersNumber
,
1527 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1528 IN UINTN DataLength
,
1529 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1530 OUT UINT32
*TransferResult
1533 return EFI_UNSUPPORTED
;
1538 Submits Async isochronous transfer to a target USB device.
1540 @param This This EFI_USB2_HC_PROTOCOL instance.
1541 @param DeviceAddress Target device address.
1542 @param EndPointAddress End point address with its direction.
1543 @param DeviceSpeed Device speed, Low speed device doesn't support this
1545 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1546 sending or receiving.
1547 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1548 @param Data Array of pointers to the buffers of data that will
1549 be transmitted to USB device or received from USB
1551 @param DataLength The size, in bytes, of the data buffer.
1552 @param Translator Transaction translator to use.
1553 @param IsochronousCallBack Function to be called when the transfer complete.
1554 @param Context Context passed to the call back function as
1557 @return EFI_UNSUPPORTED Isochronous transfer isn't supported.
1562 XhcAsyncIsochronousTransfer (
1563 IN EFI_USB2_HC_PROTOCOL
*This
,
1564 IN UINT8 DeviceAddress
,
1565 IN UINT8 EndPointAddress
,
1566 IN UINT8 DeviceSpeed
,
1567 IN UINTN MaximumPacketLength
,
1568 IN UINT8 DataBuffersNumber
,
1569 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1570 IN UINTN DataLength
,
1571 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1572 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
1576 return EFI_UNSUPPORTED
;
1580 Entry point for EFI drivers.
1582 @param ImageHandle EFI_HANDLE.
1583 @param SystemTable EFI_SYSTEM_TABLE.
1585 @retval EFI_SUCCESS Success.
1586 @retval Others Fail.
1591 XhcDriverEntryPoint (
1592 IN EFI_HANDLE ImageHandle
,
1593 IN EFI_SYSTEM_TABLE
*SystemTable
1596 return EfiLibInstallDriverBindingComponentName2 (
1599 &gXhciDriverBinding
,
1601 &gXhciComponentName
,
1602 &gXhciComponentName2
1608 Test to see if this driver supports ControllerHandle. Any
1609 ControllerHandle that has Usb2HcProtocol installed will
1612 @param This Protocol instance pointer.
1613 @param Controller Handle of device to test.
1614 @param RemainingDevicePath Not used.
1616 @return EFI_SUCCESS This driver supports this device.
1617 @return EFI_UNSUPPORTED This driver does not support this device.
1622 XhcDriverBindingSupported (
1623 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1624 IN EFI_HANDLE Controller
,
1625 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1629 EFI_PCI_IO_PROTOCOL
*PciIo
;
1630 USB_CLASSC UsbClassCReg
;
1633 // Test whether there is PCI IO Protocol attached on the controller handle.
1635 Status
= gBS
->OpenProtocol (
1637 &gEfiPciIoProtocolGuid
,
1639 This
->DriverBindingHandle
,
1641 EFI_OPEN_PROTOCOL_BY_DRIVER
1644 if (EFI_ERROR (Status
)) {
1645 return EFI_UNSUPPORTED
;
1648 Status
= PciIo
->Pci
.Read (
1651 PCI_CLASSCODE_OFFSET
,
1652 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1656 if (EFI_ERROR (Status
)) {
1657 Status
= EFI_UNSUPPORTED
;
1662 // Test whether the controller belongs to Xhci type
1664 if ((UsbClassCReg
.BaseCode
!= PCI_CLASS_SERIAL
) ||
1665 (UsbClassCReg
.SubClassCode
!= PCI_CLASS_SERIAL_USB
) ||
1666 (UsbClassCReg
.ProgInterface
!= PCI_IF_XHCI
)) {
1667 Status
= EFI_UNSUPPORTED
;
1671 gBS
->CloseProtocol (
1673 &gEfiPciIoProtocolGuid
,
1674 This
->DriverBindingHandle
,
1682 Create and initialize a USB_XHCI_INSTANCE structure.
1684 @param PciIo The PciIo on this device.
1685 @param DevicePath The device path of host controller.
1686 @param OriginalPciAttributes Original PCI attributes.
1688 @return The allocated and initialized USB_XHCI_INSTANCE structure if created,
1694 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1695 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1696 IN UINT64 OriginalPciAttributes
1699 USB_XHCI_INSTANCE
*Xhc
;
1704 Xhc
= AllocateZeroPool (sizeof (USB_XHCI_INSTANCE
));
1711 // Initialize private data structure
1713 Xhc
->Signature
= XHCI_INSTANCE_SIG
;
1715 Xhc
->DevicePath
= DevicePath
;
1716 Xhc
->OriginalPciAttributes
= OriginalPciAttributes
;
1717 CopyMem (&Xhc
->Usb2Hc
, &gXhciUsb2HcTemplate
, sizeof (EFI_USB2_HC_PROTOCOL
));
1719 InitializeListHead (&Xhc
->AsyncIntTransfers
);
1722 // Be caution that the Offset passed to XhcReadCapReg() should be Dword align
1724 Xhc
->CapLength
= XhcReadCapReg8 (Xhc
, XHC_CAPLENGTH_OFFSET
);
1725 Xhc
->HcSParams1
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS1_OFFSET
);
1726 Xhc
->HcSParams2
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS2_OFFSET
);
1727 Xhc
->HcCParams
.Dword
= XhcReadCapReg (Xhc
, XHC_HCCPARAMS_OFFSET
);
1728 Xhc
->DBOff
= XhcReadCapReg (Xhc
, XHC_DBOFF_OFFSET
);
1729 Xhc
->RTSOff
= XhcReadCapReg (Xhc
, XHC_RTSOFF_OFFSET
);
1732 // This PageSize field defines the page size supported by the xHC implementation.
1733 // This xHC supports a page size of 2^(n+12) if bit n is Set. For example,
1734 // if bit 0 is Set, the xHC supports 4k byte page sizes.
1736 PageSize
= XhcReadOpReg(Xhc
, XHC_PAGESIZE_OFFSET
) & XHC_PAGESIZE_MASK
;
1737 Xhc
->PageSize
= 1 << (HighBitSet32(PageSize
) + 12);
1739 ExtCapReg
= (UINT16
) (Xhc
->HcCParams
.Data
.ExtCapReg
);
1740 Xhc
->ExtCapRegBase
= ExtCapReg
<< 2;
1741 Xhc
->UsbLegSupOffset
= XhcGetLegSupCapAddr (Xhc
);
1743 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: Capability length 0x%x\n", Xhc
->CapLength
));
1744 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcSParams1 0x%x\n", Xhc
->HcSParams1
));
1745 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcSParams2 0x%x\n", Xhc
->HcSParams2
));
1746 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcCParams 0x%x\n", Xhc
->HcCParams
));
1747 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: DBOff 0x%x\n", Xhc
->DBOff
));
1748 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: RTSOff 0x%x\n", Xhc
->RTSOff
));
1749 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: UsbLegSupOffset 0x%x\n", Xhc
->UsbLegSupOffset
));
1752 // Create AsyncRequest Polling Timer
1754 Status
= gBS
->CreateEvent (
1755 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1757 XhcMonitorAsyncRequests
,
1762 if (EFI_ERROR (Status
)) {
1774 One notified function to stop the Host Controller when gBS->ExitBootServices() called.
1776 @param Event Pointer to this event
1777 @param Context Event hanlder private data
1782 XhcExitBootService (
1788 USB_XHCI_INSTANCE
*Xhc
;
1789 EFI_PCI_IO_PROTOCOL
*PciIo
;
1791 Xhc
= (USB_XHCI_INSTANCE
*) Context
;
1795 // Stop AsyncRequest Polling timer then stop the XHCI driver
1796 // and uninstall the XHCI protocl.
1798 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
1799 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
1801 if (Xhc
->PollTimer
!= NULL
) {
1802 gBS
->CloseEvent (Xhc
->PollTimer
);
1805 XhcClearBiosOwnership (Xhc
);
1808 // Restore original PCI attributes
1812 EfiPciIoAttributeOperationSet
,
1813 Xhc
->OriginalPciAttributes
,
1819 Starting the Usb XHCI Driver.
1821 @param This Protocol instance pointer.
1822 @param Controller Handle of device to test.
1823 @param RemainingDevicePath Not used.
1825 @return EFI_SUCCESS supports this device.
1826 @return EFI_UNSUPPORTED do not support this device.
1827 @return EFI_DEVICE_ERROR cannot be started due to device Error.
1828 @return EFI_OUT_OF_RESOURCES cannot allocate resources.
1833 XhcDriverBindingStart (
1834 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1835 IN EFI_HANDLE Controller
,
1836 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1840 EFI_PCI_IO_PROTOCOL
*PciIo
;
1842 UINT64 OriginalPciAttributes
;
1843 BOOLEAN PciAttributesSaved
;
1844 USB_XHCI_INSTANCE
*Xhc
;
1845 EFI_DEVICE_PATH_PROTOCOL
*HcDevicePath
;
1848 // Open the PciIo Protocol, then enable the USB host controller
1850 Status
= gBS
->OpenProtocol (
1852 &gEfiPciIoProtocolGuid
,
1854 This
->DriverBindingHandle
,
1856 EFI_OPEN_PROTOCOL_BY_DRIVER
1859 if (EFI_ERROR (Status
)) {
1864 // Open Device Path Protocol for on USB host controller
1866 HcDevicePath
= NULL
;
1867 Status
= gBS
->OpenProtocol (
1869 &gEfiDevicePathProtocolGuid
,
1870 (VOID
**) &HcDevicePath
,
1871 This
->DriverBindingHandle
,
1873 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1876 PciAttributesSaved
= FALSE
;
1878 // Save original PCI attributes
1880 Status
= PciIo
->Attributes (
1882 EfiPciIoAttributeOperationGet
,
1884 &OriginalPciAttributes
1887 if (EFI_ERROR (Status
)) {
1890 PciAttributesSaved
= TRUE
;
1892 Status
= PciIo
->Attributes (
1894 EfiPciIoAttributeOperationSupported
,
1898 if (!EFI_ERROR (Status
)) {
1899 Supports
&= EFI_PCI_DEVICE_ENABLE
;
1900 Status
= PciIo
->Attributes (
1902 EfiPciIoAttributeOperationEnable
,
1908 if (EFI_ERROR (Status
)) {
1909 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to enable controller\n"));
1914 // Create then install USB2_HC_PROTOCOL
1916 Xhc
= XhcCreateUsbHc (PciIo
, HcDevicePath
, OriginalPciAttributes
);
1919 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to create USB2_HC\n"));
1920 return EFI_OUT_OF_RESOURCES
;
1923 XhcSetBiosOwnership (Xhc
);
1925 XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
1926 ASSERT (XhcIsHalt (Xhc
));
1929 // After Chip Hardware Reset wait until the Controller Not Ready (CNR) flag
1930 // in the USBSTS is '0' before writing any xHC Operational or Runtime registers.
1932 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
1935 // Initialize the schedule
1940 // Start the Host Controller
1942 XhcRunHC(Xhc
, XHC_GENERIC_TIMEOUT
);
1945 // Start the asynchronous interrupt monitor
1947 Status
= gBS
->SetTimer (Xhc
->PollTimer
, TimerPeriodic
, XHC_ASYNC_TIMER_INTERVAL
);
1948 if (EFI_ERROR (Status
)) {
1949 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to start async interrupt monitor\n"));
1950 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
1955 // Create event to stop the HC when exit boot service.
1957 Status
= gBS
->CreateEventEx (
1962 &gEfiEventExitBootServicesGuid
,
1963 &Xhc
->ExitBootServiceEvent
1965 if (EFI_ERROR (Status
)) {
1970 // Install the component name protocol, don't fail the start
1971 // because of something for display.
1975 gXhciComponentName
.SupportedLanguages
,
1976 &Xhc
->ControllerNameTable
,
1977 L
"eXtensible Host Controller (USB 3.0)",
1982 gXhciComponentName2
.SupportedLanguages
,
1983 &Xhc
->ControllerNameTable
,
1984 L
"eXtensible Host Controller (USB 3.0)",
1988 Status
= gBS
->InstallProtocolInterface (
1990 &gEfiUsb2HcProtocolGuid
,
1991 EFI_NATIVE_INTERFACE
,
1994 if (EFI_ERROR (Status
)) {
1995 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to install USB2_HC Protocol\n"));
1999 DEBUG ((EFI_D_INFO
, "XhcDriverBindingStart: XHCI started for controller @ %x\n", Controller
));
2003 gBS
->CloseEvent (Xhc
->PollTimer
);
2008 if (PciAttributesSaved
) {
2010 // Restore original PCI attributes
2014 EfiPciIoAttributeOperationSet
,
2015 OriginalPciAttributes
,
2020 gBS
->CloseProtocol (
2022 &gEfiPciIoProtocolGuid
,
2023 This
->DriverBindingHandle
,
2032 Stop this driver on ControllerHandle. Support stoping any child handles
2033 created by this driver.
2035 @param This Protocol instance pointer.
2036 @param Controller Handle of device to stop driver on.
2037 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
2038 @param ChildHandleBuffer List of handles for the children we need to stop.
2040 @return EFI_SUCCESS Success.
2041 @return EFI_DEVICE_ERROR Fail.
2046 XhcDriverBindingStop (
2047 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
2048 IN EFI_HANDLE Controller
,
2049 IN UINTN NumberOfChildren
,
2050 IN EFI_HANDLE
*ChildHandleBuffer
2054 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
2055 EFI_PCI_IO_PROTOCOL
*PciIo
;
2056 USB_XHCI_INSTANCE
*Xhc
;
2060 // Test whether the Controller handler passed in is a valid
2061 // Usb controller handle that should be supported, if not,
2062 // return the error status directly
2064 Status
= gBS
->OpenProtocol (
2066 &gEfiUsb2HcProtocolGuid
,
2068 This
->DriverBindingHandle
,
2070 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2073 if (EFI_ERROR (Status
)) {
2077 Xhc
= XHC_FROM_THIS (Usb2Hc
);
2081 // Stop AsyncRequest Polling timer then stop the XHCI driver
2082 // and uninstall the XHCI protocl.
2084 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
2087 // Disable the device slots occupied by these devices on its downstream ports.
2088 // Entry 0 is reserved.
2090 for (Index
= 0; Index
< 255; Index
++) {
2091 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
||
2092 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0)) {
2095 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
2096 XhcDisableSlotCmd (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2098 XhcDisableSlotCmd64 (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2102 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2103 XhcClearBiosOwnership (Xhc
);
2105 Status
= gBS
->UninstallProtocolInterface (
2107 &gEfiUsb2HcProtocolGuid
,
2111 if (EFI_ERROR (Status
)) {
2115 if (Xhc
->PollTimer
!= NULL
) {
2116 gBS
->CloseEvent (Xhc
->PollTimer
);
2119 if (Xhc
->ExitBootServiceEvent
!= NULL
) {
2120 gBS
->CloseEvent (Xhc
->ExitBootServiceEvent
);
2123 XhciDelAllAsyncIntTransfers (Xhc
);
2126 if (Xhc
->ControllerNameTable
) {
2127 FreeUnicodeStringTable (Xhc
->ControllerNameTable
);
2131 // Restore original PCI attributes
2135 EfiPciIoAttributeOperationSet
,
2136 Xhc
->OriginalPciAttributes
,
2140 gBS
->CloseProtocol (
2142 &gEfiPciIoProtocolGuid
,
2143 This
->DriverBindingHandle
,