How to stop triggering USB Autoplay when Nordic USB Mass Storage Plugged in, but no SD Card

Problem:

I am working on USB, CDC ACM, USB Mass Storage (using SD Card). I currently want to be able to still use the USB stack functions with only my USB Mass Storage being deactivated. The caveat being, the deactivation happens during runtime (not compile time, hence cannot solve this by using KConfig). I need it during runtime because I want to do a check if whether or not my SD Card is plugged in or not. The current problem is in which the automatic USB storage pop up will show in windows. I need to stop that pop up from showing through modifying the device kernel logic on.

Here is my current `prj.conf`

# Enable USB subsystem
CONFIG_USB_COMPOSITE_DEVICE=y
CONFIG_UART_ASYNC_API=y
CONFIG_SERIAL=y
CONFIG_USB_DEVICE_PRODUCT="My Nordic USB Device"
CONFIG_USB_DEVICE_PID=0x530E
# --- Disable USB logging/console output ---
CONFIG_UART_CONSOLE=n      
CONFIG_LOG_BACKEND_UART=n       
CONFIG_USB_DRIVER_LOG_LEVEL_OFF=y
CONFIG_USB_DEVICE_LOG_LEVEL_OFF=y

# Enable the UART driver
CONFIG_UART_LINE_CTRL=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_UART_ASYNC_ADAPTER=y
CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_DEVICE_REMOTE_WAKEUP=n
CONFIG_USB_CDC_ACM=y
CONFIG_USB_CDC_ACM_LOG_LEVEL_OFF=y
# Enable Sdcard
CONFIG_DISK_DRIVER_SDMMC=y
CONFIG_FILE_SYSTEM=y
CONFIG_FAT_FILESYSTEM_ELM=y
CONFIG_FS_FATFS_LFN=y
CONFIG_FS_FATFS_MAX_LFN=255
# Enable Storae-Mass
CONFIG_USB_MASS_STORAGE=y
CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y
CONFIG_USB_MASS_STORAGE_LOG_LEVEL_ERR=y
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n
CONFIG_MASS_STORAGE_DISK_NAME="SD"

This image shows the popup everytime I plugin or reset my device, I need this popup to stop showing, even when my SD Card is not plugged in.

I've tried modifying the `msc.c` to do a check. But adding this line comprimises the USB stack, it causes the `usb_dc_status_code == USB_DC_SUSPEND`

static void mass_interface_config(struct usb_desc_header *head,
				  uint8_t bInterfaceNumber)
{
	ARG_UNUSED(head);

    // ADDED THIS_START
	if (disk_access_init(disk_pdrv) != 0) {
		LOG_ERR("Storage init ERROR !!!! - Aborting USB Mass Storage Interface Config");
		return -ENODEV;
	}
	// ADDED THIS_END

	mass_cfg.if0.bInterfaceNumber = bInterfaceNumber;
}

/* Configuration of the Mass Storage Device send to the USB Driver */
USBD_DEFINE_CFG_DATA(mass_storage_config) = {
	.usb_device_description = NULL,
	.interface_config = mass_interface_config,
	.interface_descriptor = &mass_cfg.if0,
	.cb_usb_status = mass_storage_status_cb,
	.interface = {
		.class_handler = mass_storage_class_handle_req,
		.custom_handler = NULL,
	},
	.num_endpoints = ARRAY_SIZE(mass_ep_data),
	.endpoint = mass_ep_data
};

I've also tried modifying the `mass_storage_init()` like such. But the popup still shows. The macro is just for simple example. The real implementation would be using a zbus or something to suspend and then notify this very function after the sdcard status is confirmed (whether is inserted or not).

#define TEST_STOP_MSC true
static int mass_storage_init(void)
{
	uint32_t block_size = 0U;

	if (TEST_STOP_MSC) {
		return 0;
	}

	if (disk_access_init(disk_pdrv) != 0) {
		LOG_ERR("Storage init ERROR !!!! - Aborting USB init");
		return 0;
	}

Parents Reply
  • I don't have any other suggestions for the legacy stack, unfortunately. On that stack, the MSC implementation doesn't seem to support unregistering the class; and the registration happens by the Zephyr system before main() even starts.

    That means you likely have to modify the MSC implementation much.

    If time allows, perhaps you can try the next stack on a minimally modified mass sample and see if implementing the "removed" device is simpler there.

Children
Related