Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * wakeup.c - support wakeup devices |
| 4 | * Copyright (C) 2004 Li Shaohua <shaohua.li@intel.com> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/acpi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | #include <linux/types.h> |
Bjorn Helgaas | e60cc7a | 2009-03-13 12:08:26 -0600 | [diff] [blame] | 11 | |
| 12 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "sleep.h" |
| 14 | |
Hans de Goede | ddfd9dc | 2020-04-03 17:48:33 +0200 | [diff] [blame] | 15 | struct acpi_wakeup_handler { |
| 16 | struct list_head list_node; |
| 17 | bool (*wakeup)(void *context); |
| 18 | void *context; |
| 19 | }; |
| 20 | |
| 21 | static LIST_HEAD(acpi_wakeup_handler_head); |
| 22 | static DEFINE_MUTEX(acpi_wakeup_handler_mutex); |
| 23 | |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 24 | /* |
| 25 | * We didn't lock acpi_device_lock in the file, because it invokes oops in |
| 26 | * suspend/resume and isn't really required as this is called in S-state. At |
| 27 | * that time, there is no device hotplug |
| 28 | **/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | /** |
Rafael J. Wysocki | 78f5f02 | 2010-07-06 22:09:38 -0400 | [diff] [blame] | 31 | * acpi_enable_wakeup_devices - Enable wake-up device GPEs. |
Rafael J. Wysocki | 9630bdd | 2010-02-17 23:41:07 +0100 | [diff] [blame] | 32 | * @sleep_state: ACPI system sleep state. |
| 33 | * |
Rafael J. Wysocki | 78f5f02 | 2010-07-06 22:09:38 -0400 | [diff] [blame] | 34 | * Enable wakeup device power of devices with the state.enable flag set and set |
| 35 | * the wakeup enable mask bits in the GPE registers that correspond to wakeup |
| 36 | * devices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | */ |
Rafael J. Wysocki | 78f5f02 | 2010-07-06 22:09:38 -0400 | [diff] [blame] | 38 | void acpi_enable_wakeup_devices(u8 sleep_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
chenqiwu | 07761a4 | 2020-02-23 14:08:22 +0800 | [diff] [blame] | 40 | struct acpi_device *dev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
chenqiwu | 07761a4 | 2020-02-23 14:08:22 +0800 | [diff] [blame] | 42 | list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list, |
| 43 | wakeup_list) { |
Rafael J. Wysocki | e8b6f97 | 2010-06-25 01:18:39 +0200 | [diff] [blame] | 44 | if (!dev->wakeup.flags.valid |
Rafael J. Wysocki | f2b56bc | 2011-01-06 23:34:22 +0100 | [diff] [blame] | 45 | || sleep_state > (u32) dev->wakeup.sleep_state |
| 46 | || !(device_may_wakeup(&dev->dev) |
Maximilian Luz | c6237b2 | 2020-11-05 03:06:00 +0100 | [diff] [blame] | 47 | || dev->wakeup.prepare_count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | continue; |
Rafael J. Wysocki | 9630bdd | 2010-02-17 23:41:07 +0100 | [diff] [blame] | 49 | |
Rafael J. Wysocki | f2b56bc | 2011-01-06 23:34:22 +0100 | [diff] [blame] | 50 | if (device_may_wakeup(&dev->dev)) |
Rafael J. Wysocki | 78f5f02 | 2010-07-06 22:09:38 -0400 | [diff] [blame] | 51 | acpi_enable_wakeup_device_power(dev, sleep_state); |
| 52 | |
Rafael J. Wysocki | 9630bdd | 2010-02-17 23:41:07 +0100 | [diff] [blame] | 53 | /* The wake-up power should have been enabled already. */ |
Lin Ming | 3a37898 | 2010-12-13 13:36:15 +0800 | [diff] [blame] | 54 | acpi_set_gpe_wake_mask(dev->wakeup.gpe_device, dev->wakeup.gpe_number, |
Rafael J. Wysocki | e8b6f97 | 2010-06-25 01:18:39 +0200 | [diff] [blame] | 55 | ACPI_GPE_ENABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
Rafael J. Wysocki | 78f5f02 | 2010-07-06 22:09:38 -0400 | [diff] [blame] | 60 | * acpi_disable_wakeup_devices - Disable devices' wakeup capability. |
Rafael J. Wysocki | 9630bdd | 2010-02-17 23:41:07 +0100 | [diff] [blame] | 61 | * @sleep_state: ACPI system sleep state. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | */ |
Rafael J. Wysocki | 78f5f02 | 2010-07-06 22:09:38 -0400 | [diff] [blame] | 63 | void acpi_disable_wakeup_devices(u8 sleep_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
chenqiwu | 07761a4 | 2020-02-23 14:08:22 +0800 | [diff] [blame] | 65 | struct acpi_device *dev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
chenqiwu | 07761a4 | 2020-02-23 14:08:22 +0800 | [diff] [blame] | 67 | list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list, |
| 68 | wakeup_list) { |
Rafael J. Wysocki | e8b6f97 | 2010-06-25 01:18:39 +0200 | [diff] [blame] | 69 | if (!dev->wakeup.flags.valid |
Rafael J. Wysocki | f2b56bc | 2011-01-06 23:34:22 +0100 | [diff] [blame] | 70 | || sleep_state > (u32) dev->wakeup.sleep_state |
| 71 | || !(device_may_wakeup(&dev->dev) |
Maximilian Luz | c6237b2 | 2020-11-05 03:06:00 +0100 | [diff] [blame] | 72 | || dev->wakeup.prepare_count)) |
Alexey Starikovskiy | 9b03933 | 2007-09-26 19:47:30 +0400 | [diff] [blame] | 73 | continue; |
Rafael J. Wysocki | eb9d0fe | 2008-07-07 03:34:48 +0200 | [diff] [blame] | 74 | |
Lin Ming | 3a37898 | 2010-12-13 13:36:15 +0800 | [diff] [blame] | 75 | acpi_set_gpe_wake_mask(dev->wakeup.gpe_device, dev->wakeup.gpe_number, |
Rafael J. Wysocki | e8b6f97 | 2010-06-25 01:18:39 +0200 | [diff] [blame] | 76 | ACPI_GPE_DISABLE); |
| 77 | |
Rafael J. Wysocki | f2b56bc | 2011-01-06 23:34:22 +0100 | [diff] [blame] | 78 | if (device_may_wakeup(&dev->dev)) |
Rafael J. Wysocki | e8b6f97 | 2010-06-25 01:18:39 +0200 | [diff] [blame] | 79 | acpi_disable_wakeup_device_power(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Bjorn Helgaas | 201b8c6 | 2009-03-24 16:50:19 -0600 | [diff] [blame] | 83 | int __init acpi_wakeup_device_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
chenqiwu | 07761a4 | 2020-02-23 14:08:22 +0800 | [diff] [blame] | 85 | struct acpi_device *dev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 87 | mutex_lock(&acpi_device_lock); |
chenqiwu | 07761a4 | 2020-02-23 14:08:22 +0800 | [diff] [blame] | 88 | list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list, |
| 89 | wakeup_list) { |
Rafael J. Wysocki | 2a5d242 | 2011-02-12 01:39:53 +0100 | [diff] [blame] | 90 | if (device_can_wakeup(&dev->dev)) { |
| 91 | /* Button GPEs are supposed to be always enabled. */ |
| 92 | acpi_enable_gpe(dev->wakeup.gpe_device, |
| 93 | dev->wakeup.gpe_number); |
Rafael J. Wysocki | f2b56bc | 2011-01-06 23:34:22 +0100 | [diff] [blame] | 94 | device_set_wakeup_enable(&dev->dev, true); |
Rafael J. Wysocki | 2a5d242 | 2011-02-12 01:39:53 +0100 | [diff] [blame] | 95 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 97 | mutex_unlock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return 0; |
| 99 | } |
Hans de Goede | ddfd9dc | 2020-04-03 17:48:33 +0200 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * acpi_register_wakeup_handler - Register wakeup handler |
| 103 | * @wake_irq: The IRQ through which the device may receive wakeups |
| 104 | * @wakeup: Wakeup-handler to call when the SCI has triggered a wakeup |
| 105 | * @context: Context to pass to the handler when calling it |
| 106 | * |
| 107 | * Drivers which may share an IRQ with the SCI can use this to register |
| 108 | * a handler which returns true when the device they are managing wants |
| 109 | * to trigger a wakeup. |
| 110 | */ |
| 111 | int acpi_register_wakeup_handler(int wake_irq, bool (*wakeup)(void *context), |
| 112 | void *context) |
| 113 | { |
| 114 | struct acpi_wakeup_handler *handler; |
| 115 | |
| 116 | /* |
| 117 | * If the device is not sharing its IRQ with the SCI, there is no |
| 118 | * need to register the handler. |
| 119 | */ |
| 120 | if (!acpi_sci_irq_valid() || wake_irq != acpi_sci_irq) |
| 121 | return 0; |
| 122 | |
| 123 | handler = kmalloc(sizeof(*handler), GFP_KERNEL); |
| 124 | if (!handler) |
| 125 | return -ENOMEM; |
| 126 | |
| 127 | handler->wakeup = wakeup; |
| 128 | handler->context = context; |
| 129 | |
| 130 | mutex_lock(&acpi_wakeup_handler_mutex); |
| 131 | list_add(&handler->list_node, &acpi_wakeup_handler_head); |
| 132 | mutex_unlock(&acpi_wakeup_handler_mutex); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | EXPORT_SYMBOL_GPL(acpi_register_wakeup_handler); |
| 137 | |
| 138 | /** |
| 139 | * acpi_unregister_wakeup_handler - Unregister wakeup handler |
| 140 | * @wakeup: Wakeup-handler passed to acpi_register_wakeup_handler() |
| 141 | * @context: Context passed to acpi_register_wakeup_handler() |
| 142 | */ |
| 143 | void acpi_unregister_wakeup_handler(bool (*wakeup)(void *context), |
| 144 | void *context) |
| 145 | { |
| 146 | struct acpi_wakeup_handler *handler; |
| 147 | |
| 148 | mutex_lock(&acpi_wakeup_handler_mutex); |
| 149 | list_for_each_entry(handler, &acpi_wakeup_handler_head, list_node) { |
| 150 | if (handler->wakeup == wakeup && handler->context == context) { |
| 151 | list_del(&handler->list_node); |
| 152 | kfree(handler); |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | mutex_unlock(&acpi_wakeup_handler_mutex); |
| 157 | } |
| 158 | EXPORT_SYMBOL_GPL(acpi_unregister_wakeup_handler); |
| 159 | |
| 160 | bool acpi_check_wakeup_handlers(void) |
| 161 | { |
| 162 | struct acpi_wakeup_handler *handler; |
| 163 | |
| 164 | /* No need to lock, nothing else is running when we're called. */ |
| 165 | list_for_each_entry(handler, &acpi_wakeup_handler_head, list_node) { |
| 166 | if (handler->wakeup(handler->context)) |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | return false; |
| 171 | } |