Skip to content

Instantly share code, notes, and snippets.

@hfreire
Last active July 24, 2019 17:39
Show Gist options
  • Save hfreire/32deb6be3791f15692feaa8954e954fe to your computer and use it in GitHub Desktop.
Save hfreire/32deb6be3791f15692feaa8954e954fe to your computer and use it in GitHub Desktop.
Enable/disable power on Raspberry Pi USB ports + Ethernet
#!/bin/sh
SOC_USB=/sys/devices/platform/soc/20980000.usb
if [ ! -d $SOC_USB ];
then
SOC_USB=/sys/devices/platform/soc/3f980000.usb # Raspberry Pi 3
fi
BUSPOWER=$SOC_USB/buspower
is_usb_power_on ()
{
cat $BUSPOWER | grep "Bus Power = 0x1" >/dev/null
}
case $1 in
stop)
if is_usb_power_on
then
echo 0x0 > $BUSPOWER
fi
;;
start)
if ! is_usb_power_on
then
echo 0x1 > $BUSPOWER
fi
;;
status)
if is_usb_power_on
then
echo "USB power is on"
else
echo "USB power is off"
fi
;;
*)
echo "Usage: $0 start|stop|status" >&2
exit 2
;;
esac
exit 0
@suuuehgi
Copy link

suuuehgi commented Oct 2, 2017

This does not work for RPi 3b. There you can:

ON
echo '1-1' > /sys/bus/usb/drivers/usb/bind
OFF
echo '1-1' > /sys/bus/usb/drivers/usb/unbind

For the status: You might check if folders (symlinks) are present in /sys/bus/usb/drivers/usb.

@thijstriemstra
Copy link

is it possible to disable individual usb ports?

@MarcLV
Copy link

MarcLV commented Jul 31, 2018

I've googled a bit and it seems like you cant disable individual usb ports on the RPi 3b

@dotps1
Copy link

dotps1 commented Oct 22, 2018

the code for the rpi 3b doesn't work, permission denied, even using elevation.

@hg42
Copy link

hg42 commented Mar 29, 2019

@dotps1 if you run commands with redirection a simple sudo doesn't work, because the redirection is done by the unprivileged shell (before sudo starts).

Instead you can replace
echo XYZ >PROTECTED-FILE
by something like
echo XYZ | sudo tee PROTECTED-FILE
(and appending like >>FILE would be replaced by | sudo tee -a FILE`)

or become root by sudo su first...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment