[PATCH v3 3/4] ALSA: usb-audio: qcom: tag sideband endpoints before ring allocation
From: Wesley Cheng
Date: Fri Sep 04 2026 - 03:05:11 EST
xhci_endpoint_init() picks the ring's segment pool based on whether the
endpoint has already been tagged via xhci_sideband_add_endpoint():
sideband-tagged endpoints get their ring allocated from the offload
client's own segment_pool instead of the shared xhci->segment_pool, so the
buffer reported to the ADSP over QMI is guaranteed to come from a page
meant to be ADSP-visible.
xhci_sideband_add_endpoint() must therefore run before the endpoint's
transfer ring is first allocated (i.e. before snd_usb_endpoint_prepare()
triggers xhci_endpoint_init()) for that pool selection to apply to the
first allocation. Move the xhci_sideband_add_endpoint() calls out of
uaudio_endpoint_setup() and into enable_audio_stream(), before
snd_usb_endpoint_prepare() is called for the data and sync endpoints,
and unwind them on the new error paths.
At that point in the setup sequence dev->ep_in[]/ep_out[] are not yet
populated, since the endpoint's altsetting has not been activated, so
usb_pipe_endpoint() cannot be used to find the usb_host_endpoint. Add
uaudio_find_host_endpoint(), which resolves it directly from the
interface's altsetting descriptor table instead.
Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Wesley Cheng <wesley.cheng@xxxxxxxxxxxxxxxx>
---
sound/usb/qcom/qc_audio_offload.c | 111 +++++++++++++++++++++++++++++++-------
1 file changed, 93 insertions(+), 18 deletions(-)
diff --git a/sound/usb/qcom/qc_audio_offload.c b/sound/usb/qcom/qc_audio_offload.c
index 1b8877b8ee62..f09dae8334d0 100644
--- a/sound/usb/qcom/qc_audio_offload.c
+++ b/sound/usb/qcom/qc_audio_offload.c
@@ -132,6 +132,7 @@ struct uaudio_dev {
/* xhci sideband */
struct xhci_sideband *sb;
+ struct dma_pool *segment_pool;
/* SoC USB device */
struct snd_soc_usb_device *sdev;
@@ -942,6 +943,45 @@ static void uaudio_dev_release(struct kref *kref)
wake_up(&dev->disconnect_wq);
}
+/**
+ * uaudio_find_host_endpoint() - look up usb_host_endpoint for a snd_usb_endpoint
+ * @subs: usb substream owning the target snd_usb_endpoint
+ * @endpoint: sync or data snd_usb_endpoint to resolve
+ *
+ * usb_pipe_endpoint() resolves via dev->ep_in[]/ep_out[], which are only
+ * populated once usb_set_interface() has activated the endpoint's altsetting
+ * (i.e. after snd_usb_endpoint_prepare() has run for it). Looking that up
+ * beforehand returns NULL.
+ *
+ * Instead, look the endpoint up directly in the interface's altsetting
+ * descriptor table, which is populated once at enumeration time and stays
+ * valid regardless of which altsetting is currently active.
+ *
+ * Return: matching usb_host_endpoint, or NULL if not found.
+ */
+static struct usb_host_endpoint *
+uaudio_find_host_endpoint(struct snd_usb_substream *subs,
+ struct snd_usb_endpoint *endpoint)
+{
+ struct usb_host_interface *alt;
+ struct usb_interface *iface;
+ int i;
+
+ iface = usb_ifnum_to_if(subs->dev, endpoint->iface);
+ if (!iface)
+ return NULL;
+
+ alt = usb_altnum_to_altsetting(iface, endpoint->altsetting);
+ if (!alt)
+ return NULL;
+
+ for (i = 0; i < alt->desc.bNumEndpoints; i++)
+ if (alt->endpoint[i].desc.bEndpointAddress == endpoint->ep_num)
+ return &alt->endpoint[i];
+
+ return NULL;
+}
+
/**
* enable_audio_stream() - enable usb snd endpoints
* @subs: usb substream
@@ -959,8 +999,9 @@ static void uaudio_dev_release(struct kref *kref)
static int enable_audio_stream(struct snd_usb_substream *subs,
snd_pcm_format_t pcm_format,
unsigned int channels, unsigned int cur_rate,
- int datainterval)
+ int datainterval, unsigned int card_num)
{
+ struct usb_host_endpoint *data_ep = NULL, *sync_ep = NULL;
struct snd_pcm_hw_params params;
struct snd_usb_audio *chip;
struct snd_interval *i;
@@ -998,17 +1039,49 @@ static int enable_audio_stream(struct snd_usb_substream *subs,
goto detach_ep;
}
+ data_ep = uaudio_find_host_endpoint(subs, subs->data_endpoint);
+ if (!data_ep) {
+ dev_err(&subs->dev->dev, "data ep # %d not found\n",
+ subs->data_endpoint->ep_num);
+ ret = -ENODEV;
+ goto detach_ep;
+ }
+
+ ret = xhci_sideband_add_endpoint(uadev[card_num].sb, data_ep,
+ uadev[card_num].segment_pool);
+ if (ret < 0) {
+ dev_err(&subs->dev->dev,
+ "failed to add data ep to sec intr: %d\n", ret);
+ goto detach_ep;
+ }
+
if (subs->sync_endpoint) {
+ sync_ep = uaudio_find_host_endpoint(subs, subs->sync_endpoint);
+ if (!sync_ep) {
+ dev_err(&subs->dev->dev, "sync ep # %d not found\n",
+ subs->sync_endpoint->ep_num);
+ ret = -ENODEV;
+ goto remove_data_ep;
+ }
+
+ ret = xhci_sideband_add_endpoint(uadev[card_num].sb, sync_ep,
+ uadev[card_num].segment_pool);
+ if (ret < 0) {
+ dev_err(&subs->dev->dev,
+ "failed to add sync ep to sec intr: %d\n", ret);
+ goto remove_data_ep;
+ }
+
ret = snd_usb_endpoint_prepare(chip, subs->sync_endpoint);
if (ret < 0)
- goto detach_ep;
+ goto remove_sync_ep;
}
ret = snd_usb_endpoint_prepare(chip, subs->data_endpoint);
if (ret < 0)
- goto detach_ep;
+ goto remove_sync_ep;
- dev_dbg(uaudio_qdev->data->dev,
+ dev_dbg(&subs->dev->dev,
"selected %s iface:%d altsetting:%d datainterval:%dus\n",
subs->direction ? "capture" : "playback",
subs->cur_audiofmt->iface, subs->cur_audiofmt->altsetting,
@@ -1020,6 +1093,11 @@ static int enable_audio_stream(struct snd_usb_substream *subs,
return 0;
+remove_sync_ep:
+ if (sync_ep)
+ xhci_sideband_remove_endpoint(uadev[card_num].sb, sync_ep);
+remove_data_ep:
+ xhci_sideband_remove_endpoint(uadev[card_num].sb, data_ep);
detach_ep:
snd_usb_hw_free(subs);
@@ -1141,14 +1219,6 @@ uaudio_endpoint_setup(struct snd_usb_substream *subs,
memcpy(ep_desc, &ep->desc, sizeof(ep->desc));
- ret = xhci_sideband_add_endpoint(uadev[card_num].sb, ep);
- if (ret < 0) {
- dev_err(&subs->dev->dev,
- "failed to add data ep to sec intr: %d\n", ret);
- ret = -ENODEV;
- goto exit;
- }
-
sgt = xhci_sideband_get_endpoint_buffer(uadev[card_num].sb, ep);
if (!sgt) {
dev_err(&subs->dev->dev,
@@ -1212,8 +1282,9 @@ static int uaudio_event_ring_setup(struct snd_usb_substream *subs,
goto exit;
/* event ring */
- ret = xhci_sideband_create_interrupter(uadev[card_num].sb, 1, false,
- 0, uaudio_qdev->data->intr_num);
+ ret = xhci_sideband_create_interrupter(uadev[card_num].sb, 1,
+ uadev[card_num].segment_pool,
+ false, 0, uaudio_qdev->data->intr_num);
if (ret < 0) {
dev_err(&subs->dev->dev, "failed to fetch interrupter\n");
goto put_offload;
@@ -1638,7 +1709,7 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle,
ret = enable_audio_stream(subs,
map_pcm_format(req_msg->audio_format),
req_msg->number_of_ch, req_msg->bit_rate,
- datainterval);
+ datainterval, pcm_card_num);
if (!ret)
ret = prepare_qmi_response(subs, req_msg, &resp,
@@ -1813,12 +1884,14 @@ static void qc_usb_audio_offload_probe(struct snd_usb_audio *chip)
if (!segment_pool)
goto free_sdev;
- sb = xhci_sideband_register(intf, XHCI_SIDEBAND_VENDOR, segment_pool,
+ sb = xhci_sideband_register(intf, XHCI_SIDEBAND_VENDOR,
uaudio_sideband_notifier);
if (!sb) {
dma_pool_destroy(segment_pool);
goto free_sdev;
}
+
+ uadev[chip->card->number].segment_pool = segment_pool;
} else {
sb = uadev[chip->card->number].sb;
sdev = uadev[chip->card->number].sdev;
@@ -1855,10 +1928,11 @@ static void qc_usb_audio_offload_probe(struct snd_usb_audio *chip)
return;
unreg_xhci:
- segment_pool = sb->segment_pool;
+ segment_pool = uadev[chip->card->number].segment_pool;
xhci_sideband_unregister(sb);
dma_pool_destroy(segment_pool);
uadev[chip->card->number].sb = NULL;
+ uadev[chip->card->number].segment_pool = NULL;
free_sdev:
kfree(sdev);
uadev[chip->card->number].sdev = NULL;
@@ -1918,12 +1992,13 @@ static void qc_usb_audio_offload_disconnect(struct snd_usb_audio *chip)
* This is to accommodate for devices w/ multiple UAC functions.
*/
if (chip->num_interfaces == 1) {
- struct dma_pool *segment_pool = dev->sb->segment_pool;
+ struct dma_pool *segment_pool = dev->segment_pool;
snd_soc_usb_disconnect(uaudio_qdev->auxdev->dev.parent, dev->sdev);
xhci_sideband_unregister(dev->sb);
dma_pool_destroy(segment_pool);
dev->sb = NULL;
+ dev->segment_pool = NULL;
dev->chip = NULL;
kfree(dev->sdev->ppcm_idx);
kfree(dev->sdev);
--
2.34.1