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 ACPI_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 "%s: PKG_GETINT error, idx = %d\n.", __func__, 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 "%s: PKG_GETSTR error, idx = %d\n.", __func__, 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 struct timespec bif_lastupdated;
108 struct timespec bst_lastupdated;
115 int full_charge_time;
118 static struct timespec acpi_cmbat_info_lastupdated;
120 /* XXX: devclass_get_maxunit() don't give us the current allocated units... */
121 static int acpi_cmbat_units = 0;
123 static int acpi_cmbat_info_expired(struct timespec *);
124 static void acpi_cmbat_info_updated(struct timespec *);
125 static void acpi_cmbat_get_bst(void *);
126 static void acpi_cmbat_get_bif(void *);
127 static void acpi_cmbat_notify_handler(ACPI_HANDLE, UINT32, void *);
128 static int acpi_cmbat_probe(device_t);
129 static int acpi_cmbat_attach(device_t);
130 static int acpi_cmbat_resume(device_t);
131 static int acpi_cmbat_ioctl(u_long, caddr_t, void *);
132 static int acpi_cmbat_get_total_battinfo(struct acpi_battinfo *);
135 acpi_cmbat_info_expired(struct timespec *lastupdated)
137 struct timespec curtime;
139 if (lastupdated == NULL) {
143 if (!timespecisset(lastupdated)) {
147 getnanotime(&curtime);
148 timespecsub(&curtime, lastupdated);
149 return ((curtime.tv_sec < 0 || curtime.tv_sec > acpi_battery_get_info_expire()));
154 acpi_cmbat_info_updated(struct timespec *lastupdated)
157 if (lastupdated != NULL) {
158 getnanotime(lastupdated);
163 acpi_cmbat_get_bst(void *context)
166 struct acpi_cmbat_softc *sc;
168 ACPI_OBJECT *res, *tmp;
170 ACPI_BUFFER bst_buffer;
173 sc = device_get_softc(dev);
174 h = acpi_get_handle(dev);
175 bst_buffer.Pointer = NULL;
177 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) {
181 if (sc->bst_updating) {
184 sc->bst_updating = 1;
186 bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
187 if (ACPI_FAILURE(as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer))) {
188 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
189 "error fetching current battery status -- %s\n",
190 AcpiFormatException(as));
194 res = (ACPI_OBJECT *)bst_buffer.Pointer;
196 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 4)) {
197 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
198 "battery status corrupted\n");
202 PKG_GETINT(res, tmp, 0, sc->bst.state, end);
203 PKG_GETINT(res, tmp, 1, sc->bst.rate, end);
204 PKG_GETINT(res, tmp, 2, sc->bst.cap, end);
205 PKG_GETINT(res, tmp, 3, sc->bst.volt, end);
206 acpi_cmbat_info_updated(&sc->bst_lastupdated);
208 if (bst_buffer.Pointer != NULL)
209 AcpiOsFree(bst_buffer.Pointer);
210 sc->bst_updating = 0;
214 acpi_cmbat_get_bif(void *context)
217 struct acpi_cmbat_softc *sc;
219 ACPI_OBJECT *res, *tmp;
221 ACPI_BUFFER bif_buffer;
224 sc = device_get_softc(dev);
225 h = acpi_get_handle(dev);
226 bif_buffer.Pointer = NULL;
228 if (!acpi_cmbat_info_expired(&sc->bif_lastupdated)) {
232 if (sc->bif_updating) {
235 sc->bif_updating = 1;
237 bif_buffer.Length = ACPI_ALLOCATE_BUFFER;
238 if (ACPI_FAILURE(as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer))) {
239 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
240 "error fetching current battery info -- %s\n",
241 AcpiFormatException(as));
245 res = (ACPI_OBJECT *)bif_buffer.Pointer;
247 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 13)) {
248 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
249 "battery info corrupted\n");
253 PKG_GETINT(res, tmp, 0, sc->bif.unit, end);
254 PKG_GETINT(res, tmp, 1, sc->bif.dcap, end);
255 PKG_GETINT(res, tmp, 2, sc->bif.lfcap, end);
256 PKG_GETINT(res, tmp, 3, sc->bif.btech, end);
257 PKG_GETINT(res, tmp, 4, sc->bif.dvol, end);
258 PKG_GETINT(res, tmp, 5, sc->bif.wcap, end);
259 PKG_GETINT(res, tmp, 6, sc->bif.lcap, end);
260 PKG_GETINT(res, tmp, 7, sc->bif.gra1, end);
261 PKG_GETINT(res, tmp, 8, sc->bif.gra2, end);
262 PKG_GETSTR(res, tmp, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN, end);
263 PKG_GETSTR(res, tmp, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN, end);
264 PKG_GETSTR(res, tmp, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN, end);
265 PKG_GETSTR(res, tmp, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN, end);
266 acpi_cmbat_info_updated(&sc->bif_lastupdated);
268 if (bif_buffer.Pointer != NULL)
269 AcpiOsFree(bif_buffer.Pointer);
270 sc->bif_updating = 0;
274 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
277 struct acpi_cmbat_softc *sc;
279 dev = (device_t)context;
280 if ((sc = device_get_softc(dev)) == NULL) {
285 case ACPI_BATTERY_BST_CHANGE:
286 timespecclear(&sc->bst_lastupdated);
288 case ACPI_BATTERY_BIF_CHANGE:
289 timespecclear(&sc->bif_lastupdated);
290 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
298 acpi_cmbat_probe(device_t dev)
301 if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
302 !acpi_disabled("cmbat") &&
303 acpi_MatchHid(dev, "PNP0C0A")) {
305 * Set device description.
307 device_set_desc(dev, "Control method Battery");
314 acpi_cmbat_attach(device_t dev)
318 struct acpi_cmbat_softc *sc;
320 if ((sc = device_get_softc(dev)) == NULL) {
324 handle = acpi_get_handle(dev);
326 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
327 acpi_cmbat_notify_handler, dev);
329 sc->bif_updating = sc->bst_updating = 0;
332 timespecclear(&sc->bif_lastupdated);
333 timespecclear(&sc->bst_lastupdated);
335 if (acpi_cmbat_units == 0) {
336 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BIF,
337 acpi_cmbat_ioctl, NULL)) != 0) {
340 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BST,
341 acpi_cmbat_ioctl, NULL)) != 0) {
346 if ((error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT,
347 acpi_cmbat_units)) != 0) {
352 timespecclear(&acpi_cmbat_info_lastupdated);
354 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
359 acpi_cmbat_resume(device_t dev)
362 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
366 static device_method_t acpi_cmbat_methods[] = {
367 /* Device interface */
368 DEVMETHOD(device_probe, acpi_cmbat_probe),
369 DEVMETHOD(device_attach, acpi_cmbat_attach),
370 DEVMETHOD(device_resume, acpi_cmbat_resume),
375 static driver_t acpi_cmbat_driver = {
378 sizeof(struct acpi_cmbat_softc),
381 static devclass_t acpi_cmbat_devclass;
382 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);
385 acpi_cmbat_ioctl(u_long cmd, caddr_t addr, void *arg)
388 union acpi_battery_ioctl_arg *ioctl_arg;
389 struct acpi_cmbat_softc *sc;
390 struct acpi_bif *bifp;
391 struct acpi_bst *bstp;
393 ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
394 if ((dev = devclass_get_device(acpi_cmbat_devclass,
395 ioctl_arg->unit)) == NULL) {
399 if ((sc = device_get_softc(dev)) == NULL) {
404 case ACPIIO_CMBAT_GET_BIF:
405 acpi_cmbat_get_bif(dev);
406 bifp = &ioctl_arg->bif;
407 bifp->unit = sc->bif.unit;
408 bifp->dcap = sc->bif.dcap;
409 bifp->lfcap = sc->bif.lfcap;
410 bifp->btech = sc->bif.btech;
411 bifp->dvol = sc->bif.dvol;
412 bifp->wcap = sc->bif.wcap;
413 bifp->lcap = sc->bif.lcap;
414 bifp->gra1 = sc->bif.gra1;
415 bifp->gra2 = sc->bif.gra2;
416 strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
417 strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
418 strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
419 strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
422 case ACPIIO_CMBAT_GET_BST:
423 acpi_cmbat_get_bst(dev);
424 bstp = &ioctl_arg->bst;
425 bstp->state = sc->bst.state;
426 bstp->rate = sc->bst.rate;
427 bstp->cap = sc->bst.cap;
428 bstp->volt = sc->bst.volt;
436 acpi_cmbat_get_total_battinfo(struct acpi_battinfo *battinfo)
441 int valid_rate, valid_units;
443 int total_cap, total_min, total_full;
445 struct acpi_cmbat_softc *sc;
446 static int bat_units = 0;
447 static struct acpi_cmbat_softc **bat = NULL;
450 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
453 /* Allocate array of softc pointers */
454 if (bat_units != acpi_cmbat_units) {
456 free(bat, M_ACPICMBAT);
462 bat_units = acpi_cmbat_units;
463 bat = malloc(sizeof(struct acpi_cmbat_softc *) * bat_units,
464 M_ACPICMBAT, M_NOWAIT);
470 /* Collect softc pointers */
471 for (i = 0; i < acpi_cmbat_units; i++) {
472 if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) {
477 if ((sc = device_get_softc(dev)) == NULL) {
486 /* Get battery status, valid rate and valid units */
487 batt_stat = valid_rate = valid_units = 0;
488 for (i = 0; i < acpi_cmbat_units; i++) {
489 bat[i]->not_present = 0;
490 acpi_cmbat_get_bst(bat[i]->dev);
492 /* If battey not installed, we get strange values */
493 if (bat[i]->bst.state >= ACPI_BATT_STAT_MAX ||
494 bat[i]->bst.cap == 0xffffffff ||
495 bat[i]->bst.volt == 0xffffffff ||
496 bat[i]->bif.lfcap == 0) {
497 bat[i]->not_present = 1;
503 bat[i]->cap = 100 * bat[i]->bst.cap / bat[i]->bif.lfcap;
505 batt_stat |= bat[i]->bst.state;
507 if (bat[i]->bst.rate > 0) {
509 * XXX Hack to calculate total battery time.
510 * Systems with 2 or more battries, they may get used
511 * one by one, thus bst.rate is set only to the one
512 * in use. For remaining batteries bst.rate = 0, which
513 * makes it impossible to calculate remaining time.
514 * Some other systems may need sum of bst.rate in
515 * dis-charging state.
516 * There for we sum up the bst.rate that is valid
517 * (in dis-charging state), and use the sum to
518 * calcutate remaining batteries' time.
520 if (bat[i]->bst.state & ACPI_BATT_STAT_DISCHARG) {
521 valid_rate += bat[i]->bst.rate;
526 /* Calculate total battery capacity and time */
527 total_cap = total_min = total_full = 0;
528 for (i = 0; i < acpi_cmbat_units; i++) {
529 if (bat[i]->not_present) {
533 if (valid_rate > 0) {
534 /* Use the sum of bst.rate */
535 bat[i]->min = 60 * bat[i]->bst.cap / valid_rate;
536 } else if (bat[i]->full_charge_time > 0) {
537 bat[i]->min = (bat[i]->full_charge_time * bat[i]->cap) / 100;
539 /* Couldn't find valid rate and full battery time */
542 total_min += bat[i]->min;
543 total_cap += bat[i]->cap;
544 total_full += bat[i]->full_charge_time;
548 if (valid_units == 0) {
550 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
552 cap = total_cap / valid_units;
556 if (valid_units == 0) {
558 } else if (valid_rate == 0 || (batt_stat & ACPI_BATT_STAT_CHARGING)) {
559 if (total_full == 0) {
562 min = (total_full * cap) / 100;
568 acpi_cmbat_info_updated(&acpi_cmbat_info_lastupdated);
572 battinfo->state = batt_stat;
582 acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo)
586 struct acpi_cmbat_softc *sc;
589 return (acpi_cmbat_get_total_battinfo(battinfo));
592 if (acpi_cmbat_info_expired(&acpi_cmbat_info_lastupdated)) {
593 error = acpi_cmbat_get_total_battinfo(battinfo);
600 if (unit >= acpi_cmbat_units) {
605 if ((dev = devclass_get_device(acpi_cmbat_devclass, unit)) == NULL) {
610 if ((sc = device_get_softc(dev)) == NULL) {
615 if (sc->not_present) {
618 battinfo->state = ACPI_BATT_STAT_NOT_PRESENT;
620 battinfo->cap = sc->cap;
621 battinfo->min = sc->min;
622 battinfo->state = sc->bst.state;