2 The XHCI controller driver.
4 Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 // Two arrays used to translate the XHCI port state (change)
19 // to the UEFI protocol's port state (change).
21 USB_PORT_STATE_MAP mUsbPortStateMap
[] = {
22 {XHC_PORTSC_CCS
, USB_PORT_STAT_CONNECTION
},
23 {XHC_PORTSC_PED
, USB_PORT_STAT_ENABLE
},
24 {XHC_PORTSC_OCA
, USB_PORT_STAT_OVERCURRENT
},
25 {XHC_PORTSC_RESET
, USB_PORT_STAT_RESET
}
28 USB_PORT_STATE_MAP mUsbPortChangeMap
[] = {
29 {XHC_PORTSC_CSC
, USB_PORT_STAT_C_CONNECTION
},
30 {XHC_PORTSC_PEC
, USB_PORT_STAT_C_ENABLE
},
31 {XHC_PORTSC_OCC
, USB_PORT_STAT_C_OVERCURRENT
},
32 {XHC_PORTSC_PRC
, USB_PORT_STAT_C_RESET
}
35 USB_CLEAR_PORT_MAP mUsbClearPortChangeMap
[] = {
36 {XHC_PORTSC_CSC
, EfiUsbPortConnectChange
},
37 {XHC_PORTSC_PEC
, EfiUsbPortEnableChange
},
38 {XHC_PORTSC_OCC
, EfiUsbPortOverCurrentChange
},
39 {XHC_PORTSC_PRC
, EfiUsbPortResetChange
}
42 USB_PORT_STATE_MAP mUsbHubPortStateMap
[] = {
43 {XHC_HUB_PORTSC_CCS
, USB_PORT_STAT_CONNECTION
},
44 {XHC_HUB_PORTSC_PED
, USB_PORT_STAT_ENABLE
},
45 {XHC_HUB_PORTSC_OCA
, USB_PORT_STAT_OVERCURRENT
},
46 {XHC_HUB_PORTSC_RESET
, USB_PORT_STAT_RESET
}
49 USB_PORT_STATE_MAP mUsbHubPortChangeMap
[] = {
50 {XHC_HUB_PORTSC_CSC
, USB_PORT_STAT_C_CONNECTION
},
51 {XHC_HUB_PORTSC_PEC
, USB_PORT_STAT_C_ENABLE
},
52 {XHC_HUB_PORTSC_OCC
, USB_PORT_STAT_C_OVERCURRENT
},
53 {XHC_HUB_PORTSC_PRC
, USB_PORT_STAT_C_RESET
}
56 USB_CLEAR_PORT_MAP mUsbHubClearPortChangeMap
[] = {
57 {XHC_HUB_PORTSC_CSC
, EfiUsbPortConnectChange
},
58 {XHC_HUB_PORTSC_PEC
, EfiUsbPortEnableChange
},
59 {XHC_HUB_PORTSC_OCC
, EfiUsbPortOverCurrentChange
},
60 {XHC_HUB_PORTSC_PRC
, EfiUsbPortResetChange
},
61 {XHC_HUB_PORTSC_BHRC
, Usb3PortBHPortResetChange
}
64 EFI_DRIVER_BINDING_PROTOCOL gXhciDriverBinding
= {
65 XhcDriverBindingSupported
,
66 XhcDriverBindingStart
,
74 // Template for Xhci's Usb2 Host Controller Protocol Instance.
76 EFI_USB2_HC_PROTOCOL gXhciUsb2HcTemplate
= {
83 XhcAsyncInterruptTransfer
,
84 XhcSyncInterruptTransfer
,
85 XhcIsochronousTransfer
,
86 XhcAsyncIsochronousTransfer
,
87 XhcGetRootHubPortStatus
,
88 XhcSetRootHubPortFeature
,
89 XhcClearRootHubPortFeature
,
95 Retrieves the capability of root hub ports.
97 @param This The EFI_USB2_HC_PROTOCOL instance.
98 @param MaxSpeed Max speed supported by the controller.
99 @param PortNumber Number of the root hub ports.
100 @param Is64BitCapable Whether the controller supports 64-bit memory
103 @retval EFI_SUCCESS Host controller capability were retrieved successfully.
104 @retval EFI_INVALID_PARAMETER Either of the three capability pointer is NULL.
110 IN EFI_USB2_HC_PROTOCOL
*This
,
112 OUT UINT8
*PortNumber
,
113 OUT UINT8
*Is64BitCapable
116 USB_XHCI_INSTANCE
*Xhc
;
119 if ((MaxSpeed
== NULL
) || (PortNumber
== NULL
) || (Is64BitCapable
== NULL
)) {
120 return EFI_INVALID_PARAMETER
;
123 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
125 Xhc
= XHC_FROM_THIS (This
);
126 *MaxSpeed
= EFI_USB_SPEED_SUPER
;
127 *PortNumber
= (UINT8
) (Xhc
->HcSParams1
.Data
.MaxPorts
);
128 *Is64BitCapable
= (UINT8
) Xhc
->Support64BitDma
;
129 DEBUG ((EFI_D_INFO
, "XhcGetCapability: %d ports, 64 bit %d\n", *PortNumber
, *Is64BitCapable
));
131 gBS
->RestoreTPL (OldTpl
);
138 Provides software reset for the USB host controller.
140 @param This This EFI_USB2_HC_PROTOCOL instance.
141 @param Attributes A bit mask of the reset operation to perform.
143 @retval EFI_SUCCESS The reset operation succeeded.
144 @retval EFI_INVALID_PARAMETER Attributes is not valid.
145 @retval EFI_UNSUPPOURTED The type of reset specified by Attributes is
146 not currently supported by the host controller.
147 @retval EFI_DEVICE_ERROR Host controller isn't halted to reset.
153 IN EFI_USB2_HC_PROTOCOL
*This
,
157 USB_XHCI_INSTANCE
*Xhc
;
161 Xhc
= XHC_FROM_THIS (This
);
163 if (Xhc
->DevicePath
!= NULL
) {
165 // Report Status Code to indicate reset happens
167 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
169 (EFI_IO_BUS_USB
| EFI_IOB_PC_RESET
),
174 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
176 switch (Attributes
) {
177 case EFI_USB_HC_RESET_GLOBAL
:
179 // Flow through, same behavior as Host Controller Reset
181 case EFI_USB_HC_RESET_HOST_CONTROLLER
:
182 if ((Xhc
->DebugCapSupOffset
!= 0xFFFFFFFF) && ((XhcReadExtCapReg (Xhc
, Xhc
->DebugCapSupOffset
) & 0xFF) == XHC_CAP_USB_DEBUG
) &&
183 ((XhcReadExtCapReg (Xhc
, Xhc
->DebugCapSupOffset
+ XHC_DC_DCCTRL
) & BIT0
) != 0)) {
184 Status
= EFI_SUCCESS
;
188 // Host Controller must be Halt when Reset it
190 if (!XhcIsHalt (Xhc
)) {
191 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
193 if (EFI_ERROR (Status
)) {
194 Status
= EFI_DEVICE_ERROR
;
199 Status
= XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
200 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
202 if (EFI_ERROR (Status
)) {
206 // Clean up the asynchronous transfers, currently only
207 // interrupt supports asynchronous operation.
209 XhciDelAllAsyncIntTransfers (Xhc
);
215 case EFI_USB_HC_RESET_GLOBAL_WITH_DEBUG
:
216 case EFI_USB_HC_RESET_HOST_WITH_DEBUG
:
217 Status
= EFI_UNSUPPORTED
;
221 Status
= EFI_INVALID_PARAMETER
;
225 DEBUG ((EFI_D_INFO
, "XhcReset: status %r\n", Status
));
226 gBS
->RestoreTPL (OldTpl
);
233 Retrieve the current state of the USB host controller.
235 @param This This EFI_USB2_HC_PROTOCOL instance.
236 @param State Variable to return the current host controller
239 @retval EFI_SUCCESS Host controller state was returned in State.
240 @retval EFI_INVALID_PARAMETER State is NULL.
241 @retval EFI_DEVICE_ERROR An error was encountered while attempting to
242 retrieve the host controller's current state.
248 IN EFI_USB2_HC_PROTOCOL
*This
,
249 OUT EFI_USB_HC_STATE
*State
252 USB_XHCI_INSTANCE
*Xhc
;
256 return EFI_INVALID_PARAMETER
;
259 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
261 Xhc
= XHC_FROM_THIS (This
);
263 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
264 *State
= EfiUsbHcStateHalt
;
266 *State
= EfiUsbHcStateOperational
;
269 DEBUG ((EFI_D_INFO
, "XhcGetState: current state %d\n", *State
));
270 gBS
->RestoreTPL (OldTpl
);
276 Sets the USB host controller to a specific state.
278 @param This This EFI_USB2_HC_PROTOCOL instance.
279 @param State The state of the host controller that will be set.
281 @retval EFI_SUCCESS The USB host controller was successfully placed
282 in the state specified by State.
283 @retval EFI_INVALID_PARAMETER State is invalid.
284 @retval EFI_DEVICE_ERROR Failed to set the state due to device error.
290 IN EFI_USB2_HC_PROTOCOL
*This
,
291 IN EFI_USB_HC_STATE State
294 USB_XHCI_INSTANCE
*Xhc
;
296 EFI_USB_HC_STATE CurState
;
299 Status
= XhcGetState (This
, &CurState
);
301 if (EFI_ERROR (Status
)) {
302 return EFI_DEVICE_ERROR
;
305 if (CurState
== State
) {
309 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
311 Xhc
= XHC_FROM_THIS (This
);
314 case EfiUsbHcStateHalt
:
315 Status
= XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
318 case EfiUsbHcStateOperational
:
319 if (XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HSE
)) {
320 Status
= EFI_DEVICE_ERROR
;
325 // Software must not write a one to this field unless the host controller
326 // is in the Halted state. Doing so will yield undefined results.
327 // refers to Spec[XHCI1.0-2.3.1]
329 if (!XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_HALT
)) {
330 Status
= EFI_DEVICE_ERROR
;
334 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
337 case EfiUsbHcStateSuspend
:
338 Status
= EFI_UNSUPPORTED
;
342 Status
= EFI_INVALID_PARAMETER
;
345 DEBUG ((EFI_D_INFO
, "XhcSetState: status %r\n", Status
));
346 gBS
->RestoreTPL (OldTpl
);
352 Retrieves the current status of a USB root hub port.
354 @param This This EFI_USB2_HC_PROTOCOL instance.
355 @param PortNumber The root hub port to retrieve the state from.
356 This value is zero-based.
357 @param PortStatus Variable to receive the port state.
359 @retval EFI_SUCCESS The status of the USB root hub port specified.
360 by PortNumber was returned in PortStatus.
361 @retval EFI_INVALID_PARAMETER PortNumber is invalid.
362 @retval EFI_DEVICE_ERROR Can't read register.
367 XhcGetRootHubPortStatus (
368 IN EFI_USB2_HC_PROTOCOL
*This
,
370 OUT EFI_USB_PORT_STATUS
*PortStatus
373 USB_XHCI_INSTANCE
*Xhc
;
380 USB_DEV_ROUTE ParentRouteChart
;
383 if (PortStatus
== NULL
) {
384 return EFI_INVALID_PARAMETER
;
387 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
389 Xhc
= XHC_FROM_THIS (This
);
390 Status
= EFI_SUCCESS
;
392 TotalPort
= Xhc
->HcSParams1
.Data
.MaxPorts
;
394 if (PortNumber
>= TotalPort
) {
395 Status
= EFI_INVALID_PARAMETER
;
399 Offset
= (UINT32
) (XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
400 PortStatus
->PortStatus
= 0;
401 PortStatus
->PortChangeStatus
= 0;
403 State
= XhcReadOpReg (Xhc
, Offset
);
406 // According to XHCI 1.0 spec, bit 10~13 of the root port status register identifies the speed of the attached device.
408 switch ((State
& XHC_PORTSC_PS
) >> 10) {
410 PortStatus
->PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
414 PortStatus
->PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
418 PortStatus
->PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
426 // Convert the XHCI port/port change state to UEFI status
428 MapSize
= sizeof (mUsbPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
430 for (Index
= 0; Index
< MapSize
; Index
++) {
431 if (XHC_BIT_IS_SET (State
, mUsbPortStateMap
[Index
].HwState
)) {
432 PortStatus
->PortStatus
= (UINT16
) (PortStatus
->PortStatus
| mUsbPortStateMap
[Index
].UefiState
);
436 // Bit5~8 reflects its current link state.
438 if ((State
& XHC_PORTSC_PLS
) >> 5 == 3) {
439 PortStatus
->PortStatus
|= USB_PORT_STAT_SUSPEND
;
442 MapSize
= sizeof (mUsbPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
444 for (Index
= 0; Index
< MapSize
; Index
++) {
445 if (XHC_BIT_IS_SET (State
, mUsbPortChangeMap
[Index
].HwState
)) {
446 PortStatus
->PortChangeStatus
= (UINT16
) (PortStatus
->PortChangeStatus
| mUsbPortChangeMap
[Index
].UefiState
);
450 MapSize
= sizeof (mUsbClearPortChangeMap
) / sizeof (USB_CLEAR_PORT_MAP
);
452 for (Index
= 0; Index
< MapSize
; Index
++) {
453 if (XHC_BIT_IS_SET (State
, mUsbClearPortChangeMap
[Index
].HwState
)) {
454 XhcClearRootHubPortFeature (This
, PortNumber
, (EFI_USB_PORT_FEATURE
)mUsbClearPortChangeMap
[Index
].Selector
);
459 // Poll the root port status register to enable/disable corresponding device slot if there is a device attached/detached.
460 // For those devices behind hub, we get its attach/detach event by hooking Get_Port_Status request at control transfer for those hub.
462 ParentRouteChart
.Dword
= 0;
463 XhcPollPortStatusChange (Xhc
, ParentRouteChart
, PortNumber
, PortStatus
);
466 gBS
->RestoreTPL (OldTpl
);
472 Sets a feature for the specified root hub port.
474 @param This This EFI_USB2_HC_PROTOCOL instance.
475 @param PortNumber Root hub port to set.
476 @param PortFeature Feature to set.
478 @retval EFI_SUCCESS The feature specified by PortFeature was set.
479 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
480 @retval EFI_DEVICE_ERROR Can't read register.
485 XhcSetRootHubPortFeature (
486 IN EFI_USB2_HC_PROTOCOL
*This
,
488 IN EFI_USB_PORT_FEATURE PortFeature
491 USB_XHCI_INSTANCE
*Xhc
;
498 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
500 Xhc
= XHC_FROM_THIS (This
);
501 Status
= EFI_SUCCESS
;
503 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
505 if (PortNumber
>= TotalPort
) {
506 Status
= EFI_INVALID_PARAMETER
;
510 Offset
= (UINT32
) (XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
));
511 State
= XhcReadOpReg (Xhc
, Offset
);
514 // Mask off the port status change bits, these bits are
517 State
&= ~ (BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
519 switch (PortFeature
) {
520 case EfiUsbPortEnable
:
522 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
523 // A port may be disabled by software writing a '1' to this flag.
525 Status
= EFI_SUCCESS
;
528 case EfiUsbPortSuspend
:
529 State
|= XHC_PORTSC_LWS
;
530 XhcWriteOpReg (Xhc
, Offset
, State
);
531 State
&= ~XHC_PORTSC_PLS
;
533 XhcWriteOpReg (Xhc
, Offset
, State
);
536 case EfiUsbPortReset
:
537 DEBUG ((EFI_D_INFO
, "XhcUsbPortReset!\n"));
539 // Make sure Host Controller not halt before reset it
541 if (XhcIsHalt (Xhc
)) {
542 Status
= XhcRunHC (Xhc
, XHC_GENERIC_TIMEOUT
);
544 if (EFI_ERROR (Status
)) {
545 DEBUG ((EFI_D_INFO
, "XhcSetRootHubPortFeature :failed to start HC - %r\n", Status
));
551 // 4.3.1 Resetting a Root Hub Port
552 // 1) Write the PORTSC register with the Port Reset (PR) bit set to '1'.
554 State
|= XHC_PORTSC_RESET
;
555 XhcWriteOpReg (Xhc
, Offset
, State
);
556 XhcWaitOpRegBit(Xhc
, Offset
, XHC_PORTSC_PRC
, TRUE
, XHC_GENERIC_TIMEOUT
);
559 case EfiUsbPortPower
:
561 // Not supported, ignore the operation
563 Status
= EFI_SUCCESS
;
566 case EfiUsbPortOwner
:
568 // XHCI root hub port don't has the owner bit, ignore the operation
570 Status
= EFI_SUCCESS
;
574 Status
= EFI_INVALID_PARAMETER
;
578 DEBUG ((EFI_D_INFO
, "XhcSetRootHubPortFeature: status %r\n", Status
));
579 gBS
->RestoreTPL (OldTpl
);
586 Clears a feature for the specified root hub port.
588 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
589 @param PortNumber Specifies the root hub port whose feature is
590 requested to be cleared.
591 @param PortFeature Indicates the feature selector associated with the
592 feature clear request.
594 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
595 for the USB root hub port specified by PortNumber.
596 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
597 @retval EFI_DEVICE_ERROR Can't read register.
602 XhcClearRootHubPortFeature (
603 IN EFI_USB2_HC_PROTOCOL
*This
,
605 IN EFI_USB_PORT_FEATURE PortFeature
608 USB_XHCI_INSTANCE
*Xhc
;
615 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
617 Xhc
= XHC_FROM_THIS (This
);
618 Status
= EFI_SUCCESS
;
620 TotalPort
= (Xhc
->HcSParams1
.Data
.MaxPorts
);
622 if (PortNumber
>= TotalPort
) {
623 Status
= EFI_INVALID_PARAMETER
;
627 Offset
= XHC_PORTSC_OFFSET
+ (0x10 * PortNumber
);
630 // Mask off the port status change bits, these bits are
633 State
= XhcReadOpReg (Xhc
, Offset
);
634 State
&= ~ (BIT1
| BIT17
| BIT18
| BIT19
| BIT20
| BIT21
| BIT22
| BIT23
);
636 switch (PortFeature
) {
637 case EfiUsbPortEnable
:
639 // Ports may only be enabled by the xHC. Software cannot enable a port by writing a '1' to this flag.
640 // A port may be disabled by software writing a '1' to this flag.
642 State
|= XHC_PORTSC_PED
;
643 State
&= ~XHC_PORTSC_RESET
;
644 XhcWriteOpReg (Xhc
, Offset
, State
);
647 case EfiUsbPortSuspend
:
648 State
|= XHC_PORTSC_LWS
;
649 XhcWriteOpReg (Xhc
, Offset
, State
);
650 State
&= ~XHC_PORTSC_PLS
;
651 XhcWriteOpReg (Xhc
, Offset
, State
);
654 case EfiUsbPortReset
:
656 // PORTSC_RESET BIT(4) bit is RW1S attribute, which means Write-1-to-set status:
657 // Register bits indicate status when read, a clear bit may be set by
658 // writing a '1'. Writing a '0' to RW1S bits has no effect.
662 case EfiUsbPortOwner
:
664 // XHCI root hub port don't has the owner bit, ignore the operation
668 case EfiUsbPortConnectChange
:
670 // Clear connect status change
672 State
|= XHC_PORTSC_CSC
;
673 XhcWriteOpReg (Xhc
, Offset
, State
);
676 case EfiUsbPortEnableChange
:
678 // Clear enable status change
680 State
|= XHC_PORTSC_PEC
;
681 XhcWriteOpReg (Xhc
, Offset
, State
);
684 case EfiUsbPortOverCurrentChange
:
686 // Clear PortOverCurrent change
688 State
|= XHC_PORTSC_OCC
;
689 XhcWriteOpReg (Xhc
, Offset
, State
);
692 case EfiUsbPortResetChange
:
694 // Clear Port Reset change
696 State
|= XHC_PORTSC_PRC
;
697 XhcWriteOpReg (Xhc
, Offset
, State
);
700 case EfiUsbPortPower
:
701 case EfiUsbPortSuspendChange
:
703 // Not supported or not related operation
708 Status
= EFI_INVALID_PARAMETER
;
713 DEBUG ((EFI_D_INFO
, "XhcClearRootHubPortFeature: status %r\n", Status
));
714 gBS
->RestoreTPL (OldTpl
);
721 Submits control transfer to a target USB device.
723 @param This This EFI_USB2_HC_PROTOCOL instance.
724 @param DeviceAddress The target device address.
725 @param DeviceSpeed Target device speed.
726 @param MaximumPacketLength Maximum packet size the default control transfer
727 endpoint is capable of sending or receiving.
728 @param Request USB device request to send.
729 @param TransferDirection Specifies the data direction for the data stage
730 @param Data Data buffer to be transmitted or received from USB
732 @param DataLength The size (in bytes) of the data buffer.
733 @param Timeout Indicates the maximum timeout, in millisecond.
734 @param Translator Transaction translator to be used by this device.
735 @param TransferResult Return the result of this control transfer.
737 @retval EFI_SUCCESS Transfer was completed successfully.
738 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
739 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
740 @retval EFI_TIMEOUT Transfer failed due to timeout.
741 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
747 IN EFI_USB2_HC_PROTOCOL
*This
,
748 IN UINT8 DeviceAddress
,
749 IN UINT8 DeviceSpeed
,
750 IN UINTN MaximumPacketLength
,
751 IN EFI_USB_DEVICE_REQUEST
*Request
,
752 IN EFI_USB_DATA_DIRECTION TransferDirection
,
754 IN OUT UINTN
*DataLength
,
756 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
757 OUT UINT32
*TransferResult
760 USB_XHCI_INSTANCE
*Xhc
;
764 UINT8 DescriptorType
;
769 EFI_USB_HUB_DESCRIPTOR
*HubDesc
;
772 EFI_STATUS RecoveryStatus
;
774 EFI_USB_PORT_STATUS PortStatus
;
776 EFI_USB_DEVICE_REQUEST ClearPortRequest
;
780 // Validate parameters
782 if ((Request
== NULL
) || (TransferResult
== NULL
)) {
783 return EFI_INVALID_PARAMETER
;
786 if ((TransferDirection
!= EfiUsbDataIn
) &&
787 (TransferDirection
!= EfiUsbDataOut
) &&
788 (TransferDirection
!= EfiUsbNoData
)) {
789 return EFI_INVALID_PARAMETER
;
792 if ((TransferDirection
== EfiUsbNoData
) &&
793 ((Data
!= NULL
) || (*DataLength
!= 0))) {
794 return EFI_INVALID_PARAMETER
;
797 if ((TransferDirection
!= EfiUsbNoData
) &&
798 ((Data
== NULL
) || (*DataLength
== 0))) {
799 return EFI_INVALID_PARAMETER
;
802 if ((MaximumPacketLength
!= 8) && (MaximumPacketLength
!= 16) &&
803 (MaximumPacketLength
!= 32) && (MaximumPacketLength
!= 64) &&
804 (MaximumPacketLength
!= 512)
806 return EFI_INVALID_PARAMETER
;
809 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) {
810 return EFI_INVALID_PARAMETER
;
813 if ((DeviceSpeed
== EFI_USB_SPEED_SUPER
) && (MaximumPacketLength
!= 512)) {
814 return EFI_INVALID_PARAMETER
;
817 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
819 Xhc
= XHC_FROM_THIS (This
);
821 Status
= EFI_DEVICE_ERROR
;
822 *TransferResult
= EFI_USB_ERR_SYSTEM
;
825 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
826 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: HC halted at entrance\n"));
831 // Check if the device is still enabled before every transaction.
833 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
839 // Hook the Set_Address request from UsbBus.
840 // According to XHCI 1.0 spec, the Set_Address request is replaced by XHCI's Address_Device cmd.
842 if ((Request
->Request
== USB_REQ_SET_ADDRESS
) &&
843 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
845 // Reset the BusDevAddr field of all disabled entries in UsbDevContext array firstly.
846 // This way is used to clean the history to avoid using wrong device address by XhcAsyncInterruptTransfer().
848 for (Index
= 0; Index
< 255; Index
++) {
849 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
&&
850 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0) &&
851 (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== (UINT8
)Request
->Value
)) {
852 Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
= 0;
856 if (Xhc
->UsbDevContext
[SlotId
].XhciDevAddr
== 0) {
857 Status
= EFI_DEVICE_ERROR
;
861 // The actual device address has been assigned by XHCI during initializing the device slot.
862 // So we just need establish the mapping relationship between the device address requested from UsbBus
863 // and the actual device address assigned by XHCI. The the following invocations through EFI_USB2_HC_PROTOCOL interface
864 // can find out the actual device address by it.
866 Xhc
->UsbDevContext
[SlotId
].BusDevAddr
= (UINT8
)Request
->Value
;
867 Status
= EFI_SUCCESS
;
872 // Create a new URB, insert it into the asynchronous
873 // schedule list, then poll the execution status.
874 // Note that we encode the direction in address although default control
875 // endpoint is bidirectional. XhcCreateUrb expects this
876 // combination of Ep addr and its direction.
878 Endpoint
= (UINT8
) (0 | ((TransferDirection
== EfiUsbDataIn
) ? 0x80 : 0));
894 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: failed to create URB"));
895 Status
= EFI_OUT_OF_RESOURCES
;
899 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
902 // Get the status from URB. The result is updated in XhcCheckUrbResult
903 // which is called by XhcExecTransfer
905 *TransferResult
= Urb
->Result
;
906 *DataLength
= Urb
->Completed
;
908 if (Status
== EFI_TIMEOUT
) {
910 // The transfer timed out. Abort the transfer by dequeueing of the TD.
912 RecoveryStatus
= XhcDequeueTrbFromEndpoint(Xhc
, Urb
);
913 if (EFI_ERROR(RecoveryStatus
)) {
914 DEBUG((EFI_D_ERROR
, "XhcControlTransfer: XhcDequeueTrbFromEndpoint failed\n"));
918 if (*TransferResult
== EFI_USB_NOERROR
) {
919 Status
= EFI_SUCCESS
;
920 } else if (*TransferResult
== EFI_USB_ERR_STALL
) {
921 RecoveryStatus
= XhcRecoverHaltedEndpoint(Xhc
, Urb
);
922 if (EFI_ERROR (RecoveryStatus
)) {
923 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: XhcRecoverHaltedEndpoint failed\n"));
925 Status
= EFI_DEVICE_ERROR
;
932 Xhc
->PciIo
->Flush (Xhc
->PciIo
);
934 if (Urb
->DataMap
!= NULL
) {
935 Status
= Xhc
->PciIo
->Unmap (Xhc
->PciIo
, Urb
->DataMap
);
936 ASSERT_EFI_ERROR (Status
);
937 if (EFI_ERROR (Status
)) {
938 Status
= EFI_DEVICE_ERROR
;
944 // Hook Get_Descriptor request from UsbBus as we need evaluate context and configure endpoint.
945 // Hook Get_Status request form UsbBus as we need trace device attach/detach event happened at hub.
946 // Hook Set_Config request from UsbBus as we need configure device endpoint.
948 if ((Request
->Request
== USB_REQ_GET_DESCRIPTOR
) &&
949 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
)) ||
950 ((Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_DEVICE
))))) {
951 DescriptorType
= (UINT8
)(Request
->Value
>> 8);
952 if ((DescriptorType
== USB_DESC_TYPE_DEVICE
) && ((*DataLength
== sizeof (EFI_USB_DEVICE_DESCRIPTOR
)) || ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (*DataLength
== 8)))) {
953 ASSERT (Data
!= NULL
);
955 // Store a copy of device scriptor as hub device need this info to configure endpoint.
957 CopyMem (&Xhc
->UsbDevContext
[SlotId
].DevDesc
, Data
, *DataLength
);
958 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.BcdUSB
>= 0x0300) {
960 // If it's a usb3.0 device, then its max packet size is a 2^n.
962 MaxPacket0
= 1 << Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
964 MaxPacket0
= Xhc
->UsbDevContext
[SlotId
].DevDesc
.MaxPacketSize0
;
966 Xhc
->UsbDevContext
[SlotId
].ConfDesc
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
* sizeof (EFI_USB_CONFIG_DESCRIPTOR
*));
967 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
968 Status
= XhcEvaluateContext (Xhc
, SlotId
, MaxPacket0
);
970 Status
= XhcEvaluateContext64 (Xhc
, SlotId
, MaxPacket0
);
972 } else if (DescriptorType
== USB_DESC_TYPE_CONFIG
) {
973 ASSERT (Data
!= NULL
);
974 if (*DataLength
== ((UINT16
*)Data
)[1]) {
976 // Get configuration value from request, Store the configuration descriptor for Configure_Endpoint cmd.
978 Index
= (UINT8
)Request
->Value
;
979 ASSERT (Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
);
980 Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
] = AllocateZeroPool(*DataLength
);
981 CopyMem (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
], Data
, *DataLength
);
983 // Default to use AlternateSetting 0 for all interfaces.
985 Xhc
->UsbDevContext
[SlotId
].ActiveAlternateSetting
= AllocateZeroPool (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->NumInterfaces
* sizeof (UINT8
));
987 } else if (((DescriptorType
== USB_DESC_TYPE_HUB
) ||
988 (DescriptorType
== USB_DESC_TYPE_HUB_SUPER_SPEED
)) && (*DataLength
> 2)) {
989 ASSERT (Data
!= NULL
);
990 HubDesc
= (EFI_USB_HUB_DESCRIPTOR
*)Data
;
991 ASSERT (HubDesc
->NumPorts
<= 15);
993 // The bit 5,6 of HubCharacter field of Hub Descriptor is TTT.
995 TTT
= (UINT8
)((HubDesc
->HubCharacter
& (BIT5
| BIT6
)) >> 5);
996 if (Xhc
->UsbDevContext
[SlotId
].DevDesc
.DeviceProtocol
== 2) {
998 // Don't support multi-TT feature for super speed hub now.
1001 DEBUG ((EFI_D_ERROR
, "XHCI: Don't support multi-TT feature for Hub now. (force to disable MTT)\n"));
1006 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1007 Status
= XhcConfigHubContext (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
1009 Status
= XhcConfigHubContext64 (Xhc
, SlotId
, HubDesc
->NumPorts
, TTT
, MTT
);
1012 } else if ((Request
->Request
== USB_REQ_SET_CONFIG
) &&
1013 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_DEVICE
))) {
1015 // Hook Set_Config request from UsbBus as we need configure device endpoint.
1017 for (Index
= 0; Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
; Index
++) {
1018 if (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]->ConfigurationValue
== (UINT8
)Request
->Value
) {
1019 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1020 Status
= XhcSetConfigCmd (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
1022 Status
= XhcSetConfigCmd64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
1027 } else if ((Request
->Request
== USB_REQ_SET_INTERFACE
) &&
1028 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
, USB_TARGET_INTERFACE
))) {
1030 // Hook Set_Interface request from UsbBus as we need configure interface setting.
1031 // Request->Value indicates AlterlateSetting to set
1032 // Request->Index indicates Interface to set
1034 if (Xhc
->UsbDevContext
[SlotId
].ActiveAlternateSetting
[(UINT8
) Request
->Index
] != (UINT8
) Request
->Value
) {
1035 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1036 Status
= XhcSetInterface (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Xhc
->UsbDevContext
[SlotId
].ActiveConfiguration
- 1], Request
);
1038 Status
= XhcSetInterface64 (Xhc
, SlotId
, DeviceSpeed
, Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Xhc
->UsbDevContext
[SlotId
].ActiveConfiguration
- 1], Request
);
1041 } else if ((Request
->Request
== USB_REQ_GET_STATUS
) &&
1042 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbDataIn
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
))) {
1043 ASSERT (Data
!= NULL
);
1045 // Hook Get_Status request from UsbBus to keep track of the port status change.
1047 State
= *(UINT32
*)Data
;
1048 PortStatus
.PortStatus
= 0;
1049 PortStatus
.PortChangeStatus
= 0;
1051 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
1053 // For super speed hub, its bit10~12 presents the attached device speed.
1055 if ((State
& XHC_PORTSC_PS
) >> 10 == 0) {
1056 PortStatus
.PortStatus
|= USB_PORT_STAT_SUPER_SPEED
;
1060 // For high or full/low speed hub, its bit9~10 presents the attached device speed.
1062 if (XHC_BIT_IS_SET (State
, BIT9
)) {
1063 PortStatus
.PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
1064 } else if (XHC_BIT_IS_SET (State
, BIT10
)) {
1065 PortStatus
.PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
1070 // Convert the XHCI port/port change state to UEFI status
1072 MapSize
= sizeof (mUsbHubPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
1073 for (Index
= 0; Index
< MapSize
; Index
++) {
1074 if (XHC_BIT_IS_SET (State
, mUsbHubPortStateMap
[Index
].HwState
)) {
1075 PortStatus
.PortStatus
= (UINT16
) (PortStatus
.PortStatus
| mUsbHubPortStateMap
[Index
].UefiState
);
1079 MapSize
= sizeof (mUsbHubPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
1080 for (Index
= 0; Index
< MapSize
; Index
++) {
1081 if (XHC_BIT_IS_SET (State
, mUsbHubPortChangeMap
[Index
].HwState
)) {
1082 PortStatus
.PortChangeStatus
= (UINT16
) (PortStatus
.PortChangeStatus
| mUsbHubPortChangeMap
[Index
].UefiState
);
1086 MapSize
= sizeof (mUsbHubClearPortChangeMap
) / sizeof (USB_CLEAR_PORT_MAP
);
1088 for (Index
= 0; Index
< MapSize
; Index
++) {
1089 if (XHC_BIT_IS_SET (State
, mUsbHubClearPortChangeMap
[Index
].HwState
)) {
1090 ZeroMem (&ClearPortRequest
, sizeof (EFI_USB_DEVICE_REQUEST
));
1091 ClearPortRequest
.RequestType
= USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_CLASS
, USB_TARGET_OTHER
);
1092 ClearPortRequest
.Request
= (UINT8
) USB_REQ_CLEAR_FEATURE
;
1093 ClearPortRequest
.Value
= mUsbHubClearPortChangeMap
[Index
].Selector
;
1094 ClearPortRequest
.Index
= Request
->Index
;
1095 ClearPortRequest
.Length
= 0;
1097 XhcControlTransfer (
1101 MaximumPacketLength
,
1113 XhcPollPortStatusChange (Xhc
, Xhc
->UsbDevContext
[SlotId
].RouteString
, (UINT8
)Request
->Index
, &PortStatus
);
1115 *(UINT32
*)Data
= *(UINT32
*)&PortStatus
;
1123 if (EFI_ERROR (Status
)) {
1124 DEBUG ((EFI_D_ERROR
, "XhcControlTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1127 gBS
->RestoreTPL (OldTpl
);
1134 Submits bulk transfer to a bulk endpoint of a USB device.
1136 @param This This EFI_USB2_HC_PROTOCOL instance.
1137 @param DeviceAddress Target device address.
1138 @param EndPointAddress Endpoint number and its direction in bit 7.
1139 @param DeviceSpeed Device speed, Low speed device doesn't support bulk
1141 @param MaximumPacketLength Maximum packet size the endpoint is capable of
1142 sending or receiving.
1143 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1144 @param Data Array of pointers to the buffers of data to transmit
1145 from or receive into.
1146 @param DataLength The lenght of the data buffer.
1147 @param DataToggle On input, the initial data toggle for the transfer;
1148 On output, it is updated to to next data toggle to
1149 use of the subsequent bulk transfer.
1150 @param Timeout Indicates the maximum time, in millisecond, which
1151 the transfer is allowed to complete.
1152 @param Translator A pointr to the transaction translator data.
1153 @param TransferResult A pointer to the detailed result information of the
1156 @retval EFI_SUCCESS The transfer was completed successfully.
1157 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1158 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1159 @retval EFI_TIMEOUT The transfer failed due to timeout.
1160 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1166 IN EFI_USB2_HC_PROTOCOL
*This
,
1167 IN UINT8 DeviceAddress
,
1168 IN UINT8 EndPointAddress
,
1169 IN UINT8 DeviceSpeed
,
1170 IN UINTN MaximumPacketLength
,
1171 IN UINT8 DataBuffersNumber
,
1172 IN OUT VOID
*Data
[EFI_USB_MAX_BULK_BUFFER_NUM
],
1173 IN OUT UINTN
*DataLength
,
1174 IN OUT UINT8
*DataToggle
,
1176 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1177 OUT UINT32
*TransferResult
1180 USB_XHCI_INSTANCE
*Xhc
;
1184 EFI_STATUS RecoveryStatus
;
1188 // Validate the parameters
1190 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1191 (Data
== NULL
) || (Data
[0] == NULL
) || (TransferResult
== NULL
)) {
1192 return EFI_INVALID_PARAMETER
;
1195 if ((*DataToggle
!= 0) && (*DataToggle
!= 1)) {
1196 return EFI_INVALID_PARAMETER
;
1199 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) ||
1200 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1201 ((EFI_USB_SPEED_HIGH
== DeviceSpeed
) && (MaximumPacketLength
> 512)) ||
1202 ((EFI_USB_SPEED_SUPER
== DeviceSpeed
) && (MaximumPacketLength
> 1024))) {
1203 return EFI_INVALID_PARAMETER
;
1206 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1208 Xhc
= XHC_FROM_THIS (This
);
1210 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1211 Status
= EFI_DEVICE_ERROR
;
1213 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1214 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: HC is halted\n"));
1219 // Check if the device is still enabled before every transaction.
1221 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1227 // Create a new URB, insert it into the asynchronous
1228 // schedule list, then poll the execution status.
1230 Urb
= XhcCreateUrb (
1235 MaximumPacketLength
,
1245 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: failed to create URB\n"));
1246 Status
= EFI_OUT_OF_RESOURCES
;
1250 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
1252 *TransferResult
= Urb
->Result
;
1253 *DataLength
= Urb
->Completed
;
1255 if (Status
== EFI_TIMEOUT
) {
1257 // The transfer timed out. Abort the transfer by dequeueing of the TD.
1259 RecoveryStatus
= XhcDequeueTrbFromEndpoint(Xhc
, Urb
);
1260 if (EFI_ERROR(RecoveryStatus
)) {
1261 DEBUG((EFI_D_ERROR
, "XhcBulkTransfer: XhcDequeueTrbFromEndpoint failed\n"));
1264 if (*TransferResult
== EFI_USB_NOERROR
) {
1265 Status
= EFI_SUCCESS
;
1266 } else if (*TransferResult
== EFI_USB_ERR_STALL
) {
1267 RecoveryStatus
= XhcRecoverHaltedEndpoint(Xhc
, Urb
);
1268 if (EFI_ERROR (RecoveryStatus
)) {
1269 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: XhcRecoverHaltedEndpoint failed\n"));
1271 Status
= EFI_DEVICE_ERROR
;
1275 Xhc
->PciIo
->Flush (Xhc
->PciIo
);
1276 XhcFreeUrb (Xhc
, Urb
);
1280 if (EFI_ERROR (Status
)) {
1281 DEBUG ((EFI_D_ERROR
, "XhcBulkTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1283 gBS
->RestoreTPL (OldTpl
);
1289 Submits an asynchronous interrupt transfer to an
1290 interrupt endpoint of a USB device.
1292 @param This This EFI_USB2_HC_PROTOCOL instance.
1293 @param DeviceAddress Target device address.
1294 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1295 @param DeviceSpeed Indicates device speed.
1296 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1297 @param IsNewTransfer If TRUE, to submit an new asynchronous interrupt
1298 transfer If FALSE, to remove the specified
1299 asynchronous interrupt.
1300 @param DataToggle On input, the initial data toggle to use; on output,
1301 it is updated to indicate the next data toggle.
1302 @param PollingInterval The he interval, in milliseconds, that the transfer
1304 @param DataLength The length of data to receive at the rate specified
1306 @param Translator Transaction translator to use.
1307 @param CallBackFunction Function to call at the rate specified by
1309 @param Context Context to CallBackFunction.
1311 @retval EFI_SUCCESS The request has been successfully submitted or canceled.
1312 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1313 @retval EFI_OUT_OF_RESOURCES The request failed due to a lack of resources.
1314 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
1319 XhcAsyncInterruptTransfer (
1320 IN EFI_USB2_HC_PROTOCOL
*This
,
1321 IN UINT8 DeviceAddress
,
1322 IN UINT8 EndPointAddress
,
1323 IN UINT8 DeviceSpeed
,
1324 IN UINTN MaximumPacketLength
,
1325 IN BOOLEAN IsNewTransfer
,
1326 IN OUT UINT8
*DataToggle
,
1327 IN UINTN PollingInterval
,
1328 IN UINTN DataLength
,
1329 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1330 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction
,
1331 IN VOID
*Context OPTIONAL
1334 USB_XHCI_INSTANCE
*Xhc
;
1343 // Validate parameters
1345 if (!XHCI_IS_DATAIN (EndPointAddress
)) {
1346 return EFI_INVALID_PARAMETER
;
1349 if (IsNewTransfer
) {
1350 if (DataLength
== 0) {
1351 return EFI_INVALID_PARAMETER
;
1354 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1355 return EFI_INVALID_PARAMETER
;
1358 if ((PollingInterval
> 255) || (PollingInterval
< 1)) {
1359 return EFI_INVALID_PARAMETER
;
1363 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1365 Xhc
= XHC_FROM_THIS (This
);
1368 // Delete Async interrupt transfer request.
1370 if (!IsNewTransfer
) {
1372 // The delete request may happen after device is detached.
1374 for (Index
= 0; Index
< 255; Index
++) {
1375 if (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== DeviceAddress
) {
1381 Status
= EFI_INVALID_PARAMETER
;
1385 Status
= XhciDelAsyncIntTransfer (Xhc
, DeviceAddress
, EndPointAddress
);
1386 DEBUG ((EFI_D_INFO
, "XhcAsyncInterruptTransfer: remove old transfer for addr %d, Status = %r\n", DeviceAddress
, Status
));
1390 Status
= EFI_SUCCESS
;
1392 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1393 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: HC is halt\n"));
1394 Status
= EFI_DEVICE_ERROR
;
1399 // Check if the device is still enabled before every transaction.
1401 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1406 Data
= AllocateZeroPool (DataLength
);
1409 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: failed to allocate buffer\n"));
1410 Status
= EFI_OUT_OF_RESOURCES
;
1414 Urb
= XhcCreateUrb (
1419 MaximumPacketLength
,
1420 XHC_INT_TRANSFER_ASYNC
,
1429 DEBUG ((EFI_D_ERROR
, "XhcAsyncInterruptTransfer: failed to create URB\n"));
1431 Status
= EFI_OUT_OF_RESOURCES
;
1435 InsertHeadList (&Xhc
->AsyncIntTransfers
, &Urb
->UrbList
);
1437 // Ring the doorbell
1439 Status
= RingIntTransferDoorBell (Xhc
, Urb
);
1442 Xhc
->PciIo
->Flush (Xhc
->PciIo
);
1443 gBS
->RestoreTPL (OldTpl
);
1450 Submits synchronous interrupt transfer to an interrupt endpoint
1453 @param This This EFI_USB2_HC_PROTOCOL instance.
1454 @param DeviceAddress Target device address.
1455 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1456 @param DeviceSpeed Indicates device speed.
1457 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1458 of sending or receiving.
1459 @param Data Buffer of data that will be transmitted to USB
1460 device or received from USB device.
1461 @param DataLength On input, the size, in bytes, of the data buffer; On
1462 output, the number of bytes transferred.
1463 @param DataToggle On input, the initial data toggle to use; on output,
1464 it is updated to indicate the next data toggle.
1465 @param Timeout Maximum time, in second, to complete.
1466 @param Translator Transaction translator to use.
1467 @param TransferResult Variable to receive the transfer result.
1469 @return EFI_SUCCESS The transfer was completed successfully.
1470 @return EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1471 @return EFI_INVALID_PARAMETER Some parameters are invalid.
1472 @return EFI_TIMEOUT The transfer failed due to timeout.
1473 @return EFI_DEVICE_ERROR The failed due to host controller or device error
1478 XhcSyncInterruptTransfer (
1479 IN EFI_USB2_HC_PROTOCOL
*This
,
1480 IN UINT8 DeviceAddress
,
1481 IN UINT8 EndPointAddress
,
1482 IN UINT8 DeviceSpeed
,
1483 IN UINTN MaximumPacketLength
,
1485 IN OUT UINTN
*DataLength
,
1486 IN OUT UINT8
*DataToggle
,
1488 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1489 OUT UINT32
*TransferResult
1492 USB_XHCI_INSTANCE
*Xhc
;
1496 EFI_STATUS RecoveryStatus
;
1500 // Validates parameters
1502 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1503 (Data
== NULL
) || (TransferResult
== NULL
)) {
1504 return EFI_INVALID_PARAMETER
;
1507 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1508 return EFI_INVALID_PARAMETER
;
1511 if (((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) ||
1512 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1513 ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) && (MaximumPacketLength
> 3072))) {
1514 return EFI_INVALID_PARAMETER
;
1517 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1519 Xhc
= XHC_FROM_THIS (This
);
1521 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1522 Status
= EFI_DEVICE_ERROR
;
1524 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1525 DEBUG ((EFI_D_ERROR
, "EhcSyncInterruptTransfer: HC is halt\n"));
1530 // Check if the device is still enabled before every transaction.
1532 SlotId
= XhcBusDevAddrToSlotId (Xhc
, DeviceAddress
);
1537 Urb
= XhcCreateUrb (
1542 MaximumPacketLength
,
1543 XHC_INT_TRANSFER_SYNC
,
1552 DEBUG ((EFI_D_ERROR
, "XhcSyncInterruptTransfer: failed to create URB\n"));
1553 Status
= EFI_OUT_OF_RESOURCES
;
1557 Status
= XhcExecTransfer (Xhc
, FALSE
, Urb
, Timeout
);
1559 *TransferResult
= Urb
->Result
;
1560 *DataLength
= Urb
->Completed
;
1562 if (Status
== EFI_TIMEOUT
) {
1564 // The transfer timed out. Abort the transfer by dequeueing of the TD.
1566 RecoveryStatus
= XhcDequeueTrbFromEndpoint(Xhc
, Urb
);
1567 if (EFI_ERROR(RecoveryStatus
)) {
1568 DEBUG((EFI_D_ERROR
, "XhcSyncInterruptTransfer: XhcDequeueTrbFromEndpoint failed\n"));
1571 if (*TransferResult
== EFI_USB_NOERROR
) {
1572 Status
= EFI_SUCCESS
;
1573 } else if (*TransferResult
== EFI_USB_ERR_STALL
) {
1574 RecoveryStatus
= XhcRecoverHaltedEndpoint(Xhc
, Urb
);
1575 if (EFI_ERROR (RecoveryStatus
)) {
1576 DEBUG ((EFI_D_ERROR
, "XhcSyncInterruptTransfer: XhcRecoverHaltedEndpoint failed\n"));
1578 Status
= EFI_DEVICE_ERROR
;
1582 Xhc
->PciIo
->Flush (Xhc
->PciIo
);
1583 XhcFreeUrb (Xhc
, Urb
);
1586 if (EFI_ERROR (Status
)) {
1587 DEBUG ((EFI_D_ERROR
, "XhcSyncInterruptTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1589 gBS
->RestoreTPL (OldTpl
);
1596 Submits isochronous transfer to a target USB device.
1598 @param This This EFI_USB2_HC_PROTOCOL instance.
1599 @param DeviceAddress Target device address.
1600 @param EndPointAddress End point address with its direction.
1601 @param DeviceSpeed Device speed, Low speed device doesn't support this
1603 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1604 sending or receiving.
1605 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1606 @param Data Array of pointers to the buffers of data that will
1607 be transmitted to USB device or received from USB
1609 @param DataLength The size, in bytes, of the data buffer.
1610 @param Translator Transaction translator to use.
1611 @param TransferResult Variable to receive the transfer result.
1613 @return EFI_UNSUPPORTED Isochronous transfer is unsupported.
1618 XhcIsochronousTransfer (
1619 IN EFI_USB2_HC_PROTOCOL
*This
,
1620 IN UINT8 DeviceAddress
,
1621 IN UINT8 EndPointAddress
,
1622 IN UINT8 DeviceSpeed
,
1623 IN UINTN MaximumPacketLength
,
1624 IN UINT8 DataBuffersNumber
,
1625 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1626 IN UINTN DataLength
,
1627 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1628 OUT UINT32
*TransferResult
1631 return EFI_UNSUPPORTED
;
1636 Submits Async isochronous transfer to a target USB device.
1638 @param This This EFI_USB2_HC_PROTOCOL instance.
1639 @param DeviceAddress Target device address.
1640 @param EndPointAddress End point address with its direction.
1641 @param DeviceSpeed Device speed, Low speed device doesn't support this
1643 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1644 sending or receiving.
1645 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1646 @param Data Array of pointers to the buffers of data that will
1647 be transmitted to USB device or received from USB
1649 @param DataLength The size, in bytes, of the data buffer.
1650 @param Translator Transaction translator to use.
1651 @param IsochronousCallBack Function to be called when the transfer complete.
1652 @param Context Context passed to the call back function as
1655 @return EFI_UNSUPPORTED Isochronous transfer isn't supported.
1660 XhcAsyncIsochronousTransfer (
1661 IN EFI_USB2_HC_PROTOCOL
*This
,
1662 IN UINT8 DeviceAddress
,
1663 IN UINT8 EndPointAddress
,
1664 IN UINT8 DeviceSpeed
,
1665 IN UINTN MaximumPacketLength
,
1666 IN UINT8 DataBuffersNumber
,
1667 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1668 IN UINTN DataLength
,
1669 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1670 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
1674 return EFI_UNSUPPORTED
;
1678 Entry point for EFI drivers.
1680 @param ImageHandle EFI_HANDLE.
1681 @param SystemTable EFI_SYSTEM_TABLE.
1683 @retval EFI_SUCCESS Success.
1684 @retval Others Fail.
1689 XhcDriverEntryPoint (
1690 IN EFI_HANDLE ImageHandle
,
1691 IN EFI_SYSTEM_TABLE
*SystemTable
1694 return EfiLibInstallDriverBindingComponentName2 (
1697 &gXhciDriverBinding
,
1699 &gXhciComponentName
,
1700 &gXhciComponentName2
1706 Test to see if this driver supports ControllerHandle. Any
1707 ControllerHandle that has Usb2HcProtocol installed will
1710 @param This Protocol instance pointer.
1711 @param Controller Handle of device to test.
1712 @param RemainingDevicePath Not used.
1714 @return EFI_SUCCESS This driver supports this device.
1715 @return EFI_UNSUPPORTED This driver does not support this device.
1720 XhcDriverBindingSupported (
1721 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1722 IN EFI_HANDLE Controller
,
1723 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1727 EFI_PCI_IO_PROTOCOL
*PciIo
;
1728 USB_CLASSC UsbClassCReg
;
1731 // Test whether there is PCI IO Protocol attached on the controller handle.
1733 Status
= gBS
->OpenProtocol (
1735 &gEfiPciIoProtocolGuid
,
1737 This
->DriverBindingHandle
,
1739 EFI_OPEN_PROTOCOL_BY_DRIVER
1742 if (EFI_ERROR (Status
)) {
1743 return EFI_UNSUPPORTED
;
1746 Status
= PciIo
->Pci
.Read (
1749 PCI_CLASSCODE_OFFSET
,
1750 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1754 if (EFI_ERROR (Status
)) {
1755 Status
= EFI_UNSUPPORTED
;
1760 // Test whether the controller belongs to Xhci type
1762 if ((UsbClassCReg
.BaseCode
!= PCI_CLASS_SERIAL
) ||
1763 (UsbClassCReg
.SubClassCode
!= PCI_CLASS_SERIAL_USB
) ||
1764 (UsbClassCReg
.ProgInterface
!= PCI_IF_XHCI
)) {
1765 Status
= EFI_UNSUPPORTED
;
1769 gBS
->CloseProtocol (
1771 &gEfiPciIoProtocolGuid
,
1772 This
->DriverBindingHandle
,
1780 Create and initialize a USB_XHCI_INSTANCE structure.
1782 @param PciIo The PciIo on this device.
1783 @param DevicePath The device path of host controller.
1784 @param OriginalPciAttributes Original PCI attributes.
1786 @return The allocated and initialized USB_XHCI_INSTANCE structure if created,
1792 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1793 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1794 IN UINT64 OriginalPciAttributes
1797 USB_XHCI_INSTANCE
*Xhc
;
1802 Xhc
= AllocateZeroPool (sizeof (USB_XHCI_INSTANCE
));
1809 // Initialize private data structure
1811 Xhc
->Signature
= XHCI_INSTANCE_SIG
;
1813 Xhc
->DevicePath
= DevicePath
;
1814 Xhc
->OriginalPciAttributes
= OriginalPciAttributes
;
1815 CopyMem (&Xhc
->Usb2Hc
, &gXhciUsb2HcTemplate
, sizeof (EFI_USB2_HC_PROTOCOL
));
1817 InitializeListHead (&Xhc
->AsyncIntTransfers
);
1820 // Be caution that the Offset passed to XhcReadCapReg() should be Dword align
1822 Xhc
->CapLength
= XhcReadCapReg8 (Xhc
, XHC_CAPLENGTH_OFFSET
);
1823 Xhc
->HcSParams1
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS1_OFFSET
);
1824 Xhc
->HcSParams2
.Dword
= XhcReadCapReg (Xhc
, XHC_HCSPARAMS2_OFFSET
);
1825 Xhc
->HcCParams
.Dword
= XhcReadCapReg (Xhc
, XHC_HCCPARAMS_OFFSET
);
1826 Xhc
->DBOff
= XhcReadCapReg (Xhc
, XHC_DBOFF_OFFSET
);
1827 Xhc
->RTSOff
= XhcReadCapReg (Xhc
, XHC_RTSOFF_OFFSET
);
1830 // This PageSize field defines the page size supported by the xHC implementation.
1831 // This xHC supports a page size of 2^(n+12) if bit n is Set. For example,
1832 // if bit 0 is Set, the xHC supports 4k byte page sizes.
1834 PageSize
= XhcReadOpReg(Xhc
, XHC_PAGESIZE_OFFSET
) & XHC_PAGESIZE_MASK
;
1835 Xhc
->PageSize
= 1 << (HighBitSet32(PageSize
) + 12);
1837 ExtCapReg
= (UINT16
) (Xhc
->HcCParams
.Data
.ExtCapReg
);
1838 Xhc
->ExtCapRegBase
= ExtCapReg
<< 2;
1839 Xhc
->UsbLegSupOffset
= XhcGetCapabilityAddr (Xhc
, XHC_CAP_USB_LEGACY
);
1840 Xhc
->DebugCapSupOffset
= XhcGetCapabilityAddr (Xhc
, XHC_CAP_USB_DEBUG
);
1842 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: Capability length 0x%x\n", Xhc
->CapLength
));
1843 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcSParams1 0x%x\n", Xhc
->HcSParams1
));
1844 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcSParams2 0x%x\n", Xhc
->HcSParams2
));
1845 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: HcCParams 0x%x\n", Xhc
->HcCParams
));
1846 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: DBOff 0x%x\n", Xhc
->DBOff
));
1847 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: RTSOff 0x%x\n", Xhc
->RTSOff
));
1848 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: UsbLegSupOffset 0x%x\n", Xhc
->UsbLegSupOffset
));
1849 DEBUG ((EFI_D_INFO
, "XhcCreateUsb3Hc: DebugCapSupOffset 0x%x\n", Xhc
->DebugCapSupOffset
));
1852 // Create AsyncRequest Polling Timer
1854 Status
= gBS
->CreateEvent (
1855 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1857 XhcMonitorAsyncRequests
,
1862 if (EFI_ERROR (Status
)) {
1874 One notified function to stop the Host Controller when gBS->ExitBootServices() called.
1876 @param Event Pointer to this event
1877 @param Context Event handler private data
1882 XhcExitBootService (
1888 USB_XHCI_INSTANCE
*Xhc
;
1889 EFI_PCI_IO_PROTOCOL
*PciIo
;
1891 Xhc
= (USB_XHCI_INSTANCE
*) Context
;
1895 // Stop AsyncRequest Polling timer then stop the XHCI driver
1896 // and uninstall the XHCI protocl.
1898 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
1899 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
1901 if (Xhc
->PollTimer
!= NULL
) {
1902 gBS
->CloseEvent (Xhc
->PollTimer
);
1905 XhcClearBiosOwnership (Xhc
);
1908 // Restore original PCI attributes
1912 EfiPciIoAttributeOperationSet
,
1913 Xhc
->OriginalPciAttributes
,
1919 Starting the Usb XHCI Driver.
1921 @param This Protocol instance pointer.
1922 @param Controller Handle of device to test.
1923 @param RemainingDevicePath Not used.
1925 @return EFI_SUCCESS supports this device.
1926 @return EFI_UNSUPPORTED do not support this device.
1927 @return EFI_DEVICE_ERROR cannot be started due to device Error.
1928 @return EFI_OUT_OF_RESOURCES cannot allocate resources.
1933 XhcDriverBindingStart (
1934 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1935 IN EFI_HANDLE Controller
,
1936 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1940 EFI_PCI_IO_PROTOCOL
*PciIo
;
1942 UINT64 OriginalPciAttributes
;
1943 BOOLEAN PciAttributesSaved
;
1944 USB_XHCI_INSTANCE
*Xhc
;
1945 EFI_DEVICE_PATH_PROTOCOL
*HcDevicePath
;
1948 // Open the PciIo Protocol, then enable the USB host controller
1950 Status
= gBS
->OpenProtocol (
1952 &gEfiPciIoProtocolGuid
,
1954 This
->DriverBindingHandle
,
1956 EFI_OPEN_PROTOCOL_BY_DRIVER
1959 if (EFI_ERROR (Status
)) {
1964 // Open Device Path Protocol for on USB host controller
1966 HcDevicePath
= NULL
;
1967 Status
= gBS
->OpenProtocol (
1969 &gEfiDevicePathProtocolGuid
,
1970 (VOID
**) &HcDevicePath
,
1971 This
->DriverBindingHandle
,
1973 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1976 PciAttributesSaved
= FALSE
;
1978 // Save original PCI attributes
1980 Status
= PciIo
->Attributes (
1982 EfiPciIoAttributeOperationGet
,
1984 &OriginalPciAttributes
1987 if (EFI_ERROR (Status
)) {
1990 PciAttributesSaved
= TRUE
;
1992 Status
= PciIo
->Attributes (
1994 EfiPciIoAttributeOperationSupported
,
1998 if (!EFI_ERROR (Status
)) {
1999 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
2000 Status
= PciIo
->Attributes (
2002 EfiPciIoAttributeOperationEnable
,
2008 if (EFI_ERROR (Status
)) {
2009 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to enable controller\n"));
2014 // Create then install USB2_HC_PROTOCOL
2016 Xhc
= XhcCreateUsbHc (PciIo
, HcDevicePath
, OriginalPciAttributes
);
2019 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to create USB2_HC\n"));
2020 return EFI_OUT_OF_RESOURCES
;
2024 // Enable 64-bit DMA support in the PCI layer if this controller
2027 if (Xhc
->HcCParams
.Data
.Ac64
!= 0) {
2028 Status
= PciIo
->Attributes (
2030 EfiPciIoAttributeOperationEnable
,
2031 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
2034 if (!EFI_ERROR (Status
)) {
2035 Xhc
->Support64BitDma
= TRUE
;
2038 "%a: failed to enable 64-bit DMA on 64-bit capable controller @ %p (%r)\n",
2039 __FUNCTION__
, Controller
, Status
));
2043 XhcSetBiosOwnership (Xhc
);
2045 XhcResetHC (Xhc
, XHC_RESET_TIMEOUT
);
2046 ASSERT (XhcIsHalt (Xhc
));
2049 // After Chip Hardware Reset wait until the Controller Not Ready (CNR) flag
2050 // in the USBSTS is '0' before writing any xHC Operational or Runtime registers.
2052 ASSERT (!(XHC_REG_BIT_IS_SET (Xhc
, XHC_USBSTS_OFFSET
, XHC_USBSTS_CNR
)));
2055 // Initialize the schedule
2060 // Start the Host Controller
2062 XhcRunHC(Xhc
, XHC_GENERIC_TIMEOUT
);
2065 // Start the asynchronous interrupt monitor
2067 Status
= gBS
->SetTimer (Xhc
->PollTimer
, TimerPeriodic
, XHC_ASYNC_TIMER_INTERVAL
);
2068 if (EFI_ERROR (Status
)) {
2069 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to start async interrupt monitor\n"));
2070 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2075 // Create event to stop the HC when exit boot service.
2077 Status
= gBS
->CreateEventEx (
2082 &gEfiEventExitBootServicesGuid
,
2083 &Xhc
->ExitBootServiceEvent
2085 if (EFI_ERROR (Status
)) {
2090 // Install the component name protocol, don't fail the start
2091 // because of something for display.
2095 gXhciComponentName
.SupportedLanguages
,
2096 &Xhc
->ControllerNameTable
,
2097 L
"eXtensible Host Controller (USB 3.0)",
2102 gXhciComponentName2
.SupportedLanguages
,
2103 &Xhc
->ControllerNameTable
,
2104 L
"eXtensible Host Controller (USB 3.0)",
2108 Status
= gBS
->InstallProtocolInterface (
2110 &gEfiUsb2HcProtocolGuid
,
2111 EFI_NATIVE_INTERFACE
,
2114 if (EFI_ERROR (Status
)) {
2115 DEBUG ((EFI_D_ERROR
, "XhcDriverBindingStart: failed to install USB2_HC Protocol\n"));
2119 DEBUG ((EFI_D_INFO
, "XhcDriverBindingStart: XHCI started for controller @ %x\n", Controller
));
2123 gBS
->CloseEvent (Xhc
->PollTimer
);
2128 if (PciAttributesSaved
) {
2130 // Restore original PCI attributes
2134 EfiPciIoAttributeOperationSet
,
2135 OriginalPciAttributes
,
2140 gBS
->CloseProtocol (
2142 &gEfiPciIoProtocolGuid
,
2143 This
->DriverBindingHandle
,
2152 Stop this driver on ControllerHandle. Support stoping any child handles
2153 created by this driver.
2155 @param This Protocol instance pointer.
2156 @param Controller Handle of device to stop driver on.
2157 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
2158 @param ChildHandleBuffer List of handles for the children we need to stop.
2160 @return EFI_SUCCESS Success.
2161 @return EFI_DEVICE_ERROR Fail.
2166 XhcDriverBindingStop (
2167 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
2168 IN EFI_HANDLE Controller
,
2169 IN UINTN NumberOfChildren
,
2170 IN EFI_HANDLE
*ChildHandleBuffer
2174 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
2175 EFI_PCI_IO_PROTOCOL
*PciIo
;
2176 USB_XHCI_INSTANCE
*Xhc
;
2180 // Test whether the Controller handler passed in is a valid
2181 // Usb controller handle that should be supported, if not,
2182 // return the error status directly
2184 Status
= gBS
->OpenProtocol (
2186 &gEfiUsb2HcProtocolGuid
,
2188 This
->DriverBindingHandle
,
2190 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2193 if (EFI_ERROR (Status
)) {
2197 Status
= gBS
->UninstallProtocolInterface (
2199 &gEfiUsb2HcProtocolGuid
,
2203 if (EFI_ERROR (Status
)) {
2207 Xhc
= XHC_FROM_THIS (Usb2Hc
);
2211 // Stop AsyncRequest Polling timer then stop the XHCI driver
2212 // and uninstall the XHCI protocl.
2214 gBS
->SetTimer (Xhc
->PollTimer
, TimerCancel
, 0);
2217 // Disable the device slots occupied by these devices on its downstream ports.
2218 // Entry 0 is reserved.
2220 for (Index
= 0; Index
< 255; Index
++) {
2221 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
||
2222 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0)) {
2225 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
2226 XhcDisableSlotCmd (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2228 XhcDisableSlotCmd64 (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2232 if (Xhc
->PollTimer
!= NULL
) {
2233 gBS
->CloseEvent (Xhc
->PollTimer
);
2236 if (Xhc
->ExitBootServiceEvent
!= NULL
) {
2237 gBS
->CloseEvent (Xhc
->ExitBootServiceEvent
);
2240 XhcHaltHC (Xhc
, XHC_GENERIC_TIMEOUT
);
2241 XhcClearBiosOwnership (Xhc
);
2242 XhciDelAllAsyncIntTransfers (Xhc
);
2245 if (Xhc
->ControllerNameTable
) {
2246 FreeUnicodeStringTable (Xhc
->ControllerNameTable
);
2250 // Restore original PCI attributes
2254 EfiPciIoAttributeOperationSet
,
2255 Xhc
->OriginalPciAttributes
,
2259 gBS
->CloseProtocol (
2261 &gEfiPciIoProtocolGuid
,
2262 This
->DriverBindingHandle
,