From fd5ad8e10d17340dca8893f763d72ddde1c293e1 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Tue, 17 Nov 2026 05:46:54 -0800 Subject: [PATCH 1/2] zephyr-cp: make storage.disable_usb_drive() work Zephyr always registered the MSC class and always reported media, so storage.disable_usb_drive() had no effect. Skip MSC registration and report no media when storage_usb_enabled() is false, and pass the MSC flags from the build script. Add a tusb.h shim so shared storage code can ask whether USB is connected, and helpers for the CIRCUITPY lock. Co-Authored-By: Claude Fable 5.2 --- .../zephyr-cp/cptools/build_circuitpython.py | 4 ++ ports/zephyr-cp/supervisor/usb.c | 61 ++++++++++++++++--- ports/zephyr-cp/tusb.h | 16 +++++ 3 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 ports/zephyr-cp/tusb.h diff --git a/ports/zephyr-cp/cptools/build_circuitpython.py b/ports/zephyr-cp/cptools/build_circuitpython.py index 96612d5c2e4..9cdb9977a11 100644 --- a/ports/zephyr-cp/cptools/build_circuitpython.py +++ b/ports/zephyr-cp/cptools/build_circuitpython.py @@ -594,6 +594,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..9d55139f4af 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,10 @@ 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()) { + printk("Status: Disabled\n"); + return DISK_STATUS_NOMEDIA; + } if (root == NULL) { printk("Status: No media\n"); return DISK_STATUS_NOMEDIA; @@ -224,6 +229,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 +426,14 @@ 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) { + printk("Failed to add register MSC class %d\n", err); + return; + } else { + printk("Registered MSC class for high speed\n"); + } } #if CIRCUITPY_USB_HID @@ -442,10 +481,12 @@ 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) { + printk("Failed to add register MSC class %d\n", 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(); +} From 025c62eaebd5cc801f4c132eff43577515e59266 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Wed, 18 Nov 2026 09:24:15 -0800 Subject: [PATCH 2/2] zephyr-cp: drop the printk()s added around the USB drive switch Co-Authored-By: Claude Opus 5.5 --- ports/zephyr-cp/supervisor/usb.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/ports/zephyr-cp/supervisor/usb.c b/ports/zephyr-cp/supervisor/usb.c index 9d55139f4af..c10cdec9737 100644 --- a/ports/zephyr-cp/supervisor/usb.c +++ b/ports/zephyr-cp/supervisor/usb.c @@ -125,7 +125,6 @@ int _zephyr_disk_status(struct disk_info *disk) { fs_user_mount_t *root = root_vfs == NULL ? NULL : &root_vfs->fat; int lun = 0; if (!msc_enabled()) { - printk("Status: Disabled\n"); return DISK_STATUS_NOMEDIA; } if (root == NULL) { @@ -429,10 +428,7 @@ void usb_init(void) { if (msc_enabled()) { 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"); } } @@ -484,7 +480,6 @@ void usb_init(void) { if (msc_enabled()) { 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; } }