#!/bin/bash # # Purpose : Set up what particular hardware needs, adapted from Omarchy: # - Wi-Fi: the regulatory database, and the country from your time # zone, so Wi-Fi can use the channels and power allowed there # - Intel laptops: thermald and intel-lpmd, for cooler running and # longer battery life # - Intel BE200/BE211 Wi-Fi cards: Wi-Fi 6 instead of Wi-Fi 7, which # their driver gets wrong # Each takes effect after a reboot. set -e COREDESKTOP="${COREDESKTOP:-$HOME/.local/share/coredesktop}" source "$COREDESKTOP/utils.sh" # For the tests: read /sys, /proc, /etc and /usr from under here ROOT="${HARDWARE_ROOT:-}" has_wifi() { compgen -G "$ROOT/sys/class/ieee80211/*" > /dev/null } has_battery() { compgen -G "$ROOT/sys/class/power_supply/BAT*" > /dev/null } # A field of the first CPU in /proc/cpuinfo, e.g. vendor_id or model cpu_field() { sed -nE "s/^$1[[:space:]]*:[[:space:]]*//p" "$ROOT/proc/cpuinfo" 2> /dev/null | head -n 1 } intel_cpu() { [ "$(cpu_field vendor_id)" = GenuineIntel ] && [ "$(cpu_field 'cpu family')" = 6 ] } # PCI devices, as vendor:device (8086:272b) pci_ids() { local device for device in "$ROOT"/sys/bus/pci/devices/*; do [ -r "$device/vendor" ] && [ -r "$device/device" ] || continue echo "$(sed 's/^0x//' "$device/vendor"):$(sed 's/^0x//' "$device/device")" done } # The two-letter country of the time zone /etc/localtime points at timezone_country() { local zone zone="$(readlink -f "$ROOT/etc/localtime" 2> /dev/null)" || return 1 zone="${zone#*/zoneinfo/}" [ -n "$zone" ] || return 1 awk -v zone="$zone" '!/^#/ && $3 == zone { print $1; exit }' "$ROOT/usr/share/zoneinfo/zone.tab" 2> /dev/null } # Wi-Fi starts in the "world" regulatory domain, the channels and power allowed # everywhere, until it knows the country. wireless-regdb holds each country's # rules, and sets the one in /etc/conf.d/wireless-regdom at boot. setup_wifi_country() { local conf="$ROOT/etc/conf.d/wireless-regdom" country has_wifi || return 0 install_packages wireless-regdb # Set already, by you or before grep -qE '^[[:space:]]*WIRELESS_REGDOM=' "$conf" 2> /dev/null && return 0 country="$(timezone_country)" if [[ ! "$country" =~ ^[A-Z]{2}$ ]]; then echo "Couldn't tell the country from the time zone; set it in /etc/conf.d/wireless-regdom." return 0 fi echo "Setting the Wi-Fi country to $country (from the time zone)..." echo "WIRELESS_REGDOM=\"$country\"" | sudo tee -a "$conf" > /dev/null } # thermald keeps Intel laptops from overheating before the firmware has to # throttle hard. Sandy Bridge (model 42) and newer. # intel-lpmd moves background work onto the low-power cores of Intel's hybrid # CPUs: Alder Lake, Raptor Lake, Meteor Lake, Lunar Lake and Panther Lake. setup_intel_laptop() { local model intel_cpu && has_battery || return 0 model="$(cpu_field model)" [[ "$model" =~ ^[0-9]+$ ]] || return 0 if (( model >= 42 )); then install_packages thermald enable_services thermald.service fi if [[ " 151 154 170 172 183 186 189 191 204 " == *" $model "* ]]; then install_packages intel-lpmd enable_services intel_lpmd.service fi } # Intel's BE200 (8086:272b) and BE211 (8086:e440) Wi-Fi 7 cards: with Wi-Fi 7 # (802.11be) the iwlwifi driver drops to its slowest rate, which makes Wi-Fi # unusable on some access points (seen on Dell XPS 14 and 16). Wi-Fi 6 runs at # full speed. Remove the file once Intel fixes the driver. setup_intel_wifi7() { local conf="$ROOT/etc/modprobe.d/iwlwifi-disable-eht.conf" pci_ids | grep -qxE '8086:(272b|e440)' || return 0 [ -f "$conf" ] && return 0 echo "Using Wi-Fi 6 on this Intel Wi-Fi 7 card, whose driver gets Wi-Fi 7 wrong..." sudo install -d -m 755 "$(dirname "$conf")" sudo tee "$conf" > /dev/null << 'EOF' # CoreDesktop (system/hardware.sh): Intel BE200/BE211 cards fall to their # slowest rate with Wi-Fi 7 (802.11be). Use Wi-Fi 6 until the iwlwifi driver # is fixed; delete this file to try Wi-Fi 7 again. options iwlwifi disable_11be=Y EOF } setup_wifi_country setup_intel_laptop setup_intel_wifi7