xref: /netbsd-src/sys/dev/acpi/acpi_bat.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /*	$NetBSD: acpi_bat.c,v 1.117 2021/01/29 15:20:13 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum of By Noon Software, Inc.
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright 2001 Bill Sommerfeld.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *	This product includes software developed for the NetBSD Project by
47  *	Wasabi Systems, Inc.
48  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
49  *    or promote products derived from this software without specific prior
50  *    written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
56  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62  * POSSIBILITY OF SUCH DAMAGE.
63  */
64 
65 /*
66  * ACPI Battery Driver.
67  *
68  * ACPI defines two different battery device interfaces: "Control
69  * Method" batteries, in which AML methods are defined in order to get
70  * battery status and set battery alarm thresholds, and a "Smart
71  * Battery" device, which is an SMbus device accessed through the ACPI
72  * Embedded Controller device.
73  *
74  * This driver is for the "Control Method"-style battery only.
75  */
76 
77 #include <sys/cdefs.h>
78 __KERNEL_RCSID(0, "$NetBSD: acpi_bat.c,v 1.117 2021/01/29 15:20:13 thorpej Exp $");
79 
80 #include <sys/param.h>
81 #include <sys/condvar.h>
82 #include <sys/device.h>
83 #include <sys/kernel.h>
84 #include <sys/kmem.h>
85 #include <sys/module.h>
86 #include <sys/mutex.h>
87 #include <sys/systm.h>
88 
89 #include <dev/acpi/acpireg.h>
90 #include <dev/acpi/acpivar.h>
91 
92 #define _COMPONENT		 ACPI_BAT_COMPONENT
93 ACPI_MODULE_NAME		 ("acpi_bat")
94 
95 #define	ACPI_NOTIFY_BAT_STATUS	 0x80
96 #define	ACPI_NOTIFY_BAT_INFO	 0x81
97 
98 /*
99  * Sensor indexes.
100  */
101 enum {
102 	ACPIBAT_PRESENT		 = 0,
103 	ACPIBAT_DVOLTAGE	 = 1,
104 	ACPIBAT_VOLTAGE		 = 2,
105 	ACPIBAT_DCAPACITY	 = 3,
106 	ACPIBAT_LFCCAPACITY	 = 4,
107 	ACPIBAT_CAPACITY	 = 5,
108 	ACPIBAT_CHARGERATE	 = 6,
109 	ACPIBAT_DISCHARGERATE	 = 7,
110 	ACPIBAT_CHARGING	 = 8,
111 	ACPIBAT_CHARGE_STATE	 = 9,
112 	ACPIBAT_COUNT		 = 10
113 };
114 
115 /*
116  * Battery Information, _BIF
117  * (ACPI 3.0, sec. 10.2.2.1).
118  */
119 enum {
120 	ACPIBAT_BIF_UNIT	 = 0,
121 	ACPIBAT_BIF_DCAPACITY	 = 1,
122 	ACPIBAT_BIF_LFCCAPACITY	 = 2,
123 	ACPIBAT_BIF_TECHNOLOGY	 = 3,
124 	ACPIBAT_BIF_DVOLTAGE	 = 4,
125 	ACPIBAT_BIF_WCAPACITY	 = 5,
126 	ACPIBAT_BIF_LCAPACITY	 = 6,
127 	ACPIBAT_BIF_GRANULARITY1 = 7,
128 	ACPIBAT_BIF_GRANULARITY2 = 8,
129 	ACPIBAT_BIF_MODEL	 = 9,
130 	ACPIBAT_BIF_SERIAL	 = 10,
131 	ACPIBAT_BIF_TYPE	 = 11,
132 	ACPIBAT_BIF_OEM		 = 12,
133 	ACPIBAT_BIF_COUNT	 = 13
134 };
135 
136 /*
137  * Battery Status, _BST
138  * (ACPI 3.0, sec. 10.2.2.3).
139  */
140 enum {
141 	ACPIBAT_BST_STATE	 = 0,
142 	ACPIBAT_BST_RATE	 = 1,
143 	ACPIBAT_BST_CAPACITY	 = 2,
144 	ACPIBAT_BST_VOLTAGE	 = 3,
145 	ACPIBAT_BST_COUNT	 = 4
146 };
147 
148 struct acpibat_softc {
149 	struct acpi_devnode	*sc_node;
150 	struct sysmon_envsys	*sc_sme;
151 	struct timeval		 sc_last;
152 	envsys_data_t		*sc_sensor;
153 	kmutex_t		 sc_mutex;
154 	kcondvar_t		 sc_condvar;
155 	int32_t			 sc_dcapacity;
156 	int32_t			 sc_dvoltage;
157 	int32_t			 sc_lcapacity;
158 	int32_t			 sc_wcapacity;
159 	int                      sc_present;
160 };
161 
162 static const struct device_compatible_entry compat_data[] = {
163 	{ .compat = "PNP0C0A" },
164 	DEVICE_COMPAT_EOL
165 };
166 
167 #define ACPIBAT_PWRUNIT_MA	0x00000001  /* mA not mW */
168 #define ACPIBAT_ST_DISCHARGING	0x00000001  /* battery is discharging */
169 #define ACPIBAT_ST_CHARGING	0x00000002  /* battery is charging */
170 #define ACPIBAT_ST_CRITICAL	0x00000004  /* battery is critical */
171 
172 /*
173  * A value used when _BST or _BIF is temporarily unknown.
174  */
175 #define ACPIBAT_VAL_UNKNOWN	0xFFFFFFFF
176 
177 #define ACPIBAT_VAL_ISVALID(x)						      \
178 	(((x) != ACPIBAT_VAL_UNKNOWN) ? ENVSYS_SVALID : ENVSYS_SINVALID)
179 
180 static int	    acpibat_match(device_t, cfdata_t, void *);
181 static void	    acpibat_attach(device_t, device_t, void *);
182 static int	    acpibat_detach(device_t, int);
183 static int          acpibat_get_sta(device_t);
184 static ACPI_OBJECT *acpibat_get_object(ACPI_HANDLE, const char *, uint32_t);
185 static void         acpibat_get_info(device_t);
186 static void	    acpibat_print_info(device_t, ACPI_OBJECT *);
187 static void         acpibat_get_status(device_t);
188 static void         acpibat_update_info(void *);
189 static void         acpibat_update_status(void *);
190 static void         acpibat_init_envsys(device_t);
191 static void         acpibat_notify_handler(ACPI_HANDLE, uint32_t, void *);
192 static void         acpibat_refresh(struct sysmon_envsys *, envsys_data_t *);
193 static bool	    acpibat_resume(device_t, const pmf_qual_t *);
194 static void	    acpibat_get_limits(struct sysmon_envsys *, envsys_data_t *,
195 				       sysmon_envsys_lim_t *, uint32_t *);
196 
197 CFATTACH_DECL_NEW(acpibat, sizeof(struct acpibat_softc),
198     acpibat_match, acpibat_attach, acpibat_detach, NULL);
199 
200 /*
201  * acpibat_match:
202  *
203  *	Autoconfiguration `match' routine.
204  */
205 static int
206 acpibat_match(device_t parent, cfdata_t match, void *aux)
207 {
208 	struct acpi_attach_args *aa = aux;
209 
210 	return acpi_compatible_match(aa, compat_data);
211 }
212 
213 /*
214  * acpibat_attach:
215  *
216  *	Autoconfiguration `attach' routine.
217  */
218 static void
219 acpibat_attach(device_t parent, device_t self, void *aux)
220 {
221 	struct acpibat_softc *sc = device_private(self);
222 	struct acpi_attach_args *aa = aux;
223 	ACPI_HANDLE tmp;
224 	ACPI_STATUS rv;
225 
226 	aprint_naive(": ACPI Battery\n");
227 	aprint_normal(": ACPI Battery\n");
228 
229 	sc->sc_node = aa->aa_node;
230 
231 	sc->sc_present = 0;
232 	sc->sc_dvoltage = 0;
233 	sc->sc_dcapacity = 0;
234 	sc->sc_lcapacity = 0;
235 	sc->sc_wcapacity = 0;
236 
237 	sc->sc_sme = NULL;
238 	sc->sc_sensor = NULL;
239 
240 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
241 	cv_init(&sc->sc_condvar, device_xname(self));
242 
243 	(void)pmf_device_register(self, NULL, acpibat_resume);
244 	(void)acpi_register_notify(sc->sc_node, acpibat_notify_handler);
245 
246 	sc->sc_sensor = kmem_zalloc(ACPIBAT_COUNT *
247 	    sizeof(*sc->sc_sensor), KM_SLEEP);
248 
249 	if (sc->sc_sensor == NULL)
250 		return;
251 
252 	config_interrupts(self, acpibat_init_envsys);
253 
254 	/*
255 	 * If this is ever seen, the driver should be extended.
256 	 */
257 	rv = AcpiGetHandle(sc->sc_node->ad_handle, "_BIX", &tmp);
258 
259 	if (ACPI_SUCCESS(rv))
260 		aprint_verbose_dev(self, "ACPI 4.0 functionality present\n");
261 }
262 
263 /*
264  * acpibat_detach:
265  *
266  *	Autoconfiguration `detach' routine.
267  */
268 static int
269 acpibat_detach(device_t self, int flags)
270 {
271 	struct acpibat_softc *sc = device_private(self);
272 
273 	acpi_deregister_notify(sc->sc_node);
274 
275 	cv_destroy(&sc->sc_condvar);
276 	mutex_destroy(&sc->sc_mutex);
277 
278 	if (sc->sc_sme != NULL)
279 		sysmon_envsys_unregister(sc->sc_sme);
280 
281 	if (sc->sc_sensor != NULL)
282 		kmem_free(sc->sc_sensor, ACPIBAT_COUNT *
283 		    sizeof(*sc->sc_sensor));
284 
285 	pmf_device_deregister(self);
286 
287 	return 0;
288 }
289 
290 /*
291  * acpibat_get_sta:
292  *
293  *	Evaluate whether the battery is present or absent.
294  *
295  *	Returns: 0 for no battery, 1 for present, and -1 on error.
296  */
297 static int
298 acpibat_get_sta(device_t dv)
299 {
300 	struct acpibat_softc *sc = device_private(dv);
301 	ACPI_INTEGER val;
302 	ACPI_STATUS rv;
303 
304 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_STA", &val);
305 
306 	if (ACPI_FAILURE(rv)) {
307 		aprint_error_dev(dv, "failed to evaluate _STA\n");
308 		return -1;
309 	}
310 
311 	sc->sc_sensor[ACPIBAT_PRESENT].state = ENVSYS_SVALID;
312 
313 	if ((val & ACPI_STA_BATTERY_PRESENT) == 0) {
314 		sc->sc_sensor[ACPIBAT_PRESENT].value_cur = 0;
315 		return 0;
316 	}
317 
318 	sc->sc_sensor[ACPIBAT_PRESENT].value_cur = 1;
319 
320 	return 1;
321 }
322 
323 static ACPI_OBJECT *
324 acpibat_get_object(ACPI_HANDLE hdl, const char *pth, uint32_t count)
325 {
326 	ACPI_OBJECT *obj;
327 	ACPI_BUFFER buf;
328 	ACPI_STATUS rv;
329 
330 	rv = acpi_eval_struct(hdl, pth, &buf);
331 
332 	if (ACPI_FAILURE(rv))
333 		return NULL;
334 
335 	obj = buf.Pointer;
336 
337 	if (obj->Type != ACPI_TYPE_PACKAGE) {
338 		ACPI_FREE(buf.Pointer);
339 		return NULL;
340 	}
341 
342 	if (obj->Package.Count != count) {
343 		ACPI_FREE(buf.Pointer);
344 		return NULL;
345 	}
346 
347 	return obj;
348 }
349 
350 /*
351  * acpibat_get_info:
352  *
353  * 	Get the battery info.
354  */
355 static void
356 acpibat_get_info(device_t dv)
357 {
358 	struct acpibat_softc *sc = device_private(dv);
359 	ACPI_HANDLE hdl = sc->sc_node->ad_handle;
360 	ACPI_OBJECT *elm, *obj;
361 	ACPI_STATUS rv = AE_OK;
362 	int capunit, i, rateunit;
363 	uint64_t val;
364 
365 	obj = acpibat_get_object(hdl, "_BIF", ACPIBAT_BIF_COUNT);
366 
367 	if (obj == NULL) {
368 		rv = AE_ERROR;
369 		goto out;
370 	}
371 
372 	elm = obj->Package.Elements;
373 
374 	for (i = ACPIBAT_BIF_UNIT; i < ACPIBAT_BIF_MODEL; i++) {
375 
376 		if (elm[i].Type != ACPI_TYPE_INTEGER) {
377 			rv = AE_TYPE;
378 			goto out;
379 		}
380 
381 		if (elm[i].Integer.Value != ACPIBAT_VAL_UNKNOWN &&
382 		    elm[i].Integer.Value >= INT_MAX) {
383 			rv = AE_LIMIT;
384 			goto out;
385 		}
386 	}
387 
388 	switch (elm[ACPIBAT_BIF_UNIT].Integer.Value) {
389 	case ACPIBAT_PWRUNIT_MA:
390 		capunit = ENVSYS_SAMPHOUR;
391 		rateunit = ENVSYS_SAMPS;
392 		break;
393 	default:
394 		capunit = ENVSYS_SWATTHOUR;
395 		rateunit = ENVSYS_SWATTS;
396 		break;
397 	}
398 
399 	sc->sc_sensor[ACPIBAT_DCAPACITY].units = capunit;
400 	sc->sc_sensor[ACPIBAT_LFCCAPACITY].units = capunit;
401 	sc->sc_sensor[ACPIBAT_CHARGERATE].units = rateunit;
402 	sc->sc_sensor[ACPIBAT_DISCHARGERATE].units = rateunit;
403 	sc->sc_sensor[ACPIBAT_CAPACITY].units = capunit;
404 
405 	/* Design capacity. */
406 	val = elm[ACPIBAT_BIF_DCAPACITY].Integer.Value;
407 	sc->sc_sensor[ACPIBAT_DCAPACITY].value_cur = val * 1000;
408 	sc->sc_sensor[ACPIBAT_DCAPACITY].state = ACPIBAT_VAL_ISVALID(val);
409 
410 	/* Last full charge capacity. */
411 	val = elm[ACPIBAT_BIF_LFCCAPACITY].Integer.Value;
412 	sc->sc_sensor[ACPIBAT_LFCCAPACITY].value_cur = val * 1000;
413 	sc->sc_sensor[ACPIBAT_LFCCAPACITY].state = ACPIBAT_VAL_ISVALID(val);
414 
415 	/* Design voltage. */
416 	val = elm[ACPIBAT_BIF_DVOLTAGE].Integer.Value;
417 	sc->sc_sensor[ACPIBAT_DVOLTAGE].value_cur = val * 1000;
418 	sc->sc_sensor[ACPIBAT_DVOLTAGE].state = ACPIBAT_VAL_ISVALID(val);
419 
420 	/* Design low and warning capacity. */
421 	sc->sc_lcapacity = elm[ACPIBAT_BIF_LCAPACITY].Integer.Value * 1000;
422 	sc->sc_wcapacity = elm[ACPIBAT_BIF_WCAPACITY].Integer.Value * 1000;
423 
424 	/*
425 	 * Initialize the maximum of current capacity
426 	 * to the last known full charge capacity.
427 	 */
428 	val = sc->sc_sensor[ACPIBAT_LFCCAPACITY].value_cur;
429 	sc->sc_sensor[ACPIBAT_CAPACITY].value_max = val;
430 
431 	acpibat_print_info(dv, elm);
432 
433 out:
434 	if (obj != NULL)
435 		ACPI_FREE(obj);
436 
437 	if (ACPI_FAILURE(rv))
438 		aprint_error_dev(dv, "failed to evaluate _BIF: %s\n",
439 		    AcpiFormatException(rv));
440 }
441 
442 /*
443  * acpibat_print_info:
444  *
445  * 	Display the battery info.
446  */
447 static void
448 acpibat_print_info(device_t dv, ACPI_OBJECT *elm)
449 {
450 	struct acpibat_softc *sc = device_private(dv);
451 	const char *tech, *unit;
452 	int32_t dcap, dvol;
453 	int i;
454 
455 	for (i = ACPIBAT_BIF_OEM; i > ACPIBAT_BIF_GRANULARITY2; i--) {
456 
457 		if (elm[i].Type != ACPI_TYPE_STRING)
458 			return;
459 
460 		if (elm[i].String.Pointer == NULL)
461 			return;
462 
463 		if (elm[i].String.Pointer[0] == '\0')
464 			return;
465 	}
466 
467 	dcap = elm[ACPIBAT_BIF_DCAPACITY].Integer.Value;
468 	dvol = elm[ACPIBAT_BIF_DVOLTAGE].Integer.Value;
469 
470 	/*
471 	 * Try to detect whether the battery was switched.
472 	 */
473 	if (sc->sc_dcapacity == dcap && sc->sc_dvoltage == dvol)
474 		return;
475 	else {
476 		sc->sc_dcapacity = dcap;
477 		sc->sc_dvoltage = dvol;
478 	}
479 
480 	tech = (elm[ACPIBAT_BIF_TECHNOLOGY].Integer.Value != 0) ?
481 	    "rechargeable" : "non-rechargeable";
482 
483 	aprint_normal_dev(dv, "%s %s %s battery\n",
484 	    elm[ACPIBAT_BIF_OEM].String.Pointer,
485 	    elm[ACPIBAT_BIF_TYPE].String.Pointer, tech);
486 
487 	aprint_debug_dev(dv, "model number %s, serial number %s\n",
488 	    elm[ACPIBAT_BIF_MODEL].String.Pointer,
489 	    elm[ACPIBAT_BIF_SERIAL].String.Pointer);
490 
491 #define SCALE(x) (((int)x) / 1000000), ((((int)x) % 1000000) / 1000)
492 
493 	/*
494 	 * These values are defined as follows (ACPI 4.0, p. 388):
495 	 *
496 	 * Granularity 1.	"Battery capacity granularity between low
497 	 *			 and warning in [mAh] or [mWh]. That is,
498 	 *			 this is the smallest increment in capacity
499 	 *			 that the battery is capable of measuring."
500 	 *
501 	 * Granularity 2.	"Battery capacity granularity between warning
502 	 *			 and full in [mAh] or [mWh]. [...]"
503 	 */
504 	switch (elm[ACPIBAT_BIF_UNIT].Integer.Value) {
505 	case ACPIBAT_PWRUNIT_MA:
506 		unit = "Ah";
507 		break;
508 	default:
509 		unit = "Wh";
510 		break;
511 	}
512 
513 	aprint_verbose_dev(dv, "granularity: "
514 	    "low->warn %d.%03d %s, warn->full %d.%03d %s\n",
515 	    SCALE(elm[ACPIBAT_BIF_GRANULARITY1].Integer.Value * 1000), unit,
516 	    SCALE(elm[ACPIBAT_BIF_GRANULARITY2].Integer.Value * 1000), unit);
517 }
518 
519 /*
520  * acpibat_get_status:
521  *
522  *	Get the current battery status.
523  */
524 static void
525 acpibat_get_status(device_t dv)
526 {
527 	struct acpibat_softc *sc = device_private(dv);
528 	ACPI_HANDLE hdl = sc->sc_node->ad_handle;
529 	ACPI_OBJECT *elm, *obj;
530 	ACPI_STATUS rv = AE_OK;
531 	int i, rate, state;
532 	uint64_t val;
533 
534 	obj = acpibat_get_object(hdl, "_BST", ACPIBAT_BST_COUNT);
535 
536 	if (obj == NULL) {
537 		rv = AE_ERROR;
538 		goto out;
539 	}
540 
541 	elm = obj->Package.Elements;
542 
543 	for (i = ACPIBAT_BST_STATE; i < ACPIBAT_BST_COUNT; i++) {
544 
545 		if (elm[i].Type != ACPI_TYPE_INTEGER) {
546 			rv = AE_TYPE;
547 			goto out;
548 		}
549 	}
550 
551 	state = elm[ACPIBAT_BST_STATE].Integer.Value;
552 
553 	if ((state & ACPIBAT_ST_CHARGING) != 0) {
554 		/* XXX rate can be invalid */
555 		rate = elm[ACPIBAT_BST_RATE].Integer.Value;
556 		sc->sc_sensor[ACPIBAT_CHARGERATE].state = ENVSYS_SVALID;
557 		sc->sc_sensor[ACPIBAT_CHARGERATE].value_cur = rate * 1000;
558 		sc->sc_sensor[ACPIBAT_DISCHARGERATE].state = ENVSYS_SINVALID;
559 		sc->sc_sensor[ACPIBAT_CHARGING].state = ENVSYS_SVALID;
560 		sc->sc_sensor[ACPIBAT_CHARGING].value_cur = 1;
561 	} else if ((state & ACPIBAT_ST_DISCHARGING) != 0) {
562 		rate = elm[ACPIBAT_BST_RATE].Integer.Value;
563 		sc->sc_sensor[ACPIBAT_DISCHARGERATE].state = ENVSYS_SVALID;
564 		sc->sc_sensor[ACPIBAT_DISCHARGERATE].value_cur = rate * 1000;
565 		sc->sc_sensor[ACPIBAT_CHARGERATE].state = ENVSYS_SINVALID;
566 		sc->sc_sensor[ACPIBAT_CHARGING].state = ENVSYS_SVALID;
567 		sc->sc_sensor[ACPIBAT_CHARGING].value_cur = 0;
568 	} else {
569 		sc->sc_sensor[ACPIBAT_CHARGING].state = ENVSYS_SVALID;
570 		sc->sc_sensor[ACPIBAT_CHARGING].value_cur = 0;
571 		sc->sc_sensor[ACPIBAT_CHARGERATE].state = ENVSYS_SINVALID;
572 		sc->sc_sensor[ACPIBAT_DISCHARGERATE].state = ENVSYS_SINVALID;
573 	}
574 
575 	/* Remaining capacity. */
576 	val = elm[ACPIBAT_BST_CAPACITY].Integer.Value;
577 	sc->sc_sensor[ACPIBAT_CAPACITY].value_cur = val * 1000;
578 	sc->sc_sensor[ACPIBAT_CAPACITY].state = ACPIBAT_VAL_ISVALID(val);
579 
580 	/* Battery voltage. */
581 	val = elm[ACPIBAT_BST_VOLTAGE].Integer.Value;
582 	sc->sc_sensor[ACPIBAT_VOLTAGE].value_cur = val * 1000;
583 	sc->sc_sensor[ACPIBAT_VOLTAGE].state = ACPIBAT_VAL_ISVALID(val);
584 
585 	sc->sc_sensor[ACPIBAT_CHARGE_STATE].state = ENVSYS_SVALID;
586 	sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur =
587 	    ENVSYS_BATTERY_CAPACITY_NORMAL;
588 
589 	if (sc->sc_sensor[ACPIBAT_CAPACITY].value_cur < sc->sc_wcapacity) {
590 		sc->sc_sensor[ACPIBAT_CAPACITY].state = ENVSYS_SWARNUNDER;
591 		sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur =
592 		    ENVSYS_BATTERY_CAPACITY_WARNING;
593 	}
594 
595 	if (sc->sc_sensor[ACPIBAT_CAPACITY].value_cur < sc->sc_lcapacity) {
596 		sc->sc_sensor[ACPIBAT_CAPACITY].state = ENVSYS_SCRITUNDER;
597 		sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur =
598 		    ENVSYS_BATTERY_CAPACITY_LOW;
599 	}
600 
601 	if ((state & ACPIBAT_ST_CRITICAL) != 0) {
602 		sc->sc_sensor[ACPIBAT_CAPACITY].state = ENVSYS_SCRITICAL;
603 		sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur =
604 		    ENVSYS_BATTERY_CAPACITY_CRITICAL;
605 	}
606 
607 out:
608 	if (obj != NULL)
609 		ACPI_FREE(obj);
610 
611 	if (ACPI_FAILURE(rv))
612 		aprint_error_dev(dv, "failed to evaluate _BST: %s\n",
613 		    AcpiFormatException(rv));
614 }
615 
616 static void
617 acpibat_update_info(void *arg)
618 {
619 	device_t dv = arg;
620 	struct acpibat_softc *sc = device_private(dv);
621 	int i, rv;
622 
623 	mutex_enter(&sc->sc_mutex);
624 
625 	rv = acpibat_get_sta(dv);
626 
627 	if (rv > 0) {
628 		acpibat_get_info(dv);
629 
630 		/*
631 		 * If the status changed, update the limits.
632 		 */
633 		if (sc->sc_present == 0 &&
634 		    sc->sc_sensor[ACPIBAT_CAPACITY].value_max > 0)
635 			sysmon_envsys_update_limits(sc->sc_sme,
636 			    &sc->sc_sensor[ACPIBAT_CAPACITY]);
637 	} else {
638 		i = (rv < 0) ? 0 : ACPIBAT_DVOLTAGE;
639 
640 		while (i < ACPIBAT_COUNT) {
641 			sc->sc_sensor[i].state = ENVSYS_SINVALID;
642 			i++;
643 		}
644 	}
645 
646 	sc->sc_present = rv;
647 
648 	mutex_exit(&sc->sc_mutex);
649 }
650 
651 static void
652 acpibat_update_status(void *arg)
653 {
654 	device_t dv = arg;
655 	struct acpibat_softc *sc = device_private(dv);
656 	int i, rv;
657 
658 	mutex_enter(&sc->sc_mutex);
659 
660 	rv = acpibat_get_sta(dv);
661 
662 	if (rv > 0) {
663 
664 		if (sc->sc_present == 0)
665 			acpibat_get_info(dv);
666 
667 		acpibat_get_status(dv);
668 	} else {
669 		i = (rv < 0) ? 0 : ACPIBAT_DVOLTAGE;
670 
671 		while (i < ACPIBAT_COUNT) {
672 			sc->sc_sensor[i].state = ENVSYS_SINVALID;
673 			i++;
674 		}
675 	}
676 
677 	sc->sc_present = rv;
678 	microtime(&sc->sc_last);
679 
680 	cv_broadcast(&sc->sc_condvar);
681 	mutex_exit(&sc->sc_mutex);
682 }
683 
684 /*
685  * acpibat_notify_handler:
686  *
687  *	Callback from ACPI interrupt handler to notify us of an event.
688  */
689 static void
690 acpibat_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
691 {
692 	static const int handler = OSL_NOTIFY_HANDLER;
693 	device_t dv = context;
694 
695 	switch (notify) {
696 
697 	case ACPI_NOTIFY_BUS_CHECK:
698 		break;
699 
700 	case ACPI_NOTIFY_BAT_INFO:
701 	case ACPI_NOTIFY_DEVICE_CHECK:
702 		(void)AcpiOsExecute(handler, acpibat_update_info, dv);
703 		break;
704 
705 	case ACPI_NOTIFY_BAT_STATUS:
706 		(void)AcpiOsExecute(handler, acpibat_update_status, dv);
707 		break;
708 
709 	default:
710 		aprint_error_dev(dv, "unknown notify: 0x%02X\n", notify);
711 	}
712 }
713 
714 static void
715 acpibat_init_envsys(device_t dv)
716 {
717 	struct acpibat_softc *sc = device_private(dv);
718 	int i;
719 
720 #define INITDATA(index, unit, string)					\
721 	do {								\
722 		sc->sc_sensor[index].state = ENVSYS_SVALID;		\
723 		sc->sc_sensor[index].units = unit;			\
724 		(void)strlcpy(sc->sc_sensor[index].desc, string,	\
725 		    sizeof(sc->sc_sensor[index].desc));			\
726 	} while (/* CONSTCOND */ 0)
727 
728 	INITDATA(ACPIBAT_PRESENT, ENVSYS_INDICATOR, "present");
729 	INITDATA(ACPIBAT_DCAPACITY, ENVSYS_SWATTHOUR, "design cap");
730 	INITDATA(ACPIBAT_LFCCAPACITY, ENVSYS_SWATTHOUR, "last full cap");
731 	INITDATA(ACPIBAT_DVOLTAGE, ENVSYS_SVOLTS_DC, "design voltage");
732 	INITDATA(ACPIBAT_VOLTAGE, ENVSYS_SVOLTS_DC, "voltage");
733 	INITDATA(ACPIBAT_CHARGERATE, ENVSYS_SWATTS, "charge rate");
734 	INITDATA(ACPIBAT_DISCHARGERATE, ENVSYS_SWATTS, "discharge rate");
735 	INITDATA(ACPIBAT_CAPACITY, ENVSYS_SWATTHOUR, "charge");
736 	INITDATA(ACPIBAT_CHARGING, ENVSYS_BATTERY_CHARGE, "charging");
737 	INITDATA(ACPIBAT_CHARGE_STATE, ENVSYS_BATTERY_CAPACITY, "charge state");
738 
739 #undef INITDATA
740 
741 	sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur =
742 		ENVSYS_BATTERY_CAPACITY_NORMAL;
743 
744 	sc->sc_sensor[ACPIBAT_CAPACITY].flags |=
745 	    ENVSYS_FPERCENT | ENVSYS_FVALID_MAX | ENVSYS_FMONLIMITS;
746 
747 	sc->sc_sensor[ACPIBAT_CHARGE_STATE].flags |= ENVSYS_FMONSTCHANGED;
748 
749 	/* Disable userland monitoring on these sensors. */
750 	sc->sc_sensor[ACPIBAT_VOLTAGE].flags = ENVSYS_FMONNOTSUPP;
751 	sc->sc_sensor[ACPIBAT_CHARGERATE].flags = ENVSYS_FMONNOTSUPP;
752 	sc->sc_sensor[ACPIBAT_DISCHARGERATE].flags = ENVSYS_FMONNOTSUPP;
753 	sc->sc_sensor[ACPIBAT_DCAPACITY].flags = ENVSYS_FMONNOTSUPP;
754 	sc->sc_sensor[ACPIBAT_LFCCAPACITY].flags = ENVSYS_FMONNOTSUPP;
755 	sc->sc_sensor[ACPIBAT_DVOLTAGE].flags = ENVSYS_FMONNOTSUPP;
756 
757 	/* Attach rnd(9) to the (dis)charge rates. */
758 	sc->sc_sensor[ACPIBAT_CHARGERATE].flags |= ENVSYS_FHAS_ENTROPY;
759 	sc->sc_sensor[ACPIBAT_DISCHARGERATE].flags |= ENVSYS_FHAS_ENTROPY;
760 
761 	sc->sc_sme = sysmon_envsys_create();
762 
763 	for (i = 0; i < ACPIBAT_COUNT; i++) {
764 
765 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
766 			&sc->sc_sensor[i]))
767 			goto fail;
768 	}
769 
770 	sc->sc_sme->sme_name = device_xname(dv);
771 	sc->sc_sme->sme_cookie = dv;
772 	sc->sc_sme->sme_refresh = acpibat_refresh;
773 	sc->sc_sme->sme_class = SME_CLASS_BATTERY;
774 	sc->sc_sme->sme_flags = SME_POLL_ONLY | SME_INIT_REFRESH;
775 	sc->sc_sme->sme_get_limits = acpibat_get_limits;
776 
777 	acpibat_update_info(dv);
778 	acpibat_update_status(dv);
779 
780 	if (sysmon_envsys_register(sc->sc_sme))
781 		goto fail;
782 
783 	return;
784 
785 fail:
786 	aprint_error_dev(dv, "failed to initialize sysmon\n");
787 
788 	sysmon_envsys_destroy(sc->sc_sme);
789 	kmem_free(sc->sc_sensor, ACPIBAT_COUNT * sizeof(*sc->sc_sensor));
790 
791 	sc->sc_sme = NULL;
792 	sc->sc_sensor = NULL;
793 }
794 
795 static void
796 acpibat_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
797 {
798 	device_t self = sme->sme_cookie;
799 	struct acpibat_softc *sc;
800 	struct timeval tv, tmp;
801 	ACPI_STATUS rv;
802 
803 	sc = device_private(self);
804 
805 	tmp.tv_sec = 10;
806 	tmp.tv_usec = 0;
807 
808 	microtime(&tv);
809 	timersub(&tv, &tmp, &tv);
810 
811 	if (timercmp(&tv, &sc->sc_last, <) != 0)
812 		return;
813 
814 	if (mutex_tryenter(&sc->sc_mutex) == 0)
815 		return;
816 
817 	rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, acpibat_update_status, self);
818 
819 	if (ACPI_SUCCESS(rv))
820 		cv_timedwait(&sc->sc_condvar, &sc->sc_mutex, hz);
821 
822 	mutex_exit(&sc->sc_mutex);
823 }
824 
825 static bool
826 acpibat_resume(device_t dv, const pmf_qual_t *qual)
827 {
828 
829 	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpibat_update_info, dv);
830 	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpibat_update_status, dv);
831 
832 	return true;
833 }
834 
835 static void
836 acpibat_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
837     sysmon_envsys_lim_t *limits, uint32_t *props)
838 {
839 	device_t dv = sme->sme_cookie;
840 	struct acpibat_softc *sc = device_private(dv);
841 
842 	if (edata->sensor != ACPIBAT_CAPACITY)
843 		return;
844 
845 	limits->sel_critmin = sc->sc_lcapacity;
846 	limits->sel_warnmin = sc->sc_wcapacity;
847 
848 	*props |= PROP_BATTCAP | PROP_BATTWARN | PROP_DRIVER_LIMITS;
849 }
850 
851 MODULE(MODULE_CLASS_DRIVER, acpibat, "sysmon_envsys");
852 
853 #ifdef _MODULE
854 #include "ioconf.c"
855 #endif
856 
857 static int
858 acpibat_modcmd(modcmd_t cmd, void *aux)
859 {
860 	int rv = 0;
861 
862 	switch (cmd) {
863 
864 	case MODULE_CMD_INIT:
865 
866 #ifdef _MODULE
867 		rv = config_init_component(cfdriver_ioconf_acpibat,
868 		    cfattach_ioconf_acpibat, cfdata_ioconf_acpibat);
869 #endif
870 		break;
871 
872 	case MODULE_CMD_FINI:
873 
874 #ifdef _MODULE
875 		rv = config_fini_component(cfdriver_ioconf_acpibat,
876 		    cfattach_ioconf_acpibat, cfdata_ioconf_acpibat);
877 #endif
878 		break;
879 
880 	default:
881 		rv = ENOTTY;
882 	}
883 
884 	return rv;
885 }
886