1 /*- 2 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 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 * $FreeBSD$ 27 */ 28 29 #include "opt_acpi.h" /* XXX trim includes */ 30 #include <sys/param.h> 31 #include <sys/kernel.h> 32 #include <sys/proc.h> 33 #include <sys/lock.h> 34 #include <sys/malloc.h> 35 #include <sys/mutex.h> 36 #include <sys/bus.h> 37 #include <sys/conf.h> 38 #include <sys/ioccom.h> 39 #include <sys/reboot.h> 40 #include <sys/sysctl.h> 41 #include <sys/ctype.h> 42 43 #include <machine/clock.h> 44 45 #include <machine/resource.h> 46 47 #include "acpi.h" 48 49 #include <dev/acpica/acpivar.h> 50 #include <dev/acpica/acpiio.h> 51 52 /* XXX should use our own malloc class */ 53 MALLOC_DECLARE(M_ACPIDEV); 54 55 /* 56 * ACPI Battery Abstruction Layer. 57 */ 58 59 struct acpi_batteries { 60 TAILQ_ENTRY(acpi_batteries) link; 61 struct acpi_battdesc battdesc; 62 }; 63 64 static TAILQ_HEAD(,acpi_batteries) acpi_batteries; 65 static int acpi_batteries_initted = 0; 66 static int acpi_batteries_units = 0; 67 static struct acpi_battinfo acpi_battery_battinfo; 68 69 static int 70 acpi_battery_get_units(void) 71 { 72 73 return (acpi_batteries_units); 74 } 75 76 static int 77 acpi_battery_get_battdesc(int logical_unit, struct acpi_battdesc *battdesc) 78 { 79 int i; 80 struct acpi_batteries *bp; 81 82 if (logical_unit < 0 || logical_unit >= acpi_batteries_units) { 83 return (ENXIO); 84 } 85 86 i = 0; 87 TAILQ_FOREACH(bp, &acpi_batteries, link) { 88 if (logical_unit == i) { 89 battdesc->type = bp->battdesc.type; 90 battdesc->phys_unit = bp->battdesc.phys_unit; 91 return (0); 92 } 93 i++; 94 } 95 96 return (ENXIO); 97 } 98 99 static int 100 acpi_battery_get_battinfo(int unit, struct acpi_battinfo *battinfo) 101 { 102 int error; 103 struct acpi_battdesc battdesc; 104 105 error = 0; 106 if (unit == -1) { 107 error = acpi_cmbat_get_battinfo(-1, battinfo); 108 goto out; 109 } else { 110 if ((error = acpi_battery_get_battdesc(unit, &battdesc)) != 0) { 111 goto out; 112 } 113 switch (battdesc.type) { 114 case ACPI_BATT_TYPE_CMBAT: 115 error = acpi_cmbat_get_battinfo(battdesc.phys_unit, 116 battinfo); 117 break; 118 default: 119 error = ENXIO; 120 break; 121 } 122 } 123 out: 124 return (error); 125 } 126 127 static int 128 acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg) 129 { 130 int error; 131 int logical_unit; 132 union acpi_battery_ioctl_arg *ioctl_arg; 133 134 ioctl_arg = (union acpi_battery_ioctl_arg *)addr; 135 error = 0; 136 switch (cmd) { 137 case ACPIIO_BATT_GET_UNITS: 138 *(int *)addr = acpi_battery_get_units(); 139 break; 140 141 case ACPIIO_BATT_GET_BATTDESC: 142 logical_unit = ioctl_arg->unit; 143 error = acpi_battery_get_battdesc(logical_unit, &ioctl_arg->battdesc); 144 break; 145 146 case ACPIIO_BATT_GET_BATTINFO: 147 logical_unit = ioctl_arg->unit; 148 error = acpi_battery_get_battinfo(logical_unit, 149 &ioctl_arg->battinfo); 150 break; 151 152 default: 153 error = EINVAL; 154 break; 155 } 156 157 return (error); 158 } 159 160 static int 161 acpi_battery_sysctl(SYSCTL_HANDLER_ARGS) 162 { 163 int val; 164 int error; 165 166 acpi_battery_get_battinfo(-1, &acpi_battery_battinfo); 167 val = *(u_int *)oidp->oid_arg1; 168 error = sysctl_handle_int(oidp, &val, 0, req); 169 return (error); 170 } 171 172 static int 173 acpi_battery_init(void) 174 { 175 device_t dev; 176 struct acpi_softc *sc; 177 int error; 178 179 if ((dev = devclass_get_device(acpi_devclass, 0)) == NULL) { 180 return (ENXIO); 181 } 182 if ((sc = device_get_softc(dev)) == NULL) { 183 return (ENXIO); 184 } 185 186 error = 0; 187 188 TAILQ_INIT(&acpi_batteries); 189 acpi_batteries_initted = 1; 190 191 if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_UNITS, 192 acpi_battery_ioctl, NULL)) != 0) { 193 return (error); 194 } 195 if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTDESC, 196 acpi_battery_ioctl, NULL)) != 0) { 197 return (error); 198 } 199 if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTINFO, 200 acpi_battery_ioctl, NULL)) != 0) { 201 return (error); 202 } 203 204 sysctl_ctx_init(&sc->acpi_battery_sysctl_ctx); 205 sc->acpi_battery_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_battery_sysctl_ctx, 206 SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 207 OID_AUTO, "battery", CTLFLAG_RD, 0, ""); 208 SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx, 209 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree), 210 OID_AUTO, "life", CTLTYPE_INT | CTLFLAG_RD, 211 &acpi_battery_battinfo.cap, 0, acpi_battery_sysctl, "I", ""); 212 SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx, 213 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree), 214 OID_AUTO, "time", CTLTYPE_INT | CTLFLAG_RD, 215 &acpi_battery_battinfo.min, 0, acpi_battery_sysctl, "I", ""); 216 SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx, 217 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree), 218 OID_AUTO, "state", CTLTYPE_INT | CTLFLAG_RD, 219 &acpi_battery_battinfo.state, 0, acpi_battery_sysctl, "I", ""); 220 SYSCTL_ADD_INT(&sc->acpi_battery_sysctl_ctx, 221 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree), 222 OID_AUTO, "units", CTLFLAG_RD, &acpi_batteries_units, 0, ""); 223 224 return (error); 225 } 226 227 int 228 acpi_battery_register(int type, int phys_unit) 229 { 230 int error; 231 struct acpi_batteries *bp; 232 233 error = 0; 234 if ((bp = malloc(sizeof(*bp), M_ACPIDEV, M_NOWAIT)) == NULL) { 235 return(ENOMEM); 236 } 237 238 bp->battdesc.type = type; 239 bp->battdesc.phys_unit = phys_unit; 240 if (acpi_batteries_initted == 0) { 241 if ((error = acpi_battery_init()) != 0) { 242 free(bp, M_ACPIDEV); 243 return(error); 244 } 245 } 246 247 TAILQ_INSERT_TAIL(&acpi_batteries, bp, link); 248 acpi_batteries_units++; 249 250 return(0); 251 } 252