acpi_apm.c revision 1.3.2.2 1 1.3.2.2 gdamore /* $NetBSD: acpi_apm.c,v 1.3.2.2 2006/07/13 17:49:17 gdamore Exp $ */
2 1.3.2.2 gdamore
3 1.3.2.2 gdamore /*-
4 1.3.2.2 gdamore * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.3.2.2 gdamore * All rights reserved.
6 1.3.2.2 gdamore *
7 1.3.2.2 gdamore * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 gdamore * by Christos Zoulas and by Jared McNeill.
9 1.3.2.2 gdamore *
10 1.3.2.2 gdamore * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 gdamore * modification, are permitted provided that the following conditions
12 1.3.2.2 gdamore * are met:
13 1.3.2.2 gdamore * 1. Redistributions of source code must retain the above copyright
14 1.3.2.2 gdamore * notice, this list of conditions and the following disclaimer.
15 1.3.2.2 gdamore * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.2.2 gdamore * notice, this list of conditions and the following disclaimer in the
17 1.3.2.2 gdamore * documentation and/or other materials provided with the distribution.
18 1.3.2.2 gdamore * 3. All advertising materials mentioning features or use of this software
19 1.3.2.2 gdamore * must display the following acknowledgement:
20 1.3.2.2 gdamore * This product includes software developed by the NetBSD
21 1.3.2.2 gdamore * Foundation, Inc. and its contributors.
22 1.3.2.2 gdamore * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3.2.2 gdamore * contributors may be used to endorse or promote products derived
24 1.3.2.2 gdamore * from this software without specific prior written permission.
25 1.3.2.2 gdamore *
26 1.3.2.2 gdamore * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3.2.2 gdamore * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3.2.2 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3.2.2 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3.2.2 gdamore * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3.2.2 gdamore * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3.2.2 gdamore * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3.2.2 gdamore * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3.2.2 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3.2.2 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3.2.2 gdamore * POSSIBILITY OF SUCH DAMAGE.
37 1.3.2.2 gdamore */
38 1.3.2.2 gdamore
39 1.3.2.2 gdamore /*
40 1.3.2.2 gdamore * Autoconfiguration support for the Intel ACPI Component Architecture
41 1.3.2.2 gdamore * ACPI reference implementation.
42 1.3.2.2 gdamore */
43 1.3.2.2 gdamore
44 1.3.2.2 gdamore #include <sys/cdefs.h>
45 1.3.2.2 gdamore __KERNEL_RCSID(0, "$NetBSD: acpi_apm.c,v 1.3.2.2 2006/07/13 17:49:17 gdamore Exp $");
46 1.3.2.2 gdamore
47 1.3.2.2 gdamore #include <sys/param.h>
48 1.3.2.2 gdamore #include <sys/systm.h>
49 1.3.2.2 gdamore #include <sys/device.h>
50 1.3.2.2 gdamore #include <sys/malloc.h>
51 1.3.2.2 gdamore #include <sys/kernel.h>
52 1.3.2.2 gdamore #include <sys/proc.h>
53 1.3.2.2 gdamore #include <sys/sysctl.h>
54 1.3.2.2 gdamore #include <sys/select.h>
55 1.3.2.2 gdamore #include <sys/envsys.h>
56 1.3.2.2 gdamore #include <dev/sysmon/sysmonvar.h>
57 1.3.2.2 gdamore
58 1.3.2.2 gdamore #include <dev/acpi/acpica.h>
59 1.3.2.2 gdamore #include <dev/apm/apmvar.h>
60 1.3.2.2 gdamore
61 1.3.2.2 gdamore static void acpiapm_disconnect(void *);
62 1.3.2.2 gdamore static void acpiapm_enable(void *, int);
63 1.3.2.2 gdamore static int acpiapm_set_powstate(void *, u_int, u_int);
64 1.3.2.2 gdamore static int acpiapm_get_powstat(void *, u_int, struct apm_power_info *);
65 1.3.2.2 gdamore static int acpiapm_get_event(void *, u_int *, u_int *);
66 1.3.2.2 gdamore static void acpiapm_cpu_busy(void *);
67 1.3.2.2 gdamore static void acpiapm_cpu_idle(void *);
68 1.3.2.2 gdamore static void acpiapm_get_capabilities(void *, u_int *, u_int *);
69 1.3.2.2 gdamore
70 1.3.2.2 gdamore struct apm_accessops acpiapm_accessops = {
71 1.3.2.2 gdamore acpiapm_disconnect,
72 1.3.2.2 gdamore acpiapm_enable,
73 1.3.2.2 gdamore acpiapm_set_powstate,
74 1.3.2.2 gdamore acpiapm_get_powstat,
75 1.3.2.2 gdamore acpiapm_get_event,
76 1.3.2.2 gdamore acpiapm_cpu_busy,
77 1.3.2.2 gdamore acpiapm_cpu_idle,
78 1.3.2.2 gdamore acpiapm_get_capabilities,
79 1.3.2.2 gdamore };
80 1.3.2.2 gdamore
81 1.3.2.2 gdamore #ifdef ACPI_APM_DEBUG
82 1.3.2.2 gdamore #define DPRINTF(a) uprintf a
83 1.3.2.2 gdamore #else
84 1.3.2.2 gdamore #define DPRINTF(a)
85 1.3.2.2 gdamore #endif
86 1.3.2.2 gdamore
87 1.3.2.2 gdamore struct acpi_softc;
88 1.3.2.2 gdamore extern ACPI_STATUS acpi_enter_sleep_state(struct acpi_softc *, int);
89 1.3.2.2 gdamore static int acpiapm_match(struct device *, struct cfdata *, void *);
90 1.3.2.2 gdamore static void acpiapm_attach(struct device *, struct device *, void *);
91 1.3.2.2 gdamore
92 1.3.2.2 gdamore CFATTACH_DECL(acpiapm, sizeof(struct apm_softc),
93 1.3.2.2 gdamore acpiapm_match, acpiapm_attach, NULL, NULL);
94 1.3.2.2 gdamore
95 1.3.2.2 gdamore static int
96 1.3.2.2 gdamore acpiapm_match(struct device *parent, struct cfdata *match, void *aux)
97 1.3.2.2 gdamore {
98 1.3.2.2 gdamore return apm_match();
99 1.3.2.2 gdamore }
100 1.3.2.2 gdamore
101 1.3.2.2 gdamore static void
102 1.3.2.2 gdamore acpiapm_attach(struct device *parent, struct device *self, void *aux)
103 1.3.2.2 gdamore {
104 1.3.2.2 gdamore struct apm_softc *sc = (struct apm_softc *)self;
105 1.3.2.2 gdamore
106 1.3.2.2 gdamore sc->sc_ops = &acpiapm_accessops;
107 1.3.2.2 gdamore sc->sc_cookie = parent;
108 1.3.2.2 gdamore sc->sc_vers = 0x0102;
109 1.3.2.2 gdamore sc->sc_detail = 0;
110 1.3.2.2 gdamore sc->sc_hwflags = APM_F_DONT_RUN_HOOKS;
111 1.3.2.2 gdamore apm_attach(sc);
112 1.3.2.2 gdamore }
113 1.3.2.2 gdamore
114 1.3.2.2 gdamore /*****************************************************************************
115 1.3.2.2 gdamore * Minimalistic ACPI /dev/apm emulation support, for ACPI suspend
116 1.3.2.2 gdamore *****************************************************************************/
117 1.3.2.2 gdamore
118 1.3.2.2 gdamore static void
119 1.3.2.2 gdamore acpiapm_disconnect(void *opaque)
120 1.3.2.2 gdamore {
121 1.3.2.2 gdamore return;
122 1.3.2.2 gdamore }
123 1.3.2.2 gdamore
124 1.3.2.2 gdamore static void
125 1.3.2.2 gdamore acpiapm_enable(void *opaque, int onoff)
126 1.3.2.2 gdamore {
127 1.3.2.2 gdamore return;
128 1.3.2.2 gdamore }
129 1.3.2.2 gdamore
130 1.3.2.2 gdamore static int
131 1.3.2.2 gdamore acpiapm_set_powstate(void *opaque, u_int devid, u_int powstat)
132 1.3.2.2 gdamore {
133 1.3.2.2 gdamore struct acpi_softc *sc = opaque;
134 1.3.2.2 gdamore
135 1.3.2.2 gdamore if (devid != APM_DEV_ALLDEVS)
136 1.3.2.2 gdamore return APM_ERR_UNRECOG_DEV;
137 1.3.2.2 gdamore
138 1.3.2.2 gdamore switch (powstat) {
139 1.3.2.2 gdamore case APM_SYS_READY:
140 1.3.2.2 gdamore break;
141 1.3.2.2 gdamore case APM_SYS_STANDBY:
142 1.3.2.2 gdamore acpi_enter_sleep_state(sc, ACPI_STATE_S1);
143 1.3.2.2 gdamore break;
144 1.3.2.2 gdamore case APM_SYS_SUSPEND:
145 1.3.2.2 gdamore acpi_enter_sleep_state(sc, ACPI_STATE_S3);
146 1.3.2.2 gdamore break;
147 1.3.2.2 gdamore case APM_SYS_OFF:
148 1.3.2.2 gdamore break;
149 1.3.2.2 gdamore case APM_LASTREQ_INPROG:
150 1.3.2.2 gdamore break;
151 1.3.2.2 gdamore case APM_LASTREQ_REJECTED:
152 1.3.2.2 gdamore break;
153 1.3.2.2 gdamore }
154 1.3.2.2 gdamore
155 1.3.2.2 gdamore return 0;
156 1.3.2.2 gdamore }
157 1.3.2.2 gdamore
158 1.3.2.2 gdamore static int
159 1.3.2.2 gdamore acpiapm_get_powstat(void *opaque, u_int batteryid, struct apm_power_info *pinfo)
160 1.3.2.2 gdamore {
161 1.3.2.2 gdamore int i, curcap, lowcap, warncap, cap, descap, lastcap, discharge;
162 1.3.2.2 gdamore envsys_tre_data_t etds;
163 1.3.2.2 gdamore envsys_basic_info_t ebis;
164 1.3.2.2 gdamore
165 1.3.2.2 gdamore curcap = lowcap = warncap = cap = descap = lastcap = discharge = -1;
166 1.3.2.2 gdamore
167 1.3.2.2 gdamore (void)memset(pinfo, 0, sizeof(*pinfo));
168 1.3.2.2 gdamore pinfo->ac_state = APM_AC_UNKNOWN;
169 1.3.2.2 gdamore pinfo->minutes_valid = 0;
170 1.3.2.2 gdamore pinfo->minutes_left = 0xffff;
171 1.3.2.2 gdamore pinfo->batteryid = 0;
172 1.3.2.2 gdamore pinfo->nbattery = 1;
173 1.3.2.2 gdamore pinfo->battery_flags = 0;
174 1.3.2.2 gdamore pinfo->battery_state = APM_BATT_UNKNOWN;
175 1.3.2.2 gdamore pinfo->battery_life = APM_BATT_LIFE_UNKNOWN;
176 1.3.2.2 gdamore
177 1.3.2.2 gdamore for (i = 0;; i++) {
178 1.3.2.2 gdamore const char *desc;
179 1.3.2.2 gdamore int data;
180 1.3.2.2 gdamore int flags;
181 1.3.2.2 gdamore
182 1.3.2.2 gdamore ebis.sensor = i;
183 1.3.2.2 gdamore if (sysmonioctl_envsys(0, ENVSYS_GTREINFO, (void *)&ebis, 0,
184 1.3.2.2 gdamore NULL) || (ebis.validflags & ENVSYS_FVALID) == 0)
185 1.3.2.2 gdamore break;
186 1.3.2.2 gdamore etds.sensor = i;
187 1.3.2.2 gdamore if (sysmonioctl_envsys(0, ENVSYS_GTREDATA, (void *)&etds, 0,
188 1.3.2.2 gdamore NULL))
189 1.3.2.2 gdamore continue;
190 1.3.2.2 gdamore desc = ebis.desc;
191 1.3.2.2 gdamore flags = etds.validflags;
192 1.3.2.2 gdamore data = etds.cur.data_s;
193 1.3.2.2 gdamore
194 1.3.2.2 gdamore DPRINTF(("%d %s %d %d\n", i, desc, data, flags));
195 1.3.2.2 gdamore if ((flags & ENVSYS_FCURVALID) == 0)
196 1.3.2.2 gdamore continue;
197 1.3.2.2 gdamore if (strstr(desc, " disconnected")) {
198 1.3.2.2 gdamore pinfo->ac_state = data ? APM_AC_OFF : APM_AC_ON;
199 1.3.2.2 gdamore break;
200 1.3.2.2 gdamore } else if (strstr(desc, " present") && data == 0) {
201 1.3.2.2 gdamore pinfo->battery_flags |= APM_BATT_FLAG_NO_SYSTEM_BATTERY;
202 1.3.2.2 gdamore pinfo->battery_state = APM_BATT_ABSENT;
203 1.3.2.2 gdamore } else if (strstr(desc, " charging") && data) {
204 1.3.2.2 gdamore pinfo->battery_flags |= APM_BATT_FLAG_CHARGING;
205 1.3.2.2 gdamore pinfo->battery_state = APM_BATT_CHARGING;
206 1.3.2.2 gdamore } else if (strstr(desc, " discharging") && data)
207 1.3.2.2 gdamore pinfo->battery_flags &= ~APM_BATT_FLAG_CHARGING;
208 1.3.2.2 gdamore else if (strstr(desc, " warn cap"))
209 1.3.2.2 gdamore warncap = data / 1000;
210 1.3.2.2 gdamore else if (strstr(desc, " low cap"))
211 1.3.2.2 gdamore lowcap = data / 1000;
212 1.3.2.2 gdamore else if (strstr(desc, " last full cap"))
213 1.3.2.2 gdamore lastcap = data / 1000;
214 1.3.2.2 gdamore else if (strstr(desc, " design cap"))
215 1.3.2.2 gdamore descap = data / 1000;
216 1.3.2.2 gdamore else if (strstr(desc, " charge") &&
217 1.3.2.2 gdamore strstr(desc, " charge rate") == NULL)
218 1.3.2.2 gdamore cap = data / 1000;
219 1.3.2.2 gdamore else if (strstr(desc, " discharge rate"))
220 1.3.2.2 gdamore discharge = data / 1000;
221 1.3.2.2 gdamore }
222 1.3.2.2 gdamore
223 1.3.2.2 gdamore if (cap != -1) {
224 1.3.2.2 gdamore if (warncap != -1 && cap < warncap) {
225 1.3.2.2 gdamore pinfo->battery_flags |= APM_BATT_FLAG_CRITICAL;
226 1.3.2.2 gdamore pinfo->battery_state = APM_BATT_CRITICAL;
227 1.3.2.2 gdamore } else if (lowcap != -1 && cap < lowcap) {
228 1.3.2.2 gdamore pinfo->battery_flags |= APM_BATT_FLAG_LOW;
229 1.3.2.2 gdamore pinfo->battery_state = APM_BATT_LOW;
230 1.3.2.2 gdamore }
231 1.3.2.2 gdamore if (lastcap != -1 && lastcap != 0)
232 1.3.2.2 gdamore pinfo->battery_life = 100 * cap / lastcap;
233 1.3.2.2 gdamore else if (descap != -1 && descap != 0)
234 1.3.2.2 gdamore pinfo->battery_life = 100 * cap / descap;
235 1.3.2.2 gdamore }
236 1.3.2.2 gdamore
237 1.3.2.2 gdamore if ((pinfo->battery_flags & APM_BATT_FLAG_CHARGING) == 0) {
238 1.3.2.2 gdamore if (discharge != -1 && cap != -1 && discharge != 0)
239 1.3.2.2 gdamore pinfo->minutes_left = 60 * cap / discharge;
240 1.3.2.2 gdamore if (pinfo->battery_state == APM_BATT_UNKNOWN)
241 1.3.2.2 gdamore pinfo->battery_state = pinfo->ac_state == APM_AC_ON ?
242 1.3.2.2 gdamore APM_BATT_HIGH : APM_BATT_LOW;
243 1.3.2.2 gdamore } else {
244 1.3.2.2 gdamore if (pinfo->battery_state == APM_BATT_UNKNOWN)
245 1.3.2.2 gdamore pinfo->battery_state = APM_BATT_HIGH;
246 1.3.2.2 gdamore }
247 1.3.2.2 gdamore DPRINTF(("%d %d %d %d %d %d\n", cap, warncap, lowcap, lastcap, descap,
248 1.3.2.2 gdamore discharge));
249 1.3.2.2 gdamore DPRINTF(("pinfo %d %d %d %d\n", pinfo->battery_flags,
250 1.3.2.2 gdamore pinfo->battery_state, pinfo->battery_life, pinfo->battery_life));
251 1.3.2.2 gdamore return 0;
252 1.3.2.2 gdamore }
253 1.3.2.2 gdamore
254 1.3.2.2 gdamore static int
255 1.3.2.2 gdamore acpiapm_get_event(void *opaque, u_int *event_type, u_int *event_info)
256 1.3.2.2 gdamore {
257 1.3.2.2 gdamore return APM_ERR_NOEVENTS;
258 1.3.2.2 gdamore }
259 1.3.2.2 gdamore
260 1.3.2.2 gdamore static void
261 1.3.2.2 gdamore acpiapm_cpu_busy(void *opaque)
262 1.3.2.2 gdamore {
263 1.3.2.2 gdamore return;
264 1.3.2.2 gdamore }
265 1.3.2.2 gdamore
266 1.3.2.2 gdamore static void
267 1.3.2.2 gdamore acpiapm_cpu_idle(void *opaque)
268 1.3.2.2 gdamore {
269 1.3.2.2 gdamore return;
270 1.3.2.2 gdamore }
271 1.3.2.2 gdamore
272 1.3.2.2 gdamore static void
273 1.3.2.2 gdamore acpiapm_get_capabilities(void *opaque, u_int *numbatts, u_int *capflags)
274 1.3.2.2 gdamore {
275 1.3.2.2 gdamore *numbatts = 1;
276 1.3.2.2 gdamore *capflags = APM_GLOBAL_STANDBY | APM_GLOBAL_SUSPEND;
277 1.3.2.2 gdamore return;
278 1.3.2.2 gdamore }
279