1
2
3
4
5
6
7#include <linux/init.h>
8#include <linux/acpi.h>
9#include <linux/kernel.h>
10#include <linux/types.h>
11
12#include "internal.h"
13#include "sleep.h"
14
15struct acpi_wakeup_handler {
16 struct list_head list_node;
17 bool (*wakeup)(void *context);
18 void *context;
19};
20
21static LIST_HEAD(acpi_wakeup_handler_head);
22static DEFINE_MUTEX(acpi_wakeup_handler_mutex);
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38void acpi_enable_wakeup_devices(u8 sleep_state)
39{
40 struct acpi_device *dev, *tmp;
41
42 list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
43 wakeup_list) {
44 if (!dev->wakeup.flags.valid
45 || sleep_state > (u32) dev->wakeup.sleep_state
46 || !(device_may_wakeup(&dev->dev)
47 || dev->wakeup.prepare_count))
48 continue;
49
50 if (device_may_wakeup(&dev->dev))
51 acpi_enable_wakeup_device_power(dev, sleep_state);
52
53
54 acpi_set_gpe_wake_mask(dev->wakeup.gpe_device, dev->wakeup.gpe_number,
55 ACPI_GPE_ENABLE);
56 }
57}
58
59
60
61
62
63void acpi_disable_wakeup_devices(u8 sleep_state)
64{
65 struct acpi_device *dev, *tmp;
66
67 list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
68 wakeup_list) {
69 if (!dev->wakeup.flags.valid
70 || sleep_state > (u32) dev->wakeup.sleep_state
71 || !(device_may_wakeup(&dev->dev)
72 || dev->wakeup.prepare_count))
73 continue;
74
75 acpi_set_gpe_wake_mask(dev->wakeup.gpe_device, dev->wakeup.gpe_number,
76 ACPI_GPE_DISABLE);
77
78 if (device_may_wakeup(&dev->dev))
79 acpi_disable_wakeup_device_power(dev);
80 }
81}
82
83int __init acpi_wakeup_device_init(void)
84{
85 struct acpi_device *dev, *tmp;
86
87 mutex_lock(&acpi_device_lock);
88 list_for_each_entry_safe(dev, tmp, &acpi_wakeup_device_list,
89 wakeup_list) {
90 if (device_can_wakeup(&dev->dev)) {
91
92 acpi_enable_gpe(dev->wakeup.gpe_device,
93 dev->wakeup.gpe_number);
94 device_set_wakeup_enable(&dev->dev, true);
95 }
96 }
97 mutex_unlock(&acpi_device_lock);
98 return 0;
99}
100
101
102
103
104
105
106
107
108
109
110
111int acpi_register_wakeup_handler(int wake_irq, bool (*wakeup)(void *context),
112 void *context)
113{
114 struct acpi_wakeup_handler *handler;
115
116
117
118
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}
136EXPORT_SYMBOL_GPL(acpi_register_wakeup_handler);
137
138
139
140
141
142
143void 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}
158EXPORT_SYMBOL_GPL(acpi_unregister_wakeup_handler);
159
160bool acpi_check_wakeup_handlers(void)
161{
162 struct acpi_wakeup_handler *handler;
163
164
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}
172