162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci// Copyright 2023 Maxime Ripard <mripard@kernel.org>
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include <kunit/resource.h>
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/device.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#define DEVICE_NAME "test"
962306a36Sopenharmony_ci
1062306a36Sopenharmony_cistruct test_priv {
1162306a36Sopenharmony_ci	bool probe_done;
1262306a36Sopenharmony_ci	bool release_done;
1362306a36Sopenharmony_ci	wait_queue_head_t release_wq;
1462306a36Sopenharmony_ci	struct device *dev;
1562306a36Sopenharmony_ci};
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cistatic int root_device_devm_init(struct kunit *test)
1862306a36Sopenharmony_ci{
1962306a36Sopenharmony_ci	struct test_priv *priv;
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci	priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
2262306a36Sopenharmony_ci	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);
2362306a36Sopenharmony_ci	init_waitqueue_head(&priv->release_wq);
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci	test->priv = priv;
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci	return 0;
2862306a36Sopenharmony_ci}
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_cistatic void devm_device_action(void *ptr)
3162306a36Sopenharmony_ci{
3262306a36Sopenharmony_ci	struct test_priv *priv = ptr;
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci	priv->release_done = true;
3562306a36Sopenharmony_ci	wake_up_interruptible(&priv->release_wq);
3662306a36Sopenharmony_ci}
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#define RELEASE_TIMEOUT_MS	100
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/*
4162306a36Sopenharmony_ci * Tests that a bus-less, non-probed device will run its device-managed
4262306a36Sopenharmony_ci * actions when unregistered.
4362306a36Sopenharmony_ci */
4462306a36Sopenharmony_cistatic void root_device_devm_register_unregister_test(struct kunit *test)
4562306a36Sopenharmony_ci{
4662306a36Sopenharmony_ci	struct test_priv *priv = test->priv;
4762306a36Sopenharmony_ci	int ret;
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	priv->dev = root_device_register(DEVICE_NAME);
5062306a36Sopenharmony_ci	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->dev);
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	ret = devm_add_action_or_reset(priv->dev, devm_device_action, priv);
5362306a36Sopenharmony_ci	KUNIT_ASSERT_EQ(test, ret, 0);
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	root_device_unregister(priv->dev);
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done,
5862306a36Sopenharmony_ci					       msecs_to_jiffies(RELEASE_TIMEOUT_MS));
5962306a36Sopenharmony_ci	KUNIT_EXPECT_GT(test, ret, 0);
6062306a36Sopenharmony_ci}
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_cistatic void devm_put_device_action(void *ptr)
6362306a36Sopenharmony_ci{
6462306a36Sopenharmony_ci	struct test_priv *priv = ptr;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	put_device(priv->dev);
6762306a36Sopenharmony_ci	priv->release_done = true;
6862306a36Sopenharmony_ci	wake_up_interruptible(&priv->release_wq);
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci/*
7262306a36Sopenharmony_ci * Tests that a bus-less, non-probed device will run its device-managed
7362306a36Sopenharmony_ci * actions when unregistered, even if someone still holds a reference to
7462306a36Sopenharmony_ci * it.
7562306a36Sopenharmony_ci */
7662306a36Sopenharmony_cistatic void root_device_devm_register_get_unregister_with_devm_test(struct kunit *test)
7762306a36Sopenharmony_ci{
7862306a36Sopenharmony_ci	struct test_priv *priv = test->priv;
7962306a36Sopenharmony_ci	int ret;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	priv->dev = root_device_register(DEVICE_NAME);
8262306a36Sopenharmony_ci	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->dev);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	get_device(priv->dev);
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	ret = devm_add_action_or_reset(priv->dev, devm_put_device_action, priv);
8762306a36Sopenharmony_ci	KUNIT_ASSERT_EQ(test, ret, 0);
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	root_device_unregister(priv->dev);
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	ret = wait_event_interruptible_timeout(priv->release_wq, priv->release_done,
9262306a36Sopenharmony_ci					       msecs_to_jiffies(RELEASE_TIMEOUT_MS));
9362306a36Sopenharmony_ci	KUNIT_EXPECT_GT(test, ret, 0);
9462306a36Sopenharmony_ci}
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_cistatic struct kunit_case root_device_devm_tests[] = {
9762306a36Sopenharmony_ci	KUNIT_CASE(root_device_devm_register_unregister_test),
9862306a36Sopenharmony_ci	KUNIT_CASE(root_device_devm_register_get_unregister_with_devm_test),
9962306a36Sopenharmony_ci	{}
10062306a36Sopenharmony_ci};
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_cistatic struct kunit_suite root_device_devm_test_suite = {
10362306a36Sopenharmony_ci	.name = "root-device-devm",
10462306a36Sopenharmony_ci	.init = root_device_devm_init,
10562306a36Sopenharmony_ci	.test_cases = root_device_devm_tests,
10662306a36Sopenharmony_ci};
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_cikunit_test_suite(root_device_devm_test_suite);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ciMODULE_DESCRIPTION("Test module for root devices");
11162306a36Sopenharmony_ciMODULE_AUTHOR("Maxime Ripard <mripard@kernel.org>");
11262306a36Sopenharmony_ciMODULE_LICENSE("GPL");
113