]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/base/auxiliary.c
Merge tag 'tpmdd-next-v5.12-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-jammy-kernel.git] / drivers / base / auxiliary.c
CommitLineData
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
DE
19
20static const struct auxiliary_device_id *auxiliary_match_id(const struct auxiliary_device_id *id,
21 const struct auxiliary_device *auxdev)
22{
23 for (; id->name[0]; id++) {
24 const char *p = strrchr(dev_name(&auxdev->dev), '.');
25 int match_size;
26
27 if (!p)
28 continue;
29 match_size = p - dev_name(&auxdev->dev);
30
31 /* use dev_name(&auxdev->dev) prefix before last '.' char to match to */
32 if (strlen(id->name) == match_size &&
33 !strncmp(dev_name(&auxdev->dev), id->name, match_size))
34 return id;
35 }
36 return NULL;
37}
38
39static int auxiliary_match(struct device *dev, struct device_driver *drv)
40{
41 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
42 struct auxiliary_driver *auxdrv = to_auxiliary_drv(drv);
43
44 return !!auxiliary_match_id(auxdrv->id_table, auxdev);
45}
46
47static int auxiliary_uevent(struct device *dev, struct kobj_uevent_env *env)
48{
49 const char *name, *p;
50
51 name = dev_name(dev);
52 p = strrchr(name, '.');
53
0d2bf11a
GKH
54 return add_uevent_var(env, "MODALIAS=%s%.*s", AUXILIARY_MODULE_PREFIX,
55 (int)(p - name), name);
7de3697e
DE
56}
57
58static const struct dev_pm_ops auxiliary_dev_pm_ops = {
59 SET_RUNTIME_PM_OPS(pm_generic_runtime_suspend, pm_generic_runtime_resume, NULL)
60 SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume)
61};
62
63static int auxiliary_bus_probe(struct device *dev)
64{
65 struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver);
66 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
67 int ret;
68
69 ret = dev_pm_domain_attach(dev, true);
70 if (ret) {
71 dev_warn(dev, "Failed to attach to PM Domain : %d\n", ret);
72 return ret;
73 }
74
75 ret = auxdrv->probe(auxdev, auxiliary_match_id(auxdrv->id_table, auxdev));
76 if (ret)
77 dev_pm_domain_detach(dev, true);
78
79 return ret;
80}
81
82static int auxiliary_bus_remove(struct device *dev)
83{
84 struct auxiliary_driver *auxdrv = to_auxiliary_drv(dev->driver);
85 struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
7de3697e
DE
86
87 if (auxdrv->remove)
8142a46c 88 auxdrv->remove(auxdev);
7de3697e
DE
89 dev_pm_domain_detach(dev, true);
90
8142a46c 91 return 0;
7de3697e
DE
92}
93
94static void auxiliary_bus_shutdown(struct device *dev)
95{
784b2c48
DJ
96 struct auxiliary_driver *auxdrv = NULL;
97 struct auxiliary_device *auxdev;
98
99 if (dev->driver) {
100 auxdrv = to_auxiliary_drv(dev->driver);
101 auxdev = to_auxiliary_dev(dev);
102 }
7de3697e 103
784b2c48 104 if (auxdrv && auxdrv->shutdown)
7de3697e
DE
105 auxdrv->shutdown(auxdev);
106}
107
108static struct bus_type auxiliary_bus_type = {
109 .name = "auxiliary",
110 .probe = auxiliary_bus_probe,
111 .remove = auxiliary_bus_remove,
112 .shutdown = auxiliary_bus_shutdown,
113 .match = auxiliary_match,
114 .uevent = auxiliary_uevent,
115 .pm = &auxiliary_dev_pm_ops,
116};
117
118/**
119 * auxiliary_device_init - check auxiliary_device and initialize
120 * @auxdev: auxiliary device struct
121 *
0d2bf11a
GKH
122 * This is the first step in the two-step process to register an
123 * auxiliary_device.
7de3697e 124 *
0d2bf11a
GKH
125 * When this function returns an error code, then the device_initialize will
126 * *not* have been performed, and the caller will be responsible to free any
127 * memory allocated for the auxiliary_device in the error path directly.
7de3697e 128 *
0d2bf11a
GKH
129 * It returns 0 on success. On success, the device_initialize has been
130 * performed. After this point any error unwinding will need to include a call
131 * to auxiliary_device_uninit(). In this post-initialize error scenario, a call
132 * to the device's .release callback will be triggered, and all memory clean-up
133 * is expected to be handled there.
7de3697e
DE
134 */
135int auxiliary_device_init(struct auxiliary_device *auxdev)
136{
137 struct device *dev = &auxdev->dev;
138
139 if (!dev->parent) {
140 pr_err("auxiliary_device has a NULL dev->parent\n");
141 return -EINVAL;
142 }
143
144 if (!auxdev->name) {
145 pr_err("auxiliary_device has a NULL name\n");
146 return -EINVAL;
147 }
148
149 dev->bus = &auxiliary_bus_type;
150 device_initialize(&auxdev->dev);
151 return 0;
152}
153EXPORT_SYMBOL_GPL(auxiliary_device_init);
154
155/**
156 * __auxiliary_device_add - add an auxiliary bus device
157 * @auxdev: auxiliary bus device to add to the bus
158 * @modname: name of the parent device's driver module
159 *
0d2bf11a
GKH
160 * This is the second step in the two-step process to register an
161 * auxiliary_device.
7de3697e 162 *
0d2bf11a
GKH
163 * This function must be called after a successful call to
164 * auxiliary_device_init(), which will perform the device_initialize. This
165 * means that if this returns an error code, then a call to
166 * auxiliary_device_uninit() must be performed so that the .release callback
167 * will be triggered to free the memory associated with the auxiliary_device.
7de3697e 168 *
0d2bf11a
GKH
169 * The expectation is that users will call the "auxiliary_device_add" macro so
170 * that the caller's KBUILD_MODNAME is automatically inserted for the modname
171 * parameter. Only if a user requires a custom name would this version be
172 * called directly.
7de3697e
DE
173 */
174int __auxiliary_device_add(struct auxiliary_device *auxdev, const char *modname)
175{
176 struct device *dev = &auxdev->dev;
177 int ret;
178
179 if (!modname) {
0d2bf11a 180 dev_err(dev, "auxiliary device modname is NULL\n");
7de3697e
DE
181 return -EINVAL;
182 }
183
184 ret = dev_set_name(dev, "%s.%s.%d", modname, auxdev->name, auxdev->id);
185 if (ret) {
0d2bf11a 186 dev_err(dev, "auxiliary device dev_set_name failed: %d\n", ret);
7de3697e
DE
187 return ret;
188 }
189
190 ret = device_add(dev);
191 if (ret)
192 dev_err(dev, "adding auxiliary device failed!: %d\n", ret);
193
194 return ret;
195}
196EXPORT_SYMBOL_GPL(__auxiliary_device_add);
197
198/**
199 * auxiliary_find_device - auxiliary device iterator for locating a particular device.
200 * @start: Device to begin with
201 * @data: Data to pass to match function
202 * @match: Callback function to check device
203 *
204 * This function returns a reference to a device that is 'found'
205 * for later use, as determined by the @match callback.
206 *
207 * The callback should return 0 if the device doesn't match and non-zero
208 * if it does. If the callback returns non-zero, this function will
209 * return to the caller and not iterate over any more devices.
210 */
0d2bf11a
GKH
211struct auxiliary_device *auxiliary_find_device(struct device *start,
212 const void *data,
213 int (*match)(struct device *dev, const void *data))
7de3697e
DE
214{
215 struct device *dev;
216
217 dev = bus_find_device(&auxiliary_bus_type, start, data, match);
218 if (!dev)
219 return NULL;
220
221 return to_auxiliary_dev(dev);
222}
223EXPORT_SYMBOL_GPL(auxiliary_find_device);
224
225/**
226 * __auxiliary_driver_register - register a driver for auxiliary bus devices
227 * @auxdrv: auxiliary_driver structure
228 * @owner: owning module/driver
229 * @modname: KBUILD_MODNAME for parent driver
230 */
0d2bf11a
GKH
231int __auxiliary_driver_register(struct auxiliary_driver *auxdrv,
232 struct module *owner, const char *modname)
7de3697e
DE
233{
234 if (WARN_ON(!auxdrv->probe) || WARN_ON(!auxdrv->id_table))
235 return -EINVAL;
236
237 if (auxdrv->name)
0d2bf11a
GKH
238 auxdrv->driver.name = kasprintf(GFP_KERNEL, "%s.%s", modname,
239 auxdrv->name);
7de3697e
DE
240 else
241 auxdrv->driver.name = kasprintf(GFP_KERNEL, "%s", modname);
242 if (!auxdrv->driver.name)
243 return -ENOMEM;
244
245 auxdrv->driver.owner = owner;
246 auxdrv->driver.bus = &auxiliary_bus_type;
247 auxdrv->driver.mod_name = modname;
248
249 return driver_register(&auxdrv->driver);
250}
251EXPORT_SYMBOL_GPL(__auxiliary_driver_register);
252
253/**
254 * auxiliary_driver_unregister - unregister a driver
255 * @auxdrv: auxiliary_driver structure
256 */
257void auxiliary_driver_unregister(struct auxiliary_driver *auxdrv)
258{
259 driver_unregister(&auxdrv->driver);
260 kfree(auxdrv->driver.name);
261}
262EXPORT_SYMBOL_GPL(auxiliary_driver_unregister);
263
471b12c4 264void __init auxiliary_bus_init(void)
7de3697e 265{
471b12c4 266 WARN_ON(bus_register(&auxiliary_bus_type));
7de3697e
DE
267}
268
7de3697e
DE
269MODULE_LICENSE("GPL v2");
270MODULE_DESCRIPTION("Auxiliary Bus");
271MODULE_AUTHOR("David Ertman <david.m.ertman@intel.com>");
272MODULE_AUTHOR("Kiran Patil <kiran.patil@intel.com>");