mbox series

[v21,00/39] Introduce QC USB SND audio offloading support

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

Message

Wesley Cheng May 7, 2024, 7:50 p.m. UTC
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

      USB                          |            ASoC
--------------------------------------------------------------------
                                   |  _________________________
                                   | |sm8250 platform card     |
                                   | |_________________________|
                                   |         |           |
                                   |      ___V____   ____V____
                                   |     |Q6USB   | |Q6AFE    |  
                                   |     |"codec" | |"cpu"    |
                                   |     |________| |_________|
                                   |         ^  ^        ^
                                   |         |  |________|
                                   |      ___V____    |
                                   |     |SOC-USB |   |
   ________       ________               |        |   |
  |USB SND |<--->|QC offld|<------------>|________|   |
  |(card.c)|     |        |<----------                |
  |________|     |________|___     | |                |
      ^               ^       |    | |    ____________V_________
      |               |       |    | |   |APR/GLINK             |
   __ V_______________V_____  |    | |   |______________________|
  |USB SND (endpoint.c)     | |    | |              ^
  |_________________________| |    | |              |
              ^               |    | |   ___________V___________
              |               |    | |->|audio DSP              |
   ___________V_____________  |    |    |_______________________|
  |XHCI HCD                 |<-    |
  |_________________________|      |


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

Requesting to see if we can get some Acked-By tags, and merge on usb-next.

Changelog
--------------------------------------------
Changs in v21:
- Added an offload jack disable path from the ASoC platform driver and SOC USB.
- Refactored some of the existing SOC USB context look up APIs and created some
new helpers to search for the USB context.
- Renamed snd_soc_usb_find_format to snd_soc_usb_find_supported_format
- Removed some XHCI sideband calls that would allow clients to actually enable
the IRQ line associated w/ the secondary interrupter.  This is removed because
there are other dependencies that are required for that to happen, which are not
covered as part of this series, and to avoid confusion.
- Due to the above, removed the need to export IMOD setting, and enable/disable
interrupter APIs.

Changes in v20:
- Fixed up some formatting changes pointed out in the usb.rst
- Added SB null check during XHCI sideband unregister in case caller passes
improper argument (xhci_sideband_unregister())

Changes in v19:
- Rebased to usb-next to account for some new changes in dependent drivers.

Changes in v18:
- Rebased to usb-next, which merged in part of the series.  Removed these patches.
- Reworked Kconfigs for the ASoC USB related components from QCOM Q6DSP drivers
  to keep dependencies in place for SoC USB and USB SND.
- Removed the repurposing of the stop ep sync API into existing XHCI operations.
  This will be solely used by the XHCI sideband for now.

Changes in v17:
- Fixed an issue where one patch was squashed into another.
- Re-added some kconfig checks for helpers exposed in USB SND for the soc usb
  driver, after running different kconfigs.

Changes in v16:
- Modified some code layer dependencies so that soc usb can be split as a separate
  module.
  - Split the kcontrols from ASoC QCOM common layer into a separate driver
- Reworked SOC USB kcontrols for controlling card + pcm offload routing and status
  so that there are individual controls for card and pcm devices.
- Added a kcontrol remove API in SOC USB to remove the controls on the fly.  This
  required to add some kcontrol management to SOC USB.
- Removed the disconnect work and workqueue for the QC USB offload as it is not
  required, since QMI interface driver ensures events are handled in its own WQ.

Changes in v15:
- Removed some already merged XHCI changes
- Separated SOC USB driver from being always compiled into SOC core.  Now
  configurable from kconfig.
- Fixed up ASoC kcontrol naming to fit guidelines.
- Removed some unnecessary dummy ifdefs.
- Moved usb snd offload capable kcontrol to be initialized by the platform offloading
  driver.

Changes in v14:
- Cleaned up some USB SND related feedback:
  - Renamed SNDUSB OFFLD playback available --> USB offload capable card
  - Fixed locking while checking if stream is in use
  - Replaced some mutex pairs with guard(mutex)

Changes in v13:
- Pulled in secondary/primary interrupter rework from Mathias from:
  https://git.kernel.org/pub/scm/linux/kernel/git/mnyman/xhci.git/log/drivers/usb/host?h=fix_eventhandling
  - Did some cleanup and commit message updates, and tested on current code base.
- Added mutex locking to xhci sideband to help prevent any race conditions, esp. for when accessing shared
  references.
- Addresed concerns from Hillf about gfp_flags and locking used in qc_usb_audio_offload.
- Rebased onto usb-next

Changes in v12:
- Updated copyright year to 2024.  Happy new years!
- Fixed newline format on mixer offload driver.

Changes in v11:
- Modified QMI format structures to be const

Changes in v10:
- Added new mixer for exposing kcontrol for sound card created by USB SND.  This
allows for applications to know which platform sound card has offload support.
Will return the card number.
- Broke down and cleaned up some functions/APIs within qc_audio_offload driver.
- Exported xhci_initialize_ring_info(), and modified XHCI makefile to allow for
the XHCI sideband to exist as a module.
- Reworked the jack registration and moved it to the QCOM platform card driver,
ie sm8250.
- Added an SOC USB API to fetch a standard component tag that can be appended to
the platform sound card.  Added this tag to sm8250 if any USB path exists within
the DT node.
- Moved kcontrols that existed in the Q6USB driver, and made it a bit more generic,
so that naming can be standardized across solutions.  SOC USB is now responsible
for creation of these kcontrols.
- Added a SOC USB RST document explaining some code flows and implementation details
so that other vendors can utilize the framework.
- Addressed a case where USB device connection events are lost if usb offload driver
(qc_audio_offload) is not probed when everything else has been initialized, ie 
USB SND, SOC USB and ASoC sound card.  Add a rediscover device call during module
init, to ensure that connection events will be propagated.
- Rebased to usb-next.

Changes in v9:
- Fixed the dt binding check issue with regards to num-hc-interrupters.

Changes in v8:
- Cleaned up snd_soc_usb_find_priv_data() based on Mark's feedback.  Removed some of
the duplicate looping code that was present on previous patches.  Also renamed the API.
- Integrated Mathias' suggestions on his new sideband changes:
https://git.kernel.org/pub/scm/linux/kernel/git/mnyman/xhci.git/log/?h=feature_interrupters
- Addressed some of Mathias' fixme tags, such as:
 - Resetting transfer ring dequeue/enqueue pointers
 - Issuing stop endpoint command during ep removal
 - Reset ERDP properly to first segment ring during interrupter removal. (this is currently
   just being cleared to 0, but should be pointing to a valid segment if controller is still
   running.

Changes in v7:
- Fixed dt check error for q6usb bindings
- Updated q6usb property from qcom,usb-audio-intr-num --> qcom,usb-audio-intr-idx
- Removed separate DWC3 HC interrupters num property, and place limits to XHCI one.
- Modified xhci_ring_to_sgtable() to use assigned IOVA/DMA address to fetch pages, as
it is not ensured event ring allocated is always done in the vmalloc range.

Changes in v6:
- Fixed limits and description on several DT bindings (XHCI and Q6USB)
- Fixed patch subjects to follow other ALSA/ASoC notations.

USB SND
- Addressed devices which expose multiple audio (UAC) interfaces.  These devices will
create a single USB sound card with multiple audio streams, and receive multiple
interface probe routines.  QC offload was not properly considering cases with multiple
probe calls.
- Renamed offload module name and kconfig to fit within the SND domain.
- Renamed attach/detach endpoint API to keep the hw_params notation.

Changes in v5:
- Removed some unnescessary files that were included
- Fixed some typos mentioned
- Addressed dt-binding issues and added hc-interrupters definition to usb-xhci.yaml

XHCI:
- Moved secondary skip events API to xhci-ring and updated implementation
   - Utilized existing XHCI APIs, such as inc_deq and xhci_update_erst_dequeue()

USB SND
- Renamed and reworked the APIs in "sound: usb: Export USB SND APIs for modules" patch to
include suggestions to utilize snd_usb_hw_params/free and to avoid generic naming.
- Added a resume_cb() op for completion sake.
- Addressed some locking concerns with regards to when registering for platform hooks.
- Added routine to disconnect all offloaded devices during module unbind.

ASoC
- Replaced individual PCM parameter arguments in snd_soc_usb_connect() with new
snd_soc_usb_device structure to pass along PCM info.
- Modified snd_jack set report to notify HEADPHONE event, as we do not support record path.

Changes in v4:
- Rebased to xhci/for-usb-next
- Addressed some dt-bindings comments

XHCI:
- Pulled in latest changes from Mathias' feature_interrupters branch:
https://git.kernel.org/pub/scm/linux/kernel/git/mnyman/xhci.git/log/?h=feature_interrupters

- Fixed commit text and signage for the XHCI sideband/interrupter related changes
- Added some logic to address the FIXME tags mentioned throughout the commits, such
as handling multi segment rings and building the SGT, locking concerns, and ep
cleanup operations.
- Removed some fixme tags for conditions that may not be needed/addressed.
- Repurposed the new endpoint stop sync API to be utilized in other places.
- Fixed potential compile issue if XHCI sideband config is not defined.

ASoC:
- Added sound jack control into the Q6USB driver.  Allows for userpsace to know when
an offload capable device is connected.

USB SND:
- Avoided exporting _snd_pcm_hw_param_set based on Takashi's recommendation.
- Split USB QMI packet header definitions into a separate commit.  This is used to
properly allow the QMI interface driver to parse and route QMI packets accordingly
- Added a "depends on" entry when enabling QC audio offload to avoid compile time
issues.

Changes in v3:
- Changed prefix from RFC to PATCH
- Rebased entire series to usb-next
- Updated copyright years

XHCI:
- Rebased changes on top of XHCI changes merged into usb-next, and only added
changes that were still under discussion.
- Added change to read in the "num-hc-interrupters" device property.

ASoC:
- qusb6 USB backend
  - Incorporated suggestions to fetch iommu information with existing APIs
  - Added two new sound kcontrols to fetch offload status and offload device
    selection.
    - offload status - will return the card and pcm device in use
        tinymix -D 0 get 1 --> 1, 0 (offload in progress on card#1 pcm#0)

    - device selection - set the card and pcm device to enable offload on. Ex.:
        tinymix -D 0 set 1 2 0  --> sets offload on card#2 pcm#0
                                    (this should be the USB card)

USB SND:
- Fixed up some locking related concerns for registering platform ops.
   - Moved callbacks under the register_mutex, so that 
- Modified APIs to properly pass more information about the USB SND device, so
that the Q6USB backend can build a device list/map, in order to monitor offload
status and device selection.

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.

Mathias Nyman (2):
  xhci: add helper to stop endpoint and wait for completion
  xhci: sideband: add initial api to register a sideband entity

Wesley Cheng (37):
  usb: host: xhci: Repurpose event handler for skipping interrupter
    events
  usb: xhci: Allow for secondary interrupter to set IMOD
  usb: host: xhci-mem: Cleanup pending secondary event ring events
  usb: host: xhci-mem: Allow for interrupter clients to choose specific
    index
  ASoC: Add SOC USB APIs for adding an USB backend
  ASoC: dt-bindings: qcom,q6dsp-lpass-ports: Add USB_RX port
  ASoC: qcom: qdsp6: Introduce USB AFE port to q6dsp
  ASoC: qdsp6: q6afe: Increase APR timeout
  ASoC: qcom: qdsp6: Add USB backend ASoC driver for Q6
  ALSA: usb-audio: Introduce USB SND platform op callbacks
  ALSA: usb-audio: Export USB SND APIs for modules
  ALSA: usb-audio: Save UAC sample size information
  usb: dwc3: Specify maximum number of XHCI interrupters
  usb: host: xhci-plat: Set XHCI max interrupters if property is present
  ALSA: usb-audio: qcom: Add USB QMI definitions
  ALSA: usb-audio: qcom: Introduce QC USB SND offloading support
  ALSA: usb-audio: Check for support for requested audio format
  ASoC: usb: Add PCM format check API for USB backend
  ASoC: qcom: qdsp6: Ensure PCM format is supported by USB audio device
  ALSA: usb-audio: Prevent starting of audio stream if in use
  ALSA: usb-audio: Do not allow USB offload path if PCM device is in use
  ASoC: dt-bindings: Update example for enabling USB offload on SM8250
  ALSA: usb-audio: qcom: Populate PCM and USB chip information
  ASoC: qcom: qdsp6: Add support to track available USB PCM devices
  ASoC: Introduce SND kcontrols to select sound card and PCM device
  ASoC: qcom: qdsp6: Add SOC USB offload select get/put callbacks
  ASoC: Introduce SND kcontrols to track USB offloading state
  ASoC: qcom: qdsp6: Add PCM ops to track current state
  ASoC: usb: Create SOC USB SND jack kcontrol
  ASoC: qcom: qdsp6: Add headphone jack for offload connection status
  ASoC: usb: Fetch ASoC sound card information
  ALSA: usb-audio: Add USB offloading capable kcontrol
  ALSA: usb-audio: Allow for rediscovery of connected USB SND devices
  ALSA: usb-audio: qcom: Use card and PCM index from QMI request
  ASoC: usb: Rediscover USB SND devices on USB port add
  ASoC: qcom: Populate SoC components string
  ASoC: doc: Add documentation for SOC USB

 .../bindings/sound/qcom,sm8250.yaml           |   15 +
 Documentation/sound/soc/index.rst             |    1 +
 Documentation/sound/soc/usb.rst               |  611 ++++++
 drivers/usb/dwc3/core.c                       |   12 +
 drivers/usb/dwc3/core.h                       |    2 +
 drivers/usb/dwc3/host.c                       |    3 +
 drivers/usb/host/Kconfig                      |    9 +
 drivers/usb/host/Makefile                     |    2 +
 drivers/usb/host/xhci-mem.c                   |   36 +-
 drivers/usb/host/xhci-plat.c                  |    2 +
 drivers/usb/host/xhci-ring.c                  |   50 +-
 drivers/usb/host/xhci-sideband.c              |  418 ++++
 drivers/usb/host/xhci.c                       |   43 +-
 drivers/usb/host/xhci.h                       |   18 +-
 .../sound/qcom,q6dsp-lpass-ports.h            |    1 +
 include/linux/usb/xhci-sideband.h             |   68 +
 include/sound/q6usboffload.h                  |   20 +
 include/sound/soc-usb.h                       |  188 ++
 sound/soc/Kconfig                             |   10 +
 sound/soc/Makefile                            |    2 +
 sound/soc/qcom/Kconfig                        |   15 +
 sound/soc/qcom/Makefile                       |    2 +
 sound/soc/qcom/qdsp6/Makefile                 |    1 +
 sound/soc/qcom/qdsp6/q6afe-dai.c              |   60 +
 sound/soc/qcom/qdsp6/q6afe.c                  |  193 +-
 sound/soc/qcom/qdsp6/q6afe.h                  |   36 +-
 sound/soc/qcom/qdsp6/q6dsp-lpass-ports.c      |   23 +
 sound/soc/qcom/qdsp6/q6dsp-lpass-ports.h      |    1 +
 sound/soc/qcom/qdsp6/q6routing.c              |    9 +
 sound/soc/qcom/qdsp6/q6usb.c                  |  428 ++++
 sound/soc/qcom/sm8250.c                       |   26 +-
 sound/soc/qcom/usb_offload_utils.c            |   77 +
 sound/soc/qcom/usb_offload_utils.h            |   36 +
 sound/soc/soc-usb.c                           |  678 ++++++
 sound/usb/Kconfig                             |   25 +
 sound/usb/Makefile                            |    2 +-
 sound/usb/card.c                              |  109 +
 sound/usb/card.h                              |   15 +
 sound/usb/endpoint.c                          |    1 +
 sound/usb/format.c                            |    1 +
 sound/usb/helper.c                            |    1 +
 sound/usb/pcm.c                               |  104 +-
 sound/usb/pcm.h                               |   11 +
 sound/usb/qcom/Makefile                       |    6 +
 sound/usb/qcom/mixer_usb_offload.c            |   65 +
 sound/usb/qcom/mixer_usb_offload.h            |   17 +
 sound/usb/qcom/qc_audio_offload.c             | 1914 +++++++++++++++++
 sound/usb/qcom/usb_audio_qmi_v01.c            |  892 ++++++++
 sound/usb/qcom/usb_audio_qmi_v01.h            |  162 ++
 49 files changed, 6371 insertions(+), 50 deletions(-)
 create mode 100644 Documentation/sound/soc/usb.rst
 create mode 100644 drivers/usb/host/xhci-sideband.c
 create mode 100644 include/linux/usb/xhci-sideband.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/qcom/usb_offload_utils.c
 create mode 100644 sound/soc/qcom/usb_offload_utils.h
 create mode 100644 sound/soc/soc-usb.c
 create mode 100644 sound/usb/qcom/Makefile
 create mode 100644 sound/usb/qcom/mixer_usb_offload.c
 create mode 100644 sound/usb/qcom/mixer_usb_offload.h
 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

Pierre-Louis Bossart May 7, 2024, 8:26 p.m. UTC | #1
> +const char *snd_soc_usb_get_components_tag(bool playback)
> +{
> +	if (playback)
> +		return "usbplaybackoffload: 1";
> +	else
> +		return "usbcaptureoffload : 1";

why are there different spaces and do we need spaces in the first place?

> +int snd_soc_usb_add_port(struct snd_soc_usb *usb)
> +{
> +	mutex_lock(&ctx_mutex);
> +	list_add_tail(&usb->list, &usb_ctx_list);
> +	mutex_unlock(&ctx_mutex);
> +
> +	return 0;

make the function return void?

> +int snd_soc_usb_remove_port(struct snd_soc_usb *usb)
> +{
> +	struct snd_soc_usb *ctx, *tmp;
> +
> +	mutex_lock(&ctx_mutex);
> +	list_for_each_entry_safe(ctx, tmp, &usb_ctx_list, list) {
> +		if (ctx == usb) {
> +			list_del(&ctx->list);
> +			break;
> +		}
> +	}
> +	mutex_unlock(&ctx_mutex);
> +
> +	return 0;

make this return void?
Pierre-Louis Bossart May 7, 2024, 8:37 p.m. UTC | #2
>  static struct snd_soc_dai_driver q6dsp_audio_fe_dais[] = {
> +	{
> +		.playback = {
> +			.stream_name = "USB Playback",
> +			.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_min =	8000,
> +			.rate_max = 192000,
> +		},
> +		.id = USB_RX,
> +		.name = "USB_RX",
> +	},

Wait, is this saying you will have exactly one PCM device/FE DAI
connected to the USB BE DAI exposed in patch 11?

> +	SND_SOC_DAPM_MIXER("USB Mixer", SND_SOC_NOPM, 0, 0,
> +			   usb_mixer_controls,
> +			   ARRAY_SIZE(usb_mixer_controls)),
> +

And then what is the role of the USB mixer if you only have one input?

I must be missing something.
Pierre-Louis Bossart May 7, 2024, 8:39 p.m. UTC | #3
> +config SND_SOC_QDSP6_USB
> +    tristate "SoC ALSA USB offloading backing for QDSP6"
> +    depends on SND_SOC_USB
> +    help
> +      Adds support for USB offloading for QDSP6 ASoC
> +      based platform sound cards.  This will enable the
> +      Q6USB DPCM backend DAI link, which will interact
> +      with the SoC USB framework to initialize a session
> +      with active USB SND devices.

If this is set to 'n', don't you have a risk of the FE DAIs exposed in
patch 9 not being connected to anything?
Pierre-Louis Bossart May 7, 2024, 9:20 p.m. UTC | #4
> 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.

What was the point of having a "USB Mixer" then?
Pierre-Louis Bossart May 7, 2024, 9:23 p.m. UTC | #5
> @@ -113,6 +120,12 @@ static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb,
>  	if (connected) {
>  		/* We only track the latest USB headset plugged in */
>  		data->active_usb_chip_idx = sdev->card_idx;
> +
> +		set_bit(sdev->card_idx, &data->available_card_slot);
> +		data->status[sdev->card_idx].sdev = sdev;

Not following the 'only track the latest USB headset plugged in', I
don't see anything that discard the previously latest headset...

If you plug headset1, then headset2, how is headset1 marked as not
available for USB offload?

> +	} else {
> +		clear_bit(sdev->card_idx, &data->available_card_slot);
> +		data->status[sdev->card_idx].sdev = NULL;
>  	}
>  
>  	return 0;
Pierre-Louis Bossart May 7, 2024, 9:26 p.m. UTC | #6
On 5/7/24 14:51, Wesley Cheng wrote:
> Add SND kcontrol to SOC USB, which will allow for userpsace to determine
> which USB card number and PCM device to offload.  This allows for userspace
> to potentially tag an alternate path for a specific USB SND card and PCM
> device.  Previously, control was absent, and the offload path would be
> enabled on the last USB SND device which was connected.  This logic will
> continue to be applicable if no mixer input is received for specific device
> selection.
> 
> An example to configure the offload device using tinymix:
> tinymix -D 0 set 'USB Offload Playback Route Select' 1 0
> 
> The above command will configure the offload path to utilize card#1 and PCM
> stream#0.

I don't know how this is usable in practice. Using card indices is
really hard to do, it depends on the order in which devices are
plugged-in...
Pierre-Louis Bossart May 7, 2024, 9:33 p.m. UTC | #7
> +/**
> + * snd_soc_usb_device_offload_available() - Fetch BE DAI link sound card
> + * @dev: the device to find in SOC USB
> + *
> + * Finds the component linked to a specific SOC USB instance, and returns
> + * the sound card number for the platform card supporting offloading.
> + *
> + */
> +int snd_soc_usb_device_offload_available(struct device *dev)
> +{
> +	struct snd_soc_usb *ctx;
> +
> +	ctx = snd_soc_find_usb_ctx(dev);
> +	if (!ctx)
> +		return -ENODEV;
> +
> +	return ctx->component->card->snd_card->number;
> +}

Presumably there's a notification to help applications discard this
information on removal?
Pierre-Louis Bossart May 7, 2024, 9:37 p.m. UTC | #8
On 5/7/24 14:51, Wesley Cheng wrote:
> In order to allow userspace/applications know about USB offloading status,
> expose a sound kcontrol that fetches information about which sound card
> index is associated with the ASoC platform card supporting offloading.  In
> the USB audio offloading framework, the ASoC BE DAI link is the entity
> responsible for registering to the SOC USB layer.  SOC USB will expose more
> details about the current offloading status, which includes the USB sound
> card and USB PCM device indexes currently being used.
> 
> It is expected for the USB offloading driver to add the kcontrol to the
> sound card associated with the USB audio device.  An example output would
> look like:
> 
> tinymix -D 1 get 'USB Offload Playback Capable Card'
> 0 (range -1->32)

You already gave the following examples in patch 29:

"
USB offloading idle:
tinymix -D 0 get 'USB Offload Playback Route Status'
-->-1, -1 (range -1->32)

USB offloading active(USB card#1 pcm#0):
tinymix -D 0 get 'USB Offload Playback Route Status'
-->1, 0 (range -1->32)
"

Can you clarify how many controls there would be in the end?
Also isn't tinymix -D N going to give you the controls for card N?
Pierre-Louis Bossart May 7, 2024, 9:40 p.m. UTC | #9
On 5/7/24 14:51, Wesley Cheng wrote:
> For userspace to know about certain capabilities of the current platform
> card, add tags to the components string that it can use to enable support
> for that audio path.  In case of USB offloading, the "usboffldplybk: 1" tag

usboffloadplayback?

same question as before, do we need spaces?

And if we have controls, why do we need component strings? The component
string is not dynamic to the best of my knowledge, this could be
problematic if the card is no longer capable of supporting this stream,
while a control can be updated at will.
Wesley Cheng May 8, 2024, 7:41 p.m. UTC | #10
Hi Pierre,

On 5/7/2024 2:37 PM, Pierre-Louis Bossart wrote:
> 
> 
> On 5/7/24 14:51, Wesley Cheng wrote:
>> In order to allow userspace/applications know about USB offloading status,
>> expose a sound kcontrol that fetches information about which sound card
>> index is associated with the ASoC platform card supporting offloading.  In
>> the USB audio offloading framework, the ASoC BE DAI link is the entity
>> responsible for registering to the SOC USB layer.  SOC USB will expose more
>> details about the current offloading status, which includes the USB sound
>> card and USB PCM device indexes currently being used.
>>
>> It is expected for the USB offloading driver to add the kcontrol to the
>> sound card associated with the USB audio device.  An example output would
>> look like:
>>
>> tinymix -D 1 get 'USB Offload Playback Capable Card'
>> 0 (range -1->32)
> 
> You already gave the following examples in patch 29:
> 
> "
> USB offloading idle:
> tinymix -D 0 get 'USB Offload Playback Route Status'
> -->-1, -1 (range -1->32)
> 
> USB offloading active(USB card#1 pcm#0):
> tinymix -D 0 get 'USB Offload Playback Route Status'
> -->1, 0 (range -1->32)
> "
> 
> Can you clarify how many controls there would be in the end?

For USB offload situations, there will be a set of controls for playback 
status and playback select.  The offload jack will also be there to tell 
us if there is an offload path available for the platform ASoC sound card.

> Also isn't tinymix -D N going to give you the controls for card N?
> 

Yes, since the offload portion is handled as a DPCM DAI link to the 
platform ASoC card, it will be included as a kcontrol for that.

Thanks
Wesley Cheng
Wesley Cheng May 8, 2024, 8:06 p.m. UTC | #11
Hi Pierre,

On 5/7/2024 2:40 PM, Pierre-Louis Bossart wrote:
> 
> 
> On 5/7/24 14:51, Wesley Cheng wrote:
>> For userspace to know about certain capabilities of the current platform
>> card, add tags to the components string that it can use to enable support
>> for that audio path.  In case of USB offloading, the "usboffldplybk: 1" tag
> 
> usboffloadplayback?
> 
> same question as before, do we need spaces?
> 

I think spaces are currently used as a delimiter, so I'll remove the spaces.

> And if we have controls, why do we need component strings? The component
> string is not dynamic to the best of my knowledge, this could be
> problematic if the card is no longer capable of supporting this stream,
> while a control can be updated at will.
> 

Maybe I misunderstood your comment here:

https://lore.kernel.org/linux-usb/925d7c03-c288-49a4-8bcd-395b32810d75@linux.intel.com/

At the time, I didn't include the kcontrols on the USB SND portion of 
it, which was added after this series.  My interpretation was that there 
were userspace entities that could query for general information about 
what the card supports based on the components string, or sound card 
name.  I treated this as an independent identifier, since the change to 
add the offload capable jack was present.

Thanks
Wesley Cheng
Wesley Cheng May 8, 2024, 8:16 p.m. UTC | #12
Hi Pierre,

On 5/7/2024 1:26 PM, Pierre-Louis Bossart wrote:
> 
>> +const char *snd_soc_usb_get_components_tag(bool playback)
>> +{
>> +	if (playback)
>> +		return "usbplaybackoffload: 1";
>> +	else
>> +		return "usbcaptureoffload : 1";
> 
> why are there different spaces and do we need spaces in the first place?
> 

Will remove these spaces once we clarify if this is still needed.

>> +int snd_soc_usb_add_port(struct snd_soc_usb *usb)
>> +{
>> +	mutex_lock(&ctx_mutex);
>> +	list_add_tail(&usb->list, &usb_ctx_list);
>> +	mutex_unlock(&ctx_mutex);
>> +
>> +	return 0;
> 
> make the function return void?
> 

Ack.

>> +int snd_soc_usb_remove_port(struct snd_soc_usb *usb)
>> +{
>> +	struct snd_soc_usb *ctx, *tmp;
>> +
>> +	mutex_lock(&ctx_mutex);
>> +	list_for_each_entry_safe(ctx, tmp, &usb_ctx_list, list) {
>> +		if (ctx == usb) {
>> +			list_del(&ctx->list);
>> +			break;
>> +		}
>> +	}
>> +	mutex_unlock(&ctx_mutex);
>> +
>> +	return 0;
> 
> make this return void?
> 
> 

Ack.

Thanks
Wesley Cheng
Wesley Cheng May 8, 2024, 11:34 p.m. UTC | #13
Hi Pierre,

On 5/7/2024 1:37 PM, Pierre-Louis Bossart wrote:
> 
>>   static struct snd_soc_dai_driver q6dsp_audio_fe_dais[] = {
>> +	{
>> +		.playback = {
>> +			.stream_name = "USB Playback",
>> +			.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_min =	8000,
>> +			.rate_max = 192000,
>> +		},
>> +		.id = USB_RX,
>> +		.name = "USB_RX",
>> +	},
> 
> Wait, is this saying you will have exactly one PCM device/FE DAI
> connected to the USB BE DAI exposed in patch 11?
> 
>> +	SND_SOC_DAPM_MIXER("USB Mixer", SND_SOC_NOPM, 0, 0,
>> +			   usb_mixer_controls,
>> +			   ARRAY_SIZE(usb_mixer_controls)),
>> +
> 
> And then what is the role of the USB mixer if you only have one input?
> 
> I must be missing something.
> 

Not sure if this is a QCOM specific implementation, but the way the DT 
is defined for the USB offload path is as follows:

	usb-dai-link {
		link-name = "USB Playback";

		cpu {
			sound-dai = <&q6afedai USB_RX>;
		};

		codec {
			sound-dai = <&usbdai USB_RX>;
		};

		platform {
			sound-dai = <&q6routing>;
		};
	};

Based on our DT parser helper API (qcom_snd_parse_of()) this isn't going 
to create a PCM device.  The PCM devices are created for nodes that 
don't have a codec and platform defined:

	mm1-dai-link {
		link-name = "MultiMedia1";
		cpu {
			sound-dai = <&q6asmdai  	MSM_FRONTEND_DAI_MULTIMEDIA1>;
		};
	};

The ASM path is the entity that defines the number of PCM devices that 
is created for the QC ASoC platform card, and is where the actual PCM 
data is sent over to the DSP.  So there could be several PCM devices 
that can use the USB BE DAI.

Thanks
Wesley Cheng
Wesley Cheng May 8, 2024, 11:40 p.m. UTC | #14
Hi Pierre,

On 5/7/2024 2:20 PM, Pierre-Louis Bossart wrote:
> 
>> 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.
> 
> What was the point of having a "USB Mixer" then?

The USB mixer is intended to enable/route the USB offloading path to the 
audio DSP, and is for controlling the ASoC specific entities.  This 
change is needed to resolve any contention between the USB SND PCM 
device (non offload path) and the ASoC USB BE DAI (offload path).

Thanks
Wesley Cheng
Wesley Cheng May 8, 2024, 11:57 p.m. UTC | #15
Hi Pierre,

On 5/7/2024 2:23 PM, Pierre-Louis Bossart wrote:
> 
>> @@ -113,6 +120,12 @@ static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb,
>>   	if (connected) {
>>   		/* We only track the latest USB headset plugged in */
>>   		data->active_usb_chip_idx = sdev->card_idx;
>> +
>> +		set_bit(sdev->card_idx, &data->available_card_slot);
>> +		data->status[sdev->card_idx].sdev = sdev;
> 
> Not following the 'only track the latest USB headset plugged in', I
> don't see anything that discard the previously latest headset...
> 
> If you plug headset1, then headset2, how is headset1 marked as not
> available for USB offload?
> 

It won't mark headset1 as not available for offload, because offload 
could happen on either depending on what is selected (from the kcontrol 
as well).

Thanks
Wesley Cheng
Wesley Cheng May 9, 2024, 12:10 a.m. UTC | #16
Hi Pierre,

On 5/7/2024 2:26 PM, Pierre-Louis Bossart wrote:
> 
> 
> On 5/7/24 14:51, Wesley Cheng wrote:
>> Add SND kcontrol to SOC USB, which will allow for userpsace to determine
>> which USB card number and PCM device to offload.  This allows for userspace
>> to potentially tag an alternate path for a specific USB SND card and PCM
>> device.  Previously, control was absent, and the offload path would be
>> enabled on the last USB SND device which was connected.  This logic will
>> continue to be applicable if no mixer input is received for specific device
>> selection.
>>
>> An example to configure the offload device using tinymix:
>> tinymix -D 0 set 'USB Offload Playback Route Select' 1 0
>>
>> The above command will configure the offload path to utilize card#1 and PCM
>> stream#0.
> 
> I don't know how this is usable in practice. Using card indices is
> really hard to do, it depends on the order in which devices are
> plugged-in...

How are the existing mechanisms handling USB audio devices, or what is 
the identifier being used?

Thanks
Wesley Cheng
Wesley Cheng May 9, 2024, 12:13 a.m. UTC | #17
Hi Pierre,

On 5/7/2024 2:33 PM, Pierre-Louis Bossart wrote:
> 
>> +/**
>> + * snd_soc_usb_device_offload_available() - Fetch BE DAI link sound card
>> + * @dev: the device to find in SOC USB
>> + *
>> + * Finds the component linked to a specific SOC USB instance, and returns
>> + * the sound card number for the platform card supporting offloading.
>> + *
>> + */
>> +int snd_soc_usb_device_offload_available(struct device *dev)
>> +{
>> +	struct snd_soc_usb *ctx;
>> +
>> +	ctx = snd_soc_find_usb_ctx(dev);
>> +	if (!ctx)
>> +		return -ENODEV;
>> +
>> +	return ctx->component->card->snd_card->number;
>> +}
> 
> Presumably there's a notification to help applications discard this
> information on removal?

If the platform/ASoC sound card is removed then the USB BE DAI is going 
to be unregistered from SOC USB.  This would lead to the 
snd_soc_find_usb_ctx() to return with -ENODEV to the USB SND offload 
kcontrol call.

Thanks
Wesley Cheng
Wesley Cheng May 9, 2024, 12:42 a.m. UTC | #18
Hi Pierre,

On 5/8/2024 12:41 PM, Wesley Cheng wrote:
> Hi Pierre,
> 
> On 5/7/2024 2:37 PM, Pierre-Louis Bossart wrote:
>>
>>
>> On 5/7/24 14:51, Wesley Cheng wrote:
>>> In order to allow userspace/applications know about USB offloading 
>>> status,
>>> expose a sound kcontrol that fetches information about which sound card
>>> index is associated with the ASoC platform card supporting 
>>> offloading.  In
>>> the USB audio offloading framework, the ASoC BE DAI link is the entity
>>> responsible for registering to the SOC USB layer.  SOC USB will 
>>> expose more
>>> details about the current offloading status, which includes the USB 
>>> sound
>>> card and USB PCM device indexes currently being used.
>>>
>>> It is expected for the USB offloading driver to add the kcontrol to the
>>> sound card associated with the USB audio device.  An example output 
>>> would
>>> look like:
>>>
>>> tinymix -D 1 get 'USB Offload Playback Capable Card'
>>> 0 (range -1->32)
>>
>> You already gave the following examples in patch 29:
>>
>> "
>> USB offloading idle:
>> tinymix -D 0 get 'USB Offload Playback Route Status'
>> -->-1, -1 (range -1->32)
>>
>> USB offloading active(USB card#1 pcm#0):
>> tinymix -D 0 get 'USB Offload Playback Route Status'
>> -->1, 0 (range -1->32)
>> "
>>
>> Can you clarify how many controls there would be in the end?
> 
> For USB offload situations, there will be a set of controls for playback 
> status and playback select.  The offload jack will also be there to tell 
> us if there is an offload path available for the platform ASoC sound card.
> 
>> Also isn't tinymix -D N going to give you the controls for card N?
>>
> 
> Yes, since the offload portion is handled as a DPCM DAI link to the 
> platform ASoC card, it will be included as a kcontrol for that.
> 
> Thanks
> Wesley Cheng
> 
> 

Sorry for responding again.  I read your email again, and wanted to also 
add that aside from the above, which are all within the ASoC layer, as 
we discussed previously, we should have a kcontrol in the USB SND card 
to determine if there is an ASoC platform card capable of offloading. 
This is also available from the SND card created by the USB audio device.

Thanks
Wesley Cheng
Pierre-Louis Bossart May 9, 2024, 12:54 p.m. UTC | #19
>> Wait, is this saying you will have exactly one PCM device/FE DAI
>> connected to the USB BE DAI exposed in patch 11?
>>
>>> +    SND_SOC_DAPM_MIXER("USB Mixer", SND_SOC_NOPM, 0, 0,
>>> +               usb_mixer_controls,
>>> +               ARRAY_SIZE(usb_mixer_controls)),
>>> +
>>
>> And then what is the role of the USB mixer if you only have one input?
>>
>> I must be missing something.
>>
> 
> Not sure if this is a QCOM specific implementation, but the way the DT
> is defined for the USB offload path is as follows:
> 
>     usb-dai-link {
>         link-name = "USB Playback";
> 
>         cpu {
>             sound-dai = <&q6afedai USB_RX>;
>         };
> 
>         codec {
>             sound-dai = <&usbdai USB_RX>;
>         };
> 
>         platform {
>             sound-dai = <&q6routing>;
>         };
>     };
> 
> Based on our DT parser helper API (qcom_snd_parse_of()) this isn't going
> to create a PCM device.  The PCM devices are created for nodes that
> don't have a codec and platform defined:
> 
>     mm1-dai-link {
>         link-name = "MultiMedia1";
>         cpu {
>             sound-dai = <&q6asmdai      MSM_FRONTEND_DAI_MULTIMEDIA1>;
>         };
>     };
> 
> The ASM path is the entity that defines the number of PCM devices that
> is created for the QC ASoC platform card, and is where the actual PCM
> data is sent over to the DSP.  So there could be several PCM devices
> that can use the USB BE DAI.

ok, but then how would this work with the ALSA controls reporting which
PCM device can be used? I didn't see a mechanism allowing for more than
one offloaded device, IIRC the control reported just ONE PCM device number.
Pierre-Louis Bossart May 9, 2024, 1:01 p.m. UTC | #20
On 5/8/24 18:40, Wesley Cheng wrote:
> Hi Pierre,
> 
> On 5/7/2024 2:20 PM, Pierre-Louis Bossart wrote:
>>
>>> 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.
>>
>> What was the point of having a "USB Mixer" then?
> 
> The USB mixer is intended to enable/route the USB offloading path to the
> audio DSP, and is for controlling the ASoC specific entities.  This
> change is needed to resolve any contention between the USB SND PCM
> device (non offload path) and the ASoC USB BE DAI (offload path).

Not following, sorry. Is the "USB Mixer" some sort of hardware entity
related to USB offload or just a pure DAPM processing widget handling
volume and actual mixing between streams?

I was trying to get clarity on whether there can be multiple streams
mixed before going to the USB endpoint. The commit message "only one
path is using the USB substream" is ambiguous, not sure if you are
referring to mutual exclusion between offloaded and non-offloaded paths,
or number of streams when offloaded is supported. Different concepts/levels.
Pierre-Louis Bossart May 9, 2024, 1:02 p.m. UTC | #21
On 5/8/24 18:57, Wesley Cheng wrote:
> Hi Pierre,
> 
> On 5/7/2024 2:23 PM, Pierre-Louis Bossart wrote:
>>
>>> @@ -113,6 +120,12 @@ static int q6usb_alsa_connection_cb(struct
>>> snd_soc_usb *usb,
>>>       if (connected) {
>>>           /* We only track the latest USB headset plugged in */
>>>           data->active_usb_chip_idx = sdev->card_idx;
>>> +
>>> +        set_bit(sdev->card_idx, &data->available_card_slot);
>>> +        data->status[sdev->card_idx].sdev = sdev;
>>
>> Not following the 'only track the latest USB headset plugged in', I
>> don't see anything that discard the previously latest headset...
>>
>> If you plug headset1, then headset2, how is headset1 marked as not
>> available for USB offload?
>>
> 
> It won't mark headset1 as not available for offload, because offload
> could happen on either depending on what is selected (from the kcontrol
> as well).

Right, so the wording 'only track the latest USB headset plugged in' is
incorrect or obsolete, isn't it?
Pierre-Louis Bossart May 9, 2024, 1:07 p.m. UTC | #22
On 5/8/24 19:10, Wesley Cheng wrote:
> Hi Pierre,
> 
> On 5/7/2024 2:26 PM, Pierre-Louis Bossart wrote:
>>
>>
>> On 5/7/24 14:51, Wesley Cheng wrote:
>>> Add SND kcontrol to SOC USB, which will allow for userpsace to determine
>>> which USB card number and PCM device to offload.  This allows for
>>> userspace
>>> to potentially tag an alternate path for a specific USB SND card and PCM
>>> device.  Previously, control was absent, and the offload path would be
>>> enabled on the last USB SND device which was connected.  This logic will
>>> continue to be applicable if no mixer input is received for specific
>>> device
>>> selection.
>>>
>>> An example to configure the offload device using tinymix:
>>> tinymix -D 0 set 'USB Offload Playback Route Select' 1 0
>>>
>>> The above command will configure the offload path to utilize card#1
>>> and PCM
>>> stream#0.
>>
>> I don't know how this is usable in practice. Using card indices is
>> really hard to do, it depends on the order in which devices are
>> plugged-in...
> 
> How are the existing mechanisms handling USB audio devices, or what is
> the identifier being used?

Well it's a mess, that's why I asked.

There are configuration work-arounds to make sure that 'local'
accessories are handled first and get repeatable card indices.

But between USB devices I guess the rule is 'anything goes'. Even if
there are two devices connected at boot, the index allocation will
depend on probe order. The card names are not necessarily super-useful
either, i.e. yesterday I was confused by an USB card named "CODEC"
without any details.
Pierre-Louis Bossart May 9, 2024, 1:11 p.m. UTC | #23
>>>> It is expected for the USB offloading driver to add the kcontrol to the
>>>> sound card associated with the USB audio device.  An example output
>>>> would
>>>> look like:
>>>>
>>>> tinymix -D 1 get 'USB Offload Playback Capable Card'
>>>> 0 (range -1->32)
>>>
>>> You already gave the following examples in patch 29:
>>>
>>> "
>>> USB offloading idle:
>>> tinymix -D 0 get 'USB Offload Playback Route Status'
>>> -->-1, -1 (range -1->32)
>>>
>>> USB offloading active(USB card#1 pcm#0):
>>> tinymix -D 0 get 'USB Offload Playback Route Status'
>>> -->1, 0 (range -1->32)
>>> "
>>>
>>> Can you clarify how many controls there would be in the end?
>>
>> For USB offload situations, there will be a set of controls for
>> playback status and playback select.  The offload jack will also be
>> there to tell us if there is an offload path available for the
>> platform ASoC sound card.
>>
>>> Also isn't tinymix -D N going to give you the controls for card N?
>>>
>>
>> Yes, since the offload portion is handled as a DPCM DAI link to the
>> platform ASoC card, it will be included as a kcontrol for that.
>>
>> Thanks
>> Wesley Cheng
>>
>>
> 
> Sorry for responding again.  I read your email again, and wanted to also
> add that aside from the above, which are all within the ASoC layer, as
> we discussed previously, we should have a kcontrol in the USB SND card
> to determine if there is an ASoC platform card capable of offloading.
> This is also available from the SND card created by the USB audio device.

That makes sense: if the application wanted to use a given endpoint, it
could check if there is a 'better' path exposed by another card. It'd be
a lot easier than reading controls from random cards.

Was this part of this patchset or more of an idea for a future addition?
Pierre-Louis Bossart May 9, 2024, 1:17 p.m. UTC | #24
On 5/8/24 15:06, Wesley Cheng wrote:
> Hi Pierre,
> 
> On 5/7/2024 2:40 PM, Pierre-Louis Bossart wrote:
>>
>>
>> On 5/7/24 14:51, Wesley Cheng wrote:
>>> For userspace to know about certain capabilities of the current platform
>>> card, add tags to the components string that it can use to enable
>>> support
>>> for that audio path.  In case of USB offloading, the "usboffldplybk:
>>> 1" tag
>>
>> usboffloadplayback?
>>
>> same question as before, do we need spaces?
>>
> 
> I think spaces are currently used as a delimiter, so I'll remove the
> spaces.
> 
>> And if we have controls, why do we need component strings? The component
>> string is not dynamic to the best of my knowledge, this could be
>> problematic if the card is no longer capable of supporting this stream,
>> while a control can be updated at will.
>>
> 
> Maybe I misunderstood your comment here:
> 
> https://lore.kernel.org/linux-usb/925d7c03-c288-49a4-8bcd-395b32810d75@linux.intel.com/
> 
> At the time, I didn't include the kcontrols on the USB SND portion of
> it, which was added after this series.  My interpretation was that there
> were userspace entities that could query for general information about
> what the card supports based on the components string, or sound card
> name.  I treated this as an independent identifier, since the change to
> add the offload capable jack was present.

My comment at the time stands: it's very hard to figure out that a
random card supports USB and is connected to a given endpoint.

It'd be much easier as you wrote in the comments on patch 34 to have a
control in the "regular" USB card to point to the 'better' offloaded
path exposed by another card. Applications wouldn't need to know what
this other card is, they would then use the card:device information
directly.
Wesley Cheng May 9, 2024, 9:34 p.m. UTC | #25
Hi Pierre,

On 5/9/2024 5:54 AM, Pierre-Louis Bossart wrote:
> 
> 
> 
>>> Wait, is this saying you will have exactly one PCM device/FE DAI
>>> connected to the USB BE DAI exposed in patch 11?
>>>
>>>> +    SND_SOC_DAPM_MIXER("USB Mixer", SND_SOC_NOPM, 0, 0,
>>>> +               usb_mixer_controls,
>>>> +               ARRAY_SIZE(usb_mixer_controls)),
>>>> +
>>>
>>> And then what is the role of the USB mixer if you only have one input?
>>>
>>> I must be missing something.
>>>
>>
>> Not sure if this is a QCOM specific implementation, but the way the DT
>> is defined for the USB offload path is as follows:
>>
>>      usb-dai-link {
>>          link-name = "USB Playback";
>>
>>          cpu {
>>              sound-dai = <&q6afedai USB_RX>;
>>          };
>>
>>          codec {
>>              sound-dai = <&usbdai USB_RX>;
>>          };
>>
>>          platform {
>>              sound-dai = <&q6routing>;
>>          };
>>      };
>>
>> Based on our DT parser helper API (qcom_snd_parse_of()) this isn't going
>> to create a PCM device.  The PCM devices are created for nodes that
>> don't have a codec and platform defined:
>>
>>      mm1-dai-link {
>>          link-name = "MultiMedia1";
>>          cpu {
>>              sound-dai = <&q6asmdai      MSM_FRONTEND_DAI_MULTIMEDIA1>;
>>          };
>>      };
>>
>> The ASM path is the entity that defines the number of PCM devices that
>> is created for the QC ASoC platform card, and is where the actual PCM
>> data is sent over to the DSP.  So there could be several PCM devices
>> that can use the USB BE DAI.
> 
> ok, but then how would this work with the ALSA controls reporting which
> PCM device can be used? I didn't see a mechanism allowing for more than
> one offloaded device, IIRC the control reported just ONE PCM device number.

With respects to the PCM devices exposed by the ASoC card, the USB Mixer 
controls which "Multimedia" (ASM) path can be routed to the USB BE DAI.

The kcontrols you are mentioning are controlling which USB card and USB 
PCM device to execute the offloading on.  As of now, at least for the 
QCOM implementation, we support only offloading on one path/USB 
interface.  I can't comment on how other offloading solutions look like, 
but we pass the USB PCM and card index as part of our AFE port open 
command (done from USB BE DAI).  This will result in a USB QMI message 
back (from ADSP) to our USB SND offload driver, which carries all the 
information about the selected card and PCM index to execute offloading on.

One thing I can do is to actually make the kcontrols for selecting the 
PCM and card devices to look at the num_supported_streams.  This would 
at least allow for vendors that have support for more potential 
offloading streams to select more than one.

Thanks
Wesley Cheng
Wesley Cheng May 9, 2024, 9:38 p.m. UTC | #26
Hi Pierre,

On 5/9/2024 6:01 AM, Pierre-Louis Bossart wrote:
> 
> 
> On 5/8/24 18:40, Wesley Cheng wrote:
>> Hi Pierre,
>>
>> On 5/7/2024 2:20 PM, Pierre-Louis Bossart wrote:
>>>
>>>> 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.
>>>
>>> What was the point of having a "USB Mixer" then?
>>
>> The USB mixer is intended to enable/route the USB offloading path to the
>> audio DSP, and is for controlling the ASoC specific entities.  This
>> change is needed to resolve any contention between the USB SND PCM
>> device (non offload path) and the ASoC USB BE DAI (offload path).
> 
> Not following, sorry. Is the "USB Mixer" some sort of hardware entity
> related to USB offload or just a pure DAPM processing widget handling
> volume and actual mixing between streams?
> 

It controls which Multimedia (ASM) stream can be routed to the USB BE DAI.

> I was trying to get clarity on whether there can be multiple streams
> mixed before going to the USB endpoint. The commit message "only one
> path is using the USB substream" is ambiguous, not sure if you are
> referring to mutual exclusion between offloaded and non-offloaded paths,
> or number of streams when offloaded is supported. Different concepts/levels.

Ideally we shouldn't.  Only one ASM stream should be allowed to open the 
USB AFE port at a time.

Thanks
Wesley Cheng
Wesley Cheng May 9, 2024, 9:49 p.m. UTC | #27
Hi Pierre,

On 5/9/2024 6:02 AM, Pierre-Louis Bossart wrote:
> 
> 
> On 5/8/24 18:57, Wesley Cheng wrote:
>> Hi Pierre,
>>
>> On 5/7/2024 2:23 PM, Pierre-Louis Bossart wrote:
>>>
>>>> @@ -113,6 +120,12 @@ static int q6usb_alsa_connection_cb(struct
>>>> snd_soc_usb *usb,
>>>>        if (connected) {
>>>>            /* We only track the latest USB headset plugged in */
>>>>            data->active_usb_chip_idx = sdev->card_idx;
>>>> +
>>>> +        set_bit(sdev->card_idx, &data->available_card_slot);
>>>> +        data->status[sdev->card_idx].sdev = sdev;
>>>
>>> Not following the 'only track the latest USB headset plugged in', I
>>> don't see anything that discard the previously latest headset...
>>>
>>> If you plug headset1, then headset2, how is headset1 marked as not
>>> available for USB offload?
>>>
>>
>> It won't mark headset1 as not available for offload, because offload
>> could happen on either depending on what is selected (from the kcontrol
>> as well).
> 
> Right, so the wording 'only track the latest USB headset plugged in' is
> incorrect or obsolete, isn't it?

Sure, I can reword it.  Will specify that it
"selects the latest USB headset plugged in for offloading"

Thanks
Wesley Cheng
Wesley Cheng May 9, 2024, 10:05 p.m. UTC | #28
Hi Pierre,

On 5/9/2024 6:11 AM, Pierre-Louis Bossart wrote:
> 
>>>>> It is expected for the USB offloading driver to add the kcontrol to the
>>>>> sound card associated with the USB audio device.  An example output
>>>>> would
>>>>> look like:
>>>>>
>>>>> tinymix -D 1 get 'USB Offload Playback Capable Card'
>>>>> 0 (range -1->32)
>>>>
>>>> You already gave the following examples in patch 29:
>>>>
>>>> "
>>>> USB offloading idle:
>>>> tinymix -D 0 get 'USB Offload Playback Route Status'
>>>> -->-1, -1 (range -1->32)
>>>>
>>>> USB offloading active(USB card#1 pcm#0):
>>>> tinymix -D 0 get 'USB Offload Playback Route Status'
>>>> -->1, 0 (range -1->32)
>>>> "
>>>>
>>>> Can you clarify how many controls there would be in the end?
>>>
>>> For USB offload situations, there will be a set of controls for
>>> playback status and playback select.  The offload jack will also be
>>> there to tell us if there is an offload path available for the
>>> platform ASoC sound card.
>>>
>>>> Also isn't tinymix -D N going to give you the controls for card N?
>>>>
>>>
>>> Yes, since the offload portion is handled as a DPCM DAI link to the
>>> platform ASoC card, it will be included as a kcontrol for that.
>>>
>>> Thanks
>>> Wesley Cheng
>>>
>>>
>>
>> Sorry for responding again.  I read your email again, and wanted to also
>> add that aside from the above, which are all within the ASoC layer, as
>> we discussed previously, we should have a kcontrol in the USB SND card
>> to determine if there is an ASoC platform card capable of offloading.
>> This is also available from the SND card created by the USB audio device.
> 
> That makes sense: if the application wanted to use a given endpoint, it
> could check if there is a 'better' path exposed by another card. It'd be
> a lot easier than reading controls from random cards.
> 
> Was this part of this patchset or more of an idea for a future addition?

Its part of this patchset.  Please refer to patch#34.  The 
mixer_usb_offload is initialized by the offload entity residing in USB 
SND (qc_usb_audio_offload), and will add it to the sound card associated 
with the USB device.

Thanks
Wesley Cheng
Wesley Cheng May 9, 2024, 10:09 p.m. UTC | #29
Hi Pierre,

On 5/9/2024 6:17 AM, Pierre-Louis Bossart wrote:
> 
> 
> On 5/8/24 15:06, Wesley Cheng wrote:
>> Hi Pierre,
>>
>> On 5/7/2024 2:40 PM, Pierre-Louis Bossart wrote:
>>>
>>>
>>> On 5/7/24 14:51, Wesley Cheng wrote:
>>>> For userspace to know about certain capabilities of the current platform
>>>> card, add tags to the components string that it can use to enable
>>>> support
>>>> for that audio path.  In case of USB offloading, the "usboffldplybk:
>>>> 1" tag
>>>
>>> usboffloadplayback?
>>>
>>> same question as before, do we need spaces?
>>>
>>
>> I think spaces are currently used as a delimiter, so I'll remove the
>> spaces.
>>
>>> And if we have controls, why do we need component strings? The component
>>> string is not dynamic to the best of my knowledge, this could be
>>> problematic if the card is no longer capable of supporting this stream,
>>> while a control can be updated at will.
>>>
>>
>> Maybe I misunderstood your comment here:
>>
>> https://lore.kernel.org/linux-usb/925d7c03-c288-49a4-8bcd-395b32810d75@linux.intel.com/
>>
>> At the time, I didn't include the kcontrols on the USB SND portion of
>> it, which was added after this series.  My interpretation was that there
>> were userspace entities that could query for general information about
>> what the card supports based on the components string, or sound card
>> name.  I treated this as an independent identifier, since the change to
>> add the offload capable jack was present.
> 
> My comment at the time stands: it's very hard to figure out that a
> random card supports USB and is connected to a given endpoint.
> 
> It'd be much easier as you wrote in the comments on patch 34 to have a
> control in the "regular" USB card to point to the 'better' offloaded
> path exposed by another card. Applications wouldn't need to know what
> this other card is, they would then use the card:device information
> directly.

OK, then it might be fine to remove the components tag if patch#34 is 
there.  That kcontrol is exposed as part of the sound card created for 
the USB device, so if applications queried, it would signify that there 
is an offload path available.

For this kcontrol, it will return the ASoC platform card index, would 
that be sufficient?

Thanks
Wesley Cheng
Wesley Cheng May 9, 2024, 10:32 p.m. UTC | #30
Hi Pierre,

On 5/9/2024 6:07 AM, Pierre-Louis Bossart wrote:
> 
> 
> On 5/8/24 19:10, Wesley Cheng wrote:
>> Hi Pierre,
>>
>> On 5/7/2024 2:26 PM, Pierre-Louis Bossart wrote:
>>>
>>>
>>> On 5/7/24 14:51, Wesley Cheng wrote:
>>>> Add SND kcontrol to SOC USB, which will allow for userpsace to determine
>>>> which USB card number and PCM device to offload.  This allows for
>>>> userspace
>>>> to potentially tag an alternate path for a specific USB SND card and PCM
>>>> device.  Previously, control was absent, and the offload path would be
>>>> enabled on the last USB SND device which was connected.  This logic will
>>>> continue to be applicable if no mixer input is received for specific
>>>> device
>>>> selection.
>>>>
>>>> An example to configure the offload device using tinymix:
>>>> tinymix -D 0 set 'USB Offload Playback Route Select' 1 0
>>>>
>>>> The above command will configure the offload path to utilize card#1
>>>> and PCM
>>>> stream#0.
>>>
>>> I don't know how this is usable in practice. Using card indices is
>>> really hard to do, it depends on the order in which devices are
>>> plugged-in...
>>
>> How are the existing mechanisms handling USB audio devices, or what is
>> the identifier being used?
> 
> Well it's a mess, that's why I asked.
> 
> There are configuration work-arounds to make sure that 'local'
> accessories are handled first and get repeatable card indices.
> 

So is the intention of the configuration aspect you're thinking of to 
have an entry that maps a USB device based on some identifier, which 
will take the offload path by default?

IMO, the concept of this selection of card and PCM device should happen 
after the application discovers a USB device that is offload capable. 
For example, maybe the application will use the USB VID/PID to lookup an 
entry within the configuration.  If some offload tag is present, it can 
further determine which card and PCM devices are associated w/ the USB 
device?  Although this is under the assumption the application has 
insight to the USB sysfs.

> But between USB devices I guess the rule is 'anything goes'. Even if
> there are two devices connected at boot, the index allocation will
> depend on probe order. The card names are not necessarily super-useful
> either, i.e. yesterday I was confused by an USB card named "CODEC"
> without any details.

That device is very informative :D

Thanks
Wesley Cheng