Linux reload usb device

planetceres / RESET_USB_KERNEL_MODULE.md

Restart USB ports after a power overdraw (error -110). Unplug all devices prior to running script.

for port in $(lspci | grep USB | cut -d' ' -f1); do echo -n "0000:$"| sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind; sleep 5; echo -n "0000:$" | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind; sleep 5; done 
for port in $(lspci | grep xHCI | cut -d' ' -f1); do echo -n "0000:$"| sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind; sleep 5; echo -n "0000:$" | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind; sleep 5; done 

Either my Dell Optiplex 990 computer or my Linux install has an issue: occasionally, when I re-plug a USB device into the system, it’ll cause a fault in the USB module in the kernel and USB goes dark. I’m unsure as to whether this is a hardware or software issue, but I’d simply like to restart my USB subsystem and continue working. When searching the web for ‘restart USB in Linux’ and ‘reload USB kernel module’, you get a plethora of results and none of which will work (seemingly due to how the Ubuntu standard kernel is compiled), at least for me within Ubuntu 12.04, Precise Pangolin. Until now, I’ve had no success and had to hard reset. No longer.

You’ll need root/sudo access to the machine to be able to run commands. In my case, without USB available, then I’ve either got to sprint for a PS/2 keyboard and mouse or login via SSH. You can do what I’ve done and prepared things into a suitable script I can run with just a Gnome launcher. Thanks to this fantastic post for the help. Either place the following into a script or run the commands directly:

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 

The hardware identifiers being passed around here can be revealed using a command like lspci | grep USB. In my case, the identifiers in the original post were exactly what I have in my system.

I’m yet to see if my USB will correctly come back online after freezing up as it hasn’t happened yet, but I’ll try this when it does and report back. That said, the commands above definitely reload all USB devices attached to the system; that much I’ve tried.

Источник

Disconnect and reconnect USB port via cli

Does this effectively reset the power on the device and would therefore completely reset it without having to unplug and replug it in?

If a program has opened a serial USB device(say, /dev/ttyUSB0 symlinked from /dev/myserialdevice as specified in /etc/udev/rules.d/mystuff.rules), and the device gets hung for some reason, is it then necessary to reset it with an ioctl() as above, or is it sufficient to simply close() and open() it again?

@Jarryd see Alan’s explanation in the link above: Note however, that reset followed by re-enumeration is _not_ the same thing as power-cycle followed by reconnect and re-enumeration.

Save the script below as reset_usb.py or clone this repo: https://github.com/mcarans/resetusb/.

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

#!/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_dictLinux reload usb device] = 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 = 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) 

Источник

Читайте также:  Linux скопировать всю папку

Unplug and plug in again a USB device in the terminal

For years, I have been dealing with this Ubuntu bug where my mouse freezes soon after booting and then periodically freezes after that. To fix it, I have to physically unplug the usb transceiver for the mouse and plug it back in. From my research, this bug happens with USB wireless Windows mice when dual booting Linux and Windows. Is there a way I can programmatically (from the terminal) unplug the USB transceiver instead of doing it physically? I want to achieve this in my startup bash script. I'm using Ubuntu 16.04 Edit: I solved my problem but it doesn't really relate to my question. In the file..

/etc/laptop-mode/conf.d/runtime-pm.conf 
CONTROL_RUNTIME_AUTOSUSPEND=0 

If this driver is a kernel module, you should be able to unload it with rmmod and reload it with modprobe . You only have to find out which module loads when you plugin the mouse. Try lsmod after you rebooted without mouse plugged in. Then plug it in and see, which module loaded.

@deanresin Please add the solution you found as an answer below and accept it, this way others can find it easily and vote for it.

4 Answers 4

I wrote a script to show how I’d do that:

#!/bin/bash port="1-1.1" # as shown by lsusb -t: -(.) bind_usb() < echo "$1" >/sys/bus/usb/drivers/usb/bind > unbind_usb() < echo "$1" >/sys/bus/usb/drivers/usb/unbind > unbind_usb "$port" # sleep 1 # enable delay here bind_usb "$port" 

First you need to get the bus and port number of the usb port in question. You can do that with lsusb and any device you recognize in lsusb ’s output, I use a Sandisk pendrive here:

$ lsusb Bus 001 Device 005: ID 04f2:b39a Chicony Electronics Co., Ltd Bus 001 Device 112: ID 8087:07dc Intel Corp. Bus 001 Device 019: ID 04d9:1603 Holtek Semiconductor, Inc. Keyboard Bus 001 Device 018: ID 0424:2504 Standard Microsystems Corp. USB 2.0 Hub Bus 001 Device 022: ID 0781:5567 SanDisk Corp. Cruzer Blade Bus 001 Device 002: ID 8087:8000 Intel Corp. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub $ lsusb -t /: Bus 01.Port 1: Dev 1, Driver=ehci-pci/3p, 480M |__ Port 1: Dev 2, If 0, Driver=hub/8p, 480M |__ Port 1: Dev 22, If 0, Storage, Driver=usb-storage, 480M |__ Port 2: Dev 18, If 0, Driver=hub/4p, 480M |__ Port 1: Dev 19, If 1, Interface Device, Driver=usbhid, 1.5M |__ Port 1: Dev 19, If 0, Interface Device, Driver=usbhid, 1.5M |__ Port 7: Dev 112, If 0, Driver=btusb, 12M |__ Port 7: Dev 112, If 1, Driver=btusb, 12M |__ Port 8: Dev 5, If 1, Driver=uvcvideo, 480M |__ Port 8: Dev 5, If 0, Driver=uvcvideo, 480M

From the output of lsusb you get the bus and device number of the device, then search this device in the output of lsusb -t to get the bus and port number (sometimes with subports). The syntax is:

1-2.3 # for Bus 1 Port 2 Subport 3 – strip leading zeroes! 

Use this as port in the script. Now you just need to make it executable with chmod +x /path/to/script and run it with root permissions:

Читайте также:  Совместимость с процессорами linux

I didn’t need one for my pendrive, but it may be necessary for you to add a delay between unbinding and binding again, that’s what the commented out sleep 1 line is for – you can experiment with the values, e.g. sleep 0.5 for half a second.

Note that this approach shows how to disable and enable again a certain USB port, if you want a specific device to be unbound and rebound again you’ll have to use the same USB port for this to work. One could think of a way to parse lsusb ’s output to dynamically get the bus and port number of a specific device every time the script is called, this would allow you to use any USB port, but I feel that would be an overkill here.

Unfortunately it doesn't work. I still have to physically remove the USB transceiver and plug it back in. Also, @reboot doesn't work on my machine (Ubuntu 16.04)

I have the same problem, ans I don't thing this is equivalent to physically remove the plug, because, when I reconnect the plug, I get a new device number, which does not happen here.

Sorry, I have been busy with something else, and it might be that the problem was due to another peripheral (Also it didn't arise all the time)

I got a similar scenario, where I want to remove an embedded development board in a software way. When I physically unplug and plug the board, I got a new device number (like @Camion said). But with this script, it is always the same number. And of course, it didn't solve my problem.

#!/bin/bash port="usb1" # replace '1' with actual bus number as shown by lsusb -t: -(.) bind_usb() < echo "$1" >/sys/bus/usb/drivers/usb/bind > unbind_usb() < echo "$1" >/sys/bus/usb/drivers/usb/unbind > unbind_usb "$port" # sleep 1 # enable delay here bind_usb "$port" 

Similarly to previous answers, this will reset the hub. If you observe the usb traffic in a program such as wireshark, you'll see that the previous answers don't cause the device to be re-enumerated, but simply restart the device driver. Restarting the hub goes a little deeper in the usb tree of the kernel and forces the device to completely re-enumerate.

Remark, to clarify, if lsusb -t shows Bus 01.Port 1 , then take the number after the Bus ( 01 in this case), strip leading zeroes, and prepend usb to get usb1 . ■ Side note – I have a keyboard whose wiring is a bit fickle, and if the electrical signal gets momentarily interrupted (or so I think) the right half will stop working. Even this one as well as the answer above isn't sufficient (the unbind removes the whole keyboard and the bind reset it to the original situation, but only unplug+replug fixes the issue.

you can also use ioctl to send a reset to the device in question - assuming it's still responsive

 /* usbreset -- send a USB port reset to a USB device */ #include #include #include #include #include #include int main(int argc, char **argv) < const char *filename; int fd; int rc; if (argc != 2) < fprintf(stderr, "Usage: usbreset device-filename\n"); return 1; >filename = argv[1]; fd = open(filename, O_WRONLY); if (fd < 0) < perror("Error opening output file"); return 1; >printf("Resetting USB device %s\n", filename); rc = ioctl(fd, USBDEVFS_RESET, 0); if (rc < 0) < perror("Error in ioctl"); return 1; >printf("Reset successful\n"); close(fd); return 0; > 
sudo ./usbreset /dev/bus/usb/002/004 

not my code, but I don't know who to attribute to

Читайте также:  Linux check usb devices

I've created a Python script that simplifies the whole process.

Save the script below as reset_usb.py or clone this repo.

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 
#!/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_dictLinux reload usb device] = 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 = 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) 

Источник

Оцените статью
Adblock
detector