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 reset the TT (Transaction Transaltor).
323 @param HubDev The hub device.
324 @param Port The port of the hub.
326 @retval EFI_SUCCESS The TT of the hub is reset.
327 @retval Others Failed to reset the port.
332 IN USB_DEVICE
*HubDev
,
338 Status
= UsbCtrlRequest (
343 USB_HUB_REQ_RESET_TT
,
355 Usb hub control transfer to set the hub feature.
357 @param HubDev The hub device.
358 @param Feature The feature to set.
360 @retval EFI_SUCESS The feature is set for the hub.
361 @retval Others Failed to set the feature.
365 UsbHubCtrlSetHubFeature (
366 IN USB_DEVICE
*HubDev
,
372 Status
= UsbCtrlRequest (
377 USB_HUB_REQ_SET_FEATURE
,
389 Usb hub control transfer to set the port feature.
391 @param HubDev The Usb hub device.
392 @param Port The Usb port to set feature for.
393 @param Feature The feature to set.
395 @retval EFI_SUCCESS The feature is set for the port.
396 @retval Others Failed to set the feature.
400 UsbHubCtrlSetPortFeature (
401 IN USB_DEVICE
*HubDev
,
409 // In USB bus, all the port index starts from 0. But HUB
410 // indexes its port from 1. So, port number is added one.
412 Status
= UsbCtrlRequest (
417 USB_HUB_REQ_SET_FEATURE
,
429 Read the whole usb hub descriptor. It is necessary
430 to do it in two steps because hub descriptor is of
433 @param HubDev The hub device.
434 @param HubDesc The variable to return the descriptor.
436 @retval EFI_SUCCESS The hub descriptor is read.
437 @retval Others Failed to read the hub descriptor.
442 IN USB_DEVICE
*HubDev
,
443 OUT EFI_USB_HUB_DESCRIPTOR
*HubDesc
449 // First get the hub descriptor length
451 Status
= UsbHubCtrlGetHubDesc (HubDev
, HubDesc
, 2);
453 if (EFI_ERROR (Status
)) {
458 // Get the whole hub descriptor
460 return UsbHubCtrlGetHubDesc (HubDev
, HubDesc
, HubDesc
->Length
);
466 Ack the hub change bits. If these bits are not ACKed, Hub will
467 always return changed bit map from its interrupt endpoint.
469 @param HubDev The hub device.
471 @retval EFI_SUCCESS The hub change status is ACKed.
472 @retval Others Failed to ACK the hub status.
477 IN USB_DEVICE
*HubDev
480 EFI_USB_PORT_STATUS HubState
;
483 Status
= UsbHubCtrlGetHubStatus (HubDev
, (UINT32
*) &HubState
);
485 if (EFI_ERROR (Status
)) {
489 if (USB_BIT_IS_SET (HubState
.PortChangeStatus
, USB_HUB_STAT_C_LOCAL_POWER
)) {
490 UsbHubCtrlClearHubFeature (HubDev
, USB_HUB_C_HUB_LOCAL_POWER
);
493 if (USB_BIT_IS_SET (HubState
.PortChangeStatus
, USB_HUB_STAT_C_OVER_CURRENT
)) {
494 UsbHubCtrlClearHubFeature (HubDev
, USB_HUB_C_HUB_OVER_CURRENT
);
502 Test whether the interface is a hub interface.
504 @param UsbIf The interface to test.
506 @retval TRUE The interface is a hub interface.
507 @retval FALSE The interface isn't a hub interface.
512 IN USB_INTERFACE
*UsbIf
515 EFI_USB_INTERFACE_DESCRIPTOR
*Setting
;
518 // If the hub is a high-speed hub with multiple TT,
519 // the hub will has a default setting of single TT.
521 Setting
= &UsbIf
->IfSetting
->Desc
;
523 if ((Setting
->InterfaceClass
== USB_HUB_CLASS_CODE
) &&
524 (Setting
->InterfaceSubClass
== USB_HUB_SUBCLASS_CODE
)) {
534 The callback function to the USB hub status change
535 interrupt endpoint. It is called periodically by
536 the underlying host controller.
538 @param Data The data read.
539 @param DataLength The length of the data read.
540 @param Context The context.
541 @param Result The result of the last interrupt transfer.
543 @retval EFI_SUCCESS The process is OK.
544 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
556 USB_INTERFACE
*HubIf
;
557 EFI_USB_IO_PROTOCOL
*UsbIo
;
558 EFI_USB_ENDPOINT_DESCRIPTOR
*EpDesc
;
561 HubIf
= (USB_INTERFACE
*) Context
;
562 UsbIo
= &(HubIf
->UsbIo
);
563 EpDesc
= &(HubIf
->HubEp
->Desc
);
565 if (Result
!= EFI_USB_NOERROR
) {
567 // If endpoint is stalled, clear the stall. Use UsbIo to access
568 // the control transfer so internal status are maintained.
570 if (USB_BIT_IS_SET (Result
, EFI_USB_ERR_STALL
)) {
574 USB_FEATURE_ENDPOINT_HALT
,
575 EpDesc
->EndpointAddress
580 // Delete and submit a new async interrupt
582 Status
= UsbIo
->UsbAsyncInterruptTransfer (
584 EpDesc
->EndpointAddress
,
592 if (EFI_ERROR (Status
)) {
593 DEBUG (( EFI_D_ERROR
, "UsbOnHubInterrupt: failed to remove async transfer - %r\n", Status
));
597 Status
= UsbIo
->UsbAsyncInterruptTransfer (
599 EpDesc
->EndpointAddress
,
601 USB_HUB_POLL_INTERVAL
,
602 HubIf
->NumOfPort
/ 8 + 1,
607 if (EFI_ERROR (Status
)) {
608 DEBUG (( EFI_D_ERROR
, "UsbOnHubInterrupt: failed to submit new async transfer - %r\n", Status
));
614 if ((DataLength
== 0) || (Data
== NULL
)) {
619 // OK, actually something is changed, save the change map
620 // then signal the HUB to do enumeration. This is a good
621 // practise since UsbOnHubInterrupt is called in the context
622 // of host contrller's AsyncInterrupt monitor.
624 HubIf
->ChangeMap
= AllocateZeroPool (DataLength
);
626 if (HubIf
->ChangeMap
== NULL
) {
627 return EFI_OUT_OF_RESOURCES
;
630 CopyMem (HubIf
->ChangeMap
, Data
, DataLength
);
631 gBS
->SignalEvent (HubIf
->HubNotify
);
640 Initialize the device for a non-root hub.
642 @param HubIf The USB hub interface.
644 @retval EFI_SUCCESS The hub is initialized.
645 @retval EFI_DEVICE_ERROR Failed to initialize the hub.
650 IN USB_INTERFACE
*HubIf
653 UINT8 HubDescBuffer
[256];
654 EFI_USB_HUB_DESCRIPTOR
*HubDesc
;
655 USB_ENDPOINT_DESC
*EpDesc
;
656 USB_INTERFACE_SETTING
*Setting
;
657 EFI_USB_IO_PROTOCOL
*UsbIo
;
665 // Locate the interrupt endpoint for port change map
667 HubIf
->IsHub
= FALSE
;
668 Setting
= HubIf
->IfSetting
;
669 HubDev
= HubIf
->Device
;
671 NumEndpoints
= Setting
->Desc
.NumEndpoints
;
673 for (Index
= 0; Index
< NumEndpoints
; Index
++) {
674 ASSERT ((Setting
->Endpoints
!= NULL
) && (Setting
->Endpoints
[Index
] != NULL
));
676 EpDesc
= Setting
->Endpoints
[Index
];
678 if (USB_BIT_IS_SET (EpDesc
->Desc
.EndpointAddress
, USB_ENDPOINT_DIR_IN
) &&
679 (USB_ENDPOINT_TYPE (&EpDesc
->Desc
) == USB_ENDPOINT_INTERRUPT
)) {
684 if (Index
== NumEndpoints
) {
685 DEBUG (( EFI_D_ERROR
, "UsbHubInit: no interrupt endpoint found for hub %d\n", HubDev
->Address
));
686 return EFI_DEVICE_ERROR
;
690 // The length field of descriptor is UINT8 type, so the buffer
691 // with 256 bytes is enough to hold the descriptor data.
693 HubDesc
= (EFI_USB_HUB_DESCRIPTOR
*) HubDescBuffer
;
694 Status
= UsbHubReadDesc (HubDev
, HubDesc
);
696 if (EFI_ERROR (Status
)) {
697 DEBUG (( EFI_D_ERROR
, "UsbHubInit: failed to read HUB descriptor %r\n", Status
));
701 HubIf
->NumOfPort
= HubDesc
->NumPorts
;
703 DEBUG (( EFI_D_INFO
, "UsbHubInit: hub %d has %d ports\n", HubDev
->Address
,HubIf
->NumOfPort
));
706 // OK, set IsHub to TRUE. Now usb bus can handle this device
707 // as a working HUB. If failed eariler, bus driver will not
708 // recognize it as a hub. Other parts of the bus should be able
712 HubIf
->HubApi
= &mUsbHubApi
;
713 HubIf
->HubEp
= EpDesc
;
715 if (HubIf
->Device
->Speed
== EFI_USB_SPEED_SUPER
) {
716 Depth
= (UINT16
)(HubIf
->Device
->Tier
- 1);
717 DEBUG ((EFI_D_INFO
, "UsbHubInit: Set Hub Depth as 0x%x\n", Depth
));
718 UsbHubCtrlSetHubDepth (HubIf
->Device
, Depth
);
720 for (Index
= 0; Index
< HubDesc
->NumPorts
; Index
++) {
721 UsbHubCtrlSetPortFeature (HubIf
->Device
, Index
, USB_HUB_PORT_REMOTE_WAKE_MASK
);
725 // Feed power to all the hub ports. It should be ok
726 // for both gang/individual powered hubs.
728 for (Index
= 0; Index
< HubDesc
->NumPorts
; Index
++) {
729 UsbHubCtrlSetPortFeature (HubIf
->Device
, Index
, (EFI_USB_PORT_FEATURE
) USB_HUB_PORT_POWER
);
733 // Update for the usb hub has no power on delay requirement
735 if (HubDesc
->PwrOn2PwrGood
> 0) {
736 gBS
->Stall (HubDesc
->PwrOn2PwrGood
* USB_SET_PORT_POWER_STALL
);
738 UsbHubAckHubStatus (HubIf
->Device
);
742 // Create an event to enumerate the hub's port. On
744 Status
= gBS
->CreateEvent (
752 if (EFI_ERROR (Status
)) {
753 DEBUG (( EFI_D_ERROR
, "UsbHubInit: failed to create signal for hub %d - %r\n",
754 HubDev
->Address
, Status
));
760 // Create AsyncInterrupt to query hub port change endpoint
761 // periodically. If the hub ports are changed, hub will return
762 // changed port map from the interrupt endpoint. The port map
763 // must be able to hold (HubIf->NumOfPort + 1) bits (one bit for
764 // host change status).
766 UsbIo
= &HubIf
->UsbIo
;
767 Status
= UsbIo
->UsbAsyncInterruptTransfer (
769 EpDesc
->Desc
.EndpointAddress
,
771 USB_HUB_POLL_INTERVAL
,
772 HubIf
->NumOfPort
/ 8 + 1,
777 if (EFI_ERROR (Status
)) {
778 DEBUG (( EFI_D_ERROR
, "UsbHubInit: failed to queue interrupt transfer for hub %d - %r\n",
779 HubDev
->Address
, Status
));
781 gBS
->CloseEvent (HubIf
->HubNotify
);
782 HubIf
->HubNotify
= NULL
;
787 DEBUG (( EFI_D_INFO
, "UsbHubInit: hub %d initialized\n", HubDev
->Address
));
794 Get the port status. This function is required to
795 ACK the port change bits although it will return
796 the port changes in PortState. Bus enumeration code
797 doesn't need to ACK the port change bits.
799 @param HubIf The hub interface.
800 @param Port The port of the hub to get state.
801 @param PortState Variable to return the port state.
803 @retval EFI_SUCCESS The port status is successfully returned.
804 @retval Others Failed to return the status.
808 UsbHubGetPortStatus (
809 IN USB_INTERFACE
*HubIf
,
811 OUT EFI_USB_PORT_STATUS
*PortState
816 Status
= UsbHubCtrlGetPortStatus (HubIf
->Device
, Port
, PortState
);
824 Clear the port change status.
826 @param HubIf The hub interface.
827 @param Port The hub port.
831 UsbHubClearPortChange (
832 IN USB_INTERFACE
*HubIf
,
836 EFI_USB_PORT_STATUS PortState
;
837 USB_CHANGE_FEATURE_MAP
*Map
;
841 Status
= UsbHubGetPortStatus (HubIf
, Port
, &PortState
);
843 if (EFI_ERROR (Status
)) {
848 // OK, get the usb port status, now ACK the change bits.
849 // Don't return error when failed to clear the change bits.
850 // It may lead to extra port state report. USB bus should
851 // be able to handle this.
853 for (Index
= 0; Index
< ARRAY_SIZE (mHubFeatureMap
); Index
++) {
854 Map
= &mHubFeatureMap
[Index
];
856 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, Map
->ChangedBit
)) {
857 UsbHubCtrlClearPortFeature (HubIf
->Device
, Port
, (UINT16
) Map
->Feature
);
865 Function to set the port feature for non-root hub.
867 @param HubIf The hub interface.
868 @param Port The port of the hub.
869 @param Feature The feature of the port to set.
871 @retval EFI_SUCCESS The hub port feature is set.
872 @retval Others Failed to set the port feature.
876 UsbHubSetPortFeature (
877 IN USB_INTERFACE
*HubIf
,
879 IN EFI_USB_PORT_FEATURE Feature
884 Status
= UsbHubCtrlSetPortFeature (HubIf
->Device
, Port
, (UINT8
) Feature
);
891 Interface function to clear the port feature for non-root hub.
893 @param HubIf The hub interface.
894 @param Port The port of the hub to clear feature for.
895 @param Feature The feature to clear.
897 @retval EFI_SUCCESS The port feature is cleared.
898 @retval Others Failed to clear the port feature.
902 UsbHubClearPortFeature (
903 IN USB_INTERFACE
*HubIf
,
905 IN EFI_USB_PORT_FEATURE Feature
910 Status
= UsbHubCtrlClearPortFeature (HubIf
->Device
, Port
, (UINT8
) Feature
);
917 Interface function to reset the port.
919 @param HubIf The hub interface.
920 @param Port The port to reset.
922 @retval EFI_SUCCESS The hub port is reset.
923 @retval EFI_TIMEOUT Failed to reset the port in time.
924 @retval Others Failed to reset the port.
929 IN USB_INTERFACE
*HubIf
,
933 EFI_USB_PORT_STATUS PortState
;
937 Status
= UsbHubSetPortFeature (HubIf
, Port
, (EFI_USB_PORT_FEATURE
) USB_HUB_PORT_RESET
);
939 if (EFI_ERROR (Status
)) {
944 // Drive the reset signal for worst 20ms. Check USB 2.0 Spec
945 // section 7.1.7.5 for timing requirements.
947 gBS
->Stall (USB_SET_PORT_RESET_STALL
);
950 // Check USB_PORT_STAT_C_RESET bit to see if the resetting state is done.
952 ZeroMem (&PortState
, sizeof (EFI_USB_PORT_STATUS
));
954 for (Index
= 0; Index
< USB_WAIT_PORT_STS_CHANGE_LOOP
; Index
++) {
955 Status
= UsbHubGetPortStatus (HubIf
, Port
, &PortState
);
957 if (EFI_ERROR (Status
)) {
961 if (!EFI_ERROR (Status
) &&
962 USB_BIT_IS_SET (PortState
.PortChangeStatus
, USB_PORT_STAT_C_RESET
)) {
963 gBS
->Stall (USB_SET_PORT_RECOVERY_STALL
);
967 gBS
->Stall (USB_WAIT_PORT_STS_CHANGE_STALL
);
975 Release the hub's control of the interface.
977 @param HubIf The hub interface.
979 @retval EFI_SUCCESS The interface is release of hub control.
984 IN USB_INTERFACE
*HubIf
987 EFI_USB_IO_PROTOCOL
*UsbIo
;
990 UsbIo
= &HubIf
->UsbIo
;
991 Status
= UsbIo
->UsbAsyncInterruptTransfer (
993 HubIf
->HubEp
->Desc
.EndpointAddress
,
995 USB_HUB_POLL_INTERVAL
,
1001 if (EFI_ERROR (Status
)) {
1005 gBS
->CloseEvent (HubIf
->HubNotify
);
1007 HubIf
->IsHub
= FALSE
;
1008 HubIf
->HubApi
= NULL
;
1009 HubIf
->HubEp
= NULL
;
1010 HubIf
->HubNotify
= NULL
;
1012 DEBUG (( EFI_D_INFO
, "UsbHubRelease: hub device %d released\n", HubIf
->Device
->Address
));
1019 Initialize the interface for root hub.
1021 @param HubIf The root hub interface.
1023 @retval EFI_SUCCESS The interface is initialized for root hub.
1024 @retval Others Failed to initialize the hub.
1029 IN USB_INTERFACE
*HubIf
1037 Status
= UsbHcGetCapability (HubIf
->Device
->Bus
, &MaxSpeed
, &NumOfPort
, &Support64
);
1039 if (EFI_ERROR (Status
)) {
1043 DEBUG (( EFI_D_INFO
, "UsbRootHubInit: root hub %p - max speed %d, %d ports\n",
1044 HubIf
, MaxSpeed
, NumOfPort
));
1046 HubIf
->IsHub
= TRUE
;
1047 HubIf
->HubApi
= &mUsbRootHubApi
;
1048 HubIf
->HubEp
= NULL
;
1049 HubIf
->MaxSpeed
= MaxSpeed
;
1050 HubIf
->NumOfPort
= NumOfPort
;
1051 HubIf
->HubNotify
= NULL
;
1054 // Create a timer to poll root hub ports periodically
1056 Status
= gBS
->CreateEvent (
1057 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1059 UsbRootHubEnumeration
,
1064 if (EFI_ERROR (Status
)) {
1069 // It should signal the event immediately here, or device detection
1070 // by bus enumeration might be delayed by the timer interval.
1072 gBS
->SignalEvent (HubIf
->HubNotify
);
1074 Status
= gBS
->SetTimer (
1077 USB_ROOTHUB_POLL_INTERVAL
1080 if (EFI_ERROR (Status
)) {
1081 gBS
->CloseEvent (HubIf
->HubNotify
);
1089 Get the port status. This function is required to
1090 ACK the port change bits although it will return
1091 the port changes in PortState. Bus enumeration code
1092 doesn't need to ACK the port change bits.
1094 @param HubIf The root hub interface.
1095 @param Port The root hub port to get the state.
1096 @param PortState Variable to return the port state.
1098 @retval EFI_SUCCESS The port state is returned.
1099 @retval Others Failed to retrieve the port state.
1103 UsbRootHubGetPortStatus (
1104 IN USB_INTERFACE
*HubIf
,
1106 OUT EFI_USB_PORT_STATUS
*PortState
1112 Bus
= HubIf
->Device
->Bus
;
1113 Status
= UsbHcGetRootHubPortStatus (Bus
, Port
, PortState
);
1120 Clear the port change status.
1122 @param HubIf The root hub interface.
1123 @param Port The root hub port.
1127 UsbRootHubClearPortChange (
1128 IN USB_INTERFACE
*HubIf
,
1132 EFI_USB_PORT_STATUS PortState
;
1133 USB_CHANGE_FEATURE_MAP
*Map
;
1137 Status
= UsbRootHubGetPortStatus (HubIf
, Port
, &PortState
);
1139 if (EFI_ERROR (Status
)) {
1144 // OK, get the usb port status, now ACK the change bits.
1145 // Don't return error when failed to clear the change bits.
1146 // It may lead to extra port state report. USB bus should
1147 // be able to handle this.
1149 for (Index
= 0; Index
< ARRAY_SIZE (mRootHubFeatureMap
); Index
++) {
1150 Map
= &mRootHubFeatureMap
[Index
];
1152 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, Map
->ChangedBit
)) {
1153 UsbHcClearRootHubPortFeature (HubIf
->Device
->Bus
, Port
, (EFI_USB_PORT_FEATURE
) Map
->Feature
);
1160 Set the root hub port feature.
1162 @param HubIf The Usb hub interface.
1163 @param Port The hub port.
1164 @param Feature The feature to set.
1166 @retval EFI_SUCCESS The root hub port is set with the feature.
1167 @retval Others Failed to set the feature.
1171 UsbRootHubSetPortFeature (
1172 IN USB_INTERFACE
*HubIf
,
1174 IN EFI_USB_PORT_FEATURE Feature
1179 Status
= UsbHcSetRootHubPortFeature (HubIf
->Device
->Bus
, Port
, Feature
);
1186 Clear the root hub port feature.
1188 @param HubIf The root hub interface.
1189 @param Port The root hub port.
1190 @param Feature The feature to clear.
1192 @retval EFI_SUCCESS The root hub port is cleared of the feature.
1193 @retval Others Failed to clear the feature.
1197 UsbRootHubClearPortFeature (
1198 IN USB_INTERFACE
*HubIf
,
1200 IN EFI_USB_PORT_FEATURE Feature
1205 Status
= UsbHcClearRootHubPortFeature (HubIf
->Device
->Bus
, Port
, Feature
);
1212 Interface function to reset the root hub port.
1214 @param RootIf The root hub interface.
1215 @param Port The port to reset.
1217 @retval EFI_SUCCESS The hub port is reset.
1218 @retval EFI_TIMEOUT Failed to reset the port in time.
1219 @retval EFI_NOT_FOUND The low/full speed device connected to high speed.
1220 root hub is released to the companion UHCI.
1221 @retval Others Failed to reset the port.
1225 UsbRootHubResetPort (
1226 IN USB_INTERFACE
*RootIf
,
1232 EFI_USB_PORT_STATUS PortState
;
1236 // Notice: although EHCI requires that ENABLED bit be cleared
1237 // when reset the port, we don't need to care that here. It
1238 // should be handled in the EHCI driver.
1240 Bus
= RootIf
->Device
->Bus
;
1242 Status
= UsbHcSetRootHubPortFeature (Bus
, Port
, EfiUsbPortReset
);
1244 if (EFI_ERROR (Status
)) {
1245 DEBUG (( EFI_D_ERROR
, "UsbRootHubResetPort: failed to start reset on port %d\n", Port
));
1250 // Drive the reset signal for at least 50ms. Check USB 2.0 Spec
1251 // section 7.1.7.5 for timing requirements.
1253 gBS
->Stall (USB_SET_ROOT_PORT_RESET_STALL
);
1255 Status
= UsbHcClearRootHubPortFeature (Bus
, Port
, EfiUsbPortReset
);
1257 if (EFI_ERROR (Status
)) {
1258 DEBUG (( EFI_D_ERROR
, "UsbRootHubResetPort: failed to clear reset on port %d\n", Port
));
1262 gBS
->Stall (USB_CLR_ROOT_PORT_RESET_STALL
);
1265 // USB host controller won't clear the RESET bit until
1266 // reset is actually finished.
1268 ZeroMem (&PortState
, sizeof (EFI_USB_PORT_STATUS
));
1270 for (Index
= 0; Index
< USB_WAIT_PORT_STS_CHANGE_LOOP
; Index
++) {
1271 Status
= UsbHcGetRootHubPortStatus (Bus
, Port
, &PortState
);
1273 if (EFI_ERROR (Status
)) {
1277 if (!USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_RESET
)) {
1281 gBS
->Stall (USB_WAIT_PORT_STS_CHANGE_STALL
);
1284 if (Index
== USB_WAIT_PORT_STS_CHANGE_LOOP
) {
1285 DEBUG ((EFI_D_ERROR
, "UsbRootHubResetPort: reset not finished in time on port %d\n", Port
));
1289 if (!USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_ENABLE
)) {
1291 // OK, the port is reset. If root hub is of high speed and
1292 // the device is of low/full speed, release the ownership to
1293 // companion UHCI. If root hub is of full speed, it won't
1294 // automatically enable the port, we need to enable it manually.
1296 if (RootIf
->MaxSpeed
== EFI_USB_SPEED_HIGH
) {
1297 DEBUG (( EFI_D_ERROR
, "UsbRootHubResetPort: release low/full speed device (%d) to UHCI\n", Port
));
1299 UsbRootHubSetPortFeature (RootIf
, Port
, EfiUsbPortOwner
);
1300 return EFI_NOT_FOUND
;
1304 Status
= UsbRootHubSetPortFeature (RootIf
, Port
, EfiUsbPortEnable
);
1306 if (EFI_ERROR (Status
)) {
1307 DEBUG (( EFI_D_ERROR
, "UsbRootHubResetPort: failed to enable port %d for UHCI\n", Port
));
1311 gBS
->Stall (USB_SET_ROOT_PORT_ENABLE_STALL
);
1320 Release the root hub's control of the interface.
1322 @param HubIf The root hub interface.
1324 @retval EFI_SUCCESS The root hub's control of the interface is
1330 IN USB_INTERFACE
*HubIf
1333 DEBUG (( EFI_D_INFO
, "UsbRootHubRelease: root hub released for hub %p\n", HubIf
));
1335 gBS
->SetTimer (HubIf
->HubNotify
, TimerCancel
, USB_ROOTHUB_POLL_INTERVAL
);
1336 gBS
->CloseEvent (HubIf
->HubNotify
);
1341 USB_HUB_API mUsbHubApi
= {
1343 UsbHubGetPortStatus
,
1344 UsbHubClearPortChange
,
1345 UsbHubSetPortFeature
,
1346 UsbHubClearPortFeature
,
1351 USB_HUB_API mUsbRootHubApi
= {
1353 UsbRootHubGetPortStatus
,
1354 UsbRootHubClearPortChange
,
1355 UsbRootHubSetPortFeature
,
1356 UsbRootHubClearPortFeature
,
1357 UsbRootHubResetPort
,