xref: /linux/lib/kunit/device.c (revision b8e85e6f3a09fc56b0ff574887798962ef8a8f80)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * KUnit-managed device implementation
4  *
5  * Implementation of struct kunit_device helpers for fake devices whose
6  * lifecycle is managed by KUnit.
7  *
8  * Copyright (C) 2023, Google LLC.
9  * Author: David Gow <davidgow@google.com>
10  */
11 
12 #include <linux/device.h>
13 
14 #include <kunit/test.h>
15 #include <kunit/device.h>
16 #include <kunit/resource.h>
17 
18 #include "device-impl.h"
19 
20 /* Wrappers for use with kunit_add_action() */
21 KUNIT_DEFINE_ACTION_WRAPPER(device_unregister_wrapper, device_unregister, struct device *);
22 KUNIT_DEFINE_ACTION_WRAPPER(driver_unregister_wrapper, driver_unregister, struct device_driver *);
23 
24 /* The root device for the KUnit bus, parent of all kunit_devices. */
25 static struct device *kunit_bus_device;
26 
27 /* A device owned by a KUnit test. */
28 struct kunit_device {
29 	struct device dev;
30 	/* The KUnit test which owns this device. */
31 	struct kunit *owner;
32 	/* If the driver is managed by KUnit and unique to this device. */
33 	const struct device_driver *driver;
34 };
35 
36 #define to_kunit_device(d) container_of_const(d, struct kunit_device, dev)
37 
38 static struct bus_type kunit_bus_type = {
39 	.name		= "kunit",
40 };
41 
42 /* Register the 'kunit_bus' used for fake devices. */
43 int kunit_bus_init(void)
44 {
45 	int error;
46 
47 	kunit_bus_device = root_device_register("kunit");
48 	if (IS_ERR(kunit_bus_device))
49 		return PTR_ERR(kunit_bus_device);
50 
51 	error = bus_register(&kunit_bus_type);
52 	if (error)
53 		bus_unregister(&kunit_bus_type);
54 	return error;
55 }
56 
57 /* Unregister the 'kunit_bus' in case the KUnit module is unloaded. */
58 void kunit_bus_shutdown(void)
59 {
60 	/* Make sure the bus exists before we unregister it. */
61 	if (IS_ERR_OR_NULL(kunit_bus_device))
62 		return;
63 
64 	bus_unregister(&kunit_bus_type);
65 
66 	root_device_unregister(kunit_bus_device);
67 
68 	kunit_bus_device = NULL;
69 }
70 
71 /* Release a 'fake' KUnit device. */
72 static void kunit_device_release(struct device *d)
73 {
74 	kfree(to_kunit_device(d));
75 }
76 
77 /*
78  * Create and register a KUnit-managed struct device_driver on the kunit_bus.
79  * Returns an error pointer on failure.
80  */
81 struct device_driver *kunit_driver_create(struct kunit *test, const char *name)
82 {
83 	struct device_driver *driver;
84 	int err = -ENOMEM;
85 
86 	driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL);
87 
88 	if (!driver)
89 		return ERR_PTR(err);
90 
91 	driver->name = name;
92 	driver->bus = &kunit_bus_type;
93 	driver->owner = THIS_MODULE;
94 
95 	err = driver_register(driver);
96 	if (err) {
97 		kunit_kfree(test, driver);
98 		return ERR_PTR(err);
99 	}
100 
101 	kunit_add_action(test, driver_unregister_wrapper, driver);
102 	return driver;
103 }
104 EXPORT_SYMBOL_GPL(kunit_driver_create);
105 
106 /* Helper which creates a kunit_device, attaches it to the kunit_bus*/
107 static struct kunit_device *kunit_device_register_internal(struct kunit *test,
108 							   const char *name,
109 							   const struct device_driver *drv)
110 {
111 	struct kunit_device *kunit_dev;
112 	int err = -ENOMEM;
113 
114 	kunit_dev = kzalloc(sizeof(*kunit_dev), GFP_KERNEL);
115 	if (!kunit_dev)
116 		return ERR_PTR(err);
117 
118 	kunit_dev->owner = test;
119 
120 	err = dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name);
121 	if (err) {
122 		kfree(kunit_dev);
123 		return ERR_PTR(err);
124 	}
125 
126 	kunit_dev->dev.release = kunit_device_release;
127 	kunit_dev->dev.bus = &kunit_bus_type;
128 	kunit_dev->dev.parent = kunit_bus_device;
129 
130 	err = device_register(&kunit_dev->dev);
131 	if (err) {
132 		put_device(&kunit_dev->dev);
133 		return ERR_PTR(err);
134 	}
135 
136 	kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev);
137 
138 	return kunit_dev;
139 }
140 
141 /*
142  * Create and register a new KUnit-managed device, using the user-supplied device_driver.
143  * On failure, returns an error pointer.
144  */
145 struct device *kunit_device_register_with_driver(struct kunit *test,
146 						 const char *name,
147 						 const struct device_driver *drv)
148 {
149 	struct kunit_device *kunit_dev = kunit_device_register_internal(test, name, drv);
150 
151 	if (IS_ERR_OR_NULL(kunit_dev))
152 		return ERR_CAST(kunit_dev);
153 
154 	return &kunit_dev->dev;
155 }
156 EXPORT_SYMBOL_GPL(kunit_device_register_with_driver);
157 
158 /*
159  * Create and register a new KUnit-managed device, including a matching device_driver.
160  * On failure, returns an error pointer.
161  */
162 struct device *kunit_device_register(struct kunit *test, const char *name)
163 {
164 	struct device_driver *drv;
165 	struct kunit_device *dev;
166 
167 	drv = kunit_driver_create(test, name);
168 	if (IS_ERR(drv))
169 		return ERR_CAST(drv);
170 
171 	dev = kunit_device_register_internal(test, name, drv);
172 	if (IS_ERR(dev)) {
173 		kunit_release_action(test, driver_unregister_wrapper, (void *)drv);
174 		return ERR_CAST(dev);
175 	}
176 
177 	/* Request the driver be freed. */
178 	dev->driver = drv;
179 
180 
181 	return &dev->dev;
182 }
183 EXPORT_SYMBOL_GPL(kunit_device_register);
184 
185 /* Unregisters a KUnit-managed device early (including the driver, if automatically created). */
186 void kunit_device_unregister(struct kunit *test, struct device *dev)
187 {
188 	const struct device_driver *driver = to_kunit_device(dev)->driver;
189 
190 	kunit_release_action(test, device_unregister_wrapper, dev);
191 	if (driver)
192 		kunit_release_action(test, driver_unregister_wrapper, (void *)driver);
193 }
194 EXPORT_SYMBOL_GPL(kunit_device_unregister);
195 
196