## The USBINT Sample

The USBINT Sample accompanies Chapter 12 and illustrates how to program an interrupt endpoint. This sample was modified for the 2d edition to eliminate automatic power up on handle open in the interest of reducing confusion.

The sample includes three components:

- USBINT.SYS (SYS subdirectory) is a WDM driver for a USB device based on the Anchor Chips development board.
- TEST.EXE (TEST subdirectory) is a Win32 console application for testing USBINT.SYS.
- USBINT.HEX (EZUSB subdirectory) is the firmware image for the USBINT device.

### Building USBINT

To build USBINT.SYS, follow the general instructions for building a WDM sample. The Microsoft Visual C++ project file is named USBINT.DSP in the SYS subdirectory.

To build TEST.EXE, follow the general instructions for building user-mode programs. The Microsoft Visual C++ project file is named TEST.DSP in the TEST subdirectory.

### Testing USBINT

First follow the general instructions for testing USB samples to install the device firmware (USBINT.HEX in the EZUSB subdirectory) and the driver. Then open a command prompt and run TEST.EXE from the TEST\DEBUG subdirectory. It will appear to hang. Press button number 1 on the Anchor Chips development board. Each time you press the button, the LED display will change, and TEST will record another interrupt. Terminate TEST by pressing Ctrl+Break.

### Special note about power management

This is the only USB sample driver that concerns itself with power management. Try to run this sample on a computer that supports a standby state. Before the driver allows the device to power down, it will cancel the outstanding interrupt read. This is a requirement of the USB parent driver -- you can't reduce the power to a USB device with any IRP other than an IRP_MN_WAIT_WAKE outstanding. Conversely, after power is restored, the driver restarts the interrupt read.

In order to operate correctly, this driver uses three callbacks from GENERIC.SYS that aren't used in most of the samples in the book:

GetDevicePowerState | Returns D0 for the S0 state
SaveContext | Halts the interrupt read
RestoreContext | Reissues the interrupt read

There is really no way to properly program an interrupt endpoint except by including these callback routines in your driver. You might design the driver to leave your device in a low power state most of the time, possibly with wakeup enabled. Refer to the WAKEUP sample for code examples. You would not need to modify the callback routines in this driver to add on wakeup and automatic power-down behavior.

### What to look for

The key function in this driver is StartInterruptUrb (ReadWrite.cpp), which issues and reissues the read against the interrupt endpoint. The devil in this particular sample is in all the details about when a read is started and cancelled in response to various PnP and power events, so you basically have to read the whole driver very carefully.