Skip to contentSkip to conversation

ALSA: usb-audio: qcom: move offload usage counting to stream request handler

PATCH Superseded Guan-Yu Lin 2 participants 3 messages Apr 17 – Apr 20 Sound

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: usb-audio: qcom: move offload usage counting to stream request handler

Centralize usb_offload_get()/usb_offload_put() calls from various helper functions to handle_uaudio_stream_req(), which ensures the usage count is tied directly to the QMI stream enable/dsiable requests from audio DSP. Such design provides a clear synchronization point for the offloading lifecycle and correctly balances the count for both playback and capture paths while avoiding redundant increments during auxiliary operations.

---
 sound/usb/qcom/qc_audio_offload.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
sound/usb/qcom/qc_audio_offload.c +10 -10
index 2ac813d57f4f..cddab55d4691 100644
@@ -699,7 +699,6 @@ static void uaudio_event_ring_cleanup_free(struct uaudio_dev *dev)
 		uaudio_iommu_unmap(MEM_EVENT_RING, IOVA_BASE, PAGE_SIZE,
 				   PAGE_SIZE);
 		xhci_sideband_remove_interrupter(uadev[dev->chip->card->number].sb);
-		usb_offload_put(dev->udev);
 	}
 }
 
@@ -1183,16 +1182,12 @@ static int uaudio_event_ring_setup(struct snd_usb_substream *subs,
 	dma_coherent = dev_is_dma_coherent(subs->dev->bus->sysdev);
 	er_pa = 0;
 
-	ret = usb_offload_get(subs->dev);
-	if (ret < 0)
-		goto exit;
-
 	/* event ring */
 	ret = xhci_sideband_create_interrupter(uadev[card_num].sb, 1, false,
 					       0, uaudio_qdev->data->intr_num);
 	if (ret < 0) {
 		dev_err(&subs->dev->dev, "failed to fetch interrupter\n");
-		goto put_offload;
+		goto exit;
 	}
 
 	sgt = xhci_sideband_get_event_buffer(uadev[card_num].sb);
@@ -1224,8 +1219,6 @@ static int uaudio_event_ring_setup(struct snd_usb_substream *subs,
 	mem_info->dma = 0;
 remove_interrupter:
 	xhci_sideband_remove_interrupter(uadev[card_num].sb);
-put_offload:
-	usb_offload_put(subs->dev);
 exit:
 	return ret;
 }
@@ -1489,7 +1482,6 @@ static int prepare_qmi_response(struct snd_usb_substream *subs,
 	uaudio_iommu_unmap(MEM_EVENT_RING, IOVA_BASE, PAGE_SIZE, PAGE_SIZE);
 free_sec_ring:
 	xhci_sideband_remove_interrupter(uadev[card_num].sb);
-	usb_offload_put(subs->dev);
 drop_sync_ep:
 	if (subs->sync_endpoint) {
 		uaudio_iommu_unmap(MEM_XFER_RING,
@@ -1605,6 +1597,12 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle,
 	uadev[pcm_card_num].ctrl_intf = chip->ctrl_intf;
 
 	if (req_msg->enable) {
+		ret = usb_offload_get(subs->dev);
+		if (ret < 0) {
+			guard(mutex)(&chip->mutex);
+			subs->opened = 0;
+			goto response;
+		}
 		ret = enable_audio_stream(subs,
 					  map_pcm_format(req_msg->audio_format),
 					  req_msg->number_of_ch, req_msg->bit_rate,
@@ -1658,9 +1656,11 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle,
 			uaudio_dev_intf_cleanup(uadev[pcm_card_num].udev,
 						info);
 		}
-		if (atomic_read(&uadev[pcm_card_num].in_use))
+		if (atomic_read(&uadev[pcm_card_num].in_use)) {
 			kref_put(&uadev[pcm_card_num].kref,
 				 uaudio_dev_release);
+			usb_offload_put(subs->dev);
+		}
 	}
 	mutex_unlock(&qdev_mutex);
 
Signature · 1 line
2.54.0.rc1.513.gad8abe7a5a-goog
#1 lore raw
Quoted 65 lines · Guan-Yu Lin

Centralize usb_offload_get()/usb_offload_put() calls from various helper functions to handle_uaudio_stream_req(), which ensures the usage count is tied directly to the QMI stream enable/dsiable requests from audio DSP. Such design provides a clear synchronization point for the offloading lifecycle and correctly balances the count for both playback and capture paths while avoiding redundant increments during auxiliary operations.

---
 sound/usb/qcom/qc_audio_offload.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
sound/usb/qcom/qc_audio_offload.c +1 -9
index 2ac813d57f4f..cddab55d4691 100644
@@ -699,7 +699,6 @@ static void uaudio_event_ring_cleanup_free(struct uaudio_dev *dev)
 		uaudio_iommu_unmap(MEM_EVENT_RING, IOVA_BASE, PAGE_SIZE,
 				   PAGE_SIZE);
 		xhci_sideband_remove_interrupter(uadev[dev->chip->card->number].sb);
-		usb_offload_put(dev->udev);
 	}
 }
 
@@ -1183,16 +1182,12 @@ static int uaudio_event_ring_setup(struct snd_usb_substream *subs,
 	dma_coherent = dev_is_dma_coherent(subs->dev->bus->sysdev);
 	er_pa = 0;
 
-	ret = usb_offload_get(subs->dev);
-	if (ret < 0)
-		goto exit;
-
 	/* event ring */
 	ret = xhci_sideband_create_interrupter(uadev[card_num].sb, 1, false,
 					       0, uaudio_qdev->data->intr_num);
 	if (ret < 0) {
 		dev_err(&subs->dev->dev, "failed to fetch interrupter\n");
-		goto put_offload;
+		goto exit;
 	}
 
 	sgt = xhci_sideband_get_event_buffer(uadev[card_num].sb);
@@ -1224,8 +1219,6 @@ static int uaudio_event_ring_setup(struct snd_usb_substream *subs,
 	mem_info->dma = 0;
 remove_interrupter:
 	xhci_sideband_remove_interrupter(uadev[card_num].sb);
-put_offload:
-	usb_offload_put(subs->dev);
 exit:
 	return ret;
 }
@@ -1489,7 +1482,6 @@ static int prepare_qmi_response(struct snd_usb_substream *subs,
 	uaudio_iommu_unmap(MEM_EVENT_RING, IOVA_BASE, PAGE_SIZE, PAGE_SIZE);
 free_sec_ring:
 	xhci_sideband_remove_interrupter(uadev[card_num].sb);
-	usb_offload_put(subs->dev);
 drop_sync_ep:
 	if (subs->sync_endpoint) {
 		uaudio_iommu_unmap(MEM_XFER_RING,
@@ -1605,6 +1597,12 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle,
 	uadev[pcm_card_num].ctrl_intf = chip->ctrl_intf;
 

sound/usb/qcom/qc_audio_offload.c static void handle_uaudio_stream_req(struct qmi_handle *handle, · line 1608

Quoted 10 lines
sound/usb/qcom/qc_audio_offload.c +6 -0
 	if (req_msg->enable) {
+		ret = usb_offload_get(subs->dev);
+		if (ret < 0) {
+			guard(mutex)(&chip->mutex);
+			subs->opened = 0;
+			goto response;
+		}
 		ret = enable_audio_stream(subs,
 					  map_pcm_format(req_msg->audio_format),
 					  req_msg->number_of_ch, req_msg->bit_rate,

I think the usb_offload_put() is missing at the error handling of the call above?

thanks,

Takashi

#2 lore raw
Quoted 38 lines · Takashi Iwai
Quoted 20 lines · Guan-Yu Lin

Centralize usb_offload_get()/usb_offload_put() calls from various helper functions to handle_uaudio_stream_req(), which ensures the usage count is tied directly to the QMI stream enable/dsiable requests from audio DSP. Such design provides a clear synchronization point for the offloading lifecycle and correctly balances the count for both playback and capture paths while avoiding redundant increments during auxiliary operations.

---
 sound/usb/qcom/qc_audio_offload.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
sound/usb/qcom/qc_audio_offload.c +0 -0
index 2ac813d57f4f..cddab55d4691 100644
@@ -1605,6 +1597,12 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle,
      uadev[pcm_card_num].ctrl_intf = chip->ctrl_intf;
 

sound/usb/qcom/qc_audio_offload.c static void handle_uaudio_stream_req(struct qmi_handle *handle, · line 1608

Quoted 10 lines
sound/usb/qcom/qc_audio_offload.c +6 -0
      if (req_msg->enable) {
+             ret = usb_offload_get(subs->dev);
+             if (ret < 0) {
+                     guard(mutex)(&chip->mutex);
+                     subs->opened = 0;
+                     goto response;
+             }
              ret = enable_audio_stream(subs,
                                        map_pcm_format(req_msg->audio_format),
                                        req_msg->number_of_ch, req_msg->bit_rate,

I think the usb_offload_put() is missing at the error handling of the call above?

Quoted 3 lines

thanks,

Takashi

Yes, I think we should add error handling in this case. Let me update it in the next version. Thanks.

Regards,
Guan-Yu