2 The Ehci controller driver.
4 EhciDxe driver is responsible for managing the behavior of EHCI controller.
5 It implements the interfaces of monitoring the status of all ports and transferring
6 Control, Bulk, Interrupt and Isochronous requests to Usb2.0 device.
8 Note that EhciDxe driver is enhanced to guarantee that the EHCI controller get attached
9 to the EHCI controller before a UHCI or OHCI driver attaches to the companion UHCI or
10 OHCI controller. This way avoids the control transfer on a shared port between EHCI
11 and companion host controller when UHCI or OHCI gets attached earlier than EHCI and a
12 USB 2.0 device inserts.
14 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
15 This program and the accompanying materials
16 are licensed and made available under the terms and conditions of the BSD License
17 which accompanies this distribution. The full text of the license may be found at
18 http://opensource.org/licenses/bsd-license.php
20 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
21 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
29 // Two arrays used to translate the EHCI port state (change)
30 // to the UEFI protocol's port state (change).
32 USB_PORT_STATE_MAP mUsbPortStateMap
[] = {
33 {PORTSC_CONN
, USB_PORT_STAT_CONNECTION
},
34 {PORTSC_ENABLED
, USB_PORT_STAT_ENABLE
},
35 {PORTSC_SUSPEND
, USB_PORT_STAT_SUSPEND
},
36 {PORTSC_OVERCUR
, USB_PORT_STAT_OVERCURRENT
},
37 {PORTSC_RESET
, USB_PORT_STAT_RESET
},
38 {PORTSC_POWER
, USB_PORT_STAT_POWER
},
39 {PORTSC_OWNER
, USB_PORT_STAT_OWNER
}
42 USB_PORT_STATE_MAP mUsbPortChangeMap
[] = {
43 {PORTSC_CONN_CHANGE
, USB_PORT_STAT_C_CONNECTION
},
44 {PORTSC_ENABLE_CHANGE
, USB_PORT_STAT_C_ENABLE
},
45 {PORTSC_OVERCUR_CHANGE
, USB_PORT_STAT_C_OVERCURRENT
}
48 EFI_DRIVER_BINDING_PROTOCOL
49 gEhciDriverBinding
= {
50 EhcDriverBindingSupported
,
51 EhcDriverBindingStart
,
59 Retrieves the capability of root hub ports.
61 @param This This EFI_USB_HC_PROTOCOL instance.
62 @param MaxSpeed Max speed supported by the controller.
63 @param PortNumber Number of the root hub ports.
64 @param Is64BitCapable Whether the controller supports 64-bit memory
67 @retval EFI_SUCCESS Host controller capability were retrieved successfully.
68 @retval EFI_INVALID_PARAMETER Either of the three capability pointer is NULL.
74 IN EFI_USB2_HC_PROTOCOL
*This
,
76 OUT UINT8
*PortNumber
,
77 OUT UINT8
*Is64BitCapable
83 if ((MaxSpeed
== NULL
) || (PortNumber
== NULL
) || (Is64BitCapable
== NULL
)) {
84 return EFI_INVALID_PARAMETER
;
87 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
88 Ehc
= EHC_FROM_THIS (This
);
90 *MaxSpeed
= EFI_USB_SPEED_HIGH
;
91 *PortNumber
= (UINT8
) (Ehc
->HcStructParams
& HCSP_NPORTS
);
92 *Is64BitCapable
= (UINT8
) Ehc
->Support64BitDma
;
94 DEBUG ((EFI_D_INFO
, "EhcGetCapability: %d ports, 64 bit %d\n", *PortNumber
, *Is64BitCapable
));
96 gBS
->RestoreTPL (OldTpl
);
102 Provides software reset for the USB host controller.
104 @param This This EFI_USB2_HC_PROTOCOL instance.
105 @param Attributes A bit mask of the reset operation to perform.
107 @retval EFI_SUCCESS The reset operation succeeded.
108 @retval EFI_INVALID_PARAMETER Attributes is not valid.
109 @retval EFI_UNSUPPOURTED The type of reset specified by Attributes is
110 not currently supported by the host controller.
111 @retval EFI_DEVICE_ERROR Host controller isn't halted to reset.
117 IN EFI_USB2_HC_PROTOCOL
*This
,
125 Ehc
= EHC_FROM_THIS (This
);
127 if (Ehc
->DevicePath
!= NULL
) {
129 // Report Status Code to indicate reset happens
131 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
133 (EFI_IO_BUS_USB
| EFI_IOB_PC_RESET
),
138 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
140 switch (Attributes
) {
141 case EFI_USB_HC_RESET_GLOBAL
:
143 // Flow through, same behavior as Host Controller Reset
145 case EFI_USB_HC_RESET_HOST_CONTROLLER
:
147 // Host Controller must be Halt when Reset it
149 if (EhcIsDebugPortInUse (Ehc
, NULL
)) {
150 Status
= EFI_SUCCESS
;
154 if (!EhcIsHalt (Ehc
)) {
155 Status
= EhcHaltHC (Ehc
, EHC_GENERIC_TIMEOUT
);
157 if (EFI_ERROR (Status
)) {
158 Status
= EFI_DEVICE_ERROR
;
164 // Clean up the asynchronous transfers, currently only
165 // interrupt supports asynchronous operation.
167 EhciDelAllAsyncIntTransfers (Ehc
);
168 EhcAckAllInterrupt (Ehc
);
171 Status
= EhcResetHC (Ehc
, EHC_RESET_TIMEOUT
);
173 if (EFI_ERROR (Status
)) {
177 Status
= EhcInitHC (Ehc
);
180 case EFI_USB_HC_RESET_GLOBAL_WITH_DEBUG
:
181 case EFI_USB_HC_RESET_HOST_WITH_DEBUG
:
182 Status
= EFI_UNSUPPORTED
;
186 Status
= EFI_INVALID_PARAMETER
;
190 DEBUG ((EFI_D_INFO
, "EhcReset: exit status %r\n", Status
));
191 gBS
->RestoreTPL (OldTpl
);
197 Retrieve the current state of the USB host controller.
199 @param This This EFI_USB2_HC_PROTOCOL instance.
200 @param State Variable to return the current host controller
203 @retval EFI_SUCCESS Host controller state was returned in State.
204 @retval EFI_INVALID_PARAMETER State is NULL.
205 @retval EFI_DEVICE_ERROR An error was encountered while attempting to
206 retrieve the host controller's current state.
212 IN EFI_USB2_HC_PROTOCOL
*This
,
213 OUT EFI_USB_HC_STATE
*State
220 return EFI_INVALID_PARAMETER
;
223 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
224 Ehc
= EHC_FROM_THIS (This
);
226 if (EHC_REG_BIT_IS_SET (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_HALT
)) {
227 *State
= EfiUsbHcStateHalt
;
229 *State
= EfiUsbHcStateOperational
;
232 gBS
->RestoreTPL (OldTpl
);
234 DEBUG ((EFI_D_INFO
, "EhcGetState: current state %d\n", *State
));
240 Sets the USB host controller to a specific state.
242 @param This This EFI_USB2_HC_PROTOCOL instance.
243 @param State The state of the host controller that will be set.
245 @retval EFI_SUCCESS The USB host controller was successfully placed
246 in the state specified by State.
247 @retval EFI_INVALID_PARAMETER State is invalid.
248 @retval EFI_DEVICE_ERROR Failed to set the state due to device error.
254 IN EFI_USB2_HC_PROTOCOL
*This
,
255 IN EFI_USB_HC_STATE State
261 EFI_USB_HC_STATE CurState
;
263 Status
= EhcGetState (This
, &CurState
);
265 if (EFI_ERROR (Status
)) {
266 return EFI_DEVICE_ERROR
;
269 if (CurState
== State
) {
273 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
274 Ehc
= EHC_FROM_THIS (This
);
277 case EfiUsbHcStateHalt
:
278 Status
= EhcHaltHC (Ehc
, EHC_GENERIC_TIMEOUT
);
281 case EfiUsbHcStateOperational
:
282 if (EHC_REG_BIT_IS_SET (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_SYS_ERROR
)) {
283 Status
= EFI_DEVICE_ERROR
;
288 // Software must not write a one to this field unless the host controller
289 // is in the Halted state. Doing so will yield undefined results.
290 // refers to Spec[EHCI1.0-2.3.1]
292 if (!EHC_REG_BIT_IS_SET (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_HALT
)) {
293 Status
= EFI_DEVICE_ERROR
;
297 Status
= EhcRunHC (Ehc
, EHC_GENERIC_TIMEOUT
);
300 case EfiUsbHcStateSuspend
:
301 Status
= EFI_UNSUPPORTED
;
305 Status
= EFI_INVALID_PARAMETER
;
308 DEBUG ((EFI_D_INFO
, "EhcSetState: exit status %r\n", Status
));
309 gBS
->RestoreTPL (OldTpl
);
315 Retrieves the current status of a USB root hub port.
317 @param This This EFI_USB2_HC_PROTOCOL instance.
318 @param PortNumber The root hub port to retrieve the state from.
319 This value is zero-based.
320 @param PortStatus Variable to receive the port state.
322 @retval EFI_SUCCESS The status of the USB root hub port specified.
323 by PortNumber was returned in PortStatus.
324 @retval EFI_INVALID_PARAMETER PortNumber is invalid.
325 @retval EFI_DEVICE_ERROR Can't read register.
330 EhcGetRootHubPortStatus (
331 IN EFI_USB2_HC_PROTOCOL
*This
,
333 OUT EFI_USB_PORT_STATUS
*PortStatus
345 if (PortStatus
== NULL
) {
346 return EFI_INVALID_PARAMETER
;
349 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
351 Ehc
= EHC_FROM_THIS (This
);
352 Status
= EFI_SUCCESS
;
354 TotalPort
= (Ehc
->HcStructParams
& HCSP_NPORTS
);
356 if (PortNumber
>= TotalPort
) {
357 Status
= EFI_INVALID_PARAMETER
;
361 Offset
= (UINT32
) (EHC_PORT_STAT_OFFSET
+ (4 * PortNumber
));
362 PortStatus
->PortStatus
= 0;
363 PortStatus
->PortChangeStatus
= 0;
365 if (EhcIsDebugPortInUse (Ehc
, &PortNumber
)) {
369 State
= EhcReadOpReg (Ehc
, Offset
);
372 // Identify device speed. If in K state, it is low speed.
373 // If the port is enabled after reset, the device is of
374 // high speed. The USB bus driver should retrieve the actual
375 // port speed after reset.
377 if (EHC_BIT_IS_SET (State
, PORTSC_LINESTATE_K
)) {
378 PortStatus
->PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
380 } else if (EHC_BIT_IS_SET (State
, PORTSC_ENABLED
)) {
381 PortStatus
->PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
385 // Convert the EHCI port/port change state to UEFI status
387 MapSize
= sizeof (mUsbPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
389 for (Index
= 0; Index
< MapSize
; Index
++) {
390 if (EHC_BIT_IS_SET (State
, mUsbPortStateMap
[Index
].HwState
)) {
391 PortStatus
->PortStatus
= (UINT16
) (PortStatus
->PortStatus
| mUsbPortStateMap
[Index
].UefiState
);
395 MapSize
= sizeof (mUsbPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
397 for (Index
= 0; Index
< MapSize
; Index
++) {
398 if (EHC_BIT_IS_SET (State
, mUsbPortChangeMap
[Index
].HwState
)) {
399 PortStatus
->PortChangeStatus
= (UINT16
) (PortStatus
->PortChangeStatus
| mUsbPortChangeMap
[Index
].UefiState
);
404 gBS
->RestoreTPL (OldTpl
);
410 Sets a feature for the specified root hub port.
412 @param This This EFI_USB2_HC_PROTOCOL instance.
413 @param PortNumber Root hub port to set.
414 @param PortFeature Feature to set.
416 @retval EFI_SUCCESS The feature specified by PortFeature was set.
417 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
418 @retval EFI_DEVICE_ERROR Can't read register.
423 EhcSetRootHubPortFeature (
424 IN EFI_USB2_HC_PROTOCOL
*This
,
426 IN EFI_USB_PORT_FEATURE PortFeature
436 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
437 Ehc
= EHC_FROM_THIS (This
);
438 Status
= EFI_SUCCESS
;
440 TotalPort
= (Ehc
->HcStructParams
& HCSP_NPORTS
);
442 if (PortNumber
>= TotalPort
) {
443 Status
= EFI_INVALID_PARAMETER
;
447 Offset
= (UINT32
) (EHC_PORT_STAT_OFFSET
+ (4 * PortNumber
));
448 State
= EhcReadOpReg (Ehc
, Offset
);
451 // Mask off the port status change bits, these bits are
454 State
&= ~PORTSC_CHANGE_MASK
;
456 switch (PortFeature
) {
457 case EfiUsbPortEnable
:
459 // Sofeware can't set this bit, Port can only be enable by
460 // EHCI as a part of the reset and enable
462 State
|= PORTSC_ENABLED
;
463 EhcWriteOpReg (Ehc
, Offset
, State
);
466 case EfiUsbPortSuspend
:
467 State
|= PORTSC_SUSPEND
;
468 EhcWriteOpReg (Ehc
, Offset
, State
);
471 case EfiUsbPortReset
:
473 // Make sure Host Controller not halt before reset it
475 if (EhcIsHalt (Ehc
)) {
476 Status
= EhcRunHC (Ehc
, EHC_GENERIC_TIMEOUT
);
478 if (EFI_ERROR (Status
)) {
479 DEBUG ((EFI_D_INFO
, "EhcSetRootHubPortFeature :failed to start HC - %r\n", Status
));
485 // Set one to PortReset bit must also set zero to PortEnable bit
487 State
|= PORTSC_RESET
;
488 State
&= ~PORTSC_ENABLED
;
489 EhcWriteOpReg (Ehc
, Offset
, State
);
492 case EfiUsbPortPower
:
494 // Set port power bit when PPC is 1
496 if ((Ehc
->HcCapParams
& HCSP_PPC
) == HCSP_PPC
) {
497 State
|= PORTSC_POWER
;
498 EhcWriteOpReg (Ehc
, Offset
, State
);
502 case EfiUsbPortOwner
:
503 State
|= PORTSC_OWNER
;
504 EhcWriteOpReg (Ehc
, Offset
, State
);
508 Status
= EFI_INVALID_PARAMETER
;
512 DEBUG ((EFI_D_INFO
, "EhcSetRootHubPortFeature: exit status %r\n", Status
));
514 gBS
->RestoreTPL (OldTpl
);
520 Clears a feature for the specified root hub port.
522 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
523 @param PortNumber Specifies the root hub port whose feature is
524 requested to be cleared.
525 @param PortFeature Indicates the feature selector associated with the
526 feature clear request.
528 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
529 for the USB root hub port specified by PortNumber.
530 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
531 @retval EFI_DEVICE_ERROR Can't read register.
536 EhcClearRootHubPortFeature (
537 IN EFI_USB2_HC_PROTOCOL
*This
,
539 IN EFI_USB_PORT_FEATURE PortFeature
549 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
550 Ehc
= EHC_FROM_THIS (This
);
551 Status
= EFI_SUCCESS
;
553 TotalPort
= (Ehc
->HcStructParams
& HCSP_NPORTS
);
555 if (PortNumber
>= TotalPort
) {
556 Status
= EFI_INVALID_PARAMETER
;
560 Offset
= EHC_PORT_STAT_OFFSET
+ (4 * PortNumber
);
561 State
= EhcReadOpReg (Ehc
, Offset
);
562 State
&= ~PORTSC_CHANGE_MASK
;
564 switch (PortFeature
) {
565 case EfiUsbPortEnable
:
567 // Clear PORT_ENABLE feature means disable port.
569 State
&= ~PORTSC_ENABLED
;
570 EhcWriteOpReg (Ehc
, Offset
, State
);
573 case EfiUsbPortSuspend
:
575 // A write of zero to this bit is ignored by the host
576 // controller. The host controller will unconditionally
577 // set this bit to a zero when:
578 // 1. software sets the Forct Port Resume bit to a zero from a one.
579 // 2. software sets the Port Reset bit to a one frome a zero.
581 State
&= ~PORSTSC_RESUME
;
582 EhcWriteOpReg (Ehc
, Offset
, State
);
585 case EfiUsbPortReset
:
587 // Clear PORT_RESET means clear the reset signal.
589 State
&= ~PORTSC_RESET
;
590 EhcWriteOpReg (Ehc
, Offset
, State
);
593 case EfiUsbPortOwner
:
595 // Clear port owner means this port owned by EHC
597 State
&= ~PORTSC_OWNER
;
598 EhcWriteOpReg (Ehc
, Offset
, State
);
601 case EfiUsbPortConnectChange
:
603 // Clear connect status change
605 State
|= PORTSC_CONN_CHANGE
;
606 EhcWriteOpReg (Ehc
, Offset
, State
);
609 case EfiUsbPortEnableChange
:
611 // Clear enable status change
613 State
|= PORTSC_ENABLE_CHANGE
;
614 EhcWriteOpReg (Ehc
, Offset
, State
);
617 case EfiUsbPortOverCurrentChange
:
619 // Clear PortOverCurrent change
621 State
|= PORTSC_OVERCUR_CHANGE
;
622 EhcWriteOpReg (Ehc
, Offset
, State
);
625 case EfiUsbPortPower
:
627 // Clear port power bit when PPC is 1
629 if ((Ehc
->HcCapParams
& HCSP_PPC
) == HCSP_PPC
) {
630 State
&= ~PORTSC_POWER
;
631 EhcWriteOpReg (Ehc
, Offset
, State
);
634 case EfiUsbPortSuspendChange
:
635 case EfiUsbPortResetChange
:
637 // Not supported or not related operation
642 Status
= EFI_INVALID_PARAMETER
;
647 DEBUG ((EFI_D_INFO
, "EhcClearRootHubPortFeature: exit status %r\n", Status
));
648 gBS
->RestoreTPL (OldTpl
);
654 Submits control transfer to a target USB device.
656 @param This This EFI_USB2_HC_PROTOCOL instance.
657 @param DeviceAddress The target device address.
658 @param DeviceSpeed Target device speed.
659 @param MaximumPacketLength Maximum packet size the default control transfer
660 endpoint is capable of sending or receiving.
661 @param Request USB device request to send.
662 @param TransferDirection Specifies the data direction for the data stage
663 @param Data Data buffer to be transmitted or received from USB
665 @param DataLength The size (in bytes) of the data buffer.
666 @param TimeOut Indicates the maximum timeout, in millisecond.
667 @param Translator Transaction translator to be used by this device.
668 @param TransferResult Return the result of this control transfer.
670 @retval EFI_SUCCESS Transfer was completed successfully.
671 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
672 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
673 @retval EFI_TIMEOUT Transfer failed due to timeout.
674 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
680 IN EFI_USB2_HC_PROTOCOL
*This
,
681 IN UINT8 DeviceAddress
,
682 IN UINT8 DeviceSpeed
,
683 IN UINTN MaximumPacketLength
,
684 IN EFI_USB_DEVICE_REQUEST
*Request
,
685 IN EFI_USB_DATA_DIRECTION TransferDirection
,
687 IN OUT UINTN
*DataLength
,
689 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
690 OUT UINT32
*TransferResult
700 // Validate parameters
702 if ((Request
== NULL
) || (TransferResult
== NULL
)) {
703 return EFI_INVALID_PARAMETER
;
706 if ((TransferDirection
!= EfiUsbDataIn
) &&
707 (TransferDirection
!= EfiUsbDataOut
) &&
708 (TransferDirection
!= EfiUsbNoData
)) {
709 return EFI_INVALID_PARAMETER
;
712 if ((TransferDirection
== EfiUsbNoData
) &&
713 ((Data
!= NULL
) || (*DataLength
!= 0))) {
714 return EFI_INVALID_PARAMETER
;
717 if ((TransferDirection
!= EfiUsbNoData
) &&
718 ((Data
== NULL
) || (*DataLength
== 0))) {
719 return EFI_INVALID_PARAMETER
;
722 if ((MaximumPacketLength
!= 8) && (MaximumPacketLength
!= 16) &&
723 (MaximumPacketLength
!= 32) && (MaximumPacketLength
!= 64)) {
724 return EFI_INVALID_PARAMETER
;
727 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) {
728 return EFI_INVALID_PARAMETER
;
731 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
732 Ehc
= EHC_FROM_THIS (This
);
734 Status
= EFI_DEVICE_ERROR
;
735 *TransferResult
= EFI_USB_ERR_SYSTEM
;
737 if (EhcIsHalt (Ehc
) || EhcIsSysError (Ehc
)) {
738 DEBUG ((EFI_D_ERROR
, "EhcControlTransfer: HC halted at entrance\n"));
740 EhcAckAllInterrupt (Ehc
);
744 EhcAckAllInterrupt (Ehc
);
747 // Create a new URB, insert it into the asynchronous
748 // schedule list, then poll the execution status.
751 // Encode the direction in address, although default control
752 // endpoint is bidirectional. EhcCreateUrb expects this
753 // combination of Ep addr and its direction.
755 Endpoint
= (UINT8
) (0 | ((TransferDirection
== EfiUsbDataIn
) ? 0x80 : 0));
774 DEBUG ((EFI_D_ERROR
, "EhcControlTransfer: failed to create URB"));
776 Status
= EFI_OUT_OF_RESOURCES
;
780 EhcLinkQhToAsync (Ehc
, Urb
->Qh
);
781 Status
= EhcExecTransfer (Ehc
, Urb
, TimeOut
);
782 EhcUnlinkQhFromAsync (Ehc
, Urb
->Qh
);
785 // Get the status from URB. The result is updated in EhcCheckUrbResult
786 // which is called by EhcExecTransfer
788 *TransferResult
= Urb
->Result
;
789 *DataLength
= Urb
->Completed
;
791 if (*TransferResult
== EFI_USB_NOERROR
) {
792 Status
= EFI_SUCCESS
;
795 EhcAckAllInterrupt (Ehc
);
796 EhcFreeUrb (Ehc
, Urb
);
799 Ehc
->PciIo
->Flush (Ehc
->PciIo
);
800 gBS
->RestoreTPL (OldTpl
);
802 if (EFI_ERROR (Status
)) {
803 DEBUG ((EFI_D_ERROR
, "EhcControlTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
811 Submits bulk transfer to a bulk endpoint of a USB device.
813 @param This This EFI_USB2_HC_PROTOCOL instance.
814 @param DeviceAddress Target device address.
815 @param EndPointAddress Endpoint number and its direction in bit 7.
816 @param DeviceSpeed Device speed, Low speed device doesn't support bulk
818 @param MaximumPacketLength Maximum packet size the endpoint is capable of
819 sending or receiving.
820 @param DataBuffersNumber Number of data buffers prepared for the transfer.
821 @param Data Array of pointers to the buffers of data to transmit
822 from or receive into.
823 @param DataLength The lenght of the data buffer.
824 @param DataToggle On input, the initial data toggle for the transfer;
825 On output, it is updated to to next data toggle to
826 use of the subsequent bulk transfer.
827 @param TimeOut Indicates the maximum time, in millisecond, which
828 the transfer is allowed to complete.
829 @param Translator A pointr to the transaction translator data.
830 @param TransferResult A pointer to the detailed result information of the
833 @retval EFI_SUCCESS The transfer was completed successfully.
834 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
835 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
836 @retval EFI_TIMEOUT The transfer failed due to timeout.
837 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
843 IN EFI_USB2_HC_PROTOCOL
*This
,
844 IN UINT8 DeviceAddress
,
845 IN UINT8 EndPointAddress
,
846 IN UINT8 DeviceSpeed
,
847 IN UINTN MaximumPacketLength
,
848 IN UINT8 DataBuffersNumber
,
849 IN OUT VOID
*Data
[EFI_USB_MAX_BULK_BUFFER_NUM
],
850 IN OUT UINTN
*DataLength
,
851 IN OUT UINT8
*DataToggle
,
853 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
854 OUT UINT32
*TransferResult
863 // Validate the parameters
865 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
866 (Data
== NULL
) || (Data
[0] == NULL
) || (TransferResult
== NULL
)) {
867 return EFI_INVALID_PARAMETER
;
870 if ((*DataToggle
!= 0) && (*DataToggle
!= 1)) {
871 return EFI_INVALID_PARAMETER
;
874 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) ||
875 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
876 ((EFI_USB_SPEED_HIGH
== DeviceSpeed
) && (MaximumPacketLength
> 512))) {
877 return EFI_INVALID_PARAMETER
;
880 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
881 Ehc
= EHC_FROM_THIS (This
);
883 *TransferResult
= EFI_USB_ERR_SYSTEM
;
884 Status
= EFI_DEVICE_ERROR
;
886 if (EhcIsHalt (Ehc
) || EhcIsSysError (Ehc
)) {
887 DEBUG ((EFI_D_ERROR
, "EhcBulkTransfer: HC is halted\n"));
889 EhcAckAllInterrupt (Ehc
);
893 EhcAckAllInterrupt (Ehc
);
896 // Create a new URB, insert it into the asynchronous
897 // schedule list, then poll the execution status.
917 DEBUG ((EFI_D_ERROR
, "EhcBulkTransfer: failed to create URB\n"));
919 Status
= EFI_OUT_OF_RESOURCES
;
923 EhcLinkQhToAsync (Ehc
, Urb
->Qh
);
924 Status
= EhcExecTransfer (Ehc
, Urb
, TimeOut
);
925 EhcUnlinkQhFromAsync (Ehc
, Urb
->Qh
);
927 *TransferResult
= Urb
->Result
;
928 *DataLength
= Urb
->Completed
;
929 *DataToggle
= Urb
->DataToggle
;
931 if (*TransferResult
== EFI_USB_NOERROR
) {
932 Status
= EFI_SUCCESS
;
935 EhcAckAllInterrupt (Ehc
);
936 EhcFreeUrb (Ehc
, Urb
);
939 Ehc
->PciIo
->Flush (Ehc
->PciIo
);
940 gBS
->RestoreTPL (OldTpl
);
942 if (EFI_ERROR (Status
)) {
943 DEBUG ((EFI_D_ERROR
, "EhcBulkTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
951 Submits an asynchronous interrupt transfer to an
952 interrupt endpoint of a USB device.
954 @param This This EFI_USB2_HC_PROTOCOL instance.
955 @param DeviceAddress Target device address.
956 @param EndPointAddress Endpoint number and its direction encoded in bit 7
957 @param DeviceSpeed Indicates device speed.
958 @param MaximumPacketLength Maximum packet size the target endpoint is capable
959 @param IsNewTransfer If TRUE, to submit an new asynchronous interrupt
960 transfer If FALSE, to remove the specified
961 asynchronous interrupt.
962 @param DataToggle On input, the initial data toggle to use; on output,
963 it is updated to indicate the next data toggle.
964 @param PollingInterval The he interval, in milliseconds, that the transfer
966 @param DataLength The length of data to receive at the rate specified
968 @param Translator Transaction translator to use.
969 @param CallBackFunction Function to call at the rate specified by
971 @param Context Context to CallBackFunction.
973 @retval EFI_SUCCESS The request has been successfully submitted or canceled.
974 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
975 @retval EFI_OUT_OF_RESOURCES The request failed due to a lack of resources.
976 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
981 EhcAsyncInterruptTransfer (
982 IN EFI_USB2_HC_PROTOCOL
* This
,
983 IN UINT8 DeviceAddress
,
984 IN UINT8 EndPointAddress
,
985 IN UINT8 DeviceSpeed
,
986 IN UINTN MaximumPacketLength
,
987 IN BOOLEAN IsNewTransfer
,
988 IN OUT UINT8
*DataToggle
,
989 IN UINTN PollingInterval
,
991 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
* Translator
,
992 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction
,
993 IN VOID
*Context OPTIONAL
1002 // Validate parameters
1004 if (!EHCI_IS_DATAIN (EndPointAddress
)) {
1005 return EFI_INVALID_PARAMETER
;
1008 if (IsNewTransfer
) {
1009 if (DataLength
== 0) {
1010 return EFI_INVALID_PARAMETER
;
1013 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1014 return EFI_INVALID_PARAMETER
;
1017 if ((PollingInterval
> 255) || (PollingInterval
< 1)) {
1018 return EFI_INVALID_PARAMETER
;
1022 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
1023 Ehc
= EHC_FROM_THIS (This
);
1026 // Delete Async interrupt transfer request. DataToggle will return
1027 // the next data toggle to use.
1029 if (!IsNewTransfer
) {
1030 Status
= EhciDelAsyncIntTransfer (Ehc
, DeviceAddress
, EndPointAddress
, DataToggle
);
1032 DEBUG ((EFI_D_INFO
, "EhcAsyncInterruptTransfer: remove old transfer - %r\n", Status
));
1036 Status
= EFI_SUCCESS
;
1038 if (EhcIsHalt (Ehc
) || EhcIsSysError (Ehc
)) {
1039 DEBUG ((EFI_D_ERROR
, "EhcAsyncInterruptTransfer: HC is halt\n"));
1040 EhcAckAllInterrupt (Ehc
);
1042 Status
= EFI_DEVICE_ERROR
;
1046 EhcAckAllInterrupt (Ehc
);
1048 Urb
= EhciInsertAsyncIntTransfer (
1054 MaximumPacketLength
,
1063 Status
= EFI_OUT_OF_RESOURCES
;
1068 Ehc
->PciIo
->Flush (Ehc
->PciIo
);
1069 gBS
->RestoreTPL (OldTpl
);
1076 Submits synchronous interrupt transfer to an interrupt endpoint
1079 @param This This EFI_USB2_HC_PROTOCOL instance.
1080 @param DeviceAddress Target device address.
1081 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1082 @param DeviceSpeed Indicates device speed.
1083 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1084 of sending or receiving.
1085 @param Data Buffer of data that will be transmitted to USB
1086 device or received from USB device.
1087 @param DataLength On input, the size, in bytes, of the data buffer; On
1088 output, the number of bytes transferred.
1089 @param DataToggle On input, the initial data toggle to use; on output,
1090 it is updated to indicate the next data toggle.
1091 @param TimeOut Maximum time, in second, to complete.
1092 @param Translator Transaction translator to use.
1093 @param TransferResult Variable to receive the transfer result.
1095 @return EFI_SUCCESS The transfer was completed successfully.
1096 @return EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1097 @return EFI_INVALID_PARAMETER Some parameters are invalid.
1098 @return EFI_TIMEOUT The transfer failed due to timeout.
1099 @return EFI_DEVICE_ERROR The failed due to host controller or device error
1104 EhcSyncInterruptTransfer (
1105 IN EFI_USB2_HC_PROTOCOL
*This
,
1106 IN UINT8 DeviceAddress
,
1107 IN UINT8 EndPointAddress
,
1108 IN UINT8 DeviceSpeed
,
1109 IN UINTN MaximumPacketLength
,
1111 IN OUT UINTN
*DataLength
,
1112 IN OUT UINT8
*DataToggle
,
1114 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1115 OUT UINT32
*TransferResult
1124 // Validates parameters
1126 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1127 (Data
== NULL
) || (TransferResult
== NULL
)) {
1128 return EFI_INVALID_PARAMETER
;
1131 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1132 return EFI_INVALID_PARAMETER
;
1135 if (((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) ||
1136 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1137 ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) && (MaximumPacketLength
> 3072))) {
1138 return EFI_INVALID_PARAMETER
;
1141 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
1142 Ehc
= EHC_FROM_THIS (This
);
1144 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1145 Status
= EFI_DEVICE_ERROR
;
1147 if (EhcIsHalt (Ehc
) || EhcIsSysError (Ehc
)) {
1148 DEBUG ((EFI_D_ERROR
, "EhcSyncInterruptTransfer: HC is halt\n"));
1150 EhcAckAllInterrupt (Ehc
);
1154 EhcAckAllInterrupt (Ehc
);
1156 Urb
= EhcCreateUrb (
1162 MaximumPacketLength
,
1164 EHC_INT_TRANSFER_SYNC
,
1174 DEBUG ((EFI_D_ERROR
, "EhcSyncInterruptTransfer: failed to create URB\n"));
1176 Status
= EFI_OUT_OF_RESOURCES
;
1180 EhcLinkQhToPeriod (Ehc
, Urb
->Qh
);
1181 Status
= EhcExecTransfer (Ehc
, Urb
, TimeOut
);
1182 EhcUnlinkQhFromPeriod (Ehc
, Urb
->Qh
);
1184 *TransferResult
= Urb
->Result
;
1185 *DataLength
= Urb
->Completed
;
1186 *DataToggle
= Urb
->DataToggle
;
1188 if (*TransferResult
== EFI_USB_NOERROR
) {
1189 Status
= EFI_SUCCESS
;
1192 EhcFreeUrb (Ehc
, Urb
);
1194 Ehc
->PciIo
->Flush (Ehc
->PciIo
);
1195 gBS
->RestoreTPL (OldTpl
);
1197 if (EFI_ERROR (Status
)) {
1198 DEBUG ((EFI_D_ERROR
, "EhcSyncInterruptTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1206 Submits isochronous transfer to a target USB device.
1208 @param This This EFI_USB2_HC_PROTOCOL instance.
1209 @param DeviceAddress Target device address.
1210 @param EndPointAddress End point address with its direction.
1211 @param DeviceSpeed Device speed, Low speed device doesn't support this
1213 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1214 sending or receiving.
1215 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1216 @param Data Array of pointers to the buffers of data that will
1217 be transmitted to USB device or received from USB
1219 @param DataLength The size, in bytes, of the data buffer.
1220 @param Translator Transaction translator to use.
1221 @param TransferResult Variable to receive the transfer result.
1223 @return EFI_UNSUPPORTED Isochronous transfer is unsupported.
1228 EhcIsochronousTransfer (
1229 IN EFI_USB2_HC_PROTOCOL
*This
,
1230 IN UINT8 DeviceAddress
,
1231 IN UINT8 EndPointAddress
,
1232 IN UINT8 DeviceSpeed
,
1233 IN UINTN MaximumPacketLength
,
1234 IN UINT8 DataBuffersNumber
,
1235 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1236 IN UINTN DataLength
,
1237 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1238 OUT UINT32
*TransferResult
1241 return EFI_UNSUPPORTED
;
1246 Submits Async isochronous transfer to a target USB device.
1248 @param This This EFI_USB2_HC_PROTOCOL instance.
1249 @param DeviceAddress Target device address.
1250 @param EndPointAddress End point address with its direction.
1251 @param DeviceSpeed Device speed, Low speed device doesn't support this
1253 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1254 sending or receiving.
1255 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1256 @param Data Array of pointers to the buffers of data that will
1257 be transmitted to USB device or received from USB
1259 @param DataLength The size, in bytes, of the data buffer.
1260 @param Translator Transaction translator to use.
1261 @param IsochronousCallBack Function to be called when the transfer complete.
1262 @param Context Context passed to the call back function as
1265 @return EFI_UNSUPPORTED Isochronous transfer isn't supported.
1270 EhcAsyncIsochronousTransfer (
1271 IN EFI_USB2_HC_PROTOCOL
*This
,
1272 IN UINT8 DeviceAddress
,
1273 IN UINT8 EndPointAddress
,
1274 IN UINT8 DeviceSpeed
,
1275 IN UINTN MaximumPacketLength
,
1276 IN UINT8 DataBuffersNumber
,
1277 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1278 IN UINTN DataLength
,
1279 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1280 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
1284 return EFI_UNSUPPORTED
;
1288 Entry point for EFI drivers.
1290 @param ImageHandle EFI_HANDLE.
1291 @param SystemTable EFI_SYSTEM_TABLE.
1293 @return EFI_SUCCESS Success.
1294 EFI_DEVICE_ERROR Fail.
1299 EhcDriverEntryPoint (
1300 IN EFI_HANDLE ImageHandle
,
1301 IN EFI_SYSTEM_TABLE
*SystemTable
1304 return EfiLibInstallDriverBindingComponentName2 (
1307 &gEhciDriverBinding
,
1309 &gEhciComponentName
,
1310 &gEhciComponentName2
1316 Test to see if this driver supports ControllerHandle. Any
1317 ControllerHandle that has Usb2HcProtocol installed will
1320 @param This Protocol instance pointer.
1321 @param Controller Handle of device to test.
1322 @param RemainingDevicePath Not used.
1324 @return EFI_SUCCESS This driver supports this device.
1325 @return EFI_UNSUPPORTED This driver does not support this device.
1330 EhcDriverBindingSupported (
1331 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1332 IN EFI_HANDLE Controller
,
1333 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1337 EFI_PCI_IO_PROTOCOL
*PciIo
;
1338 USB_CLASSC UsbClassCReg
;
1341 // Test whether there is PCI IO Protocol attached on the controller handle.
1343 Status
= gBS
->OpenProtocol (
1345 &gEfiPciIoProtocolGuid
,
1347 This
->DriverBindingHandle
,
1349 EFI_OPEN_PROTOCOL_BY_DRIVER
1352 if (EFI_ERROR (Status
)) {
1353 return EFI_UNSUPPORTED
;
1356 Status
= PciIo
->Pci
.Read (
1359 PCI_CLASSCODE_OFFSET
,
1360 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1364 if (EFI_ERROR (Status
)) {
1365 Status
= EFI_UNSUPPORTED
;
1370 // Test whether the controller belongs to Ehci type
1372 if ((UsbClassCReg
.BaseCode
!= PCI_CLASS_SERIAL
) || (UsbClassCReg
.SubClassCode
!= PCI_CLASS_SERIAL_USB
)
1373 || ((UsbClassCReg
.ProgInterface
!= PCI_IF_EHCI
) && (UsbClassCReg
.ProgInterface
!= PCI_IF_UHCI
) && (UsbClassCReg
.ProgInterface
!= PCI_IF_OHCI
))) {
1375 Status
= EFI_UNSUPPORTED
;
1379 gBS
->CloseProtocol (
1381 &gEfiPciIoProtocolGuid
,
1382 This
->DriverBindingHandle
,
1390 Get the usb debug port related information.
1392 @param Ehc The EHCI device.
1394 @retval RETURN_SUCCESS Get debug port number, bar and offset successfully.
1395 @retval Others The usb host controller does not supported usb debug port capability.
1399 EhcGetUsbDebugPortInfo (
1403 EFI_PCI_IO_PROTOCOL
*PciIo
;
1405 UINT8 CapabilityPtr
;
1410 ASSERT (Ehc
->PciIo
!= NULL
);
1414 // Detect if the EHCI host controller support Capaility Pointer.
1416 Status
= PciIo
->Pci
.Read (
1419 PCI_PRIMARY_STATUS_OFFSET
,
1424 if (EFI_ERROR (Status
)) {
1428 if ((PciStatus
& EFI_PCI_STATUS_CAPABILITY
) == 0) {
1430 // The Pci Device Doesn't Support Capability Pointer.
1432 return EFI_UNSUPPORTED
;
1436 // Get Pointer To Capability List
1438 Status
= PciIo
->Pci
.Read (
1441 PCI_CAPBILITY_POINTER_OFFSET
,
1446 if (EFI_ERROR (Status
)) {
1451 // Find Capability ID 0xA, Which Is For Debug Port
1453 while (CapabilityPtr
!= 0) {
1454 Status
= PciIo
->Pci
.Read (
1462 if (EFI_ERROR (Status
)) {
1466 if (CapabilityId
== EHC_DEBUG_PORT_CAP_ID
) {
1470 Status
= PciIo
->Pci
.Read (
1478 if (EFI_ERROR (Status
)) {
1484 // No Debug Port Capability Found
1486 if (CapabilityPtr
== 0) {
1487 return EFI_UNSUPPORTED
;
1491 // Get The Base Address Of Debug Port Register In Debug Port Capability Register
1493 Status
= PciIo
->Pci
.Read (
1501 if (EFI_ERROR (Status
)) {
1505 Ehc
->DebugPortOffset
= DebugPort
& 0x1FFF;
1506 Ehc
->DebugPortBarNum
= (UINT8
)((DebugPort
>> 13) - 1);
1507 Ehc
->DebugPortNum
= (UINT8
)((Ehc
->HcStructParams
& 0x00F00000) >> 20);
1514 Create and initialize a USB2_HC_DEV.
1516 @param PciIo The PciIo on this device.
1517 @param DevicePath The device path of host controller.
1518 @param OriginalPciAttributes Original PCI attributes.
1520 @return The allocated and initialized USB2_HC_DEV structure if created,
1526 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1527 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1528 IN UINT64 OriginalPciAttributes
1534 Ehc
= AllocateZeroPool (sizeof (USB2_HC_DEV
));
1541 // Init EFI_USB2_HC_PROTOCOL interface and private data structure
1543 Ehc
->Signature
= USB2_HC_DEV_SIGNATURE
;
1545 Ehc
->Usb2Hc
.GetCapability
= EhcGetCapability
;
1546 Ehc
->Usb2Hc
.Reset
= EhcReset
;
1547 Ehc
->Usb2Hc
.GetState
= EhcGetState
;
1548 Ehc
->Usb2Hc
.SetState
= EhcSetState
;
1549 Ehc
->Usb2Hc
.ControlTransfer
= EhcControlTransfer
;
1550 Ehc
->Usb2Hc
.BulkTransfer
= EhcBulkTransfer
;
1551 Ehc
->Usb2Hc
.AsyncInterruptTransfer
= EhcAsyncInterruptTransfer
;
1552 Ehc
->Usb2Hc
.SyncInterruptTransfer
= EhcSyncInterruptTransfer
;
1553 Ehc
->Usb2Hc
.IsochronousTransfer
= EhcIsochronousTransfer
;
1554 Ehc
->Usb2Hc
.AsyncIsochronousTransfer
= EhcAsyncIsochronousTransfer
;
1555 Ehc
->Usb2Hc
.GetRootHubPortStatus
= EhcGetRootHubPortStatus
;
1556 Ehc
->Usb2Hc
.SetRootHubPortFeature
= EhcSetRootHubPortFeature
;
1557 Ehc
->Usb2Hc
.ClearRootHubPortFeature
= EhcClearRootHubPortFeature
;
1558 Ehc
->Usb2Hc
.MajorRevision
= 0x2;
1559 Ehc
->Usb2Hc
.MinorRevision
= 0x0;
1562 Ehc
->DevicePath
= DevicePath
;
1563 Ehc
->OriginalPciAttributes
= OriginalPciAttributes
;
1565 InitializeListHead (&Ehc
->AsyncIntTransfers
);
1567 Ehc
->HcStructParams
= EhcReadCapRegister (Ehc
, EHC_HCSPARAMS_OFFSET
);
1568 Ehc
->HcCapParams
= EhcReadCapRegister (Ehc
, EHC_HCCPARAMS_OFFSET
);
1569 Ehc
->CapLen
= EhcReadCapRegister (Ehc
, EHC_CAPLENGTH_OFFSET
) & 0x0FF;
1571 DEBUG ((EFI_D_INFO
, "EhcCreateUsb2Hc: capability length %d\n", Ehc
->CapLen
));
1574 // EHCI Controllers with a CapLen of 0 are ignored.
1576 if (Ehc
->CapLen
== 0) {
1577 gBS
->FreePool (Ehc
);
1581 EhcGetUsbDebugPortInfo (Ehc
);
1584 // Create AsyncRequest Polling Timer
1586 Status
= gBS
->CreateEvent (
1587 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1589 EhcMonitorAsyncRequests
,
1594 if (EFI_ERROR (Status
)) {
1595 gBS
->FreePool (Ehc
);
1603 One notified function to stop the Host Controller when gBS->ExitBootServices() called.
1605 @param Event Pointer to this event
1606 @param Context Event handler private data
1611 EhcExitBootService (
1619 Ehc
= (USB2_HC_DEV
*) Context
;
1622 // Reset the Host Controller
1624 EhcResetHC (Ehc
, EHC_RESET_TIMEOUT
);
1629 Starting the Usb EHCI Driver.
1631 @param This Protocol instance pointer.
1632 @param Controller Handle of device to test.
1633 @param RemainingDevicePath Not used.
1635 @return EFI_SUCCESS supports this device.
1636 @return EFI_UNSUPPORTED do not support this device.
1637 @return EFI_DEVICE_ERROR cannot be started due to device Error.
1638 @return EFI_OUT_OF_RESOURCES cannot allocate resources.
1643 EhcDriverBindingStart (
1644 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1645 IN EFI_HANDLE Controller
,
1646 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1651 EFI_PCI_IO_PROTOCOL
*PciIo
;
1652 EFI_PCI_IO_PROTOCOL
*Instance
;
1654 UINT64 OriginalPciAttributes
;
1655 BOOLEAN PciAttributesSaved
;
1656 USB_CLASSC UsbClassCReg
;
1657 EFI_HANDLE
*HandleBuffer
;
1658 UINTN NumberOfHandles
;
1660 UINTN CompanionSegmentNumber
;
1661 UINTN CompanionBusNumber
;
1662 UINTN CompanionDeviceNumber
;
1663 UINTN CompanionFunctionNumber
;
1664 UINTN EhciSegmentNumber
;
1665 UINTN EhciBusNumber
;
1666 UINTN EhciDeviceNumber
;
1667 UINTN EhciFunctionNumber
;
1668 EFI_DEVICE_PATH_PROTOCOL
*HcDevicePath
;
1671 // Open the PciIo Protocol, then enable the USB host controller
1673 Status
= gBS
->OpenProtocol (
1675 &gEfiPciIoProtocolGuid
,
1677 This
->DriverBindingHandle
,
1679 EFI_OPEN_PROTOCOL_BY_DRIVER
1682 if (EFI_ERROR (Status
)) {
1687 // Open Device Path Protocol for on USB host controller
1689 HcDevicePath
= NULL
;
1690 Status
= gBS
->OpenProtocol (
1692 &gEfiDevicePathProtocolGuid
,
1693 (VOID
**) &HcDevicePath
,
1694 This
->DriverBindingHandle
,
1696 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1699 PciAttributesSaved
= FALSE
;
1701 // Save original PCI attributes
1703 Status
= PciIo
->Attributes (
1705 EfiPciIoAttributeOperationGet
,
1707 &OriginalPciAttributes
1710 if (EFI_ERROR (Status
)) {
1713 PciAttributesSaved
= TRUE
;
1715 Status
= PciIo
->Attributes (
1717 EfiPciIoAttributeOperationSupported
,
1721 if (!EFI_ERROR (Status
)) {
1722 Supports
&= (UINT64
)EFI_PCI_DEVICE_ENABLE
;
1723 Status
= PciIo
->Attributes (
1725 EfiPciIoAttributeOperationEnable
,
1731 if (EFI_ERROR (Status
)) {
1732 DEBUG ((EFI_D_ERROR
, "EhcDriverBindingStart: failed to enable controller\n"));
1737 // Get the Pci device class code.
1739 Status
= PciIo
->Pci
.Read (
1742 PCI_CLASSCODE_OFFSET
,
1743 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1747 if (EFI_ERROR (Status
)) {
1748 Status
= EFI_UNSUPPORTED
;
1752 // Determine if the device is UHCI or OHCI host controller or not. If yes, then find out the
1753 // companion usb ehci host controller and force EHCI driver get attached to it before
1754 // UHCI or OHCI driver attaches to UHCI or OHCI host controller.
1756 if ((UsbClassCReg
.ProgInterface
== PCI_IF_UHCI
|| UsbClassCReg
.ProgInterface
== PCI_IF_OHCI
) &&
1757 (UsbClassCReg
.BaseCode
== PCI_CLASS_SERIAL
) &&
1758 (UsbClassCReg
.SubClassCode
== PCI_CLASS_SERIAL_USB
)) {
1759 Status
= PciIo
->GetLocation (
1761 &CompanionSegmentNumber
,
1762 &CompanionBusNumber
,
1763 &CompanionDeviceNumber
,
1764 &CompanionFunctionNumber
1766 if (EFI_ERROR (Status
)) {
1770 Status
= gBS
->LocateHandleBuffer (
1772 &gEfiPciIoProtocolGuid
,
1777 if (EFI_ERROR (Status
)) {
1781 for (Index
= 0; Index
< NumberOfHandles
; Index
++) {
1783 // Get the device path on this handle
1785 Status
= gBS
->HandleProtocol (
1786 HandleBuffer
[Index
],
1787 &gEfiPciIoProtocolGuid
,
1790 ASSERT_EFI_ERROR (Status
);
1792 Status
= Instance
->Pci
.Read (
1795 PCI_CLASSCODE_OFFSET
,
1796 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1800 if (EFI_ERROR (Status
)) {
1801 Status
= EFI_UNSUPPORTED
;
1805 if ((UsbClassCReg
.ProgInterface
== PCI_IF_EHCI
) &&
1806 (UsbClassCReg
.BaseCode
== PCI_CLASS_SERIAL
) &&
1807 (UsbClassCReg
.SubClassCode
== PCI_CLASS_SERIAL_USB
)) {
1808 Status
= Instance
->GetLocation (
1815 if (EFI_ERROR (Status
)) {
1819 // Currently, the judgment on the companion usb host controller is through the
1820 // same bus number, which may vary on different platform.
1822 if (EhciBusNumber
== CompanionBusNumber
) {
1823 gBS
->CloseProtocol (
1825 &gEfiPciIoProtocolGuid
,
1826 This
->DriverBindingHandle
,
1829 EhcDriverBindingStart(This
, HandleBuffer
[Index
], NULL
);
1833 Status
= EFI_NOT_FOUND
;
1838 // Create then install USB2_HC_PROTOCOL
1840 Ehc
= EhcCreateUsb2Hc (PciIo
, HcDevicePath
, OriginalPciAttributes
);
1843 DEBUG ((EFI_D_ERROR
, "EhcDriverBindingStart: failed to create USB2_HC\n"));
1845 Status
= EFI_OUT_OF_RESOURCES
;
1850 // Enable 64-bit DMA support in the PCI layer if this controller
1853 if (EHC_BIT_IS_SET (Ehc
->HcCapParams
, HCCP_64BIT
)) {
1854 Status
= PciIo
->Attributes (
1856 EfiPciIoAttributeOperationEnable
,
1857 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
1860 if (!EFI_ERROR (Status
)) {
1861 Ehc
->Support64BitDma
= TRUE
;
1864 "%a: failed to enable 64-bit DMA on 64-bit capable controller @ %p (%r)\n",
1865 __FUNCTION__
, Controller
, Status
));
1869 Status
= gBS
->InstallProtocolInterface (
1871 &gEfiUsb2HcProtocolGuid
,
1872 EFI_NATIVE_INTERFACE
,
1876 if (EFI_ERROR (Status
)) {
1877 DEBUG ((EFI_D_ERROR
, "EhcDriverBindingStart: failed to install USB2_HC Protocol\n"));
1882 // Robustnesss improvement such as for Duet platform
1883 // Default is not required.
1885 if (FeaturePcdGet (PcdTurnOffUsbLegacySupport
)) {
1886 EhcClearLegacySupport (Ehc
);
1889 if (!EhcIsDebugPortInUse (Ehc
, NULL
)) {
1890 EhcResetHC (Ehc
, EHC_RESET_TIMEOUT
);
1893 Status
= EhcInitHC (Ehc
);
1895 if (EFI_ERROR (Status
)) {
1896 DEBUG ((EFI_D_ERROR
, "EhcDriverBindingStart: failed to init host controller\n"));
1897 goto UNINSTALL_USBHC
;
1901 // Start the asynchronous interrupt monitor
1903 Status
= gBS
->SetTimer (Ehc
->PollTimer
, TimerPeriodic
, EHC_ASYNC_POLL_INTERVAL
);
1905 if (EFI_ERROR (Status
)) {
1906 DEBUG ((EFI_D_ERROR
, "EhcDriverBindingStart: failed to start async interrupt monitor\n"));
1908 EhcHaltHC (Ehc
, EHC_GENERIC_TIMEOUT
);
1909 goto UNINSTALL_USBHC
;
1913 // Create event to stop the HC when exit boot service.
1915 Status
= gBS
->CreateEventEx (
1920 &gEfiEventExitBootServicesGuid
,
1921 &Ehc
->ExitBootServiceEvent
1923 if (EFI_ERROR (Status
)) {
1924 goto UNINSTALL_USBHC
;
1928 // Install the component name protocol, don't fail the start
1929 // because of something for display.
1933 gEhciComponentName
.SupportedLanguages
,
1934 &Ehc
->ControllerNameTable
,
1935 L
"Enhanced Host Controller (USB 2.0)",
1940 gEhciComponentName2
.SupportedLanguages
,
1941 &Ehc
->ControllerNameTable
,
1942 L
"Enhanced Host Controller (USB 2.0)",
1947 DEBUG ((EFI_D_INFO
, "EhcDriverBindingStart: EHCI started for controller @ %p\n", Controller
));
1951 gBS
->UninstallProtocolInterface (
1953 &gEfiUsb2HcProtocolGuid
,
1959 gBS
->CloseEvent (Ehc
->PollTimer
);
1960 gBS
->FreePool (Ehc
);
1963 if (PciAttributesSaved
) {
1965 // Restore original PCI attributes
1969 EfiPciIoAttributeOperationSet
,
1970 OriginalPciAttributes
,
1975 gBS
->CloseProtocol (
1977 &gEfiPciIoProtocolGuid
,
1978 This
->DriverBindingHandle
,
1987 Stop this driver on ControllerHandle. Support stopping any child handles
1988 created by this driver.
1990 @param This Protocol instance pointer.
1991 @param Controller Handle of device to stop driver on.
1992 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
1993 @param ChildHandleBuffer List of handles for the children we need to stop.
1995 @return EFI_SUCCESS Success.
1996 @return EFI_DEVICE_ERROR Fail.
2001 EhcDriverBindingStop (
2002 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
2003 IN EFI_HANDLE Controller
,
2004 IN UINTN NumberOfChildren
,
2005 IN EFI_HANDLE
*ChildHandleBuffer
2009 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
2010 EFI_PCI_IO_PROTOCOL
*PciIo
;
2014 // Test whether the Controller handler passed in is a valid
2015 // Usb controller handle that should be supported, if not,
2016 // return the error status directly
2018 Status
= gBS
->OpenProtocol (
2020 &gEfiUsb2HcProtocolGuid
,
2022 This
->DriverBindingHandle
,
2024 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2027 if (EFI_ERROR (Status
)) {
2031 Ehc
= EHC_FROM_THIS (Usb2Hc
);
2034 Status
= gBS
->UninstallProtocolInterface (
2036 &gEfiUsb2HcProtocolGuid
,
2040 if (EFI_ERROR (Status
)) {
2045 // Stop AsyncRequest Polling timer then stop the EHCI driver
2046 // and uninstall the EHCI protocl.
2048 gBS
->SetTimer (Ehc
->PollTimer
, TimerCancel
, EHC_ASYNC_POLL_INTERVAL
);
2049 EhcHaltHC (Ehc
, EHC_GENERIC_TIMEOUT
);
2051 if (Ehc
->PollTimer
!= NULL
) {
2052 gBS
->CloseEvent (Ehc
->PollTimer
);
2055 if (Ehc
->ExitBootServiceEvent
!= NULL
) {
2056 gBS
->CloseEvent (Ehc
->ExitBootServiceEvent
);
2061 if (Ehc
->ControllerNameTable
!= NULL
) {
2062 FreeUnicodeStringTable (Ehc
->ControllerNameTable
);
2066 // Disable routing of all ports to EHCI controller, so all ports are
2067 // routed back to the UHCI or OHCI controller.
2069 EhcClearOpRegBit (Ehc
, EHC_CONFIG_FLAG_OFFSET
, CONFIGFLAG_ROUTE_EHC
);
2072 // Restore original PCI attributes
2076 EfiPciIoAttributeOperationSet
,
2077 Ehc
->OriginalPciAttributes
,
2081 gBS
->CloseProtocol (
2083 &gEfiPciIoProtocolGuid
,
2084 This
->DriverBindingHandle
,