2000-08-31 14:41:23 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1999 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $Id: acpiconf.c,v 1.5 2000/08/08 14:12:19 iwasaki Exp $
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2000-09-30 20:13:57 +00:00
|
|
|
#include <sys/ioctl.h>
|
2001-11-30 11:35:01 +00:00
|
|
|
#include <sysexits.h>
|
2000-08-31 14:41:23 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2000-11-08 02:37:00 +00:00
|
|
|
#include <dev/acpica/acpiio.h>
|
2003-04-29 18:50:34 +00:00
|
|
|
#include <contrib/dev/acpica/acpi.h>
|
2000-09-30 20:13:57 +00:00
|
|
|
|
2003-12-30 17:28:06 +00:00
|
|
|
#define ACPIDEV "/dev/acpi"
|
2000-08-31 14:41:23 +00:00
|
|
|
|
2003-09-13 20:13:01 +00:00
|
|
|
static int acpifd;
|
2004-03-05 02:48:22 +00:00
|
|
|
|
2004-11-13 15:16:35 +00:00
|
|
|
static void
|
2004-11-13 01:54:22 +00:00
|
|
|
acpi_init(void)
|
2000-08-31 14:41:23 +00:00
|
|
|
{
|
2003-09-13 20:13:01 +00:00
|
|
|
acpifd = open(ACPIDEV, O_RDWR);
|
2004-11-16 06:25:02 +00:00
|
|
|
if (acpifd == -1)
|
2004-03-04 17:03:49 +00:00
|
|
|
acpifd = open(ACPIDEV, O_RDONLY);
|
2004-11-16 06:25:02 +00:00
|
|
|
if (acpifd == -1)
|
2001-11-30 11:35:01 +00:00
|
|
|
err(EX_OSFILE, ACPIDEV);
|
2003-09-13 20:13:01 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 22:50:37 +00:00
|
|
|
/* Prepare to sleep and then wait for the signal that sleeping can occur. */
|
|
|
|
static void
|
2000-08-31 14:41:23 +00:00
|
|
|
acpi_sleep(int sleep_type)
|
|
|
|
{
|
2003-12-30 17:28:06 +00:00
|
|
|
int ret;
|
2007-06-21 22:50:37 +00:00
|
|
|
|
|
|
|
/* Notify OS that we want to sleep. devd(8) gets this notify. */
|
|
|
|
ret = ioctl(acpifd, ACPIIO_REQSLPSTATE, &sleep_type);
|
|
|
|
if (ret != 0)
|
|
|
|
err(EX_IOERR, "request sleep type (%d) failed", sleep_type);
|
|
|
|
}
|
2003-12-30 17:28:06 +00:00
|
|
|
|
2007-06-21 22:50:37 +00:00
|
|
|
/* Ack or abort a pending suspend request. */
|
|
|
|
static void
|
|
|
|
acpi_sleep_ack(int err_val)
|
|
|
|
{
|
|
|
|
int ret;
|
2003-12-30 17:28:06 +00:00
|
|
|
|
2007-06-21 22:50:37 +00:00
|
|
|
ret = ioctl(acpifd, ACPIIO_ACKSLPSTATE, &err_val);
|
2003-12-30 17:28:06 +00:00
|
|
|
if (ret != 0)
|
2007-06-21 22:50:37 +00:00
|
|
|
err(EX_IOERR, "ack sleep type failed");
|
2003-09-13 20:13:01 +00:00
|
|
|
}
|
|
|
|
|
2004-11-24 16:02:20 +00:00
|
|
|
/* should be a acpi define, but doesn't appear to be */
|
|
|
|
#define UNKNOWN_CAP 0xffffffff
|
2005-07-28 19:31:28 +00:00
|
|
|
#define UNKNOWN_VOLTAGE 0xffffffff
|
2004-11-24 16:02:20 +00:00
|
|
|
|
2003-09-13 20:13:01 +00:00
|
|
|
static int
|
|
|
|
acpi_battinfo(int num)
|
|
|
|
{
|
|
|
|
union acpi_battery_ioctl_arg battio;
|
|
|
|
const char *pwr_units;
|
2005-07-28 19:31:28 +00:00
|
|
|
int hours, min;
|
2003-09-13 20:13:01 +00:00
|
|
|
|
|
|
|
if (num < 0 || num > 64)
|
|
|
|
err(EX_USAGE, "invalid battery %d", num);
|
|
|
|
|
2005-07-28 19:31:28 +00:00
|
|
|
/* Print battery design information. */
|
2003-09-13 20:13:01 +00:00
|
|
|
battio.unit = num;
|
2005-07-22 19:19:00 +00:00
|
|
|
if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio) == -1)
|
2003-09-13 20:13:01 +00:00
|
|
|
err(EX_IOERR, "get battery info (%d) failed", num);
|
|
|
|
if (battio.bif.units == 0)
|
2005-07-28 19:31:28 +00:00
|
|
|
pwr_units = "mW";
|
2003-09-13 20:13:01 +00:00
|
|
|
else
|
2005-07-28 19:31:28 +00:00
|
|
|
pwr_units = "mA";
|
2003-09-13 20:13:01 +00:00
|
|
|
|
2004-11-24 16:02:20 +00:00
|
|
|
if (battio.bif.dcap == UNKNOWN_CAP)
|
2005-07-28 19:31:28 +00:00
|
|
|
printf("Design capacity:\tunknown\n");
|
2004-11-24 16:02:20 +00:00
|
|
|
else
|
2005-07-28 19:31:28 +00:00
|
|
|
printf("Design capacity:\t%d %sh\n", battio.bif.dcap,
|
|
|
|
pwr_units);
|
2004-11-24 16:02:20 +00:00
|
|
|
if (battio.bif.lfcap == UNKNOWN_CAP)
|
2005-07-28 19:31:28 +00:00
|
|
|
printf("Last full capacity:\tunknown\n");
|
2004-11-24 16:02:20 +00:00
|
|
|
else
|
2005-07-28 19:31:28 +00:00
|
|
|
printf("Last full capacity:\t%d %sh\n", battio.bif.lfcap,
|
2004-11-24 16:02:20 +00:00
|
|
|
pwr_units);
|
2003-09-13 20:13:01 +00:00
|
|
|
printf("Technology:\t\t%s\n", battio.bif.btech == 0 ?
|
|
|
|
"primary (non-rechargeable)" : "secondary (rechargeable)");
|
2004-11-24 16:02:20 +00:00
|
|
|
if (battio.bif.dvol == UNKNOWN_CAP)
|
2005-07-28 19:31:28 +00:00
|
|
|
printf("Design voltage:\t\tunknown\n");
|
2004-11-24 16:02:20 +00:00
|
|
|
else
|
|
|
|
printf("Design voltage:\t\t%d mV\n", battio.bif.dvol);
|
2005-07-28 19:31:28 +00:00
|
|
|
printf("Capacity (warn):\t%d %sh\n", battio.bif.wcap, pwr_units);
|
|
|
|
printf("Capacity (low):\t\t%d %sh\n", battio.bif.lcap, pwr_units);
|
|
|
|
printf("Low/warn granularity:\t%d %sh\n", battio.bif.gra1, pwr_units);
|
|
|
|
printf("Warn/full granularity:\t%d %sh\n", battio.bif.gra2, pwr_units);
|
2003-09-13 20:13:01 +00:00
|
|
|
printf("Model number:\t\t%s\n", battio.bif.model);
|
|
|
|
printf("Serial number:\t\t%s\n", battio.bif.serial);
|
|
|
|
printf("Type:\t\t\t%s\n", battio.bif.type);
|
|
|
|
printf("OEM info:\t\t%s\n", battio.bif.oeminfo);
|
2000-08-31 14:41:23 +00:00
|
|
|
|
2005-07-28 19:31:28 +00:00
|
|
|
/* Print current battery state information. */
|
2004-11-24 12:49:39 +00:00
|
|
|
battio.unit = num;
|
2005-07-28 19:31:28 +00:00
|
|
|
if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1)
|
|
|
|
err(EX_IOERR, "get battery user info (%d) failed", num);
|
|
|
|
if (battio.battinfo.state != ACPI_BATT_STAT_NOT_PRESENT) {
|
2004-11-24 16:02:20 +00:00
|
|
|
printf("State:\t\t\t");
|
2005-07-28 19:31:28 +00:00
|
|
|
if (battio.battinfo.state == 0)
|
|
|
|
printf("high ");
|
|
|
|
if (battio.battinfo.state & ACPI_BATT_STAT_CRITICAL)
|
|
|
|
printf("critical ");
|
|
|
|
if (battio.battinfo.state & ACPI_BATT_STAT_DISCHARG)
|
|
|
|
printf("discharging ");
|
|
|
|
if (battio.battinfo.state & ACPI_BATT_STAT_CHARGING)
|
|
|
|
printf("charging ");
|
2004-11-24 16:02:20 +00:00
|
|
|
printf("\n");
|
2005-07-28 19:31:28 +00:00
|
|
|
if (battio.battinfo.cap == -1)
|
|
|
|
printf("Remaining capacity:\tunknown\n");
|
2004-11-24 16:02:20 +00:00
|
|
|
else
|
2005-07-28 19:31:28 +00:00
|
|
|
printf("Remaining capacity:\t%d%%\n",
|
|
|
|
battio.battinfo.cap);
|
|
|
|
if (battio.battinfo.min == -1)
|
2005-07-28 19:41:52 +00:00
|
|
|
printf("Remaining time:\t\tunknown\n");
|
2005-07-28 19:31:28 +00:00
|
|
|
else {
|
|
|
|
hours = battio.battinfo.min / 60;
|
|
|
|
min = battio.battinfo.min % 60;
|
|
|
|
printf("Remaining time:\t\t%d:%02d\n", hours, min);
|
|
|
|
}
|
|
|
|
if (battio.battinfo.rate == -1)
|
|
|
|
printf("Present rate:\t\tunknown\n");
|
2004-11-24 16:02:20 +00:00
|
|
|
else
|
2005-07-28 19:31:28 +00:00
|
|
|
printf("Present rate:\t\t%d %s\n",
|
|
|
|
battio.battinfo.rate, pwr_units);
|
|
|
|
} else
|
|
|
|
printf("State:\t\t\tnot present\n");
|
|
|
|
|
|
|
|
/* Print battery voltage information. */
|
|
|
|
battio.unit = num;
|
|
|
|
if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1)
|
|
|
|
err(EX_IOERR, "get battery status (%d) failed", num);
|
|
|
|
if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT) {
|
|
|
|
if (battio.bst.volt == UNKNOWN_VOLTAGE)
|
|
|
|
printf("Voltage:\t\tunknown\n");
|
2004-11-24 16:02:20 +00:00
|
|
|
else
|
2005-07-28 19:31:28 +00:00
|
|
|
printf("Voltage:\t\t%d mV\n", battio.bst.volt);
|
2004-11-24 15:32:07 +00:00
|
|
|
}
|
2005-07-28 19:31:28 +00:00
|
|
|
|
2000-08-31 14:41:23 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2000-11-08 02:37:00 +00:00
|
|
|
static void
|
|
|
|
usage(const char* prog)
|
|
|
|
{
|
2007-06-21 22:50:37 +00:00
|
|
|
printf("usage: %s [-h] [-i batt] [-k ack] [-s 1-4]\n", prog);
|
2000-11-08 02:37:00 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2000-08-31 14:41:23 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2000-11-08 02:37:00 +00:00
|
|
|
char c, *prog;
|
2000-08-31 14:41:23 +00:00
|
|
|
int sleep_type;
|
|
|
|
|
2000-11-08 02:37:00 +00:00
|
|
|
prog = argv[0];
|
2003-09-13 20:13:01 +00:00
|
|
|
if (argc < 2)
|
|
|
|
usage(prog);
|
|
|
|
/* NOTREACHED */
|
|
|
|
|
2000-08-31 14:41:23 +00:00
|
|
|
sleep_type = -1;
|
2003-09-13 20:13:01 +00:00
|
|
|
acpi_init();
|
2007-06-21 22:50:37 +00:00
|
|
|
while ((c = getopt(argc, argv, "hi:k:s:")) != -1) {
|
2000-08-31 14:41:23 +00:00
|
|
|
switch (c) {
|
2003-09-13 20:13:01 +00:00
|
|
|
case 'i':
|
|
|
|
acpi_battinfo(atoi(optarg));
|
|
|
|
break;
|
2007-06-21 22:50:37 +00:00
|
|
|
case 'k':
|
|
|
|
acpi_sleep_ack(atoi(optarg));
|
|
|
|
break;
|
2000-08-31 14:41:23 +00:00
|
|
|
case 's':
|
2003-07-28 16:22:45 +00:00
|
|
|
if (optarg[0] == 'S')
|
|
|
|
sleep_type = optarg[1] - '0';
|
|
|
|
else
|
|
|
|
sleep_type = optarg[0] - '0';
|
2007-06-21 22:50:37 +00:00
|
|
|
if (sleep_type < 1 || sleep_type > 4)
|
2001-11-30 11:35:01 +00:00
|
|
|
errx(EX_USAGE, "invalid sleep type (%d)",
|
2003-09-13 20:13:01 +00:00
|
|
|
sleep_type);
|
2000-08-31 14:41:23 +00:00
|
|
|
break;
|
2003-09-13 20:13:01 +00:00
|
|
|
case 'h':
|
2000-08-31 14:41:23 +00:00
|
|
|
default:
|
2003-09-13 20:13:01 +00:00
|
|
|
usage(prog);
|
|
|
|
/* NOTREACHED */
|
2000-08-31 14:41:23 +00:00
|
|
|
}
|
|
|
|
}
|
2003-09-13 20:13:01 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2000-08-31 14:41:23 +00:00
|
|
|
|
2007-06-21 22:50:37 +00:00
|
|
|
if (sleep_type != -1)
|
2000-08-31 14:41:23 +00:00
|
|
|
acpi_sleep(sleep_type);
|
2003-09-13 20:13:01 +00:00
|
|
|
|
|
|
|
close(acpifd);
|
|
|
|
exit (0);
|
2000-08-31 14:41:23 +00:00
|
|
|
}
|