glue.c 8.4 KB

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