3 Unified interface for RootHub and Hub.
5 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 // Array that maps the change bit to feature value which is
20 // used to clear these change bit. USB HUB API will clear
21 // these change bit automatically. For non-root hub, these
22 // bits determine whether hub will report the port in changed
25 USB_CHANGE_FEATURE_MAP mHubFeatureMap
[] = {
26 {USB_PORT_STAT_C_CONNECTION
, EfiUsbPortConnectChange
},
27 {USB_PORT_STAT_C_ENABLE
, EfiUsbPortEnableChange
},
28 {USB_PORT_STAT_C_SUSPEND
, EfiUsbPortSuspendChange
},
29 {USB_PORT_STAT_C_OVERCURRENT
, EfiUsbPortOverCurrentChange
},
30 {USB_PORT_STAT_C_RESET
, EfiUsbPortResetChange
}
33 USB_CHANGE_FEATURE_MAP mRootHubFeatureMap
[] = {
34 {USB_PORT_STAT_C_CONNECTION
, EfiUsbPortConnectChange
},
35 {USB_PORT_STAT_C_ENABLE
, EfiUsbPortEnableChange
},
36 {USB_PORT_STAT_C_SUSPEND
, EfiUsbPortSuspendChange
},
37 {USB_PORT_STAT_C_OVERCURRENT
, EfiUsbPortOverCurrentChange
},
38 {USB_PORT_STAT_C_RESET
, EfiUsbPortResetChange
},
42 // USB hub class specific requests. Although USB hub
43 // is related to an interface, these requests are sent
44 // to the control endpoint of the device.
47 USB hub control transfer to set the hub depth.
49 @param HubDev The device of the hub.
50 @param Depth The depth to set.
52 @retval EFI_SUCCESS Depth of the hub is set.
53 @retval Others Failed to set the depth.
57 UsbHubCtrlSetHubDepth (
58 IN USB_DEVICE
*HubDev
,
64 Status
= UsbCtrlRequest (
69 USB_HUB_REQ_SET_DEPTH
,
80 USB hub control transfer to clear the hub feature.
82 @param HubDev The device of the hub.
83 @param Feature The feature to clear.
85 @retval EFI_SUCCESS Feature of the hub is cleared.
86 @retval Others Failed to clear the feature.
90 UsbHubCtrlClearHubFeature (
91 IN USB_DEVICE
*HubDev
,
97 Status
= UsbCtrlRequest (
102 USB_HUB_REQ_CLEAR_FEATURE
,
114 Clear the feature of the device's port.
116 @param HubDev The hub device.
117 @param Port The port to clear feature.
118 @param Feature The feature to clear.
120 @retval EFI_SUCCESS The feature of the port is cleared.
121 @retval Others Failed to clear the feature.
125 UsbHubCtrlClearPortFeature (
126 IN USB_DEVICE
*HubDev
,
134 // In USB bus, all the port index starts from 0. But HUB
135 // indexes its port from 1. So, port number is added one.
137 Status
= UsbCtrlRequest (
142 USB_HUB_REQ_CLEAR_FEATURE
,
154 Clear the transaction translate buffer if full/low
155 speed control/bulk transfer failed and the transfer
156 uses this hub as translator.Remember to clear the TT
157 buffer of transaction translator, not that of the
160 @param HubDev The hub device.
161 @param Port The port of the hub.
162 @param DevAddr Address of the failed transaction.
163 @param EpNum The endpoint number of the failed transaction.
164 @param EpType The type of failed transaction.
166 @retval EFI_SUCCESS The TT buffer is cleared.
167 @retval Others Failed to clear the TT buffer.
171 UsbHubCtrlClearTTBuffer (
172 IN USB_DEVICE
*HubDev
,
183 // Check USB2.0 spec page 424 for wValue's encoding
185 Value
= (UINT16
) ((EpNum
& 0x0F) | (DevAddr
<< 4) |
186 ((EpType
& 0x03) << 11) | ((EpNum
& 0x80) << 15));
188 Status
= UsbCtrlRequest (
193 USB_HUB_REQ_CLEAR_TT
,
204 Usb hub control transfer to get the (super speed) hub descriptor.
206 @param HubDev The hub device.
207 @param Buf The buffer to hold the descriptor.
208 @param Len The length to retrieve.
210 @retval EFI_SUCCESS The hub descriptor is retrieved.
211 @retval Others Failed to retrieve the hub descriptor.
215 UsbHubCtrlGetHubDesc (
216 IN USB_DEVICE
*HubDev
,
224 DescType
= (HubDev
->Speed
== EFI_USB_SPEED_SUPER
) ?
225 USB_DESC_TYPE_HUB_SUPER_SPEED
:
228 Status
= UsbCtrlRequest (
233 USB_HUB_REQ_GET_DESC
,
234 (UINT16
) (DescType
<< 8),
245 Usb hub control transfer to get the hub status.
247 @param HubDev The hub device.
248 @param State The variable to return the status.
250 @retval EFI_SUCCESS The hub status is returned in State.
251 @retval Others Failed to get the hub status.
255 UsbHubCtrlGetHubStatus (
256 IN USB_DEVICE
*HubDev
,
262 Status
= UsbCtrlRequest (
267 USB_HUB_REQ_GET_STATUS
,
279 Usb hub control transfer to get the port status.
281 @param HubDev The hub device.
282 @param Port The port of the hub.
283 @param State Variable to return the hub port state.
285 @retval EFI_SUCCESS The port state is returned in State.
286 @retval Others Failed to retrieve the port state.
290 UsbHubCtrlGetPortStatus (
291 IN USB_DEVICE
*HubDev
,
299 // In USB bus, all the port index starts from 0. But HUB
300 // indexes its port from 1. So, port number is added one.
301 // No need to convert the hub bit to UEFI definition, they
304 Status
= UsbCtrlRequest (
309 USB_HUB_REQ_GET_STATUS
,
321 Usb hub control transfer to set the port feature.
323 @param HubDev The Usb hub device.
324 @param Port The Usb port to set feature for.
325 @param Feature The feature to set.
327 @retval EFI_SUCCESS The feature is set for the port.
328 @retval Others Failed to set the feature.
332 UsbHubCtrlSetPortFeature (
333 IN USB_DEVICE
*HubDev
,
341 // In USB bus, all the port index starts from 0. But HUB
342 // indexes its port from 1. So, port number is added one.
344 Status
= UsbCtrlRequest (
349 USB_HUB_REQ_SET_FEATURE
,
361 Read the whole usb hub descriptor. It is necessary
362 to do it in two steps because hub descriptor is of
365 @param HubDev The hub device.
366 @param HubDesc The variable to return the descriptor.
368 @retval EFI_SUCCESS The hub descriptor is read.
369 @retval Others Failed to read the hub descriptor.
374 IN USB_DEVICE
*HubDev
,
375 OUT EFI_USB_HUB_DESCRIPTOR
*HubDesc
381 // First get the hub descriptor length
383 Status
= UsbHubCtrlGetHubDesc (HubDev
, HubDesc
, 2);
385 if (EFI_ERROR (Status
)) {
390 // Get the whole hub descriptor
392 return UsbHubCtrlGetHubDesc (HubDev
, HubDesc
, HubDesc
->Length
);
398 Ack the hub change bits. If these bits are not ACKed, Hub will
399 always return changed bit map from its interrupt endpoint.
401 @param HubDev The hub device.
403 @retval EFI_SUCCESS The hub change status is ACKed.
404 @retval Others Failed to ACK the hub status.
409 IN USB_DEVICE
*HubDev
412 EFI_USB_PORT_STATUS HubState
;
415 Status
= UsbHubCtrlGetHubStatus (HubDev
, (UINT32
*) &HubState
);
417 if (EFI_ERROR (Status
)) {
421 if (USB_BIT_IS_SET (HubState
.PortChangeStatus
, USB_HUB_STAT_C_LOCAL_POWER
)) {
422 UsbHubCtrlClearHubFeature (HubDev
, USB_HUB_C_HUB_LOCAL_POWER
);
425 if (USB_BIT_IS_SET (HubState
.PortChangeStatus
, USB_HUB_STAT_C_OVER_CURRENT
)) {
426 UsbHubCtrlClearHubFeature (HubDev
, USB_HUB_C_HUB_OVER_CURRENT
);
434 Test whether the interface is a hub interface.
436 @param UsbIf The interface to test.
438 @retval TRUE The interface is a hub interface.
439 @retval FALSE The interface isn't a hub interface.
444 IN USB_INTERFACE
*UsbIf
447 EFI_USB_INTERFACE_DESCRIPTOR
*Setting
;
450 // If the hub is a high-speed hub with multiple TT,
451 // the hub will has a default setting of single TT.
453 Setting
= &UsbIf
->IfSetting
->Desc
;
455 if ((Setting
->InterfaceClass
== USB_HUB_CLASS_CODE
) &&
456 (Setting
->InterfaceSubClass
== USB_HUB_SUBCLASS_CODE
)) {
466 The callback function to the USB hub status change
467 interrupt endpoint. It is called periodically by
468 the underlying host controller.
470 @param Data The data read.
471 @param DataLength The length of the data read.
472 @param Context The context.
473 @param Result The result of the last interrupt transfer.
475 @retval EFI_SUCCESS The process is OK.
476 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
488 USB_INTERFACE
*HubIf
;
489 EFI_USB_IO_PROTOCOL
*UsbIo
;
490 EFI_USB_ENDPOINT_DESCRIPTOR
*EpDesc
;
493 HubIf
= (USB_INTERFACE
*) Context
;
494 UsbIo
= &(HubIf
->UsbIo
);
495 EpDesc
= &(HubIf
->HubEp
->Desc
);
497 if (Result
!= EFI_USB_NOERROR
) {
499 // If endpoint is stalled, clear the stall. Use UsbIo to access
500 // the control transfer so internal status are maintained.
502 if (USB_BIT_IS_SET (Result
, EFI_USB_ERR_STALL
)) {
506 USB_FEATURE_ENDPOINT_HALT
,
507 EpDesc
->EndpointAddress
512 // Delete and submit a new async interrupt
514 Status
= UsbIo
->UsbAsyncInterruptTransfer (
516 EpDesc
->EndpointAddress
,
524 if (EFI_ERROR (Status
)) {
525 DEBUG (( EFI_D_ERROR
, "UsbOnHubInterrupt: failed to remove async transfer - %r\n", Status
));
529 Status
= UsbIo
->UsbAsyncInterruptTransfer (
531 EpDesc
->EndpointAddress
,
533 USB_HUB_POLL_INTERVAL
,
534 HubIf
->NumOfPort
/ 8 + 1,
539 if (EFI_ERROR (Status
)) {
540 DEBUG (( EFI_D_ERROR
, "UsbOnHubInterrupt: failed to submit new async transfer - %r\n", Status
));
546 if ((DataLength
== 0) || (Data
== NULL
)) {
551 // OK, actually something is changed, save the change map
552 // then signal the HUB to do enumeration. This is a good
553 // practise since UsbOnHubInterrupt is called in the context
554 // of host contrller's AsyncInterrupt monitor.
556 HubIf
->ChangeMap
= AllocateZeroPool (DataLength
);
558 if (HubIf
->ChangeMap
== NULL
) {
559 return EFI_OUT_OF_RESOURCES
;
562 CopyMem (HubIf
->ChangeMap
, Data
, DataLength
);
563 gBS
->SignalEvent (HubIf
->HubNotify
);
572 Initialize the device for a non-root hub.
574 @param HubIf The USB hub interface.
576 @retval EFI_SUCCESS The hub is initialized.
577 @retval EFI_DEVICE_ERROR Failed to initialize the hub.
582 IN USB_INTERFACE
*HubIf
585 UINT8 HubDescBuffer
[256];
586 EFI_USB_HUB_DESCRIPTOR
*HubDesc
;
587 USB_ENDPOINT_DESC
*EpDesc
;
588 USB_INTERFACE_SETTING
*Setting
;
589 EFI_USB_IO_PROTOCOL
*UsbIo
;
597 // Locate the interrupt endpoint for port change map
599 HubIf
->IsHub
= FALSE
;
600 Setting
= HubIf
->IfSetting
;
601 HubDev
= HubIf
->Device
;
603 NumEndpoints
= Setting
->Desc
.NumEndpoints
;
605 for (Index
= 0; Index
< NumEndpoints
; Index
++) {
606 ASSERT ((Setting
->Endpoints
!= NULL
) && (Setting
->Endpoints
[Index
] != NULL
));
608 EpDesc
= Setting
->Endpoints
[Index
];
610 if (USB_BIT_IS_SET (EpDesc
->Desc
.EndpointAddress
, USB_ENDPOINT_DIR_IN
) &&
611 (USB_ENDPOINT_TYPE (&EpDesc
->Desc
) == USB_ENDPOINT_INTERRUPT
)) {
616 if (Index
== NumEndpoints
) {
617 DEBUG (( EFI_D_ERROR
, "UsbHubInit: no interrupt endpoint found for hub %d\n", HubDev
->Address
));
618 return EFI_DEVICE_ERROR
;
622 // The length field of descriptor is UINT8 type, so the buffer
623 // with 256 bytes is enough to hold the descriptor data.
625 HubDesc
= (EFI_USB_HUB_DESCRIPTOR
*) HubDescBuffer
;
626 Status
= UsbHubReadDesc (HubDev
, HubDesc
);
628 if (EFI_ERROR (Status
)) {
629 DEBUG (( EFI_D_ERROR
, "UsbHubInit: failed to read HUB descriptor %r\n", Status
));
633 HubIf
->NumOfPort
= HubDesc
->NumPorts
;
635 DEBUG (( EFI_D_INFO
, "UsbHubInit: hub %d has %d ports\n", HubDev
->Address
,HubIf
->NumOfPort
));
638 // OK, set IsHub to TRUE. Now usb bus can handle this device
639 // as a working HUB. If failed eariler, bus driver will not
640 // recognize it as a hub. Other parts of the bus should be able
644 HubIf
->HubApi
= &mUsbHubApi
;
645 HubIf
->HubEp
= EpDesc
;
647 if (HubIf
->Device
->Speed
== EFI_USB_SPEED_SUPER
) {
648 Depth
= (UINT16
)(HubIf
->Device
->Tier
- 1);
649 DEBUG ((EFI_D_INFO
, "UsbHubInit: Set Hub Depth as 0x%x\n", Depth
));
650 UsbHubCtrlSetHubDepth (HubIf
->Device
, Depth
);
652 for (Index
= 0; Index
< HubDesc
->NumPorts
; Index
++) {
653 UsbHubCtrlSetPortFeature (HubIf
->Device
, Index
, USB_HUB_PORT_REMOTE_WAKE_MASK
);
657 // Feed power to all the hub ports. It should be ok
658 // for both gang/individual powered hubs.
660 for (Index
= 0; Index
< HubDesc
->NumPorts
; Index
++) {
661 UsbHubCtrlSetPortFeature (HubIf
->Device
, Index
, (EFI_USB_PORT_FEATURE
) USB_HUB_PORT_POWER
);
665 // Update for the usb hub has no power on delay requirement
667 if (HubDesc
->PwrOn2PwrGood
> 0) {
668 gBS
->Stall (HubDesc
->PwrOn2PwrGood
* USB_SET_PORT_POWER_STALL
);
670 UsbHubAckHubStatus (HubIf
->Device
);
674 // Create an event to enumerate the hub's port. On
676 Status
= gBS
->CreateEvent (
684 if (EFI_ERROR (Status
)) {
685 DEBUG (( EFI_D_ERROR
, "UsbHubInit: failed to create signal for hub %d - %r\n",
686 HubDev
->Address
, Status
));
692 // Create AsyncInterrupt to query hub port change endpoint
693 // periodically. If the hub ports are changed, hub will return
694 // changed port map from the interrupt endpoint. The port map
695 // must be able to hold (HubIf->NumOfPort + 1) bits (one bit for
696 // host change status).
698 UsbIo
= &HubIf
->UsbIo
;
699 Status
= UsbIo
->UsbAsyncInterruptTransfer (
701 EpDesc
->Desc
.EndpointAddress
,
703 USB_HUB_POLL_INTERVAL
,
704 HubIf
->NumOfPort
/ 8 + 1,
709 if (EFI_ERROR (Status
)) {
710 DEBUG (( EFI_D_ERROR
, "UsbHubInit: failed to queue interrupt transfer for hub %d - %r\n",
711 HubDev
->Address
, Status
));
713 gBS
->CloseEvent (HubIf
->HubNotify
);
714 HubIf
->HubNotify
= NULL
;
719 DEBUG (( EFI_D_INFO
, "UsbHubInit: hub %d initialized\n", HubDev
->Address
));
726 Get the port status. This function is required to
727 ACK the port change bits although it will return
728 the port changes in PortState. Bus enumeration code
729 doesn't need to ACK the port change bits.
731 @param HubIf The hub interface.
732 @param Port The port of the hub to get state.
733 @param PortState Variable to return the port state.
735 @retval EFI_SUCCESS The port status is successfully returned.
736 @retval Others Failed to return the status.
740 UsbHubGetPortStatus (
741 IN USB_INTERFACE
*HubIf
,
743 OUT EFI_USB_PORT_STATUS
*PortState
748 Status
= UsbHubCtrlGetPortStatus (HubIf
->Device
, Port
, PortState
);
756 Clear the port change status.
758 @param HubIf The hub interface.
759 @param Port The hub port.
763 UsbHubClearPortChange (
764 IN USB_INTERFACE
*HubIf
,
768 EFI_USB_PORT_STATUS PortState
;
769 USB_CHANGE_FEATURE_MAP
*Map
;
773 Status
= UsbHubGetPortStatus (HubIf
, Port
, &PortState
);
775 if (EFI_ERROR (Status
)) {
780 // OK, get the usb port status, now ACK the change bits.
781 // Don't return error when failed to clear the change bits.
782 // It may lead to extra port state report. USB bus should
783 // be able to handle this.
785 for (Index
= 0; Index
< ARRAY_SIZE (mHubFeatureMap
); Index
++) {
786 Map
= &mHubFeatureMap
[Index
];
788 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, Map
->ChangedBit
)) {
789 UsbHubCtrlClearPortFeature (HubIf
->Device
, Port
, (UINT16
) Map
->Feature
);
797 Function to set the port feature for non-root hub.
799 @param HubIf The hub interface.
800 @param Port The port of the hub.
801 @param Feature The feature of the port to set.
803 @retval EFI_SUCCESS The hub port feature is set.
804 @retval Others Failed to set the port feature.
808 UsbHubSetPortFeature (
809 IN USB_INTERFACE
*HubIf
,
811 IN EFI_USB_PORT_FEATURE Feature
816 Status
= UsbHubCtrlSetPortFeature (HubIf
->Device
, Port
, (UINT8
) Feature
);
823 Interface function to clear the port feature for non-root hub.
825 @param HubIf The hub interface.
826 @param Port The port of the hub to clear feature for.
827 @param Feature The feature to clear.
829 @retval EFI_SUCCESS The port feature is cleared.
830 @retval Others Failed to clear the port feature.
834 UsbHubClearPortFeature (
835 IN USB_INTERFACE
*HubIf
,
837 IN EFI_USB_PORT_FEATURE Feature
842 Status
= UsbHubCtrlClearPortFeature (HubIf
->Device
, Port
, (UINT8
) Feature
);
849 Interface function to reset the port.
851 @param HubIf The hub interface.
852 @param Port The port to reset.
854 @retval EFI_SUCCESS The hub port is reset.
855 @retval EFI_TIMEOUT Failed to reset the port in time.
856 @retval Others Failed to reset the port.
861 IN USB_INTERFACE
*HubIf
,
865 EFI_USB_PORT_STATUS PortState
;
869 Status
= UsbHubSetPortFeature (HubIf
, Port
, (EFI_USB_PORT_FEATURE
) USB_HUB_PORT_RESET
);
871 if (EFI_ERROR (Status
)) {
876 // Drive the reset signal for worst 20ms. Check USB 2.0 Spec
877 // section 7.1.7.5 for timing requirements.
879 gBS
->Stall (USB_SET_PORT_RESET_STALL
);
882 // Check USB_PORT_STAT_C_RESET bit to see if the resetting state is done.
884 ZeroMem (&PortState
, sizeof (EFI_USB_PORT_STATUS
));
886 for (Index
= 0; Index
< USB_WAIT_PORT_STS_CHANGE_LOOP
; Index
++) {
887 Status
= UsbHubGetPortStatus (HubIf
, Port
, &PortState
);
889 if (EFI_ERROR (Status
)) {
893 if (!EFI_ERROR (Status
) &&
894 USB_BIT_IS_SET (PortState
.PortChangeStatus
, USB_PORT_STAT_C_RESET
)) {
895 gBS
->Stall (USB_SET_PORT_RECOVERY_STALL
);
899 gBS
->Stall (USB_WAIT_PORT_STS_CHANGE_STALL
);
907 Release the hub's control of the interface.
909 @param HubIf The hub interface.
911 @retval EFI_SUCCESS The interface is release of hub control.
916 IN USB_INTERFACE
*HubIf
919 EFI_USB_IO_PROTOCOL
*UsbIo
;
922 UsbIo
= &HubIf
->UsbIo
;
923 Status
= UsbIo
->UsbAsyncInterruptTransfer (
925 HubIf
->HubEp
->Desc
.EndpointAddress
,
927 USB_HUB_POLL_INTERVAL
,
933 if (EFI_ERROR (Status
)) {
937 gBS
->CloseEvent (HubIf
->HubNotify
);
939 HubIf
->IsHub
= FALSE
;
940 HubIf
->HubApi
= NULL
;
942 HubIf
->HubNotify
= NULL
;
944 DEBUG (( EFI_D_INFO
, "UsbHubRelease: hub device %d released\n", HubIf
->Device
->Address
));
951 Initialize the interface for root hub.
953 @param HubIf The root hub interface.
955 @retval EFI_SUCCESS The interface is initialized for root hub.
956 @retval Others Failed to initialize the hub.
961 IN USB_INTERFACE
*HubIf
969 Status
= UsbHcGetCapability (HubIf
->Device
->Bus
, &MaxSpeed
, &NumOfPort
, &Support64
);
971 if (EFI_ERROR (Status
)) {
975 DEBUG (( EFI_D_INFO
, "UsbRootHubInit: root hub %p - max speed %d, %d ports\n",
976 HubIf
, MaxSpeed
, NumOfPort
));
979 HubIf
->HubApi
= &mUsbRootHubApi
;
981 HubIf
->MaxSpeed
= MaxSpeed
;
982 HubIf
->NumOfPort
= NumOfPort
;
983 HubIf
->HubNotify
= NULL
;
986 // Create a timer to poll root hub ports periodically
988 Status
= gBS
->CreateEvent (
989 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
991 UsbRootHubEnumeration
,
996 if (EFI_ERROR (Status
)) {
1001 // It should signal the event immediately here, or device detection
1002 // by bus enumeration might be delayed by the timer interval.
1004 gBS
->SignalEvent (HubIf
->HubNotify
);
1006 Status
= gBS
->SetTimer (
1009 USB_ROOTHUB_POLL_INTERVAL
1012 if (EFI_ERROR (Status
)) {
1013 gBS
->CloseEvent (HubIf
->HubNotify
);
1021 Get the port status. This function is required to
1022 ACK the port change bits although it will return
1023 the port changes in PortState. Bus enumeration code
1024 doesn't need to ACK the port change bits.
1026 @param HubIf The root hub interface.
1027 @param Port The root hub port to get the state.
1028 @param PortState Variable to return the port state.
1030 @retval EFI_SUCCESS The port state is returned.
1031 @retval Others Failed to retrieve the port state.
1035 UsbRootHubGetPortStatus (
1036 IN USB_INTERFACE
*HubIf
,
1038 OUT EFI_USB_PORT_STATUS
*PortState
1044 Bus
= HubIf
->Device
->Bus
;
1045 Status
= UsbHcGetRootHubPortStatus (Bus
, Port
, PortState
);
1052 Clear the port change status.
1054 @param HubIf The root hub interface.
1055 @param Port The root hub port.
1059 UsbRootHubClearPortChange (
1060 IN USB_INTERFACE
*HubIf
,
1064 EFI_USB_PORT_STATUS PortState
;
1065 USB_CHANGE_FEATURE_MAP
*Map
;
1069 Status
= UsbRootHubGetPortStatus (HubIf
, Port
, &PortState
);
1071 if (EFI_ERROR (Status
)) {
1076 // OK, get the usb port status, now ACK the change bits.
1077 // Don't return error when failed to clear the change bits.
1078 // It may lead to extra port state report. USB bus should
1079 // be able to handle this.
1081 for (Index
= 0; Index
< ARRAY_SIZE (mRootHubFeatureMap
); Index
++) {
1082 Map
= &mRootHubFeatureMap
[Index
];
1084 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, Map
->ChangedBit
)) {
1085 UsbHcClearRootHubPortFeature (HubIf
->Device
->Bus
, Port
, (EFI_USB_PORT_FEATURE
) Map
->Feature
);
1092 Set the root hub port feature.
1094 @param HubIf The Usb hub interface.
1095 @param Port The hub port.
1096 @param Feature The feature to set.
1098 @retval EFI_SUCCESS The root hub port is set with the feature.
1099 @retval Others Failed to set the feature.
1103 UsbRootHubSetPortFeature (
1104 IN USB_INTERFACE
*HubIf
,
1106 IN EFI_USB_PORT_FEATURE Feature
1111 Status
= UsbHcSetRootHubPortFeature (HubIf
->Device
->Bus
, Port
, Feature
);
1118 Clear the root hub port feature.
1120 @param HubIf The root hub interface.
1121 @param Port The root hub port.
1122 @param Feature The feature to clear.
1124 @retval EFI_SUCCESS The root hub port is cleared of the feature.
1125 @retval Others Failed to clear the feature.
1129 UsbRootHubClearPortFeature (
1130 IN USB_INTERFACE
*HubIf
,
1132 IN EFI_USB_PORT_FEATURE Feature
1137 Status
= UsbHcClearRootHubPortFeature (HubIf
->Device
->Bus
, Port
, Feature
);
1144 Interface function to reset the root hub port.
1146 @param RootIf The root hub interface.
1147 @param Port The port to reset.
1149 @retval EFI_SUCCESS The hub port is reset.
1150 @retval EFI_TIMEOUT Failed to reset the port in time.
1151 @retval EFI_NOT_FOUND The low/full speed device connected to high speed.
1152 root hub is released to the companion UHCI.
1153 @retval Others Failed to reset the port.
1157 UsbRootHubResetPort (
1158 IN USB_INTERFACE
*RootIf
,
1164 EFI_USB_PORT_STATUS PortState
;
1168 // Notice: although EHCI requires that ENABLED bit be cleared
1169 // when reset the port, we don't need to care that here. It
1170 // should be handled in the EHCI driver.
1172 Bus
= RootIf
->Device
->Bus
;
1174 Status
= UsbHcSetRootHubPortFeature (Bus
, Port
, EfiUsbPortReset
);
1176 if (EFI_ERROR (Status
)) {
1177 DEBUG (( EFI_D_ERROR
, "UsbRootHubResetPort: failed to start reset on port %d\n", Port
));
1182 // Drive the reset signal for at least 50ms. Check USB 2.0 Spec
1183 // section 7.1.7.5 for timing requirements.
1185 gBS
->Stall (USB_SET_ROOT_PORT_RESET_STALL
);
1187 Status
= UsbHcClearRootHubPortFeature (Bus
, Port
, EfiUsbPortReset
);
1189 if (EFI_ERROR (Status
)) {
1190 DEBUG (( EFI_D_ERROR
, "UsbRootHubResetPort: failed to clear reset on port %d\n", Port
));
1194 gBS
->Stall (USB_CLR_ROOT_PORT_RESET_STALL
);
1197 // USB host controller won't clear the RESET bit until
1198 // reset is actually finished.
1200 ZeroMem (&PortState
, sizeof (EFI_USB_PORT_STATUS
));
1202 for (Index
= 0; Index
< USB_WAIT_PORT_STS_CHANGE_LOOP
; Index
++) {
1203 Status
= UsbHcGetRootHubPortStatus (Bus
, Port
, &PortState
);
1205 if (EFI_ERROR (Status
)) {
1209 if (!USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_RESET
)) {
1213 gBS
->Stall (USB_WAIT_PORT_STS_CHANGE_STALL
);
1216 if (Index
== USB_WAIT_PORT_STS_CHANGE_LOOP
) {
1217 DEBUG ((EFI_D_ERROR
, "UsbRootHubResetPort: reset not finished in time on port %d\n", Port
));
1221 if (!USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_ENABLE
)) {
1223 // OK, the port is reset. If root hub is of high speed and
1224 // the device is of low/full speed, release the ownership to
1225 // companion UHCI. If root hub is of full speed, it won't
1226 // automatically enable the port, we need to enable it manually.
1228 if (RootIf
->MaxSpeed
== EFI_USB_SPEED_HIGH
) {
1229 DEBUG (( EFI_D_ERROR
, "UsbRootHubResetPort: release low/full speed device (%d) to UHCI\n", Port
));
1231 UsbRootHubSetPortFeature (RootIf
, Port
, EfiUsbPortOwner
);
1232 return EFI_NOT_FOUND
;
1236 Status
= UsbRootHubSetPortFeature (RootIf
, Port
, EfiUsbPortEnable
);
1238 if (EFI_ERROR (Status
)) {
1239 DEBUG (( EFI_D_ERROR
, "UsbRootHubResetPort: failed to enable port %d for UHCI\n", Port
));
1243 gBS
->Stall (USB_SET_ROOT_PORT_ENABLE_STALL
);
1252 Release the root hub's control of the interface.
1254 @param HubIf The root hub interface.
1256 @retval EFI_SUCCESS The root hub's control of the interface is
1262 IN USB_INTERFACE
*HubIf
1265 DEBUG (( EFI_D_INFO
, "UsbRootHubRelease: root hub released for hub %p\n", HubIf
));
1267 gBS
->SetTimer (HubIf
->HubNotify
, TimerCancel
, USB_ROOTHUB_POLL_INTERVAL
);
1268 gBS
->CloseEvent (HubIf
->HubNotify
);
1273 USB_HUB_API mUsbHubApi
= {
1275 UsbHubGetPortStatus
,
1276 UsbHubClearPortChange
,
1277 UsbHubSetPortFeature
,
1278 UsbHubClearPortFeature
,
1283 USB_HUB_API mUsbRootHubApi
= {
1285 UsbRootHubGetPortStatus
,
1286 UsbRootHubClearPortChange
,
1287 UsbRootHubSetPortFeature
,
1288 UsbRootHubClearPortFeature
,
1289 UsbRootHubResetPort
,