2 * drivers/base/dd.c - The core device/driver interactions.
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.
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'.
12 * Copyright (c) 2002-5 Patrick Mochel
13 * Copyright (c) 2002-3 Open Source Development Labs
15 * This file is released under the GPLv2
18 #include <linux/device.h>
19 #include <linux/module.h>
22 #include "power/power.h"
24 #define to_drv(node) container_of(node, struct device_driver, kobj.entry)
28 * device_bind_driver - bind a driver to one device.
31 * Allow manual attachment of a driver to a device.
32 * Caller must have already set @dev->driver.
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.)
39 void device_bind_driver(struct device * dev)
41 pr_debug("bound device '%s' to driver '%s'\n",
42 dev->bus_id, dev->driver->name);
43 klist_add_tail(&dev->driver->klist_devices, &dev->knode_driver);
44 sysfs_create_link(&dev->driver->kobj, &dev->kobj,
45 kobject_name(&dev->kobj));
46 sysfs_create_link(&dev->kobj, &dev->driver->kobj, "driver");
50 * driver_probe_device - attempt to bind device & driver.
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.
60 * If we find a match, we call @drv->probe(@dev) if it exists, and
61 * call device_bind_driver() above.
63 int driver_probe_device(struct device_driver * drv, struct device * dev)
67 if (drv->bus->match && !drv->bus->match(dev, drv))
73 error = drv->probe(dev);
81 device_bind_driver(dev);
85 static int __device_attach(struct device_driver * drv, void * data)
87 struct device * dev = data;
90 error = driver_probe_device(drv, dev);
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.
99 /* driver matched but the probe failed */
101 "%s: probe of %s failed with error %d\n",
102 drv->name, dev->bus_id, error);
106 /* stop looking, this device is attached */
111 * device_attach - try to attach device to a driver.
114 * Walk the list of drivers that the bus has and call
115 * driver_probe_device() for each pair. If a compatible
116 * pair is found, break out and return.
118 int device_attach(struct device * dev)
121 device_bind_driver(dev);
125 return bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
128 static int __driver_attach(struct device * dev, void * data)
130 struct device_driver * drv = data;
134 error = driver_probe_device(drv, dev);
136 if (error != -ENODEV) {
137 /* driver matched but the probe failed */
139 "%s: probe of %s failed with error %d\n",
140 drv->name, dev->bus_id, error);
145 /* stop looking, this driver is attached */
152 * driver_attach - try to bind driver to devices.
155 * Walk the list of devices that the bus has on it and try to
156 * match the driver with each one. If driver_probe_device()
157 * returns 0 and the @dev->driver is set, we've found a
160 * Note that we ignore the -ENODEV error from driver_probe_device(),
161 * since it's perfectly valid for a driver not to bind to any devices.
163 void driver_attach(struct device_driver * drv)
165 bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
169 * device_release_driver - manually detach device from driver.
172 * Manually detach device from driver.
173 * Note that this is called without incrementing the bus
174 * reference count nor taking the bus's rwsem. Be sure that
175 * those are accounted for before calling this function.
177 void device_release_driver(struct device * dev)
179 struct device_driver * drv = dev->driver;
184 sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
185 sysfs_remove_link(&dev->kobj, "driver");
186 klist_del(&dev->knode_driver);
195 static int __remove_driver(struct device * dev, void * unused)
197 device_release_driver(dev);
202 * driver_detach - detach driver from all devices it controls.
205 void driver_detach(struct device_driver * drv)
207 driver_for_each_device(drv, NULL, NULL, __remove_driver);
211 EXPORT_SYMBOL_GPL(driver_probe_device);
212 EXPORT_SYMBOL_GPL(device_bind_driver);
213 EXPORT_SYMBOL_GPL(device_release_driver);
214 EXPORT_SYMBOL_GPL(device_attach);
215 EXPORT_SYMBOL_GPL(driver_attach);