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