ALSA: hda/cirrus_scodec_test: Modernize creation of dummy devices
PATCH 1/1 ALSA: hda/cirrus_scodec_test: Modernize creation of dummy devices
Replace the old direct use of platform_device APIs with newer KUnit APIs and the faux bus.
The dummy codec driver device doesn't need to be a platform device. It can be a faux bus device.
The dummy GPIO driver still must be a platform_device so that a software_node can be added to it before it probes. But use the new KUnit-managed APIs to create the platform_device and platform_driver. These will cleanup automatically when a test completes or fails.
Also use KUnit resource cleanup to destroy the faux bus driver and the GPIO software node instead of doing this "manually" in test exit() functions.
--- sound/pci/hda/cirrus_scodec_test.c | 110 +++++++++-------------------- 1 file changed, 35 insertions(+), 75 deletions(-)
sound/pci/hda/cirrus_scodec_test.c +35 -75
@@ -5,20 +5,30 @@// Copyright (C) 2023 Cirrus Logic, Inc. and // Cirrus Logic International Semiconductor Ltd. +#include <kunit/platform_device.h> +#include <kunit/resource.h> #include <kunit/test.h> +#include <linux/device.h> +#include <linux/device/faux.h> #include <linux/gpio/driver.h> #include <linux/module.h> #include <linux/platform_device.h> #include "cirrus_scodec.h" +KUNIT_DEFINE_ACTION_WRAPPER(faux_device_destroy_wrapper, faux_device_destroy, + struct faux_device *) +KUNIT_DEFINE_ACTION_WRAPPER(device_remove_software_node_wrapper, + device_remove_software_node, + struct device *) + struct cirrus_scodec_test_gpio { unsigned int pin_state; struct gpio_chip chip; }; struct cirrus_scodec_test_priv { -struct platform_device amp_pdev; + struct faux_device *amp_dev; struct platform_device *gpio_pdev; struct cirrus_scodec_test_gpio *gpio_priv; };
@@ -104,6 +114,7 @@ static int cirrus_scodec_test_gpio_probe(struct platform_device *pdev)static struct platform_driver cirrus_scodec_test_gpio_driver = { .driver.name ="cirrus_scodec_test_gpio_drv" , + .driver.owner =THIS_MODULE , .probe = cirrus_scodec_test_gpio_probe, };
@@ -112,37 +123,28 @@ static const struct software_node cirrus_scodec_test_gpio_swnode = { .name = "cirrus_scodec_test_gpio", }; -static int cirrus_scodec_test_create_gpio (struct kunit *test) +static void cirrus_scodec_test_create_gpio (struct kunit *test) {struct cirrus_scodec_test_priv *priv = test->priv; -int ret; - priv->gpio_pdev =platform_device_alloc (cirrus_scodec_test_gpio_driver.driver.name,-1 ); -if (!priv->gpio_pdev) -return -ENOMEM ; +KUNIT_ASSERT_EQ (test,0 , +kunit_platform_driver_register (test, &cirrus_scodec_test_gpio_driver)); - ret =device_add_software_node (&priv->gpio_pdev->dev, &cirrus_scodec_test_gpio_swnode); -if (ret) { -platform_device_put (priv->gpio_pdev); -KUNIT_FAIL (test,"Failed to add swnode to gpio: %d\n" , ret); -return ret; - } + priv->gpio_pdev =kunit_platform_device_alloc (test, + cirrus_scodec_test_gpio_driver.driver.name, +PLATFORM_DEVID_NONE ); +KUNIT_ASSERT_NOT_NULL (test, priv->gpio_pdev); - ret =platform_device_add (priv->gpio_pdev); -if (ret) { -platform_device_put (priv->gpio_pdev); -KUNIT_FAIL (test,"Failed to add gpio platform device: %d\n" , ret); -return ret; - } +KUNIT_ASSERT_EQ (test,0 ,device_add_software_node (&priv->gpio_pdev->dev, + &cirrus_scodec_test_gpio_swnode)); +KUNIT_ASSERT_EQ (test,0 ,kunit_add_action_or_reset (test, + device_remove_software_node_wrapper, + &priv->gpio_pdev->dev)); - priv->gpio_priv =dev_get_drvdata (&priv->gpio_pdev->dev); -if (!priv->gpio_priv) { -platform_device_put (priv->gpio_pdev); -KUNIT_FAIL (test,"Failed to get gpio private data\n" ); -return -EINVAL ; - } +KUNIT_ASSERT_EQ (test,0 ,kunit_platform_device_add (test, priv->gpio_pdev)); -return 0 ; + priv->gpio_priv =dev_get_drvdata (&priv->gpio_pdev->dev); +KUNIT_ASSERT_NOT_NULL (test, priv->gpio_priv); } static void cirrus_scodec_test_set_gpio_ref_arg(struct software_node_ref_args *arg,
@@ -192,7 +194,7 @@ static void cirrus_scodec_test_spkid_parse(struct kunit *test)const struct cirrus_scodec_test_spkid_param *param = test->param_value;int num_spk_id_refs = param->num_amps * param->gpios_per_amp;struct software_node_ref_args *refs; -struct device *dev = &priv->amp_pdev.dev; +struct device *dev = &priv->amp_dev->dev;unsigned int v;int i, ret;
@@ -235,21 +237,16 @@ static void cirrus_scodec_test_spkid_parse(struct kunit *test)static void cirrus_scodec_test_no_spkid (struct kunit *test) {struct cirrus_scodec_test_priv *priv = test->priv; -struct device *dev = &priv->amp_pdev.dev; +struct device *dev = &priv->amp_dev->dev;int ret; ret =cirrus_scodec_get_speaker_id (dev,0 ,4 ,-1 );KUNIT_EXPECT_EQ (test, ret, -ENOENT ); } -static void cirrus_scodec_test_dev_release (struct device *dev) -{ -} -static int cirrus_scodec_test_case_init (struct kunit *test) {struct cirrus_scodec_test_priv *priv; -int ret; priv =kunit_kzalloc (test,sizeof (*priv),GFP_KERNEL );if (!priv)
@@ -258,52 +255,18 @@ static int cirrus_scodec_test_case_init(struct kunit *test) test->priv = priv;/* Create dummy GPIO */ - ret =cirrus_scodec_test_create_gpio (test); -if (ret <0 ) -return ret; +cirrus_scodec_test_create_gpio (test);/* Create dummy amp driver dev */ - priv->amp_pdev.name ="cirrus_scodec_test_amp_drv" ; - priv->amp_pdev.id =-1 ; - priv->amp_pdev.dev.release = cirrus_scodec_test_dev_release; - ret =platform_device_register (&priv->amp_pdev); -KUNIT_ASSERT_GE_MSG (test, ret,0 ,"Failed to register amp platform device\n" ); + priv->amp_dev =faux_device_create ("cirrus_scodec_test_amp_drv" ,NULL ,NULL ); +KUNIT_ASSERT_NOT_NULL (test, priv->amp_dev); +KUNIT_ASSERT_EQ (test,0 ,kunit_add_action_or_reset (test, + faux_device_destroy_wrapper, + priv->amp_dev));return 0 ; } -static void cirrus_scodec_test_case_exit (struct kunit *test) -{ -struct cirrus_scodec_test_priv *priv = test->priv; - -if (priv->amp_pdev.name) -platform_device_unregister (&priv->amp_pdev); - -if (priv->gpio_pdev) { -device_remove_software_node (&priv->gpio_pdev->dev); -platform_device_unregister (priv->gpio_pdev); - } -} - -static int cirrus_scodec_test_suite_init (struct kunit_suite *suite) -{ -int ret; - -/* Register mock GPIO driver */ - ret =platform_driver_register (&cirrus_scodec_test_gpio_driver); -if (ret <0 ) { -kunit_err (suite,"Failed to register gpio platform driver, %d\n" , ret); -return ret; - } - -return 0 ; -} - -static void cirrus_scodec_test_suite_exit (struct kunit_suite *suite) -{ -platform_driver_unregister (&cirrus_scodec_test_gpio_driver); -} -static const struct cirrus_scodec_test_spkid_param cirrus_scodec_test_spkid_param_cases[] = { { .num_amps =2 , .gpios_per_amp =1 , .num_amps_sharing =1 }, { .num_amps =2 , .gpios_per_amp =2 , .num_amps_sharing =1 },
@@ -357,10 +320,7 @@ static struct kunit_case cirrus_scodec_test_cases[] = {static struct kunit_suite cirrus_scodec_test_suite = { .name ="snd-hda-scodec-cs35l56-test" , - .suite_init = cirrus_scodec_test_suite_init, - .suite_exit = cirrus_scodec_test_suite_exit, .init = cirrus_scodec_test_case_init, - .exit = cirrus_scodec_test_case_exit, .test_cases = cirrus_scodec_test_cases, };
Signature · 1 line
2.39.5
Takashi Iwai
AppliedQuoted 12 lines · Richard Fitzgerald
Replace the old direct use of
platform_deviceAPIs with newer KUnit APIs and the faux bus.The dummy codec driver device doesn't need to be a platform device. It can be a faux bus device.
The dummy GPIO driver still must be a
platform_deviceso that asoftware_nodecan be added to it before it probes. But use the new KUnit-managed APIs to create theplatform_deviceandplatform_driver. These will cleanup automatically when a test completes or fails.
Quoted 4 lines
Also use KUnit resource cleanup to destroy the faux bus driver and the GPIO software node instead of doing this "manually" in test
exit()functions.Signed-off-by Richard Fitzgerald <[email protected]>
Thanks, applied now to for-next branch.
Takashi