glue.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #define PHYSICAL_NODE_STRING "physical_node"
  26. int register_acpi_bus_type(struct acpi_bus_type *type)
  27. {
  28. if (acpi_disabled)
  29. return -ENODEV;
  30. if (type && type->bus && type->find_device) {
  31. down_write(&bus_type_sem);
  32. list_add_tail(&type->list, &bus_type_list);
  33. up_write(&bus_type_sem);
  34. printk(KERN_INFO PREFIX "bus type %s registered\n",
  35. type->bus->name);
  36. return 0;
  37. }
  38. return -ENODEV;
  39. }
  40. EXPORT_SYMBOL_GPL(register_acpi_bus_type);
  41. int unregister_acpi_bus_type(struct acpi_bus_type *type)
  42. {
  43. if (acpi_disabled)
  44. return 0;
  45. if (type) {
  46. down_write(&bus_type_sem);
  47. list_del_init(&type->list);
  48. up_write(&bus_type_sem);
  49. printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
  50. type->bus->name);
  51. return 0;
  52. }
  53. return -ENODEV;
  54. }
  55. EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
  56. static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
  57. {
  58. struct acpi_bus_type *tmp, *ret = NULL;
  59. down_read(&bus_type_sem);
  60. list_for_each_entry(tmp, &bus_type_list, list) {
  61. if (tmp->bus == type) {
  62. ret = tmp;
  63. break;
  64. }
  65. }
  66. up_read(&bus_type_sem);
  67. return ret;
  68. }
  69. static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
  70. {
  71. struct acpi_bus_type *tmp;
  72. int ret = -ENODEV;
  73. down_read(&bus_type_sem);
  74. list_for_each_entry(tmp, &bus_type_list, list) {
  75. if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
  76. ret = 0;
  77. break;
  78. }
  79. }
  80. up_read(&bus_type_sem);
  81. return ret;
  82. }
  83. /* Get device's handler per its address under its parent */
  84. struct acpi_find_child {
  85. acpi_handle handle;
  86. u64 address;
  87. };
  88. static acpi_status
  89. do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
  90. {
  91. acpi_status status;
  92. struct acpi_device_info *info;
  93. struct acpi_find_child *find = context;
  94. status = acpi_get_object_info(handle, &info);
  95. if (ACPI_SUCCESS(status)) {
  96. if ((info->address == find->address)
  97. && (info->valid & ACPI_VALID_ADR))
  98. find->handle = handle;
  99. kfree(info);
  100. }
  101. return AE_OK;
  102. }
  103. acpi_handle acpi_get_child(acpi_handle parent, u64 address)
  104. {
  105. struct acpi_find_child find = { NULL, address };
  106. if (!parent)
  107. return NULL;
  108. acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
  109. 1, do_acpi_find_child, NULL, &find, NULL);
  110. return find.handle;
  111. }
  112. EXPORT_SYMBOL(acpi_get_child);
  113. static int acpi_bind_one(struct device *dev, acpi_handle handle)
  114. {
  115. struct acpi_device *acpi_dev;
  116. acpi_status status;
  117. struct acpi_device_physical_node *physical_node, *pn;
  118. char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
  119. int retval = -EINVAL;
  120. if (dev->acpi_handle) {
  121. if (handle) {
  122. dev_warn(dev, "ACPI handle is already set\n");
  123. return -EINVAL;
  124. } else {
  125. handle = dev->acpi_handle;
  126. }
  127. }
  128. if (!handle)
  129. return -EINVAL;
  130. get_device(dev);
  131. status = acpi_bus_get_device(handle, &acpi_dev);
  132. if (ACPI_FAILURE(status))
  133. goto err;
  134. physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
  135. if (!physical_node) {
  136. retval = -ENOMEM;
  137. goto err;
  138. }
  139. mutex_lock(&acpi_dev->physical_node_lock);
  140. /* Sanity check. */
  141. list_for_each_entry(pn, &acpi_dev->physical_node_list, node)
  142. if (pn->dev == dev) {
  143. dev_warn(dev, "Already associated with ACPI node\n");
  144. goto err_free;
  145. }
  146. /* allocate physical node id according to physical_node_id_bitmap */
  147. physical_node->node_id =
  148. find_first_zero_bit(acpi_dev->physical_node_id_bitmap,
  149. ACPI_MAX_PHYSICAL_NODE);
  150. if (physical_node->node_id >= ACPI_MAX_PHYSICAL_NODE) {
  151. retval = -ENOSPC;
  152. goto err_free;
  153. }
  154. set_bit(physical_node->node_id, acpi_dev->physical_node_id_bitmap);
  155. physical_node->dev = dev;
  156. list_add_tail(&physical_node->node, &acpi_dev->physical_node_list);
  157. acpi_dev->physical_node_count++;
  158. mutex_unlock(&acpi_dev->physical_node_lock);
  159. if (!dev->acpi_handle)
  160. dev->acpi_handle = handle;
  161. if (!physical_node->node_id)
  162. strcpy(physical_node_name, PHYSICAL_NODE_STRING);
  163. else
  164. sprintf(physical_node_name,
  165. "physical_node%d", physical_node->node_id);
  166. retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  167. physical_node_name);
  168. retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  169. "firmware_node");
  170. if (acpi_dev->wakeup.flags.valid)
  171. device_set_wakeup_capable(dev, true);
  172. return 0;
  173. err:
  174. dev->acpi_handle = NULL;
  175. put_device(dev);
  176. return retval;
  177. err_free:
  178. mutex_unlock(&acpi_dev->physical_node_lock);
  179. kfree(physical_node);
  180. goto err;
  181. }
  182. static int acpi_unbind_one(struct device *dev)
  183. {
  184. struct acpi_device_physical_node *entry;
  185. struct acpi_device *acpi_dev;
  186. acpi_status status;
  187. struct list_head *node, *next;
  188. if (!dev->acpi_handle)
  189. return 0;
  190. status = acpi_bus_get_device(dev->acpi_handle, &acpi_dev);
  191. if (ACPI_FAILURE(status))
  192. goto err;
  193. mutex_lock(&acpi_dev->physical_node_lock);
  194. list_for_each_safe(node, next, &acpi_dev->physical_node_list) {
  195. char physical_node_name[sizeof(PHYSICAL_NODE_STRING) + 2];
  196. entry = list_entry(node, struct acpi_device_physical_node,
  197. node);
  198. if (entry->dev != dev)
  199. continue;
  200. list_del(node);
  201. clear_bit(entry->node_id, acpi_dev->physical_node_id_bitmap);
  202. acpi_dev->physical_node_count--;
  203. if (!entry->node_id)
  204. strcpy(physical_node_name, PHYSICAL_NODE_STRING);
  205. else
  206. sprintf(physical_node_name,
  207. "physical_node%d", entry->node_id);
  208. sysfs_remove_link(&acpi_dev->dev.kobj, physical_node_name);
  209. sysfs_remove_link(&dev->kobj, "firmware_node");
  210. dev->acpi_handle = NULL;
  211. /* acpi_bind_one increase refcnt by one */
  212. put_device(dev);
  213. kfree(entry);
  214. }
  215. mutex_unlock(&acpi_dev->physical_node_lock);
  216. return 0;
  217. err:
  218. dev_err(dev, "Oops, 'acpi_handle' corrupt\n");
  219. return -EINVAL;
  220. }
  221. static int acpi_platform_notify(struct device *dev)
  222. {
  223. struct acpi_bus_type *type;
  224. acpi_handle handle;
  225. int ret = -EINVAL;
  226. ret = acpi_bind_one(dev, NULL);
  227. if (!ret)
  228. goto out;
  229. if (!dev->bus || !dev->parent) {
  230. /* bridge devices genernally haven't bus or parent */
  231. ret = acpi_find_bridge_device(dev, &handle);
  232. goto end;
  233. }
  234. type = acpi_get_bus_type(dev->bus);
  235. if (!type) {
  236. DBG("No ACPI bus support for %s\n", dev_name(dev));
  237. ret = -EINVAL;
  238. goto end;
  239. }
  240. if ((ret = type->find_device(dev, &handle)) != 0)
  241. DBG("Can't get handler for %s\n", dev_name(dev));
  242. end:
  243. if (!ret)
  244. acpi_bind_one(dev, handle);
  245. out:
  246. #if ACPI_GLUE_DEBUG
  247. if (!ret) {
  248. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  249. acpi_get_name(dev->acpi_handle, ACPI_FULL_PATHNAME, &buffer);
  250. DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
  251. kfree(buffer.pointer);
  252. } else
  253. DBG("Device %s -> No ACPI support\n", dev_name(dev));
  254. #endif
  255. return ret;
  256. }
  257. static int acpi_platform_notify_remove(struct device *dev)
  258. {
  259. acpi_unbind_one(dev);
  260. return 0;
  261. }
  262. int __init init_acpi_device_notify(void)
  263. {
  264. if (platform_notify || platform_notify_remove) {
  265. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  266. return 0;
  267. }
  268. platform_notify = acpi_platform_notify;
  269. platform_notify_remove = acpi_platform_notify_remove;
  270. return 0;
  271. }