X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FBus%2FUsb%2FUsbBusDxe%2FUsbBus.c;h=7c58fe8d36a26a67dea4d3f9712f17fd88b27b7b;hp=b2f5fdbc7554166fa38bffe3a6da3576d0423b92;hb=b4e96b82b4e2e47e95014b51787ba5b43abac784;hpb=af4a63857cd189964cc2c6b53b54483efaba78a3 diff --git a/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbBus.c b/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbBus.c index b2f5fdbc75..7c58fe8d36 100644 --- a/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbBus.c +++ b/MdeModulePkg/Bus/Usb/UsbBusDxe/UsbBus.c @@ -2,8 +2,8 @@ Usb Bus Driver Binding and Bus IO Protocol. -Copyright (c) 2004 - 2009, Intel Corporation -All rights reserved. This program and the accompanying materials +Copyright (c) 2004 - 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 @@ -15,11 +15,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "UsbBus.h" -// -// USB_BUS_PROTOCOL is only used to locate USB_BUS -// -EFI_GUID mUsbBusProtocolGuid = EFI_USB_BUS_PROTOCOL_GUID; - EFI_USB_IO_PROTOCOL mUsbIoProtocol = { UsbIoControlTransfer, UsbIoBulkTransfer, @@ -45,7 +40,6 @@ EFI_DRIVER_BINDING_PROTOCOL mUsbBusDriverBinding = { NULL }; - /** USB_IO function to execute a control transfer. This function will execute the USB transfer. If transfer @@ -82,6 +76,7 @@ UsbIoControlTransfer ( USB_ENDPOINT_DESC *EpDesc; EFI_TPL OldTpl; EFI_STATUS Status; + UINTN RequestedDataLength; if (UsbStatus == NULL) { return EFI_INVALID_PARAMETER; @@ -92,6 +87,7 @@ UsbIoControlTransfer ( UsbIf = USB_INTERFACE_FROM_USBIO (This); Dev = UsbIf->Device; + RequestedDataLength = DataLength; Status = UsbHcControlTransfer ( Dev->Bus, Dev->Address, @@ -105,13 +101,25 @@ UsbIoControlTransfer ( &Dev->Translator, UsbStatus ); + // + // If the request completed sucessfully and the Direction of the request is + // EfiUsbDataIn or EfiUsbDataOut, then make sure the actual number of bytes + // transfered is the same as the number of bytes requested. If a different + // number of bytes were transfered, then return EFI_DEVICE_ERROR. + // + if (!EFI_ERROR (Status)) { + if (Direction != EfiUsbNoData && DataLength != RequestedDataLength) { + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } + } if (EFI_ERROR (Status) || (*UsbStatus != EFI_USB_NOERROR)) { // // Clear TT buffer when CTRL/BULK split transaction failes // Clear the TRANSLATOR TT buffer, not parent's buffer // - ASSERT (Dev->Translator.TranslatorHubAddress < USB_MAX_DEVICES); + ASSERT (Dev->Translator.TranslatorHubAddress < Dev->Bus->MaxDevices); if (Dev->Translator.TranslatorHubAddress != 0) { UsbHubCtrlClearTTBuffer ( Dev->Bus->Devices[Dev->Translator.TranslatorHubAddress], @@ -284,7 +292,7 @@ UsbIoBulkTransfer ( // Clear TT buffer when CTRL/BULK split transaction failes. // Clear the TRANSLATOR TT buffer, not parent's buffer // - ASSERT (Dev->Translator.TranslatorHubAddress < USB_MAX_DEVICES); + ASSERT (Dev->Translator.TranslatorHubAddress < Dev->Bus->MaxDevices); if (Dev->Translator.TranslatorHubAddress != 0) { UsbHubCtrlClearTTBuffer ( Dev->Bus->Devices[Dev->Translator.TranslatorHubAddress], @@ -680,7 +688,7 @@ UsbIoGetEndpointDescriptor ( @param This The USB IO instance. @param LangIDTable The table to return the language IDs. - @param TableSize The number of supported languanges. + @param TableSize The size, in bytes, of the table LangIDTable. @retval EFI_SUCCESS The language ID is return. @@ -703,7 +711,7 @@ UsbIoGetSupportedLanguages ( Dev = UsbIf->Device; *LangIDTable = Dev->LangId; - *TableSize = Dev->TotalLangId; + *TableSize = (UINT16) (Dev->TotalLangId * sizeof (UINT16)); gBS->RestoreTPL (OldTpl); return EFI_SUCCESS; @@ -817,9 +825,9 @@ UsbIoPortReset ( USB_INTERFACE *UsbIf; USB_INTERFACE *HubIf; USB_DEVICE *Dev; - UINT8 Address; EFI_TPL OldTpl; EFI_STATUS Status; + UINT8 DevAddress; OldTpl = gBS->RaiseTPL (USB_BUS_TPL); @@ -841,27 +849,32 @@ UsbIoPortReset ( goto ON_EXIT; } + HubIf->HubApi->ClearPortChange (HubIf, Dev->ParentPort); + // - // Reset the device to its current address. The device now has a - // address of ZERO, so need to set Dev->Address to zero first for - // host to communicate with the device + // Reset the device to its current address. The device now has an address + // of ZERO after port reset, so need to set Dev->Address to the device again for + // host to communicate with it. // - Address = Dev->Address; - Dev->Address = 0; - Status = UsbSetAddress (Dev, Address); - Dev->Address = Address; + DevAddress = Dev->Address; + Dev->Address = 0; + Status = UsbSetAddress (Dev, DevAddress); + Dev->Address = DevAddress; + + gBS->Stall (USB_SET_DEVICE_ADDRESS_STALL); if (EFI_ERROR (Status)) { + // + // It may fail due to device disconnection or other reasons. + // DEBUG (( EFI_D_ERROR, "UsbIoPortReset: failed to set address for device %d - %r\n", - Address, Status)); + Dev->Address, Status)); goto ON_EXIT; } - gBS->Stall (USB_SET_DEVICE_ADDRESS_STALL); + DEBUG (( EFI_D_INFO, "UsbIoPortReset: device is now ADDRESSED at %d\n", Dev->Address)); - DEBUG (( EFI_D_INFO, "UsbIoPortReset: device is now ADDRESSED at %d\n", Address)); - // // Reset the current active configure, after this device // is in CONFIGURED state. @@ -871,7 +884,7 @@ UsbIoPortReset ( if (EFI_ERROR (Status)) { DEBUG (( EFI_D_ERROR, "UsbIoPortReset: failed to set configure for device %d - %r\n", - Address, Status)); + Dev->Address, Status)); } } @@ -913,8 +926,9 @@ UsbBusBuildProtocol ( return EFI_OUT_OF_RESOURCES; } - UsbBus->Signature = USB_BUS_SIGNATURE; - UsbBus->HostHandle = Controller; + UsbBus->Signature = USB_BUS_SIGNATURE; + UsbBus->HostHandle = Controller; + UsbBus->MaxDevices = USB_MAX_DEVICES; Status = gBS->OpenProtocol ( Controller, @@ -965,15 +979,23 @@ UsbBusBuildProtocol ( goto CLOSE_HC; } - UsbHcReset (UsbBus, EFI_USB_HC_RESET_GLOBAL); - UsbHcSetState (UsbBus, EfiUsbHcStateOperational); + if (!EFI_ERROR (Status)) { + // + // The EFI_USB2_HC_PROTOCOL is produced for XHCI support. + // Then its max supported devices are 256. Otherwise it's 128. + // + ASSERT (UsbBus->Usb2Hc != NULL); + if (UsbBus->Usb2Hc->MajorRevision == 0x3) { + UsbBus->MaxDevices = 256; + } + } // // Install an EFI_USB_BUS_PROTOCOL to host controller to identify it. // Status = gBS->InstallProtocolInterface ( &Controller, - &mUsbBusProtocolGuid, + &gEfiCallerIdGuid, EFI_NATIVE_INTERFACE, &UsbBus->BusId ); @@ -1010,10 +1032,20 @@ UsbBusBuildProtocol ( RootHub->Bus = UsbBus; RootHub->NumOfInterface = 1; RootHub->Interfaces[0] = RootIf; + RootHub->Tier = 0; RootIf->Signature = USB_INTERFACE_SIGNATURE; RootIf->Device = RootHub; RootIf->DevicePath = UsbBus->DevicePath; - + + // + // Report Status Code here since we will enumerate the USB devices + // + REPORT_STATUS_CODE_WITH_DEVICE_PATH ( + EFI_PROGRESS_CODE, + (EFI_IO_BUS_USB | EFI_IOB_PC_DETECT), + UsbBus->DevicePath + ); + Status = mUsbRootHubApi.Init (RootIf); if (EFI_ERROR (Status)) { @@ -1035,7 +1067,7 @@ FREE_ROOTHUB: } UNINSTALL_USBBUS: - gBS->UninstallProtocolInterface (Controller, &mUsbBusProtocolGuid, &UsbBus->BusId); + gBS->UninstallProtocolInterface (Controller, &gEfiCallerIdGuid, &UsbBus->BusId); CLOSE_HC: if (UsbBus->Usb2Hc != NULL) { @@ -1260,6 +1292,26 @@ UsbBusControllerDriverStart ( { EFI_USB_BUS_PROTOCOL *UsbBusId; EFI_STATUS Status; + EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath; + + Status = gBS->OpenProtocol ( + Controller, + &gEfiDevicePathProtocolGuid, + (VOID **) &ParentDevicePath, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + ASSERT_EFI_ERROR (Status); + + // + // Report Status Code here since we will initialize the host controller + // + REPORT_STATUS_CODE_WITH_DEVICE_PATH ( + EFI_PROGRESS_CODE, + (EFI_IO_BUS_USB | EFI_IOB_PC_INIT), + ParentDevicePath + ); // // Locate the USB bus protocol, if it is found, USB bus @@ -1267,7 +1319,7 @@ UsbBusControllerDriverStart ( // Status = gBS->OpenProtocol ( Controller, - &mUsbBusProtocolGuid, + &gEfiCallerIdGuid, (VOID **) &UsbBusId, This->DriverBindingHandle, Controller, @@ -1278,6 +1330,7 @@ UsbBusControllerDriverStart ( // // If first start, build the bus execute environment and install bus protocol // + REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_IO_BUS_USB | EFI_P_PC_ENABLE)); Status = UsbBusBuildProtocol (This, Controller, RemainingDevicePath); if (EFI_ERROR (Status)) { return Status; @@ -1287,7 +1340,7 @@ UsbBusControllerDriverStart ( // Status = gBS->OpenProtocol ( Controller, - &mUsbBusProtocolGuid, + &gEfiCallerIdGuid, (VOID **) &UsbBusId, This->DriverBindingHandle, Controller, @@ -1360,6 +1413,7 @@ UsbBusControllerDriverStop ( EFI_TPL OldTpl; UINTN Index; EFI_STATUS Status; + EFI_STATUS ReturnStatus; Status = EFI_SUCCESS; @@ -1369,6 +1423,7 @@ UsbBusControllerDriverStop ( // OldTpl = gBS->RaiseTPL (TPL_CALLBACK); + ReturnStatus = EFI_SUCCESS; for (Index = 0; Index < NumberOfChildren; Index++) { Status = gBS->OpenProtocol ( ChildHandleBuffer[Index], @@ -1392,11 +1447,11 @@ UsbBusControllerDriverStop ( UsbIf = USB_INTERFACE_FROM_USBIO (UsbIo); UsbDev = UsbIf->Device; - UsbRemoveDevice (UsbDev); + ReturnStatus = UsbRemoveDevice (UsbDev); } gBS->RestoreTPL (OldTpl); - return EFI_SUCCESS; + return ReturnStatus; } DEBUG (( EFI_D_INFO, "UsbBusStop: usb bus stopped on %p\n", Controller)); @@ -1406,7 +1461,7 @@ UsbBusControllerDriverStop ( // Status = gBS->OpenProtocol ( Controller, - &mUsbBusProtocolGuid, + &gEfiCallerIdGuid, (VOID **) &BusId, This->DriverBindingHandle, Controller, @@ -1425,57 +1480,64 @@ UsbBusControllerDriverStop ( // BugBug: Raise TPL to callback level instead of USB_BUS_TPL to avoid TPL conflict // OldTpl = gBS->RaiseTPL (TPL_CALLBACK); - UsbHcSetState (Bus, EfiUsbHcStateHalt); RootHub = Bus->Devices[0]; RootIf = RootHub->Interfaces[0]; - mUsbRootHubApi.Release (RootIf); - - for (Index = 1; Index < USB_MAX_DEVICES; Index++) { + ASSERT (Bus->MaxDevices <= 256); + ReturnStatus = EFI_SUCCESS; + for (Index = 1; Index < Bus->MaxDevices; Index++) { if (Bus->Devices[Index] != NULL) { - UsbRemoveDevice (Bus->Devices[Index]); + Status = UsbRemoveDevice (Bus->Devices[Index]); + if (EFI_ERROR (Status)) { + ReturnStatus = Status; + } } } gBS->RestoreTPL (OldTpl); - gBS->FreePool (RootIf); - gBS->FreePool (RootHub); - Status = UsbBusFreeUsbDPList (&Bus->WantedUsbIoDPList); - ASSERT (!EFI_ERROR (Status)); + if (!EFI_ERROR (ReturnStatus)) { + mUsbRootHubApi.Release (RootIf); + gBS->FreePool (RootIf); + gBS->FreePool (RootHub); - // - // Uninstall the bus identifier and close USB_HC/USB2_HC protocols - // - gBS->UninstallProtocolInterface (Controller, &mUsbBusProtocolGuid, &Bus->BusId); + Status = UsbBusFreeUsbDPList (&Bus->WantedUsbIoDPList); + ASSERT (!EFI_ERROR (Status)); - if (Bus->Usb2Hc != NULL) { - gBS->CloseProtocol ( - Controller, - &gEfiUsb2HcProtocolGuid, - This->DriverBindingHandle, - Controller - ); - } + // + // Uninstall the bus identifier and close USB_HC/USB2_HC protocols + // + gBS->UninstallProtocolInterface (Controller, &gEfiCallerIdGuid, &Bus->BusId); - if (Bus->UsbHc != NULL) { - gBS->CloseProtocol ( - Controller, - &gEfiUsbHcProtocolGuid, - This->DriverBindingHandle, - Controller - ); - } + if (Bus->Usb2Hc != NULL) { + Status = gBS->CloseProtocol ( + Controller, + &gEfiUsb2HcProtocolGuid, + This->DriverBindingHandle, + Controller + ); + } - gBS->CloseProtocol ( - Controller, - &gEfiDevicePathProtocolGuid, - This->DriverBindingHandle, - Controller - ); + if (Bus->UsbHc != NULL) { + Status = gBS->CloseProtocol ( + Controller, + &gEfiUsbHcProtocolGuid, + This->DriverBindingHandle, + Controller + ); + } - gBS->FreePool (Bus); + if (!EFI_ERROR (Status)) { + gBS->CloseProtocol ( + Controller, + &gEfiDevicePathProtocolGuid, + This->DriverBindingHandle, + Controller + ); + gBS->FreePool (Bus); + } + } return Status; }