glue.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/init.h>
  10. #include <linux/list.h>
  11. #include <linux/device.h>
  12. #include <linux/slab.h>
  13. #include <linux/rwsem.h>
  14. #include <linux/acpi.h>
  15. #include "internal.h"
  16. #define ACPI_GLUE_DEBUG 0
  17. #if ACPI_GLUE_DEBUG
  18. #define DBG(x...) printk(PREFIX x)
  19. #else
  20. #define DBG(x...) do { } while(0)
  21. #endif
  22. static LIST_HEAD(bus_type_list);
  23. static DECLARE_RWSEM(bus_type_sem);
  24. int register_acpi_bus_type(struct acpi_bus_type *type)
  25. {
  26. if (acpi_disabled)
  27. return -ENODEV;
  28. if (type && type->bus && type->find_device) {
  29. down_write(&bus_type_sem);
  30. list_add_tail(&type->list, &bus_type_list);
  31. up_write(&bus_type_sem);
  32. printk(KERN_INFO PREFIX "bus type %s registered\n",
  33. type->bus->name);
  34. return 0;
  35. }
  36. return -ENODEV;
  37. }
  38. int unregister_acpi_bus_type(struct acpi_bus_type *type)
  39. {
  40. if (acpi_disabled)
  41. return 0;
  42. if (type) {
  43. down_write(&bus_type_sem);
  44. list_del_init(&type->list);
  45. up_write(&bus_type_sem);
  46. printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
  47. type->bus->name);
  48. return 0;
  49. }
  50. return -ENODEV;
  51. }
  52. static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
  53. {
  54. struct acpi_bus_type *tmp, *ret = NULL;
  55. down_read(&bus_type_sem);
  56. list_for_each_entry(tmp, &bus_type_list, list) {
  57. if (tmp->bus == type) {
  58. ret = tmp;
  59. break;
  60. }
  61. }
  62. up_read(&bus_type_sem);
  63. return ret;
  64. }
  65. static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
  66. {
  67. struct acpi_bus_type *tmp;
  68. int ret = -ENODEV;
  69. down_read(&bus_type_sem);
  70. list_for_each_entry(tmp, &bus_type_list, list) {
  71. if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
  72. ret = 0;
  73. break;
  74. }
  75. }
  76. up_read(&bus_type_sem);
  77. return ret;
  78. }
  79. /* Get device's handler per its address under its parent */
  80. struct acpi_find_child {
  81. acpi_handle handle;
  82. u64 address;
  83. };
  84. static acpi_status
  85. do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
  86. {
  87. acpi_status status;
  88. struct acpi_device_info *info;
  89. struct acpi_find_child *find = context;
  90. status = acpi_get_object_info(handle, &info);
  91. if (ACPI_SUCCESS(status)) {
  92. if ((info->address == find->address)
  93. && (info->valid & ACPI_VALID_ADR))
  94. find->handle = handle;
  95. kfree(info);
  96. }
  97. return AE_OK;
  98. }
  99. acpi_handle acpi_get_child(acpi_handle parent, u64 address)
  100. {
  101. struct acpi_find_child find = { NULL, address };
  102. if (!parent)
  103. return NULL;
  104. acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
  105. 1, do_acpi_find_child, NULL, &find, NULL);
  106. return find.handle;
  107. }
  108. EXPORT_SYMBOL(acpi_get_child);
  109. /* Link ACPI devices with physical devices */
  110. static void acpi_glue_data_handler(acpi_handle handle,
  111. void *context)
  112. {
  113. /* we provide an empty handler */
  114. }
  115. /* Note: a success call will increase reference count by one */
  116. struct device *acpi_get_physical_device(acpi_handle handle)
  117. {
  118. acpi_status status;
  119. struct device *dev;
  120. status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev);
  121. if (ACPI_SUCCESS(status))
  122. return get_device(dev);
  123. return NULL;
  124. }
  125. EXPORT_SYMBOL(acpi_get_physical_device);
  126. static int acpi_bind_one(struct device *dev, acpi_handle handle)
  127. {
  128. struct acpi_device *acpi_dev;
  129. acpi_status status;
  130. if (dev->archdata.acpi_handle) {
  131. dev_warn(dev, "Drivers changed 'acpi_handle'\n");
  132. return -EINVAL;
  133. }
  134. get_device(dev);
  135. status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
  136. if (ACPI_FAILURE(status)) {
  137. put_device(dev);
  138. return -EINVAL;
  139. }
  140. dev->archdata.acpi_handle = handle;
  141. status = acpi_bus_get_device(handle, &acpi_dev);
  142. if (!ACPI_FAILURE(status)) {
  143. int ret;
  144. ret = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  145. "firmware_node");
  146. ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  147. "physical_node");
  148. if (acpi_dev->wakeup.flags.valid) {
  149. device_set_wakeup_capable(dev, true);
  150. device_set_wakeup_enable(dev,
  151. acpi_dev->wakeup.state.enabled);
  152. }
  153. }
  154. return 0;
  155. }
  156. static int acpi_unbind_one(struct device *dev)
  157. {
  158. if (!dev->archdata.acpi_handle)
  159. return 0;
  160. if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
  161. struct acpi_device *acpi_dev;
  162. /* acpi_get_physical_device increase refcnt by one */
  163. put_device(dev);
  164. if (!acpi_bus_get_device(dev->archdata.acpi_handle,
  165. &acpi_dev)) {
  166. sysfs_remove_link(&dev->kobj, "firmware_node");
  167. sysfs_remove_link(&acpi_dev->dev.kobj, "physical_node");
  168. }
  169. acpi_detach_data(dev->archdata.acpi_handle,
  170. acpi_glue_data_handler);
  171. dev->archdata.acpi_handle = NULL;
  172. /* acpi_bind_one increase refcnt by one */
  173. put_device(dev);
  174. } else {
  175. dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
  176. }
  177. return 0;
  178. }
  179. static int acpi_platform_notify(struct device *dev)
  180. {
  181. struct acpi_bus_type *type;
  182. acpi_handle handle;
  183. int ret = -EINVAL;
  184. if (!dev->bus || !dev->parent) {
  185. /* bridge devices genernally haven't bus or parent */
  186. ret = acpi_find_bridge_device(dev, &handle);
  187. goto end;
  188. }
  189. type = acpi_get_bus_type(dev->bus);
  190. if (!type) {
  191. DBG("No ACPI bus support for %s\n", dev_name(dev));
  192. ret = -EINVAL;
  193. goto end;
  194. }
  195. if ((ret = type->find_device(dev, &handle)) != 0)
  196. DBG("Can't get handler for %s\n", dev_name(dev));
  197. end:
  198. if (!ret)
  199. acpi_bind_one(dev, handle);
  200. #if ACPI_GLUE_DEBUG
  201. if (!ret) {
  202. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  203. acpi_get_name(dev->archdata.acpi_handle,
  204. ACPI_FULL_PATHNAME, &buffer);
  205. DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
  206. kfree(buffer.pointer);
  207. } else
  208. DBG("Device %s -> No ACPI support\n", dev_name(dev));
  209. #endif
  210. return ret;
  211. }
  212. static int acpi_platform_notify_remove(struct device *dev)
  213. {
  214. acpi_unbind_one(dev);
  215. return 0;
  216. }
  217. int __init init_acpi_device_notify(void)
  218. {
  219. if (platform_notify || platform_notify_remove) {
  220. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  221. return 0;
  222. }
  223. platform_notify = acpi_platform_notify;
  224. platform_notify_remove = acpi_platform_notify_remove;
  225. return 0;
  226. }