Sometimes the need arises to apply a patch to a kernel module for testing purposes or to fix a regression.
The below example uses kernel 5.10 as this was the procedure I followed for a non-working Bluetooth adapter on kernel 5.10 prior to 5.10.7-2.
Install needed packages for building and source control
pamac install base-devel git
Install matching kernel headers
pamac install linux510-headers
Clone matching repository for manjaro kernel
mkdir ~/MyKernel
#Where you obviously change '~/MyMernel' to whatever directory you wish the kernel sources to be cloned into
cd ~/MyKernel
git clone https://gitlab.manjaro.org/packages/core/linux510.git
cd linux510/
Optionally one has to git checkout (or git reset --hard to) a former commit here as the development on the linux510 repository continues and might interfere with our testing (especially if you are on a slower moving branch like testing or stable):
git reset commitHash --hard
#Where onbviously 'commitHash' is the exact git commit you want to revert to.
This step is important, especially if you see error messages like this when loading the built kernel module:
modprobe: ERROR: could not insert 'btusb': Exec format error
Create new patch file with needed contents
In the below example we’re using 0900-btusb.patch as patch-extension to make life easier for us later on:
https://bugzilla.kernel.org/show_bug.cgi?id=210681#c10
Bradley Jarvis
Avoid returning error code when bluetooth version match is not made from qca_devices_table and version high is set.
--- a/drivers/bluetooth/btusb.c
+++ a/drivers/bluetooth/btusb.c
@@ -4070,6 +4070,7 @@ static int btusb_setup_qca(struct hci_dev *hdev)
}
if (!info) {
bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
+ if (ver_rom & ~0xffffU) return 0;
return -ENODEV;
}
Adjust PKGBUILD
Just for reference one might want to take a look at the arch wiki.
add our 0900-btusb.patch to end of source array:
source=(
...
# Temp Fixes
'0900-btusb.patch'
)
add (sha256-)checksum or SKIP to the end of sha256sums array:
The patch cannot be applied, probably because the checked out linux510 kernel and file to patch changed since the original tutorial.
This is not a tutorial to get some bluetooth adapter working, this is a tutorial on “how to build a patched kernel module” when there is a proper/matching patch.
Please open a new thread for your problem, post the contents of the rejects (drivers/bluetooth/btusb.c.rej).
What is this $srcDir you used in your tutorial ?
I am using your guide to build a patched kernel module for tplink UB500 bluetooth adapter, I run srcDir=src/linux-5.14 because I am running the 5.14 kernel, the command runs fine.
Then I do ln -s /usr/lib/modules/5.14.10-1-MANJARO/build/Module.symvers "$srcDir/" , it runs as well.
Then I do zcat /proc/config.gz > "$srcDir/.config" it shows error permission denied even when using sudo. I ran it under su and then it ran fine.
Now when I run make -C "$srcDir" modules_prepare it shows make: the '-C' option requires a non-empty string argument Usage: make [options] [target] ... . Please tell me what did I do wrong ? I am not knowlegeble enough to figure this out on my own. Also I did a detailed post of what I did and what went wrong while following your tutorial : patching kernel module to support tplink UB500. Please help me out here.
EDIT :-
I figured it out, it was my foolishness that I was facing those problems. I now have the module in place but when I do modprobe btusb I get this error modprobe: ‘ERROR: could not insert ‘btusb’: Exec format error’
Well after your reply I re-did EVERYTHING from scratch in your tutorial, I double checked to make sure I was building the module against the kernel 5.14 which is my currently running kernel. Here is a complete dump of what happend in the terminal. Please see if you find anything. The error modprobe: ERROR: could not insert 'btusb': Exec format error still shows up.
mkdir kernelbuild
cd kernelbuild
git clone https://gitlab.manjaro.org/packages/core/linux514.git
cd linux514
nano 0900-btusb.patch
sha256sum 0900-btusb.patch
nano PKGBUILD
makepkg --nobuild
srcDir=src/linux-5.14
ln -s /usr/lib/modules/5.14.10-1-MANJARO/build/Module.symvers "$srcDir/"
zcat /proc/config.gz > "$srcDir/.config"
make -C "$srcDir" modules_prepare
make --directory="$srcDir" M=drivers/bluetooth/
ls -l "$srcDir/drivers/bluetooth/btusb.ko"
sudo mv /usr/lib/modules/5.14.10-1-MANJARO/kernel/drivers/bluetooth/btusb.ko.xz /usr/lib/modules/5.14.10-1-MANJARO/kernel/drivers/bluetooth/btusb.ko.xz.backup
sudo cp "$srcDir/drivers/bluetooth/btusb.ko" /usr/lib/modules/5.14.10-1-MANJARO/kernel/drivers/bluetooth/
sudo depmod -a
sudo modprobe -r btusb
sudo modprobe btusb
When building the modules I saw this, it’s skipping the BTF generation for drivers due to unavailability of vmlinux hope this helps :
CC [M] drivers/bluetooth/ath3k.mod.o
LD [M] drivers/bluetooth/ath3k.ko
BTF [M] drivers/bluetooth/ath3k.ko
Skipping BTF generation for drivers/bluetooth/ath3k.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/bcm203x.mod.o
LD [M] drivers/bluetooth/bcm203x.ko
BTF [M] drivers/bluetooth/bcm203x.ko
Skipping BTF generation for drivers/bluetooth/bcm203x.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/bfusb.mod.o
LD [M] drivers/bluetooth/bfusb.ko
BTF [M] drivers/bluetooth/bfusb.ko
Skipping BTF generation for drivers/bluetooth/bfusb.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/bluecard_cs.mod.o
LD [M] drivers/bluetooth/bluecard_cs.ko
BTF [M] drivers/bluetooth/bluecard_cs.ko
Skipping BTF generation for drivers/bluetooth/bluecard_cs.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/bpa10x.mod.o
LD [M] drivers/bluetooth/bpa10x.ko
BTF [M] drivers/bluetooth/bpa10x.ko
Skipping BTF generation for drivers/bluetooth/bpa10x.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/bt3c_cs.mod.o
LD [M] drivers/bluetooth/bt3c_cs.ko
BTF [M] drivers/bluetooth/bt3c_cs.ko
Skipping BTF generation for drivers/bluetooth/bt3c_cs.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/btbcm.mod.o
LD [M] drivers/bluetooth/btbcm.ko
BTF [M] drivers/bluetooth/btbcm.ko
Skipping BTF generation for drivers/bluetooth/btbcm.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/btintel.mod.o
LD [M] drivers/bluetooth/btintel.ko
BTF [M] drivers/bluetooth/btintel.ko
Skipping BTF generation for drivers/bluetooth/btintel.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/btmrvl.mod.o
LD [M] drivers/bluetooth/btmrvl.ko
BTF [M] drivers/bluetooth/btmrvl.ko
Skipping BTF generation for drivers/bluetooth/btmrvl.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/btmrvl_sdio.mod.o
LD [M] drivers/bluetooth/btmrvl_sdio.ko
BTF [M] drivers/bluetooth/btmrvl_sdio.ko
Skipping BTF generation for drivers/bluetooth/btmrvl_sdio.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/btmtksdio.mod.o
LD [M] drivers/bluetooth/btmtksdio.ko
BTF [M] drivers/bluetooth/btmtksdio.ko
Skipping BTF generation for drivers/bluetooth/btmtksdio.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/btmtkuart.mod.o
LD [M] drivers/bluetooth/btmtkuart.ko
BTF [M] drivers/bluetooth/btmtkuart.ko
Skipping BTF generation for drivers/bluetooth/btmtkuart.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/btqca.mod.o
LD [M] drivers/bluetooth/btqca.ko
BTF [M] drivers/bluetooth/btqca.ko
Skipping BTF generation for drivers/bluetooth/btqca.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/btrsi.mod.o
LD [M] drivers/bluetooth/btrsi.ko
BTF [M] drivers/bluetooth/btrsi.ko
Skipping BTF generation for drivers/bluetooth/btrsi.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/btrtl.mod.o
LD [M] drivers/bluetooth/btrtl.ko
BTF [M] drivers/bluetooth/btrtl.ko
Skipping BTF generation for drivers/bluetooth/btrtl.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/btsdio.mod.o
LD [M] drivers/bluetooth/btsdio.ko
BTF [M] drivers/bluetooth/btsdio.ko
Skipping BTF generation for drivers/bluetooth/btsdio.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/btusb.mod.o
LD [M] drivers/bluetooth/btusb.ko
BTF [M] drivers/bluetooth/btusb.ko
Skipping BTF generation for drivers/bluetooth/btusb.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/dtl1_cs.mod.o
LD [M] drivers/bluetooth/dtl1_cs.ko
BTF [M] drivers/bluetooth/dtl1_cs.ko
Skipping BTF generation for drivers/bluetooth/dtl1_cs.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/hci_nokia.mod.o
LD [M] drivers/bluetooth/hci_nokia.ko
BTF [M] drivers/bluetooth/hci_nokia.ko
Skipping BTF generation for drivers/bluetooth/hci_nokia.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/hci_uart.mod.o
LD [M] drivers/bluetooth/hci_uart.ko
BTF [M] drivers/bluetooth/hci_uart.ko
Skipping BTF generation for drivers/bluetooth/hci_uart.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/hci_vhci.mod.o
LD [M] drivers/bluetooth/hci_vhci.ko
BTF [M] drivers/bluetooth/hci_vhci.ko
Skipping BTF generation for drivers/bluetooth/hci_vhci.ko due to unavailability of vmlinux
CC [M] drivers/bluetooth/virtio_bt.mod.o
LD [M] drivers/bluetooth/virtio_bt.ko
BTF [M] drivers/bluetooth/virtio_bt.ko
Skipping BTF generation for drivers/bluetooth/virtio_bt.ko due to unavailability of vmlinux
make: Leaving directory '/home/sensei/kernelbuild/linux514/src/linux-5.14'