Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[linux-2.6/linux-mips.git] / drivers / base / driver.c
blob562600dd540a942a08b49277f06209b016fb8d07
1 /*
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
9 */
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include "base.h"
17 #define to_dev(node) container_of(node, struct device, driver_list)
18 #define to_drv(obj) container_of(obj, struct device_driver, kobj)
21 static struct device * next_device(struct klist_iter * i)
23 struct klist_node * n = klist_next(i);
24 return n ? container_of(n, struct device, knode_driver) : NULL;
27 /**
28 * driver_for_each_device - Iterator for devices bound to a driver.
29 * @drv: Driver we're iterating.
30 * @start: Device to begin with
31 * @data: Data to pass to the callback.
32 * @fn: Function to call for each device.
34 * Iterate over the @drv's list of devices calling @fn for each one.
37 int driver_for_each_device(struct device_driver * drv, struct device * start,
38 void * data, int (*fn)(struct device *, void *))
40 struct klist_iter i;
41 struct device * dev;
42 int error = 0;
44 if (!drv)
45 return -EINVAL;
47 klist_iter_init_node(&drv->klist_devices, &i,
48 start ? &start->knode_driver : NULL);
49 while ((dev = next_device(&i)) && !error)
50 error = fn(dev, data);
51 klist_iter_exit(&i);
52 return error;
55 EXPORT_SYMBOL_GPL(driver_for_each_device);
58 /**
59 * driver_find_device - device iterator for locating a particular device.
60 * @drv: The device's driver
61 * @start: Device to begin with
62 * @data: Data to pass to match function
63 * @match: Callback function to check device
65 * This is similar to the driver_for_each_device() function above, but
66 * it returns a reference to a device that is 'found' for later use, as
67 * determined by the @match callback.
69 * The callback should return 0 if the device doesn't match and non-zero
70 * if it does. If the callback returns non-zero, this function will
71 * return to the caller and not iterate over any more devices.
73 struct device * driver_find_device(struct device_driver *drv,
74 struct device * start, void * data,
75 int (*match)(struct device *, void *))
77 struct klist_iter i;
78 struct device *dev;
80 if (!drv)
81 return NULL;
83 klist_iter_init_node(&drv->klist_devices, &i,
84 (start ? &start->knode_driver : NULL));
85 while ((dev = next_device(&i)))
86 if (match(dev, data) && get_device(dev))
87 break;
88 klist_iter_exit(&i);
89 return dev;
91 EXPORT_SYMBOL_GPL(driver_find_device);
93 /**
94 * driver_create_file - create sysfs file for driver.
95 * @drv: driver.
96 * @attr: driver attribute descriptor.
99 int driver_create_file(struct device_driver * drv, struct driver_attribute * attr)
101 int error;
102 if (get_driver(drv)) {
103 error = sysfs_create_file(&drv->kobj, &attr->attr);
104 put_driver(drv);
105 } else
106 error = -EINVAL;
107 return error;
112 * driver_remove_file - remove sysfs file for driver.
113 * @drv: driver.
114 * @attr: driver attribute descriptor.
117 void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr)
119 if (get_driver(drv)) {
120 sysfs_remove_file(&drv->kobj, &attr->attr);
121 put_driver(drv);
127 * get_driver - increment driver reference count.
128 * @drv: driver.
130 struct device_driver * get_driver(struct device_driver * drv)
132 return drv ? to_drv(kobject_get(&drv->kobj)) : NULL;
137 * put_driver - decrement driver's refcount.
138 * @drv: driver.
140 void put_driver(struct device_driver * drv)
142 kobject_put(&drv->kobj);
145 static void klist_devices_get(struct klist_node *n)
147 struct device *dev = container_of(n, struct device, knode_driver);
149 get_device(dev);
152 static void klist_devices_put(struct klist_node *n)
154 struct device *dev = container_of(n, struct device, knode_driver);
156 put_device(dev);
160 * driver_register - register driver with bus
161 * @drv: driver to register
163 * We pass off most of the work to the bus_add_driver() call,
164 * since most of the things we have to do deal with the bus
165 * structures.
167 * The one interesting aspect is that we setup @drv->unloaded
168 * as a completion that gets complete when the driver reference
169 * count reaches 0.
171 int driver_register(struct device_driver * drv)
173 if ((drv->bus->probe && drv->probe) ||
174 (drv->bus->remove && drv->remove) ||
175 (drv->bus->shutdown && drv->shutdown)) {
176 printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name);
178 klist_init(&drv->klist_devices, klist_devices_get, klist_devices_put);
179 init_completion(&drv->unloaded);
180 return bus_add_driver(drv);
185 * driver_unregister - remove driver from system.
186 * @drv: driver.
188 * Again, we pass off most of the work to the bus-level call.
190 * Though, once that is done, we wait until @drv->unloaded is completed.
191 * This will block until the driver refcount reaches 0, and it is
192 * released. Only modular drivers will call this function, and we
193 * have to guarantee that it won't complete, letting the driver
194 * unload until all references are gone.
197 void driver_unregister(struct device_driver * drv)
199 bus_remove_driver(drv);
200 wait_for_completion(&drv->unloaded);
204 * driver_find - locate driver on a bus by its name.
205 * @name: name of the driver.
206 * @bus: bus to scan for the driver.
208 * Call kset_find_obj() to iterate over list of drivers on
209 * a bus to find driver by name. Return driver if found.
211 * Note that kset_find_obj increments driver's reference count.
213 struct device_driver *driver_find(const char *name, struct bus_type *bus)
215 struct kobject *k = kset_find_obj(&bus->drivers, name);
216 if (k)
217 return to_drv(k);
218 return NULL;
221 EXPORT_SYMBOL_GPL(driver_register);
222 EXPORT_SYMBOL_GPL(driver_unregister);
223 EXPORT_SYMBOL_GPL(get_driver);
224 EXPORT_SYMBOL_GPL(put_driver);
225 EXPORT_SYMBOL_GPL(driver_find);
227 EXPORT_SYMBOL_GPL(driver_create_file);
228 EXPORT_SYMBOL_GPL(driver_remove_file);