Esempio n. 1
0
static void
acpi_cmbat_get_bst(void *arg)
{
    struct acpi_cmbat_softc *sc;
    ACPI_STATUS	as;
    ACPI_OBJECT	*res;
    ACPI_HANDLE	h;
    ACPI_BUFFER	bst_buffer;
    device_t dev;

    ACPI_SERIAL_ASSERT(cmbat);

    dev = arg;
    sc = device_get_softc(dev);
    h = acpi_get_handle(dev);
    bst_buffer.Pointer = NULL;
    bst_buffer.Length = ACPI_ALLOCATE_BUFFER;

    if (!acpi_cmbat_info_expired(&sc->bst_lastupdated))
	goto end;

    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);

    /* XXX If all batteries are critical, perhaps we should suspend. */
    if (sc->bst.state & ACPI_BATT_STAT_CRITICAL) {
    	if ((sc->flags & ACPI_BATT_STAT_CRITICAL) == 0) {
	    sc->flags |= ACPI_BATT_STAT_CRITICAL;
	    device_printf(dev, "critically low charge!\n");
	}
    } else
	sc->flags &= ~ACPI_BATT_STAT_CRITICAL;

end:
    if (bst_buffer.Pointer != NULL)
	AcpiOsFree(bst_buffer.Pointer);
}
Esempio n. 2
0
static void
acpi_cmbat_get_bif(void *context)
{
	device_t	dev;
	struct acpi_cmbat_softc *sc;
	ACPI_STATUS	as;
	ACPI_OBJECT	*res, *tmp;
	ACPI_HANDLE	h;
	ACPI_BUFFER	bif_buffer;

	dev = context;
	sc = device_get_softc(dev);
	h = acpi_get_handle(dev);
	bif_buffer.Pointer = NULL;

	if (!acpi_cmbat_info_expired(&sc->bif_lastupdated)) {
		return;
	}

	if (sc->bif_updating) {
		return;
	}
	sc->bif_updating = 1;

	bif_buffer.Length = ACPI_ALLOCATE_BUFFER;
	if (ACPI_FAILURE(as = AcpiEvaluateObject(h, "_BIF", NULL, &bif_buffer))) {
		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 ((res == NULL) || (res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 13)) {
		ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
		    "battery info corrupted\n");
		goto end;
	}

	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);
	acpi_cmbat_info_updated(&sc->bif_lastupdated);
end:
	if (bif_buffer.Pointer != NULL)
		AcpiOsFree(bif_buffer.Pointer);
	sc->bif_updating = 0;
}
Esempio n. 3
0
static void
acpi_cmbat_get_bst(void *context)
{
	device_t	dev;
	struct acpi_cmbat_softc *sc;
	ACPI_STATUS	as;
	ACPI_OBJECT	*res, *tmp;
	ACPI_HANDLE	h;
	ACPI_BUFFER	bst_buffer;

	dev = context;
	sc = device_get_softc(dev);
	h = acpi_get_handle(dev);
	bst_buffer.Pointer = NULL;

	if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) {
		return;
	}

	if (sc->bst_updating) {
		return;
	}
	sc->bst_updating = 1;

	bst_buffer.Length = ACPI_ALLOCATE_BUFFER;
	if (ACPI_FAILURE(as = AcpiEvaluateObject(h, "_BST", NULL, &bst_buffer))) {
		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 ((res == NULL) || (res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 4)) {
		ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
		    "battery status corrupted\n");
		goto end;
	}

	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);
	acpi_cmbat_info_updated(&sc->bst_lastupdated);
end:
	if (bst_buffer.Pointer != NULL)
		AcpiOsFree(bst_buffer.Pointer);
	sc->bst_updating = 0;
}
Esempio n. 4
0
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 battey 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);
}