glue.c 5.9 KB

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