dd.c 9.3 KB

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