If URB_ZERO_PACKET is set then make sure that the last usb message
sent isn't a full sized one. A partial transfer is needed to terminate
variable length bulk output (eg ethernet frames).

URB_ZERO_PACKET is easily implemented by increasing td_residue
to account for the extra packet.

Signed-off-by: David Laight <[email protected]>
---
 drivers/usb/host/xhci-ring.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index afd8eeb..1324bb0 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -3296,10 +3296,25 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, 
gfp_t mem_flags,
         * td_residue is set so that td_residue/max_packet is the number of
         * packets required to send the data after the current partial
         * transfer(s) have completed.
+        *
+        * If URB_ZERO_PACKET is unset td_residue - urb->transfer_buffer_length
+        * is always less than or equal to 2 * (max_packet - 1).
+        * If a ZLP is needed the difference is 2 * max_packet - 1.
         */
        max_packet = GET_MAX_PACKET(usb_endpoint_maxp(&urb->ep->desc));
-       td_residue = urb->transfer_buffer_length;
-       td_residue = ALIGN(td_residue, max_packet) + max_packet - 1;
+       td_residue = ALIGN(urb->transfer_buffer_length, max_packet);
+       /* If URB_ZERO_PACKET is set we might need another message. */
+       if (urb->transfer_flags & URB_ZERO_PACKET &&
+                       td_residue == urb->transfer_buffer_length) {
+               /*
+                * For v1.0 hosts we could set the last TRB with td_size == 1,
+                * but for older hosts we need an extra TRB.
+                */
+               num_trbs++;
+               /* The td_size in earlier fragments needs to be 1 higher */
+               td_residue += max_packet;
+       }
+       td_residue += max_packet - 1;
 
        ret = prepare_transfer(xhci, xhci->devs[slot_id],
                        ep_index, urb->stream_id,
@@ -3343,11 +3358,12 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, 
gfp_t mem_flags,
                 */
                trb_buff_len = (~addr & (TRB_MAX_BUFF_SIZE - 1)) + 1;
                trb_buff_len = min_t(u32, trb_buff_len, this_sg_len);
+               trb_buff_len = min_t(u32, trb_buff_len, len_left);
 
                /* Check for last fragment... */
-               if (trb_buff_len >= len_left) {
-                       /* FIXME - add check for ZERO_PACKET flag before this */
-                       trb_buff_len = len_left;
+               td_residue -= trb_buff_len;
+               if (trb_buff_len == len_left && (len_left == 0 ||
+                               td_residue < 2 * max_packet - 1)) {
                        urb_priv = urb->hcpriv;
                        urb_priv->td[0]->last_trb = ep_ring->enqueue;
                        /* Request interrupt and clear chain */
@@ -3356,7 +3372,6 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t 
mem_flags,
                } else {
                        if (trb_buff_len == 0)
                                goto next_frag;
-                       td_residue -= trb_buff_len;
                }
 
                /* Set the correct cycle bit (inverted for first TRB). */
@@ -3391,7 +3406,7 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t 
mem_flags,
                /* Advance source data pointer */
                this_sg_len -= trb_buff_len;
 next_frag:
-               if (this_sg_len == 0) {
+               if (this_sg_len == 0 && len_left != 0) {
                        sg = sg_next(sg);
                        addr = (u64) sg_dma_address(sg);
                        this_sg_len = sg_dma_len(sg);
-- 
1.8.1.2



--
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