MdeModulePkg/XhciDxe: Separate common logic to XhcTransfer
authorRuiyu Ni <ruiyu.ni@intel.com>
Wed, 28 Jun 2017 09:11:34 +0000 (17:11 +0800)
committerRuiyu Ni <ruiyu.ni@intel.com>
Wed, 5 Jul 2017 03:31:06 +0000 (11:31 +0800)
The patch separates the common logic in XhcControlTransfer,
XhcBulkTransfer and XhcSyncIntTransfer to a sub-routine
XhcTransfer. It doesn't have functionality impact.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Hao Wu <hao.a.wu@intel.com>
Cc: Star Zeng <star.zeng@intel.com>
Cc: Feng Tian <feng.tian@intel.com>
MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c

index 2f6137e..e3cbdbc 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   The XHCI controller driver.\r
 \r
-Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2011 - 2017, 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
@@ -716,6 +716,97 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
+/**\r
+  Submits a new transaction to a target USB device.\r
+\r
+  @param  Xhc                   The XHCI Instance.\r
+  @param  DeviceAddress         The target device address.\r
+  @param  EndPointAddress       Endpoint number and its direction encoded in bit 7\r
+  @param  DeviceSpeed           Target device speed.\r
+  @param  MaximumPacketLength   Maximum packet size the default control transfer\r
+                                endpoint is capable of sending or receiving.\r
+  @param  Type                  The transaction type.\r
+  @param  Request               USB device request to send.\r
+  @param  Data                  Data buffer to be transmitted or received from USB\r
+                                device.\r
+  @param  DataLength            The size (in bytes) of the data buffer.\r
+  @param  Timeout               Indicates the maximum timeout, in millisecond.\r
+  @param  TransferResult        Return the result of this control transfer.\r
+\r
+  @retval EFI_SUCCESS           Transfer was completed successfully.\r
+  @retval EFI_OUT_OF_RESOURCES  The transfer failed due to lack of resources.\r
+  @retval EFI_INVALID_PARAMETER Some parameters are invalid.\r
+  @retval EFI_TIMEOUT           Transfer failed due to timeout.\r
+  @retval EFI_DEVICE_ERROR      Transfer failed due to host controller or device error.\r
+**/\r
+EFI_STATUS\r
+XhcTransfer (\r
+  IN     USB_XHCI_INSTANCE                   *Xhc,\r
+  IN     UINT8                               DeviceAddress,\r
+  IN     UINT8                               EndPointAddress,\r
+  IN     UINT8                               DeviceSpeed,\r
+  IN     UINTN                               MaximumPacketLength,\r
+  IN     UINTN                               Type,\r
+  IN     EFI_USB_DEVICE_REQUEST              *Request,\r
+  IN OUT VOID                                *Data,\r
+  IN OUT UINTN                               *DataLength,\r
+  IN     UINTN                               Timeout,\r
+  OUT    UINT32                              *TransferResult\r
+  )\r
+{\r
+  EFI_STATUS              Status;\r
+  EFI_STATUS              RecoveryStatus;\r
+  URB                     *Urb;\r
+\r
+  ASSERT ((Type == XHC_CTRL_TRANSFER) || (Type == XHC_BULK_TRANSFER) || (Type == XHC_INT_TRANSFER_SYNC));\r
+  Urb = XhcCreateUrb (\r
+          Xhc,\r
+          DeviceAddress,\r
+          EndPointAddress,\r
+          DeviceSpeed,\r
+          MaximumPacketLength,\r
+          Type,\r
+          Request,\r
+          Data,\r
+          *DataLength,\r
+          NULL,\r
+          NULL\r
+          );\r
+\r
+  if (Urb == NULL) {\r
+    DEBUG ((DEBUG_ERROR, "XhcTransfer[Type=%d]: failed to create URB!\n", Type));\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  Status = XhcExecTransfer (Xhc, FALSE, Urb, Timeout);\r
+\r
+  *TransferResult = Urb->Result;\r
+  *DataLength     = Urb->Completed;\r
+\r
+  if (Status == EFI_TIMEOUT) {\r
+    //\r
+    // The transfer timed out. Abort the transfer by dequeueing of the TD.\r
+    //\r
+    RecoveryStatus = XhcDequeueTrbFromEndpoint (Xhc, Urb);\r
+    if (EFI_ERROR (RecoveryStatus)) {\r
+      DEBUG((DEBUG_ERROR, "XhcTransfer[Type=%d]: XhcDequeueTrbFromEndpoint failed\n", Type));\r
+    }\r
+  } else {\r
+    if (*TransferResult == EFI_USB_NOERROR) {\r
+      Status = EFI_SUCCESS;\r
+    } else if (*TransferResult == EFI_USB_ERR_STALL) {\r
+      RecoveryStatus = XhcRecoverHaltedEndpoint (Xhc, Urb);\r
+      if (EFI_ERROR (RecoveryStatus)) {\r
+        DEBUG ((DEBUG_ERROR, "XhcTransfer[Type=%d]: XhcRecoverHaltedEndpoint failed\n", Type));\r
+      }\r
+      Status = EFI_DEVICE_ERROR;\r
+    }\r
+  }\r
+\r
+  Xhc->PciIo->Flush (Xhc->PciIo);\r
+  XhcFreeUrb (Xhc, Urb);\r
+  return Status;\r
+}\r
 \r
 /**\r
   Submits control transfer to a target USB device.\r
@@ -758,7 +849,6 @@ XhcControlTransfer (
   )\r
 {\r
   USB_XHCI_INSTANCE       *Xhc;\r
-  URB                     *Urb;\r
   UINT8                   Endpoint;\r
   UINT8                   Index;\r
   UINT8                   DescriptorType;\r
@@ -769,7 +859,6 @@ XhcControlTransfer (
   EFI_USB_HUB_DESCRIPTOR  *HubDesc;\r
   EFI_TPL                 OldTpl;\r
   EFI_STATUS              Status;\r
-  EFI_STATUS              RecoveryStatus;\r
   UINTN                   MapSize;\r
   EFI_USB_PORT_STATUS     PortStatus;\r
   UINT32                  State;\r
@@ -876,70 +965,24 @@ XhcControlTransfer (
   // combination of Ep addr and its direction.\r
   //\r
   Endpoint = (UINT8) (0 | ((TransferDirection == EfiUsbDataIn) ? 0x80 : 0));\r
-  Urb = XhcCreateUrb (\r
-          Xhc,\r
-          DeviceAddress,\r
-          Endpoint,\r
-          DeviceSpeed,\r
-          MaximumPacketLength,\r
-          XHC_CTRL_TRANSFER,\r
-          Request,\r
-          Data,\r
-          *DataLength,\r
-          NULL,\r
-          NULL\r
-          );\r
+  Status = XhcTransfer (\r
+             Xhc,\r
+             DeviceAddress,\r
+             Endpoint,\r
+             DeviceSpeed,\r
+             MaximumPacketLength,\r
+             XHC_CTRL_TRANSFER,\r
+             Request,\r
+             Data,\r
+             DataLength,\r
+             Timeout,\r
+             TransferResult\r
+             );\r
 \r
-  if (Urb == NULL) {\r
-    DEBUG ((EFI_D_ERROR, "XhcControlTransfer: failed to create URB"));\r
-    Status = EFI_OUT_OF_RESOURCES;\r
+  if (EFI_ERROR (Status)) {\r
     goto ON_EXIT;\r
   }\r
 \r
-  Status = XhcExecTransfer (Xhc, FALSE, Urb, Timeout);\r
-\r
-  //\r
-  // Get the status from URB. The result is updated in XhcCheckUrbResult\r
-  // which is called by XhcExecTransfer\r
-  //\r
-  *TransferResult = Urb->Result;\r
-  *DataLength     = Urb->Completed;\r
-\r
-  if (Status == EFI_TIMEOUT) {\r
-    //\r
-    // The transfer timed out. Abort the transfer by dequeueing of the TD.\r
-    //\r
-    RecoveryStatus = XhcDequeueTrbFromEndpoint(Xhc, Urb);\r
-    if (EFI_ERROR(RecoveryStatus)) {\r
-      DEBUG((EFI_D_ERROR, "XhcControlTransfer: XhcDequeueTrbFromEndpoint failed\n"));\r
-    }\r
-    goto FREE_URB;\r
-  } else {\r
-    if (*TransferResult == EFI_USB_NOERROR) {\r
-      Status = EFI_SUCCESS;\r
-    } else if (*TransferResult == EFI_USB_ERR_STALL) {\r
-      RecoveryStatus = XhcRecoverHaltedEndpoint(Xhc, Urb);\r
-      if (EFI_ERROR (RecoveryStatus)) {\r
-        DEBUG ((EFI_D_ERROR, "XhcControlTransfer: XhcRecoverHaltedEndpoint failed\n"));\r
-      }\r
-      Status = EFI_DEVICE_ERROR;\r
-      goto FREE_URB;\r
-    } else {\r
-      goto FREE_URB;\r
-    }\r
-  }\r
-\r
-  Xhc->PciIo->Flush (Xhc->PciIo);\r
-  \r
-  if (Urb->DataMap != NULL) {\r
-    Status = Xhc->PciIo->Unmap (Xhc->PciIo, Urb->DataMap);\r
-    ASSERT_EFI_ERROR (Status);\r
-    if (EFI_ERROR (Status)) {\r
-      Status = EFI_DEVICE_ERROR;\r
-      goto FREE_URB;\r
-    }  \r
-  }\r
-\r
   //\r
   // Hook Get_Descriptor request from UsbBus as we need evaluate context and configure endpoint.\r
   // Hook Get_Status request form UsbBus as we need trace device attach/detach event happened at hub.\r
@@ -1115,11 +1158,7 @@ XhcControlTransfer (
     *(UINT32 *)Data = *(UINT32*)&PortStatus;\r
   }\r
 \r
-FREE_URB:\r
-  FreePool (Urb);\r
-\r
 ON_EXIT:\r
-\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "XhcControlTransfer: error - %r, transfer - %x\n", Status, *TransferResult));\r
   }\r
@@ -1178,10 +1217,8 @@ XhcBulkTransfer (
   )\r
 {\r
   USB_XHCI_INSTANCE       *Xhc;\r
-  URB                     *Urb;\r
   UINT8                   SlotId;\r
   EFI_STATUS              Status;\r
-  EFI_STATUS              RecoveryStatus;\r
   EFI_TPL                 OldTpl;\r
 \r
   //\r
@@ -1227,56 +1264,21 @@ XhcBulkTransfer (
   // Create a new URB, insert it into the asynchronous\r
   // schedule list, then poll the execution status.\r
   //\r
-  Urb = XhcCreateUrb (\r
-          Xhc,\r
-          DeviceAddress,\r
-          EndPointAddress,\r
-          DeviceSpeed,\r
-          MaximumPacketLength,\r
-          XHC_BULK_TRANSFER,\r
-          NULL,\r
-          Data[0],\r
-          *DataLength,\r
-          NULL,\r
-          NULL\r
-          );\r
-\r
-  if (Urb == NULL) {\r
-    DEBUG ((EFI_D_ERROR, "XhcBulkTransfer: failed to create URB\n"));\r
-    Status = EFI_OUT_OF_RESOURCES;\r
-    goto ON_EXIT;\r
-  }\r
-\r
-  Status = XhcExecTransfer (Xhc, FALSE, Urb, Timeout);\r
-\r
-  *TransferResult = Urb->Result;\r
-  *DataLength     = Urb->Completed;\r
-\r
-  if (Status == EFI_TIMEOUT) {\r
-    //\r
-    // The transfer timed out. Abort the transfer by dequeueing of the TD.\r
-    //\r
-    RecoveryStatus = XhcDequeueTrbFromEndpoint(Xhc, Urb);\r
-    if (EFI_ERROR(RecoveryStatus)) {\r
-      DEBUG((EFI_D_ERROR, "XhcBulkTransfer: XhcDequeueTrbFromEndpoint failed\n"));\r
-    }\r
-  } else {\r
-    if (*TransferResult == EFI_USB_NOERROR) {\r
-      Status = EFI_SUCCESS;\r
-    } else if (*TransferResult == EFI_USB_ERR_STALL) {\r
-      RecoveryStatus = XhcRecoverHaltedEndpoint(Xhc, Urb);\r
-      if (EFI_ERROR (RecoveryStatus)) {\r
-        DEBUG ((EFI_D_ERROR, "XhcBulkTransfer: XhcRecoverHaltedEndpoint failed\n"));\r
-      }\r
-      Status = EFI_DEVICE_ERROR;\r
-    }\r
-  }\r
-\r
-  Xhc->PciIo->Flush (Xhc->PciIo);\r
-  XhcFreeUrb (Xhc, Urb);\r
+  Status = XhcTransfer (\r
+             Xhc,\r
+             DeviceAddress,\r
+             EndPointAddress,\r
+             DeviceSpeed,\r
+             MaximumPacketLength,\r
+             XHC_BULK_TRANSFER,\r
+             NULL,\r
+             Data[0],\r
+             DataLength,\r
+             Timeout,\r
+             TransferResult\r
+             );\r
 \r
 ON_EXIT:\r
-\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "XhcBulkTransfer: error - %r, transfer - %x\n", Status, *TransferResult));\r
   }\r
@@ -1490,10 +1492,8 @@ XhcSyncInterruptTransfer (
   )\r
 {\r
   USB_XHCI_INSTANCE       *Xhc;\r
-  URB                     *Urb;\r
   UINT8                   SlotId;\r
   EFI_STATUS              Status;\r
-  EFI_STATUS              RecoveryStatus;\r
   EFI_TPL                 OldTpl;\r
 \r
   //\r
@@ -1534,53 +1534,19 @@ XhcSyncInterruptTransfer (
     goto ON_EXIT;\r
   }\r
 \r
-  Urb = XhcCreateUrb (\r
-          Xhc,\r
-          DeviceAddress,\r
-          EndPointAddress,\r
-          DeviceSpeed,\r
-          MaximumPacketLength,\r
-          XHC_INT_TRANSFER_SYNC,\r
-          NULL,\r
-          Data,\r
-          *DataLength,\r
-          NULL,\r
-          NULL\r
-          );\r
-\r
-  if (Urb == NULL) {\r
-    DEBUG ((EFI_D_ERROR, "XhcSyncInterruptTransfer: failed to create URB\n"));\r
-    Status = EFI_OUT_OF_RESOURCES;\r
-    goto ON_EXIT;\r
-  }\r
-\r
-  Status = XhcExecTransfer (Xhc, FALSE, Urb, Timeout);\r
-\r
-  *TransferResult = Urb->Result;\r
-  *DataLength     = Urb->Completed;\r
-\r
-  if (Status == EFI_TIMEOUT) {\r
-    //\r
-    // The transfer timed out. Abort the transfer by dequeueing of the TD.\r
-    //\r
-    RecoveryStatus = XhcDequeueTrbFromEndpoint(Xhc, Urb);\r
-    if (EFI_ERROR(RecoveryStatus)) {\r
-      DEBUG((EFI_D_ERROR, "XhcSyncInterruptTransfer: XhcDequeueTrbFromEndpoint failed\n"));\r
-    }\r
-  } else {\r
-    if (*TransferResult == EFI_USB_NOERROR) {\r
-      Status = EFI_SUCCESS;\r
-    } else if (*TransferResult == EFI_USB_ERR_STALL) {\r
-      RecoveryStatus = XhcRecoverHaltedEndpoint(Xhc, Urb);\r
-      if (EFI_ERROR (RecoveryStatus)) {\r
-        DEBUG ((EFI_D_ERROR, "XhcSyncInterruptTransfer: XhcRecoverHaltedEndpoint failed\n"));\r
-      }\r
-      Status = EFI_DEVICE_ERROR;\r
-    }\r
-  }\r
-\r
-  Xhc->PciIo->Flush (Xhc->PciIo);\r
-  XhcFreeUrb (Xhc, Urb);\r
+  Status = XhcTransfer (\r
+             Xhc,\r
+             DeviceAddress,\r
+             EndPointAddress,\r
+             DeviceSpeed,\r
+             MaximumPacketLength,\r
+             XHC_INT_TRANSFER_SYNC,\r
+             NULL,\r
+             Data,\r
+             DataLength,\r
+             Timeout,\r
+             TransferResult\r
+             );\r
 \r
 ON_EXIT:\r
   if (EFI_ERROR (Status)) {\r