diff --git a/bin/omarchy-bluetooth-recover b/bin/omarchy-bluetooth-recover new file mode 100755 index 000000000000..12b6c4ec41f8 --- /dev/null +++ b/bin/omarchy-bluetooth-recover @@ -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 diff --git a/bin/omarchy-hw-intel-bluetooth b/bin/omarchy-hw-intel-bluetooth new file mode 100755 index 000000000000..ca9565a76d3e --- /dev/null +++ b/bin/omarchy-hw-intel-bluetooth @@ -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 diff --git a/default/udev/intel-bluetooth-recover.rules b/default/udev/intel-bluetooth-recover.rules new file mode 100644 index 000000000000..179fe7155ee9 --- /dev/null +++ b/default/udev/intel-bluetooth-recover.rules @@ -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" diff --git a/install/hardware/all.sh b/install/hardware/all.sh index 6adcff9c8a43..e3e434519f50 100644 --- a/install/hardware/all.sh +++ b/install/hardware/all.sh @@ -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" diff --git a/install/hardware/intel/fix-bluetooth-rfkill-firmware.sh b/install/hardware/intel/fix-bluetooth-rfkill-firmware.sh new file mode 100644 index 000000000000..b052f8ce7617 --- /dev/null +++ b/install/hardware/intel/fix-bluetooth-rfkill-firmware.sh @@ -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