From 4795a7ccfd260077651a12eb4e57361562850df2 Mon Sep 17 00:00:00 2001 From: Vincent Schweiger Date: Fri, 21 Aug 2026 20:33:58 +0200 Subject: [PATCH] initial commit; version 0.1.0 --- Makefile | 8 ++++ debian/changelog | 6 +++ debian/control | 14 ++++++ debian/dkms | 1 + debian/install | 5 ++ debian/rules | 14 ++++++ debian/source/format | 1 + dkms.conf | 9 ++++ fake-battery.c | 112 +++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 170 insertions(+) create mode 100644 Makefile create mode 100644 debian/changelog create mode 100644 debian/control create mode 100644 debian/dkms create mode 100755 debian/install create mode 100755 debian/rules create mode 100644 debian/source/format create mode 100644 dkms.conf create mode 100644 fake-battery.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c0f6e06 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +KVER ?= $(shell uname -r) +obj-m := fake-battery.o + +all: + $(MAKE) -C /lib/modules/$(KVER)/build M=$(PWD) modules + +clean: + $(MAKE) -C /lib/modules/$(KVER)/build M=$(PWD) clean diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..3063b72 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,6 @@ +fake-battery-dkms (0.1.0-1) stable; urgency=low + + * Initial release + + -- Vincent Schweiger Fri, 21 Aug 2026 19:39:00 + +0200 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..61d041c --- /dev/null +++ b/debian/control @@ -0,0 +1,14 @@ +Source: fake-battery-dkms +Maintainer: Vincent Schweiger +Section: kernel +Priority: optional +Build-Depends: debhelper-compat (= 13), + dh-sequence-dkms, +Standards-Version: 4.7.0 +Rules-Requires-Root: no + +Package: fake-battery-dkms +Architecture: all +Depends: ${misc:Depends} +Description: Fake Battery + Creates a fake battery module diff --git a/debian/dkms b/debian/dkms new file mode 100644 index 0000000..7499b0c --- /dev/null +++ b/debian/dkms @@ -0,0 +1 @@ +dkms.conf diff --git a/debian/install b/debian/install new file mode 100755 index 0000000..9fc325a --- /dev/null +++ b/debian/install @@ -0,0 +1,5 @@ +#!/bin/sh + +for dir in fake-battery.c Makefile; do + echo ${dir} /usr/src/${PACKAGE_NAME}-${PACKAGE_VERSION} +done diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..464020d --- /dev/null +++ b/debian/rules @@ -0,0 +1,14 @@ +#!/usr/bin/make -f + +PACKAGE_NAME=$(shell grep PACKAGE_NAME= dkms.conf | cut -d= -f2 | cut -d\" -f2) +PACKAGE_VERSION=$(shell grep PACKAGE_VERSION= dkms.conf | cut -d= -f2 | cut -d\" -f2) +export PACKAGE_NAME PACKAGE_VERSION + +%: + dh $@ --with dkms + +# Nothing to configure, build or auto-install (this all happens after +# installation using dkms) +override_dh_auto_configure: +override_dh_auto_build: +override_dh_auto_install: diff --git a/debian/source/format b/debian/source/format new file mode 100644 index 0000000..163aaf8 --- /dev/null +++ b/debian/source/format @@ -0,0 +1 @@ +3.0 (quilt) diff --git a/dkms.conf b/dkms.conf new file mode 100644 index 0000000..e1fb232 --- /dev/null +++ b/dkms.conf @@ -0,0 +1,9 @@ +PACKAGE_VERSION="0.1.0" +PACKAGE_NAME="fake-battery" +CLEAN=true +BUILT_MODULE_NAME[0]="fake-battery" +BUILT_MODULE_LOCATION[0]="" +DEST_MODULE_LOCATION[0]="/extra" +MAKE[0]="make KVER=$kernelver -C ${dkms_tree}/${PACKAGE_NAME}/${PACKAGE_VERSION}/build" +AUTOINSTALL="yes" + diff --git a/fake-battery.c b/fake-battery.c new file mode 100644 index 0000000..9af7adf --- /dev/null +++ b/fake-battery.c @@ -0,0 +1,112 @@ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt +#include +#include +#include +#include + +static struct power_supply *fake_battery; +static struct power_supply_desc battery_desc; + +static enum power_supply_property fake_battery_props[] = { + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_HEALTH, + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_TECHNOLOGY, + POWER_SUPPLY_PROP_CAPACITY, + POWER_SUPPLY_PROP_CAPACITY_LEVEL, + POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, + POWER_SUPPLY_PROP_CHARGE_FULL, + POWER_SUPPLY_PROP_CHARGE_NOW, + POWER_SUPPLY_PROP_VOLTAGE_NOW, + POWER_SUPPLY_PROP_CURRENT_NOW, + POWER_SUPPLY_PROP_TEMP, +}; + +static int fake_battery_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + switch (psp) { + case POWER_SUPPLY_PROP_STATUS: + val->intval = POWER_SUPPLY_STATUS_FULL; + break; + case POWER_SUPPLY_PROP_HEALTH: + val->intval = POWER_SUPPLY_HEALTH_GOOD; + break; + case POWER_SUPPLY_PROP_PRESENT: + val->intval = 1; + break; + case POWER_SUPPLY_PROP_TECHNOLOGY: + val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO; + break; + case POWER_SUPPLY_PROP_CAPACITY: + val->intval = 100; + break; + case POWER_SUPPLY_PROP_CAPACITY_LEVEL: + val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL; + break; + case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: + val->intval = 5000000; /* 5000 mAh */ + break; + case POWER_SUPPLY_PROP_CHARGE_FULL: + val->intval = 5000000; /* 5000 mAh */ + break; + case POWER_SUPPLY_PROP_CHARGE_NOW: + val->intval = 5000000; /* 5000 mAh (fully charged) */ + break; + case POWER_SUPPLY_PROP_VOLTAGE_NOW: + val->intval = 4200000; /* 4.2V */ + break; + case POWER_SUPPLY_PROP_CURRENT_NOW: + val->intval = 0; /* Not charging */ + break; + case POWER_SUPPLY_PROP_TEMP: + val->intval = 250; /* 25°C in 0.1°C units */ + break; + default: + return -EINVAL; + } + return 0; +} + +static int __init fake_battery_init(void) +{ + int ret; + + pr_info("fake_battery: Loading fake battery module\n"); + + // Initialize the power supply descriptor + battery_desc.name = "fake-battery"; + battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; + battery_desc.properties = fake_battery_props; + battery_desc.num_properties = ARRAY_SIZE(fake_battery_props); + battery_desc.get_property = fake_battery_get_property; + + // Register the power supply + fake_battery = power_supply_register(NULL, &battery_desc, NULL); + if (IS_ERR(fake_battery)) { + ret = PTR_ERR(fake_battery); + pr_err("fake_battery: Failed to register power supply: %d\n", ret); + return ret; + } + + pr_info("fake_battery: Fake battery registered successfully\n"); + pr_info("fake_battery: Battery status: 100%% charged\n"); + + return 0; +} + +static void __exit fake_battery_exit(void) +{ + pr_info("fake_battery: Unloading fake battery module\n"); + if (fake_battery) + power_supply_unregister(fake_battery); +} + +module_init(fake_battery_init); +module_exit(fake_battery_exit); + +MODULE_AUTHOR("Vincent Schweiger"); +MODULE_DESCRIPTION("Fake battery module that reports a fully charged battery"); +MODULE_VERSION("0.1.0"); +MODULE_LICENSE("GPL");