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