Skip to contentSkip to conversation

Add SoundWire detach helper

PATCH RESEND Superseded Charles Keepax 3 participants 11 messages Sep 24 – Oct 20

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add SoundWire detach helper

Currently, if a SoundWire driver takes an action that causes the device to detach from the bus it needs to be careful. There will be a period of time until the detach is recognised by the core, if the driver immediately calls sdw_slave_wait_for_init() it is possible for that to complete before the detach has been recognised. Leaving the driver with a device that is potentially not ready for what follows.

To avoid this race add a new helper that drivers can use to inform the core the device will detach. This clears the enumeration completions ensuring that sdw_slave_wait_for_init() can't complete until the device re-attaches.

This is the third part of a longer series of moving these slave attach/deatach operations into the core:

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

I have based this series off Vinod's soundwire tree, but the patches apply to both the current MFD and SoundWire next branches so should be fine to apply to either tree.

Thanks,
Charles

Changes since v1:
- Correct kerneldoc

Charles Keepax (2):
soundwire: Add a helper function to indicate a device will detach
mfd: cs42l43: Move to core detach helper

 drivers/mfd/cs42l43-sdw.c     |  2 --
 drivers/mfd/cs42l43.c         | 15 +--------------
 drivers/soundwire/bus.h       |  6 ------
 include/linux/mfd/cs42l43.h   |  1 -
 include/linux/soundwire/sdw.h | 26 ++++++++++++++++++++++++++
 5 files changed, 27 insertions(+), 23 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH RESEND v2 1/2 soundwire: Add a helper function to indicate a device will detach

SoundWire devices will sometimes need to reset themselves, typical times might include when taking over the device to ensure a consistent state or after downloading a firmware update to boot into the new version. Often such a reset will also result in the device detaching from the SoundWire bus, in this case the driver has to be careful. Naively calling sdw_slave_wait_for_init() after doing the reset could immediately succeed if the bus hasn't seen the detach yet, causing the driver to proceed attempting to communicate with the device whilst it is detached.

Add a helper function that a driver can call to indicate it is about to initiate an action that will cause the device to detach from the bus. This will reinit the completions in the peripheral for device attach, ensuring that the next sdw_slave_wait_for_init() will not complete before the detach has been seen by the core.

---
 drivers/soundwire/bus.h       |  6 ------
 include/linux/soundwire/sdw.h | 26 ++++++++++++++++++++++++++
 2 files changed, 26 insertions(+), 6 deletions(-)
drivers/soundwire/bus.h +0 -6
index 44e4f51939176..59b6aee0ea6eb 100644
@@ -231,12 +231,6 @@ static inline void sdw_fill_port_params(struct sdw_port_params *params,
 int sdw_bread_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr);
 int sdw_bwrite_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value);
 
-/*
- * At the moment we only track Master-initiated hw_reset.
- * Additional fields can be added as needed
- */
-#define SDW_UNATTACH_REQUEST_MASTER_RESET	BIT(0)
-
 void sdw_clear_slave_status(struct sdw_bus *bus, u32 request);
 int sdw_slave_modalias(const struct sdw_slave *slave, char *buf, size_t size);
 void sdw_compute_slave_ports(struct sdw_master_runtime *m_rt,
include/linux/soundwire/sdw.h +26 -0
index f710e5932b4b2..4937d1a2f24f7 100644
@@ -25,6 +25,13 @@ struct device_node;
 struct sdw_bus;
 struct sdw_slave;
 
+/*
+ * At the moment we only track Master-initiated/Slave-initiated hw_reset.
+ * Additional fields can be added as needed
+ */
+#define SDW_UNATTACH_REQUEST_MASTER_RESET	BIT(0)
+#define SDW_UNATTACH_REQUEST_SLAVE_RESET	BIT(1)
+
 /* SDW spec defines and enums, as defined by MIPI 1.1. Spec */
 
 /* SDW Broadcast Device Number */
@@ -1218,6 +1225,25 @@ static inline int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u
 
 #endif /* CONFIG_SOUNDWIRE */
 
+/**
+ * sdw_slave_signal_unattach - Signal that the peripheral is about to detach
+ * @slave: Pointer to the SoundWire peripheral.
+ *
+ * Inform the core that the peripheral is about to detach from the bus, this
+ * is usually due to some implementation defined reset mechanism. After this
+ * sdw_slave_wait_for_init() should be called to wait for the device to
+ * return.
+ */
+static inline void sdw_slave_signal_unattach(struct sdw_slave *slave)
+{
+	if (!slave)
+		return;
+
+	slave->unattach_request = SDW_UNATTACH_REQUEST_SLAVE_RESET;
+	reinit_completion(&slave->enumeration_complete);
+	reinit_completion(&slave->initialization_complete);
+}
+
 /**
  * sdw_slave_wait_for_init - Wait for device initialisation
  * @slave: Pointer to the SoundWire peripheral.
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH v2 RESEND 1/2] soundwire: Add a helper function to indicate a device will detach

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] soundwire: data race in sdw_slave_signal_unattach() with complete_all() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] soundwire: data race in sdw_slave_signal_unattach() with complete_all() --

commit 0022c8987af32edaf369ec5a75ade11e2d3a1ba3
Author: Charles Keepax <[email protected]>

soundwire: Add a helper function to indicate a device will detach

Add a helper function that a driver can call to indicate it is about to initiate an action that will cause the device to detach from the bus.

Quoted 4 lines
include/linux/soundwire/sdw.h +0 -0
index 707ca6bd1e23d..7d41df30561cf 100644

[ ... ]

Quoted 11 lines
include/linux/soundwire/sdw.h +7 -0
@@ -1214,6 +1221,25 @@ static inline int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u
 
 #endif /* CONFIG_SOUNDWIRE */
 
+/**
+ * sdw_slave_signal_unattach - Signal that the peripheral is about to detach
+ * @slave: Pointer to the SoundWire peripheral.
+ *
+ * Inform the core that the peripheral is about to detach from the bus, this
+ * is usually due to some implementation defined reset mechanism. After this
+ * sdw_slave_wait_for_init() should be called to wait for the device to

include/linux/soundwire/sdw.h static inline int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u · line 1240

Quoted 10 lines
include/linux/soundwire/sdw.h +10 -0
+ * return.
+ */
+static inline void sdw_slave_signal_unattach(struct sdw_slave *slave)
+{
+	if (!slave)
+		return;
+
+	slave->unattach_request = SDW_UNATTACH_REQUEST_SLAVE_RESET;
+	reinit_completion(&slave->enumeration_complete);
+	reinit_completion(&slave->initialization_complete);

[Severity: High]
Is there a risk of a data race here in sdw_slave_signal_unattach() with complete_all()?

Since reinit_completion() is non-atomic and executes locklessly here, a race can occur if the bus ping thread concurrently processes a previous or bouncing ATTACHED event in sdw_handle_slave_status(), which calls complete_all().

If complete_all() finishes after the zeroing, the completion remains marked as done.

Would this cause subsequent calls to sdw_slave_wait_for_init() to return immediately, and potentially lead the driver to perform I/O against a detached device?

Quoted 4 lines
include/linux/soundwire/sdw.h +2 -0
+}
+
 /**
  * sdw_slave_wait_for_init - Wait for device initialisation
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#3 lore raw

Re: [PATCH v2 RESEND 1/2] soundwire: Add a helper function to indicate a device will detach

Quoted 2 lines · On Thu, 24 Sep 2026, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] soundwire: data race in sdw_slave_signal_unattach() with complete_all()

Charles, did you answer this?

Maybe this is why Vinod isn't replying?

Quoted 61 lines

--

commit 0022c8987af32edaf369ec5a75ade11e2d3a1ba3
Author: Charles Keepax <[email protected]>

soundwire: Add a helper function to indicate a device will detach

Add a helper function that a driver can call to indicate it is about to initiate an action that will cause the device to detach from the bus.

Quoted 4 lines
include/linux/soundwire/sdw.h +0 -0
index 707ca6bd1e23d..7d41df30561cf 100644

[ ... ]

Quoted 11 lines
include/linux/soundwire/sdw.h +7 -0
@@ -1214,6 +1221,25 @@ static inline int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u
 
 #endif /* CONFIG_SOUNDWIRE */
 
+/**
+ * sdw_slave_signal_unattach - Signal that the peripheral is about to detach
+ * @slave: Pointer to the SoundWire peripheral.
+ *
+ * Inform the core that the peripheral is about to detach from the bus, this
+ * is usually due to some implementation defined reset mechanism. After this
+ * sdw_slave_wait_for_init() should be called to wait for the device to

include/linux/soundwire/sdw.h static inline int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u · line 1240

Quoted 10 lines
include/linux/soundwire/sdw.h +10 -0
+ * return.
+ */
+static inline void sdw_slave_signal_unattach(struct sdw_slave *slave)
+{
+	if (!slave)
+		return;
+
+	slave->unattach_request = SDW_UNATTACH_REQUEST_SLAVE_RESET;
+	reinit_completion(&slave->enumeration_complete);
+	reinit_completion(&slave->initialization_complete);

[Severity: High]
Is there a risk of a data race here in sdw_slave_signal_unattach() with complete_all()?

Since reinit_completion() is non-atomic and executes locklessly here, a race can occur if the bus ping thread concurrently processes a previous or bouncing ATTACHED event in sdw_handle_slave_status(), which calls complete_all().

If complete_all() finishes after the zeroing, the completion remains marked as done.

Would this cause subsequent calls to sdw_slave_wait_for_init() to return immediately, and potentially lead the driver to perform I/O against a detached device?

Quoted 4 lines
include/linux/soundwire/sdw.h +2 -0
+}
+
 /**
  * sdw_slave_wait_for_init - Wait for device initialisation
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Signature · 1 line
Lee Jones
#4 lore raw

Re: [PATCH v2 RESEND 1/2] soundwire: Add a helper function to indicate a device will detach

Quoted 6 lines · Lee Jones
Quoted 2 lines · On Thu, 24 Sep 2026, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] soundwire: data race in sdw_slave_signal_unattach() with complete_all()

Charles, did you answer this?

Yeah, although Vinod wouldn't have been on either the Sashiko email or the response.

Quoted 1 line

Maybe this is why Vinod isn't replying?

He has replied, he acked the patch:

https://lore.kernel.org/linux-sound/asC1SwHb4DZkJ4Pr@parshuram/

This resend is identical to that version, I would have carried the ack but unfortunately he acked that version after I did this resend.

Also in this email he says he is happy for things to go through either tree:

https://lore.kernel.org/linux-sound/asC1kM-aOjl0GYYx@parshuram/

I can do another resend carrying the ack if that is helpful?

Thanks,
Charles

maintainer
#5 lore raw

Re: [PATCH v2 RESEND 1/2] soundwire: Add a helper function to indicate a device will detach

Quoted 21 lines · Charles Keepax
Quoted 6 lines · Lee Jones
Quoted 2 lines · On Thu, 24 Sep 2026, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] soundwire: data race in sdw_slave_signal_unattach() with complete_all()

Charles, did you answer this?

Yeah, although Vinod wouldn't have been on either the Sashiko email or the response.

Quoted 1 line

Maybe this is why Vinod isn't replying?

He has replied, he acked the patch:

https://lore.kernel.org/linux-sound/asC1SwHb4DZkJ4Pr@parshuram/

This resend is identical to that version, I would have carried the ack but unfortunately he acked that version after I did this resend.

Quoted 6 lines

Also in this email he says he is happy for things to go through either tree:

https://lore.kernel.org/linux-sound/asC1kM-aOjl0GYYx@parshuram/

I can do another resend carrying the ack if that is helpful?

Yes please.

Signature · 1 line
Lee Jones
#6 lore raw

Re: [PATCH v2 RESEND 1/2] soundwire: Add a helper function to indicate a device will detach

Quoted 6 lines · Lee Jones
Quoted 3 lines · Charles Keepax
Quoted 1 line · Lee Jones

On Thu, 24 Sep 2026, [email protected] wrote:

I can do another resend carrying the ack if that is helpful?

Yes please.

Cool yeah can do, I will wait until the merge window closes then do a resend.

Thanks,
Charles

#7 lore raw

PATCH RESEND v2 2/2 mfd: cs42l43: Move to core detach helper

In reply to Charles Keepax

Now the core has a helper to signal a device will detach from the SoundWire bus, there is no need for the cs42l43 driver code to separately track that. Switch to using the new core helper.

---
 drivers/mfd/cs42l43-sdw.c   |  2 --
 drivers/mfd/cs42l43.c       | 15 +--------------
 include/linux/mfd/cs42l43.h |  1 -
 3 files changed, 1 insertion(+), 17 deletions(-)
drivers/mfd/cs42l43-sdw.c +0 -2
index 0a6999453f325..f0825d6bf7b38 100644
@@ -102,8 +102,6 @@ static int cs42l43_sdw_update_status(struct sdw_slave *sdw, enum sdw_slave_statu
 		break;
 	case SDW_SLAVE_UNATTACHED:
 		dev_dbg(cs42l43->dev, "Device detach\n");
-
-		complete(&cs42l43->device_detach);
 		break;
 	default:
 		break;
drivers/mfd/cs42l43.c +1 -14
index 4212ebcca60b5..f08ee598366d9 100644
@@ -30,7 +30,6 @@
 #define CS42L43_RESET_DELAY_MS			20
 
 #define CS42L43_SDW_ATTACH_TIMEOUT_MS		5000
-#define CS42L43_SDW_DETACH_TIMEOUT_MS		100
 
 #define CS42L43_MCU_BOOT_STAGE1			1
 #define CS42L43_MCU_BOOT_STAGE2			2
@@ -555,7 +554,7 @@ static int cs42l43_soft_reset(struct cs42l43 *cs42l43)
 		{ CS42L43_SFT_RESET, CS42L43_SFT_RESET_VAL },
 	};
 
-	reinit_completion(&cs42l43->device_detach);
+	sdw_slave_signal_unattach(cs42l43->sdw);
 
 	/*
 	 * Apply cache only because the soft reset will cause the device to
@@ -566,17 +565,6 @@ static int cs42l43_soft_reset(struct cs42l43 *cs42l43)
 
 	msleep(CS42L43_RESET_DELAY_MS);
 
-	if (cs42l43->sdw) {
-		unsigned long timeout = msecs_to_jiffies(CS42L43_SDW_DETACH_TIMEOUT_MS);
-		unsigned long time;
-
-		time = wait_for_completion_timeout(&cs42l43->device_detach, timeout);
-		if (!time) {
-			dev_err(cs42l43->dev, "Timed out waiting for device detach\n");
-			return -ETIMEDOUT;
-		}
-	}
-
 	return -EAGAIN;
 }
 
@@ -1115,7 +1103,6 @@ int cs42l43_dev_probe(struct cs42l43 *cs42l43)
 	dev_set_drvdata(cs42l43->dev, cs42l43);
 
 	mutex_init(&cs42l43->pll_lock);
-	init_completion(&cs42l43->device_detach);
 	init_completion(&cs42l43->firmware_download);
 	INIT_WORK(&cs42l43->boot_work, cs42l43_boot_work);
 
include/linux/mfd/cs42l43.h +0 -1
index 8e993fb535e68..798dc55149662 100644
@@ -86,7 +86,6 @@ struct cs42l43 {
 	struct regmap_irq_chip_data *irq_data;
 
 	struct work_struct boot_work;
-	struct completion device_detach;
 	struct completion firmware_download;
 	int firmware_error;
 
Signature · 1 line
2.47.3
#8 lore raw

Re: [PATCH v2 RESEND 2/2] mfd: cs42l43: Move to core detach helper

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

Now the core has a helper to signal a device will detach from the SoundWire bus, there is no need for the cs42l43 driver code to separately track that. Switch to using the new core helper.

Sashiko has reviewed this patch and found no issues. It looks great!

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

Re: [PATCH v2 RESEND 2/2] mfd: cs42l43: Move to core detach helper

In reply to Charles Keepax
Quoted 11 lines · Charles Keepax

Now the core has a helper to signal a device will detach from the SoundWire bus, there is no need for the cs42l43 driver code to separately track that. Switch to using the new core helper.

---
 drivers/mfd/cs42l43-sdw.c   |  2 --
 drivers/mfd/cs42l43.c       | 15 +--------------
 include/linux/mfd/cs42l43.h |  1 -
 3 files changed, 1 insertion(+), 17 deletions(-)

Does this need to go in as a set?

Quoted 75 lines
drivers/mfd/cs42l43-sdw.c +0 -2
index 0a6999453f325..f0825d6bf7b38 100644
@@ -102,8 +102,6 @@ static int cs42l43_sdw_update_status(struct sdw_slave *sdw, enum sdw_slave_statu
 		break;
 	case SDW_SLAVE_UNATTACHED:
 		dev_dbg(cs42l43->dev, "Device detach\n");
-
-		complete(&cs42l43->device_detach);
 		break;
 	default:
 		break;
drivers/mfd/cs42l43.c +1 -14
index 4212ebcca60b5..f08ee598366d9 100644
@@ -30,7 +30,6 @@
 #define CS42L43_RESET_DELAY_MS			20
 
 #define CS42L43_SDW_ATTACH_TIMEOUT_MS		5000
-#define CS42L43_SDW_DETACH_TIMEOUT_MS		100
 
 #define CS42L43_MCU_BOOT_STAGE1			1
 #define CS42L43_MCU_BOOT_STAGE2			2
@@ -555,7 +554,7 @@ static int cs42l43_soft_reset(struct cs42l43 *cs42l43)
 		{ CS42L43_SFT_RESET, CS42L43_SFT_RESET_VAL },
 	};
 
-	reinit_completion(&cs42l43->device_detach);
+	sdw_slave_signal_unattach(cs42l43->sdw);
 
 	/*
 	 * Apply cache only because the soft reset will cause the device to
@@ -566,17 +565,6 @@ static int cs42l43_soft_reset(struct cs42l43 *cs42l43)
 
 	msleep(CS42L43_RESET_DELAY_MS);
 
-	if (cs42l43->sdw) {
-		unsigned long timeout = msecs_to_jiffies(CS42L43_SDW_DETACH_TIMEOUT_MS);
-		unsigned long time;
-
-		time = wait_for_completion_timeout(&cs42l43->device_detach, timeout);
-		if (!time) {
-			dev_err(cs42l43->dev, "Timed out waiting for device detach\n");
-			return -ETIMEDOUT;
-		}
-	}
-
 	return -EAGAIN;
 }
 
@@ -1115,7 +1103,6 @@ int cs42l43_dev_probe(struct cs42l43 *cs42l43)
 	dev_set_drvdata(cs42l43->dev, cs42l43);
 
 	mutex_init(&cs42l43->pll_lock);
-	init_completion(&cs42l43->device_detach);
 	init_completion(&cs42l43->firmware_download);
 	INIT_WORK(&cs42l43->boot_work, cs42l43_boot_work);
 
include/linux/mfd/cs42l43.h +0 -1
index 8e993fb535e68..798dc55149662 100644
@@ -86,7 +86,6 @@ struct cs42l43 {
 	struct regmap_irq_chip_data *irq_data;
 
 	struct work_struct boot_work;
-	struct completion device_detach;
 	struct completion firmware_download;
 	int firmware_error;
 
Signature · 1 line
2.47.3
Signature · 1 line
Lee Jones
#10 lore raw

Re: [PATCH v2 RESEND 2/2] mfd: cs42l43: Move to core detach helper

Quoted 14 lines · Lee Jones
Quoted 11 lines · Charles Keepax

Now the core has a helper to signal a device will detach from the SoundWire bus, there is no need for the cs42l43 driver code to separately track that. Switch to using the new core helper.

---
 drivers/mfd/cs42l43-sdw.c   |  2 --
 drivers/mfd/cs42l43.c       | 15 +--------------
 include/linux/mfd/cs42l43.h |  1 -
 3 files changed, 1 insertion(+), 17 deletions(-)
Quoted 1 line

Does this need to go in as a set?

The MFD bit won't build without the SoundWire bit, so ideally. Vinod has acked the series, although the original rather than the resend.

https://lore.kernel.org/linux-sound/asC1SwHb4DZkJ4Pr@parshuram/

Thanks,
Charles