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_smbat.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 Hans Petter Selasky
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/6.1/sys/dev/acpica/acpi_smbat.c 154427 2006-01-16 09:13:53Z bruno $");
   29 
   30 #include "opt_acpi.h"
   31 #include <sys/param.h>
   32 #include <sys/kernel.h>
   33 #include <sys/module.h>
   34 #include <sys/bus.h>
   35 
   36 #include <contrib/dev/acpica/acpi.h>
   37 #include <dev/acpica/acpivar.h>
   38 #include <dev/acpica/acpiio.h>
   39 #include <dev/acpica/acpi_smbus.h>
   40 
   41 /* Transactions have failed after 500 ms. */
   42 #define SMBUS_TIMEOUT   50
   43 
   44 struct acpi_smbat_softc {
   45         uint8_t         sb_base_addr;
   46         device_t        ec_dev;
   47 
   48         struct acpi_bif bif;
   49         struct acpi_bst bst;
   50         struct timespec bif_lastupdated;
   51         struct timespec bst_lastupdated;
   52 };
   53 
   54 static int      acpi_smbat_probe(device_t dev);
   55 static int      acpi_smbat_attach(device_t dev);
   56 static int      acpi_smbat_shutdown(device_t dev);
   57 static int      acpi_smbat_info_expired(struct timespec *lastupdated);
   58 static void     acpi_smbat_info_updated(struct timespec *lastupdated);
   59 static int      acpi_smbat_get_bif(device_t dev, struct acpi_bif *bif);
   60 static int      acpi_smbat_get_bst(device_t dev, struct acpi_bst *bst);
   61 
   62 ACPI_SERIAL_DECL(smbat, "ACPI Smart Battery");
   63 
   64 static device_method_t acpi_smbat_methods[] = {
   65         /* device interface */
   66         DEVMETHOD(device_probe, acpi_smbat_probe),
   67         DEVMETHOD(device_attach, acpi_smbat_attach),
   68         DEVMETHOD(device_shutdown, acpi_smbat_shutdown),
   69 
   70         /* ACPI battery interface */
   71         DEVMETHOD(acpi_batt_get_status, acpi_smbat_get_bst),
   72         DEVMETHOD(acpi_batt_get_info, acpi_smbat_get_bif),
   73 
   74         {0, 0}
   75 };
   76 
   77 static driver_t acpi_smbat_driver = {
   78         "battery",
   79         acpi_smbat_methods,
   80         sizeof(struct acpi_smbat_softc),
   81 };
   82 
   83 static devclass_t acpi_smbat_devclass;
   84 DRIVER_MODULE(acpi_smbat, acpi, acpi_smbat_driver, acpi_smbat_devclass, 0, 0);
   85 MODULE_DEPEND(acpi_smbat, acpi, 1, 1, 1);
   86 
   87 static int
   88 acpi_smbat_probe(device_t dev)
   89 {
   90         static char *smbat_ids[] = {"ACPI0001", "ACPI0005", NULL};
   91         ACPI_STATUS status;
   92 
   93         if (acpi_disabled("smbat") ||
   94             ACPI_ID_PROBE(device_get_parent(dev), dev, smbat_ids) == NULL)
   95                 return (ENXIO);
   96         status = AcpiEvaluateObject(acpi_get_handle(dev), "_EC", NULL, NULL);
   97         if (ACPI_FAILURE(status))
   98                 return (ENXIO);
   99 
  100         device_set_desc(dev, "ACPI Smart Battery");
  101         return (0);
  102 }
  103 
  104 static int
  105 acpi_smbat_attach(device_t dev)
  106 {
  107         struct acpi_smbat_softc *sc;
  108         uint32_t base;
  109 
  110         sc = device_get_softc(dev);
  111         if (ACPI_FAILURE(acpi_GetInteger(acpi_get_handle(dev), "_EC", &base))) {
  112                 device_printf(dev, "cannot get EC base address\n");
  113                 return (ENXIO);
  114         }
  115         sc->sb_base_addr = (base >> 8) & 0xff;
  116 
  117         /* XXX Only works with one EC, but nearly all systems only have one. */
  118         sc->ec_dev = devclass_get_device(devclass_find("acpi_ec"), 0);
  119         if (sc->ec_dev == NULL) {
  120                 device_printf(dev, "cannot find EC device\n");
  121                 return (ENXIO);
  122         }
  123 
  124         timespecclear(&sc->bif_lastupdated);
  125         timespecclear(&sc->bst_lastupdated);
  126 
  127         if (acpi_battery_register(dev) != 0) {
  128                 device_printf(dev, "cannot register battery\n");
  129                 return (ENXIO);
  130         }
  131         return (0);
  132 }
  133 
  134 static int
  135 acpi_smbat_shutdown(device_t dev)
  136 {
  137         struct acpi_smbat_softc *sc;
  138 
  139         sc = device_get_softc(dev);
  140         acpi_battery_remove(dev);
  141         return (0);
  142 }
  143 
  144 static int
  145 acpi_smbat_info_expired(struct timespec *lastupdated)
  146 {
  147         struct timespec curtime;
  148 
  149         ACPI_SERIAL_ASSERT(smbat);
  150 
  151         if (lastupdated == NULL)
  152                 return (TRUE);
  153         if (!timespecisset(lastupdated))
  154                 return (TRUE);
  155 
  156         getnanotime(&curtime);
  157         timespecsub(&curtime, lastupdated);
  158         return (curtime.tv_sec < 0 ||
  159             curtime.tv_sec > acpi_battery_get_info_expire());
  160 }
  161 
  162 static void
  163 acpi_smbat_info_updated(struct timespec *lastupdated)
  164 {
  165 
  166         ACPI_SERIAL_ASSERT(smbat);
  167 
  168         if (lastupdated != NULL)
  169                 getnanotime(lastupdated);
  170 }
  171 
  172 static int
  173 acpi_smbus_read_2(struct acpi_smbat_softc *sc, uint8_t addr, uint8_t cmd,
  174     uint16_t *ptr)
  175 {
  176         int error, to;
  177         ACPI_INTEGER val;
  178 
  179         ACPI_SERIAL_ASSERT(smbat);
  180 
  181         val = addr;
  182         error = ACPI_EC_WRITE(sc->ec_dev, sc->sb_base_addr + SMBUS_ADDR,
  183             val, 1);
  184         if (error)
  185                 goto out;
  186 
  187         val = cmd;
  188         error = ACPI_EC_WRITE(sc->ec_dev, sc->sb_base_addr + SMBUS_CMD,
  189             val, 1);
  190         if (error)
  191                 goto out;
  192 
  193         val = 0x09; /* | 0x80 if PEC */
  194         error = ACPI_EC_WRITE(sc->ec_dev, sc->sb_base_addr + SMBUS_PRTCL,
  195             val, 1);
  196         if (error)
  197                 goto out;
  198 
  199         for (to = SMBUS_TIMEOUT; to != 0; to--) {
  200                 error = ACPI_EC_READ(sc->ec_dev, sc->sb_base_addr + SMBUS_PRTCL,
  201                     &val, 1);
  202                 if (error)
  203                         goto out;
  204                 if (val == 0)
  205                         break;
  206                 AcpiOsSleep(10);
  207         }
  208         if (to == 0) {
  209                 error = ETIMEDOUT;
  210                 goto out;
  211         }
  212 
  213         error = ACPI_EC_READ(sc->ec_dev, sc->sb_base_addr + SMBUS_STS, &val, 1);
  214         if (error)
  215                 goto out;
  216         if (val & SMBUS_STS_MASK) {
  217                 printf("%s: AE_ERROR 0x%x\n",
  218                        __FUNCTION__, (int)(val & SMBUS_STS_MASK));
  219                 error = EIO;
  220                 goto out;
  221         }
  222 
  223         error = ACPI_EC_READ(sc->ec_dev, sc->sb_base_addr + SMBUS_DATA,
  224             &val, 2);
  225         if (error)
  226                 goto out;
  227 
  228         *ptr = val;
  229 
  230 out:
  231         return (error);
  232 }
  233 
  234 static int
  235 acpi_smbus_read_multi_1(struct acpi_smbat_softc *sc, uint8_t addr, uint8_t cmd,
  236     uint8_t *ptr, uint16_t len)
  237 {
  238         ACPI_INTEGER val;
  239         uint8_t to;
  240         int error;
  241 
  242         ACPI_SERIAL_ASSERT(smbat);
  243 
  244         val = addr;
  245         error = ACPI_EC_WRITE(sc->ec_dev, sc->sb_base_addr + SMBUS_ADDR,
  246             val, 1);
  247         if (error)
  248                 goto out;
  249 
  250         val = cmd;
  251         error = ACPI_EC_WRITE(sc->ec_dev, sc->sb_base_addr + SMBUS_CMD,
  252             val, 1);
  253         if (error)
  254                 goto out;
  255 
  256         val = 0x0B /* | 0x80 if PEC */ ;
  257         error = ACPI_EC_WRITE(sc->ec_dev, sc->sb_base_addr + SMBUS_PRTCL,
  258             val, 1);
  259         if (error)
  260                 goto out;
  261 
  262         for (to = SMBUS_TIMEOUT; to != 0; to--) {
  263                 error = ACPI_EC_READ(sc->ec_dev, sc->sb_base_addr + SMBUS_PRTCL,
  264                     &val, 1);
  265                 if (error)
  266                         goto out;
  267                 if (val == 0)
  268                         break;
  269                 AcpiOsSleep(10);
  270         }
  271         if (to == 0) {
  272                 error = ETIMEDOUT;
  273                 goto out;
  274         }
  275 
  276         error = ACPI_EC_READ(sc->ec_dev, sc->sb_base_addr + SMBUS_STS, &val, 1);
  277         if (error)
  278                 goto out;
  279         if (val & SMBUS_STS_MASK) {
  280                 printf("%s: AE_ERROR 0x%x\n",
  281                        __FUNCTION__, (int)(val & SMBUS_STS_MASK));
  282                 error = EIO;
  283                 goto out;
  284         }
  285 
  286         /* get length */
  287         error = ACPI_EC_READ(sc->ec_dev, sc->sb_base_addr + SMBUS_BCNT,
  288             &val, 1);
  289         if (error)
  290                 goto out;
  291         val = (val & 0x1f) + 1;
  292 
  293         bzero(ptr, len);
  294         if (len > val)
  295                 len = val;
  296 
  297         while (len--) {
  298                 error = ACPI_EC_READ(sc->ec_dev, sc->sb_base_addr + SMBUS_DATA
  299                     + len, &val, 1);
  300                 if (error)
  301                         goto out;
  302 
  303                 ptr[len] = val;
  304         }
  305 
  306 out:
  307         return (error);
  308 }
  309 
  310 static int
  311 acpi_smbat_get_bst(device_t dev, struct acpi_bst *bst)
  312 {
  313         struct acpi_smbat_softc *sc;
  314         int error;
  315         uint32_t cap_units, factor;
  316         int16_t val;
  317         uint8_t addr;
  318 
  319         ACPI_SERIAL_BEGIN(smbat);
  320 
  321         addr = SMBATT_ADDRESS;
  322         error = ENXIO;
  323         sc = device_get_softc(dev);
  324 
  325         if (!acpi_smbat_info_expired(&sc->bst_lastupdated)) {
  326                 error = 0;
  327                 goto out;
  328         }
  329 
  330         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_BATTERY_MODE, &val))
  331                 goto out;
  332         if (val & SMBATT_BM_CAPACITY_MODE) {
  333                 factor = 10;
  334                 cap_units = ACPI_BIF_UNITS_MW;
  335         } else {
  336                 factor = 1;
  337                 cap_units = ACPI_BIF_UNITS_MA;
  338         }
  339 
  340         /* get battery status */
  341         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_BATTERY_STATUS, &val))
  342                 goto out;
  343 
  344         sc->bst.state = 0;
  345         if (val & SMBATT_BS_DISCHARGING)
  346                 sc->bst.state |= ACPI_BATT_STAT_DISCHARG;
  347 
  348         if (val & SMBATT_BS_REMAINING_CAPACITY_ALARM)
  349                 sc->bst.state |= ACPI_BATT_STAT_CRITICAL;
  350 
  351         /*
  352          * If the rate is negative, it is discharging.  Otherwise,
  353          * it is charging.
  354          */
  355         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_CURRENT, &val))
  356                 goto out;
  357 
  358         if (val > 0) {
  359                 sc->bst.rate = val * factor;
  360                 sc->bst.state |= ACPI_BATT_STAT_CHARGING;
  361         } else if (val < 0)
  362                 sc->bst.rate = (-val) * factor;
  363         else
  364                 sc->bst.rate = 0;
  365 
  366         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_REMAINING_CAPACITY, &val))
  367                 goto out;
  368         sc->bst.cap = val * factor;
  369 
  370         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_VOLTAGE, &val))
  371                 goto out;
  372         sc->bst.volt = val;
  373 
  374         acpi_smbat_info_updated(&sc->bst_lastupdated);
  375         error = 0;
  376 
  377 out:
  378         if (error == 0)
  379                 memcpy(bst, &sc->bst, sizeof(sc->bst));
  380         ACPI_SERIAL_END(smbat);
  381         return (error);
  382 }
  383 
  384 static int
  385 acpi_smbat_get_bif(device_t dev, struct acpi_bif *bif)
  386 {
  387         struct acpi_smbat_softc *sc;
  388         int error;
  389         uint32_t factor;
  390         uint16_t val;
  391         uint8_t addr;
  392 
  393         ACPI_SERIAL_BEGIN(smbat);
  394 
  395         addr = SMBATT_ADDRESS;
  396         error = ENXIO;
  397         sc = device_get_softc(dev);
  398 
  399         if (!acpi_smbat_info_expired(&sc->bif_lastupdated)) {
  400                 error = 0;
  401                 goto out;
  402         }
  403 
  404         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_BATTERY_MODE, &val))
  405                 goto out;
  406         if (val & SMBATT_BM_CAPACITY_MODE) {
  407                 factor = 10;
  408                 sc->bif.units = ACPI_BIF_UNITS_MW;
  409         } else {
  410                 factor = 1;
  411                 sc->bif.units = ACPI_BIF_UNITS_MA;
  412         }
  413 
  414         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_DESIGN_CAPACITY, &val))
  415                 goto out;
  416         sc->bif.dcap = val * factor;
  417 
  418         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_FULL_CHARGE_CAPACITY, &val))
  419                 goto out;
  420         sc->bif.lfcap = val * factor;
  421         sc->bif.btech = 1;              /* secondary (rechargeable) */
  422 
  423         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_DESIGN_VOLTAGE, &val))
  424                 goto out;
  425         sc->bif.dvol = val;
  426 
  427         sc->bif.wcap = sc->bif.dcap / 10;
  428         sc->bif.lcap = sc->bif.dcap / 10;
  429 
  430         sc->bif.gra1 = factor;  /* not supported */
  431         sc->bif.gra2 = factor;  /* not supported */
  432 
  433         if (acpi_smbus_read_multi_1(sc, addr, SMBATT_CMD_DEVICE_NAME,
  434             sc->bif.model, sizeof(sc->bif.model)))
  435                 goto out;
  436 
  437         if (acpi_smbus_read_2(sc, addr, SMBATT_CMD_SERIAL_NUMBER, &val))
  438                 goto out;
  439         snprintf(sc->bif.serial, sizeof(sc->bif.serial), "0x%04x", val);
  440 
  441         if (acpi_smbus_read_multi_1(sc, addr, SMBATT_CMD_DEVICE_CHEMISTRY,
  442             sc->bif.type, sizeof(sc->bif.type)))
  443                 goto out;
  444 
  445         if (acpi_smbus_read_multi_1(sc, addr, SMBATT_CMD_MANUFACTURER_DATA,
  446             sc->bif.oeminfo, sizeof(sc->bif.oeminfo)))
  447                 goto out;
  448 
  449         /* XXX check if device was replugged during read? */
  450 
  451         acpi_smbat_info_updated(&sc->bif_lastupdated);
  452         error = 0;
  453 
  454 out:
  455         if (error == 0)
  456                 memcpy(bif, &sc->bif, sizeof(sc->bif));
  457         ACPI_SERIAL_END(smbat);
  458         return (error);
  459 }

Cache object: dd5a7ad5a92699f382e55a162335a734


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