2 * Copyright (c) 2000 Munehiro Matsuda
3 * Copyright (c) 2000 Takanori Watanabe
4 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/ioccom.h>
38 #include <machine/bus.h>
39 #include <machine/resource.h>
41 #include <sys/malloc.h>
45 #include <dev/acpica/acpivar.h>
46 #include <dev/acpica/acpiio.h>
48 MALLOC_DEFINE(M_ACPICMBAT, "acpicmbat", "ACPI control method battery data");
50 #define CMBAT_POLLRATE (60 * hz)
53 * Hooks for the ACPI CA debugging infrastructure
55 #define _COMPONENT ACPI_BATTERY
56 MODULE_NAME("BATTERY")
58 #define ACPI_BATTERY_BST_CHANGE 0x80
59 #define ACPI_BATTERY_BIF_CHANGE 0x81
61 #define PKG_GETINT(res, tmp, idx, dest, label) do { \
62 tmp = &res->Package.Elements[idx]; \
64 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), \
65 __func__ ": PKG_GETINT error, idx = %d\n.", idx); \
68 if (tmp->Type != ACPI_TYPE_INTEGER) \
70 dest = tmp->Integer.Value; \
73 #define PKG_GETSTR(res, tmp, idx, dest, size, label) do { \
76 tmp = &res->Package.Elements[idx]; \
78 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), \
79 __func__ ": PKG_GETSTR error, idx = %d\n.", idx); \
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; \
88 strncpy(dest, tmp->String.Pointer, length); \
90 case ACPI_TYPE_BUFFER: \
91 if (tmp->Buffer.Length < length) { \
92 length = tmp->Buffer.Length; \
94 strncpy(dest, tmp->Buffer.Pointer, length); \
99 dest[sizeof(dest)-1] = '\0'; \
102 struct acpi_cmbat_softc {
107 ACPI_BUFFER bif_buffer;
108 ACPI_BUFFER bst_buffer;
109 struct timespec bif_lastupdated;
110 struct timespec bst_lastupdated;
115 int full_charge_time;
117 struct callout_handle cmbat_timeout;
120 static struct timespec acpi_cmbat_info_lastupdated;
122 /* XXX: devclass_get_maxunit() don't give us the current allocated units... */
123 static int acpi_cmbat_units = 0;
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 *);
138 * Poll the battery info.
141 acpi_cmbat_timeout(void *context)
144 struct acpi_cmbat_softc *sc;
146 dev = (device_t)context;
147 sc = device_get_softc(dev);
149 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
150 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE);
154 acpi_cmbat_info_expired(struct timespec *lastupdated)
156 struct timespec curtime;
158 if (lastupdated == NULL) {
162 if (!timespecisset(lastupdated)) {
166 getnanotime(&curtime);
167 timespecsub(&curtime, lastupdated);
168 return ((curtime.tv_sec < 0 || curtime.tv_sec > acpi_battery_get_info_expire()));
173 acpi_cmbat_info_updated(struct timespec *lastupdated)
176 if (lastupdated != NULL) {
177 getnanotime(lastupdated);
182 acpi_cmbat_get_bst(void *context)
185 struct acpi_cmbat_softc *sc;
187 ACPI_OBJECT *res, *tmp;
191 sc = device_get_softc(dev);
192 h = acpi_get_handle(dev);
194 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) {
198 untimeout(acpi_cmbat_timeout, (caddr_t)dev, sc->cmbat_timeout);
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;
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));
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");
219 bzero(sc->bst_buffer.Pointer, sc->bst_buffer.Length);
220 as = AcpiEvaluateObject(h, "_BST", NULL, &sc->bst_buffer);
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;
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;
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));
237 res = (ACPI_OBJECT *)sc->bst_buffer.Pointer;
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");
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);
251 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE);
255 acpi_cmbat_get_bif(void *context)
258 struct acpi_cmbat_softc *sc;
260 ACPI_OBJECT *res, *tmp;
264 sc = device_get_softc(dev);
265 h = acpi_get_handle(dev);
267 if (!acpi_cmbat_info_expired(&sc->bif_lastupdated)) {
271 untimeout(acpi_cmbat_timeout, (caddr_t)dev, sc->cmbat_timeout);
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;
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));
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");
292 bzero(sc->bif_buffer.Pointer, sc->bif_buffer.Length);
293 as = AcpiEvaluateObject(h, "_BIF", NULL, &sc->bif_buffer);
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;
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;
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));
310 res = (ACPI_OBJECT *)sc->bif_buffer.Pointer;
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");
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);
333 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE);
337 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
340 struct acpi_cmbat_softc *sc;
342 dev = (device_t)context;
343 if ((sc = device_get_softc(dev)) == NULL) {
348 case ACPI_BATTERY_BST_CHANGE:
349 timespecclear(&sc->bst_lastupdated);
351 case ACPI_BATTERY_BIF_CHANGE:
352 timespecclear(&sc->bif_lastupdated);
353 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
361 acpi_cmbat_probe(device_t dev)
364 if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
365 acpi_MatchHid(dev, "PNP0C0A")) {
367 * Set device description.
369 device_set_desc(dev, "Control method Battery");
376 acpi_cmbat_attach(device_t dev)
380 struct acpi_cmbat_softc *sc;
382 if ((sc = device_get_softc(dev)) == NULL) {
386 handle = acpi_get_handle(dev);
388 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
389 acpi_cmbat_notify_handler, dev);
391 bzero(&sc->bif_buffer, sizeof(sc->bif_buffer));
392 bzero(&sc->bst_buffer, sizeof(sc->bst_buffer));
395 timespecclear(&sc->bif_lastupdated);
396 timespecclear(&sc->bst_lastupdated);
398 if (acpi_cmbat_units == 0) {
399 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BIF,
400 acpi_cmbat_ioctl, NULL)) != 0) {
403 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BST,
404 acpi_cmbat_ioctl, NULL)) != 0) {
409 if ((error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT,
410 acpi_cmbat_units)) != 0) {
415 timespecclear(&acpi_cmbat_info_lastupdated);
417 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
422 acpi_cmbat_resume(device_t dev)
425 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
429 static device_method_t acpi_cmbat_methods[] = {
430 /* Device interface */
431 DEVMETHOD(device_probe, acpi_cmbat_probe),
432 DEVMETHOD(device_attach, acpi_cmbat_attach),
433 DEVMETHOD(device_resume, acpi_cmbat_resume),
438 static driver_t acpi_cmbat_driver = {
441 sizeof(struct acpi_cmbat_softc),
444 static devclass_t acpi_cmbat_devclass;
445 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);
448 acpi_cmbat_ioctl(u_long cmd, caddr_t addr, void *arg)
451 union acpi_battery_ioctl_arg *ioctl_arg;
452 struct acpi_cmbat_softc *sc;
453 struct acpi_bif *bifp;
454 struct acpi_bst *bstp;
456 ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
457 if ((dev = devclass_get_device(acpi_cmbat_devclass,
458 ioctl_arg->unit)) == NULL) {
462 if ((sc = device_get_softc(dev)) == NULL) {
467 case ACPIIO_CMBAT_GET_BIF:
468 acpi_cmbat_get_bif(dev);
469 bifp = &ioctl_arg->bif;
470 bifp->unit = sc->bif.unit;
471 bifp->dcap = sc->bif.dcap;
472 bifp->lfcap = sc->bif.lfcap;
473 bifp->btech = sc->bif.btech;
474 bifp->dvol = sc->bif.dvol;
475 bifp->wcap = sc->bif.wcap;
476 bifp->lcap = sc->bif.lcap;
477 bifp->gra1 = sc->bif.gra1;
478 bifp->gra2 = sc->bif.gra2;
479 strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
480 strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
481 strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
482 strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
485 case ACPIIO_CMBAT_GET_BST:
486 acpi_cmbat_get_bst(dev);
487 bstp = &ioctl_arg->bst;
488 bstp->state = sc->bst.state;
489 bstp->rate = sc->bst.rate;
490 bstp->cap = sc->bst.cap;
491 bstp->volt = sc->bst.volt;
499 acpi_cmbat_get_total_battinfo(struct acpi_battinfo *battinfo)
504 int valid_rate, valid_units;
506 int total_cap, total_min, total_full;
508 struct acpi_cmbat_softc *sc;
509 static int bat_units = 0;
510 static struct acpi_cmbat_softc **bat = NULL;
513 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
516 /* Allocate array of softc pointers */
517 if (bat_units != acpi_cmbat_units) {
519 free(bat, M_ACPICMBAT);
525 bat_units = acpi_cmbat_units;
526 bat = malloc(sizeof(struct acpi_cmbat_softc *) * bat_units,
527 M_ACPICMBAT, M_NOWAIT);
533 /* Collect softc pointers */
534 for (i = 0; i < acpi_cmbat_units; i++) {
535 if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) {
540 if ((sc = device_get_softc(dev)) == NULL) {
549 /* Get battery status, valid rate and valid units */
550 batt_stat = valid_rate = valid_units = 0;
551 for (i = 0; i < acpi_cmbat_units; i++) {
552 bat[i]->not_present = 0;
553 acpi_cmbat_get_bst(bat[i]->dev);
555 /* If battey not installed, we get strange values */
556 if (bat[i]->bst.state >= ACPI_BATT_STAT_MAX ||
557 bat[i]->bst.cap == 0xffffffff ||
558 bat[i]->bst.volt == 0xffffffff ||
559 bat[i]->bif.lfcap == 0) {
560 bat[i]->not_present = 1;
566 bat[i]->cap = 100 * bat[i]->bst.cap / bat[i]->bif.lfcap;
568 batt_stat |= bat[i]->bst.state;
570 if (bat[i]->bst.rate > 0) {
572 * XXX Hack to calculate total battery time.
573 * Systems with 2 or more battries, they may get used
574 * one by one, thus bst.rate is set only to the one
575 * in use. For remaining batteries bst.rate = 0, which
576 * makes it impossible to calculate remaining time.
577 * Some other systems may need sum of bst.rate in
578 * dis-charging state.
579 * There for we sum up the bst.rate that is valid
580 * (in dis-charging state), and use the sum to
581 * calcutate remaining batteries' time.
583 if (bat[i]->bst.state & ACPI_BATT_STAT_DISCHARG) {
584 valid_rate += bat[i]->bst.rate;
589 /* Calculate total battery capacity and time */
590 total_cap = total_min = total_full = 0;
591 for (i = 0; i < acpi_cmbat_units; i++) {
592 if (bat[i]->not_present) {
596 if (valid_rate > 0) {
597 /* Use the sum of bst.rate */
598 bat[i]->min = 60 * bat[i]->bst.cap / valid_rate;
599 } else if (bat[i]->full_charge_time > 0) {
600 bat[i]->min = (bat[i]->full_charge_time * bat[i]->cap) / 100;
602 /* Couldn't find valid rate and full battery time */
605 total_min += bat[i]->min;
606 total_cap += bat[i]->cap;
607 total_full += bat[i]->full_charge_time;
611 if (valid_units == 0) {
613 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
615 cap = total_cap / valid_units;
619 if (valid_units == 0) {
621 } else if (valid_rate == 0 || (batt_stat & ACPI_BATT_STAT_CHARGING)) {
622 if (total_full == 0) {
625 min = (total_full * cap) / 100;
631 acpi_cmbat_info_updated(&acpi_cmbat_info_lastupdated);
635 battinfo->state = batt_stat;
645 acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo)
649 struct acpi_cmbat_softc *sc;
652 return (acpi_cmbat_get_total_battinfo(battinfo));
655 if (acpi_cmbat_info_expired(&acpi_cmbat_info_lastupdated)) {
656 error = acpi_cmbat_get_total_battinfo(battinfo);
663 if (unit >= acpi_cmbat_units) {
668 if ((dev = devclass_get_device(acpi_cmbat_devclass, unit)) == NULL) {
673 if ((sc = device_get_softc(dev)) == NULL) {
678 if (sc->not_present) {
681 battinfo->state = ACPI_BATT_STAT_NOT_PRESENT;
683 battinfo->cap = sc->cap;
684 battinfo->min = sc->min;
685 battinfo->state = sc->bst.state;