1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit-managed device implementation
5 * Implementation of struct kunit_device helpers for fake devices whose
6 * lifecycle is managed by KUnit.
8 * Copyright (C) 2023, Google LLC.
9 * Author: David Gow <davidgow@google.com>
12 #include <linux/device.h>
14 #include <kunit/test.h>
15 #include <kunit/device.h>
16 #include <kunit/resource.h>
18 #include "device-impl.h"
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 *);
24 /* The root device for the KUnit bus, parent of all kunit_devices. */
25 static struct device *kunit_bus_device;
27 /* A device owned by a KUnit test. */
30 /* The KUnit test which owns this device. */
32 /* If the driver is managed by KUnit and unique to this device. */
33 const struct device_driver *driver;
36 #define to_kunit_device(d) container_of_const(d, struct kunit_device, dev)
38 static struct bus_type kunit_bus_type = {
42 /* Register the 'kunit_bus' used for fake devices. */
43 int kunit_bus_init(void)
47 kunit_bus_device = root_device_register("kunit");
48 if (IS_ERR(kunit_bus_device))
49 return PTR_ERR(kunit_bus_device);
51 error = bus_register(&kunit_bus_type);
53 bus_unregister(&kunit_bus_type);
57 /* Release a 'fake' KUnit device. */
58 static void kunit_device_release(struct device *d)
60 kfree(to_kunit_device(d));
64 * Create and register a KUnit-managed struct device_driver on the kunit_bus.
65 * Returns an error pointer on failure.
67 struct device_driver *kunit_driver_create(struct kunit *test, const char *name)
69 struct device_driver *driver;
72 driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL);
78 driver->bus = &kunit_bus_type;
79 driver->owner = THIS_MODULE;
81 err = driver_register(driver);
83 kunit_kfree(test, driver);
87 kunit_add_action(test, driver_unregister_wrapper, driver);
90 EXPORT_SYMBOL_GPL(kunit_driver_create);
92 /* Helper which creates a kunit_device, attaches it to the kunit_bus*/
93 static struct kunit_device *kunit_device_register_internal(struct kunit *test,
95 const struct device_driver *drv)
97 struct kunit_device *kunit_dev;
100 kunit_dev = kzalloc(sizeof(*kunit_dev), GFP_KERNEL);
104 kunit_dev->owner = test;
106 err = dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name);
112 kunit_dev->dev.release = kunit_device_release;
113 kunit_dev->dev.bus = &kunit_bus_type;
114 kunit_dev->dev.parent = kunit_bus_device;
116 err = device_register(&kunit_dev->dev);
118 put_device(&kunit_dev->dev);
122 kunit_add_action(test, device_unregister_wrapper, &kunit_dev->dev);
128 * Create and register a new KUnit-managed device, using the user-supplied device_driver.
129 * On failure, returns an error pointer.
131 struct device *kunit_device_register_with_driver(struct kunit *test,
133 const struct device_driver *drv)
135 struct kunit_device *kunit_dev = kunit_device_register_internal(test, name, drv);
137 if (IS_ERR_OR_NULL(kunit_dev))
138 return ERR_CAST(kunit_dev);
140 return &kunit_dev->dev;
142 EXPORT_SYMBOL_GPL(kunit_device_register_with_driver);
145 * Create and register a new KUnit-managed device, including a matching device_driver.
146 * On failure, returns an error pointer.
148 struct device *kunit_device_register(struct kunit *test, const char *name)
150 struct device_driver *drv;
151 struct kunit_device *dev;
153 drv = kunit_driver_create(test, name);
155 return ERR_CAST(drv);
157 dev = kunit_device_register_internal(test, name, drv);
159 kunit_release_action(test, driver_unregister_wrapper, (void *)drv);
160 return ERR_CAST(dev);
163 /* Request the driver be freed. */
169 EXPORT_SYMBOL_GPL(kunit_device_register);
171 /* Unregisters a KUnit-managed device early (including the driver, if automatically created). */
172 void kunit_device_unregister(struct kunit *test, struct device *dev)
174 const struct device_driver *driver = to_kunit_device(dev)->driver;
176 kunit_release_action(test, device_unregister_wrapper, dev);
178 kunit_release_action(test, driver_unregister_wrapper, (void *)driver);
180 EXPORT_SYMBOL_GPL(kunit_device_unregister);