dd.c 9.9 KB

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