diff --git a/sound/soc/apple/macaudio-pm-volumes-test.h b/sound/soc/apple/macaudio-pm-volumes-test.h index b01ac3fc3594c..3e827fd7ae3e7 100644 --- a/sound/soc/apple/macaudio-pm-volumes-test.h +++ b/sound/soc/apple/macaudio-pm-volumes-test.h @@ -100,6 +100,9 @@ static void macaudio_pmt_pm_card(struct kunit *test, struct macaudio_pmt_pm *t) }; struct macaudio_snd_data *ma; struct snd_card *card; + static unsigned int seq; + struct device *dev; + char name[32]; unsigned int i; int ret; @@ -107,12 +110,17 @@ static void macaudio_pmt_pm_card(struct kunit *test, struct macaudio_pmt_pm *t) card = kunit_kzalloc(test, sizeof(*card), GFP_KERNEL); KUNIT_ASSERT_NOT_NULL(test, ma); KUNIT_ASSERT_NOT_NULL(test, card); + /* Cases may build more than one card: each needs its own driver name */ + snprintf(name, sizeof(name), "macaudio-pmt-test-%u", seq++); + dev = kunit_device_register(test, name); + KUNIT_ASSERT_FALSE(test, IS_ERR(dev)); INIT_LIST_HEAD(&card->controls); INIT_LIST_HEAD(&card->ctl_files); init_rwsem(&card->controls_rwsem); rwlock_init(&card->controls_rwlock); ma->card.snd_card = card; + ma->card.dev = dev; snd_soc_card_set_drvdata(&ma->card, ma); ma->cfg = &macaudio_j493_cfg; ma->has_speakers = true; @@ -149,6 +157,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, before userspace sees the card */ + KUNIT_ASSERT_EQ(test, macaudio_spk_take_volumes(ma), 0); + KUNIT_ASSERT_EQ(test, ma->spk_nr_vols, MACAUDIO_PMT_NR_VOLS - 1); } /* A daemon takes the lock and every speaker volume, as speakersafetyd does */ @@ -256,10 +267,111 @@ static void macaudio_pm_volumes_new_owner(struct kunit *test) macaudio_pmt_expect_vols(test, &t, true); } +static int macaudio_pmt_user_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *uval) +{ + uval->value.integer.value[0] = *(long *)kctl->private_data; + return 0; +} + +static int macaudio_pmt_user_put(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *uval) +{ + *(long *)kctl->private_data = uval->value.integer.value[0]; + return 1; +} + +/* + * A user-defined element (SNDRV_CTL_IOCTL_ELEM_ADD) named like a speaker + * volume, added after the card registered: no private_value, not the + * driver's, never locked, kept or set. + */ +static struct snd_kcontrol *macaudio_pmt_add_user_kctl(struct kunit *test, + struct macaudio_pmt_pm *t, + long *value) +{ + struct snd_kcontrol_new tmpl = { + .iface = SNDRV_CTL_ELEM_IFACE_MIXER, + .name = "User Speaker Volume", + .info = macaudio_pmt_vol_info, + .get = macaudio_pmt_user_get, + .put = macaudio_pmt_user_put, + }; + struct snd_kcontrol *kctl; + + kctl = macaudio_pmt_add_kctl(test, t->ma->card.snd_card, &tmpl, value); + /* As snd_ctl_elem_add() marks it; snd_ctl_new1() filters the flag out */ + kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_USER; + return kctl; +} + +static void macaudio_pm_volumes_ignore_user_elements(struct kunit *test) +{ + struct macaudio_pmt_pm t; + struct snd_kcontrol *user; + long value = 33; + + macaudio_pmt_pm_card(test, &t); + user = macaudio_pmt_add_user_kctl(test, &t, &value); + KUNIT_EXPECT_FALSE(test, snd_soc_control_matches(user, "* Speaker Volume")); + + /* Not the lock owner's, and still not in the way of the unlock */ + 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); + + /* The lock clips the driver's volumes and keeps only those */ + macaudio_pmt_notify(&t, PM_SUSPEND_PREPARE); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + macaudio_pmt_expect_vols(test, &t, true); + KUNIT_EXPECT_EQ(test, t.ma->pm_nr_volumes, MACAUDIO_PMT_NR_VOLS - 1); + KUNIT_EXPECT_EQ(test, value, 33); + + macaudio_pmt_notify(&t, PM_POST_SUSPEND); + 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_EQ(test, value, 33); +} + +/* + * A card bound again (a codec unbound and bound) takes its volumes afresh: + * the kept set is the new binding's, never the old one's plus the new. + */ +static void macaudio_pm_volumes_taken_again(struct kunit *test) +{ + struct macaudio_pmt_pm t; + unsigned int i; + + macaudio_pmt_pm_card(test, &t); + KUNIT_ASSERT_EQ(test, macaudio_spk_take_volumes(t.ma), 0); + KUNIT_ASSERT_EQ(test, t.ma->spk_nr_vols, MACAUDIO_PMT_NR_VOLS - 1); + for (i = 0; i < MACAUDIO_PMT_NR_VOLS - 1; i++) + KUNIT_EXPECT_PTR_EQ(test, t.ma->spk_vols[i], t.vols[i]); + + /* As macaudio_card_remove() leaves them for the next late_probe */ + macaudio_spk_drop_controls(t.ma); + KUNIT_EXPECT_EQ(test, t.ma->spk_nr_vols, 0); + KUNIT_EXPECT_NULL(test, t.ma->spk_vols); + KUNIT_ASSERT_EQ(test, macaudio_spk_take_volumes(t.ma), 0); + KUNIT_ASSERT_EQ(test, t.ma->spk_nr_vols, MACAUDIO_PMT_NR_VOLS - 1); + + /* The lock still covers exactly the speaker volumes */ + macaudio_pmt_take(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + macaudio_pmt_notify(&t, PM_SUSPEND_PREPARE); + macaudio_pmt_expect_vols(test, &t, true); + KUNIT_EXPECT_EQ(test, t.ma->pm_nr_volumes, MACAUDIO_PMT_NR_VOLS - 1); + macaudio_pmt_notify(&t, PM_POST_SUSPEND); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + macaudio_pmt_expect_vols(test, &t, false); +} + static struct kunit_case macaudio_pm_volumes_cases[] = { + KUNIT_CASE(macaudio_pm_volumes_ignore_user_elements), 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_taken_again), {} }; diff --git a/sound/soc/apple/macaudio.c b/sound/soc/apple/macaudio.c index e80c5a64b0265..3cd2d9386b7e4 100644 --- a/sound/soc/apple/macaudio.c +++ b/sound/soc/apple/macaudio.c @@ -575,6 +575,7 @@ static void macaudio_vlimit_unlock(struct macaudio_snd_data *ma, bool unlock) const char *name = volume_control_names[ma->cfg->amp]; struct snd_kcontrol *kctl; bool failed = false; + unsigned int i; if (!name) { WARN_ON_ONCE(1); @@ -611,12 +612,11 @@ static void macaudio_vlimit_unlock(struct macaudio_snd_data *ma, bool unlock) /* * One control at a time: snd_soc_limit_volume() stops at the first * control it cannot clip, which would leave the rest at their old - * limit and value. + * limit and value. Only the driver's own volume controls, taken before + * the card registered (macaudio_spk_take_volumes()). */ - list_for_each_entry(kctl, &ma->card.snd_card->controls, list) { - if (!snd_soc_control_matches(kctl, name)) - continue; - + for (i = 0; i < ma->spk_nr_vols; i++) { + kctl = ma->spk_vols[i]; hits++; ret = snd_soc_limit_volume(&ma->card, kctl->id.name, max); if (ret < 0) { @@ -625,6 +625,7 @@ static void macaudio_vlimit_unlock(struct macaudio_snd_data *ma, bool unlock) failed = true; } } + /* None kept (not taken yet, or dropped with the card): fail closed. */ if (!hits) dev_err(ma->card.dev, "Failed to %slock volume %s: %d\n", unlock ? "un" : "", name, -EINVAL); @@ -733,9 +734,8 @@ static void macaudio_pm_volumes_save(struct macaudio_snd_data *ma) 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++; + /* The driver's own volume controls, which live as long as the card */ + nr = ma->spk_nr_vols; if (!nr) return; @@ -744,9 +744,8 @@ static void macaudio_pm_volumes_save(struct macaudio_snd_data *ma) if (!vols || !uval) goto out; - list_for_each_entry(kctl, &ma->card.snd_card->controls, list) { - if (!snd_soc_control_matches(kctl, name)) - continue; + for (i = 0; i < nr; i++) { + kctl = ma->spk_vols[i]; memset(uval, 0, sizeof(*uval)); uval->id = kctl->id; ret = kctl->get(kctl, uval); @@ -758,7 +757,6 @@ static void macaudio_pm_volumes_save(struct macaudio_snd_data *ma) 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; @@ -805,7 +803,7 @@ static void macaudio_pm_volumes_restore(struct macaudio_snd_data *ma) static void macaudio_vlimit_update(struct macaudio_snd_data *ma) { - int i; + unsigned int i, j; bool unlock = true; struct snd_kcontrol *kctl; const char *reason; @@ -856,10 +854,8 @@ static void macaudio_vlimit_update(struct macaudio_snd_data *ma) } /* 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 (j = 0; j < ma->spk_nr_vols; j++) { + kctl = ma->spk_vols[j]; 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"; @@ -2291,6 +2287,52 @@ static int macaudio_spk_owned_write_check(struct snd_kcontrol *kctl, return macaudio_spk_leased(ma) ? -EPERM : 0; } +/* + * Forget the speaker controls the take helpers below keep. They belong to + * one binding of the card: its controls go with it, and a later late_probe + * takes the new ones afresh. + */ +static void macaudio_spk_drop_volumes(struct macaudio_snd_data *ma) +{ + struct device *dev = ma->card.dev; + + if (ma->spk_vols) + devm_kfree(dev, ma->spk_vols); + if (ma->spk_uval) + devm_kfree(dev, ma->spk_uval); + ma->spk_vols = NULL; + ma->spk_uval = NULL; + ma->spk_nr_vols = 0; +} + +static void macaudio_spk_drop_amp_gains(struct macaudio_snd_data *ma) +{ + if (ma->spk_amp_gains) + devm_kfree(ma->card.dev, ma->spk_amp_gains); + ma->spk_amp_gains = NULL; + ma->spk_nr_amp_gains = 0; +} + +static void macaudio_spk_drop_hpfs(struct macaudio_snd_data *ma) +{ + struct device *dev = ma->card.dev; + + if (ma->spk_hpfs) + devm_kfree(dev, ma->spk_hpfs); + if (ma->spk_hpf_granted) + devm_kfree(dev, ma->spk_hpf_granted); + ma->spk_hpfs = NULL; + ma->spk_hpf_granted = NULL; + ma->spk_nr_hpfs = 0; +} + +static void macaudio_spk_drop_controls(struct macaudio_snd_data *ma) +{ + macaudio_spk_drop_volumes(ma); + macaudio_spk_drop_amp_gains(ma); + macaudio_spk_drop_hpfs(ma); +} + /* * Keep the speaker volumes, which the lease sets, and refuse userspace * writes to them while leased (macaudio_spk_owned_write_check()). @@ -2313,7 +2355,7 @@ static int macaudio_spk_take_volumes(struct macaudio_snd_data *ma) if (!nr) return -ENOENT; - ma->spk_nr_vols = 0; + macaudio_spk_drop_volumes(ma); ma->spk_vols = devm_kcalloc(dev, nr, sizeof(*ma->spk_vols), GFP_KERNEL); ma->spk_uval = devm_kzalloc(dev, sizeof(*ma->spk_uval), GFP_KERNEL); if (!ma->spk_vols || !ma->spk_uval) @@ -2352,6 +2394,7 @@ static int macaudio_spk_take_amp_gains(struct macaudio_snd_data *ma) list_for_each_entry(kctl, &ma->card.snd_card->controls, list) if (macaudio_is_amp_gain(ma, kctl)) nr++; + macaudio_spk_drop_amp_gains(ma); if (!nr) return 0; @@ -2436,7 +2479,7 @@ static int macaudio_spk_take_hpfs(struct macaudio_snd_data *ma) if (!nr) return -ENOENT; - ma->spk_nr_hpfs = 0; + macaudio_spk_drop_hpfs(ma); ma->spk_hpfs = devm_kcalloc(dev, nr, sizeof(*ma->spk_hpfs), GFP_KERNEL); ma->spk_hpf_granted = devm_kcalloc(dev, nr, sizeof(*ma->spk_hpf_granted), GFP_KERNEL); if (!ma->spk_hpfs || !ma->spk_hpf_granted) @@ -3770,6 +3813,8 @@ static int macaudio_card_remove(struct snd_soc_card *card) cancel_delayed_work_sync(&ma->lock_timeout_work); mutex_lock(&ma->volume_lock_mutex); macaudio_pm_volumes_drop(ma); + /* The controls they point to go with this binding of the card. */ + macaudio_spk_drop_controls(ma); mutex_unlock(&ma->volume_lock_mutex); /* Clear per-bind control references before any subsequent ASoC rebind. */ diff --git a/sound/soc/soc-ops.c b/sound/soc/soc-ops.c index 3fbfdac6eda87..9a2a1d058e510 100644 --- a/sound/soc/soc-ops.c +++ b/sound/soc/soc-ops.c @@ -408,6 +408,15 @@ bool snd_soc_control_matches(struct snd_kcontrol *kctl, { const char *name = kctl->id.name; + /* + * A user-defined element (SNDRV_CTL_IOCTL_ELEM_ADD) is not one of the + * card's controls: its name says nothing about it and its + * private_value is not a struct soc_mixer_control, so it never + * matches a driver's pattern. + */ + if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_USER) + return false; + if (pattern[0] == '*') { int namelen; int patternlen; diff --git a/tools/testing/selftests/alsa/macaudio-fixed-controls.py b/tools/testing/selftests/alsa/macaudio-fixed-controls.py index 05f9602bb923a..294c782fad511 100644 --- a/tools/testing/selftests/alsa/macaudio-fixed-controls.py +++ b/tools/testing/selftests/alsa/macaudio-fixed-controls.py @@ -33,6 +33,7 @@ def function(source, name): #include #define SNDRV_CTL_ELEM_ACCESS_WRITE 2 #define SNDRV_CTL_ELEM_ACCESS_INACTIVE 256 +#define SNDRV_CTL_ELEM_ACCESS_USER (1 << 29) #define SNDRV_CTL_EVENT_MASK_INFO 2 #define SNDRV_CTL_EVENT_MASK_VALUE 1 #define SNDRV_CTL_ELEM_TYPE_ENUMERATED 3 @@ -80,6 +81,7 @@ def function(source, name): static bool macaudio_spk_leased(const struct macaudio_snd_data *ma) { return ma->spk_iface == 1; } #define GFP_KERNEL 0 #define devm_kcalloc(d, n, s, g) calloc(n, s) +#define devm_kfree(d, p) free(p) #define kzalloc_obj(o) calloc(1, sizeof(o)) #define kfree free #define simple_strtoul strtoul @@ -285,7 +287,8 @@ def function(source, name): check(regs[0].reg == 6 && regs[1].reg == 6, "safe 800 Hz", "HPF"); check(macaudio_spk_set_hpf(&ma, true) == 0, "grant", "HPF"); for (i = 0; i < 2; i++) check(regs[i].reg == (initial + i) % 7, "restore each amp DT corner", "HPF"); - free(ma.spk_hpfs); free(ma.spk_hpf_granted); + macaudio_spk_drop_hpfs(&ma); + check(!ma.spk_hpfs && !ma.spk_hpf_granted && !ma.spk_nr_hpfs, "drop clears capture", "HPF"); } #ifdef HAVE_DYNAMIC_HPF_SAFE_ITEM /* Dynamic safe-corner selection picks the highest " Hz" item, regardless of order. */ @@ -374,6 +377,7 @@ def main(): function(ma, "macaudio_protect_sense"), *(["#define HAVE_DYNAMIC_HPF_SAFE_ITEM 1", function(ma, "macaudio_hpf_safe_item")] if has_dyn_hpf else [function(ma, "macaudio_enum_item")]), + function(ma, "macaudio_spk_drop_hpfs"), function(ma, "macaudio_spk_take_hpfs"), function(ma, "macaudio_spk_set_hpf")] if "static int macaudio_set_fixed_hpf(" in ma: