2023-07-25 02:47:44

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 00/32] Introduce QC USB SND audio offloading support

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

Changelog
--------------------------------------------
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 (3):
xhci: add support to allocate several interrupters
xhci: add helper to stop endpoint and wait for completion
xhci: sideband: add initial api to register a sideband entity

Wesley Cheng (29):
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: 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
usb: host: xhci-plat: Set XHCI max interrupters if property is present
sound: usb: pcm: Export fixed rate check USB SND API
sound: usb: qcom: Add USB QMI definitions
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
ASoC: qcom: qdsp6: q6afe: Split USB AFE dev_token param into separate
API
sound: Pass USB SND card and PCM information to SOC USB
sound: soc: qdsp6: Add SND kcontrol to select offload device
sound: soc: qdsp6: Add SND kcontrol for fetching offload status
sound: soc: qcom: q6usb: Add headphone jack for offload connection
status
sound: usb: qc_audio_offload: Use card and PCM index from QMI request
sound: usb: card: Allow for rediscovery of connected USB SND devices
sound: soc: soc-usb: Rediscover USB SND devices on USB port add

.../bindings/sound/qcom,q6usb-dais.yaml | 49 +
.../bindings/sound/qcom,sm8250.yaml | 15 +
.../devicetree/bindings/usb/snps,dwc3.yaml | 13 +
arch/arm64/boot/dts/qcom/sm8350.dtsi.rej | 14 +
arch/arm64/configs/defconfig.rej | 24 +
drivers/usb/dwc3/core.c | 12 +
drivers/usb/dwc3/core.h | 2 +
drivers/usb/dwc3/host.c | 5 +-
drivers/usb/host/Kconfig | 9 +
drivers/usb/host/Makefile | 4 +
drivers/usb/host/xhci-debugfs.c | 2 +-
drivers/usb/host/xhci-hub.c | 29 +-
drivers/usb/host/xhci-mem.c | 167 +-
drivers/usb/host/xhci-plat.c | 2 +
drivers/usb/host/xhci-ring.c | 2 +-
drivers/usb/host/xhci-sideband.c | 195 ++
drivers/usb/host/xhci.c | 109 +-
drivers/usb/host/xhci.h | 102 +-
.../sound/qcom,q6dsp-lpass-ports.h | 1 +
include/linux/usb/xhci-intr.h | 86 +
include/linux/usb/xhci-sideband.h | 90 +
include/sound/q6usboffload.h | 20 +
include/sound/soc-usb.h | 39 +
sound/core/oss/pcm_oss.c | 4 +-
sound/soc/Makefile | 2 +-
sound/soc/qcom/Kconfig | 4 +
sound/soc/qcom/qdsp6/Makefile | 1 +
sound/soc/qcom/qdsp6/q6afe-dai.c | 50 +
sound/soc/qcom/qdsp6/q6afe.c | 208 +-
sound/soc/qcom/qdsp6/q6afe.h | 47 +-
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 | 458 +++++
sound/soc/soc-usb.c | 197 ++
sound/usb/Kconfig | 15 +
sound/usb/Makefile | 2 +-
sound/usb/card.c | 85 +
sound/usb/card.h | 32 +
sound/usb/endpoint.c | 2 +
sound/usb/helper.c | 1 +
sound/usb/pcm.c | 29 +-
sound/usb/pcm.h | 12 +
sound/usb/qcom/Makefile | 2 +
sound/usb/qcom/qc_audio_offload.c | 1820 +++++++++++++++++
sound/usb/qcom/usb_audio_qmi_v01.c | 892 ++++++++
sound/usb/qcom/usb_audio_qmi_v01.h | 162 ++
47 files changed, 4870 insertions(+), 179 deletions(-)
create mode 100644 Documentation/devicetree/bindings/sound/qcom,q6usb-dais.yaml
create mode 100644 arch/arm64/boot/dts/qcom/sm8350.dtsi.rej
create mode 100644 arch/arm64/configs/defconfig.rej
create mode 100644 drivers/usb/host/xhci-sideband.c
create mode 100644 include/linux/usb/xhci-intr.h
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/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



2023-07-25 02:47:59

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 23/32] ASoC: dt-bindings: Add Q6USB backend bindings

Add a dt-binding to describe the definition of enabling the Q6 USB backend
device for audio offloading. The node carries information, which is passed
along to the QC USB SND class driver counterpart. These parameters will be
utilized during QMI stream enable requests.

Signed-off-by: Wesley Cheng <[email protected]>
---
.../bindings/sound/qcom,q6usb-dais.yaml | 49 +++++++++++++++++++
1 file changed, 49 insertions(+)
create mode 100644 Documentation/devicetree/bindings/sound/qcom,q6usb-dais.yaml

diff --git a/Documentation/devicetree/bindings/sound/qcom,q6usb-dais.yaml b/Documentation/devicetree/bindings/sound/qcom,q6usb-dais.yaml
new file mode 100644
index 000000000000..77bc9b4c3c90
--- /dev/null
+++ b/Documentation/devicetree/bindings/sound/qcom,q6usb-dais.yaml
@@ -0,0 +1,49 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/sound/qcom,q6usb-dais.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Qualcomm ASoC DPCM USB backend DAI
+
+maintainers:
+ - Wesley Cheng <[email protected]>
+
+description:
+ The USB port is a supported AFE path on the Q6 DSP. This ASoC DPCM
+ backend DAI driver will communicate the required settings to the QC USB SND
+ class driver for properly enabling the audio stream. Parameters defined
+ under this node will carry settings, which will be passed along during
+ the QMI stream enable request.
+
+properties:
+ compatible:
+ enum:
+ - qcom,q6usb
+
+ iommus:
+ maxItems: 1
+
+ "#sound-dai-cells":
+ const: 1
+
+ qcom,usb-audio-intr-num:
+ description:
+ Desired XHCI interrupter number to use.
+ $ref: /schemas/types.yaml#/definitions/uint32
+
+required:
+ - compatible
+ - "#sound-dai-cells"
+ - qcom,usb-audio-intr-num
+
+additionalProperties: false
+
+examples:
+ - |
+ dais {
+ compatible = "qcom,q6usb";
+ #sound-dai-cells = <1>;
+ iommus = <&apps_smmu 0x180f 0x0>;
+ qcom,usb-audio-intr-num = <2>;
+ };

2023-07-25 02:50:50

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 28/32] sound: soc: qdsp6: Add SND kcontrol for fetching offload status

Add a kcontrol to the platform sound card to fetch the current offload
status. This can allow for userspace to ensure/check which USB SND
resources are actually busy versus having to attempt opening the USB SND
devices, which will result in an error if offloading is active.

Signed-off-by: Wesley Cheng <[email protected]>
---
sound/soc/qcom/qdsp6/q6usb.c | 104 ++++++++++++++++++++++++++++++++++-
1 file changed, 101 insertions(+), 3 deletions(-)

diff --git a/sound/soc/qcom/qdsp6/q6usb.c b/sound/soc/qcom/qdsp6/q6usb.c
index 87e88dafd8ad..e4ccb9d912b0 100644
--- a/sound/soc/qcom/qdsp6/q6usb.c
+++ b/sound/soc/qcom/qdsp6/q6usb.c
@@ -30,6 +30,8 @@ struct q6usb_status {
unsigned int num_pcm;
unsigned int chip_index;
unsigned int pcm_index;
+ bool prepared;
+ bool running;
};

struct q6usb_port_data {
@@ -52,6 +54,17 @@ static const struct snd_soc_dapm_route q6usb_dapm_routes[] = {
{"USB Playback", NULL, "USB_RX_BE"},
};

+static int q6usb_find_running(struct q6usb_port_data *data)
+{
+ int i;
+
+ for (i = 0; i < SNDRV_CARDS; i++) {
+ if (data->status[i].running)
+ return i;
+ }
+ return -ENODEV;
+}
+
static int q6usb_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params,
struct snd_soc_dai *dai)
@@ -81,14 +94,40 @@ static int q6usb_hw_params(struct snd_pcm_substream *substream,
goto out;

data->status[data->sel_card_idx].pcm_index = data->sel_pcm_idx;
+ data->status[data->sel_card_idx].prepared = true;
out:
mutex_unlock(&data->mutex);

return ret;
}

+static int q6usb_prepare(struct snd_pcm_substream *substream,
+ struct snd_soc_dai *dai)
+{
+ struct q6usb_port_data *data = dev_get_drvdata(dai->dev);
+
+ mutex_lock(&data->mutex);
+ data->status[data->sel_card_idx].running = true;
+ mutex_unlock(&data->mutex);
+
+ return 0;
+}
+
+static void q6usb_shutdown(struct snd_pcm_substream *substream,
+ struct snd_soc_dai *dai)
+{
+ struct q6usb_port_data *data = dev_get_drvdata(dai->dev);
+
+ mutex_lock(&data->mutex);
+ data->status[data->sel_card_idx].running = false;
+ data->status[data->sel_card_idx].prepared = false;
+ mutex_unlock(&data->mutex);
+}
+
static const struct snd_soc_dai_ops q6usb_ops = {
.hw_params = q6usb_hw_params,
+ .prepare = q6usb_prepare,
+ .shutdown = q6usb_shutdown,
};

static struct snd_soc_dai_driver q6usb_be_dais[] = {
@@ -148,10 +187,15 @@ static int q6usb_put_offload_dev(struct snd_kcontrol *kcontrol,
int pcmidx;
int cardidx;

+ mutex_lock(&data->mutex);
+
+ /* Don't allow changes to the offloading devices if session is busy */
+ if (data->sel_card_idx >= 0 && data->status[data->sel_card_idx].prepared)
+ goto out;
+
cardidx = ucontrol->value.integer.value[0];
pcmidx = ucontrol->value.integer.value[1];

- mutex_lock(&data->mutex);
if ((cardidx >= 0 && test_bit(cardidx, &data->available_card_slot))) {
data->sel_card_idx = cardidx;
changed = 1;
@@ -162,6 +206,8 @@ static int q6usb_put_offload_dev(struct snd_kcontrol *kcontrol,
data->idx_valid = true;
changed = 1;
}
+
+out:
mutex_unlock(&data->mutex);

return changed;
@@ -187,11 +233,59 @@ static const struct snd_kcontrol_new q6usb_offload_dev_ctrl = {
.put = q6usb_put_offload_dev,
};

+static int q6usb_mixer_get_offload_status(struct snd_kcontrol *kcontrol,
+ struct snd_ctl_elem_value *ucontrol)
+{
+ struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
+ struct q6usb_port_data *data = dev_get_drvdata(component->dev);
+ int running;
+ int card_idx;
+ int pcm_idx;
+
+ running = q6usb_find_running(data);
+ if (running < 0) {
+ card_idx = -1;
+ pcm_idx = -1;
+ } else {
+ card_idx = running;
+ pcm_idx = data->status[running].pcm_index;
+ }
+
+ ucontrol->value.integer.value[0] = card_idx;
+ ucontrol->value.integer.value[1] = pcm_idx;
+ return 0;
+}
+
+static int q6usb_offload_ctl_info(struct snd_kcontrol *kcontrol,
+ struct snd_ctl_elem_info *uinfo)
+{
+ uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
+ uinfo->count = 2;
+ uinfo->value.integer.min = 0;
+ uinfo->value.integer.max = SNDRV_CARDS;
+
+ return 0;
+}
+
+static const struct snd_kcontrol_new q6usb_offload_control = {
+ .iface = SNDRV_CTL_ELEM_IFACE_CARD,
+ .access = SNDRV_CTL_ELEM_ACCESS_READ,
+ .name = "Q6USB offload status",
+ .info = q6usb_offload_ctl_info,
+ .get = q6usb_mixer_get_offload_status,
+ .put = NULL,
+};
+
/* Build a mixer control for a UAC connector control (jack-detect) */
static void q6usb_connector_control_init(struct snd_soc_component *component)
{
int ret;

+ ret = snd_ctl_add(component->card->snd_card,
+ snd_ctl_new1(&q6usb_offload_control, component));
+ if (ret < 0)
+ return;
+
ret = snd_ctl_add(component->card->snd_card,
snd_ctl_new1(&q6usb_offload_dev_ctrl, component));
if (ret < 0)
@@ -229,8 +323,12 @@ static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
mutex_lock(&data->mutex);
if (connected) {
snd_soc_dapm_enable_pin(dapm, "USB_RX_BE");
- /* We only track the latest USB headset plugged in */
- if (!data->idx_valid || data->sel_card_idx < 0)
+ /*
+ * Update the latest USB headset plugged in, if session is
+ * idle.
+ */
+ if ((!data->idx_valid || data->sel_card_idx < 0) &&
+ !data->status[data->sel_card_idx].prepared)
data->sel_card_idx = card_idx;

set_bit(card_idx, &data->available_card_slot);

2023-07-25 02:58:32

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 05/32] usb: host: xhci-mem: Allow for interrupter clients to choose specific index

Some clients may operate only on a specific XHCI interrupter instance.
Allow for the associated class driver to request for the interrupter that
it requires.

Signed-off-by: Wesley Cheng <[email protected]>
---
drivers/usb/host/xhci-mem.c | 12 ++++++------
include/linux/usb/xhci-intr.h | 2 +-
2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 6b01d56c176f..310137df06eb 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -2396,7 +2396,7 @@ xhci_add_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir,
}

struct xhci_interrupter *
-xhci_create_secondary_interrupter(struct usb_hcd *hcd)
+xhci_create_secondary_interrupter(struct usb_hcd *hcd, int intr_num)
{
struct xhci_hcd *xhci = hcd_to_xhci(hcd);
struct xhci_interrupter *ir;
@@ -2411,15 +2411,15 @@ xhci_create_secondary_interrupter(struct usb_hcd *hcd)
return NULL;

spin_lock_irq(&xhci->lock);
-
/* Find available secondary interrupter, interrupter 0 is reserverd for primary */
for (i = 1; i < xhci->max_interrupters; i++) {
- if (xhci->interrupters[i] == NULL) {
- err = xhci_add_interrupter(xhci, ir, i);
- break;
+ if ((intr_num > 0 && i == intr_num) || intr_num <= 0) {
+ if (xhci->interrupters[i] == NULL) {
+ err = xhci_add_interrupter(xhci, ir, i);
+ break;
+ }
}
}
-
spin_unlock_irq(&xhci->lock);

if (!err) {
diff --git a/include/linux/usb/xhci-intr.h b/include/linux/usb/xhci-intr.h
index e0091ee2c73a..738b0f0481a6 100644
--- a/include/linux/usb/xhci-intr.h
+++ b/include/linux/usb/xhci-intr.h
@@ -81,6 +81,6 @@ struct xhci_interrupter {
};

struct xhci_interrupter *
-xhci_create_secondary_interrupter(struct usb_hcd *hcd);
+xhci_create_secondary_interrupter(struct usb_hcd *hcd, int intr_num);
void xhci_remove_secondary_interrupter(struct usb_hcd *hcd, struct xhci_interrupter *ir);
#endif

2023-07-25 02:58:32

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 02/32] xhci: add helper to stop endpoint and wait for completion

From: Mathias Nyman <[email protected]>

Expose xhci_stop_endpoint_sync() which is a synchronous variant of
xhci_queue_stop_endpoint(). This is useful for client drivers that are
using the secondary interrupters, and need to stop/clean up the current
session. The stop endpoint command handler will also take care of cleaning
up the ring.

Modifications to repurpose the new API into existing stop endpoint
sequences was implemented by Wesley Cheng.

Signed-off-by: Mathias Nyman <[email protected]>
Co-developed-by: Wesley Cheng <[email protected]>
Signed-off-by: Wesley Cheng <[email protected]>
---
drivers/usb/host/xhci-hub.c | 29 +++---------------
drivers/usb/host/xhci.c | 60 +++++++++++++++++++++++++++----------
drivers/usb/host/xhci.h | 2 ++
3 files changed, 50 insertions(+), 41 deletions(-)

diff --git a/drivers/usb/host/xhci-hub.c b/drivers/usb/host/xhci-hub.c
index 0054d02239e2..2f7309bdc922 100644
--- a/drivers/usb/host/xhci-hub.c
+++ b/drivers/usb/host/xhci-hub.c
@@ -489,7 +489,6 @@ EXPORT_SYMBOL_GPL(xhci_find_slot_id_by_port);
static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
{
struct xhci_virt_device *virt_dev;
- struct xhci_command *cmd;
unsigned long flags;
int ret;
int i;
@@ -501,10 +500,6 @@ static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)

trace_xhci_stop_device(virt_dev);

- cmd = xhci_alloc_command(xhci, true, GFP_NOIO);
- if (!cmd)
- return -ENOMEM;
-
spin_lock_irqsave(&xhci->lock, flags);
for (i = LAST_EP_INDEX; i > 0; i--) {
if (virt_dev->eps[i].ring && virt_dev->eps[i].ring->dequeue) {
@@ -521,7 +516,7 @@ static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
if (!command) {
spin_unlock_irqrestore(&xhci->lock, flags);
ret = -ENOMEM;
- goto cmd_cleanup;
+ goto out;
}

ret = xhci_queue_stop_endpoint(xhci, command, slot_id,
@@ -529,30 +524,14 @@ static int xhci_stop_device(struct xhci_hcd *xhci, int slot_id, int suspend)
if (ret) {
spin_unlock_irqrestore(&xhci->lock, flags);
xhci_free_command(xhci, command);
- goto cmd_cleanup;
+ goto out;
}
}
}
- ret = xhci_queue_stop_endpoint(xhci, cmd, slot_id, 0, suspend);
- if (ret) {
- spin_unlock_irqrestore(&xhci->lock, flags);
- goto cmd_cleanup;
- }
-
- xhci_ring_cmd_db(xhci);
spin_unlock_irqrestore(&xhci->lock, flags);
+ ret = xhci_stop_endpoint_sync(xhci, &virt_dev->eps[0], suspend);

- /* Wait for last stop endpoint command to finish */
- wait_for_completion(cmd->completion);
-
- if (cmd->status == COMP_COMMAND_ABORTED ||
- cmd->status == COMP_COMMAND_RING_STOPPED) {
- xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
- ret = -ETIME;
- }
-
-cmd_cleanup:
- xhci_free_command(xhci, cmd);
+out:
return ret;
}

diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 58d75b412bad..21413721f278 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -2764,6 +2764,46 @@ static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
return -ENOMEM;
}

+/*
+ * Synchronous XHCI stop endpoint helper. Issues the stop endpoint command and
+ * waits for the command completion before returning.
+ */
+int xhci_stop_endpoint_sync(struct xhci_hcd *xhci, struct xhci_virt_ep *ep, int suspend)
+{
+ struct xhci_command *command;
+ unsigned long flags;
+ int ret;
+
+ command = xhci_alloc_command(xhci, true, GFP_KERNEL);
+ if (!command)
+ return -ENOMEM;
+
+ spin_lock_irqsave(&xhci->lock, flags);
+ ret = xhci_queue_stop_endpoint(xhci, command, ep->vdev->slot_id,
+ ep->ep_index, suspend);
+ if (ret < 0) {
+ spin_unlock_irqrestore(&xhci->lock, flags);
+ goto out;
+ }
+
+ xhci_ring_cmd_db(xhci);
+ spin_unlock_irqrestore(&xhci->lock, flags);
+
+ ret = wait_for_completion_timeout(command->completion, msecs_to_jiffies(3000));
+ if (!ret)
+ xhci_warn(xhci, "%s: Unable to stop endpoint.\n",
+ __func__);
+
+ if (command->status == COMP_COMMAND_ABORTED ||
+ command->status == COMP_COMMAND_RING_STOPPED) {
+ xhci_warn(xhci, "Timeout while waiting for stop endpoint command\n");
+ ret = -ETIME;
+ }
+out:
+ xhci_free_command(xhci, command);
+
+ return ret;
+}

/* Issue a configure endpoint command or evaluate context command
* and wait for it to finish.
@@ -3084,7 +3124,7 @@ static void xhci_endpoint_reset(struct usb_hcd *hcd,
struct xhci_virt_device *vdev;
struct xhci_virt_ep *ep;
struct xhci_input_control_ctx *ctrl_ctx;
- struct xhci_command *stop_cmd, *cfg_cmd;
+ struct xhci_command *cfg_cmd;
unsigned int ep_index;
unsigned long flags;
u32 ep_flag;
@@ -3124,10 +3164,6 @@ static void xhci_endpoint_reset(struct usb_hcd *hcd,
if (ep_flag == SLOT_FLAG || ep_flag == EP0_FLAG)
return;

- stop_cmd = xhci_alloc_command(xhci, true, GFP_NOWAIT);
- if (!stop_cmd)
- return;
-
cfg_cmd = xhci_alloc_command_with_ctx(xhci, true, GFP_NOWAIT);
if (!cfg_cmd)
goto cleanup;
@@ -3150,23 +3186,16 @@ static void xhci_endpoint_reset(struct usb_hcd *hcd,
goto cleanup;
}

- err = xhci_queue_stop_endpoint(xhci, stop_cmd, udev->slot_id,
- ep_index, 0);
+ spin_unlock_irqrestore(&xhci->lock, flags);
+
+ err = xhci_stop_endpoint_sync(xhci, ep, 0);
if (err < 0) {
- spin_unlock_irqrestore(&xhci->lock, flags);
- xhci_free_command(xhci, cfg_cmd);
xhci_dbg(xhci, "%s: Failed to queue stop ep command, %d ",
__func__, err);
goto cleanup;
}

- xhci_ring_cmd_db(xhci);
- spin_unlock_irqrestore(&xhci->lock, flags);
-
- wait_for_completion(stop_cmd->completion);
-
spin_lock_irqsave(&xhci->lock, flags);
-
/* config ep command clears toggle if add and drop ep flags are set */
ctrl_ctx = xhci_get_input_control_ctx(cfg_cmd->in_ctx);
if (!ctrl_ctx) {
@@ -3198,7 +3227,6 @@ static void xhci_endpoint_reset(struct usb_hcd *hcd,

xhci_free_command(xhci, cfg_cmd);
cleanup:
- xhci_free_command(xhci, stop_cmd);
spin_lock_irqsave(&xhci->lock, flags);
if (ep->ep_state & EP_SOFT_CLEAR_TOGGLE)
ep->ep_state &= ~EP_SOFT_CLEAR_TOGGLE;
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index d706a27ec0a3..898b6434dc27 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -2125,6 +2125,8 @@ void xhci_ring_doorbell_for_active_rings(struct xhci_hcd *xhci,
void xhci_cleanup_command_queue(struct xhci_hcd *xhci);
void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring);
unsigned int count_trbs(u64 addr, u64 len);
+int xhci_stop_endpoint_sync(struct xhci_hcd *xhci, struct xhci_virt_ep *ep,
+ int suspend);

/* xHCI roothub code */
void xhci_set_link_state(struct xhci_hcd *xhci, struct xhci_port *port,

2023-07-25 02:59:41

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 27/32] sound: soc: qdsp6: Add SND kcontrol to select offload device

Expose a kcontrol on the platform sound card, which will allow for
userspace 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 'Q6USB offload SND device select' 1 0

The above will set the Q6AFE device token to choose offload on card#1 and
pcm#0. Device selection is made possible by setting the Q6AFE device
token. The audio DSP utilizes this parameter, and will pass this field
back to the USB offload driver within the QMI stream requests.

Signed-off-by: Wesley Cheng <[email protected]>
---
sound/soc/qcom/qdsp6/q6usb.c | 126 ++++++++++++++++++++++++++++++++++-
1 file changed, 123 insertions(+), 3 deletions(-)

diff --git a/sound/soc/qcom/qdsp6/q6usb.c b/sound/soc/qcom/qdsp6/q6usb.c
index 2fb6159bd2a8..87e88dafd8ad 100644
--- a/sound/soc/qcom/qdsp6/q6usb.c
+++ b/sound/soc/qcom/qdsp6/q6usb.c
@@ -14,6 +14,7 @@
#include <linux/dma-map-ops.h>

#include <sound/pcm.h>
+#include <sound/control.h>
#include <sound/soc.h>
#include <sound/soc-usb.h>
#include <sound/pcm_params.h>
@@ -35,9 +36,12 @@ struct q6usb_port_data {
struct q6afe_usb_cfg usb_cfg;
struct snd_soc_usb *usb;
struct q6usb_offload priv;
+ struct mutex mutex;
unsigned long available_card_slot;
struct q6usb_status status[SNDRV_CARDS];
- int active_idx;
+ bool idx_valid;
+ int sel_card_idx;
+ int sel_pcm_idx;
};

static const struct snd_soc_dapm_widget q6usb_dai_widgets[] = {
@@ -53,10 +57,36 @@ static int q6usb_hw_params(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
struct q6usb_port_data *data = dev_get_drvdata(dai->dev);
+ struct snd_soc_pcm_runtime *rtd = substream->private_data;
+ struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
+ struct q6afe_port *q6usb_afe;
int direction = substream->stream;
+ int chip_idx;
+ int ret;
+
+ mutex_lock(&data->mutex);
+ chip_idx = data->status[data->sel_card_idx].chip_index;
+
+ ret = snd_soc_usb_find_format(chip_idx, params, direction);
+ if (ret < 0)
+ goto out;
+
+ q6usb_afe = q6afe_port_get_from_id(cpu_dai->dev, USB_RX);
+ if (IS_ERR(q6usb_afe))
+ goto out;
+
+ ret = afe_port_send_usb_dev_param(q6usb_afe, data->sel_card_idx,
+ data->sel_pcm_idx);
+ if (ret < 0)
+ goto out;
+
+ data->status[data->sel_card_idx].pcm_index = data->sel_pcm_idx;
+out:
+ mutex_unlock(&data->mutex);

- return snd_soc_usb_find_format(data->active_idx, params, direction);
+ return ret;
}
+
static const struct snd_soc_dai_ops q6usb_ops = {
.hw_params = q6usb_hw_params,
};
@@ -85,6 +115,89 @@ static struct snd_soc_dai_driver q6usb_be_dais[] = {
},
};

+static int q6usb_get_offload_dev(struct snd_kcontrol *kcontrol,
+ struct snd_ctl_elem_value *ucontrol)
+{
+ struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
+ struct q6usb_port_data *data = dev_get_drvdata(component->dev);
+ int pcm_idx;
+ int card_idx;
+
+ mutex_lock(&data->mutex);
+ if (!data->idx_valid) {
+ card_idx = -1;
+ pcm_idx = -1;
+ } else {
+ card_idx = data->sel_card_idx;
+ pcm_idx = data->sel_pcm_idx;
+ }
+
+ ucontrol->value.integer.value[0] = card_idx;
+ ucontrol->value.integer.value[1] = pcm_idx;
+ mutex_unlock(&data->mutex);
+
+ return 0;
+}
+
+static int q6usb_put_offload_dev(struct snd_kcontrol *kcontrol,
+ struct snd_ctl_elem_value *ucontrol)
+{
+ struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
+ struct q6usb_port_data *data = dev_get_drvdata(component->dev);
+ int changed = 0;
+ int pcmidx;
+ int cardidx;
+
+ cardidx = ucontrol->value.integer.value[0];
+ pcmidx = ucontrol->value.integer.value[1];
+
+ mutex_lock(&data->mutex);
+ if ((cardidx >= 0 && test_bit(cardidx, &data->available_card_slot))) {
+ data->sel_card_idx = cardidx;
+ changed = 1;
+ }
+
+ if ((pcmidx >= 0 && pcmidx < data->status[cardidx].num_pcm)) {
+ data->sel_pcm_idx = pcmidx;
+ data->idx_valid = true;
+ changed = 1;
+ }
+ mutex_unlock(&data->mutex);
+
+ return changed;
+}
+
+static int q6usb_offload_dev_info(struct snd_kcontrol *kcontrol,
+ struct snd_ctl_elem_info *uinfo)
+{
+ uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
+ uinfo->count = 2;
+ uinfo->value.integer.min = -1;
+ uinfo->value.integer.max = SNDRV_CARDS;
+
+ return 0;
+}
+
+static const struct snd_kcontrol_new q6usb_offload_dev_ctrl = {
+ .iface = SNDRV_CTL_ELEM_IFACE_CARD,
+ .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
+ .name = "Q6USB offload SND device select",
+ .info = q6usb_offload_dev_info,
+ .get = q6usb_get_offload_dev,
+ .put = q6usb_put_offload_dev,
+};
+
+/* Build a mixer control for a UAC connector control (jack-detect) */
+static void q6usb_connector_control_init(struct snd_soc_component *component)
+{
+ int ret;
+
+ ret = snd_ctl_add(component->card->snd_card,
+ snd_ctl_new1(&q6usb_offload_dev_ctrl, component));
+ if (ret < 0)
+ return;
+}
+
static int q6usb_audio_ports_of_xlate_dai_name(struct snd_soc_component *component,
const struct of_phandle_args *args,
const char **dai_name)
@@ -113,10 +226,12 @@ static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
dapm = snd_soc_component_get_dapm(usb->component);
data = dev_get_drvdata(usb->component->dev);

+ mutex_lock(&data->mutex);
if (connected) {
snd_soc_dapm_enable_pin(dapm, "USB_RX_BE");
/* We only track the latest USB headset plugged in */
- data->active_idx = card_idx;
+ if (!data->idx_valid || data->sel_card_idx < 0)
+ data->sel_card_idx = card_idx;

set_bit(card_idx, &data->available_card_slot);
data->status[card_idx].num_pcm = num_pcm;
@@ -129,6 +244,7 @@ static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
}
snd_soc_dapm_sync(dapm);
+ mutex_unlock(&data->mutex);

return 0;
}
@@ -141,6 +257,8 @@ static int q6usb_component_probe(struct snd_soc_component *component)
snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
snd_soc_dapm_sync(dapm);

+ q6usb_connector_control_init(component);
+
data->usb = snd_soc_usb_add_port(component->dev, &data->priv, q6usb_alsa_connection_cb);
if (IS_ERR(data->usb)) {
dev_err(component->dev, "failed to add usb port\n");
@@ -189,6 +307,8 @@ static int q6usb_dai_dev_probe(struct platform_device *pdev)

data->priv.domain = iommu_get_domain_for_dev(&pdev->dev);

+ mutex_init(&data->mutex);
+
data->priv.dev = dev;
dev_set_drvdata(dev, data);


2023-07-25 03:01:15

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 12/32] sound: usb: Export USB SND APIs for modules

Some vendor modules will utilize useful parsing and endpoint management
APIs to start audio playback/capture.

Signed-off-by: Wesley Cheng <[email protected]>
---
sound/core/oss/pcm_oss.c | 4 ++--
sound/usb/card.c | 2 ++
sound/usb/endpoint.c | 2 ++
sound/usb/helper.c | 1 +
sound/usb/pcm.c | 9 ++++++---
sound/usb/pcm.h | 12 ++++++++++++
6 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/sound/core/oss/pcm_oss.c b/sound/core/oss/pcm_oss.c
index 728c211142d1..7773b5362e1a 100644
--- a/sound/core/oss/pcm_oss.c
+++ b/sound/core/oss/pcm_oss.c
@@ -444,8 +444,8 @@ static int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm,
}

static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
- snd_pcm_hw_param_t var, unsigned int val,
- int dir)
+ snd_pcm_hw_param_t var, unsigned int val,
+ int dir)
{
int changed;
if (hw_is_mask(var)) {
diff --git a/sound/usb/card.c b/sound/usb/card.c
index 9365d1e17836..a3fad66a3337 100644
--- a/sound/usb/card.c
+++ b/sound/usb/card.c
@@ -1076,6 +1076,7 @@ int snd_usb_autoresume(struct snd_usb_audio *chip)
}
return 0;
}
+EXPORT_SYMBOL_GPL(snd_usb_autoresume);

void snd_usb_autosuspend(struct snd_usb_audio *chip)
{
@@ -1089,6 +1090,7 @@ void snd_usb_autosuspend(struct snd_usb_audio *chip)
for (i = 0; i < chip->num_interfaces; i++)
usb_autopm_put_interface(chip->intf[i]);
}
+EXPORT_SYMBOL_GPL(snd_usb_autosuspend);

static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
{
diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c
index a385e85c4650..0757499599c8 100644
--- a/sound/usb/endpoint.c
+++ b/sound/usb/endpoint.c
@@ -866,6 +866,7 @@ snd_usb_endpoint_open(struct snd_usb_audio *chip,
mutex_unlock(&chip->mutex);
return ep;
}
+EXPORT_SYMBOL_GPL(snd_usb_endpoint_open);

/*
* snd_usb_endpoint_set_sync: Link data and sync endpoints
@@ -1503,6 +1504,7 @@ int snd_usb_endpoint_prepare(struct snd_usb_audio *chip,
mutex_unlock(&chip->mutex);
return err;
}
+EXPORT_SYMBOL_GPL(snd_usb_endpoint_prepare);

/* get the current rate set to the given clock by any endpoint */
int snd_usb_endpoint_get_clock_rate(struct snd_usb_audio *chip, int clock)
diff --git a/sound/usb/helper.c b/sound/usb/helper.c
index bf80e55d013a..4322ae3738e6 100644
--- a/sound/usb/helper.c
+++ b/sound/usb/helper.c
@@ -62,6 +62,7 @@ void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype
}
return NULL;
}
+EXPORT_SYMBOL_GPL(snd_usb_find_csint_desc);

/*
* Wrapper for usb_control_msg().
diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c
index eec5232f9fb2..b6fb6d84d9eb 100644
--- a/sound/usb/pcm.c
+++ b/sound/usb/pcm.c
@@ -87,7 +87,7 @@ static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream
/*
* find a matching audio format
*/
-static const struct audioformat *
+const struct audioformat *
find_format(struct list_head *fmt_list_head, snd_pcm_format_t format,
unsigned int rate, unsigned int channels, bool strict_match,
struct snd_usb_substream *subs)
@@ -147,8 +147,9 @@ find_format(struct list_head *fmt_list_head, snd_pcm_format_t format,
}
return found;
}
+EXPORT_SYMBOL_GPL(find_format);

-static const struct audioformat *
+const struct audioformat *
find_substream_format(struct snd_usb_substream *subs,
const struct snd_pcm_hw_params *params)
{
@@ -156,6 +157,7 @@ find_substream_format(struct snd_usb_substream *subs,
params_rate(params), params_channels(params),
true, subs);
}
+EXPORT_SYMBOL_GPL(find_substream_format);

bool snd_usb_pcm_has_fixed_rate(struct snd_usb_substream *subs)
{
@@ -446,7 +448,7 @@ int snd_usb_pcm_resume(struct snd_usb_stream *as)
return 0;
}

-static void close_endpoints(struct snd_usb_audio *chip,
+void close_endpoints(struct snd_usb_audio *chip,
struct snd_usb_substream *subs)
{
if (subs->data_endpoint) {
@@ -460,6 +462,7 @@ static void close_endpoints(struct snd_usb_audio *chip,
subs->sync_endpoint = NULL;
}
}
+EXPORT_SYMBOL(close_endpoints);

/*
* hw_params callback
diff --git a/sound/usb/pcm.h b/sound/usb/pcm.h
index 388fe2ba346d..b5216cb19e75 100644
--- a/sound/usb/pcm.h
+++ b/sound/usb/pcm.h
@@ -15,4 +15,16 @@ void snd_usb_preallocate_buffer(struct snd_usb_substream *subs);
int snd_usb_audioformat_set_sync_ep(struct snd_usb_audio *chip,
struct audioformat *fmt);

+void close_endpoints(struct snd_usb_audio *chip,
+ struct snd_usb_substream *subs);
+int configure_endpoints(struct snd_usb_audio *chip,
+ struct snd_usb_substream *subs);
+
+const struct audioformat *
+find_format(struct list_head *fmt_list_head, snd_pcm_format_t format,
+ unsigned int rate, unsigned int channels, bool strict_match,
+ struct snd_usb_substream *subs);
+const struct audioformat *
+find_substream_format(struct snd_usb_substream *subs,
+ const struct snd_pcm_hw_params *params);
#endif /* __USBAUDIO_PCM_H */

2023-07-25 03:02:44

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 13/32] dt-bindings: usb: dwc3: Add snps,num-hc-interrupters definition

Add a new definition for specifying how many XHCI secondary interrupters
can be allocated. XHCI in general can potentially support up to 1024
interrupters, which some uses may want to limit depending on how many
users utilize the interrupters.

Signed-off-by: Wesley Cheng <[email protected]>
---
.../devicetree/bindings/usb/snps,dwc3.yaml | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/Documentation/devicetree/bindings/usb/snps,dwc3.yaml b/Documentation/devicetree/bindings/usb/snps,dwc3.yaml
index 50edc4da780e..cc6012e922e0 100644
--- a/Documentation/devicetree/bindings/usb/snps,dwc3.yaml
+++ b/Documentation/devicetree/bindings/usb/snps,dwc3.yaml
@@ -376,6 +376,19 @@ properties:
items:
enum: [1, 4, 8, 16, 32, 64, 128, 256]

+ snps,num-hc-interrupters:
+ description:
+ Defines the maximum number of XHCI host controller interrupters that can
+ be supported. The XHCI host controller has support to allocate multiple
+ event rings, which can be assigned to different clients/users. The DWC3
+ controller has a maximum of 8 interrupters. If this is not defined then
+ the value will be defaulted to 1. This parameter is used only when
+ operating in host mode.
+ $ref: /schemas/types.yaml#/definitions/uint8
+ minimum: 1
+ maximum: 8
+ default: 1
+
port:
$ref: /schemas/graph.yaml#/properties/port
description:

2023-07-25 03:02:59

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 03/32] xhci: sideband: add initial api to register a sideband entity

From: Mathias Nyman <[email protected]>

Introduce XHCI sideband, which manages the USB endpoints being requested by
a client driver. This is used for when client drivers are attempting to
offload USB endpoints to another entity for handling USB transfers. XHCI
sideband will allow for drivers to fetch the required information about the
transfer ring, so the user can submit transfers independently. Expose the
required APIs for drivers to register and request for a USB endpoint.

Multiple ring segment page linking and proper endpoint clean up added by
Wesley Cheng to complete original concept code by Mathias Nyman.

Signed-off-by: Mathias Nyman <[email protected]>
Co-developed-by: Wesley Cheng <[email protected]>
Signed-off-by: Wesley Cheng <[email protected]>
---
drivers/usb/host/Kconfig | 9 ++
drivers/usb/host/Makefile | 4 +
drivers/usb/host/xhci-sideband.c | 195 ++++++++++++++++++++++++++++++
drivers/usb/host/xhci.h | 23 +---
include/linux/usb/xhci-sideband.h | 90 ++++++++++++++
5 files changed, 303 insertions(+), 18 deletions(-)
create mode 100644 drivers/usb/host/xhci-sideband.c
create mode 100644 include/linux/usb/xhci-sideband.h

diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
index c170672f847e..d9dc92bea525 100644
--- a/drivers/usb/host/Kconfig
+++ b/drivers/usb/host/Kconfig
@@ -104,6 +104,15 @@ config USB_XHCI_RZV2M
Say 'Y' to enable the support for the xHCI host controller
found in Renesas RZ/V2M SoC.

+config USB_XHCI_SIDEBAND
+ bool "xHCI support for sideband"
+ help
+ Say 'Y' to enable the support for the xHCI sideband capability.
+ provide a mechanism for a sideband datapath for payload associated
+ with audio class endpoints. This allows for an audio DSP to use
+ xHCI USB endpoints directly, allowing CPU to sleep while playing
+ audio
+
config USB_XHCI_TEGRA
tristate "xHCI support for NVIDIA Tegra SoCs"
depends on PHY_TEGRA_XUSB
diff --git a/drivers/usb/host/Makefile b/drivers/usb/host/Makefile
index be4e5245c52f..4df946c05ba0 100644
--- a/drivers/usb/host/Makefile
+++ b/drivers/usb/host/Makefile
@@ -32,6 +32,10 @@ endif
xhci-rcar-hcd-y += xhci-rcar.o
xhci-rcar-hcd-$(CONFIG_USB_XHCI_RZV2M) += xhci-rzv2m.o

+ifneq ($(CONFIG_USB_XHCI_SIDEBAND),)
+ xhci-hcd-y += xhci-sideband.o
+endif
+
obj-$(CONFIG_USB_PCI) += pci-quirks.o

obj-$(CONFIG_USB_EHCI_HCD) += ehci-hcd.o
diff --git a/drivers/usb/host/xhci-sideband.c b/drivers/usb/host/xhci-sideband.c
new file mode 100644
index 000000000000..a077eb8783cc
--- /dev/null
+++ b/drivers/usb/host/xhci-sideband.c
@@ -0,0 +1,195 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * xHCI host controller sideband support
+ *
+ * Copyright (c) 2023, Intel Corporation.
+ *
+ * Author: Mathias Nyman
+ */
+#include <linux/usb.h>
+#include <linux/dma-direct.h>
+
+#include "xhci.h"
+
+static int
+xhci_ring_to_sgtable(struct xhci_sideband *sb, struct xhci_ring *ring, struct device *dev)
+{
+ struct sg_table *sgt;
+ struct xhci_segment *seg;
+ struct page **pages;
+ unsigned int n_pages;
+ size_t sz;
+ int i;
+
+ sz = ring->num_segs * TRB_SEGMENT_SIZE;
+ n_pages = PAGE_ALIGN(sz) >> PAGE_SHIFT;
+ pages = kvmalloc_array(n_pages, sizeof(struct page *), GFP_KERNEL);
+ if (!pages)
+ return 0;
+
+ sgt = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
+ if (!sgt) {
+ kvfree(pages);
+ return 0;
+ }
+
+ seg = ring->first_seg;
+
+ /*
+ * Rings can potentially have multiple segments, create an array that
+ * carries page references to allocated segments. Utilize the
+ * sg_alloc_table_from_pages() to create the sg table, and to ensure
+ * that page links are created.
+ */
+ for (i = 0; i < ring->num_segs; i++) {
+ pages[i] = vmalloc_to_page(seg->trbs);
+ seg = seg->next;
+ }
+
+ if (sg_alloc_table_from_pages(sgt, pages, n_pages, 0, sz, GFP_KERNEL)) {
+ kvfree(pages);
+ kfree(sgt);
+
+ return 0;
+ }
+
+ sb->sgt = sgt;
+
+ return 0;
+}
+
+static int __xhci_sideband_remove_endpoint(struct xhci_sideband *sb,
+ int idx)
+{
+ struct xhci_virt_ep *ep = sb->eps[idx];
+
+ /*
+ * Issue a stop endpoint command when an endpoint is removed.
+ * The stop ep cmd handler will handle the ring cleanup.
+ */
+ xhci_stop_endpoint_sync(sb->xhci, ep, 0);
+
+ sg_free_table(sb->sgt);
+ ep->sideband = NULL;
+ sb->eps[idx] = NULL;
+
+ return 0;
+}
+
+/* sidband api functions */
+struct xhci_sideband *
+xhci_sideband_register(struct usb_device *udev)
+{
+ struct usb_hcd *hcd = bus_to_hcd(udev->bus);
+ struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+ struct xhci_virt_device *vdev;
+ struct xhci_sideband *sb;
+
+ /* make sure the usb device is connected to a xhci controller */
+ if (!udev->slot_id)
+ return NULL;
+
+ /* check this device isn't already controller via sideband */
+ vdev = xhci->devs[udev->slot_id];
+
+ if (!vdev || vdev->sideband) {
+ xhci_warn(xhci, "XHCI sideband slot already in use %d\n",
+ udev->slot_id);
+ return NULL;
+ }
+
+ sb = kzalloc_node(sizeof(*sb), GFP_KERNEL, dev_to_node(hcd->self.sysdev));
+ if (!sb)
+ return NULL;
+
+ sb->xhci = xhci;
+ sb->vdev = vdev;
+ vdev->sideband = sb;
+
+ return sb;
+}
+EXPORT_SYMBOL_GPL(xhci_sideband_register);
+
+void
+xhci_sideband_unregister(struct xhci_sideband *sb)
+{
+ int i;
+
+ for (i = 0; i < EP_CTX_PER_DEV; i++)
+ if (sb->eps[i])
+ __xhci_sideband_remove_endpoint(sb, i);
+
+ sb->vdev->sideband = NULL;
+ kfree(sb);
+}
+EXPORT_SYMBOL_GPL(xhci_sideband_unregister);
+
+int
+xhci_sideband_add_endpoint(struct xhci_sideband *sb,
+ struct usb_host_endpoint *host_ep)
+{
+ struct xhci_hcd *xhci = sb->xhci;
+ struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
+ struct xhci_virt_ep *ep;
+ unsigned int ep_index;
+
+ ep_index = xhci_get_endpoint_index(&host_ep->desc);
+ ep = &sb->vdev->eps[ep_index];
+
+ /*
+ * Note, we don't know the DMA mask of the audio DSP device, if its
+ * smaller than for xhci it won't be able to access the endpoint ring
+ * buffer. This could be solved by not allowing the audio class driver
+ * to add the endpoint the normal way, but instead offload it immediately,
+ * and let this function add the endpoint and allocate the ring buffer
+ * with the smallest common DMA mask
+ */
+ if (sb->eps[ep_index] || ep->sideband)
+ return -EBUSY;
+
+ ep->sideband = sb;
+ sb->eps[ep_index] = ep;
+ sb->ring = ep->ring;
+ xhci_ring_to_sgtable(sb, ep->ring, dev);
+
+ return 0;
+
+}
+EXPORT_SYMBOL_GPL(xhci_sideband_add_endpoint);
+
+int
+xhci_sideband_remove_endpoint(struct xhci_sideband *sb,
+ struct usb_host_endpoint *host_ep)
+{
+ struct xhci_virt_ep *ep;
+ unsigned int ep_index;
+
+ ep_index = xhci_get_endpoint_index(&host_ep->desc);
+ ep = sb->eps[ep_index];
+
+ if (!ep || !ep->sideband || sb->vdev != ep->sideband->vdev)
+ return -ENODEV;
+
+ __xhci_sideband_remove_endpoint(sb, ep_index);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(xhci_sideband_remove_endpoint);
+
+int
+xhci_sideband_stop_endpoint(struct xhci_sideband *sb,
+ struct usb_host_endpoint *host_ep)
+{
+ struct xhci_virt_ep *ep;
+ unsigned int ep_index;
+
+ ep_index = xhci_get_endpoint_index(&host_ep->desc);
+ ep = sb->eps[ep_index];
+
+
+ if (!ep || ep->sideband != sb || sb->vdev != ep->sideband->vdev)
+ return -EINVAL;
+
+ return xhci_stop_endpoint_sync(sb->xhci, ep, 0);
+}
+EXPORT_SYMBOL_GPL(xhci_sideband_stop_endpoint);
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index 898b6434dc27..15ade8ec6b6c 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -18,6 +18,7 @@
#include <linux/usb/hcd.h>
#include <linux/io-64-nonatomic-lo-hi.h>
#include <linux/usb/xhci-intr.h>
+#include <linux/usb/xhci-sideband.h>

/* Code sharing between pci-quirks and xhci hcd */
#include "xhci-ext-caps.h"
@@ -975,6 +976,8 @@ struct xhci_virt_ep {
int next_frame_id;
/* Use new Isoch TRB layout needed for extended TBC support */
bool use_extended_tbc;
+ /* set if this endpoint is controlled via sideband access*/
+ struct xhci_sideband *sideband;
};

enum xhci_overhead_type {
@@ -1004,8 +1007,6 @@ struct xhci_interval_bw_table {
unsigned int ss_bw_out;
};

-#define EP_CTX_PER_DEV 31
-
struct xhci_virt_device {
int slot_id;
struct usb_device *udev;
@@ -1038,6 +1039,8 @@ struct xhci_virt_device {
u16 current_mel;
/* Used for the debugfs interfaces. */
void *debugfs_private;
+ /* set if this device is registered for sideband access */
+ struct xhci_sideband *sideband;
};

/*
@@ -1518,22 +1521,6 @@ static inline const char *xhci_trb_type_string(u8 type)
#define NEC_FW_MINOR(p) (((p) >> 0) & 0xff)
#define NEC_FW_MAJOR(p) (((p) >> 8) & 0xff)

-/*
- * TRBS_PER_SEGMENT must be a multiple of 4,
- * since the command ring is 64-byte aligned.
- * It must also be greater than 16.
- */
-#define TRBS_PER_SEGMENT 256
-/* Allow two commands + a link TRB, along with any reserved command TRBs */
-#define MAX_RSVD_CMD_TRBS (TRBS_PER_SEGMENT - 3)
-#define TRB_SEGMENT_SIZE (TRBS_PER_SEGMENT*16)
-#define TRB_SEGMENT_SHIFT (ilog2(TRB_SEGMENT_SIZE))
-/* TRB buffer pointers can't cross 64KB boundaries */
-#define TRB_MAX_BUFF_SHIFT 16
-#define TRB_MAX_BUFF_SIZE (1 << TRB_MAX_BUFF_SHIFT)
-/* How much data is left before the 64KB boundary? */
-#define TRB_BUFF_LEN_UP_TO_BOUNDARY(addr) (TRB_MAX_BUFF_SIZE - \
- (addr & (TRB_MAX_BUFF_SIZE - 1)))
#define MAX_SOFT_RETRY 3
/*
* Limits of consecutive isoc trbs that can Block Event Interrupt (BEI) if
diff --git a/include/linux/usb/xhci-sideband.h b/include/linux/usb/xhci-sideband.h
new file mode 100644
index 000000000000..1c8c103dc91c
--- /dev/null
+++ b/include/linux/usb/xhci-sideband.h
@@ -0,0 +1,90 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * xHCI host controller sideband support
+ *
+ * Copyright (c) 2023, Intel Corporation.
+ *
+ * Author: Mathias Nyman <[email protected]>
+ */
+
+#ifndef __LINUX_XHCI_SIDEBAND_H // fixme check ok
+#define __LINUX_XHCI_SIDEBAND_H
+
+#include <linux/dma-mapping.h>
+/*
+ * TRBS_PER_SEGMENT must be a multiple of 4,
+ * since the command ring is 64-byte aligned.
+ * It must also be greater than 16.
+ */
+#define TRBS_PER_SEGMENT 256
+/* Allow two commands + a link TRB, along with any reserved command TRBs */
+#define MAX_RSVD_CMD_TRBS (TRBS_PER_SEGMENT - 3)
+#define TRB_SEGMENT_SIZE (TRBS_PER_SEGMENT*16)
+#define TRB_SEGMENT_SHIFT (ilog2(TRB_SEGMENT_SIZE))
+/* TRB buffer pointers can't cross 64KB boundaries */
+#define TRB_MAX_BUFF_SHIFT 16
+#define TRB_MAX_BUFF_SIZE (1 << TRB_MAX_BUFF_SHIFT)
+/* How much data is left before the 64KB boundary? */
+#define TRB_BUFF_LEN_UP_TO_BOUNDARY(addr) (TRB_MAX_BUFF_SIZE - \
+ (addr & (TRB_MAX_BUFF_SIZE - 1)))
+
+#define EP_CTX_PER_DEV 31
+
+struct xhci_sideband {
+ struct xhci_hcd *xhci;
+ struct xhci_virt_device *vdev;
+ struct xhci_virt_ep *eps[EP_CTX_PER_DEV];
+ struct xhci_interrupter *ir;
+ struct sg_table *sgt;
+ struct xhci_ring *ring;
+ dma_addr_t dma;
+};
+
+#if IS_ENABLED(CONFIG_USB_XHCI_SIDEBAND)
+struct xhci_sideband *
+xhci_sideband_register(struct usb_device *udev);
+void
+xhci_sideband_unregister(struct xhci_sideband *sb);
+int
+xhci_sideband_add_endpoint(struct xhci_sideband *sb,
+ struct usb_host_endpoint *host_ep);
+int
+xhci_sideband_remove_endpoint(struct xhci_sideband *sb,
+ struct usb_host_endpoint *host_ep);
+int
+xhci_sideband_stop_endpoint(struct xhci_sideband *sb,
+ struct usb_host_endpoint *host_ep);
+#else
+static struct xhci_sideband *
+xhci_sideband_register(struct usb_device *udev)
+{
+ return NULL;
+}
+
+static void
+xhci_sideband_unregister(struct xhci_sideband *sb)
+{ }
+
+static int
+xhci_sideband_add_endpoint(struct xhci_sideband *sb,
+ struct usb_host_endpoint *host_ep)
+{
+ return 0;
+}
+
+static int
+xhci_sideband_remove_endpoint(struct xhci_sideband *sb,
+ struct usb_host_endpoint *host_ep)
+{
+ return 0;
+}
+
+static int
+xhci_sideband_stop_endpoint(struct xhci_sideband *sb,
+ struct usb_host_endpoint *host_ep)
+{
+ return 0;
+}
+#endif
+#endif /* __LINUX_XHCI_SIDEBAND_H */
+

2023-07-25 03:04:47

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 09/32] ASoC: qdsp6: q6afe: Increase APR timeout

For USB offloading situations, the AFE port start command will result in a
QMI handshake between the Q6DSP and the main processor. Depending on if
the USB bus is suspended, this routine would require more time to complete,
as resuming the USB bus has some overhead associated with it. Increase the
timeout to 3s to allow for sufficient time for the USB QMI stream enable
handshake to complete.

Reviewed-by: Srinivas Kandagatla <[email protected]>
Signed-off-by: Wesley Cheng <[email protected]>
---
sound/soc/qcom/qdsp6/q6afe.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sound/soc/qcom/qdsp6/q6afe.c b/sound/soc/qcom/qdsp6/q6afe.c
index 43baf93f9c89..e9d5fa6b6b0d 100644
--- a/sound/soc/qcom/qdsp6/q6afe.c
+++ b/sound/soc/qcom/qdsp6/q6afe.c
@@ -365,7 +365,7 @@
#define AFE_API_VERSION_SLOT_MAPPING_CONFIG 1
#define AFE_API_VERSION_CODEC_DMA_CONFIG 1

-#define TIMEOUT_MS 1000
+#define TIMEOUT_MS 3000
#define AFE_CMD_RESP_AVAIL 0
#define AFE_CMD_RESP_NONE 1
#define AFE_CLK_TOKEN 1024

2023-07-25 03:05:47

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 06/32] ASoC: Add SOC USB APIs for adding an USB backend

Some platforms may have support for offloading USB audio devices to a
dedicated audio DSP. Introduce a set of APIs that allow for management of
USB sound card and PCM devices enumerated by the USB SND class driver.
This allows for the ASoC components to be aware of what USB devices are
available for offloading.

Signed-off-by: Wesley Cheng <[email protected]>
---
include/sound/soc-usb.h | 35 ++++++++
sound/soc/Makefile | 2 +-
sound/soc/soc-usb.c | 180 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 216 insertions(+), 1 deletion(-)
create mode 100644 include/sound/soc-usb.h
create mode 100644 sound/soc/soc-usb.c

diff --git a/include/sound/soc-usb.h b/include/sound/soc-usb.h
new file mode 100644
index 000000000000..378992ea07bd
--- /dev/null
+++ b/include/sound/soc-usb.h
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ */
+
+#ifndef __LINUX_SND_SOC_USB_H
+#define __LINUX_SND_SOC_USB_H
+
+/**
+ * struct snd_soc_usb
+ * @list - list head for SND SOC struct list
+ * @dev - USB backend device reference
+ * @component - reference to DAPM component
+ * @connection_status_cb - callback to notify connection events
+ * @priv_data - driver data
+ **/
+struct snd_soc_usb {
+ struct list_head list;
+ struct device *dev;
+ struct snd_soc_component *component;
+ int (*connection_status_cb)(struct snd_soc_usb *usb, int card_idx,
+ int connected);
+ void *priv_data;
+};
+
+int snd_soc_usb_connect(struct device *usbdev, int card_idx);
+int snd_soc_usb_disconnect(struct device *usbdev);
+void snd_soc_usb_set_priv_data(struct device *dev, void *priv);
+void *snd_soc_usb_get_priv_data(struct device *usbdev);
+
+struct snd_soc_usb *snd_soc_usb_add_port(struct device *dev, void *priv,
+ int (*connection_cb)(struct snd_soc_usb *usb, int card_idx,
+ int connected));
+int snd_soc_usb_remove_port(struct device *dev);
+#endif
diff --git a/sound/soc/Makefile b/sound/soc/Makefile
index 507eaed1d6a1..3305ceb59d84 100644
--- a/sound/soc/Makefile
+++ b/sound/soc/Makefile
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
-snd-soc-core-objs := soc-core.o soc-dapm.o soc-jack.o soc-utils.o soc-dai.o soc-component.o
+snd-soc-core-objs := soc-core.o soc-dapm.o soc-jack.o soc-usb.o soc-utils.o soc-dai.o soc-component.o
snd-soc-core-objs += soc-pcm.o soc-devres.o soc-ops.o soc-link.o soc-card.o
snd-soc-core-$(CONFIG_SND_SOC_COMPRESS) += soc-compress.o

diff --git a/sound/soc/soc-usb.c b/sound/soc/soc-usb.c
new file mode 100644
index 000000000000..4293451cdd49
--- /dev/null
+++ b/sound/soc/soc-usb.c
@@ -0,0 +1,180 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ */
+#include <linux/of.h>
+#include <linux/usb.h>
+#include <sound/soc.h>
+#include <sound/soc-usb.h>
+#include "../usb/card.h"
+
+static DEFINE_MUTEX(ctx_mutex);
+static LIST_HEAD(usb_ctx_list);
+
+static struct device_node *snd_soc_find_phandle(struct device *dev)
+{
+ struct device_node *node;
+
+ node = of_parse_phandle(dev->of_node, "usb-soc-be", 0);
+ if (!node)
+ return ERR_PTR(-ENODEV);
+
+ return node;
+}
+
+static struct snd_soc_usb *snd_soc_find_usb_ctx(struct device *dev)
+{
+ struct device_node *node;
+ struct snd_soc_usb *ctx = NULL;
+
+ node = snd_soc_find_phandle(dev);
+ if (IS_ERR(node))
+ return NULL;
+
+ mutex_lock(&ctx_mutex);
+ list_for_each_entry(ctx, &usb_ctx_list, list) {
+ if (ctx->dev->of_node == node) {
+ of_node_put(node);
+ mutex_unlock(&ctx_mutex);
+ return ctx;
+ }
+ }
+ of_node_put(node);
+ mutex_unlock(&ctx_mutex);
+
+ return NULL;
+}
+
+/**
+ * snd_soc_usb_get_priv_data() - Retrieve private data stored
+ * @dev: device reference
+ *
+ * Fetch the private data stored in the USB SND SOC structure.
+ *
+ */
+void *snd_soc_usb_get_priv_data(struct device *dev)
+{
+ struct snd_soc_usb *ctx;
+
+ ctx = snd_soc_find_usb_ctx(dev);
+ if (!ctx) {
+ /* Check if backend device */
+ list_for_each_entry(ctx, &usb_ctx_list, list) {
+ if (dev->of_node == ctx->dev->of_node)
+ goto out;
+ }
+ ctx = NULL;
+ }
+out:
+ return ctx ? ctx->priv_data : NULL;
+}
+EXPORT_SYMBOL_GPL(snd_soc_usb_get_priv_data);
+
+/**
+ * snd_soc_usb_add_port() - Add a USB backend port
+ * @dev: USB backend device
+ * @priv: private data
+ * @connection_cb: connection status callback
+ *
+ * Register a USB backend device to the SND USB SOC framework. Memory is
+ * allocated as part of the USB backend device.
+ *
+ */
+struct snd_soc_usb *snd_soc_usb_add_port(struct device *dev, void *priv,
+ int (*connection_cb)(struct snd_soc_usb *usb, int card_idx,
+ int connected))
+{
+ struct snd_soc_usb *usb;
+
+ usb = devm_kzalloc(dev, sizeof(*usb), GFP_KERNEL);
+ if (!usb)
+ return ERR_PTR(-ENOMEM);
+
+ usb->connection_status_cb = connection_cb;
+ usb->dev = dev;
+ usb->priv_data = priv;
+
+ mutex_lock(&ctx_mutex);
+ list_add_tail(&usb->list, &usb_ctx_list);
+ mutex_unlock(&ctx_mutex);
+
+ return usb;
+}
+EXPORT_SYMBOL_GPL(snd_soc_usb_add_port);
+
+/**
+ * snd_soc_usb_remove_port() - Remove a USB backend port
+ * @dev: USB backend device
+ *
+ * Remove a USB backend device from USB SND SOC. Memory is freed when USB
+ * backend is removed.
+ *
+ */
+int snd_soc_usb_remove_port(struct device *dev)
+{
+ struct snd_soc_usb *ctx, *tmp;
+
+ mutex_lock(&ctx_mutex);
+ list_for_each_entry_safe(ctx, tmp, &usb_ctx_list, list) {
+ if (ctx->dev == dev) {
+ list_del(&ctx->list);
+ break;
+ }
+ }
+ mutex_unlock(&ctx_mutex);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(snd_soc_usb_remove_port);
+
+/**
+ * snd_soc_usb_connect() - Notification of USB device connection
+ * @usbdev: USB bus device
+ * @card_idx: USB SND card instance
+ *
+ * Notify of a new USB SND device connection. The card_idx can be used to
+ * handle how the USB backend selects, which device to enable offloading on.
+ *
+ */
+int snd_soc_usb_connect(struct device *usbdev, int card_idx)
+{
+ struct snd_soc_usb *ctx;
+
+ if (!usbdev)
+ return -ENODEV;
+
+ ctx = snd_soc_find_usb_ctx(usbdev);
+ if (!ctx)
+ return -ENODEV;
+
+ if (ctx->connection_status_cb)
+ ctx->connection_status_cb(ctx, card_idx, 1);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(snd_soc_usb_connect);
+
+/**
+ * snd_soc_usb_disconnect() - Notification of USB device disconnection
+ * @usbdev: USB bus device
+ *
+ * Notify of a new USB SND device disconnection to the USB backend.
+ *
+ */
+int snd_soc_usb_disconnect(struct device *usbdev)
+{
+ struct snd_soc_usb *ctx;
+
+ if (!usbdev)
+ return -ENODEV;
+
+ ctx = snd_soc_find_usb_ctx(usbdev);
+ if (!ctx)
+ return -ENODEV;
+
+ if (ctx->connection_status_cb)
+ ctx->connection_status_cb(ctx, -1, 0);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(snd_soc_usb_disconnect);

2023-07-25 03:05:49

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 24/32] ASoC: dt-bindings: Update example for enabling USB offload on SM8250

Add an example on enabling of USB offload for the Q6DSP. The routing can
be done by the mixer, which can pass the multimedia stream to the USB
backend.

Signed-off-by: Wesley Cheng <[email protected]>
---
.../devicetree/bindings/sound/qcom,sm8250.yaml | 15 +++++++++++++++
1 file changed, 15 insertions(+)

diff --git a/Documentation/devicetree/bindings/sound/qcom,sm8250.yaml b/Documentation/devicetree/bindings/sound/qcom,sm8250.yaml
index 262de7a60a73..13145e010ddf 100644
--- a/Documentation/devicetree/bindings/sound/qcom,sm8250.yaml
+++ b/Documentation/devicetree/bindings/sound/qcom,sm8250.yaml
@@ -220,6 +220,21 @@ examples:
sound-dai = <&vamacro 0>;
};
};
+
+ usb-dai-link {
+ link-name = "USB Playback";
+ cpu {
+ sound-dai = <&q6afedai USB_RX>;
+ };
+
+ codec {
+ sound-dai = <&usbdai USB_RX>;
+ };
+
+ platform {
+ sound-dai = <&q6routing>;
+ };
+ };
};

- |

2023-07-25 03:05:54

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 31/32] sound: usb: card: Allow for rediscovery of connected USB SND devices

In case of notifying SND platform drivers of connection events, some of
these use cases, such as offloading, require an ASoC USB backend device to
be initialized before the events can be handled. If the USB backend device
has not yet been probed, this leads to missing initial USB audio device
connection events.

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

Signed-off-by: Wesley Cheng <[email protected]>
---
sound/usb/card.c | 19 +++++++++++++++++++
sound/usb/card.h | 2 ++
2 files changed, 21 insertions(+)

diff --git a/sound/usb/card.c b/sound/usb/card.c
index 365f6d978608..27a89aaa0bf3 100644
--- a/sound/usb/card.c
+++ b/sound/usb/card.c
@@ -170,6 +170,25 @@ struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
}
EXPORT_SYMBOL_GPL(snd_usb_find_suppported_substream);

+/*
+ * in case the platform driver was not ready at the time of USB SND
+ * device connect, expose an API to discover all connected USB devices
+ * so it can populate any dependent resources/structures.
+ */
+void snd_usb_rediscover_devices(void)
+{
+ int i;
+
+ mutex_lock(&register_mutex);
+ for (i = 0; i < SNDRV_CARDS; i++) {
+ if (usb_chip[i])
+ if (platform_ops && platform_ops->connect_cb)
+ platform_ops->connect_cb(usb_chip[i]);
+ }
+ mutex_unlock(&register_mutex);
+}
+EXPORT_SYMBOL_GPL(snd_usb_rediscover_devices);
+
/*
* disconnect streams
* called from usb_audio_disconnect()
diff --git a/sound/usb/card.h b/sound/usb/card.h
index db735680d155..dfd241334e3d 100644
--- a/sound/usb/card.h
+++ b/sound/usb/card.h
@@ -219,6 +219,7 @@ int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops);
int snd_usb_unregister_platform_ops(void);
struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
struct snd_pcm_hw_params *params, int direction);
+void snd_usb_rediscover_devices(void);
#else
static int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops)
{
@@ -235,5 +236,6 @@ static struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
{
return NULL;
}
+void snd_usb_rediscover_devices(void) { }
#endif /* IS_ENABLED(CONFIG_SND_USB_AUDIO) */
#endif /* __USBAUDIO_CARD_H */

2023-07-25 03:06:17

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 29/32] sound: soc: qcom: q6usb: Add headphone jack for offload connection status

The headphone jack framework has a well defined intrastructure for
notifying userspace entities through input devices. Expose a jack device
that carries information about if an offload capable device is connected.
Applications can further identify specific offloading information through
other SND kcontrols.

Signed-off-by: Wesley Cheng <[email protected]>
---
sound/soc/qcom/qdsp6/q6usb.c | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/sound/soc/qcom/qdsp6/q6usb.c b/sound/soc/qcom/qdsp6/q6usb.c
index e4ccb9d912b0..860dff8c1438 100644
--- a/sound/soc/qcom/qdsp6/q6usb.c
+++ b/sound/soc/qcom/qdsp6/q6usb.c
@@ -20,6 +20,7 @@
#include <sound/pcm_params.h>
#include <sound/asound.h>
#include <sound/q6usboffload.h>
+#include <sound/jack.h>

#include "q6dsp-lpass-ports.h"
#include "q6afe.h"
@@ -37,6 +38,7 @@ struct q6usb_status {
struct q6usb_port_data {
struct q6afe_usb_cfg usb_cfg;
struct snd_soc_usb *usb;
+ struct snd_soc_jack hs_jack;
struct q6usb_offload priv;
struct mutex mutex;
unsigned long available_card_slot;
@@ -279,6 +281,7 @@ static const struct snd_kcontrol_new q6usb_offload_control = {
/* Build a mixer control for a UAC connector control (jack-detect) */
static void q6usb_connector_control_init(struct snd_soc_component *component)
{
+ struct q6usb_port_data *data = dev_get_drvdata(component->dev);
int ret;

ret = snd_ctl_add(component->card->snd_card,
@@ -290,6 +293,11 @@ static void q6usb_connector_control_init(struct snd_soc_component *component)
snd_ctl_new1(&q6usb_offload_dev_ctrl, component));
if (ret < 0)
return;
+
+ ret = snd_soc_card_jack_new(component->card, "USB offload",
+ SND_JACK_HEADSET, &data->hs_jack);
+ if (ret)
+ return;
}

static int q6usb_audio_ports_of_xlate_dai_name(struct snd_soc_component *component,
@@ -322,7 +330,10 @@ static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,

mutex_lock(&data->mutex);
if (connected) {
- snd_soc_dapm_enable_pin(dapm, "USB_RX_BE");
+ if (!data->available_card_slot) {
+ snd_soc_dapm_enable_pin(dapm, "USB_RX_BE");
+ snd_jack_report(data->hs_jack.jack, 1);
+ }
/*
* Update the latest USB headset plugged in, if session is
* idle.
@@ -338,8 +349,10 @@ static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
clear_bit(card_idx, &data->available_card_slot);
data->status[card_idx].num_pcm = 0;
data->status[card_idx].chip_index = 0;
- if (!data->available_card_slot)
+ if (!data->available_card_slot) {
snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
+ snd_jack_report(data->hs_jack.jack, 0);
+ }
}
snd_soc_dapm_sync(dapm);
mutex_unlock(&data->mutex);

2023-07-25 03:06:29

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 26/32] sound: Pass USB SND card and PCM information to SOC USB

Currently, only the index to the USB SND card array is passed to the USB
backend. Pass through more information, specifically the USB SND card
number and the number of PCM devices available. The USB backend should
know about which sound resources are being shared between the ASoC and USB
SND paths. This can be utilized to properly select and maintain the
offloading devices.

Signed-off-by: Wesley Cheng <[email protected]>
---
include/sound/soc-usb.h | 9 +++++----
sound/soc/qcom/qdsp6/q6usb.c | 20 ++++++++++++++++++--
sound/soc/soc-usb.c | 12 +++++++-----
sound/usb/qcom/qc_audio_offload.c | 9 +++++----
4 files changed, 35 insertions(+), 15 deletions(-)

diff --git a/include/sound/soc-usb.h b/include/sound/soc-usb.h
index 71e6e75e600a..606128332044 100644
--- a/include/sound/soc-usb.h
+++ b/include/sound/soc-usb.h
@@ -19,20 +19,21 @@ struct snd_soc_usb {
struct device *dev;
struct snd_soc_component *component;
int (*connection_status_cb)(struct snd_soc_usb *usb, int card_idx,
- int connected);
+ int chip_idx, int num_pcm, int connected);
void *priv_data;
};

int snd_soc_usb_find_format(int card_idx, struct snd_pcm_hw_params *params,
int direction);

-int snd_soc_usb_connect(struct device *usbdev, int card_idx);
-int snd_soc_usb_disconnect(struct device *usbdev);
+int snd_soc_usb_connect(struct device *usbdev, int card_idx, int chip_idx,
+ int num_pcm);
+int snd_soc_usb_disconnect(struct device *usbdev, int card_idx);
void snd_soc_usb_set_priv_data(struct device *dev, void *priv);
void *snd_soc_usb_get_priv_data(struct device *usbdev);

struct snd_soc_usb *snd_soc_usb_add_port(struct device *dev, void *priv,
int (*connection_cb)(struct snd_soc_usb *usb, int card_idx,
- int connected));
+ int chip_idx, int num_pcm, int connected));
int snd_soc_usb_remove_port(struct device *dev);
#endif
diff --git a/sound/soc/qcom/qdsp6/q6usb.c b/sound/soc/qcom/qdsp6/q6usb.c
index c4f3039ef8c4..2fb6159bd2a8 100644
--- a/sound/soc/qcom/qdsp6/q6usb.c
+++ b/sound/soc/qcom/qdsp6/q6usb.c
@@ -25,10 +25,18 @@

#define SID_MASK 0xF

+struct q6usb_status {
+ unsigned int num_pcm;
+ unsigned int chip_index;
+ unsigned int pcm_index;
+};
+
struct q6usb_port_data {
struct q6afe_usb_cfg usb_cfg;
struct snd_soc_usb *usb;
struct q6usb_offload priv;
+ unsigned long available_card_slot;
+ struct q6usb_status status[SNDRV_CARDS];
int active_idx;
};

@@ -97,7 +105,7 @@ static int q6usb_audio_ports_of_xlate_dai_name(struct snd_soc_component *compone
}

static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
- int connected)
+ int chip_idx, int num_pcm, int connected)
{
struct snd_soc_dapm_context *dapm;
struct q6usb_port_data *data;
@@ -109,8 +117,16 @@ static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
snd_soc_dapm_enable_pin(dapm, "USB_RX_BE");
/* We only track the latest USB headset plugged in */
data->active_idx = card_idx;
+
+ set_bit(card_idx, &data->available_card_slot);
+ data->status[card_idx].num_pcm = num_pcm;
+ data->status[card_idx].chip_index = chip_idx;
} else {
- snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
+ clear_bit(card_idx, &data->available_card_slot);
+ data->status[card_idx].num_pcm = 0;
+ data->status[card_idx].chip_index = 0;
+ if (!data->available_card_slot)
+ snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
}
snd_soc_dapm_sync(dapm);

diff --git a/sound/soc/soc-usb.c b/sound/soc/soc-usb.c
index cdce1bb42df5..84dc6d0b2eab 100644
--- a/sound/soc/soc-usb.c
+++ b/sound/soc/soc-usb.c
@@ -95,7 +95,7 @@ EXPORT_SYMBOL_GPL(snd_soc_usb_find_format);
*/
struct snd_soc_usb *snd_soc_usb_add_port(struct device *dev, void *priv,
int (*connection_cb)(struct snd_soc_usb *usb, int card_idx,
- int connected))
+ int chip_idx, int num_pcm, int connected))
{
struct snd_soc_usb *usb;

@@ -149,7 +149,8 @@ EXPORT_SYMBOL_GPL(snd_soc_usb_remove_port);
* handle how the USB backend selects, which device to enable offloading on.
*
*/
-int snd_soc_usb_connect(struct device *usbdev, int card_idx)
+int snd_soc_usb_connect(struct device *usbdev, int card_idx, int chip_idx,
+ int num_pcm)
{
struct snd_soc_usb *ctx;

@@ -161,7 +162,8 @@ int snd_soc_usb_connect(struct device *usbdev, int card_idx)
return -ENODEV;

if (ctx->connection_status_cb)
- ctx->connection_status_cb(ctx, card_idx, 1);
+ ctx->connection_status_cb(ctx, card_idx, chip_idx,
+ num_pcm, 1);

return 0;
}
@@ -174,7 +176,7 @@ EXPORT_SYMBOL_GPL(snd_soc_usb_connect);
* Notify of a new USB SND device disconnection to the USB backend.
*
*/
-int snd_soc_usb_disconnect(struct device *usbdev)
+int snd_soc_usb_disconnect(struct device *usbdev, int card_idx)
{
struct snd_soc_usb *ctx;

@@ -186,7 +188,7 @@ int snd_soc_usb_disconnect(struct device *usbdev)
return -ENODEV;

if (ctx->connection_status_cb)
- ctx->connection_status_cb(ctx, -1, 0);
+ ctx->connection_status_cb(ctx, card_idx, 0, 0, 0);

return 0;
}
diff --git a/sound/usb/qcom/qc_audio_offload.c b/sound/usb/qcom/qc_audio_offload.c
index a2ae0d31df97..c90824751da9 100644
--- a/sound/usb/qcom/qc_audio_offload.c
+++ b/sound/usb/qcom/qc_audio_offload.c
@@ -1591,7 +1591,8 @@ static void qc_usb_audio_offload_probe(struct snd_usb_audio *chip)
atomic_inc(&uaudio_qdev->qdev_in_use);
mutex_unlock(&qdev_mutex);

- snd_soc_usb_connect(usb_get_usb_backend(udev), chip->index);
+ snd_soc_usb_connect(usb_get_usb_backend(udev), chip->card->number,
+ chip->index, chip->pcm_devs);
}

/**
@@ -1677,12 +1678,12 @@ static void qc_usb_audio_offload_disconnect(struct snd_usb_audio *chip)
dev->chip = NULL;
mutex_unlock(&chip->mutex);

+ snd_soc_usb_disconnect(usb_get_usb_backend(udev), card_num);
+
mutex_lock(&qdev_mutex);
atomic_dec(&uaudio_qdev->qdev_in_use);
- if (!atomic_read(&uaudio_qdev->qdev_in_use)) {
- snd_soc_usb_disconnect(usb_get_usb_backend(udev));
+ if (!atomic_read(&uaudio_qdev->qdev_in_use))
qc_usb_audio_cleanup_qmi_dev();
- }
mutex_unlock(&qdev_mutex);

xhci_sideband_unregister(dev->sb);

2023-07-25 03:07:58

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 11/32] sound: usb: card: Introduce USB SND platform op callbacks

Allow for different platforms to be notified on USB SND connect/disconnect
seqeunces. This allows for platform USB SND modules to properly initialize
and populate internal structures with references to the USB SND chip
device.

Signed-off-by: Wesley Cheng <[email protected]>
---
sound/usb/card.c | 36 ++++++++++++++++++++++++++++++++++++
sound/usb/card.h | 20 ++++++++++++++++++++
2 files changed, 56 insertions(+)

diff --git a/sound/usb/card.c b/sound/usb/card.c
index f6e99ced8068..9365d1e17836 100644
--- a/sound/usb/card.c
+++ b/sound/usb/card.c
@@ -117,6 +117,30 @@ MODULE_PARM_DESC(skip_validation, "Skip unit descriptor validation (default: no)
static DEFINE_MUTEX(register_mutex);
static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
static struct usb_driver usb_audio_driver;
+static struct snd_usb_platform_ops *platform_ops;
+
+int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops)
+{
+ if (platform_ops)
+ return -EEXIST;
+
+ mutex_lock(&register_mutex);
+ platform_ops = ops;
+ mutex_unlock(&register_mutex);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(snd_usb_register_platform_ops);
+
+int snd_usb_unregister_platform_ops(void)
+{
+ mutex_lock(&register_mutex);
+ platform_ops = NULL;
+ mutex_unlock(&register_mutex);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(snd_usb_unregister_platform_ops);

/*
* disconnect streams
@@ -908,7 +932,11 @@ static int usb_audio_probe(struct usb_interface *intf,
chip->num_interfaces++;
usb_set_intfdata(intf, chip);
atomic_dec(&chip->active);
+
+ if (platform_ops && platform_ops->connect_cb)
+ platform_ops->connect_cb(chip);
mutex_unlock(&register_mutex);
+
return 0;

__error:
@@ -945,6 +973,9 @@ static void usb_audio_disconnect(struct usb_interface *intf)
card = chip->card;

mutex_lock(&register_mutex);
+ if (platform_ops && platform_ops->disconnect_cb)
+ platform_ops->disconnect_cb(chip);
+
if (atomic_inc_return(&chip->shutdown) == 1) {
struct snd_usb_stream *as;
struct snd_usb_endpoint *ep;
@@ -1086,6 +1117,11 @@ static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
chip->system_suspend = chip->num_suspended_intf;
}

+ mutex_lock(&register_mutex);
+ if (platform_ops && platform_ops->suspend_cb)
+ platform_ops->suspend_cb(intf, message);
+ mutex_unlock(&register_mutex);
+
return 0;
}

diff --git a/sound/usb/card.h b/sound/usb/card.h
index 6ec95b2edf86..c48744724f7d 100644
--- a/sound/usb/card.h
+++ b/sound/usb/card.h
@@ -207,4 +207,24 @@ struct snd_usb_stream {
struct list_head list;
};

+struct snd_usb_platform_ops {
+ void (*connect_cb)(struct snd_usb_audio *chip);
+ void (*disconnect_cb)(struct snd_usb_audio *chip);
+ void (*suspend_cb)(struct usb_interface *intf, pm_message_t message);
+};
+
+#if IS_ENABLED(CONFIG_SND_USB_AUDIO)
+int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops);
+int snd_usb_unregister_platform_ops(void);
+#else
+static int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops)
+{
+ return -EOPNOTSUPP;
+}
+
+static int snd_usb_unregister_platform_ops(void)
+{
+ return -EOPNOTSUPP;
+}
+#endif /* IS_ENABLED(CONFIG_SND_USB_AUDIO) */
#endif /* __USBAUDIO_CARD_H */

2023-07-25 03:10:04

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 10/32] ASoC: qcom: Add USB backend ASoC driver for Q6

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.

Signed-off-by: Wesley Cheng <[email protected]>
---
include/sound/q6usboffload.h | 20 ++++
sound/soc/qcom/Kconfig | 4 +
sound/soc/qcom/qdsp6/Makefile | 1 +
sound/soc/qcom/qdsp6/q6usb.c | 208 ++++++++++++++++++++++++++++++++++
4 files changed, 233 insertions(+)
create mode 100644 include/sound/q6usboffload.h
create mode 100644 sound/soc/qcom/qdsp6/q6usb.c

diff --git a/include/sound/q6usboffload.h b/include/sound/q6usboffload.h
new file mode 100644
index 000000000000..4fb1912d9f55
--- /dev/null
+++ b/include/sound/q6usboffload.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * linux/sound/q6usboffload.h -- QDSP6 USB offload
+ *
+ * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ */
+
+/**
+ * struct q6usb_offload
+ * @dev - dev handle to usb be
+ * @sid - streamID for iommu
+ * @intr_num - usb interrupter number
+ * @domain - allocated iommu domain
+ **/
+struct q6usb_offload {
+ struct device *dev;
+ long long sid;
+ u32 intr_num;
+ struct iommu_domain *domain;
+};
diff --git a/sound/soc/qcom/Kconfig b/sound/soc/qcom/Kconfig
index e7b00d1d9e99..bb285af6bb04 100644
--- a/sound/soc/qcom/Kconfig
+++ b/sound/soc/qcom/Kconfig
@@ -114,6 +114,9 @@ config SND_SOC_QDSP6_APM
config SND_SOC_QDSP6_PRM_LPASS_CLOCKS
tristate

+config SND_SOC_QDSP6_USB
+ tristate
+
config SND_SOC_QDSP6_PRM
tristate
select SND_SOC_QDSP6_PRM_LPASS_CLOCKS
@@ -134,6 +137,7 @@ config SND_SOC_QDSP6
select SND_SOC_TOPOLOGY
select SND_SOC_QDSP6_APM
select SND_SOC_QDSP6_PRM
+ select SND_SOC_QDSP6_USB
help
To add support for MSM QDSP6 Soc Audio.
This will enable sound soc platform specific
diff --git a/sound/soc/qcom/qdsp6/Makefile b/sound/soc/qcom/qdsp6/Makefile
index 3963bf234664..c9457ee898d0 100644
--- a/sound/soc/qcom/qdsp6/Makefile
+++ b/sound/soc/qcom/qdsp6/Makefile
@@ -17,3 +17,4 @@ obj-$(CONFIG_SND_SOC_QDSP6_APM_DAI) += q6apm-dai.o
obj-$(CONFIG_SND_SOC_QDSP6_APM_LPASS_DAI) += q6apm-lpass-dais.o
obj-$(CONFIG_SND_SOC_QDSP6_PRM) += q6prm.o
obj-$(CONFIG_SND_SOC_QDSP6_PRM_LPASS_CLOCKS) += q6prm-clocks.o
+obj-$(CONFIG_SND_SOC_QDSP6_USB) += q6usb.o
diff --git a/sound/soc/qcom/qdsp6/q6usb.c b/sound/soc/qcom/qdsp6/q6usb.c
new file mode 100644
index 000000000000..64e0e2cc3abf
--- /dev/null
+++ b/sound/soc/qcom/qdsp6/q6usb.c
@@ -0,0 +1,208 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ */
+
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/iommu.h>
+#include <linux/dma-mapping.h>
+#include <linux/dma-map-ops.h>
+
+#include <sound/pcm.h>
+#include <sound/soc.h>
+#include <sound/soc-usb.h>
+#include <sound/pcm_params.h>
+#include <sound/asound.h>
+#include <sound/q6usboffload.h>
+
+#include "q6dsp-lpass-ports.h"
+#include "q6afe.h"
+
+#define SID_MASK 0xF
+
+struct q6usb_port_data {
+ struct q6afe_usb_cfg usb_cfg;
+ struct snd_soc_usb *usb;
+ struct q6usb_offload priv;
+ int active_idx;
+};
+
+static const struct snd_soc_dapm_widget q6usb_dai_widgets[] = {
+ SND_SOC_DAPM_HP("USB_RX_BE", NULL),
+};
+
+static const struct snd_soc_dapm_route q6usb_dapm_routes[] = {
+ {"USB Playback", NULL, "USB_RX_BE"},
+};
+
+static int q6usb_hw_params(struct snd_pcm_substream *substream,
+ struct snd_pcm_hw_params *params,
+ struct snd_soc_dai *dai)
+{
+ return 0;
+}
+static const struct snd_soc_dai_ops q6usb_ops = {
+ .hw_params = q6usb_hw_params,
+};
+
+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,
+ },
+};
+
+static int q6usb_audio_ports_of_xlate_dai_name(struct snd_soc_component *component,
+ const struct of_phandle_args *args,
+ const char **dai_name)
+{
+ int id = args->args[0];
+ int ret = -EINVAL;
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(q6usb_be_dais); i++) {
+ if (q6usb_be_dais[i].id == id) {
+ *dai_name = q6usb_be_dais[i].name;
+ ret = 0;
+ break;
+ }
+ }
+
+ return ret;
+}
+
+static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
+ int connected)
+{
+ struct snd_soc_dapm_context *dapm;
+ struct q6usb_port_data *data;
+
+ dapm = snd_soc_component_get_dapm(usb->component);
+ data = dev_get_drvdata(usb->component->dev);
+
+ if (connected) {
+ snd_soc_dapm_enable_pin(dapm, "USB_RX_BE");
+ /* We only track the latest USB headset plugged in */
+ data->active_idx = card_idx;
+ } else {
+ snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
+ }
+ snd_soc_dapm_sync(dapm);
+
+ return 0;
+}
+
+static int q6usb_component_probe(struct snd_soc_component *component)
+{
+ struct q6usb_port_data *data = dev_get_drvdata(component->dev);
+ struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
+
+ snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
+ snd_soc_dapm_sync(dapm);
+
+ data->usb = snd_soc_usb_add_port(component->dev, &data->priv, q6usb_alsa_connection_cb);
+ if (IS_ERR(data->usb)) {
+ dev_err(component->dev, "failed to add usb port\n");
+ return -ENODEV;
+ }
+
+ data->usb->component = component;
+
+ return 0;
+}
+
+static const struct snd_soc_component_driver q6usb_dai_component = {
+ .probe = q6usb_component_probe,
+ .name = "q6usb-dai-component",
+ .dapm_widgets = q6usb_dai_widgets,
+ .num_dapm_widgets = ARRAY_SIZE(q6usb_dai_widgets),
+ .dapm_routes = q6usb_dapm_routes,
+ .num_dapm_routes = ARRAY_SIZE(q6usb_dapm_routes),
+ .of_xlate_dai_name = q6usb_audio_ports_of_xlate_dai_name,
+};
+
+static int q6usb_dai_dev_probe(struct platform_device *pdev)
+{
+ struct device_node *node = pdev->dev.of_node;
+ struct q6usb_port_data *data;
+ struct device *dev = &pdev->dev;
+ struct of_phandle_args args;
+ int ret;
+
+ data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
+ if (!data)
+ return -ENOMEM;
+
+ ret = of_property_read_u32(node, "qcom,usb-audio-intr-num",
+ &data->priv.intr_num);
+ if (ret) {
+ dev_err(&pdev->dev, "failed to read intr num.\n");
+ return ret;
+ }
+
+ ret = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args);
+ if (ret < 0)
+ data->priv.sid = -1;
+ else
+ data->priv.sid = args.args[0] & SID_MASK;
+
+ data->priv.domain = iommu_get_domain_for_dev(&pdev->dev);
+
+ data->priv.dev = dev;
+ dev_set_drvdata(dev, data);
+
+ ret = devm_snd_soc_register_component(dev, &q6usb_dai_component,
+ q6usb_be_dais, ARRAY_SIZE(q6usb_be_dais));
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
+static int q6usb_dai_dev_remove(struct platform_device *pdev)
+{
+ snd_soc_usb_remove_port(&pdev->dev);
+
+ return 0;
+}
+
+static const struct of_device_id q6usb_dai_device_id[] = {
+ { .compatible = "qcom,q6usb" },
+ {},
+};
+MODULE_DEVICE_TABLE(of, q6usb_dai_device_id);
+
+static struct platform_driver q6usb_dai_platform_driver = {
+ .driver = {
+ .name = "q6usb-dai",
+ .of_match_table = of_match_ptr(q6usb_dai_device_id),
+ },
+ .probe = q6usb_dai_dev_probe,
+ .remove = q6usb_dai_dev_remove,
+};
+module_platform_driver(q6usb_dai_platform_driver);
+
+MODULE_DESCRIPTION("Q6 USB backend dai driver");
+MODULE_LICENSE("GPL");

2023-07-25 03:12:59

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 25/32] ASoC: qcom: qdsp6: q6afe: Split USB AFE dev_token param into separate API

The Q6USB backend can carry information about the available USB SND cards
and PCM devices discovered on the USB bus. The dev_token field is used by
the audio DSP to notify the USB offload driver of which card and PCM index
to enable playback on. Separate this into a dedicated API, so the USB
backend can set the dev_token accordingly. The audio DSP does not utilize
this information until the AFE port start command is sent, which is done
during the PCM prepare phase.

Signed-off-by: Wesley Cheng <[email protected]>
---
sound/soc/qcom/qdsp6/q6afe.c | 49 +++++++++++++++++++++++++-----------
sound/soc/qcom/qdsp6/q6afe.h | 1 +
2 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/sound/soc/qcom/qdsp6/q6afe.c b/sound/soc/qcom/qdsp6/q6afe.c
index e9d5fa6b6b0d..505371c96987 100644
--- a/sound/soc/qcom/qdsp6/q6afe.c
+++ b/sound/soc/qcom/qdsp6/q6afe.c
@@ -1394,10 +1394,42 @@ void q6afe_tdm_port_prepare(struct q6afe_port *port,
}
EXPORT_SYMBOL_GPL(q6afe_tdm_port_prepare);

-static int afe_port_send_usb_dev_param(struct q6afe_port *port, struct q6afe_usb_cfg *cfg)
+/**
+ * afe_port_send_usb_dev_param() - Send USB dev token
+ *
+ * @port: Instance of afe port
+ * @cardidx: USB SND card index to reference
+ * @pcmidx: USB SND PCM device index to reference
+ *
+ * The USB dev token carries information about which USB SND card instance and
+ * PCM device to execute the offload on. This information is carried through
+ * to the stream enable QMI request, which is handled by the offload class
+ * driver. The information is parsed to determine which USB device to query
+ * the required resources for.
+ */
+int afe_port_send_usb_dev_param(struct q6afe_port *port, int cardidx, int pcmidx)
{
- union afe_port_config *pcfg = &port->port_cfg;
struct afe_param_id_usb_audio_dev_params usb_dev;
+ int ret;
+
+ memset(&usb_dev, 0, sizeof(usb_dev));
+
+ usb_dev.cfg_minor_version = AFE_API_MINOR_VERSION_USB_AUDIO_CONFIG;
+ usb_dev.dev_token = (cardidx << 16) | (pcmidx << 8);
+ ret = q6afe_port_set_param_v2(port, &usb_dev,
+ AFE_PARAM_ID_USB_AUDIO_DEV_PARAMS,
+ AFE_MODULE_AUDIO_DEV_INTERFACE, sizeof(usb_dev));
+ if (ret)
+ dev_err(port->afe->dev, "%s: AFE device param cmd failed %d\n",
+ __func__, ret);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(afe_port_send_usb_dev_param);
+
+static int afe_port_send_usb_params(struct q6afe_port *port, struct q6afe_usb_cfg *cfg)
+{
+ union afe_port_config *pcfg = &port->port_cfg;
struct afe_param_id_usb_audio_dev_lpcm_fmt lpcm_fmt;
struct afe_param_id_usb_audio_svc_interval svc_int;
int ret = 0;
@@ -1408,20 +1440,9 @@ static int afe_port_send_usb_dev_param(struct q6afe_port *port, struct q6afe_usb
goto exit;
}

- memset(&usb_dev, 0, sizeof(usb_dev));
memset(&lpcm_fmt, 0, sizeof(lpcm_fmt));
memset(&svc_int, 0, sizeof(svc_int));

- usb_dev.cfg_minor_version = AFE_API_MINOR_VERSION_USB_AUDIO_CONFIG;
- q6afe_port_set_param_v2(port, &usb_dev,
- AFE_PARAM_ID_USB_AUDIO_DEV_PARAMS,
- AFE_MODULE_AUDIO_DEV_INTERFACE, sizeof(usb_dev));
- if (ret) {
- dev_err(port->afe->dev, "%s: AFE device param cmd failed %d\n",
- __func__, ret);
- goto exit;
- }
-
lpcm_fmt.cfg_minor_version = AFE_API_MINOR_VERSION_USB_AUDIO_CONFIG;
lpcm_fmt.endian = pcfg->usb_cfg.endian;
ret = q6afe_port_set_param_v2(port, &lpcm_fmt,
@@ -1465,7 +1486,7 @@ void q6afe_usb_port_prepare(struct q6afe_port *port,
pcfg->usb_cfg.num_channels = cfg->num_channels;
pcfg->usb_cfg.bit_width = cfg->bit_width;

- afe_port_send_usb_dev_param(port, cfg);
+ afe_port_send_usb_params(port, cfg);
}
EXPORT_SYMBOL_GPL(q6afe_usb_port_prepare);

diff --git a/sound/soc/qcom/qdsp6/q6afe.h b/sound/soc/qcom/qdsp6/q6afe.h
index e098a3e15135..7980416275e9 100644
--- a/sound/soc/qcom/qdsp6/q6afe.h
+++ b/sound/soc/qcom/qdsp6/q6afe.h
@@ -274,6 +274,7 @@ void q6afe_tdm_port_prepare(struct q6afe_port *port, struct q6afe_tdm_cfg *cfg);
void q6afe_cdc_dma_port_prepare(struct q6afe_port *port,
struct q6afe_cdc_dma_cfg *cfg);

+int afe_port_send_usb_dev_param(struct q6afe_port *port, int cardidx, int pcmidx);
int q6afe_port_set_sysclk(struct q6afe_port *port, int clk_id,
int clk_src, int clk_root,
unsigned int freq, int dir);

2023-07-25 03:27:17

by Randy Dunlap

[permalink] [raw]
Subject: Re: [PATCH v4 03/32] xhci: sideband: add initial api to register a sideband entity



On 7/24/23 19:33, Wesley Cheng wrote:
> diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
> index c170672f847e..d9dc92bea525 100644
> --- a/drivers/usb/host/Kconfig
> +++ b/drivers/usb/host/Kconfig
> @@ -104,6 +104,15 @@ config USB_XHCI_RZV2M
> Say 'Y' to enable the support for the xHCI host controller
> found in Renesas RZ/V2M SoC.
>
> +config USB_XHCI_SIDEBAND
> + bool "xHCI support for sideband"
> + help
> + Say 'Y' to enable the support for the xHCI sideband capability.
> + provide a mechanism for a sideband datapath for payload associated

Provide

> + with audio class endpoints. This allows for an audio DSP to use
> + xHCI USB endpoints directly, allowing CPU to sleep while playing
> + audio

audio.


--
~Randy

2023-07-25 03:34:58

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 20/32] sound: soc: soc-usb: Add PCM format check API for USB backend

Introduce a check for if a particular PCM format is supported by the USB
audio device connected. If the USB audio device does not have an audio
profile which can support the requested format, then notify the USB
backend.

Signed-off-by: Wesley Cheng <[email protected]>
---
include/sound/soc-usb.h | 3 +++
sound/soc/soc-usb.c | 13 +++++++++++++
2 files changed, 16 insertions(+)

diff --git a/include/sound/soc-usb.h b/include/sound/soc-usb.h
index 378992ea07bd..71e6e75e600a 100644
--- a/include/sound/soc-usb.h
+++ b/include/sound/soc-usb.h
@@ -23,6 +23,9 @@ struct snd_soc_usb {
void *priv_data;
};

+int snd_soc_usb_find_format(int card_idx, struct snd_pcm_hw_params *params,
+ int direction);
+
int snd_soc_usb_connect(struct device *usbdev, int card_idx);
int snd_soc_usb_disconnect(struct device *usbdev);
void snd_soc_usb_set_priv_data(struct device *dev, void *priv);
diff --git a/sound/soc/soc-usb.c b/sound/soc/soc-usb.c
index 4293451cdd49..cdce1bb42df5 100644
--- a/sound/soc/soc-usb.c
+++ b/sound/soc/soc-usb.c
@@ -70,6 +70,19 @@ void *snd_soc_usb_get_priv_data(struct device *dev)
}
EXPORT_SYMBOL_GPL(snd_soc_usb_get_priv_data);

+int snd_soc_usb_find_format(int card_idx, struct snd_pcm_hw_params *params,
+ int direction)
+{
+ struct snd_usb_stream *as;
+
+ as = snd_usb_find_suppported_substream(card_idx, params, direction);
+ if (!as)
+ return -EOPNOTSUPP;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(snd_soc_usb_find_format);
+
/**
* snd_soc_usb_add_port() - Add a USB backend port
* @dev: USB backend device

2023-07-25 03:35:16

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 17/32] sound: usb: qcom: Add USB QMI definitions

The Qualcomm USB audio offload driver utilizes the QMI protocol to
communicate with the audio DSP. Add the necessary QMI header and field
definitions, so the QMI interface driver is able to route the QMI packet
received to the USB audio offload driver.

Signed-off-by: Wesley Cheng <[email protected]>
---
sound/usb/qcom/usb_audio_qmi_v01.c | 892 +++++++++++++++++++++++++++++
sound/usb/qcom/usb_audio_qmi_v01.h | 162 ++++++
2 files changed, 1054 insertions(+)
create mode 100644 sound/usb/qcom/usb_audio_qmi_v01.c
create mode 100644 sound/usb/qcom/usb_audio_qmi_v01.h

diff --git a/sound/usb/qcom/usb_audio_qmi_v01.c b/sound/usb/qcom/usb_audio_qmi_v01.c
new file mode 100644
index 000000000000..bdfd67d980eb
--- /dev/null
+++ b/sound/usb/qcom/usb_audio_qmi_v01.c
@@ -0,0 +1,892 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ */
+
+#include <linux/soc/qcom/qmi.h>
+
+#include "usb_audio_qmi_v01.h"
+
+static struct qmi_elem_info mem_info_v01_ei[] = {
+ {
+ .data_type = QMI_UNSIGNED_8_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u64),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct mem_info_v01, va),
+ },
+ {
+ .data_type = QMI_UNSIGNED_8_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u64),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct mem_info_v01, pa),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct mem_info_v01, size),
+ },
+ {
+ .data_type = QMI_EOTI,
+ .array_type = NO_ARRAY,
+ .tlv_type = QMI_COMMON_TLV_TYPE,
+ },
+};
+
+static struct qmi_elem_info apps_mem_info_v01_ei[] = {
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct mem_info_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct apps_mem_info_v01, evt_ring),
+ .ei_array = mem_info_v01_ei,
+ },
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct mem_info_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct apps_mem_info_v01, tr_data),
+ .ei_array = mem_info_v01_ei,
+ },
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct mem_info_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct apps_mem_info_v01, tr_sync),
+ .ei_array = mem_info_v01_ei,
+ },
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct mem_info_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct apps_mem_info_v01, xfer_buff),
+ .ei_array = mem_info_v01_ei,
+ },
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct mem_info_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct apps_mem_info_v01, dcba),
+ .ei_array = mem_info_v01_ei,
+ },
+ {
+ .data_type = QMI_EOTI,
+ .array_type = NO_ARRAY,
+ .tlv_type = QMI_COMMON_TLV_TYPE,
+ },
+};
+
+static struct qmi_elem_info usb_endpoint_descriptor_v01_ei[] = {
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_endpoint_descriptor_v01,
+ bLength),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_endpoint_descriptor_v01,
+ bDescriptorType),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_endpoint_descriptor_v01,
+ bEndpointAddress),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_endpoint_descriptor_v01,
+ bmAttributes),
+ },
+ {
+ .data_type = QMI_UNSIGNED_2_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u16),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_endpoint_descriptor_v01,
+ wMaxPacketSize),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_endpoint_descriptor_v01,
+ bInterval),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_endpoint_descriptor_v01,
+ bRefresh),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_endpoint_descriptor_v01,
+ bSynchAddress),
+ },
+ {
+ .data_type = QMI_EOTI,
+ .array_type = NO_ARRAY,
+ .tlv_type = QMI_COMMON_TLV_TYPE,
+ },
+};
+
+static struct qmi_elem_info usb_interface_descriptor_v01_ei[] = {
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_interface_descriptor_v01,
+ bLength),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_interface_descriptor_v01,
+ bDescriptorType),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_interface_descriptor_v01,
+ bInterfaceNumber),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_interface_descriptor_v01,
+ bAlternateSetting),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_interface_descriptor_v01,
+ bNumEndpoints),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_interface_descriptor_v01,
+ bInterfaceClass),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_interface_descriptor_v01,
+ bInterfaceSubClass),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_interface_descriptor_v01,
+ bInterfaceProtocol),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0,
+ .offset = offsetof(struct usb_interface_descriptor_v01,
+ iInterface),
+ },
+ {
+ .data_type = QMI_EOTI,
+ .array_type = NO_ARRAY,
+ .tlv_type = QMI_COMMON_TLV_TYPE,
+ },
+};
+
+struct qmi_elem_info qmi_uaudio_stream_req_msg_v01_ei[] = {
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x01,
+ .offset = offsetof(struct qmi_uaudio_stream_req_msg_v01,
+ enable),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x02,
+ .offset = offsetof(struct qmi_uaudio_stream_req_msg_v01,
+ usb_token),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x10,
+ .offset = offsetof(struct qmi_uaudio_stream_req_msg_v01,
+ audio_format_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x10,
+ .offset = offsetof(struct qmi_uaudio_stream_req_msg_v01,
+ audio_format),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x11,
+ .offset = offsetof(struct qmi_uaudio_stream_req_msg_v01,
+ number_of_ch_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x11,
+ .offset = offsetof(struct qmi_uaudio_stream_req_msg_v01,
+ number_of_ch),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x12,
+ .offset = offsetof(struct qmi_uaudio_stream_req_msg_v01,
+ bit_rate_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x12,
+ .offset = offsetof(struct qmi_uaudio_stream_req_msg_v01,
+ bit_rate),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x13,
+ .offset = offsetof(struct qmi_uaudio_stream_req_msg_v01,
+ xfer_buff_size_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x13,
+ .offset = offsetof(struct qmi_uaudio_stream_req_msg_v01,
+ xfer_buff_size),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x14,
+ .offset = offsetof(struct qmi_uaudio_stream_req_msg_v01,
+ service_interval_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x14,
+ .offset = offsetof(struct qmi_uaudio_stream_req_msg_v01,
+ service_interval),
+ },
+ {
+ .data_type = QMI_EOTI,
+ .array_type = NO_ARRAY,
+ .tlv_type = QMI_COMMON_TLV_TYPE,
+ },
+};
+
+struct qmi_elem_info qmi_uaudio_stream_resp_msg_v01_ei[] = {
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct qmi_response_type_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x02,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ resp),
+ .ei_array = qmi_response_type_v01_ei,
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x10,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ status_valid),
+ },
+ {
+ .data_type = QMI_SIGNED_4_BYTE_ENUM,
+ .elem_len = 1,
+ .elem_size = sizeof(enum usb_qmi_audio_stream_status_enum_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x10,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ status),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x11,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ internal_status_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x11,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ internal_status),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x12,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ slot_id_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x12,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ slot_id),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x13,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ usb_token_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x13,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ usb_token),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x14,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ std_as_opr_intf_desc_valid),
+ },
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct usb_interface_descriptor_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x14,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ std_as_opr_intf_desc),
+ .ei_array = usb_interface_descriptor_v01_ei,
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x15,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ std_as_data_ep_desc_valid),
+ },
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct usb_endpoint_descriptor_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x15,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ std_as_data_ep_desc),
+ .ei_array = usb_endpoint_descriptor_v01_ei,
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x16,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ std_as_sync_ep_desc_valid),
+ },
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct usb_endpoint_descriptor_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x16,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ std_as_sync_ep_desc),
+ .ei_array = usb_endpoint_descriptor_v01_ei,
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x17,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ usb_audio_spec_revision_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_2_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u16),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x17,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ usb_audio_spec_revision),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x18,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ data_path_delay_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x18,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ data_path_delay),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x19,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ usb_audio_subslot_size_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x19,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ usb_audio_subslot_size),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x1A,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ xhci_mem_info_valid),
+ },
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct apps_mem_info_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x1A,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ xhci_mem_info),
+ .ei_array = apps_mem_info_v01_ei,
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x1B,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ interrupter_num_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x1B,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ interrupter_num),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x1C,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ speed_info_valid),
+ },
+ {
+ .data_type = QMI_SIGNED_4_BYTE_ENUM,
+ .elem_len = 1,
+ .elem_size = sizeof(enum usb_qmi_audio_device_speed_enum_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x1C,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ speed_info),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x1D,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ controller_num_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x1D,
+ .offset = offsetof(
+ struct qmi_uaudio_stream_resp_msg_v01,
+ controller_num),
+ },
+ {
+ .data_type = QMI_EOTI,
+ .array_type = NO_ARRAY,
+ .tlv_type = QMI_COMMON_TLV_TYPE,
+ },
+};
+
+struct qmi_elem_info qmi_uaudio_stream_ind_msg_v01_ei[] = {
+ {
+ .data_type = QMI_SIGNED_4_BYTE_ENUM,
+ .elem_len = 1,
+ .elem_size = sizeof(
+ enum usb_qmi_audio_device_indication_enum_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x01,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ dev_event),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x02,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ slot_id),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x10,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ usb_token_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_4_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u32),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x10,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ usb_token),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x11,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ std_as_opr_intf_desc_valid),
+ },
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct usb_interface_descriptor_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x11,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ std_as_opr_intf_desc),
+ .ei_array = usb_interface_descriptor_v01_ei,
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x12,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ std_as_data_ep_desc_valid),
+ },
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct usb_endpoint_descriptor_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x12,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ std_as_data_ep_desc),
+ .ei_array = usb_endpoint_descriptor_v01_ei,
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x13,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ std_as_sync_ep_desc_valid),
+ },
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct usb_endpoint_descriptor_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x13,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ std_as_sync_ep_desc),
+ .ei_array = usb_endpoint_descriptor_v01_ei,
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x14,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ usb_audio_spec_revision_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_2_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u16),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x14,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ usb_audio_spec_revision),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x15,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ data_path_delay_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x15,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ data_path_delay),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x16,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ usb_audio_subslot_size_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x16,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ usb_audio_subslot_size),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x17,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ xhci_mem_info_valid),
+ },
+ {
+ .data_type = QMI_STRUCT,
+ .elem_len = 1,
+ .elem_size = sizeof(struct apps_mem_info_v01),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x17,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ xhci_mem_info),
+ .ei_array = apps_mem_info_v01_ei,
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x18,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ interrupter_num_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x18,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ interrupter_num),
+ },
+ {
+ .data_type = QMI_OPT_FLAG,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x19,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ controller_num_valid),
+ },
+ {
+ .data_type = QMI_UNSIGNED_1_BYTE,
+ .elem_len = 1,
+ .elem_size = sizeof(u8),
+ .array_type = NO_ARRAY,
+ .tlv_type = 0x19,
+ .offset = offsetof(struct qmi_uaudio_stream_ind_msg_v01,
+ controller_num),
+ },
+ {
+ .data_type = QMI_EOTI,
+ .array_type = NO_ARRAY,
+ .tlv_type = QMI_COMMON_TLV_TYPE,
+ },
+};
diff --git a/sound/usb/qcom/usb_audio_qmi_v01.h b/sound/usb/qcom/usb_audio_qmi_v01.h
new file mode 100644
index 000000000000..68e3842affc5
--- /dev/null
+++ b/sound/usb/qcom/usb_audio_qmi_v01.h
@@ -0,0 +1,162 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ */
+
+#ifndef USB_QMI_V01_H
+#define USB_QMI_V01_H
+
+#define UAUDIO_STREAM_SERVICE_ID_V01 0x41D
+#define UAUDIO_STREAM_SERVICE_VERS_V01 0x01
+
+#define QMI_UAUDIO_STREAM_RESP_V01 0x0001
+#define QMI_UAUDIO_STREAM_REQ_V01 0x0001
+#define QMI_UAUDIO_STREAM_IND_V01 0x0001
+
+
+struct mem_info_v01 {
+ u64 va;
+ u64 pa;
+ u32 size;
+};
+
+struct apps_mem_info_v01 {
+ struct mem_info_v01 evt_ring;
+ struct mem_info_v01 tr_data;
+ struct mem_info_v01 tr_sync;
+ struct mem_info_v01 xfer_buff;
+ struct mem_info_v01 dcba;
+};
+
+struct usb_endpoint_descriptor_v01 {
+ u8 bLength;
+ u8 bDescriptorType;
+ u8 bEndpointAddress;
+ u8 bmAttributes;
+ u16 wMaxPacketSize;
+ u8 bInterval;
+ u8 bRefresh;
+ u8 bSynchAddress;
+};
+
+struct usb_interface_descriptor_v01 {
+ u8 bLength;
+ u8 bDescriptorType;
+ u8 bInterfaceNumber;
+ u8 bAlternateSetting;
+ u8 bNumEndpoints;
+ u8 bInterfaceClass;
+ u8 bInterfaceSubClass;
+ u8 bInterfaceProtocol;
+ u8 iInterface;
+};
+
+enum usb_qmi_audio_stream_status_enum_v01 {
+ USB_QMI_STREAM_STATUS_ENUM_MIN_VAL_V01 = INT_MIN,
+ USB_QMI_STREAM_REQ_SUCCESS_V01 = 0,
+ USB_QMI_STREAM_REQ_FAILURE_V01 = 1,
+ USB_QMI_STREAM_REQ_FAILURE_NOT_FOUND_V01 = 2,
+ USB_QMI_STREAM_REQ_FAILURE_INVALID_PARAM_V01 = 3,
+ USB_QMI_STREAM_REQ_FAILURE_MEMALLOC_V01 = 4,
+ USB_QMI_STREAM_STATUS_ENUM_MAX_VAL_V01 = INT_MAX,
+};
+
+enum usb_qmi_audio_device_indication_enum_v01 {
+ USB_QMI_DEVICE_INDICATION_ENUM_MIN_VAL_V01 = INT_MIN,
+ USB_QMI_DEV_CONNECT_V01 = 0,
+ USB_QMI_DEV_DISCONNECT_V01 = 1,
+ USB_QMI_DEV_SUSPEND_V01 = 2,
+ USB_QMI_DEV_RESUME_V01 = 3,
+ USB_QMI_DEVICE_INDICATION_ENUM_MAX_VAL_V01 = INT_MAX,
+};
+
+enum usb_qmi_audio_device_speed_enum_v01 {
+ USB_QMI_DEVICE_SPEED_ENUM_MIN_VAL_V01 = INT_MIN,
+ USB_QMI_DEVICE_SPEED_INVALID_V01 = 0,
+ USB_QMI_DEVICE_SPEED_LOW_V01 = 1,
+ USB_QMI_DEVICE_SPEED_FULL_V01 = 2,
+ USB_QMI_DEVICE_SPEED_HIGH_V01 = 3,
+ USB_QMI_DEVICE_SPEED_SUPER_V01 = 4,
+ USB_QMI_DEVICE_SPEED_SUPER_PLUS_V01 = 5,
+ USB_QMI_DEVICE_SPEED_ENUM_MAX_VAL_V01 = INT_MAX,
+};
+
+struct qmi_uaudio_stream_req_msg_v01 {
+ u8 enable;
+ u32 usb_token;
+ u8 audio_format_valid;
+ u32 audio_format;
+ u8 number_of_ch_valid;
+ u32 number_of_ch;
+ u8 bit_rate_valid;
+ u32 bit_rate;
+ u8 xfer_buff_size_valid;
+ u32 xfer_buff_size;
+ u8 service_interval_valid;
+ u32 service_interval;
+};
+#define QMI_UAUDIO_STREAM_REQ_MSG_V01_MAX_MSG_LEN 46
+extern struct qmi_elem_info qmi_uaudio_stream_req_msg_v01_ei[];
+
+struct qmi_uaudio_stream_resp_msg_v01 {
+ struct qmi_response_type_v01 resp;
+ u8 status_valid;
+ enum usb_qmi_audio_stream_status_enum_v01 status;
+ u8 internal_status_valid;
+ u32 internal_status;
+ u8 slot_id_valid;
+ u32 slot_id;
+ u8 usb_token_valid;
+ u32 usb_token;
+ u8 std_as_opr_intf_desc_valid;
+ struct usb_interface_descriptor_v01 std_as_opr_intf_desc;
+ u8 std_as_data_ep_desc_valid;
+ struct usb_endpoint_descriptor_v01 std_as_data_ep_desc;
+ u8 std_as_sync_ep_desc_valid;
+ struct usb_endpoint_descriptor_v01 std_as_sync_ep_desc;
+ u8 usb_audio_spec_revision_valid;
+ u16 usb_audio_spec_revision;
+ u8 data_path_delay_valid;
+ u8 data_path_delay;
+ u8 usb_audio_subslot_size_valid;
+ u8 usb_audio_subslot_size;
+ u8 xhci_mem_info_valid;
+ struct apps_mem_info_v01 xhci_mem_info;
+ u8 interrupter_num_valid;
+ u8 interrupter_num;
+ u8 speed_info_valid;
+ enum usb_qmi_audio_device_speed_enum_v01 speed_info;
+ u8 controller_num_valid;
+ u8 controller_num;
+};
+#define QMI_UAUDIO_STREAM_RESP_MSG_V01_MAX_MSG_LEN 202
+extern struct qmi_elem_info qmi_uaudio_stream_resp_msg_v01_ei[];
+
+struct qmi_uaudio_stream_ind_msg_v01 {
+ enum usb_qmi_audio_device_indication_enum_v01 dev_event;
+ u32 slot_id;
+ u8 usb_token_valid;
+ u32 usb_token;
+ u8 std_as_opr_intf_desc_valid;
+ struct usb_interface_descriptor_v01 std_as_opr_intf_desc;
+ u8 std_as_data_ep_desc_valid;
+ struct usb_endpoint_descriptor_v01 std_as_data_ep_desc;
+ u8 std_as_sync_ep_desc_valid;
+ struct usb_endpoint_descriptor_v01 std_as_sync_ep_desc;
+ u8 usb_audio_spec_revision_valid;
+ u16 usb_audio_spec_revision;
+ u8 data_path_delay_valid;
+ u8 data_path_delay;
+ u8 usb_audio_subslot_size_valid;
+ u8 usb_audio_subslot_size;
+ u8 xhci_mem_info_valid;
+ struct apps_mem_info_v01 xhci_mem_info;
+ u8 interrupter_num_valid;
+ u8 interrupter_num;
+ u8 controller_num_valid;
+ u8 controller_num;
+};
+#define QMI_UAUDIO_STREAM_IND_MSG_V01_MAX_MSG_LEN 181
+extern struct qmi_elem_info qmi_uaudio_stream_ind_msg_v01_ei[];
+
+#endif

2023-07-25 03:41:18

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 16/32] sound: usb: pcm: Export fixed rate check USB SND API

Some modules will handle calling snd_usb_endpoint_open() instead of the USB
SND framework. The USB SND endpoint open function added a new argument to
determine if the connect USB device can support multiple sampling rates.

Signed-off-by: Wesley Cheng <[email protected]>
---
sound/usb/pcm.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c
index b6fb6d84d9eb..955c6fea3bfa 100644
--- a/sound/usb/pcm.c
+++ b/sound/usb/pcm.c
@@ -186,6 +186,7 @@ bool snd_usb_pcm_has_fixed_rate(struct snd_usb_substream *subs)
}
return true;
}
+EXPORT_SYMBOL_GPL(snd_usb_pcm_has_fixed_rate);

static int init_pitch_v1(struct snd_usb_audio *chip, int ep)
{

2023-07-25 03:42:01

by Wesley Cheng

[permalink] [raw]
Subject: Re: [PATCH v4 00/32] Introduce QC USB SND audio offloading support

Apologies for the long delay in addressing some of your comments and
feedback. I understand it is difficult to refresh a lot of the material
after such a long gap, but hopefully I have addressed some of the
concerns in the latest revision. Appreciate all the suggestions/reviews!

Thanks
Wesley Cheng

On 7/24/2023 7:33 PM, Wesley Cheng wrote:
> 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
>
> Changelog
> --------------------------------------------
> 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 (3):
> xhci: add support to allocate several interrupters
> xhci: add helper to stop endpoint and wait for completion
> xhci: sideband: add initial api to register a sideband entity
>
> Wesley Cheng (29):
> 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: 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
> usb: host: xhci-plat: Set XHCI max interrupters if property is present
> sound: usb: pcm: Export fixed rate check USB SND API
> sound: usb: qcom: Add USB QMI definitions
> 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
> ASoC: qcom: qdsp6: q6afe: Split USB AFE dev_token param into separate
> API
> sound: Pass USB SND card and PCM information to SOC USB
> sound: soc: qdsp6: Add SND kcontrol to select offload device
> sound: soc: qdsp6: Add SND kcontrol for fetching offload status
> sound: soc: qcom: q6usb: Add headphone jack for offload connection
> status
> sound: usb: qc_audio_offload: Use card and PCM index from QMI request
> sound: usb: card: Allow for rediscovery of connected USB SND devices
> sound: soc: soc-usb: Rediscover USB SND devices on USB port add
>
> .../bindings/sound/qcom,q6usb-dais.yaml | 49 +
> .../bindings/sound/qcom,sm8250.yaml | 15 +
> .../devicetree/bindings/usb/snps,dwc3.yaml | 13 +
> arch/arm64/boot/dts/qcom/sm8350.dtsi.rej | 14 +
> arch/arm64/configs/defconfig.rej | 24 +
> drivers/usb/dwc3/core.c | 12 +
> drivers/usb/dwc3/core.h | 2 +
> drivers/usb/dwc3/host.c | 5 +-
> drivers/usb/host/Kconfig | 9 +
> drivers/usb/host/Makefile | 4 +
> drivers/usb/host/xhci-debugfs.c | 2 +-
> drivers/usb/host/xhci-hub.c | 29 +-
> drivers/usb/host/xhci-mem.c | 167 +-
> drivers/usb/host/xhci-plat.c | 2 +
> drivers/usb/host/xhci-ring.c | 2 +-
> drivers/usb/host/xhci-sideband.c | 195 ++
> drivers/usb/host/xhci.c | 109 +-
> drivers/usb/host/xhci.h | 102 +-
> .../sound/qcom,q6dsp-lpass-ports.h | 1 +
> include/linux/usb/xhci-intr.h | 86 +
> include/linux/usb/xhci-sideband.h | 90 +
> include/sound/q6usboffload.h | 20 +
> include/sound/soc-usb.h | 39 +
> sound/core/oss/pcm_oss.c | 4 +-
> sound/soc/Makefile | 2 +-
> sound/soc/qcom/Kconfig | 4 +
> sound/soc/qcom/qdsp6/Makefile | 1 +
> sound/soc/qcom/qdsp6/q6afe-dai.c | 50 +
> sound/soc/qcom/qdsp6/q6afe.c | 208 +-
> sound/soc/qcom/qdsp6/q6afe.h | 47 +-
> 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 | 458 +++++
> sound/soc/soc-usb.c | 197 ++
> sound/usb/Kconfig | 15 +
> sound/usb/Makefile | 2 +-
> sound/usb/card.c | 85 +
> sound/usb/card.h | 32 +
> sound/usb/endpoint.c | 2 +
> sound/usb/helper.c | 1 +
> sound/usb/pcm.c | 29 +-
> sound/usb/pcm.h | 12 +
> sound/usb/qcom/Makefile | 2 +
> sound/usb/qcom/qc_audio_offload.c | 1820 +++++++++++++++++
> sound/usb/qcom/usb_audio_qmi_v01.c | 892 ++++++++
> sound/usb/qcom/usb_audio_qmi_v01.h | 162 ++
> 47 files changed, 4870 insertions(+), 179 deletions(-)
> create mode 100644 Documentation/devicetree/bindings/sound/qcom,q6usb-dais.yaml
> create mode 100644 arch/arm64/boot/dts/qcom/sm8350.dtsi.rej
> create mode 100644 arch/arm64/configs/defconfig.rej
> create mode 100644 drivers/usb/host/xhci-sideband.c
> create mode 100644 include/linux/usb/xhci-intr.h
> 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/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
>

2023-07-25 03:49:08

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 19/32] sound: usb: card: Check for support for requested audio format

Allow for checks on a specific USB audio device to see if a requested PCM
format is supported. This is needed for support for when playback is
initiated by the ASoC USB backend path.

Signed-off-by: Wesley Cheng <[email protected]>
---
sound/usb/card.c | 28 ++++++++++++++++++++++++++++
sound/usb/card.h | 8 ++++++++
2 files changed, 36 insertions(+)

diff --git a/sound/usb/card.c b/sound/usb/card.c
index a3fad66a3337..365f6d978608 100644
--- a/sound/usb/card.c
+++ b/sound/usb/card.c
@@ -142,6 +142,34 @@ int snd_usb_unregister_platform_ops(void)
}
EXPORT_SYMBOL_GPL(snd_usb_unregister_platform_ops);

+struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
+ struct snd_pcm_hw_params *params, int direction)
+{
+ struct snd_usb_audio *chip = usb_chip[card_idx];
+ struct snd_usb_substream *subs = NULL;
+ struct snd_usb_stream *as;
+ const struct audioformat *fmt;
+
+ if (!chip)
+ return NULL;
+
+ mutex_lock(&chip->mutex);
+ if (enable[card_idx]) {
+ list_for_each_entry(as, &chip->pcm_list, list) {
+ subs = &as->substream[direction];
+ fmt = find_substream_format(subs, params);
+ if (fmt) {
+ mutex_unlock(&chip->mutex);
+ return as;
+ }
+ }
+ }
+ mutex_unlock(&chip->mutex);
+
+ return NULL;
+}
+EXPORT_SYMBOL_GPL(snd_usb_find_suppported_substream);
+
/*
* disconnect streams
* called from usb_audio_disconnect()
diff --git a/sound/usb/card.h b/sound/usb/card.h
index c48744724f7d..6d0f76ffec9d 100644
--- a/sound/usb/card.h
+++ b/sound/usb/card.h
@@ -216,6 +216,8 @@ struct snd_usb_platform_ops {
#if IS_ENABLED(CONFIG_SND_USB_AUDIO)
int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops);
int snd_usb_unregister_platform_ops(void);
+struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
+ struct snd_pcm_hw_params *params, int direction);
#else
static int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops)
{
@@ -226,5 +228,11 @@ static int snd_usb_unregister_platform_ops(void)
{
return -EOPNOTSUPP;
}
+
+static struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
+ struct snd_pcm_hw_params *params, int direction)
+{
+ return NULL;
+}
#endif /* IS_ENABLED(CONFIG_SND_USB_AUDIO) */
#endif /* __USBAUDIO_CARD_H */

2023-07-25 03:51:45

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 18/32] sound: usb: Introduce QC USB SND offloading support

Several Qualcomm SoCs have a dedicated audio DSP, which has the ability to
support USB sound devices. This vendor driver will implement the required
handshaking with the DSP, in order to pass along required resources that
will be utilized by the DSP's USB SW. The communication channel used for
this handshaking will be using the QMI protocol. Required resources
include:
- Allocated secondary event ring address
- EP transfer ring address
- Interrupter number

The above information will allow for the audio DSP to execute USB transfers
over the USB bus. It will also be able to support devices that have an
implicit feedback and sync endpoint as well. Offloading these data
transfers will allow the main/applications processor to enter lower CPU
power modes, and sustain a longer duration in those modes.

Audio offloading is initiated with the following sequence:
1. Userspace configures to route audio playback to USB backend and starts
playback on the platform soundcard.
2. The Q6DSP AFE will communicate to the audio DSP to start the USB AFE
port.
3. This results in a QMI packet with a STREAM enable command.
4. The QC audio offload driver will fetch the required resources, and pass
this information as part of the QMI response to the STREAM enable command.
5. Once the QMI response is received the audio DSP will start queuing data
on the USB bus.

Signed-off-by: Wesley Cheng <[email protected]>
---
sound/usb/Kconfig | 15 +
sound/usb/Makefile | 2 +-
sound/usb/qcom/Makefile | 2 +
sound/usb/qcom/qc_audio_offload.c | 1810 +++++++++++++++++++++++++++++
4 files changed, 1828 insertions(+), 1 deletion(-)
create mode 100644 sound/usb/qcom/Makefile
create mode 100644 sound/usb/qcom/qc_audio_offload.c

diff --git a/sound/usb/Kconfig b/sound/usb/Kconfig
index 059242f15d75..44b0fa92b6cc 100644
--- a/sound/usb/Kconfig
+++ b/sound/usb/Kconfig
@@ -165,6 +165,21 @@ config SND_BCD2000
To compile this driver as a module, choose M here: the module
will be called snd-bcd2000.

+config QC_USB_AUDIO_OFFLOAD
+ tristate "Qualcomm Audio Offload driver"
+ depends on QCOM_QMI_HELPERS
+ select SND_PCM
+ help
+ Say Y here to enable the Qualcomm USB audio offloading feature
+
+ This module sets up the required QMI stream enable/disable
+ responses to requests generated by the audio DSP. It passes the
+ USB transfer resource references, so that the audio DSP can issue
+ USB transfers to the host controller.
+
+ To compile this driver as a module, choose M here: the module
+ will be called qc-audio-offload.
+
source "sound/usb/line6/Kconfig"

endif # SND_USB
diff --git a/sound/usb/Makefile b/sound/usb/Makefile
index 9ccb21a4ff8a..2243ae333ec9 100644
--- a/sound/usb/Makefile
+++ b/sound/usb/Makefile
@@ -33,5 +33,5 @@ obj-$(CONFIG_SND_USB_UA101) += snd-usbmidi-lib.o
obj-$(CONFIG_SND_USB_USX2Y) += snd-usbmidi-lib.o
obj-$(CONFIG_SND_USB_US122L) += snd-usbmidi-lib.o

-obj-$(CONFIG_SND) += misc/ usx2y/ caiaq/ 6fire/ hiface/ bcd2000/
+obj-$(CONFIG_SND) += misc/ usx2y/ caiaq/ 6fire/ hiface/ bcd2000/ qcom/
obj-$(CONFIG_SND_USB_LINE6) += line6/
diff --git a/sound/usb/qcom/Makefile b/sound/usb/qcom/Makefile
new file mode 100644
index 000000000000..d27d39beb8ce
--- /dev/null
+++ b/sound/usb/qcom/Makefile
@@ -0,0 +1,2 @@
+snd-usb-audio-qmi-objs := usb_audio_qmi_v01.o qc_audio_offload.o
+obj-$(CONFIG_QC_USB_AUDIO_OFFLOAD) += snd-usb-audio-qmi.o
\ No newline at end of file
diff --git a/sound/usb/qcom/qc_audio_offload.c b/sound/usb/qcom/qc_audio_offload.c
new file mode 100644
index 000000000000..9b21376f2b5e
--- /dev/null
+++ b/sound/usb/qcom/qc_audio_offload.c
@@ -0,0 +1,1810 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ */
+
+#include <linux/ctype.h>
+#include <linux/moduleparam.h>
+#include <linux/module.h>
+#include <linux/usb.h>
+#include <linux/init.h>
+#include <linux/usb/hcd.h>
+#include <linux/usb/xhci-sideband.h>
+#include <linux/usb/xhci-intr.h>
+#include <linux/usb/quirks.h>
+#include <linux/usb/audio.h>
+#include <linux/usb/audio-v2.h>
+#include <linux/usb/audio-v3.h>
+#include <linux/soc/qcom/qmi.h>
+#include <linux/iommu.h>
+#include <linux/dma-mapping.h>
+#include <linux/dma-map-ops.h>
+#include <sound/q6usboffload.h>
+
+#include <sound/control.h>
+#include <sound/core.h>
+#include <sound/info.h>
+#include <sound/pcm.h>
+#include <sound/pcm_params.h>
+#include <sound/initval.h>
+
+#include <sound/soc.h>
+#include <sound/soc-usb.h>
+#include "../usbaudio.h"
+#include "../card.h"
+#include "../endpoint.h"
+#include "../helper.h"
+#include "../pcm.h"
+#include "../format.h"
+#include "../power.h"
+#include "usb_audio_qmi_v01.h"
+
+/* Stream disable request timeout during USB device disconnect */
+#define DEV_RELEASE_WAIT_TIMEOUT 10000 /* in ms */
+
+/* Data interval calculation parameters */
+#define BUS_INTERVAL_FULL_SPEED 1000 /* in us */
+#define BUS_INTERVAL_HIGHSPEED_AND_ABOVE 125 /* in us */
+#define MAX_BINTERVAL_ISOC_EP 16
+
+#define QMI_STREAM_REQ_CARD_NUM_MASK 0xffff0000
+#define QMI_STREAM_REQ_DEV_NUM_MASK 0xff00
+#define QMI_STREAM_REQ_DIRECTION 0xff
+
+/* iommu resource parameters and management */
+#define PREPEND_SID_TO_IOVA(iova, sid) ((u64)(((u64)(iova)) | \
+ (((u64)sid) << 32)))
+#define IOVA_BASE 0x1000
+#define IOVA_XFER_RING_BASE (IOVA_BASE + PAGE_SIZE * (SNDRV_CARDS + 1))
+#define IOVA_XFER_BUF_BASE (IOVA_XFER_RING_BASE + PAGE_SIZE * SNDRV_CARDS * 32)
+#define IOVA_XFER_RING_MAX (IOVA_XFER_BUF_BASE - PAGE_SIZE)
+#define IOVA_XFER_BUF_MAX (0xfffff000 - PAGE_SIZE)
+
+#define MAX_XFER_BUFF_LEN (24 * PAGE_SIZE)
+
+struct iova_info {
+ struct list_head list;
+ unsigned long start_iova;
+ size_t size;
+ bool in_use;
+};
+
+struct intf_info {
+ unsigned long data_xfer_ring_va;
+ size_t data_xfer_ring_size;
+ unsigned long sync_xfer_ring_va;
+ size_t sync_xfer_ring_size;
+ unsigned long xfer_buf_va;
+ size_t xfer_buf_size;
+ phys_addr_t xfer_buf_pa;
+ unsigned int data_ep_pipe;
+ unsigned int sync_ep_pipe;
+ u8 *xfer_buf;
+ u8 intf_num;
+ u8 pcm_card_num;
+ u8 pcm_dev_num;
+ u8 direction;
+ bool in_use;
+};
+
+struct uaudio_qmi_dev {
+ struct device *dev;
+ u32 sid;
+ u32 intr_num;
+ struct xhci_interrupter *ir;
+ struct xhci_ring *sec_ring;
+ struct iommu_domain *domain;
+
+ /* list to keep track of available iova */
+ struct list_head xfer_ring_list;
+ size_t xfer_ring_iova_size;
+ unsigned long curr_xfer_ring_iova;
+ struct list_head xfer_buf_list;
+ size_t xfer_buf_iova_size;
+ unsigned long curr_xfer_buf_iova;
+
+ /* bit fields representing pcm card enabled */
+ unsigned long card_slot;
+ /* indicate event ring mapped or not */
+ bool er_mapped;
+ /* reference count to number of possible consumers */
+ atomic_t qdev_in_use;
+ /* idx to last udev card number plugged in */
+ unsigned int last_card_num;
+};
+
+struct uaudio_dev {
+ struct usb_device *udev;
+ /* audio control interface */
+ struct usb_host_interface *ctrl_intf;
+ unsigned int usb_core_id;
+ atomic_t in_use;
+ struct kref kref;
+ wait_queue_head_t disconnect_wq;
+
+ /* interface specific */
+ int num_intf;
+ struct intf_info *info;
+ struct snd_usb_audio *chip;
+
+ /* xhci sideband */
+ struct xhci_sideband *sb;
+};
+
+static struct uaudio_dev uadev[SNDRV_CARDS];
+static struct uaudio_qmi_dev *uaudio_qdev;
+static struct uaudio_qmi_svc *uaudio_svc;
+static DEFINE_MUTEX(qdev_mutex);
+
+struct uaudio_qmi_svc {
+ struct qmi_handle *uaudio_svc_hdl;
+ struct work_struct qmi_disconnect_work;
+ struct workqueue_struct *uaudio_wq;
+ struct sockaddr_qrtr client_sq;
+ bool client_connected;
+};
+
+enum mem_type {
+ MEM_EVENT_RING,
+ MEM_XFER_RING,
+ MEM_XFER_BUF,
+};
+
+/* Supported audio formats */
+enum usb_qmi_audio_format {
+ USB_QMI_PCM_FORMAT_S8 = 0,
+ USB_QMI_PCM_FORMAT_U8,
+ USB_QMI_PCM_FORMAT_S16_LE,
+ USB_QMI_PCM_FORMAT_S16_BE,
+ USB_QMI_PCM_FORMAT_U16_LE,
+ USB_QMI_PCM_FORMAT_U16_BE,
+ USB_QMI_PCM_FORMAT_S24_LE,
+ USB_QMI_PCM_FORMAT_S24_BE,
+ USB_QMI_PCM_FORMAT_U24_LE,
+ USB_QMI_PCM_FORMAT_U24_BE,
+ USB_QMI_PCM_FORMAT_S24_3LE,
+ USB_QMI_PCM_FORMAT_S24_3BE,
+ USB_QMI_PCM_FORMAT_U24_3LE,
+ USB_QMI_PCM_FORMAT_U24_3BE,
+ USB_QMI_PCM_FORMAT_S32_LE,
+ USB_QMI_PCM_FORMAT_S32_BE,
+ USB_QMI_PCM_FORMAT_U32_LE,
+ USB_QMI_PCM_FORMAT_U32_BE,
+};
+
+static enum usb_qmi_audio_device_speed_enum_v01
+get_speed_info(enum usb_device_speed udev_speed)
+{
+ switch (udev_speed) {
+ case USB_SPEED_LOW:
+ return USB_QMI_DEVICE_SPEED_LOW_V01;
+ case USB_SPEED_FULL:
+ return USB_QMI_DEVICE_SPEED_FULL_V01;
+ case USB_SPEED_HIGH:
+ return USB_QMI_DEVICE_SPEED_HIGH_V01;
+ case USB_SPEED_SUPER:
+ return USB_QMI_DEVICE_SPEED_SUPER_V01;
+ case USB_SPEED_SUPER_PLUS:
+ return USB_QMI_DEVICE_SPEED_SUPER_PLUS_V01;
+ default:
+ return USB_QMI_DEVICE_SPEED_INVALID_V01;
+ }
+}
+
+static struct snd_usb_substream *find_substream(unsigned int card_num,
+ unsigned int pcm_idx, unsigned int direction)
+{
+ struct snd_usb_stream *as;
+ struct snd_usb_substream *subs = NULL;
+ struct snd_usb_audio *chip;
+
+ chip = uadev[card_num].chip;
+ if (!chip || atomic_read(&chip->shutdown))
+ goto done;
+
+ if (pcm_idx >= chip->pcm_devs)
+ goto done;
+
+ if (direction > SNDRV_PCM_STREAM_CAPTURE)
+ goto done;
+
+ list_for_each_entry(as, &chip->pcm_list, list) {
+ if (as->pcm_index == pcm_idx) {
+ subs = &as->substream[direction];
+ goto done;
+ }
+ }
+
+done:
+ return subs;
+}
+
+static int info_idx_from_ifnum(int card_num, int intf_num, bool enable)
+{
+ int i;
+
+ /*
+ * default index 0 is used when info is allocated upon
+ * first enable audio stream req for a pcm device
+ */
+ if (enable && !uadev[card_num].info)
+ return 0;
+
+ for (i = 0; i < uadev[card_num].num_intf; i++) {
+ if (enable && !uadev[card_num].info[i].in_use)
+ return i;
+ else if (!enable &&
+ uadev[card_num].info[i].intf_num == intf_num)
+ return i;
+ }
+
+ return -EINVAL;
+}
+
+static int get_data_interval_from_si(struct snd_usb_substream *subs,
+ u32 service_interval)
+{
+ unsigned int bus_intval, bus_intval_mult, binterval;
+
+ if (subs->dev->speed >= USB_SPEED_HIGH)
+ bus_intval = BUS_INTERVAL_HIGHSPEED_AND_ABOVE;
+ else
+ bus_intval = BUS_INTERVAL_FULL_SPEED;
+
+ if (service_interval % bus_intval)
+ return -EINVAL;
+
+ bus_intval_mult = service_interval / bus_intval;
+ binterval = ffs(bus_intval_mult);
+ if (!binterval || binterval > MAX_BINTERVAL_ISOC_EP)
+ return -EINVAL;
+
+ /* check if another bit is set then bail out */
+ bus_intval_mult = bus_intval_mult >> binterval;
+ if (bus_intval_mult)
+ return -EINVAL;
+
+ return (binterval - 1);
+}
+
+/* maps audio format received over QMI to asound.h based pcm format */
+static snd_pcm_format_t map_pcm_format(enum usb_qmi_audio_format fmt_received)
+{
+ switch (fmt_received) {
+ case USB_QMI_PCM_FORMAT_S8:
+ return SNDRV_PCM_FORMAT_S8;
+ case USB_QMI_PCM_FORMAT_U8:
+ return SNDRV_PCM_FORMAT_U8;
+ case USB_QMI_PCM_FORMAT_S16_LE:
+ return SNDRV_PCM_FORMAT_S16_LE;
+ case USB_QMI_PCM_FORMAT_S16_BE:
+ return SNDRV_PCM_FORMAT_S16_BE;
+ case USB_QMI_PCM_FORMAT_U16_LE:
+ return SNDRV_PCM_FORMAT_U16_LE;
+ case USB_QMI_PCM_FORMAT_U16_BE:
+ return SNDRV_PCM_FORMAT_U16_BE;
+ case USB_QMI_PCM_FORMAT_S24_LE:
+ return SNDRV_PCM_FORMAT_S24_LE;
+ case USB_QMI_PCM_FORMAT_S24_BE:
+ return SNDRV_PCM_FORMAT_S24_BE;
+ case USB_QMI_PCM_FORMAT_U24_LE:
+ return SNDRV_PCM_FORMAT_U24_LE;
+ case USB_QMI_PCM_FORMAT_U24_BE:
+ return SNDRV_PCM_FORMAT_U24_BE;
+ case USB_QMI_PCM_FORMAT_S24_3LE:
+ return SNDRV_PCM_FORMAT_S24_3LE;
+ case USB_QMI_PCM_FORMAT_S24_3BE:
+ return SNDRV_PCM_FORMAT_S24_3BE;
+ case USB_QMI_PCM_FORMAT_U24_3LE:
+ return SNDRV_PCM_FORMAT_U24_3LE;
+ case USB_QMI_PCM_FORMAT_U24_3BE:
+ return SNDRV_PCM_FORMAT_U24_3BE;
+ case USB_QMI_PCM_FORMAT_S32_LE:
+ return SNDRV_PCM_FORMAT_S32_LE;
+ case USB_QMI_PCM_FORMAT_S32_BE:
+ return SNDRV_PCM_FORMAT_S32_BE;
+ case USB_QMI_PCM_FORMAT_U32_LE:
+ return SNDRV_PCM_FORMAT_U32_LE;
+ case USB_QMI_PCM_FORMAT_U32_BE:
+ return SNDRV_PCM_FORMAT_U32_BE;
+ default:
+ /*
+ * We expect the caller to do input validation so we should
+ * never hit this. But we do have to return a proper
+ * snd_pcm_format_t value due to the __bitwise attribute; so
+ * just return the equivalent of 0 in case of bad input.
+ */
+ return SNDRV_PCM_FORMAT_S8;
+ }
+}
+
+/* Offloading IOMMU management */
+static unsigned long uaudio_get_iova(unsigned long *curr_iova,
+ size_t *curr_iova_size, struct list_head *head, size_t size)
+{
+ struct iova_info *info, *new_info = NULL;
+ struct list_head *curr_head;
+ unsigned long va = 0;
+ size_t tmp_size = size;
+ bool found = false;
+
+ if (size % PAGE_SIZE) {
+ dev_err(uaudio_qdev->dev, "size %zu is not page size multiple\n",
+ size);
+ goto done;
+ }
+
+ if (size > *curr_iova_size) {
+ dev_err(uaudio_qdev->dev, "size %zu > curr size %zu\n",
+ size, *curr_iova_size);
+ goto done;
+ }
+ if (*curr_iova_size == 0) {
+ dev_err(uaudio_qdev->dev, "iova mapping is full\n");
+ goto done;
+ }
+
+ list_for_each_entry(info, head, list) {
+ /* exact size iova_info */
+ if (!info->in_use && info->size == size) {
+ info->in_use = true;
+ va = info->start_iova;
+ *curr_iova_size -= size;
+ found = true;
+ dev_dbg(uaudio_qdev->dev, "exact size: %zu found\n", size);
+ goto done;
+ } else if (!info->in_use && tmp_size >= info->size) {
+ if (!new_info)
+ new_info = info;
+ dev_dbg(uaudio_qdev->dev, "partial size: %zu found\n",
+ info->size);
+ tmp_size -= info->size;
+ if (tmp_size)
+ continue;
+
+ va = new_info->start_iova;
+ for (curr_head = &new_info->list; curr_head !=
+ &info->list; curr_head = curr_head->next) {
+ new_info = list_entry(curr_head, struct
+ iova_info, list);
+ new_info->in_use = true;
+ }
+ info->in_use = true;
+ *curr_iova_size -= size;
+ found = true;
+ goto done;
+ } else {
+ /* iova region in use */
+ new_info = NULL;
+ tmp_size = size;
+ }
+ }
+
+ info = kzalloc(sizeof(struct iova_info), GFP_KERNEL);
+ if (!info) {
+ va = 0;
+ goto done;
+ }
+
+ va = info->start_iova = *curr_iova;
+ info->size = size;
+ info->in_use = true;
+ *curr_iova += size;
+ *curr_iova_size -= size;
+ found = true;
+ list_add_tail(&info->list, head);
+
+done:
+ if (!found)
+ dev_err(uaudio_qdev->dev, "unable to find %zu size iova\n",
+ size);
+ else
+ dev_dbg(uaudio_qdev->dev,
+ "va:0x%08lx curr_iova:0x%08lx curr_iova_size:%zu\n",
+ va, *curr_iova, *curr_iova_size);
+
+ return va;
+}
+
+static void uaudio_put_iova(unsigned long va, size_t size, struct list_head
+ *head, size_t *curr_iova_size)
+{
+ struct iova_info *info;
+ size_t tmp_size = size;
+ bool found = false;
+
+ list_for_each_entry(info, head, list) {
+ if (info->start_iova == va) {
+ if (!info->in_use) {
+ dev_err(uaudio_qdev->dev, "va %lu is not in use\n",
+ va);
+ return;
+ }
+ found = true;
+ info->in_use = false;
+ if (info->size == size)
+ goto done;
+ }
+
+ if (found && tmp_size >= info->size) {
+ info->in_use = false;
+ tmp_size -= info->size;
+ if (!tmp_size)
+ goto done;
+ }
+ }
+
+ if (!found) {
+ dev_err(uaudio_qdev->dev, "unable to find the va %lu\n", va);
+ return;
+ }
+done:
+ *curr_iova_size += size;
+ dev_dbg(uaudio_qdev->dev, "curr_iova_size %zu\n", *curr_iova_size);
+}
+
+/**
+ * uaudio_iommu_unmap() - unmaps iommu memory for adsp
+ * @mtype: ring type
+ * @va: virtual address to unmap
+ * @iova_size: region size
+ * @mapped_iova_size: mapped region size
+ *
+ * Unmaps the memory region that was previously assigned to the adsp.
+ *
+ */
+static void uaudio_iommu_unmap(enum mem_type mtype, unsigned long va,
+ size_t iova_size, size_t mapped_iova_size)
+{
+ size_t umap_size;
+ bool unmap = true;
+
+ if (!va || !iova_size)
+ return;
+
+ switch (mtype) {
+ case MEM_EVENT_RING:
+ if (uaudio_qdev->er_mapped)
+ uaudio_qdev->er_mapped = false;
+ else
+ unmap = false;
+ break;
+
+ case MEM_XFER_RING:
+ uaudio_put_iova(va, iova_size, &uaudio_qdev->xfer_ring_list,
+ &uaudio_qdev->xfer_ring_iova_size);
+ break;
+ case MEM_XFER_BUF:
+ uaudio_put_iova(va, iova_size, &uaudio_qdev->xfer_buf_list,
+ &uaudio_qdev->xfer_buf_iova_size);
+ break;
+ default:
+ dev_err(uaudio_qdev->dev, "unknown mem type %d\n", mtype);
+ unmap = false;
+ }
+
+ if (!unmap || !mapped_iova_size)
+ return;
+
+ dev_dbg(uaudio_qdev->dev, "type %d: unmap iova 0x%08lx size %zu\n",
+ mtype, va, mapped_iova_size);
+
+ umap_size = iommu_unmap(uaudio_qdev->domain, va, mapped_iova_size);
+ if (umap_size != mapped_iova_size)
+ dev_err(uaudio_qdev->dev,
+ "unmapped size %zu for iova 0x%08lx of mapped size %zu\n",
+ umap_size, va, mapped_iova_size);
+}
+
+/**
+ * uaudio_iommu_map() - maps iommu memory for adsp
+ * @mtype: ring type
+ * @dma_coherent: dma coherent
+ * @pa: physical address for ring/buffer
+ * @size: size of memory region
+ * @sgt: sg table for memory region
+ *
+ * Maps the XHCI related resources to a memory region that is assigned to be
+ * used by the adsp. This will be mapped to the domain, which is created by
+ * the ASoC USB backend driver.
+ *
+ */
+static unsigned long uaudio_iommu_map(enum mem_type mtype, bool dma_coherent,
+ phys_addr_t pa, size_t size, struct sg_table *sgt)
+{
+ unsigned long va_sg, va = 0;
+ bool map = true;
+ int i, ret;
+ size_t sg_len, total_len = 0;
+ struct scatterlist *sg;
+ phys_addr_t pa_sg;
+ int prot = IOMMU_READ | IOMMU_WRITE;
+
+ if (dma_coherent)
+ prot |= IOMMU_CACHE;
+
+ switch (mtype) {
+ case MEM_EVENT_RING:
+ va = IOVA_BASE;
+ /* er already mapped */
+ if (uaudio_qdev->er_mapped)
+ map = false;
+ break;
+ case MEM_XFER_RING:
+ va = uaudio_get_iova(&uaudio_qdev->curr_xfer_ring_iova,
+ &uaudio_qdev->xfer_ring_iova_size, &uaudio_qdev->xfer_ring_list,
+ size);
+ break;
+ case MEM_XFER_BUF:
+ va = uaudio_get_iova(&uaudio_qdev->curr_xfer_buf_iova,
+ &uaudio_qdev->xfer_buf_iova_size, &uaudio_qdev->xfer_buf_list,
+ size);
+ break;
+ default:
+ dev_err(uaudio_qdev->dev, "unknown mem type %d\n", mtype);
+ }
+
+ if (!va || !map)
+ goto done;
+
+ if (!sgt)
+ goto skip_sgt_map;
+
+ va_sg = va;
+ for_each_sg(sgt->sgl, sg, sgt->nents, i) {
+ sg_len = PAGE_ALIGN(sg->offset + sg->length);
+ pa_sg = page_to_phys(sg_page(sg));
+ ret = iommu_map(uaudio_qdev->domain, va_sg, pa_sg, sg_len,
+ prot, GFP_KERNEL);
+ if (ret) {
+ dev_err(uaudio_qdev->dev, "mapping failed ret%d\n", ret);
+ dev_err(uaudio_qdev->dev,
+ "type:%d, pa:%pa iova:0x%08lx sg_len:%zu\n",
+ mtype, &pa_sg, va_sg, sg_len);
+ uaudio_iommu_unmap(MEM_XFER_BUF, va, size, total_len);
+ va = 0;
+ goto done;
+ }
+ dev_dbg(uaudio_qdev->dev,
+ "type:%d map pa:%pa to iova:0x%08lx len:%zu offset:%u\n",
+ mtype, &pa_sg, va_sg, sg_len, sg->offset);
+ va_sg += sg_len;
+ total_len += sg_len;
+ }
+
+ if (size != total_len) {
+ dev_err(uaudio_qdev->dev, "iova size %zu != mapped iova size %zu\n",
+ size, total_len);
+ uaudio_iommu_unmap(MEM_XFER_BUF, va, size, total_len);
+ va = 0;
+ }
+ return va;
+
+skip_sgt_map:
+ dev_dbg(uaudio_qdev->dev, "type:%d map pa:%pa to iova:0x%08lx size:%zu\n",
+ mtype, &pa, va, size);
+
+ ret = iommu_map(uaudio_qdev->domain, va, pa, size, prot, GFP_KERNEL);
+ if (ret)
+ dev_err(uaudio_qdev->dev,
+ "failed to map pa:%pa iova:0x%lx type:%d ret:%d\n",
+ &pa, va, mtype, ret);
+done:
+ return va;
+}
+
+/* looks up alias, if any, for controller DT node and returns the index */
+static int usb_get_controller_id(struct usb_device *udev)
+{
+ if (udev->bus->sysdev && udev->bus->sysdev->of_node)
+ return of_alias_get_id(udev->bus->sysdev->of_node, "usb");
+
+ return -ENODEV;
+}
+
+/**
+ * uaudio_dev_intf_cleanup() - cleanup transfer resources
+ * @udev: usb device
+ * @info: usb offloading interface
+ *
+ * Cleans up the transfer ring related resources which are assigned per
+ * endpoint from XHCI. This is invoked when the USB endpoints are no
+ * longer in use by the adsp.
+ *
+ */
+static void uaudio_dev_intf_cleanup(struct usb_device *udev,
+ struct intf_info *info)
+{
+ uaudio_iommu_unmap(MEM_XFER_RING, info->data_xfer_ring_va,
+ info->data_xfer_ring_size, info->data_xfer_ring_size);
+ info->data_xfer_ring_va = 0;
+ info->data_xfer_ring_size = 0;
+
+ uaudio_iommu_unmap(MEM_XFER_RING, info->sync_xfer_ring_va,
+ info->sync_xfer_ring_size, info->sync_xfer_ring_size);
+ info->sync_xfer_ring_va = 0;
+ info->sync_xfer_ring_size = 0;
+
+ uaudio_iommu_unmap(MEM_XFER_BUF, info->xfer_buf_va,
+ info->xfer_buf_size, info->xfer_buf_size);
+ info->xfer_buf_va = 0;
+
+ usb_free_coherent(udev, info->xfer_buf_size,
+ info->xfer_buf, info->xfer_buf_pa);
+ info->xfer_buf_size = 0;
+ info->xfer_buf = NULL;
+ info->xfer_buf_pa = 0;
+
+ info->in_use = false;
+}
+
+/**
+ * uaudio_event_ring_cleanup_free() - cleanup secondary event ring
+ * @dev: usb offload device
+ *
+ * Cleans up the secondary event ring that was requested. This will
+ * occur when the adsp is no longer transferring data on the USB bus
+ * across all endpoints.
+ *
+ */
+static void uaudio_event_ring_cleanup_free(struct uaudio_dev *dev)
+{
+ struct usb_hcd *hcd = bus_to_hcd(dev->udev->bus);
+
+ clear_bit(dev->chip->card->number, &uaudio_qdev->card_slot);
+ /* all audio devices are disconnected */
+ if (!uaudio_qdev->card_slot) {
+ uaudio_iommu_unmap(MEM_EVENT_RING, IOVA_BASE, PAGE_SIZE,
+ PAGE_SIZE);
+ xhci_remove_secondary_interrupter(hcd, uaudio_qdev->ir);
+ uaudio_qdev->ir = NULL;
+ }
+}
+
+static void uaudio_dev_cleanup(struct uaudio_dev *dev)
+{
+ int if_idx;
+
+ if (!dev->udev)
+ return;
+
+ /* free xfer buffer and unmap xfer ring and buf per interface */
+ for (if_idx = 0; if_idx < dev->num_intf; if_idx++) {
+ if (!dev->info[if_idx].in_use)
+ continue;
+ uaudio_dev_intf_cleanup(dev->udev, &dev->info[if_idx]);
+ dev_dbg(uaudio_qdev->dev, "release resources: intf# %d card# %d\n",
+ dev->info[if_idx].intf_num, dev->chip->card->number);
+ }
+
+ dev->num_intf = 0;
+
+ /* free interface info */
+ kfree(dev->info);
+ dev->info = NULL;
+ uaudio_event_ring_cleanup_free(dev);
+ dev->udev = NULL;
+}
+
+/**
+ * disable_audio_stream() - disable usb snd endpoints
+ * @subs: usb substream
+ *
+ * Closes the USB SND endpoints associated with the current audio stream
+ * used. This will decrement the USB SND endpoint opened reference count.
+ *
+ */
+static void disable_audio_stream(struct snd_usb_substream *subs)
+{
+ struct snd_usb_audio *chip = subs->stream->chip;
+
+ if (subs->data_endpoint || subs->sync_endpoint) {
+ close_endpoints(chip, subs);
+
+ mutex_lock(&chip->mutex);
+ subs->cur_audiofmt = NULL;
+ mutex_unlock(&chip->mutex);
+ }
+
+ snd_usb_autosuspend(chip);
+}
+
+/* QMI service disconnect handlers */
+static void qmi_disconnect_work(struct work_struct *w)
+{
+ struct intf_info *info;
+ int idx, if_idx;
+ struct snd_usb_substream *subs;
+ struct snd_usb_audio *chip;
+
+ /* find all active intf for set alt 0 and cleanup usb audio dev */
+ for (idx = 0; idx < SNDRV_CARDS; idx++) {
+ if (!atomic_read(&uadev[idx].in_use))
+ continue;
+
+ chip = uadev[idx].chip;
+ for (if_idx = 0; if_idx < uadev[idx].num_intf; if_idx++) {
+ if (!uadev[idx].info || !uadev[idx].info[if_idx].in_use)
+ continue;
+ info = &uadev[idx].info[if_idx];
+ subs = find_substream(info->pcm_card_num,
+ info->pcm_dev_num,
+ info->direction);
+ if (!subs || !chip || atomic_read(&chip->shutdown)) {
+ dev_err(&subs->dev->dev,
+ "no sub for c#%u dev#%u dir%u\n",
+ info->pcm_card_num,
+ info->pcm_dev_num,
+ info->direction);
+ continue;
+ }
+ disable_audio_stream(subs);
+ }
+ atomic_set(&uadev[idx].in_use, 0);
+ mutex_lock(&chip->mutex);
+ uaudio_dev_cleanup(&uadev[idx]);
+ mutex_unlock(&chip->mutex);
+ }
+}
+
+/**
+ * qmi_bye_cb() - qmi bye message callback
+ * @handle: QMI handle
+ * @node: id of the dying node
+ *
+ * This callback is invoked when the QMI bye control message is received
+ * from the QMI client. Handle the message accordingly by ensuring that
+ * the USB offload path is disabled and cleaned up. At this point, ADSP
+ * is not utilizing the USB bus.
+ *
+ */
+static void qmi_bye_cb(struct qmi_handle *handle, unsigned int node)
+{
+ struct uaudio_qmi_svc *svc = uaudio_svc;
+
+ if (svc->uaudio_svc_hdl != handle)
+ return;
+
+ if (svc->client_connected && svc->client_sq.sq_node == node) {
+ queue_work(svc->uaudio_wq, &svc->qmi_disconnect_work);
+ svc->client_sq.sq_node = 0;
+ svc->client_sq.sq_port = 0;
+ svc->client_sq.sq_family = 0;
+ svc->client_connected = false;
+ }
+}
+
+/**
+ * qmi_svc_disconnect_cb() - qmi client disconnected
+ * @handle: QMI handle
+ * @node: id of the dying node
+ * @port: port of the dying client
+ *
+ * Invoked when the remote QMI client is disconnected. Handle this event
+ * the same way as when the QMI bye message is received. This will ensure
+ * the USB offloading path is disabled and cleaned up.
+ *
+ */
+static void qmi_svc_disconnect_cb(struct qmi_handle *handle,
+ unsigned int node, unsigned int port)
+{
+ struct uaudio_qmi_svc *svc;
+
+ if (uaudio_svc == NULL)
+ return;
+
+ svc = uaudio_svc;
+ if (svc->uaudio_svc_hdl != handle)
+ return;
+
+ if (svc->client_connected && svc->client_sq.sq_node == node &&
+ svc->client_sq.sq_port == port) {
+ queue_work(svc->uaudio_wq, &svc->qmi_disconnect_work);
+ svc->client_sq.sq_node = 0;
+ svc->client_sq.sq_port = 0;
+ svc->client_sq.sq_family = 0;
+ svc->client_connected = false;
+ }
+}
+
+/* QMI client callback handlers from QMI interface */
+static struct qmi_ops uaudio_svc_ops_options = {
+ .bye = qmi_bye_cb,
+ .del_client = qmi_svc_disconnect_cb,
+};
+
+/* kref release callback when all streams are disabled */
+static void uaudio_dev_release(struct kref *kref)
+{
+ struct uaudio_dev *dev = container_of(kref, struct uaudio_dev, kref);
+
+ uaudio_event_ring_cleanup_free(dev);
+ atomic_set(&dev->in_use, 0);
+ wake_up(&dev->disconnect_wq);
+}
+
+/**
+ * enable_audio_stream() - enable usb snd endpoints
+ * @subs: usb substream
+ * @pcm_format: pcm format requested
+ * @channels: number of channels
+ * @cur_rate: sample rate
+ * @datainterval: interval
+ *
+ * Opens all USB SND endpoints used for the data interface. This will increment
+ * the USB SND endpoint's opened count. Requests to keep the interface resumed
+ * until the audio stream is stopped. Will issue the USB set interface control
+ * message to enable the data interface.
+ *
+ */
+static int enable_audio_stream(struct snd_usb_substream *subs,
+ snd_pcm_format_t pcm_format,
+ unsigned int channels, unsigned int cur_rate,
+ int datainterval)
+{
+ struct snd_usb_audio *chip = subs->stream->chip;
+ struct snd_pcm_hw_params params;
+ struct snd_mask *m;
+ struct snd_interval *i;
+ const struct audioformat *fmt;
+ int ret;
+ bool fixed_rate;
+
+ _snd_pcm_hw_params_any(&params);
+
+ m = hw_param_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT);
+ snd_mask_leave(m, pcm_format);
+
+ i = hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS);
+ snd_interval_setinteger(i);
+
+ i = hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE);
+ snd_interval_setinteger(i);
+ i->min = i->max = cur_rate;
+
+ pm_runtime_barrier(&chip->intf[0]->dev);
+ snd_usb_autoresume(chip);
+
+ fmt = find_format(&subs->fmt_list, pcm_format, cur_rate,
+ channels, datainterval, subs);
+ if (!fmt) {
+ dev_err(uaudio_qdev->dev,
+ "cannot find format: format = %#x, rate = %d, ch = %d\n",
+ pcm_format, cur_rate, channels);
+ return -EINVAL;
+ }
+
+ if (atomic_read(&chip->shutdown)) {
+ dev_err(uaudio_qdev->dev, "chip already shutdown\n");
+ ret = -ENODEV;
+ } else {
+ if (subs->data_endpoint)
+ close_endpoints(chip, subs);
+
+ fixed_rate = snd_usb_pcm_has_fixed_rate(subs);
+ subs->data_endpoint = snd_usb_endpoint_open(chip, fmt,
+ &params, false, fixed_rate);
+ if (!subs->data_endpoint) {
+ dev_err(uaudio_qdev->dev, "failed to open data endpoint\n");
+ return -EINVAL;
+ }
+
+ if (fmt->sync_ep) {
+ subs->sync_endpoint = snd_usb_endpoint_open(chip,
+ fmt, &params, true, fixed_rate);
+ if (!subs->sync_endpoint) {
+ dev_err(uaudio_qdev->dev,
+ "failed to open sync endpoint\n");
+ return -EINVAL;
+ }
+
+ subs->data_endpoint->sync_source = subs->sync_endpoint;
+ }
+
+ mutex_lock(&chip->mutex);
+ subs->cur_audiofmt = fmt;
+ mutex_unlock(&chip->mutex);
+
+ if (subs->sync_endpoint) {
+ ret = snd_usb_endpoint_prepare(chip, subs->sync_endpoint);
+ if (ret < 0)
+ return ret;
+ }
+
+ ret = snd_usb_endpoint_prepare(chip, subs->data_endpoint);
+ if (ret < 0)
+ return ret;
+
+ dev_dbg(uaudio_qdev->dev,
+ "selected %s iface:%d altsetting:%d datainterval:%dus\n",
+ subs->direction ? "capture" : "playback",
+ fmt->iface, fmt->altsetting,
+ (1 << fmt->datainterval) *
+ (subs->dev->speed >= USB_SPEED_HIGH ?
+ BUS_INTERVAL_HIGHSPEED_AND_ABOVE :
+ BUS_INTERVAL_FULL_SPEED));
+ }
+
+ return 0;
+}
+
+/* returns usb hcd sysdev */
+static struct device *usb_get_usb_backend(struct usb_device *udev)
+{
+ if (udev->bus->sysdev && udev->bus->sysdev->of_node)
+ return udev->bus->sysdev;
+
+ return NULL;
+}
+
+/**
+ * prepare_qmi_response() - prepare stream enable response
+ * @subs: usb substream
+ * @req_msg: QMI request message
+ * @resp: QMI response buffer
+ * @info_idx: usb interface array index
+ *
+ * Prepares the QMI response for a USB QMI stream enable request. Will parse
+ * out the parameters within the stream enable request, in order to match
+ * requested audio profile to the ones exposed by the USB device connected.
+ *
+ * In addition, will fetch the XHCI transfer resources needed for the handoff to
+ * happen. This includes, transfer ring and buffer addresses and secondary event
+ * ring address. These parameters will be communicated as part of the USB QMI
+ * stream enable response.
+ *
+ */
+static int prepare_qmi_response(struct snd_usb_substream *subs,
+ struct qmi_uaudio_stream_req_msg_v01 *req_msg,
+ struct qmi_uaudio_stream_resp_msg_v01 *resp, int info_idx)
+{
+ struct usb_interface *iface;
+ struct usb_host_interface *alts;
+ struct usb_interface_descriptor *altsd;
+ struct usb_interface_assoc_descriptor *assoc;
+ struct usb_host_endpoint *ep;
+ struct uac_format_type_i_continuous_descriptor *fmt;
+ struct uac_format_type_i_discrete_descriptor *fmt_v1;
+ struct uac_format_type_i_ext_descriptor *fmt_v2;
+ struct uac1_as_header_descriptor *as;
+ struct xhci_interrupter *ir;
+ struct q6usb_offload *data;
+ struct usb_hcd *hcd;
+ int ret;
+ int protocol, card_num, pcm_dev_num;
+ void *hdr_ptr;
+ u8 *xfer_buf;
+ unsigned int data_ep_pipe = 0, sync_ep_pipe = 0;
+ u32 len, mult, remainder, xfer_buf_len;
+ unsigned long va, tr_data_va = 0, tr_sync_va = 0;
+ phys_addr_t xhci_pa, xfer_buf_pa, tr_data_pa = 0, tr_sync_pa = 0;
+ struct sg_table sgt;
+ struct page *pg;
+ bool dma_coherent;
+
+ iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
+ if (!iface) {
+ dev_err(uaudio_qdev->dev, "interface # %d does not exist\n",
+ subs->cur_audiofmt->iface);
+ ret = -ENODEV;
+ goto err;
+ }
+
+ assoc = iface->intf_assoc;
+ pcm_dev_num = (req_msg->usb_token & QMI_STREAM_REQ_DEV_NUM_MASK) >> 8;
+ xfer_buf_len = req_msg->xfer_buff_size;
+ card_num = uaudio_qdev->last_card_num;
+
+ alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
+ altsd = get_iface_desc(alts);
+ protocol = altsd->bInterfaceProtocol;
+
+ /* get format type */
+ if (protocol != UAC_VERSION_3) {
+ fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
+ UAC_FORMAT_TYPE);
+ if (!fmt) {
+ dev_err(uaudio_qdev->dev,
+ "%u:%d : no UAC_FORMAT_TYPE desc\n",
+ subs->cur_audiofmt->iface,
+ subs->cur_audiofmt->altset_idx);
+ ret = -ENODEV;
+ goto err;
+ }
+ }
+
+ if (!uadev[card_num].ctrl_intf) {
+ dev_err(uaudio_qdev->dev, "audio ctrl intf info not cached\n");
+ ret = -ENODEV;
+ goto err;
+ }
+
+ if (protocol != UAC_VERSION_3) {
+ hdr_ptr = snd_usb_find_csint_desc(uadev[card_num].ctrl_intf->extra,
+ uadev[card_num].ctrl_intf->extralen, NULL,
+ UAC_HEADER);
+ if (!hdr_ptr) {
+ dev_err(uaudio_qdev->dev, "no UAC_HEADER desc\n");
+ ret = -ENODEV;
+ goto err;
+ }
+ }
+
+ if (protocol == UAC_VERSION_1) {
+ struct uac1_ac_header_descriptor *uac1_hdr = hdr_ptr;
+
+ as = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL,
+ UAC_AS_GENERAL);
+ if (!as) {
+ dev_err(uaudio_qdev->dev,
+ "%u:%d : no UAC_AS_GENERAL desc\n",
+ subs->cur_audiofmt->iface,
+ subs->cur_audiofmt->altset_idx);
+ ret = -ENODEV;
+ goto err;
+ }
+ resp->data_path_delay = as->bDelay;
+ resp->data_path_delay_valid = 1;
+ fmt_v1 = (struct uac_format_type_i_discrete_descriptor *)fmt;
+ resp->usb_audio_subslot_size = fmt_v1->bSubframeSize;
+ resp->usb_audio_subslot_size_valid = 1;
+
+ resp->usb_audio_spec_revision = le16_to_cpu(uac1_hdr->bcdADC);
+ resp->usb_audio_spec_revision_valid = 1;
+ } else if (protocol == UAC_VERSION_2) {
+ struct uac2_ac_header_descriptor *uac2_hdr = hdr_ptr;
+
+ fmt_v2 = (struct uac_format_type_i_ext_descriptor *)fmt;
+ resp->usb_audio_subslot_size = fmt_v2->bSubslotSize;
+ resp->usb_audio_subslot_size_valid = 1;
+
+ resp->usb_audio_spec_revision = le16_to_cpu(uac2_hdr->bcdADC);
+ resp->usb_audio_spec_revision_valid = 1;
+ } else if (protocol == UAC_VERSION_3) {
+ if (assoc->bFunctionSubClass ==
+ UAC3_FUNCTION_SUBCLASS_FULL_ADC_3_0) {
+ dev_err(uaudio_qdev->dev, "full adc is not supported\n");
+ ret = -EINVAL;
+ }
+
+ switch (le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize)) {
+ case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
+ case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
+ case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
+ case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16: {
+ resp->usb_audio_subslot_size = 0x2;
+ break;
+ }
+
+ case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
+ case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
+ case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
+ case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24: {
+ resp->usb_audio_subslot_size = 0x3;
+ break;
+ }
+
+ default:
+ dev_err(uaudio_qdev->dev,
+ "%d: %u: Invalid wMaxPacketSize\n",
+ subs->cur_audiofmt->iface,
+ subs->cur_audiofmt->altset_idx);
+ ret = -EINVAL;
+ goto err;
+ }
+ resp->usb_audio_subslot_size_valid = 1;
+ } else {
+ dev_err(uaudio_qdev->dev, "unknown protocol version %x\n",
+ protocol);
+ ret = -ENODEV;
+ goto err;
+ }
+
+ resp->slot_id = subs->dev->slot_id;
+ resp->slot_id_valid = 1;
+
+ memcpy(&resp->std_as_opr_intf_desc, &alts->desc, sizeof(alts->desc));
+ resp->std_as_opr_intf_desc_valid = 1;
+
+ ep = usb_pipe_endpoint(subs->dev, subs->data_endpoint->pipe);
+ if (!ep) {
+ dev_err(uaudio_qdev->dev, "data ep # %d context is null\n",
+ subs->data_endpoint->ep_num);
+ ret = -ENODEV;
+ goto err;
+ }
+ data_ep_pipe = subs->data_endpoint->pipe;
+ memcpy(&resp->std_as_data_ep_desc, &ep->desc, sizeof(ep->desc));
+ resp->std_as_data_ep_desc_valid = 1;
+
+ ret = xhci_sideband_add_endpoint(uadev[card_num].sb, ep);
+ if (ret < 0) {
+ dev_err(uaudio_qdev->dev, "failed to get data ep ring address\n");
+ ret = -ENODEV;
+ goto err;
+ }
+
+ pg = sg_page(uadev[card_num].sb->sgt->sgl);
+ tr_data_pa = page_to_phys(pg);
+ resp->xhci_mem_info.tr_data.pa = uadev[card_num].sb->ring->first_seg->dma;
+
+ if (subs->sync_endpoint) {
+ ep = usb_pipe_endpoint(subs->dev, subs->sync_endpoint->pipe);
+ if (!ep) {
+ dev_err(uaudio_qdev->dev, "implicit fb on data ep\n");
+ goto skip_sync_ep;
+ }
+ sync_ep_pipe = subs->sync_endpoint->pipe;
+ memcpy(&resp->std_as_sync_ep_desc, &ep->desc, sizeof(ep->desc));
+ resp->std_as_sync_ep_desc_valid = 1;
+
+ ret = xhci_sideband_add_endpoint(uadev[card_num].sb, ep);
+ if (ret < 0) {
+ dev_err(uaudio_qdev->dev,
+ "failed to get sync ep ring address\n");
+ ret = -ENODEV;
+ goto drop_data_ep;
+ }
+
+ pg = sg_page(uadev[card_num].sb->sgt->sgl);
+ tr_sync_pa = page_to_phys(pg);
+ resp->xhci_mem_info.tr_sync.pa = uadev[card_num].sb->ring->first_seg->dma;
+ }
+
+skip_sync_ep:
+ data = snd_soc_usb_get_priv_data(usb_get_usb_backend(subs->dev));
+ if (!data)
+ goto drop_sync_ep;
+
+ uaudio_qdev->domain = data->domain;
+ uaudio_qdev->sid = data->sid;
+ uaudio_qdev->intr_num = data->intr_num;
+ uaudio_qdev->dev = data->dev;
+
+ resp->interrupter_num_valid = 1;
+ resp->controller_num_valid = 0;
+ ret = usb_get_controller_id(subs->dev);
+ if (ret >= 0) {
+ resp->controller_num = ret;
+ resp->controller_num_valid = 1;
+ }
+ /* map xhci data structures PA memory to iova */
+ dma_coherent = dev_is_dma_coherent(subs->dev->bus->sysdev);
+
+ /* event ring */
+ hcd = bus_to_hcd(subs->dev->bus);
+ ir = xhci_create_secondary_interrupter(hcd, uaudio_qdev->intr_num);
+ if (!ir) {
+ dev_err(uaudio_qdev->dev, "failed to fetch interrupter\n");
+ ret = -ENODEV;
+ goto drop_sync_ep;
+ }
+
+ dma_get_sgtable(subs->dev->bus->sysdev, &sgt, ir->event_ring->first_seg->trbs,
+ ir->event_ring->first_seg->dma, TRB_SEGMENT_SIZE);
+ xhci_pa = page_to_phys(sg_page(sgt.sgl));
+ if (!xhci_pa) {
+ dev_err(uaudio_qdev->dev,
+ "failed to get sec event ring address\n");
+ ret = -ENODEV;
+ goto free_sec_ring;
+ }
+ sg_free_table(&sgt);
+
+ uaudio_qdev->ir = ir;
+ resp->interrupter_num = ir->intr_num;
+
+ va = uaudio_iommu_map(MEM_EVENT_RING, dma_coherent, xhci_pa, PAGE_SIZE,
+ NULL);
+ if (!va) {
+ ret = -ENOMEM;
+ goto free_sec_ring;
+ }
+
+ resp->xhci_mem_info.evt_ring.va = PREPEND_SID_TO_IOVA(va,
+ uaudio_qdev->sid);
+ resp->xhci_mem_info.evt_ring.pa = ir->event_ring->first_seg->dma;
+ resp->xhci_mem_info.evt_ring.size = PAGE_SIZE;
+ uaudio_qdev->er_mapped = true;
+
+ resp->speed_info = get_speed_info(subs->dev->speed);
+ if (resp->speed_info == USB_QMI_DEVICE_SPEED_INVALID_V01) {
+ ret = -ENODEV;
+ goto unmap_er;
+ }
+
+ resp->speed_info_valid = 1;
+
+ /* data transfer ring */
+ va = uaudio_iommu_map(MEM_XFER_RING, dma_coherent, tr_data_pa,
+ PAGE_SIZE, NULL);
+ if (!va) {
+ ret = -ENOMEM;
+ goto unmap_er;
+ }
+
+ tr_data_va = va;
+ resp->xhci_mem_info.tr_data.va = PREPEND_SID_TO_IOVA(va,
+ uaudio_qdev->sid);
+ resp->xhci_mem_info.tr_data.size = PAGE_SIZE;
+
+ /* sync transfer ring */
+ if (!resp->xhci_mem_info.tr_sync.pa)
+ goto skip_sync;
+
+ xhci_pa = resp->xhci_mem_info.tr_sync.pa;
+ va = uaudio_iommu_map(MEM_XFER_RING, dma_coherent, tr_sync_pa,
+ PAGE_SIZE, NULL);
+ if (!va) {
+ ret = -ENOMEM;
+ goto unmap_data;
+ }
+
+ tr_sync_va = va;
+ resp->xhci_mem_info.tr_sync.va = PREPEND_SID_TO_IOVA(va,
+ uaudio_qdev->sid);
+ resp->xhci_mem_info.tr_sync.size = PAGE_SIZE;
+
+skip_sync:
+ /* xfer buffer, multiple of 4K only */
+ if (!xfer_buf_len)
+ xfer_buf_len = PAGE_SIZE;
+
+ mult = xfer_buf_len / PAGE_SIZE;
+ remainder = xfer_buf_len % PAGE_SIZE;
+ len = mult * PAGE_SIZE;
+ len += remainder ? PAGE_SIZE : 0;
+
+ if (len > MAX_XFER_BUFF_LEN) {
+ dev_err(uaudio_qdev->dev,
+ "req buf len %d > max buf len %lu, setting %lu\n",
+ len, MAX_XFER_BUFF_LEN, MAX_XFER_BUFF_LEN);
+ len = MAX_XFER_BUFF_LEN;
+ }
+
+ xfer_buf = usb_alloc_coherent(subs->dev, len, GFP_KERNEL, &xfer_buf_pa);
+ if (!xfer_buf) {
+ ret = -ENOMEM;
+ goto unmap_sync;
+ }
+
+ dma_get_sgtable(subs->dev->bus->sysdev, &sgt, xfer_buf, xfer_buf_pa,
+ len);
+ va = uaudio_iommu_map(MEM_XFER_BUF, dma_coherent, xfer_buf_pa, len,
+ &sgt);
+ if (!va) {
+ ret = -ENOMEM;
+ goto unmap_sync;
+ }
+
+ resp->xhci_mem_info.xfer_buff.pa = xfer_buf_pa;
+ resp->xhci_mem_info.xfer_buff.size = len;
+
+ resp->xhci_mem_info.xfer_buff.va = PREPEND_SID_TO_IOVA(va,
+ uaudio_qdev->sid);
+
+ resp->xhci_mem_info_valid = 1;
+
+ sg_free_table(&sgt);
+
+ if (!atomic_read(&uadev[card_num].in_use)) {
+ kref_init(&uadev[card_num].kref);
+ init_waitqueue_head(&uadev[card_num].disconnect_wq);
+ uadev[card_num].num_intf =
+ subs->dev->config->desc.bNumInterfaces;
+ uadev[card_num].info = kcalloc(uadev[card_num].num_intf,
+ sizeof(struct intf_info), GFP_KERNEL);
+ if (!uadev[card_num].info) {
+ ret = -ENOMEM;
+ goto unmap_sync;
+ }
+ uadev[card_num].udev = subs->dev;
+ atomic_set(&uadev[card_num].in_use, 1);
+ } else {
+ kref_get(&uadev[card_num].kref);
+ }
+
+ //uadev[card_num].card_num = card_num;
+ uadev[card_num].usb_core_id = resp->controller_num;
+
+ /* cache intf specific info to use it for unmap and free xfer buf */
+ uadev[card_num].info[info_idx].data_xfer_ring_va = tr_data_va;
+ uadev[card_num].info[info_idx].data_xfer_ring_size = PAGE_SIZE;
+ uadev[card_num].info[info_idx].sync_xfer_ring_va = tr_sync_va;
+ uadev[card_num].info[info_idx].sync_xfer_ring_size = PAGE_SIZE;
+ uadev[card_num].info[info_idx].xfer_buf_va = va;
+ uadev[card_num].info[info_idx].xfer_buf_pa = xfer_buf_pa;
+ uadev[card_num].info[info_idx].xfer_buf_size = len;
+ uadev[card_num].info[info_idx].data_ep_pipe = data_ep_pipe;
+ uadev[card_num].info[info_idx].sync_ep_pipe = sync_ep_pipe;
+ uadev[card_num].info[info_idx].xfer_buf = xfer_buf;
+ uadev[card_num].info[info_idx].pcm_card_num = card_num;
+ uadev[card_num].info[info_idx].pcm_dev_num = pcm_dev_num;
+ uadev[card_num].info[info_idx].direction = subs->direction;
+ uadev[card_num].info[info_idx].intf_num = subs->cur_audiofmt->iface;
+ uadev[card_num].info[info_idx].in_use = true;
+
+ set_bit(card_num, &uaudio_qdev->card_slot);
+
+ return 0;
+
+unmap_sync:
+ usb_free_coherent(subs->dev, len, xfer_buf, xfer_buf_pa);
+ uaudio_iommu_unmap(MEM_XFER_RING, tr_sync_va, PAGE_SIZE, PAGE_SIZE);
+unmap_data:
+ uaudio_iommu_unmap(MEM_XFER_RING, tr_data_va, PAGE_SIZE, PAGE_SIZE);
+unmap_er:
+ uaudio_iommu_unmap(MEM_EVENT_RING, IOVA_BASE, PAGE_SIZE, PAGE_SIZE);
+free_sec_ring:
+ xhci_remove_secondary_interrupter(hcd, uaudio_qdev->ir);
+drop_sync_ep:
+ if (subs->sync_endpoint)
+ xhci_sideband_remove_endpoint(uadev[card_num].sb,
+ usb_pipe_endpoint(subs->dev, subs->sync_endpoint->pipe));
+drop_data_ep:
+ xhci_sideband_remove_endpoint(uadev[card_num].sb,
+ usb_pipe_endpoint(subs->dev, subs->data_endpoint->pipe));
+
+err:
+ return ret;
+}
+
+/**
+ * handle_uaudio_stream_req() - handle stream enable/disable request
+ * @handle: QMI client handle
+ * @sq: qrtr socket
+ * @txn: QMI transaction context
+ * @decoded_msg: decoded QMI message
+ *
+ * Main handler for the QMI stream enable/disable requests. This executes the
+ * corresponding enable/disable stream apis, respectively.
+ *
+ */
+static void handle_uaudio_stream_req(struct qmi_handle *handle,
+ struct sockaddr_qrtr *sq,
+ struct qmi_txn *txn,
+ const void *decoded_msg)
+{
+ struct qmi_uaudio_stream_req_msg_v01 *req_msg;
+ struct qmi_uaudio_stream_resp_msg_v01 resp = {{0}, 0};
+ struct snd_usb_substream *subs;
+ struct snd_usb_audio *chip = NULL;
+ struct uaudio_qmi_svc *svc = uaudio_svc;
+ struct intf_info *info;
+ struct usb_host_endpoint *ep;
+ u8 pcm_card_num, pcm_dev_num, direction;
+ int info_idx = -EINVAL, datainterval = -EINVAL, ret = 0;
+
+ if (!svc->client_connected) {
+ svc->client_sq = *sq;
+ svc->client_connected = true;
+ }
+
+ mutex_lock(&qdev_mutex);
+ req_msg = (struct qmi_uaudio_stream_req_msg_v01 *)decoded_msg;
+ if (!req_msg->audio_format_valid || !req_msg->bit_rate_valid ||
+ !req_msg->number_of_ch_valid || !req_msg->xfer_buff_size_valid) {
+ ret = -EINVAL;
+ goto response;
+ }
+
+ if (!uaudio_qdev) {
+ ret = -EINVAL;
+ goto response;
+ }
+
+ direction = (req_msg->usb_token & QMI_STREAM_REQ_DIRECTION);
+ pcm_dev_num = (req_msg->usb_token & QMI_STREAM_REQ_DEV_NUM_MASK) >> 8;
+ pcm_card_num = req_msg->enable ? uaudio_qdev->last_card_num :
+ ffs(uaudio_qdev->card_slot) - 1;
+ if (pcm_card_num >= SNDRV_CARDS) {
+ ret = -EINVAL;
+ goto response;
+ }
+
+ if (req_msg->audio_format > USB_QMI_PCM_FORMAT_U32_BE) {
+ ret = -EINVAL;
+ goto response;
+ }
+
+ subs = find_substream(pcm_card_num, pcm_dev_num, direction);
+ chip = uadev[pcm_card_num].chip;
+ if (!subs || !chip || atomic_read(&chip->shutdown)) {
+ ret = -ENODEV;
+ goto response;
+ }
+
+ info_idx = info_idx_from_ifnum(pcm_card_num, subs->cur_audiofmt ?
+ subs->cur_audiofmt->iface : -1, req_msg->enable);
+ if (atomic_read(&chip->shutdown) || !subs->stream || !subs->stream->pcm
+ || !subs->stream->chip) {
+ ret = -ENODEV;
+ goto response;
+ }
+
+ if (req_msg->enable) {
+ if (info_idx < 0 || chip->system_suspend) {
+ ret = -EBUSY;
+ goto response;
+ }
+ }
+
+ if (req_msg->service_interval_valid) {
+ ret = get_data_interval_from_si(subs,
+ req_msg->service_interval);
+ if (ret == -EINVAL)
+ goto response;
+
+ datainterval = ret;
+ }
+
+ uadev[pcm_card_num].ctrl_intf = chip->ctrl_intf;
+
+ if (req_msg->enable) {
+ ret = enable_audio_stream(subs,
+ map_pcm_format(req_msg->audio_format),
+ req_msg->number_of_ch, req_msg->bit_rate,
+ datainterval);
+
+ if (!ret)
+ ret = prepare_qmi_response(subs, req_msg, &resp,
+ info_idx);
+ } else {
+ info = &uadev[pcm_card_num].info[info_idx];
+ if (info->data_ep_pipe) {
+ ep = usb_pipe_endpoint(uadev[pcm_card_num].udev,
+ info->data_ep_pipe);
+ if (ep)
+ xhci_sideband_stop_endpoint(uadev[pcm_card_num].sb,
+ ep);
+ xhci_sideband_remove_endpoint(uadev[pcm_card_num].sb, ep);
+ info->data_ep_pipe = 0;
+ }
+
+ if (info->sync_ep_pipe) {
+ ep = usb_pipe_endpoint(uadev[pcm_card_num].udev,
+ info->sync_ep_pipe);
+ if (ep)
+ xhci_sideband_stop_endpoint(uadev[pcm_card_num].sb,
+ ep);
+ xhci_sideband_remove_endpoint(uadev[pcm_card_num].sb, ep);
+ info->sync_ep_pipe = 0;
+ }
+
+ disable_audio_stream(subs);
+ }
+
+response:
+ if (!req_msg->enable && ret != -EINVAL && ret != -ENODEV) {
+ mutex_lock(&chip->mutex);
+ if (info_idx >= 0) {
+ info = &uadev[pcm_card_num].info[info_idx];
+ uaudio_dev_intf_cleanup(
+ uadev[pcm_card_num].udev,
+ info);
+ }
+ if (atomic_read(&uadev[pcm_card_num].in_use))
+ kref_put(&uadev[pcm_card_num].kref,
+ uaudio_dev_release);
+ mutex_unlock(&chip->mutex);
+ }
+ mutex_unlock(&qdev_mutex);
+
+ resp.usb_token = req_msg->usb_token;
+ resp.usb_token_valid = 1;
+ resp.internal_status = ret;
+ resp.internal_status_valid = 1;
+ resp.status = ret ? USB_QMI_STREAM_REQ_FAILURE_V01 : ret;
+ resp.status_valid = 1;
+ ret = qmi_send_response(svc->uaudio_svc_hdl, sq, txn,
+ QMI_UAUDIO_STREAM_RESP_V01,
+ QMI_UAUDIO_STREAM_RESP_MSG_V01_MAX_MSG_LEN,
+ qmi_uaudio_stream_resp_msg_v01_ei, &resp);
+}
+
+static struct qmi_msg_handler uaudio_stream_req_handlers = {
+ .type = QMI_REQUEST,
+ .msg_id = QMI_UAUDIO_STREAM_REQ_V01,
+ .ei = qmi_uaudio_stream_req_msg_v01_ei,
+ .decoded_size = QMI_UAUDIO_STREAM_REQ_MSG_V01_MAX_MSG_LEN,
+ .fn = handle_uaudio_stream_req,
+};
+
+/**
+ * qc_usb_audio_offload_init_qmi_dev() - initializes qmi dev
+ *
+ * Initializes the USB qdev, which is used to carry information pertaining to
+ * the offloading resources. This device is freed only when there are no longer
+ * any offloading candidates. (i.e, when all audio devices are disconnected)
+ *
+ */
+static int qc_usb_audio_offload_init_qmi_dev(struct usb_device *udev)
+{
+ uaudio_qdev = kzalloc(sizeof(struct uaudio_qmi_dev),
+ GFP_KERNEL);
+ if (!uaudio_qdev)
+ return -ENOMEM;
+
+ /* initialize xfer ring and xfer buf iova list */
+ INIT_LIST_HEAD(&uaudio_qdev->xfer_ring_list);
+ uaudio_qdev->curr_xfer_ring_iova = IOVA_XFER_RING_BASE;
+ uaudio_qdev->xfer_ring_iova_size =
+ IOVA_XFER_RING_MAX - IOVA_XFER_RING_BASE;
+
+ INIT_LIST_HEAD(&uaudio_qdev->xfer_buf_list);
+ uaudio_qdev->curr_xfer_buf_iova = IOVA_XFER_BUF_BASE;
+ uaudio_qdev->xfer_buf_iova_size =
+ IOVA_XFER_BUF_MAX - IOVA_XFER_BUF_BASE;
+
+ return 0;
+}
+
+/**
+ * qc_usb_audio_offload_probe() - platform op connect handler
+ * @chip: USB SND device
+ *
+ * Platform connect handler when a USB SND device is detected. Will
+ * notify SOC USB about the connection to enable the USB ASoC backend
+ * and populate internal USB chip array.
+ *
+ */
+static void qc_usb_audio_offload_probe(struct snd_usb_audio *chip)
+{
+ struct usb_device *udev = chip->dev;
+ struct xhci_sideband *sb;
+
+ /*
+ * If there is no priv_data, the connected device is on a USB bus
+ * that doesn't support offloading. Avoid populating entries for
+ * this device.
+ */
+ if (!snd_soc_usb_get_priv_data(usb_get_usb_backend(udev)))
+ return;
+
+ sb = xhci_sideband_register(udev);
+ if (!sb)
+ return;
+
+ mutex_lock(&chip->mutex);
+ uadev[chip->card->number].sb = sb;
+ uadev[chip->card->number].chip = chip;
+ uaudio_qdev->last_card_num = chip->card->number;
+ mutex_unlock(&chip->mutex);
+
+ mutex_lock(&qdev_mutex);
+ if (!uaudio_qdev)
+ qc_usb_audio_offload_init_qmi_dev(udev);
+
+ atomic_inc(&uaudio_qdev->qdev_in_use);
+ mutex_unlock(&qdev_mutex);
+
+ snd_soc_usb_connect(usb_get_usb_backend(udev), chip->index);
+}
+
+/**
+ * qc_usb_audio_cleanup_qmi_dev() - release qmi device
+ *
+ * Frees the USB qdev. Only occurs when there are no longer any potential
+ * devices that can utilize USB audio offloading.
+ *
+ */
+static void qc_usb_audio_cleanup_qmi_dev(void)
+{
+ kfree(uaudio_qdev);
+ uaudio_qdev = NULL;
+}
+
+/**
+ * qc_usb_audio_offload_disconnect() - platform op disconnect handler
+ * @chip: USB SND device
+ *
+ * Platform disconnect handler. Will ensure that any pending stream is
+ * halted by issuing a QMI disconnect indication packet to the adsp.
+ *
+ */
+static void qc_usb_audio_offload_disconnect(struct snd_usb_audio *chip)
+{
+ struct usb_device *udev = chip->dev;
+ struct qmi_uaudio_stream_ind_msg_v01 disconnect_ind = {0};
+ struct uaudio_qmi_svc *svc = uaudio_svc;
+ struct uaudio_dev *dev;
+ int card_num;
+ int ret;
+
+ if (!chip)
+ return;
+
+ card_num = chip->card->number;
+ if (card_num >= SNDRV_CARDS)
+ return;
+
+ mutex_lock(&chip->mutex);
+ dev = &uadev[card_num];
+
+ /* Device has already been cleaned up, or never populated */
+ if (!dev->chip)
+ return;
+
+ /* clean up */
+ if (!dev->udev)
+ goto done;
+
+ if (atomic_read(&dev->in_use)) {
+ mutex_unlock(&chip->mutex);
+ dev_dbg(uaudio_qdev->dev, "sending qmi indication disconnect\n");
+ disconnect_ind.dev_event = USB_QMI_DEV_DISCONNECT_V01;
+ disconnect_ind.slot_id = dev->udev->slot_id;
+ disconnect_ind.controller_num = dev->usb_core_id;
+ disconnect_ind.controller_num_valid = 1;
+ ret = qmi_send_indication(svc->uaudio_svc_hdl, &svc->client_sq,
+ QMI_UAUDIO_STREAM_IND_V01,
+ QMI_UAUDIO_STREAM_IND_MSG_V01_MAX_MSG_LEN,
+ qmi_uaudio_stream_ind_msg_v01_ei,
+ &disconnect_ind);
+ if (ret < 0)
+ dev_err(uaudio_qdev->dev,
+ "qmi send failed with err: %d\n", ret);
+
+ ret = wait_event_interruptible_timeout(dev->disconnect_wq,
+ !atomic_read(&dev->in_use),
+ msecs_to_jiffies(DEV_RELEASE_WAIT_TIMEOUT));
+ if (!ret) {
+ dev_err(uaudio_qdev->dev,
+ "timeout while waiting for dev_release\n");
+ atomic_set(&dev->in_use, 0);
+ } else if (ret < 0) {
+ dev_err(uaudio_qdev->dev, "failed with ret %d\n", ret);
+ atomic_set(&dev->in_use, 0);
+ }
+ mutex_lock(&chip->mutex);
+ }
+
+ uaudio_dev_cleanup(dev);
+done:
+ dev->chip = NULL;
+ mutex_unlock(&chip->mutex);
+
+ mutex_lock(&qdev_mutex);
+ atomic_dec(&uaudio_qdev->qdev_in_use);
+ if (!atomic_read(&uaudio_qdev->qdev_in_use)) {
+ snd_soc_usb_disconnect(usb_get_usb_backend(udev));
+ qc_usb_audio_cleanup_qmi_dev();
+ }
+ mutex_unlock(&qdev_mutex);
+
+ xhci_sideband_unregister(dev->sb);
+}
+
+/**
+ * qc_usb_audio_offload_suspend() - USB offload PM suspend handler
+ * @intf: USB interface
+ * @message: suspend type
+ *
+ * PM suspend handler to ensure that the USB offloading driver is able to stop
+ * any pending traffic, so that the bus can be suspended.
+ *
+ */
+static void qc_usb_audio_offload_suspend(struct usb_interface *intf, pm_message_t message)
+{
+ struct snd_usb_audio *chip = usb_get_intfdata(intf);
+ struct qmi_uaudio_stream_ind_msg_v01 disconnect_ind = {0};
+ struct uaudio_qmi_svc *svc = uaudio_svc;
+ struct uaudio_dev *dev;
+ int card_num;
+ int ret;
+
+ if (!chip)
+ return;
+
+ card_num = chip->card->number;
+ if (card_num >= SNDRV_CARDS)
+ return;
+
+
+ mutex_lock(&chip->mutex);
+ dev = &uadev[card_num];
+
+ if (atomic_read(&dev->in_use)) {
+ mutex_unlock(&chip->mutex);
+ dev_dbg(uaudio_qdev->dev, "sending qmi indication suspend\n");
+ disconnect_ind.dev_event = USB_QMI_DEV_DISCONNECT_V01;
+ disconnect_ind.slot_id = dev->udev->slot_id;
+ disconnect_ind.controller_num = dev->usb_core_id;
+ disconnect_ind.controller_num_valid = 1;
+ ret = qmi_send_indication(svc->uaudio_svc_hdl, &svc->client_sq,
+ QMI_UAUDIO_STREAM_IND_V01,
+ QMI_UAUDIO_STREAM_IND_MSG_V01_MAX_MSG_LEN,
+ qmi_uaudio_stream_ind_msg_v01_ei,
+ &disconnect_ind);
+ if (ret < 0)
+ dev_err(uaudio_qdev->dev,
+ "qmi send failed with err: %d\n", ret);
+
+ ret = wait_event_interruptible_timeout(dev->disconnect_wq,
+ !atomic_read(&dev->in_use),
+ msecs_to_jiffies(DEV_RELEASE_WAIT_TIMEOUT));
+ if (!ret) {
+ dev_err(uaudio_qdev->dev,
+ "timeout while waiting for dev_release\n");
+ atomic_set(&dev->in_use, 0);
+ } else if (ret < 0) {
+ dev_err(uaudio_qdev->dev, "failed with ret %d\n", ret);
+ atomic_set(&dev->in_use, 0);
+ }
+ mutex_lock(&chip->mutex);
+ }
+ mutex_unlock(&chip->mutex);
+}
+
+static struct snd_usb_platform_ops offload_ops = {
+ .connect_cb = qc_usb_audio_offload_probe,
+ .disconnect_cb = qc_usb_audio_offload_disconnect,
+ .suspend_cb = qc_usb_audio_offload_suspend,
+};
+
+static int __init qc_usb_audio_offload_init(void)
+{
+ struct uaudio_qmi_svc *svc;
+ int ret;
+
+ ret = snd_usb_register_platform_ops(&offload_ops);
+ if (ret < 0)
+ return ret;
+
+ svc = kzalloc(sizeof(struct uaudio_qmi_svc), GFP_KERNEL);
+ if (!svc) {
+ ret = -ENOMEM;
+ goto unreg_ops;
+ }
+
+ svc->uaudio_wq = create_singlethread_workqueue("uaudio_svc");
+ if (!svc->uaudio_wq) {
+ ret = -ENOMEM;
+ goto free_svc;
+ }
+
+ svc->uaudio_svc_hdl = kzalloc(sizeof(struct qmi_handle), GFP_KERNEL);
+ if (!svc->uaudio_svc_hdl) {
+ ret = -ENOMEM;
+ goto free_wq;
+ }
+
+ ret = qmi_handle_init(svc->uaudio_svc_hdl,
+ QMI_UAUDIO_STREAM_REQ_MSG_V01_MAX_MSG_LEN,
+ &uaudio_svc_ops_options,
+ &uaudio_stream_req_handlers);
+ ret = qmi_add_server(svc->uaudio_svc_hdl, UAUDIO_STREAM_SERVICE_ID_V01,
+ UAUDIO_STREAM_SERVICE_VERS_V01, 0);
+
+ INIT_WORK(&svc->qmi_disconnect_work, qmi_disconnect_work);
+ uaudio_svc = svc;
+
+ return 0;
+
+free_wq:
+ destroy_workqueue(svc->uaudio_wq);
+free_svc:
+ kfree(svc);
+unreg_ops:
+ snd_usb_unregister_platform_ops();
+
+ return ret;
+}
+
+static void __exit qc_usb_audio_offload_exit(void)
+{
+ struct uaudio_qmi_svc *svc = uaudio_svc;
+
+ qmi_handle_release(svc->uaudio_svc_hdl);
+ flush_workqueue(svc->uaudio_wq);
+ destroy_workqueue(svc->uaudio_wq);
+ kfree(svc);
+ uaudio_svc = NULL;
+ snd_usb_unregister_platform_ops();
+}
+
+module_init(qc_usb_audio_offload_init);
+module_exit(qc_usb_audio_offload_exit);
+
+MODULE_DESCRIPTION("QC USB Audio Offloading");
+MODULE_LICENSE("GPL");

2023-07-25 04:03:04

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 01/32] xhci: add support to allocate several interrupters

From: Mathias Nyman <[email protected]>

Modify the XHCI drivers to accommodate for handling multiple event rings in
case there are multiple interrupters. Add the required APIs so clients are
able to allocate/request for an interrupter ring, and pass this information
back to the client driver. This allows for users to handle the resource
accordingly, such as passing the event ring base address to an audio DSP.
There is no actual support for multiple MSI/MSI-X vectors.

Factoring out XHCI interrupter APIs and structures done by Wesley Cheng, in
order to allow for USB class drivers to utilze them.

Signed-off-by: Mathias Nyman <[email protected]>
Co-developed-by: Wesley Cheng <[email protected]>
Signed-off-by: Wesley Cheng <[email protected]>
---
arch/arm64/boot/dts/qcom/sm8350.dtsi.rej | 14 ++++
arch/arm64/configs/defconfig.rej | 24 ++++++
drivers/usb/host/xhci-debugfs.c | 2 +-
drivers/usb/host/xhci-mem.c | 93 ++++++++++++++++++++++--
drivers/usb/host/xhci-ring.c | 2 +-
drivers/usb/host/xhci.c | 49 ++++++++-----
drivers/usb/host/xhci.h | 77 +-------------------
include/linux/usb/xhci-intr.h | 86 ++++++++++++++++++++++
8 files changed, 245 insertions(+), 102 deletions(-)
create mode 100644 arch/arm64/boot/dts/qcom/sm8350.dtsi.rej
create mode 100644 arch/arm64/configs/defconfig.rej
create mode 100644 include/linux/usb/xhci-intr.h

diff --git a/arch/arm64/boot/dts/qcom/sm8350.dtsi.rej b/arch/arm64/boot/dts/qcom/sm8350.dtsi.rej
new file mode 100644
index 000000000000..e96ef1120160
--- /dev/null
+++ b/arch/arm64/boot/dts/qcom/sm8350.dtsi.rej
@@ -0,0 +1,14 @@
+--- arch/arm64/boot/dts/qcom/sm8350.dtsi
++++ arch/arm64/boot/dts/qcom/sm8350.dtsi
+@@ -2254,9 +2254,9 @@
+ iommus = <&apps_smmu 0x0 0x0>;
+ snps,dis_u2_susphy_quirk;
+ snps,dis_enblslpm_quirk;
+- phys = <&usb_1_hsphy>, <&usb_1_qmpphy QMP_USB43DP_USB3_PHY>;
++ phys = <&usb_1_hsphy>, <&usb_1_qmpphy>;
+ phy-names = "usb2-phy", "usb3-phy";
+-
++ dr_mode = "host";
+ ports {
+ #address-cells = <1>;
+ #size-cells = <0>;
diff --git a/arch/arm64/configs/defconfig.rej b/arch/arm64/configs/defconfig.rej
new file mode 100644
index 000000000000..c98a9b0e8290
--- /dev/null
+++ b/arch/arm64/configs/defconfig.rej
@@ -0,0 +1,24 @@
+--- arch/arm64/configs/defconfig
++++ arch/arm64/configs/defconfig
+@@ -241,7 +210,7 @@ CONFIG_BLK_DEV_NBD=m
+ CONFIG_VIRTIO_BLK=y
+ CONFIG_BLK_DEV_NVME=m
+ CONFIG_QCOM_COINCELL=m
+-CONFIG_QCOM_FASTRPC=m
++CONFIG_QCOM_FASTRPC=y
+ CONFIG_SRAM=y
+ CONFIG_PCI_ENDPOINT_TEST=m
+ CONFIG_EEPROM_AT24=m
+@@ -1254,10 +1204,10 @@ CONFIG_PHY_MVEBU_CP110_COMPHY=y
+ CONFIG_PHY_MTK_TPHY=y
+ CONFIG_PHY_QCOM_EDP=m
+ CONFIG_PHY_QCOM_PCIE2=m
+-CONFIG_PHY_QCOM_QMP=m
++CONFIG_PHY_QCOM_QMP=y
+ CONFIG_PHY_QCOM_QUSB2=m
+ CONFIG_PHY_QCOM_USB_HS=m
+-CONFIG_PHY_QCOM_USB_SNPS_FEMTO_V2=m
++CONFIG_PHY_QCOM_USB_SNPS_FEMTO_V2=y
+ CONFIG_PHY_QCOM_USB_HS_28NM=m
+ CONFIG_PHY_QCOM_USB_SS=m
+ CONFIG_PHY_R8A779F0_ETHERNET_SERDES=y
diff --git a/drivers/usb/host/xhci-debugfs.c b/drivers/usb/host/xhci-debugfs.c
index 99baa60ef50f..15a8402ee8a1 100644
--- a/drivers/usb/host/xhci-debugfs.c
+++ b/drivers/usb/host/xhci-debugfs.c
@@ -693,7 +693,7 @@ void xhci_debugfs_init(struct xhci_hcd *xhci)
"command-ring",
xhci->debugfs_root);

- xhci_debugfs_create_ring_dir(xhci, &xhci->interrupter->event_ring,
+ xhci_debugfs_create_ring_dir(xhci, &xhci->interrupters[0]->event_ring,
"event-ring",
xhci->debugfs_root);

diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index 19a402123de0..c51150af22f2 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -1840,6 +1840,26 @@ xhci_free_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir)
kfree(ir);
}

+void xhci_remove_secondary_interrupter(struct usb_hcd *hcd, struct xhci_interrupter *ir)
+{
+ struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+ unsigned int intr_num;
+
+ /* interrupter 0 is primary interrupter, don't touch it */
+ if (!ir || !ir->intr_num || ir->intr_num >= xhci->max_interrupters) {
+ xhci_dbg(xhci, "Invalid secondary interrupter, can't remove\n");
+ return;
+ }
+
+ /* fixme, should we check xhci->interrupter[intr_num] == ir */
+ spin_lock(&xhci->lock);
+ intr_num = ir->intr_num;
+ xhci_free_interrupter(xhci, ir);
+ xhci->interrupters[intr_num] = NULL;
+ spin_unlock(&xhci->lock);
+}
+EXPORT_SYMBOL_GPL(xhci_remove_secondary_interrupter);
+
void xhci_mem_cleanup(struct xhci_hcd *xhci)
{
struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
@@ -1847,9 +1867,13 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)

cancel_delayed_work_sync(&xhci->cmd_timer);

- xhci_free_interrupter(xhci, xhci->interrupter);
- xhci->interrupter = NULL;
- xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed primary event ring");
+ for (i = 0; i < xhci->max_interrupters; i++) {
+ if (xhci->interrupters[i]) {
+ xhci_free_interrupter(xhci, xhci->interrupters[i]);
+ xhci->interrupters[i] = NULL;
+ }
+ }
+ xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed interrupters");

if (xhci->cmd_ring)
xhci_ring_free(xhci, xhci->cmd_ring);
@@ -1919,6 +1943,7 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
for (i = 0; i < xhci->num_port_caps; i++)
kfree(xhci->port_caps[i].psi);
kfree(xhci->port_caps);
+ kfree(xhci->interrupters);
xhci->num_port_caps = 0;

xhci->usb2_rhub.ports = NULL;
@@ -1927,6 +1952,7 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
xhci->rh_bw = NULL;
xhci->ext_caps = NULL;
xhci->port_caps = NULL;
+ xhci->interrupters = NULL;

xhci->page_size = 0;
xhci->page_shift = 0;
@@ -2279,6 +2305,13 @@ xhci_add_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir,
return -EINVAL;
}

+ if (xhci->interrupters[intr_num]) {
+ xhci_warn(xhci, "Interrupter %d\n already set up", intr_num);
+ return -EINVAL;
+ }
+
+ xhci->interrupters[intr_num] = ir;
+ ir->intr_num = intr_num;
ir->ir_set = &xhci->run_regs->ir_set[intr_num];

/* set ERST count with the number of entries in the segment table */
@@ -2298,10 +2331,53 @@ xhci_add_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir,
return 0;
}

+struct xhci_interrupter *
+xhci_create_secondary_interrupter(struct usb_hcd *hcd)
+{
+ struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+ struct xhci_interrupter *ir;
+ unsigned int i;
+ int err = -ENOSPC;
+
+ if (!xhci->interrupters)
+ return NULL;
+
+ ir = xhci_alloc_interrupter(xhci, GFP_KERNEL);
+ if (!ir)
+ return NULL;
+
+ spin_lock_irq(&xhci->lock);
+
+ /* Find available secondary interrupter, interrupter 0 is reserverd for primary */
+ for (i = 1; i < xhci->max_interrupters; i++) {
+ if (xhci->interrupters[i] == NULL) {
+ err = xhci_add_interrupter(xhci, ir, i);
+ break;
+ }
+ }
+
+ spin_unlock_irq(&xhci->lock);
+
+ if (!err) {
+ xhci_dbg(xhci, "Add secondary interrupter %d, max interrupters %d\n",
+ i, xhci->max_interrupters);
+ return ir;
+ }
+
+ xhci_warn(xhci, "Failed to add secondary interrupter, max interrupters %d\n",
+ xhci->max_interrupters);
+
+ xhci_free_interrupter(xhci, ir);
+
+ return NULL;
+}
+EXPORT_SYMBOL_GPL(xhci_create_secondary_interrupter);
+
int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
{
- dma_addr_t dma;
+ struct xhci_interrupter *ir;
struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
+ dma_addr_t dma;
unsigned int val, val2;
u64 val_64;
u32 page_size, temp;
@@ -2425,11 +2501,14 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
/* Allocate and set up primary interrupter 0 with an event ring. */
xhci_dbg_trace(xhci, trace_xhci_dbg_init,
"Allocating primary event ring");
- xhci->interrupter = xhci_alloc_interrupter(xhci, flags);
- if (!xhci->interrupter)
+ xhci->interrupters = kcalloc_node(xhci->max_interrupters, sizeof(*xhci->interrupters),
+ flags, dev_to_node(dev));
+
+ ir = xhci_alloc_interrupter(xhci, flags);
+ if (!ir)
goto fail;

- if (xhci_add_interrupter(xhci, xhci->interrupter, 0))
+ if (xhci_add_interrupter(xhci, ir, 0))
goto fail;

xhci->isoc_bei_interval = AVOID_BEI_INTERVAL_MAX;
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index 646ff125def5..56f45408dc0c 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -3097,7 +3097,7 @@ irqreturn_t xhci_irq(struct usb_hcd *hcd)
writel(status, &xhci->op_regs->status);

/* This is the handler of the primary interrupter */
- ir = xhci->interrupter;
+ ir = xhci->interrupters[0];
if (!hcd->msi_enabled) {
u32 irq_pending;
irq_pending = readl(&ir->ir_set->irq_pending);
diff --git a/drivers/usb/host/xhci.c b/drivers/usb/host/xhci.c
index 5b73a7d281ed..58d75b412bad 100644
--- a/drivers/usb/host/xhci.c
+++ b/drivers/usb/host/xhci.c
@@ -456,7 +456,7 @@ static int xhci_init(struct usb_hcd *hcd)

static int xhci_run_finished(struct xhci_hcd *xhci)
{
- struct xhci_interrupter *ir = xhci->interrupter;
+ struct xhci_interrupter *ir = xhci->interrupters[0];
unsigned long flags;
u32 temp;

@@ -508,7 +508,7 @@ int xhci_run(struct usb_hcd *hcd)
u64 temp_64;
int ret;
struct xhci_hcd *xhci = hcd_to_xhci(hcd);
- struct xhci_interrupter *ir = xhci->interrupter;
+ struct xhci_interrupter *ir = xhci->interrupters[0];
/* Start the xHCI host controller running only after the USB 2.0 roothub
* is setup.
*/
@@ -572,7 +572,7 @@ void xhci_stop(struct usb_hcd *hcd)
{
u32 temp;
struct xhci_hcd *xhci = hcd_to_xhci(hcd);
- struct xhci_interrupter *ir = xhci->interrupter;
+ struct xhci_interrupter *ir = xhci->interrupters[0];

mutex_lock(&xhci->mutex);

@@ -668,36 +668,49 @@ EXPORT_SYMBOL_GPL(xhci_shutdown);
#ifdef CONFIG_PM
static void xhci_save_registers(struct xhci_hcd *xhci)
{
- struct xhci_interrupter *ir = xhci->interrupter;
+ struct xhci_interrupter *ir;
+ unsigned int i;

xhci->s3.command = readl(&xhci->op_regs->command);
xhci->s3.dev_nt = readl(&xhci->op_regs->dev_notification);
xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
xhci->s3.config_reg = readl(&xhci->op_regs->config_reg);

- if (!ir)
- return;
+ /* save both primary and all secondary interrupters */
+ for (i = 0; i < xhci->max_interrupters; i++) {
+ ir = xhci->interrupters[i];
+ if (!ir)
+ continue;

- ir->s3_erst_size = readl(&ir->ir_set->erst_size);
- ir->s3_erst_base = xhci_read_64(xhci, &ir->ir_set->erst_base);
- ir->s3_erst_dequeue = xhci_read_64(xhci, &ir->ir_set->erst_dequeue);
- ir->s3_irq_pending = readl(&ir->ir_set->irq_pending);
- ir->s3_irq_control = readl(&ir->ir_set->irq_control);
+ ir->s3_erst_size = readl(&ir->ir_set->erst_size);
+ ir->s3_erst_base = xhci_read_64(xhci, &ir->ir_set->erst_base);
+ ir->s3_erst_dequeue = xhci_read_64(xhci, &ir->ir_set->erst_dequeue);
+ ir->s3_irq_pending = readl(&ir->ir_set->irq_pending);
+ ir->s3_irq_control = readl(&ir->ir_set->irq_control);
+ }
}

static void xhci_restore_registers(struct xhci_hcd *xhci)
{
- struct xhci_interrupter *ir = xhci->interrupter;
+ struct xhci_interrupter *ir;
+ unsigned int i;

writel(xhci->s3.command, &xhci->op_regs->command);
writel(xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
writel(xhci->s3.config_reg, &xhci->op_regs->config_reg);
- writel(ir->s3_erst_size, &ir->ir_set->erst_size);
- xhci_write_64(xhci, ir->s3_erst_base, &ir->ir_set->erst_base);
- xhci_write_64(xhci, ir->s3_erst_dequeue, &ir->ir_set->erst_dequeue);
- writel(ir->s3_irq_pending, &ir->ir_set->irq_pending);
- writel(ir->s3_irq_control, &ir->ir_set->irq_control);
+
+ for (i = 0; i < xhci->max_interrupters; i++) {
+ ir = xhci->interrupters[i];
+ if (!ir)
+ continue;
+
+ writel(ir->s3_erst_size, &ir->ir_set->erst_size);
+ xhci_write_64(xhci, ir->s3_erst_base, &ir->ir_set->erst_base);
+ xhci_write_64(xhci, ir->s3_erst_dequeue, &ir->ir_set->erst_dequeue);
+ writel(ir->s3_irq_pending, &ir->ir_set->irq_pending);
+ writel(ir->s3_irq_control, &ir->ir_set->irq_control);
+ }
}

static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
@@ -1058,7 +1071,7 @@ int xhci_resume(struct xhci_hcd *xhci, pm_message_t msg)
xhci_dbg(xhci, "// Disabling event ring interrupts\n");
temp = readl(&xhci->op_regs->status);
writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status);
- xhci_disable_interrupter(xhci->interrupter);
+ xhci_disable_interrupter(xhci->interrupters[0]);

xhci_dbg(xhci, "cleaning up memory\n");
xhci_mem_cleanup(xhci);
diff --git a/drivers/usb/host/xhci.h b/drivers/usb/host/xhci.h
index 7e282b4522c0..d706a27ec0a3 100644
--- a/drivers/usb/host/xhci.h
+++ b/drivers/usb/host/xhci.h
@@ -17,6 +17,7 @@
#include <linux/kernel.h>
#include <linux/usb/hcd.h>
#include <linux/io-64-nonatomic-lo-hi.h>
+#include <linux/usb/xhci-intr.h>

/* Code sharing between pci-quirks and xhci hcd */
#include "xhci-ext-caps.h"
@@ -1541,18 +1542,6 @@ static inline const char *xhci_trb_type_string(u8 type)
#define AVOID_BEI_INTERVAL_MIN 8
#define AVOID_BEI_INTERVAL_MAX 32

-struct xhci_segment {
- union xhci_trb *trbs;
- /* private to HCD */
- struct xhci_segment *next;
- dma_addr_t dma;
- /* Max packet sized bounce buffer for td-fragmant alignment */
- dma_addr_t bounce_dma;
- void *bounce_buf;
- unsigned int bounce_offs;
- unsigned int bounce_len;
-};
-
enum xhci_cancelled_td_status {
TD_DIRTY = 0,
TD_HALTED,
@@ -1585,16 +1574,6 @@ struct xhci_cd {
union xhci_trb *cmd_trb;
};

-enum xhci_ring_type {
- TYPE_CTRL = 0,
- TYPE_ISOC,
- TYPE_BULK,
- TYPE_INTR,
- TYPE_STREAM,
- TYPE_COMMAND,
- TYPE_EVENT,
-};
-
static inline const char *xhci_ring_type_string(enum xhci_ring_type type)
{
switch (type) {
@@ -1615,46 +1594,6 @@ static inline const char *xhci_ring_type_string(enum xhci_ring_type type)
}

return "UNKNOWN";
-}
-
-struct xhci_ring {
- struct xhci_segment *first_seg;
- struct xhci_segment *last_seg;
- union xhci_trb *enqueue;
- struct xhci_segment *enq_seg;
- union xhci_trb *dequeue;
- struct xhci_segment *deq_seg;
- struct list_head td_list;
- /*
- * Write the cycle state into the TRB cycle field to give ownership of
- * the TRB to the host controller (if we are the producer), or to check
- * if we own the TRB (if we are the consumer). See section 4.9.1.
- */
- u32 cycle_state;
- unsigned int stream_id;
- unsigned int num_segs;
- unsigned int num_trbs_free; /* used only by xhci DbC */
- unsigned int bounce_buf_len;
- enum xhci_ring_type type;
- bool last_td_was_short;
- struct radix_tree_root *trb_address_map;
-};
-
-struct xhci_erst_entry {
- /* 64-bit event ring segment address */
- __le64 seg_addr;
- __le32 seg_size;
- /* Set to zero */
- __le32 rsvd;
-};
-
-struct xhci_erst {
- struct xhci_erst_entry *entries;
- unsigned int num_entries;
- /* xhci->event_ring keeps track of segment dma addresses */
- dma_addr_t erst_dma_addr;
- /* Num entries the ERST can contain */
- unsigned int erst_size;
};

struct xhci_scratchpad {
@@ -1707,18 +1646,6 @@ struct xhci_bus_state {
unsigned long resuming_ports;
};

-struct xhci_interrupter {
- struct xhci_ring *event_ring;
- struct xhci_erst erst;
- struct xhci_intr_reg __iomem *ir_set;
- unsigned int intr_num;
- /* For interrupter registers save and restore over suspend/resume */
- u32 s3_irq_pending;
- u32 s3_irq_control;
- u32 s3_erst_size;
- u64 s3_erst_base;
- u64 s3_erst_dequeue;
-};
/*
* It can take up to 20 ms to transition from RExit to U0 on the
* Intel Lynx Point LP xHCI host.
@@ -1799,7 +1726,7 @@ struct xhci_hcd {
struct reset_control *reset;
/* data structures */
struct xhci_device_context_array *dcbaa;
- struct xhci_interrupter *interrupter;
+ struct xhci_interrupter **interrupters;
struct xhci_ring *cmd_ring;
unsigned int cmd_ring_state;
#define CMD_RING_STATE_RUNNING (1 << 0)
diff --git a/include/linux/usb/xhci-intr.h b/include/linux/usb/xhci-intr.h
new file mode 100644
index 000000000000..e0091ee2c73a
--- /dev/null
+++ b/include/linux/usb/xhci-intr.h
@@ -0,0 +1,86 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __LINUX_XHCI_INTR_H
+#define __LINUX_XHCI_INTR_H
+
+#include <linux/kernel.h>
+
+struct xhci_erst_entry {
+ /* 64-bit event ring segment address */
+ __le64 seg_addr;
+ __le32 seg_size;
+ /* Set to zero */
+ __le32 rsvd;
+};
+
+enum xhci_ring_type {
+ TYPE_CTRL = 0,
+ TYPE_ISOC,
+ TYPE_BULK,
+ TYPE_INTR,
+ TYPE_STREAM,
+ TYPE_COMMAND,
+ TYPE_EVENT,
+};
+
+struct xhci_erst {
+ struct xhci_erst_entry *entries;
+ unsigned int num_entries;
+ /* xhci->event_ring keeps track of segment dma addresses */
+ dma_addr_t erst_dma_addr;
+ /* Num entries the ERST can contain */
+ unsigned int erst_size;
+};
+
+struct xhci_segment {
+ union xhci_trb *trbs;
+ /* private to HCD */
+ struct xhci_segment *next;
+ dma_addr_t dma;
+ /* Max packet sized bounce buffer for td-fragmant alignment */
+ dma_addr_t bounce_dma;
+ void *bounce_buf;
+ unsigned int bounce_offs;
+ unsigned int bounce_len;
+};
+
+struct xhci_ring {
+ struct xhci_segment *first_seg;
+ struct xhci_segment *last_seg;
+ union xhci_trb *enqueue;
+ struct xhci_segment *enq_seg;
+ union xhci_trb *dequeue;
+ struct xhci_segment *deq_seg;
+ struct list_head td_list;
+ /*
+ * Write the cycle state into the TRB cycle field to give ownership of
+ * the TRB to the host controller (if we are the producer), or to check
+ * if we own the TRB (if we are the consumer). See section 4.9.1.
+ */
+ u32 cycle_state;
+ unsigned int stream_id;
+ unsigned int num_segs;
+ unsigned int num_trbs_free;
+ unsigned int num_trbs_free_temp;
+ unsigned int bounce_buf_len;
+ enum xhci_ring_type type;
+ bool last_td_was_short;
+ struct radix_tree_root *trb_address_map;
+};
+
+struct xhci_interrupter {
+ struct xhci_ring *event_ring;
+ struct xhci_erst erst;
+ struct xhci_intr_reg __iomem *ir_set;
+ unsigned int intr_num;
+ /* For interrupter registers save and restore over suspend/resume */
+ u32 s3_irq_pending;
+ u32 s3_irq_control;
+ u32 s3_erst_size;
+ u64 s3_erst_base;
+ u64 s3_erst_dequeue;
+};
+
+struct xhci_interrupter *
+xhci_create_secondary_interrupter(struct usb_hcd *hcd);
+void xhci_remove_secondary_interrupter(struct usb_hcd *hcd, struct xhci_interrupter *ir);
+#endif

2023-07-25 04:03:10

by Wesley Cheng

[permalink] [raw]
Subject: [PATCH v4 15/32] usb: host: xhci-plat: Set XHCI max interrupters if property is present

Some platforms may want to limit the number of XHCI interrupters allocated.
This is passed to xhci-plat as a device property. Ensure that this is read
and the max_interrupters field is set.

Signed-off-by: Wesley Cheng <[email protected]>
---
drivers/usb/host/xhci-plat.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/drivers/usb/host/xhci-plat.c b/drivers/usb/host/xhci-plat.c
index e38b3c52c0aa..691248f470ee 100644
--- a/drivers/usb/host/xhci-plat.c
+++ b/drivers/usb/host/xhci-plat.c
@@ -252,6 +252,8 @@ int xhci_plat_probe(struct platform_device *pdev, struct device *sysdev, const s

device_property_read_u32(tmpdev, "imod-interval-ns",
&xhci->imod_interval);
+ device_property_read_u16(tmpdev, "num-hc-interrupters",
+ &xhci->max_interrupters);
}

hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);

2023-07-25 05:19:44

by Rob Herring

[permalink] [raw]
Subject: Re: [PATCH v4 24/32] ASoC: dt-bindings: Update example for enabling USB offload on SM8250


On Mon, 24 Jul 2023 19:34:08 -0700, Wesley Cheng wrote:
> Add an example on enabling of USB offload for the Q6DSP. The routing can
> be done by the mixer, which can pass the multimedia stream to the USB
> backend.
>
> Signed-off-by: Wesley Cheng <[email protected]>
> ---
> .../devicetree/bindings/sound/qcom,sm8250.yaml | 15 +++++++++++++++
> 1 file changed, 15 insertions(+)
>

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check'
on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:

dtschema/dtc warnings/errors:
Error: Documentation/devicetree/bindings/sound/qcom,sm8250.example.dts:97.44-45 syntax error
FATAL ERROR: Unable to parse input tree
make[2]: *** [scripts/Makefile.lib:419: Documentation/devicetree/bindings/sound/qcom,sm8250.example.dtb] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [/builds/robherring/dt-review-ci/linux/Makefile:1500: dt_binding_check] Error 2
make: *** [Makefile:234: __sub-make] Error 2

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/[email protected]

The base for the series is generally the latest rc1. A different dependency
should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above
error(s), then make sure 'yamllint' is installed and dt-schema is up to
date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note
that DT_SCHEMA_FILES can be set to your schema file to speed up checking
your schema. However, it must be unset to test all examples with your schema.


2023-07-25 05:31:27

by Trilok Soni

[permalink] [raw]
Subject: Re: [PATCH v4 12/32] sound: usb: Export USB SND APIs for modules

On 7/24/2023 7:33 PM, Wesley Cheng wrote:
> Some vendor modules will utilize useful parsing and endpoint management
> APIs to start audio playback/capture.
>
> Signed-off-by: Wesley Cheng <[email protected]>
> ---
> sound/core/oss/pcm_oss.c | 4 ++--
> sound/usb/card.c | 2 ++
> sound/usb/endpoint.c | 2 ++
> sound/usb/helper.c | 1 +
> sound/usb/pcm.c | 9 ++++++---
> sound/usb/pcm.h | 12 ++++++++++++
> 6 files changed, 25 insertions(+), 5 deletions(-)
>
> diff --git a/sound/core/oss/pcm_oss.c b/sound/core/oss/pcm_oss.c
> index 728c211142d1..7773b5362e1a 100644
> --- a/sound/core/oss/pcm_oss.c
> +++ b/sound/core/oss/pcm_oss.c
> @@ -444,8 +444,8 @@ static int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm,
> }
>
> static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
> - snd_pcm_hw_param_t var, unsigned int val,
> - int dir)
> + snd_pcm_hw_param_t var, unsigned int val,
> + int dir)
> {
> int changed;
> if (hw_is_mask(var)) {
> diff --git a/sound/usb/card.c b/sound/usb/card.c
> index 9365d1e17836..a3fad66a3337 100644
> --- a/sound/usb/card.c
> +++ b/sound/usb/card.c
> @@ -1076,6 +1076,7 @@ int snd_usb_autoresume(struct snd_usb_audio *chip)
> }
> return 0;
> }
> +EXPORT_SYMBOL_GPL(snd_usb_autoresume);
>
> void snd_usb_autosuspend(struct snd_usb_audio *chip)
> {
> @@ -1089,6 +1090,7 @@ void snd_usb_autosuspend(struct snd_usb_audio *chip)
> for (i = 0; i < chip->num_interfaces; i++)
> usb_autopm_put_interface(chip->intf[i]);
> }
> +EXPORT_SYMBOL_GPL(snd_usb_autosuspend);
>
> static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
> {
> diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c
> index a385e85c4650..0757499599c8 100644
> --- a/sound/usb/endpoint.c
> +++ b/sound/usb/endpoint.c
> @@ -866,6 +866,7 @@ snd_usb_endpoint_open(struct snd_usb_audio *chip,
> mutex_unlock(&chip->mutex);
> return ep;
> }
> +EXPORT_SYMBOL_GPL(snd_usb_endpoint_open);
>
> /*
> * snd_usb_endpoint_set_sync: Link data and sync endpoints
> @@ -1503,6 +1504,7 @@ int snd_usb_endpoint_prepare(struct snd_usb_audio *chip,
> mutex_unlock(&chip->mutex);
> return err;
> }
> +EXPORT_SYMBOL_GPL(snd_usb_endpoint_prepare);
>
> /* get the current rate set to the given clock by any endpoint */
> int snd_usb_endpoint_get_clock_rate(struct snd_usb_audio *chip, int clock)
> diff --git a/sound/usb/helper.c b/sound/usb/helper.c
> index bf80e55d013a..4322ae3738e6 100644
> --- a/sound/usb/helper.c
> +++ b/sound/usb/helper.c
> @@ -62,6 +62,7 @@ void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype
> }
> return NULL;
> }
> +EXPORT_SYMBOL_GPL(snd_usb_find_csint_desc);
>
> /*
> * Wrapper for usb_control_msg().
> diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c
> index eec5232f9fb2..b6fb6d84d9eb 100644
> --- a/sound/usb/pcm.c
> +++ b/sound/usb/pcm.c
> @@ -87,7 +87,7 @@ static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream
> /*
> * find a matching audio format
> */
> -static const struct audioformat *
> +const struct audioformat *
> find_format(struct list_head *fmt_list_head, snd_pcm_format_t format,
> unsigned int rate, unsigned int channels, bool strict_match,
> struct snd_usb_substream *subs)
> @@ -147,8 +147,9 @@ find_format(struct list_head *fmt_list_head, snd_pcm_format_t format,
> }
> return found;
> }
> +EXPORT_SYMBOL_GPL(find_format);
>
> -static const struct audioformat *
> +const struct audioformat *
> find_substream_format(struct snd_usb_substream *subs,
> const struct snd_pcm_hw_params *params)
> {
> @@ -156,6 +157,7 @@ find_substream_format(struct snd_usb_substream *subs,
> params_rate(params), params_channels(params),
> true, subs);
> }
> +EXPORT_SYMBOL_GPL(find_substream_format);
>
> bool snd_usb_pcm_has_fixed_rate(struct snd_usb_substream *subs)
> {
> @@ -446,7 +448,7 @@ int snd_usb_pcm_resume(struct snd_usb_stream *as)
> return 0;
> }
>
> -static void close_endpoints(struct snd_usb_audio *chip,
> +void close_endpoints(struct snd_usb_audio *chip,
> struct snd_usb_substream *subs)
> {
> if (subs->data_endpoint) {
> @@ -460,6 +462,7 @@ static void close_endpoints(struct snd_usb_audio *chip,
> subs->sync_endpoint = NULL;
> }
> }
> +EXPORT_SYMBOL(close_endpoints);

EXPORT_SYMBOL_GPL here as well?

--
---Trilok Soni


2023-07-25 06:05:59

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH v4 01/32] xhci: add support to allocate several interrupters

On Mon, Jul 24, 2023 at 07:33:45PM -0700, Wesley Cheng wrote:
> From: Mathias Nyman <[email protected]>
>
> Modify the XHCI drivers to accommodate for handling multiple event rings in
> case there are multiple interrupters. Add the required APIs so clients are
> able to allocate/request for an interrupter ring, and pass this information
> back to the client driver. This allows for users to handle the resource
> accordingly, such as passing the event ring base address to an audio DSP.
> There is no actual support for multiple MSI/MSI-X vectors.
>
> Factoring out XHCI interrupter APIs and structures done by Wesley Cheng, in
> order to allow for USB class drivers to utilze them.
>
> Signed-off-by: Mathias Nyman <[email protected]>
> Co-developed-by: Wesley Cheng <[email protected]>
> Signed-off-by: Wesley Cheng <[email protected]>
> ---
> arch/arm64/boot/dts/qcom/sm8350.dtsi.rej | 14 ++++
> arch/arm64/configs/defconfig.rej | 24 ++++++
> drivers/usb/host/xhci-debugfs.c | 2 +-
> drivers/usb/host/xhci-mem.c | 93 ++++++++++++++++++++++--
> drivers/usb/host/xhci-ring.c | 2 +-
> drivers/usb/host/xhci.c | 49 ++++++++-----
> drivers/usb/host/xhci.h | 77 +-------------------
> include/linux/usb/xhci-intr.h | 86 ++++++++++++++++++++++
> 8 files changed, 245 insertions(+), 102 deletions(-)
> create mode 100644 arch/arm64/boot/dts/qcom/sm8350.dtsi.rej
> create mode 100644 arch/arm64/configs/defconfig.rej
> create mode 100644 include/linux/usb/xhci-intr.h
>
> diff --git a/arch/arm64/boot/dts/qcom/sm8350.dtsi.rej b/arch/arm64/boot/dts/qcom/sm8350.dtsi.rej
> new file mode 100644
> index 000000000000..e96ef1120160
> --- /dev/null
> +++ b/arch/arm64/boot/dts/qcom/sm8350.dtsi.rej
> @@ -0,0 +1,14 @@
> +--- arch/arm64/boot/dts/qcom/sm8350.dtsi
> ++++ arch/arm64/boot/dts/qcom/sm8350.dtsi
> +@@ -2254,9 +2254,9 @@
> + iommus = <&apps_smmu 0x0 0x0>;
> + snps,dis_u2_susphy_quirk;
> + snps,dis_enblslpm_quirk;
> +- phys = <&usb_1_hsphy>, <&usb_1_qmpphy QMP_USB43DP_USB3_PHY>;
> ++ phys = <&usb_1_hsphy>, <&usb_1_qmpphy>;
> + phy-names = "usb2-phy", "usb3-phy";
> +-
> ++ dr_mode = "host";
> + ports {
> + #address-cells = <1>;
> + #size-cells = <0>;
> diff --git a/arch/arm64/configs/defconfig.rej b/arch/arm64/configs/defconfig.rej

<snip>

I think you need to look at this patch series a bit better and clean it
up before sending it out. These files obviously are not going to work,
and prevent any of this from being able to be applied or tested by any
bots or test systems :(

I've stopped here in my review of this series (except for the obvious
global symbol name comment.)

thanks,

greg k-h

2023-07-25 06:56:01

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH v4 12/32] sound: usb: Export USB SND APIs for modules

On Mon, Jul 24, 2023 at 07:33:56PM -0700, Wesley Cheng wrote:
> --- a/sound/usb/pcm.c
> +++ b/sound/usb/pcm.c
> @@ -87,7 +87,7 @@ static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream
> /*
> * find a matching audio format
> */
> -static const struct audioformat *
> +const struct audioformat *
> find_format(struct list_head *fmt_list_head, snd_pcm_format_t format,
> unsigned int rate, unsigned int channels, bool strict_match,
> struct snd_usb_substream *subs)
> @@ -147,8 +147,9 @@ find_format(struct list_head *fmt_list_head, snd_pcm_format_t format,
> }
> return found;
> }
> +EXPORT_SYMBOL_GPL(find_format);

This is a horrible symbol name for a global function, same for the other
ones in this file. If you really want to export them, please put them
in the proper "snd_" prefix namespace, or better yet, use a module
namespace as well to ensure that we know who is using them.

thanks,

greg k-h

2023-07-25 08:18:22

by Takashi Iwai

[permalink] [raw]
Subject: Re: [PATCH v4 12/32] sound: usb: Export USB SND APIs for modules

On Tue, 25 Jul 2023 07:33:57 +0200,
Greg KH wrote:
>
> On Mon, Jul 24, 2023 at 07:33:56PM -0700, Wesley Cheng wrote:
> > --- a/sound/usb/pcm.c
> > +++ b/sound/usb/pcm.c
> > @@ -87,7 +87,7 @@ static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream
> > /*
> > * find a matching audio format
> > */
> > -static const struct audioformat *
> > +const struct audioformat *
> > find_format(struct list_head *fmt_list_head, snd_pcm_format_t format,
> > unsigned int rate, unsigned int channels, bool strict_match,
> > struct snd_usb_substream *subs)
> > @@ -147,8 +147,9 @@ find_format(struct list_head *fmt_list_head, snd_pcm_format_t format,
> > }
> > return found;
> > }
> > +EXPORT_SYMBOL_GPL(find_format);
>
> This is a horrible symbol name for a global function, same for the other
> ones in this file. If you really want to export them, please put them
> in the proper "snd_" prefix namespace, or better yet, use a module
> namespace as well to ensure that we know who is using them.

Yes, please add snd_usb_ prefix for all exported symbols from
USB-audio driver code.

Also, make sure that EXPORT_SYMBOL_GPL() is used for those, too.


thanks,

Takashi

2023-07-25 08:21:58

by Takashi Iwai

[permalink] [raw]
Subject: Re: [PATCH v4 19/32] sound: usb: card: Check for support for requested audio format

On Tue, 25 Jul 2023 04:34:03 +0200,
Wesley Cheng wrote:
>
> Allow for checks on a specific USB audio device to see if a requested PCM
> format is supported. This is needed for support for when playback is
> initiated by the ASoC USB backend path.
>
> Signed-off-by: Wesley Cheng <[email protected]>
> ---
> sound/usb/card.c | 28 ++++++++++++++++++++++++++++
> sound/usb/card.h | 8 ++++++++
> 2 files changed, 36 insertions(+)
>
> diff --git a/sound/usb/card.c b/sound/usb/card.c
> index a3fad66a3337..365f6d978608 100644
> --- a/sound/usb/card.c
> +++ b/sound/usb/card.c
> @@ -142,6 +142,34 @@ int snd_usb_unregister_platform_ops(void)
> }
> EXPORT_SYMBOL_GPL(snd_usb_unregister_platform_ops);
>
> +struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
> + struct snd_pcm_hw_params *params, int direction)
> +{

Please add a comment what this function does.


> + struct snd_usb_audio *chip = usb_chip[card_idx];

Is the dereference safe without locking?
If the call is supposed to be allowed only in a safe situation, it
should be mentioned in the function description.


thanks,

Takashi

2023-07-25 08:42:16

by Takashi Iwai

[permalink] [raw]
Subject: Re: [PATCH v4 11/32] sound: usb: card: Introduce USB SND platform op callbacks

On Tue, 25 Jul 2023 04:33:55 +0200,
Wesley Cheng wrote:
>
> Allow for different platforms to be notified on USB SND connect/disconnect
> seqeunces. This allows for platform USB SND modules to properly initialize
> and populate internal structures with references to the USB SND chip
> device.
>
> Signed-off-by: Wesley Cheng <[email protected]>
> ---
> sound/usb/card.c | 36 ++++++++++++++++++++++++++++++++++++
> sound/usb/card.h | 20 ++++++++++++++++++++
> 2 files changed, 56 insertions(+)
>
> diff --git a/sound/usb/card.c b/sound/usb/card.c
> index f6e99ced8068..9365d1e17836 100644
> --- a/sound/usb/card.c
> +++ b/sound/usb/card.c
> @@ -117,6 +117,30 @@ MODULE_PARM_DESC(skip_validation, "Skip unit descriptor validation (default: no)
> static DEFINE_MUTEX(register_mutex);
> static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
> static struct usb_driver usb_audio_driver;
> +static struct snd_usb_platform_ops *platform_ops;
> +
> +int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops)
> +{
> + if (platform_ops)
> + return -EEXIST;
> + mutex_lock(&register_mutex);
> + platform_ops = ops;
> + mutex_unlock(&register_mutex);

The check should be inside register_mutex lock, too.
Otherwise it's racy.


> --- a/sound/usb/card.h
> +++ b/sound/usb/card.h
> @@ -207,4 +207,24 @@ struct snd_usb_stream {
> struct list_head list;
> };
>
> +struct snd_usb_platform_ops {
> + void (*connect_cb)(struct snd_usb_audio *chip);
> + void (*disconnect_cb)(struct snd_usb_audio *chip);
> + void (*suspend_cb)(struct usb_interface *intf, pm_message_t message);
> +};

Don't we need resume_cb? Even if it's unused for your platform, it'd
make sense to be a pair.

> +#if IS_ENABLED(CONFIG_SND_USB_AUDIO)
> +int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops);
> +int snd_usb_unregister_platform_ops(void);
> +#else
> +static int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops)
> +{
> + return -EOPNOTSUPP;
> +}
> +
> +static int snd_usb_unregister_platform_ops(void)
> +{
> + return -EOPNOTSUPP;
> +}
> +#endif /* IS_ENABLED(CONFIG_SND_USB_AUDIO) */

Any need for dummy function definitions? I thought those exported
functions are always with CONFIG_SND_USB_AUDIO enabled?

IOW, are they use of those functions without USB audio driver?


thanks,

Takashi

2023-07-25 09:26:09

by Takashi Iwai

[permalink] [raw]
Subject: Re: [PATCH v4 18/32] sound: usb: Introduce QC USB SND offloading support

On Tue, 25 Jul 2023 04:34:02 +0200,
Wesley Cheng wrote:
>
> --- a/sound/usb/Kconfig
> +++ b/sound/usb/Kconfig
> @@ -165,6 +165,21 @@ config SND_BCD2000
> To compile this driver as a module, choose M here: the module
> will be called snd-bcd2000.
>
> +config QC_USB_AUDIO_OFFLOAD
> + tristate "Qualcomm Audio Offload driver"
> + depends on QCOM_QMI_HELPERS
> + select SND_PCM

So the driver can be enabled without CONFIG_SND_USB_AUDIO? It makes
little sense without it.
Or is it set so intentionally for testing purpose?

About the code:

> +/* Offloading IOMMU management */
> +static unsigned long uaudio_get_iova(unsigned long *curr_iova,
> + size_t *curr_iova_size, struct list_head *head, size_t size)
> +{
> + struct iova_info *info, *new_info = NULL;
> + struct list_head *curr_head;
> + unsigned long va = 0;
> + size_t tmp_size = size;
> + bool found = false;
> +
> + if (size % PAGE_SIZE) {
> + dev_err(uaudio_qdev->dev, "size %zu is not page size multiple\n",
> + size);
> + goto done;

This can be easily triggered by user-space as it's passed directly
from the mmap call, and it implies that you can fill up the messages
easily. It's safer to make it debug message or add the rate limit.

Ditto for other error messages.

> +static void disable_audio_stream(struct snd_usb_substream *subs)
> +{
> + struct snd_usb_audio *chip = subs->stream->chip;
> +
> + if (subs->data_endpoint || subs->sync_endpoint) {
> + close_endpoints(chip, subs);
> +
> + mutex_lock(&chip->mutex);
> + subs->cur_audiofmt = NULL;
> + mutex_unlock(&chip->mutex);
> + }

Now looking at this and...

> +static int enable_audio_stream(struct snd_usb_substream *subs,
> + snd_pcm_format_t pcm_format,
> + unsigned int channels, unsigned int cur_rate,
> + int datainterval)
> +{

... this implementation, I wonder whether it'd be better to modify and
export snd_usb_hw_params() snd snd_usb_hw_free() to fit with qcom
driver. Then you can avoid lots of open code.

In general, if you see a direct use of chip->mutex, it can be often
done better in a different form. The use of an internal lock or such
from an external driver is always fragile and error-prone.

Also, the current open-code misses the potential race against the
disconnection during the operation. In snd-usb-audio, it protects
with snd_usb_lock_shutdown() and snd_usb_unlock_shutdown() pairs.

> +static int __init qc_usb_audio_offload_init(void)
> +{
> + struct uaudio_qmi_svc *svc;
> + int ret;
> +
> + ret = snd_usb_register_platform_ops(&offload_ops);
> + if (ret < 0)
> + return ret;

Registering the ops at the very first opens a potential access to the
uninitialized stuff. Imagine a suspend happens right after this
point. As the ops is already registered, it'll enter to the
suspend_cb callback and straight to Oops.

> +static void __exit qc_usb_audio_offload_exit(void)
> +{
> + struct uaudio_qmi_svc *svc = uaudio_svc;
> +
> + qmi_handle_release(svc->uaudio_svc_hdl);
> + flush_workqueue(svc->uaudio_wq);
> + destroy_workqueue(svc->uaudio_wq);
> + kfree(svc);
> + uaudio_svc = NULL;
> + snd_usb_unregister_platform_ops();

Similarly, the unregister order has to be careful, too.


thanks,

Takashi

2023-07-25 09:28:31

by Pierre-Louis Bossart

[permalink] [raw]
Subject: Re: [PATCH v4 26/32] sound: Pass USB SND card and PCM information to SOC USB



On 7/25/23 04:34, Wesley Cheng wrote:
> Currently, only the index to the USB SND card array is passed to the USB
> backend. Pass through more information, specifically the USB SND card
> number and the number of PCM devices available. The USB backend should
> know about which sound resources are being shared between the ASoC and USB
> SND paths. This can be utilized to properly select and maintain the
> offloading devices.
>
> Signed-off-by: Wesley Cheng <[email protected]>
> ---
> include/sound/soc-usb.h | 9 +++++----
> sound/soc/qcom/qdsp6/q6usb.c | 20 ++++++++++++++++++--
> sound/soc/soc-usb.c | 12 +++++++-----
> sound/usb/qcom/qc_audio_offload.c | 9 +++++----
> 4 files changed, 35 insertions(+), 15 deletions(-)
>
> diff --git a/include/sound/soc-usb.h b/include/sound/soc-usb.h
> index 71e6e75e600a..606128332044 100644
> --- a/include/sound/soc-usb.h
> +++ b/include/sound/soc-usb.h
> @@ -19,20 +19,21 @@ struct snd_soc_usb {
> struct device *dev;
> struct snd_soc_component *component;
> int (*connection_status_cb)(struct snd_soc_usb *usb, int card_idx,
> - int connected);
> + int chip_idx, int num_pcm, int connected);

I don't know what 'chip_idx' is.

The 'num_pcm' sounds problematic if there are different devices for
playback and capture. I would guess this is for playback only, but this
doesn't scale.

> void *priv_data;
> };

> +struct q6usb_status {
> + unsigned int num_pcm;
> + unsigned int chip_index;
> + unsigned int pcm_index;
> +};
> +
> struct q6usb_port_data {
> struct q6afe_usb_cfg usb_cfg;
> struct snd_soc_usb *usb;
> struct q6usb_offload priv;
> + unsigned long available_card_slot;

what is a card slot?

> + struct q6usb_status status[SNDRV_CARDS];
> int active_idx;
> };
>
> @@ -97,7 +105,7 @@ static int q6usb_audio_ports_of_xlate_dai_name(struct snd_soc_component *compone
> }
>
> static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
> - int connected)
> + int chip_idx, int num_pcm, int connected)
> {
> struct snd_soc_dapm_context *dapm;
> struct q6usb_port_data *data;
> @@ -109,8 +117,16 @@ static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
> snd_soc_dapm_enable_pin(dapm, "USB_RX_BE");
> /* We only track the latest USB headset plugged in */
> data->active_idx = card_idx;
> +
> + set_bit(card_idx, &data->available_card_slot);
> + data->status[card_idx].num_pcm = num_pcm;
> + data->status[card_idx].chip_index = chip_idx;
> } else {
> - snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
> + clear_bit(card_idx, &data->available_card_slot);
> + data->status[card_idx].num_pcm = 0;
> + data->status[card_idx].chip_index = 0;
> + if (!data->available_card_slot)
> + snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");

not able to follow what this does, this patch is rather unclear and
lacks comments.

> }
> snd_soc_dapm_sync(dapm);


2023-07-25 09:59:32

by Pierre-Louis Bossart

[permalink] [raw]
Subject: Re: [PATCH v4 06/32] ASoC: Add SOC USB APIs for adding an USB backend


> +/**
> + * struct snd_soc_usb
> + * @list - list head for SND SOC struct list
> + * @dev - USB backend device reference
> + * @component - reference to DAPM component

ASoC component, not DAPM.

> + * @connection_status_cb - callback to notify connection events
> + * @priv_data - driver data
> + **/
> +struct snd_soc_usb {
> + struct list_head list;
> + struct device *dev;
> + struct snd_soc_component *component;
> + int (*connection_status_cb)(struct snd_soc_usb *usb, int card_idx,
> + int connected);

It's not clear what 'connected' really refers to, is this a boolean
really or is this a "connection_event?


> + void *priv_data;
> +};
> +
> +int snd_soc_usb_connect(struct device *usbdev, int card_idx);
> +int snd_soc_usb_disconnect(struct device *usbdev);
> +void snd_soc_usb_set_priv_data(struct device *dev, void *priv);

this function is not part of this patch, is this intentional to have a
get but not a set?

> +void *snd_soc_usb_get_priv_data(struct device *usbdev);

you are using 'usbdev' and 'dev' for the same type of parameters, why
not align on one set of definition with a consistent naming.


> +static struct snd_soc_usb *snd_soc_find_usb_ctx(struct device *dev)
> +{
> + struct device_node *node;
> + struct snd_soc_usb *ctx = NULL;

this init doesn't seem required?

> +
> + node = snd_soc_find_phandle(dev);
> + if (IS_ERR(node))
> + return NULL;
> +
> + mutex_lock(&ctx_mutex);
> + list_for_each_entry(ctx, &usb_ctx_list, list) {
> + if (ctx->dev->of_node == node) {
> + of_node_put(node);
> + mutex_unlock(&ctx_mutex);
> + return ctx;
> + }
> + }
> + of_node_put(node);
> + mutex_unlock(&ctx_mutex);
> +
> + return NULL;
> +}
> +
> +/**
> + * snd_soc_usb_get_priv_data() - Retrieve private data stored
> + * @dev: device reference
> + *
> + * Fetch the private data stored in the USB SND SOC structure.
> + *
> + */
> +void *snd_soc_usb_get_priv_data(struct device *dev)
> +{
> + struct snd_soc_usb *ctx;
> +
> + ctx = snd_soc_find_usb_ctx(dev);

so in this function you walk through the usb_ctx_list with locking...

> + if (!ctx) {
> + /* Check if backend device */
> + list_for_each_entry(ctx, &usb_ctx_list, list) {

... and here you walk again through the list without locking.

Something's weird here, if this was intentional you need to add comments.

> + if (dev->of_node == ctx->dev->of_node)
> + goto out;
> + }
> + ctx = NULL;
> + }
> +out:
> + return ctx ? ctx->priv_data : NULL;
> +}
> +EXPORT_SYMBOL_GPL(snd_soc_usb_get_priv_data);
> +
> +/**
> + * snd_soc_usb_add_port() - Add a USB backend port
> + * @dev: USB backend device
> + * @priv: private data
> + * @connection_cb: connection status callback
> + *
> + * Register a USB backend device to the SND USB SOC framework. Memory is
> + * allocated as part of the USB backend device.
> + *
> + */
> +struct snd_soc_usb *snd_soc_usb_add_port(struct device *dev, void *priv,
> + int (*connection_cb)(struct snd_soc_usb *usb, int card_idx,
> + int connected))
> +{
> + struct snd_soc_usb *usb;
> +
> + usb = devm_kzalloc(dev, sizeof(*usb), GFP_KERNEL);
> + if (!usb)
> + return ERR_PTR(-ENOMEM);
> +
> + usb->connection_status_cb = connection_cb;
> + usb->dev = dev;
> + usb->priv_data = priv;

back to my comment above, you don't seem to need the set_priv_data() ?

> +
> + mutex_lock(&ctx_mutex);
> + list_add_tail(&usb->list, &usb_ctx_list);
> + mutex_unlock(&ctx_mutex);
> +
> + return usb;
> +}
> +EXPORT_SYMBOL_GPL(snd_soc_usb_add_port);

> +/**
> + * snd_soc_usb_connect() - Notification of USB device connection
> + * @usbdev: USB bus device
> + * @card_idx: USB SND card instance
> + *
> + * Notify of a new USB SND device connection. The card_idx can be used to
> + * handle how the USB backend selects, which device to enable offloading on.

"USB backend" is confusing, not sure if this is the same concept as DPCM
"backend" or something else. Please try to avoid overloaded terms.

> + *
> + */
> +int snd_soc_usb_connect(struct device *usbdev, int card_idx)
> +{
> + struct snd_soc_usb *ctx;
> +
> + if (!usbdev)
> + return -ENODEV;
> +
> + ctx = snd_soc_find_usb_ctx(usbdev);
> + if (!ctx)
> + return -ENODEV;
> +
> + if (ctx->connection_status_cb)
> + ctx->connection_status_cb(ctx, card_idx, 1);

so either the 'connected' value is 1...
> +
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(snd_soc_usb_connect);
> +
> +/**
> + * snd_soc_usb_disconnect() - Notification of USB device disconnection
> + * @usbdev: USB bus device
> + *
> + * Notify of a new USB SND device disconnection to the USB backend.
> + *
> + */
> +int snd_soc_usb_disconnect(struct device *usbdev)
> +{
> + struct snd_soc_usb *ctx;
> +
> + if (!usbdev)
> + return -ENODEV;
> +
> + ctx = snd_soc_find_usb_ctx(usbdev);
> + if (!ctx)
> + return -ENODEV;
> +
> + if (ctx->connection_status_cb)
> + ctx->connection_status_cb(ctx, -1, 0);

...and here it's zero.

should this 'connected' parameter be a boolean then with true/false
value, or do you want to add enums/defines for more flexibility down the
road?

> +
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(snd_soc_usb_disconnect);

2023-07-25 10:02:25

by Pierre-Louis Bossart

[permalink] [raw]
Subject: Re: [PATCH v4 10/32] ASoC: qcom: Add USB backend ASoC driver for Q6


> +struct q6usb_port_data {
> + struct q6afe_usb_cfg usb_cfg;
> + struct snd_soc_usb *usb;
> + struct q6usb_offload priv;
> + int active_idx;

what is an 'active_idx' ?


> +static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
> + int connected)
> +{
> + struct snd_soc_dapm_context *dapm;
> + struct q6usb_port_data *data;
> +
> + dapm = snd_soc_component_get_dapm(usb->component);
> + data = dev_get_drvdata(usb->component->dev);

shouldn't you test that 'dapm' and 'data' are not NULL ?

> +
> + if (connected) {

this goes back to my earlier comment that you treat 'connected' as a
boolean.

> + snd_soc_dapm_enable_pin(dapm, "USB_RX_BE");
> + /* We only track the latest USB headset plugged in */
> + data->active_idx = card_idx;
> + } else {
> + snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
> + }
> + snd_soc_dapm_sync(dapm);
> +
> + return 0;
> +}
> +
> +static int q6usb_component_probe(struct snd_soc_component *component)
> +{
> + struct q6usb_port_data *data = dev_get_drvdata(component->dev);
> + struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
> +
> + snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
> + snd_soc_dapm_sync(dapm);
> +
> + data->usb = snd_soc_usb_add_port(component->dev, &data->priv, q6usb_alsa_connection_cb);
> + if (IS_ERR(data->usb)) {
> + dev_err(component->dev, "failed to add usb port\n");
> + return -ENODEV;
> + }
> +
> + data->usb->component = component;
> +
> + return 0;
> +}
> +
> +static const struct snd_soc_component_driver q6usb_dai_component = {
> + .probe = q6usb_component_probe,

erm, if you have a .probe that adds a port, don't you need a remove that
removes the same port, and sets the pin state as well?

> + .name = "q6usb-dai-component",
> + .dapm_widgets = q6usb_dai_widgets,
> + .num_dapm_widgets = ARRAY_SIZE(q6usb_dai_widgets),
> + .dapm_routes = q6usb_dapm_routes,
> + .num_dapm_routes = ARRAY_SIZE(q6usb_dapm_routes),
> + .of_xlate_dai_name = q6usb_audio_ports_of_xlate_dai_name,
> +};
> +
> +static int q6usb_dai_dev_probe(struct platform_device *pdev)
> +{
> + struct device_node *node = pdev->dev.of_node;
> + struct q6usb_port_data *data;
> + struct device *dev = &pdev->dev;
> + struct of_phandle_args args;
> + int ret;
> +
> + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
> + if (!data)
> + return -ENOMEM;
> +
> + ret = of_property_read_u32(node, "qcom,usb-audio-intr-num",
> + &data->priv.intr_num);
> + if (ret) {
> + dev_err(&pdev->dev, "failed to read intr num.\n");
> + return ret;
> + }
> +
> + ret = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args);
> + if (ret < 0)
> + data->priv.sid = -1;
> + else
> + data->priv.sid = args.args[0] & SID_MASK;
> +
> + data->priv.domain = iommu_get_domain_for_dev(&pdev->dev);
> +
> + data->priv.dev = dev;
> + dev_set_drvdata(dev, data);
> +
> + ret = devm_snd_soc_register_component(dev, &q6usb_dai_component,
> + q6usb_be_dais, ARRAY_SIZE(q6usb_be_dais));
> + if (ret < 0)
> + return ret;
> +
> + return 0;

return devm_snd_soc_register_component

> +}
> +
> +static int q6usb_dai_dev_remove(struct platform_device *pdev)
> +{
> + snd_soc_usb_remove_port(&pdev->dev);

that seems wrong, the port is added in the component probe, not the
platform device probe.

> +
> + return 0;
> +}> +


2023-07-25 10:03:17

by Pierre-Louis Bossart

[permalink] [raw]
Subject: Re: [PATCH v4 29/32] sound: soc: qcom: q6usb: Add headphone jack for offload connection status



On 7/25/23 04:34, Wesley Cheng wrote:
> The headphone jack framework has a well defined intrastructure for

infrastructure

> notifying userspace entities through input devices. Expose a jack device
> that carries information about if an offload capable device is connected.
> Applications can further identify specific offloading information through
> other SND kcontrols.

What if you connect a set of USB speakers? Would they show as a
headphone/headset?

> Signed-off-by: Wesley Cheng <[email protected]>
> ---
> sound/soc/qcom/qdsp6/q6usb.c | 17 +++++++++++++++--
> 1 file changed, 15 insertions(+), 2 deletions(-)
>
> diff --git a/sound/soc/qcom/qdsp6/q6usb.c b/sound/soc/qcom/qdsp6/q6usb.c
> index e4ccb9d912b0..860dff8c1438 100644
> --- a/sound/soc/qcom/qdsp6/q6usb.c
> +++ b/sound/soc/qcom/qdsp6/q6usb.c
> @@ -20,6 +20,7 @@
> #include <sound/pcm_params.h>
> #include <sound/asound.h>
> #include <sound/q6usboffload.h>
> +#include <sound/jack.h>
>
> #include "q6dsp-lpass-ports.h"
> #include "q6afe.h"
> @@ -37,6 +38,7 @@ struct q6usb_status {
> struct q6usb_port_data {
> struct q6afe_usb_cfg usb_cfg;
> struct snd_soc_usb *usb;
> + struct snd_soc_jack hs_jack;
> struct q6usb_offload priv;
> struct mutex mutex;
> unsigned long available_card_slot;
> @@ -279,6 +281,7 @@ static const struct snd_kcontrol_new q6usb_offload_control = {
> /* Build a mixer control for a UAC connector control (jack-detect) */
> static void q6usb_connector_control_init(struct snd_soc_component *component)
> {
> + struct q6usb_port_data *data = dev_get_drvdata(component->dev);
> int ret;
>
> ret = snd_ctl_add(component->card->snd_card,
> @@ -290,6 +293,11 @@ static void q6usb_connector_control_init(struct snd_soc_component *component)
> snd_ctl_new1(&q6usb_offload_dev_ctrl, component));
> if (ret < 0)
> return;
> +
> + ret = snd_soc_card_jack_new(component->card, "USB offload",
> + SND_JACK_HEADSET, &data->hs_jack);

not all USB devices are headsets...

> + if (ret)
> + return;
> }
>
> static int q6usb_audio_ports_of_xlate_dai_name(struct snd_soc_component *component,
> @@ -322,7 +330,10 @@ static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
>
> mutex_lock(&data->mutex);
> if (connected) {
> - snd_soc_dapm_enable_pin(dapm, "USB_RX_BE");
> + if (!data->available_card_slot) {
> + snd_soc_dapm_enable_pin(dapm, "USB_RX_BE");
> + snd_jack_report(data->hs_jack.jack, 1);
> + }
> /*
> * Update the latest USB headset plugged in, if session is
> * idle.
> @@ -338,8 +349,10 @@ static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
> clear_bit(card_idx, &data->available_card_slot);
> data->status[card_idx].num_pcm = 0;
> data->status[card_idx].chip_index = 0;
> - if (!data->available_card_slot)
> + if (!data->available_card_slot) {
> snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
> + snd_jack_report(data->hs_jack.jack, 0);
> + }
> }
> snd_soc_dapm_sync(dapm);
> mutex_unlock(&data->mutex);

2023-07-25 10:50:45

by Pierre-Louis Bossart

[permalink] [raw]
Subject: Re: [PATCH v4 31/32] sound: usb: card: Allow for rediscovery of connected USB SND devices



On 7/25/23 04:34, Wesley Cheng wrote:
> In case of notifying SND platform drivers of connection events, some of
> these use cases, such as offloading, require an ASoC USB backend device to
> be initialized before the events can be handled. If the USB backend device
> has not yet been probed, this leads to missing initial USB audio device
> connection events.
>
> Expose an API that traverses the usb_chip array for connected devices, and
> to call the respective connection callback registered to the SND platform
> driver.
>
> Signed-off-by: Wesley Cheng <[email protected]>
> ---
> sound/usb/card.c | 19 +++++++++++++++++++
> sound/usb/card.h | 2 ++
> 2 files changed, 21 insertions(+)
>
> diff --git a/sound/usb/card.c b/sound/usb/card.c
> index 365f6d978608..27a89aaa0bf3 100644
> --- a/sound/usb/card.c
> +++ b/sound/usb/card.c
> @@ -170,6 +170,25 @@ struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
> }
> EXPORT_SYMBOL_GPL(snd_usb_find_suppported_substream);
>
> +/*
> + * in case the platform driver was not ready at the time of USB SND
> + * device connect, expose an API to discover all connected USB devices
> + * so it can populate any dependent resources/structures.
> + */
> +void snd_usb_rediscover_devices(void)
> +{
> + int i;
> +
> + mutex_lock(&register_mutex);
> + for (i = 0; i < SNDRV_CARDS; i++) {
> + if (usb_chip[i])
> + if (platform_ops && platform_ops->connect_cb)
> + platform_ops->connect_cb(usb_chip[i]);

what happens if the USB device is removed while the platform device adds
a port?

This sounds super-racy to me. It's the same set of problems we're having
between audio and display/DRM, I would be surprised if this function
dealt with all corner cases of insertion/removal, bind/unbind.


> + }
> + mutex_unlock(&register_mutex);
> +}
> +EXPORT_SYMBOL_GPL(snd_usb_rediscover_devices);
> +
> /*
> * disconnect streams
> * called from usb_audio_disconnect()
> diff --git a/sound/usb/card.h b/sound/usb/card.h
> index db735680d155..dfd241334e3d 100644
> --- a/sound/usb/card.h
> +++ b/sound/usb/card.h
> @@ -219,6 +219,7 @@ int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops);
> int snd_usb_unregister_platform_ops(void);
> struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
> struct snd_pcm_hw_params *params, int direction);
> +void snd_usb_rediscover_devices(void);
> #else
> static int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops)
> {
> @@ -235,5 +236,6 @@ static struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
> {
> return NULL;
> }
> +void snd_usb_rediscover_devices(void) { }
> #endif /* IS_ENABLED(CONFIG_SND_USB_AUDIO) */
> #endif /* __USBAUDIO_CARD_H */

2023-07-25 10:52:39

by Takashi Iwai

[permalink] [raw]
Subject: Re: [PATCH v4 31/32] sound: usb: card: Allow for rediscovery of connected USB SND devices

On Tue, 25 Jul 2023 11:15:11 +0200,
Pierre-Louis Bossart wrote:
>
>
>
> On 7/25/23 04:34, Wesley Cheng wrote:
> > In case of notifying SND platform drivers of connection events, some of
> > these use cases, such as offloading, require an ASoC USB backend device to
> > be initialized before the events can be handled. If the USB backend device
> > has not yet been probed, this leads to missing initial USB audio device
> > connection events.
> >
> > Expose an API that traverses the usb_chip array for connected devices, and
> > to call the respective connection callback registered to the SND platform
> > driver.
> >
> > Signed-off-by: Wesley Cheng <[email protected]>
> > ---
> > sound/usb/card.c | 19 +++++++++++++++++++
> > sound/usb/card.h | 2 ++
> > 2 files changed, 21 insertions(+)
> >
> > diff --git a/sound/usb/card.c b/sound/usb/card.c
> > index 365f6d978608..27a89aaa0bf3 100644
> > --- a/sound/usb/card.c
> > +++ b/sound/usb/card.c
> > @@ -170,6 +170,25 @@ struct snd_usb_stream *snd_usb_find_suppported_substream(int card_idx,
> > }
> > EXPORT_SYMBOL_GPL(snd_usb_find_suppported_substream);
> >
> > +/*
> > + * in case the platform driver was not ready at the time of USB SND
> > + * device connect, expose an API to discover all connected USB devices
> > + * so it can populate any dependent resources/structures.
> > + */
> > +void snd_usb_rediscover_devices(void)
> > +{
> > + int i;
> > +
> > + mutex_lock(&register_mutex);
> > + for (i = 0; i < SNDRV_CARDS; i++) {
> > + if (usb_chip[i])
> > + if (platform_ops && platform_ops->connect_cb)
> > + platform_ops->connect_cb(usb_chip[i]);
>
> what happens if the USB device is removed while the platform device adds
> a port?

That should be protected by the register_mutex. But there can be
other races (see below :)

> This sounds super-racy to me. It's the same set of problems we're having
> between audio and display/DRM, I would be surprised if this function
> dealt with all corner cases of insertion/removal, bind/unbind.

Yes, we need to be more careful about binding.

For example, in the current patch set, I see no way to prevent
unloading snd-usb-audio-qmi module, and it allows user to cut off the
stuff during operation, which may break things while the kernel is
running the code of the unloaded module. You need to have a proper
module refcount management for avoiding such a scenario. Most of
drivers don't need it because ALSA core part already takes care of
it. But in this case, it requires a manual adjustment.


Takashi

2023-07-25 10:53:12

by Oliver Neukum

[permalink] [raw]
Subject: Re: [PATCH v4 01/32] xhci: add support to allocate several interrupters

On 25.07.23 04:33, Wesley Cheng wrote:

Hi,

this is more of a stylistic remark, but this:

[..]
> + /* Find available secondary interrupter, interrupter 0 is reserverd for primary */
> + for (i = 1; i < xhci->max_interrupters; i++) {
> + if (xhci->interrupters[i] == NULL) {
> + err = xhci_add_interrupter(xhci, ir, i);
> + break;
> + }
> + }
> +
> + spin_unlock_irq(&xhci->lock);
> +
> + if (!err) {

Making the non error case a conditional return is ...

> + xhci_dbg(xhci, "Add secondary interrupter %d, max interrupters %d\n",
> + i, xhci->max_interrupters);
> + return ir;
> + }
> +
> + xhci_warn(xhci, "Failed to add secondary interrupter, max interrupters %d\n",
> + xhci->max_interrupters);
> +
> + xhci_free_interrupter(xhci, ir);
> +
> + return NULL;
> +}

Was this taken out of a style guide that teaches to avoid "goto"
at any cost?

Regards
Oliver


2023-07-25 10:55:16

by Pierre-Louis Bossart

[permalink] [raw]
Subject: Re: [PATCH v4 27/32] sound: soc: qdsp6: Add SND kcontrol to select offload device



On 7/25/23 04:34, Wesley Cheng wrote:
> Expose a kcontrol on the platform sound card, which will allow for
> userspace 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 'Q6USB offload SND device select' 1 0
>
> The above will set the Q6AFE device token to choose offload on card#1 and
> pcm#0. Device selection is made possible by setting the Q6AFE device
> token. The audio DSP utilizes this parameter, and will pass this field
> back to the USB offload driver within the QMI stream requests.

I must be missing something... If you have a card 0 which exposes a
control to change what the card1 does, then it means you have a card0
with a PCM device what can potentially be used concurrently with the
card1 exposing an offload device.

Is there any sort of mutual exclusion to make sure the same USB endpoint
is not used twice?

One would hope that when a device is opened the matching non-offloaded
one (or ones in the case of implicit feedback) is disabled or marked as
used?

I would guess in your Android setup you have control on such behavior at
the HAL level, but in the more generic Linux use I don't see what
would orchestrate the use of two devices, and at the kernel level what
would prevent it.


> Signed-off-by: Wesley Cheng <[email protected]>
> ---
> sound/soc/qcom/qdsp6/q6usb.c | 126 ++++++++++++++++++++++++++++++++++-
> 1 file changed, 123 insertions(+), 3 deletions(-)
>
> diff --git a/sound/soc/qcom/qdsp6/q6usb.c b/sound/soc/qcom/qdsp6/q6usb.c
> index 2fb6159bd2a8..87e88dafd8ad 100644
> --- a/sound/soc/qcom/qdsp6/q6usb.c
> +++ b/sound/soc/qcom/qdsp6/q6usb.c
> @@ -14,6 +14,7 @@
> #include <linux/dma-map-ops.h>
>
> #include <sound/pcm.h>
> +#include <sound/control.h>
> #include <sound/soc.h>
> #include <sound/soc-usb.h>
> #include <sound/pcm_params.h>
> @@ -35,9 +36,12 @@ struct q6usb_port_data {
> struct q6afe_usb_cfg usb_cfg;
> struct snd_soc_usb *usb;
> struct q6usb_offload priv;
> + struct mutex mutex;
> unsigned long available_card_slot;
> struct q6usb_status status[SNDRV_CARDS];
> - int active_idx;
> + bool idx_valid;
> + int sel_card_idx;
> + int sel_pcm_idx;
> };
>
> static const struct snd_soc_dapm_widget q6usb_dai_widgets[] = {
> @@ -53,10 +57,36 @@ static int q6usb_hw_params(struct snd_pcm_substream *substream,
> struct snd_soc_dai *dai)
> {
> struct q6usb_port_data *data = dev_get_drvdata(dai->dev);
> + struct snd_soc_pcm_runtime *rtd = substream->private_data;
> + struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
> + struct q6afe_port *q6usb_afe;
> int direction = substream->stream;
> + int chip_idx;
> + int ret;
> +
> + mutex_lock(&data->mutex);
> + chip_idx = data->status[data->sel_card_idx].chip_index;
> +
> + ret = snd_soc_usb_find_format(chip_idx, params, direction);
> + if (ret < 0)
> + goto out;
> +
> + q6usb_afe = q6afe_port_get_from_id(cpu_dai->dev, USB_RX);
> + if (IS_ERR(q6usb_afe))
> + goto out;
> +
> + ret = afe_port_send_usb_dev_param(q6usb_afe, data->sel_card_idx,
> + data->sel_pcm_idx);
> + if (ret < 0)
> + goto out;
> +
> + data->status[data->sel_card_idx].pcm_index = data->sel_pcm_idx;
> +out:
> + mutex_unlock(&data->mutex);
>
> - return snd_soc_usb_find_format(data->active_idx, params, direction);
> + return ret;
> }
> +
> static const struct snd_soc_dai_ops q6usb_ops = {
> .hw_params = q6usb_hw_params,
> };
> @@ -85,6 +115,89 @@ static struct snd_soc_dai_driver q6usb_be_dais[] = {
> },
> };
>
> +static int q6usb_get_offload_dev(struct snd_kcontrol *kcontrol,
> + struct snd_ctl_elem_value *ucontrol)
> +{
> + struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> + struct q6usb_port_data *data = dev_get_drvdata(component->dev);
> + int pcm_idx;
> + int card_idx;
> +
> + mutex_lock(&data->mutex);
> + if (!data->idx_valid) {
> + card_idx = -1;
> + pcm_idx = -1;
> + } else {
> + card_idx = data->sel_card_idx;
> + pcm_idx = data->sel_pcm_idx;
> + }
> +
> + ucontrol->value.integer.value[0] = card_idx;
> + ucontrol->value.integer.value[1] = pcm_idx;
> + mutex_unlock(&data->mutex);
> +
> + return 0;
> +}
> +
> +static int q6usb_put_offload_dev(struct snd_kcontrol *kcontrol,
> + struct snd_ctl_elem_value *ucontrol)
> +{
> + struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> + struct q6usb_port_data *data = dev_get_drvdata(component->dev);
> + int changed = 0;
> + int pcmidx;
> + int cardidx;
> +
> + cardidx = ucontrol->value.integer.value[0];
> + pcmidx = ucontrol->value.integer.value[1];
> +
> + mutex_lock(&data->mutex);
> + if ((cardidx >= 0 && test_bit(cardidx, &data->available_card_slot))) {
> + data->sel_card_idx = cardidx;
> + changed = 1;
> + }
> +
> + if ((pcmidx >= 0 && pcmidx < data->status[cardidx].num_pcm)) {
> + data->sel_pcm_idx = pcmidx;
> + data->idx_valid = true;
> + changed = 1;
> + }
> + mutex_unlock(&data->mutex);
> +
> + return changed;
> +}
> +
> +static int q6usb_offload_dev_info(struct snd_kcontrol *kcontrol,
> + struct snd_ctl_elem_info *uinfo)
> +{
> + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
> + uinfo->count = 2;
> + uinfo->value.integer.min = -1;
> + uinfo->value.integer.max = SNDRV_CARDS;
> +
> + return 0;
> +}
> +
> +static const struct snd_kcontrol_new q6usb_offload_dev_ctrl = {
> + .iface = SNDRV_CTL_ELEM_IFACE_CARD,
> + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
> + .name = "Q6USB offload SND device select",
> + .info = q6usb_offload_dev_info,
> + .get = q6usb_get_offload_dev,
> + .put = q6usb_put_offload_dev,
> +};
> +
> +/* Build a mixer control for a UAC connector control (jack-detect) */
> +static void q6usb_connector_control_init(struct snd_soc_component *component)
> +{
> + int ret;
> +
> + ret = snd_ctl_add(component->card->snd_card,
> + snd_ctl_new1(&q6usb_offload_dev_ctrl, component));
> + if (ret < 0)
> + return;
> +}
> +
> static int q6usb_audio_ports_of_xlate_dai_name(struct snd_soc_component *component,
> const struct of_phandle_args *args,
> const char **dai_name)
> @@ -113,10 +226,12 @@ static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
> dapm = snd_soc_component_get_dapm(usb->component);
> data = dev_get_drvdata(usb->component->dev);
>
> + mutex_lock(&data->mutex);
> if (connected) {
> snd_soc_dapm_enable_pin(dapm, "USB_RX_BE");
> /* We only track the latest USB headset plugged in */
> - data->active_idx = card_idx;
> + if (!data->idx_valid || data->sel_card_idx < 0)
> + data->sel_card_idx = card_idx;
>
> set_bit(card_idx, &data->available_card_slot);
> data->status[card_idx].num_pcm = num_pcm;
> @@ -129,6 +244,7 @@ static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
> snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
> }
> snd_soc_dapm_sync(dapm);
> + mutex_unlock(&data->mutex);
>
> return 0;
> }
> @@ -141,6 +257,8 @@ static int q6usb_component_probe(struct snd_soc_component *component)
> snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
> snd_soc_dapm_sync(dapm);
>
> + q6usb_connector_control_init(component);
> +
> data->usb = snd_soc_usb_add_port(component->dev, &data->priv, q6usb_alsa_connection_cb);
> if (IS_ERR(data->usb)) {
> dev_err(component->dev, "failed to add usb port\n");
> @@ -189,6 +307,8 @@ static int q6usb_dai_dev_probe(struct platform_device *pdev)
>
> data->priv.domain = iommu_get_domain_for_dev(&pdev->dev);
>
> + mutex_init(&data->mutex);
> +
> data->priv.dev = dev;
> dev_set_drvdata(dev, data);
>

2023-07-25 23:41:38

by Wesley Cheng

[permalink] [raw]
Subject: Re: [PATCH v4 18/32] sound: usb: Introduce QC USB SND offloading support

Hi Takashi,

On 7/25/2023 12:26 AM, Takashi Iwai wrote:
> On Tue, 25 Jul 2023 04:34:02 +0200,
> Wesley Cheng wrote:
>>
>> --- a/sound/usb/Kconfig
>> +++ b/sound/usb/Kconfig
>> @@ -165,6 +165,21 @@ config SND_BCD2000
>> To compile this driver as a module, choose M here: the module
>> will be called snd-bcd2000.
>>
>> +config QC_USB_AUDIO_OFFLOAD
>> + tristate "Qualcomm Audio Offload driver"
>> + depends on QCOM_QMI_HELPERS
>> + select SND_PCM
>
> So the driver can be enabled without CONFIG_SND_USB_AUDIO? It makes
> little sense without it.
> Or is it set so intentionally for testing purpose?
>

Thanks for the review. I'll change this to be dependent on
CONFIG_SND_USB_AUDIO...it shouldn't exist in the end use case w/o it.

> About the code:
>
>> +/* Offloading IOMMU management */
>> +static unsigned long uaudio_get_iova(unsigned long *curr_iova,
>> + size_t *curr_iova_size, struct list_head *head, size_t size)
>> +{
>> + struct iova_info *info, *new_info = NULL;
>> + struct list_head *curr_head;
>> + unsigned long va = 0;
>> + size_t tmp_size = size;
>> + bool found = false;
>> +
>> + if (size % PAGE_SIZE) {
>> + dev_err(uaudio_qdev->dev, "size %zu is not page size multiple\n",
>> + size);
>> + goto done;
>
> This can be easily triggered by user-space as it's passed directly
> from the mmap call, and it implies that you can fill up the messages
> easily. It's safer to make it debug message or add the rate limit.
>
> Ditto for other error messages.
>

Got it, I'll make sure to address the above dev_err().

>> +static void disable_audio_stream(struct snd_usb_substream *subs)
>> +{
>> + struct snd_usb_audio *chip = subs->stream->chip;
>> +
>> + if (subs->data_endpoint || subs->sync_endpoint) {
>> + close_endpoints(chip, subs);
>> +
>> + mutex_lock(&chip->mutex);
>> + subs->cur_audiofmt = NULL;
>> + mutex_unlock(&chip->mutex);
>> + }
>
> Now looking at this and...
>
>> +static int enable_audio_stream(struct snd_usb_substream *subs,
>> + snd_pcm_format_t pcm_format,
>> + unsigned int channels, unsigned int cur_rate,
>> + int datainterval)
>> +{
>
> ... this implementation, I wonder whether it'd be better to modify and
> export snd_usb_hw_params() snd snd_usb_hw_free() to fit with qcom
> driver. Then you can avoid lots of open code.
>

I think the problem is that snd_usb_hw_params assumes that we've already
done a PCM open on the PCM device created by USB SND. However, with the
offload path, we don't reference the USB PCM device, but the one created
by the platform sound card. Hence, I don't have access to the
snd_pcm_substream.

I attempted to derive snd_pcm_substream from snd_usb_substream, but
since PCM open isn't run, it doesn't provide a valid structure.

What do you think about adding a wrapper to snd_usb_hw_params? Have a
version that will take in snd_usb_substream, and another that is
registered to hw_params().

> In general, if you see a direct use of chip->mutex, it can be often
> done better in a different form. The use of an internal lock or such
> from an external driver is always fragile and error-prone.
>
> Also, the current open-code misses the potential race against the
> disconnection during the operation. In snd-usb-audio, it protects
> with snd_usb_lock_shutdown() and snd_usb_unlock_shutdown() pairs.
>

I agree...I think then the best approach would be something like the
above, ie:

int snd_usb_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *hw_params)
{
struct snd_usb_substream *subs = substream->runtime->private_data;

snd_usb_ep_attach(subs, hw_params);
...

int snd_usb_ep_attach(...)
{
//implementation of current code in snd_usb_hw_params()
}
EXPORT_SYMBOL(snd_usb_ep_attach);

>> +static int __init qc_usb_audio_offload_init(void)
>> +{
>> + struct uaudio_qmi_svc *svc;
>> + int ret;
>> +
>> + ret = snd_usb_register_platform_ops(&offload_ops);
>> + if (ret < 0)
>> + return ret;
>
> Registering the ops at the very first opens a potential access to the
> uninitialized stuff. Imagine a suspend happens right after this
> point. As the ops is already registered, it'll enter to the
> suspend_cb callback and straight to Oops.
>
>> +static void __exit qc_usb_audio_offload_exit(void)
>> +{
>> + struct uaudio_qmi_svc *svc = uaudio_svc;
>> +
>> + qmi_handle_release(svc->uaudio_svc_hdl);
>> + flush_workqueue(svc->uaudio_wq);
>> + destroy_workqueue(svc->uaudio_wq);
>> + kfree(svc);
>> + uaudio_svc = NULL;
>> + snd_usb_unregister_platform_ops();
>
> Similarly, the unregister order has to be careful, too.
>

Let me re-organize it a bit more.

Thanks
Wesley Cheng

2023-07-26 00:24:07

by Wesley Cheng

[permalink] [raw]
Subject: Re: [PATCH v4 06/32] ASoC: Add SOC USB APIs for adding an USB backend

Hi Pierre,

On 7/25/2023 1:10 AM, Pierre-Louis Bossart wrote:
>
>> +/**
>> + * struct snd_soc_usb
>> + * @list - list head for SND SOC struct list
>> + * @dev - USB backend device reference
>> + * @component - reference to DAPM component
>
> ASoC component, not DAPM.
>

Thanks for the detailed review. Will fix this.

>> + * @connection_status_cb - callback to notify connection events
>> + * @priv_data - driver data
>> + **/
>> +struct snd_soc_usb {
>> + struct list_head list;
>> + struct device *dev;
>> + struct snd_soc_component *component;
>> + int (*connection_status_cb)(struct snd_soc_usb *usb, int card_idx,
>> + int connected);
>
> It's not clear what 'connected' really refers to, is this a boolean
> really or is this a "connection_event?
>
>

I'll change it to boolean for now, since its currently only
connected/disconnected. If more states are required, then we can add
that in the future.

>> + void *priv_data;
>> +};
>> +
>> +int snd_soc_usb_connect(struct device *usbdev, int card_idx);
>> +int snd_soc_usb_disconnect(struct device *usbdev);
>> +void snd_soc_usb_set_priv_data(struct device *dev, void *priv);
>
> this function is not part of this patch, is this intentional to have a
> get but not a set?
>
>> +void *snd_soc_usb_get_priv_data(struct device *usbdev);
>
> you are using 'usbdev' and 'dev' for the same type of parameters, why
> not align on one set of definition with a consistent naming.
>
>

I'll take a look at this and see what happened. I think Greg mentioned
something similar and I made the change to remove the set priv, and
moved it elsewhere.

>> +static struct snd_soc_usb *snd_soc_find_usb_ctx(struct device *dev)
>> +{
>> + struct device_node *node;
>> + struct snd_soc_usb *ctx = NULL;
>
> this init doesn't seem required?
>

Yes, not needed.

>> +
>> + node = snd_soc_find_phandle(dev);
>> + if (IS_ERR(node))
>> + return NULL;
>> +
>> + mutex_lock(&ctx_mutex);
>> + list_for_each_entry(ctx, &usb_ctx_list, list) {
>> + if (ctx->dev->of_node == node) {
>> + of_node_put(node);
>> + mutex_unlock(&ctx_mutex);
>> + return ctx;
>> + }
>> + }
>> + of_node_put(node);
>> + mutex_unlock(&ctx_mutex);
>> +
>> + return NULL;
>> +}
>> +
>> +/**
>> + * snd_soc_usb_get_priv_data() - Retrieve private data stored
>> + * @dev: device reference
>> + *
>> + * Fetch the private data stored in the USB SND SOC structure.
>> + *
>> + */
>> +void *snd_soc_usb_get_priv_data(struct device *dev)
>> +{
>> + struct snd_soc_usb *ctx;
>> +
>> + ctx = snd_soc_find_usb_ctx(dev);
>
> so in this function you walk through the usb_ctx_list with locking...
>
>> + if (!ctx) {
>> + /* Check if backend device */
>> + list_for_each_entry(ctx, &usb_ctx_list, list) {
>
> ... and here you walk again through the list without locking.
>
> Something's weird here, if this was intentional you need to add comments.
>

Yes, needs a lock here.

>> + if (dev->of_node == ctx->dev->of_node)
>> + goto out;
>> + }
>> + ctx = NULL;
>> + }
>> +out:
>> + return ctx ? ctx->priv_data : NULL;
>> +}
>> +EXPORT_SYMBOL_GPL(snd_soc_usb_get_priv_data);
>> +
>> +/**
>> + * snd_soc_usb_add_port() - Add a USB backend port
>> + * @dev: USB backend device
>> + * @priv: private data
>> + * @connection_cb: connection status callback
>> + *
>> + * Register a USB backend device to the SND USB SOC framework. Memory is
>> + * allocated as part of the USB backend device.
>> + *
>> + */
>> +struct snd_soc_usb *snd_soc_usb_add_port(struct device *dev, void *priv,
>> + int (*connection_cb)(struct snd_soc_usb *usb, int card_idx,
>> + int connected))
>> +{
>> + struct snd_soc_usb *usb;
>> +
>> + usb = devm_kzalloc(dev, sizeof(*usb), GFP_KERNEL);
>> + if (!usb)
>> + return ERR_PTR(-ENOMEM);
>> +
>> + usb->connection_status_cb = connection_cb;
>> + usb->dev = dev;
>> + usb->priv_data = priv;
>
> back to my comment above, you don't seem to need the set_priv_data() ?
>

And yes, this is where I moved the priv data setting.

>> +
>> + mutex_lock(&ctx_mutex);
>> + list_add_tail(&usb->list, &usb_ctx_list);
>> + mutex_unlock(&ctx_mutex);
>> +
>> + return usb;
>> +}
>> +EXPORT_SYMBOL_GPL(snd_soc_usb_add_port);
>
>> +/**
>> + * snd_soc_usb_connect() - Notification of USB device connection
>> + * @usbdev: USB bus device
>> + * @card_idx: USB SND card instance
>> + *
>> + * Notify of a new USB SND device connection. The card_idx can be used to
>> + * handle how the USB backend selects, which device to enable offloading on.
>
> "USB backend" is confusing, not sure if this is the same concept as DPCM
> "backend" or something else. Please try to avoid overloaded terms.
>

Sure, I meant DPCM backend.

>> + *
>> + */
>> +int snd_soc_usb_connect(struct device *usbdev, int card_idx)
>> +{
>> + struct snd_soc_usb *ctx;
>> +
>> + if (!usbdev)
>> + return -ENODEV;
>> +
>> + ctx = snd_soc_find_usb_ctx(usbdev);
>> + if (!ctx)
>> + return -ENODEV;
>> +
>> + if (ctx->connection_status_cb)
>> + ctx->connection_status_cb(ctx, card_idx, 1);
>
> so either the 'connected' value is 1...
>> +
>> + return 0;
>> +}
>> +EXPORT_SYMBOL_GPL(snd_soc_usb_connect);
>> +
>> +/**
>> + * snd_soc_usb_disconnect() - Notification of USB device disconnection
>> + * @usbdev: USB bus device
>> + *
>> + * Notify of a new USB SND device disconnection to the USB backend.
>> + *
>> + */
>> +int snd_soc_usb_disconnect(struct device *usbdev)
>> +{
>> + struct snd_soc_usb *ctx;
>> +
>> + if (!usbdev)
>> + return -ENODEV;
>> +
>> + ctx = snd_soc_find_usb_ctx(usbdev);
>> + if (!ctx)
>> + return -ENODEV;
>> +
>> + if (ctx->connection_status_cb)
>> + ctx->connection_status_cb(ctx, -1, 0);
>
> ...and here it's zero.
>
> should this 'connected' parameter be a boolean then with true/false
> value, or do you want to add enums/defines for more flexibility down the
> road?
>

As mentioned above, will switch to boolean for now. I don't see a need
to have other enums.

Thanks
Wesley Cheng

2023-07-26 09:06:47

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH v4 23/32] ASoC: dt-bindings: Add Q6USB backend bindings

On 25/07/2023 04:34, Wesley Cheng wrote:
> Add a dt-binding to describe the definition of enabling the Q6 USB backend
> device for audio offloading. The node carries information, which is passed
> along to the QC USB SND class driver counterpart. These parameters will be
> utilized during QMI stream enable requests.
>
> Signed-off-by: Wesley Cheng <[email protected]>

A nit, subject: drop second/last, redundant "bindings". The
"dt-bindings" prefix is already stating that these are bindings.

> ---
> .../bindings/sound/qcom,q6usb-dais.yaml | 49 +++++++++++++++++++
> 1 file changed, 49 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/sound/qcom,q6usb-dais.yaml
>
> diff --git a/Documentation/devicetree/bindings/sound/qcom,q6usb-dais.yaml b/Documentation/devicetree/bindings/sound/qcom,q6usb-dais.yaml
> new file mode 100644
> index 000000000000..77bc9b4c3c90
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/sound/qcom,q6usb-dais.yaml
> @@ -0,0 +1,49 @@
> +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/sound/qcom,q6usb-dais.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Qualcomm ASoC DPCM USB backend DAI
> +
> +maintainers:
> + - Wesley Cheng <[email protected]>
> +
> +description:
> + The USB port is a supported AFE path on the Q6 DSP. This ASoC DPCM
> + backend DAI driver will communicate the required settings to the QC USB SND
> + class driver for properly enabling the audio stream. Parameters defined
> + under this node will carry settings, which will be passed along during
> + the QMI stream enable request.

You still refer to the drivers. I said this should be rephrased and you
confirmed that "driver" here means Linux. If you are adding bindings for
Linux drivers, then it won't work. Bindings are for hardware.


Also, missing $ref to dai-common.yaml


> +
> +properties:
> + compatible:
> + enum:
> + - qcom,q6usb
> +


Best regards,
Krzysztof


2023-07-26 13:03:51

by Takashi Iwai

[permalink] [raw]
Subject: Re: [PATCH v4 18/32] sound: usb: Introduce QC USB SND offloading support

On Wed, 26 Jul 2023 00:59:57 +0200,
Wesley Cheng wrote:
>
> >> +static int enable_audio_stream(struct snd_usb_substream *subs,
> >> + snd_pcm_format_t pcm_format,
> >> + unsigned int channels, unsigned int cur_rate,
> >> + int datainterval)
> >> +{
> >
> > ... this implementation, I wonder whether it'd be better to modify and
> > export snd_usb_hw_params() snd snd_usb_hw_free() to fit with qcom
> > driver. Then you can avoid lots of open code.
> >
>
> I think the problem is that snd_usb_hw_params assumes that we've
> already done a PCM open on the PCM device created by USB SND.
> However, with the offload path, we don't reference the USB PCM device,
> but the one created by the platform sound card. Hence, I don't have
> access to the snd_pcm_substream.
>
> I attempted to derive snd_pcm_substream from snd_usb_substream, but
> since PCM open isn't run, it doesn't provide a valid structure.
>
> What do you think about adding a wrapper to snd_usb_hw_params? Have a
> version that will take in snd_usb_substream, and another that is
> registered to hw_params().

Yes, that's what I had in mind, too.

> > In general, if you see a direct use of chip->mutex, it can be often
> > done better in a different form. The use of an internal lock or such
> > from an external driver is always fragile and error-prone.
> >
> > Also, the current open-code misses the potential race against the
> > disconnection during the operation. In snd-usb-audio, it protects
> > with snd_usb_lock_shutdown() and snd_usb_unlock_shutdown() pairs.
> >
>
> I agree...I think then the best approach would be something like the
> above, ie:
>
> int snd_usb_hw_params(struct snd_pcm_substream *substream,
> struct snd_pcm_hw_params *hw_params)
> {
> struct snd_usb_substream *subs = substream->runtime->private_data;
>
> snd_usb_ep_attach(subs, hw_params);
> ...
>
> int snd_usb_ep_attach(...)
> {
> //implementation of current code in snd_usb_hw_params()
> }
> EXPORT_SYMBOL(snd_usb_ep_attach);

Yes, exactly something like that ;)


thanks,

Takashi

2023-07-26 17:54:34

by Rob Herring

[permalink] [raw]
Subject: Re: [PATCH v4 13/32] dt-bindings: usb: dwc3: Add snps,num-hc-interrupters definition

On Mon, Jul 24, 2023 at 07:33:57PM -0700, Wesley Cheng wrote:
> Add a new definition for specifying how many XHCI secondary interrupters
> can be allocated. XHCI in general can potentially support up to 1024
> interrupters, which some uses may want to limit depending on how many
> users utilize the interrupters.

Why does this belong in DT? What 'uses' determines the value? Who wants
to change this and when?

>
> Signed-off-by: Wesley Cheng <[email protected]>
> ---
> .../devicetree/bindings/usb/snps,dwc3.yaml | 13 +++++++++++++
> 1 file changed, 13 insertions(+)
>
> diff --git a/Documentation/devicetree/bindings/usb/snps,dwc3.yaml b/Documentation/devicetree/bindings/usb/snps,dwc3.yaml
> index 50edc4da780e..cc6012e922e0 100644
> --- a/Documentation/devicetree/bindings/usb/snps,dwc3.yaml
> +++ b/Documentation/devicetree/bindings/usb/snps,dwc3.yaml
> @@ -376,6 +376,19 @@ properties:
> items:
> enum: [1, 4, 8, 16, 32, 64, 128, 256]
>
> + snps,num-hc-interrupters:
> + description:
> + Defines the maximum number of XHCI host controller interrupters that can
> + be supported. The XHCI host controller has support to allocate multiple
> + event rings, which can be assigned to different clients/users. The DWC3
> + controller has a maximum of 8 interrupters. If this is not defined then
> + the value will be defaulted to 1. This parameter is used only when
> + operating in host mode.
> + $ref: /schemas/types.yaml#/definitions/uint8
> + minimum: 1
> + maximum: 8
> + default: 1
> +
> port:
> $ref: /schemas/graph.yaml#/properties/port
> description:

2023-07-26 23:34:18

by Wesley Cheng

[permalink] [raw]
Subject: Re: [PATCH v4 27/32] sound: soc: qdsp6: Add SND kcontrol to select offload device

Hi Pierre,

On 7/25/2023 2:16 AM, Pierre-Louis Bossart wrote:
>
>
> On 7/25/23 04:34, Wesley Cheng wrote:
>> Expose a kcontrol on the platform sound card, which will allow for
>> userspace 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 'Q6USB offload SND device select' 1 0
>>
>> The above will set the Q6AFE device token to choose offload on card#1 and
>> pcm#0. Device selection is made possible by setting the Q6AFE device
>> token. The audio DSP utilizes this parameter, and will pass this field
>> back to the USB offload driver within the QMI stream requests.
>
> I must be missing something... If you have a card 0 which exposes a
> control to change what the card1 does, then it means you have a card0
> with a PCM device what can potentially be used concurrently with the
> card1 exposing an offload device.
>
> Is there any sort of mutual exclusion to make sure the same USB endpoint
> is not used twice?
> > One would hope that when a device is opened the matching non-offloaded
> one (or ones in the case of implicit feedback) is disabled or marked as
> used?
>
> I would guess in your Android setup you have control on such behavior at
> the HAL level, but in the more generic Linux use I don't see what
> would orchestrate the use of two devices, and at the kernel level what
> would prevent it.
>
Still going through the comments and trying to address the suggestions
in the code, so will reply pack to those as I make the needed changes.

As for the above question, the following change was made with the
intention to prevent the above scenario.

https://lore.kernel.org/linux-usb/[email protected]/

Thanks
Wesley Cheng

2023-08-08 01:32:51

by Wesley Cheng

[permalink] [raw]
Subject: Re: [PATCH v4 10/32] ASoC: qcom: Add USB backend ASoC driver for Q6

Hi Pierre,

On 7/25/2023 1:45 AM, Pierre-Louis Bossart wrote:
>
>> +struct q6usb_port_data {
>> + struct q6afe_usb_cfg usb_cfg;
>> + struct snd_soc_usb *usb;
>> + struct q6usb_offload priv;
>> + int active_idx;
>
> what is an 'active_idx' ?
>
>

active_idx carries the USB sound card we're going to be offloading.

>> +static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx,
>> + int connected)
>> +{
>> + struct snd_soc_dapm_context *dapm;
>> + struct q6usb_port_data *data;
>> +
>> + dapm = snd_soc_component_get_dapm(usb->component);
>> + data = dev_get_drvdata(usb->component->dev);
>
> shouldn't you test that 'dapm' and 'data' are not NULL ?
>

q6usb_component_probe() would be the one that registers to SOC USB to
add this callback. At that time, the component's dev and dapm
references should be populated, so that should ensure that those are
valid. However, we could see that usb->component to be NULL, as that
assignment happens after adding the port. Instead I will add a check
for usb->component before attempting to access the dapm/data params.

Another thing I will modify is to add a component removal callback,
which will remove the SOC USB port. That will ensure that no
connection_cb() events are issued, so we don't run into any NULL pointer
issues during the remove path.

>> +
>> + if (connected) {
>
> this goes back to my earlier comment that you treat 'connected' as a
> boolean.
>

Done, changed to boolean.

>> + snd_soc_dapm_enable_pin(dapm, "USB_RX_BE");
>> + /* We only track the latest USB headset plugged in */
>> + data->active_idx = card_idx;
>> + } else {
>> + snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
>> + }
>> + snd_soc_dapm_sync(dapm);
>> +
>> + return 0;
>> +}
>> +
>> +static int q6usb_component_probe(struct snd_soc_component *component)
>> +{
>> + struct q6usb_port_data *data = dev_get_drvdata(component->dev);
>> + struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
>> +
>> + snd_soc_dapm_disable_pin(dapm, "USB_RX_BE");
>> + snd_soc_dapm_sync(dapm);
>> +
>> + data->usb = snd_soc_usb_add_port(component->dev, &data->priv, q6usb_alsa_connection_cb);
>> + if (IS_ERR(data->usb)) {
>> + dev_err(component->dev, "failed to add usb port\n");
>> + return -ENODEV;
>> + }
>> +
>> + data->usb->component = component;
>> +
>> + return 0;
>> +}
>> +
>> +static const struct snd_soc_component_driver q6usb_dai_component = {
>> + .probe = q6usb_component_probe,
>
> erm, if you have a .probe that adds a port, don't you need a remove that
> removes the same port, and sets the pin state as well?
>

Will add this as mentioned above.

>> + .name = "q6usb-dai-component",
>> + .dapm_widgets = q6usb_dai_widgets,
>> + .num_dapm_widgets = ARRAY_SIZE(q6usb_dai_widgets),
>> + .dapm_routes = q6usb_dapm_routes,
>> + .num_dapm_routes = ARRAY_SIZE(q6usb_dapm_routes),
>> + .of_xlate_dai_name = q6usb_audio_ports_of_xlate_dai_name,
>> +};
>> +
>> +static int q6usb_dai_dev_probe(struct platform_device *pdev)
>> +{
>> + struct device_node *node = pdev->dev.of_node;
>> + struct q6usb_port_data *data;
>> + struct device *dev = &pdev->dev;
>> + struct of_phandle_args args;
>> + int ret;
>> +
>> + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
>> + if (!data)
>> + return -ENOMEM;
>> +
>> + ret = of_property_read_u32(node, "qcom,usb-audio-intr-num",
>> + &data->priv.intr_num);
>> + if (ret) {
>> + dev_err(&pdev->dev, "failed to read intr num.\n");
>> + return ret;
>> + }
>> +
>> + ret = of_parse_phandle_with_fixed_args(node, "iommus", 1, 0, &args);
>> + if (ret < 0)
>> + data->priv.sid = -1;
>> + else
>> + data->priv.sid = args.args[0] & SID_MASK;
>> +
>> + data->priv.domain = iommu_get_domain_for_dev(&pdev->dev);
>> +
>> + data->priv.dev = dev;
>> + dev_set_drvdata(dev, data);
>> +
>> + ret = devm_snd_soc_register_component(dev, &q6usb_dai_component,
>> + q6usb_be_dais, ARRAY_SIZE(q6usb_be_dais));
>> + if (ret < 0)
>> + return ret;
>> +
>> + return 0;
>
> return devm_snd_soc_register_component
>
>> +}
>> +
>> +static int q6usb_dai_dev_remove(struct platform_device *pdev)
>> +{
>> + snd_soc_usb_remove_port(&pdev->dev);
>
> that seems wrong, the port is added in the component probe, not the
> platform device probe.
>

Will fix this.

Thanks
Wesley Cheng