dd.c 8.5 KB

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