xref: /linux/drivers/base/auxiliary.c (revision 2e31b16101834bdc0b720967845d6a0a309cf27b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2019-2020 Intel Corporation
4  *
5  * Please see Documentation/driver-api/auxiliary_bus.rst for more information.
6  */
7 
8 #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
9 
10 #include <linux/device.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/pm_domain.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/string.h>
17 #include <linux/auxiliary_bus.h>
18 #include "base.h"
19 
20 /**
21  * DOC: PURPOSE
22  *
23  * In some subsystems, the functionality of the core device (PCI/ACPI/other) is
24  * too complex for a single device to be managed by a monolithic driver (e.g.
25  * Sound Open Firmware), multiple devices might implement a common intersection
26  * of functionality (e.g. NICs + RDMA), or a driver may want to export an
27  * interface for another subsystem to drive (e.g. SIOV Physical Function export
28  * Virtual Function management).  A split of the functionality into child-
29  * devices representing sub-domains of functionality makes it possible to
30  * compartmentalize, layer, and distribute domain-specific concerns via a Linux
31  * device-driver model.
32  *
33  * An example for this kind of requirement is the audio subsystem where a
34  * single IP is handling multiple entities such as HDMI, Soundwire, local
35  * devices such as mics/speakers etc. The split for the core's functionality
36  * can be arbitrary or be defined by the DSP firmware topology and include
37  * hooks for test/debug. This allows for the audio core device to be minimal
38  * and focused on hardware-specific control and communication.
39  *
40  * Each auxiliary_device represents a part of its parent functionality. The
41  * generic behavior can be extended and specialized as needed by encapsulating
42  * an auxiliary_device within other domain-specific structures and the use of
43  * .ops callbacks. Devices on the auxiliary bus do not share any structures and
44  * the use of a communication channel with the parent is domain-specific.
45  *
46  * Note that ops are intended as a way to augment instance behavior within a
47  * class of auxiliary devices, it is not the mechanism for exporting common
48  * infrastructure from the parent. Consider EXPORT_SYMBOL_NS() to convey
49  * infrastructure from the parent module to the auxiliary module(s).
50  */
51 
52 /**
53  * DOC: USAGE
54  *
55  * The auxiliary bus is to be used when a driver and one or more kernel
56  * modules, who share a common header file with the driver, need a mechanism to
57  * connect and provide access to a shared object allocated by the
58  * auxiliary_device's registering driver.  The registering driver for the
59  * auxiliary_device(s) and the kernel module(s) registering auxiliary_drivers
60  * can be from the same subsystem, or from multiple subsystems.
61  *
62  * The emphasis here is on a common generic interface that keeps subsystem
63  * customization out of the bus infrastructure.
64  *
65  * One example is a PCI network device that is RDMA-capable and exports a child
66  * device to be driven by an auxiliary_driver in the RDMA subsystem.  The PCI
67  * driver allocates and registers an auxiliary_device for each physical
68  * function on the NIC.  The RDMA driver registers an auxiliary_driver that
69  * claims each of these auxiliary_devices.  This conveys data/ops published by
70  * the parent PCI device/driver to the RDMA auxiliary_driver.
71  *
72  * Another use case is for the PCI device to be split out into multiple sub
73  * functions.  For each sub function an auxiliary_device is created.  A PCI sub
74  * function driver binds to such devices that creates its own one or more class
75  * devices.  A PCI sub function auxiliary device is likely to be contained in a
76  * struct with additional attributes such as user defined sub function number
77  * and optional attributes such as resources and a link to the parent device.
78  * These attributes could be used by systemd/udev; and hence should be
79  * initialized before a driver binds to an auxiliary_device.
80  *
81  * A key requirement for utilizing the auxiliary bus is that there is no
82  * dependency on a physical bus, device, register accesses or regmap support.
83  * These individual devices split from the core cannot live on the platform bus
84  * as they are not physical devices that are controlled by DT/ACPI.  The same
85  * argument applies for not using MFD in this scenario as MFD relies on
86  * individual function devices being physical devices.
87  */
88 
89 /**
90  * DOC: EXAMPLE
91  *
92  * Auxiliary devices are created and registered by a subsystem-level core
93  * device that needs to break up its functionality into smaller fragments. One
94  * way to extend the scope of an auxiliary_device is to encapsulate it within a
95  * domain-specific structure defined by the parent device. This structure
96  * contains the auxiliary_device and any associated shared data/callbacks
97  * needed to establish the connection with the parent.
98  *
99  * An example is:
100  *
101  * .. code-block:: c
102  *
103  *         struct foo {
104  *		struct auxiliary_device auxdev;
105  *		void (*connect)(struct auxiliary_device *auxdev);
106  *		void (*disconnect)(struct auxiliary_device *auxdev);
107  *		void *data;
108  *        };
109  *
110  * The parent device then registers the auxiliary_device by calling
111  * auxiliary_device_init(), and then auxiliary_device_add(), with the pointer
112  * to the auxdev member of the above structure. The parent provides a name for
113  * the auxiliary_device that, combined with the parent's KBUILD_MODNAME,
114  * creates a match_name that is be used for matching and binding with a driver.
115  *
116  * Whenever an auxiliary_driver is registered, based on the match_name, the
117  * auxiliary_driver's probe() is invoked for the matching devices.  The
118  * auxiliary_driver can also be encapsulated inside custom drivers that make
119  * the core device's functionality extensible by adding additional
120  * domain-specific ops as follows:
121  *
122  * .. code-block:: c
123  *
124  *	struct my_ops {
125  *		void (*send)(struct auxiliary_device *auxdev);
126  *		void (*receive)(struct auxiliary_device *auxdev);
127  *	};
128  *
129  *
130  *	struct my_driver {
131  *		struct auxiliary_driver auxiliary_drv;
132  *		const struct my_ops ops;
133  *	};
134  *
135  * An example of this type of usage is:
136  *
137  * .. code-block:: c
138  *
139  *	const struct auxiliary_device_id my_auxiliary_id_table[] = {
140  *		{ .name = "foo_mod.foo_dev" },
141  *		{ },
142  *	};
143  *
144  *	const struct my_ops my_custom_ops = {
145  *		.send = my_tx,
146  *		.receive = my_rx,
147  *	};
148  *
149  *	const struct my_driver my_drv = {
150  *		.auxiliary_drv = {
151  *			.name = "myauxiliarydrv",
152  *			.id_table = my_auxiliary_id_table,
153  *			.probe = my_probe,
154  *			.remove = my_remove,
155  *			.shutdown = my_shutdown,
156  *		},
157  *		.ops = my_custom_ops,
158  *	};
159  *
160  * Please note that such custom ops approach is valid, but it is hard to implement
161  * it right without global locks per-device to protect from auxiliary_drv removal
162  * during call to that ops. In addition, this implementation lacks proper module
163  * dependency, which causes to load/unload races between auxiliary parent and devices
164  * modules.
165  *
166  * The most easiest way to provide these ops reliably without needing to
167  * have a lock is to EXPORT_SYMBOL*() them and rely on already existing
168  * modules infrastructure for validity and correct dependencies chains.
169  */
170 
auxiliary_match_id(const struct auxiliary_device_id * id,const struct auxiliary_device * auxdev)171 static const struct auxiliary_device_id *auxiliary_match_id(const struct auxiliary_device_id *id,
172 							    const struct auxiliary_device *auxdev)
173 {
174 	const char *auxdev_name = dev_name(&auxdev->dev);
175 	const char *p = strrchr(auxdev_name, '.');
176 	int match_size;
177 
178 	if (!p)
179 		return NULL;
180 	match_size = p - auxdev_name;
181 
182 	for (; id->name[0]; id++) {
183 		/* use dev_name(&auxdev->dev) prefix before last '.' char to match to */
184 		if (strlen(id->name) == match_size &&
185 		    !strncmp(auxdev_name, id->name, match_size))
186 			return id;
187 	}
188 	return NULL;
189 }
190 
auxiliary_match(struct device * dev,const struct device_driver * drv)191 static int auxiliary_match(struct device *dev, const struct device_driver *drv)
192 {
193 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
194 	const struct auxiliary_driver *auxdrv = to_auxiliary_drv(drv);
195 
196 	return !!auxiliary_match_id(auxdrv->id_table, auxdev);
197 }
198 
auxiliary_uevent(const struct device * dev,struct kobj_uevent_env * env)199 static int auxiliary_uevent(const struct device *dev, struct kobj_uevent_env *env)
200 {
201 	const char *name, *p;
202 
203 	name = dev_name(dev);
204 	p = strrchr(name, '.');
205 
206 	return add_uevent_var(env, "MODALIAS=%s%.*s", AUXILIARY_MODULE_PREFIX,
207 			      (int)(p - name), name);
208 }
209 
auxiliary_bus_probe(struct device * dev)210 static int auxiliary_bus_probe(struct device *dev)
211 {
212 	const struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver);
213 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
214 	int ret;
215 
216 	ret = dev_pm_domain_attach(dev, PD_FLAG_ATTACH_POWER_ON |
217 					PD_FLAG_DETACH_POWER_OFF);
218 	if (ret) {
219 		dev_warn(dev, "Failed to attach to PM Domain : %d\n", ret);
220 		return ret;
221 	}
222 
223 	return auxdrv->probe(auxdev, auxiliary_match_id(auxdrv->id_table, auxdev));
224 }
225 
auxiliary_bus_remove(struct device * dev)226 static void auxiliary_bus_remove(struct device *dev)
227 {
228 	const struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver);
229 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
230 
231 	if (auxdrv->remove)
232 		auxdrv->remove(auxdev);
233 }
234 
auxiliary_bus_shutdown(struct device * dev)235 static void auxiliary_bus_shutdown(struct device *dev)
236 {
237 	const struct auxiliary_driver *auxdrv = NULL;
238 	struct auxiliary_device *auxdev;
239 
240 	if (dev->driver) {
241 		auxdrv = to_auxiliary_drv(dev->driver);
242 		auxdev = to_auxiliary_dev(dev);
243 	}
244 
245 	if (auxdrv && auxdrv->shutdown)
246 		auxdrv->shutdown(auxdev);
247 }
248 
249 static const struct bus_type auxiliary_bus_type = {
250 	.name = "auxiliary",
251 	.probe = auxiliary_bus_probe,
252 	.remove = auxiliary_bus_remove,
253 	.shutdown = auxiliary_bus_shutdown,
254 	.match = auxiliary_match,
255 	.uevent = auxiliary_uevent,
256 };
257 
258 /**
259  * auxiliary_device_init - check auxiliary_device and initialize
260  * @auxdev: auxiliary device struct
261  *
262  * This is the second step in the three-step process to register an
263  * auxiliary_device.
264  *
265  * When this function returns an error code, then the device_initialize will
266  * *not* have been performed, and the caller will be responsible to free any
267  * memory allocated for the auxiliary_device in the error path directly.
268  *
269  * It returns 0 on success.  On success, the device_initialize has been
270  * performed.  After this point any error unwinding will need to include a call
271  * to auxiliary_device_uninit().  In this post-initialize error scenario, a call
272  * to the device's .release callback will be triggered, and all memory clean-up
273  * is expected to be handled there.
274  */
auxiliary_device_init(struct auxiliary_device * auxdev)275 int auxiliary_device_init(struct auxiliary_device *auxdev)
276 {
277 	struct device *dev = &auxdev->dev;
278 
279 	if (!dev->parent) {
280 		pr_err("auxiliary_device has a NULL dev->parent\n");
281 		return -EINVAL;
282 	}
283 
284 	if (!auxdev->name) {
285 		pr_err("auxiliary_device has a NULL name\n");
286 		return -EINVAL;
287 	}
288 
289 	dev->bus = &auxiliary_bus_type;
290 	device_initialize(&auxdev->dev);
291 	mutex_init(&auxdev->sysfs.lock);
292 	return 0;
293 }
294 EXPORT_SYMBOL_GPL(auxiliary_device_init);
295 
296 /**
297  * __auxiliary_device_add - add an auxiliary bus device
298  * @auxdev: auxiliary bus device to add to the bus
299  * @modname: name of the parent device's driver module
300  *
301  * This is the third step in the three-step process to register an
302  * auxiliary_device.
303  *
304  * This function must be called after a successful call to
305  * auxiliary_device_init(), which will perform the device_initialize.  This
306  * means that if this returns an error code, then a call to
307  * auxiliary_device_uninit() must be performed so that the .release callback
308  * will be triggered to free the memory associated with the auxiliary_device.
309  *
310  * The expectation is that users will call the "auxiliary_device_add" macro so
311  * that the caller's KBUILD_MODNAME is automatically inserted for the modname
312  * parameter.  Only if a user requires a custom name would this version be
313  * called directly.
314  */
__auxiliary_device_add(struct auxiliary_device * auxdev,const char * modname)315 int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname)
316 {
317 	struct device *dev = &auxdev->dev;
318 	int ret;
319 
320 	if (!modname) {
321 		dev_err(dev, "auxiliary device modname is NULL\n");
322 		return -EINVAL;
323 	}
324 
325 	ret = dev_set_name(dev, "%s.%s.%d", modname, auxdev->name, auxdev->id);
326 	if (ret) {
327 		dev_err(dev, "auxiliary device dev_set_name failed: %d\n", ret);
328 		return ret;
329 	}
330 
331 	ret = device_add(dev);
332 	if (ret)
333 		dev_err(dev, "adding auxiliary device failed!: %d\n", ret);
334 
335 	return ret;
336 }
337 EXPORT_SYMBOL_GPL(__auxiliary_device_add);
338 
339 /**
340  * __auxiliary_driver_register - register a driver for auxiliary bus devices
341  * @auxdrv: auxiliary_driver structure
342  * @owner: owning module/driver
343  * @modname: KBUILD_MODNAME for parent driver
344  *
345  * The expectation is that users will call the "auxiliary_driver_register"
346  * macro so that the caller's KBUILD_MODNAME is automatically inserted for the
347  * modname parameter.  Only if a user requires a custom name would this version
348  * be called directly.
349  */
__auxiliary_driver_register(struct auxiliary_driver * auxdrv,struct module * owner,const char * modname)350 int __auxiliary_driver_register(struct auxiliary_driver *auxdrv,
351 				struct module *owner, const char *modname)
352 {
353 	int ret;
354 
355 	if (WARN_ON(!auxdrv->probe) || WARN_ON(!auxdrv->id_table))
356 		return -EINVAL;
357 
358 	if (auxdrv->name)
359 		auxdrv->driver.name = kasprintf(GFP_KERNEL, "%s.%s", modname,
360 						auxdrv->name);
361 	else
362 		auxdrv->driver.name = kasprintf(GFP_KERNEL, "%s", modname);
363 	if (!auxdrv->driver.name)
364 		return -ENOMEM;
365 
366 	auxdrv->driver.owner = owner;
367 	auxdrv->driver.bus = &auxiliary_bus_type;
368 	auxdrv->driver.mod_name = modname;
369 
370 	ret = driver_register(&auxdrv->driver);
371 	if (ret)
372 		kfree(auxdrv->driver.name);
373 
374 	return ret;
375 }
376 EXPORT_SYMBOL_GPL(__auxiliary_driver_register);
377 
378 /**
379  * auxiliary_driver_unregister - unregister a driver
380  * @auxdrv: auxiliary_driver structure
381  */
auxiliary_driver_unregister(struct auxiliary_driver * auxdrv)382 void auxiliary_driver_unregister(struct auxiliary_driver *auxdrv)
383 {
384 	driver_unregister(&auxdrv->driver);
385 	kfree(auxdrv->driver.name);
386 }
387 EXPORT_SYMBOL_GPL(auxiliary_driver_unregister);
388 
auxiliary_device_release(struct device * dev)389 static void auxiliary_device_release(struct device *dev)
390 {
391 	struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
392 
393 	of_node_put(dev->of_node);
394 	kfree(auxdev);
395 }
396 
397 /**
398  * auxiliary_device_create - create a device on the auxiliary bus
399  * @dev: parent device
400  * @modname: module name used to create the auxiliary driver name.
401  * @devname: auxiliary bus device name
402  * @platform_data: auxiliary bus device platform data
403  * @id: auxiliary bus device id
404  *
405  * Helper to create an auxiliary bus device.
406  * The device created matches driver 'modname.devname' on the auxiliary bus.
407  */
auxiliary_device_create(struct device * dev,const char * modname,const char * devname,void * platform_data,int id)408 struct auxiliary_device *auxiliary_device_create(struct device *dev,
409 						 const char *modname,
410 						 const char *devname,
411 						 void *platform_data,
412 						 int id)
413 {
414 	struct auxiliary_device *auxdev;
415 	int ret;
416 
417 	auxdev = kzalloc_obj(*auxdev);
418 	if (!auxdev)
419 		return NULL;
420 
421 	auxdev->id = id;
422 	auxdev->name = devname;
423 	auxdev->dev.parent = dev;
424 	auxdev->dev.platform_data = platform_data;
425 	auxdev->dev.release = auxiliary_device_release;
426 	device_set_of_node_from_dev(&auxdev->dev, dev);
427 
428 	ret = auxiliary_device_init(auxdev);
429 	if (ret) {
430 		of_node_put(auxdev->dev.of_node);
431 		kfree(auxdev);
432 		return NULL;
433 	}
434 
435 	ret = __auxiliary_device_add(auxdev, modname);
436 	if (ret) {
437 		/*
438 		 * It may look odd but auxdev should not be freed here.
439 		 * auxiliary_device_uninit() calls device_put() which call
440 		 * the device release function, freeing auxdev.
441 		 */
442 		auxiliary_device_uninit(auxdev);
443 		return NULL;
444 	}
445 
446 	return auxdev;
447 }
448 EXPORT_SYMBOL_GPL(auxiliary_device_create);
449 
450 /**
451  * auxiliary_device_destroy - remove an auxiliary device
452  * @auxdev: pointer to the auxdev to be removed
453  *
454  * Helper to remove an auxiliary device created with
455  * auxiliary_device_create()
456  */
auxiliary_device_destroy(void * auxdev)457 void auxiliary_device_destroy(void *auxdev)
458 {
459 	struct auxiliary_device *_auxdev = auxdev;
460 
461 	auxiliary_device_delete(_auxdev);
462 	auxiliary_device_uninit(_auxdev);
463 }
464 EXPORT_SYMBOL_GPL(auxiliary_device_destroy);
465 
466 /**
467  * __devm_auxiliary_device_create - create a managed device on the auxiliary bus
468  * @dev: parent device
469  * @modname: module name used to create the auxiliary driver name.
470  * @devname: auxiliary bus device name
471  * @platform_data: auxiliary bus device platform data
472  * @id: auxiliary bus device id
473  *
474  * Device managed helper to create an auxiliary bus device.
475  * The device created matches driver 'modname.devname' on the auxiliary bus.
476  */
__devm_auxiliary_device_create(struct device * dev,const char * modname,const char * devname,void * platform_data,int id)477 struct auxiliary_device *__devm_auxiliary_device_create(struct device *dev,
478 							const char *modname,
479 							const char *devname,
480 							void *platform_data,
481 							int id)
482 {
483 	struct auxiliary_device *auxdev;
484 	int ret;
485 
486 	auxdev = auxiliary_device_create(dev, modname, devname, platform_data, id);
487 	if (!auxdev)
488 		return NULL;
489 
490 	ret = devm_add_action_or_reset(dev, auxiliary_device_destroy,
491 				       auxdev);
492 	if (ret)
493 		return NULL;
494 
495 	return auxdev;
496 }
497 EXPORT_SYMBOL_GPL(__devm_auxiliary_device_create);
498 
499 /**
500  * dev_is_auxiliary - check if the device is an auxiliary one
501  * @dev: device to check
502  */
dev_is_auxiliary(struct device * dev)503 bool dev_is_auxiliary(struct device *dev)
504 {
505 	return dev->bus == &auxiliary_bus_type;
506 }
507 EXPORT_SYMBOL_GPL(dev_is_auxiliary);
508 
auxiliary_bus_init(void)509 void __init auxiliary_bus_init(void)
510 {
511 	WARN_ON(bus_register(&auxiliary_bus_type));
512 }
513