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