From 5577401e1427ec3190360a60ff2f9bab4a819952 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Wed, 11 Nov 2026 10:59:04 -0600 Subject: [PATCH 1/2] feat(usb_device): take_msc_from_ejected_host() for devices that stay 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) Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- components/usb_device/README.md | 7 ++++-- components/usb_device/include/usb_device.hpp | 24 ++++++++++++++++++++ components/usb_device/src/usb_device.cpp | 14 ++++++++++-- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/components/usb_device/README.md b/components/usb_device/README.md index dd08558bea..d35f484130 100644 --- a/components/usb_device/README.md +++ b/components/usb_device/README.md @@ -316,8 +316,11 @@ Taking a medium from an attached host with `set_msc_owner()` is refused (`device_or_resource_busy`) because host writes already queued could land under the application's volume: eject the drive on the host first. Turn it off to decide yourself with `set_msc_owner(lun, MscOwner::Host / App)` — for example only expose an SD card -while a "USB drive" screen is shown. `msc_owner()`, `msc_capacity()` and -`MscFunction::on_event` (hand-over started / done / failed, format required) +while a "USB drive" screen is shown. On a device whose USB stays connected for +other functions (a console, a vendor link), the host is always attached, so take +the medium back with `take_msc_from_ejected_host(lun, ec)` once the host has +ejected the drive (esp_tinyusb only acts on an eject itself with auto_handover). +`msc_owner()`, `msc_capacity()` and `MscFunction::on_event` (hand-over started / done / failed, format required) report the state; the event callback runs in the TinyUSB task for host-driven hand-overs, so act on it from your own task. diff --git a/components/usb_device/include/usb_device.hpp b/components/usb_device/include/usb_device.hpp index 1c064fa51e..c514e39bea 100644 --- a/components/usb_device/include/usb_device.hpp +++ b/components/usb_device/include/usb_device.hpp @@ -595,6 +595,26 @@ class UsbDevice : public BaseComponent { /// @brief Convenience overload of set_msc_owner() that ignores errors. bool set_msc_owner(size_t lun, MscOwner owner); + /** + * @brief Take an MSC medium back (MscOwner::App) from a host that is still + * attached but has ejected it -- for a device whose USB stays connected + * for its other functions (a console, a vendor link) while the medium + * goes back and forth, with auto_handover off. + * + * set_msc_owner() refuses that (`device_or_resource_busy`), since it cannot + * tell whether the host is done with the medium: esp_tinyusb implements the + * host's eject (SCSI START STOP UNIT) itself and only acts on it with + * auto_handover. Call this instead once the host has ejected the drive (Finder, + * `diskutil eject`, "Safely remove"), e.g. after the user confirmed it, or + * after your host tool ejected it. The host's queued writes are still drained + * before the volume is mounted here; a host that did NOT eject may lose writes + * it had cached, as it would on an unplug. + * @param lun Medium index (position in MscFunction::media). + * @param[out] ec As for set_msc_owner(), minus `device_or_resource_busy`. + * @return true if the application now has the medium. + */ + bool take_msc_from_ejected_host(size_t lun, std::error_code &ec); + /// @brief Who currently has an MSC medium (nullopt if MSC is not enabled / /// initialized or the index is out of range). An unformatted medium /// waiting for format_msc_medium() reports App, with nothing mounted. @@ -730,6 +750,10 @@ class UsbDevice : public BaseComponent { /// @param owner The owner the event refers to. void handle_msc_event(const void *storage, MscEvent event, MscOwner owner); + /// @brief Internal: set_msc_owner(), optionally trusting that the attached + /// host has ejected the medium (take_msc_from_ejected_host()). + bool set_msc_owner(size_t lun, MscOwner owner, bool host_ejected, std::error_code &ec); + /// @brief Internal: hand an MSC medium over and confirm the result against the /// VFS (esp_tinyusb's setter records the requested owner even when the /// mount / unmount failed, and not every failure raises an event). diff --git a/components/usb_device/src/usb_device.cpp b/components/usb_device/src/usb_device.cpp index 729e5010ae..79e23c67bb 100644 --- a/components/usb_device/src/usb_device.cpp +++ b/components/usb_device/src/usb_device.cpp @@ -2497,6 +2497,14 @@ bool UsbDevice::hand_over_msc(size_t index, MscOwner owner, std::error_code &ec) } bool UsbDevice::set_msc_owner(size_t lun, MscOwner owner, std::error_code &ec) { + return set_msc_owner(lun, owner, false, ec); +} + +bool UsbDevice::take_msc_from_ejected_host(size_t lun, std::error_code &ec) { + return set_msc_owner(lun, MscOwner::App, true, ec); +} + +bool UsbDevice::set_msc_owner(size_t lun, MscOwner owner, bool host_ejected, std::error_code &ec) { ec.clear(); #if (CFG_TUD_MSC > 0) if (!initialized_ || !config_.msc) { @@ -2511,8 +2519,10 @@ bool UsbDevice::set_msc_owner(size_t lun, MscOwner owner, std::error_code &ec) { // esp_tinyusb accepts WRITE(10) data and runs the write later on the TinyUSB // task, without re-checking ownership, so a write the host already queued // could land after the FAT volume is mounted for the application. - const auto host_attached = [this, lun]() { - if (!tud_mounted()) + const auto host_attached = [this, lun, host_ejected]() { + // the caller vouches that the attached host has ejected the medium (it + // stays attached for the other functions): only its queued writes matter + if (!tud_mounted() || host_ejected) return false; logger_.warn("MSC medium {}: the attached host still has it; eject it on the host (or " "detach) before handing it to the application", From 53860c45b9692582e7fd9d21c8e93d41c95c89e4 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Wed, 11 Nov 2026 15:29:28 -0600 Subject: [PATCH 2/2] fix(usb_device): address the take_msc_from_ejected_host() review - 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) Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- components/usb_device/README.md | 4 +++- components/usb_device/include/usb_device.hpp | 12 +++++++----- components/usb_device/src/usb_device.cpp | 7 ++++--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/components/usb_device/README.md b/components/usb_device/README.md index d35f484130..8d4af90fb2 100644 --- a/components/usb_device/README.md +++ b/components/usb_device/README.md @@ -314,7 +314,9 @@ when done (the `msc_example` does). The hand-over covers **all media at once**: esp_tinyusb ignores which drive was ejected, so ejecting either returns both. Taking a medium from an attached host with `set_msc_owner()` is refused (`device_or_resource_busy`) because host writes already queued could land under -the application's volume: eject the drive on the host first. Turn it off to decide yourself with +the application's volume: eject the drive on the host first. + +Set `auto_handover = false` to decide yourself with `set_msc_owner(lun, MscOwner::Host / App)` — for example only expose an SD card while a "USB drive" screen is shown. On a device whose USB stays connected for other functions (a console, a vendor link), the host is always attached, so take diff --git a/components/usb_device/include/usb_device.hpp b/components/usb_device/include/usb_device.hpp index c514e39bea..6409e47d5a 100644 --- a/components/usb_device/include/usb_device.hpp +++ b/components/usb_device/include/usb_device.hpp @@ -610,7 +610,9 @@ class UsbDevice : public BaseComponent { * before the volume is mounted here; a host that did NOT eject may lose writes * it had cached, as it would on an unplug. * @param lun Medium index (position in MscFunction::media). - * @param[out] ec As for set_msc_owner(), minus `device_or_resource_busy`. + * @param[out] ec As for set_msc_owner(), except that it skips the + * attached-host check (the reason set_msc_owner() would report + * `device_or_resource_busy` here). * @return true if the application now has the medium. */ bool take_msc_from_ejected_host(size_t lun, std::error_code &ec); @@ -750,10 +752,6 @@ class UsbDevice : public BaseComponent { /// @param owner The owner the event refers to. void handle_msc_event(const void *storage, MscEvent event, MscOwner owner); - /// @brief Internal: set_msc_owner(), optionally trusting that the attached - /// host has ejected the medium (take_msc_from_ejected_host()). - bool set_msc_owner(size_t lun, MscOwner owner, bool host_ejected, std::error_code &ec); - /// @brief Internal: hand an MSC medium over and confirm the result against the /// VFS (esp_tinyusb's setter records the requested owner even when the /// mount / unmount failed, and not every failure raises an event). @@ -798,6 +796,10 @@ class UsbDevice : public BaseComponent { static UsbDevice *instance(); private: + /// @brief set_msc_owner(), optionally trusting that the attached host has + /// ejected the medium (take_msc_from_ejected_host()). + bool set_msc_owner_impl(size_t lun, MscOwner owner, bool host_ejected, std::error_code &ec); + // Trampoline registered as tinyusb_config_t::event_cb; routes // TINYUSB_EVENT_ATTACHED/DETACHED to the private handlers below. friend void espp_usb_device_event_cb(tinyusb_event_t *event, void *arg); diff --git a/components/usb_device/src/usb_device.cpp b/components/usb_device/src/usb_device.cpp index 79e23c67bb..6dc52a7980 100644 --- a/components/usb_device/src/usb_device.cpp +++ b/components/usb_device/src/usb_device.cpp @@ -2497,14 +2497,15 @@ bool UsbDevice::hand_over_msc(size_t index, MscOwner owner, std::error_code &ec) } bool UsbDevice::set_msc_owner(size_t lun, MscOwner owner, std::error_code &ec) { - return set_msc_owner(lun, owner, false, ec); + return set_msc_owner_impl(lun, owner, false, ec); } bool UsbDevice::take_msc_from_ejected_host(size_t lun, std::error_code &ec) { - return set_msc_owner(lun, MscOwner::App, true, ec); + return set_msc_owner_impl(lun, MscOwner::App, true, ec); } -bool UsbDevice::set_msc_owner(size_t lun, MscOwner owner, bool host_ejected, std::error_code &ec) { +bool UsbDevice::set_msc_owner_impl(size_t lun, MscOwner owner, bool host_ejected, + std::error_code &ec) { ec.clear(); #if (CFG_TUD_MSC > 0) if (!initialized_ || !config_.msc) {