diff mbox series

[RFC,04/14] sound: usb: card: Introduce USB SND vendor op callbacks

Message ID 20221223233200.26089-5-quic_wcheng@quicinc.com
State New
Headers show
Series Introduce QC USB SND audio offloading support | expand

Commit Message

Wesley Cheng Dec. 23, 2022, 11:31 p.m. UTC
Allow for different vendors to be notified on USB SND connect/disconnect
seqeunces.  This allows for vendor USB SND modules to properly initialize
and populate internal structures with references to the USB SND chip
device.

Signed-off-by: Wesley Cheng <quic_wcheng@quicinc.com>
---
 sound/usb/card.c | 22 ++++++++++++++++++++++
 sound/usb/card.h |  7 +++++++
 2 files changed, 29 insertions(+)

Comments

Dmitry Baryshkov Dec. 24, 2022, 11:03 a.m. UTC | #1
Hi,

On Sat, 24 Dec 2022 at 01:33, Wesley Cheng <quic_wcheng@quicinc.com> wrote:
>
> Allow for different vendors to be notified on USB SND connect/disconnect
> seqeunces.  This allows for vendor USB SND modules to properly initialize
> and populate internal structures with references to the USB SND chip
> device.

The commit message definitely needs some improvement. We do not notify
vendors on SND connect/disconnect events.


>
> Signed-off-by: Wesley Cheng <quic_wcheng@quicinc.com>
> ---
>  sound/usb/card.c | 22 ++++++++++++++++++++++
>  sound/usb/card.h |  7 +++++++
>  2 files changed, 29 insertions(+)
>
> diff --git a/sound/usb/card.c b/sound/usb/card.c
> index 26268ffb8274..212f55a7683c 100644
> --- a/sound/usb/card.c
> +++ b/sound/usb/card.c
> @@ -117,6 +117,21 @@ MODULE_PARM_DESC(skip_validation, "Skip unit descriptor validation (default: no)
>  static DEFINE_MUTEX(register_mutex);
>  static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
>  static struct usb_driver usb_audio_driver;
> +static struct snd_usb_vendor_ops *vendor_ops;
> +
> +int snd_usb_register_vendor_ops(struct snd_usb_vendor_ops *ops)

platform ops?

> +{
> +       vendor_ops = ops;
> +       return 0;
> +}
> +EXPORT_SYMBOL_GPL(snd_usb_register_vendor_ops);

What happens if several platforms try to register different ops? I saw
from the patch 09/14 that you register these ops unconditionally. If
other devices follow your approach there is an obvious conflict.

> +
> +int snd_usb_unregister_vendor_ops(void)
> +{
> +       vendor_ops = NULL;
> +       return 0;
> +}
> +EXPORT_SYMBOL_GPL(snd_usb_unregister_vendor_ops);
>
>  /*
>   * disconnect streams
> @@ -910,6 +925,10 @@ static int usb_audio_probe(struct usb_interface *intf,
>         usb_set_intfdata(intf, chip);
>         atomic_dec(&chip->active);
>         mutex_unlock(&register_mutex);
> +
> +       if (vendor_ops->connect_cb)
> +               vendor_ops->connect_cb(intf, chip);
> +
>         return 0;
>
>   __error:
> @@ -943,6 +962,9 @@ static void usb_audio_disconnect(struct usb_interface *intf)
>         if (chip == USB_AUDIO_IFACE_UNUSED)
>                 return;
>
> +       if (vendor_ops->disconnect_cb)
> +               vendor_ops->disconnect_cb(intf);
> +
>         card = chip->card;
>
>         mutex_lock(&register_mutex);
> diff --git a/sound/usb/card.h b/sound/usb/card.h
> index 40061550105a..a785bb256b0d 100644
> --- a/sound/usb/card.h
> +++ b/sound/usb/card.h
> @@ -206,4 +206,11 @@ struct snd_usb_stream {
>         struct list_head list;
>  };
>
> +struct snd_usb_vendor_ops {
> +       void (*connect_cb)(struct usb_interface *intf, struct snd_usb_audio *chip);
> +       void (*disconnect_cb)(struct usb_interface *intf);
> +};
> +
> +int snd_usb_register_vendor_ops(struct snd_usb_vendor_ops *ops);
> +int snd_usb_unregister_vendor_ops(void);
>  #endif /* __USBAUDIO_CARD_H */



--
With best wishes

Dmitry
Wesley Cheng Dec. 27, 2022, 9:07 p.m. UTC | #2
Hi Dmitry,

On 12/24/2022 3:03 AM, Dmitry Baryshkov wrote:
> Hi,
> 
> On Sat, 24 Dec 2022 at 01:33, Wesley Cheng <quic_wcheng@quicinc.com> wrote:
>>
>> Allow for different vendors to be notified on USB SND connect/disconnect
>> seqeunces.  This allows for vendor USB SND modules to properly initialize
>> and populate internal structures with references to the USB SND chip
>> device.
> 
> The commit message definitely needs some improvement. We do not notify
> vendors on SND connect/disconnect events.
> 
> 
>>
>> Signed-off-by: Wesley Cheng <quic_wcheng@quicinc.com>
>> ---
>>   sound/usb/card.c | 22 ++++++++++++++++++++++
>>   sound/usb/card.h |  7 +++++++
>>   2 files changed, 29 insertions(+)
>>
>> diff --git a/sound/usb/card.c b/sound/usb/card.c
>> index 26268ffb8274..212f55a7683c 100644
>> --- a/sound/usb/card.c
>> +++ b/sound/usb/card.c
>> @@ -117,6 +117,21 @@ MODULE_PARM_DESC(skip_validation, "Skip unit descriptor validation (default: no)
>>   static DEFINE_MUTEX(register_mutex);
>>   static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
>>   static struct usb_driver usb_audio_driver;
>> +static struct snd_usb_vendor_ops *vendor_ops;
>> +
>> +int snd_usb_register_vendor_ops(struct snd_usb_vendor_ops *ops)
> 
> platform ops?
> 

Will change it.

>> +{
>> +       vendor_ops = ops;
>> +       return 0;
>> +}
>> +EXPORT_SYMBOL_GPL(snd_usb_register_vendor_ops);
> 
> What happens if several platforms try to register different ops? I saw
> from the patch 09/14 that you register these ops unconditionally. If
> other devices follow your approach there is an obvious conflict.
> 

Thank you for the review.

That is true.  I don't think there is a proper need to have multiple 
vendor ops being registered, so maybe just returning an error for if ops 
are already registered is sufficient.

Thanks
Wesley Cheng
Dmitry Baryshkov Dec. 27, 2022, 9:33 p.m. UTC | #3
On 27/12/2022 23:07, Wesley Cheng wrote:
> Hi Dmitry,
> 
> On 12/24/2022 3:03 AM, Dmitry Baryshkov wrote:
>> Hi,
>>
>> On Sat, 24 Dec 2022 at 01:33, Wesley Cheng <quic_wcheng@quicinc.com> 
>> wrote:
>>>
>>> Allow for different vendors to be notified on USB SND connect/disconnect
>>> seqeunces.  This allows for vendor USB SND modules to properly 
>>> initialize
>>> and populate internal structures with references to the USB SND chip
>>> device.
>>
>> The commit message definitely needs some improvement. We do not notify
>> vendors on SND connect/disconnect events.
>>
>>
>>>
>>> Signed-off-by: Wesley Cheng <quic_wcheng@quicinc.com>
>>> ---
>>>   sound/usb/card.c | 22 ++++++++++++++++++++++
>>>   sound/usb/card.h |  7 +++++++
>>>   2 files changed, 29 insertions(+)
>>>
>>> diff --git a/sound/usb/card.c b/sound/usb/card.c
>>> index 26268ffb8274..212f55a7683c 100644
>>> --- a/sound/usb/card.c
>>> +++ b/sound/usb/card.c
>>> @@ -117,6 +117,21 @@ MODULE_PARM_DESC(skip_validation, "Skip unit 
>>> descriptor validation (default: no)
>>>   static DEFINE_MUTEX(register_mutex);
>>>   static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
>>>   static struct usb_driver usb_audio_driver;
>>> +static struct snd_usb_vendor_ops *vendor_ops;
>>> +
>>> +int snd_usb_register_vendor_ops(struct snd_usb_vendor_ops *ops)
>>
>> platform ops?
>>
> 
> Will change it.
> 
>>> +{
>>> +       vendor_ops = ops;
>>> +       return 0;
>>> +}
>>> +EXPORT_SYMBOL_GPL(snd_usb_register_vendor_ops);
>>
>> What happens if several platforms try to register different ops? I saw
>> from the patch 09/14 that you register these ops unconditionally. If
>> other devices follow your approach there is an obvious conflict.
>>
> 
> Thank you for the review.
> 
> That is true.  I don't think there is a proper need to have multiple 
> vendor ops being registered, so maybe just returning an error for if ops 
> are already registered is sufficient.

This would be a required step. And also you have to check the running 
platform before registering your ops unconditionally. Ideally this 
should be done as a part of the device's driver, so that we can control 
registration of the platform ops using the usual interface.

> 
> Thanks
> Wesley Cheng
Oliver Neukum Dec. 29, 2022, 1:49 p.m. UTC | #4
On 24.12.22 00:31, Wesley Cheng wrote:
> Allow for different vendors to be notified on USB SND connect/disconnect
> seqeunces.  This allows for vendor USB SND modules to properly initialize
> and populate internal structures with references to the USB SND chip
> device.

Hi,

this raises a design question. If the system is suspending or, worse,
hibernating, how do you make sure the offloader and the device are
suspended in the correct order?
And what happens if you need to go into reset_resume() when resuming?

	Regards
		Oliver
Takashi Iwai Dec. 29, 2022, 2:20 p.m. UTC | #5
On Thu, 29 Dec 2022 14:49:21 +0100,
Oliver Neukum wrote:
> 
> 
> 
> On 24.12.22 00:31, Wesley Cheng wrote:
> > Allow for different vendors to be notified on USB SND connect/disconnect
> > seqeunces.  This allows for vendor USB SND modules to properly initialize
> > and populate internal structures with references to the USB SND chip
> > device.
> 
> Hi,
> 
> this raises a design question. If the system is suspending or, worse,
> hibernating, how do you make sure the offloader and the device are
> suspended in the correct order?
> And what happens if you need to go into reset_resume() when resuming?

I guess we'd need to establish a device link when the binding from the
offload driver is done.  Then the PM order will be assured.


Takashi
Wesley Cheng Dec. 30, 2022, 7:10 a.m. UTC | #6
Hi,

On 12/29/2022 6:20 AM, Takashi Iwai wrote:
> On Thu, 29 Dec 2022 14:49:21 +0100,
> Oliver Neukum wrote:
>>
>>
>>
>> On 24.12.22 00:31, Wesley Cheng wrote:
>>> Allow for different vendors to be notified on USB SND connect/disconnect
>>> seqeunces.  This allows for vendor USB SND modules to properly initialize
>>> and populate internal structures with references to the USB SND chip
>>> device.
>>
>> Hi,
>>
>> this raises a design question. If the system is suspending or, worse,
>> hibernating, how do you make sure the offloader and the device are
>> suspended in the correct order?
>> And what happens if you need to go into reset_resume() when resuming?

It may depend on how the offloading is implemented, but we do have a 
mechanism to force the audio stream off from the qc_usb_audio_offload. 
Regardless of if the UDEV is suspended first, or the USB backend, as 
long as we ensure that the offloading is disabled before entering 
suspend, I think that should be sufficient.  I would need to add some 
suspend handling in the offload driver to issue the command to stop the 
offloading.

As for the resume path, is there a concern if either device is resumed 
first?  The only scenario where maybe it could cause some mishandling is 
if the USB backend is resumed before the offload driver is 
connected/resumed.  This means that the userspace ALSA would have access 
to the platform sound card, and could potentially attempt to route audio 
streams to it.  I think in worst case, if we were going through a 
reset_resume() we would end up rejecting that request coming from the 
audio DSP to enable the stream.  However, userspace entities would be 
resumed/unfrozen last, so not sure if that would ever be a problem.

The reset_resume() path is fine.  Bus reset is going to cause a 
disconnect() callback in the offload driver, in which we already have 
the proper handling for ensuring the offload path is halted, and we 
reject any incoming stream start requests.

Thanks
Wesley Cheng
Oliver Neukum Jan. 3, 2023, 12:20 p.m. UTC | #7
On 30.12.22 08:10, Wesley Cheng wrote:

> It may depend on how the offloading is implemented, but we do have a mechanism to force the audio stream off from the qc_usb_audio_offload. Regardless of if the UDEV is suspended first, or the USB backend, as long as we ensure that the offloading is disabled before entering suspend, I think that should be sufficient.

You would presumably output garbage, if the UDEV is asleep but the backend is not.

  
> The reset_resume() path is fine.  Bus reset is going to cause a disconnect() callback in the offload driver, in which we already have the proper handling for ensuring the offload path is halted, and we reject any incoming stream start requests.

How? If we go the reset_resume() code path, we find that usb-audio does not make
a difference between regular resume() and reset_resume()

	Regards
		Oliver
Wesley Cheng Jan. 3, 2023, 11:45 p.m. UTC | #8
Hi Oliver,

On 1/3/2023 4:49 AM, Takashi Iwai wrote:
> On Tue, 03 Jan 2023 13:20:48 +0100,
> Oliver Neukum wrote:
>>
>>
>>
>> On 30.12.22 08:10, Wesley Cheng wrote:
>>
>>> It may depend on how the offloading is implemented, but we do have a mechanism to force the audio stream off from the qc_usb_audio_offload. Regardless of if the UDEV is suspended first, or the USB backend, as long as we ensure that the offloading is disabled before entering suspend, I think that should be sufficient.
>>
>> You would presumably output garbage, if the UDEV is asleep but the backend is not.
>>

As long as the stream is halted, i.e. the audio DSP doesn't execute 
further transfers on the bus, there shouldn't be any noise/static that 
will continue to be outputted.  When I mentioned that we have a 
mechanism to force for the offloading to be disabled to the audio DSP 
side, it will no longer submit any audio data to the USB bus.

>>   
>>> The reset_resume() path is fine.  Bus reset is going to cause a disconnect() callback in the offload driver, in which we already have the proper handling for ensuring the offload path is halted, and we reject any incoming stream start requests.
>>
>> How? If we go the reset_resume() code path, we find that usb-audio does not make
>> a difference between regular resume() and reset_resume()
> 
> Note that, for USB audio, there is no much difference between resume()
> and reset_resume(), especially about the PCM stream handling that is
> the main target for the offload (the mixer isn't handled there).
> And for the PCM, we just set the power state for UAC3, and that's
> all.  All the rest is handled by the PCM core handler as usual.
> 

Sorry, I was under the impression that the USB SND class driver didn't 
register a reset_resume() callback, which Takashi helped clarify that it 
does indeed do.  (if no callback is registered, then USB interface is 
re-binded in the resume path)  However, it doesn't explicitly treat the 
reset_resume differently than a normal resume.

For the offload path, we don't need to do anything special either - if 
we have ensured that the stream was stopped in the suspend path. (to be 
added) It would be up to the userspace to restart the ASoC PCM stream, 
which would cause another stream request enable QMI command handshake to 
happen before any transfers would start.

One thing that I could add to protect the situation where the USB ASoC 
backend is resumed before UDEV, is to check the chip->system_suspend 
state.  Normally, the offload driver needs to ensure the bus is in U0 
before starting the audio stream, but it is done using runtime PM:

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)
{
...
	pm_runtime_barrier(&chip->intf[0]->dev);
	snd_usb_autoresume(chip);

In case we're in the PM resume path, I don't believe PM runtime can be 
triggered to resume a device.  In this case, the snd_usb_autoresume() 
would return w/o ensuring the USB SND device is fully exited from PM 
suspend.  Although, this situation would be a corner case, as userspace 
entities (userspace ALSA) are going to be unfrozen after kernel devices 
are resumed, so most likely there should be no request to enable the 
audio stream if kernel devices are still resuming.

I don't see an issue with the sequence of UDEV being resumed before USB 
backend.  In this case, the USB bus is ready to go, and able to handle 
stream enable requests.

Thanks
Wesley Cheng
diff mbox series

Patch

diff --git a/sound/usb/card.c b/sound/usb/card.c
index 26268ffb8274..212f55a7683c 100644
--- a/sound/usb/card.c
+++ b/sound/usb/card.c
@@ -117,6 +117,21 @@  MODULE_PARM_DESC(skip_validation, "Skip unit descriptor validation (default: no)
 static DEFINE_MUTEX(register_mutex);
 static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
 static struct usb_driver usb_audio_driver;
+static struct snd_usb_vendor_ops *vendor_ops;
+
+int snd_usb_register_vendor_ops(struct snd_usb_vendor_ops *ops)
+{
+	vendor_ops = ops;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(snd_usb_register_vendor_ops);
+
+int snd_usb_unregister_vendor_ops(void)
+{
+	vendor_ops = NULL;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(snd_usb_unregister_vendor_ops);
 
 /*
  * disconnect streams
@@ -910,6 +925,10 @@  static int usb_audio_probe(struct usb_interface *intf,
 	usb_set_intfdata(intf, chip);
 	atomic_dec(&chip->active);
 	mutex_unlock(&register_mutex);
+
+	if (vendor_ops->connect_cb)
+		vendor_ops->connect_cb(intf, chip);
+
 	return 0;
 
  __error:
@@ -943,6 +962,9 @@  static void usb_audio_disconnect(struct usb_interface *intf)
 	if (chip == USB_AUDIO_IFACE_UNUSED)
 		return;
 
+	if (vendor_ops->disconnect_cb)
+		vendor_ops->disconnect_cb(intf);
+
 	card = chip->card;
 
 	mutex_lock(&register_mutex);
diff --git a/sound/usb/card.h b/sound/usb/card.h
index 40061550105a..a785bb256b0d 100644
--- a/sound/usb/card.h
+++ b/sound/usb/card.h
@@ -206,4 +206,11 @@  struct snd_usb_stream {
 	struct list_head list;
 };
 
+struct snd_usb_vendor_ops {
+	void (*connect_cb)(struct usb_interface *intf, struct snd_usb_audio *chip);
+	void (*disconnect_cb)(struct usb_interface *intf);
+};
+
+int snd_usb_register_vendor_ops(struct snd_usb_vendor_ops *ops);
+int snd_usb_unregister_vendor_ops(void);
 #endif /* __USBAUDIO_CARD_H */