3 Usb bus enumeration support.
5 Copyright (c) 2007 - 2014, 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.
59 IN USB_INTERFACE
*UsbIf
62 UsbCloseHostProtoByChild (UsbIf
->Device
->Bus
, UsbIf
->Handle
);
64 gBS
->UninstallMultipleProtocolInterfaces (
66 &gEfiDevicePathProtocolGuid
,
68 &gEfiUsbIoProtocolGuid
,
73 if (UsbIf
->DevicePath
!= NULL
) {
74 FreePool (UsbIf
->DevicePath
);
82 Create an interface for the descriptor IfDesc. Each
83 device's configuration can have several interfaces.
85 @param Device The device has the interface descriptor.
86 @param IfDesc The interface descriptor.
88 @return The created USB interface for the descriptor, or NULL.
93 IN USB_DEVICE
*Device
,
94 IN USB_INTERFACE_DESC
*IfDesc
97 USB_DEVICE_PATH UsbNode
;
102 UsbIf
= AllocateZeroPool (sizeof (USB_INTERFACE
));
108 UsbIf
->Signature
= USB_INTERFACE_SIGNATURE
;
109 UsbIf
->Device
= Device
;
110 UsbIf
->IfDesc
= IfDesc
;
111 ASSERT (IfDesc
->ActiveIndex
< USB_MAX_INTERFACE_SETTING
);
112 UsbIf
->IfSetting
= IfDesc
->Settings
[IfDesc
->ActiveIndex
];
117 sizeof (EFI_USB_IO_PROTOCOL
)
121 // Install protocols for USBIO and device path
123 UsbNode
.Header
.Type
= MESSAGING_DEVICE_PATH
;
124 UsbNode
.Header
.SubType
= MSG_USB_DP
;
125 UsbNode
.ParentPortNumber
= Device
->ParentPort
;
126 UsbNode
.InterfaceNumber
= UsbIf
->IfSetting
->Desc
.InterfaceNumber
;
128 SetDevicePathNodeLength (&UsbNode
.Header
, sizeof (UsbNode
));
130 HubIf
= Device
->ParentIf
;
131 ASSERT (HubIf
!= NULL
);
133 UsbIf
->DevicePath
= AppendDevicePathNode (HubIf
->DevicePath
, &UsbNode
.Header
);
135 if (UsbIf
->DevicePath
== NULL
) {
136 DEBUG ((EFI_D_ERROR
, "UsbCreateInterface: failed to create device path\n"));
138 Status
= EFI_OUT_OF_RESOURCES
;
142 Status
= gBS
->InstallMultipleProtocolInterfaces (
144 &gEfiDevicePathProtocolGuid
,
146 &gEfiUsbIoProtocolGuid
,
151 if (EFI_ERROR (Status
)) {
152 DEBUG ((EFI_D_ERROR
, "UsbCreateInterface: failed to install UsbIo - %r\n", Status
));
157 // Open USB Host Controller Protocol by Child
159 Status
= UsbOpenHostProtoByChild (Device
->Bus
, UsbIf
->Handle
);
161 if (EFI_ERROR (Status
)) {
162 gBS
->UninstallMultipleProtocolInterfaces (
164 &gEfiDevicePathProtocolGuid
,
166 &gEfiUsbIoProtocolGuid
,
171 DEBUG ((EFI_D_ERROR
, "UsbCreateInterface: failed to open host for child - %r\n", Status
));
178 if (UsbIf
->DevicePath
!= NULL
) {
179 FreePool (UsbIf
->DevicePath
);
188 Free the resource used by this USB device.
190 @param Device The USB device to free.
195 IN USB_DEVICE
*Device
198 if (Device
->DevDesc
!= NULL
) {
199 UsbFreeDevDesc (Device
->DevDesc
);
202 gBS
->FreePool (Device
);
207 Create a device which is on the parent's ParentPort port.
209 @param ParentIf The parent HUB interface.
210 @param ParentPort The port on the HUB this device is connected to.
212 @return Created USB device, Or NULL.
217 IN USB_INTERFACE
*ParentIf
,
223 ASSERT (ParentIf
!= NULL
);
225 Device
= AllocateZeroPool (sizeof (USB_DEVICE
));
227 if (Device
== NULL
) {
231 Device
->Bus
= ParentIf
->Device
->Bus
;
232 Device
->MaxPacket0
= 8;
233 Device
->ParentAddr
= ParentIf
->Device
->Address
;
234 Device
->ParentIf
= ParentIf
;
235 Device
->ParentPort
= ParentPort
;
236 Device
->Tier
= (UINT8
)(ParentIf
->Device
->Tier
+ 1);
242 Connect the USB interface with its driver. EFI USB bus will
243 create a USB interface for each separate interface descriptor.
245 @param UsbIf The interface to connect driver to.
247 @return EFI_SUCCESS Interface is managed by some driver.
248 @return Others Failed to locate a driver for this interface.
253 IN USB_INTERFACE
*UsbIf
259 Status
= EFI_SUCCESS
;
262 // Hub is maintained by the USB bus driver. Otherwise try to
263 // connect drivers with this interface
265 if (UsbIsHubInterface (UsbIf
)) {
266 DEBUG ((EFI_D_INFO
, "UsbConnectDriver: found a hub device\n"));
267 Status
= mUsbHubApi
.Init (UsbIf
);
271 // This function is called in both UsbIoControlTransfer and
272 // the timer callback in hub enumeration. So, at least it is
273 // called at TPL_CALLBACK. Some driver sitting on USB has
274 // twisted TPL used. It should be no problem for us to connect
275 // or disconnect at CALLBACK.
279 // Only recursively wanted usb child device
281 if (UsbBusIsWantedUsbIO (UsbIf
->Device
->Bus
, UsbIf
)) {
282 OldTpl
= UsbGetCurrentTpl ();
283 DEBUG ((EFI_D_INFO
, "UsbConnectDriver: TPL before connect is %d, %p\n", (UINT32
)OldTpl
, UsbIf
->Handle
));
285 gBS
->RestoreTPL (TPL_CALLBACK
);
287 Status
= gBS
->ConnectController (UsbIf
->Handle
, NULL
, NULL
, TRUE
);
288 UsbIf
->IsManaged
= (BOOLEAN
)!EFI_ERROR (Status
);
290 DEBUG ((EFI_D_INFO
, "UsbConnectDriver: TPL after connect is %d\n", (UINT32
)UsbGetCurrentTpl()));
291 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK
);
293 gBS
->RaiseTPL (OldTpl
);
302 Select an alternate setting for the interface.
303 Each interface can have several mutually exclusive
304 settings. Only one setting is active. It will
305 also reset its endpoints' toggle to zero.
307 @param IfDesc The interface descriptor to set.
308 @param Alternate The alternate setting number to locate.
310 @retval EFI_NOT_FOUND There is no setting with this alternate index.
311 @retval EFI_SUCCESS The interface is set to Alternate setting.
316 IN USB_INTERFACE_DESC
*IfDesc
,
320 USB_INTERFACE_SETTING
*Setting
;
324 // Locate the active alternate setting
328 for (Index
= 0; Index
< IfDesc
->NumOfSetting
; Index
++) {
329 ASSERT (Index
< USB_MAX_INTERFACE_SETTING
);
330 Setting
= IfDesc
->Settings
[Index
];
332 if (Setting
->Desc
.AlternateSetting
== Alternate
) {
337 if (Index
== IfDesc
->NumOfSetting
) {
338 return EFI_NOT_FOUND
;
341 IfDesc
->ActiveIndex
= Index
;
343 ASSERT (Setting
!= NULL
);
344 DEBUG ((EFI_D_INFO
, "UsbSelectSetting: setting %d selected for interface %d\n",
345 Alternate
, Setting
->Desc
.InterfaceNumber
));
348 // Reset the endpoint toggle to zero
350 for (Index
= 0; Index
< Setting
->Desc
.NumEndpoints
; Index
++) {
351 Setting
->Endpoints
[Index
]->Toggle
= 0;
359 Select a new configuration for the device. Each
360 device may support several configurations.
362 @param Device The device to select configuration.
363 @param ConfigValue The index of the configuration ( != 0).
365 @retval EFI_NOT_FOUND There is no configuration with the index.
366 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource.
367 @retval EFI_SUCCESS The configuration is selected.
372 IN USB_DEVICE
*Device
,
376 USB_DEVICE_DESC
*DevDesc
;
377 USB_CONFIG_DESC
*ConfigDesc
;
378 USB_INTERFACE_DESC
*IfDesc
;
379 USB_INTERFACE
*UsbIf
;
384 // Locate the active config, then set the device's pointer
386 DevDesc
= Device
->DevDesc
;
389 for (Index
= 0; Index
< DevDesc
->Desc
.NumConfigurations
; Index
++) {
390 ConfigDesc
= DevDesc
->Configs
[Index
];
392 if (ConfigDesc
->Desc
.ConfigurationValue
== ConfigValue
) {
397 if (Index
== DevDesc
->Desc
.NumConfigurations
) {
398 return EFI_NOT_FOUND
;
401 Device
->ActiveConfig
= ConfigDesc
;
403 DEBUG ((EFI_D_INFO
, "UsbSelectConfig: config %d selected for device %d\n",
404 ConfigValue
, Device
->Address
));
407 // Create interfaces for each USB interface descriptor.
409 for (Index
= 0; Index
< ConfigDesc
->Desc
.NumInterfaces
; Index
++) {
411 // First select the default interface setting, and reset
412 // the endpoint toggles to zero for its endpoints.
414 IfDesc
= ConfigDesc
->Interfaces
[Index
];
415 UsbSelectSetting (IfDesc
, IfDesc
->Settings
[0]->Desc
.AlternateSetting
);
418 // Create a USB_INTERFACE and install USB_IO and other protocols
420 UsbIf
= UsbCreateInterface (Device
, ConfigDesc
->Interfaces
[Index
]);
423 Device
->NumOfInterface
= Index
;
424 return EFI_OUT_OF_RESOURCES
;
427 ASSERT (Index
< USB_MAX_INTERFACE
);
428 Device
->Interfaces
[Index
] = UsbIf
;
431 // Connect the device to drivers, if it failed, ignore
432 // the error. Don't let the unsupported interfaces to block
433 // the supported interfaces.
435 Status
= UsbConnectDriver (UsbIf
);
437 if (EFI_ERROR (Status
)) {
438 DEBUG ((EFI_D_ERROR
, "UsbSelectConfig: failed to connect driver %r, ignored\n", Status
));
442 Device
->NumOfInterface
= Index
;
449 Disconnect the USB interface with its driver.
451 @param UsbIf The interface to disconnect driver from.
455 UsbDisconnectDriver (
456 IN USB_INTERFACE
*UsbIf
463 // Release the hub if it's a hub controller, otherwise
464 // disconnect the driver if it is managed by other drivers.
466 Status
= EFI_SUCCESS
;
468 Status
= UsbIf
->HubApi
->Release (UsbIf
);
470 } else if (UsbIf
->IsManaged
) {
472 // This function is called in both UsbIoControlTransfer and
473 // the timer callback in hub enumeration. So, at least it is
474 // called at TPL_CALLBACK. Some driver sitting on USB has
475 // twisted TPL used. It should be no problem for us to connect
476 // or disconnect at CALLBACK.
478 OldTpl
= UsbGetCurrentTpl ();
479 DEBUG ((EFI_D_INFO
, "UsbDisconnectDriver: old TPL is %d, %p\n", (UINT32
)OldTpl
, UsbIf
->Handle
));
481 gBS
->RestoreTPL (TPL_CALLBACK
);
483 Status
= gBS
->DisconnectController (UsbIf
->Handle
, NULL
, NULL
);
484 if (!EFI_ERROR (Status
)) {
485 UsbIf
->IsManaged
= FALSE
;
488 DEBUG (( EFI_D_INFO
, "UsbDisconnectDriver: TPL after disconnect is %d, %d\n", (UINT32
)UsbGetCurrentTpl(), Status
));
489 ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK
);
491 gBS
->RaiseTPL (OldTpl
);
499 Remove the current device configuration.
501 @param Device The USB device to remove configuration from.
506 IN USB_DEVICE
*Device
509 USB_INTERFACE
*UsbIf
;
512 EFI_STATUS ReturnStatus
;
515 // Remove each interface of the device
517 ReturnStatus
= EFI_SUCCESS
;
518 for (Index
= 0; Index
< Device
->NumOfInterface
; Index
++) {
519 ASSERT (Index
< USB_MAX_INTERFACE
);
520 UsbIf
= Device
->Interfaces
[Index
];
526 Status
= UsbDisconnectDriver (UsbIf
);
527 if (!EFI_ERROR (Status
)) {
528 UsbFreeInterface (UsbIf
);
529 Device
->Interfaces
[Index
] = NULL
;
531 ReturnStatus
= Status
;
535 Device
->ActiveConfig
= NULL
;
541 Remove the device and all its children from the bus.
543 @param Device The device to remove.
545 @retval EFI_SUCCESS The device is removed.
550 IN USB_DEVICE
*Device
556 EFI_STATUS ReturnStatus
;
562 // Remove all the devices on its downstream ports. Search from devices[1].
563 // Devices[0] is the root hub.
565 ReturnStatus
= EFI_SUCCESS
;
566 for (Index
= 1; Index
< Bus
->MaxDevices
; Index
++) {
567 Child
= Bus
->Devices
[Index
];
569 if ((Child
== NULL
) || (Child
->ParentAddr
!= Device
->Address
)) {
573 Status
= UsbRemoveDevice (Child
);
575 if (!EFI_ERROR (Status
)) {
576 Bus
->Devices
[Index
] = NULL
;
578 Bus
->Devices
[Index
]->DisconnectFail
= TRUE
;
579 ReturnStatus
= Status
;
580 DEBUG ((EFI_D_INFO
, "UsbRemoveDevice: failed to remove child %p at parent %p\n", Child
, Device
));
584 if (EFI_ERROR (ReturnStatus
)) {
588 Status
= UsbRemoveConfig (Device
);
590 if (!EFI_ERROR (Status
)) {
591 DEBUG (( EFI_D_INFO
, "UsbRemoveDevice: device %d removed\n", Device
->Address
));
593 ASSERT (Device
->Address
< Bus
->MaxDevices
);
594 Bus
->Devices
[Device
->Address
] = NULL
;
595 UsbFreeDevice (Device
);
597 Bus
->Devices
[Device
->Address
]->DisconnectFail
= TRUE
;
604 Find the child device on the hub's port.
606 @param HubIf The hub interface.
607 @param Port The port of the hub this child is connected to.
609 @return The device on the hub's port, or NULL if there is none.
614 IN USB_INTERFACE
*HubIf
,
622 Bus
= HubIf
->Device
->Bus
;
625 // Start checking from device 1, device 0 is the root hub
627 for (Index
= 1; Index
< Bus
->MaxDevices
; Index
++) {
628 Device
= Bus
->Devices
[Index
];
630 if ((Device
!= NULL
) && (Device
->ParentAddr
== HubIf
->Device
->Address
) &&
631 (Device
->ParentPort
== Port
)) {
642 Enumerate and configure the new device on the port of this HUB interface.
644 @param HubIf The HUB that has the device connected.
645 @param Port The port index of the hub (started with zero).
647 @retval EFI_SUCCESS The device is enumerated (added or removed).
648 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device.
649 @retval Others Failed to enumerate the device.
654 IN USB_INTERFACE
*HubIf
,
662 EFI_USB_PORT_STATUS PortState
;
667 Parent
= HubIf
->Device
;
669 HubApi
= HubIf
->HubApi
;
670 Address
= Bus
->MaxDevices
;
672 gBS
->Stall (USB_WAIT_PORT_STABLE_STALL
);
675 // Hub resets the device for at least 10 milliseconds.
676 // Host learns device speed. If device is of low/full speed
677 // and the hub is a EHCI root hub, ResetPort will release
678 // the device to its companion UHCI and return an error.
680 Status
= HubApi
->ResetPort (HubIf
, Port
);
682 if (EFI_ERROR (Status
)) {
683 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to reset port %d - %r\n", Port
, Status
));
688 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: hub port %d is reset\n", Port
));
690 Child
= UsbCreateDevice (HubIf
, Port
);
693 return EFI_OUT_OF_RESOURCES
;
697 // OK, now identify the device speed. After reset, hub
698 // fully knows the actual device speed.
700 Status
= HubApi
->GetPortStatus (HubIf
, Port
, &PortState
);
702 if (EFI_ERROR (Status
)) {
703 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to get speed of port %d\n", Port
));
707 if (!USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_CONNECTION
)) {
708 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: No device present at port %d\n", Port
));
710 } else if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_SUPER_SPEED
)){
711 Child
->Speed
= EFI_USB_SPEED_SUPER
;
712 Child
->MaxPacket0
= 512;
713 } else if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_HIGH_SPEED
)) {
714 Child
->Speed
= EFI_USB_SPEED_HIGH
;
715 Child
->MaxPacket0
= 64;
716 } else if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_LOW_SPEED
)) {
717 Child
->Speed
= EFI_USB_SPEED_LOW
;
718 Child
->MaxPacket0
= 8;
720 Child
->Speed
= EFI_USB_SPEED_FULL
;
721 Child
->MaxPacket0
= 8;
724 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: device is of %d speed\n", Child
->Speed
));
726 if (((Child
->Speed
== EFI_USB_SPEED_LOW
) || (Child
->Speed
== EFI_USB_SPEED_FULL
)) &&
727 (Parent
->Speed
== EFI_USB_SPEED_HIGH
)) {
729 // If the child is a low or full speed device, it is necessary to
730 // set the transaction translator. Port TT is 1-based.
731 // This is quite simple:
732 // 1. if parent is of high speed, then parent is our translator
733 // 2. otherwise use parent's translator.
735 Child
->Translator
.TranslatorHubAddress
= Parent
->Address
;
736 Child
->Translator
.TranslatorPortNumber
= (UINT8
) (Port
+ 1);
738 Child
->Translator
= Parent
->Translator
;
740 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: device uses translator (%d, %d)\n",
741 Child
->Translator
.TranslatorHubAddress
,
742 Child
->Translator
.TranslatorPortNumber
));
745 // After port is reset, hub establishes a signal path between
746 // the device and host (DEFALUT state). Device's registers are
747 // reset, use default address 0 (host enumerates one device at
748 // a time) , and ready to respond to control transfer at EP 0.
752 // Host assigns an address to the device. Device completes the
753 // status stage with default address, then switches to new address.
754 // ADDRESS state. Address zero is reserved for root hub.
756 ASSERT (Bus
->MaxDevices
<= 256);
757 for (Address
= 1; Address
< Bus
->MaxDevices
; Address
++) {
758 if (Bus
->Devices
[Address
] == NULL
) {
763 if (Address
>= Bus
->MaxDevices
) {
764 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: address pool is full for port %d\n", Port
));
766 Status
= EFI_ACCESS_DENIED
;
770 Status
= UsbSetAddress (Child
, (UINT8
)Address
);
771 Child
->Address
= (UINT8
)Address
;
772 Bus
->Devices
[Address
] = Child
;
774 if (EFI_ERROR (Status
)) {
775 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to set device address - %r\n", Status
));
779 gBS
->Stall (USB_SET_DEVICE_ADDRESS_STALL
);
781 DEBUG ((EFI_D_INFO
, "UsbEnumerateNewDev: device is now ADDRESSED at %d\n", Address
));
784 // Host sends a Get_Descriptor request to learn the max packet
785 // size of default pipe (only part of the device's descriptor).
787 Status
= UsbGetMaxPacketSize0 (Child
);
789 if (EFI_ERROR (Status
)) {
790 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to get max packet for EP 0 - %r\n", Status
));
794 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: max packet size for EP 0 is %d\n", Child
->MaxPacket0
));
797 // Host learns about the device's abilities by requesting device's
798 // entire descriptions.
800 Status
= UsbBuildDescTable (Child
);
802 if (EFI_ERROR (Status
)) {
803 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to build descriptor table - %r\n", Status
));
808 // Select a default configuration: UEFI must set the configuration
809 // before the driver can connect to the device.
811 Config
= Child
->DevDesc
->Configs
[0]->Desc
.ConfigurationValue
;
812 Status
= UsbSetConfig (Child
, Config
);
814 if (EFI_ERROR (Status
)) {
815 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to set configure %d - %r\n", Config
, Status
));
819 DEBUG (( EFI_D_INFO
, "UsbEnumerateNewDev: device %d is now in CONFIGED state\n", Address
));
822 // Host assigns and loads a device driver.
824 Status
= UsbSelectConfig (Child
, Config
);
826 if (EFI_ERROR (Status
)) {
827 DEBUG ((EFI_D_ERROR
, "UsbEnumerateNewDev: failed to create interfaces - %r\n", Status
));
832 // Report Status Code to indicate USB device has been detected by hotplug
834 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
836 (EFI_IO_BUS_USB
| EFI_IOB_PC_HOTPLUG
),
843 // If reach here, it means the enumeration process on a given port is interrupted due to error.
844 // The s/w resources, including the assigned address(Address) and the allocated usb device data
845 // structure(Bus->Devices[Address]), will NOT be freed here. These resources will be freed when
846 // the device is unplugged from the port or DriverBindingStop() is invoked.
848 // This way is used to co-work with the lower layer EDKII UHCI/EHCI/XHCI host controller driver.
849 // It's mainly because to keep UEFI spec unchanged EDKII XHCI driver have to maintain a state machine
850 // to keep track of the mapping between actual address and request address. If the request address
851 // (Address) is freed here, the Address value will be used by next enumerated device. Then EDKII XHCI
852 // host controller driver will have wrong information, which will cause further transaction error.
854 // EDKII UHCI/EHCI doesn't get impacted as it's make sense to reserve s/w resource till it gets unplugged.
861 Process the events on the port.
863 @param HubIf The HUB that has the device connected.
864 @param Port The port index of the hub (started with zero).
866 @retval EFI_SUCCESS The device is enumerated (added or removed).
867 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device.
868 @retval Others Failed to enumerate the device.
873 IN USB_INTERFACE
*HubIf
,
879 EFI_USB_PORT_STATUS PortState
;
883 HubApi
= HubIf
->HubApi
;
886 // Host learns of the new device by polling the hub for port changes.
888 Status
= HubApi
->GetPortStatus (HubIf
, Port
, &PortState
);
890 if (EFI_ERROR (Status
)) {
891 DEBUG ((EFI_D_ERROR
, "UsbEnumeratePort: failed to get state of port %d\n", Port
));
896 // Only handle connection/enable/overcurrent/reset change.
897 // Usb super speed hub may report other changes, such as warm reset change. Ignore them.
899 if ((PortState
.PortChangeStatus
& (USB_PORT_STAT_C_CONNECTION
| USB_PORT_STAT_C_ENABLE
| USB_PORT_STAT_C_OVERCURRENT
| USB_PORT_STAT_C_RESET
)) == 0) {
903 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: port %d state - %02x, change - %02x on %p\n",
904 Port
, PortState
.PortStatus
, PortState
.PortChangeStatus
, HubIf
));
907 // This driver only process two kinds of events now: over current and
908 // connect/disconnect. Other three events are: ENABLE, SUSPEND, RESET.
909 // ENABLE/RESET is used to reset port. SUSPEND isn't supported.
912 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, USB_PORT_STAT_C_OVERCURRENT
)) {
914 if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_OVERCURRENT
)) {
917 // Both OverCurrent and OverCurrentChange set, means over current occurs,
918 // which probably is caused by short circuit. It has to wait system hardware
919 // to perform recovery.
921 DEBUG (( EFI_D_ERROR
, "UsbEnumeratePort: Critical Over Current\n", Port
));
922 return EFI_DEVICE_ERROR
;
927 // Only OverCurrentChange set, means system has been recoveried from
928 // over current. As a result, all ports are nearly power-off, so
929 // it's necessary to detach and enumerate all ports again.
931 DEBUG (( EFI_D_ERROR
, "UsbEnumeratePort: 2.0 device Recovery Over Current\n", Port
));
934 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, USB_PORT_STAT_C_ENABLE
)) {
937 // 1.1 roothub port reg doesn't reflect over-current state, while its counterpart
938 // on 2.0 roothub does. When over-current has influence on 1.1 device, the port
939 // would be disabled, so it's also necessary to detach and enumerate again.
941 DEBUG (( EFI_D_ERROR
, "UsbEnumeratePort: 1.1 device Recovery Over Current\n", Port
));
944 if (USB_BIT_IS_SET (PortState
.PortChangeStatus
, USB_PORT_STAT_C_CONNECTION
)) {
947 // Device connected or disconnected normally.
949 DEBUG ((EFI_D_INFO
, "UsbEnumeratePort: Device Connect/Disconnect Normally\n", Port
));
953 // Following as the above cases, it's safety to remove and create again.
955 Child
= UsbFindChild (HubIf
, Port
);
958 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: device at port %d removed from root hub %p\n", Port
, HubIf
));
959 UsbRemoveDevice (Child
);
962 if (USB_BIT_IS_SET (PortState
.PortStatus
, USB_PORT_STAT_CONNECTION
)) {
964 // Now, new device connected, enumerate and configure the device
966 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: new device connected at port %d\n", Port
));
967 Status
= UsbEnumerateNewDev (HubIf
, Port
);
970 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: device disconnected event on port %d\n", Port
));
973 HubApi
->ClearPortChange (HubIf
, Port
);
979 Enumerate all the changed hub ports.
981 @param Event The event that is triggered.
982 @param Context The context to the event.
992 USB_INTERFACE
*HubIf
;
998 ASSERT (Context
!= NULL
);
1000 HubIf
= (USB_INTERFACE
*) Context
;
1002 for (Index
= 0; Index
< HubIf
->NumOfPort
; Index
++) {
1003 Child
= UsbFindChild (HubIf
, Index
);
1004 if ((Child
!= NULL
) && (Child
->DisconnectFail
== TRUE
)) {
1005 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: The device disconnect fails at port %d from hub %p, try again\n", Index
, HubIf
));
1006 UsbRemoveDevice (Child
);
1010 if (HubIf
->ChangeMap
== NULL
) {
1015 // HUB starts its port index with 1.
1020 for (Index
= 0; Index
< HubIf
->NumOfPort
; Index
++) {
1021 if (USB_BIT_IS_SET (HubIf
->ChangeMap
[Byte
], USB_BIT (Bit
))) {
1022 UsbEnumeratePort (HubIf
, Index
);
1025 USB_NEXT_BIT (Byte
, Bit
);
1028 UsbHubAckHubStatus (HubIf
->Device
);
1030 gBS
->FreePool (HubIf
->ChangeMap
);
1031 HubIf
->ChangeMap
= NULL
;
1037 Enumerate all the changed hub ports.
1039 @param Event The event that is triggered.
1040 @param Context The context to the event.
1045 UsbRootHubEnumeration (
1050 USB_INTERFACE
*RootHub
;
1054 RootHub
= (USB_INTERFACE
*) Context
;
1056 for (Index
= 0; Index
< RootHub
->NumOfPort
; Index
++) {
1057 Child
= UsbFindChild (RootHub
, Index
);
1058 if ((Child
!= NULL
) && (Child
->DisconnectFail
== TRUE
)) {
1059 DEBUG (( EFI_D_INFO
, "UsbEnumeratePort: The device disconnect fails at port %d from root hub %p, try again\n", Index
, RootHub
));
1060 UsbRemoveDevice (Child
);
1063 UsbEnumeratePort (RootHub
, Index
);