glue.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. return sta_present ? FIND_CHILD_MAX_SCORE : FIND_CHILD_MIN_SCORE;
  90. }
  91. struct acpi_device *acpi_find_child_device(struct acpi_device *parent,
  92. u64 address, bool check_children)
  93. {
  94. struct acpi_device *adev, *ret = NULL;
  95. int ret_score = 0;
  96. if (!parent)
  97. return NULL;
  98. list_for_each_entry(adev, &parent->children, node) {
  99. unsigned long long addr;
  100. acpi_status status;
  101. int score;
  102. status = acpi_evaluate_integer(adev->handle, METHOD_NAME__ADR,
  103. NULL, &addr);
  104. if (ACPI_FAILURE(status) || addr != address)
  105. continue;
  106. if (!ret) {
  107. /* This is the first matching object. Save it. */
  108. ret = adev;
  109. continue;
  110. }
  111. /*
  112. * There is more than one matching device object with the same
  113. * _ADR value. That really is unexpected, so we are kind of
  114. * beyond the scope of the spec here. We have to choose which
  115. * one to return, though.
  116. *
  117. * First, check if the previously found object is good enough
  118. * and return it if so. Second, do the same for the object that
  119. * we've just found.
  120. */
  121. if (!ret_score) {
  122. ret_score = find_child_checks(ret, check_children);
  123. if (ret_score == FIND_CHILD_MAX_SCORE)
  124. return ret;
  125. }
  126. score = find_child_checks(adev, check_children);
  127. if (score == FIND_CHILD_MAX_SCORE) {
  128. return adev;
  129. } else if (score > ret_score) {
  130. ret = adev;
  131. ret_score = score;
  132. }
  133. }
  134. return ret;
  135. }
  136. EXPORT_SYMBOL_GPL(acpi_find_child_device);
  137. static void acpi_physnode_link_name(char *buf, unsigned int node_id)
  138. {
  139. if (node_id > 0)
  140. snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
  141. PHYSICAL_NODE_STRING "%u", node_id);
  142. else
  143. strcpy(buf, PHYSICAL_NODE_STRING);
  144. }
  145. int acpi_bind_one(struct device *dev, struct acpi_device *acpi_dev)
  146. {
  147. struct acpi_device_physical_node *physical_node, *pn;
  148. char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
  149. struct list_head *physnode_list;
  150. unsigned int node_id;
  151. int retval = -EINVAL;
  152. bool coherent;
  153. if (has_acpi_companion(dev)) {
  154. if (acpi_dev) {
  155. dev_warn(dev, "ACPI companion already set\n");
  156. return -EINVAL;
  157. } else {
  158. acpi_dev = ACPI_COMPANION(dev);
  159. }
  160. }
  161. if (!acpi_dev)
  162. return -EINVAL;
  163. get_device(&acpi_dev->dev);
  164. get_device(dev);
  165. physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
  166. if (!physical_node) {
  167. retval = -ENOMEM;
  168. goto err;
  169. }
  170. mutex_lock(&acpi_dev->physical_node_lock);
  171. /*
  172. * Keep the list sorted by node_id so that the IDs of removed nodes can
  173. * be recycled easily.
  174. */
  175. physnode_list = &acpi_dev->physical_node_list;
  176. node_id = 0;
  177. list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
  178. /* Sanity check. */
  179. if (pn->dev == dev) {
  180. mutex_unlock(&acpi_dev->physical_node_lock);
  181. dev_warn(dev, "Already associated with ACPI node\n");
  182. kfree(physical_node);
  183. if (ACPI_COMPANION(dev) != acpi_dev)
  184. goto err;
  185. put_device(dev);
  186. put_device(&acpi_dev->dev);
  187. return 0;
  188. }
  189. if (pn->node_id == node_id) {
  190. physnode_list = &pn->node;
  191. node_id++;
  192. }
  193. }
  194. physical_node->node_id = node_id;
  195. physical_node->dev = dev;
  196. list_add(&physical_node->node, physnode_list);
  197. acpi_dev->physical_node_count++;
  198. if (!has_acpi_companion(dev))
  199. ACPI_COMPANION_SET(dev, acpi_dev);
  200. if (acpi_check_dma(acpi_dev, &coherent))
  201. arch_setup_dma_ops(dev, 0, 0, NULL, coherent);
  202. acpi_physnode_link_name(physical_node_name, node_id);
  203. retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  204. physical_node_name);
  205. if (retval)
  206. dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
  207. physical_node_name, retval);
  208. retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  209. "firmware_node");
  210. if (retval)
  211. dev_err(dev, "Failed to create link firmware_node (%d)\n",
  212. retval);
  213. mutex_unlock(&acpi_dev->physical_node_lock);
  214. if (acpi_dev->wakeup.flags.valid)
  215. device_set_wakeup_capable(dev, true);
  216. return 0;
  217. err:
  218. ACPI_COMPANION_SET(dev, NULL);
  219. put_device(dev);
  220. put_device(&acpi_dev->dev);
  221. return retval;
  222. }
  223. EXPORT_SYMBOL_GPL(acpi_bind_one);
  224. int acpi_unbind_one(struct device *dev)
  225. {
  226. struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
  227. struct acpi_device_physical_node *entry;
  228. if (!acpi_dev)
  229. return 0;
  230. mutex_lock(&acpi_dev->physical_node_lock);
  231. list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
  232. if (entry->dev == dev) {
  233. char physnode_name[PHYSICAL_NODE_NAME_SIZE];
  234. list_del(&entry->node);
  235. acpi_dev->physical_node_count--;
  236. acpi_physnode_link_name(physnode_name, entry->node_id);
  237. sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
  238. sysfs_remove_link(&dev->kobj, "firmware_node");
  239. ACPI_COMPANION_SET(dev, NULL);
  240. /* Drop references taken by acpi_bind_one(). */
  241. put_device(dev);
  242. put_device(&acpi_dev->dev);
  243. kfree(entry);
  244. break;
  245. }
  246. mutex_unlock(&acpi_dev->physical_node_lock);
  247. return 0;
  248. }
  249. EXPORT_SYMBOL_GPL(acpi_unbind_one);
  250. static int acpi_platform_notify(struct device *dev)
  251. {
  252. struct acpi_bus_type *type = acpi_get_bus_type(dev);
  253. struct acpi_device *adev;
  254. int ret;
  255. ret = acpi_bind_one(dev, NULL);
  256. if (ret && type) {
  257. struct acpi_device *adev;
  258. adev = type->find_companion(dev);
  259. if (!adev) {
  260. DBG("Unable to get handle for %s\n", dev_name(dev));
  261. ret = -ENODEV;
  262. goto out;
  263. }
  264. ret = acpi_bind_one(dev, adev);
  265. if (ret)
  266. goto out;
  267. }
  268. adev = ACPI_COMPANION(dev);
  269. if (!adev)
  270. goto out;
  271. if (type && type->setup)
  272. type->setup(dev);
  273. else if (adev->handler && adev->handler->bind)
  274. adev->handler->bind(dev);
  275. out:
  276. #if ACPI_GLUE_DEBUG
  277. if (!ret) {
  278. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  279. acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
  280. DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
  281. kfree(buffer.pointer);
  282. } else
  283. DBG("Device %s -> No ACPI support\n", dev_name(dev));
  284. #endif
  285. return ret;
  286. }
  287. static int acpi_platform_notify_remove(struct device *dev)
  288. {
  289. struct acpi_device *adev = ACPI_COMPANION(dev);
  290. struct acpi_bus_type *type;
  291. if (!adev)
  292. return 0;
  293. type = acpi_get_bus_type(dev);
  294. if (type && type->cleanup)
  295. type->cleanup(dev);
  296. else if (adev->handler && adev->handler->unbind)
  297. adev->handler->unbind(dev);
  298. acpi_unbind_one(dev);
  299. return 0;
  300. }
  301. int __init init_acpi_device_notify(void)
  302. {
  303. if (platform_notify || platform_notify_remove) {
  304. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  305. return 0;
  306. }
  307. platform_notify = acpi_platform_notify;
  308. platform_notify_remove = acpi_platform_notify_remove;
  309. return 0;
  310. }