[PATCH] ALSA: caiaq: Serialize audio stream start and stop

From: YuanZhe Liu

Date: Sat Dec 05 2026 - 11:41:33 EST


The caiaq PCM driver shares a single "streaming" flag and one set of
isochronous URBs among all substreams, but stream_start() and
stream_stop() are not serialized. The PCM core allows prepare() to run
concurrently for the playback and capture substreams, and close() may
run concurrently as well.

stream_stop() clears cdev->streaming before it kills the URBs. A
concurrent prepare() then sees streaming == 0 and starts submitting the
same URBs while they are still active or being killed. This triggers
"URB ... submitted while active" in usb_submit_urb() and returns
-EBUSY, or -EPERM when the URB is being killed.

In addition, when stream_start() fails part-way, it only clears the
flag and leaves the already submitted URBs active. The following
stream_stop() becomes a no-op, and the next prepare() resubmits the
still-active URBs, hitting the same warning.

Serialize the stream state machine with a mutex and stop the stream
when a submit fails, following the same pattern as the usb6fire
driver.

Fixes: 523f1dce3743 ("[ALSA] Add Native Instrument usb audio device support")
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
---
sound/usb/caiaq/audio.c | 8 +++++++-
sound/usb/caiaq/device.c | 1 +
sound/usb/caiaq/device.h | 3 +++
3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/sound/usb/caiaq/audio.c b/sound/usb/caiaq/audio.c
index bb6280aa3533..fa1f592ae410 100644
--- a/sound/usb/caiaq/audio.c
+++ b/sound/usb/caiaq/audio.c
@@ -81,6 +81,8 @@ all_substreams_zero(struct snd_pcm_substream **subs)
return 1;
}

+static void stream_stop(struct snd_usb_caiaqdev *cdev);
+
static int stream_start(struct snd_usb_caiaqdev *cdev)
{
int i, ret;
@@ -104,7 +106,7 @@ static int stream_start(struct snd_usb_caiaqdev *cdev)
if (ret) {
dev_err(dev, "unable to trigger read #%d! (ret %d)\n",
i, ret);
- cdev->streaming = 0;
+ stream_stop(cdev);
return -EPIPE;
}
}
@@ -151,6 +153,7 @@ static int snd_usb_caiaq_substream_close(struct snd_pcm_substream *substream)
struct device *dev = caiaqdev_to_dev(cdev);

dev_dbg(dev, "%s(%p)\n", __func__, substream);
+ guard(mutex)(&cdev->stream_mutex);
if (all_substreams_zero(cdev->sub_playback) &&
all_substreams_zero(cdev->sub_capture)) {
/* when the last client has stopped streaming,
@@ -215,6 +218,8 @@ static int snd_usb_caiaq_pcm_prepare(struct snd_pcm_substream *substream)
cdev->audio_in_buf_pos[index] = in_pos;
}

+ guard(mutex)(&cdev->stream_mutex);
+
if (cdev->streaming)
return 0;

@@ -846,6 +851,7 @@ void snd_usb_caiaq_audio_disconnect(struct snd_usb_caiaqdev *cdev)
struct device *dev = caiaqdev_to_dev(cdev);

dev_dbg(dev, "%s(%p)\n", __func__, cdev);
+ guard(mutex)(&cdev->stream_mutex);
stream_stop(cdev);
}

diff --git a/sound/usb/caiaq/device.c b/sound/usb/caiaq/device.c
index f604846ec6e6..52d446af97a3 100644
--- a/sound/usb/caiaq/device.c
+++ b/sound/usb/caiaq/device.c
@@ -443,6 +443,7 @@ static int create_card(struct usb_device *usb_dev,
cdev->chip.usb_id = USB_ID(le16_to_cpu(usb_dev->descriptor.idVendor),
le16_to_cpu(usb_dev->descriptor.idProduct));
spin_lock_init(&cdev->spinlock);
+ mutex_init(&cdev->stream_mutex);

*cardp = card;
return 0;
diff --git a/sound/usb/caiaq/device.h b/sound/usb/caiaq/device.h
index 1c6f34693fa8..9255eae23514 100644
--- a/sound/usb/caiaq/device.h
+++ b/sound/usb/caiaq/device.h
@@ -2,6 +2,8 @@
#ifndef CAIAQ_DEVICE_H
#define CAIAQ_DEVICE_H

+#include <linux/mutex.h>
+
#include "../usbaudio.h"

#define USB_VID_NATIVEINSTRUMENTS 0x17cc
@@ -72,6 +74,7 @@ struct snd_usb_caiaqdev {

struct caiaq_device_spec spec;
spinlock_t spinlock;
+ struct mutex stream_mutex; /* protects the audio stream state */
wait_queue_head_t ep1_wait_queue;
wait_queue_head_t prepare_wait_queue;
int spec_received, audio_parm_answer;
--
2.34.1