Example #1
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;
	if ((dev = devclass_get_device(acpi_cmbat_devclass,
			ioctl_arg->unit)) == NULL) {
		return (ENXIO);
	}

	if ((sc = device_get_softc(dev)) == 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->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;
		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;
	}

	return (0);
}
Example #2
0
static void
acpi_cmbat_init_battery(void *arg)
{
    struct acpi_cmbat_softc *sc;
    int		retry, valid;
    device_t	dev;

    dev = (device_t)arg;
    sc = device_get_softc(dev);
    ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
		"battery initialization start\n");

    /*
     * Try repeatedly to get valid data from the battery.  Since the
     * embedded controller isn't always ready just after boot, we may have
     * to wait a while.
     */
    for (retry = 0; retry < ACPI_CMBAT_RETRY_MAX; retry++, AcpiOsSleep(10000)) {
	/* batteries on DOCK can be ejected w/ DOCK during retrying */
	if (!device_is_attached(dev))
	    return;

	if (!acpi_BatteryIsPresent(dev))
	    continue;

	/*
	 * Only query the battery if this is the first try or the specific
	 * type of info is still invalid.
	 */
	ACPI_SERIAL_BEGIN(cmbat);
	if (retry == 0 || !acpi_battery_bst_valid(&sc->bst)) {
	    timespecclear(&sc->bst_lastupdated);
	    acpi_cmbat_get_bst(dev);
	}
	if (retry == 0 || !acpi_battery_bif_valid(&sc->bif))
	    acpi_cmbat_get_bif(dev);

	valid = acpi_battery_bst_valid(&sc->bst) &&
	    acpi_battery_bif_valid(&sc->bif);
	ACPI_SERIAL_END(cmbat);

	if (valid)
	    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);
    }
}
Example #3
0
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;
}
Example #4
0
static int
acpi_cmbat_bst(device_t dev, struct acpi_bst *bstp)
{
    struct acpi_cmbat_softc *sc;

    sc = device_get_softc(dev);

    ACPI_SERIAL_BEGIN(cmbat);
    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;
    ACPI_SERIAL_END(cmbat);

    return (0);
}
Example #5
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);
}
/* Get info about one or all batteries. */
int
acpi_battery_get_battinfo(device_t dev, struct acpi_battinfo *battinfo)
{
    int	batt_stat, devcount, dev_idx, error, i;
    int total_cap, total_min, valid_rate, valid_units;
    devclass_t batt_dc;
    device_t batt_dev;
    struct acpi_bst *bst;
    struct acpi_bif *bif;
    struct acpi_battinfo *bi;

    /*
     * Get the battery devclass and max unit for battery devices.  If there
     * are none or error, return immediately.
     */
    batt_dc = devclass_find("battery");
    if (batt_dc == NULL)
	return (ENXIO);
    devcount = devclass_get_maxunit(batt_dc);
    if (devcount == 0)
	return (ENXIO);

    /*
     * Allocate storage for all _BST data, their derived battinfo data,
     * and the current battery's _BIF data.
     */
    bst = kmalloc(devcount * sizeof(*bst), M_TEMP, M_WAITOK | M_ZERO);
    bi = kmalloc(devcount * sizeof(*bi), M_TEMP, M_WAITOK | M_ZERO);
    bif = kmalloc(sizeof(*bif), M_TEMP, M_WAITOK | M_ZERO);

    /*
     * Pass 1:  for each battery that is present and valid, get its status,
     * calculate percent capacity remaining, and sum all the current
     * discharge rates.
     */
    dev_idx = -1;
    batt_stat = valid_rate = valid_units = 0;
    for (i = 0; i < devcount; i++) {
	/* Default info for every battery is "not present". */
	acpi_reset_battinfo(&bi[i]);

	/*
	 * Find the device.  Since devcount is in terms of max units, this
	 * may be a sparse array so skip devices that aren't present.
	 */
	batt_dev = devclass_get_device(batt_dc, i);
	if (batt_dev == NULL)
	    continue;

	/* If examining a specific battery and this is it, record its index. */
	if (dev != NULL && dev == batt_dev)
	    dev_idx = i;

	/*
	 * Be sure we can get various info from the battery.  Note that
	 * acpi_BatteryIsPresent() is not enough because smart batteries only
	 * return that the device is present.
	 */
	if (!acpi_BatteryIsPresent(batt_dev) ||
	    ACPI_BATT_GET_STATUS(batt_dev, &bst[i]) != 0 ||
	    ACPI_BATT_GET_INFO(batt_dev, bif) != 0)
	    continue;

	/* If a battery is not installed, we sometimes get strange values. */
	if (!acpi_battery_bst_valid(&bst[i]) ||
	    !acpi_battery_bif_valid(bif))
	    continue;

	/*
	 * Record current state.  If both charging and discharging are set,
	 * ignore the charging flag.
	 */
	valid_units++;
	if ((bst[i].state & ACPI_BATT_STAT_DISCHARG) != 0)
	    bst[i].state &= ~ACPI_BATT_STAT_CHARGING;
	batt_stat |= bst[i].state;
	bi[i].state = bst[i].state;

	/*
	 * If the battery info is in terms of mA, convert to mW by
	 * multiplying by the design voltage.  If the design voltage
	 * is 0 (due to some error reading the battery), skip this
	 * conversion.
	 */
	if (bif->units == ACPI_BIF_UNITS_MA && bif->dvol != 0 && dev == NULL) {
	    bst[i].rate = (bst[i].rate * bif->dvol) / 1000;
	    bst[i].cap = (bst[i].cap * bif->dvol) / 1000;
	    bif->lfcap = (bif->lfcap * bif->dvol) / 1000;
	}

	/*
	 * The calculation above may set bif->lfcap to zero. This was
	 * seen on a laptop with a broken battery. The result of the
	 * division was rounded to zero.
	 */
	if (!acpi_battery_bif_valid(bif))
	    continue;

	/* Calculate percent capacity remaining. */
	bi[i].cap = (100 * bst[i].cap) / bif->lfcap;

	/*
	 * Some laptops report the "design-capacity" instead of the
	 * "real-capacity" when the battery is fully charged.  That breaks
	 * the above arithmetic as it needs to be 100% maximum.
	 */
	if (bi[i].cap > 100)
	    bi[i].cap = 100;

	/*
	 * On systems with more than one battery, they may get used
	 * sequentially, thus bst.rate may only signify the one currently
	 * in use.  For the remaining batteries, bst.rate will be zero,
	 * which makes it impossible to calculate the total remaining time.
	 * Therefore, we sum the bst.rate for batteries in the discharging
	 * state and use the sum to calculate the total remaining time.
	 */
	if (bst[i].rate != ACPI_BATT_UNKNOWN &&
	    (bst[i].state & ACPI_BATT_STAT_DISCHARG) != 0)
	    valid_rate += bst[i].rate;

	/*
	 * Some DSDTs report a negative 16-bit value for the rate and/or
	 * report 0 as 65536.
	 */
	if (acpi_quirks & ACPI_Q_BATT_RATE_ABS &&
	    bif->units == ACPI_BIF_UNITS_MA &&
	    bst[i].rate != ACPI_BATT_UNKNOWN &&
	    (int16_t)bst[i].rate < 0)
		bst[i].rate = abs((int16_t)bst[i].rate);
    }

    /* If the caller asked for a device but we didn't find it, error. */
    if (dev != NULL && dev_idx == -1) {
	error = ENXIO;
	goto out;
    }

    /* Pass 2:  calculate capacity and remaining time for all batteries. */
    total_cap = total_min = 0;
    for (i = 0; i < devcount; i++) {
	/*
	 * If any batteries are discharging, use the sum of the bst.rate
	 * values.  Otherwise, we are on AC power, and there is infinite
	 * time remaining for this battery until we go offline.
	 */
	if (valid_rate > 0)
	    bi[i].min = (60 * bst[i].cap) / valid_rate;
	else
	    bi[i].min = 0;
	total_min += bi[i].min;

	/* If this battery is not present, don't use its capacity. */
	if (bi[i].cap != -1)
	    total_cap += bi[i].cap;
    }

    /*
     * Return total battery percent and time remaining.  If there are
     * no valid batteries, report values as unknown.
     */
    if (valid_units > 0) {
	if (dev == NULL) {
	    battinfo->cap = total_cap / valid_units;
	    battinfo->min = total_min;
	    battinfo->state = batt_stat;
	    battinfo->rate = valid_rate;
	} else {
	    battinfo->cap = bi[dev_idx].cap;
	    battinfo->min = bi[dev_idx].min;
	    battinfo->state = bi[dev_idx].state;
	    battinfo->rate = bst[dev_idx].rate;
	}

	/*
	 * If the queried battery has no discharge rate or is charging,
	 * report that we don't know the remaining time.
	 */
	if (valid_rate == 0 || (battinfo->state & ACPI_BATT_STAT_CHARGING))
	    battinfo->min = -1;
    } else
	acpi_reset_battinfo(battinfo);

    error = 0;

out:
    if (bi)
	kfree(bi, M_TEMP);
    if (bif)
	kfree(bif, M_TEMP);
    if (bst)
	kfree(bst, M_TEMP);
    return (error);
}