]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c
MdeModulePkg/Xhci: Fill the 'interval' field for ISO endpoint context
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / XhciDxe / XhciSched.c
index dea5b1af1884b8426b3bc888c44dd4d790205cf1..58a2f984a9450a127e065f95fa8ec35580ef15c7 100644 (file)
@@ -2,7 +2,7 @@
 \r
   XHCI transfer scheduling routines.\r
 \r
-Copyright (c) 2011 - 2013, 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
@@ -295,7 +295,7 @@ XhcCreateTransferTrb (
       TrbStart->TrbCtrSetup.wValue        = Urb->Request->Value;\r
       TrbStart->TrbCtrSetup.wIndex        = Urb->Request->Index;\r
       TrbStart->TrbCtrSetup.wLength       = Urb->Request->Length;\r
-      TrbStart->TrbCtrSetup.Lenth         = 8;\r
+      TrbStart->TrbCtrSetup.Length        = 8;\r
       TrbStart->TrbCtrSetup.IntTarget     = 0;\r
       TrbStart->TrbCtrSetup.IOC           = 1;\r
       TrbStart->TrbCtrSetup.IDT           = 1;\r
@@ -321,7 +321,7 @@ XhcCreateTransferTrb (
         TrbStart = (TRB *)(UINTN)EPRing->RingEnqueue;\r
         TrbStart->TrbCtrData.TRBPtrLo  = XHC_LOW_32BIT(Urb->DataPhy);\r
         TrbStart->TrbCtrData.TRBPtrHi  = XHC_HIGH_32BIT(Urb->DataPhy);\r
-        TrbStart->TrbCtrData.Lenth     = (UINT32) Urb->DataLen;\r
+        TrbStart->TrbCtrData.Length    = (UINT32) Urb->DataLen;\r
         TrbStart->TrbCtrData.TDSize    = 0;\r
         TrbStart->TrbCtrData.IntTarget = 0;\r
         TrbStart->TrbCtrData.ISP       = 1;\r
@@ -387,7 +387,7 @@ XhcCreateTransferTrb (
         TrbStart = (TRB *)(UINTN)EPRing->RingEnqueue;\r
         TrbStart->TrbNormal.TRBPtrLo  = XHC_LOW_32BIT((UINT8 *) Urb->DataPhy + TotalLen);\r
         TrbStart->TrbNormal.TRBPtrHi  = XHC_HIGH_32BIT((UINT8 *) Urb->DataPhy + TotalLen);\r
-        TrbStart->TrbNormal.Lenth     = (UINT32) Len;\r
+        TrbStart->TrbNormal.Length    = (UINT32) Len;\r
         TrbStart->TrbNormal.TDSize    = 0;\r
         TrbStart->TrbNormal.IntTarget = 0;\r
         TrbStart->TrbNormal.ISP       = 1;\r
@@ -422,7 +422,7 @@ XhcCreateTransferTrb (
         TrbStart = (TRB *)(UINTN)EPRing->RingEnqueue;\r
         TrbStart->TrbNormal.TRBPtrLo  = XHC_LOW_32BIT((UINT8 *) Urb->DataPhy + TotalLen);\r
         TrbStart->TrbNormal.TRBPtrHi  = XHC_HIGH_32BIT((UINT8 *) Urb->DataPhy + TotalLen);\r
-        TrbStart->TrbNormal.Lenth     = (UINT32) Len;\r
+        TrbStart->TrbNormal.Length    = (UINT32) Len;\r
         TrbStart->TrbNormal.TDSize    = 0;\r
         TrbStart->TrbNormal.IntTarget = 0;\r
         TrbStart->TrbNormal.ISP       = 1;\r
@@ -525,6 +525,7 @@ XhcInitSched (
     ASSERT (ScratchEntry != NULL);\r
     Xhc->ScratchEntry = ScratchEntry;\r
 \r
+    ScratchPhy = 0;\r
     Status = UsbHcAllocateAlignedPages (\r
                Xhc->PciIo,\r
                EFI_SIZE_TO_PAGES (MaxScratchpadBufs * sizeof (UINT64)),\r
@@ -542,6 +543,7 @@ XhcInitSched (
     // Allocate each scratch buffer\r
     //\r
     for (Index = 0; Index < MaxScratchpadBufs; Index++) {\r
+      ScratchEntryPhy = 0;\r
       Status = UsbHcAllocateAlignedPages (\r
                  Xhc->PciIo,\r
                  EFI_SIZE_TO_PAGES (Xhc->PageSize),\r
@@ -643,12 +645,8 @@ XhcRecoverHaltedEndpoint (
   )\r
 {\r
   EFI_STATUS                  Status;\r
-  EVT_TRB_COMMAND_COMPLETION  *EvtTrb;\r
-  CMD_TRB_RESET_ENDPOINT      CmdTrbResetED;\r
-  CMD_SET_TR_DEQ_POINTER      CmdSetTRDeq;\r
   UINT8                       Dci;\r
   UINT8                       SlotId;\r
-  EFI_PHYSICAL_ADDRESS        PhyAddr;\r
 \r
   Status = EFI_SUCCESS;\r
   SlotId = XhcBusDevAddrToSlotId (Xhc, Urb->Ep.BusAddr);\r
@@ -663,17 +661,7 @@ XhcRecoverHaltedEndpoint (
   //\r
   // 1) Send Reset endpoint command to transit from halt to stop state\r
   //\r
-  ZeroMem (&CmdTrbResetED, sizeof (CmdTrbResetED));\r
-  CmdTrbResetED.CycleBit = 1;\r
-  CmdTrbResetED.Type     = TRB_TYPE_RESET_ENDPOINT;\r
-  CmdTrbResetED.EDID     = Dci;\r
-  CmdTrbResetED.SlotId   = SlotId;\r
-  Status = XhcCmdTransfer (\r
-             Xhc,\r
-             (TRB_TEMPLATE *) (UINTN) &CmdTrbResetED,\r
-             XHC_GENERIC_TIMEOUT,\r
-             (TRB_TEMPLATE **) (UINTN) &EvtTrb\r
-             );\r
+  Status = XhcResetEndpoint(Xhc, SlotId, Dci);\r
   if (EFI_ERROR(Status)) {\r
     DEBUG ((EFI_D_ERROR, "XhcRecoverHaltedEndpoint: Reset Endpoint Failed, Status = %r\n", Status));\r
     goto Done;\r
@@ -682,22 +670,70 @@ XhcRecoverHaltedEndpoint (
   //\r
   // 2)Set dequeue pointer\r
   //\r
-  ZeroMem (&CmdSetTRDeq, sizeof (CmdSetTRDeq));\r
-  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, Urb->Ring->RingEnqueue, sizeof (CMD_SET_TR_DEQ_POINTER));\r
-  CmdSetTRDeq.PtrLo    = XHC_LOW_32BIT (PhyAddr) | Urb->Ring->RingPCS;\r
-  CmdSetTRDeq.PtrHi    = XHC_HIGH_32BIT (PhyAddr);\r
-  CmdSetTRDeq.CycleBit = 1;\r
-  CmdSetTRDeq.Type     = TRB_TYPE_SET_TR_DEQUE;\r
-  CmdSetTRDeq.Endpoint = Dci;\r
-  CmdSetTRDeq.SlotId   = SlotId;\r
-  Status = XhcCmdTransfer (\r
-             Xhc,\r
-             (TRB_TEMPLATE *) (UINTN) &CmdSetTRDeq,\r
-             XHC_GENERIC_TIMEOUT,\r
-             (TRB_TEMPLATE **) (UINTN) &EvtTrb\r
-             );\r
+  Status = XhcSetTrDequeuePointer(Xhc, SlotId, Dci, Urb);\r
+  if (EFI_ERROR(Status)) {\r
+    DEBUG ((EFI_D_ERROR, "XhcRecoverHaltedEndpoint: Set Transfer Ring Dequeue Pointer Failed, Status = %r\n", Status));\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // 3)Ring the doorbell to transit from stop to active\r
+  //\r
+  XhcRingDoorBell (Xhc, SlotId, Dci);\r
+\r
+Done:\r
+  return Status;\r
+}\r
+\r
+/**\r
+  System software shall use a Stop Endpoint Command (section 4.6.9) and the Set TR Dequeue Pointer\r
+  Command (section 4.6.10) to remove the timed-out TDs from the xHC transfer ring. The next write to\r
+  the Doorbell of the Endpoint will transition the Endpoint Context from the Stopped to the Running\r
+  state.\r
+\r
+  @param  Xhc                   The XHCI Instance.\r
+  @param  Urb                   The urb which doesn't get completed in a specified timeout range.\r
+\r
+  @retval EFI_SUCCESS           The dequeuing of the TDs is successful.\r
+  @retval Others                Failed to stop the endpoint and dequeue the TDs.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+XhcDequeueTrbFromEndpoint (\r
+  IN  USB_XHCI_INSTANCE   *Xhc,\r
+  IN  URB                 *Urb\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  UINT8                       Dci;\r
+  UINT8                       SlotId;\r
+\r
+  Status = EFI_SUCCESS;\r
+  SlotId = XhcBusDevAddrToSlotId (Xhc, Urb->Ep.BusAddr);\r
+  if (SlotId == 0) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+  Dci = XhcEndpointToDci (Urb->Ep.EpAddr, (UINT8)(Urb->Ep.Direction));\r
+  ASSERT (Dci < 32);\r
+  \r
+  DEBUG ((EFI_D_INFO, "Stop Slot = %x,Dci = %x\n", SlotId, Dci));\r
+\r
+  //\r
+  // 1) Send Stop endpoint command to stop xHC from executing of the TDs on the endpoint\r
+  //\r
+  Status = XhcStopEndpoint(Xhc, SlotId, Dci);\r
+  if (EFI_ERROR(Status)) {\r
+    DEBUG ((EFI_D_ERROR, "XhcDequeueTrbFromEndpoint: Stop Endpoint Failed, Status = %r\n", Status));\r
+    goto Done;\r
+  }\r
+\r
+  //\r
+  // 2)Set dequeue pointer\r
+  //\r
+  Status = XhcSetTrDequeuePointer(Xhc, SlotId, Dci, Urb);\r
   if (EFI_ERROR(Status)) {\r
-    DEBUG ((EFI_D_ERROR, "XhcRecoverHaltedEndpoint: Set Dequeue Pointer Failed, Status = %r\n", Status));\r
+    DEBUG ((EFI_D_ERROR, "XhcDequeueTrbFromEndpoint: Set Transfer Ring Dequeue Pointer Failed, Status = %r\n", Status));\r
     goto Done;\r
   }\r
 \r
@@ -750,7 +786,7 @@ CreateEventRing (
   //\r
   EventRing->EventRingCCS = 1;\r
 \r
-  Size = EFI_SIZE_TO_PAGES (sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBER);\r
+  Size = sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBER;\r
   Buf = UsbHcAllocateMem (Xhc->MemPool, Size);\r
   ASSERT (Buf != NULL);\r
   ASSERT (((UINTN) Buf & 0x3F) == 0);\r
@@ -973,7 +1009,11 @@ IsAsyncIntTrb (
         return TRUE;\r
       }\r
       CheckedTrb++;\r
-      if ((UINTN)CheckedTrb >= ((UINTN) CheckedUrb->Ring->RingSeg0 + sizeof (TRB_TEMPLATE) * CheckedUrb->Ring->TrbNumber)) {\r
+      //\r
+      // If the checked TRB is the link TRB at the end of the transfer ring,\r
+      // recircle it to the head of the ring.\r
+      //\r
+      if (CheckedTrb->Type == TRB_TYPE_LINK) {\r
         CheckedTrb = (TRB_TEMPLATE*) CheckedUrb->Ring->RingSeg0;\r
       }\r
     }\r
@@ -1025,7 +1065,7 @@ IsTransferRingTrb (
   @return Whether the result of URB transfer is finialized.\r
 \r
 **/\r
-EFI_STATUS\r
+BOOLEAN\r
 XhcCheckUrbResult (\r
   IN  USB_XHCI_INSTANCE   *Xhc,\r
   IN  URB                 *Urb\r
@@ -1056,7 +1096,6 @@ XhcCheckUrbResult (
 \r
   if (XhcIsHalt (Xhc) || XhcIsSysError (Xhc)) {\r
     Urb->Result |= EFI_USB_ERR_SYSTEM;\r
-    Status       = EFI_DEVICE_ERROR;\r
     goto EXIT;\r
   }\r
 \r
@@ -1105,37 +1144,37 @@ XhcCheckUrbResult (
         CheckedUrb->Result  |= EFI_USB_ERR_STALL;\r
         CheckedUrb->Finished = TRUE;\r
         DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: STALL_ERROR! Completecode = %x\n",EvtTrb->Completecode));\r
-        break;\r
+        goto EXIT;\r
 \r
       case TRB_COMPLETION_BABBLE_ERROR:\r
         CheckedUrb->Result  |= EFI_USB_ERR_BABBLE;\r
         CheckedUrb->Finished = TRUE;\r
         DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: BABBLE_ERROR! Completecode = %x\n",EvtTrb->Completecode));\r
-        break;\r
+        goto EXIT;\r
 \r
       case TRB_COMPLETION_DATA_BUFFER_ERROR:\r
         CheckedUrb->Result  |= EFI_USB_ERR_BUFFER;\r
         CheckedUrb->Finished = TRUE;\r
         DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: ERR_BUFFER! Completecode = %x\n",EvtTrb->Completecode));\r
-        break;\r
+        goto EXIT;\r
 \r
       case TRB_COMPLETION_USB_TRANSACTION_ERROR:\r
         CheckedUrb->Result  |= EFI_USB_ERR_TIMEOUT;\r
         CheckedUrb->Finished = TRUE;\r
         DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: TRANSACTION_ERROR! Completecode = %x\n",EvtTrb->Completecode));\r
-        break;\r
+        goto EXIT;\r
 \r
       case TRB_COMPLETION_SHORT_PACKET:\r
       case TRB_COMPLETION_SUCCESS:\r
         if (EvtTrb->Completecode == TRB_COMPLETION_SHORT_PACKET) {\r
-          DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: short packet happens!\n"));\r
+          DEBUG ((EFI_D_VERBOSE, "XhcCheckUrbResult: short packet happens!\n"));\r
         }\r
 \r
         TRBType = (UINT8) (TRBPtr->Type);\r
         if ((TRBType == TRB_TYPE_DATA_STAGE) ||\r
             (TRBType == TRB_TYPE_NORMAL) ||\r
             (TRBType == TRB_TYPE_ISOCH)) {\r
-          CheckedUrb->Completed += (CheckedUrb->DataLen - EvtTrb->Lenth);\r
+          CheckedUrb->Completed += (((TRANSFER_TRB_NORMAL*)TRBPtr)->Length - EvtTrb->Length);\r
         }\r
 \r
         break;\r
@@ -1144,7 +1183,7 @@ XhcCheckUrbResult (
         DEBUG ((EFI_D_ERROR, "Transfer Default Error Occur! Completecode = 0x%x!\n",EvtTrb->Completecode));\r
         CheckedUrb->Result  |= EFI_USB_ERR_TIMEOUT;\r
         CheckedUrb->Finished = TRUE;\r
-        break;\r
+        goto EXIT;\r
     }\r
 \r
     //\r
@@ -1187,7 +1226,7 @@ EXIT:
     XhcWriteRuntimeReg (Xhc, XHC_ERDP_OFFSET + 4, XHC_HIGH_32BIT (PhyAddr));\r
   }\r
 \r
-  return Status;\r
+  return Urb->Finished;\r
 }\r
 \r
 \r
@@ -1214,9 +1253,10 @@ XhcExecTransfer (
 {\r
   EFI_STATUS              Status;\r
   UINTN                   Index;\r
-  UINT                  Loop;\r
+  UINT64                  Loop;\r
   UINT8                   SlotId;\r
   UINT8                   Dci;\r
+  BOOLEAN                 Finished;\r
 \r
   if (CmdTransfer) {\r
     SlotId = 0;\r
@@ -1239,8 +1279,8 @@ XhcExecTransfer (
   XhcRingDoorBell (Xhc, SlotId, Dci);\r
 \r
   for (Index = 0; Index < Loop; Index++) {\r
-    Status = XhcCheckUrbResult (Xhc, Urb);\r
-    if (Urb->Finished) {\r
+    Finished = XhcCheckUrbResult (Xhc, Urb);\r
+    if (Finished) {\r
       break;\r
     }\r
     gBS->Stall (XHC_1_MICROSECOND);\r
@@ -1248,6 +1288,9 @@ XhcExecTransfer (
 \r
   if (Index == Loop) {\r
     Urb->Result = EFI_USB_ERR_TIMEOUT;\r
+    Status      = EFI_TIMEOUT;\r
+  } else if (Urb->Result != EFI_USB_NOERROR) {\r
+    Status      = EFI_DEVICE_ERROR;\r
   }\r
 \r
   return Status;\r
@@ -1535,6 +1578,10 @@ XhcPollPortStatusChange (
 \r
   Status = EFI_SUCCESS;\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
   if (ParentRouteChart.Dword == 0) {\r
     RouteChart.Route.RouteString = 0;\r
     RouteChart.Route.RootPortNum = Port + 1;\r
@@ -1549,6 +1596,15 @@ XhcPollPortStatusChange (
     RouteChart.Route.TierNum       = ParentRouteChart.Route.TierNum + 1;\r
   }\r
 \r
+  SlotId = XhcRouteStringToSlotId (Xhc, RouteChart);\r
+  if (SlotId != 0) {\r
+    if (Xhc->HcCParams.Data.Csz == 0) {\r
+      Status = XhcDisableSlotCmd (Xhc, SlotId);\r
+    } else {\r
+      Status = XhcDisableSlotCmd64 (Xhc, SlotId);\r
+    }\r
+  }\r
+\r
   if (((PortState->PortStatus & USB_PORT_STAT_ENABLE) != 0) &&\r
       ((PortState->PortStatus & USB_PORT_STAT_CONNECTION) != 0)) {\r
     //\r
@@ -1566,26 +1622,15 @@ XhcPollPortStatusChange (
     // Execute Enable_Slot cmd for attached device, initialize device context and assign device address.\r
     //\r
     SlotId = XhcRouteStringToSlotId (Xhc, RouteChart);\r
-    if (SlotId == 0) {\r
+    if ((SlotId == 0) && ((PortState->PortChangeStatus & USB_PORT_STAT_C_RESET) != 0)) {\r
       if (Xhc->HcCParams.Data.Csz == 0) {\r
         Status = XhcInitializeDeviceSlot (Xhc, ParentRouteChart, Port, RouteChart, Speed);\r
       } else {\r
         Status = XhcInitializeDeviceSlot64 (Xhc, ParentRouteChart, Port, RouteChart, Speed);\r
       }\r
     }\r
-  } else if ((PortState->PortStatus & USB_PORT_STAT_CONNECTION) == 0) {\r
-    //\r
-    // Device is detached. Disable the allocated device slot and release resource.\r
-    //\r
-    SlotId = XhcRouteStringToSlotId (Xhc, RouteChart);\r
-    if (SlotId != 0) {\r
-      if (Xhc->HcCParams.Data.Csz == 0) {\r
-        Status = XhcDisableSlotCmd (Xhc, SlotId);\r
-      } else {\r
-        Status = XhcDisableSlotCmd64 (Xhc, SlotId);\r
-      }\r
-    }\r
-  }\r
+  } \r
+\r
   return Status;\r
 }\r
 \r
@@ -2070,6 +2115,10 @@ XhcInitializeDeviceSlot (
   // 8) Issue an Address Device Command for the Device Slot, where the command points to the Input\r
   //    Context data structure described above.\r
   //\r
+  // Delay 10ms to meet TRSTRCY delay requirement in usb 2.0 spec chapter 7.1.7.5 before sending SetAddress() request\r
+  // to device.\r
+  //\r
+  gBS->Stall (XHC_RESET_RECOVERY_DELAY);\r
   ZeroMem (&CmdTrbAddr, sizeof (CmdTrbAddr));\r
   PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, Xhc->UsbDevContext[SlotId].InputContext, sizeof (INPUT_CONTEXT));\r
   CmdTrbAddr.PtrLo    = XHC_LOW_32BIT (PhyAddr);\r
@@ -2276,6 +2325,10 @@ XhcInitializeDeviceSlot64 (
   // 8) Issue an Address Device Command for the Device Slot, where the command points to the Input\r
   //    Context data structure described above.\r
   //\r
+  // Delay 10ms to meet TRSTRCY delay requirement in usb 2.0 spec chapter 7.1.7.5 before sending SetAddress() request\r
+  // to device.\r
+  //\r
+  gBS->Stall (XHC_RESET_RECOVERY_DELAY);\r
   ZeroMem (&CmdTrbAddr, sizeof (CmdTrbAddr));\r
   PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, Xhc->UsbDevContext[SlotId].InputContext, sizeof (INPUT_CONTEXT_64));\r
   CmdTrbAddr.PtrLo    = XHC_LOW_32BIT (PhyAddr);\r
@@ -2383,6 +2436,10 @@ XhcDisableSlotCmd (
     }\r
   }\r
 \r
+  if (Xhc->UsbDevContext[SlotId].ActiveAlternateSetting != NULL) {\r
+    FreePool (Xhc->UsbDevContext[SlotId].ActiveAlternateSetting);\r
+  }\r
+\r
   if (Xhc->UsbDevContext[SlotId].InputContext != NULL) {\r
     UsbHcFreeMem (Xhc->MemPool, Xhc->UsbDevContext[SlotId].InputContext, sizeof (INPUT_CONTEXT));\r
   }\r
@@ -2486,6 +2543,10 @@ XhcDisableSlotCmd64 (
     }\r
   }\r
 \r
+  if (Xhc->UsbDevContext[SlotId].ActiveAlternateSetting != NULL) {\r
+    FreePool (Xhc->UsbDevContext[SlotId].ActiveAlternateSetting);\r
+  }\r
+\r
   if (Xhc->UsbDevContext[SlotId].InputContext != NULL) {\r
     UsbHcFreeMem (Xhc->MemPool, Xhc->UsbDevContext[SlotId].InputContext, sizeof (INPUT_CONTEXT_64));\r
   }\r
@@ -2504,249 +2565,408 @@ XhcDisableSlotCmd64 (
   return Status;\r
 }\r
 \r
-\r
 /**\r
-  Configure all the device endpoints through XHCI's Configure_Endpoint cmd.\r
+  Initialize endpoint context in input context.\r
 \r
-  @param  Xhc           The XHCI Instance.\r
-  @param  SlotId        The slot id to be configured.\r
-  @param  DeviceSpeed   The device's speed.\r
-  @param  ConfigDesc    The pointer to the usb device configuration descriptor.\r
+  @param Xhc            The XHCI Instance.\r
+  @param SlotId         The slot id to be configured.\r
+  @param DeviceSpeed    The device's speed.\r
+  @param InputContext   The pointer to the input context.\r
+  @param IfDesc         The pointer to the usb device interface descriptor.\r
 \r
-  @retval EFI_SUCCESS   Successfully configure all the device endpoints.\r
+  @return The maximum device context index of endpoint.\r
 \r
 **/\r
-EFI_STATUS\r
+UINT8\r
 EFIAPI\r
-XhcSetConfigCmd (\r
-  IN USB_XHCI_INSTANCE        *Xhc,\r
-  IN UINT8                    SlotId,\r
-  IN UINT8                    DeviceSpeed,\r
-  IN USB_CONFIG_DESCRIPTOR    *ConfigDesc\r
+XhcInitializeEndpointContext (\r
+  IN USB_XHCI_INSTANCE          *Xhc,\r
+  IN UINT8                      SlotId,\r
+  IN UINT8                      DeviceSpeed,\r
+  IN INPUT_CONTEXT              *InputContext,\r
+  IN USB_INTERFACE_DESCRIPTOR   *IfDesc\r
   )\r
 {\r
-  EFI_STATUS                  Status;\r
-  USB_INTERFACE_DESCRIPTOR    *IfDesc;\r
-  USB_ENDPOINT_DESCRIPTOR     *EpDesc;\r
-  UINT8                       Index;\r
-  UINTN                       NumEp;\r
-  UINTN                       EpIndex;\r
-  UINT8                       EpAddr;\r
-  UINT8                       Direction;\r
-  UINT8                       Dci;\r
-  UINT8                       MaxDci;\r
-  EFI_PHYSICAL_ADDRESS        PhyAddr;\r
-  UINT8                       Interval;\r
+  USB_ENDPOINT_DESCRIPTOR       *EpDesc;\r
+  UINTN                         NumEp;\r
+  UINTN                         EpIndex;\r
+  UINT8                         EpAddr;\r
+  UINT8                         Direction;\r
+  UINT8                         Dci;\r
+  UINT8                         MaxDci;\r
+  EFI_PHYSICAL_ADDRESS          PhyAddr;\r
+  UINT8                         Interval;\r
+  TRANSFER_RING                 *EndpointTransferRing;\r
 \r
-  TRANSFER_RING               *EndpointTransferRing;\r
-  CMD_TRB_CONFIG_ENDPOINT     CmdTrbCfgEP;\r
-  INPUT_CONTEXT               *InputContext;\r
-  DEVICE_CONTEXT              *OutputContext;\r
-  EVT_TRB_COMMAND_COMPLETION  *EvtTrb;\r
-  //\r
-  // 4.6.6 Configure Endpoint\r
-  //\r
-  InputContext  = Xhc->UsbDevContext[SlotId].InputContext;\r
-  OutputContext = Xhc->UsbDevContext[SlotId].OutputContext;\r
-  ZeroMem (InputContext, sizeof (INPUT_CONTEXT));\r
-  CopyMem (&InputContext->Slot, &OutputContext->Slot, sizeof (SLOT_CONTEXT));\r
+  MaxDci = 0;\r
 \r
-  ASSERT (ConfigDesc != NULL);\r
+  NumEp = IfDesc->NumEndpoints;\r
 \r
-  MaxDci = 0;\r
+  EpDesc = (USB_ENDPOINT_DESCRIPTOR *)(IfDesc + 1);\r
+  for (EpIndex = 0; EpIndex < NumEp; EpIndex++) {\r
+    while (EpDesc->DescriptorType != USB_DESC_TYPE_ENDPOINT) {\r
+      EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+    }\r
 \r
-  IfDesc = (USB_INTERFACE_DESCRIPTOR *)(ConfigDesc + 1);\r
-  for (Index = 0; Index < ConfigDesc->NumInterfaces; Index++) {\r
-    while (IfDesc->DescriptorType != USB_DESC_TYPE_INTERFACE) {\r
-      IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);\r
+    if (EpDesc->Length < sizeof (USB_ENDPOINT_DESCRIPTOR)) {\r
+      EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+      continue;\r
     }\r
 \r
-    NumEp = IfDesc->NumEndpoints;\r
+    EpAddr    = (UINT8)(EpDesc->EndpointAddress & 0x0F);\r
+    Direction = (UINT8)((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);\r
 \r
-    EpDesc = (USB_ENDPOINT_DESCRIPTOR *)(IfDesc + 1);\r
-    for (EpIndex = 0; EpIndex < NumEp; EpIndex++) {\r
-      while (EpDesc->DescriptorType != USB_DESC_TYPE_ENDPOINT) {\r
-        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
-      }\r
+    Dci = XhcEndpointToDci (EpAddr, Direction);\r
+    ASSERT (Dci < 32);\r
+    if (Dci > MaxDci) {\r
+      MaxDci = Dci;\r
+    }\r
 \r
-      EpAddr    = (UINT8)(EpDesc->EndpointAddress & 0x0F);\r
-      Direction = (UINT8)((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);\r
+    InputContext->InputControlContext.Dword2 |= (BIT0 << Dci);\r
+    InputContext->EP[Dci-1].MaxPacketSize     = EpDesc->MaxPacketSize;\r
 \r
-      Dci = XhcEndpointToDci (EpAddr, Direction);\r
-      ASSERT (Dci < 32);\r
-      if (Dci > MaxDci) {\r
-        MaxDci = Dci;\r
-      }\r
+    if (DeviceSpeed == EFI_USB_SPEED_SUPER) {\r
+      //\r
+      // 6.2.3.4, shall be set to the value defined in the bMaxBurst field of the SuperSpeed Endpoint Companion Descriptor.\r
+      //\r
+      InputContext->EP[Dci-1].MaxBurstSize = 0x0;\r
+    } else {\r
+      InputContext->EP[Dci-1].MaxBurstSize = 0x0;\r
+    }\r
+\r
+    switch (EpDesc->Attributes & USB_ENDPOINT_TYPE_MASK) {\r
+      case USB_ENDPOINT_BULK:\r
+        if (Direction == EfiUsbDataIn) {\r
+          InputContext->EP[Dci-1].CErr   = 3;\r
+          InputContext->EP[Dci-1].EPType = ED_BULK_IN;\r
+        } else {\r
+          InputContext->EP[Dci-1].CErr   = 3;\r
+          InputContext->EP[Dci-1].EPType = ED_BULK_OUT;\r
+        }\r
 \r
-      InputContext->InputControlContext.Dword2 |= (BIT0 << Dci);\r
-      InputContext->EP[Dci-1].MaxPacketSize     = EpDesc->MaxPacketSize;\r
+        InputContext->EP[Dci-1].AverageTRBLength = 0x1000;\r
+        if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {\r
+          EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));\r
+          Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;\r
+          CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);\r
+        }\r
 \r
-      if (DeviceSpeed == EFI_USB_SPEED_SUPER) {\r
+        break;\r
+      case USB_ENDPOINT_ISO:\r
+        if (Direction == EfiUsbDataIn) {\r
+          InputContext->EP[Dci-1].CErr   = 0;\r
+          InputContext->EP[Dci-1].EPType = ED_ISOCH_IN;\r
+        } else {\r
+          InputContext->EP[Dci-1].CErr   = 0;\r
+          InputContext->EP[Dci-1].EPType = ED_ISOCH_OUT;\r
+        }\r
         //\r
-        // 6.2.3.4, shall be set to the value defined in the bMaxBurst field of the SuperSpeed Endpoint Companion Descriptor.\r
+        // Get the bInterval from descriptor and init the the interval field of endpoint context.\r
+        // Refer to XHCI 1.1 spec section 6.2.3.6.\r
         //\r
-        InputContext->EP[Dci-1].MaxBurstSize = 0x0;\r
-      } else {\r
-        InputContext->EP[Dci-1].MaxBurstSize = 0x0;\r
-      }\r
-\r
-      switch (EpDesc->Attributes & USB_ENDPOINT_TYPE_MASK) {\r
-        case USB_ENDPOINT_BULK:\r
-          if (Direction == EfiUsbDataIn) {\r
-            InputContext->EP[Dci-1].CErr   = 3;\r
-            InputContext->EP[Dci-1].EPType = ED_BULK_IN;\r
-          } else {\r
-            InputContext->EP[Dci-1].CErr   = 3;\r
-            InputContext->EP[Dci-1].EPType = ED_BULK_OUT;\r
-          }\r
+        if (DeviceSpeed == EFI_USB_SPEED_FULL) {\r
+          Interval = EpDesc->Interval;\r
+          ASSERT (Interval >= 1 && Interval <= 16);\r
+          InputContext->EP[Dci-1].Interval = Interval + 2;\r
+        } else if ((DeviceSpeed == EFI_USB_SPEED_HIGH) || (DeviceSpeed == EFI_USB_SPEED_SUPER)) {\r
+          Interval = EpDesc->Interval;\r
+          ASSERT (Interval >= 1 && Interval <= 16);\r
+          InputContext->EP[Dci-1].Interval = Interval - 1;\r
+        }\r
 \r
-          InputContext->EP[Dci-1].AverageTRBLength = 0x1000;\r
-          if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {\r
-            EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));\r
-            Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;\r
-            CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);\r
-          }\r
-\r
-          break;\r
-        case USB_ENDPOINT_ISO:\r
-          if (Direction == EfiUsbDataIn) {\r
-            InputContext->EP[Dci-1].CErr   = 0;\r
-            InputContext->EP[Dci-1].EPType = ED_ISOCH_IN;\r
-          } else {\r
-            InputContext->EP[Dci-1].CErr   = 0;\r
-            InputContext->EP[Dci-1].EPType = ED_ISOCH_OUT;\r
-          }\r
-          break;\r
-        case USB_ENDPOINT_INTERRUPT:\r
-          if (Direction == EfiUsbDataIn) {\r
-            InputContext->EP[Dci-1].CErr   = 3;\r
-            InputContext->EP[Dci-1].EPType = ED_INTERRUPT_IN;\r
-          } else {\r
-            InputContext->EP[Dci-1].CErr   = 3;\r
-            InputContext->EP[Dci-1].EPType = ED_INTERRUPT_OUT;\r
-          }\r
-          InputContext->EP[Dci-1].AverageTRBLength = 0x1000;\r
-          InputContext->EP[Dci-1].MaxESITPayload   = EpDesc->MaxPacketSize;\r
+        //\r
+        // Do not support isochronous transfer now.\r
+        //\r
+        DEBUG ((EFI_D_INFO, "XhcInitializeEndpointContext: Unsupport ISO EP found, Transfer ring is not allocated.\n"));\r
+        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+        continue;\r
+      case USB_ENDPOINT_INTERRUPT:\r
+        if (Direction == EfiUsbDataIn) {\r
+          InputContext->EP[Dci-1].CErr   = 3;\r
+          InputContext->EP[Dci-1].EPType = ED_INTERRUPT_IN;\r
+        } else {\r
+          InputContext->EP[Dci-1].CErr   = 3;\r
+          InputContext->EP[Dci-1].EPType = ED_INTERRUPT_OUT;\r
+        }\r
+        InputContext->EP[Dci-1].AverageTRBLength = 0x1000;\r
+        InputContext->EP[Dci-1].MaxESITPayload   = EpDesc->MaxPacketSize;\r
+        //\r
+        // Get the bInterval from descriptor and init the the interval field of endpoint context\r
+        //\r
+        if ((DeviceSpeed == EFI_USB_SPEED_FULL) || (DeviceSpeed == EFI_USB_SPEED_LOW)) {\r
+          Interval = EpDesc->Interval;\r
           //\r
-          // Get the bInterval from descriptor and init the the interval field of endpoint context\r
+          // Calculate through the bInterval field of Endpoint descriptor.\r
           //\r
-          if ((DeviceSpeed == EFI_USB_SPEED_FULL) || (DeviceSpeed == EFI_USB_SPEED_LOW)) {\r
-            Interval = EpDesc->Interval;\r
-            //\r
-            // Hard code the interval to MAX first, need calculate through the bInterval field of Endpoint descriptor.\r
-            //\r
-            InputContext->EP[Dci-1].Interval = 6;\r
-          } else if ((DeviceSpeed == EFI_USB_SPEED_HIGH) || (DeviceSpeed == EFI_USB_SPEED_SUPER)) {\r
-            Interval = EpDesc->Interval;\r
-            ASSERT (Interval >= 1 && Interval <= 16);\r
-            //\r
-            // Refer to XHCI 1.0 spec section 6.2.3.6, table 61\r
-            //\r
-            InputContext->EP[Dci-1].Interval         = Interval - 1;\r
-            InputContext->EP[Dci-1].AverageTRBLength = 0x1000;\r
-            InputContext->EP[Dci-1].MaxESITPayload   = 0x0002;\r
-            InputContext->EP[Dci-1].MaxBurstSize     = 0x0;\r
-            InputContext->EP[Dci-1].CErr             = 3;\r
-          }\r
-\r
-          if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {\r
-            EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));\r
-            Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;\r
-            CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);\r
-          }\r
-          break;\r
-\r
-        case USB_ENDPOINT_CONTROL:\r
-        default:\r
-          ASSERT (0);\r
-          break;\r
-      }\r
+          ASSERT (Interval != 0);\r
+          InputContext->EP[Dci-1].Interval = (UINT32)HighBitSet32((UINT32)Interval) + 3;\r
+        } else if ((DeviceSpeed == EFI_USB_SPEED_HIGH) || (DeviceSpeed == EFI_USB_SPEED_SUPER)) {\r
+          Interval = EpDesc->Interval;\r
+          ASSERT (Interval >= 1 && Interval <= 16);\r
+          //\r
+          // Refer to XHCI 1.0 spec section 6.2.3.6, table 61\r
+          //\r
+          InputContext->EP[Dci-1].Interval         = Interval - 1;\r
+          InputContext->EP[Dci-1].AverageTRBLength = 0x1000;\r
+          InputContext->EP[Dci-1].MaxESITPayload   = 0x0002;\r
+          InputContext->EP[Dci-1].MaxBurstSize     = 0x0;\r
+          InputContext->EP[Dci-1].CErr             = 3;\r
+        }\r
 \r
-      PhyAddr = UsbHcGetPciAddrForHostAddr (\r
-                  Xhc->MemPool,\r
-                  ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingSeg0,\r
-                  sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER\r
-                  );\r
-      PhyAddr &= ~(0x0F);\r
-      PhyAddr |= ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingPCS;\r
-      InputContext->EP[Dci-1].PtrLo = XHC_LOW_32BIT (PhyAddr);\r
-      InputContext->EP[Dci-1].PtrHi = XHC_HIGH_32BIT (PhyAddr);\r
+        if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {\r
+          EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));\r
+          Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;\r
+          CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);\r
+        }\r
+        break;\r
 \r
-      EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+      case USB_ENDPOINT_CONTROL:\r
+        //\r
+        // Do not support control transfer now.\r
+        //\r
+        DEBUG ((EFI_D_INFO, "XhcInitializeEndpointContext: Unsupport Control EP found, Transfer ring is not allocated.\n"));\r
+      default:\r
+        DEBUG ((EFI_D_INFO, "XhcInitializeEndpointContext: Unknown EP found, Transfer ring is not allocated.\n"));\r
+        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+        continue;\r
     }\r
-    IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);\r
-  }\r
 \r
-  InputContext->InputControlContext.Dword2 |= BIT0;\r
-  InputContext->Slot.ContextEntries         = MaxDci;\r
-  //\r
-  // configure endpoint\r
-  //\r
-  ZeroMem (&CmdTrbCfgEP, sizeof (CmdTrbCfgEP));\r
-  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, InputContext, sizeof (INPUT_CONTEXT));\r
-  CmdTrbCfgEP.PtrLo    = XHC_LOW_32BIT (PhyAddr);\r
-  CmdTrbCfgEP.PtrHi    = XHC_HIGH_32BIT (PhyAddr);\r
-  CmdTrbCfgEP.CycleBit = 1;\r
-  CmdTrbCfgEP.Type     = TRB_TYPE_CON_ENDPOINT;\r
-  CmdTrbCfgEP.SlotId   = Xhc->UsbDevContext[SlotId].SlotId;\r
-  DEBUG ((EFI_D_INFO, "Configure Endpoint\n"));\r
-  Status = XhcCmdTransfer (\r
-             Xhc,\r
-             (TRB_TEMPLATE *) (UINTN) &CmdTrbCfgEP,\r
-             XHC_GENERIC_TIMEOUT,\r
-             (TRB_TEMPLATE **) (UINTN) &EvtTrb\r
-             );\r
-  if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "XhcSetConfigCmd: Config Endpoint Failed, Status = %r\n", Status));\r
+    PhyAddr = UsbHcGetPciAddrForHostAddr (\r
+                Xhc->MemPool,\r
+                ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingSeg0,\r
+                sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER\r
+                );\r
+    PhyAddr &= ~((EFI_PHYSICAL_ADDRESS)0x0F);\r
+    PhyAddr |= (EFI_PHYSICAL_ADDRESS)((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingPCS;\r
+    InputContext->EP[Dci-1].PtrLo = XHC_LOW_32BIT (PhyAddr);\r
+    InputContext->EP[Dci-1].PtrHi = XHC_HIGH_32BIT (PhyAddr);\r
+\r
+    EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
   }\r
-  return Status;\r
+\r
+  return MaxDci;\r
 }\r
 \r
 /**\r
-  Configure all the device endpoints through XHCI's Configure_Endpoint cmd.\r
+  Initialize endpoint context in input context.\r
 \r
-  @param  Xhc           The XHCI Instance.\r
-  @param  SlotId        The slot id to be configured.\r
-  @param  DeviceSpeed   The device's speed.\r
-  @param  ConfigDesc    The pointer to the usb device configuration descriptor.\r
+  @param Xhc            The XHCI Instance.\r
+  @param SlotId         The slot id to be configured.\r
+  @param DeviceSpeed    The device's speed.\r
+  @param InputContext   The pointer to the input context.\r
+  @param IfDesc         The pointer to the usb device interface descriptor.\r
 \r
-  @retval EFI_SUCCESS   Successfully configure all the device endpoints.\r
+  @return The maximum device context index of endpoint.\r
 \r
 **/\r
-EFI_STATUS\r
+UINT8\r
 EFIAPI\r
-XhcSetConfigCmd64 (\r
-  IN USB_XHCI_INSTANCE        *Xhc,\r
-  IN UINT8                    SlotId,\r
-  IN UINT8                    DeviceSpeed,\r
-  IN USB_CONFIG_DESCRIPTOR    *ConfigDesc\r
+XhcInitializeEndpointContext64 (\r
+  IN USB_XHCI_INSTANCE          *Xhc,\r
+  IN UINT8                      SlotId,\r
+  IN UINT8                      DeviceSpeed,\r
+  IN INPUT_CONTEXT_64           *InputContext,\r
+  IN USB_INTERFACE_DESCRIPTOR   *IfDesc\r
   )\r
 {\r
-  EFI_STATUS                  Status;\r
-  USB_INTERFACE_DESCRIPTOR    *IfDesc;\r
-  USB_ENDPOINT_DESCRIPTOR     *EpDesc;\r
-  UINT8                       Index;\r
-  UINTN                       NumEp;\r
-  UINTN                       EpIndex;\r
-  UINT8                       EpAddr;\r
-  UINT8                       Direction;\r
+  USB_ENDPOINT_DESCRIPTOR       *EpDesc;\r
+  UINTN                         NumEp;\r
+  UINTN                         EpIndex;\r
+  UINT8                         EpAddr;\r
+  UINT8                         Direction;\r
+  UINT8                         Dci;\r
+  UINT8                         MaxDci;\r
+  EFI_PHYSICAL_ADDRESS          PhyAddr;\r
+  UINT8                         Interval;\r
+  TRANSFER_RING                 *EndpointTransferRing;\r
+\r
+  MaxDci = 0;\r
+\r
+  NumEp = IfDesc->NumEndpoints;\r
+\r
+  EpDesc = (USB_ENDPOINT_DESCRIPTOR *)(IfDesc + 1);\r
+  for (EpIndex = 0; EpIndex < NumEp; EpIndex++) {\r
+    while (EpDesc->DescriptorType != USB_DESC_TYPE_ENDPOINT) {\r
+      EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+    }\r
+\r
+    if (EpDesc->Length < sizeof (USB_ENDPOINT_DESCRIPTOR)) {\r
+      EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+      continue;\r
+    }\r
+\r
+    EpAddr    = (UINT8)(EpDesc->EndpointAddress & 0x0F);\r
+    Direction = (UINT8)((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);\r
+\r
+    Dci = XhcEndpointToDci (EpAddr, Direction);\r
+    ASSERT (Dci < 32);\r
+    if (Dci > MaxDci) {\r
+      MaxDci = Dci;\r
+    }\r
+\r
+    InputContext->InputControlContext.Dword2 |= (BIT0 << Dci);\r
+    InputContext->EP[Dci-1].MaxPacketSize     = EpDesc->MaxPacketSize;\r
+\r
+    if (DeviceSpeed == EFI_USB_SPEED_SUPER) {\r
+      //\r
+      // 6.2.3.4, shall be set to the value defined in the bMaxBurst field of the SuperSpeed Endpoint Companion Descriptor.\r
+      //\r
+      InputContext->EP[Dci-1].MaxBurstSize = 0x0;\r
+    } else {\r
+      InputContext->EP[Dci-1].MaxBurstSize = 0x0;\r
+    }\r
+\r
+    switch (EpDesc->Attributes & USB_ENDPOINT_TYPE_MASK) {\r
+      case USB_ENDPOINT_BULK:\r
+        if (Direction == EfiUsbDataIn) {\r
+          InputContext->EP[Dci-1].CErr   = 3;\r
+          InputContext->EP[Dci-1].EPType = ED_BULK_IN;\r
+        } else {\r
+          InputContext->EP[Dci-1].CErr   = 3;\r
+          InputContext->EP[Dci-1].EPType = ED_BULK_OUT;\r
+        }\r
+\r
+        InputContext->EP[Dci-1].AverageTRBLength = 0x1000;\r
+        if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {\r
+          EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));\r
+          Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;\r
+          CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);\r
+        }\r
+\r
+        break;\r
+      case USB_ENDPOINT_ISO:\r
+        if (Direction == EfiUsbDataIn) {\r
+          InputContext->EP[Dci-1].CErr   = 0;\r
+          InputContext->EP[Dci-1].EPType = ED_ISOCH_IN;\r
+        } else {\r
+          InputContext->EP[Dci-1].CErr   = 0;\r
+          InputContext->EP[Dci-1].EPType = ED_ISOCH_OUT;\r
+        }\r
+        //\r
+        // Get the bInterval from descriptor and init the the interval field of endpoint context.\r
+        // Refer to XHCI 1.1 spec section 6.2.3.6.\r
+        //\r
+        if (DeviceSpeed == EFI_USB_SPEED_FULL) {\r
+          Interval = EpDesc->Interval;\r
+          ASSERT (Interval >= 1 && Interval <= 16);\r
+          InputContext->EP[Dci-1].Interval = Interval + 2;\r
+        } else if ((DeviceSpeed == EFI_USB_SPEED_HIGH) || (DeviceSpeed == EFI_USB_SPEED_SUPER)) {\r
+          Interval = EpDesc->Interval;\r
+          ASSERT (Interval >= 1 && Interval <= 16);\r
+          InputContext->EP[Dci-1].Interval = Interval - 1;\r
+        }\r
+\r
+        //\r
+        // Do not support isochronous transfer now.\r
+        //\r
+        DEBUG ((EFI_D_INFO, "XhcInitializeEndpointContext64: Unsupport ISO EP found, Transfer ring is not allocated.\n"));\r
+        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+        continue;\r
+      case USB_ENDPOINT_INTERRUPT:\r
+        if (Direction == EfiUsbDataIn) {\r
+          InputContext->EP[Dci-1].CErr   = 3;\r
+          InputContext->EP[Dci-1].EPType = ED_INTERRUPT_IN;\r
+        } else {\r
+          InputContext->EP[Dci-1].CErr   = 3;\r
+          InputContext->EP[Dci-1].EPType = ED_INTERRUPT_OUT;\r
+        }\r
+        InputContext->EP[Dci-1].AverageTRBLength = 0x1000;\r
+        InputContext->EP[Dci-1].MaxESITPayload   = EpDesc->MaxPacketSize;\r
+        //\r
+        // Get the bInterval from descriptor and init the the interval field of endpoint context\r
+        //\r
+        if ((DeviceSpeed == EFI_USB_SPEED_FULL) || (DeviceSpeed == EFI_USB_SPEED_LOW)) {\r
+          Interval = EpDesc->Interval;\r
+          //\r
+          // Calculate through the bInterval field of Endpoint descriptor.\r
+          //\r
+          ASSERT (Interval != 0);\r
+          InputContext->EP[Dci-1].Interval = (UINT32)HighBitSet32((UINT32)Interval) + 3;\r
+        } else if ((DeviceSpeed == EFI_USB_SPEED_HIGH) || (DeviceSpeed == EFI_USB_SPEED_SUPER)) {\r
+          Interval = EpDesc->Interval;\r
+          ASSERT (Interval >= 1 && Interval <= 16);\r
+          //\r
+          // Refer to XHCI 1.0 spec section 6.2.3.6, table 61\r
+          //\r
+          InputContext->EP[Dci-1].Interval         = Interval - 1;\r
+          InputContext->EP[Dci-1].AverageTRBLength = 0x1000;\r
+          InputContext->EP[Dci-1].MaxESITPayload   = 0x0002;\r
+          InputContext->EP[Dci-1].MaxBurstSize     = 0x0;\r
+          InputContext->EP[Dci-1].CErr             = 3;\r
+        }\r
+\r
+        if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {\r
+          EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));\r
+          Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;\r
+          CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);\r
+        }\r
+        break;\r
+\r
+      case USB_ENDPOINT_CONTROL:\r
+        //\r
+        // Do not support control transfer now.\r
+        //\r
+        DEBUG ((EFI_D_INFO, "XhcInitializeEndpointContext64: Unsupport Control EP found, Transfer ring is not allocated.\n"));\r
+      default:\r
+        DEBUG ((EFI_D_INFO, "XhcInitializeEndpointContext64: Unknown EP found, Transfer ring is not allocated.\n"));\r
+        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+        continue;\r
+    }\r
+\r
+    PhyAddr = UsbHcGetPciAddrForHostAddr (\r
+                Xhc->MemPool,\r
+                ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingSeg0,\r
+                sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER\r
+                );\r
+    PhyAddr &= ~((EFI_PHYSICAL_ADDRESS)0x0F);\r
+    PhyAddr |= (EFI_PHYSICAL_ADDRESS)((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingPCS;\r
+    InputContext->EP[Dci-1].PtrLo = XHC_LOW_32BIT (PhyAddr);\r
+    InputContext->EP[Dci-1].PtrHi = XHC_HIGH_32BIT (PhyAddr);\r
+\r
+    EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+  }\r
+\r
+  return MaxDci;\r
+}\r
+\r
+/**\r
+  Configure all the device endpoints through XHCI's Configure_Endpoint cmd.\r
+\r
+  @param  Xhc           The XHCI Instance.\r
+  @param  SlotId        The slot id to be configured.\r
+  @param  DeviceSpeed   The device's speed.\r
+  @param  ConfigDesc    The pointer to the usb device configuration descriptor.\r
+\r
+  @retval EFI_SUCCESS   Successfully configure all the device endpoints.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+XhcSetConfigCmd (\r
+  IN USB_XHCI_INSTANCE        *Xhc,\r
+  IN UINT8                    SlotId,\r
+  IN UINT8                    DeviceSpeed,\r
+  IN USB_CONFIG_DESCRIPTOR    *ConfigDesc\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  USB_INTERFACE_DESCRIPTOR    *IfDesc;\r
+  UINT8                       Index;\r
   UINT8                       Dci;\r
   UINT8                       MaxDci;\r
   EFI_PHYSICAL_ADDRESS        PhyAddr;\r
-  UINT8                       Interval;\r
 \r
-  TRANSFER_RING               *EndpointTransferRing;\r
   CMD_TRB_CONFIG_ENDPOINT     CmdTrbCfgEP;\r
-  INPUT_CONTEXT_64            *InputContext;\r
-  DEVICE_CONTEXT_64           *OutputContext;\r
+  INPUT_CONTEXT               *InputContext;\r
+  DEVICE_CONTEXT              *OutputContext;\r
   EVT_TRB_COMMAND_COMPLETION  *EvtTrb;\r
   //\r
   // 4.6.6 Configure Endpoint\r
   //\r
   InputContext  = Xhc->UsbDevContext[SlotId].InputContext;\r
   OutputContext = Xhc->UsbDevContext[SlotId].OutputContext;\r
-  ZeroMem (InputContext, sizeof (INPUT_CONTEXT_64));\r
-  CopyMem (&InputContext->Slot, &OutputContext->Slot, sizeof (SLOT_CONTEXT_64));\r
+  ZeroMem (InputContext, sizeof (INPUT_CONTEXT));\r
+  CopyMem (&InputContext->Slot, &OutputContext->Slot, sizeof (SLOT_CONTEXT));\r
 \r
   ASSERT (ConfigDesc != NULL);\r
 \r
@@ -2754,125 +2974,110 @@ XhcSetConfigCmd64 (
 \r
   IfDesc = (USB_INTERFACE_DESCRIPTOR *)(ConfigDesc + 1);\r
   for (Index = 0; Index < ConfigDesc->NumInterfaces; Index++) {\r
-    while (IfDesc->DescriptorType != USB_DESC_TYPE_INTERFACE) {\r
+    while ((IfDesc->DescriptorType != USB_DESC_TYPE_INTERFACE) || (IfDesc->AlternateSetting != 0)) {\r
       IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);\r
     }\r
 \r
-    NumEp = IfDesc->NumEndpoints;\r
+    if (IfDesc->Length < sizeof (USB_INTERFACE_DESCRIPTOR)) {\r
+      IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);\r
+      continue;\r
+    }\r
 \r
-    EpDesc = (USB_ENDPOINT_DESCRIPTOR *)(IfDesc + 1);\r
-    for (EpIndex = 0; EpIndex < NumEp; EpIndex++) {\r
-      while (EpDesc->DescriptorType != USB_DESC_TYPE_ENDPOINT) {\r
-        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
-      }\r
+    Dci = XhcInitializeEndpointContext (Xhc, SlotId, DeviceSpeed, InputContext, IfDesc);\r
+    if (Dci > MaxDci) {\r
+      MaxDci = Dci;\r
+    }\r
 \r
-      EpAddr    = (UINT8)(EpDesc->EndpointAddress & 0x0F);\r
-      Direction = (UINT8)((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);\r
+    IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);\r
+  }\r
 \r
-      Dci = XhcEndpointToDci (EpAddr, Direction);\r
-      ASSERT (Dci < 32);\r
-      if (Dci > MaxDci) {\r
-        MaxDci = Dci;\r
-      }\r
+  InputContext->InputControlContext.Dword2 |= BIT0;\r
+  InputContext->Slot.ContextEntries         = MaxDci;\r
+  //\r
+  // configure endpoint\r
+  //\r
+  ZeroMem (&CmdTrbCfgEP, sizeof (CmdTrbCfgEP));\r
+  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, InputContext, sizeof (INPUT_CONTEXT));\r
+  CmdTrbCfgEP.PtrLo    = XHC_LOW_32BIT (PhyAddr);\r
+  CmdTrbCfgEP.PtrHi    = XHC_HIGH_32BIT (PhyAddr);\r
+  CmdTrbCfgEP.CycleBit = 1;\r
+  CmdTrbCfgEP.Type     = TRB_TYPE_CON_ENDPOINT;\r
+  CmdTrbCfgEP.SlotId   = Xhc->UsbDevContext[SlotId].SlotId;\r
+  DEBUG ((EFI_D_INFO, "Configure Endpoint\n"));\r
+  Status = XhcCmdTransfer (\r
+             Xhc,\r
+             (TRB_TEMPLATE *) (UINTN) &CmdTrbCfgEP,\r
+             XHC_GENERIC_TIMEOUT,\r
+             (TRB_TEMPLATE **) (UINTN) &EvtTrb\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "XhcSetConfigCmd: Config Endpoint Failed, Status = %r\n", Status));\r
+  } else {\r
+    Xhc->UsbDevContext[SlotId].ActiveConfiguration = ConfigDesc->ConfigurationValue;\r
+  }\r
+\r
+  return Status;\r
+}\r
 \r
-      InputContext->InputControlContext.Dword2 |= (BIT0 << Dci);\r
-      InputContext->EP[Dci-1].MaxPacketSize     = EpDesc->MaxPacketSize;\r
+/**\r
+  Configure all the device endpoints through XHCI's Configure_Endpoint cmd.\r
 \r
-      if (DeviceSpeed == EFI_USB_SPEED_SUPER) {\r
-        //\r
-        // 6.2.3.4, shall be set to the value defined in the bMaxBurst field of the SuperSpeed Endpoint Companion Descriptor.\r
-        //\r
-        InputContext->EP[Dci-1].MaxBurstSize = 0x0;\r
-      } else {\r
-        InputContext->EP[Dci-1].MaxBurstSize = 0x0;\r
-      }\r
+  @param  Xhc           The XHCI Instance.\r
+  @param  SlotId        The slot id to be configured.\r
+  @param  DeviceSpeed   The device's speed.\r
+  @param  ConfigDesc    The pointer to the usb device configuration descriptor.\r
 \r
-      switch (EpDesc->Attributes & USB_ENDPOINT_TYPE_MASK) {\r
-        case USB_ENDPOINT_BULK:\r
-          if (Direction == EfiUsbDataIn) {\r
-            InputContext->EP[Dci-1].CErr   = 3;\r
-            InputContext->EP[Dci-1].EPType = ED_BULK_IN;\r
-          } else {\r
-            InputContext->EP[Dci-1].CErr   = 3;\r
-            InputContext->EP[Dci-1].EPType = ED_BULK_OUT;\r
-          }\r
+  @retval EFI_SUCCESS   Successfully configure all the device endpoints.\r
 \r
-          InputContext->EP[Dci-1].AverageTRBLength = 0x1000;\r
-          if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {\r
-            EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));\r
-            Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;\r
-            CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);\r
-          }\r
-\r
-          break;\r
-        case USB_ENDPOINT_ISO:\r
-          if (Direction == EfiUsbDataIn) {\r
-            InputContext->EP[Dci-1].CErr   = 0;\r
-            InputContext->EP[Dci-1].EPType = ED_ISOCH_IN;\r
-          } else {\r
-            InputContext->EP[Dci-1].CErr   = 0;\r
-            InputContext->EP[Dci-1].EPType = ED_ISOCH_OUT;\r
-          }\r
-          break;\r
-        case USB_ENDPOINT_INTERRUPT:\r
-          if (Direction == EfiUsbDataIn) {\r
-            InputContext->EP[Dci-1].CErr   = 3;\r
-            InputContext->EP[Dci-1].EPType = ED_INTERRUPT_IN;\r
-          } else {\r
-            InputContext->EP[Dci-1].CErr   = 3;\r
-            InputContext->EP[Dci-1].EPType = ED_INTERRUPT_OUT;\r
-          }\r
-          InputContext->EP[Dci-1].AverageTRBLength = 0x1000;\r
-          InputContext->EP[Dci-1].MaxESITPayload   = EpDesc->MaxPacketSize;\r
-          //\r
-          // Get the bInterval from descriptor and init the the interval field of endpoint context\r
-          //\r
-          if ((DeviceSpeed == EFI_USB_SPEED_FULL) || (DeviceSpeed == EFI_USB_SPEED_LOW)) {\r
-            Interval = EpDesc->Interval;\r
-            //\r
-            // Hard code the interval to MAX first, need calculate through the bInterval field of Endpoint descriptor.\r
-            //\r
-            InputContext->EP[Dci-1].Interval = 6;\r
-          } else if ((DeviceSpeed == EFI_USB_SPEED_HIGH) || (DeviceSpeed == EFI_USB_SPEED_SUPER)) {\r
-            Interval = EpDesc->Interval;\r
-            ASSERT (Interval >= 1 && Interval <= 16);\r
-            //\r
-            // Refer to XHCI 1.0 spec section 6.2.3.6, table 61\r
-            //\r
-            InputContext->EP[Dci-1].Interval         = Interval - 1;\r
-            InputContext->EP[Dci-1].AverageTRBLength = 0x1000;\r
-            InputContext->EP[Dci-1].MaxESITPayload   = 0x0002;\r
-            InputContext->EP[Dci-1].MaxBurstSize     = 0x0;\r
-            InputContext->EP[Dci-1].CErr             = 3;\r
-          }\r
-\r
-          if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {\r
-            EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));\r
-            Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;\r
-            CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);\r
-          }\r
-          break;\r
-\r
-        case USB_ENDPOINT_CONTROL:\r
-        default:\r
-          ASSERT (0);\r
-          break;\r
-      }\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+XhcSetConfigCmd64 (\r
+  IN USB_XHCI_INSTANCE        *Xhc,\r
+  IN UINT8                    SlotId,\r
+  IN UINT8                    DeviceSpeed,\r
+  IN USB_CONFIG_DESCRIPTOR    *ConfigDesc\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  USB_INTERFACE_DESCRIPTOR    *IfDesc;\r
+  UINT8                       Index;\r
+  UINT8                       Dci;\r
+  UINT8                       MaxDci;\r
+  EFI_PHYSICAL_ADDRESS        PhyAddr;\r
+\r
+  CMD_TRB_CONFIG_ENDPOINT     CmdTrbCfgEP;\r
+  INPUT_CONTEXT_64            *InputContext;\r
+  DEVICE_CONTEXT_64           *OutputContext;\r
+  EVT_TRB_COMMAND_COMPLETION  *EvtTrb;\r
+  //\r
+  // 4.6.6 Configure Endpoint\r
+  //\r
+  InputContext  = Xhc->UsbDevContext[SlotId].InputContext;\r
+  OutputContext = Xhc->UsbDevContext[SlotId].OutputContext;\r
+  ZeroMem (InputContext, sizeof (INPUT_CONTEXT_64));\r
+  CopyMem (&InputContext->Slot, &OutputContext->Slot, sizeof (SLOT_CONTEXT_64));\r
 \r
-      PhyAddr = UsbHcGetPciAddrForHostAddr (\r
-                  Xhc->MemPool,\r
-                  ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingSeg0,\r
-                  sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER\r
-                  );\r
+  ASSERT (ConfigDesc != NULL);\r
 \r
-      PhyAddr &= ~(0x0F);\r
-      PhyAddr |= ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingPCS;\r
+  MaxDci = 0;\r
 \r
-      InputContext->EP[Dci-1].PtrLo = XHC_LOW_32BIT (PhyAddr);\r
-      InputContext->EP[Dci-1].PtrHi = XHC_HIGH_32BIT (PhyAddr);\r
+  IfDesc = (USB_INTERFACE_DESCRIPTOR *)(ConfigDesc + 1);\r
+  for (Index = 0; Index < ConfigDesc->NumInterfaces; Index++) {\r
+    while ((IfDesc->DescriptorType != USB_DESC_TYPE_INTERFACE) || (IfDesc->AlternateSetting != 0)) {\r
+      IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);\r
+    }\r
 \r
-      EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+    if (IfDesc->Length < sizeof (USB_INTERFACE_DESCRIPTOR)) {\r
+      IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);\r
+      continue;\r
+    }\r
+\r
+    Dci = XhcInitializeEndpointContext64 (Xhc, SlotId, DeviceSpeed, InputContext, IfDesc);\r
+    if (Dci > MaxDci) {\r
+      MaxDci = Dci;\r
     }\r
\r
     IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);\r
   }\r
 \r
@@ -2897,11 +3102,561 @@ XhcSetConfigCmd64 (
              );\r
   if (EFI_ERROR (Status)) {\r
     DEBUG ((EFI_D_ERROR, "XhcSetConfigCmd64: Config Endpoint Failed, Status = %r\n", Status));\r
+  } else {\r
+    Xhc->UsbDevContext[SlotId].ActiveConfiguration = ConfigDesc->ConfigurationValue;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Stop endpoint through XHCI's Stop_Endpoint cmd.\r
+\r
+  @param  Xhc                   The XHCI Instance.\r
+  @param  SlotId                The slot id to be configured.\r
+  @param  Dci                   The device context index of endpoint.\r
+\r
+  @retval EFI_SUCCESS           Stop endpoint successfully.\r
+  @retval Others                Failed to stop endpoint.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+XhcStopEndpoint (\r
+  IN USB_XHCI_INSTANCE      *Xhc,\r
+  IN UINT8                  SlotId,\r
+  IN UINT8                  Dci\r
+  )\r
+{\r
+  EFI_STATUS                    Status;\r
+  EVT_TRB_COMMAND_COMPLETION    *EvtTrb;\r
+  CMD_TRB_STOP_ENDPOINT         CmdTrbStopED;\r
+\r
+  DEBUG ((EFI_D_INFO, "XhcStopEndpoint: Slot = 0x%x, Dci = 0x%x\n", SlotId, Dci));\r
+\r
+  //\r
+  // Send stop endpoint command to transit Endpoint from running to stop state\r
+  //\r
+  ZeroMem (&CmdTrbStopED, sizeof (CmdTrbStopED));\r
+  CmdTrbStopED.CycleBit = 1;\r
+  CmdTrbStopED.Type     = TRB_TYPE_STOP_ENDPOINT;\r
+  CmdTrbStopED.EDID     = Dci;\r
+  CmdTrbStopED.SlotId   = SlotId;\r
+  Status = XhcCmdTransfer (\r
+             Xhc,\r
+             (TRB_TEMPLATE *) (UINTN) &CmdTrbStopED,\r
+             XHC_GENERIC_TIMEOUT,\r
+             (TRB_TEMPLATE **) (UINTN) &EvtTrb\r
+             );\r
+  if (EFI_ERROR(Status)) {\r
+    DEBUG ((EFI_D_ERROR, "XhcStopEndpoint: Stop Endpoint Failed, Status = %r\n", Status));\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Reset endpoint through XHCI's Reset_Endpoint cmd.\r
+\r
+  @param  Xhc                   The XHCI Instance.\r
+  @param  SlotId                The slot id to be configured.\r
+  @param  Dci                   The device context index of endpoint.\r
+\r
+  @retval EFI_SUCCESS           Reset endpoint successfully.\r
+  @retval Others                Failed to reset endpoint.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+XhcResetEndpoint (\r
+  IN USB_XHCI_INSTANCE      *Xhc,\r
+  IN UINT8                  SlotId,\r
+  IN UINT8                  Dci\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  EVT_TRB_COMMAND_COMPLETION  *EvtTrb;\r
+  CMD_TRB_RESET_ENDPOINT      CmdTrbResetED;\r
+\r
+  DEBUG ((EFI_D_INFO, "XhcResetEndpoint: Slot = 0x%x, Dci = 0x%x\n", SlotId, Dci));\r
+\r
+  //\r
+  // Send stop endpoint command to transit Endpoint from running to stop state\r
+  //\r
+  ZeroMem (&CmdTrbResetED, sizeof (CmdTrbResetED));\r
+  CmdTrbResetED.CycleBit = 1;\r
+  CmdTrbResetED.Type     = TRB_TYPE_RESET_ENDPOINT;\r
+  CmdTrbResetED.EDID     = Dci;\r
+  CmdTrbResetED.SlotId   = SlotId;\r
+  Status = XhcCmdTransfer (\r
+             Xhc,\r
+             (TRB_TEMPLATE *) (UINTN) &CmdTrbResetED,\r
+             XHC_GENERIC_TIMEOUT,\r
+             (TRB_TEMPLATE **) (UINTN) &EvtTrb\r
+             );\r
+  if (EFI_ERROR(Status)) {\r
+    DEBUG ((EFI_D_ERROR, "XhcResetEndpoint: Reset Endpoint Failed, Status = %r\n", Status));\r
   }\r
 \r
   return Status;\r
 }\r
 \r
+/**\r
+  Set transfer ring dequeue pointer through XHCI's Set_Tr_Dequeue_Pointer cmd.\r
+\r
+  @param  Xhc                   The XHCI Instance.\r
+  @param  SlotId                The slot id to be configured.\r
+  @param  Dci                   The device context index of endpoint.\r
+  @param  Urb                   The dequeue pointer of the transfer ring specified\r
+                                by the urb to be updated.\r
+\r
+  @retval EFI_SUCCESS           Set transfer ring dequeue pointer succeeds.\r
+  @retval Others                Failed to set transfer ring dequeue pointer.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+XhcSetTrDequeuePointer (\r
+  IN USB_XHCI_INSTANCE      *Xhc,\r
+  IN UINT8                  SlotId,\r
+  IN UINT8                  Dci,\r
+  IN URB                    *Urb\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  EVT_TRB_COMMAND_COMPLETION  *EvtTrb;\r
+  CMD_SET_TR_DEQ_POINTER      CmdSetTRDeq;\r
+  EFI_PHYSICAL_ADDRESS        PhyAddr;\r
+\r
+  DEBUG ((EFI_D_INFO, "XhcSetTrDequeuePointer: Slot = 0x%x, Dci = 0x%x, Urb = 0x%x\n", SlotId, Dci, Urb));\r
+\r
+  //\r
+  // Send stop endpoint command to transit Endpoint from running to stop state\r
+  //\r
+  ZeroMem (&CmdSetTRDeq, sizeof (CmdSetTRDeq));\r
+  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, Urb->Ring->RingEnqueue, sizeof (CMD_SET_TR_DEQ_POINTER));\r
+  CmdSetTRDeq.PtrLo    = XHC_LOW_32BIT (PhyAddr) | Urb->Ring->RingPCS;\r
+  CmdSetTRDeq.PtrHi    = XHC_HIGH_32BIT (PhyAddr);\r
+  CmdSetTRDeq.CycleBit = 1;\r
+  CmdSetTRDeq.Type     = TRB_TYPE_SET_TR_DEQUE;\r
+  CmdSetTRDeq.Endpoint = Dci;\r
+  CmdSetTRDeq.SlotId   = SlotId;\r
+  Status = XhcCmdTransfer (\r
+             Xhc,\r
+             (TRB_TEMPLATE *) (UINTN) &CmdSetTRDeq,\r
+             XHC_GENERIC_TIMEOUT,\r
+             (TRB_TEMPLATE **) (UINTN) &EvtTrb\r
+             );\r
+  if (EFI_ERROR(Status)) {\r
+    DEBUG ((EFI_D_ERROR, "XhcSetTrDequeuePointer: Set TR Dequeue Pointer Failed, Status = %r\n", Status));\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Set interface through XHCI's Configure_Endpoint cmd.\r
+\r
+  @param  Xhc           The XHCI Instance.\r
+  @param  SlotId        The slot id to be configured.\r
+  @param  DeviceSpeed   The device's speed.\r
+  @param  ConfigDesc    The pointer to the usb device configuration descriptor.\r
+  @param  Request       USB device request to send.\r
+\r
+  @retval EFI_SUCCESS   Successfully set interface.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+XhcSetInterface (\r
+  IN USB_XHCI_INSTANCE        *Xhc,\r
+  IN UINT8                    SlotId,\r
+  IN UINT8                    DeviceSpeed,\r
+  IN USB_CONFIG_DESCRIPTOR    *ConfigDesc,\r
+  IN EFI_USB_DEVICE_REQUEST   *Request\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  USB_INTERFACE_DESCRIPTOR    *IfDescActive;\r
+  USB_INTERFACE_DESCRIPTOR    *IfDescSet;\r
+  USB_INTERFACE_DESCRIPTOR    *IfDesc;\r
+  USB_ENDPOINT_DESCRIPTOR     *EpDesc;\r
+  UINTN                       NumEp;\r
+  UINTN                       EpIndex;\r
+  UINT8                       EpAddr;\r
+  UINT8                       Direction;\r
+  UINT8                       Dci;\r
+  UINT8                       MaxDci;\r
+  EFI_PHYSICAL_ADDRESS        PhyAddr;\r
+  VOID                        *RingSeg;\r
+\r
+  CMD_TRB_CONFIG_ENDPOINT     CmdTrbCfgEP;\r
+  INPUT_CONTEXT               *InputContext;\r
+  DEVICE_CONTEXT              *OutputContext;\r
+  EVT_TRB_COMMAND_COMPLETION  *EvtTrb;\r
+\r
+  Status = EFI_SUCCESS;\r
+\r
+  InputContext  = Xhc->UsbDevContext[SlotId].InputContext;\r
+  OutputContext = Xhc->UsbDevContext[SlotId].OutputContext;\r
+  //\r
+  // XHCI 4.6.6 Configure Endpoint\r
+  // When this command is used to "Set an Alternate Interface on a device", software shall set the Drop\r
+  // Context and Add Context flags as follows:\r
+  // 1) If an endpoint is not modified by the Alternate Interface setting, then software shall set the Drop\r
+  // Context and Add Context flags to '0'.\r
+  //\r
+  // Except the interface indicated by Reqeust->Index, no impact to other interfaces.\r
+  // So the default Drop Context and Add Context flags can be '0' to cover 1).\r
+  //\r
+  ZeroMem (InputContext, sizeof (INPUT_CONTEXT));\r
+  CopyMem (&InputContext->Slot, &OutputContext->Slot, sizeof (SLOT_CONTEXT));\r
+\r
+  ASSERT (ConfigDesc != NULL);\r
+\r
+  MaxDci = 0;\r
+\r
+  IfDescActive = NULL;\r
+  IfDescSet = NULL;\r
+\r
+  IfDesc = (USB_INTERFACE_DESCRIPTOR *)(ConfigDesc + 1);\r
+  while ((UINTN) IfDesc < ((UINTN) ConfigDesc + ConfigDesc->TotalLength)) {\r
+    if ((IfDesc->DescriptorType == USB_DESC_TYPE_INTERFACE) && (IfDesc->Length >= sizeof (USB_INTERFACE_DESCRIPTOR))) {\r
+      if (IfDesc->InterfaceNumber == (UINT8) Request->Index) {\r
+        if (IfDesc->AlternateSetting == Xhc->UsbDevContext[SlotId].ActiveAlternateSetting[IfDesc->InterfaceNumber]) {\r
+          //\r
+          // Find out the active interface descriptor.\r
+          //\r
+          IfDescActive = IfDesc;\r
+        } else if (IfDesc->AlternateSetting == (UINT8) Request->Value) {\r
+          //\r
+          // Find out the interface descriptor to set.\r
+          //\r
+          IfDescSet = IfDesc;\r
+        }\r
+      }\r
+    }\r
+    IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);\r
+  }\r
+\r
+  //\r
+  // XHCI 4.6.6 Configure Endpoint\r
+  // When this command is used to "Set an Alternate Interface on a device", software shall set the Drop\r
+  // Context and Add Context flags as follows:\r
+  // 2) If an endpoint previously disabled, is enabled by the Alternate Interface setting, then software shall set\r
+  // the Drop Context flag to '0' and Add Context flag to '1', and initialize the Input Endpoint Context.\r
+  // 3) If an endpoint previously enabled, is disabled by the Alternate Interface setting, then software shall set\r
+  // the Drop Context flag to '1' and Add Context flag to '0'.\r
+  // 4) If a parameter of an enabled endpoint is modified by an Alternate Interface setting, the Drop Context\r
+  // and Add Context flags shall be set to '1'.\r
+  //\r
+  // Below codes are to cover 2), 3) and 4).\r
+  //\r
+\r
+  if ((IfDescActive != NULL) && (IfDescSet != NULL)) {\r
+    NumEp = IfDescActive->NumEndpoints;\r
+    EpDesc = (USB_ENDPOINT_DESCRIPTOR *) (IfDescActive + 1);\r
+    for (EpIndex = 0; EpIndex < NumEp; EpIndex++) {\r
+      while (EpDesc->DescriptorType != USB_DESC_TYPE_ENDPOINT) {\r
+        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+      }\r
+\r
+      if (EpDesc->Length < sizeof (USB_ENDPOINT_DESCRIPTOR)) {\r
+        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+        continue;\r
+      }\r
+\r
+      EpAddr    = (UINT8) (EpDesc->EndpointAddress & 0x0F);\r
+      Direction = (UINT8) ((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);\r
+\r
+      Dci = XhcEndpointToDci (EpAddr, Direction);\r
+      ASSERT (Dci < 32);\r
+      if (Dci > MaxDci) {\r
+        MaxDci = Dci;\r
+      }\r
+      //\r
+      // XHCI 4.3.6 - Setting Alternate Interfaces\r
+      // 1) Stop any Running Transfer Rings affected by the Alternate Interface setting.\r
+      //\r
+      Status = XhcStopEndpoint (Xhc, SlotId, Dci);\r
+      if (EFI_ERROR (Status)) {\r
+        return Status;\r
+      }\r
+      //\r
+      // XHCI 4.3.6 - Setting Alternate Interfaces\r
+      // 2) Free Transfer Rings of all endpoints that will be affected by the Alternate Interface setting.\r
+      //\r
+      if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1] != NULL) {\r
+        RingSeg = ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1])->RingSeg0;\r
+        if (RingSeg != NULL) {\r
+          UsbHcFreeMem (Xhc->MemPool, RingSeg, sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER);\r
+        }\r
+        FreePool (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1]);\r
+        Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1] = NULL;\r
+      }\r
+\r
+      //\r
+      // Set the Drop Context flag to '1'.\r
+      //\r
+      InputContext->InputControlContext.Dword1 |= (BIT0 << Dci);\r
+\r
+      EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+    }\r
+\r
+    //\r
+    // XHCI 4.3.6 - Setting Alternate Interfaces\r
+    // 3) Clear all the Endpoint Context fields of each endpoint that will be disabled by the Alternate\r
+    // Interface setting, to '0'.\r
+    //\r
+    // The step 3) has been covered by the ZeroMem () to InputContext at the start of the function.\r
+    //\r
+\r
+    //\r
+    // XHCI 4.3.6 - Setting Alternate Interfaces\r
+    // 4) For each endpoint enabled by the Configure Endpoint Command:\r
+    //   a. Allocate a Transfer Ring.\r
+    //   b. Initialize the Transfer Ring Segment(s) by clearing all fields of all TRBs to '0'.\r
+    //   c. Initialize the Endpoint Context data structure.\r
+    //\r
+    Dci = XhcInitializeEndpointContext (Xhc, SlotId, DeviceSpeed, InputContext, IfDescSet);\r
+    if (Dci > MaxDci) {\r
+      MaxDci = Dci;\r
+    }\r
+\r
+    InputContext->InputControlContext.Dword2 |= BIT0;\r
+    InputContext->Slot.ContextEntries         = MaxDci;\r
+    //\r
+    // XHCI 4.3.6 - Setting Alternate Interfaces\r
+    // 5) Issue and successfully complete a Configure Endpoint Command.\r
+    //\r
+    ZeroMem (&CmdTrbCfgEP, sizeof (CmdTrbCfgEP));\r
+    PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, InputContext, sizeof (INPUT_CONTEXT));\r
+    CmdTrbCfgEP.PtrLo    = XHC_LOW_32BIT (PhyAddr);\r
+    CmdTrbCfgEP.PtrHi    = XHC_HIGH_32BIT (PhyAddr);\r
+    CmdTrbCfgEP.CycleBit = 1;\r
+    CmdTrbCfgEP.Type     = TRB_TYPE_CON_ENDPOINT;\r
+    CmdTrbCfgEP.SlotId   = Xhc->UsbDevContext[SlotId].SlotId;\r
+    DEBUG ((EFI_D_INFO, "SetInterface: Configure Endpoint\n"));\r
+    Status = XhcCmdTransfer (\r
+               Xhc,\r
+               (TRB_TEMPLATE *) (UINTN) &CmdTrbCfgEP,\r
+               XHC_GENERIC_TIMEOUT,\r
+               (TRB_TEMPLATE **) (UINTN) &EvtTrb\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "SetInterface: Config Endpoint Failed, Status = %r\n", Status));\r
+    } else {\r
+      //\r
+      // Update the active AlternateSetting.\r
+      //\r
+      Xhc->UsbDevContext[SlotId].ActiveAlternateSetting[(UINT8) Request->Index] = (UINT8) Request->Value;\r
+    }\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Set interface through XHCI's Configure_Endpoint cmd.\r
+\r
+  @param  Xhc           The XHCI Instance.\r
+  @param  SlotId        The slot id to be configured.\r
+  @param  DeviceSpeed   The device's speed.\r
+  @param  ConfigDesc    The pointer to the usb device configuration descriptor.\r
+  @param  Request       USB device request to send.\r
+\r
+  @retval EFI_SUCCESS   Successfully set interface.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+XhcSetInterface64 (\r
+  IN USB_XHCI_INSTANCE        *Xhc,\r
+  IN UINT8                    SlotId,\r
+  IN UINT8                    DeviceSpeed,\r
+  IN USB_CONFIG_DESCRIPTOR    *ConfigDesc,\r
+  IN EFI_USB_DEVICE_REQUEST   *Request\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  USB_INTERFACE_DESCRIPTOR    *IfDescActive;\r
+  USB_INTERFACE_DESCRIPTOR    *IfDescSet;\r
+  USB_INTERFACE_DESCRIPTOR    *IfDesc;\r
+  USB_ENDPOINT_DESCRIPTOR     *EpDesc;\r
+  UINTN                       NumEp;\r
+  UINTN                       EpIndex;\r
+  UINT8                       EpAddr;\r
+  UINT8                       Direction;\r
+  UINT8                       Dci;\r
+  UINT8                       MaxDci;\r
+  EFI_PHYSICAL_ADDRESS        PhyAddr;\r
+  VOID                        *RingSeg;\r
+\r
+  CMD_TRB_CONFIG_ENDPOINT     CmdTrbCfgEP;\r
+  INPUT_CONTEXT_64            *InputContext;\r
+  DEVICE_CONTEXT_64           *OutputContext;\r
+  EVT_TRB_COMMAND_COMPLETION  *EvtTrb;\r
+\r
+  Status = EFI_SUCCESS;\r
+\r
+  InputContext  = Xhc->UsbDevContext[SlotId].InputContext;\r
+  OutputContext = Xhc->UsbDevContext[SlotId].OutputContext;\r
+  //\r
+  // XHCI 4.6.6 Configure Endpoint\r
+  // When this command is used to "Set an Alternate Interface on a device", software shall set the Drop\r
+  // Context and Add Context flags as follows:\r
+  // 1) If an endpoint is not modified by the Alternate Interface setting, then software shall set the Drop\r
+  // Context and Add Context flags to '0'.\r
+  //\r
+  // Except the interface indicated by Reqeust->Index, no impact to other interfaces.\r
+  // So the default Drop Context and Add Context flags can be '0' to cover 1).\r
+  //\r
+  ZeroMem (InputContext, sizeof (INPUT_CONTEXT_64));\r
+  CopyMem (&InputContext->Slot, &OutputContext->Slot, sizeof (SLOT_CONTEXT_64));\r
+\r
+  ASSERT (ConfigDesc != NULL);\r
+\r
+  MaxDci = 0;\r
+\r
+  IfDescActive = NULL;\r
+  IfDescSet = NULL;\r
+\r
+  IfDesc = (USB_INTERFACE_DESCRIPTOR *)(ConfigDesc + 1);\r
+  while ((UINTN) IfDesc < ((UINTN) ConfigDesc + ConfigDesc->TotalLength)) {\r
+    if ((IfDesc->DescriptorType == USB_DESC_TYPE_INTERFACE) && (IfDesc->Length >= sizeof (USB_INTERFACE_DESCRIPTOR))) {\r
+      if (IfDesc->InterfaceNumber == (UINT8) Request->Index) {\r
+        if (IfDesc->AlternateSetting == Xhc->UsbDevContext[SlotId].ActiveAlternateSetting[IfDesc->InterfaceNumber]) {\r
+          //\r
+          // Find out the active interface descriptor.\r
+          //\r
+          IfDescActive = IfDesc;\r
+        } else if (IfDesc->AlternateSetting == (UINT8) Request->Value) {\r
+          //\r
+          // Find out the interface descriptor to set.\r
+          //\r
+          IfDescSet = IfDesc;\r
+        }\r
+      }\r
+    }\r
+    IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);\r
+  }\r
+\r
+  //\r
+  // XHCI 4.6.6 Configure Endpoint\r
+  // When this command is used to "Set an Alternate Interface on a device", software shall set the Drop\r
+  // Context and Add Context flags as follows:\r
+  // 2) If an endpoint previously disabled, is enabled by the Alternate Interface setting, then software shall set\r
+  // the Drop Context flag to '0' and Add Context flag to '1', and initialize the Input Endpoint Context.\r
+  // 3) If an endpoint previously enabled, is disabled by the Alternate Interface setting, then software shall set\r
+  // the Drop Context flag to '1' and Add Context flag to '0'.\r
+  // 4) If a parameter of an enabled endpoint is modified by an Alternate Interface setting, the Drop Context\r
+  // and Add Context flags shall be set to '1'.\r
+  //\r
+  // Below codes are to cover 2), 3) and 4).\r
+  //\r
+\r
+  if ((IfDescActive != NULL) && (IfDescSet != NULL)) {\r
+    NumEp = IfDescActive->NumEndpoints;\r
+    EpDesc = (USB_ENDPOINT_DESCRIPTOR *) (IfDescActive + 1);\r
+    for (EpIndex = 0; EpIndex < NumEp; EpIndex++) {\r
+      while (EpDesc->DescriptorType != USB_DESC_TYPE_ENDPOINT) {\r
+        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+      }\r
+\r
+      if (EpDesc->Length < sizeof (USB_ENDPOINT_DESCRIPTOR)) {\r
+        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+        continue;\r
+      }\r
+\r
+      EpAddr    = (UINT8) (EpDesc->EndpointAddress & 0x0F);\r
+      Direction = (UINT8) ((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);\r
+\r
+      Dci = XhcEndpointToDci (EpAddr, Direction);\r
+      ASSERT (Dci < 32);\r
+      if (Dci > MaxDci) {\r
+        MaxDci = Dci;\r
+      }\r
+      //\r
+      // XHCI 4.3.6 - Setting Alternate Interfaces\r
+      // 1) Stop any Running Transfer Rings affected by the Alternate Interface setting.\r
+      //\r
+      Status = XhcStopEndpoint (Xhc, SlotId, Dci);\r
+      if (EFI_ERROR (Status)) {\r
+        return Status;\r
+      }\r
+      //\r
+      // XHCI 4.3.6 - Setting Alternate Interfaces\r
+      // 2) Free Transfer Rings of all endpoints that will be affected by the Alternate Interface setting.\r
+      //\r
+      if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1] != NULL) {\r
+        RingSeg = ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1])->RingSeg0;\r
+        if (RingSeg != NULL) {\r
+          UsbHcFreeMem (Xhc->MemPool, RingSeg, sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER);\r
+        }\r
+        FreePool (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1]);\r
+        Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1] = NULL;\r
+      }\r
+\r
+      //\r
+      // Set the Drop Context flag to '1'.\r
+      //\r
+      InputContext->InputControlContext.Dword1 |= (BIT0 << Dci);\r
+\r
+      EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+    }\r
+\r
+    //\r
+    // XHCI 4.3.6 - Setting Alternate Interfaces\r
+    // 3) Clear all the Endpoint Context fields of each endpoint that will be disabled by the Alternate\r
+    // Interface setting, to '0'.\r
+    //\r
+    // The step 3) has been covered by the ZeroMem () to InputContext at the start of the function.\r
+    //\r
+\r
+    //\r
+    // XHCI 4.3.6 - Setting Alternate Interfaces\r
+    // 4) For each endpoint enabled by the Configure Endpoint Command:\r
+    //   a. Allocate a Transfer Ring.\r
+    //   b. Initialize the Transfer Ring Segment(s) by clearing all fields of all TRBs to '0'.\r
+    //   c. Initialize the Endpoint Context data structure.\r
+    //\r
+    Dci = XhcInitializeEndpointContext64 (Xhc, SlotId, DeviceSpeed, InputContext, IfDescSet);\r
+    if (Dci > MaxDci) {\r
+      MaxDci = Dci;\r
+    }\r
+\r
+    InputContext->InputControlContext.Dword2 |= BIT0;\r
+    InputContext->Slot.ContextEntries         = MaxDci;\r
+    //\r
+    // XHCI 4.3.6 - Setting Alternate Interfaces\r
+    // 5) Issue and successfully complete a Configure Endpoint Command.\r
+    //\r
+    ZeroMem (&CmdTrbCfgEP, sizeof (CmdTrbCfgEP));\r
+    PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, InputContext, sizeof (INPUT_CONTEXT_64));\r
+    CmdTrbCfgEP.PtrLo    = XHC_LOW_32BIT (PhyAddr);\r
+    CmdTrbCfgEP.PtrHi    = XHC_HIGH_32BIT (PhyAddr);\r
+    CmdTrbCfgEP.CycleBit = 1;\r
+    CmdTrbCfgEP.Type     = TRB_TYPE_CON_ENDPOINT;\r
+    CmdTrbCfgEP.SlotId   = Xhc->UsbDevContext[SlotId].SlotId;\r
+    DEBUG ((EFI_D_INFO, "SetInterface64: Configure Endpoint\n"));\r
+    Status = XhcCmdTransfer (\r
+               Xhc,\r
+               (TRB_TEMPLATE *) (UINTN) &CmdTrbCfgEP,\r
+               XHC_GENERIC_TIMEOUT,\r
+               (TRB_TEMPLATE **) (UINTN) &EvtTrb\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "SetInterface64: Config Endpoint Failed, Status = %r\n", Status));\r
+    } else {\r
+      //\r
+      // Update the active AlternateSetting.\r
+      //\r
+      Xhc->UsbDevContext[SlotId].ActiveAlternateSetting[(UINT8) Request->Index] = (UINT8) Request->Value;\r
+    }\r
+  }\r
+\r
+  return Status;\r
+}\r
 \r
 /**\r
   Evaluate the endpoint 0 context through XHCI's Evaluate_Context cmd.\r