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