glue.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. mutex_unlock(&acpi_dev->physical_node_lock);
  203. dev_warn(dev, "Already associated with ACPI node\n");
  204. kfree(physical_node);
  205. if (ACPI_HANDLE(dev) != handle)
  206. goto err;
  207. put_device(dev);
  208. return 0;
  209. }
  210. if (pn->node_id == node_id) {
  211. physnode_list = &pn->node;
  212. node_id++;
  213. }
  214. }
  215. physical_node->node_id = node_id;
  216. physical_node->dev = dev;
  217. list_add(&physical_node->node, physnode_list);
  218. acpi_dev->physical_node_count++;
  219. if (!ACPI_HANDLE(dev))
  220. ACPI_HANDLE_SET(dev, acpi_dev->handle);
  221. acpi_physnode_link_name(physical_node_name, node_id);
  222. retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  223. physical_node_name);
  224. if (retval)
  225. dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
  226. physical_node_name, retval);
  227. retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  228. "firmware_node");
  229. if (retval)
  230. dev_err(dev, "Failed to create link firmware_node (%d)\n",
  231. retval);
  232. mutex_unlock(&acpi_dev->physical_node_lock);
  233. if (acpi_dev->wakeup.flags.valid)
  234. device_set_wakeup_capable(dev, true);
  235. return 0;
  236. err:
  237. ACPI_HANDLE_SET(dev, NULL);
  238. put_device(dev);
  239. return retval;
  240. }
  241. EXPORT_SYMBOL_GPL(acpi_bind_one);
  242. int acpi_unbind_one(struct device *dev)
  243. {
  244. struct acpi_device_physical_node *entry;
  245. struct acpi_device *acpi_dev;
  246. acpi_status status;
  247. if (!ACPI_HANDLE(dev))
  248. return 0;
  249. status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
  250. if (ACPI_FAILURE(status)) {
  251. dev_err(dev, "Oops, ACPI handle corrupt in %s()\n", __func__);
  252. return -EINVAL;
  253. }
  254. mutex_lock(&acpi_dev->physical_node_lock);
  255. list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
  256. if (entry->dev == dev) {
  257. char physnode_name[PHYSICAL_NODE_NAME_SIZE];
  258. list_del(&entry->node);
  259. acpi_dev->physical_node_count--;
  260. acpi_physnode_link_name(physnode_name, entry->node_id);
  261. sysfs_remove_link(&acpi_dev->dev.kobj, physnode_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. break;
  268. }
  269. mutex_unlock(&acpi_dev->physical_node_lock);
  270. return 0;
  271. }
  272. EXPORT_SYMBOL_GPL(acpi_unbind_one);
  273. static int acpi_platform_notify(struct device *dev)
  274. {
  275. struct acpi_bus_type *type = acpi_get_bus_type(dev);
  276. acpi_handle handle;
  277. int ret;
  278. ret = acpi_bind_one(dev, NULL);
  279. if (ret && type) {
  280. ret = type->find_device(dev, &handle);
  281. if (ret) {
  282. DBG("Unable to get handle for %s\n", dev_name(dev));
  283. goto out;
  284. }
  285. ret = acpi_bind_one(dev, handle);
  286. if (ret)
  287. goto out;
  288. }
  289. if (type && type->setup)
  290. type->setup(dev);
  291. out:
  292. #if ACPI_GLUE_DEBUG
  293. if (!ret) {
  294. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  295. acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
  296. DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
  297. kfree(buffer.pointer);
  298. } else
  299. DBG("Device %s -> No ACPI support\n", dev_name(dev));
  300. #endif
  301. return ret;
  302. }
  303. static int acpi_platform_notify_remove(struct device *dev)
  304. {
  305. struct acpi_bus_type *type;
  306. type = acpi_get_bus_type(dev);
  307. if (type && type->cleanup)
  308. type->cleanup(dev);
  309. acpi_unbind_one(dev);
  310. return 0;
  311. }
  312. int __init init_acpi_device_notify(void)
  313. {
  314. if (platform_notify || platform_notify_remove) {
  315. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  316. return 0;
  317. }
  318. platform_notify = acpi_platform_notify;
  319. platform_notify_remove = acpi_platform_notify_remove;
  320. return 0;
  321. }