acpiconf.c revision 148310
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 148310 2005-07-22 19:19:00Z njl $
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>
40114246Snjl#include <contrib/dev/acpica/acpi.h>
4168475Siwasaki
42124001Snjl#define ACPIDEV		"/dev/acpi"
43124001Snjl#define RC_SUSPEND_PATH	"/etc/rc.suspend"
44124001Snjl#define RC_RESUME_PATH	"/etc/rc.resume"
4565283Siwasaki
46120036Snjlstatic int	acpifd;
47126625Stakawata
48137666Sphilipstatic void
49137638Sphilipacpi_init(void)
50120036Snjl{
51120036Snjl	acpifd = open(ACPIDEV, O_RDWR);
52137763Simp	if (acpifd == -1)
53126609Stakawata		acpifd = open(ACPIDEV, O_RDONLY);
54137763Simp	if (acpifd == -1)
55120036Snjl		err(EX_OSFILE, ACPIDEV);
56120036Snjl}
57120036Snjl
58120036Snjlstatic int
5965283Siwasakiacpi_sleep(int sleep_type)
6065283Siwasaki{
61124001Snjl	char cmd[64];
62124001Snjl	int ret;
63124001Snjl
64124001Snjl	/* Run the suspend rc script, if available. */
65124001Snjl	if (access(RC_SUSPEND_PATH, X_OK) == 0) {
66124001Snjl		snprintf(cmd, sizeof(cmd), "%s acpi %d", RC_SUSPEND_PATH,
67124001Snjl		    sleep_type);
68124001Snjl		system(cmd);
69124001Snjl	}
70124001Snjl
71124001Snjl	ret = ioctl(acpifd, ACPIIO_SETSLPSTATE, &sleep_type);
72124001Snjl
73124001Snjl	/* Run the resume rc script, if available. */
74124001Snjl	if (access(RC_RESUME_PATH, X_OK) == 0) {
75124001Snjl		snprintf(cmd, sizeof(cmd), "%s acpi %d", RC_RESUME_PATH,
76124001Snjl		    sleep_type);
77124001Snjl		system(cmd);
78124001Snjl	}
79124001Snjl
80124001Snjl	if (ret != 0)
8187121Scjc		err(EX_IOERR, "sleep type (%d) failed", sleep_type);
8265283Siwasaki
8365283Siwasaki	return (0);
8465283Siwasaki}
8565283Siwasaki
86138049Simp/* should be a acpi define, but doesn't appear to be */
87138049Simp#define UNKNOWN_CAP 0xffffffff
88138049Simp
89120036Snjlstatic int
90120036Snjlacpi_battinfo(int num)
91120036Snjl{
92120036Snjl	union acpi_battery_ioctl_arg battio;
93120036Snjl	const char *pwr_units;
94120036Snjl
95120036Snjl	if (num < 0 || num > 64)
96120036Snjl		err(EX_USAGE, "invalid battery %d", num);
97120036Snjl
98120036Snjl	battio.unit = num;
99148310Snjl	if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio) == -1)
100120036Snjl		err(EX_IOERR, "get battery info (%d) failed", num);
101120036Snjl	printf("Battery %d information\n", num);
102120036Snjl	if (battio.bif.units == 0)
103120036Snjl		pwr_units = "mWh";
104120036Snjl	else
105120036Snjl		pwr_units = "mAh";
106120036Snjl
107138049Simp	if (battio.bif.dcap == UNKNOWN_CAP)
108138049Simp		printf("Design capacity:\tUnknown\n");
109138049Simp	else
110138049Simp		printf("Design capacity:\t%d %s\n", battio.bif.dcap, pwr_units);
111138049Simp	if (battio.bif.lfcap == UNKNOWN_CAP)
112138049Simp		printf("Last full capacity:\tUnknown\n");
113138049Simp	else
114138049Simp		printf("Last full capacity:\t%d %s\n", battio.bif.lfcap,
115138049Simp		    pwr_units);
116120036Snjl	printf("Technology:\t\t%s\n", battio.bif.btech == 0 ?
117120036Snjl	    "primary (non-rechargeable)" : "secondary (rechargeable)");
118138049Simp	if (battio.bif.dvol == UNKNOWN_CAP)
119138049Simp		printf("Design voltage:\t\tUnknown\n");
120138049Simp	else
121138049Simp		printf("Design voltage:\t\t%d mV\n", battio.bif.dvol);
122120036Snjl	printf("Capacity (warn):\t%d %s\n", battio.bif.wcap, pwr_units);
123120036Snjl	printf("Capacity (low):\t\t%d %s\n", battio.bif.lcap, pwr_units);
124120036Snjl	printf("Low/warn granularity:\t%d %s\n", battio.bif.gra1, pwr_units);
125120036Snjl	printf("Warn/full granularity:\t%d %s\n", battio.bif.gra2, pwr_units);
126120036Snjl	printf("Model number:\t\t%s\n", battio.bif.model);
127120036Snjl	printf("Serial number:\t\t%s\n", battio.bif.serial);
128120036Snjl	printf("Type:\t\t\t%s\n", battio.bif.type);
129120036Snjl	printf("OEM info:\t\t%s\n", battio.bif.oeminfo);
130120036Snjl
131138044Sphk	battio.unit = num;
132148310Snjl	if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1)
133138044Sphk		err(EX_IOERR, "get battery info (%d) failed", num);
134138044Sphk
135138047Simp	if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT) {
136138049Simp		printf("State:\t\t\t");
137138049Simp		if (battio.bst.state & ACPI_BATT_STAT_CRITICAL)
138138049Simp			printf("CRITICAL ");
139138049Simp		if (battio.bst.state & ACPI_BATT_STAT_DISCHARG)
140138049Simp			printf("Discharging ");
141138049Simp		if (battio.bst.state & ACPI_BATT_STAT_CHARGING)
142138049Simp			printf("Charging");
143138049Simp		printf("\n");
144138049Simp		if (battio.bst.rate == UNKNOWN_CAP)
145138049Simp			printf("Present Rate:\t\tUnknown\n");
146138049Simp		else
147138049Simp			printf("Present Rate:\t\t%d %s\n", battio.bst.rate,
148138049Simp			    pwr_units);
149138049Simp		if (battio.bst.cap == UNKNOWN_CAP)
150138049Simp			printf("Remaining Capacity:\tUnknown\n");
151138049Simp		else
152138049Simp			printf("Remaining Capacity:\t%d %s\n", battio.bst.cap,
153138049Simp			    pwr_units);
154138049Simp		if (battio.bst.volt == UNKNOWN_CAP)
155138049Simp			printf("Volt:\t\t\tUnknown\n");
156138049Simp		else
157138049Simp			printf("Volt:\t\t\t%d mV\n", battio.bst.volt);
158138047Simp	} else {
159138047Simp		printf("State:\t\t\tNot Present\n");
160138047Simp	}
161120036Snjl	return (0);
162120036Snjl}
163120036Snjl
16468475Siwasakistatic void
16568475Siwasakiusage(const char* prog)
16668475Siwasaki{
167133933Snjl	printf("usage: %s [-h] [-i batt] [-s 1-5]\n", prog);
16868475Siwasaki	exit(0);
16968475Siwasaki}
17068475Siwasaki
17165283Siwasakiint
17265283Siwasakimain(int argc, char *argv[])
17365283Siwasaki{
17468475Siwasaki	char	c, *prog;
17565283Siwasaki	int	sleep_type;
17665283Siwasaki
17768475Siwasaki	prog = argv[0];
178120036Snjl	if (argc < 2)
179120036Snjl		usage(prog);
180120036Snjl		/* NOTREACHED */
181120036Snjl
18265283Siwasaki	sleep_type = -1;
183120036Snjl	acpi_init();
184133933Snjl	while ((c = getopt(argc, argv, "hi:s:")) != -1) {
18565283Siwasaki		switch (c) {
186120036Snjl		case 'i':
187120036Snjl			acpi_battinfo(atoi(optarg));
188120036Snjl			break;
18965283Siwasaki		case 's':
190118127Snjl			if (optarg[0] == 'S')
191118127Snjl				sleep_type = optarg[1] - '0';
192118127Snjl			else
193118127Snjl				sleep_type = optarg[0] - '0';
19487121Scjc			if (sleep_type < 0 || sleep_type > 5)
19587121Scjc				errx(EX_USAGE, "invalid sleep type (%d)",
196120036Snjl				     sleep_type);
19765283Siwasaki			break;
198120036Snjl		case 'h':
19965283Siwasaki		default:
200120036Snjl			usage(prog);
201120036Snjl			/* NOTREACHED */
20265283Siwasaki		}
20365283Siwasaki	}
204120036Snjl	argc -= optind;
205120036Snjl	argv += optind;
20665283Siwasaki
20765283Siwasaki	if (sleep_type != -1) {
20865283Siwasaki		sleep(1);	/* wait 1 sec. for key-release event */
20965283Siwasaki		acpi_sleep(sleep_type);
21065283Siwasaki	}
211120036Snjl
212120036Snjl	close(acpifd);
213120036Snjl	exit (0);
21465283Siwasaki}
215