View on GitHub

Devendra's Log

Custom USB driver and app using WinUSB and C#

Writing USB drivers used to be a tough proposition before WinUSB. There are other frameworks like the WinDriver toolkit from Jungo that have existed before WinUSB, but they can easily cost several thousand dollars in developer licenses. You can target Windows XP SP3 and beyond with WinUSB. If you are new to USB I suggest reading Jack Ganssle’s USB Overview.

Creating a Custom Driver

WinUSB has a kernel mode driver (winusb.sys) that talks with the device and a user mode DLL (winusb.dll) that communicates with the driver. To communication with the USB device Windows needs to know about your device. This configuration is provided in an INF file. I refer you to the INF template as a starting point. I am reproducing a modified version of the template that works with 32-bit and 64-bit Windows. Remember to change strings that have MY in them, and the class and device interface GUIDs. You’ll also need to edit the USB vendor and product IDs to match your device.

To understand how to put together a driver package I refer you to the WinUSB (winusb.sys) Installation MSDN document. You’ll need the latest version of Windows Driver Kit.

Your objective then is to have a folder with the following elements.

<root folder>

You can now use the Windows Device Manager to update the driver for your, as yet, unknown device. Point Windows to the INF file you have created. If all goes well you’ll see a MY DEVICE NAME device category with a device called MY DEVICE NAME, or something else appropriate if you’ve changed that in the INF file. Windows 8 refuses to install unsigned drivers, you’ll either need to enable installation of unsigned drivers, or test-sign the driver.

Using WinUSB without a driver package

Your USB device may work with WinUSB without requiring a custom INF. Details are available under section “Installing WinUSB by specifying the system-provided device class” at WinUSB Installation. You’ll need to edit Windows registry to set the device interface GUID each time the device is plugged to a different physical port. If your device provides the right descriptors it should work with Windows 8 without requiring any extra effort.

Communicate with the device in C# using WinUSBNet

We’ll use the excellent WinUSBNet component to communicate with the device. Download and reference it in your C# project. Here’s a short snippet of code that demonstrates how you can use WinUSBNet to access your USB device.

Here’s some sample output produced by running the console app above. The information regarding the USB Device is printed using the printInfo method.

Interface - id:0, protocol:1, class:2, subclass:2
  Pipe - address:129
Interface - id:1, protocol:0, class:10, subclass:0
  Pipe - address:3
  Pipe - address:130
00 02 00 00 00 00 00 06 00 00 00 07 00 00 00 02 01 01 01

The most significant bit of the pipe’s address indicates direction, it is IN (data arriving at the host) when set. Thus address 129 (0x81) is actually a bulk data IN endpoint with ID 1. WinUSBNet only supports control and bulk transfer endpoints.