Nav links

Wednesday, 11 August 2010

USB mouse erratic after resuming from sleep in Windows 7

Since I installed Windows 7 shortly after its release I have been having intermittent problems with my old Microsoft Intellimouse Explorer 3. Occasionally after the PC resumed from sleep mode the mouse pointer would move very slowly. My temporary fix was to unplug and replug the mouse into the USB port, which would fix the problem. Sending the PC to sleep and resuming again would also sometimes cure it.

I had hoped that I would magically find the problem fixed when I built a new PC last week. However, the problem remained, leading me to think it was caused by the combination of my mouse and Windows 7, as it hadn't been a problem for many years in Windows XP. A bit of Googling found other people with the same problem - Mouse stops responding when pc resumes from sleep. However, their solutions did not work for me.

My next step was to look for a software equivalent of the pulling out and inserting of the USB plug. I found this in the Device Manager. Go through all the USB Root Hubs, checking the Power tab in each to the one containing my USB device. At this stage the Advanced | Reset Hub button does not fix it. However, Driver | Disable then Enable does fix it. This means a workaround could be to write a script that does this action, triggered by the PC resuming. There is a trigger option in the Task Scheduler.

Googling further, I found a Microsoft page, A USB device may no longer work correctly after Windows Vista resumes from sleep or from hibernation, describing a change made in Windows Vista to how USB devices were treated after resuming. In summary, instead of devices being forcibly reset they are pleasantly asked to resume. Older, or perhaps defective devices, might not cope with this, so Microsoft include a hub by hub method of setting this back to the old method using a registry edit, by adding the ForceHCResetOnResume registry value for the relevant USB host controller. I made this change a couple of days ago, and it seems to have cured the problem.

Update: a few days later

The problem was not cured by the registry change, so I had a go at scripting the Reset Hub option in the Device Manager. However, this proved excessively tricky, even before I got to the hurdle of escalating privileges to administrator. My WSH script would open the device manager using "mmc devmgmt.msc", but I couldn't then activate that window to pass keypresses to.

I then learnt about devcon.exe, a Microsoft command-line alternative to the Device Manager. The previously linked version of devcon does not work in Windows 7. Instead we need to grab it from the 650MB Windows Driver Kit. Luckily you don't need to install the whole thing, but can extract just the small file we need. I used IZArc to extract \WDK\setuptools_x64fre.msi from the ISO, then Univeral Extractor to get devcon.exe from the MSI.

To run devcon.exe you need to run a command prompt as administrator. Then something like devcon.exe find * will list all devices, and I found the relevant USB hub by comparing with the values in the GUI Device Manager.

The necessary command was then devcon.exe restart "@USB\ROOT_HUB\4&2E16C4AF&0"

The final step was to get this command to run automatically when my PC resumed from standby. The relevant trigger in the Task Scheduler was Log: System; Source: Power-TroubleShooter; Event ID: 1. This task was set to "Run with the highest privileges".

I guess this post should have been called "How to reset a USB hub automatically upon resume from sleep in Windows 7".

In hindsight, a new mouse might have been a more straightforward option!