Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH v2] ALSA: caiaq: Serialize audio stream start and stop
@ 2026-12-05 17:19 YuanZhe Liu
  2026-12-06 11:07 ` Takashi Iwai
  0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-12-05 17:19 UTC (permalink / raw)
  To: Daniel Mack, Takashi Iwai
  Cc: Jaroslav Kysela, linux-sound, linux-kernel, Bin Yu, Cheng Wen

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@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
---
Changes in v2:
  - Rebase onto sound for-next; keep the caiaq MIDI lock and EP1
    mutex added since v7.3.5.

 sound/usb/caiaq/audio.c  | 8 +++++++-
 sound/usb/caiaq/device.c | 1 +
 sound/usb/caiaq/device.h | 1 +
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/sound/usb/caiaq/audio.c b/sound/usb/caiaq/audio.c
index c7d47f0833da..0af763c8b570 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;
 
@@ -858,6 +863,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 d28465e916df..804df963b839 100644
--- a/sound/usb/caiaq/device.c
+++ b/sound/usb/caiaq/device.c
@@ -457,6 +457,7 @@ static int create_card(struct usb_device *usb_dev,
 	spin_lock_init(&cdev->spinlock);
 	spin_lock_init(&cdev->midi_lock);
 	mutex_init(&cdev->ep1_out_mutex);
+	mutex_init(&cdev->stream_mutex);
 
 	*cardp = card;
 	return 0;
diff --git a/sound/usb/caiaq/device.h b/sound/usb/caiaq/device.h
index 16f65e7867d1..9acdf7637166 100644
--- a/sound/usb/caiaq/device.h
+++ b/sound/usb/caiaq/device.h
@@ -79,6 +79,7 @@ struct snd_usb_caiaqdev {
 	struct caiaq_device_spec spec;
 	spinlock_t spinlock;	/* for PCM audio */
 	spinlock_t midi_lock;	/* midi_x_stream, midi_out_active */
+	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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-06 11:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 17:19 [PATCH v2] ALSA: caiaq: Serialize audio stream start and stop YuanZhe Liu
2026-12-06 11:07 ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox