1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1999 Mitsuru IWASAKI <[email protected]>
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
acpi_init(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
acpi_sleep(int sleep_type)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
acpi_sleep_ack(int err_val)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
acpi_battinfo(int num)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_BIF, &battio) == -1)
100 err(EX_IOERR, "get battery info (%d) failed", num);
101 amp = battio.bif.units;
102 pwr_units = amp ? "mA" : "mW";
103 if (battio.bif.dcap == UNKNOWN_CAP)
104 printf("Design capacity:\tunknown\n");
105 else
106 printf("Design capacity:\t%d %sh\n", battio.bif.dcap,
107 pwr_units);
108 if (battio.bif.lfcap == UNKNOWN_CAP)
109 printf("Last full capacity:\tunknown\n");
110 else
111 printf("Last full capacity:\t%d %sh\n", battio.bif.lfcap,
112 pwr_units);
113 printf("Technology:\t\t%s\n", battio.bif.btech == 0 ?
114 "primary (non-rechargeable)" : "secondary (rechargeable)");
115 if (battio.bif.dvol == UNKNOWN_CAP)
116 printf("Design voltage:\t\tunknown\n");
117 else
118 printf("Design voltage:\t\t%d mV\n", battio.bif.dvol);
119 printf("Capacity (warn):\t%d %sh\n", battio.bif.wcap, pwr_units);
120 printf("Capacity (low):\t\t%d %sh\n", battio.bif.lcap, pwr_units);
121 printf("Low/warn granularity:\t%d %sh\n", battio.bif.gra1, pwr_units);
122 printf("Warn/full granularity:\t%d %sh\n", battio.bif.gra2, pwr_units);
123 printf("Model number:\t\t%s\n", battio.bif.model);
124 printf("Serial number:\t\t%s\n", battio.bif.serial);
125 printf("Type:\t\t\t%s\n", battio.bif.type);
126 printf("OEM info:\t\t%s\n", battio.bif.oeminfo);
127
128 /* Fetch battery voltage information. */
129 volt = UNKNOWN_VOLTAGE;
130 battio.unit = num;
131 if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1)
132 err(EX_IOERR, "get battery status (%d) failed", num);
133 if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT)
134 volt = battio.bst.volt;
135
136 /* Print current battery state information. */
137 battio.unit = num;
138 if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1)
139 err(EX_IOERR, "get battery user info (%d) failed", num);
140 if (battio.battinfo.state != ACPI_BATT_STAT_NOT_PRESENT) {
141 const char *state;
142 switch (battio.battinfo.state & ACPI_BATT_STAT_BST_MASK) {
143 case 0:
144 state = "high";
145 break;
146 case ACPI_BATT_STAT_DISCHARG:
147 state = "discharging";
148 break;
149 case ACPI_BATT_STAT_CHARGING:
150 state = "charging";
151 break;
152 case ACPI_BATT_STAT_CRITICAL:
153 state = "critical";
154 break;
155 case ACPI_BATT_STAT_DISCHARG | ACPI_BATT_STAT_CRITICAL:
156 state = "critical discharging";
157 break;
158 case ACPI_BATT_STAT_CHARGING | ACPI_BATT_STAT_CRITICAL:
159 state = "critical charging";
160 break;
161 default:
162 state = "invalid";
163 }
164 printf("State:\t\t\t%s\n", state);
165 if (battio.battinfo.cap == -1)
166 printf("Remaining capacity:\tunknown\n");
167 else
168 printf("Remaining capacity:\t%d%%\n",
169 battio.battinfo.cap);
170 if (battio.battinfo.min == -1)
171 printf("Remaining time:\t\tunknown\n");
172 else {
173 hours = battio.battinfo.min / 60;
174 min = battio.battinfo.min % 60;
175 printf("Remaining time:\t\t%d:%02d\n", hours, min);
176 }
177 if (battio.battinfo.rate == -1)
178 printf("Present rate:\t\tunknown\n");
179 else if (amp && volt != UNKNOWN_VOLTAGE) {
180 printf("Present rate:\t\t%d mA (%d mW)\n",
181 battio.battinfo.rate,
182 battio.battinfo.rate * volt / 1000);
183 } else
184 printf("Present rate:\t\t%d %s\n",
185 battio.battinfo.rate, pwr_units);
186 } else
187 printf("State:\t\t\tnot present\n");
188
189 /* Print battery voltage information. */
190 if (volt == UNKNOWN_VOLTAGE)
191 printf("Present voltage:\tunknown\n");
192 else
193 printf("Present voltage:\t%d mV\n", volt);
194
195 return (0);
196 }
197
198 static void
usage(const char * prog)199 usage(const char* prog)
200 {
201 printf("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog);
202 exit(0);
203 }
204
205 int
main(int argc,char * argv[])206 main(int argc, char *argv[])
207 {
208 char *prog, *end;
209 int c, sleep_type, battery, ack;
210 int iflag = 0, kflag = 0, sflag = 0;
211
212 prog = argv[0];
213 if (argc < 2)
214 usage(prog);
215 /* NOTREACHED */
216
217 sleep_type = -1;
218 acpi_init();
219 while ((c = getopt(argc, argv, "hi:k:s:")) != -1) {
220 switch (c) {
221 case 'i':
222 iflag = 1;
223 battery = strtol(optarg, &end, 10);
224 if ((size_t)(end - optarg) != strlen(optarg))
225 errx(EX_USAGE, "invalid battery");
226 break;
227 case 'k':
228 kflag = 1;
229 ack = strtol(optarg, &end, 10);
230 if ((size_t)(end - optarg) != strlen(optarg))
231 errx(EX_USAGE, "invalid ack argument");
232 break;
233 case 's':
234 sflag = 1;
235 if (optarg[0] == 'S')
236 optarg++;
237 sleep_type = strtol(optarg, &end, 10);
238 if ((size_t)(end - optarg) != strlen(optarg))
239 errx(EX_USAGE, "invalid sleep type");
240 if (sleep_type < 1 || sleep_type > 4)
241 errx(EX_USAGE, "invalid sleep type (%d)",
242 sleep_type);
243 break;
244 case 'h':
245 default:
246 usage(prog);
247 /* NOTREACHED */
248 }
249 }
250 argc -= optind;
251 argv += optind;
252
253 if (iflag != 0 && kflag != 0 && sflag != 0)
254 errx(EX_USAGE, "-i, -k and -s are mutually exclusive");
255
256 if (iflag != 0) {
257 if (kflag != 0)
258 errx(EX_USAGE, "-i and -k are mutually exclusive");
259 if (sflag != 0)
260 errx(EX_USAGE, "-i and -s are mutually exclusive");
261 acpi_battinfo(battery);
262 }
263
264 if (kflag != 0) {
265 if (sflag != 0)
266 errx(EX_USAGE, "-k and -s are mutually exclusive");
267 acpi_sleep_ack(ack);
268 }
269
270
271 if (sflag != 0)
272 acpi_sleep(sleep_type);
273
274 close(acpifd);
275 exit (0);
276 }
277