]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbBusDxe/UsbEnumer.c
MdeModulePkg: add support for ORICO PEUS3-2P card
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusDxe / UsbEnumer.c
index 3a2b8bc017ba92ba650633ca07ed62c7b029a843..8d031b90457efa05fb2b068a7cb47806029696fb 100644 (file)
@@ -2,8 +2,8 @@
 \r
     Usb bus enumeration support.\r
 \r
-Copyright (c) 2007 - 2008, Intel Corporation\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2007 - 2012, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php\r
@@ -15,7 +15,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "UsbBus.h"\r
 \r
-\r
 /**\r
   Return the endpoint descriptor in this interface.\r
 \r
@@ -32,9 +31,12 @@ UsbGetEndpointDesc (
   )\r
 {\r
   USB_ENDPOINT_DESC       *EpDesc;\r
-  UINTN                   Index;\r
-\r
-  for (Index = 0; Index < UsbIf->IfSetting->Desc.NumEndpoints; Index++) {\r
+  UINT8                   Index;\r
+  UINT8                   NumEndpoints;\r
+  \r
+  NumEndpoints = UsbIf->IfSetting->Desc.NumEndpoints;\r
+  \r
+  for (Index = 0; Index < NumEndpoints; Index++) {\r
     EpDesc = UsbIf->IfSetting->Endpoints[Index];\r
 \r
     if (EpDesc->Desc.EndpointAddress == EpAddr) {\r
@@ -51,8 +53,6 @@ UsbGetEndpointDesc (
 \r
   @param  UsbIf                 The USB interface to free.\r
 \r
-  @return None.\r
-\r
 **/\r
 VOID\r
 UsbFreeInterface (\r
@@ -71,10 +71,10 @@ UsbFreeInterface (
          );\r
 \r
   if (UsbIf->DevicePath != NULL) {\r
-    gBS->FreePool (UsbIf->DevicePath);\r
+    FreePool (UsbIf->DevicePath);\r
   }\r
 \r
-  gBS->FreePool (UsbIf);\r
+  FreePool (UsbIf);\r
 }\r
 \r
 \r
@@ -108,6 +108,7 @@ UsbCreateInterface (
   UsbIf->Signature  = USB_INTERFACE_SIGNATURE;\r
   UsbIf->Device     = Device;\r
   UsbIf->IfDesc     = IfDesc;\r
+  ASSERT (IfDesc->ActiveIndex < USB_MAX_INTERFACE_SETTING);\r
   UsbIf->IfSetting  = IfDesc->Settings[IfDesc->ActiveIndex];\r
 \r
   CopyMem (\r
@@ -175,10 +176,10 @@ UsbCreateInterface (
 \r
 ON_ERROR:\r
   if (UsbIf->DevicePath != NULL) {\r
-    gBS->FreePool (UsbIf->DevicePath);\r
+    FreePool (UsbIf->DevicePath);\r
   }\r
 \r
-  gBS->FreePool (UsbIf);\r
+  FreePool (UsbIf);\r
   return NULL;\r
 }\r
 \r
@@ -188,8 +189,6 @@ ON_ERROR:
 \r
   @param  Device                The USB device to free.\r
 \r
-  @return None.\r
-\r
 **/\r
 VOID\r
 UsbFreeDevice (\r
@@ -234,13 +233,14 @@ UsbCreateDevice (
   Device->ParentAddr  = ParentIf->Device->Address;\r
   Device->ParentIf    = ParentIf;\r
   Device->ParentPort  = ParentPort;\r
+  Device->Tier        = (UINT8)(ParentIf->Device->Tier + 1);\r
   return Device;\r
 }\r
 \r
 \r
 /**\r
   Connect the USB interface with its driver. EFI USB bus will\r
-  create a USB interface for each seperate interface descriptor.\r
+  create a USB interface for each separate interface descriptor.\r
 \r
   @param  UsbIf             The interface to connect driver to.\r
 \r
@@ -280,14 +280,14 @@ UsbConnectDriver (
     //\r
     if (UsbBusIsWantedUsbIO (UsbIf->Device->Bus, UsbIf)) {\r
       OldTpl            = UsbGetCurrentTpl ();\r
-      DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL before connect is %d\n", OldTpl));\r
+      DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL before connect is %d, %p\n", (UINT32)OldTpl, UsbIf->Handle));\r
 \r
       gBS->RestoreTPL (TPL_CALLBACK);\r
 \r
       Status            = gBS->ConnectController (UsbIf->Handle, NULL, NULL, TRUE);\r
       UsbIf->IsManaged  = (BOOLEAN)!EFI_ERROR (Status);\r
 \r
-      DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL after connect is %d\n", UsbGetCurrentTpl()));\r
+      DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL after connect is %d\n", (UINT32)UsbGetCurrentTpl()));\r
       ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK);\r
 \r
       gBS->RaiseTPL (OldTpl);\r
@@ -326,6 +326,7 @@ UsbSelectSetting (
   Setting = NULL;\r
 \r
   for (Index = 0; Index < IfDesc->NumOfSetting; Index++) {\r
+    ASSERT (Index < USB_MAX_INTERFACE_SETTING);\r
     Setting = IfDesc->Settings[Index];\r
 \r
     if (Setting->Desc.AlternateSetting == Alternate) {\r
@@ -339,6 +340,7 @@ UsbSelectSetting (
 \r
   IfDesc->ActiveIndex = Index;\r
 \r
+  ASSERT (Setting != NULL);\r
   DEBUG ((EFI_D_INFO, "UsbSelectSetting: setting %d selected for interface %d\n",\r
               Alternate, Setting->Desc.InterfaceNumber));\r
 \r
@@ -421,6 +423,7 @@ UsbSelectConfig (
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
+    ASSERT (Index < USB_MAX_INTERFACE);\r
     Device->Interfaces[Index] = UsbIf;\r
 \r
     //\r
@@ -446,8 +449,6 @@ UsbSelectConfig (
 \r
   @param  UsbIf                 The interface to disconnect driver from.\r
 \r
-  @return None.\r
-\r
 **/\r
 VOID\r
 UsbDisconnectDriver (\r
@@ -455,6 +456,7 @@ UsbDisconnectDriver (
   )\r
 {\r
   EFI_TPL                 OldTpl;\r
+  EFI_STATUS              Status;\r
 \r
   //\r
   // Release the hub if it's a hub controller, otherwise\r
@@ -472,14 +474,14 @@ UsbDisconnectDriver (
     // or disconnect at CALLBACK.\r
     //\r
     OldTpl           = UsbGetCurrentTpl ();\r
-    DEBUG ((EFI_D_INFO, "UsbDisconnectDriver: old TPL is %d\n", OldTpl));\r
+    DEBUG ((EFI_D_INFO, "UsbDisconnectDriver: old TPL is %d, %p\n", (UINT32)OldTpl, UsbIf->Handle));\r
 \r
     gBS->RestoreTPL (TPL_CALLBACK);\r
 \r
-    gBS->DisconnectController (UsbIf->Handle, NULL, NULL);\r
+    Status = gBS->DisconnectController (UsbIf->Handle, NULL, NULL);\r
     UsbIf->IsManaged = FALSE;\r
 \r
-    DEBUG (( EFI_D_INFO, "UsbDisconnectDriver: TPL after disconnect is %d\n", UsbGetCurrentTpl()));\r
+    DEBUG (( EFI_D_INFO, "UsbDisconnectDriver: TPL after disconnect is %d, %d\n", (UINT32)UsbGetCurrentTpl(), Status));\r
     ASSERT (UsbGetCurrentTpl () == TPL_CALLBACK);\r
 \r
     gBS->RaiseTPL (OldTpl);\r
@@ -492,8 +494,6 @@ UsbDisconnectDriver (
 \r
   @param  Device                The USB device to remove configuration from.\r
 \r
-  @return None.\r
-\r
 **/\r
 VOID\r
 UsbRemoveConfig (\r
@@ -506,7 +506,8 @@ UsbRemoveConfig (
   //\r
   // Remove each interface of the device\r
   //\r
-  for (Index = 0; Index < Device->NumOfInterface; Index++) {\r
+  for (Index = 0; Index < Device->NumOfInterface; Index++) {    \r
+    ASSERT (Index < USB_MAX_INTERFACE);\r
     UsbIf = Device->Interfaces[Index];\r
 \r
     if (UsbIf == NULL) {\r
@@ -539,7 +540,7 @@ UsbRemoveDevice (
   USB_BUS                 *Bus;\r
   USB_DEVICE              *Child;\r
   EFI_STATUS              Status;\r
-  UINT8                   Index;\r
+  UINTN                   Index;\r
 \r
   Bus = Device->Bus;\r
 \r
@@ -547,7 +548,7 @@ UsbRemoveDevice (
   // Remove all the devices on its downstream ports. Search from devices[1].\r
   // Devices[0] is the root hub.\r
   //\r
-  for (Index = 1; Index < USB_MAX_DEVICES; Index++) {\r
+  for (Index = 1; Index < Bus->MaxDevices; Index++) {\r
     Child = Bus->Devices[Index];\r
 \r
     if ((Child == NULL) || (Child->ParentAddr != Device->Address)) {\r
@@ -566,6 +567,7 @@ UsbRemoveDevice (
 \r
   DEBUG (( EFI_D_INFO, "UsbRemoveDevice: device %d removed\n", Device->Address));\r
 \r
+  ASSERT (Device->Address < Bus->MaxDevices);\r
   Bus->Devices[Device->Address] = NULL;\r
   UsbFreeDevice (Device);\r
 \r
@@ -597,7 +599,7 @@ UsbFindChild (
   //\r
   // Start checking from device 1, device 0 is the root hub\r
   //\r
-  for (Index = 1; Index < USB_MAX_DEVICES; Index++) {\r
+  for (Index = 1; Index < Bus->MaxDevices; Index++) {\r
     Device = Bus->Devices[Index];\r
 \r
     if ((Device != NULL) && (Device->ParentAddr == HubIf->Device->Address) &&\r
@@ -633,15 +635,15 @@ UsbEnumerateNewDev (
   USB_DEVICE              *Child;\r
   USB_DEVICE              *Parent;\r
   EFI_USB_PORT_STATUS     PortState;\r
-  UINT8                   Address;\r
+  UINTN                   Address;\r
   UINT8                   Config;\r
   EFI_STATUS              Status;\r
 \r
-  Address = USB_MAX_DEVICES;\r
   Parent  = HubIf->Device;\r
   Bus     = Parent->Bus;\r
-  HubApi  = HubIf->HubApi;\r
-  \r
+  HubApi  = HubIf->HubApi;  \r
+  Address = Bus->MaxDevices;\r
+\r
   gBS->Stall (USB_WAIT_PORT_STABLE_STALL);\r
   \r
   //\r
@@ -677,38 +679,42 @@ UsbEnumerateNewDev (
     goto ON_ERROR;\r
   }\r
 \r
-  if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_LOW_SPEED)) {\r
-    Child->Speed = EFI_USB_SPEED_LOW;\r
-\r
+  if (!USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_CONNECTION)) {\r
+    DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: No device presented at port %d\n", Port));\r
+    goto ON_ERROR;\r
+  } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_SUPER_SPEED)){\r
+    Child->Speed      = EFI_USB_SPEED_SUPER;\r
+    Child->MaxPacket0 = 512;\r
   } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_HIGH_SPEED)) {\r
-    Child->Speed = EFI_USB_SPEED_HIGH;\r
-\r
+    Child->Speed      = EFI_USB_SPEED_HIGH;\r
+    Child->MaxPacket0 = 64;\r
+  } else if (USB_BIT_IS_SET (PortState.PortStatus, USB_PORT_STAT_LOW_SPEED)) {\r
+    Child->Speed      = EFI_USB_SPEED_LOW;\r
+    Child->MaxPacket0 = 8;\r
   } else {\r
-    Child->Speed = EFI_USB_SPEED_FULL;\r
+    Child->Speed      = EFI_USB_SPEED_FULL;\r
+    Child->MaxPacket0 = 8;\r
   }\r
 \r
   DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device is of %d speed\n", Child->Speed));\r
 \r
-  if (Child->Speed != EFI_USB_SPEED_HIGH) {\r
+  if (((Child->Speed == EFI_USB_SPEED_LOW) || (Child->Speed == EFI_USB_SPEED_FULL)) &&\r
+      (Parent->Speed == EFI_USB_SPEED_HIGH)) {\r
     //\r
-    // If the child isn't a high speed device, it is necessary to\r
+    // If the child is a low or full speed device, it is necessary to\r
     // set the transaction translator. Port TT is 1-based.\r
     // This is quite simple:\r
     //  1. if parent is of high speed, then parent is our translator\r
     //  2. otherwise use parent's translator.\r
     //\r
-    if (Parent->Speed == EFI_USB_SPEED_HIGH) {\r
-      Child->Translator.TranslatorHubAddress  = Parent->Address;\r
-      Child->Translator.TranslatorPortNumber  = (UINT8) (Port + 1);\r
-\r
-    } else {\r
-      Child->Translator = Parent->Translator;\r
-    }\r
-\r
-    DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device uses translator (%d, %d)\n",\r
-                Child->Translator.TranslatorHubAddress,\r
-                Child->Translator.TranslatorPortNumber));\r
+    Child->Translator.TranslatorHubAddress  = Parent->Address;\r
+    Child->Translator.TranslatorPortNumber  = (UINT8) (Port + 1);\r
+  } else {\r
+    Child->Translator = Parent->Translator;\r
   }\r
+  DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: device uses translator (%d, %d)\n",\r
+           Child->Translator.TranslatorHubAddress,\r
+           Child->Translator.TranslatorPortNumber));\r
 \r
   //\r
   // After port is reset, hub establishes a signal path between\r
@@ -717,50 +723,51 @@ UsbEnumerateNewDev (
   // a time) , and ready to respond to control transfer at EP 0.\r
   //\r
 \r
-  //\r
-  // Host sends a Get_Descriptor request to learn the max packet\r
-  // size of default pipe (only part of the device's descriptor).\r
-  //\r
-  Status = UsbGetMaxPacketSize0 (Child);\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get max packet for EP 0 - %r\n", Status));\r
-    goto ON_ERROR;\r
-  }\r
-\r
-  DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: max packet size for EP 0 is %d\n", Child->MaxPacket0));\r
-\r
   //\r
   // Host assigns an address to the device. Device completes the\r
   // status stage with default address, then switches to new address.\r
   // ADDRESS state. Address zero is reserved for root hub.\r
   //\r
-  for (Address = 1; Address < USB_MAX_DEVICES; Address++) {\r
+  ASSERT (Bus->MaxDevices <= 256);\r
+  for (Address = 1; Address < Bus->MaxDevices; Address++) {\r
     if (Bus->Devices[Address] == NULL) {\r
       break;\r
     }\r
   }\r
 \r
-  if (Address == USB_MAX_DEVICES) {\r
+  if (Address >= Bus->MaxDevices) {\r
     DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: address pool is full for port %d\n", Port));\r
 \r
     Status = EFI_ACCESS_DENIED;\r
     goto ON_ERROR;\r
   }\r
 \r
+  Status                = UsbSetAddress (Child, (UINT8)Address);\r
+  Child->Address        = (UINT8)Address;\r
   Bus->Devices[Address] = Child;\r
-  Status                = UsbSetAddress (Child, Address);\r
-  Child->Address        = Address;\r
 \r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to set device address - %r\n", Status));\r
     goto ON_ERROR;\r
   }\r
-  \r
+\r
   gBS->Stall (USB_SET_DEVICE_ADDRESS_STALL);\r
 \r
   DEBUG ((EFI_D_INFO, "UsbEnumerateNewDev: device is now ADDRESSED at %d\n", Address));\r
 \r
+  //\r
+  // Host sends a Get_Descriptor request to learn the max packet\r
+  // size of default pipe (only part of the device's descriptor).\r
+  //\r
+  Status = UsbGetMaxPacketSize0 (Child);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "UsbEnumerateNewDev: failed to get max packet for EP 0 - %r\n", Status));\r
+    goto ON_ERROR;\r
+  }\r
+\r
+  DEBUG (( EFI_D_INFO, "UsbEnumerateNewDev: max packet size for EP 0 is %d\n", Child->MaxPacket0));\r
+\r
   //\r
   // Host learns about the device's abilities by requesting device's\r
   // entire descriptions.\r
@@ -799,7 +806,7 @@ UsbEnumerateNewDev (
   return EFI_SUCCESS;\r
 \r
 ON_ERROR:\r
-  if (Address != USB_MAX_DEVICES) {\r
+  if (Address != Bus->MaxDevices) {\r
     Bus->Devices[Address] = NULL;\r
   }\r
 \r
@@ -846,12 +853,16 @@ UsbEnumeratePort (
     return Status;\r
   }\r
 \r
-  if (PortState.PortChangeStatus == 0) {\r
+  //\r
+  // Only handle connection/enable/overcurrent/reset change.\r
+  // Usb super speed hub may report other changes, such as warm reset change. Ignore them.\r
+  //\r
+  if ((PortState.PortChangeStatus & (USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE | USB_PORT_STAT_C_OVERCURRENT | USB_PORT_STAT_C_RESET)) == 0) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
-  DEBUG (( EFI_D_INFO, "UsbEnumeratePort: port %d state - %x, change - %x\n",\r
-              Port, PortState.PortStatus, PortState.PortChangeStatus));\r
+  DEBUG (( EFI_D_INFO, "UsbEnumeratePort: port %d state - %02x, change - %02x on %p\n",\r
+              Port, PortState.PortChangeStatus, PortState.PortStatus, HubIf));\r
 \r
   //\r
   // This driver only process two kinds of events now: over current and\r
@@ -905,7 +916,7 @@ UsbEnumeratePort (
   Child = UsbFindChild (HubIf, Port);\r
   \r
   if (Child != NULL) {\r
-    DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device at port %d removed from system\n", Port));\r
+    DEBUG (( EFI_D_INFO, "UsbEnumeratePort: device at port %d removed from root hub %p\n", Port, HubIf));\r
     UsbRemoveDevice (Child);\r
   }\r
   \r
@@ -931,8 +942,6 @@ UsbEnumeratePort (
   @param  Event                 The event that is triggered.\r
   @param  Context               The context to the event.\r
 \r
-  @return None.\r
-\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -946,7 +955,7 @@ UsbHubEnumeration (
   UINT8                   Bit;\r
   UINT8                   Index;\r
 \r
-  ASSERT (Context);\r
+  ASSERT (Context != NULL);\r
 \r
   HubIf = (USB_INTERFACE *) Context;\r
 \r
@@ -982,8 +991,6 @@ UsbHubEnumeration (
   @param  Event                 The event that is triggered.\r
   @param  Context               The context to the event.\r
 \r
-  @return None.\r
-\r
 **/\r
 VOID\r
 EFIAPI\r