blob: 20febc00a5258e253aeb9a309a9447660df40a38 [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
14 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
15 * Copyright (c) 2007 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>
21#include <linux/module.h>
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -070022#include <linux/kthread.h>
Andrew Morton735a7ff2006-10-27 11:42:37 -070023#include <linux/wait.h>
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080024
25#include "base.h"
26#include "power/power.h"
27
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080028
Kay Sievers1901fb22006-10-07 21:55:55 +020029static void driver_bound(struct device *dev)
30{
31 if (klist_node_attached(&dev->knode_driver)) {
32 printk(KERN_WARNING "%s: device %s already bound\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -080033 __func__, kobject_name(&dev->kobj));
Kay Sievers1901fb22006-10-07 21:55:55 +020034 return;
35 }
36
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -080037 pr_debug("driver: '%s': %s: bound to device '%s'\n", dev->bus_id,
Harvey Harrison2b3a3022008-03-04 16:41:05 -080038 __func__, dev->driver->name);
Kay Sievers1901fb22006-10-07 21:55:55 +020039
40 if (dev->bus)
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070041 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
Kay Sievers1901fb22006-10-07 21:55:55 +020042 BUS_NOTIFY_BOUND_DRIVER, dev);
43
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080044 klist_add_tail(&dev->knode_driver, &dev->driver->p->klist_devices);
Kay Sievers1901fb22006-10-07 21:55:55 +020045}
46
47static int driver_sysfs_add(struct device *dev)
48{
49 int ret;
50
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080051 ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
Kay Sievers1901fb22006-10-07 21:55:55 +020052 kobject_name(&dev->kobj));
53 if (ret == 0) {
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080054 ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
Kay Sievers1901fb22006-10-07 21:55:55 +020055 "driver");
56 if (ret)
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080057 sysfs_remove_link(&dev->driver->p->kobj,
Kay Sievers1901fb22006-10-07 21:55:55 +020058 kobject_name(&dev->kobj));
59 }
60 return ret;
61}
62
63static void driver_sysfs_remove(struct device *dev)
64{
65 struct device_driver *drv = dev->driver;
66
67 if (drv) {
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -080068 sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
Kay Sievers1901fb22006-10-07 21:55:55 +020069 sysfs_remove_link(&dev->kobj, "driver");
70 }
71}
72
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080073/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080074 * device_bind_driver - bind a driver to one device.
75 * @dev: device.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080076 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080077 * Allow manual attachment of a driver to a device.
78 * Caller must have already set @dev->driver.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080079 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080080 * Note that this does not modify the bus reference count
81 * nor take the bus's rwsem. Please verify those are accounted
82 * for before calling this. (It is ok to call with no other effort
83 * from a driver's probe() method.)
Patrick Mochel0d3e5a22005-04-05 23:46:33 -070084 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080085 * This function must be called with @dev->sem held.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080086 */
Andrew Mortonf86db392006-08-14 22:43:20 -070087int device_bind_driver(struct device *dev)
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080088{
Cornelia Huckcb986b72006-11-27 10:35:12 +010089 int ret;
90
91 ret = driver_sysfs_add(dev);
92 if (!ret)
93 driver_bound(dev);
94 return ret;
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080095}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -080096EXPORT_SYMBOL_GPL(device_bind_driver);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -080097
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -070098static atomic_t probe_count = ATOMIC_INIT(0);
Andrew Morton735a7ff2006-10-27 11:42:37 -070099static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
100
Cornelia Huck21c7f302007-02-05 16:15:25 -0800101static int really_probe(struct device *dev, struct device_driver *drv)
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800102{
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700103 int ret = 0;
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800104
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700105 atomic_inc(&probe_count);
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800106 pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800107 drv->bus->name, __func__, drv->name, dev->bus_id);
Tejun Heo9ac78492007-01-20 16:00:26 +0900108 WARN_ON(!list_empty(&dev->devres_head));
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800109
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800110 dev->driver = drv;
Kay Sievers1901fb22006-10-07 21:55:55 +0200111 if (driver_sysfs_add(dev)) {
112 printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800113 __func__, dev->bus_id);
Kay Sievers1901fb22006-10-07 21:55:55 +0200114 goto probe_failed;
115 }
116
Russell King594c8282006-01-05 14:29:51 +0000117 if (dev->bus->probe) {
118 ret = dev->bus->probe(dev);
Kay Sievers1901fb22006-10-07 21:55:55 +0200119 if (ret)
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700120 goto probe_failed;
Russell King594c8282006-01-05 14:29:51 +0000121 } else if (drv->probe) {
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700122 ret = drv->probe(dev);
Kay Sievers1901fb22006-10-07 21:55:55 +0200123 if (ret)
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700124 goto probe_failed;
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800125 }
Kay Sievers1901fb22006-10-07 21:55:55 +0200126
127 driver_bound(dev);
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700128 ret = 1;
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800129 pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800130 drv->bus->name, __func__, dev->bus_id, drv->name);
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700131 goto done;
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700132
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700133probe_failed:
Tejun Heo9ac78492007-01-20 16:00:26 +0900134 devres_release_all(dev);
Kay Sievers1901fb22006-10-07 21:55:55 +0200135 driver_sysfs_remove(dev);
136 dev->driver = NULL;
137
Cornelia Huckc578abb2006-11-27 10:35:10 +0100138 if (ret != -ENODEV && ret != -ENXIO) {
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700139 /* driver matched but the probe failed */
140 printk(KERN_WARNING
141 "%s: probe of %s failed with error %d\n",
142 drv->name, dev->bus_id, ret);
143 }
Cornelia Huckc578abb2006-11-27 10:35:10 +0100144 /*
145 * Ignore errors returned by ->probe so that the next driver can try
146 * its luck.
147 */
148 ret = 0;
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700149done:
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700150 atomic_dec(&probe_count);
Andrew Morton735a7ff2006-10-27 11:42:37 -0700151 wake_up(&probe_waitqueue);
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700152 return ret;
153}
154
155/**
156 * driver_probe_done
157 * Determine if the probe sequence is finished or not.
158 *
159 * Should somehow figure out how to use a semaphore, not an atomic variable...
160 */
161int driver_probe_done(void)
162{
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800163 pr_debug("%s: probe_count = %d\n", __func__,
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700164 atomic_read(&probe_count));
165 if (atomic_read(&probe_count))
166 return -EBUSY;
167 return 0;
168}
169
170/**
171 * driver_probe_device - attempt to bind device & driver together
172 * @drv: driver to bind a device to
173 * @dev: device to try to bind to the driver
174 *
175 * First, we call the bus's match function, if one present, which should
176 * compare the device IDs the driver supports with the device IDs of the
177 * device. Note we don't do this ourselves because we don't know the
178 * format of the ID structures, nor what is to be considered a match and
179 * what is not.
180 *
Cornelia Huck21c7f302007-02-05 16:15:25 -0800181 * This function returns 1 if a match is found, -ENODEV if the device is
182 * not registered, and 0 otherwise.
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700183 *
184 * This function must be called with @dev->sem held. When called for a
185 * USB interface, @dev->parent->sem must be held as well.
186 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800187int driver_probe_device(struct device_driver *drv, struct device *dev)
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700188{
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700189 int ret = 0;
190
Alan Sternf2eaae12006-09-18 16:22:34 -0400191 if (!device_is_registered(dev))
192 return -ENODEV;
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700193 if (drv->bus->match && !drv->bus->match(dev, drv))
194 goto done;
195
Greg Kroah-Hartman7dc72b22007-11-28 23:49:41 -0800196 pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800197 drv->bus->name, __func__, dev->bus_id, drv->name);
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700198
Cornelia Huck21c7f302007-02-05 16:15:25 -0800199 ret = really_probe(dev, drv);
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -0700200
201done:
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700202 return ret;
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800203}
204
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800205static int __device_attach(struct device_driver *drv, void *data)
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800206{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800207 struct device *dev = data;
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700208 return driver_probe_device(drv, dev);
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800209}
210
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800211/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800212 * device_attach - try to attach device to a driver.
213 * @dev: device.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800214 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800215 * Walk the list of drivers that the bus has and call
216 * driver_probe_device() for each pair. If a compatible
217 * pair is found, break out and return.
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700218 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800219 * Returns 1 if the device was bound to a driver;
220 * 0 if no matching device was found;
221 * -ENODEV if the device is not registered.
Alan Sternbf74ad52005-11-17 16:54:12 -0500222 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800223 * When called for a USB interface, @dev->parent->sem must be held.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800224 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800225int device_attach(struct device *dev)
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800226{
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700227 int ret = 0;
228
229 down(&dev->sem);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800230 if (dev->driver) {
Andrew Mortonf86db392006-08-14 22:43:20 -0700231 ret = device_bind_driver(dev);
232 if (ret == 0)
233 ret = 1;
Cornelia Huckc6a46692007-02-05 16:15:26 -0800234 else {
235 dev->driver = NULL;
236 ret = 0;
237 }
Cornelia Huck21c7f302007-02-05 16:15:25 -0800238 } else {
Adrian Bunk5adc55d2007-03-27 03:02:51 +0200239 ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
Cornelia Huck21c7f302007-02-05 16:15:25 -0800240 }
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700241 up(&dev->sem);
242 return ret;
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800243}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800244EXPORT_SYMBOL_GPL(device_attach);
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800245
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800246static int __driver_attach(struct device *dev, void *data)
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800247{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800248 struct device_driver *drv = data;
mochel@digitalimplant.org2287c322005-03-24 10:50:24 -0800249
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700250 /*
251 * Lock device and try to bind to it. We drop the error
252 * here and always return 0, because we need to keep trying
253 * to bind to devices and some drivers will return an error
254 * simply if it didn't support the device.
255 *
256 * driver_probe_device() will spit a warning if there
257 * is an error.
258 */
259
Arjan van de Ven6cd49582008-09-14 08:32:06 -0700260 if (drv->bus->match && !drv->bus->match(dev, drv))
261 return 0;
262
Alan Sternbf74ad52005-11-17 16:54:12 -0500263 if (dev->parent) /* Needed for USB */
264 down(&dev->parent->sem);
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700265 down(&dev->sem);
266 if (!dev->driver)
267 driver_probe_device(drv, dev);
268 up(&dev->sem);
Alan Sternbf74ad52005-11-17 16:54:12 -0500269 if (dev->parent)
270 up(&dev->parent->sem);
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700271
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800272 return 0;
273}
274
275/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800276 * driver_attach - try to bind driver to devices.
277 * @drv: driver.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800278 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800279 * Walk the list of devices that the bus has on it and try to
280 * match the driver with each one. If driver_probe_device()
281 * returns 0 and the @dev->driver is set, we've found a
282 * compatible pair.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800283 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800284int driver_attach(struct device_driver *drv)
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800285{
Andrew Mortonf86db392006-08-14 22:43:20 -0700286 return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800287}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800288EXPORT_SYMBOL_GPL(driver_attach);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800289
Stefan Richterab71c6f2007-06-17 11:02:12 +0200290/*
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800291 * __device_release_driver() must be called with @dev->sem held.
292 * When called for a USB interface, @dev->parent->sem must be held as well.
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800293 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800294static void __device_release_driver(struct device *dev)
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800295{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800296 struct device_driver *drv;
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800297
Alan Sternef2c5172007-11-16 11:57:28 -0500298 drv = dev->driver;
Alan Sternc95a6b02005-05-06 15:38:33 -0400299 if (drv) {
Kay Sievers1901fb22006-10-07 21:55:55 +0200300 driver_sysfs_remove(dev);
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700301 sysfs_remove_link(&dev->kobj, "driver");
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700302
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000303 if (dev->bus)
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700304 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000305 BUS_NOTIFY_UNBIND_DRIVER,
306 dev);
307
Alan Stern0f836ca2006-03-31 11:52:25 -0500308 if (dev->bus && dev->bus->remove)
Russell King594c8282006-01-05 14:29:51 +0000309 dev->bus->remove(dev);
310 else if (drv->remove)
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700311 drv->remove(dev);
Tejun Heo9ac78492007-01-20 16:00:26 +0900312 devres_release_all(dev);
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700313 dev->driver = NULL;
Alan Sternef2c5172007-11-16 11:57:28 -0500314 klist_remove(&dev->knode_driver);
Patrick Mochel0d3e5a22005-04-05 23:46:33 -0700315 }
Alan Sternc95a6b02005-05-06 15:38:33 -0400316}
317
Stefan Richterab71c6f2007-06-17 11:02:12 +0200318/**
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800319 * device_release_driver - manually detach device from driver.
320 * @dev: device.
Stefan Richterab71c6f2007-06-17 11:02:12 +0200321 *
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800322 * Manually detach device from driver.
323 * When called for a USB interface, @dev->parent->sem must be held.
Stefan Richterab71c6f2007-06-17 11:02:12 +0200324 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800325void device_release_driver(struct device *dev)
Alan Sternc95a6b02005-05-06 15:38:33 -0400326{
327 /*
328 * If anyone calls device_release_driver() recursively from
329 * within their ->remove callback for the same device, they
330 * will deadlock right here.
331 */
332 down(&dev->sem);
333 __device_release_driver(dev);
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800334 up(&dev->sem);
335}
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800336EXPORT_SYMBOL_GPL(device_release_driver);
mochel@digitalimplant.org94e7b1c52005-03-21 12:25:36 -0800337
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800338/**
339 * driver_detach - detach driver from all devices it controls.
340 * @drv: driver.
341 */
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800342void driver_detach(struct device_driver *drv)
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800343{
Greg Kroah-Hartman4a3ad202008-01-24 22:50:12 -0800344 struct device *dev;
Alan Sternc95a6b02005-05-06 15:38:33 -0400345
346 for (;;) {
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800347 spin_lock(&drv->p->klist_devices.k_lock);
348 if (list_empty(&drv->p->klist_devices.k_list)) {
349 spin_unlock(&drv->p->klist_devices.k_lock);
Alan Sternc95a6b02005-05-06 15:38:33 -0400350 break;
351 }
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800352 dev = list_entry(drv->p->klist_devices.k_list.prev,
Alan Sternc95a6b02005-05-06 15:38:33 -0400353 struct device, knode_driver.n_node);
354 get_device(dev);
Greg Kroah-Hartmane5dd1272007-11-28 15:59:15 -0800355 spin_unlock(&drv->p->klist_devices.k_lock);
Alan Sternc95a6b02005-05-06 15:38:33 -0400356
Alan Sternbf74ad52005-11-17 16:54:12 -0500357 if (dev->parent) /* Needed for USB */
358 down(&dev->parent->sem);
Alan Sternc95a6b02005-05-06 15:38:33 -0400359 down(&dev->sem);
360 if (dev->driver == drv)
361 __device_release_driver(dev);
362 up(&dev->sem);
Alan Sternbf74ad52005-11-17 16:54:12 -0500363 if (dev->parent)
364 up(&dev->parent->sem);
Alan Sternc95a6b02005-05-06 15:38:33 -0400365 put_device(dev);
366 }
mochel@digitalimplant.org07e4a3e2005-03-21 10:52:54 -0800367}