I've not been able to test this on a PiZero, but it does work on other RPi hardware. It may be worth a try - I'll leave that to you. In either case, please give us the courtesy of some feedback: either you did not try it, or you did & the result.
1. install uhubctl
$ sudo apt update && sudo apt full-upgrade -y
$ sudo apt install uhubctl
2. list USB hubs
$ sudo uhubctl
# NOTE: my output shown (w/ 1 USB device connected @ Port 2) for illustration
Current status for hub 2 [1d6b:0003 Linux 5.4.51-v7l+ xhci-hcd xHCI Host Controller 0000:01:00.0, USB 3.00, 4 ports]
Port 1: 02a0 power 5gbps Rx.Detect
Port 2: 0203 power 5gbps U0 enable connect [1058:0748 Western Digital My Passport <numeric string>]
Port 3: 02a0 power 5gbps Rx.Detect
Port 4: 02a0 power 5gbps Rx.Detect
Current status for hub 1 [1d6b:0002 Linux 5.4.51-v7l+ xhci-hcd xHCI Host Controller 0000:01:00.0, USB 2.00, 1 ports]
Port 1: 0507 power highspeed suspend enable connect [2109:3431 USB2.0 Hub, USB 2.10, 4 ports]
If uhubctl
finds a USB hub that supports power switching, it will be listed here. It will not list hubs that do not support power switching.
If I understand your setup correctly, you have connected an external hub to your Pi Zero. According to the uhubctl
docs, you should be able to power-switch an external hub if that hub supports power-switching.
3. power your hub up or down
Assuming your external hub is listed in Step 2 above, you should now refer to the uhubctl
docs for further instructions. As an outline, I would suggest:
- check the status of the hub of interest using the
hub #
from above; e.g. for port 2
of my hub 2
from above:
$ sudo uhubctl -l 2 -p 2
Current status for hub 2 [1d6b:0003 Linux 5.4.51-v7l+ xhci-hcd xHCI Host Controller 0000:01:00.0, USB 3.00, 4 ports]
Port 2: 0203 power 5gbps U0 enable connect [1058:0748 Western Digital My Passport <numeric string>]
Current status for hub 1 [1d6b:0002 Linux 5.4.51-v7l+ xhci-hcd xHCI Host Controller 0000:01:00.0, USB 2.00, 1 ports]
- turn off power to a port (NOTE 1: RPi hardware does not support powering individual ports - all ports are either ON or they are all OFF. NOTE 2: This limitation may or may not apply to an external hub - that will depend on the manufacturer's implementation)
$ sudo uhubctl -l 2 -p 2 -a off
...
Sent power off request
# more output...
- check status of powered-off port
$ sudo uhubctl -l 2 -p 2
Current status for hub 2 [1d6b:0003 Linux 5.4.51-v7l+ xhci-hcd xHCI Host Controller 0000:01:00.0, USB 3.00, 4 ports]
Port 2: 0080 off
Current status for hub 1 [1d6b:0002 Linux 5.4.51-v7l+ xhci-hcd xHCI Host Controller 0000:01:00.0, USB 2.00, 1 ports]
4. Conclusion:
This works on my RPi 4 (as shown above), and has also worked on my Rpi 3B+. AFAIK, it should also work on the RPi Zero - we await your feedback.
EDIT: Have just realized that while uhubctl
appears to work on my RPi 4B, it does not. However it does work on my RPi 3B+, and I am told that an upgrade to ver 2.2 of uhubctl
will resolve issues with the RPi 4B.
REFERENCES:
Power Management for USB; p/o Docs > The Linux driver API guide > Linux USB API
What is sysfs
?
How to turn USB port power on and off in Raspberry PI 4
Raspberry Pi USB Hardware Documentation
GitHub site for uhubctl
GitHub site for hub-ctrl.c