acpi_apm.c revision 1.16 1 1.16 pgoyette /* $NetBSD: acpi_apm.c,v 1.16 2010/03/24 01:45:37 pgoyette 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 *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos /*
33 1.1 christos * Autoconfiguration support for the Intel ACPI Component Architecture
34 1.1 christos * ACPI reference implementation.
35 1.1 christos */
36 1.1 christos
37 1.1 christos #include <sys/cdefs.h>
38 1.16 pgoyette __KERNEL_RCSID(0, "$NetBSD: acpi_apm.c,v 1.16 2010/03/24 01:45:37 pgoyette Exp $");
39 1.1 christos
40 1.1 christos #include <sys/param.h>
41 1.1 christos #include <sys/device.h>
42 1.1 christos #include <sys/sysctl.h>
43 1.15 jruoho #include <sys/systm.h>
44 1.1 christos
45 1.15 jruoho #include <dev/acpi/acpivar.h>
46 1.1 christos #include <dev/apm/apmvar.h>
47 1.1 christos
48 1.1 christos static void acpiapm_disconnect(void *);
49 1.1 christos static void acpiapm_enable(void *, int);
50 1.1 christos static int acpiapm_set_powstate(void *, u_int, u_int);
51 1.1 christos static int acpiapm_get_powstat(void *, u_int, struct apm_power_info *);
52 1.1 christos static int acpiapm_get_event(void *, u_int *, u_int *);
53 1.1 christos static void acpiapm_cpu_busy(void *);
54 1.1 christos static void acpiapm_cpu_idle(void *);
55 1.1 christos static void acpiapm_get_capabilities(void *, u_int *, u_int *);
56 1.1 christos
57 1.1 christos struct apm_accessops acpiapm_accessops = {
58 1.1 christos acpiapm_disconnect,
59 1.1 christos acpiapm_enable,
60 1.1 christos acpiapm_set_powstate,
61 1.1 christos acpiapm_get_powstat,
62 1.1 christos acpiapm_get_event,
63 1.1 christos acpiapm_cpu_busy,
64 1.1 christos acpiapm_cpu_idle,
65 1.1 christos acpiapm_get_capabilities,
66 1.1 christos };
67 1.1 christos
68 1.1 christos #ifdef ACPI_APM_DEBUG
69 1.1 christos #define DPRINTF(a) uprintf a
70 1.1 christos #else
71 1.1 christos #define DPRINTF(a)
72 1.1 christos #endif
73 1.1 christos
74 1.4 christos #ifndef ACPI_APM_DEFAULT_STANDBY_STATE
75 1.4 christos #define ACPI_APM_DEFAULT_STANDBY_STATE (1)
76 1.4 christos #endif
77 1.4 christos #ifndef ACPI_APM_DEFAULT_SUSPEND_STATE
78 1.4 christos #define ACPI_APM_DEFAULT_SUSPEND_STATE (3)
79 1.4 christos #endif
80 1.4 christos #define ACPI_APM_DEFAULT_CAP \
81 1.4 christos ((ACPI_APM_DEFAULT_STANDBY_STATE!=0 ? APM_GLOBAL_STANDBY : 0) | \
82 1.4 christos (ACPI_APM_DEFAULT_SUSPEND_STATE!=0 ? APM_GLOBAL_SUSPEND : 0))
83 1.4 christos #define ACPI_APM_STATE_MIN (0)
84 1.4 christos #define ACPI_APM_STATE_MAX (4)
85 1.4 christos
86 1.4 christos /* It is assumed that there is only acpiapm instance. */
87 1.4 christos static int resumed = 0, capability_changed = 0;
88 1.4 christos static int standby_state = ACPI_APM_DEFAULT_STANDBY_STATE;
89 1.4 christos static int suspend_state = ACPI_APM_DEFAULT_SUSPEND_STATE;
90 1.4 christos static int capabilities = ACPI_APM_DEFAULT_CAP;
91 1.4 christos static int acpiapm_node = CTL_EOL, standby_node = CTL_EOL;
92 1.4 christos
93 1.1 christos struct acpi_softc;
94 1.1 christos extern ACPI_STATUS acpi_enter_sleep_state(struct acpi_softc *, int);
95 1.13 cube static int acpiapm_match(device_t, cfdata_t , void *);
96 1.13 cube static void acpiapm_attach(device_t, device_t, void *);
97 1.4 christos static int sysctl_state(SYSCTLFN_PROTO);
98 1.1 christos
99 1.13 cube CFATTACH_DECL_NEW(acpiapm, sizeof(struct apm_softc),
100 1.1 christos acpiapm_match, acpiapm_attach, NULL, NULL);
101 1.1 christos
102 1.1 christos static int
103 1.4 christos /*ARGSUSED*/
104 1.13 cube acpiapm_match(device_t parent, cfdata_t match, void *aux)
105 1.1 christos {
106 1.1 christos return apm_match();
107 1.1 christos }
108 1.1 christos
109 1.1 christos static void
110 1.4 christos /*ARGSUSED*/
111 1.13 cube acpiapm_attach(device_t parent, device_t self, void *aux)
112 1.1 christos {
113 1.13 cube struct apm_softc *sc = device_private(self);
114 1.1 christos
115 1.13 cube sc->sc_dev = self;
116 1.1 christos sc->sc_ops = &acpiapm_accessops;
117 1.1 christos sc->sc_cookie = parent;
118 1.1 christos sc->sc_vers = 0x0102;
119 1.1 christos sc->sc_detail = 0;
120 1.1 christos sc->sc_hwflags = APM_F_DONT_RUN_HOOKS;
121 1.1 christos apm_attach(sc);
122 1.1 christos }
123 1.1 christos
124 1.4 christos static int
125 1.4 christos get_state_value(int id)
126 1.4 christos {
127 1.4 christos const int states[] = {
128 1.4 christos ACPI_STATE_S0,
129 1.4 christos ACPI_STATE_S1,
130 1.4 christos ACPI_STATE_S2,
131 1.4 christos ACPI_STATE_S3,
132 1.4 christos ACPI_STATE_S4
133 1.4 christos };
134 1.4 christos
135 1.4 christos if (id < ACPI_APM_STATE_MIN || id > ACPI_APM_STATE_MAX)
136 1.4 christos return ACPI_STATE_S0;
137 1.4 christos
138 1.4 christos return states[id];
139 1.4 christos }
140 1.4 christos
141 1.4 christos static int
142 1.4 christos sysctl_state(SYSCTLFN_ARGS)
143 1.4 christos {
144 1.4 christos int newstate, error, *ref, cap, oldcap;
145 1.4 christos struct sysctlnode node;
146 1.4 christos
147 1.4 christos if (rnode->sysctl_num == standby_node) {
148 1.4 christos ref = &standby_state;
149 1.4 christos cap = APM_GLOBAL_STANDBY;
150 1.4 christos } else {
151 1.4 christos ref = &suspend_state;
152 1.4 christos cap = APM_GLOBAL_SUSPEND;
153 1.4 christos }
154 1.4 christos
155 1.4 christos newstate = *ref;
156 1.4 christos node = *rnode;
157 1.4 christos node.sysctl_data = &newstate;
158 1.4 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
159 1.4 christos if (error || newp == NULL)
160 1.4 christos return error;
161 1.4 christos
162 1.4 christos if (newstate < ACPI_APM_STATE_MIN || newstate > ACPI_APM_STATE_MAX)
163 1.4 christos return EINVAL;
164 1.4 christos
165 1.4 christos *ref = newstate;
166 1.4 christos oldcap = capabilities;
167 1.4 christos capabilities = newstate != 0 ? oldcap | cap : oldcap & ~cap;
168 1.4 christos if ((capabilities ^ oldcap) != 0)
169 1.4 christos capability_changed = 1;
170 1.4 christos
171 1.4 christos return 0;
172 1.4 christos }
173 1.4 christos
174 1.4 christos SYSCTL_SETUP(sysctl_acpiapm_setup, "sysctl machdep.acpiapm subtree setup")
175 1.4 christos {
176 1.4 christos const struct sysctlnode *node;
177 1.4 christos
178 1.4 christos if (sysctl_createv(clog, 0, NULL, NULL,
179 1.4 christos CTLFLAG_PERMANENT,
180 1.4 christos CTLTYPE_NODE, "machdep", NULL,
181 1.4 christos NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL))
182 1.4 christos return;
183 1.4 christos
184 1.4 christos if (sysctl_createv(clog, 0, NULL, &node,
185 1.4 christos CTLFLAG_PERMANENT,
186 1.4 christos CTLTYPE_NODE, "acpiapm", NULL,
187 1.4 christos NULL, 0, NULL, 0,
188 1.4 christos CTL_MACHDEP, CTL_CREATE, CTL_EOL))
189 1.4 christos return;
190 1.4 christos acpiapm_node = node->sysctl_num;
191 1.4 christos
192 1.4 christos if (sysctl_createv(clog, 0, NULL, &node,
193 1.4 christos CTLFLAG_READWRITE,
194 1.4 christos CTLTYPE_INT, "standby", NULL,
195 1.4 christos &sysctl_state, 0, NULL, 0,
196 1.4 christos CTL_MACHDEP, acpiapm_node, CTL_CREATE, CTL_EOL))
197 1.4 christos return;
198 1.4 christos standby_node = node->sysctl_num;
199 1.4 christos
200 1.4 christos if (sysctl_createv(clog, 0, NULL, NULL,
201 1.4 christos CTLFLAG_READWRITE,
202 1.4 christos CTLTYPE_INT, "suspend", NULL,
203 1.4 christos &sysctl_state, 0, NULL, 0,
204 1.4 christos CTL_MACHDEP, acpiapm_node, CTL_CREATE, CTL_EOL))
205 1.4 christos return;
206 1.4 christos }
207 1.4 christos
208 1.1 christos /*****************************************************************************
209 1.1 christos * Minimalistic ACPI /dev/apm emulation support, for ACPI suspend
210 1.1 christos *****************************************************************************/
211 1.1 christos
212 1.1 christos static void
213 1.4 christos /*ARGSUSED*/
214 1.8 christos acpiapm_disconnect(void *opaque)
215 1.1 christos {
216 1.1 christos return;
217 1.1 christos }
218 1.1 christos
219 1.1 christos static void
220 1.4 christos /*ARGSUSED*/
221 1.8 christos acpiapm_enable(void *opaque, int onoff)
222 1.1 christos {
223 1.1 christos return;
224 1.1 christos }
225 1.1 christos
226 1.1 christos static int
227 1.1 christos acpiapm_set_powstate(void *opaque, u_int devid, u_int powstat)
228 1.1 christos {
229 1.13 cube struct acpi_softc *sc = device_private((device_t)opaque);
230 1.1 christos
231 1.1 christos if (devid != APM_DEV_ALLDEVS)
232 1.1 christos return APM_ERR_UNRECOG_DEV;
233 1.1 christos
234 1.1 christos switch (powstat) {
235 1.1 christos case APM_SYS_READY:
236 1.1 christos break;
237 1.1 christos case APM_SYS_STANDBY:
238 1.4 christos acpi_enter_sleep_state(sc, get_state_value(standby_state));
239 1.4 christos resumed = 1;
240 1.1 christos break;
241 1.1 christos case APM_SYS_SUSPEND:
242 1.4 christos acpi_enter_sleep_state(sc, get_state_value(suspend_state));
243 1.4 christos resumed = 1;
244 1.1 christos break;
245 1.1 christos case APM_SYS_OFF:
246 1.1 christos break;
247 1.1 christos case APM_LASTREQ_INPROG:
248 1.1 christos break;
249 1.1 christos case APM_LASTREQ_REJECTED:
250 1.1 christos break;
251 1.1 christos }
252 1.1 christos
253 1.1 christos return 0;
254 1.1 christos }
255 1.1 christos
256 1.1 christos static int
257 1.4 christos /*ARGSUSED*/
258 1.8 christos acpiapm_get_powstat(void *opaque, u_int batteryid,
259 1.7 xtraeme struct apm_power_info *pinfo)
260 1.1 christos {
261 1.4 christos #define APM_BATT_FLAG_WATERMARK_MASK (APM_BATT_FLAG_CRITICAL | \
262 1.4 christos APM_BATT_FLAG_LOW | \
263 1.4 christos APM_BATT_FLAG_HIGH)
264 1.1 christos int i, curcap, lowcap, warncap, cap, descap, lastcap, discharge;
265 1.16 pgoyette int cap_valid, lastcap_valid, discharge_valid, present;
266 1.1 christos envsys_tre_data_t etds;
267 1.1 christos envsys_basic_info_t ebis;
268 1.1 christos
269 1.5 gdt /* Denote most variables as unitialized. */
270 1.6 gdt curcap = lowcap = warncap = descap = -1;
271 1.5 gdt
272 1.5 gdt /* Prepare to aggregate these two variables over all batteries. */
273 1.6 gdt cap = lastcap = discharge = 0;
274 1.16 pgoyette cap_valid = lastcap_valid = discharge_valid = present = 0;
275 1.1 christos
276 1.1 christos (void)memset(pinfo, 0, sizeof(*pinfo));
277 1.1 christos pinfo->ac_state = APM_AC_UNKNOWN;
278 1.1 christos pinfo->minutes_valid = 0;
279 1.11 plunky pinfo->minutes_left = 0;
280 1.1 christos pinfo->batteryid = 0;
281 1.5 gdt pinfo->nbattery = 0; /* to be incremented as batteries are found */
282 1.1 christos pinfo->battery_flags = 0;
283 1.4 christos pinfo->battery_state = APM_BATT_UNKNOWN; /* ignored */
284 1.1 christos pinfo->battery_life = APM_BATT_LIFE_UNKNOWN;
285 1.1 christos
286 1.9 christos sysmonopen_envsys(0, 0, 0, &lwp0);
287 1.9 christos
288 1.1 christos for (i = 0;; i++) {
289 1.1 christos const char *desc;
290 1.1 christos int data;
291 1.1 christos int flags;
292 1.1 christos
293 1.1 christos ebis.sensor = i;
294 1.1 christos if (sysmonioctl_envsys(0, ENVSYS_GTREINFO, (void *)&ebis, 0,
295 1.1 christos NULL) || (ebis.validflags & ENVSYS_FVALID) == 0)
296 1.1 christos break;
297 1.1 christos etds.sensor = i;
298 1.1 christos if (sysmonioctl_envsys(0, ENVSYS_GTREDATA, (void *)&etds, 0,
299 1.1 christos NULL))
300 1.1 christos continue;
301 1.1 christos desc = ebis.desc;
302 1.1 christos flags = etds.validflags;
303 1.1 christos data = etds.cur.data_s;
304 1.1 christos
305 1.1 christos DPRINTF(("%d %s %d %d\n", i, desc, data, flags));
306 1.1 christos if ((flags & ENVSYS_FCURVALID) == 0)
307 1.1 christos continue;
308 1.10 xtraeme if (strstr(desc, " connected")) {
309 1.10 xtraeme pinfo->ac_state = data ? APM_AC_ON : APM_AC_OFF;
310 1.16 pgoyette } else if (strstr(desc, " present") && data != 0)
311 1.16 pgoyette present++;
312 1.4 christos else if (strstr(desc, " charging") && data)
313 1.1 christos pinfo->battery_flags |= APM_BATT_FLAG_CHARGING;
314 1.10 xtraeme else if (strstr(desc, " charging") && !data)
315 1.1 christos pinfo->battery_flags &= ~APM_BATT_FLAG_CHARGING;
316 1.1 christos else if (strstr(desc, " warn cap"))
317 1.1 christos warncap = data / 1000;
318 1.1 christos else if (strstr(desc, " low cap"))
319 1.1 christos lowcap = data / 1000;
320 1.5 gdt else if (strstr(desc, " last full cap")) {
321 1.5 gdt lastcap += data / 1000;
322 1.5 gdt lastcap_valid = 1;
323 1.5 gdt }
324 1.1 christos else if (strstr(desc, " design cap"))
325 1.1 christos descap = data / 1000;
326 1.1 christos else if (strstr(desc, " charge") &&
327 1.12 jmcneill strstr(desc, " charge rate") == NULL &&
328 1.12 jmcneill strstr(desc, " charge state") == NULL) {
329 1.5 gdt cap += data / 1000;
330 1.5 gdt cap_valid = 1;
331 1.5 gdt pinfo->nbattery++;
332 1.5 gdt }
333 1.6 gdt else if (strstr(desc, " discharge rate")) {
334 1.6 gdt discharge += data / 1000;
335 1.6 gdt discharge_valid = 1;
336 1.6 gdt }
337 1.1 christos }
338 1.9 christos sysmonclose_envsys(0, 0, 0, &lwp0);
339 1.1 christos
340 1.16 pgoyette if (present == 0)
341 1.16 pgoyette pinfo->battery_flags |= APM_BATT_FLAG_NO_SYSTEM_BATTERY;
342 1.16 pgoyette
343 1.5 gdt if (cap_valid > 0) {
344 1.4 christos if (warncap != -1 && cap < warncap)
345 1.1 christos pinfo->battery_flags |= APM_BATT_FLAG_CRITICAL;
346 1.4 christos else if (lowcap != -1) {
347 1.4 christos if (cap < lowcap)
348 1.4 christos pinfo->battery_flags |= APM_BATT_FLAG_LOW;
349 1.4 christos else
350 1.4 christos pinfo->battery_flags |= APM_BATT_FLAG_HIGH;
351 1.1 christos }
352 1.5 gdt if (lastcap_valid > 0 && lastcap != 0)
353 1.1 christos pinfo->battery_life = 100 * cap / lastcap;
354 1.2 christos else if (descap != -1 && descap != 0)
355 1.1 christos pinfo->battery_life = 100 * cap / descap;
356 1.1 christos }
357 1.1 christos
358 1.1 christos if ((pinfo->battery_flags & APM_BATT_FLAG_CHARGING) == 0) {
359 1.4 christos /* discharging */
360 1.3 hira if (discharge != -1 && cap != -1 && discharge != 0)
361 1.1 christos pinfo->minutes_left = 60 * cap / discharge;
362 1.1 christos }
363 1.4 christos if ((pinfo->battery_flags & APM_BATT_FLAG_WATERMARK_MASK) == 0 &&
364 1.4 christos (pinfo->battery_flags & APM_BATT_FLAG_NO_SYSTEM_BATTERY) == 0) {
365 1.4 christos if (pinfo->ac_state == APM_AC_ON)
366 1.4 christos pinfo->battery_flags |= APM_BATT_FLAG_HIGH;
367 1.4 christos else
368 1.4 christos pinfo->battery_flags |= APM_BATT_FLAG_LOW;
369 1.4 christos }
370 1.4 christos
371 1.1 christos DPRINTF(("%d %d %d %d %d %d\n", cap, warncap, lowcap, lastcap, descap,
372 1.1 christos discharge));
373 1.4 christos DPRINTF(("pinfo %d %d %d\n", pinfo->battery_flags,
374 1.4 christos pinfo->battery_life, pinfo->battery_life));
375 1.1 christos return 0;
376 1.1 christos }
377 1.1 christos
378 1.1 christos static int
379 1.4 christos /*ARGSUSED*/
380 1.8 christos acpiapm_get_event(void *opaque, u_int *event_type, u_int *event_info)
381 1.1 christos {
382 1.4 christos if (capability_changed) {
383 1.4 christos capability_changed = 0;
384 1.4 christos *event_type = APM_CAP_CHANGE;
385 1.4 christos *event_info = 0;
386 1.4 christos return 0;
387 1.4 christos }
388 1.4 christos if (resumed) {
389 1.4 christos resumed = 0;
390 1.4 christos *event_type = APM_NORMAL_RESUME;
391 1.4 christos *event_info = 0;
392 1.4 christos return 0;
393 1.4 christos }
394 1.4 christos
395 1.1 christos return APM_ERR_NOEVENTS;
396 1.1 christos }
397 1.1 christos
398 1.1 christos static void
399 1.4 christos /*ARGSUSED*/
400 1.8 christos acpiapm_cpu_busy(void *opaque)
401 1.1 christos {
402 1.1 christos return;
403 1.1 christos }
404 1.1 christos
405 1.1 christos static void
406 1.4 christos /*ARGSUSED*/
407 1.8 christos acpiapm_cpu_idle(void *opaque)
408 1.1 christos {
409 1.1 christos return;
410 1.1 christos }
411 1.1 christos
412 1.1 christos static void
413 1.4 christos /*ARGSUSED*/
414 1.8 christos acpiapm_get_capabilities(void *opaque, u_int *numbatts,
415 1.7 xtraeme u_int *capflags)
416 1.1 christos {
417 1.1 christos *numbatts = 1;
418 1.4 christos *capflags = capabilities;
419 1.1 christos return;
420 1.1 christos }
421