Visitar URL original
drivers/power: Do not call gauge methods a driver did not implement. by casaroli · Pull Request #20506 · apache/nuttx · GitHub
Skip to content

drivers/power: Do not call gauge methods a driver did not implement. - #20506

Merged
acassis merged 2 commits into
apache:masterfrom
casaroli:battery-gauge-optional-ops
Oct 8, 2026
Merged

acassis merged 2 commits into
apache:masterfrom
casaroli:battery-gauge-optional-ops

Conversation

@casaroli

@casaroli casaroli commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Summary

The battery gauge upper half calls dev->ops->X() for every BATIOC_* command, but it checks only the argument pointer. The operations are optional: max1704x implements 4 of 8, and the fake gauge leaves chipid and operate NULL. A request for a missing method calls address 0. On ARM this is a hard fault (INVSTATE).

This change checks each method pointer. A missing method now returns -ENOTTY, the same value as an unknown command.

The first commit fixes older nxstyle errors in battery_gauge.c, because CI checks every file that a change touches. It changes whitespace only.

Impact

Gauge drivers and their users only. Ioctls that crashed now return ENOTTY. Implemented methods do not change.

Testing

sim:nsh with BATTERY_GAUGE and BATTERY_FAKE_GAUGE. A local test app (not part of this PR) registers the fake gauge and sends ioctls to /dev/charge/fakegauge.

Before:

gaugetest: ioctl(BATIOC_VOLTAGE) ret=0 errno=0 (ok) value=4000
gaugetest: ioctl(BATIOC_CHIPID) ...
=== sim exit status: 11 (signal 11)

After (errno 25 is ENOTTY):

gaugetest: ioctl(BATIOC_VOLTAGE) ret=0 errno=0 (ok) value=4000
gaugetest: ioctl(BATIOC_CHIPID) ret=-1 errno=25 (Unknown error 25) value=-1
gaugetest: ioctl(BATIOC_OPERATE) ret=-1 errno=25 (Unknown error 25) value=-1
gaugetest: done

nucleo-f412zg:nsh with BATTERY_GAUGE, MAX1704X, BQ27426 and BATTERY_FAKE_GAUGE builds with arm-none-eabi-gcc 13.2.

tools/checkpatch.sh -c -u -m -g passes.

Whitespace only.  Add a blank line after the declarations in the BATIOC_*
cases, and indent two case labels like the others.  The errors are older
than the next commit, but checkpatch checks the whole file.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
Every BATIOC_* case in the gauge upper half calls dev->ops->X() after
checking only that the *argument* pointer is non-NULL.  The operations
themselves are optional -- max1704x.c implements four of the eight, and
the in-tree fake gauge leaves chipid and operate NULL -- so asking a gauge
for something it does not provide calls through a NULL pointer.

On ARM that is not a null dereference but a branch to address 0, which has
the Thumb bit clear: the core takes a usage fault with CFSR bit 17,
INVSTATE, escalated to a hard fault.  From userspace it looks like the
program stopped mid-run for no reason, and on a board whose assert path
delays before resetting it looks like a hang.

Guard all eight.  A missing method is now ENOTTY, which is what the default
case already returns for an unrecognised command and what a caller can
sensibly probe for.

Tested on sim:nsh with the fake gauge and a local test app.  Before the
change, ioctl(BATIOC_CHIPID) kills the simulator with SIGSEGV.  After the
change, BATIOC_CHIPID and BATIOC_OPERATE return ENOTTY and
BATIOC_VOLTAGE still returns the voltage.  nucleo-f412zg:nsh with
BATTERY_GAUGE, MAX1704X, BQ27426 and BATTERY_FAKE_GAUGE builds.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@github-actions github-actions Bot added Area: Drivers Drivers issues Size: M The size of the change in this PR is medium labels Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@acassis
acassis merged commit 209f73f into apache:master Oct 8, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants