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