/*- * Copyright (c) 2000 Takanori Watanabe * Copyright (c) 2000 Mitsuru IWASAKI * 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. * * $FreeBSD$ */ #include "opt_acpi.h" #include #include #include #include #include #include #include #include #include #include "acpi.h" #include #include struct acpi_cmbat_softc { device_t dev; struct acpi_bif bif; struct acpi_bst bst; size_t bif_size; size_t bst_size; }; /* XXX: devclass_get_maxunit() don't give us the current allocated units... */ static int acpi_cmbat_units = 0; #define ACPI_BATTERY_BST_CHANGE 0x80 #define ACPI_BATTERY_BIF_CHANGE 0x81 #define PKG_GETINT(res, tmp, idx, dest, label) do { \ tmp = &res->Package.Elements[idx]; \ if (tmp->Type != ACPI_TYPE_NUMBER) \ goto label ; \ dest = tmp->Number.Value; \ } while(0) #define PKG_GETSTR(res, tmp, idx, dest, size, label) do { \ tmp = &res->Package.Elements[idx]; \ bzero(dest, sizeof(dest)); \ switch (tmp->Type) { \ case ACPI_TYPE_STRING: \ strncpy(dest, tmp->String.Pointer, size); \ break; \ case ACPI_TYPE_BUFFER: \ strncpy(dest, tmp->Buffer.Pointer, size); \ break; \ default: \ goto label; \ } \ dest[size-1] = '\0'; \ } while(0) static void acpi_cmbat_get_bst(void *context) { device_t dev = context; struct acpi_cmbat_softc *sc = device_get_softc(dev); ACPI_STATUS as; ACPI_BUFFER b; ACPI_OBJECT *res, *tmp; ACPI_HANDLE h = acpi_get_handle(dev); retry: b.Length = sc->bst_size; if(!b.Length){ b.Pointer = NULL; }else { if(!(b.Pointer = malloc(b.Length, M_TEMP, M_NOWAIT))){ device_printf(dev,"malloc failed"); return; } } as = AcpiEvaluateObject(h, "_BST", NULL, &b); if(as == AE_BUFFER_OVERFLOW){ sc->bst_size = b.Length; if(b.Pointer){ free(b.Pointer, M_TEMP); } device_printf(dev,"bst_size changed to %d\n",sc->bst_size); goto retry; }else if(as != AE_OK){ device_printf(dev, "CANNOT FOUND _BST (%d)\n", as); return; } res = b.Pointer; if ((res->Type != ACPI_TYPE_PACKAGE) && (res->Package.Count < 4)) return ; PKG_GETINT(res, tmp, 0, sc->bst.state, end); PKG_GETINT(res, tmp, 1, sc->bst.rate, end); PKG_GETINT(res, tmp, 2, sc->bst.cap, end); PKG_GETINT(res, tmp, 3, sc->bst.volt, end); end: free(b.Pointer, M_TEMP); } static void acpi_cmbat_get_bif(void *context) { device_t dev = context; struct acpi_cmbat_softc *sc = device_get_softc(dev); ACPI_BUFFER b; ACPI_STATUS as; ACPI_HANDLE h = acpi_get_handle(dev); ACPI_OBJECT *res, *tmp; retry: b.Length = sc->bif_size; if(!b.Length){ b.Pointer = NULL; }else { if(!(b.Pointer = malloc(b.Length, M_TEMP, M_NOWAIT))){ device_printf(dev,"malloc failed"); return; } } as = AcpiEvaluateObject(h, "_BIF", NULL, &b); if(as == AE_BUFFER_OVERFLOW){ sc->bif_size = b.Length; if(b.Pointer){ free(b.Pointer, M_TEMP); } device_printf(dev,"bif_size changed to %d\n",sc->bif_size); goto retry; }else if(as != AE_OK){ device_printf(dev, "CANNOT FOUND _BIF (%d)\n", as); return; } res = b.Pointer; if ((res->Type != ACPI_TYPE_PACKAGE) && (res->Package.Count < 13)) return ; PKG_GETINT(res, tmp, 0, sc->bif.unit, 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); PKG_GETINT(res, tmp, 4, sc->bif.dvol, end); PKG_GETINT(res, tmp, 5, sc->bif.wcap, end); PKG_GETINT(res, tmp, 6, sc->bif.lcap, end); PKG_GETINT(res, tmp, 7, sc->bif.gra1, end); PKG_GETINT(res, tmp, 8, sc->bif.gra2, end); PKG_GETSTR(res, tmp, 9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN, end); PKG_GETSTR(res, tmp, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN, end); PKG_GETSTR(res, tmp, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN, end); PKG_GETSTR(res, tmp, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN, end); end: free(b.Pointer, M_TEMP); } static void acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) { switch (notify) { case ACPI_BATTERY_BST_CHANGE: AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bst, context); break; case ACPI_BATTERY_BIF_CHANGE: AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cmbat_get_bif, context); break; default: break; } } static int acpi_cmbat_probe(device_t dev) { if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) && acpi_MatchHid(dev, "PNP0C0A")) { /* * Set device description */ device_set_desc(dev, "Control method Battery"); return(0); } return(ENXIO); } static int acpi_cmbat_attach(device_t dev) { ACPI_HANDLE handle = acpi_get_handle(dev); struct acpi_cmbat_softc *sc; sc = device_get_softc(dev); AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY, acpi_cmbat_notify_handler,dev); sc->bif_size = 0; sc->bst_size = 0; acpi_cmbat_get_bif(dev); acpi_cmbat_get_bst(dev); acpi_cmbat_units++; return(0); } static device_method_t acpi_cmbat_methods[] = { /* Device interface */ DEVMETHOD(device_probe, acpi_cmbat_probe), DEVMETHOD(device_attach, acpi_cmbat_attach), {0, 0} }; static driver_t acpi_cmbat_driver = { "acpi_cmbat", acpi_cmbat_methods, sizeof(struct acpi_cmbat_softc), }; static devclass_t acpi_cmbat_devclass; DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);