Commit | Line | Data |
---|---|---|
07e4a3e2 | 1 | /* |
4a3ad20c | 2 | * drivers/base/dd.c - The core device/driver interactions. |
07e4a3e2 | 3 | * |
4a3ad20c GKH |
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. | |
07e4a3e2 | 7 | * |
4a3ad20c GKH |
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'. | |
07e4a3e2 | 11 | * |
4a3ad20c GKH |
12 | * Copyright (c) 2002-5 Patrick Mochel |
13 | * Copyright (c) 2002-3 Open Source Development Labs | |
b4028437 GKH |
14 | * Copyright (c) 2007-2009 Greg Kroah-Hartman <gregkh@suse.de> |
15 | * Copyright (c) 2007-2009 Novell Inc. | |
07e4a3e2 | 16 | * |
4a3ad20c | 17 | * This file is released under the GPLv2 |
07e4a3e2 PM |
18 | */ |
19 | ||
20 | #include <linux/device.h> | |
216773a7 | 21 | #include <linux/delay.h> |
07e4a3e2 | 22 | #include <linux/module.h> |
d779249e | 23 | #include <linux/kthread.h> |
735a7ffb | 24 | #include <linux/wait.h> |
216773a7 | 25 | #include <linux/async.h> |
5e928f77 | 26 | #include <linux/pm_runtime.h> |
ab78029e | 27 | #include <linux/pinctrl/devinfo.h> |
6fa3eb70 | 28 | #include <linux/time_log.h> |
07e4a3e2 PM |
29 | |
30 | #include "base.h" | |
31 | #include "power/power.h" | |
32 | ||
d1c3414c GL |
33 | /* |
34 | * Deferred Probe infrastructure. | |
35 | * | |
36 | * Sometimes driver probe order matters, but the kernel doesn't always have | |
37 | * dependency information which means some drivers will get probed before a | |
38 | * resource it depends on is available. For example, an SDHCI driver may | |
39 | * first need a GPIO line from an i2c GPIO controller before it can be | |
40 | * initialized. If a required resource is not available yet, a driver can | |
41 | * request probing to be deferred by returning -EPROBE_DEFER from its probe hook | |
42 | * | |
43 | * Deferred probe maintains two lists of devices, a pending list and an active | |
44 | * list. A driver returning -EPROBE_DEFER causes the device to be added to the | |
45 | * pending list. A successful driver probe will trigger moving all devices | |
46 | * from the pending to the active list so that the workqueue will eventually | |
47 | * retry them. | |
48 | * | |
49 | * The deferred_probe_mutex must be held any time the deferred_probe_*_list | |
ef8a3fd6 | 50 | * of the (struct device*)->p->deferred_probe pointers are manipulated |
d1c3414c GL |
51 | */ |
52 | static DEFINE_MUTEX(deferred_probe_mutex); | |
53 | static LIST_HEAD(deferred_probe_pending_list); | |
54 | static LIST_HEAD(deferred_probe_active_list); | |
55 | static struct workqueue_struct *deferred_wq; | |
f1c8467e | 56 | static atomic_t deferred_trigger_count = ATOMIC_INIT(0); |
d1c3414c GL |
57 | |
58 | /** | |
59 | * deferred_probe_work_func() - Retry probing devices in the active list. | |
60 | */ | |
61 | static void deferred_probe_work_func(struct work_struct *work) | |
62 | { | |
63 | struct device *dev; | |
ef8a3fd6 | 64 | struct device_private *private; |
d1c3414c GL |
65 | /* |
66 | * This block processes every device in the deferred 'active' list. | |
67 | * Each device is removed from the active list and passed to | |
68 | * bus_probe_device() to re-attempt the probe. The loop continues | |
69 | * until every device in the active list is removed and retried. | |
70 | * | |
71 | * Note: Once the device is removed from the list and the mutex is | |
72 | * released, it is possible for the device get freed by another thread | |
73 | * and cause a illegal pointer dereference. This code uses | |
74 | * get/put_device() to ensure the device structure cannot disappear | |
75 | * from under our feet. | |
76 | */ | |
77 | mutex_lock(&deferred_probe_mutex); | |
78 | while (!list_empty(&deferred_probe_active_list)) { | |
ef8a3fd6 GKH |
79 | private = list_first_entry(&deferred_probe_active_list, |
80 | typeof(*dev->p), deferred_probe); | |
81 | dev = private->device; | |
82 | list_del_init(&private->deferred_probe); | |
d1c3414c GL |
83 | |
84 | get_device(dev); | |
85 | ||
8b0372a2 GKH |
86 | /* |
87 | * Drop the mutex while probing each device; the probe path may | |
88 | * manipulate the deferred list | |
89 | */ | |
d1c3414c | 90 | mutex_unlock(&deferred_probe_mutex); |
8153584e MB |
91 | |
92 | /* | |
93 | * Force the device to the end of the dpm_list since | |
94 | * the PM code assumes that the order we add things to | |
95 | * the list is a good order for suspend but deferred | |
96 | * probe makes that very unsafe. | |
97 | */ | |
98 | device_pm_lock(); | |
99 | device_pm_move_last(dev); | |
100 | device_pm_unlock(); | |
101 | ||
d1c3414c GL |
102 | dev_dbg(dev, "Retrying from deferred list\n"); |
103 | bus_probe_device(dev); | |
8153584e | 104 | |
d1c3414c GL |
105 | mutex_lock(&deferred_probe_mutex); |
106 | ||
107 | put_device(dev); | |
108 | } | |
109 | mutex_unlock(&deferred_probe_mutex); | |
110 | } | |
111 | static DECLARE_WORK(deferred_probe_work, deferred_probe_work_func); | |
112 | ||
113 | static void driver_deferred_probe_add(struct device *dev) | |
114 | { | |
115 | mutex_lock(&deferred_probe_mutex); | |
ef8a3fd6 | 116 | if (list_empty(&dev->p->deferred_probe)) { |
d1c3414c | 117 | dev_dbg(dev, "Added to deferred list\n"); |
1d29cfa5 | 118 | list_add_tail(&dev->p->deferred_probe, &deferred_probe_pending_list); |
d1c3414c GL |
119 | } |
120 | mutex_unlock(&deferred_probe_mutex); | |
121 | } | |
122 | ||
123 | void driver_deferred_probe_del(struct device *dev) | |
124 | { | |
125 | mutex_lock(&deferred_probe_mutex); | |
ef8a3fd6 | 126 | if (!list_empty(&dev->p->deferred_probe)) { |
d1c3414c | 127 | dev_dbg(dev, "Removed from deferred list\n"); |
ef8a3fd6 | 128 | list_del_init(&dev->p->deferred_probe); |
d1c3414c GL |
129 | } |
130 | mutex_unlock(&deferred_probe_mutex); | |
131 | } | |
132 | ||
133 | static bool driver_deferred_probe_enable = false; | |
134 | /** | |
135 | * driver_deferred_probe_trigger() - Kick off re-probing deferred devices | |
136 | * | |
137 | * This functions moves all devices from the pending list to the active | |
138 | * list and schedules the deferred probe workqueue to process them. It | |
139 | * should be called anytime a driver is successfully bound to a device. | |
f1c8467e GL |
140 | * |
141 | * Note, there is a race condition in multi-threaded probe. In the case where | |
142 | * more than one device is probing at the same time, it is possible for one | |
143 | * probe to complete successfully while another is about to defer. If the second | |
144 | * depends on the first, then it will get put on the pending list after the | |
145 | * trigger event has already occured and will be stuck there. | |
146 | * | |
147 | * The atomic 'deferred_trigger_count' is used to determine if a successful | |
148 | * trigger has occurred in the midst of probing a driver. If the trigger count | |
149 | * changes in the midst of a probe, then deferred processing should be triggered | |
150 | * again. | |
d1c3414c GL |
151 | */ |
152 | static void driver_deferred_probe_trigger(void) | |
153 | { | |
154 | if (!driver_deferred_probe_enable) | |
155 | return; | |
156 | ||
8b0372a2 GKH |
157 | /* |
158 | * A successful probe means that all the devices in the pending list | |
d1c3414c | 159 | * should be triggered to be reprobed. Move all the deferred devices |
8b0372a2 GKH |
160 | * into the active list so they can be retried by the workqueue |
161 | */ | |
d1c3414c | 162 | mutex_lock(&deferred_probe_mutex); |
f1c8467e | 163 | atomic_inc(&deferred_trigger_count); |
d1c3414c GL |
164 | list_splice_tail_init(&deferred_probe_pending_list, |
165 | &deferred_probe_active_list); | |
166 | mutex_unlock(&deferred_probe_mutex); | |
167 | ||
8b0372a2 GKH |
168 | /* |
169 | * Kick the re-probe thread. It may already be scheduled, but it is | |
170 | * safe to kick it again. | |
171 | */ | |
d1c3414c GL |
172 | queue_work(deferred_wq, &deferred_probe_work); |
173 | } | |
174 | ||
175 | /** | |
176 | * deferred_probe_initcall() - Enable probing of deferred devices | |
177 | * | |
178 | * We don't want to get in the way when the bulk of drivers are getting probed. | |
179 | * Instead, this initcall makes sure that deferred probing is delayed until | |
180 | * late_initcall time. | |
181 | */ | |
182 | static int deferred_probe_initcall(void) | |
183 | { | |
184 | deferred_wq = create_singlethread_workqueue("deferwq"); | |
185 | if (WARN_ON(!deferred_wq)) | |
186 | return -ENOMEM; | |
187 | ||
188 | driver_deferred_probe_enable = true; | |
189 | driver_deferred_probe_trigger(); | |
d72cca1e GL |
190 | /* Sort as many dependencies as possible before exiting initcalls */ |
191 | flush_workqueue(deferred_wq); | |
d1c3414c GL |
192 | return 0; |
193 | } | |
194 | late_initcall(deferred_probe_initcall); | |
07e4a3e2 | 195 | |
1901fb26 | 196 | static void driver_bound(struct device *dev) |
07e4a3e2 | 197 | { |
8940b4f3 | 198 | if (klist_node_attached(&dev->p->knode_driver)) { |
f86db396 | 199 | printk(KERN_WARNING "%s: device %s already bound\n", |
2b3a302a | 200 | __func__, kobject_name(&dev->kobj)); |
1901fb26 | 201 | return; |
f86db396 | 202 | } |
4c898c7f | 203 | |
1e0b2cf9 | 204 | pr_debug("driver: '%s': %s: bound to device '%s'\n", dev_name(dev), |
2b3a302a | 205 | __func__, dev->driver->name); |
116af378 | 206 | |
fbb88fad SS |
207 | klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices); |
208 | ||
8b0372a2 GKH |
209 | /* |
210 | * Make sure the device is no longer in one of the deferred lists and | |
211 | * kick off retrying all pending devices | |
212 | */ | |
d1c3414c GL |
213 | driver_deferred_probe_del(dev); |
214 | driver_deferred_probe_trigger(); | |
215 | ||
116af378 | 216 | if (dev->bus) |
c6f7e72a | 217 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, |
116af378 | 218 | BUS_NOTIFY_BOUND_DRIVER, dev); |
1901fb26 KS |
219 | } |
220 | ||
221 | static int driver_sysfs_add(struct device *dev) | |
222 | { | |
223 | int ret; | |
224 | ||
45daef0f MD |
225 | if (dev->bus) |
226 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, | |
227 | BUS_NOTIFY_BIND_DRIVER, dev); | |
228 | ||
e5dd1278 | 229 | ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj, |
07e4a3e2 | 230 | kobject_name(&dev->kobj)); |
f86db396 | 231 | if (ret == 0) { |
e5dd1278 | 232 | ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj, |
f86db396 AM |
233 | "driver"); |
234 | if (ret) | |
e5dd1278 | 235 | sysfs_remove_link(&dev->driver->p->kobj, |
f86db396 AM |
236 | kobject_name(&dev->kobj)); |
237 | } | |
238 | return ret; | |
07e4a3e2 PM |
239 | } |
240 | ||
1901fb26 KS |
241 | static void driver_sysfs_remove(struct device *dev) |
242 | { | |
243 | struct device_driver *drv = dev->driver; | |
244 | ||
245 | if (drv) { | |
e5dd1278 | 246 | sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj)); |
1901fb26 KS |
247 | sysfs_remove_link(&dev->kobj, "driver"); |
248 | } | |
249 | } | |
250 | ||
251 | /** | |
4a3ad20c GKH |
252 | * device_bind_driver - bind a driver to one device. |
253 | * @dev: device. | |
1901fb26 | 254 | * |
4a3ad20c GKH |
255 | * Allow manual attachment of a driver to a device. |
256 | * Caller must have already set @dev->driver. | |
1901fb26 | 257 | * |
4a3ad20c GKH |
258 | * Note that this does not modify the bus reference count |
259 | * nor take the bus's rwsem. Please verify those are accounted | |
260 | * for before calling this. (It is ok to call with no other effort | |
261 | * from a driver's probe() method.) | |
1901fb26 | 262 | * |
8e9394ce | 263 | * This function must be called with the device lock held. |
1901fb26 KS |
264 | */ |
265 | int device_bind_driver(struct device *dev) | |
266 | { | |
cb986b74 CH |
267 | int ret; |
268 | ||
269 | ret = driver_sysfs_add(dev); | |
270 | if (!ret) | |
271 | driver_bound(dev); | |
272 | return ret; | |
1901fb26 | 273 | } |
4a3ad20c | 274 | EXPORT_SYMBOL_GPL(device_bind_driver); |
1901fb26 | 275 | |
d779249e | 276 | static atomic_t probe_count = ATOMIC_INIT(0); |
735a7ffb AM |
277 | static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue); |
278 | ||
21c7f30b | 279 | static int really_probe(struct device *dev, struct device_driver *drv) |
07e4a3e2 | 280 | { |
0d3e5a2e | 281 | int ret = 0; |
f1c8467e | 282 | int local_trigger_count = atomic_read(&deferred_trigger_count); |
07e4a3e2 | 283 | |
d779249e | 284 | atomic_inc(&probe_count); |
7dc72b28 | 285 | pr_debug("bus: '%s': %s: probing driver %s with device %s\n", |
1e0b2cf9 | 286 | drv->bus->name, __func__, drv->name, dev_name(dev)); |
9ac7849e | 287 | WARN_ON(!list_empty(&dev->devres_head)); |
07e4a3e2 | 288 | |
07e4a3e2 | 289 | dev->driver = drv; |
ab78029e LW |
290 | |
291 | /* If using pinctrl, bind pins now before probing */ | |
292 | ret = pinctrl_bind_pins(dev); | |
293 | if (ret) | |
294 | goto probe_failed; | |
295 | ||
1901fb26 KS |
296 | if (driver_sysfs_add(dev)) { |
297 | printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n", | |
1e0b2cf9 | 298 | __func__, dev_name(dev)); |
1901fb26 KS |
299 | goto probe_failed; |
300 | } | |
301 | ||
594c8281 | 302 | if (dev->bus->probe) { |
6fa3eb70 | 303 | TIME_LOG_START(); |
594c8281 | 304 | ret = dev->bus->probe(dev); |
6fa3eb70 | 305 | TIME_LOG_END("[probe] drv:%s dev:%s\n", drv->name, dev->init_name); |
1901fb26 | 306 | if (ret) |
d779249e | 307 | goto probe_failed; |
594c8281 | 308 | } else if (drv->probe) { |
6fa3eb70 | 309 | TIME_LOG_START(); |
0d3e5a2e | 310 | ret = drv->probe(dev); |
6fa3eb70 | 311 | TIME_LOG_END("[probe] drv:%s dev:%s\n", drv->name, dev->init_name); |
1901fb26 | 312 | if (ret) |
d779249e | 313 | goto probe_failed; |
f86db396 | 314 | } |
1901fb26 KS |
315 | |
316 | driver_bound(dev); | |
0d3e5a2e | 317 | ret = 1; |
7dc72b28 | 318 | pr_debug("bus: '%s': %s: bound device %s to driver %s\n", |
1e0b2cf9 | 319 | drv->bus->name, __func__, dev_name(dev), drv->name); |
d779249e | 320 | goto done; |
0d3e5a2e | 321 | |
d779249e | 322 | probe_failed: |
9ac7849e | 323 | devres_release_all(dev); |
1901fb26 KS |
324 | driver_sysfs_remove(dev); |
325 | dev->driver = NULL; | |
0998d063 | 326 | dev_set_drvdata(dev, NULL); |
1901fb26 | 327 | |
d1c3414c GL |
328 | if (ret == -EPROBE_DEFER) { |
329 | /* Driver requested deferred probing */ | |
330 | dev_info(dev, "Driver %s requests probe deferral\n", drv->name); | |
331 | driver_deferred_probe_add(dev); | |
f1c8467e GL |
332 | /* Did a trigger occur while probing? Need to re-trigger if yes */ |
333 | if (local_trigger_count != atomic_read(&deferred_trigger_count)) | |
334 | driver_deferred_probe_trigger(); | |
d1c3414c | 335 | } else if (ret != -ENODEV && ret != -ENXIO) { |
0d3e5a2e PM |
336 | /* driver matched but the probe failed */ |
337 | printk(KERN_WARNING | |
338 | "%s: probe of %s failed with error %d\n", | |
1e0b2cf9 | 339 | drv->name, dev_name(dev), ret); |
bcbe4f94 WS |
340 | } else { |
341 | pr_debug("%s: probe of %s rejects match %d\n", | |
342 | drv->name, dev_name(dev), ret); | |
0d3e5a2e | 343 | } |
c578abbc CH |
344 | /* |
345 | * Ignore errors returned by ->probe so that the next driver can try | |
346 | * its luck. | |
347 | */ | |
348 | ret = 0; | |
d779249e | 349 | done: |
d779249e | 350 | atomic_dec(&probe_count); |
735a7ffb | 351 | wake_up(&probe_waitqueue); |
d779249e GKH |
352 | return ret; |
353 | } | |
354 | ||
355 | /** | |
356 | * driver_probe_done | |
357 | * Determine if the probe sequence is finished or not. | |
358 | * | |
359 | * Should somehow figure out how to use a semaphore, not an atomic variable... | |
360 | */ | |
361 | int driver_probe_done(void) | |
362 | { | |
2b3a302a | 363 | pr_debug("%s: probe_count = %d\n", __func__, |
d779249e GKH |
364 | atomic_read(&probe_count)); |
365 | if (atomic_read(&probe_count)) | |
366 | return -EBUSY; | |
367 | return 0; | |
368 | } | |
369 | ||
216773a7 AV |
370 | /** |
371 | * wait_for_device_probe | |
372 | * Wait for device probing to be completed. | |
216773a7 | 373 | */ |
b23530eb | 374 | void wait_for_device_probe(void) |
216773a7 AV |
375 | { |
376 | /* wait for the known devices to complete their probing */ | |
b23530eb | 377 | wait_event(probe_waitqueue, atomic_read(&probe_count) == 0); |
216773a7 | 378 | async_synchronize_full(); |
216773a7 | 379 | } |
d4d5291c | 380 | EXPORT_SYMBOL_GPL(wait_for_device_probe); |
216773a7 | 381 | |
d779249e GKH |
382 | /** |
383 | * driver_probe_device - attempt to bind device & driver together | |
384 | * @drv: driver to bind a device to | |
385 | * @dev: device to try to bind to the driver | |
386 | * | |
49b420a1 | 387 | * This function returns -ENODEV if the device is not registered, |
af901ca1 | 388 | * 1 if the device is bound successfully and 0 otherwise. |
d779249e | 389 | * |
8e9394ce GKH |
390 | * This function must be called with @dev lock held. When called for a |
391 | * USB interface, @dev->parent lock must be held as well. | |
d779249e | 392 | */ |
4a3ad20c | 393 | int driver_probe_device(struct device_driver *drv, struct device *dev) |
d779249e | 394 | { |
d779249e GKH |
395 | int ret = 0; |
396 | ||
f2eaae19 AS |
397 | if (!device_is_registered(dev)) |
398 | return -ENODEV; | |
d779249e | 399 | |
7dc72b28 | 400 | pr_debug("bus: '%s': %s: matched device %s with driver %s\n", |
1e0b2cf9 | 401 | drv->bus->name, __func__, dev_name(dev), drv->name); |
d779249e | 402 | |
5e928f77 | 403 | pm_runtime_barrier(dev); |
21c7f30b | 404 | ret = really_probe(dev, drv); |
fa180eb4 | 405 | pm_request_idle(dev); |
d779249e | 406 | |
0d3e5a2e | 407 | return ret; |
07e4a3e2 PM |
408 | } |
409 | ||
4a3ad20c | 410 | static int __device_attach(struct device_driver *drv, void *data) |
2287c322 | 411 | { |
4a3ad20c | 412 | struct device *dev = data; |
49b420a1 ML |
413 | |
414 | if (!driver_match_device(drv, dev)) | |
415 | return 0; | |
416 | ||
0d3e5a2e | 417 | return driver_probe_device(drv, dev); |
2287c322 PM |
418 | } |
419 | ||
07e4a3e2 | 420 | /** |
4a3ad20c GKH |
421 | * device_attach - try to attach device to a driver. |
422 | * @dev: device. | |
07e4a3e2 | 423 | * |
4a3ad20c GKH |
424 | * Walk the list of drivers that the bus has and call |
425 | * driver_probe_device() for each pair. If a compatible | |
426 | * pair is found, break out and return. | |
0d3e5a2e | 427 | * |
4a3ad20c | 428 | * Returns 1 if the device was bound to a driver; |
59a3cd7f | 429 | * 0 if no matching driver was found; |
4a3ad20c | 430 | * -ENODEV if the device is not registered. |
bf74ad5b | 431 | * |
8e9394ce | 432 | * When called for a USB interface, @dev->parent lock must be held. |
07e4a3e2 | 433 | */ |
4a3ad20c | 434 | int device_attach(struct device *dev) |
07e4a3e2 | 435 | { |
0d3e5a2e PM |
436 | int ret = 0; |
437 | ||
8e9394ce | 438 | device_lock(dev); |
07e4a3e2 | 439 | if (dev->driver) { |
8497d6a2 SO |
440 | if (klist_node_attached(&dev->p->knode_driver)) { |
441 | ret = 1; | |
442 | goto out_unlock; | |
443 | } | |
f86db396 AM |
444 | ret = device_bind_driver(dev); |
445 | if (ret == 0) | |
446 | ret = 1; | |
c6a46696 CH |
447 | else { |
448 | dev->driver = NULL; | |
449 | ret = 0; | |
450 | } | |
21c7f30b | 451 | } else { |
5adc55da | 452 | ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach); |
fa180eb4 | 453 | pm_request_idle(dev); |
21c7f30b | 454 | } |
8497d6a2 | 455 | out_unlock: |
8e9394ce | 456 | device_unlock(dev); |
0d3e5a2e | 457 | return ret; |
2287c322 | 458 | } |
4a3ad20c | 459 | EXPORT_SYMBOL_GPL(device_attach); |
2287c322 | 460 | |
4a3ad20c | 461 | static int __driver_attach(struct device *dev, void *data) |
2287c322 | 462 | { |
4a3ad20c | 463 | struct device_driver *drv = data; |
0d3e5a2e PM |
464 | |
465 | /* | |
466 | * Lock device and try to bind to it. We drop the error | |
467 | * here and always return 0, because we need to keep trying | |
468 | * to bind to devices and some drivers will return an error | |
469 | * simply if it didn't support the device. | |
470 | * | |
471 | * driver_probe_device() will spit a warning if there | |
472 | * is an error. | |
473 | */ | |
474 | ||
49b420a1 | 475 | if (!driver_match_device(drv, dev)) |
6cd49586 AV |
476 | return 0; |
477 | ||
bf74ad5b | 478 | if (dev->parent) /* Needed for USB */ |
8e9394ce GKH |
479 | device_lock(dev->parent); |
480 | device_lock(dev); | |
0d3e5a2e PM |
481 | if (!dev->driver) |
482 | driver_probe_device(drv, dev); | |
8e9394ce | 483 | device_unlock(dev); |
bf74ad5b | 484 | if (dev->parent) |
8e9394ce | 485 | device_unlock(dev->parent); |
0d3e5a2e | 486 | |
07e4a3e2 PM |
487 | return 0; |
488 | } | |
489 | ||
490 | /** | |
4a3ad20c GKH |
491 | * driver_attach - try to bind driver to devices. |
492 | * @drv: driver. | |
07e4a3e2 | 493 | * |
4a3ad20c GKH |
494 | * Walk the list of devices that the bus has on it and try to |
495 | * match the driver with each one. If driver_probe_device() | |
496 | * returns 0 and the @dev->driver is set, we've found a | |
497 | * compatible pair. | |
07e4a3e2 | 498 | */ |
4a3ad20c | 499 | int driver_attach(struct device_driver *drv) |
07e4a3e2 | 500 | { |
f86db396 | 501 | return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach); |
07e4a3e2 | 502 | } |
4a3ad20c | 503 | EXPORT_SYMBOL_GPL(driver_attach); |
07e4a3e2 | 504 | |
ab71c6f0 | 505 | /* |
8e9394ce GKH |
506 | * __device_release_driver() must be called with @dev lock held. |
507 | * When called for a USB interface, @dev->parent lock must be held as well. | |
07e4a3e2 | 508 | */ |
4a3ad20c | 509 | static void __device_release_driver(struct device *dev) |
07e4a3e2 | 510 | { |
4a3ad20c | 511 | struct device_driver *drv; |
07e4a3e2 | 512 | |
ef2c5174 | 513 | drv = dev->driver; |
c95a6b05 | 514 | if (drv) { |
e1866b33 | 515 | pm_runtime_get_sync(dev); |
5e928f77 | 516 | |
1901fb26 | 517 | driver_sysfs_remove(dev); |
0d3e5a2e | 518 | |
116af378 | 519 | if (dev->bus) |
c6f7e72a | 520 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, |
116af378 BH |
521 | BUS_NOTIFY_UNBIND_DRIVER, |
522 | dev); | |
523 | ||
2aed351e | 524 | pm_runtime_put_sync(dev); |
e1866b33 | 525 | |
0f836ca4 | 526 | if (dev->bus && dev->bus->remove) |
594c8281 RK |
527 | dev->bus->remove(dev); |
528 | else if (drv->remove) | |
0d3e5a2e | 529 | drv->remove(dev); |
9ac7849e | 530 | devres_release_all(dev); |
0d3e5a2e | 531 | dev->driver = NULL; |
0998d063 | 532 | dev_set_drvdata(dev, NULL); |
8940b4f3 | 533 | klist_remove(&dev->p->knode_driver); |
309b7d60 JR |
534 | if (dev->bus) |
535 | blocking_notifier_call_chain(&dev->bus->p->bus_notifier, | |
536 | BUS_NOTIFY_UNBOUND_DRIVER, | |
537 | dev); | |
5e928f77 | 538 | |
0d3e5a2e | 539 | } |
07e4a3e2 PM |
540 | } |
541 | ||
ab71c6f0 | 542 | /** |
4a3ad20c GKH |
543 | * device_release_driver - manually detach device from driver. |
544 | * @dev: device. | |
ab71c6f0 | 545 | * |
4a3ad20c | 546 | * Manually detach device from driver. |
8e9394ce | 547 | * When called for a USB interface, @dev->parent lock must be held. |
ab71c6f0 | 548 | */ |
4a3ad20c | 549 | void device_release_driver(struct device *dev) |
94e7b1c5 | 550 | { |
c95a6b05 AS |
551 | /* |
552 | * If anyone calls device_release_driver() recursively from | |
553 | * within their ->remove callback for the same device, they | |
554 | * will deadlock right here. | |
555 | */ | |
8e9394ce | 556 | device_lock(dev); |
c95a6b05 | 557 | __device_release_driver(dev); |
8e9394ce | 558 | device_unlock(dev); |
94e7b1c5 | 559 | } |
4a3ad20c | 560 | EXPORT_SYMBOL_GPL(device_release_driver); |
c95a6b05 | 561 | |
07e4a3e2 PM |
562 | /** |
563 | * driver_detach - detach driver from all devices it controls. | |
564 | * @drv: driver. | |
565 | */ | |
4a3ad20c | 566 | void driver_detach(struct device_driver *drv) |
07e4a3e2 | 567 | { |
8940b4f3 | 568 | struct device_private *dev_prv; |
4a3ad20c | 569 | struct device *dev; |
c95a6b05 AS |
570 | |
571 | for (;;) { | |
e5dd1278 GKH |
572 | spin_lock(&drv->p->klist_devices.k_lock); |
573 | if (list_empty(&drv->p->klist_devices.k_list)) { | |
574 | spin_unlock(&drv->p->klist_devices.k_lock); | |
c95a6b05 AS |
575 | break; |
576 | } | |
8940b4f3 GKH |
577 | dev_prv = list_entry(drv->p->klist_devices.k_list.prev, |
578 | struct device_private, | |
579 | knode_driver.n_node); | |
580 | dev = dev_prv->device; | |
c95a6b05 | 581 | get_device(dev); |
e5dd1278 | 582 | spin_unlock(&drv->p->klist_devices.k_lock); |
c95a6b05 | 583 | |
bf74ad5b | 584 | if (dev->parent) /* Needed for USB */ |
8e9394ce GKH |
585 | device_lock(dev->parent); |
586 | device_lock(dev); | |
c95a6b05 AS |
587 | if (dev->driver == drv) |
588 | __device_release_driver(dev); | |
8e9394ce | 589 | device_unlock(dev); |
bf74ad5b | 590 | if (dev->parent) |
8e9394ce | 591 | device_unlock(dev->parent); |
c95a6b05 AS |
592 | put_device(dev); |
593 | } | |
07e4a3e2 | 594 | } |
b4028437 GKH |
595 | |
596 | /* | |
597 | * These exports can't be _GPL due to .h files using this within them, and it | |
598 | * might break something that was previously working... | |
599 | */ | |
600 | void *dev_get_drvdata(const struct device *dev) | |
601 | { | |
602 | if (dev && dev->p) | |
603 | return dev->p->driver_data; | |
604 | return NULL; | |
605 | } | |
606 | EXPORT_SYMBOL(dev_get_drvdata); | |
607 | ||
c8705082 | 608 | int dev_set_drvdata(struct device *dev, void *data) |
b4028437 GKH |
609 | { |
610 | int error; | |
611 | ||
b4028437 GKH |
612 | if (!dev->p) { |
613 | error = device_private_init(dev); | |
614 | if (error) | |
c8705082 | 615 | return error; |
b4028437 GKH |
616 | } |
617 | dev->p->driver_data = data; | |
c8705082 | 618 | return 0; |
b4028437 GKH |
619 | } |
620 | EXPORT_SYMBOL(dev_set_drvdata); |