Skip to content

Instantly share code, notes, and snippets.

@erichanson1
Last active June 1, 2026 09:16
Show Gist options
  • Select an option

  • Save erichanson1/4d4298f7f5bd6d4476803263cba493cd to your computer and use it in GitHub Desktop.

Select an option

Save erichanson1/4d4298f7f5bd6d4476803263cba493cd to your computer and use it in GitHub Desktop.
Rebuilding Kernel Object ( hid-magicmouse )

Summary

Whilst the Apple Magic Mouse is supported by Ubuntu, I've been struggling a bit with it, as by default it is configured as a 3 button mouse and rather than left clicking, I keep finding myself middle clicking and not getting the response I want.

The actual purpose of this gist is to show how to recompile a Kernel module and integrate it in to the build, rather more than fixing my Magic Mouse issue.

Pre-requisites

Create a directory to hold the source code ( the suggested default location is /usr/src ).

mkdir /usr/src
cd /usr/src

Obtaining the Kernel Source

apt-get source linux-image-unsigned-$(uname -r)

My system returned

apt-get source linux-image-$(uname -r)
Reading package lists... Done
E: You must put some 'deb-src' URIs in your sources.list

As Ubuntu 25.10 (Questing Quokka) uses the newer sources (DEB822-STYLE FORMAT) style apt configuration file, adding the deb-src type to the file ubuntu.sources should be enough Make a backup of the original file first. cp -p /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources~

I then edited /etc/apt/sources.list.d/ubuntu.sources to read

Types: deb deb-src
URIs: http://gb.archive.ubuntu.com/ubuntu/
Suites: questing questing-updates questing-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

Types: deb deb-src
URIs: http://security.ubuntu.com/ubuntu/
Suites: questing-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

Then I ran apt-get update to ensure that the changes are registered by apt.

I was then able to run apt-get source linux-image-unsigned-$(uname -r) in my /usr/src directory

However, what I really need is the source for the Kernel Module /usr/lib/modules/6.17.0-8-generic/kernel/drivers/hid/hid-magicmouse.ko.zst (which confusingly is actually /lib/modules/6.17.0-1005-oracle/kernel/drivers/hid/hid-magicmouse.ko.zst )

dpkg-query -S '/lib/modules/6.17.0-8-generic/kernel/drivers/hid/hid-magicmouse.ko.zst'
linux-modules-6.17.0-8-generic: /lib/modules/6.17.0-8-generic/kernel/drivers/hid/hid-magicmouse.ko.zst

# or

apt-file search hid-magicmouse
linux-modules-6.17.0-1001-realtime: /lib/modules/6.17.0-1001-realtime/kernel/drivers/hid/hid-magicmouse.ko.zst
...
linux-modules-6.17.0-7-generic: /lib/modules/6.17.0-7-generic/kernel/drivers/hid/hid-magicmouse.ko.zst
linux-modules-6.17.0-8-generic: /lib/modules/6.17.0-8-generic/kernel/drivers/hid/hid-magicmouse.ko.zst
apt-get source linux-modules-6.17.0-8-generic
Reading package lists... Done
Picking 'linux' as source package instead of 'linux-modules-6.17.0-8-generic'
NOTICE: 'linux' packaging is maintained in the 'Git' version control system at:
git://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/questing
Please use:
git clone git://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/questing
to retrieve the latest (possibly unreleased) updates to the package.
Need to get 250 MB of source archives.
Get:1 http://gb.archive.ubuntu.com/ubuntu questing-updates/main linux 6.17.0-8.8 (dsc) [8,747 B]
Get:2 http://gb.archive.ubuntu.com/ubuntu questing-updates/main linux 6.17.0-8.8 (tar) [249 MB]
Get:3 http://gb.archive.ubuntu.com/ubuntu questing-updates/main linux 6.17.0-8.8 (diff) [1,405 kB]
Fetched 250 MB in 60s (4,177 kB/s)
....

The source code for hid-magicmouse is in /usr/src/linux-6.17.0/drivers/hid/hid-magicmouse.c

I edited the file ( I commented out the original start/stop co-ordinates for the middle mouse button, and set them both to 0, as we currently emulate a 3 button mouse as well ). I can always put them back later on if I need them! I then ran ( from the base directory of the modules)

/usr/src/linux-6.17.0# make -C /lib/modules/$(uname -r)/build M=$(pwd)  drivers/hid/hid-magicmouse.ko
make: Entering directory '/usr/src/linux-headers-6.17.0-8-generic'
make[1]: Entering directory '/usr/src/linux-6.17.0'
warning: the compiler differs from the one used to build the kernel
  The kernel was built by: x86_64-linux-gnu-gcc (Ubuntu 15.2.0-4ubuntu4) 15.2.0
  You are using:           gcc (Ubuntu 15.2.0-4ubuntu4) 15.2.0
  CC [M]  drivers/hid/hid-magicmouse.o
  MODPOST Module.symvers
  CC [M]  drivers/hid/hid-magicmouse.mod.o
  LD [M]  drivers/hid/hid-magicmouse.ko
  BTF [M] drivers/hid/hid-magicmouse.ko
Skipping BTF generation for drivers/hid/hid-magicmouse.ko due to unavailability of vmlinux
make[1]: Leaving directory '/usr/src/linux-6.17.0'
make: Leaving directory '/usr/src/linux-headers-6.17.0-8-generic'

This concerned me slightly as it appears that the Kernel will only load signed modules, and I've not created a signed module

Performing a Google Search

The "Skipping BTF generation for drivers due to unavailability of vmlinux" error means your system can't find the full kernel image (vmlinux) needed to debug/sign kernel modules, often seen when building drivers (like NVIDIA, VirtualBox, or custom ones) after a kernel update; solutions involve installing pahole, ensuring vmlinux is in /sys/kernel/btf (or symlinked), disabling Secure Boot, or updating your kernel headers/DKMS.

Solutions Install pahole: This tool is essential for BTF generation. sudo apt install pahole (Debian/Ubuntu) or sudo dnf install pahole (Fedora/RHEL). Check/Create vmlinux: Look for vmlinux in /sys/kernel/btf/. If present, try creating a symlink: sudo ln -s /sys/kernel/btf/vmlinux /lib/modules/$(uname -r)/build/

I followed the above instructions,

apt install pahole
ln -s /sys/kernel/btf/vmlinux /lib/modules/$(uname -r)/build/

Then removed the previous kernel object created, and re-ran make

/usr/src/linux-6.17.0# rm drivers/hid/hid-magicmouse.ko
/usr/src/linux-6.17.0# make -C /lib/modules/$(uname -r)/build M=$(pwd)  drivers/hid/hid-magicmouse.ko
make: Entering directory '/usr/src/linux-headers-6.17.0-8-generic'
make[1]: Entering directory '/src/linux-6.17.0'
warning: the compiler differs from the one used to build the kernel
  The kernel was built by: x86_64-linux-gnu-gcc (Ubuntu 15.2.0-4ubuntu4) 15.2.0
  You are using:           gcc (Ubuntu 15.2.0-4ubuntu4) 15.2.0
  MODPOST Module.symvers
  LD [M]  drivers/hid/hid-magicmouse.ko
  BTF [M] drivers/hid/hid-magicmouse.ko
make[1]: Leaving directory '/usr/src/linux-6.17.0'
make: Leaving directory '/usr/src/linux-headers-6.17.0-8-generic'
zstd -19 drivers/hid/hid-magicmouse.ko

/usr/src/linux-6.17.0# depmod -A

Test what we've just built

lsmod | grep magic
hid_magicmouse         24576  0
hid                   270336  9 usbhid,hid_apple,apple_ib_als,hid_sensor_hub,snd_soc_sdca,hid_generic,apple_ibridge,uhid,hid_magicmouse

modprobe -r hid_magicmouse
modprobe -vv -i ./drivers/hid/hid-magicmouse.ko.zst
modprobe: INFO: custom logging function 0x5894c1b19c50 registered
insmod /usr/src/linux-6.17.0/./drivers/hid/hid-magicmouse.ko.zst
modprobe: INFO: context 0x5894dc24b510 released

lsmod | grep magic
hid_magicmouse         24576  0
hid                   270336  9 usbhid,hid_apple,apple_ib_als,hid_sensor_hub,snd_soc_sdca,hid_generic,apple_ibridge,uhid,hid_magicmouse

So far so good. I also tested the mouse and it seemed functional. Create a backup of the original, and then put the new module in place.

cp -rp /lib/modules/6.17.0-8-generic/kernel/drivers/hid/hid-magicmouse.ko.zst /root/
cp ./drivers/hid/hid-magicmouse.ko.zst /lib/modules/6.17.0-8-generic/kernel/drivers/hid/
ls -l /lib/modules/6.17.0-8-generic/kernel/drivers/hid/hid-magicmouse*
# check that ownership and permissions match 644 root:root 
depmod -A

modprobe -r hid_magicmouse
modprobe -i -vv hid-magicmouse
modprobe: INFO: custom logging function 0x571b8f281c50 registered
insmod /lib/modules/6.17.0-8-generic/kernel/drivers/hid/hid-magicmouse.ko.zst

dracut --regenerate-all --force --hostonly
lsinitrd | grep magic
-rw-r--r--   1 root     root        48744 Jan  5 16:48 usr/lib/modules/6.17.0-8-generic/kernel/drivers/hid/hid-magicmouse.ko.zst

reboot

It worked.


Disabling Horizontal Scrolling.

I asked CoPilot how to disable horizontal scrolling in the file hid-magicmouse.c CoPilot suggested

Option 1: Patch the driver to ignore horizontal events

You can comment out or remove these lines in magicmouse_emit_touch:

if (step_x != 0) {
    msc->touches[id].scroll_x -= step_x *
        (64 - scroll_speed) * msc->scroll_accel;
    msc->scroll_jiffies = now;
    // input_report_rel(input, REL_HWHEEL, -step_x); // <--- comment or remove this line
}

...

if (step_x_hr != 0 &&
    msc->touches[id].scroll_x_active) {
    msc->touches[id].scroll_x_hr -= step_x_hr *
        step_hr;
    // input_report_rel(input, REL_HWHEEL_HI_RES, -step_x_hr * SCROLL_HR_MULT); // <--- comment or remove this line
}

Option 2: Patch the event registration

You could also prevent the driver from registering that it is capable of horizontal scrolling at all. In magicmouse_setup_input:

if (emulate_scroll_wheel) {
    __set_bit(REL_WHEEL, input->relbit);
    __set_bit(REL_HWHEEL, input->relbit);       // <--- Remove or comment this line
    __set_bit(REL_WHEEL_HI_RES, input->relbit);
    __set_bit(REL_HWHEEL_HI_RES, input->relbit); // <--- Remove or comment this line
}

However, if the emission code still runs, it just won't deliver events. Option 1 is safer.

I decided to go with Option 1, so I commented out the code. ( In my case it was lines 305 and lines 328 to 330 )

cd /usr/src/linux-6.17.0/drivers/hid
vi hid-magicmouse.c
cd ../..
make -C /lib/modules/$(uname -r)/build M=$(pwd)  drivers/hid/hid-magicmouse.ko

make: Entering directory '/usr/src/linux-headers-6.17.0-8-generic'
make[1]: Entering directory '/usr/src/linux-6.17.0'
warning: the compiler differs from the one used to build the kernel
  The kernel was built by: x86_64-linux-gnu-gcc (Ubuntu 15.2.0-4ubuntu4) 15.2.0
  You are using:           gcc (Ubuntu 15.2.0-4ubuntu4) 15.2.0
  CC [M]  drivers/hid/hid-magicmouse.o
  MODPOST Module.symvers
  CC [M]  drivers/hid/hid-magicmouse.mod.o
  LD [M]  drivers/hid/hid-magicmouse.ko
  BTF [M] drivers/hid/hid-magicmouse.ko
make[1]: Leaving directory '/usr/src/linux-6.17.0'
make: Leaving directory '/usr/src/linux-headers-6.17.0-8-generic'

modprobe -r hid_magicmouse
modprobe -vv -i ./drivers/hid/hid-magicmouse.ko

modprobe -vv -i ./drivers/hid/hid-magicmouse.ko
modprobe: INFO: custom logging function 0x5b8195cbec50 registered
insmod /usr/src/linux-6.17.0/./drivers/hid/hid-magicmouse.ko 
modprobe: INFO: context 0x5b81d0e3f520 released

dracut --regenerate-all --force --hostonly
lsinitrd | grep magic
-rw-r--r--   1 root     root        48544 Jan 13 17:29 usr/lib/modules/6.17.0-8-generic/kernel/drivers/hid/hid-magicmouse.ko
...

reboot

It worked. I tested to see if the mouse was still working, it was. Using this gist, I tested if Vertical Scrolling was still working - it was. I also tested (using some of the code snippets in this gist) whether Horizontal Scrolling was still working - it wasn't. So far so good.

cp ./drivers/hid/hid-magicmouse.ko /lib/modules/6.17.0-8-generic/kernel/drivers/hid/
ls -l /lib/modules/6.17.0-8-generic/kernel/drivers/hid/hid-magicmouse*
# check that ownership and permissions match
depmod -A

modprobe -r hid_magicmouse
modprobe -i -vv hid-magicmouse

modprobe -i -vv hid_magicmouse
modprobe: INFO: custom logging function 0x57cbfae68c50 registered
insmod /lib/modules/6.17.0-8-generic/kernel/drivers/hid/hid-magicmouse.ko 
modprobe: INFO: context 0x57cc090f2520 released


---
# Useful Links
- https://askubuntu.com/questions/515407/how-recipe-to-build-only-one-kernel-module
- https://stackoverflow.com/questions/8744087/how-to-recompile-just-a-single-kernel-module
- 
@erichanson1

erichanson1 commented Feb 22, 2026

Copy link
Copy Markdown
Author

I still find that I "right click" more often than I intend to, so in order to move the right click further to the right, I modified the middle click co-ordinates further

From around line 28 of drivers/hid/hid-magicmouse.c

static int middle_button_start = 500;
static int middle_button_stop = 500;

I'll see if that works.
( The code suggests that the Horizontal Scroll goes from -1100 to 1258 - a total of 2358, a third of which is 786. 1258 - 786 ~ 500.
So 2/3 of the width goes from -1100 to + 500, the remainder goes to the right hand button. )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment