Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / dev / acpica5 / 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.29 2004/05/30 20:08:23 phk Exp $
29  * $DragonFly: src/sys/dev/acpica5/acpi_cmbat.c,v 1.4 2004/07/05 00:07:35 dillon Exp $
30  */
31
32 #include "opt_acpi.h"
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/ioccom.h>
38
39 #include <machine/bus.h>
40 #include <sys/rman.h>
41 #include <sys/malloc.h>
42
43 #include "acpi.h"
44 #include <dev/acpica5/acpivar.h>
45 #include <dev/acpica5/acpiio.h>
46
47 MALLOC_DEFINE(M_ACPICMBAT, "acpicmbat", "ACPI control method battery data");
48
49 /* Number of times to retry initialization before giving up. */
50 #define ACPI_CMBAT_RETRY_MAX    6
51
52 /* Check the battery once a minute. */
53 #define CMBAT_POLLRATE  (60 * hz)
54
55 /* Hooks for the ACPI CA debugging infrastructure */
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 struct acpi_cmbat_softc {
63     device_t        dev;
64
65     struct acpi_bif bif;
66     struct acpi_bst bst;
67     struct timespec bif_lastupdated;
68     struct timespec bst_lastupdated;
69     int             bif_updating;
70     int             bst_updating;
71
72     int             present;
73     int             cap;
74     int             min;
75     int             full_charge_time;
76     int             initializing;
77 };
78
79 static struct timespec  acpi_cmbat_info_lastupdated;
80
81 /* XXX: devclass_get_maxunit() don't give us the current allocated units. */
82 static int              acpi_cmbat_units = 0;
83
84 static int              acpi_cmbat_info_expired(struct timespec *);
85 static void             acpi_cmbat_info_updated(struct timespec *);
86 static void             acpi_cmbat_get_bst(void *);
87 static void             acpi_cmbat_get_bif(void *);
88 static void             acpi_cmbat_notify_handler(ACPI_HANDLE, UINT32, void *);
89 static int              acpi_cmbat_probe(device_t);
90 static int              acpi_cmbat_attach(device_t);
91 static int              acpi_cmbat_resume(device_t);
92 static int              acpi_cmbat_ioctl(u_long, caddr_t, void *);
93 static int              acpi_cmbat_is_bst_valid(struct acpi_bst*);
94 static int              acpi_cmbat_is_bif_valid(struct acpi_bif*);
95 static int              acpi_cmbat_get_total_battinfo(struct acpi_battinfo *);
96 static void             acpi_cmbat_init_battery(void *);
97
98 static device_method_t acpi_cmbat_methods[] = {
99     /* Device interface */
100     DEVMETHOD(device_probe,     acpi_cmbat_probe),
101     DEVMETHOD(device_attach,    acpi_cmbat_attach),
102     DEVMETHOD(device_resume,    acpi_cmbat_resume),
103
104     {0, 0}
105 };
106
107 static driver_t acpi_cmbat_driver = {
108     "acpi_cmbat",
109     acpi_cmbat_methods,
110     sizeof(struct acpi_cmbat_softc),
111 };
112
113 static devclass_t acpi_cmbat_devclass;
114 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);
115 MODULE_DEPEND(acpi_cmbat, acpi, 1, 1, 1);
116
117 static int
118 acpi_cmbat_info_expired(struct timespec *lastupdated)
119 {
120     struct timespec     curtime;
121
122     if (lastupdated == NULL)
123         return (1);
124     if (!timespecisset(lastupdated))
125         return (1);
126
127     getnanotime(&curtime);
128     timespecsub(&curtime, lastupdated);
129     return (curtime.tv_sec < 0 ||
130             curtime.tv_sec > acpi_battery_get_info_expire());
131 }
132
133
134 static void
135 acpi_cmbat_info_updated(struct timespec *lastupdated)
136 {
137     if (lastupdated != NULL)
138         getnanotime(lastupdated);
139 }
140
141 static void
142 acpi_cmbat_get_bst(void *context)
143 {
144     device_t    dev;
145     struct acpi_cmbat_softc *sc;
146     ACPI_STATUS as;
147     ACPI_OBJECT *res;
148     ACPI_HANDLE h;
149     ACPI_BUFFER bst_buffer;
150
151     dev = context;
152     sc = device_get_softc(dev);
153     h = acpi_get_handle(dev);
154
155     if (!acpi_cmbat_info_expired(&sc->bst_lastupdated))
156         return;
157     if (sc->bst_updating)
158         return;
159     sc->bst_updating = 1;
160
161     bst_buffer.Pointer = NULL;
162     bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
163     as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer);
164     if (ACPI_FAILURE(as)) {
165         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
166                     "error fetching current battery status -- %s\n",
167                     AcpiFormatException(as));
168         goto end;
169     }
170
171     res = (ACPI_OBJECT *)bst_buffer.Pointer;
172     if (!ACPI_PKG_VALID(res, 4)) {
173         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
174                     "battery status corrupted\n");
175         goto end;
176     }
177
178     if (acpi_PkgInt32(res, 0, &sc->bst.state) != 0)
179         goto end;
180     if (acpi_PkgInt32(res, 1, &sc->bst.rate) != 0)
181         goto end;
182     if (acpi_PkgInt32(res, 2, &sc->bst.cap) != 0)
183         goto end;
184     if (acpi_PkgInt32(res, 3, &sc->bst.volt) != 0)
185         goto end;
186     acpi_cmbat_info_updated(&sc->bst_lastupdated);
187
188 end:
189     if (bst_buffer.Pointer != NULL)
190         AcpiOsFree(bst_buffer.Pointer);
191     sc->bst_updating = 0;
192 }
193
194 static void
195 acpi_cmbat_get_bif(void *context)
196 {
197     device_t    dev;
198     struct acpi_cmbat_softc *sc;
199     ACPI_STATUS as;
200     ACPI_OBJECT *res;
201     ACPI_HANDLE h;
202     ACPI_BUFFER bif_buffer;
203
204     dev = context;
205     sc = device_get_softc(dev);
206     h = acpi_get_handle(dev);
207
208     if (!acpi_cmbat_info_expired(&sc->bif_lastupdated))
209         return;
210     if (sc->bif_updating)
211         return;
212     sc->bif_updating = 1;
213
214     bif_buffer.Pointer = NULL;
215     bif_buffer.Length = ACPI_ALLOCATE_BUFFER;
216     as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer);
217     if (ACPI_FAILURE(as)) {
218         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
219                     "error fetching current battery info -- %s\n",
220                     AcpiFormatException(as));
221         goto end;
222     }
223
224     res = (ACPI_OBJECT *)bif_buffer.Pointer;
225     if (!ACPI_PKG_VALID(res, 13)) {
226         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
227                     "battery info corrupted\n");
228         goto end;
229     }
230
231     if (acpi_PkgInt32(res,  0, &sc->bif.units) != 0)
232         goto end;
233     if (acpi_PkgInt32(res,  1, &sc->bif.dcap) != 0)
234         goto end;
235     if (acpi_PkgInt32(res,  2, &sc->bif.lfcap) != 0)
236         goto end;
237     if (acpi_PkgInt32(res,  3, &sc->bif.btech) != 0)
238         goto end;
239     if (acpi_PkgInt32(res,  4, &sc->bif.dvol) != 0)
240         goto end;
241     if (acpi_PkgInt32(res,  5, &sc->bif.wcap) != 0)
242         goto end;
243     if (acpi_PkgInt32(res,  6, &sc->bif.lcap) != 0)
244         goto end;
245     if (acpi_PkgInt32(res,  7, &sc->bif.gra1) != 0)
246         goto end;
247     if (acpi_PkgInt32(res,  8, &sc->bif.gra2) != 0)
248         goto end;
249     if (acpi_PkgStr(res,  9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN) != 0)
250         goto end;
251     if (acpi_PkgStr(res, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN) != 0)
252         goto end;
253     if (acpi_PkgStr(res, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN) != 0)
254         goto end;
255     if (acpi_PkgStr(res, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN) != 0)
256         goto end;
257     acpi_cmbat_info_updated(&sc->bif_lastupdated);
258
259 end:
260     if (bif_buffer.Pointer != NULL)
261         AcpiOsFree(bif_buffer.Pointer);
262     sc->bif_updating = 0;
263 }
264
265 static void
266 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
267 {
268     device_t dev;
269     struct acpi_cmbat_softc     *sc;
270
271     dev = (device_t)context;
272     if ((sc = device_get_softc(dev)) == NULL)
273         return;
274
275     acpi_UserNotify("CMBAT", h, notify);
276
277     switch (notify) {
278     case ACPI_NOTIFY_DEVICE_CHECK:
279     case ACPI_BATTERY_BST_CHANGE:
280         timespecclear(&sc->bst_lastupdated);
281         break;
282     case ACPI_NOTIFY_BUS_CHECK:
283     case ACPI_BATTERY_BIF_CHANGE:
284         timespecclear(&sc->bif_lastupdated);
285         AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
286         break;
287     default:
288         break;
289     }
290 }
291
292 static int
293 acpi_cmbat_probe(device_t dev)
294 {
295     if (acpi_get_type(dev) == ACPI_TYPE_DEVICE &&
296         !acpi_disabled("cmbat") && acpi_MatchHid(dev, "PNP0C0A")) {
297
298         device_set_desc(dev, "Control Method Battery");
299         return (0);
300     }
301     return (ENXIO);
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_INTWAIT);
469
470         /* Collect softc pointers */
471         for (i = 0; i < acpi_cmbat_units; i++) {
472             if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) {
473                 error = ENXIO;
474                 goto out;
475             }
476             if ((sc = device_get_softc(dev)) == NULL) {
477                 error = ENXIO;
478                 goto out;
479             }
480             bat[i] = sc;
481         }
482     }
483
484     /* Get battery status, valid rate and valid units */
485     batt_stat = valid_rate = valid_units = 0;
486     for (i = 0; i < acpi_cmbat_units; i++) {
487         bat[i]->present = acpi_BatteryIsPresent(bat[i]->dev);
488         if (!bat[i]->present)
489             continue;
490
491         acpi_cmbat_get_bst(bat[i]->dev);
492
493         /* If battery not installed, we get strange values */
494         if (!acpi_cmbat_is_bst_valid(&(bat[i]->bst)) ||
495             !acpi_cmbat_is_bif_valid(&(bat[i]->bif))) {
496
497             bat[i]->present = 0;
498             continue;
499         }
500
501         valid_units++;
502         bat[i]->cap = 100 * bat[i]->bst.cap / bat[i]->bif.lfcap;
503         batt_stat |= bat[i]->bst.state;
504
505         if (bat[i]->bst.rate > 0) {
506             /*
507              * XXX Hack to calculate total battery time.
508              * Systems with 2 or more battries, they may get used
509              * one by one, thus bst.rate is set only to the one
510              * in use. For remaining batteries bst.rate = 0, which
511              * makes it impossible to calculate remaining time.
512              * Some other systems may need sum of bst.rate in
513              * dis-charging state.
514              * There for we sum up the bst.rate that is valid
515              * (in dis-charging state), and use the sum to
516              * calcutate remaining batteries' time.
517              */
518             if (bat[i]->bst.state & ACPI_BATT_STAT_DISCHARG)
519                 valid_rate += bat[i]->bst.rate;
520         }
521     }
522
523     /* Calculate total battery capacity and time */
524     total_cap = total_min = total_full = 0;
525     for (i = 0; i < acpi_cmbat_units; i++) {
526         if (!bat[i]->present)
527             continue;
528
529         if (valid_rate > 0) {
530             /* Use the sum of bst.rate */
531             bat[i]->min = 60 * bat[i]->bst.cap / valid_rate;
532         } else if (bat[i]->full_charge_time > 0) {
533             bat[i]->min = (bat[i]->full_charge_time * bat[i]->cap) / 100;
534         } else {
535             /* Couldn't find valid rate and full battery time */
536             bat[i]->min = 0;
537         }
538         total_min += bat[i]->min;
539         total_cap += bat[i]->cap;
540         total_full += bat[i]->full_charge_time;
541     }
542
543     /* Battery life */
544     if (valid_units == 0) {
545         cap = -1;
546         batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
547     } else {
548         cap = total_cap / valid_units;
549     }
550
551     /* Battery time */
552     if (valid_units == 0) {
553         min = -1;
554     } else if (valid_rate == 0 || (batt_stat & ACPI_BATT_STAT_CHARGING)) {
555         if (total_full == 0)
556             min = -1;
557         else
558             min = (total_full * cap) / 100;
559     } else {
560         min = total_min;
561     }
562     acpi_cmbat_info_updated(&acpi_cmbat_info_lastupdated);
563
564 out:
565     battinfo->cap = cap;
566     battinfo->min = min;
567     battinfo->state = batt_stat;
568
569     return (error);
570 }
571
572 static void
573 acpi_cmbat_init_battery(void *arg)
574 {
575     int         retry;
576     device_t    dev = (device_t)arg;
577     struct acpi_cmbat_softc *sc = device_get_softc(dev);
578
579     if (sc->initializing)
580         return;
581
582     sc->initializing = 1;
583     ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
584                 "battery initialization start\n");
585
586     for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10, 0)) {
587         sc->present = acpi_BatteryIsPresent(dev);
588         if (!sc->present)
589             continue;
590
591         timespecclear(&sc->bst_lastupdated);
592         timespecclear(&sc->bif_lastupdated);
593
594         acpi_cmbat_get_bst(dev);
595         if (!acpi_cmbat_is_bst_valid(&sc->bst))
596             continue;
597
598         acpi_cmbat_get_bif(dev);
599         if (!acpi_cmbat_is_bif_valid(&sc->bif))
600             continue;
601         break;
602     }
603
604     sc->initializing = 0;
605     if (retry == ACPI_CMBAT_RETRY_MAX) {
606         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
607                     "battery initialization failed, giving up\n");
608     } else {
609         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
610                     "battery initialization done, tried %d times\n", retry + 1);
611     }
612 }
613
614 /*
615  * Public interfaces.
616  */
617 int
618 acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo)
619 {
620     int         error;
621     device_t    dev;
622     struct acpi_cmbat_softc *sc;
623
624     if (unit == -1)
625         return (acpi_cmbat_get_total_battinfo(battinfo));
626
627     if (acpi_cmbat_info_expired(&acpi_cmbat_info_lastupdated)) {
628         error = acpi_cmbat_get_total_battinfo(battinfo);
629         if (error)
630             goto out;
631     }
632
633     error = 0;
634     if (unit >= acpi_cmbat_units) {
635         error = ENXIO;
636         goto out;
637     }
638
639     if ((dev = devclass_get_device(acpi_cmbat_devclass, unit)) == NULL) {
640         error = ENXIO;
641         goto out;
642     }
643     if ((sc = device_get_softc(dev)) == NULL) {
644         error = ENXIO;
645         goto out;
646     }
647
648     if (!sc->present) {
649         battinfo->cap = -1;
650         battinfo->min = -1;
651         battinfo->state = ACPI_BATT_STAT_NOT_PRESENT;
652     } else {
653         battinfo->cap = sc->cap;
654         battinfo->min = sc->min;
655         battinfo->state = sc->bst.state;
656     }
657
658 out:
659     return (error);
660 }