1 // SPDX-License-Identifier: GPL-2.0
3 * wakeup.c - support wakeup devices
4 * Copyright (C) 2004 Li Shaohua <shaohua.li@intel.com>
7 #include <linux/init.h>
8 #include <linux/acpi.h>
9 #include <linux/kernel.h>
10 #include <linux/types.h>
15 struct acpi_wakeup_handler
{
16 struct list_head list_node
;
17 bool (*wakeup
)(void *context
);
21 static LIST_HEAD(acpi_wakeup_handler_head
);
22 static DEFINE_MUTEX(acpi_wakeup_handler_mutex
);
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
29 #define _COMPONENT ACPI_SYSTEM_COMPONENT
30 ACPI_MODULE_NAME("wakeup_devices")
33 * acpi_enable_wakeup_devices - Enable wake-up device GPEs.
34 * @sleep_state: ACPI system sleep state.
36 * Enable wakeup device power of devices with the state.enable flag set and set
37 * the wakeup enable mask bits in the GPE registers that correspond to wakeup
40 void acpi_enable_wakeup_devices(u8 sleep_state
)
42 struct list_head
*node
, *next
;
44 list_for_each_safe(node
, next
, &acpi_wakeup_device_list
) {
45 struct acpi_device
*dev
=
46 container_of(node
, struct acpi_device
, wakeup_list
);
48 if (!dev
->wakeup
.flags
.valid
49 || sleep_state
> (u32
) dev
->wakeup
.sleep_state
50 || !(device_may_wakeup(&dev
->dev
)
51 || dev
->wakeup
.prepare_count
))
54 if (device_may_wakeup(&dev
->dev
))
55 acpi_enable_wakeup_device_power(dev
, sleep_state
);
57 /* The wake-up power should have been enabled already. */
58 acpi_set_gpe_wake_mask(dev
->wakeup
.gpe_device
, dev
->wakeup
.gpe_number
,
64 * acpi_disable_wakeup_devices - Disable devices' wakeup capability.
65 * @sleep_state: ACPI system sleep state.
67 void acpi_disable_wakeup_devices(u8 sleep_state
)
69 struct list_head
*node
, *next
;
71 list_for_each_safe(node
, next
, &acpi_wakeup_device_list
) {
72 struct acpi_device
*dev
=
73 container_of(node
, struct acpi_device
, wakeup_list
);
75 if (!dev
->wakeup
.flags
.valid
76 || sleep_state
> (u32
) dev
->wakeup
.sleep_state
77 || !(device_may_wakeup(&dev
->dev
)
78 || dev
->wakeup
.prepare_count
))
81 acpi_set_gpe_wake_mask(dev
->wakeup
.gpe_device
, dev
->wakeup
.gpe_number
,
84 if (device_may_wakeup(&dev
->dev
))
85 acpi_disable_wakeup_device_power(dev
);
89 int __init
acpi_wakeup_device_init(void)
91 struct list_head
*node
, *next
;
93 mutex_lock(&acpi_device_lock
);
94 list_for_each_safe(node
, next
, &acpi_wakeup_device_list
) {
95 struct acpi_device
*dev
= container_of(node
,
98 if (device_can_wakeup(&dev
->dev
)) {
99 /* Button GPEs are supposed to be always enabled. */
100 acpi_enable_gpe(dev
->wakeup
.gpe_device
,
101 dev
->wakeup
.gpe_number
);
102 device_set_wakeup_enable(&dev
->dev
, true);
105 mutex_unlock(&acpi_device_lock
);
110 * acpi_register_wakeup_handler - Register wakeup handler
111 * @wake_irq: The IRQ through which the device may receive wakeups
112 * @wakeup: Wakeup-handler to call when the SCI has triggered a wakeup
113 * @context: Context to pass to the handler when calling it
115 * Drivers which may share an IRQ with the SCI can use this to register
116 * a handler which returns true when the device they are managing wants
117 * to trigger a wakeup.
119 int acpi_register_wakeup_handler(int wake_irq
, bool (*wakeup
)(void *context
),
122 struct acpi_wakeup_handler
*handler
;
125 * If the device is not sharing its IRQ with the SCI, there is no
126 * need to register the handler.
128 if (!acpi_sci_irq_valid() || wake_irq
!= acpi_sci_irq
)
131 handler
= kmalloc(sizeof(*handler
), GFP_KERNEL
);
135 handler
->wakeup
= wakeup
;
136 handler
->context
= context
;
138 mutex_lock(&acpi_wakeup_handler_mutex
);
139 list_add(&handler
->list_node
, &acpi_wakeup_handler_head
);
140 mutex_unlock(&acpi_wakeup_handler_mutex
);
144 EXPORT_SYMBOL_GPL(acpi_register_wakeup_handler
);
147 * acpi_unregister_wakeup_handler - Unregister wakeup handler
148 * @wakeup: Wakeup-handler passed to acpi_register_wakeup_handler()
149 * @context: Context passed to acpi_register_wakeup_handler()
151 void acpi_unregister_wakeup_handler(bool (*wakeup
)(void *context
),
154 struct acpi_wakeup_handler
*handler
;
156 mutex_lock(&acpi_wakeup_handler_mutex
);
157 list_for_each_entry(handler
, &acpi_wakeup_handler_head
, list_node
) {
158 if (handler
->wakeup
== wakeup
&& handler
->context
== context
) {
159 list_del(&handler
->list_node
);
164 mutex_unlock(&acpi_wakeup_handler_mutex
);
166 EXPORT_SYMBOL_GPL(acpi_unregister_wakeup_handler
);
168 bool acpi_check_wakeup_handlers(void)
170 struct acpi_wakeup_handler
*handler
;
172 /* No need to lock, nothing else is running when we're called. */
173 list_for_each_entry(handler
, &acpi_wakeup_handler_head
, list_node
) {
174 if (handler
->wakeup(handler
->context
))