From: John Youn <[email protected]>

If the trb->enqueue == trb->dequeue, then it could be full or empty.
This could also happen at TRB index 0, so modify the check to handle
that condition. At index 0, the previous TRB is the one just before the
link TRB.

Signed-off-by: John Youn <[email protected]>
Signed-off-by: Felipe Balbi <[email protected]>
---
 drivers/usb/dwc3/gadget.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index 5284919fb283..fd3a52a8cbaf 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -820,6 +820,25 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
        trace_dwc3_prepare_trb(dep, trb);
 }
 
+/**
+ * dwc3_ep_prev_trb() - Returns the previous TRB in the ring
+ * @dep: The endpoint with the TRB ring
+ * @index: The index of the current TRB in the ring
+ *
+ * Returns the TRB prior to the one pointed to by the index. If the
+ * index is 0, we will wrap backwards, skip the link TRB, and return
+ * the one just before that.
+ */
+static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
+{
+       if (!index)
+               index = DWC3_TRB_NUM - 2;
+       else
+               index = dep->trb_enqueue - 1;
+
+       return &dep->trb_pool[index];
+}
+
 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
 {
        struct dwc3_trb         *tmp;
@@ -833,12 +852,9 @@ static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
         * more transfers in our ring.
         */
        if (dep->trb_enqueue == dep->trb_dequeue) {
-               /* If we're full, enqueue/dequeue are > 0 */
-               if (dep->trb_enqueue) {
-                       tmp = &dep->trb_pool[dep->trb_enqueue - 1];
-                       if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
-                               return 0;
-               }
+               tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
+               if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
+                       return 0;
 
                return DWC3_TRB_NUM - 1;
        }
-- 
2.8.3

---------------------------------------------------------------------
Intel Finland Oy
Registered Address: PL 281, 00181 Helsinki 
Business Identity Code: 0357606 - 4 
Domiciled in Helsinki 

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to