Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ASoC: amd: acp: add DMIC capture switch with mic mute LED support
@ 2026-12-04 20:01 Bahadır Hamza Öztürk
  2026-12-04 20:23 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Bahadır Hamza Öztürk @ 2026-12-04 20:01 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood, Vijendar Mukunda
  Cc: Venkata Prasad Potturu, Jaroslav Kysela, Takashi Iwai,
	linux-sound, Bahadır Hamza Öztürk

On ACP platforms using the legacy PDM path (e.g. Huawei MateBook
D15/D16/14 AMD with the acp3x-es83xx machine driver), the internal
digital microphone has no capture switch. User space can only mute it
in software, so the ctl-led layer never sees a mute and the mic mute
LED (exposed as platform::micmute by huawei-wmi) never lights up.

Register-level experiments on a MateBook D15 (BOM-WXX9, Renoir)
found no usable hardware mute in the ACP PDM block:
 - the gain field in ACP_WOV_MISC_CTRL[4:3] only attenuates (~19 dB)
 - clearing ACP_WOV_PDM_ENABLE stalls the DMA and the stream
 - ACP_WOV_MISC_CTRL bit 5 gates the PDM data, but the decimator then
   outputs a constant full-scale negative DC value instead of silence

Add a "DMIC Capture Switch" control with SNDRV_CTL_ELEM_ACCESS_MIC_LED
and implement the mute in software: while the switch is off, captured
data between the last handled position and the current hardware
position is cleared in the .pointer callback, before the hw_ptr is
updated, so neither period-based nor mmap readers can observe it.

The control is only added when the ACP PDM controller is present.

Tested on Huawei MateBook D15 (BOM-WXX9): with the switch off the
recording is digital silence, the stream keeps running and the mic
mute LED follows the switch.

Signed-off-by: Bahadır Hamza Öztürk <bahadirhamza2@gmail.com>
---
 sound/soc/amd/acp/acp-platform.c | 78 ++++++++++++++++++++++++++++++++
 sound/soc/amd/acp/amd.h          |  2 +
 2 files changed, 80 insertions(+)

diff --git a/sound/soc/amd/acp/acp-platform.c b/sound/soc/amd/acp/acp-platform.c
index f2c885377..2163e233e 100644
--- a/sound/soc/amd/acp/acp-platform.c
+++ b/sound/soc/amd/acp/acp-platform.c
@@ -268,10 +268,33 @@ static int acp_dma_hw_params(struct snd_soc_component *component,
 	/* Configure ACP DMA block with params */
 	config_pte_for_stream(chip, stream);
 	config_acp_dma(chip, stream, size);
+	stream->mute_pos = 0;
 
 	return 0;
 }
 
+/*
+ * The ACP PDM block has no mute control, so mute the DMIC in software:
+ * every captured byte between the last handled position and the
+ * current hardware position is cleared before user space can see it.
+ * This is called from the .pointer callback, which runs before any
+ * hw_ptr update, so it covers both period interrupts and mmap readers
+ * that query the position in between.
+ */
+static void acp_dmic_soft_mute(struct snd_pcm_substream *substream,
+			       struct acp_stream *stream, u32 pos, u32 buffersize)
+{
+	unsigned char *area = substream->runtime->dma_area;
+	u32 start = stream->mute_pos;
+
+	if (start <= pos) {
+		memset(area + start, 0, pos - start);
+	} else {
+		memset(area + start, 0, buffersize - start);
+		memset(area, 0, pos);
+	}
+}
+
 static snd_pcm_uframes_t acp_dma_pointer(struct snd_soc_component *component,
 					 struct snd_pcm_substream *substream)
 {
@@ -291,6 +314,13 @@ static snd_pcm_uframes_t acp_dma_pointer(struct snd_soc_component *component,
 
 	pos = do_div(bytescount, buffersize);
 
+	if (stream->dai_id == DMIC_INSTANCE &&
+	    substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
+		if (READ_ONCE(chip->dmic_mute))
+			acp_dmic_soft_mute(substream, stream, pos, buffersize);
+		stream->mute_pos = pos;
+	}
+
 	return bytes_to_frames(substream->runtime, pos);
 }
 
@@ -320,8 +350,56 @@ static int acp_dma_close(struct snd_soc_component *component,
 	return 0;
 }
 
+static int acp_dmic_switch_get(struct snd_kcontrol *kcontrol,
+			       struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
+	struct acp_chip_info *chip = dev_get_drvdata(component->dev->parent);
+
+	ucontrol->value.integer.value[0] = !READ_ONCE(chip->dmic_mute);
+	return 0;
+}
+
+static int acp_dmic_switch_put(struct snd_kcontrol *kcontrol,
+			       struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
+	struct acp_chip_info *chip = dev_get_drvdata(component->dev->parent);
+	bool mute = !ucontrol->value.integer.value[0];
+
+	if (mute == READ_ONCE(chip->dmic_mute))
+		return 0;
+
+	WRITE_ONCE(chip->dmic_mute, mute);
+	return 1;
+}
+
+static const struct snd_kcontrol_new acp_dmic_controls[] = {
+	{
+		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
+		.name	= "DMIC Capture Switch",
+		.access	= SNDRV_CTL_ELEM_ACCESS_READWRITE |
+			  SNDRV_CTL_ELEM_ACCESS_MIC_LED,
+		.info	= snd_ctl_boolean_mono_info,
+		.get	= acp_dmic_switch_get,
+		.put	= acp_dmic_switch_put,
+	},
+};
+
+static int acp_pcm_probe(struct snd_soc_component *component)
+{
+	struct acp_chip_info *chip = dev_get_drvdata(component->dev->parent);
+
+	if (!chip->is_pdm_dev)
+		return 0;
+
+	return snd_soc_add_component_controls(component, acp_dmic_controls,
+					      ARRAY_SIZE(acp_dmic_controls));
+}
+
 static const struct snd_soc_component_driver acp_pcm_component = {
 	.name			= DRV_NAME,
+	.probe			= acp_pcm_probe,
 	.open			= acp_dma_open,
 	.close			= acp_dma_close,
 	.hw_params		= acp_dma_hw_params,
diff --git a/sound/soc/amd/acp/amd.h b/sound/soc/amd/acp/amd.h
index 73a028e67..4fc7b85f9 100644
--- a/sound/soc/amd/acp/amd.h
+++ b/sound/soc/amd/acp/amd.h
@@ -170,6 +170,7 @@ struct acp_chip_info {
 	bool is_pdm_config;	/* flag set to true when PDM configuration is selected from BIOS */
 	bool is_i2s_config;	/* flag set to true when I2S configuration is selected from BIOS */
 	bool tdm_mode;
+	bool dmic_mute;		/* set when the "DMIC Capture Switch" is off */
 };
 
 struct acp_stream {
@@ -183,6 +184,7 @@ struct acp_stream {
 	u32 reg_offset;
 	u32 pte_offset;
 	u32 fifo_offset;
+	u32 mute_pos;		/* DMIC: buffer offset already handled by soft mute */
 };
 
 struct acp_resource {
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ASoC: amd: acp: add DMIC capture switch with mic mute LED support
  2026-12-04 20:01 [PATCH] ASoC: amd: acp: add DMIC capture switch with mic mute LED support Bahadır Hamza Öztürk
@ 2026-12-04 20:23 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 20:23 UTC (permalink / raw)
  To: Bahadır Hamza Öztürk; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] ASoC: amd: acp: NULL deref in acp_pcm_probe() before
  dev_set_drvdata()
- [Medium] ASoC: amd: acp: mmap readers bypass DMIC soft mute while mute
  LED is on
- [Medium] ASoC: amd: acp: stale mute_pos leaks audio after prepare or
  resume
- [Medium] ASoC: amd: acp: modulo mute_pos misses full-buffer wraparound
  in pointer
- [Medium] ASoC: amd: acp: missing snd_ctl_led_request() and SND_CTL_LED
  Kconfig select
- [Medium] ASoC: amd: acp: unmuting in acp_dmic_switch_put() leaks
  pending muted audio
- [Medium] ASoC: amd: acp: unconditional MIC_LED flag breaks mic mute
  LED state
--

commit c6c200b5312901dd7e1038bc5492d1be0b4d966a
Author: Bahadır Hamza Öztürk <bahadirhamza2@gmail.com>

ASoC: amd: acp: add DMIC capture switch with mic mute LED support

This commit adds a "DMIC Capture Switch" mixer control with
SNDRV_CTL_ELEM_ACCESS_MIC_LED to the ACP PCM component when a PDM
controller is present, and implements muting in software by zeroing
newly captured samples in the .pointer callback.

> diff --git a/sound/soc/amd/acp/acp-platform.c b/sound/soc/amd/acp/acp-platform.c
> index f2c8853775ad3..2163e233e6cba 100644
> --- a/sound/soc/amd/acp/acp-platform.c
> +++ b/sound/soc/amd/acp/acp-platform.c
> @@ -268,10 +268,33 @@ static int acp_dma_hw_params(struct snd_soc_component *component,
>  	/* Configure ACP DMA block with params */
>  	config_pte_for_stream(chip, stream);
>  	config_acp_dma(chip, stream, size);
> +	stream->mute_pos = 0;

[Severity: Medium]
Can stream->mute_pos go stale across prepare or system resume and leak
unmuted audio?

stream->mute_pos is only reset here in acp_dma_hw_params(), not in
acp_dmic_prepare(), acp_dmic_dai_trigger(), or restore_acp_pdm_params().
After system suspend/resume (where acp_reset() zeroes the hardware byte
counter) or STOP/XRUN recovery, DMA restarts at offset 0 while
stream->mute_pos still holds its previous non-zero offset.  When
acp_dma_pointer() next observes pos >= stream->mute_pos while muted,
acp_dmic_soft_mute() only zeroes [start, pos) and skips [0, start),
delivering live audio to userspace while the mute LED is on.

Additionally, unlike acp_i2s_trigger(), acp_dmic_dai_trigger() never
snapshots stream->bytescount on start, so reopening a stream without an
ACP reset can begin at a non-zero ring offset that does not match
mute_pos = 0.

>  
>  	return 0;
>  }
>  
> +/*
> + * The ACP PDM block has no mute control, so mute the DMIC in software:
> + * every captured byte between the last handled position and the
> + * current hardware position is cleared before user space can see it.
> + * This is called from the .pointer callback, which runs before any
> + * hw_ptr update, so it covers both period interrupts and mmap readers
> + * that query the position in between.
> + */
> +static void acp_dmic_soft_mute(struct snd_pcm_substream *substream,
> +			       struct acp_stream *stream, u32 pos, u32 buffersize)
> +{
> +	unsigned char *area = substream->runtime->dma_area;
> +	u32 start = stream->mute_pos;
> +
> +	if (start <= pos) {
> +		memset(area + start, 0, pos - start);
> +	} else {
> +		memset(area + start, 0, buffersize - start);
> +		memset(area, 0, pos);
> +	}
> +}

[Severity: Medium]
Can mmap capture readers still read live microphone audio while muted?

Both acp_pcm_hardware_capture and acp6x_pcm_hardware_capture advertise
SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID, mapping
substream->runtime->dma_area directly into userspace.  Because PDM DMA
continues writing live samples into those pages and acp_dmic_soft_mute()
only zeroes them retroactively in acp_dma_pointer(), an mmap reader
inspecting the buffer ahead of hw_ptr can capture unmuted audio while the
hardware mic mute LED stays lit.

Scrubbing up to 64 KB with memset() here also runs in hardirq atomic
context while holding both chip->acp_lock (from acp_irq_handler()) and
the PCM substream spinlock.

[ ... ]

> @@ -291,6 +314,13 @@ static snd_pcm_uframes_t acp_dma_pointer(struct snd_soc_component *component,
>  
>  	pos = do_div(bytescount, buffersize);
>  
> +	if (stream->dai_id == DMIC_INSTANCE &&
> +	    substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
> +		if (READ_ONCE(chip->dmic_mute))
> +			acp_dmic_soft_mute(substream, stream, pos, buffersize);
> +		stream->mute_pos = pos;
> +	}

[Severity: Medium]
Will a full-buffer wraparound between .pointer() calls bypass the soft
mute?

do_div(bytescount, buffersize) discards the unwrapped byte progression
before calling acp_dmic_soft_mute().  With a 2-period buffer (as small as
2048 bytes) under a delayed or coalesced interrupt, or under
no_period_wakeup, DMA can advance by buffersize bytes so pos equals
stream->mute_pos.  acp_dmic_soft_mute() then clears zero bytes while
snd_pcm_update_hw_ptr0() detects the wrap via jiffies and advances hw_ptr
by runtime->buffer_size, exposing the full unmuted buffer to userspace.

Tracking the unwrapped 64-bit byte count before do_div() and clearing the
whole ring buffer whenever the delta reaches buffersize (or moves
backwards) would avoid this.

[ ... ]

> +static int acp_dmic_switch_put(struct snd_kcontrol *kcontrol,
> +			       struct snd_ctl_elem_value *ucontrol)
> +{
> +	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> +	struct acp_chip_info *chip = dev_get_drvdata(component->dev->parent);
> +	bool mute = !ucontrol->value.integer.value[0];
> +
> +	if (mute == READ_ONCE(chip->dmic_mute))
> +		return 0;
> +
> +	WRITE_ONCE(chip->dmic_mute, mute);
> +	return 1;
> +}

[Severity: Medium]
Does clearing chip->dmic_mute here leak audio that was captured while
muted?

While muted, PDM DMA keeps writing live samples ahead of
stream->mute_pos.  Clearing chip->dmic_mute without first zeroing the
pending [stream->mute_pos, current_hw_pos) range causes the next
acp_dma_pointer() call to skip acp_dmic_soft_mute() and advance hw_ptr
over up to a full period of audio recorded while the mute LED was on.

> +
> +static const struct snd_kcontrol_new acp_dmic_controls[] = {
> +	{
> +		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
> +		.name	= "DMIC Capture Switch",
> +		.access	= SNDRV_CTL_ELEM_ACCESS_READWRITE |
> +			  SNDRV_CTL_ELEM_ACCESS_MIC_LED,
> +		.info	= snd_ctl_boolean_mono_info,
> +		.get	= acp_dmic_switch_get,
> +		.put	= acp_dmic_switch_put,
> +	},
> +};

[Severity: Medium]
Does setting SNDRV_CTL_ELEM_ACCESS_MIC_LED unconditionally on this
control regress mic mute LED handling on other ACP platforms?

Because chip->dmic_mute defaults to false (switch on), adding this
control with SNDRV_CTL_ELEM_ACCESS_MIC_LED registers an always-on entry
in the global snd_ctl_leds mic list in sound/core/control_led.c.  On ACP
7.0/7.1/7.2 laptops, snd_amd_acp_acpi_find_config() defaults to
FLAG_AMD_LEGACY_ONLY_DMIC and registers acp-pdm-mach as a second card
alongside snd_hda_intel; since existing userspace does not toggle the new
DMIC switch on acp-pdm-mach, snd_ctl_led_set_state() keeps the mic mute
LED permanently off even when HDA capture is muted.

On legacy I2S+DMIC cards such as acp3x-es83xx, none of the codec capture
switches or headset mic pin switches carry SNDRV_CTL_ELEM_ACCESS_MIC_LED,
so muting DMIC lights the mic mute LED even while the analog headset mic
is live.

> +
> +static int acp_pcm_probe(struct snd_soc_component *component)
> +{
> +	struct acp_chip_info *chip = dev_get_drvdata(component->dev->parent);
> +
> +	if (!chip->is_pdm_dev)
> +		return 0;

[Severity: High]
Can dev_get_drvdata(component->dev->parent) return NULL here and crash
during probe?

In acp_pci_probe(), both create_acp_platform_devs() and
acp_machine_select() run before dev_set_drvdata(&pci->dev, chip):

sound/soc/amd/acp/acp-pci.c:acp_pci_probe() {
	...
	acp_machine_select(chip);

	INIT_LIST_HEAD(&chip->stream_list);
	spin_lock_init(&chip->acp_lock);
skip_pdev_creation:
	dev_set_drvdata(&pci->dev, chip);
	...
}

When the platform, DMIC codec, and machine drivers are built in or
already loaded, acp_machine_select() synchronously calls
devm_snd_soc_register_card() -> snd_soc_component_probe() ->
acp_pcm_probe(), causing a NULL pointer dereference on chip->is_pdm_dev.
Even if acp_pcm_probe() avoided parent drvdata, snd_card_register() ->
snd_ctl_led_register() synchronously invokes acp_dmic_switch_get() for
SNDRV_CTL_ELEM_ACCESS_MIC_LED controls, which also dereferences
dev_get_drvdata(component->dev->parent).

Setting dev_set_drvdata(&pci->dev, chip) in acp_pci_probe() before
creating the child platform devices would avoid both crashes.

> +
> +	return snd_soc_add_component_controls(component, acp_dmic_controls,
> +					      ARRAY_SIZE(acp_dmic_controls));
> +}

[Severity: Medium]
Should acp_pcm_probe() call snd_ctl_led_request(), and should
SND_SOC_AMD_ACP_PCM select SND_CTL_LED?

Adding a control with SNDRV_CTL_ELEM_ACCESS_MIC_LED does not auto-load
snd-ctl-led on its own, so when CONFIG_SND_CTL_LED=m and no HDA or SOF
driver requests the layer, the mic mute LED stays inactive.  In addition,
SND_CTL_LED in sound/core/Kconfig has no prompt, so AMD ACP ASoC-only
configurations without CONFIG_SND_HDA_GENERIC_LEDS cannot enable it at
all unless SND_SOC_AMD_ACP_PCM selects it.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204200115.10636-1-bahadirhamza2@gmail.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04 20:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 20:01 [PATCH] ASoC: amd: acp: add DMIC capture switch with mic mute LED support Bahadır Hamza Öztürk
2026-12-04 20:23 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox