ia64/linux-2.6.18-xen.hg

view drivers/base/dd.c @ 881:e9f508296fc7

pcie io space multiplex: backport of bus event notification patch

back port of 116af378201ef793424cd10508ccf18b06d8a021 and
ec0676ee28528dc8dda13a93ee4b1f215a0c2f9d.

commit 116af378201ef793424cd10508ccf18b06d8a021
Author: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Date: Wed Oct 25 13:44:59 2006 +1000

Driver core: add notification of bus events

Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>


commit ec0676ee28528dc8dda13a93ee4b1f215a0c2f9d
Author: Alan Stern <stern@rowland.harvard.edu>
Date: Fri Dec 5 14:10:31 2008 -0500

Driver core: move the bus notifier call points

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
author Keir Fraser <keir.fraser@citrix.com>
date Thu May 28 09:56:11 2009 +0100 (2009-05-28)
parents 831230e53067
children
line source
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 */
18 #include <linux/device.h>
19 #include <linux/module.h>
21 #include "base.h"
22 #include "power/power.h"
24 #define to_drv(node) container_of(node, struct device_driver, kobj.entry)
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 *
39 * This function must be called with @dev->sem held.
40 */
41 void device_bind_driver(struct device * dev)
42 {
43 if (klist_node_attached(&dev->knode_driver))
44 return;
46 pr_debug("bound device '%s' to driver '%s'\n",
47 dev->bus_id, dev->driver->name);
49 if (dev->bus)
50 blocking_notifier_call_chain(&dev->bus->bus_notifier,
51 BUS_NOTIFY_BOUND_DRIVER, dev);
53 klist_add_tail(&dev->knode_driver, &dev->driver->klist_devices);
54 sysfs_create_link(&dev->driver->kobj, &dev->kobj,
55 kobject_name(&dev->kobj));
56 sysfs_create_link(&dev->kobj, &dev->driver->kobj, "driver");
57 }
59 /**
60 * driver_probe_device - attempt to bind device & driver.
61 * @drv: driver.
62 * @dev: device.
63 *
64 * First, we call the bus's match function, if one present, which
65 * should compare the device IDs the driver supports with the
66 * device IDs of the device. Note we don't do this ourselves
67 * because we don't know the format of the ID structures, nor what
68 * is to be considered a match and what is not.
69 *
70 * This function returns 1 if a match is found, an error if one
71 * occurs (that is not -ENODEV or -ENXIO), and 0 otherwise.
72 *
73 * This function must be called with @dev->sem held. When called
74 * for a USB interface, @dev->parent->sem must be held as well.
75 */
76 int driver_probe_device(struct device_driver * drv, struct device * dev)
77 {
78 int ret = 0;
80 if (drv->bus->match && !drv->bus->match(dev, drv))
81 goto Done;
83 pr_debug("%s: Matched Device %s with Driver %s\n",
84 drv->bus->name, dev->bus_id, drv->name);
85 dev->driver = drv;
86 if (dev->bus->probe) {
87 ret = dev->bus->probe(dev);
88 if (ret) {
89 dev->driver = NULL;
90 goto ProbeFailed;
91 }
92 } else if (drv->probe) {
93 ret = drv->probe(dev);
94 if (ret) {
95 dev->driver = NULL;
96 goto ProbeFailed;
97 }
98 }
99 device_bind_driver(dev);
100 ret = 1;
101 pr_debug("%s: Bound Device %s to Driver %s\n",
102 drv->bus->name, dev->bus_id, drv->name);
103 goto Done;
105 ProbeFailed:
106 if (ret == -ENODEV || ret == -ENXIO) {
107 /* Driver matched, but didn't support device
108 * or device not found.
109 * Not an error; keep going.
110 */
111 ret = 0;
112 } else {
113 /* driver matched but the probe failed */
114 printk(KERN_WARNING
115 "%s: probe of %s failed with error %d\n",
116 drv->name, dev->bus_id, ret);
117 }
118 Done:
119 return ret;
120 }
122 static int __device_attach(struct device_driver * drv, void * data)
123 {
124 struct device * dev = data;
125 return driver_probe_device(drv, dev);
126 }
128 /**
129 * device_attach - try to attach device to a driver.
130 * @dev: device.
131 *
132 * Walk the list of drivers that the bus has and call
133 * driver_probe_device() for each pair. If a compatible
134 * pair is found, break out and return.
135 *
136 * Returns 1 if the device was bound to a driver;
137 * 0 if no matching device was found; error code otherwise.
138 *
139 * When called for a USB interface, @dev->parent->sem must be held.
140 */
141 int device_attach(struct device * dev)
142 {
143 int ret = 0;
145 down(&dev->sem);
146 if (dev->driver) {
147 device_bind_driver(dev);
148 ret = 1;
149 } else
150 ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
151 up(&dev->sem);
152 return ret;
153 }
155 static int __driver_attach(struct device * dev, void * data)
156 {
157 struct device_driver * drv = data;
159 /*
160 * Lock device and try to bind to it. We drop the error
161 * here and always return 0, because we need to keep trying
162 * to bind to devices and some drivers will return an error
163 * simply if it didn't support the device.
164 *
165 * driver_probe_device() will spit a warning if there
166 * is an error.
167 */
169 if (dev->parent) /* Needed for USB */
170 down(&dev->parent->sem);
171 down(&dev->sem);
172 if (!dev->driver)
173 driver_probe_device(drv, dev);
174 up(&dev->sem);
175 if (dev->parent)
176 up(&dev->parent->sem);
178 return 0;
179 }
181 /**
182 * driver_attach - try to bind driver to devices.
183 * @drv: driver.
184 *
185 * Walk the list of devices that the bus has on it and try to
186 * match the driver with each one. If driver_probe_device()
187 * returns 0 and the @dev->driver is set, we've found a
188 * compatible pair.
189 */
190 void driver_attach(struct device_driver * drv)
191 {
192 bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
193 }
195 /**
196 * device_release_driver - manually detach device from driver.
197 * @dev: device.
198 *
199 * Manually detach device from driver.
200 *
201 * __device_release_driver() must be called with @dev->sem held.
202 * When called for a USB interface, @dev->parent->sem must be held
203 * as well.
204 */
206 static void __device_release_driver(struct device * dev)
207 {
208 struct device_driver * drv;
210 drv = dev->driver;
211 if (drv) {
212 get_driver(drv);
213 sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
214 sysfs_remove_link(&dev->kobj, "driver");
215 klist_remove(&dev->knode_driver);
217 if (dev->bus)
218 blocking_notifier_call_chain(&dev->bus->bus_notifier,
219 BUS_NOTIFY_UNBIND_DRIVER,
220 dev);
222 if (dev->bus && dev->bus->remove)
223 dev->bus->remove(dev);
224 else if (drv->remove)
225 drv->remove(dev);
226 dev->driver = NULL;
227 put_driver(drv);
228 }
229 }
231 void device_release_driver(struct device * dev)
232 {
233 /*
234 * If anyone calls device_release_driver() recursively from
235 * within their ->remove callback for the same device, they
236 * will deadlock right here.
237 */
238 down(&dev->sem);
239 __device_release_driver(dev);
240 up(&dev->sem);
241 }
244 /**
245 * driver_detach - detach driver from all devices it controls.
246 * @drv: driver.
247 */
248 void driver_detach(struct device_driver * drv)
249 {
250 struct device * dev;
252 for (;;) {
253 spin_lock(&drv->klist_devices.k_lock);
254 if (list_empty(&drv->klist_devices.k_list)) {
255 spin_unlock(&drv->klist_devices.k_lock);
256 break;
257 }
258 dev = list_entry(drv->klist_devices.k_list.prev,
259 struct device, knode_driver.n_node);
260 get_device(dev);
261 spin_unlock(&drv->klist_devices.k_lock);
263 if (dev->parent) /* Needed for USB */
264 down(&dev->parent->sem);
265 down(&dev->sem);
266 if (dev->driver == drv)
267 __device_release_driver(dev);
268 up(&dev->sem);
269 if (dev->parent)
270 up(&dev->parent->sem);
271 put_device(dev);
272 }
273 }
276 EXPORT_SYMBOL_GPL(device_bind_driver);
277 EXPORT_SYMBOL_GPL(device_release_driver);
278 EXPORT_SYMBOL_GPL(device_attach);
279 EXPORT_SYMBOL_GPL(driver_attach);