glue.c 7.9 KB

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