2000-12-22 14:41:55 +00:00
|
|
|
/*-
|
2001-06-23 10:38:25 +00:00
|
|
|
* Copyright (c) 2000 Munehiro Matsuda
|
2000-12-22 14:41:55 +00:00
|
|
|
* 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 <sys/conf.h>
|
|
|
|
|
|
|
|
#include <machine/bus.h>
|
|
|
|
#include <machine/resource.h>
|
|
|
|
#include <sys/rman.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
|
|
|
|
#include "acpi.h"
|
|
|
|
|
|
|
|
#include <dev/acpica/acpivar.h>
|
|
|
|
#include <dev/acpica/acpiio.h>
|
|
|
|
|
2001-10-30 14:24:26 +00:00
|
|
|
MALLOC_DEFINE(M_ACPICMBAT, "acpicmbat", "ACPI control method battery data");
|
|
|
|
|
2001-10-30 15:51:52 +00:00
|
|
|
#define CMBAT_POLLRATE (60 * hz)
|
2001-10-22 18:01:37 +00:00
|
|
|
|
2001-05-29 20:13:42 +00:00
|
|
|
/*
|
|
|
|
* Hooks for the ACPI CA debugging infrastructure
|
|
|
|
*/
|
2001-10-30 15:51:52 +00:00
|
|
|
#define _COMPONENT ACPI_BATTERY
|
2002-02-23 05:24:14 +00:00
|
|
|
ACPI_MODULE_NAME("BATTERY")
|
2001-05-29 20:13:42 +00:00
|
|
|
|
2001-10-30 15:51:52 +00:00
|
|
|
#define ACPI_BATTERY_BST_CHANGE 0x80
|
|
|
|
#define ACPI_BATTERY_BIF_CHANGE 0x81
|
2000-12-24 19:12:10 +00:00
|
|
|
|
2001-10-30 15:51:52 +00:00
|
|
|
#define PKG_GETINT(res, tmp, idx, dest, label) do { \
|
2000-12-22 14:41:55 +00:00
|
|
|
tmp = &res->Package.Elements[idx]; \
|
2001-06-23 10:38:25 +00:00
|
|
|
if (tmp == NULL) { \
|
2001-11-18 18:12:07 +00:00
|
|
|
ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), \
|
2002-03-12 00:15:06 +00:00
|
|
|
"%s: PKG_GETINT error, idx = %d\n.", __func__, idx); \
|
2001-06-23 10:38:25 +00:00
|
|
|
goto label; \
|
|
|
|
} \
|
2001-01-31 09:30:57 +00:00
|
|
|
if (tmp->Type != ACPI_TYPE_INTEGER) \
|
2001-06-23 10:38:25 +00:00
|
|
|
goto label; \
|
2001-01-31 09:30:57 +00:00
|
|
|
dest = tmp->Integer.Value; \
|
2001-10-30 15:51:52 +00:00
|
|
|
} while (0)
|
2000-12-22 14:41:55 +00:00
|
|
|
|
2001-10-30 15:51:52 +00:00
|
|
|
#define PKG_GETSTR(res, tmp, idx, dest, size, label) do { \
|
2000-12-22 14:41:55 +00:00
|
|
|
size_t length; \
|
|
|
|
length = size; \
|
|
|
|
tmp = &res->Package.Elements[idx]; \
|
2001-06-23 10:38:25 +00:00
|
|
|
if (tmp == NULL) { \
|
2001-11-18 18:12:07 +00:00
|
|
|
ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), \
|
2002-03-12 00:15:06 +00:00
|
|
|
"%s: PKG_GETSTR error, idx = %d\n.", __func__, idx); \
|
2001-06-23 10:38:25 +00:00
|
|
|
goto label; \
|
|
|
|
} \
|
2000-12-22 14:41:55 +00:00
|
|
|
bzero(dest, sizeof(dest)); \
|
|
|
|
switch (tmp->Type) { \
|
|
|
|
case ACPI_TYPE_STRING: \
|
|
|
|
if (tmp->String.Length < length) { \
|
|
|
|
length = tmp->String.Length; \
|
|
|
|
} \
|
|
|
|
strncpy(dest, tmp->String.Pointer, length); \
|
|
|
|
break; \
|
|
|
|
case ACPI_TYPE_BUFFER: \
|
|
|
|
if (tmp->Buffer.Length < length) { \
|
2001-06-23 10:38:25 +00:00
|
|
|
length = tmp->Buffer.Length; \
|
2000-12-22 14:41:55 +00:00
|
|
|
} \
|
|
|
|
strncpy(dest, tmp->Buffer.Pointer, length); \
|
|
|
|
break; \
|
|
|
|
default: \
|
|
|
|
goto label; \
|
|
|
|
} \
|
2001-02-26 20:32:18 +00:00
|
|
|
dest[sizeof(dest)-1] = '\0'; \
|
2001-10-30 15:51:52 +00:00
|
|
|
} while (0)
|
2000-12-22 14:41:55 +00:00
|
|
|
|
2001-10-30 15:51:52 +00:00
|
|
|
struct acpi_cmbat_softc {
|
|
|
|
device_t dev;
|
|
|
|
|
|
|
|
struct acpi_bif bif;
|
|
|
|
struct acpi_bst bst;
|
|
|
|
struct timespec bif_lastupdated;
|
|
|
|
struct timespec bst_lastupdated;
|
2001-12-22 16:05:41 +00:00
|
|
|
int bif_updating;
|
|
|
|
int bst_updating;
|
2001-10-30 15:51:52 +00:00
|
|
|
|
2002-08-21 15:01:53 +00:00
|
|
|
int present;
|
2001-10-30 15:51:52 +00:00
|
|
|
int cap;
|
|
|
|
int min;
|
|
|
|
int full_charge_time;
|
2002-11-03 10:49:24 +00:00
|
|
|
int initializing;
|
2001-10-30 15:51:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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 *);
|
2002-11-03 10:49:24 +00:00
|
|
|
static int acpi_cmbat_is_bst_valid(struct acpi_bst*);
|
|
|
|
static int acpi_cmbat_is_bif_valid(struct acpi_bif*);
|
2001-10-30 15:51:52 +00:00
|
|
|
static int acpi_cmbat_get_total_battinfo(struct acpi_battinfo *);
|
2002-11-03 10:49:24 +00:00
|
|
|
static void acpi_cmbat_init_battery(void *);
|
2001-10-30 15:51:52 +00:00
|
|
|
|
2001-06-23 10:38:25 +00:00
|
|
|
static __inline int
|
|
|
|
acpi_cmbat_info_expired(struct timespec *lastupdated)
|
|
|
|
{
|
|
|
|
struct timespec curtime;
|
|
|
|
|
|
|
|
if (lastupdated == NULL) {
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2001-06-24 02:39:08 +00:00
|
|
|
if (!timespecisset(lastupdated)) {
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2001-06-23 10:38:25 +00:00
|
|
|
getnanotime(&curtime);
|
|
|
|
timespecsub(&curtime, lastupdated);
|
2001-10-30 14:24:26 +00:00
|
|
|
return ((curtime.tv_sec < 0 || curtime.tv_sec > acpi_battery_get_info_expire()));
|
2001-06-23 10:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
acpi_cmbat_info_updated(struct timespec *lastupdated)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (lastupdated != NULL) {
|
|
|
|
getnanotime(lastupdated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-22 14:41:55 +00:00
|
|
|
static void
|
|
|
|
acpi_cmbat_get_bst(void *context)
|
|
|
|
{
|
2001-10-30 15:51:52 +00:00
|
|
|
device_t dev;
|
|
|
|
struct acpi_cmbat_softc *sc;
|
2000-12-22 14:41:55 +00:00
|
|
|
ACPI_STATUS as;
|
|
|
|
ACPI_OBJECT *res, *tmp;
|
2001-10-30 15:51:52 +00:00
|
|
|
ACPI_HANDLE h;
|
2002-02-23 05:24:14 +00:00
|
|
|
ACPI_BUFFER bst_buffer;
|
2001-10-30 15:51:52 +00:00
|
|
|
|
|
|
|
dev = context;
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
h = acpi_get_handle(dev);
|
2002-02-23 05:24:14 +00:00
|
|
|
bst_buffer.Pointer = NULL;
|
2001-06-23 10:38:25 +00:00
|
|
|
|
|
|
|
if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-12-22 16:05:41 +00:00
|
|
|
if (sc->bst_updating) {
|
|
|
|
return;
|
2000-12-22 14:41:55 +00:00
|
|
|
}
|
2001-12-22 16:05:41 +00:00
|
|
|
sc->bst_updating = 1;
|
2000-12-22 14:41:55 +00:00
|
|
|
|
2002-02-23 05:24:14 +00:00
|
|
|
bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
|
|
|
|
if (ACPI_FAILURE(as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer))) {
|
2001-11-18 18:12:07 +00:00
|
|
|
ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
|
2001-12-22 16:05:41 +00:00
|
|
|
"error fetching current battery status -- %s\n",
|
|
|
|
AcpiFormatException(as));
|
2001-10-30 14:24:26 +00:00
|
|
|
goto end;
|
2000-12-22 14:41:55 +00:00
|
|
|
}
|
|
|
|
|
2002-02-23 05:24:14 +00:00
|
|
|
res = (ACPI_OBJECT *)bst_buffer.Pointer;
|
2000-12-22 14:41:55 +00:00
|
|
|
|
2002-07-23 00:47:13 +00:00
|
|
|
if ((res == NULL) || (res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 4)) {
|
2001-11-18 18:12:07 +00:00
|
|
|
ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
|
|
|
|
"battery status corrupted\n");
|
2001-10-30 14:24:26 +00:00
|
|
|
goto end;
|
2001-06-23 10:38:25 +00:00
|
|
|
}
|
2000-12-22 14:41:55 +00:00
|
|
|
|
|
|
|
PKG_GETINT(res, tmp, 0, sc->bst.state, end);
|
|
|
|
PKG_GETINT(res, tmp, 1, sc->bst.rate, end);
|
|
|
|
PKG_GETINT(res, tmp, 2, sc->bst.cap, end);
|
|
|
|
PKG_GETINT(res, tmp, 3, sc->bst.volt, end);
|
2001-06-23 10:38:25 +00:00
|
|
|
acpi_cmbat_info_updated(&sc->bst_lastupdated);
|
2000-12-22 14:41:55 +00:00
|
|
|
end:
|
2002-02-23 05:24:14 +00:00
|
|
|
if (bst_buffer.Pointer != NULL)
|
|
|
|
AcpiOsFree(bst_buffer.Pointer);
|
2001-12-22 16:05:41 +00:00
|
|
|
sc->bst_updating = 0;
|
2000-12-22 14:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
acpi_cmbat_get_bif(void *context)
|
|
|
|
{
|
2001-10-30 15:51:52 +00:00
|
|
|
device_t dev;
|
|
|
|
struct acpi_cmbat_softc *sc;
|
2000-12-22 14:41:55 +00:00
|
|
|
ACPI_STATUS as;
|
|
|
|
ACPI_OBJECT *res, *tmp;
|
2001-10-30 15:51:52 +00:00
|
|
|
ACPI_HANDLE h;
|
2002-02-23 05:24:14 +00:00
|
|
|
ACPI_BUFFER bif_buffer;
|
2001-10-30 15:51:52 +00:00
|
|
|
|
|
|
|
dev = context;
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
h = acpi_get_handle(dev);
|
2002-02-23 05:24:14 +00:00
|
|
|
bif_buffer.Pointer = NULL;
|
2000-12-22 14:41:55 +00:00
|
|
|
|
2001-06-23 10:38:25 +00:00
|
|
|
if (!acpi_cmbat_info_expired(&sc->bif_lastupdated)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-12-22 16:05:41 +00:00
|
|
|
if (sc->bif_updating) {
|
|
|
|
return;
|
2000-12-22 14:41:55 +00:00
|
|
|
}
|
2001-12-22 16:05:41 +00:00
|
|
|
sc->bif_updating = 1;
|
2000-12-22 14:41:55 +00:00
|
|
|
|
2002-02-23 05:24:14 +00:00
|
|
|
bif_buffer.Length = ACPI_ALLOCATE_BUFFER;
|
|
|
|
if (ACPI_FAILURE(as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer))) {
|
2001-11-18 18:12:07 +00:00
|
|
|
ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
|
2001-12-22 16:05:41 +00:00
|
|
|
"error fetching current battery info -- %s\n",
|
|
|
|
AcpiFormatException(as));
|
2001-10-22 18:01:37 +00:00
|
|
|
goto end;
|
2000-12-22 14:41:55 +00:00
|
|
|
}
|
|
|
|
|
2002-02-23 05:24:14 +00:00
|
|
|
res = (ACPI_OBJECT *)bif_buffer.Pointer;
|
2001-06-23 10:38:25 +00:00
|
|
|
|
2002-07-23 00:47:13 +00:00
|
|
|
if ((res == NULL) || (res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 13)) {
|
2001-11-18 18:12:07 +00:00
|
|
|
ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
|
|
|
|
"battery info corrupted\n");
|
2001-10-22 18:01:37 +00:00
|
|
|
goto end;
|
2001-06-23 10:38:25 +00:00
|
|
|
}
|
2000-12-22 14:41:55 +00:00
|
|
|
|
|
|
|
PKG_GETINT(res, tmp, 0, sc->bif.unit, end);
|
|
|
|
PKG_GETINT(res, tmp, 1, sc->bif.dcap, end);
|
|
|
|
PKG_GETINT(res, tmp, 2, sc->bif.lfcap, end);
|
|
|
|
PKG_GETINT(res, tmp, 3, sc->bif.btech, end);
|
|
|
|
PKG_GETINT(res, tmp, 4, sc->bif.dvol, end);
|
|
|
|
PKG_GETINT(res, tmp, 5, sc->bif.wcap, end);
|
|
|
|
PKG_GETINT(res, tmp, 6, sc->bif.lcap, end);
|
|
|
|
PKG_GETINT(res, tmp, 7, sc->bif.gra1, end);
|
|
|
|
PKG_GETINT(res, tmp, 8, sc->bif.gra2, end);
|
|
|
|
PKG_GETSTR(res, tmp, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN, end);
|
|
|
|
PKG_GETSTR(res, tmp, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN, end);
|
|
|
|
PKG_GETSTR(res, tmp, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN, end);
|
|
|
|
PKG_GETSTR(res, tmp, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN, end);
|
2001-06-23 10:38:25 +00:00
|
|
|
acpi_cmbat_info_updated(&sc->bif_lastupdated);
|
2000-12-22 14:41:55 +00:00
|
|
|
end:
|
2002-02-23 05:24:14 +00:00
|
|
|
if (bif_buffer.Pointer != NULL)
|
|
|
|
AcpiOsFree(bif_buffer.Pointer);
|
2001-12-22 16:05:41 +00:00
|
|
|
sc->bif_updating = 0;
|
2000-12-22 14:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
|
|
|
|
{
|
2001-06-24 02:39:08 +00:00
|
|
|
device_t dev;
|
|
|
|
struct acpi_cmbat_softc *sc;
|
|
|
|
|
|
|
|
dev = (device_t)context;
|
|
|
|
if ((sc = device_get_softc(dev)) == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2001-06-23 10:38:25 +00:00
|
|
|
|
2000-12-22 14:41:55 +00:00
|
|
|
switch (notify) {
|
|
|
|
case ACPI_BATTERY_BST_CHANGE:
|
2001-06-24 02:39:08 +00:00
|
|
|
timespecclear(&sc->bst_lastupdated);
|
2000-12-22 14:41:55 +00:00
|
|
|
break;
|
|
|
|
case ACPI_BATTERY_BIF_CHANGE:
|
2001-06-24 02:39:08 +00:00
|
|
|
timespecclear(&sc->bif_lastupdated);
|
2001-10-22 18:01:37 +00:00
|
|
|
AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, dev);
|
2000-12-22 14:41:55 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
acpi_cmbat_probe(device_t dev)
|
|
|
|
{
|
|
|
|
|
|
|
|
if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
|
2001-11-22 17:43:15 +00:00
|
|
|
!acpi_disabled("cmbat") &&
|
2000-12-22 14:41:55 +00:00
|
|
|
acpi_MatchHid(dev, "PNP0C0A")) {
|
|
|
|
/*
|
2001-10-30 15:51:52 +00:00
|
|
|
* Set device description.
|
2000-12-22 14:41:55 +00:00
|
|
|
*/
|
|
|
|
device_set_desc(dev, "Control method Battery");
|
2001-10-30 15:51:52 +00:00
|
|
|
return (0);
|
2000-12-22 14:41:55 +00:00
|
|
|
}
|
2001-10-30 15:51:52 +00:00
|
|
|
return (ENXIO);
|
2000-12-22 14:41:55 +00:00
|
|
|
}
|
2001-10-30 15:51:52 +00:00
|
|
|
|
2000-12-22 14:41:55 +00:00
|
|
|
static int
|
|
|
|
acpi_cmbat_attach(device_t dev)
|
|
|
|
{
|
2001-10-30 15:51:52 +00:00
|
|
|
int error;
|
|
|
|
ACPI_HANDLE handle;
|
2000-12-22 14:41:55 +00:00
|
|
|
struct acpi_cmbat_softc *sc;
|
2001-06-23 10:38:25 +00:00
|
|
|
|
|
|
|
if ((sc = device_get_softc(dev)) == NULL) {
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
2001-10-30 15:51:52 +00:00
|
|
|
|
|
|
|
handle = acpi_get_handle(dev);
|
|
|
|
|
2000-12-22 14:41:55 +00:00
|
|
|
AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
|
2001-06-23 10:38:25 +00:00
|
|
|
acpi_cmbat_notify_handler, dev);
|
2001-10-30 15:51:52 +00:00
|
|
|
|
2001-12-22 16:05:41 +00:00
|
|
|
sc->bif_updating = sc->bst_updating = 0;
|
2001-06-23 10:38:25 +00:00
|
|
|
sc->dev = dev;
|
|
|
|
|
|
|
|
timespecclear(&sc->bif_lastupdated);
|
|
|
|
timespecclear(&sc->bst_lastupdated);
|
2000-12-22 14:41:55 +00:00
|
|
|
|
2000-12-24 19:12:10 +00:00
|
|
|
if (acpi_cmbat_units == 0) {
|
2001-06-23 10:38:25 +00:00
|
|
|
if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BIF,
|
|
|
|
acpi_cmbat_ioctl, NULL)) != 0) {
|
2000-12-24 19:12:10 +00:00
|
|
|
return (error);
|
2001-06-23 10:38:25 +00:00
|
|
|
}
|
|
|
|
if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BST,
|
|
|
|
acpi_cmbat_ioctl, NULL)) != 0) {
|
2000-12-24 19:12:10 +00:00
|
|
|
return (error);
|
2001-06-23 10:38:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT,
|
|
|
|
acpi_cmbat_units)) != 0) {
|
|
|
|
return (error);
|
2000-12-24 19:12:10 +00:00
|
|
|
}
|
2000-12-22 14:41:55 +00:00
|
|
|
|
|
|
|
acpi_cmbat_units++;
|
2001-06-23 10:38:25 +00:00
|
|
|
timespecclear(&acpi_cmbat_info_lastupdated);
|
2002-11-03 10:49:24 +00:00
|
|
|
sc->initializing = 0;
|
2001-06-23 10:38:25 +00:00
|
|
|
|
2002-11-03 10:49:24 +00:00
|
|
|
AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_init_battery, dev);
|
2001-10-30 15:51:52 +00:00
|
|
|
return (0);
|
2000-12-22 14:41:55 +00:00
|
|
|
}
|
|
|
|
|
2001-10-22 18:01:37 +00:00
|
|
|
static int
|
|
|
|
acpi_cmbat_resume(device_t dev)
|
|
|
|
{
|
|
|
|
|
2002-11-03 10:49:24 +00:00
|
|
|
AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_init_battery, dev);
|
2001-10-22 18:01:37 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2001-10-30 15:51:52 +00:00
|
|
|
|
2000-12-22 14:41:55 +00:00
|
|
|
static device_method_t acpi_cmbat_methods[] = {
|
|
|
|
/* Device interface */
|
|
|
|
DEVMETHOD(device_probe, acpi_cmbat_probe),
|
|
|
|
DEVMETHOD(device_attach, acpi_cmbat_attach),
|
2001-10-22 18:01:37 +00:00
|
|
|
DEVMETHOD(device_resume, acpi_cmbat_resume),
|
2000-12-22 14:41:55 +00:00
|
|
|
|
|
|
|
{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);
|
2000-12-24 19:12:10 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
acpi_cmbat_ioctl(u_long cmd, caddr_t addr, void *arg)
|
|
|
|
{
|
2001-10-30 15:51:52 +00:00
|
|
|
device_t dev;
|
|
|
|
union acpi_battery_ioctl_arg *ioctl_arg;
|
|
|
|
struct acpi_cmbat_softc *sc;
|
2001-06-23 10:38:25 +00:00
|
|
|
struct acpi_bif *bifp;
|
|
|
|
struct acpi_bst *bstp;
|
|
|
|
|
|
|
|
ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
|
|
|
|
if ((dev = devclass_get_device(acpi_cmbat_devclass,
|
|
|
|
ioctl_arg->unit)) == NULL) {
|
2001-10-30 15:51:52 +00:00
|
|
|
return (ENXIO);
|
2000-12-24 19:12:10 +00:00
|
|
|
}
|
|
|
|
|
2001-06-23 10:38:25 +00:00
|
|
|
if ((sc = device_get_softc(dev)) == NULL) {
|
2001-10-30 15:51:52 +00:00
|
|
|
return (ENXIO);
|
2001-06-23 10:38:25 +00:00
|
|
|
}
|
2000-12-24 19:12:10 +00:00
|
|
|
|
2003-02-15 01:46:22 +00:00
|
|
|
/*
|
|
|
|
* No security check required: information retrieval only. If
|
|
|
|
* new functions are added here, a check might be required.
|
|
|
|
*/
|
|
|
|
|
2001-06-23 10:38:25 +00:00
|
|
|
switch (cmd) {
|
2000-12-24 19:12:10 +00:00
|
|
|
case ACPIIO_CMBAT_GET_BIF:
|
|
|
|
acpi_cmbat_get_bif(dev);
|
|
|
|
bifp = &ioctl_arg->bif;
|
|
|
|
bifp->unit = sc->bif.unit;
|
|
|
|
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;
|
2002-08-21 15:01:53 +00:00
|
|
|
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;
|
2000-12-24 19:12:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-10-30 15:51:52 +00:00
|
|
|
return (0);
|
2000-12-24 19:12:10 +00:00
|
|
|
}
|
2001-06-23 10:38:25 +00:00
|
|
|
|
2002-11-03 10:49:24 +00:00
|
|
|
static __inline 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline int
|
|
|
|
acpi_cmbat_is_bif_valid(struct acpi_bif *bif)
|
|
|
|
{
|
|
|
|
if (bif->lfcap == 0) {
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2001-06-23 10:38:25 +00:00
|
|
|
static int
|
|
|
|
acpi_cmbat_get_total_battinfo(struct acpi_battinfo *battinfo)
|
|
|
|
{
|
2001-10-30 15:51:52 +00:00
|
|
|
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;
|
2001-06-23 10:38:25 +00:00
|
|
|
|
|
|
|
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) {
|
2001-10-30 14:24:26 +00:00
|
|
|
free(bat, M_ACPICMBAT);
|
|
|
|
bat = NULL;
|
2001-06-23 10:38:25 +00:00
|
|
|
}
|
|
|
|
bat_units = 0;
|
|
|
|
}
|
|
|
|
if (bat == NULL) {
|
|
|
|
bat_units = acpi_cmbat_units;
|
|
|
|
bat = malloc(sizeof(struct acpi_cmbat_softc *) * bat_units,
|
2001-10-30 14:24:26 +00:00
|
|
|
M_ACPICMBAT, M_NOWAIT);
|
2001-06-23 10:38:25 +00:00
|
|
|
if (bat == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Collect softc pointers */
|
2001-10-30 15:51:52 +00:00
|
|
|
for (i = 0; i < acpi_cmbat_units; i++) {
|
2001-06-23 10:38:25 +00:00
|
|
|
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++) {
|
2002-08-21 15:01:53 +00:00
|
|
|
bat[i]->present = acpi_BatteryIsPresent(bat[i]->dev);
|
|
|
|
if (!bat[i]->present)
|
|
|
|
continue;
|
|
|
|
|
2001-06-23 10:38:25 +00:00
|
|
|
acpi_cmbat_get_bst(bat[i]->dev);
|
|
|
|
|
|
|
|
/* If battey not installed, we get strange values */
|
2002-11-03 10:49:24 +00:00
|
|
|
if (!acpi_cmbat_is_bst_valid(&(bat[i]->bst)) ||
|
|
|
|
!acpi_cmbat_is_bif_valid(&(bat[i]->bif))) {
|
2002-08-21 15:01:53 +00:00
|
|
|
bat[i]->present = 0;
|
2001-06-23 10:38:25 +00:00
|
|
|
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.
|
2001-10-30 15:51:52 +00:00
|
|
|
* Some other systems may need sum of bst.rate in
|
2001-06-23 10:38:25 +00:00
|
|
|
* 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++) {
|
2002-08-21 15:01:53 +00:00
|
|
|
if (!bat[i]->present) {
|
2001-06-23 10:38:25 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2002-11-03 10:49:24 +00:00
|
|
|
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);
|
|
|
|
#define ACPI_CMBAT_RETRY_MAX 6
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
sc->initializing = 0;
|
|
|
|
}
|
|
|
|
|
2001-06-23 10:38:25 +00:00
|
|
|
/*
|
|
|
|
* Public interfaces.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo)
|
|
|
|
{
|
2001-10-30 15:51:52 +00:00
|
|
|
int error;
|
|
|
|
device_t dev;
|
|
|
|
struct acpi_cmbat_softc *sc;
|
2001-06-23 10:38:25 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-08-21 15:01:53 +00:00
|
|
|
if (!sc->present) {
|
2001-06-23 10:38:25 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|