dd.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. struct stupid_thread_structure {
  83. struct device_driver *drv;
  84. struct device *dev;
  85. };
  86. static atomic_t probe_count = ATOMIC_INIT(0);
  87. static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
  88. static int really_probe(void *void_data)
  89. {
  90. struct stupid_thread_structure *data = void_data;
  91. struct device_driver *drv = data->drv;
  92. struct device *dev = data->dev;
  93. int ret = 0;
  94. atomic_inc(&probe_count);
  95. pr_debug("%s: Probing driver %s with device %s\n",
  96. drv->bus->name, drv->name, dev->bus_id);
  97. dev->driver = drv;
  98. if (driver_sysfs_add(dev)) {
  99. printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
  100. __FUNCTION__, dev->bus_id);
  101. goto probe_failed;
  102. }
  103. if (dev->bus->probe) {
  104. ret = dev->bus->probe(dev);
  105. if (ret)
  106. goto probe_failed;
  107. } else if (drv->probe) {
  108. ret = drv->probe(dev);
  109. if (ret)
  110. goto probe_failed;
  111. }
  112. driver_bound(dev);
  113. ret = 1;
  114. pr_debug("%s: Bound Device %s to Driver %s\n",
  115. drv->bus->name, dev->bus_id, drv->name);
  116. goto done;
  117. probe_failed:
  118. driver_sysfs_remove(dev);
  119. dev->driver = NULL;
  120. if (ret != -ENODEV && ret != -ENXIO) {
  121. /* driver matched but the probe failed */
  122. printk(KERN_WARNING
  123. "%s: probe of %s failed with error %d\n",
  124. drv->name, dev->bus_id, ret);
  125. }
  126. /*
  127. * Ignore errors returned by ->probe so that the next driver can try
  128. * its luck.
  129. */
  130. ret = 0;
  131. done:
  132. kfree(data);
  133. atomic_dec(&probe_count);
  134. wake_up(&probe_waitqueue);
  135. return ret;
  136. }
  137. /**
  138. * driver_probe_done
  139. * Determine if the probe sequence is finished or not.
  140. *
  141. * Should somehow figure out how to use a semaphore, not an atomic variable...
  142. */
  143. int driver_probe_done(void)
  144. {
  145. pr_debug("%s: probe_count = %d\n", __FUNCTION__,
  146. atomic_read(&probe_count));
  147. if (atomic_read(&probe_count))
  148. return -EBUSY;
  149. return 0;
  150. }
  151. /**
  152. * driver_probe_device - attempt to bind device & driver together
  153. * @drv: driver to bind a device to
  154. * @dev: device to try to bind to the driver
  155. *
  156. * First, we call the bus's match function, if one present, which should
  157. * compare the device IDs the driver supports with the device IDs of the
  158. * device. Note we don't do this ourselves because we don't know the
  159. * format of the ID structures, nor what is to be considered a match and
  160. * what is not.
  161. *
  162. * This function returns 1 if a match is found, an error if one occurs
  163. * (that is not -ENODEV or -ENXIO), and 0 otherwise.
  164. *
  165. * This function must be called with @dev->sem held. When called for a
  166. * USB interface, @dev->parent->sem must be held as well.
  167. */
  168. int driver_probe_device(struct device_driver * drv, struct device * dev)
  169. {
  170. struct stupid_thread_structure *data;
  171. struct task_struct *probe_task;
  172. int ret = 0;
  173. if (!device_is_registered(dev))
  174. return -ENODEV;
  175. if (drv->bus->match && !drv->bus->match(dev, drv))
  176. goto done;
  177. pr_debug("%s: Matched Device %s with Driver %s\n",
  178. drv->bus->name, dev->bus_id, drv->name);
  179. data = kmalloc(sizeof(*data), GFP_KERNEL);
  180. if (!data)
  181. return -ENOMEM;
  182. data->drv = drv;
  183. data->dev = dev;
  184. if (drv->multithread_probe) {
  185. probe_task = kthread_run(really_probe, data,
  186. "probe-%s", dev->bus_id);
  187. if (IS_ERR(probe_task))
  188. ret = really_probe(data);
  189. } else
  190. ret = really_probe(data);
  191. done:
  192. return ret;
  193. }
  194. static int __device_attach(struct device_driver * drv, void * data)
  195. {
  196. struct device * dev = data;
  197. return driver_probe_device(drv, dev);
  198. }
  199. /**
  200. * device_attach - try to attach device to a driver.
  201. * @dev: device.
  202. *
  203. * Walk the list of drivers that the bus has and call
  204. * driver_probe_device() for each pair. If a compatible
  205. * pair is found, break out and return.
  206. *
  207. * Returns 1 if the device was bound to a driver;
  208. * 0 if no matching device was found; error code otherwise.
  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. ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
  222. up(&dev->sem);
  223. return ret;
  224. }
  225. static int __driver_attach(struct device * dev, void * data)
  226. {
  227. struct device_driver * drv = data;
  228. /*
  229. * Lock device and try to bind to it. We drop the error
  230. * here and always return 0, because we need to keep trying
  231. * to bind to devices and some drivers will return an error
  232. * simply if it didn't support the device.
  233. *
  234. * driver_probe_device() will spit a warning if there
  235. * is an error.
  236. */
  237. if (dev->parent) /* Needed for USB */
  238. down(&dev->parent->sem);
  239. down(&dev->sem);
  240. if (!dev->driver)
  241. driver_probe_device(drv, dev);
  242. up(&dev->sem);
  243. if (dev->parent)
  244. up(&dev->parent->sem);
  245. return 0;
  246. }
  247. /**
  248. * driver_attach - try to bind driver to devices.
  249. * @drv: driver.
  250. *
  251. * Walk the list of devices that the bus has on it and try to
  252. * match the driver with each one. If driver_probe_device()
  253. * returns 0 and the @dev->driver is set, we've found a
  254. * compatible pair.
  255. */
  256. int driver_attach(struct device_driver * drv)
  257. {
  258. return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  259. }
  260. /**
  261. * device_release_driver - manually detach device from driver.
  262. * @dev: device.
  263. *
  264. * Manually detach device from driver.
  265. *
  266. * __device_release_driver() must be called with @dev->sem held.
  267. * When called for a USB interface, @dev->parent->sem must be held
  268. * as well.
  269. */
  270. static void __device_release_driver(struct device * dev)
  271. {
  272. struct device_driver * drv;
  273. drv = dev->driver;
  274. if (drv) {
  275. get_driver(drv);
  276. driver_sysfs_remove(dev);
  277. sysfs_remove_link(&dev->kobj, "driver");
  278. klist_remove(&dev->knode_driver);
  279. if (dev->bus)
  280. blocking_notifier_call_chain(&dev->bus->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. dev->driver = NULL;
  288. put_driver(drv);
  289. }
  290. }
  291. void device_release_driver(struct device * dev)
  292. {
  293. /*
  294. * If anyone calls device_release_driver() recursively from
  295. * within their ->remove callback for the same device, they
  296. * will deadlock right here.
  297. */
  298. down(&dev->sem);
  299. __device_release_driver(dev);
  300. up(&dev->sem);
  301. }
  302. /**
  303. * driver_detach - detach driver from all devices it controls.
  304. * @drv: driver.
  305. */
  306. void driver_detach(struct device_driver * drv)
  307. {
  308. struct device * dev;
  309. for (;;) {
  310. spin_lock(&drv->klist_devices.k_lock);
  311. if (list_empty(&drv->klist_devices.k_list)) {
  312. spin_unlock(&drv->klist_devices.k_lock);
  313. break;
  314. }
  315. dev = list_entry(drv->klist_devices.k_list.prev,
  316. struct device, knode_driver.n_node);
  317. get_device(dev);
  318. spin_unlock(&drv->klist_devices.k_lock);
  319. if (dev->parent) /* Needed for USB */
  320. down(&dev->parent->sem);
  321. down(&dev->sem);
  322. if (dev->driver == drv)
  323. __device_release_driver(dev);
  324. up(&dev->sem);
  325. if (dev->parent)
  326. up(&dev->parent->sem);
  327. put_device(dev);
  328. }
  329. }
  330. #ifdef CONFIG_PCI_MULTITHREAD_PROBE
  331. static int __init wait_for_probes(void)
  332. {
  333. DEFINE_WAIT(wait);
  334. printk(KERN_INFO "%s: waiting for %d threads\n", __FUNCTION__,
  335. atomic_read(&probe_count));
  336. if (!atomic_read(&probe_count))
  337. return 0;
  338. while (atomic_read(&probe_count)) {
  339. prepare_to_wait(&probe_waitqueue, &wait, TASK_UNINTERRUPTIBLE);
  340. if (atomic_read(&probe_count))
  341. schedule();
  342. }
  343. finish_wait(&probe_waitqueue, &wait);
  344. return 0;
  345. }
  346. core_initcall_sync(wait_for_probes);
  347. postcore_initcall_sync(wait_for_probes);
  348. arch_initcall_sync(wait_for_probes);
  349. subsys_initcall_sync(wait_for_probes);
  350. fs_initcall_sync(wait_for_probes);
  351. device_initcall_sync(wait_for_probes);
  352. late_initcall_sync(wait_for_probes);
  353. #endif
  354. EXPORT_SYMBOL_GPL(device_bind_driver);
  355. EXPORT_SYMBOL_GPL(device_release_driver);
  356. EXPORT_SYMBOL_GPL(device_attach);
  357. EXPORT_SYMBOL_GPL(driver_attach);