glue.c 6.9 KB

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