xref: /freebsd/usr.sbin/acpi/acpiconf/acpiconf.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
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 #include <contrib/dev/acpica/acpi.h>
41 
42 #define ACPIDEV		"/dev/acpi"
43 #define RC_SUSPEND_PATH	"/etc/rc.suspend"
44 #define RC_RESUME_PATH	"/etc/rc.resume"
45 
46 static int	acpifd;
47 
48 static void
49 acpi_init(void)
50 {
51 	acpifd = open(ACPIDEV, O_RDWR);
52 	if (acpifd == -1)
53 		acpifd = open(ACPIDEV, O_RDONLY);
54 	if (acpifd == -1)
55 		err(EX_OSFILE, ACPIDEV);
56 }
57 
58 static int
59 acpi_sleep(int sleep_type)
60 {
61 	char cmd[64];
62 	int ret;
63 
64 	/* Run the suspend rc script, if available. */
65 	if (access(RC_SUSPEND_PATH, X_OK) == 0) {
66 		snprintf(cmd, sizeof(cmd), "%s acpi %d", RC_SUSPEND_PATH,
67 		    sleep_type);
68 		system(cmd);
69 	}
70 
71 	ret = ioctl(acpifd, ACPIIO_SETSLPSTATE, &sleep_type);
72 
73 	/* Run the resume rc script, if available. */
74 	if (access(RC_RESUME_PATH, X_OK) == 0) {
75 		snprintf(cmd, sizeof(cmd), "%s acpi %d", RC_RESUME_PATH,
76 		    sleep_type);
77 		system(cmd);
78 	}
79 
80 	if (ret != 0)
81 		err(EX_IOERR, "sleep type (%d) failed", sleep_type);
82 
83 	return (0);
84 }
85 
86 /* should be a acpi define, but doesn't appear to be */
87 #define UNKNOWN_CAP 0xffffffff
88 
89 static int
90 acpi_battinfo(int num)
91 {
92 	union acpi_battery_ioctl_arg battio;
93 	const char *pwr_units;
94 
95 	if (num < 0 || num > 64)
96 		err(EX_USAGE, "invalid battery %d", num);
97 
98 	battio.unit = num;
99 	if (ioctl(acpifd, ACPIIO_CMBAT_GET_BIF, &battio) == -1)
100 		err(EX_IOERR, "get battery info (%d) failed", num);
101 	printf("Battery %d information\n", num);
102 	if (battio.bif.units == 0)
103 		pwr_units = "mWh";
104 	else
105 		pwr_units = "mAh";
106 
107 	if (battio.bif.dcap == UNKNOWN_CAP)
108 		printf("Design capacity:\tUnknown\n");
109 	else
110 		printf("Design capacity:\t%d %s\n", battio.bif.dcap, pwr_units);
111 	if (battio.bif.lfcap == UNKNOWN_CAP)
112 		printf("Last full capacity:\tUnknown\n");
113 	else
114 		printf("Last full capacity:\t%d %s\n", battio.bif.lfcap,
115 		    pwr_units);
116 	printf("Technology:\t\t%s\n", battio.bif.btech == 0 ?
117 	    "primary (non-rechargeable)" : "secondary (rechargeable)");
118 	if (battio.bif.dvol == UNKNOWN_CAP)
119 		printf("Design voltage:\t\tUnknown\n");
120 	else
121 		printf("Design voltage:\t\t%d mV\n", battio.bif.dvol);
122 	printf("Capacity (warn):\t%d %s\n", battio.bif.wcap, pwr_units);
123 	printf("Capacity (low):\t\t%d %s\n", battio.bif.lcap, pwr_units);
124 	printf("Low/warn granularity:\t%d %s\n", battio.bif.gra1, pwr_units);
125 	printf("Warn/full granularity:\t%d %s\n", battio.bif.gra2, pwr_units);
126 	printf("Model number:\t\t%s\n", battio.bif.model);
127 	printf("Serial number:\t\t%s\n", battio.bif.serial);
128 	printf("Type:\t\t\t%s\n", battio.bif.type);
129 	printf("OEM info:\t\t%s\n", battio.bif.oeminfo);
130 
131 	battio.unit = num;
132 	if (ioctl(acpifd, ACPIIO_CMBAT_GET_BST, &battio) == -1)
133 		err(EX_IOERR, "get battery info (%d) failed", num);
134 
135 	if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT) {
136 		printf("State:\t\t\t");
137 		if (battio.bst.state & ACPI_BATT_STAT_CRITICAL)
138 			printf("CRITICAL ");
139 		if (battio.bst.state & ACPI_BATT_STAT_DISCHARG)
140 			printf("Discharging ");
141 		if (battio.bst.state & ACPI_BATT_STAT_CHARGING)
142 			printf("Charging");
143 		printf("\n");
144 		if (battio.bst.rate == UNKNOWN_CAP)
145 			printf("Present Rate:\t\tUnknown\n");
146 		else
147 			printf("Present Rate:\t\t%d %s\n", battio.bst.rate,
148 			    pwr_units);
149 		if (battio.bst.cap == UNKNOWN_CAP)
150 			printf("Remaining Capacity:\tUnknown\n");
151 		else
152 			printf("Remaining Capacity:\t%d %s\n", battio.bst.cap,
153 			    pwr_units);
154 		if (battio.bst.volt == UNKNOWN_CAP)
155 			printf("Volt:\t\t\tUnknown\n");
156 		else
157 			printf("Volt:\t\t\t%d mV\n", battio.bst.volt);
158 	} else {
159 		printf("State:\t\t\tNot Present\n");
160 	}
161 	return (0);
162 }
163 
164 static void
165 usage(const char* prog)
166 {
167 	printf("usage: %s [-h] [-i batt] [-s 1-5]\n", prog);
168 	exit(0);
169 }
170 
171 int
172 main(int argc, char *argv[])
173 {
174 	char	c, *prog;
175 	int	sleep_type;
176 
177 	prog = argv[0];
178 	if (argc < 2)
179 		usage(prog);
180 		/* NOTREACHED */
181 
182 	sleep_type = -1;
183 	acpi_init();
184 	while ((c = getopt(argc, argv, "hi:s:")) != -1) {
185 		switch (c) {
186 		case 'i':
187 			acpi_battinfo(atoi(optarg));
188 			break;
189 		case 's':
190 			if (optarg[0] == 'S')
191 				sleep_type = optarg[1] - '0';
192 			else
193 				sleep_type = optarg[0] - '0';
194 			if (sleep_type < 0 || sleep_type > 5)
195 				errx(EX_USAGE, "invalid sleep type (%d)",
196 				     sleep_type);
197 			break;
198 		case 'h':
199 		default:
200 			usage(prog);
201 			/* NOTREACHED */
202 		}
203 	}
204 	argc -= optind;
205 	argv += optind;
206 
207 	if (sleep_type != -1) {
208 		sleep(1);	/* wait 1 sec. for key-release event */
209 		acpi_sleep(sleep_type);
210 	}
211 
212 	close(acpifd);
213 	exit (0);
214 }
215