glue.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. struct acpi_device *acpi_dev;
  128. acpi_status status;
  129. if (dev->archdata.acpi_handle) {
  130. printk(KERN_WARNING PREFIX
  131. "Drivers changed 'acpi_handle' for %s\n", dev->bus_id);
  132. return -EINVAL;
  133. }
  134. get_device(dev);
  135. status = acpi_attach_data(handle, acpi_glue_data_handler, dev);
  136. if (ACPI_FAILURE(status)) {
  137. put_device(dev);
  138. return -EINVAL;
  139. }
  140. dev->archdata.acpi_handle = handle;
  141. status = acpi_bus_get_device(handle, &acpi_dev);
  142. if (!ACPI_FAILURE(status)) {
  143. int ret;
  144. ret = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
  145. "firmware_node");
  146. ret = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
  147. "physical_node");
  148. }
  149. return 0;
  150. }
  151. static int acpi_unbind_one(struct device *dev)
  152. {
  153. if (!dev->archdata.acpi_handle)
  154. return 0;
  155. if (dev == acpi_get_physical_device(dev->archdata.acpi_handle)) {
  156. struct acpi_device *acpi_dev;
  157. /* acpi_get_physical_device increase refcnt by one */
  158. put_device(dev);
  159. if (!acpi_bus_get_device(dev->archdata.acpi_handle,
  160. &acpi_dev)) {
  161. sysfs_remove_link(&dev->kobj, "firmware_node");
  162. sysfs_remove_link(&acpi_dev->dev.kobj, "physical_node");
  163. }
  164. acpi_detach_data(dev->archdata.acpi_handle,
  165. acpi_glue_data_handler);
  166. dev->archdata.acpi_handle = NULL;
  167. /* acpi_bind_one increase refcnt by one */
  168. put_device(dev);
  169. } else {
  170. printk(KERN_ERR PREFIX
  171. "Oops, 'acpi_handle' corrupt for %s\n", dev->bus_id);
  172. }
  173. return 0;
  174. }
  175. static int acpi_platform_notify(struct device *dev)
  176. {
  177. struct acpi_bus_type *type;
  178. acpi_handle handle;
  179. int ret = -EINVAL;
  180. if (!dev->bus || !dev->parent) {
  181. /* bridge devices genernally haven't bus or parent */
  182. ret = acpi_find_bridge_device(dev, &handle);
  183. goto end;
  184. }
  185. type = acpi_get_bus_type(dev->bus);
  186. if (!type) {
  187. DBG("No ACPI bus support for %s\n", dev->bus_id);
  188. ret = -EINVAL;
  189. goto end;
  190. }
  191. if ((ret = type->find_device(dev, &handle)) != 0)
  192. DBG("Can't get handler for %s\n", dev->bus_id);
  193. end:
  194. if (!ret)
  195. acpi_bind_one(dev, handle);
  196. #if ACPI_GLUE_DEBUG
  197. if (!ret) {
  198. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  199. acpi_get_name(dev->archdata.acpi_handle,
  200. ACPI_FULL_PATHNAME, &buffer);
  201. DBG("Device %s -> %s\n", dev->bus_id, (char *)buffer.pointer);
  202. kfree(buffer.pointer);
  203. } else
  204. DBG("Device %s -> No ACPI support\n", dev->bus_id);
  205. #endif
  206. return ret;
  207. }
  208. static int acpi_platform_notify_remove(struct device *dev)
  209. {
  210. acpi_unbind_one(dev);
  211. return 0;
  212. }
  213. static int __init init_acpi_device_notify(void)
  214. {
  215. if (acpi_disabled)
  216. return 0;
  217. if (platform_notify || platform_notify_remove) {
  218. printk(KERN_ERR PREFIX "Can't use platform_notify\n");
  219. return 0;
  220. }
  221. platform_notify = acpi_platform_notify;
  222. platform_notify_remove = acpi_platform_notify_remove;
  223. return 0;
  224. }
  225. arch_initcall(init_acpi_device_notify);
  226. #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE)
  227. #ifdef CONFIG_PM
  228. static u32 rtc_handler(void *context)
  229. {
  230. acpi_clear_event(ACPI_EVENT_RTC);
  231. acpi_disable_event(ACPI_EVENT_RTC, 0);
  232. return ACPI_INTERRUPT_HANDLED;
  233. }
  234. static inline void rtc_wake_setup(void)
  235. {
  236. acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
  237. /*
  238. * After the RTC handler is installed, the Fixed_RTC event should
  239. * be disabled. Only when the RTC alarm is set will it be enabled.
  240. */
  241. acpi_clear_event(ACPI_EVENT_RTC);
  242. acpi_disable_event(ACPI_EVENT_RTC, 0);
  243. }
  244. static void rtc_wake_on(struct device *dev)
  245. {
  246. acpi_clear_event(ACPI_EVENT_RTC);
  247. acpi_enable_event(ACPI_EVENT_RTC, 0);
  248. }
  249. static void rtc_wake_off(struct device *dev)
  250. {
  251. acpi_disable_event(ACPI_EVENT_RTC, 0);
  252. }
  253. #else
  254. #define rtc_wake_setup() do{}while(0)
  255. #define rtc_wake_on NULL
  256. #define rtc_wake_off NULL
  257. #endif
  258. /* Every ACPI platform has a mc146818 compatible "cmos rtc". Here we find
  259. * its device node and pass extra config data. This helps its driver use
  260. * capabilities that the now-obsolete mc146818 didn't have, and informs it
  261. * that this board's RTC is wakeup-capable (per ACPI spec).
  262. */
  263. #include <linux/mc146818rtc.h>
  264. static struct cmos_rtc_board_info rtc_info;
  265. /* PNP devices are registered in a subsys_initcall();
  266. * ACPI specifies the PNP IDs to use.
  267. */
  268. #include <linux/pnp.h>
  269. static int __init pnp_match(struct device *dev, void *data)
  270. {
  271. static const char *ids[] = { "PNP0b00", "PNP0b01", "PNP0b02", };
  272. struct pnp_dev *pnp = to_pnp_dev(dev);
  273. int i;
  274. for (i = 0; i < ARRAY_SIZE(ids); i++) {
  275. if (compare_pnp_id(pnp->id, ids[i]) != 0)
  276. return 1;
  277. }
  278. return 0;
  279. }
  280. static struct device *__init get_rtc_dev(void)
  281. {
  282. return bus_find_device(&pnp_bus_type, NULL, NULL, pnp_match);
  283. }
  284. static int __init acpi_rtc_init(void)
  285. {
  286. struct device *dev = get_rtc_dev();
  287. if (dev) {
  288. rtc_wake_setup();
  289. rtc_info.wake_on = rtc_wake_on;
  290. rtc_info.wake_off = rtc_wake_off;
  291. /* workaround bug in some ACPI tables */
  292. if (acpi_gbl_FADT.month_alarm && !acpi_gbl_FADT.day_alarm) {
  293. DBG("bogus FADT month_alarm\n");
  294. acpi_gbl_FADT.month_alarm = 0;
  295. }
  296. rtc_info.rtc_day_alarm = acpi_gbl_FADT.day_alarm;
  297. rtc_info.rtc_mon_alarm = acpi_gbl_FADT.month_alarm;
  298. rtc_info.rtc_century = acpi_gbl_FADT.century;
  299. /* NOTE: S4_RTC_WAKE is NOT currently useful to Linux */
  300. if (acpi_gbl_FADT.flags & ACPI_FADT_S4_RTC_WAKE)
  301. printk(PREFIX "RTC can wake from S4\n");
  302. dev->platform_data = &rtc_info;
  303. /* RTC always wakes from S1/S2/S3, and often S4/STD */
  304. device_init_wakeup(dev, 1);
  305. put_device(dev);
  306. } else
  307. DBG("RTC unavailable?\n");
  308. return 0;
  309. }
  310. /* do this between RTC subsys_initcall() and rtc_cmos driver_initcall() */
  311. fs_initcall(acpi_rtc_init);
  312. #endif