From c08db05455bcb2259849a096acf2e90cce258849 Mon Sep 17 00:00:00 2001 From: Angus Ainslie Date: Wed, 2 Feb 2022 15:08:54 -0800 Subject: [PATCH 1/3] usb: dwc3: dwc3-generic: check the parent nodes The kernel devicetree has definitions for port and hub nodes as subnodes to the USB devices. These subnodes don't contain all of the data required to properly configure the dwc3. Check the parent nodes if the data is not in the port/hub node. Here's an example from the librem5 kernel dts file &usb_dwc3_0 { #address-cells = <1>; #size-cells = <0>; dr_mode = "otg"; snps,dis_u3_susphy_quirk; status = "okay"; port@0 { reg = <0>; typec_hs: endpoint { remote-endpoint = <&usb_con_hs>; }; }; port@1 { reg = <1>; typec_ss: endpoint { remote-endpoint = <&usb_con_ss>; }; }; }; &usb_dwc3_1 { dr_mode = "host"; status = "okay"; #address-cells = <1>; #size-cells = <0>; /* Microchip USB2642 */ hub@1 { compatible = "usb424,2640"; reg = <1>; #address-cells = <1>; #size-cells = <0>; mass-storage@1 { compatible = "usb424,4041"; reg = <1>; }; }; }; Signed-off-by: Angus Ainslie --- drivers/usb/dwc3/dwc3-generic.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/drivers/usb/dwc3/dwc3-generic.c b/drivers/usb/dwc3/dwc3-generic.c index 8d53ba7790..01bd0ca190 100644 --- a/drivers/usb/dwc3/dwc3-generic.c +++ b/drivers/usb/dwc3/dwc3-generic.c @@ -110,7 +110,12 @@ static int dwc3_generic_of_to_plat(struct udevice *dev) struct dwc3_generic_plat *plat = dev_get_plat(dev); ofnode node = dev_ofnode(dev); - plat->base = dev_read_addr(dev); + if (!strncmp(dev->name, "port", 4) || !strncmp(dev->name, "hub", 3)) { + /* This is a leaf so check the parent */ + plat->base = dev_read_addr(dev->parent); + } else { + plat->base = dev_read_addr(dev); + } plat->maximum_speed = usb_get_maximum_speed(node); if (plat->maximum_speed == USB_SPEED_UNKNOWN) { @@ -120,8 +125,13 @@ static int dwc3_generic_of_to_plat(struct udevice *dev) plat->dr_mode = usb_get_dr_mode(node); if (plat->dr_mode == USB_DR_MODE_UNKNOWN) { - pr_err("Invalid usb mode setup\n"); - return -ENODEV; + /* might be a leaf so check the parent for mode */ + node = dev_ofnode(dev->parent); + plat->dr_mode = usb_get_dr_mode(node); + if (plat->dr_mode == USB_DR_MODE_UNKNOWN) { + pr_err("Invalid usb mode setup\n"); + return -ENODEV; + } } return 0; @@ -301,16 +311,20 @@ static int dwc3_glue_bind(struct udevice *parent) { ofnode node; int ret; + enum usb_dr_mode dr_mode; + + dr_mode = usb_get_dr_mode(dev_ofnode(parent)); ofnode_for_each_subnode(node, dev_ofnode(parent)) { const char *name = ofnode_get_name(node); - enum usb_dr_mode dr_mode; struct udevice *dev; const char *driver = NULL; debug("%s: subnode name: %s\n", __func__, name); - dr_mode = usb_get_dr_mode(node); + /* if the parent node doesn't have a mode check the leaf */ + if (!dr_mode) + dr_mode = usb_get_dr_mode(node); switch (dr_mode) { case USB_DR_MODE_PERIPHERAL: @@ -450,6 +464,7 @@ static const struct udevice_id dwc3_glue_ids[] = { { .compatible = "rockchip,rk3328-dwc3" }, { .compatible = "rockchip,rk3399-dwc3" }, { .compatible = "qcom,dwc3" }, + { .compatible = "fsl,imx8mq-dwc3" }, { .compatible = "intel,tangier-dwc3" }, { } }; From fb146fbc1ae551ce3bdb2966dd36a0b38e62b5dc Mon Sep 17 00:00:00 2001 From: Angus Ainslie Date: Wed, 2 Feb 2022 15:08:55 -0800 Subject: [PATCH 2/3] usb: dwc3: core: stop the core when it's removed If u-boot doesn't stop the core when it's finished with it then linux can't find it. Signed-off-by: Angus Ainslie --- drivers/usb/dwc3/core.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c index ce1c0e88c2..b592a487e0 100644 --- a/drivers/usb/dwc3/core.c +++ b/drivers/usb/dwc3/core.c @@ -706,6 +706,14 @@ static void dwc3_gadget_run(struct dwc3 *dwc) mdelay(100); } +static void dwc3_core_stop(struct dwc3 *dwc) +{ + u32 reg; + + reg = dwc3_readl(dwc->regs, DWC3_DCTL); + dwc3_writel(dwc->regs, DWC3_DCTL, reg & ~(DWC3_DCTL_RUN_STOP)); +} + static void dwc3_core_exit_mode(struct dwc3 *dwc) { switch (dwc->dr_mode) { @@ -1128,6 +1136,7 @@ void dwc3_remove(struct dwc3 *dwc) dwc3_core_exit_mode(dwc); dwc3_event_buffers_cleanup(dwc); dwc3_free_event_buffers(dwc); + dwc3_core_stop(dwc); dwc3_core_exit(dwc); kfree(dwc->mem); } From d5daa02d8d9e7c403a3339db1966e8413e64e408 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Mon, 27 Sep 2021 14:42:58 +0200 Subject: [PATCH 3/3] usb: xhci: reset endpoint on USB stall There are devices which cause a USB stall when trying to read strings. Specifically Arduino Mega R3 stalls when trying to read the product string. The stall currently remains unhandled, and subsequent retries submit new transfers on a stopped endpoint which ultimately cause a crash in abort_td(): WARN halted endpoint, queueing URB anyway. XHCI control transfer timed out, aborting... Unexpected XHCI event TRB, skipping... (3affe040 00000000 13000000 02008401) BUG at drivers/usb/host/xhci-ring.c:505/abort_td()! BUG! resetting ... Linux seems to be able to recover from the stall by issuing a TRB_RESET_EP command. Introduce reset_ep() which issues a TRB_RESET_EP followed by setting the transfer ring dequeue pointer via TRB_SET_DEQ. This allows to properly recover from a USB stall error and continue communicating with the USB device. Signed-off-by: Stefan Agner --- drivers/usb/host/xhci-ring.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c index 0b3e7a2f55..eb6dfcdb09 100644 --- a/drivers/usb/host/xhci-ring.c +++ b/drivers/usb/host/xhci-ring.c @@ -481,6 +481,33 @@ union xhci_trb *xhci_wait_for_event(struct xhci_ctrl *ctrl, trb_type expected) BUG(); } +/* + * Send reset endpoint command for given endpoint. This recovers from a + * halted endpoint (e.g. due to a stall error). + */ +static void reset_ep(struct usb_device *udev, int ep_index) +{ + struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); + struct xhci_ring *ring = ctrl->devs[udev->slot_id]->eps[ep_index].ring; + union xhci_trb *event; + u32 field; + + printf("Resetting EP %d...\n", ep_index); + xhci_queue_command(ctrl, NULL, udev->slot_id, ep_index, TRB_RESET_EP); + event = xhci_wait_for_event(ctrl, TRB_COMPLETION); + field = le32_to_cpu(event->trans_event.flags); + BUG_ON(TRB_TO_SLOT_ID(field) != udev->slot_id); + xhci_acknowledge_event(ctrl); + + xhci_queue_command(ctrl, (void *)((uintptr_t)ring->enqueue | + ring->cycle_state), udev->slot_id, ep_index, TRB_SET_DEQ); + event = xhci_wait_for_event(ctrl, TRB_COMPLETION); + BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) + != udev->slot_id || GET_COMP_CODE(le32_to_cpu( + event->event_cmd.status)) != COMP_SUCCESS); + xhci_acknowledge_event(ctrl); +} + /* * Stops transfer processing for an endpoint and throws away all unprocessed * TRBs by setting the xHC's dequeue pointer to our enqueue pointer. The next @@ -928,6 +955,10 @@ int xhci_ctrl_tx(struct usb_device *udev, unsigned long pipe, record_transfer_result(udev, event, length); xhci_acknowledge_event(ctrl); + if (udev->status == USB_ST_STALLED) { + reset_ep(udev, ep_index); + return -EPIPE; + } /* Invalidate buffer to make it available to usb-core */ if (length > 0)