glue.c 7.8 KB

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