acpi_wakedev.c revision 1.2.2.3 1 /* $NetBSD: acpi_wakedev.c,v 1.2.2.3 2010/03/11 15:03:22 yamt 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.2.2.3 2010/03/11 15:03:22 yamt Exp $");
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/kmem.h>
35 #include <sys/queue.h>
36 #include <sys/sysctl.h>
37 #include <sys/systm.h>
38
39 #include <dev/acpi/acpireg.h>
40 #include <dev/acpi/acpivar.h>
41 #include <dev/acpi/acpi_wakedev.h>
42
43 #define _COMPONENT ACPI_BUS_COMPONENT
44 ACPI_MODULE_NAME ("acpi_wakedev")
45
46 struct acpi_wakedev {
47 struct acpi_devnode *aw_node;
48 struct sysctllog *aw_sysctllog;
49 int aw_enabled;
50
51 TAILQ_ENTRY(acpi_wakedev) aw_list;
52 };
53
54 struct acpi_wakedev;
55 static int acpi_wakedev_node = -1;
56
57 static TAILQ_HEAD(, acpi_wakedev) acpi_wakedevlist =
58 TAILQ_HEAD_INITIALIZER(acpi_wakedevlist);
59
60 static const char * const acpi_wakedev_default[] = {
61 "PNP0C0C", /* power button */
62 "PNP0C0E", /* sleep button */
63 "PNP0C0D", /* lid switch */
64 "PNP03??", /* PC KBD port */
65 NULL,
66 };
67
68 static void acpi_wakedev_sysctl_add(struct acpi_wakedev *);
69 static bool acpi_wakedev_add(struct acpi_softc *, struct acpi_devnode *);
70 static void acpi_wakedev_print(struct acpi_wakedev *);
71 static void acpi_wakedev_prepare(struct acpi_devnode *, int, int);
72
73 SYSCTL_SETUP(sysctl_acpi_wakedev_setup, "sysctl hw.wake subtree setup")
74 {
75 const struct sysctlnode *rnode;
76 int err;
77
78 err = sysctl_createv(NULL, 0, NULL, NULL,
79 CTLFLAG_PERMANENT,
80 CTLTYPE_NODE, "hw", NULL,
81 NULL, 0, NULL, 0, CTL_HW, CTL_EOL);
82 if (err)
83 return;
84 err = sysctl_createv(NULL, 0, NULL, &rnode,
85 CTLFLAG_PERMANENT,
86 CTLTYPE_NODE, "wake", NULL,
87 NULL, 0, NULL, 0,
88 CTL_HW, CTL_CREATE, CTL_EOL);
89 if (err)
90 return;
91 acpi_wakedev_node = rnode->sysctl_num;
92 }
93
94 static void
95 acpi_wakedev_sysctl_add(struct acpi_wakedev *aw)
96 {
97 int err;
98
99 if (acpi_wakedev_node == -1)
100 return;
101
102 err = sysctl_createv(&aw->aw_sysctllog, 0, NULL, NULL,
103 CTLFLAG_READWRITE, CTLTYPE_INT, aw->aw_node->ad_name,
104 NULL, NULL, 0, &aw->aw_enabled, 0,
105 CTL_HW, acpi_wakedev_node, CTL_CREATE, CTL_EOL);
106 if (err)
107 aprint_error("%s: sysctl_createv(hw.wake.%s) failed (%d)\n",
108 __func__, aw->aw_node->ad_name, err);
109 }
110
111 static bool
112 acpi_wakedev_add(struct acpi_softc *sc, struct acpi_devnode *ad)
113 {
114 struct acpi_wakedev *aw;
115 ACPI_HANDLE hdl;
116
117 if (ACPI_FAILURE(AcpiGetHandle(ad->ad_handle, "_PRW", &hdl)))
118 return false;
119
120 aw = kmem_alloc(sizeof(*aw), KM_SLEEP);
121 if (aw == NULL) {
122 aprint_error("%s: kmem_alloc failed\n", __func__);
123 return false;
124 }
125 aw->aw_node = ad;
126 aw->aw_sysctllog = NULL;
127 if (acpi_match_hid(ad->ad_devinfo, acpi_wakedev_default))
128 aw->aw_enabled = 1;
129 else
130 aw->aw_enabled = 0;
131
132 TAILQ_INSERT_TAIL(&acpi_wakedevlist, aw, aw_list);
133
134 acpi_wakedev_sysctl_add(aw);
135
136 return true;
137 }
138
139 static void
140 acpi_wakedev_print(struct acpi_wakedev *aw)
141 {
142 aprint_debug(" %s", aw->aw_node->ad_name);
143 }
144
145 int
146 acpi_wakedev_scan(struct acpi_softc *sc)
147 {
148 struct acpi_devnode *ad;
149 struct acpi_wakedev *aw;
150 ACPI_DEVICE_INFO *di;
151 int count = 0;
152
153 #define ACPI_STA_DEV_VALID \
154 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|ACPI_STA_DEV_OK)
155
156 SIMPLEQ_FOREACH(ad, &sc->sc_devnodes, ad_list) {
157
158 di = ad->ad_devinfo;
159
160 if (di->Type != ACPI_TYPE_DEVICE)
161 continue;
162
163 if ((di->Valid & ACPI_VALID_STA) != 0 &&
164 (di->CurrentStatus & ACPI_STA_DEV_VALID) !=
165 ACPI_STA_DEV_VALID)
166 continue;
167
168 if (acpi_wakedev_add(sc, ad) == true)
169 ++count;
170 }
171
172 #undef ACPI_STA_DEV_VALID
173
174 if (count == 0)
175 return 0;
176
177 aprint_debug_dev(sc->sc_dev, "wakeup devices:");
178 TAILQ_FOREACH(aw, &acpi_wakedevlist, aw_list)
179 acpi_wakedev_print(aw);
180 aprint_debug("\n");
181
182 return count;
183 }
184
185 void
186 acpi_wakedev_commit(struct acpi_softc *sc, int state)
187 {
188 struct acpi_wakedev *aw;
189
190 /*
191 * As noted in ACPI 3.0 (p. 243), preparing
192 * a device for wakeup is a two-step process:
193 *
194 * 1. Enable all power resources in _PRW.
195 *
196 * 2. If present, execute _DSW/_PSW method.
197 *
198 * XXX: The first one is yet to be implemented.
199 */
200 TAILQ_FOREACH(aw, &acpi_wakedevlist, aw_list) {
201
202 if (aw->aw_enabled) {
203 aprint_debug_dev(sc->sc_dev, "set wake GPE (%s)\n",
204 aw->aw_node->ad_name);
205 acpi_set_wake_gpe(aw->aw_node->ad_handle);
206 } else
207 acpi_clear_wake_gpe(aw->aw_node->ad_handle);
208
209 acpi_wakedev_prepare(aw->aw_node, aw->aw_enabled, state);
210 }
211 }
212
213 static void
214 acpi_wakedev_prepare(struct acpi_devnode *ad, int enable, int state)
215 {
216 ACPI_OBJECT_LIST arg;
217 ACPI_OBJECT obj[3];
218 ACPI_STATUS rv;
219
220 /*
221 * First try to call the Device Sleep Wake control method, _DSW.
222 * Only if this is not available, resort to to the Power State
223 * Wake control method, _PSW, which was deprecated in ACPI 3.0.
224 *
225 * The arguments to these methods are as follows:
226 *
227 * arg0 arg1 arg2
228 * ---- ---- ----
229 * _PSW 0: disable
230 * 1: enable
231 *
232 * _DSW 0: disable 0: S0 0: D0
233 * 1: enable 1: S1 1: D0 or D1
234 * 2: D0, D1, or D2
235 * x: Sx 3: D0, D1, D2 or D3
236 */
237 arg.Count = 3;
238 arg.Pointer = obj;
239
240 obj[0].Integer.Value = enable;
241 obj[1].Integer.Value = state;
242 obj[2].Integer.Value = 3;
243
244 obj[0].Type = obj[1].Type = obj[2].Type = ACPI_TYPE_INTEGER;
245
246 rv = AcpiEvaluateObject(ad->ad_handle, "_DSW", &arg, NULL);
247
248 if (ACPI_SUCCESS(rv))
249 return;
250
251 if (rv != AE_NOT_FOUND)
252 goto fail;
253
254 rv = acpi_eval_set_integer(ad->ad_handle, "_PSW", enable);
255
256 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
257 goto fail;
258
259 return;
260
261 fail:
262 aprint_error_dev(ad->ad_device, "failed to evaluate wake "
263 "control method: %s\n", AcpiFormatException(rv));
264 }
265