3 Usb bus enumeration support.
5 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 Return the endpoint descriptor in this interface.
15 @param UsbIf The interface to search in.
16 @param EpAddr The address of the endpoint to return.
18 @return The endpoint descriptor or NULL.
23 IN USB_INTERFACE
*UsbIf
,
27 USB_ENDPOINT_DESC
*EpDesc
;
31 NumEndpoints
= UsbIf
->IfSetting
->Desc
.NumEndpoints
;
33 for (Index
= 0; Index
< NumEndpoints
; Index
++) {
34 EpDesc
= UsbIf
->IfSetting
->Endpoints
[Index
];
36 if (EpDesc
->Desc
.EndpointAddress
== EpAddr
) {
46 Free the resource used by USB interface.
48 @param UsbIf The USB interface to free.
50 @retval EFI_ACCESS_DENIED The interface is still occupied.
51 @retval EFI_SUCCESS The interface is freed.
55 IN USB_INTERFACE
*UsbIf
60 UsbCloseHostProtoByChild (UsbIf
->Device
->Bus
, UsbIf
->Handle
);
62 Status
= gBS
->UninstallMultipleProtocolInterfaces (
64 &gEfiDevicePathProtocolGuid
, UsbIf
->DevicePath
,
65 &gEfiUsbIoProtocolGuid
, &UsbIf
->UsbIo
,
68 if (!EFI_ERROR (Status
)) {
69 if (UsbIf
->DevicePath
!= NULL
) {
70 FreePool (UsbIf
->DevicePath
);
74 UsbOpenHostProtoByChild (UsbIf
->Device
->Bus
, UsbIf
->Handle
);
81 Create an interface for the descriptor IfDesc. Each
82 device's configuration can have several interfaces.
84 @param Device The device has the interface descriptor.
85 @param IfDesc The interface descriptor.
87 @return The created USB interface for the descriptor, or NULL.
92 IN USB_DEVICE
*Device
,
93 IN USB_INTERFACE_DESC
*IfDesc
96 USB_DEVICE_PATH UsbNode
;
101 UsbIf
= AllocateZeroPool (sizeof (USB_INTERFACE
));
107 UsbIf
->Signature
= USB_INTERFACE_SIGNATURE
;
108 UsbIf
->Device
= Device
;
109 UsbIf
->IfDesc
= IfDesc
;
110 ASSERT (IfDesc
->ActiveIndex
< USB_MAX_INTERFACE_SETTING
);
111 UsbIf
->IfSetting
= IfDesc
->Settings
[IfDesc
->ActiveIndex
];
116 sizeof (EFI_USB_IO_PROTOCOL
)
120 // Install protocols for USBIO and device path
122 UsbNode
.Header
.Type
= MESSAGING_DEVICE_PATH
;
123 UsbNode
.Header
.SubType
= MSG_USB_DP
;
124 UsbNode
.ParentPortNumber
= Device
->ParentPort
;
125 UsbNode
.InterfaceNumber
= UsbIf
->IfSetting
->Desc
.InterfaceNumber
;
127 SetDevicePathNodeLength (&UsbNode
.Header
, sizeof (UsbNode
));
129 HubIf
= Device
->ParentIf
;
130 ASSERT (HubIf
!= NULL
);
132 UsbIf
->DevicePath
= AppendDevicePathNode (HubIf
->DevicePath
, &UsbNode
.Header
);
134 if (UsbIf
->DevicePath
== NULL
) {
135 DEBUG ((EFI_D_ERROR
, "UsbCreateInterface: failed to create device path\n"));
137 Status
= EFI_OUT_OF_RESOURCES
;
141 Status
= gBS
->InstallMultipleProtocolInterfaces (
143 &gEfiDevicePathProtocolGuid
,
145 &gEfiUsbIoProtocolGuid
,
150 if (EFI_ERROR (Status
)) {
151 DEBUG ((EFI_D_ERROR
, "UsbCreateInterface: failed to install UsbIo - %r\n", Status
));
156 // Open USB Host Controller Protocol by Child
158 Status
= UsbOpenHostProtoByChild (Device
->Bus
, UsbIf
->Handle
);
160 if (EFI_ERROR (Status
)) {
161 gBS
->UninstallMultipleProtocolInterfaces (
163 &gEfiDevicePathProtocolGuid
,
165 &gEfiUsbIoProtocolGuid
,
170 DEBUG ((EFI_D_ERROR
, "UsbCreateInterface: failed to open host for child - %r\n", Status
));
177 if (UsbIf
->DevicePath
!= NULL
) {
178 FreePool (UsbIf
->DevicePath
);
187 Free the resource used by this USB device.
189 @param Device The USB device to free.
194 IN USB_DEVICE
*Device
197 if (Device
->DevDesc
!= NULL
) {
198 UsbFreeDevDesc (Device
->DevDesc
);
201 gBS
->FreePool (Device
);
206 Create a device which is on the parent's ParentPort port.
208 @param ParentIf The parent HUB interface.
209 @param ParentPort The port on the HUB this device is connected to.
211 @return Created USB device, Or NULL.
216 IN USB_INTERFACE
*ParentIf
,
222 ASSERT (ParentIf
!= NULL
);
224 Device
= AllocateZeroPool (sizeof (USB_DEVICE
));
226 if (Device
== NULL
) {
230 Device
->Bus
= ParentIf
->Device
->Bus
;
231 Device
->MaxPacket0
= 8;
232 Device
->ParentAddr
= ParentIf
->Device
->Address
;
233 Device
->ParentIf
= ParentIf
;
234 Device
->ParentPort
= ParentPort
;
235 Device
->Tier
= (UINT8
)(ParentIf
->Device
->Tier
+ 1);
241 Connect the USB interface with its driver. EFI USB bus will
242 create a USB interface for each separate interface descriptor.
244 @param UsbIf The interface to connect driver to.
246 @return EFI_SUCCESS Interface is managed by some driver.
247 @return Others Failed to locate a driver for this interface.
252 IN USB_INTERFACE
*UsbIf
258 Status
= EFI_SUCCESS
;
261 // Hub is maintained by the USB bus driver. Otherwise try to
262 // connect drivers with this interface
264 if (UsbIsHubInterface (UsbIf
)) {
265 DEBUG ((EFI_D_INFO
, "UsbConnectDriver: found a hub device\n"));
266 Status
= mUsbHubApi
.Init (UsbIf
);
270 // This function is called in both UsbIoControlTransfer and
271 // the timer callback in hub enumeration. So, at least it is
272 // called at TPL_CALLBACK. Some driver sitting on USB has
273 // twisted TPL used. It should be no problem for us to connect
274 // or disconnect at CALLBACK.
278 // Only recursively wanted usb child device
280 if (UsbBusIsWantedUsbIO (UsbIf
->Device
->Bus
, UsbIf
)) {
281 OldTpl
= UsbGetCurrentTpl ();
282 DEBUG ((EFI_D_INFO
, "UsbConnectDriver: TPL before connect is %d, %p\n", (UINT32
)OldTpl
, UsbIf
->Handle
));
284 gBS
->RestoreTPL (TPL_CALLBACK
);
286 Status
= gBS
->ConnectController (UsbIf
->Handle
, NULL
, NULL
, TRUE
);
287 UsbIf
->IsManaged
= (BOOLEAN
)!EFI_ERROR (Status
);
289 DEBUG ((EFI_D_INFO
, "UsbConnectDriver: TPL after connect is %d\n", (UINT32
)UsbGetCurrentTpl()));
290 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK
);
292 gBS
->RaiseTPL (OldTpl
);
301 Select an alternate setting for the interface.
302 Each interface can have several mutually exclusive
303 settings. Only one setting is active. It will
304 also reset its endpoints' toggle to zero.
306 @param IfDesc The interface descriptor to set.
307 @param Alternate The alternate setting number to locate.
309 @retval EFI_NOT_FOUND There is no setting with this alternate index.
310 @retval EFI_SUCCESS The interface is set to Alternate setting.
315 IN USB_INTERFACE_DESC
*IfDesc
,
319 USB_INTERFACE_SETTING
*Setting
;
323 // Locate the active alternate setting
327 for (Index
= 0; Index
< IfDesc
->NumOfSetting
; Index
++) {
328 ASSERT (Index
< USB_MAX_INTERFACE_SETTING
);
329 Setting
= IfDesc
->Settings
[Index
];
331 if (Setting
->Desc
.AlternateSetting
== Alternate
) {
336 if (Index
== IfDesc
->NumOfSetting
) {
337 return EFI_NOT_FOUND
;
340 IfDesc
->ActiveIndex
= Index
;
342 ASSERT (Setting
!= NULL
);
343 DEBUG ((EFI_D_INFO
, "UsbSelectSetting: setting %d selected for interface %d\n",
344 Alternate
, Setting
->Desc
.InterfaceNumber
));
347 // Reset the endpoint toggle to zero
349 for (Index
= 0; Index
< Setting
->Desc
.NumEndpoints
; Index
++) {
350 Setting
->Endpoints
[Index
]->Toggle
= 0;
358 Select a new configuration for the device. Each
359 device may support several configurations.
361 @param Device The device to select configuration.
362 @param ConfigValue The index of the configuration ( != 0).
364 @retval EFI_NOT_FOUND There is no configuration with the index.
365 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
366 @retval EFI_SUCCESS The configuration is selected.
371 IN USB_DEVICE
*Device
,
375 USB_DEVICE_DESC
*DevDesc
;
376 USB_CONFIG_DESC
*ConfigDesc
;
377 USB_INTERFACE_DESC
*IfDesc
;
378 USB_INTERFACE
*UsbIf
;
383 // Locate the active config, then set the device's pointer
385 DevDesc
= Device
->DevDesc
;
388 for (Index
= 0; Index
< DevDesc
->Desc
.NumConfigurations
; Index
++) {
389 ConfigDesc
= DevDesc
->Configs
[Index
];
391 if (ConfigDesc
->Desc
.ConfigurationValue
== ConfigValue
) {
396 if (Index
== DevDesc
->Desc
.NumConfigurations
) {
397 return EFI_NOT_FOUND
;
400 Device
->ActiveConfig
= ConfigDesc
;
402 DEBUG ((EFI_D_INFO
, "UsbSelectConfig: config %d selected for device %d\n",
403 ConfigValue
, Device
->Address
));
406 // Create interfaces for each USB interface descriptor.
408 for (Index
= 0; Index
< ConfigDesc
->Desc
.NumInterfaces
; Index
++) {
410 // First select the default interface setting, and reset
411 // the endpoint toggles to zero for its endpoints.
413 IfDesc
= ConfigDesc
->Interfaces
[Index
];
414 UsbSelectSetting (IfDesc
, IfDesc
->Settings
[0]->Desc
.AlternateSetting
);
417 // Create a USB_INTERFACE and install USB_IO and other protocols
419 UsbIf
= UsbCreateInterface (Device
, ConfigDesc
->Interfaces
[Index
]);
422 Device
->NumOfInterface
= Index
;
423 return EFI_OUT_OF_RESOURCES
;
426 ASSERT (Index
< USB_MAX_INTERFACE
);
427 Device
->Interfaces
[Index
] = UsbIf
;
430 // Connect the device to drivers, if it failed, ignore
431 // the error. Don't let the unsupported interfaces to block
432 // the supported interfaces.
434 Status
= UsbConnectDriver (UsbIf
);
436 if (EFI_ERROR (Status
)) {
439 "UsbSelectConfig: failed to connect driver %r, ignored\n",
445 Device
->NumOfInterface
= Index
;
452 Disconnect the USB interface with its driver.
454 @param UsbIf The interface to disconnect driver from.
458 UsbDisconnectDriver (
459 IN USB_INTERFACE
*UsbIf
466 // Release the hub if it's a hub controller, otherwise
467 // disconnect the driver if it is managed by other drivers.
469 Status
= EFI_SUCCESS
;
471 Status
= UsbIf
->HubApi
->Release (UsbIf
);
473 } else if (UsbIf
->IsManaged
) {
475 // This function is called in both UsbIoControlTransfer and
476 // the timer callback in hub enumeration. So, at least it is
477 // called at TPL_CALLBACK. Some driver sitting on USB has
478 // twisted TPL used. It should be no problem for us to connect
479 // or disconnect at CALLBACK.
481 OldTpl
= UsbGetCurrentTpl ();
482 DEBUG ((EFI_D_INFO
, "UsbDisconnectDriver: old TPL is %d, %p\n", (UINT32
)OldTpl
, UsbIf
->Handle
));
484 gBS
->RestoreTPL (TPL_CALLBACK
);
486 Status
= gBS
->DisconnectController (UsbIf
->Handle
, NULL
, NULL
);
487 if (!EFI_ERROR (Status
)) {
488 UsbIf
->IsManaged
= FALSE
;
491 DEBUG (( EFI_D_INFO
, "UsbDisconnectDriver: TPL after disconnect is %d, %d\n", (UINT32
)UsbGetCurrentTpl(), Status
));
492 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK
);
494 gBS
->RaiseTPL (OldTpl
);
502 Remove the current device configuration.
504 @param Device The USB device to remove configuration from.
509 IN USB_DEVICE
*Device
512 USB_INTERFACE
*UsbIf
;
515 EFI_STATUS ReturnStatus
;
518 // Remove each interface of the device
520 ReturnStatus
= EFI_SUCCESS
;
521 for (Index
= 0; Index
< Device
->NumOfInterface
; Index
++) {
522 ASSERT (Index
< USB_MAX_INTERFACE
);
523 UsbIf
= Device
->Interfaces
[Index
];
529 Status
= UsbDisconnectDriver (UsbIf
);
530 if (!EFI_ERROR (Status
)) {
531 Status
= UsbFreeInterface (UsbIf
);
532 if (EFI_ERROR (Status
)) {
533 UsbConnectDriver (UsbIf
);
537 if (!EFI_ERROR (Status
)) {
538 Device
->Interfaces
[Index
] = NULL
;
540 ReturnStatus
= Status
;
544 Device
->ActiveConfig
= NULL
;
550 Remove the device and all its children from the bus.
552 @param Device The device to remove.
554 @retval EFI_SUCCESS The device is removed.
559 IN USB_DEVICE
*Device
565 EFI_STATUS ReturnStatus
;
571 // Remove all the devices on its downstream ports. Search from devices[1].
572 // Devices[0] is the root hub.
574 ReturnStatus
= EFI_SUCCESS
;
575 for (Index
= 1; Index
< Bus
->MaxDevices
; Index
++) {
576 Child
= Bus
->Devices
[Index
];
578 if ((Child
== NULL
) || (Child
->ParentAddr
!= Device
->Address
)) {
582 Status
= UsbRemoveDevice (Child
);
584 if (!EFI_ERROR (Status
)) {
585 Bus
->Devices
[Index
] = NULL
;
587 Bus
->Devices
[Index
]->DisconnectFail
= TRUE
;
588 ReturnStatus
= Status
;
589 DEBUG ((EFI_D_INFO
, "UsbRemoveDevice: failed to remove child %p at parent %p\n", Child
, Device
));
593 if (EFI_ERROR (ReturnStatus
)) {
597 Status
= UsbRemoveConfig (Device
);
599 if (!EFI_ERROR (Status
)) {
600 DEBUG (( EFI_D_INFO
, "UsbRemoveDevice: device %d removed\n", Device
->Address
));
602 ASSERT (Device
->Address
< Bus
->MaxDevices
);
603 Bus
->Devices
[Device
->Address
] = NULL
;
604 UsbFreeDevice (Device
);
606 Bus
->Devices
[Device
->Address
]->DisconnectFail
= TRUE
;
613 Find the child device on the hub's port.
615 @param HubIf The hub interface.
616 @param Port The port of the hub this child is connected to.
618 @return The device on the hub's port, or NULL if there is none.
623 IN USB_INTERFACE
*HubIf
,
631 Bus
= HubIf
->Device
->Bus
;
634 // Start checking from device 1, device 0 is the root hub
636 for (Index
= 1; Index
< Bus
->MaxDevices
; Index
++) {
637 Device
= Bus
->Devices
[Index
];
639 if ((Device
!= NULL
) && (Device
->ParentAddr
== HubIf
->Device
->Address
) &&
640 (Device
->ParentPort
== Port
)) {
651 Enumerate and configure the new device on the port of this HUB interface.
653 @param HubIf The HUB that has the device connected.
654 @param Port The port index of the hub (started with zero).
655 @param ResetIsNeeded The boolean to control whether skip the reset of the port.
657 @retval EFI_SUCCESS The device is enumerated (added or removed).
658 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device.
659 @retval Others Failed to enumerate the device.
664 IN USB_INTERFACE
*HubIf
,
666 IN BOOLEAN ResetIsNeeded
673 EFI_USB_PORT_STATUS PortState
;
678 Parent
= HubIf
->Device
;
680 HubApi
= HubIf
->HubApi
;
681 Address
= Bus
->MaxDevices
;
683 gBS
->Stall (USB_WAIT_PORT_STABLE_STALL
);
686 // Hub resets the device for at least 10 milliseconds.
687 // Host learns device speed. If device is of low/full speed
688 // and the hub is a EHCI root hub, ResetPort will release
689 // the device to its companion UHCI and return an error.
692 Status
= HubApi
->ResetPort (HubIf
, Port
);
693 if (EFI_ERROR (Status
)) {
694 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to reset port %d - %r\n", Port
, Status
));
698 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: hub port %d is reset\n", Port
));
700 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: hub port %d reset is skipped\n", Port
));
703 Child
= UsbCreateDevice (HubIf
, Port
);
706 return EFI_OUT_OF_RESOURCES
;
710 // OK, now identify the device speed. After reset, hub
711 // fully knows the actual device speed.
713 Status
= HubApi
->GetPortStatus (HubIf
, Port
, &PortState
);
715 if (EFI_ERROR (Status
)) {
716 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to get speed of port %d\n", Port
));
720 if (!USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_CONNECTION
)) {
721 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: No device present at port %d\n", Port
));
723 } else if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_SUPER_SPEED
)){
724 Child
->Speed
= EFI_USB_SPEED_SUPER
;
725 Child
->MaxPacket0
= 512;
726 } else if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_HIGH_SPEED
)) {
727 Child
->Speed
= EFI_USB_SPEED_HIGH
;
728 Child
->MaxPacket0
= 64;
729 } else if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_LOW_SPEED
)) {
730 Child
->Speed
= EFI_USB_SPEED_LOW
;
731 Child
->MaxPacket0
= 8;
733 Child
->Speed
= EFI_USB_SPEED_FULL
;
734 Child
->MaxPacket0
= 8;
737 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: device is of %d speed\n", Child
->Speed
));
739 if (((Child
->Speed
== EFI_USB_SPEED_LOW
) || (Child
->Speed
== EFI_USB_SPEED_FULL
)) &&
740 (Parent
->Speed
== EFI_USB_SPEED_HIGH
)) {
742 // If the child is a low or full speed device, it is necessary to
743 // set the transaction translator. Port TT is 1-based.
744 // This is quite simple:
745 // 1. if parent is of high speed, then parent is our translator
746 // 2. otherwise use parent's translator.
748 Child
->Translator
.TranslatorHubAddress
= Parent
->Address
;
749 Child
->Translator
.TranslatorPortNumber
= (UINT8
) (Port
+ 1);
751 Child
->Translator
= Parent
->Translator
;
753 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: device uses translator (%d, %d)\n",
754 Child
->Translator
.TranslatorHubAddress
,
755 Child
->Translator
.TranslatorPortNumber
));
758 // After port is reset, hub establishes a signal path between
759 // the device and host (DEFALUT state). Device's registers are
760 // reset, use default address 0 (host enumerates one device at
761 // a time) , and ready to respond to control transfer at EP 0.
765 // Host assigns an address to the device. Device completes the
766 // status stage with default address, then switches to new address.
767 // ADDRESS state. Address zero is reserved for root hub.
769 ASSERT (Bus
->MaxDevices
<= 256);
770 for (Address
= 1; Address
< Bus
->MaxDevices
; Address
++) {
771 if (Bus
->Devices
[Address
] == NULL
) {
776 if (Address
>= Bus
->MaxDevices
) {
777 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: address pool is full for port %d\n", Port
));
779 Status
= EFI_ACCESS_DENIED
;
783 Status
= UsbSetAddress (Child
, (UINT8
)Address
);
784 Child
->Address
= (UINT8
)Address
;
785 Bus
->Devices
[Address
] = Child
;
787 if (EFI_ERROR (Status
)) {
788 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to set device address - %r\n", Status
));
792 gBS
->Stall (USB_SET_DEVICE_ADDRESS_STALL
);
794 DEBUG ((EFI_D_INFO
, "UsbEnumerateNewDev: device is now ADDRESSED at %d\n", Address
));
797 // Host sends a Get_Descriptor request to learn the max packet
798 // size of default pipe (only part of the device's descriptor).
800 Status
= UsbGetMaxPacketSize0 (Child
);
802 if (EFI_ERROR (Status
)) {
803 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to get max packet for EP 0 - %r\n", Status
));
807 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: max packet size for EP 0 is %d\n", Child
->MaxPacket0
));
810 // Host learns about the device's abilities by requesting device's
811 // entire descriptions.
813 Status
= UsbBuildDescTable (Child
);
815 if (EFI_ERROR (Status
)) {
816 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to build descriptor table - %r\n", Status
));
821 // Select a default configuration: UEFI must set the configuration
822 // before the driver can connect to the device.
824 Config
= Child
->DevDesc
->Configs
[0]->Desc
.ConfigurationValue
;
825 Status
= UsbSetConfig (Child
, Config
);
827 if (EFI_ERROR (Status
)) {
828 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to set configure %d - %r\n", Config
, Status
));
832 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: device %d is now in CONFIGED state\n", Address
));
835 // Host assigns and loads a device driver.
837 Status
= UsbSelectConfig (Child
, Config
);
839 if (EFI_ERROR (Status
)) {
840 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to create interfaces - %r\n", Status
));
845 // Report Status Code to indicate USB device has been detected by hotplug
847 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
849 (EFI_IO_BUS_USB
| EFI_IOB_PC_HOTPLUG
),
856 // If reach here, it means the enumeration process on a given port is interrupted due to error.
857 // The s/w resources, including the assigned address(Address) and the allocated usb device data
858 // structure(Bus->Devices[Address]), will NOT be freed here. These resources will be freed when
859 // the device is unplugged from the port or DriverBindingStop() is invoked.
861 // This way is used to co-work with the lower layer EDKII UHCI/EHCI/XHCI host controller driver.
862 // It's mainly because to keep UEFI spec unchanged EDKII XHCI driver have to maintain a state machine
863 // to keep track of the mapping between actual address and request address. If the request address
864 // (Address) is freed here, the Address value will be used by next enumerated device. Then EDKII XHCI
865 // host controller driver will have wrong information, which will cause further transaction error.
867 // EDKII UHCI/EHCI doesn't get impacted as it's make sense to reserve s/w resource till it gets unplugged.
874 Process the events on the port.
876 @param HubIf The HUB that has the device connected.
877 @param Port The port index of the hub (started with zero).
879 @retval EFI_SUCCESS The device is enumerated (added or removed).
880 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device.
881 @retval Others Failed to enumerate the device.
886 IN USB_INTERFACE
*HubIf
,
892 EFI_USB_PORT_STATUS PortState
;
896 HubApi
= HubIf
->HubApi
;
899 // Host learns of the new device by polling the hub for port changes.
901 Status
= HubApi
->GetPortStatus (HubIf
, Port
, &PortState
);
903 if (EFI_ERROR (Status
)) {
904 DEBUG ((EFI_D_ERROR
, "UsbEnumeratePort: failed to get state of port %d\n", Port
));
909 // Only handle connection/enable/overcurrent/reset change.
910 // Usb super speed hub may report other changes, such as warm reset change. Ignore them.
912 if ((PortState
.PortChangeStatus
& (USB_PORT_STAT_C_CONNECTION
| USB_PORT_STAT_C_ENABLE
| USB_PORT_STAT_C_OVERCURRENT
| USB_PORT_STAT_C_RESET
)) == 0) {
916 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: port %d state - %02x, change - %02x on %p\n",
917 Port
, PortState
.PortStatus
, PortState
.PortChangeStatus
, HubIf
));
920 // This driver only process two kinds of events now: over current and
921 // connect/disconnect. Other three events are: ENABLE, SUSPEND, RESET.
922 // ENABLE/RESET is used to reset port. SUSPEND isn't supported.
925 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, USB_PORT_STAT_C_OVERCURRENT
)) {
927 if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_OVERCURRENT
)) {
930 // Both OverCurrent and OverCurrentChange set, means over current occurs,
931 // which probably is caused by short circuit. It has to wait system hardware
932 // to perform recovery.
934 DEBUG (( EFI_D_ERROR
, "UsbEnumeratePort: Critical Over Current\n", Port
));
935 return EFI_DEVICE_ERROR
;
940 // Only OverCurrentChange set, means system has been recoveried from
941 // over current. As a result, all ports are nearly power-off, so
942 // it's necessary to detach and enumerate all ports again.
944 DEBUG (( EFI_D_ERROR
, "UsbEnumeratePort: 2.0 device Recovery Over Current\n", Port
));
947 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, USB_PORT_STAT_C_ENABLE
)) {
950 // 1.1 roothub port reg doesn't reflect over-current state, while its counterpart
951 // on 2.0 roothub does. When over-current has influence on 1.1 device, the port
952 // would be disabled, so it's also necessary to detach and enumerate again.
954 DEBUG (( EFI_D_ERROR
, "UsbEnumeratePort: 1.1 device Recovery Over Current\n", Port
));
957 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, USB_PORT_STAT_C_CONNECTION
)) {
960 // Device connected or disconnected normally.
962 DEBUG ((EFI_D_INFO
, "UsbEnumeratePort: Device Connect/Disconnect Normally\n", Port
));
966 // Following as the above cases, it's safety to remove and create again.
968 Child
= UsbFindChild (HubIf
, Port
);
971 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: device at port %d removed from root hub %p\n", Port
, HubIf
));
972 UsbRemoveDevice (Child
);
975 if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_CONNECTION
)) {
977 // Now, new device connected, enumerate and configure the device
979 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: new device connected at port %d\n", Port
));
980 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, USB_PORT_STAT_C_RESET
)) {
981 Status
= UsbEnumerateNewDev (HubIf
, Port
, FALSE
);
983 Status
= UsbEnumerateNewDev (HubIf
, Port
, TRUE
);
987 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: device disconnected event on port %d\n", Port
));
990 HubApi
->ClearPortChange (HubIf
, Port
);
996 Enumerate all the changed hub ports.
998 @param Event The event that is triggered.
999 @param Context The context to the event.
1009 USB_INTERFACE
*HubIf
;
1015 ASSERT (Context
!= NULL
);
1017 HubIf
= (USB_INTERFACE
*) Context
;
1019 for (Index
= 0; Index
< HubIf
->NumOfPort
; Index
++) {
1020 Child
= UsbFindChild (HubIf
, Index
);
1021 if ((Child
!= NULL
) && (Child
->DisconnectFail
== TRUE
)) {
1022 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: The device disconnect fails at port %d from hub %p, try again\n", Index
, HubIf
));
1023 UsbRemoveDevice (Child
);
1027 if (HubIf
->ChangeMap
== NULL
) {
1032 // HUB starts its port index with 1.
1037 for (Index
= 0; Index
< HubIf
->NumOfPort
; Index
++) {
1038 if (USB_BIT_IS_SET (HubIf
->ChangeMap
[Byte
], USB_BIT (Bit
))) {
1039 UsbEnumeratePort (HubIf
, Index
);
1042 USB_NEXT_BIT (Byte
, Bit
);
1045 UsbHubAckHubStatus (HubIf
->Device
);
1047 gBS
->FreePool (HubIf
->ChangeMap
);
1048 HubIf
->ChangeMap
= NULL
;
1054 Enumerate all the changed hub ports.
1056 @param Event The event that is triggered.
1057 @param Context The context to the event.
1062 UsbRootHubEnumeration (
1067 USB_INTERFACE
*RootHub
;
1071 RootHub
= (USB_INTERFACE
*) Context
;
1073 for (Index
= 0; Index
< RootHub
->NumOfPort
; Index
++) {
1074 Child
= UsbFindChild (RootHub
, Index
);
1075 if ((Child
!= NULL
) && (Child
->DisconnectFail
== TRUE
)) {
1076 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: The device disconnect fails at port %d from root hub %p, try again\n", Index
, RootHub
));
1077 UsbRemoveDevice (Child
);
1080 UsbEnumeratePort (RootHub
, Index
);