Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions components/usb_device/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,15 @@ 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. `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)
Comment thread
Copilot marked this conversation as resolved.
Resolved
report the state; the event callback runs in the TinyUSB task for host-driven
hand-overs, so act on it from your own task.

Expand Down
26 changes: 26 additions & 0 deletions components/usb_device/include/usb_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,28 @@ 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(), 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);

/// @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.
Expand Down Expand Up @@ -774,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);
Expand Down
15 changes: 13 additions & 2 deletions components/usb_device/src/usb_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2497,6 +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_impl(lun, owner, false, ec);
}

bool UsbDevice::take_msc_from_ejected_host(size_t lun, std::error_code &ec) {
return set_msc_owner_impl(lun, MscOwner::App, true, 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) {
Expand All @@ -2511,8 +2520,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",
Expand Down
Loading