xref: /linux/drivers/base/faux.c (revision 1a262c768f5b5a1ebbdec8cfa588f75d3a825a8d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2025 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
4  * Copyright (c) 2025 The Linux Foundation
5  *
6  * A "simple" faux bus that allows devices to be created and added
7  * automatically to it.  This is to be used whenever you need to create a
8  * device that is not associated with any "real" system resources, and do
9  * not want to have to deal with a bus/driver binding logic.  It is
10  * intended to be very simple, with only a create and a destroy function
11  * available.
12  */
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/container_of.h>
18 #include <linux/device/faux.h>
19 #include "base.h"
20 
21 /*
22  * Internal wrapper structure so we can hold a pointer to the
23  * faux_device_ops for this device.
24  */
25 struct faux_object {
26 	struct faux_device faux_dev;
27 	const struct faux_device_ops *faux_ops;
28 	const struct attribute_group **groups;
29 };
30 #define to_faux_object(dev) container_of_const(dev, struct faux_object, faux_dev.dev)
31 
32 static struct device *faux_bus_root;
33 
34 static int faux_match(struct device *dev, const struct device_driver *drv)
35 {
36 	/* Match always succeeds, we only have one driver */
37 	return 1;
38 }
39 
40 static int faux_probe(struct device *dev)
41 {
42 	struct faux_object *faux_obj = to_faux_object(dev);
43 	struct faux_device *faux_dev = &faux_obj->faux_dev;
44 	const struct faux_device_ops *faux_ops = faux_obj->faux_ops;
45 	int ret;
46 
47 	if (faux_ops && faux_ops->probe) {
48 		ret = faux_ops->probe(faux_dev);
49 		if (ret)
50 			return ret;
51 	}
52 
53 	/*
54 	 * Add groups after the probe succeeds to ensure resources are
55 	 * initialized correctly
56 	 */
57 	ret = device_add_groups(dev, faux_obj->groups);
58 	if (ret && faux_ops && faux_ops->remove)
59 		faux_ops->remove(faux_dev);
60 
61 	return ret;
62 }
63 
64 static void faux_remove(struct device *dev)
65 {
66 	struct faux_object *faux_obj = to_faux_object(dev);
67 	struct faux_device *faux_dev = &faux_obj->faux_dev;
68 	const struct faux_device_ops *faux_ops = faux_obj->faux_ops;
69 
70 	device_remove_groups(dev, faux_obj->groups);
71 
72 	if (faux_ops && faux_ops->remove)
73 		faux_ops->remove(faux_dev);
74 }
75 
76 static const struct bus_type faux_bus_type = {
77 	.name		= "faux",
78 	.match		= faux_match,
79 	.probe		= faux_probe,
80 	.remove		= faux_remove,
81 };
82 
83 static struct device_driver faux_driver = {
84 	.name		= "faux_driver",
85 	.bus		= &faux_bus_type,
86 	.probe_type	= PROBE_FORCE_SYNCHRONOUS,
87 	.suppress_bind_attrs = true,
88 };
89 
90 static void faux_device_release(struct device *dev)
91 {
92 	struct faux_object *faux_obj = to_faux_object(dev);
93 
94 	kfree(faux_obj);
95 }
96 
97 /**
98  * faux_device_create_with_groups - Create and register with the driver
99  *		core a faux device and populate the device with an initial
100  *		set of sysfs attributes.
101  * @name:	The name of the device we are adding, must be unique for
102  *		all faux devices.
103  * @parent:	Pointer to a potential parent struct device.  If set to
104  *		NULL, the device will be created in the "root" of the faux
105  *		device tree in sysfs.
106  * @faux_ops:	struct faux_device_ops that the new device will call back
107  *		into, can be NULL.
108  * @groups:	The set of sysfs attributes that will be created for this
109  *		device when it is registered with the driver core.
110  *
111  * Create a new faux device and register it in the driver core properly.
112  * If present, callbacks in @faux_ops will be called with the device that
113  * for the caller to do something with at the proper time given the
114  * device's lifecycle.
115  *
116  * Note, when this function is called, the functions specified in struct
117  * faux_ops can be called before the function returns, so be prepared for
118  * everything to be properly initialized before that point in time.  If the
119  * probe callback (if one is present) does NOT succeed, the creation of the
120  * device will fail and NULL will be returned.
121  *
122  * Return:
123  * * NULL if an error happened with creating the device
124  * * pointer to a valid struct faux_device that is registered with sysfs
125  */
126 struct faux_device *faux_device_create_with_groups(const char *name,
127 						   struct device *parent,
128 						   const struct faux_device_ops *faux_ops,
129 						   const struct attribute_group **groups)
130 {
131 	struct faux_object *faux_obj;
132 	struct faux_device *faux_dev;
133 	struct device *dev;
134 	int ret;
135 
136 	if (!faux_bus_root)
137 		return NULL;
138 
139 	faux_obj = kzalloc_obj(*faux_obj);
140 	if (!faux_obj)
141 		return NULL;
142 
143 	/* Save off the callbacks and groups so we can use them in the future */
144 	faux_obj->faux_ops = faux_ops;
145 	faux_obj->groups = groups;
146 
147 	/* Initialize the device portion and register it with the driver core */
148 	faux_dev = &faux_obj->faux_dev;
149 	dev = &faux_dev->dev;
150 
151 	device_initialize(dev);
152 	dev->release = faux_device_release;
153 	if (parent)
154 		dev->parent = parent;
155 	else
156 		dev->parent = faux_bus_root;
157 	dev->bus = &faux_bus_type;
158 	dev_set_name(dev, "%s", name);
159 	device_set_pm_not_required(dev);
160 
161 	ret = device_add(dev);
162 	if (ret) {
163 		pr_err("%s: device_add for faux device '%s' failed with %d\n",
164 		       __func__, name, ret);
165 		put_device(dev);
166 		return NULL;
167 	}
168 
169 	/*
170 	 * Verify that we did bind the driver to the device (i.e. probe worked),
171 	 * if not, let's fail the creation as trying to guess if probe was
172 	 * successful is almost impossible to determine by the caller.
173 	 */
174 	if (!dev->driver) {
175 		dev_dbg(dev, "probe did not succeed, tearing down the device\n");
176 		faux_device_destroy(faux_dev);
177 		faux_dev = NULL;
178 	}
179 
180 	return faux_dev;
181 }
182 EXPORT_SYMBOL_GPL(faux_device_create_with_groups);
183 
184 /**
185  * faux_device_create - create and register with the driver core a faux device
186  * @name:	The name of the device we are adding, must be unique for all
187  *		faux devices.
188  * @parent:	Pointer to a potential parent struct device.  If set to
189  *		NULL, the device will be created in the "root" of the faux
190  *		device tree in sysfs.
191  * @faux_ops:	struct faux_device_ops that the new device will call back
192  *		into, can be NULL.
193  *
194  * Create a new faux device and register it in the driver core properly.
195  * If present, callbacks in @faux_ops will be called with the device that
196  * for the caller to do something with at the proper time given the
197  * device's lifecycle.
198  *
199  * Note, when this function is called, the functions specified in struct
200  * faux_ops can be called before the function returns, so be prepared for
201  * everything to be properly initialized before that point in time.
202  *
203  * Return:
204  * * NULL if an error happened with creating the device
205  * * pointer to a valid struct faux_device that is registered with sysfs
206  */
207 struct faux_device *faux_device_create(const char *name,
208 				       struct device *parent,
209 				       const struct faux_device_ops *faux_ops)
210 {
211 	return faux_device_create_with_groups(name, parent, faux_ops, NULL);
212 }
213 EXPORT_SYMBOL_GPL(faux_device_create);
214 
215 /**
216  * faux_device_destroy - destroy a faux device
217  * @faux_dev:	faux device to destroy
218  *
219  * Unregisters and cleans up a device that was created with a call to
220  * faux_device_create()
221  */
222 void faux_device_destroy(struct faux_device *faux_dev)
223 {
224 	struct device *dev = &faux_dev->dev;
225 
226 	if (!faux_dev)
227 		return;
228 
229 	device_del(dev);
230 
231 	/* The final put_device() will clean up the memory we allocated for this device. */
232 	put_device(dev);
233 }
234 EXPORT_SYMBOL_GPL(faux_device_destroy);
235 
236 int __init faux_bus_init(void)
237 {
238 	struct device *root;
239 	int ret;
240 
241 	root = root_device_register("faux");
242 	if (IS_ERR(root))
243 		return PTR_ERR(root);
244 
245 	ret = bus_register(&faux_bus_type);
246 	if (ret)
247 		goto err_deregister_root;
248 
249 	ret = driver_register(&faux_driver);
250 	if (ret)
251 		goto err_deregister_bus;
252 
253 	faux_bus_root = root;
254 
255 	return 0;
256 
257 err_deregister_bus:
258 	bus_unregister(&faux_bus_type);
259 err_deregister_root:
260 	root_device_unregister(root);
261 
262 	return ret;
263 }
264