Linux – How to Logically Eject and Reattach a USB Device

linuxusbusb device

We've bought a commercial application who just work only if their dongle usb is connected to the server. However sometimes the application can not recognize the dongle, so it doesn't work, but if someone eject the dongle physically from USB port and attach it again it will recognize and work fine.

There are 43 modules loaded on the server and attach/eject the dongle does not increase/decrease number of modules.

Also I have usbmon0, usbmon1 and usbmon2 files in /dev before/after eject/attach the dongle and number of files in /dev will not change before/after eject/attach the dongle.

journalctl -f command after ejecting the dongle:

Jan 19 18:10:28 iwr kernel: usb 2-2.1: USB disconnect, device number 5

journalctl -f command after attaching the dongle:

Jan 19 18:11:11 iwr kernel: usb 2-2.1: new full-speed USB device number 6 using uhci_hcd
Jan 19 18:11:11 iwr kernel: usb 2-2.1: New USB device found, idVendor=0403, idProduct=c580
Jan 19 18:11:11 iwr kernel: usb 2-2.1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
Jan 19 18:11:11 iwr kernel: usb 2-2.1: Product: HID UNIKEY
Jan 19 18:11:11 iwr kernel: usb 2-2.1: Manufacturer: OEM
Jan 19 18:11:11 iwr kernel: usbhid 2-2.1:1.0: couldn't find an input interrupt endpoint

Can I eject and then attach it logically? (issue a command, remove a module, etc.)

Best Answer

Many answers found on the Internet (including those in TNW's comment) rely on /sys/bus/usb/devices/2-2/power/level or /sys/bus/usb/devices/2-2/power/control which are both deprecated since 2.6.something kernel. For newer kernels, the suggested procedure is to unbind and rebind its driver, which usually results in a power cycle:

# Find out which driver to unbind
tree /sys/bus/usb/devices/2-2.1 | grep driver
|-- driver -> ../../../../../../bus/usb/drivers/whatever

# Unbind the driver
echo 2-2.1 > /sys/bus/usb/drivers/whatever/unbind

# Rebind the driver
echo 2-2.1 > /sys/bus/usb/drivers/whatever/bind
Related Question