2024-04-27 03:40:06 +03:00
|
|
|
/* $NetBSD: acpi_bat.c,v 1.123 2024/04/27 00:40:06 christos Exp $ */
|
2003-11-01 06:45:58 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2003 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Charles M. Hannum of By Noon Software, Inc.
|
|
|
|
*
|
|
|
|
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
|
|
|
*/
|
2002-03-24 06:46:10 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2001 Bill Sommerfeld.
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed for the NetBSD Project by
|
|
|
|
* Wasabi Systems, Inc.
|
|
|
|
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
|
|
|
|
* or promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ACPI Battery Driver.
|
|
|
|
*
|
|
|
|
* ACPI defines two different battery device interfaces: "Control
|
|
|
|
* Method" batteries, in which AML methods are defined in order to get
|
|
|
|
* battery status and set battery alarm thresholds, and a "Smart
|
|
|
|
* Battery" device, which is an SMbus device accessed through the ACPI
|
|
|
|
* Embedded Controller device.
|
|
|
|
*
|
|
|
|
* This driver is for the "Control Method"-style battery only.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2024-04-27 03:40:06 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: acpi_bat.c,v 1.123 2024/04/27 00:40:06 christos Exp $");
|
2002-03-24 06:46:10 +03:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2010-03-05 17:00:16 +03:00
|
|
|
#include <sys/condvar.h>
|
2002-03-24 06:46:10 +03:00
|
|
|
#include <sys/device.h>
|
2010-03-05 17:00:16 +03:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/kmem.h>
|
2010-02-28 20:22:41 +03:00
|
|
|
#include <sys/module.h>
|
Imported envsys 2, a brief description of the new features:
(Part 2: drivers)
* Support for detachable sensors.
* Cleaned up the API for simplicity and efficiency.
* Ability to send capacity/critical/warning events to powerd(8).
* Adapted all the code to the new locking order.
* Compatibility with the old envsys API: the ENVSYS_GTREINFO
and ENVSYS_GTREDATA ioctl(2)s are supported.
* Added support for a 'dictionary based communication channel' between
sysmon_power(9) and powerd(8), that means there is no 32 bytes event
size restriction anymore.
* Binary compatibility with old envstat(8) and powerd(8) via COMPAT_40.
* All drivers with the n^2 gtredata bug were fixed, PR kern/36226.
Tested by:
blymn: smsc(4).
bouyer: ipmi(4), mfi(4).
kefren: ug(4).
njoly: viaenv(4), adt7463.c.
riz: owtemp(4).
xtraeme: acpiacad(4), acpibat(4), acpitz(4), aiboost(4), it(4), lm(4).
2007-07-01 11:37:12 +04:00
|
|
|
#include <sys/mutex.h>
|
2010-03-05 17:00:16 +03:00
|
|
|
#include <sys/systm.h>
|
2010-01-23 18:06:47 +03:00
|
|
|
|
2002-03-24 06:46:10 +03:00
|
|
|
#include <dev/acpi/acpireg.h>
|
|
|
|
#include <dev/acpi/acpivar.h>
|
|
|
|
|
2010-01-23 18:06:47 +03:00
|
|
|
#define _COMPONENT ACPI_BAT_COMPONENT
|
|
|
|
ACPI_MODULE_NAME ("acpi_bat")
|
2009-09-16 14:47:54 +04:00
|
|
|
|
2010-04-27 09:57:43 +04:00
|
|
|
#define ACPI_NOTIFY_BAT_STATUS 0x80
|
|
|
|
#define ACPI_NOTIFY_BAT_INFO 0x81
|
|
|
|
|
2010-01-21 11:57:17 +03:00
|
|
|
/*
|
|
|
|
* Sensor indexes.
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
ACPIBAT_PRESENT = 0,
|
2010-03-17 12:21:50 +03:00
|
|
|
ACPIBAT_DVOLTAGE = 1,
|
|
|
|
ACPIBAT_VOLTAGE = 2,
|
|
|
|
ACPIBAT_DCAPACITY = 3,
|
|
|
|
ACPIBAT_LFCCAPACITY = 4,
|
|
|
|
ACPIBAT_CAPACITY = 5,
|
|
|
|
ACPIBAT_CHARGERATE = 6,
|
|
|
|
ACPIBAT_DISCHARGERATE = 7,
|
|
|
|
ACPIBAT_CHARGING = 8,
|
|
|
|
ACPIBAT_CHARGE_STATE = 9,
|
|
|
|
ACPIBAT_COUNT = 10
|
2010-01-21 11:57:17 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Battery Information, _BIF
|
|
|
|
* (ACPI 3.0, sec. 10.2.2.1).
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
ACPIBAT_BIF_UNIT = 0,
|
|
|
|
ACPIBAT_BIF_DCAPACITY = 1,
|
|
|
|
ACPIBAT_BIF_LFCCAPACITY = 2,
|
|
|
|
ACPIBAT_BIF_TECHNOLOGY = 3,
|
|
|
|
ACPIBAT_BIF_DVOLTAGE = 4,
|
|
|
|
ACPIBAT_BIF_WCAPACITY = 5,
|
|
|
|
ACPIBAT_BIF_LCAPACITY = 6,
|
|
|
|
ACPIBAT_BIF_GRANULARITY1 = 7,
|
|
|
|
ACPIBAT_BIF_GRANULARITY2 = 8,
|
|
|
|
ACPIBAT_BIF_MODEL = 9,
|
|
|
|
ACPIBAT_BIF_SERIAL = 10,
|
|
|
|
ACPIBAT_BIF_TYPE = 11,
|
|
|
|
ACPIBAT_BIF_OEM = 12,
|
|
|
|
ACPIBAT_BIF_COUNT = 13
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Battery Status, _BST
|
|
|
|
* (ACPI 3.0, sec. 10.2.2.3).
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
ACPIBAT_BST_STATE = 0,
|
|
|
|
ACPIBAT_BST_RATE = 1,
|
|
|
|
ACPIBAT_BST_CAPACITY = 2,
|
|
|
|
ACPIBAT_BST_VOLTAGE = 3,
|
|
|
|
ACPIBAT_BST_COUNT = 4
|
|
|
|
};
|
2002-12-31 08:26:56 +03:00
|
|
|
|
2002-03-24 06:46:10 +03:00
|
|
|
struct acpibat_softc {
|
2010-01-23 18:06:47 +03:00
|
|
|
struct acpi_devnode *sc_node;
|
|
|
|
struct sysmon_envsys *sc_sme;
|
2011-01-13 16:05:13 +03:00
|
|
|
struct timeval sc_last;
|
2010-01-31 09:45:09 +03:00
|
|
|
envsys_data_t *sc_sensor;
|
2010-01-23 18:06:47 +03:00
|
|
|
kmutex_t sc_mutex;
|
|
|
|
kcondvar_t sc_condvar;
|
2011-07-13 13:58:53 +04:00
|
|
|
int32_t sc_dcapacity;
|
|
|
|
int32_t sc_dvoltage;
|
2010-03-17 10:40:34 +03:00
|
|
|
int32_t sc_lcapacity;
|
|
|
|
int32_t sc_wcapacity;
|
2010-01-24 14:32:13 +03:00
|
|
|
int sc_present;
|
2022-01-07 04:10:57 +03:00
|
|
|
bool sc_dying;
|
2002-03-24 06:46:10 +03:00
|
|
|
};
|
|
|
|
|
2021-01-29 18:20:13 +03:00
|
|
|
static const struct device_compatible_entry compat_data[] = {
|
|
|
|
{ .compat = "PNP0C0A" },
|
|
|
|
DEVICE_COMPAT_EOL
|
2003-11-03 09:03:47 +03:00
|
|
|
};
|
|
|
|
|
2002-12-30 12:37:50 +03:00
|
|
|
#define ACPIBAT_PWRUNIT_MA 0x00000001 /* mA not mW */
|
2002-12-31 08:26:56 +03:00
|
|
|
#define ACPIBAT_ST_DISCHARGING 0x00000001 /* battery is discharging */
|
|
|
|
#define ACPIBAT_ST_CHARGING 0x00000002 /* battery is charging */
|
|
|
|
#define ACPIBAT_ST_CRITICAL 0x00000004 /* battery is critical */
|
2002-12-30 12:37:50 +03:00
|
|
|
|
2010-01-21 11:57:17 +03:00
|
|
|
/*
|
2010-03-17 10:48:18 +03:00
|
|
|
* A value used when _BST or _BIF is temporarily unknown.
|
2010-01-21 11:57:17 +03:00
|
|
|
*/
|
|
|
|
#define ACPIBAT_VAL_UNKNOWN 0xFFFFFFFF
|
|
|
|
|
|
|
|
#define ACPIBAT_VAL_ISVALID(x) \
|
|
|
|
(((x) != ACPIBAT_VAL_UNKNOWN) ? ENVSYS_SVALID : ENVSYS_SINVALID)
|
|
|
|
|
2010-01-23 18:06:47 +03:00
|
|
|
static int acpibat_match(device_t, cfdata_t, void *);
|
|
|
|
static void acpibat_attach(device_t, device_t, void *);
|
2010-01-31 09:10:53 +03:00
|
|
|
static int acpibat_detach(device_t, int);
|
2010-01-23 18:06:47 +03:00
|
|
|
static int acpibat_get_sta(device_t);
|
2010-10-25 11:48:03 +04:00
|
|
|
static ACPI_OBJECT *acpibat_get_object(ACPI_HANDLE, const char *, uint32_t);
|
2010-01-23 18:06:47 +03:00
|
|
|
static void acpibat_get_info(device_t);
|
2010-03-17 12:21:50 +03:00
|
|
|
static void acpibat_print_info(device_t, ACPI_OBJECT *);
|
2010-01-23 18:06:47 +03:00
|
|
|
static void acpibat_get_status(device_t);
|
|
|
|
static void acpibat_update_info(void *);
|
|
|
|
static void acpibat_update_status(void *);
|
|
|
|
static void acpibat_init_envsys(device_t);
|
2010-04-14 23:27:28 +04:00
|
|
|
static void acpibat_notify_handler(ACPI_HANDLE, uint32_t, void *);
|
2010-01-23 18:06:47 +03:00
|
|
|
static void acpibat_refresh(struct sysmon_envsys *, envsys_data_t *);
|
2010-02-25 01:37:54 +03:00
|
|
|
static bool acpibat_resume(device_t, const pmf_qual_t *);
|
2010-03-17 10:40:34 +03:00
|
|
|
static void acpibat_get_limits(struct sysmon_envsys *, envsys_data_t *,
|
|
|
|
sysmon_envsys_lim_t *, uint32_t *);
|
2002-03-24 06:46:10 +03:00
|
|
|
|
2007-10-18 03:30:58 +04:00
|
|
|
CFATTACH_DECL_NEW(acpibat, sizeof(struct acpibat_softc),
|
2010-01-31 09:10:53 +03:00
|
|
|
acpibat_match, acpibat_attach, acpibat_detach, NULL);
|
2002-03-24 06:46:10 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* acpibat_match:
|
|
|
|
*
|
|
|
|
* Autoconfiguration `match' routine.
|
|
|
|
*/
|
2004-05-01 16:03:27 +04:00
|
|
|
static int
|
2009-05-12 13:29:46 +04:00
|
|
|
acpibat_match(device_t parent, cfdata_t match, void *aux)
|
2002-03-24 06:46:10 +03:00
|
|
|
{
|
|
|
|
struct acpi_attach_args *aa = aux;
|
|
|
|
|
2021-01-29 18:20:13 +03:00
|
|
|
return acpi_compatible_match(aa, compat_data);
|
2002-03-24 06:46:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* acpibat_attach:
|
|
|
|
*
|
|
|
|
* Autoconfiguration `attach' routine.
|
|
|
|
*/
|
2004-05-01 16:03:27 +04:00
|
|
|
static void
|
2007-10-18 03:30:58 +04:00
|
|
|
acpibat_attach(device_t parent, device_t self, void *aux)
|
2002-03-24 06:46:10 +03:00
|
|
|
{
|
2007-10-18 03:30:58 +04:00
|
|
|
struct acpibat_softc *sc = device_private(self);
|
2002-03-24 06:46:10 +03:00
|
|
|
struct acpi_attach_args *aa = aux;
|
2011-01-09 12:47:55 +03:00
|
|
|
ACPI_HANDLE tmp;
|
|
|
|
ACPI_STATUS rv;
|
2002-03-24 06:46:10 +03:00
|
|
|
|
2010-01-23 18:06:47 +03:00
|
|
|
aprint_naive(": ACPI Battery\n");
|
|
|
|
aprint_normal(": ACPI Battery\n");
|
2002-03-24 06:46:10 +03:00
|
|
|
|
|
|
|
sc->sc_node = aa->aa_node;
|
2010-03-17 10:40:34 +03:00
|
|
|
|
2008-06-03 19:02:31 +04:00
|
|
|
mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
|
|
|
|
cv_init(&sc->sc_condvar, device_xname(self));
|
|
|
|
|
2010-01-31 09:45:09 +03:00
|
|
|
sc->sc_sensor = kmem_zalloc(ACPIBAT_COUNT *
|
|
|
|
sizeof(*sc->sc_sensor), KM_SLEEP);
|
|
|
|
|
2018-08-10 20:11:56 +03:00
|
|
|
config_interrupts(self, acpibat_init_envsys);
|
2011-01-09 12:47:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is ever seen, the driver should be extended.
|
|
|
|
*/
|
|
|
|
rv = AcpiGetHandle(sc->sc_node->ad_handle, "_BIX", &tmp);
|
|
|
|
if (ACPI_SUCCESS(rv))
|
|
|
|
aprint_verbose_dev(self, "ACPI 4.0 functionality present\n");
|
2002-03-24 06:46:10 +03:00
|
|
|
}
|
|
|
|
|
2010-01-31 09:10:53 +03:00
|
|
|
/*
|
|
|
|
* acpibat_detach:
|
|
|
|
*
|
|
|
|
* Autoconfiguration `detach' routine.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
acpibat_detach(device_t self, int flags)
|
|
|
|
{
|
|
|
|
struct acpibat_softc *sc = device_private(self);
|
|
|
|
|
2022-01-07 04:10:57 +03:00
|
|
|
/* Prevent further use of sc->sc_sme in acpibat_update_info. */
|
|
|
|
mutex_enter(&sc->sc_mutex);
|
|
|
|
sc->sc_dying = true;
|
|
|
|
mutex_exit(&sc->sc_mutex);
|
|
|
|
|
|
|
|
/* Prevent further calls to acpibat_resume. */
|
2021-12-31 17:20:24 +03:00
|
|
|
pmf_device_deregister(self);
|
2022-01-07 04:10:57 +03:00
|
|
|
|
|
|
|
/* Prevent further calls to acpibat_notify_handler. */
|
2010-04-15 11:02:24 +04:00
|
|
|
acpi_deregister_notify(sc->sc_node);
|
2010-01-31 09:10:53 +03:00
|
|
|
|
2022-01-07 04:10:57 +03:00
|
|
|
/* Detach sensors and prevent further calls to acpibat_refresh. */
|
2010-01-31 09:10:53 +03:00
|
|
|
if (sc->sc_sme != NULL)
|
|
|
|
sysmon_envsys_unregister(sc->sc_sme);
|
|
|
|
|
2022-01-07 04:10:57 +03:00
|
|
|
/*
|
|
|
|
* Wait for calls to acpibat_update_info/status in case sysmon
|
|
|
|
* envsys refreshed the sensors and queued them but they didn't
|
|
|
|
* run before sysmon_envsys_unregister. After this point, no
|
|
|
|
* asynchronous access to the softc is possible.
|
|
|
|
*/
|
|
|
|
AcpiOsWaitEventsComplete();
|
|
|
|
|
2010-01-31 09:45:09 +03:00
|
|
|
if (sc->sc_sensor != NULL)
|
|
|
|
kmem_free(sc->sc_sensor, ACPIBAT_COUNT *
|
|
|
|
sizeof(*sc->sc_sensor));
|
|
|
|
|
2021-12-31 17:20:24 +03:00
|
|
|
cv_destroy(&sc->sc_condvar);
|
|
|
|
mutex_destroy(&sc->sc_mutex);
|
2010-01-31 09:10:53 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-30 16:06:43 +03:00
|
|
|
/*
|
2010-01-23 18:06:47 +03:00
|
|
|
* acpibat_get_sta:
|
2010-01-21 11:57:17 +03:00
|
|
|
*
|
|
|
|
* Evaluate whether the battery is present or absent.
|
|
|
|
*
|
|
|
|
* Returns: 0 for no battery, 1 for present, and -1 on error.
|
2002-12-30 16:06:43 +03:00
|
|
|
*/
|
2004-05-01 16:03:27 +04:00
|
|
|
static int
|
2010-01-23 18:06:47 +03:00
|
|
|
acpibat_get_sta(device_t dv)
|
2002-12-30 16:06:43 +03:00
|
|
|
{
|
2007-10-19 03:54:54 +04:00
|
|
|
struct acpibat_softc *sc = device_private(dv);
|
2004-03-24 14:26:46 +03:00
|
|
|
ACPI_INTEGER val;
|
2002-12-30 16:06:43 +03:00
|
|
|
ACPI_STATUS rv;
|
|
|
|
|
2003-05-22 19:35:51 +04:00
|
|
|
rv = acpi_eval_integer(sc->sc_node->ad_handle, "_STA", &val);
|
2003-11-03 21:07:10 +03:00
|
|
|
if (ACPI_FAILURE(rv)) {
|
2021-12-31 17:20:56 +03:00
|
|
|
aprint_error_dev(dv, "failed to evaluate _STA: %s\n",
|
|
|
|
AcpiFormatException(rv));
|
2004-04-11 10:48:25 +04:00
|
|
|
return -1;
|
2002-12-30 16:06:43 +03:00
|
|
|
}
|
|
|
|
|
2010-01-21 11:57:17 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_PRESENT].state = ENVSYS_SVALID;
|
|
|
|
|
2010-03-08 14:45:45 +03:00
|
|
|
if ((val & ACPI_STA_BATTERY_PRESENT) == 0) {
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_PRESENT].value_cur = 0;
|
2010-01-21 11:57:17 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->sc_sensor[ACPIBAT_PRESENT].value_cur = 1;
|
Imported envsys 2, a brief description of the new features:
(Part 2: drivers)
* Support for detachable sensors.
* Cleaned up the API for simplicity and efficiency.
* Ability to send capacity/critical/warning events to powerd(8).
* Adapted all the code to the new locking order.
* Compatibility with the old envsys API: the ENVSYS_GTREINFO
and ENVSYS_GTREDATA ioctl(2)s are supported.
* Added support for a 'dictionary based communication channel' between
sysmon_power(9) and powerd(8), that means there is no 32 bytes event
size restriction anymore.
* Binary compatibility with old envstat(8) and powerd(8) via COMPAT_40.
* All drivers with the n^2 gtredata bug were fixed, PR kern/36226.
Tested by:
blymn: smsc(4).
bouyer: ipmi(4), mfi(4).
kefren: ug(4).
njoly: viaenv(4), adt7463.c.
riz: owtemp(4).
xtraeme: acpiacad(4), acpibat(4), acpitz(4), aiboost(4), it(4), lm(4).
2007-07-01 11:37:12 +04:00
|
|
|
|
2010-01-21 11:57:17 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ACPI_OBJECT *
|
2010-10-25 11:48:03 +04:00
|
|
|
acpibat_get_object(ACPI_HANDLE hdl, const char *pth, uint32_t count)
|
2010-01-21 11:57:17 +03:00
|
|
|
{
|
|
|
|
ACPI_OBJECT *obj;
|
|
|
|
ACPI_BUFFER buf;
|
|
|
|
ACPI_STATUS rv;
|
|
|
|
|
|
|
|
rv = acpi_eval_struct(hdl, pth, &buf);
|
|
|
|
if (ACPI_FAILURE(rv))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
obj = buf.Pointer;
|
|
|
|
if (obj->Type != ACPI_TYPE_PACKAGE) {
|
|
|
|
ACPI_FREE(buf.Pointer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (obj->Package.Count != count) {
|
|
|
|
ACPI_FREE(buf.Pointer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
2002-12-30 16:06:43 +03:00
|
|
|
}
|
2002-03-24 06:46:10 +03:00
|
|
|
|
|
|
|
/*
|
2010-01-21 11:57:17 +03:00
|
|
|
* acpibat_get_info:
|
2002-03-24 06:46:10 +03:00
|
|
|
*
|
2010-03-17 12:21:50 +03:00
|
|
|
* Get the battery info.
|
2002-03-24 06:46:10 +03:00
|
|
|
*/
|
2010-01-23 18:06:47 +03:00
|
|
|
static void
|
2007-10-19 03:54:54 +04:00
|
|
|
acpibat_get_info(device_t dv)
|
2002-03-24 06:46:10 +03:00
|
|
|
{
|
2007-10-19 03:54:54 +04:00
|
|
|
struct acpibat_softc *sc = device_private(dv);
|
2010-01-21 11:57:17 +03:00
|
|
|
ACPI_HANDLE hdl = sc->sc_node->ad_handle;
|
|
|
|
ACPI_OBJECT *elm, *obj;
|
|
|
|
ACPI_STATUS rv = AE_OK;
|
2010-10-25 11:48:03 +04:00
|
|
|
int capunit, i, rateunit;
|
|
|
|
uint64_t val;
|
2002-12-30 16:06:43 +03:00
|
|
|
|
2010-01-21 11:57:17 +03:00
|
|
|
obj = acpibat_get_object(hdl, "_BIF", ACPIBAT_BIF_COUNT);
|
|
|
|
if (obj == NULL) {
|
|
|
|
rv = AE_ERROR;
|
2002-03-24 06:46:10 +03:00
|
|
|
goto out;
|
|
|
|
}
|
2010-01-21 11:57:17 +03:00
|
|
|
|
|
|
|
elm = obj->Package.Elements;
|
|
|
|
for (i = ACPIBAT_BIF_UNIT; i < ACPIBAT_BIF_MODEL; i++) {
|
|
|
|
if (elm[i].Type != ACPI_TYPE_INTEGER) {
|
|
|
|
rv = AE_TYPE;
|
|
|
|
goto out;
|
|
|
|
}
|
2015-01-01 01:01:01 +03:00
|
|
|
if (elm[i].Integer.Value != ACPIBAT_VAL_UNKNOWN &&
|
|
|
|
elm[i].Integer.Value >= INT_MAX) {
|
2014-10-02 16:42:12 +04:00
|
|
|
rv = AE_LIMIT;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-12-30 07:47:22 +03:00
|
|
|
}
|
2003-02-16 19:50:09 +03:00
|
|
|
|
2015-01-01 01:01:01 +03:00
|
|
|
switch (elm[ACPIBAT_BIF_UNIT].Integer.Value) {
|
|
|
|
case ACPIBAT_PWRUNIT_MA:
|
2003-02-16 19:50:09 +03:00
|
|
|
capunit = ENVSYS_SAMPHOUR;
|
|
|
|
rateunit = ENVSYS_SAMPS;
|
2015-01-01 01:01:01 +03:00
|
|
|
break;
|
|
|
|
default:
|
2003-02-16 19:50:09 +03:00
|
|
|
capunit = ENVSYS_SWATTHOUR;
|
|
|
|
rateunit = ENVSYS_SWATTS;
|
2015-01-01 01:01:01 +03:00
|
|
|
break;
|
2003-02-16 19:50:09 +03:00
|
|
|
}
|
2003-11-02 01:55:53 +03:00
|
|
|
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_DCAPACITY].units = capunit;
|
|
|
|
sc->sc_sensor[ACPIBAT_LFCCAPACITY].units = capunit;
|
|
|
|
sc->sc_sensor[ACPIBAT_CHARGERATE].units = rateunit;
|
|
|
|
sc->sc_sensor[ACPIBAT_DISCHARGERATE].units = rateunit;
|
|
|
|
sc->sc_sensor[ACPIBAT_CAPACITY].units = capunit;
|
|
|
|
|
2010-01-21 11:57:17 +03:00
|
|
|
/* Design capacity. */
|
2010-03-17 10:48:18 +03:00
|
|
|
val = elm[ACPIBAT_BIF_DCAPACITY].Integer.Value;
|
|
|
|
sc->sc_sensor[ACPIBAT_DCAPACITY].value_cur = val * 1000;
|
2010-01-21 11:57:17 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_DCAPACITY].state = ACPIBAT_VAL_ISVALID(val);
|
|
|
|
|
|
|
|
/* Last full charge capacity. */
|
2010-03-17 10:48:18 +03:00
|
|
|
val = elm[ACPIBAT_BIF_LFCCAPACITY].Integer.Value;
|
|
|
|
sc->sc_sensor[ACPIBAT_LFCCAPACITY].value_cur = val * 1000;
|
2010-01-21 11:57:17 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_LFCCAPACITY].state = ACPIBAT_VAL_ISVALID(val);
|
|
|
|
|
|
|
|
/* Design voltage. */
|
2010-03-17 10:48:18 +03:00
|
|
|
val = elm[ACPIBAT_BIF_DVOLTAGE].Integer.Value;
|
|
|
|
sc->sc_sensor[ACPIBAT_DVOLTAGE].value_cur = val * 1000;
|
2010-01-21 11:57:17 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_DVOLTAGE].state = ACPIBAT_VAL_ISVALID(val);
|
|
|
|
|
2010-03-17 10:40:34 +03:00
|
|
|
/* Design low and warning capacity. */
|
|
|
|
sc->sc_lcapacity = elm[ACPIBAT_BIF_LCAPACITY].Integer.Value * 1000;
|
|
|
|
sc->sc_wcapacity = elm[ACPIBAT_BIF_WCAPACITY].Integer.Value * 1000;
|
2003-11-02 01:55:53 +03:00
|
|
|
|
2010-01-21 11:57:17 +03:00
|
|
|
/*
|
2010-03-17 10:40:34 +03:00
|
|
|
* Initialize the maximum of current capacity
|
|
|
|
* to the last known full charge capacity.
|
2010-01-21 11:57:17 +03:00
|
|
|
*/
|
|
|
|
val = sc->sc_sensor[ACPIBAT_LFCCAPACITY].value_cur;
|
|
|
|
sc->sc_sensor[ACPIBAT_CAPACITY].value_max = val;
|
2002-03-24 06:46:10 +03:00
|
|
|
|
2010-03-17 12:21:50 +03:00
|
|
|
acpibat_print_info(dv, elm);
|
|
|
|
|
2002-03-24 06:46:10 +03:00
|
|
|
out:
|
2010-01-21 11:57:17 +03:00
|
|
|
if (obj != NULL)
|
|
|
|
ACPI_FREE(obj);
|
|
|
|
|
|
|
|
if (ACPI_FAILURE(rv))
|
|
|
|
aprint_error_dev(dv, "failed to evaluate _BIF: %s\n",
|
|
|
|
AcpiFormatException(rv));
|
2002-03-24 06:46:10 +03:00
|
|
|
}
|
|
|
|
|
2010-03-17 12:21:50 +03:00
|
|
|
/*
|
|
|
|
* acpibat_print_info:
|
|
|
|
*
|
|
|
|
* Display the battery info.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
acpibat_print_info(device_t dv, ACPI_OBJECT *elm)
|
|
|
|
{
|
2010-06-30 19:38:27 +04:00
|
|
|
struct acpibat_softc *sc = device_private(dv);
|
2011-07-13 13:58:53 +04:00
|
|
|
const char *tech, *unit;
|
|
|
|
int32_t dcap, dvol;
|
2010-03-17 12:21:50 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = ACPIBAT_BIF_OEM; i > ACPIBAT_BIF_GRANULARITY2; i--) {
|
|
|
|
if (elm[i].Type != ACPI_TYPE_STRING)
|
|
|
|
return;
|
|
|
|
if (elm[i].String.Pointer == NULL)
|
|
|
|
return;
|
2010-06-30 19:38:27 +04:00
|
|
|
if (elm[i].String.Pointer[0] == '\0')
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-13 13:58:53 +04:00
|
|
|
dcap = elm[ACPIBAT_BIF_DCAPACITY].Integer.Value;
|
|
|
|
dvol = elm[ACPIBAT_BIF_DVOLTAGE].Integer.Value;
|
2010-06-30 19:38:27 +04:00
|
|
|
|
2011-07-13 13:58:53 +04:00
|
|
|
/*
|
|
|
|
* Try to detect whether the battery was switched.
|
|
|
|
*/
|
|
|
|
if (sc->sc_dcapacity == dcap && sc->sc_dvoltage == dvol)
|
2010-06-30 19:38:27 +04:00
|
|
|
return;
|
|
|
|
else {
|
2011-07-13 13:58:53 +04:00
|
|
|
sc->sc_dcapacity = dcap;
|
|
|
|
sc->sc_dvoltage = dvol;
|
2010-03-17 12:21:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tech = (elm[ACPIBAT_BIF_TECHNOLOGY].Integer.Value != 0) ?
|
2010-04-16 05:52:54 +04:00
|
|
|
"rechargeable" : "non-rechargeable";
|
2010-03-17 12:21:50 +03:00
|
|
|
|
2010-04-16 05:52:54 +04:00
|
|
|
aprint_normal_dev(dv, "%s %s %s battery\n",
|
2010-03-17 12:21:50 +03:00
|
|
|
elm[ACPIBAT_BIF_OEM].String.Pointer,
|
2010-04-16 05:52:54 +04:00
|
|
|
elm[ACPIBAT_BIF_TYPE].String.Pointer, tech);
|
|
|
|
|
2011-07-13 13:58:53 +04:00
|
|
|
aprint_debug_dev(dv, "model number %s, serial number %s\n",
|
|
|
|
elm[ACPIBAT_BIF_MODEL].String.Pointer,
|
|
|
|
elm[ACPIBAT_BIF_SERIAL].String.Pointer);
|
2010-03-17 14:07:59 +03:00
|
|
|
|
|
|
|
#define SCALE(x) (((int)x) / 1000000), ((((int)x) % 1000000) / 1000)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These values are defined as follows (ACPI 4.0, p. 388):
|
|
|
|
*
|
|
|
|
* Granularity 1. "Battery capacity granularity between low
|
|
|
|
* and warning in [mAh] or [mWh]. That is,
|
|
|
|
* this is the smallest increment in capacity
|
|
|
|
* that the battery is capable of measuring."
|
|
|
|
*
|
|
|
|
* Granularity 2. "Battery capacity granularity between warning
|
|
|
|
* and full in [mAh] or [mWh]. [...]"
|
|
|
|
*/
|
2015-01-01 01:01:01 +03:00
|
|
|
switch (elm[ACPIBAT_BIF_UNIT].Integer.Value) {
|
|
|
|
case ACPIBAT_PWRUNIT_MA:
|
2010-04-16 05:52:54 +04:00
|
|
|
unit = "Ah";
|
2015-01-01 01:01:01 +03:00
|
|
|
break;
|
|
|
|
default:
|
2010-04-16 05:52:54 +04:00
|
|
|
unit = "Wh";
|
2015-01-01 01:01:01 +03:00
|
|
|
break;
|
|
|
|
}
|
2010-06-30 19:38:27 +04:00
|
|
|
|
|
|
|
aprint_verbose_dev(dv, "granularity: "
|
|
|
|
"low->warn %d.%03d %s, warn->full %d.%03d %s\n",
|
2010-03-17 14:07:59 +03:00
|
|
|
SCALE(elm[ACPIBAT_BIF_GRANULARITY1].Integer.Value * 1000), unit,
|
|
|
|
SCALE(elm[ACPIBAT_BIF_GRANULARITY2].Integer.Value * 1000), unit);
|
2010-03-17 12:21:50 +03:00
|
|
|
}
|
|
|
|
|
2002-03-24 06:46:10 +03:00
|
|
|
/*
|
|
|
|
* acpibat_get_status:
|
|
|
|
*
|
2010-03-17 12:21:50 +03:00
|
|
|
* Get the current battery status.
|
2002-03-24 06:46:10 +03:00
|
|
|
*/
|
2010-01-23 18:06:47 +03:00
|
|
|
static void
|
2007-10-19 03:54:54 +04:00
|
|
|
acpibat_get_status(device_t dv)
|
2002-03-24 06:46:10 +03:00
|
|
|
{
|
2007-10-19 03:54:54 +04:00
|
|
|
struct acpibat_softc *sc = device_private(dv);
|
2010-01-21 11:57:17 +03:00
|
|
|
ACPI_HANDLE hdl = sc->sc_node->ad_handle;
|
|
|
|
ACPI_OBJECT *elm, *obj;
|
|
|
|
ACPI_STATUS rv = AE_OK;
|
2010-10-25 11:48:03 +04:00
|
|
|
int i, rate, state;
|
|
|
|
uint64_t val;
|
2002-03-24 06:46:10 +03:00
|
|
|
|
2010-01-21 11:57:17 +03:00
|
|
|
obj = acpibat_get_object(hdl, "_BST", ACPIBAT_BST_COUNT);
|
|
|
|
if (obj == NULL) {
|
2003-05-22 19:35:51 +04:00
|
|
|
rv = AE_ERROR;
|
|
|
|
goto out;
|
2002-03-24 06:46:10 +03:00
|
|
|
}
|
2010-01-21 11:57:17 +03:00
|
|
|
|
|
|
|
elm = obj->Package.Elements;
|
|
|
|
for (i = ACPIBAT_BST_STATE; i < ACPIBAT_BST_COUNT; i++) {
|
|
|
|
if (elm[i].Type != ACPI_TYPE_INTEGER) {
|
|
|
|
rv = AE_TYPE;
|
|
|
|
goto out;
|
|
|
|
}
|
2002-12-30 09:20:02 +03:00
|
|
|
}
|
2002-03-24 06:46:10 +03:00
|
|
|
|
2010-01-21 11:57:17 +03:00
|
|
|
state = elm[ACPIBAT_BST_STATE].Integer.Value;
|
|
|
|
if ((state & ACPIBAT_ST_CHARGING) != 0) {
|
2010-01-28 01:17:28 +03:00
|
|
|
/* XXX rate can be invalid */
|
|
|
|
rate = elm[ACPIBAT_BST_RATE].Integer.Value;
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_CHARGERATE].state = ENVSYS_SVALID;
|
2010-01-21 11:57:17 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_CHARGERATE].value_cur = rate * 1000;
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_DISCHARGERATE].state = ENVSYS_SINVALID;
|
|
|
|
sc->sc_sensor[ACPIBAT_CHARGING].state = ENVSYS_SVALID;
|
|
|
|
sc->sc_sensor[ACPIBAT_CHARGING].value_cur = 1;
|
2010-01-21 11:57:17 +03:00
|
|
|
} else if ((state & ACPIBAT_ST_DISCHARGING) != 0) {
|
2010-01-28 01:17:28 +03:00
|
|
|
rate = elm[ACPIBAT_BST_RATE].Integer.Value;
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_DISCHARGERATE].state = ENVSYS_SVALID;
|
2010-01-21 11:57:17 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_DISCHARGERATE].value_cur = rate * 1000;
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_CHARGERATE].state = ENVSYS_SINVALID;
|
|
|
|
sc->sc_sensor[ACPIBAT_CHARGING].state = ENVSYS_SVALID;
|
|
|
|
sc->sc_sensor[ACPIBAT_CHARGING].value_cur = 0;
|
2010-01-21 11:57:17 +03:00
|
|
|
} else {
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_CHARGING].state = ENVSYS_SVALID;
|
|
|
|
sc->sc_sensor[ACPIBAT_CHARGING].value_cur = 0;
|
|
|
|
sc->sc_sensor[ACPIBAT_CHARGERATE].state = ENVSYS_SINVALID;
|
|
|
|
sc->sc_sensor[ACPIBAT_DISCHARGERATE].state = ENVSYS_SINVALID;
|
2003-11-01 13:24:17 +03:00
|
|
|
}
|
Imported envsys 2, a brief description of the new features:
(Part 2: drivers)
* Support for detachable sensors.
* Cleaned up the API for simplicity and efficiency.
* Ability to send capacity/critical/warning events to powerd(8).
* Adapted all the code to the new locking order.
* Compatibility with the old envsys API: the ENVSYS_GTREINFO
and ENVSYS_GTREDATA ioctl(2)s are supported.
* Added support for a 'dictionary based communication channel' between
sysmon_power(9) and powerd(8), that means there is no 32 bytes event
size restriction anymore.
* Binary compatibility with old envstat(8) and powerd(8) via COMPAT_40.
* All drivers with the n^2 gtredata bug were fixed, PR kern/36226.
Tested by:
blymn: smsc(4).
bouyer: ipmi(4), mfi(4).
kefren: ug(4).
njoly: viaenv(4), adt7463.c.
riz: owtemp(4).
xtraeme: acpiacad(4), acpibat(4), acpitz(4), aiboost(4), it(4), lm(4).
2007-07-01 11:37:12 +04:00
|
|
|
|
2010-01-21 11:57:17 +03:00
|
|
|
/* Remaining capacity. */
|
2010-03-17 10:48:18 +03:00
|
|
|
val = elm[ACPIBAT_BST_CAPACITY].Integer.Value;
|
|
|
|
sc->sc_sensor[ACPIBAT_CAPACITY].value_cur = val * 1000;
|
2010-01-21 11:57:17 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_CAPACITY].state = ACPIBAT_VAL_ISVALID(val);
|
|
|
|
|
|
|
|
/* Battery voltage. */
|
2010-03-17 10:48:18 +03:00
|
|
|
val = elm[ACPIBAT_BST_VOLTAGE].Integer.Value;
|
|
|
|
sc->sc_sensor[ACPIBAT_VOLTAGE].value_cur = val * 1000;
|
2010-01-21 11:57:17 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_VOLTAGE].state = ACPIBAT_VAL_ISVALID(val);
|
|
|
|
|
2010-01-24 14:32:13 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_CHARGE_STATE].state = ENVSYS_SVALID;
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur =
|
2007-11-04 02:05:21 +03:00
|
|
|
ENVSYS_BATTERY_CAPACITY_NORMAL;
|
2007-09-04 20:56:30 +04:00
|
|
|
|
2010-03-17 10:40:34 +03:00
|
|
|
if (sc->sc_sensor[ACPIBAT_CAPACITY].value_cur < sc->sc_wcapacity) {
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_CAPACITY].state = ENVSYS_SWARNUNDER;
|
|
|
|
sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur =
|
2007-11-04 02:05:21 +03:00
|
|
|
ENVSYS_BATTERY_CAPACITY_WARNING;
|
2007-09-02 23:38:32 +04:00
|
|
|
}
|
Imported envsys 2, a brief description of the new features:
(Part 2: drivers)
* Support for detachable sensors.
* Cleaned up the API for simplicity and efficiency.
* Ability to send capacity/critical/warning events to powerd(8).
* Adapted all the code to the new locking order.
* Compatibility with the old envsys API: the ENVSYS_GTREINFO
and ENVSYS_GTREDATA ioctl(2)s are supported.
* Added support for a 'dictionary based communication channel' between
sysmon_power(9) and powerd(8), that means there is no 32 bytes event
size restriction anymore.
* Binary compatibility with old envstat(8) and powerd(8) via COMPAT_40.
* All drivers with the n^2 gtredata bug were fixed, PR kern/36226.
Tested by:
blymn: smsc(4).
bouyer: ipmi(4), mfi(4).
kefren: ug(4).
njoly: viaenv(4), adt7463.c.
riz: owtemp(4).
xtraeme: acpiacad(4), acpibat(4), acpitz(4), aiboost(4), it(4), lm(4).
2007-07-01 11:37:12 +04:00
|
|
|
|
2010-03-17 10:40:34 +03:00
|
|
|
if (sc->sc_sensor[ACPIBAT_CAPACITY].value_cur < sc->sc_lcapacity) {
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_CAPACITY].state = ENVSYS_SCRITUNDER;
|
|
|
|
sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur =
|
2007-11-04 02:05:21 +03:00
|
|
|
ENVSYS_BATTERY_CAPACITY_LOW;
|
2007-09-02 23:38:32 +04:00
|
|
|
}
|
Imported envsys 2, a brief description of the new features:
(Part 2: drivers)
* Support for detachable sensors.
* Cleaned up the API for simplicity and efficiency.
* Ability to send capacity/critical/warning events to powerd(8).
* Adapted all the code to the new locking order.
* Compatibility with the old envsys API: the ENVSYS_GTREINFO
and ENVSYS_GTREDATA ioctl(2)s are supported.
* Added support for a 'dictionary based communication channel' between
sysmon_power(9) and powerd(8), that means there is no 32 bytes event
size restriction anymore.
* Binary compatibility with old envstat(8) and powerd(8) via COMPAT_40.
* All drivers with the n^2 gtredata bug were fixed, PR kern/36226.
Tested by:
blymn: smsc(4).
bouyer: ipmi(4), mfi(4).
kefren: ug(4).
njoly: viaenv(4), adt7463.c.
riz: owtemp(4).
xtraeme: acpiacad(4), acpibat(4), acpitz(4), aiboost(4), it(4), lm(4).
2007-07-01 11:37:12 +04:00
|
|
|
|
2010-01-21 11:57:17 +03:00
|
|
|
if ((state & ACPIBAT_ST_CRITICAL) != 0) {
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_CAPACITY].state = ENVSYS_SCRITICAL;
|
|
|
|
sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur =
|
2007-11-04 02:05:21 +03:00
|
|
|
ENVSYS_BATTERY_CAPACITY_CRITICAL;
|
2007-09-02 23:38:32 +04:00
|
|
|
}
|
Imported envsys 2, a brief description of the new features:
(Part 2: drivers)
* Support for detachable sensors.
* Cleaned up the API for simplicity and efficiency.
* Ability to send capacity/critical/warning events to powerd(8).
* Adapted all the code to the new locking order.
* Compatibility with the old envsys API: the ENVSYS_GTREINFO
and ENVSYS_GTREDATA ioctl(2)s are supported.
* Added support for a 'dictionary based communication channel' between
sysmon_power(9) and powerd(8), that means there is no 32 bytes event
size restriction anymore.
* Binary compatibility with old envstat(8) and powerd(8) via COMPAT_40.
* All drivers with the n^2 gtredata bug were fixed, PR kern/36226.
Tested by:
blymn: smsc(4).
bouyer: ipmi(4), mfi(4).
kefren: ug(4).
njoly: viaenv(4), adt7463.c.
riz: owtemp(4).
xtraeme: acpiacad(4), acpibat(4), acpitz(4), aiboost(4), it(4), lm(4).
2007-07-01 11:37:12 +04:00
|
|
|
|
2003-11-02 01:55:53 +03:00
|
|
|
out:
|
2010-01-21 11:57:17 +03:00
|
|
|
if (obj != NULL)
|
|
|
|
ACPI_FREE(obj);
|
|
|
|
|
|
|
|
if (ACPI_FAILURE(rv))
|
|
|
|
aprint_error_dev(dv, "failed to evaluate _BST: %s\n",
|
|
|
|
AcpiFormatException(rv));
|
2003-02-16 19:50:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-01-23 18:06:47 +03:00
|
|
|
acpibat_update_info(void *arg)
|
2003-02-16 19:50:09 +03:00
|
|
|
{
|
2010-01-23 18:06:47 +03:00
|
|
|
device_t dv = arg;
|
2007-10-19 03:54:54 +04:00
|
|
|
struct acpibat_softc *sc = device_private(dv);
|
2010-01-23 18:06:47 +03:00
|
|
|
int i, rv;
|
2003-02-16 19:50:09 +03:00
|
|
|
|
2010-01-23 18:06:47 +03:00
|
|
|
mutex_enter(&sc->sc_mutex);
|
2003-02-16 19:50:09 +03:00
|
|
|
|
2022-01-07 04:10:57 +03:00
|
|
|
/* Don't touch sc_sme if we're detaching. */
|
|
|
|
if (sc->sc_dying)
|
|
|
|
goto out;
|
|
|
|
|
2010-01-23 18:06:47 +03:00
|
|
|
rv = acpibat_get_sta(dv);
|
2010-04-03 20:29:22 +04:00
|
|
|
if (rv > 0) {
|
2010-01-23 18:06:47 +03:00
|
|
|
acpibat_get_info(dv);
|
2010-04-03 20:29:22 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the status changed, update the limits.
|
|
|
|
*/
|
|
|
|
if (sc->sc_present == 0 &&
|
|
|
|
sc->sc_sensor[ACPIBAT_CAPACITY].value_max > 0)
|
|
|
|
sysmon_envsys_update_limits(sc->sc_sme,
|
|
|
|
&sc->sc_sensor[ACPIBAT_CAPACITY]);
|
|
|
|
} else {
|
2010-03-17 12:21:50 +03:00
|
|
|
i = (rv < 0) ? 0 : ACPIBAT_DVOLTAGE;
|
2010-01-23 18:06:47 +03:00
|
|
|
while (i < ACPIBAT_COUNT) {
|
|
|
|
sc->sc_sensor[i].state = ENVSYS_SINVALID;
|
|
|
|
i++;
|
|
|
|
}
|
2007-08-08 12:36:41 +04:00
|
|
|
}
|
Imported envsys 2, a brief description of the new features:
(Part 2: drivers)
* Support for detachable sensors.
* Cleaned up the API for simplicity and efficiency.
* Ability to send capacity/critical/warning events to powerd(8).
* Adapted all the code to the new locking order.
* Compatibility with the old envsys API: the ENVSYS_GTREINFO
and ENVSYS_GTREDATA ioctl(2)s are supported.
* Added support for a 'dictionary based communication channel' between
sysmon_power(9) and powerd(8), that means there is no 32 bytes event
size restriction anymore.
* Binary compatibility with old envstat(8) and powerd(8) via COMPAT_40.
* All drivers with the n^2 gtredata bug were fixed, PR kern/36226.
Tested by:
blymn: smsc(4).
bouyer: ipmi(4), mfi(4).
kefren: ug(4).
njoly: viaenv(4), adt7463.c.
riz: owtemp(4).
xtraeme: acpiacad(4), acpibat(4), acpitz(4), aiboost(4), it(4), lm(4).
2007-07-01 11:37:12 +04:00
|
|
|
|
2010-01-24 14:32:13 +03:00
|
|
|
sc->sc_present = rv;
|
2022-01-07 04:10:57 +03:00
|
|
|
out:
|
2010-01-23 18:06:47 +03:00
|
|
|
mutex_exit(&sc->sc_mutex);
|
2003-02-16 19:50:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-01-23 18:06:47 +03:00
|
|
|
acpibat_update_status(void *arg)
|
2003-02-16 19:50:09 +03:00
|
|
|
{
|
2007-10-19 03:54:54 +04:00
|
|
|
device_t dv = arg;
|
|
|
|
struct acpibat_softc *sc = device_private(dv);
|
2010-01-23 18:06:47 +03:00
|
|
|
int i, rv;
|
2003-02-16 19:50:09 +03:00
|
|
|
|
2010-01-23 18:06:47 +03:00
|
|
|
mutex_enter(&sc->sc_mutex);
|
2002-03-24 06:46:10 +03:00
|
|
|
|
2010-01-23 18:06:47 +03:00
|
|
|
rv = acpibat_get_sta(dv);
|
2010-01-24 14:32:13 +03:00
|
|
|
if (rv > 0) {
|
|
|
|
if (sc->sc_present == 0)
|
|
|
|
acpibat_get_info(dv);
|
2010-01-23 18:06:47 +03:00
|
|
|
acpibat_get_status(dv);
|
2010-01-24 14:32:13 +03:00
|
|
|
} else {
|
2010-03-17 12:21:50 +03:00
|
|
|
i = (rv < 0) ? 0 : ACPIBAT_DVOLTAGE;
|
2010-01-23 18:06:47 +03:00
|
|
|
while (i < ACPIBAT_COUNT) {
|
|
|
|
sc->sc_sensor[i].state = ENVSYS_SINVALID;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2008-06-03 05:11:18 +04:00
|
|
|
|
2010-01-24 14:32:13 +03:00
|
|
|
sc->sc_present = rv;
|
2011-01-13 16:05:13 +03:00
|
|
|
microtime(&sc->sc_last);
|
2010-01-24 14:32:13 +03:00
|
|
|
|
2008-06-03 19:02:31 +04:00
|
|
|
cv_broadcast(&sc->sc_condvar);
|
|
|
|
mutex_exit(&sc->sc_mutex);
|
2008-06-03 05:11:18 +04:00
|
|
|
}
|
|
|
|
|
2002-03-24 06:46:10 +03:00
|
|
|
/*
|
|
|
|
* acpibat_notify_handler:
|
|
|
|
*
|
|
|
|
* Callback from ACPI interrupt handler to notify us of an event.
|
|
|
|
*/
|
2004-05-01 16:03:27 +04:00
|
|
|
static void
|
2010-04-14 23:27:28 +04:00
|
|
|
acpibat_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
|
2002-03-24 06:46:10 +03:00
|
|
|
{
|
2010-01-23 18:06:47 +03:00
|
|
|
static const int handler = OSL_NOTIFY_HANDLER;
|
2007-10-19 03:54:54 +04:00
|
|
|
device_t dv = context;
|
2002-12-30 12:37:50 +03:00
|
|
|
|
2002-03-24 06:46:10 +03:00
|
|
|
switch (notify) {
|
2010-04-27 09:57:43 +04:00
|
|
|
case ACPI_NOTIFY_BUS_CHECK:
|
2002-12-30 12:37:50 +03:00
|
|
|
break;
|
2010-04-27 09:57:43 +04:00
|
|
|
case ACPI_NOTIFY_BAT_INFO:
|
|
|
|
case ACPI_NOTIFY_DEVICE_CHECK:
|
2010-01-23 18:06:47 +03:00
|
|
|
(void)AcpiOsExecute(handler, acpibat_update_info, dv);
|
2002-12-30 16:06:43 +03:00
|
|
|
break;
|
2010-04-27 09:57:43 +04:00
|
|
|
case ACPI_NOTIFY_BAT_STATUS:
|
2010-01-23 18:06:47 +03:00
|
|
|
(void)AcpiOsExecute(handler, acpibat_update_status, dv);
|
2002-03-24 06:46:10 +03:00
|
|
|
break;
|
|
|
|
default:
|
2010-01-23 18:06:47 +03:00
|
|
|
aprint_error_dev(dv, "unknown notify: 0x%02X\n", notify);
|
2002-03-24 06:46:10 +03:00
|
|
|
}
|
|
|
|
}
|
2002-12-31 08:26:56 +03:00
|
|
|
|
2004-05-01 16:03:27 +04:00
|
|
|
static void
|
2007-10-19 03:54:54 +04:00
|
|
|
acpibat_init_envsys(device_t dv)
|
2002-12-31 08:26:56 +03:00
|
|
|
{
|
2007-10-19 03:54:54 +04:00
|
|
|
struct acpibat_softc *sc = device_private(dv);
|
2010-01-23 18:06:47 +03:00
|
|
|
int i;
|
2002-12-31 08:26:56 +03:00
|
|
|
|
2007-11-16 11:00:11 +03:00
|
|
|
#define INITDATA(index, unit, string) \
|
2010-01-23 18:06:47 +03:00
|
|
|
do { \
|
|
|
|
sc->sc_sensor[index].state = ENVSYS_SVALID; \
|
|
|
|
sc->sc_sensor[index].units = unit; \
|
|
|
|
(void)strlcpy(sc->sc_sensor[index].desc, string, \
|
|
|
|
sizeof(sc->sc_sensor[index].desc)); \
|
|
|
|
} while (/* CONSTCOND */ 0)
|
2003-11-02 01:55:53 +03:00
|
|
|
|
2003-02-16 19:50:09 +03:00
|
|
|
INITDATA(ACPIBAT_PRESENT, ENVSYS_INDICATOR, "present");
|
2010-01-23 18:06:47 +03:00
|
|
|
INITDATA(ACPIBAT_DCAPACITY, ENVSYS_SWATTHOUR, "design cap");
|
|
|
|
INITDATA(ACPIBAT_LFCCAPACITY, ENVSYS_SWATTHOUR, "last full cap");
|
2002-12-31 08:26:56 +03:00
|
|
|
INITDATA(ACPIBAT_DVOLTAGE, ENVSYS_SVOLTS_DC, "design voltage");
|
|
|
|
INITDATA(ACPIBAT_VOLTAGE, ENVSYS_SVOLTS_DC, "voltage");
|
2010-01-23 18:06:47 +03:00
|
|
|
INITDATA(ACPIBAT_CHARGERATE, ENVSYS_SWATTS, "charge rate");
|
|
|
|
INITDATA(ACPIBAT_DISCHARGERATE, ENVSYS_SWATTS, "discharge rate");
|
|
|
|
INITDATA(ACPIBAT_CAPACITY, ENVSYS_SWATTHOUR, "charge");
|
2007-11-04 02:05:21 +03:00
|
|
|
INITDATA(ACPIBAT_CHARGING, ENVSYS_BATTERY_CHARGE, "charging");
|
|
|
|
INITDATA(ACPIBAT_CHARGE_STATE, ENVSYS_BATTERY_CAPACITY, "charge state");
|
2002-12-31 08:26:56 +03:00
|
|
|
|
2003-11-02 01:55:53 +03:00
|
|
|
#undef INITDATA
|
|
|
|
|
2011-06-21 00:24:59 +04:00
|
|
|
sc->sc_sensor[ACPIBAT_CHARGE_STATE].value_cur =
|
|
|
|
ENVSYS_BATTERY_CAPACITY_NORMAL;
|
|
|
|
|
2010-03-22 18:08:35 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_CAPACITY].flags |=
|
|
|
|
ENVSYS_FPERCENT | ENVSYS_FVALID_MAX | ENVSYS_FMONLIMITS;
|
|
|
|
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_CHARGE_STATE].flags |= ENVSYS_FMONSTCHANGED;
|
2007-08-27 00:47:42 +04:00
|
|
|
|
2010-03-17 10:40:34 +03:00
|
|
|
/* Disable userland monitoring on these sensors. */
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sensor[ACPIBAT_VOLTAGE].flags = ENVSYS_FMONNOTSUPP;
|
|
|
|
sc->sc_sensor[ACPIBAT_CHARGERATE].flags = ENVSYS_FMONNOTSUPP;
|
|
|
|
sc->sc_sensor[ACPIBAT_DISCHARGERATE].flags = ENVSYS_FMONNOTSUPP;
|
|
|
|
sc->sc_sensor[ACPIBAT_DCAPACITY].flags = ENVSYS_FMONNOTSUPP;
|
|
|
|
sc->sc_sensor[ACPIBAT_LFCCAPACITY].flags = ENVSYS_FMONNOTSUPP;
|
|
|
|
sc->sc_sensor[ACPIBAT_DVOLTAGE].flags = ENVSYS_FMONNOTSUPP;
|
|
|
|
|
2012-08-14 18:36:43 +04:00
|
|
|
/* Attach rnd(9) to the (dis)charge rates. */
|
|
|
|
sc->sc_sensor[ACPIBAT_CHARGERATE].flags |= ENVSYS_FHAS_ENTROPY;
|
|
|
|
sc->sc_sensor[ACPIBAT_DISCHARGERATE].flags |= ENVSYS_FHAS_ENTROPY;
|
|
|
|
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sme = sysmon_envsys_create();
|
2010-01-23 18:06:47 +03:00
|
|
|
|
2010-01-21 11:57:17 +03:00
|
|
|
for (i = 0; i < ACPIBAT_COUNT; i++) {
|
2007-11-16 11:00:11 +03:00
|
|
|
if (sysmon_envsys_sensor_attach(sc->sc_sme,
|
2010-01-23 18:06:47 +03:00
|
|
|
&sc->sc_sensor[i]))
|
|
|
|
goto fail;
|
2007-11-16 11:00:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sc->sc_sme->sme_name = device_xname(dv);
|
2008-06-03 19:02:31 +04:00
|
|
|
sc->sc_sme->sme_cookie = dv;
|
|
|
|
sc->sc_sme->sme_refresh = acpibat_refresh;
|
2007-11-16 11:00:11 +03:00
|
|
|
sc->sc_sme->sme_class = SME_CLASS_BATTERY;
|
2024-04-27 03:40:06 +03:00
|
|
|
sc->sc_sme->sme_flags = SME_POLL_ONLY;
|
2010-03-17 10:40:34 +03:00
|
|
|
sc->sc_sme->sme_get_limits = acpibat_get_limits;
|
2002-12-31 08:26:56 +03:00
|
|
|
|
2024-04-26 21:19:18 +03:00
|
|
|
if (sysmon_envsys_register(sc->sc_sme))
|
|
|
|
goto fail;
|
|
|
|
|
2021-12-31 17:20:24 +03:00
|
|
|
(void)acpi_register_notify(sc->sc_node, acpibat_notify_handler);
|
2010-01-23 18:06:47 +03:00
|
|
|
acpibat_update_info(dv);
|
|
|
|
acpibat_update_status(dv);
|
2003-10-31 01:12:02 +03:00
|
|
|
|
2021-12-31 17:20:24 +03:00
|
|
|
(void)pmf_device_register(dv, NULL, acpibat_resume);
|
|
|
|
|
2010-01-23 18:06:47 +03:00
|
|
|
return;
|
|
|
|
fail:
|
|
|
|
aprint_error_dev(dv, "failed to initialize sysmon\n");
|
2010-01-31 09:45:09 +03:00
|
|
|
|
2010-01-23 18:06:47 +03:00
|
|
|
sysmon_envsys_destroy(sc->sc_sme);
|
2010-01-31 09:45:09 +03:00
|
|
|
kmem_free(sc->sc_sensor, ACPIBAT_COUNT * sizeof(*sc->sc_sensor));
|
|
|
|
|
2010-01-31 09:10:53 +03:00
|
|
|
sc->sc_sme = NULL;
|
2010-01-31 09:45:09 +03:00
|
|
|
sc->sc_sensor = NULL;
|
2002-12-31 08:26:56 +03:00
|
|
|
}
|
2008-06-03 19:02:31 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
acpibat_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
|
|
|
|
{
|
2011-01-13 16:05:13 +03:00
|
|
|
device_t self = sme->sme_cookie;
|
|
|
|
struct acpibat_softc *sc;
|
|
|
|
struct timeval tv, tmp;
|
2010-01-23 18:06:47 +03:00
|
|
|
ACPI_STATUS rv;
|
2008-06-03 19:02:31 +04:00
|
|
|
|
2011-01-13 16:05:13 +03:00
|
|
|
sc = device_private(self);
|
|
|
|
|
|
|
|
tmp.tv_sec = 10;
|
|
|
|
tmp.tv_usec = 0;
|
|
|
|
|
|
|
|
microtime(&tv);
|
|
|
|
timersub(&tv, &tmp, &tv);
|
|
|
|
if (timercmp(&tv, &sc->sc_last, <) != 0)
|
|
|
|
return;
|
|
|
|
|
2010-03-17 11:07:27 +03:00
|
|
|
if (mutex_tryenter(&sc->sc_mutex) == 0)
|
2010-01-23 18:06:47 +03:00
|
|
|
return;
|
|
|
|
|
2011-01-13 16:05:13 +03:00
|
|
|
rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, acpibat_update_status, self);
|
2010-01-23 18:06:47 +03:00
|
|
|
if (ACPI_SUCCESS(rv))
|
|
|
|
cv_timedwait(&sc->sc_condvar, &sc->sc_mutex, hz);
|
|
|
|
|
|
|
|
mutex_exit(&sc->sc_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2010-02-25 01:37:54 +03:00
|
|
|
acpibat_resume(device_t dv, const pmf_qual_t *qual)
|
2010-01-23 18:06:47 +03:00
|
|
|
{
|
|
|
|
|
2011-01-04 08:48:48 +03:00
|
|
|
(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpibat_update_info, dv);
|
|
|
|
(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpibat_update_status, dv);
|
2010-01-23 18:06:47 +03:00
|
|
|
|
|
|
|
return true;
|
2008-06-03 19:02:31 +04:00
|
|
|
}
|
2010-02-28 20:22:41 +03:00
|
|
|
|
2010-03-17 10:40:34 +03:00
|
|
|
static void
|
|
|
|
acpibat_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
|
|
|
|
sysmon_envsys_lim_t *limits, uint32_t *props)
|
|
|
|
{
|
|
|
|
device_t dv = sme->sme_cookie;
|
|
|
|
struct acpibat_softc *sc = device_private(dv);
|
|
|
|
|
|
|
|
if (edata->sensor != ACPIBAT_CAPACITY)
|
|
|
|
return;
|
|
|
|
|
|
|
|
limits->sel_critmin = sc->sc_lcapacity;
|
|
|
|
limits->sel_warnmin = sc->sc_wcapacity;
|
|
|
|
|
|
|
|
*props |= PROP_BATTCAP | PROP_BATTWARN | PROP_DRIVER_LIMITS;
|
|
|
|
}
|
|
|
|
|
2015-04-24 02:23:00 +03:00
|
|
|
MODULE(MODULE_CLASS_DRIVER, acpibat, "sysmon_envsys");
|
2010-02-28 20:22:41 +03:00
|
|
|
|
2011-02-16 12:05:12 +03:00
|
|
|
#ifdef _MODULE
|
2010-03-21 10:09:56 +03:00
|
|
|
#include "ioconf.c"
|
2011-02-16 12:05:12 +03:00
|
|
|
#endif
|
2010-02-28 20:22:41 +03:00
|
|
|
|
|
|
|
static int
|
2011-02-16 12:05:12 +03:00
|
|
|
acpibat_modcmd(modcmd_t cmd, void *aux)
|
2010-02-28 20:22:41 +03:00
|
|
|
{
|
2011-02-16 12:05:12 +03:00
|
|
|
int rv = 0;
|
2010-02-28 20:22:41 +03:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case MODULE_CMD_INIT:
|
2011-02-16 12:05:12 +03:00
|
|
|
#ifdef _MODULE
|
|
|
|
rv = config_init_component(cfdriver_ioconf_acpibat,
|
2010-03-26 18:51:55 +03:00
|
|
|
cfattach_ioconf_acpibat, cfdata_ioconf_acpibat);
|
2011-02-16 12:05:12 +03:00
|
|
|
#endif
|
|
|
|
break;
|
2010-02-28 20:22:41 +03:00
|
|
|
case MODULE_CMD_FINI:
|
2011-02-16 12:05:12 +03:00
|
|
|
#ifdef _MODULE
|
|
|
|
rv = config_fini_component(cfdriver_ioconf_acpibat,
|
2010-03-26 18:51:55 +03:00
|
|
|
cfattach_ioconf_acpibat, cfdata_ioconf_acpibat);
|
2011-02-16 12:05:12 +03:00
|
|
|
#endif
|
|
|
|
break;
|
2010-02-28 20:22:41 +03:00
|
|
|
default:
|
2011-02-16 12:05:12 +03:00
|
|
|
rv = ENOTTY;
|
2010-02-28 20:22:41 +03:00
|
|
|
}
|
|
|
|
|
2011-02-16 12:05:12 +03:00
|
|
|
return rv;
|
|
|
|
}
|