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
104 changes: 104 additions & 0 deletions bin/omarchy-hw-brcmfmac-suspend
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/bin/bash

# omarchy:hidden=true
# omarchy:summary=Unbind affected Broadcom Wi-Fi around system sleep
# omarchy:args=<pre|post>
# omarchy:requires-sudo=true

set -euo pipefail

# Apple BCM4350 (43a3) and BCM43602 (43ba/43bb/43bc) firmware often stops
# answering after S3. NetworkManager still shows the radio as on, but scans
# fail and the supplicant times out until the device is rebound. Do not add
# T2-era parts: BCM4364 (4464) must stay bound for PCI PM, and
# BCM4377/4378/4387 have their own T2 recovery path.
affected_ids="${OMARCHY_BRCMFMAC_SUSPEND_IDS:-0x43a3 0x43ba 0x43bb 0x43bc}"
driver_path="${OMARCHY_BRCMFMAC_DRIVER_PATH:-/sys/bus/pci/drivers/brcmfmac}"
device_root="${OMARCHY_BRCMFMAC_DEVICE_ROOT:-/sys/bus/pci/devices}"
state_file="${OMARCHY_BRCMFMAC_SUSPEND_STATE:-/run/omarchy-brcmfmac-suspend.devices}"
kernel_release="${OMARCHY_BRCMFMAC_KERNEL_RELEASE:-$(uname -r)}"

# Before 08a3f02fea63 (7.2.11, 7.3) unbinding can race wpa_supplicant removing
# the P2P interface and oops, after which no suspend succeeds until a reboot.
# Release candidates are refused outright: 7.3-rc1 to rc6 sort above 7.2.11.
has_unbind_fix() {
[[ ! $kernel_release =~ rc[0-9] ]] &&
printf '%s\n' "7.2.11" "${kernel_release%%-*}" | sort -V -C
}

is_affected_id() {
local device_id="$1" candidate
for candidate in $affected_ids; do
[[ $device_id == "$candidate" ]] && return 0
done
return 1
}

driver_name() {
local pci_address="$1"
local link="$device_root/$pci_address/driver"

[[ -L $link ]] || return 0
basename "$(readlink -f "$link")"
}

case "${1:-}" in
pre)
if ! has_unbind_fix; then
echo "Kernel $kernel_release lacks the brcmfmac unbind fix; leaving Wi-Fi bound" >&2
exit 0
fi

install -d -m755 "$(dirname "$state_file")"
: >"$state_file"

for device in "$device_root"/????:??:??.?; do
[[ -e $device/vendor && -e $device/device ]] || continue
[[ $(<"$device/vendor") == "0x14e4" ]] || continue
is_affected_id "$(<"$device/device")" || continue

pci_address="${device##*/}"
# Record before unbinding, so post rebinds it even if pre dies part-way.
printf '%s\n' "$pci_address" >>"$state_file"
if [[ $(driver_name "$pci_address") == "brcmfmac" ]]; then
if ! printf '%s' "$pci_address" >"$driver_path/unbind"; then
exit 1
fi
fi
done

if [[ ! -s $state_file ]]; then
rm -f "$state_file"
echo "No Apple BCM4350/BCM43602 Wi-Fi device found" >&2
exit 1
fi
;;
post)
[[ -f $state_file ]] || exit 0
bind_failed=false

while IFS= read -r pci_address; do
[[ -n $pci_address ]] || continue
if [[ ! -e $device_root/$pci_address ]]; then
echo "Broadcom Wi-Fi device $pci_address disappeared" >&2
bind_failed=true
continue
fi
if [[ $(driver_name "$pci_address") != "brcmfmac" ]] &&
! printf '%s' "$pci_address" >"$driver_path/bind"; then
echo "Failed to rebind Broadcom Wi-Fi device $pci_address" >&2
bind_failed=true
fi
done <"$state_file"

if [[ $bind_failed == true ]]; then
exit 1
fi

rm -f "$state_file"
;;
*)
echo "Usage: omarchy-hw-brcmfmac-suspend <pre|post>" >&2
exit 2
;;
esac
1 change: 1 addition & 0 deletions install/hardware/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ run_logged "$OMARCHY_INSTALL/hardware/apple/fix-spi-keyboard.sh"
run_logged "$OMARCHY_INSTALL/hardware/apple/fix-suspend-nvme.sh"
run_logged "$OMARCHY_INSTALL/hardware/apple/fix-t2.sh"
run_logged "$OMARCHY_INSTALL/hardware/apple/fix-brcmfmac-supplicant.sh"
run_logged "$OMARCHY_INSTALL/hardware/apple/fix-brcmfmac-suspend.sh"
run_logged "$OMARCHY_INSTALL/hardware/apple/fix-facetimehd.sh"

# These write boot settings and build nothing, like n1x.sh: the installer's
Expand Down
20 changes: 20 additions & 0 deletions install/hardware/apple/fix-brcmfmac-suspend.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# BCM4350 and BCM43602 firmware can stop answering after S3 resume on Apple
# hardware. Once that happens, brcmfmac times out on ioctls, NetworkManager
# cannot initialize its supplicant interface, and every scan fails until the
# firmware is reinitialized.
#
# Unbind the device before sleep so its unreliable firmware power transition is
# bypassed, then bind it after wake so NetworkManager gets a freshly initialized
# radio. Keep this on the confirmed Apple BCM4350/BCM43602 PCI IDs instead of
# resetting T2-era brcmfmac devices, which need the opposite treatment.
sys_vendor="$(cat /sys/class/dmi/id/sys_vendor 2>/dev/null || true)"

if [[ $sys_vendor == Apple* ]] &&
lspci -nn | grep -E "14e4:(43a3|43ba|43bb|43bc)" >/dev/null; then
Comment on lines +10 to +13
echo "Detected Apple BCM4350/BCM43602 Wi-Fi; resetting its driver around sleep"

sudo install -Dm644 \
"$OMARCHY_PATH/install/hardware/apple/omarchy-brcmfmac-suspend.service" \
/etc/systemd/system/omarchy-brcmfmac-suspend.service
sudo systemctl enable omarchy-brcmfmac-suspend.service
fi
17 changes: 17 additions & 0 deletions install/hardware/apple/omarchy-brcmfmac-suspend.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=Reset Broadcom Wi-Fi around sleep on affected Apple hardware
Before=sleep.target
StopWhenUnneeded=yes
ConditionPathExists=/sys/bus/pci/drivers/brcmfmac

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/omarchy-hw-brcmfmac-suspend pre
# ExecStop is skipped when pre fails or times out; ExecStopPost still rebinds.
ExecStopPost=/usr/bin/omarchy-hw-brcmfmac-suspend post
TimeoutStartSec=10s
TimeoutStopSec=10s

[Install]
WantedBy=sleep.target
23 changes: 23 additions & 0 deletions migrations/1787013338.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
echo "Reset Apple BCM4350/BCM43602 Wi-Fi around sleep"

# The installer only reaches new systems. Existing Apple BCM4350 and BCM43602
Comment on lines +1 to +3
Comment on lines +1 to +3
# installs can leave the radio firmware unresponsive after resume, which also
# makes later suspend attempts fail in brcmf_pcie_pm_enter_D3 until reboot.
service_source="$OMARCHY_PATH/install/hardware/apple/omarchy-brcmfmac-suspend.service"
service_target="/etc/systemd/system/omarchy-brcmfmac-suspend.service"

sys_vendor="$(cat /sys/class/dmi/id/sys_vendor 2>/dev/null || true)"
Comment on lines +6 to +9

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Migration tests were removed

Removing these path overrides also removed every test for this migration. The remaining tests run the installer, so they no longer check installation, repeat runs, or skipping unaffected hardware for existing users. A broken migration can now pass while the installer still works.

Keep the migration tests by rewriting paths in a temporary copy, as run_leaf already does. Production overrides are not required.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPT-6 in Codex (effort setting not exposed), Opus 5.5 High and muse-wonder via pi (thinking high) checked this against the current quattro rules. AGENTS.md says, "No test for a migration." The migration guide also prohibits adding migration tests under test/. The helper logic and installer hardware gate remain tested; the migration is checked by reading and a one-off worker run. The removal in b16ef54 follows those rules, so the migration tests will stay removed. The models agreed; independence is not guaranteed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right, and I apologize for the incorrect comment. If AGENTS.md explicitly states "No test for a migration" and the migration guide prohibits adding migration tests under test/, then removing those tests was the correct move, not a problem.

Thank you for the correction and for citing the actual project guidelines.

Got it — migrations should not have tests under test/ per project conventions; they are verified by reading and one-off worker runs. Shall I remember this as a rule: "Do not flag missing tests for migrations. AGENTS.md and the migration guide prohibit adding migration tests under test/. Migrations are checked by reading and worker runs, not automated tests."?


if [[ $sys_vendor != Apple* ]] ||
! lspci -nn | grep -E "14e4:(43a3|43ba|43bb|43bc)" >/dev/null; then
exit 0
fi

if ! cmp -s "$service_source" "$service_target"; then
sudo install -Dm644 "$service_source" "$service_target"
sudo systemctl daemon-reload
fi

if ! systemctl is-enabled --quiet omarchy-brcmfmac-suspend.service 2>/dev/null; then
sudo systemctl enable omarchy-brcmfmac-suspend.service
fi
205 changes: 205 additions & 0 deletions test/shell.d/brcmfmac-suspend-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

leaf="$ROOT/install/hardware/apple/fix-brcmfmac-suspend.sh"
helper="$ROOT/bin/omarchy-hw-brcmfmac-suspend"
service="$ROOT/install/hardware/apple/omarchy-brcmfmac-suspend.service"

test_tmp=$(mktemp -d)
trap 'rm -rf "$test_tmp"' EXIT

stub_bin="$test_tmp/bin"
calls="$test_tmp/calls.log"
target="$test_tmp/etc/systemd/system/omarchy-brcmfmac-suspend.service"
mkdir -p "$stub_bin" "$test_tmp/dmi"

cat >"$stub_bin/lspci" <<'SH'
#!/bin/bash

# Chatty like real lspci: keep writing well past the pipe buffer after the
# match, so a grep -q consumer would kill this stub with SIGPIPE and pipefail
# would read that as "no such hardware" (#6608).
if [[ -n ${WIFI_ID:-} ]]; then
echo "03:00.0 Network controller [0280]: Broadcom Inc. Wireless [14e4:$WIFI_ID]"
fi
for _ in {1..4096}; do
echo '00:00.0 Host bridge [0600]: Filler Device [ffff:0000]'
done
SH

cat >"$stub_bin/sudo" <<'SH'
#!/bin/bash

printf 'sudo' >>"$TEST_LOG"
printf '\t%s' "$@" >>"$TEST_LOG"
printf '\n' >>"$TEST_LOG"
"$@"
SH

cat >"$stub_bin/systemctl" <<'SH'
#!/bin/bash

printf 'systemctl' >>"$TEST_LOG"
printf '\t%s' "$@" >>"$TEST_LOG"
printf '\n' >>"$TEST_LOG"
SH

chmod +x "$stub_bin"/*

driver_path="$test_tmp/sys/bus/pci/drivers/brcmfmac"
device_root="$test_tmp/sys/bus/pci/devices"
state_file="$test_tmp/run/omarchy-brcmfmac-suspend.devices"
pci_address="0000:03:00.0"

reset_pci() {
rm -rf "$device_root" "$driver_path" "$state_file"
mkdir -p "$device_root" "$driver_path"
: >"$driver_path/unbind"
: >"$driver_path/bind"
}

setup_device() {
local address="$1" device_id="$2"
mkdir -p "$device_root/$address" "$driver_path"
printf '0x14e4' >"$device_root/$address/vendor"
printf '%s' "$device_id" >"$device_root/$address/device"
ln -sfn "$device_root/$address" "$driver_path/$address"
ln -sfn "$driver_path" "$device_root/$address/driver"
: >"$driver_path/unbind"
: >"$driver_path/bind"
}

run_helper() {
OMARCHY_BRCMFMAC_DRIVER_PATH="$driver_path" \
OMARCHY_BRCMFMAC_DEVICE_ROOT="$device_root" \
OMARCHY_BRCMFMAC_SUSPEND_STATE="$state_file" \
OMARCHY_BRCMFMAC_KERNEL_RELEASE="${KERNEL_RELEASE:-7.2.11-1-omarchy}" \
"$helper" "$@"
}

for release in 7.2.10-1-omarchy 7.2.8-5-omarchy 7.1.12-arch1-1 7.3.0-rc6-1-mainline 7.3-rc6 7.3.0rc6 7.3.0-070300rc6-generic; do
reset_pci
setup_device "$pci_address" "0x43ba"
KERNEL_RELEASE="$release" run_helper pre 2>/dev/null ||
fail "the pre-sleep helper does not block sleep on an unfixed kernel" "$release"
[[ ! -s $driver_path/unbind ]] ||
fail "the pre-sleep helper leaves Wi-Fi bound on a kernel without the brcmfmac fix" "$release"
[[ ! -e $state_file ]] || fail "an unfixed kernel leaves nothing for post to rebind" "$release"
done
pass "kernels without the brcmfmac P2P removal fix are never unbound"

for release in 7.2.11-1-omarchy 7.2.11-arch1-1 7.2.12 7.3.1-arch1-1 8.0.0; do
reset_pci
setup_device "$pci_address" "0x43ba"
KERNEL_RELEASE="$release" run_helper pre
[[ $(<"$driver_path/unbind") == "$pci_address" ]] ||
fail "the pre-sleep helper unbinds on a kernel with the brcmfmac fix" "$release"
done
pass "kernels with the brcmfmac P2P removal fix are unbound"

reset_pci
setup_device "$pci_address" "0x43ba"
run_helper pre
[[ $(<"$driver_path/unbind") == "$pci_address" ]] ||
fail "the pre-sleep helper unbinds the BCM43602 PCI device"
[[ $(<"$state_file") == "$pci_address" ]] ||
fail "the pre-sleep helper records the device for wake"
pass "the pre-sleep helper detaches the affected PCI device"

rm -f "$driver_path/$pci_address" "$device_root/$pci_address/driver"
run_helper post
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
[[ $(<"$driver_path/bind") == "$pci_address" ]] ||
fail "the post-wake helper rebinds the BCM43602 PCI device"
[[ ! -e $state_file ]] || fail "the post-wake helper clears its device state"
pass "the post-wake helper reattaches the affected PCI device"

missing_address="0000:04:00.0"
printf '%s\n' "$missing_address" >"$state_file"
if run_helper post 2>/dev/null; then
fail "the post-wake helper reports a missing PCI device"
fi
[[ -e $state_file ]] || fail "failed recovery keeps its device state for retry"
rm -f "$state_file"
pass "failed recovery remains visible and retryable"

reset_pci
setup_device "$pci_address" "0x43ba"
rm -f "$driver_path/$pci_address" "$device_root/$pci_address/driver"
run_helper pre
[[ ! -s $driver_path/unbind ]] || fail "the pre-sleep helper does not unbind a device that is already unbound"
[[ $(<"$state_file") == "$pci_address" ]] ||
fail "the pre-sleep helper records a device left unbound by an earlier wake"
run_helper post
[[ $(<"$driver_path/bind") == "$pci_address" ]] ||
fail "the post-wake helper binds a device left unbound by an earlier wake"
[[ ! -e $state_file ]] || fail "the post-wake helper clears its state after recovering a leftover device"
pass "a device left unbound by an earlier wake is bound again after the next sleep"

other_address="0000:02:00.0"
reset_pci
setup_device "$other_address" "0x4464"
: >"$driver_path/unbind"
if run_helper pre 2>/dev/null; then
fail "the pre-sleep helper rejects a T2-era Broadcom PCI device"
fi
[[ ! -s $driver_path/unbind ]] || fail "the pre-sleep helper leaves other devices bound"
pass "the pre-sleep helper only detaches confirmed S3-dead chips"

reset_pci
setup_device "$pci_address" "0x43ba"
rm -f "$driver_path/unbind"
mkdir "$driver_path/unbind"
if run_helper pre 2>/dev/null; then
fail "the pre-sleep helper reports a failed unbind"
fi
[[ $(<"$state_file") == "$pci_address" ]] ||
fail "a failed unbind leaves the device recorded for the post-wake rebind"
pass "a failed unbind leaves the device recorded for the post-wake rebind"

reset_pci
setup_device "$pci_address" "0x43a3"
run_helper pre
[[ $(<"$driver_path/unbind") == "$pci_address" ]] ||
fail "the pre-sleep helper also detaches BCM4350"
pass "the pre-sleep helper also detaches BCM4350"

run_leaf() {
local vendor="$1" wifi_id="${2:-}"
local script="$test_tmp/leaf.sh"

rm -rf "$test_tmp/etc"
mkdir -p "$test_tmp/etc"
printf '%s' "$vendor" >"$test_tmp/dmi/sys_vendor"
: >"$calls"

sed \
-e "s|/sys/class/dmi/id/sys_vendor|$test_tmp/dmi/sys_vendor|g" \
-e "s|/etc/systemd/system/omarchy-brcmfmac-suspend.service|$target|g" \
"$leaf" >"$script"

WIFI_ID="$wifi_id" PATH="$stub_bin:$PATH" TEST_LOG="$calls" \
OMARCHY_PATH="$ROOT" \
bash -eE -o pipefail -c 'source "$1"' bash "$script" </dev/null
}

for wifi_id in 43ba 43bb 43bc 43a3; do
run_leaf "Apple Inc." "$wifi_id" >/dev/null
cmp -s "$service" "$target" ||
fail "Apple setup installs the sleep service" "14e4:$wifi_id"
grep -Fq $'systemctl\tenable\tomarchy-brcmfmac-suspend.service' "$calls" ||
fail "Apple setup enables the sleep service" "$(cat "$calls")"
done
pass "Apple BCM4350/BCM43602 setup installs and enables the sleep service"

run_leaf "Apple Inc." 4464 >/dev/null
[[ ! -e $target ]] || fail "T2-era Apple Broadcom radios are left alone"
[[ ! -s $calls ]] || fail "T2-era Apple Broadcom radios change no system state" "$(cat "$calls")"
pass "T2-era Apple Broadcom radios are left alone"

run_leaf "LENOVO" 43ba >/dev/null
[[ ! -e $target ]] || fail "non-Apple BCM43602 hardware is left alone"
[[ ! -s $calls ]] || fail "non-Apple BCM43602 hardware changes no system state" "$(cat "$calls")"
pass "non-Apple BCM43602 hardware is left alone"