Identify, disconnect and reconnect USB device via terminal

As far as I know modprobe is used to add/remove modules / drivers. With the command modprobe -r -f uhci_hcd you are trying to remove a module built in the kernel see this post on Unix & Linux.

If you want to deactivate your USB device and than reactivate it, have a look at his posting here: Disconnect and reconnect USB post via CLI.

Or try the much easier way here: Is there a way to power down a USB device using software?.

Some useful information can be found on this kernel.org page on power management. under USB Port Power Control and below that at User Interface for Port Power Control.

I am running Lubuntu 16.04. My USB device for testing was a 8GB USB stick.

First of all open a terminal and type dmesg -w. Next, plug in your USB device.

[ 1897.772736] usb 2-2: new high-speed USB device number 7 using ehci-pci
[ 1897.907178] usb 2-2: New USB device found, idVendor=13fe, idProduct=5500
[ 1897.907185] usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 1897.907189] usb 2-2: Product: Silicon-Power8G
[ 1897.907193] usb 2-2: Manufacturer: UFD 3.0
[ 1897.907196] usb 2-2: SerialNumber: P1602059070C691668B63659
[ 1897.907802] usb-storage 2-2:1.0: USB Mass Storage device detected
[ 1897.909153] scsi host22: usb-storage 2-2:1.0
[ 1898.910257] scsi 22:0:0:0: Direct-Access     UFD 3.0  Silicon-Power8G  PMAP PQ: 0 ANSI: 6
[ 1898.910896] sd 22:0:0:0: Attached scsi generic sg4 type 0
[ 1898.911521] sd 22:0:0:0: [sdd] 15126528 512-byte logical blocks: (7.74 GB/7.21 GiB)
[ 1898.912261] sd 22:0:0:0: [sdd] Write Protect is off
[ 1898.912266] sd 22:0:0:0: [sdd] Mode Sense: 45 00 00 00
[ 1898.912982] sd 22:0:0:0: [sdd] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[ 1898.950371]  sdd:
[ 1898.953487] sd 22:0:0:0: [sdd] Attached SCSI removable disk
[ 1899.243793] FAT-fs (sdd): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.

Here we can see that a new ehci-pci device at usb 2-2 has been recognized. After a few lines usb 2-2 changes to usb-storage 2-2:1.0 to scsi 22:0:0:0 and finally to sd 22:0:0:0 What these numbers exactly mean is beyond my knowledge. It has something to do with the USB-Port, but that is not everything. At least it is an identifier.

Anyway, now we go for the /sys/ folder. Somewhere inside /sys/ we will find the driver that is responsible for our usb device. We want to unbind our device from that driver. Therefore we need to find out the path to the driver and its unbind command. We are going to use the tree command.

We type tree /sys/ | grep 22:0:0:0

alex@ga-P55A-UD5:~$ tree /sys/ | grep 22:0:0:0
│   ├── sdd -> ../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/block/sdd
│   │   │   ├── 22:0:0:0 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0
│   │   │   │   ├── 22:0:0:0 -> ../../../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0
│   │   ├── sdd -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/block/sdd
│   │   ├── 22:0:0:0 -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/bsg/22:0:0:0
│   │   ├── 22:0:0:0 -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/scsi_device/22:0:0:0
│   │   ├── 22:0:0:0 -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/scsi_disk/22:0:0:0
│   │   └── sg4 -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/scsi_generic/sg4
│   │   ├── 8:48 -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/block/sdd
│       ├── 21:4 -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/scsi_generic/sg4
│       ├── 249:4 -> ../../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/bsg/22:0:0:0
│   │   │   │   │   │   │   │   ├── 22:0:0:0
│   │   │   │   │   │   │   │   │   │       ├── device -> ../../../22:0:0:0
│   │   │   │   │   │   │   │   │   │   └── 22:0:0:0
│   │   │   │   │   │   │   │   │   │       ├── device -> ../../../22:0:0:0
│   │   │   │   │   │   │   │   │   │   └── 22:0:0:0
│   │   │   │   │   │   │   │   │   │       ├── device -> ../../../22:0:0:0
│   │   │   │   │   │   │   │   │   │   └── 22:0:0:0
│   │   │   │   │   │   │   │   │   │       ├── device -> ../../../22:0:0:0
│   │   │   │   │   │   │   │   │   │       ├── device -> ../../../22:0:0:0

The very first line is already telling us where to go:

../devices/pci0000:00/0000:00:1d.7/usb2/2-2/2-2:1.0/host22/target22:0:0/22:0:0:0/block/sdd

Remember, we are still searching for the unbind command somewhere in a driver folder, that is somehow attached to our device. Our device, regarding to dmesg was also called 2-2:1.0, so we use that as parent folder for tree:

tree /sys/devices/pci0000\:00/0000\:00\:1d.7/usb2/2-2/2-2\:1.0/ | grep driver
alex@ga-P55A-UD5:~$ tree /sys/devices/pci0000\:00/0000\:00\:1d.7/usb2/2-2/2-2\:1.0/ | grep driver
├── driver -> ../../../../../../bus/usb/drivers/usb-storage
│   │   │   ├── driver -> ../../../../../../../../../bus/scsi/drivers/sd

Now we have found the path to the driver that is attached to the usb device.

So let's ls /sys/bus/usb/drivers/usb-storage/

alex@ga-P55A-UD5:~$ ls /sys/bus/usb/drivers/usb-storage/
2-2:1.0  bind  module  new_id  remove_id  uevent  unbind

Now we have all the information we need, so we type the following:

echo -n "2-2:1.0" | sudo tee /sys/bus/usb/drivers/usb-storage/unbind`

and right after that, because you wanted the reconnect effect:

echo -n "2-2:1.0" | sudo tee /sys/bus/usb/drivers/usb-storage/bind

Output of dmesg -w:

[ 3516.792135] usb-storage 2-2:1.0: USB Mass Storage device detected
[ 3516.792406] scsi host23: usb-storage 2-2:1.0
[ 3517.792201] scsi 23:0:0:0: Direct-Access     UFD 3.0  Silicon-Power8G  PMAP PQ: 0 ANSI: 6
[ 3517.792879] sd 23:0:0:0: Attached scsi generic sg4 type 0
[ 3517.793592] sd 23:0:0:0: [sdd] 15126528 512-byte logical blocks: (7.74 GB/7.21 GiB)
[ 3517.794340] sd 23:0:0:0: [sdd] Write Protect is off
[ 3517.794346] sd 23:0:0:0: [sdd] Mode Sense: 45 00 00 00
[ 3517.796083] sd 23:0:0:0: [sdd] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[ 3517.801135]  sdd:
[ 3517.803788] sd 23:0:0:0: [sdd] Attached SCSI removable disk
[ 3518.096593] FAT-fs (sdd): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.

I hope this will help you to find a solution.

If you want to read more about the topic: https://lwn.net/Articles/143397/