dd.c 16 KB

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