6.2. Writing the Device Driver Without DriverWizard

There may be times when you choose to write your driver directly, without using DriverWizard. In such cases, either follow the steps outlined in this section to create a new driver project, or select a WinDriver sample that most closely resembles your target driver and modify it to suit your specific requirements.

6.2.1. Include the Required WinDriver Files

  1. Include the relevant WinDriver header files in your driver project.
    All header files are found under the WinDriver/include directory.

    All WinDriver projects require the windrvr.h header file.
    When using the WDU_xxx WinDriver USB API [B.2], include the wdu_lib.h header file; (this file already includes windrvr.h).

    Include any other header file that provides APIs that you wish to use from your code (e.g., files from the WinDriver/samples/shared directory, which provide convenient diagnostics functions.)

  2. Include the relevant header files from your source code: For example, to use the USB API from the wdu_lib.h header file, add the following line to the code:
    #include "wdu_lib.h"
  3. Link your code with the WDAPI library (Windows) / shared object (Linux):
    • For Windows: WinDriver\lib\<CPU>\wdapi1260.lib, where the <CPU> directory is either x86 (32-bit binaries for x86 platforms), amd64 (64-bit binaries for x64 platforms), or amd64\x86 (32-bit binaries for x64 platforms [A.2]
    • For Linux: From the WinDriver/lib directory — libwdapi1260.so or libwdapi1260_32.so (for 32-bit applications targeted at 64-bit platforms)
      Note: When using libwdapi1260_32.so, first create a copy of this file in a different directory and rename it to libwdapi1260.so, then link your code with the renamed file [A.2].

    You can also include the library's source files in your project instead of linking the project with the library. The C source files are located under the WinDriver/src/wdapi directory.

    [Note]
    When linking your project with the WDAPI library/shared object, you will need to distribute this binary with your driver.
    For Windows, get wdapi1260.dll / wdapi1260_32.dll (for 32-bit applications targeted at 64-bit platforms) from the WinDriver\redist directory.
    For Linux, get libwdapi1260.so / libwdapi1260_32.so (for 32-bit applications targeted at 64-bit platforms) from the WinDriver/lib directory.

    Note: On Windows and Linux, when using the DLL/shared object file for 32-bit applications on 64-bit platforms (wdapi1260_32.dll / libwdapi1260_32.so), rename the copy of the file in the distribution package, by removing the _32 portion [A.2].
    For detailed distribution instructions, refer to Chapter 10.

  4. Add any other WinDriver source files that implement API that you which to use in your code (e.g., files from the WinDriver/samples/shared directory.)

6.2.2. Write Your Code

  1. Call WDU_Init() [B.4.1] at the beginning of your program to initialize WinDriver for your USB device, and wait for the device-attach callback. The relevant device information will be provided in the attach callback.
  2. Once the attach callback is received, you can start using one of the WDU_Transfer() [B.4.8.1] functions family to send and receive data.
  3. To finish, call WDU_Uninit() [B.4.7] to unregister from the device.

6.2.3. Configure and Build Your Code

After including the required files and writing your code, make sure that the required build flags and environment variables are set, then build your code.

[Note]
When developing a driver for a 64-bit platform [A], your project or makefile must include the KERNEL_64BIT preprocessor definition. In the makefiles, the definition is added using the -D flag: -DKERNEL_64BIT. (The sample and wizard-generated Linux and Windows GCC makefiles and the Windows MS Visual Studio projects, in the 64-bit WinDriver toolkit, already include this definition.)
[Note]
Before building your code, verify that the WD_BASEDIR environment variable is set to the location of the of the WinDriver installation directory.
On Windows and Linux you can define the WD_BASEDIR environment variable globally — as explained in Chapter 4: For Windows — refer to the Windows WD_BASEDIR note in Section 4.2.1; for Linux: refer to Section 4.2.2.2, Step 8.