Rename Makefile.inc to Makefile.sub
[dragonfly.git] / sys / dev / acpica / acpi_cmbat.c
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.1 2003/09/24 03:32:16 drhodus 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_NOWAIT);
501                 if (bat == NULL) {
502                         error = ENOMEM;
503                         goto out;
504                 }
505
506                 /* Collect softc pointers */
507                 for (i = 0; i < acpi_cmbat_units; i++) {
508                         if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) {
509                                 error = ENXIO;
510                                 goto out;
511                         }
512
513                         if ((sc = device_get_softc(dev)) == NULL) {
514                                 error = ENXIO;
515                                 goto out;
516                         }
517
518                         bat[i] = sc;
519                 }
520         }
521
522         /* Get battery status, valid rate and valid units */
523         batt_stat = valid_rate = valid_units = 0;
524         for (i = 0; i < acpi_cmbat_units; i++) {
525                 bat[i]->present = acpi_BatteryIsPresent(bat[i]->dev);
526                 if (!bat[i]->present)
527                         continue;
528
529                 acpi_cmbat_get_bst(bat[i]->dev);
530
531                 /* If battey not installed, we get strange values */
532                 if (!acpi_cmbat_is_bst_valid(&(bat[i]->bst)) ||
533                     !acpi_cmbat_is_bif_valid(&(bat[i]->bif))) {
534                         bat[i]->present = 0;
535                         continue;
536                 }
537
538                 valid_units++;
539
540                 bat[i]->cap = 100 * bat[i]->bst.cap / bat[i]->bif.lfcap;
541
542                 batt_stat |= bat[i]->bst.state;
543
544                 if (bat[i]->bst.rate > 0) {
545                         /*
546                          * XXX Hack to calculate total battery time.
547                          * Systems with 2 or more battries, they may get used
548                          * one by one, thus bst.rate is set only to the one
549                          * in use. For remaining batteries bst.rate = 0, which
550                          * makes it impossible to calculate remaining time.
551                          * Some other systems may need sum of bst.rate in
552                          * dis-charging state.
553                          * There for we sum up the bst.rate that is valid
554                          * (in dis-charging state), and use the sum to
555                          * calcutate remaining batteries' time.
556                          */
557                         if (bat[i]->bst.state & ACPI_BATT_STAT_DISCHARG) {
558                                 valid_rate += bat[i]->bst.rate;
559                         }
560                 }
561         }
562
563         /* Calculate total battery capacity and time */
564         total_cap = total_min = total_full = 0;
565         for (i = 0; i < acpi_cmbat_units; i++) {
566                 if (!bat[i]->present) {
567                         continue;
568                 }
569
570                 if (valid_rate > 0) {
571                         /* Use the sum of bst.rate */
572                         bat[i]->min = 60 * bat[i]->bst.cap / valid_rate;
573                 } else if (bat[i]->full_charge_time > 0) {
574                         bat[i]->min = (bat[i]->full_charge_time * bat[i]->cap) / 100;
575                 } else {
576                         /* Couldn't find valid rate and full battery time */
577                         bat[i]->min = 0;
578                 }
579                 total_min += bat[i]->min;
580                 total_cap += bat[i]->cap;
581                 total_full += bat[i]->full_charge_time;
582         }
583
584         /* Battery life */
585         if (valid_units == 0) {
586                 cap = -1;
587                 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
588         } else {
589                 cap = total_cap / valid_units;
590         }
591
592         /* Battery time */
593         if (valid_units == 0) {
594                 min = -1;
595         } else if (valid_rate == 0 || (batt_stat & ACPI_BATT_STAT_CHARGING)) {
596                 if (total_full == 0) {
597                         min = -1;
598                 } else {
599                         min = (total_full * cap) / 100;
600                 }
601         } else {
602                 min = total_min;
603         }
604
605         acpi_cmbat_info_updated(&acpi_cmbat_info_lastupdated);
606 out:
607         battinfo->cap = cap;
608         battinfo->min = min;
609         battinfo->state = batt_stat;
610
611         return (error);
612 }
613
614 static void
615 acpi_cmbat_init_battery(void *arg)
616 {
617         int             retry;
618         device_t        dev = (device_t)arg;
619         struct acpi_cmbat_softc *sc = device_get_softc(dev);
620 #define ACPI_CMBAT_RETRY_MAX    6
621
622         if (sc->initializing) {
623                 return;
624         }
625
626         sc->initializing = 1;
627
628         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
629                     "battery initialization start\n");
630
631         for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10, 0)) {
632                 sc->present = acpi_BatteryIsPresent(dev);
633                 if (!sc->present) {
634                         continue;
635                 }
636
637                 timespecclear(&sc->bst_lastupdated);
638                 timespecclear(&sc->bif_lastupdated);
639
640                 acpi_cmbat_get_bst(dev);
641
642                 if (!acpi_cmbat_is_bst_valid(&sc->bst)) {
643                         continue;
644                 }
645
646                 acpi_cmbat_get_bif(dev);
647
648                 if (!acpi_cmbat_is_bif_valid(&sc->bif)) {
649                         continue;
650                 }
651
652                 break;
653         }
654
655         if (retry == ACPI_CMBAT_RETRY_MAX)
656                 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
657                             "battery initialization failed, giving up\n");
658         else
659                 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
660                             "battery initialization done, tried %d times\n",
661                             retry+1);
662
663         sc->initializing = 0;
664 }
665
666 /*
667  * Public interfaces.
668  */
669
670 int
671 acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo)
672 {
673         int             error;
674         device_t        dev;
675         struct acpi_cmbat_softc *sc;
676
677         if (unit == -1) {
678                 return (acpi_cmbat_get_total_battinfo(battinfo));
679         }
680
681         if (acpi_cmbat_info_expired(&acpi_cmbat_info_lastupdated)) {
682                 error = acpi_cmbat_get_total_battinfo(battinfo);
683                 if (error) {
684                         goto out;
685                 }
686         }
687
688         error = 0;
689         if (unit >= acpi_cmbat_units) {
690                 error = ENXIO;
691                 goto out;
692         }
693
694         if ((dev = devclass_get_device(acpi_cmbat_devclass, unit)) == NULL) {
695                 error = ENXIO;
696                 goto out;
697         }
698
699         if ((sc = device_get_softc(dev)) == NULL) {
700                 error = ENXIO;
701                 goto out;
702         }
703
704         if (!sc->present) {
705                 battinfo->cap = -1;
706                 battinfo->min = -1;
707                 battinfo->state = ACPI_BATT_STAT_NOT_PRESENT;
708         } else {
709                 battinfo->cap = sc->cap;
710                 battinfo->min = sc->min;
711                 battinfo->state = sc->bst.state;
712         }
713 out:
714         return (error);
715 }
716