blob: ee95c76bfd3dbb0b7ecdee8b02d9da078d6ed7eb [file] [log] [blame]
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -08001/*
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08002 * drivers/base/dd.c - The core device/driver interactions.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -08003 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08004 * This file contains the (sometimes tricky) code that controls the
5 * interactions between devices and drivers, which primarily includes
6 * driver binding and unbinding.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -08007 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -08008 * 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'.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080011 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080012 * Copyright (c) 2002-5 Patrick Mochel
13 * Copyright (c) 2002-3 Open Source Development Labs
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -070014 * Copyright (c) 2007-2009 Greg Kroah-Hartman <gregkh@suse.de>
15 * Copyright (c) 2007-2009 Novell Inc.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080016 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080017 * This file is released under the GPLv2
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080018 */
19
20#include <linux/device.h>
Arjan van de Ven216773a2009-02-14 01:59:06 +010021#include <linux/delay.h>
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080022#include <linux/module.h>
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -070023#include <linux/kthread.h>
Andrew Morton735a7ff2006-10-27 11:42:37 -070024#include <linux/wait.h>
Arjan van de Ven216773a2009-02-14 01:59:06 +010025#include <linux/async.h>
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020026#include <linux/pm_runtime.h>
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080027
28#include "base.h"
29#include "power/power.h"
30
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080031
Kay Sievers1901fb22006-10-07 21:55:55 +020032static void driver_bound(struct device *dev)
33{
Greg Kroah-Hartman8940b4f2008-12-16 12:25:49 -080034 if (klist_node_attached(&dev->p->knode_driver)) {
Kay Sievers1901fb22006-10-07 21:55:55 +020035 printk(KERN_WARNING "%s: device %s already bound\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -080036 __func__, kobject_name(&dev->kobj));
Kay Sievers1901fb22006-10-07 21:55:55 +020037 return;
38 }
39
Kay Sievers1e0b2cf2008-10-30 01:36:48 +010040 pr_debug("driver: '%s': %s: bound to device '%s'\n", dev_name(dev),
Harvey Harrison2b3a3022008-03-04 16:41:05 -080041 __func__, dev->driver->name);
Kay Sievers1901fb22006-10-07 21:55:55 +020042
43 if (dev->bus)
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070044 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
Kay Sievers1901fb22006-10-07 21:55:55 +020045 BUS_NOTIFY_BOUND_DRIVER, dev);
46
Greg Kroah-Hartman8940b4f2008-12-16 12:25:49 -080047 klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
Kay Sievers1901fb22006-10-07 21:55:55 +020048}
49
50static int driver_sysfs_add(struct device *dev)
51{
52 int ret;
53
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080054 ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
Kay Sievers1901fb22006-10-07 21:55:55 +020055 kobject_name(&dev->kobj));
56 if (ret == 0) {
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080057 ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
Kay Sievers1901fb22006-10-07 21:55:55 +020058 "driver");
59 if (ret)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080060 sysfs_remove_link(&dev->driver->p->kobj,
Kay Sievers1901fb22006-10-07 21:55:55 +020061 kobject_name(&dev->kobj));
62 }
63 return ret;
64}
65
66static void driver_sysfs_remove(struct device *dev)
67{
68 struct device_driver *drv = dev->driver;
69
70 if (drv) {
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080071 sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
Kay Sievers1901fb22006-10-07 21:55:55 +020072 sysfs_remove_link(&dev->kobj, "driver");
73 }
74}
75
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080076/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080077 * device_bind_driver - bind a driver to one device.
78 * @dev: device.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080079 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080080 * Allow manual attachment of a driver to a device.
81 * Caller must have already set @dev->driver.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080082 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080083 * Note that this does not modify the bus reference count
84 * nor take the bus's rwsem. Please verify those are accounted
85 * for before calling this. (It is ok to call with no other effort
86 * from a driver's probe() method.)
Patrick Mochel0d3e5a22005-04-05 23:46:33 -070087 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080088 * This function must be called with @dev->sem held.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080089 */
Andrew Mortonf86db392006-08-14 22:43:20 -070090int device_bind_driver(struct device *dev)
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080091{
Cornelia Huckcb986b72006-11-27 10:35:12 +010092 int ret;
93
94 ret = driver_sysfs_add(dev);
95 if (!ret)
96 driver_bound(dev);
97 return ret;
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080098}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080099EXPORT_SYMBOL_GPL(device_bind_driver);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800100
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700101static atomic_t probe_count = ATOMIC_INIT(0);
Andrew Morton735a7ff2006-10-27 11:42:37 -0700102static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
103
Cornelia Huck21c7f302007-02-05 16:15:25 -0800104static int really_probe(struct device *dev, struct device_driver *drv)
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800105{
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700106 int ret = 0;
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800107
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700108 atomic_inc(&probe_count);
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800109 pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100110 drv->bus->name, __func__, drv->name, dev_name(dev));
Tejun Heo9ac78492007-01-20 16:00:26 +0900111 WARN_ON(!list_empty(&dev->devres_head));
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800112
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800113 dev->driver = drv;
Kay Sievers1901fb22006-10-07 21:55:55 +0200114 if (driver_sysfs_add(dev)) {
115 printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100116 __func__, dev_name(dev));
Kay Sievers1901fb22006-10-07 21:55:55 +0200117 goto probe_failed;
118 }
119
Russell King594c8282006-01-05 14:29:51 +0000120 if (dev->bus->probe) {
121 ret = dev->bus->probe(dev);
Kay Sievers1901fb22006-10-07 21:55:55 +0200122 if (ret)
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700123 goto probe_failed;
Russell King594c8282006-01-05 14:29:51 +0000124 } else if (drv->probe) {
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700125 ret = drv->probe(dev);
Kay Sievers1901fb22006-10-07 21:55:55 +0200126 if (ret)
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700127 goto probe_failed;
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800128 }
Kay Sievers1901fb22006-10-07 21:55:55 +0200129
130 driver_bound(dev);
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700131 ret = 1;
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800132 pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100133 drv->bus->name, __func__, dev_name(dev), drv->name);
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700134 goto done;
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700135
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700136probe_failed:
Tejun Heo9ac78492007-01-20 16:00:26 +0900137 devres_release_all(dev);
Kay Sievers1901fb22006-10-07 21:55:55 +0200138 driver_sysfs_remove(dev);
139 dev->driver = NULL;
140
Cornelia Huckc578abb2006-11-27 10:35:10 +0100141 if (ret != -ENODEV && ret != -ENXIO) {
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700142 /* driver matched but the probe failed */
143 printk(KERN_WARNING
144 "%s: probe of %s failed with error %d\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100145 drv->name, dev_name(dev), ret);
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700146 }
Cornelia Huckc578abb2006-11-27 10:35:10 +0100147 /*
148 * Ignore errors returned by ->probe so that the next driver can try
149 * its luck.
150 */
151 ret = 0;
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700152done:
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700153 atomic_dec(&probe_count);
Andrew Morton735a7ff2006-10-27 11:42:37 -0700154 wake_up(&probe_waitqueue);
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700155 return ret;
156}
157
158/**
159 * driver_probe_done
160 * Determine if the probe sequence is finished or not.
161 *
162 * Should somehow figure out how to use a semaphore, not an atomic variable...
163 */
164int driver_probe_done(void)
165{
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800166 pr_debug("%s: probe_count = %d\n", __func__,
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700167 atomic_read(&probe_count));
168 if (atomic_read(&probe_count))
169 return -EBUSY;
170 return 0;
171}
172
173/**
Arjan van de Ven216773a2009-02-14 01:59:06 +0100174 * wait_for_device_probe
175 * Wait for device probing to be completed.
Arjan van de Ven216773a2009-02-14 01:59:06 +0100176 */
Ming Leib23530e2009-02-21 16:45:07 +0800177void wait_for_device_probe(void)
Arjan van de Ven216773a2009-02-14 01:59:06 +0100178{
179 /* wait for the known devices to complete their probing */
Ming Leib23530e2009-02-21 16:45:07 +0800180 wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
Arjan van de Ven216773a2009-02-14 01:59:06 +0100181 async_synchronize_full();
Arjan van de Ven216773a2009-02-14 01:59:06 +0100182}
Arjan van de Vend4d52912009-04-21 13:32:54 -0700183EXPORT_SYMBOL_GPL(wait_for_device_probe);
Arjan van de Ven216773a2009-02-14 01:59:06 +0100184
185/**
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700186 * driver_probe_device - attempt to bind device & driver together
187 * @drv: driver to bind a device to
188 * @dev: device to try to bind to the driver
189 *
Ming Lei49b420a2009-01-21 23:27:47 +0800190 * This function returns -ENODEV if the device is not registered,
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200191 * 1 if the device is bound successfully and 0 otherwise.
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700192 *
193 * This function must be called with @dev->sem held. When called for a
194 * USB interface, @dev->parent->sem must be held as well.
195 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800196int driver_probe_device(struct device_driver *drv, struct device *dev)
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700197{
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700198 int ret = 0;
199
Alan Sternf2eaae12006-09-18 16:22:34 -0400200 if (!device_is_registered(dev))
201 return -ENODEV;
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700202
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800203 pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
Kay Sievers1e0b2cf2008-10-30 01:36:48 +0100204 drv->bus->name, __func__, dev_name(dev), drv->name);
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700205
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200206 pm_runtime_get_noresume(dev);
207 pm_runtime_barrier(dev);
Cornelia Huck21c7f302007-02-05 16:15:25 -0800208 ret = really_probe(dev, drv);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200209 pm_runtime_put_sync(dev);
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700210
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700211 return ret;
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800212}
213
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800214static int __device_attach(struct device_driver *drv, void *data)
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800215{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800216 struct device *dev = data;
Ming Lei49b420a2009-01-21 23:27:47 +0800217
218 if (!driver_match_device(drv, dev))
219 return 0;
220
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700221 return driver_probe_device(drv, dev);
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800222}
223
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800224/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800225 * device_attach - try to attach device to a driver.
226 * @dev: device.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800227 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800228 * Walk the list of drivers that the bus has and call
229 * driver_probe_device() for each pair. If a compatible
230 * pair is found, break out and return.
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700231 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800232 * Returns 1 if the device was bound to a driver;
Dmitry Torokhov59a3cd72009-05-05 20:38:28 -0700233 * 0 if no matching driver was found;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800234 * -ENODEV if the device is not registered.
Alan Sternbf74ad52005-11-17 16:54:12 -0500235 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800236 * When called for a USB interface, @dev->parent->sem must be held.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800237 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800238int device_attach(struct device *dev)
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800239{
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700240 int ret = 0;
241
242 down(&dev->sem);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800243 if (dev->driver) {
Andrew Mortonf86db392006-08-14 22:43:20 -0700244 ret = device_bind_driver(dev);
245 if (ret == 0)
246 ret = 1;
Cornelia Huckc6a46692007-02-05 16:15:26 -0800247 else {
248 dev->driver = NULL;
249 ret = 0;
250 }
Cornelia Huck21c7f302007-02-05 16:15:25 -0800251 } else {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200252 pm_runtime_get_noresume(dev);
Adrian Bunk5adc55d2007-03-27 03:02:51 +0200253 ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200254 pm_runtime_put_sync(dev);
Cornelia Huck21c7f302007-02-05 16:15:25 -0800255 }
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700256 up(&dev->sem);
257 return ret;
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800258}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800259EXPORT_SYMBOL_GPL(device_attach);
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800260
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800261static int __driver_attach(struct device *dev, void *data)
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800262{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800263 struct device_driver *drv = data;
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800264
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700265 /*
266 * Lock device and try to bind to it. We drop the error
267 * here and always return 0, because we need to keep trying
268 * to bind to devices and some drivers will return an error
269 * simply if it didn't support the device.
270 *
271 * driver_probe_device() will spit a warning if there
272 * is an error.
273 */
274
Ming Lei49b420a2009-01-21 23:27:47 +0800275 if (!driver_match_device(drv, dev))
Arjan van de Ven6cd49582008-09-14 08:32:06 -0700276 return 0;
277
Alan Sternbf74ad52005-11-17 16:54:12 -0500278 if (dev->parent) /* Needed for USB */
279 down(&dev->parent->sem);
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700280 down(&dev->sem);
281 if (!dev->driver)
282 driver_probe_device(drv, dev);
283 up(&dev->sem);
Alan Sternbf74ad52005-11-17 16:54:12 -0500284 if (dev->parent)
285 up(&dev->parent->sem);
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700286
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800287 return 0;
288}
289
290/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800291 * driver_attach - try to bind driver to devices.
292 * @drv: driver.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800293 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800294 * Walk the list of devices that the bus has on it and try to
295 * match the driver with each one. If driver_probe_device()
296 * returns 0 and the @dev->driver is set, we've found a
297 * compatible pair.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800298 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800299int driver_attach(struct device_driver *drv)
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800300{
Andrew Mortonf86db392006-08-14 22:43:20 -0700301 return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800302}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800303EXPORT_SYMBOL_GPL(driver_attach);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800304
Stefan Richterab71c6f2007-06-17 11:02:12 +0200305/*
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800306 * __device_release_driver() must be called with @dev->sem held.
307 * When called for a USB interface, @dev->parent->sem must be held as well.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800308 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800309static void __device_release_driver(struct device *dev)
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800310{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800311 struct device_driver *drv;
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800312
Alan Sternef2c5172007-11-16 11:57:28 -0500313 drv = dev->driver;
Alan Sternc95a6b02005-05-06 15:38:33 -0400314 if (drv) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200315 pm_runtime_get_noresume(dev);
316 pm_runtime_barrier(dev);
317
Kay Sievers1901fb22006-10-07 21:55:55 +0200318 driver_sysfs_remove(dev);
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700319
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000320 if (dev->bus)
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700321 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000322 BUS_NOTIFY_UNBIND_DRIVER,
323 dev);
324
Alan Stern0f836ca2006-03-31 11:52:25 -0500325 if (dev->bus && dev->bus->remove)
Russell King594c8282006-01-05 14:29:51 +0000326 dev->bus->remove(dev);
327 else if (drv->remove)
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700328 drv->remove(dev);
Tejun Heo9ac78492007-01-20 16:00:26 +0900329 devres_release_all(dev);
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700330 dev->driver = NULL;
Greg Kroah-Hartman8940b4f2008-12-16 12:25:49 -0800331 klist_remove(&dev->p->knode_driver);
Joerg Roedel309b7d62009-04-24 14:57:00 +0200332 if (dev->bus)
333 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
334 BUS_NOTIFY_UNBOUND_DRIVER,
335 dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200336
337 pm_runtime_put_sync(dev);
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700338 }
Alan Sternc95a6b02005-05-06 15:38:33 -0400339}
340
Stefan Richterab71c6f2007-06-17 11:02:12 +0200341/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800342 * device_release_driver - manually detach device from driver.
343 * @dev: device.
Stefan Richterab71c6f2007-06-17 11:02:12 +0200344 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800345 * Manually detach device from driver.
346 * When called for a USB interface, @dev->parent->sem must be held.
Stefan Richterab71c6f2007-06-17 11:02:12 +0200347 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800348void device_release_driver(struct device *dev)
Alan Sternc95a6b02005-05-06 15:38:33 -0400349{
350 /*
351 * If anyone calls device_release_driver() recursively from
352 * within their ->remove callback for the same device, they
353 * will deadlock right here.
354 */
355 down(&dev->sem);
356 __device_release_driver(dev);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800357 up(&dev->sem);
358}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800359EXPORT_SYMBOL_GPL(device_release_driver);
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -0800360
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800361/**
362 * driver_detach - detach driver from all devices it controls.
363 * @drv: driver.
364 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800365void driver_detach(struct device_driver *drv)
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800366{
Greg Kroah-Hartman8940b4f2008-12-16 12:25:49 -0800367 struct device_private *dev_prv;
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800368 struct device *dev;
Alan Sternc95a6b02005-05-06 15:38:33 -0400369
370 for (;;) {
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800371 spin_lock(&drv->p->klist_devices.k_lock);
372 if (list_empty(&drv->p->klist_devices.k_list)) {
373 spin_unlock(&drv->p->klist_devices.k_lock);
Alan Sternc95a6b02005-05-06 15:38:33 -0400374 break;
375 }
Greg Kroah-Hartman8940b4f2008-12-16 12:25:49 -0800376 dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
377 struct device_private,
378 knode_driver.n_node);
379 dev = dev_prv->device;
Alan Sternc95a6b02005-05-06 15:38:33 -0400380 get_device(dev);
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800381 spin_unlock(&drv->p->klist_devices.k_lock);
Alan Sternc95a6b02005-05-06 15:38:33 -0400382
Alan Sternbf74ad52005-11-17 16:54:12 -0500383 if (dev->parent) /* Needed for USB */
384 down(&dev->parent->sem);
Alan Sternc95a6b02005-05-06 15:38:33 -0400385 down(&dev->sem);
386 if (dev->driver == drv)
387 __device_release_driver(dev);
388 up(&dev->sem);
Alan Sternbf74ad52005-11-17 16:54:12 -0500389 if (dev->parent)
390 up(&dev->parent->sem);
Alan Sternc95a6b02005-05-06 15:38:33 -0400391 put_device(dev);
392 }
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800393}
Greg Kroah-Hartmanb4028432009-05-11 14:16:57 -0700394
395/*
396 * These exports can't be _GPL due to .h files using this within them, and it
397 * might break something that was previously working...
398 */
399void *dev_get_drvdata(const struct device *dev)
400{
401 if (dev && dev->p)
402 return dev->p->driver_data;
403 return NULL;
404}
405EXPORT_SYMBOL(dev_get_drvdata);
406
407void dev_set_drvdata(struct device *dev, void *data)
408{
409 int error;
410
411 if (!dev)
412 return;
413 if (!dev->p) {
414 error = device_private_init(dev);
415 if (error)
416 return;
417 }
418 dev->p->driver_data = data;
419}
420EXPORT_SYMBOL(dev_set_drvdata);