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