xref: /linux/include/kunit/device.h (revision 2697b79a469b68e3ad3640f55284359c1396278d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * KUnit basic device implementation
4  *
5  * Helpers for creating and managing fake devices for KUnit tests.
6  *
7  * Copyright (C) 2023, Google LLC.
8  * Author: David Gow <davidgow@google.com>
9  */
10 
11 #ifndef _KUNIT_DEVICE_H
12 #define _KUNIT_DEVICE_H
13 
14 #if IS_ENABLED(CONFIG_KUNIT)
15 
16 #include <kunit/test.h>
17 
18 struct device;
19 struct device_driver;
20 
21 /**
22  * kunit_driver_create() - Create a struct device_driver attached to the kunit_bus
23  * @test: The test context object.
24  * @name: The name to give the created driver.
25  *
26  * Creates a struct device_driver attached to the kunit_bus, with the name @name.
27  * This driver will automatically be cleaned up on test exit.
28  *
29  * Return: a stub struct device_driver, managed by KUnit, with the name @name.
30  */
31 struct device_driver *kunit_driver_create(struct kunit *test, const char *name);
32 
33 /**
34  * kunit_device_register() - Create a struct device for use in KUnit tests
35  * @test: The test context object.
36  * @name: The name to give the created device.
37  *
38  * Creates a struct kunit_device (which is a struct device) with the given name,
39  * and a corresponding driver. The device and driver will be cleaned up on test
40  * exit, or when kunit_device_unregister is called. See also
41  * kunit_device_register_with_driver, if you wish to provide your own
42  * struct device_driver.
43  *
44  * Return: a pointer to a struct device which will be cleaned up when the test
45  * exits, or an error pointer if the device could not be allocated or registered.
46  */
47 struct device *kunit_device_register(struct kunit *test, const char *name);
48 
49 /**
50  * kunit_device_register_with_driver() - Create a struct device for use in KUnit tests
51  * @test: The test context object.
52  * @name: The name to give the created device.
53  * @drv: The struct device_driver to associate with the device.
54  *
55  * Creates a struct kunit_device (which is a struct device) with the given
56  * name, and driver. The device will be cleaned up on test exit, or when
57  * kunit_device_unregister is called. See also kunit_device_register, if you
58  * wish KUnit to create and manage a driver for you.
59  *
60  * Return: a pointer to a struct device which will be cleaned up when the test
61  * exits, or an error pointer if the device could not be allocated or registered.
62  */
63 struct device *kunit_device_register_with_driver(struct kunit *test,
64 						 const char *name,
65 						 const struct device_driver *drv);
66 
67 /**
68  * kunit_device_unregister() - Unregister a KUnit-managed device
69  * @test: The test context object which created the device
70  * @dev: The device.
71  *
72  * Unregisters and destroys a struct device which was created with
73  * kunit_device_register or kunit_device_register_with_driver. If KUnit created
74  * a driver, cleans it up as well.
75  */
76 void kunit_device_unregister(struct kunit *test, struct device *dev);
77 
78 #endif
79 
80 #endif
81