Search code examples
pythonusbpyusb

How to bind/unbind usb device in PyUsb?


I need to on/off several usb devices on my python script. Can i bind and unbind usb devices with PyUsb?

I can do it with shell commands:
Power off:
echo "device_nuber" > /sys/bus/usb/drivers/usb/unbind
Power on:
echo "device_nuber" > /sys/bus/usb/drivers/usb/bind

How execute the same in python script?


Solution

  • You can do this with attach_kernel_driver and detach_kernel_driver.

    
    import usb.core
    dev = usb.core.find(idVendor=0x1234,idProduct=0x5678)
    # unbind interface 0
    dev.detach_kernel_driver(0)
    # bind interface 0
    dev.attach_kernel_driver(0)