Merge from vendor branch LIBPCAP:
[dragonfly.git] / usr.sbin / acpi / acpiconf / acpiconf.c
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: src/usr.sbin/acpi/acpiconf/acpiconf.c,v 1.14 2004/03/05 02:48:22 takawata Exp $
28  *      $DragonFly: src/usr.sbin/acpi/acpiconf/acpiconf.c,v 1.2 2005/12/05 01:04:00 swildner Exp $
29  */
30
31 #include <sys/param.h>
32
33 #include <err.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <sys/ioctl.h>
37 #include <sysexits.h>
38 #include <unistd.h>
39
40 #include "acpiio.h"
41 #include "acpi.h"
42
43 #define ACPIDEV         "/dev/acpi"
44 #define RC_SUSPEND_PATH "/etc/rc.suspend"
45 #define RC_RESUME_PATH  "/etc/rc.resume"
46
47 static int      acpifd;
48
49 static int
50 acpi_init(void)
51 {
52         acpifd = open(ACPIDEV, O_RDWR);
53         if (acpifd == -1){
54                 acpifd = open(ACPIDEV, O_RDONLY);
55         }
56         if (acpifd == -1){
57                 err(EX_OSFILE, ACPIDEV);
58         }
59 }
60
61 static int
62 acpi_enable_disable(int enable)
63 {
64         if (ioctl(acpifd, enable, NULL) == -1) {
65                 if (enable == ACPIIO_ENABLE)
66                         err(EX_IOERR, "enable failed");
67                 else
68                         err(EX_IOERR, "disable failed");
69         }
70
71         return (0);
72 }
73
74 static int
75 acpi_sleep(int sleep_type)
76 {
77         char cmd[64];
78         int ret;
79
80         /* Run the suspend rc script, if available. */
81         if (access(RC_SUSPEND_PATH, X_OK) == 0) {
82                 snprintf(cmd, sizeof(cmd), "%s acpi %d", RC_SUSPEND_PATH,
83                     sleep_type);
84                 system(cmd);
85         }
86
87         ret = ioctl(acpifd, ACPIIO_SETSLPSTATE, &sleep_type);
88
89         /* Run the resume rc script, if available. */
90         if (access(RC_RESUME_PATH, X_OK) == 0) {
91                 snprintf(cmd, sizeof(cmd), "%s acpi %d", RC_RESUME_PATH,
92                     sleep_type);
93                 system(cmd);
94         }
95
96         if (ret != 0)
97                 err(EX_IOERR, "sleep type (%d) failed", sleep_type);
98
99         return (0);
100 }
101
102 static int
103 acpi_battinfo(int num)
104 {
105         union acpi_battery_ioctl_arg battio;
106         const char *pwr_units;
107
108         if (num < 0 || num > 64)
109                 err(EX_USAGE, "invalid battery %d", num);
110
111         battio.unit = num;
112         if (ioctl(acpifd, ACPIIO_CMBAT_GET_BIF, &battio) == -1)
113                 err(EX_IOERR, "get battery info (%d) failed", num);
114         printf("Battery %d information\n", num);
115         if (battio.bif.units == 0)
116                 pwr_units = "mWh";
117         else
118                 pwr_units = "mAh";
119
120         printf("Design capacity:\t%d %s\n", battio.bif.dcap, pwr_units);
121         printf("Last full capacity:\t%d %s\n", battio.bif.lfcap, pwr_units);
122         printf("Technology:\t\t%s\n", battio.bif.btech == 0 ?
123             "primary (non-rechargeable)" : "secondary (rechargeable)");
124         printf("Design voltage:\t\t%d mV\n", battio.bif.dvol);
125         printf("Capacity (warn):\t%d %s\n", battio.bif.wcap, pwr_units);
126         printf("Capacity (low):\t\t%d %s\n", battio.bif.lcap, pwr_units);
127         printf("Low/warn granularity:\t%d %s\n", battio.bif.gra1, pwr_units);
128         printf("Warn/full granularity:\t%d %s\n", battio.bif.gra2, pwr_units);
129         printf("Model number:\t\t%s\n", battio.bif.model);
130         printf("Serial number:\t\t%s\n", battio.bif.serial);
131         printf("Type:\t\t\t%s\n", battio.bif.type);
132         printf("OEM info:\t\t%s\n", battio.bif.oeminfo);
133
134         return (0);
135 }
136
137 static void
138 usage(const char* prog)
139 {
140         printf("usage: %s [-deh] [-i batt] [-s 1-5]\n", prog);
141         exit(0);
142 }
143
144 int
145 main(int argc, char *argv[])
146 {
147         char    c, *prog;
148         int     sleep_type;
149
150         prog = argv[0];
151         if (argc < 2)
152                 usage(prog);
153                 /* NOTREACHED */
154
155         sleep_type = -1;
156         acpi_init();
157         while ((c = getopt(argc, argv, "dehi:s:")) != -1) {
158                 switch (c) {
159                 case 'i':
160                         acpi_battinfo(atoi(optarg));
161                         break;
162                 case 'd':
163                         acpi_enable_disable(ACPIIO_DISABLE);
164                         break;
165                 case 'e':
166                         acpi_enable_disable(ACPIIO_ENABLE);
167                         break;
168                 case 's':
169                         if (optarg[0] == 'S')
170                                 sleep_type = optarg[1] - '0';
171                         else
172                                 sleep_type = optarg[0] - '0';
173                         if (sleep_type < 0 || sleep_type > 5)
174                                 errx(EX_USAGE, "invalid sleep type (%d)",
175                                      sleep_type);
176                         break;
177                 case 'h':
178                 default:
179                         usage(prog);
180                         /* NOTREACHED */
181                 }
182         }
183         argc -= optind;
184         argv += optind;
185
186         if (sleep_type != -1) {
187                 sleep(1);       /* wait 1 sec. for key-release event */
188                 acpi_sleep(sleep_type);
189         }
190
191         close(acpifd);
192         exit (0);
193 }