glue.c 8.8 KB

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