The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/acpica/acpi_battery.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2005 Nate Lawson
    3  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD: releng/8.1/sys/dev/acpica/acpi_battery.c 196405 2009-08-20 20:23:28Z jhb $");
   30 
   31 #include "opt_acpi.h"
   32 #include <sys/param.h>
   33 #include <sys/kernel.h>
   34 #include <sys/malloc.h>
   35 #include <sys/bus.h>
   36 #include <sys/ioccom.h>
   37 #include <sys/sysctl.h>
   38 
   39 #include <contrib/dev/acpica/include/acpi.h>
   40 
   41 #include <dev/acpica/acpivar.h>
   42 #include <dev/acpica/acpiio.h>
   43 
   44 /* Default seconds before re-sampling the battery state. */
   45 #define ACPI_BATTERY_INFO_EXPIRE        5
   46 
   47 static int      acpi_batteries_initted;
   48 static int      acpi_battery_info_expire = ACPI_BATTERY_INFO_EXPIRE;
   49 static struct   acpi_battinfo   acpi_battery_battinfo;
   50 static struct   sysctl_ctx_list acpi_battery_sysctl_ctx;
   51 static struct   sysctl_oid      *acpi_battery_sysctl_tree;
   52 
   53 ACPI_SERIAL_DECL(battery, "ACPI generic battery");
   54 
   55 static void acpi_reset_battinfo(struct acpi_battinfo *info);
   56 static void acpi_battery_clean_str(char *str, int len);
   57 static device_t acpi_battery_find_dev(u_int logical_unit);
   58 static int acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg);
   59 static int acpi_battery_sysctl(SYSCTL_HANDLER_ARGS);
   60 static int acpi_battery_units_sysctl(SYSCTL_HANDLER_ARGS);
   61 static int acpi_battery_init(void);
   62 
   63 int
   64 acpi_battery_register(device_t dev)
   65 {
   66     int error;
   67 
   68     error = 0;
   69     ACPI_SERIAL_BEGIN(battery);
   70     if (!acpi_batteries_initted)
   71         error = acpi_battery_init();
   72     ACPI_SERIAL_END(battery);
   73     return (error);
   74 }
   75 
   76 int
   77 acpi_battery_remove(device_t dev)
   78 {
   79 
   80     return (0);
   81 }
   82 
   83 int
   84 acpi_battery_get_units(void)
   85 {
   86     devclass_t batt_dc;
   87 
   88     batt_dc = devclass_find("battery");
   89     if (batt_dc == NULL)
   90         return (0);
   91     return (devclass_get_count(batt_dc));
   92 }
   93 
   94 int
   95 acpi_battery_get_info_expire(void)
   96 {
   97 
   98     return (acpi_battery_info_expire);
   99 }
  100 
  101 /* Check _BST results for validity. */
  102 int
  103 acpi_battery_bst_valid(struct acpi_bst *bst)
  104 {
  105     return (bst->state < ACPI_BATT_STAT_MAX && bst->cap != ACPI_BATT_UNKNOWN &&
  106         bst->volt != ACPI_BATT_UNKNOWN);
  107 }
  108 
  109 /* Check _BIF results for validity. */
  110 int
  111 acpi_battery_bif_valid(struct acpi_bif *bif)
  112 {
  113     return (bif->lfcap != 0);
  114 }
  115 
  116 /* Get info about one or all batteries. */
  117 int
  118 acpi_battery_get_battinfo(device_t dev, struct acpi_battinfo *battinfo)
  119 {
  120     int batt_stat, devcount, dev_idx, error, i;
  121     int total_cap, total_min, valid_rate, valid_units;
  122     devclass_t batt_dc;
  123     device_t batt_dev;
  124     struct acpi_bst *bst;
  125     struct acpi_bif *bif;
  126     struct acpi_battinfo *bi;
  127 
  128     /*
  129      * Get the battery devclass and max unit for battery devices.  If there
  130      * are none or error, return immediately.
  131      */
  132     batt_dc = devclass_find("battery");
  133     if (batt_dc == NULL)
  134         return (ENXIO);
  135     devcount = devclass_get_maxunit(batt_dc);
  136     if (devcount == 0)
  137         return (ENXIO);
  138 
  139     /*
  140      * Allocate storage for all _BST data, their derived battinfo data,
  141      * and the current battery's _BIF data.
  142      */
  143     bst = malloc(devcount * sizeof(*bst), M_TEMP, M_WAITOK | M_ZERO);
  144     bi = malloc(devcount * sizeof(*bi), M_TEMP, M_WAITOK | M_ZERO);
  145     bif = malloc(sizeof(*bif), M_TEMP, M_WAITOK | M_ZERO);
  146 
  147     /*
  148      * Pass 1:  for each battery that is present and valid, get its status,
  149      * calculate percent capacity remaining, and sum all the current
  150      * discharge rates.
  151      */
  152     dev_idx = -1;
  153     batt_stat = valid_rate = valid_units = 0;
  154     for (i = 0; i < devcount; i++) {
  155         /* Default info for every battery is "not present". */
  156         acpi_reset_battinfo(&bi[i]);
  157 
  158         /*
  159          * Find the device.  Since devcount is in terms of max units, this
  160          * may be a sparse array so skip devices that aren't present.
  161          */
  162         batt_dev = devclass_get_device(batt_dc, i);
  163         if (batt_dev == NULL)
  164             continue;
  165 
  166         /* If examining a specific battery and this is it, record its index. */
  167         if (dev != NULL && dev == batt_dev)
  168             dev_idx = i;
  169 
  170         /*
  171          * Be sure we can get various info from the battery.  Note that
  172          * acpi_BatteryIsPresent() is not enough because smart batteries only
  173          * return that the device is present.
  174          */
  175         if (!acpi_BatteryIsPresent(batt_dev) ||
  176             ACPI_BATT_GET_STATUS(batt_dev, &bst[i]) != 0 ||
  177             ACPI_BATT_GET_INFO(batt_dev, bif) != 0)
  178             continue;
  179 
  180         /* If a battery is not installed, we sometimes get strange values. */
  181         if (!acpi_battery_bst_valid(&bst[i]) ||
  182             !acpi_battery_bif_valid(bif))
  183             continue;
  184 
  185         /*
  186          * Record current state.  If both charging and discharging are set,
  187          * ignore the charging flag.
  188          */
  189         valid_units++;
  190         if ((bst[i].state & ACPI_BATT_STAT_DISCHARG) != 0)
  191             bst[i].state &= ~ACPI_BATT_STAT_CHARGING;
  192         batt_stat |= bst[i].state;
  193         bi[i].state = bst[i].state;
  194 
  195         /*
  196          * If the battery info is in terms of mA, convert to mW by
  197          * multiplying by the design voltage.  If the design voltage
  198          * is 0 (due to some error reading the battery), skip this
  199          * conversion.
  200          */
  201         if (bif->units == ACPI_BIF_UNITS_MA && bif->dvol != 0 && dev == NULL) {
  202             bst[i].rate = (bst[i].rate * bif->dvol) / 1000;
  203             bst[i].cap = (bst[i].cap * bif->dvol) / 1000;
  204             bif->lfcap = (bif->lfcap * bif->dvol) / 1000;
  205         }
  206 
  207         /* Calculate percent capacity remaining. */
  208         bi[i].cap = (100 * bst[i].cap) / bif->lfcap;
  209 
  210         /*
  211          * Some laptops report the "design-capacity" instead of the
  212          * "real-capacity" when the battery is fully charged.  That breaks
  213          * the above arithmetic as it needs to be 100% maximum.
  214          */
  215         if (bi[i].cap > 100)
  216             bi[i].cap = 100;
  217 
  218         /*
  219          * On systems with more than one battery, they may get used
  220          * sequentially, thus bst.rate may only signify the one currently
  221          * in use.  For the remaining batteries, bst.rate will be zero,
  222          * which makes it impossible to calculate the total remaining time.
  223          * Therefore, we sum the bst.rate for batteries in the discharging
  224          * state and use the sum to calculate the total remaining time.
  225          */
  226         if (bst[i].rate != ACPI_BATT_UNKNOWN &&
  227             (bst[i].state & ACPI_BATT_STAT_DISCHARG) != 0)
  228             valid_rate += bst[i].rate;
  229     }
  230 
  231     /* If the caller asked for a device but we didn't find it, error. */
  232     if (dev != NULL && dev_idx == -1) {
  233         error = ENXIO;
  234         goto out;
  235     }
  236 
  237     /* Pass 2:  calculate capacity and remaining time for all batteries. */
  238     total_cap = total_min = 0;
  239     for (i = 0; i < devcount; i++) {
  240         /*
  241          * If any batteries are discharging, use the sum of the bst.rate
  242          * values.  Otherwise, we are on AC power, and there is infinite
  243          * time remaining for this battery until we go offline.
  244          */
  245         if (valid_rate > 0)
  246             bi[i].min = (60 * bst[i].cap) / valid_rate;
  247         else
  248             bi[i].min = 0;
  249         total_min += bi[i].min;
  250 
  251         /* If this battery is not present, don't use its capacity. */
  252         if (bi[i].cap != -1)
  253             total_cap += bi[i].cap;
  254     }
  255 
  256     /*
  257      * Return total battery percent and time remaining.  If there are
  258      * no valid batteries, report values as unknown.
  259      */
  260     if (valid_units > 0) {
  261         if (dev == NULL) {
  262             battinfo->cap = total_cap / valid_units;
  263             battinfo->min = total_min;
  264             battinfo->state = batt_stat;
  265             battinfo->rate = valid_rate;
  266         } else {
  267             battinfo->cap = bi[dev_idx].cap;
  268             battinfo->min = bi[dev_idx].min;
  269             battinfo->state = bi[dev_idx].state;
  270             battinfo->rate = bst[dev_idx].rate;
  271         }
  272 
  273         /*
  274          * If the queried battery has no discharge rate or is charging,
  275          * report that we don't know the remaining time.
  276          */
  277         if (valid_rate == 0 || (battinfo->state & ACPI_BATT_STAT_CHARGING))
  278             battinfo->min = -1;
  279     } else
  280         acpi_reset_battinfo(battinfo);
  281 
  282     error = 0;
  283 
  284 out:
  285     if (bi)
  286         free(bi, M_TEMP);
  287     if (bif)
  288         free(bif, M_TEMP);
  289     if (bst)
  290         free(bst, M_TEMP);
  291     return (error);
  292 }
  293 
  294 static void
  295 acpi_reset_battinfo(struct acpi_battinfo *info)
  296 {
  297     info->cap = -1;
  298     info->min = -1;
  299     info->state = ACPI_BATT_STAT_NOT_PRESENT;
  300     info->rate = -1;
  301 }
  302 
  303 /* Make string printable, removing invalid chars. */
  304 static void
  305 acpi_battery_clean_str(char *str, int len)
  306 {
  307     int i;
  308 
  309     for (i = 0; i < len && *str != '\0'; i++, str++) {
  310         if (!isprint(*str))
  311             *str = '?';
  312     }
  313 
  314     /* NUL-terminate the string if we reached the end. */
  315     if (i == len)
  316         *str = '\0';
  317 }
  318 
  319 /*
  320  * The battery interface deals with devices and methods but userland
  321  * expects a logical unit number.  Convert a logical unit to a device_t.
  322  */
  323 static device_t
  324 acpi_battery_find_dev(u_int logical_unit)
  325 {
  326     int found_unit, i, maxunit;
  327     device_t dev;
  328     devclass_t batt_dc;
  329 
  330     dev = NULL;
  331     found_unit = 0;
  332     batt_dc = devclass_find("battery");
  333     maxunit = devclass_get_maxunit(batt_dc);
  334     for (i = 0; i < maxunit; i++) {
  335         dev = devclass_get_device(batt_dc, i);
  336         if (dev == NULL)
  337             continue;
  338         if (logical_unit == found_unit)
  339             break;
  340         found_unit++;
  341         dev = NULL;
  342     }
  343 
  344     return (dev);
  345 }
  346 
  347 static int
  348 acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg)
  349 {
  350     union acpi_battery_ioctl_arg *ioctl_arg;
  351     int error, unit;
  352     device_t dev;
  353 
  354     /* For commands that use the ioctl_arg struct, validate it first. */
  355     error = ENXIO;
  356     unit = 0;
  357     dev = NULL;
  358     ioctl_arg = NULL;
  359     if (IOCPARM_LEN(cmd) == sizeof(*ioctl_arg)) {
  360         ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
  361         unit = ioctl_arg->unit;
  362         if (unit != ACPI_BATTERY_ALL_UNITS)
  363             dev = acpi_battery_find_dev(unit);
  364     }
  365 
  366     /*
  367      * No security check required: information retrieval only.  If
  368      * new functions are added here, a check might be required.
  369      */
  370     switch (cmd) {
  371     case ACPIIO_BATT_GET_UNITS:
  372         *(int *)addr = acpi_battery_get_units();
  373         error = 0;
  374         break;
  375     case ACPIIO_BATT_GET_BATTINFO:
  376         if (dev != NULL || unit == ACPI_BATTERY_ALL_UNITS) {
  377             bzero(&ioctl_arg->battinfo, sizeof(ioctl_arg->battinfo));
  378             error = acpi_battery_get_battinfo(dev, &ioctl_arg->battinfo);
  379         }
  380         break;
  381     case ACPIIO_BATT_GET_BIF:
  382         if (dev != NULL) {
  383             bzero(&ioctl_arg->bif, sizeof(ioctl_arg->bif));
  384             error = ACPI_BATT_GET_INFO(dev, &ioctl_arg->bif);
  385 
  386             /*
  387              * Remove invalid characters.  Perhaps this should be done
  388              * within a convenience function so all callers get the
  389              * benefit.
  390              */
  391             acpi_battery_clean_str(ioctl_arg->bif.model,
  392                 sizeof(ioctl_arg->bif.model));
  393             acpi_battery_clean_str(ioctl_arg->bif.serial,
  394                 sizeof(ioctl_arg->bif.serial));
  395             acpi_battery_clean_str(ioctl_arg->bif.type,
  396                 sizeof(ioctl_arg->bif.type));
  397             acpi_battery_clean_str(ioctl_arg->bif.oeminfo,
  398                 sizeof(ioctl_arg->bif.oeminfo));
  399         }
  400         break;
  401     case ACPIIO_BATT_GET_BST:
  402         if (dev != NULL) {
  403             bzero(&ioctl_arg->bst, sizeof(ioctl_arg->bst));
  404             error = ACPI_BATT_GET_STATUS(dev, &ioctl_arg->bst);
  405         }
  406         break;
  407     default:
  408         error = EINVAL;
  409     }
  410 
  411     return (error);
  412 }
  413 
  414 static int
  415 acpi_battery_sysctl(SYSCTL_HANDLER_ARGS)
  416 {
  417     int val, error;
  418 
  419     acpi_battery_get_battinfo(NULL, &acpi_battery_battinfo);
  420     val = *(u_int *)oidp->oid_arg1;
  421     error = sysctl_handle_int(oidp, &val, 0, req);
  422     return (error);
  423 }
  424 
  425 static int
  426 acpi_battery_units_sysctl(SYSCTL_HANDLER_ARGS)
  427 {
  428     int count, error;
  429 
  430     count = acpi_battery_get_units();
  431     error = sysctl_handle_int(oidp, &count, 0, req);
  432     return (error);
  433 }
  434 
  435 static int
  436 acpi_battery_init(void)
  437 {
  438     struct acpi_softc   *sc;
  439     device_t             dev;
  440     int                  error;
  441 
  442     ACPI_SERIAL_ASSERT(battery);
  443 
  444     error = ENXIO;
  445     dev = devclass_get_device(devclass_find("acpi"), 0);
  446     if (dev == NULL)
  447         goto out;
  448     sc = device_get_softc(dev);
  449 
  450     error = acpi_register_ioctl(ACPIIO_BATT_GET_UNITS, acpi_battery_ioctl,
  451         NULL);
  452     if (error != 0)
  453         goto out;
  454     error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTINFO, acpi_battery_ioctl,
  455         NULL);
  456     if (error != 0)
  457         goto out;
  458     error = acpi_register_ioctl(ACPIIO_BATT_GET_BIF, acpi_battery_ioctl, NULL);
  459     if (error != 0)
  460         goto out;
  461     error = acpi_register_ioctl(ACPIIO_BATT_GET_BST, acpi_battery_ioctl, NULL);
  462     if (error != 0)
  463         goto out;
  464 
  465     sysctl_ctx_init(&acpi_battery_sysctl_ctx);
  466     acpi_battery_sysctl_tree = SYSCTL_ADD_NODE(&acpi_battery_sysctl_ctx,
  467         SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO, "battery", CTLFLAG_RD,
  468         0, "battery status and info");
  469     SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
  470         SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
  471         OID_AUTO, "life", CTLTYPE_INT | CTLFLAG_RD,
  472         &acpi_battery_battinfo.cap, 0, acpi_battery_sysctl, "I",
  473         "percent capacity remaining");
  474     SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
  475         SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
  476         OID_AUTO, "time", CTLTYPE_INT | CTLFLAG_RD,
  477         &acpi_battery_battinfo.min, 0, acpi_battery_sysctl, "I",
  478         "remaining time in minutes");
  479     SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
  480         SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
  481         OID_AUTO, "state", CTLTYPE_INT | CTLFLAG_RD,
  482         &acpi_battery_battinfo.state, 0, acpi_battery_sysctl, "I",
  483         "current status flags");
  484     SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
  485         SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
  486         OID_AUTO, "units", CTLTYPE_INT | CTLFLAG_RD,
  487         NULL, 0, acpi_battery_units_sysctl, "I", "number of batteries");
  488     SYSCTL_ADD_INT(&acpi_battery_sysctl_ctx,
  489         SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
  490         OID_AUTO, "info_expire", CTLFLAG_RW,
  491         &acpi_battery_info_expire, 0,
  492         "time in seconds until info is refreshed");
  493 
  494     acpi_batteries_initted = TRUE;
  495 
  496 out:
  497     if (error != 0) {
  498         acpi_deregister_ioctl(ACPIIO_BATT_GET_UNITS, acpi_battery_ioctl);
  499         acpi_deregister_ioctl(ACPIIO_BATT_GET_BATTINFO, acpi_battery_ioctl);
  500         acpi_deregister_ioctl(ACPIIO_BATT_GET_BIF, acpi_battery_ioctl);
  501         acpi_deregister_ioctl(ACPIIO_BATT_GET_BST, acpi_battery_ioctl);
  502     }
  503     return (error);
  504 }

Cache object: 25ad41c126b678ac750c2f2f04a34fab


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.