xref: /dragonfly/sys/dev/acpica/acpi_cmbat.c (revision ee46a572)
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     .gpri = KOBJ_GPRI_ACPI
103 };
104 
105 static devclass_t acpi_cmbat_devclass;
106 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, NULL, NULL);
107 MODULE_DEPEND(acpi_cmbat, acpi, 1, 1, 1);
108 
109 static int
110 acpi_cmbat_probe(device_t dev)
111 {
112     static char *cmbat_ids[] = { "PNP0C0A", NULL };
113 
114     if (acpi_disabled("cmbat") ||
115 	ACPI_ID_PROBE(device_get_parent(dev), dev, cmbat_ids) == NULL)
116 	return (ENXIO);
117 
118     device_set_desc(dev, "ACPI Control Method Battery");
119     return (0);
120 }
121 
122 static int
123 acpi_cmbat_attach(device_t dev)
124 {
125     int		error;
126     ACPI_HANDLE	handle, h;
127     struct acpi_cmbat_softc *sc;
128 
129     sc = device_get_softc(dev);
130     handle = acpi_get_handle(dev);
131     sc->dev = dev;
132 
133     ACPI_SERIAL_INIT(cmbat);
134 
135     timespecclear(&sc->bst_lastupdated);
136 
137     sc->bix_present = ACPI_SUCCESS(AcpiGetHandle(handle, "_BIX", &h));
138     if (sc->bix_present)
139 	device_printf(dev, "supports extended information\n");
140 
141     error = acpi_battery_register(dev);
142     if (error != 0) {
143     	device_printf(dev, "registering battery failed\n");
144 	return (error);
145     }
146 
147     /*
148      * Install a system notify handler in addition to the device notify.
149      * Toshiba notebook uses this alternate notify for its battery.
150      */
151     AcpiInstallNotifyHandler(handle, ACPI_ALL_NOTIFY,
152 	acpi_cmbat_notify_handler, dev);
153 
154     AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
155 
156     return (0);
157 }
158 
159 static int
160 acpi_cmbat_detach(device_t dev)
161 {
162     ACPI_HANDLE	handle;
163 
164     handle = acpi_get_handle(dev);
165     AcpiRemoveNotifyHandler(handle, ACPI_ALL_NOTIFY, acpi_cmbat_notify_handler);
166     acpi_battery_remove(dev);
167     return (0);
168 }
169 
170 static int
171 acpi_cmbat_resume(device_t dev)
172 {
173 
174     AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_init_battery, dev);
175     return (0);
176 }
177 
178 static void
179 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
180 {
181     struct acpi_cmbat_softc *sc;
182     device_t dev;
183 
184     dev = (device_t)context;
185     sc = device_get_softc(dev);
186 
187     switch (notify) {
188     case ACPI_NOTIFY_DEVICE_CHECK:
189     case ACPI_BATTERY_BST_CHANGE:
190 	/*
191 	 * Clear the last updated time.  The next call to retrieve the
192 	 * battery status will get the new value for us.
193 	 */
194 	timespecclear(&sc->bst_lastupdated);
195 	break;
196     case ACPI_NOTIFY_BUS_CHECK:
197     case ACPI_BATTERY_BIF_CHANGE:
198 	/*
199 	 * Queue a callback to get the current battery info from thread
200 	 * context.  It's not safe to block in a notify handler.
201 	 */
202 	AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cmbat_get_bif_task, dev);
203 	break;
204     default:
205 	device_printf(sc->dev, "unknown notify: %#x\n", notify);
206 	break;
207     }
208 
209     acpi_UserNotify("CMBAT", h, notify);
210 }
211 
212 static int
213 acpi_cmbat_info_expired(struct timespec *lastupdated)
214 {
215     struct timespec	curtime;
216 
217     ACPI_SERIAL_ASSERT(cmbat);
218 
219     if (lastupdated == NULL)
220 	return (TRUE);
221     if (!timespecisset(lastupdated))
222 	return (TRUE);
223 
224     getnanotime(&curtime);
225     timespecsub(&curtime, lastupdated);
226     return (curtime.tv_sec < 0 ||
227 	    curtime.tv_sec > acpi_battery_get_info_expire());
228 }
229 
230 static void
231 acpi_cmbat_info_updated(struct timespec *lastupdated)
232 {
233 
234     ACPI_SERIAL_ASSERT(cmbat);
235 
236     if (lastupdated != NULL)
237 	getnanotime(lastupdated);
238 }
239 
240 static void
241 acpi_cmbat_get_bst(void *arg)
242 {
243     struct acpi_cmbat_softc *sc;
244     ACPI_STATUS	as;
245     ACPI_OBJECT	*res;
246     ACPI_HANDLE	h;
247     ACPI_BUFFER	bst_buffer;
248     device_t dev;
249 
250     ACPI_SERIAL_ASSERT(cmbat);
251 
252     dev = arg;
253     sc = device_get_softc(dev);
254     h = acpi_get_handle(dev);
255     bst_buffer.Pointer = NULL;
256     bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
257 
258     if (!acpi_cmbat_info_expired(&sc->bst_lastupdated))
259 	goto end;
260 
261     as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer);
262     if (ACPI_FAILURE(as)) {
263 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
264 		    "error fetching current battery status -- %s\n",
265 		    AcpiFormatException(as));
266 	goto end;
267     }
268 
269     res = (ACPI_OBJECT *)bst_buffer.Pointer;
270     if (!ACPI_PKG_VALID(res, 4)) {
271 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
272 		    "battery status corrupted\n");
273 	goto end;
274     }
275 
276     if (acpi_PkgInt32(res, 0, &sc->bst.state) != 0)
277 	goto end;
278     if (acpi_PkgInt32(res, 1, &sc->bst.rate) != 0)
279 	goto end;
280     if (acpi_PkgInt32(res, 2, &sc->bst.cap) != 0)
281 	goto end;
282     if (acpi_PkgInt32(res, 3, &sc->bst.volt) != 0)
283 	goto end;
284     acpi_cmbat_info_updated(&sc->bst_lastupdated);
285 
286     /* Clear out undefined/extended bits that might be set by hardware. */
287     sc->bst.state &= ACPI_BATT_STAT_BST_MASK;
288     if ((sc->bst.state & ACPI_BATT_STAT_INVALID) == ACPI_BATT_STAT_INVALID)
289 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
290 	    "battery reports simultaneous charging and discharging\n");
291 
292     /* XXX If all batteries are critical, perhaps we should suspend. */
293     if (sc->bst.state & ACPI_BATT_STAT_CRITICAL) {
294     	if ((sc->flags & ACPI_BATT_STAT_CRITICAL) == 0) {
295 	    sc->flags |= ACPI_BATT_STAT_CRITICAL;
296 	    device_printf(dev, "critically low charge!\n");
297 	}
298     } else
299 	sc->flags &= ~ACPI_BATT_STAT_CRITICAL;
300 
301 end:
302     if (bst_buffer.Pointer != NULL)
303 	AcpiOsFree(bst_buffer.Pointer);
304 }
305 
306 /* XXX There should be a cleaner way to do this locking. */
307 static void
308 acpi_cmbat_get_bif_task(void *arg)
309 {
310 
311     ACPI_SERIAL_BEGIN(cmbat);
312     acpi_cmbat_get_bif(arg);
313     ACPI_SERIAL_END(cmbat);
314 }
315 
316 static void
317 acpi_cmbat_get_bif(void *arg)
318 {
319     struct acpi_cmbat_softc *sc;
320     ACPI_STATUS	as;
321     ACPI_OBJECT	*res;
322     ACPI_HANDLE	h;
323     ACPI_BUFFER	info_buffer;
324     device_t dev;
325     int i;
326 
327     ACPI_SERIAL_ASSERT(cmbat);
328 
329     dev = arg;
330     sc = device_get_softc(dev);
331     h = acpi_get_handle(dev);
332     info_buffer.Pointer = NULL;
333     info_buffer.Length = ACPI_ALLOCATE_BUFFER;
334 
335     as = AcpiEvaluateObject(h, sc->bix_present ? "_BIX" : "_BIF", NULL,
336 	&info_buffer);
337     if (ACPI_FAILURE(as)) {
338 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
339 		    "error fetching current %sbattery info -- %s\n",
340 		    sc->bix_present ? "extended " : "",
341 		    AcpiFormatException(as));
342 	goto end;
343     }
344 
345     res = (ACPI_OBJECT *)info_buffer.Pointer;
346     if (!ACPI_PKG_VALID(res, sc->bix_present ? 20 : 13)) {
347 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
348 		    "%sbattery info corrupted\n",
349 		    sc->bix_present ? "extended " : "");
350 	goto end;
351     }
352 
353     i = sc->bix_present ? 1 : 0; /* _BIX: Skip Revision field. */
354     if (acpi_PkgInt32(res, i++, &sc->bif.units) != 0)
355 	goto end;
356     if (acpi_PkgInt32(res, i++, &sc->bif.dcap) != 0)
357 	goto end;
358     if (acpi_PkgInt32(res, i++, &sc->bif.lfcap) != 0)
359 	goto end;
360     if (acpi_PkgInt32(res, i++, &sc->bif.btech) != 0)
361 	goto end;
362     if (acpi_PkgInt32(res, i++, &sc->bif.dvol) != 0)
363 	goto end;
364     if (acpi_PkgInt32(res, i++, &sc->bif.wcap) != 0)
365 	goto end;
366     if (acpi_PkgInt32(res, i++, &sc->bif.lcap) != 0)
367 	goto end;
368     if (sc->bix_present)
369 	i += 6;	   /* _BIX: Continue with Battery Capacity Granularity 1. */
370     if (acpi_PkgInt32(res, i++, &sc->bif.gra1) != 0)
371 	goto end;
372     if (acpi_PkgInt32(res, i++, &sc->bif.gra2) != 0)
373 	goto end;
374     if (acpi_PkgStr(res,  i++, sc->bif.model, ACPI_CMBAT_MAXSTRLEN) != 0)
375 	goto end;
376     if (acpi_PkgStr(res, i++, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN) != 0)
377 	goto end;
378     if (acpi_PkgStr(res, i++, sc->bif.type, ACPI_CMBAT_MAXSTRLEN) != 0)
379 	goto end;
380     if (acpi_PkgStr(res, i++, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN) != 0)
381 	goto end;
382     /* _BIX: Ignore Battery Swapping Capability field. */
383 
384 end:
385     if (info_buffer.Pointer != NULL)
386 	AcpiOsFree(info_buffer.Pointer);
387 }
388 
389 static int
390 acpi_cmbat_bif(device_t dev, struct acpi_bif *bifp)
391 {
392     struct acpi_cmbat_softc *sc;
393 
394     sc = device_get_softc(dev);
395 
396     /*
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.
401      */
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);
417 
418     return (0);
419 }
420 
421 static int
422 acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp)
423 {
424     struct acpi_cmbat_softc *sc;
425 
426     sc = device_get_softc(dev);
427 
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;
435     } else
436 	bstp->state = ACPI_BATT_STAT_NOT_PRESENT;
437     ACPI_SERIAL_END(cmbat);
438 
439     return (0);
440 }
441 
442 static void
443 acpi_cmbat_init_battery(void *arg)
444 {
445     struct acpi_cmbat_softc *sc;
446     int		retry, valid;
447     device_t	dev;
448 
449     dev = (device_t)arg;
450     sc = device_get_softc(dev);
451     ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
452 		"battery initialization start\n");
453 
454     /*
455      * Try repeatedly to get valid data from the battery.  Since the
456      * embedded controller isn't always ready just after boot, we may have
457      * to wait a while.
458      */
459     for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10000)) {
460 	/* batteries on DOCK can be ejected w/ DOCK during retrying */
461 	if (!device_is_attached(dev))
462 	    return;
463 
464 	if (!acpi_BatteryIsPresent(dev))
465 	    continue;
466 
467 	/*
468 	 * Only query the battery if this is the first try or the specific
469 	 * type of info is still invalid.
470 	 */
471 	ACPI_SERIAL_BEGIN(cmbat);
472 	if (retry == 0 || !acpi_battery_bst_valid(&sc->bst)) {
473 	    timespecclear(&sc->bst_lastupdated);
474 	    acpi_cmbat_get_bst(dev);
475 	}
476 	if (retry == 0 || !acpi_battery_bif_valid(&sc->bif))
477 	    acpi_cmbat_get_bif(dev);
478 
479 	valid = acpi_battery_bst_valid(&sc->bst) &&
480 	    acpi_battery_bif_valid(&sc->bif);
481 	ACPI_SERIAL_END(cmbat);
482 
483 	if (valid)
484 	    break;
485     }
486 
487     if (retry == ACPI_CMBAT_RETRY_MAX) {
488 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
489 		    "battery initialization failed, giving up\n");
490     } else {
491 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
492 		    "battery initialization done, tried %d times\n", retry + 1);
493     }
494 }
495