xref: /freebsd/sys/dev/acpica/acpi_battery.c (revision 02e9120893770924227138ba49df1edb3896112a)
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 #include "opt_acpi.h"
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/bus.h>
34 #include <sys/ioccom.h>
35 #include <sys/sysctl.h>
36 
37 #include <contrib/dev/acpica/include/acpi.h>
38 
39 #include <dev/acpica/acpivar.h>
40 #include <dev/acpica/acpiio.h>
41 
42 /* Default seconds before re-sampling the battery state. */
43 #define	ACPI_BATTERY_INFO_EXPIRE	5
44 
45 static int	acpi_batteries_initialized;
46 static int	acpi_battery_info_expire = ACPI_BATTERY_INFO_EXPIRE;
47 static struct	acpi_battinfo	acpi_battery_battinfo;
48 static struct	sysctl_ctx_list	acpi_battery_sysctl_ctx;
49 static struct	sysctl_oid	*acpi_battery_sysctl_tree;
50 
51 ACPI_SERIAL_DECL(battery, "ACPI generic battery");
52 
53 static void acpi_reset_battinfo(struct acpi_battinfo *info);
54 static void acpi_battery_clean_str(char *str, int len);
55 static device_t acpi_battery_find_dev(u_int logical_unit);
56 static int acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg);
57 static int acpi_battery_sysctl(SYSCTL_HANDLER_ARGS);
58 static int acpi_battery_units_sysctl(SYSCTL_HANDLER_ARGS);
59 static int acpi_battery_init(void);
60 
61 int
62 acpi_battery_register(device_t dev)
63 {
64     int error;
65 
66     ACPI_SERIAL_BEGIN(battery);
67     error = acpi_battery_init();
68     ACPI_SERIAL_END(battery);
69 
70     return (error);
71 }
72 
73 int
74 acpi_battery_remove(device_t dev)
75 {
76 
77     return (0);
78 }
79 
80 int
81 acpi_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 
91 int
92 acpi_battery_get_info_expire(void)
93 {
94 
95     return (acpi_battery_info_expire);
96 }
97 
98 /* Check _BST results for validity. */
99 int
100 acpi_battery_bst_valid(struct acpi_bst *bst)
101 {
102 
103     return (bst->state != ACPI_BATT_STAT_NOT_PRESENT &&
104 	bst->cap != ACPI_BATT_UNKNOWN && bst->volt != ACPI_BATT_UNKNOWN);
105 }
106 
107 /* Check _BI[FX] results for validity. */
108 int
109 acpi_battery_bix_valid(struct acpi_bix *bix)
110 {
111 
112     return (bix->lfcap != 0);
113 }
114 
115 /* Get info about one or all batteries. */
116 int
117 acpi_battery_get_battinfo(device_t dev, struct acpi_battinfo *battinfo)
118 {
119     int	batt_stat, devcount, dev_idx, error, i;
120     int total_cap, total_lfcap, total_min, valid_rate, valid_units;
121     devclass_t batt_dc;
122     device_t batt_dev;
123     struct acpi_bst *bst;
124     struct acpi_bix *bix;
125     struct acpi_battinfo *bi;
126 
127     /*
128      * Get the battery devclass and max unit for battery devices.  If there
129      * are none or error, return immediately.
130      */
131     batt_dc = devclass_find("battery");
132     if (batt_dc == NULL)
133 	return (ENXIO);
134     devcount = devclass_get_maxunit(batt_dc);
135     if (devcount == 0)
136 	return (ENXIO);
137 
138     /*
139      * Allocate storage for all _BST data, their derived battinfo data,
140      * and the current battery's _BIX (or _BIF) data.
141      */
142     bst = malloc(devcount * sizeof(*bst), M_TEMP, M_WAITOK | M_ZERO);
143     bi = malloc(devcount * sizeof(*bi), M_TEMP, M_WAITOK | M_ZERO);
144     bix = malloc(sizeof(*bix), M_TEMP, M_WAITOK | M_ZERO);
145 
146     /*
147      * Pass 1:  for each battery that is present and valid, get its status,
148      * calculate percent capacity remaining, and sum all the current
149      * discharge rates.
150      */
151     dev_idx = -1;
152     batt_stat = valid_rate = valid_units = 0;
153     total_cap = total_lfcap = 0;
154     for (i = 0; i < devcount; i++) {
155 	/* Default info for every battery is "not present". */
156 	acpi_reset_battinfo(&bi[i]);
157 
158 	/*
159 	 * Find the device.  Since devcount is in terms of max units, this
160 	 * may be a sparse array so skip devices that aren't present.
161 	 */
162 	batt_dev = devclass_get_device(batt_dc, i);
163 	if (batt_dev == NULL)
164 	    continue;
165 
166 	/* If examining a specific battery and this is it, record its index. */
167 	if (dev != NULL && dev == batt_dev)
168 	    dev_idx = i;
169 
170 	/*
171 	 * Be sure we can get various info from the battery.
172 	 */
173 	if (ACPI_BATT_GET_STATUS(batt_dev, &bst[i]) != 0 ||
174 	    ACPI_BATT_GET_INFO(batt_dev, bix, sizeof(*bix)) != 0)
175 		continue;
176 
177 	/* If a battery is not installed, we sometimes get strange values. */
178 	if (!acpi_battery_bst_valid(&bst[i]) ||
179 	    !acpi_battery_bix_valid(bix))
180 	    continue;
181 
182 	/*
183 	 * Record current state.  If both charging and discharging are set,
184 	 * ignore the charging flag.
185 	 */
186 	valid_units++;
187 	if ((bst[i].state & ACPI_BATT_STAT_DISCHARG) != 0)
188 	    bst[i].state &= ~ACPI_BATT_STAT_CHARGING;
189 	batt_stat |= bst[i].state;
190 	bi[i].state = bst[i].state;
191 
192 	/*
193 	 * If the battery info is in terms of mA, convert to mW by
194 	 * multiplying by the design voltage.  If the design voltage
195 	 * is 0 (due to some error reading the battery), skip this
196 	 * conversion.
197 	 */
198 	if (bix->units == ACPI_BIX_UNITS_MA && bix->dvol != 0 && dev == NULL) {
199 	    bst[i].rate = (bst[i].rate * bix->dvol) / 1000;
200 	    bst[i].cap = (bst[i].cap * bix->dvol) / 1000;
201 	    bix->lfcap = (bix->lfcap * bix->dvol) / 1000;
202 	}
203 
204 	/*
205 	 * The calculation above may set bix->lfcap to zero. This was
206 	 * seen on a laptop with a broken battery. The result of the
207 	 * division was rounded to zero.
208 	 */
209 	if (!acpi_battery_bix_valid(bix))
210 	    continue;
211 
212 	/*
213 	 * Some laptops report the "design-capacity" instead of the
214 	 * "real-capacity" when the battery is fully charged.  That breaks
215 	 * the above arithmetic as it needs to be 100% maximum.
216 	 */
217 	if (bst[i].cap > bix->lfcap)
218 	    bst[i].cap = bix->lfcap;
219 
220 	/* Calculate percent capacity remaining. */
221 	bi[i].cap = (100 * bst[i].cap) / bix->lfcap;
222 
223 	/* If this battery is not present, don't use its capacity. */
224 	if (bi[i].cap != -1) {
225 	    total_cap += bst[i].cap;
226 	    total_lfcap += bix->lfcap;
227 	}
228 
229 	/*
230 	 * On systems with more than one battery, they may get used
231 	 * sequentially, thus bst.rate may only signify the one currently
232 	 * in use.  For the remaining batteries, bst.rate will be zero,
233 	 * which makes it impossible to calculate the total remaining time.
234 	 * Therefore, we sum the bst.rate for batteries in the discharging
235 	 * state and use the sum to calculate the total remaining time.
236 	 */
237 	if (bst[i].rate != ACPI_BATT_UNKNOWN &&
238 	    (bst[i].state & ACPI_BATT_STAT_DISCHARG) != 0)
239 	    valid_rate += bst[i].rate;
240     }
241 
242     /* If the caller asked for a device but we didn't find it, error. */
243     if (dev != NULL && dev_idx == -1) {
244 	error = ENXIO;
245 	goto out;
246     }
247 
248     /* Pass 2:  calculate capacity and remaining time for all batteries. */
249     total_min = 0;
250     for (i = 0; i < devcount; i++) {
251 	/*
252 	 * If any batteries are discharging, use the sum of the bst.rate
253 	 * values.  Otherwise, we are on AC power, and there is infinite
254 	 * time remaining for this battery until we go offline.
255 	 */
256 	if (valid_rate > 0)
257 	    bi[i].min = (60 * bst[i].cap) / valid_rate;
258 	else
259 	    bi[i].min = 0;
260 	total_min += bi[i].min;
261     }
262 
263     /*
264      * Return total battery percent and time remaining.  If there are
265      * no valid batteries, report values as unknown.
266      */
267     if (valid_units > 0) {
268 	if (dev == NULL) {
269 	    battinfo->cap = (total_cap * 100) / total_lfcap;
270 	    battinfo->min = total_min;
271 	    battinfo->state = batt_stat;
272 	    battinfo->rate = valid_rate;
273 	} else {
274 	    battinfo->cap = bi[dev_idx].cap;
275 	    battinfo->min = bi[dev_idx].min;
276 	    battinfo->state = bi[dev_idx].state;
277 	    battinfo->rate = bst[dev_idx].rate;
278 	}
279 
280 	/*
281 	 * If the queried battery has no discharge rate or is charging,
282 	 * report that we don't know the remaining time.
283 	 */
284 	if (valid_rate == 0 || (battinfo->state & ACPI_BATT_STAT_CHARGING))
285 	    battinfo->min = -1;
286     } else
287 	acpi_reset_battinfo(battinfo);
288 
289     error = 0;
290 
291 out:
292     free(bi, M_TEMP);
293     free(bix, M_TEMP);
294     free(bst, M_TEMP);
295     return (error);
296 }
297 
298 static void
299 acpi_reset_battinfo(struct acpi_battinfo *info)
300 {
301     info->cap = -1;
302     info->min = -1;
303     info->state = ACPI_BATT_STAT_NOT_PRESENT;
304     info->rate = -1;
305 }
306 
307 /* Make string printable, removing invalid chars. */
308 static void
309 acpi_battery_clean_str(char *str, int len)
310 {
311     int i;
312 
313     for (i = 0; i < len && *str != '\0'; i++, str++) {
314 	if (!isprint(*str))
315 	    *str = '?';
316     }
317 
318     /* NUL-terminate the string if we reached the end. */
319     if (i == len)
320 	*str = '\0';
321 }
322 
323 /*
324  * The battery interface deals with devices and methods but userland
325  * expects a logical unit number.  Convert a logical unit to a device_t.
326  */
327 static device_t
328 acpi_battery_find_dev(u_int logical_unit)
329 {
330     int found_unit, i, maxunit;
331     device_t dev;
332     devclass_t batt_dc;
333 
334     dev = NULL;
335     found_unit = 0;
336     batt_dc = devclass_find("battery");
337     maxunit = devclass_get_maxunit(batt_dc);
338     for (i = 0; i < maxunit; i++) {
339 	dev = devclass_get_device(batt_dc, i);
340 	if (dev == NULL)
341 	    continue;
342 	if (logical_unit == found_unit)
343 	    break;
344 	found_unit++;
345 	dev = NULL;
346     }
347 
348     return (dev);
349 }
350 
351 static int
352 acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg)
353 {
354     union acpi_battery_ioctl_arg *ioctl_arg;
355     int error, unit;
356     device_t dev;
357 
358     /* For commands that use the ioctl_arg struct, validate it first. */
359     error = ENXIO;
360     unit = 0;
361     dev = NULL;
362     ioctl_arg = NULL;
363     if (IOCPARM_LEN(cmd) == sizeof(union acpi_battery_ioctl_arg) ||
364         IOCPARM_LEN(cmd) == sizeof(union acpi_battery_ioctl_arg_v1)) {
365 	ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
366 	unit = ioctl_arg->unit;
367 	if (unit != ACPI_BATTERY_ALL_UNITS)
368 	    dev = acpi_battery_find_dev(unit);
369     }
370 
371     /*
372      * No security check required: information retrieval only.  If
373      * new functions are added here, a check might be required.
374      */
375     /* Unit check */
376     switch (cmd) {
377     case ACPIIO_BATT_GET_UNITS:
378 	*(int *)addr = acpi_battery_get_units();
379 	error = 0;
380 	break;
381     case ACPIIO_BATT_GET_BATTINFO:
382     case ACPIIO_BATT_GET_BATTINFO_V1:
383 	if (dev != NULL || unit == ACPI_BATTERY_ALL_UNITS) {
384 	    bzero(&ioctl_arg->battinfo, sizeof(ioctl_arg->battinfo));
385 	    error = acpi_battery_get_battinfo(dev, &ioctl_arg->battinfo);
386 	}
387 	break;
388     case ACPIIO_BATT_GET_BIF:
389 	if (dev != NULL) {
390 	    bzero(&ioctl_arg->bif, sizeof(ioctl_arg->bif));
391 	    error = ACPI_BATT_GET_INFO(dev, &ioctl_arg->bif,
392 		sizeof(ioctl_arg->bif));
393 	}
394 	break;
395     case ACPIIO_BATT_GET_BIX:
396 	if (dev != NULL) {
397 	    bzero(&ioctl_arg->bix, sizeof(ioctl_arg->bix));
398 	    error = ACPI_BATT_GET_INFO(dev, &ioctl_arg->bix,
399 		sizeof(ioctl_arg->bix));
400 	}
401 	break;
402     case ACPIIO_BATT_GET_BST:
403     case ACPIIO_BATT_GET_BST_V1:
404 	if (dev != NULL) {
405 	    bzero(&ioctl_arg->bst, sizeof(ioctl_arg->bst));
406 	    error = ACPI_BATT_GET_STATUS(dev, &ioctl_arg->bst);
407 	}
408 	break;
409     default:
410 	error = EINVAL;
411     }
412 
413     /* Sanitize the string members. */
414     switch (cmd) {
415     case ACPIIO_BATT_GET_BIX:
416     case ACPIIO_BATT_GET_BIF:
417 	    /*
418 	     * Remove invalid characters.  Perhaps this should be done
419 	     * within a convenience function so all callers get the
420 	     * benefit.
421 	     */
422 	    acpi_battery_clean_str(ioctl_arg->bix.model,
423 		sizeof(ioctl_arg->bix.model));
424 	    acpi_battery_clean_str(ioctl_arg->bix.serial,
425 		sizeof(ioctl_arg->bix.serial));
426 	    acpi_battery_clean_str(ioctl_arg->bix.type,
427 		sizeof(ioctl_arg->bix.type));
428 	    acpi_battery_clean_str(ioctl_arg->bix.oeminfo,
429 		sizeof(ioctl_arg->bix.oeminfo));
430     };
431 
432     return (error);
433 }
434 
435 static int
436 acpi_battery_sysctl(SYSCTL_HANDLER_ARGS)
437 {
438     int val, error;
439 
440     acpi_battery_get_battinfo(NULL, &acpi_battery_battinfo);
441     val = *(u_int *)oidp->oid_arg1;
442     error = sysctl_handle_int(oidp, &val, 0, req);
443     return (error);
444 }
445 
446 static int
447 acpi_battery_units_sysctl(SYSCTL_HANDLER_ARGS)
448 {
449     int count, error;
450 
451     count = acpi_battery_get_units();
452     error = sysctl_handle_int(oidp, &count, 0, req);
453     return (error);
454 }
455 
456 static int
457 acpi_battery_init(void)
458 {
459     struct acpi_softc	*sc;
460     device_t		 dev;
461     int	 		 error;
462 
463     ACPI_SERIAL_ASSERT(battery);
464 
465     if (acpi_batteries_initialized)
466 	    return(0);
467 
468     error = ENXIO;
469     dev = devclass_get_device(devclass_find("acpi"), 0);
470     if (dev == NULL)
471 	goto out;
472     sc = device_get_softc(dev);
473 
474 #define	ACPI_REGISTER_IOCTL(a, b, c) do {	\
475     error = acpi_register_ioctl(a, b, c);	\
476     if (error)					\
477 	goto out;				\
478     } while (0)
479 
480     ACPI_REGISTER_IOCTL(ACPIIO_BATT_GET_UNITS, acpi_battery_ioctl, NULL);
481     ACPI_REGISTER_IOCTL(ACPIIO_BATT_GET_BATTINFO, acpi_battery_ioctl, NULL);
482     ACPI_REGISTER_IOCTL(ACPIIO_BATT_GET_BATTINFO_V1, acpi_battery_ioctl, NULL);
483     ACPI_REGISTER_IOCTL(ACPIIO_BATT_GET_BIF, acpi_battery_ioctl, NULL);
484     ACPI_REGISTER_IOCTL(ACPIIO_BATT_GET_BIX, acpi_battery_ioctl, NULL);
485     ACPI_REGISTER_IOCTL(ACPIIO_BATT_GET_BST, acpi_battery_ioctl, NULL);
486     ACPI_REGISTER_IOCTL(ACPIIO_BATT_GET_BST_V1, acpi_battery_ioctl, NULL);
487 #undef	ACPI_REGISTER_IOCTL
488 
489     sysctl_ctx_init(&acpi_battery_sysctl_ctx);
490     acpi_battery_sysctl_tree = SYSCTL_ADD_NODE(&acpi_battery_sysctl_ctx,
491 	SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO, "battery",
492 	CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "battery status and info");
493     SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
494 	SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
495 	OID_AUTO, "life", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
496 	&acpi_battery_battinfo.cap, 0, acpi_battery_sysctl, "I",
497 	"percent capacity remaining");
498     SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
499 	SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
500 	OID_AUTO, "time", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
501 	&acpi_battery_battinfo.min, 0, acpi_battery_sysctl, "I",
502 	"remaining time in minutes");
503     SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
504 	SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
505 	OID_AUTO, "rate", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
506 	&acpi_battery_battinfo.rate, 0, acpi_battery_sysctl, "I",
507 	"present rate in mW");
508     SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
509 	SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
510 	OID_AUTO, "state", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
511 	&acpi_battery_battinfo.state, 0, acpi_battery_sysctl, "I",
512 	"current status flags");
513     SYSCTL_ADD_PROC(&acpi_battery_sysctl_ctx,
514 	SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
515 	OID_AUTO, "units", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
516 	NULL, 0, acpi_battery_units_sysctl, "I", "number of batteries");
517     SYSCTL_ADD_INT(&acpi_battery_sysctl_ctx,
518 	SYSCTL_CHILDREN(acpi_battery_sysctl_tree),
519 	OID_AUTO, "info_expire", CTLFLAG_RW,
520 	&acpi_battery_info_expire, 0,
521 	"time in seconds until info is refreshed");
522 
523     acpi_batteries_initialized = TRUE;
524 
525 out:
526     if (error) {
527 	acpi_deregister_ioctl(ACPIIO_BATT_GET_UNITS, acpi_battery_ioctl);
528 	acpi_deregister_ioctl(ACPIIO_BATT_GET_BATTINFO, acpi_battery_ioctl);
529 	acpi_deregister_ioctl(ACPIIO_BATT_GET_BATTINFO_V1, acpi_battery_ioctl);
530 	acpi_deregister_ioctl(ACPIIO_BATT_GET_BIF, acpi_battery_ioctl);
531 	acpi_deregister_ioctl(ACPIIO_BATT_GET_BIX, acpi_battery_ioctl);
532 	acpi_deregister_ioctl(ACPIIO_BATT_GET_BST, acpi_battery_ioctl);
533 	acpi_deregister_ioctl(ACPIIO_BATT_GET_BST_V1, acpi_battery_ioctl);
534     }
535     return (error);
536 }
537