]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Usb/UsbMouseDxe/UsbMouse.c
MdeModulePkg/UsbMouse: Get HID descriptor from the return of Get_Desc(Configuration...
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbMouseDxe / UsbMouse.c
index d81eaf7eae07e36211845a131a1bd6f92cf5589c..d4e5a03475849f4d3ca9c0f117862fec4de3aa22 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   USB Mouse Driver that manages USB mouse and produces Simple Pointer Protocol.\r
 \r
-Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 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
@@ -521,46 +521,108 @@ InitializeUsbMouseDevice (
   IN OUT USB_MOUSE_DEV           *UsbMouseDev\r
   )\r
 {\r
-  EFI_USB_IO_PROTOCOL     *UsbIo;\r
-  UINT8                   Protocol;\r
-  EFI_STATUS              Status;\r
-  EFI_USB_HID_DESCRIPTOR  MouseHidDesc;\r
-  UINT8                   *ReportDesc;\r
-  UINT8                   ReportId;\r
-  UINT8                   Duration;\r
+  EFI_USB_IO_PROTOCOL       *UsbIo;\r
+  UINT8                     Protocol;\r
+  EFI_STATUS                Status;\r
+  EFI_USB_HID_DESCRIPTOR    *MouseHidDesc;\r
+  UINT8                     *ReportDesc;\r
+  UINT8                     ReportId;\r
+  UINT8                     Duration;\r
+  EFI_USB_CONFIG_DESCRIPTOR ConfigDesc;\r
+  VOID                      *Buf;\r
+  UINT32                    TransferResult;\r
+  UINT16                    Total;\r
+  USB_DESC_HEAD             *Head;\r
+  BOOLEAN                   Start;\r
 \r
   UsbIo = UsbMouseDev->UsbIo;\r
 \r
   //\r
-  // Get HID descriptor\r
+  // Get the current configuration descriptor. Note that it doesn't include other descriptors.\r
+  //\r
+  Status = UsbIo->UsbGetConfigDescriptor (\r
+                    UsbIo,\r
+                    &ConfigDesc\r
+                    );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // By issuing Get_Descriptor(Configuration) request with total length, we get the Configuration descriptor,\r
+  // all Interface descriptors, all Endpoint descriptors, and the HID descriptor for each interface.\r
   //\r
-  Status = UsbGetHidDescriptor (\r
+  Buf = AllocateZeroPool (ConfigDesc.TotalLength);\r
+  if (Buf == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  Status = UsbGetDescriptor (\r
              UsbIo,\r
-             UsbMouseDev->InterfaceDescriptor.InterfaceNumber,\r
-             &MouseHidDesc\r
+             (UINT16)((USB_DESC_TYPE_CONFIG << 8) | (ConfigDesc.ConfigurationValue - 1)),\r
+             0,\r
+             ConfigDesc.TotalLength,\r
+             Buf,\r
+             &TransferResult\r
              );\r
   if (EFI_ERROR (Status)) {\r
+    FreePool (Buf);\r
     return Status;\r
   }\r
 \r
+  Total = 0;\r
+  Start = FALSE;\r
+  Head  = (USB_DESC_HEAD *)Buf;  \r
+  MouseHidDesc = NULL;\r
+\r
+  //\r
+  // Get HID descriptor from the receipt of Get_Descriptor(Configuration) request.\r
+  // This algorithm is based on the fact that the HID descriptor shall be interleaved\r
+  // between the interface and endpoint descriptors for HID interfaces.\r
+  //\r
+  while (Total < ConfigDesc.TotalLength) {\r
+    if (Head->Type == USB_DESC_TYPE_INTERFACE) {\r
+      if ((((USB_INTERFACE_DESCRIPTOR *)Head)->InterfaceNumber == UsbMouseDev->InterfaceDescriptor.InterfaceNumber) &&\r
+        (((USB_INTERFACE_DESCRIPTOR *)Head)->AlternateSetting == UsbMouseDev->InterfaceDescriptor.AlternateSetting)) {\r
+        Start = TRUE;\r
+      }\r
+    }\r
+    if ((Start == TRUE) && (Head->Type == USB_DESC_TYPE_ENDPOINT)) {\r
+      break;\r
+    }\r
+    if ((Start == TRUE) && (Head->Type == USB_DESC_TYPE_HID)) {\r
+      MouseHidDesc = (EFI_USB_HID_DESCRIPTOR *)Head;\r
+      break;\r
+    }\r
+    Total += (UINT16)Head->Len;\r
+    Head   = (USB_DESC_HEAD*)((UINT8 *)Buf + Total);\r
+  }\r
+\r
+  if (MouseHidDesc == NULL) {\r
+    FreePool (Buf);\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
   //\r
   // Get report descriptor\r
   //\r
-  if (MouseHidDesc.HidClassDesc[0].DescriptorType != USB_DESC_TYPE_REPORT) {\r
+  if (MouseHidDesc->HidClassDesc[0].DescriptorType != USB_DESC_TYPE_REPORT) {\r
+    FreePool (Buf);\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  ReportDesc = AllocateZeroPool (MouseHidDesc.HidClassDesc[0].DescriptorLength);\r
+  ReportDesc = AllocateZeroPool (MouseHidDesc->HidClassDesc[0].DescriptorLength);\r
   ASSERT (ReportDesc != NULL);\r
 \r
   Status = UsbGetReportDescriptor (\r
              UsbIo,\r
              UsbMouseDev->InterfaceDescriptor.InterfaceNumber,\r
-             MouseHidDesc.HidClassDesc[0].DescriptorLength,\r
+             MouseHidDesc->HidClassDesc[0].DescriptorLength,\r
              ReportDesc\r
              );\r
 \r
   if (EFI_ERROR (Status)) {\r
+    FreePool (Buf);\r
     FreePool (ReportDesc);\r
     return Status;\r
   }\r
@@ -571,10 +633,11 @@ InitializeUsbMouseDevice (
   Status = ParseMouseReportDescriptor (\r
              UsbMouseDev,\r
              ReportDesc,\r
-             MouseHidDesc.HidClassDesc[0].DescriptorLength\r
+             MouseHidDesc->HidClassDesc[0].DescriptorLength\r
              );\r
 \r
   if (EFI_ERROR (Status)) {\r
+    FreePool (Buf);\r
     FreePool (ReportDesc);\r
     return Status;\r
   }\r
@@ -610,6 +673,7 @@ InitializeUsbMouseDevice (
                );\r
 \r
     if (EFI_ERROR (Status)) {\r
+      FreePool (Buf);\r
       FreePool (ReportDesc);\r
       return Status;\r
     }\r
@@ -632,6 +696,7 @@ InitializeUsbMouseDevice (
     Duration\r
     );\r
 \r
+  FreePool (Buf);\r
   FreePool (ReportDesc);\r
 \r
   //\r