acpiconf.c revision 170976
165283Siwasaki/*- 265283Siwasaki * Copyright (c) 1999 Mitsuru IWASAKI <iwasaki@FreeBSD.org> 365283Siwasaki * All rights reserved. 465283Siwasaki * 565283Siwasaki * Redistribution and use in source and binary forms, with or without 665283Siwasaki * modification, are permitted provided that the following conditions 765283Siwasaki * are met: 865283Siwasaki * 1. Redistributions of source code must retain the above copyright 965283Siwasaki * notice, this list of conditions and the following disclaimer. 1065283Siwasaki * 2. Redistributions in binary form must reproduce the above copyright 1165283Siwasaki * notice, this list of conditions and the following disclaimer in the 1265283Siwasaki * documentation and/or other materials provided with the distribution. 1365283Siwasaki * 1465283Siwasaki * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1565283Siwasaki * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1665283Siwasaki * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1765283Siwasaki * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1865283Siwasaki * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1965283Siwasaki * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2065283Siwasaki * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2165283Siwasaki * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2265283Siwasaki * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2365283Siwasaki * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2465283Siwasaki * SUCH DAMAGE. 2565283Siwasaki * 2665283Siwasaki * $Id: acpiconf.c,v 1.5 2000/08/08 14:12:19 iwasaki Exp $ 2765283Siwasaki * $FreeBSD: head/usr.sbin/acpi/acpiconf/acpiconf.c 170976 2007-06-21 22:50:37Z njl $ 2865283Siwasaki */ 2965283Siwasaki 3065283Siwasaki#include <sys/param.h> 3165283Siwasaki 3265283Siwasaki#include <err.h> 3365283Siwasaki#include <fcntl.h> 3465283Siwasaki#include <stdio.h> 3566490Smsmith#include <sys/ioctl.h> 3687121Scjc#include <sysexits.h> 3765283Siwasaki#include <unistd.h> 3865283Siwasaki 3968475Siwasaki#include <dev/acpica/acpiio.h> 40114246Snjl#include <contrib/dev/acpica/acpi.h> 4168475Siwasaki 42124001Snjl#define ACPIDEV "/dev/acpi" 4365283Siwasaki 44120036Snjlstatic int acpifd; 45126625Stakawata 46137666Sphilipstatic void 47137638Sphilipacpi_init(void) 48120036Snjl{ 49120036Snjl acpifd = open(ACPIDEV, O_RDWR); 50137763Simp if (acpifd == -1) 51126609Stakawata acpifd = open(ACPIDEV, O_RDONLY); 52137763Simp if (acpifd == -1) 53120036Snjl err(EX_OSFILE, ACPIDEV); 54120036Snjl} 55120036Snjl 56170976Snjl/* Prepare to sleep and then wait for the signal that sleeping can occur. */ 57170976Snjlstatic void 5865283Siwasakiacpi_sleep(int sleep_type) 5965283Siwasaki{ 60124001Snjl int ret; 61170976Snjl 62170976Snjl /* Notify OS that we want to sleep. devd(8) gets this notify. */ 63170976Snjl ret = ioctl(acpifd, ACPIIO_REQSLPSTATE, &sleep_type); 64170976Snjl if (ret != 0) 65170976Snjl err(EX_IOERR, "request sleep type (%d) failed", sleep_type); 66170976Snjl} 67124001Snjl 68170976Snjl/* Ack or abort a pending suspend request. */ 69170976Snjlstatic void 70170976Snjlacpi_sleep_ack(int err_val) 71170976Snjl{ 72170976Snjl int ret; 73124001Snjl 74170976Snjl ret = ioctl(acpifd, ACPIIO_ACKSLPSTATE, &err_val); 75124001Snjl if (ret != 0) 76170976Snjl err(EX_IOERR, "ack sleep type failed"); 7765283Siwasaki} 7865283Siwasaki 79138049Simp/* should be a acpi define, but doesn't appear to be */ 80138049Simp#define UNKNOWN_CAP 0xffffffff 81148491Snjl#define UNKNOWN_VOLTAGE 0xffffffff 82138049Simp 83120036Snjlstatic int 84120036Snjlacpi_battinfo(int num) 85120036Snjl{ 86120036Snjl union acpi_battery_ioctl_arg battio; 87120036Snjl const char *pwr_units; 88148491Snjl int hours, min; 89120036Snjl 90120036Snjl if (num < 0 || num > 64) 91120036Snjl err(EX_USAGE, "invalid battery %d", num); 92120036Snjl 93148491Snjl /* Print battery design information. */ 94120036Snjl battio.unit = num; 95148310Snjl if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio) == -1) 96120036Snjl err(EX_IOERR, "get battery info (%d) failed", num); 97120036Snjl if (battio.bif.units == 0) 98148491Snjl pwr_units = "mW"; 99120036Snjl else 100148491Snjl pwr_units = "mA"; 101120036Snjl 102138049Simp if (battio.bif.dcap == UNKNOWN_CAP) 103148491Snjl printf("Design capacity:\tunknown\n"); 104138049Simp else 105148491Snjl printf("Design capacity:\t%d %sh\n", battio.bif.dcap, 106148491Snjl pwr_units); 107138049Simp if (battio.bif.lfcap == UNKNOWN_CAP) 108148491Snjl printf("Last full capacity:\tunknown\n"); 109138049Simp else 110148491Snjl printf("Last full capacity:\t%d %sh\n", battio.bif.lfcap, 111138049Simp pwr_units); 112120036Snjl printf("Technology:\t\t%s\n", battio.bif.btech == 0 ? 113120036Snjl "primary (non-rechargeable)" : "secondary (rechargeable)"); 114138049Simp if (battio.bif.dvol == UNKNOWN_CAP) 115148491Snjl printf("Design voltage:\t\tunknown\n"); 116138049Simp else 117138049Simp printf("Design voltage:\t\t%d mV\n", battio.bif.dvol); 118148491Snjl printf("Capacity (warn):\t%d %sh\n", battio.bif.wcap, pwr_units); 119148491Snjl printf("Capacity (low):\t\t%d %sh\n", battio.bif.lcap, pwr_units); 120148491Snjl printf("Low/warn granularity:\t%d %sh\n", battio.bif.gra1, pwr_units); 121148491Snjl printf("Warn/full granularity:\t%d %sh\n", battio.bif.gra2, pwr_units); 122120036Snjl printf("Model number:\t\t%s\n", battio.bif.model); 123120036Snjl printf("Serial number:\t\t%s\n", battio.bif.serial); 124120036Snjl printf("Type:\t\t\t%s\n", battio.bif.type); 125120036Snjl printf("OEM info:\t\t%s\n", battio.bif.oeminfo); 126120036Snjl 127148491Snjl /* Print current battery state information. */ 128138044Sphk battio.unit = num; 129148491Snjl if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1) 130148491Snjl err(EX_IOERR, "get battery user info (%d) failed", num); 131148491Snjl if (battio.battinfo.state != ACPI_BATT_STAT_NOT_PRESENT) { 132138049Simp printf("State:\t\t\t"); 133148491Snjl if (battio.battinfo.state == 0) 134148491Snjl printf("high "); 135148491Snjl if (battio.battinfo.state & ACPI_BATT_STAT_CRITICAL) 136148491Snjl printf("critical "); 137148491Snjl if (battio.battinfo.state & ACPI_BATT_STAT_DISCHARG) 138148491Snjl printf("discharging "); 139148491Snjl if (battio.battinfo.state & ACPI_BATT_STAT_CHARGING) 140148491Snjl printf("charging "); 141138049Simp printf("\n"); 142148491Snjl if (battio.battinfo.cap == -1) 143148491Snjl printf("Remaining capacity:\tunknown\n"); 144138049Simp else 145148491Snjl printf("Remaining capacity:\t%d%%\n", 146148491Snjl battio.battinfo.cap); 147148491Snjl if (battio.battinfo.min == -1) 148148493Snjl printf("Remaining time:\t\tunknown\n"); 149148491Snjl else { 150148491Snjl hours = battio.battinfo.min / 60; 151148491Snjl min = battio.battinfo.min % 60; 152148491Snjl printf("Remaining time:\t\t%d:%02d\n", hours, min); 153148491Snjl } 154148491Snjl if (battio.battinfo.rate == -1) 155148491Snjl printf("Present rate:\t\tunknown\n"); 156138049Simp else 157148491Snjl printf("Present rate:\t\t%d %s\n", 158148491Snjl battio.battinfo.rate, pwr_units); 159148491Snjl } else 160148491Snjl printf("State:\t\t\tnot present\n"); 161148491Snjl 162148491Snjl /* Print battery voltage information. */ 163148491Snjl battio.unit = num; 164148491Snjl if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1) 165148491Snjl err(EX_IOERR, "get battery status (%d) failed", num); 166148491Snjl if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT) { 167148491Snjl if (battio.bst.volt == UNKNOWN_VOLTAGE) 168148491Snjl printf("Voltage:\t\tunknown\n"); 169138049Simp else 170148491Snjl printf("Voltage:\t\t%d mV\n", battio.bst.volt); 171138047Simp } 172148491Snjl 173120036Snjl return (0); 174120036Snjl} 175120036Snjl 17668475Siwasakistatic void 17768475Siwasakiusage(const char* prog) 17868475Siwasaki{ 179170976Snjl printf("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog); 18068475Siwasaki exit(0); 18168475Siwasaki} 18268475Siwasaki 18365283Siwasakiint 18465283Siwasakimain(int argc, char *argv[]) 18565283Siwasaki{ 18668475Siwasaki char c, *prog; 18765283Siwasaki int sleep_type; 18865283Siwasaki 18968475Siwasaki prog = argv[0]; 190120036Snjl if (argc < 2) 191120036Snjl usage(prog); 192120036Snjl /* NOTREACHED */ 193120036Snjl 19465283Siwasaki sleep_type = -1; 195120036Snjl acpi_init(); 196170976Snjl while ((c = getopt(argc, argv, "hi:k:s:")) != -1) { 19765283Siwasaki switch (c) { 198120036Snjl case 'i': 199120036Snjl acpi_battinfo(atoi(optarg)); 200120036Snjl break; 201170976Snjl case 'k': 202170976Snjl acpi_sleep_ack(atoi(optarg)); 203170976Snjl break; 20465283Siwasaki case 's': 205118127Snjl if (optarg[0] == 'S') 206118127Snjl sleep_type = optarg[1] - '0'; 207118127Snjl else 208118127Snjl sleep_type = optarg[0] - '0'; 209170976Snjl if (sleep_type < 1 || sleep_type > 4) 21087121Scjc errx(EX_USAGE, "invalid sleep type (%d)", 211120036Snjl sleep_type); 21265283Siwasaki break; 213120036Snjl case 'h': 21465283Siwasaki default: 215120036Snjl usage(prog); 216120036Snjl /* NOTREACHED */ 21765283Siwasaki } 21865283Siwasaki } 219120036Snjl argc -= optind; 220120036Snjl argv += optind; 22165283Siwasaki 222170976Snjl if (sleep_type != -1) 22365283Siwasaki acpi_sleep(sleep_type); 224120036Snjl 225120036Snjl close(acpifd); 226120036Snjl exit (0); 22765283Siwasaki} 228