summaryrefslogtreecommitdiffstats
path: root/sys/dev/acpica/acpi_cmbat.c
blob: 8bcf2869ce05b11811fc03122acd6e421dc3192b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*-
 * Copyright (c) 2000 Munehiro Matsuda
 * Copyright (c) 2000 Takanori Watanabe
 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include "opt_acpi.h"
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/ioccom.h>

#include <machine/bus.h>
#include <sys/rman.h>
#include <sys/malloc.h>

#include "acpi.h"
#include <dev/acpica/acpivar.h>
#include <dev/acpica/acpiio.h>

MALLOC_DEFINE(M_ACPICMBAT, "acpicmbat", "ACPI control method battery data");

/* Number of times to retry initialization before giving up. */
#define ACPI_CMBAT_RETRY_MAX	6

/* Check the battery once a minute. */
#define	CMBAT_POLLRATE	(60 * hz)

/* Hooks for the ACPI CA debugging infrastructure */
#define	_COMPONENT	ACPI_BATTERY
ACPI_MODULE_NAME("BATTERY")

#define	ACPI_BATTERY_BST_CHANGE 0x80
#define	ACPI_BATTERY_BIF_CHANGE 0x81

struct acpi_cmbat_softc {
    device_t	    dev;

    struct acpi_bif bif;
    struct acpi_bst bst;
    struct timespec bif_lastupdated;
    struct timespec bst_lastupdated;
    int		    bif_updating;
    int		    bst_updating;

    int		    present;
    int		    cap;
    int		    min;
    int		    full_charge_time;
    int		    initializing;
};

static struct timespec	acpi_cmbat_info_lastupdated;

/* XXX: devclass_get_maxunit() don't give us the current allocated units. */
static int		acpi_cmbat_units = 0;

static int		acpi_cmbat_info_expired(struct timespec *);
static void		acpi_cmbat_info_updated(struct timespec *);
static void		acpi_cmbat_get_bst(void *);
static void		acpi_cmbat_get_bif(void *);
static void		acpi_cmbat_notify_handler(ACPI_HANDLE, UINT32, void *);
static int		acpi_cmbat_probe(device_t);
static int		acpi_cmbat_attach(device_t);
static int		acpi_cmbat_resume(device_t);
static int		acpi_cmbat_ioctl(u_long, caddr_t, void *);
static int		acpi_cmbat_is_bst_valid(struct acpi_bst*);
static int		acpi_cmbat_is_bif_valid(struct acpi_bif*);
static int		acpi_cmbat_get_total_battinfo(struct acpi_battinfo *);
static void		acpi_cmbat_init_battery(void *);

static device_method_t acpi_cmbat_methods[] = {
    /* Device interface */
    DEVMETHOD(device_probe,	acpi_cmbat_probe),
    DEVMETHOD(device_attach,	acpi_cmbat_attach),
    DEVMETHOD(device_resume,	acpi_cmbat_resume),

    {0, 0}
};

static driver_t acpi_cmbat_driver = {
    "acpi_cmbat",
    acpi_cmbat_methods,
    sizeof(struct acpi_cmbat_softc),
};

static devclass_t acpi_cmbat_devclass;
DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);

static int
acpi_cmbat_info_expired(struct timespec *lastupdated)
{
    struct timespec	curtime;

    if (lastupdated == NULL)
	return (1);
    if (!timespecisset(lastupdated))
	return (1);

    getnanotime(&curtime);
    timespecsub(&curtime, lastupdated);
    return (curtime.tv_sec < 0 ||
	    curtime.tv_sec > acpi_battery_get_info_expire());
}


static void
acpi_cmbat_info_updated(struct timespec *lastupdated)
{
    if (lastupdated != NULL)
	getnanotime(lastupdated);
}

static void
acpi_cmbat_get_bst(void *context)
{
    device_t	dev;
    struct acpi_cmbat_softc *sc;
    ACPI_STATUS	as;
    ACPI_OBJECT	*res;
    ACPI_HANDLE	h;
    ACPI_BUFFER	bst_buffer;

    dev = context;
    sc = device_get_softc(dev);
    h = acpi_get_handle(dev);

    if (!acpi_cmbat_info_expired(&sc->bst_lastupdated))
	return;
    if (sc->bst_updating)
	return;
    sc->bst_updating = 1;

    bst_buffer.Pointer = NULL;
    bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
    as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer);
    if (ACPI_FAILURE(as)) {
	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
		    "error fetching current battery status -- %s\n",
		    AcpiFormatException(as));
	goto end;
    }

    res = (ACPI_OBJECT *)bst_buffer.Pointer;
    if (!ACPI_PKG_VALID(res, 4)) {
	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
		    "battery status corrupted\n");
	goto end;
    }

    if (acpi_PkgInt32(res, 0, &sc->bst.state) != 0)
	goto end;
    if (acpi_PkgInt32(res, 1, &sc->bst.rate) != 0)
	goto end;
    if (acpi_PkgInt32(res, 2, &sc->bst.cap) != 0)
	goto end;
    if (acpi_PkgInt32(res, 3, &sc->bst.volt) != 0)
	goto end;
    acpi_cmbat_info_updated(&sc->bst_lastupdated);

end:
    if (bst_buffer.Pointer != NULL)
	AcpiOsFree(bst_buffer.Pointer);
    sc->bst_updating = 0;
}

static void
acpi_cmbat_get_bif(void *context)
{
    device_t	dev;
    struct acpi_cmbat_softc *sc;
    ACPI_STATUS	as;
    ACPI_OBJECT	*res;
    ACPI_HANDLE	h;
    ACPI_BUFFER	bif_buffer;

    dev = context;
    sc = device_get_softc(dev);
    h = acpi_get_handle(dev);

    if (!acpi_cmbat_info_expired(&sc->bif_lastupdated))
	return;
    if (sc->bif_updating)
	return;
    sc->bif_updating = 1;

    bif_buffer.Pointer = NULL;
    bif_buffer.Length = ACPI_ALLOCATE_BUFFER;
    as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer);
    if (ACPI_FAILURE(as)) {
	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
		    "error fetching current battery info -- %s\n",
		    AcpiFormatException(as));
	goto end;
    }

    res = (ACPI_OBJECT *)bif_buffer.Pointer;
    if (!ACPI_PKG_VALID(res, 13)) {
	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
		    "battery info corrupted\n");
	goto end;
    }

    if (acpi_PkgInt32(res,  0, &sc->bif.units) != 0)
	goto end;
    if (acpi_PkgInt32(res,  1, &sc->bif.dcap) != 0)
	goto end;
    if (acpi_PkgInt32(res,  2, &sc->bif.lfcap) != 0)
	goto end;
    if (acpi_PkgInt32(res,  3, &sc->bif.btech) != 0)
	goto end;
    if (acpi_PkgInt32(res,  4, &sc->bif.dvol) != 0)
	goto end;
    if (acpi_PkgInt32(res,  5, &sc->bif.wcap) != 0)
	goto end;
    if (acpi_PkgInt32(res,  6, &sc->bif.lcap) != 0)
	goto end;
    if (acpi_PkgInt32(res,  7, &sc->bif.gra1) != 0)
	goto end;
    if (acpi_PkgInt32(res,  8, &sc->bif.gra2) != 0)
	goto end;
    if (acpi_PkgStr(res,  9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN) != 0)
	goto end;
    if (acpi_PkgStr(res, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN) != 0)
	goto end;
    if (acpi_PkgStr(res, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN) != 0)
	goto end;
    if (acpi_PkgStr(res, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN) != 0)
	goto end;
    acpi_cmbat_info_updated(&sc->bif_lastupdated);

end:
    if (bif_buffer.Pointer != NULL)
	AcpiOsFree(bif_buffer.Pointer);
    sc->bif_updating = 0;
}

static void
acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
{
    device_t dev;
    struct acpi_cmbat_softc	*sc;

    dev = (device_t)context;
    if ((sc = device_get_softc(dev)) == NULL)
	return;

    acpi_UserNotify("CMBAT", h, notify);

    switch (notify) {
    case ACPI_NOTIFY_DEVICE_CHECK:
    case ACPI_BATTERY_BST_CHANGE:
	timespecclear(&sc->bst_lastupdated);
	break;
    case ACPI_NOTIFY_BUS_CHECK:
    case ACPI_BATTERY_BIF_CHANGE:
	timespecclear(&sc->bif_lastupdated);
	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
	break;
    default:
	break;
    }
}

static int
acpi_cmbat_probe(device_t dev)
{
    if (acpi_get_type(dev) == ACPI_TYPE_DEVICE &&
	!acpi_disabled("cmbat") && acpi_MatchHid(dev, "PNP0C0A")) {

	device_set_desc(dev, "Control Method Battery");
	return (0);
    }
    return (ENXIO);
}

static int
acpi_cmbat_attach(device_t dev)
{
    int		error;
    ACPI_HANDLE	handle;
    struct acpi_cmbat_softc *sc;

    if ((sc = device_get_softc(dev)) == NULL)
	return (ENXIO);

    handle = acpi_get_handle(dev);

    /*
     * Install a system notify handler in addition to the device notify.
     * Toshiba notebook uses this alternate notify for its battery.
     */
    AcpiInstallNotifyHandler(handle, ACPI_SYSTEM_NOTIFY,
			     acpi_cmbat_notify_handler, dev);
    AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
			     acpi_cmbat_notify_handler, dev);

    sc->bif_updating = sc->bst_updating = 0;
    sc->dev = dev;

    timespecclear(&sc->bif_lastupdated);
    timespecclear(&sc->bst_lastupdated);

    if (acpi_cmbat_units == 0) {
	error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BIF,
				    acpi_cmbat_ioctl, NULL);
	if (error != 0)
	    return (error);
	error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BST,
				    acpi_cmbat_ioctl, NULL);
	if (error != 0)
		return (error);
    }

    error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT, acpi_cmbat_units);
    if (error != 0)
	return (error);

    acpi_cmbat_units++;
    timespecclear(&acpi_cmbat_info_lastupdated);
    sc->initializing = 0;
    AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_init_battery, dev);

    return (0);
}

static int
acpi_cmbat_resume(device_t dev)
{
    AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_init_battery, dev);
    return (0);
}

static int
acpi_cmbat_ioctl(u_long cmd, caddr_t addr, void *arg)
{
    device_t	dev;
    union acpi_battery_ioctl_arg *ioctl_arg;
    struct acpi_cmbat_softc *sc;
    struct acpi_bif	*bifp;
    struct acpi_bst	*bstp;

    ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
    dev = devclass_get_device(acpi_cmbat_devclass, ioctl_arg->unit);
    if (dev == NULL)
	return (ENXIO);
    sc = device_get_softc(dev);
    if (sc == NULL)
	return (ENXIO);

    /*
     * No security check required: information retrieval only.  If
     * new functions are added here, a check might be required.
     */
    switch (cmd) {
    case ACPIIO_CMBAT_GET_BIF:
	acpi_cmbat_get_bif(dev);
	bifp = &ioctl_arg->bif;
	bifp->units = sc->bif.units;
	bifp->dcap = sc->bif.dcap;
	bifp->lfcap = sc->bif.lfcap;
	bifp->btech = sc->bif.btech;
	bifp->dvol = sc->bif.dvol;
	bifp->wcap = sc->bif.wcap;
	bifp->lcap = sc->bif.lcap;
	bifp->gra1 = sc->bif.gra1;
	bifp->gra2 = sc->bif.gra2;
	strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
	strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
	strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
	strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
	break;
    case ACPIIO_CMBAT_GET_BST:
	bstp = &ioctl_arg->bst;
	if (acpi_BatteryIsPresent(dev)) {
	    acpi_cmbat_get_bst(dev);
	    bstp->state = sc->bst.state;
	    bstp->rate = sc->bst.rate;
	    bstp->cap = sc->bst.cap;
	    bstp->volt = sc->bst.volt;
	} else {
	    bstp->state = ACPI_BATT_STAT_NOT_PRESENT;
	}
	break;
    default:
	break;
    }

    return (0);
}

static int
acpi_cmbat_is_bst_valid(struct acpi_bst *bst)
{
    if (bst->state >= ACPI_BATT_STAT_MAX || bst->cap == 0xffffffff ||
	bst->volt == 0xffffffff)

	return (0);
    else
	return (1);
}

static int
acpi_cmbat_is_bif_valid(struct acpi_bif *bif)
{
    if (bif->lfcap == 0)
	return (0);
    else
	return (1);
}

static int
acpi_cmbat_get_total_battinfo(struct acpi_battinfo *battinfo)
{
    int		i;
    int		error;
    int		batt_stat;
    int		valid_rate, valid_units;
    int		cap, min;
    int		total_cap, total_min, total_full;
    device_t	dev;
    struct acpi_cmbat_softc *sc;
    static int	bat_units = 0;
    static struct acpi_cmbat_softc **bat = NULL;

    cap = min = -1;
    batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
    error = 0;

    /* Allocate array of softc pointers */
    if (bat_units != acpi_cmbat_units) {
	if (bat != NULL) {
	    free(bat, M_ACPICMBAT);
	    bat = NULL;
	}
	bat_units = 0;
    }
    if (bat == NULL) {
	bat_units = acpi_cmbat_units;
	bat = malloc(sizeof(struct acpi_cmbat_softc *) * bat_units,
		     M_ACPICMBAT, M_NOWAIT);
	if (bat == NULL) {
	    error = ENOMEM;
	    goto out;
	}

	/* Collect softc pointers */
	for (i = 0; i < acpi_cmbat_units; i++) {
	    if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) {
		error = ENXIO;
		goto out;
	    }
	    if ((sc = device_get_softc(dev)) == NULL) {
		error = ENXIO;
		goto out;
	    }
	    bat[i] = sc;
	}
    }

    /* Get battery status, valid rate and valid units */
    batt_stat = valid_rate = valid_units = 0;
    for (i = 0; i < acpi_cmbat_units; i++) {
	bat[i]->present = acpi_BatteryIsPresent(bat[i]->dev);
	if (!bat[i]->present)
	    continue;

	acpi_cmbat_get_bst(bat[i]->dev);

	/* If battery not installed, we get strange values */
	if (!acpi_cmbat_is_bst_valid(&(bat[i]->bst)) ||
	    !acpi_cmbat_is_bif_valid(&(bat[i]->bif))) {

	    bat[i]->present = 0;
	    continue;
	}

	valid_units++;
	bat[i]->cap = 100 * bat[i]->bst.cap / bat[i]->bif.lfcap;
	batt_stat |= bat[i]->bst.state;

	if (bat[i]->bst.rate > 0) {
	    /*
	     * XXX Hack to calculate total battery time.
	     * Systems with 2 or more battries, they may get used
	     * one by one, thus bst.rate is set only to the one
	     * in use. For remaining batteries bst.rate = 0, which
	     * makes it impossible to calculate remaining time.
	     * Some other systems may need sum of bst.rate in
	     * dis-charging state.
	     * There for we sum up the bst.rate that is valid
	     * (in dis-charging state), and use the sum to
	     * calcutate remaining batteries' time.
	     */
	    if (bat[i]->bst.state & ACPI_BATT_STAT_DISCHARG)
		valid_rate += bat[i]->bst.rate;
	}
    }

    /* Calculate total battery capacity and time */
    total_cap = total_min = total_full = 0;
    for (i = 0; i < acpi_cmbat_units; i++) {
	if (!bat[i]->present)
	    continue;

	if (valid_rate > 0) {
	    /* Use the sum of bst.rate */
	    bat[i]->min = 60 * bat[i]->bst.cap / valid_rate;
	} else if (bat[i]->full_charge_time > 0) {
	    bat[i]->min = (bat[i]->full_charge_time * bat[i]->cap) / 100;
	} else {
	    /* Couldn't find valid rate and full battery time */
	    bat[i]->min = 0;
	}
	total_min += bat[i]->min;
	total_cap += bat[i]->cap;
	total_full += bat[i]->full_charge_time;
    }

    /* Battery life */
    if (valid_units == 0) {
	cap = -1;
	batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
    } else {
	cap = total_cap / valid_units;
    }

    /* Battery time */
    if (valid_units == 0) {
	min = -1;
    } else if (valid_rate == 0 || (batt_stat & ACPI_BATT_STAT_CHARGING)) {
	if (total_full == 0)
	    min = -1;
	else
	    min = (total_full * cap) / 100;
    } else {
	min = total_min;
    }
    acpi_cmbat_info_updated(&acpi_cmbat_info_lastupdated);

out:
    battinfo->cap = cap;
    battinfo->min = min;
    battinfo->state = batt_stat;

    return (error);
}

static void
acpi_cmbat_init_battery(void *arg)
{
    int		retry;
    device_t	dev = (device_t)arg;
    struct acpi_cmbat_softc *sc = device_get_softc(dev);

    if (sc->initializing)
	return;

    sc->initializing = 1;
    ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
		"battery initialization start\n");

    for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10, 0)) {
	sc->present = acpi_BatteryIsPresent(dev);
	if (!sc->present)
	    continue;

	timespecclear(&sc->bst_lastupdated);
	timespecclear(&sc->bif_lastupdated);

	acpi_cmbat_get_bst(dev);
	if (!acpi_cmbat_is_bst_valid(&sc->bst))
	    continue;

	acpi_cmbat_get_bif(dev);
	if (!acpi_cmbat_is_bif_valid(&sc->bif))
	    continue;
	break;
    }

    sc->initializing = 0;
    if (retry == ACPI_CMBAT_RETRY_MAX) {
	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
		    "battery initialization failed, giving up\n");
    } else {
	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
		    "battery initialization done, tried %d times\n", retry + 1);
    }
}

/*
 * Public interfaces.
 */
int
acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo)
{
    int		error;
    device_t	dev;
    struct acpi_cmbat_softc *sc;

    if (unit == -1)
	return (acpi_cmbat_get_total_battinfo(battinfo));

    if (acpi_cmbat_info_expired(&acpi_cmbat_info_lastupdated)) {
	error = acpi_cmbat_get_total_battinfo(battinfo);
	if (error)
	    goto out;
    }

    error = 0;
    if (unit >= acpi_cmbat_units) {
	error = ENXIO;
	goto out;
    }

    if ((dev = devclass_get_device(acpi_cmbat_devclass, unit)) == NULL) {
	error = ENXIO;
	goto out;
    }
    if ((sc = device_get_softc(dev)) == NULL) {
	error = ENXIO;
	goto out;
    }

    if (!sc->present) {
	battinfo->cap = -1;
	battinfo->min = -1;
	battinfo->state = ACPI_BATT_STAT_NOT_PRESENT;
    } else {
	battinfo->cap = sc->cap;
	battinfo->min = sc->min;
	battinfo->state = sc->bst.state;
    }

out:
    return (error);
}
OpenPOWER on IntegriCloud