acpiconf.c revision 193531
165283Siwasaki/*-
265283Siwasaki * Copyright (c) 1999 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
365283Siwasaki * All rights reserved.
465283Siwasaki *
565283Siwasaki * Redistribution and use in source and binary forms, with or without
665283Siwasaki * modification, are permitted provided that the following conditions
765283Siwasaki * are met:
865283Siwasaki * 1. Redistributions of source code must retain the above copyright
965283Siwasaki *    notice, this list of conditions and the following disclaimer.
1065283Siwasaki * 2. Redistributions in binary form must reproduce the above copyright
1165283Siwasaki *    notice, this list of conditions and the following disclaimer in the
1265283Siwasaki *    documentation and/or other materials provided with the distribution.
1365283Siwasaki *
1465283Siwasaki * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1565283Siwasaki * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1665283Siwasaki * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1765283Siwasaki * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1865283Siwasaki * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1965283Siwasaki * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2065283Siwasaki * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2165283Siwasaki * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2265283Siwasaki * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2365283Siwasaki * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2465283Siwasaki * SUCH DAMAGE.
2565283Siwasaki *
2665283Siwasaki *	$Id: acpiconf.c,v 1.5 2000/08/08 14:12:19 iwasaki Exp $
2765283Siwasaki *	$FreeBSD: head/usr.sbin/acpi/acpiconf/acpiconf.c 193531 2009-06-05 18:50:45Z jkim $
2865283Siwasaki */
2965283Siwasaki
3065283Siwasaki#include <sys/param.h>
3165283Siwasaki
3265283Siwasaki#include <err.h>
3365283Siwasaki#include <fcntl.h>
3465283Siwasaki#include <stdio.h>
3566490Smsmith#include <sys/ioctl.h>
3687121Scjc#include <sysexits.h>
3765283Siwasaki#include <unistd.h>
3865283Siwasaki
3968475Siwasaki#include <dev/acpica/acpiio.h>
4068475Siwasaki
41193531Sjkim#include <contrib/dev/acpica/include/acpi.h>
42193531Sjkim
43124001Snjl#define ACPIDEV		"/dev/acpi"
4465283Siwasaki
45120036Snjlstatic int	acpifd;
46126625Stakawata
47137666Sphilipstatic void
48137638Sphilipacpi_init(void)
49120036Snjl{
50120036Snjl	acpifd = open(ACPIDEV, O_RDWR);
51137763Simp	if (acpifd == -1)
52126609Stakawata		acpifd = open(ACPIDEV, O_RDONLY);
53137763Simp	if (acpifd == -1)
54120036Snjl		err(EX_OSFILE, ACPIDEV);
55120036Snjl}
56120036Snjl
57170976Snjl/* Prepare to sleep and then wait for the signal that sleeping can occur. */
58170976Snjlstatic void
5965283Siwasakiacpi_sleep(int sleep_type)
6065283Siwasaki{
61124001Snjl	int ret;
62170976Snjl
63170976Snjl	/* Notify OS that we want to sleep.  devd(8) gets this notify. */
64170976Snjl	ret = ioctl(acpifd, ACPIIO_REQSLPSTATE, &sleep_type);
65170976Snjl	if (ret != 0)
66170976Snjl		err(EX_IOERR, "request sleep type (%d) failed", sleep_type);
67170976Snjl}
68124001Snjl
69170976Snjl/* Ack or abort a pending suspend request. */
70170976Snjlstatic void
71170976Snjlacpi_sleep_ack(int err_val)
72170976Snjl{
73170976Snjl	int ret;
74124001Snjl
75170976Snjl	ret = ioctl(acpifd, ACPIIO_ACKSLPSTATE, &err_val);
76124001Snjl	if (ret != 0)
77170976Snjl		err(EX_IOERR, "ack sleep type failed");
7865283Siwasaki}
7965283Siwasaki
80138049Simp/* should be a acpi define, but doesn't appear to be */
81138049Simp#define UNKNOWN_CAP 0xffffffff
82148491Snjl#define UNKNOWN_VOLTAGE 0xffffffff
83138049Simp
84120036Snjlstatic int
85120036Snjlacpi_battinfo(int num)
86120036Snjl{
87120036Snjl	union acpi_battery_ioctl_arg battio;
88120036Snjl	const char *pwr_units;
89148491Snjl	int hours, min;
90120036Snjl
91120036Snjl	if (num < 0 || num > 64)
92120036Snjl		err(EX_USAGE, "invalid battery %d", num);
93120036Snjl
94148491Snjl	/* Print battery design information. */
95120036Snjl	battio.unit = num;
96148310Snjl	if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio) == -1)
97120036Snjl		err(EX_IOERR, "get battery info (%d) failed", num);
98120036Snjl	if (battio.bif.units == 0)
99148491Snjl		pwr_units = "mW";
100120036Snjl	else
101148491Snjl		pwr_units = "mA";
102120036Snjl
103138049Simp	if (battio.bif.dcap == UNKNOWN_CAP)
104148491Snjl		printf("Design capacity:\tunknown\n");
105138049Simp	else
106148491Snjl		printf("Design capacity:\t%d %sh\n", battio.bif.dcap,
107148491Snjl		    pwr_units);
108138049Simp	if (battio.bif.lfcap == UNKNOWN_CAP)
109148491Snjl		printf("Last full capacity:\tunknown\n");
110138049Simp	else
111148491Snjl		printf("Last full capacity:\t%d %sh\n", battio.bif.lfcap,
112138049Simp		    pwr_units);
113120036Snjl	printf("Technology:\t\t%s\n", battio.bif.btech == 0 ?
114120036Snjl	    "primary (non-rechargeable)" : "secondary (rechargeable)");
115138049Simp	if (battio.bif.dvol == UNKNOWN_CAP)
116148491Snjl		printf("Design voltage:\t\tunknown\n");
117138049Simp	else
118138049Simp		printf("Design voltage:\t\t%d mV\n", battio.bif.dvol);
119148491Snjl	printf("Capacity (warn):\t%d %sh\n", battio.bif.wcap, pwr_units);
120148491Snjl	printf("Capacity (low):\t\t%d %sh\n", battio.bif.lcap, pwr_units);
121148491Snjl	printf("Low/warn granularity:\t%d %sh\n", battio.bif.gra1, pwr_units);
122148491Snjl	printf("Warn/full granularity:\t%d %sh\n", battio.bif.gra2, pwr_units);
123120036Snjl	printf("Model number:\t\t%s\n", battio.bif.model);
124120036Snjl	printf("Serial number:\t\t%s\n", battio.bif.serial);
125120036Snjl	printf("Type:\t\t\t%s\n", battio.bif.type);
126120036Snjl	printf("OEM info:\t\t%s\n", battio.bif.oeminfo);
127120036Snjl
128148491Snjl	/* Print current battery state information. */
129138044Sphk	battio.unit = num;
130148491Snjl	if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1)
131148491Snjl		err(EX_IOERR, "get battery user info (%d) failed", num);
132148491Snjl	if (battio.battinfo.state != ACPI_BATT_STAT_NOT_PRESENT) {
133138049Simp		printf("State:\t\t\t");
134148491Snjl		if (battio.battinfo.state == 0)
135148491Snjl			printf("high ");
136148491Snjl		if (battio.battinfo.state & ACPI_BATT_STAT_CRITICAL)
137148491Snjl			printf("critical ");
138148491Snjl		if (battio.battinfo.state & ACPI_BATT_STAT_DISCHARG)
139148491Snjl			printf("discharging ");
140148491Snjl		if (battio.battinfo.state & ACPI_BATT_STAT_CHARGING)
141148491Snjl			printf("charging ");
142138049Simp		printf("\n");
143148491Snjl		if (battio.battinfo.cap == -1)
144148491Snjl			printf("Remaining capacity:\tunknown\n");
145138049Simp		else
146148491Snjl			printf("Remaining capacity:\t%d%%\n",
147148491Snjl			    battio.battinfo.cap);
148148491Snjl		if (battio.battinfo.min == -1)
149148493Snjl			printf("Remaining time:\t\tunknown\n");
150148491Snjl		else {
151148491Snjl			hours = battio.battinfo.min / 60;
152148491Snjl			min = battio.battinfo.min % 60;
153148491Snjl			printf("Remaining time:\t\t%d:%02d\n", hours, min);
154148491Snjl		}
155148491Snjl		if (battio.battinfo.rate == -1)
156148491Snjl			printf("Present rate:\t\tunknown\n");
157138049Simp		else
158148491Snjl			printf("Present rate:\t\t%d %s\n",
159148491Snjl			    battio.battinfo.rate, pwr_units);
160148491Snjl	} else
161148491Snjl		printf("State:\t\t\tnot present\n");
162148491Snjl
163148491Snjl	/* Print battery voltage information. */
164148491Snjl	battio.unit = num;
165148491Snjl	if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1)
166148491Snjl		err(EX_IOERR, "get battery status (%d) failed", num);
167148491Snjl	if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT) {
168148491Snjl		if (battio.bst.volt == UNKNOWN_VOLTAGE)
169148491Snjl			printf("Voltage:\t\tunknown\n");
170138049Simp		else
171148491Snjl			printf("Voltage:\t\t%d mV\n", battio.bst.volt);
172138047Simp	}
173148491Snjl
174120036Snjl	return (0);
175120036Snjl}
176120036Snjl
17768475Siwasakistatic void
17868475Siwasakiusage(const char* prog)
17968475Siwasaki{
180170976Snjl	printf("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog);
18168475Siwasaki	exit(0);
18268475Siwasaki}
18368475Siwasaki
18465283Siwasakiint
18565283Siwasakimain(int argc, char *argv[])
18665283Siwasaki{
18768475Siwasaki	char	c, *prog;
18865283Siwasaki	int	sleep_type;
18965283Siwasaki
19068475Siwasaki	prog = argv[0];
191120036Snjl	if (argc < 2)
192120036Snjl		usage(prog);
193120036Snjl		/* NOTREACHED */
194120036Snjl
19565283Siwasaki	sleep_type = -1;
196120036Snjl	acpi_init();
197170976Snjl	while ((c = getopt(argc, argv, "hi:k:s:")) != -1) {
19865283Siwasaki		switch (c) {
199120036Snjl		case 'i':
200120036Snjl			acpi_battinfo(atoi(optarg));
201120036Snjl			break;
202170976Snjl		case 'k':
203170976Snjl			acpi_sleep_ack(atoi(optarg));
204170976Snjl			break;
20565283Siwasaki		case 's':
206118127Snjl			if (optarg[0] == 'S')
207118127Snjl				sleep_type = optarg[1] - '0';
208118127Snjl			else
209118127Snjl				sleep_type = optarg[0] - '0';
210170976Snjl			if (sleep_type < 1 || sleep_type > 4)
21187121Scjc				errx(EX_USAGE, "invalid sleep type (%d)",
212120036Snjl				     sleep_type);
21365283Siwasaki			break;
214120036Snjl		case 'h':
21565283Siwasaki		default:
216120036Snjl			usage(prog);
217120036Snjl			/* NOTREACHED */
21865283Siwasaki		}
21965283Siwasaki	}
220120036Snjl	argc -= optind;
221120036Snjl	argv += optind;
22265283Siwasaki
223170976Snjl	if (sleep_type != -1)
22465283Siwasaki		acpi_sleep(sleep_type);
225120036Snjl
226120036Snjl	close(acpifd);
227120036Snjl	exit (0);
22865283Siwasaki}
229