mbox series

[RFC,v2,00/22] Introduce QC USB SND audio offloading support

Message ID 20230126031424.14582-1-quic_wcheng@quicinc.com
Headers show
Series Introduce QC USB SND audio offloading support | expand

Message

Wesley Cheng Jan. 26, 2023, 3:14 a.m. UTC
Changes in v2:

XHCI:
- Replaced XHCI and HCD changes with Mathias' XHCI interrupter changes
in his tree:
https://git.kernel.org/pub/scm/linux/kernel/git/mnyman/xhci.git/log/?h=feature_interrupters

Adjustments made to Mathias' changes:
  - Created xhci-intr.h to export/expose interrupter APIs versus exposing xhci.h.
    Moved dependent structures to this file as well. (so clients can parse out
    information from "struct xhci_interrupter")
  - Added some basic locking when requesting interrupters.
  - Fixed up some sanity checks.
  - Removed clearing of the ERSTBA during freeing of the interrupter. (pending
    issue where SMMU fault occurs if DMA addr returned is 64b - TODO)

- Clean up pending events in the XHCI secondary interrupter.  While testing USB
bus suspend, it was seen that on bus resume, the xHCI HC would run into a command
timeout.
- Added offloading APIs to xHCI to fetch transfer and event ring information.

ASoC:
- Modified soc-usb to allow for multiple USB port additions.  For this to work,
the USB offload driver has to have a reference to the USB backend by adding
a "usb-soc-be" DT entry to the device saved into XHCI sysdev.
- Created separate dt-bindings for defining USB_RX port.
- Increased APR timeout to accommodate the situation where the AFE port start
command could be delayed due to having to issue a USB bus resume while
handling the QMI stream start command.

USB SND:
- Added a platform ops during usb_audio_suspend().  This allows for the USB
offload driver to halt the audio stream when system enters PM suspend.  This
ensures the audio DSP is not issuing transfers on the USB bus.
- Do not override platform ops if they are already populated.
- Introduce a shared status variable between the USB offload and USB SND layers,
to ensure that only one path is active at a time.  If the USB bus is occupied,
then userspace is notified that the path is busy.

Several Qualcomm based chipsets can support USB audio offloading to a
dedicated audio DSP, which can take over issuing transfers to the USB
host controller.  The intention is to reduce the load on the main
processors in the SoC, and allow them to be placed into lower power modes.
There are several parts to this design:
  1. Adding ASoC binding layer
  2. Create a USB backend for Q6DSP
  3. Introduce XHCI interrupter support
  4. Create vendor ops for the USB SND driver

Adding ASoC binding layer:
soc-usb: Intention is to treat a USB port similar to a headphone jack.
The port is always present on the device, but cable/pin status can be
enabled/disabled.  Expose mechanisms for USB backend ASoC drivers to
communicate with USB SND.

Create a USB backend for Q6DSP:
q6usb: Basic backend driver that will be responsible for maintaining the
resources needed to initiate a playback stream using the Q6DSP.  Will
be the entity that checks to make sure the connected USB audio device
supports the requested PCM format.  If it does not, the PCM open call will
fail, and userpsace ALSA can take action accordingly.

Introduce XHCI interrupter support:
XHCI HCD supports multiple interrupters, which allows for events to be routed
to different event rings.  This is determined by "Interrupter Target" field
specified in Section "6.4.1.1 Normal TRB" of the XHCI specification.

Events in the offloading case will be routed to an event ring that is assigned
to the audio DSP.

Create vendor ops for the USB SND driver:
qc_audio_offload: This particular driver has several components associated
with it:
- QMI stream request handler
- XHCI interrupter and resource management
- audio DSP memory management

When the audio DSP wants to enable a playback stream, the request is first
received by the ASoC platform sound card.  Depending on the selected route,
ASoC will bring up the individual DAIs in the path.  The Q6USB backend DAI
will send an AFE port start command (with enabling the USB playback path), and
the audio DSP will handle the request accordingly.

Part of the AFE USB port start handling will have an exchange of control
messages using the QMI protocol.  The qc_audio_offload driver will populate the
buffer information:
- Event ring base address
- EP transfer ring base address

and pass it along to the audio DSP.  All endpoint management will now be handed
over to the DSP, and the main processor is not involved in transfers.

Overall, implementing this feature will still expose separate sound card and PCM
devices for both the platorm card and USB audio device:
 0 [SM8250MTPWCD938]: sm8250 - SM8250-MTP-WCD9380-WSA8810-VA-D
                      SM8250-MTP-WCD9380-WSA8810-VA-DMIC
 1 [Audio          ]: USB-Audio - USB Audio
                      Generic USB Audio at usb-xhci-hcd.1.auto-1.4, high speed

This is to ensure that userspace ALSA entities can decide which route to take
when executing the audio playback.  In the above, if card#1 is selected, then
USB audio data will take the legacy path over the USB PCM drivers, etc...

This feature was validated using:
- tinymix: set/enable the multimedia path to route to USB backend
- tinyplay: issue playback on platform card

Mathias Nyman (4):
  xhci: fix event ring segment table related masks and variables in
    header
  xhci: remove xhci_test_trb_in_td_math early development check
  xhci: Refactor interrupter code for initial multi interrupter support.
  xhci: Add support to allocate several interrupters

Wesley Cheng (18):
  usb: xhci: Add XHCI APIs to support USB offloading
  usb: host: xhci-mem: Cleanup pending secondary event ring events
  ASoC: Add SOC USB APIs for adding an USB backend
  ASoC: dt-bindings: Add USB_RX port
  ASoC: qcom: qdsp6: Introduce USB AFE port to q6dsp
  ASoC: qdsp6: q6afe: Increase APR timeout
  ASoC: qcom: Add USB backend ASoC driver for Q6
  sound: usb: card: Introduce USB SND platform op callbacks
  sound: usb: Export USB SND APIs for modules
  dt-bindings: usb: dwc3: Add snps,num-hc-interrupters definition
  usb: dwc3: Add DT parameter to specify maximum number of interrupters
  sound: usb: Introduce QC USB SND offloading support
  sound: usb: card: Check for support for requested audio format
  sound: soc: soc-usb: Add PCM format check API for USB backend
  sound: soc: qcom: qusb6: Ensure PCM format is supported by USB audio
    device
  sound: usb: Prevent starting of audio stream if in use
  ASoC: dt-bindings: Add Q6USB backend bindings
  ASoC: dt-bindings: Update example for enabling USB offload on SM8250

 .../bindings/sound/qcom,q6usb-dais.yaml       |   55 +
 .../bindings/sound/qcom,sm8250.yaml           |   13 +
 .../devicetree/bindings/usb/snps,dwc3.yaml    |   12 +
 drivers/usb/dwc3/core.c                       |   12 +
 drivers/usb/dwc3/core.h                       |    2 +
 drivers/usb/dwc3/host.c                       |    5 +-
 drivers/usb/host/xhci-debugfs.c               |    2 +-
 drivers/usb/host/xhci-mem.c                   |  471 +++--
 drivers/usb/host/xhci-ring.c                  |   68 +-
 drivers/usb/host/xhci.c                       |  199 +-
 drivers/usb/host/xhci.h                       |   81 +-
 .../sound/qcom,q6dsp-lpass-ports.h            |    1 +
 include/linux/usb/xhci-intr.h                 |   94 +
 include/sound/pcm_params.h                    |    4 +
 include/sound/q6usboffload.h                  |   20 +
 include/sound/soc-usb.h                       |   36 +
 sound/core/oss/pcm_oss.c                      |   58 -
 sound/core/pcm_lib.c                          |   65 +
 sound/soc/Makefile                            |    2 +-
 sound/soc/qcom/Kconfig                        |    4 +
 sound/soc/qcom/qdsp6/Makefile                 |    1 +
 sound/soc/qcom/qdsp6/q6afe-dai.c              |   48 +
 sound/soc/qcom/qdsp6/q6afe.c                  |  185 +-
 sound/soc/qcom/qdsp6/q6afe.h                  |   46 +-
 sound/soc/qcom/qdsp6/q6dsp-lpass-ports.c      |   23 +
 sound/soc/qcom/qdsp6/q6dsp-lpass-ports.h      |    1 +
 sound/soc/qcom/qdsp6/q6routing.c              |    8 +
 sound/soc/qcom/qdsp6/q6usb.c                  |  234 +++
 sound/soc/soc-usb.c                           |  215 ++
 sound/usb/Kconfig                             |   14 +
 sound/usb/Makefile                            |    2 +-
 sound/usb/card.c                              |   58 +
 sound/usb/card.h                              |   29 +
 sound/usb/endpoint.c                          |    2 +
 sound/usb/helper.c                            |    1 +
 sound/usb/pcm.c                               |   28 +-
 sound/usb/pcm.h                               |   12 +
 sound/usb/qcom/Makefile                       |    2 +
 sound/usb/qcom/qc_audio_offload.c             | 1789 +++++++++++++++++
 sound/usb/qcom/usb_audio_qmi_v01.c            |  892 ++++++++
 sound/usb/qcom/usb_audio_qmi_v01.h            |  162 ++
 41 files changed, 4546 insertions(+), 410 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/sound/qcom,q6usb-dais.yaml
 create mode 100644 include/linux/usb/xhci-intr.h
 create mode 100644 include/sound/q6usboffload.h
 create mode 100644 include/sound/soc-usb.h
 create mode 100644 sound/soc/qcom/qdsp6/q6usb.c
 create mode 100644 sound/soc/soc-usb.c
 create mode 100644 sound/usb/qcom/Makefile
 create mode 100644 sound/usb/qcom/qc_audio_offload.c
 create mode 100644 sound/usb/qcom/usb_audio_qmi_v01.c
 create mode 100644 sound/usb/qcom/usb_audio_qmi_v01.h

Comments

Wesley Cheng Jan. 30, 2023, 10:54 p.m. UTC | #1
Hi Pierre,

On 1/26/2023 7:38 AM, Pierre-Louis Bossart wrote:
> 
> 
> On 1/25/23 21:14, Wesley Cheng wrote:
>> The QC ADSP is able to support USB playback endpoints, so that the main
>> application processor can be placed into lower CPU power modes.  This adds
>> the required AFE port configurations and port start command to start an
>> audio session.
>>
>> Specifically, the QC ADSP can support all potential endpoints that are
>> exposed by the audio data interface.  This includes, feedback endpoints
>> (both implicit and explicit) as well as the isochronous (data) endpoints.
>> The size of audio samples sent per USB frame (microframe) will be adjusted
>> based on information received on the feedback endpoint.
> 
> I think you meant "support all potential endpoint types"
> 
> It's likely that some USB devices have more endpoints than what the DSP
> can handle, no?
> 

True, as we discussed before, we only handle the endpoints for the audio 
interface.  Other endpoints, such as HID, or control is still handled by 
the main processor.

> And that brings me back to the question: what is a port and the
> relationship between port/backend/endpoints?
> 
> Sorry for being picky on terminology, but if I learned something in days
> in standardization it's that there shouldn't be any ambiguity on
> concepts, otherwise everyone is lost at some point.
> 

No worries, I can understand where you're coming from :).  After 
re-reading some of the notations used, I can see where people may be 
confused.

> 
>>   static struct afe_port_map port_maps[AFE_PORT_MAX] = {
>> +	[USB_RX] = { AFE_PORT_ID_USB_RX, USB_RX, 1, 1},
>>   	[HDMI_RX] = { AFE_PORT_ID_MULTICHAN_HDMI_RX, HDMI_RX, 1, 1},
>>   	[SLIMBUS_0_RX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_0_RX,
>>   				SLIMBUS_0_RX, 1, 1},
> 
> And if I look here a port seems to be a very specific AFE concept
> related to interface type? Do we even need to refer to a port in the USB
> parts?
> 

Well, this is a design specific to how the Q6 AFE is implemented.  There 
is a concept for an AFE port to be opened.  However, as mentioned 
earlier, the "port" term used in soc-usb should be more for how many USB 
devices can be supported.

If there was a case the audio DSP would support more than one USB 
device, I believe another AFE port would need to be added.

Thanks
Wesley Cheng
Wesley Cheng Jan. 30, 2023, 10:59 p.m. UTC | #2
Hi Pierre,

On 1/26/2023 7:44 AM, Pierre-Louis Bossart wrote:
> 
> 
> On 1/25/23 21:14, Wesley Cheng wrote:
>> Create a USB BE component that will register a new USB port to the ASoC USB
>> framework.  This will handle determination on if the requested audio
>> profile is supported by the USB device currently selected.
> 
> Can you clarify how? because ...
> 
> 
>> +static struct snd_soc_dai_driver q6usb_be_dais[] = {
>> +	{
>> +		.playback = {
>> +			.stream_name = "USB BE RX",
>> +			.rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |
>> +				SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |
>> +				SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
>> +				SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |
>> +				SNDRV_PCM_RATE_192000,
>> +			.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
>> +				SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE |
>> +				SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |
>> +				SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
>> +			.channels_min = 1,
>> +			.channels_max = 2,
>> +			.rate_max =     192000,
>> +			.rate_min =	8000,
>> +		},
>> +		.id = USB_RX,
>> +		.name = "USB_RX_BE",
>> +		.ops = &q6usb_ops,
>> +	},
>> +};
> 
> ... here I see a single DAI, so presumably ONE endpoint can be supported?
> 

One USB audio device can be supported.
one AFE port = one USB audio device

> I didn't see in the rest of the code how a card with multiple endpoint
> would be rejected, nor how the capabilities are checked?
> 

Need to take a look at this query a bit more.  Let me try to pass in a 
format that can't be supported by the audio DSP, and see if the formats 
specified in this structure will not allow userspace to start the session.

When you say a "card with multiple endpoints" are you referring to a USB 
device that exposes multiple data (ISOC let's say) eps for its data 
interface?  I haven't run into a device like that.

Thanks
Wesley Cheng
Pierre-Louis Bossart Jan. 30, 2023, 11:59 p.m. UTC | #3
On 1/30/23 16:54, Wesley Cheng wrote:
> Hi Pierre,
> 
> On 1/26/2023 7:38 AM, Pierre-Louis Bossart wrote:
>>
>>
>> On 1/25/23 21:14, Wesley Cheng wrote:
>>> The QC ADSP is able to support USB playback endpoints, so that the main
>>> application processor can be placed into lower CPU power modes.  This
>>> adds
>>> the required AFE port configurations and port start command to start an
>>> audio session.
>>>
>>> Specifically, the QC ADSP can support all potential endpoints that are
>>> exposed by the audio data interface.  This includes, feedback endpoints
>>> (both implicit and explicit) as well as the isochronous (data)
>>> endpoints.
>>> The size of audio samples sent per USB frame (microframe) will be
>>> adjusted
>>> based on information received on the feedback endpoint.
>>
>> I think you meant "support all potential endpoint types"
>>
>> It's likely that some USB devices have more endpoints than what the DSP
>> can handle, no?
>>
> 
> True, as we discussed before, we only handle the endpoints for the audio
> interface.  Other endpoints, such as HID, or control is still handled by
> the main processor.

The number of isoc/audio endpoints can be larger than 1 per direction,
it's not uncommon for a USB device to have multiple connectors on the
front side for instruments, mics, monitor speakers, you name it. Just
google 'motu' or 'rme usb' and you'll see examples of USB devices that
are very different from plain vanilla headsets.

>> And that brings me back to the question: what is a port and the
>> relationship between port/backend/endpoints?
>>
>> Sorry for being picky on terminology, but if I learned something in days
>> in standardization it's that there shouldn't be any ambiguity on
>> concepts, otherwise everyone is lost at some point.
>>
> 
> No worries, I can understand where you're coming from :).  After
> re-reading some of the notations used, I can see where people may be
> confused.
> 
>>
>>>   static struct afe_port_map port_maps[AFE_PORT_MAX] = {
>>> +    [USB_RX] = { AFE_PORT_ID_USB_RX, USB_RX, 1, 1},
>>>       [HDMI_RX] = { AFE_PORT_ID_MULTICHAN_HDMI_RX, HDMI_RX, 1, 1},
>>>       [SLIMBUS_0_RX] = { AFE_PORT_ID_SLIMBUS_MULTI_CHAN_0_RX,
>>>                   SLIMBUS_0_RX, 1, 1},
>>
>> And if I look here a port seems to be a very specific AFE concept
>> related to interface type? Do we even need to refer to a port in the USB
>> parts?
>>
> 
> Well, this is a design specific to how the Q6 AFE is implemented.  There
> is a concept for an AFE port to be opened.  However, as mentioned
> earlier, the "port" term used in soc-usb should be more for how many USB
> devices can be supported.
> 
> If there was a case the audio DSP would support more than one USB
> device, I believe another AFE port would need to be added.


would the suggested infrastructure work though, even if the DSP could
deal with multiple endpoints on different devices ? You have static
mutexes and ops, can that scale to more than one USB device?
Wesley Cheng Feb. 7, 2023, 1:15 a.m. UTC | #4
Hi Pierre,

On 1/26/2023 8:12 AM, Pierre-Louis Bossart wrote:
> 
> 
> On 1/25/23 21:14, Wesley Cheng wrote:
>> 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.
> 
> It's good to maintain mutual exclusion, but it's still very hard for an
> application to figure out which card can be used when.
> 
> Returning -EBUSY is not super helpful. There should be something like a
> notification or connection status so that routing decisions can be made
> without trial-and-error.
> 

The USB offload driver does have access to the USB substream that is 
being utilized/offloaded.  Maybe in addition to this check, we can also 
set the PCM runtime state as well (for that particular substream)?  That 
way userspace can fetch information about if the stream is busy or not.

Thanks
Wesley Cheng
Pierre-Louis Bossart Feb. 7, 2023, 1:29 p.m. UTC | #5
On 2/6/23 19:15, Wesley Cheng wrote:
> Hi Pierre,
> 
> On 1/26/2023 8:12 AM, Pierre-Louis Bossart wrote:
>>
>>
>> On 1/25/23 21:14, Wesley Cheng wrote:
>>> 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.
>>
>> It's good to maintain mutual exclusion, but it's still very hard for an
>> application to figure out which card can be used when.
>>
>> Returning -EBUSY is not super helpful. There should be something like a
>> notification or connection status so that routing decisions can be made
>> without trial-and-error.
>>
> 
> The USB offload driver does have access to the USB substream that is
> being utilized/offloaded.  Maybe in addition to this check, we can also
> set the PCM runtime state as well (for that particular substream)?  That
> way userspace can fetch information about if the stream is busy or not.

You're missing the point. When a card is exposed but the PCM devices may
or may not be usable (consuming data with no sound rendered or returning
an error), it's much better to provide a clear connection status to
userspace.

Let me give you an example. Intel drivers can expose 3 HDMI/DP PCM
devices. Userspace has no idea which one to use, so there's a jack
control that tells userspace whether there is a receiver connected so
that the audio server can use the relevant PCM device.

Audio routing based on trial and error is really problematic, errors can
happen but they should be exceptional (e.g. xruns), not a means of
driver-userspace communication on the device status.
Wesley Cheng Feb. 11, 2023, 9:52 a.m. UTC | #6
Hi Pierre,

On 2/7/2023 5:29 AM, Pierre-Louis Bossart wrote:
> 
> 
> On 2/6/23 19:15, Wesley Cheng wrote:
>> Hi Pierre,
>>
>> On 1/26/2023 8:12 AM, Pierre-Louis Bossart wrote:
>>>
>>>
>>> On 1/25/23 21:14, Wesley Cheng wrote:
>>>> 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.
>>>
>>> It's good to maintain mutual exclusion, but it's still very hard for an
>>> application to figure out which card can be used when.
>>>
>>> Returning -EBUSY is not super helpful. There should be something like a
>>> notification or connection status so that routing decisions can be made
>>> without trial-and-error.
>>>
>>
>> The USB offload driver does have access to the USB substream that is
>> being utilized/offloaded.  Maybe in addition to this check, we can also
>> set the PCM runtime state as well (for that particular substream)?  That
>> way userspace can fetch information about if the stream is busy or not.
> 
> You're missing the point. When a card is exposed but the PCM devices may
> or may not be usable (consuming data with no sound rendered or returning
> an error), it's much better to provide a clear connection status to
> userspace.
> 
> Let me give you an example. Intel drivers can expose 3 HDMI/DP PCM
> devices. Userspace has no idea which one to use, so there's a jack
> control that tells userspace whether there is a receiver connected so
> that the audio server can use the relevant PCM device.
> 
> Audio routing based on trial and error is really problematic, errors can
> happen but they should be exceptional (e.g. xruns), not a means of
> driver-userspace communication on the device status.

Thanks for clarifying.  The example helped me understand a bit more on 
how the potential use of the SND control interface.  Since we're dealing 
with multiple sound cards here (platform sound card (offload) and USB 
SND card (legacy)), what do you think about creating a SND control on 
both the USB backend (platform card) and the USB SND card listing the 
PCM device status?

That way at least userspace can have the information about which PCM dev 
(USB substream) is available (and not offloaded, or vice versa).  So the 
USB SND control will contain the PCM devices (exposed by the card) and 
if any are offloaded (if so mark them as unavailable).  Likewise, for 
the USB backend, if the legacy path is being used, mark them as 
unavailable for offloading.

Thanks
Wesley Cheng
Pierre-Louis Bossart Feb. 13, 2023, 3:22 p.m. UTC | #7
On 2/11/23 03:52, Wesley Cheng wrote:
> Hi Pierre,
> 
> On 2/7/2023 5:29 AM, Pierre-Louis Bossart wrote:
>>
>>
>> On 2/6/23 19:15, Wesley Cheng wrote:
>>> Hi Pierre,
>>>
>>> On 1/26/2023 8:12 AM, Pierre-Louis Bossart wrote:
>>>>
>>>>
>>>> On 1/25/23 21:14, Wesley Cheng wrote:
>>>>> 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.
>>>>
>>>> It's good to maintain mutual exclusion, but it's still very hard for an
>>>> application to figure out which card can be used when.
>>>>
>>>> Returning -EBUSY is not super helpful. There should be something like a
>>>> notification or connection status so that routing decisions can be made
>>>> without trial-and-error.
>>>>
>>>
>>> The USB offload driver does have access to the USB substream that is
>>> being utilized/offloaded.  Maybe in addition to this check, we can also
>>> set the PCM runtime state as well (for that particular substream)?  That
>>> way userspace can fetch information about if the stream is busy or not.
>>
>> You're missing the point. When a card is exposed but the PCM devices may
>> or may not be usable (consuming data with no sound rendered or returning
>> an error), it's much better to provide a clear connection status to
>> userspace.
>>
>> Let me give you an example. Intel drivers can expose 3 HDMI/DP PCM
>> devices. Userspace has no idea which one to use, so there's a jack
>> control that tells userspace whether there is a receiver connected so
>> that the audio server can use the relevant PCM device.
>>
>> Audio routing based on trial and error is really problematic, errors can
>> happen but they should be exceptional (e.g. xruns), not a means of
>> driver-userspace communication on the device status.
> 
> Thanks for clarifying.  The example helped me understand a bit more on
> how the potential use of the SND control interface.  Since we're dealing
> with multiple sound cards here (platform sound card (offload) and USB
> SND card (legacy)), what do you think about creating a SND control on
> both the USB backend (platform card) and the USB SND card listing the
> PCM device status?
> 
> That way at least userspace can have the information about which PCM dev
> (USB substream) is available (and not offloaded, or vice versa).  So the
> USB SND control will contain the PCM devices (exposed by the card) and
> if any are offloaded (if so mark them as unavailable).  Likewise, for
> the USB backend, if the legacy path is being used, mark them as
> unavailable for offloading.

We definitively need a control to indicate that a PCM offload device is
available or not.
There's still a very large open with the notion of having separate cards
for the same audio device. Not only would it duplicate the control parts
for e.g. volume control, but it would introduce the need to tag devices
across two cards are being the same physical device.
I still think the least-bad option is to have a single card and an
optional PCM device for offload.
Wesley Cheng Feb. 13, 2023, 8:12 p.m. UTC | #8
Hi Pierre,

On 2/13/2023 7:22 AM, Pierre-Louis Bossart wrote:
> 
> 
> On 2/11/23 03:52, Wesley Cheng wrote:
>> Hi Pierre,
>>
>> On 2/7/2023 5:29 AM, Pierre-Louis Bossart wrote:
>>>
>>>
>>> On 2/6/23 19:15, Wesley Cheng wrote:
>>>> Hi Pierre,
>>>>
>>>> On 1/26/2023 8:12 AM, Pierre-Louis Bossart wrote:
>>>>>
>>>>>
>>>>> On 1/25/23 21:14, Wesley Cheng wrote:
>>>>>> 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.
>>>>>
>>>>> It's good to maintain mutual exclusion, but it's still very hard for an
>>>>> application to figure out which card can be used when.
>>>>>
>>>>> Returning -EBUSY is not super helpful. There should be something like a
>>>>> notification or connection status so that routing decisions can be made
>>>>> without trial-and-error.
>>>>>
>>>>
>>>> The USB offload driver does have access to the USB substream that is
>>>> being utilized/offloaded.  Maybe in addition to this check, we can also
>>>> set the PCM runtime state as well (for that particular substream)?  That
>>>> way userspace can fetch information about if the stream is busy or not.
>>>
>>> You're missing the point. When a card is exposed but the PCM devices may
>>> or may not be usable (consuming data with no sound rendered or returning
>>> an error), it's much better to provide a clear connection status to
>>> userspace.
>>>
>>> Let me give you an example. Intel drivers can expose 3 HDMI/DP PCM
>>> devices. Userspace has no idea which one to use, so there's a jack
>>> control that tells userspace whether there is a receiver connected so
>>> that the audio server can use the relevant PCM device.
>>>
>>> Audio routing based on trial and error is really problematic, errors can
>>> happen but they should be exceptional (e.g. xruns), not a means of
>>> driver-userspace communication on the device status.
>>
>> Thanks for clarifying.  The example helped me understand a bit more on
>> how the potential use of the SND control interface.  Since we're dealing
>> with multiple sound cards here (platform sound card (offload) and USB
>> SND card (legacy)), what do you think about creating a SND control on
>> both the USB backend (platform card) and the USB SND card listing the
>> PCM device status?
>>
>> That way at least userspace can have the information about which PCM dev
>> (USB substream) is available (and not offloaded, or vice versa).  So the
>> USB SND control will contain the PCM devices (exposed by the card) and
>> if any are offloaded (if so mark them as unavailable).  Likewise, for
>> the USB backend, if the legacy path is being used, mark them as
>> unavailable for offloading.
> 
> We definitively need a control to indicate that a PCM offload device is
> available or not.
> There's still a very large open with the notion of having separate cards
> for the same audio device. Not only would it duplicate the control parts
> for e.g. volume control, but it would introduce the need to tag devices
> across two cards are being the same physical device.

The volume control would still be done through the card that is exposed 
by the USB SND card (even for the offload path)[no vol control option 
for the USB device on the platform card].

In the last discussion, you did mention that maybe we can tag the 
offload path as the "power saving" option for a particular USB stream. 
Although I'm not sure how intricate the logic is, but if userspace marks 
to use the power saving path, then would it already know which card and 
PCM devices are involved?

Although, that part is missing, ie to select the card and pcm device 
that we want to offload.  It may be possible to do this with another 
control on the USB ASoC backend driver.  I believe the audio DSP can 
support device selection.

> I still think the least-bad option is to have a single card and an
> optional PCM device for offload.

This is most likely the end goal, but as mentioned previously, its going 
to be a large effort to slowly decouple some of the PCM related 
operations from USB SND.  IMO, that would most likely be another 
significant patch series in itself.

Thanks
Wesley Cheng