2 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include "opt_acpi.h" /* XXX trim includes */
30 #include <sys/param.h>
31 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/mutex.h>
38 #include <sys/ioccom.h>
39 #include <sys/reboot.h>
40 #include <sys/sysctl.h>
41 #include <sys/ctype.h>
43 #include <machine/clock.h>
45 #include <machine/resource.h>
49 #include <dev/acpica/acpivar.h>
50 #include <dev/acpica/acpiio.h>
52 MALLOC_DEFINE(M_ACPIBATT, "acpibatt", "ACPI generic battery data");
55 * ACPI Battery Abstruction Layer.
58 struct acpi_batteries {
59 TAILQ_ENTRY(acpi_batteries) link;
60 struct acpi_battdesc battdesc;
63 static TAILQ_HEAD(,acpi_batteries) acpi_batteries;
64 static int acpi_batteries_initted = 0;
65 static int acpi_batteries_units = 0;
66 static int acpi_battery_info_expire = 5;
67 static struct acpi_battinfo acpi_battery_battinfo;
70 acpi_battery_get_units(void)
73 return (acpi_batteries_units);
77 acpi_battery_get_battdesc(int logical_unit, struct acpi_battdesc *battdesc)
80 struct acpi_batteries *bp;
82 if (logical_unit < 0 || logical_unit >= acpi_batteries_units) {
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;
100 acpi_battery_get_battinfo(int unit, struct acpi_battinfo *battinfo)
103 struct acpi_battdesc battdesc;
107 error = acpi_cmbat_get_battinfo(-1, battinfo);
110 if ((error = acpi_battery_get_battdesc(unit, &battdesc)) != 0) {
113 switch (battdesc.type) {
114 case ACPI_BATT_TYPE_CMBAT:
115 error = acpi_cmbat_get_battinfo(battdesc.phys_unit,
128 acpi_battery_get_info_expire(void)
131 return (acpi_battery_info_expire);
135 acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg)
139 union acpi_battery_ioctl_arg *ioctl_arg;
141 ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
144 case ACPIIO_BATT_GET_UNITS:
145 *(int *)addr = acpi_battery_get_units();
148 case ACPIIO_BATT_GET_BATTDESC:
149 logical_unit = ioctl_arg->unit;
150 error = acpi_battery_get_battdesc(logical_unit, &ioctl_arg->battdesc);
153 case ACPIIO_BATT_GET_BATTINFO:
154 logical_unit = ioctl_arg->unit;
155 error = acpi_battery_get_battinfo(logical_unit,
156 &ioctl_arg->battinfo);
168 acpi_battery_sysctl(SYSCTL_HANDLER_ARGS)
173 acpi_battery_get_battinfo(-1, &acpi_battery_battinfo);
174 val = *(u_int *)oidp->oid_arg1;
175 error = sysctl_handle_int(oidp, &val, 0, req);
180 acpi_battery_init(void)
183 struct acpi_softc *sc;
186 if ((dev = devclass_get_device(devclass_find("acpi"), 0)) == NULL) {
189 if ((sc = device_get_softc(dev)) == NULL) {
195 TAILQ_INIT(&acpi_batteries);
196 acpi_batteries_initted = 1;
198 if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_UNITS,
199 acpi_battery_ioctl, NULL)) != 0) {
202 if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTDESC,
203 acpi_battery_ioctl, NULL)) != 0) {
206 if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTINFO,
207 acpi_battery_ioctl, NULL)) != 0) {
211 sysctl_ctx_init(&sc->acpi_battery_sysctl_ctx);
212 sc->acpi_battery_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_battery_sysctl_ctx,
213 SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
214 OID_AUTO, "battery", CTLFLAG_RD, 0, "");
215 SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx,
216 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
217 OID_AUTO, "life", CTLTYPE_INT | CTLFLAG_RD,
218 &acpi_battery_battinfo.cap, 0, acpi_battery_sysctl, "I", "");
219 SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx,
220 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
221 OID_AUTO, "time", CTLTYPE_INT | CTLFLAG_RD,
222 &acpi_battery_battinfo.min, 0, acpi_battery_sysctl, "I", "");
223 SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx,
224 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
225 OID_AUTO, "state", CTLTYPE_INT | CTLFLAG_RD,
226 &acpi_battery_battinfo.state, 0, acpi_battery_sysctl, "I", "");
227 SYSCTL_ADD_INT(&sc->acpi_battery_sysctl_ctx,
228 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
229 OID_AUTO, "units", CTLFLAG_RD, &acpi_batteries_units, 0, "");
230 SYSCTL_ADD_INT(&sc->acpi_battery_sysctl_ctx,
231 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
232 OID_AUTO, "info_expire", CTLFLAG_RD | CTLFLAG_RW,
233 &acpi_battery_info_expire, 0, "");
239 acpi_battery_register(int type, int phys_unit)
242 struct acpi_batteries *bp;
245 if ((bp = malloc(sizeof(*bp), M_ACPIBATT, M_NOWAIT)) == NULL) {
249 bp->battdesc.type = type;
250 bp->battdesc.phys_unit = phys_unit;
251 if (acpi_batteries_initted == 0) {
252 if ((error = acpi_battery_init()) != 0) {
253 free(bp, M_ACPIBATT);
258 TAILQ_INSERT_TAIL(&acpi_batteries, bp, link);
259 acpi_batteries_units++;