acpi_wakedev.c revision 1.2.6.3 1 1.2.6.1 uebayasi /* $NetBSD: acpi_wakedev.c,v 1.2.6.3 2010/10/22 07:21:53 uebayasi Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.2.6.1 uebayasi * Copyright (c) 2009, 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.2.6.1 uebayasi __KERNEL_RCSID(0, "$NetBSD: acpi_wakedev.c,v 1.2.6.3 2010/10/22 07:21:53 uebayasi Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/device.h>
34 1.1 jmcneill #include <sys/sysctl.h>
35 1.2.6.1 uebayasi #include <sys/systm.h>
36 1.1 jmcneill
37 1.1 jmcneill #include <dev/acpi/acpireg.h>
38 1.2.6.1 uebayasi #include <dev/acpi/acpivar.h>
39 1.2.6.2 uebayasi #include <dev/acpi/acpi_power.h>
40 1.1 jmcneill #include <dev/acpi/acpi_wakedev.h>
41 1.1 jmcneill
42 1.2.6.2 uebayasi #define _COMPONENT ACPI_BUS_COMPONENT
43 1.2.6.2 uebayasi ACPI_MODULE_NAME ("acpi_wakedev")
44 1.1 jmcneill
45 1.1 jmcneill static const char * const acpi_wakedev_default[] = {
46 1.1 jmcneill "PNP0C0C", /* power button */
47 1.1 jmcneill "PNP0C0E", /* sleep button */
48 1.1 jmcneill "PNP0C0D", /* lid switch */
49 1.1 jmcneill "PNP03??", /* PC KBD port */
50 1.1 jmcneill NULL,
51 1.1 jmcneill };
52 1.1 jmcneill
53 1.2.6.3 uebayasi static int32_t acpi_wakedev_acpinode = CTL_EOL;
54 1.2.6.3 uebayasi static int32_t acpi_wakedev_wakenode = CTL_EOL;
55 1.2.6.1 uebayasi
56 1.2.6.2 uebayasi static void acpi_wakedev_method(struct acpi_devnode *, int, int);
57 1.2.6.2 uebayasi static void acpi_wakedev_gpe(struct acpi_devnode *, int, int);
58 1.2.6.2 uebayasi static void acpi_wakedev_power(struct acpi_devnode *, ACPI_OBJECT *);
59 1.2.6.1 uebayasi
60 1.2.6.1 uebayasi SYSCTL_SETUP(sysctl_acpi_wakedev_setup, "sysctl hw.acpi.wake subtree setup")
61 1.1 jmcneill {
62 1.2.6.3 uebayasi const struct sysctlnode *rnode;
63 1.1 jmcneill int err;
64 1.1 jmcneill
65 1.1 jmcneill err = sysctl_createv(NULL, 0, NULL, &rnode,
66 1.2.6.1 uebayasi CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
67 1.2.6.1 uebayasi NULL, NULL, 0, NULL, 0,
68 1.2.6.1 uebayasi CTL_HW, CTL_EOL);
69 1.2.6.1 uebayasi
70 1.2.6.1 uebayasi if (err != 0)
71 1.2.6.3 uebayasi return;
72 1.2.6.1 uebayasi
73 1.2.6.1 uebayasi err = sysctl_createv(NULL, 0, &rnode, &rnode,
74 1.2.6.1 uebayasi CTLFLAG_PERMANENT, CTLTYPE_NODE, "acpi",
75 1.2.6.1 uebayasi NULL, NULL, 0, NULL, 0,
76 1.2.6.1 uebayasi CTL_CREATE, CTL_EOL);
77 1.2.6.1 uebayasi
78 1.2.6.1 uebayasi if (err != 0)
79 1.2.6.3 uebayasi return;
80 1.2.6.3 uebayasi
81 1.2.6.3 uebayasi acpi_wakedev_acpinode = rnode->sysctl_num;
82 1.2.6.1 uebayasi
83 1.2.6.1 uebayasi err = sysctl_createv(NULL, 0, &rnode, &rnode,
84 1.2.6.1 uebayasi CTLFLAG_PERMANENT, CTLTYPE_NODE,
85 1.2.6.1 uebayasi "wake", SYSCTL_DESCR("ACPI device wake-up"),
86 1.1 jmcneill NULL, 0, NULL, 0,
87 1.2.6.1 uebayasi CTL_CREATE, CTL_EOL);
88 1.2.6.1 uebayasi
89 1.2.6.1 uebayasi if (err != 0)
90 1.2.6.3 uebayasi return;
91 1.2.6.1 uebayasi
92 1.2.6.3 uebayasi acpi_wakedev_wakenode = rnode->sysctl_num;
93 1.1 jmcneill }
94 1.1 jmcneill
95 1.2.6.1 uebayasi void
96 1.2.6.1 uebayasi acpi_wakedev_add(struct acpi_devnode *ad)
97 1.1 jmcneill {
98 1.1 jmcneill int err;
99 1.1 jmcneill
100 1.2.6.1 uebayasi KASSERT(ad != NULL && ad->ad_root != NULL);
101 1.2.6.1 uebayasi KASSERT((ad->ad_flags & ACPI_DEVICE_WAKEUP) != 0);
102 1.2.6.1 uebayasi
103 1.2.6.1 uebayasi ad->ad_wake = 0;
104 1.2.6.1 uebayasi
105 1.2.6.1 uebayasi if (acpi_match_hid(ad->ad_devinfo, acpi_wakedev_default))
106 1.2.6.1 uebayasi ad->ad_wake = 1;
107 1.2.6.1 uebayasi
108 1.2.6.3 uebayasi if (acpi_wakedev_acpinode == CTL_EOL ||
109 1.2.6.3 uebayasi acpi_wakedev_wakenode == CTL_EOL)
110 1.1 jmcneill return;
111 1.1 jmcneill
112 1.2.6.3 uebayasi err = sysctl_createv(NULL, 0, NULL, NULL,
113 1.2.6.1 uebayasi CTLFLAG_READWRITE, CTLTYPE_BOOL, ad->ad_name,
114 1.2.6.1 uebayasi NULL, NULL, 0, &ad->ad_wake, 0,
115 1.2.6.3 uebayasi CTL_HW, acpi_wakedev_acpinode, acpi_wakedev_wakenode,
116 1.2.6.1 uebayasi CTL_CREATE, CTL_EOL);
117 1.2.6.1 uebayasi
118 1.2.6.1 uebayasi if (err != 0)
119 1.2.6.1 uebayasi aprint_error_dev(ad->ad_root, "sysctl_createv"
120 1.2.6.1 uebayasi "(hw.acpi.wake.%s) failed (err %d)\n", ad->ad_name, err);
121 1.1 jmcneill }
122 1.1 jmcneill
123 1.2.6.1 uebayasi void
124 1.2.6.1 uebayasi acpi_wakedev_commit(struct acpi_softc *sc, int state)
125 1.1 jmcneill {
126 1.2.6.1 uebayasi struct acpi_devnode *ad;
127 1.2.6.1 uebayasi
128 1.2.6.1 uebayasi /*
129 1.2.6.2 uebayasi * To prepare a device for wakeup:
130 1.2.6.1 uebayasi *
131 1.2.6.2 uebayasi * 1. Set appropriate GPEs.
132 1.2.6.1 uebayasi *
133 1.2.6.2 uebayasi * 2. Enable all power resources in _PRW.
134 1.2.6.1 uebayasi *
135 1.2.6.2 uebayasi * 3. If present, execute _DSW/_PSW method.
136 1.2.6.1 uebayasi */
137 1.2.6.1 uebayasi SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
138 1.1 jmcneill
139 1.2.6.1 uebayasi if ((ad->ad_flags & ACPI_DEVICE_WAKEUP) == 0)
140 1.2.6.1 uebayasi continue;
141 1.1 jmcneill
142 1.2.6.2 uebayasi acpi_wakedev_gpe(ad, ad->ad_wake, state);
143 1.2.6.2 uebayasi acpi_wakedev_method(ad, ad->ad_wake, state);
144 1.1 jmcneill }
145 1.2.6.1 uebayasi }
146 1.2.6.1 uebayasi
147 1.2.6.1 uebayasi static void
148 1.2.6.2 uebayasi acpi_wakedev_method(struct acpi_devnode *ad, int enable, int state)
149 1.2.6.1 uebayasi {
150 1.2.6.1 uebayasi ACPI_OBJECT_LIST arg;
151 1.2.6.1 uebayasi ACPI_OBJECT obj[3];
152 1.2.6.1 uebayasi ACPI_STATUS rv;
153 1.2.6.1 uebayasi
154 1.2.6.1 uebayasi /*
155 1.2.6.1 uebayasi * First try to call the Device Sleep Wake control method, _DSW.
156 1.2.6.1 uebayasi * Only if this is not available, resort to to the Power State
157 1.2.6.1 uebayasi * Wake control method, _PSW, which was deprecated in ACPI 3.0.
158 1.2.6.1 uebayasi *
159 1.2.6.1 uebayasi * The arguments to these methods are as follows:
160 1.2.6.1 uebayasi *
161 1.2.6.1 uebayasi * arg0 arg1 arg2
162 1.2.6.1 uebayasi * ---- ---- ----
163 1.2.6.1 uebayasi * _PSW 0: disable
164 1.2.6.1 uebayasi * 1: enable
165 1.2.6.1 uebayasi *
166 1.2.6.1 uebayasi * _DSW 0: disable 0: S0 0: D0
167 1.2.6.1 uebayasi * 1: enable 1: S1 1: D0 or D1
168 1.2.6.1 uebayasi * 2: D0, D1, or D2
169 1.2.6.1 uebayasi * x: Sx 3: D0, D1, D2 or D3
170 1.2.6.1 uebayasi */
171 1.2.6.1 uebayasi arg.Count = 3;
172 1.2.6.1 uebayasi arg.Pointer = obj;
173 1.2.6.1 uebayasi
174 1.2.6.1 uebayasi obj[0].Integer.Value = enable;
175 1.2.6.1 uebayasi obj[1].Integer.Value = state;
176 1.2.6.2 uebayasi obj[2].Integer.Value = ACPI_STATE_D0;
177 1.1 jmcneill
178 1.2.6.1 uebayasi obj[0].Type = obj[1].Type = obj[2].Type = ACPI_TYPE_INTEGER;
179 1.1 jmcneill
180 1.2.6.1 uebayasi rv = AcpiEvaluateObject(ad->ad_handle, "_DSW", &arg, NULL);
181 1.1 jmcneill
182 1.2.6.1 uebayasi if (ACPI_SUCCESS(rv))
183 1.2.6.1 uebayasi return;
184 1.2.6.1 uebayasi
185 1.2.6.1 uebayasi if (rv != AE_NOT_FOUND)
186 1.2.6.1 uebayasi goto fail;
187 1.2.6.1 uebayasi
188 1.2.6.1 uebayasi rv = acpi_eval_set_integer(ad->ad_handle, "_PSW", enable);
189 1.2.6.1 uebayasi
190 1.2.6.1 uebayasi if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
191 1.2.6.1 uebayasi goto fail;
192 1.2.6.1 uebayasi
193 1.2.6.1 uebayasi return;
194 1.2.6.1 uebayasi
195 1.2.6.1 uebayasi fail:
196 1.2.6.1 uebayasi aprint_error_dev(ad->ad_root, "failed to evaluate wake "
197 1.2.6.1 uebayasi "control method: %s\n", AcpiFormatException(rv));
198 1.1 jmcneill }
199 1.1 jmcneill
200 1.1 jmcneill static void
201 1.2.6.2 uebayasi acpi_wakedev_gpe(struct acpi_devnode *ad, int enable, int state)
202 1.1 jmcneill {
203 1.2.6.1 uebayasi ACPI_OBJECT *elm, *obj;
204 1.2.6.2 uebayasi ACPI_HANDLE hdl = NULL;
205 1.2.6.1 uebayasi ACPI_INTEGER val;
206 1.2.6.1 uebayasi ACPI_BUFFER buf;
207 1.2.6.1 uebayasi ACPI_STATUS rv;
208 1.1 jmcneill
209 1.2.6.2 uebayasi rv = acpi_eval_struct(ad->ad_handle, "_PRW", &buf);
210 1.1 jmcneill
211 1.2.6.1 uebayasi if (ACPI_FAILURE(rv))
212 1.2.6.1 uebayasi return;
213 1.1 jmcneill
214 1.2.6.1 uebayasi obj = buf.Pointer;
215 1.2.6.1 uebayasi
216 1.2.6.1 uebayasi if (obj->Type != ACPI_TYPE_PACKAGE || obj->Package.Count < 2)
217 1.2.6.1 uebayasi goto out;
218 1.1 jmcneill
219 1.2.6.1 uebayasi /*
220 1.2.6.1 uebayasi * As noted in ACPI 3.0 (section 7.2.10), the _PRW object is
221 1.2.6.1 uebayasi * a package in which the first element is either an integer
222 1.2.6.1 uebayasi * or again a package. In the latter case the package inside
223 1.2.6.1 uebayasi * the package element has two elements, a reference handle
224 1.2.6.1 uebayasi * and the GPE number.
225 1.2.6.1 uebayasi */
226 1.2.6.1 uebayasi elm = &obj->Package.Elements[0];
227 1.2.6.1 uebayasi
228 1.2.6.1 uebayasi switch (elm->Type) {
229 1.2.6.1 uebayasi
230 1.2.6.1 uebayasi case ACPI_TYPE_INTEGER:
231 1.2.6.1 uebayasi val = elm->Integer.Value;
232 1.2.6.1 uebayasi break;
233 1.2.6.1 uebayasi
234 1.2.6.1 uebayasi case ACPI_TYPE_PACKAGE:
235 1.2.6.1 uebayasi
236 1.2.6.1 uebayasi if (elm->Package.Count < 2)
237 1.2.6.1 uebayasi goto out;
238 1.2.6.1 uebayasi
239 1.2.6.1 uebayasi if (elm->Package.Elements[0].Type != ACPI_TYPE_LOCAL_REFERENCE)
240 1.2.6.1 uebayasi goto out;
241 1.2.6.1 uebayasi
242 1.2.6.1 uebayasi if (elm->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
243 1.2.6.1 uebayasi goto out;
244 1.2.6.1 uebayasi
245 1.2.6.2 uebayasi hdl = elm->Package.Elements[0].Reference.Handle;
246 1.2.6.1 uebayasi val = elm->Package.Elements[1].Integer.Value;
247 1.2.6.1 uebayasi break;
248 1.2.6.1 uebayasi
249 1.2.6.1 uebayasi default:
250 1.2.6.1 uebayasi goto out;
251 1.2.6.1 uebayasi }
252 1.2.6.1 uebayasi
253 1.2.6.2 uebayasi /*
254 1.2.6.2 uebayasi * The second element is an integer that contains the
255 1.2.6.2 uebayasi * lowest sleep state that can be entered while still
256 1.2.6.2 uebayasi * providing wake-up functionality. The rest of the
257 1.2.6.2 uebayasi * elements are references to power resources.
258 1.2.6.2 uebayasi */
259 1.2.6.2 uebayasi elm = &obj->Package.Elements[1];
260 1.2.6.2 uebayasi
261 1.2.6.2 uebayasi if (elm->Type != ACPI_TYPE_INTEGER)
262 1.2.6.2 uebayasi goto out;
263 1.2.6.2 uebayasi
264 1.2.6.2 uebayasi if (state > elm->Integer.Value)
265 1.2.6.2 uebayasi aprint_error_dev(ad->ad_root, "sleep state S%d "
266 1.2.6.2 uebayasi "loses wake for %s\n", state, ad->ad_name);
267 1.2.6.2 uebayasi
268 1.2.6.1 uebayasi /*
269 1.2.6.2 uebayasi * Turn on power resources.
270 1.2.6.1 uebayasi */
271 1.2.6.2 uebayasi if (enable != 0)
272 1.2.6.2 uebayasi acpi_wakedev_power(ad, obj);
273 1.2.6.2 uebayasi
274 1.2.6.2 uebayasi /*
275 1.2.6.2 uebayasi * Set both runtime and wake GPEs, but unset only wake GPEs.
276 1.2.6.2 uebayasi */
277 1.2.6.2 uebayasi if (enable != 0)
278 1.2.6.2 uebayasi (void)AcpiEnableGpe(hdl, val, ACPI_GPE_TYPE_WAKE_RUN);
279 1.2.6.2 uebayasi else
280 1.2.6.2 uebayasi (void)AcpiDisableGpe(hdl, val, ACPI_GPE_TYPE_WAKE);
281 1.2.6.1 uebayasi
282 1.2.6.1 uebayasi ACPI_DEBUG_PRINT((ACPI_DB_INFO, "wake GPE %s for %s\n",
283 1.2.6.1 uebayasi (enable != 0) ? "enabled" : "disabled", ad->ad_name));
284 1.2.6.1 uebayasi
285 1.2.6.1 uebayasi out:
286 1.2.6.1 uebayasi ACPI_FREE(buf.Pointer);
287 1.1 jmcneill }
288 1.2.6.2 uebayasi
289 1.2.6.2 uebayasi static void
290 1.2.6.2 uebayasi acpi_wakedev_power(struct acpi_devnode *ad, ACPI_OBJECT *obj)
291 1.2.6.2 uebayasi {
292 1.2.6.2 uebayasi ACPI_OBJECT *elm;
293 1.2.6.2 uebayasi ACPI_HANDLE hdl;
294 1.2.6.2 uebayasi ACPI_STATUS rv;
295 1.2.6.2 uebayasi uint32_t i, n;
296 1.2.6.2 uebayasi
297 1.2.6.2 uebayasi n = obj->Package.Count;
298 1.2.6.2 uebayasi
299 1.2.6.2 uebayasi if (n < 3)
300 1.2.6.2 uebayasi return;
301 1.2.6.2 uebayasi
302 1.2.6.2 uebayasi for (i = 2; i < n; i++) {
303 1.2.6.2 uebayasi
304 1.2.6.2 uebayasi elm = &obj->Package.Elements[i];
305 1.2.6.2 uebayasi rv = acpi_eval_reference_handle(elm, &hdl);
306 1.2.6.2 uebayasi
307 1.2.6.2 uebayasi if (ACPI_FAILURE(rv))
308 1.2.6.2 uebayasi continue;
309 1.2.6.2 uebayasi
310 1.2.6.2 uebayasi (void)acpi_power_res(hdl, ad->ad_handle, true);
311 1.2.6.2 uebayasi }
312 1.2.6.2 uebayasi }
313