acpi_battery.c revision 148352
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 148352 2005-07-23 19:36:00Z njl $");
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 "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);
147    bi = malloc(devcount * sizeof(*bi), M_TEMP, M_WAITOK);
148    bif = malloc(sizeof(*bif), M_TEMP, M_WAITOK);
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	total_cap += bi[i].cap;
229    }
230
231    /*
232     * Return total battery percent and time remaining.  If there are
233     * no valid batteries, report values as unknown.
234     */
235    if (valid_units > 0) {
236	if (dev == NULL) {
237	    battinfo->cap = total_cap / valid_units;
238	    battinfo->min = total_min;
239	    battinfo->state = batt_stat;
240	    battinfo->rate = valid_rate;
241	} else {
242	    battinfo->cap = bi[dev_idx].cap;
243	    battinfo->min = bi[dev_idx].min;
244	    battinfo->state = bi[dev_idx].state;
245	    battinfo->rate = bst[dev_idx].rate;
246	}
247    } else
248	acpi_reset_battinfo(battinfo);
249
250    error = 0;
251
252out:
253    if (bi)
254	free(bi, M_TEMP);
255    if (bif)
256	free(bif, M_TEMP);
257    if (bst)
258	free(bst, M_TEMP);
259    return (error);
260}
261
262static void
263acpi_reset_battinfo(struct acpi_battinfo *info)
264{
265    info->cap = -1;
266    info->min = -1;
267    info->state = ACPI_BATT_STAT_NOT_PRESENT;
268    info->rate = -1;
269}
270
271static int
272acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg)
273{
274    union acpi_battery_ioctl_arg *ioctl_arg;
275    int error, unit;
276    device_t dev;
277
278    error = ENXIO;
279    ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
280    unit = ioctl_arg->unit;
281    if (unit != ACPI_BATTERY_ALL_UNITS)
282	dev = devclass_get_device(devclass_find("battery"), unit);
283    else
284	dev = NULL;
285
286    /*
287     * No security check required: information retrieval only.  If
288     * new functions are added here, a check might be required.
289     */
290    switch (cmd) {
291    case ACPIIO_BATT_GET_UNITS:
292	*(int *)addr = acpi_battery_get_units();
293	break;
294    case ACPIIO_BATT_GET_BATTINFO:
295	if (dev != NULL || unit == ACPI_BATTERY_ALL_UNITS)
296	    error = acpi_battery_get_battinfo(dev, &ioctl_arg->battinfo);
297	break;
298    case ACPIIO_BATT_GET_BIF:
299	if (dev != NULL)
300	    error = ACPI_BATT_GET_INFO(dev, &ioctl_arg->bif);
301	break;
302    case ACPIIO_BATT_GET_BST:
303	if (dev != NULL)
304	    error = ACPI_BATT_GET_STATUS(dev, &ioctl_arg->bst);
305	break;
306    default:
307	error = EINVAL;
308    }
309
310    return (error);
311}
312
313static int
314acpi_battery_sysctl(SYSCTL_HANDLER_ARGS)
315{
316    int val, error;
317
318    acpi_battery_get_battinfo(NULL, &acpi_battery_battinfo);
319    val = *(u_int *)oidp->oid_arg1;
320    error = sysctl_handle_int(oidp, &val, 0, req);
321    return (error);
322}
323
324static int
325acpi_battery_units_sysctl(SYSCTL_HANDLER_ARGS)
326{
327    int count, error;
328
329    count = acpi_battery_get_units();
330    error = sysctl_handle_int(oidp, &count, 0, req);
331    return (error);
332}
333
334static int
335acpi_battery_init(void)
336{
337    struct acpi_softc	*sc;
338    device_t		 dev;
339    int	 		 error;
340
341    ACPI_SERIAL_ASSERT(battery);
342
343    error = ENXIO;
344    dev = devclass_get_device(devclass_find("acpi"), 0);
345    if (dev == NULL)
346	goto out;
347    sc = device_get_softc(dev);
348
349    error = acpi_register_ioctl(ACPIIO_BATT_GET_UNITS, acpi_battery_ioctl,
350	NULL);
351    if (error != 0)
352	goto out;
353    error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTINFO, acpi_battery_ioctl,
354	NULL);
355    if (error != 0)
356	goto out;
357    error = acpi_register_ioctl(ACPIIO_BATT_GET_BIF, acpi_battery_ioctl, NULL);
358    if (error != 0)
359	goto out;
360    error = acpi_register_ioctl(ACPIIO_BATT_GET_BST, acpi_battery_ioctl, NULL);
361    if (error != 0)
362	goto out;
363
364    sysctl_ctx_init(&acpi_battery_sysctl_ctx);
365    acpi_battery_sysctl_tree = SYSCTL_ADD_NODE(&acpi_battery_sysctl_ctx,
366	SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO, "battery", CTLFLAG_RD,
367	0, "");
368    SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
369	SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
370	OID_AUTO, "life", CTLTYPE_INT | CTLFLAG_RD,
371	&acpi_battery_battinfo.cap, 0, acpi_battery_sysctl, "I", "");
372    SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
373	SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
374	OID_AUTO, "time", CTLTYPE_INT | CTLFLAG_RD,
375	&acpi_battery_battinfo.min, 0, acpi_battery_sysctl, "I", "");
376    SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
377	SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
378	OID_AUTO, "state", CTLTYPE_INT | CTLFLAG_RD,
379	&acpi_battery_battinfo.state, 0, acpi_battery_sysctl, "I", "");
380    SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
381	SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
382	OID_AUTO, "units", CTLTYPE_INT | CTLFLAG_RD,
383	NULL, 0, acpi_battery_units_sysctl, "I", "");
384    SYSCTL_ADD_INT(&acpi_battery_sysctl_ctx,
385	SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
386	OID_AUTO, "info_expire", CTLFLAG_RD | CTLFLAG_RW,
387	&acpi_battery_info_expire, 0, "");
388
389    acpi_batteries_initted = TRUE;
390
391out:
392    if (error != 0) {
393	acpi_deregister_ioctl(ACPIIO_BATT_GET_UNITS, acpi_battery_ioctl);
394	acpi_deregister_ioctl(ACPIIO_BATT_GET_BATTINFO, acpi_battery_ioctl);
395	acpi_deregister_ioctl(ACPIIO_BATT_GET_BIF, acpi_battery_ioctl);
396	acpi_deregister_ioctl(ACPIIO_BATT_GET_BST, acpi_battery_ioctl);
397    }
398    return (error);
399}
400