Overview
The Human Interface Device (HID) class driver provides a framework for creating drivers for keyboards, mice, game controllers, and other input devices that conform to the USB HID specification. The HID stack abstracts low‑level device interactions, allowing developers to focus on device‑specific logic.
Key Features
- Plug‑and‑play support through the Windows Driver Framework (WDF).
- Automatic report parsing and delivery to user‑mode applications.
- Compatibility with both USB and Bluetooth HID devices.
- Extensible data‑exchange mechanisms via custom report descriptors.
Getting Started
Follow these steps to create a basic HID driver using KMDF.
- Install the Windows Driver Kit (WDK) and Visual Studio.
- Create a new
KMDf Driverproject. - Add the
hidclasslibrary reference. - Implement the
EvtDeviceAddcallback to register your device.
#include <ntddk.h>
#include <wdf.h>
#include <hidport.h>
DRIVER_INITIALIZE DriverEntry;
EVT_WDF_DRIVER_DEVICE_ADD HidEvtDeviceAdd;
NTSTATUS
DriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath)
{
WDF_DRIVER_CONFIG config;
WDF_DRIVER_CONFIG_INIT(&config, HidEvtDeviceAdd);
return WdfDriverCreate(DriverObject, RegistryPath, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE);
}
For a complete walkthrough, see the full example.
Design Guidelines
Adhere to these best practices to ensure a robust and maintainable HID driver.
| Guideline | Description |
|---|---|
| Use KMDF | Leverage the Kernel‑Mode Driver Framework for lifecycle management and power handling. |
| Validate Report Descriptors | Validate all incoming report descriptors to prevent malformed data from causing crashes. |
| Minimize IRQL Levels | Perform heavyweight processing at PASSIVE_LEVEL; keep ISR short. |
| Secure User‑Mode Interaction | Use IOCTL_HID_GET_FEATURE and related IOCTLs with proper access checks. |
Sample Drivers
Microsoft provides several reference implementations:
API Reference
Key functions and structures are listed below. Click each name for the full MSDN description.
Troubleshooting
Common issues and resolutions:
- Device not enumerating
- Verify the
VendorIDandProductIDin the INF match the hardware. - Reports return garbage
- Check that the report descriptor aligns with the device's firmware format.
- Driver fails to load (0xC0000005)
- Enable Driver Verifier to pinpoint the offending code path.
FAQ
Q: Do I need to write a mini‑driver for each HID device?
A: Most devices can share a single driver by handling multiple report IDs.
Q: Can I use a user‑mode driver?
A: Yes, the UMDF HID class driver is available but lacks some low‑level features.