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