12e6c5fc5SMitsuru IWASAKI /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
42e6c5fc5SMitsuru IWASAKI * Copyright (c) 1999 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
52e6c5fc5SMitsuru IWASAKI * All rights reserved.
62e6c5fc5SMitsuru IWASAKI *
72e6c5fc5SMitsuru IWASAKI * Redistribution and use in source and binary forms, with or without
82e6c5fc5SMitsuru IWASAKI * modification, are permitted provided that the following conditions
92e6c5fc5SMitsuru IWASAKI * are met:
102e6c5fc5SMitsuru IWASAKI * 1. Redistributions of source code must retain the above copyright
112e6c5fc5SMitsuru IWASAKI * notice, this list of conditions and the following disclaimer.
122e6c5fc5SMitsuru IWASAKI * 2. Redistributions in binary form must reproduce the above copyright
132e6c5fc5SMitsuru IWASAKI * notice, this list of conditions and the following disclaimer in the
142e6c5fc5SMitsuru IWASAKI * documentation and/or other materials provided with the distribution.
152e6c5fc5SMitsuru IWASAKI *
162e6c5fc5SMitsuru IWASAKI * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172e6c5fc5SMitsuru IWASAKI * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182e6c5fc5SMitsuru IWASAKI * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192e6c5fc5SMitsuru IWASAKI * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202e6c5fc5SMitsuru IWASAKI * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212e6c5fc5SMitsuru IWASAKI * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222e6c5fc5SMitsuru IWASAKI * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232e6c5fc5SMitsuru IWASAKI * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242e6c5fc5SMitsuru IWASAKI * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252e6c5fc5SMitsuru IWASAKI * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262e6c5fc5SMitsuru IWASAKI * SUCH DAMAGE.
272e6c5fc5SMitsuru IWASAKI *
282e6c5fc5SMitsuru IWASAKI * $Id: acpiconf.c,v 1.5 2000/08/08 14:12:19 iwasaki Exp $
292e6c5fc5SMitsuru IWASAKI */
302e6c5fc5SMitsuru IWASAKI
312e6c5fc5SMitsuru IWASAKI #include <sys/param.h>
322e6c5fc5SMitsuru IWASAKI
332e6c5fc5SMitsuru IWASAKI #include <err.h>
342e6c5fc5SMitsuru IWASAKI #include <fcntl.h>
352e6c5fc5SMitsuru IWASAKI #include <stdio.h>
36e434fd9bSMike Smith #include <sys/ioctl.h>
3728f49c6dSCrist J. Clark #include <sysexits.h>
382e6c5fc5SMitsuru IWASAKI #include <unistd.h>
392e6c5fc5SMitsuru IWASAKI
40d3f4a2caSMitsuru IWASAKI #include <dev/acpica/acpiio.h>
4199065116SJung-uk Kim
4299065116SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
43e434fd9bSMike Smith
442e6c5fc5SMitsuru IWASAKI #define ACPIDEV "/dev/acpi"
452e6c5fc5SMitsuru IWASAKI
4664fdad23SNate Lawson static int acpifd;
478aabbec2STakanori Watanabe
486af2c48aSPhilip Paeps static void
acpi_init(void)49413efd91SPhilip Paeps acpi_init(void)
5064fdad23SNate Lawson {
5164fdad23SNate Lawson acpifd = open(ACPIDEV, O_RDWR);
5246433fccSWarner Losh if (acpifd == -1)
53fe27a95eSTakanori Watanabe acpifd = open(ACPIDEV, O_RDONLY);
5446433fccSWarner Losh if (acpifd == -1)
5564fdad23SNate Lawson err(EX_OSFILE, ACPIDEV);
5664fdad23SNate Lawson }
5764fdad23SNate Lawson
5800a30448SNate Lawson /* Prepare to sleep and then wait for the signal that sleeping can occur. */
5900a30448SNate Lawson static void
acpi_sleep(int sleep_type)602e6c5fc5SMitsuru IWASAKI acpi_sleep(int sleep_type)
612e6c5fc5SMitsuru IWASAKI {
62e776370eSNate Lawson int ret;
63e776370eSNate Lawson
6400a30448SNate Lawson /* Notify OS that we want to sleep. devd(8) gets this notify. */
6500a30448SNate Lawson ret = ioctl(acpifd, ACPIIO_REQSLPSTATE, &sleep_type);
66e776370eSNate Lawson if (ret != 0)
6700a30448SNate Lawson err(EX_IOERR, "request sleep type (%d) failed", sleep_type);
6800a30448SNate Lawson }
6964fdad23SNate Lawson
7000a30448SNate Lawson /* Ack or abort a pending suspend request. */
7100a30448SNate Lawson static void
acpi_sleep_ack(int err_val)7200a30448SNate Lawson acpi_sleep_ack(int err_val)
7300a30448SNate Lawson {
7400a30448SNate Lawson int ret;
7500a30448SNate Lawson
7600a30448SNate Lawson ret = ioctl(acpifd, ACPIIO_ACKSLPSTATE, &err_val);
7700a30448SNate Lawson if (ret != 0)
7800a30448SNate Lawson err(EX_IOERR, "ack sleep type failed");
792e6c5fc5SMitsuru IWASAKI }
8064fdad23SNate Lawson
81bfccea1eSWarner Losh /* should be a acpi define, but doesn't appear to be */
82bfccea1eSWarner Losh #define UNKNOWN_CAP 0xffffffff
837a20dc58SNate Lawson #define UNKNOWN_VOLTAGE 0xffffffff
84bfccea1eSWarner Losh
8564fdad23SNate Lawson static int
acpi_battinfo(int num)8664fdad23SNate Lawson acpi_battinfo(int num)
8764fdad23SNate Lawson {
8864fdad23SNate Lawson union acpi_battery_ioctl_arg battio;
8964fdad23SNate Lawson const char *pwr_units;
909c1af421SAlexander Motin int hours, min, amp;
919c1af421SAlexander Motin uint32_t volt;
9264fdad23SNate Lawson
9364fdad23SNate Lawson if (num < 0 || num > 64)
94fba61279SNiclas Zeising errx(EX_USAGE, "invalid battery %d", num);
9564fdad23SNate Lawson
967a20dc58SNate Lawson /* Print battery design information. */
9764fdad23SNate Lawson battio.unit = num;
98294de6bbSHiroki Sato if (ioctl(acpifd, ACPIIO_BATT_GET_BIX, &battio) == -1)
9964fdad23SNate Lawson err(EX_IOERR, "get battery info (%d) failed", num);
100294de6bbSHiroki Sato amp = battio.bix.units;
1019c1af421SAlexander Motin pwr_units = amp ? "mA" : "mW";
102294de6bbSHiroki Sato if (battio.bix.dcap == UNKNOWN_CAP)
1037a20dc58SNate Lawson printf("Design capacity:\tunknown\n");
104bfccea1eSWarner Losh else
105294de6bbSHiroki Sato printf("Design capacity:\t%d %sh\n", battio.bix.dcap,
1067a20dc58SNate Lawson pwr_units);
107294de6bbSHiroki Sato if (battio.bix.lfcap == UNKNOWN_CAP)
1087a20dc58SNate Lawson printf("Last full capacity:\tunknown\n");
109bfccea1eSWarner Losh else
110294de6bbSHiroki Sato printf("Last full capacity:\t%d %sh\n", battio.bix.lfcap,
111bfccea1eSWarner Losh pwr_units);
112294de6bbSHiroki Sato printf("Technology:\t\t%s\n", battio.bix.btech == 0 ?
11364fdad23SNate Lawson "primary (non-rechargeable)" : "secondary (rechargeable)");
114294de6bbSHiroki Sato if (ACPI_BIX_REV_MIN_CHECK(battio.bix.rev, ACPI_BIX_REV_1)) {
115294de6bbSHiroki Sato printf("Battery Swappable Capability:\t");
116294de6bbSHiroki Sato if (battio.bix.scap == ACPI_BIX_SCAP_NO)
117294de6bbSHiroki Sato printf("Non-swappable\n");
118294de6bbSHiroki Sato else if (battio.bix.scap == ACPI_BIX_SCAP_COLD)
119294de6bbSHiroki Sato printf("cold swap\n");
120294de6bbSHiroki Sato else if (battio.bix.scap == ACPI_BIX_SCAP_HOT)
121294de6bbSHiroki Sato printf("hot swap\n");
122294de6bbSHiroki Sato else
123294de6bbSHiroki Sato printf("unknown\n");
124294de6bbSHiroki Sato }
125294de6bbSHiroki Sato if (battio.bix.dvol == UNKNOWN_CAP)
1267a20dc58SNate Lawson printf("Design voltage:\t\tunknown\n");
127bfccea1eSWarner Losh else
128294de6bbSHiroki Sato printf("Design voltage:\t\t%d mV\n", battio.bix.dvol);
129294de6bbSHiroki Sato printf("Capacity (warn):\t%d %sh\n", battio.bix.wcap, pwr_units);
130294de6bbSHiroki Sato printf("Capacity (low):\t\t%d %sh\n", battio.bix.lcap, pwr_units);
131294de6bbSHiroki Sato if (ACPI_BIX_REV_MIN_CHECK(battio.bix.rev, ACPI_BIX_REV_0)) {
132294de6bbSHiroki Sato if (battio.bix.cycles != ACPI_BATT_UNKNOWN)
133294de6bbSHiroki Sato printf("Cycle Count:\t\t%d\n", battio.bix.cycles);
1349d478581SMatt Audesse printf("Measurement Accuracy:\t%d%%\n",
135294de6bbSHiroki Sato battio.bix.accuracy / 1000);
136294de6bbSHiroki Sato if (battio.bix.stmax != ACPI_BATT_UNKNOWN)
137294de6bbSHiroki Sato printf("Max Sampling Time:\t%d ms\n",
138294de6bbSHiroki Sato battio.bix.stmax);
139294de6bbSHiroki Sato if (battio.bix.stmin != ACPI_BATT_UNKNOWN)
140294de6bbSHiroki Sato printf("Min Sampling Time:\t%d ms\n",
141294de6bbSHiroki Sato battio.bix.stmin);
142294de6bbSHiroki Sato printf("Max Average Interval:\t%d ms\n",
143294de6bbSHiroki Sato battio.bix.aimax);
144294de6bbSHiroki Sato printf("Min Average Interval:\t%d ms\n",
145294de6bbSHiroki Sato battio.bix.aimin);
146294de6bbSHiroki Sato }
147294de6bbSHiroki Sato printf("Low/warn granularity:\t%d %sh\n", battio.bix.gra1, pwr_units);
148294de6bbSHiroki Sato printf("Warn/full granularity:\t%d %sh\n", battio.bix.gra2, pwr_units);
149294de6bbSHiroki Sato printf("Model number:\t\t%s\n", battio.bix.model);
150294de6bbSHiroki Sato printf("Serial number:\t\t%s\n", battio.bix.serial);
151294de6bbSHiroki Sato printf("Type:\t\t\t%s\n", battio.bix.type);
152294de6bbSHiroki Sato printf("OEM info:\t\t%s\n", battio.bix.oeminfo);
1532e6c5fc5SMitsuru IWASAKI
1549c1af421SAlexander Motin /* Fetch battery voltage information. */
1559c1af421SAlexander Motin volt = UNKNOWN_VOLTAGE;
1569c1af421SAlexander Motin battio.unit = num;
1579c1af421SAlexander Motin if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1)
1589c1af421SAlexander Motin err(EX_IOERR, "get battery status (%d) failed", num);
1599c1af421SAlexander Motin if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT)
1609c1af421SAlexander Motin volt = battio.bst.volt;
1619c1af421SAlexander Motin
1627a20dc58SNate Lawson /* Print current battery state information. */
1637a20dc58SNate Lawson battio.unit = num;
1647a20dc58SNate Lawson if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1)
1657a20dc58SNate Lawson err(EX_IOERR, "get battery user info (%d) failed", num);
1667a20dc58SNate Lawson if (battio.battinfo.state != ACPI_BATT_STAT_NOT_PRESENT) {
1677ecb6034SJung-uk Kim const char *state;
1687ecb6034SJung-uk Kim switch (battio.battinfo.state & ACPI_BATT_STAT_BST_MASK) {
1697ecb6034SJung-uk Kim case 0:
1707ecb6034SJung-uk Kim state = "high";
1717ecb6034SJung-uk Kim break;
1727ecb6034SJung-uk Kim case ACPI_BATT_STAT_DISCHARG:
1737ecb6034SJung-uk Kim state = "discharging";
1747ecb6034SJung-uk Kim break;
1757ecb6034SJung-uk Kim case ACPI_BATT_STAT_CHARGING:
1767ecb6034SJung-uk Kim state = "charging";
1777ecb6034SJung-uk Kim break;
1787ecb6034SJung-uk Kim case ACPI_BATT_STAT_CRITICAL:
1797ecb6034SJung-uk Kim state = "critical";
1807ecb6034SJung-uk Kim break;
1817ecb6034SJung-uk Kim case ACPI_BATT_STAT_DISCHARG | ACPI_BATT_STAT_CRITICAL:
1827ecb6034SJung-uk Kim state = "critical discharging";
1837ecb6034SJung-uk Kim break;
1847ecb6034SJung-uk Kim case ACPI_BATT_STAT_CHARGING | ACPI_BATT_STAT_CRITICAL:
1857ecb6034SJung-uk Kim state = "critical charging";
1867ecb6034SJung-uk Kim break;
1877ecb6034SJung-uk Kim default:
1887ecb6034SJung-uk Kim state = "invalid";
1897ecb6034SJung-uk Kim }
1907ecb6034SJung-uk Kim printf("State:\t\t\t%s\n", state);
1917a20dc58SNate Lawson if (battio.battinfo.cap == -1)
1927a20dc58SNate Lawson printf("Remaining capacity:\tunknown\n");
1937a20dc58SNate Lawson else
1947a20dc58SNate Lawson printf("Remaining capacity:\t%d%%\n",
1957a20dc58SNate Lawson battio.battinfo.cap);
1967a20dc58SNate Lawson if (battio.battinfo.min == -1)
1970af442fbSNate Lawson printf("Remaining time:\t\tunknown\n");
1987a20dc58SNate Lawson else {
1997a20dc58SNate Lawson hours = battio.battinfo.min / 60;
2007a20dc58SNate Lawson min = battio.battinfo.min % 60;
2017a20dc58SNate Lawson printf("Remaining time:\t\t%d:%02d\n", hours, min);
2027a20dc58SNate Lawson }
2037a20dc58SNate Lawson if (battio.battinfo.rate == -1)
2047a20dc58SNate Lawson printf("Present rate:\t\tunknown\n");
2059c1af421SAlexander Motin else if (amp && volt != UNKNOWN_VOLTAGE) {
2069c1af421SAlexander Motin printf("Present rate:\t\t%d mA (%d mW)\n",
2079c1af421SAlexander Motin battio.battinfo.rate,
2089c1af421SAlexander Motin battio.battinfo.rate * volt / 1000);
2099c1af421SAlexander Motin } else
2107a20dc58SNate Lawson printf("Present rate:\t\t%d %s\n",
2117a20dc58SNate Lawson battio.battinfo.rate, pwr_units);
2127a20dc58SNate Lawson } else
2137a20dc58SNate Lawson printf("State:\t\t\tnot present\n");
2147a20dc58SNate Lawson
2157a20dc58SNate Lawson /* Print battery voltage information. */
2169c1af421SAlexander Motin if (volt == UNKNOWN_VOLTAGE)
2179c1af421SAlexander Motin printf("Present voltage:\tunknown\n");
218bfccea1eSWarner Losh else
2199c1af421SAlexander Motin printf("Present voltage:\t%d mV\n", volt);
2207a20dc58SNate Lawson
2212e6c5fc5SMitsuru IWASAKI return (0);
2222e6c5fc5SMitsuru IWASAKI }
2232e6c5fc5SMitsuru IWASAKI
224d3f4a2caSMitsuru IWASAKI static void
usage(const char * prog)225d3f4a2caSMitsuru IWASAKI usage(const char* prog)
226d3f4a2caSMitsuru IWASAKI {
22700a30448SNate Lawson printf("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog);
228d3f4a2caSMitsuru IWASAKI exit(0);
229d3f4a2caSMitsuru IWASAKI }
230d3f4a2caSMitsuru IWASAKI
2312e6c5fc5SMitsuru IWASAKI int
main(int argc,char * argv[])2322e6c5fc5SMitsuru IWASAKI main(int argc, char *argv[])
2332e6c5fc5SMitsuru IWASAKI {
234fba61279SNiclas Zeising char *prog, *end;
235fba61279SNiclas Zeising int c, sleep_type, battery, ack;
236fba61279SNiclas Zeising int iflag = 0, kflag = 0, sflag = 0;
2372e6c5fc5SMitsuru IWASAKI
238d3f4a2caSMitsuru IWASAKI prog = argv[0];
23964fdad23SNate Lawson if (argc < 2)
24064fdad23SNate Lawson usage(prog);
24164fdad23SNate Lawson /* NOTREACHED */
24264fdad23SNate Lawson
2432e6c5fc5SMitsuru IWASAKI sleep_type = -1;
24464fdad23SNate Lawson acpi_init();
24500a30448SNate Lawson while ((c = getopt(argc, argv, "hi:k:s:")) != -1) {
2462e6c5fc5SMitsuru IWASAKI switch (c) {
24764fdad23SNate Lawson case 'i':
248fba61279SNiclas Zeising iflag = 1;
249fba61279SNiclas Zeising battery = strtol(optarg, &end, 10);
250fba61279SNiclas Zeising if ((size_t)(end - optarg) != strlen(optarg))
251fba61279SNiclas Zeising errx(EX_USAGE, "invalid battery");
25264fdad23SNate Lawson break;
25300a30448SNate Lawson case 'k':
254fba61279SNiclas Zeising kflag = 1;
255fba61279SNiclas Zeising ack = strtol(optarg, &end, 10);
256fba61279SNiclas Zeising if ((size_t)(end - optarg) != strlen(optarg))
257fba61279SNiclas Zeising errx(EX_USAGE, "invalid ack argument");
25800a30448SNate Lawson break;
2592e6c5fc5SMitsuru IWASAKI case 's':
260fba61279SNiclas Zeising sflag = 1;
261d886fcc2SNate Lawson if (optarg[0] == 'S')
262fba61279SNiclas Zeising optarg++;
263fba61279SNiclas Zeising sleep_type = strtol(optarg, &end, 10);
264fba61279SNiclas Zeising if ((size_t)(end - optarg) != strlen(optarg))
265fba61279SNiclas Zeising errx(EX_USAGE, "invalid sleep type");
26600a30448SNate Lawson if (sleep_type < 1 || sleep_type > 4)
26728f49c6dSCrist J. Clark errx(EX_USAGE, "invalid sleep type (%d)",
26828f49c6dSCrist J. Clark sleep_type);
2692e6c5fc5SMitsuru IWASAKI break;
27064fdad23SNate Lawson case 'h':
2712e6c5fc5SMitsuru IWASAKI default:
27264fdad23SNate Lawson usage(prog);
27364fdad23SNate Lawson /* NOTREACHED */
27464fdad23SNate Lawson }
27564fdad23SNate Lawson }
2762e6c5fc5SMitsuru IWASAKI argc -= optind;
2772e6c5fc5SMitsuru IWASAKI argv += optind;
2782e6c5fc5SMitsuru IWASAKI
279fba61279SNiclas Zeising if (iflag != 0 && kflag != 0 && sflag != 0)
280fba61279SNiclas Zeising errx(EX_USAGE, "-i, -k and -s are mutually exclusive");
281fba61279SNiclas Zeising
282fba61279SNiclas Zeising if (iflag != 0) {
283fba61279SNiclas Zeising if (kflag != 0)
284fba61279SNiclas Zeising errx(EX_USAGE, "-i and -k are mutually exclusive");
285fba61279SNiclas Zeising if (sflag != 0)
286fba61279SNiclas Zeising errx(EX_USAGE, "-i and -s are mutually exclusive");
287fba61279SNiclas Zeising acpi_battinfo(battery);
288fba61279SNiclas Zeising }
289fba61279SNiclas Zeising
290fba61279SNiclas Zeising if (kflag != 0) {
291fba61279SNiclas Zeising if (sflag != 0)
292fba61279SNiclas Zeising errx(EX_USAGE, "-k and -s are mutually exclusive");
293fba61279SNiclas Zeising acpi_sleep_ack(ack);
294fba61279SNiclas Zeising }
295fba61279SNiclas Zeising
296fba61279SNiclas Zeising
297fba61279SNiclas Zeising if (sflag != 0)
2982e6c5fc5SMitsuru IWASAKI acpi_sleep(sleep_type);
29964fdad23SNate Lawson
30064fdad23SNate Lawson close(acpifd);
30164fdad23SNate Lawson exit (0);
3022e6c5fc5SMitsuru IWASAKI }
303