History log of /freebsd-current/sys/dev/acpica/acpi_battery.c
Revision Date Author Comments
# 2e850b83 16-Apr-2024 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>

acpi_battery: avoid divide-by-zero when no devices have capacity info

On laptops with builtin batteries, disconnecting the battery may show up
as a battery without any capacity information. (The theory is that one
is disconnecting the cells but the electronics identifying the battery
are still connected.) As a result, the loop over all batteries in
acpi_battery_get_battinfo results in total_lfcap == 0.

So, just check that total_lfcap is non-zero to avoid a division by zero
(triggerable by sysctl hw.acpi.battery).

Reported by: Stefano Marinelli
Tested by: Stefano Marinelli
Reviewed by: emaste
Differential Revision: https://reviews.freebsd.org/D44818


# 685dc743 16-Aug-2023 Warner Losh <imp@FreeBSD.org>

sys: Remove $FreeBSD$: one-line .c pattern

Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/


# 7029da5c 26-Feb-2020 Pawel Biernacki <kaktus@FreeBSD.org>

Mark more nodes as CTLFLAG_MPSAFE or CTLFLAG_NEEDGIANT (17 of many)

r357614 added CTLFLAG_NEEDGIANT to make it easier to find nodes that are
still not MPSAFE (or already are but aren’t properly marked).
Use it in preparation for a general review of all nodes.

This is non-functional change that adds annotations to SYSCTL_NODE and
SYSCTL_PROC nodes using one of the soon-to-be-required flags.

Mark all obvious cases as MPSAFE. All entries that haven't been marked
as MPSAFE before are by default marked as NEEDGIANT

Approved by: kib (mentor, blanket)
Commented by: kib, gallatin, melifaro
Differential Revision: https://reviews.freebsd.org/D23718


# 294de6bb 18-Feb-2020 Hiroki Sato <hrs@FreeBSD.org>

Add _BIX (Battery Information Extended) object support.

ACPI Control Method Batteries have a _BIF and/or _BIX object which
provide static properties of the battery. FreeBSD acpi_cmbat module
supported _BIF object only, which was deprecated as of ACPI 4.0.
_BIX is an extended version of _BIF defined in ACPI 4.0 or later.

As of writing, _BIX has two revisions. One is in ACPI 4.0 (rev.0) and
another is in ACPI 6.0 (rev.1). It seems that hardware vendors still
stick to _BIF only or _BIX rev.0 + _BIF for the maximum compatibility.
Microsoft requires _BIX rev.0 for Windows machines, so there are some
laptop machines with _BIX rev.0 only. In this case, FreeBSD does not
recognize the battery information.

After this change, the acpi_cmbat module gets battery information from
_BIX or _BIF object and internally uses _BIX rev.1 data structure as
the primary information store in the kernel. ACPIIO_BATT_GET_BI[FX]
returns an acpi_bi[fx] structure built by using information obtained
from a _BIF or a _BIX object found on the system. The revision number
field can be used to check which field is available. The acpiconf(8)
utility will show additional information if _BIX is available.

Although ABIs of ACPIIO_BATT_* were changed, the existing APIs for
userland utilities are not changed and the backward-compatible ABIs
are provided. This means that older versions of acpiconf(8) can also
work with the new kernel. The (union acpi_battery_ioctl_arg) was
padded to 256 byte long to avoid another ABI change in the future.
A _BIX object with its revision number >1 will be treated as
compatible with the rev.1 _BIX format.

Reviewed by: takawata
MFC after: 1 week
Differential Revision: https://reviews.freebsd.org/D23728


# 594d1c72 21-May-2019 Allan Jude <allanjude@FreeBSD.org>

Correct the way remaining battery life is calculated

Previously, if a system had multiple batteries, the remaining life
percentage was calculated as the average of each battery's percent
remaining. This results in rather incorrect values when you consider the
case of the Thinkpad X270 that has a small 3 cell internally battery, and
a hot-swappable 9 cell battery that is used first. Battery 0 is at 100%,
but battery 1 is at 10%, you do not infact have 55% of your capacity
remaining.

The new method calculates the percentage based on remaining capacity
out of total capacity, giving a much more accurate reading.

PR: 229818
Submitted by: Keegan Drake H.P. <kd-dev@pm.me>
MFC after: 2 weeks
Sponsored by: Klara Systems
Event: Waterloo Hackathon 2019


# 00377637 19-Feb-2019 Tom Jones <thj@FreeBSD.org>

Fix style after r340832

Reported by: jhb
Reviewed by: jhb, jtl
Approved by: jtl (mentor)
MFC after: 3 days
Differential Revision: https://reviews/freebsd.org/D18354


# 831f4679 22-Nov-2018 Tom Jones <thj@FreeBSD.org>

Add support for none ACPI battery method batteries

Remove the requirement that a device be a ACPI method battery to be supported
as a battery.

Require now that the device be in the battery devclass and implement the
get_status and get_info functions. This allows batteries which are not ACPI
method batteries to be supported.

Reviewed by: jtl
Approved by: jtl (mentor)
MFC after: 1 Month
Differential Revision: https://reviews.freebsd.org/D17434


# 68d0cda6 29-Oct-2018 John Baldwin <jhb@FreeBSD.org>

Make battery emptying rate available as sysctl variable.

Curiously, the in-kernel routines always use the design voltage to
convert from mA to mW, but acpiconf in userland uses the current
voltage. As a result, this can report a different mW rate than
acpiconf.

Submitted by: Manuel Stühn <freebsdnewbie@freenet.de>
MFC after: 2 months
Differential Revision: https://reviews.freebsd.org/D17077


# 27fcbbca 26-Nov-2011 Jean-Sébastien Pédron <dumbbell@FreeBSD.org>

Prevent a division by zero with some broken batteries

This problem was seen on a laptop with a dead battery.


# 766d7e65 17-Dec-2010 Andriy Gapon <avg@FreeBSD.org>

small cleanup of acpi battery status setting and checking

This is based on the patch submitted by Yuri Skripachov.
Overview of the changes:
- clarify double-use of some ACPI_BATT_STAT_* definitions
- clean up undefined/extended status bits returned by _BST
- warn about charging+discharging bits being set at the same time

PR: kern/124744
Submitted by: Yuri Skripachov <y.skripachov@gmail.com>
Tested by: Yuri Skripachov <y.skripachov@gmail.com>
MFC after: 2 weeks


# a7d5f7eb 19-Oct-2010 Jamie Gritton <jamie@FreeBSD.org>

A new jail(8) with a configuration file, to replace the work currently done
by /etc/rc.d/jail.


# 247db074 20-Aug-2009 John Baldwin <jhb@FreeBSD.org>

MFC 196403: Temporarily revert the new-bus locking for 8.0 release.

Approved by: re (kib)


# a56fe095 20-Aug-2009 John Baldwin <jhb@FreeBSD.org>

Temporarily revert the new-bus locking for 8.0 release. It will be
reintroduced after HEAD is reopened for commits by re@.

Approved by: re (kib), attilio


# 444b9186 02-Aug-2009 Attilio Rao <attilio@FreeBSD.org>

Make the newbus subsystem Giant free by adding the new newbus sxlock.
The newbus lock is responsible for protecting newbus internIal structures,
device states and devclass flags. It is necessary to hold it when all
such datas are accessed. For the other operations, softc locking should
ensure enough protection to avoid races.

Newbus lock is automatically held when virtual operations on the device
and bus are invoked when loading the driver or when the suspend/resume
take place. For other 'spourious' operations trying to access/modify
the newbus topology, newbus lock needs to be automatically acquired and
dropped.

For the moment Giant is also acquired in some key point (modules subsystem)
in order to avoid problems before the 8.0 release as module handlers could
make assumptions about it. This Giant locking should go just after
the release happens.

Please keep in mind that the public interface can be expanded in order
to provide more support, if there are really necessities at some point
and also some bugs could arise as long as the patch needs a bit of
further testing.

Bump __FreeBSD_version in order to reflect the newbus lock introduction.

Reviewed by: ed, hps, jhb, imp, mav, scottl
No answer by: ariff, thompsa, yongari
Tested by: pho,
G. Trematerra <giovanni dot trematerra at gmail dot com>,
Brandon Gooch <jamesbrandongooch at gmail dot com>
Sponsored by: Yahoo! Incorporated
Approved by: re (ksmith)


# 129d3046 05-Jun-2009 Jung-uk Kim <jkim@FreeBSD.org>

Import ACPICA 20090521.


# aaac7452 02-Jun-2009 Jung-uk Kim <jkim@FreeBSD.org>

Chase ACPICA API changes (for kernel and boot loader).


# 0f9e2596 17-Jan-2009 Alexander Motin <mav@FreeBSD.org>

Convert battery capacity/rate from mA to mW only when summary is requested.
Unconditional conversion causes incorrect rate units reported by acpiconf.

MFC after: 2 weeks


# d7f03759 19-Oct-2008 Ulf Lilleengen <lulf@FreeBSD.org>

- Import the HEAD csup code which is the basis for the cvsmode work.


# 1de9b7e4 20-Nov-2007 Jung-uk Kim <jkim@FreeBSD.org>

Check battery presence first before trying to get battery information.

PR: kern/117591
Tested by: Jessica Mahoney (root at varusonline dot com)


# 4306354d 09-Oct-2007 Nate Lawson <njl@FreeBSD.org>

When the EC times out (common with Compaqs), it may report a design
voltage of 0. This can result in a divide by zero trap. Add a guard
for this case. The value of lfcap is checked in acpi_battery_bif_valid()
just before this, so it is safe.

Reportd by: sam
Approved by: re
MFC after: 3 days


# ee939186 21-Dec-2006 Warner Losh <imp@FreeBSD.org>

ACPIIO_BATT_GET_UNITS would always return ENXIO. However, it should never
return an error since it returns a count of battery devices in the system.
Set it to 0 explicitly, since it is the only switch branch that doesn't set
it.

# I guess no one uses it.


# 197b4dcc 10-Jun-2006 Nate Lawson <njl@FreeBSD.org>

Minor sysctl cleanup. The RW flag means read|write and so it is redundant
to add the RD flag. Also, the debug node does not need to be writable.


# f08e63dc 22-Nov-2005 Nate Lawson <njl@FreeBSD.org>

Simplify checks for valid battery info via DeMorgan's Rule. No
functional change.


# 49ab8241 23-Oct-2005 Nate Lawson <njl@FreeBSD.org>

Add prototype to be consistent.


# 9aa72f74 22-Oct-2005 Nate Lawson <njl@FreeBSD.org>

Cleanups and support code for importing smart battery support.

* Use ACPI_BATT_UNKNOWN instead of constants
* Use maxunit instead of a count of devices since we may have sparse
battery devices in the future. Only userland should be using unit
numbers anyway, so provide a translation function. (Kernel use of
batteries should be restricted to looking up a device_t and calling
methods directly.
* Don't check acpi_BatteryIsPresent() in acpi_battery. Leave it up to
the hardware-specific driver (i.e. cmbat) since smart batteries seem
to not report the "battery present" flag.
* Convert mA to mW if the battery uses those units. CM-batteries only
used mW so this deficiency went unnoticed.
* Clean strings reported in the battery info from any control chars.
* Only dereference the unit from ioctl_arg if the full struct is present.
Unit wouldn't have been used later if it wasn't present but this is
cleaner. Translate the unit if it's not ACPI_BATTERY_ALL_UNITS.
* bzero structs before returning them to usermode for future compat.

Most of this work was submitted by Hans Petter Selasky and then majorly
reworked by myself.

Submitted by: Hans Petter Selasky <hselasky / c2i.net>


# 2a191126 11-Sep-2005 David E. O'Brien <obrien@FreeBSD.org>

Canonize the include of acpi.h.


# 84cdcfb3 11-Aug-2005 Nate Lawson <njl@FreeBSD.org>

Modify the fix in rev 1.16 slightly. Instead of setting the cap to zero,
just skip summing it if it's unknown.

MFC after: 1 day


# 9396c1b2 05-Aug-2005 Nate Lawson <njl@FreeBSD.org>

When summing capacities, skip info from batteries that weren't present.
Previously, we used all info (including -1 or "not present") which would
keep the system from reaching 100% when charging.

Reported by: Eric Anderson
MFC after: 2 days


# e4515dd5 28-Jul-2005 Nate Lawson <njl@FreeBSD.org>

Add descriptions for the sysctls.

MFC after: 1 day


# 50d0e47a 28-Jul-2005 Nate Lawson <njl@FreeBSD.org>

Zero the _BST, _BIF, and battinfo allocations. This is needed since we
later sum capacities for all batteries, even those that weren't actually
present. We only need to do this for _BST but do it for all of them.

Reported by: Eric Anderson
MFC after: 1 day


# f4e31e2e 25-Jul-2005 Nate Lawson <njl@FreeBSD.org>

Correct minor output bug. When the battery being queried is charging or
the overall discharge rate is 0, mark the battery remaining time as
"unknown", not 0.

Reported by: Eric Kjeldergaard
MFC after: 4 days


# 76f6fe4f 23-Jul-2005 Nate Lawson <njl@FreeBSD.org>

Rewrite the acpi_battery interface to allow for other battery types
(i.e., smart battery) and fix various bugs found during the cleanup.

API changes:
* kernel access:
Access to individual batteries is now via devclass_find("battery").
Introduce new methods ACPI_BATT_GET_STATUS (for _BST-formatted data) and
ACPI_BATT_GET_INFO (for _BIF-formatted data). The helper function
acpi_battery_get_battinfo() now takes a device_t instead of a unit #
argument. If dev is NULL, this signifies all batteries.

* ioctl access:
The ACPIIO_BATT_GET_TYPE and ACPIIO_BATT_GET_BATTDESC ioctls have been
removed. Since there is now no need for a mapping between "virtual" unit
and physical unit, usermode programs can just specify the unit directly and
skip the old translation steps. In fact, acpiconf(8) was actually already
doing this and virtual unit was the same as physical unit in all cases
since there was previously only one battery type (acpi_cmbat). Additionally,
we now map the ACPIIO_BATT_GET_BIF and ACPIIO_BATT_GET_BST ioctls for all
batteries, if they provide the associated methods.

* apm compatibility device/ioctls: no change
* sysctl: no change

Since most third-party applications use the apm(4) compat interface, there
should be very few affected applications (if any).

Reviewed by: bruno
MFC after: 5 days


# 8f764959 17-Mar-2005 Nate Lawson <njl@FreeBSD.org>

Introduce a general name for the previously cmbat-only ioctls. It has the
same value as the previous ioctls so no binary change. Also, make a few
style changes to reduce diffs to my tree.

Loosely based on code from: Hans Petter Selasky


# dad97fee 02-Mar-2005 David E. O'Brien <obrien@FreeBSD.org>

Fix SCM ID's.


# 07d4077e 13-Aug-2004 Nate Lawson <njl@FreeBSD.org>

MPSAFE locking

* Serialize acpi_battery_get_battdesc(), acpi_battery_register(), and
acpi_battery_remove().
* Assert that the sx lock is held in acpi_batteries_init().
* Remove check for device_get_softc() returning NULL.


# 7a5230b8 12-Jul-2004 Nate Lawson <njl@FreeBSD.org>

Add the ability to detach a battery. Now batteries that are detached are
also removed from the battery list.


# 798fb860 11-Aug-2003 Nate Lawson <njl@FreeBSD.org>

Style cleanups to match the rest of this directory. For acpi_battery.c,
remove unused includes.


# a89fcf28 14-Feb-2003 Takanori Watanabe <takawata@FreeBSD.org>

Allow non-privilaged user to retrive battery or AC line information.

Reviewed by: rwatson


# fc0ea94a 16-Oct-2002 John Baldwin <jhb@FreeBSD.org>

Catch up to changes in acpivar.h to add support for using ACPI on
4-stable systems.

Sponsored by: The Weather Channel


# 3273b005 07-Jan-2002 Mike Smith <msmith@FreeBSD.org>

Staticise devclasses and some unnecessarily global variables.


# 9febbb40 30-Oct-2001 Mitsuru IWASAKI <iwasaki@FreeBSD.org>

Some improvements of control method battery driver.
- Add a new MIB for battery info expire time in order to make it changeable.
Battery info expire time can be specified by
hw.acpi.battery.info_expire in sec.
- Add own MALLOC type and fix some potential memory leakages.
- Change some frequent printings to verbose printing.
- Stop timeout during acpi_cmbat_get_bst() too. This should reduce
the races with BIF evaluation.
- Remove acpi_cmbat_get_bif() invocation from acpi_cmbat_attach().
This was redundant because this should be called from
acpi_cmbat_timeout() now.


# f86214b6 26-Oct-2001 Mitsuru IWASAKI <iwasaki@FreeBSD.org>

Add APM compatibility feature to ACPI.
This emulates APM device node interface APIs (mainly ioctl) and
provides APM services for the applications. The goal is to support
most of APM applications without any changes.
Implemented ioctls in this commit are:
- APMIO_SUSPEND (mapped ACPI S3 as default but changable by sysctl)
- APMIO_STANDBY (mapped ACPI S1 as default but changable by sysctl)
- APMIO_GETINFO and APMIO_GETINFO_OLD
- APMIO_GETPWSTATUS

With above, many APM applications which get batteries, ac-line
info. and transition the system into suspend/standby mode (such as
wmapm, xbatt) should work with ACPI enabled kernel (if ACPI works well :-)

Reviewed by: arch@, audit@ and some guys


# 4c1cdee6 26-Aug-2001 Mike Smith <msmith@FreeBSD.org>

Updates to match the ACPI CA 20010816 import:

- New debug macro (ACPI_DEBUG_PRINT), reducing debug-case code size.
- New debug level/subsystem codes.


# 6f69255b 05-Jul-2001 Mike Smith <msmith@FreeBSD.org>

Add a new helper function for finding resources in resource buffers.

Move the ACPI generic battery code into a new file.