xref: /dragonfly/sys/dev/acpica/acpi_cmbat.c (revision 279dd846)
1 /*-
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>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: head/sys/dev/acpica/acpi_cmbat.c 246128 2013-01-30 18:01:20Z sbz $
30  */
31 
32 #include "opt_acpi.h"
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 
38 #include <sys/rman.h>
39 
40 #include "acpi.h"
41 #include <dev/acpica/acpivar.h>
42 #include <dev/acpica/acpiio.h>
43 
44 /* Number of times to retry initialization before giving up. */
45 #define ACPI_CMBAT_RETRY_MAX	6
46 
47 /* Check the battery once a minute. */
48 #define	CMBAT_POLLRATE		(60 * hz)
49 
50 /* Hooks for the ACPICA debugging infrastructure */
51 #define	_COMPONENT	ACPI_BATTERY
52 ACPI_MODULE_NAME("BATTERY")
53 
54 #define	ACPI_BATTERY_BST_CHANGE	0x80
55 #define	ACPI_BATTERY_BIF_CHANGE	0x81
56 
57 struct acpi_cmbat_softc {
58     device_t	    dev;
59     int		    flags;
60     int		    bix_present;
61 
62     struct acpi_bif bif;
63     struct acpi_bst bst;
64     struct timespec bst_lastupdated;
65 };
66 
67 ACPI_SERIAL_DECL(cmbat, "ACPI cmbat");
68 
69 static int		acpi_cmbat_probe(device_t dev);
70 static int		acpi_cmbat_attach(device_t dev);
71 static int		acpi_cmbat_detach(device_t dev);
72 static int		acpi_cmbat_resume(device_t dev);
73 static void		acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify,
74 			    void *context);
75 static int		acpi_cmbat_info_expired(struct timespec *lastupdated);
76 static void		acpi_cmbat_info_updated(struct timespec *lastupdated);
77 static void		acpi_cmbat_get_bst(void *arg);
78 static void		acpi_cmbat_get_bif_task(void *arg);
79 static void		acpi_cmbat_get_bif(void *arg);
80 static int		acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp);
81 static int		acpi_cmbat_bif(device_t dev, struct acpi_bif *bifp);
82 static void		acpi_cmbat_init_battery(void *arg);
83 
84 static device_method_t acpi_cmbat_methods[] = {
85     /* Device interface */
86     DEVMETHOD(device_probe,	acpi_cmbat_probe),
87     DEVMETHOD(device_attach,	acpi_cmbat_attach),
88     DEVMETHOD(device_detach,	acpi_cmbat_detach),
89     DEVMETHOD(device_resume,	acpi_cmbat_resume),
90 
91     /* ACPI battery interface */
92     DEVMETHOD(acpi_batt_get_info, acpi_cmbat_bif),
93     DEVMETHOD(acpi_batt_get_status, acpi_cmbat_bst),
94 
95     DEVMETHOD_END
96 };
97 
98 static driver_t acpi_cmbat_driver = {
99     "battery",
100     acpi_cmbat_methods,
101     sizeof(struct acpi_cmbat_softc),
102 };
103 
104 static devclass_t acpi_cmbat_devclass;
105 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, NULL, NULL);
106 MODULE_DEPEND(acpi_cmbat, acpi, 1, 1, 1);
107 
108 static int
109 acpi_cmbat_probe(device_t dev)
110 {
111     static char *cmbat_ids[] = { "PNP0C0A", NULL };
112 
113     if (acpi_disabled("cmbat") ||
114 	ACPI_ID_PROBE(device_get_parent(dev), dev, cmbat_ids) == NULL)
115 	return (ENXIO);
116 
117     device_set_desc(dev, "ACPI Control Method Battery");
118     return (0);
119 }
120 
121 static int
122 acpi_cmbat_attach(device_t dev)
123 {
124     int		error;
125     ACPI_HANDLE	handle, h;
126     struct acpi_cmbat_softc *sc;
127 
128     sc = device_get_softc(dev);
129     handle = acpi_get_handle(dev);
130     sc->dev = dev;
131 
132     ACPI_SERIAL_INIT(cmbat);
133 
134     timespecclear(&sc->bst_lastupdated);
135 
136     sc->bix_present = ACPI_SUCCESS(AcpiGetHandle(handle, "_BIX", &h));
137     if (sc->bix_present)
138 	device_printf(dev, "supports extended information\n");
139 
140     error = acpi_battery_register(dev);
141     if (error != 0) {
142     	device_printf(dev, "registering battery failed\n");
143 	return (error);
144     }
145 
146     /*
147      * Install a system notify handler in addition to the device notify.
148      * Toshiba notebook uses this alternate notify for its battery.
149      */
150     AcpiInstallNotifyHandler(handle, ACPI_ALL_NOTIFY,
151 	acpi_cmbat_notify_handler, dev);
152 
153     AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
154 
155     return (0);
156 }
157 
158 static int
159 acpi_cmbat_detach(device_t dev)
160 {
161     ACPI_HANDLE	handle;
162 
163     handle = acpi_get_handle(dev);
164     AcpiRemoveNotifyHandler(handle, ACPI_ALL_NOTIFY, acpi_cmbat_notify_handler);
165     acpi_battery_remove(dev);
166     return (0);
167 }
168 
169 static int
170 acpi_cmbat_resume(device_t dev)
171 {
172 
173     AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
174     return (0);
175 }
176 
177 static void
178 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
179 {
180     struct acpi_cmbat_softc *sc;
181     device_t dev;
182 
183     dev = (device_t)context;
184     sc = device_get_softc(dev);
185 
186     switch (notify) {
187     case ACPI_NOTIFY_DEVICE_CHECK:
188     case ACPI_BATTERY_BST_CHANGE:
189 	/*
190 	 * Clear the last updated time.  The next call to retrieve the
191 	 * battery status will get the new value for us.
192 	 */
193 	timespecclear(&sc->bst_lastupdated);
194 	break;
195     case ACPI_NOTIFY_BUS_CHECK:
196     case ACPI_BATTERY_BIF_CHANGE:
197 	/*
198 	 * Queue a callback to get the current battery info from thread
199 	 * context.  It's not safe to block in a notify handler.
200 	 */
201 	AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_get_bif_task, dev);
202 	break;
203     }
204 
205     acpi_UserNotify("CMBAT", h, notify);
206 }
207 
208 static int
209 acpi_cmbat_info_expired(struct timespec *lastupdated)
210 {
211     struct timespec	curtime;
212 
213     ACPI_SERIAL_ASSERT(cmbat);
214 
215     if (lastupdated == NULL)
216 	return (TRUE);
217     if (!timespecisset(lastupdated))
218 	return (TRUE);
219 
220     getnanotime(&curtime);
221     timespecsub(&curtime, lastupdated);
222     return (curtime.tv_sec < 0 ||
223 	    curtime.tv_sec > acpi_battery_get_info_expire());
224 }
225 
226 static void
227 acpi_cmbat_info_updated(struct timespec *lastupdated)
228 {
229 
230     ACPI_SERIAL_ASSERT(cmbat);
231 
232     if (lastupdated != NULL)
233 	getnanotime(lastupdated);
234 }
235 
236 static void
237 acpi_cmbat_get_bst(void *arg)
238 {
239     struct acpi_cmbat_softc *sc;
240     ACPI_STATUS	as;
241     ACPI_OBJECT	*res;
242     ACPI_HANDLE	h;
243     ACPI_BUFFER	bst_buffer;
244     device_t dev;
245 
246     ACPI_SERIAL_ASSERT(cmbat);
247 
248     dev = arg;
249     sc = device_get_softc(dev);
250     h = acpi_get_handle(dev);
251     bst_buffer.Pointer = NULL;
252     bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
253 
254     if (!acpi_cmbat_info_expired(&sc->bst_lastupdated))
255 	goto end;
256 
257     as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer);
258     if (ACPI_FAILURE(as)) {
259 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
260 		    "error fetching current battery status -- %s\n",
261 		    AcpiFormatException(as));
262 	goto end;
263     }
264 
265     res = (ACPI_OBJECT *)bst_buffer.Pointer;
266     if (!ACPI_PKG_VALID(res, 4)) {
267 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
268 		    "battery status corrupted\n");
269 	goto end;
270     }
271 
272     if (acpi_PkgInt32(res, 0, &sc->bst.state) != 0)
273 	goto end;
274     if (acpi_PkgInt32(res, 1, &sc->bst.rate) != 0)
275 	goto end;
276     if (acpi_PkgInt32(res, 2, &sc->bst.cap) != 0)
277 	goto end;
278     if (acpi_PkgInt32(res, 3, &sc->bst.volt) != 0)
279 	goto end;
280     acpi_cmbat_info_updated(&sc->bst_lastupdated);
281 
282     /* Clear out undefined/extended bits that might be set by hardware. */
283     sc->bst.state &= ACPI_BATT_STAT_BST_MASK;
284     if ((sc->bst.state & ACPI_BATT_STAT_INVALID) == ACPI_BATT_STAT_INVALID)
285 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
286 	    "battery reports simultaneous charging and discharging\n");
287 
288     /* XXX If all batteries are critical, perhaps we should suspend. */
289     if (sc->bst.state & ACPI_BATT_STAT_CRITICAL) {
290     	if ((sc->flags & ACPI_BATT_STAT_CRITICAL) == 0) {
291 	    sc->flags |= ACPI_BATT_STAT_CRITICAL;
292 	    device_printf(dev, "critically low charge!\n");
293 	}
294     } else
295 	sc->flags &= ~ACPI_BATT_STAT_CRITICAL;
296 
297 end:
298     if (bst_buffer.Pointer != NULL)
299 	AcpiOsFree(bst_buffer.Pointer);
300 }
301 
302 /* XXX There should be a cleaner way to do this locking. */
303 static void
304 acpi_cmbat_get_bif_task(void *arg)
305 {
306 
307     ACPI_SERIAL_BEGIN(cmbat);
308     acpi_cmbat_get_bif(arg);
309     ACPI_SERIAL_END(cmbat);
310 }
311 
312 static void
313 acpi_cmbat_get_bif(void *arg)
314 {
315     struct acpi_cmbat_softc *sc;
316     ACPI_STATUS	as;
317     ACPI_OBJECT	*res;
318     ACPI_HANDLE	h;
319     ACPI_BUFFER	info_buffer;
320     device_t dev;
321     int i;
322 
323     ACPI_SERIAL_ASSERT(cmbat);
324 
325     dev = arg;
326     sc = device_get_softc(dev);
327     h = acpi_get_handle(dev);
328     info_buffer.Pointer = NULL;
329     info_buffer.Length = ACPI_ALLOCATE_BUFFER;
330 
331     as = AcpiEvaluateObject(h, sc->bix_present ? "_BIX" : "_BIF", NULL,
332 	&info_buffer);
333     if (ACPI_FAILURE(as)) {
334 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
335 		    "error fetching current %sbattery info -- %s\n",
336 		    sc->bix_present ? "extended " : "",
337 		    AcpiFormatException(as));
338 	goto end;
339     }
340 
341     res = (ACPI_OBJECT *)info_buffer.Pointer;
342     if (!ACPI_PKG_VALID(res, sc->bix_present ? 20 : 13)) {
343 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
344 		    "%sbattery info corrupted\n",
345 		    sc->bix_present ? "extended " : "");
346 	goto end;
347     }
348 
349     i = sc->bix_present ? 1 : 0; /* _BIX: Skip Revision field. */
350     if (acpi_PkgInt32(res, i++, &sc->bif.units) != 0)
351 	goto end;
352     if (acpi_PkgInt32(res, i++, &sc->bif.dcap) != 0)
353 	goto end;
354     if (acpi_PkgInt32(res, i++, &sc->bif.lfcap) != 0)
355 	goto end;
356     if (acpi_PkgInt32(res, i++, &sc->bif.btech) != 0)
357 	goto end;
358     if (acpi_PkgInt32(res, i++, &sc->bif.dvol) != 0)
359 	goto end;
360     if (acpi_PkgInt32(res, i++, &sc->bif.wcap) != 0)
361 	goto end;
362     if (acpi_PkgInt32(res, i++, &sc->bif.lcap) != 0)
363 	goto end;
364     if (sc->bix_present)
365 	i += 6;	   /* _BIX: Continue with Battery Capacity Granularity 1. */
366     if (acpi_PkgInt32(res, i++, &sc->bif.gra1) != 0)
367 	goto end;
368     if (acpi_PkgInt32(res, i++, &sc->bif.gra2) != 0)
369 	goto end;
370     if (acpi_PkgStr(res,  i++, sc->bif.model, ACPI_CMBAT_MAXSTRLEN) != 0)
371 	goto end;
372     if (acpi_PkgStr(res, i++, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN) != 0)
373 	goto end;
374     if (acpi_PkgStr(res, i++, sc->bif.type, ACPI_CMBAT_MAXSTRLEN) != 0)
375 	goto end;
376     if (acpi_PkgStr(res, i++, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN) != 0)
377 	goto end;
378     /* _BIX: Ignore Battery Swapping Capability field. */
379 
380 end:
381     if (info_buffer.Pointer != NULL)
382 	AcpiOsFree(info_buffer.Pointer);
383 }
384 
385 static int
386 acpi_cmbat_bif(device_t dev, struct acpi_bif *bifp)
387 {
388     struct acpi_cmbat_softc *sc;
389 
390     sc = device_get_softc(dev);
391 
392     /*
393      * Just copy the data.  The only value that should change is the
394      * last-full capacity, so we only update when we get a notify that says
395      * the info has changed.  Many systems apparently take a long time to
396      * process a _BIF call so we avoid it if possible.
397      */
398     ACPI_SERIAL_BEGIN(cmbat);
399     bifp->units = sc->bif.units;
400     bifp->dcap = sc->bif.dcap;
401     bifp->lfcap = sc->bif.lfcap;
402     bifp->btech = sc->bif.btech;
403     bifp->dvol = sc->bif.dvol;
404     bifp->wcap = sc->bif.wcap;
405     bifp->lcap = sc->bif.lcap;
406     bifp->gra1 = sc->bif.gra1;
407     bifp->gra2 = sc->bif.gra2;
408     strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
409     strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
410     strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
411     strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
412     ACPI_SERIAL_END(cmbat);
413 
414     return (0);
415 }
416 
417 static int
418 acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp)
419 {
420     struct acpi_cmbat_softc *sc;
421 
422     sc = device_get_softc(dev);
423 
424     ACPI_SERIAL_BEGIN(cmbat);
425     if (acpi_BatteryIsPresent(dev)) {
426 	acpi_cmbat_get_bst(dev);
427 	bstp->state = sc->bst.state;
428 	bstp->rate = sc->bst.rate;
429 	bstp->cap = sc->bst.cap;
430 	bstp->volt = sc->bst.volt;
431     } else
432 	bstp->state = ACPI_BATT_STAT_NOT_PRESENT;
433     ACPI_SERIAL_END(cmbat);
434 
435     return (0);
436 }
437 
438 static void
439 acpi_cmbat_init_battery(void *arg)
440 {
441     struct acpi_cmbat_softc *sc;
442     int		retry, valid;
443     device_t	dev;
444 
445     dev = (device_t)arg;
446     sc = device_get_softc(dev);
447     ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
448 		"battery initialization start\n");
449 
450     /*
451      * Try repeatedly to get valid data from the battery.  Since the
452      * embedded controller isn't always ready just after boot, we may have
453      * to wait a while.
454      */
455     for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10000)) {
456 	/* batteries on DOCK can be ejected w/ DOCK during retrying */
457 	if (!device_is_attached(dev))
458 	    return;
459 
460 	if (!acpi_BatteryIsPresent(dev))
461 	    continue;
462 
463 	/*
464 	 * Only query the battery if this is the first try or the specific
465 	 * type of info is still invalid.
466 	 */
467 	ACPI_SERIAL_BEGIN(cmbat);
468 	if (retry == 0 || !acpi_battery_bst_valid(&sc->bst)) {
469 	    timespecclear(&sc->bst_lastupdated);
470 	    acpi_cmbat_get_bst(dev);
471 	}
472 	if (retry == 0 || !acpi_battery_bif_valid(&sc->bif))
473 	    acpi_cmbat_get_bif(dev);
474 
475 	valid = acpi_battery_bst_valid(&sc->bst) &&
476 	    acpi_battery_bif_valid(&sc->bif);
477 	ACPI_SERIAL_END(cmbat);
478 
479 	if (valid)
480 	    break;
481     }
482 
483     if (retry == ACPI_CMBAT_RETRY_MAX) {
484 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
485 		    "battery initialization failed, giving up\n");
486     } else {
487 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
488 		    "battery initialization done, tried %d times\n", retry + 1);
489     }
490 }
491