Skip to content

Commit bd889a2

Browse files
tmon-nordicgithub-actions[bot]
authored andcommitted
[nrf fromtree] drivers: udc_dwc2: Allocate multiple of bMaxPacketSize0
Control OUT buffers must be multiple of bMaxPacketSize0 in Buffer DMA mode. While the transfer can be configured to smaller values, DMA will write data past the buffer (and transfer size counter will underflow) if the packet on the bus is larger or if there are multiple back-to-back SETUP packets. Signed-off-by: Tomasz Moń <[email protected]> (cherry picked from commit a61afc8) (cherry picked from commit 5ee4d80)
1 parent 769eafb commit bd889a2

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

drivers/usb/udc/udc_dwc2.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,7 @@ static void dwc2_prep_rx(const struct device *dev, struct net_buf *buf,
666666
doeptsiz = usb_dwc2_set_doeptsizn_pktcnt(pktcnt) |
667667
usb_dwc2_set_doeptsizn_xfersize(xfersize);
668668
if (cfg->addr == USB_CONTROL_EP_OUT) {
669-
/* Use 1 to allow 8 byte long buffers for SETUP data */
670-
doeptsiz |= (1 << USB_DWC2_DOEPTSIZ0_SUPCNT_POS);
669+
doeptsiz |= (3 << USB_DWC2_DOEPTSIZ0_SUPCNT_POS);
671670
}
672671

673672
priv->rx_siz[ep_idx] = doeptsiz;
@@ -727,10 +726,17 @@ static void dwc2_handle_xfer_next(const struct device *dev,
727726

728727
static int dwc2_ctrl_feed_dout(const struct device *dev, const size_t length)
729728
{
729+
struct udc_dwc2_data *const priv = udc_get_private(dev);
730730
struct udc_ep_config *ep_cfg = udc_get_ep_cfg(dev, USB_CONTROL_EP_OUT);
731731
struct net_buf *buf;
732+
size_t alloc_len = length;
733+
734+
if (dwc2_in_buffer_dma_mode(dev)) {
735+
/* Control OUT buffers must be multiple of bMaxPacketSize0 */
736+
alloc_len = ROUND_UP(length, 64);
737+
}
732738

733-
buf = udc_ctrl_alloc(dev, USB_CONTROL_EP_OUT, length);
739+
buf = udc_ctrl_alloc(dev, USB_CONTROL_EP_OUT, alloc_len);
734740
if (buf == NULL) {
735741
return -ENOMEM;
736742
}

0 commit comments

Comments
 (0)