glue.c 7.5 KB

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