max3421: a control transfer submitted right after a control timeout overwrites the pending abort

Open
#4,475 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
58/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
c

Research direction

Start with src/host/usbh.c and hcd_max3421.c, tracing the control timeout, abort, and setup paths cited in the issue. Check how EP0 state and busy_lock behave when a new transfer is submitted before HXFRDN retires the abort. The fix should prevent the replacement SETUP from being lost or a late completion from advancing the replacement; the proposed HIL case on metro_m4_express could confirm this.

Written by the indexing model from the issue text.

Description

Bug 🐞 follow-up Prio 📌

Greptile raised this in adafruit/Adafruit_TinyUSB_Arduino#615, the vendored sync of d543126c5. I checked it against
26adc208a (src/host/usbh.c and hcd_max3421.c are unchanged on current master).

Related: #4070 lists max3421 as aborting an active transfer, which this contradicts. #4472 plans HIL abort tests.

Trigger

On a MAX3421 host, a device NAKs a control stage for longer than CFG_TUH_CONTROL_TIMEOUT_MS. A new control transfer
to the same address is then submitted before the next HXFRDN, either from the TIMEOUT complete_cb or by
control_xfer_dispatch_pending() on the next loop pass.

Causal path

  1. usbh.c:2433-2434: control_xfer_timeout_expired() calls hcd_edpt_abort_xfer(), then
    control_xfer_complete(TIMEOUT) straight away. That sets the slot IDLE and calls the callback (:1200-1203). A
    resubmit reaches hcd_setup_send() at :1102, or at :1172 through the dispatch at :913-915.
  2. hcd_max3421.c:765-768: the abort only sets EP_STATE_ABORTING, which takes effect on the next NAK
    (:901-903).
  3. hcd_max3421.c:781-799: hcd_setup_send() overwrites the shared EP0 entry with EP_STATE_ATTEMPT_1. The NAK loop
    still holds busy_lock, so no SETUP is launched.
  4. hcd_max3421.c:905-908: the next NAK re-writes the cached _hcd_data.hxfr of the old stage. The MAX3421E
    Programming Guide (p10, p56) says every retry is a CPU write of HXFR; the chip has no hardware abort.

Consequence

  • The replacement's SETUP never goes on the wire, and while the device keeps NAKing it times out too.
  • If the device later ACKs the old IN, RCVDAV (hcd_max3421.c:1066-1072) writes up to 8 bytes into
    _usbh_epbuf.request. usbh_control_xfer_cb() (usbh.c:1216, :1249-1254) treats that completion as the SETUP
    and starts the data stage with a corrupted wLength and direction.
  • A late zero-length status completion can likewise advance the replacement past a SETUP the device never received.

Fix direction

Keep the slot unavailable until the abort retires, or make hcd_setup_send() defer a new SETUP while EP0 is
EP_STATE_ABORTING and busy_lock is held.

Uncertain

  • Not reproduced on hardware.
  • The window depends on the gap between the abort and the next HXFRDN. It is wider where the MAX3421 interrupt is
    serviced from a task, as in the ESP32 BSP.
  • The corruption branch needs a device that answers the old IN after the timeout.
  • A HIL case with a NAK-forever EP0 fixture on metro_m4_express would confirm it.
Dominant language
C
Stars
7.3k
Forks
1.6k
Avg merge
1d 19h
Merged PRs (30d)
67

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from hathach/tinyusb

All issues in hathach/tinyusb

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.