diff --git a/ports/zephyr-cp/cptools/build_circuitpython.py b/ports/zephyr-cp/cptools/build_circuitpython.py index 89ef4fa0845..48971d77f4c 100644 --- a/ports/zephyr-cp/cptools/build_circuitpython.py +++ b/ports/zephyr-cp/cptools/build_circuitpython.py @@ -630,6 +630,10 @@ async def build_circuitpython(): # noqa: C901 "layout dtsi, or disable the USB device controller in the devicetree." ) circuitpython_flags.append(f"-DCIRCUITPY_USB_DEVICE={1 if usb_ok else 0}") + # MSC is the CIRCUITPY drive interface. The Zephyr port registers it only when + # storage.disable_usb_drive() has not turned it off (see supervisor/usb.c). + circuitpython_flags.append(f"-DCIRCUITPY_USB_MSC={1 if usb_ok else 0}") + circuitpython_flags.append(f"-DCIRCUITPY_USB_MSC_ENABLED_DEFAULT={1 if usb_ok else 0}") if usb_ok: enabled_modules.add("usb_cdc") diff --git a/ports/zephyr-cp/supervisor/usb.c b/ports/zephyr-cp/supervisor/usb.c index 44fc23c0c9a..c10cdec9737 100644 --- a/ports/zephyr-cp/supervisor/usb.c +++ b/ports/zephyr-cp/supervisor/usb.c @@ -94,6 +94,7 @@ int _zephyr_disk_status(struct disk_info *disk); int _zephyr_disk_read(struct disk_info *disk, uint8_t *data_buf, uint32_t start_sector, uint32_t num_sector); int _zephyr_disk_write(struct disk_info *disk, const uint8_t *data_buf, uint32_t start_sector, uint32_t num_sector); int _zephyr_disk_ioctl(struct disk_info *disk, uint8_t cmd, void *buff); +static bool msc_enabled(void); static const struct disk_operations disk_ops = { .init = _zephyr_disk_init, @@ -123,6 +124,9 @@ int _zephyr_disk_status(struct disk_info *disk) { supervisor_vfs_t *root_vfs = filesystem_circuitpy(); fs_user_mount_t *root = root_vfs == NULL ? NULL : &root_vfs->fat; int lun = 0; + if (!msc_enabled()) { + return DISK_STATUS_NOMEDIA; + } if (root == NULL) { printk("Status: No media\n"); return DISK_STATUS_NOMEDIA; @@ -224,6 +228,38 @@ int _zephyr_disk_ioctl(struct disk_info *disk, uint8_t cmd, void *buff) { // volatile to avoid the compiler caching stale values. static volatile bool usb_configured = false; +// Whether the CIRCUITPY MSC drive is enabled (storage.disable_usb_drive() clears it). +static bool msc_enabled(void) { + #if CIRCUITPY_USB_MSC && CIRCUITPY_STORAGE + return storage_usb_enabled(); + #else + return false; + #endif +} + +// Release the USB MSC lock on CIRCUITPY, if disk status took it. Other holders are not touched. +void usb_msc_release_circuitpy_lock(void) { + if (!locked[0]) { + return; + } + supervisor_vfs_t *circuitpy = filesystem_circuitpy(); + if (circuitpy != NULL) { + blockdev_unlock(circuitpy); + } + locked[0] = false; +} + +// Whether USB MSC holds the blockdev lock on this mount. +bool usb_msc_holds_lock(fs_user_mount_t *vfs) { + supervisor_vfs_t *circuitpy = filesystem_circuitpy(); + return locked[0] && circuitpy != NULL && &circuitpy->fat == vfs; +} + +// Zephyr's MSC reports the LUN state on every TEST UNIT READY, so there is no eject state to reset. +void usb_msc_remount(fs_user_mount_t *fs_mount) { + (void)fs_mount; +} + static const struct device *const console_dev = DEVICE_DT_GET(DT_NODELABEL(cdc_acm_console)); static bool console_dtr = false; static bool usb_suspended = false; @@ -389,12 +425,11 @@ void usb_init(void) { } } - err = usbd_register_class(&main_usbd, "msc_0", USBD_SPEED_HS, 1); - if (err) { - printk("Failed to add register MSC class %d\n", err); - return; - } else { - printk("Registered MSC class for high speed\n"); + if (msc_enabled()) { + err = usbd_register_class(&main_usbd, "msc_0", USBD_SPEED_HS, 1); + if (err) { + return; + } } #if CIRCUITPY_USB_HID @@ -442,10 +477,11 @@ void usb_init(void) { } } - err = usbd_register_class(&main_usbd, "msc_0", USBD_SPEED_FS, 1); - if (err) { - printk("Failed to add register MSC class %d\n", err); - return; + if (msc_enabled()) { + err = usbd_register_class(&main_usbd, "msc_0", USBD_SPEED_FS, 1); + if (err) { + return; + } } /* doc functions register end */ diff --git a/ports/zephyr-cp/tusb.h b/ports/zephyr-cp/tusb.h new file mode 100644 index 00000000000..2233b4c2e2e --- /dev/null +++ b/ports/zephyr-cp/tusb.h @@ -0,0 +1,16 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 Adafruit Industries LLC +// +// SPDX-License-Identifier: MIT + +#pragma once + +// Zephyr USB does not use TinyUSB. Shared code (shared-module/storage) asks +// tud_connected() whether the host is attached, so answer from the Zephyr USB state. + +#include + +#include "supervisor/usb.h" + +static inline bool tud_connected(void) { + return usb_connected(); +}