Menu
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. IDS Imaging Development Systems GmbH is a leading manufacturer of digital industrial cameras: USB 2.0 cameras, USB 3.0 cameras, GigE cameras, Stereo 3D cameras, CMOS cameras IDS also offers a lot of accessory for machine vision cameras and Frame Grabbers.
From what I can tell, there are no kernel modules being loaded for the camera. The only one that looks related is
usbhid
.cmcgintycmcginty
16 Answers
Save the following as
usbreset.c
The run the following commands in terminal:
- Compile the program:
- Get the Bus and Device ID of the USB device you want to reset:
- Make our compiled program executable:
- Execute the program with sudo privilege; make necessary substitution for
<Bus>
and<Device>
ids as found by running thelsusb
command:
Source of above program: http://marc.info/?l=linux-usb&m=121459435621262&w=2
Li LoLi Lo
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)
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:ssokolowssokolow
This will reset all of USB1/2/3 attached ports[1]:
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))Tamás TapsonyiTamás Tapsonyi
I needed to automate this in a python script, so I adapted LiLo's extremely helpful answer to the following:
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:This might also be helpful if you don't already have a c compiler setup on your system, but you do have python.
PeterPeter
I'm using kind of sledgehammer by reloading the modules.This is my usb_reset.sh script:
And this is my systemd service file /usr/lib/systemd/system/usbreset.service which runs usb_reset.sh after my diplay manager has started:
Ulrich-Lorenz SchlüterUlrich-Lorenz Schlüter
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:
Maybe this option didn't exist in 2010 when the question was asked.
mviereckmviereck
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:
Script:
mcaransmcarans
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.
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.
chandankchandank
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:
In this string 004 is the device number
RaghuRaghu
Here is script that will only reset a matching product/vendor ID.
cmcgintycmcginty
Did somebody order a sledgehammer? This is pieced together from various other answers here.
Mark K CowanMark K Cowan
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:
Then, this c++ file's resetDeviceConnection should perform this task, of resetting a device connection as identified by vid and pid.
(stolen from my personal TIL catalog:https://github.com/Marviel/TIL/blob/master/unix_tools/Reset_specific_USB_Device.md)
MarvielMarviel
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:If this does not work, perhaps somebody else can figure out how to send a real reset to a device.
TinoTino
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
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.
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:
Ok, the path /sys/bus/usb/drivers/usb/2-1.3/ match with my info mouse! XDDD.
It's time to remove the device!
Plug again the usb device and it's work again!
user242078user242078
If you know your device name, this python script will work:
ClayClay
i made a simple bash script for reset particular USB device.
ThohtThoht
protected by Community♦Mar 20 at 14:53
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?
Not the answer you're looking for? Browse other questions tagged command-lineusb or ask your own question.
I am a newbie learning how to write WDM device drivers for USB devices and found that the materials available are all too hard to comprehend (the DDK online doc is one of the most difficult to read, and the WDM Device driver book by Oney isn't anything better).
So, I've got a simple question. Where do I start if I want to create a virtual USB device (for example, a virtual USB mouse which looks like a real USB mouse attached to a USB port) for testing/learning.
So far what I understand is the HIDClass driver (hidclass.sys) has a minidriver for the usb bus (hidusb.sys) that carries out the enumeration of attached USB hardware. So, if I want to hijack the hardware enumeration process and creates my own virtual hardware, should I include a filter driver somewhere to intercept some IRPs related to the hardware enumeration process?
Sorry if the above does not make sense at all since I am still in the learning stage and this is actually one of the exercise I think could help me learn about writing USB device drivers better.
JavaManJavaMan
4 Answers
Windows uses a Plug and Play Architecture. When you insert a USB device, It sends low level USB request to the device and then based on the response from a device decides what driver to load. Matching is done by comparing vendor id, product id and etc to inf files sections. Drivers come in the form of a compiled xxx.sys with xxx.inf file and is loaded to kernel space. Windows decides which xxx.sys to load based on the *.inf file that comes with the device's driver.
These files have sections like this:
(a more detailed description on what's in
inf
files can be found over on https://docs.microsoft.com/en-us/windows-hardware/drivers/install/inf-manufacturer-section)A detailed look at the USB enumeration process (Use USB Logger):
- USB Device Plugged
- USB Bus Driver Request
- GetDescriptor(Device)
- GetDescriptor(Configuration)
- GetDescriptor(String iSerialNumber), used as Device Instance ID
- GetDescriptor(String iProduct), used in the 'new Hardware been identified' popups
- The PNP (Plug and Play) manager is informed that a device was added by the bus drivers.
- The PNP manager then asks the bus driver for device information by using a PNP request, asking for:
- DeviceID string, representing the USB Vendor and Product ID,
- HardwareIDs string,
- CompatibleIDs string, representing USB device' Interface Class, Subclass and Protocol, and
- InstanceID string, representing the uid for this particular device within the set of all instances with the same compatible id hooked up to the computer.
For any connected USB device you can see these strings using the Device Manager:
- Open the Device Manager (windows menu -> 'device manager', or control panel -> 'System' -> 'Hardware' -> 'Device Manager')
- then use the 'view' menu to switch to 'Device by Connection'
- open 'ACPI [...]' -> 'PCI bus'/'PCI Express Root Complex' -> '[...] USB [...] Host Controller'
- expand any of the entries under the host controller, and for any of the devices listed, right click to get their properties, open the 'details' tab, and then use the property pulldown menu to find 'Hardware Ids', 'Compatible Ids', 'Device Instance ID', 'Matching Device Id', 'Service', etc.
For example, I have a USB storage device with
Device Id = usbclass_08&subclass_06&prot_50
hooked up, and this string can be matched to an .inf
file that was added to the list of known devices after first enumeration. This file has a string Service = USBSTOR
, and so we know that usbstor.sys
is used to interface with this USB Mass Storage Device.Let's continue with matching process.
- The PNP Manager tries to determine whether Device was already 'installed':
- It search the registry for a key matching the 'DeviceInstance ID' to see which service handles interfacing with this device. Specifically, it searches for this in
HKEY_LOCAL_MACHINESYSTEMCurrentControlSetEnumUSB
- It search the registry for a key matching the 'DeviceInstance ID' to see which service handles interfacing with this device. Specifically, it searches for this in
For disk on key, you can see something like:
- The PNP Manager then loads the associated driver based on a match between the strings in PNP requests and data from the .inf database:
- inf database located under: C:WINDOWSinf
- drivers .sys files located: C:WINDOWSsystem32drivers
- If PNP can't find matching string, you will get prompt to show a path to xxx.sys and xxx.inf
For writing drivers my advice is:
- Don't start with implementing HID (human interface device) devices, because you can cause windows to use your custom driver for you mouse or keyboard instead of original driver, this will disable your mouse or keyboard, very dangerous.
- Don't load drivers into your dev machine:
- use a virtual machine and install your drivers there. Set up a kernel debugger for your virtual machine: http://www.codeproject.com/KB/winsdk/KernelModeDebuggerSetup.asp
- or load drivers on other test machine.
- Good learning platform for USB drivers is 'OSR USB-FX2 Learning Kit'
Alex.SalnikovAlex.Salnikov
Use Device Simulation Framework (DSF).
Amir SaniyanAmir Saniyan
You can use the USB/IP project to emulate any device that you want. In my blog I demonstrated how to emulate USB Mouse device in python using the USB/IP project:http://breaking-the-system.blogspot.com/2014/08/emulating-usb-devices-in-python-with-no.html
It wont help you to understand how to create the virtual USB device (the process is done in the USB/IP driver, you could read the code), but it will create the virtual USB HID device and you could play with the HID arguments sent to the USB driver.
Yaron ShaniYaron Shani
Wouldn't it make more sense to provide your own bus type and enumerator?
Simon RichterSimon Richter