Deleted Added
full compact
acpiconf.c (148493) acpiconf.c (170976)
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

--- 10 unchanged lines hidden (view full) ---

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 $
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

--- 10 unchanged lines hidden (view full) ---

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 148493 2005-07-28 19:41:52Z njl $
27 * $FreeBSD: head/usr.sbin/acpi/acpiconf/acpiconf.c 170976 2007-06-21 22:50:37Z 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#include <contrib/dev/acpica/acpi.h>
41
42#define ACPIDEV "/dev/acpi"
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#include <contrib/dev/acpica/acpi.h>
41
42#define ACPIDEV "/dev/acpi"
43#define RC_SUSPEND_PATH "/etc/rc.suspend"
44#define RC_RESUME_PATH "/etc/rc.resume"
45
46static int acpifd;
47
48static void
49acpi_init(void)
50{
51 acpifd = open(ACPIDEV, O_RDWR);
52 if (acpifd == -1)
53 acpifd = open(ACPIDEV, O_RDONLY);
54 if (acpifd == -1)
55 err(EX_OSFILE, ACPIDEV);
56}
57
43
44static int acpifd;
45
46static void
47acpi_init(void)
48{
49 acpifd = open(ACPIDEV, O_RDWR);
50 if (acpifd == -1)
51 acpifd = open(ACPIDEV, O_RDONLY);
52 if (acpifd == -1)
53 err(EX_OSFILE, ACPIDEV);
54}
55
58static int
56/* Prepare to sleep and then wait for the signal that sleeping can occur. */
57static void
59acpi_sleep(int sleep_type)
60{
58acpi_sleep(int sleep_type)
59{
61 char cmd[64];
62 int ret;
60 int ret;
61
62 /* Notify OS that we want to sleep. devd(8) gets this notify. */
63 ret = ioctl(acpifd, ACPIIO_REQSLPSTATE, &sleep_type);
64 if (ret != 0)
65 err(EX_IOERR, "request sleep type (%d) failed", sleep_type);
66}
63
67
64 /* Run the suspend rc script, if available. */
65 if (access(RC_SUSPEND_PATH, X_OK) == 0) {
66 snprintf(cmd, sizeof(cmd), "%s acpi %d", RC_SUSPEND_PATH,
67 sleep_type);
68 system(cmd);
69 }
68/* Ack or abort a pending suspend request. */
69static void
70acpi_sleep_ack(int err_val)
71{
72 int ret;
70
73
71 ret = ioctl(acpifd, ACPIIO_SETSLPSTATE, &sleep_type);
72
73 /* Run the resume rc script, if available. */
74 if (access(RC_RESUME_PATH, X_OK) == 0) {
75 snprintf(cmd, sizeof(cmd), "%s acpi %d", RC_RESUME_PATH,
76 sleep_type);
77 system(cmd);
78 }
79
74 ret = ioctl(acpifd, ACPIIO_ACKSLPSTATE, &err_val);
80 if (ret != 0)
75 if (ret != 0)
81 err(EX_IOERR, "sleep type (%d) failed", sleep_type);
82
83 return (0);
76 err(EX_IOERR, "ack sleep type failed");
84}
85
86/* should be a acpi define, but doesn't appear to be */
87#define UNKNOWN_CAP 0xffffffff
88#define UNKNOWN_VOLTAGE 0xffffffff
89
90static int
91acpi_battinfo(int num)

--- 86 unchanged lines hidden (view full) ---

178 }
179
180 return (0);
181}
182
183static void
184usage(const char* prog)
185{
77}
78
79/* should be a acpi define, but doesn't appear to be */
80#define UNKNOWN_CAP 0xffffffff
81#define UNKNOWN_VOLTAGE 0xffffffff
82
83static int
84acpi_battinfo(int num)

--- 86 unchanged lines hidden (view full) ---

171 }
172
173 return (0);
174}
175
176static void
177usage(const char* prog)
178{
186 printf("usage: %s [-h] [-i batt] [-s 1-5]\n", prog);
179 printf("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog);
187 exit(0);
188}
189
190int
191main(int argc, char *argv[])
192{
193 char c, *prog;
194 int sleep_type;
195
196 prog = argv[0];
197 if (argc < 2)
198 usage(prog);
199 /* NOTREACHED */
200
201 sleep_type = -1;
202 acpi_init();
180 exit(0);
181}
182
183int
184main(int argc, char *argv[])
185{
186 char c, *prog;
187 int sleep_type;
188
189 prog = argv[0];
190 if (argc < 2)
191 usage(prog);
192 /* NOTREACHED */
193
194 sleep_type = -1;
195 acpi_init();
203 while ((c = getopt(argc, argv, "hi:s:")) != -1) {
196 while ((c = getopt(argc, argv, "hi:k:s:")) != -1) {
204 switch (c) {
205 case 'i':
206 acpi_battinfo(atoi(optarg));
207 break;
197 switch (c) {
198 case 'i':
199 acpi_battinfo(atoi(optarg));
200 break;
201 case 'k':
202 acpi_sleep_ack(atoi(optarg));
203 break;
208 case 's':
209 if (optarg[0] == 'S')
210 sleep_type = optarg[1] - '0';
211 else
212 sleep_type = optarg[0] - '0';
204 case 's':
205 if (optarg[0] == 'S')
206 sleep_type = optarg[1] - '0';
207 else
208 sleep_type = optarg[0] - '0';
213 if (sleep_type < 0 || sleep_type > 5)
209 if (sleep_type < 1 || sleep_type > 4)
214 errx(EX_USAGE, "invalid sleep type (%d)",
215 sleep_type);
216 break;
217 case 'h':
218 default:
219 usage(prog);
220 /* NOTREACHED */
221 }
222 }
223 argc -= optind;
224 argv += optind;
225
210 errx(EX_USAGE, "invalid sleep type (%d)",
211 sleep_type);
212 break;
213 case 'h':
214 default:
215 usage(prog);
216 /* NOTREACHED */
217 }
218 }
219 argc -= optind;
220 argv += optind;
221
226 if (sleep_type != -1) {
227 sleep(1); /* wait 1 sec. for key-release event */
222 if (sleep_type != -1)
228 acpi_sleep(sleep_type);
223 acpi_sleep(sleep_type);
229 }
230
231 close(acpifd);
232 exit (0);
233}
224
225 close(acpifd);
226 exit (0);
227}