2 * driver.c - centralized device driver management
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
7 * This file is released under the GPLv2
11 #include <linux/config.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
18 #define to_dev(node) container_of(node, struct device, driver_list)
19 #define to_drv(obj) container_of(obj, struct device_driver, kobj)
23 * driver_for_each_device - Iterator for devices bound to a driver.
24 * @drv: Driver we're iterating.
25 * @data: Data to pass to the callback.
26 * @fn: Function to call for each device.
28 * Take the bus's rwsem and iterate over the @drv's list of devices,
29 * calling @fn for each one.
32 int driver_for_each_device(struct device_driver * drv, struct device * start,
33 void * data, int (*fn)(struct device *, void *))
35 struct list_head * head;
39 down_read(&drv->bus->subsys.rwsem);
41 dev = list_prepare_entry(start, head, driver_list);
42 list_for_each_entry_continue(dev, head, driver_list) {
44 error = fn(dev, data);
49 up_read(&drv->bus->subsys.rwsem);
53 EXPORT_SYMBOL(driver_for_each_device);
57 * driver_create_file - create sysfs file for driver.
59 * @attr: driver attribute descriptor.
62 int driver_create_file(struct device_driver * drv, struct driver_attribute * attr)
65 if (get_driver(drv)) {
66 error = sysfs_create_file(&drv->kobj, &attr->attr);
75 * driver_remove_file - remove sysfs file for driver.
77 * @attr: driver attribute descriptor.
80 void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr)
82 if (get_driver(drv)) {
83 sysfs_remove_file(&drv->kobj, &attr->attr);
90 * get_driver - increment driver reference count.
93 struct device_driver * get_driver(struct device_driver * drv)
95 return drv ? to_drv(kobject_get(&drv->kobj)) : NULL;
100 * put_driver - decrement driver's refcount.
103 void put_driver(struct device_driver * drv)
105 kobject_put(&drv->kobj);
110 * driver_register - register driver with bus
111 * @drv: driver to register
113 * We pass off most of the work to the bus_add_driver() call,
114 * since most of the things we have to do deal with the bus
117 * The one interesting aspect is that we setup @drv->unloaded
118 * as a completion that gets complete when the driver reference
121 int driver_register(struct device_driver * drv)
123 INIT_LIST_HEAD(&drv->devices);
124 init_completion(&drv->unloaded);
125 return bus_add_driver(drv);
130 * driver_unregister - remove driver from system.
133 * Again, we pass off most of the work to the bus-level call.
135 * Though, once that is done, we wait until @drv->unloaded is completed.
136 * This will block until the driver refcount reaches 0, and it is
137 * released. Only modular drivers will call this function, and we
138 * have to guarantee that it won't complete, letting the driver
139 * unload until all references are gone.
142 void driver_unregister(struct device_driver * drv)
144 bus_remove_driver(drv);
145 wait_for_completion(&drv->unloaded);
149 * driver_find - locate driver on a bus by its name.
150 * @name: name of the driver.
151 * @bus: bus to scan for the driver.
153 * Call kset_find_obj() to iterate over list of drivers on
154 * a bus to find driver by name. Return driver if found.
156 * Note that kset_find_obj increments driver's reference count.
158 struct device_driver *driver_find(const char *name, struct bus_type *bus)
160 struct kobject *k = kset_find_obj(&bus->drivers, name);
166 EXPORT_SYMBOL_GPL(driver_register);
167 EXPORT_SYMBOL_GPL(driver_unregister);
168 EXPORT_SYMBOL_GPL(get_driver);
169 EXPORT_SYMBOL_GPL(put_driver);
170 EXPORT_SYMBOL_GPL(driver_find);
172 EXPORT_SYMBOL_GPL(driver_create_file);
173 EXPORT_SYMBOL_GPL(driver_remove_file);