79a4fd1540f3c49f9d5053191e79acbff77444a0
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / base / dd.c
1 /*
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 * Copyright (c) 2007-2009 Greg Kroah-Hartman <gregkh@suse.de>
15 * Copyright (c) 2007-2009 Novell Inc.
16 *
17 * This file is released under the GPLv2
18 */
19
20 #include <linux/device.h>
21 #include <linux/delay.h>
22 #include <linux/module.h>
23 #include <linux/kthread.h>
24 #include <linux/wait.h>
25 #include <linux/async.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/pinctrl/devinfo.h>
28 #include <linux/time_log.h>
29
30 #include "base.h"
31 #include "power/power.h"
32
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
50 * of the (struct device*)->p->deferred_probe pointers are manipulated
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;
56 static atomic_t deferred_trigger_count = ATOMIC_INIT(0);
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;
64 struct device_private *private;
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)) {
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);
83
84 get_device(dev);
85
86 /*
87 * Drop the mutex while probing each device; the probe path may
88 * manipulate the deferred list
89 */
90 mutex_unlock(&deferred_probe_mutex);
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
102 dev_dbg(dev, "Retrying from deferred list\n");
103 bus_probe_device(dev);
104
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);
116 if (list_empty(&dev->p->deferred_probe)) {
117 dev_dbg(dev, "Added to deferred list\n");
118 list_add_tail(&dev->p->deferred_probe, &deferred_probe_pending_list);
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);
126 if (!list_empty(&dev->p->deferred_probe)) {
127 dev_dbg(dev, "Removed from deferred list\n");
128 list_del_init(&dev->p->deferred_probe);
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.
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.
151 */
152 static void driver_deferred_probe_trigger(void)
153 {
154 if (!driver_deferred_probe_enable)
155 return;
156
157 /*
158 * A successful probe means that all the devices in the pending list
159 * should be triggered to be reprobed. Move all the deferred devices
160 * into the active list so they can be retried by the workqueue
161 */
162 mutex_lock(&deferred_probe_mutex);
163 atomic_inc(&deferred_trigger_count);
164 list_splice_tail_init(&deferred_probe_pending_list,
165 &deferred_probe_active_list);
166 mutex_unlock(&deferred_probe_mutex);
167
168 /*
169 * Kick the re-probe thread. It may already be scheduled, but it is
170 * safe to kick it again.
171 */
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();
190 /* Sort as many dependencies as possible before exiting initcalls */
191 flush_workqueue(deferred_wq);
192 return 0;
193 }
194 late_initcall(deferred_probe_initcall);
195
196 static void driver_bound(struct device *dev)
197 {
198 if (klist_node_attached(&dev->p->knode_driver)) {
199 printk(KERN_WARNING "%s: device %s already bound\n",
200 __func__, kobject_name(&dev->kobj));
201 return;
202 }
203
204 pr_debug("driver: '%s': %s: bound to device '%s'\n", dev_name(dev),
205 __func__, dev->driver->name);
206
207 klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
208
209 /*
210 * Make sure the device is no longer in one of the deferred lists and
211 * kick off retrying all pending devices
212 */
213 driver_deferred_probe_del(dev);
214 driver_deferred_probe_trigger();
215
216 if (dev->bus)
217 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
218 BUS_NOTIFY_BOUND_DRIVER, dev);
219 }
220
221 static int driver_sysfs_add(struct device *dev)
222 {
223 int ret;
224
225 if (dev->bus)
226 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
227 BUS_NOTIFY_BIND_DRIVER, dev);
228
229 ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
230 kobject_name(&dev->kobj));
231 if (ret == 0) {
232 ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
233 "driver");
234 if (ret)
235 sysfs_remove_link(&dev->driver->p->kobj,
236 kobject_name(&dev->kobj));
237 }
238 return ret;
239 }
240
241 static void driver_sysfs_remove(struct device *dev)
242 {
243 struct device_driver *drv = dev->driver;
244
245 if (drv) {
246 sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
247 sysfs_remove_link(&dev->kobj, "driver");
248 }
249 }
250
251 /**
252 * device_bind_driver - bind a driver to one device.
253 * @dev: device.
254 *
255 * Allow manual attachment of a driver to a device.
256 * Caller must have already set @dev->driver.
257 *
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.)
262 *
263 * This function must be called with the device lock held.
264 */
265 int device_bind_driver(struct device *dev)
266 {
267 int ret;
268
269 ret = driver_sysfs_add(dev);
270 if (!ret)
271 driver_bound(dev);
272 return ret;
273 }
274 EXPORT_SYMBOL_GPL(device_bind_driver);
275
276 static atomic_t probe_count = ATOMIC_INIT(0);
277 static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
278
279 static int really_probe(struct device *dev, struct device_driver *drv)
280 {
281 int ret = 0;
282 int local_trigger_count = atomic_read(&deferred_trigger_count);
283
284 atomic_inc(&probe_count);
285 pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
286 drv->bus->name, __func__, drv->name, dev_name(dev));
287 WARN_ON(!list_empty(&dev->devres_head));
288
289 dev->driver = drv;
290
291 /* If using pinctrl, bind pins now before probing */
292 ret = pinctrl_bind_pins(dev);
293 if (ret)
294 goto probe_failed;
295
296 if (driver_sysfs_add(dev)) {
297 printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
298 __func__, dev_name(dev));
299 goto probe_failed;
300 }
301
302 if (dev->bus->probe) {
303 TIME_LOG_START();
304 ret = dev->bus->probe(dev);
305 TIME_LOG_END("[probe] drv:%s dev:%s\n", drv->name, dev->init_name);
306 if (ret)
307 goto probe_failed;
308 } else if (drv->probe) {
309 TIME_LOG_START();
310 ret = drv->probe(dev);
311 TIME_LOG_END("[probe] drv:%s dev:%s\n", drv->name, dev->init_name);
312 if (ret)
313 goto probe_failed;
314 }
315
316 driver_bound(dev);
317 ret = 1;
318 pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
319 drv->bus->name, __func__, dev_name(dev), drv->name);
320 goto done;
321
322 probe_failed:
323 devres_release_all(dev);
324 driver_sysfs_remove(dev);
325 dev->driver = NULL;
326 dev_set_drvdata(dev, NULL);
327
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);
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();
335 } else if (ret != -ENODEV && ret != -ENXIO) {
336 /* driver matched but the probe failed */
337 printk(KERN_WARNING
338 "%s: probe of %s failed with error %d\n",
339 drv->name, dev_name(dev), ret);
340 } else {
341 pr_debug("%s: probe of %s rejects match %d\n",
342 drv->name, dev_name(dev), ret);
343 }
344 /*
345 * Ignore errors returned by ->probe so that the next driver can try
346 * its luck.
347 */
348 ret = 0;
349 done:
350 atomic_dec(&probe_count);
351 wake_up(&probe_waitqueue);
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 {
363 pr_debug("%s: probe_count = %d\n", __func__,
364 atomic_read(&probe_count));
365 if (atomic_read(&probe_count))
366 return -EBUSY;
367 return 0;
368 }
369
370 /**
371 * wait_for_device_probe
372 * Wait for device probing to be completed.
373 */
374 void wait_for_device_probe(void)
375 {
376 /* wait for the known devices to complete their probing */
377 wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
378 async_synchronize_full();
379 }
380 EXPORT_SYMBOL_GPL(wait_for_device_probe);
381
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 *
387 * This function returns -ENODEV if the device is not registered,
388 * 1 if the device is bound successfully and 0 otherwise.
389 *
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.
392 */
393 int driver_probe_device(struct device_driver *drv, struct device *dev)
394 {
395 int ret = 0;
396
397 if (!device_is_registered(dev))
398 return -ENODEV;
399
400 pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
401 drv->bus->name, __func__, dev_name(dev), drv->name);
402
403 pm_runtime_barrier(dev);
404 ret = really_probe(dev, drv);
405 pm_request_idle(dev);
406
407 return ret;
408 }
409
410 static int __device_attach(struct device_driver *drv, void *data)
411 {
412 struct device *dev = data;
413
414 if (!driver_match_device(drv, dev))
415 return 0;
416
417 return driver_probe_device(drv, dev);
418 }
419
420 /**
421 * device_attach - try to attach device to a driver.
422 * @dev: device.
423 *
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.
427 *
428 * Returns 1 if the device was bound to a driver;
429 * 0 if no matching driver was found;
430 * -ENODEV if the device is not registered.
431 *
432 * When called for a USB interface, @dev->parent lock must be held.
433 */
434 int device_attach(struct device *dev)
435 {
436 int ret = 0;
437
438 device_lock(dev);
439 if (dev->driver) {
440 if (klist_node_attached(&dev->p->knode_driver)) {
441 ret = 1;
442 goto out_unlock;
443 }
444 ret = device_bind_driver(dev);
445 if (ret == 0)
446 ret = 1;
447 else {
448 dev->driver = NULL;
449 ret = 0;
450 }
451 } else {
452 ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
453 pm_request_idle(dev);
454 }
455 out_unlock:
456 device_unlock(dev);
457 return ret;
458 }
459 EXPORT_SYMBOL_GPL(device_attach);
460
461 static int __driver_attach(struct device *dev, void *data)
462 {
463 struct device_driver *drv = data;
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
475 if (!driver_match_device(drv, dev))
476 return 0;
477
478 if (dev->parent) /* Needed for USB */
479 device_lock(dev->parent);
480 device_lock(dev);
481 if (!dev->driver)
482 driver_probe_device(drv, dev);
483 device_unlock(dev);
484 if (dev->parent)
485 device_unlock(dev->parent);
486
487 return 0;
488 }
489
490 /**
491 * driver_attach - try to bind driver to devices.
492 * @drv: driver.
493 *
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.
498 */
499 int driver_attach(struct device_driver *drv)
500 {
501 return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
502 }
503 EXPORT_SYMBOL_GPL(driver_attach);
504
505 /*
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.
508 */
509 static void __device_release_driver(struct device *dev)
510 {
511 struct device_driver *drv;
512
513 drv = dev->driver;
514 if (drv) {
515 pm_runtime_get_sync(dev);
516
517 driver_sysfs_remove(dev);
518
519 if (dev->bus)
520 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
521 BUS_NOTIFY_UNBIND_DRIVER,
522 dev);
523
524 pm_runtime_put_sync(dev);
525
526 if (dev->bus && dev->bus->remove)
527 dev->bus->remove(dev);
528 else if (drv->remove)
529 drv->remove(dev);
530 devres_release_all(dev);
531 dev->driver = NULL;
532 dev_set_drvdata(dev, NULL);
533 klist_remove(&dev->p->knode_driver);
534 if (dev->bus)
535 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
536 BUS_NOTIFY_UNBOUND_DRIVER,
537 dev);
538
539 }
540 }
541
542 /**
543 * device_release_driver - manually detach device from driver.
544 * @dev: device.
545 *
546 * Manually detach device from driver.
547 * When called for a USB interface, @dev->parent lock must be held.
548 */
549 void device_release_driver(struct device *dev)
550 {
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 */
556 device_lock(dev);
557 __device_release_driver(dev);
558 device_unlock(dev);
559 }
560 EXPORT_SYMBOL_GPL(device_release_driver);
561
562 /**
563 * driver_detach - detach driver from all devices it controls.
564 * @drv: driver.
565 */
566 void driver_detach(struct device_driver *drv)
567 {
568 struct device_private *dev_prv;
569 struct device *dev;
570
571 for (;;) {
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);
575 break;
576 }
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;
581 get_device(dev);
582 spin_unlock(&drv->p->klist_devices.k_lock);
583
584 if (dev->parent) /* Needed for USB */
585 device_lock(dev->parent);
586 device_lock(dev);
587 if (dev->driver == drv)
588 __device_release_driver(dev);
589 device_unlock(dev);
590 if (dev->parent)
591 device_unlock(dev->parent);
592 put_device(dev);
593 }
594 }
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
608 int dev_set_drvdata(struct device *dev, void *data)
609 {
610 int error;
611
612 if (!dev->p) {
613 error = device_private_init(dev);
614 if (error)
615 return error;
616 }
617 dev->p->driver_data = data;
618 return 0;
619 }
620 EXPORT_SYMBOL(dev_set_drvdata);