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: releng/5.1/sys/dev/acpica/acpi_battery.c 110894 2003-02-15 01:46:22Z takawata $
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/malloc.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/ioccom.h>
37 #include <sys/reboot.h>
38 #include <sys/sysctl.h>
39 #include <sys/ctype.h>
40
41 #include <machine/clock.h>
42
43 #include <machine/resource.h>
44
45 #include "acpi.h"
46
47 #include <dev/acpica/acpivar.h>
48 #include <dev/acpica/acpiio.h>
49
50 MALLOC_DEFINE(M_ACPIBATT, "acpibatt", "ACPI generic battery data");
51
52 /*
53 * ACPI Battery Abstruction Layer.
54 */
55
56 struct acpi_batteries {
57 TAILQ_ENTRY(acpi_batteries) link;
58 struct acpi_battdesc battdesc;
59 };
60
61 static TAILQ_HEAD(,acpi_batteries) acpi_batteries;
62 static int acpi_batteries_initted = 0;
63 static int acpi_batteries_units = 0;
64 static int acpi_battery_info_expire = 5;
65 static struct acpi_battinfo acpi_battery_battinfo;
66
67 int
68 acpi_battery_get_units(void)
69 {
70
71 return (acpi_batteries_units);
72 }
73
74 int
75 acpi_battery_get_battdesc(int logical_unit, struct acpi_battdesc *battdesc)
76 {
77 int i;
78 struct acpi_batteries *bp;
79
80 if (logical_unit < 0 || logical_unit >= acpi_batteries_units) {
81 return (ENXIO);
82 }
83
84 i = 0;
85 TAILQ_FOREACH(bp, &acpi_batteries, link) {
86 if (logical_unit == i) {
87 battdesc->type = bp->battdesc.type;
88 battdesc->phys_unit = bp->battdesc.phys_unit;
89 return (0);
90 }
91 i++;
92 }
93
94 return (ENXIO);
95 }
96
97 int
98 acpi_battery_get_battinfo(int unit, struct acpi_battinfo *battinfo)
99 {
100 int error;
101 struct acpi_battdesc battdesc;
102
103 error = 0;
104 if (unit == -1) {
105 error = acpi_cmbat_get_battinfo(-1, battinfo);
106 goto out;
107 } else {
108 if ((error = acpi_battery_get_battdesc(unit, &battdesc)) != 0) {
109 goto out;
110 }
111 switch (battdesc.type) {
112 case ACPI_BATT_TYPE_CMBAT:
113 error = acpi_cmbat_get_battinfo(battdesc.phys_unit,
114 battinfo);
115 break;
116 default:
117 error = ENXIO;
118 break;
119 }
120 }
121 out:
122 return (error);
123 }
124
125 int
126 acpi_battery_get_info_expire(void)
127 {
128
129 return (acpi_battery_info_expire);
130 }
131
132 static int
133 acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg)
134 {
135 int error;
136 int logical_unit;
137 union acpi_battery_ioctl_arg *ioctl_arg;
138
139 ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
140 error = 0;
141
142 /*
143 * No security check required: information retrieval only. If
144 * new functions are added here, a check might be required.
145 */
146
147 switch (cmd) {
148 case ACPIIO_BATT_GET_UNITS:
149 *(int *)addr = acpi_battery_get_units();
150 break;
151
152 case ACPIIO_BATT_GET_BATTDESC:
153 logical_unit = ioctl_arg->unit;
154 error = acpi_battery_get_battdesc(logical_unit, &ioctl_arg->battdesc);
155 break;
156
157 case ACPIIO_BATT_GET_BATTINFO:
158 logical_unit = ioctl_arg->unit;
159 error = acpi_battery_get_battinfo(logical_unit,
160 &ioctl_arg->battinfo);
161 break;
162
163 default:
164 error = EINVAL;
165 break;
166 }
167
168 return (error);
169 }
170
171 static int
172 acpi_battery_sysctl(SYSCTL_HANDLER_ARGS)
173 {
174 int val;
175 int error;
176
177 acpi_battery_get_battinfo(-1, &acpi_battery_battinfo);
178 val = *(u_int *)oidp->oid_arg1;
179 error = sysctl_handle_int(oidp, &val, 0, req);
180 return (error);
181 }
182
183 static int
184 acpi_battery_init(void)
185 {
186 device_t dev;
187 struct acpi_softc *sc;
188 int error;
189
190 if ((dev = devclass_get_device(devclass_find("acpi"), 0)) == NULL) {
191 return (ENXIO);
192 }
193 if ((sc = device_get_softc(dev)) == NULL) {
194 return (ENXIO);
195 }
196
197 error = 0;
198
199 TAILQ_INIT(&acpi_batteries);
200 acpi_batteries_initted = 1;
201
202 if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_UNITS,
203 acpi_battery_ioctl, NULL)) != 0) {
204 return (error);
205 }
206 if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTDESC,
207 acpi_battery_ioctl, NULL)) != 0) {
208 return (error);
209 }
210 if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTINFO,
211 acpi_battery_ioctl, NULL)) != 0) {
212 return (error);
213 }
214
215 sysctl_ctx_init(&sc->acpi_battery_sysctl_ctx);
216 sc->acpi_battery_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_battery_sysctl_ctx,
217 SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
218 OID_AUTO, "battery", CTLFLAG_RD, 0, "");
219 SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx,
220 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
221 OID_AUTO, "life", CTLTYPE_INT | CTLFLAG_RD,
222 &acpi_battery_battinfo.cap, 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, "time", CTLTYPE_INT | CTLFLAG_RD,
226 &acpi_battery_battinfo.min, 0, acpi_battery_sysctl, "I", "");
227 SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx,
228 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
229 OID_AUTO, "state", CTLTYPE_INT | CTLFLAG_RD,
230 &acpi_battery_battinfo.state, 0, acpi_battery_sysctl, "I", "");
231 SYSCTL_ADD_INT(&sc->acpi_battery_sysctl_ctx,
232 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
233 OID_AUTO, "units", CTLFLAG_RD, &acpi_batteries_units, 0, "");
234 SYSCTL_ADD_INT(&sc->acpi_battery_sysctl_ctx,
235 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
236 OID_AUTO, "info_expire", CTLFLAG_RD | CTLFLAG_RW,
237 &acpi_battery_info_expire, 0, "");
238
239 return (error);
240 }
241
242 int
243 acpi_battery_register(int type, int phys_unit)
244 {
245 int error;
246 struct acpi_batteries *bp;
247
248 error = 0;
249 if ((bp = malloc(sizeof(*bp), M_ACPIBATT, M_NOWAIT)) == NULL) {
250 return(ENOMEM);
251 }
252
253 bp->battdesc.type = type;
254 bp->battdesc.phys_unit = phys_unit;
255 if (acpi_batteries_initted == 0) {
256 if ((error = acpi_battery_init()) != 0) {
257 free(bp, M_ACPIBATT);
258 return(error);
259 }
260 }
261
262 TAILQ_INSERT_TAIL(&acpi_batteries, bp, link);
263 acpi_batteries_units++;
264
265 return(0);
266 }
Cache object: 215fc9081ce9f041fdf745a3e82c91fc
|