aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base/dd.c
diff options
context:
space:
mode:
authormochel@digitalimplant.org <mochel@digitalimplant.org>2005-03-21 13:52:54 -0500
committerGreg Kroah-Hartman <gregkh@suse.de>2005-06-20 18:15:13 -0400
commit07e4a3e27fe414980ddc85a358e5a56abc48b363 (patch)
treeeb32858e7facf8b24a2f0fc2d4e829d6ee715c09 /drivers/base/dd.c
parentaf70316af182f4716cc5eec7e0d27fc731d164bd (diff)
[PATCH] Move device/driver code to drivers/base/dd.c
This relocates the driver binding/unbinding code to drivers/base/dd.c. This is done for two reasons: One, it's not code related to the bus_type itself; it uses some from that, some from devices, and some from drivers. And Two, it will make it easier to do some of the upcoming lock removal on that code.. Signed-off-by: Patrick Mochel <mochel@digitalimplant.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/base/dd.c')
-rw-r--r--drivers/base/dd.c200
1 files changed, 200 insertions, 0 deletions
diff --git a/drivers/base/dd.c b/drivers/base/dd.c
new file mode 100644
index 000000000000..b709b1e0cb2a
--- /dev/null
+++ b/drivers/base/dd.c
@@ -0,0 +1,200 @@
1/*
2 * drivers/base/dd.c - The core device/driver interactions.
3 *
4 * This file contains the (sometimes tricky) code that controls the
5 * interactions between devices and drivers, which primarily includes
6 * driver binding and unbinding.
7 *
8 * All of this code used to exist in drivers/base/bus.c, but was
9 * relocated to here in the name of compartmentalization (since it wasn't
10 * strictly code just for the 'struct bus_type'.
11 *
12 * Copyright (c) 2002-5 Patrick Mochel
13 * Copyright (c) 2002-3 Open Source Development Labs
14 *
15 * This file is released under the GPLv2
16 */
17
18#include <linux/device.h>
19#include <linux/module.h>
20
21#include "base.h"
22#include "power/power.h"
23
24#define to_drv(node) container_of(node, struct device_driver, kobj.entry)
25
26
27/**
28 * device_bind_driver - bind a driver to one device.
29 * @dev: device.
30 *
31 * Allow manual attachment of a driver to a device.
32 * Caller must have already set @dev->driver.
33 *
34 * Note that this does not modify the bus reference count
35 * nor take the bus's rwsem. Please verify those are accounted
36 * for before calling this. (It is ok to call with no other effort
37 * from a driver's probe() method.)
38 */
39void device_bind_driver(struct device * dev)
40{
41 pr_debug("bound device '%s' to driver '%s'\n",
42 dev->bus_id, dev->driver->name);
43 list_add_tail(&dev->driver_list, &dev->driver->devices);
44 sysfs_create_link(&dev->driver->kobj, &dev->kobj,
45 kobject_name(&dev->kobj));
46 sysfs_create_link(&dev->kobj, &dev->driver->kobj, "driver");
47}
48
49/**
50 * driver_probe_device - attempt to bind device & driver.
51 * @drv: driver.
52 * @dev: device.
53 *
54 * First, we call the bus's match function, if one present, which
55 * should compare the device IDs the driver supports with the
56 * device IDs of the device. Note we don't do this ourselves
57 * because we don't know the format of the ID structures, nor what
58 * is to be considered a match and what is not.
59 *
60 * If we find a match, we call @drv->probe(@dev) if it exists, and
61 * call device_bind_driver() above.
62 */
63int driver_probe_device(struct device_driver * drv, struct device * dev)
64{
65 int error = 0;
66
67 if (drv->bus->match && !drv->bus->match(dev, drv))
68 return -ENODEV;
69
70 down(&dev->sem);
71 dev->driver = drv;
72 if (drv->probe) {
73 error = drv->probe(dev);
74 if (error) {
75 dev->driver = NULL;
76 up(&dev->sem);
77 return error;
78 }
79 }
80 up(&dev->sem);
81 device_bind_driver(dev);
82 return 0;
83}
84
85/**
86 * device_attach - try to attach device to a driver.
87 * @dev: device.
88 *
89 * Walk the list of drivers that the bus has and call
90 * driver_probe_device() for each pair. If a compatible
91 * pair is found, break out and return.
92 */
93int device_attach(struct device * dev)
94{
95 struct bus_type * bus = dev->bus;
96 struct list_head * entry;
97 int error;
98
99 if (dev->driver) {
100 device_bind_driver(dev);
101 return 1;
102 }
103
104 if (bus->match) {
105 list_for_each(entry, &bus->drivers.list) {
106 struct device_driver * drv = to_drv(entry);
107 error = driver_probe_device(drv, dev);
108 if (!error)
109 /* success, driver matched */
110 return 1;
111 if (error != -ENODEV && error != -ENXIO)
112 /* driver matched but the probe failed */
113 printk(KERN_WARNING
114 "%s: probe of %s failed with error %d\n",
115 drv->name, dev->bus_id, error);
116 }
117 }
118
119 return 0;
120}
121
122/**
123 * driver_attach - try to bind driver to devices.
124 * @drv: driver.
125 *
126 * Walk the list of devices that the bus has on it and try to
127 * match the driver with each one. If driver_probe_device()
128 * returns 0 and the @dev->driver is set, we've found a
129 * compatible pair.
130 *
131 * Note that we ignore the -ENODEV error from driver_probe_device(),
132 * since it's perfectly valid for a driver not to bind to any devices.
133 */
134void driver_attach(struct device_driver * drv)
135{
136 struct bus_type * bus = drv->bus;
137 struct list_head * entry;
138 int error;
139
140 if (!bus->match)
141 return;
142
143 list_for_each(entry, &bus->devices.list) {
144 struct device * dev = container_of(entry, struct device, bus_list);
145 if (!dev->driver) {
146 error = driver_probe_device(drv, dev);
147 if (error && (error != -ENODEV))
148 /* driver matched but the probe failed */
149 printk(KERN_WARNING
150 "%s: probe of %s failed with error %d\n",
151 drv->name, dev->bus_id, error);
152 }
153 }
154}
155
156/**
157 * device_release_driver - manually detach device from driver.
158 * @dev: device.
159 *
160 * Manually detach device from driver.
161 * Note that this is called without incrementing the bus
162 * reference count nor taking the bus's rwsem. Be sure that
163 * those are accounted for before calling this function.
164 */
165void device_release_driver(struct device * dev)
166{
167 struct device_driver * drv;
168
169 down(&dev->sem);
170 drv = dev->driver;
171 if (drv) {
172 sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
173 sysfs_remove_link(&dev->kobj, "driver");
174 list_del_init(&dev->driver_list);
175 if (drv->remove)
176 drv->remove(dev);
177 dev->driver = NULL;
178 }
179 up(&dev->sem);
180}
181
182/**
183 * driver_detach - detach driver from all devices it controls.
184 * @drv: driver.
185 */
186void driver_detach(struct device_driver * drv)
187{
188 while (!list_empty(&drv->devices)) {
189 struct device * dev = container_of(drv->devices.next, struct device, driver_list);
190 device_release_driver(dev);
191 }
192}
193
194
195EXPORT_SYMBOL_GPL(driver_probe_device);
196EXPORT_SYMBOL_GPL(device_bind_driver);
197EXPORT_SYMBOL_GPL(device_release_driver);
198EXPORT_SYMBOL_GPL(device_attach);
199EXPORT_SYMBOL_GPL(driver_attach);
200