xref: /freebsd/usr.sbin/acpi/acpiconf/acpiconf.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
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 <unistd.h>
37 
38 #include <dev/acpica/acpiio.h>
39 
40 #include <contrib/dev/acpica/acgcc.h>
41 #include <contrib/dev/acpica/actypes.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(1, NULL);
53 	}
54 	if (ioctl(fd, enable, NULL) == -1) {
55 		err(1, NULL);
56 	}
57 	close(fd);
58 
59 	return (0);
60 }
61 
62 static int
63 acpi_sleep(int sleep_type)
64 {
65 	int	fd;
66 
67 	fd  = open(ACPIDEV, O_RDWR);
68 	if (fd == -1) {
69 		err(1, NULL);
70 	}
71 	if (ioctl(fd, ACPIIO_SETSLPSTATE, &sleep_type) == -1) {
72 		err(1, NULL);
73 	}
74 	close(fd);
75 
76 	return (0);
77 }
78 
79 static void
80 usage(const char* prog)
81 {
82 	printf("usage: %s [-deh] [-s [1|2|3|4|4b|5]]\n", prog);
83 	exit(0);
84 }
85 
86 int
87 main(int argc, char *argv[])
88 {
89 	char	c, *prog;
90 	int	sleep_type;
91 
92 	prog = argv[0];
93 	sleep_type = -1;
94 	while ((c = getopt(argc, argv, "dehs:")) != -1) {
95 		switch (c) {
96 		case 'd':
97 			acpi_enable_disable(ACPIIO_DISABLE);
98 			break;
99 
100 		case 'e':
101 			acpi_enable_disable(ACPIIO_ENABLE);
102 			break;
103 
104 		case 'h':
105 			usage(prog);
106 			break;
107 
108 		case 's':
109 			sleep_type = optarg[0] - '0';
110 			if (sleep_type < 0 || sleep_type > 5) {
111 				fprintf(stderr, "%s: invalid sleep type (%d)\n",
112 				    argv[0], sleep_type);
113 				return -1;
114 			}
115 
116 			/* convert sleep type value to ACPICA format */
117 			switch (sleep_type) {
118 			case 4:
119 				if (optarg[1] == 'b') {
120 					sleep_type = ACPI_STATE_S4BIOS;
121 				}
122 				break;
123 
124 			case 5:
125 				sleep_type = ACPI_STATE_S5;
126 				break;
127 
128 			default:
129 				break;
130 			}
131 			break;
132 		default:
133 			argc -= optind;
134 			argv += optind;
135 		}
136 	}
137 
138 	if (sleep_type != -1) {
139 		sleep(1);	/* wait 1 sec. for key-release event */
140 		acpi_sleep(sleep_type);
141 	}
142 	return (0);
143 }
144