Linux Battery Kernel for Ubuntu

Ubuntu’s goal of putting the system tray completely under OS control got us thinking about how to connect the Red Reactor data to the battery management functions built into the OS.

The result is a Red Reactor OS Kernel module that defines the battery system and links the output from a Red Reactor device driver to the OS. The Kernel module creates a virtual device file at /dev/redreactor to which the device driver reports its data. Each of these reports triggers the kernel module to interpret the data and link it to the battery properties queried by the OS. The Kernel module defines which properties are supported, and the property data can be accessed by user applications via the virtual system files at /sys/class/power_supply/BAT0 and /sys/class/power_supply/AC0.

The properties managed by the redreactor kernel are:

POWER_SUPPLY_PROP_STATUS (charging, discharging, full)
POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN (uW for chosen batteries)
POWER_SUPPLY_PROP_ENERGY_FULL (uW based on maximum charge voltage)
POWER_SUPPLY_PROP_CHARGE_NOW (% charge)
POWER_SUPPLY_PROP_CAPACITY (% charge)
POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN (set to 10%)
POWER_SUPPLY_PROP_CAPACITY_LEVEL (critical, low, normal, high, full)
POWER_SUPPLY_PROP_VOLTAGE_NOW (averaged reading in micro-Volts)
POWER_SUPPLY_PROP_CURRENT_NOW (averaged reading in micro-Amps)

Plus a number of static parameters for battery type etc.

These values are updated every time the Red Reactor driver writes to the device file at /dev/redreactor, with averaged voltage and current samples and the battery capacity value. After loading the Red Reactor Kernel module the OS battery icon is displayed in the system tray where the CAPACITY_LEVEL property is used by the pop-up window, and the status information shows the STATUS and CAPACITY properties as well. All the properties enabled this way are shown by the Power Statistics OS application, which also shows the AC power status as active or not. However, the RATE value (shown below as being in uW) is incorrectly taken from the CURRENT_NOW property (uA) which means the OS estimates for time to full charge / time to empty are incorrectly calculated for the battery energy values provided by the Red Reactor driver.

The data managed by the kernel module is also accessible via the OS virtual file system at /sys/class/power_supply/BAT0 and /sys/class/power_supply/AC0. The uevent process file contains the full status.

The Ubuntu OS will also provide a pop-up warning when the battery capacity falls below 10%, and the Red Reactor driver will trigger a safe shutdown at the battery’s minimum voltage level defined in the driver.

Install scripts will help you install the kernel module , and the driver is installed as a system service so both will run every time the system is restarted.

It has definitely been a case of trial and error going from a few Googled examples to getting it working! And I have to say, I think the Ubuntu OS restrictions on the system tray have resulted in it providing a battery status display that is not as friendly and useful as it could have been. For example, with our RR_BatWay system tray application both voltage and current are immediately visible as a tooltip [not available via Ubuntu’s System Tray], and a custom application can always produce much more useful information than a generic battery status icon. And it is all very well for the OS to want to standardise on the icon set, but really, the Ubuntu icons aren’t that great either!

But I’ve added a lot of comments in the code if you want to explore this approach further for your system, and feel free to provide feedback on tips and tricks, or how to fix the time to emtpy/full estimates from the OS! I would definitely recommend to review the Linux power_supply.h file and the Linux source code for power_supply_core.c for more background info, as linked from our GitHub pages.

All the code and further information is available from the new RR_Ubuntu GitHub folder at https://github.com/Scally-H/RedReactor/tree/main/RR_Ubuntu

UPDATE: Although the Ubuntu settings screen did not show an option to enable the battery percentage value to be added to the battery icon, the following command did the trick:

gsettings set org.gnome.desktop.interface show-battery-percentage true
Adding Battery Capacity Percentage to Ubuntu System Tray

3 thoughts on “Ubuntu Kernel Module and Driver

  1. Paul Turner says:

    Great work! Definitely worth exploring but agree the OS options seem limited.

  2. Pascal says:

    Check out the command line above if you want to show the battery percentage on the Ubuntu system tray and can’t find the option in the power settings menu!

Leave a Reply

Your email address will not be published. Required fields are marked *

8 − 6 =