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