]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.sbin/acpi/acpiconf/acpiconf.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.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$
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 <dev/acpica/acpiio.h>
40
41 #include <contrib/dev/acpica/include/acpi.h>
42
43 #define ACPIDEV         "/dev/acpi"
44
45 static int      acpifd;
46
47 static void
48 acpi_init(void)
49 {
50         acpifd = open(ACPIDEV, O_RDWR);
51         if (acpifd == -1)
52                 acpifd = open(ACPIDEV, O_RDONLY);
53         if (acpifd == -1)
54                 err(EX_OSFILE, ACPIDEV);
55 }
56
57 /* Prepare to sleep and then wait for the signal that sleeping can occur. */
58 static void
59 acpi_sleep(int sleep_type)
60 {
61         int ret;
62   
63         /* Notify OS that we want to sleep.  devd(8) gets this notify. */
64         ret = ioctl(acpifd, ACPIIO_REQSLPSTATE, &sleep_type);
65         if (ret != 0)
66                 err(EX_IOERR, "request sleep type (%d) failed", sleep_type);
67 }
68
69 /* Ack or abort a pending suspend request. */
70 static void
71 acpi_sleep_ack(int err_val)
72 {
73         int ret;
74
75         ret = ioctl(acpifd, ACPIIO_ACKSLPSTATE, &err_val);
76         if (ret != 0)
77                 err(EX_IOERR, "ack sleep type failed");
78 }
79
80 /* should be a acpi define, but doesn't appear to be */
81 #define UNKNOWN_CAP 0xffffffff
82 #define UNKNOWN_VOLTAGE 0xffffffff
83
84 static int
85 acpi_battinfo(int num)
86 {
87         union acpi_battery_ioctl_arg battio;
88         const char *pwr_units;
89         int hours, min, amp;
90         uint32_t volt;
91
92         if (num < 0 || num > 64)
93                 err(EX_USAGE, "invalid battery %d", num);
94
95         /* Print battery design information. */
96         battio.unit = num;
97         if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio) == -1)
98                 err(EX_IOERR, "get battery info (%d) failed", num);
99         amp = battio.bif.units;
100         pwr_units = amp ? "mA" : "mW";
101         if (battio.bif.dcap == UNKNOWN_CAP)
102                 printf("Design capacity:\tunknown\n");
103         else
104                 printf("Design capacity:\t%d %sh\n", battio.bif.dcap,
105                     pwr_units);
106         if (battio.bif.lfcap == UNKNOWN_CAP)
107                 printf("Last full capacity:\tunknown\n");
108         else
109                 printf("Last full capacity:\t%d %sh\n", battio.bif.lfcap,
110                     pwr_units);
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");
115         else
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);
125
126         /* Fetch battery voltage information. */
127         volt = UNKNOWN_VOLTAGE;
128         battio.unit = num;
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;
133
134         /* Print current battery state information. */
135         battio.unit = num;
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                 const char *state;
140                 switch (battio.battinfo.state & ACPI_BATT_STAT_BST_MASK) {
141                 case 0:
142                         state = "high";
143                         break;
144                 case ACPI_BATT_STAT_DISCHARG:
145                         state = "discharging";
146                         break;
147                 case ACPI_BATT_STAT_CHARGING:
148                         state = "charging";
149                         break;
150                 case ACPI_BATT_STAT_CRITICAL:
151                         state = "critical";
152                         break;
153                 case ACPI_BATT_STAT_DISCHARG | ACPI_BATT_STAT_CRITICAL:
154                         state = "critical discharging";
155                         break;
156                 case ACPI_BATT_STAT_CHARGING | ACPI_BATT_STAT_CRITICAL:
157                         state = "critical charging";
158                         break;
159                 default:
160                         state = "invalid";
161                 }
162                 printf("State:\t\t\t%s\n", state);
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 if (amp && volt != UNKNOWN_VOLTAGE) {
178                         printf("Present rate:\t\t%d mA (%d mW)\n",
179                             battio.battinfo.rate,
180                             battio.battinfo.rate * volt / 1000);
181                 } else
182                         printf("Present rate:\t\t%d %s\n",
183                             battio.battinfo.rate, pwr_units);
184         } else
185                 printf("State:\t\t\tnot present\n");
186
187         /* Print battery voltage information. */
188         if (volt == UNKNOWN_VOLTAGE)
189                 printf("Present voltage:\tunknown\n");
190         else
191                 printf("Present voltage:\t%d mV\n", volt);
192
193         return (0);
194 }
195
196 static void
197 usage(const char* prog)
198 {
199         printf("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog);
200         exit(0);
201 }
202
203 int
204 main(int argc, char *argv[])
205 {
206         char    c, *prog;
207         int     sleep_type;
208
209         prog = argv[0];
210         if (argc < 2)
211                 usage(prog);
212                 /* NOTREACHED */
213
214         sleep_type = -1;
215         acpi_init();
216         while ((c = getopt(argc, argv, "hi:k:s:")) != -1) {
217                 switch (c) {
218                 case 'i':
219                         acpi_battinfo(atoi(optarg));
220                         break;
221                 case 'k':
222                         acpi_sleep_ack(atoi(optarg));
223                         break;
224                 case 's':
225                         if (optarg[0] == 'S')
226                                 sleep_type = optarg[1] - '0';
227                         else
228                                 sleep_type = optarg[0] - '0';
229                         if (sleep_type < 1 || sleep_type > 4)
230                                 errx(EX_USAGE, "invalid sleep type (%d)",
231                                      sleep_type);
232                         break;
233                 case 'h':
234                 default:
235                         usage(prog);
236                         /* NOTREACHED */
237                 }
238         }
239         argc -= optind;
240         argv += optind;
241
242         if (sleep_type != -1)
243                 acpi_sleep(sleep_type);
244
245         close(acpifd);
246         exit (0);
247 }