1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1999 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 *	$Id: acpiconf.c,v 1.5 2000/08/08 14:12:19 iwasaki Exp $
29 */
30
31#include <sys/param.h>
32
33#include <err.h>
34#include <fcntl.h>
35#include <stdio.h>
36#include <sys/ioctl.h>
37#include <sysexits.h>
38#include <unistd.h>
39
40#include <dev/acpica/acpiio.h>
41
42#include <contrib/dev/acpica/include/acpi.h>
43
44#define ACPIDEV		"/dev/acpi"
45
46static int	acpifd;
47
48static void
49acpi_init(void)
50{
51	acpifd = open(ACPIDEV, O_RDWR);
52	if (acpifd == -1)
53		acpifd = open(ACPIDEV, O_RDONLY);
54	if (acpifd == -1)
55		err(EX_OSFILE, ACPIDEV);
56}
57
58/* Prepare to sleep and then wait for the signal that sleeping can occur. */
59static void
60acpi_sleep(int sleep_type)
61{
62	int ret;
63
64	/* Notify OS that we want to sleep.  devd(8) gets this notify. */
65	ret = ioctl(acpifd, ACPIIO_REQSLPSTATE, &sleep_type);
66	if (ret != 0)
67		err(EX_IOERR, "request sleep type (%d) failed", sleep_type);
68}
69
70/* Ack or abort a pending suspend request. */
71static void
72acpi_sleep_ack(int err_val)
73{
74	int ret;
75
76	ret = ioctl(acpifd, ACPIIO_ACKSLPSTATE, &err_val);
77	if (ret != 0)
78		err(EX_IOERR, "ack sleep type failed");
79}
80
81/* should be a acpi define, but doesn't appear to be */
82#define UNKNOWN_CAP 0xffffffff
83#define UNKNOWN_VOLTAGE 0xffffffff
84
85static int
86acpi_battinfo(int num)
87{
88	union acpi_battery_ioctl_arg battio;
89	const char *pwr_units;
90	int hours, min, amp;
91	uint32_t volt;
92
93	if (num < 0 || num > 64)
94		errx(EX_USAGE, "invalid battery %d", num);
95
96	/* Print battery design information. */
97	battio.unit = num;
98	if (ioctl(acpifd, ACPIIO_BATT_GET_BIX, &battio) == -1)
99		err(EX_IOERR, "get battery info (%d) failed", num);
100	amp = battio.bix.units;
101	pwr_units = amp ? "mA" : "mW";
102	if (battio.bix.dcap == UNKNOWN_CAP)
103		printf("Design capacity:\tunknown\n");
104	else
105		printf("Design capacity:\t%d %sh\n", battio.bix.dcap,
106		    pwr_units);
107	if (battio.bix.lfcap == UNKNOWN_CAP)
108		printf("Last full capacity:\tunknown\n");
109	else
110		printf("Last full capacity:\t%d %sh\n", battio.bix.lfcap,
111		    pwr_units);
112	printf("Technology:\t\t%s\n", battio.bix.btech == 0 ?
113	    "primary (non-rechargeable)" : "secondary (rechargeable)");
114	if (ACPI_BIX_REV_MIN_CHECK(battio.bix.rev, ACPI_BIX_REV_1)) {
115		printf("Battery Swappable Capability:\t");
116		if (battio.bix.scap == ACPI_BIX_SCAP_NO)
117			printf("Non-swappable\n");
118		else if (battio.bix.scap == ACPI_BIX_SCAP_COLD)
119			printf("cold swap\n");
120		else if (battio.bix.scap == ACPI_BIX_SCAP_HOT)
121			printf("hot swap\n");
122		else
123			printf("unknown\n");
124	}
125	if (battio.bix.dvol == UNKNOWN_CAP)
126		printf("Design voltage:\t\tunknown\n");
127	else
128		printf("Design voltage:\t\t%d mV\n", battio.bix.dvol);
129	printf("Capacity (warn):\t%d %sh\n", battio.bix.wcap, pwr_units);
130	printf("Capacity (low):\t\t%d %sh\n", battio.bix.lcap, pwr_units);
131	if (ACPI_BIX_REV_MIN_CHECK(battio.bix.rev, ACPI_BIX_REV_0)) {
132		if (battio.bix.cycles != ACPI_BATT_UNKNOWN)
133			printf("Cycle Count:\t\t%d\n", battio.bix.cycles);
134		printf("Measurement Accuracy:\t%d%%\n",
135		    battio.bix.accuracy / 1000);
136		if (battio.bix.stmax != ACPI_BATT_UNKNOWN)
137			printf("Max Sampling Time:\t%d ms\n",
138			    battio.bix.stmax);
139		if (battio.bix.stmin != ACPI_BATT_UNKNOWN)
140			printf("Min Sampling Time:\t%d ms\n",
141			    battio.bix.stmin);
142		printf("Max Average Interval:\t%d ms\n",
143		    battio.bix.aimax);
144		printf("Min Average Interval:\t%d ms\n",
145		    battio.bix.aimin);
146	}
147	printf("Low/warn granularity:\t%d %sh\n", battio.bix.gra1, pwr_units);
148	printf("Warn/full granularity:\t%d %sh\n", battio.bix.gra2, pwr_units);
149	printf("Model number:\t\t%s\n", battio.bix.model);
150	printf("Serial number:\t\t%s\n", battio.bix.serial);
151	printf("Type:\t\t\t%s\n", battio.bix.type);
152	printf("OEM info:\t\t%s\n", battio.bix.oeminfo);
153
154	/* Fetch battery voltage information. */
155	volt = UNKNOWN_VOLTAGE;
156	battio.unit = num;
157	if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1)
158		err(EX_IOERR, "get battery status (%d) failed", num);
159	if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT)
160		volt = battio.bst.volt;
161
162	/* Print current battery state information. */
163	battio.unit = num;
164	if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1)
165		err(EX_IOERR, "get battery user info (%d) failed", num);
166	if (battio.battinfo.state != ACPI_BATT_STAT_NOT_PRESENT) {
167		const char *state;
168		switch (battio.battinfo.state & ACPI_BATT_STAT_BST_MASK) {
169		case 0:
170			state = "high";
171			break;
172		case ACPI_BATT_STAT_DISCHARG:
173			state = "discharging";
174			break;
175		case ACPI_BATT_STAT_CHARGING:
176			state = "charging";
177			break;
178		case ACPI_BATT_STAT_CRITICAL:
179			state = "critical";
180			break;
181		case ACPI_BATT_STAT_DISCHARG | ACPI_BATT_STAT_CRITICAL:
182			state = "critical discharging";
183			break;
184		case ACPI_BATT_STAT_CHARGING | ACPI_BATT_STAT_CRITICAL:
185			state = "critical charging";
186			break;
187		default:
188			state = "invalid";
189		}
190		printf("State:\t\t\t%s\n", state);
191		if (battio.battinfo.cap == -1)
192			printf("Remaining capacity:\tunknown\n");
193		else
194			printf("Remaining capacity:\t%d%%\n",
195			    battio.battinfo.cap);
196		if (battio.battinfo.min == -1)
197			printf("Remaining time:\t\tunknown\n");
198		else {
199			hours = battio.battinfo.min / 60;
200			min = battio.battinfo.min % 60;
201			printf("Remaining time:\t\t%d:%02d\n", hours, min);
202		}
203		if (battio.battinfo.rate == -1)
204			printf("Present rate:\t\tunknown\n");
205		else if (amp && volt != UNKNOWN_VOLTAGE) {
206			printf("Present rate:\t\t%d mA (%d mW)\n",
207			    battio.battinfo.rate,
208			    battio.battinfo.rate * volt / 1000);
209		} else
210			printf("Present rate:\t\t%d %s\n",
211			    battio.battinfo.rate, pwr_units);
212	} else
213		printf("State:\t\t\tnot present\n");
214
215	/* Print battery voltage information. */
216	if (volt == UNKNOWN_VOLTAGE)
217		printf("Present voltage:\tunknown\n");
218	else
219		printf("Present voltage:\t%d mV\n", volt);
220
221	return (0);
222}
223
224static void
225usage(const char* prog)
226{
227	printf("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog);
228	exit(0);
229}
230
231int
232main(int argc, char *argv[])
233{
234	char	*prog, *end;
235	int	c, sleep_type, battery, ack;
236	int	iflag = 0, kflag = 0, sflag = 0;
237
238	prog = argv[0];
239	if (argc < 2)
240		usage(prog);
241		/* NOTREACHED */
242
243	sleep_type = -1;
244	acpi_init();
245	while ((c = getopt(argc, argv, "hi:k:s:")) != -1) {
246		switch (c) {
247		case 'i':
248			iflag = 1;
249			battery = strtol(optarg, &end, 10);
250			if ((size_t)(end - optarg) != strlen(optarg))
251			    errx(EX_USAGE, "invalid battery");
252			break;
253		case 'k':
254			kflag = 1;
255			ack = strtol(optarg, &end, 10);
256			if ((size_t)(end - optarg) != strlen(optarg))
257			    errx(EX_USAGE, "invalid ack argument");
258			break;
259		case 's':
260			sflag = 1;
261			if (optarg[0] == 'S')
262				optarg++;
263			sleep_type = strtol(optarg, &end, 10);
264			if ((size_t)(end - optarg) != strlen(optarg))
265			    errx(EX_USAGE, "invalid sleep type");
266			if (sleep_type < 1 || sleep_type > 4)
267				errx(EX_USAGE, "invalid sleep type (%d)",
268				     sleep_type);
269			break;
270		case 'h':
271		default:
272			usage(prog);
273			/* NOTREACHED */
274		}
275	}
276	argc -= optind;
277	argv += optind;
278
279	if (iflag != 0 && kflag != 0 && sflag != 0)
280			errx(EX_USAGE, "-i, -k and -s are mutually exclusive");
281
282	if (iflag  != 0) {
283		if (kflag != 0)
284			errx(EX_USAGE, "-i and -k are mutually exclusive");
285		if (sflag != 0)
286			errx(EX_USAGE, "-i and -s are mutually exclusive");
287		acpi_battinfo(battery);
288	}
289
290	if (kflag != 0) {
291		if (sflag != 0)
292			errx(EX_USAGE, "-k and -s are mutually exclusive");
293		acpi_sleep_ack(ack);
294	}
295
296
297	if (sflag != 0)
298		acpi_sleep(sleep_type);
299
300	close(acpifd);
301	exit (0);
302}
303