acpi_battery.c revision 348726
1/*- 2 * Copyright (c) 2005 Nate Lawson 3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: stable/11/sys/dev/acpica/acpi_battery.c 348726 2019-06-06 05:10:32Z allanjude $"); 30 31#include "opt_acpi.h" 32#include <sys/param.h> 33#include <sys/kernel.h> 34#include <sys/malloc.h> 35#include <sys/bus.h> 36#include <sys/ioccom.h> 37#include <sys/sysctl.h> 38 39#include <contrib/dev/acpica/include/acpi.h> 40 41#include <dev/acpica/acpivar.h> 42#include <dev/acpica/acpiio.h> 43 44/* Default seconds before re-sampling the battery state. */ 45#define ACPI_BATTERY_INFO_EXPIRE 5 46 47static int acpi_batteries_initted; 48static int acpi_battery_info_expire = ACPI_BATTERY_INFO_EXPIRE; 49static struct acpi_battinfo acpi_battery_battinfo; 50static struct sysctl_ctx_list acpi_battery_sysctl_ctx; 51static struct sysctl_oid *acpi_battery_sysctl_tree; 52 53ACPI_SERIAL_DECL(battery, "ACPI generic battery"); 54 55static void acpi_reset_battinfo(struct acpi_battinfo *info); 56static void acpi_battery_clean_str(char *str, int len); 57static device_t acpi_battery_find_dev(u_int logical_unit); 58static int acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg); 59static int acpi_battery_sysctl(SYSCTL_HANDLER_ARGS); 60static int acpi_battery_units_sysctl(SYSCTL_HANDLER_ARGS); 61static int acpi_battery_init(void); 62 63int 64acpi_battery_register(device_t dev) 65{ 66 int error; 67 68 error = 0; 69 ACPI_SERIAL_BEGIN(battery); 70 if (!acpi_batteries_initted) 71 error = acpi_battery_init(); 72 ACPI_SERIAL_END(battery); 73 return (error); 74} 75 76int 77acpi_battery_remove(device_t dev) 78{ 79 80 return (0); 81} 82 83int 84acpi_battery_get_units(void) 85{ 86 devclass_t batt_dc; 87 88 batt_dc = devclass_find("battery"); 89 if (batt_dc == NULL) 90 return (0); 91 return (devclass_get_count(batt_dc)); 92} 93 94int 95acpi_battery_get_info_expire(void) 96{ 97 98 return (acpi_battery_info_expire); 99} 100 101/* Check _BST results for validity. */ 102int 103acpi_battery_bst_valid(struct acpi_bst *bst) 104{ 105 106 return (bst->state != ACPI_BATT_STAT_NOT_PRESENT && 107 bst->cap != ACPI_BATT_UNKNOWN && bst->volt != ACPI_BATT_UNKNOWN); 108} 109 110/* Check _BIF results for validity. */ 111int 112acpi_battery_bif_valid(struct acpi_bif *bif) 113{ 114 return (bif->lfcap != 0); 115} 116 117/* Get info about one or all batteries. */ 118int 119acpi_battery_get_battinfo(device_t dev, struct acpi_battinfo *battinfo) 120{ 121 int batt_stat, devcount, dev_idx, error, i; 122 int total_cap, total_lfcap, total_min, valid_rate, valid_units; 123 devclass_t batt_dc; 124 device_t batt_dev; 125 struct acpi_bst *bst; 126 struct acpi_bif *bif; 127 struct acpi_battinfo *bi; 128 129 /* 130 * Get the battery devclass and max unit for battery devices. If there 131 * are none or error, return immediately. 132 */ 133 batt_dc = devclass_find("battery"); 134 if (batt_dc == NULL) 135 return (ENXIO); 136 devcount = devclass_get_maxunit(batt_dc); 137 if (devcount == 0) 138 return (ENXIO); 139 140 /* 141 * Allocate storage for all _BST data, their derived battinfo data, 142 * and the current battery's _BIF data. 143 */ 144 bst = malloc(devcount * sizeof(*bst), M_TEMP, M_WAITOK | M_ZERO); 145 bi = malloc(devcount * sizeof(*bi), M_TEMP, M_WAITOK | M_ZERO); 146 bif = malloc(sizeof(*bif), M_TEMP, M_WAITOK | M_ZERO); 147 148 /* 149 * Pass 1: for each battery that is present and valid, get its status, 150 * calculate percent capacity remaining, and sum all the current 151 * discharge rates. 152 */ 153 dev_idx = -1; 154 batt_stat = valid_rate = valid_units = 0; 155 total_cap = total_lfcap = 0; 156 for (i = 0; i < devcount; i++) { 157 /* Default info for every battery is "not present". */ 158 acpi_reset_battinfo(&bi[i]); 159 160 /* 161 * Find the device. Since devcount is in terms of max units, this 162 * may be a sparse array so skip devices that aren't present. 163 */ 164 batt_dev = devclass_get_device(batt_dc, i); 165 if (batt_dev == NULL) 166 continue; 167 168 /* If examining a specific battery and this is it, record its index. */ 169 if (dev != NULL && dev == batt_dev) 170 dev_idx = i; 171 172 /* 173 * Be sure we can get various info from the battery. Note that 174 * acpi_BatteryIsPresent() is not enough because smart batteries only 175 * return that the device is present. 176 */ 177 if (!acpi_BatteryIsPresent(batt_dev) || 178 ACPI_BATT_GET_STATUS(batt_dev, &bst[i]) != 0 || 179 ACPI_BATT_GET_INFO(batt_dev, bif) != 0) 180 continue; 181 182 /* If a battery is not installed, we sometimes get strange values. */ 183 if (!acpi_battery_bst_valid(&bst[i]) || 184 !acpi_battery_bif_valid(bif)) 185 continue; 186 187 /* 188 * Record current state. If both charging and discharging are set, 189 * ignore the charging flag. 190 */ 191 valid_units++; 192 if ((bst[i].state & ACPI_BATT_STAT_DISCHARG) != 0) 193 bst[i].state &= ~ACPI_BATT_STAT_CHARGING; 194 batt_stat |= bst[i].state; 195 bi[i].state = bst[i].state; 196 197 /* 198 * If the battery info is in terms of mA, convert to mW by 199 * multiplying by the design voltage. If the design voltage 200 * is 0 (due to some error reading the battery), skip this 201 * conversion. 202 */ 203 if (bif->units == ACPI_BIF_UNITS_MA && bif->dvol != 0 && dev == NULL) { 204 bst[i].rate = (bst[i].rate * bif->dvol) / 1000; 205 bst[i].cap = (bst[i].cap * bif->dvol) / 1000; 206 bif->lfcap = (bif->lfcap * bif->dvol) / 1000; 207 } 208 209 /* 210 * The calculation above may set bif->lfcap to zero. This was 211 * seen on a laptop with a broken battery. The result of the 212 * division was rounded to zero. 213 */ 214 if (!acpi_battery_bif_valid(bif)) 215 continue; 216 217 /* 218 * Some laptops report the "design-capacity" instead of the 219 * "real-capacity" when the battery is fully charged. That breaks 220 * the above arithmetic as it needs to be 100% maximum. 221 */ 222 if (bst[i].cap > bif->lfcap) 223 bst[i].cap = bif->lfcap; 224 225 /* Calculate percent capacity remaining. */ 226 bi[i].cap = (100 * bst[i].cap) / bif->lfcap; 227 228 /* If this battery is not present, don't use its capacity. */ 229 if (bi[i].cap != -1) { 230 total_cap += bst[i].cap; 231 total_lfcap += bif->lfcap; 232 } 233 234 /* 235 * On systems with more than one battery, they may get used 236 * sequentially, thus bst.rate may only signify the one currently 237 * in use. For the remaining batteries, bst.rate will be zero, 238 * which makes it impossible to calculate the total remaining time. 239 * Therefore, we sum the bst.rate for batteries in the discharging 240 * state and use the sum to calculate the total remaining time. 241 */ 242 if (bst[i].rate != ACPI_BATT_UNKNOWN && 243 (bst[i].state & ACPI_BATT_STAT_DISCHARG) != 0) 244 valid_rate += bst[i].rate; 245 } 246 247 /* If the caller asked for a device but we didn't find it, error. */ 248 if (dev != NULL && dev_idx == -1) { 249 error = ENXIO; 250 goto out; 251 } 252 253 /* Pass 2: calculate capacity and remaining time for all batteries. */ 254 total_min = 0; 255 for (i = 0; i < devcount; i++) { 256 /* 257 * If any batteries are discharging, use the sum of the bst.rate 258 * values. Otherwise, we are on AC power, and there is infinite 259 * time remaining for this battery until we go offline. 260 */ 261 if (valid_rate > 0) 262 bi[i].min = (60 * bst[i].cap) / valid_rate; 263 else 264 bi[i].min = 0; 265 total_min += bi[i].min; 266 } 267 268 /* 269 * Return total battery percent and time remaining. If there are 270 * no valid batteries, report values as unknown. 271 */ 272 if (valid_units > 0) { 273 if (dev == NULL) { 274 battinfo->cap = (total_cap * 100) / total_lfcap; 275 battinfo->min = total_min; 276 battinfo->state = batt_stat; 277 battinfo->rate = valid_rate; 278 } else { 279 battinfo->cap = bi[dev_idx].cap; 280 battinfo->min = bi[dev_idx].min; 281 battinfo->state = bi[dev_idx].state; 282 battinfo->rate = bst[dev_idx].rate; 283 } 284 285 /* 286 * If the queried battery has no discharge rate or is charging, 287 * report that we don't know the remaining time. 288 */ 289 if (valid_rate == 0 || (battinfo->state & ACPI_BATT_STAT_CHARGING)) 290 battinfo->min = -1; 291 } else 292 acpi_reset_battinfo(battinfo); 293 294 error = 0; 295 296out: 297 if (bi) 298 free(bi, M_TEMP); 299 if (bif) 300 free(bif, M_TEMP); 301 if (bst) 302 free(bst, M_TEMP); 303 return (error); 304} 305 306static void 307acpi_reset_battinfo(struct acpi_battinfo *info) 308{ 309 info->cap = -1; 310 info->min = -1; 311 info->state = ACPI_BATT_STAT_NOT_PRESENT; 312 info->rate = -1; 313} 314 315/* Make string printable, removing invalid chars. */ 316static void 317acpi_battery_clean_str(char *str, int len) 318{ 319 int i; 320 321 for (i = 0; i < len && *str != '\0'; i++, str++) { 322 if (!isprint(*str)) 323 *str = '?'; 324 } 325 326 /* NUL-terminate the string if we reached the end. */ 327 if (i == len) 328 *str = '\0'; 329} 330 331/* 332 * The battery interface deals with devices and methods but userland 333 * expects a logical unit number. Convert a logical unit to a device_t. 334 */ 335static device_t 336acpi_battery_find_dev(u_int logical_unit) 337{ 338 int found_unit, i, maxunit; 339 device_t dev; 340 devclass_t batt_dc; 341 342 dev = NULL; 343 found_unit = 0; 344 batt_dc = devclass_find("battery"); 345 maxunit = devclass_get_maxunit(batt_dc); 346 for (i = 0; i < maxunit; i++) { 347 dev = devclass_get_device(batt_dc, i); 348 if (dev == NULL) 349 continue; 350 if (logical_unit == found_unit) 351 break; 352 found_unit++; 353 dev = NULL; 354 } 355 356 return (dev); 357} 358 359static int 360acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg) 361{ 362 union acpi_battery_ioctl_arg *ioctl_arg; 363 int error, unit; 364 device_t dev; 365 366 /* For commands that use the ioctl_arg struct, validate it first. */ 367 error = ENXIO; 368 unit = 0; 369 dev = NULL; 370 ioctl_arg = NULL; 371 if (IOCPARM_LEN(cmd) == sizeof(*ioctl_arg)) { 372 ioctl_arg = (union acpi_battery_ioctl_arg *)addr; 373 unit = ioctl_arg->unit; 374 if (unit != ACPI_BATTERY_ALL_UNITS) 375 dev = acpi_battery_find_dev(unit); 376 } 377 378 /* 379 * No security check required: information retrieval only. If 380 * new functions are added here, a check might be required. 381 */ 382 switch (cmd) { 383 case ACPIIO_BATT_GET_UNITS: 384 *(int *)addr = acpi_battery_get_units(); 385 error = 0; 386 break; 387 case ACPIIO_BATT_GET_BATTINFO: 388 if (dev != NULL || unit == ACPI_BATTERY_ALL_UNITS) { 389 bzero(&ioctl_arg->battinfo, sizeof(ioctl_arg->battinfo)); 390 error = acpi_battery_get_battinfo(dev, &ioctl_arg->battinfo); 391 } 392 break; 393 case ACPIIO_BATT_GET_BIF: 394 if (dev != NULL) { 395 bzero(&ioctl_arg->bif, sizeof(ioctl_arg->bif)); 396 error = ACPI_BATT_GET_INFO(dev, &ioctl_arg->bif); 397 398 /* 399 * Remove invalid characters. Perhaps this should be done 400 * within a convenience function so all callers get the 401 * benefit. 402 */ 403 acpi_battery_clean_str(ioctl_arg->bif.model, 404 sizeof(ioctl_arg->bif.model)); 405 acpi_battery_clean_str(ioctl_arg->bif.serial, 406 sizeof(ioctl_arg->bif.serial)); 407 acpi_battery_clean_str(ioctl_arg->bif.type, 408 sizeof(ioctl_arg->bif.type)); 409 acpi_battery_clean_str(ioctl_arg->bif.oeminfo, 410 sizeof(ioctl_arg->bif.oeminfo)); 411 } 412 break; 413 case ACPIIO_BATT_GET_BST: 414 if (dev != NULL) { 415 bzero(&ioctl_arg->bst, sizeof(ioctl_arg->bst)); 416 error = ACPI_BATT_GET_STATUS(dev, &ioctl_arg->bst); 417 } 418 break; 419 default: 420 error = EINVAL; 421 } 422 423 return (error); 424} 425 426static int 427acpi_battery_sysctl(SYSCTL_HANDLER_ARGS) 428{ 429 int val, error; 430 431 acpi_battery_get_battinfo(NULL, &acpi_battery_battinfo); 432 val = *(u_int *)oidp->oid_arg1; 433 error = sysctl_handle_int(oidp, &val, 0, req); 434 return (error); 435} 436 437static int 438acpi_battery_units_sysctl(SYSCTL_HANDLER_ARGS) 439{ 440 int count, error; 441 442 count = acpi_battery_get_units(); 443 error = sysctl_handle_int(oidp, &count, 0, req); 444 return (error); 445} 446 447static int 448acpi_battery_init(void) 449{ 450 struct acpi_softc *sc; 451 device_t dev; 452 int error; 453 454 ACPI_SERIAL_ASSERT(battery); 455 456 error = ENXIO; 457 dev = devclass_get_device(devclass_find("acpi"), 0); 458 if (dev == NULL) 459 goto out; 460 sc = device_get_softc(dev); 461 462 error = acpi_register_ioctl(ACPIIO_BATT_GET_UNITS, acpi_battery_ioctl, 463 NULL); 464 if (error != 0) 465 goto out; 466 error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTINFO, acpi_battery_ioctl, 467 NULL); 468 if (error != 0) 469 goto out; 470 error = acpi_register_ioctl(ACPIIO_BATT_GET_BIF, acpi_battery_ioctl, NULL); 471 if (error != 0) 472 goto out; 473 error = acpi_register_ioctl(ACPIIO_BATT_GET_BST, acpi_battery_ioctl, NULL); 474 if (error != 0) 475 goto out; 476 477 sysctl_ctx_init(&acpi_battery_sysctl_ctx); 478 acpi_battery_sysctl_tree = SYSCTL_ADD_NODE(&acpi_battery_sysctl_ctx, 479 SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO, "battery", CTLFLAG_RD, 480 0, "battery status and info"); 481 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx, 482 SYSCTL_CHILDREN(acpi_battery_sysctl_tree), 483 OID_AUTO, "life", CTLTYPE_INT | CTLFLAG_RD, 484 &acpi_battery_battinfo.cap, 0, acpi_battery_sysctl, "I", 485 "percent capacity remaining"); 486 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx, 487 SYSCTL_CHILDREN(acpi_battery_sysctl_tree), 488 OID_AUTO, "time", CTLTYPE_INT | CTLFLAG_RD, 489 &acpi_battery_battinfo.min, 0, acpi_battery_sysctl, "I", 490 "remaining time in minutes"); 491 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx, 492 SYSCTL_CHILDREN(acpi_battery_sysctl_tree), 493 OID_AUTO, "rate", CTLTYPE_INT | CTLFLAG_RD, 494 &acpi_battery_battinfo.rate, 0, acpi_battery_sysctl, "I", 495 "present rate in mW"); 496 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx, 497 SYSCTL_CHILDREN(acpi_battery_sysctl_tree), 498 OID_AUTO, "state", CTLTYPE_INT | CTLFLAG_RD, 499 &acpi_battery_battinfo.state, 0, acpi_battery_sysctl, "I", 500 "current status flags"); 501 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx, 502 SYSCTL_CHILDREN(acpi_battery_sysctl_tree), 503 OID_AUTO, "units", CTLTYPE_INT | CTLFLAG_RD, 504 NULL, 0, acpi_battery_units_sysctl, "I", "number of batteries"); 505 SYSCTL_ADD_INT(&acpi_battery_sysctl_ctx, 506 SYSCTL_CHILDREN(acpi_battery_sysctl_tree), 507 OID_AUTO, "info_expire", CTLFLAG_RW, 508 &acpi_battery_info_expire, 0, 509 "time in seconds until info is refreshed"); 510 511 acpi_batteries_initted = TRUE; 512 513out: 514 if (error != 0) { 515 acpi_deregister_ioctl(ACPIIO_BATT_GET_UNITS, acpi_battery_ioctl); 516 acpi_deregister_ioctl(ACPIIO_BATT_GET_BATTINFO, acpi_battery_ioctl); 517 acpi_deregister_ioctl(ACPIIO_BATT_GET_BIF, acpi_battery_ioctl); 518 acpi_deregister_ioctl(ACPIIO_BATT_GET_BST, acpi_battery_ioctl); 519 } 520 return (error); 521} 522