dd.c 16 KB

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