glue.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. int register_acpi_bus_type(struct acpi_bus_type *type)
  26. {
  27. if (acpi_disabled)
  28. return -ENODEV;
  29. if (type && type->bus && type->find_device) {
  30. down_write(&bus_type_sem);
  31. list_add_tail(&type->list, &bus_type_list);
  32. up_write(&bus_type_sem);
  33. printk(KERN_INFO PREFIX "bus type %s registered\n",
  34. type->bus->name);
  35. return 0;
  36. }
  37. return -ENODEV;
  38. }
  39. int unregister_acpi_bus_type(struct acpi_bus_type *type)
  40. {
  41. if (acpi_disabled)
  42. return 0;
  43. if (type) {
  44. down_write(&bus_type_sem);
  45. list_del_init(&type->list);
  46. up_write(&bus_type_sem);
  47. printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
  48. type->bus->name);
  49. return 0;
  50. }
  51. return -ENODEV;
  52. }
  53. static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
  54. {
  55. struct acpi_bus_type *tmp, *ret = NULL;
  56. down_read(&bus_type_sem);
  57. list_for_each_entry(tmp, &bus_type_list, list) {
  58. if (tmp->bus == type) {
  59. ret = tmp;
  60. break;
  61. }
  62. }
  63. up_read(&bus_type_sem);
  64. return ret;
  65. }
  66. static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
  67. {
  68. struct acpi_bus_type *tmp;
  69. int ret = -ENODEV;
  70. down_read(&bus_type_sem);
  71. list_for_each_entry(tmp, &bus_type_list, list) {
  72. if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
  73. ret = 0;
  74. break;
  75. }
  76. }
  77. up_read(&bus_type_sem);
  78. return ret;
  79. }
  80. /* Get device's handler per its address under its parent */
  81. struct acpi_find_child {
  82. acpi_handle handle;
  83. u64 address;
  84. };
  85. static acpi_status
  86. do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
  87. {
  88. acpi_status status;
  89. struct acpi_device_info *info;
  90. struct acpi_find_child *find = context;
  91. status = acpi_get_object_info(handle, &info);
  92. if (ACPI_SUCCESS(status)) {
  93. if ((info->address == find->address)
  94. && (info->valid & ACPI_VALID_ADR))
  95. find->handle = handle;
  96. kfree(info);
  97. }
  98. return AE_OK;
  99. }
  100. acpi_handle acpi_get_child(acpi_handle parent, u64 address)
  101. {
  102. struct acpi_find_child find = { NULL, address };
  103. if (!parent)
  104. return NULL;
  105. acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
  106. 1, do_acpi_find_child, NULL, &find, NULL);
  107. return find.handle;
  108. }
  109. EXPORT_SYMBOL(acpi_get_child);
  110. /* Link ACPI devices with physical devices */
  111. static void acpi_glue_data_handler(acpi_handle handle,
  112. void *context)
  113. {
  114. /* we provide an empty handler */
  115. }
  116. /* Note: a success call will increase reference count by one */
  117. struct device *acpi_get_physical_device(acpi_handle handle)
  118. {
  119. acpi_status status;
  120. struct device *dev;
  121. status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev);
  122. if (ACPI_SUCCESS(status))
  123. return get_device(dev);
  124. return NULL;
  125. }
  126. EXPORT_SYMBOL(acpi_get_physical_device);
  127. static int acpi_bind_one(struct device *dev, acpi_handle handle)
  128. {
  129. struct acpi_device *acpi_dev;
  130. acpi_status status;
  131. if (dev->archdata.acpi_handle) {
  132. dev_warn(dev, "Drivers changed 'acpi_handle'\n");
  133. return -EINVAL;
  134. }
  135. get_device(dev);
  136. status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
  137. if (ACPI_FAILURE(status)) {
  138. put_device(dev);
  139. return -EINVAL;
  140. }
  141. dev->archdata.acpi_handle = handle;
  142. status = acpi_bus_get_device(handle, &acpi_dev);
  143. if (!ACPI_FAILURE(status)) {
  144. int ret;
  145. ret = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  146. "firmware_node");
  147. ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  148. "physical_node");
  149. if (acpi_dev->wakeup.flags.valid)
  150. device_set_wakeup_capable(dev, true);
  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. }