From 8788ddba886ef9787f0c76b95260d573832d491f Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 28 Nov 2026 15:44:04 +1100 Subject: [PATCH 1/2] ASoC: apple: macaudio: only the Speaker Protection lease lifts the speakers The speakers had two owners. "Speaker Protection", the lease of a protection engine in the signal path (speakerguardd v2), and "Speaker Volume Unlock", the volume lock of the older daemons (speakersafetyd, speakerguardd 0.1): a process that held ELEM_LOCK on that control and on every speaker volume, and pinged it with SPEAKER_MAGIC_VALUE, lifted the volumes to the amps' 0 dB on any machine whose interface was not switched to the lease, which is every machine at every bind. Make the lease the only way up, on every machine: - ELEM_LOCK on "Speaker Volume Unlock" fails with -EPERM, and so does the magic ping, with a one-time log line saying why, so that an older daemon fails at once instead of believing it guards the speakers. 0 and 1 stay accepted no-ops so that a saved mixer state still restores, and the control reads 0. - macaudio_vlimit_unlock() lifts the ceiling only while leased, so nothing outside macaudio_spk_enter_lease() can lift it. - The volume lock's lease machinery is gone with its only user: the ping deadline and its works, the back-end trigger bookkeeping that drove them, and the speaker volumes the system-sleep lock kept for the daemon's next ping. Unchanged: "Speaker Volume Unlock" is still the interface at every bind, with the speaker volumes at its ceiling; a CAP_SYS_ADMIN write of "Speaker Protection" to "Speaker Protection Interface" hands the speakers to the lease through the floor, which grants as before; the 30 s unowned revert and an administrator's revert put them back at the floor under the ceiling; system sleep withdraws the lease as before. The KUnit suites follow: apple-speaker-volume-unlock and apple-speaker-pm-volumes now prove that the lock and the ping are refused and lift nothing, across a system sleep too, and that the ceiling cannot be lifted outside the lease; apple-speaker-interface keeps the switch, grant, revert and unowned cases with the older daemon refused throughout. Signed-off-by: Ryan Murray --- sound/soc/apple/macaudio-iface-test.h | 64 +-- sound/soc/apple/macaudio-pm-volumes-test.h | 99 ++-- sound/soc/apple/macaudio-protection-test.h | 1 - sound/soc/apple/macaudio-vlimit-test.h | 215 ++------ sound/soc/apple/macaudio.c | 588 +++------------------ 5 files changed, 171 insertions(+), 796 deletions(-) diff --git a/sound/soc/apple/macaudio-iface-test.h b/sound/soc/apple/macaudio-iface-test.h index 6d54f87fcd5fb..6632ad0126898 100644 --- a/sound/soc/apple/macaudio-iface-test.h +++ b/sound/soc/apple/macaudio-iface-test.h @@ -1,8 +1,9 @@ /* SPDX-License-Identifier: GPL-2.0-only */ /* * "Speaker Protection Interface": the volume lock in charge from the bind, - * the switch to the lease and back through the floor of both, and the way - * back when the lease has no owner. On the card of fake controls that + * at a ceiling that nothing lifts, the switch to the lease (the only owner + * that lifts the speakers) and back through the floor, and the way back + * when the lease has no owner. On the card of fake controls that * macaudio-protection-test.h builds, with the volume lock's controls added. */ #include @@ -11,7 +12,7 @@ struct macaudio_test_iface { struct macaudio_test_spk t; struct snd_kcontrol *slk; struct snd_kcontrol *iface; - /* The legacy daemon's control file */ + /* A retired daemon's control file (speakersafetyd, speakerguardd 0.1) */ struct snd_ctl_file *daemon; }; @@ -61,14 +62,14 @@ static int macaudio_test_iface_write(struct kunit *test, struct macaudio_test_if return macaudio_spk_iface_put(f->iface, uval); } -/* The legacy daemon takes the lock and every speaker volume */ +/* A retired daemon holds every speaker volume; the lock control is refused */ static void macaudio_test_daemon_take(struct kunit *test, struct macaudio_test_iface *f) { unsigned int i; for (i = 0; i < MACAUDIO_TEST_NR_SPK; i++) f->t.vols[i]->vd[0].owner = f->daemon; - KUNIT_ASSERT_EQ(test, macaudio_slk_lock(f->slk, f->daemon), 0); + KUNIT_EXPECT_EQ(test, macaudio_slk_lock(f->slk, f->daemon), -EPERM); } static int macaudio_test_daemon_ping(struct kunit *test, struct macaudio_test_iface *f) @@ -111,37 +112,32 @@ static void macaudio_iface_volume_lock_from_binding(struct kunit *test) macaudio_test_expect_limit(test, &f, 1, 1); KUNIT_EXPECT_FALSE(test, delayed_work_pending(&f.t.ma->spk_unowned_work)); - /* Speaker volumes are the daemon's to write */ + /* Speaker volumes are writable, below the ceiling */ KUNIT_EXPECT_EQ(test, f.t.vols[0]->write_check(f.t.vols[0], NULL), 0); - /* The daemon unlocks them as on a lock-only kernel */ + /* A retired daemon's lock and ping are refused and lift nothing */ macaudio_test_daemon_take(test, &f); - KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); - KUNIT_EXPECT_TRUE(test, f.t.ma->speaker_volume_unlocked); - macaudio_test_expect_limit(test, &f, 1, SN012776_0DB); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); + macaudio_test_expect_limit(test, &f, 1, 1); } -/* Into the lease: the lock's ceiling, then the lease's floor, then the lift */ +/* Into the lease: the lease's floor, then the lift */ static void macaudio_iface_switch_goes_through_the_floor(struct kunit *test) { struct macaudio_test_iface f; - unsigned int i; macaudio_test_iface_card(test, &f); macaudio_test_daemon_take(test, &f); - KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); - for (i = 0; i < MACAUDIO_TEST_NR_SPK; i++) - macaudio_test_vol(f.t.vols[i])->value = 180; + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), 1); KUNIT_EXPECT_TRUE(test, macaudio_spk_leased(f.t.ma)); - KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); /* At the lease's floor, below a ceiling that no longer applies */ macaudio_test_expect_limit(test, &f, 0, SN012776_0DB); KUNIT_EXPECT_FALSE(test, macaudio_spk_admitted(f.t.ma)); /* The volume lock is refused, and so are the daemon's volume writes */ - KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EBUSY); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); KUNIT_EXPECT_EQ(test, f.t.vols[0]->write_check(f.t.vols[0], NULL), -EPERM); KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), 0); @@ -156,8 +152,7 @@ static void macaudio_iface_switch_goes_through_the_floor(struct kunit *test) /* * Back to the volume lock: the speakers come back at the floor under the - * lock's ceiling, and go up only once the daemon pings, as after a cold - * bind; its first ping learns that it lost the lock. + * lock's ceiling, and stay there: no daemon lifts them. */ static void macaudio_iface_revert_starts_at_the_floor(struct kunit *test) { @@ -174,20 +169,23 @@ static void macaudio_iface_revert_starts_at_the_floor(struct kunit *test) macaudio_test_settle(f.t.ma); KUNIT_EXPECT_FALSE(test, macaudio_spk_leased(f.t.ma)); KUNIT_EXPECT_FALSE(test, macaudio_spk_admitted(f.t.ma)); - KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); macaudio_test_expect_limit(test, &f, 0, 1); /* The lease grants nothing any more */ KUNIT_EXPECT_EQ(test, macaudio_test_write(test, &f.t, 50), -EBUSY); macaudio_test_expect_limit(test, &f, 0, 1); - /* The daemon's lock was lost: told once, then it unlocks */ - KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -ETIMEDOUT); - KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); + /* Nor does the retired daemon, however often it pings */ + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); macaudio_test_expect_limit(test, &f, 0, 1); - KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); - KUNIT_EXPECT_TRUE(test, f.t.ma->speaker_volume_unlocked); - /* Unlocking lifts the ceiling only: the daemon sets the volumes itself */ - macaudio_test_expect_limit(test, &f, 0, SN012776_0DB); + + /* The lease takes them again and grants as before */ + KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), 1); + KUNIT_EXPECT_EQ(test, macaudio_test_write(test, &f.t, 50), 0); + KUNIT_EXPECT_TRUE(test, macaudio_spk_admitted(f.t.ma)); + macaudio_test_expect_limit(test, &f, SN012776_0DB, SN012776_0DB); + KUNIT_EXPECT_EQ(test, macaudio_test_write(test, &f.t, 0), 0); + macaudio_test_settle(f.t.ma); } /* The engine dies: the lease's floor at once, the volume lock after the grace */ @@ -220,7 +218,6 @@ static void macaudio_iface_unowned_lease_reverts(struct kunit *test) flush_delayed_work(&f.t.ma->spk_unowned_work); macaudio_test_settle(f.t.ma); KUNIT_EXPECT_FALSE(test, macaudio_spk_leased(f.t.ma)); - KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); macaudio_test_expect_limit(test, &f, 0, 1); } @@ -249,7 +246,7 @@ static void macaudio_iface_needs_cap_sys_admin(struct kunit *test) * J813: its four SN012776 (the fake card's Left/Right Woofer/Tweeter * volumes, the J813 amps' prefixes) are behind both interfaces from the * bind, as on the MCA machines. Under the volume lock they stay at its - * ceiling until a daemon holds the lock and pings; under the lease they are + * ceiling, whatever a daemon does; under the lease they are * at the floor and no speaker start is admitted until an owner is granted. */ static void macaudio_iface_j813_speakers_protected(struct kunit *test) @@ -259,21 +256,16 @@ static void macaudio_iface_j813_speakers_protected(struct kunit *test) KUNIT_ASSERT_TRUE(test, macaudio_speakers_allowed(&macaudio_j813_cfg)); macaudio_test_iface_card_cfg(test, &f, &macaudio_j813_cfg); KUNIT_EXPECT_FALSE(test, macaudio_spk_leased(f.t.ma)); - KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); macaudio_test_expect_limit(test, &f, 1, 1); - /* A daemon that holds the lock but has not pinged lifts nothing */ + /* A daemon that holds the volumes and pings lifts nothing */ macaudio_test_daemon_take(test, &f); - KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); macaudio_test_expect_limit(test, &f, 1, 1); - KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); - KUNIT_EXPECT_TRUE(test, f.t.ma->speaker_volume_unlocked); - macaudio_test_expect_limit(test, &f, 1, SN012776_0DB); /* Into the lease: the floor, and nothing admitted without an owner */ KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), 1); KUNIT_EXPECT_TRUE(test, macaudio_spk_leased(f.t.ma)); - KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); KUNIT_EXPECT_FALSE(test, macaudio_spk_admitted(f.t.ma)); macaudio_test_expect_limit(test, &f, 0, SN012776_0DB); KUNIT_EXPECT_EQ(test, macaudio_test_write(test, &f.t, 50), -EPERM); diff --git a/sound/soc/apple/macaudio-pm-volumes-test.h b/sound/soc/apple/macaudio-pm-volumes-test.h index dab9d69bb0faa..3b58f7faf9282 100644 --- a/sound/soc/apple/macaudio-pm-volumes-test.h +++ b/sound/soc/apple/macaudio-pm-volumes-test.h @@ -1,7 +1,8 @@ /* SPDX-License-Identifier: GPL-2.0-only */ /* - * The speaker volumes the system-sleep lock clips are set again at the - * daemon's first ping after it, through the lock control and the PM notifier. + * Under "Speaker Volume Unlock" the speaker volumes sit at its ceiling from + * the bind, and a system sleep, a retired daemon's lock and ping, or both, + * never lift it. The card here is shared with macaudio-vlimit-test.h. */ #include @@ -49,8 +50,6 @@ static void macaudio_pmt_pm_cleanup(void *data) { struct macaudio_snd_data *ma = data; - cancel_delayed_work_sync(&ma->lock_timeout_work); - macaudio_pm_volumes_drop(ma); macaudio_spk_fini(ma); } @@ -59,11 +58,11 @@ static const char *const macaudio_pmt_vol_names[] = { "Right Woofer Speaker Volume", "Left Tweeter Speaker Volume", "Right Tweeter Speaker Volume", - /* Not a speaker volume: never locked, kept or set */ + /* Not a speaker volume: never clipped */ "Headphone Playback Volume", }; -/* As the daemon set them before the sleep */ +/* What the amps' volumes held before the bind */ static const long macaudio_pmt_vols[] = { 201, 190, 201, 170, 120 }; #define MACAUDIO_PMT_NR_VOLS ARRAY_SIZE(macaudio_pmt_vol_names) @@ -88,7 +87,7 @@ static struct snd_kcontrol *macaudio_pmt_add_kctl(struct kunit *test, struct snd return kctl; } -/* A card with speaker safety, its lock control and the volumes, none owned yet */ +/* A bound card with speaker safety, its lock control and the volumes at its ceiling */ static void macaudio_pmt_pm_card(struct kunit *test, struct macaudio_pmt_pm *t) { struct snd_kcontrol_new slk = { @@ -97,6 +96,7 @@ static void macaudio_pmt_pm_card(struct kunit *test, struct macaudio_pmt_pm *t) .info = snd_ctl_boolean_mono_info, .get = macaudio_slk_get, .put = macaudio_slk_put, + .lock = macaudio_slk_lock, }; struct macaudio_snd_data *ma; struct snd_card *card; @@ -119,7 +119,6 @@ static void macaudio_pmt_pm_card(struct kunit *test, struct macaudio_pmt_pm *t) ma->has_safety = true; INIT_LIST_HEAD(&ma->card.rtd_list); mutex_init(&ma->volume_lock_mutex); - INIT_DELAYED_WORK(&ma->lock_timeout_work, macaudio_vlimit_timeout_work); /* The volume lock is the interface, as at every bind */ mutex_init(&ma->spk_mutex); KUNIT_ASSERT_EQ(test, macaudio_spk_init(ma), 0); @@ -147,9 +146,17 @@ static void macaudio_pmt_pm_card(struct kunit *test, struct macaudio_pmt_pm *t) ret = kunit_add_action_or_reset(test, macaudio_pmt_pm_cleanup, ma); KUNIT_ASSERT_EQ(test, ret, 0); + + /* As macaudio_late_probe() does at every bind */ + mutex_lock(&ma->volume_lock_mutex); + macaudio_vlimit_unlock(ma, false); + mutex_unlock(&ma->volume_lock_mutex); } -/* A daemon takes the lock and every speaker volume, as speakersafetyd does */ +/* + * A retired daemon (speakersafetyd, speakerguardd 0.1) holds every speaker + * volume and asks for the lock control: refused. + */ static void macaudio_pmt_take(struct kunit *test, struct macaudio_pmt_pm *t) { struct snd_ctl_file *owner = kunit_kzalloc(test, sizeof(*owner), GFP_KERNEL); @@ -158,7 +165,7 @@ static void macaudio_pmt_take(struct kunit *test, struct macaudio_pmt_pm *t) KUNIT_ASSERT_NOT_NULL(test, owner); for (i = 0; i < MACAUDIO_PMT_NR_VOLS - 1; i++) t->vols[i]->vd[0].owner = owner; - KUNIT_ASSERT_EQ(test, macaudio_slk_lock(t->slk, owner), 0); + KUNIT_EXPECT_EQ(test, macaudio_slk_lock(t->slk, owner), -EPERM); } static int macaudio_pmt_ping(struct kunit *test, struct macaudio_pmt_pm *t) @@ -175,9 +182,8 @@ static void macaudio_pmt_notify(struct macaudio_pmt_pm *t, unsigned long action) macaudio_pm_notify(&t->ma->pm_nb, action, NULL); } -/* The speaker volumes as the daemon set them, or clipped to the lock's ceiling */ -static void macaudio_pmt_expect_vols(struct kunit *test, struct macaudio_pmt_pm *t, - bool clipped) +/* Every speaker volume clipped to the lock's ceiling, and limited there */ +static void macaudio_pmt_expect_vols(struct kunit *test, struct macaudio_pmt_pm *t) { unsigned int i; @@ -185,79 +191,48 @@ static void macaudio_pmt_expect_vols(struct kunit *test, struct macaudio_pmt_pm bool speaker = i < MACAUDIO_PMT_NR_VOLS - 1; KUNIT_EXPECT_EQ(test, macaudio_pmt_vol(t->vols[i])->value, - speaker && clipped ? 1L : macaudio_pmt_vols[i]); + speaker ? 1L : macaudio_pmt_vols[i]); + if (speaker) + KUNIT_EXPECT_EQ(test, macaudio_pmt_vol(t->vols[i])->mc.platform_max, 1); } } -static void macaudio_pm_volumes_restored_after_sleep(struct kunit *test) +/* A sleep under the volume lock leaves the ceiling where it was */ +static void macaudio_pm_volumes_sleep_keeps_the_ceiling(struct kunit *test) { struct macaudio_pmt_pm t; macaudio_pmt_pm_card(test, &t); - macaudio_pmt_take(test, &t); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); - macaudio_pmt_expect_vols(test, &t, false); + macaudio_pmt_expect_vols(test, &t); macaudio_pmt_notify(&t, PM_SUSPEND_PREPARE); - KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); - macaudio_pmt_expect_vols(test, &t, true); - /* A ping before the freeze leaves them locked */ - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); - - /* Locked after the resume until the daemon pings */ + macaudio_pmt_expect_vols(test, &t); macaudio_pmt_notify(&t, PM_POST_SUSPEND); - KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); - macaudio_pmt_expect_vols(test, &t, true); - - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); - macaudio_pmt_expect_vols(test, &t, false); - KUNIT_EXPECT_NULL(test, t.ma->pm_volumes); + macaudio_pmt_expect_vols(test, &t); + KUNIT_EXPECT_FALSE(test, macaudio_spk_leased(t.ma)); } -/* The daemon pings only once a stream runs: a second sleep can come first. */ -static void macaudio_pm_volumes_second_sleep_before_ping(struct kunit *test) +/* A retired daemon's lock and pings around a sleep lift nothing */ +static void macaudio_pm_volumes_daemon_across_sleep(struct kunit *test) { struct macaudio_pmt_pm t; macaudio_pmt_pm_card(test, &t); macaudio_pmt_take(test, &t); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); + macaudio_pmt_expect_vols(test, &t); macaudio_pmt_notify(&t, PM_SUSPEND_PREPARE); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); macaudio_pmt_notify(&t, PM_POST_SUSPEND); - macaudio_pmt_notify(&t, PM_SUSPEND_PREPARE); - macaudio_pmt_notify(&t, PM_POST_SUSPEND); - macaudio_pmt_expect_vols(test, &t, true); - - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - macaudio_pmt_expect_vols(test, &t, false); -} - -/* A new owner, such as a restarted daemon, sets the volumes itself. */ -static void macaudio_pm_volumes_new_owner(struct kunit *test) -{ - struct macaudio_pmt_pm t; - - macaudio_pmt_pm_card(test, &t); - macaudio_pmt_take(test, &t); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - macaudio_pmt_notify(&t, PM_SUSPEND_PREPARE); - macaudio_pmt_notify(&t, PM_POST_SUSPEND); - macaudio_pmt_take(test, &t); - KUNIT_EXPECT_NULL(test, t.ma->pm_volumes); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); - macaudio_pmt_expect_vols(test, &t, true); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); + macaudio_pmt_expect_vols(test, &t); } static struct kunit_case macaudio_pm_volumes_cases[] = { - KUNIT_CASE(macaudio_pm_volumes_restored_after_sleep), - KUNIT_CASE(macaudio_pm_volumes_second_sleep_before_ping), - KUNIT_CASE(macaudio_pm_volumes_new_owner), + KUNIT_CASE(macaudio_pm_volumes_sleep_keeps_the_ceiling), + KUNIT_CASE(macaudio_pm_volumes_daemon_across_sleep), {} }; diff --git a/sound/soc/apple/macaudio-protection-test.h b/sound/soc/apple/macaudio-protection-test.h index 1f6cd5ddb987d..3cc43fe67fd82 100644 --- a/sound/soc/apple/macaudio-protection-test.h +++ b/sound/soc/apple/macaudio-protection-test.h @@ -164,7 +164,6 @@ static void macaudio_test_spk_card(struct kunit *test, struct macaudio_test_spk ma->has_speakers = true; mutex_init(&ma->spk_mutex); mutex_init(&ma->volume_lock_mutex); - INIT_DELAYED_WORK(&ma->lock_timeout_work, macaudio_vlimit_timeout_work); INIT_WORK(&ma->lock_fail_work, macaudio_lock_fail_work); INIT_WORK(&ma->sw_gain_stop_work, macaudio_sw_gain_stop_work); /* The lease is the interface, as after "Speaker Protection Interface" */ diff --git a/sound/soc/apple/macaudio-vlimit-test.h b/sound/soc/apple/macaudio-vlimit-test.h index 26b8764805a65..f2341ebcae95d 100644 --- a/sound/soc/apple/macaudio-vlimit-test.h +++ b/sound/soc/apple/macaudio-vlimit-test.h @@ -1,12 +1,11 @@ /* SPDX-License-Identifier: GPL-2.0-only */ /* - * The "Speaker Volume Unlock" lease: the speaker volumes leave the safe - * ceiling only while one process holds the lock control and every speaker - * volume, and pings within SPEAKER_LOCK_TIMEOUT ms while speaker playback - * runs. Built on the card of macaudio-pm-volumes-test.h. + * "Speaker Volume Unlock" lifts nothing: the lock and ping of the daemons + * that used to lift the speaker volumes from it (speakersafetyd, + * speakerguardd 0.1) are refused, and the volumes stay at the ceiling. + * Built on the card of macaudio-pm-volumes-test.h. */ #include -#include static int macaudio_vlt_put(struct kunit *test, struct macaudio_pmt_pm *t, long val) { @@ -26,15 +25,6 @@ static long macaudio_vlt_get(struct kunit *test, struct macaudio_pmt_pm *t) return uval->value.integer.value[0]; } -/* Speaker playback runs (or stops): the ping deadline is armed (or held). */ -static void macaudio_vlt_playing(struct macaudio_pmt_pm *t, bool playing) -{ - if (playing) - macaudio_vlimit_enable_timeout(t->ma); - else - macaudio_vlimit_disable_timeout(t->ma); -} - /* Saved mixer states hold 0 or 1: restoring one is a no-op, nothing else is. */ static void macaudio_vlimit_restore_values_are_noops(struct kunit *test) { @@ -45,201 +35,66 @@ static void macaudio_vlimit_restore_values_are_noops(struct kunit *test) KUNIT_EXPECT_EQ(test, macaudio_vlt_put(test, &t, 1), 0); KUNIT_EXPECT_EQ(test, macaudio_vlt_put(test, &t, 2), -EINVAL); KUNIT_EXPECT_EQ(test, macaudio_vlt_put(test, &t, SPEAKER_MAGIC_VALUE + 1), -EINVAL); - KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); - KUNIT_EXPECT_EQ(test, macaudio_vlt_get(test, &t), 0L); - - /* Even for the owner */ - macaudio_pmt_take(test, &t); - KUNIT_EXPECT_EQ(test, macaudio_vlt_put(test, &t, 1), 0); - KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); -} - -/* A ping without the lock is refused and unlocks nothing. */ -static void macaudio_vlimit_ping_needs_the_lock(struct kunit *test) -{ - struct macaudio_pmt_pm t; - - macaudio_pmt_pm_card(test, &t); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); - KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); - KUNIT_EXPECT_EQ(test, macaudio_vlt_get(test, &t), 0L); -} - -/* The lock alone is not enough: every speaker volume must be the owner's. */ -static void macaudio_vlimit_needs_every_volume(struct kunit *test) -{ - struct snd_ctl_file *other = kunit_kzalloc(test, sizeof(*other), GFP_KERNEL); - struct macaudio_pmt_pm t; - unsigned int i; - - KUNIT_ASSERT_NOT_NULL(test, other); - for (i = 0; i < MACAUDIO_PMT_NR_VOLS - 1; i++) { - macaudio_pmt_pm_card(test, &t); - macaudio_pmt_take(test, &t); - t.vols[i]->vd[0].owner = other; - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - KUNIT_EXPECT_FALSE_MSG(test, t.ma->speaker_volume_unlocked, - "%s held by another file", macaudio_pmt_vol_names[i]); - - macaudio_pmt_pm_card(test, &t); - macaudio_pmt_take(test, &t); - t.vols[i]->vd[0].owner = NULL; - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - KUNIT_EXPECT_FALSE_MSG(test, t.ma->speaker_volume_unlocked, - "%s not locked", macaudio_pmt_vol_names[i]); - } - - /* A volume that is not a speaker volume does not count */ - macaudio_pmt_pm_card(test, &t); - macaudio_pmt_take(test, &t); - t.vols[MACAUDIO_PMT_NR_VOLS - 1]->vd[0].owner = other; - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); -} - -/* Closing the lock relocks at once and clips the speaker volumes only. */ -static void macaudio_vlimit_unlock_clips(struct kunit *test) -{ - struct macaudio_pmt_pm t; - - macaudio_pmt_pm_card(test, &t); - macaudio_pmt_take(test, &t); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); - KUNIT_EXPECT_EQ(test, macaudio_vlt_get(test, &t), 1L); - macaudio_pmt_expect_vols(test, &t, false); - - macaudio_slk_unlock(t.slk); - KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); KUNIT_EXPECT_EQ(test, macaudio_vlt_get(test, &t), 0L); - macaudio_pmt_expect_vols(test, &t, true); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); - KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + macaudio_pmt_expect_vols(test, &t); } -/* - * While playback runs the lease ends SPEAKER_LOCK_TIMEOUT ms after the last - * ping. The owner's next ping reports the loss with -ETIMEDOUT and unlocks - * nothing; only the ping after it starts a new lease. - */ -static void macaudio_vlimit_lease_expires_while_playing(struct kunit *test) +/* No file may take the lock control, whoever it is */ +static void macaudio_vlimit_lock_refused(struct kunit *test) { + struct snd_ctl_file *file = kunit_kzalloc(test, sizeof(*file), GFP_KERNEL); struct macaudio_pmt_pm t; + KUNIT_ASSERT_NOT_NULL(test, file); macaudio_pmt_pm_card(test, &t); - macaudio_pmt_take(test, &t); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - macaudio_vlt_playing(&t, true); - KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); - - msleep(SPEAKER_LOCK_TIMEOUT * 2); - flush_delayed_work(&t.ma->lock_timeout_work); - KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); - macaudio_pmt_expect_vols(test, &t, true); - - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -ETIMEDOUT); - KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); - macaudio_vlt_playing(&t, false); + /* KUnit runs with CAP_SYS_ADMIN: root is refused too */ + KUNIT_EXPECT_TRUE(test, capable(CAP_SYS_ADMIN)); + KUNIT_EXPECT_EQ(test, macaudio_slk_lock(t.slk, file), -EPERM); + KUNIT_EXPECT_EQ(test, macaudio_slk_lock(t.slk, file), -EPERM); + macaudio_pmt_expect_vols(test, &t); } /* - * Each ping moves the deadline to SPEAKER_LOCK_TIMEOUT ms after itself and - * re-arms the expiry. Checked on the deadline, not by racing the clock. + * A daemon that holds every speaker volume and pings, as speakersafetyd + * does, is refused every time and lifts nothing. */ -static void macaudio_vlimit_pings_keep_the_lease(struct kunit *test) +static void macaudio_vlimit_daemon_never_unlocks(struct kunit *test) { struct macaudio_pmt_pm t; - ktime_t before, deadline; unsigned int i; macaudio_pmt_pm_card(test, &t); macaudio_pmt_take(test, &t); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - macaudio_vlt_playing(&t, true); - deadline = t.ma->speaker_lock_timeout; - for (i = 0; i < 3; i++) { - usleep_range(1000, 2000); - before = ktime_get(); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); - KUNIT_EXPECT_GT(test, t.ma->speaker_lock_timeout, deadline); - KUNIT_EXPECT_GE(test, t.ma->speaker_lock_timeout, - ktime_add_ms(before, SPEAKER_LOCK_TIMEOUT)); - KUNIT_EXPECT_TRUE(test, delayed_work_pending(&t.ma->lock_timeout_work)); - deadline = t.ma->speaker_lock_timeout; - } - macaudio_vlt_playing(&t, false); -} - -/* - * Without playback the deadline is held: an idle owner keeps its lease, and - * the time it had left runs on when playback starts again. - */ -static void macaudio_vlimit_idle_holds_the_lease(struct kunit *test) -{ - struct macaudio_pmt_pm t; - - macaudio_pmt_pm_card(test, &t); - macaudio_pmt_take(test, &t); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - msleep(SPEAKER_LOCK_TIMEOUT * 2); - KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); - KUNIT_EXPECT_EQ(test, t.ma->speaker_lock_remain, ms_to_ktime(SPEAKER_LOCK_TIMEOUT)); - KUNIT_EXPECT_FALSE(test, delayed_work_pending(&t.ma->lock_timeout_work)); - - /* The time left runs from the start of playback and then ends */ - macaudio_vlt_playing(&t, true); - KUNIT_EXPECT_TRUE(test, delayed_work_pending(&t.ma->lock_timeout_work)); - msleep(SPEAKER_LOCK_TIMEOUT * 2); - flush_delayed_work(&t.ma->lock_timeout_work); - KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); - macaudio_vlt_playing(&t, false); -} - -/* A new owner never inherits the previous owner's lease. */ -static void macaudio_vlimit_new_owner_pings_first(struct kunit *test) -{ - struct macaudio_pmt_pm t; + for (i = 0; i < 8; i++) + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); + KUNIT_EXPECT_EQ(test, macaudio_vlt_get(test, &t), 0L); + macaudio_pmt_expect_vols(test, &t); + /* A ping without even holding the volumes fares no better */ macaudio_pmt_pm_card(test, &t); - macaudio_pmt_take(test, &t); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); - - macaudio_pmt_take(test, &t); - KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); - KUNIT_EXPECT_LE(test, t.ma->speaker_lock_remain, 0); - /* The relock was the switch of owners, not a lost lease */ - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); + macaudio_pmt_expect_vols(test, &t); } -/* A card without speaker safety never touches its volumes. */ -static void macaudio_vlimit_without_safety(struct kunit *test) +/* Outside the lease the driver itself cannot lift the ceiling either */ +static void macaudio_vlimit_lift_needs_the_lease(struct kunit *test) { struct macaudio_pmt_pm t; macaudio_pmt_pm_card(test, &t); - t.ma->has_safety = false; - macaudio_pmt_take(test, &t); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); - KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); - macaudio_slk_unlock(t.slk); - macaudio_pmt_expect_vols(test, &t, false); + KUNIT_ASSERT_FALSE(test, macaudio_spk_leased(t.ma)); + KUNIT_ASSERT_FALSE(test, macaudio_spk_lease_only(t.ma->cfg)); + mutex_lock(&t.ma->volume_lock_mutex); + macaudio_vlimit_unlock(t.ma, true); + mutex_unlock(&t.ma->volume_lock_mutex); + macaudio_pmt_expect_vols(test, &t); } static struct kunit_case macaudio_vlimit_cases[] = { KUNIT_CASE(macaudio_vlimit_restore_values_are_noops), - KUNIT_CASE(macaudio_vlimit_ping_needs_the_lock), - KUNIT_CASE(macaudio_vlimit_needs_every_volume), - KUNIT_CASE(macaudio_vlimit_unlock_clips), - KUNIT_CASE_SLOW(macaudio_vlimit_lease_expires_while_playing), - KUNIT_CASE(macaudio_vlimit_pings_keep_the_lease), - KUNIT_CASE_SLOW(macaudio_vlimit_idle_holds_the_lease), - KUNIT_CASE(macaudio_vlimit_new_owner_pings_first), - KUNIT_CASE(macaudio_vlimit_without_safety), + KUNIT_CASE(macaudio_vlimit_lock_refused), + KUNIT_CASE(macaudio_vlimit_daemon_never_unlocks), + KUNIT_CASE(macaudio_vlimit_lift_needs_the_lease), {} }; diff --git a/sound/soc/apple/macaudio.c b/sound/soc/apple/macaudio.c index b641b455dd7f4..818fda16d4257 100644 --- a/sound/soc/apple/macaudio.c +++ b/sound/soc/apple/macaudio.c @@ -66,11 +66,10 @@ */ #define MACAUDIO_MAX_BCLK_FREQ 24576000 +/* The ping of the retired "Speaker Volume Unlock" daemons, refused */ #define SPEAKER_MAGIC_VALUE (s32)0xdec1be15 /* Primary, Secondary and Speaker Sense */ #define MACAUDIO_NUM_FE_LINKS 3 -/* The "Speaker Volume Unlock" lease, in milliseconds */ -#define SPEAKER_LOCK_TIMEOUT 250 /* * While "Speaker Protection" is the interface, this long without an owner of * it (the engine died, or never came) puts "Speaker Volume Unlock" back in @@ -228,16 +227,11 @@ struct ma_codec_idle { u32 idle_mask; }; -/* A speaker volume control and its value, as the system-sleep lock found it */ -struct macaudio_pm_volume { - struct snd_kcontrol *kctl; - long value[2]; -}; - /* * The interface that owns the speakers (see "Speaker Protection Interface"): - * the volume lock of speakersafetyd and speakerguardd 0.1, or the lease of a - * protection engine in the signal path. + * the volume lock, which holds the speaker volumes at its ceiling and lifts + * them for no one, or the lease of a protection engine in the signal path, + * the only owner that can lift them. */ enum macaudio_spk_iface { MACAUDIO_SPK_IFACE_UNLOCK, @@ -358,32 +352,15 @@ struct macaudio_snd_data { struct snd_soc_component *sw_gain_component; /* - * "Speaker Volume Unlock": the speakersafetyd / speakerguardd 0.1 - * interface (see macaudio_vlimit_update()). + * "Speaker Volume Unlock": the speaker volumes' ceiling outside the + * lease. Nothing lifts it: the retired daemons' lock and ping on the + * control are refused (macaudio_slk_lock(), macaudio_slk_put()). */ int speaker_sample_rate; struct snd_kcontrol *speaker_sample_rate_kctl; struct mutex volume_lock_mutex; - bool speaker_volume_unlocked; - bool speaker_volume_was_locked; struct snd_kcontrol *speaker_lock_kctl; - struct snd_ctl_file *speaker_lock_owner; - u64 bes_active; - bool speaker_lock_timeout_enabled; - ktime_t speaker_lock_timeout; - ktime_t speaker_lock_remain; - struct delayed_work lock_timeout_work; - struct work_struct lock_update_work; - /* - * The speaker volumes as the system-sleep lock found them. Locking clips - * them to its ceiling and unlocking only lifts the ceiling; the daemon - * keeps its lease across the sleep and does not set them again. - */ - struct macaudio_pm_volume *pm_volumes; - unsigned int pm_nr_volumes; - /* Allocated with them, so that setting them again cannot fail */ - struct snd_ctl_elem_value *pm_uval; /* * Software-gain amplifier: a lower gain cannot change samples that @@ -528,15 +505,12 @@ static struct macaudio_link_props macaudio_fe_link_props[] = { }; /* - * "Speaker Volume Unlock": speaker protection by a userspace daemon - * (speakersafetyd, speakerguardd 0.1), the default interface. - * - * The daemon holds SNDRV_CTL_IOCTL_ELEM_LOCK on "Speaker Volume Unlock" and - * on every speaker volume control, and pings the unlock control with - * SPEAKER_MAGIC_VALUE at least every SPEAKER_LOCK_TIMEOUT ms while the - * speakers play. While it does, the volumes may go up to the amps' 0 dB; - * otherwise they are held at the lock's ceiling. The daemon governs them - * from its thermal model. + * "Speaker Volume Unlock": the default interface, from every bind. The + * speaker volumes are held at its ceiling, the safe level, and nothing + * lifts it: only the "Speaker Protection" lease can (see + * macaudio_spk_enter_lease()). The lock and ping of the daemons that used + * to lift it (SNDRV_CTL_IOCTL_ELEM_LOCK on the control and every speaker + * volume, then SPEAKER_MAGIC_VALUE written to it) are refused. */ static void macaudio_vlimit_unlock(struct macaudio_snd_data *ma, bool unlock) @@ -551,6 +525,9 @@ static void macaudio_vlimit_unlock(struct macaudio_snd_data *ma, bool unlock) return; } + /* Only the lease lifts the ceiling (macaudio_spk_enter_lease()) */ + unlock = unlock && macaudio_spk_leased(ma); + switch (ma->cfg->amp) { case AMP_NONE: WARN_ON_ONCE(1); @@ -666,323 +643,6 @@ static void macaudio_lock_fail_work(struct work_struct *work) macaudio_stop_playback(ma, false); } -static void macaudio_pm_volumes_drop(struct macaudio_snd_data *ma) -{ - kfree(ma->pm_volumes); - kfree(ma->pm_uval); - ma->pm_volumes = NULL; - ma->pm_nr_volumes = 0; - ma->pm_uval = NULL; -} - -/* - * Keep the speaker volumes before the system-sleep lock clips them. - * - * Context: volume_lock_mutex and the card's controls_rwsem held, so the daemon - * cannot set a volume between this and the clip. - */ -static void macaudio_pm_volumes_save(struct macaudio_snd_data *ma) -{ - const char *name = volume_control_names[ma->cfg->amp]; - struct snd_ctl_elem_value *uval = NULL; - struct macaudio_pm_volume *vols = NULL; - struct snd_kcontrol *kctl; - unsigned int nr = 0, i = 0; - int ret; - - lockdep_assert_held(&ma->volume_lock_mutex); - lockdep_assert_held(&ma->card.snd_card->controls_rwsem); - - if (!ma->has_safety || !name || ma->card.snd_card->shutdown) - return; - /* - * Locked already, as after a sleep the daemon has not pinged since: - * the volumes are at the ceiling, and those from before it are kept. - */ - if (!ma->speaker_volume_unlocked) - return; - macaudio_pm_volumes_drop(ma); - - list_for_each_entry(kctl, &ma->card.snd_card->controls, list) - if (snd_soc_control_matches(kctl, name)) - nr++; - if (!nr) - return; - - vols = kzalloc_objs(*vols, nr); - uval = kzalloc_obj(*uval); - if (!vols || !uval) - goto out; - - list_for_each_entry(kctl, &ma->card.snd_card->controls, list) { - if (!snd_soc_control_matches(kctl, name)) - continue; - memset(uval, 0, sizeof(*uval)); - uval->id = kctl->id; - ret = kctl->get(kctl, uval); - if (ret < 0) { - dev_warn(ma->card.dev, "Failed to keep volume %s for the system sleep: %d\n", - kctl->id.name, ret); - goto out; - } - vols[i].kctl = kctl; - vols[i].value[0] = uval->value.integer.value[0]; - vols[i].value[1] = uval->value.integer.value[1]; - i++; - } - - ma->pm_volumes = vols; - ma->pm_nr_volumes = i; - ma->pm_uval = uval; - return; -out: - kfree(uval); - kfree(vols); -} - -/* - * At the unlock after a system sleep: the volumes the daemon had set. - * - * Context: volume_lock_mutex and the card's controls_rwsem held, so no volume - * write races these. - */ -static void macaudio_pm_volumes_restore(struct macaudio_snd_data *ma) -{ - struct snd_ctl_elem_value *uval = ma->pm_uval; - unsigned int i; - int ret; - - if (!ma->pm_volumes) - return; - - for (i = 0; i < ma->pm_nr_volumes; i++) { - struct snd_kcontrol *kctl = ma->pm_volumes[i].kctl; - - memset(uval, 0, sizeof(*uval)); - uval->id = kctl->id; - uval->value.integer.value[0] = ma->pm_volumes[i].value[0]; - uval->value.integer.value[1] = ma->pm_volumes[i].value[1]; - ret = kctl->put(kctl, uval); - if (ret < 0) - dev_err(ma->card.dev, "Failed to restore volume %s: %d\n", - kctl->id.name, ret); - else if (ret > 0) - snd_ctl_notify(ma->card.snd_card, SNDRV_CTL_EVENT_MASK_VALUE, - &kctl->id); - } - macaudio_pm_volumes_drop(ma); -} - -static void macaudio_vlimit_update(struct macaudio_snd_data *ma) -{ - int i; - bool unlock = true; - struct snd_kcontrol *kctl; - const char *reason; - - /* Do nothing if there is no safety configured */ - if (!ma->has_safety) - return; - - /* - * "Speaker Protection" owns the speakers: the lock is held at its - * ceiling until the interface switches back (macaudio_spk_leave_lease()). - */ - if (macaudio_spk_leased(ma)) - return; - - /* No protection daemon can hold these speakers: nothing to unlock */ - if (macaudio_spk_lease_only(ma->cfg)) - return; - - /* Check that someone is holding the main lock */ - if (!ma->speaker_lock_owner) { - reason = "Main control not locked"; - unlock = false; - } - - /* Check that the control has been pinged within the timeout */ - if (ma->speaker_lock_remain <= 0) { - reason = "Lock timeout"; - unlock = false; - } - - /* - * System sleep: userspace is frozen long before the card suspends (over - * half a second on J616s), and a playback stream left running then plays - * out its stale buffer and stops on an xrun into the still unmuted amps. - * Lock the volumes before the freeze instead of when the frozen daemon's - * lease runs out. - */ - if (ma->pm_quiesced) { - reason = "System suspend"; - unlock = false; - } - - /* Check that *every* limited control is locked by the same owner */ - list_for_each_entry(kctl, &ma->card.snd_card->controls, list) { - if(!snd_soc_control_matches(kctl, volume_control_names[ma->cfg->amp])) - continue; - - for (i = 0; i < kctl->count; i++) { - if (kctl->vd[i].owner != ma->speaker_lock_owner) { - reason = "Not all child controls locked by the same process"; - unlock = false; - } - } - } - - - if (unlock != ma->speaker_volume_unlocked) { - if (unlock) { - dev_info(ma->card.dev, "Speaker volumes unlocked\n"); - } else { - dev_info(ma->card.dev, "Speaker volumes locked: %s\n", reason); - /* The system-sleep lock is the kernel's own, not a lease the daemon lost */ - if (!ma->pm_quiesced) - ma->speaker_volume_was_locked = true; - } - - macaudio_vlimit_unlock(ma, unlock); - if (unlock) - macaudio_pm_volumes_restore(ma); - if (!unlock && ma->cfg->amp == AMP_MAX98360A) { - /* Samples already converted at the old gain must not play */ - atomic_inc(&ma->sw_gain_lock_epoch); - schedule_work(&ma->sw_gain_stop_work); - } - ma->speaker_volume_unlocked = unlock; - snd_ctl_notify(ma->card.snd_card, SNDRV_CTL_EVENT_MASK_VALUE, - &ma->speaker_lock_kctl->id); - } -} - -/* - * The volume lock's part of a system sleep (see macaudio_pm_notify()), after - * pm_quiesced has been set or cleared. - * - * Volume writes hold the controls for writing: none can land between - * keeping the volumes and clipping them. Control writes take - * volume_lock_mutex inside it as well. - */ -static void macaudio_vlimit_pm(struct macaudio_snd_data *ma, bool quiesce) -{ - down_read(&ma->card.snd_card->controls_rwsem); - mutex_lock(&ma->volume_lock_mutex); - /* - * The lease ends here, so after the resume the volumes stay locked until - * the daemon's next ping. Its clock (CLOCK_MONOTONIC) stops across s2idle: - * a lease left running would unlock them at resume and expire before the - * thawed daemon pings, which reports a lost lock and restarts it cold. - */ - if (quiesce) { - cancel_delayed_work(&ma->lock_timeout_work); - ma->speaker_lock_remain = 0; - macaudio_pm_volumes_save(ma); - } - macaudio_vlimit_update(ma); - mutex_unlock(&ma->volume_lock_mutex); - up_read(&ma->card.snd_card->controls_rwsem); -} - -static void macaudio_vlimit_enable_timeout(struct macaudio_snd_data *ma) -{ - mutex_lock(&ma->volume_lock_mutex); - - if (ma->speaker_lock_timeout_enabled) { - mutex_unlock(&ma->volume_lock_mutex); - return; - } - - if (ma->speaker_lock_remain > 0) { - ma->speaker_lock_timeout = ktime_add(ktime_get(), ma->speaker_lock_remain); - schedule_delayed_work(&ma->lock_timeout_work, usecs_to_jiffies(ktime_to_us(ma->speaker_lock_remain))); - dev_dbg(ma->card.dev, "Enabling volume limit timeout: %ld us left\n", - (long)ktime_to_us(ma->speaker_lock_remain)); - } - - macaudio_vlimit_update(ma); - - ma->speaker_lock_timeout_enabled = true; - mutex_unlock(&ma->volume_lock_mutex); -} - -static void macaudio_vlimit_disable_timeout(struct macaudio_snd_data *ma) -{ - ktime_t now; - - mutex_lock(&ma->volume_lock_mutex); - - if (!ma->speaker_lock_timeout_enabled) { - mutex_unlock(&ma->volume_lock_mutex); - return; - } - - now = ktime_get(); - - cancel_delayed_work(&ma->lock_timeout_work); - - if (ktime_after(now, ma->speaker_lock_timeout)) - ma->speaker_lock_remain = 0; - else if (ma->speaker_lock_remain > 0) - ma->speaker_lock_remain = ktime_sub(ma->speaker_lock_timeout, now); - - dev_dbg(ma->card.dev, "Disabling volume limit timeout: %ld us left\n", - (long)ktime_to_us(ma->speaker_lock_remain)); - - macaudio_vlimit_update(ma); - - ma->speaker_lock_timeout_enabled = false; - - mutex_unlock(&ma->volume_lock_mutex); -} - -static void macaudio_vlimit_timeout_work(struct work_struct *wrk) -{ - struct macaudio_snd_data *ma = container_of(to_delayed_work(wrk), - struct macaudio_snd_data, lock_timeout_work); - ktime_t now, left; - - mutex_lock(&ma->volume_lock_mutex); - - /* - * A ping or a disabled timeout can have cancelled this work after it - * had already started running; neither must expire the lease. - */ - if (!ma->speaker_lock_timeout_enabled || !ma->speaker_lock_owner || - ma->speaker_lock_remain <= 0) - goto out; - - /* A ping renewed the lease while this work was already running */ - now = ktime_get(); - if (ktime_before(now, ma->speaker_lock_timeout)) { - left = ktime_sub(ma->speaker_lock_timeout, now); - schedule_delayed_work(&ma->lock_timeout_work, - usecs_to_jiffies(ktime_to_us(left))); - goto out; - } - - ma->speaker_lock_remain = 0; - macaudio_vlimit_update(ma); - -out: - mutex_unlock(&ma->volume_lock_mutex); -} - -static void macaudio_vlimit_update_work(struct work_struct *wrk) -{ - struct macaudio_snd_data *ma = container_of(wrk, - struct macaudio_snd_data, lock_update_work); - - /* An unlock here sets the kept volumes again: no volume write may race it */ - down_read(&ma->card.snd_card->controls_rwsem); - if (ma->bes_active) - macaudio_vlimit_enable_timeout(ma); - else - macaudio_vlimit_disable_timeout(ma); - up_read(&ma->card.snd_card->controls_rwsem); -} - static bool macaudio_sw_gain_pcm(struct snd_pcm_substream *substream) { struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); @@ -1614,10 +1274,10 @@ static int macaudio_pm_notify(struct notifier_block *nb, unsigned long action, dev_warn(ma->card.dev, "speaker floor for system sleep waited %lld ms for the protection lock\n", waited); + /* Under the volume lock the speakers are at its ceiling already */ if (macaudio_spk_leased(ma)) macaudio_spk_floor(ma, "system sleep"); mutex_unlock(&ma->spk_mutex); - macaudio_vlimit_pm(ma, true); return NOTIFY_OK; case PM_POST_SUSPEND: case PM_POST_HIBERNATION: @@ -1627,7 +1287,6 @@ static int macaudio_pm_notify(struct notifier_block *nb, unsigned long action, if (ma->spk_owner) ma->spk_lapsed = true; mutex_unlock(&ma->spk_mutex); - macaudio_vlimit_pm(ma, false); return NOTIFY_OK; default: return NOTIFY_DONE; @@ -1637,20 +1296,19 @@ static int macaudio_pm_notify(struct notifier_block *nb, unsigned long action, /* * The "Speaker Protection Interface" switch. * - * At every card bind "Speaker Volume Unlock" is in charge, exactly as on a - * kernel without the lease. A CAP_SYS_ADMIN write of "Speaker Protection" - * hands the speakers to the lease, through the floor of both: the volume - * lock's ceiling first, then no speaker start, the running streams stopped, - * the lease's safe level, the amps' own gains fixed at their limit, and only - * then the ceiling lifted, with every volume still at the safe level. A - * write of "Speaker Volume Unlock", or SPEAKER_LEASE_UNOWNED_MS without an - * owner of "Speaker Protection", hands them back through the lease's floor: - * the volumes stay at the safe level under the lock's ceiling, and go up - * only once a daemon holds the lock and pings it, as after a cold bind. Its - * first ping after the switch fails with -ETIMEDOUT: the lock was lost. + * At every card bind "Speaker Volume Unlock" is in charge: the speaker + * volumes at its ceiling, the safe level, which nothing lifts. A + * CAP_SYS_ADMIN write of "Speaker Protection" hands the speakers to the + * lease, the only owner that can lift them: no speaker start, the running + * streams stopped, the lease's safe level, the amps' own gains fixed at + * their limit, and only then the ceiling lifted, with every volume still at + * the safe level. A write of "Speaker Volume Unlock", or + * SPEAKER_LEASE_UNOWNED_MS without an owner of "Speaker Protection", hands + * them back through the lease's floor: the volumes stay at the safe level + * under the lock's ceiling until the lease is the interface again. * - * While leased the lock's ping fails with -EBUSY, and the speaker volumes - * and amp gains are the driver's: userspace writes fail with -EPERM. + * While leased the speaker volumes and amp gains are the driver's: + * userspace writes fail with -EPERM. * * The switch is state for the boot, not a mixer setting: INACTIVE keeps * alsactl off it. Whatever makes it persist (the protection engine's @@ -1756,17 +1414,11 @@ static int macaudio_spk_enter_lease(struct macaudio_snd_data *ma) if (READ_ONCE(ma->spk_floor_failed) || READ_ONCE(ma->speaker_lock_failed)) return -EIO; - /* The volume lock's ceiling first: whatever a daemon had unlocked goes down */ + /* + * The volume lock's ceiling holds: it is never lifted outside the + * lease. No speaker start from here on: nothing is granted yet. + */ mutex_lock(&ma->volume_lock_mutex); - cancel_delayed_work(&ma->lock_timeout_work); - ma->speaker_lock_remain = 0; - macaudio_pm_volumes_drop(ma); - macaudio_vlimit_update(ma); - if (READ_ONCE(ma->speaker_lock_failed)) { - mutex_unlock(&ma->volume_lock_mutex); - return -EIO; - } - /* No speaker start from here on: nothing is granted yet */ WRITE_ONCE(ma->spk_iface, MACAUDIO_SPK_IFACE_LEASE); mutex_unlock(&ma->volume_lock_mutex); @@ -1827,16 +1479,9 @@ static void macaudio_spk_leave_lease(struct macaudio_snd_data *ma, const char *w } mutex_lock(&ma->volume_lock_mutex); - /* The lock's ceiling over the safe level; it lifts on a fresh ping only */ + /* The lock's ceiling over the safe level, for good: nothing lifts it */ if (!ma->spk_dead) macaudio_vlimit_unlock(ma, false); - ma->speaker_volume_unlocked = false; - cancel_delayed_work(&ma->lock_timeout_work); - ma->speaker_lock_remain = 0; - ma->speaker_lock_timeout = 0; - macaudio_pm_volumes_drop(ma); - /* A daemon still holding the lock learns that it lost it */ - ma->speaker_volume_was_locked = true; if (READ_ONCE(ma->spk_floor_failed) && !READ_ONCE(ma->speaker_lock_failed)) { WRITE_ONCE(ma->speaker_lock_failed, true); schedule_work(&ma->lock_fail_work); @@ -1847,7 +1492,6 @@ static void macaudio_spk_leave_lease(struct macaudio_snd_data *ma, const char *w schedule_work(&ma->sw_gain_stop_work); } WRITE_ONCE(ma->spk_iface, MACAUDIO_SPK_IFACE_UNLOCK); - macaudio_vlimit_update(ma); mutex_unlock(&ma->volume_lock_mutex); dev_info(ma->card.dev, "Speaker protection interface: Speaker Volume Unlock (%s)\n", why); @@ -2199,8 +1843,8 @@ static void macaudio_protect_sense(struct snd_soc_card *card) } /* - * Speaker volumes and amp gains: the volume lock's daemon sets them under - * "Speaker Volume Unlock"; while leased they are the driver's alone. + * Speaker volumes and amp gains: writable under "Speaker Volume Unlock", + * below its ceiling; while leased they are the driver's alone. */ static int macaudio_spk_owned_write_check(struct snd_kcontrol *kctl, struct snd_ctl_file *file) @@ -3188,20 +2832,14 @@ static int macaudio_be_trigger(struct snd_pcm_substream *substream, int cmd) case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: if (macaudio_spk_leased(ma) && !macaudio_spk_admitted(ma)) return macaudio_spk_refuse(ma); - ma->bes_active |= BIT(rtd->dai_link->id); - break; + return 0; case SNDRV_PCM_TRIGGER_SUSPEND: case SNDRV_PCM_TRIGGER_PAUSE_PUSH: case SNDRV_PCM_TRIGGER_STOP: - ma->bes_active &= ~BIT(rtd->dai_link->id); - break; + return 0; default: return -EINVAL; } - - /* The volume lock's lease runs only while the speakers play */ - schedule_work(&ma->lock_update_work); - return 0; } static const struct snd_soc_ops macaudio_fe_ops = { @@ -3604,15 +3242,6 @@ static int macaudio_card_remove(struct snd_soc_card *card) kthread_cancel_work_sync(&ma->spk_expire_work); kthread_cancel_work_sync(&ma->spk_stop_work); cancel_delayed_work_sync(&ma->spk_unowned_work); - /* - * No back-end can trigger any more, but a lock update it queued takes - * the card's controls_rwsem: finish it while the snd_card still exists. - */ - cancel_work_sync(&ma->lock_update_work); - cancel_delayed_work_sync(&ma->lock_timeout_work); - mutex_lock(&ma->volume_lock_mutex); - macaudio_pm_volumes_drop(ma); - mutex_unlock(&ma->volume_lock_mutex); return 0; } @@ -3862,6 +3491,19 @@ static int macaudio_slk_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_ return 0; } +/* + * The daemons that used to lift the speaker volumes from this control + * (speakersafetyd, speakerguardd 0.1) lock it and ping it: both are + * refused, so that such a daemon fails at once instead of believing it + * guards speakers it cannot reach. Only the "Speaker Protection" lease + * lifts them. + */ +static void macaudio_slk_refused(struct macaudio_snd_data *ma) +{ + dev_info_once(ma->card.dev, + "Speaker Volume Unlock refused: only the Speaker Protection lease lifts the speakers\n"); +} + static int macaudio_slk_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *uvalue) { struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); @@ -3869,10 +3511,9 @@ static int macaudio_slk_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_v long val = uvalue->value.integer.value[0]; /* - * Reads return the lock state (0 or 1), so a saved mixer state holds - * one of those. Writing it back changes nothing: only the lock owner's - * magic value renews the lease. Accept it as a no-op so that restoring - * a saved state before the daemon has taken the lock is not an error. + * Reads return 0, and a saved mixer state may hold 0 or 1. Writing it + * back changes nothing: accept it as a no-op so that restoring a saved + * state is not an error. */ if (val == 0 || val == 1) return 0; @@ -3880,105 +3521,23 @@ static int macaudio_slk_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_v if (val != SPEAKER_MAGIC_VALUE) return -EINVAL; - mutex_lock(&ma->volume_lock_mutex); - - if (!ma->speaker_lock_owner) { - mutex_unlock(&ma->volume_lock_mutex); - return -EPERM; - } - - /* "Speaker Protection" owns the speakers: no unlock to renew */ - if (macaudio_spk_leased(ma)) { - mutex_unlock(&ma->volume_lock_mutex); - return -EBUSY; - } - - /* Serves as a notification that the lock was lost at some point */ - if (ma->speaker_volume_was_locked) { - ma->speaker_volume_was_locked = false; - mutex_unlock(&ma->volume_lock_mutex); - return -ETIMEDOUT; - } - - /* Between suspend-prepare and the freeze: the lease starts again after the resume */ - if (ma->pm_quiesced) { - mutex_unlock(&ma->volume_lock_mutex); - return 0; - } - - cancel_delayed_work(&ma->lock_timeout_work); - - ma->speaker_lock_remain = ms_to_ktime(SPEAKER_LOCK_TIMEOUT); - ma->speaker_lock_timeout = ktime_add(ktime_get(), ma->speaker_lock_remain); - macaudio_vlimit_update(ma); - - if (ma->speaker_lock_timeout_enabled) { - dev_dbg(ma->card.dev, "Volume limit timeout ping: %ld us left\n", - (long)ktime_to_us(ma->speaker_lock_remain)); - schedule_delayed_work(&ma->lock_timeout_work, usecs_to_jiffies(ktime_to_us(ma->speaker_lock_remain))); - } - - mutex_unlock(&ma->volume_lock_mutex); - - return 0; + macaudio_slk_refused(ma); + return -EPERM; } +/* The ceiling is never lifted from here: the volumes read as locked */ static int macaudio_slk_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *uvalue) { - struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); - struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(card); - - uvalue->value.integer.value[0] = ma->speaker_volume_unlocked ? 1 : 0; - + uvalue->value.integer.value[0] = 0; return 0; } static int macaudio_slk_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_file *owner) { struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); - struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(card); - mutex_lock(&ma->volume_lock_mutex); - - /* - * A lease belongs to the owner that pinged for it. Whatever the - * previous owner had left must not unlock the volume for a new one - * before it has pinged itself. - */ - cancel_delayed_work(&ma->lock_timeout_work); - ma->speaker_lock_remain = 0; - ma->speaker_lock_timeout = 0; - /* A new owner sets the volumes itself */ - macaudio_pm_volumes_drop(ma); - - ma->speaker_lock_owner = owner; - macaudio_vlimit_update(ma); - - /* - * Reset the unintended lock flag when the control is first locked. - * At this point the state is locked and cannot be unlocked until - * userspace writes to this control, so this cannot spuriously become - * true again until that point. - */ - ma->speaker_volume_was_locked = false; - - mutex_unlock(&ma->volume_lock_mutex); - - return 0; -} - -static void macaudio_slk_unlock(struct snd_kcontrol *kcontrol) -{ - struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); - struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(card); - - mutex_lock(&ma->volume_lock_mutex); - cancel_delayed_work(&ma->lock_timeout_work); - ma->speaker_lock_owner = NULL; - ma->speaker_lock_remain = 0; - ma->speaker_lock_timeout = 0; - macaudio_vlimit_update(ma); - mutex_unlock(&ma->volume_lock_mutex); + macaudio_slk_refused(snd_soc_card_get_drvdata(card)); + return -EPERM; } /* The speaker controls go last: a card without speakers drops them. */ @@ -4003,7 +3562,7 @@ static const struct snd_kcontrol_new macaudio_controls[] = { .name = "Speaker Volume Unlock", .info = macaudio_slk_info, .put = macaudio_slk_put, .get = macaudio_slk_get, - .lock = macaudio_slk_lock, .unlock = macaudio_slk_unlock, + .lock = macaudio_slk_lock, }, { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, @@ -4071,10 +3630,10 @@ struct macaudio_platform_cfg macaudio_j313_cfg = { * launched on the rising SCLK edge: DSP_A with an inverted bit clock. The * speakers are one MAX98360A per channel on the "spkr" back-end, with no * sense lines and no amplifier controls; the volume is the CPU DAI's - * software gain, which the volume lock holds 20 dB down until a protection - * daemon takes it, and the lease 20 dB down below the full scale until its - * engine is granted. The "Speaker Sense" capture is the SpeakerTap, a - * digital tap of the words the amplifier receives; there is no V/I sense. + * software gain, which the volume lock holds 20 dB down, and the lease + * 20 dB down below the full scale until its engine is granted. The + * "Speaker Sense" capture is the SpeakerTap, a digital tap of the words the + * amplifier receives; there is no V/I sense. */ struct macaudio_platform_cfg macaudio_j700_cfg = { .enable_speakers = true, @@ -4150,9 +3709,8 @@ struct macaudio_platform_cfg macaudio_j775_cfg = { * woofer and one tweeter per side) on "spkr", whose voltage and current * sense return on the same stream. As on the other SN012776 laptops, the * speakers are behind the speaker protection interfaces from the bind: the - * volumes stay at the lock's ceiling until a protection daemon - * (speakersafetyd) holds them, or under "Speaker Protection" at the floor - * until an owner is granted. Each amp's level is its apple,speaker-config + * volumes stay at the lock's ceiling, or under "Speaker Protection" at the + * floor until an owner is granted. Each amp's level is its apple,speaker-config * from the boot loader. */ struct macaudio_platform_cfg macaudio_j813_cfg = { @@ -4238,8 +3796,8 @@ struct macaudio_platform_cfg macaudio_k114_cfg = { * two woofers and a tweeter per side, in ADT speaker-config order (left * woofer 1, right woofer 1, left tweeter, right tweeter, left woofer 2, * right woofer 2: left in even slots, right in odd), and twelve V/I sense - * words on the same stream. Locked to the safe level until speakersafetyd - * holds the volume, as on J813. Each amp's level is its apple,speaker-config + * words on the same stream. Locked to the safe level until a "Speaker + * Protection" owner is granted, as on J813. Each amp's level is its apple,speaker-config * from the boot loader (15 on all six in the J815 ADT). */ struct macaudio_platform_cfg macaudio_j815_cfg = { @@ -4340,8 +3898,8 @@ struct macaudio_platform_cfg macaudio_j614_cfg = { /* * J604 (T8132, MacBook Pro 14" M4): the J614s's six amps, two woofers and a * tweeter per side, on all six playback and twelve sense slots of the same - * frame. The speakers stay at their safe level until speakersafetyd takes - * them with the J604 profile. + * frame. The speakers stay at their safe level until a "Speaker Protection" + * owner is granted. */ struct macaudio_platform_cfg macaudio_j604_cfg = { .enable_speakers = true, @@ -4661,8 +4219,6 @@ static int macaudio_snd_platform_probe(struct platform_device *pdev) } } - INIT_WORK(&data->lock_update_work, macaudio_vlimit_update_work); - INIT_DELAYED_WORK(&data->lock_timeout_work, macaudio_vlimit_timeout_work); INIT_WORK(&data->sw_gain_stop_work, macaudio_sw_gain_stop_work); INIT_WORK(&data->lock_fail_work, macaudio_lock_fail_work); atomic_set(&data->sw_gain_lock_epoch, 0); @@ -4686,8 +4242,6 @@ static void macaudio_snd_platform_remove(struct platform_device *pdev) /* The card's removal stops the lease timer and its works; devm frees them */ snd_soc_unregister_card(&ma->card); - cancel_work_sync(&ma->lock_update_work); - cancel_delayed_work_sync(&ma->lock_timeout_work); cancel_work_sync(&ma->sw_gain_stop_work); cancel_work_sync(&ma->lock_fail_work); cancel_delayed_work_sync(&ma->spk_unowned_work); From ea865dc5e5ee463be091aae479d01038429b14d1 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Sat, 28 Nov 2026 15:57:26 +1100 Subject: [PATCH 2/2] ASoC: apple: macaudio: volume lock only after an explicit selection The previous commit refused "Speaker Volume Unlock" outright. That also takes away a user's deliberate choice of the older daemon (speakersafetyd), which the distributions keep as a one-command opt-out. Bring the volume lock back as it is on main and gate it instead. A daemon may take the volume lock only once a CAP_SYS_ADMIN process has written "Speaker Volume Unlock" to "Speaker Protection Interface" during this card bind, also where that is the interface already, as it is from every bind. Until then ELEM_LOCK on "Speaker Volume Unlock" and the SPEAKER_MAGIC_VALUE ping fail with -EPERM, with a one-time log line saying why, and macaudio_vlimit_update() never unlocks. The selection starts cleared at every bind, the kernel's own return to the volume lock (no owner of the lease for 30 s) does not set it, and a write of "Speaker Protection" clears it at once and relocks whatever a daemon had lifted, even where the lease itself is refused. While leased the ping still fails with -EBUSY. So a daemon started by a unit or preset alone gets -EPERM and fails loudly, and only an explicit root selection (the opt-out tool, before it starts the daemon) or the lease can lift the speakers. The lease path is unchanged. The volume lock's KUnit suites run selected as before, with new cases for the refusal without a selection, the owner that loses the selection, the kernel's revert that selects nothing, the unprivileged write that selects nothing, and the write of "Speaker Protection" that deselects. Signed-off-by: Ryan Murray --- sound/soc/apple/macaudio-iface-test.h | 119 +++- sound/soc/apple/macaudio-pm-volumes-test.h | 101 ++-- sound/soc/apple/macaudio-protection-test.h | 1 + sound/soc/apple/macaudio-vlimit-test.h | 258 +++++++- sound/soc/apple/macaudio.c | 669 ++++++++++++++++++--- 5 files changed, 981 insertions(+), 167 deletions(-) diff --git a/sound/soc/apple/macaudio-iface-test.h b/sound/soc/apple/macaudio-iface-test.h index 6632ad0126898..8ac7099ac5dcb 100644 --- a/sound/soc/apple/macaudio-iface-test.h +++ b/sound/soc/apple/macaudio-iface-test.h @@ -1,9 +1,8 @@ /* SPDX-License-Identifier: GPL-2.0-only */ /* * "Speaker Protection Interface": the volume lock in charge from the bind, - * at a ceiling that nothing lifts, the switch to the lease (the only owner - * that lifts the speakers) and back through the floor, and the way back - * when the lease has no owner. On the card of fake controls that + * the switch to the lease and back through the floor of both, and the way + * back when the lease has no owner. On the card of fake controls that * macaudio-protection-test.h builds, with the volume lock's controls added. */ #include @@ -12,7 +11,7 @@ struct macaudio_test_iface { struct macaudio_test_spk t; struct snd_kcontrol *slk; struct snd_kcontrol *iface; - /* A retired daemon's control file (speakersafetyd, speakerguardd 0.1) */ + /* The legacy daemon's control file */ struct snd_ctl_file *daemon; }; @@ -62,14 +61,21 @@ static int macaudio_test_iface_write(struct kunit *test, struct macaudio_test_if return macaudio_spk_iface_put(f->iface, uval); } -/* A retired daemon holds every speaker volume; the lock control is refused */ +/* The opt-out: an administrator selects the volume lock before its daemon starts */ +static void macaudio_test_select_legacy(struct kunit *test, struct macaudio_test_iface *f) +{ + KUNIT_EXPECT_GE(test, macaudio_test_iface_write(test, f, MACAUDIO_SPK_IFACE_UNLOCK), 0); + KUNIT_EXPECT_TRUE(test, f->t.ma->spk_legacy_selected); +} + +/* The legacy daemon takes the lock and every speaker volume */ static void macaudio_test_daemon_take(struct kunit *test, struct macaudio_test_iface *f) { unsigned int i; for (i = 0; i < MACAUDIO_TEST_NR_SPK; i++) f->t.vols[i]->vd[0].owner = f->daemon; - KUNIT_EXPECT_EQ(test, macaudio_slk_lock(f->slk, f->daemon), -EPERM); + KUNIT_ASSERT_EQ(test, macaudio_slk_lock(f->slk, f->daemon), 0); } static int macaudio_test_daemon_ping(struct kunit *test, struct macaudio_test_iface *f) @@ -112,32 +118,46 @@ static void macaudio_iface_volume_lock_from_binding(struct kunit *test) macaudio_test_expect_limit(test, &f, 1, 1); KUNIT_EXPECT_FALSE(test, delayed_work_pending(&f.t.ma->spk_unowned_work)); - /* Speaker volumes are writable, below the ceiling */ + /* Speaker volumes are the daemon's to write */ KUNIT_EXPECT_EQ(test, f.t.vols[0]->write_check(f.t.vols[0], NULL), 0); - /* A retired daemon's lock and ping are refused and lift nothing */ - macaudio_test_daemon_take(test, &f); + /* A daemon started on its own is refused */ + KUNIT_EXPECT_FALSE(test, f.t.ma->spk_legacy_selected); + KUNIT_EXPECT_EQ(test, macaudio_slk_lock(f.slk, f.daemon), -EPERM); KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); macaudio_test_expect_limit(test, &f, 1, 1); + + /* Once selected, the daemon unlocks them as on a lock-only kernel */ + macaudio_test_select_legacy(test, &f); + macaudio_test_daemon_take(test, &f); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); + KUNIT_EXPECT_TRUE(test, f.t.ma->speaker_volume_unlocked); + macaudio_test_expect_limit(test, &f, 1, SN012776_0DB); } -/* Into the lease: the lease's floor, then the lift */ +/* Into the lease: the lock's ceiling, then the lease's floor, then the lift */ static void macaudio_iface_switch_goes_through_the_floor(struct kunit *test) { struct macaudio_test_iface f; + unsigned int i; macaudio_test_iface_card(test, &f); + macaudio_test_select_legacy(test, &f); macaudio_test_daemon_take(test, &f); - KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); + for (i = 0; i < MACAUDIO_TEST_NR_SPK; i++) + macaudio_test_vol(f.t.vols[i])->value = 180; KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), 1); KUNIT_EXPECT_TRUE(test, macaudio_spk_leased(f.t.ma)); + KUNIT_EXPECT_FALSE(test, f.t.ma->spk_legacy_selected); + KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); /* At the lease's floor, below a ceiling that no longer applies */ macaudio_test_expect_limit(test, &f, 0, SN012776_0DB); KUNIT_EXPECT_FALSE(test, macaudio_spk_admitted(f.t.ma)); /* The volume lock is refused, and so are the daemon's volume writes */ - KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EBUSY); KUNIT_EXPECT_EQ(test, f.t.vols[0]->write_check(f.t.vols[0], NULL), -EPERM); KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), 0); @@ -152,40 +172,41 @@ static void macaudio_iface_switch_goes_through_the_floor(struct kunit *test) /* * Back to the volume lock: the speakers come back at the floor under the - * lock's ceiling, and stay there: no daemon lifts them. + * lock's ceiling, and go up only once the daemon pings, as after a cold + * bind; its first ping learns that it lost the lock. */ static void macaudio_iface_revert_starts_at_the_floor(struct kunit *test) { struct macaudio_test_iface f; macaudio_test_iface_card(test, &f); + macaudio_test_select_legacy(test, &f); macaudio_test_daemon_take(test, &f); KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), 1); macaudio_test_take(test, &f.t); KUNIT_EXPECT_EQ(test, macaudio_test_write(test, &f.t, 50), 0); macaudio_test_expect_limit(test, &f, SN012776_0DB, SN012776_0DB); + /* The administrator's revert selects the volume lock again */ KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_UNLOCK), 1); macaudio_test_settle(f.t.ma); + KUNIT_EXPECT_TRUE(test, f.t.ma->spk_legacy_selected); KUNIT_EXPECT_FALSE(test, macaudio_spk_leased(f.t.ma)); KUNIT_EXPECT_FALSE(test, macaudio_spk_admitted(f.t.ma)); + KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); macaudio_test_expect_limit(test, &f, 0, 1); /* The lease grants nothing any more */ KUNIT_EXPECT_EQ(test, macaudio_test_write(test, &f.t, 50), -EBUSY); macaudio_test_expect_limit(test, &f, 0, 1); - /* Nor does the retired daemon, however often it pings */ - KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); - KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); + /* The daemon's lock was lost: told once, then it unlocks */ + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -ETIMEDOUT); + KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); macaudio_test_expect_limit(test, &f, 0, 1); - - /* The lease takes them again and grants as before */ - KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), 1); - KUNIT_EXPECT_EQ(test, macaudio_test_write(test, &f.t, 50), 0); - KUNIT_EXPECT_TRUE(test, macaudio_spk_admitted(f.t.ma)); - macaudio_test_expect_limit(test, &f, SN012776_0DB, SN012776_0DB); - KUNIT_EXPECT_EQ(test, macaudio_test_write(test, &f.t, 0), 0); - macaudio_test_settle(f.t.ma); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); + KUNIT_EXPECT_TRUE(test, f.t.ma->speaker_volume_unlocked); + /* Unlocking lifts the ceiling only: the daemon sets the volumes itself */ + macaudio_test_expect_limit(test, &f, 0, SN012776_0DB); } /* The engine dies: the lease's floor at once, the volume lock after the grace */ @@ -218,9 +239,42 @@ static void macaudio_iface_unowned_lease_reverts(struct kunit *test) flush_delayed_work(&f.t.ma->spk_unowned_work); macaudio_test_settle(f.t.ma); KUNIT_EXPECT_FALSE(test, macaudio_spk_leased(f.t.ma)); + KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); + macaudio_test_expect_limit(test, &f, 0, 1); + /* The kernel's own revert selects nothing: a daemon is still refused */ + KUNIT_EXPECT_FALSE(test, f.t.ma->spk_legacy_selected); + KUNIT_EXPECT_EQ(test, macaudio_slk_lock(f.slk, f.daemon), -EPERM); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); macaudio_test_expect_limit(test, &f, 0, 1); } +/* + * A write of "Speaker Protection" deselects the volume lock at once, even + * where it cannot enter the lease, and relocks what the daemon had lifted. + */ +static void macaudio_iface_protection_write_deselects(struct kunit *test) +{ + struct macaudio_test_iface f; + + macaudio_test_iface_card(test, &f); + macaudio_test_select_legacy(test, &f); + macaudio_test_daemon_take(test, &f); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); + KUNIT_EXPECT_TRUE(test, f.t.ma->speaker_volume_unlocked); + + /* The lease refused during a system sleep: still deselected */ + WRITE_ONCE(f.t.ma->pm_quiesced, true); + KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), + -EBUSY); + WRITE_ONCE(f.t.ma->pm_quiesced, false); + KUNIT_EXPECT_FALSE(test, macaudio_spk_leased(f.t.ma)); + KUNIT_EXPECT_FALSE(test, f.t.ma->spk_legacy_selected); + KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); + macaudio_test_expect_limit(test, &f, 1, 1); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); + macaudio_test_expect_limit(test, &f, 1, 1); +} + /* Only a CAP_SYS_ADMIN process hands the speakers over */ static void macaudio_iface_needs_cap_sys_admin(struct kunit *test) { @@ -235,6 +289,10 @@ static void macaudio_iface_needs_cap_sys_admin(struct kunit *test) old = override_creds(cred); KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), -EPERM); + /* Nor does such a process select the volume lock */ + KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_UNLOCK), + -EPERM); + KUNIT_EXPECT_FALSE(test, f.t.ma->spk_legacy_selected); revert_creds(old); put_cred(cred); KUNIT_EXPECT_FALSE(test, macaudio_spk_leased(f.t.ma)); @@ -246,7 +304,7 @@ static void macaudio_iface_needs_cap_sys_admin(struct kunit *test) * J813: its four SN012776 (the fake card's Left/Right Woofer/Tweeter * volumes, the J813 amps' prefixes) are behind both interfaces from the * bind, as on the MCA machines. Under the volume lock they stay at its - * ceiling, whatever a daemon does; under the lease they are + * ceiling until a daemon holds the lock and pings; under the lease they are * at the floor and no speaker start is admitted until an owner is granted. */ static void macaudio_iface_j813_speakers_protected(struct kunit *test) @@ -256,16 +314,22 @@ static void macaudio_iface_j813_speakers_protected(struct kunit *test) KUNIT_ASSERT_TRUE(test, macaudio_speakers_allowed(&macaudio_j813_cfg)); macaudio_test_iface_card_cfg(test, &f, &macaudio_j813_cfg); KUNIT_EXPECT_FALSE(test, macaudio_spk_leased(f.t.ma)); + KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); macaudio_test_expect_limit(test, &f, 1, 1); - /* A daemon that holds the volumes and pings lifts nothing */ + /* A daemon that holds the lock but has not pinged lifts nothing */ + macaudio_test_select_legacy(test, &f); macaudio_test_daemon_take(test, &f); - KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -EPERM); + KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); macaudio_test_expect_limit(test, &f, 1, 1); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); + KUNIT_EXPECT_TRUE(test, f.t.ma->speaker_volume_unlocked); + macaudio_test_expect_limit(test, &f, 1, SN012776_0DB); /* Into the lease: the floor, and nothing admitted without an owner */ KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), 1); KUNIT_EXPECT_TRUE(test, macaudio_spk_leased(f.t.ma)); + KUNIT_EXPECT_FALSE(test, f.t.ma->speaker_volume_unlocked); KUNIT_EXPECT_FALSE(test, macaudio_spk_admitted(f.t.ma)); macaudio_test_expect_limit(test, &f, 0, SN012776_0DB); KUNIT_EXPECT_EQ(test, macaudio_test_write(test, &f.t, 50), -EPERM); @@ -296,6 +360,7 @@ static struct kunit_case macaudio_iface_cases[] = { KUNIT_CASE(macaudio_iface_switch_goes_through_the_floor), KUNIT_CASE(macaudio_iface_revert_starts_at_the_floor), KUNIT_CASE(macaudio_iface_unowned_lease_reverts), + KUNIT_CASE(macaudio_iface_protection_write_deselects), KUNIT_CASE(macaudio_iface_needs_cap_sys_admin), KUNIT_CASE(macaudio_iface_control_is_inactive), KUNIT_CASE(macaudio_iface_j813_speakers_protected), diff --git a/sound/soc/apple/macaudio-pm-volumes-test.h b/sound/soc/apple/macaudio-pm-volumes-test.h index 3b58f7faf9282..b01ac3fc3594c 100644 --- a/sound/soc/apple/macaudio-pm-volumes-test.h +++ b/sound/soc/apple/macaudio-pm-volumes-test.h @@ -1,8 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-only */ /* - * Under "Speaker Volume Unlock" the speaker volumes sit at its ceiling from - * the bind, and a system sleep, a retired daemon's lock and ping, or both, - * never lift it. The card here is shared with macaudio-vlimit-test.h. + * The speaker volumes the system-sleep lock clips are set again at the + * daemon's first ping after it, through the lock control and the PM notifier. */ #include @@ -50,6 +49,8 @@ static void macaudio_pmt_pm_cleanup(void *data) { struct macaudio_snd_data *ma = data; + cancel_delayed_work_sync(&ma->lock_timeout_work); + macaudio_pm_volumes_drop(ma); macaudio_spk_fini(ma); } @@ -58,11 +59,11 @@ static const char *const macaudio_pmt_vol_names[] = { "Right Woofer Speaker Volume", "Left Tweeter Speaker Volume", "Right Tweeter Speaker Volume", - /* Not a speaker volume: never clipped */ + /* Not a speaker volume: never locked, kept or set */ "Headphone Playback Volume", }; -/* What the amps' volumes held before the bind */ +/* As the daemon set them before the sleep */ static const long macaudio_pmt_vols[] = { 201, 190, 201, 170, 120 }; #define MACAUDIO_PMT_NR_VOLS ARRAY_SIZE(macaudio_pmt_vol_names) @@ -87,7 +88,7 @@ static struct snd_kcontrol *macaudio_pmt_add_kctl(struct kunit *test, struct snd return kctl; } -/* A bound card with speaker safety, its lock control and the volumes at its ceiling */ +/* A card with speaker safety, its lock control and the volumes, none owned yet */ static void macaudio_pmt_pm_card(struct kunit *test, struct macaudio_pmt_pm *t) { struct snd_kcontrol_new slk = { @@ -96,7 +97,6 @@ static void macaudio_pmt_pm_card(struct kunit *test, struct macaudio_pmt_pm *t) .info = snd_ctl_boolean_mono_info, .get = macaudio_slk_get, .put = macaudio_slk_put, - .lock = macaudio_slk_lock, }; struct macaudio_snd_data *ma; struct snd_card *card; @@ -119,8 +119,11 @@ static void macaudio_pmt_pm_card(struct kunit *test, struct macaudio_pmt_pm *t) ma->has_safety = true; INIT_LIST_HEAD(&ma->card.rtd_list); mutex_init(&ma->volume_lock_mutex); + INIT_DELAYED_WORK(&ma->lock_timeout_work, macaudio_vlimit_timeout_work); /* The volume lock is the interface, as at every bind */ mutex_init(&ma->spk_mutex); + /* As after an administrator's write of "Speaker Volume Unlock" */ + ma->spk_legacy_selected = true; KUNIT_ASSERT_EQ(test, macaudio_spk_init(ma), 0); t->ma = ma; @@ -146,17 +149,9 @@ static void macaudio_pmt_pm_card(struct kunit *test, struct macaudio_pmt_pm *t) ret = kunit_add_action_or_reset(test, macaudio_pmt_pm_cleanup, ma); KUNIT_ASSERT_EQ(test, ret, 0); - - /* As macaudio_late_probe() does at every bind */ - mutex_lock(&ma->volume_lock_mutex); - macaudio_vlimit_unlock(ma, false); - mutex_unlock(&ma->volume_lock_mutex); } -/* - * A retired daemon (speakersafetyd, speakerguardd 0.1) holds every speaker - * volume and asks for the lock control: refused. - */ +/* A daemon takes the lock and every speaker volume, as speakersafetyd does */ static void macaudio_pmt_take(struct kunit *test, struct macaudio_pmt_pm *t) { struct snd_ctl_file *owner = kunit_kzalloc(test, sizeof(*owner), GFP_KERNEL); @@ -165,7 +160,7 @@ static void macaudio_pmt_take(struct kunit *test, struct macaudio_pmt_pm *t) KUNIT_ASSERT_NOT_NULL(test, owner); for (i = 0; i < MACAUDIO_PMT_NR_VOLS - 1; i++) t->vols[i]->vd[0].owner = owner; - KUNIT_EXPECT_EQ(test, macaudio_slk_lock(t->slk, owner), -EPERM); + KUNIT_ASSERT_EQ(test, macaudio_slk_lock(t->slk, owner), 0); } static int macaudio_pmt_ping(struct kunit *test, struct macaudio_pmt_pm *t) @@ -182,8 +177,9 @@ static void macaudio_pmt_notify(struct macaudio_pmt_pm *t, unsigned long action) macaudio_pm_notify(&t->ma->pm_nb, action, NULL); } -/* Every speaker volume clipped to the lock's ceiling, and limited there */ -static void macaudio_pmt_expect_vols(struct kunit *test, struct macaudio_pmt_pm *t) +/* The speaker volumes as the daemon set them, or clipped to the lock's ceiling */ +static void macaudio_pmt_expect_vols(struct kunit *test, struct macaudio_pmt_pm *t, + bool clipped) { unsigned int i; @@ -191,48 +187,79 @@ static void macaudio_pmt_expect_vols(struct kunit *test, struct macaudio_pmt_pm bool speaker = i < MACAUDIO_PMT_NR_VOLS - 1; KUNIT_EXPECT_EQ(test, macaudio_pmt_vol(t->vols[i])->value, - speaker ? 1L : macaudio_pmt_vols[i]); - if (speaker) - KUNIT_EXPECT_EQ(test, macaudio_pmt_vol(t->vols[i])->mc.platform_max, 1); + speaker && clipped ? 1L : macaudio_pmt_vols[i]); } } -/* A sleep under the volume lock leaves the ceiling where it was */ -static void macaudio_pm_volumes_sleep_keeps_the_ceiling(struct kunit *test) +static void macaudio_pm_volumes_restored_after_sleep(struct kunit *test) { struct macaudio_pmt_pm t; macaudio_pmt_pm_card(test, &t); - macaudio_pmt_expect_vols(test, &t); + macaudio_pmt_take(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + macaudio_pmt_expect_vols(test, &t, false); macaudio_pmt_notify(&t, PM_SUSPEND_PREPARE); - macaudio_pmt_expect_vols(test, &t); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + macaudio_pmt_expect_vols(test, &t, true); + /* A ping before the freeze leaves them locked */ + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + + /* Locked after the resume until the daemon pings */ macaudio_pmt_notify(&t, PM_POST_SUSPEND); - macaudio_pmt_expect_vols(test, &t); - KUNIT_EXPECT_FALSE(test, macaudio_spk_leased(t.ma)); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + macaudio_pmt_expect_vols(test, &t, true); + + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + macaudio_pmt_expect_vols(test, &t, false); + KUNIT_EXPECT_NULL(test, t.ma->pm_volumes); } -/* A retired daemon's lock and pings around a sleep lift nothing */ -static void macaudio_pm_volumes_daemon_across_sleep(struct kunit *test) +/* The daemon pings only once a stream runs: a second sleep can come first. */ +static void macaudio_pm_volumes_second_sleep_before_ping(struct kunit *test) { struct macaudio_pmt_pm t; macaudio_pmt_pm_card(test, &t); macaudio_pmt_take(test, &t); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); - macaudio_pmt_expect_vols(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); macaudio_pmt_notify(&t, PM_SUSPEND_PREPARE); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); macaudio_pmt_notify(&t, PM_POST_SUSPEND); + macaudio_pmt_notify(&t, PM_SUSPEND_PREPARE); + macaudio_pmt_notify(&t, PM_POST_SUSPEND); + macaudio_pmt_expect_vols(test, &t, true); + + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + macaudio_pmt_expect_vols(test, &t, false); +} + +/* A new owner, such as a restarted daemon, sets the volumes itself. */ +static void macaudio_pm_volumes_new_owner(struct kunit *test) +{ + struct macaudio_pmt_pm t; + + macaudio_pmt_pm_card(test, &t); + macaudio_pmt_take(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + macaudio_pmt_notify(&t, PM_SUSPEND_PREPARE); + macaudio_pmt_notify(&t, PM_POST_SUSPEND); + macaudio_pmt_take(test, &t); - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); - macaudio_pmt_expect_vols(test, &t); + KUNIT_EXPECT_NULL(test, t.ma->pm_volumes); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + macaudio_pmt_expect_vols(test, &t, true); } static struct kunit_case macaudio_pm_volumes_cases[] = { - KUNIT_CASE(macaudio_pm_volumes_sleep_keeps_the_ceiling), - KUNIT_CASE(macaudio_pm_volumes_daemon_across_sleep), + KUNIT_CASE(macaudio_pm_volumes_restored_after_sleep), + KUNIT_CASE(macaudio_pm_volumes_second_sleep_before_ping), + KUNIT_CASE(macaudio_pm_volumes_new_owner), {} }; diff --git a/sound/soc/apple/macaudio-protection-test.h b/sound/soc/apple/macaudio-protection-test.h index 3cc43fe67fd82..1f6cd5ddb987d 100644 --- a/sound/soc/apple/macaudio-protection-test.h +++ b/sound/soc/apple/macaudio-protection-test.h @@ -164,6 +164,7 @@ static void macaudio_test_spk_card(struct kunit *test, struct macaudio_test_spk ma->has_speakers = true; mutex_init(&ma->spk_mutex); mutex_init(&ma->volume_lock_mutex); + INIT_DELAYED_WORK(&ma->lock_timeout_work, macaudio_vlimit_timeout_work); INIT_WORK(&ma->lock_fail_work, macaudio_lock_fail_work); INIT_WORK(&ma->sw_gain_stop_work, macaudio_sw_gain_stop_work); /* The lease is the interface, as after "Speaker Protection Interface" */ diff --git a/sound/soc/apple/macaudio-vlimit-test.h b/sound/soc/apple/macaudio-vlimit-test.h index f2341ebcae95d..c23b53a28cba4 100644 --- a/sound/soc/apple/macaudio-vlimit-test.h +++ b/sound/soc/apple/macaudio-vlimit-test.h @@ -1,11 +1,13 @@ /* SPDX-License-Identifier: GPL-2.0-only */ /* - * "Speaker Volume Unlock" lifts nothing: the lock and ping of the daemons - * that used to lift the speaker volumes from it (speakersafetyd, - * speakerguardd 0.1) are refused, and the volumes stay at the ceiling. - * Built on the card of macaudio-pm-volumes-test.h. + * The "Speaker Volume Unlock" lease: the speaker volumes leave the safe + * ceiling only while one process holds the lock control and every speaker + * volume, and pings within SPEAKER_LOCK_TIMEOUT ms while speaker playback + * runs, and only once an administrator has selected the volume lock. Built + * on the card of macaudio-pm-volumes-test.h, selected unless a case says. */ #include +#include static int macaudio_vlt_put(struct kunit *test, struct macaudio_pmt_pm *t, long val) { @@ -25,6 +27,15 @@ static long macaudio_vlt_get(struct kunit *test, struct macaudio_pmt_pm *t) return uval->value.integer.value[0]; } +/* Speaker playback runs (or stops): the ping deadline is armed (or held). */ +static void macaudio_vlt_playing(struct macaudio_pmt_pm *t, bool playing) +{ + if (playing) + macaudio_vlimit_enable_timeout(t->ma); + else + macaudio_vlimit_disable_timeout(t->ma); +} + /* Saved mixer states hold 0 or 1: restoring one is a no-op, nothing else is. */ static void macaudio_vlimit_restore_values_are_noops(struct kunit *test) { @@ -35,66 +46,249 @@ static void macaudio_vlimit_restore_values_are_noops(struct kunit *test) KUNIT_EXPECT_EQ(test, macaudio_vlt_put(test, &t, 1), 0); KUNIT_EXPECT_EQ(test, macaudio_vlt_put(test, &t, 2), -EINVAL); KUNIT_EXPECT_EQ(test, macaudio_vlt_put(test, &t, SPEAKER_MAGIC_VALUE + 1), -EINVAL); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); KUNIT_EXPECT_EQ(test, macaudio_vlt_get(test, &t), 0L); - macaudio_pmt_expect_vols(test, &t); + + /* Even for the owner */ + macaudio_pmt_take(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_vlt_put(test, &t, 1), 0); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); } -/* No file may take the lock control, whoever it is */ -static void macaudio_vlimit_lock_refused(struct kunit *test) +/* A ping without the lock is refused and unlocks nothing. */ +static void macaudio_vlimit_ping_needs_the_lock(struct kunit *test) { - struct snd_ctl_file *file = kunit_kzalloc(test, sizeof(*file), GFP_KERNEL); struct macaudio_pmt_pm t; - KUNIT_ASSERT_NOT_NULL(test, file); macaudio_pmt_pm_card(test, &t); - /* KUnit runs with CAP_SYS_ADMIN: root is refused too */ - KUNIT_EXPECT_TRUE(test, capable(CAP_SYS_ADMIN)); - KUNIT_EXPECT_EQ(test, macaudio_slk_lock(t.slk, file), -EPERM); - KUNIT_EXPECT_EQ(test, macaudio_slk_lock(t.slk, file), -EPERM); - macaudio_pmt_expect_vols(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + KUNIT_EXPECT_EQ(test, macaudio_vlt_get(test, &t), 0L); +} + +/* The lock alone is not enough: every speaker volume must be the owner's. */ +static void macaudio_vlimit_needs_every_volume(struct kunit *test) +{ + struct snd_ctl_file *other = kunit_kzalloc(test, sizeof(*other), GFP_KERNEL); + struct macaudio_pmt_pm t; + unsigned int i; + + KUNIT_ASSERT_NOT_NULL(test, other); + for (i = 0; i < MACAUDIO_PMT_NR_VOLS - 1; i++) { + macaudio_pmt_pm_card(test, &t); + macaudio_pmt_take(test, &t); + t.vols[i]->vd[0].owner = other; + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_FALSE_MSG(test, t.ma->speaker_volume_unlocked, + "%s held by another file", macaudio_pmt_vol_names[i]); + + macaudio_pmt_pm_card(test, &t); + macaudio_pmt_take(test, &t); + t.vols[i]->vd[0].owner = NULL; + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_FALSE_MSG(test, t.ma->speaker_volume_unlocked, + "%s not locked", macaudio_pmt_vol_names[i]); + } + + /* A volume that is not a speaker volume does not count */ + macaudio_pmt_pm_card(test, &t); + macaudio_pmt_take(test, &t); + t.vols[MACAUDIO_PMT_NR_VOLS - 1]->vd[0].owner = other; + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); +} + +/* Closing the lock relocks at once and clips the speaker volumes only. */ +static void macaudio_vlimit_unlock_clips(struct kunit *test) +{ + struct macaudio_pmt_pm t; + + macaudio_pmt_pm_card(test, &t); + macaudio_pmt_take(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + KUNIT_EXPECT_EQ(test, macaudio_vlt_get(test, &t), 1L); + macaudio_pmt_expect_vols(test, &t, false); + + macaudio_slk_unlock(t.slk); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + KUNIT_EXPECT_EQ(test, macaudio_vlt_get(test, &t), 0L); + macaudio_pmt_expect_vols(test, &t, true); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); +} + +/* + * While playback runs the lease ends SPEAKER_LOCK_TIMEOUT ms after the last + * ping. The owner's next ping reports the loss with -ETIMEDOUT and unlocks + * nothing; only the ping after it starts a new lease. + */ +static void macaudio_vlimit_lease_expires_while_playing(struct kunit *test) +{ + struct macaudio_pmt_pm t; + + macaudio_pmt_pm_card(test, &t); + macaudio_pmt_take(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + macaudio_vlt_playing(&t, true); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + + msleep(SPEAKER_LOCK_TIMEOUT * 2); + flush_delayed_work(&t.ma->lock_timeout_work); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + macaudio_pmt_expect_vols(test, &t, true); + + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -ETIMEDOUT); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + macaudio_vlt_playing(&t, false); } /* - * A daemon that holds every speaker volume and pings, as speakersafetyd - * does, is refused every time and lifts nothing. + * Each ping moves the deadline to SPEAKER_LOCK_TIMEOUT ms after itself and + * re-arms the expiry. Checked on the deadline, not by racing the clock. */ -static void macaudio_vlimit_daemon_never_unlocks(struct kunit *test) +static void macaudio_vlimit_pings_keep_the_lease(struct kunit *test) { struct macaudio_pmt_pm t; + ktime_t before, deadline; unsigned int i; macaudio_pmt_pm_card(test, &t); macaudio_pmt_take(test, &t); - for (i = 0; i < 8; i++) - KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); - KUNIT_EXPECT_EQ(test, macaudio_vlt_get(test, &t), 0L); - macaudio_pmt_expect_vols(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + macaudio_vlt_playing(&t, true); + deadline = t.ma->speaker_lock_timeout; + for (i = 0; i < 3; i++) { + usleep_range(1000, 2000); + before = ktime_get(); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + KUNIT_EXPECT_GT(test, t.ma->speaker_lock_timeout, deadline); + KUNIT_EXPECT_GE(test, t.ma->speaker_lock_timeout, + ktime_add_ms(before, SPEAKER_LOCK_TIMEOUT)); + KUNIT_EXPECT_TRUE(test, delayed_work_pending(&t.ma->lock_timeout_work)); + deadline = t.ma->speaker_lock_timeout; + } + macaudio_vlt_playing(&t, false); +} + +/* + * Without playback the deadline is held: an idle owner keeps its lease, and + * the time it had left runs on when playback starts again. + */ +static void macaudio_vlimit_idle_holds_the_lease(struct kunit *test) +{ + struct macaudio_pmt_pm t; + + macaudio_pmt_pm_card(test, &t); + macaudio_pmt_take(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + msleep(SPEAKER_LOCK_TIMEOUT * 2); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + KUNIT_EXPECT_EQ(test, t.ma->speaker_lock_remain, ms_to_ktime(SPEAKER_LOCK_TIMEOUT)); + KUNIT_EXPECT_FALSE(test, delayed_work_pending(&t.ma->lock_timeout_work)); + + /* The time left runs from the start of playback and then ends */ + macaudio_vlt_playing(&t, true); + KUNIT_EXPECT_TRUE(test, delayed_work_pending(&t.ma->lock_timeout_work)); + msleep(SPEAKER_LOCK_TIMEOUT * 2); + flush_delayed_work(&t.ma->lock_timeout_work); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + macaudio_vlt_playing(&t, false); +} + +/* A new owner never inherits the previous owner's lease. */ +static void macaudio_vlimit_new_owner_pings_first(struct kunit *test) +{ + struct macaudio_pmt_pm t; + + macaudio_pmt_pm_card(test, &t); + macaudio_pmt_take(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + + macaudio_pmt_take(test, &t); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + KUNIT_EXPECT_LE(test, t.ma->speaker_lock_remain, 0); + /* The relock was the switch of owners, not a lost lease */ + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); +} - /* A ping without even holding the volumes fares no better */ +/* A card without speaker safety never touches its volumes. */ +static void macaudio_vlimit_without_safety(struct kunit *test) +{ + struct macaudio_pmt_pm t; + + macaudio_pmt_pm_card(test, &t); + t.ma->has_safety = false; + macaudio_pmt_take(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + macaudio_slk_unlock(t.slk); + macaudio_pmt_expect_vols(test, &t, false); +} + +/* + * Without an administrator's selection the lock and the ping are refused, + * for root too, and nothing is unlocked. + */ +static void macaudio_vlimit_needs_the_selection(struct kunit *test) +{ + struct snd_ctl_file *owner = kunit_kzalloc(test, sizeof(*owner), GFP_KERNEL); + struct macaudio_pmt_pm t; + unsigned int i; + + KUNIT_ASSERT_NOT_NULL(test, owner); macaudio_pmt_pm_card(test, &t); + t.ma->spk_legacy_selected = false; + KUNIT_EXPECT_TRUE(test, capable(CAP_SYS_ADMIN)); + for (i = 0; i < MACAUDIO_PMT_NR_VOLS - 1; i++) + t.vols[i]->vd[0].owner = owner; + KUNIT_EXPECT_EQ(test, macaudio_slk_lock(t.slk, owner), -EPERM); + KUNIT_EXPECT_NULL(test, t.ma->speaker_lock_owner); KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); - macaudio_pmt_expect_vols(test, &t); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + /* Saved mixer states still restore */ + KUNIT_EXPECT_EQ(test, macaudio_vlt_put(test, &t, 0), 0); + KUNIT_EXPECT_EQ(test, macaudio_vlt_put(test, &t, 1), 0); } -/* Outside the lease the driver itself cannot lift the ceiling either */ -static void macaudio_vlimit_lift_needs_the_lease(struct kunit *test) +/* A daemon that took the lock while selected loses it with the selection */ +static void macaudio_vlimit_deselected_owner_is_refused(struct kunit *test) { struct macaudio_pmt_pm t; macaudio_pmt_pm_card(test, &t); - KUNIT_ASSERT_FALSE(test, macaudio_spk_leased(t.ma)); - KUNIT_ASSERT_FALSE(test, macaudio_spk_lease_only(t.ma->cfg)); + macaudio_pmt_take(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + mutex_lock(&t.ma->volume_lock_mutex); - macaudio_vlimit_unlock(t.ma, true); + t.ma->spk_legacy_selected = false; + macaudio_vlimit_update(t.ma); mutex_unlock(&t.ma->volume_lock_mutex); - macaudio_pmt_expect_vols(test, &t); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + macaudio_pmt_expect_vols(test, &t, true); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -EPERM); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); } static struct kunit_case macaudio_vlimit_cases[] = { + KUNIT_CASE(macaudio_vlimit_needs_the_selection), + KUNIT_CASE(macaudio_vlimit_deselected_owner_is_refused), KUNIT_CASE(macaudio_vlimit_restore_values_are_noops), - KUNIT_CASE(macaudio_vlimit_lock_refused), - KUNIT_CASE(macaudio_vlimit_daemon_never_unlocks), - KUNIT_CASE(macaudio_vlimit_lift_needs_the_lease), + KUNIT_CASE(macaudio_vlimit_ping_needs_the_lock), + KUNIT_CASE(macaudio_vlimit_needs_every_volume), + KUNIT_CASE(macaudio_vlimit_unlock_clips), + KUNIT_CASE_SLOW(macaudio_vlimit_lease_expires_while_playing), + KUNIT_CASE(macaudio_vlimit_pings_keep_the_lease), + KUNIT_CASE_SLOW(macaudio_vlimit_idle_holds_the_lease), + KUNIT_CASE(macaudio_vlimit_new_owner_pings_first), + KUNIT_CASE(macaudio_vlimit_without_safety), {} }; diff --git a/sound/soc/apple/macaudio.c b/sound/soc/apple/macaudio.c index 818fda16d4257..1ab09b7bebdd9 100644 --- a/sound/soc/apple/macaudio.c +++ b/sound/soc/apple/macaudio.c @@ -66,10 +66,11 @@ */ #define MACAUDIO_MAX_BCLK_FREQ 24576000 -/* The ping of the retired "Speaker Volume Unlock" daemons, refused */ #define SPEAKER_MAGIC_VALUE (s32)0xdec1be15 /* Primary, Secondary and Speaker Sense */ #define MACAUDIO_NUM_FE_LINKS 3 +/* The "Speaker Volume Unlock" lease, in milliseconds */ +#define SPEAKER_LOCK_TIMEOUT 250 /* * While "Speaker Protection" is the interface, this long without an owner of * it (the engine died, or never came) puts "Speaker Volume Unlock" back in @@ -227,11 +228,16 @@ struct ma_codec_idle { u32 idle_mask; }; +/* A speaker volume control and its value, as the system-sleep lock found it */ +struct macaudio_pm_volume { + struct snd_kcontrol *kctl; + long value[2]; +}; + /* * The interface that owns the speakers (see "Speaker Protection Interface"): - * the volume lock, which holds the speaker volumes at its ceiling and lifts - * them for no one, or the lease of a protection engine in the signal path, - * the only owner that can lift them. + * the volume lock of speakersafetyd and speakerguardd 0.1, or the lease of a + * protection engine in the signal path. */ enum macaudio_spk_iface { MACAUDIO_SPK_IFACE_UNLOCK, @@ -352,15 +358,40 @@ struct macaudio_snd_data { struct snd_soc_component *sw_gain_component; /* - * "Speaker Volume Unlock": the speaker volumes' ceiling outside the - * lease. Nothing lifts it: the retired daemons' lock and ping on the - * control are refused (macaudio_slk_lock(), macaudio_slk_put()). + * "Speaker Volume Unlock": the speakersafetyd / speakerguardd 0.1 + * interface (see macaudio_vlimit_update()). */ int speaker_sample_rate; struct snd_kcontrol *speaker_sample_rate_kctl; struct mutex volume_lock_mutex; + /* + * A CAP_SYS_ADMIN process wrote "Speaker Volume Unlock" to "Speaker + * Protection Interface" since the card bound, and no write of "Speaker + * Protection" came after it: only then may a daemon take the volume + * lock (see macaudio_slk_selected()). Never set by the kernel itself. + * Written under spk_mutex and volume_lock_mutex. + */ + bool spk_legacy_selected; + bool speaker_volume_unlocked; + bool speaker_volume_was_locked; struct snd_kcontrol *speaker_lock_kctl; + struct snd_ctl_file *speaker_lock_owner; + u64 bes_active; + bool speaker_lock_timeout_enabled; + ktime_t speaker_lock_timeout; + ktime_t speaker_lock_remain; + struct delayed_work lock_timeout_work; + struct work_struct lock_update_work; + /* + * The speaker volumes as the system-sleep lock found them. Locking clips + * them to its ceiling and unlocking only lifts the ceiling; the daemon + * keeps its lease across the sleep and does not set them again. + */ + struct macaudio_pm_volume *pm_volumes; + unsigned int pm_nr_volumes; + /* Allocated with them, so that setting them again cannot fail */ + struct snd_ctl_elem_value *pm_uval; /* * Software-gain amplifier: a lower gain cannot change samples that @@ -505,12 +536,20 @@ static struct macaudio_link_props macaudio_fe_link_props[] = { }; /* - * "Speaker Volume Unlock": the default interface, from every bind. The - * speaker volumes are held at its ceiling, the safe level, and nothing - * lifts it: only the "Speaker Protection" lease can (see - * macaudio_spk_enter_lease()). The lock and ping of the daemons that used - * to lift it (SNDRV_CTL_IOCTL_ELEM_LOCK on the control and every speaker - * volume, then SPEAKER_MAGIC_VALUE written to it) are refused. + * "Speaker Volume Unlock": speaker protection by a userspace daemon + * (speakersafetyd, speakerguardd 0.1), the default interface. + * + * The daemon holds SNDRV_CTL_IOCTL_ELEM_LOCK on "Speaker Volume Unlock" and + * on every speaker volume control, and pings the unlock control with + * SPEAKER_MAGIC_VALUE at least every SPEAKER_LOCK_TIMEOUT ms while the + * speakers play. While it does, the volumes may go up to the amps' 0 dB; + * otherwise they are held at the lock's ceiling. The daemon governs them + * from its thermal model. + * + * Only once an administrator has selected it, though: a CAP_SYS_ADMIN write + * of "Speaker Volume Unlock" to "Speaker Protection Interface" since the + * card bound. Until then, and after a write of "Speaker Protection", the + * lock and the ping fail with -EPERM (see macaudio_slk_refused()). */ static void macaudio_vlimit_unlock(struct macaudio_snd_data *ma, bool unlock) @@ -525,9 +564,6 @@ static void macaudio_vlimit_unlock(struct macaudio_snd_data *ma, bool unlock) return; } - /* Only the lease lifts the ceiling (macaudio_spk_enter_lease()) */ - unlock = unlock && macaudio_spk_leased(ma); - switch (ma->cfg->amp) { case AMP_NONE: WARN_ON_ONCE(1); @@ -643,6 +679,329 @@ static void macaudio_lock_fail_work(struct work_struct *work) macaudio_stop_playback(ma, false); } +static void macaudio_pm_volumes_drop(struct macaudio_snd_data *ma) +{ + kfree(ma->pm_volumes); + kfree(ma->pm_uval); + ma->pm_volumes = NULL; + ma->pm_nr_volumes = 0; + ma->pm_uval = NULL; +} + +/* + * Keep the speaker volumes before the system-sleep lock clips them. + * + * Context: volume_lock_mutex and the card's controls_rwsem held, so the daemon + * cannot set a volume between this and the clip. + */ +static void macaudio_pm_volumes_save(struct macaudio_snd_data *ma) +{ + const char *name = volume_control_names[ma->cfg->amp]; + struct snd_ctl_elem_value *uval = NULL; + struct macaudio_pm_volume *vols = NULL; + struct snd_kcontrol *kctl; + unsigned int nr = 0, i = 0; + int ret; + + lockdep_assert_held(&ma->volume_lock_mutex); + lockdep_assert_held(&ma->card.snd_card->controls_rwsem); + + if (!ma->has_safety || !name || ma->card.snd_card->shutdown) + return; + /* + * Locked already, as after a sleep the daemon has not pinged since: + * the volumes are at the ceiling, and those from before it are kept. + */ + if (!ma->speaker_volume_unlocked) + return; + macaudio_pm_volumes_drop(ma); + + list_for_each_entry(kctl, &ma->card.snd_card->controls, list) + if (snd_soc_control_matches(kctl, name)) + nr++; + if (!nr) + return; + + vols = kzalloc_objs(*vols, nr); + uval = kzalloc_obj(*uval); + if (!vols || !uval) + goto out; + + list_for_each_entry(kctl, &ma->card.snd_card->controls, list) { + if (!snd_soc_control_matches(kctl, name)) + continue; + memset(uval, 0, sizeof(*uval)); + uval->id = kctl->id; + ret = kctl->get(kctl, uval); + if (ret < 0) { + dev_warn(ma->card.dev, "Failed to keep volume %s for the system sleep: %d\n", + kctl->id.name, ret); + goto out; + } + vols[i].kctl = kctl; + vols[i].value[0] = uval->value.integer.value[0]; + vols[i].value[1] = uval->value.integer.value[1]; + i++; + } + + ma->pm_volumes = vols; + ma->pm_nr_volumes = i; + ma->pm_uval = uval; + return; +out: + kfree(uval); + kfree(vols); +} + +/* + * At the unlock after a system sleep: the volumes the daemon had set. + * + * Context: volume_lock_mutex and the card's controls_rwsem held, so no volume + * write races these. + */ +static void macaudio_pm_volumes_restore(struct macaudio_snd_data *ma) +{ + struct snd_ctl_elem_value *uval = ma->pm_uval; + unsigned int i; + int ret; + + if (!ma->pm_volumes) + return; + + for (i = 0; i < ma->pm_nr_volumes; i++) { + struct snd_kcontrol *kctl = ma->pm_volumes[i].kctl; + + memset(uval, 0, sizeof(*uval)); + uval->id = kctl->id; + uval->value.integer.value[0] = ma->pm_volumes[i].value[0]; + uval->value.integer.value[1] = ma->pm_volumes[i].value[1]; + ret = kctl->put(kctl, uval); + if (ret < 0) + dev_err(ma->card.dev, "Failed to restore volume %s: %d\n", + kctl->id.name, ret); + else if (ret > 0) + snd_ctl_notify(ma->card.snd_card, SNDRV_CTL_EVENT_MASK_VALUE, + &kctl->id); + } + macaudio_pm_volumes_drop(ma); +} + +static void macaudio_vlimit_update(struct macaudio_snd_data *ma) +{ + int i; + bool unlock = true; + struct snd_kcontrol *kctl; + const char *reason; + + /* Do nothing if there is no safety configured */ + if (!ma->has_safety) + return; + + /* + * "Speaker Protection" owns the speakers: the lock is held at its + * ceiling until the interface switches back (macaudio_spk_leave_lease()). + */ + if (macaudio_spk_leased(ma)) + return; + + /* No protection daemon can hold these speakers: nothing to unlock */ + if (macaudio_spk_lease_only(ma->cfg)) + return; + + /* Only after an explicit selection of the volume lock */ + if (!ma->spk_legacy_selected) { + reason = "Speaker Volume Unlock not selected"; + unlock = false; + } + + /* Check that someone is holding the main lock */ + if (!ma->speaker_lock_owner) { + reason = "Main control not locked"; + unlock = false; + } + + /* Check that the control has been pinged within the timeout */ + if (ma->speaker_lock_remain <= 0) { + reason = "Lock timeout"; + unlock = false; + } + + /* + * System sleep: userspace is frozen long before the card suspends (over + * half a second on J616s), and a playback stream left running then plays + * out its stale buffer and stops on an xrun into the still unmuted amps. + * Lock the volumes before the freeze instead of when the frozen daemon's + * lease runs out. + */ + if (ma->pm_quiesced) { + reason = "System suspend"; + unlock = false; + } + + /* Check that *every* limited control is locked by the same owner */ + list_for_each_entry(kctl, &ma->card.snd_card->controls, list) { + if(!snd_soc_control_matches(kctl, volume_control_names[ma->cfg->amp])) + continue; + + for (i = 0; i < kctl->count; i++) { + if (kctl->vd[i].owner != ma->speaker_lock_owner) { + reason = "Not all child controls locked by the same process"; + unlock = false; + } + } + } + + + if (unlock != ma->speaker_volume_unlocked) { + if (unlock) { + dev_info(ma->card.dev, "Speaker volumes unlocked\n"); + } else { + dev_info(ma->card.dev, "Speaker volumes locked: %s\n", reason); + /* The system-sleep lock is the kernel's own, not a lease the daemon lost */ + if (!ma->pm_quiesced) + ma->speaker_volume_was_locked = true; + } + + macaudio_vlimit_unlock(ma, unlock); + if (unlock) + macaudio_pm_volumes_restore(ma); + if (!unlock && ma->cfg->amp == AMP_MAX98360A) { + /* Samples already converted at the old gain must not play */ + atomic_inc(&ma->sw_gain_lock_epoch); + schedule_work(&ma->sw_gain_stop_work); + } + ma->speaker_volume_unlocked = unlock; + snd_ctl_notify(ma->card.snd_card, SNDRV_CTL_EVENT_MASK_VALUE, + &ma->speaker_lock_kctl->id); + } +} + +/* + * The volume lock's part of a system sleep (see macaudio_pm_notify()), after + * pm_quiesced has been set or cleared. + * + * Volume writes hold the controls for writing: none can land between + * keeping the volumes and clipping them. Control writes take + * volume_lock_mutex inside it as well. + */ +static void macaudio_vlimit_pm(struct macaudio_snd_data *ma, bool quiesce) +{ + down_read(&ma->card.snd_card->controls_rwsem); + mutex_lock(&ma->volume_lock_mutex); + /* + * The lease ends here, so after the resume the volumes stay locked until + * the daemon's next ping. Its clock (CLOCK_MONOTONIC) stops across s2idle: + * a lease left running would unlock them at resume and expire before the + * thawed daemon pings, which reports a lost lock and restarts it cold. + */ + if (quiesce) { + cancel_delayed_work(&ma->lock_timeout_work); + ma->speaker_lock_remain = 0; + macaudio_pm_volumes_save(ma); + } + macaudio_vlimit_update(ma); + mutex_unlock(&ma->volume_lock_mutex); + up_read(&ma->card.snd_card->controls_rwsem); +} + +static void macaudio_vlimit_enable_timeout(struct macaudio_snd_data *ma) +{ + mutex_lock(&ma->volume_lock_mutex); + + if (ma->speaker_lock_timeout_enabled) { + mutex_unlock(&ma->volume_lock_mutex); + return; + } + + if (ma->speaker_lock_remain > 0) { + ma->speaker_lock_timeout = ktime_add(ktime_get(), ma->speaker_lock_remain); + schedule_delayed_work(&ma->lock_timeout_work, usecs_to_jiffies(ktime_to_us(ma->speaker_lock_remain))); + dev_dbg(ma->card.dev, "Enabling volume limit timeout: %ld us left\n", + (long)ktime_to_us(ma->speaker_lock_remain)); + } + + macaudio_vlimit_update(ma); + + ma->speaker_lock_timeout_enabled = true; + mutex_unlock(&ma->volume_lock_mutex); +} + +static void macaudio_vlimit_disable_timeout(struct macaudio_snd_data *ma) +{ + ktime_t now; + + mutex_lock(&ma->volume_lock_mutex); + + if (!ma->speaker_lock_timeout_enabled) { + mutex_unlock(&ma->volume_lock_mutex); + return; + } + + now = ktime_get(); + + cancel_delayed_work(&ma->lock_timeout_work); + + if (ktime_after(now, ma->speaker_lock_timeout)) + ma->speaker_lock_remain = 0; + else if (ma->speaker_lock_remain > 0) + ma->speaker_lock_remain = ktime_sub(ma->speaker_lock_timeout, now); + + dev_dbg(ma->card.dev, "Disabling volume limit timeout: %ld us left\n", + (long)ktime_to_us(ma->speaker_lock_remain)); + + macaudio_vlimit_update(ma); + + ma->speaker_lock_timeout_enabled = false; + + mutex_unlock(&ma->volume_lock_mutex); +} + +static void macaudio_vlimit_timeout_work(struct work_struct *wrk) +{ + struct macaudio_snd_data *ma = container_of(to_delayed_work(wrk), + struct macaudio_snd_data, lock_timeout_work); + ktime_t now, left; + + mutex_lock(&ma->volume_lock_mutex); + + /* + * A ping or a disabled timeout can have cancelled this work after it + * had already started running; neither must expire the lease. + */ + if (!ma->speaker_lock_timeout_enabled || !ma->speaker_lock_owner || + ma->speaker_lock_remain <= 0) + goto out; + + /* A ping renewed the lease while this work was already running */ + now = ktime_get(); + if (ktime_before(now, ma->speaker_lock_timeout)) { + left = ktime_sub(ma->speaker_lock_timeout, now); + schedule_delayed_work(&ma->lock_timeout_work, + usecs_to_jiffies(ktime_to_us(left))); + goto out; + } + + ma->speaker_lock_remain = 0; + macaudio_vlimit_update(ma); + +out: + mutex_unlock(&ma->volume_lock_mutex); +} + +static void macaudio_vlimit_update_work(struct work_struct *wrk) +{ + struct macaudio_snd_data *ma = container_of(wrk, + struct macaudio_snd_data, lock_update_work); + + /* An unlock here sets the kept volumes again: no volume write may race it */ + down_read(&ma->card.snd_card->controls_rwsem); + if (ma->bes_active) + macaudio_vlimit_enable_timeout(ma); + else + macaudio_vlimit_disable_timeout(ma); + up_read(&ma->card.snd_card->controls_rwsem); +} + static bool macaudio_sw_gain_pcm(struct snd_pcm_substream *substream) { struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); @@ -1274,10 +1633,10 @@ static int macaudio_pm_notify(struct notifier_block *nb, unsigned long action, dev_warn(ma->card.dev, "speaker floor for system sleep waited %lld ms for the protection lock\n", waited); - /* Under the volume lock the speakers are at its ceiling already */ if (macaudio_spk_leased(ma)) macaudio_spk_floor(ma, "system sleep"); mutex_unlock(&ma->spk_mutex); + macaudio_vlimit_pm(ma, true); return NOTIFY_OK; case PM_POST_SUSPEND: case PM_POST_HIBERNATION: @@ -1287,6 +1646,7 @@ static int macaudio_pm_notify(struct notifier_block *nb, unsigned long action, if (ma->spk_owner) ma->spk_lapsed = true; mutex_unlock(&ma->spk_mutex); + macaudio_vlimit_pm(ma, false); return NOTIFY_OK; default: return NOTIFY_DONE; @@ -1296,19 +1656,25 @@ static int macaudio_pm_notify(struct notifier_block *nb, unsigned long action, /* * The "Speaker Protection Interface" switch. * - * At every card bind "Speaker Volume Unlock" is in charge: the speaker - * volumes at its ceiling, the safe level, which nothing lifts. A - * CAP_SYS_ADMIN write of "Speaker Protection" hands the speakers to the - * lease, the only owner that can lift them: no speaker start, the running - * streams stopped, the lease's safe level, the amps' own gains fixed at - * their limit, and only then the ceiling lifted, with every volume still at - * the safe level. A write of "Speaker Volume Unlock", or - * SPEAKER_LEASE_UNOWNED_MS without an owner of "Speaker Protection", hands - * them back through the lease's floor: the volumes stay at the safe level - * under the lock's ceiling until the lease is the interface again. + * At every card bind "Speaker Volume Unlock" is in charge, exactly as on a + * kernel without the lease. A CAP_SYS_ADMIN write of "Speaker Protection" + * hands the speakers to the lease, through the floor of both: the volume + * lock's ceiling first, then no speaker start, the running streams stopped, + * the lease's safe level, the amps' own gains fixed at their limit, and only + * then the ceiling lifted, with every volume still at the safe level. A + * write of "Speaker Volume Unlock", or SPEAKER_LEASE_UNOWNED_MS without an + * owner of "Speaker Protection", hands them back through the lease's floor: + * the volumes stay at the safe level under the lock's ceiling, and go up + * only once a daemon holds the lock and pings it, as after a cold bind. Its + * first ping after the switch fails with -ETIMEDOUT: the lock was lost. * - * While leased the speaker volumes and amp gains are the driver's: - * userspace writes fail with -EPERM. + * A daemon may hold the lock at all only after an explicit CAP_SYS_ADMIN + * write of "Speaker Volume Unlock" during this bind, also where it is the + * interface already. The kernel's own returns to it (no owner of the lease) + * select nothing, and a write of "Speaker Protection" deselects it. + * + * While leased the lock's ping fails with -EBUSY, and the speaker volumes + * and amp gains are the driver's: userspace writes fail with -EPERM. * * The switch is state for the boot, not a mixer setting: INACTIVE keeps * alsactl off it. Whatever makes it persist (the protection engine's @@ -1414,11 +1780,17 @@ static int macaudio_spk_enter_lease(struct macaudio_snd_data *ma) if (READ_ONCE(ma->spk_floor_failed) || READ_ONCE(ma->speaker_lock_failed)) return -EIO; - /* - * The volume lock's ceiling holds: it is never lifted outside the - * lease. No speaker start from here on: nothing is granted yet. - */ + /* The volume lock's ceiling first: whatever a daemon had unlocked goes down */ mutex_lock(&ma->volume_lock_mutex); + cancel_delayed_work(&ma->lock_timeout_work); + ma->speaker_lock_remain = 0; + macaudio_pm_volumes_drop(ma); + macaudio_vlimit_update(ma); + if (READ_ONCE(ma->speaker_lock_failed)) { + mutex_unlock(&ma->volume_lock_mutex); + return -EIO; + } + /* No speaker start from here on: nothing is granted yet */ WRITE_ONCE(ma->spk_iface, MACAUDIO_SPK_IFACE_LEASE); mutex_unlock(&ma->volume_lock_mutex); @@ -1479,9 +1851,16 @@ static void macaudio_spk_leave_lease(struct macaudio_snd_data *ma, const char *w } mutex_lock(&ma->volume_lock_mutex); - /* The lock's ceiling over the safe level, for good: nothing lifts it */ + /* The lock's ceiling over the safe level; it lifts on a fresh ping only */ if (!ma->spk_dead) macaudio_vlimit_unlock(ma, false); + ma->speaker_volume_unlocked = false; + cancel_delayed_work(&ma->lock_timeout_work); + ma->speaker_lock_remain = 0; + ma->speaker_lock_timeout = 0; + macaudio_pm_volumes_drop(ma); + /* A daemon still holding the lock learns that it lost it */ + ma->speaker_volume_was_locked = true; if (READ_ONCE(ma->spk_floor_failed) && !READ_ONCE(ma->speaker_lock_failed)) { WRITE_ONCE(ma->speaker_lock_failed, true); schedule_work(&ma->lock_fail_work); @@ -1492,6 +1871,7 @@ static void macaudio_spk_leave_lease(struct macaudio_snd_data *ma, const char *w schedule_work(&ma->sw_gain_stop_work); } WRITE_ONCE(ma->spk_iface, MACAUDIO_SPK_IFACE_UNLOCK); + macaudio_vlimit_update(ma); mutex_unlock(&ma->volume_lock_mutex); dev_info(ma->card.dev, "Speaker protection interface: Speaker Volume Unlock (%s)\n", why); @@ -1513,6 +1893,26 @@ static void macaudio_spk_unowned_work(struct work_struct *work) up_write(&ma->card.snd_card->controls_rwsem); } +/* + * Select or deselect the volume lock for daemons (spk_legacy_selected). A + * deselection relocks whatever a daemon had unlocked. + * + * Context: the card's controls_rwsem and spk_mutex held. + */ +static void macaudio_slk_select(struct macaudio_snd_data *ma, bool selected) +{ + lockdep_assert_held(&ma->spk_mutex); + + mutex_lock(&ma->volume_lock_mutex); + if (ma->spk_legacy_selected != selected) { + ma->spk_legacy_selected = selected; + dev_info(ma->card.dev, "Speaker Volume Unlock %s by the administrator\n", + selected ? "selected" : "deselected"); + macaudio_vlimit_update(ma); + } + mutex_unlock(&ma->volume_lock_mutex); +} + static int macaudio_spk_iface_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { @@ -1545,6 +1945,9 @@ static int macaudio_spk_iface_put(struct snd_kcontrol *kcontrol, return -EPERM; mutex_lock(&ma->spk_mutex); + /* A write of "Speaker Protection" deselects the volume lock, at once */ + if (item == MACAUDIO_SPK_IFACE_LEASE) + macaudio_slk_select(ma, false); if (item == READ_ONCE(ma->spk_iface)) { ret = 0; } else if (item == MACAUDIO_SPK_IFACE_LEASE) { @@ -1555,6 +1958,12 @@ static int macaudio_spk_iface_put(struct snd_kcontrol *kcontrol, macaudio_spk_leave_lease(ma, "switched by the administrator"); ret = 1; } + /* + * An explicit write of "Speaker Volume Unlock", even where it is the + * interface already (as from the bind), selects the volume lock. + */ + if (item == MACAUDIO_SPK_IFACE_UNLOCK) + macaudio_slk_select(ma, true); mutex_unlock(&ma->spk_mutex); return ret; } @@ -1843,8 +2252,8 @@ static void macaudio_protect_sense(struct snd_soc_card *card) } /* - * Speaker volumes and amp gains: writable under "Speaker Volume Unlock", - * below its ceiling; while leased they are the driver's alone. + * Speaker volumes and amp gains: the volume lock's daemon sets them under + * "Speaker Volume Unlock"; while leased they are the driver's alone. */ static int macaudio_spk_owned_write_check(struct snd_kcontrol *kctl, struct snd_ctl_file *file) @@ -2832,14 +3241,20 @@ static int macaudio_be_trigger(struct snd_pcm_substream *substream, int cmd) case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: if (macaudio_spk_leased(ma) && !macaudio_spk_admitted(ma)) return macaudio_spk_refuse(ma); - return 0; + ma->bes_active |= BIT(rtd->dai_link->id); + break; case SNDRV_PCM_TRIGGER_SUSPEND: case SNDRV_PCM_TRIGGER_PAUSE_PUSH: case SNDRV_PCM_TRIGGER_STOP: - return 0; + ma->bes_active &= ~BIT(rtd->dai_link->id); + break; default: return -EINVAL; } + + /* The volume lock's lease runs only while the speakers play */ + schedule_work(&ma->lock_update_work); + return 0; } static const struct snd_soc_ops macaudio_fe_ops = { @@ -3188,6 +3603,8 @@ static int macaudio_late_probe(struct snd_soc_card *card) return dev_err_probe(card->dev, ret, "software gain ceiling not set\n"); mutex_lock(&ma->volume_lock_mutex); + /* Every bind starts unselected: only an administrator selects it */ + ma->spk_legacy_selected = false; macaudio_vlimit_unlock(ma, false); ret = READ_ONCE(ma->speaker_lock_failed) ? -EIO : 0; mutex_unlock(&ma->volume_lock_mutex); @@ -3242,6 +3659,15 @@ static int macaudio_card_remove(struct snd_soc_card *card) kthread_cancel_work_sync(&ma->spk_expire_work); kthread_cancel_work_sync(&ma->spk_stop_work); cancel_delayed_work_sync(&ma->spk_unowned_work); + /* + * No back-end can trigger any more, but a lock update it queued takes + * the card's controls_rwsem: finish it while the snd_card still exists. + */ + cancel_work_sync(&ma->lock_update_work); + cancel_delayed_work_sync(&ma->lock_timeout_work); + mutex_lock(&ma->volume_lock_mutex); + macaudio_pm_volumes_drop(ma); + mutex_unlock(&ma->volume_lock_mutex); return 0; } @@ -3481,6 +3907,19 @@ static int macaudio_sss_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_v return 0; } +/* + * The volume lock lifts the speakers only for a daemon an administrator + * chose: a CAP_SYS_ADMIN write of "Speaker Volume Unlock" to "Speaker + * Protection Interface" since the card bound (spk_legacy_selected). Without + * it the lock and the ping are refused, so a daemon started on its own fails + * at once instead of believing it guards the speakers. + */ +static void macaudio_slk_refused(struct macaudio_snd_data *ma) +{ + dev_info_once(ma->card.dev, + "Speaker Volume Unlock refused: not selected on Speaker Protection Interface\n"); +} + static int macaudio_slk_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; @@ -3491,19 +3930,6 @@ static int macaudio_slk_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_ return 0; } -/* - * The daemons that used to lift the speaker volumes from this control - * (speakersafetyd, speakerguardd 0.1) lock it and ping it: both are - * refused, so that such a daemon fails at once instead of believing it - * guards speakers it cannot reach. Only the "Speaker Protection" lease - * lifts them. - */ -static void macaudio_slk_refused(struct macaudio_snd_data *ma) -{ - dev_info_once(ma->card.dev, - "Speaker Volume Unlock refused: only the Speaker Protection lease lifts the speakers\n"); -} - static int macaudio_slk_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *uvalue) { struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); @@ -3511,9 +3937,10 @@ static int macaudio_slk_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_v long val = uvalue->value.integer.value[0]; /* - * Reads return 0, and a saved mixer state may hold 0 or 1. Writing it - * back changes nothing: accept it as a no-op so that restoring a saved - * state is not an error. + * Reads return the lock state (0 or 1), so a saved mixer state holds + * one of those. Writing it back changes nothing: only the lock owner's + * magic value renews the lease. Accept it as a no-op so that restoring + * a saved state before the daemon has taken the lock is not an error. */ if (val == 0 || val == 1) return 0; @@ -3521,23 +3948,118 @@ static int macaudio_slk_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_v if (val != SPEAKER_MAGIC_VALUE) return -EINVAL; - macaudio_slk_refused(ma); - return -EPERM; + mutex_lock(&ma->volume_lock_mutex); + + if (!ma->speaker_lock_owner) { + mutex_unlock(&ma->volume_lock_mutex); + return -EPERM; + } + + /* "Speaker Protection" owns the speakers: no unlock to renew */ + if (macaudio_spk_leased(ma)) { + mutex_unlock(&ma->volume_lock_mutex); + return -EBUSY; + } + + /* Deselected since the daemon took the lock */ + if (!ma->spk_legacy_selected) { + mutex_unlock(&ma->volume_lock_mutex); + macaudio_slk_refused(ma); + return -EPERM; + } + + /* Serves as a notification that the lock was lost at some point */ + if (ma->speaker_volume_was_locked) { + ma->speaker_volume_was_locked = false; + mutex_unlock(&ma->volume_lock_mutex); + return -ETIMEDOUT; + } + + /* Between suspend-prepare and the freeze: the lease starts again after the resume */ + if (ma->pm_quiesced) { + mutex_unlock(&ma->volume_lock_mutex); + return 0; + } + + cancel_delayed_work(&ma->lock_timeout_work); + + ma->speaker_lock_remain = ms_to_ktime(SPEAKER_LOCK_TIMEOUT); + ma->speaker_lock_timeout = ktime_add(ktime_get(), ma->speaker_lock_remain); + macaudio_vlimit_update(ma); + + if (ma->speaker_lock_timeout_enabled) { + dev_dbg(ma->card.dev, "Volume limit timeout ping: %ld us left\n", + (long)ktime_to_us(ma->speaker_lock_remain)); + schedule_delayed_work(&ma->lock_timeout_work, usecs_to_jiffies(ktime_to_us(ma->speaker_lock_remain))); + } + + mutex_unlock(&ma->volume_lock_mutex); + + return 0; } -/* The ceiling is never lifted from here: the volumes read as locked */ static int macaudio_slk_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *uvalue) { - uvalue->value.integer.value[0] = 0; + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); + struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(card); + + uvalue->value.integer.value[0] = ma->speaker_volume_unlocked ? 1 : 0; + return 0; } static int macaudio_slk_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_file *owner) { struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); + struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(card); - macaudio_slk_refused(snd_soc_card_get_drvdata(card)); - return -EPERM; + mutex_lock(&ma->volume_lock_mutex); + + if (!ma->spk_legacy_selected) { + mutex_unlock(&ma->volume_lock_mutex); + macaudio_slk_refused(ma); + return -EPERM; + } + + /* + * A lease belongs to the owner that pinged for it. Whatever the + * previous owner had left must not unlock the volume for a new one + * before it has pinged itself. + */ + cancel_delayed_work(&ma->lock_timeout_work); + ma->speaker_lock_remain = 0; + ma->speaker_lock_timeout = 0; + /* A new owner sets the volumes itself */ + macaudio_pm_volumes_drop(ma); + + ma->speaker_lock_owner = owner; + macaudio_vlimit_update(ma); + + /* + * Reset the unintended lock flag when the control is first locked. + * At this point the state is locked and cannot be unlocked until + * userspace writes to this control, so this cannot spuriously become + * true again until that point. + */ + ma->speaker_volume_was_locked = false; + + mutex_unlock(&ma->volume_lock_mutex); + + return 0; +} + +static void macaudio_slk_unlock(struct snd_kcontrol *kcontrol) +{ + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); + struct macaudio_snd_data *ma = snd_soc_card_get_drvdata(card); + + mutex_lock(&ma->volume_lock_mutex); + cancel_delayed_work(&ma->lock_timeout_work); + ma->speaker_lock_owner = NULL; + ma->speaker_lock_remain = 0; + ma->speaker_lock_timeout = 0; + macaudio_vlimit_update(ma); + mutex_unlock(&ma->volume_lock_mutex); } /* The speaker controls go last: a card without speakers drops them. */ @@ -3562,7 +4084,7 @@ static const struct snd_kcontrol_new macaudio_controls[] = { .name = "Speaker Volume Unlock", .info = macaudio_slk_info, .put = macaudio_slk_put, .get = macaudio_slk_get, - .lock = macaudio_slk_lock, + .lock = macaudio_slk_lock, .unlock = macaudio_slk_unlock, }, { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, @@ -3630,10 +4152,10 @@ struct macaudio_platform_cfg macaudio_j313_cfg = { * launched on the rising SCLK edge: DSP_A with an inverted bit clock. The * speakers are one MAX98360A per channel on the "spkr" back-end, with no * sense lines and no amplifier controls; the volume is the CPU DAI's - * software gain, which the volume lock holds 20 dB down, and the lease - * 20 dB down below the full scale until its engine is granted. The - * "Speaker Sense" capture is the SpeakerTap, a digital tap of the words the - * amplifier receives; there is no V/I sense. + * software gain, which the volume lock holds 20 dB down until a protection + * daemon takes it, and the lease 20 dB down below the full scale until its + * engine is granted. The "Speaker Sense" capture is the SpeakerTap, a + * digital tap of the words the amplifier receives; there is no V/I sense. */ struct macaudio_platform_cfg macaudio_j700_cfg = { .enable_speakers = true, @@ -3709,8 +4231,9 @@ struct macaudio_platform_cfg macaudio_j775_cfg = { * woofer and one tweeter per side) on "spkr", whose voltage and current * sense return on the same stream. As on the other SN012776 laptops, the * speakers are behind the speaker protection interfaces from the bind: the - * volumes stay at the lock's ceiling, or under "Speaker Protection" at the - * floor until an owner is granted. Each amp's level is its apple,speaker-config + * volumes stay at the lock's ceiling until a protection daemon + * (speakersafetyd) holds them, or under "Speaker Protection" at the floor + * until an owner is granted. Each amp's level is its apple,speaker-config * from the boot loader. */ struct macaudio_platform_cfg macaudio_j813_cfg = { @@ -3796,8 +4319,8 @@ struct macaudio_platform_cfg macaudio_k114_cfg = { * two woofers and a tweeter per side, in ADT speaker-config order (left * woofer 1, right woofer 1, left tweeter, right tweeter, left woofer 2, * right woofer 2: left in even slots, right in odd), and twelve V/I sense - * words on the same stream. Locked to the safe level until a "Speaker - * Protection" owner is granted, as on J813. Each amp's level is its apple,speaker-config + * words on the same stream. Locked to the safe level until speakersafetyd + * holds the volume, as on J813. Each amp's level is its apple,speaker-config * from the boot loader (15 on all six in the J815 ADT). */ struct macaudio_platform_cfg macaudio_j815_cfg = { @@ -3898,8 +4421,8 @@ struct macaudio_platform_cfg macaudio_j614_cfg = { /* * J604 (T8132, MacBook Pro 14" M4): the J614s's six amps, two woofers and a * tweeter per side, on all six playback and twelve sense slots of the same - * frame. The speakers stay at their safe level until a "Speaker Protection" - * owner is granted. + * frame. The speakers stay at their safe level until speakersafetyd takes + * them with the J604 profile. */ struct macaudio_platform_cfg macaudio_j604_cfg = { .enable_speakers = true, @@ -4219,6 +4742,8 @@ static int macaudio_snd_platform_probe(struct platform_device *pdev) } } + INIT_WORK(&data->lock_update_work, macaudio_vlimit_update_work); + INIT_DELAYED_WORK(&data->lock_timeout_work, macaudio_vlimit_timeout_work); INIT_WORK(&data->sw_gain_stop_work, macaudio_sw_gain_stop_work); INIT_WORK(&data->lock_fail_work, macaudio_lock_fail_work); atomic_set(&data->sw_gain_lock_epoch, 0); @@ -4242,6 +4767,8 @@ static void macaudio_snd_platform_remove(struct platform_device *pdev) /* The card's removal stops the lease timer and its works; devm frees them */ snd_soc_unregister_card(&ma->card); + cancel_work_sync(&ma->lock_update_work); + cancel_delayed_work_sync(&ma->lock_timeout_work); cancel_work_sync(&ma->sw_gain_stop_work); cancel_work_sync(&ma->lock_fail_work); cancel_delayed_work_sync(&ma->spk_unowned_work);