From cb8d67d2f227975d6f204ba589bc927af7fef11c Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Mon, 3 Feb 2020 22:30:51 +0000 Subject: [PATCH 01/34] Quirky fix to Magic Mouse 2 driver not loading when reconnecting This fix is based on the work of [0xABAD](https://github.com/0xABAD/magic-mouse-2), but I changed his method of detecting the mouse by searching for the Product ID and not depend on the unit codes. --- install.sh | 38 ++++++++++++++++++++++++++++++++ scripts/magic-mouse-2-add.sh | 11 +++++++++ udev/rules.d/10-magicmouse.rules | 5 +++++ 3 files changed, 54 insertions(+) create mode 100755 install.sh create mode 100755 scripts/magic-mouse-2-add.sh create mode 100644 udev/rules.d/10-magicmouse.rules diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..c0c1fe7 --- /dev/null +++ b/install.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +set -e +set -x + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +OPT="/opt/magic-mouse-fix/" +UDEV="/etc/udev/rules.d/" + +# Install drive through DKMS +chmod u+x ${DIR}/scripts/install.sh +${DIR}/scripts/install.sh + +# Build drive to be used for the Bluetooth fix +cd ${DIR}/linux/drivers/hid +make clean +make +cd ${DIR} + +# Copy `.ko` and script to activate it to OPT directory +mkdir -p ${OPT} +cp -f ${DIR}/linux/drivers/hid/hid-magicmouse.ko ${OPT}/hid-magicmouse.ko +cp -f ${DIR}/scripts/magic-mouse-2-add.sh ${OPT}/magic-mouse-2-add.sh + +# Copy udev rule and reload udev +cp -f ${DIR}/udev/rules.d/10-magicmouse.rules ${UDEV}/10-magicmouse.rules +udevadm control -R + +# Disable eSCO mode in Bluetooth to fix disconnection problems with the mouse +echo 1 | tee /sys/module/bluetooth/parameters/disable_esco +/etc/init.d/bluetooth restart +# persist setting +echo "options bluetooth disable_esco=1" | tee /etc/modprobe.d/bluetooth-tweaks.conf + +# Clean driver build +cd ${DIR}/linux/drivers/hid +make clean +cd ${DIR} \ No newline at end of file diff --git a/scripts/magic-mouse-2-add.sh b/scripts/magic-mouse-2-add.sh new file mode 100755 index 0000000..1714a3a --- /dev/null +++ b/scripts/magic-mouse-2-add.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +reload() { + modprobe -r hid_magicmouse + insmod /opt/magic-mouse-fix/hid-magicmouse.ko \ + scroll_acceleration=1 \ + scroll_speed=25 \ + middle_click_3finger=1 +} + +reload & diff --git a/udev/rules.d/10-magicmouse.rules b/udev/rules.d/10-magicmouse.rules new file mode 100644 index 0000000..e43e84d --- /dev/null +++ b/udev/rules.d/10-magicmouse.rules @@ -0,0 +1,5 @@ +SUBSYSTEMS=="input", \ + ATTRS{idProduct}=="e300" + ACTION=="add", \ + SYMLINK+="input/magicmouse", \ + RUN+="/opt/magic-mouse-fix/magic-mouse-2-add.sh" From 4b04c2b755129342b1018a740d23d54230445f9d Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Mon, 3 Feb 2020 23:32:12 +0000 Subject: [PATCH 02/34] install now gives exec permission to driver add script. Updated README. README now has information on the new script and on how the fixes work. --- README.md | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- install.sh | 1 + 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c9c2ba9..f5e6630 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,25 @@ # 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. +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. It also contains 2 fixes to the Magic Mouse 2 regarding Bluetooth random disconnections and this driver not loading on bluetooth reconnection. -This driver is based off of the work of @robotrovsky, @svartalf and probably others. +This driver is based off of the work of @robotrovsky, @svartalf, @ 0xABAD and probably others. The driver is tested in combination with the xf86-libinput and xf86-mtrack driver. Please help to test this driver and report issues. +## Install Driver with DKMS and the two fixes. + +Setup/install with: + +``` + 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 +``` + ## 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. @@ -118,6 +130,76 @@ Now unplug the trackpad and plug it back in, to see which driver gets loaded. */ ``` + + +## Fixes + +Below is the explanation to 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 complaining 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 we'll move it to `/opt/magic-mouse-fix/`: + +``` +cd Link-Magic-Trackpad-2-Driver/linux/drivers/hid +make clean +make +# Create the folder +sudo mkdir -p /opt/magic-mouse-fix/ +sudo cp -f hid-magicmouse.ko /opt/magic-mouse-fix/hid-magicmouse.ko +``` + +With that we'll create a shell script that will load the driver. Let's create it at `/opt/magic-mouse-fix/` and name it `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 its contents: + +``` +#!/bin/sh + +reload() { + modprobe -r hid_magicmouse + insmod /opt/magic-mouse-fix/hid-magicmouse.ko \ + scroll_acceleration=1 \ + scroll_speed=25 \ + middle_click_3finger=1 +} + +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 eariler. + +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: + +``` +SUBSYSTEMS=="input", \ + ATTRS{idProduct}=="e300" + ACTION=="add", \ + SYMLINK+="input/magicmouse", \ + 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. Note that it loads the driver anytime an input device is detected with an idProduct of `e300`, which should be the code for a Magic Mouse 2, but more testing should be done. + +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 sometimes the kernel will attempt to reload the driver several times and may a few seconds. + ## Thanks * https://github.com/ponyfleisch/hid-magictrackpad2 * https://github.com/adam-h/Linux-Magic-Trackpad-2-Driver diff --git a/install.sh b/install.sh index c0c1fe7..d924da1 100755 --- a/install.sh +++ b/install.sh @@ -21,6 +21,7 @@ cd ${DIR} mkdir -p ${OPT} cp -f ${DIR}/linux/drivers/hid/hid-magicmouse.ko ${OPT}/hid-magicmouse.ko cp -f ${DIR}/scripts/magic-mouse-2-add.sh ${OPT}/magic-mouse-2-add.sh +chmod +x ${OPT}/magic-mouse-2-add.sh # Copy udev rule and reload udev cp -f ${DIR}/udev/rules.d/10-magicmouse.rules ${UDEV}/10-magicmouse.rules From 16edb87264af06dfac7c9fbc7248e82d36f6e82d Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Wed, 5 Feb 2020 10:26:04 +0000 Subject: [PATCH 03/34] Corrected udev rule. When an USB with that product ID is detected then a input driver is loaded. I think this is the correct interpretation. --- README.md | 4 ++-- udev/rules.d/10-magicmouse.rules | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f5e6630..b0db69d 100644 --- a/README.md +++ b/README.md @@ -183,8 +183,8 @@ You can also adjust the scroll_speed to a value of your liking (somewhere betwee 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: ``` -SUBSYSTEMS=="input", \ - ATTRS{idProduct}=="e300" +SUBSYSTEMS=="usb", \ + ATTRS{idProduct}=="e300", \ ACTION=="add", \ SYMLINK+="input/magicmouse", \ RUN+="/opt/magic-mouse-fix/magic-mouse-2-add.sh" diff --git a/udev/rules.d/10-magicmouse.rules b/udev/rules.d/10-magicmouse.rules index e43e84d..5b55774 100644 --- a/udev/rules.d/10-magicmouse.rules +++ b/udev/rules.d/10-magicmouse.rules @@ -1,5 +1,5 @@ -SUBSYSTEMS=="input", \ - ATTRS{idProduct}=="e300" +SUBSYSTEMS=="usb", \ + ATTRS{idProduct}=="e300", \ ACTION=="add", \ SYMLINK+="input/magicmouse", \ RUN+="/opt/magic-mouse-fix/magic-mouse-2-add.sh" From 5dc24126991de9ee8f2a3becfb9d89743d1a49e2 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Wed, 5 Feb 2020 14:58:17 +0000 Subject: [PATCH 04/34] Added Vendor ID to the udev rule. --- README.md | 3 ++- udev/rules.d/10-magicmouse.rules | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b0db69d..4c4c922 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ 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. It also contains 2 fixes to the Magic Mouse 2 regarding Bluetooth random disconnections and this driver not loading on bluetooth reconnection. -This driver is based off of the work of @robotrovsky, @svartalf, @ 0xABAD and probably others. +This driver is based off of the work of @robotrovsky, @svartalf, @0xABAD and probably others. The driver is tested in combination with the xf86-libinput and xf86-mtrack driver. @@ -184,6 +184,7 @@ We now need to create a `udev` rule that runs the script and loads the driver wh ``` SUBSYSTEMS=="usb", \ + ATTRS{idVendor}=="0cf3", \ ATTRS{idProduct}=="e300", \ ACTION=="add", \ SYMLINK+="input/magicmouse", \ diff --git a/udev/rules.d/10-magicmouse.rules b/udev/rules.d/10-magicmouse.rules index 5b55774..4d5fa7e 100644 --- a/udev/rules.d/10-magicmouse.rules +++ b/udev/rules.d/10-magicmouse.rules @@ -1,4 +1,5 @@ SUBSYSTEMS=="usb", \ + ATTRS{idVendor}=="0cf3", \ ATTRS{idProduct}=="e300", \ ACTION=="add", \ SYMLINK+="input/magicmouse", \ From ff2d5afb55a5e645d3b6dee795a9eb23a5f0da45 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Thu, 6 Feb 2020 13:22:02 +0000 Subject: [PATCH 05/34] updated udev rule to take into account the mouse Vendor and Product ID. Previously the rule used the USB ID of the computer and not the mouse. --- README.md | 10 +++++----- udev/rules.d/10-magicmouse.rules | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4c4c922..aa05f9a 100644 --- a/README.md +++ b/README.md @@ -183,15 +183,15 @@ You can also adjust the scroll_speed to a value of your liking (somewhere betwee 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: ``` -SUBSYSTEMS=="usb", \ - ATTRS{idVendor}=="0cf3", \ - ATTRS{idProduct}=="e300", \ +SUBSYSTEM=="input", \ + KERNEL=="mouse*", \ + KERNELS=="0005:004C:0269*", \ ACTION=="add", \ - SYMLINK+="input/magicmouse", \ + 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. Note that it loads the driver anytime an input device is detected with an idProduct of `e300`, which should be the code for a Magic Mouse 2, but more testing should be done. +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: diff --git a/udev/rules.d/10-magicmouse.rules b/udev/rules.d/10-magicmouse.rules index 4d5fa7e..06b24ad 100644 --- a/udev/rules.d/10-magicmouse.rules +++ b/udev/rules.d/10-magicmouse.rules @@ -1,6 +1,6 @@ -SUBSYSTEMS=="usb", \ - ATTRS{idVendor}=="0cf3", \ - ATTRS{idProduct}=="e300", \ +SUBSYSTEM=="input", \ + KERNEL=="mouse*", \ + KERNELS=="0005:004C:0269*", \ ACTION=="add", \ - SYMLINK+="input/magicmouse", \ + SYMLINK+="input/magicmouse-%k", \ RUN+="/opt/magic-mouse-fix/magic-mouse-2-add.sh" From dfbde41a4863a95b22db5f38a4b15eb195deb2de Mon Sep 17 00:00:00 2001 From: Ricardo Rodrigues Date: Wed, 19 Feb 2020 10:55:11 +0000 Subject: [PATCH 06/34] Removed extra slash (/) from UDEV and OPT paths. --- install.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/install.sh b/install.sh index d924da1..288cfbe 100755 --- a/install.sh +++ b/install.sh @@ -4,8 +4,8 @@ set -e set -x DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -OPT="/opt/magic-mouse-fix/" -UDEV="/etc/udev/rules.d/" +OPT="/opt/magic-mouse-fix" +UDEV="/etc/udev/rules.d" # Install drive through DKMS chmod u+x ${DIR}/scripts/install.sh @@ -36,4 +36,4 @@ echo "options bluetooth disable_esco=1" | tee /etc/modprobe.d/bluetooth-tweaks.c # Clean driver build cd ${DIR}/linux/drivers/hid make clean -cd ${DIR} \ No newline at end of file +cd ${DIR} From fc35cdd14e83a70bf0f7596c8d4b8406e3a19ae7 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Sun, 24 May 2020 01:03:07 +0100 Subject: [PATCH 07/34] Updated udev rule and Run script to avoid repeated loading of the driver Also added a smaller install script that does not install dkms and does not install the bluetooth fix. --- install-fix.sh | 30 ++++++++++++++++++++++++++++++ scripts/magic-mouse-2-add.sh | 20 +++++++++++++++----- udev/rules.d/10-magicmouse.rules | 1 + 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100755 install-fix.sh diff --git a/install-fix.sh b/install-fix.sh new file mode 100755 index 0000000..eee9d4a --- /dev/null +++ b/install-fix.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +set -e +set -x + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +OPT="/opt/magic-mouse-fix" +UDEV="/etc/udev/rules.d" + +# Build drive to be used for the Bluetooth fix +cd ${DIR}/linux/drivers/hid +make clean +make +cd ${DIR} + +# Copy `.ko` and script to activate it to OPT directory +mkdir -p ${OPT} +cp -f ${DIR}/linux/drivers/hid/hid-magicmouse.ko ${OPT}/hid-magicmouse.ko +cp -f ${DIR}/scripts/magic-mouse-2-add.sh ${OPT}/magic-mouse-2-add.sh +chmod +x ${OPT}/magic-mouse-2-add.sh + +# Copy udev rule and reload udev +cp -f ${DIR}/udev/rules.d/10-magicmouse.rules ${UDEV}/10-magicmouse.rules +udevadm control -R +/etc/init.d/bluetooth restart + +# Clean driver build +cd ${DIR}/linux/drivers/hid +make clean +cd ${DIR} \ No newline at end of file diff --git a/scripts/magic-mouse-2-add.sh b/scripts/magic-mouse-2-add.sh index 1714a3a..da9e4cd 100755 --- a/scripts/magic-mouse-2-add.sh +++ b/scripts/magic-mouse-2-add.sh @@ -1,11 +1,21 @@ #!/bin/sh +FILE=/tmp/magicmouse-driveload + reload() { - modprobe -r hid_magicmouse - insmod /opt/magic-mouse-fix/hid-magicmouse.ko \ - scroll_acceleration=1 \ - scroll_speed=25 \ - middle_click_3finger=1 + if [ ! -f "$FILE" ]; then + touch $FILE + + modprobe -r hid_magicmouse + insmod /opt/magic-mouse-fix/hid-magicmouse.ko \ + scroll_acceleration=1 \ + scroll_speed=25 \ + middle_click_3finger=1 + + sleep 2 + rm -f "$FILE" + + fi } reload & diff --git a/udev/rules.d/10-magicmouse.rules b/udev/rules.d/10-magicmouse.rules index 06b24ad..96af28e 100644 --- a/udev/rules.d/10-magicmouse.rules +++ b/udev/rules.d/10-magicmouse.rules @@ -1,5 +1,6 @@ SUBSYSTEM=="input", \ KERNEL=="mouse*", \ + SUBSYSTEMS=="hid", \ KERNELS=="0005:004C:0269*", \ ACTION=="add", \ SYMLINK+="input/magicmouse-%k", \ From 8d6629ff8e1caacd59a61db853bc56b1c8e3e0ad Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Sun, 24 May 2020 11:04:12 +0100 Subject: [PATCH 08/34] Delay before loading new driver. Added a delay after removing the previous driver and before adding the new one --- scripts/magic-mouse-2-add.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/magic-mouse-2-add.sh b/scripts/magic-mouse-2-add.sh index da9e4cd..bd4a244 100755 --- a/scripts/magic-mouse-2-add.sh +++ b/scripts/magic-mouse-2-add.sh @@ -7,6 +7,7 @@ reload() { touch $FILE modprobe -r hid_magicmouse + sleep 2 insmod /opt/magic-mouse-fix/hid-magicmouse.ko \ scroll_acceleration=1 \ scroll_speed=25 \ From 4621aced09dc98ea79cb24ec64d5137efde6c1c6 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Fri, 19 Jun 2020 10:56:40 +0100 Subject: [PATCH 09/34] Restricted udev rule. Added load of hid-generic. Added remove-fix.sh. --- remove-fix.sh | 16 ++++++++++++++++ scripts/magic-mouse-2-add.sh | 1 + udev/rules.d/10-magicmouse.rules | 2 ++ 3 files changed, 19 insertions(+) create mode 100755 remove-fix.sh diff --git a/remove-fix.sh b/remove-fix.sh new file mode 100755 index 0000000..be413ee --- /dev/null +++ b/remove-fix.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +set -e +set -x + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +OPT="/opt/magic-mouse-fix" +UDEV="/etc/udev/rules.d" + +# Copy `.ko` and script to activate it to OPT directory +rm -rf ${OPT} + +# Remove the udev rule and reload udev +rm ${UDEV}/10-magicmouse.rules +udevadm control -R +/etc/init.d/bluetooth restart diff --git a/scripts/magic-mouse-2-add.sh b/scripts/magic-mouse-2-add.sh index bd4a244..0690db8 100755 --- a/scripts/magic-mouse-2-add.sh +++ b/scripts/magic-mouse-2-add.sh @@ -7,6 +7,7 @@ reload() { touch $FILE modprobe -r hid_magicmouse + modprobe -a hid-generic sleep 2 insmod /opt/magic-mouse-fix/hid-magicmouse.ko \ scroll_acceleration=1 \ diff --git a/udev/rules.d/10-magicmouse.rules b/udev/rules.d/10-magicmouse.rules index 96af28e..8dfd5e8 100644 --- a/udev/rules.d/10-magicmouse.rules +++ b/udev/rules.d/10-magicmouse.rules @@ -1,7 +1,9 @@ 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" From abd68763cf87276350bf2564747e776424f11cfd Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Tue, 21 Jul 2020 20:07:13 +0100 Subject: [PATCH 10/34] Replaces Bluetooth restart method to use Systemctl. Adds Root check. for newer systems bluetooth restart now is done with systemctl. It also adds a small check to detect root. If not it asks to run it as sudo. --- install-fix.sh | 7 ++++++- install.sh | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/install-fix.sh b/install-fix.sh index eee9d4a..7efcf9d 100755 --- a/install-fix.sh +++ b/install-fix.sh @@ -1,5 +1,10 @@ #!/bin/bash +if [ "$EUID" -ne 0 ] + then echo "Please run as root" + exit +fi + set -e set -x @@ -22,7 +27,7 @@ chmod +x ${OPT}/magic-mouse-2-add.sh # Copy udev rule and reload udev cp -f ${DIR}/udev/rules.d/10-magicmouse.rules ${UDEV}/10-magicmouse.rules udevadm control -R -/etc/init.d/bluetooth restart +systemctl restart bluetooth # Clean driver build cd ${DIR}/linux/drivers/hid diff --git a/install.sh b/install.sh index 288cfbe..d1918fd 100755 --- a/install.sh +++ b/install.sh @@ -1,5 +1,10 @@ #!/bin/bash +if [ "$EUID" -ne 0 ] + then echo "Please run as root" + exit +fi + set -e set -x @@ -29,7 +34,7 @@ udevadm control -R # Disable eSCO mode in Bluetooth to fix disconnection problems with the mouse echo 1 | tee /sys/module/bluetooth/parameters/disable_esco -/etc/init.d/bluetooth restart +systemctl restart bluetooth # persist setting echo "options bluetooth disable_esco=1" | tee /etc/modprobe.d/bluetooth-tweaks.conf From 0afd5fe6993f30555d80a5650d8ba163cf7f8298 Mon Sep 17 00:00:00 2001 From: Tancredi D'Onofrio Date: Mon, 28 Sep 2020 09:39:42 +0200 Subject: [PATCH 11/34] Add delay to mouse wheel event from drag start This adds a 1/5 of a second delay from the registration of a drag event to actually starting to scroll. The result is a slight decrease in sensitivity when dragging a finger on the Magic Mouse to start a scroll. The actual scrolling is the same as before, there is a just a small delay at the beginning of the stroke which helps to eliminate accidental scrolls when just resting the finger lightly on the mouse surface. --- linux/drivers/hid/hid-magicmouse.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/linux/drivers/hid/hid-magicmouse.c b/linux/drivers/hid/hid-magicmouse.c index 1745a64..cef3707 100644 --- a/linux/drivers/hid/hid-magicmouse.c +++ b/linux/drivers/hid/hid-magicmouse.c @@ -121,6 +121,7 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie * @ntouches: Number of touches in most recent touch report. * @scroll_accel: Number of consecutive scroll motions. * @scroll_jiffies: Time of last scroll motion. + * @drag_start: Time of drag start. * @touches: Most recent data for a touch, indexed by tracking ID. * @tracking_ids: Mapping of current touch input data to @touches. */ @@ -131,6 +132,7 @@ struct magicmouse_sc { int ntouches; int scroll_accel; unsigned long scroll_jiffies; + unsigned long drag_start; struct { short x; @@ -314,6 +316,8 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, msc->touches[id].scroll_x = x; msc->touches[id].scroll_y = y; + msc->drag_start = now; + /* Reset acceleration after half a second. */ if (scroll_acceleration && time_before(now, msc->scroll_jiffies + HZ / 2)) @@ -324,7 +328,19 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, break; case TOUCH_STATE_DRAG: - step_x /= (64 - (int)scroll_speed) * msc->scroll_accel; + /* Add a fifth of a second delay since the drag start in which + * drag events are not registered. This decreases the + * sensitivity of dragging on Magic Mouse devices. + */ + if (time_before(now, msc->drag_start + HZ / 5)) { + step_x = 0; + step_y = 0; + } + else { + step_x /= (64 - (int)scroll_speed) * msc->scroll_accel; + step_y /= (64 - (int)scroll_speed) * msc->scroll_accel; + } + if (step_x != 0) { msc->touches[id].scroll_x -= step_x * (64 - scroll_speed) * msc->scroll_accel; @@ -332,7 +348,6 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, input_report_rel(input, REL_HWHEEL, -step_x); } - step_y /= (64 - (int)scroll_speed) * msc->scroll_accel; if (step_y != 0) { msc->touches[id].scroll_y -= step_y * (64 - scroll_speed) * msc->scroll_accel; From 9ffcb9f3e9d812345750d8e6732002175071bca6 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Sat, 13 Feb 2021 21:12:47 +0000 Subject: [PATCH 12/34] Improve Driver Loading through Modprobe and DKMS This update simplifies the code. Please run `remove.sh` before updating the driver to ensure a smooth transition. Removed unnecessary files from repo. Changes were made to install, the main loading script to make use of modprobe more accurately. The parameters for hid-magicmouse were moved to /etc/modprobe.d/hid-magicmouse.conf . This allows the unloading and loading of the module with predefined parameters. A `remove.sh` script was added to uninstall the driver. NOTE: It does not remove the bluetooth fix. Changed DKMS version to reflect the driver name. --- etc/modprobe.d/hid-magicmouse.conf | 4 +++ .../udev}/rules.d/10-magicmouse.rules | 0 install-fix.sh | 35 ------------------- install.sh | 35 ++++++++----------- linux/drivers/hid/dkms.conf | 2 +- remove-fix.sh => remove.sh | 8 +++-- scripts/magic-mouse-2-add.sh | 6 +--- .../X11/xorg.conf.d/90-magictrackpad.conf | 35 ------------------- 8 files changed, 26 insertions(+), 99 deletions(-) create mode 100644 etc/modprobe.d/hid-magicmouse.conf rename {udev => etc/udev}/rules.d/10-magicmouse.rules (100%) delete mode 100755 install-fix.sh rename remove-fix.sh => remove.sh (63%) delete mode 100644 usr/share/X11/xorg.conf.d/90-magictrackpad.conf diff --git a/etc/modprobe.d/hid-magicmouse.conf b/etc/modprobe.d/hid-magicmouse.conf new file mode 100644 index 0000000..dc344df --- /dev/null +++ b/etc/modprobe.d/hid-magicmouse.conf @@ -0,0 +1,4 @@ +options hid-magicmouse \ + scroll_acceleration=1 \ + scroll_speed=25 \ + middle_click_3finger=1 \ No newline at end of file diff --git a/udev/rules.d/10-magicmouse.rules b/etc/udev/rules.d/10-magicmouse.rules similarity index 100% rename from udev/rules.d/10-magicmouse.rules rename to etc/udev/rules.d/10-magicmouse.rules diff --git a/install-fix.sh b/install-fix.sh deleted file mode 100755 index 7efcf9d..0000000 --- a/install-fix.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash - -if [ "$EUID" -ne 0 ] - then echo "Please run as root" - exit -fi - -set -e -set -x - -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -OPT="/opt/magic-mouse-fix" -UDEV="/etc/udev/rules.d" - -# Build drive to be used for the Bluetooth fix -cd ${DIR}/linux/drivers/hid -make clean -make -cd ${DIR} - -# Copy `.ko` and script to activate it to OPT directory -mkdir -p ${OPT} -cp -f ${DIR}/linux/drivers/hid/hid-magicmouse.ko ${OPT}/hid-magicmouse.ko -cp -f ${DIR}/scripts/magic-mouse-2-add.sh ${OPT}/magic-mouse-2-add.sh -chmod +x ${OPT}/magic-mouse-2-add.sh - -# Copy udev rule and reload udev -cp -f ${DIR}/udev/rules.d/10-magicmouse.rules ${UDEV}/10-magicmouse.rules -udevadm control -R -systemctl restart bluetooth - -# Clean driver build -cd ${DIR}/linux/drivers/hid -make clean -cd ${DIR} \ No newline at end of file diff --git a/install.sh b/install.sh index d1918fd..0ba1bb0 100755 --- a/install.sh +++ b/install.sh @@ -9,36 +9,29 @@ set -e set -x DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -OPT="/opt/magic-mouse-fix" -UDEV="/etc/udev/rules.d" +OPT_DIR="/opt/magic-mouse-fix" +UDEV_DIR="/etc/udev/rules.d" +MODPROBE_DIR="/etc/modprobe.d" + + +# Copy Modprobe config file +cp -f ${DIR}${MODPROBE_DIR}/hid-magicmouse.conf ${MODPROBE_DIR}/hid-magicmouse.conf # Install drive through DKMS chmod u+x ${DIR}/scripts/install.sh ${DIR}/scripts/install.sh -# Build drive to be used for the Bluetooth fix -cd ${DIR}/linux/drivers/hid -make clean -make -cd ${DIR} - -# Copy `.ko` and script to activate it to OPT directory -mkdir -p ${OPT} -cp -f ${DIR}/linux/drivers/hid/hid-magicmouse.ko ${OPT}/hid-magicmouse.ko -cp -f ${DIR}/scripts/magic-mouse-2-add.sh ${OPT}/magic-mouse-2-add.sh -chmod +x ${OPT}/magic-mouse-2-add.sh +# Copy script to load the driver to OPT directory +mkdir -p ${OPT_DIR} +cp -f ${DIR}/scripts/magic-mouse-2-add.sh ${OPT_DIR}/magic-mouse-2-add.sh +chmod +x ${OPT_DIR}/magic-mouse-2-add.sh # Copy udev rule and reload udev -cp -f ${DIR}/udev/rules.d/10-magicmouse.rules ${UDEV}/10-magicmouse.rules +cp -f ${DIR}${UDEV_DIR}/10-magicmouse.rules ${UDEV_DIR}/10-magicmouse.rules udevadm control -R # Disable eSCO mode in Bluetooth to fix disconnection problems with the mouse echo 1 | tee /sys/module/bluetooth/parameters/disable_esco systemctl restart bluetooth -# persist setting -echo "options bluetooth disable_esco=1" | tee /etc/modprobe.d/bluetooth-tweaks.conf - -# Clean driver build -cd ${DIR}/linux/drivers/hid -make clean -cd ${DIR} +# persist eSCO mode in Bluetooth setting +echo "options bluetooth disable_esco=1" | tee /etc/modprobe.d/bluetooth-tweaks.conf \ No newline at end of file diff --git a/linux/drivers/hid/dkms.conf b/linux/drivers/hid/dkms.conf index a601f98..b39fbf7 100644 --- a/linux/drivers/hid/dkms.conf +++ b/linux/drivers/hid/dkms.conf @@ -3,6 +3,6 @@ CLEAN="make KERNEL_VERSION=${kernelver} clean" BUILT_MODULE_NAME[0]=hid-magicmouse DEST_MODULE_LOCATION[0]='/kernel/drivers/hid' PACKAGE_NAME=hid-magicmouse-dkms -PACKAGE_VERSION=4.18+magictrackpad2 +PACKAGE_VERSION=4.18+magicmouse2 REMAKE_INITRD=yes AUTOINSTALL=yes diff --git a/remove-fix.sh b/remove.sh similarity index 63% rename from remove-fix.sh rename to remove.sh index be413ee..76d8ed5 100755 --- a/remove-fix.sh +++ b/remove.sh @@ -7,10 +7,14 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" OPT="/opt/magic-mouse-fix" UDEV="/etc/udev/rules.d" +# Remove drive through DKMS +chmod u+x ${DIR}/scripts/remove.sh +${DIR}/scripts/remove.sh + # Copy `.ko` and script to activate it to OPT directory rm -rf ${OPT} # Remove the udev rule and reload udev -rm ${UDEV}/10-magicmouse.rules +rm -f ${UDEV}/10-magicmouse.rules udevadm control -R -/etc/init.d/bluetooth restart +systemctl restart bluetooth diff --git a/scripts/magic-mouse-2-add.sh b/scripts/magic-mouse-2-add.sh index 0690db8..f7f95a5 100755 --- a/scripts/magic-mouse-2-add.sh +++ b/scripts/magic-mouse-2-add.sh @@ -7,12 +7,8 @@ reload() { touch $FILE modprobe -r hid_magicmouse - modprobe -a hid-generic sleep 2 - insmod /opt/magic-mouse-fix/hid-magicmouse.ko \ - scroll_acceleration=1 \ - scroll_speed=25 \ - middle_click_3finger=1 + modprobe -a hid-generic hid_magicmouse sleep 2 rm -f "$FILE" diff --git a/usr/share/X11/xorg.conf.d/90-magictrackpad.conf b/usr/share/X11/xorg.conf.d/90-magictrackpad.conf deleted file mode 100644 index afd1b22..0000000 --- a/usr/share/X11/xorg.conf.d/90-magictrackpad.conf +++ /dev/null @@ -1,35 +0,0 @@ -Section "InputClass" - Identifier "Touchpads" - Driver "mtrack" - MatchProduct "Trackpad" - MatchDevicePath "/dev/input/event*" - # options... - Option "Sensitivity" "0.55" - Option "FingerHigh" "10" - Option "FingerLow" "10" - Option "TapButton1" "1" - Option "TapButton2" "3" - Option "TapButton3" "2" - Option "TapButton4" "0" - Option "ButtonIntegrated" "true" - Option "ClickTime" "25" - Option "ScrollDistance" "75" - Option "ScrollSmooth" "1" - Option "TapDragEnable" "false" - -# natural scroll - Option "ScrollDownButton" "4" - Option "ScrollUpButton" "5" - Option "ScrollLeftButton" "7" - Option "ScrollRightButton" "6" - - -# three finger drag: - Option "SwipeDistance" "1" - Option "SwipeLeftButton" "1" - Option "SwipeRightButton" "1" - Option "SwipeUpButton" "1" - Option "SwipeDownButton" "1" - Option "SwipeClickTime" "0" - Option "SwipeSensitivity" "1000" -EndSection From aa328aa6ed7016fe12de4a3be40089b80574099b Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Sun, 14 Feb 2021 01:18:33 +0000 Subject: [PATCH 13/34] Setup VSCode workspace for Ubuntu 20.10 --- .vscode/c_cpp_properties.json | 46 +++++++++++++++++++++++++++++++++++ .vscode/tasks.json | 46 +++++++++++++++++++++++++++++++++++ hid-magicmouse.code-workspace | 11 +++++++++ 3 files changed, 103 insertions(+) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/tasks.json create mode 100644 hid-magicmouse.code-workspace diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..04fee6a --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -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 +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..6408fa6 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,46 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "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 + } + }, + { + "label": "Uninstall the Magicmouse 2 Driver (requires sudo password input)", + "type": "shell", + "command": "sudo ./remove.sh", + "args": [], + "group": { + "kind": "build", + "isDefault": true + } + } + ] +} diff --git a/hid-magicmouse.code-workspace b/hid-magicmouse.code-workspace new file mode 100644 index 0000000..0700333 --- /dev/null +++ b/hid-magicmouse.code-workspace @@ -0,0 +1,11 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": { + "files.associations": { + } + } +} \ No newline at end of file From 4b5acdb4f74e47d93e3a177842e6dc16e036024c Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Sun, 14 Feb 2021 02:26:42 +0000 Subject: [PATCH 14/34] Move delay duration to a parameter. Also add parameter definition to conf file. --- etc/modprobe.d/hid-magicmouse.conf | 3 ++- linux/drivers/hid/hid-magicmouse.c | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/etc/modprobe.d/hid-magicmouse.conf b/etc/modprobe.d/hid-magicmouse.conf index dc344df..0e93b3b 100644 --- a/etc/modprobe.d/hid-magicmouse.conf +++ b/etc/modprobe.d/hid-magicmouse.conf @@ -1,4 +1,5 @@ options hid-magicmouse \ scroll_acceleration=1 \ scroll_speed=25 \ - middle_click_3finger=1 \ No newline at end of file + middle_click_3finger=1 \ + scroll_delay=50 \ No newline at end of file diff --git a/linux/drivers/hid/hid-magicmouse.c b/linux/drivers/hid/hid-magicmouse.c index cef3707..511e7a5 100644 --- a/linux/drivers/hid/hid-magicmouse.c +++ b/linux/drivers/hid/hid-magicmouse.c @@ -51,6 +51,19 @@ static int param_set_scroll_speed(const char *val, module_param_call(scroll_speed, param_set_scroll_speed, param_get_uint, &scroll_speed, 0644); MODULE_PARM_DESC(scroll_speed, "Scroll speed, value from 0 (slow) to 63 (fast)"); +// HZ (time constant for 1 second) is 250. Meaning that 50 is a quarter of that time (200ms). +static unsigned int scroll_delay = 50; +static int param_set_scroll_delay(const char *val, + const struct kernel_param *kp) { + unsigned long delay; + if (!val || kstrtoul(val, 0, &delay) || delay > 1000) + return -EINVAL; + scroll_delay = delay; + return 0; +} +module_param_call(scroll_delay, param_set_scroll_delay, param_get_uint, &scroll_delay, 0644); +MODULE_PARM_DESC(scroll_delay, "Scroll delay, value from 0 (immediate) to 1000 (very late scroll)"); + static bool scroll_acceleration = false; module_param(scroll_acceleration, bool, 0644); MODULE_PARM_DESC(scroll_acceleration, "Accelerate sequential scroll events"); @@ -332,7 +345,7 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, * drag events are not registered. This decreases the * sensitivity of dragging on Magic Mouse devices. */ - if (time_before(now, msc->drag_start + HZ / 5)) { + if (time_before(now, msc->drag_start + scroll_delay)) { step_x = 0; step_y = 0; } From c5ad14afbdfcccaca15298f725b9e8ab11edf7b3 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Sun, 14 Feb 2021 02:28:08 +0000 Subject: [PATCH 15/34] Change dkms version name. Update tasks available for VSCode. --- .vscode/tasks.json | 11 +++++++++++ scripts/install.sh | 2 +- scripts/remove.sh | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 6408fa6..0a5b39e 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -1,6 +1,17 @@ { "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", diff --git a/scripts/install.sh b/scripts/install.sh index 678e6cd..1b6b4a6 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -4,7 +4,7 @@ set -e set -x dkms_name="hid-magicmouse-dkms" -dkms_version="4.18+magictrackpad2" +dkms_version="4.18+magicmouse2" DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # add diff --git a/scripts/remove.sh b/scripts/remove.sh index 046adf8..b33e4b9 100755 --- a/scripts/remove.sh +++ b/scripts/remove.sh @@ -3,7 +3,7 @@ set -e dkms_name="hid-magicmouse-dkms" -dkms_version="4.18+magictrackpad2" +dkms_version="4.18+magicmouse2" if dkms status -m $dkms_name -v $dkms_version | egrep '(added|built|installed)' >/dev/null ; then # if dkms bindings exist, remove them From a81d708a2fb8bbc8143eeddef8aea7710d58e226 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Sun, 14 Feb 2021 03:02:27 +0000 Subject: [PATCH 16/34] Update remove.sh to remove modprobe configuration file. Update README. --- README.md | 107 ++++++++++++++++++++++-------------------------------- remove.sh | 14 +++++-- 2 files changed, 54 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index aa05f9a..afde9e0 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,6 @@ This repository contains the linux hid-magicmouse driver with Magic Trackpad 2 a This driver is based off of the work of @robotrovsky, @svartalf, @0xABAD and probably others. -The driver is tested in combination with the xf86-libinput and xf86-mtrack driver. - Please help to test this driver and report issues. ## Install Driver with DKMS and the two fixes. @@ -13,69 +11,40 @@ Please help to test this driver and report issues. Setup/install with: ``` - 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 +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 ``` -## 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. +## Uninstall Driver. -## 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. - -## mTrack -An example configuration for mtrack can be found in: ``` -usr/share/X11/xorg.conf.d/90-magictrackpad.conf +sudo ./remove.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: +## 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. -You will need a 4.18 or above kernel. +## 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. Scrolling is a single finger up or down motion anywhere on the touch surface. -``` - 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 -``` +### Changing Parameters -If you want test out 3 finger middle click feature (please do) +Several parameters are avaliable 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. -``` - 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 -``` +## Troubleshooting (outdated) +If the driver is not working, please make sure that the correct hid-magicmouse driver gets loaded and try the following steps: -Remove with: ``` - sudo ./remove.sh +cd linux/drivers/hid +make +sudo rmmod hid_magicmouse +sudo insmod ./hid-magicmouse.ko +tail -f ~/.local/share/xorg/Xorg.0.log ``` -Or just use regular `dkms` commands once you've added `./linux/drivers/hid`. - -## Troubleshooting -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 Now unplug the trackpad and plug it back in, to see which driver gets loaded. @@ -151,28 +120,37 @@ echo "options bluetooth disable_esco=1" | sudo tee /etc/modprobe.d/bluetooth-twe [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 we'll move it to `/opt/magic-mouse-fix/`: +To begin we need to build the driver and register it has a kernel module, please take a look at `scripts/install.sh`. -``` -cd Link-Magic-Trackpad-2-Driver/linux/drivers/hid -make clean -make -# Create the folder +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/ -sudo cp -f hid-magicmouse.ko /opt/magic-mouse-fix/hid-magicmouse.ko ``` -With that we'll create a shell script that will load the driver. Let's create it at `/opt/magic-mouse-fix/` and name it `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 its contents: +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 its contents: -``` +```bash #!/bin/sh +FILE=/tmp/magicmouse-driveload + reload() { - modprobe -r hid_magicmouse - insmod /opt/magic-mouse-fix/hid-magicmouse.ko \ + 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 & @@ -185,7 +163,10 @@ We now need to create a `udev` rule that runs the script and loads the driver wh ``` 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" @@ -199,7 +180,7 @@ 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 sometimes the kernel will attempt to reload the driver several times and may a few seconds. +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 diff --git a/remove.sh b/remove.sh index 76d8ed5..f1fa442 100755 --- a/remove.sh +++ b/remove.sh @@ -4,17 +4,23 @@ set -e set -x DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -OPT="/opt/magic-mouse-fix" -UDEV="/etc/udev/rules.d" +OPT_DIR="/opt/magic-mouse-fix" +UDEV_DIR="/etc/udev/rules.d" +MODPROBE_DIR="/etc/modprobe.d" # Remove drive through DKMS chmod u+x ${DIR}/scripts/remove.sh ${DIR}/scripts/remove.sh +# Remove Modprobe configuration file +rm -f ${MODPROBE_DIR}/hid-magicmouse.conf + # Copy `.ko` and script to activate it to OPT directory -rm -rf ${OPT} +rm -rf ${OPT_DIR} # Remove the udev rule and reload udev -rm -f ${UDEV}/10-magicmouse.rules +rm -f ${UDEV_DIR}/10-magicmouse.rules udevadm control -R + +# Restart Bluetooth systemctl restart bluetooth From b6d4df0ee38f8851e6ba90839ca18b985f4131c5 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Sun, 14 Feb 2021 23:45:23 +0000 Subject: [PATCH 17/34] Update driver loading script. It now uses better file locking. --- scripts/magic-mouse-2-add.sh | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/scripts/magic-mouse-2-add.sh b/scripts/magic-mouse-2-add.sh index f7f95a5..48fc368 100755 --- a/scripts/magic-mouse-2-add.sh +++ b/scripts/magic-mouse-2-add.sh @@ -1,19 +1,20 @@ #!/bin/sh -FILE=/tmp/magicmouse-driveload +FILE=/tmp/magicmouse.lock reload() { - if [ ! -f "$FILE" ]; then - touch $FILE - - modprobe -r hid_magicmouse - sleep 2 - modprobe -a hid-generic hid_magicmouse - - sleep 2 - rm -f "$FILE" - + # Check is Lock File exists, if not create it and set trap on exit + if { set -C; 2>/dev/null >$FILE; }; then + trap "rm -f $FILE" EXIT + else + # Lock file exists. exiting. + exit fi + + modprobe -r hid_magicmouse + sleep 2 + modprobe -a hid-generic hid_magicmouse + sleep 2 } reload & From 197383dbc18eafecc46d91db5b702f9ff9109bc1 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Sun, 21 Feb 2021 02:14:12 +0000 Subject: [PATCH 18/34] Rename /opt folder from magic-mouse-fix to magicmouse-hid --- etc/udev/rules.d/10-magicmouse.rules | 2 +- install.sh | 2 +- remove.sh | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/etc/udev/rules.d/10-magicmouse.rules b/etc/udev/rules.d/10-magicmouse.rules index 8dfd5e8..fa97432 100644 --- a/etc/udev/rules.d/10-magicmouse.rules +++ b/etc/udev/rules.d/10-magicmouse.rules @@ -6,4 +6,4 @@ SUBSYSTEM=="input", \ DRIVERS=="hid-generic|magicmouse", \ ACTION=="add", \ SYMLINK+="input/magicmouse-%k", \ - RUN+="/opt/magic-mouse-fix/magic-mouse-2-add.sh" + RUN+="/opt/magicmouse-hid/magic-mouse-2-add.sh" diff --git a/install.sh b/install.sh index 0ba1bb0..5b46cdd 100755 --- a/install.sh +++ b/install.sh @@ -9,7 +9,7 @@ set -e set -x DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -OPT_DIR="/opt/magic-mouse-fix" +OPT_DIR="/opt/magicmouse-hid" UDEV_DIR="/etc/udev/rules.d" MODPROBE_DIR="/etc/modprobe.d" diff --git a/remove.sh b/remove.sh index f1fa442..cb15154 100755 --- a/remove.sh +++ b/remove.sh @@ -4,7 +4,7 @@ set -e set -x DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -OPT_DIR="/opt/magic-mouse-fix" +OPT_DIR="/opt/magicmouse-hid" UDEV_DIR="/etc/udev/rules.d" MODPROBE_DIR="/etc/modprobe.d" From fef19d757bcaab0e810cf7e550fc5c325bdf8687 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Sun, 21 Feb 2021 03:21:47 +0000 Subject: [PATCH 19/34] Add DEB file support. You can now generate a debian file for the project by running `./build-deb.sh`. --- .gitignore | 3 +++ build-deb.sh | 39 +++++++++++++++++++++++++++++++++++++ pkg-debian/DEBIAN/conffiles | 1 + pkg-debian/DEBIAN/control | 13 +++++++++++++ pkg-debian/DEBIAN/md5sums | 15 ++++++++++++++ pkg-debian/DEBIAN/postinst | 20 +++++++++++++++++++ pkg-debian/DEBIAN/prerm | 14 +++++++++++++ 7 files changed, 105 insertions(+) create mode 100644 .gitignore create mode 100755 build-deb.sh create mode 100644 pkg-debian/DEBIAN/conffiles create mode 100644 pkg-debian/DEBIAN/control create mode 100644 pkg-debian/DEBIAN/md5sums create mode 100755 pkg-debian/DEBIAN/postinst create mode 100755 pkg-debian/DEBIAN/prerm diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..884b9e5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +pkg-debian/* +!pkg-debian/DEBIAN +*.deb diff --git a/build-deb.sh b/build-deb.sh new file mode 100755 index 0000000..d42f3da --- /dev/null +++ b/build-deb.sh @@ -0,0 +1,39 @@ +#!/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_1.0.0-0_amd64.deb" + +cp -rf ${DIR}${ETC_DIR} ${DIR}${PKG_DIR} + +mkdir -p ${DIR}${PKG_DIR}${OPT_DIR} +# Copy scripts to /opt +cp -f ${DIR}/scripts/magic-mouse-2-add.sh ${DIR}${PKG_DIR}${OPT_DIR}/magic-mouse-2-add.sh +# cp -f ${DIR}/install.sh ${DIR}${PKG_DIR}${OPT_DIR}/install.sh +# cp -f ${DIR}/remove.sh ${DIR}${PKG_DIR}${OPT_DIR}/remove.sh + +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} \ No newline at end of file diff --git a/pkg-debian/DEBIAN/conffiles b/pkg-debian/DEBIAN/conffiles new file mode 100644 index 0000000..c8c51ba --- /dev/null +++ b/pkg-debian/DEBIAN/conffiles @@ -0,0 +1 @@ +/etc/modprobe.d/hid-magicmouse.conf diff --git a/pkg-debian/DEBIAN/control b/pkg-debian/DEBIAN/control new file mode 100644 index 0000000..66c7d0b --- /dev/null +++ b/pkg-debian/DEBIAN/control @@ -0,0 +1,13 @@ +Package: magicmouse-hid +Version: 1.0.0-0 +Maintainer: Ricardo Rodrigues +Description: Magic Mouse 2 driver for Linux + This driver makes use of DKMS to load the driver in the kernel, while + making use of udev rules to load the driver when a mouse is connected. +Homepage: https://github.com/RicardoEPRodrigues/Linux-Magic-Trackpad-2-Driver +Architecture: all +Depends: dkms, bash +Section: utils +Priority: optional +Essential: no +Installed-Size: 354 diff --git a/pkg-debian/DEBIAN/md5sums b/pkg-debian/DEBIAN/md5sums new file mode 100644 index 0000000..cfdb339 --- /dev/null +++ b/pkg-debian/DEBIAN/md5sums @@ -0,0 +1,15 @@ +4e98e3ddfe2f2677c4ba7d40160ad805 opt/magicmouse-hid/scripts/remove.sh +19394e43a8b68454dd1ecb0b053b1eb4 opt/magicmouse-hid/scripts/install.sh +9d8311a258e4a5f2d21621c510d2865b opt/magicmouse-hid/magic-mouse-2-add.sh +a01b7f00714f6281bf2489654825d920 opt/magicmouse-hid/linux/linux/drivers/hid/Makefile +db55932808cb9137512a48a5f10e91ca opt/magicmouse-hid/linux/linux/drivers/hid/dkms.conf +65e911f8a2c968a242bdeea3483822e6 opt/magicmouse-hid/linux/linux/drivers/hid/hid-quirks.c +09cc6d34174bd1d0b1f81bd28954524f opt/magicmouse-hid/linux/linux/drivers/hid/hid-magicmouse.c +2cc66b539552c6edd592484e3e1d5ba3 opt/magicmouse-hid/linux/linux/drivers/hid/hid-ids.h +a01b7f00714f6281bf2489654825d920 opt/magicmouse-hid/linux/drivers/hid/Makefile +db55932808cb9137512a48a5f10e91ca opt/magicmouse-hid/linux/drivers/hid/dkms.conf +65e911f8a2c968a242bdeea3483822e6 opt/magicmouse-hid/linux/drivers/hid/hid-quirks.c +09cc6d34174bd1d0b1f81bd28954524f opt/magicmouse-hid/linux/drivers/hid/hid-magicmouse.c +2cc66b539552c6edd592484e3e1d5ba3 opt/magicmouse-hid/linux/drivers/hid/hid-ids.h +44c561036e33a298dcf6729bdf475826 etc/udev/rules.d/10-magicmouse.rules +968e12ffccc68d19011cc2f29a4badad etc/modprobe.d/hid-magicmouse.conf diff --git a/pkg-debian/DEBIAN/postinst b/pkg-debian/DEBIAN/postinst new file mode 100755 index 0000000..126044c --- /dev/null +++ b/pkg-debian/DEBIAN/postinst @@ -0,0 +1,20 @@ +#!/bin/bash + +DIR="/opt/magicmouse-hid" +cd ${DIR} + +# Install drive through DKMS +chmod u+x ${DIR}/scripts/install.sh +${DIR}/scripts/install.sh + +# Copy script to load the driver to OPT directory +chmod +x ${DIR}/magic-mouse-2-add.sh + +# Copy udev rule and reload udev +udevadm control -R + +# Disable eSCO mode in Bluetooth to fix disconnection problems with the mouse +echo 1 | tee /sys/module/bluetooth/parameters/disable_esco +systemctl restart bluetooth +# persist eSCO mode in Bluetooth setting +echo "options bluetooth disable_esco=1" | tee /etc/modprobe.d/bluetooth-tweaks.conf \ No newline at end of file diff --git a/pkg-debian/DEBIAN/prerm b/pkg-debian/DEBIAN/prerm new file mode 100755 index 0000000..3a9d6eb --- /dev/null +++ b/pkg-debian/DEBIAN/prerm @@ -0,0 +1,14 @@ +#!/bin/bash + +DIR="/opt/magicmouse-hid" +cd ${DIR} + +# Remove drive through DKMS +chmod u+x ${DIR}/scripts/remove.sh +${DIR}/scripts/remove.sh + +# Reload udev after removing the udev rule +udevadm control -R + +# Restart Bluetooth +systemctl restart bluetooth From 64905932c0a749eff83b174788c4b6478b588141 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Sun, 21 Feb 2021 03:28:20 +0000 Subject: [PATCH 20/34] remove amd64 from .deb file name. --- build-deb.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build-deb.sh b/build-deb.sh index d42f3da..1e7a831 100755 --- a/build-deb.sh +++ b/build-deb.sh @@ -8,7 +8,7 @@ ETC_DIR="/etc" PKG_DIR="/pkg-debian" OPT_DIR="/opt/magicmouse-hid" -DEB="magicmouse-hid_1.0.0-0_amd64.deb" +DEB="magicmouse-hid_1.0.0-0.deb" cp -rf ${DIR}${ETC_DIR} ${DIR}${PKG_DIR} From 206d565924732b7d4bd358d8794d006ed40f672d Mon Sep 17 00:00:00 2001 From: Ricardo Rodrigues Date: Sun, 21 Feb 2021 16:04:42 +0000 Subject: [PATCH 21/34] Add `.deb` file info to README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index afde9e0..a7be798 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,11 @@ This driver is based off of the work of @robotrovsky, @svartalf, @0xABAD and pro Please help to test this driver and report issues. +## Ubuntu (and derivatives) + +A `.deb` file is now available in Releases. Get the [latest version here](https://github.com/RicardoEPRodrigues/magicmouse-hid/releases/latest). + + ## Install Driver with DKMS and the two fixes. Setup/install with: From 0cf961c46deb4c896a14d7338cf13b9bdf98d5cc Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Fri, 26 Feb 2021 15:46:26 +0000 Subject: [PATCH 22/34] Improve code. Move drag start to be per finger. --- linux/drivers/hid/hid-magicmouse.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/linux/drivers/hid/hid-magicmouse.c b/linux/drivers/hid/hid-magicmouse.c index 511e7a5..f8f5eeb 100644 --- a/linux/drivers/hid/hid-magicmouse.c +++ b/linux/drivers/hid/hid-magicmouse.c @@ -4,6 +4,7 @@ * Copyright (c) 2010 Michael Poole * Copyright (c) 2010 Chase Douglas * Copyright (c) 2018 Rohit Pidaparthi + * Copyright (c) 2021 Ricardo Rodrigues */ /* @@ -145,7 +146,6 @@ struct magicmouse_sc { int ntouches; int scroll_accel; unsigned long scroll_jiffies; - unsigned long drag_start; struct { short x; @@ -153,6 +153,7 @@ struct magicmouse_sc { short scroll_x; short scroll_y; u8 size; + unsigned long drag_start; } touches[MAX_TOUCHES]; int tracking_ids[MAX_TOUCHES]; }; @@ -169,7 +170,10 @@ static int magicmouse_firm_touch(struct magicmouse_sc *msc) int idx = msc->tracking_ids[ii]; if (msc->touches[idx].size < 8) { /* Ignore this touch. */ - } else if (touch >= 0) { + continue; + } + + if (touch >= 0) { touch = -1; break; } else { @@ -207,6 +211,8 @@ static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state) if (emulate_3button) { int id; + id = magicmouse_firm_touch(msc); + /* If some button was pressed before, keep it held * down. Otherwise, if there's exactly one firm * touch, use that to override the mouse's guess. @@ -215,9 +221,8 @@ static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state) /* The button was released. */ } else if (last_state != 0) { state = last_state; - } else if (middle_click_3finger){ + } else if (id >= 0 && middle_click_3finger){ int x; - id = magicmouse_firm_touch(msc); x = msc->touches[id].x; if (magicmouse_detect_3finger_click(msc) > 2) state = 4; @@ -225,7 +230,7 @@ static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state) state = 1; else if (x > 0) state = 2; - } else if ((id = magicmouse_firm_touch(msc)) >= 0) { + } else if (id >= 0) { int x = msc->touches[id].x; if (x < middle_button_start) state = 1; @@ -233,7 +238,7 @@ static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state) state = 2; else state = 4; - }/* else: we keep the mouse's guess */ + }/* else: we keep the mouse's guess */ input_report_key(msc->input, BTN_MIDDLE, state & 4); } @@ -329,7 +334,7 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, msc->touches[id].scroll_x = x; msc->touches[id].scroll_y = y; - msc->drag_start = now; + msc->touches[id].drag_start = now; /* Reset acceleration after half a second. */ if (scroll_acceleration && time_before(now, @@ -345,7 +350,7 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, * drag events are not registered. This decreases the * sensitivity of dragging on Magic Mouse devices. */ - if (time_before(now, msc->drag_start + scroll_delay)) { + if (time_before(now, msc->touches[id].drag_start + scroll_delay)) { step_x = 0; step_y = 0; } From 2e94ebb3d2b3dfe1ffff460ac9ca41bccb3f3a0c Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Fri, 26 Feb 2021 17:01:10 +0000 Subject: [PATCH 23/34] Add position delay. Remove time delay. This commit adds two new variables for delaying scroll until fingers move further away. Remove time delay. --- .vscode/tasks.json | 10 ++++++ linux/drivers/hid/hid-magicmouse.c | 49 +++++++++++++++++++----------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 0a5b39e..9e1049c 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -43,6 +43,16 @@ "isDefault": true } }, + { + "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 + } + }, { "label": "Uninstall the Magicmouse 2 Driver (requires sudo password input)", "type": "shell", diff --git a/linux/drivers/hid/hid-magicmouse.c b/linux/drivers/hid/hid-magicmouse.c index f8f5eeb..e214aad 100644 --- a/linux/drivers/hid/hid-magicmouse.c +++ b/linux/drivers/hid/hid-magicmouse.c @@ -52,18 +52,29 @@ static int param_set_scroll_speed(const char *val, module_param_call(scroll_speed, param_set_scroll_speed, param_get_uint, &scroll_speed, 0644); MODULE_PARM_DESC(scroll_speed, "Scroll speed, value from 0 (slow) to 63 (fast)"); -// HZ (time constant for 1 second) is 250. Meaning that 50 is a quarter of that time (200ms). -static unsigned int scroll_delay = 50; -static int param_set_scroll_delay(const char *val, +static unsigned int scroll_delay_pox_x = 200; +static int param_set_scroll_delay_pox_x(const char *val, const struct kernel_param *kp) { unsigned long delay; - if (!val || kstrtoul(val, 0, &delay) || delay > 1000) + if (!val || kstrtoul(val, 0, &delay)) return -EINVAL; - scroll_delay = delay; + scroll_delay_pox_x = delay; return 0; } -module_param_call(scroll_delay, param_set_scroll_delay, param_get_uint, &scroll_delay, 0644); -MODULE_PARM_DESC(scroll_delay, "Scroll delay, value from 0 (immediate) to 1000 (very late scroll)"); +module_param_call(scroll_delay_pox_x, param_set_scroll_delay_pox_x, param_get_uint, &scroll_delay_pox_x, 0644); +MODULE_PARM_DESC(scroll_delay_pox_x, "Scroll X position delay before start scrolling"); + +static unsigned int scroll_delay_pox_y = 200; +static int param_set_scroll_delay_pox_y(const char *val, + const struct kernel_param *kp) { + unsigned long delay; + if (!val || kstrtoul(val, 0, &delay)) + return -EINVAL; + scroll_delay_pox_y = delay; + return 0; +} +module_param_call(scroll_delay_pox_y, param_set_scroll_delay_pox_y, param_get_uint, &scroll_delay_pox_y, 0644); +MODULE_PARM_DESC(scroll_delay_pox_y, "Scroll Y position delay before start scrolling"); static bool scroll_acceleration = false; module_param(scroll_acceleration, bool, 0644); @@ -150,10 +161,11 @@ struct magicmouse_sc { struct { short x; short y; + short scroll_x_start; + short scroll_y_start; short scroll_x; short scroll_y; u8 size; - unsigned long drag_start; } touches[MAX_TOUCHES]; int tracking_ids[MAX_TOUCHES]; }; @@ -331,11 +343,11 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, /* Calculate and apply the scroll motion. */ switch (state) { case TOUCH_STATE_START: + msc->touches[id].scroll_x_start = x; + msc->touches[id].scroll_y_start = y; msc->touches[id].scroll_x = x; msc->touches[id].scroll_y = y; - msc->touches[id].drag_start = now; - /* Reset acceleration after half a second. */ if (scroll_acceleration && time_before(now, msc->scroll_jiffies + HZ / 2)) @@ -346,17 +358,20 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, break; case TOUCH_STATE_DRAG: - /* Add a fifth of a second delay since the drag start in which + /* Add a position delay since the drag start in which * drag events are not registered. This decreases the * sensitivity of dragging on Magic Mouse devices. */ - if (time_before(now, msc->touches[id].drag_start + scroll_delay)) { - step_x = 0; - step_y = 0; + if (abs(step_x) < scroll_delay_pox_x) { + step_x = 0; + } else { + step_x /= (64 - (int)scroll_speed) * msc->scroll_accel; } - else { - step_x /= (64 - (int)scroll_speed) * msc->scroll_accel; - step_y /= (64 - (int)scroll_speed) * msc->scroll_accel; + + if (abs(step_y) < scroll_delay_pox_y) { + step_y = 0; + } else { + step_y /= (64 - (int)scroll_speed) * msc->scroll_accel; } if (step_x != 0) { From a0d1de8a4129ef9f12ab73da98f0798ee08b3c25 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Fri, 26 Feb 2021 21:39:49 +0000 Subject: [PATCH 24/34] update build options --- .vscode/tasks.json | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 9e1049c..1a83278 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -41,7 +41,8 @@ "group": { "kind": "build", "isDefault": true - } + }, + "problemMatcher": [] }, { "label": "Reinstall the Magicmouse 2 Driver (requires sudo password input)", @@ -51,7 +52,8 @@ "group": { "kind": "build", "isDefault": true - } + }, + "problemMatcher": [] }, { "label": "Uninstall the Magicmouse 2 Driver (requires sudo password input)", @@ -61,7 +63,8 @@ "group": { "kind": "build", "isDefault": true - } + }, + "problemMatcher": [] } ] } From c9644f4183147b79e7084d76d74c7de5eebdd148 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Sat, 27 Feb 2021 15:24:12 +0000 Subject: [PATCH 25/34] Update ignore file to ignore md5sums --- .gitignore | 2 ++ pkg-debian/DEBIAN/md5sums | 15 --------------- 2 files changed, 2 insertions(+), 15 deletions(-) delete mode 100644 pkg-debian/DEBIAN/md5sums diff --git a/.gitignore b/.gitignore index 884b9e5..64a3045 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ pkg-debian/* !pkg-debian/DEBIAN *.deb + +pkg-debian/DEBIAN/md5sums diff --git a/pkg-debian/DEBIAN/md5sums b/pkg-debian/DEBIAN/md5sums deleted file mode 100644 index cfdb339..0000000 --- a/pkg-debian/DEBIAN/md5sums +++ /dev/null @@ -1,15 +0,0 @@ -4e98e3ddfe2f2677c4ba7d40160ad805 opt/magicmouse-hid/scripts/remove.sh -19394e43a8b68454dd1ecb0b053b1eb4 opt/magicmouse-hid/scripts/install.sh -9d8311a258e4a5f2d21621c510d2865b opt/magicmouse-hid/magic-mouse-2-add.sh -a01b7f00714f6281bf2489654825d920 opt/magicmouse-hid/linux/linux/drivers/hid/Makefile -db55932808cb9137512a48a5f10e91ca opt/magicmouse-hid/linux/linux/drivers/hid/dkms.conf -65e911f8a2c968a242bdeea3483822e6 opt/magicmouse-hid/linux/linux/drivers/hid/hid-quirks.c -09cc6d34174bd1d0b1f81bd28954524f opt/magicmouse-hid/linux/linux/drivers/hid/hid-magicmouse.c -2cc66b539552c6edd592484e3e1d5ba3 opt/magicmouse-hid/linux/linux/drivers/hid/hid-ids.h -a01b7f00714f6281bf2489654825d920 opt/magicmouse-hid/linux/drivers/hid/Makefile -db55932808cb9137512a48a5f10e91ca opt/magicmouse-hid/linux/drivers/hid/dkms.conf -65e911f8a2c968a242bdeea3483822e6 opt/magicmouse-hid/linux/drivers/hid/hid-quirks.c -09cc6d34174bd1d0b1f81bd28954524f opt/magicmouse-hid/linux/drivers/hid/hid-magicmouse.c -2cc66b539552c6edd592484e3e1d5ba3 opt/magicmouse-hid/linux/drivers/hid/hid-ids.h -44c561036e33a298dcf6729bdf475826 etc/udev/rules.d/10-magicmouse.rules -968e12ffccc68d19011cc2f29a4badad etc/modprobe.d/hid-magicmouse.conf From 69abef890650970fbeafbbec2b6d3f4a147a0e00 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Sat, 27 Feb 2021 15:43:20 +0000 Subject: [PATCH 26/34] Correct variable names. Add Author and Description to module. --- etc/modprobe.d/hid-magicmouse.conf | 3 ++- linux/drivers/hid/hid-magicmouse.c | 31 ++++++++++++++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/etc/modprobe.d/hid-magicmouse.conf b/etc/modprobe.d/hid-magicmouse.conf index 0e93b3b..f772d82 100644 --- a/etc/modprobe.d/hid-magicmouse.conf +++ b/etc/modprobe.d/hid-magicmouse.conf @@ -2,4 +2,5 @@ options hid-magicmouse \ scroll_acceleration=1 \ scroll_speed=25 \ middle_click_3finger=1 \ - scroll_delay=50 \ No newline at end of file + scroll_delay_pos_x=200 \ + scroll_delay_pos_y=200 \ No newline at end of file diff --git a/linux/drivers/hid/hid-magicmouse.c b/linux/drivers/hid/hid-magicmouse.c index e214aad..003261a 100644 --- a/linux/drivers/hid/hid-magicmouse.c +++ b/linux/drivers/hid/hid-magicmouse.c @@ -52,29 +52,29 @@ static int param_set_scroll_speed(const char *val, module_param_call(scroll_speed, param_set_scroll_speed, param_get_uint, &scroll_speed, 0644); MODULE_PARM_DESC(scroll_speed, "Scroll speed, value from 0 (slow) to 63 (fast)"); -static unsigned int scroll_delay_pox_x = 200; -static int param_set_scroll_delay_pox_x(const char *val, +static unsigned int scroll_delay_pos_x = 200; +static int param_set_scroll_delay_pos_x(const char *val, const struct kernel_param *kp) { unsigned long delay; if (!val || kstrtoul(val, 0, &delay)) return -EINVAL; - scroll_delay_pox_x = delay; + scroll_delay_pos_x = delay; return 0; } -module_param_call(scroll_delay_pox_x, param_set_scroll_delay_pox_x, param_get_uint, &scroll_delay_pox_x, 0644); -MODULE_PARM_DESC(scroll_delay_pox_x, "Scroll X position delay before start scrolling"); +module_param_call(scroll_delay_pos_x, param_set_scroll_delay_pos_x, param_get_uint, &scroll_delay_pos_x, 0644); +MODULE_PARM_DESC(scroll_delay_pos_x, "Scroll X position delay before start scrolling"); -static unsigned int scroll_delay_pox_y = 200; -static int param_set_scroll_delay_pox_y(const char *val, +static unsigned int scroll_delay_pos_y = 200; +static int param_set_scroll_delay_pos_y(const char *val, const struct kernel_param *kp) { unsigned long delay; if (!val || kstrtoul(val, 0, &delay)) return -EINVAL; - scroll_delay_pox_y = delay; + scroll_delay_pos_y = delay; return 0; } -module_param_call(scroll_delay_pox_y, param_set_scroll_delay_pox_y, param_get_uint, &scroll_delay_pox_y, 0644); -MODULE_PARM_DESC(scroll_delay_pox_y, "Scroll Y position delay before start scrolling"); +module_param_call(scroll_delay_pos_y, param_set_scroll_delay_pos_y, param_get_uint, &scroll_delay_pos_y, 0644); +MODULE_PARM_DESC(scroll_delay_pos_y, "Scroll Y position delay before start scrolling"); static bool scroll_acceleration = false; module_param(scroll_acceleration, bool, 0644); @@ -362,13 +362,13 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, * drag events are not registered. This decreases the * sensitivity of dragging on Magic Mouse devices. */ - if (abs(step_x) < scroll_delay_pox_x) { + if (abs(step_x) < scroll_delay_pos_x) { step_x = 0; } else { step_x /= (64 - (int)scroll_speed) * msc->scroll_accel; } - if (abs(step_y) < scroll_delay_pox_y) { + if (abs(step_y) < scroll_delay_pos_y) { step_y = 0; } else { step_y /= (64 - (int)scroll_speed) * msc->scroll_accel; @@ -891,4 +891,11 @@ static struct hid_driver magicmouse_driver = { }; module_hid_driver(magicmouse_driver); +MODULE_AUTHOR("Ricardo Rodrigues"); +MODULE_AUTHOR("Rohit Pidaparthi"); +MODULE_AUTHOR("Chase Douglas"); +MODULE_AUTHOR("Michael Poole"); + +MODULE_DESCRIPTION("Magic Mouse 2 driver for Linux"); + MODULE_LICENSE("GPL"); From dd4d31fdfcbf4ca7c3e5aa1bca112d7788b98b04 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Sat, 27 Feb 2021 15:53:13 +0000 Subject: [PATCH 27/34] Bump version number --- build-deb.sh | 2 +- pkg-debian/DEBIAN/control | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build-deb.sh b/build-deb.sh index 1e7a831..a6e31a7 100755 --- a/build-deb.sh +++ b/build-deb.sh @@ -8,7 +8,7 @@ ETC_DIR="/etc" PKG_DIR="/pkg-debian" OPT_DIR="/opt/magicmouse-hid" -DEB="magicmouse-hid_1.0.0-0.deb" +DEB="magicmouse-hid_1.1.0-0.deb" cp -rf ${DIR}${ETC_DIR} ${DIR}${PKG_DIR} diff --git a/pkg-debian/DEBIAN/control b/pkg-debian/DEBIAN/control index 66c7d0b..7748dbf 100644 --- a/pkg-debian/DEBIAN/control +++ b/pkg-debian/DEBIAN/control @@ -1,5 +1,5 @@ Package: magicmouse-hid -Version: 1.0.0-0 +Version: 1.1.0-0 Maintainer: Ricardo Rodrigues Description: Magic Mouse 2 driver for Linux This driver makes use of DKMS to load the driver in the kernel, while From e050c1363e859bf9a7fbd3cf243d96a5d2ab62e2 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Mon, 7 Jun 2021 18:48:38 +0100 Subject: [PATCH 28/34] Apply fix from @johnchen902 This fix removes the need for external files to load the driver. It turns out the driver already loaded itself, but without a delay it would fail. This is now fixed. Based on the work seen here https://github.com/johnchen902/linux/commit/cb700d058a4c627f18c936cddff30f73e4f666ef --- build-deb.sh | 8 +- etc/udev/rules.d/10-magicmouse.rules | 9 -- install.sh | 16 +--- linux/drivers/hid/hid-magicmouse.c | 127 ++++++++++++++++++--------- pkg-debian/DEBIAN/postinst | 7 +- pkg-debian/DEBIAN/prerm | 4 +- remove.sh | 17 ++-- scripts/magic-mouse-2-add.sh | 20 ----- 8 files changed, 104 insertions(+), 104 deletions(-) delete mode 100644 etc/udev/rules.d/10-magicmouse.rules delete mode 100755 scripts/magic-mouse-2-add.sh diff --git a/build-deb.sh b/build-deb.sh index a6e31a7..71d74fc 100755 --- a/build-deb.sh +++ b/build-deb.sh @@ -8,16 +8,10 @@ ETC_DIR="/etc" PKG_DIR="/pkg-debian" OPT_DIR="/opt/magicmouse-hid" -DEB="magicmouse-hid_1.1.0-0.deb" +DEB="magicmouse-hid_2.0.0-0.deb" cp -rf ${DIR}${ETC_DIR} ${DIR}${PKG_DIR} -mkdir -p ${DIR}${PKG_DIR}${OPT_DIR} -# Copy scripts to /opt -cp -f ${DIR}/scripts/magic-mouse-2-add.sh ${DIR}${PKG_DIR}${OPT_DIR}/magic-mouse-2-add.sh -# cp -f ${DIR}/install.sh ${DIR}${PKG_DIR}${OPT_DIR}/install.sh -# cp -f ${DIR}/remove.sh ${DIR}${PKG_DIR}${OPT_DIR}/remove.sh - 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 diff --git a/etc/udev/rules.d/10-magicmouse.rules b/etc/udev/rules.d/10-magicmouse.rules deleted file mode 100644 index fa97432..0000000 --- a/etc/udev/rules.d/10-magicmouse.rules +++ /dev/null @@ -1,9 +0,0 @@ -SUBSYSTEM=="input", \ - KERNEL=="mouse*", \ - DRIVER=="", \ - SUBSYSTEMS=="hid", \ - KERNELS=="0005:004C:0269*", \ - DRIVERS=="hid-generic|magicmouse", \ - ACTION=="add", \ - SYMLINK+="input/magicmouse-%k", \ - RUN+="/opt/magicmouse-hid/magic-mouse-2-add.sh" diff --git a/install.sh b/install.sh index 5b46cdd..6500bb0 100755 --- a/install.sh +++ b/install.sh @@ -9,8 +9,6 @@ set -e set -x DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -OPT_DIR="/opt/magicmouse-hid" -UDEV_DIR="/etc/udev/rules.d" MODPROBE_DIR="/etc/modprobe.d" @@ -21,17 +19,11 @@ cp -f ${DIR}${MODPROBE_DIR}/hid-magicmouse.conf ${MODPROBE_DIR}/hid-magicmouse.c chmod u+x ${DIR}/scripts/install.sh ${DIR}/scripts/install.sh -# Copy script to load the driver to OPT directory -mkdir -p ${OPT_DIR} -cp -f ${DIR}/scripts/magic-mouse-2-add.sh ${OPT_DIR}/magic-mouse-2-add.sh -chmod +x ${OPT_DIR}/magic-mouse-2-add.sh - -# Copy udev rule and reload udev -cp -f ${DIR}${UDEV_DIR}/10-magicmouse.rules ${UDEV_DIR}/10-magicmouse.rules -udevadm control -R - # Disable eSCO mode in Bluetooth to fix disconnection problems with the mouse echo 1 | tee /sys/module/bluetooth/parameters/disable_esco systemctl restart bluetooth # persist eSCO mode in Bluetooth setting -echo "options bluetooth disable_esco=1" | tee /etc/modprobe.d/bluetooth-tweaks.conf \ No newline at end of file +echo "options bluetooth disable_esco=1" | tee /etc/modprobe.d/bluetooth-tweaks.conf + +# Load driver +sudo modprobe -a hid_magicmouse \ No newline at end of file diff --git a/linux/drivers/hid/hid-magicmouse.c b/linux/drivers/hid/hid-magicmouse.c index 003261a..92c5740 100644 --- a/linux/drivers/hid/hid-magicmouse.c +++ b/linux/drivers/hid/hid-magicmouse.c @@ -21,7 +21,7 @@ #include #include #include -#include +#include #include "hid-ids.h" @@ -168,6 +168,9 @@ struct magicmouse_sc { u8 size; } touches[MAX_TOUCHES]; int tracking_ids[MAX_TOUCHES]; + + struct hid_device *hdev; + struct delayed_work work; }; static int magicmouse_firm_touch(struct magicmouse_sc *msc) @@ -570,6 +573,21 @@ static int magicmouse_raw_event(struct hid_device *hdev, return 1; } +static int magicmouse_event(struct hid_device *hdev, struct hid_field *field, + struct hid_usage *usage, __s32 value) +{ + struct magicmouse_sc *msc = hid_get_drvdata(hdev); + if (msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 && + field->report->id == MOUSE2_REPORT_ID) { + // magic_mouse_raw_event has done all the work. Skip hidinput. + // + // Specifically, hidinput may modify BTN_LEFT and BTN_RIGHT, + // breaking emulate_3button. + return 1; + } + return 0; +} + static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev) { int error; @@ -743,26 +761,66 @@ static int magicmouse_input_configured(struct hid_device *hdev, } + +static int magicmouse_enable_multitouch(struct hid_device *hdev) +{ + const u8 *feature; + const u8 feature_mt[] = { 0xD7, 0x01 }; + const u8 feature_mt_mouse2[] = { 0xF1, 0x02, 0x01 }; + const u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 }; + const u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 }; + u8 *buf; + int ret; + int feature_size; + + if (hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) { + if (hdev->vendor == BT_VENDOR_ID_APPLE) { + feature_size = sizeof(feature_mt_trackpad2_bt); + feature = feature_mt_trackpad2_bt; + } else { /* USB_VENDOR_ID_APPLE */ + feature_size = sizeof(feature_mt_trackpad2_usb); + feature = feature_mt_trackpad2_usb; + } + } else if (hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) { + feature_size = sizeof(feature_mt_mouse2); + feature = feature_mt_mouse2; + } else { + feature_size = sizeof(feature_mt); + feature = feature_mt; + } + + buf = kmemdup(feature, feature_size, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + ret = hid_hw_raw_request(hdev, buf[0], buf, feature_size, + HID_FEATURE_REPORT, HID_REQ_SET_REPORT); + kfree(buf); + return ret; +} + +static void magicmouse_enable_mt_work(struct work_struct *work) +{ + struct magicmouse_sc *msc = + container_of(work, struct magicmouse_sc, work.work); + int ret; + + ret = magicmouse_enable_multitouch(msc->hdev); + if (ret < 0) + hid_err(msc->hdev, "unable to request touch data (%d)\n", ret); +} + static int magicmouse_probe(struct hid_device *hdev, const struct hid_device_id *id) { - __u8 feature_mt_mouse2_bt[] = { 0xF1, 0x02, 0x01 }; - __u8 feature_mt[] = { 0xD7, 0x01 }; - __u8 feature_mt_trackpad2_usb[] = { 0x02, 0x01 }; - __u8 feature_mt_trackpad2_bt[] = { 0xF1, 0x02, 0x01 }; - __u8 *feature; struct magicmouse_sc *msc; struct hid_report *report; int ret; - int feature_size; - struct usb_interface *intf; if (id->vendor == USB_VENDOR_ID_APPLE && - id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) { - intf = to_usb_interface(hdev->dev.parent); - if (intf->cur_altsetting->desc.bInterfaceNumber != 1) - return 0; - } + id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 && + hdev->type != HID_TYPE_USBMOUSE) + return 0; msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL); if (msc == NULL) { @@ -771,6 +829,8 @@ static int magicmouse_probe(struct hid_device *hdev, } msc->scroll_accel = SCROLL_ACCEL_DEFAULT; + msc->hdev = hdev; + INIT_DEFERRABLE_WORK(&msc->work, magicmouse_enable_mt_work); msc->quirks = id->driver_data; hid_set_drvdata(hdev, msc); @@ -829,36 +889,14 @@ static int magicmouse_probe(struct hid_device *hdev, * but there seems to be no other way of switching the mode. * Thus the super-ugly hacky success check below. */ - if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE || - id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD) { - feature_size = sizeof(feature_mt); - feature = kmemdup(feature_mt, feature_size, GFP_KERNEL); - } - else if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2){ - feature_size = sizeof(feature_mt_mouse2_bt); - feature = kmemdup(feature_mt_mouse2_bt, feature_size, GFP_KERNEL); - } else { /* USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 */ - if (id->vendor == BT_VENDOR_ID_APPLE) { - feature_size = sizeof(feature_mt_trackpad2_bt); - feature = kmemdup(feature_mt_trackpad2_bt, feature_size, - GFP_KERNEL); - } else { /* USB_VENDOR_ID_APPLE */ - feature_size = sizeof(feature_mt_trackpad2_usb); - feature = kmemdup(feature_mt_trackpad2_usb, feature_size, - GFP_KERNEL); - } - } - if (!feature) { - ret = -ENOMEM; - goto err_stop_hw; - } - ret = hid_hw_raw_request(hdev, feature[0], feature, feature_size, - HID_FEATURE_REPORT, HID_REQ_SET_REPORT); - kfree(feature); - if (ret != -EIO && ret != feature_size) { + ret = magicmouse_enable_multitouch(hdev); + if (ret != -EIO && ret < 0) { hid_err(hdev, "unable to request touch data (%d)\n", ret); goto err_stop_hw; } + if (ret == -EIO && id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) { + schedule_delayed_work(&msc->work, msecs_to_jiffies(500)); + } return 0; err_stop_hw: @@ -866,6 +904,13 @@ static int magicmouse_probe(struct hid_device *hdev, return ret; } +static void magicmouse_remove(struct hid_device *hdev) +{ + struct magicmouse_sc *msc = hid_get_drvdata(hdev); + cancel_delayed_work_sync(&msc->work); + hid_hw_stop(hdev); +} + static const struct hid_device_id magic_mice[] = { { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 }, @@ -885,7 +930,9 @@ static struct hid_driver magicmouse_driver = { .name = "magicmouse", .id_table = magic_mice, .probe = magicmouse_probe, + .remove = magicmouse_remove, .raw_event = magicmouse_raw_event, + .event = magicmouse_event, .input_mapping = magicmouse_input_mapping, .input_configured = magicmouse_input_configured, }; diff --git a/pkg-debian/DEBIAN/postinst b/pkg-debian/DEBIAN/postinst index 126044c..7fd1707 100755 --- a/pkg-debian/DEBIAN/postinst +++ b/pkg-debian/DEBIAN/postinst @@ -7,11 +7,8 @@ cd ${DIR} chmod u+x ${DIR}/scripts/install.sh ${DIR}/scripts/install.sh -# Copy script to load the driver to OPT directory -chmod +x ${DIR}/magic-mouse-2-add.sh - -# Copy udev rule and reload udev -udevadm control -R +# Load driver +sudo modprobe -a hid_magicmouse # Disable eSCO mode in Bluetooth to fix disconnection problems with the mouse echo 1 | tee /sys/module/bluetooth/parameters/disable_esco diff --git a/pkg-debian/DEBIAN/prerm b/pkg-debian/DEBIAN/prerm index 3a9d6eb..4b34b82 100755 --- a/pkg-debian/DEBIAN/prerm +++ b/pkg-debian/DEBIAN/prerm @@ -7,8 +7,8 @@ cd ${DIR} chmod u+x ${DIR}/scripts/remove.sh ${DIR}/scripts/remove.sh -# Reload udev after removing the udev rule -udevadm control -R +# Remove loaded driver +modprobe -r hid_magicmouse # Restart Bluetooth systemctl restart bluetooth diff --git a/remove.sh b/remove.sh index cb15154..97a3b6f 100755 --- a/remove.sh +++ b/remove.sh @@ -1,26 +1,25 @@ #!/bin/bash +if [ "$EUID" -ne 0 ] + then echo "Please run as root" + exit +fi + set -e set -x DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -OPT_DIR="/opt/magicmouse-hid" -UDEV_DIR="/etc/udev/rules.d" MODPROBE_DIR="/etc/modprobe.d" # Remove drive through DKMS chmod u+x ${DIR}/scripts/remove.sh ${DIR}/scripts/remove.sh +# Remove loaded driver +modprobe -r hid_magicmouse + # Remove Modprobe configuration file rm -f ${MODPROBE_DIR}/hid-magicmouse.conf -# Copy `.ko` and script to activate it to OPT directory -rm -rf ${OPT_DIR} - -# Remove the udev rule and reload udev -rm -f ${UDEV_DIR}/10-magicmouse.rules -udevadm control -R - # Restart Bluetooth systemctl restart bluetooth diff --git a/scripts/magic-mouse-2-add.sh b/scripts/magic-mouse-2-add.sh deleted file mode 100755 index 48fc368..0000000 --- a/scripts/magic-mouse-2-add.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh - -FILE=/tmp/magicmouse.lock - -reload() { - # Check is Lock File exists, if not create it and set trap on exit - if { set -C; 2>/dev/null >$FILE; }; then - trap "rm -f $FILE" EXIT - else - # Lock file exists. exiting. - exit - fi - - modprobe -r hid_magicmouse - sleep 2 - modprobe -a hid-generic hid_magicmouse - sleep 2 -} - -reload & From ec80ec67951ad6e235c553a988f93493a866d410 Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Mon, 7 Jun 2021 18:54:29 +0100 Subject: [PATCH 29/34] Add John Chen to list of authors --- linux/drivers/hid/hid-magicmouse.c | 1 + 1 file changed, 1 insertion(+) diff --git a/linux/drivers/hid/hid-magicmouse.c b/linux/drivers/hid/hid-magicmouse.c index 92c5740..4581294 100644 --- a/linux/drivers/hid/hid-magicmouse.c +++ b/linux/drivers/hid/hid-magicmouse.c @@ -939,6 +939,7 @@ static struct hid_driver magicmouse_driver = { module_hid_driver(magicmouse_driver); MODULE_AUTHOR("Ricardo Rodrigues"); +MODULE_AUTHOR("John Chen"); MODULE_AUTHOR("Rohit Pidaparthi"); MODULE_AUTHOR("Chase Douglas"); MODULE_AUTHOR("Michael Poole"); From 71570b78c676646dddc0e2035ba1bb0518552d8a Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Mon, 7 Jun 2021 20:44:34 +0100 Subject: [PATCH 30/34] Add ability to stop scrolling when moving the mouse. This update brings a feature that allows you to stop scrolling if the mouse is moving. It is still very rough, but it works. --- build-deb.sh | 2 +- etc/modprobe.d/hid-magicmouse.conf | 7 +- linux/drivers/hid/hid-magicmouse.c | 139 ++++++++++++++++------------- 3 files changed, 82 insertions(+), 66 deletions(-) diff --git a/build-deb.sh b/build-deb.sh index 71d74fc..dd3d9c2 100755 --- a/build-deb.sh +++ b/build-deb.sh @@ -8,7 +8,7 @@ ETC_DIR="/etc" PKG_DIR="/pkg-debian" OPT_DIR="/opt/magicmouse-hid" -DEB="magicmouse-hid_2.0.0-0.deb" +DEB="magicmouse-hid_2.1.0-0.deb" cp -rf ${DIR}${ETC_DIR} ${DIR}${PKG_DIR} diff --git a/etc/modprobe.d/hid-magicmouse.conf b/etc/modprobe.d/hid-magicmouse.conf index f772d82..38b2edc 100644 --- a/etc/modprobe.d/hid-magicmouse.conf +++ b/etc/modprobe.d/hid-magicmouse.conf @@ -1,6 +1,7 @@ options hid-magicmouse \ scroll_acceleration=1 \ - scroll_speed=25 \ + stop_scroll_while_moving=1 \ + scroll_speed=10 \ middle_click_3finger=1 \ - scroll_delay_pos_x=200 \ - scroll_delay_pos_y=200 \ No newline at end of file + scroll_delay_pos_x=300 \ + scroll_delay_pos_y=250 \ No newline at end of file diff --git a/linux/drivers/hid/hid-magicmouse.c b/linux/drivers/hid/hid-magicmouse.c index 4581294..78a8e6c 100644 --- a/linux/drivers/hid/hid-magicmouse.c +++ b/linux/drivers/hid/hid-magicmouse.c @@ -40,6 +40,10 @@ static bool emulate_scroll_wheel = true; module_param(emulate_scroll_wheel, bool, 0644); MODULE_PARM_DESC(emulate_scroll_wheel, "Emulate a scroll wheel"); +static bool stop_scroll_while_moving = true; +module_param(stop_scroll_while_moving, bool, 0644); +MODULE_PARM_DESC(stop_scroll_while_moving, "Stop scrolling whenever the mouse moves"); + static unsigned int scroll_speed = 32; static int param_set_scroll_speed(const char *val, const struct kernel_param *kp) { @@ -157,12 +161,12 @@ struct magicmouse_sc { int ntouches; int scroll_accel; unsigned long scroll_jiffies; + int x; + int y; struct { short x; short y; - short scroll_x_start; - short scroll_y_start; short scroll_x; short scroll_y; u8 size; @@ -265,7 +269,7 @@ static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state) } static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, - u8 *tdata, int npoints) + u8 *tdata, int npoints, int mouse_loc_x, int mouse_loc_y) { struct input_dev *input = msc->input; int id, x, y, size, orientation, touch_major, touch_minor, state, down; @@ -343,54 +347,61 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, int step_x = msc->touches[id].scroll_x - x; int step_y = msc->touches[id].scroll_y - y; - /* Calculate and apply the scroll motion. */ - switch (state) { - case TOUCH_STATE_START: - msc->touches[id].scroll_x_start = x; - msc->touches[id].scroll_y_start = y; - msc->touches[id].scroll_x = x; - msc->touches[id].scroll_y = y; - - /* Reset acceleration after half a second. */ - if (scroll_acceleration && time_before(now, - msc->scroll_jiffies + HZ / 2)) - msc->scroll_accel = max_t(int, - msc->scroll_accel - 1, 1); - else - msc->scroll_accel = SCROLL_ACCEL_DEFAULT; - - break; - case TOUCH_STATE_DRAG: - /* Add a position delay since the drag start in which - * drag events are not registered. This decreases the - * sensitivity of dragging on Magic Mouse devices. - */ - if (abs(step_x) < scroll_delay_pos_x) { - step_x = 0; - } else { - step_x /= (64 - (int)scroll_speed) * msc->scroll_accel; - } - - if (abs(step_y) < scroll_delay_pos_y) { - step_y = 0; - } else { - step_y /= (64 - (int)scroll_speed) * msc->scroll_accel; - } - - 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); - } + /* Determine if the mouse has moved, if so then disable scrolling. */ + bool continue_scroll = true; + if (stop_scroll_while_moving) + { + continue_scroll = msc->x == mouse_loc_x && msc->y == mouse_loc_y; + } - if (step_y != 0) { - msc->touches[id].scroll_y -= step_y * - (64 - scroll_speed) * msc->scroll_accel; - msc->scroll_jiffies = now; - input_report_rel(input, REL_WHEEL, step_y); + if (continue_scroll) { + /* Calculate and apply the scroll motion. */ + switch (state) { + case TOUCH_STATE_START: + msc->touches[id].scroll_x = x; + msc->touches[id].scroll_y = y; + + /* Reset acceleration after half a second. */ + if (scroll_acceleration && time_before(now, + msc->scroll_jiffies + HZ / 2)) + msc->scroll_accel = max_t(int, + msc->scroll_accel - 1, 1); + else + msc->scroll_accel = SCROLL_ACCEL_DEFAULT; + + break; + case TOUCH_STATE_DRAG: + /* Add a position delay since the drag start in which + * drag events are not registered. This decreases the + * sensitivity of dragging on Magic Mouse devices. + */ + if (abs(step_x) < scroll_delay_pos_x) { + step_x = 0; + } else { + step_x /= (64 - (int)scroll_speed) * msc->scroll_accel; + } + + if (abs(step_y) < scroll_delay_pos_y) { + step_y = 0; + } else { + step_y /= (64 - (int)scroll_speed) * msc->scroll_accel; + } + + 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); + } + + if (step_y != 0) { + msc->touches[id].scroll_y -= step_y * + (64 - scroll_speed) * msc->scroll_accel; + msc->scroll_jiffies = now; + input_report_rel(input, REL_WHEEL, step_y); + } + break; } - break; } } @@ -444,7 +455,7 @@ static int magicmouse_raw_event(struct hid_device *hdev, } msc->ntouches = 0; for (ii = 0; ii < npoints; ii++) - magicmouse_emit_touch(msc, ii, data + ii * 9 + 4, npoints); + magicmouse_emit_touch(msc, ii, data + ii * 9 + 4, npoints, 0, 0); clicks = data[1]; break; @@ -460,7 +471,7 @@ static int magicmouse_raw_event(struct hid_device *hdev, } msc->ntouches = 0; for (ii = 0; ii < npoints; ii++) - magicmouse_emit_touch(msc, ii, data + ii * 9 + 12, npoints); + magicmouse_emit_touch(msc, ii, data + ii * 9 + 12, npoints, 0, 0); clicks = data[1]; break; @@ -475,8 +486,6 @@ static int magicmouse_raw_event(struct hid_device *hdev, return 0; } msc->ntouches = 0; - for (ii = 0; ii < npoints; ii++) - magicmouse_emit_touch(msc, ii, data + ii * 8 + 6, npoints); /* When emulating three-button mode, it is important * to have the current touch information before @@ -484,8 +493,10 @@ static int magicmouse_raw_event(struct hid_device *hdev, */ x = (int)(((data[3] & 0x0c) << 28) | (data[1] << 22)) >> 22; y = (int)(((data[3] & 0x30) << 26) | (data[2] << 22)) >> 22; - clicks = data[3]; + for (ii = 0; ii < npoints; ii++) + magicmouse_emit_touch(msc, ii, data + ii * 8 + 6, npoints, x, y); + clicks = data[3]; /* The following bits provide a device specific timestamp. They * are unused here. * @@ -524,6 +535,14 @@ static int magicmouse_raw_event(struct hid_device *hdev, return 0; } msc->ntouches = 0; + + /* When emulating three-button mode, it is important + * to have the current touch information before + * generating a click event. + */ + x = (int)((data[3] << 24) | (data[2] << 16)) >> 16; + y = (int)((data[5] << 24) | (data[4] << 16)) >> 16; + // print the values of the first 14 bytes of data and number of points and size. // printk("The contents of npoints are: %i\n", npoints); // printk("Size is: %i\n", size); @@ -533,14 +552,8 @@ static int magicmouse_raw_event(struct hid_device *hdev, // printk("data %i is: %i\n", jj, d); // } for (ii = 0; ii < npoints; ii++) - magicmouse_emit_touch(msc, ii, data + ii * 8 + 14, npoints); - - /* When emulating three-button mode, it is important - * to have the current touch information before - * generating a click event. - */ - x = (int)((data[3] << 24) | (data[2] << 16)) >> 16; - y = (int)((data[5] << 24) | (data[4] << 16)) >> 16; + magicmouse_emit_touch(msc, ii, data + ii * 8 + 14, npoints, x, y); + clicks = data[1]; break; case DOUBLE_REPORT_ID: @@ -557,6 +570,8 @@ static int magicmouse_raw_event(struct hid_device *hdev, if (input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE || input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) { + msc->x = x; + msc->y = y; magicmouse_emit_buttons(msc, clicks & 3); input_report_rel(input, REL_X, x); input_report_rel(input, REL_Y, y); From c21b7ccb48d3e6bdbf0dddcb5db54fea01a0a23c Mon Sep 17 00:00:00 2001 From: RicardoEPRodrigues Date: Fri, 16 Jul 2021 14:48:59 +0100 Subject: [PATCH 31/34] Apply fixes from kernel 5.15. Refers to #12. --- README.md | 4 ++- linux/drivers/hid/hid-magicmouse.c | 57 ++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a7be798..8aba68d 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,13 @@ # 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. It also contains 2 fixes to the Magic Mouse 2 regarding Bluetooth random disconnections and this driver not loading on bluetooth reconnection. +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 this driver not loading on bluetooth reconnection. This driver is based off of the work of @robotrovsky, @svartalf, @0xABAD and probably others. Please help to test this driver and report issues. +**NOTE**: Since kernel version 5.15, there is support for the mouse without the need for this driver. We try to backport changes to this driver to have it up-to-date with the one on the kernel and try to offer a bit more functionality but it is not perfect. If you want the mouse to just work either install a more recent kernel or proceed with this driver installation. + ## Ubuntu (and derivatives) A `.deb` file is now available in Releases. Get the [latest version here](https://github.com/RicardoEPRodrigues/magicmouse-hid/releases/latest). diff --git a/linux/drivers/hid/hid-magicmouse.c b/linux/drivers/hid/hid-magicmouse.c index 78a8e6c..485f148 100644 --- a/linux/drivers/hid/hid-magicmouse.c +++ b/linux/drivers/hid/hid-magicmouse.c @@ -106,6 +106,10 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie #define TOUCH_STATE_START 0x30 #define TOUCH_STATE_DRAG 0x40 +/* Number of high-resolution events for each low-resolution detent. */ +#define SCROLL_HR_STEPS 10 +#define SCROLL_HR_MULT (120 / SCROLL_HR_STEPS) +#define SCROLL_HR_THRESHOLD 90 /* units */ #define SCROLL_ACCEL_DEFAULT 3 /* Touch surface information. Dimension is in hundredths of a mm, min and max @@ -169,7 +173,11 @@ struct magicmouse_sc { short y; short scroll_x; short scroll_y; + short scroll_x_hr; + short scroll_y_hr; u8 size; + bool scroll_x_active; + bool scroll_y_active; } touches[MAX_TOUCHES]; int tracking_ids[MAX_TOUCHES]; @@ -346,6 +354,10 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, unsigned long now = jiffies; int step_x = msc->touches[id].scroll_x - x; int step_y = msc->touches[id].scroll_y - y; + int step_hr = ((64 - (int)scroll_speed) * msc->scroll_accel) / + SCROLL_HR_STEPS; + int step_x_hr = msc->touches[id].scroll_x_hr - x; + int step_y_hr = msc->touches[id].scroll_y_hr - y; /* Determine if the mouse has moved, if so then disable scrolling. */ bool continue_scroll = true; @@ -360,6 +372,10 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, case TOUCH_STATE_START: msc->touches[id].scroll_x = x; msc->touches[id].scroll_y = y; + msc->touches[id].scroll_x_hr = x; + msc->touches[id].scroll_y_hr = y; + msc->touches[id].scroll_x_active = false; + msc->touches[id].scroll_y_active = false; /* Reset acceleration after half a second. */ if (scroll_acceleration && time_before(now, @@ -400,6 +416,40 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, msc->scroll_jiffies = now; input_report_rel(input, REL_WHEEL, step_y); } + + if (!msc->touches[id].scroll_x_active && + abs(step_x_hr) > SCROLL_HR_THRESHOLD) { + msc->touches[id].scroll_x_active = true; + msc->touches[id].scroll_x_hr = x; + step_x_hr = 0; + } + + step_x_hr /= step_hr; + 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); + } + + if (!msc->touches[id].scroll_y_active && + abs(step_y_hr) > SCROLL_HR_THRESHOLD) { + msc->touches[id].scroll_y_active = true; + msc->touches[id].scroll_y_hr = y; + step_y_hr = 0; + } + + step_y_hr /= step_hr; + if (step_y_hr != 0 && + msc->touches[id].scroll_y_active) { + msc->touches[id].scroll_y_hr -= step_y_hr * + step_hr; + input_report_rel(input, + REL_WHEEL_HI_RES, + step_y_hr * SCROLL_HR_MULT); + } break; } } @@ -623,6 +673,8 @@ static int magicmouse_setup_input(struct input_dev *input, struct hid_device *hd if (emulate_scroll_wheel) { __set_bit(REL_WHEEL, input->relbit); __set_bit(REL_HWHEEL, input->relbit); + __set_bit(REL_WHEEL_HI_RES, input->relbit); + __set_bit(REL_HWHEEL_HI_RES, input->relbit); } } else if (input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD){ /* input->keybit is initialized with incorrect button info @@ -835,7 +887,7 @@ static int magicmouse_probe(struct hid_device *hdev, if (id->vendor == USB_VENDOR_ID_APPLE && id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 && hdev->type != HID_TYPE_USBMOUSE) - return 0; + return -ENODEV; msc = devm_kzalloc(&hdev->dev, sizeof(*msc), GFP_KERNEL); if (msc == NULL) { @@ -922,7 +974,8 @@ static int magicmouse_probe(struct hid_device *hdev, static void magicmouse_remove(struct hid_device *hdev) { struct magicmouse_sc *msc = hid_get_drvdata(hdev); - cancel_delayed_work_sync(&msc->work); + if (msc) + cancel_delayed_work_sync(&msc->work); hid_hw_stop(hdev); } From f6d6d0d01537531271c20e02a5f33a73c7c04615 Mon Sep 17 00:00:00 2001 From: Elton Santana Date: Wed, 28 Jul 2021 18:05:01 -0300 Subject: [PATCH 32/34] Add reload driver instructions to README file --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 8aba68d..110b4d3 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,15 @@ The drivers supports regular mouse motion and additionally scrolling and mouse m Several parameters are avaliable 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: + +``` +sudo rmmod hid_magicmouse +sudo modprobe hid_magicmouse +``` + ## Troubleshooting (outdated) If the driver is not working, please make sure that the correct hid-magicmouse driver gets loaded and try the following steps: From 50efecebccdda91337613ed84fbfe00a5d9c7ff3 Mon Sep 17 00:00:00 2001 From: Ricardo Rodrigues Date: Fri, 24 Sep 2021 10:54:50 +0100 Subject: [PATCH 33/34] Update DEB file version to match new numbering. --- pkg-debian/DEBIAN/control | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg-debian/DEBIAN/control b/pkg-debian/DEBIAN/control index 7748dbf..8f27992 100644 --- a/pkg-debian/DEBIAN/control +++ b/pkg-debian/DEBIAN/control @@ -1,5 +1,5 @@ Package: magicmouse-hid -Version: 1.1.0-0 +Version: 2.1.0-0 Maintainer: Ricardo Rodrigues Description: Magic Mouse 2 driver for Linux This driver makes use of DKMS to load the driver in the kernel, while From e82cb580fd4eddf32eba1e4bfbecf2ad21b69338 Mon Sep 17 00:00:00 2001 From: Ricardo Rodrigues Date: Wed, 26 Jun 2024 22:06:25 +0100 Subject: [PATCH 34/34] Update README.md --- README.md | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 110b4d3..42af91a 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,13 @@ # 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 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 this driver not loading on bluetooth reconnection. +> [!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, @0xABAD 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. -Please help to test this driver and report issues. - -**NOTE**: Since kernel version 5.15, there is support for the mouse without the need for this driver. We try to backport changes to this driver to have it up-to-date with the one on the kernel and try to offer a bit more functionality but it is not perfect. If you want the mouse to just work either install a more recent kernel or proceed with this driver installation. +This driver is based on the work of @robotrovsky, @svartalf, @0xABAD, and probably others. Thank you! ## Ubuntu (and derivatives) @@ -33,14 +34,14 @@ sudo ./remove.sh ``` ## 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. +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. ## 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. Scrolling is a single finger up or down motion anywhere on the touch surface. +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 avaliable 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. +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 @@ -64,7 +65,7 @@ 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: @@ -119,11 +120,11 @@ Now unplug the trackpad and plug it back in, to see which driver gets loaded. ## Fixes -Below is the explanation to 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. +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 complaining 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: +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 @@ -136,15 +137,15 @@ echo "options bluetooth disable_esco=1" | sudo tee /etc/modprobe.d/bluetooth-twe [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 has a kernel module, please take a look at `scripts/install.sh`. +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. +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 its contents: +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 @@ -172,7 +173,7 @@ reload() { 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 eariler. +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: @@ -196,7 +197,7 @@ 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. +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