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