diff --git a/drivers/gpu/drm/apple/audio-card-test.c b/drivers/gpu/drm/apple/audio-card-test.c new file mode 100644 index 0000000000000..7413c11a85537 --- /dev/null +++ b/drivers/gpu/drm/apple/audio-card-test.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* Included by audio.c: what the DCP reports reaches the card until it is unbound. */ +#include +#include +#include + +static void dcpaud_card_unbound_test(struct kunit *test) +{ + struct dcp_audio *dcpaud = kunit_kzalloc(test, sizeof(*dcpaud), GFP_KERNEL); + struct platform_device *pdev; + + KUNIT_ASSERT_NOT_NULL(test, dcpaud); + pdev = kunit_platform_device_alloc(test, "dcpaud-card", PLATFORM_DEVID_AUTO); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev); + KUNIT_ASSERT_EQ(test, kunit_platform_device_add(test, pdev), 0); + platform_set_drvdata(pdev, dcpaud); + dcpaud->dev = &pdev->dev; + dcpaud->port_name = "Test"; + dcpaud->port_id = "Test"; + mutex_init(&dcpaud->data_lock); +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) + mutex_init(&dcpaud->link_lock); + INIT_WORK(&dcpaud->stale_work, dcpaud_stale_work); + dcpaud->native_link = true; +#endif + INIT_DELAYED_WORK(&dcpaud->void_work, dcpaud_void_work); + KUNIT_ASSERT_EQ(test, dcpaud_init_snd_card(dcpaud), 0); + + dcpaud_connect(pdev, true); + KUNIT_EXPECT_TRUE(test, dcpaud->connected); + dcpaud_disconnect(pdev); + KUNIT_EXPECT_FALSE(test, dcpaud->connected); + dcpaud_connect(pdev, true); + + dcpaud_comp_unbind(&pdev->dev, NULL, NULL); + KUNIT_EXPECT_NULL(test, dcpaud->card); + KUNIT_EXPECT_FALSE(test, dcpaud->connected); + /* The DCP, unbound after it, still reports. */ + dcpaud_disconnect(pdev); + dcpaud_connect(pdev, true); + KUNIT_EXPECT_FALSE(test, dcpaud->connected); + dcpaud_refresh_eld(pdev); +#if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) + KUNIT_EXPECT_FALSE(test, work_pending(&dcpaud->stale_work)); +#endif +} + +static struct kunit_case dcpaud_card_cases[] = { + KUNIT_CASE(dcpaud_card_unbound_test), + {} +}; + +static struct kunit_suite dcpaud_card_suite = { + .name = "apple-dcpaud-card", + .test_cases = dcpaud_card_cases, +}; + +kunit_test_suite(dcpaud_card_suite); diff --git a/drivers/gpu/drm/apple/audio.c b/drivers/gpu/drm/apple/audio.c index f96300bd043b5..e4eb7c8bc7ad9 100644 --- a/drivers/gpu/drm/apple/audio.c +++ b/drivers/gpu/drm/apple/audio.c @@ -1797,13 +1797,27 @@ static void dcpaud_build_eld(struct dcp_audio *dcpaud, u8 *eld) DIV_ROUND_UP(drm_eld_calc_baseline_block_size(eld), 4); } +/* + * The card, while dcpaud_comp_unbind() has not taken it. The reference keeps it + * with its controls, jack and PCM should it be freed meanwhile; ALSA ignores + * what is reported to a disconnected card. Called with data_lock held. + */ +static struct snd_card *dcpaud_card_get(struct dcp_audio *dcpaud) +{ + lockdep_assert_held(&dcpaud->data_lock); + if (dcpaud->card) + get_device(&dcpaud->card->card_dev); + return dcpaud->card; +} + /* expects to be called with data_lock locked and unlocks it */ static void dcpaud_report_hotplug(struct dcp_audio *dcpaud, bool connected, const u8 *eld) { + struct snd_card *card __free(snd_card_unref) = dcpaud_card_get(dcpaud); struct snd_pcm_substream *substream = dcpaud->substream; - if (!dcpaud->card) { + if (!card) { mutex_unlock(&dcpaud->data_lock); return; } @@ -1819,8 +1833,7 @@ static void dcpaud_report_hotplug(struct dcp_audio *dcpaud, bool connected, memcpy(dcpaud->eld, eld, DCPAUD_ELD_SIZE); mutex_unlock(&dcpaud->data_lock); if (changed) - snd_ctl_notify(dcpaud->card, SNDRV_CTL_EVENT_MASK_VALUE, - &dcpaud->eld_ctl->id); + snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &dcpaud->eld_ctl->id); return; } @@ -1836,7 +1849,7 @@ static void dcpaud_report_hotplug(struct dcp_audio *dcpaud, bool connected, mutex_unlock(&dcpaud->data_lock); /* The name first: the jack makes the output available. */ - snd_ctl_notify(dcpaud->card, SNDRV_CTL_EVENT_MASK_VALUE, &dcpaud->eld_ctl->id); + snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &dcpaud->eld_ctl->id); snd_jack_report(dcpaud->jack, connected ? SND_JACK_AVOUT : 0); if (!connected) { @@ -2088,11 +2101,14 @@ static bool dcpaud_refresh_eld_from_edid(struct dcp_audio *dcpaud) void dcpaud_refresh_eld(struct platform_device *pdev) { struct dcp_audio *dcpaud = platform_get_drvdata(pdev); + struct snd_card *card __free(snd_card_unref) = NULL; - if (!dcpaud || !dcpaud->dcp_dev || !dcpaud->card) + if (!dcpaud || !dcpaud->dcp_dev) return; - if (dcpaud_refresh_eld_from_edid(dcpaud)) - snd_ctl_notify(dcpaud->card, SNDRV_CTL_EVENT_MASK_VALUE, &dcpaud->eld_ctl->id); + scoped_guard(mutex, &dcpaud->data_lock) + card = dcpaud_card_get(dcpaud); + if (card && dcpaud_refresh_eld_from_edid(dcpaud)) + snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &dcpaud->eld_ctl->id); } void dcpaud_disconnect(struct platform_device *pdev) @@ -2105,9 +2121,15 @@ void dcpaud_disconnect(struct platform_device *pdev) dcpaud_report_hotplug(dcpaud, false, NULL); #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) - /* The service may go without a release, and a held link with it. */ - if (dcpaud->native_link) - schedule_work(&dcpaud->stale_work); + /* + * The service may go without a release, and a held link with it. Not + * once dcpaud_comp_unbind() took the card: it cancels this and ends the + * link itself. + */ + scoped_guard(mutex, &dcpaud->data_lock) { + if (dcpaud->native_link && dcpaud->card) + schedule_work(&dcpaud->stale_work); + } #endif } @@ -2234,6 +2256,7 @@ static int dcpaud_comp_bind(struct device *dev, struct device *main, void *data) static void dcpaud_unbind(struct device *dev) { struct dcp_audio *dcpaud = dev_get_drvdata(dev); + struct snd_card *card; #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) /* @@ -2246,8 +2269,20 @@ static void dcpaud_unbind(struct device *dev) dcpaud->replaced_enabled = false; } #endif + + /* + * The DCP unbinds after its audio device: what it still reports must + * not reach the card. The sink goes with it, so that a card made again + * gets its jack and ELD from the next report. + */ + scoped_guard(mutex, &dcpaud->data_lock) { + card = dcpaud->card; + dcpaud->card = NULL; + dcpaud->connected = false; + memset(dcpaud->eld, 0, DCPAUD_ELD_SIZE); + } /* snd_card_free_when_closed() checks for NULL */ - snd_card_free_when_closed(dcpaud->card); + snd_card_free_when_closed(card); #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO) if (dcpaud->native_link) { @@ -2606,6 +2641,7 @@ void dcp_audio_unregister(void) } #if IS_ENABLED(CONFIG_DRM_APPLE_DISPLAY_KUNIT_TEST) +#include "audio-card-test.c" #include "audio-no-sio-test.c" #include "audio-legacy-test.c" #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T6030_AUDIO)