acpi_apm.c revision 1.1 1 /* $NetBSD: acpi_apm.c,v 1.1 2006/07/08 20:23:53 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.1 2006/07/08 20:23:53 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 struct acpi_softc;
88 extern ACPI_STATUS acpi_enter_sleep_state(struct acpi_softc *, int);
89 static int acpiapm_match(struct device *, struct cfdata *, void *);
90 static void acpiapm_attach(struct device *, struct device *, void *);
91
92 CFATTACH_DECL(acpiapm, sizeof(struct apm_softc),
93 acpiapm_match, acpiapm_attach, NULL, NULL);
94
95 static int
96 acpiapm_match(struct device *parent, struct cfdata *match, void *aux)
97 {
98 return apm_match();
99 }
100
101 static void
102 acpiapm_attach(struct device *parent, struct device *self, void *aux)
103 {
104 struct apm_softc *sc = (struct apm_softc *)self;
105
106 sc->sc_ops = &acpiapm_accessops;
107 sc->sc_cookie = parent;
108 sc->sc_vers = 0x0102;
109 sc->sc_detail = 0;
110 sc->sc_hwflags = APM_F_DONT_RUN_HOOKS;
111 apm_attach(sc);
112 }
113
114 /*****************************************************************************
115 * Minimalistic ACPI /dev/apm emulation support, for ACPI suspend
116 *****************************************************************************/
117
118 static void
119 acpiapm_disconnect(void *opaque)
120 {
121 return;
122 }
123
124 static void
125 acpiapm_enable(void *opaque, int onoff)
126 {
127 return;
128 }
129
130 static int
131 acpiapm_set_powstate(void *opaque, u_int devid, u_int powstat)
132 {
133 struct acpi_softc *sc = opaque;
134
135 if (devid != APM_DEV_ALLDEVS)
136 return APM_ERR_UNRECOG_DEV;
137
138 switch (powstat) {
139 case APM_SYS_READY:
140 break;
141 case APM_SYS_STANDBY:
142 acpi_enter_sleep_state(sc, ACPI_STATE_S1);
143 break;
144 case APM_SYS_SUSPEND:
145 acpi_enter_sleep_state(sc, ACPI_STATE_S3);
146 break;
147 case APM_SYS_OFF:
148 break;
149 case APM_LASTREQ_INPROG:
150 break;
151 case APM_LASTREQ_REJECTED:
152 break;
153 }
154
155 return 0;
156 }
157
158 static int
159 acpiapm_get_powstat(void *opaque, u_int batteryid, struct apm_power_info *pinfo)
160 {
161 int i, curcap, lowcap, warncap, cap, descap, lastcap, discharge;
162 envsys_tre_data_t etds;
163 envsys_basic_info_t ebis;
164
165 curcap = lowcap = warncap = cap = descap = lastcap = discharge = -1;
166
167 (void)memset(pinfo, 0, sizeof(*pinfo));
168 pinfo->ac_state = APM_AC_UNKNOWN;
169 pinfo->minutes_valid = 0;
170 pinfo->minutes_left = 0xffff;
171 pinfo->batteryid = 0;
172 pinfo->nbattery = 1;
173 pinfo->battery_flags = 0;
174 pinfo->battery_state = APM_BATT_UNKNOWN;
175 pinfo->battery_life = APM_BATT_LIFE_UNKNOWN;
176
177 for (i = 0;; i++) {
178 const char *desc;
179 int data;
180 int flags;
181
182 ebis.sensor = i;
183 if (sysmonioctl_envsys(0, ENVSYS_GTREINFO, (void *)&ebis, 0,
184 NULL) || (ebis.validflags & ENVSYS_FVALID) == 0)
185 break;
186 etds.sensor = i;
187 if (sysmonioctl_envsys(0, ENVSYS_GTREDATA, (void *)&etds, 0,
188 NULL))
189 continue;
190 desc = ebis.desc;
191 flags = etds.validflags;
192 data = etds.cur.data_s;
193
194 DPRINTF(("%d %s %d %d\n", i, desc, data, flags));
195 if ((flags & ENVSYS_FCURVALID) == 0)
196 continue;
197 if (strstr(desc, " disconnected")) {
198 pinfo->ac_state = data ? APM_AC_OFF : APM_AC_ON;
199 break;
200 } else if (strstr(desc, " present") && data == 0) {
201 pinfo->battery_flags |= APM_BATT_FLAG_NO_SYSTEM_BATTERY;
202 pinfo->battery_state = APM_BATT_ABSENT;
203 } else if (strstr(desc, " charging") && data) {
204 pinfo->battery_flags |= APM_BATT_FLAG_CHARGING;
205 pinfo->battery_state = APM_BATT_CHARGING;
206 } else if (strstr(desc, " discharging") && data)
207 pinfo->battery_flags &= ~APM_BATT_FLAG_CHARGING;
208 else if (strstr(desc, " warn cap"))
209 warncap = data / 1000;
210 else if (strstr(desc, " low cap"))
211 lowcap = data / 1000;
212 else if (strstr(desc, " last full cap"))
213 lastcap = data / 1000;
214 else if (strstr(desc, " design cap"))
215 descap = data / 1000;
216 else if (strstr(desc, " charge") &&
217 strstr(desc, " charge rate") == NULL)
218 cap = data / 1000;
219 else if (strstr(desc, " discharge rate"))
220 discharge = data / 1000;
221 }
222
223 if (cap != -1) {
224 if (warncap != -1 && cap < warncap) {
225 pinfo->battery_flags |= APM_BATT_FLAG_CRITICAL;
226 pinfo->battery_state = APM_BATT_CRITICAL;
227 } else if (lowcap != -1 && cap < lowcap) {
228 pinfo->battery_flags |= APM_BATT_FLAG_LOW;
229 pinfo->battery_state = APM_BATT_LOW;
230 }
231 if (lastcap != -1)
232 pinfo->battery_life = 100 * cap / lastcap;
233 else if (descap != -1)
234 pinfo->battery_life = 100 * cap / descap;
235 }
236
237 if ((pinfo->battery_flags & APM_BATT_FLAG_CHARGING) == 0) {
238 if (discharge != -1 && cap != -1)
239 pinfo->minutes_left = 60 * cap / discharge;
240 if (pinfo->battery_state == APM_BATT_UNKNOWN)
241 pinfo->battery_state = pinfo->ac_state == APM_AC_ON ?
242 APM_BATT_HIGH : APM_BATT_LOW;
243 } else {
244 if (pinfo->battery_state == APM_BATT_UNKNOWN)
245 pinfo->battery_state = APM_BATT_HIGH;
246 }
247 DPRINTF(("%d %d %d %d %d %d\n", cap, warncap, lowcap, lastcap, descap,
248 discharge));
249 DPRINTF(("pinfo %d %d %d %d\n", pinfo->battery_flags,
250 pinfo->battery_state, pinfo->battery_life, pinfo->battery_life));
251 return 0;
252 }
253
254 static int
255 acpiapm_get_event(void *opaque, u_int *event_type, u_int *event_info)
256 {
257 return APM_ERR_NOEVENTS;
258 }
259
260 static void
261 acpiapm_cpu_busy(void *opaque)
262 {
263 return;
264 }
265
266 static void
267 acpiapm_cpu_idle(void *opaque)
268 {
269 return;
270 }
271
272 static void
273 acpiapm_get_capabilities(void *opaque, u_int *numbatts, u_int *capflags)
274 {
275 *numbatts = 1;
276 *capflags = APM_GLOBAL_STANDBY | APM_GLOBAL_SUSPEND;
277 return;
278 }
279