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