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 device_printf(dev, "%s: PKG_GETINT idx = %d\n.", \
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 device_printf(dev, "%s: PKG_GETSTR idx = %d\n.", \
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 #define CMBAT_DPRINT(dev, x...) do { \
103 if (acpi_get_verbose(acpi_device_get_parent_softc(dev))) \
104 device_printf(dev, x); \
107 struct acpi_cmbat_softc {
112 ACPI_BUFFER bif_buffer;
113 ACPI_BUFFER bst_buffer;
114 struct timespec bif_lastupdated;
115 struct timespec bst_lastupdated;
120 int full_charge_time;
122 struct callout_handle cmbat_timeout;
125 static struct timespec acpi_cmbat_info_lastupdated;
127 /* XXX: devclass_get_maxunit() don't give us the current allocated units... */
128 static int acpi_cmbat_units = 0;
130 static void acpi_cmbat_timeout(void *);
131 static int acpi_cmbat_info_expired(struct timespec *);
132 static void acpi_cmbat_info_updated(struct timespec *);
133 static void acpi_cmbat_get_bst(void *);
134 static void acpi_cmbat_get_bif(void *);
135 static void acpi_cmbat_notify_handler(ACPI_HANDLE, UINT32, void *);
136 static int acpi_cmbat_probe(device_t);
137 static int acpi_cmbat_attach(device_t);
138 static int acpi_cmbat_resume(device_t);
139 static int acpi_cmbat_ioctl(u_long, caddr_t, void *);
140 static int acpi_cmbat_get_total_battinfo(struct acpi_battinfo *);
143 * Poll the battery info.
146 acpi_cmbat_timeout(void *context)
149 struct acpi_cmbat_softc *sc;
151 dev = (device_t)context;
152 sc = device_get_softc(dev);
154 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
155 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE);
159 acpi_cmbat_info_expired(struct timespec *lastupdated)
161 struct timespec curtime;
163 if (lastupdated == NULL) {
167 if (!timespecisset(lastupdated)) {
171 getnanotime(&curtime);
172 timespecsub(&curtime, lastupdated);
173 return ((curtime.tv_sec < 0 || curtime.tv_sec > acpi_battery_get_info_expire()));
178 acpi_cmbat_info_updated(struct timespec *lastupdated)
181 if (lastupdated != NULL) {
182 getnanotime(lastupdated);
187 acpi_cmbat_get_bst(void *context)
190 struct acpi_cmbat_softc *sc;
192 ACPI_OBJECT *res, *tmp;
196 sc = device_get_softc(dev);
197 h = acpi_get_handle(dev);
199 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) {
203 untimeout(acpi_cmbat_timeout, (caddr_t)dev, sc->cmbat_timeout);
205 if (sc->bst_buffer.Length == 0) {
206 if (sc->bst_buffer.Pointer != NULL) {
207 free(sc->bst_buffer.Pointer, M_ACPICMBAT);
208 sc->bst_buffer.Pointer = NULL;
210 as = AcpiEvaluateObject(h, "_BST", NULL, &sc->bst_buffer);
211 if (as != AE_BUFFER_OVERFLOW) {
212 CMBAT_DPRINT(dev, "CANNOT FOUND _BST - %s\n",
213 AcpiFormatException(as));
217 sc->bst_buffer.Pointer = malloc(sc->bst_buffer.Length, M_ACPICMBAT, M_NOWAIT);
218 if (sc->bst_buffer.Pointer == NULL) {
219 device_printf(dev, "malloc failed");
224 bzero(sc->bst_buffer.Pointer, sc->bst_buffer.Length);
225 as = AcpiEvaluateObject(h, "_BST", NULL, &sc->bst_buffer);
227 if (as == AE_BUFFER_OVERFLOW) {
228 if (sc->bst_buffer.Pointer != NULL) {
229 free(sc->bst_buffer.Pointer, M_ACPICMBAT);
230 sc->bst_buffer.Pointer = NULL;
232 CMBAT_DPRINT(dev, "bst size changed to %d\n", sc->bst_buffer.Length);
233 sc->bst_buffer.Length = 0;
235 } else if (as != AE_OK) {
236 CMBAT_DPRINT(dev, "CANNOT FOUND _BST - %s\n",
237 AcpiFormatException(as));
241 res = (ACPI_OBJECT *)sc->bst_buffer.Pointer;
243 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 4)) {
244 CMBAT_DPRINT(dev, "Battery status corrupted\n");
248 PKG_GETINT(res, tmp, 0, sc->bst.state, end);
249 PKG_GETINT(res, tmp, 1, sc->bst.rate, end);
250 PKG_GETINT(res, tmp, 2, sc->bst.cap, end);
251 PKG_GETINT(res, tmp, 3, sc->bst.volt, end);
252 acpi_cmbat_info_updated(&sc->bst_lastupdated);
254 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE);
258 acpi_cmbat_get_bif(void *context)
261 struct acpi_cmbat_softc *sc;
263 ACPI_OBJECT *res, *tmp;
267 sc = device_get_softc(dev);
268 h = acpi_get_handle(dev);
270 if (!acpi_cmbat_info_expired(&sc->bif_lastupdated)) {
274 untimeout(acpi_cmbat_timeout, (caddr_t)dev, sc->cmbat_timeout);
276 if (sc->bif_buffer.Length == 0) {
277 if (sc->bif_buffer.Pointer != NULL) {
278 free(sc->bif_buffer.Pointer, M_ACPICMBAT);
279 sc->bif_buffer.Pointer = NULL;
281 as = AcpiEvaluateObject(h, "_BIF", NULL, &sc->bif_buffer);
282 if (as != AE_BUFFER_OVERFLOW) {
283 CMBAT_DPRINT(dev, "CANNOT FOUND _BIF - %s\n",
284 AcpiFormatException(as));
288 sc->bif_buffer.Pointer = malloc(sc->bif_buffer.Length, M_ACPICMBAT, M_NOWAIT);
289 if (sc->bif_buffer.Pointer == NULL) {
290 device_printf(dev, "malloc failed");
295 bzero(sc->bif_buffer.Pointer, sc->bif_buffer.Length);
296 as = AcpiEvaluateObject(h, "_BIF", NULL, &sc->bif_buffer);
298 if (as == AE_BUFFER_OVERFLOW) {
299 if (sc->bif_buffer.Pointer != NULL) {
300 free(sc->bif_buffer.Pointer, M_ACPICMBAT);
301 sc->bif_buffer.Pointer = NULL;
303 CMBAT_DPRINT(dev, "bif size changed to %d\n", sc->bif_buffer.Length);
304 sc->bif_buffer.Length = 0;
306 } else if (as != AE_OK) {
307 CMBAT_DPRINT(dev, "CANNOT FOUND _BIF - %s\n",
308 AcpiFormatException(as));
312 res = (ACPI_OBJECT *)sc->bif_buffer.Pointer;
314 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 13)) {
315 CMBAT_DPRINT(dev, "Battery info corrupted\n");
319 PKG_GETINT(res, tmp, 0, sc->bif.unit, end);
320 PKG_GETINT(res, tmp, 1, sc->bif.dcap, end);
321 PKG_GETINT(res, tmp, 2, sc->bif.lfcap, end);
322 PKG_GETINT(res, tmp, 3, sc->bif.btech, end);
323 PKG_GETINT(res, tmp, 4, sc->bif.dvol, end);
324 PKG_GETINT(res, tmp, 5, sc->bif.wcap, end);
325 PKG_GETINT(res, tmp, 6, sc->bif.lcap, end);
326 PKG_GETINT(res, tmp, 7, sc->bif.gra1, end);
327 PKG_GETINT(res, tmp, 8, sc->bif.gra2, end);
328 PKG_GETSTR(res, tmp, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN, end);
329 PKG_GETSTR(res, tmp, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN, end);
330 PKG_GETSTR(res, tmp, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN, end);
331 PKG_GETSTR(res, tmp, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN, end);
332 acpi_cmbat_info_updated(&sc->bif_lastupdated);
334 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE);
338 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
341 struct acpi_cmbat_softc *sc;
343 dev = (device_t)context;
344 if ((sc = device_get_softc(dev)) == NULL) {
349 case ACPI_BATTERY_BST_CHANGE:
350 timespecclear(&sc->bst_lastupdated);
352 case ACPI_BATTERY_BIF_CHANGE:
353 timespecclear(&sc->bif_lastupdated);
354 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
362 acpi_cmbat_probe(device_t dev)
365 if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
366 acpi_MatchHid(dev, "PNP0C0A")) {
368 * Set device description.
370 device_set_desc(dev, "Control method Battery");
377 acpi_cmbat_attach(device_t dev)
381 struct acpi_cmbat_softc *sc;
383 if ((sc = device_get_softc(dev)) == NULL) {
387 handle = acpi_get_handle(dev);
389 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
390 acpi_cmbat_notify_handler, dev);
392 bzero(&sc->bif_buffer, sizeof(sc->bif_buffer));
393 bzero(&sc->bst_buffer, sizeof(sc->bst_buffer));
396 timespecclear(&sc->bif_lastupdated);
397 timespecclear(&sc->bst_lastupdated);
399 if (acpi_cmbat_units == 0) {
400 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BIF,
401 acpi_cmbat_ioctl, NULL)) != 0) {
404 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BST,
405 acpi_cmbat_ioctl, NULL)) != 0) {
410 if ((error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT,
411 acpi_cmbat_units)) != 0) {
416 timespecclear(&acpi_cmbat_info_lastupdated);
418 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
423 acpi_cmbat_resume(device_t dev)
426 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
430 static device_method_t acpi_cmbat_methods[] = {
431 /* Device interface */
432 DEVMETHOD(device_probe, acpi_cmbat_probe),
433 DEVMETHOD(device_attach, acpi_cmbat_attach),
434 DEVMETHOD(device_resume, acpi_cmbat_resume),
439 static driver_t acpi_cmbat_driver = {
442 sizeof(struct acpi_cmbat_softc),
445 static devclass_t acpi_cmbat_devclass;
446 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);
449 acpi_cmbat_ioctl(u_long cmd, caddr_t addr, void *arg)
452 union acpi_battery_ioctl_arg *ioctl_arg;
453 struct acpi_cmbat_softc *sc;
454 struct acpi_bif *bifp;
455 struct acpi_bst *bstp;
457 ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
458 if ((dev = devclass_get_device(acpi_cmbat_devclass,
459 ioctl_arg->unit)) == NULL) {
463 if ((sc = device_get_softc(dev)) == NULL) {
468 case ACPIIO_CMBAT_GET_BIF:
469 acpi_cmbat_get_bif(dev);
470 bifp = &ioctl_arg->bif;
471 bifp->unit = sc->bif.unit;
472 bifp->dcap = sc->bif.dcap;
473 bifp->lfcap = sc->bif.lfcap;
474 bifp->btech = sc->bif.btech;
475 bifp->dvol = sc->bif.dvol;
476 bifp->wcap = sc->bif.wcap;
477 bifp->lcap = sc->bif.lcap;
478 bifp->gra1 = sc->bif.gra1;
479 bifp->gra2 = sc->bif.gra2;
480 strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
481 strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
482 strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
483 strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
486 case ACPIIO_CMBAT_GET_BST:
487 acpi_cmbat_get_bst(dev);
488 bstp = &ioctl_arg->bst;
489 bstp->state = sc->bst.state;
490 bstp->rate = sc->bst.rate;
491 bstp->cap = sc->bst.cap;
492 bstp->volt = sc->bst.volt;
500 acpi_cmbat_get_total_battinfo(struct acpi_battinfo *battinfo)
505 int valid_rate, valid_units;
507 int total_cap, total_min, total_full;
509 struct acpi_cmbat_softc *sc;
510 static int bat_units = 0;
511 static struct acpi_cmbat_softc **bat = NULL;
514 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
517 /* Allocate array of softc pointers */
518 if (bat_units != acpi_cmbat_units) {
520 free(bat, M_ACPICMBAT);
526 bat_units = acpi_cmbat_units;
527 bat = malloc(sizeof(struct acpi_cmbat_softc *) * bat_units,
528 M_ACPICMBAT, M_NOWAIT);
534 /* Collect softc pointers */
535 for (i = 0; i < acpi_cmbat_units; i++) {
536 if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) {
541 if ((sc = device_get_softc(dev)) == NULL) {
550 /* Get battery status, valid rate and valid units */
551 batt_stat = valid_rate = valid_units = 0;
552 for (i = 0; i < acpi_cmbat_units; i++) {
553 bat[i]->not_present = 0;
554 acpi_cmbat_get_bst(bat[i]->dev);
556 /* If battey not installed, we get strange values */
557 if (bat[i]->bst.state >= ACPI_BATT_STAT_MAX ||
558 bat[i]->bst.cap == 0xffffffff ||
559 bat[i]->bst.volt == 0xffffffff ||
560 bat[i]->bif.lfcap == 0) {
561 bat[i]->not_present = 1;
567 bat[i]->cap = 100 * bat[i]->bst.cap / bat[i]->bif.lfcap;
569 batt_stat |= bat[i]->bst.state;
571 if (bat[i]->bst.rate > 0) {
573 * XXX Hack to calculate total battery time.
574 * Systems with 2 or more battries, they may get used
575 * one by one, thus bst.rate is set only to the one
576 * in use. For remaining batteries bst.rate = 0, which
577 * makes it impossible to calculate remaining time.
578 * Some other systems may need sum of bst.rate in
579 * dis-charging state.
580 * There for we sum up the bst.rate that is valid
581 * (in dis-charging state), and use the sum to
582 * calcutate remaining batteries' time.
584 if (bat[i]->bst.state & ACPI_BATT_STAT_DISCHARG) {
585 valid_rate += bat[i]->bst.rate;
590 /* Calculate total battery capacity and time */
591 total_cap = total_min = total_full = 0;
592 for (i = 0; i < acpi_cmbat_units; i++) {
593 if (bat[i]->not_present) {
597 if (valid_rate > 0) {
598 /* Use the sum of bst.rate */
599 bat[i]->min = 60 * bat[i]->bst.cap / valid_rate;
600 } else if (bat[i]->full_charge_time > 0) {
601 bat[i]->min = (bat[i]->full_charge_time * bat[i]->cap) / 100;
603 /* Couldn't find valid rate and full battery time */
606 total_min += bat[i]->min;
607 total_cap += bat[i]->cap;
608 total_full += bat[i]->full_charge_time;
612 if (valid_units == 0) {
614 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
616 cap = total_cap / valid_units;
620 if (valid_units == 0) {
622 } else if (valid_rate == 0 || (batt_stat & ACPI_BATT_STAT_CHARGING)) {
623 if (total_full == 0) {
626 min = (total_full * cap) / 100;
632 acpi_cmbat_info_updated(&acpi_cmbat_info_lastupdated);
636 battinfo->state = batt_stat;
646 acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo)
650 struct acpi_cmbat_softc *sc;
653 return (acpi_cmbat_get_total_battinfo(battinfo));
656 if (acpi_cmbat_info_expired(&acpi_cmbat_info_lastupdated)) {
657 error = acpi_cmbat_get_total_battinfo(battinfo);
664 if (unit >= acpi_cmbat_units) {
669 if ((dev = devclass_get_device(acpi_cmbat_devclass, unit)) == NULL) {
674 if ((sc = device_get_softc(dev)) == NULL) {
679 if (sc->not_present) {
682 battinfo->state = ACPI_BATT_STAT_NOT_PRESENT;
684 battinfo->cap = sc->cap;
685 battinfo->min = sc->min;
686 battinfo->state = sc->bst.state;