Skip to contentSkip to conversation

ASoC: codecs: wcd937x: add USB Type-C analog audio mux support

PATCH Superseded Val Packett 5 participants 11 messages Oct 20

Subsystems (6)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover ASoC: codecs: wcd937x: add USB Type-C analog audio mux support

Currently only wcd939x has support for USB-C SBU audio in mainline, but the older WCD codecs can do the same thing 939x does with USBSS, with an external 3rd party mux instead. This series adds support for that based on wcd939x code.

Tested on sm7325-motorola-dubai.

v2:
* Pull R-b for dt-bindings
* Add missing depends on TYPEC || !TYPEC to kconfig (sashiko-bot)
* Do not leak the of_graph device_node (sashiko-bot)
* Guard against NULL wcd_mbhc / mbhc->cfg pointers because typec callbacks belong to the platform device which has a longer lifecycle than the soc codec part (sashiko-bot)
* Also apply the same leak/null fixes to wcd939x, keeping it in sync
v1: https://lore.kernel.org/all/[email protected]/

Thanks,
~val

---
Val Packett (4):
ASoC: dt-bindings: qcom,wcd937x: Add USB-C graph properties
ASoC: codecs: wcd937x: add USB Type-C analog audio mux support
ASoC: codecs: wcd-mbhc: guard against NULL cfg in
typec_report_(un)plug
ASoC: codecs: wcd939x: fix ref leak and NULL deref with USB-C audio

 .../bindings/sound/qcom,wcd937x.yaml          |  15 ++
 sound/soc/codecs/Kconfig                      |   2 +
 sound/soc/codecs/wcd-mbhc-v2.c                |   4 +-
 sound/soc/codecs/wcd937x.c                    | 167 +++++++++++++++++-
 sound/soc/codecs/wcd939x.c                    |  18 +-
 5 files changed, 194 insertions(+), 12 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 1/4 ASoC: dt-bindings: qcom,wcd937x: Add USB-C graph properties

This codec can be used with a USB-C SBU mux to support analog headphone adapters. Describe the mode-switch, orientation-switch and port properties similarly to wcd939x.

---
 .../devicetree/bindings/sound/qcom,wcd937x.yaml   | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
Documentation/devicetree/bindings/sound/qcom,wcd937x.yaml +15 -0
index f94203798f24..f15b9a9d90b1 100644
@@ -25,6 +25,21 @@ properties:
           - const: qcom,wcd9375-codec
           - const: qcom,wcd9370-codec
 
+  mode-switch:
+    description: Flag the port as possible handler of altmode switching
+    type: boolean
+
+  orientation-switch:
+    description: Flag the port as possible handler of orientation switching
+    type: boolean
+
+  port:
+    $ref: /schemas/graph.yaml#/properties/port
+    description:
+      A port node to link the WCD937x Codec node to an external USB-C MUX IC for
+      handling altmode muxing and orientation switching to detect and enable
+      Audio Accessory Mode.
+
   vdd-px-supply:
     description: A reference to the 1.8V I/O supply
 
Signature · 1 line
2.55.0
#2 lore raw

PATCH v2 2/4 ASoC: codecs: wcd937x: add USB Type-C analog audio mux support

In reply to Val Packett

On mobile devices, this codec is frequently paired with a USB-C SBU mux like the FSA4480 to support analog headphone adapters. To support this, register the codec in the Type-C graph whenever one is present in the device tree, handle the hotplug notifications and report detected GND-MIC pin swap back to the external mux as Type-C orientation.

Based on code from the wcd939x driver.

---
 sound/soc/codecs/Kconfig   |   2 +
 sound/soc/codecs/wcd937x.c | 167 +++++++++++++++++++++++++++++++++++--
 2 files changed, 163 insertions(+), 6 deletions(-)
sound/soc/codecs/Kconfig +2 -0
index ba59dabeeccd..b77e62b0b517 100644
@@ -2497,11 +2497,13 @@ config SND_SOC_WCD937X
 	depends on SND_SOC_WCD937X_SDW
 	tristate
 	depends on SOUNDWIRE || !SOUNDWIRE
+	depends on TYPEC || !TYPEC
 	select SND_SOC_WCD_CLASSH
 	select SND_SOC_WCD_COMMON
 
 config SND_SOC_WCD937X_SDW
 	tristate "WCD9370/WCD9375 Codec - SDW"
+	depends on TYPEC || !TYPEC
 	select SND_SOC_WCD937X
 	select SND_SOC_WCD_MBHC
 	select REGMAP_IRQ
sound/soc/codecs/wcd937x.c +161 -6

266 lines not on this page · raw message

Signature · 1 line
2.55.0
#4 lore raw

PATCH v2 3/4 ASoC: codecs: wcd-mbhc: guard against NULL cfg in typec_report_(un)plug

In reply to Val Packett

mbhc->cfg is only set by wcd_mbhc_start() which is called by the codec's set_jack op. wcd_mbhc_typec_report_plug/unplug are called by the platform device's typec callbacks, which are not synchronized with codec ops, so a hotplug could be reported before set_jack was called. Though unlikely to happen in practice, add a check to be safe.

Found by sashiko-bot via review of wcd937x Type-C integration patches.

Fixes 0c105997eefd ("ASoC: codec: wcd-mbhc-v2: add support when connected behind an USB-C audio mux")
---
 sound/soc/codecs/wcd-mbhc-v2.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
sound/soc/codecs/wcd-mbhc-v2.c +2 -2
index 850be84d65ce..fd40c1686d66 100644
@@ -594,7 +594,7 @@ static irqreturn_t wcd_mbhc_mech_plug_detect_irq(int irq, void *data)
 int wcd_mbhc_typec_report_unplug(struct wcd_mbhc *mbhc)
 {
 
-	if (!mbhc || !mbhc->cfg->typec_analog_mux)
+	if (!mbhc || !mbhc->cfg || !mbhc->cfg->typec_analog_mux)
 		return -EINVAL;
 
 	if (mbhc->mbhc_cb->clk_setup)
@@ -611,7 +611,7 @@ EXPORT_SYMBOL_GPL(wcd_mbhc_typec_report_unplug);
 
 int wcd_mbhc_typec_report_plug(struct wcd_mbhc *mbhc)
 {
-	if (!mbhc || !mbhc->cfg->typec_analog_mux)
+	if (!mbhc || !mbhc->cfg || !mbhc->cfg->typec_analog_mux)
 		return -EINVAL;
 
 	if (mbhc->mbhc_cb->clk_setup)
Signature · 1 line
2.55.0
#6 lore raw

PATCH v2 4/4 ASoC: codecs: wcd939x: fix ref leak and NULL deref with USB-C audio

In reply to Val Packett

Some code issues were found during review of a port of typec code from wcd939x to wcd937x, pointed out by sashiko-bot:

* wcd939x->wcd_mbhc may be accessed by the typec callbacks which are independent of the ASoC codec lifecycle, don't leave it as NULL/PTR_ERR;
* wcd939x->typec_switch reference was leaked on unbind, put it there;
* the device_node reference returned by of_graph_get_remote_node() was being leaked, add a __free annotation to the declaration.

Fix them in wcd939x as well.

Fixes 10f514bd172a ("ASoC: codecs: Add WCD939x Codec driver")
---
 sound/soc/codecs/wcd939x.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
sound/soc/codecs/wcd939x.c +14 -4
index 26fe0b6313cd..56e041e70fd4 100644
@@ -2466,6 +2466,7 @@ static int wcd939x_mbhc_init(struct snd_soc_component *component)
 {
 	struct wcd939x_priv *wcd939x = snd_soc_component_get_drvdata(component);
 	struct wcd_mbhc_intr *intr_ids = &wcd939x->intr_ids;
+	struct wcd_mbhc *mbhc;
 
 	intr_ids->mbhc_sw_intr = regmap_irq_get_virq(wcd939x->irq_chip,
 						     WCD939X_IRQ_MBHC_SW_DET);
@@ -2482,9 +2483,12 @@ static int wcd939x_mbhc_init(struct snd_soc_component *component)
 	intr_ids->hph_right_ocp = regmap_irq_get_virq(wcd939x->irq_chip,
 						      WCD939X_IRQ_HPHR_OCP_INT);
 
-	wcd939x->wcd_mbhc = wcd_mbhc_init(component, &mbhc_cb, intr_ids, wcd_mbhc_fields, true);
-	if (IS_ERR(wcd939x->wcd_mbhc))
-		return PTR_ERR(wcd939x->wcd_mbhc);
+	mbhc = wcd_mbhc_init(component, &mbhc_cb, intr_ids, wcd_mbhc_fields, true);
+	if (IS_ERR(mbhc))
+		return PTR_ERR(mbhc);
+
+	/* May be accessed by platform dev typec callbacks, only set if valid */
+	wcd939x->wcd_mbhc = mbhc;
 
 	snd_soc_add_component_controls(component, impedance_detect_controls,
 				       ARRAY_SIZE(impedance_detect_controls));
@@ -2499,6 +2503,8 @@ static void wcd939x_mbhc_deinit(struct snd_soc_component *component)
 	struct wcd939x_priv *wcd939x = snd_soc_component_get_drvdata(component);
 
 	wcd_mbhc_deinit(wcd939x->wcd_mbhc);
+	/* May be accessed by platform dev typec callbacks, clear */
+	wcd939x->wcd_mbhc = NULL;
 }
 
 /* END MBHC */
@@ -3171,7 +3177,7 @@ static int wcd939x_populate_dt_data(struct wcd939x_priv *wcd939x, struct device
 {
 	struct wcd_mbhc_config *cfg = &wcd939x->mbhc_cfg;
 #if IS_ENABLED(CONFIG_TYPEC)
-	struct device_node *np;
+	struct device_node *np __free(device_node);
 #endif /* CONFIG_TYPEC */
 	int ret;
 
@@ -3434,6 +3440,10 @@ static void wcd939x_unbind(struct device *dev)
 	put_device(wcd939x->txdev);
 	put_device(wcd939x->rxdev);
 	component_unbind_all(dev, wcd939x);
+#if IS_ENABLED(CONFIG_TYPEC)
+	if (wcd939x->typec_analog_mux)
+		typec_switch_put(wcd939x->typec_switch);
+#endif /* CONFIG_TYPEC */
 }
 
 static const struct component_master_ops wcd939x_comp_ops = {
Signature · 1 line
2.55.0
#3 lore raw
In reply to Val Packett
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] ASoC: wcd937x: uninitialized __free(device_node) pointer in wcd937x_probe() - [High] ASoC: codecs…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] ASoC: wcd937x: uninitialized __free(device_node) pointer in wcd937x_probe()
- [High] ASoC: codecs: wcd937x: use-after-free race in wcd937x_mbhc_deinit() --

commit 4c12f167e90fb501eb42394e2202994cfc931062
Author: Val Packett <[email protected]>

ASoC: codecs: wcd937x: add USB Type-C analog audio mux support

This commit adds USB Type-C analog audio mux and orientation switch support to the WCD937x codec driver, handling hotplug notifications via MBHC and reporting GND-MIC pin swap orientation back to the external mux.

Quoted 4 lines
sound/soc/codecs/wcd937x.c +0 -0
index 7ffbb445f8ac4..48f8852d34513 100644

[ ... ]

Quoted 36 lines
sound/soc/codecs/wcd937x.c +25 -0
@@ -2038,6 +2052,8 @@ static void wcd937x_mbhc_deinit(struct snd_soc_component *component)
 	struct wcd937x_priv *wcd937x = snd_soc_component_get_drvdata(component);
 
 	wcd_mbhc_deinit(wcd937x->wcd_mbhc);
+	/* May be accessed by platform dev typec callbacks, clear */
+	wcd937x->wcd_mbhc = NULL;
 }
 
 /* END MBHC */
@@ -2634,6 +2650,40 @@ static const struct snd_soc_component_driver soc_codec_dev_wcd937x = {
 	.endianness = 1,
 };
 
+#if IS_ENABLED(CONFIG_TYPEC)
+/* Get USB-C plug orientation to provide swap event for MBHC */
+static int wcd937x_typec_switch_set(struct typec_switch_dev *sw,
+				    enum typec_orientation orientation)
+{
+	struct wcd937x_priv *wcd937x = typec_switch_get_drvdata(sw);
+
+	wcd937x->typec_orientation = orientation;
+
+	return 0;
+}
+
+static int wcd937x_typec_mux_set(struct typec_mux_dev *mux,
+				 struct typec_mux_state *state)
+{
+	struct wcd937x_priv *wcd937x = typec_mux_get_drvdata(mux);
+	unsigned int previous_mode = wcd937x->typec_mode;
+
+	if (!wcd937x->wcd_mbhc)
+		return -EINVAL;
+
+	if (wcd937x->typec_mode != state->mode) {
+		wcd937x->typec_mode = state->mode;

sound/soc/codecs/wcd937x.c static const struct snd_soc_component_driver soc_codec_dev_wcd937x = { · line 2685

Quoted 10 lines
sound/soc/codecs/wcd937x.c +10 -0
+
+		if (wcd937x->typec_mode == TYPEC_MODE_AUDIO)
+			return wcd_mbhc_typec_report_plug(wcd937x->wcd_mbhc);
+		else if (previous_mode == TYPEC_MODE_AUDIO)
+			return wcd_mbhc_typec_report_unplug(wcd937x->wcd_mbhc);
+	}
+
+	return 0;
+}
+#endif /* CONFIG_TYPEC */

[Severity: High]
Can wcd937x_typec_mux_set() race with wcd937x_mbhc_deinit() and cause a use-after-free on wcd937x->wcd_mbhc?

The Type-C mux is registered in wcd937x_probe() via devres, so it remains active during component teardown:

wcd937x_unbind()
snd_soc_unregister_component()

    wcd937x_soc_codec_remove()
      wcd937x_mbhc_deinit()
        wcd_mbhc_deinit(wcd937x->wcd_mbhc)

Inside wcd_mbhc_deinit(), the driver frees IRQs, cancels mbhc->mbhc_plug_detect_work, and calls kfree(mbhc), all before wcd937x_mbhc_deinit() sets wcd937x->wcd_mbhc = NULL:

sound/soc/codecs/wcd-mbhc-v2.c:wcd_mbhc_deinit() {
	...
	mutex_lock(&mbhc->lock);
	wcd_cancel_hs_detect_plug(mbhc, &mbhc->correct_plug_swch);
	cancel_work_sync(&mbhc->mbhc_plug_detect_work);
	mutex_unlock(&mbhc->lock);

	kfree(mbhc);
}

Because there is no synchronization between wcd937x_typec_mux_set() and wcd937x_mbhc_deinit(), a concurrent wcd937x_typec_mux_set() callback can observe a non-NULL wcd937x->wcd_mbhc while or after wcd_mbhc_deinit() runs, calling wcd_mbhc_typec_report_plug() or wcd_mbhc_typec_report_unplug() on freed memory and scheduling mbhc->mbhc_plug_detect_work after cancel_work_sync() and kfree(mbhc).

[ ... ]

Quoted 19 lines
sound/soc/codecs/wcd937x.c +8 -0
@@ -2907,6 +3043,9 @@ static int wcd937x_probe(struct platform_device *pdev)
 	struct device *dev = &pdev->dev;
 	struct wcd937x_priv *wcd937x;
 	struct wcd_mbhc_config *cfg;
+#if IS_ENABLED(CONFIG_TYPEC)
+	struct device_node *np __free(device_node);
+#endif /* CONFIG_TYPEC */
 	int ret;
 
 	wcd937x = devm_kzalloc(dev, sizeof(*wcd937x), GFP_KERNEL);
@@ -2951,6 +3090,22 @@ static int wcd937x_probe(struct platform_device *pdev)
 
 	wcd_dt_parse_mbhc_data(dev, &wcd937x->mbhc_cfg);
 
+#if IS_ENABLED(CONFIG_TYPEC)
+	/*
+	 * If the node has a port and a valid remote endpoint,
+	 * consider HP lines as connected to a USB-C SBU mux
+	 */

sound/soc/codecs/wcd937x.c static int wcd937x_probe(struct platform_device *pdev) · line 3107

Quoted 10 lines
sound/soc/codecs/wcd937x.c +10 -0
+	np = of_graph_get_remote_node(dev->of_node, 0, 0);
+	if (np) {
+		wcd937x->typec_analog_mux = true;
+		cfg->typec_analog_mux = true;
+
+		ret = wcd937x_add_typec(wcd937x, dev);
+		if (ret)
+			return ret;
+	}
+#endif /* CONFIG_TYPEC */

[Severity: High]
Does wcd937x_probe() pass an uninitialized np pointer to of_node_put() on early return when CONFIG_TYPEC is enabled?

In wcd937x_probe(), np is declared with __free(device_node) without being initialized to NULL, and is not assigned until of_graph_get_remote_node() is called later in the function.

Between the declaration and that assignment, there are five early return paths if devm_kzalloc(), devm_gpiod_get(), devm_gpiod_get_optional(), devm_regulator_bulk_get_enable(), or wcd_dt_parse_micbias_info() fails:

sound/soc/codecs/wcd937x.c:wcd937x_probe() {
	...
	wcd937x = devm_kzalloc(dev, sizeof(*wcd937x), GFP_KERNEL);
	if (!wcd937x)
		return -ENOMEM;
	...
	wcd937x->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
	if (IS_ERR(wcd937x->reset_gpio))
		return dev_err_probe(dev, PTR_ERR(wcd937x->reset_gpio),
				     "failed to reset wcd gpio\n");
	...
}

If any of those early returns is taken, the __free(device_node) cleanup handler runs on the uninitialized stack variable np. Should np be initialized to NULL at declaration, or declared where of_graph_get_remote_node() is called?

[ ... ]

Quoted 4 lines
sound/soc/codecs/wcd937x.c +0 -0
 	ret = wcd937x_add_slave_components(wcd937x, dev, &match);
 	if (ret)
 		return ret;

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw

Re: [PATCH v2 4/4] ASoC: codecs: wcd939x: fix ref leak and NULL deref with USB-C audio

In reply to Val Packett
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] ASoC: wcd939x: uninitialized __free() pointer in wcd939x_populate_dt_data() - [High] ASoC…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] ASoC: wcd939x: uninitialized __free() pointer in wcd939x_populate_dt_data()
- [High] ASoC: wcd939x: use-after-free race in wcd939x_mbhc_deinit() with typec mux --

commit 263e4868f28e72190c4609440782fec0a736dcae
Author: Val Packett <[email protected]>

ASoC: codecs: wcd939x: fix ref leak and NULL deref with USB-C audio

This commit addresses three issues with USB-C audio support in wcd939x:
preventing wcd939x->wcd_mbhc from holding an error pointer or stale pointer for Type-C callbacks, releasing wcd939x->typec_switch on unbind, and adding a __free(device_node) cleanup annotation for the node returned by of_graph_get_remote_node().

Quoted 4 lines
sound/soc/codecs/wcd939x.c +0 -0
index 2d463f062e6d7..17503e4785e2c 100644

[ ... ]

sound/soc/codecs/wcd939x.c static void wcd939x_mbhc_deinit(struct snd_soc_component *component) · line 2512

Quoted 7 lines
sound/soc/codecs/wcd939x.c +2 -0
@@ -2503,6 +2507,8 @@ static void wcd939x_mbhc_deinit(struct snd_soc_component *component)
 	struct wcd939x_priv *wcd939x = snd_soc_component_get_drvdata(component);
 
 	wcd_mbhc_deinit(wcd939x->wcd_mbhc);
+	/* May be accessed by platform dev typec callbacks, clear */
+	wcd939x->wcd_mbhc = NULL;
 }

[Severity: High]
Can wcd939x_typec_mux_set() race with wcd939x_mbhc_deinit() and access a freed wcd939x->wcd_mbhc?

In wcd939x_mbhc_deinit(), wcd939x->wcd_mbhc is only set to NULL after wcd_mbhc_deinit() has already returned, and without synchronization against wcd939x_typec_mux_set(). Inside wcd_mbhc_deinit(), execution sleeps across free_irq() and cancel_work_sync(&mbhc->mbhc_plug_detect_work) before calling kfree(mbhc):

sound/soc/codecs/wcd-mbhc-v2.c:wcd_mbhc_deinit() {
	...
	mutex_lock(&mbhc->lock);
	wcd_cancel_hs_detect_plug(mbhc,	&mbhc->correct_plug_swch);
	cancel_work_sync(&mbhc->mbhc_plug_detect_work);
	mutex_unlock(&mbhc->lock);

	kfree(mbhc);
}

During that window, wcd939x_typec_mux_set() can still see a non-NULL wcd939x->wcd_mbhc:

sound/soc/codecs/wcd939x.c:wcd939x_typec_mux_set() {
	...
	if (!wcd939x->wcd_mbhc)
		return -EINVAL;

	if (wcd939x->typec_mode != state->mode) {
		wcd939x->typec_mode = state->mode;

		if (wcd939x->typec_mode == TYPEC_MODE_AUDIO)
			return wcd_mbhc_typec_report_plug(wcd939x->wcd_mbhc);
		else if (previous_mode == TYPEC_MODE_AUDIO)
			return wcd_mbhc_typec_report_unplug(wcd939x->wcd_mbhc);
	}
	...
}

Wouldn't a concurrent wcd939x_typec_mux_set() call dereference freed mbhc memory or reschedule mbhc_plug_detect_work after cancel_work_sync() has completed?

sound/soc/codecs/wcd939x.c static int wcd939x_populate_dt_data(struct wcd939x_priv *wcd939x, struct device · line 3186

Quoted 10 lines
sound/soc/codecs/wcd939x.c +1 -1
 /* END MBHC */
@@ -3175,7 +3181,7 @@ static int wcd939x_populate_dt_data(struct wcd939x_priv *wcd939x, struct device
 {
 	struct wcd_mbhc_config *cfg = &wcd939x->mbhc_cfg;
 #if IS_ENABLED(CONFIG_TYPEC)
-	struct device_node *np;
+	struct device_node *np __free(device_node);
 #endif /* CONFIG_TYPEC */
 	int ret;

[Severity: High]
Does wcd939x_populate_dt_data() pass an uninitialized np pointer to of_node_put() on early return?

Here np is declared with __free(device_node) without being initialized to NULL, while three early return paths precede its assignment at of_graph_get_remote_node():

sound/soc/codecs/wcd939x.c:wcd939x_populate_dt_data() {
	...
	wcd939x->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
	if (IS_ERR(wcd939x->reset_gpio))
		return dev_err_probe(dev, PTR_ERR(wcd939x->reset_gpio),
				     "Failed to get reset gpio\n");

	ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(wcd939x_supplies),
					     wcd939x_supplies);
	if (ret)
		return dev_err_probe(dev, ret, "Failed to get and enable supplies\n");

	ret = wcd_dt_parse_micbias_info(&wcd939x->common);
	if (ret)
		return dev_err_probe(dev, ret, "Failed to get micbias\n");
	...
	np = of_graph_get_remote_node(dev->of_node, 0, 0);
}

If any of those three calls fails or defers probe when CONFIG_TYPEC is enabled, the cleanup handler will run on an uninitialized np pointer. Should np be initialized to NULL (or declared at the point of assignment)?

[ ... ]

Quoted 1 line

dari

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Reviewed-by on v2 4/4
#8 lore raw

Re: [PATCH v2 4/4] ASoC: codecs: wcd939x: fix ref leak and NULL deref with USB-C audio

In reply to Val Packett
Quoted 65 lines · Val Packett

Some code issues were found during review of a port of typec code from wcd939x to wcd937x, pointed out by sashiko-bot:

* wcd939x->wcd_mbhc may be accessed by the typec callbacks which are independent of the ASoC codec lifecycle, don't leave it as NULL/PTR_ERR;
* wcd939x->typec_switch reference was leaked on unbind, put it there;
* the device_node reference returned by of_graph_get_remote_node() was being leaked, add a __free annotation to the declaration.

Fix them in wcd939x as well.

Fixes 10f514bd172a ("ASoC: codecs: Add WCD939x Codec driver")
---
  sound/soc/codecs/wcd939x.c | 18 ++++++++++++++----
  1 file changed, 14 insertions(+), 4 deletions(-)
sound/soc/codecs/wcd939x.c +10 -4
index 26fe0b6313cd..56e041e70fd4 100644
@@ -2466,6 +2466,7 @@ static int wcd939x_mbhc_init(struct snd_soc_component *component)
  {
  	struct wcd939x_priv *wcd939x = snd_soc_component_get_drvdata(component);
  	struct wcd_mbhc_intr *intr_ids = &wcd939x->intr_ids;
+	struct wcd_mbhc *mbhc;
 
  	intr_ids->mbhc_sw_intr = regmap_irq_get_virq(wcd939x->irq_chip,
  						     WCD939X_IRQ_MBHC_SW_DET);
@@ -2482,9 +2483,12 @@ static int wcd939x_mbhc_init(struct snd_soc_component *component)
  	intr_ids->hph_right_ocp = regmap_irq_get_virq(wcd939x->irq_chip,
  						      WCD939X_IRQ_HPHR_OCP_INT);
 
-	wcd939x->wcd_mbhc = wcd_mbhc_init(component, &mbhc_cb, intr_ids, wcd_mbhc_fields, true);
-	if (IS_ERR(wcd939x->wcd_mbhc))
-		return PTR_ERR(wcd939x->wcd_mbhc);
+	mbhc = wcd_mbhc_init(component, &mbhc_cb, intr_ids, wcd_mbhc_fields, true);
+	if (IS_ERR(mbhc))
+		return PTR_ERR(mbhc);
+
+	/* May be accessed by platform dev typec callbacks, only set if valid */
+	wcd939x->wcd_mbhc = mbhc;
 
  	snd_soc_add_component_controls(component, impedance_detect_controls,
  				       ARRAY_SIZE(impedance_detect_controls));
@@ -2499,6 +2503,8 @@ static void wcd939x_mbhc_deinit(struct snd_soc_component *component)
  	struct wcd939x_priv *wcd939x = snd_soc_component_get_drvdata(component);
 
  	wcd_mbhc_deinit(wcd939x->wcd_mbhc);
+	/* May be accessed by platform dev typec callbacks, clear */
+	wcd939x->wcd_mbhc = NULL;
  }
 
  /* END MBHC */
@@ -3171,7 +3177,7 @@ static int wcd939x_populate_dt_data(struct wcd939x_priv *wcd939x, struct device
  {
  	struct wcd_mbhc_config *cfg = &wcd939x->mbhc_cfg;
  #if IS_ENABLED(CONFIG_TYPEC)
-	struct device_node *np;
+	struct device_node *np __free(device_node);
  #endif /* CONFIG_TYPEC */
  	int ret;
 

sound/soc/codecs/wcd939x.c static void wcd939x_unbind(struct device *dev) · line 3449

Quoted 10 lines
sound/soc/codecs/wcd939x.c +4 -0
@@ -3434,6 +3440,10 @@ static void wcd939x_unbind(struct device *dev)
  	put_device(wcd939x->txdev);
  	put_device(wcd939x->rxdev);
  	component_unbind_all(dev, wcd939x);
+#if IS_ENABLED(CONFIG_TYPEC)
+	if (wcd939x->typec_analog_mux)
+		typec_switch_put(wcd939x->typec_switch);
+#endif /* CONFIG_TYPEC */
  }
 
  static const struct component_master_ops wcd939x_comp_ops = {

Thanks,
Neil

Reviewed-by on v2 3/4
#5 lore raw

Re: [PATCH v2 3/4] ASoC: codecs: wcd-mbhc: guard against NULL cfg in typec_report_(un)plug

In reply to Val Packett
Quoted 29 lines · Val Packett

mbhc->cfg is only set by wcd_mbhc_start() which is called by the codec's set_jack op. wcd_mbhc_typec_report_plug/unplug are called by the platform device's typec callbacks, which are not synchronized with codec ops, so a hotplug could be reported before set_jack was called. Though unlikely to happen in practice, add a check to be safe.

Found by sashiko-bot via review of wcd937x Type-C integration patches.

Fixes 0c105997eefd ("ASoC: codec: wcd-mbhc-v2: add support when connected behind an USB-C audio mux")
---
  sound/soc/codecs/wcd-mbhc-v2.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
sound/soc/codecs/wcd-mbhc-v2.c +1 -1
index 850be84d65ce..fd40c1686d66 100644
@@ -594,7 +594,7 @@ static irqreturn_t wcd_mbhc_mech_plug_detect_irq(int irq, void *data)
  int wcd_mbhc_typec_report_unplug(struct wcd_mbhc *mbhc)
  {
 
-	if (!mbhc || !mbhc->cfg->typec_analog_mux)
+	if (!mbhc || !mbhc->cfg || !mbhc->cfg->typec_analog_mux)
  		return -EINVAL;
 
  	if (mbhc->mbhc_cb->clk_setup)

sound/soc/codecs/wcd-mbhc-v2.c EXPORT_SYMBOL_GPL(wcd_mbhc_typec_report_unplug); · line 617

Quoted 8 lines
sound/soc/codecs/wcd-mbhc-v2.c +1 -1
@@ -611,7 +611,7 @@ EXPORT_SYMBOL_GPL(wcd_mbhc_typec_report_unplug);
 
  int wcd_mbhc_typec_report_plug(struct wcd_mbhc *mbhc)
  {
-	if (!mbhc || !mbhc->cfg->typec_analog_mux)
+	if (!mbhc || !mbhc->cfg || !mbhc->cfg->typec_analog_mux)
  		return -EINVAL;
 
  	if (mbhc->mbhc_cb->clk_setup)

Thanks,
Neil

#9 lore raw

Re: [PATCH v2 4/4] ASoC: codecs: wcd939x: fix ref leak and NULL deref with USB-C audio

In reply to Val Packett
Quoted 2 lines

Some code issues were found during review of a port of typec code from wcd939x to wcd937x, pointed out by sashiko-bot:

…

Would there be a need to offer corresponding adjustments as separate patches?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.3#n81

Regards,
Markus

maintainer
#10 lore raw

Re: [PATCH v2 4/4] ASoC: codecs: wcd939x: fix ref leak and NULL deref with USB-C audio

Quoted 6 lines · Markus Elfring
Quoted 2 lines

Some code issues were found during review of a port of typec code from wcd939x to wcd937x, pointed out by sashiko-bot:

…

Would there be a need to offer corresponding adjustments as separate patches?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.3#n81

Feel free to ignore Markus, he has a long history of sending unhelpful review comments and continues to ignore repeated requests to stop.