Sample Plug and Play Driver

Drivers are informed about their device node through a system control call issued by Configuration Manger.


/*
  *  DRIVER.C - NewDriver Support for SAMPLE
  *  This file processes the Driver requests.
  */
 
 #include <basedef.h>
 #include <vmm.h>
 #include <debug.h>
 #include <configmg.h>
 #include <pnpdrvs.h>
 #include <misc.h>
 #include <driver.h>
 
 #pragma VxD_LOCKED_CODE_SEG
 
 IO_RESOURCE    Res1=
 {
  {2, IOType_Range, 0, 0, 0, 0, 0},
  {
     {0xFFFF, 1, 0x103, 0x103, 0, 0xFF, 0xFF},
     {0xFFFF, 1, 0x203, 0x203, 0, 0xFF, 0xFF},
  }
 };
 
 IO_RESOURCE    Res2=
 {
  {2, IOType_Range, 0, 0, 0, 0, 0},
  {
     {0x0300, 6, 0x200, 0x307, 0, 3, 3},
     {0x03F0, 4, 0x300, 0x313, 0, 3, 3},
  }
 };
 
 /*****************************************************************
  *
  *    ConfigHandler - Handler of the Configuration Manager 
  *    configuration call
  *
  *    Exported.
  *
  *    ENTRY:    Standard config handler.
  *    EXIT:    Standard Configuration Manager return value.
  *
  ****************************************************************/
 CONFIGRET CM_HANDLER
 ConfigHandler(CONFIGFUNC cfFuncName, SUBCONFIGFUNC scfSubFuncName,
     DEVNODE dnToDevNode, DEVNODE dnAboutDevNode, ULONG ulFlags)
 {
     LOG_CONF    lc;
     RES_DES    rd;
     CONFIGRET    status;
 
     switch (cfFuncName) {
 
         case CONFIG_FILTER:
 
             // Get the first filtered logical configuration.
 
             status=CM_Get_First_Log_Conf(&lc,
                             dnAboutDevNode,
                             FILTERED_LOG_CONF);
             // For all logical configuration (we stopped when
             // the status is not CR_SUCCESS since it can only
             // become CR_NO_MORE_LOG_CONF).
 
             while (status==CR_SUCCESS) {
 
                 // For all resource descriptors
 
                 rd=(RES_DES)lc;
                 while (CM_Get_Next_Res_Des(&rd,
                                 rd,
                                 ResType_Mem,
                                 NULL, 0)==CR_SUCCESS) {
 
                     // For all memory resource
                     // (for instance) do some
                     // checking (like ISA impose
                     // <16Meg)
                 }
                 
                 // Next logical configuration.
 
                 status=CM_Get_Next_Log_Conf(&lc,
                                 lc,
                                 0);
 
             }
 
             return(CR_SUCCESS);
 
         case CONFIG_START:
 
             // You may or may not treat CONFIG_START_FIRST_START
             // different from CONFIG_START_DYNAMIC_START.
 
             // As far as CM goes, the first time it would
             // have called with CONFIG_START_DYNAMIC_START; 
             // it will call with CONFIG_START_FIRST_START
             // instead.
 
             return(CR_SUCCESS);
 
         case CONFIG_TEST:
             return(CR_SUCCESS);
 
         case CONFIG_STOP:
             return(CR_SUCCESS);
 
         case CONFIG_REMOVE:
             // deallocate per instance data.
 
             return(LeaveInstance(CR_SUCCESS));
 
         default:
             return(CR_DEFAULT);
     }
 }
 
 /*****************************************************************
  *
  *    NewDriver - Register a new driver for the new device node
  *
  *    Exported.
  *
  *    ENTRY: DevNode is the new device node that has just been created.
  *
  *    EXIT:  Standard Configuration Manager return value.
  *
  ****************************************************************/
 CONFIGRET CM_INTERNAL
 NewDriver(DEVNODE DevNode)
 {
     LOG_CONF    lc;
     RES_DES     rd1, rd2;
 
     // Any failure should use LeaveInstance to unload the DLVxD if
     // it is the only instance. Only if it is success will we not
     // return with an "OR"ed CR_UNLOAD.
 
     // BUGBUG error checking
 
     CM_Add_Empty_Log_Conf(&lc, DevNode, 0, BASIC_LOG_CONF);
 
     CM_Add_Res_Des(&rd1, lc, ResType_IO, &Res1, SIZEOF_IORANGE(2), 0);
 
     CM_Add_Res_Des(&rd2, lc, ResType_IO, &Res2, SIZEOF_IORANGE(2), 0);
 
     CM_Register_Device_Driver(DevNode, ConfigHandler, 0);
 
     // Allocate per instance data
 
     return(CR_SUCCESS);
 }