2015-07-02 18:54:48

by Reyad Attiyat

[permalink] [raw]
Subject: [PATCH v3] usb: xhci: Add support for URB_ZERO_PACKET to bulk/sg transfers

This commit checks for the URB_ZERO_PACKET flag and creates an extra
zero-length td if the urb transfer length is a multiple of the endpoint's
max packet length.

Signed-off-by: Reyad Attiyat <[email protected]>
---
drivers/usb/host/xhci-ring.c | 66 ++++++++++++++++++++++++++++++++++----------
drivers/usb/host/xhci.c | 5 ++++
2 files changed, 57 insertions(+), 14 deletions(-)

diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 7d34cbf..31ea1b9 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -3038,9 +3038,11 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
struct xhci_td *td;
struct scatterlist *sg;
int num_sgs;
- int trb_buff_len, this_sg_len, running_total;
+ int trb_buff_len, this_sg_len, running_total, ret;
unsigned int total_packet_count;
+ bool zero_length_needed;
bool first_trb;
+ int last_trb_num;
u64 addr;
bool more_trbs_coming;

@@ -3056,13 +3058,27 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
total_packet_count = DIV_ROUND_UP(urb->transfer_buffer_length,
usb_endpoint_maxp(&urb->ep->desc));

- trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
+ ret = prepare_transfer(xhci, xhci->devs[slot_id],
ep_index, urb->stream_id,
num_trbs, urb, 0, mem_flags);
- if (trb_buff_len < 0)
- return trb_buff_len;
+ if (ret < 0)
+ return ret;

urb_priv = urb->hcpriv;
+
+ /* Deal with URB_ZERO_PACKET - need one more td/trb */
+ zero_length_needed = urb->transfer_flags & URB_ZERO_PACKET &&
+ urb_priv->length == 2;
+ if (zero_length_needed) {
+ num_trbs++;
+ xhci_dbg(xhci, "Creating zero length td.\n");
+ ret = prepare_transfer(xhci, xhci->devs[slot_id],
+ ep_index, urb->stream_id,
+ 1, urb, 1, mem_flags);
+ if (ret < 0)
+ return ret;
+ }
+
td = urb_priv->td[0];

/*
@@ -3092,6 +3108,7 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
trb_buff_len = urb->transfer_buffer_length;

first_trb = true;
+ last_trb_num = zero_length_needed ? 2 : 1;
/* Queue the first TRB, even if it's zero-length */
do {
u32 field = 0;
@@ -3109,12 +3126,15 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
/* Chain all the TRBs together; clear the chain bit in the last
* TRB to indicate it's the last TRB in the chain.
*/
- if (num_trbs > 1) {
+ if (num_trbs > last_trb_num) {
field |= TRB_CHAIN;
- } else {
- /* FIXME - add check for ZERO_PACKET flag before this */
+ } else if (num_trbs == last_trb_num) {
td->last_trb = ep_ring->enqueue;
field |= TRB_IOC;
+ } else if (zero_length_needed && num_trbs == 1) {
+ trb_buff_len = 0;
+ urb_priv->td[1]->last_trb = ep_ring->enqueue;
+ field |= TRB_IOC;
}

/* Only set interrupt on short packet for IN endpoints */
@@ -3176,7 +3196,7 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
if (running_total + trb_buff_len > urb->transfer_buffer_length)
trb_buff_len =
urb->transfer_buffer_length - running_total;
- } while (running_total < urb->transfer_buffer_length);
+ } while (num_trbs > 0);

check_trb_math(urb, num_trbs, running_total);
giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
@@ -3194,7 +3214,9 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
int num_trbs;
struct xhci_generic_trb *start_trb;
bool first_trb;
+ int last_trb_num;
bool more_trbs_coming;
+ bool zero_length_needed;
int start_cycle;
u32 field, length_field;

@@ -3225,7 +3247,6 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
num_trbs++;
running_total += TRB_MAX_BUFF_SIZE;
}
- /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */

ret = prepare_transfer(xhci, xhci->devs[slot_id],
ep_index, urb->stream_id,
@@ -3234,6 +3255,20 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
return ret;

urb_priv = urb->hcpriv;
+
+ /* Deal with URB_ZERO_PACKET - need one more td/trb */
+ zero_length_needed = urb->transfer_flags & URB_ZERO_PACKET &&
+ urb_priv->length == 2;
+ if (zero_length_needed) {
+ num_trbs++;
+ xhci_dbg(xhci, "Creating zero length td.\n");
+ ret = prepare_transfer(xhci, xhci->devs[slot_id],
+ ep_index, urb->stream_id,
+ 1, urb, 1, mem_flags);
+ if (ret < 0)
+ return ret;
+ }
+
td = urb_priv->td[0];

/*
@@ -3255,7 +3290,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
trb_buff_len = urb->transfer_buffer_length;

first_trb = true;
-
+ last_trb_num = zero_length_needed ? 2 : 1;
/* Queue the first TRB, even if it's zero-length */
do {
u32 remainder = 0;
@@ -3272,12 +3307,15 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
/* Chain all the TRBs together; clear the chain bit in the last
* TRB to indicate it's the last TRB in the chain.
*/
- if (num_trbs > 1) {
+ if (num_trbs > last_trb_num) {
field |= TRB_CHAIN;
- } else {
- /* FIXME - add check for ZERO_PACKET flag before this */
+ } else if (num_trbs == last_trb_num) {
td->last_trb = ep_ring->enqueue;
field |= TRB_IOC;
+ } else if (zero_length_needed && num_trbs == 1) {
+ trb_buff_len = 0;
+ urb_priv->td[1]->last_trb = ep_ring->enqueue;
+ field |= TRB_IOC;
}

/* Only set interrupt on short packet for IN endpoints */
@@ -3315,7 +3353,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
trb_buff_len = urb->transfer_buffer_length - running_total;
if (trb_buff_len > TRB_MAX_BUFF_SIZE)
trb_buff_len = TRB_MAX_BUFF_SIZE;
- } while (running_total < urb->transfer_buffer_length);
+ } while (num_trbs > 0);

check_trb_math(urb, num_trbs, running_total);
giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 36bf089..40f433d 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -1340,6 +1340,11 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)

if (usb_endpoint_xfer_isoc(&urb->ep->desc))
size = urb->number_of_packets;
+ else if (usb_endpoint_is_bulk_out(&urb->ep->desc) &&
+ urb->transfer_buffer_length > 0 &&
+ urb->transfer_flags & URB_ZERO_PACKET &&
+ !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc)))
+ size = 2;
else
size = 1;

--
2.4.3


2015-07-02 19:00:41

by Reyad Attiyat

[permalink] [raw]
Subject: Re: [PATCH v3] usb: xhci: Add support for URB_ZERO_PACKET to bulk/sg transfers

This version of the patch changes last_trb varible name to
last_trb_num and fixes code style. I have also added a td to the urb
td array. This now gets prepared properl,y with prepare_transfer(),
and is handled correctly when transferred and completed. It only calls
the urb completion callback once as there is a check in finish_td() to
ensure that all td's have been received.

If you think I should change anything else please let me know.

Thank you,
Reyad Attiyat

On Thu, Jul 2, 2015 at 1:54 PM, Reyad Attiyat <[email protected]> wrote:
> This commit checks for the URB_ZERO_PACKET flag and creates an extra
> zero-length td if the urb transfer length is a multiple of the endpoint's
> max packet length.
>
> Signed-off-by: Reyad Attiyat <[email protected]>
> ---
> drivers/usb/host/xhci-ring.c | 66 ++++++++++++++++++++++++++++++++++----------
> drivers/usb/host/xhci.c | 5 ++++
> 2 files changed, 57 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
> index 7d34cbf..31ea1b9 100644
> --- a/drivers/usb/host/xhci-ring.c
> +++ b/drivers/usb/host/xhci-ring.c
> @@ -3038,9 +3038,11 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
> struct xhci_td *td;
> struct scatterlist *sg;
> int num_sgs;
> - int trb_buff_len, this_sg_len, running_total;
> + int trb_buff_len, this_sg_len, running_total, ret;
> unsigned int total_packet_count;
> + bool zero_length_needed;
> bool first_trb;
> + int last_trb_num;
> u64 addr;
> bool more_trbs_coming;
>
> @@ -3056,13 +3058,27 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
> total_packet_count = DIV_ROUND_UP(urb->transfer_buffer_length,
> usb_endpoint_maxp(&urb->ep->desc));
>
> - trb_buff_len = prepare_transfer(xhci, xhci->devs[slot_id],
> + ret = prepare_transfer(xhci, xhci->devs[slot_id],
> ep_index, urb->stream_id,
> num_trbs, urb, 0, mem_flags);
> - if (trb_buff_len < 0)
> - return trb_buff_len;
> + if (ret < 0)
> + return ret;
>
> urb_priv = urb->hcpriv;
> +
> + /* Deal with URB_ZERO_PACKET - need one more td/trb */
> + zero_length_needed = urb->transfer_flags & URB_ZERO_PACKET &&
> + urb_priv->length == 2;
> + if (zero_length_needed) {
> + num_trbs++;
> + xhci_dbg(xhci, "Creating zero length td.\n");
> + ret = prepare_transfer(xhci, xhci->devs[slot_id],
> + ep_index, urb->stream_id,
> + 1, urb, 1, mem_flags);
> + if (ret < 0)
> + return ret;
> + }
> +
> td = urb_priv->td[0];
>
> /*
> @@ -3092,6 +3108,7 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
> trb_buff_len = urb->transfer_buffer_length;
>
> first_trb = true;
> + last_trb_num = zero_length_needed ? 2 : 1;
> /* Queue the first TRB, even if it's zero-length */
> do {
> u32 field = 0;
> @@ -3109,12 +3126,15 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
> /* Chain all the TRBs together; clear the chain bit in the last
> * TRB to indicate it's the last TRB in the chain.
> */
> - if (num_trbs > 1) {
> + if (num_trbs > last_trb_num) {
> field |= TRB_CHAIN;
> - } else {
> - /* FIXME - add check for ZERO_PACKET flag before this */
> + } else if (num_trbs == last_trb_num) {
> td->last_trb = ep_ring->enqueue;
> field |= TRB_IOC;
> + } else if (zero_length_needed && num_trbs == 1) {
> + trb_buff_len = 0;
> + urb_priv->td[1]->last_trb = ep_ring->enqueue;
> + field |= TRB_IOC;
> }
>
> /* Only set interrupt on short packet for IN endpoints */
> @@ -3176,7 +3196,7 @@ static int queue_bulk_sg_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
> if (running_total + trb_buff_len > urb->transfer_buffer_length)
> trb_buff_len =
> urb->transfer_buffer_length - running_total;
> - } while (running_total < urb->transfer_buffer_length);
> + } while (num_trbs > 0);
>
> check_trb_math(urb, num_trbs, running_total);
> giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
> @@ -3194,7 +3214,9 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
> int num_trbs;
> struct xhci_generic_trb *start_trb;
> bool first_trb;
> + int last_trb_num;
> bool more_trbs_coming;
> + bool zero_length_needed;
> int start_cycle;
> u32 field, length_field;
>
> @@ -3225,7 +3247,6 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
> num_trbs++;
> running_total += TRB_MAX_BUFF_SIZE;
> }
> - /* FIXME: this doesn't deal with URB_ZERO_PACKET - need one more */
>
> ret = prepare_transfer(xhci, xhci->devs[slot_id],
> ep_index, urb->stream_id,
> @@ -3234,6 +3255,20 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
> return ret;
>
> urb_priv = urb->hcpriv;
> +
> + /* Deal with URB_ZERO_PACKET - need one more td/trb */
> + zero_length_needed = urb->transfer_flags & URB_ZERO_PACKET &&
> + urb_priv->length == 2;
> + if (zero_length_needed) {
> + num_trbs++;
> + xhci_dbg(xhci, "Creating zero length td.\n");
> + ret = prepare_transfer(xhci, xhci->devs[slot_id],
> + ep_index, urb->stream_id,
> + 1, urb, 1, mem_flags);
> + if (ret < 0)
> + return ret;
> + }
> +
> td = urb_priv->td[0];
>
> /*
> @@ -3255,7 +3290,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
> trb_buff_len = urb->transfer_buffer_length;
>
> first_trb = true;
> -
> + last_trb_num = zero_length_needed ? 2 : 1;
> /* Queue the first TRB, even if it's zero-length */
> do {
> u32 remainder = 0;
> @@ -3272,12 +3307,15 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
> /* Chain all the TRBs together; clear the chain bit in the last
> * TRB to indicate it's the last TRB in the chain.
> */
> - if (num_trbs > 1) {
> + if (num_trbs > last_trb_num) {
> field |= TRB_CHAIN;
> - } else {
> - /* FIXME - add check for ZERO_PACKET flag before this */
> + } else if (num_trbs == last_trb_num) {
> td->last_trb = ep_ring->enqueue;
> field |= TRB_IOC;
> + } else if (zero_length_needed && num_trbs == 1) {
> + trb_buff_len = 0;
> + urb_priv->td[1]->last_trb = ep_ring->enqueue;
> + field |= TRB_IOC;
> }
>
> /* Only set interrupt on short packet for IN endpoints */
> @@ -3315,7 +3353,7 @@ int xhci_queue_bulk_tx(struct xhci_hcd *xhci, gfp_t mem_flags,
> trb_buff_len = urb->transfer_buffer_length - running_total;
> if (trb_buff_len > TRB_MAX_BUFF_SIZE)
> trb_buff_len = TRB_MAX_BUFF_SIZE;
> - } while (running_total < urb->transfer_buffer_length);
> + } while (num_trbs > 0);
>
> check_trb_math(urb, num_trbs, running_total);
> giveback_first_trb(xhci, slot_id, ep_index, urb->stream_id,
> diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
> index 36bf089..40f433d 100644
> --- a/drivers/usb/host/xhci.c
> +++ b/drivers/usb/host/xhci.c
> @@ -1340,6 +1340,11 @@ int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
>
> if (usb_endpoint_xfer_isoc(&urb->ep->desc))
> size = urb->number_of_packets;
> + else if (usb_endpoint_is_bulk_out(&urb->ep->desc) &&
> + urb->transfer_buffer_length > 0 &&
> + urb->transfer_flags & URB_ZERO_PACKET &&
> + !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc)))
> + size = 2;
> else
> size = 1;
>
> --
> 2.4.3
>

2015-07-03 13:36:55

by Mathias Nyman

[permalink] [raw]
Subject: Re: [PATCH v3] usb: xhci: Add support for URB_ZERO_PACKET to bulk/sg transfers

On 02.07.2015 22:00, Reyad Attiyat wrote:
> This version of the patch changes last_trb varible name to
> last_trb_num and fixes code style. I have also added a td to the urb
> td array. This now gets prepared properl,y with prepare_transfer(),
> and is handled correctly when transferred and completed. It only calls
> the urb completion callback once as there is a check in finish_td() to
> ensure that all td's have been received.
>
> If you think I should change anything else please let me know.
>
> Thank you,
> Reyad Attiyat
>
> On Thu, Jul 2, 2015 at 1:54 PM, Reyad Attiyat <[email protected]> wrote:
>> This commit checks for the URB_ZERO_PACKET flag and creates an extra
>> zero-length td if the urb transfer length is a multiple of the endpoint's
>> max packet length.
>>
>> Signed-off-by: Reyad Attiyat <[email protected]>
>> ---

Looks good, Thank you

I assume it works with your wireless card?

I'll go on a two weeks vacation starting now, but after that I'll
send it forward. (it should go to usb-next and reach 4.3 kernel anyway)

-Mathias