acpiconf.c revision 66490
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 66490 2000-09-30 20:13:57Z msmith $
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 <unistd.h>
37
38#include <dev/acpi/acpiio.h>
39#include <dev/acpi/acpireg.h>
40
41#define ACPIDEV	"/dev/acpi"
42
43static int
44acpi_enable_disable(int enable)
45{
46	int	fd;
47
48	fd  = open(ACPIDEV, O_RDWR);
49	if (fd == -1) {
50		err(1, NULL);
51	}
52	if (ioctl(fd, enable, NULL) == -1) {
53		err(1, NULL);
54	}
55	close(fd);
56
57	return (0);
58}
59
60static int
61acpi_sleep(int sleep_type)
62{
63	int	fd;
64
65	fd  = open(ACPIDEV, O_RDWR);
66	if (fd == -1) {
67		err(1, NULL);
68	}
69	if (ioctl(fd, ACPIIO_SETSLPSTATE, &sleep_type) == -1) {
70		err(1, NULL);
71	}
72	close(fd);
73
74	return (0);
75}
76
77int
78main(int argc, char *argv[])
79{
80	char	c;
81	int	sleep_type;
82
83	sleep_type = -1;
84	while ((c = getopt(argc, argv, "eds:")) != -1) {
85		switch (c) {
86		case 'e':
87			acpi_enable_disable(ACPIIO_ENABLE);
88			break;
89
90		case 'd':
91			acpi_enable_disable(ACPIIO_DISABLE);
92			break;
93
94		case 's':
95			sleep_type = *optarg - '0';
96			if (sleep_type < ACPI_S_STATE_S0 || sleep_type > ACPI_S_STATE_S5) {
97				fprintf(stderr, "%s: invalid sleep type (%d)\n",
98				    argv[0], sleep_type);
99				return -1;
100			}
101			break;
102		default:
103			argc -= optind;
104			argv += optind;
105		}
106	}
107
108	if (sleep_type != -1) {
109		sleep(1);	/* wait 1 sec. for key-release event */
110		acpi_sleep(sleep_type);
111	}
112	return (0);
113}
114