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;
117 int full_charge_time;
119 struct callout_handle cmbat_timeout;
122 static struct timespec acpi_cmbat_info_lastupdated;
124 /* XXX: devclass_get_maxunit() don't give us the current allocated units... */
125 static int acpi_cmbat_units = 0;
127 static void acpi_cmbat_timeout(void *);
128 static int acpi_cmbat_info_expired(struct timespec *);
129 static void acpi_cmbat_info_updated(struct timespec *);
130 static void acpi_cmbat_get_bst(void *);
131 static void acpi_cmbat_get_bif(void *);
132 static void acpi_cmbat_notify_handler(ACPI_HANDLE, UINT32, void *);
133 static int acpi_cmbat_probe(device_t);
134 static int acpi_cmbat_attach(device_t);
135 static int acpi_cmbat_resume(device_t);
136 static int acpi_cmbat_ioctl(u_long, caddr_t, void *);
137 static int acpi_cmbat_get_total_battinfo(struct acpi_battinfo *);
140 * Poll the battery info.
143 acpi_cmbat_timeout(void *context)
146 struct acpi_cmbat_softc *sc;
148 dev = (device_t)context;
149 sc = device_get_softc(dev);
151 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
152 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE);
156 acpi_cmbat_info_expired(struct timespec *lastupdated)
158 struct timespec curtime;
160 if (lastupdated == NULL) {
164 if (!timespecisset(lastupdated)) {
168 getnanotime(&curtime);
169 timespecsub(&curtime, lastupdated);
170 return ((curtime.tv_sec < 0 || curtime.tv_sec > acpi_battery_get_info_expire()));
175 acpi_cmbat_info_updated(struct timespec *lastupdated)
178 if (lastupdated != NULL) {
179 getnanotime(lastupdated);
184 acpi_cmbat_get_bst(void *context)
187 struct acpi_cmbat_softc *sc;
189 ACPI_OBJECT *res, *tmp;
193 sc = device_get_softc(dev);
194 h = acpi_get_handle(dev);
196 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) {
200 if (sc->bst_updating) {
203 sc->bst_updating = 1;
205 untimeout(acpi_cmbat_timeout, (caddr_t)dev, sc->cmbat_timeout);
207 if ((as = acpi_EvaluateIntoBuffer(h, "_BST", NULL, &sc->bst_buffer)) != AE_OK) {
208 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
209 "error fetching current battery status -- %s\n",
210 AcpiFormatException(as));
214 res = (ACPI_OBJECT *)sc->bst_buffer.Pointer;
216 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 4)) {
217 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
218 "battery status corrupted\n");
222 PKG_GETINT(res, tmp, 0, sc->bst.state, end);
223 PKG_GETINT(res, tmp, 1, sc->bst.rate, end);
224 PKG_GETINT(res, tmp, 2, sc->bst.cap, end);
225 PKG_GETINT(res, tmp, 3, sc->bst.volt, end);
226 acpi_cmbat_info_updated(&sc->bst_lastupdated);
228 sc->bst_updating = 0;
229 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE);
233 acpi_cmbat_get_bif(void *context)
236 struct acpi_cmbat_softc *sc;
238 ACPI_OBJECT *res, *tmp;
242 sc = device_get_softc(dev);
243 h = acpi_get_handle(dev);
245 if (!acpi_cmbat_info_expired(&sc->bif_lastupdated)) {
249 if (sc->bif_updating) {
252 sc->bif_updating = 1;
254 untimeout(acpi_cmbat_timeout, (caddr_t)dev, sc->cmbat_timeout);
256 if ((as = acpi_EvaluateIntoBuffer(h, "_BIF", NULL, &sc->bif_buffer)) != AE_OK) {
257 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
258 "error fetching current battery info -- %s\n",
259 AcpiFormatException(as));
263 res = (ACPI_OBJECT *)sc->bif_buffer.Pointer;
265 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 13)) {
266 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
267 "battery info corrupted\n");
271 PKG_GETINT(res, tmp, 0, sc->bif.unit, end);
272 PKG_GETINT(res, tmp, 1, sc->bif.dcap, end);
273 PKG_GETINT(res, tmp, 2, sc->bif.lfcap, end);
274 PKG_GETINT(res, tmp, 3, sc->bif.btech, end);
275 PKG_GETINT(res, tmp, 4, sc->bif.dvol, end);
276 PKG_GETINT(res, tmp, 5, sc->bif.wcap, end);
277 PKG_GETINT(res, tmp, 6, sc->bif.lcap, end);
278 PKG_GETINT(res, tmp, 7, sc->bif.gra1, end);
279 PKG_GETINT(res, tmp, 8, sc->bif.gra2, end);
280 PKG_GETSTR(res, tmp, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN, end);
281 PKG_GETSTR(res, tmp, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN, end);
282 PKG_GETSTR(res, tmp, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN, end);
283 PKG_GETSTR(res, tmp, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN, end);
284 acpi_cmbat_info_updated(&sc->bif_lastupdated);
286 sc->bif_updating = 0;
287 sc->cmbat_timeout = timeout(acpi_cmbat_timeout, dev, CMBAT_POLLRATE);
291 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
294 struct acpi_cmbat_softc *sc;
296 dev = (device_t)context;
297 if ((sc = device_get_softc(dev)) == NULL) {
302 case ACPI_BATTERY_BST_CHANGE:
303 timespecclear(&sc->bst_lastupdated);
305 case ACPI_BATTERY_BIF_CHANGE:
306 timespecclear(&sc->bif_lastupdated);
307 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
315 acpi_cmbat_probe(device_t dev)
318 if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
319 !acpi_disabled("cmbat") &&
320 acpi_MatchHid(dev, "PNP0C0A")) {
322 * Set device description.
324 device_set_desc(dev, "Control method Battery");
331 acpi_cmbat_attach(device_t dev)
335 struct acpi_cmbat_softc *sc;
337 if ((sc = device_get_softc(dev)) == NULL) {
341 handle = acpi_get_handle(dev);
343 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
344 acpi_cmbat_notify_handler, dev);
346 bzero(&sc->bif_buffer, sizeof(sc->bif_buffer));
347 bzero(&sc->bst_buffer, sizeof(sc->bst_buffer));
348 sc->bif_updating = sc->bst_updating = 0;
351 timespecclear(&sc->bif_lastupdated);
352 timespecclear(&sc->bst_lastupdated);
354 if (acpi_cmbat_units == 0) {
355 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BIF,
356 acpi_cmbat_ioctl, NULL)) != 0) {
359 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BST,
360 acpi_cmbat_ioctl, NULL)) != 0) {
365 if ((error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT,
366 acpi_cmbat_units)) != 0) {
371 timespecclear(&acpi_cmbat_info_lastupdated);
373 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
378 acpi_cmbat_resume(device_t dev)
381 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
385 static device_method_t acpi_cmbat_methods[] = {
386 /* Device interface */
387 DEVMETHOD(device_probe, acpi_cmbat_probe),
388 DEVMETHOD(device_attach, acpi_cmbat_attach),
389 DEVMETHOD(device_resume, acpi_cmbat_resume),
394 static driver_t acpi_cmbat_driver = {
397 sizeof(struct acpi_cmbat_softc),
400 static devclass_t acpi_cmbat_devclass;
401 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);
404 acpi_cmbat_ioctl(u_long cmd, caddr_t addr, void *arg)
407 union acpi_battery_ioctl_arg *ioctl_arg;
408 struct acpi_cmbat_softc *sc;
409 struct acpi_bif *bifp;
410 struct acpi_bst *bstp;
412 ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
413 if ((dev = devclass_get_device(acpi_cmbat_devclass,
414 ioctl_arg->unit)) == NULL) {
418 if ((sc = device_get_softc(dev)) == NULL) {
423 case ACPIIO_CMBAT_GET_BIF:
424 acpi_cmbat_get_bif(dev);
425 bifp = &ioctl_arg->bif;
426 bifp->unit = sc->bif.unit;
427 bifp->dcap = sc->bif.dcap;
428 bifp->lfcap = sc->bif.lfcap;
429 bifp->btech = sc->bif.btech;
430 bifp->dvol = sc->bif.dvol;
431 bifp->wcap = sc->bif.wcap;
432 bifp->lcap = sc->bif.lcap;
433 bifp->gra1 = sc->bif.gra1;
434 bifp->gra2 = sc->bif.gra2;
435 strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
436 strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
437 strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
438 strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
441 case ACPIIO_CMBAT_GET_BST:
442 acpi_cmbat_get_bst(dev);
443 bstp = &ioctl_arg->bst;
444 bstp->state = sc->bst.state;
445 bstp->rate = sc->bst.rate;
446 bstp->cap = sc->bst.cap;
447 bstp->volt = sc->bst.volt;
455 acpi_cmbat_get_total_battinfo(struct acpi_battinfo *battinfo)
460 int valid_rate, valid_units;
462 int total_cap, total_min, total_full;
464 struct acpi_cmbat_softc *sc;
465 static int bat_units = 0;
466 static struct acpi_cmbat_softc **bat = NULL;
469 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
472 /* Allocate array of softc pointers */
473 if (bat_units != acpi_cmbat_units) {
475 free(bat, M_ACPICMBAT);
481 bat_units = acpi_cmbat_units;
482 bat = malloc(sizeof(struct acpi_cmbat_softc *) * bat_units,
483 M_ACPICMBAT, M_NOWAIT);
489 /* Collect softc pointers */
490 for (i = 0; i < acpi_cmbat_units; i++) {
491 if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) {
496 if ((sc = device_get_softc(dev)) == NULL) {
505 /* Get battery status, valid rate and valid units */
506 batt_stat = valid_rate = valid_units = 0;
507 for (i = 0; i < acpi_cmbat_units; i++) {
508 bat[i]->not_present = 0;
509 acpi_cmbat_get_bst(bat[i]->dev);
511 /* If battey not installed, we get strange values */
512 if (bat[i]->bst.state >= ACPI_BATT_STAT_MAX ||
513 bat[i]->bst.cap == 0xffffffff ||
514 bat[i]->bst.volt == 0xffffffff ||
515 bat[i]->bif.lfcap == 0) {
516 bat[i]->not_present = 1;
522 bat[i]->cap = 100 * bat[i]->bst.cap / bat[i]->bif.lfcap;
524 batt_stat |= bat[i]->bst.state;
526 if (bat[i]->bst.rate > 0) {
528 * XXX Hack to calculate total battery time.
529 * Systems with 2 or more battries, they may get used
530 * one by one, thus bst.rate is set only to the one
531 * in use. For remaining batteries bst.rate = 0, which
532 * makes it impossible to calculate remaining time.
533 * Some other systems may need sum of bst.rate in
534 * dis-charging state.
535 * There for we sum up the bst.rate that is valid
536 * (in dis-charging state), and use the sum to
537 * calcutate remaining batteries' time.
539 if (bat[i]->bst.state & ACPI_BATT_STAT_DISCHARG) {
540 valid_rate += bat[i]->bst.rate;
545 /* Calculate total battery capacity and time */
546 total_cap = total_min = total_full = 0;
547 for (i = 0; i < acpi_cmbat_units; i++) {
548 if (bat[i]->not_present) {
552 if (valid_rate > 0) {
553 /* Use the sum of bst.rate */
554 bat[i]->min = 60 * bat[i]->bst.cap / valid_rate;
555 } else if (bat[i]->full_charge_time > 0) {
556 bat[i]->min = (bat[i]->full_charge_time * bat[i]->cap) / 100;
558 /* Couldn't find valid rate and full battery time */
561 total_min += bat[i]->min;
562 total_cap += bat[i]->cap;
563 total_full += bat[i]->full_charge_time;
567 if (valid_units == 0) {
569 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
571 cap = total_cap / valid_units;
575 if (valid_units == 0) {
577 } else if (valid_rate == 0 || (batt_stat & ACPI_BATT_STAT_CHARGING)) {
578 if (total_full == 0) {
581 min = (total_full * cap) / 100;
587 acpi_cmbat_info_updated(&acpi_cmbat_info_lastupdated);
591 battinfo->state = batt_stat;
601 acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo)
605 struct acpi_cmbat_softc *sc;
608 return (acpi_cmbat_get_total_battinfo(battinfo));
611 if (acpi_cmbat_info_expired(&acpi_cmbat_info_lastupdated)) {
612 error = acpi_cmbat_get_total_battinfo(battinfo);
619 if (unit >= acpi_cmbat_units) {
624 if ((dev = devclass_get_device(acpi_cmbat_devclass, unit)) == NULL) {
629 if ((sc = device_get_softc(dev)) == NULL) {
634 if (sc->not_present) {
637 battinfo->state = ACPI_BATT_STAT_NOT_PRESENT;
639 battinfo->cap = sc->cap;
640 battinfo->min = sc->min;
641 battinfo->state = sc->bst.state;