acpi_apm.c revision 1.4 1 /* $NetBSD: acpi_apm.c,v 1.4 2006/08/06 15:47:51 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas and by Jared McNeill.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Autoconfiguration support for the Intel ACPI Component Architecture
41 * ACPI reference implementation.
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: acpi_apm.c,v 1.4 2006/08/06 15:47:51 christos Exp $");
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/device.h>
50 #include <sys/malloc.h>
51 #include <sys/kernel.h>
52 #include <sys/proc.h>
53 #include <sys/sysctl.h>
54 #include <sys/select.h>
55 #include <sys/envsys.h>
56 #include <dev/sysmon/sysmonvar.h>
57
58 #include <dev/acpi/acpica.h>
59 #include <dev/apm/apmvar.h>
60
61 static void acpiapm_disconnect(void *);
62 static void acpiapm_enable(void *, int);
63 static int acpiapm_set_powstate(void *, u_int, u_int);
64 static int acpiapm_get_powstat(void *, u_int, struct apm_power_info *);
65 static int acpiapm_get_event(void *, u_int *, u_int *);
66 static void acpiapm_cpu_busy(void *);
67 static void acpiapm_cpu_idle(void *);
68 static void acpiapm_get_capabilities(void *, u_int *, u_int *);
69
70 struct apm_accessops acpiapm_accessops = {
71 acpiapm_disconnect,
72 acpiapm_enable,
73 acpiapm_set_powstate,
74 acpiapm_get_powstat,
75 acpiapm_get_event,
76 acpiapm_cpu_busy,
77 acpiapm_cpu_idle,
78 acpiapm_get_capabilities,
79 };
80
81 #ifdef ACPI_APM_DEBUG
82 #define DPRINTF(a) uprintf a
83 #else
84 #define DPRINTF(a)
85 #endif
86
87 #ifndef ACPI_APM_DEFAULT_STANDBY_STATE
88 #define ACPI_APM_DEFAULT_STANDBY_STATE (1)
89 #endif
90 #ifndef ACPI_APM_DEFAULT_SUSPEND_STATE
91 #define ACPI_APM_DEFAULT_SUSPEND_STATE (3)
92 #endif
93 #define ACPI_APM_DEFAULT_CAP \
94 ((ACPI_APM_DEFAULT_STANDBY_STATE!=0 ? APM_GLOBAL_STANDBY : 0) | \
95 (ACPI_APM_DEFAULT_SUSPEND_STATE!=0 ? APM_GLOBAL_SUSPEND : 0))
96 #define ACPI_APM_STATE_MIN (0)
97 #define ACPI_APM_STATE_MAX (4)
98
99 /* It is assumed that there is only acpiapm instance. */
100 static int resumed = 0, capability_changed = 0;
101 static int standby_state = ACPI_APM_DEFAULT_STANDBY_STATE;
102 static int suspend_state = ACPI_APM_DEFAULT_SUSPEND_STATE;
103 static int capabilities = ACPI_APM_DEFAULT_CAP;
104 static int acpiapm_node = CTL_EOL, standby_node = CTL_EOL;
105
106 struct acpi_softc;
107 extern ACPI_STATUS acpi_enter_sleep_state(struct acpi_softc *, int);
108 static int acpiapm_match(struct device *, struct cfdata *, void *);
109 static void acpiapm_attach(struct device *, struct device *, void *);
110 static int sysctl_state(SYSCTLFN_PROTO);
111
112 CFATTACH_DECL(acpiapm, sizeof(struct apm_softc),
113 acpiapm_match, acpiapm_attach, NULL, NULL);
114
115 static int
116 /*ARGSUSED*/
117 acpiapm_match(struct device *parent, struct cfdata *match, void *aux)
118 {
119 return apm_match();
120 }
121
122 static void
123 /*ARGSUSED*/
124 acpiapm_attach(struct device *parent, struct device *self, void *aux)
125 {
126 struct apm_softc *sc = (struct apm_softc *)self;
127
128 sc->sc_ops = &acpiapm_accessops;
129 sc->sc_cookie = parent;
130 sc->sc_vers = 0x0102;
131 sc->sc_detail = 0;
132 sc->sc_hwflags = APM_F_DONT_RUN_HOOKS;
133 apm_attach(sc);
134 }
135
136 static int
137 get_state_value(int id)
138 {
139 const int states[] = {
140 ACPI_STATE_S0,
141 ACPI_STATE_S1,
142 ACPI_STATE_S2,
143 ACPI_STATE_S3,
144 ACPI_STATE_S4
145 };
146
147 if (id < ACPI_APM_STATE_MIN || id > ACPI_APM_STATE_MAX)
148 return ACPI_STATE_S0;
149
150 return states[id];
151 }
152
153 static int
154 sysctl_state(SYSCTLFN_ARGS)
155 {
156 int newstate, error, *ref, cap, oldcap;
157 struct sysctlnode node;
158
159 if (rnode->sysctl_num == standby_node) {
160 ref = &standby_state;
161 cap = APM_GLOBAL_STANDBY;
162 } else {
163 ref = &suspend_state;
164 cap = APM_GLOBAL_SUSPEND;
165 }
166
167 newstate = *ref;
168 node = *rnode;
169 node.sysctl_data = &newstate;
170 error = sysctl_lookup(SYSCTLFN_CALL(&node));
171 if (error || newp == NULL)
172 return error;
173
174 if (newstate < ACPI_APM_STATE_MIN || newstate > ACPI_APM_STATE_MAX)
175 return EINVAL;
176
177 *ref = newstate;
178 oldcap = capabilities;
179 capabilities = newstate != 0 ? oldcap | cap : oldcap & ~cap;
180 if ((capabilities ^ oldcap) != 0)
181 capability_changed = 1;
182
183 return 0;
184 }
185
186 SYSCTL_SETUP(sysctl_acpiapm_setup, "sysctl machdep.acpiapm subtree setup")
187 {
188 const struct sysctlnode *node;
189
190 if (sysctl_createv(clog, 0, NULL, NULL,
191 CTLFLAG_PERMANENT,
192 CTLTYPE_NODE, "machdep", NULL,
193 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL))
194 return;
195
196 if (sysctl_createv(clog, 0, NULL, &node,
197 CTLFLAG_PERMANENT,
198 CTLTYPE_NODE, "acpiapm", NULL,
199 NULL, 0, NULL, 0,
200 CTL_MACHDEP, CTL_CREATE, CTL_EOL))
201 return;
202 acpiapm_node = node->sysctl_num;
203
204 if (sysctl_createv(clog, 0, NULL, &node,
205 CTLFLAG_READWRITE,
206 CTLTYPE_INT, "standby", NULL,
207 &sysctl_state, 0, NULL, 0,
208 CTL_MACHDEP, acpiapm_node, CTL_CREATE, CTL_EOL))
209 return;
210 standby_node = node->sysctl_num;
211
212 if (sysctl_createv(clog, 0, NULL, NULL,
213 CTLFLAG_READWRITE,
214 CTLTYPE_INT, "suspend", NULL,
215 &sysctl_state, 0, NULL, 0,
216 CTL_MACHDEP, acpiapm_node, CTL_CREATE, CTL_EOL))
217 return;
218 }
219
220 /*****************************************************************************
221 * Minimalistic ACPI /dev/apm emulation support, for ACPI suspend
222 *****************************************************************************/
223
224 static void
225 /*ARGSUSED*/
226 acpiapm_disconnect(void *opaque)
227 {
228 return;
229 }
230
231 static void
232 /*ARGSUSED*/
233 acpiapm_enable(void *opaque, int onoff)
234 {
235 return;
236 }
237
238 static int
239 acpiapm_set_powstate(void *opaque, u_int devid, u_int powstat)
240 {
241 struct acpi_softc *sc = opaque;
242
243 if (devid != APM_DEV_ALLDEVS)
244 return APM_ERR_UNRECOG_DEV;
245
246 switch (powstat) {
247 case APM_SYS_READY:
248 break;
249 case APM_SYS_STANDBY:
250 acpi_enter_sleep_state(sc, get_state_value(standby_state));
251 resumed = 1;
252 break;
253 case APM_SYS_SUSPEND:
254 acpi_enter_sleep_state(sc, get_state_value(suspend_state));
255 resumed = 1;
256 break;
257 case APM_SYS_OFF:
258 break;
259 case APM_LASTREQ_INPROG:
260 break;
261 case APM_LASTREQ_REJECTED:
262 break;
263 }
264
265 return 0;
266 }
267
268 static int
269 /*ARGSUSED*/
270 acpiapm_get_powstat(void *opaque, u_int batteryid, struct apm_power_info *pinfo)
271 {
272 #define APM_BATT_FLAG_WATERMARK_MASK (APM_BATT_FLAG_CRITICAL | \
273 APM_BATT_FLAG_LOW | \
274 APM_BATT_FLAG_HIGH)
275 int i, curcap, lowcap, warncap, cap, descap, lastcap, discharge;
276 envsys_tre_data_t etds;
277 envsys_basic_info_t ebis;
278
279 curcap = lowcap = warncap = cap = descap = lastcap = discharge = -1;
280
281 (void)memset(pinfo, 0, sizeof(*pinfo));
282 pinfo->ac_state = APM_AC_UNKNOWN;
283 pinfo->minutes_valid = 0;
284 pinfo->minutes_left = 0xffff; /* unknown */
285 pinfo->batteryid = 0;
286 pinfo->nbattery = 1;
287 pinfo->battery_flags = 0;
288 pinfo->battery_state = APM_BATT_UNKNOWN; /* ignored */
289 pinfo->battery_life = APM_BATT_LIFE_UNKNOWN;
290
291 for (i = 0;; i++) {
292 const char *desc;
293 int data;
294 int flags;
295
296 ebis.sensor = i;
297 if (sysmonioctl_envsys(0, ENVSYS_GTREINFO, (void *)&ebis, 0,
298 NULL) || (ebis.validflags & ENVSYS_FVALID) == 0)
299 break;
300 etds.sensor = i;
301 if (sysmonioctl_envsys(0, ENVSYS_GTREDATA, (void *)&etds, 0,
302 NULL))
303 continue;
304 desc = ebis.desc;
305 flags = etds.validflags;
306 data = etds.cur.data_s;
307
308 DPRINTF(("%d %s %d %d\n", i, desc, data, flags));
309 if ((flags & ENVSYS_FCURVALID) == 0)
310 continue;
311 if (strstr(desc, " disconnected")) {
312 pinfo->ac_state = data ? APM_AC_OFF : APM_AC_ON;
313 } else if (strstr(desc, " present") && data == 0)
314 pinfo->battery_flags |= APM_BATT_FLAG_NO_SYSTEM_BATTERY;
315 else if (strstr(desc, " charging") && data)
316 pinfo->battery_flags |= APM_BATT_FLAG_CHARGING;
317 else if (strstr(desc, " discharging") && data)
318 pinfo->battery_flags &= ~APM_BATT_FLAG_CHARGING;
319 else if (strstr(desc, " warn cap"))
320 warncap = data / 1000;
321 else if (strstr(desc, " low cap"))
322 lowcap = data / 1000;
323 else if (strstr(desc, " last full cap"))
324 lastcap = data / 1000;
325 else if (strstr(desc, " design cap"))
326 descap = data / 1000;
327 else if (strstr(desc, " charge") &&
328 strstr(desc, " charge rate") == NULL)
329 cap = data / 1000;
330 else if (strstr(desc, " discharge rate"))
331 discharge = data / 1000;
332 }
333
334 if (cap != -1) {
335 if (warncap != -1 && cap < warncap)
336 pinfo->battery_flags |= APM_BATT_FLAG_CRITICAL;
337 else if (lowcap != -1) {
338 if (cap < lowcap)
339 pinfo->battery_flags |= APM_BATT_FLAG_LOW;
340 else
341 pinfo->battery_flags |= APM_BATT_FLAG_HIGH;
342 }
343 if (lastcap != -1 && lastcap != 0)
344 pinfo->battery_life = 100 * cap / lastcap;
345 else if (descap != -1 && descap != 0)
346 pinfo->battery_life = 100 * cap / descap;
347 }
348
349 if ((pinfo->battery_flags & APM_BATT_FLAG_CHARGING) == 0) {
350 /* discharging */
351 if (discharge != -1 && cap != -1 && discharge != 0)
352 pinfo->minutes_left = 60 * cap / discharge;
353 }
354 if ((pinfo->battery_flags & APM_BATT_FLAG_WATERMARK_MASK) == 0 &&
355 (pinfo->battery_flags & APM_BATT_FLAG_NO_SYSTEM_BATTERY) == 0) {
356 if (pinfo->ac_state == APM_AC_ON)
357 pinfo->battery_flags |= APM_BATT_FLAG_HIGH;
358 else
359 pinfo->battery_flags |= APM_BATT_FLAG_LOW;
360 }
361
362 DPRINTF(("%d %d %d %d %d %d\n", cap, warncap, lowcap, lastcap, descap,
363 discharge));
364 DPRINTF(("pinfo %d %d %d\n", pinfo->battery_flags,
365 pinfo->battery_life, pinfo->battery_life));
366 return 0;
367 }
368
369 static int
370 /*ARGSUSED*/
371 acpiapm_get_event(void *opaque, u_int *event_type, u_int *event_info)
372 {
373 if (capability_changed) {
374 capability_changed = 0;
375 *event_type = APM_CAP_CHANGE;
376 *event_info = 0;
377 return 0;
378 }
379 if (resumed) {
380 resumed = 0;
381 *event_type = APM_NORMAL_RESUME;
382 *event_info = 0;
383 return 0;
384 }
385
386 return APM_ERR_NOEVENTS;
387 }
388
389 static void
390 /*ARGSUSED*/
391 acpiapm_cpu_busy(void *opaque)
392 {
393 return;
394 }
395
396 static void
397 /*ARGSUSED*/
398 acpiapm_cpu_idle(void *opaque)
399 {
400 return;
401 }
402
403 static void
404 /*ARGSUSED*/
405 acpiapm_get_capabilities(void *opaque, u_int *numbatts, u_int *capflags)
406 {
407 *numbatts = 1;
408 *capflags = capabilities;
409 return;
410 }
411