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 the UHCI driver attaches to the companion UHCI controller.
10 This way avoids the control transfer on a shared port between EHCI and companion host
11 controller when UHCI gets attached earlier than EHCI and a USB 2.0 device inserts.
13 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
14 This program and the accompanying materials
15 are licensed and made available under the terms and conditions of the BSD License
16 which accompanies this distribution. The full text of the license may be found at
17 http://opensource.org/licenses/bsd-license.php
19 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
20 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
28 // Two arrays used to translate the EHCI port state (change)
29 // to the UEFI protocol's port state (change).
31 USB_PORT_STATE_MAP mUsbPortStateMap
[] = {
32 {PORTSC_CONN
, USB_PORT_STAT_CONNECTION
},
33 {PORTSC_ENABLED
, USB_PORT_STAT_ENABLE
},
34 {PORTSC_SUSPEND
, USB_PORT_STAT_SUSPEND
},
35 {PORTSC_OVERCUR
, USB_PORT_STAT_OVERCURRENT
},
36 {PORTSC_RESET
, USB_PORT_STAT_RESET
},
37 {PORTSC_POWER
, USB_PORT_STAT_POWER
},
38 {PORTSC_OWNER
, USB_PORT_STAT_OWNER
}
41 USB_PORT_STATE_MAP mUsbPortChangeMap
[] = {
42 {PORTSC_CONN_CHANGE
, USB_PORT_STAT_C_CONNECTION
},
43 {PORTSC_ENABLE_CHANGE
, USB_PORT_STAT_C_ENABLE
},
44 {PORTSC_OVERCUR_CHANGE
, USB_PORT_STAT_C_OVERCURRENT
}
47 EFI_DRIVER_BINDING_PROTOCOL
48 gEhciDriverBinding
= {
49 EhcDriverBindingSupported
,
50 EhcDriverBindingStart
,
58 Retrieves the capability of root hub ports.
60 @param This This EFI_USB_HC_PROTOCOL instance.
61 @param MaxSpeed Max speed supported by the controller.
62 @param PortNumber Number of the root hub ports.
63 @param Is64BitCapable Whether the controller supports 64-bit memory
66 @retval EFI_SUCCESS Host controller capability were retrieved successfully.
67 @retval EFI_INVALID_PARAMETER Either of the three capability pointer is NULL.
73 IN EFI_USB2_HC_PROTOCOL
*This
,
75 OUT UINT8
*PortNumber
,
76 OUT UINT8
*Is64BitCapable
82 if ((MaxSpeed
== NULL
) || (PortNumber
== NULL
) || (Is64BitCapable
== NULL
)) {
83 return EFI_INVALID_PARAMETER
;
86 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
87 Ehc
= EHC_FROM_THIS (This
);
89 *MaxSpeed
= EFI_USB_SPEED_HIGH
;
90 *PortNumber
= (UINT8
) (Ehc
->HcStructParams
& HCSP_NPORTS
);
91 *Is64BitCapable
= (UINT8
) (Ehc
->HcCapParams
& HCCP_64BIT
);
93 DEBUG ((EFI_D_INFO
, "EhcGetCapability: %d ports, 64 bit %d\n", *PortNumber
, *Is64BitCapable
));
95 gBS
->RestoreTPL (OldTpl
);
101 Provides software reset for the USB host controller.
103 @param This This EFI_USB2_HC_PROTOCOL instance.
104 @param Attributes A bit mask of the reset operation to perform.
106 @retval EFI_SUCCESS The reset operation succeeded.
107 @retval EFI_INVALID_PARAMETER Attributes is not valid.
108 @retval EFI_UNSUPPOURTED The type of reset specified by Attributes is
109 not currently supported by the host controller.
110 @retval EFI_DEVICE_ERROR Host controller isn't halted to reset.
116 IN EFI_USB2_HC_PROTOCOL
*This
,
124 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
125 Ehc
= EHC_FROM_THIS (This
);
127 switch (Attributes
) {
128 case EFI_USB_HC_RESET_GLOBAL
:
130 // Flow through, same behavior as Host Controller Reset
132 case EFI_USB_HC_RESET_HOST_CONTROLLER
:
134 // Host Controller must be Halt when Reset it
136 if (!EhcIsHalt (Ehc
)) {
137 Status
= EhcHaltHC (Ehc
, EHC_GENERIC_TIMEOUT
);
139 if (EFI_ERROR (Status
)) {
140 Status
= EFI_DEVICE_ERROR
;
146 // Clean up the asynchronous transfers, currently only
147 // interrupt supports asynchronous operation.
149 EhciDelAllAsyncIntTransfers (Ehc
);
150 EhcAckAllInterrupt (Ehc
);
153 Status
= EhcResetHC (Ehc
, EHC_RESET_TIMEOUT
);
155 if (EFI_ERROR (Status
)) {
159 Status
= EhcInitHC (Ehc
);
162 case EFI_USB_HC_RESET_GLOBAL_WITH_DEBUG
:
163 case EFI_USB_HC_RESET_HOST_WITH_DEBUG
:
164 Status
= EFI_UNSUPPORTED
;
168 Status
= EFI_INVALID_PARAMETER
;
172 DEBUG ((EFI_D_INFO
, "EhcReset: exit status %r\n", Status
));
173 gBS
->RestoreTPL (OldTpl
);
179 Retrieve the current state of the USB host controller.
181 @param This This EFI_USB2_HC_PROTOCOL instance.
182 @param State Variable to return the current host controller
185 @retval EFI_SUCCESS Host controller state was returned in State.
186 @retval EFI_INVALID_PARAMETER State is NULL.
187 @retval EFI_DEVICE_ERROR An error was encountered while attempting to
188 retrieve the host controller's current state.
194 IN EFI_USB2_HC_PROTOCOL
*This
,
195 OUT EFI_USB_HC_STATE
*State
202 return EFI_INVALID_PARAMETER
;
205 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
206 Ehc
= EHC_FROM_THIS (This
);
208 if (EHC_REG_BIT_IS_SET (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_HALT
)) {
209 *State
= EfiUsbHcStateHalt
;
211 *State
= EfiUsbHcStateOperational
;
214 gBS
->RestoreTPL (OldTpl
);
216 DEBUG ((EFI_D_INFO
, "EhcGetState: current state %d\n", *State
));
222 Sets the USB host controller to a specific state.
224 @param This This EFI_USB2_HC_PROTOCOL instance.
225 @param State The state of the host controller that will be set.
227 @retval EFI_SUCCESS The USB host controller was successfully placed
228 in the state specified by State.
229 @retval EFI_INVALID_PARAMETER State is invalid.
230 @retval EFI_DEVICE_ERROR Failed to set the state due to device error.
236 IN EFI_USB2_HC_PROTOCOL
*This
,
237 IN EFI_USB_HC_STATE State
243 EFI_USB_HC_STATE CurState
;
245 Status
= EhcGetState (This
, &CurState
);
247 if (EFI_ERROR (Status
)) {
248 return EFI_DEVICE_ERROR
;
251 if (CurState
== State
) {
255 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
256 Ehc
= EHC_FROM_THIS (This
);
259 case EfiUsbHcStateHalt
:
260 Status
= EhcHaltHC (Ehc
, EHC_GENERIC_TIMEOUT
);
263 case EfiUsbHcStateOperational
:
264 if (EHC_REG_BIT_IS_SET (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_SYS_ERROR
)) {
265 Status
= EFI_DEVICE_ERROR
;
270 // Software must not write a one to this field unless the host controller
271 // is in the Halted state. Doing so will yield undefined results.
272 // refers to Spec[EHCI1.0-2.3.1]
274 if (!EHC_REG_BIT_IS_SET (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_HALT
)) {
275 Status
= EFI_DEVICE_ERROR
;
279 Status
= EhcRunHC (Ehc
, EHC_GENERIC_TIMEOUT
);
282 case EfiUsbHcStateSuspend
:
283 Status
= EFI_UNSUPPORTED
;
287 Status
= EFI_INVALID_PARAMETER
;
290 DEBUG ((EFI_D_INFO
, "EhcSetState: exit status %r\n", Status
));
291 gBS
->RestoreTPL (OldTpl
);
297 Retrieves the current status of a USB root hub port.
299 @param This This EFI_USB2_HC_PROTOCOL instance.
300 @param PortNumber The root hub port to retrieve the state from.
301 This value is zero-based.
302 @param PortStatus Variable to receive the port state.
304 @retval EFI_SUCCESS The status of the USB root hub port specified.
305 by PortNumber was returned in PortStatus.
306 @retval EFI_INVALID_PARAMETER PortNumber is invalid.
307 @retval EFI_DEVICE_ERROR Can't read register.
312 EhcGetRootHubPortStatus (
313 IN EFI_USB2_HC_PROTOCOL
*This
,
315 OUT EFI_USB_PORT_STATUS
*PortStatus
327 if (PortStatus
== NULL
) {
328 return EFI_INVALID_PARAMETER
;
331 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
333 Ehc
= EHC_FROM_THIS (This
);
334 Status
= EFI_SUCCESS
;
336 TotalPort
= (Ehc
->HcStructParams
& HCSP_NPORTS
);
338 if (PortNumber
>= TotalPort
) {
339 Status
= EFI_INVALID_PARAMETER
;
343 Offset
= (UINT32
) (EHC_PORT_STAT_OFFSET
+ (4 * PortNumber
));
344 PortStatus
->PortStatus
= 0;
345 PortStatus
->PortChangeStatus
= 0;
347 State
= EhcReadOpReg (Ehc
, Offset
);
350 // Identify device speed. If in K state, it is low speed.
351 // If the port is enabled after reset, the device is of
352 // high speed. The USB bus driver should retrieve the actual
353 // port speed after reset.
355 if (EHC_BIT_IS_SET (State
, PORTSC_LINESTATE_K
)) {
356 PortStatus
->PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
358 } else if (EHC_BIT_IS_SET (State
, PORTSC_ENABLED
)) {
359 PortStatus
->PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
363 // Convert the EHCI port/port change state to UEFI status
365 MapSize
= sizeof (mUsbPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
367 for (Index
= 0; Index
< MapSize
; Index
++) {
368 if (EHC_BIT_IS_SET (State
, mUsbPortStateMap
[Index
].HwState
)) {
369 PortStatus
->PortStatus
= (UINT16
) (PortStatus
->PortStatus
| mUsbPortStateMap
[Index
].UefiState
);
373 MapSize
= sizeof (mUsbPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
375 for (Index
= 0; Index
< MapSize
; Index
++) {
376 if (EHC_BIT_IS_SET (State
, mUsbPortChangeMap
[Index
].HwState
)) {
377 PortStatus
->PortChangeStatus
= (UINT16
) (PortStatus
->PortChangeStatus
| mUsbPortChangeMap
[Index
].UefiState
);
382 gBS
->RestoreTPL (OldTpl
);
388 Sets a feature for the specified root hub port.
390 @param This This EFI_USB2_HC_PROTOCOL instance.
391 @param PortNumber Root hub port to set.
392 @param PortFeature Feature to set.
394 @retval EFI_SUCCESS The feature specified by PortFeature was set.
395 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
396 @retval EFI_DEVICE_ERROR Can't read register.
401 EhcSetRootHubPortFeature (
402 IN EFI_USB2_HC_PROTOCOL
*This
,
404 IN EFI_USB_PORT_FEATURE PortFeature
414 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
415 Ehc
= EHC_FROM_THIS (This
);
416 Status
= EFI_SUCCESS
;
418 TotalPort
= (Ehc
->HcStructParams
& HCSP_NPORTS
);
420 if (PortNumber
>= TotalPort
) {
421 Status
= EFI_INVALID_PARAMETER
;
425 Offset
= (UINT32
) (EHC_PORT_STAT_OFFSET
+ (4 * PortNumber
));
426 State
= EhcReadOpReg (Ehc
, Offset
);
429 // Mask off the port status change bits, these bits are
432 State
&= ~PORTSC_CHANGE_MASK
;
434 switch (PortFeature
) {
435 case EfiUsbPortEnable
:
437 // Sofeware can't set this bit, Port can only be enable by
438 // EHCI as a part of the reset and enable
440 State
|= PORTSC_ENABLED
;
441 EhcWriteOpReg (Ehc
, Offset
, State
);
444 case EfiUsbPortSuspend
:
445 State
|= PORTSC_SUSPEND
;
446 EhcWriteOpReg (Ehc
, Offset
, State
);
449 case EfiUsbPortReset
:
451 // Make sure Host Controller not halt before reset it
453 if (EhcIsHalt (Ehc
)) {
454 Status
= EhcRunHC (Ehc
, EHC_GENERIC_TIMEOUT
);
456 if (EFI_ERROR (Status
)) {
457 DEBUG ((EFI_D_INFO
, "EhcSetRootHubPortFeature :failed to start HC - %r\n", Status
));
463 // Set one to PortReset bit must also set zero to PortEnable bit
465 State
|= PORTSC_RESET
;
466 State
&= ~PORTSC_ENABLED
;
467 EhcWriteOpReg (Ehc
, Offset
, State
);
470 case EfiUsbPortPower
:
472 // Set port power bit when PPC is 1
474 if ((Ehc
->HcCapParams
& HCSP_PPC
) == HCSP_PPC
) {
475 State
|= PORTSC_POWER
;
476 EhcWriteOpReg (Ehc
, Offset
, State
);
480 case EfiUsbPortOwner
:
481 State
|= PORTSC_OWNER
;
482 EhcWriteOpReg (Ehc
, Offset
, State
);
486 Status
= EFI_INVALID_PARAMETER
;
490 DEBUG ((EFI_D_INFO
, "EhcSetRootHubPortFeature: exit status %r\n", Status
));
492 gBS
->RestoreTPL (OldTpl
);
498 Clears a feature for the specified root hub port.
500 @param This A pointer to the EFI_USB2_HC_PROTOCOL instance.
501 @param PortNumber Specifies the root hub port whose feature is
502 requested to be cleared.
503 @param PortFeature Indicates the feature selector associated with the
504 feature clear request.
506 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
507 for the USB root hub port specified by PortNumber.
508 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
509 @retval EFI_DEVICE_ERROR Can't read register.
514 EhcClearRootHubPortFeature (
515 IN EFI_USB2_HC_PROTOCOL
*This
,
517 IN EFI_USB_PORT_FEATURE PortFeature
527 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
528 Ehc
= EHC_FROM_THIS (This
);
529 Status
= EFI_SUCCESS
;
531 TotalPort
= (Ehc
->HcStructParams
& HCSP_NPORTS
);
533 if (PortNumber
>= TotalPort
) {
534 Status
= EFI_INVALID_PARAMETER
;
538 Offset
= EHC_PORT_STAT_OFFSET
+ (4 * PortNumber
);
539 State
= EhcReadOpReg (Ehc
, Offset
);
540 State
&= ~PORTSC_CHANGE_MASK
;
542 switch (PortFeature
) {
543 case EfiUsbPortEnable
:
545 // Clear PORT_ENABLE feature means disable port.
547 State
&= ~PORTSC_ENABLED
;
548 EhcWriteOpReg (Ehc
, Offset
, State
);
551 case EfiUsbPortSuspend
:
553 // A write of zero to this bit is ignored by the host
554 // controller. The host controller will unconditionally
555 // set this bit to a zero when:
556 // 1. software sets the Forct Port Resume bit to a zero from a one.
557 // 2. software sets the Port Reset bit to a one frome a zero.
559 State
&= ~PORSTSC_RESUME
;
560 EhcWriteOpReg (Ehc
, Offset
, State
);
563 case EfiUsbPortReset
:
565 // Clear PORT_RESET means clear the reset signal.
567 State
&= ~PORTSC_RESET
;
568 EhcWriteOpReg (Ehc
, Offset
, State
);
571 case EfiUsbPortOwner
:
573 // Clear port owner means this port owned by EHC
575 State
&= ~PORTSC_OWNER
;
576 EhcWriteOpReg (Ehc
, Offset
, State
);
579 case EfiUsbPortConnectChange
:
581 // Clear connect status change
583 State
|= PORTSC_CONN_CHANGE
;
584 EhcWriteOpReg (Ehc
, Offset
, State
);
587 case EfiUsbPortEnableChange
:
589 // Clear enable status change
591 State
|= PORTSC_ENABLE_CHANGE
;
592 EhcWriteOpReg (Ehc
, Offset
, State
);
595 case EfiUsbPortOverCurrentChange
:
597 // Clear PortOverCurrent change
599 State
|= PORTSC_OVERCUR_CHANGE
;
600 EhcWriteOpReg (Ehc
, Offset
, State
);
603 case EfiUsbPortPower
:
605 // Clear port power bit when PPC is 1
607 if ((Ehc
->HcCapParams
& HCSP_PPC
) == HCSP_PPC
) {
608 State
&= ~PORTSC_POWER
;
609 EhcWriteOpReg (Ehc
, Offset
, State
);
612 case EfiUsbPortSuspendChange
:
613 case EfiUsbPortResetChange
:
615 // Not supported or not related operation
620 Status
= EFI_INVALID_PARAMETER
;
625 DEBUG ((EFI_D_INFO
, "EhcClearRootHubPortFeature: exit status %r\n", Status
));
626 gBS
->RestoreTPL (OldTpl
);
632 Submits control transfer to a target USB device.
634 @param This This EFI_USB2_HC_PROTOCOL instance.
635 @param DeviceAddress The target device address.
636 @param DeviceSpeed Target device speed.
637 @param MaximumPacketLength Maximum packet size the default control transfer
638 endpoint is capable of sending or receiving.
639 @param Request USB device request to send.
640 @param TransferDirection Specifies the data direction for the data stage
641 @param Data Data buffer to be transmitted or received from USB
643 @param DataLength The size (in bytes) of the data buffer.
644 @param TimeOut Indicates the maximum timeout, in millisecond.
645 @param Translator Transaction translator to be used by this device.
646 @param TransferResult Return the result of this control transfer.
648 @retval EFI_SUCCESS Transfer was completed successfully.
649 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
650 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
651 @retval EFI_TIMEOUT Transfer failed due to timeout.
652 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
658 IN EFI_USB2_HC_PROTOCOL
*This
,
659 IN UINT8 DeviceAddress
,
660 IN UINT8 DeviceSpeed
,
661 IN UINTN MaximumPacketLength
,
662 IN EFI_USB_DEVICE_REQUEST
*Request
,
663 IN EFI_USB_DATA_DIRECTION TransferDirection
,
665 IN OUT UINTN
*DataLength
,
667 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
668 OUT UINT32
*TransferResult
678 // Validate parameters
680 if ((Request
== NULL
) || (TransferResult
== NULL
)) {
681 return EFI_INVALID_PARAMETER
;
684 if ((TransferDirection
!= EfiUsbDataIn
) &&
685 (TransferDirection
!= EfiUsbDataOut
) &&
686 (TransferDirection
!= EfiUsbNoData
)) {
687 return EFI_INVALID_PARAMETER
;
690 if ((TransferDirection
== EfiUsbNoData
) &&
691 ((Data
!= NULL
) || (*DataLength
!= 0))) {
692 return EFI_INVALID_PARAMETER
;
695 if ((TransferDirection
!= EfiUsbNoData
) &&
696 ((Data
== NULL
) || (*DataLength
== 0))) {
697 return EFI_INVALID_PARAMETER
;
700 if ((MaximumPacketLength
!= 8) && (MaximumPacketLength
!= 16) &&
701 (MaximumPacketLength
!= 32) && (MaximumPacketLength
!= 64)) {
702 return EFI_INVALID_PARAMETER
;
705 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) {
706 return EFI_INVALID_PARAMETER
;
709 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
710 Ehc
= EHC_FROM_THIS (This
);
712 Status
= EFI_DEVICE_ERROR
;
713 *TransferResult
= EFI_USB_ERR_SYSTEM
;
715 if (EhcIsHalt (Ehc
) || EhcIsSysError (Ehc
)) {
716 DEBUG ((EFI_D_ERROR
, "EhcControlTransfer: HC halted at entrance\n"));
718 EhcAckAllInterrupt (Ehc
);
722 EhcAckAllInterrupt (Ehc
);
725 // Create a new URB, insert it into the asynchronous
726 // schedule list, then poll the execution status.
729 // Encode the direction in address, although default control
730 // endpoint is bidirectional. EhcCreateUrb expects this
731 // combination of Ep addr and its direction.
733 Endpoint
= (UINT8
) (0 | ((TransferDirection
== EfiUsbDataIn
) ? 0x80 : 0));
752 DEBUG ((EFI_D_ERROR
, "EhcControlTransfer: failed to create URB"));
754 Status
= EFI_OUT_OF_RESOURCES
;
758 EhcLinkQhToAsync (Ehc
, Urb
->Qh
);
759 Status
= EhcExecTransfer (Ehc
, Urb
, TimeOut
);
760 EhcUnlinkQhFromAsync (Ehc
, Urb
->Qh
);
763 // Get the status from URB. The result is updated in EhcCheckUrbResult
764 // which is called by EhcExecTransfer
766 *TransferResult
= Urb
->Result
;
767 *DataLength
= Urb
->Completed
;
769 if (*TransferResult
== EFI_USB_NOERROR
) {
770 Status
= EFI_SUCCESS
;
773 EhcAckAllInterrupt (Ehc
);
774 EhcFreeUrb (Ehc
, Urb
);
777 Ehc
->PciIo
->Flush (Ehc
->PciIo
);
778 gBS
->RestoreTPL (OldTpl
);
780 if (EFI_ERROR (Status
)) {
781 DEBUG ((EFI_D_ERROR
, "EhcControlTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
789 Submits bulk transfer to a bulk endpoint of a USB device.
791 @param This This EFI_USB2_HC_PROTOCOL instance.
792 @param DeviceAddress Target device address.
793 @param EndPointAddress Endpoint number and its direction in bit 7.
794 @param DeviceSpeed Device speed, Low speed device doesn't support bulk
796 @param MaximumPacketLength Maximum packet size the endpoint is capable of
797 sending or receiving.
798 @param DataBuffersNumber Number of data buffers prepared for the transfer.
799 @param Data Array of pointers to the buffers of data to transmit
800 from or receive into.
801 @param DataLength The lenght of the data buffer.
802 @param DataToggle On input, the initial data toggle for the transfer;
803 On output, it is updated to to next data toggle to
804 use of the subsequent bulk transfer.
805 @param TimeOut Indicates the maximum time, in millisecond, which
806 the transfer is allowed to complete.
807 @param Translator A pointr to the transaction translator data.
808 @param TransferResult A pointer to the detailed result information of the
811 @retval EFI_SUCCESS The transfer was completed successfully.
812 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
813 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
814 @retval EFI_TIMEOUT The transfer failed due to timeout.
815 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
821 IN EFI_USB2_HC_PROTOCOL
*This
,
822 IN UINT8 DeviceAddress
,
823 IN UINT8 EndPointAddress
,
824 IN UINT8 DeviceSpeed
,
825 IN UINTN MaximumPacketLength
,
826 IN UINT8 DataBuffersNumber
,
827 IN OUT VOID
*Data
[EFI_USB_MAX_BULK_BUFFER_NUM
],
828 IN OUT UINTN
*DataLength
,
829 IN OUT UINT8
*DataToggle
,
831 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
832 OUT UINT32
*TransferResult
841 // Validate the parameters
843 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
844 (Data
== NULL
) || (Data
[0] == NULL
) || (TransferResult
== NULL
)) {
845 return EFI_INVALID_PARAMETER
;
848 if ((*DataToggle
!= 0) && (*DataToggle
!= 1)) {
849 return EFI_INVALID_PARAMETER
;
852 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) ||
853 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
854 ((EFI_USB_SPEED_HIGH
== DeviceSpeed
) && (MaximumPacketLength
> 512))) {
855 return EFI_INVALID_PARAMETER
;
858 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
859 Ehc
= EHC_FROM_THIS (This
);
861 *TransferResult
= EFI_USB_ERR_SYSTEM
;
862 Status
= EFI_DEVICE_ERROR
;
864 if (EhcIsHalt (Ehc
) || EhcIsSysError (Ehc
)) {
865 DEBUG ((EFI_D_ERROR
, "EhcBulkTransfer: HC is halted\n"));
867 EhcAckAllInterrupt (Ehc
);
871 EhcAckAllInterrupt (Ehc
);
874 // Create a new URB, insert it into the asynchronous
875 // schedule list, then poll the execution status.
895 DEBUG ((EFI_D_ERROR
, "EhcBulkTransfer: failed to create URB\n"));
897 Status
= EFI_OUT_OF_RESOURCES
;
901 EhcLinkQhToAsync (Ehc
, Urb
->Qh
);
902 Status
= EhcExecTransfer (Ehc
, Urb
, TimeOut
);
903 EhcUnlinkQhFromAsync (Ehc
, Urb
->Qh
);
905 *TransferResult
= Urb
->Result
;
906 *DataLength
= Urb
->Completed
;
907 *DataToggle
= Urb
->DataToggle
;
909 if (*TransferResult
== EFI_USB_NOERROR
) {
910 Status
= EFI_SUCCESS
;
913 EhcAckAllInterrupt (Ehc
);
914 EhcFreeUrb (Ehc
, Urb
);
917 Ehc
->PciIo
->Flush (Ehc
->PciIo
);
918 gBS
->RestoreTPL (OldTpl
);
920 if (EFI_ERROR (Status
)) {
921 DEBUG ((EFI_D_ERROR
, "EhcBulkTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
929 Submits an asynchronous interrupt transfer to an
930 interrupt endpoint of a USB device.
932 @param This This EFI_USB2_HC_PROTOCOL instance.
933 @param DeviceAddress Target device address.
934 @param EndPointAddress Endpoint number and its direction encoded in bit 7
935 @param DeviceSpeed Indicates device speed.
936 @param MaximumPacketLength Maximum packet size the target endpoint is capable
937 @param IsNewTransfer If TRUE, to submit an new asynchronous interrupt
938 transfer If FALSE, to remove the specified
939 asynchronous interrupt.
940 @param DataToggle On input, the initial data toggle to use; on output,
941 it is updated to indicate the next data toggle.
942 @param PollingInterval The he interval, in milliseconds, that the transfer
944 @param DataLength The length of data to receive at the rate specified
946 @param Translator Transaction translator to use.
947 @param CallBackFunction Function to call at the rate specified by
949 @param Context Context to CallBackFunction.
951 @retval EFI_SUCCESS The request has been successfully submitted or canceled.
952 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
953 @retval EFI_OUT_OF_RESOURCES The request failed due to a lack of resources.
954 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
959 EhcAsyncInterruptTransfer (
960 IN EFI_USB2_HC_PROTOCOL
* This
,
961 IN UINT8 DeviceAddress
,
962 IN UINT8 EndPointAddress
,
963 IN UINT8 DeviceSpeed
,
964 IN UINTN MaximumPacketLength
,
965 IN BOOLEAN IsNewTransfer
,
966 IN OUT UINT8
*DataToggle
,
967 IN UINTN PollingInterval
,
969 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
* Translator
,
970 IN EFI_ASYNC_USB_TRANSFER_CALLBACK CallBackFunction
,
971 IN VOID
*Context OPTIONAL
981 // Validate parameters
983 if (!EHCI_IS_DATAIN (EndPointAddress
)) {
984 return EFI_INVALID_PARAMETER
;
988 if (DataLength
== 0) {
989 return EFI_INVALID_PARAMETER
;
992 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
993 return EFI_INVALID_PARAMETER
;
996 if ((PollingInterval
> 255) || (PollingInterval
< 1)) {
997 return EFI_INVALID_PARAMETER
;
1001 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
1002 Ehc
= EHC_FROM_THIS (This
);
1005 // Delete Async interrupt transfer request. DataToggle will return
1006 // the next data toggle to use.
1008 if (!IsNewTransfer
) {
1009 Status
= EhciDelAsyncIntTransfer (Ehc
, DeviceAddress
, EndPointAddress
, DataToggle
);
1011 DEBUG ((EFI_D_INFO
, "EhcAsyncInterruptTransfer: remove old transfer - %r\n", Status
));
1015 Status
= EFI_SUCCESS
;
1017 if (EhcIsHalt (Ehc
) || EhcIsSysError (Ehc
)) {
1018 DEBUG ((EFI_D_ERROR
, "EhcAsyncInterruptTransfer: HC is halt\n"));
1019 EhcAckAllInterrupt (Ehc
);
1021 Status
= EFI_DEVICE_ERROR
;
1025 EhcAckAllInterrupt (Ehc
);
1027 Data
= AllocatePool (DataLength
);
1030 DEBUG ((EFI_D_ERROR
, "EhcAsyncInterruptTransfer: failed to allocate buffer\n"));
1032 Status
= EFI_OUT_OF_RESOURCES
;
1036 Urb
= EhcCreateUrb (
1042 MaximumPacketLength
,
1044 EHC_INT_TRANSFER_ASYNC
,
1054 DEBUG ((EFI_D_ERROR
, "EhcAsyncInterruptTransfer: failed to create URB\n"));
1056 gBS
->FreePool (Data
);
1057 Status
= EFI_OUT_OF_RESOURCES
;
1062 // New asynchronous transfer must inserted to the head.
1063 // Check the comments in EhcMoniteAsyncRequests
1065 EhcLinkQhToPeriod (Ehc
, Urb
->Qh
);
1066 InsertHeadList (&Ehc
->AsyncIntTransfers
, &Urb
->UrbList
);
1069 Ehc
->PciIo
->Flush (Ehc
->PciIo
);
1070 gBS
->RestoreTPL (OldTpl
);
1077 Submits synchronous interrupt transfer to an interrupt endpoint
1080 @param This This EFI_USB2_HC_PROTOCOL instance.
1081 @param DeviceAddress Target device address.
1082 @param EndPointAddress Endpoint number and its direction encoded in bit 7
1083 @param DeviceSpeed Indicates device speed.
1084 @param MaximumPacketLength Maximum packet size the target endpoint is capable
1085 of sending or receiving.
1086 @param Data Buffer of data that will be transmitted to USB
1087 device or received from USB device.
1088 @param DataLength On input, the size, in bytes, of the data buffer; On
1089 output, the number of bytes transferred.
1090 @param DataToggle On input, the initial data toggle to use; on output,
1091 it is updated to indicate the next data toggle.
1092 @param TimeOut Maximum time, in second, to complete.
1093 @param Translator Transaction translator to use.
1094 @param TransferResult Variable to receive the transfer result.
1096 @return EFI_SUCCESS The transfer was completed successfully.
1097 @return EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
1098 @return EFI_INVALID_PARAMETER Some parameters are invalid.
1099 @return EFI_TIMEOUT The transfer failed due to timeout.
1100 @return EFI_DEVICE_ERROR The failed due to host controller or device error
1105 EhcSyncInterruptTransfer (
1106 IN EFI_USB2_HC_PROTOCOL
*This
,
1107 IN UINT8 DeviceAddress
,
1108 IN UINT8 EndPointAddress
,
1109 IN UINT8 DeviceSpeed
,
1110 IN UINTN MaximumPacketLength
,
1112 IN OUT UINTN
*DataLength
,
1113 IN OUT UINT8
*DataToggle
,
1115 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1116 OUT UINT32
*TransferResult
1125 // Validates parameters
1127 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
1128 (Data
== NULL
) || (TransferResult
== NULL
)) {
1129 return EFI_INVALID_PARAMETER
;
1132 if (!EHCI_IS_DATAIN (EndPointAddress
)) {
1133 return EFI_INVALID_PARAMETER
;
1136 if ((*DataToggle
!= 1) && (*DataToggle
!= 0)) {
1137 return EFI_INVALID_PARAMETER
;
1140 if (((DeviceSpeed
== EFI_USB_SPEED_LOW
) && (MaximumPacketLength
!= 8)) ||
1141 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1142 ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) && (MaximumPacketLength
> 3072))) {
1143 return EFI_INVALID_PARAMETER
;
1146 OldTpl
= gBS
->RaiseTPL (EHC_TPL
);
1147 Ehc
= EHC_FROM_THIS (This
);
1149 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1150 Status
= EFI_DEVICE_ERROR
;
1152 if (EhcIsHalt (Ehc
) || EhcIsSysError (Ehc
)) {
1153 DEBUG ((EFI_D_ERROR
, "EhcSyncInterruptTransfer: HC is halt\n"));
1155 EhcAckAllInterrupt (Ehc
);
1159 EhcAckAllInterrupt (Ehc
);
1161 Urb
= EhcCreateUrb (
1167 MaximumPacketLength
,
1169 EHC_INT_TRANSFER_SYNC
,
1179 DEBUG ((EFI_D_ERROR
, "EhcSyncInterruptTransfer: failed to create URB\n"));
1181 Status
= EFI_OUT_OF_RESOURCES
;
1185 EhcLinkQhToPeriod (Ehc
, Urb
->Qh
);
1186 Status
= EhcExecTransfer (Ehc
, Urb
, TimeOut
);
1187 EhcUnlinkQhFromPeriod (Ehc
, Urb
->Qh
);
1189 *TransferResult
= Urb
->Result
;
1190 *DataLength
= Urb
->Completed
;
1191 *DataToggle
= Urb
->DataToggle
;
1193 if (*TransferResult
== EFI_USB_NOERROR
) {
1194 Status
= EFI_SUCCESS
;
1198 Ehc
->PciIo
->Flush (Ehc
->PciIo
);
1199 gBS
->RestoreTPL (OldTpl
);
1201 if (EFI_ERROR (Status
)) {
1202 DEBUG ((EFI_D_ERROR
, "EhcSyncInterruptTransfer: error - %r, transfer - %x\n", Status
, *TransferResult
));
1210 Submits isochronous transfer to a target USB device.
1212 @param This This EFI_USB2_HC_PROTOCOL instance.
1213 @param DeviceAddress Target device address.
1214 @param EndPointAddress End point address with its direction.
1215 @param DeviceSpeed Device speed, Low speed device doesn't support this
1217 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1218 sending or receiving.
1219 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1220 @param Data Array of pointers to the buffers of data that will
1221 be transmitted to USB device or received from USB
1223 @param DataLength The size, in bytes, of the data buffer.
1224 @param Translator Transaction translator to use.
1225 @param TransferResult Variable to receive the transfer result.
1227 @return EFI_UNSUPPORTED Isochronous transfer is unsupported.
1232 EhcIsochronousTransfer (
1233 IN EFI_USB2_HC_PROTOCOL
*This
,
1234 IN UINT8 DeviceAddress
,
1235 IN UINT8 EndPointAddress
,
1236 IN UINT8 DeviceSpeed
,
1237 IN UINTN MaximumPacketLength
,
1238 IN UINT8 DataBuffersNumber
,
1239 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1240 IN UINTN DataLength
,
1241 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1242 OUT UINT32
*TransferResult
1245 return EFI_UNSUPPORTED
;
1250 Submits Async isochronous transfer to a target USB device.
1252 @param This This EFI_USB2_HC_PROTOCOL instance.
1253 @param DeviceAddress Target device address.
1254 @param EndPointAddress End point address with its direction.
1255 @param DeviceSpeed Device speed, Low speed device doesn't support this
1257 @param MaximumPacketLength Maximum packet size that the endpoint is capable of
1258 sending or receiving.
1259 @param DataBuffersNumber Number of data buffers prepared for the transfer.
1260 @param Data Array of pointers to the buffers of data that will
1261 be transmitted to USB device or received from USB
1263 @param DataLength The size, in bytes, of the data buffer.
1264 @param Translator Transaction translator to use.
1265 @param IsochronousCallBack Function to be called when the transfer complete.
1266 @param Context Context passed to the call back function as
1269 @return EFI_UNSUPPORTED Isochronous transfer isn't supported.
1274 EhcAsyncIsochronousTransfer (
1275 IN EFI_USB2_HC_PROTOCOL
*This
,
1276 IN UINT8 DeviceAddress
,
1277 IN UINT8 EndPointAddress
,
1278 IN UINT8 DeviceSpeed
,
1279 IN UINTN MaximumPacketLength
,
1280 IN UINT8 DataBuffersNumber
,
1281 IN OUT VOID
*Data
[EFI_USB_MAX_ISO_BUFFER_NUM
],
1282 IN UINTN DataLength
,
1283 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1284 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
1288 return EFI_UNSUPPORTED
;
1292 Entry point for EFI drivers.
1294 @param ImageHandle EFI_HANDLE.
1295 @param SystemTable EFI_SYSTEM_TABLE.
1297 @return EFI_SUCCESS Success.
1298 EFI_DEVICE_ERROR Fail.
1303 EhcDriverEntryPoint (
1304 IN EFI_HANDLE ImageHandle
,
1305 IN EFI_SYSTEM_TABLE
*SystemTable
1308 return EfiLibInstallDriverBindingComponentName2 (
1311 &gEhciDriverBinding
,
1313 &gEhciComponentName
,
1314 &gEhciComponentName2
1320 Test to see if this driver supports ControllerHandle. Any
1321 ControllerHandle that has Usb2HcProtocol installed will
1324 @param This Protocol instance pointer.
1325 @param Controller Handle of device to test.
1326 @param RemainingDevicePath Not used.
1328 @return EFI_SUCCESS This driver supports this device.
1329 @return EFI_UNSUPPORTED This driver does not support this device.
1334 EhcDriverBindingSupported (
1335 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1336 IN EFI_HANDLE Controller
,
1337 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1341 EFI_PCI_IO_PROTOCOL
*PciIo
;
1342 USB_CLASSC UsbClassCReg
;
1345 // Test whether there is PCI IO Protocol attached on the controller handle.
1347 Status
= gBS
->OpenProtocol (
1349 &gEfiPciIoProtocolGuid
,
1351 This
->DriverBindingHandle
,
1353 EFI_OPEN_PROTOCOL_BY_DRIVER
1356 if (EFI_ERROR (Status
)) {
1357 return EFI_UNSUPPORTED
;
1360 Status
= PciIo
->Pci
.Read (
1363 PCI_CLASSCODE_OFFSET
,
1364 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1368 if (EFI_ERROR (Status
)) {
1369 Status
= EFI_UNSUPPORTED
;
1374 // Test whether the controller belongs to Ehci type
1376 if ((UsbClassCReg
.BaseCode
!= PCI_CLASS_SERIAL
) || (UsbClassCReg
.SubClassCode
!= PCI_CLASS_SERIAL_USB
)
1377 || ((UsbClassCReg
.ProgInterface
!= PCI_IF_EHCI
) && (UsbClassCReg
.ProgInterface
!=PCI_IF_UHCI
))) {
1379 Status
= EFI_UNSUPPORTED
;
1383 gBS
->CloseProtocol (
1385 &gEfiPciIoProtocolGuid
,
1386 This
->DriverBindingHandle
,
1395 Create and initialize a USB2_HC_DEV.
1397 @param PciIo The PciIo on this device.
1398 @param OriginalPciAttributes Original PCI attributes.
1400 @return The allocated and initialized USB2_HC_DEV structure if created,
1406 IN EFI_PCI_IO_PROTOCOL
*PciIo
,
1407 IN UINT64 OriginalPciAttributes
1413 Ehc
= AllocateZeroPool (sizeof (USB2_HC_DEV
));
1420 // Init EFI_USB2_HC_PROTOCOL interface and private data structure
1422 Ehc
->Signature
= USB2_HC_DEV_SIGNATURE
;
1424 Ehc
->Usb2Hc
.GetCapability
= EhcGetCapability
;
1425 Ehc
->Usb2Hc
.Reset
= EhcReset
;
1426 Ehc
->Usb2Hc
.GetState
= EhcGetState
;
1427 Ehc
->Usb2Hc
.SetState
= EhcSetState
;
1428 Ehc
->Usb2Hc
.ControlTransfer
= EhcControlTransfer
;
1429 Ehc
->Usb2Hc
.BulkTransfer
= EhcBulkTransfer
;
1430 Ehc
->Usb2Hc
.AsyncInterruptTransfer
= EhcAsyncInterruptTransfer
;
1431 Ehc
->Usb2Hc
.SyncInterruptTransfer
= EhcSyncInterruptTransfer
;
1432 Ehc
->Usb2Hc
.IsochronousTransfer
= EhcIsochronousTransfer
;
1433 Ehc
->Usb2Hc
.AsyncIsochronousTransfer
= EhcAsyncIsochronousTransfer
;
1434 Ehc
->Usb2Hc
.GetRootHubPortStatus
= EhcGetRootHubPortStatus
;
1435 Ehc
->Usb2Hc
.SetRootHubPortFeature
= EhcSetRootHubPortFeature
;
1436 Ehc
->Usb2Hc
.ClearRootHubPortFeature
= EhcClearRootHubPortFeature
;
1437 Ehc
->Usb2Hc
.MajorRevision
= 0x2;
1438 Ehc
->Usb2Hc
.MinorRevision
= 0x0;
1441 Ehc
->OriginalPciAttributes
= OriginalPciAttributes
;
1443 InitializeListHead (&Ehc
->AsyncIntTransfers
);
1445 Ehc
->HcStructParams
= EhcReadCapRegister (Ehc
, EHC_HCSPARAMS_OFFSET
);
1446 Ehc
->HcCapParams
= EhcReadCapRegister (Ehc
, EHC_HCCPARAMS_OFFSET
);
1447 Ehc
->CapLen
= EhcReadCapRegister (Ehc
, EHC_CAPLENGTH_OFFSET
) & 0x0FF;
1449 DEBUG ((EFI_D_INFO
, "EhcCreateUsb2Hc: capability length %d\n", Ehc
->CapLen
));
1452 // EHCI Controllers with a CapLen of 0 are ignored.
1454 if (Ehc
->CapLen
== 0) {
1455 gBS
->FreePool (Ehc
);
1460 // Create AsyncRequest Polling Timer
1462 Status
= gBS
->CreateEvent (
1463 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1465 EhcMonitorAsyncRequests
,
1470 if (EFI_ERROR (Status
)) {
1471 gBS
->FreePool (Ehc
);
1479 One notified function to stop the Host Controller when gBS->ExitBootServices() called.
1481 @param Event Pointer to this event
1482 @param Context Event hanlder private data
1487 EhcExitBootService (
1495 Ehc
= (USB2_HC_DEV
*) Context
;
1498 // Reset the Host Controller
1500 EhcResetHC (Ehc
, EHC_RESET_TIMEOUT
);
1505 Starting the Usb EHCI Driver.
1507 @param This Protocol instance pointer.
1508 @param Controller Handle of device to test.
1509 @param RemainingDevicePath Not used.
1511 @return EFI_SUCCESS supports this device.
1512 @return EFI_UNSUPPORTED do not support this device.
1513 @return EFI_DEVICE_ERROR cannot be started due to device Error.
1514 @return EFI_OUT_OF_RESOURCES cannot allocate resources.
1519 EhcDriverBindingStart (
1520 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1521 IN EFI_HANDLE Controller
,
1522 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1527 EFI_PCI_IO_PROTOCOL
*PciIo
;
1528 EFI_PCI_IO_PROTOCOL
*Instance
;
1530 UINT64 OriginalPciAttributes
;
1531 BOOLEAN PciAttributesSaved
;
1532 USB_CLASSC UsbClassCReg
;
1533 EFI_HANDLE
*HandleBuffer
;
1534 UINTN NumberOfHandles
;
1536 UINTN UhciSegmentNumber
;
1537 UINTN UhciBusNumber
;
1538 UINTN UhciDeviceNumber
;
1539 UINTN UhciFunctionNumber
;
1540 UINTN EhciSegmentNumber
;
1541 UINTN EhciBusNumber
;
1542 UINTN EhciDeviceNumber
;
1543 UINTN EhciFunctionNumber
;
1546 // Open the PciIo Protocol, then enable the USB host controller
1548 Status
= gBS
->OpenProtocol (
1550 &gEfiPciIoProtocolGuid
,
1552 This
->DriverBindingHandle
,
1554 EFI_OPEN_PROTOCOL_BY_DRIVER
1557 if (EFI_ERROR (Status
)) {
1561 PciAttributesSaved
= FALSE
;
1563 // Save original PCI attributes
1565 Status
= PciIo
->Attributes (
1567 EfiPciIoAttributeOperationGet
,
1569 &OriginalPciAttributes
1572 if (EFI_ERROR (Status
)) {
1575 PciAttributesSaved
= TRUE
;
1577 Status
= PciIo
->Attributes (
1579 EfiPciIoAttributeOperationSupported
,
1583 if (!EFI_ERROR (Status
)) {
1584 Supports
&= EFI_PCI_DEVICE_ENABLE
;
1585 Status
= PciIo
->Attributes (
1587 EfiPciIoAttributeOperationEnable
,
1593 if (EFI_ERROR (Status
)) {
1594 DEBUG ((EFI_D_ERROR
, "EhcDriverBindingStart: failed to enable controller\n"));
1599 // Get the Pci device class code.
1601 Status
= PciIo
->Pci
.Read (
1604 PCI_CLASSCODE_OFFSET
,
1605 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1609 if (EFI_ERROR (Status
)) {
1610 Status
= EFI_UNSUPPORTED
;
1614 // determine if the device is UHCI host controller or not. If yes, then find out the
1615 // companion usb ehci host controller and force EHCI driver get attached to it before
1616 // UHCI driver attaches to UHCI host controller.
1618 if ((UsbClassCReg
.ProgInterface
== PCI_IF_UHCI
) &&
1619 (UsbClassCReg
.BaseCode
== PCI_CLASS_SERIAL
) &&
1620 (UsbClassCReg
.SubClassCode
== PCI_CLASS_SERIAL_USB
)) {
1621 Status
= PciIo
->GetLocation (
1628 if (EFI_ERROR (Status
)) {
1632 Status
= gBS
->LocateHandleBuffer (
1634 &gEfiPciIoProtocolGuid
,
1639 if (EFI_ERROR (Status
)) {
1643 for (Index
= 0; Index
< NumberOfHandles
; Index
++) {
1645 // Get the device path on this handle
1647 Status
= gBS
->HandleProtocol (
1648 HandleBuffer
[Index
],
1649 &gEfiPciIoProtocolGuid
,
1652 ASSERT_EFI_ERROR (Status
);
1654 Status
= Instance
->Pci
.Read (
1657 PCI_CLASSCODE_OFFSET
,
1658 sizeof (USB_CLASSC
) / sizeof (UINT8
),
1662 if (EFI_ERROR (Status
)) {
1663 Status
= EFI_UNSUPPORTED
;
1667 if ((UsbClassCReg
.ProgInterface
== PCI_IF_EHCI
) &&
1668 (UsbClassCReg
.BaseCode
== PCI_CLASS_SERIAL
) &&
1669 (UsbClassCReg
.SubClassCode
== PCI_CLASS_SERIAL_USB
)) {
1670 Status
= Instance
->GetLocation (
1677 if (EFI_ERROR (Status
)) {
1681 // Currently, the judgment on the companion usb host controller is through the
1682 // same bus number, which may vary on different platform.
1684 if (EhciBusNumber
== UhciBusNumber
) {
1685 gBS
->CloseProtocol (
1687 &gEfiPciIoProtocolGuid
,
1688 This
->DriverBindingHandle
,
1691 EhcDriverBindingStart(This
, HandleBuffer
[Index
], NULL
);
1695 Status
= EFI_NOT_FOUND
;
1700 // Create then install USB2_HC_PROTOCOL
1702 Ehc
= EhcCreateUsb2Hc (PciIo
, OriginalPciAttributes
);
1705 DEBUG ((EFI_D_ERROR
, "EhcDriverBindingStart: failed to create USB2_HC\n"));
1707 Status
= EFI_OUT_OF_RESOURCES
;
1711 Status
= gBS
->InstallProtocolInterface (
1713 &gEfiUsb2HcProtocolGuid
,
1714 EFI_NATIVE_INTERFACE
,
1718 if (EFI_ERROR (Status
)) {
1719 DEBUG ((EFI_D_ERROR
, "EhcDriverBindingStart: failed to install USB2_HC Protocol\n"));
1724 // Robustnesss improvement such as for Duet platform
1725 // Default is not required.
1727 if (FeaturePcdGet (PcdTurnOffUsbLegacySupport
)) {
1728 EhcClearLegacySupport (Ehc
);
1730 EhcResetHC (Ehc
, EHC_RESET_TIMEOUT
);
1732 Status
= EhcInitHC (Ehc
);
1734 if (EFI_ERROR (Status
)) {
1735 DEBUG ((EFI_D_ERROR
, "EhcDriverBindingStart: failed to init host controller\n"));
1736 goto UNINSTALL_USBHC
;
1740 // Start the asynchronous interrupt monitor
1742 Status
= gBS
->SetTimer (Ehc
->PollTimer
, TimerPeriodic
, EHC_ASYNC_POLL_INTERVAL
);
1744 if (EFI_ERROR (Status
)) {
1745 DEBUG ((EFI_D_ERROR
, "EhcDriverBindingStart: failed to start async interrupt monitor\n"));
1747 EhcHaltHC (Ehc
, EHC_GENERIC_TIMEOUT
);
1748 goto UNINSTALL_USBHC
;
1752 // Create event to stop the HC when exit boot service.
1754 Status
= gBS
->CreateEventEx (
1759 &gEfiEventExitBootServicesGuid
,
1760 &Ehc
->ExitBootServiceEvent
1762 if (EFI_ERROR (Status
)) {
1763 goto UNINSTALL_USBHC
;
1767 // Install the component name protocol, don't fail the start
1768 // because of something for display.
1772 gEhciComponentName
.SupportedLanguages
,
1773 &Ehc
->ControllerNameTable
,
1774 L
"Enhanced Host Controller (USB 2.0)",
1779 gEhciComponentName2
.SupportedLanguages
,
1780 &Ehc
->ControllerNameTable
,
1781 L
"Enhanced Host Controller (USB 2.0)",
1786 DEBUG ((EFI_D_INFO
, "EhcDriverBindingStart: EHCI started for controller @ %p\n", Controller
));
1790 gBS
->UninstallProtocolInterface (
1792 &gEfiUsb2HcProtocolGuid
,
1798 gBS
->CloseEvent (Ehc
->PollTimer
);
1799 gBS
->FreePool (Ehc
);
1802 if (PciAttributesSaved
) {
1804 // Restore original PCI attributes
1808 EfiPciIoAttributeOperationSet
,
1809 OriginalPciAttributes
,
1814 gBS
->CloseProtocol (
1816 &gEfiPciIoProtocolGuid
,
1817 This
->DriverBindingHandle
,
1826 Stop this driver on ControllerHandle. Support stoping any child handles
1827 created by this driver.
1829 @param This Protocol instance pointer.
1830 @param Controller Handle of device to stop driver on.
1831 @param NumberOfChildren Number of Children in the ChildHandleBuffer.
1832 @param ChildHandleBuffer List of handles for the children we need to stop.
1834 @return EFI_SUCCESS Success.
1835 @return EFI_DEVICE_ERROR Fail.
1840 EhcDriverBindingStop (
1841 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1842 IN EFI_HANDLE Controller
,
1843 IN UINTN NumberOfChildren
,
1844 IN EFI_HANDLE
*ChildHandleBuffer
1848 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
1849 EFI_PCI_IO_PROTOCOL
*PciIo
;
1853 // Test whether the Controller handler passed in is a valid
1854 // Usb controller handle that should be supported, if not,
1855 // return the error status directly
1857 Status
= gBS
->OpenProtocol (
1859 &gEfiUsb2HcProtocolGuid
,
1861 This
->DriverBindingHandle
,
1863 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1866 if (EFI_ERROR (Status
)) {
1870 Ehc
= EHC_FROM_THIS (Usb2Hc
);
1874 // Stop AsyncRequest Polling timer then stop the EHCI driver
1875 // and uninstall the EHCI protocl.
1877 gBS
->SetTimer (Ehc
->PollTimer
, TimerCancel
, EHC_ASYNC_POLL_INTERVAL
);
1878 EhcHaltHC (Ehc
, EHC_GENERIC_TIMEOUT
);
1880 Status
= gBS
->UninstallProtocolInterface (
1882 &gEfiUsb2HcProtocolGuid
,
1886 if (EFI_ERROR (Status
)) {
1890 if (Ehc
->PollTimer
!= NULL
) {
1891 gBS
->CloseEvent (Ehc
->PollTimer
);
1894 if (Ehc
->ExitBootServiceEvent
!= NULL
) {
1895 gBS
->CloseEvent (Ehc
->ExitBootServiceEvent
);
1900 if (Ehc
->ControllerNameTable
!= NULL
) {
1901 FreeUnicodeStringTable (Ehc
->ControllerNameTable
);
1905 // Disable routing of all ports to EHCI controller, so all ports are
1906 // routed back to the UHCI controller.
1908 EhcClearOpRegBit (Ehc
, EHC_CONFIG_FLAG_OFFSET
, CONFIGFLAG_ROUTE_EHC
);
1911 // Restore original PCI attributes
1915 EfiPciIoAttributeOperationSet
,
1916 Ehc
->OriginalPciAttributes
,
1920 gBS
->CloseProtocol (
1922 &gEfiPciIoProtocolGuid
,
1923 This
->DriverBindingHandle
,