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
29 * $FreeBSD: src/sys/dev/acpica/acpi_cmbat.c,v 1.46.8.1 2009/04/15 03:14:26 kensmith Exp $
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/ioccom.h>
40 #include <sys/malloc.h>
43 #include <dev/acpica5/acpivar.h>
44 #include <dev/acpica5/acpiio.h>
46 MALLOC_DEFINE(M_ACPICMBAT, "acpicmbat", "ACPI control method battery data");
48 /* Number of times to retry initialization before giving up. */
49 #define ACPI_CMBAT_RETRY_MAX 6
51 /* Check the battery once a minute. */
52 #define CMBAT_POLLRATE (60 * hz)
54 /* 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 struct acpi_cmbat_softc {
67 struct timespec bst_lastupdated;
70 ACPI_SERIAL_DECL(cmbat, "ACPI cmbat");
72 static int acpi_cmbat_probe(device_t dev);
73 static int acpi_cmbat_attach(device_t dev);
74 static int acpi_cmbat_detach(device_t dev);
75 static int acpi_cmbat_resume(device_t dev);
76 static void acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify,
78 static int acpi_cmbat_info_expired(struct timespec *lastupdated);
79 static void acpi_cmbat_info_updated(struct timespec *lastupdated);
80 static void acpi_cmbat_get_bst(void *arg);
81 static void acpi_cmbat_get_bif_task(void *arg);
82 static void acpi_cmbat_get_bif(void *arg);
83 static int acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp);
84 static int acpi_cmbat_bif(device_t dev, struct acpi_bif *bifp);
85 static void acpi_cmbat_init_battery(void *arg);
87 static device_method_t acpi_cmbat_methods[] = {
88 /* Device interface */
89 DEVMETHOD(device_probe, acpi_cmbat_probe),
90 DEVMETHOD(device_attach, acpi_cmbat_attach),
91 DEVMETHOD(device_detach, acpi_cmbat_detach),
92 DEVMETHOD(device_resume, acpi_cmbat_resume),
94 /* ACPI battery interface */
95 DEVMETHOD(acpi_batt_get_info, acpi_cmbat_bif),
96 DEVMETHOD(acpi_batt_get_status, acpi_cmbat_bst),
101 static driver_t acpi_cmbat_driver = {
104 sizeof(struct acpi_cmbat_softc),
107 static devclass_t acpi_cmbat_devclass;
108 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);
109 MODULE_DEPEND(acpi_cmbat, acpi, 1, 1, 1);
112 acpi_cmbat_probe(device_t dev)
114 static char *cmbat_ids[] = { "PNP0C0A", NULL };
116 if (acpi_disabled("cmbat") ||
117 ACPI_ID_PROBE(device_get_parent(dev), dev, cmbat_ids) == NULL)
120 device_set_desc(dev, "ACPI Control Method Battery");
125 acpi_cmbat_attach(device_t dev)
129 struct acpi_cmbat_softc *sc;
131 sc = device_get_softc(dev);
132 handle = acpi_get_handle(dev);
135 ACPI_SERIAL_INIT(cmbat);
137 timespecclear(&sc->bst_lastupdated);
139 error = acpi_battery_register(dev);
141 device_printf(dev, "registering battery failed\n");
146 * Install a system notify handler in addition to the device notify.
147 * Toshiba notebook uses this alternate notify for its battery.
149 AcpiInstallNotifyHandler(handle, ACPI_ALL_NOTIFY,
150 acpi_cmbat_notify_handler, dev);
152 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
158 acpi_cmbat_detach(device_t dev)
162 handle = acpi_get_handle(dev);
163 AcpiRemoveNotifyHandler(handle, ACPI_ALL_NOTIFY, acpi_cmbat_notify_handler);
164 acpi_battery_remove(dev);
169 acpi_cmbat_resume(device_t dev)
172 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
177 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
179 struct acpi_cmbat_softc *sc;
182 dev = (device_t)context;
183 sc = device_get_softc(dev);
186 case ACPI_NOTIFY_DEVICE_CHECK:
187 case ACPI_BATTERY_BST_CHANGE:
189 * Clear the last updated time. The next call to retrieve the
190 * battery status will get the new value for us.
192 timespecclear(&sc->bst_lastupdated);
194 case ACPI_NOTIFY_BUS_CHECK:
195 case ACPI_BATTERY_BIF_CHANGE:
197 * Queue a callback to get the current battery info from thread
198 * context. It's not safe to block in a notify handler.
200 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_get_bif_task, dev);
204 acpi_UserNotify("CMBAT", h, notify);
208 acpi_cmbat_info_expired(struct timespec *lastupdated)
210 struct timespec curtime;
212 ACPI_SERIAL_ASSERT(cmbat);
214 if (lastupdated == NULL)
216 if (!timespecisset(lastupdated))
219 getnanotime(&curtime);
220 timespecsub(&curtime, lastupdated);
221 return (curtime.tv_sec < 0 ||
222 curtime.tv_sec > acpi_battery_get_info_expire());
226 acpi_cmbat_info_updated(struct timespec *lastupdated)
229 ACPI_SERIAL_ASSERT(cmbat);
231 if (lastupdated != NULL)
232 getnanotime(lastupdated);
236 acpi_cmbat_get_bst(void *arg)
238 struct acpi_cmbat_softc *sc;
242 ACPI_BUFFER bst_buffer;
245 ACPI_SERIAL_ASSERT(cmbat);
248 sc = device_get_softc(dev);
249 h = acpi_get_handle(dev);
250 bst_buffer.Pointer = NULL;
251 bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
253 if (!acpi_cmbat_info_expired(&sc->bst_lastupdated))
256 as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer);
257 if (ACPI_FAILURE(as)) {
258 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
259 "error fetching current battery status -- %s\n",
260 AcpiFormatException(as));
264 res = (ACPI_OBJECT *)bst_buffer.Pointer;
265 if (!ACPI_PKG_VALID(res, 4)) {
266 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
267 "battery status corrupted\n");
271 if (acpi_PkgInt32(res, 0, &sc->bst.state) != 0)
273 if (acpi_PkgInt32(res, 1, &sc->bst.rate) != 0)
275 if (acpi_PkgInt32(res, 2, &sc->bst.cap) != 0)
277 if (acpi_PkgInt32(res, 3, &sc->bst.volt) != 0)
279 acpi_cmbat_info_updated(&sc->bst_lastupdated);
281 /* XXX If all batteries are critical, perhaps we should suspend. */
282 if (sc->bst.state & ACPI_BATT_STAT_CRITICAL) {
283 if ((sc->flags & ACPI_BATT_STAT_CRITICAL) == 0) {
284 sc->flags |= ACPI_BATT_STAT_CRITICAL;
285 device_printf(dev, "critically low charge!\n");
288 sc->flags &= ~ACPI_BATT_STAT_CRITICAL;
291 if (bst_buffer.Pointer != NULL)
292 AcpiOsFree(bst_buffer.Pointer);
295 /* XXX There should be a cleaner way to do this locking. */
297 acpi_cmbat_get_bif_task(void *arg)
300 ACPI_SERIAL_BEGIN(cmbat);
301 acpi_cmbat_get_bif(arg);
302 ACPI_SERIAL_END(cmbat);
306 acpi_cmbat_get_bif(void *arg)
308 struct acpi_cmbat_softc *sc;
312 ACPI_BUFFER bif_buffer;
315 ACPI_SERIAL_ASSERT(cmbat);
318 sc = device_get_softc(dev);
319 h = acpi_get_handle(dev);
320 bif_buffer.Pointer = NULL;
321 bif_buffer.Length = ACPI_ALLOCATE_BUFFER;
323 as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer);
324 if (ACPI_FAILURE(as)) {
325 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
326 "error fetching current battery info -- %s\n",
327 AcpiFormatException(as));
331 res = (ACPI_OBJECT *)bif_buffer.Pointer;
332 if (!ACPI_PKG_VALID(res, 13)) {
333 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
334 "battery info corrupted\n");
338 if (acpi_PkgInt32(res, 0, &sc->bif.units) != 0)
340 if (acpi_PkgInt32(res, 1, &sc->bif.dcap) != 0)
342 if (acpi_PkgInt32(res, 2, &sc->bif.lfcap) != 0)
344 if (acpi_PkgInt32(res, 3, &sc->bif.btech) != 0)
346 if (acpi_PkgInt32(res, 4, &sc->bif.dvol) != 0)
348 if (acpi_PkgInt32(res, 5, &sc->bif.wcap) != 0)
350 if (acpi_PkgInt32(res, 6, &sc->bif.lcap) != 0)
352 if (acpi_PkgInt32(res, 7, &sc->bif.gra1) != 0)
354 if (acpi_PkgInt32(res, 8, &sc->bif.gra2) != 0)
356 if (acpi_PkgStr(res, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN) != 0)
358 if (acpi_PkgStr(res, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN) != 0)
360 if (acpi_PkgStr(res, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN) != 0)
362 if (acpi_PkgStr(res, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN) != 0)
366 if (bif_buffer.Pointer != NULL)
367 AcpiOsFree(bif_buffer.Pointer);
371 acpi_cmbat_bif(device_t dev, struct acpi_bif *bifp)
373 struct acpi_cmbat_softc *sc;
375 sc = device_get_softc(dev);
378 * Just copy the data. The only value that should change is the
379 * last-full capacity, so we only update when we get a notify that says
380 * the info has changed. Many systems apparently take a long time to
381 * process a _BIF call so we avoid it if possible.
383 ACPI_SERIAL_BEGIN(cmbat);
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));
397 ACPI_SERIAL_END(cmbat);
403 acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp)
405 struct acpi_cmbat_softc *sc;
407 sc = device_get_softc(dev);
409 ACPI_SERIAL_BEGIN(cmbat);
410 if (acpi_BatteryIsPresent(dev)) {
411 acpi_cmbat_get_bst(dev);
412 bstp->state = sc->bst.state;
413 bstp->rate = sc->bst.rate;
414 bstp->cap = sc->bst.cap;
415 bstp->volt = sc->bst.volt;
417 bstp->state = ACPI_BATT_STAT_NOT_PRESENT;
418 ACPI_SERIAL_END(cmbat);
424 acpi_cmbat_init_battery(void *arg)
426 struct acpi_cmbat_softc *sc;
431 sc = device_get_softc(dev);
432 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
433 "battery initialization start\n");
436 * Try repeatedly to get valid data from the battery. Since the
437 * embedded controller isn't always ready just after boot, we may have
440 for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10000)) {
441 /* batteries on DOCK can be ejected w/ DOCK during retrying */
442 if (!device_is_attached(dev))
445 if (!acpi_BatteryIsPresent(dev))
449 * Only query the battery if this is the first try or the specific
450 * type of info is still invalid.
452 ACPI_SERIAL_BEGIN(cmbat);
453 if (retry == 0 || !acpi_battery_bst_valid(&sc->bst)) {
454 timespecclear(&sc->bst_lastupdated);
455 acpi_cmbat_get_bst(dev);
457 if (retry == 0 || !acpi_battery_bif_valid(&sc->bif))
458 acpi_cmbat_get_bif(dev);
460 valid = acpi_battery_bst_valid(&sc->bst) &&
461 acpi_battery_bif_valid(&sc->bif);
462 ACPI_SERIAL_END(cmbat);
468 if (retry == ACPI_CMBAT_RETRY_MAX) {
469 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
470 "battery initialization failed, giving up\n");
472 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
473 "battery initialization done, tried %d times\n", retry + 1);