How do you reset a USB device from the command line?Startup script to unmount and re-mount a USB deviceReset CDMA USB Modem without unplug/replugHow do I prevent Notifications and Icon Popups when Phone Connected to USB?Dual booting: every time Windows boots, it increases the scrolling speed of the mouse on UbuntuHow to unmount MTP USB device from command lineUSB ports will not recognize flash driveCanon LIDE 110: should reconnect USB for each scanHow can I reset usb port after over current draw?fprint timing out on login 16.04 LTSHow to find out which USB is which on a BBBConnect to bluetooth device from command lineReset a USB Modem without physical access (KVM virtual machine)Cannot mount/see/access USB-device - ttyUSB0USB-Device detectionRunning Ubuntu Touch applications in command line for gdb debugging?Video capture with eMPIA 2861 chip not workingHow to unmount MTP USB device from command linegphoto2: Could not claim the USB deviceusbtmc0 device: how to change default behaviourRescuing flash drive, device disappears on read error, how to create byte copy
Do I need to be arrogant to get ahead?
Why did it take so long to abandon sail after steamships were demonstrated?
Are all passive ability checks floors for active ability checks?
How to make healing in an exploration game interesting
Declaring defaulted assignment operator as constexpr: which compiler is right?
Are there verbs that are neither telic, or atelic?
Use void Apex method in Lightning Web Component
A link redirect to http instead of https: how critical is it?
How to explain that I do not want to visit a country due to personal safety concern?
Charles Hockett - 'F' article?
Did Ender ever learn that he killed Stilson and/or Bonzo?
What is this large pipe coming out of my roof?
Employee lack of ownership
SOQL: Populate a Literal List in WHERE IN Clause
Stiffness of a cantilever beam
Have researchers managed to "reverse time"? If so, what does that mean for physics?
Python if-else code style for reduced code
Is it normal that my co-workers at a fitness company criticize my food choices?
What are substitutions for coconut in curry?
Why do Australian milk farmers need to protest supermarkets' milk price?
Is a party consisting of only a bard, a cleric, and a warlock functional long-term?
Do the common programs (for example: "ls", "cat") in Linux and BSD come from the same source code?
Time travel from stationary position?
What did Alexander Pope mean by "Expletives their feeble Aid do join"?
How do you reset a USB device from the command line?
Startup script to unmount and re-mount a USB deviceReset CDMA USB Modem without unplug/replugHow do I prevent Notifications and Icon Popups when Phone Connected to USB?Dual booting: every time Windows boots, it increases the scrolling speed of the mouse on UbuntuHow to unmount MTP USB device from command lineUSB ports will not recognize flash driveCanon LIDE 110: should reconnect USB for each scanHow can I reset usb port after over current draw?fprint timing out on login 16.04 LTSHow to find out which USB is which on a BBBConnect to bluetooth device from command lineReset a USB Modem without physical access (KVM virtual machine)Cannot mount/see/access USB-device - ttyUSB0USB-Device detectionRunning Ubuntu Touch applications in command line for gdb debugging?Video capture with eMPIA 2861 chip not workingHow to unmount MTP USB device from command linegphoto2: Could not claim the USB deviceusbtmc0 device: how to change default behaviourRescuing flash drive, device disappears on read error, how to create byte copy
Is it possible to reset the connection of a USB device, without physically disconnecting/connecting from the PC?
Specifically, my device is a digital camera. I'm using gphoto2, but lately I get "device read errors", so I'd like to try to do a software-reset of the connection.
From what I can tell, there are no kernel modules being loaded for the camera. The only one that looks related is usbhid.
command-line usb
add a comment |
Is it possible to reset the connection of a USB device, without physically disconnecting/connecting from the PC?
Specifically, my device is a digital camera. I'm using gphoto2, but lately I get "device read errors", so I'd like to try to do a software-reset of the connection.
From what I can tell, there are no kernel modules being loaded for the camera. The only one that looks related is usbhid.
command-line usb
Which version of Ubuntu are you using?
– User
Aug 1 '10 at 20:15
i tried both solutions by Li Lo and ssokolow, all i get is permission denied, nomatter if i use the usbreset code or the command line "echo 0 > ..." i use sudo, also my usb devices are owned by root but i can use them without admin rights(cameras..)
– user290672
Jun 8 '14 at 16:40
1
If you are getting read errors, you might have some data corruption. If your camera uses an external memory card (such as MicroSD), it might be wise to connect it to the computer and run fsck.
– TSJNachos117
Jun 8 '14 at 18:47
add a comment |
Is it possible to reset the connection of a USB device, without physically disconnecting/connecting from the PC?
Specifically, my device is a digital camera. I'm using gphoto2, but lately I get "device read errors", so I'd like to try to do a software-reset of the connection.
From what I can tell, there are no kernel modules being loaded for the camera. The only one that looks related is usbhid.
command-line usb
Is it possible to reset the connection of a USB device, without physically disconnecting/connecting from the PC?
Specifically, my device is a digital camera. I'm using gphoto2, but lately I get "device read errors", so I'd like to try to do a software-reset of the connection.
From what I can tell, there are no kernel modules being loaded for the camera. The only one that looks related is usbhid.
command-line usb
command-line usb
asked Aug 1 '10 at 19:46
cmcgintycmcginty
2,74652431
2,74652431
Which version of Ubuntu are you using?
– User
Aug 1 '10 at 20:15
i tried both solutions by Li Lo and ssokolow, all i get is permission denied, nomatter if i use the usbreset code or the command line "echo 0 > ..." i use sudo, also my usb devices are owned by root but i can use them without admin rights(cameras..)
– user290672
Jun 8 '14 at 16:40
1
If you are getting read errors, you might have some data corruption. If your camera uses an external memory card (such as MicroSD), it might be wise to connect it to the computer and run fsck.
– TSJNachos117
Jun 8 '14 at 18:47
add a comment |
Which version of Ubuntu are you using?
– User
Aug 1 '10 at 20:15
i tried both solutions by Li Lo and ssokolow, all i get is permission denied, nomatter if i use the usbreset code or the command line "echo 0 > ..." i use sudo, also my usb devices are owned by root but i can use them without admin rights(cameras..)
– user290672
Jun 8 '14 at 16:40
1
If you are getting read errors, you might have some data corruption. If your camera uses an external memory card (such as MicroSD), it might be wise to connect it to the computer and run fsck.
– TSJNachos117
Jun 8 '14 at 18:47
Which version of Ubuntu are you using?
– User
Aug 1 '10 at 20:15
Which version of Ubuntu are you using?
– User
Aug 1 '10 at 20:15
i tried both solutions by Li Lo and ssokolow, all i get is permission denied, nomatter if i use the usbreset code or the command line "echo 0 > ..." i use sudo, also my usb devices are owned by root but i can use them without admin rights(cameras..)
– user290672
Jun 8 '14 at 16:40
i tried both solutions by Li Lo and ssokolow, all i get is permission denied, nomatter if i use the usbreset code or the command line "echo 0 > ..." i use sudo, also my usb devices are owned by root but i can use them without admin rights(cameras..)
– user290672
Jun 8 '14 at 16:40
1
1
If you are getting read errors, you might have some data corruption. If your camera uses an external memory card (such as MicroSD), it might be wise to connect it to the computer and run fsck.
– TSJNachos117
Jun 8 '14 at 18:47
If you are getting read errors, you might have some data corruption. If your camera uses an external memory card (such as MicroSD), it might be wise to connect it to the computer and run fsck.
– TSJNachos117
Jun 8 '14 at 18:47
add a comment |
16 Answers
16
active
oldest
votes
Save the following as usbreset.c
/* usbreset -- send a USB port reset to a USB device */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
int main(int argc, char **argv)
const char *filename;
int fd;
int rc;
if (argc != 2)
fprintf(stderr, "Usage: usbreset device-filenamen");
return 1;
filename = argv[1];
fd = open(filename, O_WRONLY);
if (fd < 0)
perror("Error opening output file");
return 1;
printf("Resetting USB device %sn", filename);
rc = ioctl(fd, USBDEVFS_RESET, 0);
if (rc < 0)
perror("Error in ioctl");
return 1;
printf("Reset successfuln");
close(fd);
return 0;
The run the following commands in terminal:
Compile the program:
$ cc usbreset.c -o usbresetGet the Bus and Device ID of the USB device you want to reset:
$ lsusb
Bus 002 Device 003: ID 0fe9:9010 DVICOMake our compiled program executable:
$ chmod +x usbresetExecute the program with sudo privilege; make necessary substitution for
<Bus>and<Device>ids as found by running thelsusbcommand:$ sudo ./usbreset /dev/bus/usb/002/003
Source of above program: http://marc.info/?l=linux-usb&m=121459435621262&w=2
I got errors like this: ./usbreset: command not found Dan 11.04 Natty
– user24895
Sep 13 '11 at 4:33
3
This works with ubuntu 13.10. The device ID can vary. TO get it for the mouse I have wrapped above code in few shell commandsecho $(lsusb | grep Mouse) mouse=$( lsusb | grep Mouse | perl -nE "/D+(d+)D+(d+).+/; print qq($1/$2)") sudo /path/to/c-program/usbreset /dev/bus/usb/$mouse
– knb
Dec 22 '13 at 11:04
1
my external drive seems to become undetectable (I have to hard reconnect the usb cable); it is a usb2.0 connected on a usb3.0 desktop PC port; when I runusbreset /dev/bus/usb/011/001that is one of the 2 usb 3.0 root hubs atlsusb, it errors: "Error in ioctl: Is a directory", any ideia? I tried on both usb 3.0 hubs
– Aquarius Power
Oct 30 '14 at 3:34
1
If anyone reading this have a (usb) mouse freeze after logging in on Ubuntu 16.04 (with dmesg filled by "input irq status -75") , i can confirm that this is the only solution that worked for me. Thank you
– Agustin Baez
May 2 '16 at 12:31
1
@ Aquarius, I also get the same error "Error in ioctl: Is a directory". Is it resolved ?
– ransh
Feb 12 '17 at 13:21
|
show 2 more comments
I haven't found myself in your specific circumstances before, so I'm not sure if it'll do enough, but the simplest way I've found to reset a USB device is this command: (No external apps necessary)
sudo sh -c "echo 0 > /sys/bus/usb/devices/1-4.6/authorized"
sudo sh -c "echo 1 > /sys/bus/usb/devices/1-4.6/authorized"
That's the actual one I use to reset my Kinect since libfreenect seems to have no API for putting it back to sleep. It's on my Gentoo box, but the kernel should be new enough to use the same path structure for sysfs.
Yours obviously wouldn't be 1-4.6 but you can either pull that device path from your kernel log (dmesg) or you can use something like lsusb to get the vendor and product IDs and then use a quick command like this to list how the paths relate to different vendor/product ID pairs:
for X in /sys/bus/usb/devices/*; do
echo "$X"
cat "$X/idVendor" 2>/dev/null
cat "$X/idProduct" 2>/dev/null
echo
done
sh: 1: cannot create /sys/bus/usb/devices/1-3.1:1.0/authorized: Directory nonexistent
– Nicolas Marchildon
May 31 '12 at 3:22
It looks like they've changed the layout of the usbfs filesystem. I'll try to figure out what the new way of doing things is on Ubuntu once I'm not so sleepy.
– ssokolow
Jun 2 '12 at 15:46
9
Thank you worked great! Maybe you should also mention to perform aecho 1 > /sys/bus/usb/devices/whatever/authorizedinside a script to re-enable the device as soon as it has been disabled. I did it on both my mouse and usb keyboard and I ended up with a completely deaf system :)
– Avio
Apr 28 '13 at 8:43
1
It's extremely strange if it automatically re-set the value to 1 as setting it to 0 is telling the system you don't want the device to be "authorized" and therefore inaccessible.
– Tim Tisdall
Oct 18 '13 at 19:45
2
A note for anyone who tries to switch to the| sudo tee ...approach to privileged/syswrites: That breaks badly if you don't already have your sudo credentials cached.sudo sh -c "..."works as expected when sudo needs to prompt for a password.
– ssokolow
Jun 5 '16 at 10:40
|
show 5 more comments
This will reset all of USB1/2/3 attached ports[1]:
for i in /sys/bus/pci/drivers/[uoex]hci_hcd/*:*; do
[ -e "$i" ] || continue
echo "$i##*/" > "$i%/*/unbind"
echo "$i##*/" > "$i%/*/bind"
done
I believe this will solve your problem. If you do not want to reset all of the USB endpoints, you can use appropriate device ID from /sys/bus/pci/drivers/ehci_hcd
Notes:
[1]: the *hci_hcd kernel drivers typically control the USB ports. ohci_hcd and uhci_hcd are for USB1.1 ports, ehci_hcd is for USB2 ports and xhci_hcd is for USB3 ports. (see https://en.wikipedia.org/wiki/Host_controller_interface_(USB,_Firewire))
do you believe it may work to wakeup an usb storage?
– Aquarius Power
Jun 30 '14 at 5:53
2
Although I've received the following message:ls: cannot access /sys/bus/pci/drivers/ehci_hcd/: No such file or directorythis has resolved the issue, the mouse has started working immediately. +1
– Attila Fulop
Oct 10 '14 at 6:16
2
@Otheus OHCI and UHCI are the USB 1.1 host standards, EHCI is the USB 2.0 host standard, and XHCI is the USB 3.0 host standard.
– ssokolow
Jul 20 '16 at 19:02
2
This is a beautiful solution. However, on some later Kernels and other *nix distributions, you will find that you need to substitute*hci_hcdwith*hci-pci, as the hci_hcd driver is already compiled into the Kernel.
– not2qubit
Mar 1 '17 at 17:14
1
On a Banana Pi, there apparently is no PCI bus, I had to use the following:for i in /sys/bus/usb/drivers/*/*:*; do
– Martin Hansen
Jun 29 '17 at 9:14
|
show 5 more comments
I needed to automate this in a python script, so I adapted LiLo's extremely helpful answer to the following:
#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE
import fcntl
driver = sys.argv[-1]
print "resetting driver:", driver
USBDEVFS_RESET= 21780
try:
lsusb_out = Popen("lsusb | grep -i %s"%driver, shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().split()
bus = lsusb_out[1]
device = lsusb_out[3][:-1]
f = open("/dev/bus/usb/%s/%s"%(bus, device), 'w', os.O_WRONLY)
fcntl.ioctl(f, USBDEVFS_RESET, 0)
except Exception, msg:
print "failed to reset device:", msg
In my case it was the cp210x driver (which I could tell from lsmod | grep usbserial), so you could save the above snippet as reset_usb.py and then do this:
sudo python reset_usb.py cp210x
This might also be helpful if you don't already have a c compiler setup on your system, but you do have python.
worked for me on a Raspberry
– webo80
Jun 23 '16 at 14:36
1
A few more words on your solution please. For example, something about the constantUSBDEVFS_RESET. Is it always the same for all systems?
– not2qubit
Feb 28 '17 at 7:25
@not2qubitUSBDEVFS_RESETis the same for all systems. For MIPS it is 536892692.
– yegorich
Apr 21 '17 at 9:09
Newer versions of lsusb seem to need the-targument (tree mode) to show the driver info that this script is expecting, but the script then needs some updates to parse the different output lines this generates
– Cheetah
Oct 19 '17 at 18:56
See my answer here askubuntu.com/a/988297/558070 for a much improved version of this script.
– mcarans
Dec 21 '17 at 10:30
add a comment |
I'm using kind of sledgehammer by reloading the modules.
This is my usb_reset.sh script:
#!/bin/bash
# USB drivers
rmmod xhci_pci
rmmod ehci_pci
# uncomment if you have firewire
#rmmod ohci_pci
modprobe xhci_pci
modprobe ehci_pci
# uncomment if you have firewire
#modprobe ohci_pci
And this is my systemd service file /usr/lib/systemd/system/usbreset.service which runs usb_reset.sh after my diplay manager has started:
[Unit]
Description=usbreset Service
After=gdm.service
Wants=gdm.service
[Service]
Type=oneshot
ExecStart=/path/to/usb_reset.sh
Using the listpci option of my script here: askubuntu.com/a/988297/558070 will help identify which USB module to reload (eg. xhci_pci, ehci_pci).
– mcarans
Jan 24 '18 at 15:14
1
Unfortunately on my system these kernel modules are not separate form the kernel, so this won't work:rmmod: ERROR: Module xhci_pci is builtin.
– unfa
Jun 28 '18 at 13:00
add a comment |
As the special case of the question is a communication problem of gphoto2 with a camera on USB, there is an option in gphoto2 to reset its USB connection:
gphoto2 --reset
Maybe this option didn't exist in 2010 when the question was asked.
add a comment |
Quickest way to reset will be to reset the USB controller itself. Doing so will enforce udev to unregister the device on disconnection, and register is back once you enable it.
echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci_hcd/unbind
echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci_hcd/unbind
echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci_hcd/bind
echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci_hcd/bind
This should work for most PC environment. However, if you are using some custom hardware you can simply iterate through the device names. With this method you don't need to find out the device name by lsusb. You can incorporate in a automated script as well.
1
You need to run these commands as root/sudo, and it will not work on all systems (on some, you'll need to replaceehci_hcdwithehci-pci. More info on this solution (perhaps where it came from?): davidjb.com/blog/2012/06/…
– Lambart
Nov 5 '15 at 17:43
add a comment |
I made a python script which will reset a particular USB device based on the device number. You can find out the device number from command lsusb.
for example:
$ lsusb
Bus 002 Device 004: ID 046d:c312 Logitech, Inc. DeLuxe 250 Keyboard
In this string 004 is the device number
import os
import argparse
import subprocess
path='/sys/bus/usb/devices/'
def runbash(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
out = p.stdout.read().strip()
return out
def reset_device(dev_num):
sub_dirs = []
for root, dirs, files in os.walk(path):
for name in dirs:
sub_dirs.append(os.path.join(root, name))
dev_found = 0
for sub_dir in sub_dirs:
if True == os.path.isfile(sub_dir+'/devnum'):
fd = open(sub_dir+'/devnum','r')
line = fd.readline()
if int(dev_num) == int(line):
print ('Your device is at: '+sub_dir)
dev_found = 1
break
fd.close()
if dev_found == 1:
reset_file = sub_dir+'/authorized'
runbash('echo 0 > '+reset_file)
runbash('echo 1 > '+reset_file)
print ('Device reset successful')
else:
print ("No such device")
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--devnum', dest='devnum')
args = parser.parse_args()
if args.devnum is None:
print('Usage:usb_reset.py -d <device_number> nThe device number can be obtained from lsusb command result')
return
reset_device(args.devnum)
if __name__=='__main__':
main()
add a comment |
Here is script that will only reset a matching product/vendor ID.
#!/bin/bash
set -euo pipefail
IFS=$'nt'
VENDOR="045e"
PRODUCT="0719"
for DIR in $(find /sys/bus/usb/devices/ -maxdepth 1 -type l); do
if [[ -f $DIR/idVendor && -f $DIR/idProduct &&
$(cat $DIR/idVendor) == $VENDOR && $(cat $DIR/idProduct) == $PRODUCT ]]; then
echo 0 > $DIR/authorized
sleep 0.5
echo 1 > $DIR/authorized
fi
done
1
I found your script is useful. But what should I do if the$DIRdisappears and device is not visible?
– Eugen Konkov
Oct 13 '17 at 7:36
add a comment |
I've created a Python script that simplifies the whole process based on answers here.
Save the script below as reset_usb.py or clone this repo.
Usage:
python reset_usb.py help # Show this help
sudo python reset_usb.py list # List all USB devices
sudo python reset_usb.py path /dev/bus/usb/XXX/YYY # Reset USB device using path /dev/bus/usb/XXX/YYY
sudo python reset_usb.py search "search terms" # Search for USB device using the search terms within the search string returned by list and reset matching device
sudo python reset_usb.py listpci # List all PCI USB devices
sudo python reset_usb.py pathpci /sys/bus/pci/drivers/.../XXXX:XX:XX.X # Reset PCI USB device using path /sys/bus/pci/drivers/.../XXXX:XX:XX.X
sudo python reset_usb.py searchpci "search terms" # Search for PCI USB device using the search terms within the search string returned by listpci and reset matching device
Script:
#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE
import fcntl
instructions = '''
Usage: python reset_usb.py help : Show this help
sudo python reset_usb.py list : List all USB devices
sudo python reset_usb.py path /dev/bus/usb/XXX/YYY : Reset USB device using path /dev/bus/usb/XXX/YYY
sudo python reset_usb.py search "search terms" : Search for USB device using the search terms within the search string returned by list and reset matching device
sudo python reset_usb.py listpci : List all PCI USB devices
sudo python reset_usb.py pathpci /sys/bus/pci/drivers/.../XXXX:XX:XX.X : Reset PCI USB device using path
sudo python reset_usb.py searchpci "search terms" : Search for PCI USB device using the search terms within the search string returned by listpci and reset matching device
'''
if len(sys.argv) < 2:
print(instructions)
sys.exit(0)
option = sys.argv[1].lower()
if 'help' in option:
print(instructions)
sys.exit(0)
def create_pci_list():
pci_usb_list = list()
try:
lspci_out = Popen('lspci -Dvmm', shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().decode('utf-8')
pci_devices = lspci_out.split('%s%s' % (os.linesep, os.linesep))
for pci_device in pci_devices:
device_dict = dict()
categories = pci_device.split(os.linesep)
for category in categories:
key, value = category.split('t')
device_dict[key[:-1]] = value.strip()
if 'USB' not in device_dict['Class']:
continue
for root, dirs, files in os.walk('/sys/bus/pci/drivers/'):
slot = device_dict['Slot']
if slot in dirs:
device_dict['path'] = os.path.join(root, slot)
break
pci_usb_list.append(device_dict)
except Exception as ex:
print('Failed to list pci devices! Error: %s' % ex)
sys.exit(-1)
return pci_usb_list
def create_usb_list():
device_list = list()
try:
lsusb_out = Popen('lsusb -v', shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().decode('utf-8')
usb_devices = lsusb_out.split('%s%s' % (os.linesep, os.linesep))
for device_categories in usb_devices:
if not device_categories:
continue
categories = device_categories.split(os.linesep)
device_stuff = categories[0].strip().split()
bus = device_stuff[1]
device = device_stuff[3][:-1]
device_dict = 'bus': bus, 'device': device
device_info = ' '.join(device_stuff[6:])
device_dict['description'] = device_info
for category in categories:
if not category:
continue
categoryinfo = category.strip().split()
if categoryinfo[0] == 'iManufacturer':
manufacturer_info = ' '.join(categoryinfo[2:])
device_dict['manufacturer'] = manufacturer_info
if categoryinfo[0] == 'iProduct':
device_info = ' '.join(categoryinfo[2:])
device_dict['device'] = device_info
path = '/dev/bus/usb/%s/%s' % (bus, device)
device_dict['path'] = path
device_list.append(device_dict)
except Exception as ex:
print('Failed to list usb devices! Error: %s' % ex)
sys.exit(-1)
return device_list
if 'listpci' in option:
pci_usb_list = create_pci_list()
for device in pci_usb_list:
print('path=%s' % device['path'])
print(' manufacturer=%s' % device['SVendor'])
print(' device=%s' % device['SDevice'])
print(' search string=%s %s' % (device['SVendor'], device['SDevice']))
sys.exit(0)
if 'list' in option:
usb_list = create_usb_list()
for device in usb_list:
print('path=%s' % device['path'])
print(' description=%s' % device['description'])
print(' manufacturer=%s' % device['manufacturer'])
print(' device=%s' % device['device'])
print(' search string=%s %s %s' % (device['description'], device['manufacturer'], device['device']))
sys.exit(0)
if len(sys.argv) < 3:
print(instructions)
sys.exit(0)
option2 = sys.argv[2]
print('Resetting device: %s' % option2)
# echo -n "0000:39:00.0" | tee /sys/bus/pci/drivers/xhci_hcd/unbind;echo -n "0000:39:00.0" | tee /sys/bus/pci/drivers/xhci_hcd/bind
def reset_pci_usb_device(dev_path):
folder, slot = os.path.split(dev_path)
try:
fp = open(os.path.join(folder, 'unbind'), 'wt')
fp.write(slot)
fp.close()
fp = open(os.path.join(folder, 'bind'), 'wt')
fp.write(slot)
fp.close()
print('Successfully reset %s' % dev_path)
sys.exit(0)
except Exception as ex:
print('Failed to reset device! Error: %s' % ex)
sys.exit(-1)
if 'pathpci' in option:
reset_pci_usb_device(option2)
if 'searchpci' in option:
pci_usb_list = create_pci_list()
for device in pci_usb_list:
text = '%s %s' % (device['SVendor'], device['SDevice'])
if option2 in text:
reset_pci_usb_device(device['path'])
print('Failed to find device!')
sys.exit(-1)
def reset_usb_device(dev_path):
USBDEVFS_RESET = 21780
try:
f = open(dev_path, 'w', os.O_WRONLY)
fcntl.ioctl(f, USBDEVFS_RESET, 0)
print('Successfully reset %s' % dev_path)
sys.exit(0)
except Exception as ex:
print('Failed to reset device! Error: %s' % ex)
sys.exit(-1)
if 'path' in option:
reset_usb_device(option2)
if 'search' in option:
usb_list = create_usb_list()
for device in usb_list:
text = '%s %s %s' % (device['description'], device['manufacturer'], device['device'])
if option2 in text:
reset_usb_device(device['path'])
print('Failed to find device!')
sys.exit(-1)
add a comment |
Did somebody order a sledgehammer? This is pieced together from various other answers here.
#!/bin/bash
# Root required
if (( UID )); then
exec sudo "$0" "$@"
fi
cd /sys/bus/pci/drivers
function reinit (
local d="$1"
test -e "$d"
for d in ?hci_???; do
echo " - $d"
reinit "$d"
done
Mark, have you found that the unbinding is really necessary or is it here just to be on the safe side?
– ndemou
Nov 10 '16 at 14:27
This is a sledgehammer, it probably does a lot of unnecessary things
– Mark K Cowan
Nov 10 '16 at 14:37
@MarkKCowan , How do you use it? What are the command arguments needed/expected?
– not2qubit
Feb 28 '17 at 7:15
1
@not2qubit: No command-line arguments required. The$@in the sudo proxy is just a force of habbit, having it prevents bugs if I later decide to add arguments (and forget to update the sudo proxy).
– Mark K Cowan
Nov 21 '17 at 14:54
1
@MarkKCowan Doh! Sorry mate! Oh yes of curse! I should not be commenting on this site while sleepy. Upvoted!
– not2qubit
Nov 21 '17 at 16:29
|
show 2 more comments
Sometimes I want to perform this operation on a particular device, as identified by VID (vendor id) and PID (product id). This is a script I've found useful for this purpose, that uses the nifty libusb library.
First run:
sudo apt-get install libusb-dev
Then, this c++ file's resetDeviceConnection should perform this task, of resetting a device connection as identified by vid and pid.
#include <libusb-1.0/libusb.h>
int resetDeviceConnection(UINT_16 vid, UINT_16 pid)
/*Open libusb*/
int resetStatus = 0;
libusb_context * context;
libusb_init(&context);
libusb_device_handle * dev_handle = libusb_open_device_with_vid_pid(context,vid,pid);
if (dev_handle == NULL)
printf("usb resetting unsuccessful! No matching device found, or error encountered!n");
resetStatus = 1;
else
/*reset the device, if one was found*/
resetStatus = libusb_reset_device(dev_handle);
/*exit libusb*/
libusb_exit(context);
return resetStatus;
(stolen from my personal TIL catalog:
https://github.com/Marviel/TIL/blob/master/unix_tools/Reset_specific_USB_Device.md)
3
Please can you show how this script is run.
– George Udosen
Dec 29 '16 at 14:28
Sure thing, let me update.
– Marviel
Dec 29 '16 at 20:31
1
@Marviel, we're still waiting for an update...
– not2qubit
Feb 28 '17 at 7:18
needs downvote as useless
– Eugen Konkov
Oct 13 '17 at 7:36
add a comment |
Perhaps this works for a camera, too:
Following revived a starved USB 3.0 HDD on a 3.4.42 (kernel.org) Linux on my side. dmesg told, that it was timing out commands after 360s (sorry, I cannot copy the syslog here, not connected networks) and the drive hung completely. Processes accessing the device were blocked in the kernel, unkillable. NFS hung, ZFS hung, dd hung.
After doing this, everything worked again. dmesg told just a single line about the USB device found.
I really have no idea what following does in detail. But it worked.
The following example output is from Debian Squeeze with 2.6.32-5-686 kernel, so I think it works for 2.6 and above:
$ ls -al /dev/sdb
brw-rw---T 1 root floppy 8, 16 Jun 3 20:24 /dev/sdb
$ ls -al /sys/dev/block/8:16/device/rescan
--w------- 1 root root 4096 Jun 6 01:46 /sys/dev/block/8:16/device/rescan
$ echo 1 > /sys/dev/block/8:16/device/rescan
If this does not work, perhaps somebody else can figure out how to send a real reset to a device.
add a comment |
Try this, it's a software unplug (Eject).
Sometimes doesn't work simply unbind device for some devices.
Example:
I want to remove or eject my "Genius NetScroll 120".
Then i first Check my attached usb device
$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 003: ID 03f0:231d Hewlett-Packard
Bus 001 Device 004: ID 138a:0007 Validity Sensors, Inc. VFS451 Fingerprint Reader
Bus 001 Device 005: ID 04f2:b163 Chicony Electronics Co., Ltd
Bus 002 Device 009: ID 0458:003a KYE Systems Corp. (Mouse Systems) NetScroll+ Mini Traveler / Genius NetScroll 120 **<----This my Mouse! XDDD**
Ok, i found my mouse, it's has a Bus 002, Device 009, idVendor 0458 and idProduct 003a, so this is a reference device info about the mouse.
This is important, the Bus number is the begin name path to device and i will check the product Id and Vendor to ensure the correct device to remove.
$ ls /sys/bus/usb/drivers/usb/
1-1/ 1-1.1/ 1-1.3/ 1-1.5/ 2-1/ 2-1.3/ bind uevent unbind usb1/ usb2/
Pay atention on the folders, check the begining with folder number 2, i will check this one because my Bus is 002, and one by one i have check each folder containing the correct idVendor and idProduct about my mouse info.
In this case, i will retrieve the info with this command:
cat /sys/bus/usb/drivers/usb/2-1.3/idVendor
0458
cat /sys/bus/usb/drivers/usb/2-1.3/idProduct
003a
Ok, the path /sys/bus/usb/drivers/usb/2-1.3/ match with my info mouse! XDDD.
It's time to remove the device!
su -c "echo 1 > /sys/bus/usb/drivers/usb/2-1.3/remove"
Plug again the usb device and it's work again!
9
What if you can't plug it in again? (for example it's an internal sdcard reader)
– aleb
Jun 29 '14 at 20:57
add a comment |
If you know your device name, this python script will work:
#!/usr/bin/python
"""
USB Reset
Call as "usbreset.py <device_file_path>"
With device_file_path like "/dev/bus/usb/bus_number/device_number"
"""
import fcntl, sys, os
USBDEVFS_RESET = ord('U') << (4*2) | 20
def main():
fd = os.open(sys.argv[1], os.O_WRONLY)
if fd < 0: sys.exit(1)
fcntl.ioctl(fd, USBDEVFS_RESET, 0)
os.close(fd)
sys.exit(0)
# end main
if __name__ == '__main__':
main()
add a comment |
i made a simple bash script for reset particular USB device.
#!/bin/bash
#type lsusb to find "vendor" and "product" ID in terminal
set -euo pipefail
IFS=$'nt'
#edit the below two lines of vendor and product values using lsusb result
dev=$(lsusb -t | grep usbdevicename | grep 'If 1' | cut -d' ' -f13|cut -d"," -f1)
#VENDOR=05a3
#PRODUCT=9230
VENDOR=$(lsusb -s $dev | cut -d' ' -f6 | cut -d: -f1)
PRODUCT=$(lsusb -s $dev | cut -d' ' -f6 | cut -d: -f2)
for DIR in $(find /sys/bus/usb/devices/ -maxdepth 1 -type l); do
if [[ -f $DIR/idVendor && -f $DIR/idProduct &&
$(cat $DIR/idVendor) == $VENDOR && $(cat $DIR/idProduct) == $PRODUCT ]]; then
echo 0 > $DIR/authorized
sleep 0.5
echo 1 > $DIR/authorized
fi
done
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f645%2fhow-do-you-reset-a-usb-device-from-the-command-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
16 Answers
16
active
oldest
votes
16 Answers
16
active
oldest
votes
active
oldest
votes
active
oldest
votes
Save the following as usbreset.c
/* usbreset -- send a USB port reset to a USB device */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
int main(int argc, char **argv)
const char *filename;
int fd;
int rc;
if (argc != 2)
fprintf(stderr, "Usage: usbreset device-filenamen");
return 1;
filename = argv[1];
fd = open(filename, O_WRONLY);
if (fd < 0)
perror("Error opening output file");
return 1;
printf("Resetting USB device %sn", filename);
rc = ioctl(fd, USBDEVFS_RESET, 0);
if (rc < 0)
perror("Error in ioctl");
return 1;
printf("Reset successfuln");
close(fd);
return 0;
The run the following commands in terminal:
Compile the program:
$ cc usbreset.c -o usbresetGet the Bus and Device ID of the USB device you want to reset:
$ lsusb
Bus 002 Device 003: ID 0fe9:9010 DVICOMake our compiled program executable:
$ chmod +x usbresetExecute the program with sudo privilege; make necessary substitution for
<Bus>and<Device>ids as found by running thelsusbcommand:$ sudo ./usbreset /dev/bus/usb/002/003
Source of above program: http://marc.info/?l=linux-usb&m=121459435621262&w=2
I got errors like this: ./usbreset: command not found Dan 11.04 Natty
– user24895
Sep 13 '11 at 4:33
3
This works with ubuntu 13.10. The device ID can vary. TO get it for the mouse I have wrapped above code in few shell commandsecho $(lsusb | grep Mouse) mouse=$( lsusb | grep Mouse | perl -nE "/D+(d+)D+(d+).+/; print qq($1/$2)") sudo /path/to/c-program/usbreset /dev/bus/usb/$mouse
– knb
Dec 22 '13 at 11:04
1
my external drive seems to become undetectable (I have to hard reconnect the usb cable); it is a usb2.0 connected on a usb3.0 desktop PC port; when I runusbreset /dev/bus/usb/011/001that is one of the 2 usb 3.0 root hubs atlsusb, it errors: "Error in ioctl: Is a directory", any ideia? I tried on both usb 3.0 hubs
– Aquarius Power
Oct 30 '14 at 3:34
1
If anyone reading this have a (usb) mouse freeze after logging in on Ubuntu 16.04 (with dmesg filled by "input irq status -75") , i can confirm that this is the only solution that worked for me. Thank you
– Agustin Baez
May 2 '16 at 12:31
1
@ Aquarius, I also get the same error "Error in ioctl: Is a directory". Is it resolved ?
– ransh
Feb 12 '17 at 13:21
|
show 2 more comments
Save the following as usbreset.c
/* usbreset -- send a USB port reset to a USB device */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
int main(int argc, char **argv)
const char *filename;
int fd;
int rc;
if (argc != 2)
fprintf(stderr, "Usage: usbreset device-filenamen");
return 1;
filename = argv[1];
fd = open(filename, O_WRONLY);
if (fd < 0)
perror("Error opening output file");
return 1;
printf("Resetting USB device %sn", filename);
rc = ioctl(fd, USBDEVFS_RESET, 0);
if (rc < 0)
perror("Error in ioctl");
return 1;
printf("Reset successfuln");
close(fd);
return 0;
The run the following commands in terminal:
Compile the program:
$ cc usbreset.c -o usbresetGet the Bus and Device ID of the USB device you want to reset:
$ lsusb
Bus 002 Device 003: ID 0fe9:9010 DVICOMake our compiled program executable:
$ chmod +x usbresetExecute the program with sudo privilege; make necessary substitution for
<Bus>and<Device>ids as found by running thelsusbcommand:$ sudo ./usbreset /dev/bus/usb/002/003
Source of above program: http://marc.info/?l=linux-usb&m=121459435621262&w=2
I got errors like this: ./usbreset: command not found Dan 11.04 Natty
– user24895
Sep 13 '11 at 4:33
3
This works with ubuntu 13.10. The device ID can vary. TO get it for the mouse I have wrapped above code in few shell commandsecho $(lsusb | grep Mouse) mouse=$( lsusb | grep Mouse | perl -nE "/D+(d+)D+(d+).+/; print qq($1/$2)") sudo /path/to/c-program/usbreset /dev/bus/usb/$mouse
– knb
Dec 22 '13 at 11:04
1
my external drive seems to become undetectable (I have to hard reconnect the usb cable); it is a usb2.0 connected on a usb3.0 desktop PC port; when I runusbreset /dev/bus/usb/011/001that is one of the 2 usb 3.0 root hubs atlsusb, it errors: "Error in ioctl: Is a directory", any ideia? I tried on both usb 3.0 hubs
– Aquarius Power
Oct 30 '14 at 3:34
1
If anyone reading this have a (usb) mouse freeze after logging in on Ubuntu 16.04 (with dmesg filled by "input irq status -75") , i can confirm that this is the only solution that worked for me. Thank you
– Agustin Baez
May 2 '16 at 12:31
1
@ Aquarius, I also get the same error "Error in ioctl: Is a directory". Is it resolved ?
– ransh
Feb 12 '17 at 13:21
|
show 2 more comments
Save the following as usbreset.c
/* usbreset -- send a USB port reset to a USB device */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
int main(int argc, char **argv)
const char *filename;
int fd;
int rc;
if (argc != 2)
fprintf(stderr, "Usage: usbreset device-filenamen");
return 1;
filename = argv[1];
fd = open(filename, O_WRONLY);
if (fd < 0)
perror("Error opening output file");
return 1;
printf("Resetting USB device %sn", filename);
rc = ioctl(fd, USBDEVFS_RESET, 0);
if (rc < 0)
perror("Error in ioctl");
return 1;
printf("Reset successfuln");
close(fd);
return 0;
The run the following commands in terminal:
Compile the program:
$ cc usbreset.c -o usbresetGet the Bus and Device ID of the USB device you want to reset:
$ lsusb
Bus 002 Device 003: ID 0fe9:9010 DVICOMake our compiled program executable:
$ chmod +x usbresetExecute the program with sudo privilege; make necessary substitution for
<Bus>and<Device>ids as found by running thelsusbcommand:$ sudo ./usbreset /dev/bus/usb/002/003
Source of above program: http://marc.info/?l=linux-usb&m=121459435621262&w=2
Save the following as usbreset.c
/* usbreset -- send a USB port reset to a USB device */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
int main(int argc, char **argv)
const char *filename;
int fd;
int rc;
if (argc != 2)
fprintf(stderr, "Usage: usbreset device-filenamen");
return 1;
filename = argv[1];
fd = open(filename, O_WRONLY);
if (fd < 0)
perror("Error opening output file");
return 1;
printf("Resetting USB device %sn", filename);
rc = ioctl(fd, USBDEVFS_RESET, 0);
if (rc < 0)
perror("Error in ioctl");
return 1;
printf("Reset successfuln");
close(fd);
return 0;
The run the following commands in terminal:
Compile the program:
$ cc usbreset.c -o usbresetGet the Bus and Device ID of the USB device you want to reset:
$ lsusb
Bus 002 Device 003: ID 0fe9:9010 DVICOMake our compiled program executable:
$ chmod +x usbresetExecute the program with sudo privilege; make necessary substitution for
<Bus>and<Device>ids as found by running thelsusbcommand:$ sudo ./usbreset /dev/bus/usb/002/003
Source of above program: http://marc.info/?l=linux-usb&m=121459435621262&w=2
edited Oct 20 '13 at 22:07
Aditya
9,388125589
9,388125589
answered Aug 2 '10 at 2:27
Li LoLi Lo
11.6k32837
11.6k32837
I got errors like this: ./usbreset: command not found Dan 11.04 Natty
– user24895
Sep 13 '11 at 4:33
3
This works with ubuntu 13.10. The device ID can vary. TO get it for the mouse I have wrapped above code in few shell commandsecho $(lsusb | grep Mouse) mouse=$( lsusb | grep Mouse | perl -nE "/D+(d+)D+(d+).+/; print qq($1/$2)") sudo /path/to/c-program/usbreset /dev/bus/usb/$mouse
– knb
Dec 22 '13 at 11:04
1
my external drive seems to become undetectable (I have to hard reconnect the usb cable); it is a usb2.0 connected on a usb3.0 desktop PC port; when I runusbreset /dev/bus/usb/011/001that is one of the 2 usb 3.0 root hubs atlsusb, it errors: "Error in ioctl: Is a directory", any ideia? I tried on both usb 3.0 hubs
– Aquarius Power
Oct 30 '14 at 3:34
1
If anyone reading this have a (usb) mouse freeze after logging in on Ubuntu 16.04 (with dmesg filled by "input irq status -75") , i can confirm that this is the only solution that worked for me. Thank you
– Agustin Baez
May 2 '16 at 12:31
1
@ Aquarius, I also get the same error "Error in ioctl: Is a directory". Is it resolved ?
– ransh
Feb 12 '17 at 13:21
|
show 2 more comments
I got errors like this: ./usbreset: command not found Dan 11.04 Natty
– user24895
Sep 13 '11 at 4:33
3
This works with ubuntu 13.10. The device ID can vary. TO get it for the mouse I have wrapped above code in few shell commandsecho $(lsusb | grep Mouse) mouse=$( lsusb | grep Mouse | perl -nE "/D+(d+)D+(d+).+/; print qq($1/$2)") sudo /path/to/c-program/usbreset /dev/bus/usb/$mouse
– knb
Dec 22 '13 at 11:04
1
my external drive seems to become undetectable (I have to hard reconnect the usb cable); it is a usb2.0 connected on a usb3.0 desktop PC port; when I runusbreset /dev/bus/usb/011/001that is one of the 2 usb 3.0 root hubs atlsusb, it errors: "Error in ioctl: Is a directory", any ideia? I tried on both usb 3.0 hubs
– Aquarius Power
Oct 30 '14 at 3:34
1
If anyone reading this have a (usb) mouse freeze after logging in on Ubuntu 16.04 (with dmesg filled by "input irq status -75") , i can confirm that this is the only solution that worked for me. Thank you
– Agustin Baez
May 2 '16 at 12:31
1
@ Aquarius, I also get the same error "Error in ioctl: Is a directory". Is it resolved ?
– ransh
Feb 12 '17 at 13:21
I got errors like this: ./usbreset: command not found Dan 11.04 Natty
– user24895
Sep 13 '11 at 4:33
I got errors like this: ./usbreset: command not found Dan 11.04 Natty
– user24895
Sep 13 '11 at 4:33
3
3
This works with ubuntu 13.10. The device ID can vary. TO get it for the mouse I have wrapped above code in few shell commands
echo $(lsusb | grep Mouse) mouse=$( lsusb | grep Mouse | perl -nE "/D+(d+)D+(d+).+/; print qq($1/$2)") sudo /path/to/c-program/usbreset /dev/bus/usb/$mouse– knb
Dec 22 '13 at 11:04
This works with ubuntu 13.10. The device ID can vary. TO get it for the mouse I have wrapped above code in few shell commands
echo $(lsusb | grep Mouse) mouse=$( lsusb | grep Mouse | perl -nE "/D+(d+)D+(d+).+/; print qq($1/$2)") sudo /path/to/c-program/usbreset /dev/bus/usb/$mouse– knb
Dec 22 '13 at 11:04
1
1
my external drive seems to become undetectable (I have to hard reconnect the usb cable); it is a usb2.0 connected on a usb3.0 desktop PC port; when I run
usbreset /dev/bus/usb/011/001 that is one of the 2 usb 3.0 root hubs at lsusb, it errors: "Error in ioctl: Is a directory", any ideia? I tried on both usb 3.0 hubs– Aquarius Power
Oct 30 '14 at 3:34
my external drive seems to become undetectable (I have to hard reconnect the usb cable); it is a usb2.0 connected on a usb3.0 desktop PC port; when I run
usbreset /dev/bus/usb/011/001 that is one of the 2 usb 3.0 root hubs at lsusb, it errors: "Error in ioctl: Is a directory", any ideia? I tried on both usb 3.0 hubs– Aquarius Power
Oct 30 '14 at 3:34
1
1
If anyone reading this have a (usb) mouse freeze after logging in on Ubuntu 16.04 (with dmesg filled by "input irq status -75") , i can confirm that this is the only solution that worked for me. Thank you
– Agustin Baez
May 2 '16 at 12:31
If anyone reading this have a (usb) mouse freeze after logging in on Ubuntu 16.04 (with dmesg filled by "input irq status -75") , i can confirm that this is the only solution that worked for me. Thank you
– Agustin Baez
May 2 '16 at 12:31
1
1
@ Aquarius, I also get the same error "Error in ioctl: Is a directory". Is it resolved ?
– ransh
Feb 12 '17 at 13:21
@ Aquarius, I also get the same error "Error in ioctl: Is a directory". Is it resolved ?
– ransh
Feb 12 '17 at 13:21
|
show 2 more comments
I haven't found myself in your specific circumstances before, so I'm not sure if it'll do enough, but the simplest way I've found to reset a USB device is this command: (No external apps necessary)
sudo sh -c "echo 0 > /sys/bus/usb/devices/1-4.6/authorized"
sudo sh -c "echo 1 > /sys/bus/usb/devices/1-4.6/authorized"
That's the actual one I use to reset my Kinect since libfreenect seems to have no API for putting it back to sleep. It's on my Gentoo box, but the kernel should be new enough to use the same path structure for sysfs.
Yours obviously wouldn't be 1-4.6 but you can either pull that device path from your kernel log (dmesg) or you can use something like lsusb to get the vendor and product IDs and then use a quick command like this to list how the paths relate to different vendor/product ID pairs:
for X in /sys/bus/usb/devices/*; do
echo "$X"
cat "$X/idVendor" 2>/dev/null
cat "$X/idProduct" 2>/dev/null
echo
done
sh: 1: cannot create /sys/bus/usb/devices/1-3.1:1.0/authorized: Directory nonexistent
– Nicolas Marchildon
May 31 '12 at 3:22
It looks like they've changed the layout of the usbfs filesystem. I'll try to figure out what the new way of doing things is on Ubuntu once I'm not so sleepy.
– ssokolow
Jun 2 '12 at 15:46
9
Thank you worked great! Maybe you should also mention to perform aecho 1 > /sys/bus/usb/devices/whatever/authorizedinside a script to re-enable the device as soon as it has been disabled. I did it on both my mouse and usb keyboard and I ended up with a completely deaf system :)
– Avio
Apr 28 '13 at 8:43
1
It's extremely strange if it automatically re-set the value to 1 as setting it to 0 is telling the system you don't want the device to be "authorized" and therefore inaccessible.
– Tim Tisdall
Oct 18 '13 at 19:45
2
A note for anyone who tries to switch to the| sudo tee ...approach to privileged/syswrites: That breaks badly if you don't already have your sudo credentials cached.sudo sh -c "..."works as expected when sudo needs to prompt for a password.
– ssokolow
Jun 5 '16 at 10:40
|
show 5 more comments
I haven't found myself in your specific circumstances before, so I'm not sure if it'll do enough, but the simplest way I've found to reset a USB device is this command: (No external apps necessary)
sudo sh -c "echo 0 > /sys/bus/usb/devices/1-4.6/authorized"
sudo sh -c "echo 1 > /sys/bus/usb/devices/1-4.6/authorized"
That's the actual one I use to reset my Kinect since libfreenect seems to have no API for putting it back to sleep. It's on my Gentoo box, but the kernel should be new enough to use the same path structure for sysfs.
Yours obviously wouldn't be 1-4.6 but you can either pull that device path from your kernel log (dmesg) or you can use something like lsusb to get the vendor and product IDs and then use a quick command like this to list how the paths relate to different vendor/product ID pairs:
for X in /sys/bus/usb/devices/*; do
echo "$X"
cat "$X/idVendor" 2>/dev/null
cat "$X/idProduct" 2>/dev/null
echo
done
sh: 1: cannot create /sys/bus/usb/devices/1-3.1:1.0/authorized: Directory nonexistent
– Nicolas Marchildon
May 31 '12 at 3:22
It looks like they've changed the layout of the usbfs filesystem. I'll try to figure out what the new way of doing things is on Ubuntu once I'm not so sleepy.
– ssokolow
Jun 2 '12 at 15:46
9
Thank you worked great! Maybe you should also mention to perform aecho 1 > /sys/bus/usb/devices/whatever/authorizedinside a script to re-enable the device as soon as it has been disabled. I did it on both my mouse and usb keyboard and I ended up with a completely deaf system :)
– Avio
Apr 28 '13 at 8:43
1
It's extremely strange if it automatically re-set the value to 1 as setting it to 0 is telling the system you don't want the device to be "authorized" and therefore inaccessible.
– Tim Tisdall
Oct 18 '13 at 19:45
2
A note for anyone who tries to switch to the| sudo tee ...approach to privileged/syswrites: That breaks badly if you don't already have your sudo credentials cached.sudo sh -c "..."works as expected when sudo needs to prompt for a password.
– ssokolow
Jun 5 '16 at 10:40
|
show 5 more comments
I haven't found myself in your specific circumstances before, so I'm not sure if it'll do enough, but the simplest way I've found to reset a USB device is this command: (No external apps necessary)
sudo sh -c "echo 0 > /sys/bus/usb/devices/1-4.6/authorized"
sudo sh -c "echo 1 > /sys/bus/usb/devices/1-4.6/authorized"
That's the actual one I use to reset my Kinect since libfreenect seems to have no API for putting it back to sleep. It's on my Gentoo box, but the kernel should be new enough to use the same path structure for sysfs.
Yours obviously wouldn't be 1-4.6 but you can either pull that device path from your kernel log (dmesg) or you can use something like lsusb to get the vendor and product IDs and then use a quick command like this to list how the paths relate to different vendor/product ID pairs:
for X in /sys/bus/usb/devices/*; do
echo "$X"
cat "$X/idVendor" 2>/dev/null
cat "$X/idProduct" 2>/dev/null
echo
done
I haven't found myself in your specific circumstances before, so I'm not sure if it'll do enough, but the simplest way I've found to reset a USB device is this command: (No external apps necessary)
sudo sh -c "echo 0 > /sys/bus/usb/devices/1-4.6/authorized"
sudo sh -c "echo 1 > /sys/bus/usb/devices/1-4.6/authorized"
That's the actual one I use to reset my Kinect since libfreenect seems to have no API for putting it back to sleep. It's on my Gentoo box, but the kernel should be new enough to use the same path structure for sysfs.
Yours obviously wouldn't be 1-4.6 but you can either pull that device path from your kernel log (dmesg) or you can use something like lsusb to get the vendor and product IDs and then use a quick command like this to list how the paths relate to different vendor/product ID pairs:
for X in /sys/bus/usb/devices/*; do
echo "$X"
cat "$X/idVendor" 2>/dev/null
cat "$X/idProduct" 2>/dev/null
echo
done
edited Jun 4 '16 at 12:50
muru
1
1
answered Sep 13 '11 at 6:56
ssokolowssokolow
830615
830615
sh: 1: cannot create /sys/bus/usb/devices/1-3.1:1.0/authorized: Directory nonexistent
– Nicolas Marchildon
May 31 '12 at 3:22
It looks like they've changed the layout of the usbfs filesystem. I'll try to figure out what the new way of doing things is on Ubuntu once I'm not so sleepy.
– ssokolow
Jun 2 '12 at 15:46
9
Thank you worked great! Maybe you should also mention to perform aecho 1 > /sys/bus/usb/devices/whatever/authorizedinside a script to re-enable the device as soon as it has been disabled. I did it on both my mouse and usb keyboard and I ended up with a completely deaf system :)
– Avio
Apr 28 '13 at 8:43
1
It's extremely strange if it automatically re-set the value to 1 as setting it to 0 is telling the system you don't want the device to be "authorized" and therefore inaccessible.
– Tim Tisdall
Oct 18 '13 at 19:45
2
A note for anyone who tries to switch to the| sudo tee ...approach to privileged/syswrites: That breaks badly if you don't already have your sudo credentials cached.sudo sh -c "..."works as expected when sudo needs to prompt for a password.
– ssokolow
Jun 5 '16 at 10:40
|
show 5 more comments
sh: 1: cannot create /sys/bus/usb/devices/1-3.1:1.0/authorized: Directory nonexistent
– Nicolas Marchildon
May 31 '12 at 3:22
It looks like they've changed the layout of the usbfs filesystem. I'll try to figure out what the new way of doing things is on Ubuntu once I'm not so sleepy.
– ssokolow
Jun 2 '12 at 15:46
9
Thank you worked great! Maybe you should also mention to perform aecho 1 > /sys/bus/usb/devices/whatever/authorizedinside a script to re-enable the device as soon as it has been disabled. I did it on both my mouse and usb keyboard and I ended up with a completely deaf system :)
– Avio
Apr 28 '13 at 8:43
1
It's extremely strange if it automatically re-set the value to 1 as setting it to 0 is telling the system you don't want the device to be "authorized" and therefore inaccessible.
– Tim Tisdall
Oct 18 '13 at 19:45
2
A note for anyone who tries to switch to the| sudo tee ...approach to privileged/syswrites: That breaks badly if you don't already have your sudo credentials cached.sudo sh -c "..."works as expected when sudo needs to prompt for a password.
– ssokolow
Jun 5 '16 at 10:40
sh: 1: cannot create /sys/bus/usb/devices/1-3.1:1.0/authorized: Directory nonexistent
– Nicolas Marchildon
May 31 '12 at 3:22
sh: 1: cannot create /sys/bus/usb/devices/1-3.1:1.0/authorized: Directory nonexistent
– Nicolas Marchildon
May 31 '12 at 3:22
It looks like they've changed the layout of the usbfs filesystem. I'll try to figure out what the new way of doing things is on Ubuntu once I'm not so sleepy.
– ssokolow
Jun 2 '12 at 15:46
It looks like they've changed the layout of the usbfs filesystem. I'll try to figure out what the new way of doing things is on Ubuntu once I'm not so sleepy.
– ssokolow
Jun 2 '12 at 15:46
9
9
Thank you worked great! Maybe you should also mention to perform a
echo 1 > /sys/bus/usb/devices/whatever/authorized inside a script to re-enable the device as soon as it has been disabled. I did it on both my mouse and usb keyboard and I ended up with a completely deaf system :)– Avio
Apr 28 '13 at 8:43
Thank you worked great! Maybe you should also mention to perform a
echo 1 > /sys/bus/usb/devices/whatever/authorized inside a script to re-enable the device as soon as it has been disabled. I did it on both my mouse and usb keyboard and I ended up with a completely deaf system :)– Avio
Apr 28 '13 at 8:43
1
1
It's extremely strange if it automatically re-set the value to 1 as setting it to 0 is telling the system you don't want the device to be "authorized" and therefore inaccessible.
– Tim Tisdall
Oct 18 '13 at 19:45
It's extremely strange if it automatically re-set the value to 1 as setting it to 0 is telling the system you don't want the device to be "authorized" and therefore inaccessible.
– Tim Tisdall
Oct 18 '13 at 19:45
2
2
A note for anyone who tries to switch to the
| sudo tee ... approach to privileged /sys writes: That breaks badly if you don't already have your sudo credentials cached. sudo sh -c "..." works as expected when sudo needs to prompt for a password.– ssokolow
Jun 5 '16 at 10:40
A note for anyone who tries to switch to the
| sudo tee ... approach to privileged /sys writes: That breaks badly if you don't already have your sudo credentials cached. sudo sh -c "..." works as expected when sudo needs to prompt for a password.– ssokolow
Jun 5 '16 at 10:40
|
show 5 more comments
This will reset all of USB1/2/3 attached ports[1]:
for i in /sys/bus/pci/drivers/[uoex]hci_hcd/*:*; do
[ -e "$i" ] || continue
echo "$i##*/" > "$i%/*/unbind"
echo "$i##*/" > "$i%/*/bind"
done
I believe this will solve your problem. If you do not want to reset all of the USB endpoints, you can use appropriate device ID from /sys/bus/pci/drivers/ehci_hcd
Notes:
[1]: the *hci_hcd kernel drivers typically control the USB ports. ohci_hcd and uhci_hcd are for USB1.1 ports, ehci_hcd is for USB2 ports and xhci_hcd is for USB3 ports. (see https://en.wikipedia.org/wiki/Host_controller_interface_(USB,_Firewire))
do you believe it may work to wakeup an usb storage?
– Aquarius Power
Jun 30 '14 at 5:53
2
Although I've received the following message:ls: cannot access /sys/bus/pci/drivers/ehci_hcd/: No such file or directorythis has resolved the issue, the mouse has started working immediately. +1
– Attila Fulop
Oct 10 '14 at 6:16
2
@Otheus OHCI and UHCI are the USB 1.1 host standards, EHCI is the USB 2.0 host standard, and XHCI is the USB 3.0 host standard.
– ssokolow
Jul 20 '16 at 19:02
2
This is a beautiful solution. However, on some later Kernels and other *nix distributions, you will find that you need to substitute*hci_hcdwith*hci-pci, as the hci_hcd driver is already compiled into the Kernel.
– not2qubit
Mar 1 '17 at 17:14
1
On a Banana Pi, there apparently is no PCI bus, I had to use the following:for i in /sys/bus/usb/drivers/*/*:*; do
– Martin Hansen
Jun 29 '17 at 9:14
|
show 5 more comments
This will reset all of USB1/2/3 attached ports[1]:
for i in /sys/bus/pci/drivers/[uoex]hci_hcd/*:*; do
[ -e "$i" ] || continue
echo "$i##*/" > "$i%/*/unbind"
echo "$i##*/" > "$i%/*/bind"
done
I believe this will solve your problem. If you do not want to reset all of the USB endpoints, you can use appropriate device ID from /sys/bus/pci/drivers/ehci_hcd
Notes:
[1]: the *hci_hcd kernel drivers typically control the USB ports. ohci_hcd and uhci_hcd are for USB1.1 ports, ehci_hcd is for USB2 ports and xhci_hcd is for USB3 ports. (see https://en.wikipedia.org/wiki/Host_controller_interface_(USB,_Firewire))
do you believe it may work to wakeup an usb storage?
– Aquarius Power
Jun 30 '14 at 5:53
2
Although I've received the following message:ls: cannot access /sys/bus/pci/drivers/ehci_hcd/: No such file or directorythis has resolved the issue, the mouse has started working immediately. +1
– Attila Fulop
Oct 10 '14 at 6:16
2
@Otheus OHCI and UHCI are the USB 1.1 host standards, EHCI is the USB 2.0 host standard, and XHCI is the USB 3.0 host standard.
– ssokolow
Jul 20 '16 at 19:02
2
This is a beautiful solution. However, on some later Kernels and other *nix distributions, you will find that you need to substitute*hci_hcdwith*hci-pci, as the hci_hcd driver is already compiled into the Kernel.
– not2qubit
Mar 1 '17 at 17:14
1
On a Banana Pi, there apparently is no PCI bus, I had to use the following:for i in /sys/bus/usb/drivers/*/*:*; do
– Martin Hansen
Jun 29 '17 at 9:14
|
show 5 more comments
This will reset all of USB1/2/3 attached ports[1]:
for i in /sys/bus/pci/drivers/[uoex]hci_hcd/*:*; do
[ -e "$i" ] || continue
echo "$i##*/" > "$i%/*/unbind"
echo "$i##*/" > "$i%/*/bind"
done
I believe this will solve your problem. If you do not want to reset all of the USB endpoints, you can use appropriate device ID from /sys/bus/pci/drivers/ehci_hcd
Notes:
[1]: the *hci_hcd kernel drivers typically control the USB ports. ohci_hcd and uhci_hcd are for USB1.1 ports, ehci_hcd is for USB2 ports and xhci_hcd is for USB3 ports. (see https://en.wikipedia.org/wiki/Host_controller_interface_(USB,_Firewire))
This will reset all of USB1/2/3 attached ports[1]:
for i in /sys/bus/pci/drivers/[uoex]hci_hcd/*:*; do
[ -e "$i" ] || continue
echo "$i##*/" > "$i%/*/unbind"
echo "$i##*/" > "$i%/*/bind"
done
I believe this will solve your problem. If you do not want to reset all of the USB endpoints, you can use appropriate device ID from /sys/bus/pci/drivers/ehci_hcd
Notes:
[1]: the *hci_hcd kernel drivers typically control the USB ports. ohci_hcd and uhci_hcd are for USB1.1 ports, ehci_hcd is for USB2 ports and xhci_hcd is for USB3 ports. (see https://en.wikipedia.org/wiki/Host_controller_interface_(USB,_Firewire))
edited Jul 31 '18 at 18:25
fresskoma
1034
1034
answered May 4 '13 at 11:02
Tamás TapsonyiTamás Tapsonyi
44142
44142
do you believe it may work to wakeup an usb storage?
– Aquarius Power
Jun 30 '14 at 5:53
2
Although I've received the following message:ls: cannot access /sys/bus/pci/drivers/ehci_hcd/: No such file or directorythis has resolved the issue, the mouse has started working immediately. +1
– Attila Fulop
Oct 10 '14 at 6:16
2
@Otheus OHCI and UHCI are the USB 1.1 host standards, EHCI is the USB 2.0 host standard, and XHCI is the USB 3.0 host standard.
– ssokolow
Jul 20 '16 at 19:02
2
This is a beautiful solution. However, on some later Kernels and other *nix distributions, you will find that you need to substitute*hci_hcdwith*hci-pci, as the hci_hcd driver is already compiled into the Kernel.
– not2qubit
Mar 1 '17 at 17:14
1
On a Banana Pi, there apparently is no PCI bus, I had to use the following:for i in /sys/bus/usb/drivers/*/*:*; do
– Martin Hansen
Jun 29 '17 at 9:14
|
show 5 more comments
do you believe it may work to wakeup an usb storage?
– Aquarius Power
Jun 30 '14 at 5:53
2
Although I've received the following message:ls: cannot access /sys/bus/pci/drivers/ehci_hcd/: No such file or directorythis has resolved the issue, the mouse has started working immediately. +1
– Attila Fulop
Oct 10 '14 at 6:16
2
@Otheus OHCI and UHCI are the USB 1.1 host standards, EHCI is the USB 2.0 host standard, and XHCI is the USB 3.0 host standard.
– ssokolow
Jul 20 '16 at 19:02
2
This is a beautiful solution. However, on some later Kernels and other *nix distributions, you will find that you need to substitute*hci_hcdwith*hci-pci, as the hci_hcd driver is already compiled into the Kernel.
– not2qubit
Mar 1 '17 at 17:14
1
On a Banana Pi, there apparently is no PCI bus, I had to use the following:for i in /sys/bus/usb/drivers/*/*:*; do
– Martin Hansen
Jun 29 '17 at 9:14
do you believe it may work to wakeup an usb storage?
– Aquarius Power
Jun 30 '14 at 5:53
do you believe it may work to wakeup an usb storage?
– Aquarius Power
Jun 30 '14 at 5:53
2
2
Although I've received the following message:
ls: cannot access /sys/bus/pci/drivers/ehci_hcd/: No such file or directory this has resolved the issue, the mouse has started working immediately. +1– Attila Fulop
Oct 10 '14 at 6:16
Although I've received the following message:
ls: cannot access /sys/bus/pci/drivers/ehci_hcd/: No such file or directory this has resolved the issue, the mouse has started working immediately. +1– Attila Fulop
Oct 10 '14 at 6:16
2
2
@Otheus OHCI and UHCI are the USB 1.1 host standards, EHCI is the USB 2.0 host standard, and XHCI is the USB 3.0 host standard.
– ssokolow
Jul 20 '16 at 19:02
@Otheus OHCI and UHCI are the USB 1.1 host standards, EHCI is the USB 2.0 host standard, and XHCI is the USB 3.0 host standard.
– ssokolow
Jul 20 '16 at 19:02
2
2
This is a beautiful solution. However, on some later Kernels and other *nix distributions, you will find that you need to substitute
*hci_hcd with *hci-pci, as the hci_hcd driver is already compiled into the Kernel.– not2qubit
Mar 1 '17 at 17:14
This is a beautiful solution. However, on some later Kernels and other *nix distributions, you will find that you need to substitute
*hci_hcd with *hci-pci, as the hci_hcd driver is already compiled into the Kernel.– not2qubit
Mar 1 '17 at 17:14
1
1
On a Banana Pi, there apparently is no PCI bus, I had to use the following:
for i in /sys/bus/usb/drivers/*/*:*; do– Martin Hansen
Jun 29 '17 at 9:14
On a Banana Pi, there apparently is no PCI bus, I had to use the following:
for i in /sys/bus/usb/drivers/*/*:*; do– Martin Hansen
Jun 29 '17 at 9:14
|
show 5 more comments
I needed to automate this in a python script, so I adapted LiLo's extremely helpful answer to the following:
#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE
import fcntl
driver = sys.argv[-1]
print "resetting driver:", driver
USBDEVFS_RESET= 21780
try:
lsusb_out = Popen("lsusb | grep -i %s"%driver, shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().split()
bus = lsusb_out[1]
device = lsusb_out[3][:-1]
f = open("/dev/bus/usb/%s/%s"%(bus, device), 'w', os.O_WRONLY)
fcntl.ioctl(f, USBDEVFS_RESET, 0)
except Exception, msg:
print "failed to reset device:", msg
In my case it was the cp210x driver (which I could tell from lsmod | grep usbserial), so you could save the above snippet as reset_usb.py and then do this:
sudo python reset_usb.py cp210x
This might also be helpful if you don't already have a c compiler setup on your system, but you do have python.
worked for me on a Raspberry
– webo80
Jun 23 '16 at 14:36
1
A few more words on your solution please. For example, something about the constantUSBDEVFS_RESET. Is it always the same for all systems?
– not2qubit
Feb 28 '17 at 7:25
@not2qubitUSBDEVFS_RESETis the same for all systems. For MIPS it is 536892692.
– yegorich
Apr 21 '17 at 9:09
Newer versions of lsusb seem to need the-targument (tree mode) to show the driver info that this script is expecting, but the script then needs some updates to parse the different output lines this generates
– Cheetah
Oct 19 '17 at 18:56
See my answer here askubuntu.com/a/988297/558070 for a much improved version of this script.
– mcarans
Dec 21 '17 at 10:30
add a comment |
I needed to automate this in a python script, so I adapted LiLo's extremely helpful answer to the following:
#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE
import fcntl
driver = sys.argv[-1]
print "resetting driver:", driver
USBDEVFS_RESET= 21780
try:
lsusb_out = Popen("lsusb | grep -i %s"%driver, shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().split()
bus = lsusb_out[1]
device = lsusb_out[3][:-1]
f = open("/dev/bus/usb/%s/%s"%(bus, device), 'w', os.O_WRONLY)
fcntl.ioctl(f, USBDEVFS_RESET, 0)
except Exception, msg:
print "failed to reset device:", msg
In my case it was the cp210x driver (which I could tell from lsmod | grep usbserial), so you could save the above snippet as reset_usb.py and then do this:
sudo python reset_usb.py cp210x
This might also be helpful if you don't already have a c compiler setup on your system, but you do have python.
worked for me on a Raspberry
– webo80
Jun 23 '16 at 14:36
1
A few more words on your solution please. For example, something about the constantUSBDEVFS_RESET. Is it always the same for all systems?
– not2qubit
Feb 28 '17 at 7:25
@not2qubitUSBDEVFS_RESETis the same for all systems. For MIPS it is 536892692.
– yegorich
Apr 21 '17 at 9:09
Newer versions of lsusb seem to need the-targument (tree mode) to show the driver info that this script is expecting, but the script then needs some updates to parse the different output lines this generates
– Cheetah
Oct 19 '17 at 18:56
See my answer here askubuntu.com/a/988297/558070 for a much improved version of this script.
– mcarans
Dec 21 '17 at 10:30
add a comment |
I needed to automate this in a python script, so I adapted LiLo's extremely helpful answer to the following:
#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE
import fcntl
driver = sys.argv[-1]
print "resetting driver:", driver
USBDEVFS_RESET= 21780
try:
lsusb_out = Popen("lsusb | grep -i %s"%driver, shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().split()
bus = lsusb_out[1]
device = lsusb_out[3][:-1]
f = open("/dev/bus/usb/%s/%s"%(bus, device), 'w', os.O_WRONLY)
fcntl.ioctl(f, USBDEVFS_RESET, 0)
except Exception, msg:
print "failed to reset device:", msg
In my case it was the cp210x driver (which I could tell from lsmod | grep usbserial), so you could save the above snippet as reset_usb.py and then do this:
sudo python reset_usb.py cp210x
This might also be helpful if you don't already have a c compiler setup on your system, but you do have python.
I needed to automate this in a python script, so I adapted LiLo's extremely helpful answer to the following:
#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE
import fcntl
driver = sys.argv[-1]
print "resetting driver:", driver
USBDEVFS_RESET= 21780
try:
lsusb_out = Popen("lsusb | grep -i %s"%driver, shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().split()
bus = lsusb_out[1]
device = lsusb_out[3][:-1]
f = open("/dev/bus/usb/%s/%s"%(bus, device), 'w', os.O_WRONLY)
fcntl.ioctl(f, USBDEVFS_RESET, 0)
except Exception, msg:
print "failed to reset device:", msg
In my case it was the cp210x driver (which I could tell from lsmod | grep usbserial), so you could save the above snippet as reset_usb.py and then do this:
sudo python reset_usb.py cp210x
This might also be helpful if you don't already have a c compiler setup on your system, but you do have python.
edited Nov 9 '16 at 18:37
David Foerster
28.5k1366112
28.5k1366112
answered Mar 2 '15 at 20:38
PeterPeter
28135
28135
worked for me on a Raspberry
– webo80
Jun 23 '16 at 14:36
1
A few more words on your solution please. For example, something about the constantUSBDEVFS_RESET. Is it always the same for all systems?
– not2qubit
Feb 28 '17 at 7:25
@not2qubitUSBDEVFS_RESETis the same for all systems. For MIPS it is 536892692.
– yegorich
Apr 21 '17 at 9:09
Newer versions of lsusb seem to need the-targument (tree mode) to show the driver info that this script is expecting, but the script then needs some updates to parse the different output lines this generates
– Cheetah
Oct 19 '17 at 18:56
See my answer here askubuntu.com/a/988297/558070 for a much improved version of this script.
– mcarans
Dec 21 '17 at 10:30
add a comment |
worked for me on a Raspberry
– webo80
Jun 23 '16 at 14:36
1
A few more words on your solution please. For example, something about the constantUSBDEVFS_RESET. Is it always the same for all systems?
– not2qubit
Feb 28 '17 at 7:25
@not2qubitUSBDEVFS_RESETis the same for all systems. For MIPS it is 536892692.
– yegorich
Apr 21 '17 at 9:09
Newer versions of lsusb seem to need the-targument (tree mode) to show the driver info that this script is expecting, but the script then needs some updates to parse the different output lines this generates
– Cheetah
Oct 19 '17 at 18:56
See my answer here askubuntu.com/a/988297/558070 for a much improved version of this script.
– mcarans
Dec 21 '17 at 10:30
worked for me on a Raspberry
– webo80
Jun 23 '16 at 14:36
worked for me on a Raspberry
– webo80
Jun 23 '16 at 14:36
1
1
A few more words on your solution please. For example, something about the constant
USBDEVFS_RESET. Is it always the same for all systems?– not2qubit
Feb 28 '17 at 7:25
A few more words on your solution please. For example, something about the constant
USBDEVFS_RESET. Is it always the same for all systems?– not2qubit
Feb 28 '17 at 7:25
@not2qubit
USBDEVFS_RESET is the same for all systems. For MIPS it is 536892692.– yegorich
Apr 21 '17 at 9:09
@not2qubit
USBDEVFS_RESET is the same for all systems. For MIPS it is 536892692.– yegorich
Apr 21 '17 at 9:09
Newer versions of lsusb seem to need the
-t argument (tree mode) to show the driver info that this script is expecting, but the script then needs some updates to parse the different output lines this generates– Cheetah
Oct 19 '17 at 18:56
Newer versions of lsusb seem to need the
-t argument (tree mode) to show the driver info that this script is expecting, but the script then needs some updates to parse the different output lines this generates– Cheetah
Oct 19 '17 at 18:56
See my answer here askubuntu.com/a/988297/558070 for a much improved version of this script.
– mcarans
Dec 21 '17 at 10:30
See my answer here askubuntu.com/a/988297/558070 for a much improved version of this script.
– mcarans
Dec 21 '17 at 10:30
add a comment |
I'm using kind of sledgehammer by reloading the modules.
This is my usb_reset.sh script:
#!/bin/bash
# USB drivers
rmmod xhci_pci
rmmod ehci_pci
# uncomment if you have firewire
#rmmod ohci_pci
modprobe xhci_pci
modprobe ehci_pci
# uncomment if you have firewire
#modprobe ohci_pci
And this is my systemd service file /usr/lib/systemd/system/usbreset.service which runs usb_reset.sh after my diplay manager has started:
[Unit]
Description=usbreset Service
After=gdm.service
Wants=gdm.service
[Service]
Type=oneshot
ExecStart=/path/to/usb_reset.sh
Using the listpci option of my script here: askubuntu.com/a/988297/558070 will help identify which USB module to reload (eg. xhci_pci, ehci_pci).
– mcarans
Jan 24 '18 at 15:14
1
Unfortunately on my system these kernel modules are not separate form the kernel, so this won't work:rmmod: ERROR: Module xhci_pci is builtin.
– unfa
Jun 28 '18 at 13:00
add a comment |
I'm using kind of sledgehammer by reloading the modules.
This is my usb_reset.sh script:
#!/bin/bash
# USB drivers
rmmod xhci_pci
rmmod ehci_pci
# uncomment if you have firewire
#rmmod ohci_pci
modprobe xhci_pci
modprobe ehci_pci
# uncomment if you have firewire
#modprobe ohci_pci
And this is my systemd service file /usr/lib/systemd/system/usbreset.service which runs usb_reset.sh after my diplay manager has started:
[Unit]
Description=usbreset Service
After=gdm.service
Wants=gdm.service
[Service]
Type=oneshot
ExecStart=/path/to/usb_reset.sh
Using the listpci option of my script here: askubuntu.com/a/988297/558070 will help identify which USB module to reload (eg. xhci_pci, ehci_pci).
– mcarans
Jan 24 '18 at 15:14
1
Unfortunately on my system these kernel modules are not separate form the kernel, so this won't work:rmmod: ERROR: Module xhci_pci is builtin.
– unfa
Jun 28 '18 at 13:00
add a comment |
I'm using kind of sledgehammer by reloading the modules.
This is my usb_reset.sh script:
#!/bin/bash
# USB drivers
rmmod xhci_pci
rmmod ehci_pci
# uncomment if you have firewire
#rmmod ohci_pci
modprobe xhci_pci
modprobe ehci_pci
# uncomment if you have firewire
#modprobe ohci_pci
And this is my systemd service file /usr/lib/systemd/system/usbreset.service which runs usb_reset.sh after my diplay manager has started:
[Unit]
Description=usbreset Service
After=gdm.service
Wants=gdm.service
[Service]
Type=oneshot
ExecStart=/path/to/usb_reset.sh
I'm using kind of sledgehammer by reloading the modules.
This is my usb_reset.sh script:
#!/bin/bash
# USB drivers
rmmod xhci_pci
rmmod ehci_pci
# uncomment if you have firewire
#rmmod ohci_pci
modprobe xhci_pci
modprobe ehci_pci
# uncomment if you have firewire
#modprobe ohci_pci
And this is my systemd service file /usr/lib/systemd/system/usbreset.service which runs usb_reset.sh after my diplay manager has started:
[Unit]
Description=usbreset Service
After=gdm.service
Wants=gdm.service
[Service]
Type=oneshot
ExecStart=/path/to/usb_reset.sh
edited Jan 16 '16 at 9:21
answered Jan 9 '16 at 10:18
Ulrich-Lorenz SchlüterUlrich-Lorenz Schlüter
55626
55626
Using the listpci option of my script here: askubuntu.com/a/988297/558070 will help identify which USB module to reload (eg. xhci_pci, ehci_pci).
– mcarans
Jan 24 '18 at 15:14
1
Unfortunately on my system these kernel modules are not separate form the kernel, so this won't work:rmmod: ERROR: Module xhci_pci is builtin.
– unfa
Jun 28 '18 at 13:00
add a comment |
Using the listpci option of my script here: askubuntu.com/a/988297/558070 will help identify which USB module to reload (eg. xhci_pci, ehci_pci).
– mcarans
Jan 24 '18 at 15:14
1
Unfortunately on my system these kernel modules are not separate form the kernel, so this won't work:rmmod: ERROR: Module xhci_pci is builtin.
– unfa
Jun 28 '18 at 13:00
Using the listpci option of my script here: askubuntu.com/a/988297/558070 will help identify which USB module to reload (eg. xhci_pci, ehci_pci).
– mcarans
Jan 24 '18 at 15:14
Using the listpci option of my script here: askubuntu.com/a/988297/558070 will help identify which USB module to reload (eg. xhci_pci, ehci_pci).
– mcarans
Jan 24 '18 at 15:14
1
1
Unfortunately on my system these kernel modules are not separate form the kernel, so this won't work:
rmmod: ERROR: Module xhci_pci is builtin.– unfa
Jun 28 '18 at 13:00
Unfortunately on my system these kernel modules are not separate form the kernel, so this won't work:
rmmod: ERROR: Module xhci_pci is builtin.– unfa
Jun 28 '18 at 13:00
add a comment |
As the special case of the question is a communication problem of gphoto2 with a camera on USB, there is an option in gphoto2 to reset its USB connection:
gphoto2 --reset
Maybe this option didn't exist in 2010 when the question was asked.
add a comment |
As the special case of the question is a communication problem of gphoto2 with a camera on USB, there is an option in gphoto2 to reset its USB connection:
gphoto2 --reset
Maybe this option didn't exist in 2010 when the question was asked.
add a comment |
As the special case of the question is a communication problem of gphoto2 with a camera on USB, there is an option in gphoto2 to reset its USB connection:
gphoto2 --reset
Maybe this option didn't exist in 2010 when the question was asked.
As the special case of the question is a communication problem of gphoto2 with a camera on USB, there is an option in gphoto2 to reset its USB connection:
gphoto2 --reset
Maybe this option didn't exist in 2010 when the question was asked.
edited Aug 31 '16 at 18:49
answered Aug 31 '16 at 13:19
mviereckmviereck
1996
1996
add a comment |
add a comment |
Quickest way to reset will be to reset the USB controller itself. Doing so will enforce udev to unregister the device on disconnection, and register is back once you enable it.
echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci_hcd/unbind
echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci_hcd/unbind
echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci_hcd/bind
echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci_hcd/bind
This should work for most PC environment. However, if you are using some custom hardware you can simply iterate through the device names. With this method you don't need to find out the device name by lsusb. You can incorporate in a automated script as well.
1
You need to run these commands as root/sudo, and it will not work on all systems (on some, you'll need to replaceehci_hcdwithehci-pci. More info on this solution (perhaps where it came from?): davidjb.com/blog/2012/06/…
– Lambart
Nov 5 '15 at 17:43
add a comment |
Quickest way to reset will be to reset the USB controller itself. Doing so will enforce udev to unregister the device on disconnection, and register is back once you enable it.
echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci_hcd/unbind
echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci_hcd/unbind
echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci_hcd/bind
echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci_hcd/bind
This should work for most PC environment. However, if you are using some custom hardware you can simply iterate through the device names. With this method you don't need to find out the device name by lsusb. You can incorporate in a automated script as well.
1
You need to run these commands as root/sudo, and it will not work on all systems (on some, you'll need to replaceehci_hcdwithehci-pci. More info on this solution (perhaps where it came from?): davidjb.com/blog/2012/06/…
– Lambart
Nov 5 '15 at 17:43
add a comment |
Quickest way to reset will be to reset the USB controller itself. Doing so will enforce udev to unregister the device on disconnection, and register is back once you enable it.
echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci_hcd/unbind
echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci_hcd/unbind
echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci_hcd/bind
echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci_hcd/bind
This should work for most PC environment. However, if you are using some custom hardware you can simply iterate through the device names. With this method you don't need to find out the device name by lsusb. You can incorporate in a automated script as well.
Quickest way to reset will be to reset the USB controller itself. Doing so will enforce udev to unregister the device on disconnection, and register is back once you enable it.
echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci_hcd/unbind
echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci_hcd/unbind
echo -n "0000:00:1a.0" | tee /sys/bus/pci/drivers/ehci_hcd/bind
echo -n "0000:00:1d.0" | tee /sys/bus/pci/drivers/ehci_hcd/bind
This should work for most PC environment. However, if you are using some custom hardware you can simply iterate through the device names. With this method you don't need to find out the device name by lsusb. You can incorporate in a automated script as well.
answered Nov 24 '14 at 19:34
chandankchandank
259412
259412
1
You need to run these commands as root/sudo, and it will not work on all systems (on some, you'll need to replaceehci_hcdwithehci-pci. More info on this solution (perhaps where it came from?): davidjb.com/blog/2012/06/…
– Lambart
Nov 5 '15 at 17:43
add a comment |
1
You need to run these commands as root/sudo, and it will not work on all systems (on some, you'll need to replaceehci_hcdwithehci-pci. More info on this solution (perhaps where it came from?): davidjb.com/blog/2012/06/…
– Lambart
Nov 5 '15 at 17:43
1
1
You need to run these commands as root/sudo, and it will not work on all systems (on some, you'll need to replace
ehci_hcd with ehci-pci. More info on this solution (perhaps where it came from?): davidjb.com/blog/2012/06/…– Lambart
Nov 5 '15 at 17:43
You need to run these commands as root/sudo, and it will not work on all systems (on some, you'll need to replace
ehci_hcd with ehci-pci. More info on this solution (perhaps where it came from?): davidjb.com/blog/2012/06/…– Lambart
Nov 5 '15 at 17:43
add a comment |
I made a python script which will reset a particular USB device based on the device number. You can find out the device number from command lsusb.
for example:
$ lsusb
Bus 002 Device 004: ID 046d:c312 Logitech, Inc. DeLuxe 250 Keyboard
In this string 004 is the device number
import os
import argparse
import subprocess
path='/sys/bus/usb/devices/'
def runbash(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
out = p.stdout.read().strip()
return out
def reset_device(dev_num):
sub_dirs = []
for root, dirs, files in os.walk(path):
for name in dirs:
sub_dirs.append(os.path.join(root, name))
dev_found = 0
for sub_dir in sub_dirs:
if True == os.path.isfile(sub_dir+'/devnum'):
fd = open(sub_dir+'/devnum','r')
line = fd.readline()
if int(dev_num) == int(line):
print ('Your device is at: '+sub_dir)
dev_found = 1
break
fd.close()
if dev_found == 1:
reset_file = sub_dir+'/authorized'
runbash('echo 0 > '+reset_file)
runbash('echo 1 > '+reset_file)
print ('Device reset successful')
else:
print ("No such device")
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--devnum', dest='devnum')
args = parser.parse_args()
if args.devnum is None:
print('Usage:usb_reset.py -d <device_number> nThe device number can be obtained from lsusb command result')
return
reset_device(args.devnum)
if __name__=='__main__':
main()
add a comment |
I made a python script which will reset a particular USB device based on the device number. You can find out the device number from command lsusb.
for example:
$ lsusb
Bus 002 Device 004: ID 046d:c312 Logitech, Inc. DeLuxe 250 Keyboard
In this string 004 is the device number
import os
import argparse
import subprocess
path='/sys/bus/usb/devices/'
def runbash(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
out = p.stdout.read().strip()
return out
def reset_device(dev_num):
sub_dirs = []
for root, dirs, files in os.walk(path):
for name in dirs:
sub_dirs.append(os.path.join(root, name))
dev_found = 0
for sub_dir in sub_dirs:
if True == os.path.isfile(sub_dir+'/devnum'):
fd = open(sub_dir+'/devnum','r')
line = fd.readline()
if int(dev_num) == int(line):
print ('Your device is at: '+sub_dir)
dev_found = 1
break
fd.close()
if dev_found == 1:
reset_file = sub_dir+'/authorized'
runbash('echo 0 > '+reset_file)
runbash('echo 1 > '+reset_file)
print ('Device reset successful')
else:
print ("No such device")
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--devnum', dest='devnum')
args = parser.parse_args()
if args.devnum is None:
print('Usage:usb_reset.py -d <device_number> nThe device number can be obtained from lsusb command result')
return
reset_device(args.devnum)
if __name__=='__main__':
main()
add a comment |
I made a python script which will reset a particular USB device based on the device number. You can find out the device number from command lsusb.
for example:
$ lsusb
Bus 002 Device 004: ID 046d:c312 Logitech, Inc. DeLuxe 250 Keyboard
In this string 004 is the device number
import os
import argparse
import subprocess
path='/sys/bus/usb/devices/'
def runbash(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
out = p.stdout.read().strip()
return out
def reset_device(dev_num):
sub_dirs = []
for root, dirs, files in os.walk(path):
for name in dirs:
sub_dirs.append(os.path.join(root, name))
dev_found = 0
for sub_dir in sub_dirs:
if True == os.path.isfile(sub_dir+'/devnum'):
fd = open(sub_dir+'/devnum','r')
line = fd.readline()
if int(dev_num) == int(line):
print ('Your device is at: '+sub_dir)
dev_found = 1
break
fd.close()
if dev_found == 1:
reset_file = sub_dir+'/authorized'
runbash('echo 0 > '+reset_file)
runbash('echo 1 > '+reset_file)
print ('Device reset successful')
else:
print ("No such device")
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--devnum', dest='devnum')
args = parser.parse_args()
if args.devnum is None:
print('Usage:usb_reset.py -d <device_number> nThe device number can be obtained from lsusb command result')
return
reset_device(args.devnum)
if __name__=='__main__':
main()
I made a python script which will reset a particular USB device based on the device number. You can find out the device number from command lsusb.
for example:
$ lsusb
Bus 002 Device 004: ID 046d:c312 Logitech, Inc. DeLuxe 250 Keyboard
In this string 004 is the device number
import os
import argparse
import subprocess
path='/sys/bus/usb/devices/'
def runbash(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
out = p.stdout.read().strip()
return out
def reset_device(dev_num):
sub_dirs = []
for root, dirs, files in os.walk(path):
for name in dirs:
sub_dirs.append(os.path.join(root, name))
dev_found = 0
for sub_dir in sub_dirs:
if True == os.path.isfile(sub_dir+'/devnum'):
fd = open(sub_dir+'/devnum','r')
line = fd.readline()
if int(dev_num) == int(line):
print ('Your device is at: '+sub_dir)
dev_found = 1
break
fd.close()
if dev_found == 1:
reset_file = sub_dir+'/authorized'
runbash('echo 0 > '+reset_file)
runbash('echo 1 > '+reset_file)
print ('Device reset successful')
else:
print ("No such device")
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--devnum', dest='devnum')
args = parser.parse_args()
if args.devnum is None:
print('Usage:usb_reset.py -d <device_number> nThe device number can be obtained from lsusb command result')
return
reset_device(args.devnum)
if __name__=='__main__':
main()
edited Dec 19 '16 at 22:05
Jonathon Reinhart
1033
1033
answered Sep 7 '16 at 11:42
RaghuRaghu
211
211
add a comment |
add a comment |
Here is script that will only reset a matching product/vendor ID.
#!/bin/bash
set -euo pipefail
IFS=$'nt'
VENDOR="045e"
PRODUCT="0719"
for DIR in $(find /sys/bus/usb/devices/ -maxdepth 1 -type l); do
if [[ -f $DIR/idVendor && -f $DIR/idProduct &&
$(cat $DIR/idVendor) == $VENDOR && $(cat $DIR/idProduct) == $PRODUCT ]]; then
echo 0 > $DIR/authorized
sleep 0.5
echo 1 > $DIR/authorized
fi
done
1
I found your script is useful. But what should I do if the$DIRdisappears and device is not visible?
– Eugen Konkov
Oct 13 '17 at 7:36
add a comment |
Here is script that will only reset a matching product/vendor ID.
#!/bin/bash
set -euo pipefail
IFS=$'nt'
VENDOR="045e"
PRODUCT="0719"
for DIR in $(find /sys/bus/usb/devices/ -maxdepth 1 -type l); do
if [[ -f $DIR/idVendor && -f $DIR/idProduct &&
$(cat $DIR/idVendor) == $VENDOR && $(cat $DIR/idProduct) == $PRODUCT ]]; then
echo 0 > $DIR/authorized
sleep 0.5
echo 1 > $DIR/authorized
fi
done
1
I found your script is useful. But what should I do if the$DIRdisappears and device is not visible?
– Eugen Konkov
Oct 13 '17 at 7:36
add a comment |
Here is script that will only reset a matching product/vendor ID.
#!/bin/bash
set -euo pipefail
IFS=$'nt'
VENDOR="045e"
PRODUCT="0719"
for DIR in $(find /sys/bus/usb/devices/ -maxdepth 1 -type l); do
if [[ -f $DIR/idVendor && -f $DIR/idProduct &&
$(cat $DIR/idVendor) == $VENDOR && $(cat $DIR/idProduct) == $PRODUCT ]]; then
echo 0 > $DIR/authorized
sleep 0.5
echo 1 > $DIR/authorized
fi
done
Here is script that will only reset a matching product/vendor ID.
#!/bin/bash
set -euo pipefail
IFS=$'nt'
VENDOR="045e"
PRODUCT="0719"
for DIR in $(find /sys/bus/usb/devices/ -maxdepth 1 -type l); do
if [[ -f $DIR/idVendor && -f $DIR/idProduct &&
$(cat $DIR/idVendor) == $VENDOR && $(cat $DIR/idProduct) == $PRODUCT ]]; then
echo 0 > $DIR/authorized
sleep 0.5
echo 1 > $DIR/authorized
fi
done
edited Nov 21 '17 at 11:32
derHugo
2,32521531
2,32521531
answered Apr 30 '17 at 3:50
cmcgintycmcginty
2,74652431
2,74652431
1
I found your script is useful. But what should I do if the$DIRdisappears and device is not visible?
– Eugen Konkov
Oct 13 '17 at 7:36
add a comment |
1
I found your script is useful. But what should I do if the$DIRdisappears and device is not visible?
– Eugen Konkov
Oct 13 '17 at 7:36
1
1
I found your script is useful. But what should I do if the
$DIR disappears and device is not visible?– Eugen Konkov
Oct 13 '17 at 7:36
I found your script is useful. But what should I do if the
$DIR disappears and device is not visible?– Eugen Konkov
Oct 13 '17 at 7:36
add a comment |
I've created a Python script that simplifies the whole process based on answers here.
Save the script below as reset_usb.py or clone this repo.
Usage:
python reset_usb.py help # Show this help
sudo python reset_usb.py list # List all USB devices
sudo python reset_usb.py path /dev/bus/usb/XXX/YYY # Reset USB device using path /dev/bus/usb/XXX/YYY
sudo python reset_usb.py search "search terms" # Search for USB device using the search terms within the search string returned by list and reset matching device
sudo python reset_usb.py listpci # List all PCI USB devices
sudo python reset_usb.py pathpci /sys/bus/pci/drivers/.../XXXX:XX:XX.X # Reset PCI USB device using path /sys/bus/pci/drivers/.../XXXX:XX:XX.X
sudo python reset_usb.py searchpci "search terms" # Search for PCI USB device using the search terms within the search string returned by listpci and reset matching device
Script:
#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE
import fcntl
instructions = '''
Usage: python reset_usb.py help : Show this help
sudo python reset_usb.py list : List all USB devices
sudo python reset_usb.py path /dev/bus/usb/XXX/YYY : Reset USB device using path /dev/bus/usb/XXX/YYY
sudo python reset_usb.py search "search terms" : Search for USB device using the search terms within the search string returned by list and reset matching device
sudo python reset_usb.py listpci : List all PCI USB devices
sudo python reset_usb.py pathpci /sys/bus/pci/drivers/.../XXXX:XX:XX.X : Reset PCI USB device using path
sudo python reset_usb.py searchpci "search terms" : Search for PCI USB device using the search terms within the search string returned by listpci and reset matching device
'''
if len(sys.argv) < 2:
print(instructions)
sys.exit(0)
option = sys.argv[1].lower()
if 'help' in option:
print(instructions)
sys.exit(0)
def create_pci_list():
pci_usb_list = list()
try:
lspci_out = Popen('lspci -Dvmm', shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().decode('utf-8')
pci_devices = lspci_out.split('%s%s' % (os.linesep, os.linesep))
for pci_device in pci_devices:
device_dict = dict()
categories = pci_device.split(os.linesep)
for category in categories:
key, value = category.split('t')
device_dict[key[:-1]] = value.strip()
if 'USB' not in device_dict['Class']:
continue
for root, dirs, files in os.walk('/sys/bus/pci/drivers/'):
slot = device_dict['Slot']
if slot in dirs:
device_dict['path'] = os.path.join(root, slot)
break
pci_usb_list.append(device_dict)
except Exception as ex:
print('Failed to list pci devices! Error: %s' % ex)
sys.exit(-1)
return pci_usb_list
def create_usb_list():
device_list = list()
try:
lsusb_out = Popen('lsusb -v', shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().decode('utf-8')
usb_devices = lsusb_out.split('%s%s' % (os.linesep, os.linesep))
for device_categories in usb_devices:
if not device_categories:
continue
categories = device_categories.split(os.linesep)
device_stuff = categories[0].strip().split()
bus = device_stuff[1]
device = device_stuff[3][:-1]
device_dict = 'bus': bus, 'device': device
device_info = ' '.join(device_stuff[6:])
device_dict['description'] = device_info
for category in categories:
if not category:
continue
categoryinfo = category.strip().split()
if categoryinfo[0] == 'iManufacturer':
manufacturer_info = ' '.join(categoryinfo[2:])
device_dict['manufacturer'] = manufacturer_info
if categoryinfo[0] == 'iProduct':
device_info = ' '.join(categoryinfo[2:])
device_dict['device'] = device_info
path = '/dev/bus/usb/%s/%s' % (bus, device)
device_dict['path'] = path
device_list.append(device_dict)
except Exception as ex:
print('Failed to list usb devices! Error: %s' % ex)
sys.exit(-1)
return device_list
if 'listpci' in option:
pci_usb_list = create_pci_list()
for device in pci_usb_list:
print('path=%s' % device['path'])
print(' manufacturer=%s' % device['SVendor'])
print(' device=%s' % device['SDevice'])
print(' search string=%s %s' % (device['SVendor'], device['SDevice']))
sys.exit(0)
if 'list' in option:
usb_list = create_usb_list()
for device in usb_list:
print('path=%s' % device['path'])
print(' description=%s' % device['description'])
print(' manufacturer=%s' % device['manufacturer'])
print(' device=%s' % device['device'])
print(' search string=%s %s %s' % (device['description'], device['manufacturer'], device['device']))
sys.exit(0)
if len(sys.argv) < 3:
print(instructions)
sys.exit(0)
option2 = sys.argv[2]
print('Resetting device: %s' % option2)
# echo -n "0000:39:00.0" | tee /sys/bus/pci/drivers/xhci_hcd/unbind;echo -n "0000:39:00.0" | tee /sys/bus/pci/drivers/xhci_hcd/bind
def reset_pci_usb_device(dev_path):
folder, slot = os.path.split(dev_path)
try:
fp = open(os.path.join(folder, 'unbind'), 'wt')
fp.write(slot)
fp.close()
fp = open(os.path.join(folder, 'bind'), 'wt')
fp.write(slot)
fp.close()
print('Successfully reset %s' % dev_path)
sys.exit(0)
except Exception as ex:
print('Failed to reset device! Error: %s' % ex)
sys.exit(-1)
if 'pathpci' in option:
reset_pci_usb_device(option2)
if 'searchpci' in option:
pci_usb_list = create_pci_list()
for device in pci_usb_list:
text = '%s %s' % (device['SVendor'], device['SDevice'])
if option2 in text:
reset_pci_usb_device(device['path'])
print('Failed to find device!')
sys.exit(-1)
def reset_usb_device(dev_path):
USBDEVFS_RESET = 21780
try:
f = open(dev_path, 'w', os.O_WRONLY)
fcntl.ioctl(f, USBDEVFS_RESET, 0)
print('Successfully reset %s' % dev_path)
sys.exit(0)
except Exception as ex:
print('Failed to reset device! Error: %s' % ex)
sys.exit(-1)
if 'path' in option:
reset_usb_device(option2)
if 'search' in option:
usb_list = create_usb_list()
for device in usb_list:
text = '%s %s %s' % (device['description'], device['manufacturer'], device['device'])
if option2 in text:
reset_usb_device(device['path'])
print('Failed to find device!')
sys.exit(-1)
add a comment |
I've created a Python script that simplifies the whole process based on answers here.
Save the script below as reset_usb.py or clone this repo.
Usage:
python reset_usb.py help # Show this help
sudo python reset_usb.py list # List all USB devices
sudo python reset_usb.py path /dev/bus/usb/XXX/YYY # Reset USB device using path /dev/bus/usb/XXX/YYY
sudo python reset_usb.py search "search terms" # Search for USB device using the search terms within the search string returned by list and reset matching device
sudo python reset_usb.py listpci # List all PCI USB devices
sudo python reset_usb.py pathpci /sys/bus/pci/drivers/.../XXXX:XX:XX.X # Reset PCI USB device using path /sys/bus/pci/drivers/.../XXXX:XX:XX.X
sudo python reset_usb.py searchpci "search terms" # Search for PCI USB device using the search terms within the search string returned by listpci and reset matching device
Script:
#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE
import fcntl
instructions = '''
Usage: python reset_usb.py help : Show this help
sudo python reset_usb.py list : List all USB devices
sudo python reset_usb.py path /dev/bus/usb/XXX/YYY : Reset USB device using path /dev/bus/usb/XXX/YYY
sudo python reset_usb.py search "search terms" : Search for USB device using the search terms within the search string returned by list and reset matching device
sudo python reset_usb.py listpci : List all PCI USB devices
sudo python reset_usb.py pathpci /sys/bus/pci/drivers/.../XXXX:XX:XX.X : Reset PCI USB device using path
sudo python reset_usb.py searchpci "search terms" : Search for PCI USB device using the search terms within the search string returned by listpci and reset matching device
'''
if len(sys.argv) < 2:
print(instructions)
sys.exit(0)
option = sys.argv[1].lower()
if 'help' in option:
print(instructions)
sys.exit(0)
def create_pci_list():
pci_usb_list = list()
try:
lspci_out = Popen('lspci -Dvmm', shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().decode('utf-8')
pci_devices = lspci_out.split('%s%s' % (os.linesep, os.linesep))
for pci_device in pci_devices:
device_dict = dict()
categories = pci_device.split(os.linesep)
for category in categories:
key, value = category.split('t')
device_dict[key[:-1]] = value.strip()
if 'USB' not in device_dict['Class']:
continue
for root, dirs, files in os.walk('/sys/bus/pci/drivers/'):
slot = device_dict['Slot']
if slot in dirs:
device_dict['path'] = os.path.join(root, slot)
break
pci_usb_list.append(device_dict)
except Exception as ex:
print('Failed to list pci devices! Error: %s' % ex)
sys.exit(-1)
return pci_usb_list
def create_usb_list():
device_list = list()
try:
lsusb_out = Popen('lsusb -v', shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().decode('utf-8')
usb_devices = lsusb_out.split('%s%s' % (os.linesep, os.linesep))
for device_categories in usb_devices:
if not device_categories:
continue
categories = device_categories.split(os.linesep)
device_stuff = categories[0].strip().split()
bus = device_stuff[1]
device = device_stuff[3][:-1]
device_dict = 'bus': bus, 'device': device
device_info = ' '.join(device_stuff[6:])
device_dict['description'] = device_info
for category in categories:
if not category:
continue
categoryinfo = category.strip().split()
if categoryinfo[0] == 'iManufacturer':
manufacturer_info = ' '.join(categoryinfo[2:])
device_dict['manufacturer'] = manufacturer_info
if categoryinfo[0] == 'iProduct':
device_info = ' '.join(categoryinfo[2:])
device_dict['device'] = device_info
path = '/dev/bus/usb/%s/%s' % (bus, device)
device_dict['path'] = path
device_list.append(device_dict)
except Exception as ex:
print('Failed to list usb devices! Error: %s' % ex)
sys.exit(-1)
return device_list
if 'listpci' in option:
pci_usb_list = create_pci_list()
for device in pci_usb_list:
print('path=%s' % device['path'])
print(' manufacturer=%s' % device['SVendor'])
print(' device=%s' % device['SDevice'])
print(' search string=%s %s' % (device['SVendor'], device['SDevice']))
sys.exit(0)
if 'list' in option:
usb_list = create_usb_list()
for device in usb_list:
print('path=%s' % device['path'])
print(' description=%s' % device['description'])
print(' manufacturer=%s' % device['manufacturer'])
print(' device=%s' % device['device'])
print(' search string=%s %s %s' % (device['description'], device['manufacturer'], device['device']))
sys.exit(0)
if len(sys.argv) < 3:
print(instructions)
sys.exit(0)
option2 = sys.argv[2]
print('Resetting device: %s' % option2)
# echo -n "0000:39:00.0" | tee /sys/bus/pci/drivers/xhci_hcd/unbind;echo -n "0000:39:00.0" | tee /sys/bus/pci/drivers/xhci_hcd/bind
def reset_pci_usb_device(dev_path):
folder, slot = os.path.split(dev_path)
try:
fp = open(os.path.join(folder, 'unbind'), 'wt')
fp.write(slot)
fp.close()
fp = open(os.path.join(folder, 'bind'), 'wt')
fp.write(slot)
fp.close()
print('Successfully reset %s' % dev_path)
sys.exit(0)
except Exception as ex:
print('Failed to reset device! Error: %s' % ex)
sys.exit(-1)
if 'pathpci' in option:
reset_pci_usb_device(option2)
if 'searchpci' in option:
pci_usb_list = create_pci_list()
for device in pci_usb_list:
text = '%s %s' % (device['SVendor'], device['SDevice'])
if option2 in text:
reset_pci_usb_device(device['path'])
print('Failed to find device!')
sys.exit(-1)
def reset_usb_device(dev_path):
USBDEVFS_RESET = 21780
try:
f = open(dev_path, 'w', os.O_WRONLY)
fcntl.ioctl(f, USBDEVFS_RESET, 0)
print('Successfully reset %s' % dev_path)
sys.exit(0)
except Exception as ex:
print('Failed to reset device! Error: %s' % ex)
sys.exit(-1)
if 'path' in option:
reset_usb_device(option2)
if 'search' in option:
usb_list = create_usb_list()
for device in usb_list:
text = '%s %s %s' % (device['description'], device['manufacturer'], device['device'])
if option2 in text:
reset_usb_device(device['path'])
print('Failed to find device!')
sys.exit(-1)
add a comment |
I've created a Python script that simplifies the whole process based on answers here.
Save the script below as reset_usb.py or clone this repo.
Usage:
python reset_usb.py help # Show this help
sudo python reset_usb.py list # List all USB devices
sudo python reset_usb.py path /dev/bus/usb/XXX/YYY # Reset USB device using path /dev/bus/usb/XXX/YYY
sudo python reset_usb.py search "search terms" # Search for USB device using the search terms within the search string returned by list and reset matching device
sudo python reset_usb.py listpci # List all PCI USB devices
sudo python reset_usb.py pathpci /sys/bus/pci/drivers/.../XXXX:XX:XX.X # Reset PCI USB device using path /sys/bus/pci/drivers/.../XXXX:XX:XX.X
sudo python reset_usb.py searchpci "search terms" # Search for PCI USB device using the search terms within the search string returned by listpci and reset matching device
Script:
#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE
import fcntl
instructions = '''
Usage: python reset_usb.py help : Show this help
sudo python reset_usb.py list : List all USB devices
sudo python reset_usb.py path /dev/bus/usb/XXX/YYY : Reset USB device using path /dev/bus/usb/XXX/YYY
sudo python reset_usb.py search "search terms" : Search for USB device using the search terms within the search string returned by list and reset matching device
sudo python reset_usb.py listpci : List all PCI USB devices
sudo python reset_usb.py pathpci /sys/bus/pci/drivers/.../XXXX:XX:XX.X : Reset PCI USB device using path
sudo python reset_usb.py searchpci "search terms" : Search for PCI USB device using the search terms within the search string returned by listpci and reset matching device
'''
if len(sys.argv) < 2:
print(instructions)
sys.exit(0)
option = sys.argv[1].lower()
if 'help' in option:
print(instructions)
sys.exit(0)
def create_pci_list():
pci_usb_list = list()
try:
lspci_out = Popen('lspci -Dvmm', shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().decode('utf-8')
pci_devices = lspci_out.split('%s%s' % (os.linesep, os.linesep))
for pci_device in pci_devices:
device_dict = dict()
categories = pci_device.split(os.linesep)
for category in categories:
key, value = category.split('t')
device_dict[key[:-1]] = value.strip()
if 'USB' not in device_dict['Class']:
continue
for root, dirs, files in os.walk('/sys/bus/pci/drivers/'):
slot = device_dict['Slot']
if slot in dirs:
device_dict['path'] = os.path.join(root, slot)
break
pci_usb_list.append(device_dict)
except Exception as ex:
print('Failed to list pci devices! Error: %s' % ex)
sys.exit(-1)
return pci_usb_list
def create_usb_list():
device_list = list()
try:
lsusb_out = Popen('lsusb -v', shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().decode('utf-8')
usb_devices = lsusb_out.split('%s%s' % (os.linesep, os.linesep))
for device_categories in usb_devices:
if not device_categories:
continue
categories = device_categories.split(os.linesep)
device_stuff = categories[0].strip().split()
bus = device_stuff[1]
device = device_stuff[3][:-1]
device_dict = 'bus': bus, 'device': device
device_info = ' '.join(device_stuff[6:])
device_dict['description'] = device_info
for category in categories:
if not category:
continue
categoryinfo = category.strip().split()
if categoryinfo[0] == 'iManufacturer':
manufacturer_info = ' '.join(categoryinfo[2:])
device_dict['manufacturer'] = manufacturer_info
if categoryinfo[0] == 'iProduct':
device_info = ' '.join(categoryinfo[2:])
device_dict['device'] = device_info
path = '/dev/bus/usb/%s/%s' % (bus, device)
device_dict['path'] = path
device_list.append(device_dict)
except Exception as ex:
print('Failed to list usb devices! Error: %s' % ex)
sys.exit(-1)
return device_list
if 'listpci' in option:
pci_usb_list = create_pci_list()
for device in pci_usb_list:
print('path=%s' % device['path'])
print(' manufacturer=%s' % device['SVendor'])
print(' device=%s' % device['SDevice'])
print(' search string=%s %s' % (device['SVendor'], device['SDevice']))
sys.exit(0)
if 'list' in option:
usb_list = create_usb_list()
for device in usb_list:
print('path=%s' % device['path'])
print(' description=%s' % device['description'])
print(' manufacturer=%s' % device['manufacturer'])
print(' device=%s' % device['device'])
print(' search string=%s %s %s' % (device['description'], device['manufacturer'], device['device']))
sys.exit(0)
if len(sys.argv) < 3:
print(instructions)
sys.exit(0)
option2 = sys.argv[2]
print('Resetting device: %s' % option2)
# echo -n "0000:39:00.0" | tee /sys/bus/pci/drivers/xhci_hcd/unbind;echo -n "0000:39:00.0" | tee /sys/bus/pci/drivers/xhci_hcd/bind
def reset_pci_usb_device(dev_path):
folder, slot = os.path.split(dev_path)
try:
fp = open(os.path.join(folder, 'unbind'), 'wt')
fp.write(slot)
fp.close()
fp = open(os.path.join(folder, 'bind'), 'wt')
fp.write(slot)
fp.close()
print('Successfully reset %s' % dev_path)
sys.exit(0)
except Exception as ex:
print('Failed to reset device! Error: %s' % ex)
sys.exit(-1)
if 'pathpci' in option:
reset_pci_usb_device(option2)
if 'searchpci' in option:
pci_usb_list = create_pci_list()
for device in pci_usb_list:
text = '%s %s' % (device['SVendor'], device['SDevice'])
if option2 in text:
reset_pci_usb_device(device['path'])
print('Failed to find device!')
sys.exit(-1)
def reset_usb_device(dev_path):
USBDEVFS_RESET = 21780
try:
f = open(dev_path, 'w', os.O_WRONLY)
fcntl.ioctl(f, USBDEVFS_RESET, 0)
print('Successfully reset %s' % dev_path)
sys.exit(0)
except Exception as ex:
print('Failed to reset device! Error: %s' % ex)
sys.exit(-1)
if 'path' in option:
reset_usb_device(option2)
if 'search' in option:
usb_list = create_usb_list()
for device in usb_list:
text = '%s %s %s' % (device['description'], device['manufacturer'], device['device'])
if option2 in text:
reset_usb_device(device['path'])
print('Failed to find device!')
sys.exit(-1)
I've created a Python script that simplifies the whole process based on answers here.
Save the script below as reset_usb.py or clone this repo.
Usage:
python reset_usb.py help # Show this help
sudo python reset_usb.py list # List all USB devices
sudo python reset_usb.py path /dev/bus/usb/XXX/YYY # Reset USB device using path /dev/bus/usb/XXX/YYY
sudo python reset_usb.py search "search terms" # Search for USB device using the search terms within the search string returned by list and reset matching device
sudo python reset_usb.py listpci # List all PCI USB devices
sudo python reset_usb.py pathpci /sys/bus/pci/drivers/.../XXXX:XX:XX.X # Reset PCI USB device using path /sys/bus/pci/drivers/.../XXXX:XX:XX.X
sudo python reset_usb.py searchpci "search terms" # Search for PCI USB device using the search terms within the search string returned by listpci and reset matching device
Script:
#!/usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE
import fcntl
instructions = '''
Usage: python reset_usb.py help : Show this help
sudo python reset_usb.py list : List all USB devices
sudo python reset_usb.py path /dev/bus/usb/XXX/YYY : Reset USB device using path /dev/bus/usb/XXX/YYY
sudo python reset_usb.py search "search terms" : Search for USB device using the search terms within the search string returned by list and reset matching device
sudo python reset_usb.py listpci : List all PCI USB devices
sudo python reset_usb.py pathpci /sys/bus/pci/drivers/.../XXXX:XX:XX.X : Reset PCI USB device using path
sudo python reset_usb.py searchpci "search terms" : Search for PCI USB device using the search terms within the search string returned by listpci and reset matching device
'''
if len(sys.argv) < 2:
print(instructions)
sys.exit(0)
option = sys.argv[1].lower()
if 'help' in option:
print(instructions)
sys.exit(0)
def create_pci_list():
pci_usb_list = list()
try:
lspci_out = Popen('lspci -Dvmm', shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().decode('utf-8')
pci_devices = lspci_out.split('%s%s' % (os.linesep, os.linesep))
for pci_device in pci_devices:
device_dict = dict()
categories = pci_device.split(os.linesep)
for category in categories:
key, value = category.split('t')
device_dict[key[:-1]] = value.strip()
if 'USB' not in device_dict['Class']:
continue
for root, dirs, files in os.walk('/sys/bus/pci/drivers/'):
slot = device_dict['Slot']
if slot in dirs:
device_dict['path'] = os.path.join(root, slot)
break
pci_usb_list.append(device_dict)
except Exception as ex:
print('Failed to list pci devices! Error: %s' % ex)
sys.exit(-1)
return pci_usb_list
def create_usb_list():
device_list = list()
try:
lsusb_out = Popen('lsusb -v', shell=True, bufsize=64, stdin=PIPE, stdout=PIPE, close_fds=True).stdout.read().strip().decode('utf-8')
usb_devices = lsusb_out.split('%s%s' % (os.linesep, os.linesep))
for device_categories in usb_devices:
if not device_categories:
continue
categories = device_categories.split(os.linesep)
device_stuff = categories[0].strip().split()
bus = device_stuff[1]
device = device_stuff[3][:-1]
device_dict = 'bus': bus, 'device': device
device_info = ' '.join(device_stuff[6:])
device_dict['description'] = device_info
for category in categories:
if not category:
continue
categoryinfo = category.strip().split()
if categoryinfo[0] == 'iManufacturer':
manufacturer_info = ' '.join(categoryinfo[2:])
device_dict['manufacturer'] = manufacturer_info
if categoryinfo[0] == 'iProduct':
device_info = ' '.join(categoryinfo[2:])
device_dict['device'] = device_info
path = '/dev/bus/usb/%s/%s' % (bus, device)
device_dict['path'] = path
device_list.append(device_dict)
except Exception as ex:
print('Failed to list usb devices! Error: %s' % ex)
sys.exit(-1)
return device_list
if 'listpci' in option:
pci_usb_list = create_pci_list()
for device in pci_usb_list:
print('path=%s' % device['path'])
print(' manufacturer=%s' % device['SVendor'])
print(' device=%s' % device['SDevice'])
print(' search string=%s %s' % (device['SVendor'], device['SDevice']))
sys.exit(0)
if 'list' in option:
usb_list = create_usb_list()
for device in usb_list:
print('path=%s' % device['path'])
print(' description=%s' % device['description'])
print(' manufacturer=%s' % device['manufacturer'])
print(' device=%s' % device['device'])
print(' search string=%s %s %s' % (device['description'], device['manufacturer'], device['device']))
sys.exit(0)
if len(sys.argv) < 3:
print(instructions)
sys.exit(0)
option2 = sys.argv[2]
print('Resetting device: %s' % option2)
# echo -n "0000:39:00.0" | tee /sys/bus/pci/drivers/xhci_hcd/unbind;echo -n "0000:39:00.0" | tee /sys/bus/pci/drivers/xhci_hcd/bind
def reset_pci_usb_device(dev_path):
folder, slot = os.path.split(dev_path)
try:
fp = open(os.path.join(folder, 'unbind'), 'wt')
fp.write(slot)
fp.close()
fp = open(os.path.join(folder, 'bind'), 'wt')
fp.write(slot)
fp.close()
print('Successfully reset %s' % dev_path)
sys.exit(0)
except Exception as ex:
print('Failed to reset device! Error: %s' % ex)
sys.exit(-1)
if 'pathpci' in option:
reset_pci_usb_device(option2)
if 'searchpci' in option:
pci_usb_list = create_pci_list()
for device in pci_usb_list:
text = '%s %s' % (device['SVendor'], device['SDevice'])
if option2 in text:
reset_pci_usb_device(device['path'])
print('Failed to find device!')
sys.exit(-1)
def reset_usb_device(dev_path):
USBDEVFS_RESET = 21780
try:
f = open(dev_path, 'w', os.O_WRONLY)
fcntl.ioctl(f, USBDEVFS_RESET, 0)
print('Successfully reset %s' % dev_path)
sys.exit(0)
except Exception as ex:
print('Failed to reset device! Error: %s' % ex)
sys.exit(-1)
if 'path' in option:
reset_usb_device(option2)
if 'search' in option:
usb_list = create_usb_list()
for device in usb_list:
text = '%s %s %s' % (device['description'], device['manufacturer'], device['device'])
if option2 in text:
reset_usb_device(device['path'])
print('Failed to find device!')
sys.exit(-1)
edited 26 mins ago
Pablo Bianchi
2,94521535
2,94521535
answered Dec 21 '17 at 10:15
mcaransmcarans
4481311
4481311
add a comment |
add a comment |
Did somebody order a sledgehammer? This is pieced together from various other answers here.
#!/bin/bash
# Root required
if (( UID )); then
exec sudo "$0" "$@"
fi
cd /sys/bus/pci/drivers
function reinit (
local d="$1"
test -e "$d"
for d in ?hci_???; do
echo " - $d"
reinit "$d"
done
Mark, have you found that the unbinding is really necessary or is it here just to be on the safe side?
– ndemou
Nov 10 '16 at 14:27
This is a sledgehammer, it probably does a lot of unnecessary things
– Mark K Cowan
Nov 10 '16 at 14:37
@MarkKCowan , How do you use it? What are the command arguments needed/expected?
– not2qubit
Feb 28 '17 at 7:15
1
@not2qubit: No command-line arguments required. The$@in the sudo proxy is just a force of habbit, having it prevents bugs if I later decide to add arguments (and forget to update the sudo proxy).
– Mark K Cowan
Nov 21 '17 at 14:54
1
@MarkKCowan Doh! Sorry mate! Oh yes of curse! I should not be commenting on this site while sleepy. Upvoted!
– not2qubit
Nov 21 '17 at 16:29
|
show 2 more comments
Did somebody order a sledgehammer? This is pieced together from various other answers here.
#!/bin/bash
# Root required
if (( UID )); then
exec sudo "$0" "$@"
fi
cd /sys/bus/pci/drivers
function reinit (
local d="$1"
test -e "$d"
for d in ?hci_???; do
echo " - $d"
reinit "$d"
done
Mark, have you found that the unbinding is really necessary or is it here just to be on the safe side?
– ndemou
Nov 10 '16 at 14:27
This is a sledgehammer, it probably does a lot of unnecessary things
– Mark K Cowan
Nov 10 '16 at 14:37
@MarkKCowan , How do you use it? What are the command arguments needed/expected?
– not2qubit
Feb 28 '17 at 7:15
1
@not2qubit: No command-line arguments required. The$@in the sudo proxy is just a force of habbit, having it prevents bugs if I later decide to add arguments (and forget to update the sudo proxy).
– Mark K Cowan
Nov 21 '17 at 14:54
1
@MarkKCowan Doh! Sorry mate! Oh yes of curse! I should not be commenting on this site while sleepy. Upvoted!
– not2qubit
Nov 21 '17 at 16:29
|
show 2 more comments
Did somebody order a sledgehammer? This is pieced together from various other answers here.
#!/bin/bash
# Root required
if (( UID )); then
exec sudo "$0" "$@"
fi
cd /sys/bus/pci/drivers
function reinit (
local d="$1"
test -e "$d"
for d in ?hci_???; do
echo " - $d"
reinit "$d"
done
Did somebody order a sledgehammer? This is pieced together from various other answers here.
#!/bin/bash
# Root required
if (( UID )); then
exec sudo "$0" "$@"
fi
cd /sys/bus/pci/drivers
function reinit (
local d="$1"
test -e "$d"
for d in ?hci_???; do
echo " - $d"
reinit "$d"
done
edited Nov 21 '17 at 11:32
derHugo
2,32521531
2,32521531
answered Jun 28 '16 at 14:08
Mark K CowanMark K Cowan
1528
1528
Mark, have you found that the unbinding is really necessary or is it here just to be on the safe side?
– ndemou
Nov 10 '16 at 14:27
This is a sledgehammer, it probably does a lot of unnecessary things
– Mark K Cowan
Nov 10 '16 at 14:37
@MarkKCowan , How do you use it? What are the command arguments needed/expected?
– not2qubit
Feb 28 '17 at 7:15
1
@not2qubit: No command-line arguments required. The$@in the sudo proxy is just a force of habbit, having it prevents bugs if I later decide to add arguments (and forget to update the sudo proxy).
– Mark K Cowan
Nov 21 '17 at 14:54
1
@MarkKCowan Doh! Sorry mate! Oh yes of curse! I should not be commenting on this site while sleepy. Upvoted!
– not2qubit
Nov 21 '17 at 16:29
|
show 2 more comments
Mark, have you found that the unbinding is really necessary or is it here just to be on the safe side?
– ndemou
Nov 10 '16 at 14:27
This is a sledgehammer, it probably does a lot of unnecessary things
– Mark K Cowan
Nov 10 '16 at 14:37
@MarkKCowan , How do you use it? What are the command arguments needed/expected?
– not2qubit
Feb 28 '17 at 7:15
1
@not2qubit: No command-line arguments required. The$@in the sudo proxy is just a force of habbit, having it prevents bugs if I later decide to add arguments (and forget to update the sudo proxy).
– Mark K Cowan
Nov 21 '17 at 14:54
1
@MarkKCowan Doh! Sorry mate! Oh yes of curse! I should not be commenting on this site while sleepy. Upvoted!
– not2qubit
Nov 21 '17 at 16:29
Mark, have you found that the unbinding is really necessary or is it here just to be on the safe side?
– ndemou
Nov 10 '16 at 14:27
Mark, have you found that the unbinding is really necessary or is it here just to be on the safe side?
– ndemou
Nov 10 '16 at 14:27
This is a sledgehammer, it probably does a lot of unnecessary things
– Mark K Cowan
Nov 10 '16 at 14:37
This is a sledgehammer, it probably does a lot of unnecessary things
– Mark K Cowan
Nov 10 '16 at 14:37
@MarkKCowan , How do you use it? What are the command arguments needed/expected?
– not2qubit
Feb 28 '17 at 7:15
@MarkKCowan , How do you use it? What are the command arguments needed/expected?
– not2qubit
Feb 28 '17 at 7:15
1
1
@not2qubit: No command-line arguments required. The
$@ in the sudo proxy is just a force of habbit, having it prevents bugs if I later decide to add arguments (and forget to update the sudo proxy).– Mark K Cowan
Nov 21 '17 at 14:54
@not2qubit: No command-line arguments required. The
$@ in the sudo proxy is just a force of habbit, having it prevents bugs if I later decide to add arguments (and forget to update the sudo proxy).– Mark K Cowan
Nov 21 '17 at 14:54
1
1
@MarkKCowan Doh! Sorry mate! Oh yes of curse! I should not be commenting on this site while sleepy. Upvoted!
– not2qubit
Nov 21 '17 at 16:29
@MarkKCowan Doh! Sorry mate! Oh yes of curse! I should not be commenting on this site while sleepy. Upvoted!
– not2qubit
Nov 21 '17 at 16:29
|
show 2 more comments
Sometimes I want to perform this operation on a particular device, as identified by VID (vendor id) and PID (product id). This is a script I've found useful for this purpose, that uses the nifty libusb library.
First run:
sudo apt-get install libusb-dev
Then, this c++ file's resetDeviceConnection should perform this task, of resetting a device connection as identified by vid and pid.
#include <libusb-1.0/libusb.h>
int resetDeviceConnection(UINT_16 vid, UINT_16 pid)
/*Open libusb*/
int resetStatus = 0;
libusb_context * context;
libusb_init(&context);
libusb_device_handle * dev_handle = libusb_open_device_with_vid_pid(context,vid,pid);
if (dev_handle == NULL)
printf("usb resetting unsuccessful! No matching device found, or error encountered!n");
resetStatus = 1;
else
/*reset the device, if one was found*/
resetStatus = libusb_reset_device(dev_handle);
/*exit libusb*/
libusb_exit(context);
return resetStatus;
(stolen from my personal TIL catalog:
https://github.com/Marviel/TIL/blob/master/unix_tools/Reset_specific_USB_Device.md)
3
Please can you show how this script is run.
– George Udosen
Dec 29 '16 at 14:28
Sure thing, let me update.
– Marviel
Dec 29 '16 at 20:31
1
@Marviel, we're still waiting for an update...
– not2qubit
Feb 28 '17 at 7:18
needs downvote as useless
– Eugen Konkov
Oct 13 '17 at 7:36
add a comment |
Sometimes I want to perform this operation on a particular device, as identified by VID (vendor id) and PID (product id). This is a script I've found useful for this purpose, that uses the nifty libusb library.
First run:
sudo apt-get install libusb-dev
Then, this c++ file's resetDeviceConnection should perform this task, of resetting a device connection as identified by vid and pid.
#include <libusb-1.0/libusb.h>
int resetDeviceConnection(UINT_16 vid, UINT_16 pid)
/*Open libusb*/
int resetStatus = 0;
libusb_context * context;
libusb_init(&context);
libusb_device_handle * dev_handle = libusb_open_device_with_vid_pid(context,vid,pid);
if (dev_handle == NULL)
printf("usb resetting unsuccessful! No matching device found, or error encountered!n");
resetStatus = 1;
else
/*reset the device, if one was found*/
resetStatus = libusb_reset_device(dev_handle);
/*exit libusb*/
libusb_exit(context);
return resetStatus;
(stolen from my personal TIL catalog:
https://github.com/Marviel/TIL/blob/master/unix_tools/Reset_specific_USB_Device.md)
3
Please can you show how this script is run.
– George Udosen
Dec 29 '16 at 14:28
Sure thing, let me update.
– Marviel
Dec 29 '16 at 20:31
1
@Marviel, we're still waiting for an update...
– not2qubit
Feb 28 '17 at 7:18
needs downvote as useless
– Eugen Konkov
Oct 13 '17 at 7:36
add a comment |
Sometimes I want to perform this operation on a particular device, as identified by VID (vendor id) and PID (product id). This is a script I've found useful for this purpose, that uses the nifty libusb library.
First run:
sudo apt-get install libusb-dev
Then, this c++ file's resetDeviceConnection should perform this task, of resetting a device connection as identified by vid and pid.
#include <libusb-1.0/libusb.h>
int resetDeviceConnection(UINT_16 vid, UINT_16 pid)
/*Open libusb*/
int resetStatus = 0;
libusb_context * context;
libusb_init(&context);
libusb_device_handle * dev_handle = libusb_open_device_with_vid_pid(context,vid,pid);
if (dev_handle == NULL)
printf("usb resetting unsuccessful! No matching device found, or error encountered!n");
resetStatus = 1;
else
/*reset the device, if one was found*/
resetStatus = libusb_reset_device(dev_handle);
/*exit libusb*/
libusb_exit(context);
return resetStatus;
(stolen from my personal TIL catalog:
https://github.com/Marviel/TIL/blob/master/unix_tools/Reset_specific_USB_Device.md)
Sometimes I want to perform this operation on a particular device, as identified by VID (vendor id) and PID (product id). This is a script I've found useful for this purpose, that uses the nifty libusb library.
First run:
sudo apt-get install libusb-dev
Then, this c++ file's resetDeviceConnection should perform this task, of resetting a device connection as identified by vid and pid.
#include <libusb-1.0/libusb.h>
int resetDeviceConnection(UINT_16 vid, UINT_16 pid)
/*Open libusb*/
int resetStatus = 0;
libusb_context * context;
libusb_init(&context);
libusb_device_handle * dev_handle = libusb_open_device_with_vid_pid(context,vid,pid);
if (dev_handle == NULL)
printf("usb resetting unsuccessful! No matching device found, or error encountered!n");
resetStatus = 1;
else
/*reset the device, if one was found*/
resetStatus = libusb_reset_device(dev_handle);
/*exit libusb*/
libusb_exit(context);
return resetStatus;
(stolen from my personal TIL catalog:
https://github.com/Marviel/TIL/blob/master/unix_tools/Reset_specific_USB_Device.md)
edited Nov 21 '17 at 11:32
derHugo
2,32521531
2,32521531
answered Dec 29 '16 at 13:53
MarvielMarviel
111
111
3
Please can you show how this script is run.
– George Udosen
Dec 29 '16 at 14:28
Sure thing, let me update.
– Marviel
Dec 29 '16 at 20:31
1
@Marviel, we're still waiting for an update...
– not2qubit
Feb 28 '17 at 7:18
needs downvote as useless
– Eugen Konkov
Oct 13 '17 at 7:36
add a comment |
3
Please can you show how this script is run.
– George Udosen
Dec 29 '16 at 14:28
Sure thing, let me update.
– Marviel
Dec 29 '16 at 20:31
1
@Marviel, we're still waiting for an update...
– not2qubit
Feb 28 '17 at 7:18
needs downvote as useless
– Eugen Konkov
Oct 13 '17 at 7:36
3
3
Please can you show how this script is run.
– George Udosen
Dec 29 '16 at 14:28
Please can you show how this script is run.
– George Udosen
Dec 29 '16 at 14:28
Sure thing, let me update.
– Marviel
Dec 29 '16 at 20:31
Sure thing, let me update.
– Marviel
Dec 29 '16 at 20:31
1
1
@Marviel, we're still waiting for an update...
– not2qubit
Feb 28 '17 at 7:18
@Marviel, we're still waiting for an update...
– not2qubit
Feb 28 '17 at 7:18
needs downvote as useless
– Eugen Konkov
Oct 13 '17 at 7:36
needs downvote as useless
– Eugen Konkov
Oct 13 '17 at 7:36
add a comment |
Perhaps this works for a camera, too:
Following revived a starved USB 3.0 HDD on a 3.4.42 (kernel.org) Linux on my side. dmesg told, that it was timing out commands after 360s (sorry, I cannot copy the syslog here, not connected networks) and the drive hung completely. Processes accessing the device were blocked in the kernel, unkillable. NFS hung, ZFS hung, dd hung.
After doing this, everything worked again. dmesg told just a single line about the USB device found.
I really have no idea what following does in detail. But it worked.
The following example output is from Debian Squeeze with 2.6.32-5-686 kernel, so I think it works for 2.6 and above:
$ ls -al /dev/sdb
brw-rw---T 1 root floppy 8, 16 Jun 3 20:24 /dev/sdb
$ ls -al /sys/dev/block/8:16/device/rescan
--w------- 1 root root 4096 Jun 6 01:46 /sys/dev/block/8:16/device/rescan
$ echo 1 > /sys/dev/block/8:16/device/rescan
If this does not work, perhaps somebody else can figure out how to send a real reset to a device.
add a comment |
Perhaps this works for a camera, too:
Following revived a starved USB 3.0 HDD on a 3.4.42 (kernel.org) Linux on my side. dmesg told, that it was timing out commands after 360s (sorry, I cannot copy the syslog here, not connected networks) and the drive hung completely. Processes accessing the device were blocked in the kernel, unkillable. NFS hung, ZFS hung, dd hung.
After doing this, everything worked again. dmesg told just a single line about the USB device found.
I really have no idea what following does in detail. But it worked.
The following example output is from Debian Squeeze with 2.6.32-5-686 kernel, so I think it works for 2.6 and above:
$ ls -al /dev/sdb
brw-rw---T 1 root floppy 8, 16 Jun 3 20:24 /dev/sdb
$ ls -al /sys/dev/block/8:16/device/rescan
--w------- 1 root root 4096 Jun 6 01:46 /sys/dev/block/8:16/device/rescan
$ echo 1 > /sys/dev/block/8:16/device/rescan
If this does not work, perhaps somebody else can figure out how to send a real reset to a device.
add a comment |
Perhaps this works for a camera, too:
Following revived a starved USB 3.0 HDD on a 3.4.42 (kernel.org) Linux on my side. dmesg told, that it was timing out commands after 360s (sorry, I cannot copy the syslog here, not connected networks) and the drive hung completely. Processes accessing the device were blocked in the kernel, unkillable. NFS hung, ZFS hung, dd hung.
After doing this, everything worked again. dmesg told just a single line about the USB device found.
I really have no idea what following does in detail. But it worked.
The following example output is from Debian Squeeze with 2.6.32-5-686 kernel, so I think it works for 2.6 and above:
$ ls -al /dev/sdb
brw-rw---T 1 root floppy 8, 16 Jun 3 20:24 /dev/sdb
$ ls -al /sys/dev/block/8:16/device/rescan
--w------- 1 root root 4096 Jun 6 01:46 /sys/dev/block/8:16/device/rescan
$ echo 1 > /sys/dev/block/8:16/device/rescan
If this does not work, perhaps somebody else can figure out how to send a real reset to a device.
Perhaps this works for a camera, too:
Following revived a starved USB 3.0 HDD on a 3.4.42 (kernel.org) Linux on my side. dmesg told, that it was timing out commands after 360s (sorry, I cannot copy the syslog here, not connected networks) and the drive hung completely. Processes accessing the device were blocked in the kernel, unkillable. NFS hung, ZFS hung, dd hung.
After doing this, everything worked again. dmesg told just a single line about the USB device found.
I really have no idea what following does in detail. But it worked.
The following example output is from Debian Squeeze with 2.6.32-5-686 kernel, so I think it works for 2.6 and above:
$ ls -al /dev/sdb
brw-rw---T 1 root floppy 8, 16 Jun 3 20:24 /dev/sdb
$ ls -al /sys/dev/block/8:16/device/rescan
--w------- 1 root root 4096 Jun 6 01:46 /sys/dev/block/8:16/device/rescan
$ echo 1 > /sys/dev/block/8:16/device/rescan
If this does not work, perhaps somebody else can figure out how to send a real reset to a device.
answered Jun 6 '13 at 0:08
TinoTino
44147
44147
add a comment |
add a comment |
Try this, it's a software unplug (Eject).
Sometimes doesn't work simply unbind device for some devices.
Example:
I want to remove or eject my "Genius NetScroll 120".
Then i first Check my attached usb device
$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 003: ID 03f0:231d Hewlett-Packard
Bus 001 Device 004: ID 138a:0007 Validity Sensors, Inc. VFS451 Fingerprint Reader
Bus 001 Device 005: ID 04f2:b163 Chicony Electronics Co., Ltd
Bus 002 Device 009: ID 0458:003a KYE Systems Corp. (Mouse Systems) NetScroll+ Mini Traveler / Genius NetScroll 120 **<----This my Mouse! XDDD**
Ok, i found my mouse, it's has a Bus 002, Device 009, idVendor 0458 and idProduct 003a, so this is a reference device info about the mouse.
This is important, the Bus number is the begin name path to device and i will check the product Id and Vendor to ensure the correct device to remove.
$ ls /sys/bus/usb/drivers/usb/
1-1/ 1-1.1/ 1-1.3/ 1-1.5/ 2-1/ 2-1.3/ bind uevent unbind usb1/ usb2/
Pay atention on the folders, check the begining with folder number 2, i will check this one because my Bus is 002, and one by one i have check each folder containing the correct idVendor and idProduct about my mouse info.
In this case, i will retrieve the info with this command:
cat /sys/bus/usb/drivers/usb/2-1.3/idVendor
0458
cat /sys/bus/usb/drivers/usb/2-1.3/idProduct
003a
Ok, the path /sys/bus/usb/drivers/usb/2-1.3/ match with my info mouse! XDDD.
It's time to remove the device!
su -c "echo 1 > /sys/bus/usb/drivers/usb/2-1.3/remove"
Plug again the usb device and it's work again!
9
What if you can't plug it in again? (for example it's an internal sdcard reader)
– aleb
Jun 29 '14 at 20:57
add a comment |
Try this, it's a software unplug (Eject).
Sometimes doesn't work simply unbind device for some devices.
Example:
I want to remove or eject my "Genius NetScroll 120".
Then i first Check my attached usb device
$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 003: ID 03f0:231d Hewlett-Packard
Bus 001 Device 004: ID 138a:0007 Validity Sensors, Inc. VFS451 Fingerprint Reader
Bus 001 Device 005: ID 04f2:b163 Chicony Electronics Co., Ltd
Bus 002 Device 009: ID 0458:003a KYE Systems Corp. (Mouse Systems) NetScroll+ Mini Traveler / Genius NetScroll 120 **<----This my Mouse! XDDD**
Ok, i found my mouse, it's has a Bus 002, Device 009, idVendor 0458 and idProduct 003a, so this is a reference device info about the mouse.
This is important, the Bus number is the begin name path to device and i will check the product Id and Vendor to ensure the correct device to remove.
$ ls /sys/bus/usb/drivers/usb/
1-1/ 1-1.1/ 1-1.3/ 1-1.5/ 2-1/ 2-1.3/ bind uevent unbind usb1/ usb2/
Pay atention on the folders, check the begining with folder number 2, i will check this one because my Bus is 002, and one by one i have check each folder containing the correct idVendor and idProduct about my mouse info.
In this case, i will retrieve the info with this command:
cat /sys/bus/usb/drivers/usb/2-1.3/idVendor
0458
cat /sys/bus/usb/drivers/usb/2-1.3/idProduct
003a
Ok, the path /sys/bus/usb/drivers/usb/2-1.3/ match with my info mouse! XDDD.
It's time to remove the device!
su -c "echo 1 > /sys/bus/usb/drivers/usb/2-1.3/remove"
Plug again the usb device and it's work again!
9
What if you can't plug it in again? (for example it's an internal sdcard reader)
– aleb
Jun 29 '14 at 20:57
add a comment |
Try this, it's a software unplug (Eject).
Sometimes doesn't work simply unbind device for some devices.
Example:
I want to remove or eject my "Genius NetScroll 120".
Then i first Check my attached usb device
$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 003: ID 03f0:231d Hewlett-Packard
Bus 001 Device 004: ID 138a:0007 Validity Sensors, Inc. VFS451 Fingerprint Reader
Bus 001 Device 005: ID 04f2:b163 Chicony Electronics Co., Ltd
Bus 002 Device 009: ID 0458:003a KYE Systems Corp. (Mouse Systems) NetScroll+ Mini Traveler / Genius NetScroll 120 **<----This my Mouse! XDDD**
Ok, i found my mouse, it's has a Bus 002, Device 009, idVendor 0458 and idProduct 003a, so this is a reference device info about the mouse.
This is important, the Bus number is the begin name path to device and i will check the product Id and Vendor to ensure the correct device to remove.
$ ls /sys/bus/usb/drivers/usb/
1-1/ 1-1.1/ 1-1.3/ 1-1.5/ 2-1/ 2-1.3/ bind uevent unbind usb1/ usb2/
Pay atention on the folders, check the begining with folder number 2, i will check this one because my Bus is 002, and one by one i have check each folder containing the correct idVendor and idProduct about my mouse info.
In this case, i will retrieve the info with this command:
cat /sys/bus/usb/drivers/usb/2-1.3/idVendor
0458
cat /sys/bus/usb/drivers/usb/2-1.3/idProduct
003a
Ok, the path /sys/bus/usb/drivers/usb/2-1.3/ match with my info mouse! XDDD.
It's time to remove the device!
su -c "echo 1 > /sys/bus/usb/drivers/usb/2-1.3/remove"
Plug again the usb device and it's work again!
Try this, it's a software unplug (Eject).
Sometimes doesn't work simply unbind device for some devices.
Example:
I want to remove or eject my "Genius NetScroll 120".
Then i first Check my attached usb device
$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 003: ID 03f0:231d Hewlett-Packard
Bus 001 Device 004: ID 138a:0007 Validity Sensors, Inc. VFS451 Fingerprint Reader
Bus 001 Device 005: ID 04f2:b163 Chicony Electronics Co., Ltd
Bus 002 Device 009: ID 0458:003a KYE Systems Corp. (Mouse Systems) NetScroll+ Mini Traveler / Genius NetScroll 120 **<----This my Mouse! XDDD**
Ok, i found my mouse, it's has a Bus 002, Device 009, idVendor 0458 and idProduct 003a, so this is a reference device info about the mouse.
This is important, the Bus number is the begin name path to device and i will check the product Id and Vendor to ensure the correct device to remove.
$ ls /sys/bus/usb/drivers/usb/
1-1/ 1-1.1/ 1-1.3/ 1-1.5/ 2-1/ 2-1.3/ bind uevent unbind usb1/ usb2/
Pay atention on the folders, check the begining with folder number 2, i will check this one because my Bus is 002, and one by one i have check each folder containing the correct idVendor and idProduct about my mouse info.
In this case, i will retrieve the info with this command:
cat /sys/bus/usb/drivers/usb/2-1.3/idVendor
0458
cat /sys/bus/usb/drivers/usb/2-1.3/idProduct
003a
Ok, the path /sys/bus/usb/drivers/usb/2-1.3/ match with my info mouse! XDDD.
It's time to remove the device!
su -c "echo 1 > /sys/bus/usb/drivers/usb/2-1.3/remove"
Plug again the usb device and it's work again!
answered Jan 31 '14 at 11:15
user242078user242078
1
1
9
What if you can't plug it in again? (for example it's an internal sdcard reader)
– aleb
Jun 29 '14 at 20:57
add a comment |
9
What if you can't plug it in again? (for example it's an internal sdcard reader)
– aleb
Jun 29 '14 at 20:57
9
9
What if you can't plug it in again? (for example it's an internal sdcard reader)
– aleb
Jun 29 '14 at 20:57
What if you can't plug it in again? (for example it's an internal sdcard reader)
– aleb
Jun 29 '14 at 20:57
add a comment |
If you know your device name, this python script will work:
#!/usr/bin/python
"""
USB Reset
Call as "usbreset.py <device_file_path>"
With device_file_path like "/dev/bus/usb/bus_number/device_number"
"""
import fcntl, sys, os
USBDEVFS_RESET = ord('U') << (4*2) | 20
def main():
fd = os.open(sys.argv[1], os.O_WRONLY)
if fd < 0: sys.exit(1)
fcntl.ioctl(fd, USBDEVFS_RESET, 0)
os.close(fd)
sys.exit(0)
# end main
if __name__ == '__main__':
main()
add a comment |
If you know your device name, this python script will work:
#!/usr/bin/python
"""
USB Reset
Call as "usbreset.py <device_file_path>"
With device_file_path like "/dev/bus/usb/bus_number/device_number"
"""
import fcntl, sys, os
USBDEVFS_RESET = ord('U') << (4*2) | 20
def main():
fd = os.open(sys.argv[1], os.O_WRONLY)
if fd < 0: sys.exit(1)
fcntl.ioctl(fd, USBDEVFS_RESET, 0)
os.close(fd)
sys.exit(0)
# end main
if __name__ == '__main__':
main()
add a comment |
If you know your device name, this python script will work:
#!/usr/bin/python
"""
USB Reset
Call as "usbreset.py <device_file_path>"
With device_file_path like "/dev/bus/usb/bus_number/device_number"
"""
import fcntl, sys, os
USBDEVFS_RESET = ord('U') << (4*2) | 20
def main():
fd = os.open(sys.argv[1], os.O_WRONLY)
if fd < 0: sys.exit(1)
fcntl.ioctl(fd, USBDEVFS_RESET, 0)
os.close(fd)
sys.exit(0)
# end main
if __name__ == '__main__':
main()
If you know your device name, this python script will work:
#!/usr/bin/python
"""
USB Reset
Call as "usbreset.py <device_file_path>"
With device_file_path like "/dev/bus/usb/bus_number/device_number"
"""
import fcntl, sys, os
USBDEVFS_RESET = ord('U') << (4*2) | 20
def main():
fd = os.open(sys.argv[1], os.O_WRONLY)
if fd < 0: sys.exit(1)
fcntl.ioctl(fd, USBDEVFS_RESET, 0)
os.close(fd)
sys.exit(0)
# end main
if __name__ == '__main__':
main()
edited Nov 21 '17 at 11:42
derHugo
2,32521531
2,32521531
answered Aug 4 '17 at 14:35
ClayClay
1011
1011
add a comment |
add a comment |
i made a simple bash script for reset particular USB device.
#!/bin/bash
#type lsusb to find "vendor" and "product" ID in terminal
set -euo pipefail
IFS=$'nt'
#edit the below two lines of vendor and product values using lsusb result
dev=$(lsusb -t | grep usbdevicename | grep 'If 1' | cut -d' ' -f13|cut -d"," -f1)
#VENDOR=05a3
#PRODUCT=9230
VENDOR=$(lsusb -s $dev | cut -d' ' -f6 | cut -d: -f1)
PRODUCT=$(lsusb -s $dev | cut -d' ' -f6 | cut -d: -f2)
for DIR in $(find /sys/bus/usb/devices/ -maxdepth 1 -type l); do
if [[ -f $DIR/idVendor && -f $DIR/idProduct &&
$(cat $DIR/idVendor) == $VENDOR && $(cat $DIR/idProduct) == $PRODUCT ]]; then
echo 0 > $DIR/authorized
sleep 0.5
echo 1 > $DIR/authorized
fi
done
add a comment |
i made a simple bash script for reset particular USB device.
#!/bin/bash
#type lsusb to find "vendor" and "product" ID in terminal
set -euo pipefail
IFS=$'nt'
#edit the below two lines of vendor and product values using lsusb result
dev=$(lsusb -t | grep usbdevicename | grep 'If 1' | cut -d' ' -f13|cut -d"," -f1)
#VENDOR=05a3
#PRODUCT=9230
VENDOR=$(lsusb -s $dev | cut -d' ' -f6 | cut -d: -f1)
PRODUCT=$(lsusb -s $dev | cut -d' ' -f6 | cut -d: -f2)
for DIR in $(find /sys/bus/usb/devices/ -maxdepth 1 -type l); do
if [[ -f $DIR/idVendor && -f $DIR/idProduct &&
$(cat $DIR/idVendor) == $VENDOR && $(cat $DIR/idProduct) == $PRODUCT ]]; then
echo 0 > $DIR/authorized
sleep 0.5
echo 1 > $DIR/authorized
fi
done
add a comment |
i made a simple bash script for reset particular USB device.
#!/bin/bash
#type lsusb to find "vendor" and "product" ID in terminal
set -euo pipefail
IFS=$'nt'
#edit the below two lines of vendor and product values using lsusb result
dev=$(lsusb -t | grep usbdevicename | grep 'If 1' | cut -d' ' -f13|cut -d"," -f1)
#VENDOR=05a3
#PRODUCT=9230
VENDOR=$(lsusb -s $dev | cut -d' ' -f6 | cut -d: -f1)
PRODUCT=$(lsusb -s $dev | cut -d' ' -f6 | cut -d: -f2)
for DIR in $(find /sys/bus/usb/devices/ -maxdepth 1 -type l); do
if [[ -f $DIR/idVendor && -f $DIR/idProduct &&
$(cat $DIR/idVendor) == $VENDOR && $(cat $DIR/idProduct) == $PRODUCT ]]; then
echo 0 > $DIR/authorized
sleep 0.5
echo 1 > $DIR/authorized
fi
done
i made a simple bash script for reset particular USB device.
#!/bin/bash
#type lsusb to find "vendor" and "product" ID in terminal
set -euo pipefail
IFS=$'nt'
#edit the below two lines of vendor and product values using lsusb result
dev=$(lsusb -t | grep usbdevicename | grep 'If 1' | cut -d' ' -f13|cut -d"," -f1)
#VENDOR=05a3
#PRODUCT=9230
VENDOR=$(lsusb -s $dev | cut -d' ' -f6 | cut -d: -f1)
PRODUCT=$(lsusb -s $dev | cut -d' ' -f6 | cut -d: -f2)
for DIR in $(find /sys/bus/usb/devices/ -maxdepth 1 -type l); do
if [[ -f $DIR/idVendor && -f $DIR/idProduct &&
$(cat $DIR/idVendor) == $VENDOR && $(cat $DIR/idProduct) == $PRODUCT ]]; then
echo 0 > $DIR/authorized
sleep 0.5
echo 1 > $DIR/authorized
fi
done
edited Jan 21 at 12:26
Olorin
2,656924
2,656924
answered Jan 21 at 11:58
ThohtThoht
63
63
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f645%2fhow-do-you-reset-a-usb-device-from-the-command-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Which version of Ubuntu are you using?
– User
Aug 1 '10 at 20:15
i tried both solutions by Li Lo and ssokolow, all i get is permission denied, nomatter if i use the usbreset code or the command line "echo 0 > ..." i use sudo, also my usb devices are owned by root but i can use them without admin rights(cameras..)
– user290672
Jun 8 '14 at 16:40
1
If you are getting read errors, you might have some data corruption. If your camera uses an external memory card (such as MicroSD), it might be wise to connect it to the computer and run fsck.
– TSJNachos117
Jun 8 '14 at 18:47