Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quirky fix to Magic Mouse 2 driver not loading when reconnecting #1

Open
wants to merge 36 commits into
base: magicmouse2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
cb8d67d
Quirky fix to Magic Mouse 2 driver not loading when reconnecting
RicardoEPRodrigues Feb 3, 2020
4b04c2b
install now gives exec permission to driver add script. Updated README.
RicardoEPRodrigues Feb 3, 2020
16edb87
Corrected udev rule.
RicardoEPRodrigues Feb 5, 2020
5dc2412
Added Vendor ID to the udev rule.
RicardoEPRodrigues Feb 5, 2020
ff2d5af
updated udev rule to take into account the mouse Vendor and Product ID.
RicardoEPRodrigues Feb 6, 2020
dfbde41
Removed extra slash (/) from UDEV and OPT paths.
RicardoEPRodrigues Feb 19, 2020
fc35cdd
Updated udev rule and Run script to avoid repeated loading of the driver
RicardoEPRodrigues May 24, 2020
8d6629f
Delay before loading new driver.
RicardoEPRodrigues May 24, 2020
4621ace
Restricted udev rule. Added load of hid-generic. Added remove-fix.sh.
RicardoEPRodrigues Jun 19, 2020
abd6876
Replaces Bluetooth restart method to use Systemctl. Adds Root check.
RicardoEPRodrigues Jul 21, 2020
0afd5fe
Add delay to mouse wheel event from drag start
tanc Sep 28, 2020
9ffcb9f
Improve Driver Loading through Modprobe and DKMS
RicardoEPRodrigues Feb 13, 2021
aa328aa
Setup VSCode workspace for Ubuntu 20.10
RicardoEPRodrigues Feb 14, 2021
aa53b84
Merge branch 'magicmouse2' into magicmouse-delay
RicardoEPRodrigues Feb 14, 2021
4b5acdb
Move delay duration to a parameter.
RicardoEPRodrigues Feb 14, 2021
c5ad14a
Change dkms version name. Update tasks available for VSCode.
RicardoEPRodrigues Feb 14, 2021
a81d708
Update remove.sh to remove modprobe configuration file. Update README.
RicardoEPRodrigues Feb 14, 2021
b6d4df0
Update driver loading script. It now uses better file locking.
RicardoEPRodrigues Feb 14, 2021
197383d
Rename /opt folder from magic-mouse-fix to magicmouse-hid
RicardoEPRodrigues Feb 21, 2021
fef19d7
Add DEB file support.
RicardoEPRodrigues Feb 21, 2021
6490593
remove amd64 from .deb file name.
RicardoEPRodrigues Feb 21, 2021
206d565
Add `.deb` file info to README.md
RicardoEPRodrigues Feb 21, 2021
0cf961c
Improve code. Move drag start to be per finger.
RicardoEPRodrigues Feb 26, 2021
2e94ebb
Add position delay. Remove time delay.
RicardoEPRodrigues Feb 26, 2021
a0d1de8
update build options
RicardoEPRodrigues Feb 26, 2021
c9644f4
Update ignore file to ignore md5sums
RicardoEPRodrigues Feb 27, 2021
69abef8
Correct variable names. Add Author and Description to module.
RicardoEPRodrigues Feb 27, 2021
dd4d31f
Bump version number
RicardoEPRodrigues Feb 27, 2021
e050c13
Apply fix from @johnchen902
RicardoEPRodrigues Jun 7, 2021
ec80ec6
Add John Chen to list of authors
RicardoEPRodrigues Jun 7, 2021
71570b7
Add ability to stop scrolling when moving the mouse.
RicardoEPRodrigues Jun 7, 2021
c21b7cc
Apply fixes from kernel 5.15. Refers to #12.
RicardoEPRodrigues Jul 16, 2021
f6d6d0d
Add reload driver instructions to README file
EltonGarcia Jul 28, 2021
c39143f
Merge pull request #13 from EltonGarcia/reload-instructions
RicardoEPRodrigues Jul 28, 2021
50efece
Update DEB file version to match new numbering.
RicardoEPRodrigues Sep 24, 2021
e82cb58
Update README.md
RicardoEPRodrigues Jun 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pkg-debian/*
!pkg-debian/DEBIAN
*.deb

pkg-debian/DEBIAN/md5sums
46 changes: 46 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}",
"/usr/include",
"/usr/src/linux-headers-5.8.0-7642-generic/include/",
"/usr/src/linux-headers-5.8.0-7642-generic/include/uapi",
"/usr/src/linux-headers-5.8.0-7642-generic/include/generated/uapi",
"/usr/src/linux-headers-5.8.0-7642-generic/arch/x86/include/",
"/usr/src/linux-headers-5.8.0-7642-generic/arch/x86/include/generated",
"/usr/src/linux-headers-5.8.0-7642-generic/arch/x86/include/uapi",
"/usr/src/linux-headers-5.8.0-7642-generic/arch/x86/include/generated/uapi",
"/usr/lib/gcc/x86_64-linux-gnu/10/include/"
],
"defines": [
"KBUILD_MODNAME=\"hello_module\"",
"__GNUC__",
"__KERNEL__",
"MODULE"
],
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
"${workspaceFolder}",
"/usr/include",
"/usr/src/linux-headers-5.8.0-7642-generic/include/",
"/usr/src/linux-headers-5.8.0-7642-generic/include/uapi",
"/usr/src/linux-headers-5.8.0-7642-generic/include/generated/uapi",
"/usr/src/linux-headers-5.8.0-7642-generic/arch/x86/include/",
"/usr/src/linux-headers-5.8.0-7642-generic/arch/x86/include/generated",
"/usr/src/linux-headers-5.8.0-7642-generic/arch/x86/include/uapi",
"/usr/src/linux-headers-5.8.0-7642-generic/arch/x86/include/generated/uapi",
"/usr/lib/gcc/x86_64-linux-gnu/10/include/"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
70 changes: 70 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Build and Clean the Magicmouse 2 Driver",
"type": "shell",
"command": "cd linux/drivers/hid && make KERNEL_SRC=LINUX_PATH && make KERNEL_SRC=LINUX_PATH clean",
"args": [],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
"label": "Build the Magicmouse 2 Driver",
"type": "shell",
"command": "cd linux/drivers/hid && make KERNEL_SRC=LINUX_PATH",
"args": [],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
"label": "Clean the Magicmouse 2 Driver",
"type": "shell",
"command": "cd linux/drivers/hid && make KERNEL_SRC=LINUX_PATH clean",
"args": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Install the Magicmouse 2 Driver (requires sudo password input)",
"type": "shell",
"command": "sudo ./install.sh",
"args": [],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
"label": "Reinstall the Magicmouse 2 Driver (requires sudo password input)",
"type": "shell",
"command": "sudo ./remove.sh && sudo ./install.sh",
"args": [],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
"label": "Uninstall the Magicmouse 2 Driver (requires sudo password input)",
"type": "shell",
"command": "sudo ./remove.sh",
"args": [],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
}
]
}
167 changes: 124 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,71 @@
# Linux Apple Magic Mouse 2 and Magic Trackpad 2 Driver

This repository contains the linux hid-magicmouse driver with Magic Trackpad 2 and Magic Mouse 2 support for Linux 4.18. For older kernels you might have to diff and backport.
> [!WARNING]
> **In kernel version 5.15 support was added for the magic mouse 2**. Meaning that this driver is only necessary for prior versions of the kernel (`<= 5.14`). If you want the mouse to "just work" either install a more recent kernel or proceed with this driver installation.
>
> Furthermore, this **repository is in Maintenance mode**, meaning that no new features are planned.

This driver is based off of the work of @robotrovsky, @svartalf and probably others.
This repository contains the Linux hid-magicmouse driver with Magic Trackpad 2 and Magic Mouse 2 support for Linux 4.18 onwards. For older kernels, you might have to diff and backport. It also contains 2 fixes to the Magic Mouse 2 regarding Bluetooth random disconnections and no scroll after a Bluetooth reconnection.

The driver is tested in combination with the xf86-libinput and xf86-mtrack driver.
This driver is based on the work of @robotrovsky, @svartalf, @0xABAD, and probably others. Thank you!

Please help to test this driver and report issues.
## Ubuntu (and derivatives)

## Apple Magic Trackpad 2
The driver supports bluetooth and USB for the trackpad. To connect the Trackpad via bluetooth, it must be clicked once after it is turned on, then the Trackpad tries to reconnect to the last paired (and trusted) connection.
A `.deb` file is now available in Releases. Get the [latest version here](https://github.com/RicardoEPRodrigues/magicmouse-hid/releases/latest).

## Apple Magic Mouse 2
The drivers supports regular mouse motion and additionally scrolling and mouse middle click. Middle click is a single finger click near the middle portion of the touch surface OR a 3 finger click anywhere on the touch surface if you put the mouse in 3 finger middle click mode (instructions on how to do this are in the installation section. If you like this, please let me know so I can make it the default). Scrolling is a single finger up or down motion anywhere on the touch surface.

## libinput
You can just use the standard xf86-libinput driver and configure it through your Window-Manager-Settings. This driver works very well, but does not support three-finger-drag, but tap-to-drag.
## Install Driver with DKMS and the two fixes.

Setup/install with:

## mTrack
An example configuration for mtrack can be found in:
```
usr/share/X11/xorg.conf.d/90-magictrackpad.conf
sudo apt-get install dkms
git clone https://github.com/RicardoEPRodrigues/Linux-Magic-Trackpad-2-Driver.git
cd Linux-Magic-Trackpad-2-Driver
chmod u+x install.sh
sudo ./install.sh
```
This configuration supports tap-to-click, two-finger-scroll and three-finger-drag. Though scrolling is not as smooth as with xf86-libinput. It can be used as starting point for your own configuration. Make sure, that you have xf86-input-mtrack-git installed and it gets loaded. You find more information about the options here: https://github.com/p2rkw/xf86-input-mtrack

## Installation with DKMS

@adam-h made a DKMS which can be used for testing:

Setup/install with:
## Uninstall Driver.

You will need a 4.18 or above kernel.

```
sudo apt-get install dkms
git clone https://github.com/rohitpid/Linux-Magic-Trackpad-2-Driver.git
cd Linux-Magic-Trackpad-2-Driver/scripts
chmod u+x install.sh
sudo ./install.sh
sudo ./remove.sh
```

If you want test out 3 finger middle click feature (please do)
## Apple Magic Trackpad 2
The driver supports Bluetooth and USB for the trackpad. To connect the Trackpad via Bluetooth, it must be clicked once after it is turned on, then the Trackpad tries to reconnect to the last paired (and trusted) connection.

```
cd Linux-Magic-Trackpad-2-Driver/linux/drivers/hid
make clean
make
sudo rmmod hid-magicmouse
sudo insmod ./hid-magicmouse.ko middle_click_3finger=1
```
## Apple Magic Mouse 2
The driver supports regular mouse motion and, additionally, scrolling and mouse middle click. Middle click is a single finger click near the middle portion of the touch surface OR a 3 finger click anywhere on the touch surface if you put the mouse in 3 finger middle click mode. Scrolling is a single finger up or down motion anywhere on the touch surface.

### Changing Parameters

Several parameters are available for you to modify to personalize the driver to your taste. These can be found in `/etc/modprobe.d/hid-magicmouse.conf` after install. Modify them and the next time the driver is loaded it will have the new values.

### Reloading the driver

After changing the parameters the driver can be reloaded using the following commands:

Remove with:
```
sudo ./remove.sh
sudo rmmod hid_magicmouse
sudo modprobe hid_magicmouse
```
Or just use regular `dkms` commands once you've added `./linux/drivers/hid`.

## Troubleshooting
## Troubleshooting (outdated)
If the driver is not working, please make sure that the correct hid-magicmouse driver gets loaded and try the following steps:

cd linux/drivers/hid
make
sudo rmmod hid_magicmouse
sudo insmod ./hid-magicmouse.ko
tail -f ~/.local/share/xorg/Xorg.0.log
```
cd linux/drivers/hid
make
sudo rmmod hid_magicmouse
sudo insmod ./hid-magicmouse.ko
tail -f ~/.local/share/xorg/Xorg.0.log
```

Now unplug the trackpad and plug it back in, to see which driver gets loaded.

## Data Layout of bluetooth packets.
## Data Layout of Bluetooth packets.

```
/* The data layout for magic mouse 2 is:
Expand Down Expand Up @@ -118,6 +116,89 @@ Now unplug the trackpad and plug it back in, to see which driver gets loaded.
*/
```



## Fixes

Below is the explanation of the 2 fixes performed when running the `install.sh` shown above. The first relates to the disconnection of the mouse over Bluetooth and will restart the Bluetooth service. The second regards the driver not being loaded when the mouse reconnects with the computer.

### Bluetooth fix

There have been many complaints of repeated and random disconnections of the Magic Mouse 2. One solution to this is to disable `eSCO mode` on the Bluetooth service as shown [in this answer](https://askubuntu.com/a/629495/297110). You can disable it like this:

```
echo 1 | sudo tee /sys/module/bluetooth/parameters/disable_esco
sudo /etc/init.d/bluetooth restart
# persist setting
echo "options bluetooth disable_esco=1" | sudo tee /etc/modprobe.d/bluetooth-tweaks.conf
```

### Driver not loading when connecting Magic Mouse 2

[0xABAD](https://github.com/0xABAD/magic-mouse-2) created a fix that loads the driver when it detects the mouse. Here we'll show an updated version that was changed a bit to use the idProduct of the device to identify any Magic Mouse 2.

To begin we need to build the driver and register it as a kernel module, please take a look at `scripts/install.sh`.

With that, we'll create a shell script that will load the driver. Let's create a folder in `/opt/` to place it into.

```bash
sudo mkdir -p /opt/magic-mouse-fix/
```

Let's create a script named `magic-mouse-2-add.sh` (to create and edit it use something like `sudo nano /opt/magic-mouse-fix/magic-mouse-2-add.sh`). This should be the contents:

```bash
#!/bin/sh

FILE=/tmp/magicmouse-driveload

reload() {
if [ ! -f "$FILE" ]; then
touch $FILE

modprobe -r hid_magicmouse
sleep 2
modprobe hid-generic
modprobe hid_magicmouse \
scroll_acceleration=1 \
scroll_speed=25 \
middle_click_3finger=1

sleep 2
rm -f "$FILE"

fi
}

reload &
```

You can also adjust the scroll_speed to a value of your liking (somewhere between 0 to 63). If you wish to disable scroll acceleration or middle-clicking with 3 fingers then set those values to zero. Give the script permission to run with `sudo chmod +x /opt/magic-mouse-fix/magic-mouse-2-add.sh`. When this script is run it will unload the default Magic Mouse driver and then load the new one built earlier.

We now need to create a `udev` rule that runs the script and loads the driver when the Mouse connects. In `/etc/udev/rules.d` directory create a `10-magicmouse.rules` file and add the following:

```
SUBSYSTEM=="input", \
KERNEL=="mouse*", \
DRIVER=="", \
SUBSYSTEMS=="hid", \
KERNELS=="0005:004C:0269*", \
DRIVERS=="hid-generic|magicmouse", \
ACTION=="add", \
SYMLINK+="input/magicmouse-%k", \
RUN+="/opt/magic-mouse-fix/magic-mouse-2-add.sh"
```

The `10-` prefix was picked arbitrarily and could be any number as it is used to determine the lexical ordering of rules in the kernel. The earlier the file is loaded guarantees that the rule will be applied before any others.

Now we need to reload the `udev` database with:

```
sudo udevadm control -R
```

With that in place, the Magic Mouse 2 will now be properly loaded with scrolling when connected via Bluetooth. Note that isn't perfect and bugs may be around.

## Thanks
* https://github.com/ponyfleisch/hid-magictrackpad2
* https://github.com/adam-h/Linux-Magic-Trackpad-2-Driver
Expand Down
33 changes: 33 additions & 0 deletions build-deb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

set -e
set -x

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ETC_DIR="/etc"
PKG_DIR="/pkg-debian"
OPT_DIR="/opt/magicmouse-hid"

DEB="magicmouse-hid_2.1.0-0.deb"

cp -rf ${DIR}${ETC_DIR} ${DIR}${PKG_DIR}

mkdir -p ${DIR}${PKG_DIR}${OPT_DIR}/scripts
cp -f ${DIR}/scripts/install.sh ${DIR}${PKG_DIR}${OPT_DIR}/scripts/install.sh
cp -f ${DIR}/scripts/remove.sh ${DIR}${PKG_DIR}${OPT_DIR}/scripts/remove.sh

# Copy source to /opt
cp -rf ${DIR}/linux ${DIR}${PKG_DIR}${OPT_DIR}/linux

# Generate MD5sums
cd ${DIR}${PKG_DIR}
find ${DIR}${PKG_DIR} -type f ! -regex '.*?debian-binary.*' ! -regex '.*?DEBIAN.*' -printf '%P ' | xargs md5sum > ${DIR}${PKG_DIR}/DEBIAN/md5sums
cd ${DIR}

# Set correct permissions
chmod 0775 ${DIR}${PKG_DIR}/DEBIAN/postinst
chmod 0775 ${DIR}${PKG_DIR}/DEBIAN/prerm

dpkg -b ${DIR}${PKG_DIR} ${DIR}/${DEB}

dpkg -I ${DIR}/${DEB}
7 changes: 7 additions & 0 deletions etc/modprobe.d/hid-magicmouse.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
options hid-magicmouse \
scroll_acceleration=1 \
stop_scroll_while_moving=1 \
scroll_speed=10 \
middle_click_3finger=1 \
scroll_delay_pos_x=300 \
scroll_delay_pos_y=250
11 changes: 11 additions & 0 deletions hid-magicmouse.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"folders": [
{
"path": "."
}
],
"settings": {
"files.associations": {
}
}
}
Loading