From 02d900ddbe77ccb12b95b88bac89f7d85a90d599 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Fri, 6 Nov 2026 18:19:01 +1100 Subject: [PATCH] ASoC: macaudio: keep the speaker route while protection is granted "Speaker Playback Mux" picks the front-end that drives the speakers. The owner of "Speaker Protection" grants playback for the front-end its protection engine runs on, but the mux stays writable by any user of the card. On the MCA machines (J316, J413, J274, J293, J313 and their siblings) both front-ends are real PCMs, so a write under a running grant routes the other one, which carries no protection, to the amps. The owner notices on its next check of the route and withdraws, but until then that stream plays at the granted operating point. Refuse writes to the speaker mux with -EBUSY while protection is granted. Before a grant the route may change as before: the owner checks it before it grants. Signed-off-by: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Assisted-by: LLM --- sound/soc/apple/macaudio-protection-test.h | 43 ++++++++++++++++++++++ sound/soc/apple/macaudio.c | 26 ++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/sound/soc/apple/macaudio-protection-test.h b/sound/soc/apple/macaudio-protection-test.h index 6bff9c91916a0..875fe40b16e30 100644 --- a/sound/soc/apple/macaudio-protection-test.h +++ b/sound/soc/apple/macaudio-protection-test.h @@ -992,7 +992,50 @@ static void macaudio_sense_owned_writes(struct kunit *test) KUNIT_EXPECT_EQ(test, macaudio_sense_owner_check(t.ma, &other), 0); } +static void macaudio_route_is_protected(struct kunit *test) +{ + static const char *const names[] = { "Speaker Playback Mux", "Headphone Playback Mux" }; + struct macaudio_test_spk t = {}; + struct snd_kcontrol *mux[ARRAY_SIZE(names)]; + unsigned int i; + + macaudio_test_spk_card(test, &t, &macaudio_j413_cfg, SN012776_0DB); + for (i = 0; i < ARRAY_SIZE(names); i++) { + struct snd_kcontrol_new tmpl = { + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, + .name = names[i], + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, + .info = snd_ctl_boolean_mono_info, + }; + + mux[i] = macaudio_test_add_kctl(test, t.ma->card.snd_card, &tmpl, NULL); + } + macaudio_protect_sense(&t.ma->card); + KUNIT_EXPECT_TRUE(test, mux[0]->write_check == macaudio_route_write_check); + KUNIT_EXPECT_NULL(test, mux[1]->write_check); +} + +static void macaudio_route_fixed_while_granted(struct kunit *test) +{ + struct macaudio_test_spk t = {}; + + macaudio_test_spk_card(test, &t, &macaudio_j316_cfg, SN012776_0DB); + KUNIT_EXPECT_EQ(test, macaudio_route_check(t.ma), 0); + macaudio_test_take(test, &t); + /* Owned but not granted: the owner checks the route before it grants. */ + KUNIT_EXPECT_EQ(test, macaudio_route_check(t.ma), 0); + KUNIT_EXPECT_EQ(test, macaudio_test_write(test, &t, 30), 0); + KUNIT_EXPECT_EQ(test, macaudio_route_check(t.ma), -EBUSY); + KUNIT_EXPECT_EQ(test, macaudio_test_write(test, &t, 0), 0); + KUNIT_EXPECT_EQ(test, macaudio_route_check(t.ma), 0); + KUNIT_EXPECT_EQ(test, macaudio_test_write(test, &t, 30), 0); + macaudio_spk_unlock(t.spk); + KUNIT_EXPECT_EQ(test, macaudio_route_check(t.ma), 0); +} + static struct kunit_case macaudio_protection_cases[] = { + KUNIT_CASE(macaudio_route_is_protected), + KUNIT_CASE(macaudio_route_fixed_while_granted), KUNIT_CASE(macaudio_sense_owned_writes), KUNIT_CASE(macaudio_sense_controls_are_protected), KUNIT_CASE(macaudio_spk_floor_from_binding), diff --git a/sound/soc/apple/macaudio.c b/sound/soc/apple/macaudio.c index 67c8a51e0df1d..b853029f3dff3 100644 --- a/sound/soc/apple/macaudio.c +++ b/sound/soc/apple/macaudio.c @@ -1088,7 +1088,29 @@ static int macaudio_sense_write_check(struct snd_kcontrol *kctl, struct snd_ctl_ return macaudio_sense_owner_check(ma, file); } -/* Match both mono and prefixed amp DAPM switches; leave jack controls alone. */ +/* + * The speaker route may not change under a grant: the owner admitted the + * front-end its engine drives, and another one routed to the speakers would + * reach the amps without protection until the owner noticed. Re-route only + * while protection is withdrawn. + */ +static int macaudio_route_check(struct macaudio_snd_data *ma) +{ + /* Pairs with the release in macaudio_spk_grant() */ + return smp_load_acquire(&ma->spk_granted) ? -EBUSY : 0; +} + +static int macaudio_route_write_check(struct snd_kcontrol *kctl, struct snd_ctl_file *file) +{ + struct snd_soc_card *card = snd_soc_dapm_to_card(snd_soc_dapm_kcontrol_to_dapm(kctl)); + + return macaudio_route_check(snd_soc_card_get_drvdata(card)); +} + +/* + * Match both mono and prefixed amp DAPM sense switches, and the speaker + * route; leave jack controls alone. + */ static void macaudio_protect_sense(struct snd_soc_card *card) { struct snd_kcontrol *kctl; @@ -1099,6 +1121,8 @@ static void macaudio_protect_sense(struct snd_soc_card *card) !strcmp(kctl->id.name, "ISENSE Switch") || !strcmp(kctl->id.name, "VSENSE Switch")) kctl->write_check = macaudio_sense_write_check; + else if (!strcmp(kctl->id.name, "Speaker Playback Mux")) + kctl->write_check = macaudio_route_write_check; } }