acpi_wakedev.c revision 1.2.6.2 1 1.2.6.1 uebayasi /* $NetBSD: acpi_wakedev.c,v 1.2.6.2 2010/08/17 06:46:01 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.2 2010/08/17 06:46:01 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.1 uebayasi static const struct sysctlnode *rnode = NULL;
54 1.2.6.1 uebayasi
55 1.2.6.2 uebayasi static void acpi_wakedev_method(struct acpi_devnode *, int, int);
56 1.2.6.2 uebayasi static void acpi_wakedev_gpe(struct acpi_devnode *, int, int);
57 1.2.6.2 uebayasi static void acpi_wakedev_power(struct acpi_devnode *, ACPI_OBJECT *);
58 1.2.6.1 uebayasi
59 1.2.6.1 uebayasi SYSCTL_SETUP(sysctl_acpi_wakedev_setup, "sysctl hw.acpi.wake subtree setup")
60 1.1 jmcneill {
61 1.1 jmcneill int err;
62 1.1 jmcneill
63 1.1 jmcneill err = sysctl_createv(NULL, 0, NULL, &rnode,
64 1.2.6.1 uebayasi CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
65 1.2.6.1 uebayasi NULL, NULL, 0, NULL, 0,
66 1.2.6.1 uebayasi CTL_HW, CTL_EOL);
67 1.2.6.1 uebayasi
68 1.2.6.1 uebayasi if (err != 0)
69 1.2.6.1 uebayasi goto fail;
70 1.2.6.1 uebayasi
71 1.2.6.1 uebayasi err = sysctl_createv(NULL, 0, &rnode, &rnode,
72 1.2.6.1 uebayasi CTLFLAG_PERMANENT, CTLTYPE_NODE, "acpi",
73 1.2.6.1 uebayasi NULL, NULL, 0, NULL, 0,
74 1.2.6.1 uebayasi CTL_CREATE, CTL_EOL);
75 1.2.6.1 uebayasi
76 1.2.6.1 uebayasi if (err != 0)
77 1.2.6.1 uebayasi goto fail;
78 1.2.6.1 uebayasi
79 1.2.6.1 uebayasi err = sysctl_createv(NULL, 0, &rnode, &rnode,
80 1.2.6.1 uebayasi CTLFLAG_PERMANENT, CTLTYPE_NODE,
81 1.2.6.1 uebayasi "wake", SYSCTL_DESCR("ACPI device wake-up"),
82 1.1 jmcneill NULL, 0, NULL, 0,
83 1.2.6.1 uebayasi CTL_CREATE, CTL_EOL);
84 1.2.6.1 uebayasi
85 1.2.6.1 uebayasi if (err != 0)
86 1.2.6.1 uebayasi goto fail;
87 1.2.6.1 uebayasi
88 1.2.6.1 uebayasi return;
89 1.2.6.1 uebayasi
90 1.2.6.1 uebayasi fail:
91 1.2.6.1 uebayasi rnode = NULL;
92 1.1 jmcneill }
93 1.1 jmcneill
94 1.2.6.1 uebayasi void
95 1.2.6.1 uebayasi acpi_wakedev_add(struct acpi_devnode *ad)
96 1.1 jmcneill {
97 1.1 jmcneill int err;
98 1.1 jmcneill
99 1.2.6.1 uebayasi KASSERT(ad != NULL && ad->ad_root != NULL);
100 1.2.6.1 uebayasi KASSERT((ad->ad_flags & ACPI_DEVICE_WAKEUP) != 0);
101 1.2.6.1 uebayasi
102 1.2.6.1 uebayasi ad->ad_wake = 0;
103 1.2.6.1 uebayasi
104 1.2.6.1 uebayasi if (acpi_match_hid(ad->ad_devinfo, acpi_wakedev_default))
105 1.2.6.1 uebayasi ad->ad_wake = 1;
106 1.2.6.1 uebayasi
107 1.2.6.1 uebayasi if (rnode == NULL)
108 1.1 jmcneill return;
109 1.1 jmcneill
110 1.2.6.1 uebayasi err = sysctl_createv(NULL, 0, &rnode, NULL,
111 1.2.6.1 uebayasi CTLFLAG_READWRITE, CTLTYPE_BOOL, ad->ad_name,
112 1.2.6.1 uebayasi NULL, NULL, 0, &ad->ad_wake, 0,
113 1.2.6.1 uebayasi CTL_CREATE, CTL_EOL);
114 1.2.6.1 uebayasi
115 1.2.6.1 uebayasi if (err != 0)
116 1.2.6.1 uebayasi aprint_error_dev(ad->ad_root, "sysctl_createv"
117 1.2.6.1 uebayasi "(hw.acpi.wake.%s) failed (err %d)\n", ad->ad_name, err);
118 1.1 jmcneill }
119 1.1 jmcneill
120 1.2.6.1 uebayasi void
121 1.2.6.1 uebayasi acpi_wakedev_commit(struct acpi_softc *sc, int state)
122 1.1 jmcneill {
123 1.2.6.1 uebayasi struct acpi_devnode *ad;
124 1.2.6.1 uebayasi
125 1.2.6.1 uebayasi /*
126 1.2.6.2 uebayasi * To prepare a device for wakeup:
127 1.2.6.1 uebayasi *
128 1.2.6.2 uebayasi * 1. Set appropriate GPEs.
129 1.2.6.1 uebayasi *
130 1.2.6.2 uebayasi * 2. Enable all power resources in _PRW.
131 1.2.6.1 uebayasi *
132 1.2.6.2 uebayasi * 3. If present, execute _DSW/_PSW method.
133 1.2.6.1 uebayasi */
134 1.2.6.1 uebayasi SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
135 1.1 jmcneill
136 1.2.6.1 uebayasi if ((ad->ad_flags & ACPI_DEVICE_WAKEUP) == 0)
137 1.2.6.1 uebayasi continue;
138 1.1 jmcneill
139 1.2.6.2 uebayasi acpi_wakedev_gpe(ad, ad->ad_wake, state);
140 1.2.6.2 uebayasi acpi_wakedev_method(ad, ad->ad_wake, state);
141 1.1 jmcneill }
142 1.2.6.1 uebayasi }
143 1.2.6.1 uebayasi
144 1.2.6.1 uebayasi static void
145 1.2.6.2 uebayasi acpi_wakedev_method(struct acpi_devnode *ad, int enable, int state)
146 1.2.6.1 uebayasi {
147 1.2.6.1 uebayasi ACPI_OBJECT_LIST arg;
148 1.2.6.1 uebayasi ACPI_OBJECT obj[3];
149 1.2.6.1 uebayasi ACPI_STATUS rv;
150 1.2.6.1 uebayasi
151 1.2.6.1 uebayasi /*
152 1.2.6.1 uebayasi * First try to call the Device Sleep Wake control method, _DSW.
153 1.2.6.1 uebayasi * Only if this is not available, resort to to the Power State
154 1.2.6.1 uebayasi * Wake control method, _PSW, which was deprecated in ACPI 3.0.
155 1.2.6.1 uebayasi *
156 1.2.6.1 uebayasi * The arguments to these methods are as follows:
157 1.2.6.1 uebayasi *
158 1.2.6.1 uebayasi * arg0 arg1 arg2
159 1.2.6.1 uebayasi * ---- ---- ----
160 1.2.6.1 uebayasi * _PSW 0: disable
161 1.2.6.1 uebayasi * 1: enable
162 1.2.6.1 uebayasi *
163 1.2.6.1 uebayasi * _DSW 0: disable 0: S0 0: D0
164 1.2.6.1 uebayasi * 1: enable 1: S1 1: D0 or D1
165 1.2.6.1 uebayasi * 2: D0, D1, or D2
166 1.2.6.1 uebayasi * x: Sx 3: D0, D1, D2 or D3
167 1.2.6.1 uebayasi */
168 1.2.6.1 uebayasi arg.Count = 3;
169 1.2.6.1 uebayasi arg.Pointer = obj;
170 1.2.6.1 uebayasi
171 1.2.6.1 uebayasi obj[0].Integer.Value = enable;
172 1.2.6.1 uebayasi obj[1].Integer.Value = state;
173 1.2.6.2 uebayasi obj[2].Integer.Value = ACPI_STATE_D0;
174 1.1 jmcneill
175 1.2.6.1 uebayasi obj[0].Type = obj[1].Type = obj[2].Type = ACPI_TYPE_INTEGER;
176 1.1 jmcneill
177 1.2.6.1 uebayasi rv = AcpiEvaluateObject(ad->ad_handle, "_DSW", &arg, NULL);
178 1.1 jmcneill
179 1.2.6.1 uebayasi if (ACPI_SUCCESS(rv))
180 1.2.6.1 uebayasi return;
181 1.2.6.1 uebayasi
182 1.2.6.1 uebayasi if (rv != AE_NOT_FOUND)
183 1.2.6.1 uebayasi goto fail;
184 1.2.6.1 uebayasi
185 1.2.6.1 uebayasi rv = acpi_eval_set_integer(ad->ad_handle, "_PSW", enable);
186 1.2.6.1 uebayasi
187 1.2.6.1 uebayasi if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
188 1.2.6.1 uebayasi goto fail;
189 1.2.6.1 uebayasi
190 1.2.6.1 uebayasi return;
191 1.2.6.1 uebayasi
192 1.2.6.1 uebayasi fail:
193 1.2.6.1 uebayasi aprint_error_dev(ad->ad_root, "failed to evaluate wake "
194 1.2.6.1 uebayasi "control method: %s\n", AcpiFormatException(rv));
195 1.1 jmcneill }
196 1.1 jmcneill
197 1.1 jmcneill static void
198 1.2.6.2 uebayasi acpi_wakedev_gpe(struct acpi_devnode *ad, int enable, int state)
199 1.1 jmcneill {
200 1.2.6.1 uebayasi ACPI_OBJECT *elm, *obj;
201 1.2.6.2 uebayasi ACPI_HANDLE hdl = NULL;
202 1.2.6.1 uebayasi ACPI_INTEGER val;
203 1.2.6.1 uebayasi ACPI_BUFFER buf;
204 1.2.6.1 uebayasi ACPI_STATUS rv;
205 1.1 jmcneill
206 1.2.6.2 uebayasi rv = acpi_eval_struct(ad->ad_handle, "_PRW", &buf);
207 1.1 jmcneill
208 1.2.6.1 uebayasi if (ACPI_FAILURE(rv))
209 1.2.6.1 uebayasi return;
210 1.1 jmcneill
211 1.2.6.1 uebayasi obj = buf.Pointer;
212 1.2.6.1 uebayasi
213 1.2.6.1 uebayasi if (obj->Type != ACPI_TYPE_PACKAGE || obj->Package.Count < 2)
214 1.2.6.1 uebayasi goto out;
215 1.1 jmcneill
216 1.2.6.1 uebayasi /*
217 1.2.6.1 uebayasi * As noted in ACPI 3.0 (section 7.2.10), the _PRW object is
218 1.2.6.1 uebayasi * a package in which the first element is either an integer
219 1.2.6.1 uebayasi * or again a package. In the latter case the package inside
220 1.2.6.1 uebayasi * the package element has two elements, a reference handle
221 1.2.6.1 uebayasi * and the GPE number.
222 1.2.6.1 uebayasi */
223 1.2.6.1 uebayasi elm = &obj->Package.Elements[0];
224 1.2.6.1 uebayasi
225 1.2.6.1 uebayasi switch (elm->Type) {
226 1.2.6.1 uebayasi
227 1.2.6.1 uebayasi case ACPI_TYPE_INTEGER:
228 1.2.6.1 uebayasi val = elm->Integer.Value;
229 1.2.6.1 uebayasi break;
230 1.2.6.1 uebayasi
231 1.2.6.1 uebayasi case ACPI_TYPE_PACKAGE:
232 1.2.6.1 uebayasi
233 1.2.6.1 uebayasi if (elm->Package.Count < 2)
234 1.2.6.1 uebayasi goto out;
235 1.2.6.1 uebayasi
236 1.2.6.1 uebayasi if (elm->Package.Elements[0].Type != ACPI_TYPE_LOCAL_REFERENCE)
237 1.2.6.1 uebayasi goto out;
238 1.2.6.1 uebayasi
239 1.2.6.1 uebayasi if (elm->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
240 1.2.6.1 uebayasi goto out;
241 1.2.6.1 uebayasi
242 1.2.6.2 uebayasi hdl = elm->Package.Elements[0].Reference.Handle;
243 1.2.6.1 uebayasi val = elm->Package.Elements[1].Integer.Value;
244 1.2.6.1 uebayasi break;
245 1.2.6.1 uebayasi
246 1.2.6.1 uebayasi default:
247 1.2.6.1 uebayasi goto out;
248 1.2.6.1 uebayasi }
249 1.2.6.1 uebayasi
250 1.2.6.2 uebayasi /*
251 1.2.6.2 uebayasi * The second element is an integer that contains the
252 1.2.6.2 uebayasi * lowest sleep state that can be entered while still
253 1.2.6.2 uebayasi * providing wake-up functionality. The rest of the
254 1.2.6.2 uebayasi * elements are references to power resources.
255 1.2.6.2 uebayasi */
256 1.2.6.2 uebayasi elm = &obj->Package.Elements[1];
257 1.2.6.2 uebayasi
258 1.2.6.2 uebayasi if (elm->Type != ACPI_TYPE_INTEGER)
259 1.2.6.2 uebayasi goto out;
260 1.2.6.2 uebayasi
261 1.2.6.2 uebayasi if (state > elm->Integer.Value)
262 1.2.6.2 uebayasi aprint_error_dev(ad->ad_root, "sleep state S%d "
263 1.2.6.2 uebayasi "loses wake for %s\n", state, ad->ad_name);
264 1.2.6.2 uebayasi
265 1.2.6.1 uebayasi /*
266 1.2.6.2 uebayasi * Turn on power resources.
267 1.2.6.1 uebayasi */
268 1.2.6.2 uebayasi if (enable != 0)
269 1.2.6.2 uebayasi acpi_wakedev_power(ad, obj);
270 1.2.6.2 uebayasi
271 1.2.6.2 uebayasi /*
272 1.2.6.2 uebayasi * Set both runtime and wake GPEs, but unset only wake GPEs.
273 1.2.6.2 uebayasi */
274 1.2.6.2 uebayasi if (enable != 0)
275 1.2.6.2 uebayasi (void)AcpiEnableGpe(hdl, val, ACPI_GPE_TYPE_WAKE_RUN);
276 1.2.6.2 uebayasi else
277 1.2.6.2 uebayasi (void)AcpiDisableGpe(hdl, val, ACPI_GPE_TYPE_WAKE);
278 1.2.6.1 uebayasi
279 1.2.6.1 uebayasi ACPI_DEBUG_PRINT((ACPI_DB_INFO, "wake GPE %s for %s\n",
280 1.2.6.1 uebayasi (enable != 0) ? "enabled" : "disabled", ad->ad_name));
281 1.2.6.1 uebayasi
282 1.2.6.1 uebayasi out:
283 1.2.6.1 uebayasi ACPI_FREE(buf.Pointer);
284 1.1 jmcneill }
285 1.2.6.2 uebayasi
286 1.2.6.2 uebayasi static void
287 1.2.6.2 uebayasi acpi_wakedev_power(struct acpi_devnode *ad, ACPI_OBJECT *obj)
288 1.2.6.2 uebayasi {
289 1.2.6.2 uebayasi ACPI_OBJECT *elm;
290 1.2.6.2 uebayasi ACPI_HANDLE hdl;
291 1.2.6.2 uebayasi ACPI_STATUS rv;
292 1.2.6.2 uebayasi uint32_t i, n;
293 1.2.6.2 uebayasi
294 1.2.6.2 uebayasi n = obj->Package.Count;
295 1.2.6.2 uebayasi
296 1.2.6.2 uebayasi if (n < 3)
297 1.2.6.2 uebayasi return;
298 1.2.6.2 uebayasi
299 1.2.6.2 uebayasi for (i = 2; i < n; i++) {
300 1.2.6.2 uebayasi
301 1.2.6.2 uebayasi elm = &obj->Package.Elements[i];
302 1.2.6.2 uebayasi rv = acpi_eval_reference_handle(elm, &hdl);
303 1.2.6.2 uebayasi
304 1.2.6.2 uebayasi if (ACPI_FAILURE(rv))
305 1.2.6.2 uebayasi continue;
306 1.2.6.2 uebayasi
307 1.2.6.2 uebayasi (void)acpi_power_res(hdl, ad->ad_handle, true);
308 1.2.6.2 uebayasi }
309 1.2.6.2 uebayasi }
310