Skip to content

Multiple USB contexts for same device lead to bus faults #74291

@tmon-nordic

Description

@tmon-nordic

Describe the bug
USBD shell module uses zephyr_udc0 node

/* Default device descriptors and context used in the shell. */
USBD_DEVICE_DEFINE(sh_uds_ctx, DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)),
0x2fe3, 0xffff);

and so do all USB samples
USBD_DEVICE_DEFINE(sample_usbd,
DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)),
ZEPHYR_PROJECT_USB_VID, CONFIG_SAMPLE_USBD_PID);

This leads to multiple issues, like handling SETUP packets twice in:

k_msgq_get(&usbd_msgq, &event, K_FOREVER);
STRUCT_SECTION_FOREACH(usbd_context, uds_ctx) {
if (uds_ctx->dev == event.dev) {
usbd_event_handler(uds_ctx, &event);
}
}

Because the first SETUP handler will free the buffer, this leads to BUS FAULT on second time the handler is called.

To Reproduce
Steps to reproduce the behavior:

  1. CONF_FILE=usbd_next_prj.conf west build --pristine -b nrf54h20dk/nrf54h20/cpuapp -d build-usb-cdc-acm-54h20/ ~/zephyrproject/zephyr/samples/subsys/usb/cdc_acm -- -DCONFIG_SHELL=y -DCONFIG_USBD_SHELL=y
  2. cd build-usb-cdc-acm-54h20
  3. west flash
  4. Connect nRF54H20 USB to host and observe BUS FAULT on UART (J-Link on debugger USB)

Expected behavior
Build should fail if there is more than 1 context for the same udc device.

Impact
Wasted time trying to understand what is going on (multiple context pointing to same device).

Logs and console output

*** Booting Zephyr OS build v3.6.0-6257-gd45605e6a3df ***
[00:00:00.022,014] <inf> LD_DVFS_LIB: Trim ABB for default voltage.
[00:00:00.022,018] <inf> LD_DVFS_LIB: Waiting for backend init
[00:00:00.022,040] <inf> NRFS_BACKEND: Bound to sysctrl.
[00:00:00.022,054] <inf> LD_DVFS_LIB: nrfs_dvfs_init
[00:00:00.022,058] <inf> LD_DVFS_LIB: nrfs_dvfs_init_prepare_request
[00:00:00.022,131] <inf> cdc_acm_echo: USB device support enabled
[00:00:00.022,135] <inf> cdc_acm_echo: Wait for DTR
[00:00:00.022,245] <inf> LD_DVFS_LIB: nrfs_dvfs_evt_handler
[00:00:00.022,249] <inf> LD_DVFS_LIB: DVFS handler EVT_INIT_DONE
[00:00:00.022,257] <inf> LD_DVFS_LIB: DVFS handler EVT_INIT_DONE handled
[00:00:00.022,266] <inf> LD_DVFS_LIB: DVFS init done.
[00:00:00.022,570] <inf> cdc_acm_echo: USBD message: VBUS ready
[00:00:00.054,340] <inf> cdc_acm_echo: USBD message: Device suspended
[00:00:00.220,251] <inf> cdc_acm_echo: USBD message: Bus reset
[00:00:00.600,531] <inf> cdc_acm_echo: USBD message: Bus reset
[00:00:00.674,722] <err> os: ***** BUS FAULT *****
[00:00:00.674,727] <err> os:   Precise data bus error
[00:00:00.674,729] <err> os:   BFAR Address: 0x0
[00:00:00.674,737] <err> os: r0/a1:  0x2f011b28  r1/a2:  0x2f017778  r2/a3:  0x00000000
[00:00:00.674,742] <err> os: r3/a4:  0x00000008 r12/ip:  0x2f0154fc r14/lr:  0x0e0ad411
[00:00:00.674,744] <err> os:  xpsr:  0x21000000
[00:00:00.674,750] <err> os: Faulting instruction address (r15/pc): 0x0e0ac7da
[00:00:00.674,761] <err> os: >>> ZEPHYR FATAL ERROR 25: Unknown error on CPU 0
[00:00:00.674,772] <err> os: Current thread: 0x2f012640 (usbd)
[00:00:00.798,732] <err> os: Halting system

Environment (please complete the following information):

  • OS: Linux
  • Toolchain: Zephyr SDK 0.16.5
  • Commit SHA or Version used: d45605e

Additional context
The same runtime issue happens when tests/subsys/usb/device_next is build with build_all.conf configuration.

Metadata

Metadata

Assignees

Labels

area: USBUniversal Serial BusbugThe issue is a bug, or the PR is fixing a bugpriority: lowLow impact/importance bug

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions