Skip to content

Composite with Audio may cause Hard Fault #36

@aryeh-nigri

Description

@aryeh-nigri

Describe the bug
In composite mode, the AUDIO class uses pdev->classId inside USBD_AUDIO_Sync(). This is unsafe when USBD_AUDIO_Sync() is called from an async context (e.g. I2S DMA callback), because pdev->classId may currently point to another class (HID), leading to wrong pClassDataCmsit[]/pUserData[] indexing and a HardFault.

How To Reproduce

  1. Indicate the global behavior of your application project.

    • USB composite device: HID interface (like keyboard or mouse) and Audio.
    • Audio data is played via I2S with DMA.
    • DMA Half/Full transfer callbacks call USBD_AUDIO_Sync() to adjust/read audio buffer state and trigger AudioCmd().
  2. The modules that you suspect to be the cause of the problem (Driver, BSP, MW ...).

    • Class/AUDIO (specifically USBD_AUDIO_Sync()).
    • Also, general composite behavior because classId is used as a global context selector.
  3. The use case that generates the problem.

    • Start audio playback from Host to the device (USB Audio OUT streaming).
    • While audio is playing, generate HID traffic (move mouse / press keyboard keys).
    • Eventually (typically within a few seconds), the target hits HardFault_Handler.
  4. How we can reproduce the problem.
    Composite Audio + HID application example:
    https://github.com/STMicroelectronics/STM32CubeH7/tree/dev/usb/composite/Projects/STM32H743I-EVAL/Applications/USB_Device/Composite_Audio_HID

Additional context

  • Root cause: USBD_AUDIO_Sync() assumes it is always called with pdev->classId already set to the AUDIO instance. That assumption is not true when USBD_AUDIO_Sync() is invoked from non-USB asynchronous contexts (DMA callbacks).

  • Proposed fix idea: make USBD_AUDIO_Sync() (and any similar helper callable outside USB context) not depend on pdev->classId, similar to USBD_HID_SendReport.

If the intended usage is “USBD_AUDIO_Sync() must only be called from the AUDIO class callbacks (where classId is set by the core)”, it would help a lot to document that requirement clearly, because it’s very easy to call it from the audio DMA layer, and the failure mode is a hard crash.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinginternal bug trackerIssue confirmed and reported into a ticket in the internal bug tracking systemmwMiddleware-related issue or pull-request.st usbSTMicroelectronics' Universal Serial Bus (Host or Device) libraryusbUSB-related (host or device) issue or pull-request

Type

No fields configured for Bug.

Projects

Status

Analyzed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions