glue.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. }
  151. return 0;
  152. }
  153. static int acpi_unbind_one(struct device *dev)
  154. {
  155. if (!dev->archdata.acpi_handle)
  156. return 0;
  157. if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
  158. struct acpi_device *acpi_dev;
  159. /* acpi_get_physical_device increase refcnt by one */
  160. put_device(dev);
  161. if (!acpi_bus_get_device(dev->archdata.acpi_handle,
  162. &acpi_dev)) {
  163. sysfs_remove_link(&dev->kobj, "firmware_node");
  164. sysfs_remove_link(&acpi_dev->dev.kobj, "physical_node");
  165. }
  166. acpi_detach_data(dev->archdata.acpi_handle,
  167. acpi_glue_data_handler);
  168. dev->archdata.acpi_handle = NULL;
  169. /* acpi_bind_one increase refcnt by one */
  170. put_device(dev);
  171. } else {
  172. dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
  173. }
  174. return 0;
  175. }
  176. static int acpi_platform_notify(struct device *dev)
  177. {
  178. struct acpi_bus_type *type;
  179. acpi_handle handle;
  180. int ret = -EINVAL;
  181. if (!dev->bus || !dev->parent) {
  182. /* bridge devices genernally haven't bus or parent */
  183. ret = acpi_find_bridge_device(dev, &handle);
  184. goto end;
  185. }
  186. type = acpi_get_bus_type(dev->bus);
  187. if (!type) {
  188. DBG("No ACPI bus support for %s\n", dev_name(dev));
  189. ret = -EINVAL;
  190. goto end;
  191. }
  192. if ((ret = type->find_device(dev, &handle)) != 0)
  193. DBG("Can't get handler for %s\n", dev_name(dev));
  194. end:
  195. if (!ret)
  196. acpi_bind_one(dev, handle);
  197. #if ACPI_GLUE_DEBUG
  198. if (!ret) {
  199. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  200. acpi_get_name(dev->archdata.acpi_handle,
  201. ACPI_FULL_PATHNAME, &buffer);
  202. DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
  203. kfree(buffer.pointer);
  204. } else
  205. DBG("Device %s -> No ACPI support\n", dev_name(dev));
  206. #endif
  207. return ret;
  208. }
  209. static int acpi_platform_notify_remove(struct device *dev)
  210. {
  211. acpi_unbind_one(dev);
  212. return 0;
  213. }
  214. int __init init_acpi_device_notify(void)
  215. {
  216. if (platform_notify || platform_notify_remove) {
  217. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  218. return 0;
  219. }
  220. platform_notify = acpi_platform_notify;
  221. platform_notify_remove = acpi_platform_notify_remove;
  222. return 0;
  223. }