glue.c 6.9 KB

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