glue.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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...)
  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. EXPORT_SYMBOL(register_acpi_bus_type);
  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. EXPORT_SYMBOL(unregister_acpi_bus_type);
  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. acpi_integer 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_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  90. struct acpi_find_child *find = context;
  91. status = acpi_get_object_info(handle, &buffer);
  92. if (ACPI_SUCCESS(status)) {
  93. info = buffer.pointer;
  94. if (info->address == find->address)
  95. find->handle = handle;
  96. kfree(buffer.pointer);
  97. }
  98. return AE_OK;
  99. }
  100. acpi_handle acpi_get_child(acpi_handle parent, acpi_integer 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, &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. u32 function, 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. acpi_status status;
  130. if (dev->archdata.acpi_handle) {
  131. printk(KERN_WARNING PREFIX
  132. "Drivers changed 'acpi_handle' for %s\n", dev->bus_id);
  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. return 0;
  143. }
  144. static int acpi_unbind_one(struct device *dev)
  145. {
  146. if (!dev->archdata.acpi_handle)
  147. return 0;
  148. if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
  149. /* acpi_get_physical_device increase refcnt by one */
  150. put_device(dev);
  151. acpi_detach_data(dev->archdata.acpi_handle,
  152. acpi_glue_data_handler);
  153. dev->archdata.acpi_handle = NULL;
  154. /* acpi_bind_one increase refcnt by one */
  155. put_device(dev);
  156. } else {
  157. printk(KERN_ERR PREFIX
  158. "Oops, 'acpi_handle' corrupt for %s\n", dev->bus_id);
  159. }
  160. return 0;
  161. }
  162. static int acpi_platform_notify(struct device *dev)
  163. {
  164. struct acpi_bus_type *type;
  165. acpi_handle handle;
  166. int ret = -EINVAL;
  167. if (!dev->bus || !dev->parent) {
  168. /* bridge devices genernally haven't bus or parent */
  169. ret = acpi_find_bridge_device(dev, &handle);
  170. goto end;
  171. }
  172. type = acpi_get_bus_type(dev->bus);
  173. if (!type) {
  174. DBG("No ACPI bus support for %s\n", dev->bus_id);
  175. ret = -EINVAL;
  176. goto end;
  177. }
  178. if ((ret = type->find_device(dev, &handle)) != 0)
  179. DBG("Can't get handler for %s\n", dev->bus_id);
  180. end:
  181. if (!ret)
  182. acpi_bind_one(dev, handle);
  183. #if ACPI_GLUE_DEBUG
  184. if (!ret) {
  185. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  186. acpi_get_name(dev->archdata.acpi_handle,
  187. ACPI_FULL_PATHNAME, &buffer);
  188. DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer);
  189. kfree(buffer.pointer);
  190. } else
  191. DBG("Device %s -> No ACPI support\n", dev->bus_id);
  192. #endif
  193. return ret;
  194. }
  195. static int acpi_platform_notify_remove(struct device *dev)
  196. {
  197. acpi_unbind_one(dev);
  198. return 0;
  199. }
  200. static int __init init_acpi_device_notify(void)
  201. {
  202. if (acpi_disabled)
  203. return 0;
  204. if (platform_notify || platform_notify_remove) {
  205. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  206. return 0;
  207. }
  208. platform_notify = acpi_platform_notify;
  209. platform_notify_remove = acpi_platform_notify_remove;
  210. return 0;
  211. }
  212. arch_initcall(init_acpi_device_notify);