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.14 2004/03/05 02:48:22 takawata Exp $
28 * $DragonFly: src/usr.sbin/acpi/acpiconf/acpiconf.c,v 1.3 2008/09/29 06:59:45 hasso Exp $
31 #include <sys/param.h>
36 #include <sys/ioctl.h>
43 #define ACPIDEV "/dev/acpi"
44 #define RC_SUSPEND_PATH "/etc/rc.suspend"
45 #define RC_RESUME_PATH "/etc/rc.resume"
52 acpifd = open(ACPIDEV, O_RDWR);
54 acpifd = open(ACPIDEV, O_RDONLY);
56 err(EX_OSFILE, ACPIDEV);
60 acpi_enable_disable(int enable)
62 if (ioctl(acpifd, enable, NULL) == -1) {
63 if (enable == ACPIIO_ENABLE)
64 err(EX_IOERR, "enable failed");
66 err(EX_IOERR, "disable failed");
73 acpi_sleep(int sleep_type)
78 /* Run the suspend rc script, if available. */
79 if (access(RC_SUSPEND_PATH, X_OK) == 0) {
80 snprintf(cmd, sizeof(cmd), "%s acpi %d", RC_SUSPEND_PATH,
85 ret = ioctl(acpifd, ACPIIO_SETSLPSTATE, &sleep_type);
87 /* Run the resume rc script, if available. */
88 if (access(RC_RESUME_PATH, X_OK) == 0) {
89 snprintf(cmd, sizeof(cmd), "%s acpi %d", RC_RESUME_PATH,
95 err(EX_IOERR, "sleep type (%d) failed", sleep_type);
100 /* should be a acpi define, but doesn't appear to be */
101 #define UNKNOWN_CAP 0xffffffff
102 #define UNKNOWN_VOLTAGE 0xffffffff
105 acpi_battinfo(int num)
107 union acpi_battery_ioctl_arg battio;
108 const char *pwr_units;
111 if (num < 0 || num > 64)
112 err(EX_USAGE, "invalid battery %d", num);
114 /* Print battery design information. */
116 if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio) == -1)
117 err(EX_IOERR, "get battery info (%d) failed", num);
118 if (battio.bif.units == 0)
123 if (battio.bif.dcap == UNKNOWN_CAP)
124 printf("Design capacity:\tunknown\n");
126 printf("Design capacity:\t%d %sh\n", battio.bif.dcap,
128 if (battio.bif.lfcap == UNKNOWN_CAP)
129 printf("Last full capacity:\tunknown\n");
131 printf("Last full capacity:\t%d %sh\n", battio.bif.lfcap,
133 printf("Technology:\t\t%s\n", battio.bif.btech == 0 ?
134 "primary (non-rechargeable)" : "secondary (rechargeable)");
135 if (battio.bif.dvol == UNKNOWN_CAP)
136 printf("Design voltage:\t\tunknown\n");
138 printf("Design voltage:\t\t%d mV\n", battio.bif.dvol);
139 printf("Capacity (warn):\t%d %sh\n", battio.bif.wcap, pwr_units);
140 printf("Capacity (low):\t\t%d %sh\n", battio.bif.lcap, pwr_units);
141 printf("Low/warn granularity:\t%d %sh\n", battio.bif.gra1, pwr_units);
142 printf("Warn/full granularity:\t%d %sh\n", battio.bif.gra2, pwr_units);
143 printf("Model number:\t\t%s\n", battio.bif.model);
144 printf("Serial number:\t\t%s\n", battio.bif.serial);
145 printf("Type:\t\t\t%s\n", battio.bif.type);
146 printf("OEM info:\t\t%s\n", battio.bif.oeminfo);
148 /* Print current battery state information. */
150 if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1)
151 err(EX_IOERR, "get battery user info (%d) failed", num);
152 if (battio.battinfo.state != ACPI_BATT_STAT_NOT_PRESENT) {
153 printf("State:\t\t\t");
154 if (battio.battinfo.state == 0)
156 if (battio.battinfo.state & ACPI_BATT_STAT_CRITICAL)
158 if (battio.battinfo.state & ACPI_BATT_STAT_DISCHARG)
159 printf("discharging ");
160 if (battio.battinfo.state & ACPI_BATT_STAT_CHARGING)
163 if (battio.battinfo.cap == -1)
164 printf("Remaining capacity:\tunknown\n");
166 printf("Remaining capacity:\t%d%%\n",
167 battio.battinfo.cap);
168 if (battio.battinfo.min == -1)
169 printf("Remaining time:\t\tunknown\n");
171 hours = battio.battinfo.min / 60;
172 min = battio.battinfo.min % 60;
173 printf("Remaining time:\t\t%d:%02d\n", hours, min);
175 if (battio.battinfo.rate == -1)
176 printf("Present rate:\t\tunknown\n");
178 printf("Present rate:\t\t%d %s\n",
179 battio.battinfo.rate, pwr_units);
181 printf("State:\t\t\tnot present\n");
183 /* Print battery voltage information. */
185 if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1)
186 err(EX_IOERR, "get battery status (%d) failed", num);
187 if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT) {
188 if (battio.bst.volt == UNKNOWN_VOLTAGE)
189 printf("Voltage:\t\tunknown\n");
191 printf("Voltage:\t\t%d mV\n", battio.bst.volt);
198 usage(const char* prog)
200 printf("usage: %s [-deh] [-i batt] [-s 1-5]\n", prog);
205 main(int argc, char *argv[])
217 while ((c = getopt(argc, argv, "dehi:s:")) != -1) {
220 acpi_battinfo(atoi(optarg));
223 acpi_enable_disable(ACPIIO_DISABLE);
226 acpi_enable_disable(ACPIIO_ENABLE);
229 if (optarg[0] == 'S')
230 sleep_type = optarg[1] - '0';
232 sleep_type = optarg[0] - '0';
233 if (sleep_type < 0 || sleep_type > 5)
234 errx(EX_USAGE, "invalid sleep type (%d)",
246 if (sleep_type != -1) {
247 sleep(1); /* wait 1 sec. for key-release event */
248 acpi_sleep(sleep_type);