2 * Copyright (c) 2005 Nate Lawson
3 * Copyright (c) 2000 Munehiro Matsuda
4 * Copyright (c) 2000 Takanori Watanabe
5 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/ioccom.h>
40 #include <machine/bus.h>
42 #include <sys/malloc.h>
44 #include <contrib/dev/acpica/include/acpi.h>
46 #include <dev/acpica/acpivar.h>
47 #include <dev/acpica/acpiio.h>
49 static MALLOC_DEFINE(M_ACPICMBAT, "acpicmbat",
50 "ACPI control method battery data");
52 /* Number of times to retry initialization before giving up. */
53 #define ACPI_CMBAT_RETRY_MAX 6
55 /* Check the battery once a minute. */
56 #define CMBAT_POLLRATE (60 * hz)
58 /* Hooks for the ACPI CA debugging infrastructure */
59 #define _COMPONENT ACPI_BATTERY
60 ACPI_MODULE_NAME("BATTERY")
62 #define ACPI_BATTERY_BST_CHANGE 0x80
63 #define ACPI_BATTERY_BIF_CHANGE 0x81
65 struct acpi_cmbat_softc {
71 struct timespec bst_lastupdated;
74 ACPI_SERIAL_DECL(cmbat, "ACPI cmbat");
76 static int acpi_cmbat_probe(device_t dev);
77 static int acpi_cmbat_attach(device_t dev);
78 static int acpi_cmbat_detach(device_t dev);
79 static int acpi_cmbat_resume(device_t dev);
80 static void acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify,
82 static int acpi_cmbat_info_expired(struct timespec *lastupdated);
83 static void acpi_cmbat_info_updated(struct timespec *lastupdated);
84 static void acpi_cmbat_get_bst(void *arg);
85 static void acpi_cmbat_get_bif_task(void *arg);
86 static void acpi_cmbat_get_bif(void *arg);
87 static int acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp);
88 static int acpi_cmbat_bif(device_t dev, struct acpi_bif *bifp);
89 static void acpi_cmbat_init_battery(void *arg);
91 static device_method_t acpi_cmbat_methods[] = {
92 /* Device interface */
93 DEVMETHOD(device_probe, acpi_cmbat_probe),
94 DEVMETHOD(device_attach, acpi_cmbat_attach),
95 DEVMETHOD(device_detach, acpi_cmbat_detach),
96 DEVMETHOD(device_resume, acpi_cmbat_resume),
98 /* ACPI battery interface */
99 DEVMETHOD(acpi_batt_get_info, acpi_cmbat_bif),
100 DEVMETHOD(acpi_batt_get_status, acpi_cmbat_bst),
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_probe(device_t dev)
118 static char *cmbat_ids[] = { "PNP0C0A", NULL };
121 if (acpi_disabled("cmbat"))
123 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, cmbat_ids, NULL);
125 device_set_desc(dev, "ACPI Control Method Battery");
130 acpi_cmbat_attach(device_t dev)
134 struct acpi_cmbat_softc *sc;
136 sc = device_get_softc(dev);
137 handle = acpi_get_handle(dev);
140 timespecclear(&sc->bst_lastupdated);
142 error = acpi_battery_register(dev);
144 device_printf(dev, "registering battery failed\n");
149 * Install a system notify handler in addition to the device notify.
150 * Toshiba notebook uses this alternate notify for its battery.
152 AcpiInstallNotifyHandler(handle, ACPI_ALL_NOTIFY,
153 acpi_cmbat_notify_handler, dev);
155 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
161 acpi_cmbat_detach(device_t dev)
165 handle = acpi_get_handle(dev);
166 AcpiRemoveNotifyHandler(handle, ACPI_ALL_NOTIFY, acpi_cmbat_notify_handler);
167 acpi_battery_remove(dev);
170 * Force any pending notification handler calls to complete by
171 * requesting cmbat serialisation while freeing and clearing the
174 ACPI_SERIAL_BEGIN(cmbat);
175 device_set_softc(dev, NULL);
176 ACPI_SERIAL_END(cmbat);
182 acpi_cmbat_resume(device_t dev)
185 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
190 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
192 struct acpi_cmbat_softc *sc;
195 dev = (device_t)context;
196 sc = device_get_softc(dev);
199 case ACPI_NOTIFY_DEVICE_CHECK:
200 case ACPI_BATTERY_BST_CHANGE:
202 * Clear the last updated time. The next call to retrieve the
203 * battery status will get the new value for us.
205 timespecclear(&sc->bst_lastupdated);
207 case ACPI_NOTIFY_BUS_CHECK:
208 case ACPI_BATTERY_BIF_CHANGE:
210 * Queue a callback to get the current battery info from thread
211 * context. It's not safe to block in a notify handler.
213 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_get_bif_task, dev);
217 acpi_UserNotify("CMBAT", h, notify);
221 acpi_cmbat_info_expired(struct timespec *lastupdated)
223 struct timespec curtime;
225 ACPI_SERIAL_ASSERT(cmbat);
227 if (lastupdated == NULL)
229 if (!timespecisset(lastupdated))
232 getnanotime(&curtime);
233 timespecsub(&curtime, lastupdated, &curtime);
234 return (curtime.tv_sec < 0 ||
235 curtime.tv_sec > acpi_battery_get_info_expire());
239 acpi_cmbat_info_updated(struct timespec *lastupdated)
242 ACPI_SERIAL_ASSERT(cmbat);
244 if (lastupdated != NULL)
245 getnanotime(lastupdated);
249 acpi_cmbat_get_bst(void *arg)
251 struct acpi_cmbat_softc *sc;
255 ACPI_BUFFER bst_buffer;
258 ACPI_SERIAL_ASSERT(cmbat);
261 sc = device_get_softc(dev);
262 h = acpi_get_handle(dev);
263 bst_buffer.Pointer = NULL;
264 bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
266 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated))
269 as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer);
270 if (ACPI_FAILURE(as)) {
271 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
272 "error fetching current battery status -- %s\n",
273 AcpiFormatException(as));
277 res = (ACPI_OBJECT *)bst_buffer.Pointer;
278 if (!ACPI_PKG_VALID(res, 4)) {
279 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
280 "battery status corrupted\n");
284 if (acpi_PkgInt32(res, 0, &sc->bst.state) != 0)
286 if (acpi_PkgInt32(res, 1, &sc->bst.rate) != 0)
288 if (acpi_PkgInt32(res, 2, &sc->bst.cap) != 0)
290 if (acpi_PkgInt32(res, 3, &sc->bst.volt) != 0)
292 acpi_cmbat_info_updated(&sc->bst_lastupdated);
294 /* Clear out undefined/extended bits that might be set by hardware. */
295 sc->bst.state &= ACPI_BATT_STAT_BST_MASK;
296 if ((sc->bst.state & ACPI_BATT_STAT_INVALID) == ACPI_BATT_STAT_INVALID)
297 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
298 "battery reports simultaneous charging and discharging\n");
300 /* XXX If all batteries are critical, perhaps we should suspend. */
301 if (sc->bst.state & ACPI_BATT_STAT_CRITICAL) {
302 if ((sc->flags & ACPI_BATT_STAT_CRITICAL) == 0) {
303 sc->flags |= ACPI_BATT_STAT_CRITICAL;
304 device_printf(dev, "critically low charge!\n");
307 sc->flags &= ~ACPI_BATT_STAT_CRITICAL;
310 if (bst_buffer.Pointer != NULL)
311 AcpiOsFree(bst_buffer.Pointer);
314 /* XXX There should be a cleaner way to do this locking. */
316 acpi_cmbat_get_bif_task(void *arg)
319 ACPI_SERIAL_BEGIN(cmbat);
320 acpi_cmbat_get_bif(arg);
321 ACPI_SERIAL_END(cmbat);
325 acpi_cmbat_get_bif(void *arg)
327 struct acpi_cmbat_softc *sc;
331 ACPI_BUFFER bif_buffer;
334 ACPI_SERIAL_ASSERT(cmbat);
337 sc = device_get_softc(dev);
338 h = acpi_get_handle(dev);
339 bif_buffer.Pointer = NULL;
340 bif_buffer.Length = ACPI_ALLOCATE_BUFFER;
342 as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer);
343 if (ACPI_FAILURE(as)) {
344 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
345 "error fetching current battery info -- %s\n",
346 AcpiFormatException(as));
350 res = (ACPI_OBJECT *)bif_buffer.Pointer;
351 if (!ACPI_PKG_VALID(res, 13)) {
352 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
353 "battery info corrupted\n");
357 if (acpi_PkgInt32(res, 0, &sc->bif.units) != 0)
359 if (acpi_PkgInt32(res, 1, &sc->bif.dcap) != 0)
361 if (acpi_PkgInt32(res, 2, &sc->bif.lfcap) != 0)
363 if (acpi_PkgInt32(res, 3, &sc->bif.btech) != 0)
365 if (acpi_PkgInt32(res, 4, &sc->bif.dvol) != 0)
367 if (acpi_PkgInt32(res, 5, &sc->bif.wcap) != 0)
369 if (acpi_PkgInt32(res, 6, &sc->bif.lcap) != 0)
371 if (acpi_PkgInt32(res, 7, &sc->bif.gra1) != 0)
373 if (acpi_PkgInt32(res, 8, &sc->bif.gra2) != 0)
375 if (acpi_PkgStr(res, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN) != 0)
377 if (acpi_PkgStr(res, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN) != 0)
379 if (acpi_PkgStr(res, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN) != 0)
381 if (acpi_PkgStr(res, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN) != 0)
385 if (bif_buffer.Pointer != NULL)
386 AcpiOsFree(bif_buffer.Pointer);
390 acpi_cmbat_bif(device_t dev, struct acpi_bif *bifp)
392 struct acpi_cmbat_softc *sc;
394 sc = device_get_softc(dev);
397 * Just copy the data. The only value that should change is the
398 * last-full capacity, so we only update when we get a notify that says
399 * the info has changed. Many systems apparently take a long time to
400 * process a _BIF call so we avoid it if possible.
402 ACPI_SERIAL_BEGIN(cmbat);
403 bifp->units = sc->bif.units;
404 bifp->dcap = sc->bif.dcap;
405 bifp->lfcap = sc->bif.lfcap;
406 bifp->btech = sc->bif.btech;
407 bifp->dvol = sc->bif.dvol;
408 bifp->wcap = sc->bif.wcap;
409 bifp->lcap = sc->bif.lcap;
410 bifp->gra1 = sc->bif.gra1;
411 bifp->gra2 = sc->bif.gra2;
412 strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
413 strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
414 strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
415 strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
416 ACPI_SERIAL_END(cmbat);
422 acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp)
424 struct acpi_cmbat_softc *sc;
426 sc = device_get_softc(dev);
428 ACPI_SERIAL_BEGIN(cmbat);
429 if (acpi_BatteryIsPresent(dev)) {
430 acpi_cmbat_get_bst(dev);
431 bstp->state = sc->bst.state;
432 bstp->rate = sc->bst.rate;
433 bstp->cap = sc->bst.cap;
434 bstp->volt = sc->bst.volt;
436 bstp->state = ACPI_BATT_STAT_NOT_PRESENT;
437 ACPI_SERIAL_END(cmbat);
443 acpi_cmbat_init_battery(void *arg)
445 struct acpi_cmbat_softc *sc;
450 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
451 "battery initialization start\n");
454 * Try repeatedly to get valid data from the battery. Since the
455 * embedded controller isn't always ready just after boot, we may have
458 for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10000)) {
460 * Batteries on DOCK can be ejected w/ DOCK during retrying.
462 * If there is a valid softc pointer the device may be in
463 * attaching, attached or detaching state. If the state is
464 * different from attached retry getting the device state
465 * until it becomes stable. This solves a race if the ACPI
466 * notification handler is called during attach, because
467 * device_is_attached() doesn't return non-zero until after
468 * the attach code has been executed.
470 ACPI_SERIAL_BEGIN(cmbat);
471 sc = device_get_softc(dev);
473 ACPI_SERIAL_END(cmbat);
477 if (!acpi_BatteryIsPresent(dev) || !device_is_attached(dev)) {
478 ACPI_SERIAL_END(cmbat);
483 * Only query the battery if this is the first try or the specific
484 * type of info is still invalid.
486 if (retry == 0 || !acpi_battery_bst_valid(&sc->bst)) {
487 timespecclear(&sc->bst_lastupdated);
488 acpi_cmbat_get_bst(dev);
490 if (retry == 0 || !acpi_battery_bif_valid(&sc->bif))
491 acpi_cmbat_get_bif(dev);
493 valid = acpi_battery_bst_valid(&sc->bst) &&
494 acpi_battery_bif_valid(&sc->bif);
495 ACPI_SERIAL_END(cmbat);
501 if (retry == ACPI_CMBAT_RETRY_MAX) {
502 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
503 "battery initialization failed, giving up\n");
505 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
506 "battery initialization done, tried %d times\n", retry + 1);