From 9cd15d3e920e25348c6de814f4a8c890ca9c0d77 Mon Sep 17 00:00:00 2001
From: j1nx
Date: Tue, 30 Mar 2021 14:29:32 +0200
Subject: [PATCH] Changes to the bluetooth system
---
.../0001-btuart-reduced-baud-rate-rpi3b.patch | 16 +++++++++++++
.../package/btspeaker/brcm_bt.service | 8 ++++---
.../package/btspeaker/bthelper@.service | 8 +++++++
.../package/btspeaker/btspeaker.mk | 23 +++++++++++++++++++
4 files changed, 52 insertions(+), 3 deletions(-)
create mode 100644 buildroot-external/package/btspeaker/0001-btuart-reduced-baud-rate-rpi3b.patch
create mode 100644 buildroot-external/package/btspeaker/bthelper@.service
diff --git a/buildroot-external/package/btspeaker/0001-btuart-reduced-baud-rate-rpi3b.patch b/buildroot-external/package/btspeaker/0001-btuart-reduced-baud-rate-rpi3b.patch
new file mode 100644
index 00000000..ce7f42d9
--- /dev/null
+++ b/buildroot-external/package/btspeaker/0001-btuart-reduced-baud-rate-rpi3b.patch
@@ -0,0 +1,16 @@
+Bluetooth: hci0: Frame reassembly failed (-84)
+https://github.com/raspberrypi/firmware/issues/1150
+
+Signed-off-by: Luca Giovenzana
+
+--- btuart.orig 2020-03-25 03:05:24.491176583 +0100
++++ btuart 2020-03-25 03:05:43.603107833 +0100
+@@ -19,7 +19,7 @@ if [ "$uart0" = "$serial1" ] ; then
+ if [ "$uart0_pins" = "16" ] ; then
+ $HCIATTACH /dev/serial1 bcm43xx 3000000 flow - $BDADDR
+ else
+- $HCIATTACH /dev/serial1 bcm43xx 921600 noflow - $BDADDR
++ $HCIATTACH /dev/serial1 bcm43xx 460800 noflow - $BDADDR
+ fi
+ else
+ $HCIATTACH /dev/serial1 bcm43xx 460800 noflow - $BDADDR
diff --git a/buildroot-external/package/btspeaker/brcm_bt.service b/buildroot-external/package/btspeaker/brcm_bt.service
index e34e9b88..0bb77e55 100644
--- a/buildroot-external/package/btspeaker/brcm_bt.service
+++ b/buildroot-external/package/btspeaker/brcm_bt.service
@@ -1,12 +1,14 @@
[Unit]
Description=Broadcom BCM4343* bluetooth HCI
-ConditionPathIsDirectory=/proc/device-tree/soc/gpio@7e200000/bt_pins
Before=bluetooth.service
Before=btspeaker.service
+Requires=dev-serial1.device
+After=dev-serial1.device
+ConditionFileNotEmpty=/proc/device-tree/soc/gpio@7e200000/bt_pins/brcm,pins
[Service]
-Type=simple
-ExecStart=btattach -B /dev/ttyAMA0 -P bcm -S 921600
+Type=forking
+ExecStart=/usr/bin/btuart
[Install]
WantedBy=multi-user.target
diff --git a/buildroot-external/package/btspeaker/bthelper@.service b/buildroot-external/package/btspeaker/bthelper@.service
new file mode 100644
index 00000000..c8a16c15
--- /dev/null
+++ b/buildroot-external/package/btspeaker/bthelper@.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=Raspberry Pi bluetooth helper
+Requires=bluetooth.service
+After=bluetooth.service
+
+[Service]
+Type=simple
+ExecStart=/usr/bin/bthelper %I
diff --git a/buildroot-external/package/btspeaker/btspeaker.mk b/buildroot-external/package/btspeaker/btspeaker.mk
index 29a64df7..7737a283 100644
--- a/buildroot-external/package/btspeaker/btspeaker.mk
+++ b/buildroot-external/package/btspeaker/btspeaker.mk
@@ -10,6 +10,16 @@ BTSPEAKER_SITE_METHOD = local
BTSPEAKER_LICENSE = Apache License 2.0
BTSPEAKER_LICENSE_FILES = LICENSE
+define BTSPEAKER_BUILD_CMDS
+ curl -L -o $(@D)/BCM43430A1.hcd https://raw.githubusercontent.com/RPi-Distro/bluez-firmware/e7fd166981ab4bb9a36c2d1500205a078a35714d/broadcom/BCM43430A1.hcd
+ curl -L -o $(@D)/BCM4345C0.hcd https://raw.githubusercontent.com/RPi-Distro/bluez-firmware/e7fd166981ab4bb9a36c2d1500205a078a35714d/broadcom/BCM4345C0.hcd
+ curl -L -o $(@D)/btuart https://raw.githubusercontent.com/RPi-Distro/pi-bluetooth/6f6386e91e33966d3c4a3cfee72d61e14c6a11ef/usr/bin/btuart
+ curl -L -o $(@D)/bthelper https://raw.githubusercontent.com/RPi-Distro/pi-bluetooth/6f6386e91e33966d3c4a3cfee72d61e14c6a11ef/usr/bin/bthelper
+ curl -L -o $(@D)/90-pi-bluetooth.rules https://raw.githubusercontent.com/RPi-Distro/pi-bluetooth/6f6386e91e33966d3c4a3cfee72d61e14c6a11ef/lib/udev/rules.d/90-pi-bluetooth.rules
+
+ patch $(@D)/btuart $(@D)/0001-btuart-reduced-baud-rate-rpi3b.patch
+endef
+
define BTSPEAKER_INSTALL_TARGET_CMDS
$(INSTALL) -D -m 644 $(@D)/btspeaker.service \
$(TARGET_DIR)/usr/lib/systemd/system/btspeaker.service
@@ -17,11 +27,24 @@ define BTSPEAKER_INSTALL_TARGET_CMDS
$(INSTALL) -D -m 644 $(@D)/brcm_bt.service \
$(TARGET_DIR)/usr/lib/systemd/system/brcm_bt.service
+ $(INSTALL) -D -m 644 $(@D)/bthelper@.service \
+ $(TARGET_DIR)/usr/lib/systemd/system/bthelper@.service
+
mkdir -p $(TARGET_DIR)/etc/bluetooth
$(INSTALL) -D -m 644 $(@D)/main.conf \
$(TARGET_DIR)/etc/bluetooth/main.conf
$(INSTALL) -D -m 600 $(@D)/pin.conf \
$(TARGET_DIR)/etc/bluetooth/pin.conf
+
+ $(INSTALL) -d $(TARGET_DIR)/usr/bin
+ $(INSTALL) -m 0755 $(@D)/btuart $(TARGET_DIR)/usr/bin/
+ $(INSTALL) -m 0755 $(@D)/bthelper $(TARGET_DIR)/usr/bin/
+
+ $(INSTALL) -d $(TARGET_DIR)/lib/firmware/brcm
+ $(INSTALL) -m 0644 $(@D)/*.hcd $(TARGET_DIR)/lib/firmware/brcm/
+
+ $(INSTALL) -d $(TARGET_DIR)/usr/lib/udev/rules.d
+ $(INSTALL) -m 0644 $(@D)/90-pi-bluetooth.rules $(TARGET_DIR)/usr/lib/udev/rules.d/
endef
$(eval $(generic-package))