Skip to content

feat(usb_device): take_msc_from_ejected_host() for devices that stay attached - #898

Merged
finger563 merged 2 commits into
mainfrom
feat/usb-msc-host-eject
Nov 11, 2026
Merged

finger563 merged 2 commits into
mainfrom
feat/usb-msc-host-eject

Conversation

@finger563

Copy link
Copy Markdown
Contributor

Summary

Adds UsbDevice::take_msc_from_ejected_host(lun, ec). It takes an MSC medium back from a host that is still attached but has ejected it.

Who this is for: a device that keeps its USB connected for other functions, such as a CDC console or a vendor link, while an SD card goes back and forth as a USB drive with auto_handover off. On such a device the host is always attached, so there was no way to take the card back:

  • set_msc_owner(lun, MscOwner::App) refuses with device_or_resource_busy while tud_mounted(), because it can't tell whether the host is done with the medium.
  • esp_tinyusb handles the host's eject (SCSI START STOP UNIT) itself, and acts on it only when auto_handover is on. With it off the eject is ignored.

The new call skips the attached-host check on the caller's word that the host ejected the drive (after the user confirmed it, or after a host tool ran diskutil eject). It still drains the TinyUSB task's queued writes before the application's volume is mounted. A host that did not eject may lose writes it had cached, as it would on an unplug; this is documented.

  • usb_device.hpp: the public take_msc_from_ejected_host(), plus a protected set_msc_owner(lun, owner, host_ejected, ec) that both public calls forward to.
  • README.md: the hand-over section now describes the always-attached case.

Testing

I used this from esp-box-emu (ESP32-S3) and tab5-emu (ESP32-P4). Each runs a composite device (CDC console, vendor link and MSC SD card) and hands the card over and back on request from the host (emulink drive on/off, which ejects the drive on macOS before asking for the card back):

  • About 10 cycles on each board, with the host writing files in between. A 1 MB file the Mac wrote read back identical on the device.
  • The console and link stayed up throughout.
  • Without this call, the take-back was refused on every cycle.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

…attached

A device that keeps its USB connected for a console or vendor link while an
MSC medium goes back and forth (auto_handover off) could not take the medium
back: set_msc_owner(App) refuses while the host is attached, and esp_tinyusb
ignores the host's eject without auto_handover. take_msc_from_ejected_host()
skips the attached-host check on the caller's word that the host ejected the
drive, and still drains the host's queued writes before mounting.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
Copilot AI balanced review requested due to automatic review settings November 11, 2026 16:59
@github-actions

Copy link
Copy Markdown

✅Static analysis result - no issues found! ✅

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

3 open issues
What changed in this PR

Adds an API to reclaim an MSC medium from a host that remains USB-attached but has ejected the drive, enabling “always-connected” composite devices to hand SD media back to the application without a detach.

Changes:

  • Add UsbDevice::take_msc_from_ejected_host(lun, ec) and route MSC ownership changes through an internal helper that can bypass the “attached host” refusal.
  • Adjust attached-host detection to treat “host ejected” as not blocking ownership transfer while still allowing queued-write draining behavior to run.
  • Update README hand-over documentation for the always-attached composite-device scenario.
File Description
components/​usb_device/​src/​usb_device.cpp Adds the new public API and introduces an internal set_msc_owner(..., host_ejected, ...) path that bypasses the attached-host busy check when appropriate.
components/​usb_device/​include/​usb_device.hpp Publicly documents take_msc_from_ejected_host() and adds the internal overload used by both ownership-setting entry points.
components/​usb_device/​README.md Documents the always-attached use case and points users to take_msc_from_ejected_host().

Review effort: Lite

Comment thread components/usb_device/README.md Resolved
- set_msc_owner()'s host_ejected overload is now the private
  set_msc_owner_impl(), so it is not part of the subclass API
- take_msc_from_ejected_host()'s ec documentation says it skips the
  attached-host check instead of promising an error code never occurs
- README: name auto_handover where the hand-over text says to turn it off

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the new API that reclaims ejected MSC media while keeping other USB functions connected.

No open issues 🎉

3 resolved since last review

Review effort: Balanced

@finger563
finger563 merged commit 08295c0 into main Nov 11, 2026
185 checks passed
@finger563
finger563 deleted the feat/usb-msc-host-eject branch November 11, 2026 22:21
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.

2 participants