glue.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. /* ToDo: When a PCI bridge is found, return the PCI device behind the bridge
  126. * This should work in general, but did not on a Lenovo T61 for the
  127. * graphics card. But this must be fixed when the PCI device is
  128. * bound and the kernel device struct is attached to the acpi device
  129. * Note: A success call will increase reference count by one
  130. * Do call put_device(dev) on the returned device then
  131. */
  132. struct device *acpi_get_physical_pci_device(acpi_handle handle)
  133. {
  134. struct device *dev;
  135. long long device_id;
  136. acpi_status status;
  137. status =
  138. acpi_evaluate_integer(handle, "_ADR", NULL, &device_id);
  139. if (ACPI_FAILURE(status))
  140. return NULL;
  141. /* We need to attempt to determine whether the _ADR refers to a
  142. PCI device or not. There's no terribly good way to do this,
  143. so the best we can hope for is to assume that there'll never
  144. be a device in the host bridge */
  145. if (device_id >= 0x10000) {
  146. /* It looks like a PCI device. Does it exist? */
  147. dev = acpi_get_physical_device(handle);
  148. } else {
  149. /* It doesn't look like a PCI device. Does its parent
  150. exist? */
  151. acpi_handle phandle;
  152. if (acpi_get_parent(handle, &phandle))
  153. return NULL;
  154. dev = acpi_get_physical_device(phandle);
  155. }
  156. if (!dev)
  157. return NULL;
  158. return dev;
  159. }
  160. EXPORT_SYMBOL(acpi_get_physical_pci_device);
  161. static int acpi_bind_one(struct device *dev, acpi_handle handle)
  162. {
  163. struct acpi_device *acpi_dev;
  164. acpi_status status;
  165. if (dev->archdata.acpi_handle) {
  166. dev_warn(dev, "Drivers changed 'acpi_handle'\n");
  167. return -EINVAL;
  168. }
  169. get_device(dev);
  170. status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
  171. if (ACPI_FAILURE(status)) {
  172. put_device(dev);
  173. return -EINVAL;
  174. }
  175. dev->archdata.acpi_handle = handle;
  176. status = acpi_bus_get_device(handle, &acpi_dev);
  177. if (!ACPI_FAILURE(status)) {
  178. int ret;
  179. ret = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  180. "firmware_node");
  181. ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  182. "physical_node");
  183. if (acpi_dev->wakeup.flags.valid) {
  184. device_set_wakeup_capable(dev, true);
  185. device_set_wakeup_enable(dev,
  186. acpi_dev->wakeup.state.enabled);
  187. }
  188. }
  189. return 0;
  190. }
  191. static int acpi_unbind_one(struct device *dev)
  192. {
  193. if (!dev->archdata.acpi_handle)
  194. return 0;
  195. if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
  196. struct acpi_device *acpi_dev;
  197. /* acpi_get_physical_device increase refcnt by one */
  198. put_device(dev);
  199. if (!acpi_bus_get_device(dev->archdata.acpi_handle,
  200. &acpi_dev)) {
  201. sysfs_remove_link(&dev->kobj, "firmware_node");
  202. sysfs_remove_link(&acpi_dev->dev.kobj, "physical_node");
  203. }
  204. acpi_detach_data(dev->archdata.acpi_handle,
  205. acpi_glue_data_handler);
  206. dev->archdata.acpi_handle = NULL;
  207. /* acpi_bind_one increase refcnt by one */
  208. put_device(dev);
  209. } else {
  210. dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
  211. }
  212. return 0;
  213. }
  214. static int acpi_platform_notify(struct device *dev)
  215. {
  216. struct acpi_bus_type *type;
  217. acpi_handle handle;
  218. int ret = -EINVAL;
  219. if (!dev->bus || !dev->parent) {
  220. /* bridge devices genernally haven't bus or parent */
  221. ret = acpi_find_bridge_device(dev, &handle);
  222. goto end;
  223. }
  224. type = acpi_get_bus_type(dev->bus);
  225. if (!type) {
  226. DBG("No ACPI bus support for %s\n", dev->bus_id);
  227. ret = -EINVAL;
  228. goto end;
  229. }
  230. if ((ret = type->find_device(dev, &handle)) != 0)
  231. DBG("Can't get handler for %s\n", dev->bus_id);
  232. end:
  233. if (!ret)
  234. acpi_bind_one(dev, handle);
  235. #if ACPI_GLUE_DEBUG
  236. if (!ret) {
  237. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  238. acpi_get_name(dev->archdata.acpi_handle,
  239. ACPI_FULL_PATHNAME, &buffer);
  240. DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer);
  241. kfree(buffer.pointer);
  242. } else
  243. DBG("Device %s -> No ACPI support\n", dev->bus_id);
  244. #endif
  245. return ret;
  246. }
  247. static int acpi_platform_notify_remove(struct device *dev)
  248. {
  249. acpi_unbind_one(dev);
  250. return 0;
  251. }
  252. static int __init init_acpi_device_notify(void)
  253. {
  254. if (acpi_disabled)
  255. return 0;
  256. if (platform_notify || platform_notify_remove) {
  257. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  258. return 0;
  259. }
  260. platform_notify = acpi_platform_notify;
  261. platform_notify_remove = acpi_platform_notify_remove;
  262. return 0;
  263. }
  264. arch_initcall(init_acpi_device_notify);