1 /*- 2 * Copyright (c) 2005 Nate Lawson 3 * Copyright (c) 2000 Munehiro Matsuda 4 * Copyright (c) 2000 Takanori Watanabe 5 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: src/sys/dev/acpica/acpi_cmbat.c,v 1.46.8.1 2009/04/15 03:14:26 kensmith Exp $ 30 */ 31 32 #include "opt_acpi.h" 33 #include <sys/param.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/bus.h> 37 38 #include <sys/rman.h> 39 #include <sys/malloc.h> 40 41 #include "acpi.h" 42 #include <dev/acpica/acpivar.h> 43 #include <dev/acpica/acpiio.h> 44 45 MALLOC_DEFINE(M_ACPICMBAT, "acpicmbat", "ACPI control method battery data"); 46 47 /* Number of times to retry initialization before giving up. */ 48 #define ACPI_CMBAT_RETRY_MAX 6 49 50 /* Check the battery once a minute. */ 51 #define CMBAT_POLLRATE (60 * hz) 52 53 /* Hooks for the ACPI CA debugging infrastructure */ 54 #define _COMPONENT ACPI_BATTERY 55 ACPI_MODULE_NAME("BATTERY") 56 57 #define ACPI_BATTERY_BST_CHANGE 0x80 58 #define ACPI_BATTERY_BIF_CHANGE 0x81 59 60 struct acpi_cmbat_softc { 61 device_t dev; 62 int flags; 63 64 struct acpi_bif bif; 65 struct acpi_bst bst; 66 struct timespec bst_lastupdated; 67 }; 68 69 ACPI_SERIAL_DECL(cmbat, "ACPI cmbat"); 70 71 static int acpi_cmbat_probe(device_t dev); 72 static int acpi_cmbat_attach(device_t dev); 73 static int acpi_cmbat_detach(device_t dev); 74 static int acpi_cmbat_resume(device_t dev); 75 static void acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, 76 void *context); 77 static int acpi_cmbat_info_expired(struct timespec *lastupdated); 78 static void acpi_cmbat_info_updated(struct timespec *lastupdated); 79 static void acpi_cmbat_get_bst(void *arg); 80 static void acpi_cmbat_get_bif_task(void *arg); 81 static void acpi_cmbat_get_bif(void *arg); 82 static int acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp); 83 static int acpi_cmbat_bif(device_t dev, struct acpi_bif *bifp); 84 static void acpi_cmbat_init_battery(void *arg); 85 86 static device_method_t acpi_cmbat_methods[] = { 87 /* Device interface */ 88 DEVMETHOD(device_probe, acpi_cmbat_probe), 89 DEVMETHOD(device_attach, acpi_cmbat_attach), 90 DEVMETHOD(device_detach, acpi_cmbat_detach), 91 DEVMETHOD(device_resume, acpi_cmbat_resume), 92 93 /* ACPI battery interface */ 94 DEVMETHOD(acpi_batt_get_info, acpi_cmbat_bif), 95 DEVMETHOD(acpi_batt_get_status, acpi_cmbat_bst), 96 97 DEVMETHOD_END 98 }; 99 100 static driver_t acpi_cmbat_driver = { 101 "battery", 102 acpi_cmbat_methods, 103 sizeof(struct acpi_cmbat_softc), 104 }; 105 106 static devclass_t acpi_cmbat_devclass; 107 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, NULL, NULL); 108 MODULE_DEPEND(acpi_cmbat, acpi, 1, 1, 1); 109 110 static int 111 acpi_cmbat_probe(device_t dev) 112 { 113 static char *cmbat_ids[] = { "PNP0C0A", NULL }; 114 115 if (acpi_disabled("cmbat") || 116 ACPI_ID_PROBE(device_get_parent(dev), dev, cmbat_ids) == NULL) 117 return (ENXIO); 118 119 device_set_desc(dev, "ACPI Control Method Battery"); 120 return (0); 121 } 122 123 static int 124 acpi_cmbat_attach(device_t dev) 125 { 126 int error; 127 ACPI_HANDLE handle; 128 struct acpi_cmbat_softc *sc; 129 130 sc = device_get_softc(dev); 131 handle = acpi_get_handle(dev); 132 sc->dev = dev; 133 134 ACPI_SERIAL_INIT(cmbat); 135 136 timespecclear(&sc->bst_lastupdated); 137 138 error = acpi_battery_register(dev); 139 if (error != 0) { 140 device_printf(dev, "registering battery failed\n"); 141 return (error); 142 } 143 144 /* 145 * Install a system notify handler in addition to the device notify. 146 * Toshiba notebook uses this alternate notify for its battery. 147 */ 148 AcpiInstallNotifyHandler(handle, ACPI_ALL_NOTIFY, 149 acpi_cmbat_notify_handler, dev); 150 151 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev); 152 153 return (0); 154 } 155 156 static int 157 acpi_cmbat_detach(device_t dev) 158 { 159 ACPI_HANDLE handle; 160 161 handle = acpi_get_handle(dev); 162 AcpiRemoveNotifyHandler(handle, ACPI_ALL_NOTIFY, acpi_cmbat_notify_handler); 163 acpi_battery_remove(dev); 164 return (0); 165 } 166 167 static int 168 acpi_cmbat_resume(device_t dev) 169 { 170 171 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev); 172 return (0); 173 } 174 175 static void 176 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 177 { 178 struct acpi_cmbat_softc *sc; 179 device_t dev; 180 181 dev = (device_t)context; 182 sc = device_get_softc(dev); 183 184 switch (notify) { 185 case ACPI_NOTIFY_DEVICE_CHECK: 186 case ACPI_BATTERY_BST_CHANGE: 187 /* 188 * Clear the last updated time. The next call to retrieve the 189 * battery status will get the new value for us. 190 */ 191 timespecclear(&sc->bst_lastupdated); 192 break; 193 case ACPI_NOTIFY_BUS_CHECK: 194 case ACPI_BATTERY_BIF_CHANGE: 195 /* 196 * Queue a callback to get the current battery info from thread 197 * context. It's not safe to block in a notify handler. 198 */ 199 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_get_bif_task, dev); 200 break; 201 } 202 203 acpi_UserNotify("CMBAT", h, notify); 204 } 205 206 static int 207 acpi_cmbat_info_expired(struct timespec *lastupdated) 208 { 209 struct timespec curtime; 210 211 ACPI_SERIAL_ASSERT(cmbat); 212 213 if (lastupdated == NULL) 214 return (TRUE); 215 if (!timespecisset(lastupdated)) 216 return (TRUE); 217 218 getnanotime(&curtime); 219 timespecsub(&curtime, lastupdated); 220 return (curtime.tv_sec < 0 || 221 curtime.tv_sec > acpi_battery_get_info_expire()); 222 } 223 224 static void 225 acpi_cmbat_info_updated(struct timespec *lastupdated) 226 { 227 228 ACPI_SERIAL_ASSERT(cmbat); 229 230 if (lastupdated != NULL) 231 getnanotime(lastupdated); 232 } 233 234 static void 235 acpi_cmbat_get_bst(void *arg) 236 { 237 struct acpi_cmbat_softc *sc; 238 ACPI_STATUS as; 239 ACPI_OBJECT *res; 240 ACPI_HANDLE h; 241 ACPI_BUFFER bst_buffer; 242 device_t dev; 243 244 ACPI_SERIAL_ASSERT(cmbat); 245 246 dev = arg; 247 sc = device_get_softc(dev); 248 h = acpi_get_handle(dev); 249 bst_buffer.Pointer = NULL; 250 bst_buffer.Length = ACPI_ALLOCATE_BUFFER; 251 252 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) 253 goto end; 254 255 as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer); 256 if (ACPI_FAILURE(as)) { 257 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 258 "error fetching current battery status -- %s\n", 259 AcpiFormatException(as)); 260 goto end; 261 } 262 263 res = (ACPI_OBJECT *)bst_buffer.Pointer; 264 if (!ACPI_PKG_VALID(res, 4)) { 265 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 266 "battery status corrupted\n"); 267 goto end; 268 } 269 270 if (acpi_PkgInt32(res, 0, &sc->bst.state) != 0) 271 goto end; 272 if (acpi_PkgInt32(res, 1, &sc->bst.rate) != 0) 273 goto end; 274 if (acpi_PkgInt32(res, 2, &sc->bst.cap) != 0) 275 goto end; 276 if (acpi_PkgInt32(res, 3, &sc->bst.volt) != 0) 277 goto end; 278 acpi_cmbat_info_updated(&sc->bst_lastupdated); 279 280 /* XXX If all batteries are critical, perhaps we should suspend. */ 281 if (sc->bst.state & ACPI_BATT_STAT_CRITICAL) { 282 if ((sc->flags & ACPI_BATT_STAT_CRITICAL) == 0) { 283 sc->flags |= ACPI_BATT_STAT_CRITICAL; 284 device_printf(dev, "critically low charge!\n"); 285 } 286 } else 287 sc->flags &= ~ACPI_BATT_STAT_CRITICAL; 288 289 end: 290 if (bst_buffer.Pointer != NULL) 291 AcpiOsFree(bst_buffer.Pointer); 292 } 293 294 /* XXX There should be a cleaner way to do this locking. */ 295 static void 296 acpi_cmbat_get_bif_task(void *arg) 297 { 298 299 ACPI_SERIAL_BEGIN(cmbat); 300 acpi_cmbat_get_bif(arg); 301 ACPI_SERIAL_END(cmbat); 302 } 303 304 static void 305 acpi_cmbat_get_bif(void *arg) 306 { 307 struct acpi_cmbat_softc *sc; 308 ACPI_STATUS as; 309 ACPI_OBJECT *res; 310 ACPI_HANDLE h; 311 ACPI_BUFFER bif_buffer; 312 device_t dev; 313 314 ACPI_SERIAL_ASSERT(cmbat); 315 316 dev = arg; 317 sc = device_get_softc(dev); 318 h = acpi_get_handle(dev); 319 bif_buffer.Pointer = NULL; 320 bif_buffer.Length = ACPI_ALLOCATE_BUFFER; 321 322 as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer); 323 if (ACPI_FAILURE(as)) { 324 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 325 "error fetching current battery info -- %s\n", 326 AcpiFormatException(as)); 327 goto end; 328 } 329 330 res = (ACPI_OBJECT *)bif_buffer.Pointer; 331 if (!ACPI_PKG_VALID(res, 13)) { 332 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 333 "battery info corrupted\n"); 334 goto end; 335 } 336 337 if (acpi_PkgInt32(res, 0, &sc->bif.units) != 0) 338 goto end; 339 if (acpi_PkgInt32(res, 1, &sc->bif.dcap) != 0) 340 goto end; 341 if (acpi_PkgInt32(res, 2, &sc->bif.lfcap) != 0) 342 goto end; 343 if (acpi_PkgInt32(res, 3, &sc->bif.btech) != 0) 344 goto end; 345 if (acpi_PkgInt32(res, 4, &sc->bif.dvol) != 0) 346 goto end; 347 if (acpi_PkgInt32(res, 5, &sc->bif.wcap) != 0) 348 goto end; 349 if (acpi_PkgInt32(res, 6, &sc->bif.lcap) != 0) 350 goto end; 351 if (acpi_PkgInt32(res, 7, &sc->bif.gra1) != 0) 352 goto end; 353 if (acpi_PkgInt32(res, 8, &sc->bif.gra2) != 0) 354 goto end; 355 if (acpi_PkgStr(res, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN) != 0) 356 goto end; 357 if (acpi_PkgStr(res, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN) != 0) 358 goto end; 359 if (acpi_PkgStr(res, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN) != 0) 360 goto end; 361 if (acpi_PkgStr(res, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN) != 0) 362 goto end; 363 364 end: 365 if (bif_buffer.Pointer != NULL) 366 AcpiOsFree(bif_buffer.Pointer); 367 } 368 369 static int 370 acpi_cmbat_bif(device_t dev, struct acpi_bif *bifp) 371 { 372 struct acpi_cmbat_softc *sc; 373 374 sc = device_get_softc(dev); 375 376 /* 377 * Just copy the data. The only value that should change is the 378 * last-full capacity, so we only update when we get a notify that says 379 * the info has changed. Many systems apparently take a long time to 380 * process a _BIF call so we avoid it if possible. 381 */ 382 ACPI_SERIAL_BEGIN(cmbat); 383 bifp->units = sc->bif.units; 384 bifp->dcap = sc->bif.dcap; 385 bifp->lfcap = sc->bif.lfcap; 386 bifp->btech = sc->bif.btech; 387 bifp->dvol = sc->bif.dvol; 388 bifp->wcap = sc->bif.wcap; 389 bifp->lcap = sc->bif.lcap; 390 bifp->gra1 = sc->bif.gra1; 391 bifp->gra2 = sc->bif.gra2; 392 strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model)); 393 strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial)); 394 strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type)); 395 strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo)); 396 ACPI_SERIAL_END(cmbat); 397 398 return (0); 399 } 400 401 static int 402 acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp) 403 { 404 struct acpi_cmbat_softc *sc; 405 406 sc = device_get_softc(dev); 407 408 ACPI_SERIAL_BEGIN(cmbat); 409 if (acpi_BatteryIsPresent(dev)) { 410 acpi_cmbat_get_bst(dev); 411 bstp->state = sc->bst.state; 412 bstp->rate = sc->bst.rate; 413 bstp->cap = sc->bst.cap; 414 bstp->volt = sc->bst.volt; 415 } else 416 bstp->state = ACPI_BATT_STAT_NOT_PRESENT; 417 ACPI_SERIAL_END(cmbat); 418 419 return (0); 420 } 421 422 static void 423 acpi_cmbat_init_battery(void *arg) 424 { 425 struct acpi_cmbat_softc *sc; 426 int retry, valid; 427 device_t dev; 428 429 dev = (device_t)arg; 430 sc = device_get_softc(dev); 431 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 432 "battery initialization start\n"); 433 434 /* 435 * Try repeatedly to get valid data from the battery. Since the 436 * embedded controller isn't always ready just after boot, we may have 437 * to wait a while. 438 */ 439 for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10000)) { 440 /* batteries on DOCK can be ejected w/ DOCK during retrying */ 441 if (!device_is_attached(dev)) 442 return; 443 444 if (!acpi_BatteryIsPresent(dev)) 445 continue; 446 447 /* 448 * Only query the battery if this is the first try or the specific 449 * type of info is still invalid. 450 */ 451 ACPI_SERIAL_BEGIN(cmbat); 452 if (retry == 0 || !acpi_battery_bst_valid(&sc->bst)) { 453 timespecclear(&sc->bst_lastupdated); 454 acpi_cmbat_get_bst(dev); 455 } 456 if (retry == 0 || !acpi_battery_bif_valid(&sc->bif)) 457 acpi_cmbat_get_bif(dev); 458 459 valid = acpi_battery_bst_valid(&sc->bst) && 460 acpi_battery_bif_valid(&sc->bif); 461 ACPI_SERIAL_END(cmbat); 462 463 if (valid) 464 break; 465 } 466 467 if (retry == ACPI_CMBAT_RETRY_MAX) { 468 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 469 "battery initialization failed, giving up\n"); 470 } else { 471 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), 472 "battery initialization done, tried %d times\n", retry + 1); 473 } 474 } 475