wakeup.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * wakeup.c - support wakeup devices
  3. * Copyright (C) 2004 Li Shaohua <shaohua.li@intel.com>
  4. */
  5. #include <linux/init.h>
  6. #include <linux/acpi.h>
  7. #include <acpi/acpi_drivers.h>
  8. #include <linux/kernel.h>
  9. #include <linux/types.h>
  10. #include <acpi/acevents.h>
  11. #include "sleep.h"
  12. #define _COMPONENT ACPI_SYSTEM_COMPONENT
  13. ACPI_MODULE_NAME ("wakeup_devices")
  14. extern struct list_head acpi_wakeup_device_list;
  15. extern spinlock_t acpi_device_lock;
  16. #ifdef CONFIG_ACPI_SLEEP
  17. /**
  18. * acpi_enable_wakeup_device_prep - prepare wakeup devices
  19. * @sleep_state: ACPI state
  20. * Enable all wakup devices power if the devices' wakeup level
  21. * is higher than requested sleep level
  22. */
  23. void
  24. acpi_enable_wakeup_device_prep(
  25. u8 sleep_state)
  26. {
  27. struct list_head * node, * next;
  28. ACPI_FUNCTION_TRACE("acpi_enable_wakeup_device_prep");
  29. spin_lock(&acpi_device_lock);
  30. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  31. struct acpi_device * dev = container_of(node,
  32. struct acpi_device, wakeup_list);
  33. if (!dev->wakeup.flags.valid ||
  34. !dev->wakeup.state.enabled ||
  35. (sleep_state > (u32) dev->wakeup.sleep_state))
  36. continue;
  37. spin_unlock(&acpi_device_lock);
  38. acpi_enable_wakeup_device_power(dev);
  39. spin_lock(&acpi_device_lock);
  40. }
  41. spin_unlock(&acpi_device_lock);
  42. }
  43. /**
  44. * acpi_enable_wakeup_device - enable wakeup devices
  45. * @sleep_state: ACPI state
  46. * Enable all wakup devices's GPE
  47. */
  48. void
  49. acpi_enable_wakeup_device(
  50. u8 sleep_state)
  51. {
  52. struct list_head * node, * next;
  53. /*
  54. * Caution: this routine must be invoked when interrupt is disabled
  55. * Refer ACPI2.0: P212
  56. */
  57. ACPI_FUNCTION_TRACE("acpi_enable_wakeup_device");
  58. spin_lock(&acpi_device_lock);
  59. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  60. struct acpi_device * dev = container_of(node,
  61. struct acpi_device, wakeup_list);
  62. /* If users want to disable run-wake GPE,
  63. * we only disable it for wake and leave it for runtime
  64. */
  65. if (dev->wakeup.flags.run_wake && !dev->wakeup.state.enabled) {
  66. spin_unlock(&acpi_device_lock);
  67. acpi_set_gpe_type(dev->wakeup.gpe_device,
  68. dev->wakeup.gpe_number, ACPI_GPE_TYPE_RUNTIME);
  69. /* Re-enable it, since set_gpe_type will disable it */
  70. acpi_enable_gpe(dev->wakeup.gpe_device,
  71. dev->wakeup.gpe_number, ACPI_ISR);
  72. spin_lock(&acpi_device_lock);
  73. continue;
  74. }
  75. if (!dev->wakeup.flags.valid ||
  76. !dev->wakeup.state.enabled ||
  77. (sleep_state > (u32) dev->wakeup.sleep_state))
  78. continue;
  79. spin_unlock(&acpi_device_lock);
  80. /* run-wake GPE has been enabled */
  81. if (!dev->wakeup.flags.run_wake)
  82. acpi_enable_gpe(dev->wakeup.gpe_device,
  83. dev->wakeup.gpe_number, ACPI_ISR);
  84. dev->wakeup.state.active = 1;
  85. spin_lock(&acpi_device_lock);
  86. }
  87. spin_unlock(&acpi_device_lock);
  88. }
  89. /**
  90. * acpi_disable_wakeup_device - disable devices' wakeup capability
  91. * @sleep_state: ACPI state
  92. * Disable all wakup devices's GPE and wakeup capability
  93. */
  94. void
  95. acpi_disable_wakeup_device (
  96. u8 sleep_state)
  97. {
  98. struct list_head * node, * next;
  99. ACPI_FUNCTION_TRACE("acpi_disable_wakeup_device");
  100. spin_lock(&acpi_device_lock);
  101. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  102. struct acpi_device * dev = container_of(node,
  103. struct acpi_device, wakeup_list);
  104. if (dev->wakeup.flags.run_wake && !dev->wakeup.state.enabled) {
  105. spin_unlock(&acpi_device_lock);
  106. acpi_set_gpe_type(dev->wakeup.gpe_device,
  107. dev->wakeup.gpe_number, ACPI_GPE_TYPE_WAKE_RUN);
  108. /* Re-enable it, since set_gpe_type will disable it */
  109. acpi_enable_gpe(dev->wakeup.gpe_device,
  110. dev->wakeup.gpe_number, ACPI_NOT_ISR);
  111. spin_lock(&acpi_device_lock);
  112. continue;
  113. }
  114. if (!dev->wakeup.flags.valid ||
  115. !dev->wakeup.state.active ||
  116. (sleep_state > (u32) dev->wakeup.sleep_state))
  117. continue;
  118. spin_unlock(&acpi_device_lock);
  119. acpi_disable_wakeup_device_power(dev);
  120. /* Never disable run-wake GPE */
  121. if (!dev->wakeup.flags.run_wake) {
  122. acpi_disable_gpe(dev->wakeup.gpe_device,
  123. dev->wakeup.gpe_number, ACPI_NOT_ISR);
  124. acpi_clear_gpe(dev->wakeup.gpe_device,
  125. dev->wakeup.gpe_number, ACPI_NOT_ISR);
  126. }
  127. dev->wakeup.state.active = 0;
  128. spin_lock(&acpi_device_lock);
  129. }
  130. spin_unlock(&acpi_device_lock);
  131. }
  132. static int __init acpi_wakeup_device_init(void)
  133. {
  134. struct list_head * node, * next;
  135. if (acpi_disabled)
  136. return 0;
  137. printk("ACPI wakeup devices: \n");
  138. spin_lock(&acpi_device_lock);
  139. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  140. struct acpi_device * dev = container_of(node,
  141. struct acpi_device, wakeup_list);
  142. /* In case user doesn't load button driver */
  143. if (dev->wakeup.flags.run_wake && !dev->wakeup.state.enabled) {
  144. spin_unlock(&acpi_device_lock);
  145. acpi_set_gpe_type(dev->wakeup.gpe_device,
  146. dev->wakeup.gpe_number, ACPI_GPE_TYPE_WAKE_RUN);
  147. acpi_enable_gpe(dev->wakeup.gpe_device,
  148. dev->wakeup.gpe_number, ACPI_NOT_ISR);
  149. dev->wakeup.state.enabled = 1;
  150. spin_lock(&acpi_device_lock);
  151. }
  152. printk("%4s ", dev->pnp.bus_id);
  153. }
  154. spin_unlock(&acpi_device_lock);
  155. printk("\n");
  156. return 0;
  157. }
  158. late_initcall(acpi_wakeup_device_init);
  159. #endif
  160. /*
  161. * Disable all wakeup GPEs before power off.
  162. *
  163. * Since acpi_enter_sleep_state() will disable all
  164. * RUNTIME GPEs, we simply mark all GPES that
  165. * are not enabled for wakeup from S5 as RUNTIME.
  166. */
  167. void acpi_wakeup_gpe_poweroff_prepare(void)
  168. {
  169. struct list_head * node, * next;
  170. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  171. struct acpi_device * dev = container_of(node,
  172. struct acpi_device, wakeup_list);
  173. /* The GPE can wakeup system from S5, don't touch it */
  174. if ((u32)dev->wakeup.sleep_state == ACPI_STATE_S5)
  175. continue;
  176. /* acpi_set_gpe_type will automatically disable GPE */
  177. acpi_set_gpe_type(dev->wakeup.gpe_device,
  178. dev->wakeup.gpe_number, ACPI_GPE_TYPE_RUNTIME);
  179. }
  180. }