wakeup.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * wakeup.c - support wakeup devices
  4. * Copyright (C) 2004 Li Shaohua <shaohua.li@intel.com>
  5. */
  6. #include <linux/init.h>
  7. #include <linux/acpi.h>
  8. #include <linux/kernel.h>
  9. #include <linux/types.h>
  10. #include "internal.h"
  11. #include "sleep.h"
  12. struct acpi_wakeup_handler {
  13. struct list_head list_node;
  14. bool (*wakeup)(void *context);
  15. void *context;
  16. };
  17. static LIST_HEAD(acpi_wakeup_handler_head);
  18. static DEFINE_MUTEX(acpi_wakeup_handler_mutex);
  19. /*
  20. * We didn't lock acpi_device_lock in the file, because it invokes oops in
  21. * suspend/resume and isn't really required as this is called in S-state. At
  22. * that time, there is no device hotplug
  23. **/
  24. /**
  25. * acpi_enable_wakeup_devices - Enable wake-up device GPEs.
  26. * @sleep_state: ACPI system sleep state.
  27. *
  28. * Enable wakeup device power of devices with the state.enable flag set and set
  29. * the wakeup enable mask bits in the GPE registers that correspond to wakeup
  30. * devices.
  31. */
  32. void acpi_enable_wakeup_devices(u8 sleep_state)
  33. {
  34. struct acpi_device *dev, *tmp;
  35. list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
  36. wakeup_list) {
  37. if (!dev->wakeup.flags.valid
  38. || sleep_state > (u32) dev->wakeup.sleep_state
  39. || !(device_may_wakeup(&dev->dev)
  40. || dev->wakeup.prepare_count))
  41. continue;
  42. if (device_may_wakeup(&dev->dev))
  43. acpi_enable_wakeup_device_power(dev, sleep_state);
  44. /* The wake-up power should have been enabled already. */
  45. acpi_set_gpe_wake_mask(dev->wakeup.gpe_device, dev->wakeup.gpe_number,
  46. ACPI_GPE_ENABLE);
  47. }
  48. }
  49. /**
  50. * acpi_disable_wakeup_devices - Disable devices' wakeup capability.
  51. * @sleep_state: ACPI system sleep state.
  52. */
  53. void acpi_disable_wakeup_devices(u8 sleep_state)
  54. {
  55. struct acpi_device *dev, *tmp;
  56. list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
  57. wakeup_list) {
  58. if (!dev->wakeup.flags.valid
  59. || sleep_state > (u32) dev->wakeup.sleep_state
  60. || !(device_may_wakeup(&dev->dev)
  61. || dev->wakeup.prepare_count))
  62. continue;
  63. acpi_set_gpe_wake_mask(dev->wakeup.gpe_device, dev->wakeup.gpe_number,
  64. ACPI_GPE_DISABLE);
  65. if (device_may_wakeup(&dev->dev))
  66. acpi_disable_wakeup_device_power(dev);
  67. }
  68. }
  69. int __init acpi_wakeup_device_init(void)
  70. {
  71. struct acpi_device *dev, *tmp;
  72. mutex_lock(&acpi_device_lock);
  73. list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
  74. wakeup_list) {
  75. if (device_can_wakeup(&dev->dev)) {
  76. /* Button GPEs are supposed to be always enabled. */
  77. acpi_enable_gpe(dev->wakeup.gpe_device,
  78. dev->wakeup.gpe_number);
  79. device_set_wakeup_enable(&dev->dev, true);
  80. }
  81. }
  82. mutex_unlock(&acpi_device_lock);
  83. return 0;
  84. }
  85. /**
  86. * acpi_register_wakeup_handler - Register wakeup handler
  87. * @wake_irq: The IRQ through which the device may receive wakeups
  88. * @wakeup: Wakeup-handler to call when the SCI has triggered a wakeup
  89. * @context: Context to pass to the handler when calling it
  90. *
  91. * Drivers which may share an IRQ with the SCI can use this to register
  92. * a handler which returns true when the device they are managing wants
  93. * to trigger a wakeup.
  94. */
  95. int acpi_register_wakeup_handler(int wake_irq, bool (*wakeup)(void *context),
  96. void *context)
  97. {
  98. struct acpi_wakeup_handler *handler;
  99. /*
  100. * If the device is not sharing its IRQ with the SCI, there is no
  101. * need to register the handler.
  102. */
  103. if (!acpi_sci_irq_valid() || wake_irq != acpi_sci_irq)
  104. return 0;
  105. handler = kmalloc(sizeof(*handler), GFP_KERNEL);
  106. if (!handler)
  107. return -ENOMEM;
  108. handler->wakeup = wakeup;
  109. handler->context = context;
  110. mutex_lock(&acpi_wakeup_handler_mutex);
  111. list_add(&handler->list_node, &acpi_wakeup_handler_head);
  112. mutex_unlock(&acpi_wakeup_handler_mutex);
  113. return 0;
  114. }
  115. EXPORT_SYMBOL_GPL(acpi_register_wakeup_handler);
  116. /**
  117. * acpi_unregister_wakeup_handler - Unregister wakeup handler
  118. * @wakeup: Wakeup-handler passed to acpi_register_wakeup_handler()
  119. * @context: Context passed to acpi_register_wakeup_handler()
  120. */
  121. void acpi_unregister_wakeup_handler(bool (*wakeup)(void *context),
  122. void *context)
  123. {
  124. struct acpi_wakeup_handler *handler;
  125. mutex_lock(&acpi_wakeup_handler_mutex);
  126. list_for_each_entry(handler, &acpi_wakeup_handler_head, list_node) {
  127. if (handler->wakeup == wakeup && handler->context == context) {
  128. list_del(&handler->list_node);
  129. kfree(handler);
  130. break;
  131. }
  132. }
  133. mutex_unlock(&acpi_wakeup_handler_mutex);
  134. }
  135. EXPORT_SYMBOL_GPL(acpi_unregister_wakeup_handler);
  136. bool acpi_check_wakeup_handlers(void)
  137. {
  138. struct acpi_wakeup_handler *handler;
  139. /* No need to lock, nothing else is running when we're called. */
  140. list_for_each_entry(handler, &acpi_wakeup_handler_head, list_node) {
  141. if (handler->wakeup(handler->context))
  142. return true;
  143. }
  144. return false;
  145. }