glue.c 7.6 KB

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