PNP: centralize resource option allocations
[linux-2.6/mini2440.git] / drivers / base / driver.c
blob2ef5acf4368b9e8deef4440d865a87cef8d0b0a6
1 /*
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/string.h>
17 #include "base.h"
19 #define to_dev(node) container_of(node, struct device, driver_list)
22 static struct device *next_device(struct klist_iter *i)
24 struct klist_node *n = klist_next(i);
25 return n ? container_of(n, struct device, knode_driver) : NULL;
28 /**
29 * driver_for_each_device - Iterator for devices bound to a driver.
30 * @drv: Driver we're iterating.
31 * @start: Device to begin with
32 * @data: Data to pass to the callback.
33 * @fn: Function to call for each device.
35 * 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->p->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;
54 EXPORT_SYMBOL_GPL(driver_for_each_device);
56 /**
57 * driver_find_device - device iterator for locating a particular device.
58 * @drv: The device's driver
59 * @start: Device to begin with
60 * @data: Data to pass to match function
61 * @match: Callback function to check device
63 * This is similar to the driver_for_each_device() function above, but
64 * it returns a reference to a device that is 'found' for later use, as
65 * determined by the @match callback.
67 * The callback should return 0 if the device doesn't match and non-zero
68 * if it does. If the callback returns non-zero, this function will
69 * return to the caller and not iterate over any more devices.
71 struct device *driver_find_device(struct device_driver *drv,
72 struct device *start, void *data,
73 int (*match)(struct device *dev, void *data))
75 struct klist_iter i;
76 struct device *dev;
78 if (!drv)
79 return NULL;
81 klist_iter_init_node(&drv->p->klist_devices, &i,
82 (start ? &start->knode_driver : NULL));
83 while ((dev = next_device(&i)))
84 if (match(dev, data) && get_device(dev))
85 break;
86 klist_iter_exit(&i);
87 return dev;
89 EXPORT_SYMBOL_GPL(driver_find_device);
91 /**
92 * driver_create_file - create sysfs file for driver.
93 * @drv: driver.
94 * @attr: driver attribute descriptor.
96 int driver_create_file(struct device_driver *drv,
97 struct driver_attribute *attr)
99 int error;
100 if (drv)
101 error = sysfs_create_file(&drv->p->kobj, &attr->attr);
102 else
103 error = -EINVAL;
104 return error;
106 EXPORT_SYMBOL_GPL(driver_create_file);
109 * driver_remove_file - remove sysfs file for driver.
110 * @drv: driver.
111 * @attr: driver attribute descriptor.
113 void driver_remove_file(struct device_driver *drv,
114 struct driver_attribute *attr)
116 if (drv)
117 sysfs_remove_file(&drv->p->kobj, &attr->attr);
119 EXPORT_SYMBOL_GPL(driver_remove_file);
122 * driver_add_kobj - add a kobject below the specified driver
123 * @drv: requesting device driver
124 * @kobj: kobject to add below this driver
125 * @fmt: format string that names the kobject
127 * You really don't want to do this, this is only here due to one looney
128 * iseries driver, go poke those developers if you are annoyed about
129 * this...
131 int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
132 const char *fmt, ...)
134 va_list args;
135 char *name;
136 int ret;
138 va_start(args, fmt);
139 name = kvasprintf(GFP_KERNEL, fmt, args);
140 va_end(args);
142 if (!name)
143 return -ENOMEM;
145 ret = kobject_add(kobj, &drv->p->kobj, "%s", name);
146 kfree(name);
147 return ret;
149 EXPORT_SYMBOL_GPL(driver_add_kobj);
152 * get_driver - increment driver reference count.
153 * @drv: driver.
155 struct device_driver *get_driver(struct device_driver *drv)
157 if (drv) {
158 struct driver_private *priv;
159 struct kobject *kobj;
161 kobj = kobject_get(&drv->p->kobj);
162 priv = to_driver(kobj);
163 return priv->driver;
165 return NULL;
167 EXPORT_SYMBOL_GPL(get_driver);
170 * put_driver - decrement driver's refcount.
171 * @drv: driver.
173 void put_driver(struct device_driver *drv)
175 kobject_put(&drv->p->kobj);
177 EXPORT_SYMBOL_GPL(put_driver);
179 static int driver_add_groups(struct device_driver *drv,
180 struct attribute_group **groups)
182 int error = 0;
183 int i;
185 if (groups) {
186 for (i = 0; groups[i]; i++) {
187 error = sysfs_create_group(&drv->p->kobj, groups[i]);
188 if (error) {
189 while (--i >= 0)
190 sysfs_remove_group(&drv->p->kobj,
191 groups[i]);
192 break;
196 return error;
199 static void driver_remove_groups(struct device_driver *drv,
200 struct attribute_group **groups)
202 int i;
204 if (groups)
205 for (i = 0; groups[i]; i++)
206 sysfs_remove_group(&drv->p->kobj, groups[i]);
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
215 * structures.
217 int driver_register(struct device_driver *drv)
219 int ret;
220 struct device_driver *other;
222 if ((drv->bus->probe && drv->probe) ||
223 (drv->bus->remove && drv->remove) ||
224 (drv->bus->shutdown && drv->shutdown))
225 printk(KERN_WARNING "Driver '%s' needs updating - please use "
226 "bus_type methods\n", drv->name);
228 other = driver_find(drv->name, drv->bus);
229 if (other) {
230 put_driver(other);
231 printk(KERN_ERR "Error: Driver '%s' is already registered, "
232 "aborting...\n", drv->name);
233 return -EEXIST;
236 ret = bus_add_driver(drv);
237 if (ret)
238 return ret;
239 ret = driver_add_groups(drv, drv->groups);
240 if (ret)
241 bus_remove_driver(drv);
242 return ret;
244 EXPORT_SYMBOL_GPL(driver_register);
247 * driver_unregister - remove driver from system.
248 * @drv: driver.
250 * Again, we pass off most of the work to the bus-level call.
252 void driver_unregister(struct device_driver *drv)
254 driver_remove_groups(drv, drv->groups);
255 bus_remove_driver(drv);
257 EXPORT_SYMBOL_GPL(driver_unregister);
260 * driver_find - locate driver on a bus by its name.
261 * @name: name of the driver.
262 * @bus: bus to scan for the driver.
264 * Call kset_find_obj() to iterate over list of drivers on
265 * a bus to find driver by name. Return driver if found.
267 * Note that kset_find_obj increments driver's reference count.
269 struct device_driver *driver_find(const char *name, struct bus_type *bus)
271 struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
272 struct driver_private *priv;
274 if (k) {
275 priv = to_driver(k);
276 return priv->driver;
278 return NULL;
280 EXPORT_SYMBOL_GPL(driver_find);