]>
Commit | Line | Data |
---|---|---|
7de3697e DE |
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> | |
7bbb79ff | 12 | #include <linux/slab.h> |
7de3697e DE |
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> | |
471b12c4 | 18 | #include "base.h" |
7de3697e | 19 | |
e1b51868 IW |
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 | |
81089c89 | 95 | * domain-specific structure defined by the parent device. This structure |
e1b51868 IW |
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 | ||
7de3697e DE |
161 | static const struct auxiliary_device_id *auxiliary_match_id(const struct auxiliary_device_id *id, |
162 | const struct auxiliary_device *auxdev) | |
163 | { | |
164 | for (; id->name[0]; id++) { | |
165 | const char *p = strrchr(dev_name(&auxdev->dev), '.'); | |
166 | int match_size; | |
167 | ||
168 | if (!p) | |
169 | continue; | |
170 | match_size = p - dev_name(&auxdev->dev); | |
171 | ||
172 | /* use dev_name(&auxdev->dev) prefix before last '.' char to match to */ | |
173 | if (strlen(id->name) == match_size && | |
174 | !strncmp(dev_name(&auxdev->dev), id->name, match_size)) | |
175 | return id; | |
176 | } | |
177 | return NULL; | |
178 | } | |
179 | ||
d69d8048 | 180 | static int auxiliary_match(struct device *dev, const struct device_driver *drv) |
7de3697e DE |
181 | { |
182 | struct auxiliary_device *auxdev = to_auxiliary_dev(dev); | |
ff985c75 | 183 | const struct auxiliary_driver *auxdrv = to_auxiliary_drv(drv); |
7de3697e DE |
184 | |
185 | return !!auxiliary_match_id(auxdrv->id_table, auxdev); | |
186 | } | |
187 | ||
2a81ada3 | 188 | static int auxiliary_uevent(const struct device *dev, struct kobj_uevent_env *env) |
7de3697e DE |
189 | { |
190 | const char *name, *p; | |
191 | ||
192 | name = dev_name(dev); | |
193 | p = strrchr(name, '.'); | |
194 | ||
0d2bf11a GKH |
195 | return add_uevent_var(env, "MODALIAS=%s%.*s", AUXILIARY_MODULE_PREFIX, |
196 | (int)(p - name), name); | |
7de3697e DE |
197 | } |
198 | ||
199 | static const struct dev_pm_ops auxiliary_dev_pm_ops = { | |
200 | SET_RUNTIME_PM_OPS(pm_generic_runtime_suspend, pm_generic_runtime_resume, NULL) | |
201 | SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume) | |
202 | }; | |
203 | ||
204 | static int auxiliary_bus_probe(struct device *dev) | |
205 | { | |
ff985c75 | 206 | const struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver); |
7de3697e DE |
207 | struct auxiliary_device *auxdev = to_auxiliary_dev(dev); |
208 | int ret; | |
209 | ||
210 | ret = dev_pm_domain_attach(dev, true); | |
211 | if (ret) { | |
212 | dev_warn(dev, "Failed to attach to PM Domain : %d\n", ret); | |
213 | return ret; | |
214 | } | |
215 | ||
216 | ret = auxdrv->probe(auxdev, auxiliary_match_id(auxdrv->id_table, auxdev)); | |
217 | if (ret) | |
218 | dev_pm_domain_detach(dev, true); | |
219 | ||
220 | return ret; | |
221 | } | |
222 | ||
fc7a6209 | 223 | static void auxiliary_bus_remove(struct device *dev) |
7de3697e | 224 | { |
ff985c75 | 225 | const struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver); |
7de3697e | 226 | struct auxiliary_device *auxdev = to_auxiliary_dev(dev); |
7de3697e DE |
227 | |
228 | if (auxdrv->remove) | |
8142a46c | 229 | auxdrv->remove(auxdev); |
7de3697e | 230 | dev_pm_domain_detach(dev, true); |
7de3697e DE |
231 | } |
232 | ||
233 | static void auxiliary_bus_shutdown(struct device *dev) | |
234 | { | |
ff985c75 | 235 | const struct auxiliary_driver *auxdrv = NULL; |
784b2c48 DJ |
236 | struct auxiliary_device *auxdev; |
237 | ||
238 | if (dev->driver) { | |
239 | auxdrv = to_auxiliary_drv(dev->driver); | |
240 | auxdev = to_auxiliary_dev(dev); | |
241 | } | |
7de3697e | 242 | |
784b2c48 | 243 | if (auxdrv && auxdrv->shutdown) |
7de3697e DE |
244 | auxdrv->shutdown(auxdev); |
245 | } | |
246 | ||
580fc9c7 | 247 | static const struct bus_type auxiliary_bus_type = { |
7de3697e DE |
248 | .name = "auxiliary", |
249 | .probe = auxiliary_bus_probe, | |
250 | .remove = auxiliary_bus_remove, | |
251 | .shutdown = auxiliary_bus_shutdown, | |
252 | .match = auxiliary_match, | |
253 | .uevent = auxiliary_uevent, | |
254 | .pm = &auxiliary_dev_pm_ops, | |
255 | }; | |
256 | ||
257 | /** | |
258 | * auxiliary_device_init - check auxiliary_device and initialize | |
259 | * @auxdev: auxiliary device struct | |
260 | * | |
b2477038 | 261 | * This is the second step in the three-step process to register an |
0d2bf11a | 262 | * auxiliary_device. |
7de3697e | 263 | * |
0d2bf11a GKH |
264 | * When this function returns an error code, then the device_initialize will |
265 | * *not* have been performed, and the caller will be responsible to free any | |
266 | * memory allocated for the auxiliary_device in the error path directly. | |
7de3697e | 267 | * |
0d2bf11a GKH |
268 | * It returns 0 on success. On success, the device_initialize has been |
269 | * performed. After this point any error unwinding will need to include a call | |
270 | * to auxiliary_device_uninit(). In this post-initialize error scenario, a call | |
271 | * to the device's .release callback will be triggered, and all memory clean-up | |
272 | * is expected to be handled there. | |
7de3697e DE |
273 | */ |
274 | int auxiliary_device_init(struct auxiliary_device *auxdev) | |
275 | { | |
276 | struct device *dev = &auxdev->dev; | |
277 | ||
278 | if (!dev->parent) { | |
279 | pr_err("auxiliary_device has a NULL dev->parent\n"); | |
280 | return -EINVAL; | |
281 | } | |
282 | ||
283 | if (!auxdev->name) { | |
284 | pr_err("auxiliary_device has a NULL name\n"); | |
285 | return -EINVAL; | |
286 | } | |
287 | ||
288 | dev->bus = &auxiliary_bus_type; | |
289 | device_initialize(&auxdev->dev); | |
a8088783 | 290 | mutex_init(&auxdev->sysfs.lock); |
7de3697e DE |
291 | return 0; |
292 | } | |
293 | EXPORT_SYMBOL_GPL(auxiliary_device_init); | |
294 | ||
295 | /** | |
296 | * __auxiliary_device_add - add an auxiliary bus device | |
297 | * @auxdev: auxiliary bus device to add to the bus | |
298 | * @modname: name of the parent device's driver module | |
299 | * | |
b2477038 | 300 | * This is the third step in the three-step process to register an |
0d2bf11a | 301 | * auxiliary_device. |
7de3697e | 302 | * |
0d2bf11a GKH |
303 | * This function must be called after a successful call to |
304 | * auxiliary_device_init(), which will perform the device_initialize. This | |
305 | * means that if this returns an error code, then a call to | |
306 | * auxiliary_device_uninit() must be performed so that the .release callback | |
307 | * will be triggered to free the memory associated with the auxiliary_device. | |
7de3697e | 308 | * |
0d2bf11a GKH |
309 | * The expectation is that users will call the "auxiliary_device_add" macro so |
310 | * that the caller's KBUILD_MODNAME is automatically inserted for the modname | |
311 | * parameter. Only if a user requires a custom name would this version be | |
312 | * called directly. | |
7de3697e DE |
313 | */ |
314 | int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname) | |
315 | { | |
316 | struct device *dev = &auxdev->dev; | |
317 | int ret; | |
318 | ||
319 | if (!modname) { | |
0d2bf11a | 320 | dev_err(dev, "auxiliary device modname is NULL\n"); |
7de3697e DE |
321 | return -EINVAL; |
322 | } | |
323 | ||
324 | ret = dev_set_name(dev, "%s.%s.%d", modname, auxdev->name, auxdev->id); | |
325 | if (ret) { | |
0d2bf11a | 326 | dev_err(dev, "auxiliary device dev_set_name failed: %d\n", ret); |
7de3697e DE |
327 | return ret; |
328 | } | |
329 | ||
330 | ret = device_add(dev); | |
331 | if (ret) | |
332 | dev_err(dev, "adding auxiliary device failed!: %d\n", ret); | |
333 | ||
334 | return ret; | |
335 | } | |
336 | EXPORT_SYMBOL_GPL(__auxiliary_device_add); | |
337 | ||
7de3697e DE |
338 | /** |
339 | * __auxiliary_driver_register - register a driver for auxiliary bus devices | |
340 | * @auxdrv: auxiliary_driver structure | |
341 | * @owner: owning module/driver | |
342 | * @modname: KBUILD_MODNAME for parent driver | |
05021dca IW |
343 | * |
344 | * The expectation is that users will call the "auxiliary_driver_register" | |
345 | * macro so that the caller's KBUILD_MODNAME is automatically inserted for the | |
346 | * modname parameter. Only if a user requires a custom name would this version | |
347 | * be called directly. | |
7de3697e | 348 | */ |
0d2bf11a GKH |
349 | int __auxiliary_driver_register(struct auxiliary_driver *auxdrv, |
350 | struct module *owner, const char *modname) | |
7de3697e | 351 | { |
4afa0c22 PU |
352 | int ret; |
353 | ||
7de3697e DE |
354 | if (WARN_ON(!auxdrv->probe) || WARN_ON(!auxdrv->id_table)) |
355 | return -EINVAL; | |
356 | ||
357 | if (auxdrv->name) | |
0d2bf11a GKH |
358 | auxdrv->driver.name = kasprintf(GFP_KERNEL, "%s.%s", modname, |
359 | auxdrv->name); | |
7de3697e DE |
360 | else |
361 | auxdrv->driver.name = kasprintf(GFP_KERNEL, "%s", modname); | |
362 | if (!auxdrv->driver.name) | |
363 | return -ENOMEM; | |
364 | ||
365 | auxdrv->driver.owner = owner; | |
366 | auxdrv->driver.bus = &auxiliary_bus_type; | |
367 | auxdrv->driver.mod_name = modname; | |
368 | ||
4afa0c22 PU |
369 | ret = driver_register(&auxdrv->driver); |
370 | if (ret) | |
371 | kfree(auxdrv->driver.name); | |
372 | ||
373 | return ret; | |
7de3697e DE |
374 | } |
375 | EXPORT_SYMBOL_GPL(__auxiliary_driver_register); | |
376 | ||
377 | /** | |
378 | * auxiliary_driver_unregister - unregister a driver | |
379 | * @auxdrv: auxiliary_driver structure | |
380 | */ | |
381 | void auxiliary_driver_unregister(struct auxiliary_driver *auxdrv) | |
382 | { | |
383 | driver_unregister(&auxdrv->driver); | |
384 | kfree(auxdrv->driver.name); | |
385 | } | |
386 | EXPORT_SYMBOL_GPL(auxiliary_driver_unregister); | |
387 | ||
471b12c4 | 388 | void __init auxiliary_bus_init(void) |
7de3697e | 389 | { |
471b12c4 | 390 | WARN_ON(bus_register(&auxiliary_bus_type)); |
7de3697e | 391 | } |