acpi_apm.c revision 1.4 1 1.4 christos /* $NetBSD: acpi_apm.c,v 1.4 2006/08/06 15:47:51 christos 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.4 christos __KERNEL_RCSID(0, "$NetBSD: acpi_apm.c,v 1.4 2006/08/06 15:47:51 christos 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.4 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.4 christos #ifndef ACPI_APM_DEFAULT_STANDBY_STATE
88 1.4 christos #define ACPI_APM_DEFAULT_STANDBY_STATE (1)
89 1.4 christos #endif
90 1.4 christos #ifndef ACPI_APM_DEFAULT_SUSPEND_STATE
91 1.4 christos #define ACPI_APM_DEFAULT_SUSPEND_STATE (3)
92 1.4 christos #endif
93 1.4 christos #define ACPI_APM_DEFAULT_CAP \
94 1.4 christos ((ACPI_APM_DEFAULT_STANDBY_STATE!=0 ? APM_GLOBAL_STANDBY : 0) | \
95 1.4 christos (ACPI_APM_DEFAULT_SUSPEND_STATE!=0 ? APM_GLOBAL_SUSPEND : 0))
96 1.4 christos #define ACPI_APM_STATE_MIN (0)
97 1.4 christos #define ACPI_APM_STATE_MAX (4)
98 1.4 christos
99 1.4 christos /* It is assumed that there is only acpiapm instance. */
100 1.4 christos static int resumed = 0, capability_changed = 0;
101 1.4 christos static int standby_state = ACPI_APM_DEFAULT_STANDBY_STATE;
102 1.4 christos static int suspend_state = ACPI_APM_DEFAULT_SUSPEND_STATE;
103 1.4 christos static int capabilities = ACPI_APM_DEFAULT_CAP;
104 1.4 christos static int acpiapm_node = CTL_EOL, standby_node = CTL_EOL;
105 1.4 christos
106 1.1 christos struct acpi_softc;
107 1.1 christos extern ACPI_STATUS acpi_enter_sleep_state(struct acpi_softc *, int);
108 1.1 christos static int acpiapm_match(struct device *, struct cfdata *, void *);
109 1.1 christos static void acpiapm_attach(struct device *, struct device *, void *);
110 1.4 christos static int sysctl_state(SYSCTLFN_PROTO);
111 1.1 christos
112 1.1 christos CFATTACH_DECL(acpiapm, sizeof(struct apm_softc),
113 1.1 christos acpiapm_match, acpiapm_attach, NULL, NULL);
114 1.1 christos
115 1.1 christos static int
116 1.4 christos /*ARGSUSED*/
117 1.1 christos acpiapm_match(struct device *parent, struct cfdata *match, void *aux)
118 1.1 christos {
119 1.1 christos return apm_match();
120 1.1 christos }
121 1.1 christos
122 1.1 christos static void
123 1.4 christos /*ARGSUSED*/
124 1.1 christos acpiapm_attach(struct device *parent, struct device *self, void *aux)
125 1.1 christos {
126 1.1 christos struct apm_softc *sc = (struct apm_softc *)self;
127 1.1 christos
128 1.1 christos sc->sc_ops = &acpiapm_accessops;
129 1.1 christos sc->sc_cookie = parent;
130 1.1 christos sc->sc_vers = 0x0102;
131 1.1 christos sc->sc_detail = 0;
132 1.1 christos sc->sc_hwflags = APM_F_DONT_RUN_HOOKS;
133 1.1 christos apm_attach(sc);
134 1.1 christos }
135 1.1 christos
136 1.4 christos static int
137 1.4 christos get_state_value(int id)
138 1.4 christos {
139 1.4 christos const int states[] = {
140 1.4 christos ACPI_STATE_S0,
141 1.4 christos ACPI_STATE_S1,
142 1.4 christos ACPI_STATE_S2,
143 1.4 christos ACPI_STATE_S3,
144 1.4 christos ACPI_STATE_S4
145 1.4 christos };
146 1.4 christos
147 1.4 christos if (id < ACPI_APM_STATE_MIN || id > ACPI_APM_STATE_MAX)
148 1.4 christos return ACPI_STATE_S0;
149 1.4 christos
150 1.4 christos return states[id];
151 1.4 christos }
152 1.4 christos
153 1.4 christos static int
154 1.4 christos sysctl_state(SYSCTLFN_ARGS)
155 1.4 christos {
156 1.4 christos int newstate, error, *ref, cap, oldcap;
157 1.4 christos struct sysctlnode node;
158 1.4 christos
159 1.4 christos if (rnode->sysctl_num == standby_node) {
160 1.4 christos ref = &standby_state;
161 1.4 christos cap = APM_GLOBAL_STANDBY;
162 1.4 christos } else {
163 1.4 christos ref = &suspend_state;
164 1.4 christos cap = APM_GLOBAL_SUSPEND;
165 1.4 christos }
166 1.4 christos
167 1.4 christos newstate = *ref;
168 1.4 christos node = *rnode;
169 1.4 christos node.sysctl_data = &newstate;
170 1.4 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
171 1.4 christos if (error || newp == NULL)
172 1.4 christos return error;
173 1.4 christos
174 1.4 christos if (newstate < ACPI_APM_STATE_MIN || newstate > ACPI_APM_STATE_MAX)
175 1.4 christos return EINVAL;
176 1.4 christos
177 1.4 christos *ref = newstate;
178 1.4 christos oldcap = capabilities;
179 1.4 christos capabilities = newstate != 0 ? oldcap | cap : oldcap & ~cap;
180 1.4 christos if ((capabilities ^ oldcap) != 0)
181 1.4 christos capability_changed = 1;
182 1.4 christos
183 1.4 christos return 0;
184 1.4 christos }
185 1.4 christos
186 1.4 christos SYSCTL_SETUP(sysctl_acpiapm_setup, "sysctl machdep.acpiapm subtree setup")
187 1.4 christos {
188 1.4 christos const struct sysctlnode *node;
189 1.4 christos
190 1.4 christos if (sysctl_createv(clog, 0, NULL, NULL,
191 1.4 christos CTLFLAG_PERMANENT,
192 1.4 christos CTLTYPE_NODE, "machdep", NULL,
193 1.4 christos NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL))
194 1.4 christos return;
195 1.4 christos
196 1.4 christos if (sysctl_createv(clog, 0, NULL, &node,
197 1.4 christos CTLFLAG_PERMANENT,
198 1.4 christos CTLTYPE_NODE, "acpiapm", NULL,
199 1.4 christos NULL, 0, NULL, 0,
200 1.4 christos CTL_MACHDEP, CTL_CREATE, CTL_EOL))
201 1.4 christos return;
202 1.4 christos acpiapm_node = node->sysctl_num;
203 1.4 christos
204 1.4 christos if (sysctl_createv(clog, 0, NULL, &node,
205 1.4 christos CTLFLAG_READWRITE,
206 1.4 christos CTLTYPE_INT, "standby", NULL,
207 1.4 christos &sysctl_state, 0, NULL, 0,
208 1.4 christos CTL_MACHDEP, acpiapm_node, CTL_CREATE, CTL_EOL))
209 1.4 christos return;
210 1.4 christos standby_node = node->sysctl_num;
211 1.4 christos
212 1.4 christos if (sysctl_createv(clog, 0, NULL, NULL,
213 1.4 christos CTLFLAG_READWRITE,
214 1.4 christos CTLTYPE_INT, "suspend", NULL,
215 1.4 christos &sysctl_state, 0, NULL, 0,
216 1.4 christos CTL_MACHDEP, acpiapm_node, CTL_CREATE, CTL_EOL))
217 1.4 christos return;
218 1.4 christos }
219 1.4 christos
220 1.1 christos /*****************************************************************************
221 1.1 christos * Minimalistic ACPI /dev/apm emulation support, for ACPI suspend
222 1.1 christos *****************************************************************************/
223 1.1 christos
224 1.1 christos static void
225 1.4 christos /*ARGSUSED*/
226 1.1 christos acpiapm_disconnect(void *opaque)
227 1.1 christos {
228 1.1 christos return;
229 1.1 christos }
230 1.1 christos
231 1.1 christos static void
232 1.4 christos /*ARGSUSED*/
233 1.1 christos acpiapm_enable(void *opaque, int onoff)
234 1.1 christos {
235 1.1 christos return;
236 1.1 christos }
237 1.1 christos
238 1.1 christos static int
239 1.1 christos acpiapm_set_powstate(void *opaque, u_int devid, u_int powstat)
240 1.1 christos {
241 1.1 christos struct acpi_softc *sc = opaque;
242 1.1 christos
243 1.1 christos if (devid != APM_DEV_ALLDEVS)
244 1.1 christos return APM_ERR_UNRECOG_DEV;
245 1.1 christos
246 1.1 christos switch (powstat) {
247 1.1 christos case APM_SYS_READY:
248 1.1 christos break;
249 1.1 christos case APM_SYS_STANDBY:
250 1.4 christos acpi_enter_sleep_state(sc, get_state_value(standby_state));
251 1.4 christos resumed = 1;
252 1.1 christos break;
253 1.1 christos case APM_SYS_SUSPEND:
254 1.4 christos acpi_enter_sleep_state(sc, get_state_value(suspend_state));
255 1.4 christos resumed = 1;
256 1.1 christos break;
257 1.1 christos case APM_SYS_OFF:
258 1.1 christos break;
259 1.1 christos case APM_LASTREQ_INPROG:
260 1.1 christos break;
261 1.1 christos case APM_LASTREQ_REJECTED:
262 1.1 christos break;
263 1.1 christos }
264 1.1 christos
265 1.1 christos return 0;
266 1.1 christos }
267 1.1 christos
268 1.1 christos static int
269 1.4 christos /*ARGSUSED*/
270 1.1 christos acpiapm_get_powstat(void *opaque, u_int batteryid, struct apm_power_info *pinfo)
271 1.1 christos {
272 1.4 christos #define APM_BATT_FLAG_WATERMARK_MASK (APM_BATT_FLAG_CRITICAL | \
273 1.4 christos APM_BATT_FLAG_LOW | \
274 1.4 christos APM_BATT_FLAG_HIGH)
275 1.1 christos int i, curcap, lowcap, warncap, cap, descap, lastcap, discharge;
276 1.1 christos envsys_tre_data_t etds;
277 1.1 christos envsys_basic_info_t ebis;
278 1.1 christos
279 1.1 christos curcap = lowcap = warncap = cap = descap = lastcap = discharge = -1;
280 1.1 christos
281 1.1 christos (void)memset(pinfo, 0, sizeof(*pinfo));
282 1.1 christos pinfo->ac_state = APM_AC_UNKNOWN;
283 1.1 christos pinfo->minutes_valid = 0;
284 1.4 christos pinfo->minutes_left = 0xffff; /* unknown */
285 1.1 christos pinfo->batteryid = 0;
286 1.1 christos pinfo->nbattery = 1;
287 1.1 christos pinfo->battery_flags = 0;
288 1.4 christos pinfo->battery_state = APM_BATT_UNKNOWN; /* ignored */
289 1.1 christos pinfo->battery_life = APM_BATT_LIFE_UNKNOWN;
290 1.1 christos
291 1.1 christos for (i = 0;; i++) {
292 1.1 christos const char *desc;
293 1.1 christos int data;
294 1.1 christos int flags;
295 1.1 christos
296 1.1 christos ebis.sensor = i;
297 1.1 christos if (sysmonioctl_envsys(0, ENVSYS_GTREINFO, (void *)&ebis, 0,
298 1.1 christos NULL) || (ebis.validflags & ENVSYS_FVALID) == 0)
299 1.1 christos break;
300 1.1 christos etds.sensor = i;
301 1.1 christos if (sysmonioctl_envsys(0, ENVSYS_GTREDATA, (void *)&etds, 0,
302 1.1 christos NULL))
303 1.1 christos continue;
304 1.1 christos desc = ebis.desc;
305 1.1 christos flags = etds.validflags;
306 1.1 christos data = etds.cur.data_s;
307 1.1 christos
308 1.1 christos DPRINTF(("%d %s %d %d\n", i, desc, data, flags));
309 1.1 christos if ((flags & ENVSYS_FCURVALID) == 0)
310 1.1 christos continue;
311 1.1 christos if (strstr(desc, " disconnected")) {
312 1.1 christos pinfo->ac_state = data ? APM_AC_OFF : APM_AC_ON;
313 1.4 christos } else if (strstr(desc, " present") && data == 0)
314 1.1 christos pinfo->battery_flags |= APM_BATT_FLAG_NO_SYSTEM_BATTERY;
315 1.4 christos else if (strstr(desc, " charging") && data)
316 1.1 christos pinfo->battery_flags |= APM_BATT_FLAG_CHARGING;
317 1.4 christos else if (strstr(desc, " discharging") && data)
318 1.1 christos pinfo->battery_flags &= ~APM_BATT_FLAG_CHARGING;
319 1.1 christos else if (strstr(desc, " warn cap"))
320 1.1 christos warncap = data / 1000;
321 1.1 christos else if (strstr(desc, " low cap"))
322 1.1 christos lowcap = data / 1000;
323 1.1 christos else if (strstr(desc, " last full cap"))
324 1.1 christos lastcap = data / 1000;
325 1.1 christos else if (strstr(desc, " design cap"))
326 1.1 christos descap = data / 1000;
327 1.1 christos else if (strstr(desc, " charge") &&
328 1.1 christos strstr(desc, " charge rate") == NULL)
329 1.1 christos cap = data / 1000;
330 1.1 christos else if (strstr(desc, " discharge rate"))
331 1.1 christos discharge = data / 1000;
332 1.1 christos }
333 1.1 christos
334 1.1 christos if (cap != -1) {
335 1.4 christos if (warncap != -1 && cap < warncap)
336 1.1 christos pinfo->battery_flags |= APM_BATT_FLAG_CRITICAL;
337 1.4 christos else if (lowcap != -1) {
338 1.4 christos if (cap < lowcap)
339 1.4 christos pinfo->battery_flags |= APM_BATT_FLAG_LOW;
340 1.4 christos else
341 1.4 christos pinfo->battery_flags |= APM_BATT_FLAG_HIGH;
342 1.1 christos }
343 1.2 christos if (lastcap != -1 && lastcap != 0)
344 1.1 christos pinfo->battery_life = 100 * cap / lastcap;
345 1.2 christos else if (descap != -1 && descap != 0)
346 1.1 christos pinfo->battery_life = 100 * cap / descap;
347 1.1 christos }
348 1.1 christos
349 1.1 christos if ((pinfo->battery_flags & APM_BATT_FLAG_CHARGING) == 0) {
350 1.4 christos /* discharging */
351 1.3 hira if (discharge != -1 && cap != -1 && discharge != 0)
352 1.1 christos pinfo->minutes_left = 60 * cap / discharge;
353 1.1 christos }
354 1.4 christos if ((pinfo->battery_flags & APM_BATT_FLAG_WATERMARK_MASK) == 0 &&
355 1.4 christos (pinfo->battery_flags & APM_BATT_FLAG_NO_SYSTEM_BATTERY) == 0) {
356 1.4 christos if (pinfo->ac_state == APM_AC_ON)
357 1.4 christos pinfo->battery_flags |= APM_BATT_FLAG_HIGH;
358 1.4 christos else
359 1.4 christos pinfo->battery_flags |= APM_BATT_FLAG_LOW;
360 1.4 christos }
361 1.4 christos
362 1.1 christos DPRINTF(("%d %d %d %d %d %d\n", cap, warncap, lowcap, lastcap, descap,
363 1.1 christos discharge));
364 1.4 christos DPRINTF(("pinfo %d %d %d\n", pinfo->battery_flags,
365 1.4 christos pinfo->battery_life, pinfo->battery_life));
366 1.1 christos return 0;
367 1.1 christos }
368 1.1 christos
369 1.1 christos static int
370 1.4 christos /*ARGSUSED*/
371 1.1 christos acpiapm_get_event(void *opaque, u_int *event_type, u_int *event_info)
372 1.1 christos {
373 1.4 christos if (capability_changed) {
374 1.4 christos capability_changed = 0;
375 1.4 christos *event_type = APM_CAP_CHANGE;
376 1.4 christos *event_info = 0;
377 1.4 christos return 0;
378 1.4 christos }
379 1.4 christos if (resumed) {
380 1.4 christos resumed = 0;
381 1.4 christos *event_type = APM_NORMAL_RESUME;
382 1.4 christos *event_info = 0;
383 1.4 christos return 0;
384 1.4 christos }
385 1.4 christos
386 1.1 christos return APM_ERR_NOEVENTS;
387 1.1 christos }
388 1.1 christos
389 1.1 christos static void
390 1.4 christos /*ARGSUSED*/
391 1.1 christos acpiapm_cpu_busy(void *opaque)
392 1.1 christos {
393 1.1 christos return;
394 1.1 christos }
395 1.1 christos
396 1.1 christos static void
397 1.4 christos /*ARGSUSED*/
398 1.1 christos acpiapm_cpu_idle(void *opaque)
399 1.1 christos {
400 1.1 christos return;
401 1.1 christos }
402 1.1 christos
403 1.1 christos static void
404 1.4 christos /*ARGSUSED*/
405 1.1 christos acpiapm_get_capabilities(void *opaque, u_int *numbatts, u_int *capflags)
406 1.1 christos {
407 1.1 christos *numbatts = 1;
408 1.4 christos *capflags = capabilities;
409 1.1 christos return;
410 1.1 christos }
411