Skip to content

Instantly share code, notes, and snippets.

@sashomasho
Created April 8, 2019 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sashomasho/ae3477f3bd3a03220eb51e12c28b4f26 to your computer and use it in GitHub Desktop.
Save sashomasho/ae3477f3bd3a03220eb51e12c28b4f26 to your computer and use it in GitHub Desktop.
#!/bin/sh
#TODO: Pass interface name in as a parameter. Right now wlp3s0 is assumed.
# Figure out what pci slot Linux has assigned the Network controller: Intel Corporation Wireless 7260
wirelessPCI=$(lspci |grep "Wireless 7260")
pci=$(echo ${wirelessPCI} | awk '{ print $1 }')
devicePath="/sys/bus/pci/devices/0000:$pci/remove"
# Not the best solution as this script can hang.
# But since if this script fails the ONLY way to revive the wifi anyway is a reboot...
# Feel free to improve the script if you have the scriptfu ninja skills to do so.
while true; do
# Tell Linux to remove the wifi card from the PCI device list only if it exists in the first place.
if [ -f $devicePath ]; then
echo 'removing device'
echo 1 | sudo tee $devicePath > /dev/null
sleep 1
fi
# Reprobe the driver modules in case we have removed them in a failed attempt to wake the network card.
echo 'reprobing drivers'
sudo modprobe iwlmvm
sudo modprobe iwlwifi
# Try to have Linux bring the network card back online as a PCI device.
echo 'pci rescan'
echo 1 | sudo tee /sys/bus/pci/rescan > /dev/null
sleep 1
# Check if Linux managed to bring the network card back online as a PCI device.
if [ -f $devicePath ]; then
echo 'device is back'
# Looks like we are back in business.
# So we try to set the PCI slot with some voodoo I don't understand that the Intel devs told me to try.
# https://bugzilla.kernel.org/show_bug.cgi?id=191601
sudo setpci -s $pci 0x50.B=0x40
sleep 1
wifiId=$(rfkill list |grep Wireless |awk -F: '{ print $1 }')
echo "rfkill unblock wireless device: $wifiId"
sudo rfkill unblock $wifiId
sleep 1
# Bring the wireless network interface up.
sudo ifconfig wlp3s0 up
# Did the wifi interface actually go live?
exitCode=$?
echo "device UP status $exitCode"
if [ $exitCode -eq 0 ];then
# Not sure why in the hell this is not the default for wireless intefaces.
# It is well documented that: (power_management === ON) === Wifi-Stupidity
sudo iwconfig wlp3s0 power off
# The exit code will be the exit code of our attempt at turning power management off for wlp3s0.
break
fi
else
# It's worse than that the wifi's dead Jim! Dead Jim! Dead!
# We tell Linux to remove the the wifi driver modules and loop back in an attempt to revive the wifi.
echo "it's dead Jim"
sudo modprobe -r iwlmvm
sudo modprobe -r iwlwifi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment