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

Cache object: c7bcceed1da983f3771c0602498593ae


[ 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.