Skip to contentSkip to conversation

ASoC: rt721-sdca: handle HID button read errors

PATCH In review Avi Weiss 3 participants 6 messages Nov 1 – Nov 3

Subsystems (3)
lore
Share & tools
lore

Avi Weiss

author
#0 lore raw

PATCH v2 1/4 ASoC: rt721-sdca: handle HID button read errors

rt_sdca_button_detect() returns zero when a regmap read fails, so RT721 treats the error as a valid no-button result.

Return the original error and skip button reporting when a read fails. Still report jack changes, and clear button state if the headset is removed.

Fixes bbca8e7050e0 ("ASoC: rt-sdw-common: Common functions for Realtek soundwire driver")

---
Changes in v2:
- report jack state on button read failures even when SDCA_0 is clear, fixing the missed unplug identified during review of v1
- document negative error returns from rt_sdca_button_detect()

 sound/soc/codecs/rt-sdw-common.c |  6 +++++-
 sound/soc/codecs/rt721-sdca.c    | 11 +++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
sound/soc/codecs/rt-sdw-common.c +5 -1
index ad61943ce75ff..2b24d6ac8f143 100644
@@ -187,6 +187,7 @@ EXPORT_SYMBOL_GPL(rt_sdca_headset_detect);
  *
  * A button type will be returned regarding to buffer,
  * it returns zero if buffer cannot be recognized.
+ * A negative errno is returned if a register read fails.
  */
 int rt_sdca_button_detect(struct regmap *map, unsigned int entity_id,
 	unsigned int hid_buf_addr, unsigned int hid_id)
@@ -199,7 +200,7 @@ int rt_sdca_button_detect(struct regmap *map, unsigned int entity_id,
 	ret = regmap_read(map, SDW_SDCA_CTL(SDCA_NUM_HID, entity_id,
 			RT_SDCA_CTL_HIDTX_CURRENT_OWNER, 0), &owner);
 	if (ret < 0)
-		return 0;
+		return ret;
 
 	/* if owner is device then there is no button event from device */
 	if (owner == 1)
@@ -229,6 +230,9 @@ int rt_sdca_button_detect(struct regmap *map, unsigned int entity_id,
 			SDW_SDCA_CTL(SDCA_NUM_HID, entity_id,
 				RT_SDCA_CTL_HIDTX_CURRENT_OWNER, 0), 0x01);
 
+	if (ret < 0)
+		return ret;
+
 	return btn_type;
 }
 EXPORT_SYMBOL_GPL(rt_sdca_button_detect);
sound/soc/codecs/rt721-sdca.c +11 -0
index 9f567caecb70a..82ef8245474cb 100644
@@ -248,6 +248,17 @@ static void rt721_sdca_jack_detect_handler(struct work_struct *work)
 					RT721_SDCA_ENT_HID01, RT721_BUF_ADDR_HID1,
 					RT721_SDCA_HID_ID);
 
+	if (btn_type < 0) {
+		unsigned int mask = SND_JACK_HEADSET;
+
+		if (rt721->jack_type != SND_JACK_HEADSET)
+			mask |= SND_JACK_BTN_0 | SND_JACK_BTN_1 |
+				SND_JACK_BTN_2 | SND_JACK_BTN_3;
+
+		snd_soc_jack_report(rt721->hs_jack, rt721->jack_type, mask);
+		return;
+	}
+
 	if (rt721->jack_type == 0)
 		btn_type = 0;
 
author
#1 lore raw

PATCH v2 2/4 ASoC: rt711-sdca: handle HID button read errors

rt711_sdca_button_detect() returns zero when a regmap read fails, so the jack handler cannot distinguish the error from a valid no-button result.

Return the original error and make the button result signed. Skip button reporting on read failure, but still report jack changes and clear button state when the headset is removed.

Fixes 7ad4d237e7c4 ("ASoC: rt711-sdca: Add RT711 SDCA vendor-specific driver")

---
Changes in v2:
- report jack state on button read failures regardless of SDCA_0, preserving the original jack reporting behavior

 sound/soc/codecs/rt711-sdca.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
sound/soc/codecs/rt711-sdca.c +18 -4
index 9781f289f8adc..f6c8377c51ab5 100644
@@ -184,10 +184,10 @@ static int rt711_sdca_calibration(struct rt711_sdca_priv *rt711)
 	return ret;
 }
 
-static unsigned int rt711_sdca_button_detect(struct rt711_sdca_priv *rt711)
+static int rt711_sdca_button_detect(struct rt711_sdca_priv *rt711)
 {
-	unsigned int btn_type = 0, offset, idx, val, owner;
-	int ret;
+	unsigned int offset, idx, val, owner;
+	int btn_type = 0, ret;
 	unsigned char buf[3];
 
 	/* get current UMP message owner */
@@ -195,7 +195,7 @@ static unsigned int rt711_sdca_button_detect(struct rt711_sdca_priv *rt711)
 		SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01, RT711_SDCA_CTL_HIDTX_CURRENT_OWNER, 0),
 		&owner);
 	if (ret < 0)
-		return 0;
+		return ret;
 
 	/* if owner is device then there is no button event from device */
 	if (owner == 1)
@@ -259,6 +259,9 @@ static unsigned int rt711_sdca_button_detect(struct rt711_sdca_priv *rt711)
 			SDW_SDCA_CTL(FUNC_NUM_HID, RT711_SDCA_ENT_HID01,
 				RT711_SDCA_CTL_HIDTX_SET_OWNER_TO_DEVICE, 0), 0x01);
 
+	if (ret < 0)
+		return ret;
+
 	return btn_type;
 }
 
@@ -330,6 +333,17 @@ static void rt711_sdca_jack_detect_handler(struct work_struct *work)
 	if (rt711->scp_sdca_stat2 & SDW_SCP_SDCA_INT_SDCA_8)
 		btn_type = rt711_sdca_button_detect(rt711);
 
+	if (btn_type < 0) {
+		unsigned int mask = SND_JACK_HEADSET;
+
+		if (rt711->jack_type != SND_JACK_HEADSET)
+			mask |= SND_JACK_BTN_0 | SND_JACK_BTN_1 |
+				SND_JACK_BTN_2 | SND_JACK_BTN_3;
+
+		snd_soc_jack_report(rt711->hs_jack, rt711->jack_type, mask);
+		return;
+	}
+
 	if (rt711->jack_type == 0)
 		btn_type = 0;
 
#2 lore raw

Re: [PATCH v2 2/4] ASoC: rt711-sdca: handle HID button read errors

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] ASoC: rt711-sdca: no-op error path in rt711_sdca_jack_detect_handler() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] ASoC: rt711-sdca: no-op error path in rt711_sdca_jack_detect_handler() --

commit 54e5617fc05f75e04b8c64c9ddcc952bcf29b404
Author: Avi Weiss <[email protected]>

ASoC: rt711-sdca: handle HID button read errors

This commit changes rt711_sdca_button_detect() to return negative error codes on regmap_read() failures instead of zero, and updates rt711_sdca_jack_detect_handler() to handle negative btn_type values.

Quoted 4 lines
sound/soc/codecs/rt711-sdca.c +0 -0
index 9781f289f8adc..f6c8377c51ab5 100644

[ ... ]

Quoted 4 lines
sound/soc/codecs/rt711-sdca.c +0 -0
@@ -330,6 +333,17 @@ static void rt711_sdca_jack_detect_handler(struct work_struct *work)
 	if (rt711->scp_sdca_stat2 & SDW_SCP_SDCA_INT_SDCA_8)
 		btn_type = rt711_sdca_button_detect(rt711);
 

sound/soc/codecs/rt711-sdca.c static void rt711_sdca_jack_detect_handler(struct work_struct *work) · line 345

Quoted 10 lines
sound/soc/codecs/rt711-sdca.c +10 -0
+	if (btn_type < 0) {
+		unsigned int mask = SND_JACK_HEADSET;
+
+		if (rt711->jack_type != SND_JACK_HEADSET)
+			mask |= SND_JACK_BTN_0 | SND_JACK_BTN_1 |
+				SND_JACK_BTN_2 | SND_JACK_BTN_3;
+
+		snd_soc_jack_report(rt711->hs_jack, rt711->jack_type, mask);
+		return;
+	}

[Severity: Medium]
Does this btn_type < 0 branch actually change the jack or button reporting behavior when regmap_read() fails in rt711_sdca_button_detect()?

In sound/soc/codecs/rt711-sdca.c, both rt711_sdca_jack_detect_handler() and rt711_sdca_btn_check_handler() immediately follow every non-zero button press report with a synchronous release report that clears SND_JACK_BTN_0..3:

    snd_soc_jack_report(rt711->hs_jack, rt711->jack_type | btn_type,
            SND_JACK_HEADSET |
            SND_JACK_BTN_0 | SND_JACK_BTN_1 |
            SND_JACK_BTN_2 | SND_JACK_BTN_3);

    if (btn_type) {
        /* button released */
        snd_soc_jack_report(rt711->hs_jack, rt711->jack_type,
            SND_JACK_HEADSET |
            SND_JACK_BTN_0 | SND_JACK_BTN_1 |
            SND_JACK_BTN_2 | SND_JACK_BTN_3);

Because SND_JACK_BTN_0..3 are never left set in rt711->hs_jack->status across work invocations, and snd_soc_jack_report() -> snd_jack_report() reports the full jack->status for all keys and switches in jack->type regardless of mask, calling:

    snd_soc_jack_report(rt711->hs_jack, rt711->jack_type, mask);

produces the exact same hs_jack->status value and input notifications as the normal btn_type == 0 path below (aside from skipping the dev_dbg() messages).

Additionally, because snd_soc_jack_report() is called here even when SDW_SCP_SDCA_INT_SDCA_0 is not set in rt711->scp_sdca_stat1, a button-only interrupt where rt711_sdca_button_detect() fails will still perform a full jack and button report rather than skipping button reporting.

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

PATCH v2 3/4 ASoC: rt712-sdca: handle HID button read errors

In reply to Avi Weiss

rt712_sdca_button_detect() returns zero when a regmap read fails, so the jack handler cannot distinguish the error from a valid no-button result.

Return the original error and make the button result signed. Skip button reporting on read failure, but still report jack changes and clear button state when the headset is removed.

Fixes 6c39710da75c ("ASoC: rt712-sdca: Add RT712 SDCA driver for Jack and Amp topology")

---
Changes in v2:
- report jack state on button read failures regardless of SDCA_0, preserving the original jack reporting behavior

 sound/soc/codecs/rt712-sdca.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
sound/soc/codecs/rt712-sdca.c +18 -4
index 20961e5760c65..08e08ec96bc50 100644
@@ -335,10 +335,10 @@ static int rt712_sdca_calibration(struct rt712_sdca_priv *rt712)
 	return ret;
 }
 
-static unsigned int rt712_sdca_button_detect(struct rt712_sdca_priv *rt712)
+static int rt712_sdca_button_detect(struct rt712_sdca_priv *rt712)
 {
-	unsigned int btn_type = 0, offset, idx, val, owner;
-	int ret;
+	unsigned int offset, idx, val, owner;
+	int btn_type = 0, ret;
 	unsigned char buf[3];
 
 	/* get current UMP message owner */
@@ -346,7 +346,7 @@ static unsigned int rt712_sdca_button_detect(struct rt712_sdca_priv *rt712)
 		SDW_SDCA_CTL(FUNC_NUM_HID, RT712_SDCA_ENT_HID01, RT712_SDCA_CTL_HIDTX_CURRENT_OWNER, 0),
 		&owner);
 	if (ret < 0)
-		return 0;
+		return ret;
 
 	/* if owner is device then there is no button event from device */
 	if (owner == 1)
@@ -416,6 +416,9 @@ static unsigned int rt712_sdca_button_detect(struct rt712_sdca_priv *rt712)
 					RT712_SDCA_CTL_HIDTX_CURRENT_OWNER, 0), 0x01);
 	}
 
+	if (ret < 0)
+		return ret;
+
 	return btn_type;
 }
 
@@ -485,6 +488,17 @@ static void rt712_sdca_jack_detect_handler(struct work_struct *work)
 	if (rt712->scp_sdca_stat2 & SDW_SCP_SDCA_INT_SDCA_8)
 		btn_type = rt712_sdca_button_detect(rt712);
 
+	if (btn_type < 0) {
+		unsigned int mask = SND_JACK_HEADSET;
+
+		if (rt712->jack_type != SND_JACK_HEADSET)
+			mask |= SND_JACK_BTN_0 | SND_JACK_BTN_1 |
+				SND_JACK_BTN_2 | SND_JACK_BTN_3;
+
+		snd_soc_jack_report(rt712->hs_jack, rt712->jack_type, mask);
+		return;
+	}
+
 	if (rt712->jack_type == 0)
 		btn_type = 0;
 
author
#4 lore raw

PATCH v2 4/4 ASoC: rt722-sdca: handle HID button read errors

In reply to Avi Weiss

rt722_sdca_button_detect() returns zero when a regmap read fails, so the jack handler cannot distinguish the error from a valid no-button result.

Return the original error and make the button result signed. Skip button reporting on read failure, but still report jack changes and clear button state when the headset is removed.

Fixes 7f5d6036ca00 ("ASoC: rt722-sdca: Add RT722 SDCA driver")

---
Changes in v2:
- report jack state on button read failures regardless of SDCA_0, preserving the original jack reporting behavior

 sound/soc/codecs/rt722-sdca.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
sound/soc/codecs/rt722-sdca.c +18 -4
index 1dad8012d904e..2d47420c0505b 100644
@@ -95,10 +95,10 @@ static int rt722_sdca_btn_type(unsigned char *buffer)
 	return 0;
 }
 
-static unsigned int rt722_sdca_button_detect(struct rt722_sdca_priv *rt722)
+static int rt722_sdca_button_detect(struct rt722_sdca_priv *rt722)
 {
-	unsigned int btn_type = 0, offset, idx, val, owner;
-	int ret;
+	unsigned int offset, idx, val, owner;
+	int btn_type = 0, ret;
 	unsigned char buf[3];
 
 	/* get current UMP message owner */
@@ -106,7 +106,7 @@ static unsigned int rt722_sdca_button_detect(struct rt722_sdca_priv *rt722)
 		SDW_SDCA_CTL(FUNC_NUM_HID, RT722_SDCA_ENT_HID01,
 			RT722_SDCA_CTL_HIDTX_CURRENT_OWNER, 0), &owner);
 	if (ret < 0)
-		return 0;
+		return ret;
 
 	/* if owner is device then there is no button event from device */
 	if (owner == 1)
@@ -138,6 +138,9 @@ static unsigned int rt722_sdca_button_detect(struct rt722_sdca_priv *rt722)
 			SDW_SDCA_CTL(FUNC_NUM_HID, RT722_SDCA_ENT_HID01,
 				RT722_SDCA_CTL_HIDTX_CURRENT_OWNER, 0), 0x01);
 
+	if (ret < 0)
+		return ret;
+
 	return btn_type;
 }
 
@@ -207,6 +210,17 @@ static void rt722_sdca_jack_detect_handler(struct work_struct *work)
 	if (rt722->scp_sdca_stat2 & SDW_SCP_SDCA_INT_SDCA_8)
 		btn_type = rt722_sdca_button_detect(rt722);
 
+	if (btn_type < 0) {
+		unsigned int mask = SND_JACK_HEADSET;
+
+		if (rt722->jack_type != SND_JACK_HEADSET)
+			mask |= SND_JACK_BTN_0 | SND_JACK_BTN_1 |
+				SND_JACK_BTN_2 | SND_JACK_BTN_3;
+
+		snd_soc_jack_report(rt722->hs_jack, rt722->jack_type, mask);
+		return;
+	}
+
 	if (rt722->jack_type == 0)
 		btn_type = 0;
 
maintainer
#5 lore raw
In reply to Avi Weiss
Quoted 1 line · Avi Weiss

ASoC: rt721-sdca: handle HID button read errors

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-7.5

Thanks!

[1/4] ASoC: rt721-sdca: handle HID button read errors

      https://git.kernel.org/broonie/sound/c/fbfcb71025b8
[2/4] ASoC: rt711-sdca: handle HID button read errors
      https://git.kernel.org/broonie/sound/c/92017b92d9dd
[3/4] ASoC: rt712-sdca: handle HID button read errors
      https://git.kernel.org/broonie/sound/c/fc9d50bb07fe
[4/4] ASoC: rt722-sdca: handle HID button read errors
      https://git.kernel.org/broonie/sound/c/5928cfa8474f

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark