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