glue.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #define PHYSICAL_NODE_NAME_SIZE (sizeof(PHYSICAL_NODE_STRING) + 10)
  32. int register_acpi_bus_type(struct acpi_bus_type *type)
  33. {
  34. if (acpi_disabled)
  35. return -ENODEV;
  36. if (type && type->match && type->find_device) {
  37. down_write(&bus_type_sem);
  38. list_add_tail(&type->list, &bus_type_list);
  39. up_write(&bus_type_sem);
  40. printk(KERN_INFO PREFIX "bus type %s registered\n", type->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 "bus type %s unregistered\n",
  55. type->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 device *dev)
  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->match(dev)) {
  67. ret = tmp;
  68. break;
  69. }
  70. }
  71. up_read(&bus_type_sem);
  72. return ret;
  73. }
  74. #define FIND_CHILD_MIN_SCORE 1
  75. #define FIND_CHILD_MAX_SCORE 2
  76. static acpi_status acpi_dev_present(acpi_handle handle, u32 lvl_not_used,
  77. void *not_used, void **ret_p)
  78. {
  79. struct acpi_device *adev = NULL;
  80. acpi_bus_get_device(handle, &adev);
  81. if (adev) {
  82. *ret_p = handle;
  83. return AE_CTRL_TERMINATE;
  84. }
  85. return AE_OK;
  86. }
  87. static int do_find_child_checks(acpi_handle handle, bool is_bridge)
  88. {
  89. bool sta_present = true;
  90. unsigned long long sta;
  91. acpi_status status;
  92. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  93. if (status == AE_NOT_FOUND)
  94. sta_present = false;
  95. else if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
  96. return -ENODEV;
  97. if (is_bridge) {
  98. void *test = NULL;
  99. /* Check if this object has at least one child device. */
  100. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  101. acpi_dev_present, NULL, NULL, &test);
  102. if (!test)
  103. return -ENODEV;
  104. }
  105. return sta_present ? FIND_CHILD_MAX_SCORE : FIND_CHILD_MIN_SCORE;
  106. }
  107. struct find_child_context {
  108. u64 addr;
  109. bool is_bridge;
  110. acpi_handle ret;
  111. int ret_score;
  112. };
  113. static acpi_status do_find_child(acpi_handle handle, u32 lvl_not_used,
  114. void *data, void **not_used)
  115. {
  116. struct find_child_context *context = data;
  117. unsigned long long addr;
  118. acpi_status status;
  119. int score;
  120. status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &addr);
  121. if (ACPI_FAILURE(status) || addr != context->addr)
  122. return AE_OK;
  123. if (!context->ret) {
  124. /* This is the first matching object. Save its handle. */
  125. context->ret = handle;
  126. return AE_OK;
  127. }
  128. /*
  129. * There is more than one matching object with the same _ADR value.
  130. * That really is unexpected, so we are kind of beyond the scope of the
  131. * spec here. We have to choose which one to return, though.
  132. *
  133. * First, check if the previously found object is good enough and return
  134. * its handle if so. Second, check the same for the object that we've
  135. * just found.
  136. */
  137. if (!context->ret_score) {
  138. score = do_find_child_checks(context->ret, context->is_bridge);
  139. if (score == FIND_CHILD_MAX_SCORE)
  140. return AE_CTRL_TERMINATE;
  141. else
  142. context->ret_score = score;
  143. }
  144. score = do_find_child_checks(handle, context->is_bridge);
  145. if (score == FIND_CHILD_MAX_SCORE) {
  146. context->ret = handle;
  147. return AE_CTRL_TERMINATE;
  148. } else if (score > context->ret_score) {
  149. context->ret = handle;
  150. context->ret_score = score;
  151. }
  152. return AE_OK;
  153. }
  154. acpi_handle acpi_find_child(acpi_handle parent, u64 addr, bool is_bridge)
  155. {
  156. if (parent) {
  157. struct find_child_context context = {
  158. .addr = addr,
  159. .is_bridge = is_bridge,
  160. };
  161. acpi_walk_namespace(ACPI_TYPE_DEVICE, parent, 1, do_find_child,
  162. NULL, &context, NULL);
  163. return context.ret;
  164. }
  165. return NULL;
  166. }
  167. EXPORT_SYMBOL_GPL(acpi_find_child);
  168. static void acpi_physnode_link_name(char *buf, unsigned int node_id)
  169. {
  170. if (node_id > 0)
  171. snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
  172. PHYSICAL_NODE_STRING "%u", node_id);
  173. else
  174. strcpy(buf, PHYSICAL_NODE_STRING);
  175. }
  176. int acpi_bind_one(struct device *dev, acpi_handle handle)
  177. {
  178. struct acpi_device *acpi_dev;
  179. acpi_status status;
  180. struct acpi_device_physical_node *physical_node, *pn;
  181. char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
  182. struct list_head *physnode_list;
  183. unsigned int node_id;
  184. int retval = -EINVAL;
  185. if (ACPI_HANDLE(dev)) {
  186. if (handle) {
  187. dev_warn(dev, "ACPI handle is already set\n");
  188. return -EINVAL;
  189. } else {
  190. handle = ACPI_HANDLE(dev);
  191. }
  192. }
  193. if (!handle)
  194. return -EINVAL;
  195. get_device(dev);
  196. status = acpi_bus_get_device(handle, &acpi_dev);
  197. if (ACPI_FAILURE(status))
  198. goto err;
  199. physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
  200. if (!physical_node) {
  201. retval = -ENOMEM;
  202. goto err;
  203. }
  204. mutex_lock(&acpi_dev->physical_node_lock);
  205. /*
  206. * Keep the list sorted by node_id so that the IDs of removed nodes can
  207. * be recycled easily.
  208. */
  209. physnode_list = &acpi_dev->physical_node_list;
  210. node_id = 0;
  211. list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
  212. /* Sanity check. */
  213. if (pn->dev == dev) {
  214. mutex_unlock(&acpi_dev->physical_node_lock);
  215. dev_warn(dev, "Already associated with ACPI node\n");
  216. kfree(physical_node);
  217. if (ACPI_HANDLE(dev) != handle)
  218. goto err;
  219. put_device(dev);
  220. return 0;
  221. }
  222. if (pn->node_id == node_id) {
  223. physnode_list = &pn->node;
  224. node_id++;
  225. }
  226. }
  227. physical_node->node_id = node_id;
  228. physical_node->dev = dev;
  229. list_add(&physical_node->node, physnode_list);
  230. acpi_dev->physical_node_count++;
  231. if (!ACPI_HANDLE(dev))
  232. ACPI_HANDLE_SET(dev, acpi_dev->handle);
  233. acpi_physnode_link_name(physical_node_name, node_id);
  234. retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  235. physical_node_name);
  236. if (retval)
  237. dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
  238. physical_node_name, retval);
  239. retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  240. "firmware_node");
  241. if (retval)
  242. dev_err(dev, "Failed to create link firmware_node (%d)\n",
  243. retval);
  244. mutex_unlock(&acpi_dev->physical_node_lock);
  245. if (acpi_dev->wakeup.flags.valid)
  246. device_set_wakeup_capable(dev, true);
  247. return 0;
  248. err:
  249. ACPI_HANDLE_SET(dev, NULL);
  250. put_device(dev);
  251. return retval;
  252. }
  253. EXPORT_SYMBOL_GPL(acpi_bind_one);
  254. int acpi_unbind_one(struct device *dev)
  255. {
  256. struct acpi_device_physical_node *entry;
  257. struct acpi_device *acpi_dev;
  258. acpi_status status;
  259. if (!ACPI_HANDLE(dev))
  260. return 0;
  261. status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
  262. if (ACPI_FAILURE(status)) {
  263. dev_err(dev, "Oops, ACPI handle corrupt in %s()\n", __func__);
  264. return -EINVAL;
  265. }
  266. mutex_lock(&acpi_dev->physical_node_lock);
  267. list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
  268. if (entry->dev == dev) {
  269. char physnode_name[PHYSICAL_NODE_NAME_SIZE];
  270. list_del(&entry->node);
  271. acpi_dev->physical_node_count--;
  272. acpi_physnode_link_name(physnode_name, entry->node_id);
  273. sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
  274. sysfs_remove_link(&dev->kobj, "firmware_node");
  275. ACPI_HANDLE_SET(dev, NULL);
  276. /* acpi_bind_one() increase refcnt by one. */
  277. put_device(dev);
  278. kfree(entry);
  279. break;
  280. }
  281. mutex_unlock(&acpi_dev->physical_node_lock);
  282. return 0;
  283. }
  284. EXPORT_SYMBOL_GPL(acpi_unbind_one);
  285. static int acpi_platform_notify(struct device *dev)
  286. {
  287. struct acpi_bus_type *type = acpi_get_bus_type(dev);
  288. acpi_handle handle;
  289. int ret;
  290. ret = acpi_bind_one(dev, NULL);
  291. if (ret && type) {
  292. ret = type->find_device(dev, &handle);
  293. if (ret) {
  294. DBG("Unable to get handle for %s\n", dev_name(dev));
  295. goto out;
  296. }
  297. ret = acpi_bind_one(dev, handle);
  298. if (ret)
  299. goto out;
  300. }
  301. if (type && type->setup)
  302. type->setup(dev);
  303. out:
  304. #if ACPI_GLUE_DEBUG
  305. if (!ret) {
  306. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  307. acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
  308. DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
  309. kfree(buffer.pointer);
  310. } else
  311. DBG("Device %s -> No ACPI support\n", dev_name(dev));
  312. #endif
  313. return ret;
  314. }
  315. static int acpi_platform_notify_remove(struct device *dev)
  316. {
  317. struct acpi_bus_type *type;
  318. type = acpi_get_bus_type(dev);
  319. if (type && type->cleanup)
  320. type->cleanup(dev);
  321. acpi_unbind_one(dev);
  322. return 0;
  323. }
  324. int __init init_acpi_device_notify(void)
  325. {
  326. if (platform_notify || platform_notify_remove) {
  327. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  328. return 0;
  329. }
  330. platform_notify = acpi_platform_notify;
  331. platform_notify_remove = acpi_platform_notify_remove;
  332. return 0;
  333. }