dd.c 10 KB

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