dd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. static void driver_bound(struct device *dev)
  29. {
  30. if (klist_node_attached(&dev->p->knode_driver)) {
  31. printk(KERN_WARNING "%s: device %s already bound\n",
  32. __func__, kobject_name(&dev->kobj));
  33. return;
  34. }
  35. pr_debug("driver: '%s': %s: bound to device '%s'\n", dev_name(dev),
  36. __func__, dev->driver->name);
  37. klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
  38. if (dev->bus)
  39. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  40. BUS_NOTIFY_BOUND_DRIVER, dev);
  41. }
  42. static int driver_sysfs_add(struct device *dev)
  43. {
  44. int ret;
  45. if (dev->bus)
  46. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  47. BUS_NOTIFY_BIND_DRIVER, dev);
  48. ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
  49. kobject_name(&dev->kobj));
  50. if (ret == 0) {
  51. ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
  52. "driver");
  53. if (ret)
  54. sysfs_remove_link(&dev->driver->p->kobj,
  55. kobject_name(&dev->kobj));
  56. }
  57. return ret;
  58. }
  59. static void driver_sysfs_remove(struct device *dev)
  60. {
  61. struct device_driver *drv = dev->driver;
  62. if (drv) {
  63. sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
  64. sysfs_remove_link(&dev->kobj, "driver");
  65. }
  66. }
  67. /**
  68. * device_bind_driver - bind a driver to one device.
  69. * @dev: device.
  70. *
  71. * Allow manual attachment of a driver to a device.
  72. * Caller must have already set @dev->driver.
  73. *
  74. * Note that this does not modify the bus reference count
  75. * nor take the bus's rwsem. Please verify those are accounted
  76. * for before calling this. (It is ok to call with no other effort
  77. * from a driver's probe() method.)
  78. *
  79. * This function must be called with the device lock held.
  80. */
  81. int device_bind_driver(struct device *dev)
  82. {
  83. int ret;
  84. ret = driver_sysfs_add(dev);
  85. if (!ret)
  86. driver_bound(dev);
  87. return ret;
  88. }
  89. EXPORT_SYMBOL_GPL(device_bind_driver);
  90. static atomic_t probe_count = ATOMIC_INIT(0);
  91. static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
  92. static int really_probe(struct device *dev, struct device_driver *drv)
  93. {
  94. int ret = 0;
  95. atomic_inc(&probe_count);
  96. pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
  97. drv->bus->name, __func__, drv->name, dev_name(dev));
  98. WARN_ON(!list_empty(&dev->devres_head));
  99. dev->driver = drv;
  100. if (driver_sysfs_add(dev)) {
  101. printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
  102. __func__, dev_name(dev));
  103. goto probe_failed;
  104. }
  105. if (dev->bus->probe) {
  106. ret = dev->bus->probe(dev);
  107. if (ret)
  108. goto probe_failed;
  109. } else if (drv->probe) {
  110. ret = drv->probe(dev);
  111. if (ret)
  112. goto probe_failed;
  113. }
  114. driver_bound(dev);
  115. ret = 1;
  116. pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
  117. drv->bus->name, __func__, dev_name(dev), drv->name);
  118. goto done;
  119. probe_failed:
  120. devres_release_all(dev);
  121. driver_sysfs_remove(dev);
  122. dev->driver = NULL;
  123. if (ret != -ENODEV && ret != -ENXIO) {
  124. /* driver matched but the probe failed */
  125. printk(KERN_WARNING
  126. "%s: probe of %s failed with error %d\n",
  127. drv->name, dev_name(dev), ret);
  128. } else {
  129. pr_debug("%s: probe of %s rejects match %d\n",
  130. drv->name, dev_name(dev), ret);
  131. }
  132. /*
  133. * Ignore errors returned by ->probe so that the next driver can try
  134. * its luck.
  135. */
  136. ret = 0;
  137. done:
  138. atomic_dec(&probe_count);
  139. wake_up(&probe_waitqueue);
  140. return ret;
  141. }
  142. /**
  143. * driver_probe_done
  144. * Determine if the probe sequence is finished or not.
  145. *
  146. * Should somehow figure out how to use a semaphore, not an atomic variable...
  147. */
  148. int driver_probe_done(void)
  149. {
  150. pr_debug("%s: probe_count = %d\n", __func__,
  151. atomic_read(&probe_count));
  152. if (atomic_read(&probe_count))
  153. return -EBUSY;
  154. return 0;
  155. }
  156. /**
  157. * wait_for_device_probe
  158. * Wait for device probing to be completed.
  159. */
  160. void wait_for_device_probe(void)
  161. {
  162. /* wait for the known devices to complete their probing */
  163. wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
  164. async_synchronize_full();
  165. }
  166. EXPORT_SYMBOL_GPL(wait_for_device_probe);
  167. /**
  168. * driver_probe_device - attempt to bind device & driver together
  169. * @drv: driver to bind a device to
  170. * @dev: device to try to bind to the driver
  171. *
  172. * This function returns -ENODEV if the device is not registered,
  173. * 1 if the device is bound successfully and 0 otherwise.
  174. *
  175. * This function must be called with @dev lock held. When called for a
  176. * USB interface, @dev->parent lock must be held as well.
  177. */
  178. int driver_probe_device(struct device_driver *drv, struct device *dev)
  179. {
  180. int ret = 0;
  181. if (!device_is_registered(dev))
  182. return -ENODEV;
  183. pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
  184. drv->bus->name, __func__, dev_name(dev), drv->name);
  185. pm_runtime_get_noresume(dev);
  186. pm_runtime_barrier(dev);
  187. ret = really_probe(dev, drv);
  188. pm_runtime_put_sync(dev);
  189. return ret;
  190. }
  191. static int __device_attach(struct device_driver *drv, void *data)
  192. {
  193. struct device *dev = data;
  194. if (!driver_match_device(drv, dev))
  195. return 0;
  196. return driver_probe_device(drv, dev);
  197. }
  198. /**
  199. * device_attach - try to attach device to a driver.
  200. * @dev: device.
  201. *
  202. * Walk the list of drivers that the bus has and call
  203. * driver_probe_device() for each pair. If a compatible
  204. * pair is found, break out and return.
  205. *
  206. * Returns 1 if the device was bound to a driver;
  207. * 0 if no matching driver was found;
  208. * -ENODEV if the device is not registered.
  209. *
  210. * When called for a USB interface, @dev->parent lock must be held.
  211. */
  212. int device_attach(struct device *dev)
  213. {
  214. int ret = 0;
  215. device_lock(dev);
  216. if (dev->driver) {
  217. if (klist_node_attached(&dev->p->knode_driver)) {
  218. ret = 1;
  219. goto out_unlock;
  220. }
  221. ret = device_bind_driver(dev);
  222. if (ret == 0)
  223. ret = 1;
  224. else {
  225. dev->driver = NULL;
  226. ret = 0;
  227. }
  228. } else {
  229. pm_runtime_get_noresume(dev);
  230. ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
  231. pm_runtime_put_sync(dev);
  232. }
  233. out_unlock:
  234. device_unlock(dev);
  235. return ret;
  236. }
  237. EXPORT_SYMBOL_GPL(device_attach);
  238. static int __driver_attach(struct device *dev, void *data)
  239. {
  240. struct device_driver *drv = data;
  241. /*
  242. * Lock device and try to bind to it. We drop the error
  243. * here and always return 0, because we need to keep trying
  244. * to bind to devices and some drivers will return an error
  245. * simply if it didn't support the device.
  246. *
  247. * driver_probe_device() will spit a warning if there
  248. * is an error.
  249. */
  250. if (!driver_match_device(drv, dev))
  251. return 0;
  252. if (dev->parent) /* Needed for USB */
  253. device_lock(dev->parent);
  254. device_lock(dev);
  255. if (!dev->driver)
  256. driver_probe_device(drv, dev);
  257. device_unlock(dev);
  258. if (dev->parent)
  259. device_unlock(dev->parent);
  260. return 0;
  261. }
  262. /**
  263. * driver_attach - try to bind driver to devices.
  264. * @drv: driver.
  265. *
  266. * Walk the list of devices that the bus has on it and try to
  267. * match the driver with each one. If driver_probe_device()
  268. * returns 0 and the @dev->driver is set, we've found a
  269. * compatible pair.
  270. */
  271. int driver_attach(struct device_driver *drv)
  272. {
  273. return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  274. }
  275. EXPORT_SYMBOL_GPL(driver_attach);
  276. /*
  277. * __device_release_driver() must be called with @dev lock held.
  278. * When called for a USB interface, @dev->parent lock must be held as well.
  279. */
  280. static void __device_release_driver(struct device *dev)
  281. {
  282. struct device_driver *drv;
  283. drv = dev->driver;
  284. if (drv) {
  285. pm_runtime_get_sync(dev);
  286. driver_sysfs_remove(dev);
  287. if (dev->bus)
  288. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  289. BUS_NOTIFY_UNBIND_DRIVER,
  290. dev);
  291. pm_runtime_put_sync(dev);
  292. if (dev->bus && dev->bus->remove)
  293. dev->bus->remove(dev);
  294. else if (drv->remove)
  295. drv->remove(dev);
  296. devres_release_all(dev);
  297. dev->driver = NULL;
  298. klist_remove(&dev->p->knode_driver);
  299. if (dev->bus)
  300. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  301. BUS_NOTIFY_UNBOUND_DRIVER,
  302. dev);
  303. }
  304. }
  305. /**
  306. * device_release_driver - manually detach device from driver.
  307. * @dev: device.
  308. *
  309. * Manually detach device from driver.
  310. * When called for a USB interface, @dev->parent lock must be held.
  311. */
  312. void device_release_driver(struct device *dev)
  313. {
  314. /*
  315. * If anyone calls device_release_driver() recursively from
  316. * within their ->remove callback for the same device, they
  317. * will deadlock right here.
  318. */
  319. device_lock(dev);
  320. __device_release_driver(dev);
  321. device_unlock(dev);
  322. }
  323. EXPORT_SYMBOL_GPL(device_release_driver);
  324. /**
  325. * driver_detach - detach driver from all devices it controls.
  326. * @drv: driver.
  327. */
  328. void driver_detach(struct device_driver *drv)
  329. {
  330. struct device_private *dev_prv;
  331. struct device *dev;
  332. for (;;) {
  333. spin_lock(&drv->p->klist_devices.k_lock);
  334. if (list_empty(&drv->p->klist_devices.k_list)) {
  335. spin_unlock(&drv->p->klist_devices.k_lock);
  336. break;
  337. }
  338. dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
  339. struct device_private,
  340. knode_driver.n_node);
  341. dev = dev_prv->device;
  342. get_device(dev);
  343. spin_unlock(&drv->p->klist_devices.k_lock);
  344. if (dev->parent) /* Needed for USB */
  345. device_lock(dev->parent);
  346. device_lock(dev);
  347. if (dev->driver == drv)
  348. __device_release_driver(dev);
  349. device_unlock(dev);
  350. if (dev->parent)
  351. device_unlock(dev->parent);
  352. put_device(dev);
  353. }
  354. }
  355. /*
  356. * These exports can't be _GPL due to .h files using this within them, and it
  357. * might break something that was previously working...
  358. */
  359. void *dev_get_drvdata(const struct device *dev)
  360. {
  361. if (dev && dev->p)
  362. return dev->p->driver_data;
  363. return NULL;
  364. }
  365. EXPORT_SYMBOL(dev_get_drvdata);
  366. int dev_set_drvdata(struct device *dev, void *data)
  367. {
  368. int error;
  369. if (!dev->p) {
  370. error = device_private_init(dev);
  371. if (error)
  372. return error;
  373. }
  374. dev->p->driver_data = data;
  375. return 0;
  376. }
  377. EXPORT_SYMBOL(dev_set_drvdata);