1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * driver.c - centralized device driver management
4 *
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
7 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2007 Novell Inc.
9 */
10
11 #include <linux/device/driver.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/sysfs.h>
18 #include "base.h"
19
next_device(struct klist_iter * i)20 static struct device *next_device(struct klist_iter *i)
21 {
22 struct klist_node *n = klist_next(i);
23 struct device *dev = NULL;
24 struct device_private *dev_prv;
25
26 if (n) {
27 dev_prv = to_device_private_driver(n);
28 dev = dev_prv->device;
29 }
30 return dev;
31 }
32
33 /**
34 * driver_set_override() - Helper to set or clear driver override.
35 * @dev: Device to change
36 * @override: Address of string to change (e.g. &device->driver_override);
37 * The contents will be freed and hold newly allocated override.
38 * @s: NUL-terminated string, new driver name to force a match, pass empty
39 * string to clear it ("" or "\n", where the latter is only for sysfs
40 * interface).
41 * @len: length of @s
42 *
43 * Helper to set or clear driver override in a device, intended for the cases
44 * when the driver_override field is allocated by driver/bus code.
45 *
46 * Returns: 0 on success or a negative error code on failure.
47 */
driver_set_override(struct device * dev,const char ** override,const char * s,size_t len)48 int driver_set_override(struct device *dev, const char **override,
49 const char *s, size_t len)
50 {
51 const char *new, *old;
52 char *cp;
53
54 if (!override || !s)
55 return -EINVAL;
56
57 /*
58 * The stored value will be used in sysfs show callback (sysfs_emit()),
59 * which has a length limit of PAGE_SIZE and adds a trailing newline.
60 * Thus we can store one character less to avoid truncation during sysfs
61 * show.
62 */
63 if (len >= (PAGE_SIZE - 1))
64 return -EINVAL;
65
66 if (!len) {
67 /* Empty string passed - clear override */
68 device_lock(dev);
69 old = *override;
70 *override = NULL;
71 device_unlock(dev);
72 kfree(old);
73
74 return 0;
75 }
76
77 cp = strnchr(s, len, '\n');
78 if (cp)
79 len = cp - s;
80
81 new = kstrndup(s, len, GFP_KERNEL);
82 if (!new)
83 return -ENOMEM;
84
85 device_lock(dev);
86 old = *override;
87 if (cp != s) {
88 *override = new;
89 } else {
90 /* "\n" passed - clear override */
91 kfree(new);
92 *override = NULL;
93 }
94 device_unlock(dev);
95
96 kfree(old);
97
98 return 0;
99 }
100 EXPORT_SYMBOL_GPL(driver_set_override);
101
102 /**
103 * driver_for_each_device - Iterator for devices bound to a driver.
104 * @drv: Driver we're iterating.
105 * @start: Device to begin with
106 * @data: Data to pass to the callback.
107 * @fn: Function to call for each device.
108 *
109 * Iterate over the @drv's list of devices calling @fn for each one.
110 */
driver_for_each_device(struct device_driver * drv,struct device * start,void * data,int (* fn)(struct device *,void *))111 int driver_for_each_device(struct device_driver *drv, struct device *start,
112 void *data, int (*fn)(struct device *, void *))
113 {
114 struct klist_iter i;
115 struct device *dev;
116 int error = 0;
117
118 if (!drv)
119 return -EINVAL;
120
121 klist_iter_init_node(&drv->p->klist_devices, &i,
122 start ? &start->p->knode_driver : NULL);
123 while (!error && (dev = next_device(&i)))
124 error = fn(dev, data);
125 klist_iter_exit(&i);
126 return error;
127 }
128 EXPORT_SYMBOL_GPL(driver_for_each_device);
129
130 /**
131 * driver_find_device - device iterator for locating a particular device.
132 * @drv: The device's driver
133 * @start: Device to begin with
134 * @data: Data to pass to match function
135 * @match: Callback function to check device
136 *
137 * This is similar to the driver_for_each_device() function above, but
138 * it returns a reference to a device that is 'found' for later use, as
139 * determined by the @match callback.
140 *
141 * The callback should return 0 if the device doesn't match and non-zero
142 * if it does. If the callback returns non-zero, this function will
143 * return to the caller and not iterate over any more devices.
144 */
driver_find_device(struct device_driver * drv,struct device * start,const void * data,int (* match)(struct device * dev,const void * data))145 struct device *driver_find_device(struct device_driver *drv,
146 struct device *start, const void *data,
147 int (*match)(struct device *dev, const void *data))
148 {
149 struct klist_iter i;
150 struct device *dev;
151
152 if (!drv || !drv->p)
153 return NULL;
154
155 klist_iter_init_node(&drv->p->klist_devices, &i,
156 (start ? &start->p->knode_driver : NULL));
157 while ((dev = next_device(&i)))
158 if (match(dev, data) && get_device(dev))
159 break;
160 klist_iter_exit(&i);
161 return dev;
162 }
163 EXPORT_SYMBOL_GPL(driver_find_device);
164
165 /**
166 * driver_create_file - create sysfs file for driver.
167 * @drv: driver.
168 * @attr: driver attribute descriptor.
169 */
driver_create_file(struct device_driver * drv,const struct driver_attribute * attr)170 int driver_create_file(struct device_driver *drv,
171 const struct driver_attribute *attr)
172 {
173 int error;
174
175 if (drv)
176 error = sysfs_create_file(&drv->p->kobj, &attr->attr);
177 else
178 error = -EINVAL;
179 return error;
180 }
181 EXPORT_SYMBOL_GPL(driver_create_file);
182
183 /**
184 * driver_remove_file - remove sysfs file for driver.
185 * @drv: driver.
186 * @attr: driver attribute descriptor.
187 */
driver_remove_file(struct device_driver * drv,const struct driver_attribute * attr)188 void driver_remove_file(struct device_driver *drv,
189 const struct driver_attribute *attr)
190 {
191 if (drv)
192 sysfs_remove_file(&drv->p->kobj, &attr->attr);
193 }
194 EXPORT_SYMBOL_GPL(driver_remove_file);
195
driver_add_groups(struct device_driver * drv,const struct attribute_group ** groups)196 int driver_add_groups(struct device_driver *drv,
197 const struct attribute_group **groups)
198 {
199 return sysfs_create_groups(&drv->p->kobj, groups);
200 }
201
driver_remove_groups(struct device_driver * drv,const struct attribute_group ** groups)202 void driver_remove_groups(struct device_driver *drv,
203 const struct attribute_group **groups)
204 {
205 sysfs_remove_groups(&drv->p->kobj, groups);
206 }
207
208 /**
209 * driver_register - register driver with bus
210 * @drv: driver to register
211 *
212 * We pass off most of the work to the bus_add_driver() call,
213 * since most of the things we have to do deal with the bus
214 * structures.
215 */
driver_register(struct device_driver * drv)216 int driver_register(struct device_driver *drv)
217 {
218 int ret;
219 struct device_driver *other;
220
221 if (!drv->bus->p) {
222 pr_err("Driver '%s' was unable to register with bus_type '%s' because the bus was not initialized.\n",
223 drv->name, drv->bus->name);
224 return -EINVAL;
225 }
226
227 if ((drv->bus->probe && drv->probe) ||
228 (drv->bus->remove && drv->remove) ||
229 (drv->bus->shutdown && drv->shutdown))
230 pr_warn("Driver '%s' needs updating - please use "
231 "bus_type methods\n", drv->name);
232
233 other = driver_find(drv->name, drv->bus);
234 if (other) {
235 pr_err("Error: Driver '%s' is already registered, "
236 "aborting...\n", drv->name);
237 return -EBUSY;
238 }
239
240 ret = bus_add_driver(drv);
241 if (ret)
242 return ret;
243 ret = driver_add_groups(drv, drv->groups);
244 if (ret) {
245 bus_remove_driver(drv);
246 return ret;
247 }
248 kobject_uevent(&drv->p->kobj, KOBJ_ADD);
249
250 return ret;
251 }
252 EXPORT_SYMBOL_GPL(driver_register);
253
254 /**
255 * driver_unregister - remove driver from system.
256 * @drv: driver.
257 *
258 * Again, we pass off most of the work to the bus-level call.
259 */
driver_unregister(struct device_driver * drv)260 void driver_unregister(struct device_driver *drv)
261 {
262 if (!drv || !drv->p) {
263 WARN(1, "Unexpected driver unregister!\n");
264 return;
265 }
266 driver_remove_groups(drv, drv->groups);
267 bus_remove_driver(drv);
268 }
269 EXPORT_SYMBOL_GPL(driver_unregister);
270
271 /**
272 * driver_find - locate driver on a bus by its name.
273 * @name: name of the driver.
274 * @bus: bus to scan for the driver.
275 *
276 * Call kset_find_obj() to iterate over list of drivers on
277 * a bus to find driver by name. Return driver if found.
278 *
279 * This routine provides no locking to prevent the driver it returns
280 * from being unregistered or unloaded while the caller is using it.
281 * The caller is responsible for preventing this.
282 */
driver_find(const char * name,struct bus_type * bus)283 struct device_driver *driver_find(const char *name, struct bus_type *bus)
284 {
285 struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
286 struct driver_private *priv;
287
288 if (k) {
289 /* Drop reference added by kset_find_obj() */
290 kobject_put(k);
291 priv = to_driver(k);
292 return priv->driver;
293 }
294 return NULL;
295 }
296 EXPORT_SYMBOL_GPL(driver_find);
297