← Back to Pull requests

zephyr: Rework the IIOD USB class pipe lifecycle #1586

Closed
unknown closed this pull request without merging on Nov 12, 2026 14:09 UTC
View on GitHub
unknown unknown commented on Sep 24, 2026 13:57 UTC
Author

PR Description

This branch fixes a series of correctness bugs in zephyr/iiod/usb.c's pipe handling, reworks how each pipe is served so those bugs can't recur, and backs it with a new hardware-free test suite.

Bug fixes — each only reachable via specific host behavior (replug, SET_INTERFACE) or hardware, which is why none surfaced until hunted for on a bench:

  • Wrong endpoint addresses on asymmetric controllers. Addresses were cached from the wrong speed's descriptor array in .init (it runs once per speed, high-speed first, so the full-speed pass's placeholder values stuck by luck until overwritten). MAX32 assigns IN and OUT asymmetrically (IN at 0x87, OUT at 0x01), which broke.

  • Fix: resolve addresses from the speed-correct array at each use, as CDC ACM and MSC do, and validate the resolved map at .enable where the negotiated speed is known.

  • Boot-time fault on composite devices. The server thread waits on a semaphore from boot, but it was initialized in .init, which doesn't run until usbd_init(). A composite device calls that later, so the thread waited on an uninitialized semaphore and faulted.

  • Fix: initialize the semaphore statically in the instance definition, as CDC ECM and Bluetooth HCI do.

  • Racy stale-data discard after reopen. Discarding stale receive bytes relied on a cross-thread ring buffer reset that no single site could ever get right.

  • Fix: drop the ring buffer. Each received net_buf is now queued to the interpreter as-is, stamped with the session it arrived for, so staleness is a per-buffer check instead of a cross-thread reset — one fewer copy per received byte, and rx-fifo-size's byte buffer is replaced by a small rx-depth buffer count (10296 bytes of RAM back on apard32690).

  • Pipe stuck refusing OPEN_PIPE after an abnormal session end. OPEN_PIPE returned early when the pipe's open flag was already set, assuming only the control handler ends a session. A receive error or a SET_INTERFACE (which cancels every queued buffer, even re-selecting the same alternate) ends one too, leaving the flag stuck and every later OPEN_PIPE refused for the rest of the configuration.

  • Fix: always start a fresh session unconditionally; the flag still gates whether the completion handler drops packets, but no longer gates whether the pipe can reopen.

  • Write falsely reported success. Teardown gave the TX semaphore itself to synthesize a completion the stack already guarantees (it dequeues the IN endpoint before disabling the class). The leftover count made the next write's wait return immediately, reporting success for a transfer that hadn't started and reusing a buffer the controller still held. OPEN_PIPE had a matching reset of the same semaphore, out from under a writer that could be blocked on it.

  • Fix: drop both the synthesized give and the reset — nothing manufactures a stale count, so nothing needs resetting.

  • Reopening a pipe could stall or deadlock the whole bus. OPEN_PIPE created the interpreter thread for a session, and CLOSE_PIPE only asked it to leave, so a reopen had to join it — from the control handler, on the USB stack's single cooperative thread. That stalls every endpoint while it waits, and deadlocks outright if the thread being joined is itself blocked on a write completion that only that same cooperative thread delivers.

  • Fix: the largest change in the branch — give every pipe one thread, created once at startup, that parks between sessions and is signalled to start and stop one, so no join is needed; a reader leaves as soon as its session number is superseded rather than being waited on. Pipe 0 (the command channel) drops its special-case handling and runs the same loop, so RESET_PIPES actually ends its session instead of only discarding what it had buffered.

    Also included

  • Fewer USB round trips. Every TX chunk costs a transfer and a completion, so a response larger than tx-buf-size paid one round trip per 64 bytes. Default raised to 512 — one round trip per chunk at high speed instead of eight.
  • Devicetree bounds. num-pipes and tx-buf-size get lower bounds in the binding, so a config the code can't actually run (an array too small to hold the pipes, a chunk size that isn't a whole packet) is caught at build time instead of at a client's next handshake.
  • Tests

    Adds zephyr/tests/iiod_usb_class, a native_sim suite (libiio.iiod.usb_class) that puts both ends of the bus in one image — the class on a virtual UDC driven through the real USB host stack — so it exercises the actual libiio vendor requests and BINARY handshake with no board, no USB/IP, and no root, and runs in a fraction of a second. 14 cases, covering:

  • Open, close, and reopen — including reopening with no pause in between

  • Out-of-range and unknown vendor requests

  • Bytes delivered to a closed pipe being dropped

  • One pipe cycled enough to prove buffers from a prior session don't strand another pipe

  • Recovery after SET_INTERFACE and after a full reconfiguration

  • The command channel surviving RESET_PIPES, including repeated resets
  • Each covers a bug above or a hazard the rework introduces (e.g. the parked-thread handoff), catching regressions that previously needed a real board to reproduce.

    PR Type


  • Bug fix (a change that fixes an issue)

  • New feature (a change that adds new functionality)

  • Breaking change (a change that affects other repos or cause CIs to fail)
  • PR Checklist


  • I have conducted a self-review of my own code changes

  • I have commented new code, particularly complex or unclear areas

  • I have checked that I did not introduce new warnings or errors (CI output)

  • I have checked that components that use libiio did not get broken

  • I have updated the documentation accordingly (GitHub Pages, READMEs, etc)
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: petejohanson-adi Review duration: 70571m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1586"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%