From ed07a2bb11b358fdece44a760fc193d56f22cfb2 Mon Sep 17 00:00:00 2001 From: Britton Chesley Date: Tue, 16 May 2023 15:40:50 -0500 Subject: [PATCH] MdeModulePkg/UsbBusDxe: USB issue fix when the port reset BZ #4456 Fixed a bug which led to an ASSERT due to the USB device context being maintained after a port reset, but the underlying XHCI context was uninitialized. Specifically, Xhc->UsbDevContext is freed after a reset and only re-allocates the default [0] enpoint transfer ring. In order to avoid a memory leak, device enumeration is performed after freeing the necessary buffers. This allocates the Xhc->UsbDevContext for all endpoints of the USB device. Signed-off-by: Britton Chesley --- MdeModulePkg/Bus/Usb/UsbBusDxe/UsbBus.c | 27 ++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbBus.c b/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbBus.c index c25f3cc2f2..2826ac130e 100644 --- a/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbBus.c +++ b/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbBus.c @@ -3,6 +3,7 @@ Usb Bus Driver Binding and Bus IO Protocol. Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.
+Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved. SPDX-License-Identifier: BSD-2-Clause-Patent **/ @@ -821,6 +822,7 @@ UsbIoPortReset ( EFI_TPL OldTpl; EFI_STATUS Status; UINT8 DevAddress; + UINT8 Config; OldTpl = gBS->RaiseTPL (USB_BUS_TPL); @@ -882,8 +884,26 @@ UsbIoPortReset ( // is in CONFIGURED state. // if (Dev->ActiveConfig != NULL) { - Status = UsbSetConfig (Dev, Dev->ActiveConfig->Desc.ConfigurationValue); + UsbFreeDevDesc (Dev->DevDesc); + Status = UsbRemoveConfig (Dev); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "UsbIoPortReset: Failed to remove configuration - %r\n", Status)); + } + + Status = UsbGetMaxPacketSize0 (Dev); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "UsbIoPortReset: Failed to get max packet size - %r\n", Status)); + } + + Status = UsbBuildDescTable (Dev); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "UsbIoPortReset: Failed to build descriptor table - %r\n", Status)); + } + + Config = Dev->DevDesc->Configs[0]->Desc.ConfigurationValue; + + Status = UsbSetConfig (Dev, Config); if (EFI_ERROR (Status)) { DEBUG (( DEBUG_ERROR, @@ -892,6 +912,11 @@ UsbIoPortReset ( Status )); } + + Status = UsbSelectConfig (Dev, Config); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "UsbIoPortReset: Failed to set configuration - %r\n", Status)); + } } ON_EXIT: