dd.c 15 KB

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