acpiconf.c revision 120036
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 120036 2003-09-13 20:13:01Z 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
41#include <contrib/dev/acpica/acpi.h>
42
43#define ACPIDEV	"/dev/acpi"
44
45static int	acpifd;
46
47static int
48acpi_init()
49{
50	acpifd = open(ACPIDEV, O_RDWR);
51	if (acpifd == -1)
52		err(EX_OSFILE, ACPIDEV);
53}
54
55static int
56acpi_enable_disable(int enable)
57{
58	if (ioctl(acpifd, enable, NULL) == -1) {
59		if (enable == ACPIIO_ENABLE)
60			err(EX_IOERR, "enable failed");
61		else
62			err(EX_IOERR, "disable failed");
63	}
64
65	return (0);
66}
67
68static int
69acpi_sleep(int sleep_type)
70{
71	if (ioctl(acpifd, ACPIIO_SETSLPSTATE, &sleep_type) == -1)
72		err(EX_IOERR, "sleep type (%d) failed", sleep_type);
73
74	return (0);
75}
76
77static int
78acpi_battinfo(int num)
79{
80	union acpi_battery_ioctl_arg battio;
81	const char *pwr_units;
82
83	if (num < 0 || num > 64)
84		err(EX_USAGE, "invalid battery %d", num);
85
86	battio.unit = num;
87	if (ioctl(acpifd, ACPIIO_CMBAT_GET_BIF, &battio) == -1)
88		err(EX_IOERR, "get battery info (%d) failed", num);
89	printf("Battery %d information\n", num);
90	if (battio.bif.units == 0)
91		pwr_units = "mWh";
92	else
93		pwr_units = "mAh";
94
95	printf("Design capacity:\t%d %s\n", battio.bif.dcap, pwr_units);
96	printf("Last full capacity:\t%d %s\n", battio.bif.lfcap, pwr_units);
97	printf("Technology:\t\t%s\n", battio.bif.btech == 0 ?
98	    "primary (non-rechargeable)" : "secondary (rechargeable)");
99	printf("Design voltage:\t\t%d mV\n", battio.bif.dvol);
100	printf("Capacity (warn):\t%d %s\n", battio.bif.wcap, pwr_units);
101	printf("Capacity (low):\t\t%d %s\n", battio.bif.lcap, pwr_units);
102	printf("Low/warn granularity:\t%d %s\n", battio.bif.gra1, pwr_units);
103	printf("Warn/full granularity:\t%d %s\n", battio.bif.gra2, pwr_units);
104	printf("Model number:\t\t%s\n", battio.bif.model);
105	printf("Serial number:\t\t%s\n", battio.bif.serial);
106	printf("Type:\t\t\t%s\n", battio.bif.type);
107	printf("OEM info:\t\t%s\n", battio.bif.oeminfo);
108
109	return (0);
110}
111
112static void
113usage(const char* prog)
114{
115	printf("usage: %s [-deh] [-i batt] [-s 1-5]\n", prog);
116	exit(0);
117}
118
119int
120main(int argc, char *argv[])
121{
122	char	c, *prog;
123	int	sleep_type;
124
125	prog = argv[0];
126	if (argc < 2)
127		usage(prog);
128		/* NOTREACHED */
129
130	sleep_type = -1;
131	acpi_init();
132	while ((c = getopt(argc, argv, "dehi:s:")) != -1) {
133		switch (c) {
134		case 'i':
135			acpi_battinfo(atoi(optarg));
136			break;
137		case 'd':
138			acpi_enable_disable(ACPIIO_DISABLE);
139			break;
140		case 'e':
141			acpi_enable_disable(ACPIIO_ENABLE);
142			break;
143		case 's':
144			if (optarg[0] == 'S')
145				sleep_type = optarg[1] - '0';
146			else
147				sleep_type = optarg[0] - '0';
148			if (sleep_type < 0 || sleep_type > 5)
149				errx(EX_USAGE, "invalid sleep type (%d)",
150				     sleep_type);
151			break;
152		case 'h':
153		default:
154			usage(prog);
155			/* NOTREACHED */
156		}
157	}
158	argc -= optind;
159	argv += optind;
160
161	if (sleep_type != -1) {
162		sleep(1);	/* wait 1 sec. for key-release event */
163		acpi_sleep(sleep_type);
164	}
165
166	close(acpifd);
167	exit (0);
168}
169