acpi_bat.c revision 1.35 1 1.35 mycroft /* $NetBSD: acpi_bat.c,v 1.35 2003/11/03 18:07:10 mycroft Exp $ */
2 1.26 mycroft
3 1.26 mycroft /*-
4 1.26 mycroft * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.26 mycroft * All rights reserved.
6 1.26 mycroft *
7 1.26 mycroft * This code is derived from software contributed to The NetBSD Foundation
8 1.26 mycroft * by Charles M. Hannum of By Noon Software, Inc.
9 1.26 mycroft *
10 1.26 mycroft * Redistribution and use in source and binary forms, with or without
11 1.26 mycroft * modification, are permitted provided that the following conditions
12 1.26 mycroft * are met:
13 1.26 mycroft * 1. Redistributions of source code must retain the above copyright
14 1.26 mycroft * notice, this list of conditions and the following disclaimer.
15 1.26 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.26 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.26 mycroft * documentation and/or other materials provided with the distribution.
18 1.26 mycroft * 3. All advertising materials mentioning features or use of this software
19 1.26 mycroft * must display the following acknowledgement:
20 1.26 mycroft * This product includes software developed by the NetBSD
21 1.26 mycroft * Foundation, Inc. and its contributors.
22 1.26 mycroft * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.26 mycroft * contributors may be used to endorse or promote products derived
24 1.26 mycroft * from this software without specific prior written permission.
25 1.26 mycroft *
26 1.26 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.26 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.26 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.26 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.26 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.26 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.26 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.26 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.26 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.26 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.26 mycroft * POSSIBILITY OF SUCH DAMAGE.
37 1.26 mycroft */
38 1.1 sommerfe
39 1.1 sommerfe /*
40 1.1 sommerfe * Copyright 2001 Bill Sommerfeld.
41 1.1 sommerfe * All rights reserved.
42 1.1 sommerfe *
43 1.1 sommerfe * Redistribution and use in source and binary forms, with or without
44 1.1 sommerfe * modification, are permitted provided that the following conditions
45 1.1 sommerfe * are met:
46 1.1 sommerfe * 1. Redistributions of source code must retain the above copyright
47 1.1 sommerfe * notice, this list of conditions and the following disclaimer.
48 1.1 sommerfe * 2. Redistributions in binary form must reproduce the above copyright
49 1.1 sommerfe * notice, this list of conditions and the following disclaimer in the
50 1.1 sommerfe * documentation and/or other materials provided with the distribution.
51 1.1 sommerfe * 3. All advertising materials mentioning features or use of this software
52 1.1 sommerfe * must display the following acknowledgement:
53 1.1 sommerfe * This product includes software developed for the NetBSD Project by
54 1.1 sommerfe * Wasabi Systems, Inc.
55 1.1 sommerfe * 4. The name of Wasabi Systems, Inc. may not be used to endorse
56 1.1 sommerfe * or promote products derived from this software without specific prior
57 1.1 sommerfe * written permission.
58 1.1 sommerfe *
59 1.1 sommerfe * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
60 1.1 sommerfe * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61 1.1 sommerfe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62 1.1 sommerfe * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
63 1.1 sommerfe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 1.1 sommerfe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 1.1 sommerfe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 1.1 sommerfe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 1.1 sommerfe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 1.1 sommerfe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69 1.1 sommerfe * POSSIBILITY OF SUCH DAMAGE.
70 1.1 sommerfe */
71 1.1 sommerfe
72 1.13 explorer #if 0
73 1.1 sommerfe #define ACPI_BAT_DEBUG
74 1.13 explorer #endif
75 1.1 sommerfe
76 1.1 sommerfe /*
77 1.1 sommerfe * ACPI Battery Driver.
78 1.1 sommerfe *
79 1.1 sommerfe * ACPI defines two different battery device interfaces: "Control
80 1.1 sommerfe * Method" batteries, in which AML methods are defined in order to get
81 1.1 sommerfe * battery status and set battery alarm thresholds, and a "Smart
82 1.1 sommerfe * Battery" device, which is an SMbus device accessed through the ACPI
83 1.1 sommerfe * Embedded Controller device.
84 1.1 sommerfe *
85 1.1 sommerfe * This driver is for the "Control Method"-style battery only.
86 1.1 sommerfe */
87 1.1 sommerfe
88 1.1 sommerfe #include <sys/cdefs.h>
89 1.35 mycroft __KERNEL_RCSID(0, "$NetBSD: acpi_bat.c,v 1.35 2003/11/03 18:07:10 mycroft Exp $");
90 1.1 sommerfe
91 1.1 sommerfe #include <sys/param.h>
92 1.1 sommerfe #include <sys/systm.h>
93 1.1 sommerfe #include <sys/kernel.h> /* for hz */
94 1.1 sommerfe #include <sys/device.h>
95 1.14 explorer #include <dev/sysmon/sysmonvar.h>
96 1.1 sommerfe
97 1.1 sommerfe #include <dev/acpi/acpica.h>
98 1.1 sommerfe #include <dev/acpi/acpireg.h>
99 1.1 sommerfe #include <dev/acpi/acpivar.h>
100 1.1 sommerfe
101 1.14 explorer /* sensor indexes */
102 1.15 tshiozak #define ACPIBAT_PRESENT 0
103 1.15 tshiozak #define ACPIBAT_DCAPACITY 1
104 1.15 tshiozak #define ACPIBAT_LFCCAPACITY 2
105 1.15 tshiozak #define ACPIBAT_TECHNOLOGY 3
106 1.15 tshiozak #define ACPIBAT_DVOLTAGE 4
107 1.15 tshiozak #define ACPIBAT_WCAPACITY 5
108 1.15 tshiozak #define ACPIBAT_LCAPACITY 6
109 1.15 tshiozak #define ACPIBAT_VOLTAGE 7
110 1.29 mycroft #define ACPIBAT_CHARGERATE 8
111 1.29 mycroft #define ACPIBAT_DISCHARGERATE 9
112 1.29 mycroft #define ACPIBAT_CAPACITY 10
113 1.29 mycroft #define ACPIBAT_CHARGING 11
114 1.29 mycroft #define ACPIBAT_DISCHARGING 12
115 1.29 mycroft #define ACPIBAT_NSENSORS 13 /* number of sensors */
116 1.14 explorer
117 1.14 explorer const struct envsys_range acpibat_range_amp[] = {
118 1.14 explorer { 0, 1, ENVSYS_SVOLTS_DC },
119 1.14 explorer { 1, 2, ENVSYS_SAMPS },
120 1.14 explorer { 2, 3, ENVSYS_SAMPHOUR },
121 1.14 explorer { 1, 0, -1 },
122 1.14 explorer };
123 1.14 explorer
124 1.14 explorer const struct envsys_range acpibat_range_watt[] = {
125 1.14 explorer { 0, 1, ENVSYS_SVOLTS_DC },
126 1.14 explorer { 1, 2, ENVSYS_SWATTS },
127 1.14 explorer { 2, 3, ENVSYS_SWATTHOUR },
128 1.14 explorer { 1, 0, -1 },
129 1.14 explorer };
130 1.11 explorer
131 1.1 sommerfe struct acpibat_softc {
132 1.1 sommerfe struct device sc_dev; /* base device glue */
133 1.1 sommerfe struct acpi_devnode *sc_node; /* our ACPI devnode */
134 1.1 sommerfe int sc_flags; /* see below */
135 1.15 tshiozak int sc_available; /* available information level */
136 1.14 explorer
137 1.14 explorer struct sysmon_envsys sc_sysmon;
138 1.14 explorer struct envsys_basic_info sc_info[ACPIBAT_NSENSORS];
139 1.14 explorer struct envsys_tre_data sc_data[ACPIBAT_NSENSORS];
140 1.14 explorer
141 1.15 tshiozak struct simplelock sc_lock;
142 1.23 mycroft
143 1.23 mycroft struct timeval sc_lastupdate, sc_updateinterval;
144 1.1 sommerfe };
145 1.1 sommerfe
146 1.33 kochi static const char * const bat_hid[] = {
147 1.33 kochi "PNP0C0A",
148 1.33 kochi NULL
149 1.33 kochi };
150 1.33 kochi
151 1.11 explorer /*
152 1.11 explorer * These flags are used to examine the battery device data returned from
153 1.11 explorer * the ACPI interface, specifically the "battery status"
154 1.11 explorer */
155 1.11 explorer #define ACPIBAT_PWRUNIT_MA 0x00000001 /* mA not mW */
156 1.11 explorer
157 1.11 explorer /*
158 1.11 explorer * These flags are used to examine the battery charge/discharge/critical
159 1.11 explorer * state returned from a get-status command.
160 1.11 explorer */
161 1.14 explorer #define ACPIBAT_ST_DISCHARGING 0x00000001 /* battery is discharging */
162 1.14 explorer #define ACPIBAT_ST_CHARGING 0x00000002 /* battery is charging */
163 1.14 explorer #define ACPIBAT_ST_CRITICAL 0x00000004 /* battery is critical */
164 1.11 explorer
165 1.11 explorer /*
166 1.13 explorer * Flags for battery status from _STA return
167 1.13 explorer */
168 1.15 tshiozak #define ACPIBAT_STA_PRESENT 0x00000010 /* battery present */
169 1.13 explorer
170 1.13 explorer /*
171 1.11 explorer * These flags are used to set internal state in our softc.
172 1.11 explorer */
173 1.1 sommerfe #define ABAT_F_VERBOSE 0x01 /* verbose events */
174 1.1 sommerfe #define ABAT_F_PWRUNIT_MA 0x02 /* mA instead of mW */
175 1.15 tshiozak #define ABAT_F_PRESENT 0x04 /* is the battery present? */
176 1.15 tshiozak #define ABAT_F_LOCKED 0x08 /* is locked? */
177 1.15 tshiozak
178 1.15 tshiozak #define ABAT_SET(sc, f) (void)((sc)->sc_flags |= (f))
179 1.15 tshiozak #define ABAT_CLEAR(sc, f) (void)((sc)->sc_flags &= ~(f))
180 1.15 tshiozak #define ABAT_ISSET(sc, f) ((sc)->sc_flags & (f))
181 1.15 tshiozak
182 1.15 tshiozak /*
183 1.15 tshiozak * Available info level
184 1.15 tshiozak */
185 1.15 tshiozak
186 1.15 tshiozak #define ABAT_ALV_NONE 0 /* none is available */
187 1.15 tshiozak #define ABAT_ALV_PRESENCE 1 /* presence info is available */
188 1.15 tshiozak #define ABAT_ALV_INFO 2 /* battery info is available */
189 1.15 tshiozak #define ABAT_ALV_STAT 3 /* battery status is available */
190 1.15 tshiozak
191 1.15 tshiozak #define ABAT_ASSERT_LOCKED(sc) \
192 1.15 tshiozak do { \
193 1.15 tshiozak if (!((sc)->sc_flags & ABAT_F_LOCKED)) \
194 1.15 tshiozak panic("acpi_bat (expected to be locked)"); \
195 1.15 tshiozak } while(/*CONSTCOND*/0)
196 1.15 tshiozak #define ABAT_ASSERT_UNLOCKED(sc) \
197 1.15 tshiozak do { \
198 1.15 tshiozak if (((sc)->sc_flags & ABAT_F_LOCKED)) \
199 1.15 tshiozak panic("acpi_bat (expected to be unlocked)"); \
200 1.15 tshiozak } while(/*CONSTCOND*/0)
201 1.15 tshiozak #define ABAT_LOCK(sc, s) \
202 1.15 tshiozak do { \
203 1.15 tshiozak ABAT_ASSERT_UNLOCKED(sc); \
204 1.15 tshiozak (s) = splhigh(); \
205 1.15 tshiozak simple_lock(&(sc)->sc_lock); \
206 1.15 tshiozak ABAT_SET((sc), ABAT_F_LOCKED); \
207 1.15 tshiozak } while(/*CONSTCOND*/0)
208 1.15 tshiozak #define ABAT_UNLOCK(sc, s) \
209 1.15 tshiozak do { \
210 1.15 tshiozak ABAT_ASSERT_LOCKED(sc); \
211 1.15 tshiozak ABAT_CLEAR((sc), ABAT_F_LOCKED); \
212 1.15 tshiozak simple_unlock(&(sc)->sc_lock); \
213 1.15 tshiozak splx((s)); \
214 1.15 tshiozak } while(/*CONSTCOND*/0)
215 1.1 sommerfe
216 1.1 sommerfe int acpibat_match(struct device *, struct cfdata *, void *);
217 1.1 sommerfe void acpibat_attach(struct device *, struct device *, void *);
218 1.1 sommerfe
219 1.6 thorpej CFATTACH_DECL(acpibat, sizeof(struct acpibat_softc),
220 1.7 thorpej acpibat_match, acpibat_attach, NULL, NULL);
221 1.1 sommerfe
222 1.15 tshiozak static void acpibat_clear_presence(struct acpibat_softc *);
223 1.15 tshiozak static void acpibat_clear_info(struct acpibat_softc *);
224 1.15 tshiozak static void acpibat_clear_stat(struct acpibat_softc *);
225 1.15 tshiozak static int acpibat_battery_present(struct acpibat_softc *);
226 1.15 tshiozak static ACPI_STATUS acpibat_get_status(struct acpibat_softc *);
227 1.15 tshiozak static ACPI_STATUS acpibat_get_info(struct acpibat_softc *);
228 1.15 tshiozak static void acpibat_print_info(struct acpibat_softc *);
229 1.15 tshiozak static void acpibat_print_stat(struct acpibat_softc *);
230 1.15 tshiozak static void acpibat_update(void *);
231 1.15 tshiozak
232 1.14 explorer static void acpibat_init_envsys(struct acpibat_softc *);
233 1.15 tshiozak static void acpibat_notify_handler(ACPI_HANDLE, UINT32, void *context);
234 1.14 explorer static int acpibat_gtredata(struct sysmon_envsys *, struct envsys_tre_data *);
235 1.14 explorer static int acpibat_streinfo(struct sysmon_envsys *, struct envsys_basic_info *);
236 1.1 sommerfe
237 1.1 sommerfe /*
238 1.1 sommerfe * acpibat_match:
239 1.1 sommerfe *
240 1.1 sommerfe * Autoconfiguration `match' routine.
241 1.1 sommerfe */
242 1.1 sommerfe int
243 1.1 sommerfe acpibat_match(struct device *parent, struct cfdata *match, void *aux)
244 1.1 sommerfe {
245 1.1 sommerfe struct acpi_attach_args *aa = aux;
246 1.1 sommerfe
247 1.1 sommerfe if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
248 1.1 sommerfe return (0);
249 1.1 sommerfe
250 1.33 kochi return (acpi_match_hid(aa->aa_node->ad_devinfo, bat_hid));
251 1.1 sommerfe }
252 1.1 sommerfe
253 1.1 sommerfe /*
254 1.1 sommerfe * acpibat_attach:
255 1.1 sommerfe *
256 1.1 sommerfe * Autoconfiguration `attach' routine.
257 1.1 sommerfe */
258 1.1 sommerfe void
259 1.1 sommerfe acpibat_attach(struct device *parent, struct device *self, void *aux)
260 1.1 sommerfe {
261 1.1 sommerfe struct acpibat_softc *sc = (void *) self;
262 1.1 sommerfe struct acpi_attach_args *aa = aux;
263 1.1 sommerfe ACPI_STATUS rv;
264 1.1 sommerfe
265 1.11 explorer printf(": ACPI Battery (Control Method)\n");
266 1.1 sommerfe
267 1.1 sommerfe sc->sc_node = aa->aa_node;
268 1.15 tshiozak simple_lock_init(&sc->sc_lock);
269 1.1 sommerfe
270 1.1 sommerfe rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
271 1.13 explorer ACPI_DEVICE_NOTIFY,
272 1.13 explorer acpibat_notify_handler, sc);
273 1.35 mycroft if (ACPI_FAILURE(rv)) {
274 1.34 mycroft printf("%s: unable to register DEVICE NOTIFY handler: %s\n",
275 1.34 mycroft sc->sc_dev.dv_xname, AcpiFormatException(rv));
276 1.1 sommerfe return;
277 1.1 sommerfe }
278 1.1 sommerfe
279 1.1 sommerfe /* XXX See acpibat_notify_handler() */
280 1.1 sommerfe rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
281 1.13 explorer ACPI_SYSTEM_NOTIFY,
282 1.13 explorer acpibat_notify_handler, sc);
283 1.35 mycroft if (ACPI_FAILURE(rv)) {
284 1.34 mycroft printf("%s: unable to register SYSTEM NOTIFY handler: %s\n",
285 1.34 mycroft sc->sc_dev.dv_xname, AcpiFormatException(rv));
286 1.1 sommerfe return;
287 1.1 sommerfe }
288 1.13 explorer
289 1.15 tshiozak #ifdef ACPI_BAT_DEBUG
290 1.15 tshiozak ABAT_SET(sc, ABAT_F_VERBOSE);
291 1.15 tshiozak #endif
292 1.1 sommerfe
293 1.14 explorer acpibat_init_envsys(sc);
294 1.1 sommerfe }
295 1.1 sommerfe
296 1.15 tshiozak /*
297 1.15 tshiozak * clear informations
298 1.15 tshiozak */
299 1.15 tshiozak
300 1.15 tshiozak void
301 1.15 tshiozak acpibat_clear_presence(struct acpibat_softc *sc)
302 1.15 tshiozak {
303 1.15 tshiozak
304 1.15 tshiozak ABAT_ASSERT_LOCKED(sc);
305 1.15 tshiozak
306 1.15 tshiozak acpibat_clear_info(sc);
307 1.15 tshiozak sc->sc_available = ABAT_ALV_NONE;
308 1.15 tshiozak ABAT_CLEAR(sc, ABAT_F_PRESENT);
309 1.15 tshiozak }
310 1.15 tshiozak
311 1.15 tshiozak void
312 1.15 tshiozak acpibat_clear_info(struct acpibat_softc *sc)
313 1.15 tshiozak {
314 1.15 tshiozak
315 1.15 tshiozak ABAT_ASSERT_LOCKED(sc);
316 1.15 tshiozak
317 1.15 tshiozak acpibat_clear_stat(sc);
318 1.16 tshiozak if (sc->sc_available>ABAT_ALV_PRESENCE)
319 1.16 tshiozak sc->sc_available = ABAT_ALV_PRESENCE;
320 1.24 mycroft sc->sc_data[ACPIBAT_DCAPACITY].validflags &= ~ENVSYS_FCURVALID;
321 1.28 mycroft sc->sc_data[ACPIBAT_LFCCAPACITY].validflags &= ~ENVSYS_FCURVALID;
322 1.24 mycroft sc->sc_data[ACPIBAT_CAPACITY].validflags &= ~ENVSYS_FMAXVALID;
323 1.24 mycroft sc->sc_data[ACPIBAT_TECHNOLOGY].validflags &= ~ENVSYS_FCURVALID;
324 1.24 mycroft sc->sc_data[ACPIBAT_DVOLTAGE].validflags &= ~ENVSYS_FCURVALID;
325 1.31 mycroft sc->sc_data[ACPIBAT_WCAPACITY].validflags &= ~(ENVSYS_FCURVALID | ENVSYS_FMAXVALID | ENVSYS_FFRACVALID);
326 1.31 mycroft sc->sc_data[ACPIBAT_LCAPACITY].validflags &= ~(ENVSYS_FCURVALID | ENVSYS_FMAXVALID | ENVSYS_FFRACVALID);
327 1.15 tshiozak }
328 1.15 tshiozak
329 1.15 tshiozak void
330 1.15 tshiozak acpibat_clear_stat(struct acpibat_softc *sc)
331 1.1 sommerfe {
332 1.15 tshiozak
333 1.15 tshiozak ABAT_ASSERT_LOCKED(sc);
334 1.15 tshiozak
335 1.16 tshiozak if (sc->sc_available>ABAT_ALV_INFO)
336 1.16 tshiozak sc->sc_available = ABAT_ALV_INFO;
337 1.29 mycroft sc->sc_data[ACPIBAT_CHARGERATE].validflags &= ~ENVSYS_FCURVALID;
338 1.29 mycroft sc->sc_data[ACPIBAT_DISCHARGERATE].validflags &= ~ENVSYS_FCURVALID;
339 1.24 mycroft sc->sc_data[ACPIBAT_CAPACITY].validflags &= ~(ENVSYS_FCURVALID | ENVSYS_FFRACVALID);
340 1.24 mycroft sc->sc_data[ACPIBAT_CAPACITY].warnflags = 0;
341 1.24 mycroft sc->sc_data[ACPIBAT_VOLTAGE].validflags &= ~ENVSYS_FCURVALID;
342 1.29 mycroft sc->sc_data[ACPIBAT_CHARGING].validflags &= ~ENVSYS_FCURVALID;
343 1.24 mycroft sc->sc_data[ACPIBAT_DISCHARGING].validflags &= ~ENVSYS_FCURVALID;
344 1.1 sommerfe }
345 1.1 sommerfe
346 1.15 tshiozak
347 1.13 explorer /*
348 1.13 explorer * returns 0 for no battery, 1 for present, and -1 on error
349 1.13 explorer */
350 1.15 tshiozak int
351 1.15 tshiozak acpibat_battery_present(struct acpibat_softc *sc)
352 1.13 explorer {
353 1.13 explorer u_int32_t sta;
354 1.20 kochi int s, val;
355 1.13 explorer ACPI_STATUS rv;
356 1.13 explorer
357 1.20 kochi rv = acpi_eval_integer(sc->sc_node->ad_handle, "_STA", &val);
358 1.35 mycroft if (ACPI_FAILURE(rv)) {
359 1.34 mycroft printf("%s: failed to evaluate _STA: %s\n",
360 1.34 mycroft sc->sc_dev.dv_xname, AcpiFormatException(rv));
361 1.13 explorer return (-1);
362 1.13 explorer }
363 1.13 explorer
364 1.20 kochi sta = (u_int32_t)val;
365 1.13 explorer
366 1.15 tshiozak ABAT_LOCK(sc, s);
367 1.15 tshiozak sc->sc_available = ABAT_ALV_PRESENCE;
368 1.15 tshiozak if (sta & ACPIBAT_STA_PRESENT) {
369 1.15 tshiozak ABAT_SET(sc, ABAT_F_PRESENT);
370 1.15 tshiozak sc->sc_data[ACPIBAT_PRESENT].cur.data_s = 1;
371 1.15 tshiozak } else
372 1.15 tshiozak sc->sc_data[ACPIBAT_PRESENT].cur.data_s = 0;
373 1.24 mycroft sc->sc_data[ACPIBAT_PRESENT].validflags |= ENVSYS_FCURVALID;
374 1.15 tshiozak ABAT_UNLOCK(sc, s);
375 1.13 explorer
376 1.15 tshiozak return ((sta & ACPIBAT_STA_PRESENT)?1:0);
377 1.13 explorer }
378 1.1 sommerfe
379 1.1 sommerfe /*
380 1.1 sommerfe * acpibat_get_info
381 1.1 sommerfe *
382 1.1 sommerfe * Get, and possibly display, the battery info.
383 1.1 sommerfe */
384 1.1 sommerfe
385 1.15 tshiozak ACPI_STATUS
386 1.15 tshiozak acpibat_get_info(struct acpibat_softc *sc)
387 1.1 sommerfe {
388 1.1 sommerfe ACPI_OBJECT *p1, *p2;
389 1.1 sommerfe ACPI_STATUS rv;
390 1.1 sommerfe ACPI_BUFFER buf;
391 1.15 tshiozak int capunit, rateunit, s;
392 1.13 explorer
393 1.1 sommerfe rv = acpi_eval_struct(sc->sc_node->ad_handle, "_BIF", &buf);
394 1.35 mycroft if (ACPI_FAILURE(rv)) {
395 1.34 mycroft printf("%s: failed to evaluate _BIF: %s\n",
396 1.34 mycroft sc->sc_dev.dv_xname, AcpiFormatException(rv));
397 1.15 tshiozak return (rv);
398 1.1 sommerfe }
399 1.1 sommerfe p1 = (ACPI_OBJECT *)buf.Pointer;
400 1.32 mycroft
401 1.1 sommerfe if (p1->Type != ACPI_TYPE_PACKAGE) {
402 1.1 sommerfe printf("%s: expected PACKAGE, got %d\n", sc->sc_dev.dv_xname,
403 1.1 sommerfe p1->Type);
404 1.1 sommerfe goto out;
405 1.1 sommerfe }
406 1.8 jmcneill if (p1->Package.Count < 13) {
407 1.1 sommerfe printf("%s: expected 13 elts, got %d\n",
408 1.1 sommerfe sc->sc_dev.dv_xname, p1->Package.Count);
409 1.8 jmcneill goto out;
410 1.8 jmcneill }
411 1.32 mycroft p2 = p1->Package.Elements;
412 1.15 tshiozak
413 1.15 tshiozak ABAT_LOCK(sc, s);
414 1.15 tshiozak if ((p2[0].Integer.Value & ACPIBAT_PWRUNIT_MA) != 0) {
415 1.15 tshiozak ABAT_SET(sc, ABAT_F_PWRUNIT_MA);
416 1.15 tshiozak sc->sc_sysmon.sme_ranges = acpibat_range_amp;
417 1.15 tshiozak capunit = ENVSYS_SAMPHOUR;
418 1.15 tshiozak rateunit = ENVSYS_SAMPS;
419 1.15 tshiozak } else {
420 1.15 tshiozak ABAT_CLEAR(sc, ABAT_F_PWRUNIT_MA);
421 1.15 tshiozak sc->sc_sysmon.sme_ranges = acpibat_range_watt;
422 1.15 tshiozak capunit = ENVSYS_SWATTHOUR;
423 1.15 tshiozak rateunit = ENVSYS_SWATTS;
424 1.15 tshiozak }
425 1.32 mycroft
426 1.32 mycroft #define INITDATA(index, unit) \
427 1.32 mycroft sc->sc_data[index].units = unit; \
428 1.32 mycroft sc->sc_info[index].units = unit;
429 1.32 mycroft
430 1.32 mycroft INITDATA(ACPIBAT_DCAPACITY, capunit);
431 1.32 mycroft INITDATA(ACPIBAT_LFCCAPACITY, capunit);
432 1.32 mycroft INITDATA(ACPIBAT_WCAPACITY, capunit);
433 1.32 mycroft INITDATA(ACPIBAT_LCAPACITY, capunit);
434 1.32 mycroft INITDATA(ACPIBAT_CHARGERATE, rateunit);
435 1.32 mycroft INITDATA(ACPIBAT_DISCHARGERATE, rateunit);
436 1.32 mycroft INITDATA(ACPIBAT_CAPACITY, capunit);
437 1.32 mycroft
438 1.32 mycroft #undef INITDATA
439 1.1 sommerfe
440 1.14 explorer sc->sc_data[ACPIBAT_DCAPACITY].cur.data_s = p2[1].Integer.Value * 1000;
441 1.24 mycroft sc->sc_data[ACPIBAT_DCAPACITY].validflags |= ENVSYS_FCURVALID;
442 1.14 explorer sc->sc_data[ACPIBAT_LFCCAPACITY].cur.data_s = p2[2].Integer.Value * 1000;
443 1.28 mycroft sc->sc_data[ACPIBAT_LFCCAPACITY].validflags |= ENVSYS_FCURVALID;
444 1.28 mycroft sc->sc_data[ACPIBAT_CAPACITY].max.data_s = p2[2].Integer.Value * 1000;
445 1.24 mycroft sc->sc_data[ACPIBAT_CAPACITY].validflags |= ENVSYS_FMAXVALID;
446 1.14 explorer sc->sc_data[ACPIBAT_TECHNOLOGY].cur.data_s = p2[3].Integer.Value;
447 1.24 mycroft sc->sc_data[ACPIBAT_TECHNOLOGY].validflags |= ENVSYS_FCURVALID;
448 1.14 explorer sc->sc_data[ACPIBAT_DVOLTAGE].cur.data_s = p2[4].Integer.Value * 1000;
449 1.24 mycroft sc->sc_data[ACPIBAT_DVOLTAGE].validflags |= ENVSYS_FCURVALID;
450 1.14 explorer sc->sc_data[ACPIBAT_WCAPACITY].cur.data_s = p2[5].Integer.Value * 1000;
451 1.31 mycroft sc->sc_data[ACPIBAT_WCAPACITY].max.data_s = p2[2].Integer.Value * 1000;
452 1.31 mycroft sc->sc_data[ACPIBAT_WCAPACITY].validflags |= ENVSYS_FCURVALID | ENVSYS_FMAXVALID | ENVSYS_FFRACVALID;
453 1.14 explorer sc->sc_data[ACPIBAT_LCAPACITY].cur.data_s = p2[6].Integer.Value * 1000;
454 1.31 mycroft sc->sc_data[ACPIBAT_LCAPACITY].max.data_s = p2[2].Integer.Value * 1000;
455 1.31 mycroft sc->sc_data[ACPIBAT_LCAPACITY].validflags |= ENVSYS_FCURVALID | ENVSYS_FMAXVALID | ENVSYS_FFRACVALID;
456 1.15 tshiozak sc->sc_available = ABAT_ALV_INFO;
457 1.15 tshiozak ABAT_UNLOCK(sc, s);
458 1.15 tshiozak
459 1.32 mycroft printf("%s: battery info: %s, %s, %s, %s\n", sc->sc_dev.dv_xname,
460 1.32 mycroft p2[12].String.Pointer, p2[11].String.Pointer,
461 1.32 mycroft p2[9].String.Pointer, p2[10].String.Pointer);
462 1.1 sommerfe
463 1.15 tshiozak rv = AE_OK;
464 1.1 sommerfe
465 1.1 sommerfe out:
466 1.1 sommerfe AcpiOsFree(buf.Pointer);
467 1.15 tshiozak return (rv);
468 1.1 sommerfe }
469 1.1 sommerfe
470 1.1 sommerfe /*
471 1.1 sommerfe * acpibat_get_status:
472 1.1 sommerfe *
473 1.1 sommerfe * Get, and possibly display, the current battery line status.
474 1.1 sommerfe */
475 1.15 tshiozak ACPI_STATUS
476 1.15 tshiozak acpibat_get_status(struct acpibat_softc *sc)
477 1.1 sommerfe {
478 1.15 tshiozak int flags, status, s;
479 1.1 sommerfe ACPI_OBJECT *p1, *p2;
480 1.1 sommerfe ACPI_STATUS rv;
481 1.1 sommerfe ACPI_BUFFER buf;
482 1.1 sommerfe
483 1.20 kochi rv = acpi_eval_struct(sc->sc_node->ad_handle, "_BST", &buf);
484 1.35 mycroft if (ACPI_FAILURE(rv)) {
485 1.34 mycroft printf("%s: failed to evaluate _BST: %s\n",
486 1.34 mycroft sc->sc_dev.dv_xname, AcpiFormatException(rv));
487 1.15 tshiozak return (rv);
488 1.1 sommerfe }
489 1.1 sommerfe p1 = (ACPI_OBJECT *)buf.Pointer;
490 1.1 sommerfe
491 1.1 sommerfe if (p1->Type != ACPI_TYPE_PACKAGE) {
492 1.1 sommerfe printf("bat: expected PACKAGE, got %d\n", p1->Type);
493 1.20 kochi rv = AE_ERROR;
494 1.20 kochi goto out;
495 1.1 sommerfe }
496 1.10 jmcneill if (p1->Package.Count < 4) {
497 1.1 sommerfe printf("bat: expected 4 elts, got %d\n", p1->Package.Count);
498 1.20 kochi rv = AE_ERROR;
499 1.20 kochi goto out;
500 1.10 jmcneill }
501 1.1 sommerfe p2 = p1->Package.Elements;
502 1.1 sommerfe
503 1.15 tshiozak ABAT_LOCK(sc, s);
504 1.15 tshiozak status = p2[0].Integer.Value;
505 1.29 mycroft sc->sc_data[ACPIBAT_CHARGERATE].validflags &= ~ENVSYS_FCURVALID;
506 1.29 mycroft sc->sc_data[ACPIBAT_DISCHARGERATE].validflags &= ~ENVSYS_FCURVALID;
507 1.29 mycroft if (p2[1].Integer.Value != -1) {
508 1.30 mycroft if (status & ACPIBAT_ST_CHARGING) {
509 1.29 mycroft sc->sc_data[ACPIBAT_CHARGERATE].cur.data_s = p2[1].Integer.Value * 1000;
510 1.29 mycroft sc->sc_data[ACPIBAT_CHARGERATE].validflags |= ENVSYS_FCURVALID;
511 1.30 mycroft } else if (status & ACPIBAT_ST_DISCHARGING) {
512 1.29 mycroft sc->sc_data[ACPIBAT_DISCHARGERATE].cur.data_s = p2[1].Integer.Value * 1000;
513 1.29 mycroft sc->sc_data[ACPIBAT_DISCHARGERATE].validflags |= ENVSYS_FCURVALID;
514 1.29 mycroft }
515 1.29 mycroft }
516 1.14 explorer sc->sc_data[ACPIBAT_CAPACITY].cur.data_s = p2[2].Integer.Value * 1000;
517 1.24 mycroft sc->sc_data[ACPIBAT_CAPACITY].validflags |= ENVSYS_FCURVALID | ENVSYS_FFRACVALID;
518 1.14 explorer sc->sc_data[ACPIBAT_VOLTAGE].cur.data_s = p2[3].Integer.Value * 1000;
519 1.24 mycroft sc->sc_data[ACPIBAT_VOLTAGE].validflags |= ENVSYS_FCURVALID;
520 1.14 explorer flags = 0;
521 1.15 tshiozak if (sc->sc_data[ACPIBAT_CAPACITY].cur.data_s <
522 1.15 tshiozak sc->sc_data[ACPIBAT_WCAPACITY].cur.data_s)
523 1.14 explorer flags |= ENVSYS_WARN_UNDER;
524 1.15 tshiozak if (status & ACPIBAT_ST_CRITICAL)
525 1.14 explorer flags |= ENVSYS_WARN_CRITUNDER;
526 1.14 explorer sc->sc_data[ACPIBAT_CAPACITY].warnflags = flags;
527 1.29 mycroft sc->sc_data[ACPIBAT_CHARGING].cur.data_s =
528 1.29 mycroft ((status & ACPIBAT_ST_CHARGING) != 0);
529 1.29 mycroft sc->sc_data[ACPIBAT_CHARGING].validflags |= ENVSYS_FCURVALID;
530 1.15 tshiozak sc->sc_data[ACPIBAT_DISCHARGING].cur.data_s =
531 1.15 tshiozak ((status & ACPIBAT_ST_DISCHARGING) != 0);
532 1.24 mycroft sc->sc_data[ACPIBAT_DISCHARGING].validflags |= ENVSYS_FCURVALID;
533 1.15 tshiozak sc->sc_available = ABAT_ALV_STAT;
534 1.15 tshiozak ABAT_UNLOCK(sc, s);
535 1.15 tshiozak
536 1.20 kochi rv = AE_OK;
537 1.32 mycroft
538 1.32 mycroft out:
539 1.20 kochi AcpiOsFree(buf.Pointer);
540 1.20 kochi return (rv);
541 1.15 tshiozak }
542 1.15 tshiozak
543 1.15 tshiozak #define SCALE(x) ((x)/1000000), (((x)%1000000)/1000)
544 1.22 gson #define CAPUNITS(sc) (ABAT_ISSET((sc), ABAT_F_PWRUNIT_MA)?"Ah":"Wh")
545 1.22 gson #define RATEUNITS(sc) (ABAT_ISSET((sc), ABAT_F_PWRUNIT_MA)?"A":"W")
546 1.15 tshiozak static void
547 1.15 tshiozak acpibat_print_info(struct acpibat_softc *sc)
548 1.15 tshiozak {
549 1.15 tshiozak const char *tech;
550 1.15 tshiozak
551 1.15 tshiozak if (sc->sc_data[ACPIBAT_TECHNOLOGY].cur.data_s)
552 1.15 tshiozak tech = "secondary";
553 1.15 tshiozak else
554 1.15 tshiozak tech = "primary";
555 1.15 tshiozak
556 1.34 mycroft printf("%s: %s battery, Design %d.%03d%s, Last full %d.%03d%s"
557 1.15 tshiozak "Warn %d.%03d%s Low %d.%03d%s\n",
558 1.15 tshiozak sc->sc_dev.dv_xname, tech,
559 1.15 tshiozak SCALE(sc->sc_data[ACPIBAT_DCAPACITY].cur.data_s), CAPUNITS(sc),
560 1.15 tshiozak SCALE(sc->sc_data[ACPIBAT_LFCCAPACITY].cur.data_s),CAPUNITS(sc),
561 1.15 tshiozak SCALE(sc->sc_data[ACPIBAT_WCAPACITY].cur.data_s), CAPUNITS(sc),
562 1.15 tshiozak SCALE(sc->sc_data[ACPIBAT_LCAPACITY].cur.data_s), CAPUNITS(sc));
563 1.15 tshiozak }
564 1.15 tshiozak
565 1.15 tshiozak static void
566 1.15 tshiozak acpibat_print_stat(struct acpibat_softc *sc)
567 1.15 tshiozak {
568 1.15 tshiozak const char *capstat, *chargestat;
569 1.21 gson int percent, denom;
570 1.20 kochi
571 1.20 kochi percent = 0;
572 1.15 tshiozak
573 1.15 tshiozak if (sc->sc_data[ACPIBAT_CAPACITY].warnflags&ENVSYS_WARN_CRITUNDER)
574 1.15 tshiozak capstat = "CRITICAL ";
575 1.15 tshiozak else if (sc->sc_data[ACPIBAT_CAPACITY].warnflags&ENVSYS_WARN_UNDER)
576 1.15 tshiozak capstat = "UNDER ";
577 1.15 tshiozak else
578 1.15 tshiozak capstat = "";
579 1.15 tshiozak if (sc->sc_data[ACPIBAT_CHARGING].cur.data_s)
580 1.15 tshiozak chargestat = "charging";
581 1.15 tshiozak else if (sc->sc_data[ACPIBAT_DISCHARGING].cur.data_s)
582 1.15 tshiozak chargestat = "discharging";
583 1.15 tshiozak else
584 1.15 tshiozak chargestat = "idling";
585 1.21 gson denom = sc->sc_data[ACPIBAT_DCAPACITY].cur.data_s / 100;
586 1.21 gson if (denom > 0)
587 1.21 gson percent = (sc->sc_data[ACPIBAT_CAPACITY].cur.data_s) / denom;
588 1.15 tshiozak printf("%s: %s%s: %d.%03dV cap %d.%03d%s (%d%%) rate %d.%03d%s\n",
589 1.15 tshiozak sc->sc_dev.dv_xname,
590 1.15 tshiozak capstat, chargestat,
591 1.15 tshiozak SCALE(sc->sc_data[ACPIBAT_VOLTAGE].cur.data_s),
592 1.15 tshiozak SCALE(sc->sc_data[ACPIBAT_CAPACITY].cur.data_s), CAPUNITS(sc),
593 1.15 tshiozak percent,
594 1.29 mycroft SCALE(sc->sc_data[ACPIBAT_CHARGING].cur.data_s ?
595 1.29 mycroft sc->sc_data[ACPIBAT_CHARGERATE].cur.data_s :
596 1.29 mycroft sc->sc_data[ACPIBAT_DISCHARGING].cur.data_s ?
597 1.29 mycroft sc->sc_data[ACPIBAT_DISCHARGERATE].cur.data_s : 0),
598 1.29 mycroft RATEUNITS(sc));
599 1.15 tshiozak }
600 1.15 tshiozak
601 1.15 tshiozak static void
602 1.15 tshiozak acpibat_update(void *arg)
603 1.15 tshiozak {
604 1.15 tshiozak struct acpibat_softc *sc = arg;
605 1.15 tshiozak
606 1.15 tshiozak if (sc->sc_available < ABAT_ALV_INFO) {
607 1.15 tshiozak /* current information is invalid */
608 1.16 tshiozak #if 0
609 1.16 tshiozak /*
610 1.16 tshiozak * XXX: The driver sometimes unaware that the battery exist.
611 1.16 tshiozak * (i.e. just after the boot or resuming)
612 1.16 tshiozak * Thus, the driver should always check it here.
613 1.16 tshiozak */
614 1.15 tshiozak if (sc->sc_available < ABAT_ALV_PRESENCE)
615 1.16 tshiozak #endif
616 1.15 tshiozak /* presence is invalid */
617 1.16 tshiozak if (acpibat_battery_present(sc)<0) {
618 1.15 tshiozak /* error */
619 1.16 tshiozak printf("%s: cannot get battery presence.\n",
620 1.16 tshiozak sc->sc_dev.dv_xname);
621 1.15 tshiozak return;
622 1.16 tshiozak }
623 1.15 tshiozak if (ABAT_ISSET(sc, ABAT_F_PRESENT)) {
624 1.15 tshiozak /* the battery is present. */
625 1.15 tshiozak if (ABAT_ISSET(sc, ABAT_F_VERBOSE))
626 1.15 tshiozak printf("%s: battery is present.\n",
627 1.15 tshiozak sc->sc_dev.dv_xname);
628 1.15 tshiozak if (ACPI_FAILURE(acpibat_get_info(sc)))
629 1.15 tshiozak return;
630 1.15 tshiozak if (ABAT_ISSET(sc, ABAT_F_VERBOSE))
631 1.15 tshiozak acpibat_print_info(sc);
632 1.15 tshiozak } else {
633 1.15 tshiozak /* the battery is not present. */
634 1.15 tshiozak if (ABAT_ISSET(sc, ABAT_F_VERBOSE))
635 1.15 tshiozak printf("%s: battery is not present.\n",
636 1.15 tshiozak sc->sc_dev.dv_xname);
637 1.15 tshiozak return;
638 1.15 tshiozak }
639 1.15 tshiozak } else {
640 1.15 tshiozak /* current information is valid */
641 1.16 tshiozak if (!ABAT_ISSET(sc, ABAT_F_PRESENT)) {
642 1.15 tshiozak /* the battery is not present. */
643 1.15 tshiozak return;
644 1.16 tshiozak }
645 1.15 tshiozak }
646 1.15 tshiozak
647 1.15 tshiozak if (ACPI_FAILURE(acpibat_get_status(sc)))
648 1.15 tshiozak return;
649 1.15 tshiozak
650 1.15 tshiozak if (ABAT_ISSET(sc, ABAT_F_VERBOSE))
651 1.15 tshiozak acpibat_print_stat(sc);
652 1.1 sommerfe }
653 1.1 sommerfe
654 1.1 sommerfe /*
655 1.1 sommerfe * acpibat_notify_handler:
656 1.1 sommerfe *
657 1.1 sommerfe * Callback from ACPI interrupt handler to notify us of an event.
658 1.1 sommerfe */
659 1.1 sommerfe void
660 1.1 sommerfe acpibat_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
661 1.1 sommerfe {
662 1.1 sommerfe struct acpibat_softc *sc = context;
663 1.15 tshiozak int rv, s;
664 1.1 sommerfe
665 1.11 explorer #ifdef ACPI_BAT_DEBUG
666 1.11 explorer printf("%s: received notify message: 0x%x\n",
667 1.11 explorer sc->sc_dev.dv_xname, notify);
668 1.11 explorer #endif
669 1.11 explorer
670 1.1 sommerfe switch (notify) {
671 1.1 sommerfe case ACPI_NOTIFY_BusCheck:
672 1.11 explorer break;
673 1.11 explorer
674 1.11 explorer case ACPI_NOTIFY_BatteryInformationChanged:
675 1.15 tshiozak ABAT_LOCK(sc, s);
676 1.15 tshiozak acpibat_clear_presence(sc);
677 1.15 tshiozak ABAT_UNLOCK(sc, s);
678 1.11 explorer rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
679 1.15 tshiozak acpibat_update, sc);
680 1.35 mycroft if (ACPI_FAILURE(rv))
681 1.34 mycroft printf("%s: unable to queue status check: %s\n",
682 1.34 mycroft sc->sc_dev.dv_xname, AcpiFormatException(rv));
683 1.13 explorer break;
684 1.11 explorer
685 1.1 sommerfe case ACPI_NOTIFY_BatteryStatusChanged:
686 1.15 tshiozak ABAT_LOCK(sc, s);
687 1.15 tshiozak acpibat_clear_stat(sc);
688 1.15 tshiozak ABAT_UNLOCK(sc, s);
689 1.1 sommerfe rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
690 1.15 tshiozak acpibat_update, sc);
691 1.35 mycroft if (ACPI_FAILURE(rv))
692 1.34 mycroft printf("%s: unable to queue status check: %s\n",
693 1.34 mycroft sc->sc_dev.dv_xname, AcpiFormatException(rv));
694 1.1 sommerfe break;
695 1.11 explorer
696 1.1 sommerfe default:
697 1.1 sommerfe printf("%s: received unknown notify message: 0x%x\n",
698 1.11 explorer sc->sc_dev.dv_xname, notify);
699 1.1 sommerfe }
700 1.14 explorer }
701 1.14 explorer
702 1.15 tshiozak void
703 1.14 explorer acpibat_init_envsys(struct acpibat_softc *sc)
704 1.14 explorer {
705 1.32 mycroft int capunit, rateunit;
706 1.14 explorer
707 1.15 tshiozak #if 0
708 1.14 explorer if (sc->sc_flags & ABAT_F_PWRUNIT_MA) {
709 1.15 tshiozak #endif
710 1.15 tshiozak /* XXX */
711 1.14 explorer sc->sc_sysmon.sme_ranges = acpibat_range_amp;
712 1.14 explorer capunit = ENVSYS_SAMPHOUR;
713 1.14 explorer rateunit = ENVSYS_SAMPS;
714 1.15 tshiozak #if 0
715 1.14 explorer } else {
716 1.14 explorer sc->sc_sysmon.sme_ranges = acpibat_range_watt;
717 1.14 explorer capunit = ENVSYS_SWATTHOUR;
718 1.14 explorer rateunit = ENVSYS_SWATTS;
719 1.14 explorer }
720 1.15 tshiozak #endif
721 1.14 explorer
722 1.32 mycroft #define INITDATA(index, unit, string) \
723 1.32 mycroft sc->sc_data[index].sensor = index; \
724 1.32 mycroft sc->sc_data[index].units = unit; \
725 1.32 mycroft sc->sc_data[index].validflags = ENVSYS_FVALID; \
726 1.32 mycroft sc->sc_data[index].warnflags = 0; \
727 1.32 mycroft sc->sc_info[index].sensor = index; \
728 1.32 mycroft sc->sc_info[index].units = unit; \
729 1.32 mycroft sc->sc_info[index].validflags = ENVSYS_FVALID; \
730 1.32 mycroft snprintf(sc->sc_info[index].desc, sizeof(sc->sc_info->desc), \
731 1.32 mycroft "%s %s", sc->sc_dev.dv_xname, string); \
732 1.32 mycroft
733 1.15 tshiozak INITDATA(ACPIBAT_PRESENT, ENVSYS_INDICATOR, "present");
734 1.14 explorer INITDATA(ACPIBAT_DCAPACITY, capunit, "design cap");
735 1.28 mycroft INITDATA(ACPIBAT_LFCCAPACITY, capunit, "last full cap");
736 1.14 explorer INITDATA(ACPIBAT_TECHNOLOGY, ENVSYS_INTEGER, "technology");
737 1.14 explorer INITDATA(ACPIBAT_DVOLTAGE, ENVSYS_SVOLTS_DC, "design voltage");
738 1.14 explorer INITDATA(ACPIBAT_WCAPACITY, capunit, "warn cap");
739 1.14 explorer INITDATA(ACPIBAT_LCAPACITY, capunit, "low cap");
740 1.14 explorer INITDATA(ACPIBAT_VOLTAGE, ENVSYS_SVOLTS_DC, "voltage");
741 1.29 mycroft INITDATA(ACPIBAT_CHARGERATE, rateunit, "charge rate");
742 1.29 mycroft INITDATA(ACPIBAT_DISCHARGERATE, rateunit, "discharge rate");
743 1.29 mycroft INITDATA(ACPIBAT_CAPACITY, capunit, "charge");
744 1.14 explorer INITDATA(ACPIBAT_CHARGING, ENVSYS_INDICATOR, "charging");
745 1.14 explorer INITDATA(ACPIBAT_DISCHARGING, ENVSYS_INDICATOR, "discharging");
746 1.32 mycroft
747 1.32 mycroft #undef INITDATA
748 1.14 explorer
749 1.14 explorer sc->sc_sysmon.sme_sensor_info = sc->sc_info;
750 1.14 explorer sc->sc_sysmon.sme_sensor_data = sc->sc_data;
751 1.14 explorer sc->sc_sysmon.sme_cookie = sc;
752 1.14 explorer sc->sc_sysmon.sme_gtredata = acpibat_gtredata;
753 1.14 explorer sc->sc_sysmon.sme_streinfo = acpibat_streinfo;
754 1.14 explorer sc->sc_sysmon.sme_nsensors = ACPIBAT_NSENSORS;
755 1.14 explorer sc->sc_sysmon.sme_envsys_version = 1000;
756 1.14 explorer
757 1.23 mycroft sc->sc_updateinterval.tv_sec = 1;
758 1.23 mycroft sc->sc_updateinterval.tv_usec = 0;
759 1.23 mycroft
760 1.14 explorer if (sysmon_envsys_register(&sc->sc_sysmon))
761 1.14 explorer printf("%s: unable to register with sysmon\n",
762 1.14 explorer sc->sc_dev.dv_xname);
763 1.14 explorer }
764 1.14 explorer
765 1.14 explorer int
766 1.14 explorer acpibat_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
767 1.14 explorer {
768 1.14 explorer struct acpibat_softc *sc = sme->sme_cookie;
769 1.14 explorer
770 1.23 mycroft if (ratecheck(&sc->sc_lastupdate, &sc->sc_updateinterval))
771 1.23 mycroft acpibat_update(sc);
772 1.15 tshiozak
773 1.14 explorer /* XXX locking */
774 1.14 explorer *tred = sc->sc_data[tred->sensor];
775 1.14 explorer /* XXX locking */
776 1.14 explorer
777 1.14 explorer return (0);
778 1.14 explorer }
779 1.14 explorer
780 1.14 explorer int
781 1.14 explorer acpibat_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
782 1.14 explorer {
783 1.14 explorer
784 1.14 explorer /* XXX Not implemented */
785 1.14 explorer binfo->validflags = 0;
786 1.14 explorer
787 1.14 explorer return (0);
788 1.1 sommerfe }
789