dd.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. data->drv = drv;
  158. data->dev = dev;
  159. if (drv->multithread_probe) {
  160. probe_task = kthread_run(really_probe, data,
  161. "probe-%s", dev->bus_id);
  162. if (IS_ERR(probe_task))
  163. ret = PTR_ERR(probe_task);
  164. } else
  165. ret = really_probe(data);
  166. done:
  167. return ret;
  168. }
  169. static int __device_attach(struct device_driver * drv, void * data)
  170. {
  171. struct device * dev = data;
  172. return driver_probe_device(drv, dev);
  173. }
  174. /**
  175. * device_attach - try to attach device to a driver.
  176. * @dev: device.
  177. *
  178. * Walk the list of drivers that the bus has and call
  179. * driver_probe_device() for each pair. If a compatible
  180. * pair is found, break out and return.
  181. *
  182. * Returns 1 if the device was bound to a driver;
  183. * 0 if no matching device was found; error code otherwise.
  184. *
  185. * When called for a USB interface, @dev->parent->sem must be held.
  186. */
  187. int device_attach(struct device * dev)
  188. {
  189. int ret = 0;
  190. down(&dev->sem);
  191. if (dev->driver) {
  192. ret = device_bind_driver(dev);
  193. if (ret == 0)
  194. ret = 1;
  195. } else
  196. ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
  197. up(&dev->sem);
  198. return ret;
  199. }
  200. static int __driver_attach(struct device * dev, void * data)
  201. {
  202. struct device_driver * drv = data;
  203. /*
  204. * Lock device and try to bind to it. We drop the error
  205. * here and always return 0, because we need to keep trying
  206. * to bind to devices and some drivers will return an error
  207. * simply if it didn't support the device.
  208. *
  209. * driver_probe_device() will spit a warning if there
  210. * is an error.
  211. */
  212. if (dev->parent) /* Needed for USB */
  213. down(&dev->parent->sem);
  214. down(&dev->sem);
  215. if (!dev->driver)
  216. driver_probe_device(drv, dev);
  217. up(&dev->sem);
  218. if (dev->parent)
  219. up(&dev->parent->sem);
  220. return 0;
  221. }
  222. /**
  223. * driver_attach - try to bind driver to devices.
  224. * @drv: driver.
  225. *
  226. * Walk the list of devices that the bus has on it and try to
  227. * match the driver with each one. If driver_probe_device()
  228. * returns 0 and the @dev->driver is set, we've found a
  229. * compatible pair.
  230. */
  231. int driver_attach(struct device_driver * drv)
  232. {
  233. return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  234. }
  235. /**
  236. * device_release_driver - manually detach device from driver.
  237. * @dev: device.
  238. *
  239. * Manually detach device from driver.
  240. *
  241. * __device_release_driver() must be called with @dev->sem held.
  242. * When called for a USB interface, @dev->parent->sem must be held
  243. * as well.
  244. */
  245. static void __device_release_driver(struct device * dev)
  246. {
  247. struct device_driver * drv;
  248. drv = dev->driver;
  249. if (drv) {
  250. get_driver(drv);
  251. sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
  252. sysfs_remove_link(&dev->kobj, "driver");
  253. klist_remove(&dev->knode_driver);
  254. if (dev->bus && dev->bus->remove)
  255. dev->bus->remove(dev);
  256. else if (drv->remove)
  257. drv->remove(dev);
  258. dev->driver = NULL;
  259. put_driver(drv);
  260. }
  261. }
  262. void device_release_driver(struct device * dev)
  263. {
  264. /*
  265. * If anyone calls device_release_driver() recursively from
  266. * within their ->remove callback for the same device, they
  267. * will deadlock right here.
  268. */
  269. down(&dev->sem);
  270. __device_release_driver(dev);
  271. up(&dev->sem);
  272. }
  273. /**
  274. * driver_detach - detach driver from all devices it controls.
  275. * @drv: driver.
  276. */
  277. void driver_detach(struct device_driver * drv)
  278. {
  279. struct device * dev;
  280. for (;;) {
  281. spin_lock(&drv->klist_devices.k_lock);
  282. if (list_empty(&drv->klist_devices.k_list)) {
  283. spin_unlock(&drv->klist_devices.k_lock);
  284. break;
  285. }
  286. dev = list_entry(drv->klist_devices.k_list.prev,
  287. struct device, knode_driver.n_node);
  288. get_device(dev);
  289. spin_unlock(&drv->klist_devices.k_lock);
  290. if (dev->parent) /* Needed for USB */
  291. down(&dev->parent->sem);
  292. down(&dev->sem);
  293. if (dev->driver == drv)
  294. __device_release_driver(dev);
  295. up(&dev->sem);
  296. if (dev->parent)
  297. up(&dev->parent->sem);
  298. put_device(dev);
  299. }
  300. }
  301. EXPORT_SYMBOL_GPL(device_bind_driver);
  302. EXPORT_SYMBOL_GPL(device_release_driver);
  303. EXPORT_SYMBOL_GPL(device_attach);
  304. EXPORT_SYMBOL_GPL(driver_attach);