From 31d2c70e5eccca6481c953a49858a632a10d7ee0 Mon Sep 17 00:00:00 2001 From: njl Date: Sat, 13 Sep 2003 20:13:01 +0000 Subject: [PATCH] Add the -i flag to acpiconf(8) to retrieve battery information. Rename a few structure elements. --- sys/dev/acpica/acpi_cmbat.c | 4 +- sys/dev/acpica/acpiio.h | 4 +- usr.sbin/acpi/acpiconf/acpiconf.8 | 3 + usr.sbin/acpi/acpiconf/acpiconf.c | 95 +++++++++++++++++++++---------- 4 files changed, 73 insertions(+), 33 deletions(-) diff --git a/sys/dev/acpica/acpi_cmbat.c b/sys/dev/acpica/acpi_cmbat.c index 9ca5989038fe..a5a9a092a53d 100644 --- a/sys/dev/acpica/acpi_cmbat.c +++ b/sys/dev/acpica/acpi_cmbat.c @@ -264,7 +264,7 @@ acpi_cmbat_get_bif(void *context) goto end; } - PKG_GETINT(res, tmp, 0, sc->bif.unit, end); + PKG_GETINT(res, tmp, 0, sc->bif.units, end); PKG_GETINT(res, tmp, 1, sc->bif.dcap, end); PKG_GETINT(res, tmp, 2, sc->bif.lfcap, end); PKG_GETINT(res, tmp, 3, sc->bif.btech, end); @@ -404,7 +404,7 @@ acpi_cmbat_ioctl(u_long cmd, caddr_t addr, void *arg) case ACPIIO_CMBAT_GET_BIF: acpi_cmbat_get_bif(dev); bifp = &ioctl_arg->bif; - bifp->unit = sc->bif.unit; + bifp->units = sc->bif.units; bifp->dcap = sc->bif.dcap; bifp->lfcap = sc->bif.lfcap; bifp->btech = sc->bif.btech; diff --git a/sys/dev/acpica/acpiio.h b/sys/dev/acpica/acpiio.h index a34002d6708c..5642492ca9f5 100644 --- a/sys/dev/acpica/acpiio.h +++ b/sys/dev/acpica/acpiio.h @@ -50,10 +50,10 @@ struct acpi_battinfo { #define ACPI_CMBAT_MAXSTRLEN 32 struct acpi_bif { - u_int32_t unit; /* 0 for mWh, 1 for mAh */ + u_int32_t units; /* 0 for mWh, 1 for mAh */ u_int32_t dcap; /* Design Capacity */ u_int32_t lfcap; /* Last Full capacity */ - u_int32_t btech; /* Battery Technorogy */ + u_int32_t btech; /* Battery Technology */ u_int32_t dvol; /* Design voltage (mV) */ u_int32_t wcap; /* WARN capacity */ u_int32_t lcap; /* Low capacity */ diff --git a/usr.sbin/acpi/acpiconf/acpiconf.8 b/usr.sbin/acpi/acpiconf/acpiconf.8 index 373e6d45fd33..47f90c827370 100644 --- a/usr.sbin/acpi/acpiconf/acpiconf.8 +++ b/usr.sbin/acpi/acpiconf/acpiconf.8 @@ -36,6 +36,7 @@ .Sh SYNOPSIS .Nm .Op Fl deh +.Op Fl i Ar batt .Op Fl s Ar type .Sh DESCRIPTION The @@ -50,6 +51,8 @@ Disables ACPI power management. Enables ACPI power management. .It Fl h Displays a summary of available options. +.It Fl i Ar batt +Get design information about the specified battery. .It Fl s Ar type Enters the specified sleep mode. Recognized types are diff --git a/usr.sbin/acpi/acpiconf/acpiconf.c b/usr.sbin/acpi/acpiconf/acpiconf.c index 86cc655defab..9be6ad8e0bef 100644 --- a/usr.sbin/acpi/acpiconf/acpiconf.c +++ b/usr.sbin/acpi/acpiconf/acpiconf.c @@ -42,22 +42,25 @@ #define ACPIDEV "/dev/acpi" +static int acpifd; + +static int +acpi_init() +{ + acpifd = open(ACPIDEV, O_RDWR); + if (acpifd == -1) + err(EX_OSFILE, ACPIDEV); +} + static int acpi_enable_disable(int enable) { - int fd; - - fd = open(ACPIDEV, O_RDWR); - if (fd == -1) { - err(EX_OSFILE, ACPIDEV); - } - if (ioctl(fd, enable, NULL) == -1) { + if (ioctl(acpifd, enable, NULL) == -1) { if (enable == ACPIIO_ENABLE) err(EX_IOERR, "enable failed"); else err(EX_IOERR, "disable failed"); } - close(fd); return (0); } @@ -65,16 +68,43 @@ acpi_enable_disable(int enable) static int acpi_sleep(int sleep_type) { - int fd; - - fd = open(ACPIDEV, O_RDWR); - if (fd == -1) { - err(EX_OSFILE, ACPIDEV); - } - if (ioctl(fd, ACPIIO_SETSLPSTATE, &sleep_type) == -1) { + if (ioctl(acpifd, ACPIIO_SETSLPSTATE, &sleep_type) == -1) err(EX_IOERR, "sleep type (%d) failed", sleep_type); - } - close(fd); + + return (0); +} + +static int +acpi_battinfo(int num) +{ + union acpi_battery_ioctl_arg battio; + const char *pwr_units; + + if (num < 0 || num > 64) + err(EX_USAGE, "invalid battery %d", num); + + battio.unit = num; + if (ioctl(acpifd, ACPIIO_CMBAT_GET_BIF, &battio) == -1) + err(EX_IOERR, "get battery info (%d) failed", num); + printf("Battery %d information\n", num); + if (battio.bif.units == 0) + pwr_units = "mWh"; + else + pwr_units = "mAh"; + + printf("Design capacity:\t%d %s\n", battio.bif.dcap, pwr_units); + printf("Last full capacity:\t%d %s\n", battio.bif.lfcap, pwr_units); + printf("Technology:\t\t%s\n", battio.bif.btech == 0 ? + "primary (non-rechargeable)" : "secondary (rechargeable)"); + printf("Design voltage:\t\t%d mV\n", battio.bif.dvol); + printf("Capacity (warn):\t%d %s\n", battio.bif.wcap, pwr_units); + printf("Capacity (low):\t\t%d %s\n", battio.bif.lcap, pwr_units); + printf("Low/warn granularity:\t%d %s\n", battio.bif.gra1, pwr_units); + printf("Warn/full granularity:\t%d %s\n", battio.bif.gra2, pwr_units); + 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); return (0); } @@ -82,7 +112,7 @@ acpi_sleep(int sleep_type) static void usage(const char* prog) { - printf("usage: %s [-deh] [-s [1|2|3|4|4b|5]]\n", prog); + printf("usage: %s [-deh] [-i batt] [-s 1-5]\n", prog); exit(0); } @@ -93,21 +123,23 @@ main(int argc, char *argv[]) int sleep_type; prog = argv[0]; + if (argc < 2) + usage(prog); + /* NOTREACHED */ + sleep_type = -1; - while ((c = getopt(argc, argv, "dehs:")) != -1) { + acpi_init(); + while ((c = getopt(argc, argv, "dehi:s:")) != -1) { switch (c) { + case 'i': + acpi_battinfo(atoi(optarg)); + break; case 'd': acpi_enable_disable(ACPIIO_DISABLE); break; - case 'e': acpi_enable_disable(ACPIIO_ENABLE); break; - - case 'h': - usage(prog); - break; - case 's': if (optarg[0] == 'S') sleep_type = optarg[1] - '0'; @@ -115,17 +147,22 @@ main(int argc, char *argv[]) sleep_type = optarg[0] - '0'; if (sleep_type < 0 || sleep_type > 5) errx(EX_USAGE, "invalid sleep type (%d)", - sleep_type); + sleep_type); break; + case 'h': default: - argc -= optind; - argv += optind; + usage(prog); + /* NOTREACHED */ } } + argc -= optind; + argv += optind; if (sleep_type != -1) { sleep(1); /* wait 1 sec. for key-release event */ acpi_sleep(sleep_type); } - return (0); + + close(acpifd); + exit (0); }