2019-04-30 03:09:02

by Jim Lin

[permalink] [raw]
Subject: [PATCH v3 1/1] usb: xhci: Add Clear_TT_Buffer

USB 2.0 specification chapter 11.17.5 says "as part of endpoint halt
processing for full-/low-speed endpoints connected via a TT, the host
software must use the Clear_TT_Buffer request to the TT to ensure
that the buffer is not in the busy state".

In our case, a full-speed speaker (ConferenceCam) is behind a high-
speed hub (ConferenceCam Connect), sometimes once we get STALL on a
request we may continue to get STALL with the folllowing requests,
like Set_Interface.

Here we add Clear_TT_Buffer for the following Set_Interface requests
to get ACK successfully.

Signed-off-by: Jim Lin <[email protected]>
---
v2: xhci_clear_tt_buffer_complete: add static, shorter indentation
, remove its claiming in xhci.h
v3: Add description for clearing_tt (xhci.h)

drivers/usb/host/xhci-ring.c | 28 ++++++++++++++++++++++++++++
drivers/usb/host/xhci.c | 7 +++++++
drivers/usb/host/xhci.h | 2 ++
3 files changed, 37 insertions(+)

diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 9215a28dad40..02b1ebad81e7 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -1786,6 +1786,33 @@ struct xhci_segment *trb_in_td(struct xhci_hcd *xhci,
return NULL;
}

+static void xhci_clear_hub_tt_buffer(struct xhci_hcd *xhci,
+ unsigned int slot_id, struct xhci_td *td)
+{
+ struct xhci_virt_device *dev;
+ struct xhci_slot_ctx *slot_ctx;
+ int saved_devnum;
+
+ /*
+ * As part of low/full-speed endpoint-halt processing
+ * we must clear the TT buffer (USB 2.0 specification 11.17.5).
+ */
+ if (td->urb->dev->tt && !usb_pipeint(td->urb->pipe) &&
+ (td->urb->dev->tt->hub != xhci_to_hcd(xhci)->self.root_hub) &&
+ !xhci->clearing_tt) {
+ xhci->clearing_tt = 1;
+ dev = xhci->devs[slot_id];
+ slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
+ /* Update devnum temporarily for usb_hub_clear_tt_buffer */
+ saved_devnum = td->urb->dev->devnum;
+ td->urb->dev->devnum = (int) le32_to_cpu(slot_ctx->dev_state) &
+ DEV_ADDR_MASK;
+ if (usb_hub_clear_tt_buffer(td->urb))
+ xhci->clearing_tt = 0;
+ td->urb->dev->devnum = saved_devnum;
+ }
+}
+
static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
unsigned int slot_id, unsigned int ep_index,
unsigned int stream_id, struct xhci_td *td,
@@ -1804,6 +1831,7 @@ static void xhci_cleanup_halted_endpoint(struct xhci_hcd *xhci,
if (reset_type == EP_HARD_RESET) {
ep->ep_state |= EP_HARD_CLEAR_TOGGLE;
xhci_cleanup_stalled_ring(xhci, ep_index, stream_id, td);
+ xhci_clear_hub_tt_buffer(xhci, slot_id, td);
}
xhci_ring_cmd_db(xhci);
}
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 7fa58c99f126..4890c3518aa3 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -5132,6 +5132,12 @@ int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
}
EXPORT_SYMBOL_GPL(xhci_gen_setup);

+static void xhci_clear_tt_buffer_complete(struct usb_hcd *hcd,
+ struct usb_host_endpoint *ep)
+{
+ hcd_to_xhci(hcd)->clearing_tt = 0;
+}
+
static const struct hc_driver xhci_hc_driver = {
.description = "xhci-hcd",
.product_desc = "xHCI Host Controller",
@@ -5192,6 +5198,7 @@ static const struct hc_driver xhci_hc_driver = {
.enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout,
.disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout,
.find_raw_port_number = xhci_find_raw_port_number,
+ .clear_tt_buffer_complete = xhci_clear_tt_buffer_complete,
};

void xhci_init_driver(struct hc_driver *drv,
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index 9334cdee382a..dab1476e5bdf 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -1864,6 +1864,8 @@ struct xhci_hcd {
unsigned hw_lpm_support:1;
/* Broken Suspend flag for SNPS Suspend resume issue */
unsigned broken_suspend:1;
+ /* Clear_TT_Buffer in progress */
+ unsigned clearing_tt:1;
/* cached usb2 extened protocol capabilites */
u32 *ext_caps;
unsigned int num_ext_caps;
--
2.1.4


2019-05-02 10:55:49

by Mathias Nyman

[permalink] [raw]
Subject: Re: [PATCH v3 1/1] usb: xhci: Add Clear_TT_Buffer

On 30.4.2019 6.06, Jim Lin wrote:
> USB 2.0 specification chapter 11.17.5 says "as part of endpoint halt
> processing for full-/low-speed endpoints connected via a TT, the host
> software must use the Clear_TT_Buffer request to the TT to ensure
> that the buffer is not in the busy state".

Good point, xhci isn't making sure TT buffers get cleared when they should.

>
> In our case, a full-speed speaker (ConferenceCam) is behind a high-
> speed hub (ConferenceCam Connect), sometimes once we get STALL on a
> request we may continue to get STALL with the folllowing requests,
> like Set_Interface.
>
> Here we add Clear_TT_Buffer for the following Set_Interface requests
> to get ACK successfully.
>
> Signed-off-by: Jim Lin <[email protected]>
> ---
> v2: xhci_clear_tt_buffer_complete: add static, shorter indentation
> , remove its claiming in xhci.h
> v3: Add description for clearing_tt (xhci.h)
>
> drivers/usb/host/xhci-ring.c | 28 ++++++++++++++++++++++++++++
> drivers/usb/host/xhci.c | 7 +++++++
> drivers/usb/host/xhci.h | 2 ++
> 3 files changed, 37 insertions(+)
>
> diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
> index 9215a28dad40..02b1ebad81e7 100644
> --- a/drivers/usb/host/xhci-ring.c
> +++ b/drivers/usb/host/xhci-ring.c
> @@ -1786,6 +1786,33 @@ struct xhci_segment *trb_in_td(struct xhci_hcd *xhci,
> return NULL;
> }
>
> +static void xhci_clear_hub_tt_buffer(struct xhci_hcd *xhci,
> + unsigned int slot_id, struct xhci_td *td)
> +{
> + struct xhci_virt_device *dev;
> + struct xhci_slot_ctx *slot_ctx;
> + int saved_devnum;
> +
> + /*
> + * As part of low/full-speed endpoint-halt processing
> + * we must clear the TT buffer (USB 2.0 specification 11.17.5).
> + */
> + if (td->urb->dev->tt && !usb_pipeint(td->urb->pipe) &&
> + (td->urb->dev->tt->hub != xhci_to_hcd(xhci)->self.root_hub) &&
> + !xhci->clearing_tt) {
> + xhci->clearing_tt = 1;

one xhci->clearing_tt under is not enough, there might be several HS hubs, or
multi TT hubs with halted endpoints at the same time that need TT clearing.

How about a flag per endpoint?

For example Aadding a EP_CLEARING_TT flag for ep_state in struct xhci_virt_ep?
just like EP_STOP_CMD_PENDING, or EP_HALTED

> + dev = xhci->devs[slot_id];
> + slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
> + /* Update devnum temporarily for usb_hub_clear_tt_buffer */
> + saved_devnum = td->urb->dev->devnum;
> + td->urb->dev->devnum = (int) le32_to_cpu(slot_ctx->dev_state) &
> + DEV_ADDR_MASK;

Changing the struct usb_device devnum on the fly seems like a bit of a hack, and probably
causes issues elsewhere. Devnum is tied to uevents, usbfs, sysfs etc.

We need another solution, some options:

- Let usb_hub_clear_tt_buffer() figure out address and not just use devnum if host == xhci.
- Add address to struct usb_device, (would have both address and devnum), use it when needed.
- Provide address as parameter to usb_clear_tt_buffer() (api change, changes other host drivers)
- Force devnum to be same as address, usb core can't choose address for xhci devices.

-Mathias

2019-05-02 12:35:14

by Jim Lin

[permalink] [raw]
Subject: Re: [PATCH v3 1/1] usb: xhci: Add Clear_TT_Buffer



On 2019年05月02日 18:56, Mathias Nyman wrote:
> On 30.4.2019 6.06, Jim Lin wrote:
>> USB 2.0 specification chapter 11.17.5 says "as part of endpoint halt
>> processing for full-/low-speed endpoints connected via a TT, the host
>> software must use the Clear_TT_Buffer request to the TT to ensure
>> that the buffer is not in the busy state".
>
> Good point, xhci isn't making sure TT buffers get cleared when they
> should.
>
>>
>> In our case, a full-speed speaker (ConferenceCam) is behind a high-
>> speed hub (ConferenceCam Connect), sometimes once we get STALL on a
>> request we may continue to get STALL with the folllowing requests,
>> like Set_Interface.
>>
>> Here we add Clear_TT_Buffer for the following Set_Interface requests
>> to get ACK successfully.
>>
>> Signed-off-by: Jim Lin <[email protected]>
>> ---
>> v2: xhci_clear_tt_buffer_complete: add static, shorter indentation
>> , remove its claiming in xhci.h
>> v3: Add description for clearing_tt (xhci.h)
>>
>> drivers/usb/host/xhci-ring.c | 28 ++++++++++++++++++++++++++++
>> drivers/usb/host/xhci.c | 7 +++++++
>> drivers/usb/host/xhci.h | 2 ++
>> 3 files changed, 37 insertions(+)
>>
>> diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
>> index 9215a28dad40..02b1ebad81e7 100644
>> --- a/drivers/usb/host/xhci-ring.c
>> +++ b/drivers/usb/host/xhci-ring.c
>> @@ -1786,6 +1786,33 @@ struct xhci_segment *trb_in_td(struct xhci_hcd
>> *xhci,
>> return NULL;
>> }
>> +static void xhci_clear_hub_tt_buffer(struct xhci_hcd *xhci,
>> + unsigned int slot_id, struct xhci_td *td)
>> +{
>> + struct xhci_virt_device *dev;
>> + struct xhci_slot_ctx *slot_ctx;
>> + int saved_devnum;
>> +
>> + /*
>> + * As part of low/full-speed endpoint-halt processing
>> + * we must clear the TT buffer (USB 2.0 specification 11.17.5).
>> + */
>> + if (td->urb->dev->tt && !usb_pipeint(td->urb->pipe) &&
>> + (td->urb->dev->tt->hub != xhci_to_hcd(xhci)->self.root_hub) &&
>> + !xhci->clearing_tt) {
>> + xhci->clearing_tt = 1;
>
> one xhci->clearing_tt under is not enough, there might be several HS
> hubs, or
> multi TT hubs with halted endpoints at the same time that need TT
> clearing.
>
> How about a flag per endpoint?
>
> For example Aadding a EP_CLEARING_TT flag for ep_state in struct
> xhci_virt_ep?
> just like EP_STOP_CMD_PENDING, or EP_HALTED
>
>> + dev = xhci->devs[slot_id];
>> + slot_ctx = xhci_get_slot_ctx(xhci, dev->out_ctx);
>> + /* Update devnum temporarily for usb_hub_clear_tt_buffer */
>> + saved_devnum = td->urb->dev->devnum;
>> + td->urb->dev->devnum = (int) le32_to_cpu(slot_ctx->dev_state) &
>> + DEV_ADDR_MASK;
>
> Changing the struct usb_device devnum on the fly seems like a bit of a
> hack, and probably
> causes issues elsewhere. Devnum is tied to uevents, usbfs, sysfs etc.
>
> We need another solution, some options:
>
> - Let usb_hub_clear_tt_buffer() figure out address and not just use
> devnum if host == xhci.
> - Add address to struct usb_device, (would have both address and
> devnum), use it when needed.
> - Provide address as parameter to usb_clear_tt_buffer() (api change,
> changes other host drivers)
> - Force devnum to be same as address, usb core can't choose address
> for xhci devices.
>
> -Mathias
Thanks for review. Will try.

--nvpublic