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