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 0000000..727adf0 Binary files /dev/null and b/build/MagiskSamsungKBDOverride.zip differ 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 +