2 * driver.c - centralized device driver management
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2007 Novell Inc.
9 * This file is released under the GPLv2
13 #include <linux/device.h>
14 #include <linux/module.h>
15 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/sysfs.h>
21 static struct device *next_device(struct klist_iter *i)
23 struct klist_node *n = klist_next(i);
24 struct device *dev = NULL;
25 struct device_private *dev_prv;
28 dev_prv = to_device_private_driver(n);
29 dev = dev_prv->device;
35 * driver_set_override() - Helper to set or clear driver override.
36 * @dev: Device to change
37 * @override: Address of string to change (e.g. &device->driver_override);
38 * The contents will be freed and hold newly allocated override.
39 * @s: NUL-terminated string, new driver name to force a match, pass empty
40 * string to clear it ("" or "\n", where the latter is only for sysfs
44 * Helper to set or clear driver override in a device, intended for the cases
45 * when the driver_override field is allocated by driver/bus code.
47 * Returns: 0 on success or a negative error code on failure.
49 int driver_set_override(struct device *dev, const char **override,
50 const char *s, size_t len)
52 const char *new, *old;
59 * The stored value will be used in sysfs show callback (sysfs_emit()),
60 * which has a length limit of PAGE_SIZE and adds a trailing newline.
61 * Thus we can store one character less to avoid truncation during sysfs
64 if (len >= (PAGE_SIZE - 1))
68 /* Empty string passed - clear override */
78 cp = strnchr(s, len, '\n');
82 new = kstrndup(s, len, GFP_KERNEL);
91 /* "\n" passed - clear override */
101 EXPORT_SYMBOL_GPL(driver_set_override);
104 * driver_for_each_device - Iterator for devices bound to a driver.
105 * @drv: Driver we're iterating.
106 * @start: Device to begin with
107 * @data: Data to pass to the callback.
108 * @fn: Function to call for each device.
110 * Iterate over the @drv's list of devices calling @fn for each one.
112 int driver_for_each_device(struct device_driver *drv, struct device *start,
113 void *data, int (*fn)(struct device *, void *))
122 klist_iter_init_node(&drv->p->klist_devices, &i,
123 start ? &start->p->knode_driver : NULL);
124 while ((dev = next_device(&i)) && !error)
125 error = fn(dev, data);
129 EXPORT_SYMBOL_GPL(driver_for_each_device);
132 * driver_find_device - device iterator for locating a particular device.
133 * @drv: The device's driver
134 * @start: Device to begin with
135 * @data: Data to pass to match function
136 * @match: Callback function to check device
138 * This is similar to the driver_for_each_device() function above, but
139 * it returns a reference to a device that is 'found' for later use, as
140 * determined by the @match callback.
142 * The callback should return 0 if the device doesn't match and non-zero
143 * if it does. If the callback returns non-zero, this function will
144 * return to the caller and not iterate over any more devices.
146 struct device *driver_find_device(struct device_driver *drv,
147 struct device *start, void *data,
148 int (*match)(struct device *dev, void *data))
156 klist_iter_init_node(&drv->p->klist_devices, &i,
157 (start ? &start->p->knode_driver : NULL));
158 while ((dev = next_device(&i)))
159 if (match(dev, data) && get_device(dev))
164 EXPORT_SYMBOL_GPL(driver_find_device);
167 * driver_create_file - create sysfs file for driver.
169 * @attr: driver attribute descriptor.
171 int driver_create_file(struct device_driver *drv,
172 const struct driver_attribute *attr)
177 error = sysfs_create_file(&drv->p->kobj, &attr->attr);
182 EXPORT_SYMBOL_GPL(driver_create_file);
185 * driver_remove_file - remove sysfs file for driver.
187 * @attr: driver attribute descriptor.
189 void driver_remove_file(struct device_driver *drv,
190 const struct driver_attribute *attr)
193 sysfs_remove_file(&drv->p->kobj, &attr->attr);
195 EXPORT_SYMBOL_GPL(driver_remove_file);
197 int driver_add_groups(struct device_driver *drv,
198 const struct attribute_group **groups)
200 return sysfs_create_groups(&drv->p->kobj, groups);
203 void driver_remove_groups(struct device_driver *drv,
204 const struct attribute_group **groups)
206 sysfs_remove_groups(&drv->p->kobj, groups);
210 * driver_register - register driver with bus
211 * @drv: driver to register
213 * We pass off most of the work to the bus_add_driver() call,
214 * since most of the things we have to do deal with the bus
217 int driver_register(struct device_driver *drv)
220 struct device_driver *other;
222 BUG_ON(!drv->bus->p);
224 if ((drv->bus->probe && drv->probe) ||
225 (drv->bus->remove && drv->remove) ||
226 (drv->bus->shutdown && drv->shutdown))
227 printk(KERN_WARNING "Driver '%s' needs updating - please use "
228 "bus_type methods\n", drv->name);
230 other = driver_find(drv->name, drv->bus);
232 printk(KERN_ERR "Error: Driver '%s' is already registered, "
233 "aborting...\n", drv->name);
237 ret = bus_add_driver(drv);
240 ret = driver_add_groups(drv, drv->groups);
242 bus_remove_driver(drv);
245 kobject_uevent(&drv->p->kobj, KOBJ_ADD);
249 EXPORT_SYMBOL_GPL(driver_register);
252 * driver_unregister - remove driver from system.
255 * Again, we pass off most of the work to the bus-level call.
257 void driver_unregister(struct device_driver *drv)
259 if (!drv || !drv->p) {
260 WARN(1, "Unexpected driver unregister!\n");
263 driver_remove_groups(drv, drv->groups);
264 bus_remove_driver(drv);
266 EXPORT_SYMBOL_GPL(driver_unregister);
269 * driver_find - locate driver on a bus by its name.
270 * @name: name of the driver.
271 * @bus: bus to scan for the driver.
273 * Call kset_find_obj() to iterate over list of drivers on
274 * a bus to find driver by name. Return driver if found.
276 * This routine provides no locking to prevent the driver it returns
277 * from being unregistered or unloaded while the caller is using it.
278 * The caller is responsible for preventing this.
280 struct device_driver *driver_find(const char *name, struct bus_type *bus)
282 struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
283 struct driver_private *priv;
286 /* Drop reference added by kset_find_obj() */
293 EXPORT_SYMBOL_GPL(driver_find);