Repository navigation
feat(usb_device): take_msc_from_ejected_host() for devices that stay attached - #898
Merged
Merged
Conversation
…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
|
✅Static analysis result - no issues found! ✅ |
Contributor
There was a problem hiding this comment.
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
The wording “minusdevice_or_resource_busy” is a strong guarantee that may be inaccurate if… · New This helper is documented as “Internal” but isprotected, which makes it part of the subclass API… · New The sentence “Turn it off to decide yourself…” is unclear in this excerpt because the pronoun… · New
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
Copilot stopped reviewing on behalf of
finger563 due to an error
November 11, 2026 17:17
- 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
Merged
6 tasks done
Merged
4 of 5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


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_handoveroff. 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 withdevice_or_resource_busywhiletud_mounted(), because it can't tell whether the host is done with the medium.auto_handoveris 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 publictake_msc_from_ejected_host(), plus a protectedset_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):🤖 Generated with Claude Code
https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY