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>
49 * Hooks for the ACPI CA debugging infrastructure
51 #define _COMPONENT ACPI_BATTERY
52 MODULE_NAME("BATTERY")
54 static void acpi_cmbat_get_bst(void *);
55 static void acpi_cmbat_get_bif(void *);
56 static void acpi_cmbat_notify_handler(ACPI_HANDLE, UINT32, void *);
57 static int acpi_cmbat_probe(device_t);
58 static int acpi_cmbat_attach(device_t);
59 static int acpi_cmbat_ioctl(u_long, caddr_t, void *);
61 struct acpi_cmbat_softc {
65 ACPI_BUFFER bif_buffer;
66 ACPI_BUFFER bst_buffer;
67 struct timespec bif_lastupdated;
68 struct timespec bst_lastupdated;
76 static struct timespec acpi_cmbat_info_lastupdated;
78 /* XXX: devclass_get_maxunit() don't give us the current allocated units... */
79 static int acpi_cmbat_units = 0;
81 #define ACPI_BATTERY_BST_CHANGE 0x80
82 #define ACPI_BATTERY_BIF_CHANGE 0x81
84 #define PKG_GETINT(res, tmp, idx, dest, label) do { \
85 tmp = &res->Package.Elements[idx]; \
87 device_printf(dev, "%s: PKG_GETINT idx = %d\n.", \
91 if (tmp->Type != ACPI_TYPE_INTEGER) \
93 dest = tmp->Integer.Value; \
96 #define PKG_GETSTR(res, tmp, idx, dest, size, label) do { \
99 tmp = &res->Package.Elements[idx]; \
101 device_printf(dev, "%s: PKG_GETSTR idx = %d\n.", \
105 bzero(dest, sizeof(dest)); \
106 switch (tmp->Type) { \
107 case ACPI_TYPE_STRING: \
108 if (tmp->String.Length < length) { \
109 length = tmp->String.Length; \
111 strncpy(dest, tmp->String.Pointer, length); \
113 case ACPI_TYPE_BUFFER: \
114 if (tmp->Buffer.Length < length) { \
115 length = tmp->Buffer.Length; \
117 strncpy(dest, tmp->Buffer.Pointer, length); \
122 dest[sizeof(dest)-1] = '\0'; \
125 #define BATTERY_INFO_EXPIRE 5
127 acpi_cmbat_info_expired(struct timespec *lastupdated)
129 struct timespec curtime;
131 if (lastupdated == NULL) {
135 if (!timespecisset(lastupdated)) {
139 getnanotime(&curtime);
140 timespecsub(&curtime, lastupdated);
141 return ((curtime.tv_sec < 0 || curtime.tv_sec > BATTERY_INFO_EXPIRE));
146 acpi_cmbat_info_updated(struct timespec *lastupdated)
149 if (lastupdated != NULL) {
150 getnanotime(lastupdated);
155 acpi_cmbat_get_bst(void *context)
157 device_t dev = context;
158 struct acpi_cmbat_softc *sc = device_get_softc(dev);
160 ACPI_OBJECT *res, *tmp;
161 ACPI_HANDLE h = acpi_get_handle(dev);
163 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) {
168 if (sc->bst_buffer.Length == 0) {
169 sc->bst_buffer.Pointer = NULL;
170 as = AcpiEvaluateObject(h, "_BST", NULL, &sc->bst_buffer);
171 if (as != AE_BUFFER_OVERFLOW){
172 device_printf(dev, "CANNOT FOUND _BST (%d)\n", as);
176 sc->bst_buffer.Pointer = malloc(sc->bst_buffer.Length, M_DEVBUF, M_NOWAIT);
177 if (sc->bst_buffer.Pointer == NULL) {
178 device_printf(dev,"malloc failed");
183 bzero(sc->bst_buffer.Pointer, sc->bst_buffer.Length);
184 as = AcpiEvaluateObject(h, "_BST", NULL, &sc->bst_buffer);
186 if (as == AE_BUFFER_OVERFLOW){
187 if (sc->bst_buffer.Pointer){
188 free(sc->bst_buffer.Pointer, M_DEVBUF);
190 device_printf(dev, "bst size changed to %d\n", sc->bst_buffer.Length);
191 sc->bst_buffer.Length = 0;
193 } else if (as != AE_OK){
194 device_printf(dev, "CANNOT FOUND _BST (%d)\n", as);
198 res = (ACPI_OBJECT *)sc->bst_buffer.Pointer;
200 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 4)) {
201 device_printf(dev, "Battery status corrupted\n");
205 PKG_GETINT(res, tmp, 0, sc->bst.state, end);
206 PKG_GETINT(res, tmp, 1, sc->bst.rate, end);
207 PKG_GETINT(res, tmp, 2, sc->bst.cap, end);
208 PKG_GETINT(res, tmp, 3, sc->bst.volt, end);
209 acpi_cmbat_info_updated(&sc->bst_lastupdated);
214 acpi_cmbat_get_bif(void *context)
216 device_t dev = context;
217 struct acpi_cmbat_softc *sc = device_get_softc(dev);
219 ACPI_HANDLE h = acpi_get_handle(dev);
220 ACPI_OBJECT *res, *tmp;
222 if (!acpi_cmbat_info_expired(&sc->bif_lastupdated)) {
227 if (sc->bif_buffer.Length == 0) {
228 sc->bif_buffer.Pointer = NULL;
229 as = AcpiEvaluateObject(h, "_BIF", NULL, &sc->bif_buffer);
230 if (as != AE_BUFFER_OVERFLOW){
231 device_printf(dev, "CANNOT FOUND _BIF (%d)\n", as);
235 sc->bif_buffer.Pointer = malloc(sc->bif_buffer.Length, M_DEVBUF, M_NOWAIT);
236 if (sc->bif_buffer.Pointer == NULL) {
237 device_printf(dev,"malloc failed");
242 bzero(sc->bif_buffer.Pointer, sc->bif_buffer.Length);
243 as = AcpiEvaluateObject(h, "_BIF", NULL, &sc->bif_buffer);
245 if (as == AE_BUFFER_OVERFLOW){
246 if (sc->bif_buffer.Pointer){
247 free(sc->bif_buffer.Pointer, M_DEVBUF);
249 device_printf(dev, "bif size changed to %d\n", sc->bif_buffer.Length);
250 sc->bif_buffer.Length = 0;
252 } else if (as != AE_OK){
253 device_printf(dev, "CANNOT FOUND _BIF (%d)\n", as);
257 res = (ACPI_OBJECT *)sc->bif_buffer.Pointer;
259 if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 13)) {
260 device_printf(dev, "Battery info corrupted\n");
264 PKG_GETINT(res, tmp, 0, sc->bif.unit, end);
265 PKG_GETINT(res, tmp, 1, sc->bif.dcap, end);
266 PKG_GETINT(res, tmp, 2, sc->bif.lfcap, end);
267 PKG_GETINT(res, tmp, 3, sc->bif.btech, end);
268 PKG_GETINT(res, tmp, 4, sc->bif.dvol, end);
269 PKG_GETINT(res, tmp, 5, sc->bif.wcap, end);
270 PKG_GETINT(res, tmp, 6, sc->bif.lcap, end);
271 PKG_GETINT(res, tmp, 7, sc->bif.gra1, end);
272 PKG_GETINT(res, tmp, 8, sc->bif.gra2, end);
273 PKG_GETSTR(res, tmp, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN, end);
274 PKG_GETSTR(res, tmp, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN, end);
275 PKG_GETSTR(res, tmp, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN, end);
276 PKG_GETSTR(res, tmp, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN, end);
277 acpi_cmbat_info_updated(&sc->bif_lastupdated);
282 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
285 struct acpi_cmbat_softc *sc;
287 dev = (device_t)context;
288 if ((sc = device_get_softc(dev)) == NULL) {
293 case ACPI_BATTERY_BST_CHANGE:
294 timespecclear(&sc->bst_lastupdated);
296 case ACPI_BATTERY_BIF_CHANGE:
297 timespecclear(&sc->bif_lastupdated);
305 acpi_cmbat_probe(device_t dev)
308 if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
309 acpi_MatchHid(dev, "PNP0C0A")) {
311 * Set device description
313 device_set_desc(dev, "Control method Battery");
320 acpi_cmbat_attach(device_t dev)
323 ACPI_HANDLE handle = acpi_get_handle(dev);
324 struct acpi_cmbat_softc *sc;
326 if ((sc = device_get_softc(dev)) == NULL) {
329 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
330 acpi_cmbat_notify_handler, dev);
331 bzero(&sc->bif_buffer, sizeof(sc->bif_buffer));
332 bzero(&sc->bst_buffer, sizeof(sc->bst_buffer));
335 timespecclear(&sc->bif_lastupdated);
336 timespecclear(&sc->bst_lastupdated);
338 if (acpi_cmbat_units == 0) {
339 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BIF,
340 acpi_cmbat_ioctl, NULL)) != 0) {
343 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BST,
344 acpi_cmbat_ioctl, NULL)) != 0) {
349 if ((error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT,
350 acpi_cmbat_units)) != 0) {
355 timespecclear(&acpi_cmbat_info_lastupdated);
361 static device_method_t acpi_cmbat_methods[] = {
362 /* Device interface */
363 DEVMETHOD(device_probe, acpi_cmbat_probe),
364 DEVMETHOD(device_attach, acpi_cmbat_attach),
369 static driver_t acpi_cmbat_driver = {
372 sizeof(struct acpi_cmbat_softc),
375 static devclass_t acpi_cmbat_devclass;
376 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);
379 acpi_cmbat_ioctl(u_long cmd, caddr_t addr, void *arg)
382 union acpi_battery_ioctl_arg *ioctl_arg;
383 struct acpi_cmbat_softc *sc;
384 struct acpi_bif *bifp;
385 struct acpi_bst *bstp;
387 ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
388 if ((dev = devclass_get_device(acpi_cmbat_devclass,
389 ioctl_arg->unit)) == NULL) {
393 if ((sc = device_get_softc(dev)) == NULL) {
398 case ACPIIO_CMBAT_GET_BIF:
399 acpi_cmbat_get_bif(dev);
400 bifp = &ioctl_arg->bif;
401 bifp->unit = sc->bif.unit;
402 bifp->dcap = sc->bif.dcap;
403 bifp->lfcap = sc->bif.lfcap;
404 bifp->btech = sc->bif.btech;
405 bifp->dvol = sc->bif.dvol;
406 bifp->wcap = sc->bif.wcap;
407 bifp->lcap = sc->bif.lcap;
408 bifp->gra1 = sc->bif.gra1;
409 bifp->gra2 = sc->bif.gra2;
410 strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
411 strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
412 strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
413 strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
416 case ACPIIO_CMBAT_GET_BST:
417 acpi_cmbat_get_bst(dev);
418 bstp = &ioctl_arg->bst;
419 bstp->state = sc->bst.state;
420 bstp->rate = sc->bst.rate;
421 bstp->cap = sc->bst.cap;
422 bstp->volt = sc->bst.volt;
430 acpi_cmbat_get_total_battinfo(struct acpi_battinfo *battinfo)
435 int valid_rate, valid_units;
437 int total_cap, total_min, total_full;
439 struct acpi_cmbat_softc *sc;
440 static int bat_units = 0;
441 static struct acpi_cmbat_softc **bat = NULL;
444 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
447 /* Allocate array of softc pointers */
448 if (bat_units != acpi_cmbat_units) {
455 bat_units = acpi_cmbat_units;
456 bat = malloc(sizeof(struct acpi_cmbat_softc *) * bat_units,
463 /* Collect softc pointers */
464 for (i = 0; i < acpi_cmbat_units; i++) {
465 if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) {
470 if ((sc = device_get_softc(dev)) == NULL) {
479 /* Get battery status, valid rate and valid units */
480 batt_stat = valid_rate = valid_units = 0;
481 for (i = 0; i < acpi_cmbat_units; i++) {
482 acpi_cmbat_get_bif(bat[i]->dev);
483 acpi_cmbat_get_bst(bat[i]->dev);
485 /* If battey not installed, we get strange values */
486 if (bat[i]->bst.state >= ACPI_BATT_STAT_MAX ||
487 bat[i]->bst.cap == 0xffffffff ||
488 bat[i]->bst.volt == 0xffffffff ||
489 bat[i]->bif.lfcap == 0) {
490 bat[i]->not_present = 1;
493 bat[i]->not_present = 0;
498 bat[i]->cap = 100 * bat[i]->bst.cap / bat[i]->bif.lfcap;
500 batt_stat |= bat[i]->bst.state;
502 if (bat[i]->bst.rate > 0) {
504 * XXX Hack to calculate total battery time.
505 * Systems with 2 or more battries, they may get used
506 * one by one, thus bst.rate is set only to the one
507 * in use. For remaining batteries bst.rate = 0, which
508 * makes it impossible to calculate remaining time.
509 * Some other systems may need sum of bst.rate in
510 * dis-charging state.
511 * There for we sum up the bst.rate that is valid
512 * (in dis-charging state), and use the sum to
513 * calcutate remaining batteries' time.
515 if (bat[i]->bst.state & ACPI_BATT_STAT_DISCHARG) {
516 valid_rate += bat[i]->bst.rate;
521 /* Calculate total battery capacity and time */
522 total_cap = total_min = total_full = 0;
523 for (i = 0; i < acpi_cmbat_units; i++) {
524 if (bat[i]->not_present) {
528 if (valid_rate > 0) {
529 /* Use the sum of bst.rate */
530 bat[i]->min = 60 * bat[i]->bst.cap / valid_rate;
531 } else if (bat[i]->full_charge_time > 0) {
532 bat[i]->min = (bat[i]->full_charge_time * bat[i]->cap) / 100;
534 /* Couldn't find valid rate and full battery time */
537 total_min += bat[i]->min;
538 total_cap += bat[i]->cap;
539 total_full += bat[i]->full_charge_time;
543 if (valid_units == 0) {
545 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
547 cap = total_cap / valid_units;
551 if (valid_units == 0) {
553 } else if (valid_rate == 0 || (batt_stat & ACPI_BATT_STAT_CHARGING)) {
554 if (total_full == 0) {
557 min = (total_full * cap) / 100;
563 acpi_cmbat_info_updated(&acpi_cmbat_info_lastupdated);
567 battinfo->state = batt_stat;
577 acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo)
581 struct acpi_cmbat_softc *sc;
584 return (acpi_cmbat_get_total_battinfo(battinfo));
587 if (acpi_cmbat_info_expired(&acpi_cmbat_info_lastupdated)) {
588 error = acpi_cmbat_get_total_battinfo(battinfo);
595 if (unit >= acpi_cmbat_units) {
600 if ((dev = devclass_get_device(acpi_cmbat_devclass, unit)) == NULL) {
605 if ((sc = device_get_softc(dev)) == NULL) {
610 if (sc->not_present) {
613 battinfo->state = ACPI_BATT_STAT_NOT_PRESENT;
615 battinfo->cap = sc->cap;
616 battinfo->min = sc->min;
617 battinfo->state = sc->bst.state;