blob: 85042ada8a5bc1eee38e376547926e42d1bfba08 [file] [log] [blame]
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -08001/*
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);
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -080043 klist_add_tail(&dev->driver->klist_devices, &dev->knode_driver);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080044 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
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -080085static int __device_attach(struct device_driver * drv, void * data)
86{
87 struct device * dev = data;
88 int error;
89
90 error = driver_probe_device(drv, dev);
91
92 if (error == -ENODEV && error == -ENXIO) {
93 /* Driver matched, but didn't support device
94 * or device not found.
95 * Not an error; keep going.
96 */
97 error = 0;
98 } else {
99 /* driver matched but the probe failed */
100 printk(KERN_WARNING
101 "%s: probe of %s failed with error %d\n",
102 drv->name, dev->bus_id, error);
103 }
104 return 0;
105}
106
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800107/**
108 * device_attach - try to attach device to a driver.
109 * @dev: device.
110 *
111 * Walk the list of drivers that the bus has and call
112 * driver_probe_device() for each pair. If a compatible
113 * pair is found, break out and return.
114 */
115int device_attach(struct device * dev)
116{
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800117 if (dev->driver) {
118 device_bind_driver(dev);
119 return 1;
120 }
121
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800122 return bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
123}
124
125static int __driver_attach(struct device * dev, void * data)
126{
127 struct device_driver * drv = data;
128 int error = 0;
129
130 if (!dev->driver) {
131 error = driver_probe_device(drv, dev);
132 if (error) {
133 if (error != -ENODEV) {
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800134 /* driver matched but the probe failed */
135 printk(KERN_WARNING
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800136 "%s: probe of %s failed with error %d\n",
137 drv->name, dev->bus_id, error);
138 } else
139 error = 0;
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800140 }
141 }
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800142 return 0;
143}
144
145/**
146 * driver_attach - try to bind driver to devices.
147 * @drv: driver.
148 *
149 * Walk the list of devices that the bus has on it and try to
150 * match the driver with each one. If driver_probe_device()
151 * returns 0 and the @dev->driver is set, we've found a
152 * compatible pair.
153 *
154 * Note that we ignore the -ENODEV error from driver_probe_device(),
155 * since it's perfectly valid for a driver not to bind to any devices.
156 */
157void driver_attach(struct device_driver * drv)
158{
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800159 bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800160}
161
162/**
163 * device_release_driver - manually detach device from driver.
164 * @dev: device.
165 *
166 * Manually detach device from driver.
167 * Note that this is called without incrementing the bus
168 * reference count nor taking the bus's rwsem. Be sure that
169 * those are accounted for before calling this function.
170 */
171void device_release_driver(struct device * dev)
172{
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -0800173 struct device_driver * drv = dev->driver;
174
175 if (!drv)
176 return;
177
178 sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
179 sysfs_remove_link(&dev->kobj, "driver");
180 klist_remove(&dev->knode_driver);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800181
182 down(&dev->sem);
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -0800183 if (drv->remove)
184 drv->remove(dev);
185 dev->driver = NULL;
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800186 up(&dev->sem);
187}
188
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -0800189static int __remove_driver(struct device * dev, void * unused)
190{
191 device_release_driver(dev);
192 return 0;
193}
194
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800195/**
196 * driver_detach - detach driver from all devices it controls.
197 * @drv: driver.
198 */
199void driver_detach(struct device_driver * drv)
200{
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -0800201 driver_for_each_device(drv, NULL, NULL, __remove_driver);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800202}
203
204
205EXPORT_SYMBOL_GPL(driver_probe_device);
206EXPORT_SYMBOL_GPL(device_bind_driver);
207EXPORT_SYMBOL_GPL(device_release_driver);
208EXPORT_SYMBOL_GPL(device_attach);
209EXPORT_SYMBOL_GPL(driver_attach);
210