2 * Copyright (c) 2005 Nate Lawson
3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: head/sys/dev/acpica/acpi_battery.c 227992 2011-11-26 13:43:50Z dumbbell $
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
35 #include <sys/sysctl.h>
39 #include <dev/acpica/acpivar.h>
40 #include <dev/acpica/acpiio.h>
42 /* Default seconds before re-sampling the battery state. */
43 #define ACPI_BATTERY_INFO_EXPIRE 5
45 static int acpi_batteries_initted;
46 static int acpi_battery_info_expire = ACPI_BATTERY_INFO_EXPIRE;
47 static struct acpi_battinfo acpi_battery_battinfo;
48 static struct sysctl_ctx_list acpi_battery_sysctl_ctx;
49 static struct sysctl_oid *acpi_battery_sysctl_tree;
51 ACPI_SERIAL_DECL(battery, "ACPI generic battery");
53 static void acpi_reset_battinfo(struct acpi_battinfo *info);
54 static void acpi_battery_clean_str(char *str, int len);
55 static device_t acpi_battery_find_dev(u_int logical_unit);
56 static int acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg);
57 static int acpi_battery_sysctl(SYSCTL_HANDLER_ARGS);
58 static int acpi_battery_units_sysctl(SYSCTL_HANDLER_ARGS);
59 static int acpi_battery_init(void);
62 acpi_battery_register(device_t dev)
67 ACPI_SERIAL_INIT(battery);
68 ACPI_SERIAL_BEGIN(battery);
69 if (!acpi_batteries_initted)
70 error = acpi_battery_init();
71 ACPI_SERIAL_END(battery);
76 acpi_battery_remove(device_t dev)
83 acpi_battery_get_units(void)
87 batt_dc = devclass_find("battery");
90 return (devclass_get_count(batt_dc));
94 acpi_battery_get_info_expire(void)
97 return (acpi_battery_info_expire);
100 /* Check _BST results for validity. */
102 acpi_battery_bst_valid(struct acpi_bst *bst)
104 return (bst->state != ACPI_BATT_STAT_NOT_PRESENT &&
105 bst->cap != ACPI_BATT_UNKNOWN && bst->volt != ACPI_BATT_UNKNOWN);
108 /* Check _BIF results for validity. */
110 acpi_battery_bif_valid(struct acpi_bif *bif)
112 return (bif->lfcap != 0);
115 /* Get info about one or all batteries. */
117 acpi_battery_get_battinfo(device_t dev, struct acpi_battinfo *battinfo)
119 int batt_stat, devcount, dev_idx, error, i;
120 int total_cap, total_min, valid_rate, valid_units;
123 struct acpi_bst *bst;
124 struct acpi_bif *bif;
125 struct acpi_battinfo *bi;
128 * Get the battery devclass and max unit for battery devices. If there
129 * are none or error, return immediately.
131 batt_dc = devclass_find("battery");
134 devcount = devclass_get_maxunit(batt_dc);
139 * Allocate storage for all _BST data, their derived battinfo data,
140 * and the current battery's _BIF data.
142 bst = kmalloc(devcount * sizeof(*bst), M_TEMP, M_WAITOK | M_ZERO);
143 bi = kmalloc(devcount * sizeof(*bi), M_TEMP, M_WAITOK | M_ZERO);
144 bif = kmalloc(sizeof(*bif), M_TEMP, M_WAITOK | M_ZERO);
147 * Pass 1: for each battery that is present and valid, get its status,
148 * calculate percent capacity remaining, and sum all the current
152 batt_stat = valid_rate = valid_units = 0;
153 for (i = 0; i < devcount; i++) {
154 /* Default info for every battery is "not present". */
155 acpi_reset_battinfo(&bi[i]);
158 * Find the device. Since devcount is in terms of max units, this
159 * may be a sparse array so skip devices that aren't present.
161 batt_dev = devclass_get_device(batt_dc, i);
162 if (batt_dev == NULL)
165 /* If examining a specific battery and this is it, record its index. */
166 if (dev != NULL && dev == batt_dev)
170 * Be sure we can get various info from the battery. Note that
171 * acpi_BatteryIsPresent() is not enough because smart batteries only
172 * return that the device is present.
174 if (!acpi_BatteryIsPresent(batt_dev) ||
175 ACPI_BATT_GET_STATUS(batt_dev, &bst[i]) != 0 ||
176 ACPI_BATT_GET_INFO(batt_dev, bif) != 0)
179 /* If a battery is not installed, we sometimes get strange values. */
180 if (!acpi_battery_bst_valid(&bst[i]) ||
181 !acpi_battery_bif_valid(bif))
185 * Record current state. If both charging and discharging are set,
186 * ignore the charging flag.
189 if ((bst[i].state & ACPI_BATT_STAT_DISCHARG) != 0)
190 bst[i].state &= ~ACPI_BATT_STAT_CHARGING;
191 batt_stat |= bst[i].state;
192 bi[i].state = bst[i].state;
195 * If the battery info is in terms of mA, convert to mW by
196 * multiplying by the design voltage. If the design voltage
197 * is 0 (due to some error reading the battery), skip this
200 if (bif->units == ACPI_BIF_UNITS_MA && bif->dvol != 0 && dev == NULL) {
201 bst[i].rate = (bst[i].rate * bif->dvol) / 1000;
202 bst[i].cap = (bst[i].cap * bif->dvol) / 1000;
203 bif->lfcap = (bif->lfcap * bif->dvol) / 1000;
207 * The calculation above may set bif->lfcap to zero. This was
208 * seen on a laptop with a broken battery. The result of the
209 * division was rounded to zero.
211 if (!acpi_battery_bif_valid(bif))
214 /* Calculate percent capacity remaining. */
215 bi[i].cap = (100 * bst[i].cap) / bif->lfcap;
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.
226 * Some DSDTs report a negative 16-bit value for the rate and/or
229 if (acpi_quirks & ACPI_Q_BATT_RATE_ABS &&
230 bif->units == ACPI_BIF_UNITS_MA &&
231 bst[i].rate != ACPI_BATT_UNKNOWN &&
232 (int16_t)bst[i].rate < 0)
233 bst[i].rate = abs((int16_t)bst[i].rate);
236 * On systems with more than one battery, they may get used
237 * sequentially, thus bst.rate may only signify the one currently
238 * in use. For the remaining batteries, bst.rate will be zero,
239 * which makes it impossible to calculate the total remaining time.
240 * Therefore, we sum the bst.rate for batteries in the discharging
241 * state and use the sum to calculate the total remaining time.
243 if (bst[i].rate != ACPI_BATT_UNKNOWN &&
244 (bst[i].state & ACPI_BATT_STAT_DISCHARG) != 0)
245 valid_rate += bst[i].rate;
248 /* If the caller asked for a device but we didn't find it, error. */
249 if (dev != NULL && dev_idx == -1) {
254 /* Pass 2: calculate capacity and remaining time for all batteries. */
255 total_cap = total_min = 0;
256 for (i = 0; i < devcount; i++) {
258 * If any batteries are discharging, use the sum of the bst.rate
259 * values. Otherwise, we are on AC power, and there is infinite
260 * time remaining for this battery until we go offline.
263 bi[i].min = (60 * bst[i].cap) / valid_rate;
266 total_min += bi[i].min;
268 /* If this battery is not present, don't use its capacity. */
270 total_cap += bi[i].cap;
274 * Return total battery percent and time remaining. If there are
275 * no valid batteries, report values as unknown.
277 if (valid_units > 0) {
279 battinfo->cap = total_cap / valid_units;
280 battinfo->min = total_min;
281 battinfo->state = batt_stat;
282 battinfo->rate = valid_rate;
284 battinfo->cap = bi[dev_idx].cap;
285 battinfo->min = bi[dev_idx].min;
286 battinfo->state = bi[dev_idx].state;
287 battinfo->rate = bst[dev_idx].rate;
291 * If the queried battery has no discharge rate or is charging,
292 * report that we don't know the remaining time.
294 if (valid_rate == 0 || (battinfo->state & ACPI_BATT_STAT_CHARGING))
297 acpi_reset_battinfo(battinfo);
312 acpi_reset_battinfo(struct acpi_battinfo *info)
316 info->state = ACPI_BATT_STAT_NOT_PRESENT;
320 /* Make string printable, removing invalid chars. */
322 acpi_battery_clean_str(char *str, int len)
326 for (i = 0; i < len && *str != '\0'; i++, str++) {
331 /* NUL-terminate the string if we reached the end. */
337 * The battery interface deals with devices and methods but userland
338 * expects a logical unit number. Convert a logical unit to a device_t.
341 acpi_battery_find_dev(u_int logical_unit)
343 int found_unit, i, maxunit;
349 batt_dc = devclass_find("battery");
350 maxunit = devclass_get_maxunit(batt_dc);
351 for (i = 0; i < maxunit; i++) {
352 dev = devclass_get_device(batt_dc, i);
355 if (logical_unit == found_unit)
365 acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg)
367 union acpi_battery_ioctl_arg *ioctl_arg;
371 /* For commands that use the ioctl_arg struct, validate it first. */
376 if (IOCPARM_LEN(cmd) == sizeof(*ioctl_arg)) {
377 ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
378 unit = ioctl_arg->unit;
379 if (unit != ACPI_BATTERY_ALL_UNITS)
380 dev = acpi_battery_find_dev(unit);
384 * No security check required: information retrieval only. If
385 * new functions are added here, a check might be required.
388 case ACPIIO_BATT_GET_UNITS:
389 *(int *)addr = acpi_battery_get_units();
392 case ACPIIO_BATT_GET_BATTINFO:
393 if (dev != NULL || unit == ACPI_BATTERY_ALL_UNITS) {
394 bzero(&ioctl_arg->battinfo, sizeof(ioctl_arg->battinfo));
395 error = acpi_battery_get_battinfo(dev, &ioctl_arg->battinfo);
398 case ACPIIO_BATT_GET_BIF:
400 bzero(&ioctl_arg->bif, sizeof(ioctl_arg->bif));
401 error = ACPI_BATT_GET_INFO(dev, &ioctl_arg->bif);
404 * Remove invalid characters. Perhaps this should be done
405 * within a convenience function so all callers get the
408 acpi_battery_clean_str(ioctl_arg->bif.model,
409 sizeof(ioctl_arg->bif.model));
410 acpi_battery_clean_str(ioctl_arg->bif.serial,
411 sizeof(ioctl_arg->bif.serial));
412 acpi_battery_clean_str(ioctl_arg->bif.type,
413 sizeof(ioctl_arg->bif.type));
414 acpi_battery_clean_str(ioctl_arg->bif.oeminfo,
415 sizeof(ioctl_arg->bif.oeminfo));
418 case ACPIIO_BATT_GET_BST:
420 bzero(&ioctl_arg->bst, sizeof(ioctl_arg->bst));
421 error = ACPI_BATT_GET_STATUS(dev, &ioctl_arg->bst);
432 acpi_battery_sysctl(SYSCTL_HANDLER_ARGS)
436 acpi_battery_get_battinfo(NULL, &acpi_battery_battinfo);
437 val = *(u_int *)oidp->oid_arg1;
438 error = sysctl_handle_int(oidp, &val, 0, req);
443 acpi_battery_units_sysctl(SYSCTL_HANDLER_ARGS)
447 count = acpi_battery_get_units();
448 error = sysctl_handle_int(oidp, &count, 0, req);
453 acpi_battery_init(void)
455 struct acpi_softc *sc;
459 ACPI_SERIAL_ASSERT(battery);
462 dev = devclass_get_device(devclass_find("acpi"), 0);
465 sc = device_get_softc(dev);
467 error = acpi_register_ioctl(ACPIIO_BATT_GET_UNITS, acpi_battery_ioctl,
471 error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTINFO, acpi_battery_ioctl,
475 error = acpi_register_ioctl(ACPIIO_BATT_GET_BIF, acpi_battery_ioctl, NULL);
478 error = acpi_register_ioctl(ACPIIO_BATT_GET_BST, acpi_battery_ioctl, NULL);
482 sysctl_ctx_init(&acpi_battery_sysctl_ctx);
483 acpi_battery_sysctl_tree = SYSCTL_ADD_NODE(&acpi_battery_sysctl_ctx,
484 SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO, "battery", CTLFLAG_RD,
485 0, "battery status and info");
486 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
487 SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
488 OID_AUTO, "life", CTLTYPE_INT | CTLFLAG_RD,
489 &acpi_battery_battinfo.cap, 0, acpi_battery_sysctl, "I",
490 "percent capacity remaining");
491 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
492 SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
493 OID_AUTO, "time", CTLTYPE_INT | CTLFLAG_RD,
494 &acpi_battery_battinfo.min, 0, acpi_battery_sysctl, "I",
495 "remaining time in minutes");
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");
511 acpi_batteries_initted = TRUE;
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);