dma: create spool files mode 660
[dragonfly.git] / sys / dev / acpica5 / acpi_battery.c
blob0122c81a1ac14e4ad6ec65b44a305c0da24befb7
1 /*-
2 * Copyright (c) 2005 Nate Lawson
3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4 * All rights reserved.
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.
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.
27 * $FreeBSD: src/sys/dev/acpica/acpi_battery.c,v 1.26 2007/11/20 18:35:36 jkim Exp $
28 * $DragonFly: src/sys/dev/acpica5/acpi_battery.c,v 1.4 2008/09/29 06:59:45 hasso Exp $
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/sysctl.h>
37 #include <sys/thread2.h>
39 #include "acpi.h"
40 #include <dev/acpica5/acpivar.h>
41 #include <dev/acpica5/acpiio.h>
43 /* Default seconds before re-sampling the battery state. */
44 #define ACPI_BATTERY_INFO_EXPIRE 5
46 static int acpi_batteries_initted;
47 static int acpi_battery_info_expire = ACPI_BATTERY_INFO_EXPIRE;
48 static struct acpi_battinfo acpi_battery_battinfo;
49 static struct sysctl_ctx_list acpi_battery_sysctl_ctx;
50 static struct sysctl_oid *acpi_battery_sysctl_tree;
52 static void acpi_reset_battinfo(struct acpi_battinfo *info);
53 static void acpi_battery_clean_str(char *str, int len);
54 static device_t acpi_battery_find_dev(u_int logical_unit);
55 static int acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg);
56 static int acpi_battery_sysctl(SYSCTL_HANDLER_ARGS);
57 static int acpi_battery_units_sysctl(SYSCTL_HANDLER_ARGS);
58 static int acpi_battery_init(void);
60 int
61 acpi_battery_register(device_t dev)
63 int error;
65 error = 0;
66 crit_enter();
67 if (!acpi_batteries_initted)
68 error = acpi_battery_init();
69 crit_exit();
70 return (error);
73 int
74 acpi_battery_remove(device_t dev)
76 return (0);
79 int
80 acpi_battery_get_units(void)
82 devclass_t batt_dc;
84 batt_dc = devclass_find("battery");
85 if (batt_dc == NULL)
86 return (0);
87 return (devclass_get_count(batt_dc));
90 int
91 acpi_battery_get_info_expire(void)
93 return (acpi_battery_info_expire);
96 /* Check _BST results for validity. */
97 int
98 acpi_battery_bst_valid(struct acpi_bst *bst)
100 return (bst->state < ACPI_BATT_STAT_MAX && bst->cap != ACPI_BATT_UNKNOWN &&
101 bst->volt != ACPI_BATT_UNKNOWN);
104 /* Check _BIF results for validity. */
106 acpi_battery_bif_valid(struct acpi_bif *bif)
108 return (bif->lfcap != 0);
111 /* Get info about one or all batteries. */
113 acpi_battery_get_battinfo(device_t dev, struct acpi_battinfo *battinfo)
115 int batt_stat, devcount, dev_idx, error, i;
116 int total_cap, total_min, valid_rate, valid_units;
117 devclass_t batt_dc;
118 device_t batt_dev;
119 struct acpi_bst *bst;
120 struct acpi_bif *bif;
121 struct acpi_battinfo *bi;
124 * Get the battery devclass and max unit for battery devices. If there
125 * are none or error, return immediately.
127 batt_dc = devclass_find("battery");
128 if (batt_dc == NULL)
129 return (ENXIO);
130 devcount = devclass_get_maxunit(batt_dc);
131 if (devcount == 0)
132 return (ENXIO);
135 * Allocate storage for all _BST data, their derived battinfo data,
136 * and the current battery's _BIF data.
138 bst = kmalloc(devcount * sizeof(*bst), M_TEMP, M_WAITOK | M_ZERO);
139 bi = kmalloc(devcount * sizeof(*bi), M_TEMP, M_WAITOK | M_ZERO);
140 bif = kmalloc(sizeof(*bif), M_TEMP, M_WAITOK | M_ZERO);
143 * Pass 1: for each battery that is present and valid, get its status,
144 * calculate percent capacity remaining, and sum all the current
145 * discharge rates.
147 dev_idx = -1;
148 batt_stat = valid_rate = valid_units = 0;
149 for (i = 0; i < devcount; i++) {
150 /* Default info for every battery is "not present". */
151 acpi_reset_battinfo(&bi[i]);
154 * Find the device. Since devcount is in terms of max units, this
155 * may be a sparse array so skip devices that aren't present.
157 batt_dev = devclass_get_device(batt_dc, i);
158 if (batt_dev == NULL)
159 continue;
161 /* If examining a specific battery and this is it, record its index. */
162 if (dev != NULL && dev == batt_dev)
163 dev_idx = i;
166 * Be sure we can get various info from the battery. Note that
167 * acpi_BatteryIsPresent() is not enough because smart batteries only
168 * return that the device is present.
170 if (!acpi_BatteryIsPresent(batt_dev) ||
171 ACPI_BATT_GET_STATUS(batt_dev, &bst[i]) != 0 ||
172 ACPI_BATT_GET_INFO(batt_dev, bif) != 0)
173 continue;
175 /* If a battery is not installed, we sometimes get strange values. */
176 if (!acpi_battery_bst_valid(&bst[i]) ||
177 !acpi_battery_bif_valid(bif))
178 continue;
181 * Record current state. If both charging and discharging are set,
182 * ignore the charging flag.
184 valid_units++;
185 if ((bst[i].state & ACPI_BATT_STAT_DISCHARG) != 0)
186 bst[i].state &= ~ACPI_BATT_STAT_CHARGING;
187 batt_stat |= bst[i].state;
188 bi[i].state = bst[i].state;
191 * If the battery info is in terms of mA, convert to mW by
192 * multiplying by the design voltage. If the design voltage
193 * is 0 (due to some error reading the battery), skip this
194 * conversion.
196 if (bif->units == ACPI_BIF_UNITS_MA && bif->dvol != 0) {
197 bst[i].rate = (bst[i].rate * bif->dvol) / 1000;
198 bst[i].cap = (bst[i].cap * bif->dvol) / 1000;
199 bif->lfcap = (bif->lfcap * bif->dvol) / 1000;
202 /* Calculate percent capacity remaining. */
203 bi[i].cap = (100 * bst[i].cap) / bif->lfcap;
206 * Some laptops report the "design-capacity" instead of the
207 * "real-capacity" when the battery is fully charged. That breaks
208 * the above arithmetic as it needs to be 100% maximum.
210 if (bi[i].cap > 100)
211 bi[i].cap = 100;
214 * On systems with more than one battery, they may get used
215 * sequentially, thus bst.rate may only signify the one currently
216 * in use. For the remaining batteries, bst.rate will be zero,
217 * which makes it impossible to calculate the total remaining time.
218 * Therefore, we sum the bst.rate for batteries in the discharging
219 * state and use the sum to calculate the total remaining time.
221 if (bst[i].rate != ACPI_BATT_UNKNOWN &&
222 (bst[i].state & ACPI_BATT_STAT_DISCHARG) != 0)
223 valid_rate += bst[i].rate;
226 /* If the caller asked for a device but we didn't find it, error. */
227 if (dev != NULL && dev_idx == -1) {
228 error = ENXIO;
229 goto out;
232 /* Pass 2: calculate capacity and remaining time for all batteries. */
233 total_cap = total_min = 0;
234 for (i = 0; i < devcount; i++) {
236 * If any batteries are discharging, use the sum of the bst.rate
237 * values. Otherwise, we are on AC power, and there is infinite
238 * time remaining for this battery until we go offline.
240 if (valid_rate > 0)
241 bi[i].min = (60 * bst[i].cap) / valid_rate;
242 else
243 bi[i].min = 0;
244 total_min += bi[i].min;
246 /* If this battery is not present, don't use its capacity. */
247 if (bi[i].cap != -1)
248 total_cap += bi[i].cap;
252 * Return total battery percent and time remaining. If there are
253 * no valid batteries, report values as unknown.
255 if (valid_units > 0) {
256 if (dev == NULL) {
257 battinfo->cap = total_cap / valid_units;
258 battinfo->min = total_min;
259 battinfo->state = batt_stat;
260 battinfo->rate = valid_rate;
261 } else {
262 battinfo->cap = bi[dev_idx].cap;
263 battinfo->min = bi[dev_idx].min;
264 battinfo->state = bi[dev_idx].state;
265 battinfo->rate = bst[dev_idx].rate;
269 * If the queried battery has no discharge rate or is charging,
270 * report that we don't know the remaining time.
272 if (valid_rate == 0 || (battinfo->state & ACPI_BATT_STAT_CHARGING))
273 battinfo->min = -1;
274 } else
275 acpi_reset_battinfo(battinfo);
277 error = 0;
279 out:
280 if (bi)
281 kfree(bi, M_TEMP);
282 if (bif)
283 kfree(bif, M_TEMP);
284 if (bst)
285 kfree(bst, M_TEMP);
286 return (error);
289 static void
290 acpi_reset_battinfo(struct acpi_battinfo *info)
292 info->cap = -1;
293 info->min = -1;
294 info->state = ACPI_BATT_STAT_NOT_PRESENT;
295 info->rate = -1;
298 /* Make string printable, removing invalid chars. */
299 static void
300 acpi_battery_clean_str(char *str, int len)
302 int i;
304 for (i = 0; i < len && *str != '\0'; i++, str++) {
305 if (!isprint(*str))
306 *str = '?';
309 /* NUL-terminate the string if we reached the end. */
310 if (i == len)
311 *str = '\0';
315 * The battery interface deals with devices and methods but userland
316 * expects a logical unit number. Convert a logical unit to a device_t.
318 static device_t
319 acpi_battery_find_dev(u_int logical_unit)
321 int found_unit, i, maxunit;
322 device_t dev;
323 devclass_t batt_dc;
325 dev = NULL;
326 found_unit = 0;
327 batt_dc = devclass_find("battery");
328 maxunit = devclass_get_maxunit(batt_dc);
329 for (i = 0; i < maxunit; i++) {
330 dev = devclass_get_device(batt_dc, i);
331 if (dev == NULL)
332 continue;
333 if (logical_unit == found_unit)
334 break;
335 found_unit++;
336 dev = NULL;
339 return (dev);
342 static int
343 acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg)
345 union acpi_battery_ioctl_arg *ioctl_arg;
346 int error, unit;
347 device_t dev;
349 /* For commands that use the ioctl_arg struct, validate it first. */
350 error = ENXIO;
351 unit = 0;
352 dev = NULL;
353 ioctl_arg = NULL;
354 if (IOCPARM_LEN(cmd) == sizeof(*ioctl_arg)) {
355 ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
356 unit = ioctl_arg->unit;
357 if (unit != ACPI_BATTERY_ALL_UNITS)
358 dev = acpi_battery_find_dev(unit);
362 * No security check required: information retrieval only. If
363 * new functions are added here, a check might be required.
365 switch (cmd) {
366 case ACPIIO_BATT_GET_UNITS:
367 *(int *)addr = acpi_battery_get_units();
368 error = 0;
369 break;
370 case ACPIIO_BATT_GET_BATTINFO:
371 if (dev != NULL || unit == ACPI_BATTERY_ALL_UNITS) {
372 bzero(&ioctl_arg->battinfo, sizeof(ioctl_arg->battinfo));
373 error = acpi_battery_get_battinfo(dev, &ioctl_arg->battinfo);
375 break;
376 case ACPIIO_BATT_GET_BIF:
377 if (dev != NULL) {
378 bzero(&ioctl_arg->bif, sizeof(ioctl_arg->bif));
379 error = ACPI_BATT_GET_INFO(dev, &ioctl_arg->bif);
382 * Remove invalid characters. Perhaps this should be done
383 * within a convenience function so all callers get the
384 * benefit.
386 acpi_battery_clean_str(ioctl_arg->bif.model,
387 sizeof(ioctl_arg->bif.model));
388 acpi_battery_clean_str(ioctl_arg->bif.serial,
389 sizeof(ioctl_arg->bif.serial));
390 acpi_battery_clean_str(ioctl_arg->bif.type,
391 sizeof(ioctl_arg->bif.type));
392 acpi_battery_clean_str(ioctl_arg->bif.oeminfo,
393 sizeof(ioctl_arg->bif.oeminfo));
395 break;
396 case ACPIIO_BATT_GET_BST:
397 if (dev != NULL) {
398 bzero(&ioctl_arg->bst, sizeof(ioctl_arg->bst));
399 error = ACPI_BATT_GET_STATUS(dev, &ioctl_arg->bst);
401 break;
402 default:
403 error = EINVAL;
406 return (error);
409 static int
410 acpi_battery_sysctl(SYSCTL_HANDLER_ARGS)
412 int val, error;
414 acpi_battery_get_battinfo(NULL, &acpi_battery_battinfo);
415 val = *(u_int *)oidp->oid_arg1;
416 error = sysctl_handle_int(oidp, &val, 0, req);
417 return (error);
420 static int
421 acpi_battery_units_sysctl(SYSCTL_HANDLER_ARGS)
423 int count, error;
425 count = acpi_battery_get_units();
426 error = sysctl_handle_int(oidp, &count, 0, req);
427 return (error);
430 static int
431 acpi_battery_init(void)
433 struct acpi_softc *sc;
434 device_t dev;
435 int error;
437 error = ENXIO;
438 dev = devclass_get_device(devclass_find("acpi"), 0);
439 if (dev == NULL)
440 goto out;
441 sc = device_get_softc(dev);
443 error = acpi_register_ioctl(ACPIIO_BATT_GET_UNITS, acpi_battery_ioctl,
444 NULL);
445 if (error != 0)
446 goto out;
447 error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTINFO, acpi_battery_ioctl,
448 NULL);
449 if (error != 0)
450 goto out;
451 error = acpi_register_ioctl(ACPIIO_BATT_GET_BIF, acpi_battery_ioctl, NULL);
452 if (error != 0)
453 goto out;
454 error = acpi_register_ioctl(ACPIIO_BATT_GET_BST, acpi_battery_ioctl, NULL);
455 if (error != 0)
456 goto out;
458 sysctl_ctx_init(&acpi_battery_sysctl_ctx);
459 acpi_battery_sysctl_tree = SYSCTL_ADD_NODE(&acpi_battery_sysctl_ctx,
460 SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO, "battery", CTLFLAG_RD,
461 0, "battery status and info");
462 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
463 SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
464 OID_AUTO, "life", CTLTYPE_INT | CTLFLAG_RD,
465 &acpi_battery_battinfo.cap, 0, acpi_battery_sysctl, "I",
466 "percent capacity remaining");
467 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
468 SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
469 OID_AUTO, "time", CTLTYPE_INT | CTLFLAG_RD,
470 &acpi_battery_battinfo.min, 0, acpi_battery_sysctl, "I",
471 "remaining time in minutes");
472 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
473 SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
474 OID_AUTO, "state", CTLTYPE_INT | CTLFLAG_RD,
475 &acpi_battery_battinfo.state, 0, acpi_battery_sysctl, "I",
476 "current status flags");
477 SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
478 SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
479 OID_AUTO, "units", CTLTYPE_INT | CTLFLAG_RD,
480 NULL, 0, acpi_battery_units_sysctl, "I", "number of batteries");
481 SYSCTL_ADD_INT(&acpi_battery_sysctl_ctx,
482 SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
483 OID_AUTO, "info_expire", CTLFLAG_RW,
484 &acpi_battery_info_expire, 0,
485 "time in seconds until info is refreshed");
487 acpi_batteries_initted = TRUE;
489 out:
490 if (error != 0) {
491 acpi_deregister_ioctl(ACPIIO_BATT_GET_UNITS, acpi_battery_ioctl);
492 acpi_deregister_ioctl(ACPIIO_BATT_GET_BATTINFO, acpi_battery_ioctl);
493 acpi_deregister_ioctl(ACPIIO_BATT_GET_BIF, acpi_battery_ioctl);
494 acpi_deregister_ioctl(ACPIIO_BATT_GET_BST, acpi_battery_ioctl);
496 return (error);