glue.c 7.6 KB

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