1 /*- 2 * Copyright (c) 2000 Munehiro Matsuda 3 * Copyright (c) 2000 Takanori Watanabe 4 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include "opt_acpi.h" 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/bus.h> 35 #include <sys/ioccom.h> 36 #include <sys/conf.h> 37 38 #include <machine/bus.h> 39 #include <machine/resource.h> 40 #include <sys/rman.h> 41 #include <sys/malloc.h> 42 43 #include "acpi.h" 44 45 #include <dev/acpica/acpivar.h> 46 #include <dev/acpica/acpiio.h> 47 48 MALLOC_DEFINE(M_ACPICMBAT, "acpicmbat", "ACPI control method battery data"); 49 50 #define CMBAT_POLLRATE (60 * hz) 51 52 /* 53 * Hooks for the ACPI CA debugging infrastructure 54 */ 55 #define _COMPONENT ACPI_BATTERY 56 MODULE_NAME("BATTERY") 57 58 #define ACPI_BATTERY_BST_CHANGE 0x80 59 #define ACPI_BATTERY_BIF_CHANGE 0x81 60 61 #define PKG_GETINT(res, tmp, idx, dest, label) do { \ 62 tmp = &res->Package.Elements[idx]; \ 63 if (tmp == NULL) { \ 64 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), \ 65 __func__ ": PKG_GETINT error, idx = %d\n.", idx); \ 66 goto label; \ 67 } \ 68 if (tmp->Type != ACPI_TYPE_INTEGER) \ 69 goto label; \ 70 dest = tmp->Integer.Value; \ 71 } while (0) 72 73 #define PKG_GETSTR(res, tmp, idx, dest, size, label) do { \ 74 size_t length; \ 75 length = size; \ 76 tmp = &res->Package.Elements[idx]; \ 77 if (tmp == NULL) { \ 78 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), \ 79 __func__ ": PKG_GETSTR error, idx = %d\n.", idx); \ 80 goto label; \ 81 } \ 82 bzero(dest, sizeof(dest)); \ 83 switch (tmp->Type) { \ 84 case ACPI_TYPE_STRING: \ 85 if (tmp->String.Length < length) { \ 86 length = tmp->String.Length; \ 87 } \ 88 strncpy(dest, tmp->String.Pointer, length); \ 89 break; \ 90 case ACPI_TYPE_BUFFER: \ 91 if (tmp->Buffer.Length < length) { \ 92 length = tmp->Buffer.Length; \ 93 } \ 94 strncpy(dest, tmp->Buffer.Pointer, length); \ 95 break; \ 96 default: \ 97 goto label; \ 98 } \ 99 dest[sizeof(dest)-1] = '\0'; \ 100 } while (0) 101 102 struct acpi_cmbat_softc { 103 device_t dev; 104 105 struct acpi_bif bif; 106 struct acpi_bst bst; 107 ACPI_BUFFER bif_buffer; 108 ACPI_BUFFER bst_buffer; 109 struct timespec bif_lastupdated; 110 struct timespec bst_lastupdated; 111 112 int not_present; 113 int cap; 114 int min; 115 int full_charge_time; 116 117 struct callout_handle cmbat_timeout; 118 }; 119 120 static struct timespec acpi_cmbat_info_lastupdated; 121 122 /* XXX: devclass_get_maxunit() don't give us the current allocated units... */ 123 static int acpi_cmbat_units = 0; 124 125 static void acpi_cmbat_timeout(void *); 126 static int acpi_cmbat_info_expired(struct timespec *); 127 static void acpi_cmbat_info_updated(struct timespec *); 128 static void acpi_cmbat_get_bst(void *); 129 static void acpi_cmbat_get_bif(void *); 130 static void acpi_cmbat_notify_handler(ACPI_HANDLE, UINT32, void *); 131 static int acpi_cmbat_probe(device_t); 132 static int acpi_cmbat_attach(device_t); 133 static int acpi_cmbat_resume(device_t); 134 static int acpi_cmbat_ioctl(u_long, caddr_t, void *); 135 static int acpi_cmbat_get_total_battinfo(struct acpi_battinfo *); 136 137 /* 138 * Poll the battery info. 139 */ 140 static void 141 acpi_cmbat_timeout(void *context) 142 { 143 device_t dev; 144 struct acpi_cmbat_softc *sc; 145 146 dev = (device_t)context; 147 sc = device_get_softc(dev); 148 149 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev); 150 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE); 151 } 152 153 static __inline int 154 acpi_cmbat_info_expired(struct timespec *lastupdated) 155 { 156 struct timespec curtime; 157 158 if (lastupdated == NULL) { 159 return (1); 160 } 161 162 if (!timespecisset(lastupdated)) { 163 return (1); 164 } 165 166 getnanotime(&curtime); 167 timespecsub(&curtime, lastupdated); 168 return ((curtime.tv_sec < 0 || curtime.tv_sec > acpi_battery_get_info_expire())); 169 } 170 171 172 static __inline void 173 acpi_cmbat_info_updated(struct timespec *lastupdated) 174 { 175 176 if (lastupdated != NULL) { 177 getnanotime(lastupdated); 178 } 179 } 180 181 static void 182 acpi_cmbat_get_bst(void *context) 183 { 184 device_t dev; 185 struct acpi_cmbat_softc *sc; 186 ACPI_STATUS as; 187 ACPI_OBJECT *res, *tmp; 188 ACPI_HANDLE h; 189 190 dev = context; 191 sc = device_get_softc(dev); 192 h = acpi_get_handle(dev); 193 194 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) { 195 return; 196 } 197 198 untimeout(acpi_cmbat_timeout, (caddr_t)dev, sc->cmbat_timeout); 199 retry: 200 if (sc->bst_buffer.Length == 0) { 201 if (sc->bst_buffer.Pointer != NULL) { 202 free(sc->bst_buffer.Pointer, M_ACPICMBAT); 203 sc->bst_buffer.Pointer = NULL; 204 } 205 as = AcpiEvaluateObject(h, "_BST", NULL, &sc->bst_buffer); 206 if (as != AE_BUFFER_OVERFLOW) { 207 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 208 "couldn't find _BST - %s\n", AcpiFormatException(as)); 209 goto end; 210 } 211 212 sc->bst_buffer.Pointer = malloc(sc->bst_buffer.Length, M_ACPICMBAT, M_NOWAIT); 213 if (sc->bst_buffer.Pointer == NULL) { 214 device_printf(dev, "malloc failed"); 215 goto end; 216 } 217 } 218 219 bzero(sc->bst_buffer.Pointer, sc->bst_buffer.Length); 220 as = AcpiEvaluateObject(h, "_BST", NULL, &sc->bst_buffer); 221 222 if (as == AE_BUFFER_OVERFLOW) { 223 if (sc->bst_buffer.Pointer != NULL) { 224 free(sc->bst_buffer.Pointer, M_ACPICMBAT); 225 sc->bst_buffer.Pointer = NULL; 226 } 227 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 228 "bst size changed to %d\n", sc->bst_buffer.Length); 229 sc->bst_buffer.Length = 0; 230 goto retry; 231 } else if (as != AE_OK) { 232 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 233 "couldn't find _BST - %s\n", AcpiFormatException(as)); 234 goto end; 235 } 236 237 res = (ACPI_OBJECT *)sc->bst_buffer.Pointer; 238 239 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 4)) { 240 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 241 "battery status corrupted\n"); 242 goto end; 243 } 244 245 PKG_GETINT(res, tmp, 0, sc->bst.state, end); 246 PKG_GETINT(res, tmp, 1, sc->bst.rate, end); 247 PKG_GETINT(res, tmp, 2, sc->bst.cap, end); 248 PKG_GETINT(res, tmp, 3, sc->bst.volt, end); 249 acpi_cmbat_info_updated(&sc->bst_lastupdated); 250 end: 251 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE); 252 } 253 254 static void 255 acpi_cmbat_get_bif(void *context) 256 { 257 device_t dev; 258 struct acpi_cmbat_softc *sc; 259 ACPI_STATUS as; 260 ACPI_OBJECT *res, *tmp; 261 ACPI_HANDLE h; 262 263 dev = context; 264 sc = device_get_softc(dev); 265 h = acpi_get_handle(dev); 266 267 if (!acpi_cmbat_info_expired(&sc->bif_lastupdated)) { 268 return; 269 } 270 271 untimeout(acpi_cmbat_timeout, (caddr_t)dev, sc->cmbat_timeout); 272 retry: 273 if (sc->bif_buffer.Length == 0) { 274 if (sc->bif_buffer.Pointer != NULL) { 275 free(sc->bif_buffer.Pointer, M_ACPICMBAT); 276 sc->bif_buffer.Pointer = NULL; 277 } 278 as = AcpiEvaluateObject(h, "_BIF", NULL, &sc->bif_buffer); 279 if (as != AE_BUFFER_OVERFLOW) { 280 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 281 "couldn't find _BIF - %s\n", AcpiFormatException(as)); 282 goto end; 283 } 284 285 sc->bif_buffer.Pointer = malloc(sc->bif_buffer.Length, M_ACPICMBAT, M_NOWAIT); 286 if (sc->bif_buffer.Pointer == NULL) { 287 device_printf(dev, "malloc failed"); 288 goto end; 289 } 290 } 291 292 bzero(sc->bif_buffer.Pointer, sc->bif_buffer.Length); 293 as = AcpiEvaluateObject(h, "_BIF", NULL, &sc->bif_buffer); 294 295 if (as == AE_BUFFER_OVERFLOW) { 296 if (sc->bif_buffer.Pointer != NULL) { 297 free(sc->bif_buffer.Pointer, M_ACPICMBAT); 298 sc->bif_buffer.Pointer = NULL; 299 } 300 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 301 "bif size changed to %d\n", sc->bif_buffer.Length); 302 sc->bif_buffer.Length = 0; 303 goto retry; 304 } else if (as != AE_OK) { 305 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 306 "couldn't find _BIF - %s\n", AcpiFormatException(as)); 307 goto end; 308 } 309 310 res = (ACPI_OBJECT *)sc->bif_buffer.Pointer; 311 312 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 13)) { 313 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 314 "battery info corrupted\n"); 315 goto end; 316 } 317 318 PKG_GETINT(res, tmp, 0, sc->bif.unit, end); 319 PKG_GETINT(res, tmp, 1, sc->bif.dcap, end); 320 PKG_GETINT(res, tmp, 2, sc->bif.lfcap, end); 321 PKG_GETINT(res, tmp, 3, sc->bif.btech, end); 322 PKG_GETINT(res, tmp, 4, sc->bif.dvol, end); 323 PKG_GETINT(res, tmp, 5, sc->bif.wcap, end); 324 PKG_GETINT(res, tmp, 6, sc->bif.lcap, end); 325 PKG_GETINT(res, tmp, 7, sc->bif.gra1, end); 326 PKG_GETINT(res, tmp, 8, sc->bif.gra2, end); 327 PKG_GETSTR(res, tmp, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN, end); 328 PKG_GETSTR(res, tmp, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN, end); 329 PKG_GETSTR(res, tmp, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN, end); 330 PKG_GETSTR(res, tmp, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN, end); 331 acpi_cmbat_info_updated(&sc->bif_lastupdated); 332 end: 333 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE); 334 } 335 336 static void 337 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 338 { 339 device_t dev; 340 struct acpi_cmbat_softc *sc; 341 342 dev = (device_t)context; 343 if ((sc = device_get_softc(dev)) == NULL) { 344 return; 345 } 346 347 switch (notify) { 348 case ACPI_BATTERY_BST_CHANGE: 349 timespecclear(&sc->bst_lastupdated); 350 break; 351 case ACPI_BATTERY_BIF_CHANGE: 352 timespecclear(&sc->bif_lastupdated); 353 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev); 354 break; 355 default: 356 break; 357 } 358 } 359 360 static int 361 acpi_cmbat_probe(device_t dev) 362 { 363 364 if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) && 365 !acpi_disabled("cmbat") && 366 acpi_MatchHid(dev, "PNP0C0A")) { 367 /* 368 * Set device description. 369 */ 370 device_set_desc(dev, "Control method Battery"); 371 return (0); 372 } 373 return (ENXIO); 374 } 375 376 static int 377 acpi_cmbat_attach(device_t dev) 378 { 379 int error; 380 ACPI_HANDLE handle; 381 struct acpi_cmbat_softc *sc; 382 383 if ((sc = device_get_softc(dev)) == NULL) { 384 return (ENXIO); 385 } 386 387 handle = acpi_get_handle(dev); 388 389 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY, 390 acpi_cmbat_notify_handler, dev); 391 392 bzero(&sc->bif_buffer, sizeof(sc->bif_buffer)); 393 bzero(&sc->bst_buffer, sizeof(sc->bst_buffer)); 394 sc->dev = dev; 395 396 timespecclear(&sc->bif_lastupdated); 397 timespecclear(&sc->bst_lastupdated); 398 399 if (acpi_cmbat_units == 0) { 400 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BIF, 401 acpi_cmbat_ioctl, NULL)) != 0) { 402 return (error); 403 } 404 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BST, 405 acpi_cmbat_ioctl, NULL)) != 0) { 406 return (error); 407 } 408 } 409 410 if ((error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT, 411 acpi_cmbat_units)) != 0) { 412 return (error); 413 } 414 415 acpi_cmbat_units++; 416 timespecclear(&acpi_cmbat_info_lastupdated); 417 418 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev); 419 return (0); 420 } 421 422 static int 423 acpi_cmbat_resume(device_t dev) 424 { 425 426 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev); 427 return (0); 428 } 429 430 static device_method_t acpi_cmbat_methods[] = { 431 /* Device interface */ 432 DEVMETHOD(device_probe, acpi_cmbat_probe), 433 DEVMETHOD(device_attach, acpi_cmbat_attach), 434 DEVMETHOD(device_resume, acpi_cmbat_resume), 435 436 {0, 0} 437 }; 438 439 static driver_t acpi_cmbat_driver = { 440 "acpi_cmbat", 441 acpi_cmbat_methods, 442 sizeof(struct acpi_cmbat_softc), 443 }; 444 445 static devclass_t acpi_cmbat_devclass; 446 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0); 447 448 static int 449 acpi_cmbat_ioctl(u_long cmd, caddr_t addr, void *arg) 450 { 451 device_t dev; 452 union acpi_battery_ioctl_arg *ioctl_arg; 453 struct acpi_cmbat_softc *sc; 454 struct acpi_bif *bifp; 455 struct acpi_bst *bstp; 456 457 ioctl_arg = (union acpi_battery_ioctl_arg *)addr; 458 if ((dev = devclass_get_device(acpi_cmbat_devclass, 459 ioctl_arg->unit)) == NULL) { 460 return (ENXIO); 461 } 462 463 if ((sc = device_get_softc(dev)) == NULL) { 464 return (ENXIO); 465 } 466 467 switch (cmd) { 468 case ACPIIO_CMBAT_GET_BIF: 469 acpi_cmbat_get_bif(dev); 470 bifp = &ioctl_arg->bif; 471 bifp->unit = sc->bif.unit; 472 bifp->dcap = sc->bif.dcap; 473 bifp->lfcap = sc->bif.lfcap; 474 bifp->btech = sc->bif.btech; 475 bifp->dvol = sc->bif.dvol; 476 bifp->wcap = sc->bif.wcap; 477 bifp->lcap = sc->bif.lcap; 478 bifp->gra1 = sc->bif.gra1; 479 bifp->gra2 = sc->bif.gra2; 480 strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model)); 481 strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial)); 482 strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type)); 483 strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo)); 484 break; 485 486 case ACPIIO_CMBAT_GET_BST: 487 acpi_cmbat_get_bst(dev); 488 bstp = &ioctl_arg->bst; 489 bstp->state = sc->bst.state; 490 bstp->rate = sc->bst.rate; 491 bstp->cap = sc->bst.cap; 492 bstp->volt = sc->bst.volt; 493 break; 494 } 495 496 return (0); 497 } 498 499 static int 500 acpi_cmbat_get_total_battinfo(struct acpi_battinfo *battinfo) 501 { 502 int i; 503 int error; 504 int batt_stat; 505 int valid_rate, valid_units; 506 int cap, min; 507 int total_cap, total_min, total_full; 508 device_t dev; 509 struct acpi_cmbat_softc *sc; 510 static int bat_units = 0; 511 static struct acpi_cmbat_softc **bat = NULL; 512 513 cap = min = -1; 514 batt_stat = ACPI_BATT_STAT_NOT_PRESENT; 515 error = 0; 516 517 /* Allocate array of softc pointers */ 518 if (bat_units != acpi_cmbat_units) { 519 if (bat != NULL) { 520 free(bat, M_ACPICMBAT); 521 bat = NULL; 522 } 523 bat_units = 0; 524 } 525 if (bat == NULL) { 526 bat_units = acpi_cmbat_units; 527 bat = malloc(sizeof(struct acpi_cmbat_softc *) * bat_units, 528 M_ACPICMBAT, M_NOWAIT); 529 if (bat == NULL) { 530 error = ENOMEM; 531 goto out; 532 } 533 534 /* Collect softc pointers */ 535 for (i = 0; i < acpi_cmbat_units; i++) { 536 if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) { 537 error = ENXIO; 538 goto out; 539 } 540 541 if ((sc = device_get_softc(dev)) == NULL) { 542 error = ENXIO; 543 goto out; 544 } 545 546 bat[i] = sc; 547 } 548 } 549 550 /* Get battery status, valid rate and valid units */ 551 batt_stat = valid_rate = valid_units = 0; 552 for (i = 0; i < acpi_cmbat_units; i++) { 553 bat[i]->not_present = 0; 554 acpi_cmbat_get_bst(bat[i]->dev); 555 556 /* If battey not installed, we get strange values */ 557 if (bat[i]->bst.state >= ACPI_BATT_STAT_MAX || 558 bat[i]->bst.cap == 0xffffffff || 559 bat[i]->bst.volt == 0xffffffff || 560 bat[i]->bif.lfcap == 0) { 561 bat[i]->not_present = 1; 562 continue; 563 } 564 565 valid_units++; 566 567 bat[i]->cap = 100 * bat[i]->bst.cap / bat[i]->bif.lfcap; 568 569 batt_stat |= bat[i]->bst.state; 570 571 if (bat[i]->bst.rate > 0) { 572 /* 573 * XXX Hack to calculate total battery time. 574 * Systems with 2 or more battries, they may get used 575 * one by one, thus bst.rate is set only to the one 576 * in use. For remaining batteries bst.rate = 0, which 577 * makes it impossible to calculate remaining time. 578 * Some other systems may need sum of bst.rate in 579 * dis-charging state. 580 * There for we sum up the bst.rate that is valid 581 * (in dis-charging state), and use the sum to 582 * calcutate remaining batteries' time. 583 */ 584 if (bat[i]->bst.state & ACPI_BATT_STAT_DISCHARG) { 585 valid_rate += bat[i]->bst.rate; 586 } 587 } 588 } 589 590 /* Calculate total battery capacity and time */ 591 total_cap = total_min = total_full = 0; 592 for (i = 0; i < acpi_cmbat_units; i++) { 593 if (bat[i]->not_present) { 594 continue; 595 } 596 597 if (valid_rate > 0) { 598 /* Use the sum of bst.rate */ 599 bat[i]->min = 60 * bat[i]->bst.cap / valid_rate; 600 } else if (bat[i]->full_charge_time > 0) { 601 bat[i]->min = (bat[i]->full_charge_time * bat[i]->cap) / 100; 602 } else { 603 /* Couldn't find valid rate and full battery time */ 604 bat[i]->min = 0; 605 } 606 total_min += bat[i]->min; 607 total_cap += bat[i]->cap; 608 total_full += bat[i]->full_charge_time; 609 } 610 611 /* Battery life */ 612 if (valid_units == 0) { 613 cap = -1; 614 batt_stat = ACPI_BATT_STAT_NOT_PRESENT; 615 } else { 616 cap = total_cap / valid_units; 617 } 618 619 /* Battery time */ 620 if (valid_units == 0) { 621 min = -1; 622 } else if (valid_rate == 0 || (batt_stat & ACPI_BATT_STAT_CHARGING)) { 623 if (total_full == 0) { 624 min = -1; 625 } else { 626 min = (total_full * cap) / 100; 627 } 628 } else { 629 min = total_min; 630 } 631 632 acpi_cmbat_info_updated(&acpi_cmbat_info_lastupdated); 633 out: 634 battinfo->cap = cap; 635 battinfo->min = min; 636 battinfo->state = batt_stat; 637 638 return (error); 639 } 640 641 /* 642 * Public interfaces. 643 */ 644 645 int 646 acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo) 647 { 648 int error; 649 device_t dev; 650 struct acpi_cmbat_softc *sc; 651 652 if (unit == -1) { 653 return (acpi_cmbat_get_total_battinfo(battinfo)); 654 } 655 656 if (acpi_cmbat_info_expired(&acpi_cmbat_info_lastupdated)) { 657 error = acpi_cmbat_get_total_battinfo(battinfo); 658 if (error) { 659 goto out; 660 } 661 } 662 663 error = 0; 664 if (unit >= acpi_cmbat_units) { 665 error = ENXIO; 666 goto out; 667 } 668 669 if ((dev = devclass_get_device(acpi_cmbat_devclass, unit)) == NULL) { 670 error = ENXIO; 671 goto out; 672 } 673 674 if ((sc = device_get_softc(dev)) == NULL) { 675 error = ENXIO; 676 goto out; 677 } 678 679 if (sc->not_present) { 680 battinfo->cap = -1; 681 battinfo->min = -1; 682 battinfo->state = ACPI_BATT_STAT_NOT_PRESENT; 683 } else { 684 battinfo->cap = sc->cap; 685 battinfo->min = sc->min; 686 battinfo->state = sc->bst.state; 687 } 688 out: 689 return (error); 690 } 691 692