blob: 34bd38aa7eb8073002b44069167beab5aad1a8e9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
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>
16#include "base.h"
17
18#define to_dev(node) container_of(node, struct device, driver_list)
19#define to_drv(obj) container_of(obj, struct device_driver, kobj)
20
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080021
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080022static struct device * next_device(struct klist_iter * i)
23{
24 struct klist_node * n = klist_next(i);
25 return n ? container_of(n, struct device, knode_driver) : NULL;
26}
27
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080028/**
29 * driver_for_each_device - Iterator for devices bound to a driver.
30 * @drv: Driver we're iterating.
31 * @data: Data to pass to the callback.
32 * @fn: Function to call for each device.
33 *
34 * Take the bus's rwsem and iterate over the @drv's list of devices,
35 * calling @fn for each one.
36 */
37
38int driver_for_each_device(struct device_driver * drv, struct device * start,
39 void * data, int (*fn)(struct device *, void *))
40{
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080041 struct klist_iter i;
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080042 struct device * dev;
43 int error = 0;
44
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080045 if (!drv)
46 return -EINVAL;
47
48 klist_iter_init_node(&drv->klist_devices, &i,
49 start ? &start->knode_driver : NULL);
50 while ((dev = next_device(&i)) && !error)
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080051 error = fn(dev, data);
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080052 klist_iter_exit(&i);
mochel@digitalimplant.orgfae3cd02005-03-21 10:59:56 -080053 return error;
54}
55
56EXPORT_SYMBOL(driver_for_each_device);
57
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/**
60 * driver_create_file - create sysfs file for driver.
61 * @drv: driver.
62 * @attr: driver attribute descriptor.
63 */
64
65int driver_create_file(struct device_driver * drv, struct driver_attribute * attr)
66{
67 int error;
68 if (get_driver(drv)) {
69 error = sysfs_create_file(&drv->kobj, &attr->attr);
70 put_driver(drv);
71 } else
72 error = -EINVAL;
73 return error;
74}
75
76
77/**
78 * driver_remove_file - remove sysfs file for driver.
79 * @drv: driver.
80 * @attr: driver attribute descriptor.
81 */
82
83void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr)
84{
85 if (get_driver(drv)) {
86 sysfs_remove_file(&drv->kobj, &attr->attr);
87 put_driver(drv);
88 }
89}
90
91
92/**
93 * get_driver - increment driver reference count.
94 * @drv: driver.
95 */
96struct device_driver * get_driver(struct device_driver * drv)
97{
98 return drv ? to_drv(kobject_get(&drv->kobj)) : NULL;
99}
100
101
102/**
103 * put_driver - decrement driver's refcount.
104 * @drv: driver.
105 */
106void put_driver(struct device_driver * drv)
107{
108 kobject_put(&drv->kobj);
109}
110
111
112/**
113 * driver_register - register driver with bus
114 * @drv: driver to register
115 *
116 * We pass off most of the work to the bus_add_driver() call,
117 * since most of the things we have to do deal with the bus
118 * structures.
119 *
120 * The one interesting aspect is that we setup @drv->unloaded
121 * as a completion that gets complete when the driver reference
122 * count reaches 0.
123 */
124int driver_register(struct device_driver * drv)
125{
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -0800126 klist_init(&drv->klist_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 init_completion(&drv->unloaded);
128 return bus_add_driver(drv);
129}
130
131
132/**
133 * driver_unregister - remove driver from system.
134 * @drv: driver.
135 *
136 * Again, we pass off most of the work to the bus-level call.
137 *
138 * Though, once that is done, we wait until @drv->unloaded is completed.
139 * This will block until the driver refcount reaches 0, and it is
140 * released. Only modular drivers will call this function, and we
141 * have to guarantee that it won't complete, letting the driver
142 * unload until all references are gone.
143 */
144
145void driver_unregister(struct device_driver * drv)
146{
147 bus_remove_driver(drv);
148 wait_for_completion(&drv->unloaded);
149}
150
151/**
152 * driver_find - locate driver on a bus by its name.
153 * @name: name of the driver.
154 * @bus: bus to scan for the driver.
155 *
156 * Call kset_find_obj() to iterate over list of drivers on
157 * a bus to find driver by name. Return driver if found.
158 *
159 * Note that kset_find_obj increments driver's reference count.
160 */
161struct device_driver *driver_find(const char *name, struct bus_type *bus)
162{
163 struct kobject *k = kset_find_obj(&bus->drivers, name);
164 if (k)
165 return to_drv(k);
166 return NULL;
167}
168
169EXPORT_SYMBOL_GPL(driver_register);
170EXPORT_SYMBOL_GPL(driver_unregister);
171EXPORT_SYMBOL_GPL(get_driver);
172EXPORT_SYMBOL_GPL(put_driver);
173EXPORT_SYMBOL_GPL(driver_find);
174
175EXPORT_SYMBOL_GPL(driver_create_file);
176EXPORT_SYMBOL_GPL(driver_remove_file);