Hardware: Raspberry Pi 3 Model B
Software: Windows 10 / 2016-09-23 Raspbian Jessie Lite (minimal)I was mildly annoyed searching for how to turn the power for USB ports on/off. The official documentation doesn't include it and the instructions online are out of date and/or incomplete. So here it is from scratch! Essentially, you're going to install an additional Linux module, download a third party app's source (https://github.com/codazoda/hub-ctrl.c), compile it and then run it.
These instructions assume a base install of Raspbian as indicated above and a default configuration.
So to get started, log in to your RPi via SSH (http://www.putty.org/) or a local connection and then type in:
sudo apt-get update
sudo apt-get install libusb-dev
git clone https://github.com/codazoda/hub-ctrl.c
gcc -o hub-ctrl hub-ctrl.c -lusb
cp hub-ctrl ..
At this point you're back where you started in the root but hub-ctrl will now function and can turn off your USB and Ethernet port. For a detailed walk through of what the commands above do:
- Update the list of what Linux updates/versions exist and where they are located
- Install the current version of libusb-dev
- Copy the source code of hub-ctrl.c to your machine
- Change into the directory where the hub-ctrl.c code was copied
- Compile it into an executable named hub-ctrl (note the lack of a .c)
- Copy the compiled exectuable to the root directory
- Change back into the root directory
Now it all comes down to running commands against your newly compiled hub-ctrl command.
To jump right into it, here are some examples:
- Turn off Ethernet Port: sudo ./hub-ctrl -h 0 -P 1 -p 0
- Turn on Ethernet Port: sudo ./hub-ctrl -h 0 -P 1 -p 1
- Turn off all USB Ports: sudo ./hub-ctrl -h 0 -P 2 -p 0
- Turn on all USB Ports: sudo ./hub-ctrl -h 0 -P 2 -p 1
Essentially the number after the -P determines the port while the number after the -p (case matters in Linux don't you know) determines the state (0 = off, 1 = on).
Now, the information elsewhere refers to shutting down individual ports and using values of -P to do it. They claim a -P 3 would shut down an individual port. My own experience is...no. While it did not throw an error, no USB devices shut down when attempting individual control. It only worked when I shut all USB ports down.
So to do a full USB reset via power-off/on you could munge together a command like this:
sudo ./hub-ctrl -h 0 -P 2 -p 0 ; sleep 5; sudo ./hub-ctrl -h 0 -P 2 -p 1;
This would bounce all USB devices via a power off, wait 5 seconds, then power them all back on.