dd.c 15 KB

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