Control the power of a usb port in Python
Asked Answered
U

2

6

I was wondering if it could be possible to control the power of usb ports in Python, using vendor ids and product ids. It should be controlling powers instead of just enabling and disabling the ports. It would be appreciated if you could provide some examples.

Unwonted answered 24/11, 2020 at 16:28 Comment(4)
What do you mean by "power"Bui
I meant power that is bus poweredUnwonted
What's your hardware and OS? I'm pretty sure it's possible on a Raspberry Pi, probably much harder on a PC/Mac.Sainfoin
I'm using LinuxUnwonted
M
4

Look into the subprocess module in the standard library:

What commands you need will depend on the OS.

Windows

For windows you will want to look into devcon

This has been answered in previous posts

import subprocess
# Fetches the list of all usb devices:
result = subprocess.run(['devcon', 'hwids', '=usb'], 
    capture_output=True, text=True)

# ... add code to parse the result and get the hwid of the device you want ...

subprocess.run(['devcon', 'disable', parsed_hwid]) # to disable
subprocess.run(['devcon', 'enable', parsed_hwid]) # to enable

Linux

See posts on shell comands

import subprocess

# determine desired usb device

# to disable
subprocess.run(['echo', '0', '>' '/sys/bus/usb/devices/usbX/power/autosuspend_delay_ms']) 
subprocess.run(['echo', 'auto', '>' '/sys/bus/usb/devices/usbX/power/control']) 
# to enable
subprocess.run(['echo', 'on', '>' '/sys/bus/usb/devices/usbX/power/control']) 

Modulus answered 24/11, 2020 at 16:45 Comment(3)
I'm using Linux. How do I apply vendor ids and product ids to this?Unwonted
You can list the the usb devices using the lsusb command. This will list usb buses and the vender id. Then you can check the vender id and/or product id for each usbX with the cat command. Example: "cat /sys/bus/usb/devices/usb1/idVendor"Modulus
You can also use pyserial's serial.tools.list_ports.comports to get vendor ids.Anamorphoscope
P
0

So far I came to the conclusion that you cannot control the power of a USB port. The 5V USB is always provided, and it's up to the device to use it or not. You can check this with a 5V fan or light. I've tried various methods (disconnect/reconnect/bind/unbind/reset). Best so far are bind/unbind as it forces a cold restart of the device (but no power cycle).

I came up with a solution to reset USB devices, ports and controllers in a python script, which supports all of the above methods. You can find the script at my Github page

Usage:

usb_reset.py -d 8086:1001 --reset-hub

The script uses among others the following solution to reset USB hubs/controllers:

Unbindind a USB port / controller works best via:

echo "myhub" > "/sys/bus/usb/drivers/usb/unbind"
echo "myhub" > "/sys/bus/usb/drivers/usb/bind"

Where myhub is found in /sys/bus/usb/devices/*

Or litteral controllers:

echo "mycontroller" > "/sys/bus/pci/drivers/unbind"
echo "mycontroller" > "/sys/bus/pci/drivers/bind"

Where mycontroller is found in /sys/bus/pci/drivers/[uoex]hci_hcd/*:*

Poaceous answered 28/10, 2022 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.