Fix for FW 6.0.279225

Open
#12 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
30/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
embedded-iot

Research direction

The issue includes a proposed patch in usbms.c and usbms.h for MTK power supply paths. Read the existing sysfs path selection and its related definitions, then check how the fallback handles both firmware naming schemes. Done means USB cable detection, battery readings, and charger type work on FW 6.x while FW 5.x behavior remains unchanged; the issue reports an on-device test for FW 6.x.

Written by the indexing model from the issue text.

Description

Hi all,

this is an AI-generated pull request after the Kobo updated silently to firmware 6.0.x and koreader stopped working.

This is for the KoboUSBMS part because USB connection stopped working even after getting latest Koreader to work.

Pull request for koreader/KoboUSBMS: MTK fallback to the battery power supply on FW 6.x

Commit message

MTK: fall back to the battery power supply on FW 6.x

Kobo FW 6.0 renamed the bd71827 power supply from bd71827_bat to battery
(same driver, same attributes). On MTK, usbms hardcoded the old name for
capacity, status and charger_type, and it uses status for cable sensing,
so on FW 6.x it never saw the cable, showed 0% and gave up after 60 s.

If bd71827_bat/status is missing and battery/status exists, use the
battery node for all three. Nothing changes when bd71827_bat exists
(FW 5.x).

Fixes #11

Co-Authored-By: Claude Opus 5.5 <[email protected]>

PR title

MTK: fall back to the battery power supply on FW 6.x

PR description

Fixes #11.

Problem. Kobo FW 6.0 renamed the bd71827 power supply from bd71827_bat to battery.
The driver is unchanged (bd71827-power), and so are its attributes. On MTK, usbms hardcodes
the old name for MTK_BATT_CAP_SYSFS, MTK_BATT_STATUS_SYSFS and MTK_CHARGER_TYPE_SYSFS.
Because sysfs_is_usb_plugged() reads MTK_BATT_STATUS_SYSFS for cable sensing and returns
false when the file can't be opened, the effect on FW 6.x is:

  • "Waiting to be plugged in…" with the cable already in, and the 60 s countdown runs out.
    The post-timeout re-check uses the same function, so it can't recover.
  • Unplug/replug is caught as a uevent, but the re-check after the 2 s settle fails
    ("still unplugged despite the plug in event"), so the countdown just starts over.
  • The battery reads 0%, and the charger-type check is skipped (CHARGER_TYPE_SYSFS → NULL).

Evidence.

  • Kobo's own firmware images (qt6-update-*.zip from ereaderfiles.kobo.com),
    spa-bw/kernel.img (Clara BW): 5.18.270971 has bd71827_bat / bd71827_ac, while
    6.0.279225 only has battery.
  • Live sysfs on a 6.0 Clara Colour, from #11:
    /sys/class/power_supply/battery -> …/bd71827-power.4.auto/power_supply/battery, with
    battery/charger_type present.
  • Observed on a Clara BW (N365), FW 6.0.279225, KOReader kobov5 nightly
    v2026.07.2-362-g98d22cc: all three symptoms above.

Change. In the is_mtk branch, if bd71827_bat/status does not exist and
battery/status does, use battery/{capacity,status,charger_type} (new MTK_FW6_*
defines) and log it. Otherwise the existing paths are used exactly as before, so nothing
changes on FW 5.x, where bd71827_bat exists. The battery/* strings match the sunxi ones,
but they get their own defines because they mean "MTK on FW 6", not sunxi.

The four existing *_CHARGER_TYPE_SYSFS defines are realigned by two columns. That is what
clang-format (the repo's .clang-format, AlignConsecutiveMacros: AcrossComments) produces
once the longer MTK_FW6_CHARGER_TYPE_SYSFS joins the block. usbms.c needs no reformatting.

Testing.

  • Built the way KOReader ships it: koreader-base d530560f (pinned by KOReader 98d22cc),
    make TARGET=kobov5 kobo-usbms in koreader/kokobo:6.0.0-22.04, with this change added via
    PATCH_FILES in thirdparty/kobo-usbms/CMakeLists.txt. As a control, the unmodified
    build reproduces the usbms shipped in that nightly byte for byte (sha256 14a1e494…).
  • The patched build compiles warning-free with the project's warning flags. In the resulting
    KoboUSBMS.tar.gz, only usbms differs from the control build.
  • The binary tested on the device was built from this change before the clang-format
    realignment above. That realignment is whitespace in four #define lines and nothing else
    (git diff -w identical).
  • Tested on device: Clara BW (N365), FW 6.0.279225, KOReader kobov5 nightly
    v2026.07.2-362-g98d22cc. Before: "KoboUSBMS failed to setup an USBMS session" on every
    attempt. After: the cable is detected right away, the USB screen shows the real battery
    level, and the host enumerates 2237:4239 and attaches the 12.3 GB storage, which reads
    fine. Ejecting from the host ends the session, and KOReader comes back.
  • Not tested on FW 5.x hardware. There, bd71827_bat/status exists (confirmed in the
    5.18.270971 kernel), so the else branch is taken and behaviour is identical to before.

Follow-up. After merge, koreader-base needs its pin in
thirdparty/kobo-usbms/CMakeLists.txt bumped for the fix to reach KOReader builds.
Related KOReader-side fix for the same rename: koreader/koreader#16423.


The patch

diff --git a/usbms.c b/usbms.c
index 42675b8..523e74a 100644
--- a/usbms.c
+++ b/usbms.c
@@ -1137,13 +1137,21 @@ int
 
 	// And setup the sysfs paths & usb check based on the device…
 	if (ctx.fbink_state.is_mtk) {
-		BATT_CAP_SYSFS     = MTK_BATT_CAP_SYSFS;
-		CHARGER_TYPE_SYSFS = MTK_CHARGER_TYPE_SYSFS;
+		// NOTE: FW 6.x renamed the bd71827 power supply from bd71827_bat to battery...
+		if (access(MTK_BATT_STATUS_SYSFS, F_OK) != 0 && access(MTK_FW6_BATT_STATUS_SYSFS, F_OK) == 0) {
+			BATT_CAP_SYSFS     = MTK_FW6_BATT_CAP_SYSFS;
+			CHARGER_TYPE_SYSFS = MTK_FW6_CHARGER_TYPE_SYSFS;
+			BATT_STATUS_SYSFS  = MTK_FW6_BATT_STATUS_SYSFS;
+			LOG(LOG_INFO, "Using the FW 6.x MTK power supply sysfs entries (battery instead of bd71827_bat)");
+		} else {
+			BATT_CAP_SYSFS     = MTK_BATT_CAP_SYSFS;
+			CHARGER_TYPE_SYSFS = MTK_CHARGER_TYPE_SYSFS;
+			BATT_STATUS_SYSFS  = MTK_BATT_STATUS_SYSFS;
+		}
 
 		// The CM_USB_Plug_IN ioctl still doesn't look at the right power supplies...
 		// NOTE: And will actually crash the kernel if you attempt it!
-		BATT_STATUS_SYSFS = MTK_BATT_STATUS_SYSFS;
-		fxpIsUSBPlugged   = &sysfs_is_usb_plugged;
+		fxpIsUSBPlugged = &sysfs_is_usb_plugged;
 		LOG(LOG_INFO, "Using the MTK battery status sysfs entry to handle cable sensing");
 	} else if (ctx.fbink_state.is_sunxi) {
 		BATT_CAP_SYSFS     = SUNXI_BATT_CAP_SYSFS;
diff --git a/usbms.h b/usbms.h
index 1662cd4..415f969 100644
--- a/usbms.h
+++ b/usbms.h
@@ -133,11 +133,15 @@ const char* BATT_CAP_SYSFS = NULL;
 const char* BATT_STATUS_SYSFS      = NULL;
 bool (*fxpIsUSBPlugged)(int, bool) = NULL;
 // These, on the other hand, are only available on Mk. 7+
-#define NXP_CHARGER_TYPE_SYSFS   "/sys/class/power_supply/mc13892_charger/device/charger_type"
-#define SUNXI_CHARGER_TYPE_SYSFS "/sys/class/power_supply/charger/device/charger_type"
+#define NXP_CHARGER_TYPE_SYSFS     "/sys/class/power_supply/mc13892_charger/device/charger_type"
+#define SUNXI_CHARGER_TYPE_SYSFS   "/sys/class/power_supply/charger/device/charger_type"
 // Finally, we're also starting to see more standard stuff... (e.g., on Mk. 10)
-#define STD_CHARGER_TYPE_SYSFS   "/sys/class/power_supply/ac/device/charger_type"
-#define MTK_CHARGER_TYPE_SYSFS   "/sys/class/power_supply/bd71827_bat/charger_type"
+#define STD_CHARGER_TYPE_SYSFS     "/sys/class/power_supply/ac/device/charger_type"
+#define MTK_CHARGER_TYPE_SYSFS     "/sys/class/power_supply/bd71827_bat/charger_type"
+// FW 6.x renamed the bd71827 power supply from bd71827_bat to battery (same driver, same attributes)
+#define MTK_FW6_BATT_CAP_SYSFS     "/sys/class/power_supply/battery/capacity"
+#define MTK_FW6_BATT_STATUS_SYSFS  "/sys/class/power_supply/battery/status"
+#define MTK_FW6_CHARGER_TYPE_SYSFS "/sys/class/power_supply/battery/charger_type"
 // For ref., on mainline w/ @akemnade's driver: /sys/class/power_supply/rn5t618-usb/usb_type
 const char* CHARGER_TYPE_SYSFS = NULL;
 // For the weird standalone USB-C controller found on sunxi & Mk. 9...
Dominant language
C
Stars
25
Forks
7
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from koreader/KoboUSBMS

All issues in koreader/KoboUSBMS

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.