/*-
* 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.
*/
/*
* 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.
*/
/*
* 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");
}
/* 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. */
pmf_device_deregister(self);
/* Prevent further calls to acpibat_notify_handler. */
acpi_deregister_notify(sc->sc_node);
/* Detach sensors and prevent further calls to acpibat_refresh. */
if (sc->sc_sme != NULL)
sysmon_envsys_unregister(sc->sc_sme);
/*
* 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();
if (sc->sc_sensor != NULL)
kmem_free(sc->sc_sensor, ACPIBAT_COUNT *
sizeof(*sc->sc_sensor));
/*
* acpibat_get_sta:
*
* Evaluate whether the battery is present or absent.
*
* Returns: 0 for no battery, 1 for present, and -1 on error.
*/
static int
acpibat_get_sta(device_t dv)
{
struct acpibat_softc *sc = device_private(dv);
ACPI_INTEGER val;
ACPI_STATUS rv;
rv = acpi_eval_integer(sc->sc_node->ad_handle, "_STA", &val);
if (ACPI_FAILURE(rv)) {
aprint_error_dev(dv, "failed to evaluate _STA: %s\n",
AcpiFormatException(rv));
return -1;
}
/* Design capacity. */
val = elm[ACPIBAT_BIF_DCAPACITY].Integer.Value;
sc->sc_sensor[ACPIBAT_DCAPACITY].value_cur = val * 1000;
sc->sc_sensor[ACPIBAT_DCAPACITY].state = ACPIBAT_VAL_ISVALID(val);
/* Last full charge capacity. */
val = elm[ACPIBAT_BIF_LFCCAPACITY].Integer.Value;
sc->sc_sensor[ACPIBAT_LFCCAPACITY].value_cur = val * 1000;
sc->sc_sensor[ACPIBAT_LFCCAPACITY].state = ACPIBAT_VAL_ISVALID(val);
/* Design voltage. */
val = elm[ACPIBAT_BIF_DVOLTAGE].Integer.Value;
sc->sc_sensor[ACPIBAT_DVOLTAGE].value_cur = val * 1000;
sc->sc_sensor[ACPIBAT_DVOLTAGE].state = ACPIBAT_VAL_ISVALID(val);
/*
* Initialize the maximum of current capacity
* to the last known full charge capacity.
*/
val = sc->sc_sensor[ACPIBAT_LFCCAPACITY].value_cur;
sc->sc_sensor[ACPIBAT_CAPACITY].value_max = val;
acpibat_print_info(dv, elm);
out:
if (obj != NULL)
ACPI_FREE(obj);
if (ACPI_FAILURE(rv))
aprint_error_dev(dv, "failed to evaluate _BIF: %s\n",
AcpiFormatException(rv));
}
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;
if (elm[i].String.Pointer[0] == '\0')
return;
}
/*
* 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]. [...]"
*/
switch (elm[ACPIBAT_BIF_UNIT].Integer.Value) {
case ACPIBAT_PWRUNIT_MA:
unit = "Ah";
break;
default:
unit = "Wh";
break;
}
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;
}
}