acpiconf.c revision 148491
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: head/usr.sbin/acpi/acpiconf/acpiconf.c 148491 2005-07-28 19:31:28Z njl $
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
46static int	acpifd;
47
48static void
49acpi_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
58static int
59acpi_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#define UNKNOWN_VOLTAGE 0xffffffff
89
90static int
91acpi_battinfo(int num)
92{
93	union acpi_battery_ioctl_arg battio;
94	const char *pwr_units;
95	int hours, min;
96
97	if (num < 0 || num > 64)
98		err(EX_USAGE, "invalid battery %d", num);
99
100	/* Print battery design information. */
101	battio.unit = num;
102	if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio) == -1)
103		err(EX_IOERR, "get battery info (%d) failed", num);
104	if (battio.bif.units == 0)
105		pwr_units = "mW";
106	else
107		pwr_units = "mA";
108
109	if (battio.bif.dcap == UNKNOWN_CAP)
110		printf("Design capacity:\tunknown\n");
111	else
112		printf("Design capacity:\t%d %sh\n", battio.bif.dcap,
113		    pwr_units);
114	if (battio.bif.lfcap == UNKNOWN_CAP)
115		printf("Last full capacity:\tunknown\n");
116	else
117		printf("Last full capacity:\t%d %sh\n", battio.bif.lfcap,
118		    pwr_units);
119	printf("Technology:\t\t%s\n", battio.bif.btech == 0 ?
120	    "primary (non-rechargeable)" : "secondary (rechargeable)");
121	if (battio.bif.dvol == UNKNOWN_CAP)
122		printf("Design voltage:\t\tunknown\n");
123	else
124		printf("Design voltage:\t\t%d mV\n", battio.bif.dvol);
125	printf("Capacity (warn):\t%d %sh\n", battio.bif.wcap, pwr_units);
126	printf("Capacity (low):\t\t%d %sh\n", battio.bif.lcap, pwr_units);
127	printf("Low/warn granularity:\t%d %sh\n", battio.bif.gra1, pwr_units);
128	printf("Warn/full granularity:\t%d %sh\n", battio.bif.gra2, pwr_units);
129	printf("Model number:\t\t%s\n", battio.bif.model);
130	printf("Serial number:\t\t%s\n", battio.bif.serial);
131	printf("Type:\t\t\t%s\n", battio.bif.type);
132	printf("OEM info:\t\t%s\n", battio.bif.oeminfo);
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		printf("State:\t\t\t");
140		if (battio.battinfo.state == 0)
141			printf("high ");
142		if (battio.battinfo.state & ACPI_BATT_STAT_CRITICAL)
143			printf("critical ");
144		if (battio.battinfo.state & ACPI_BATT_STAT_DISCHARG)
145			printf("discharging ");
146		if (battio.battinfo.state & ACPI_BATT_STAT_CHARGING)
147			printf("charging ");
148		printf("\n");
149		if (battio.battinfo.cap == -1)
150			printf("Remaining capacity:\tunknown\n");
151		else
152			printf("Remaining capacity:\t%d%%\n",
153			    battio.battinfo.cap);
154		if (battio.battinfo.min == -1)
155			printf("Remaining time:\tunknown\n");
156		else {
157			hours = battio.battinfo.min / 60;
158			min = battio.battinfo.min % 60;
159			printf("Remaining time:\t\t%d:%02d\n", hours, min);
160		}
161		if (battio.battinfo.rate == -1)
162			printf("Present rate:\t\tunknown\n");
163		else
164			printf("Present rate:\t\t%d %s\n",
165			    battio.battinfo.rate, pwr_units);
166	} else
167		printf("State:\t\t\tnot present\n");
168
169	/* Print battery voltage information. */
170	battio.unit = num;
171	if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1)
172		err(EX_IOERR, "get battery status (%d) failed", num);
173	if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT) {
174		if (battio.bst.volt == UNKNOWN_VOLTAGE)
175			printf("Voltage:\t\tunknown\n");
176		else
177			printf("Voltage:\t\t%d mV\n", battio.bst.volt);
178	}
179
180	return (0);
181}
182
183static void
184usage(const char* prog)
185{
186	printf("usage: %s [-h] [-i batt] [-s 1-5]\n", prog);
187	exit(0);
188}
189
190int
191main(int argc, char *argv[])
192{
193	char	c, *prog;
194	int	sleep_type;
195
196	prog = argv[0];
197	if (argc < 2)
198		usage(prog);
199		/* NOTREACHED */
200
201	sleep_type = -1;
202	acpi_init();
203	while ((c = getopt(argc, argv, "hi:s:")) != -1) {
204		switch (c) {
205		case 'i':
206			acpi_battinfo(atoi(optarg));
207			break;
208		case 's':
209			if (optarg[0] == 'S')
210				sleep_type = optarg[1] - '0';
211			else
212				sleep_type = optarg[0] - '0';
213			if (sleep_type < 0 || sleep_type > 5)
214				errx(EX_USAGE, "invalid sleep type (%d)",
215				     sleep_type);
216			break;
217		case 'h':
218		default:
219			usage(prog);
220			/* NOTREACHED */
221		}
222	}
223	argc -= optind;
224	argv += optind;
225
226	if (sleep_type != -1) {
227		sleep(1);	/* wait 1 sec. for key-release event */
228		acpi_sleep(sleep_type);
229	}
230
231	close(acpifd);
232	exit (0);
233}
234