3 Usb bus enumeration support.
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 Return the endpoint descriptor in this interface.
21 @param UsbIf The interface to search in.
22 @param EpAddr The address of the endpoint to return.
24 @return The endpoint descriptor or NULL.
29 IN USB_INTERFACE
*UsbIf
,
33 USB_ENDPOINT_DESC
*EpDesc
;
37 NumEndpoints
= UsbIf
->IfSetting
->Desc
.NumEndpoints
;
39 for (Index
= 0; Index
< NumEndpoints
; Index
++) {
40 EpDesc
= UsbIf
->IfSetting
->Endpoints
[Index
];
42 if (EpDesc
->Desc
.EndpointAddress
== EpAddr
) {
52 Free the resource used by USB interface.
54 @param UsbIf The USB interface to free.
56 @retval EFI_ACCESS_DENIED The interface is still occupied.
57 @retval EFI_SUCCESS The interface is freed.
61 IN USB_INTERFACE
*UsbIf
66 UsbCloseHostProtoByChild (UsbIf
->Device
->Bus
, UsbIf
->Handle
);
68 Status
= gBS
->UninstallMultipleProtocolInterfaces (
70 &gEfiDevicePathProtocolGuid
, UsbIf
->DevicePath
,
71 &gEfiUsbIoProtocolGuid
, &UsbIf
->UsbIo
,
74 if (!EFI_ERROR (Status
)) {
75 if (UsbIf
->DevicePath
!= NULL
) {
76 FreePool (UsbIf
->DevicePath
);
80 UsbOpenHostProtoByChild (UsbIf
->Device
->Bus
, UsbIf
->Handle
);
87 Create an interface for the descriptor IfDesc. Each
88 device's configuration can have several interfaces.
90 @param Device The device has the interface descriptor.
91 @param IfDesc The interface descriptor.
93 @return The created USB interface for the descriptor, or NULL.
98 IN USB_DEVICE
*Device
,
99 IN USB_INTERFACE_DESC
*IfDesc
102 USB_DEVICE_PATH UsbNode
;
103 USB_INTERFACE
*UsbIf
;
104 USB_INTERFACE
*HubIf
;
107 UsbIf
= AllocateZeroPool (sizeof (USB_INTERFACE
));
113 UsbIf
->Signature
= USB_INTERFACE_SIGNATURE
;
114 UsbIf
->Device
= Device
;
115 UsbIf
->IfDesc
= IfDesc
;
116 ASSERT (IfDesc
->ActiveIndex
< USB_MAX_INTERFACE_SETTING
);
117 UsbIf
->IfSetting
= IfDesc
->Settings
[IfDesc
->ActiveIndex
];
122 sizeof (EFI_USB_IO_PROTOCOL
)
126 // Install protocols for USBIO and device path
128 UsbNode
.Header
.Type
= MESSAGING_DEVICE_PATH
;
129 UsbNode
.Header
.SubType
= MSG_USB_DP
;
130 UsbNode
.ParentPortNumber
= Device
->ParentPort
;
131 UsbNode
.InterfaceNumber
= UsbIf
->IfSetting
->Desc
.InterfaceNumber
;
133 SetDevicePathNodeLength (&UsbNode
.Header
, sizeof (UsbNode
));
135 HubIf
= Device
->ParentIf
;
136 ASSERT (HubIf
!= NULL
);
138 UsbIf
->DevicePath
= AppendDevicePathNode (HubIf
->DevicePath
, &UsbNode
.Header
);
140 if (UsbIf
->DevicePath
== NULL
) {
141 DEBUG ((EFI_D_ERROR
, "UsbCreateInterface: failed to create device path\n"));
143 Status
= EFI_OUT_OF_RESOURCES
;
147 Status
= gBS
->InstallMultipleProtocolInterfaces (
149 &gEfiDevicePathProtocolGuid
,
151 &gEfiUsbIoProtocolGuid
,
156 if (EFI_ERROR (Status
)) {
157 DEBUG ((EFI_D_ERROR
, "UsbCreateInterface: failed to install UsbIo - %r\n", Status
));
162 // Open USB Host Controller Protocol by Child
164 Status
= UsbOpenHostProtoByChild (Device
->Bus
, UsbIf
->Handle
);
166 if (EFI_ERROR (Status
)) {
167 gBS
->UninstallMultipleProtocolInterfaces (
169 &gEfiDevicePathProtocolGuid
,
171 &gEfiUsbIoProtocolGuid
,
176 DEBUG ((EFI_D_ERROR
, "UsbCreateInterface: failed to open host for child - %r\n", Status
));
183 if (UsbIf
->DevicePath
!= NULL
) {
184 FreePool (UsbIf
->DevicePath
);
193 Free the resource used by this USB device.
195 @param Device The USB device to free.
200 IN USB_DEVICE
*Device
203 if (Device
->DevDesc
!= NULL
) {
204 UsbFreeDevDesc (Device
->DevDesc
);
207 gBS
->FreePool (Device
);
212 Create a device which is on the parent's ParentPort port.
214 @param ParentIf The parent HUB interface.
215 @param ParentPort The port on the HUB this device is connected to.
217 @return Created USB device, Or NULL.
222 IN USB_INTERFACE
*ParentIf
,
228 ASSERT (ParentIf
!= NULL
);
230 Device
= AllocateZeroPool (sizeof (USB_DEVICE
));
232 if (Device
== NULL
) {
236 Device
->Bus
= ParentIf
->Device
->Bus
;
237 Device
->MaxPacket0
= 8;
238 Device
->ParentAddr
= ParentIf
->Device
->Address
;
239 Device
->ParentIf
= ParentIf
;
240 Device
->ParentPort
= ParentPort
;
241 Device
->Tier
= (UINT8
)(ParentIf
->Device
->Tier
+ 1);
247 Connect the USB interface with its driver. EFI USB bus will
248 create a USB interface for each separate interface descriptor.
250 @param UsbIf The interface to connect driver to.
252 @return EFI_SUCCESS Interface is managed by some driver.
253 @return Others Failed to locate a driver for this interface.
258 IN USB_INTERFACE
*UsbIf
264 Status
= EFI_SUCCESS
;
267 // Hub is maintained by the USB bus driver. Otherwise try to
268 // connect drivers with this interface
270 if (UsbIsHubInterface (UsbIf
)) {
271 DEBUG ((EFI_D_INFO
, "UsbConnectDriver: found a hub device\n"));
272 Status
= mUsbHubApi
.Init (UsbIf
);
276 // This function is called in both UsbIoControlTransfer and
277 // the timer callback in hub enumeration. So, at least it is
278 // called at TPL_CALLBACK. Some driver sitting on USB has
279 // twisted TPL used. It should be no problem for us to connect
280 // or disconnect at CALLBACK.
284 // Only recursively wanted usb child device
286 if (UsbBusIsWantedUsbIO (UsbIf
->Device
->Bus
, UsbIf
)) {
287 OldTpl
= UsbGetCurrentTpl ();
288 DEBUG ((EFI_D_INFO
, "UsbConnectDriver: TPL before connect is %d, %p\n", (UINT32
)OldTpl
, UsbIf
->Handle
));
290 gBS
->RestoreTPL (TPL_CALLBACK
);
292 Status
= gBS
->ConnectController (UsbIf
->Handle
, NULL
, NULL
, TRUE
);
293 UsbIf
->IsManaged
= (BOOLEAN
)!EFI_ERROR (Status
);
295 DEBUG ((EFI_D_INFO
, "UsbConnectDriver: TPL after connect is %d\n", (UINT32
)UsbGetCurrentTpl()));
296 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK
);
298 gBS
->RaiseTPL (OldTpl
);
307 Select an alternate setting for the interface.
308 Each interface can have several mutually exclusive
309 settings. Only one setting is active. It will
310 also reset its endpoints' toggle to zero.
312 @param IfDesc The interface descriptor to set.
313 @param Alternate The alternate setting number to locate.
315 @retval EFI_NOT_FOUND There is no setting with this alternate index.
316 @retval EFI_SUCCESS The interface is set to Alternate setting.
321 IN USB_INTERFACE_DESC
*IfDesc
,
325 USB_INTERFACE_SETTING
*Setting
;
329 // Locate the active alternate setting
333 for (Index
= 0; Index
< IfDesc
->NumOfSetting
; Index
++) {
334 ASSERT (Index
< USB_MAX_INTERFACE_SETTING
);
335 Setting
= IfDesc
->Settings
[Index
];
337 if (Setting
->Desc
.AlternateSetting
== Alternate
) {
342 if (Index
== IfDesc
->NumOfSetting
) {
343 return EFI_NOT_FOUND
;
346 IfDesc
->ActiveIndex
= Index
;
348 ASSERT (Setting
!= NULL
);
349 DEBUG ((EFI_D_INFO
, "UsbSelectSetting: setting %d selected for interface %d\n",
350 Alternate
, Setting
->Desc
.InterfaceNumber
));
353 // Reset the endpoint toggle to zero
355 for (Index
= 0; Index
< Setting
->Desc
.NumEndpoints
; Index
++) {
356 Setting
->Endpoints
[Index
]->Toggle
= 0;
364 Select a new configuration for the device. Each
365 device may support several configurations.
367 @param Device The device to select configuration.
368 @param ConfigValue The index of the configuration ( != 0).
370 @retval EFI_NOT_FOUND There is no configuration with the index.
371 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
372 @retval EFI_SUCCESS The configuration is selected.
377 IN USB_DEVICE
*Device
,
381 USB_DEVICE_DESC
*DevDesc
;
382 USB_CONFIG_DESC
*ConfigDesc
;
383 USB_INTERFACE_DESC
*IfDesc
;
384 USB_INTERFACE
*UsbIf
;
389 // Locate the active config, then set the device's pointer
391 DevDesc
= Device
->DevDesc
;
394 for (Index
= 0; Index
< DevDesc
->Desc
.NumConfigurations
; Index
++) {
395 ConfigDesc
= DevDesc
->Configs
[Index
];
397 if (ConfigDesc
->Desc
.ConfigurationValue
== ConfigValue
) {
402 if (Index
== DevDesc
->Desc
.NumConfigurations
) {
403 return EFI_NOT_FOUND
;
406 Device
->ActiveConfig
= ConfigDesc
;
408 DEBUG ((EFI_D_INFO
, "UsbSelectConfig: config %d selected for device %d\n",
409 ConfigValue
, Device
->Address
));
412 // Create interfaces for each USB interface descriptor.
414 for (Index
= 0; Index
< ConfigDesc
->Desc
.NumInterfaces
; Index
++) {
416 // First select the default interface setting, and reset
417 // the endpoint toggles to zero for its endpoints.
419 IfDesc
= ConfigDesc
->Interfaces
[Index
];
420 UsbSelectSetting (IfDesc
, IfDesc
->Settings
[0]->Desc
.AlternateSetting
);
423 // Create a USB_INTERFACE and install USB_IO and other protocols
425 UsbIf
= UsbCreateInterface (Device
, ConfigDesc
->Interfaces
[Index
]);
428 Device
->NumOfInterface
= Index
;
429 return EFI_OUT_OF_RESOURCES
;
432 ASSERT (Index
< USB_MAX_INTERFACE
);
433 Device
->Interfaces
[Index
] = UsbIf
;
436 // Connect the device to drivers, if it failed, ignore
437 // the error. Don't let the unsupported interfaces to block
438 // the supported interfaces.
440 Status
= UsbConnectDriver (UsbIf
);
442 if (EFI_ERROR (Status
)) {
445 "UsbSelectConfig: failed to connect driver %r, ignored\n",
451 Device
->NumOfInterface
= Index
;
458 Disconnect the USB interface with its driver.
460 @param UsbIf The interface to disconnect driver from.
464 UsbDisconnectDriver (
465 IN USB_INTERFACE
*UsbIf
472 // Release the hub if it's a hub controller, otherwise
473 // disconnect the driver if it is managed by other drivers.
475 Status
= EFI_SUCCESS
;
477 Status
= UsbIf
->HubApi
->Release (UsbIf
);
479 } else if (UsbIf
->IsManaged
) {
481 // This function is called in both UsbIoControlTransfer and
482 // the timer callback in hub enumeration. So, at least it is
483 // called at TPL_CALLBACK. Some driver sitting on USB has
484 // twisted TPL used. It should be no problem for us to connect
485 // or disconnect at CALLBACK.
487 OldTpl
= UsbGetCurrentTpl ();
488 DEBUG ((EFI_D_INFO
, "UsbDisconnectDriver: old TPL is %d, %p\n", (UINT32
)OldTpl
, UsbIf
->Handle
));
490 gBS
->RestoreTPL (TPL_CALLBACK
);
492 Status
= gBS
->DisconnectController (UsbIf
->Handle
, NULL
, NULL
);
493 if (!EFI_ERROR (Status
)) {
494 UsbIf
->IsManaged
= FALSE
;
497 DEBUG (( EFI_D_INFO
, "UsbDisconnectDriver: TPL after disconnect is %d, %d\n", (UINT32
)UsbGetCurrentTpl(), Status
));
498 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK
);
500 gBS
->RaiseTPL (OldTpl
);
508 Remove the current device configuration.
510 @param Device The USB device to remove configuration from.
515 IN USB_DEVICE
*Device
518 USB_INTERFACE
*UsbIf
;
521 EFI_STATUS ReturnStatus
;
524 // Remove each interface of the device
526 ReturnStatus
= EFI_SUCCESS
;
527 for (Index
= 0; Index
< Device
->NumOfInterface
; Index
++) {
528 ASSERT (Index
< USB_MAX_INTERFACE
);
529 UsbIf
= Device
->Interfaces
[Index
];
535 Status
= UsbDisconnectDriver (UsbIf
);
536 if (!EFI_ERROR (Status
)) {
537 Status
= UsbFreeInterface (UsbIf
);
538 if (EFI_ERROR (Status
)) {
539 UsbConnectDriver (UsbIf
);
543 if (!EFI_ERROR (Status
)) {
544 Device
->Interfaces
[Index
] = NULL
;
546 ReturnStatus
= Status
;
550 Device
->ActiveConfig
= NULL
;
556 Remove the device and all its children from the bus.
558 @param Device The device to remove.
560 @retval EFI_SUCCESS The device is removed.
565 IN USB_DEVICE
*Device
571 EFI_STATUS ReturnStatus
;
577 // Remove all the devices on its downstream ports. Search from devices[1].
578 // Devices[0] is the root hub.
580 ReturnStatus
= EFI_SUCCESS
;
581 for (Index
= 1; Index
< Bus
->MaxDevices
; Index
++) {
582 Child
= Bus
->Devices
[Index
];
584 if ((Child
== NULL
) || (Child
->ParentAddr
!= Device
->Address
)) {
588 Status
= UsbRemoveDevice (Child
);
590 if (!EFI_ERROR (Status
)) {
591 Bus
->Devices
[Index
] = NULL
;
593 Bus
->Devices
[Index
]->DisconnectFail
= TRUE
;
594 ReturnStatus
= Status
;
595 DEBUG ((EFI_D_INFO
, "UsbRemoveDevice: failed to remove child %p at parent %p\n", Child
, Device
));
599 if (EFI_ERROR (ReturnStatus
)) {
603 Status
= UsbRemoveConfig (Device
);
605 if (!EFI_ERROR (Status
)) {
606 DEBUG (( EFI_D_INFO
, "UsbRemoveDevice: device %d removed\n", Device
->Address
));
608 ASSERT (Device
->Address
< Bus
->MaxDevices
);
609 Bus
->Devices
[Device
->Address
] = NULL
;
610 UsbFreeDevice (Device
);
612 Bus
->Devices
[Device
->Address
]->DisconnectFail
= TRUE
;
619 Find the child device on the hub's port.
621 @param HubIf The hub interface.
622 @param Port The port of the hub this child is connected to.
624 @return The device on the hub's port, or NULL if there is none.
629 IN USB_INTERFACE
*HubIf
,
637 Bus
= HubIf
->Device
->Bus
;
640 // Start checking from device 1, device 0 is the root hub
642 for (Index
= 1; Index
< Bus
->MaxDevices
; Index
++) {
643 Device
= Bus
->Devices
[Index
];
645 if ((Device
!= NULL
) && (Device
->ParentAddr
== HubIf
->Device
->Address
) &&
646 (Device
->ParentPort
== Port
)) {
657 Enumerate and configure the new device on the port of this HUB interface.
659 @param HubIf The HUB that has the device connected.
660 @param Port The port index of the hub (started with zero).
661 @param ResetIsNeeded The boolean to control whether skip the reset of the port.
663 @retval EFI_SUCCESS The device is enumerated (added or removed).
664 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device.
665 @retval Others Failed to enumerate the device.
670 IN USB_INTERFACE
*HubIf
,
672 IN BOOLEAN ResetIsNeeded
679 EFI_USB_PORT_STATUS PortState
;
684 Parent
= HubIf
->Device
;
686 HubApi
= HubIf
->HubApi
;
687 Address
= Bus
->MaxDevices
;
689 gBS
->Stall (USB_WAIT_PORT_STABLE_STALL
);
692 // Hub resets the device for at least 10 milliseconds.
693 // Host learns device speed. If device is of low/full speed
694 // and the hub is a EHCI root hub, ResetPort will release
695 // the device to its companion UHCI and return an error.
698 Status
= HubApi
->ResetPort (HubIf
, Port
);
699 if (EFI_ERROR (Status
)) {
700 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to reset port %d - %r\n", Port
, Status
));
704 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: hub port %d is reset\n", Port
));
706 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: hub port %d reset is skipped\n", Port
));
709 Child
= UsbCreateDevice (HubIf
, Port
);
712 return EFI_OUT_OF_RESOURCES
;
716 // OK, now identify the device speed. After reset, hub
717 // fully knows the actual device speed.
719 Status
= HubApi
->GetPortStatus (HubIf
, Port
, &PortState
);
721 if (EFI_ERROR (Status
)) {
722 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to get speed of port %d\n", Port
));
726 if (!USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_CONNECTION
)) {
727 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: No device present at port %d\n", Port
));
729 } else if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_SUPER_SPEED
)){
730 Child
->Speed
= EFI_USB_SPEED_SUPER
;
731 Child
->MaxPacket0
= 512;
732 } else if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_HIGH_SPEED
)) {
733 Child
->Speed
= EFI_USB_SPEED_HIGH
;
734 Child
->MaxPacket0
= 64;
735 } else if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_LOW_SPEED
)) {
736 Child
->Speed
= EFI_USB_SPEED_LOW
;
737 Child
->MaxPacket0
= 8;
739 Child
->Speed
= EFI_USB_SPEED_FULL
;
740 Child
->MaxPacket0
= 8;
743 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: device is of %d speed\n", Child
->Speed
));
745 if (((Child
->Speed
== EFI_USB_SPEED_LOW
) || (Child
->Speed
== EFI_USB_SPEED_FULL
)) &&
746 (Parent
->Speed
== EFI_USB_SPEED_HIGH
)) {
748 // If the child is a low or full speed device, it is necessary to
749 // set the transaction translator. Port TT is 1-based.
750 // This is quite simple:
751 // 1. if parent is of high speed, then parent is our translator
752 // 2. otherwise use parent's translator.
754 Child
->Translator
.TranslatorHubAddress
= Parent
->Address
;
755 Child
->Translator
.TranslatorPortNumber
= (UINT8
) (Port
+ 1);
757 Child
->Translator
= Parent
->Translator
;
759 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: device uses translator (%d, %d)\n",
760 Child
->Translator
.TranslatorHubAddress
,
761 Child
->Translator
.TranslatorPortNumber
));
764 // After port is reset, hub establishes a signal path between
765 // the device and host (DEFALUT state). Device's registers are
766 // reset, use default address 0 (host enumerates one device at
767 // a time) , and ready to respond to control transfer at EP 0.
771 // Host assigns an address to the device. Device completes the
772 // status stage with default address, then switches to new address.
773 // ADDRESS state. Address zero is reserved for root hub.
775 ASSERT (Bus
->MaxDevices
<= 256);
776 for (Address
= 1; Address
< Bus
->MaxDevices
; Address
++) {
777 if (Bus
->Devices
[Address
] == NULL
) {
782 if (Address
>= Bus
->MaxDevices
) {
783 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: address pool is full for port %d\n", Port
));
785 Status
= EFI_ACCESS_DENIED
;
789 Status
= UsbSetAddress (Child
, (UINT8
)Address
);
790 Child
->Address
= (UINT8
)Address
;
791 Bus
->Devices
[Address
] = Child
;
793 if (EFI_ERROR (Status
)) {
794 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to set device address - %r\n", Status
));
798 gBS
->Stall (USB_SET_DEVICE_ADDRESS_STALL
);
800 DEBUG ((EFI_D_INFO
, "UsbEnumerateNewDev: device is now ADDRESSED at %d\n", Address
));
803 // Host sends a Get_Descriptor request to learn the max packet
804 // size of default pipe (only part of the device's descriptor).
806 Status
= UsbGetMaxPacketSize0 (Child
);
808 if (EFI_ERROR (Status
)) {
809 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to get max packet for EP 0 - %r\n", Status
));
813 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: max packet size for EP 0 is %d\n", Child
->MaxPacket0
));
816 // Host learns about the device's abilities by requesting device's
817 // entire descriptions.
819 Status
= UsbBuildDescTable (Child
);
821 if (EFI_ERROR (Status
)) {
822 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to build descriptor table - %r\n", Status
));
827 // Select a default configuration: UEFI must set the configuration
828 // before the driver can connect to the device.
830 Config
= Child
->DevDesc
->Configs
[0]->Desc
.ConfigurationValue
;
831 Status
= UsbSetConfig (Child
, Config
);
833 if (EFI_ERROR (Status
)) {
834 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to set configure %d - %r\n", Config
, Status
));
838 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: device %d is now in CONFIGED state\n", Address
));
841 // Host assigns and loads a device driver.
843 Status
= UsbSelectConfig (Child
, Config
);
845 if (EFI_ERROR (Status
)) {
846 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to create interfaces - %r\n", Status
));
851 // Report Status Code to indicate USB device has been detected by hotplug
853 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
855 (EFI_IO_BUS_USB
| EFI_IOB_PC_HOTPLUG
),
862 // If reach here, it means the enumeration process on a given port is interrupted due to error.
863 // The s/w resources, including the assigned address(Address) and the allocated usb device data
864 // structure(Bus->Devices[Address]), will NOT be freed here. These resources will be freed when
865 // the device is unplugged from the port or DriverBindingStop() is invoked.
867 // This way is used to co-work with the lower layer EDKII UHCI/EHCI/XHCI host controller driver.
868 // It's mainly because to keep UEFI spec unchanged EDKII XHCI driver have to maintain a state machine
869 // to keep track of the mapping between actual address and request address. If the request address
870 // (Address) is freed here, the Address value will be used by next enumerated device. Then EDKII XHCI
871 // host controller driver will have wrong information, which will cause further transaction error.
873 // EDKII UHCI/EHCI doesn't get impacted as it's make sense to reserve s/w resource till it gets unplugged.
880 Process the events on the port.
882 @param HubIf The HUB that has the device connected.
883 @param Port The port index of the hub (started with zero).
885 @retval EFI_SUCCESS The device is enumerated (added or removed).
886 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device.
887 @retval Others Failed to enumerate the device.
892 IN USB_INTERFACE
*HubIf
,
898 EFI_USB_PORT_STATUS PortState
;
902 HubApi
= HubIf
->HubApi
;
905 // Host learns of the new device by polling the hub for port changes.
907 Status
= HubApi
->GetPortStatus (HubIf
, Port
, &PortState
);
909 if (EFI_ERROR (Status
)) {
910 DEBUG ((EFI_D_ERROR
, "UsbEnumeratePort: failed to get state of port %d\n", Port
));
915 // Only handle connection/enable/overcurrent/reset change.
916 // Usb super speed hub may report other changes, such as warm reset change. Ignore them.
918 if ((PortState
.PortChangeStatus
& (USB_PORT_STAT_C_CONNECTION
| USB_PORT_STAT_C_ENABLE
| USB_PORT_STAT_C_OVERCURRENT
| USB_PORT_STAT_C_RESET
)) == 0) {
922 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: port %d state - %02x, change - %02x on %p\n",
923 Port
, PortState
.PortStatus
, PortState
.PortChangeStatus
, HubIf
));
926 // This driver only process two kinds of events now: over current and
927 // connect/disconnect. Other three events are: ENABLE, SUSPEND, RESET.
928 // ENABLE/RESET is used to reset port. SUSPEND isn't supported.
931 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, USB_PORT_STAT_C_OVERCURRENT
)) {
933 if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_OVERCURRENT
)) {
936 // Both OverCurrent and OverCurrentChange set, means over current occurs,
937 // which probably is caused by short circuit. It has to wait system hardware
938 // to perform recovery.
940 DEBUG (( EFI_D_ERROR
, "UsbEnumeratePort: Critical Over Current\n", Port
));
941 return EFI_DEVICE_ERROR
;
946 // Only OverCurrentChange set, means system has been recoveried from
947 // over current. As a result, all ports are nearly power-off, so
948 // it's necessary to detach and enumerate all ports again.
950 DEBUG (( EFI_D_ERROR
, "UsbEnumeratePort: 2.0 device Recovery Over Current\n", Port
));
953 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, USB_PORT_STAT_C_ENABLE
)) {
956 // 1.1 roothub port reg doesn't reflect over-current state, while its counterpart
957 // on 2.0 roothub does. When over-current has influence on 1.1 device, the port
958 // would be disabled, so it's also necessary to detach and enumerate again.
960 DEBUG (( EFI_D_ERROR
, "UsbEnumeratePort: 1.1 device Recovery Over Current\n", Port
));
963 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, USB_PORT_STAT_C_CONNECTION
)) {
966 // Device connected or disconnected normally.
968 DEBUG ((EFI_D_INFO
, "UsbEnumeratePort: Device Connect/Disconnect Normally\n", Port
));
972 // Following as the above cases, it's safety to remove and create again.
974 Child
= UsbFindChild (HubIf
, Port
);
977 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: device at port %d removed from root hub %p\n", Port
, HubIf
));
978 UsbRemoveDevice (Child
);
981 if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_CONNECTION
)) {
983 // Now, new device connected, enumerate and configure the device
985 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: new device connected at port %d\n", Port
));
986 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, USB_PORT_STAT_C_RESET
)) {
987 Status
= UsbEnumerateNewDev (HubIf
, Port
, FALSE
);
989 Status
= UsbEnumerateNewDev (HubIf
, Port
, TRUE
);
993 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: device disconnected event on port %d\n", Port
));
996 HubApi
->ClearPortChange (HubIf
, Port
);
1002 Enumerate all the changed hub ports.
1004 @param Event The event that is triggered.
1005 @param Context The context to the event.
1015 USB_INTERFACE
*HubIf
;
1021 ASSERT (Context
!= NULL
);
1023 HubIf
= (USB_INTERFACE
*) Context
;
1025 for (Index
= 0; Index
< HubIf
->NumOfPort
; Index
++) {
1026 Child
= UsbFindChild (HubIf
, Index
);
1027 if ((Child
!= NULL
) && (Child
->DisconnectFail
== TRUE
)) {
1028 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: The device disconnect fails at port %d from hub %p, try again\n", Index
, HubIf
));
1029 UsbRemoveDevice (Child
);
1033 if (HubIf
->ChangeMap
== NULL
) {
1038 // HUB starts its port index with 1.
1043 for (Index
= 0; Index
< HubIf
->NumOfPort
; Index
++) {
1044 if (USB_BIT_IS_SET (HubIf
->ChangeMap
[Byte
], USB_BIT (Bit
))) {
1045 UsbEnumeratePort (HubIf
, Index
);
1048 USB_NEXT_BIT (Byte
, Bit
);
1051 UsbHubAckHubStatus (HubIf
->Device
);
1053 gBS
->FreePool (HubIf
->ChangeMap
);
1054 HubIf
->ChangeMap
= NULL
;
1060 Enumerate all the changed hub ports.
1062 @param Event The event that is triggered.
1063 @param Context The context to the event.
1068 UsbRootHubEnumeration (
1073 USB_INTERFACE
*RootHub
;
1077 RootHub
= (USB_INTERFACE
*) Context
;
1079 for (Index
= 0; Index
< RootHub
->NumOfPort
; Index
++) {
1080 Child
= UsbFindChild (RootHub
, Index
);
1081 if ((Child
!= NULL
) && (Child
->DisconnectFail
== TRUE
)) {
1082 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: The device disconnect fails at port %d from root hub %p, try again\n", Index
, RootHub
));
1083 UsbRemoveDevice (Child
);
1086 UsbEnumeratePort (RootHub
, Index
);