1 // SPDX-License-Identifier: GPL-2.0
3 * driver.c - centralized device driver management
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.
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>
20 static struct device *next_device(struct klist_iter *i)
22 struct klist_node *n = klist_next(i);
23 struct device *dev = NULL;
24 struct device_private *dev_prv;
27 dev_prv = to_device_private_driver(n);
28 dev = dev_prv->device;
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
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.
46 * Returns: 0 on success or a negative error code on failure.
48 int driver_set_override(struct device *dev, const char **override,
49 const char *s, size_t len)
51 const char *new, *old;
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
63 if (len >= (PAGE_SIZE - 1))
67 /* Empty string passed - clear override */
77 cp = strnchr(s, len, '\n');
81 new = kstrndup(s, len, GFP_KERNEL);
90 /* "\n" passed - clear override */
100 EXPORT_SYMBOL_GPL(driver_set_override);
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.
109 * Iterate over the @drv's list of devices calling @fn for each one.
111 int driver_for_each_device(struct device_driver *drv, struct device *start,
112 void *data, int (*fn)(struct device *, void *))
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);
128 EXPORT_SYMBOL_GPL(driver_for_each_device);
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
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.
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.
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))
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))
163 EXPORT_SYMBOL_GPL(driver_find_device);
166 * driver_create_file - create sysfs file for driver.
168 * @attr: driver attribute descriptor.
170 int driver_create_file(struct device_driver *drv,
171 const struct driver_attribute *attr)
176 error = sysfs_create_file(&drv->p->kobj, &attr->attr);
181 EXPORT_SYMBOL_GPL(driver_create_file);
184 * driver_remove_file - remove sysfs file for driver.
186 * @attr: driver attribute descriptor.
188 void driver_remove_file(struct device_driver *drv,
189 const struct driver_attribute *attr)
192 sysfs_remove_file(&drv->p->kobj, &attr->attr);
194 EXPORT_SYMBOL_GPL(driver_remove_file);
196 int driver_add_groups(struct device_driver *drv,
197 const struct attribute_group **groups)
199 return sysfs_create_groups(&drv->p->kobj, groups);
202 void driver_remove_groups(struct device_driver *drv,
203 const struct attribute_group **groups)
205 sysfs_remove_groups(&drv->p->kobj, groups);
209 * driver_register - register driver with bus
210 * @drv: driver to register
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
216 int driver_register(struct device_driver *drv)
219 struct device_driver *other;
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);
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);
233 other = driver_find(drv->name, drv->bus);
235 pr_err("Error: Driver '%s' is already registered, "
236 "aborting...\n", drv->name);
240 ret = bus_add_driver(drv);
243 ret = driver_add_groups(drv, drv->groups);
245 bus_remove_driver(drv);
248 kobject_uevent(&drv->p->kobj, KOBJ_ADD);
249 deferred_probe_extend_timeout();
253 EXPORT_SYMBOL_GPL(driver_register);
256 * driver_unregister - remove driver from system.
259 * Again, we pass off most of the work to the bus-level call.
261 void driver_unregister(struct device_driver *drv)
263 if (!drv || !drv->p) {
264 WARN(1, "Unexpected driver unregister!\n");
267 driver_remove_groups(drv, drv->groups);
268 bus_remove_driver(drv);
270 EXPORT_SYMBOL_GPL(driver_unregister);
273 * driver_find - locate driver on a bus by its name.
274 * @name: name of the driver.
275 * @bus: bus to scan for the driver.
277 * Call kset_find_obj() to iterate over list of drivers on
278 * a bus to find driver by name. Return driver if found.
280 * This routine provides no locking to prevent the driver it returns
281 * from being unregistered or unloaded while the caller is using it.
282 * The caller is responsible for preventing this.
284 struct device_driver *driver_find(const char *name, struct bus_type *bus)
286 struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
287 struct driver_private *priv;
290 /* Drop reference added by kset_find_obj() */
297 EXPORT_SYMBOL_GPL(driver_find);