Skip to contentSkip to conversation

ASoC: core: Warn about controls whose register fields overlap

PATCH In review HyeongJun An 3 participants 5 messages Oct 19 – Nov 2

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: core: Warn about controls whose register fields overlap

A control that writes part of another control's register field also changes that control. Usually the bits it was meant to own are then out of reach. 26 of these were fixed in codec drivers this cycle. Most dated back to the first version of the driver.

So check each control as it is added. If it shares a register with a control of the same component, and the two fields overlap but are not the same, print a warning. Identical fields are not reported, as some drivers expose one field under two names.

Only controls with the volsw, volsw_sx and enum_double put callbacks are checked. For other controls the bits written are not known.

Add a KUnit test for the field calculation.

---

Notes:
On for-7.4 an allmodconfig build gives four warnings in three drivers:

cs42l56: ADC Mute Switch with ADCA Invert and ADCB Invert
es8326: ALC Capture Recovery Level with ALC Capture Target Level
es8375: DMIC Polarity with DMIC Volume

cs42l56 is the one I asked about in the first series [1]. The ADC Mute Switch writes the ADC invert bits. The real mute bits are also written by cs42l56_mute() on every stream start and stop. Would you rather move the control or drop it?

For es8326 and es8375 Everest confirmed both are bugs and offered to fix them this week [2].

DAPM widget controls are not checked. Adding them would give 142 more warnings. 140 of those are in max98520, which uses the max of SOC_DAPM_SINGLE as a channel number. Should DAPM be covered in a follow-up?

Tested with KUnit and with a test card that adds overlapping, identical, _EXT and NOPM controls. I have none of the hardware.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

 sound/soc/soc-core.c     | 51 ++++++++++++++++++++++++++++++++++++++--
 sound/soc/soc-internal.h | 11 +++++++++
 sound/soc/soc-ops-test.c | 50 +++++++++++++++++++++++++++++++++++++++
 sound/soc/soc-ops.c      | 48 +++++++++++++++++++++++++++++++++++++
 4 files changed, 158 insertions(+), 2 deletions(-)
sound/soc/soc-core.c +49 -2
index 97269b4ee4ca..f33bece378b6 100644
@@ -2021,6 +2021,47 @@ struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
 }
 EXPORT_SYMBOL_GPL(snd_soc_cnew);
 
+/*
+ * Two controls that write part of the same register field each change the
+ * other. Identical fields are allowed, as some drivers expose one field
+ * under two names.
+ */
+static void soc_check_control_overlap(struct snd_card *card,
+				      struct device *dev,
+				      struct snd_kcontrol *new)
+{
+	struct snd_soc_control_field a[2], b[2];
+	struct snd_kcontrol *kctl;
+	int na, nb, i, j;
+
+	na = snd_soc_control_fields(new, a);
+	if (!na)
+		return;
+
+	guard(rwsem_read)(&card->controls_rwsem);
+
+	list_for_each_entry(kctl, &card->controls, list) {
+		if (kctl->private_data != new->private_data)
+			continue;
+
+		nb = snd_soc_control_fields(kctl, b);
+		for (i = 0; i < na; i++) {
+			for (j = 0; j < nb; j++) {
+				if (a[i].reg != b[j].reg ||
+				    a[i].reg == SND_SOC_NOPM ||
+				    !(a[i].mask & b[j].mask) ||
+				    a[i].mask == b[j].mask)
+					continue;
+
+				dev_warn(dev,
+					 "ASoC: %s (%#x) overlaps %s (%#x) in register %#x\n",
+					 new->id.name, a[i].mask, kctl->id.name,
+					 b[j].mask, a[i].reg);
+			}
+		}
+	}
+}
+
 int snd_soc_add_controls(struct snd_card *card, struct device *dev,
 			 const struct snd_kcontrol_new *controls, int num_controls,
 			 const char *prefix, void *data)
@@ -2029,8 +2070,14 @@ int snd_soc_add_controls(struct snd_card *card, struct device *dev,
 
 	for (i = 0; i < num_controls; i++) {
 		const struct snd_kcontrol_new *control = &controls[i];
-		int err = snd_ctl_add(card, snd_soc_cnew(control, data,
-							 control->name, prefix));
+		struct snd_kcontrol *kctl;
+		int err;
+
+		kctl = snd_soc_cnew(control, data, control->name, prefix);
+		if (kctl)
+			soc_check_control_overlap(card, dev, kctl);
+
+		err = snd_ctl_add(card, kctl);
 		if (err < 0) {
 			dev_err(dev, "ASoC: Failed to add %s: %d\n",
 				control->name, err);
sound/soc/soc-internal.h +11 -0
index b844b27574c8..3866da8e3bee 100644
@@ -24,6 +24,17 @@ int snd_soc_add_controls(struct snd_card *card, struct device *dev,
 			 const struct snd_kcontrol_new *controls, int num_controls,
 			 const char *prefix, void *data);
 
+/*
+ * In soc-ops
+ */
+struct snd_soc_control_field {
+	unsigned int reg;
+	unsigned int mask;
+};
+
+int snd_soc_control_fields(const struct snd_kcontrol *kctl,
+			   struct snd_soc_control_field *fields);
+
 /*
  * In soc-dai
  */
sound/soc/soc-ops-test.c +50 -0

71 lines not on this page · raw message

sound/soc/soc-ops.c +48 -0

63 lines not on this page · raw message

Signature · 1 line
2.43.0
#1 lore raw
Quoted 19 lines · HyeongJun An

A control that writes part of another control's register field also changes that control. Usually the bits it was meant to own are then out of reach. 26 of these were fixed in codec drivers this cycle. Most dated back to the first version of the driver.

So check each control as it is added. If it shares a register with a control of the same component, and the two fields overlap but are not the same, print a warning. Identical fields are not reported, as some drivers expose one field under two names.

Only controls with the volsw, volsw_sx and enum_double put callbacks are checked. For other controls the bits written are not known.

Add a KUnit test for the field calculation.

Quoted 6 lines

---

Notes:
On for-7.4 an allmodconfig build gives four warnings in three drivers:

cs42l56: ADC Mute Switch with ADCA Invert and ADCB Invert

Looking at this one I think it makes sense to drop the control here. As the control doesn't currently point to the right bits we know no one is successfully using it, and really it is simplest to have either automatic muting or manual muting not both.

I can do a patch to fix up if you like? Or happy for you to do so if you prefer?

Thanks,
Charles

#2 lore raw

Hello Charles,

Thank you for looking at this, and for the explanation. That makes sense to me. If it is all right with you, I would be glad to send the patch myself. I will Cc you on it, and I would be grateful for a review when you have a moment.

Best regards,
HyeongJun

#3 lore raw
Quoted 4 lines · HyeongJun An

Thank you for looking at this, and for the explanation. That makes sense to me. If it is all right with you, I would be glad to send the patch myself. I will Cc you on it, and I would be grateful for a review when you have a moment.

Perfect, I am more than happy help out and review the patch :-)

Thanks,
Charles

maintainer
#4 lore raw
In reply to HyeongJun An
Quoted 1 line · HyeongJun An

ASoC: core: Warn about controls whose register fields overlap

Applied to

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

Thanks!

[1/1] ASoC: core: Warn about controls whose register fields overlap

      https://git.kernel.org/broonie/sound/c/b00ec42dff99

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