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.29 2004/05/30 20:08:23 phk Exp $
29 * $DragonFly: src/sys/dev/acpica5/acpi_cmbat.c,v 1.10 2007/01/17 17:31:19 y0netan1 Exp $
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/ioccom.h>
39 #include <sys/malloc.h>
42 #include <dev/acpica5/acpivar.h>
43 #include <dev/acpica5/acpiio.h>
45 MALLOC_DEFINE(M_ACPICMBAT, "acpicmbat", "ACPI control method battery data");
47 /* Number of times to retry initialization before giving up. */
48 #define ACPI_CMBAT_RETRY_MAX 6
50 /* Check the battery once a minute. */
51 #define CMBAT_POLLRATE (60 * hz)
53 /* Hooks for the ACPI CA debugging infrastructure */
54 #define _COMPONENT ACPI_BATTERY
55 ACPI_MODULE_NAME("BATTERY")
57 #define ACPI_BATTERY_BST_CHANGE 0x80
58 #define ACPI_BATTERY_BIF_CHANGE 0x81
60 struct acpi_cmbat_softc {
65 struct timespec bif_lastupdated;
66 struct timespec bst_lastupdated;
77 static struct timespec acpi_cmbat_info_lastupdated;
79 /* XXX: devclass_get_maxunit() don't give us the current allocated units. */
80 static int acpi_cmbat_units = 0;
82 static int acpi_cmbat_info_expired(struct timespec *);
83 static void acpi_cmbat_info_updated(struct timespec *);
84 static void acpi_cmbat_get_bst(void *);
85 static void acpi_cmbat_get_bif(void *);
86 static void acpi_cmbat_notify_handler(ACPI_HANDLE, UINT32, void *);
87 static int acpi_cmbat_probe(device_t);
88 static int acpi_cmbat_attach(device_t);
89 static int acpi_cmbat_resume(device_t);
90 static int acpi_cmbat_ioctl(u_long, caddr_t, void *);
91 static int acpi_cmbat_is_bst_valid(struct acpi_bst*);
92 static int acpi_cmbat_is_bif_valid(struct acpi_bif*);
93 static int acpi_cmbat_get_total_battinfo(struct acpi_battinfo *);
94 static void acpi_cmbat_init_battery(void *);
96 static device_method_t acpi_cmbat_methods[] = {
97 /* Device interface */
98 DEVMETHOD(device_probe, acpi_cmbat_probe),
99 DEVMETHOD(device_attach, acpi_cmbat_attach),
100 DEVMETHOD(device_resume, acpi_cmbat_resume),
105 static driver_t acpi_cmbat_driver = {
108 sizeof(struct acpi_cmbat_softc),
111 static devclass_t acpi_cmbat_devclass;
112 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);
113 MODULE_DEPEND(acpi_cmbat, acpi, 1, 1, 1);
116 acpi_cmbat_info_expired(struct timespec *lastupdated)
118 struct timespec curtime;
120 if (lastupdated == NULL)
122 if (!timespecisset(lastupdated))
125 getnanotime(&curtime);
126 timespecsub(&curtime, lastupdated);
127 return (curtime.tv_sec < 0 ||
128 curtime.tv_sec > acpi_battery_get_info_expire());
133 acpi_cmbat_info_updated(struct timespec *lastupdated)
135 if (lastupdated != NULL)
136 getnanotime(lastupdated);
140 acpi_cmbat_get_bst(void *context)
143 struct acpi_cmbat_softc *sc;
147 ACPI_BUFFER bst_buffer;
150 sc = device_get_softc(dev);
151 h = acpi_get_handle(dev);
153 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated))
155 if (sc->bst_updating)
157 sc->bst_updating = 1;
159 bst_buffer.Pointer = NULL;
160 bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
161 as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer);
162 if (ACPI_FAILURE(as)) {
163 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
164 "error fetching current battery status -- %s\n",
165 AcpiFormatException(as));
169 res = (ACPI_OBJECT *)bst_buffer.Pointer;
170 if (!ACPI_PKG_VALID(res, 4)) {
171 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
172 "battery status corrupted\n");
176 if (acpi_PkgInt32(res, 0, &sc->bst.state) != 0)
178 if (acpi_PkgInt32(res, 1, &sc->bst.rate) != 0)
180 if (acpi_PkgInt32(res, 2, &sc->bst.cap) != 0)
182 if (acpi_PkgInt32(res, 3, &sc->bst.volt) != 0)
184 acpi_cmbat_info_updated(&sc->bst_lastupdated);
187 if (bst_buffer.Pointer != NULL)
188 AcpiOsFree(bst_buffer.Pointer);
189 sc->bst_updating = 0;
193 acpi_cmbat_get_bif(void *context)
196 struct acpi_cmbat_softc *sc;
200 ACPI_BUFFER bif_buffer;
203 sc = device_get_softc(dev);
204 h = acpi_get_handle(dev);
206 if (!acpi_cmbat_info_expired(&sc->bif_lastupdated))
208 if (sc->bif_updating)
210 sc->bif_updating = 1;
212 bif_buffer.Pointer = NULL;
213 bif_buffer.Length = ACPI_ALLOCATE_BUFFER;
214 as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer);
215 if (ACPI_FAILURE(as)) {
216 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
217 "error fetching current battery info -- %s\n",
218 AcpiFormatException(as));
222 res = (ACPI_OBJECT *)bif_buffer.Pointer;
223 if (!ACPI_PKG_VALID(res, 13)) {
224 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
225 "battery info corrupted\n");
229 if (acpi_PkgInt32(res, 0, &sc->bif.units) != 0)
231 if (acpi_PkgInt32(res, 1, &sc->bif.dcap) != 0)
233 if (acpi_PkgInt32(res, 2, &sc->bif.lfcap) != 0)
235 if (acpi_PkgInt32(res, 3, &sc->bif.btech) != 0)
237 if (acpi_PkgInt32(res, 4, &sc->bif.dvol) != 0)
239 if (acpi_PkgInt32(res, 5, &sc->bif.wcap) != 0)
241 if (acpi_PkgInt32(res, 6, &sc->bif.lcap) != 0)
243 if (acpi_PkgInt32(res, 7, &sc->bif.gra1) != 0)
245 if (acpi_PkgInt32(res, 8, &sc->bif.gra2) != 0)
247 if (acpi_PkgStr(res, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN) != 0)
249 if (acpi_PkgStr(res, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN) != 0)
251 if (acpi_PkgStr(res, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN) != 0)
253 if (acpi_PkgStr(res, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN) != 0)
255 acpi_cmbat_info_updated(&sc->bif_lastupdated);
258 if (bif_buffer.Pointer != NULL)
259 AcpiOsFree(bif_buffer.Pointer);
260 sc->bif_updating = 0;
264 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
267 struct acpi_cmbat_softc *sc;
269 dev = (device_t)context;
270 if ((sc = device_get_softc(dev)) == NULL)
273 acpi_UserNotify("CMBAT", h, notify);
276 case ACPI_NOTIFY_DEVICE_CHECK:
277 case ACPI_BATTERY_BST_CHANGE:
278 timespecclear(&sc->bst_lastupdated);
280 case ACPI_NOTIFY_BUS_CHECK:
281 case ACPI_BATTERY_BIF_CHANGE:
282 timespecclear(&sc->bif_lastupdated);
283 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_get_bif, dev);
291 acpi_cmbat_probe(device_t dev)
293 if (acpi_get_type(dev) == ACPI_TYPE_DEVICE &&
294 !acpi_disabled("cmbat") && acpi_MatchHid(dev, "PNP0C0A")) {
296 device_set_desc(dev, "Control Method Battery");
303 acpi_cmbat_attach(device_t dev)
307 struct acpi_cmbat_softc *sc;
309 if ((sc = device_get_softc(dev)) == NULL)
312 handle = acpi_get_handle(dev);
315 * Install a system notify handler in addition to the device notify.
316 * Toshiba notebook uses this alternate notify for its battery.
318 AcpiInstallNotifyHandler(handle, ACPI_SYSTEM_NOTIFY,
319 acpi_cmbat_notify_handler, dev);
320 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
321 acpi_cmbat_notify_handler, dev);
323 sc->bif_updating = sc->bst_updating = 0;
326 timespecclear(&sc->bif_lastupdated);
327 timespecclear(&sc->bst_lastupdated);
329 if (acpi_cmbat_units == 0) {
330 error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BIF,
331 acpi_cmbat_ioctl, NULL);
334 error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BST,
335 acpi_cmbat_ioctl, NULL);
340 error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT, acpi_cmbat_units);
345 timespecclear(&acpi_cmbat_info_lastupdated);
346 sc->initializing = 0;
347 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
353 acpi_cmbat_resume(device_t dev)
355 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
360 acpi_cmbat_ioctl(u_long cmd, caddr_t addr, void *arg)
363 union acpi_battery_ioctl_arg *ioctl_arg;
364 struct acpi_cmbat_softc *sc;
365 struct acpi_bif *bifp;
366 struct acpi_bst *bstp;
368 ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
369 dev = devclass_get_device(acpi_cmbat_devclass, ioctl_arg->unit);
372 sc = device_get_softc(dev);
377 * No security check required: information retrieval only. If
378 * new functions are added here, a check might be required.
381 case ACPIIO_CMBAT_GET_BIF:
382 acpi_cmbat_get_bif(dev);
383 bifp = &ioctl_arg->bif;
384 bifp->units = sc->bif.units;
385 bifp->dcap = sc->bif.dcap;
386 bifp->lfcap = sc->bif.lfcap;
387 bifp->btech = sc->bif.btech;
388 bifp->dvol = sc->bif.dvol;
389 bifp->wcap = sc->bif.wcap;
390 bifp->lcap = sc->bif.lcap;
391 bifp->gra1 = sc->bif.gra1;
392 bifp->gra2 = sc->bif.gra2;
393 strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
394 strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
395 strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
396 strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
398 case ACPIIO_CMBAT_GET_BST:
399 bstp = &ioctl_arg->bst;
400 if (acpi_BatteryIsPresent(dev)) {
401 acpi_cmbat_get_bst(dev);
402 bstp->state = sc->bst.state;
403 bstp->rate = sc->bst.rate;
404 bstp->cap = sc->bst.cap;
405 bstp->volt = sc->bst.volt;
407 bstp->state = ACPI_BATT_STAT_NOT_PRESENT;
418 acpi_cmbat_is_bst_valid(struct acpi_bst *bst)
420 if (bst->state >= ACPI_BATT_STAT_MAX || bst->cap == 0xffffffff ||
421 bst->volt == 0xffffffff)
429 acpi_cmbat_is_bif_valid(struct acpi_bif *bif)
438 acpi_cmbat_get_total_battinfo(struct acpi_battinfo *battinfo)
443 int valid_rate, valid_units;
445 int total_cap, total_min, total_full;
447 struct acpi_cmbat_softc *sc;
448 static int bat_units = 0;
449 static struct acpi_cmbat_softc **bat = NULL;
452 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
455 /* Allocate array of softc pointers */
456 if (bat_units != acpi_cmbat_units) {
458 kfree(bat, M_ACPICMBAT);
464 bat_units = acpi_cmbat_units;
465 bat = kmalloc(sizeof(struct acpi_cmbat_softc *) * bat_units,
466 M_ACPICMBAT, M_INTWAIT);
468 /* Collect softc pointers */
469 for (i = 0; i < acpi_cmbat_units; i++) {
470 if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) {
474 if ((sc = device_get_softc(dev)) == NULL) {
482 /* Get battery status, valid rate and valid units */
483 batt_stat = valid_rate = valid_units = 0;
484 for (i = 0; i < acpi_cmbat_units; i++) {
485 bat[i]->present = acpi_BatteryIsPresent(bat[i]->dev);
486 if (!bat[i]->present)
489 acpi_cmbat_get_bst(bat[i]->dev);
491 /* If battery not installed, we get strange values */
492 if (!acpi_cmbat_is_bst_valid(&(bat[i]->bst)) ||
493 !acpi_cmbat_is_bif_valid(&(bat[i]->bif))) {
500 bat[i]->cap = 100 * bat[i]->bst.cap / bat[i]->bif.lfcap;
501 batt_stat |= bat[i]->bst.state;
503 if (bat[i]->bst.rate > 0) {
505 * XXX Hack to calculate total battery time.
506 * Systems with 2 or more battries, they may get used
507 * one by one, thus bst.rate is set only to the one
508 * in use. For remaining batteries bst.rate = 0, which
509 * makes it impossible to calculate remaining time.
510 * Some other systems may need sum of bst.rate in
511 * dis-charging state.
512 * There for we sum up the bst.rate that is valid
513 * (in dis-charging state), and use the sum to
514 * calcutate remaining batteries' time.
516 if (bat[i]->bst.state & ACPI_BATT_STAT_DISCHARG)
517 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]->present)
527 if (valid_rate > 0) {
528 /* Use the sum of bst.rate */
529 bat[i]->min = 60 * bat[i]->bst.cap / valid_rate;
530 } else if (bat[i]->full_charge_time > 0) {
531 bat[i]->min = (bat[i]->full_charge_time * bat[i]->cap) / 100;
533 /* Couldn't find valid rate and full battery time */
536 total_min += bat[i]->min;
537 total_cap += bat[i]->cap;
538 total_full += bat[i]->full_charge_time;
542 if (valid_units == 0) {
544 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
546 cap = total_cap / valid_units;
550 if (valid_units == 0) {
552 } else if (valid_rate == 0 || (batt_stat & ACPI_BATT_STAT_CHARGING)) {
556 min = (total_full * cap) / 100;
560 acpi_cmbat_info_updated(&acpi_cmbat_info_lastupdated);
565 battinfo->state = batt_stat;
571 acpi_cmbat_init_battery(void *arg)
574 device_t dev = (device_t)arg;
575 struct acpi_cmbat_softc *sc = device_get_softc(dev);
577 if (sc->initializing)
580 sc->initializing = 1;
581 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
582 "battery initialization start\n");
584 for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10)) {
585 sc->present = acpi_BatteryIsPresent(dev);
589 timespecclear(&sc->bst_lastupdated);
590 timespecclear(&sc->bif_lastupdated);
592 acpi_cmbat_get_bst(dev);
593 if (!acpi_cmbat_is_bst_valid(&sc->bst))
596 acpi_cmbat_get_bif(dev);
597 if (!acpi_cmbat_is_bif_valid(&sc->bif))
602 sc->initializing = 0;
603 if (retry == ACPI_CMBAT_RETRY_MAX) {
604 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
605 "battery initialization failed, giving up\n");
607 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
608 "battery initialization done, tried %d times\n", retry + 1);
616 acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo)
620 struct acpi_cmbat_softc *sc;
623 return (acpi_cmbat_get_total_battinfo(battinfo));
625 if (acpi_cmbat_info_expired(&acpi_cmbat_info_lastupdated)) {
626 error = acpi_cmbat_get_total_battinfo(battinfo);
632 if (unit >= acpi_cmbat_units) {
637 if ((dev = devclass_get_device(acpi_cmbat_devclass, unit)) == NULL) {
641 if ((sc = device_get_softc(dev)) == NULL) {
649 battinfo->state = ACPI_BATT_STAT_NOT_PRESENT;
651 battinfo->cap = sc->cap;
652 battinfo->min = sc->min;
653 battinfo->state = sc->bst.state;