}
 }
 
+/* Give back previous TD and move on to the next TD. */
+static void xhci_dequeue_td(struct xhci_hcd *xhci, struct xhci_td *td, struct xhci_ring *ring,
+                           u32 status)
+{
+       ring->dequeue = td->end_trb;
+       ring->deq_seg = td->end_seg;
+       inc_deq(xhci, ring);
+
+       xhci_td_cleanup(xhci, td, ring, status);
+}
 
 /* Complete the cancelled URBs we unlinked from td_list. */
 static void xhci_giveback_invalidated_tds(struct xhci_virt_ep *ep)
                break;
        }
 
-       /* Update ring dequeue pointer */
-       ep_ring->dequeue = td->end_trb;
-       ep_ring->deq_seg = td->end_seg;
-       inc_deq(xhci, ep_ring);
-
-       xhci_td_cleanup(xhci, td, ep_ring, td->status);
+       xhci_dequeue_td(xhci, td, ep_ring, td->status);
 }
 
 /* sum trb lengths from the first trb up to stop_trb, _excluding_ stop_trb */
        /* calc actual length */
        frame->actual_length = 0;
 
-       /* Update ring dequeue pointer */
-       ep->ring->dequeue = td->end_trb;
-       ep->ring->deq_seg = td->end_seg;
-       inc_deq(xhci, ep->ring);
-
-       xhci_td_cleanup(xhci, td, ep->ring, status);
+       xhci_dequeue_td(xhci, td, ep->ring, status);
 }
 
 /*
 
        if (td && td->error_mid_td && !trb_in_td(xhci, td, ep_trb_dma, false)) {
                xhci_dbg(xhci, "Missing TD completion event after mid TD error\n");
-               ep_ring->dequeue = td->end_trb;
-               ep_ring->deq_seg = td->end_seg;
-               inc_deq(xhci, ep_ring);
-               xhci_td_cleanup(xhci, td, ep_ring, td->status);
+               xhci_dequeue_td(xhci, td, ep_ring, td->status);
        }
 
        if (list_empty(&ep_ring->td_list)) {