Click here to Skip to main content
11,581,982 members (74,938 online)
Click here to Skip to main content
Add your own
alternative version

Developing a WDF USB Kernel Mode Driver for the OSR USB FX2

, 30 Mar 2006 MIT 230.8K 8.1K 166
This article describes the process of developing a USB Kernel mode device driver using the WDF Kernel Mode Driver Foundation.
article_demo.zip
tester.exe
wdf_usb_api.dll
kmdf_usb_driver.zip
WDF_Usb_driver
makefile
sources
wdf_usb_man.inf
#ifndef __PROTOTYPES_H
#define __PROTOTYPES_H

/*revert to warning level 3 to prevent problems with the DDK declarations.
  just disabling the warnings does not work because they get set back to default
  inside ntddk.h.
  NOTE: apparently this behavior is corrected in the vista DDK.*/
#pragma warning(push, 3)

#pragma warning(disable:4200)  // nameless struct/union
#pragma warning(disable:4201)  // nameless struct/union
#pragma warning(disable:4115)  // named typedef in parenthesis
#pragma warning(disable:4214)  // bit field types other than int

#include <ntddk.h>
#include "usbdi.h"
#include "wdf.h"
#include "wdfusb.h"

#pragma warning(pop)


#define __DRIVER_NAME         "WDF_USB: "

#define VC_SET_LIGHT_BAR      0xD8
#define VC_GET_LIGHT_BAR      0xD7

/*declaration of the device context. we have to declare the type of the
  device context, and an accessor function name that will return to us
  a pointer to the device context.*/
typedef struct _DEVICE_CONTEXT {
  WDFUSBDEVICE      UsbDevice;
  WDFUSBINTERFACE   UsbInterface;
  WDFQUEUE          IoWriteQueue;
  WDFQUEUE          IoReadQueue;
  WDFQUEUE          IoControlEntryQueue;
  WDFQUEUE          IoControlSerialQueue;
  WDFQUEUE          SwitchChangeRequestQueue;
  BYTE              ActSwitchPack;
  WDFUSBPIPE        UsbInterruptPipe;
  WDFUSBPIPE        UsbBulkInPipe;
  WDFUSBPIPE        UsbBulkOutPipe;
  WDFMEMORY         WdfMemLEDArrayState;
  BYTE              D0LEDArrayState;
} DEVICE_CONTEXT, *PDEVICE_CONTEXT;

WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(DEVICE_CONTEXT, GetDeviceContext);

NTSTATUS
DriverEntry(
    IN PDRIVER_OBJECT  DriverObject, 
    IN PUNICODE_STRING  RegistryPath);

NTSTATUS
EvtDeviceAdd(
    IN WDFDRIVER        Driver,
    IN PWDFDEVICE_INIT  DeviceInit
    );

NTSTATUS
EvtDevicePrepareHardware(
    IN WDFDEVICE    Device,
    IN WDFCMRESLIST ResourceList,
    IN WDFCMRESLIST ResourceListTranslated
    );

VOID
EvtDeviceIoRead(
    IN WDFQUEUE  Queue,
    IN WDFREQUEST  Request,
    IN size_t  Length
    );

VOID
EvtDeviceIoWrite(
    IN WDFQUEUE  Queue,
    IN WDFREQUEST  Request,
    IN size_t  Length
    );

VOID
EvtDeviceIoControlEntry(
    IN WDFQUEUE  Queue,
    IN WDFREQUEST  Request,
    IN size_t  OutputBufferLength,
    IN size_t  InputBufferLength,
    IN ULONG  IoControlCode
    );

VOID
EvtDeviceIoControlSerial(
    IN WDFQUEUE  Queue,
    IN WDFREQUEST  Request,
    IN size_t  OutputBufferLength,
    IN size_t  InputBufferLength,
    IN ULONG  IoControlCode
    );

VOID
EvtUsbDeviceInterrupt(
    WDFUSBPIPE  Pipe,
    WDFMEMORY  Buffer,
    size_t  NumBytesTransferred,
    WDFCONTEXT  Context
    );

NTSTATUS
EvtDeviceD0Entry(
    IN WDFDEVICE  Device,
    IN WDF_POWER_DEVICE_STATE  PreviousState
    );

NTSTATUS
EvtDeviceD0Exit(
    IN WDFDEVICE  Device,
    IN WDF_POWER_DEVICE_STATE  TargetState
    );

NTSTATUS
EvtDeviceD0ExitPreInterruptsDisabled(
    IN WDFDEVICE  Device,
    IN WDF_POWER_DEVICE_STATE  TargetState
    );

NTSTATUS
InitPowerManagement(
    IN WDFDEVICE  Device,
    IN PDEVICE_CONTEXT Context
    );

VOID
EvtIoReadComplete(
    IN WDFREQUEST  Request,
    IN WDFIOTARGET  Target,
    IN PWDF_REQUEST_COMPLETION_PARAMS  Params,
    IN WDFCONTEXT  Context
    );

VOID
EvtIoWriteComplete(
    IN WDFREQUEST  Request,
    IN WDFIOTARGET  Target,
    IN PWDF_REQUEST_COMPLETION_PARAMS  Params,
    IN WDFCONTEXT  Context
    );

NTSTATUS
CreateQueues(
    WDFDEVICE Device,
    PDEVICE_CONTEXT Context
    );

NTSTATUS
ConfigureUsbInterface(
    WDFDEVICE Device,
    PDEVICE_CONTEXT DeviceContext
    );

NTSTATUS
ConfigureUsbPipes(
    PDEVICE_CONTEXT DeviceContext
    );

LPCSTR
PowerName(
    WDF_POWER_DEVICE_STATE PowerState
    );

VOID
IoCtlGetSwitchPack(
    IN WDFQUEUE  Queue,
    IN WDFREQUEST  Request,
    IN size_t  OutputBufferLength,
    IN size_t  InputBufferLength
    );

VOID
IoCtlGetSwitchPackChange(
    IN WDFQUEUE  Queue,
    IN WDFREQUEST  Request,
    IN size_t  OutputBufferLength,
    IN size_t  InputBufferLength
    );

VOID
IoCtlSetLightBar(
    IN WDFQUEUE  Queue,
    IN WDFREQUEST  Request,
    IN size_t  OutputBufferLength,
    IN size_t  InputBufferLength
    );

VOID
IoCtlGetLightBar(
    IN WDFQUEUE  Queue,
    IN WDFREQUEST  Request,
    IN size_t  OutputBufferLength,
    IN size_t  InputBufferLength
    );

NTSTATUS
llSetLightBar(
    IN PDEVICE_CONTEXT Context,
    IN WDFMEMORY State
    );

NTSTATUS
llGetLightBar(
    IN PDEVICE_CONTEXT Context,
    IN WDFMEMORY State
    );

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Bruno van Dooren
Web Developer
Belgium Belgium
I am a professional software developer with an interest in everything that is about making hardware work.

In the course of my work, I have programmed device drivers and services on Windows and linux.

I have written firmware for embedded devices in C and assembly language, and have designed and implemented real-time applications for testing of sattelite payload equipment.

Generally, finding out how to interface hardware with software is my hobby and job.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 30 Mar 2006
Article Copyright 2006 by Bruno van Dooren
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid