glue.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 <linux/dma-mapping.h>
  17. #include "internal.h"
  18. #define ACPI_GLUE_DEBUG 0
  19. #if ACPI_GLUE_DEBUG
  20. #define DBG(fmt, ...) \
  21. printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__)
  22. #else
  23. #define DBG(fmt, ...) \
  24. do { \
  25. if (0) \
  26. printk(KERN_DEBUG PREFIX fmt, ##__VA_ARGS__); \
  27. } while (0)
  28. #endif
  29. static LIST_HEAD(bus_type_list);
  30. static DECLARE_RWSEM(bus_type_sem);
  31. #define PHYSICAL_NODE_STRING "physical_node"
  32. #define PHYSICAL_NODE_NAME_SIZE (sizeof(PHYSICAL_NODE_STRING) + 10)
  33. int register_acpi_bus_type(struct acpi_bus_type *type)
  34. {
  35. if (acpi_disabled)
  36. return -ENODEV;
  37. if (type && type->match && type->find_companion) {
  38. down_write(&bus_type_sem);
  39. list_add_tail(&type->list, &bus_type_list);
  40. up_write(&bus_type_sem);
  41. printk(KERN_INFO PREFIX "bus type %s registered\n", type->name);
  42. return 0;
  43. }
  44. return -ENODEV;
  45. }
  46. EXPORT_SYMBOL_GPL(register_acpi_bus_type);
  47. int unregister_acpi_bus_type(struct acpi_bus_type *type)
  48. {
  49. if (acpi_disabled)
  50. return 0;
  51. if (type) {
  52. down_write(&bus_type_sem);
  53. list_del_init(&type->list);
  54. up_write(&bus_type_sem);
  55. printk(KERN_INFO PREFIX "bus type %s unregistered\n",
  56. type->name);
  57. return 0;
  58. }
  59. return -ENODEV;
  60. }
  61. EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
  62. static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
  63. {
  64. struct acpi_bus_type *tmp, *ret = NULL;
  65. down_read(&bus_type_sem);
  66. list_for_each_entry(tmp, &bus_type_list, list) {
  67. if (tmp->match(dev)) {
  68. ret = tmp;
  69. break;
  70. }
  71. }
  72. up_read(&bus_type_sem);
  73. return ret;
  74. }
  75. #define FIND_CHILD_MIN_SCORE 1
  76. #define FIND_CHILD_MAX_SCORE 2
  77. static int find_child_checks(struct acpi_device *adev, bool check_children)
  78. {
  79. bool sta_present = true;
  80. unsigned long long sta;
  81. acpi_status status;
  82. status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
  83. if (status == AE_NOT_FOUND)
  84. sta_present = false;
  85. else if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
  86. return -ENODEV;
  87. if (check_children && list_empty(&adev->children))
  88. return -ENODEV;
  89. /*
  90. * If the device has a _HID (or _CID) returning a valid ACPI/PNP
  91. * device ID, it is better to make it look less attractive here, so that
  92. * the other device with the same _ADR value (that may not have a valid
  93. * device ID) can be matched going forward. [This means a second spec
  94. * violation in a row, so whatever we do here is best effort anyway.]
  95. */
  96. return sta_present && list_empty(&adev->pnp.ids) ?
  97. FIND_CHILD_MAX_SCORE : FIND_CHILD_MIN_SCORE;
  98. }
  99. struct acpi_device *acpi_find_child_device(struct acpi_device *parent,
  100. u64 address, bool check_children)
  101. {
  102. struct acpi_device *adev, *ret = NULL;
  103. int ret_score = 0;
  104. if (!parent)
  105. return NULL;
  106. list_for_each_entry(adev, &parent->children, node) {
  107. unsigned long long addr;
  108. acpi_status status;
  109. int score;
  110. status = acpi_evaluate_integer(adev->handle, METHOD_NAME__ADR,
  111. NULL, &addr);
  112. if (ACPI_FAILURE(status) || addr != address)
  113. continue;
  114. if (!ret) {
  115. /* This is the first matching object. Save it. */
  116. ret = adev;
  117. continue;
  118. }
  119. /*
  120. * There is more than one matching device object with the same
  121. * _ADR value. That really is unexpected, so we are kind of
  122. * beyond the scope of the spec here. We have to choose which
  123. * one to return, though.
  124. *
  125. * First, check if the previously found object is good enough
  126. * and return it if so. Second, do the same for the object that
  127. * we've just found.
  128. */
  129. if (!ret_score) {
  130. ret_score = find_child_checks(ret, check_children);
  131. if (ret_score == FIND_CHILD_MAX_SCORE)
  132. return ret;
  133. }
  134. score = find_child_checks(adev, check_children);
  135. if (score == FIND_CHILD_MAX_SCORE) {
  136. return adev;
  137. } else if (score > ret_score) {
  138. ret = adev;
  139. ret_score = score;
  140. }
  141. }
  142. return ret;
  143. }
  144. EXPORT_SYMBOL_GPL(acpi_find_child_device);
  145. static void acpi_physnode_link_name(char *buf, unsigned int node_id)
  146. {
  147. if (node_id > 0)
  148. snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
  149. PHYSICAL_NODE_STRING "%u", node_id);
  150. else
  151. strcpy(buf, PHYSICAL_NODE_STRING);
  152. }
  153. int acpi_bind_one(struct device *dev, struct acpi_device *acpi_dev)
  154. {
  155. struct acpi_device_physical_node *physical_node, *pn;
  156. char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
  157. struct list_head *physnode_list;
  158. unsigned int node_id;
  159. int retval = -EINVAL;
  160. if (has_acpi_companion(dev)) {
  161. if (acpi_dev) {
  162. dev_warn(dev, "ACPI companion already set\n");
  163. return -EINVAL;
  164. } else {
  165. acpi_dev = ACPI_COMPANION(dev);
  166. }
  167. }
  168. if (!acpi_dev)
  169. return -EINVAL;
  170. get_device(&acpi_dev->dev);
  171. get_device(dev);
  172. physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
  173. if (!physical_node) {
  174. retval = -ENOMEM;
  175. goto err;
  176. }
  177. mutex_lock(&acpi_dev->physical_node_lock);
  178. /*
  179. * Keep the list sorted by node_id so that the IDs of removed nodes can
  180. * be recycled easily.
  181. */
  182. physnode_list = &acpi_dev->physical_node_list;
  183. node_id = 0;
  184. list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
  185. /* Sanity check. */
  186. if (pn->dev == dev) {
  187. mutex_unlock(&acpi_dev->physical_node_lock);
  188. dev_warn(dev, "Already associated with ACPI node\n");
  189. kfree(physical_node);
  190. if (ACPI_COMPANION(dev) != acpi_dev)
  191. goto err;
  192. put_device(dev);
  193. put_device(&acpi_dev->dev);
  194. return 0;
  195. }
  196. if (pn->node_id == node_id) {
  197. physnode_list = &pn->node;
  198. node_id++;
  199. }
  200. }
  201. physical_node->node_id = node_id;
  202. physical_node->dev = dev;
  203. list_add(&physical_node->node, physnode_list);
  204. acpi_dev->physical_node_count++;
  205. if (!has_acpi_companion(dev))
  206. ACPI_COMPANION_SET(dev, acpi_dev);
  207. acpi_physnode_link_name(physical_node_name, node_id);
  208. retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  209. physical_node_name);
  210. if (retval)
  211. dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
  212. physical_node_name, retval);
  213. retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  214. "firmware_node");
  215. if (retval)
  216. dev_err(dev, "Failed to create link firmware_node (%d)\n",
  217. retval);
  218. mutex_unlock(&acpi_dev->physical_node_lock);
  219. if (acpi_dev->wakeup.flags.valid)
  220. device_set_wakeup_capable(dev, true);
  221. return 0;
  222. err:
  223. ACPI_COMPANION_SET(dev, NULL);
  224. put_device(dev);
  225. put_device(&acpi_dev->dev);
  226. return retval;
  227. }
  228. EXPORT_SYMBOL_GPL(acpi_bind_one);
  229. int acpi_unbind_one(struct device *dev)
  230. {
  231. struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
  232. struct acpi_device_physical_node *entry;
  233. if (!acpi_dev)
  234. return 0;
  235. mutex_lock(&acpi_dev->physical_node_lock);
  236. list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
  237. if (entry->dev == dev) {
  238. char physnode_name[PHYSICAL_NODE_NAME_SIZE];
  239. list_del(&entry->node);
  240. acpi_dev->physical_node_count--;
  241. acpi_physnode_link_name(physnode_name, entry->node_id);
  242. sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
  243. sysfs_remove_link(&dev->kobj, "firmware_node");
  244. ACPI_COMPANION_SET(dev, NULL);
  245. /* Drop references taken by acpi_bind_one(). */
  246. put_device(dev);
  247. put_device(&acpi_dev->dev);
  248. kfree(entry);
  249. break;
  250. }
  251. mutex_unlock(&acpi_dev->physical_node_lock);
  252. return 0;
  253. }
  254. EXPORT_SYMBOL_GPL(acpi_unbind_one);
  255. static int acpi_platform_notify(struct device *dev)
  256. {
  257. struct acpi_bus_type *type = acpi_get_bus_type(dev);
  258. struct acpi_device *adev;
  259. int ret;
  260. ret = acpi_bind_one(dev, NULL);
  261. if (ret && type) {
  262. struct acpi_device *adev;
  263. adev = type->find_companion(dev);
  264. if (!adev) {
  265. DBG("Unable to get handle for %s\n", dev_name(dev));
  266. ret = -ENODEV;
  267. goto out;
  268. }
  269. ret = acpi_bind_one(dev, adev);
  270. if (ret)
  271. goto out;
  272. }
  273. adev = ACPI_COMPANION(dev);
  274. if (!adev)
  275. goto out;
  276. if (type && type->setup)
  277. type->setup(dev);
  278. else if (adev->handler && adev->handler->bind)
  279. adev->handler->bind(dev);
  280. out:
  281. #if ACPI_GLUE_DEBUG
  282. if (!ret) {
  283. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  284. acpi_get_name(ACPI_HANDLE(dev), ACPI_FULL_PATHNAME, &buffer);
  285. DBG("Device %s -> %s\n", dev_name(dev), (char *)buffer.pointer);
  286. kfree(buffer.pointer);
  287. } else
  288. DBG("Device %s -> No ACPI support\n", dev_name(dev));
  289. #endif
  290. return ret;
  291. }
  292. static int acpi_platform_notify_remove(struct device *dev)
  293. {
  294. struct acpi_device *adev = ACPI_COMPANION(dev);
  295. struct acpi_bus_type *type;
  296. if (!adev)
  297. return 0;
  298. type = acpi_get_bus_type(dev);
  299. if (type && type->cleanup)
  300. type->cleanup(dev);
  301. else if (adev->handler && adev->handler->unbind)
  302. adev->handler->unbind(dev);
  303. acpi_unbind_one(dev);
  304. return 0;
  305. }
  306. void __init init_acpi_device_notify(void)
  307. {
  308. if (platform_notify || platform_notify_remove) {
  309. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  310. return;
  311. }
  312. platform_notify = acpi_platform_notify;
  313. platform_notify_remove = acpi_platform_notify_remove;
  314. }