acpiconf.c revision 137638
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 137638 2004-11-13 01:54:22Z philip $
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
4865283Siwasakistatic int
49137638Sphilipacpi_init(void)
50120036Snjl{
51120036Snjl	acpifd = open(ACPIDEV, O_RDWR);
52126609Stakawata	if (acpifd == -1){
53126609Stakawata		acpifd = open(ACPIDEV, O_RDONLY);
54126609Stakawata	}
55126609Stakawata	if (acpifd == -1){
56120036Snjl		err(EX_OSFILE, ACPIDEV);
57126609Stakawata	}
58120036Snjl}
59120036Snjl
60120036Snjlstatic int
6165283Siwasakiacpi_sleep(int sleep_type)
6265283Siwasaki{
63124001Snjl	char cmd[64];
64124001Snjl	int ret;
65124001Snjl
66124001Snjl	/* Run the suspend rc script, if available. */
67124001Snjl	if (access(RC_SUSPEND_PATH, X_OK) == 0) {
68124001Snjl		snprintf(cmd, sizeof(cmd), "%s acpi %d", RC_SUSPEND_PATH,
69124001Snjl		    sleep_type);
70124001Snjl		system(cmd);
71124001Snjl	}
72124001Snjl
73124001Snjl	ret = ioctl(acpifd, ACPIIO_SETSLPSTATE, &sleep_type);
74124001Snjl
75124001Snjl	/* Run the resume rc script, if available. */
76124001Snjl	if (access(RC_RESUME_PATH, X_OK) == 0) {
77124001Snjl		snprintf(cmd, sizeof(cmd), "%s acpi %d", RC_RESUME_PATH,
78124001Snjl		    sleep_type);
79124001Snjl		system(cmd);
80124001Snjl	}
81124001Snjl
82124001Snjl	if (ret != 0)
8387121Scjc		err(EX_IOERR, "sleep type (%d) failed", sleep_type);
8465283Siwasaki
8565283Siwasaki	return (0);
8665283Siwasaki}
8765283Siwasaki
88120036Snjlstatic int
89120036Snjlacpi_battinfo(int num)
90120036Snjl{
91120036Snjl	union acpi_battery_ioctl_arg battio;
92120036Snjl	const char *pwr_units;
93120036Snjl
94120036Snjl	if (num < 0 || num > 64)
95120036Snjl		err(EX_USAGE, "invalid battery %d", num);
96120036Snjl
97120036Snjl	battio.unit = num;
98120036Snjl	if (ioctl(acpifd, ACPIIO_CMBAT_GET_BIF, &battio) == -1)
99120036Snjl		err(EX_IOERR, "get battery info (%d) failed", num);
100120036Snjl	printf("Battery %d information\n", num);
101120036Snjl	if (battio.bif.units == 0)
102120036Snjl		pwr_units = "mWh";
103120036Snjl	else
104120036Snjl		pwr_units = "mAh";
105120036Snjl
106120036Snjl	printf("Design capacity:\t%d %s\n", battio.bif.dcap, pwr_units);
107120036Snjl	printf("Last full capacity:\t%d %s\n", battio.bif.lfcap, pwr_units);
108120036Snjl	printf("Technology:\t\t%s\n", battio.bif.btech == 0 ?
109120036Snjl	    "primary (non-rechargeable)" : "secondary (rechargeable)");
110120036Snjl	printf("Design voltage:\t\t%d mV\n", battio.bif.dvol);
111120036Snjl	printf("Capacity (warn):\t%d %s\n", battio.bif.wcap, pwr_units);
112120036Snjl	printf("Capacity (low):\t\t%d %s\n", battio.bif.lcap, pwr_units);
113120036Snjl	printf("Low/warn granularity:\t%d %s\n", battio.bif.gra1, pwr_units);
114120036Snjl	printf("Warn/full granularity:\t%d %s\n", battio.bif.gra2, pwr_units);
115120036Snjl	printf("Model number:\t\t%s\n", battio.bif.model);
116120036Snjl	printf("Serial number:\t\t%s\n", battio.bif.serial);
117120036Snjl	printf("Type:\t\t\t%s\n", battio.bif.type);
118120036Snjl	printf("OEM info:\t\t%s\n", battio.bif.oeminfo);
119120036Snjl
120120036Snjl	return (0);
121120036Snjl}
122120036Snjl
12368475Siwasakistatic void
12468475Siwasakiusage(const char* prog)
12568475Siwasaki{
126133933Snjl	printf("usage: %s [-h] [-i batt] [-s 1-5]\n", prog);
12768475Siwasaki	exit(0);
12868475Siwasaki}
12968475Siwasaki
13065283Siwasakiint
13165283Siwasakimain(int argc, char *argv[])
13265283Siwasaki{
13368475Siwasaki	char	c, *prog;
13465283Siwasaki	int	sleep_type;
13565283Siwasaki
13668475Siwasaki	prog = argv[0];
137120036Snjl	if (argc < 2)
138120036Snjl		usage(prog);
139120036Snjl		/* NOTREACHED */
140120036Snjl
14165283Siwasaki	sleep_type = -1;
142120036Snjl	acpi_init();
143133933Snjl	while ((c = getopt(argc, argv, "hi:s:")) != -1) {
14465283Siwasaki		switch (c) {
145120036Snjl		case 'i':
146120036Snjl			acpi_battinfo(atoi(optarg));
147120036Snjl			break;
14865283Siwasaki		case 's':
149118127Snjl			if (optarg[0] == 'S')
150118127Snjl				sleep_type = optarg[1] - '0';
151118127Snjl			else
152118127Snjl				sleep_type = optarg[0] - '0';
15387121Scjc			if (sleep_type < 0 || sleep_type > 5)
15487121Scjc				errx(EX_USAGE, "invalid sleep type (%d)",
155120036Snjl				     sleep_type);
15665283Siwasaki			break;
157120036Snjl		case 'h':
15865283Siwasaki		default:
159120036Snjl			usage(prog);
160120036Snjl			/* NOTREACHED */
16165283Siwasaki		}
16265283Siwasaki	}
163120036Snjl	argc -= optind;
164120036Snjl	argv += optind;
16565283Siwasaki
16665283Siwasaki	if (sleep_type != -1) {
16765283Siwasaki		sleep(1);	/* wait 1 sec. for key-release event */
16865283Siwasaki		acpi_sleep(sleep_type);
16965283Siwasaki	}
170120036Snjl
171120036Snjl	close(acpifd);
172120036Snjl	exit (0);
17365283Siwasaki}
174