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)
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
;
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.
38 int driver_for_each_device(struct device_driver
* drv
, struct device
* start
,
39 void * data
, int (*fn
)(struct device
*, void *))
48 klist_iter_init_node(&drv
->klist_devices
, &i
,
49 start
? &start
->knode_driver
: NULL
);
50 while ((dev
= next_device(&i
)) && !error
)
51 error
= fn(dev
, data
);
56 EXPORT_SYMBOL_GPL(driver_for_each_device
);
60 * driver_find_device - device iterator for locating a particular device.
61 * @drv: The device's driver
62 * @start: Device to begin with
63 * @data: Data to pass to match function
64 * @match: Callback function to check device
66 * This is similar to the driver_for_each_device() function above, but
67 * it returns a reference to a device that is 'found' for later use, as
68 * determined by the @match callback.
70 * The callback should return 0 if the device doesn't match and non-zero
71 * if it does. If the callback returns non-zero, this function will
72 * return to the caller and not iterate over any more devices.
74 struct device
* driver_find_device(struct device_driver
*drv
,
75 struct device
* start
, void * data
,
76 int (*match
)(struct device
*, void *))
84 klist_iter_init_node(&drv
->klist_devices
, &i
,
85 (start
? &start
->knode_driver
: NULL
));
86 while ((dev
= next_device(&i
)))
87 if (match(dev
, data
) && get_device(dev
))
92 EXPORT_SYMBOL_GPL(driver_find_device
);
95 * driver_create_file - create sysfs file for driver.
97 * @attr: driver attribute descriptor.
100 int driver_create_file(struct device_driver
* drv
, struct driver_attribute
* attr
)
103 if (get_driver(drv
)) {
104 error
= sysfs_create_file(&drv
->kobj
, &attr
->attr
);
113 * driver_remove_file - remove sysfs file for driver.
115 * @attr: driver attribute descriptor.
118 void driver_remove_file(struct device_driver
* drv
, struct driver_attribute
* attr
)
120 if (get_driver(drv
)) {
121 sysfs_remove_file(&drv
->kobj
, &attr
->attr
);
128 * get_driver - increment driver reference count.
131 struct device_driver
* get_driver(struct device_driver
* drv
)
133 return drv
? to_drv(kobject_get(&drv
->kobj
)) : NULL
;
138 * put_driver - decrement driver's refcount.
141 void put_driver(struct device_driver
* drv
)
143 kobject_put(&drv
->kobj
);
146 static void klist_devices_get(struct klist_node
*n
)
148 struct device
*dev
= container_of(n
, struct device
, knode_driver
);
153 static void klist_devices_put(struct klist_node
*n
)
155 struct device
*dev
= container_of(n
, struct device
, knode_driver
);
161 * driver_register - register driver with bus
162 * @drv: driver to register
164 * We pass off most of the work to the bus_add_driver() call,
165 * since most of the things we have to do deal with the bus
168 * The one interesting aspect is that we setup @drv->unloaded
169 * as a completion that gets complete when the driver reference
172 int driver_register(struct device_driver
* drv
)
174 klist_init(&drv
->klist_devices
, klist_devices_get
, klist_devices_put
);
175 init_completion(&drv
->unloaded
);
176 return bus_add_driver(drv
);
181 * driver_unregister - remove driver from system.
184 * Again, we pass off most of the work to the bus-level call.
186 * Though, once that is done, we wait until @drv->unloaded is completed.
187 * This will block until the driver refcount reaches 0, and it is
188 * released. Only modular drivers will call this function, and we
189 * have to guarantee that it won't complete, letting the driver
190 * unload until all references are gone.
193 void driver_unregister(struct device_driver
* drv
)
195 bus_remove_driver(drv
);
196 wait_for_completion(&drv
->unloaded
);
200 * driver_find - locate driver on a bus by its name.
201 * @name: name of the driver.
202 * @bus: bus to scan for the driver.
204 * Call kset_find_obj() to iterate over list of drivers on
205 * a bus to find driver by name. Return driver if found.
207 * Note that kset_find_obj increments driver's reference count.
209 struct device_driver
*driver_find(const char *name
, struct bus_type
*bus
)
211 struct kobject
*k
= kset_find_obj(&bus
->drivers
, name
);
217 EXPORT_SYMBOL_GPL(driver_register
);
218 EXPORT_SYMBOL_GPL(driver_unregister
);
219 EXPORT_SYMBOL_GPL(get_driver
);
220 EXPORT_SYMBOL_GPL(put_driver
);
221 EXPORT_SYMBOL_GPL(driver_find
);
223 EXPORT_SYMBOL_GPL(driver_create_file
);
224 EXPORT_SYMBOL_GPL(driver_remove_file
);