From 0b242dea4244d2474259699f97da9472fb4e3145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mo=C5=84?= Date: Mon, 19 Aug 2024 10:34:04 +0200 Subject: [PATCH] drivers: udc_dwc2: Do not enable inactive endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fail TxFIFO write if the endpoint is not activated, because there is essentially no fifo assigned. Writing to not activated endpoint is possible, but it does tend to corrupt fifo number 0 which is used for control transfers. USB stack should not really be attempting to write to endpoints that have not been activated (ep_enable called). Signed-off-by: Tomasz Moń --- drivers/usb/udc/udc_dwc2.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/drivers/usb/udc/udc_dwc2.c b/drivers/usb/udc/udc_dwc2.c index 81c14792d27..d9a27afdffd 100644 --- a/drivers/usb/udc/udc_dwc2.c +++ b/drivers/usb/udc/udc_dwc2.c @@ -409,6 +409,15 @@ static int dwc2_tx_fifo_write(const struct device *dev, } diepctl = sys_read32(diepctl_reg); + if (!(diepctl & USB_DWC2_DEPCTL_USBACTEP)) { + /* Do not attempt to write data on inactive endpoint, because + * no fifo is assigned to inactive endpoint and therefore it is + * possible that the write will corrupt other endpoint fifo. + */ + irq_unlock(key); + return -ENOENT; + } + if (is_iso) { if (priv->sof_num & 1) { diepctl |= USB_DWC2_DEPCTL_SETODDFR;