Driver core: move the driver specific module code into the driver core
[deliverable/linux.git] / drivers / base / driver.c
CommitLineData
1da177e4
LT
1/*
2 * driver.c - centralized device driver management
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 *
7 * This file is released under the GPLv2
8 *
9 */
10
1da177e4
LT
11#include <linux/device.h>
12#include <linux/module.h>
13#include <linux/errno.h>
14#include <linux/string.h>
15#include "base.h"
16
17#define to_dev(node) container_of(node, struct device, driver_list)
18#define to_drv(obj) container_of(obj, struct device_driver, kobj)
19
fae3cd00 20
94e7b1c5 21static struct device * next_device(struct klist_iter * i)
22{
23 struct klist_node * n = klist_next(i);
24 return n ? container_of(n, struct device, knode_driver) : NULL;
25}
26
fae3cd00 27/**
28 * driver_for_each_device - Iterator for devices bound to a driver.
29 * @drv: Driver we're iterating.
c41455fb 30 * @start: Device to begin with
fae3cd00 31 * @data: Data to pass to the callback.
32 * @fn: Function to call for each device.
33 *
4d12d2d9 34 * Iterate over the @drv's list of devices calling @fn for each one.
fae3cd00 35 */
36
37int driver_for_each_device(struct device_driver * drv, struct device * start,
38 void * data, int (*fn)(struct device *, void *))
39{
94e7b1c5 40 struct klist_iter i;
fae3cd00 41 struct device * dev;
42 int error = 0;
43
94e7b1c5 44 if (!drv)
45 return -EINVAL;
46
47 klist_iter_init_node(&drv->klist_devices, &i,
48 start ? &start->knode_driver : NULL);
49 while ((dev = next_device(&i)) && !error)
fae3cd00 50 error = fn(dev, data);
94e7b1c5 51 klist_iter_exit(&i);
fae3cd00 52 return error;
53}
54
126eddfb 55EXPORT_SYMBOL_GPL(driver_for_each_device);
fae3cd00 56
57
0edb5860
CH
58/**
59 * driver_find_device - device iterator for locating a particular device.
c41455fb 60 * @drv: The device's driver
0edb5860
CH
61 * @start: Device to begin with
62 * @data: Data to pass to match function
63 * @match: Callback function to check device
64 *
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.
68 *
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.
72 */
73struct device * driver_find_device(struct device_driver *drv,
74 struct device * start, void * data,
75 int (*match)(struct device *, void *))
76{
77 struct klist_iter i;
78 struct device *dev;
79
80 if (!drv)
81 return NULL;
82
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;
90}
91EXPORT_SYMBOL_GPL(driver_find_device);
92
1da177e4
LT
93/**
94 * driver_create_file - create sysfs file for driver.
95 * @drv: driver.
96 * @attr: driver attribute descriptor.
97 */
98
99int driver_create_file(struct device_driver * drv, struct driver_attribute * attr)
100{
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;
108}
109
110
111/**
112 * driver_remove_file - remove sysfs file for driver.
113 * @drv: driver.
114 * @attr: driver attribute descriptor.
115 */
116
117void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr)
118{
119 if (get_driver(drv)) {
120 sysfs_remove_file(&drv->kobj, &attr->attr);
121 put_driver(drv);
122 }
123}
124
125
cbe9c595
GKH
126/**
127 * driver_add_kobj - add a kobject below the specified driver
128 *
129 * You really don't want to do this, this is only here due to one looney
130 * iseries driver, go poke those developers if you are annoyed about
131 * this...
132 */
133int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
134 const char *fmt, ...)
135{
136 va_list args;
137 char *name;
138
139 va_start(args, fmt);
140 name = kvasprintf(GFP_KERNEL, fmt, args);
141 va_end(args);
142
143 if (!name)
144 return -ENOMEM;
145
146 return kobject_add_ng(kobj, &drv->kobj, "%s", name);
147}
148EXPORT_SYMBOL_GPL(driver_add_kobj);
149
1da177e4
LT
150/**
151 * get_driver - increment driver reference count.
152 * @drv: driver.
153 */
154struct device_driver * get_driver(struct device_driver * drv)
155{
156 return drv ? to_drv(kobject_get(&drv->kobj)) : NULL;
157}
158
159
160/**
161 * put_driver - decrement driver's refcount.
162 * @drv: driver.
163 */
164void put_driver(struct device_driver * drv)
165{
166 kobject_put(&drv->kobj);
167}
168
57c74534
CH
169static int driver_add_groups(struct device_driver *drv,
170 struct attribute_group **groups)
171{
172 int error = 0;
173 int i;
174
175 if (groups) {
176 for (i = 0; groups[i]; i++) {
177 error = sysfs_create_group(&drv->kobj, groups[i]);
178 if (error) {
179 while (--i >= 0)
180 sysfs_remove_group(&drv->kobj,
181 groups[i]);
182 break;
183 }
184 }
185 }
186 return error;
187}
188
189static void driver_remove_groups(struct device_driver *drv,
190 struct attribute_group **groups)
191{
192 int i;
193
194 if (groups)
195 for (i = 0; groups[i]; i++)
196 sysfs_remove_group(&drv->kobj, groups[i]);
197}
198
199
1da177e4
LT
200/**
201 * driver_register - register driver with bus
202 * @drv: driver to register
203 *
204 * We pass off most of the work to the bus_add_driver() call,
205 * since most of the things we have to do deal with the bus
206 * structures.
1da177e4
LT
207 */
208int driver_register(struct device_driver * drv)
209{
57c74534
CH
210 int ret;
211
594c8281
RK
212 if ((drv->bus->probe && drv->probe) ||
213 (drv->bus->remove && drv->remove) ||
214 (drv->bus->shutdown && drv->shutdown)) {
215 printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name);
216 }
81107bf5 217 klist_init(&drv->klist_devices, NULL, NULL);
57c74534
CH
218 ret = bus_add_driver(drv);
219 if (ret)
220 return ret;
221 ret = driver_add_groups(drv, drv->groups);
222 if (ret)
223 bus_remove_driver(drv);
224 return ret;
1da177e4
LT
225}
226
1da177e4
LT
227/**
228 * driver_unregister - remove driver from system.
229 * @drv: driver.
230 *
231 * Again, we pass off most of the work to the bus-level call.
1da177e4
LT
232 */
233
234void driver_unregister(struct device_driver * drv)
235{
57c74534 236 driver_remove_groups(drv, drv->groups);
1da177e4 237 bus_remove_driver(drv);
1da177e4
LT
238}
239
240/**
241 * driver_find - locate driver on a bus by its name.
242 * @name: name of the driver.
243 * @bus: bus to scan for the driver.
244 *
245 * Call kset_find_obj() to iterate over list of drivers on
246 * a bus to find driver by name. Return driver if found.
247 *
248 * Note that kset_find_obj increments driver's reference count.
249 */
250struct device_driver *driver_find(const char *name, struct bus_type *bus)
251{
c6f7e72a 252 struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
1da177e4
LT
253 if (k)
254 return to_drv(k);
255 return NULL;
256}
257
258EXPORT_SYMBOL_GPL(driver_register);
259EXPORT_SYMBOL_GPL(driver_unregister);
260EXPORT_SYMBOL_GPL(get_driver);
261EXPORT_SYMBOL_GPL(put_driver);
262EXPORT_SYMBOL_GPL(driver_find);
263
264EXPORT_SYMBOL_GPL(driver_create_file);
265EXPORT_SYMBOL_GPL(driver_remove_file);
This page took 0.284055 seconds and 5 git commands to generate.