glue.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. int register_acpi_bus_type(struct acpi_bus_type *type)
  32. {
  33. if (acpi_disabled)
  34. return -ENODEV;
  35. if (type && type->match && type->find_device) {
  36. down_write(&bus_type_sem);
  37. list_add_tail(&type->list, &bus_type_list);
  38. up_write(&bus_type_sem);
  39. printk(KERN_INFO PREFIX "bus type %s registered\n", type->name);
  40. return 0;
  41. }
  42. return -ENODEV;
  43. }
  44. EXPORT_SYMBOL_GPL(register_acpi_bus_type);
  45. int unregister_acpi_bus_type(struct acpi_bus_type *type)
  46. {
  47. if (acpi_disabled)
  48. return 0;
  49. if (type) {
  50. down_write(&bus_type_sem);
  51. list_del_init(&type->list);
  52. up_write(&bus_type_sem);
  53. printk(KERN_INFO PREFIX "bus type %s unregistered\n",
  54. type->name);
  55. return 0;
  56. }
  57. return -ENODEV;
  58. }
  59. EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
  60. static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
  61. {
  62. struct acpi_bus_type *tmp, *ret = NULL;
  63. down_read(&bus_type_sem);
  64. list_for_each_entry(tmp, &bus_type_list, list) {
  65. if (tmp->match(dev)) {
  66. ret = tmp;
  67. break;
  68. }
  69. }
  70. up_read(&bus_type_sem);
  71. return ret;
  72. }
  73. static acpi_status do_acpi_find_child(acpi_handle handle, u32 lvl_not_used,
  74. void *addr_p, void **ret_p)
  75. {
  76. unsigned long long addr;
  77. acpi_status status;
  78. status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &addr);
  79. if (ACPI_SUCCESS(status) && addr == *((u64 *)addr_p)) {
  80. *ret_p = handle;
  81. return AE_CTRL_TERMINATE;
  82. }
  83. return AE_OK;
  84. }
  85. acpi_handle acpi_get_child(acpi_handle parent, u64 address)
  86. {
  87. void *ret = NULL;
  88. if (!parent)
  89. return NULL;
  90. acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, 1, NULL,
  91. do_acpi_find_child, &address, &ret);
  92. return (acpi_handle)ret;
  93. }
  94. EXPORT_SYMBOL(acpi_get_child);
  95. static int acpi_bind_one(struct device *dev, acpi_handle handle)
  96. {
  97. struct acpi_device *acpi_dev;
  98. acpi_status status;
  99. struct acpi_device_physical_node *physical_node, *pn;
  100. char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
  101. int retval = -EINVAL;
  102. if (ACPI_HANDLE(dev)) {
  103. if (handle) {
  104. dev_warn(dev, "ACPI handle is already set\n");
  105. return -EINVAL;
  106. } else {
  107. handle = ACPI_HANDLE(dev);
  108. }
  109. }
  110. if (!handle)
  111. return -EINVAL;
  112. get_device(dev);
  113. status = acpi_bus_get_device(handle, &acpi_dev);
  114. if (ACPI_FAILURE(status))
  115. goto err;
  116. physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
  117. if (!physical_node) {
  118. retval = -ENOMEM;
  119. goto err;
  120. }
  121. mutex_lock(&acpi_dev->physical_node_lock);
  122. /* Sanity check. */
  123. list_for_each_entry(pn, &acpi_dev->physical_node_list, node)
  124. if (pn->dev == dev) {
  125. dev_warn(dev, "Already associated with ACPI node\n");
  126. goto err_free;
  127. }
  128. /* allocate physical node id according to physical_node_id_bitmap */
  129. physical_node->node_id =
  130. find_first_zero_bit(acpi_dev->physical_node_id_bitmap,
  131. ACPI_MAX_PHYSICAL_NODE);
  132. if (physical_node->node_id >= ACPI_MAX_PHYSICAL_NODE) {
  133. retval = -ENOSPC;
  134. goto err_free;
  135. }
  136. set_bit(physical_node->node_id, acpi_dev->physical_node_id_bitmap);
  137. physical_node->dev = dev;
  138. list_add_tail(&physical_node->node, &acpi_dev->physical_node_list);
  139. acpi_dev->physical_node_count++;
  140. mutex_unlock(&acpi_dev->physical_node_lock);
  141. if (!ACPI_HANDLE(dev))
  142. ACPI_HANDLE_SET(dev, acpi_dev->handle);
  143. if (!physical_node->node_id)
  144. strcpy(physical_node_name, PHYSICAL_NODE_STRING);
  145. else
  146. sprintf(physical_node_name,
  147. "physical_node%d", physical_node->node_id);
  148. retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  149. physical_node_name);
  150. retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  151. "firmware_node");
  152. if (acpi_dev->wakeup.flags.valid)
  153. device_set_wakeup_capable(dev, true);
  154. return 0;
  155. err:
  156. ACPI_HANDLE_SET(dev, NULL);
  157. put_device(dev);
  158. return retval;
  159. err_free:
  160. mutex_unlock(&acpi_dev->physical_node_lock);
  161. kfree(physical_node);
  162. goto err;
  163. }
  164. static int acpi_unbind_one(struct device *dev)
  165. {
  166. struct acpi_device_physical_node *entry;
  167. struct acpi_device *acpi_dev;
  168. acpi_status status;
  169. struct list_head *node, *next;
  170. if (!ACPI_HANDLE(dev))
  171. return 0;
  172. status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
  173. if (ACPI_FAILURE(status))
  174. goto err;
  175. mutex_lock(&acpi_dev->physical_node_lock);
  176. list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
  177. char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
  178. entry = list_entry(node, struct acpi_device_physical_node,
  179. node);
  180. if (entry->dev != dev)
  181. continue;
  182. list_del(node);
  183. clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap);
  184. acpi_dev->physical_node_count--;
  185. if (!entry->node_id)
  186. strcpy(physical_node_name, PHYSICAL_NODE_STRING);
  187. else
  188. sprintf(physical_node_name,
  189. "physical_node%d", entry->node_id);
  190. sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
  191. sysfs_remove_link(&dev->kobj, "firmware_node");
  192. ACPI_HANDLE_SET(dev, NULL);
  193. /* acpi_bind_one increase refcnt by one */
  194. put_device(dev);
  195. kfree(entry);
  196. }
  197. mutex_unlock(&acpi_dev->physical_node_lock);
  198. return 0;
  199. err:
  200. dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
  201. return -EINVAL;
  202. }
  203. static int acpi_platform_notify(struct device *dev)
  204. {
  205. struct acpi_bus_type *type = acpi_get_bus_type(dev);
  206. acpi_handle handle;
  207. int ret;
  208. ret = acpi_bind_one(dev, NULL);
  209. if (ret && type) {
  210. ret = type->find_device(dev, &handle);
  211. if (ret) {
  212. DBG("Unable to get handle for %s\n", dev_name(dev));
  213. goto out;
  214. }
  215. ret = acpi_bind_one(dev, handle);
  216. if (ret)
  217. goto out;
  218. }
  219. if (type && type->setup)
  220. type->setup(dev);
  221. out:
  222. #if ACPI_GLUE_DEBUG
  223. if (!ret) {
  224. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  225. acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
  226. DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
  227. kfree(buffer.pointer);
  228. } else
  229. DBG("Device %s -> No ACPI support\n", dev_name(dev));
  230. #endif
  231. return ret;
  232. }
  233. static int acpi_platform_notify_remove(struct device *dev)
  234. {
  235. struct acpi_bus_type *type;
  236. type = acpi_get_bus_type(dev);
  237. if (type && type->cleanup)
  238. type->cleanup(dev);
  239. acpi_unbind_one(dev);
  240. return 0;
  241. }
  242. int __init init_acpi_device_notify(void)
  243. {
  244. if (platform_notify || platform_notify_remove) {
  245. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  246. return 0;
  247. }
  248. platform_notify = acpi_platform_notify;
  249. platform_notify_remove = acpi_platform_notify_remove;
  250. return 0;
  251. }