0

Is there a way with pyusb to unbind a USB device?

I know using the following bash the USB is unbound.

DEVICE=$(grep 064f /sys/bus/usb/devices/*/idVendor | tr '/' ' ' | awk '{ print $5 }')
/bin/bash -c "echo $DEVICE >/sys/bus/usb/drivers/usb/unbind"

But for various reasons I like to move away from bash and switch to Python, and ideally avoid maintaining my custom, complicated logic. So using a existing library makes sense to me.

Selected answer in stackoverflow.com#q54863367 suggests detach_kernel_driver to work for this purpose, but I don't see that happening on my environment; It does unmount the volume in the designated USB device (confirmed by watching the disk space on the USB disappears in lsblk's output) but I still see that OS detects the USB device.

$ ipython
In [7]: import usb
   ...: dev = usb.core.find(idVendor=0x064f, idProduct=0x03f3)
In [8]: dev.detach_kernel_driver(0)
$ watch lsusb
:
Bus 002 Device 043: ID 064f:03f3 WIBU-Systems AG CmStick/M (article no. 1011)

Environment

Linux (At the time of writing, Ubuntu 16.04 (I know EoLed) or 18.04. But environment shouldn't be a limiting factor. Open for available solutions regardless the version.


UPDATE: My usecase requires mimicing removal of USB device. We've been happy with the operation typically called as un/bind, and also happy with the bash solution to realize un/bind.

IsaacS
  • 3,551
  • 6
  • 39
  • 61

2 Answers2

1

A quick search of the PyUSB code makes it seem like there is no feature for binding or unbinding. So PyUSB is not the answer.

However, you don't need to use Bash to unbind a device. Python has a standard library that lets you get directory listings, read files, and write to files, so you can just use Python's standard library instead of Bash.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • Thx for confirming about `PyUSB`. To clarify I added update to OP, IINM I need to un/bind USB and we've only found `bash`-way to achieve that. – IsaacS Dec 03 '21 at 19:54
  • 1
    Repeating the point I made in my second paragraph: your Bash script isn't doing anything too complicated. It's reading a directory, reading some files, and then writing a little bit of text to a file. These things are simple to do in Python. Have you tried making a pure Python implementation? What exact step are you stuck on? – David Grayson Dec 03 '21 at 19:59
  • Just looking at what your Bash script does, I can tell tell that the first step would be to get a directory listing of `/sys/bus/usb/devices`. Then descend into every subdirectory of it (each subdirectory represents a device). Then read the `idVendor` file in each of those subdirectories. Then figure out what the correct `DEVICE` string is. Then open the `unbind` file and write that string to it. Then close the `unbind` file. – David Grayson Dec 03 '21 at 20:04
  • Oh! I now know why you mentioned those standard libs. Sounds reasonable enough. I'll try and report back. – IsaacS Dec 03 '21 at 21:48
0

Thought I'd close OP but coudln't choose an appripriate reason so answer by myself instead.

As I concluded myself with a help from the maintainer in pyusb#399 I found I was misunderstood. Using detach_kernel_driver as suggested in stackoverflow.com#q54863367 worked for my purpose as well.

IsaacS
  • 3,551
  • 6
  • 39
  • 61