Deleted Added
sdiff udiff text old ( 148968 ) new ( 150003 )
full compact
1/*-
2 * Copyright (c) 2005 Nate Lawson
3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi_battery.c 150003 2005-09-11 18:39:03Z obrien $");
30
31#include "opt_acpi.h"
32#include <sys/param.h>
33#include <sys/kernel.h>
34#include <sys/malloc.h>
35#include <sys/bus.h>
36#include <sys/ioccom.h>
37#include <sys/sysctl.h>
38
39#include <contrib/dev/acpica/acpi.h>
40#include <dev/acpica/acpivar.h>
41#include <dev/acpica/acpiio.h>
42
43/* Default seconds before re-sampling the battery state. */
44#define ACPI_BATTERY_INFO_EXPIRE 5
45
46static int acpi_batteries_initted;
47static int acpi_battery_info_expire = ACPI_BATTERY_INFO_EXPIRE;
48static struct acpi_battinfo acpi_battery_battinfo;
49static struct sysctl_ctx_list acpi_battery_sysctl_ctx;
50static struct sysctl_oid *acpi_battery_sysctl_tree;
51
52ACPI_SERIAL_DECL(battery, "ACPI generic battery");
53
54static void acpi_reset_battinfo(struct acpi_battinfo *info);
55static int acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg);
56static int acpi_battery_sysctl(SYSCTL_HANDLER_ARGS);
57static int acpi_battery_units_sysctl(SYSCTL_HANDLER_ARGS);
58static int acpi_battery_init(void);
59
60int
61acpi_battery_register(device_t dev)
62{
63 int error;
64
65 error = 0;
66 ACPI_SERIAL_BEGIN(battery);
67 if (!acpi_batteries_initted)
68 error = acpi_battery_init();
69 ACPI_SERIAL_END(battery);
70 return (error);
71}
72
73int
74acpi_battery_remove(device_t dev)
75{
76
77 return (0);
78}
79
80int
81acpi_battery_get_units(void)
82{
83 devclass_t batt_dc;
84
85 batt_dc = devclass_find("battery");
86 if (batt_dc == NULL)
87 return (0);
88 return (devclass_get_count(batt_dc));
89}
90
91int
92acpi_battery_get_info_expire(void)
93{
94
95 return (acpi_battery_info_expire);
96}
97
98/* Check _BST results for validity. */
99int
100acpi_battery_bst_valid(struct acpi_bst *bst)
101{
102 if (bst->state >= ACPI_BATT_STAT_MAX || bst->cap == 0xffffffff ||
103 bst->volt == 0xffffffff)
104 return (FALSE);
105 else
106 return (TRUE);
107}
108
109/* Check _BIF results for validity. */
110int
111acpi_battery_bif_valid(struct acpi_bif *bif)
112{
113 if (bif->lfcap == 0)
114 return (FALSE);
115 else
116 return (TRUE);
117}
118
119/* Get info about one or all batteries. */
120int
121acpi_battery_get_battinfo(device_t dev, struct acpi_battinfo *battinfo)
122{
123 int batt_stat, devcount, dev_idx, error, i;
124 int total_cap, total_min, valid_rate, valid_units;
125 devclass_t batt_dc;
126 device_t batt_dev;
127 struct acpi_bst *bst;
128 struct acpi_bif *bif;
129 struct acpi_battinfo *bi;
130
131 /*
132 * Get the battery devclass and number of devices. If there are none
133 * or error, return immediately.
134 */
135 batt_dc = devclass_find("battery");
136 if (batt_dc == NULL)
137 return (ENXIO);
138 devcount = devclass_get_count(batt_dc);
139 if (devcount == 0)
140 return (ENXIO);
141
142 /*
143 * Allocate storage for all _BST data, their derived battinfo data,
144 * and the current battery's _BIF data.
145 */
146 bst = malloc(devcount * sizeof(*bst), M_TEMP, M_WAITOK | M_ZERO);
147 bi = malloc(devcount * sizeof(*bi), M_TEMP, M_WAITOK | M_ZERO);
148 bif = malloc(sizeof(*bif), M_TEMP, M_WAITOK | M_ZERO);
149
150 /*
151 * Pass 1: for each battery that is present and valid, get its status,
152 * calculate percent capacity remaining, and sum all the current
153 * discharge rates.
154 */
155 dev_idx = -1;
156 batt_stat = valid_rate = valid_units = 0;
157 for (i = 0; i < devcount; i++) {
158 /* Find the device. If it disappeared, the user can try again. */
159 batt_dev = devclass_get_device(batt_dc, i);
160 if (batt_dev == NULL) {
161 error = ENOMEM;
162 goto out;
163 }
164
165 /* Default info for every battery is "not present". */
166 acpi_reset_battinfo(&bi[i]);
167
168 /* If examining a specific battery and this is it, record its index. */
169 if (dev != NULL && dev == batt_dev)
170 dev_idx = i;
171
172 /* Be sure we can get various info from the battery. */
173 if (!acpi_BatteryIsPresent(batt_dev) ||
174 ACPI_BATT_GET_STATUS(batt_dev, &bst[i]) != 0 ||
175 ACPI_BATT_GET_INFO(batt_dev, bif) != 0)
176 continue;
177
178 /* If a battery is not installed, we sometimes get strange values. */
179 if (!acpi_battery_bst_valid(&bst[i]) ||
180 !acpi_battery_bif_valid(bif))
181 continue;
182
183 /* Record state and calculate percent capacity remaining. */
184 valid_units++;
185 batt_stat |= bst[i].state;
186 bi[i].state = bst[i].state;
187 bi[i].cap = 100 * bst[i].cap / bif->lfcap;
188
189 /*
190 * Some laptops report the "design-capacity" instead of the
191 * "real-capacity" when the battery is fully charged. That breaks
192 * the above arithmetic as it needs to be 100% maximum.
193 */
194 if (bi[i].cap > 100)
195 bi[i].cap = 100;
196
197 /*
198 * On systems with more than one battery, they may get used
199 * sequentially, thus bst.rate may only signify the one currently
200 * in use. For the remaining batteries, bst.rate will be zero,
201 * which makes it impossible to calculate the total remaining time.
202 * Therefore, we sum the bst.rate for batteries in the discharging
203 * state and use the sum to calculate the total remaining time.
204 */
205 if (bst[i].rate > 0 && (bst[i].state & ACPI_BATT_STAT_DISCHARG))
206 valid_rate += bst[i].rate;
207 }
208
209 /* If the caller asked for a device but we didn't find it, error. */
210 if (dev != NULL && dev_idx < 0) {
211 error = ENXIO;
212 goto out;
213 }
214
215 /* Pass 2: calculate capacity and remaining time for all batteries. */
216 total_cap = total_min = 0;
217 for (i = 0; i < devcount; i++) {
218 /*
219 * If any batteries are discharging, use the sum of the bst.rate
220 * values. Otherwise, we are on AC power, and there is infinite
221 * time remaining for this battery until we go offline.
222 */
223 if (valid_rate > 0)
224 bi[i].min = 60 * bst[i].cap / valid_rate;
225 else
226 bi[i].min = 0;
227 total_min += bi[i].min;
228
229 /* If this battery is not present, don't use its capacity. */
230 if (bi[i].cap != -1)
231 total_cap += bi[i].cap;
232 }
233
234 /*
235 * Return total battery percent and time remaining. If there are
236 * no valid batteries, report values as unknown.
237 */
238 if (valid_units > 0) {
239 if (dev == NULL) {
240 battinfo->cap = total_cap / valid_units;
241 battinfo->min = total_min;
242 battinfo->state = batt_stat;
243 battinfo->rate = valid_rate;
244 } else {
245 battinfo->cap = bi[dev_idx].cap;
246 battinfo->min = bi[dev_idx].min;
247 battinfo->state = bi[dev_idx].state;
248 battinfo->rate = bst[dev_idx].rate;
249 }
250
251 /*
252 * If the queried battery has no discharge rate or is charging,
253 * report that we don't know the remaining time.
254 */
255 if (valid_rate == 0 || (battinfo->state & ACPI_BATT_STAT_CHARGING))
256 battinfo->min = -1;
257 } else
258 acpi_reset_battinfo(battinfo);
259
260 error = 0;
261
262out:
263 if (bi)
264 free(bi, M_TEMP);
265 if (bif)
266 free(bif, M_TEMP);
267 if (bst)
268 free(bst, M_TEMP);
269 return (error);
270}
271
272static void
273acpi_reset_battinfo(struct acpi_battinfo *info)
274{
275 info->cap = -1;
276 info->min = -1;
277 info->state = ACPI_BATT_STAT_NOT_PRESENT;
278 info->rate = -1;
279}
280
281static int
282acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg)
283{
284 union acpi_battery_ioctl_arg *ioctl_arg;
285 int error, unit;
286 device_t dev;
287
288 error = ENXIO;
289 ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
290 unit = ioctl_arg->unit;
291 if (unit != ACPI_BATTERY_ALL_UNITS)
292 dev = devclass_get_device(devclass_find("battery"), unit);
293 else
294 dev = NULL;
295
296 /*
297 * No security check required: information retrieval only. If
298 * new functions are added here, a check might be required.
299 */
300 switch (cmd) {
301 case ACPIIO_BATT_GET_UNITS:
302 *(int *)addr = acpi_battery_get_units();
303 break;
304 case ACPIIO_BATT_GET_BATTINFO:
305 if (dev != NULL || unit == ACPI_BATTERY_ALL_UNITS)
306 error = acpi_battery_get_battinfo(dev, &ioctl_arg->battinfo);
307 break;
308 case ACPIIO_BATT_GET_BIF:
309 if (dev != NULL)
310 error = ACPI_BATT_GET_INFO(dev, &ioctl_arg->bif);
311 break;
312 case ACPIIO_BATT_GET_BST:
313 if (dev != NULL)
314 error = ACPI_BATT_GET_STATUS(dev, &ioctl_arg->bst);
315 break;
316 default:
317 error = EINVAL;
318 }
319
320 return (error);
321}
322
323static int
324acpi_battery_sysctl(SYSCTL_HANDLER_ARGS)
325{
326 int val, error;
327
328 acpi_battery_get_battinfo(NULL, &acpi_battery_battinfo);
329 val = *(u_int *)oidp->oid_arg1;
330 error = sysctl_handle_int(oidp, &val, 0, req);
331 return (error);
332}
333
334static int
335acpi_battery_units_sysctl(SYSCTL_HANDLER_ARGS)
336{
337 int count, error;
338
339 count = acpi_battery_get_units();
340 error = sysctl_handle_int(oidp, &count, 0, req);
341 return (error);
342}
343
344static int
345acpi_battery_init(void)
346{
347 struct acpi_softc *sc;
348 device_t dev;
349 int error;
350
351 ACPI_SERIAL_ASSERT(battery);
352
353 error = ENXIO;
354 dev = devclass_get_device(devclass_find("acpi"), 0);
355 if (dev == NULL)
356 goto out;
357 sc = device_get_softc(dev);
358
359 error = acpi_register_ioctl(ACPIIO_BATT_GET_UNITS, acpi_battery_ioctl,
360 NULL);
361 if (error != 0)
362 goto out;
363 error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTINFO, acpi_battery_ioctl,
364 NULL);
365 if (error != 0)
366 goto out;
367 error = acpi_register_ioctl(ACPIIO_BATT_GET_BIF, acpi_battery_ioctl, NULL);
368 if (error != 0)
369 goto out;
370 error = acpi_register_ioctl(ACPIIO_BATT_GET_BST, acpi_battery_ioctl, NULL);
371 if (error != 0)
372 goto out;
373
374 sysctl_ctx_init(&acpi_battery_sysctl_ctx);
375 acpi_battery_sysctl_tree = SYSCTL_ADD_NODE(&acpi_battery_sysctl_ctx,
376 SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO, "battery", CTLFLAG_RD,
377 0, "battery status and info");
378 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
379 SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
380 OID_AUTO, "life", CTLTYPE_INT | CTLFLAG_RD,
381 &acpi_battery_battinfo.cap, 0, acpi_battery_sysctl, "I",
382 "percent capacity remaining");
383 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
384 SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
385 OID_AUTO, "time", CTLTYPE_INT | CTLFLAG_RD,
386 &acpi_battery_battinfo.min, 0, acpi_battery_sysctl, "I",
387 "remaining time in minutes");
388 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
389 SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
390 OID_AUTO, "state", CTLTYPE_INT | CTLFLAG_RD,
391 &acpi_battery_battinfo.state, 0, acpi_battery_sysctl, "I",
392 "current status flags");
393 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
394 SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
395 OID_AUTO, "units", CTLTYPE_INT | CTLFLAG_RD,
396 NULL, 0, acpi_battery_units_sysctl, "I", "number of batteries");
397 SYSCTL_ADD_INT(&acpi_battery_sysctl_ctx,
398 SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
399 OID_AUTO, "info_expire", CTLFLAG_RD | CTLFLAG_RW,
400 &acpi_battery_info_expire, 0,
401 "time in seconds until info is refreshed");
402
403 acpi_batteries_initted = TRUE;
404
405out:
406 if (error != 0) {
407 acpi_deregister_ioctl(ACPIIO_BATT_GET_UNITS, acpi_battery_ioctl);
408 acpi_deregister_ioctl(ACPIIO_BATT_GET_BATTINFO, acpi_battery_ioctl);
409 acpi_deregister_ioctl(ACPIIO_BATT_GET_BIF, acpi_battery_ioctl);
410 acpi_deregister_ioctl(ACPIIO_BATT_GET_BST, acpi_battery_ioctl);
411 }
412 return (error);
413}