glue.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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/init.h>
  10. #include <linux/list.h>
  11. #include <linux/device.h>
  12. #include <linux/rwsem.h>
  13. #include <linux/acpi.h>
  14. #define ACPI_GLUE_DEBUG 0
  15. #if ACPI_GLUE_DEBUG
  16. #define DBG(x...) printk(PREFIX x)
  17. #else
  18. #define DBG(x...)
  19. #endif
  20. static LIST_HEAD(bus_type_list);
  21. static DECLARE_RWSEM(bus_type_sem);
  22. int register_acpi_bus_type(struct acpi_bus_type *type)
  23. {
  24. if (acpi_disabled)
  25. return -ENODEV;
  26. if (type && type->bus && type->find_device) {
  27. down_write(&bus_type_sem);
  28. list_add_tail(&type->list, &bus_type_list);
  29. up_write(&bus_type_sem);
  30. printk(KERN_INFO PREFIX "bus type %s registered\n",
  31. type->bus->name);
  32. return 0;
  33. }
  34. return -ENODEV;
  35. }
  36. EXPORT_SYMBOL(register_acpi_bus_type);
  37. int unregister_acpi_bus_type(struct acpi_bus_type *type)
  38. {
  39. if (acpi_disabled)
  40. return 0;
  41. if (type) {
  42. down_write(&bus_type_sem);
  43. list_del_init(&type->list);
  44. up_write(&bus_type_sem);
  45. printk(KERN_INFO PREFIX "ACPI bus type %s unregistered\n",
  46. type->bus->name);
  47. return 0;
  48. }
  49. return -ENODEV;
  50. }
  51. EXPORT_SYMBOL(unregister_acpi_bus_type);
  52. static struct acpi_bus_type *acpi_get_bus_type(struct bus_type *type)
  53. {
  54. struct acpi_bus_type *tmp, *ret = NULL;
  55. down_read(&bus_type_sem);
  56. list_for_each_entry(tmp, &bus_type_list, list) {
  57. if (tmp->bus == type) {
  58. ret = tmp;
  59. break;
  60. }
  61. }
  62. up_read(&bus_type_sem);
  63. return ret;
  64. }
  65. static int acpi_find_bridge_device(struct device *dev, acpi_handle * handle)
  66. {
  67. struct acpi_bus_type *tmp;
  68. int ret = -ENODEV;
  69. down_read(&bus_type_sem);
  70. list_for_each_entry(tmp, &bus_type_list, list) {
  71. if (tmp->find_bridge && !tmp->find_bridge(dev, handle)) {
  72. ret = 0;
  73. break;
  74. }
  75. }
  76. up_read(&bus_type_sem);
  77. return ret;
  78. }
  79. /* Get device's handler per its address under its parent */
  80. struct acpi_find_child {
  81. acpi_handle handle;
  82. acpi_integer address;
  83. };
  84. static acpi_status
  85. do_acpi_find_child(acpi_handle handle, u32 lvl, void *context, void **rv)
  86. {
  87. acpi_status status;
  88. struct acpi_device_info *info;
  89. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  90. struct acpi_find_child *find = context;
  91. status = acpi_get_object_info(handle, &buffer);
  92. if (ACPI_SUCCESS(status)) {
  93. info = buffer.pointer;
  94. if (info->address == find->address)
  95. find->handle = handle;
  96. kfree(buffer.pointer);
  97. }
  98. return AE_OK;
  99. }
  100. acpi_handle acpi_get_child(acpi_handle parent, acpi_integer address)
  101. {
  102. struct acpi_find_child find = { NULL, address };
  103. if (!parent)
  104. return NULL;
  105. acpi_walk_namespace(ACPI_TYPE_DEVICE, parent,
  106. 1, do_acpi_find_child, &find, NULL);
  107. return find.handle;
  108. }
  109. EXPORT_SYMBOL(acpi_get_child);
  110. /* Link ACPI devices with physical devices */
  111. static void acpi_glue_data_handler(acpi_handle handle,
  112. u32 function, void *context)
  113. {
  114. /* we provide an empty handler */
  115. }
  116. /* Note: a success call will increase reference count by one */
  117. struct device *acpi_get_physical_device(acpi_handle handle)
  118. {
  119. acpi_status status;
  120. struct device *dev;
  121. status = acpi_get_data(handle, acpi_glue_data_handler, (void **)&dev);
  122. if (ACPI_SUCCESS(status))
  123. return get_device(dev);
  124. return NULL;
  125. }
  126. EXPORT_SYMBOL(acpi_get_physical_device);
  127. static int acpi_bind_one(struct device *dev, acpi_handle handle)
  128. {
  129. acpi_status status;
  130. if (dev->archdata.acpi_handle) {
  131. printk(KERN_WARNING PREFIX
  132. "Drivers changed 'acpi_handle' for %s\n", dev->bus_id);
  133. return -EINVAL;
  134. }
  135. get_device(dev);
  136. status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
  137. if (ACPI_FAILURE(status)) {
  138. put_device(dev);
  139. return -EINVAL;
  140. }
  141. dev->archdata.acpi_handle = handle;
  142. return 0;
  143. }
  144. static int acpi_unbind_one(struct device *dev)
  145. {
  146. if (!dev->archdata.acpi_handle)
  147. return 0;
  148. if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
  149. /* acpi_get_physical_device increase refcnt by one */
  150. put_device(dev);
  151. acpi_detach_data(dev->archdata.acpi_handle,
  152. acpi_glue_data_handler);
  153. dev->archdata.acpi_handle = NULL;
  154. /* acpi_bind_one increase refcnt by one */
  155. put_device(dev);
  156. } else {
  157. printk(KERN_ERR PREFIX
  158. "Oops, 'acpi_handle' corrupt for %s\n", dev->bus_id);
  159. }
  160. return 0;
  161. }
  162. static int acpi_platform_notify(struct device *dev)
  163. {
  164. struct acpi_bus_type *type;
  165. acpi_handle handle;
  166. int ret = -EINVAL;
  167. if (!dev->bus || !dev->parent) {
  168. /* bridge devices genernally haven't bus or parent */
  169. ret = acpi_find_bridge_device(dev, &handle);
  170. goto end;
  171. }
  172. type = acpi_get_bus_type(dev->bus);
  173. if (!type) {
  174. DBG("No ACPI bus support for %s\n", dev->bus_id);
  175. ret = -EINVAL;
  176. goto end;
  177. }
  178. if ((ret = type->find_device(dev, &handle)) != 0)
  179. DBG("Can't get handler for %s\n", dev->bus_id);
  180. end:
  181. if (!ret)
  182. acpi_bind_one(dev, handle);
  183. #if ACPI_GLUE_DEBUG
  184. if (!ret) {
  185. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  186. acpi_get_name(dev->archdata.acpi_handle,
  187. ACPI_FULL_PATHNAME, &buffer);
  188. DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer);
  189. kfree(buffer.pointer);
  190. } else
  191. DBG("Device %s -> No ACPI support\n", dev->bus_id);
  192. #endif
  193. return ret;
  194. }
  195. static int acpi_platform_notify_remove(struct device *dev)
  196. {
  197. acpi_unbind_one(dev);
  198. return 0;
  199. }
  200. static int __init init_acpi_device_notify(void)
  201. {
  202. if (acpi_disabled)
  203. return 0;
  204. if (platform_notify || platform_notify_remove) {
  205. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  206. return 0;
  207. }
  208. platform_notify = acpi_platform_notify;
  209. platform_notify_remove = acpi_platform_notify_remove;
  210. return 0;
  211. }
  212. arch_initcall(init_acpi_device_notify);
  213. #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE)
  214. #ifdef CONFIG_PM
  215. static u32 rtc_handler(void *context)
  216. {
  217. acpi_clear_event(ACPI_EVENT_RTC);
  218. acpi_disable_event(ACPI_EVENT_RTC, 0);
  219. return ACPI_INTERRUPT_HANDLED;
  220. }
  221. static inline void rtc_wake_setup(void)
  222. {
  223. acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
  224. }
  225. static void rtc_wake_on(struct device *dev)
  226. {
  227. acpi_clear_event(ACPI_EVENT_RTC);
  228. acpi_enable_event(ACPI_EVENT_RTC, 0);
  229. }
  230. static void rtc_wake_off(struct device *dev)
  231. {
  232. acpi_disable_event(ACPI_EVENT_RTC, 0);
  233. }
  234. #else
  235. #define rtc_wake_setup() do{}while(0)
  236. #define rtc_wake_on NULL
  237. #define rtc_wake_off NULL
  238. #endif
  239. /* Every ACPI platform has a mc146818 compatible "cmos rtc". Here we find
  240. * its device node and pass extra config data. This helps its driver use
  241. * capabilities that the now-obsolete mc146818 didn't have, and informs it
  242. * that this board's RTC is wakeup-capable (per ACPI spec).
  243. */
  244. #include <linux/mc146818rtc.h>
  245. static struct cmos_rtc_board_info rtc_info;
  246. /* PNP devices are registered in a subsys_initcall();
  247. * ACPI specifies the PNP IDs to use.
  248. */
  249. #include <linux/pnp.h>
  250. static int __init pnp_match(struct device *dev, void *data)
  251. {
  252. static const char *ids[] = { "PNP0b00", "PNP0b01", "PNP0b02", };
  253. struct pnp_dev *pnp = to_pnp_dev(dev);
  254. int i;
  255. for (i = 0; i < ARRAY_SIZE(ids); i++) {
  256. if (compare_pnp_id(pnp->id, ids[i]) != 0)
  257. return 1;
  258. }
  259. return 0;
  260. }
  261. static struct device *__init get_rtc_dev(void)
  262. {
  263. return bus_find_device(&pnp_bus_type, NULL, NULL, pnp_match);
  264. }
  265. static int __init acpi_rtc_init(void)
  266. {
  267. struct device *dev = get_rtc_dev();
  268. if (dev) {
  269. rtc_wake_setup();
  270. rtc_info.wake_on = rtc_wake_on;
  271. rtc_info.wake_off = rtc_wake_off;
  272. /* workaround bug in some ACPI tables */
  273. if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) {
  274. DBG("bogus FADT month_alarm\n");
  275. acpi_gbl_FADT.month_alarm = 0;
  276. }
  277. rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm;
  278. rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm;
  279. rtc_info.rtc_century = acpi_gbl_FADT.century;
  280. /* NOTE: S4_RTC_WAKE is NOT currently useful to Linux */
  281. if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE)
  282. printk(PREFIX "RTC can wake from S4\n");
  283. dev->platform_data = &rtc_info;
  284. /* RTC always wakes from S1/S2/S3, and often S4/STD */
  285. device_init_wakeup(dev, 1);
  286. put_device(dev);
  287. } else
  288. DBG("RTC unavailable?\n");
  289. return 0;
  290. }
  291. /* do this between RTC subsys_initcall() and rtc_cmos driver_initcall() */
  292. fs_initcall(acpi_rtc_init);
  293. #endif