Skip to content

Conversation

@poornadharshan13-rgb
Copy link
Contributor

🧩 Description

This PR fixes incorrect handling of Zero-Length Packets (ZLP) in the DWC2 driver when receiving data (OUT transfers).
Previously, the DWC2 RX-FIFO transfer logic would misinterpret a ZLP as a full-size packet, causing the transfer to complete with the wrong size (equal to the requested buffer length rather than actual received bytes).

This issue was reported in #3154 (ESP32-S3 bulk transfer issues).


🛠️ Changes

  • Fix: Correctly handle ZLP in DWC2 RX-FIFO (ensure xfer->actual_len is set to 0 when ZLP is received).
  • Fix: Ensure transfer completion only occurs when true end-of-transfer conditions are met (packet smaller than max packet size or explicit ZLP).
  • Add: Safety checks in tusb_memcpy_safe() and tusb_memset_safe() to avoid dereferencing NULL pointers.
  • Refactor: Minor cleanup and better handling of RX-FIFO edge cases.

✅ Testing

  • Tested on ESP32-S3 with a custom vendor class using libusb host code (libusb_test.zip from issue ESP32-S3 bulk transfer issues #3154).
  • Verified with various transfer sizes (1–32768 bytes), including boundary cases and ZLP.
  • ZLP is now correctly reported as zero-length.
  • No regression observed on normal bulk transfers.

📦 Affected Components

  • src/portable/synopsys/dwc2/dcd_dwc2.c
  • src/common/tusb_common.c (safe mem helpers)

🧪 Repro Steps (Verified)

  1. Queue a bulk OUT transfer using usbd_edpt_xfer() for >8 KB buffer (e.g., 32768 bytes).
  2. Send varying sizes of data from host using libusb_test.
  3. Observe correct xfer->actual_len for all sizes, including ZLP.

🧠 Notes

  • Behavior regression introduced after TinyUSB 0.15.0~10, likely due to DWC2 RX-FIFO refactor.
  • This PR restores correct ZLP handling and adds guard checks to improve driver safety.

🏁 Conclusion

Fixes #3154:
✅ Correct ZLP detection and handling for DWC2 (ESP32-S3).
✅ Safe memory helpers prevent null pointer usage.
✅ Restores expected behavior for bulk OUT transfers.

Copy link
Owner

@hathach hathach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you very much for detail issue

@hathach hathach merged commit 2e29d1c into hathach:master Oct 11, 2025
111 of 112 checks passed
projectgus pushed a commit to micropython/tinyusb-espressif that referenced this pull request Nov 12, 2025
Details in hathach#3293

Original commit message follows:

successfully
projectgus pushed a commit to micropython/tinyusb-espressif that referenced this pull request Nov 12, 2025
Details in hathach#3293

Original commit message follows:

successfully
projectgus added a commit to projectgus/micropython that referenced this pull request Nov 12, 2025
Temporarily switch from the espressif TinyUSB component to
a MicroPython fork where this fix has been cherry-picked:
hathach/tinyusb#3293

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
projectgus added a commit to projectgus/micropython that referenced this pull request Nov 12, 2025
Temporarily switch from the espressif TinyUSB component to
a MicroPython fork where this fix has been cherry-picked:
hathach/tinyusb#3293

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
projectgus added a commit to projectgus/micropython that referenced this pull request Nov 12, 2025
Temporarily switch from the espressif TinyUSB component to
a MicroPython fork where this fix has been cherry-picked:
hathach/tinyusb#3293

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
projectgus added a commit to projectgus/micropython that referenced this pull request Nov 12, 2025
Temporarily switch from the espressif TinyUSB component to
a MicroPython fork where this fix has been cherry-picked:
hathach/tinyusb#3293

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
projectgus added a commit to projectgus/micropython that referenced this pull request Nov 12, 2025
Temporarily switch from the espressif TinyUSB component to
a MicroPython fork where this fix has been cherry-picked:
hathach/tinyusb#3293

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
@hathach hathach mentioned this pull request Nov 17, 2025
1 task
projectgus added a commit to projectgus/micropython that referenced this pull request Nov 18, 2025
Temporarily switch from the espressif TinyUSB component to
a MicroPython fork where this fix has been cherry-picked:
hathach/tinyusb#3293

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
projectgus added a commit to projectgus/micropython that referenced this pull request Nov 18, 2025
Temporarily switch from the espressif TinyUSB component to
a MicroPython fork where this fix has been cherry-picked:
hathach/tinyusb#3293

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
dpgeorge pushed a commit to projectgus/micropython that referenced this pull request Nov 19, 2025
Temporarily switch from the espressif TinyUSB component to
a MicroPython fork where this fix has been cherry-picked:
hathach/tinyusb#3293

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
Anton-2 pushed a commit to Anton-2/micropython that referenced this pull request Nov 19, 2025
Temporarily switch from the espressif TinyUSB component to
a MicroPython fork where this fix has been cherry-picked:
hathach/tinyusb#3293

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
dpgeorge added a commit to dpgeorge/micropython that referenced this pull request Nov 24, 2025
This is 0.19.0 plus the following changes:
- remove obsolete dcd_esp32sx
- fix for HID stylus descriptor and HID example
- typos, docs and generator scripts
- MTP fix
- DWC2 enumeration when EP0 size=8
- DWC2 fix for EP0 IN
- stm32 FSDEV IRQ remapping fix
- DWC2 ZLP fix

The reason we need the extra 24 commits is due to a bug with TinyUSB's
handling of zero-length-packets in the DWC2 (Synopsis) backend, which
affects the stm32 port.  That's fixed by
hathach/tinyusb#3293

Signed-off-by: Damien George <damien@micropython.org>
Anton-2 pushed a commit to Anton-2/micropython that referenced this pull request Dec 3, 2025
This is 0.19.0 plus the following changes:
- remove obsolete dcd_esp32sx
- fix for HID stylus descriptor and HID example
- typos, docs and generator scripts
- MTP fix
- DWC2 enumeration when EP0 size=8
- DWC2 fix for EP0 IN
- stm32 FSDEV IRQ remapping fix
- DWC2 ZLP fix

The reason we need the extra 24 commits is due to a bug with TinyUSB's
handling of zero-length-packets in the DWC2 (Synopsis) backend, which
affects the stm32 port.  That's fixed by
hathach/tinyusb#3293

Signed-off-by: Damien George <damien@micropython.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ESP32-S3 bulk transfer issues

3 participants