blue-debug opened a new pull request, #21621:
URL: https://github.com/apache/nuttx/pull/21621

   *Note: Please adhere to [Contributing 
Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   
   ## Summary
   
   This PR brings fixes to the audio upper half and the sim ALSA lower half 
from Vela's audio stack, plus `audio_unregister()`. Two of the bugs crash the 
sim today: closing a device while it is still playing, and a volume request on 
a host whose ALSA mixer has no usable volume control. The others leak memory on 
error paths or skip the lower half's stop(). Each change is its own commit:
   
   - audio, arch/sim: `sim_mixer_open()` returned 0 on every failure, so the 
sim registered its pcm devices with a NULL volume handle, and the first volume 
request (nxplayer sends one when it starts playing) aborted the sim in 
alsa-lib. A mixer that cannot be opened now makes `sim_audio_initialize()` 
fail, and `audio_register()` rejects a NULL name or lower half. A mixer without 
a "Master" or "Capture" element is not an error: the device is registered and 
volume requests return -ENOTTY. While here, fix the nxstyle errors in 
sim_alsa.c.
   - audio: `audio_register()` kept the upper half it had allocated when 
`register_driver()` failed.
   - audio: `audio_register()` no longer calls mkdir() for /dev/audio or 
CONFIG_AUDIO_DEV_PATH; `register_driver()` already creates the intermediate 
directories.
   - audio: add `audio_unregister()`, so that a lower half can undo 
`audio_register()`, for example when a later step of its initialization fails 
or its device goes away. It returns -EBUSY while the device is open, because 
open files still use the upper half.
   - audio: `audio_complete()` set the state to OPEN unconditionally, so when 
the lower half reported completion before the application issued AUDIOIOC_STOP, 
`audio_stop()` saw OPEN and never called the lower half's stop(). 
`audio_stop()` now moves to DRAINING before it calls stop(), and 
`audio_complete()` only moves DRAINING to OPEN, under the spinlock.
   - audio: the first `audio_open()` leaked `upper->status` when the private 
data could not be allocated, and the private data was not freed when the lock 
could not be taken.
   - audio: a lower half can complete a buffer after the last close has freed 
`upper->status`, e.g. from a DMA interrupt that was already pending; 
`audio_dequeuebuffer()` then dereferenced NULL. Such completions are now 
dropped.
   - Documentation/audio: describe `audio_unregister()`.
   
   ## Impact
   
   Affects: audio upper half (audio/audio.c, include/nuttx/audio/audio.h), the 
sim ALSA lower half, Documentation/os/audio.
   
   Behavior: `audio_register()` returns -EINVAL for a NULL name or lower half 
and frees its upper half on failure. On a sim host whose ALSA mixer cannot be 
opened the sim registers no audio devices instead of crashing on the first 
volume request; without a "Master" or "Capture" element, volume requests return 
-ENOTTY. AUDIOIOC_STOP reaches the lower half's stop() whenever the device is 
running or paused; the state returns to OPEN when the lower half then reports 
completion. Buffer completions after the last close are dropped.
   
   Compatibility: adds `audio_unregister()`; no struct, ioctl or Kconfig 
changes.
   
   ## Testing
   
   Simulator (sim:alsa, 64-bit: CONFIG_SIM_M32 disabled because the host has no 
32-bit libasound; CONFIG_DEBUG_AUDIO_ERROR enabled). Every commit builds on its 
own with apache/nuttx-apps master, the tip also builds with 
CONFIG_AUDIO_MULTI_SESSION and with `-Werror`, and checkpatch passes.
   
   The host ALSA configuration is replaced with `ALSA_CONFIG_PATH`: playback 
goes into a `file` plugin so the played data can be compared, capture reads 
from a file, and the control device is either a `remap` of the sound card that 
renames its "Master" and "Capture" controls, or a card that does not exist. A 
small test program (not part of this PR, built as `hello`) drives 
/dev/audio/pcm0p. Steps:
   
   - `hello closerun`: queue 4 buffers, START, close the device without STOP, 
wait 1.5 s, then reopen and play 16 buffers
   - `hello unreg`: `audio_register()` with NULL arguments, register a dummy 
lower half, `audio_unregister()` while it is open and after close, 50 
register/open/close/unregister rounds
   - `hello leak`: 100 open/REGISTERMQ/ALLOCBUFFER/FREEBUFFER/close rounds and 
10 full playback rounds, comparing the heap before and after
   - `hello vol` and nxplayer playing a 2 s WAV twice, with a mixer that has no 
"Master" element
   - boot with a mixer that cannot be opened
   
   Testing logs before change (master 8a345362637):
   
   ```
   nsh> hello closerun
   PASS: START with 4 buffers queued ret 0
   INFO: closed the device while it was running
   (sim exits with SIGSEGV; gdb:)
   #0  audio_dequeuebuffer (upper=0x7ffff3b11000, apb=0x7ffff3b247e0, status=0) 
at audio.c:1445
   1445      upper->status->tail++;
   #1  0x000000004008286d in audio_callback (handle=0x7ffff3b11000, reason=1, 
apb=0x7ffff3b247e0, status=0) at audio.c:1653
   #2  0x000000004001dc50 in sim_audio_process (priv=0x7ffff3b10f40) at 
sim/posix/sim_alsa.c:1019
   ...
   nxplayer> device /dev/audio/pcm0p
   nxplayer> play /host/t.wav
   nuttx: simple.c:270: snd_mixer_selem_has_playback_channel: Assertion `elem' 
failed.
   (sim exits with SIGABRT)
   ```
   
   Testing logs after change:
   
   ```
   nsh> uname -a
   NuttX 13.1.0 27c3dfb25a4 Nov 10 2026 13:56:53 sim sim
   nsh> hello closerun
   PASS: START with 4 buffers queued ret 0
   INFO: closed the device while it was running
   PASS: still alive 1.5 s after the last close
   PASS: after reopen: 16 of 16 buffers dequeued, ret 0
   PASS: after reopen: /host/alsa_played.raw is 131072 bytes, expected 131072, 
identical
   === audtest closerun: PASS (0 failed)
   nsh> hello unreg
   PASS: audio_register("ut0", NULL) ret -22
   PASS: audio_register(NULL, dev) ret -22
   PASS: audio_register("ut0") ret 0, /dev/audio/ut0 exists
   PASS: audio_unregister while open ret -16, node kept
   PASS: audio_unregister after close ret 0, node gone, dev->priv NULL
   PASS: 50 x register/open/close/unregister: heap 1941464 -> 1941464
   === audtest unreg: PASS (0 failed)
   nsh> hello leak
   PASS: 100 x open/REGISTERMQ/ALLOCBUFFER/FREEBUFFER/close: heap 1941640 -> 
1941640
   PASS: 10 x open/CONFIGURE/stream 16 buffers/STOP/close (0 bad): heap 1941640 
-> 1941640
   ...
   nsh> hello vol
   INFO: GETCAPS(FEATURE/VOLUME) ret -25
   INFO: CONFIGURE(FEATURE/VOLUME 500) ret -25
   nsh> nxplayer
   nxplayer> device /dev/audio/pcm0p
   nxplayer> play /host/t.wav
   nxplayer_setvolume: ERROR: AUDIOIOC_CONFIGURE ioctl failed: 25
   nxplayer> play /host/t.wav
   nxplayer_setvolume: ERROR: AUDIOIOC_CONFIGURE ioctl failed: 25
   nxplayer> q
   ...
   (boot with a mixer that cannot be opened)
   audio_register: ERROR: Invalid arguments
   audio_register: ERROR: Invalid arguments
   audio_register: ERROR: Invalid arguments
   audio_register: ERROR: Invalid arguments
   audio_register: ERROR: Invalid arguments
   nsh> ls /dev/audio
   nsh: ls: stat failed: 2
   ```
   
   Result: PASS. Closing a running device no longer crashes, and the device 
plays normally after reopening. The data ALSA received is byte-identical to the 
data the test queued, and after nxplayer to the WAV's PCM data. Without a 
"Master" element, volume requests fail with -ENOTTY and playback continues; 
with a mixer that cannot be opened, the sim boots without audio devices. The 
heap is unchanged after the open/close, playback and register/unregister rounds.
   
   Additional platform coverage:
   
   - BES platform (Cortex-M55): these changes are part of Vela's audio stack, 
where playback and recording go through them.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to