acpi_apm.c revision 1.6 1 /* $NetBSD: acpi_apm.c,v 1.6 2006/10/11 19:14:29 gdt 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.6 2006/10/11 19:14:29 gdt 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 int cap_valid, lastcap_valid, discharge_valid;
277 envsys_tre_data_t etds;
278 envsys_basic_info_t ebis;
279
280 /* Denote most variables as unitialized. */
281 curcap = lowcap = warncap = descap = -1;
282
283 /* Prepare to aggregate these two variables over all batteries. */
284 cap = lastcap = discharge = 0;
285 cap_valid = lastcap_valid = discharge_valid = 0;
286
287 (void)memset(pinfo, 0, sizeof(*pinfo));
288 pinfo->ac_state = APM_AC_UNKNOWN;
289 pinfo->minutes_valid = 0;
290 pinfo->minutes_left = 0xffff; /* unknown */
291 pinfo->batteryid = 0;
292 pinfo->nbattery = 0; /* to be incremented as batteries are found */
293 pinfo->battery_flags = 0;
294 pinfo->battery_state = APM_BATT_UNKNOWN; /* ignored */
295 pinfo->battery_life = APM_BATT_LIFE_UNKNOWN;
296
297 for (i = 0;; i++) {
298 const char *desc;
299 int data;
300 int flags;
301
302 ebis.sensor = i;
303 if (sysmonioctl_envsys(0, ENVSYS_GTREINFO, (void *)&ebis, 0,
304 NULL) || (ebis.validflags & ENVSYS_FVALID) == 0)
305 break;
306 etds.sensor = i;
307 if (sysmonioctl_envsys(0, ENVSYS_GTREDATA, (void *)&etds, 0,
308 NULL))
309 continue;
310 desc = ebis.desc;
311 flags = etds.validflags;
312 data = etds.cur.data_s;
313
314 DPRINTF(("%d %s %d %d\n", i, desc, data, flags));
315 if ((flags & ENVSYS_FCURVALID) == 0)
316 continue;
317 if (strstr(desc, " disconnected")) {
318 pinfo->ac_state = data ? APM_AC_OFF : APM_AC_ON;
319 } else if (strstr(desc, " present") && data == 0)
320 pinfo->battery_flags |= APM_BATT_FLAG_NO_SYSTEM_BATTERY;
321 else if (strstr(desc, " charging") && data)
322 pinfo->battery_flags |= APM_BATT_FLAG_CHARGING;
323 else if (strstr(desc, " discharging") && data)
324 pinfo->battery_flags &= ~APM_BATT_FLAG_CHARGING;
325 else if (strstr(desc, " warn cap"))
326 warncap = data / 1000;
327 else if (strstr(desc, " low cap"))
328 lowcap = data / 1000;
329 else if (strstr(desc, " last full cap")) {
330 lastcap += data / 1000;
331 lastcap_valid = 1;
332 }
333 else if (strstr(desc, " design cap"))
334 descap = data / 1000;
335 else if (strstr(desc, " charge") &&
336 strstr(desc, " charge rate") == NULL) {
337 cap += data / 1000;
338 cap_valid = 1;
339 pinfo->nbattery++;
340 }
341 else if (strstr(desc, " discharge rate")) {
342 discharge += data / 1000;
343 discharge_valid = 1;
344 }
345 }
346
347 if (cap_valid > 0) {
348 if (warncap != -1 && cap < warncap)
349 pinfo->battery_flags |= APM_BATT_FLAG_CRITICAL;
350 else if (lowcap != -1) {
351 if (cap < lowcap)
352 pinfo->battery_flags |= APM_BATT_FLAG_LOW;
353 else
354 pinfo->battery_flags |= APM_BATT_FLAG_HIGH;
355 }
356 if (lastcap_valid > 0 && lastcap != 0)
357 pinfo->battery_life = 100 * cap / lastcap;
358 else if (descap != -1 && descap != 0)
359 pinfo->battery_life = 100 * cap / descap;
360 }
361
362 if ((pinfo->battery_flags & APM_BATT_FLAG_CHARGING) == 0) {
363 /* discharging */
364 if (discharge != -1 && cap != -1 && discharge != 0)
365 pinfo->minutes_left = 60 * cap / discharge;
366 }
367 if ((pinfo->battery_flags & APM_BATT_FLAG_WATERMARK_MASK) == 0 &&
368 (pinfo->battery_flags & APM_BATT_FLAG_NO_SYSTEM_BATTERY) == 0) {
369 if (pinfo->ac_state == APM_AC_ON)
370 pinfo->battery_flags |= APM_BATT_FLAG_HIGH;
371 else
372 pinfo->battery_flags |= APM_BATT_FLAG_LOW;
373 }
374
375 DPRINTF(("%d %d %d %d %d %d\n", cap, warncap, lowcap, lastcap, descap,
376 discharge));
377 DPRINTF(("pinfo %d %d %d\n", pinfo->battery_flags,
378 pinfo->battery_life, pinfo->battery_life));
379 return 0;
380 }
381
382 static int
383 /*ARGSUSED*/
384 acpiapm_get_event(void *opaque, u_int *event_type, u_int *event_info)
385 {
386 if (capability_changed) {
387 capability_changed = 0;
388 *event_type = APM_CAP_CHANGE;
389 *event_info = 0;
390 return 0;
391 }
392 if (resumed) {
393 resumed = 0;
394 *event_type = APM_NORMAL_RESUME;
395 *event_info = 0;
396 return 0;
397 }
398
399 return APM_ERR_NOEVENTS;
400 }
401
402 static void
403 /*ARGSUSED*/
404 acpiapm_cpu_busy(void *opaque)
405 {
406 return;
407 }
408
409 static void
410 /*ARGSUSED*/
411 acpiapm_cpu_idle(void *opaque)
412 {
413 return;
414 }
415
416 static void
417 /*ARGSUSED*/
418 acpiapm_get_capabilities(void *opaque, u_int *numbatts, u_int *capflags)
419 {
420 *numbatts = 1;
421 *capflags = capabilities;
422 return;
423 }
424