glue.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Link physical devices with ACPI devices support
  3. *
  4. * Copyright (c) 2005 David Shaohua Li <shaohua.li@intel.com>
  5. * Copyright (c) 2005 Intel Corp.
  6. *
  7. * This file is released under the GPLv2.
  8. */
  9. #include <linux/export.h>
  10. #include <linux/init.h>
  11. #include <linux/list.h>
  12. #include <linux/device.h>
  13. #include <linux/slab.h>
  14. #include <linux/rwsem.h>
  15. #include <linux/acpi.h>
  16. #include <linux/dma-mapping.h>
  17. #include "internal.h"
  18. #define ACPI_GLUE_DEBUG 0
  19. #if ACPI_GLUE_DEBUG
  20. #define DBG(fmt, ...) \
  21. printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__)
  22. #else
  23. #define DBG(fmt, ...) \
  24. do { \
  25. if (0) \
  26. printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__); \
  27. } while (0)
  28. #endif
  29. static LIST_HEAD(bus_type_list);
  30. static DECLARE_RWSEM(bus_type_sem);
  31. #define PHYSICAL_NODE_STRING "physical_node"
  32. #define PHYSICAL_NODE_NAME_SIZE (sizeof(PHYSICAL_NODE_STRING) + 10)
  33. int register_acpi_bus_type(struct acpi_bus_type *type)
  34. {
  35. if (acpi_disabled)
  36. return -ENODEV;
  37. if (type && type->match && type->find_companion) {
  38. down_write(&bus_type_sem);
  39. list_add_tail(&type->list, &bus_type_list);
  40. up_write(&bus_type_sem);
  41. printk(KERN_INFO PREFIX "bus type %s registered\n", type->name);
  42. return 0;
  43. }
  44. return -ENODEV;
  45. }
  46. EXPORT_SYMBOL_GPL(register_acpi_bus_type);
  47. int unregister_acpi_bus_type(struct acpi_bus_type *type)
  48. {
  49. if (acpi_disabled)
  50. return 0;
  51. if (type) {
  52. down_write(&bus_type_sem);
  53. list_del_init(&type->list);
  54. up_write(&bus_type_sem);
  55. printk(KERN_INFO PREFIX "bus type %s unregistered\n",
  56. type->name);
  57. return 0;
  58. }
  59. return -ENODEV;
  60. }
  61. EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
  62. static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
  63. {
  64. struct acpi_bus_type *tmp, *ret = NULL;
  65. down_read(&bus_type_sem);
  66. list_for_each_entry(tmp, &bus_type_list, list) {
  67. if (tmp->match(dev)) {
  68. ret = tmp;
  69. break;
  70. }
  71. }
  72. up_read(&bus_type_sem);
  73. return ret;
  74. }
  75. #define FIND_CHILD_MIN_SCORE 1
  76. #define FIND_CHILD_MAX_SCORE 2
  77. static int find_child_checks(struct acpi_device *adev, bool check_children)
  78. {
  79. bool sta_present = true;
  80. unsigned long long sta;
  81. acpi_status status;
  82. status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
  83. if (status == AE_NOT_FOUND)
  84. sta_present = false;
  85. else if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
  86. return -ENODEV;
  87. if (check_children && list_empty(&adev->children))
  88. return -ENODEV;
  89. /*
  90. * If the device has a _HID returning a valid ACPI/PNP device ID, it is
  91. * better to make it look less attractive here, so that the other device
  92. * with the same _ADR value (that may not have a valid device ID) can be
  93. * matched going forward. [This means a second spec violation in a row,
  94. * so whatever we do here is best effort anyway.]
  95. */
  96. return sta_present && !adev->pnp.type.platform_id ?
  97. FIND_CHILD_MAX_SCORE : FIND_CHILD_MIN_SCORE;
  98. }
  99. struct acpi_device *acpi_find_child_device(struct acpi_device *parent,
  100. u64 address, bool check_children)
  101. {
  102. struct acpi_device *adev, *ret = NULL;
  103. int ret_score = 0;
  104. if (!parent)
  105. return NULL;
  106. list_for_each_entry(adev, &parent->children, node) {
  107. unsigned long long addr;
  108. acpi_status status;
  109. int score;
  110. status = acpi_evaluate_integer(adev->handle, METHOD_NAME__ADR,
  111. NULL, &addr);
  112. if (ACPI_FAILURE(status) || addr != address)
  113. continue;
  114. if (!ret) {
  115. /* This is the first matching object. Save it. */
  116. ret = adev;
  117. continue;
  118. }
  119. /*
  120. * There is more than one matching device object with the same
  121. * _ADR value. That really is unexpected, so we are kind of
  122. * beyond the scope of the spec here. We have to choose which
  123. * one to return, though.
  124. *
  125. * First, check if the previously found object is good enough
  126. * and return it if so. Second, do the same for the object that
  127. * we've just found.
  128. */
  129. if (!ret_score) {
  130. ret_score = find_child_checks(ret, check_children);
  131. if (ret_score == FIND_CHILD_MAX_SCORE)
  132. return ret;
  133. }
  134. score = find_child_checks(adev, check_children);
  135. if (score == FIND_CHILD_MAX_SCORE) {
  136. return adev;
  137. } else if (score > ret_score) {
  138. ret = adev;
  139. ret_score = score;
  140. }
  141. }
  142. return ret;
  143. }
  144. EXPORT_SYMBOL_GPL(acpi_find_child_device);
  145. static void acpi_physnode_link_name(char *buf, unsigned int node_id)
  146. {
  147. if (node_id > 0)
  148. snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
  149. PHYSICAL_NODE_STRING "%u", node_id);
  150. else
  151. strcpy(buf, PHYSICAL_NODE_STRING);
  152. }
  153. int acpi_bind_one(struct device *dev, struct acpi_device *acpi_dev)
  154. {
  155. struct acpi_device_physical_node *physical_node, *pn;
  156. char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
  157. struct list_head *physnode_list;
  158. unsigned int node_id;
  159. int retval = -EINVAL;
  160. enum dev_dma_attr attr;
  161. if (has_acpi_companion(dev)) {
  162. if (acpi_dev) {
  163. dev_warn(dev, "ACPI companion already set\n");
  164. return -EINVAL;
  165. } else {
  166. acpi_dev = ACPI_COMPANION(dev);
  167. }
  168. }
  169. if (!acpi_dev)
  170. return -EINVAL;
  171. get_device(&acpi_dev->dev);
  172. get_device(dev);
  173. physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
  174. if (!physical_node) {
  175. retval = -ENOMEM;
  176. goto err;
  177. }
  178. mutex_lock(&acpi_dev->physical_node_lock);
  179. /*
  180. * Keep the list sorted by node_id so that the IDs of removed nodes can
  181. * be recycled easily.
  182. */
  183. physnode_list = &acpi_dev->physical_node_list;
  184. node_id = 0;
  185. list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
  186. /* Sanity check. */
  187. if (pn->dev == dev) {
  188. mutex_unlock(&acpi_dev->physical_node_lock);
  189. dev_warn(dev, "Already associated with ACPI node\n");
  190. kfree(physical_node);
  191. if (ACPI_COMPANION(dev) != acpi_dev)
  192. goto err;
  193. put_device(dev);
  194. put_device(&acpi_dev->dev);
  195. return 0;
  196. }
  197. if (pn->node_id == node_id) {
  198. physnode_list = &pn->node;
  199. node_id++;
  200. }
  201. }
  202. physical_node->node_id = node_id;
  203. physical_node->dev = dev;
  204. list_add(&physical_node->node, physnode_list);
  205. acpi_dev->physical_node_count++;
  206. if (!has_acpi_companion(dev))
  207. ACPI_COMPANION_SET(dev, acpi_dev);
  208. attr = acpi_get_dma_attr(acpi_dev);
  209. if (attr != DEV_DMA_NOT_SUPPORTED)
  210. arch_setup_dma_ops(dev, 0, 0, NULL,
  211. attr == DEV_DMA_COHERENT);
  212. acpi_physnode_link_name(physical_node_name, node_id);
  213. retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  214. physical_node_name);
  215. if (retval)
  216. dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
  217. physical_node_name, retval);
  218. retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  219. "firmware_node");
  220. if (retval)
  221. dev_err(dev, "Failed to create link firmware_node (%d)\n",
  222. retval);
  223. mutex_unlock(&acpi_dev->physical_node_lock);
  224. if (acpi_dev->wakeup.flags.valid)
  225. device_set_wakeup_capable(dev, true);
  226. return 0;
  227. err:
  228. ACPI_COMPANION_SET(dev, NULL);
  229. put_device(dev);
  230. put_device(&acpi_dev->dev);
  231. return retval;
  232. }
  233. EXPORT_SYMBOL_GPL(acpi_bind_one);
  234. int acpi_unbind_one(struct device *dev)
  235. {
  236. struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
  237. struct acpi_device_physical_node *entry;
  238. if (!acpi_dev)
  239. return 0;
  240. mutex_lock(&acpi_dev->physical_node_lock);
  241. list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
  242. if (entry->dev == dev) {
  243. char physnode_name[PHYSICAL_NODE_NAME_SIZE];
  244. list_del(&entry->node);
  245. acpi_dev->physical_node_count--;
  246. acpi_physnode_link_name(physnode_name, entry->node_id);
  247. sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
  248. sysfs_remove_link(&dev->kobj, "firmware_node");
  249. ACPI_COMPANION_SET(dev, NULL);
  250. /* Drop references taken by acpi_bind_one(). */
  251. put_device(dev);
  252. put_device(&acpi_dev->dev);
  253. kfree(entry);
  254. break;
  255. }
  256. mutex_unlock(&acpi_dev->physical_node_lock);
  257. return 0;
  258. }
  259. EXPORT_SYMBOL_GPL(acpi_unbind_one);
  260. static int acpi_platform_notify(struct device *dev)
  261. {
  262. struct acpi_bus_type *type = acpi_get_bus_type(dev);
  263. struct acpi_device *adev;
  264. int ret;
  265. ret = acpi_bind_one(dev, NULL);
  266. if (ret && type) {
  267. struct acpi_device *adev;
  268. adev = type->find_companion(dev);
  269. if (!adev) {
  270. DBG("Unable to get handle for %s\n", dev_name(dev));
  271. ret = -ENODEV;
  272. goto out;
  273. }
  274. ret = acpi_bind_one(dev, adev);
  275. if (ret)
  276. goto out;
  277. }
  278. adev = ACPI_COMPANION(dev);
  279. if (!adev)
  280. goto out;
  281. if (type && type->setup)
  282. type->setup(dev);
  283. else if (adev->handler && adev->handler->bind)
  284. adev->handler->bind(dev);
  285. out:
  286. #if ACPI_GLUE_DEBUG
  287. if (!ret) {
  288. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  289. acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
  290. DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
  291. kfree(buffer.pointer);
  292. } else
  293. DBG("Device %s -> No ACPI support\n", dev_name(dev));
  294. #endif
  295. return ret;
  296. }
  297. static int acpi_platform_notify_remove(struct device *dev)
  298. {
  299. struct acpi_device *adev = ACPI_COMPANION(dev);
  300. struct acpi_bus_type *type;
  301. if (!adev)
  302. return 0;
  303. type = acpi_get_bus_type(dev);
  304. if (type && type->cleanup)
  305. type->cleanup(dev);
  306. else if (adev->handler && adev->handler->unbind)
  307. adev->handler->unbind(dev);
  308. acpi_unbind_one(dev);
  309. return 0;
  310. }
  311. void __init init_acpi_device_notify(void)
  312. {
  313. if (platform_notify || platform_notify_remove) {
  314. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  315. return;
  316. }
  317. platform_notify = acpi_platform_notify;
  318. platform_notify_remove = acpi_platform_notify_remove;
  319. }