1 // SPDX-License-Identifier: GPL-2.0
3 * Test managed platform driver
6 #include <linux/completion.h>
7 #include <linux/device/bus.h>
8 #include <linux/device/driver.h>
9 #include <linux/platform_device.h>
11 #include <kunit/platform_device.h>
12 #include <kunit/resource.h>
14 struct kunit_platform_device_alloc_params
{
19 static int kunit_platform_device_alloc_init(struct kunit_resource
*res
, void *context
)
21 struct kunit_platform_device_alloc_params
*params
= context
;
22 struct platform_device
*pdev
;
24 pdev
= platform_device_alloc(params
->name
, params
->id
);
33 static void kunit_platform_device_alloc_exit(struct kunit_resource
*res
)
35 struct platform_device
*pdev
= res
->data
;
37 platform_device_put(pdev
);
41 * kunit_platform_device_alloc() - Allocate a KUnit test managed platform device
43 * @name: device name of platform device to alloc
44 * @id: identifier of platform device to alloc.
46 * Allocate a test managed platform device. The device is put when the test completes.
48 * Return: Allocated platform device on success, NULL on failure.
50 struct platform_device
*
51 kunit_platform_device_alloc(struct kunit
*test
, const char *name
, int id
)
53 struct kunit_platform_device_alloc_params params
= {
58 return kunit_alloc_resource(test
,
59 kunit_platform_device_alloc_init
,
60 kunit_platform_device_alloc_exit
,
63 EXPORT_SYMBOL_GPL(kunit_platform_device_alloc
);
65 static void kunit_platform_device_add_exit(struct kunit_resource
*res
)
67 struct platform_device
*pdev
= res
->data
;
69 platform_device_unregister(pdev
);
73 kunit_platform_device_alloc_match(struct kunit
*test
,
74 struct kunit_resource
*res
, void *match_data
)
76 struct platform_device
*pdev
= match_data
;
78 return res
->data
== pdev
&& res
->free
== kunit_platform_device_alloc_exit
;
81 KUNIT_DEFINE_ACTION_WRAPPER(platform_device_unregister_wrapper
,
82 platform_device_unregister
, struct platform_device
*);
84 * kunit_platform_device_add() - Register a KUnit test managed platform device
86 * @pdev: platform device to add
88 * Register a test managed platform device. The device is unregistered when the
91 * Return: 0 on success, negative errno on failure.
93 int kunit_platform_device_add(struct kunit
*test
, struct platform_device
*pdev
)
95 struct kunit_resource
*res
;
98 ret
= platform_device_add(pdev
);
102 res
= kunit_find_resource(test
, kunit_platform_device_alloc_match
, pdev
);
105 * Transfer the reference count of the platform device if it
106 * was allocated with kunit_platform_device_alloc(). In this
107 * case, calling platform_device_put() when the test exits from
108 * kunit_platform_device_alloc_exit() would lead to reference
109 * count underflow because platform_device_unregister_wrapper()
110 * calls platform_device_unregister() which also calls
111 * platform_device_put().
113 * Usually callers transfer the refcount initialized in
114 * platform_device_alloc() to platform_device_add() by calling
115 * platform_device_unregister() when platform_device_add()
116 * succeeds or platform_device_put() when it fails. KUnit has to
117 * keep this straight by redirecting the free routine for the
118 * resource to the right function. Luckily this only has to
119 * account for the success scenario.
121 res
->free
= kunit_platform_device_add_exit
;
122 kunit_put_resource(res
);
124 ret
= kunit_add_action_or_reset(test
, platform_device_unregister_wrapper
, pdev
);
131 EXPORT_SYMBOL_GPL(kunit_platform_device_add
);
133 struct kunit_platform_device_probe_nb
{
134 struct completion
*x
;
136 struct notifier_block nb
;
139 static int kunit_platform_device_probe_notify(struct notifier_block
*nb
,
140 unsigned long event
, void *data
)
142 struct kunit_platform_device_probe_nb
*knb
;
143 struct device
*dev
= data
;
145 knb
= container_of(nb
, struct kunit_platform_device_probe_nb
, nb
);
146 if (event
!= BUS_NOTIFY_BOUND_DRIVER
|| knb
->dev
!= dev
)
154 static void kunit_platform_device_probe_nb_remove(void *nb
)
156 bus_unregister_notifier(&platform_bus_type
, nb
);
160 * kunit_platform_device_prepare_wait_for_probe() - Prepare a completion
161 * variable to wait for a platform device to probe
162 * @test: test context
163 * @pdev: platform device to prepare to wait for probe of
164 * @x: completion variable completed when @dev has probed
166 * Prepare a completion variable @x to wait for @pdev to probe. Waiting on the
167 * completion forces a preemption, allowing the platform driver to probe.
173 * static int kunit_platform_driver_probe(struct platform_device *pdev)
178 * static void kunit_platform_driver_test(struct kunit *test)
180 * struct platform_device *pdev;
181 * struct platform_driver *pdrv;
182 * DECLARE_COMPLETION_ONSTACK(comp);
184 * pdev = kunit_platform_device_alloc(test, "kunit-platform", -1);
185 * KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
186 * KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_add(test, pdev));
188 * pdrv = kunit_kzalloc(test, sizeof(*pdrv), GFP_KERNEL);
189 * KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdrv);
191 * pdrv->probe = kunit_platform_driver_probe;
192 * pdrv->driver.name = "kunit-platform";
193 * pdrv->driver.owner = THIS_MODULE;
195 * KUNIT_ASSERT_EQ(test, 0, kunit_platform_device_prepare_wait_for_probe(test, pdev, &comp));
196 * KUNIT_ASSERT_EQ(test, 0, kunit_platform_driver_register(test, pdrv));
198 * KUNIT_EXPECT_NE(test, 0, wait_for_completion_timeout(&comp, 3 * HZ));
201 * Return: 0 on success, negative errno on failure.
203 int kunit_platform_device_prepare_wait_for_probe(struct kunit
*test
,
204 struct platform_device
*pdev
,
205 struct completion
*x
)
207 struct device
*dev
= &pdev
->dev
;
208 struct kunit_platform_device_probe_nb
*knb
;
211 knb
= kunit_kzalloc(test
, sizeof(*knb
), GFP_KERNEL
);
215 knb
->nb
.notifier_call
= kunit_platform_device_probe_notify
;
220 bound
= device_is_bound(dev
);
224 kunit_kfree(test
, knb
);
228 bus_register_notifier(&platform_bus_type
, &knb
->nb
);
229 device_unlock(&pdev
->dev
);
231 return kunit_add_action_or_reset(test
, kunit_platform_device_probe_nb_remove
, &knb
->nb
);
233 EXPORT_SYMBOL_GPL(kunit_platform_device_prepare_wait_for_probe
);
235 KUNIT_DEFINE_ACTION_WRAPPER(platform_driver_unregister_wrapper
,
236 platform_driver_unregister
, struct platform_driver
*);
238 * kunit_platform_driver_register() - Register a KUnit test managed platform driver
239 * @test: test context
240 * @drv: platform driver to register
242 * Register a test managed platform driver. This allows callers to embed the
243 * @drv in a container structure and use container_of() in the probe function
244 * to pass information to KUnit tests.
250 * struct kunit_test_context {
251 * struct platform_driver pdrv;
255 * static inline struct kunit_test_context *
256 * to_test_context(struct platform_device *pdev)
258 * return container_of(to_platform_driver(pdev->dev.driver),
259 * struct kunit_test_context,
263 * static int kunit_platform_driver_probe(struct platform_device *pdev)
265 * struct kunit_test_context *ctx;
267 * ctx = to_test_context(pdev);
268 * ctx->data = "test data";
273 * static void kunit_platform_driver_test(struct kunit *test)
275 * struct kunit_test_context *ctx;
277 * ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
278 * KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
280 * ctx->pdrv.probe = kunit_platform_driver_probe;
281 * ctx->pdrv.driver.name = "kunit-platform";
282 * ctx->pdrv.driver.owner = THIS_MODULE;
284 * KUNIT_EXPECT_EQ(test, 0, kunit_platform_driver_register(test, &ctx->pdrv));
285 * <... wait for driver to probe ...>
286 * KUNIT_EXPECT_STREQ(test, ctx->data, "test data");
289 * Return: 0 on success, negative errno on failure.
291 int kunit_platform_driver_register(struct kunit
*test
,
292 struct platform_driver
*drv
)
296 ret
= platform_driver_register(drv
);
300 return kunit_add_action_or_reset(test
, platform_driver_unregister_wrapper
, drv
);
302 EXPORT_SYMBOL_GPL(kunit_platform_driver_register
);