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 ACPI_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 "%s: PKG_GETINT error, idx = %d\n.", __func__, 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 "%s: PKG_GETSTR error, idx = %d\n.", __func__, 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 struct timespec bif_lastupdated; 108 struct timespec bst_lastupdated; 109 int bif_updating; 110 int bst_updating; 111 112 int present; 113 int cap; 114 int min; 115 int full_charge_time; 116 }; 117 118 static struct timespec acpi_cmbat_info_lastupdated; 119 120 /* XXX: devclass_get_maxunit() don't give us the current allocated units... */ 121 static int acpi_cmbat_units = 0; 122 123 static int acpi_cmbat_info_expired(struct timespec *); 124 static void acpi_cmbat_info_updated(struct timespec *); 125 static void acpi_cmbat_get_bst(void *); 126 static void acpi_cmbat_get_bif(void *); 127 static void acpi_cmbat_notify_handler(ACPI_HANDLE, UINT32, void *); 128 static int acpi_cmbat_probe(device_t); 129 static int acpi_cmbat_attach(device_t); 130 static int acpi_cmbat_resume(device_t); 131 static int acpi_cmbat_ioctl(u_long, caddr_t, void *); 132 static int acpi_cmbat_get_total_battinfo(struct acpi_battinfo *); 133 134 static __inline int 135 acpi_cmbat_info_expired(struct timespec *lastupdated) 136 { 137 struct timespec curtime; 138 139 if (lastupdated == NULL) { 140 return (1); 141 } 142 143 if (!timespecisset(lastupdated)) { 144 return (1); 145 } 146 147 getnanotime(&curtime); 148 timespecsub(&curtime, lastupdated); 149 return ((curtime.tv_sec < 0 || curtime.tv_sec > acpi_battery_get_info_expire())); 150 } 151 152 153 static __inline void 154 acpi_cmbat_info_updated(struct timespec *lastupdated) 155 { 156 157 if (lastupdated != NULL) { 158 getnanotime(lastupdated); 159 } 160 } 161 162 static void 163 acpi_cmbat_get_bst(void *context) 164 { 165 device_t dev; 166 struct acpi_cmbat_softc *sc; 167 ACPI_STATUS as; 168 ACPI_OBJECT *res, *tmp; 169 ACPI_HANDLE h; 170 ACPI_BUFFER bst_buffer; 171 172 dev = context; 173 sc = device_get_softc(dev); 174 h = acpi_get_handle(dev); 175 bst_buffer.Pointer = NULL; 176 177 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) { 178 return; 179 } 180 181 if (sc->bst_updating) { 182 return; 183 } 184 sc->bst_updating = 1; 185 186 bst_buffer.Length = ACPI_ALLOCATE_BUFFER; 187 if (ACPI_FAILURE(as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer))) { 188 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 189 "error fetching current battery status -- %s\n", 190 AcpiFormatException(as)); 191 goto end; 192 } 193 194 res = (ACPI_OBJECT *)bst_buffer.Pointer; 195 196 if ((res == NULL) || (res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 4)) { 197 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 198 "battery status corrupted\n"); 199 goto end; 200 } 201 202 PKG_GETINT(res, tmp, 0, sc->bst.state, end); 203 PKG_GETINT(res, tmp, 1, sc->bst.rate, end); 204 PKG_GETINT(res, tmp, 2, sc->bst.cap, end); 205 PKG_GETINT(res, tmp, 3, sc->bst.volt, end); 206 acpi_cmbat_info_updated(&sc->bst_lastupdated); 207 end: 208 if (bst_buffer.Pointer != NULL) 209 AcpiOsFree(bst_buffer.Pointer); 210 sc->bst_updating = 0; 211 } 212 213 static void 214 acpi_cmbat_get_bif(void *context) 215 { 216 device_t dev; 217 struct acpi_cmbat_softc *sc; 218 ACPI_STATUS as; 219 ACPI_OBJECT *res, *tmp; 220 ACPI_HANDLE h; 221 ACPI_BUFFER bif_buffer; 222 223 dev = context; 224 sc = device_get_softc(dev); 225 h = acpi_get_handle(dev); 226 bif_buffer.Pointer = NULL; 227 228 if (!acpi_cmbat_info_expired(&sc->bif_lastupdated)) { 229 return; 230 } 231 232 if (sc->bif_updating) { 233 return; 234 } 235 sc->bif_updating = 1; 236 237 bif_buffer.Length = ACPI_ALLOCATE_BUFFER; 238 if (ACPI_FAILURE(as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer))) { 239 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 240 "error fetching current battery info -- %s\n", 241 AcpiFormatException(as)); 242 goto end; 243 } 244 245 res = (ACPI_OBJECT *)bif_buffer.Pointer; 246 247 if ((res == NULL) || (res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 13)) { 248 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 249 "battery info corrupted\n"); 250 goto end; 251 } 252 253 PKG_GETINT(res, tmp, 0, sc->bif.unit, end); 254 PKG_GETINT(res, tmp, 1, sc->bif.dcap, end); 255 PKG_GETINT(res, tmp, 2, sc->bif.lfcap, end); 256 PKG_GETINT(res, tmp, 3, sc->bif.btech, end); 257 PKG_GETINT(res, tmp, 4, sc->bif.dvol, end); 258 PKG_GETINT(res, tmp, 5, sc->bif.wcap, end); 259 PKG_GETINT(res, tmp, 6, sc->bif.lcap, end); 260 PKG_GETINT(res, tmp, 7, sc->bif.gra1, end); 261 PKG_GETINT(res, tmp, 8, sc->bif.gra2, end); 262 PKG_GETSTR(res, tmp, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN, end); 263 PKG_GETSTR(res, tmp, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN, end); 264 PKG_GETSTR(res, tmp, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN, end); 265 PKG_GETSTR(res, tmp, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN, end); 266 acpi_cmbat_info_updated(&sc->bif_lastupdated); 267 end: 268 if (bif_buffer.Pointer != NULL) 269 AcpiOsFree(bif_buffer.Pointer); 270 sc->bif_updating = 0; 271 } 272 273 static void 274 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 275 { 276 device_t dev; 277 struct acpi_cmbat_softc *sc; 278 279 dev = (device_t)context; 280 if ((sc = device_get_softc(dev)) == NULL) { 281 return; 282 } 283 284 switch (notify) { 285 case ACPI_BATTERY_BST_CHANGE: 286 timespecclear(&sc->bst_lastupdated); 287 break; 288 case ACPI_BATTERY_BIF_CHANGE: 289 timespecclear(&sc->bif_lastupdated); 290 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev); 291 break; 292 default: 293 break; 294 } 295 } 296 297 static int 298 acpi_cmbat_probe(device_t dev) 299 { 300 301 if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) && 302 !acpi_disabled("cmbat") && 303 acpi_MatchHid(dev, "PNP0C0A")) { 304 /* 305 * Set device description. 306 */ 307 device_set_desc(dev, "Control method Battery"); 308 return (0); 309 } 310 return (ENXIO); 311 } 312 313 static int 314 acpi_cmbat_attach(device_t dev) 315 { 316 int error; 317 ACPI_HANDLE handle; 318 struct acpi_cmbat_softc *sc; 319 320 if ((sc = device_get_softc(dev)) == NULL) { 321 return (ENXIO); 322 } 323 324 handle = acpi_get_handle(dev); 325 326 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY, 327 acpi_cmbat_notify_handler, dev); 328 329 sc->bif_updating = sc->bst_updating = 0; 330 sc->dev = dev; 331 332 timespecclear(&sc->bif_lastupdated); 333 timespecclear(&sc->bst_lastupdated); 334 335 if (acpi_cmbat_units == 0) { 336 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BIF, 337 acpi_cmbat_ioctl, NULL)) != 0) { 338 return (error); 339 } 340 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BST, 341 acpi_cmbat_ioctl, NULL)) != 0) { 342 return (error); 343 } 344 } 345 346 if ((error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT, 347 acpi_cmbat_units)) != 0) { 348 return (error); 349 } 350 351 acpi_cmbat_units++; 352 timespecclear(&acpi_cmbat_info_lastupdated); 353 354 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev); 355 return (0); 356 } 357 358 static int 359 acpi_cmbat_resume(device_t dev) 360 { 361 362 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev); 363 return (0); 364 } 365 366 static device_method_t acpi_cmbat_methods[] = { 367 /* Device interface */ 368 DEVMETHOD(device_probe, acpi_cmbat_probe), 369 DEVMETHOD(device_attach, acpi_cmbat_attach), 370 DEVMETHOD(device_resume, acpi_cmbat_resume), 371 372 {0, 0} 373 }; 374 375 static driver_t acpi_cmbat_driver = { 376 "acpi_cmbat", 377 acpi_cmbat_methods, 378 sizeof(struct acpi_cmbat_softc), 379 }; 380 381 static devclass_t acpi_cmbat_devclass; 382 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0); 383 384 static int 385 acpi_cmbat_ioctl(u_long cmd, caddr_t addr, void *arg) 386 { 387 device_t dev; 388 union acpi_battery_ioctl_arg *ioctl_arg; 389 struct acpi_cmbat_softc *sc; 390 struct acpi_bif *bifp; 391 struct acpi_bst *bstp; 392 393 ioctl_arg = (union acpi_battery_ioctl_arg *)addr; 394 if ((dev = devclass_get_device(acpi_cmbat_devclass, 395 ioctl_arg->unit)) == NULL) { 396 return (ENXIO); 397 } 398 399 if ((sc = device_get_softc(dev)) == NULL) { 400 return (ENXIO); 401 } 402 403 switch (cmd) { 404 case ACPIIO_CMBAT_GET_BIF: 405 acpi_cmbat_get_bif(dev); 406 bifp = &ioctl_arg->bif; 407 bifp->unit = sc->bif.unit; 408 bifp->dcap = sc->bif.dcap; 409 bifp->lfcap = sc->bif.lfcap; 410 bifp->btech = sc->bif.btech; 411 bifp->dvol = sc->bif.dvol; 412 bifp->wcap = sc->bif.wcap; 413 bifp->lcap = sc->bif.lcap; 414 bifp->gra1 = sc->bif.gra1; 415 bifp->gra2 = sc->bif.gra2; 416 strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model)); 417 strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial)); 418 strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type)); 419 strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo)); 420 break; 421 422 case ACPIIO_CMBAT_GET_BST: 423 bstp = &ioctl_arg->bst; 424 if (acpi_BatteryIsPresent(dev)) { 425 acpi_cmbat_get_bst(dev); 426 bstp->state = sc->bst.state; 427 bstp->rate = sc->bst.rate; 428 bstp->cap = sc->bst.cap; 429 bstp->volt = sc->bst.volt; 430 } else 431 bstp->state = ACPI_BATT_STAT_NOT_PRESENT; 432 break; 433 } 434 435 return (0); 436 } 437 438 static int 439 acpi_cmbat_get_total_battinfo(struct acpi_battinfo *battinfo) 440 { 441 int i; 442 int error; 443 int batt_stat; 444 int valid_rate, valid_units; 445 int cap, min; 446 int total_cap, total_min, total_full; 447 device_t dev; 448 struct acpi_cmbat_softc *sc; 449 static int bat_units = 0; 450 static struct acpi_cmbat_softc **bat = NULL; 451 452 cap = min = -1; 453 batt_stat = ACPI_BATT_STAT_NOT_PRESENT; 454 error = 0; 455 456 /* Allocate array of softc pointers */ 457 if (bat_units != acpi_cmbat_units) { 458 if (bat != NULL) { 459 free(bat, M_ACPICMBAT); 460 bat = NULL; 461 } 462 bat_units = 0; 463 } 464 if (bat == NULL) { 465 bat_units = acpi_cmbat_units; 466 bat = malloc(sizeof(struct acpi_cmbat_softc *) * bat_units, 467 M_ACPICMBAT, M_NOWAIT); 468 if (bat == NULL) { 469 error = ENOMEM; 470 goto out; 471 } 472 473 /* Collect softc pointers */ 474 for (i = 0; i < acpi_cmbat_units; i++) { 475 if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) { 476 error = ENXIO; 477 goto out; 478 } 479 480 if ((sc = device_get_softc(dev)) == NULL) { 481 error = ENXIO; 482 goto out; 483 } 484 485 bat[i] = sc; 486 } 487 } 488 489 /* Get battery status, valid rate and valid units */ 490 batt_stat = valid_rate = valid_units = 0; 491 for (i = 0; i < acpi_cmbat_units; i++) { 492 bat[i]->present = acpi_BatteryIsPresent(bat[i]->dev); 493 if (!bat[i]->present) 494 continue; 495 496 acpi_cmbat_get_bst(bat[i]->dev); 497 498 /* If battey not installed, we get strange values */ 499 if (bat[i]->bst.state >= ACPI_BATT_STAT_MAX || 500 bat[i]->bst.cap == 0xffffffff || 501 bat[i]->bst.volt == 0xffffffff || 502 bat[i]->bif.lfcap == 0) { 503 bat[i]->present = 0; 504 continue; 505 } 506 507 valid_units++; 508 509 bat[i]->cap = 100 * bat[i]->bst.cap / bat[i]->bif.lfcap; 510 511 batt_stat |= bat[i]->bst.state; 512 513 if (bat[i]->bst.rate > 0) { 514 /* 515 * XXX Hack to calculate total battery time. 516 * Systems with 2 or more battries, they may get used 517 * one by one, thus bst.rate is set only to the one 518 * in use. For remaining batteries bst.rate = 0, which 519 * makes it impossible to calculate remaining time. 520 * Some other systems may need sum of bst.rate in 521 * dis-charging state. 522 * There for we sum up the bst.rate that is valid 523 * (in dis-charging state), and use the sum to 524 * calcutate remaining batteries' time. 525 */ 526 if (bat[i]->bst.state & ACPI_BATT_STAT_DISCHARG) { 527 valid_rate += bat[i]->bst.rate; 528 } 529 } 530 } 531 532 /* Calculate total battery capacity and time */ 533 total_cap = total_min = total_full = 0; 534 for (i = 0; i < acpi_cmbat_units; i++) { 535 if (!bat[i]->present) { 536 continue; 537 } 538 539 if (valid_rate > 0) { 540 /* Use the sum of bst.rate */ 541 bat[i]->min = 60 * bat[i]->bst.cap / valid_rate; 542 } else if (bat[i]->full_charge_time > 0) { 543 bat[i]->min = (bat[i]->full_charge_time * bat[i]->cap) / 100; 544 } else { 545 /* Couldn't find valid rate and full battery time */ 546 bat[i]->min = 0; 547 } 548 total_min += bat[i]->min; 549 total_cap += bat[i]->cap; 550 total_full += bat[i]->full_charge_time; 551 } 552 553 /* Battery life */ 554 if (valid_units == 0) { 555 cap = -1; 556 batt_stat = ACPI_BATT_STAT_NOT_PRESENT; 557 } else { 558 cap = total_cap / valid_units; 559 } 560 561 /* Battery time */ 562 if (valid_units == 0) { 563 min = -1; 564 } else if (valid_rate == 0 || (batt_stat & ACPI_BATT_STAT_CHARGING)) { 565 if (total_full == 0) { 566 min = -1; 567 } else { 568 min = (total_full * cap) / 100; 569 } 570 } else { 571 min = total_min; 572 } 573 574 acpi_cmbat_info_updated(&acpi_cmbat_info_lastupdated); 575 out: 576 battinfo->cap = cap; 577 battinfo->min = min; 578 battinfo->state = batt_stat; 579 580 return (error); 581 } 582 583 /* 584 * Public interfaces. 585 */ 586 587 int 588 acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo) 589 { 590 int error; 591 device_t dev; 592 struct acpi_cmbat_softc *sc; 593 594 if (unit == -1) { 595 return (acpi_cmbat_get_total_battinfo(battinfo)); 596 } 597 598 if (acpi_cmbat_info_expired(&acpi_cmbat_info_lastupdated)) { 599 error = acpi_cmbat_get_total_battinfo(battinfo); 600 if (error) { 601 goto out; 602 } 603 } 604 605 error = 0; 606 if (unit >= acpi_cmbat_units) { 607 error = ENXIO; 608 goto out; 609 } 610 611 if ((dev = devclass_get_device(acpi_cmbat_devclass, unit)) == NULL) { 612 error = ENXIO; 613 goto out; 614 } 615 616 if ((sc = device_get_softc(dev)) == NULL) { 617 error = ENXIO; 618 goto out; 619 } 620 621 if (!sc->present) { 622 battinfo->cap = -1; 623 battinfo->min = -1; 624 battinfo->state = ACPI_BATT_STAT_NOT_PRESENT; 625 } else { 626 battinfo->cap = sc->cap; 627 battinfo->min = sc->min; 628 battinfo->state = sc->bst.state; 629 } 630 out: 631 return (error); 632 } 633 634