Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions bin/omarchy-bluetooth-recover
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

# omarchy:summary=Reload btusb when an rfkill unblock leaves no Bluetooth adapter
# omarchy:group=bluetooth
# omarchy:hidden=true

# omarchy-bluetooth-power keeps the off state in the rfkill soft block, and
# systemd-rfkill restores that block early on the next boot. On Intel CNVi
# adapters that restore can land while btusb is still downloading the firmware:
# the device drops off the bus mid-transfer, the download fails with -19
# (ENODEV), and hci0 is torn down completely.
#
# Bluetooth: hci0: Failed to send firmware data (-19)
# Bluetooth: hci0: FW download error recovery failed (-19)
#
# BlueZ is then left with no controller at all, so the unblock-and-wait in
# power_on() has nothing to wait for and turning Bluetooth back on always
# reports that the adapter did not come up. Reloading btusb starts the firmware
# download over.

set -u

ADAPTER_WAIT_SECONDS=${OMARCHY_BLUETOOTH_ADAPTER_WAIT_SECONDS:-5}

adapter_present() {
compgen -G "/sys/class/bluetooth/hci*" >/dev/null
}

# The ordinary case is an adapter that just needs a moment to appear after the
# unblock. Only a wait that runs out means the firmware never loaded, so nothing
# is reloaded on hardware that does not have this problem.
deadline=$((SECONDS + ADAPTER_WAIT_SECONDS))
while :; do
adapter_present && exit 0
((SECONDS < deadline)) || break
sleep 0.2
done

# Still blocked means Bluetooth is meant to be off, and a missing adapter is the
# correct state rather than something to recover.
rfkill list bluetooth 2>/dev/null | grep -q "Soft blocked: yes" && exit 0

logger -t omarchy-bluetooth-recover "No Bluetooth adapter after unblock; reloading btusb"

modprobe -r btusb btintel 2>/dev/null
modprobe btusb || exit 1
systemctl restart bluetooth
15 changes: 15 additions & 0 deletions bin/omarchy-hw-intel-bluetooth
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

# omarchy:summary=Detect an Intel Bluetooth controller.

# Read sysfs rather than shelling out: usbutils is not part of the base install,
# so lsusb is usually absent. Intel's USB vendor is 8087 and Bluetooth
# controllers report the wireless device class (0xe0).

for device in /sys/bus/usb/devices/*/; do
[[ $(cat "$device/idVendor" 2>/dev/null) == "8087" ]] || continue
[[ $(cat "$device/bDeviceClass" 2>/dev/null) == "e0" ]] || continue
exit 0
done

exit 1
4 changes: 4 additions & 0 deletions default/udev/intel-bluetooth-recover.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Lifting the Bluetooth rfkill block can leave no adapter behind when a
# boot-time block interrupted the firmware download. Check once the block is
# gone, and reload btusb if the adapter never came back.
SUBSYSTEM=="rfkill", ACTION=="change", ENV{RFKILL_TYPE}=="bluetooth", ENV{RFKILL_STATE}=="1", TAG+="systemd", ENV{SYSTEMD_WANTS}+="omarchy-bluetooth-recover.service"
1 change: 1 addition & 0 deletions install/hardware/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ run_logged "$OMARCHY_INSTALL/hardware/intel/ipu7-camera.sh"
run_logged "$OMARCHY_INSTALL/hardware/intel/fred.sh"
run_logged "$OMARCHY_INSTALL/hardware/intel/fix-wifi7-eht.sh"
run_logged "$OMARCHY_INSTALL/hardware/intel/sof-firmware.sh"
run_logged "$OMARCHY_INSTALL/hardware/intel/fix-bluetooth-rfkill-firmware.sh"

run_logged "$OMARCHY_INSTALL/hardware/asus/fix-asus-ptl-display-backlight.sh"
run_logged "$OMARCHY_INSTALL/hardware/asus/fix-asus-ptl-b9406-display.sh"
Expand Down
37 changes: 37 additions & 0 deletions install/hardware/intel/fix-bluetooth-rfkill-firmware.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Recover Intel Bluetooth when an rfkill soft block interrupts the firmware load.
#
# omarchy-bluetooth-power holds the off state in the rfkill soft block because
# that is the half systemd-rfkill restores across reboots. On Intel CNVi adapters
# the restore happens early enough to land while btusb is still downloading the
# firmware: the device drops off the bus mid-transfer, the download fails with
# -19 (ENODEV) and hci0 is torn down, so the machine boots with no Bluetooth
# controller rather than one that is merely powered off.
#
# Bluetooth: hci0: Found device firmware: intel/ibt-0040-0041.sfi
# Bluetooth: hci0: Failed to send firmware data (-19)
# Bluetooth: hci0: FW download error recovery failed (-19)
#
# Turning Bluetooth back on cannot recover from that on its own: power_on() lifts
# the block and waits for BlueZ to power an adapter that no longer exists, then
# falls back to a bluetoothctl power on that has no controller to address.
#
# So watch for the unblock and reload btusb when it leaves no adapter behind.

if omarchy-hw-intel-bluetooth; then
sudo mkdir -p /etc/systemd/system /etc/udev/rules.d

sudo tee /etc/systemd/system/omarchy-bluetooth-recover.service >/dev/null <<'UNIT'
[Unit]
Description=Recover the Bluetooth adapter after an rfkill unblock

[Service]
Type=oneshot
ExecStart=/usr/bin/omarchy-bluetooth-recover
UNIT

sudo cp -f "$OMARCHY_PATH/default/udev/intel-bluetooth-recover.rules" \
/etc/udev/rules.d/90-omarchy-bluetooth-recover.rules

sudo systemctl daemon-reload
sudo udevadm control --reload-rules
fi