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/include/acpi.h> 42 43 #define ACPIDEV "/dev/acpi" 44 45 static int acpifd; 46 47 static void 48 acpi_init(void) 49 { 50 acpifd = open(ACPIDEV, O_RDWR); 51 if (acpifd == -1) 52 acpifd = open(ACPIDEV, O_RDONLY); 53 if (acpifd == -1) 54 err(EX_OSFILE, ACPIDEV); 55 } 56 57 /* Prepare to sleep and then wait for the signal that sleeping can occur. */ 58 static void 59 acpi_sleep(int sleep_type) 60 { 61 int ret; 62 63 /* Notify OS that we want to sleep. devd(8) gets this notify. */ 64 ret = ioctl(acpifd, ACPIIO_REQSLPSTATE, &sleep_type); 65 if (ret != 0) 66 err(EX_IOERR, "request sleep type (%d) failed", sleep_type); 67 } 68 69 /* Ack or abort a pending suspend request. */ 70 static void 71 acpi_sleep_ack(int err_val) 72 { 73 int ret; 74 75 ret = ioctl(acpifd, ACPIIO_ACKSLPSTATE, &err_val); 76 if (ret != 0) 77 err(EX_IOERR, "ack sleep type failed"); 78 } 79 80 /* should be a acpi define, but doesn't appear to be */ 81 #define UNKNOWN_CAP 0xffffffff 82 #define UNKNOWN_VOLTAGE 0xffffffff 83 84 static int 85 acpi_battinfo(int num) 86 { 87 union acpi_battery_ioctl_arg battio; 88 const char *pwr_units; 89 int hours, min; 90 91 if (num < 0 || num > 64) 92 err(EX_USAGE, "invalid battery %d", num); 93 94 /* Print battery design information. */ 95 battio.unit = num; 96 if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio) == -1) 97 err(EX_IOERR, "get battery info (%d) failed", num); 98 if (battio.bif.units == 0) 99 pwr_units = "mW"; 100 else 101 pwr_units = "mA"; 102 103 if (battio.bif.dcap == UNKNOWN_CAP) 104 printf("Design capacity:\tunknown\n"); 105 else 106 printf("Design capacity:\t%d %sh\n", battio.bif.dcap, 107 pwr_units); 108 if (battio.bif.lfcap == UNKNOWN_CAP) 109 printf("Last full capacity:\tunknown\n"); 110 else 111 printf("Last full capacity:\t%d %sh\n", battio.bif.lfcap, 112 pwr_units); 113 printf("Technology:\t\t%s\n", battio.bif.btech == 0 ? 114 "primary (non-rechargeable)" : "secondary (rechargeable)"); 115 if (battio.bif.dvol == UNKNOWN_CAP) 116 printf("Design voltage:\t\tunknown\n"); 117 else 118 printf("Design voltage:\t\t%d mV\n", battio.bif.dvol); 119 printf("Capacity (warn):\t%d %sh\n", battio.bif.wcap, pwr_units); 120 printf("Capacity (low):\t\t%d %sh\n", battio.bif.lcap, pwr_units); 121 printf("Low/warn granularity:\t%d %sh\n", battio.bif.gra1, pwr_units); 122 printf("Warn/full granularity:\t%d %sh\n", battio.bif.gra2, pwr_units); 123 printf("Model number:\t\t%s\n", battio.bif.model); 124 printf("Serial number:\t\t%s\n", battio.bif.serial); 125 printf("Type:\t\t\t%s\n", battio.bif.type); 126 printf("OEM info:\t\t%s\n", battio.bif.oeminfo); 127 128 /* Print current battery state information. */ 129 battio.unit = num; 130 if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1) 131 err(EX_IOERR, "get battery user info (%d) failed", num); 132 if (battio.battinfo.state != ACPI_BATT_STAT_NOT_PRESENT) { 133 printf("State:\t\t\t"); 134 if (battio.battinfo.state == 0) 135 printf("high "); 136 if (battio.battinfo.state & ACPI_BATT_STAT_CRITICAL) 137 printf("critical "); 138 if (battio.battinfo.state & ACPI_BATT_STAT_DISCHARG) 139 printf("discharging "); 140 if (battio.battinfo.state & ACPI_BATT_STAT_CHARGING) 141 printf("charging "); 142 printf("\n"); 143 if (battio.battinfo.cap == -1) 144 printf("Remaining capacity:\tunknown\n"); 145 else 146 printf("Remaining capacity:\t%d%%\n", 147 battio.battinfo.cap); 148 if (battio.battinfo.min == -1) 149 printf("Remaining time:\t\tunknown\n"); 150 else { 151 hours = battio.battinfo.min / 60; 152 min = battio.battinfo.min % 60; 153 printf("Remaining time:\t\t%d:%02d\n", hours, min); 154 } 155 if (battio.battinfo.rate == -1) 156 printf("Present rate:\t\tunknown\n"); 157 else 158 printf("Present rate:\t\t%d %s\n", 159 battio.battinfo.rate, pwr_units); 160 } else 161 printf("State:\t\t\tnot present\n"); 162 163 /* Print battery voltage information. */ 164 battio.unit = num; 165 if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1) 166 err(EX_IOERR, "get battery status (%d) failed", num); 167 if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT) { 168 if (battio.bst.volt == UNKNOWN_VOLTAGE) 169 printf("Voltage:\t\tunknown\n"); 170 else 171 printf("Voltage:\t\t%d mV\n", battio.bst.volt); 172 } 173 174 return (0); 175 } 176 177 static void 178 usage(const char* prog) 179 { 180 printf("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog); 181 exit(0); 182 } 183 184 int 185 main(int argc, char *argv[]) 186 { 187 char c, *prog; 188 int sleep_type; 189 190 prog = argv[0]; 191 if (argc < 2) 192 usage(prog); 193 /* NOTREACHED */ 194 195 sleep_type = -1; 196 acpi_init(); 197 while ((c = getopt(argc, argv, "hi:k:s:")) != -1) { 198 switch (c) { 199 case 'i': 200 acpi_battinfo(atoi(optarg)); 201 break; 202 case 'k': 203 acpi_sleep_ack(atoi(optarg)); 204 break; 205 case 's': 206 if (optarg[0] == 'S') 207 sleep_type = optarg[1] - '0'; 208 else 209 sleep_type = optarg[0] - '0'; 210 if (sleep_type < 1 || sleep_type > 4) 211 errx(EX_USAGE, "invalid sleep type (%d)", 212 sleep_type); 213 break; 214 case 'h': 215 default: 216 usage(prog); 217 /* NOTREACHED */ 218 } 219 } 220 argc -= optind; 221 argv += optind; 222 223 if (sleep_type != -1) 224 acpi_sleep(sleep_type); 225 226 close(acpifd); 227 exit (0); 228 } 229