glue.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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_device) {
  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. static acpi_status acpi_dev_present(acpi_handle handle, u32 lvl_not_used,
  75. void *not_used, void **ret_p)
  76. {
  77. struct acpi_device *adev = NULL;
  78. acpi_bus_get_device(handle, &adev);
  79. if (adev) {
  80. *ret_p = handle;
  81. return AE_CTRL_TERMINATE;
  82. }
  83. return AE_OK;
  84. }
  85. static bool acpi_extra_checks_passed(acpi_handle handle, bool is_bridge)
  86. {
  87. unsigned long long sta;
  88. acpi_status status;
  89. status = acpi_bus_get_status_handle(handle, &sta);
  90. if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
  91. return false;
  92. if (is_bridge) {
  93. void *test = NULL;
  94. /* Check if this object has at least one child device. */
  95. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  96. acpi_dev_present, NULL, NULL, &test);
  97. return !!test;
  98. }
  99. return true;
  100. }
  101. struct find_child_context {
  102. u64 addr;
  103. bool is_bridge;
  104. acpi_handle ret;
  105. bool ret_checked;
  106. };
  107. static acpi_status do_find_child(acpi_handle handle, u32 lvl_not_used,
  108. void *data, void **not_used)
  109. {
  110. struct find_child_context *context = data;
  111. unsigned long long addr;
  112. acpi_status status;
  113. status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &addr);
  114. if (ACPI_FAILURE(status) || addr != context->addr)
  115. return AE_OK;
  116. if (!context->ret) {
  117. /* This is the first matching object. Save its handle. */
  118. context->ret = handle;
  119. return AE_OK;
  120. }
  121. /*
  122. * There is more than one matching object with the same _ADR value.
  123. * That really is unexpected, so we are kind of beyond the scope of the
  124. * spec here. We have to choose which one to return, though.
  125. *
  126. * First, check if the previously found object is good enough and return
  127. * its handle if so. Second, check the same for the object that we've
  128. * just found.
  129. */
  130. if (!context->ret_checked) {
  131. if (acpi_extra_checks_passed(context->ret, context->is_bridge))
  132. return AE_CTRL_TERMINATE;
  133. else
  134. context->ret_checked = true;
  135. }
  136. if (acpi_extra_checks_passed(handle, context->is_bridge)) {
  137. context->ret = handle;
  138. return AE_CTRL_TERMINATE;
  139. }
  140. return AE_OK;
  141. }
  142. acpi_handle acpi_find_child(acpi_handle parent, u64 addr, bool is_bridge)
  143. {
  144. if (parent) {
  145. struct find_child_context context = {
  146. .addr = addr,
  147. .is_bridge = is_bridge,
  148. };
  149. acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, 1, do_find_child,
  150. NULL, &context, NULL);
  151. return context.ret;
  152. }
  153. return NULL;
  154. }
  155. EXPORT_SYMBOL_GPL(acpi_find_child);
  156. static void acpi_physnode_link_name(char *buf, unsigned int node_id)
  157. {
  158. if (node_id > 0)
  159. snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
  160. PHYSICAL_NODE_STRING "%u", node_id);
  161. else
  162. strcpy(buf, PHYSICAL_NODE_STRING);
  163. }
  164. int acpi_bind_one(struct device *dev, acpi_handle handle)
  165. {
  166. struct acpi_device *acpi_dev;
  167. acpi_status status;
  168. struct acpi_device_physical_node *physical_node, *pn;
  169. char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
  170. struct list_head *physnode_list;
  171. unsigned int node_id;
  172. int retval = -EINVAL;
  173. if (ACPI_HANDLE(dev)) {
  174. if (handle) {
  175. dev_warn(dev, "ACPI handle is already set\n");
  176. return -EINVAL;
  177. } else {
  178. handle = ACPI_HANDLE(dev);
  179. }
  180. }
  181. if (!handle)
  182. return -EINVAL;
  183. get_device(dev);
  184. status = acpi_bus_get_device(handle, &acpi_dev);
  185. if (ACPI_FAILURE(status))
  186. goto err;
  187. physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
  188. if (!physical_node) {
  189. retval = -ENOMEM;
  190. goto err;
  191. }
  192. mutex_lock(&acpi_dev->physical_node_lock);
  193. /*
  194. * Keep the list sorted by node_id so that the IDs of removed nodes can
  195. * be recycled easily.
  196. */
  197. physnode_list = &acpi_dev->physical_node_list;
  198. node_id = 0;
  199. list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
  200. /* Sanity check. */
  201. if (pn->dev == dev) {
  202. dev_warn(dev, "Already associated with ACPI node\n");
  203. if (ACPI_HANDLE(dev) == handle)
  204. retval = 0;
  205. goto out_free;
  206. }
  207. if (pn->node_id == node_id) {
  208. physnode_list = &pn->node;
  209. node_id++;
  210. }
  211. }
  212. physical_node->node_id = node_id;
  213. physical_node->dev = dev;
  214. list_add(&physical_node->node, physnode_list);
  215. acpi_dev->physical_node_count++;
  216. if (!ACPI_HANDLE(dev))
  217. ACPI_HANDLE_SET(dev, acpi_dev->handle);
  218. acpi_physnode_link_name(physical_node_name, node_id);
  219. retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  220. physical_node_name);
  221. retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  222. "firmware_node");
  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_HANDLE_SET(dev, NULL);
  229. put_device(dev);
  230. return retval;
  231. out_free:
  232. mutex_unlock(&acpi_dev->physical_node_lock);
  233. kfree(physical_node);
  234. if (retval)
  235. goto err;
  236. put_device(dev);
  237. return 0;
  238. }
  239. EXPORT_SYMBOL_GPL(acpi_bind_one);
  240. int acpi_unbind_one(struct device *dev)
  241. {
  242. struct acpi_device_physical_node *entry;
  243. struct acpi_device *acpi_dev;
  244. acpi_status status;
  245. struct list_head *node, *next;
  246. if (!ACPI_HANDLE(dev))
  247. return 0;
  248. status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
  249. if (ACPI_FAILURE(status))
  250. goto err;
  251. mutex_lock(&acpi_dev->physical_node_lock);
  252. list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
  253. char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
  254. entry = list_entry(node, struct acpi_device_physical_node,
  255. node);
  256. if (entry->dev != dev)
  257. continue;
  258. list_del(node);
  259. acpi_dev->physical_node_count--;
  260. acpi_physnode_link_name(physical_node_name, entry->node_id);
  261. sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
  262. sysfs_remove_link(&dev->kobj, "firmware_node");
  263. ACPI_HANDLE_SET(dev, NULL);
  264. /* acpi_bind_one increase refcnt by one */
  265. put_device(dev);
  266. kfree(entry);
  267. }
  268. mutex_unlock(&acpi_dev->physical_node_lock);
  269. return 0;
  270. err:
  271. dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
  272. return -EINVAL;
  273. }
  274. EXPORT_SYMBOL_GPL(acpi_unbind_one);
  275. static int acpi_platform_notify(struct device *dev)
  276. {
  277. struct acpi_bus_type *type = acpi_get_bus_type(dev);
  278. acpi_handle handle;
  279. int ret;
  280. ret = acpi_bind_one(dev, NULL);
  281. if (ret && type) {
  282. ret = type->find_device(dev, &handle);
  283. if (ret) {
  284. DBG("Unable to get handle for %s\n", dev_name(dev));
  285. goto out;
  286. }
  287. ret = acpi_bind_one(dev, handle);
  288. if (ret)
  289. goto out;
  290. }
  291. if (type && type->setup)
  292. type->setup(dev);
  293. out:
  294. #if ACPI_GLUE_DEBUG
  295. if (!ret) {
  296. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  297. acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
  298. DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
  299. kfree(buffer.pointer);
  300. } else
  301. DBG("Device %s -> No ACPI support\n", dev_name(dev));
  302. #endif
  303. return ret;
  304. }
  305. static int acpi_platform_notify_remove(struct device *dev)
  306. {
  307. struct acpi_bus_type *type;
  308. type = acpi_get_bus_type(dev);
  309. if (type && type->cleanup)
  310. type->cleanup(dev);
  311. acpi_unbind_one(dev);
  312. return 0;
  313. }
  314. int __init init_acpi_device_notify(void)
  315. {
  316. if (platform_notify || platform_notify_remove) {
  317. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  318. return 0;
  319. }
  320. platform_notify = acpi_platform_notify;
  321. platform_notify_remove = acpi_platform_notify_remove;
  322. return 0;
  323. }