1 // SPDX-License-Identifier: GPL-2.0
4 #include <kunit/resource.h>
6 #include <linux/device.h>
8 #define DEVICE_NAME "test"
13 wait_queue_head_t release_wq;
17 static int root_device_devm_init(struct kunit *test)
19 struct test_priv *priv;
21 priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
22 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);
23 init_waitqueue_head(&priv->release_wq);
30 static void devm_device_action(void *ptr)
32 struct test_priv *priv = ptr;
34 priv->release_done = true;
35 wake_up_interruptible(&priv->release_wq);
38 #define RELEASE_TIMEOUT_MS 100
41 * Tests that a bus-less, non-probed device will run its device-managed
42 * actions when unregistered.
44 static void root_device_devm_register_unregister_test(struct kunit *test)
46 struct test_priv *priv = test->priv;
49 priv->dev = root_device_register(DEVICE_NAME);
50 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->dev);
52 ret = devm_add_action_or_reset(priv->dev, devm_device_action, priv);
53 KUNIT_ASSERT_EQ(test, ret, 0);
55 root_device_unregister(priv->dev);
57 ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done,
58 msecs_to_jiffies(RELEASE_TIMEOUT_MS));
59 KUNIT_EXPECT_GT(test, ret, 0);
62 static void devm_put_device_action(void *ptr)
64 struct test_priv *priv = ptr;
66 put_device(priv->dev);
67 priv->release_done = true;
68 wake_up_interruptible(&priv->release_wq);
72 * Tests that a bus-less, non-probed device will run its device-managed
73 * actions when unregistered, even if someone still holds a reference to
76 static void root_device_devm_register_get_unregister_with_devm_test(struct kunit *test)
78 struct test_priv *priv = test->priv;
81 priv->dev = root_device_register(DEVICE_NAME);
82 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->dev);
84 get_device(priv->dev);
86 ret = devm_add_action_or_reset(priv->dev, devm_put_device_action, priv);
87 KUNIT_ASSERT_EQ(test, ret, 0);
89 root_device_unregister(priv->dev);
91 ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done,
92 msecs_to_jiffies(RELEASE_TIMEOUT_MS));
93 KUNIT_EXPECT_GT(test, ret, 0);
96 static struct kunit_case root_device_devm_tests[] = {
97 KUNIT_CASE(root_device_devm_register_unregister_test),
98 KUNIT_CASE(root_device_devm_register_get_unregister_with_devm_test),
102 static struct kunit_suite root_device_devm_test_suite = {
103 .name = "root-device-devm",
104 .init = root_device_devm_init,
105 .test_cases = root_device_devm_tests,
108 kunit_test_suite(root_device_devm_test_suite);
110 MODULE_DESCRIPTION("Test module for root devices");
112 MODULE_LICENSE("GPL");