KMDF-Based USB Device Driver Hangs and Cannot Be Unloaded After Surprise Removal or System Suspend and Resume

Article translations Article translations
Close Close
Article ID: 2516416 - View products that this article applies to.
Expand all | Collapse all

SYMPTOMS

A USB device driver is based on the Microsoft Windows Kernel Mode Driver Framework (KMDF) may hang and be unable to be unloaded and reloaded after the USB device is surprise-removed.

CAUSE

This problem may occur if the USB device driver has all of the following characteristics:
  • The USB device driver is based on the Microsoft Windows Kernel Mode Driver Framework (KMDF) version 1.9 or earlier.
  • The USB device driver implements a USB Continuous Reader to perform Read (IN) transfers from a Bulk IN or Interrupt IN pipe on the device.
  • The USB device driver's D0Exit handler calls WdfIoTargetStop with a handle to the USB Device to stop all pending transfers for the device, without first calling WdfIoTargetStop with a handle to each USB Pipe belonging to the USB Device to stop all pending transfers for each of the device's USB pipes.
The problem may be more likely to occur, or occur more frequently, if the following condition is also true:
  • The USB device driver does not implement an EvtReadersFailed callback routine for the USB Continuous Reader, or implements an EvtReadersFailed callback routine that returns TRUE for transfers that fail due to errors associated with the device being surprised removed such as STATUS_NO_SUCH_DEVICE or other unrecoverable errors.

Under this configuration, if the USB device is surprise-removed, USB transfers being performed by the USB Continuous Reader will complete with an error such as STATUS_NO_SUCH_DEVICE. If an EvtReadersFailed callback routine is not registered for the USB Continuous Reader, the failed transfers are retried continuously.

Meanwhile, the USB device driver's D0Exit handler may be called to handle the device surprise removal, and call WdfIoTargetStop for the USB Device to stop all pending transfers. If the USB device driver's D0Exit handler does not first call WdfIoTargetStop with a handle to each USB Pipe belonging to the USB Device to stop all pending transfers for each of the device's USB pipes, a deadlock may occur which prevents Windows from completing removal of the device and unloading the device's driver.

In this scenario, there are two bugs in the Microsoft KMDF framework that can cause a WdfIoTargetStop operation for a USB Device to hang while attempting to cancel pending transfers for a USB Continuous Reader:
  1. In preparation for cancelling pending USB transfers, the Microsoft KMDF framework issues a USB Get Port Status request to get the device's connection status, but does not set the WDF_REQUEST_SEND_OPTION_IGNORE_TARGET_STATE option. This may cause such requests to block indefinitely if the device is disconnected.
  2. The Microsoft KMDF framework's implementation of the WdfIoTargetStop method for a USB Device does not wait for pending transfers to complete for a Continuous Reader pipe. This may cause deadlocks between multiple threads performing actions for this device.

RESOLUTION

A KMDF-Based USB device driver can work around these problems by implementing the following solution:
  • In the USB device driver's D0Exit routine, call WdfIoTargetStop for each of the USB Pipes (including the pipe associated with the USB Continuous Reader) before calling WdfIoTargetStop for the USB Device itself. This will avoid deadlocks with the thread performing the actions related to stopping the USB I/O targets.

This is the correct implementation for stopping a USB IO Target, and is demonstrated in KMDF-based USB device driver samples included in the Windows Driver Kit for Windows 7, including the USBSamp and OSRUSBFX2 samples.

MORE INFORMATION

A fix for this problem is being considered for inclusion in a future version of the Microsoft Windows Kernel Mode Driver Framework (KMDF).

Under some circumstances, it may be possible to reduce the likelihood or frequency of occurrence of this problem by implementing the following functionality in the affected KMDF-Based USB device driver:
  • Register an EvtReadersFailed callback routine for the USB Continuous Reader, and return FALSE from this callback routine on errors such as STATUS_NO_SUCH_DEVICE. This will stop the USB Continuous Reader and prevent further retries of this failed transfer.
While there are benefits to be gained from implementing a EvtReadersFailed callback routine for a USB Continuous Reader, and the above implementation may reduce the likelihood or frequency of occurrence of this problem under some circumstances, this is not a sufficient solution to the above-described problem in all circumstances under which this problem might occur, and is not the Microsoft-recommended solution for this issue.

For more information on Windows driver development details related to this issue, see the following topics in the Microsoft Windows Driver Kit (WDK) documentation:

WdfIoTargetStop Method (WDK)
Working with USB Pipes (WDK)
WdfUsbTargetPipeConfigContinuousReader Method (WDK)
EvtUsbTargetPipeReadersFailed (WDK)
UsbSamp sample (WDK)
OSRUSBFX2 sample (WDK)

Note This is a "FAST PUBLISH" article created directly from within the Microsoft support organization. The information contained herein is provided as-is in response to emerging issues. As a result of the speed in making it available, the materials may include typographical errors and may be revised at any time without notice. See Terms of Use for other considerations.

Properties

Article ID: 2516416 - Last Review: March 18, 2011 - Revision: 1.0
APPLIES TO
  • Windows Driver Kit 7.0
  • Microsoft Windows Driver Kit
  • Windows 7 Ultimate
  • Windows 7 Enterprise
  • Windows 7 Professional
  • Windows 7 Home Premium
  • Windows 7 Home Basic
  • Windows 7 Starter
  • Windows Server 2008 R2 Standard
  • Windows Server 2008 R2 Enterprise
  • Windows Server 2008 R2 Datacenter
  • Windows Vista Ultimate
  • Windows Vista Enterprise
  • Windows Vista Business
  • Windows Vista Home Premium
  • Windows Vista Starter
  • Windows Server 2008 Standard
  • Windows Server 2008 Enterprise
  • Windows Server 2008 Datacenter
  • Microsoft Windows XP Professional
  • Microsoft Windows XP Home Edition
  • Windows XP Embedded
Keywords: 
KB2516416

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com