Best Practices for Maintainable Device Drivers

Other versions of this page are also available for the following:

Windows Mobile Not SupportedWindows Embedded CE Supported

8/28/2008

While developing a device driver, if feasible, write consistent, self-validating code, in a non-confusing manner.

The following list shows best practices for maintainability:

  • Define the power states of your device.
  • Remove the DOSYSGEN logic from sources files.
  • Use all source and header files.
  • Verify that no source or header files contain unused code.
  • Module-level data, meaning data that controls or tracks instances, should be initialized and deinitialized in a global variable and/or in DLL_PROCESS_ATTACH and DLL_PROCESS_DETACH.
    Instance-level data, meaning data that is specific to a logical object, should be initialized and deinitialized in the Init and DeInit routines. Initializing instance-level data in a global variable or process attach can lead to accessing stale data not initialized during consecutive Init routine calls, since the DLL may not be loaded in between instances.
    Be aware of any implicit initialization to zero or NULL.
  • Call the DisableThreadLibraryCalls function within DLL_PROCESS_ATTACH in each DLL.
    DLL_PROCESS_ATTACH is in the DLLENTRY function. Installable ISR DLLs cannot call DisableThreadLibraryCalls.
  • Move duplicated code into functions.
  • Move code that multiple drivers share into a shared library.
  • Use only necessary critical sections.
  • Do not use the HalXXX bus parent access functions: HalGetBusData, HalTranslateBusAddress, and so on.
    Instead, use the CEDDK bus access functions: BusTransBusAddrToVirtual, GetBusNamePrefix, and so on.
  • Use the DEBUGCHK or ASSERT macros to ensure all assumptions are explicit.
  • Use validation functions to validate important structures, global data, device states, and so on.
  • Use debug zones of appropriate size.
  • Run the Application Verifier on the driver.
  • Declare destructors as virtual, if you expect a C++ class to have subclasses.

See Also

Reference

CEDDK Dynamic-Link Library Bus Access Reference

Concepts

Bus Drivers
Windows Embedded CE Drivers

Other Resources

Power States
Application Verifier Tool
Controlling Debug Message Output With Macros