#!/bin/sh
# linuxlite-broadcom-ac-blacklist
# -----------------------------------------------------------------------------
# If a wl-only Broadcom 802.11ac Wi-Fi chip is present, blacklist the in-tree
# b43/bcma SoftMAC drivers. Those drivers CANNOT drive these chips (b43 logs
# "UNSUPPORTED PHY") and `bcma` bus init can wedge boot for ~90s on a warm
# reboot (HT force / PLL enable timeout). The card's real driver is the
# proprietary broadcom-sta (`wl`), installable from Lite Driver Manager, which
# expects exactly these modules blacklisted.
#
# ID-SCOPED ON PURPOSE: the blacklist is written ONLY when one of these
# specific 802.11ac device IDs is present, so OLDER Broadcom chips that b43
# legitimately drives (11b/g/n: 4311/4312/4331/...) are never touched.
#
# Run in the target by Calamares at install time (so a shipped/OEM laptop never
# wedges on first boot) and safe to run standalone. Fail-safe: any error leaves
# modules loading normally.
# -----------------------------------------------------------------------------
CONF=/etc/modprobe.d/linuxlite-broadcom-ac.conf

# Vendor 14e4 device IDs that are 802.11ac and b43-INCOMPATIBLE (wl-only).
# This is deliberately the wl-only AC subset, NOT every broadcom-sta card.
# Extend only after confirming (dmesg "UNSUPPORTED PHY") b43 can't drive an ID.
#   0x43b1 = BCM4352 (802.11ac)
#   0x43a0 = BCM4360 (802.11ac)
for d in /sys/bus/pci/devices/*/; do
    [ -r "$d/vendor" ] && [ -r "$d/device" ] || continue
    v=$(cat "$d/vendor" 2>/dev/null)
    p=$(cat "$d/device" 2>/dev/null)
    [ "$v" = "0x14e4" ] || continue
    case "$p" in
        0x43b1|0x43a0)
            {
                echo "# Linux Lite: a wl-only Broadcom 802.11ac chip (14e4:${p#0x}) is present."
                echo "# b43/bcma cannot drive it and bcma bus init can wedge boot (~90s)."
                echo "# Install broadcom-sta (wl) from Lite Driver Manager for Wi-Fi."
                echo "blacklist bcma"
                echo "blacklist b43"
                echo "blacklist b43legacy"
                echo "blacklist brcmsmac"
                echo "blacklist ssb"
            } > "$CONF" 2>/dev/null
            exit 0
            ;;
    esac
done

# No wl-only AC card here -> drop any stale blacklist (e.g. from a cloned image
# whose master had such a card) so this machine's own Broadcom works normally.
rm -f "$CONF" 2>/dev/null
exit 0
