wakeup.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 acpi_enable_wakeup_device_prep(u8 sleep_state)
  24. {
  25. struct list_head *node, *next;
  26. ACPI_FUNCTION_TRACE("acpi_enable_wakeup_device_prep");
  27. spin_lock(&acpi_device_lock);
  28. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  29. struct acpi_device *dev = container_of(node,
  30. struct acpi_device,
  31. wakeup_list);
  32. if (!dev->wakeup.flags.valid ||
  33. !dev->wakeup.state.enabled ||
  34. (sleep_state > (u32) dev->wakeup.sleep_state))
  35. continue;
  36. spin_unlock(&acpi_device_lock);
  37. acpi_enable_wakeup_device_power(dev);
  38. spin_lock(&acpi_device_lock);
  39. }
  40. spin_unlock(&acpi_device_lock);
  41. }
  42. /**
  43. * acpi_enable_wakeup_device - enable wakeup devices
  44. * @sleep_state: ACPI state
  45. * Enable all wakup devices's GPE
  46. */
  47. void acpi_enable_wakeup_device(u8 sleep_state)
  48. {
  49. struct list_head *node, *next;
  50. /*
  51. * Caution: this routine must be invoked when interrupt is disabled
  52. * Refer ACPI2.0: P212
  53. */
  54. ACPI_FUNCTION_TRACE("acpi_enable_wakeup_device");
  55. spin_lock(&acpi_device_lock);
  56. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  57. struct acpi_device *dev = container_of(node,
  58. struct acpi_device,
  59. wakeup_list);
  60. /* If users want to disable run-wake GPE,
  61. * we only disable it for wake and leave it for runtime
  62. */
  63. if (dev->wakeup.flags.run_wake && !dev->wakeup.state.enabled) {
  64. spin_unlock(&acpi_device_lock);
  65. acpi_set_gpe_type(dev->wakeup.gpe_device,
  66. dev->wakeup.gpe_number,
  67. ACPI_GPE_TYPE_RUNTIME);
  68. /* Re-enable it, since set_gpe_type will disable it */
  69. acpi_enable_gpe(dev->wakeup.gpe_device,
  70. dev->wakeup.gpe_number, ACPI_ISR);
  71. spin_lock(&acpi_device_lock);
  72. continue;
  73. }
  74. if (!dev->wakeup.flags.valid ||
  75. !dev->wakeup.state.enabled ||
  76. (sleep_state > (u32) dev->wakeup.sleep_state))
  77. continue;
  78. spin_unlock(&acpi_device_lock);
  79. /* run-wake GPE has been enabled */
  80. if (!dev->wakeup.flags.run_wake)
  81. acpi_enable_gpe(dev->wakeup.gpe_device,
  82. dev->wakeup.gpe_number, ACPI_ISR);
  83. dev->wakeup.state.active = 1;
  84. spin_lock(&acpi_device_lock);
  85. }
  86. spin_unlock(&acpi_device_lock);
  87. }
  88. /**
  89. * acpi_disable_wakeup_device - disable devices' wakeup capability
  90. * @sleep_state: ACPI state
  91. * Disable all wakup devices's GPE and wakeup capability
  92. */
  93. void acpi_disable_wakeup_device(u8 sleep_state)
  94. {
  95. struct list_head *node, *next;
  96. ACPI_FUNCTION_TRACE("acpi_disable_wakeup_device");
  97. spin_lock(&acpi_device_lock);
  98. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  99. struct acpi_device *dev = container_of(node,
  100. struct acpi_device,
  101. wakeup_list);
  102. if (dev->wakeup.flags.run_wake && !dev->wakeup.state.enabled) {
  103. spin_unlock(&acpi_device_lock);
  104. acpi_set_gpe_type(dev->wakeup.gpe_device,
  105. dev->wakeup.gpe_number,
  106. ACPI_GPE_TYPE_WAKE_RUN);
  107. /* Re-enable it, since set_gpe_type will disable it */
  108. acpi_enable_gpe(dev->wakeup.gpe_device,
  109. dev->wakeup.gpe_number, ACPI_NOT_ISR);
  110. spin_lock(&acpi_device_lock);
  111. continue;
  112. }
  113. if (!dev->wakeup.flags.valid ||
  114. !dev->wakeup.state.active ||
  115. (sleep_state > (u32) dev->wakeup.sleep_state))
  116. continue;
  117. spin_unlock(&acpi_device_lock);
  118. acpi_disable_wakeup_device_power(dev);
  119. /* Never disable run-wake GPE */
  120. if (!dev->wakeup.flags.run_wake) {
  121. acpi_disable_gpe(dev->wakeup.gpe_device,
  122. dev->wakeup.gpe_number, ACPI_NOT_ISR);
  123. acpi_clear_gpe(dev->wakeup.gpe_device,
  124. dev->wakeup.gpe_number, ACPI_NOT_ISR);
  125. }
  126. dev->wakeup.state.active = 0;
  127. spin_lock(&acpi_device_lock);
  128. }
  129. spin_unlock(&acpi_device_lock);
  130. }
  131. static int __init acpi_wakeup_device_init(void)
  132. {
  133. struct list_head *node, *next;
  134. if (acpi_disabled)
  135. return 0;
  136. printk("ACPI wakeup devices: \n");
  137. spin_lock(&acpi_device_lock);
  138. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  139. struct acpi_device *dev = container_of(node,
  140. struct acpi_device,
  141. 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,
  147. ACPI_GPE_TYPE_WAKE_RUN);
  148. acpi_enable_gpe(dev->wakeup.gpe_device,
  149. dev->wakeup.gpe_number, ACPI_NOT_ISR);
  150. dev->wakeup.state.enabled = 1;
  151. spin_lock(&acpi_device_lock);
  152. }
  153. printk("%4s ", dev->pnp.bus_id);
  154. }
  155. spin_unlock(&acpi_device_lock);
  156. printk("\n");
  157. return 0;
  158. }
  159. late_initcall(acpi_wakeup_device_init);
  160. #endif
  161. /*
  162. * Disable all wakeup GPEs before power off.
  163. *
  164. * Since acpi_enter_sleep_state() will disable all
  165. * RUNTIME GPEs, we simply mark all GPES that
  166. * are not enabled for wakeup from S5 as RUNTIME.
  167. */
  168. void acpi_wakeup_gpe_poweroff_prepare(void)
  169. {
  170. struct list_head *node, *next;
  171. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  172. struct acpi_device *dev = container_of(node,
  173. struct acpi_device,
  174. wakeup_list);
  175. /* The GPE can wakeup system from S5, don't touch it */
  176. if ((u32) dev->wakeup.sleep_state == ACPI_STATE_S5)
  177. continue;
  178. /* acpi_set_gpe_type will automatically disable GPE */
  179. acpi_set_gpe_type(dev->wakeup.gpe_device,
  180. dev->wakeup.gpe_number,
  181. ACPI_GPE_TYPE_RUNTIME);
  182. }
  183. }