glue.c 8.8 KB

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