dd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. return 0;
  160. }
  161. late_initcall(deferred_probe_initcall);
  162. static void driver_bound(struct device *dev)
  163. {
  164. if (klist_node_attached(&dev->p->knode_driver)) {
  165. printk(KERN_WARNING "%s: device %s already bound\n",
  166. __func__, kobject_name(&dev->kobj));
  167. return;
  168. }
  169. pr_debug("driver: '%s': %s: bound to device '%s'\n", dev_name(dev),
  170. __func__, dev->driver->name);
  171. klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
  172. /*
  173. * Make sure the device is no longer in one of the deferred lists and
  174. * kick off retrying all pending devices
  175. */
  176. driver_deferred_probe_del(dev);
  177. driver_deferred_probe_trigger();
  178. if (dev->bus)
  179. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  180. BUS_NOTIFY_BOUND_DRIVER, dev);
  181. }
  182. static int driver_sysfs_add(struct device *dev)
  183. {
  184. int ret;
  185. if (dev->bus)
  186. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  187. BUS_NOTIFY_BIND_DRIVER, dev);
  188. ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
  189. kobject_name(&dev->kobj));
  190. if (ret == 0) {
  191. ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
  192. "driver");
  193. if (ret)
  194. sysfs_remove_link(&dev->driver->p->kobj,
  195. kobject_name(&dev->kobj));
  196. }
  197. return ret;
  198. }
  199. static void driver_sysfs_remove(struct device *dev)
  200. {
  201. struct device_driver *drv = dev->driver;
  202. if (drv) {
  203. sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
  204. sysfs_remove_link(&dev->kobj, "driver");
  205. }
  206. }
  207. /**
  208. * device_bind_driver - bind a driver to one device.
  209. * @dev: device.
  210. *
  211. * Allow manual attachment of a driver to a device.
  212. * Caller must have already set @dev->driver.
  213. *
  214. * Note that this does not modify the bus reference count
  215. * nor take the bus's rwsem. Please verify those are accounted
  216. * for before calling this. (It is ok to call with no other effort
  217. * from a driver's probe() method.)
  218. *
  219. * This function must be called with the device lock held.
  220. */
  221. int device_bind_driver(struct device *dev)
  222. {
  223. int ret;
  224. ret = driver_sysfs_add(dev);
  225. if (!ret)
  226. driver_bound(dev);
  227. return ret;
  228. }
  229. EXPORT_SYMBOL_GPL(device_bind_driver);
  230. static atomic_t probe_count = ATOMIC_INIT(0);
  231. static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
  232. static int really_probe(struct device *dev, struct device_driver *drv)
  233. {
  234. int ret = 0;
  235. atomic_inc(&probe_count);
  236. pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
  237. drv->bus->name, __func__, drv->name, dev_name(dev));
  238. WARN_ON(!list_empty(&dev->devres_head));
  239. dev->driver = drv;
  240. /* If using pinctrl, bind pins now before probing */
  241. ret = pinctrl_bind_pins(dev);
  242. if (ret)
  243. goto probe_failed;
  244. if (driver_sysfs_add(dev)) {
  245. printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
  246. __func__, dev_name(dev));
  247. goto probe_failed;
  248. }
  249. if (dev->bus->probe) {
  250. ret = dev->bus->probe(dev);
  251. if (ret)
  252. goto probe_failed;
  253. } else if (drv->probe) {
  254. ret = drv->probe(dev);
  255. if (ret)
  256. goto probe_failed;
  257. }
  258. driver_bound(dev);
  259. ret = 1;
  260. pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
  261. drv->bus->name, __func__, dev_name(dev), drv->name);
  262. goto done;
  263. probe_failed:
  264. devres_release_all(dev);
  265. driver_sysfs_remove(dev);
  266. dev->driver = NULL;
  267. dev_set_drvdata(dev, NULL);
  268. if (ret == -EPROBE_DEFER) {
  269. /* Driver requested deferred probing */
  270. dev_info(dev, "Driver %s requests probe deferral\n", drv->name);
  271. driver_deferred_probe_add(dev);
  272. } else if (ret != -ENODEV && ret != -ENXIO) {
  273. /* driver matched but the probe failed */
  274. printk(KERN_WARNING
  275. "%s: probe of %s failed with error %d\n",
  276. drv->name, dev_name(dev), ret);
  277. } else {
  278. pr_debug("%s: probe of %s rejects match %d\n",
  279. drv->name, dev_name(dev), ret);
  280. }
  281. /*
  282. * Ignore errors returned by ->probe so that the next driver can try
  283. * its luck.
  284. */
  285. ret = 0;
  286. done:
  287. atomic_dec(&probe_count);
  288. wake_up(&probe_waitqueue);
  289. return ret;
  290. }
  291. /**
  292. * driver_probe_done
  293. * Determine if the probe sequence is finished or not.
  294. *
  295. * Should somehow figure out how to use a semaphore, not an atomic variable...
  296. */
  297. int driver_probe_done(void)
  298. {
  299. pr_debug("%s: probe_count = %d\n", __func__,
  300. atomic_read(&probe_count));
  301. if (atomic_read(&probe_count))
  302. return -EBUSY;
  303. return 0;
  304. }
  305. /**
  306. * wait_for_device_probe
  307. * Wait for device probing to be completed.
  308. */
  309. void wait_for_device_probe(void)
  310. {
  311. /* wait for the known devices to complete their probing */
  312. wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
  313. async_synchronize_full();
  314. }
  315. EXPORT_SYMBOL_GPL(wait_for_device_probe);
  316. /**
  317. * driver_probe_device - attempt to bind device & driver together
  318. * @drv: driver to bind a device to
  319. * @dev: device to try to bind to the driver
  320. *
  321. * This function returns -ENODEV if the device is not registered,
  322. * 1 if the device is bound successfully and 0 otherwise.
  323. *
  324. * This function must be called with @dev lock held. When called for a
  325. * USB interface, @dev->parent lock must be held as well.
  326. */
  327. int driver_probe_device(struct device_driver *drv, struct device *dev)
  328. {
  329. int ret = 0;
  330. if (!device_is_registered(dev))
  331. return -ENODEV;
  332. pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
  333. drv->bus->name, __func__, dev_name(dev), drv->name);
  334. pm_runtime_barrier(dev);
  335. ret = really_probe(dev, drv);
  336. pm_runtime_idle(dev);
  337. return ret;
  338. }
  339. static int __device_attach(struct device_driver *drv, void *data)
  340. {
  341. struct device *dev = data;
  342. if (!driver_match_device(drv, dev))
  343. return 0;
  344. return driver_probe_device(drv, dev);
  345. }
  346. /**
  347. * device_attach - try to attach device to a driver.
  348. * @dev: device.
  349. *
  350. * Walk the list of drivers that the bus has and call
  351. * driver_probe_device() for each pair. If a compatible
  352. * pair is found, break out and return.
  353. *
  354. * Returns 1 if the device was bound to a driver;
  355. * 0 if no matching driver was found;
  356. * -ENODEV if the device is not registered.
  357. *
  358. * When called for a USB interface, @dev->parent lock must be held.
  359. */
  360. int device_attach(struct device *dev)
  361. {
  362. int ret = 0;
  363. device_lock(dev);
  364. if (dev->driver) {
  365. if (klist_node_attached(&dev->p->knode_driver)) {
  366. ret = 1;
  367. goto out_unlock;
  368. }
  369. ret = device_bind_driver(dev);
  370. if (ret == 0)
  371. ret = 1;
  372. else {
  373. dev->driver = NULL;
  374. ret = 0;
  375. }
  376. } else {
  377. ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
  378. pm_runtime_idle(dev);
  379. }
  380. out_unlock:
  381. device_unlock(dev);
  382. return ret;
  383. }
  384. EXPORT_SYMBOL_GPL(device_attach);
  385. static int __driver_attach(struct device *dev, void *data)
  386. {
  387. struct device_driver *drv = data;
  388. /*
  389. * Lock device and try to bind to it. We drop the error
  390. * here and always return 0, because we need to keep trying
  391. * to bind to devices and some drivers will return an error
  392. * simply if it didn't support the device.
  393. *
  394. * driver_probe_device() will spit a warning if there
  395. * is an error.
  396. */
  397. if (!driver_match_device(drv, dev))
  398. return 0;
  399. if (dev->parent) /* Needed for USB */
  400. device_lock(dev->parent);
  401. device_lock(dev);
  402. if (!dev->driver)
  403. driver_probe_device(drv, dev);
  404. device_unlock(dev);
  405. if (dev->parent)
  406. device_unlock(dev->parent);
  407. return 0;
  408. }
  409. /**
  410. * driver_attach - try to bind driver to devices.
  411. * @drv: driver.
  412. *
  413. * Walk the list of devices that the bus has on it and try to
  414. * match the driver with each one. If driver_probe_device()
  415. * returns 0 and the @dev->driver is set, we've found a
  416. * compatible pair.
  417. */
  418. int driver_attach(struct device_driver *drv)
  419. {
  420. return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  421. }
  422. EXPORT_SYMBOL_GPL(driver_attach);
  423. /*
  424. * __device_release_driver() must be called with @dev lock held.
  425. * When called for a USB interface, @dev->parent lock must be held as well.
  426. */
  427. static void __device_release_driver(struct device *dev)
  428. {
  429. struct device_driver *drv;
  430. drv = dev->driver;
  431. if (drv) {
  432. pm_runtime_get_sync(dev);
  433. driver_sysfs_remove(dev);
  434. if (dev->bus)
  435. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  436. BUS_NOTIFY_UNBIND_DRIVER,
  437. dev);
  438. pm_runtime_put_sync(dev);
  439. if (dev->bus && dev->bus->remove)
  440. dev->bus->remove(dev);
  441. else if (drv->remove)
  442. drv->remove(dev);
  443. devres_release_all(dev);
  444. dev->driver = NULL;
  445. dev_set_drvdata(dev, NULL);
  446. klist_remove(&dev->p->knode_driver);
  447. if (dev->bus)
  448. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  449. BUS_NOTIFY_UNBOUND_DRIVER,
  450. dev);
  451. }
  452. }
  453. /**
  454. * device_release_driver - manually detach device from driver.
  455. * @dev: device.
  456. *
  457. * Manually detach device from driver.
  458. * When called for a USB interface, @dev->parent lock must be held.
  459. */
  460. void device_release_driver(struct device *dev)
  461. {
  462. /*
  463. * If anyone calls device_release_driver() recursively from
  464. * within their ->remove callback for the same device, they
  465. * will deadlock right here.
  466. */
  467. device_lock(dev);
  468. __device_release_driver(dev);
  469. device_unlock(dev);
  470. }
  471. EXPORT_SYMBOL_GPL(device_release_driver);
  472. /**
  473. * driver_detach - detach driver from all devices it controls.
  474. * @drv: driver.
  475. */
  476. void driver_detach(struct device_driver *drv)
  477. {
  478. struct device_private *dev_prv;
  479. struct device *dev;
  480. for (;;) {
  481. spin_lock(&drv->p->klist_devices.k_lock);
  482. if (list_empty(&drv->p->klist_devices.k_list)) {
  483. spin_unlock(&drv->p->klist_devices.k_lock);
  484. break;
  485. }
  486. dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
  487. struct device_private,
  488. knode_driver.n_node);
  489. dev = dev_prv->device;
  490. get_device(dev);
  491. spin_unlock(&drv->p->klist_devices.k_lock);
  492. if (dev->parent) /* Needed for USB */
  493. device_lock(dev->parent);
  494. device_lock(dev);
  495. if (dev->driver == drv)
  496. __device_release_driver(dev);
  497. device_unlock(dev);
  498. if (dev->parent)
  499. device_unlock(dev->parent);
  500. put_device(dev);
  501. }
  502. }
  503. /*
  504. * These exports can't be _GPL due to .h files using this within them, and it
  505. * might break something that was previously working...
  506. */
  507. void *dev_get_drvdata(const struct device *dev)
  508. {
  509. if (dev && dev->p)
  510. return dev->p->driver_data;
  511. return NULL;
  512. }
  513. EXPORT_SYMBOL(dev_get_drvdata);
  514. int dev_set_drvdata(struct device *dev, void *data)
  515. {
  516. int error;
  517. if (!dev->p) {
  518. error = device_private_init(dev);
  519. if (error)
  520. return error;
  521. }
  522. dev->p->driver_data = data;
  523. return 0;
  524. }
  525. EXPORT_SYMBOL(dev_set_drvdata);