1 /* $NetBSD: acpi_bat.c,v 1.121 2022/01/07 01:10:57 riastradh 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.121 2022/01/07 01:10:57 riastradh 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 bool sc_dying; 161 }; 162 163 static const struct device_compatible_entry compat_data[] = { 164 { .compat = "PNP0C0A" }, 165 DEVICE_COMPAT_EOL 166 }; 167 168 #define ACPIBAT_PWRUNIT_MA 0x00000001 /* mA not mW */ 169 #define ACPIBAT_ST_DISCHARGING 0x00000001 /* battery is discharging */ 170 #define ACPIBAT_ST_CHARGING 0x00000002 /* battery is charging */ 171 #define ACPIBAT_ST_CRITICAL 0x00000004 /* battery is critical */ 172 173 /* 174 * A value used when _BST or _BIF is temporarily unknown. 175 */ 176 #define ACPIBAT_VAL_UNKNOWN 0xFFFFFFFF 177 178 #define ACPIBAT_VAL_ISVALID(x) \ 179 (((x) != ACPIBAT_VAL_UNKNOWN) ? ENVSYS_SVALID : ENVSYS_SINVALID) 180 181 static int acpibat_match(device_t, cfdata_t, void *); 182 static void acpibat_attach(device_t, device_t, void *); 183 static int acpibat_detach(device_t, int); 184 static int acpibat_get_sta(device_t); 185 static ACPI_OBJECT *acpibat_get_object(ACPI_HANDLE, const char *, uint32_t); 186 static void acpibat_get_info(device_t); 187 static void acpibat_print_info(device_t, ACPI_OBJECT *); 188 static void acpibat_get_status(device_t); 189 static void acpibat_update_info(void *); 190 static void acpibat_update_status(void *); 191 static void acpibat_init_envsys(device_t); 192 static void acpibat_notify_handler(ACPI_HANDLE, uint32_t, void *); 193 static void acpibat_refresh(struct sysmon_envsys *, envsys_data_t *); 194 static bool acpibat_resume(device_t, const pmf_qual_t *); 195 static void acpibat_get_limits(struct sysmon_envsys *, envsys_data_t *, 196 sysmon_envsys_lim_t *, uint32_t *); 197 198 CFATTACH_DECL_NEW(acpibat, sizeof(struct acpibat_softc), 199 acpibat_match, acpibat_attach, acpibat_detach, NULL); 200 201 /* 202 * acpibat_match: 203 * 204 * Autoconfiguration `match' routine. 205 */ 206 static int 207 acpibat_match(device_t parent, cfdata_t match, void *aux) 208 { 209 struct acpi_attach_args *aa = aux; 210 211 return acpi_compatible_match(aa, compat_data); 212 } 213 214 /* 215 * acpibat_attach: 216 * 217 * Autoconfiguration `attach' routine. 218 */ 219 static void 220 acpibat_attach(device_t parent, device_t self, void *aux) 221 { 222 struct acpibat_softc *sc = device_private(self); 223 struct acpi_attach_args *aa = aux; 224 ACPI_HANDLE tmp; 225 ACPI_STATUS rv; 226 227 aprint_naive(": ACPI Battery\n"); 228 aprint_normal(": ACPI Battery\n"); 229 230 sc->sc_node = aa->aa_node; 231 232 sc->sc_present = 0; 233 sc->sc_dvoltage = 0; 234 sc->sc_dcapacity = 0; 235 sc->sc_lcapacity = 0; 236 sc->sc_wcapacity = 0; 237 238 sc->sc_sme = NULL; 239 240 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE); 241 cv_init(&sc->sc_condvar, device_xname(self)); 242 243 sc->sc_sensor = kmem_zalloc(ACPIBAT_COUNT * 244 sizeof(*sc->sc_sensor), KM_SLEEP); 245 246 config_interrupts(self, acpibat_init_envsys); 247 248 /* 249 * If this is ever seen, the driver should be extended. 250 */ 251 rv = AcpiGetHandle(sc->sc_node->ad_handle, "_BIX", &tmp); 252 if (ACPI_SUCCESS(rv)) 253 aprint_verbose_dev(self, "ACPI 4.0 functionality present\n"); 254 } 255 256 /* 257 * acpibat_detach: 258 * 259 * Autoconfiguration `detach' routine. 260 */ 261 static int 262 acpibat_detach(device_t self, int flags) 263 { 264 struct acpibat_softc *sc = device_private(self); 265 266 /* Prevent further use of sc->sc_sme in acpibat_update_info. */ 267 mutex_enter(&sc->sc_mutex); 268 sc->sc_dying = true; 269 mutex_exit(&sc->sc_mutex); 270 271 /* Prevent further calls to acpibat_resume. */ 272 pmf_device_deregister(self); 273 274 /* Prevent further calls to acpibat_notify_handler. */ 275 acpi_deregister_notify(sc->sc_node); 276 277 /* Detach sensors and prevent further calls to acpibat_refresh. */ 278 if (sc->sc_sme != NULL) 279 sysmon_envsys_unregister(sc->sc_sme); 280 281 /* 282 * Wait for calls to acpibat_update_info/status in case sysmon 283 * envsys refreshed the sensors and queued them but they didn't 284 * run before sysmon_envsys_unregister. After this point, no 285 * asynchronous access to the softc is possible. 286 */ 287 AcpiOsWaitEventsComplete(); 288 289 if (sc->sc_sensor != NULL) 290 kmem_free(sc->sc_sensor, ACPIBAT_COUNT * 291 sizeof(*sc->sc_sensor)); 292 293 cv_destroy(&sc->sc_condvar); 294 mutex_destroy(&sc->sc_mutex); 295 296 return 0; 297 } 298 299 /* 300 * acpibat_get_sta: 301 * 302 * Evaluate whether the battery is present or absent. 303 * 304 * Returns: 0 for no battery, 1 for present, and -1 on error. 305 */ 306 static int 307 acpibat_get_sta(device_t dv) 308 { 309 struct acpibat_softc *sc = device_private(dv); 310 ACPI_INTEGER val; 311 ACPI_STATUS rv; 312 313 rv = acpi_eval_integer(sc->sc_node->ad_handle, "_STA", &val); 314 if (ACPI_FAILURE(rv)) { 315 aprint_error_dev(dv, "failed to evaluate _STA: %s\n", 316 AcpiFormatException(rv)); 317 return -1; 318 } 319 320 sc->sc_sensor[ACPIBAT_PRESENT].state = ENVSYS_SVALID; 321 322 if ((val & ACPI_STA_BATTERY_PRESENT) == 0) { 323 sc->sc_sensor[ACPIBAT_PRESENT].value_cur = 0; 324 return 0; 325 } 326 327 sc->sc_sensor[ACPIBAT_PRESENT].value_cur = 1; 328 329 return 1; 330 } 331 332 static ACPI_OBJECT * 333 acpibat_get_object(ACPI_HANDLE hdl, const char *pth, uint32_t count) 334 { 335 ACPI_OBJECT *obj; 336 ACPI_BUFFER buf; 337 ACPI_STATUS rv; 338 339 rv = acpi_eval_struct(hdl, pth, &buf); 340 if (ACPI_FAILURE(rv)) 341 return NULL; 342 343 obj = buf.Pointer; 344 if (obj->Type != ACPI_TYPE_PACKAGE) { 345 ACPI_FREE(buf.Pointer); 346 return NULL; 347 } 348 if (obj->Package.Count != count) { 349 ACPI_FREE(buf.Pointer); 350 return NULL; 351 } 352 353 return obj; 354 } 355 356 /* 357 * acpibat_get_info: 358 * 359 * Get the battery info. 360 */ 361 static void 362 acpibat_get_info(device_t dv) 363 { 364 struct acpibat_softc *sc = device_private(dv); 365 ACPI_HANDLE hdl = sc->sc_node->ad_handle; 366 ACPI_OBJECT *elm, *obj; 367 ACPI_STATUS rv = AE_OK; 368 int capunit, i, rateunit; 369 uint64_t val; 370 371 obj = acpibat_get_object(hdl, "_BIF", ACPIBAT_BIF_COUNT); 372 if (obj == NULL) { 373 rv = AE_ERROR; 374 goto out; 375 } 376 377 elm = obj->Package.Elements; 378 for (i = ACPIBAT_BIF_UNIT; i < ACPIBAT_BIF_MODEL; i++) { 379 if (elm[i].Type != ACPI_TYPE_INTEGER) { 380 rv = AE_TYPE; 381 goto out; 382 } 383 if (elm[i].Integer.Value != ACPIBAT_VAL_UNKNOWN && 384 elm[i].Integer.Value >= INT_MAX) { 385 rv = AE_LIMIT; 386 goto out; 387 } 388 } 389 390 switch (elm[ACPIBAT_BIF_UNIT].Integer.Value) { 391 case ACPIBAT_PWRUNIT_MA: 392 capunit = ENVSYS_SAMPHOUR; 393 rateunit = ENVSYS_SAMPS; 394 break; 395 default: 396 capunit = ENVSYS_SWATTHOUR; 397 rateunit = ENVSYS_SWATTS; 398 break; 399 } 400 401 sc->sc_sensor[ACPIBAT_DCAPACITY].units = capunit; 402 sc->sc_sensor[ACPIBAT_LFCCAPACITY].units = capunit; 403 sc->sc_sensor[ACPIBAT_CHARGERATE].units = rateunit; 404 sc->sc_sensor[ACPIBAT_DISCHARGERATE].units = rateunit; 405 sc->sc_sensor[ACPIBAT_CAPACITY].units = capunit; 406 407 /* Design capacity. */ 408 val = elm[ACPIBAT_BIF_DCAPACITY].Integer.Value; 409 sc->sc_sensor[ACPIBAT_DCAPACITY].value_cur = val * 1000; 410 sc->sc_sensor[ACPIBAT_DCAPACITY].state = ACPIBAT_VAL_ISVALID(val); 411 412 /* Last full charge capacity. */ 413 val = elm[ACPIBAT_BIF_LFCCAPACITY].Integer.Value; 414 sc->sc_sensor[ACPIBAT_LFCCAPACITY].value_cur = val * 1000; 415 sc->sc_sensor[ACPIBAT_LFCCAPACITY].state = ACPIBAT_VAL_ISVALID(val); 416 417 /* Design voltage. */ 418 val = elm[ACPIBAT_BIF_DVOLTAGE].Integer.Value; 419 sc->sc_sensor[ACPIBAT_DVOLTAGE].value_cur = val * 1000; 420 sc->sc_sensor[ACPIBAT_DVOLTAGE].state = ACPIBAT_VAL_ISVALID(val); 421 422 /* Design low and warning capacity. */ 423 sc->sc_lcapacity = elm[ACPIBAT_BIF_LCAPACITY].Integer.Value * 1000; 424 sc->sc_wcapacity = elm[ACPIBAT_BIF_WCAPACITY].Integer.Value * 1000; 425 426 /* 427 * Initialize the maximum of current capacity 428 * to the last known full charge capacity. 429 */ 430 val = sc->sc_sensor[ACPIBAT_LFCCAPACITY].value_cur; 431 sc->sc_sensor[ACPIBAT_CAPACITY].value_max = val; 432 433 acpibat_print_info(dv, elm); 434 435 out: 436 if (obj != NULL) 437 ACPI_FREE(obj); 438 439 if (ACPI_FAILURE(rv)) 440 aprint_error_dev(dv, "failed to evaluate _BIF: %s\n", 441 AcpiFormatException(rv)); 442 } 443 444 /* 445 * acpibat_print_info: 446 * 447 * Display the battery info. 448 */ 449 static void 450 acpibat_print_info(device_t dv, ACPI_OBJECT *elm) 451 { 452 struct acpibat_softc *sc = device_private(dv); 453 const char *tech, *unit; 454 int32_t dcap, dvol; 455 int i; 456 457 for (i = ACPIBAT_BIF_OEM; i > ACPIBAT_BIF_GRANULARITY2; i--) { 458 if (elm[i].Type != ACPI_TYPE_STRING) 459 return; 460 if (elm[i].String.Pointer == NULL) 461 return; 462 if (elm[i].String.Pointer[0] == '\0') 463 return; 464 } 465 466 dcap = elm[ACPIBAT_BIF_DCAPACITY].Integer.Value; 467 dvol = elm[ACPIBAT_BIF_DVOLTAGE].Integer.Value; 468 469 /* 470 * Try to detect whether the battery was switched. 471 */ 472 if (sc->sc_dcapacity == dcap && sc->sc_dvoltage == dvol) 473 return; 474 else { 475 sc->sc_dcapacity = dcap; 476 sc->sc_dvoltage = dvol; 477 } 478 479 tech = (elm[ACPIBAT_BIF_TECHNOLOGY].Integer.Value != 0) ? 480 "rechargeable" : "non-rechargeable"; 481 482 aprint_normal_dev(dv, "%s %s %s battery\n", 483 elm[ACPIBAT_BIF_OEM].String.Pointer, 484 elm[ACPIBAT_BIF_TYPE].String.Pointer, tech); 485 486 aprint_debug_dev(dv, "model number %s, serial number %s\n", 487 elm[ACPIBAT_BIF_MODEL].String.Pointer, 488 elm[ACPIBAT_BIF_SERIAL].String.Pointer); 489 490 #define SCALE(x) (((int)x) / 1000000), ((((int)x) % 1000000) / 1000) 491 492 /* 493 * These values are defined as follows (ACPI 4.0, p. 388): 494 * 495 * Granularity 1. "Battery capacity granularity between low 496 * and warning in [mAh] or [mWh]. That is, 497 * this is the smallest increment in capacity 498 * that the battery is capable of measuring." 499 * 500 * Granularity 2. "Battery capacity granularity between warning 501 * and full in [mAh] or [mWh]. [...]" 502 */ 503 switch (elm[ACPIBAT_BIF_UNIT].Integer.Value) { 504 case ACPIBAT_PWRUNIT_MA: 505 unit = "Ah"; 506 break; 507 default: 508 unit = "Wh"; 509 break; 510 } 511 512 aprint_verbose_dev(dv, "granularity: " 513 "low->warn %d.%03d %s, warn->full %d.%03d %s\n", 514 SCALE(elm[ACPIBAT_BIF_GRANULARITY1].Integer.Value * 1000), unit, 515 SCALE(elm[ACPIBAT_BIF_GRANULARITY2].Integer.Value * 1000), unit); 516 } 517 518 /* 519 * acpibat_get_status: 520 * 521 * Get the current battery status. 522 */ 523 static void 524 acpibat_get_status(device_t dv) 525 { 526 struct acpibat_softc *sc = device_private(dv); 527 ACPI_HANDLE hdl = sc->sc_node->ad_handle; 528 ACPI_OBJECT *elm, *obj; 529 ACPI_STATUS rv = AE_OK; 530 int i, rate, state; 531 uint64_t val; 532 533 obj = acpibat_get_object(hdl, "_BST", ACPIBAT_BST_COUNT); 534 if (obj == NULL) { 535 rv = AE_ERROR; 536 goto out; 537 } 538 539 elm = obj->Package.Elements; 540 for (i = ACPIBAT_BST_STATE; i < ACPIBAT_BST_COUNT; i++) { 541 if (elm[i].Type != ACPI_TYPE_INTEGER) { 542 rv = AE_TYPE; 543 goto out; 544 } 545 } 546 547 state = elm[ACPIBAT_BST_STATE].Integer.Value; 548 if ((state & ACPIBAT_ST_CHARGING) != 0) { 549 /* XXX rate can be invalid */ 550 rate = elm[ACPIBAT_BST_RATE].Integer.Value; 551 sc->sc_sensor[ACPIBAT_CHARGERATE].state = ENVSYS_SVALID; 552 sc->sc_sensor[ACPIBAT_CHARGERATE].value_cur = rate * 1000; 553 sc->sc_sensor[ACPIBAT_DISCHARGERATE].state = ENVSYS_SINVALID; 554 sc->sc_sensor[ACPIBAT_CHARGING].state = ENVSYS_SVALID; 555 sc->sc_sensor[ACPIBAT_CHARGING].value_cur = 1; 556 } else if ((state & ACPIBAT_ST_DISCHARGING) != 0) { 557 rate = elm[ACPIBAT_BST_RATE].Integer.Value; 558 sc->sc_sensor[ACPIBAT_DISCHARGERATE].state = ENVSYS_SVALID; 559 sc->sc_sensor[ACPIBAT_DISCHARGERATE].value_cur = rate * 1000; 560 sc->sc_sensor[ACPIBAT_CHARGERATE].state = ENVSYS_SINVALID; 561 sc->sc_sensor[ACPIBAT_CHARGING].state = ENVSYS_SVALID; 562 sc->sc_sensor[ACPIBAT_CHARGING].value_cur = 0; 563 } else { 564 sc->sc_sensor[ACPIBAT_CHARGING].state = ENVSYS_SVALID; 565 sc->sc_sensor[ACPIBAT_CHARGING].value_cur = 0; 566 sc->sc_sensor[ACPIBAT_CHARGERATE].state = ENVSYS_SINVALID; 567 sc->sc_sensor[ACPIBAT_DISCHARGERATE].state = ENVSYS_SINVALID; 568 } 569 570 /* Remaining capacity. */ 571 val = elm[ACPIBAT_BST_CAPACITY].Integer.Value; 572 sc->sc_sensor[ACPIBAT_CAPACITY].value_cur = val * 1000; 573 sc->sc_sensor[ACPIBAT_CAPACITY].state = ACPIBAT_VAL_ISVALID(val); 574 575 /* Battery voltage. */ 576 val = elm[ACPIBAT_BST_VOLTAGE].Integer.Value; 577 sc->sc_sensor[ACPIBAT_VOLTAGE].value_cur = val * 1000; 578 sc->sc_sensor[ACPIBAT_VOLTAGE].state = ACPIBAT_VAL_ISVALID(val); 579 580 sc->sc_sensor[ACPIBAT_CHARGE_STATE].state = ENVSYS_SVALID; 581 sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur = 582 ENVSYS_BATTERY_CAPACITY_NORMAL; 583 584 if (sc->sc_sensor[ACPIBAT_CAPACITY].value_cur < sc->sc_wcapacity) { 585 sc->sc_sensor[ACPIBAT_CAPACITY].state = ENVSYS_SWARNUNDER; 586 sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur = 587 ENVSYS_BATTERY_CAPACITY_WARNING; 588 } 589 590 if (sc->sc_sensor[ACPIBAT_CAPACITY].value_cur < sc->sc_lcapacity) { 591 sc->sc_sensor[ACPIBAT_CAPACITY].state = ENVSYS_SCRITUNDER; 592 sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur = 593 ENVSYS_BATTERY_CAPACITY_LOW; 594 } 595 596 if ((state & ACPIBAT_ST_CRITICAL) != 0) { 597 sc->sc_sensor[ACPIBAT_CAPACITY].state = ENVSYS_SCRITICAL; 598 sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur = 599 ENVSYS_BATTERY_CAPACITY_CRITICAL; 600 } 601 602 out: 603 if (obj != NULL) 604 ACPI_FREE(obj); 605 606 if (ACPI_FAILURE(rv)) 607 aprint_error_dev(dv, "failed to evaluate _BST: %s\n", 608 AcpiFormatException(rv)); 609 } 610 611 static void 612 acpibat_update_info(void *arg) 613 { 614 device_t dv = arg; 615 struct acpibat_softc *sc = device_private(dv); 616 int i, rv; 617 618 mutex_enter(&sc->sc_mutex); 619 620 /* Don't touch sc_sme if we're detaching. */ 621 if (sc->sc_dying) 622 goto out; 623 624 rv = acpibat_get_sta(dv); 625 if (rv > 0) { 626 acpibat_get_info(dv); 627 628 /* 629 * If the status changed, update the limits. 630 */ 631 if (sc->sc_present == 0 && 632 sc->sc_sensor[ACPIBAT_CAPACITY].value_max > 0) 633 sysmon_envsys_update_limits(sc->sc_sme, 634 &sc->sc_sensor[ACPIBAT_CAPACITY]); 635 } else { 636 i = (rv < 0) ? 0 : ACPIBAT_DVOLTAGE; 637 while (i < ACPIBAT_COUNT) { 638 sc->sc_sensor[i].state = ENVSYS_SINVALID; 639 i++; 640 } 641 } 642 643 sc->sc_present = rv; 644 out: 645 mutex_exit(&sc->sc_mutex); 646 } 647 648 static void 649 acpibat_update_status(void *arg) 650 { 651 device_t dv = arg; 652 struct acpibat_softc *sc = device_private(dv); 653 int i, rv; 654 655 mutex_enter(&sc->sc_mutex); 656 657 rv = acpibat_get_sta(dv); 658 if (rv > 0) { 659 if (sc->sc_present == 0) 660 acpibat_get_info(dv); 661 acpibat_get_status(dv); 662 } else { 663 i = (rv < 0) ? 0 : ACPIBAT_DVOLTAGE; 664 while (i < ACPIBAT_COUNT) { 665 sc->sc_sensor[i].state = ENVSYS_SINVALID; 666 i++; 667 } 668 } 669 670 sc->sc_present = rv; 671 microtime(&sc->sc_last); 672 673 cv_broadcast(&sc->sc_condvar); 674 mutex_exit(&sc->sc_mutex); 675 } 676 677 /* 678 * acpibat_notify_handler: 679 * 680 * Callback from ACPI interrupt handler to notify us of an event. 681 */ 682 static void 683 acpibat_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context) 684 { 685 static const int handler = OSL_NOTIFY_HANDLER; 686 device_t dv = context; 687 688 switch (notify) { 689 case ACPI_NOTIFY_BUS_CHECK: 690 break; 691 case ACPI_NOTIFY_BAT_INFO: 692 case ACPI_NOTIFY_DEVICE_CHECK: 693 (void)AcpiOsExecute(handler, acpibat_update_info, dv); 694 break; 695 case ACPI_NOTIFY_BAT_STATUS: 696 (void)AcpiOsExecute(handler, acpibat_update_status, dv); 697 break; 698 default: 699 aprint_error_dev(dv, "unknown notify: 0x%02X\n", notify); 700 } 701 } 702 703 static void 704 acpibat_init_envsys(device_t dv) 705 { 706 struct acpibat_softc *sc = device_private(dv); 707 int i; 708 709 #define INITDATA(index, unit, string) \ 710 do { \ 711 sc->sc_sensor[index].state = ENVSYS_SVALID; \ 712 sc->sc_sensor[index].units = unit; \ 713 (void)strlcpy(sc->sc_sensor[index].desc, string, \ 714 sizeof(sc->sc_sensor[index].desc)); \ 715 } while (/* CONSTCOND */ 0) 716 717 INITDATA(ACPIBAT_PRESENT, ENVSYS_INDICATOR, "present"); 718 INITDATA(ACPIBAT_DCAPACITY, ENVSYS_SWATTHOUR, "design cap"); 719 INITDATA(ACPIBAT_LFCCAPACITY, ENVSYS_SWATTHOUR, "last full cap"); 720 INITDATA(ACPIBAT_DVOLTAGE, ENVSYS_SVOLTS_DC, "design voltage"); 721 INITDATA(ACPIBAT_VOLTAGE, ENVSYS_SVOLTS_DC, "voltage"); 722 INITDATA(ACPIBAT_CHARGERATE, ENVSYS_SWATTS, "charge rate"); 723 INITDATA(ACPIBAT_DISCHARGERATE, ENVSYS_SWATTS, "discharge rate"); 724 INITDATA(ACPIBAT_CAPACITY, ENVSYS_SWATTHOUR, "charge"); 725 INITDATA(ACPIBAT_CHARGING, ENVSYS_BATTERY_CHARGE, "charging"); 726 INITDATA(ACPIBAT_CHARGE_STATE, ENVSYS_BATTERY_CAPACITY, "charge state"); 727 728 #undef INITDATA 729 730 sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur = 731 ENVSYS_BATTERY_CAPACITY_NORMAL; 732 733 sc->sc_sensor[ACPIBAT_CAPACITY].flags |= 734 ENVSYS_FPERCENT | ENVSYS_FVALID_MAX | ENVSYS_FMONLIMITS; 735 736 sc->sc_sensor[ACPIBAT_CHARGE_STATE].flags |= ENVSYS_FMONSTCHANGED; 737 738 /* Disable userland monitoring on these sensors. */ 739 sc->sc_sensor[ACPIBAT_VOLTAGE].flags = ENVSYS_FMONNOTSUPP; 740 sc->sc_sensor[ACPIBAT_CHARGERATE].flags = ENVSYS_FMONNOTSUPP; 741 sc->sc_sensor[ACPIBAT_DISCHARGERATE].flags = ENVSYS_FMONNOTSUPP; 742 sc->sc_sensor[ACPIBAT_DCAPACITY].flags = ENVSYS_FMONNOTSUPP; 743 sc->sc_sensor[ACPIBAT_LFCCAPACITY].flags = ENVSYS_FMONNOTSUPP; 744 sc->sc_sensor[ACPIBAT_DVOLTAGE].flags = ENVSYS_FMONNOTSUPP; 745 746 /* Attach rnd(9) to the (dis)charge rates. */ 747 sc->sc_sensor[ACPIBAT_CHARGERATE].flags |= ENVSYS_FHAS_ENTROPY; 748 sc->sc_sensor[ACPIBAT_DISCHARGERATE].flags |= ENVSYS_FHAS_ENTROPY; 749 750 sc->sc_sme = sysmon_envsys_create(); 751 752 for (i = 0; i < ACPIBAT_COUNT; i++) { 753 if (sysmon_envsys_sensor_attach(sc->sc_sme, 754 &sc->sc_sensor[i])) 755 goto fail; 756 } 757 758 sc->sc_sme->sme_name = device_xname(dv); 759 sc->sc_sme->sme_cookie = dv; 760 sc->sc_sme->sme_refresh = acpibat_refresh; 761 sc->sc_sme->sme_class = SME_CLASS_BATTERY; 762 sc->sc_sme->sme_flags = SME_POLL_ONLY | SME_INIT_REFRESH; 763 sc->sc_sme->sme_get_limits = acpibat_get_limits; 764 765 (void)acpi_register_notify(sc->sc_node, acpibat_notify_handler); 766 acpibat_update_info(dv); 767 acpibat_update_status(dv); 768 769 if (sysmon_envsys_register(sc->sc_sme)) 770 goto fail; 771 772 (void)pmf_device_register(dv, NULL, acpibat_resume); 773 774 return; 775 776 fail: 777 aprint_error_dev(dv, "failed to initialize sysmon\n"); 778 779 sysmon_envsys_destroy(sc->sc_sme); 780 kmem_free(sc->sc_sensor, ACPIBAT_COUNT * sizeof(*sc->sc_sensor)); 781 782 sc->sc_sme = NULL; 783 sc->sc_sensor = NULL; 784 } 785 786 static void 787 acpibat_refresh(struct sysmon_envsys *sme, envsys_data_t *edata) 788 { 789 device_t self = sme->sme_cookie; 790 struct acpibat_softc *sc; 791 struct timeval tv, tmp; 792 ACPI_STATUS rv; 793 794 sc = device_private(self); 795 796 tmp.tv_sec = 10; 797 tmp.tv_usec = 0; 798 799 microtime(&tv); 800 timersub(&tv, &tmp, &tv); 801 if (timercmp(&tv, &sc->sc_last, <) != 0) 802 return; 803 804 if (mutex_tryenter(&sc->sc_mutex) == 0) 805 return; 806 807 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, acpibat_update_status, self); 808 if (ACPI_SUCCESS(rv)) 809 cv_timedwait(&sc->sc_condvar, &sc->sc_mutex, hz); 810 811 mutex_exit(&sc->sc_mutex); 812 } 813 814 static bool 815 acpibat_resume(device_t dv, const pmf_qual_t *qual) 816 { 817 818 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpibat_update_info, dv); 819 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpibat_update_status, dv); 820 821 return true; 822 } 823 824 static void 825 acpibat_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata, 826 sysmon_envsys_lim_t *limits, uint32_t *props) 827 { 828 device_t dv = sme->sme_cookie; 829 struct acpibat_softc *sc = device_private(dv); 830 831 if (edata->sensor != ACPIBAT_CAPACITY) 832 return; 833 834 limits->sel_critmin = sc->sc_lcapacity; 835 limits->sel_warnmin = sc->sc_wcapacity; 836 837 *props |= PROP_BATTCAP | PROP_BATTWARN | PROP_DRIVER_LIMITS; 838 } 839 840 MODULE(MODULE_CLASS_DRIVER, acpibat, "sysmon_envsys"); 841 842 #ifdef _MODULE 843 #include "ioconf.c" 844 #endif 845 846 static int 847 acpibat_modcmd(modcmd_t cmd, void *aux) 848 { 849 int rv = 0; 850 851 switch (cmd) { 852 case MODULE_CMD_INIT: 853 #ifdef _MODULE 854 rv = config_init_component(cfdriver_ioconf_acpibat, 855 cfattach_ioconf_acpibat, cfdata_ioconf_acpibat); 856 #endif 857 break; 858 case MODULE_CMD_FINI: 859 #ifdef _MODULE 860 rv = config_fini_component(cfdriver_ioconf_acpibat, 861 cfattach_ioconf_acpibat, cfdata_ioconf_acpibat); 862 #endif 863 break; 864 default: 865 rv = ENOTTY; 866 } 867 868 return rv; 869 } 870