600dac673dd14d87d23015dd2d469dd0ae79b112
[dragonfly.git] / usr.sbin / acpi / acpiconf / acpiconf.c
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: 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 $
29  */
30
31 #include <sys/param.h>
32
33 #include <err.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <sys/ioctl.h>
37 #include <sysexits.h>
38 #include <unistd.h>
39
40 #include "acpiio.h"
41 #include "acpi.h"
42
43 #define ACPIDEV         "/dev/acpi"
44 #define RC_SUSPEND_PATH "/etc/rc.suspend"
45 #define RC_RESUME_PATH  "/etc/rc.resume"
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 static int
60 acpi_enable_disable(int enable)
61 {
62         if (ioctl(acpifd, enable, NULL) == -1) {
63                 if (enable == ACPIIO_ENABLE)
64                         err(EX_IOERR, "enable failed");
65                 else
66                         err(EX_IOERR, "disable failed");
67         }
68
69         return (0);
70 }
71
72 static int
73 acpi_sleep(int sleep_type)
74 {
75         char cmd[64];
76         int ret;
77
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,
81                     sleep_type);
82                 system(cmd);
83         }
84
85         ret = ioctl(acpifd, ACPIIO_SETSLPSTATE, &sleep_type);
86
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,
90                     sleep_type);
91                 system(cmd);
92         }
93
94         if (ret != 0)
95                 err(EX_IOERR, "sleep type (%d) failed", sleep_type);
96
97         return (0);
98 }
99
100 /* should be a acpi define, but doesn't appear to be */
101 #define UNKNOWN_CAP 0xffffffff
102 #define UNKNOWN_VOLTAGE 0xffffffff
103
104 static int
105 acpi_battinfo(int num)
106 {
107         union acpi_battery_ioctl_arg battio;
108         const char *pwr_units;
109         int hours, min;
110
111         if (num < 0 || num > 64)
112                 err(EX_USAGE, "invalid battery %d", num);
113
114         /* Print battery design information. */
115         battio.unit = num;
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)
119                 pwr_units = "mW";
120         else
121                 pwr_units = "mA";
122
123         if (battio.bif.dcap == UNKNOWN_CAP)
124                 printf("Design capacity:\tunknown\n");
125         else
126                 printf("Design capacity:\t%d %sh\n", battio.bif.dcap,
127                     pwr_units);
128         if (battio.bif.lfcap == UNKNOWN_CAP)
129                 printf("Last full capacity:\tunknown\n");
130         else
131                 printf("Last full capacity:\t%d %sh\n", battio.bif.lfcap,
132                     pwr_units);
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");
137         else
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);
147
148         /* Print current battery state information. */
149         battio.unit = num;
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)
155                         printf("high ");
156                 if (battio.battinfo.state & ACPI_BATT_STAT_CRITICAL)
157                         printf("critical ");
158                 if (battio.battinfo.state & ACPI_BATT_STAT_DISCHARG)
159                         printf("discharging ");
160                 if (battio.battinfo.state & ACPI_BATT_STAT_CHARGING)
161                         printf("charging ");
162                 printf("\n");
163                 if (battio.battinfo.cap == -1)
164                         printf("Remaining capacity:\tunknown\n");
165                 else
166                         printf("Remaining capacity:\t%d%%\n",
167                             battio.battinfo.cap);
168                 if (battio.battinfo.min == -1)
169                         printf("Remaining time:\t\tunknown\n");
170                 else {
171                         hours = battio.battinfo.min / 60;
172                         min = battio.battinfo.min % 60;
173                         printf("Remaining time:\t\t%d:%02d\n", hours, min);
174                 }
175                 if (battio.battinfo.rate == -1)
176                         printf("Present rate:\t\tunknown\n");
177                 else
178                         printf("Present rate:\t\t%d %s\n",
179                             battio.battinfo.rate, pwr_units);
180         } else
181                 printf("State:\t\t\tnot present\n");
182
183         /* Print battery voltage information. */
184         battio.unit = num;
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");
190                 else
191                         printf("Voltage:\t\t%d mV\n", battio.bst.volt);
192         }
193
194         return (0);
195 }
196
197 static void
198 usage(const char* prog)
199 {
200         printf("usage: %s [-deh] [-i batt] [-s 1-5]\n", prog);
201         exit(0);
202 }
203
204 int
205 main(int argc, char *argv[])
206 {
207         char    c, *prog;
208         int     sleep_type;
209
210         prog = argv[0];
211         if (argc < 2)
212                 usage(prog);
213                 /* NOTREACHED */
214
215         sleep_type = -1;
216         acpi_init();
217         while ((c = getopt(argc, argv, "dehi:s:")) != -1) {
218                 switch (c) {
219                 case 'i':
220                         acpi_battinfo(atoi(optarg));
221                         break;
222                 case 'd':
223                         acpi_enable_disable(ACPIIO_DISABLE);
224                         break;
225                 case 'e':
226                         acpi_enable_disable(ACPIIO_ENABLE);
227                         break;
228                 case 's':
229                         if (optarg[0] == 'S')
230                                 sleep_type = optarg[1] - '0';
231                         else
232                                 sleep_type = optarg[0] - '0';
233                         if (sleep_type < 0 || sleep_type > 5)
234                                 errx(EX_USAGE, "invalid sleep type (%d)",
235                                      sleep_type);
236                         break;
237                 case 'h':
238                 default:
239                         usage(prog);
240                         /* NOTREACHED */
241                 }
242         }
243         argc -= optind;
244         argv += optind;
245
246         if (sleep_type != -1) {
247                 sleep(1);       /* wait 1 sec. for key-release event */
248                 acpi_sleep(sleep_type);
249         }
250
251         close(acpifd);
252         exit (0);
253 }