X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FBus%2FUsb%2FUsbBusDxe%2FUsbEnumer.c;h=b0e6b835ac24f03c1aedc8e73ff9bb731eb5279d;hb=b659b503fa7c43677ccab40d311dbab33366ee97;hp=f5ec52d988e52e5ec8cc278d1fcb8387bbf658fd;hpb=b4c24e2d42b6d91a6abdca2c3341469350e57483;p=mirror_edk2.git diff --git a/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbEnumer.c b/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbEnumer.c index f5ec52d988..b0e6b835ac 100644 --- a/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbEnumer.c +++ b/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbEnumer.c @@ -1,7 +1,9 @@ /** @file -Copyright (c) 2007 - 2008, Intel Corporation -All rights reserved. This program and the accompanying materials + Usb bus enumeration support. + +Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.
+This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php @@ -9,29 +11,17 @@ http://opensource.org/licenses/bsd-license.php THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. - Module Name: - - UsbEnumer.c - - Abstract: - - Usb bus enumeration support - - Revision History - - **/ #include "UsbBus.h" - /** - Return the endpoint descriptor in this interface + Return the endpoint descriptor in this interface. - @param UsbIf The interface to search in - @param EpAddr The address of the endpoint to return + @param UsbIf The interface to search in. + @param EpAddr The address of the endpoint to return. - @return The endpoint descriptor or NULL + @return The endpoint descriptor or NULL. **/ USB_ENDPOINT_DESC * @@ -41,9 +31,12 @@ UsbGetEndpointDesc ( ) { USB_ENDPOINT_DESC *EpDesc; - UINTN Index; - - for (Index = 0; Index < UsbIf->IfSetting->Desc.NumEndpoints; Index++) { + UINT8 Index; + UINT8 NumEndpoints; + + NumEndpoints = UsbIf->IfSetting->Desc.NumEndpoints; + + for (Index = 0; Index < NumEndpoints; Index++) { EpDesc = UsbIf->IfSetting->Endpoints[Index]; if (EpDesc->Desc.EndpointAddress == EpAddr) { @@ -56,35 +49,37 @@ UsbGetEndpointDesc ( /** - Free the resource used by USB interface - - @param UsbIf The USB interface to free + Free the resource used by USB interface. - @return None + @param UsbIf The USB interface to free. + @retval EFI_ACCESS_DENIED The interface is still occupied. + @retval EFI_SUCCESS The interface is freed. **/ -STATIC -VOID +EFI_STATUS UsbFreeInterface ( IN USB_INTERFACE *UsbIf ) { - UsbCloseHostProtoByChild (UsbIf->Device->Bus, UsbIf->Handle); + EFI_STATUS Status; - gBS->UninstallMultipleProtocolInterfaces ( - UsbIf->Handle, - &gEfiDevicePathProtocolGuid, - UsbIf->DevicePath, - &gEfiUsbIoProtocolGuid, - &UsbIf->UsbIo, - NULL - ); + UsbCloseHostProtoByChild (UsbIf->Device->Bus, UsbIf->Handle); - if (UsbIf->DevicePath != NULL) { - gBS->FreePool (UsbIf->DevicePath); + Status = gBS->UninstallMultipleProtocolInterfaces ( + UsbIf->Handle, + &gEfiDevicePathProtocolGuid, UsbIf->DevicePath, + &gEfiUsbIoProtocolGuid, &UsbIf->UsbIo, + NULL + ); + if (!EFI_ERROR (Status)) { + if (UsbIf->DevicePath != NULL) { + FreePool (UsbIf->DevicePath); + } + FreePool (UsbIf); + } else { + UsbOpenHostProtoByChild (UsbIf->Device->Bus, UsbIf->Handle); } - - gBS->FreePool (UsbIf); + return Status; } @@ -92,13 +87,12 @@ UsbFreeInterface ( Create an interface for the descriptor IfDesc. Each device's configuration can have several interfaces. - @param Device The device has the interface descriptor - @param IfDesc The interface descriptor + @param Device The device has the interface descriptor. + @param IfDesc The interface descriptor. @return The created USB interface for the descriptor, or NULL. **/ -STATIC USB_INTERFACE * UsbCreateInterface ( IN USB_DEVICE *Device, @@ -119,6 +113,7 @@ UsbCreateInterface ( UsbIf->Signature = USB_INTERFACE_SIGNATURE; UsbIf->Device = Device; UsbIf->IfDesc = IfDesc; + ASSERT (IfDesc->ActiveIndex < USB_MAX_INTERFACE_SETTING); UsbIf->IfSetting = IfDesc->Settings[IfDesc->ActiveIndex]; CopyMem ( @@ -185,24 +180,21 @@ UsbCreateInterface ( return UsbIf; ON_ERROR: - if (UsbIf->DevicePath) { - gBS->FreePool (UsbIf->DevicePath); + if (UsbIf->DevicePath != NULL) { + FreePool (UsbIf->DevicePath); } - gBS->FreePool (UsbIf); + FreePool (UsbIf); return NULL; } /** - Free the resource used by this USB device + Free the resource used by this USB device. - @param Device The USB device to free - - @return None + @param Device The USB device to free. **/ -STATIC VOID UsbFreeDevice ( IN USB_DEVICE *Device @@ -219,13 +211,12 @@ UsbFreeDevice ( /** Create a device which is on the parent's ParentPort port. - @param ParentIf The parent HUB interface - @param ParentPort The port on the HUB this device is connected to + @param ParentIf The parent HUB interface. + @param ParentPort The port on the HUB this device is connected to. - @return Created USB device + @return Created USB device, Or NULL. **/ -STATIC USB_DEVICE * UsbCreateDevice ( IN USB_INTERFACE *ParentIf, @@ -247,21 +238,21 @@ UsbCreateDevice ( Device->ParentAddr = ParentIf->Device->Address; Device->ParentIf = ParentIf; Device->ParentPort = ParentPort; + Device->Tier = (UINT8)(ParentIf->Device->Tier + 1); return Device; } /** Connect the USB interface with its driver. EFI USB bus will - create a USB interface for each seperate interface descriptor. + create a USB interface for each separate interface descriptor. - @param UsbIf The interface to connect driver to + @param UsbIf The interface to connect driver to. - @return EFI_SUCCESS : Interface is managed by some driver - @return Others : Failed to locate a driver for this interface + @return EFI_SUCCESS Interface is managed by some driver. + @return Others Failed to locate a driver for this interface. **/ -STATIC EFI_STATUS UsbConnectDriver ( IN USB_INTERFACE *UsbIf @@ -294,14 +285,14 @@ UsbConnectDriver ( // if (UsbBusIsWantedUsbIO (UsbIf->Device->Bus, UsbIf)) { OldTpl = UsbGetCurrentTpl (); - DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL before connect is %d\n", OldTpl)); + DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL before connect is %d, %p\n", (UINT32)OldTpl, UsbIf->Handle)); gBS->RestoreTPL (TPL_CALLBACK); Status = gBS->ConnectController (UsbIf->Handle, NULL, NULL, TRUE); UsbIf->IsManaged = (BOOLEAN)!EFI_ERROR (Status); - DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL after connect is %d\n", UsbGetCurrentTpl())); + DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL after connect is %d\n", (UINT32)UsbGetCurrentTpl())); ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK); gBS->RaiseTPL (OldTpl); @@ -318,10 +309,10 @@ UsbConnectDriver ( settings. Only one setting is active. It will also reset its endpoints' toggle to zero. - @param IfDesc The interface descriptor to set - @param Alternate The alternate setting number to locate + @param IfDesc The interface descriptor to set. + @param Alternate The alternate setting number to locate. - @retval EFI_NOT_FOUND There is no setting with this alternate index + @retval EFI_NOT_FOUND There is no setting with this alternate index. @retval EFI_SUCCESS The interface is set to Alternate setting. **/ @@ -332,7 +323,7 @@ UsbSelectSetting ( ) { USB_INTERFACE_SETTING *Setting; - UINT8 Index; + UINTN Index; // // Locate the active alternate setting @@ -340,6 +331,7 @@ UsbSelectSetting ( Setting = NULL; for (Index = 0; Index < IfDesc->NumOfSetting; Index++) { + ASSERT (Index < USB_MAX_INTERFACE_SETTING); Setting = IfDesc->Settings[Index]; if (Setting->Desc.AlternateSetting == Alternate) { @@ -353,6 +345,7 @@ UsbSelectSetting ( IfDesc->ActiveIndex = Index; + ASSERT (Setting != NULL); DEBUG ((EFI_D_INFO, "UsbSelectSetting: setting %d selected for interface %d\n", Alternate, Setting->Desc.InterfaceNumber)); @@ -371,11 +364,11 @@ UsbSelectSetting ( Select a new configuration for the device. Each device may support several configurations. - @param Device The device to select configuration - @param ConfigValue The index of the configuration ( != 0) + @param Device The device to select configuration. + @param ConfigValue The index of the configuration ( != 0). - @retval EFI_NOT_FOUND There is no configuration with the index - @retval EFI_OUT_OF_RESOURCES Failed to allocate resource + @retval EFI_NOT_FOUND There is no configuration with the index. + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource. @retval EFI_SUCCESS The configuration is selected. **/ @@ -432,9 +425,11 @@ UsbSelectConfig ( UsbIf = UsbCreateInterface (Device, ConfigDesc->Interfaces[Index]); if (UsbIf == NULL) { + Device->NumOfInterface = Index; return EFI_OUT_OF_RESOURCES; } + ASSERT (Index < USB_MAX_INTERFACE); Device->Interfaces[Index] = UsbIf; // @@ -455,29 +450,27 @@ UsbSelectConfig ( } - /** Disconnect the USB interface with its driver. - @param UsbIf The interface to disconnect driver from - - @return None + @param UsbIf The interface to disconnect driver from. **/ -STATIC -VOID +EFI_STATUS UsbDisconnectDriver ( IN USB_INTERFACE *UsbIf ) { EFI_TPL OldTpl; + EFI_STATUS Status; // // Release the hub if it's a hub controller, otherwise // disconnect the driver if it is managed by other drivers. // + Status = EFI_SUCCESS; if (UsbIf->IsHub) { - UsbIf->HubApi->Release (UsbIf); + Status = UsbIf->HubApi->Release (UsbIf); } else if (UsbIf->IsManaged) { // @@ -488,65 +481,79 @@ UsbDisconnectDriver ( // or disconnect at CALLBACK. // OldTpl = UsbGetCurrentTpl (); - DEBUG ((EFI_D_INFO, "UsbDisconnectDriver: old TPL is %d\n", OldTpl)); + DEBUG ((EFI_D_INFO, "UsbDisconnectDriver: old TPL is %d, %p\n", (UINT32)OldTpl, UsbIf->Handle)); gBS->RestoreTPL (TPL_CALLBACK); - gBS->DisconnectController (UsbIf->Handle, NULL, NULL); - UsbIf->IsManaged = FALSE; - - DEBUG (( EFI_D_INFO, "UsbDisconnectDriver: TPL after disconnect is %d\n", UsbGetCurrentTpl())); + Status = gBS->DisconnectController (UsbIf->Handle, NULL, NULL); + if (!EFI_ERROR (Status)) { + UsbIf->IsManaged = FALSE; + } + + DEBUG (( EFI_D_INFO, "UsbDisconnectDriver: TPL after disconnect is %d, %d\n", (UINT32)UsbGetCurrentTpl(), Status)); ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK); gBS->RaiseTPL (OldTpl); } + + return Status; } - /** - Remove the current device configuration + Remove the current device configuration. - @param Device The USB device to remove configuration from - - @return None + @param Device The USB device to remove configuration from. **/ -VOID +EFI_STATUS UsbRemoveConfig ( IN USB_DEVICE *Device ) { USB_INTERFACE *UsbIf; UINTN Index; + EFI_STATUS Status; + EFI_STATUS ReturnStatus; // // Remove each interface of the device // - for (Index = 0; Index < Device->NumOfInterface; Index++) { + ReturnStatus = EFI_SUCCESS; + for (Index = 0; Index < Device->NumOfInterface; Index++) { + ASSERT (Index < USB_MAX_INTERFACE); UsbIf = Device->Interfaces[Index]; if (UsbIf == NULL) { continue; } - UsbDisconnectDriver (UsbIf); - UsbFreeInterface (UsbIf); - Device->Interfaces[Index] = NULL; + Status = UsbDisconnectDriver (UsbIf); + if (!EFI_ERROR (Status)) { + Status = UsbFreeInterface (UsbIf); + if (EFI_ERROR (Status)) { + UsbConnectDriver (UsbIf); + } + } + + if (!EFI_ERROR (Status)) { + Device->Interfaces[Index] = NULL; + } else { + ReturnStatus = Status; + } } Device->ActiveConfig = NULL; - Device->NumOfInterface = 0; + return ReturnStatus; } - /** Remove the device and all its children from the bus. - @param Device The device to remove + @param Device The device to remove. - @retval EFI_SUCCESS The device is removed + @retval EFI_SUCCESS The device is removed. **/ EFI_STATUS @@ -557,7 +564,8 @@ UsbRemoveDevice ( USB_BUS *Bus; USB_DEVICE *Child; EFI_STATUS Status; - UINT8 Index; + EFI_STATUS ReturnStatus; + UINTN Index; Bus = Device->Bus; @@ -565,7 +573,8 @@ UsbRemoveDevice ( // Remove all the devices on its downstream ports. Search from devices[1]. // Devices[0] is the root hub. // - for (Index = 1; Index < USB_MAX_DEVICES; Index++) { + ReturnStatus = EFI_SUCCESS; + for (Index = 1; Index < Bus->MaxDevices; Index++) { Child = Bus->Devices[Index]; if ((Child == NULL) || (Child->ParentAddr != Device->Address)) { @@ -574,33 +583,43 @@ UsbRemoveDevice ( Status = UsbRemoveDevice (Child); - if (EFI_ERROR (Status)) { - DEBUG ((EFI_D_ERROR, "UsbRemoveDevice: failed to remove child, ignore error\n")); + if (!EFI_ERROR (Status)) { Bus->Devices[Index] = NULL; + } else { + Bus->Devices[Index]->DisconnectFail = TRUE; + ReturnStatus = Status; + DEBUG ((EFI_D_INFO, "UsbRemoveDevice: failed to remove child %p at parent %p\n", Child, Device)); } } - UsbRemoveConfig (Device); + if (EFI_ERROR (ReturnStatus)) { + return ReturnStatus; + } - DEBUG (( EFI_D_INFO, "UsbRemoveDevice: device %d removed\n", Device->Address)); + Status = UsbRemoveConfig (Device); - Bus->Devices[Device->Address] = NULL; - UsbFreeDevice (Device); + if (!EFI_ERROR (Status)) { + DEBUG (( EFI_D_INFO, "UsbRemoveDevice: device %d removed\n", Device->Address)); - return EFI_SUCCESS; + ASSERT (Device->Address < Bus->MaxDevices); + Bus->Devices[Device->Address] = NULL; + UsbFreeDevice (Device); + } else { + Bus->Devices[Device->Address]->DisconnectFail = TRUE; + } + return Status; } /** - Find the child device on the hub's port + Find the child device on the hub's port. - @param HubIf The hub interface - @param Port The port of the hub this child is connected to + @param HubIf The hub interface. + @param Port The port of the hub this child is connected to. - @return The device on the hub's port, or NULL if there is none + @return The device on the hub's port, or NULL if there is none. **/ -STATIC USB_DEVICE * UsbFindChild ( IN USB_INTERFACE *HubIf, @@ -616,7 +635,7 @@ UsbFindChild ( // // Start checking from device 1, device 0 is the root hub // - for (Index = 1; Index < USB_MAX_DEVICES; Index++) { + for (Index = 1; Index < Bus->MaxDevices; Index++) { Device = Bus->Devices[Index]; if ((Device != NULL) && (Device->ParentAddr == HubIf->Device->Address) && @@ -630,23 +649,23 @@ UsbFindChild ( } - /** Enumerate and configure the new device on the port of this HUB interface. - @param HubIf The HUB that has the device connected - @param Port The port index of the hub (started with zero) + @param HubIf The HUB that has the device connected. + @param Port The port index of the hub (started with zero). + @param ResetIsNeeded The boolean to control whether skip the reset of the port. - @retval EFI_SUCCESS The device is enumerated (added or removed) - @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device - @retval Others Failed to enumerate the device + @retval EFI_SUCCESS The device is enumerated (added or removed). + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device. + @retval Others Failed to enumerate the device. **/ -STATIC EFI_STATUS UsbEnumerateNewDev ( IN USB_INTERFACE *HubIf, - IN UINT8 Port + IN UINT8 Port, + IN BOOLEAN ResetIsNeeded ) { USB_BUS *Bus; @@ -654,15 +673,15 @@ UsbEnumerateNewDev ( USB_DEVICE *Child; USB_DEVICE *Parent; EFI_USB_PORT_STATUS PortState; - UINT8 Address; + UINTN Address; UINT8 Config; EFI_STATUS Status; - Address = USB_MAX_DEVICES; Parent = HubIf->Device; Bus = Parent->Bus; - HubApi = HubIf->HubApi; - + HubApi = HubIf->HubApi; + Address = Bus->MaxDevices; + gBS->Stall (USB_WAIT_PORT_STABLE_STALL); // @@ -671,16 +690,18 @@ UsbEnumerateNewDev ( // and the hub is a EHCI root hub, ResetPort will release // the device to its companion UHCI and return an error. // - Status = HubApi->ResetPort (HubIf, Port); - - if (EFI_ERROR (Status)) { - DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to reset port %d - %r\n", Port, Status)); - - return Status; + if (ResetIsNeeded) { + Status = HubApi->ResetPort (HubIf, Port); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to reset port %d - %r\n", Port, Status)); + + return Status; + } + DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: hub port %d is reset\n", Port)); + } else { + DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: hub port %d reset is skipped\n", Port)); } - DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: hub port %d is reset\n", Port)); - Child = UsbCreateDevice (HubIf, Port); if (Child == NULL) { @@ -698,38 +719,42 @@ UsbEnumerateNewDev ( goto ON_ERROR; } - if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_LOW_SPEED)) { - Child->Speed = EFI_USB_SPEED_LOW; - + if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_CONNECTION)) { + DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: No device present at port %d\n", Port)); + goto ON_ERROR; + } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_SUPER_SPEED)){ + Child->Speed = EFI_USB_SPEED_SUPER; + Child->MaxPacket0 = 512; } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_HIGH_SPEED)) { - Child->Speed = EFI_USB_SPEED_HIGH; - + Child->Speed = EFI_USB_SPEED_HIGH; + Child->MaxPacket0 = 64; + } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_LOW_SPEED)) { + Child->Speed = EFI_USB_SPEED_LOW; + Child->MaxPacket0 = 8; } else { - Child->Speed = EFI_USB_SPEED_FULL; + Child->Speed = EFI_USB_SPEED_FULL; + Child->MaxPacket0 = 8; } DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device is of %d speed\n", Child->Speed)); - if (Child->Speed != EFI_USB_SPEED_HIGH) { + if (((Child->Speed == EFI_USB_SPEED_LOW) || (Child->Speed == EFI_USB_SPEED_FULL)) && + (Parent->Speed == EFI_USB_SPEED_HIGH)) { // - // If the child isn't a high speed device, it is necessary to + // If the child is a low or full speed device, it is necessary to // set the transaction translator. Port TT is 1-based. // This is quite simple: // 1. if parent is of high speed, then parent is our translator // 2. otherwise use parent's translator. // - if (Parent->Speed == EFI_USB_SPEED_HIGH) { - Child->Translator.TranslatorHubAddress = Parent->Address; - Child->Translator.TranslatorPortNumber = Port + 1; - - } else { - Child->Translator = Parent->Translator; - } - - DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device uses translator (%d, %d)\n", - Child->Translator.TranslatorHubAddress, - Child->Translator.TranslatorPortNumber)); + Child->Translator.TranslatorHubAddress = Parent->Address; + Child->Translator.TranslatorPortNumber = (UINT8) (Port + 1); + } else { + Child->Translator = Parent->Translator; } + DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device uses translator (%d, %d)\n", + Child->Translator.TranslatorHubAddress, + Child->Translator.TranslatorPortNumber)); // // After port is reset, hub establishes a signal path between @@ -738,50 +763,51 @@ UsbEnumerateNewDev ( // a time) , and ready to respond to control transfer at EP 0. // - // - // Host sends a Get_Descriptor request to learn the max packet - // size of default pipe (only part of the device's descriptor). - // - Status = UsbGetMaxPacketSize0 (Child); - - if (EFI_ERROR (Status)) { - DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get max packet for EP 0 - %r\n", Status)); - goto ON_ERROR; - } - - DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: max packet size for EP 0 is %d\n", Child->MaxPacket0)); - // // Host assigns an address to the device. Device completes the // status stage with default address, then switches to new address. // ADDRESS state. Address zero is reserved for root hub. // - for (Address = 1; Address < USB_MAX_DEVICES; Address++) { + ASSERT (Bus->MaxDevices <= 256); + for (Address = 1; Address < Bus->MaxDevices; Address++) { if (Bus->Devices[Address] == NULL) { break; } } - if (Address == USB_MAX_DEVICES) { + if (Address >= Bus->MaxDevices) { DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: address pool is full for port %d\n", Port)); Status = EFI_ACCESS_DENIED; goto ON_ERROR; } + Status = UsbSetAddress (Child, (UINT8)Address); + Child->Address = (UINT8)Address; Bus->Devices[Address] = Child; - Status = UsbSetAddress (Child, Address); - Child->Address = Address; if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to set device address - %r\n", Status)); goto ON_ERROR; } - + gBS->Stall (USB_SET_DEVICE_ADDRESS_STALL); DEBUG ((EFI_D_INFO, "UsbEnumerateNewDev: device is now ADDRESSED at %d\n", Address)); + // + // Host sends a Get_Descriptor request to learn the max packet + // size of default pipe (only part of the device's descriptor). + // + Status = UsbGetMaxPacketSize0 (Child); + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get max packet for EP 0 - %r\n", Status)); + goto ON_ERROR; + } + + DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: max packet size for EP 0 is %d\n", Child->MaxPacket0)); + // // Host learns about the device's abilities by requesting device's // entire descriptions. @@ -817,34 +843,46 @@ UsbEnumerateNewDev ( goto ON_ERROR; } + // + // Report Status Code to indicate USB device has been detected by hotplug + // + REPORT_STATUS_CODE_WITH_DEVICE_PATH ( + EFI_PROGRESS_CODE, + (EFI_IO_BUS_USB | EFI_IOB_PC_HOTPLUG), + Bus->DevicePath + ); return EFI_SUCCESS; ON_ERROR: - if (Address != USB_MAX_DEVICES) { - Bus->Devices[Address] = NULL; - } - - if (Child != NULL) { - UsbFreeDevice (Child); - } - + // + // If reach here, it means the enumeration process on a given port is interrupted due to error. + // The s/w resources, including the assigned address(Address) and the allocated usb device data + // structure(Bus->Devices[Address]), will NOT be freed here. These resources will be freed when + // the device is unplugged from the port or DriverBindingStop() is invoked. + // + // This way is used to co-work with the lower layer EDKII UHCI/EHCI/XHCI host controller driver. + // It's mainly because to keep UEFI spec unchanged EDKII XHCI driver have to maintain a state machine + // to keep track of the mapping between actual address and request address. If the request address + // (Address) is freed here, the Address value will be used by next enumerated device. Then EDKII XHCI + // host controller driver will have wrong information, which will cause further transaction error. + // + // EDKII UHCI/EHCI doesn't get impacted as it's make sense to reserve s/w resource till it gets unplugged. + // return Status; } - /** Process the events on the port. - @param HubIf The HUB that has the device connected - @param Port The port index of the hub (started with zero) + @param HubIf The HUB that has the device connected. + @param Port The port index of the hub (started with zero). - @retval EFI_SUCCESS The device is enumerated (added or removed) - @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device - @retval Others Failed to enumerate the device + @retval EFI_SUCCESS The device is enumerated (added or removed). + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for the device. + @retval Others Failed to enumerate the device. **/ -STATIC EFI_STATUS UsbEnumeratePort ( IN USB_INTERFACE *HubIf, @@ -869,12 +907,16 @@ UsbEnumeratePort ( return Status; } - if (PortState.PortChangeStatus == 0) { + // + // Only handle connection/enable/overcurrent/reset change. + // Usb super speed hub may report other changes, such as warm reset change. Ignore them. + // + if ((PortState.PortChangeStatus & (USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE | USB_PORT_STAT_C_OVERCURRENT | USB_PORT_STAT_C_RESET)) == 0) { return EFI_SUCCESS; } - DEBUG (( EFI_D_INFO, "UsbEnumeratePort: port %d state - %x, change - %x\n", - Port, PortState.PortStatus, PortState.PortChangeStatus)); + DEBUG (( EFI_D_INFO, "UsbEnumeratePort: port %d state - %02x, change - %02x on %p\n", + Port, PortState.PortStatus, PortState.PortChangeStatus, HubIf)); // // This driver only process two kinds of events now: over current and @@ -919,7 +961,7 @@ UsbEnumeratePort ( // Case4: // Device connected or disconnected normally. // - DEBUG ((EFI_D_ERROR, "UsbEnumeratePort: Device Connect/Discount Normally\n", Port)); + DEBUG ((EFI_D_INFO, "UsbEnumeratePort: Device Connect/Disconnect Normally\n", Port)); } // @@ -928,7 +970,7 @@ UsbEnumeratePort ( Child = UsbFindChild (HubIf, Port); if (Child != NULL) { - DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device at port %d removed from system\n", Port)); + DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device at port %d removed from root hub %p\n", Port, HubIf)); UsbRemoveDevice (Child); } @@ -937,7 +979,11 @@ UsbEnumeratePort ( // Now, new device connected, enumerate and configure the device // DEBUG (( EFI_D_INFO, "UsbEnumeratePort: new device connected at port %d\n", Port)); - Status = UsbEnumerateNewDev (HubIf, Port); + if (USB_BIT_IS_SET (PortState.PortChangeStatus, USB_PORT_STAT_C_RESET)) { + Status = UsbEnumerateNewDev (HubIf, Port, FALSE); + } else { + Status = UsbEnumerateNewDev (HubIf, Port, TRUE); + } } else { DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device disconnected event on port %d\n", Port)); @@ -949,15 +995,14 @@ UsbEnumeratePort ( /** - Enumerate all the changed hub ports - - @param Event The event that is triggered - @param Context The context to the event + Enumerate all the changed hub ports. - @return None + @param Event The event that is triggered. + @param Context The context to the event. **/ VOID +EFIAPI UsbHubEnumeration ( IN EFI_EVENT Event, IN VOID *Context @@ -967,11 +1012,20 @@ UsbHubEnumeration ( UINT8 Byte; UINT8 Bit; UINT8 Index; - - ASSERT (Context); + USB_DEVICE *Child; + + ASSERT (Context != NULL); HubIf = (USB_INTERFACE *) Context; + for (Index = 0; Index < HubIf->NumOfPort; Index++) { + Child = UsbFindChild (HubIf, Index); + if ((Child != NULL) && (Child->DisconnectFail == TRUE)) { + DEBUG (( EFI_D_INFO, "UsbEnumeratePort: The device disconnect fails at port %d from hub %p, try again\n", Index, HubIf)); + UsbRemoveDevice (Child); + } + } + if (HubIf->ChangeMap == NULL) { return ; } @@ -999,12 +1053,10 @@ UsbHubEnumeration ( /** - Enumerate all the changed hub ports + Enumerate all the changed hub ports. - @param Event The event that is triggered - @param Context The context to the event - - @return None + @param Event The event that is triggered. + @param Context The context to the event. **/ VOID @@ -1016,10 +1068,17 @@ UsbRootHubEnumeration ( { USB_INTERFACE *RootHub; UINT8 Index; + USB_DEVICE *Child; RootHub = (USB_INTERFACE *) Context; for (Index = 0; Index < RootHub->NumOfPort; Index++) { + Child = UsbFindChild (RootHub, Index); + if ((Child != NULL) && (Child->DisconnectFail == TRUE)) { + DEBUG (( EFI_D_INFO, "UsbEnumeratePort: The device disconnect fails at port %d from root hub %p, try again\n", Index, RootHub)); + UsbRemoveDevice (Child); + } + UsbEnumeratePort (RootHub, Index); } }