dd.c 9.3 KB

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