xref: /freebsd/usr.sbin/acpi/acpiconf/acpiconf.c (revision 77b7cdf1999ee965ad494fddd184b18f532ac91a)
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$
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 
45 static int
46 acpi_enable_disable(int enable)
47 {
48 	int	fd;
49 
50 	fd  = open(ACPIDEV, O_RDWR);
51 	if (fd == -1) {
52 		err(EX_OSFILE, ACPIDEV);
53 	}
54 	if (ioctl(fd, enable, NULL) == -1) {
55 		if (enable == ACPIIO_ENABLE)
56 			err(EX_IOERR, "enable failed");
57 		else
58 			err(EX_IOERR, "disable failed");
59 	}
60 	close(fd);
61 
62 	return (0);
63 }
64 
65 static int
66 acpi_sleep(int sleep_type)
67 {
68 	int	fd;
69 
70 	fd  = open(ACPIDEV, O_RDWR);
71 	if (fd == -1) {
72 		err(EX_OSFILE, ACPIDEV);
73 	}
74 	if (ioctl(fd, ACPIIO_SETSLPSTATE, &sleep_type) == -1) {
75 		err(EX_IOERR, "sleep type (%d) failed", sleep_type);
76 	}
77 	close(fd);
78 
79 	return (0);
80 }
81 
82 static void
83 usage(const char* prog)
84 {
85 	printf("usage: %s [-deh] [-s [1|2|3|4|4b|5]]\n", prog);
86 	exit(0);
87 }
88 
89 int
90 main(int argc, char *argv[])
91 {
92 	char	c, *prog;
93 	int	sleep_type;
94 
95 	prog = argv[0];
96 	sleep_type = -1;
97 	while ((c = getopt(argc, argv, "dehs:")) != -1) {
98 		switch (c) {
99 		case 'd':
100 			acpi_enable_disable(ACPIIO_DISABLE);
101 			break;
102 
103 		case 'e':
104 			acpi_enable_disable(ACPIIO_ENABLE);
105 			break;
106 
107 		case 'h':
108 			usage(prog);
109 			break;
110 
111 		case 's':
112 			sleep_type = optarg[0] - '0';
113 			if (sleep_type < 0 || sleep_type > 5)
114 				errx(EX_USAGE, "invalid sleep type (%d)",
115 				    sleep_type);
116 			break;
117 		default:
118 			argc -= optind;
119 			argv += optind;
120 		}
121 	}
122 
123 	if (sleep_type != -1) {
124 		sleep(1);	/* wait 1 sec. for key-release event */
125 		acpi_sleep(sleep_type);
126 	}
127 	return (0);
128 }
129