3

After twiddling LEDs with /sys/class/leds/, I'm now trying to understand how to control the battery in my computer with linux kernel power supply attributes, specifically CHARGE_CONTROL_LIMIT. However, no such attribute exists in /sys/class/power_supply/BAT0/.

How I've tried to answer this question:

From what I understand reading man 5 sysfs, sysfs (usually mounted as sys) is an interface between userspace and kernelspace.

Hypothesis:

  1. There exists a kernel module that creates and updates the contents of /sys/class/power_supply/BAT0/

  2. That kernel module may, but does not seem to implement CHARGE_CONTROL_LIMIT. It does implement other attributes:

cat /sys/class/power_supply/BAT0/charge_full     
2884000
cat /sys/class/power_supply/BAT0/charge_now 
2884000
cat /sys/class/power_supply/BAT0/cycle_count
0                                                 <--- this seems broken
cat /sys/class/power_supply/BAT0/capacity
100

To test this hypothesis, I sought the documentation/source of the responsible kernel module.

A hang-up: Trying to find the kernel module that's responsible for /sys/class/power_supply/BAT0/

ArchWiki and this page demonstrate how to look-up a kernel module, given a modalias.

cat /sys/class/power_supply/BAT0/device/modalias
acpi:PNP0C0A:

sudo grep -E 'PNP0C0A' /lib/modules/*/modules.alias
# nothing is found

I see that the battery hardware is of type acpi, however, there's no exact match for its modalias in modules.alias. (I tried a a few combinations of wildcards to find it, to no avail).

@0andriy suggested checking /drivers/platform/x86, which I did as follows:

find /lib/modules/5.0.0-15-generic/kernel -type f -name '*.ko' | xargs modinfo | grep -E 'alias.*PNP0C0A'
# Nothing

# Trying another query, to check that the query works:
find /lib/modules/5.0.0-15-generic/kernel/drivers -type f -name '*.ko' | xargs modinfo | grep -E 'alias.*TOS'
alias:          acpi*:TOS1900:*
alias:          acpi*:TOS6208:*
alias:          acpi*:TOS6207:*
alias:          acpi*:TOS6200:*

This is an HP laptop with a recent kernel

uname -a                                                                     
Linux lithium 5.0.0-15-generic #16-Ubuntu SMP Mon May 6 17:41:33 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
RemarkableBucket
  • 340
  • 4
  • 11

2 Answers2

1

The problem is that the power source may be from a module already statically linked to the kernel (not visible in lsmod or present in /lib/modules), rather than left as a standalone module. So:

A) check /sys/module first. This is a directory which contains entries for both dynamically linked (i.e. visible by lsmod) and statically linked ones. Check if you see a (probably lowercase) pnp* entry there.

B) if you do, and it's not in lsmod, you'll need the kernel sources. These can possibly be obtained from HP, but you can start with the stock sources (http://kernel.org) since there's a good chance this is in the mainline kernel.

C) Those /sys entries are created by code calling "power_supply_register". Try a brute force grep over all the modules, to see which matches. You can also do this over the Linux source tree (something like find . -type f | xargs grep power_supply_register, or grep -R, etc).

Whether or not you can mess with the charging limits, etc, is up to the module in question. Good luck finding it.

Technologeeks
  • 7,674
  • 25
  • 36
1

I was looking for the kernel module that drives the battery of my laptop, and mine uses the same modalias as yours: acpi:PNP0C0A:

On my system this is driven by the 'battery' module.
This is the case at least on openSUSE Leap 15.4, with kernel 5.14.21

grep /lib/modules/$(uname -r)/modules.alias -e ":PNP0C0A"
alias acpi*:PNP0C0A:* battery

modinfo confirms this is what I was searching for:

modinfo battery
filename:       /lib/modules/5.14.21-150400.22-default/kernel/drivers/acpi/battery.ko.zst
license:        GPL
description:    ACPI Battery Driver
author:         Alexey Starikovskiy <astarikovskiy@suse.de>
author:         Paul Diefenbaugh
suserelease:    SLE15-SP4
srcversion:     69EB486D2C8DDEBE723A052
alias:          acpi*:PNP0C0A:*
depends:        
supported:      yes
retpoline:      Y
intree:         Y
name:           battery
vermagic:       5.14.21-150400.22-default SMP preempt mod_unload modversions 

I don't know the answer to why doesnt it show up in your search, but I hope this is helpful.