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