2000-08-31 14:41:23 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2000-08-31 14:41:23 +00:00
|
|
|
* Copyright (c) 1999 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.
|
|
|
|
*
|
|
|
|
* $Id: acpiconf.c,v 1.5 2000/08/08 14:12:19 iwasaki Exp $
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2000-09-30 20:13:57 +00:00
|
|
|
#include <sys/ioctl.h>
|
2001-11-30 11:35:01 +00:00
|
|
|
#include <sysexits.h>
|
2000-08-31 14:41:23 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2000-11-08 02:37:00 +00:00
|
|
|
#include <dev/acpica/acpiio.h>
|
2009-06-05 18:50:45 +00:00
|
|
|
|
|
|
|
#include <contrib/dev/acpica/include/acpi.h>
|
2000-09-30 20:13:57 +00:00
|
|
|
|
2003-12-30 17:28:06 +00:00
|
|
|
#define ACPIDEV "/dev/acpi"
|
2000-08-31 14:41:23 +00:00
|
|
|
|
2003-09-13 20:13:01 +00:00
|
|
|
static int acpifd;
|
2004-03-05 02:48:22 +00:00
|
|
|
|
2004-11-13 15:16:35 +00:00
|
|
|
static void
|
2004-11-13 01:54:22 +00:00
|
|
|
acpi_init(void)
|
2000-08-31 14:41:23 +00:00
|
|
|
{
|
2003-09-13 20:13:01 +00:00
|
|
|
acpifd = open(ACPIDEV, O_RDWR);
|
2004-11-16 06:25:02 +00:00
|
|
|
if (acpifd == -1)
|
2004-03-04 17:03:49 +00:00
|
|
|
acpifd = open(ACPIDEV, O_RDONLY);
|
2004-11-16 06:25:02 +00:00
|
|
|
if (acpifd == -1)
|
2001-11-30 11:35:01 +00:00
|
|
|
err(EX_OSFILE, ACPIDEV);
|
2003-09-13 20:13:01 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 22:50:37 +00:00
|
|
|
/* Prepare to sleep and then wait for the signal that sleeping can occur. */
|
|
|
|
static void
|
2000-08-31 14:41:23 +00:00
|
|
|
acpi_sleep(int sleep_type)
|
|
|
|
{
|
2003-12-30 17:28:06 +00:00
|
|
|
int ret;
|
2007-06-21 22:50:37 +00:00
|
|
|
|
|
|
|
/* Notify OS that we want to sleep. devd(8) gets this notify. */
|
|
|
|
ret = ioctl(acpifd, ACPIIO_REQSLPSTATE, &sleep_type);
|
|
|
|
if (ret != 0)
|
|
|
|
err(EX_IOERR, "request sleep type (%d) failed", sleep_type);
|
|
|
|
}
|
2003-12-30 17:28:06 +00:00
|
|
|
|
2007-06-21 22:50:37 +00:00
|
|
|
/* Ack or abort a pending suspend request. */
|
|
|
|
static void
|
|
|
|
acpi_sleep_ack(int err_val)
|
|
|
|
{
|
|
|
|
int ret;
|
2003-12-30 17:28:06 +00:00
|
|
|
|
2007-06-21 22:50:37 +00:00
|
|
|
ret = ioctl(acpifd, ACPIIO_ACKSLPSTATE, &err_val);
|
2003-12-30 17:28:06 +00:00
|
|
|
if (ret != 0)
|
2007-06-21 22:50:37 +00:00
|
|
|
err(EX_IOERR, "ack sleep type failed");
|
2003-09-13 20:13:01 +00:00
|
|
|
}
|
|
|
|
|
2004-11-24 16:02:20 +00:00
|
|
|
/* should be a acpi define, but doesn't appear to be */
|
|
|
|
#define UNKNOWN_CAP 0xffffffff
|
2005-07-28 19:31:28 +00:00
|
|
|
#define UNKNOWN_VOLTAGE 0xffffffff
|
2004-11-24 16:02:20 +00:00
|
|
|
|
2003-09-13 20:13:01 +00:00
|
|
|
static int
|
|
|
|
acpi_battinfo(int num)
|
|
|
|
{
|
|
|
|
union acpi_battery_ioctl_arg battio;
|
|
|
|
const char *pwr_units;
|
2010-08-24 18:07:59 +00:00
|
|
|
int hours, min, amp;
|
|
|
|
uint32_t volt;
|
2003-09-13 20:13:01 +00:00
|
|
|
|
|
|
|
if (num < 0 || num > 64)
|
2017-12-09 15:59:10 +00:00
|
|
|
errx(EX_USAGE, "invalid battery %d", num);
|
2003-09-13 20:13:01 +00:00
|
|
|
|
2005-07-28 19:31:28 +00:00
|
|
|
/* Print battery design information. */
|
2003-09-13 20:13:01 +00:00
|
|
|
battio.unit = num;
|
2020-02-19 06:28:55 +00:00
|
|
|
if (ioctl(acpifd, ACPIIO_BATT_GET_BIX, &battio) == -1)
|
2003-09-13 20:13:01 +00:00
|
|
|
err(EX_IOERR, "get battery info (%d) failed", num);
|
2020-02-19 06:28:55 +00:00
|
|
|
amp = battio.bix.units;
|
2010-08-24 18:07:59 +00:00
|
|
|
pwr_units = amp ? "mA" : "mW";
|
2020-02-19 06:28:55 +00:00
|
|
|
if (battio.bix.dcap == UNKNOWN_CAP)
|
2005-07-28 19:31:28 +00:00
|
|
|
printf("Design capacity:\tunknown\n");
|
2004-11-24 16:02:20 +00:00
|
|
|
else
|
2020-02-19 06:28:55 +00:00
|
|
|
printf("Design capacity:\t%d %sh\n", battio.bix.dcap,
|
2005-07-28 19:31:28 +00:00
|
|
|
pwr_units);
|
2020-02-19 06:28:55 +00:00
|
|
|
if (battio.bix.lfcap == UNKNOWN_CAP)
|
2005-07-28 19:31:28 +00:00
|
|
|
printf("Last full capacity:\tunknown\n");
|
2004-11-24 16:02:20 +00:00
|
|
|
else
|
2020-02-19 06:28:55 +00:00
|
|
|
printf("Last full capacity:\t%d %sh\n", battio.bix.lfcap,
|
2004-11-24 16:02:20 +00:00
|
|
|
pwr_units);
|
2020-02-19 06:28:55 +00:00
|
|
|
printf("Technology:\t\t%s\n", battio.bix.btech == 0 ?
|
2003-09-13 20:13:01 +00:00
|
|
|
"primary (non-rechargeable)" : "secondary (rechargeable)");
|
2020-02-19 06:28:55 +00:00
|
|
|
if (ACPI_BIX_REV_MIN_CHECK(battio.bix.rev, ACPI_BIX_REV_1)) {
|
|
|
|
printf("Battery Swappable Capability:\t");
|
|
|
|
if (battio.bix.scap == ACPI_BIX_SCAP_NO)
|
|
|
|
printf("Non-swappable\n");
|
|
|
|
else if (battio.bix.scap == ACPI_BIX_SCAP_COLD)
|
|
|
|
printf("cold swap\n");
|
|
|
|
else if (battio.bix.scap == ACPI_BIX_SCAP_HOT)
|
|
|
|
printf("hot swap\n");
|
|
|
|
else
|
|
|
|
printf("unknown\n");
|
|
|
|
}
|
|
|
|
if (battio.bix.dvol == UNKNOWN_CAP)
|
2005-07-28 19:31:28 +00:00
|
|
|
printf("Design voltage:\t\tunknown\n");
|
2004-11-24 16:02:20 +00:00
|
|
|
else
|
2020-02-19 06:28:55 +00:00
|
|
|
printf("Design voltage:\t\t%d mV\n", battio.bix.dvol);
|
|
|
|
printf("Capacity (warn):\t%d %sh\n", battio.bix.wcap, pwr_units);
|
|
|
|
printf("Capacity (low):\t\t%d %sh\n", battio.bix.lcap, pwr_units);
|
|
|
|
if (ACPI_BIX_REV_MIN_CHECK(battio.bix.rev, ACPI_BIX_REV_0)) {
|
|
|
|
if (battio.bix.cycles != ACPI_BATT_UNKNOWN)
|
|
|
|
printf("Cycle Count:\t\t%d\n", battio.bix.cycles);
|
|
|
|
printf("Mesurement Accuracy:\t%d %%\n",
|
|
|
|
battio.bix.accuracy / 1000);
|
|
|
|
if (battio.bix.stmax != ACPI_BATT_UNKNOWN)
|
|
|
|
printf("Max Sampling Time:\t%d ms\n",
|
|
|
|
battio.bix.stmax);
|
|
|
|
if (battio.bix.stmin != ACPI_BATT_UNKNOWN)
|
|
|
|
printf("Min Sampling Time:\t%d ms\n",
|
|
|
|
battio.bix.stmin);
|
|
|
|
printf("Max Average Interval:\t%d ms\n",
|
|
|
|
battio.bix.aimax);
|
|
|
|
printf("Min Average Interval:\t%d ms\n",
|
|
|
|
battio.bix.aimin);
|
|
|
|
}
|
|
|
|
printf("Low/warn granularity:\t%d %sh\n", battio.bix.gra1, pwr_units);
|
|
|
|
printf("Warn/full granularity:\t%d %sh\n", battio.bix.gra2, pwr_units);
|
|
|
|
printf("Model number:\t\t%s\n", battio.bix.model);
|
|
|
|
printf("Serial number:\t\t%s\n", battio.bix.serial);
|
|
|
|
printf("Type:\t\t\t%s\n", battio.bix.type);
|
|
|
|
printf("OEM info:\t\t%s\n", battio.bix.oeminfo);
|
2000-08-31 14:41:23 +00:00
|
|
|
|
2010-08-24 18:07:59 +00:00
|
|
|
/* Fetch battery voltage information. */
|
|
|
|
volt = UNKNOWN_VOLTAGE;
|
|
|
|
battio.unit = num;
|
|
|
|
if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1)
|
|
|
|
err(EX_IOERR, "get battery status (%d) failed", num);
|
|
|
|
if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT)
|
|
|
|
volt = battio.bst.volt;
|
|
|
|
|
2005-07-28 19:31:28 +00:00
|
|
|
/* Print current battery state information. */
|
2004-11-24 12:49:39 +00:00
|
|
|
battio.unit = num;
|
2005-07-28 19:31:28 +00:00
|
|
|
if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1)
|
|
|
|
err(EX_IOERR, "get battery user info (%d) failed", num);
|
|
|
|
if (battio.battinfo.state != ACPI_BATT_STAT_NOT_PRESENT) {
|
2015-04-06 21:51:55 +00:00
|
|
|
const char *state;
|
|
|
|
switch (battio.battinfo.state & ACPI_BATT_STAT_BST_MASK) {
|
|
|
|
case 0:
|
|
|
|
state = "high";
|
|
|
|
break;
|
|
|
|
case ACPI_BATT_STAT_DISCHARG:
|
|
|
|
state = "discharging";
|
|
|
|
break;
|
|
|
|
case ACPI_BATT_STAT_CHARGING:
|
|
|
|
state = "charging";
|
|
|
|
break;
|
|
|
|
case ACPI_BATT_STAT_CRITICAL:
|
|
|
|
state = "critical";
|
|
|
|
break;
|
|
|
|
case ACPI_BATT_STAT_DISCHARG | ACPI_BATT_STAT_CRITICAL:
|
|
|
|
state = "critical discharging";
|
|
|
|
break;
|
|
|
|
case ACPI_BATT_STAT_CHARGING | ACPI_BATT_STAT_CRITICAL:
|
|
|
|
state = "critical charging";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
state = "invalid";
|
|
|
|
}
|
|
|
|
printf("State:\t\t\t%s\n", state);
|
2005-07-28 19:31:28 +00:00
|
|
|
if (battio.battinfo.cap == -1)
|
|
|
|
printf("Remaining capacity:\tunknown\n");
|
2004-11-24 16:02:20 +00:00
|
|
|
else
|
2005-07-28 19:31:28 +00:00
|
|
|
printf("Remaining capacity:\t%d%%\n",
|
|
|
|
battio.battinfo.cap);
|
|
|
|
if (battio.battinfo.min == -1)
|
2005-07-28 19:41:52 +00:00
|
|
|
printf("Remaining time:\t\tunknown\n");
|
2005-07-28 19:31:28 +00:00
|
|
|
else {
|
|
|
|
hours = battio.battinfo.min / 60;
|
|
|
|
min = battio.battinfo.min % 60;
|
|
|
|
printf("Remaining time:\t\t%d:%02d\n", hours, min);
|
|
|
|
}
|
|
|
|
if (battio.battinfo.rate == -1)
|
|
|
|
printf("Present rate:\t\tunknown\n");
|
2010-08-24 18:07:59 +00:00
|
|
|
else if (amp && volt != UNKNOWN_VOLTAGE) {
|
|
|
|
printf("Present rate:\t\t%d mA (%d mW)\n",
|
|
|
|
battio.battinfo.rate,
|
|
|
|
battio.battinfo.rate * volt / 1000);
|
|
|
|
} else
|
2005-07-28 19:31:28 +00:00
|
|
|
printf("Present rate:\t\t%d %s\n",
|
|
|
|
battio.battinfo.rate, pwr_units);
|
|
|
|
} else
|
|
|
|
printf("State:\t\t\tnot present\n");
|
|
|
|
|
|
|
|
/* Print battery voltage information. */
|
2010-08-24 18:07:59 +00:00
|
|
|
if (volt == UNKNOWN_VOLTAGE)
|
|
|
|
printf("Present voltage:\tunknown\n");
|
|
|
|
else
|
|
|
|
printf("Present voltage:\t%d mV\n", volt);
|
2005-07-28 19:31:28 +00:00
|
|
|
|
2000-08-31 14:41:23 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2000-11-08 02:37:00 +00:00
|
|
|
static void
|
|
|
|
usage(const char* prog)
|
|
|
|
{
|
2007-06-21 22:50:37 +00:00
|
|
|
printf("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog);
|
2000-11-08 02:37:00 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2000-08-31 14:41:23 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2017-12-09 15:59:10 +00:00
|
|
|
char *prog, *end;
|
|
|
|
int c, sleep_type, battery, ack;
|
|
|
|
int iflag = 0, kflag = 0, sflag = 0;
|
2000-08-31 14:41:23 +00:00
|
|
|
|
2000-11-08 02:37:00 +00:00
|
|
|
prog = argv[0];
|
2003-09-13 20:13:01 +00:00
|
|
|
if (argc < 2)
|
|
|
|
usage(prog);
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
2000-08-31 14:41:23 +00:00
|
|
|
sleep_type = -1;
|
2003-09-13 20:13:01 +00:00
|
|
|
acpi_init();
|
2007-06-21 22:50:37 +00:00
|
|
|
while ((c = getopt(argc, argv, "hi:k:s:")) != -1) {
|
2000-08-31 14:41:23 +00:00
|
|
|
switch (c) {
|
2003-09-13 20:13:01 +00:00
|
|
|
case 'i':
|
2017-12-09 15:59:10 +00:00
|
|
|
iflag = 1;
|
|
|
|
battery = strtol(optarg, &end, 10);
|
|
|
|
if ((size_t)(end - optarg) != strlen(optarg))
|
|
|
|
errx(EX_USAGE, "invalid battery");
|
2003-09-13 20:13:01 +00:00
|
|
|
break;
|
2007-06-21 22:50:37 +00:00
|
|
|
case 'k':
|
2017-12-09 15:59:10 +00:00
|
|
|
kflag = 1;
|
|
|
|
ack = strtol(optarg, &end, 10);
|
|
|
|
if ((size_t)(end - optarg) != strlen(optarg))
|
|
|
|
errx(EX_USAGE, "invalid ack argument");
|
2007-06-21 22:50:37 +00:00
|
|
|
break;
|
2000-08-31 14:41:23 +00:00
|
|
|
case 's':
|
2017-12-09 15:59:10 +00:00
|
|
|
sflag = 1;
|
2003-07-28 16:22:45 +00:00
|
|
|
if (optarg[0] == 'S')
|
2017-12-09 15:59:10 +00:00
|
|
|
optarg++;
|
|
|
|
sleep_type = strtol(optarg, &end, 10);
|
|
|
|
if ((size_t)(end - optarg) != strlen(optarg))
|
|
|
|
errx(EX_USAGE, "invalid sleep type");
|
2007-06-21 22:50:37 +00:00
|
|
|
if (sleep_type < 1 || sleep_type > 4)
|
2001-11-30 11:35:01 +00:00
|
|
|
errx(EX_USAGE, "invalid sleep type (%d)",
|
2003-09-13 20:13:01 +00:00
|
|
|
sleep_type);
|
2000-08-31 14:41:23 +00:00
|
|
|
break;
|
2003-09-13 20:13:01 +00:00
|
|
|
case 'h':
|
2000-08-31 14:41:23 +00:00
|
|
|
default:
|
2003-09-13 20:13:01 +00:00
|
|
|
usage(prog);
|
|
|
|
/* NOTREACHED */
|
2000-08-31 14:41:23 +00:00
|
|
|
}
|
|
|
|
}
|
2003-09-13 20:13:01 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2000-08-31 14:41:23 +00:00
|
|
|
|
2017-12-09 15:59:10 +00:00
|
|
|
if (iflag != 0 && kflag != 0 && sflag != 0)
|
|
|
|
errx(EX_USAGE, "-i, -k and -s are mutually exclusive");
|
|
|
|
|
|
|
|
if (iflag != 0) {
|
|
|
|
if (kflag != 0)
|
|
|
|
errx(EX_USAGE, "-i and -k are mutually exclusive");
|
|
|
|
if (sflag != 0)
|
|
|
|
errx(EX_USAGE, "-i and -s are mutually exclusive");
|
|
|
|
acpi_battinfo(battery);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kflag != 0) {
|
|
|
|
if (sflag != 0)
|
|
|
|
errx(EX_USAGE, "-k and -s are mutually exclusive");
|
|
|
|
acpi_sleep_ack(ack);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (sflag != 0)
|
2000-08-31 14:41:23 +00:00
|
|
|
acpi_sleep(sleep_type);
|
2003-09-13 20:13:01 +00:00
|
|
|
|
|
|
|
close(acpifd);
|
|
|
|
exit (0);
|
2000-08-31 14:41:23 +00:00
|
|
|
}
|