From 390518a22be123941b126e2fcce438bcaf3ec8f5 Mon Sep 17 00:00:00 2001
From: Sebastian Krzyszkowiak <sebastian.krzyszkowiak@puri.sm>
Date: Tue, 24 Jan 2023 10:10:37 +0100
Subject: [PATCH] defaults: Improve reliability of bluetooth-brcmfmac against
 system suspend

hciattach isn't aware that the card gets reset at system suspend, requiring
full reinitialization, so it needs to be restarted. Unfortunately,
I haven't found a way to describe such relation using systemd unit
relations alone that wouldn't break things, so a system-sleep hook
is added to stop the service before going into suspend.

The service itself is also tweaked to better handle errors and to make
it restartable (previously it would require the card to be reset before
hciattach could work again).

Signed-off-by: Sebastian Krzyszkowiak <sebastian.krzyszkowiak@puri.sm>
---
 .../librem5-base-defaults.bluetooth-brcmfmac.service  |  6 ++++--
 debian/librem5-base-defaults.install                  |  3 +++
 default/systemd/system-sleep/bluetooth-brcmfmac       | 11 +++++++++++
 3 files changed, 18 insertions(+), 2 deletions(-)
 create mode 100755 default/systemd/system-sleep/bluetooth-brcmfmac

diff --git a/debian/librem5-base-defaults.bluetooth-brcmfmac.service b/debian/librem5-base-defaults.bluetooth-brcmfmac.service
index 8f192c2..9883e93 100644
--- a/debian/librem5-base-defaults.bluetooth-brcmfmac.service
+++ b/debian/librem5-base-defaults.bluetooth-brcmfmac.service
@@ -3,10 +3,12 @@ Description=Bluetooth attachment for brcmfmac driver
 After=sys-bus-sdio-drivers-brcmfmac-mmc1:0001:1.device
 BindsTo=sys-bus-sdio-drivers-brcmfmac-mmc1:0001:1.device
 ConditionPathExists=/etc/firmware/brcm/BCM4362A2.hcd
+StopWhenUnneeded=true
 
 [Install]
-WantedBy=sys-bus-sdio-drivers-brcmfmac-mmc1:0001:1.device
+RequiredBy=sys-bus-sdio-drivers-brcmfmac-mmc1:0001:1.device
 
 [Service]
 Type=forking
-ExecStart=/usr/bin/hciattach -s 115200 /dev/ttymxc3 bcm43xx 1500000
+ExecStart=sh -c '/usr/bin/hciattach -s 115200 /dev/ttymxc3 bcm43xx 1500000 || /usr/bin/hciattach -s 1500000 /dev/ttymxc3 bcm43xx 1500000'
+Restart=on-abort
diff --git a/debian/librem5-base-defaults.install b/debian/librem5-base-defaults.install
index 07d32f0..c33a306 100644
--- a/debian/librem5-base-defaults.install
+++ b/debian/librem5-base-defaults.install
@@ -56,6 +56,9 @@ default/systemd/systemd-localed.service.d/*	lib/systemd/system/systemd-localed.s
 # enable zram
 default/systemd/zram-generator.conf.d/*	lib/systemd/zram-generator.conf.d/
 
+# suspend hooks
+default/systemd/system-sleep/*		lib/systemd/system-sleep/
+
 # initramfs modules
 default/initramfs-tools/modules.d/*	usr/share/initramfs-tools/modules.d/
 
diff --git a/default/systemd/system-sleep/bluetooth-brcmfmac b/default/systemd/system-sleep/bluetooth-brcmfmac
new file mode 100755
index 0000000..6df9860
--- /dev/null
+++ b/default/systemd/system-sleep/bluetooth-brcmfmac
@@ -0,0 +1,11 @@
+#!/bin/sh
+# Stop hciattach before system suspend to not interfere with
+# freshly reset card after resume.
+#
+# No need to start it back here as it gets restarted automatically
+# because of RequiredBy relation in its systemd unit.
+case "${1}/${2}" in
+        hibernate|suspend|pre*)
+                systemctl stop bluetooth-brcmfmac.service
+                ;;
+esac
-- 
GitLab