dd.c 8.9 KB

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