glue.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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->archdata.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->archdata.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->archdata.acpi_handle)
  174. return 0;
  175. status = acpi_bus_get_device(dev->archdata.acpi_handle,
  176. &acpi_dev);
  177. if (ACPI_FAILURE(status))
  178. goto err;
  179. mutex_lock(&acpi_dev->physical_node_lock);
  180. list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
  181. char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
  182. entry = list_entry(node, struct acpi_device_physical_node,
  183. node);
  184. if (entry->dev != dev)
  185. continue;
  186. list_del(node);
  187. clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap);
  188. acpi_dev->physical_node_count--;
  189. if (!entry->node_id)
  190. strcpy(physical_node_name, PHYSICAL_NODE_STRING);
  191. else
  192. sprintf(physical_node_name,
  193. "physical_node%d", entry->node_id);
  194. sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
  195. sysfs_remove_link(&dev->kobj, "firmware_node");
  196. dev->archdata.acpi_handle = NULL;
  197. /* acpi_bind_one increase refcnt by one */
  198. put_device(dev);
  199. kfree(entry);
  200. }
  201. mutex_unlock(&acpi_dev->physical_node_lock);
  202. return 0;
  203. err:
  204. dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
  205. return -EINVAL;
  206. }
  207. static int acpi_platform_notify(struct device *dev)
  208. {
  209. struct acpi_bus_type *type;
  210. acpi_handle handle;
  211. int ret = -EINVAL;
  212. if (!dev->bus || !dev->parent) {
  213. /* bridge devices genernally haven't bus or parent */
  214. ret = acpi_find_bridge_device(dev, &handle);
  215. goto end;
  216. }
  217. type = acpi_get_bus_type(dev->bus);
  218. if (!type) {
  219. DBG("No ACPI bus support for %s\n", dev_name(dev));
  220. ret = -EINVAL;
  221. goto end;
  222. }
  223. if ((ret = type->find_device(dev, &handle)) != 0)
  224. DBG("Can't get handler for %s\n", dev_name(dev));
  225. end:
  226. if (!ret)
  227. acpi_bind_one(dev, handle);
  228. #if ACPI_GLUE_DEBUG
  229. if (!ret) {
  230. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  231. acpi_get_name(dev->archdata.acpi_handle,
  232. ACPI_FULL_PATHNAME, &buffer);
  233. DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
  234. kfree(buffer.pointer);
  235. } else
  236. DBG("Device %s -> No ACPI support\n", dev_name(dev));
  237. #endif
  238. return ret;
  239. }
  240. static int acpi_platform_notify_remove(struct device *dev)
  241. {
  242. acpi_unbind_one(dev);
  243. return 0;
  244. }
  245. int __init init_acpi_device_notify(void)
  246. {
  247. if (platform_notify || platform_notify_remove) {
  248. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  249. return 0;
  250. }
  251. platform_notify = acpi_platform_notify;
  252. platform_notify_remove = acpi_platform_notify_remove;
  253. return 0;
  254. }