1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2019-2020 Intel Corporation
5 * Please see Documentation/driver-api/auxiliary_bus.rst for more information.
8 #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
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>
19 static const struct auxiliary_device_id *auxiliary_match_id(const struct auxiliary_device_id *id,
20 const struct auxiliary_device *auxdev)
22 for (; id->name[0]; id++) {
23 const char *p = strrchr(dev_name(&auxdev->dev), '.');
28 match_size = p - dev_name(&auxdev->dev);
30 /* use dev_name(&auxdev->dev) prefix before last '.' char to match to */
31 if (strlen(id->name) == match_size &&
32 !strncmp(dev_name(&auxdev->dev), id->name, match_size))
38 static int auxiliary_match(struct device *dev, struct device_driver *drv)
40 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
41 struct auxiliary_driver *auxdrv = to_auxiliary_drv(drv);
43 return !!auxiliary_match_id(auxdrv->id_table, auxdev);
46 static int auxiliary_uevent(struct device *dev, struct kobj_uevent_env *env)
51 p = strrchr(name, '.');
53 return add_uevent_var(env, "MODALIAS=%s%.*s", AUXILIARY_MODULE_PREFIX,
54 (int)(p - name), name);
57 static const struct dev_pm_ops auxiliary_dev_pm_ops = {
58 SET_RUNTIME_PM_OPS(pm_generic_runtime_suspend, pm_generic_runtime_resume, NULL)
59 SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume)
62 static int auxiliary_bus_probe(struct device *dev)
64 struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver);
65 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
68 ret = dev_pm_domain_attach(dev, true);
70 dev_warn(dev, "Failed to attach to PM Domain : %d\n", ret);
74 ret = auxdrv->probe(auxdev, auxiliary_match_id(auxdrv->id_table, auxdev));
76 dev_pm_domain_detach(dev, true);
81 static int auxiliary_bus_remove(struct device *dev)
83 struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver);
84 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
87 auxdrv->remove(auxdev);
88 dev_pm_domain_detach(dev, true);
93 static void auxiliary_bus_shutdown(struct device *dev)
95 struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver);
96 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
99 auxdrv->shutdown(auxdev);
102 static struct bus_type auxiliary_bus_type = {
104 .probe = auxiliary_bus_probe,
105 .remove = auxiliary_bus_remove,
106 .shutdown = auxiliary_bus_shutdown,
107 .match = auxiliary_match,
108 .uevent = auxiliary_uevent,
109 .pm = &auxiliary_dev_pm_ops,
113 * auxiliary_device_init - check auxiliary_device and initialize
114 * @auxdev: auxiliary device struct
116 * This is the first step in the two-step process to register an
119 * When this function returns an error code, then the device_initialize will
120 * *not* have been performed, and the caller will be responsible to free any
121 * memory allocated for the auxiliary_device in the error path directly.
123 * It returns 0 on success. On success, the device_initialize has been
124 * performed. After this point any error unwinding will need to include a call
125 * to auxiliary_device_uninit(). In this post-initialize error scenario, a call
126 * to the device's .release callback will be triggered, and all memory clean-up
127 * is expected to be handled there.
129 int auxiliary_device_init(struct auxiliary_device *auxdev)
131 struct device *dev = &auxdev->dev;
134 pr_err("auxiliary_device has a NULL dev->parent\n");
139 pr_err("auxiliary_device has a NULL name\n");
143 dev->bus = &auxiliary_bus_type;
144 device_initialize(&auxdev->dev);
147 EXPORT_SYMBOL_GPL(auxiliary_device_init);
150 * __auxiliary_device_add - add an auxiliary bus device
151 * @auxdev: auxiliary bus device to add to the bus
152 * @modname: name of the parent device's driver module
154 * This is the second step in the two-step process to register an
157 * This function must be called after a successful call to
158 * auxiliary_device_init(), which will perform the device_initialize. This
159 * means that if this returns an error code, then a call to
160 * auxiliary_device_uninit() must be performed so that the .release callback
161 * will be triggered to free the memory associated with the auxiliary_device.
163 * The expectation is that users will call the "auxiliary_device_add" macro so
164 * that the caller's KBUILD_MODNAME is automatically inserted for the modname
165 * parameter. Only if a user requires a custom name would this version be
168 int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname)
170 struct device *dev = &auxdev->dev;
174 dev_err(dev, "auxiliary device modname is NULL\n");
178 ret = dev_set_name(dev, "%s.%s.%d", modname, auxdev->name, auxdev->id);
180 dev_err(dev, "auxiliary device dev_set_name failed: %d\n", ret);
184 ret = device_add(dev);
186 dev_err(dev, "adding auxiliary device failed!: %d\n", ret);
190 EXPORT_SYMBOL_GPL(__auxiliary_device_add);
193 * auxiliary_find_device - auxiliary device iterator for locating a particular device.
194 * @start: Device to begin with
195 * @data: Data to pass to match function
196 * @match: Callback function to check device
198 * This function returns a reference to a device that is 'found'
199 * for later use, as determined by the @match callback.
201 * The callback should return 0 if the device doesn't match and non-zero
202 * if it does. If the callback returns non-zero, this function will
203 * return to the caller and not iterate over any more devices.
205 struct auxiliary_device *auxiliary_find_device(struct device *start,
207 int (*match)(struct device *dev, const void *data))
211 dev = bus_find_device(&auxiliary_bus_type, start, data, match);
215 return to_auxiliary_dev(dev);
217 EXPORT_SYMBOL_GPL(auxiliary_find_device);
220 * __auxiliary_driver_register - register a driver for auxiliary bus devices
221 * @auxdrv: auxiliary_driver structure
222 * @owner: owning module/driver
223 * @modname: KBUILD_MODNAME for parent driver
225 int __auxiliary_driver_register(struct auxiliary_driver *auxdrv,
226 struct module *owner, const char *modname)
228 if (WARN_ON(!auxdrv->probe) || WARN_ON(!auxdrv->id_table))
232 auxdrv->driver.name = kasprintf(GFP_KERNEL, "%s.%s", modname,
235 auxdrv->driver.name = kasprintf(GFP_KERNEL, "%s", modname);
236 if (!auxdrv->driver.name)
239 auxdrv->driver.owner = owner;
240 auxdrv->driver.bus = &auxiliary_bus_type;
241 auxdrv->driver.mod_name = modname;
243 return driver_register(&auxdrv->driver);
245 EXPORT_SYMBOL_GPL(__auxiliary_driver_register);
248 * auxiliary_driver_unregister - unregister a driver
249 * @auxdrv: auxiliary_driver structure
251 void auxiliary_driver_unregister(struct auxiliary_driver *auxdrv)
253 driver_unregister(&auxdrv->driver);
254 kfree(auxdrv->driver.name);
256 EXPORT_SYMBOL_GPL(auxiliary_driver_unregister);
258 static int __init auxiliary_bus_init(void)
260 return bus_register(&auxiliary_bus_type);
263 static void __exit auxiliary_bus_exit(void)
265 bus_unregister(&auxiliary_bus_type);
268 module_init(auxiliary_bus_init);
269 module_exit(auxiliary_bus_exit);
271 MODULE_LICENSE("GPL v2");
272 MODULE_DESCRIPTION("Auxiliary Bus");
273 MODULE_AUTHOR("David Ertman <david.m.ertman@intel.com>");
274 MODULE_AUTHOR("Kiran Patil <kiran.patil@intel.com>");