acpiconf.c revision 138044
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 138044 2004-11-24 12:49:39Z phk $ 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" 43124001Snjl#define RC_SUSPEND_PATH "/etc/rc.suspend" 44124001Snjl#define RC_RESUME_PATH "/etc/rc.resume" 4565283Siwasaki 46120036Snjlstatic int acpifd; 47126625Stakawata 48137666Sphilipstatic void 49137638Sphilipacpi_init(void) 50120036Snjl{ 51120036Snjl acpifd = open(ACPIDEV, O_RDWR); 52137763Simp if (acpifd == -1) 53126609Stakawata acpifd = open(ACPIDEV, O_RDONLY); 54137763Simp if (acpifd == -1) 55120036Snjl err(EX_OSFILE, ACPIDEV); 56120036Snjl} 57120036Snjl 58120036Snjlstatic int 5965283Siwasakiacpi_sleep(int sleep_type) 6065283Siwasaki{ 61124001Snjl char cmd[64]; 62124001Snjl int ret; 63124001Snjl 64124001Snjl /* Run the suspend rc script, if available. */ 65124001Snjl if (access(RC_SUSPEND_PATH, X_OK) == 0) { 66124001Snjl snprintf(cmd, sizeof(cmd), "%s acpi %d", RC_SUSPEND_PATH, 67124001Snjl sleep_type); 68124001Snjl system(cmd); 69124001Snjl } 70124001Snjl 71124001Snjl ret = ioctl(acpifd, ACPIIO_SETSLPSTATE, &sleep_type); 72124001Snjl 73124001Snjl /* Run the resume rc script, if available. */ 74124001Snjl if (access(RC_RESUME_PATH, X_OK) == 0) { 75124001Snjl snprintf(cmd, sizeof(cmd), "%s acpi %d", RC_RESUME_PATH, 76124001Snjl sleep_type); 77124001Snjl system(cmd); 78124001Snjl } 79124001Snjl 80124001Snjl if (ret != 0) 8187121Scjc err(EX_IOERR, "sleep type (%d) failed", sleep_type); 8265283Siwasaki 8365283Siwasaki return (0); 8465283Siwasaki} 8565283Siwasaki 86120036Snjlstatic int 87120036Snjlacpi_battinfo(int num) 88120036Snjl{ 89120036Snjl union acpi_battery_ioctl_arg battio; 90120036Snjl const char *pwr_units; 91120036Snjl 92120036Snjl if (num < 0 || num > 64) 93120036Snjl err(EX_USAGE, "invalid battery %d", num); 94120036Snjl 95120036Snjl battio.unit = num; 96120036Snjl if (ioctl(acpifd, ACPIIO_CMBAT_GET_BIF, &battio) == -1) 97120036Snjl err(EX_IOERR, "get battery info (%d) failed", num); 98120036Snjl printf("Battery %d information\n", num); 99120036Snjl if (battio.bif.units == 0) 100120036Snjl pwr_units = "mWh"; 101120036Snjl else 102120036Snjl pwr_units = "mAh"; 103120036Snjl 104120036Snjl printf("Design capacity:\t%d %s\n", battio.bif.dcap, pwr_units); 105120036Snjl printf("Last full capacity:\t%d %s\n", battio.bif.lfcap, pwr_units); 106120036Snjl printf("Technology:\t\t%s\n", battio.bif.btech == 0 ? 107120036Snjl "primary (non-rechargeable)" : "secondary (rechargeable)"); 108120036Snjl printf("Design voltage:\t\t%d mV\n", battio.bif.dvol); 109120036Snjl printf("Capacity (warn):\t%d %s\n", battio.bif.wcap, pwr_units); 110120036Snjl printf("Capacity (low):\t\t%d %s\n", battio.bif.lcap, pwr_units); 111120036Snjl printf("Low/warn granularity:\t%d %s\n", battio.bif.gra1, pwr_units); 112120036Snjl printf("Warn/full granularity:\t%d %s\n", battio.bif.gra2, pwr_units); 113120036Snjl printf("Model number:\t\t%s\n", battio.bif.model); 114120036Snjl printf("Serial number:\t\t%s\n", battio.bif.serial); 115120036Snjl printf("Type:\t\t\t%s\n", battio.bif.type); 116120036Snjl printf("OEM info:\t\t%s\n", battio.bif.oeminfo); 117120036Snjl 118138044Sphk battio.unit = num; 119138044Sphk if (ioctl(acpifd, ACPIIO_CMBAT_GET_BST, &battio) == -1) 120138044Sphk err(EX_IOERR, "get battery info (%d) failed", num); 121138044Sphk printf("State:\t\t\t%d\n", battio.bst.state); 122138044Sphk printf("Present rate:\t\t%d\n", battio.bst.rate); 123138044Sphk printf("Remaining capacity:\t%d mWh\n", battio.bst.cap); 124138044Sphk printf("Volt:\t\t\t%.3f V\n", battio.bst.volt * .001); 125138044Sphk 126120036Snjl return (0); 127120036Snjl} 128120036Snjl 12968475Siwasakistatic void 13068475Siwasakiusage(const char* prog) 13168475Siwasaki{ 132133933Snjl printf("usage: %s [-h] [-i batt] [-s 1-5]\n", prog); 13368475Siwasaki exit(0); 13468475Siwasaki} 13568475Siwasaki 13665283Siwasakiint 13765283Siwasakimain(int argc, char *argv[]) 13865283Siwasaki{ 13968475Siwasaki char c, *prog; 14065283Siwasaki int sleep_type; 14165283Siwasaki 14268475Siwasaki prog = argv[0]; 143120036Snjl if (argc < 2) 144120036Snjl usage(prog); 145120036Snjl /* NOTREACHED */ 146120036Snjl 14765283Siwasaki sleep_type = -1; 148120036Snjl acpi_init(); 149133933Snjl while ((c = getopt(argc, argv, "hi:s:")) != -1) { 15065283Siwasaki switch (c) { 151120036Snjl case 'i': 152120036Snjl acpi_battinfo(atoi(optarg)); 153120036Snjl break; 15465283Siwasaki case 's': 155118127Snjl if (optarg[0] == 'S') 156118127Snjl sleep_type = optarg[1] - '0'; 157118127Snjl else 158118127Snjl sleep_type = optarg[0] - '0'; 15987121Scjc if (sleep_type < 0 || sleep_type > 5) 16087121Scjc errx(EX_USAGE, "invalid sleep type (%d)", 161120036Snjl sleep_type); 16265283Siwasaki break; 163120036Snjl case 'h': 16465283Siwasaki default: 165120036Snjl usage(prog); 166120036Snjl /* NOTREACHED */ 16765283Siwasaki } 16865283Siwasaki } 169120036Snjl argc -= optind; 170120036Snjl argv += optind; 17165283Siwasaki 17265283Siwasaki if (sleep_type != -1) { 17365283Siwasaki sleep(1); /* wait 1 sec. for key-release event */ 17465283Siwasaki acpi_sleep(sleep_type); 17565283Siwasaki } 176120036Snjl 177120036Snjl close(acpifd); 178120036Snjl exit (0); 17965283Siwasaki} 180