From a2c013b14fae2b0bcdf68eca2899f3f63c180cc3 Mon Sep 17 00:00:00 2001 From: Evan Reichard Date: Wed, 15 Oct 2025 20:38:16 -0400 Subject: [PATCH] feat: add magisk module --- .gitmodules | 2 +- Makefile | 31 +++-------------- README.md | 22 +++++++----- apatch/Makefile | 27 +++++++++++++++ lib => apatch/lib | 0 sam_hid_kbd_rem.c => apatch/sam_hid_kbd_rem.c | 0 sam_hid_kbd_rem.h => apatch/sam_hid_kbd_rem.h | 0 build/MagiskSamsungKBDOverride.zip | Bin 0 -> 792 bytes .../sam_hid_kbd_rem.kpm | Bin sam_hid_kbd_rem.patch => kernel.patch | 0 magisk/module.prop | 6 ++++ magisk/service.sh | 32 ++++++++++++++++++ 12 files changed, 84 insertions(+), 36 deletions(-) create mode 100644 apatch/Makefile rename lib => apatch/lib (100%) rename sam_hid_kbd_rem.c => apatch/sam_hid_kbd_rem.c (100%) rename sam_hid_kbd_rem.h => apatch/sam_hid_kbd_rem.h (100%) create mode 100644 build/MagiskSamsungKBDOverride.zip rename sam_hid_kbd_rem.kpm => build/sam_hid_kbd_rem.kpm (100%) rename sam_hid_kbd_rem.patch => kernel.patch (100%) create mode 100644 magisk/module.prop create mode 100755 magisk/service.sh diff --git a/.gitmodules b/.gitmodules index 6695d62..b2f2ef4 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "KernelPatch"] - path = lib + path = apatch/lib url = git@github.com:bmax121/KernelPatch.git diff --git a/Makefile b/Makefile index b939472..3d1c1df 100644 --- a/Makefile +++ b/Makefile @@ -1,27 +1,6 @@ -ifndef TARGET_COMPILE - TARGET_COMPILE = aarch64-none-elf- -endif +build_magisk: + rm -f ./build/MagiskSamsungKBDOverride.zip + cd ./magisk && zip -r ../build/MagiskSamsungKBDOverride.zip * -ifndef KP_DIR - KP_DIR = ./lib -endif - -CC = $(TARGET_COMPILE)gcc -LD = $(TARGET_COMPILE)ld - -INCLUDE_DIRS := . include patch/include linux/include linux/arch/arm64/include linux/tools/arch/arm64/include - -INCLUDE_FLAGS := $(foreach dir,$(INCLUDE_DIRS),-I$(KP_DIR)/kernel/$(dir)) - -objs := sam_hid_kbd_rem.c - -all: sam_hid_kbd_rem.kpm - -sam_hid_kbd_rem.kpm: ${objs} - ${CC} $(CFLAGS) $(INCLUDE_FLAGS) -O2 $^ -r -o $@ - - -.PHONY: clean -clean: - rm -rf *.kpm - find . -name "*.o" | xargs rm -f +build_apatch: + $(MAKE) -C ./apatch sam_hid_kbd_rem.kpm diff --git a/README.md b/README.md index 21c2cc5..eb55985 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,25 @@ -# KernelPatch / APatch - Android Generic BT Keyboard Fix +# Magisk / KernelPatch / APatch - Android Generic BT Keyboard Fix -### Description - -This is a [KernelPatch](https://github.com/bmax121/KernelPatch) / [APatch](https://github.com/bmax121/APatch) module used to fix an -issue with the Samsung Galaxy Tab S7 on some kernels where a Bluetooth keyboard connects but fails to work. +## Description The underlying issue is that some cheap keyboards report their USB Vendor ID as `0x04E8` and Product ID as `0x7021`, which is the same as a Samsung Wireless Keyboard. This causes the Samsung HID driver to claim the device, which doesn't support the keyboard properly. -This patch blocks the Samsung driver from matching these devices and prevents custom quirks from being applied, allowing the generic HID driver to handle them instead. +We implement both a KernelPatch / APatch module, as well as a Magisk module. The former is a more comprehensive fix that fully patches +out the driver inside the Linux Kernel. The latter is a Magisk module that ignores special drivers while connecting to the device. -### Installation +NOTE: The KernelPatch / APatch module will only work with APatch. The Magisk module will work for both. -Install the patch by copying the `sam_hid_kbd_rem.kpm` file to your device, opening APatch, and loading/installing the module. +### APatch Installation + +Install the patch by copying the `./build/sam_hid_kbd_rem.kpm` file to your device, opening APatch, and loading/installing the module. + +### Magisk Installation + +Install the patch by copying the `./build/MagiskSamsungKBDOverride.zip` file to your device, opening APatch or Magisk, and installing the module. ### Kernel Patch -You can also patch the kernel directly by applying the provided patch file `sam_hid_kbd_rem.patch`. At the time of writing this, the patch was +You can also patch the kernel directly by applying the provided patch file `kernel.patch`. At the time of writing this, the patch was applied to the [LineageOS/android_kernel_samsung_sm8250](https://github.com/LineageOS/android_kernel_samsung_sm8250/tree/8acbe9760ef02f0d044c0f0ec1bb469fa383fb07) at hash `8acbe9760ef02f0d044c0f0ec1bb469fa383fb07`. diff --git a/apatch/Makefile b/apatch/Makefile new file mode 100644 index 0000000..9951e3c --- /dev/null +++ b/apatch/Makefile @@ -0,0 +1,27 @@ +ifndef TARGET_COMPILE + TARGET_COMPILE = aarch64-none-elf- +endif + +ifndef KP_DIR + KP_DIR = ./lib +endif + +CC = $(TARGET_COMPILE)gcc +LD = $(TARGET_COMPILE)ld + +INCLUDE_DIRS := . include patch/include linux/include linux/arch/arm64/include linux/tools/arch/arm64/include + +INCLUDE_FLAGS := $(foreach dir,$(INCLUDE_DIRS),-I$(KP_DIR)/kernel/$(dir)) + +objs := sam_hid_kbd_rem.c + +all: sam_hid_kbd_rem.kpm + +sam_hid_kbd_rem.kpm: ${objs} + ${CC} $(CFLAGS) $(INCLUDE_FLAGS) -O2 $^ -r -o ../build/$@ + + +.PHONY: clean +clean: + rm -rf ../build/*.kpm + find . -name "../build/*.o" | xargs rm -f diff --git a/lib b/apatch/lib similarity index 100% rename from lib rename to apatch/lib diff --git a/sam_hid_kbd_rem.c b/apatch/sam_hid_kbd_rem.c similarity index 100% rename from sam_hid_kbd_rem.c rename to apatch/sam_hid_kbd_rem.c diff --git a/sam_hid_kbd_rem.h b/apatch/sam_hid_kbd_rem.h similarity index 100% rename from sam_hid_kbd_rem.h rename to apatch/sam_hid_kbd_rem.h diff --git a/build/MagiskSamsungKBDOverride.zip b/build/MagiskSamsungKBDOverride.zip new file mode 100644 index 0000000000000000000000000000000000000000..727adf0658ff502c0901cf98b8b6ec09913e1257 GIT binary patch literal 792 zcmWIWW@Zs#U|`^2Q0noI-mh02S_b6J17dCl8HU{al+v73y@I0rg3u6724(@_4;j~i zxU_J?{zT|NW5%X=qY%sylwaP0o)@Ht!gXFph!yy0O$!RC$!B^Cwz zN=tLknXU2lnr!v${`;uzH3nw!Ow)hw+a15J#QIK+WVa;$(no5EIj`5Iz3$yVqnEw> z9`o$V+ebHtPyOC}J2CXv=A&sbJ1Zij=6M`sl}l}D`|kGcrft#rb8_k3KeP6J{B!+f z+OMKdX;D)&uYI%8XyVZoYdanBlBsrbvP$z6@m8BlrqS!3C#R=3otdKb&EDx@_AdP; z2lgJ05WmNq`Oq`_w4Lxwr8fr(*C%bMTv)sy*Ie=IACL4(uj(UPndcf>IJ1B2S1@5V z&`jcUR@-tY>p{@ukG$QlV$Ys&yCm_Xaazd&{R6p5E7NYWh?+7gtz8qM8dNypO}MSS z?Yh*}p)GQ(Vt3}x6f|VypEFI0VX@2Pd5;>?qEO473+0gH_kD|}5 z-LK37ug|*jKsDv?|C8rpSf#2NZ#>{Pu=2D2${XO#$Rx*%D~U+}Qx5|JFxnWFG=f+t x362$#;Ls8qvVoWpk1+6U<8GjV$jJa`9w-@Ln8(TnvYH797Xs--U;<)b004@~DvJOB literal 0 HcmV?d00001 diff --git a/sam_hid_kbd_rem.kpm b/build/sam_hid_kbd_rem.kpm similarity index 100% rename from sam_hid_kbd_rem.kpm rename to build/sam_hid_kbd_rem.kpm diff --git a/sam_hid_kbd_rem.patch b/kernel.patch similarity index 100% rename from sam_hid_kbd_rem.patch rename to kernel.patch diff --git a/magisk/module.prop b/magisk/module.prop new file mode 100644 index 0000000..5ab0796 --- /dev/null +++ b/magisk/module.prop @@ -0,0 +1,6 @@ +id=samsung_kbd_override +name=Samsung Keyboard Override +version=1.0 +versionCode=1 +author=EvanReichard +description=Fixes Samsung keyboard driver binding issues diff --git a/magisk/service.sh b/magisk/service.sh new file mode 100755 index 0000000..e721448 --- /dev/null +++ b/magisk/service.sh @@ -0,0 +1,32 @@ +#!/system/bin/sh + +VENDOR_ID=04E8 +DEVICE_ID=7021 +DEV_DIR=/sys/bus/hid/devices/*:$VENDOR_ID:$DEVICE_ID*/ +IGNORE_PARAM=/sys/module/hid/parameters/ignore_special_drivers +LOG="[samsung_kbd_override]" + +# Wait Boot +while [ "$(getprop sys.boot_completed)" != "1" ]; do + sleep 1 +done + +# Loop Service +while sleep 1; do + if [ ! -d $DEV_DIR ]; then + continue + fi + + if [ ! -d $DEV_DIR/driver ]; then + if [ "$(cat $IGNORE_PARAM)" = "0" ]; then + echo "$LOG driver unbound -> ignoring special drivers" + echo "1" > $IGNORE_PARAM + fi + else + if [ "$(cat $IGNORE_PARAM)" = "1" ]; then + echo "$LOG driver bound -> restoring special drivers" + echo "0" > $IGNORE_PARAM + fi + fi +done +