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
28 * $FreeBSD: src/sys/dev/acpica/acpi_cmbat.c,v 1.22.2.1 2003/08/22 20:49:20 jhb Exp $
29 * $DragonFly: src/sys/dev/acpica/Attic/acpi_cmbat.c,v 1.2 2004/05/05 22:19:23 dillon Exp $
33 #include <sys/param.h>
34 #include <sys/kernel.h>
36 #include <sys/ioccom.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
42 #include <sys/malloc.h>
46 #include <dev/acpica/acpivar.h>
47 #include <dev/acpica/acpiio.h>
49 MALLOC_DEFINE(M_ACPICMBAT, "acpicmbat", "ACPI control method battery data");
51 #define CMBAT_POLLRATE (60 * hz)
54 * Hooks for the ACPI CA debugging infrastructure
56 #define _COMPONENT ACPI_BATTERY
57 ACPI_MODULE_NAME("BATTERY")
59 #define ACPI_BATTERY_BST_CHANGE 0x80
60 #define ACPI_BATTERY_BIF_CHANGE 0x81
62 #define PKG_GETINT(res, tmp, idx, dest, label) do { \
63 tmp = &res->Package.Elements[idx]; \
65 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), \
66 "%s: PKG_GETINT error, idx = %d\n.", __func__, idx); \
69 if (tmp->Type != ACPI_TYPE_INTEGER) \
71 dest = tmp->Integer.Value; \
74 #define PKG_GETSTR(res, tmp, idx, dest, size, label) do { \
77 tmp = &res->Package.Elements[idx]; \
79 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), \
80 "%s: PKG_GETSTR error, idx = %d\n.", __func__, idx); \
83 bzero(dest, sizeof(dest)); \
84 switch (tmp->Type) { \
85 case ACPI_TYPE_STRING: \
86 if (tmp->String.Length < length) { \
87 length = tmp->String.Length; \
89 strncpy(dest, tmp->String.Pointer, length); \
91 case ACPI_TYPE_BUFFER: \
92 if (tmp->Buffer.Length < length) { \
93 length = tmp->Buffer.Length; \
95 strncpy(dest, tmp->Buffer.Pointer, length); \
100 dest[sizeof(dest)-1] = '\0'; \
103 struct acpi_cmbat_softc {
108 struct timespec bif_lastupdated;
109 struct timespec bst_lastupdated;
116 int full_charge_time;
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 int acpi_cmbat_info_expired(struct timespec *);
126 static void acpi_cmbat_info_updated(struct timespec *);
127 static void acpi_cmbat_get_bst(void *);
128 static void acpi_cmbat_get_bif(void *);
129 static void acpi_cmbat_notify_handler(ACPI_HANDLE, UINT32, void *);
130 static int acpi_cmbat_probe(device_t);
131 static int acpi_cmbat_attach(device_t);
132 static int acpi_cmbat_resume(device_t);
133 static int acpi_cmbat_ioctl(u_long, caddr_t, void *);
134 static int acpi_cmbat_is_bst_valid(struct acpi_bst*);
135 static int acpi_cmbat_is_bif_valid(struct acpi_bif*);
136 static int acpi_cmbat_get_total_battinfo(struct acpi_battinfo *);
137 static void acpi_cmbat_init_battery(void *);
140 acpi_cmbat_info_expired(struct timespec *lastupdated)
142 struct timespec curtime;
144 if (lastupdated == NULL) {
148 if (!timespecisset(lastupdated)) {
152 getnanotime(&curtime);
153 timespecsub(&curtime, lastupdated);
154 return ((curtime.tv_sec < 0 || curtime.tv_sec > acpi_battery_get_info_expire()));
159 acpi_cmbat_info_updated(struct timespec *lastupdated)
162 if (lastupdated != NULL) {
163 getnanotime(lastupdated);
168 acpi_cmbat_get_bst(void *context)
171 struct acpi_cmbat_softc *sc;
173 ACPI_OBJECT *res, *tmp;
175 ACPI_BUFFER bst_buffer;
178 sc = device_get_softc(dev);
179 h = acpi_get_handle(dev);
180 bst_buffer.Pointer = NULL;
182 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) {
186 if (sc->bst_updating) {
189 sc->bst_updating = 1;
191 bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
192 if (ACPI_FAILURE(as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer))) {
193 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
194 "error fetching current battery status -- %s\n",
195 AcpiFormatException(as));
199 res = (ACPI_OBJECT *)bst_buffer.Pointer;
201 if ((res == NULL) || (res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 4)) {
202 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
203 "battery status corrupted\n");
207 PKG_GETINT(res, tmp, 0, sc->bst.state, end);
208 PKG_GETINT(res, tmp, 1, sc->bst.rate, end);
209 PKG_GETINT(res, tmp, 2, sc->bst.cap, end);
210 PKG_GETINT(res, tmp, 3, sc->bst.volt, end);
211 acpi_cmbat_info_updated(&sc->bst_lastupdated);
213 if (bst_buffer.Pointer != NULL)
214 AcpiOsFree(bst_buffer.Pointer);
215 sc->bst_updating = 0;
219 acpi_cmbat_get_bif(void *context)
222 struct acpi_cmbat_softc *sc;
224 ACPI_OBJECT *res, *tmp;
226 ACPI_BUFFER bif_buffer;
229 sc = device_get_softc(dev);
230 h = acpi_get_handle(dev);
231 bif_buffer.Pointer = NULL;
233 if (!acpi_cmbat_info_expired(&sc->bif_lastupdated)) {
237 if (sc->bif_updating) {
240 sc->bif_updating = 1;
242 bif_buffer.Length = ACPI_ALLOCATE_BUFFER;
243 if (ACPI_FAILURE(as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer))) {
244 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
245 "error fetching current battery info -- %s\n",
246 AcpiFormatException(as));
250 res = (ACPI_OBJECT *)bif_buffer.Pointer;
252 if ((res == NULL) || (res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 13)) {
253 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
254 "battery info corrupted\n");
258 PKG_GETINT(res, tmp, 0, sc->bif.unit, end);
259 PKG_GETINT(res, tmp, 1, sc->bif.dcap, end);
260 PKG_GETINT(res, tmp, 2, sc->bif.lfcap, end);
261 PKG_GETINT(res, tmp, 3, sc->bif.btech, end);
262 PKG_GETINT(res, tmp, 4, sc->bif.dvol, end);
263 PKG_GETINT(res, tmp, 5, sc->bif.wcap, end);
264 PKG_GETINT(res, tmp, 6, sc->bif.lcap, end);
265 PKG_GETINT(res, tmp, 7, sc->bif.gra1, end);
266 PKG_GETINT(res, tmp, 8, sc->bif.gra2, end);
267 PKG_GETSTR(res, tmp, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN, end);
268 PKG_GETSTR(res, tmp, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN, end);
269 PKG_GETSTR(res, tmp, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN, end);
270 PKG_GETSTR(res, tmp, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN, end);
271 acpi_cmbat_info_updated(&sc->bif_lastupdated);
273 if (bif_buffer.Pointer != NULL)
274 AcpiOsFree(bif_buffer.Pointer);
275 sc->bif_updating = 0;
279 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
282 struct acpi_cmbat_softc *sc;
284 dev = (device_t)context;
285 if ((sc = device_get_softc(dev)) == NULL) {
290 case ACPI_BATTERY_BST_CHANGE:
291 timespecclear(&sc->bst_lastupdated);
293 case ACPI_BATTERY_BIF_CHANGE:
294 timespecclear(&sc->bif_lastupdated);
295 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
303 acpi_cmbat_probe(device_t dev)
306 if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
307 !acpi_disabled("cmbat") &&
308 acpi_MatchHid(dev, "PNP0C0A")) {
310 * Set device description.
312 device_set_desc(dev, "Control method Battery");
319 acpi_cmbat_attach(device_t dev)
323 struct acpi_cmbat_softc *sc;
325 if ((sc = device_get_softc(dev)) == NULL) {
329 handle = acpi_get_handle(dev);
331 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
332 acpi_cmbat_notify_handler, dev);
334 sc->bif_updating = sc->bst_updating = 0;
337 timespecclear(&sc->bif_lastupdated);
338 timespecclear(&sc->bst_lastupdated);
340 if (acpi_cmbat_units == 0) {
341 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BIF,
342 acpi_cmbat_ioctl, NULL)) != 0) {
345 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BST,
346 acpi_cmbat_ioctl, NULL)) != 0) {
351 if ((error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT,
352 acpi_cmbat_units)) != 0) {
357 timespecclear(&acpi_cmbat_info_lastupdated);
358 sc->initializing = 0;
360 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_init_battery, dev);
365 acpi_cmbat_resume(device_t dev)
368 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_init_battery, dev);
372 static device_method_t acpi_cmbat_methods[] = {
373 /* Device interface */
374 DEVMETHOD(device_probe, acpi_cmbat_probe),
375 DEVMETHOD(device_attach, acpi_cmbat_attach),
376 DEVMETHOD(device_resume, acpi_cmbat_resume),
381 static driver_t acpi_cmbat_driver = {
384 sizeof(struct acpi_cmbat_softc),
387 static devclass_t acpi_cmbat_devclass;
388 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);
391 acpi_cmbat_ioctl(u_long cmd, caddr_t addr, void *arg)
394 union acpi_battery_ioctl_arg *ioctl_arg;
395 struct acpi_cmbat_softc *sc;
396 struct acpi_bif *bifp;
397 struct acpi_bst *bstp;
399 ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
400 if ((dev = devclass_get_device(acpi_cmbat_devclass,
401 ioctl_arg->unit)) == NULL) {
405 if ((sc = device_get_softc(dev)) == NULL) {
410 * No security check required: information retrieval only. If
411 * new functions are added here, a check might be required.
415 case ACPIIO_CMBAT_GET_BIF:
416 acpi_cmbat_get_bif(dev);
417 bifp = &ioctl_arg->bif;
418 bifp->unit = sc->bif.unit;
419 bifp->dcap = sc->bif.dcap;
420 bifp->lfcap = sc->bif.lfcap;
421 bifp->btech = sc->bif.btech;
422 bifp->dvol = sc->bif.dvol;
423 bifp->wcap = sc->bif.wcap;
424 bifp->lcap = sc->bif.lcap;
425 bifp->gra1 = sc->bif.gra1;
426 bifp->gra2 = sc->bif.gra2;
427 strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
428 strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
429 strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
430 strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
433 case ACPIIO_CMBAT_GET_BST:
434 bstp = &ioctl_arg->bst;
435 if (acpi_BatteryIsPresent(dev)) {
436 acpi_cmbat_get_bst(dev);
437 bstp->state = sc->bst.state;
438 bstp->rate = sc->bst.rate;
439 bstp->cap = sc->bst.cap;
440 bstp->volt = sc->bst.volt;
442 bstp->state = ACPI_BATT_STAT_NOT_PRESENT;
450 acpi_cmbat_is_bst_valid(struct acpi_bst *bst)
452 if (bst->state >= ACPI_BATT_STAT_MAX ||
453 bst->cap == 0xffffffff ||
454 bst->volt == 0xffffffff) {
462 acpi_cmbat_is_bif_valid(struct acpi_bif *bif)
464 if (bif->lfcap == 0) {
472 acpi_cmbat_get_total_battinfo(struct acpi_battinfo *battinfo)
477 int valid_rate, valid_units;
479 int total_cap, total_min, total_full;
481 struct acpi_cmbat_softc *sc;
482 static int bat_units = 0;
483 static struct acpi_cmbat_softc **bat = NULL;
486 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
489 /* Allocate array of softc pointers */
490 if (bat_units != acpi_cmbat_units) {
492 free(bat, M_ACPICMBAT);
498 bat_units = acpi_cmbat_units;
499 bat = malloc(sizeof(struct acpi_cmbat_softc *) * bat_units,
500 M_ACPICMBAT, M_INTWAIT);
502 /* Collect softc pointers */
503 for (i = 0; i < acpi_cmbat_units; i++) {
504 if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) {
509 if ((sc = device_get_softc(dev)) == NULL) {
518 /* Get battery status, valid rate and valid units */
519 batt_stat = valid_rate = valid_units = 0;
520 for (i = 0; i < acpi_cmbat_units; i++) {
521 bat[i]->present = acpi_BatteryIsPresent(bat[i]->dev);
522 if (!bat[i]->present)
525 acpi_cmbat_get_bst(bat[i]->dev);
527 /* If battey not installed, we get strange values */
528 if (!acpi_cmbat_is_bst_valid(&(bat[i]->bst)) ||
529 !acpi_cmbat_is_bif_valid(&(bat[i]->bif))) {
536 bat[i]->cap = 100 * bat[i]->bst.cap / bat[i]->bif.lfcap;
538 batt_stat |= bat[i]->bst.state;
540 if (bat[i]->bst.rate > 0) {
542 * XXX Hack to calculate total battery time.
543 * Systems with 2 or more battries, they may get used
544 * one by one, thus bst.rate is set only to the one
545 * in use. For remaining batteries bst.rate = 0, which
546 * makes it impossible to calculate remaining time.
547 * Some other systems may need sum of bst.rate in
548 * dis-charging state.
549 * There for we sum up the bst.rate that is valid
550 * (in dis-charging state), and use the sum to
551 * calcutate remaining batteries' time.
553 if (bat[i]->bst.state & ACPI_BATT_STAT_DISCHARG) {
554 valid_rate += bat[i]->bst.rate;
559 /* Calculate total battery capacity and time */
560 total_cap = total_min = total_full = 0;
561 for (i = 0; i < acpi_cmbat_units; i++) {
562 if (!bat[i]->present) {
566 if (valid_rate > 0) {
567 /* Use the sum of bst.rate */
568 bat[i]->min = 60 * bat[i]->bst.cap / valid_rate;
569 } else if (bat[i]->full_charge_time > 0) {
570 bat[i]->min = (bat[i]->full_charge_time * bat[i]->cap) / 100;
572 /* Couldn't find valid rate and full battery time */
575 total_min += bat[i]->min;
576 total_cap += bat[i]->cap;
577 total_full += bat[i]->full_charge_time;
581 if (valid_units == 0) {
583 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
585 cap = total_cap / valid_units;
589 if (valid_units == 0) {
591 } else if (valid_rate == 0 || (batt_stat & ACPI_BATT_STAT_CHARGING)) {
592 if (total_full == 0) {
595 min = (total_full * cap) / 100;
601 acpi_cmbat_info_updated(&acpi_cmbat_info_lastupdated);
605 battinfo->state = batt_stat;
611 acpi_cmbat_init_battery(void *arg)
614 device_t dev = (device_t)arg;
615 struct acpi_cmbat_softc *sc = device_get_softc(dev);
616 #define ACPI_CMBAT_RETRY_MAX 6
618 if (sc->initializing) {
622 sc->initializing = 1;
624 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
625 "battery initialization start\n");
627 for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10, 0)) {
628 sc->present = acpi_BatteryIsPresent(dev);
633 timespecclear(&sc->bst_lastupdated);
634 timespecclear(&sc->bif_lastupdated);
636 acpi_cmbat_get_bst(dev);
638 if (!acpi_cmbat_is_bst_valid(&sc->bst)) {
642 acpi_cmbat_get_bif(dev);
644 if (!acpi_cmbat_is_bif_valid(&sc->bif)) {
651 if (retry == ACPI_CMBAT_RETRY_MAX)
652 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
653 "battery initialization failed, giving up\n");
655 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
656 "battery initialization done, tried %d times\n",
659 sc->initializing = 0;
667 acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo)
671 struct acpi_cmbat_softc *sc;
674 return (acpi_cmbat_get_total_battinfo(battinfo));
677 if (acpi_cmbat_info_expired(&acpi_cmbat_info_lastupdated)) {
678 error = acpi_cmbat_get_total_battinfo(battinfo);
685 if (unit >= acpi_cmbat_units) {
690 if ((dev = devclass_get_device(acpi_cmbat_devclass, unit)) == NULL) {
695 if ((sc = device_get_softc(dev)) == NULL) {
703 battinfo->state = ACPI_BATT_STAT_NOT_PRESENT;
705 battinfo->cap = sc->cap;
706 battinfo->min = sc->min;
707 battinfo->state = sc->bst.state;