glue.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 = NULL;
  179. struct acpi_device_physical_node *physical_node, *pn;
  180. char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
  181. struct list_head *physnode_list;
  182. unsigned int node_id;
  183. int retval = -EINVAL;
  184. if (ACPI_COMPANION(dev)) {
  185. if (handle) {
  186. dev_warn(dev, "ACPI companion already set\n");
  187. return -EINVAL;
  188. } else {
  189. acpi_dev = ACPI_COMPANION(dev);
  190. }
  191. } else {
  192. acpi_bus_get_device(handle, &acpi_dev);
  193. }
  194. if (!acpi_dev)
  195. return -EINVAL;
  196. get_device(dev);
  197. physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
  198. if (!physical_node) {
  199. retval = -ENOMEM;
  200. goto err;
  201. }
  202. mutex_lock(&acpi_dev->physical_node_lock);
  203. /*
  204. * Keep the list sorted by node_id so that the IDs of removed nodes can
  205. * be recycled easily.
  206. */
  207. physnode_list = &acpi_dev->physical_node_list;
  208. node_id = 0;
  209. list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
  210. /* Sanity check. */
  211. if (pn->dev == dev) {
  212. mutex_unlock(&acpi_dev->physical_node_lock);
  213. dev_warn(dev, "Already associated with ACPI node\n");
  214. kfree(physical_node);
  215. if (ACPI_COMPANION(dev) != acpi_dev)
  216. goto err;
  217. put_device(dev);
  218. return 0;
  219. }
  220. if (pn->node_id == node_id) {
  221. physnode_list = &pn->node;
  222. node_id++;
  223. }
  224. }
  225. physical_node->node_id = node_id;
  226. physical_node->dev = dev;
  227. list_add(&physical_node->node, physnode_list);
  228. acpi_dev->physical_node_count++;
  229. if (!ACPI_COMPANION(dev))
  230. ACPI_COMPANION_SET(dev, acpi_dev);
  231. acpi_physnode_link_name(physical_node_name, node_id);
  232. retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  233. physical_node_name);
  234. if (retval)
  235. dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
  236. physical_node_name, retval);
  237. retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  238. "firmware_node");
  239. if (retval)
  240. dev_err(dev, "Failed to create link firmware_node (%d)\n",
  241. retval);
  242. mutex_unlock(&acpi_dev->physical_node_lock);
  243. if (acpi_dev->wakeup.flags.valid)
  244. device_set_wakeup_capable(dev, true);
  245. return 0;
  246. err:
  247. ACPI_COMPANION_SET(dev, NULL);
  248. put_device(dev);
  249. return retval;
  250. }
  251. EXPORT_SYMBOL_GPL(acpi_bind_one);
  252. int acpi_unbind_one(struct device *dev)
  253. {
  254. struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
  255. struct acpi_device_physical_node *entry;
  256. if (!acpi_dev)
  257. return 0;
  258. mutex_lock(&acpi_dev->physical_node_lock);
  259. list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
  260. if (entry->dev == dev) {
  261. char physnode_name[PHYSICAL_NODE_NAME_SIZE];
  262. list_del(&entry->node);
  263. acpi_dev->physical_node_count--;
  264. acpi_physnode_link_name(physnode_name, entry->node_id);
  265. sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
  266. sysfs_remove_link(&dev->kobj, "firmware_node");
  267. ACPI_COMPANION_SET(dev, NULL);
  268. /* acpi_bind_one() increase refcnt by one. */
  269. put_device(dev);
  270. kfree(entry);
  271. break;
  272. }
  273. mutex_unlock(&acpi_dev->physical_node_lock);
  274. return 0;
  275. }
  276. EXPORT_SYMBOL_GPL(acpi_unbind_one);
  277. void acpi_preset_companion(struct device *dev, acpi_handle parent, u64 addr)
  278. {
  279. struct acpi_device *adev;
  280. if (!acpi_bus_get_device(acpi_get_child(parent, addr), &adev))
  281. ACPI_COMPANION_SET(dev, adev);
  282. }
  283. EXPORT_SYMBOL_GPL(acpi_preset_companion);
  284. static int acpi_platform_notify(struct device *dev)
  285. {
  286. struct acpi_bus_type *type = acpi_get_bus_type(dev);
  287. acpi_handle handle;
  288. int ret;
  289. ret = acpi_bind_one(dev, NULL);
  290. if (ret && type) {
  291. ret = type->find_device(dev, &handle);
  292. if (ret) {
  293. DBG("Unable to get handle for %s\n", dev_name(dev));
  294. goto out;
  295. }
  296. ret = acpi_bind_one(dev, handle);
  297. if (ret)
  298. goto out;
  299. }
  300. if (type && type->setup)
  301. type->setup(dev);
  302. out:
  303. #if ACPI_GLUE_DEBUG
  304. if (!ret) {
  305. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  306. acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
  307. DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
  308. kfree(buffer.pointer);
  309. } else
  310. DBG("Device %s -> No ACPI support\n", dev_name(dev));
  311. #endif
  312. return ret;
  313. }
  314. static int acpi_platform_notify_remove(struct device *dev)
  315. {
  316. struct acpi_bus_type *type;
  317. type = acpi_get_bus_type(dev);
  318. if (type && type->cleanup)
  319. type->cleanup(dev);
  320. acpi_unbind_one(dev);
  321. return 0;
  322. }
  323. int __init init_acpi_device_notify(void)
  324. {
  325. if (platform_notify || platform_notify_remove) {
  326. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  327. return 0;
  328. }
  329. platform_notify = acpi_platform_notify;
  330. platform_notify_remove = acpi_platform_notify_remove;
  331. return 0;
  332. }