xref: /dragonfly/sys/dev/acpica/acpi_cmbat.c (revision 333227be)
1 /*-
2  * Copyright (c) 2000 Munehiro Matsuda
3  * Copyright (c) 2000 Takanori Watanabe
4  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/dev/acpica/acpi_cmbat.c,v 1.22.2.1 2003/08/22 20:49:20 jhb Exp $
29  *      $DragonFly: src/sys/dev/acpica/Attic/acpi_cmbat.c,v 1.2 2004/05/05 22:19:23 dillon Exp $
30  */
31 
32 #include "opt_acpi.h"
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/ioccom.h>
37 #include <sys/conf.h>
38 
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42 #include <sys/malloc.h>
43 
44 #include  "acpi.h"
45 
46 #include <dev/acpica/acpivar.h>
47 #include <dev/acpica/acpiio.h>
48 
49 MALLOC_DEFINE(M_ACPICMBAT, "acpicmbat", "ACPI control method battery data");
50 
51 #define	CMBAT_POLLRATE	(60 * hz)
52 
53 /*
54  * Hooks for the ACPI CA debugging infrastructure
55  */
56 #define	_COMPONENT	ACPI_BATTERY
57 ACPI_MODULE_NAME("BATTERY")
58 
59 #define	ACPI_BATTERY_BST_CHANGE 0x80
60 #define	ACPI_BATTERY_BIF_CHANGE 0x81
61 
62 #define	PKG_GETINT(res, tmp, idx, dest, label) do {			\
63 	tmp = &res->Package.Elements[idx];				\
64 	if (tmp == NULL) {						\
65 		ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),	\
66 		    "%s: PKG_GETINT error, idx = %d\n.", __func__, idx); \
67 		goto label;						\
68 	}								\
69 	if (tmp->Type != ACPI_TYPE_INTEGER)				\
70 		goto label;						\
71 	dest = tmp->Integer.Value;					\
72 } while (0)
73 
74 #define	PKG_GETSTR(res, tmp, idx, dest, size, label) do {              	\
75 	size_t	length;							\
76 	length = size;							\
77 	tmp = &res->Package.Elements[idx]; 				\
78 	if (tmp == NULL) {						\
79 		ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),	\
80 		    "%s: PKG_GETSTR error, idx = %d\n.", __func__, idx); \
81 		goto label;						\
82 	}								\
83 	bzero(dest, sizeof(dest));					\
84 	switch (tmp->Type) {						\
85 	case ACPI_TYPE_STRING:						\
86 		if (tmp->String.Length < length) {			\
87 			length = tmp->String.Length;			\
88 		}							\
89 		strncpy(dest, tmp->String.Pointer, length);		\
90 		break;							\
91 	case ACPI_TYPE_BUFFER:						\
92 		if (tmp->Buffer.Length < length) {			\
93 			length = tmp->Buffer.Length;			\
94 		}							\
95 		strncpy(dest, tmp->Buffer.Pointer, length);		\
96 		break;							\
97 	default:							\
98 		goto label;						\
99 	}								\
100 	dest[sizeof(dest)-1] = '\0';					\
101 } while (0)
102 
103 struct acpi_cmbat_softc {
104 	device_t	dev;
105 
106 	struct acpi_bif	bif;
107 	struct acpi_bst	bst;
108 	struct timespec	bif_lastupdated;
109 	struct timespec	bst_lastupdated;
110 	int		bif_updating;
111 	int		bst_updating;
112 
113 	int		present;
114 	int		cap;
115 	int		min;
116 	int		full_charge_time;
117 	int		initializing;
118 };
119 
120 static struct timespec	 acpi_cmbat_info_lastupdated;
121 
122 /* XXX: devclass_get_maxunit() don't give us the current allocated units... */
123 static int		 acpi_cmbat_units = 0;
124 
125 static int		 acpi_cmbat_info_expired(struct timespec *);
126 static void		 acpi_cmbat_info_updated(struct timespec *);
127 static void		 acpi_cmbat_get_bst(void *);
128 static void		 acpi_cmbat_get_bif(void *);
129 static void		 acpi_cmbat_notify_handler(ACPI_HANDLE, UINT32, void *);
130 static int		 acpi_cmbat_probe(device_t);
131 static int		 acpi_cmbat_attach(device_t);
132 static int		 acpi_cmbat_resume(device_t);
133 static int		 acpi_cmbat_ioctl(u_long, caddr_t, void *);
134 static int		 acpi_cmbat_is_bst_valid(struct acpi_bst*);
135 static int		 acpi_cmbat_is_bif_valid(struct acpi_bif*);
136 static int		 acpi_cmbat_get_total_battinfo(struct acpi_battinfo *);
137 static void		 acpi_cmbat_init_battery(void *);
138 
139 static __inline int
140 acpi_cmbat_info_expired(struct timespec *lastupdated)
141 {
142 	struct timespec	curtime;
143 
144 	if (lastupdated == NULL) {
145 		return (1);
146 	}
147 
148 	if (!timespecisset(lastupdated)) {
149 		return (1);
150 	}
151 
152 	getnanotime(&curtime);
153 	timespecsub(&curtime, lastupdated);
154 	return ((curtime.tv_sec < 0 || curtime.tv_sec > acpi_battery_get_info_expire()));
155 }
156 
157 
158 static __inline void
159 acpi_cmbat_info_updated(struct timespec *lastupdated)
160 {
161 
162 	if (lastupdated != NULL) {
163 		getnanotime(lastupdated);
164 	}
165 }
166 
167 static void
168 acpi_cmbat_get_bst(void *context)
169 {
170 	device_t	dev;
171 	struct acpi_cmbat_softc *sc;
172 	ACPI_STATUS	as;
173 	ACPI_OBJECT	*res, *tmp;
174 	ACPI_HANDLE	h;
175 	ACPI_BUFFER	bst_buffer;
176 
177 	dev = context;
178 	sc = device_get_softc(dev);
179 	h = acpi_get_handle(dev);
180 	bst_buffer.Pointer = NULL;
181 
182 	if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) {
183 		return;
184 	}
185 
186 	if (sc->bst_updating) {
187 		return;
188 	}
189 	sc->bst_updating = 1;
190 
191 	bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
192 	if (ACPI_FAILURE(as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer))) {
193 		ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
194 		    "error fetching current battery status -- %s\n",
195 		    AcpiFormatException(as));
196 		goto end;
197 	}
198 
199 	res = (ACPI_OBJECT *)bst_buffer.Pointer;
200 
201 	if ((res == NULL) || (res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 4)) {
202 		ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
203 		    "battery status corrupted\n");
204 		goto end;
205 	}
206 
207 	PKG_GETINT(res, tmp, 0, sc->bst.state, end);
208 	PKG_GETINT(res, tmp, 1, sc->bst.rate, end);
209 	PKG_GETINT(res, tmp, 2, sc->bst.cap, end);
210 	PKG_GETINT(res, tmp, 3, sc->bst.volt, end);
211 	acpi_cmbat_info_updated(&sc->bst_lastupdated);
212 end:
213 	if (bst_buffer.Pointer != NULL)
214 		AcpiOsFree(bst_buffer.Pointer);
215 	sc->bst_updating = 0;
216 }
217 
218 static void
219 acpi_cmbat_get_bif(void *context)
220 {
221 	device_t	dev;
222 	struct acpi_cmbat_softc *sc;
223 	ACPI_STATUS	as;
224 	ACPI_OBJECT	*res, *tmp;
225 	ACPI_HANDLE	h;
226 	ACPI_BUFFER	bif_buffer;
227 
228 	dev = context;
229 	sc = device_get_softc(dev);
230 	h = acpi_get_handle(dev);
231 	bif_buffer.Pointer = NULL;
232 
233 	if (!acpi_cmbat_info_expired(&sc->bif_lastupdated)) {
234 		return;
235 	}
236 
237 	if (sc->bif_updating) {
238 		return;
239 	}
240 	sc->bif_updating = 1;
241 
242 	bif_buffer.Length = ACPI_ALLOCATE_BUFFER;
243 	if (ACPI_FAILURE(as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer))) {
244 		ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
245 		    "error fetching current battery info -- %s\n",
246 		    AcpiFormatException(as));
247 		goto end;
248 	}
249 
250 	res = (ACPI_OBJECT *)bif_buffer.Pointer;
251 
252 	if ((res == NULL) || (res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 13)) {
253 		ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
254 		    "battery info corrupted\n");
255 		goto end;
256 	}
257 
258 	PKG_GETINT(res, tmp,  0, sc->bif.unit, end);
259 	PKG_GETINT(res, tmp,  1, sc->bif.dcap, end);
260 	PKG_GETINT(res, tmp,  2, sc->bif.lfcap, end);
261 	PKG_GETINT(res, tmp,  3, sc->bif.btech, end);
262 	PKG_GETINT(res, tmp,  4, sc->bif.dvol, end);
263 	PKG_GETINT(res, tmp,  5, sc->bif.wcap, end);
264 	PKG_GETINT(res, tmp,  6, sc->bif.lcap, end);
265 	PKG_GETINT(res, tmp,  7, sc->bif.gra1, end);
266 	PKG_GETINT(res, tmp,  8, sc->bif.gra2, end);
267 	PKG_GETSTR(res, tmp,  9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN, end);
268 	PKG_GETSTR(res, tmp, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN, end);
269 	PKG_GETSTR(res, tmp, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN, end);
270 	PKG_GETSTR(res, tmp, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN, end);
271 	acpi_cmbat_info_updated(&sc->bif_lastupdated);
272 end:
273 	if (bif_buffer.Pointer != NULL)
274 		AcpiOsFree(bif_buffer.Pointer);
275 	sc->bif_updating = 0;
276 }
277 
278 static void
279 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
280 {
281 	device_t	dev;
282 	struct acpi_cmbat_softc	*sc;
283 
284 	dev = (device_t)context;
285 	if ((sc = device_get_softc(dev)) == NULL) {
286 		return;
287 	}
288 
289 	switch (notify) {
290 	case ACPI_BATTERY_BST_CHANGE:
291 		timespecclear(&sc->bst_lastupdated);
292 		break;
293 	case ACPI_BATTERY_BIF_CHANGE:
294 		timespecclear(&sc->bif_lastupdated);
295 		AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
296 		break;
297 	default:
298 		break;
299 	}
300 }
301 
302 static int
303 acpi_cmbat_probe(device_t dev)
304 {
305 
306 	if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
307 	    !acpi_disabled("cmbat") &&
308 	    acpi_MatchHid(dev, "PNP0C0A")) {
309 		/*
310 		 * Set device description.
311 		 */
312 		device_set_desc(dev, "Control method Battery");
313 		return (0);
314 	}
315 	return (ENXIO);
316 }
317 
318 static int
319 acpi_cmbat_attach(device_t dev)
320 {
321 	int		error;
322 	ACPI_HANDLE	handle;
323 	struct acpi_cmbat_softc *sc;
324 
325 	if ((sc = device_get_softc(dev)) == NULL) {
326 		return (ENXIO);
327 	}
328 
329 	handle = acpi_get_handle(dev);
330 
331 	AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
332 				 acpi_cmbat_notify_handler, dev);
333 
334 	sc->bif_updating = sc->bst_updating = 0;
335 	sc->dev = dev;
336 
337 	timespecclear(&sc->bif_lastupdated);
338 	timespecclear(&sc->bst_lastupdated);
339 
340 	if (acpi_cmbat_units == 0) {
341 		if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BIF,
342 				acpi_cmbat_ioctl, NULL)) != 0) {
343 			return (error);
344 		}
345 		if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BST,
346 				acpi_cmbat_ioctl, NULL)) != 0) {
347 			return (error);
348 		}
349 	}
350 
351 	if ((error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT,
352 			acpi_cmbat_units)) != 0) {
353 		return (error);
354 	}
355 
356 	acpi_cmbat_units++;
357 	timespecclear(&acpi_cmbat_info_lastupdated);
358 	sc->initializing = 0;
359 
360 	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_init_battery, dev);
361 	return (0);
362 }
363 
364 static int
365 acpi_cmbat_resume(device_t dev)
366 {
367 
368 	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_init_battery, dev);
369 	return (0);
370 }
371 
372 static device_method_t acpi_cmbat_methods[] = {
373 	/* Device interface */
374 	DEVMETHOD(device_probe,		acpi_cmbat_probe),
375 	DEVMETHOD(device_attach,	acpi_cmbat_attach),
376 	DEVMETHOD(device_resume,	acpi_cmbat_resume),
377 
378 	{0, 0}
379 };
380 
381 static driver_t acpi_cmbat_driver = {
382 	"acpi_cmbat",
383 	acpi_cmbat_methods,
384 	sizeof(struct acpi_cmbat_softc),
385 };
386 
387 static devclass_t acpi_cmbat_devclass;
388 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);
389 
390 static int
391 acpi_cmbat_ioctl(u_long cmd, caddr_t addr, void *arg)
392 {
393 	device_t	dev;
394 	union acpi_battery_ioctl_arg *ioctl_arg;
395 	struct acpi_cmbat_softc *sc;
396 	struct acpi_bif	*bifp;
397 	struct acpi_bst	*bstp;
398 
399 	ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
400 	if ((dev = devclass_get_device(acpi_cmbat_devclass,
401 			ioctl_arg->unit)) == NULL) {
402 		return (ENXIO);
403 	}
404 
405 	if ((sc = device_get_softc(dev)) == NULL) {
406 		return (ENXIO);
407 	}
408 
409         /*
410          * No security check required: information retrieval only.  If
411          * new functions are added here, a check might be required.
412          */
413 
414 	switch (cmd) {
415 	case ACPIIO_CMBAT_GET_BIF:
416 		acpi_cmbat_get_bif(dev);
417 		bifp = &ioctl_arg->bif;
418 		bifp->unit = sc->bif.unit;
419 		bifp->dcap = sc->bif.dcap;
420 		bifp->lfcap = sc->bif.lfcap;
421 		bifp->btech = sc->bif.btech;
422 		bifp->dvol = sc->bif.dvol;
423 		bifp->wcap = sc->bif.wcap;
424 		bifp->lcap = sc->bif.lcap;
425 		bifp->gra1 = sc->bif.gra1;
426 		bifp->gra2 = sc->bif.gra2;
427 		strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
428 		strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
429 		strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
430 		strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
431 		break;
432 
433 	case ACPIIO_CMBAT_GET_BST:
434 		bstp = &ioctl_arg->bst;
435 		if (acpi_BatteryIsPresent(dev)) {
436 			acpi_cmbat_get_bst(dev);
437 			bstp->state = sc->bst.state;
438 			bstp->rate = sc->bst.rate;
439 			bstp->cap = sc->bst.cap;
440 			bstp->volt = sc->bst.volt;
441 		} else
442 			bstp->state = ACPI_BATT_STAT_NOT_PRESENT;
443 		break;
444 	}
445 
446 	return (0);
447 }
448 
449 static __inline int
450 acpi_cmbat_is_bst_valid(struct acpi_bst *bst)
451 {
452 	if (bst->state >= ACPI_BATT_STAT_MAX ||
453 	    bst->cap == 0xffffffff ||
454 	    bst->volt == 0xffffffff) {
455 		return (0);
456 	}
457 
458 	return (1);
459 }
460 
461 static __inline int
462 acpi_cmbat_is_bif_valid(struct acpi_bif *bif)
463 {
464 	if (bif->lfcap == 0) {
465 		return (0);
466 	}
467 
468 	return (1);
469 }
470 
471 static int
472 acpi_cmbat_get_total_battinfo(struct acpi_battinfo *battinfo)
473 {
474 	int		i;
475 	int		error;
476 	int		batt_stat;
477 	int		valid_rate, valid_units;
478 	int		cap, min;
479 	int		total_cap, total_min, total_full;
480 	device_t	dev;
481 	struct acpi_cmbat_softc *sc;
482 	static int	bat_units = 0;
483 	static struct acpi_cmbat_softc **bat = NULL;
484 
485 	cap = min = -1;
486 	batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
487 	error = 0;
488 
489 	/* Allocate array of softc pointers */
490 	if (bat_units != acpi_cmbat_units) {
491 		if (bat != NULL) {
492 			free(bat, M_ACPICMBAT);
493 			bat = NULL;
494 		}
495 		bat_units = 0;
496 	}
497 	if (bat == NULL) {
498 		bat_units = acpi_cmbat_units;
499 		bat = malloc(sizeof(struct acpi_cmbat_softc *) * bat_units,
500 			     M_ACPICMBAT, M_INTWAIT);
501 
502 		/* Collect softc pointers */
503 		for (i = 0; i < acpi_cmbat_units; i++) {
504 			if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) {
505 				error = ENXIO;
506 				goto out;
507 			}
508 
509 			if ((sc = device_get_softc(dev)) == NULL) {
510 				error = ENXIO;
511 				goto out;
512 			}
513 
514 			bat[i] = sc;
515 		}
516 	}
517 
518 	/* Get battery status, valid rate and valid units */
519 	batt_stat = valid_rate = valid_units = 0;
520 	for (i = 0; i < acpi_cmbat_units; i++) {
521 		bat[i]->present = acpi_BatteryIsPresent(bat[i]->dev);
522 		if (!bat[i]->present)
523 			continue;
524 
525 		acpi_cmbat_get_bst(bat[i]->dev);
526 
527 		/* If battey not installed, we get strange values */
528 		if (!acpi_cmbat_is_bst_valid(&(bat[i]->bst)) ||
529 		    !acpi_cmbat_is_bif_valid(&(bat[i]->bif))) {
530 			bat[i]->present = 0;
531 			continue;
532 		}
533 
534 		valid_units++;
535 
536 		bat[i]->cap = 100 * bat[i]->bst.cap / bat[i]->bif.lfcap;
537 
538 		batt_stat |= bat[i]->bst.state;
539 
540 		if (bat[i]->bst.rate > 0) {
541 			/*
542 			 * XXX Hack to calculate total battery time.
543 			 * Systems with 2 or more battries, they may get used
544 			 * one by one, thus bst.rate is set only to the one
545 			 * in use. For remaining batteries bst.rate = 0, which
546 			 * makes it impossible to calculate remaining time.
547 			 * Some other systems may need sum of bst.rate in
548 			 * dis-charging state.
549 			 * There for we sum up the bst.rate that is valid
550 			 * (in dis-charging state), and use the sum to
551 			 * calcutate remaining batteries' time.
552 			 */
553 			if (bat[i]->bst.state & ACPI_BATT_STAT_DISCHARG) {
554 				valid_rate += bat[i]->bst.rate;
555 			}
556 		}
557 	}
558 
559 	/* Calculate total battery capacity and time */
560 	total_cap = total_min = total_full = 0;
561 	for (i = 0; i < acpi_cmbat_units; i++) {
562 		if (!bat[i]->present) {
563 			continue;
564 		}
565 
566 		if (valid_rate > 0) {
567 			/* Use the sum of bst.rate */
568 			bat[i]->min = 60 * bat[i]->bst.cap / valid_rate;
569 		} else if (bat[i]->full_charge_time > 0) {
570 			bat[i]->min = (bat[i]->full_charge_time * bat[i]->cap) / 100;
571 		} else {
572 			/* Couldn't find valid rate and full battery time */
573 			bat[i]->min = 0;
574 		}
575 		total_min += bat[i]->min;
576 		total_cap += bat[i]->cap;
577 		total_full += bat[i]->full_charge_time;
578 	}
579 
580 	/* Battery life */
581 	if (valid_units == 0) {
582 		cap = -1;
583 		batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
584 	} else {
585 		cap = total_cap / valid_units;
586 	}
587 
588 	/* Battery time */
589 	if (valid_units == 0) {
590 		min = -1;
591 	} else if (valid_rate == 0 || (batt_stat & ACPI_BATT_STAT_CHARGING)) {
592 		if (total_full == 0) {
593 			min = -1;
594 		} else {
595 			min = (total_full * cap) / 100;
596 		}
597 	} else {
598 		min = total_min;
599 	}
600 
601 	acpi_cmbat_info_updated(&acpi_cmbat_info_lastupdated);
602 out:
603 	battinfo->cap = cap;
604 	battinfo->min = min;
605 	battinfo->state = batt_stat;
606 
607 	return (error);
608 }
609 
610 static void
611 acpi_cmbat_init_battery(void *arg)
612 {
613 	int		retry;
614 	device_t	dev = (device_t)arg;
615 	struct acpi_cmbat_softc *sc = device_get_softc(dev);
616 #define ACPI_CMBAT_RETRY_MAX	6
617 
618 	if (sc->initializing) {
619 		return;
620 	}
621 
622 	sc->initializing = 1;
623 
624 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
625 		    "battery initialization start\n");
626 
627 	for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10, 0)) {
628 		sc->present = acpi_BatteryIsPresent(dev);
629 		if (!sc->present) {
630 			continue;
631 		}
632 
633 		timespecclear(&sc->bst_lastupdated);
634 		timespecclear(&sc->bif_lastupdated);
635 
636 		acpi_cmbat_get_bst(dev);
637 
638 		if (!acpi_cmbat_is_bst_valid(&sc->bst)) {
639 			continue;
640 		}
641 
642 		acpi_cmbat_get_bif(dev);
643 
644 		if (!acpi_cmbat_is_bif_valid(&sc->bif)) {
645 			continue;
646 		}
647 
648 		break;
649 	}
650 
651 	if (retry == ACPI_CMBAT_RETRY_MAX)
652 		ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
653 			    "battery initialization failed, giving up\n");
654 	else
655 		ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
656 			    "battery initialization done, tried %d times\n",
657 			    retry+1);
658 
659 	sc->initializing = 0;
660 }
661 
662 /*
663  * Public interfaces.
664  */
665 
666 int
667 acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo)
668 {
669 	int		error;
670 	device_t	dev;
671 	struct acpi_cmbat_softc *sc;
672 
673 	if (unit == -1) {
674 		return (acpi_cmbat_get_total_battinfo(battinfo));
675 	}
676 
677 	if (acpi_cmbat_info_expired(&acpi_cmbat_info_lastupdated)) {
678 		error = acpi_cmbat_get_total_battinfo(battinfo);
679 		if (error) {
680 			goto out;
681 		}
682 	}
683 
684 	error = 0;
685 	if (unit >= acpi_cmbat_units) {
686 		error = ENXIO;
687 		goto out;
688 	}
689 
690 	if ((dev = devclass_get_device(acpi_cmbat_devclass, unit)) == NULL) {
691 		error = ENXIO;
692 		goto out;
693 	}
694 
695 	if ((sc = device_get_softc(dev)) == NULL) {
696 		error = ENXIO;
697 		goto out;
698 	}
699 
700 	if (!sc->present) {
701 		battinfo->cap = -1;
702 		battinfo->min = -1;
703 		battinfo->state = ACPI_BATT_STAT_NOT_PRESENT;
704 	} else {
705 		battinfo->cap = sc->cap;
706 		battinfo->min = sc->min;
707 		battinfo->state = sc->bst.state;
708 	}
709 out:
710 	return (error);
711 }
712 
713