xref: /freebsd/usr.sbin/acpi/acpiconf/acpiconf.c (revision c03c5b1c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  *	$FreeBSD$
30  */
31 
32 #include <sys/param.h>
33 
34 #include <err.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <sys/ioctl.h>
38 #include <sysexits.h>
39 #include <unistd.h>
40 
41 #include <dev/acpica/acpiio.h>
42 
43 #include <contrib/dev/acpica/include/acpi.h>
44 
45 #define ACPIDEV		"/dev/acpi"
46 
47 static int	acpifd;
48 
49 static void
50 acpi_init(void)
51 {
52 	acpifd = open(ACPIDEV, O_RDWR);
53 	if (acpifd == -1)
54 		acpifd = open(ACPIDEV, O_RDONLY);
55 	if (acpifd == -1)
56 		err(EX_OSFILE, ACPIDEV);
57 }
58 
59 /* Prepare to sleep and then wait for the signal that sleeping can occur. */
60 static void
61 acpi_sleep(int sleep_type)
62 {
63 	int ret;
64 
65 	/* Notify OS that we want to sleep.  devd(8) gets this notify. */
66 	ret = ioctl(acpifd, ACPIIO_REQSLPSTATE, &sleep_type);
67 	if (ret != 0)
68 		err(EX_IOERR, "request sleep type (%d) failed", sleep_type);
69 }
70 
71 /* Ack or abort a pending suspend request. */
72 static void
73 acpi_sleep_ack(int err_val)
74 {
75 	int ret;
76 
77 	ret = ioctl(acpifd, ACPIIO_ACKSLPSTATE, &err_val);
78 	if (ret != 0)
79 		err(EX_IOERR, "ack sleep type failed");
80 }
81 
82 /* should be a acpi define, but doesn't appear to be */
83 #define UNKNOWN_CAP 0xffffffff
84 #define UNKNOWN_VOLTAGE 0xffffffff
85 
86 static int
87 acpi_battinfo(int num)
88 {
89 	union acpi_battery_ioctl_arg battio;
90 	const char *pwr_units;
91 	int hours, min, amp;
92 	uint32_t volt;
93 
94 	if (num < 0 || num > 64)
95 		errx(EX_USAGE, "invalid battery %d", num);
96 
97 	/* Print battery design information. */
98 	battio.unit = num;
99 	if (ioctl(acpifd, ACPIIO_BATT_GET_BIX, &battio) == -1)
100 		err(EX_IOERR, "get battery info (%d) failed", num);
101 	amp = battio.bix.units;
102 	pwr_units = amp ? "mA" : "mW";
103 	if (battio.bix.dcap == UNKNOWN_CAP)
104 		printf("Design capacity:\tunknown\n");
105 	else
106 		printf("Design capacity:\t%d %sh\n", battio.bix.dcap,
107 		    pwr_units);
108 	if (battio.bix.lfcap == UNKNOWN_CAP)
109 		printf("Last full capacity:\tunknown\n");
110 	else
111 		printf("Last full capacity:\t%d %sh\n", battio.bix.lfcap,
112 		    pwr_units);
113 	printf("Technology:\t\t%s\n", battio.bix.btech == 0 ?
114 	    "primary (non-rechargeable)" : "secondary (rechargeable)");
115 	if (ACPI_BIX_REV_MIN_CHECK(battio.bix.rev, ACPI_BIX_REV_1)) {
116 		printf("Battery Swappable Capability:\t");
117 		if (battio.bix.scap == ACPI_BIX_SCAP_NO)
118 			printf("Non-swappable\n");
119 		else if (battio.bix.scap == ACPI_BIX_SCAP_COLD)
120 			printf("cold swap\n");
121 		else if (battio.bix.scap == ACPI_BIX_SCAP_HOT)
122 			printf("hot swap\n");
123 		else
124 			printf("unknown\n");
125 	}
126 	if (battio.bix.dvol == UNKNOWN_CAP)
127 		printf("Design voltage:\t\tunknown\n");
128 	else
129 		printf("Design voltage:\t\t%d mV\n", battio.bix.dvol);
130 	printf("Capacity (warn):\t%d %sh\n", battio.bix.wcap, pwr_units);
131 	printf("Capacity (low):\t\t%d %sh\n", battio.bix.lcap, pwr_units);
132 	if (ACPI_BIX_REV_MIN_CHECK(battio.bix.rev, ACPI_BIX_REV_0)) {
133 		if (battio.bix.cycles != ACPI_BATT_UNKNOWN)
134 			printf("Cycle Count:\t\t%d\n", battio.bix.cycles);
135 		printf("Measurement Accuracy:\t%d %%\n",
136 		    battio.bix.accuracy / 1000);
137 		if (battio.bix.stmax != ACPI_BATT_UNKNOWN)
138 			printf("Max Sampling Time:\t%d ms\n",
139 			    battio.bix.stmax);
140 		if (battio.bix.stmin != ACPI_BATT_UNKNOWN)
141 			printf("Min Sampling Time:\t%d ms\n",
142 			    battio.bix.stmin);
143 		printf("Max Average Interval:\t%d ms\n",
144 		    battio.bix.aimax);
145 		printf("Min Average Interval:\t%d ms\n",
146 		    battio.bix.aimin);
147 	}
148 	printf("Low/warn granularity:\t%d %sh\n", battio.bix.gra1, pwr_units);
149 	printf("Warn/full granularity:\t%d %sh\n", battio.bix.gra2, pwr_units);
150 	printf("Model number:\t\t%s\n", battio.bix.model);
151 	printf("Serial number:\t\t%s\n", battio.bix.serial);
152 	printf("Type:\t\t\t%s\n", battio.bix.type);
153 	printf("OEM info:\t\t%s\n", battio.bix.oeminfo);
154 
155 	/* Fetch battery voltage information. */
156 	volt = UNKNOWN_VOLTAGE;
157 	battio.unit = num;
158 	if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1)
159 		err(EX_IOERR, "get battery status (%d) failed", num);
160 	if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT)
161 		volt = battio.bst.volt;
162 
163 	/* Print current battery state information. */
164 	battio.unit = num;
165 	if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1)
166 		err(EX_IOERR, "get battery user info (%d) failed", num);
167 	if (battio.battinfo.state != ACPI_BATT_STAT_NOT_PRESENT) {
168 		const char *state;
169 		switch (battio.battinfo.state & ACPI_BATT_STAT_BST_MASK) {
170 		case 0:
171 			state = "high";
172 			break;
173 		case ACPI_BATT_STAT_DISCHARG:
174 			state = "discharging";
175 			break;
176 		case ACPI_BATT_STAT_CHARGING:
177 			state = "charging";
178 			break;
179 		case ACPI_BATT_STAT_CRITICAL:
180 			state = "critical";
181 			break;
182 		case ACPI_BATT_STAT_DISCHARG | ACPI_BATT_STAT_CRITICAL:
183 			state = "critical discharging";
184 			break;
185 		case ACPI_BATT_STAT_CHARGING | ACPI_BATT_STAT_CRITICAL:
186 			state = "critical charging";
187 			break;
188 		default:
189 			state = "invalid";
190 		}
191 		printf("State:\t\t\t%s\n", state);
192 		if (battio.battinfo.cap == -1)
193 			printf("Remaining capacity:\tunknown\n");
194 		else
195 			printf("Remaining capacity:\t%d%%\n",
196 			    battio.battinfo.cap);
197 		if (battio.battinfo.min == -1)
198 			printf("Remaining time:\t\tunknown\n");
199 		else {
200 			hours = battio.battinfo.min / 60;
201 			min = battio.battinfo.min % 60;
202 			printf("Remaining time:\t\t%d:%02d\n", hours, min);
203 		}
204 		if (battio.battinfo.rate == -1)
205 			printf("Present rate:\t\tunknown\n");
206 		else if (amp && volt != UNKNOWN_VOLTAGE) {
207 			printf("Present rate:\t\t%d mA (%d mW)\n",
208 			    battio.battinfo.rate,
209 			    battio.battinfo.rate * volt / 1000);
210 		} else
211 			printf("Present rate:\t\t%d %s\n",
212 			    battio.battinfo.rate, pwr_units);
213 	} else
214 		printf("State:\t\t\tnot present\n");
215 
216 	/* Print battery voltage information. */
217 	if (volt == UNKNOWN_VOLTAGE)
218 		printf("Present voltage:\tunknown\n");
219 	else
220 		printf("Present voltage:\t%d mV\n", volt);
221 
222 	return (0);
223 }
224 
225 static void
226 usage(const char* prog)
227 {
228 	printf("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog);
229 	exit(0);
230 }
231 
232 int
233 main(int argc, char *argv[])
234 {
235 	char	*prog, *end;
236 	int	c, sleep_type, battery, ack;
237 	int	iflag = 0, kflag = 0, sflag = 0;
238 
239 	prog = argv[0];
240 	if (argc < 2)
241 		usage(prog);
242 		/* NOTREACHED */
243 
244 	sleep_type = -1;
245 	acpi_init();
246 	while ((c = getopt(argc, argv, "hi:k:s:")) != -1) {
247 		switch (c) {
248 		case 'i':
249 			iflag = 1;
250 			battery = strtol(optarg, &end, 10);
251 			if ((size_t)(end - optarg) != strlen(optarg))
252 			    errx(EX_USAGE, "invalid battery");
253 			break;
254 		case 'k':
255 			kflag = 1;
256 			ack = strtol(optarg, &end, 10);
257 			if ((size_t)(end - optarg) != strlen(optarg))
258 			    errx(EX_USAGE, "invalid ack argument");
259 			break;
260 		case 's':
261 			sflag = 1;
262 			if (optarg[0] == 'S')
263 				optarg++;
264 			sleep_type = strtol(optarg, &end, 10);
265 			if ((size_t)(end - optarg) != strlen(optarg))
266 			    errx(EX_USAGE, "invalid sleep type");
267 			if (sleep_type < 1 || sleep_type > 4)
268 				errx(EX_USAGE, "invalid sleep type (%d)",
269 				     sleep_type);
270 			break;
271 		case 'h':
272 		default:
273 			usage(prog);
274 			/* NOTREACHED */
275 		}
276 	}
277 	argc -= optind;
278 	argv += optind;
279 
280 	if (iflag != 0 && kflag != 0 && sflag != 0)
281 			errx(EX_USAGE, "-i, -k and -s are mutually exclusive");
282 
283 	if (iflag  != 0) {
284 		if (kflag != 0)
285 			errx(EX_USAGE, "-i and -k are mutually exclusive");
286 		if (sflag != 0)
287 			errx(EX_USAGE, "-i and -s are mutually exclusive");
288 		acpi_battinfo(battery);
289 	}
290 
291 	if (kflag != 0) {
292 		if (sflag != 0)
293 			errx(EX_USAGE, "-k and -s are mutually exclusive");
294 		acpi_sleep_ack(ack);
295 	}
296 
297 
298 	if (sflag != 0)
299 		acpi_sleep(sleep_type);
300 
301 	close(acpifd);
302 	exit (0);
303 }
304