[PATCH v3 19/28] sound: usb: Prevent starting of audio stream if in use

From: Wesley Cheng
Date: Wed Mar 08 2023 - 19:00:07 EST


With USB audio offloading, an audio session is started from the ASoC
platform sound card and PCM devices. Likewise, the USB SND path is still
readily available for use, in case the non-offload path is desired. In
order to prevent the two entities from attempting to use the USB bus,
introduce a flag that determines when either paths are in use.

If a PCM device is already in use, the check will return an error to
userspace notifying that the stream is currently busy. This ensures that
only one path is using the USB substream.

Signed-off-by: Wesley Cheng <quic_wcheng@xxxxxxxxxxx>
---
sound/usb/card.h | 1 +
sound/usb/pcm.c | 19 +++++++++++++++++--
sound/usb/qcom/qc_audio_offload.c | 15 ++++++++++++++-
3 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/sound/usb/card.h b/sound/usb/card.h
index 4535b48edd1b..922868d2dccd 100644
--- a/sound/usb/card.h
+++ b/sound/usb/card.h
@@ -164,6 +164,7 @@ struct snd_usb_substream {
unsigned int pkt_offset_adj; /* Bytes to drop from beginning of packets (for non-compliant devices) */
unsigned int stream_offset_adj; /* Bytes to drop from beginning of stream (for non-compliant devices) */

+ unsigned int opened:1; /* pcm device opened */
unsigned int running: 1; /* running status */
unsigned int period_elapsed_pending; /* delay period handling */

diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c
index 482c160cbc17..c8590ff78d11 100644
--- a/sound/usb/pcm.c
+++ b/sound/usb/pcm.c
@@ -1208,8 +1208,15 @@ static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
struct snd_pcm_runtime *runtime = substream->runtime;
struct snd_usb_substream *subs = &as->substream[direction];
+ struct snd_usb_audio *chip = subs->stream->chip;
int ret;

+ mutex_lock(&chip->mutex);
+ if (subs->opened) {
+ mutex_unlock(&chip->mutex);
+ return -EBUSY;
+ }
+
runtime->hw = snd_usb_hardware;
/* need an explicit sync to catch applptr update in low-latency mode */
if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
@@ -1226,13 +1233,17 @@ static int snd_usb_pcm_open(struct snd_pcm_substream *substream)

ret = setup_hw_info(runtime, subs);
if (ret < 0)
- return ret;
+ goto out;
ret = snd_usb_autoresume(subs->stream->chip);
if (ret < 0)
- return ret;
+ goto out;
ret = snd_media_stream_init(subs, as->pcm, direction);
if (ret < 0)
snd_usb_autosuspend(subs->stream->chip);
+ subs->opened = 1;
+out:
+ mutex_unlock(&chip->mutex);
+
return ret;
}

@@ -1241,6 +1252,7 @@ static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
int direction = substream->stream;
struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
struct snd_usb_substream *subs = &as->substream[direction];
+ struct snd_usb_audio *chip = subs->stream->chip;
int ret;

snd_media_stop_pipeline(subs);
@@ -1254,6 +1266,9 @@ static int snd_usb_pcm_close(struct snd_pcm_substream *substream)

subs->pcm_substream = NULL;
snd_usb_autosuspend(subs->stream->chip);
+ mutex_lock(&chip->mutex);
+ subs->opened = 0;
+ mutex_unlock(&chip->mutex);

return 0;
}
diff --git a/sound/usb/qcom/qc_audio_offload.c b/sound/usb/qcom/qc_audio_offload.c
index 2663906644f2..2c8dd5605849 100644
--- a/sound/usb/qcom/qc_audio_offload.c
+++ b/sound/usb/qcom/qc_audio_offload.c
@@ -1402,12 +1402,17 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle,
goto response;
}

+ mutex_lock(&chip->mutex);
if (req_msg->enable) {
- if (info_idx < 0 || chip->system_suspend) {
+ if (info_idx < 0 || chip->system_suspend || subs->opened) {
ret = -EBUSY;
+ mutex_unlock(&chip->mutex);
+
goto response;
}
+ subs->opened = 1;
}
+ mutex_unlock(&chip->mutex);

if (req_msg->service_interval_valid) {
ret = get_data_interval_from_si(subs,
@@ -1429,6 +1434,11 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle,
if (!ret)
ret = prepare_qmi_response(subs, req_msg, &resp,
info_idx);
+ if (ret < 0) {
+ mutex_lock(&chip->mutex);
+ subs->opened = 0;
+ mutex_unlock(&chip->mutex);
+ }
} else {
info = &uadev[pcm_card_num].info[info_idx];
if (info->data_ep_pipe) {
@@ -1450,6 +1460,9 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle,
}

disable_audio_stream(subs);
+ mutex_lock(&chip->mutex);
+ subs->opened = 0;
+ mutex_unlock(&chip->mutex);
}

response: