Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add is_isr parameter to dcd_edpt_xfer, dcd_edpt_xfer_fifo, usbd_edpt_xfer, and usbd_edpt_xfer_fifo functions #3313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Copilot wants to merge 9 commits into master
base: master
Choose a base branch
Loading
from copilot/fix-dcd-edpt-xfer-issue

Conversation

Copy link
Contributor

Copilot AI commented Oct 22, 2025
edited
Loading

Overview

This PR fixes issue #3311 by adding a bool is_isr parameter to both the low-level DCD functions (dcd_edpt_xfer() and dcd_edpt_xfer_fifo()) and the high-level wrapper functions (usbd_edpt_xfer() and usbd_edpt_xfer_fifo()). This allows both DCD drivers and class drivers to properly handle synchronization based on whether they are being called from ISR context or task context.

Problem

Previously, these functions hardcoded usbd_spin_lock(false) and usbd_spin_unlock(false), assuming they were always called from non-ISR (task) context. However, in some scenarios, these functions may need to be called from ISR context, which requires different locking behavior to avoid deadlocks or race conditions.

Solution

Added a bool is_isr parameter to both the DCD-level and USBD-level functions that indicates the calling context:

  • is_isr = false: Called from task context (normal application code, deferred processing)
  • is_isr = true: Called from ISR context (interrupt handlers)

This parameter is then passed through the call chain to usbd_spin_lock() and usbd_spin_unlock() to ensure proper synchronization.

Changes

API Updates

  • src/device/dcd.h: Updated DCD function declarations to include bool is_isr parameter
  • src/device/usbd_pvt.h: Updated USBD wrapper function declarations to include bool is_isr parameter
  • src/device/usbd.c: Updated implementations to accept and pass through the is_isr parameter

DCD Driver Updates

Updated all ~30 DCD driver implementations across multiple MCU families:

  • STM32, NXP, Nordic, Microchip, Renesas, TI, WCH, Raspberry Pi, Dialog, Nuvoton, Bridgetek, Sony, Mentor Graphics, ValentyUSB, and others
  • Modified usbd_spin_lock()/usbd_spin_unlock() calls to use the is_isr parameter
  • Added (void) is_isr; to suppress unused parameter warnings where appropriate

Class Driver Updates

Updated all class drivers to pass the appropriate is_isr value:

  • Most callers pass false (task context)
  • ISR functions like audiod_rx_xfer_isr() and audiod_tx_xfer_isr() pass true (ISR context)
  • Updated files: audio, CDC, HID, MSC, MTP, MIDI, USBTMC, NCM, ECM/RNDIS, video, BTH device classes

Test Updates

  • Updated unit test mocks to expect the new parameter
  • All 21 unit tests pass

Validation

  • ✅ All unit tests pass (21/21)
  • ✅ STM32F4 CDC_MSC example builds successfully
  • ✅ No compilation errors across tested platforms

Example Usage

// DCD level - called from task context (e.g., in usbd.c)
dcd_edpt_xfer(rhport, ep_addr, buffer, total_bytes, false);
// DCD level - called from ISR context (future use)
dcd_edpt_xfer(rhport, ep_addr, buffer, total_bytes, true);
// USBD level - called from task context (most class drivers)
usbd_edpt_xfer(rhport, ep_addr, buffer, total_bytes, false);
// USBD level - called from ISR context (e.g., in audiod_rx_xfer_isr)
usbd_edpt_xfer(rhport, ep_addr, buffer, total_bytes, true);

Fixes #3311

Original prompt

Fix issue #3311 by adding is_isr argument to dcd_edpt_xfer and dcd_edpt_xfer_fifo. If they are called in isr context then set is_isr to true otherwise false.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 5 commits October 22, 2025 05:21
Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
Copilot AI changed the title (削除) [WIP] Fix issue #3311 by adding is_isr argument to transfer functions (削除ここまで) (追記) Add is_isr parameter to dcd_edpt_xfer and dcd_edpt_xfer_fifo functions (追記ここまで) Oct 22, 2025
Copilot AI requested a review from HiFiPhile October 22, 2025 05:35
Copilot finished work on behalf of HiFiPhile October 22, 2025 05:35
Copy link
Collaborator

@copilot

  • revert all submodule changes
  • Add is_isr argument to usbd_edpt_xfer and usbd_edpt_xfer_fifo, set it to true if called in ISR, for example audiod_rx_xfer_isr

- Added bool is_isr parameter to usbd_edpt_xfer() and usbd_edpt_xfer_fifo() wrapper functions
- These are called by class drivers to queue USB transfers
- Updated all callers to pass false by default (non-ISR context)
- Updated audiod_rx_xfer_isr() and audiod_tx_xfer_isr() to pass true (ISR context)
- All 21 unit tests pass
Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 22, 2025

@copilot

  • revert all submodule changes
  • Add is_isr argument to usbd_edpt_xfer and usbd_edpt_xfer_fifo, set it to true if called in ISR, for example audiod_rx_xfer_isr

Added is_isr parameter to usbd_edpt_xfer() and usbd_edpt_xfer_fifo() wrapper functions. ISR functions like audiod_rx_xfer_isr() and audiod_tx_xfer_isr() now pass true, while all other callers pass false. Submodule changes will be reverted in next commit. Commit: 27415e6

Copilot AI changed the title (削除) Add is_isr parameter to dcd_edpt_xfer and dcd_edpt_xfer_fifo functions (削除ここまで) (追記) Add is_isr parameter to dcd_edpt_xfer, dcd_edpt_xfer_fifo, usbd_edpt_xfer, and usbd_edpt_xfer_fifo functions (追記ここまで) Oct 22, 2025
Copilot finished work on behalf of HiFiPhile October 22, 2025 06:13
@HiFiPhile HiFiPhile force-pushed the copilot/fix-dcd-edpt-xfer-issue branch 4 times, most recently from f06b05d to 4143243 Compare October 22, 2025 15:11
Signed-off-by: Mengsk <admin@hifiphile.com>
@HiFiPhile HiFiPhile force-pushed the copilot/fix-dcd-edpt-xfer-issue branch from 4143243 to 30132e8 Compare October 22, 2025 18:22
@HiFiPhile HiFiPhile marked this pull request as ready for review October 22, 2025 18:57
@Copilot Copilot AI review requested due to automatic review settings October 22, 2025 18:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a bool is_isr parameter to the DCD and USBD transfer functions (dcd_edpt_xfer, dcd_edpt_xfer_fifo, usbd_edpt_xfer, and usbd_edpt_xfer_fifo) to indicate whether the functions are being called from ISR or task context. This enables proper synchronization handling by allowing the locking mechanism to differentiate between interrupt and non-interrupt contexts.

Key changes:

  • Updated function signatures across DCD layer, USBD layer, and class drivers
  • Modified locking calls in DWC2 driver to use the new is_isr parameter
  • Most callers pass false (task context); ISR functions like audio callbacks pass true

Reviewed Changes

Copilot reviewed 49 out of 49 changed files in this pull request and generated 30 comments.

Show a summary per file
File Description
src/device/dcd.h Added is_isr parameter to DCD function declarations
src/device/usbd_pvt.h Added is_isr parameter to USBD wrapper declarations
src/device/usbd.c Updated implementations to accept and propagate is_isr parameter
src/device/usbd_control.c Updated control transfer calls to pass false for task context
src/portable/synopsys/dwc2/dcd_dwc2.c Modified to use is_isr parameter in spin lock/unlock calls
src/portable//dcd_.c (30+ files) Added is_isr parameter with (void) is_isr; suppression
src/class/audio/audio_device.c Updated ISR callbacks to pass true; non-ISR calls pass false
src/class/*/[device].c (10+ files) Updated all transfer calls to pass false for task context
test/unit-test/test/device/usbd/test_usbd.c Updated mock expectations to include new parameter
test/unit-test/test/device/msc/test_msc_device.c Updated mock expectations to include new parameter
test/fuzz/dcd_fuzz.cc Added parameter with unused suppression
src/tusb.c Updated stream transfer call to pass false

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot Copilot left review comments

+1 more reviewer

@HiFiPhile HiFiPhile HiFiPhile approved these changes

Reviewers whose approvals may not affect merge requirements

At least 1 approving review is required to merge this pull request.

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

DCD DWC2 dcd_edpt_xfer() called from ISR hangs on spinlock

2 participants

AltStyle によって変換されたページ (->オリジナル) /