[v13,50/53] ALSA: usb-audio: Allow for rediscovery of connected USB SND devices

Message ID 20240203023645.31105-51-quic_wcheng@quicinc.com
State New
Headers
Series Introduce QC USB SND audio offloading support |

Commit Message

Wesley Cheng Feb. 3, 2024, 2:36 a.m. UTC
  In case of notifying SND platform drivers of connection events, some of
these use cases, such as offloading, require an ASoC USB backend device to
be initialized before the events can be handled.  If the USB backend device
has not yet been probed, this leads to missing initial USB audio device
connection events.

Expose an API that traverses the usb_chip array for connected devices, and
to call the respective connection callback registered to the SND platform
driver.

Signed-off-by: Wesley Cheng <quic_wcheng@quicinc.com>
---
 sound/usb/card.c                  | 19 +++++++++++++++++++
 sound/usb/card.h                  |  2 ++
 sound/usb/qcom/qc_audio_offload.c |  2 ++
 3 files changed, 23 insertions(+)
  

Comments

Amadeusz Sławiński Feb. 5, 2024, 9:01 a.m. UTC | #1
On 2/3/2024 3:36 AM, Wesley Cheng wrote:
> In case of notifying SND platform drivers of connection events, some of
> these use cases, such as offloading, require an ASoC USB backend device to
> be initialized before the events can be handled.  If the USB backend device
> has not yet been probed, this leads to missing initial USB audio device
> connection events.
> 
> Expose an API that traverses the usb_chip array for connected devices, and
> to call the respective connection callback registered to the SND platform
> driver.
> 
> Signed-off-by: Wesley Cheng <quic_wcheng@quicinc.com>
> ---
>   sound/usb/card.c                  | 19 +++++++++++++++++++
>   sound/usb/card.h                  |  2 ++
>   sound/usb/qcom/qc_audio_offload.c |  2 ++
>   3 files changed, 23 insertions(+)
> 
> diff --git a/sound/usb/card.c b/sound/usb/card.c
> index 11b827b7a2a5..995b2df676ab 100644
> --- a/sound/usb/card.c
> +++ b/sound/usb/card.c
> @@ -202,6 +202,25 @@ struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
>   }
>   EXPORT_SYMBOL_GPL(snd_usb_find_suppported_substream);
>   
> +/*
> + * in case the platform driver was not ready at the time of USB SND
> + * device connect, expose an API to discover all connected USB devices
> + * so it can populate any dependent resources/structures.
> + */
> +void snd_usb_rediscover_devices(void)
> +{
> +	int i;
> +
> +	mutex_lock(&register_mutex);
> +	for (i = 0; i < SNDRV_CARDS; i++) {
> +		if (usb_chip[i])
> +			if (platform_ops && platform_ops->connect_cb)
> +				platform_ops->connect_cb(usb_chip[i]);

if inside if, it can just be && or maybe move callback check before 
mutex lock and just return early if it is not present?

> +	}
> +	mutex_unlock(&register_mutex);
> +}
> +EXPORT_SYMBOL_GPL(snd_usb_rediscover_devices);
> +
>   /*
>    * disconnect streams
>    * called from usb_audio_disconnect()
> diff --git a/sound/usb/card.h b/sound/usb/card.h
> index 6d59995440c3..3a0d68f453a1 100644
> --- a/sound/usb/card.h
> +++ b/sound/usb/card.h
> @@ -222,11 +222,13 @@ int snd_usb_unregister_platform_ops(void);
>   #if IS_ENABLED(CONFIG_SND_USB_AUDIO)
>   struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
>   			struct snd_pcm_hw_params *params, int direction);
> +void snd_usb_rediscover_devices(void);
>   #else
>   static struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
>   			struct snd_pcm_hw_params *params, int direction)
>   {
>   	return NULL;
>   }
> +static void snd_usb_rediscover_devices(void) { }
>   #endif /* IS_ENABLED(CONFIG_SND_USB_AUDIO) */
>   #endif /* __USBAUDIO_CARD_H */
> diff --git a/sound/usb/qcom/qc_audio_offload.c b/sound/usb/qcom/qc_audio_offload.c
> index 08af82ec22ad..9b0f98600e58 100644
> --- a/sound/usb/qcom/qc_audio_offload.c
> +++ b/sound/usb/qcom/qc_audio_offload.c
> @@ -1867,6 +1867,8 @@ static int __init qc_usb_audio_offload_init(void)
>   	if (ret < 0)
>   		goto release_qmi;
>   
> +	snd_usb_rediscover_devices();
> +
>   	return 0;
>   
>   release_qmi:
>
  
Wesley Cheng Feb. 5, 2024, 9:27 p.m. UTC | #2
Hi Amadeusz,

On 2/5/2024 1:01 AM, Amadeusz Sławiński wrote:
> On 2/3/2024 3:36 AM, Wesley Cheng wrote:
>> In case of notifying SND platform drivers of connection events, some of
>> these use cases, such as offloading, require an ASoC USB backend 
>> device to
>> be initialized before the events can be handled.  If the USB backend 
>> device
>> has not yet been probed, this leads to missing initial USB audio device
>> connection events.
>>
>> Expose an API that traverses the usb_chip array for connected devices, 
>> and
>> to call the respective connection callback registered to the SND platform
>> driver.
>>
>> Signed-off-by: Wesley Cheng <quic_wcheng@quicinc.com>
>> ---
>>   sound/usb/card.c                  | 19 +++++++++++++++++++
>>   sound/usb/card.h                  |  2 ++
>>   sound/usb/qcom/qc_audio_offload.c |  2 ++
>>   3 files changed, 23 insertions(+)
>>
>> diff --git a/sound/usb/card.c b/sound/usb/card.c
>> index 11b827b7a2a5..995b2df676ab 100644
>> --- a/sound/usb/card.c
>> +++ b/sound/usb/card.c
>> @@ -202,6 +202,25 @@ struct snd_usb_stream 
>> *snd_usb_find_suppported_substream(int card_idx,
>>   }
>>   EXPORT_SYMBOL_GPL(snd_usb_find_suppported_substream);
>> +/*
>> + * in case the platform driver was not ready at the time of USB SND
>> + * device connect, expose an API to discover all connected USB devices
>> + * so it can populate any dependent resources/structures.
>> + */
>> +void snd_usb_rediscover_devices(void)
>> +{
>> +    int i;
>> +
>> +    mutex_lock(&register_mutex);
>> +    for (i = 0; i < SNDRV_CARDS; i++) {
>> +        if (usb_chip[i])
>> +            if (platform_ops && platform_ops->connect_cb)
>> +                platform_ops->connect_cb(usb_chip[i]);
> 
> if inside if, it can just be && or maybe move callback check before 
> mutex lock and just return early if it is not present?
> 

Thanks for pointing that out.  Makes sense, and will update it.

Thanks
Wesley Cheng
  
Takashi Iwai Feb. 6, 2024, 1 p.m. UTC | #3
On Mon, 05 Feb 2024 10:01:03 +0100,
Amadeusz Sławiński wrote:
> 
> On 2/3/2024 3:36 AM, Wesley Cheng wrote:
> > In case of notifying SND platform drivers of connection events, some of
> > these use cases, such as offloading, require an ASoC USB backend device to
> > be initialized before the events can be handled.  If the USB backend device
> > has not yet been probed, this leads to missing initial USB audio device
> > connection events.
> > 
> > Expose an API that traverses the usb_chip array for connected devices, and
> > to call the respective connection callback registered to the SND platform
> > driver.
> > 
> > Signed-off-by: Wesley Cheng <quic_wcheng@quicinc.com>
> > ---
> >   sound/usb/card.c                  | 19 +++++++++++++++++++
> >   sound/usb/card.h                  |  2 ++
> >   sound/usb/qcom/qc_audio_offload.c |  2 ++
> >   3 files changed, 23 insertions(+)
> > 
> > diff --git a/sound/usb/card.c b/sound/usb/card.c
> > index 11b827b7a2a5..995b2df676ab 100644
> > --- a/sound/usb/card.c
> > +++ b/sound/usb/card.c
> > @@ -202,6 +202,25 @@ struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
> >   }
> >   EXPORT_SYMBOL_GPL(snd_usb_find_suppported_substream);
> >   +/*
> > + * in case the platform driver was not ready at the time of USB SND
> > + * device connect, expose an API to discover all connected USB devices
> > + * so it can populate any dependent resources/structures.
> > + */
> > +void snd_usb_rediscover_devices(void)
> > +{
> > +	int i;
> > +
> > +	mutex_lock(&register_mutex);
> > +	for (i = 0; i < SNDRV_CARDS; i++) {
> > +		if (usb_chip[i])
> > +			if (platform_ops && platform_ops->connect_cb)
> > +				platform_ops->connect_cb(usb_chip[i]);
> 
> if inside if, it can just be && or maybe move callback check before
> mutex lock and just return early if it is not present?

The callback check must be inside mutex; otherwise you'll get a race
about the platform_ops registration.


thanks,

Takashi
  

Patch

diff --git a/sound/usb/card.c b/sound/usb/card.c
index 11b827b7a2a5..995b2df676ab 100644
--- a/sound/usb/card.c
+++ b/sound/usb/card.c
@@ -202,6 +202,25 @@  struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
 }
 EXPORT_SYMBOL_GPL(snd_usb_find_suppported_substream);
 
+/*
+ * in case the platform driver was not ready at the time of USB SND
+ * device connect, expose an API to discover all connected USB devices
+ * so it can populate any dependent resources/structures.
+ */
+void snd_usb_rediscover_devices(void)
+{
+	int i;
+
+	mutex_lock(&register_mutex);
+	for (i = 0; i < SNDRV_CARDS; i++) {
+		if (usb_chip[i])
+			if (platform_ops && platform_ops->connect_cb)
+				platform_ops->connect_cb(usb_chip[i]);
+	}
+	mutex_unlock(&register_mutex);
+}
+EXPORT_SYMBOL_GPL(snd_usb_rediscover_devices);
+
 /*
  * disconnect streams
  * called from usb_audio_disconnect()
diff --git a/sound/usb/card.h b/sound/usb/card.h
index 6d59995440c3..3a0d68f453a1 100644
--- a/sound/usb/card.h
+++ b/sound/usb/card.h
@@ -222,11 +222,13 @@  int snd_usb_unregister_platform_ops(void);
 #if IS_ENABLED(CONFIG_SND_USB_AUDIO)
 struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
 			struct snd_pcm_hw_params *params, int direction);
+void snd_usb_rediscover_devices(void);
 #else
 static struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
 			struct snd_pcm_hw_params *params, int direction)
 {
 	return NULL;
 }
+static void snd_usb_rediscover_devices(void) { }
 #endif /* IS_ENABLED(CONFIG_SND_USB_AUDIO) */
 #endif /* __USBAUDIO_CARD_H */
diff --git a/sound/usb/qcom/qc_audio_offload.c b/sound/usb/qcom/qc_audio_offload.c
index 08af82ec22ad..9b0f98600e58 100644
--- a/sound/usb/qcom/qc_audio_offload.c
+++ b/sound/usb/qcom/qc_audio_offload.c
@@ -1867,6 +1867,8 @@  static int __init qc_usb_audio_offload_init(void)
 	if (ret < 0)
 		goto release_qmi;
 
+	snd_usb_rediscover_devices();
+
 	return 0;
 
 release_qmi: