2 * Copyright (c) 1999 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $Id: acpiconf.c,v 1.5 2000/08/08 14:12:19 iwasaki Exp $
27 * $FreeBSD: src/usr.sbin/acpi/acpiconf/acpiconf.c,v 1.26.8.1 2009/04/15 03:14:26 kensmith Exp $
30 #include <sys/param.h>
35 #include <sys/ioctl.h>
42 #define ACPIDEV "/dev/acpi"
49 acpifd = open(ACPIDEV, O_RDWR);
51 acpifd = open(ACPIDEV, O_RDONLY);
53 err(EX_OSFILE, ACPIDEV);
56 /* Prepare to sleep and then wait for the signal that sleeping can occur. */
58 acpi_sleep(int sleep_type)
62 /* Notify OS that we want to sleep. devd(8) gets this notify. */
63 ret = ioctl(acpifd, ACPIIO_REQSLPSTATE, &sleep_type);
65 err(EX_IOERR, "request sleep type (%d) failed", sleep_type);
68 /* Ack or abort a pending suspend request. */
70 acpi_sleep_ack(int err_val)
74 ret = ioctl(acpifd, ACPIIO_ACKSLPSTATE, &err_val);
76 err(EX_IOERR, "ack sleep type failed");
79 /* should be a acpi define, but doesn't appear to be */
80 #define UNKNOWN_CAP 0xffffffff
81 #define UNKNOWN_VOLTAGE 0xffffffff
84 acpi_battinfo(int num)
86 union acpi_battery_ioctl_arg battio;
87 const char *pwr_units;
91 if (num < 0 || num > 64)
92 err(EX_USAGE, "invalid battery %d", num);
94 /* Print battery design information. */
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";
101 if (battio.bif.dcap == UNKNOWN_CAP)
102 printf("Design capacity:\tunknown\n");
104 printf("Design capacity:\t%d %sh\n", battio.bif.dcap,
106 if (battio.bif.lfcap == UNKNOWN_CAP)
107 printf("Last full capacity:\tunknown\n");
109 printf("Last full capacity:\t%d %sh\n", battio.bif.lfcap,
111 printf("Technology:\t\t%s\n", battio.bif.btech == 0 ?
112 "primary (non-rechargeable)" : "secondary (rechargeable)");
113 if (battio.bif.dvol == UNKNOWN_CAP)
114 printf("Design voltage:\t\tunknown\n");
116 printf("Design voltage:\t\t%d mV\n", battio.bif.dvol);
117 printf("Capacity (warn):\t%d %sh\n", battio.bif.wcap, pwr_units);
118 printf("Capacity (low):\t\t%d %sh\n", battio.bif.lcap, pwr_units);
119 printf("Low/warn granularity:\t%d %sh\n", battio.bif.gra1, pwr_units);
120 printf("Warn/full granularity:\t%d %sh\n", battio.bif.gra2, pwr_units);
121 printf("Model number:\t\t%s\n", battio.bif.model);
122 printf("Serial number:\t\t%s\n", battio.bif.serial);
123 printf("Type:\t\t\t%s\n", battio.bif.type);
124 printf("OEM info:\t\t%s\n", battio.bif.oeminfo);
126 /* Fetch battery voltage information. */
127 volt = UNKNOWN_VOLTAGE;
129 if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1)
130 err(EX_IOERR, "get battery status (%d) failed", num);
131 if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT)
132 volt = battio.bst.volt;
134 /* Print current battery state information. */
136 if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1)
137 err(EX_IOERR, "get battery user info (%d) failed", num);
138 if (battio.battinfo.state != ACPI_BATT_STAT_NOT_PRESENT) {
139 printf("State:\t\t\t");
140 if (battio.battinfo.state == 0)
142 if (battio.battinfo.state & ACPI_BATT_STAT_CRITICAL)
144 if (battio.battinfo.state & ACPI_BATT_STAT_DISCHARG)
145 printf("discharging ");
146 if (battio.battinfo.state & ACPI_BATT_STAT_CHARGING)
149 if (battio.battinfo.cap == -1)
150 printf("Remaining capacity:\tunknown\n");
152 printf("Remaining capacity:\t%d%%\n",
153 battio.battinfo.cap);
154 if (battio.battinfo.min == -1)
155 printf("Remaining time:\t\tunknown\n");
157 hours = battio.battinfo.min / 60;
158 min = battio.battinfo.min % 60;
159 printf("Remaining time:\t\t%d:%02d\n", hours, min);
161 if (battio.battinfo.rate == -1)
162 printf("Present rate:\t\tunknown\n");
163 else if (amp && volt != UNKNOWN_VOLTAGE) {
164 printf("Present rate:\t\t%d mA (%d mW)\n",
165 battio.battinfo.rate,
166 battio.battinfo.rate * volt / 1000);
168 printf("Present rate:\t\t%d %s\n",
169 battio.battinfo.rate, pwr_units);
171 printf("State:\t\t\tnot present\n");
173 /* Print battery voltage information. */
175 if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1)
176 err(EX_IOERR, "get battery status (%d) failed", num);
177 if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT) {
178 if (battio.bst.volt == UNKNOWN_VOLTAGE)
179 printf("Voltage:\t\tunknown\n");
181 printf("Voltage:\t\t%d mV\n", battio.bst.volt);
188 usage(const char* prog)
190 printf("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog);
195 main(int argc, char *argv[])
207 while ((c = getopt(argc, argv, "hi:k:s:")) != -1) {
210 acpi_battinfo(atoi(optarg));
213 acpi_sleep_ack(atoi(optarg));
216 if (optarg[0] == 'S')
217 sleep_type = optarg[1] - '0';
219 sleep_type = optarg[0] - '0';
220 if (sleep_type < 1 || sleep_type > 4)
221 errx(EX_USAGE, "invalid sleep type (%d)",
233 if (sleep_type != -1)
234 acpi_sleep(sleep_type);