home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / source / devnews / vol2 / sample3 / vdft.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-07  |  13.8 KB  |  521 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /*                     IBM Sample Virtual Device Driver                       */
  4. /*                                                                            */
  5. /*                 Copyright (c) IBM Corporation 1993                         */
  6. /*                         All Rights Reserved                                */
  7. /*                                                                            */
  8. /*   DISCLAIMER OF WARRANTIES.  The following [enclosed] code is              */
  9. /*   sample code created by IBM Corporation. This sample code is not          */
  10. /*   part of any standard or IBM product and is provided to you solely        */
  11. /*   for  the purpose of assisting you in the development of your             */
  12. /*   applications.  The code is provided "AS IS", without                     */
  13. /*   warranty of any kind.  IBM shall not be liable for any damages           */
  14. /*   arising out of your use of the sample code, even if they have been       */
  15. /*   advised of the possibility of such damages.                              */
  16. /*                                                                            */
  17. /******************************************************************************/
  18. /**************************** END OF SPECIFICATIONS ***************************/
  19.  
  20. #define INCL_VDH
  21. #define NULL 0
  22.  
  23. #include <mvdm.h>                       /* VDH services, etc.        */
  24. #include <basemid.h>                    /* message numbers           */
  25. #include <builtin.h>                    /* builtin functions         */
  26. #include "VDFTP.H"                      /* Local Stuff               */
  27. #include "common.h"
  28.  
  29. /*                                                                   */
  30. /*                 GLOBAL DATA AREA FOR VDFT                         */
  31. /*                                                                   */
  32. #define DEBUG
  33. HIRQ hirq_DFT = 0;                      /* holds handle for EOI hook */
  34.  
  35. HVDM HVDM_Owner = 0;                    /* hvdm of owner             */
  36. INT   Status_byte;                      /* Status Register value     */
  37. PMESSAGE pMessage = NULL;
  38.  
  39. HVDHSEM SemOS2Handle = 0;
  40. HVDHSEM SemWinHandle = 0;
  41. #pragma data_seg(CSWAP_DATA)
  42.  
  43.  
  44. #pragma data_seg()
  45.  
  46. #pragma data_seg(SWAPINSTDATA)
  47.  
  48. HVDM  HVDM_Current;                     /* Current VDM handle  */
  49. VDFTPDB PDB_Current;                    /* Current PDB         */
  50.  
  51. #pragma data_seg()
  52.  
  53. #pragma data_seg(CINIT_DATA)
  54.  
  55. char szPDDName[]=DFT_NAME;
  56.  
  57. #pragma data_seg()
  58.  
  59. #pragma alloc_text(CSWAP_TEXT, VDFTDaIn, VDFTStIn, VDFTDaOut)
  60. /*
  61.  *local function prototypes go here
  62.  */
  63.  
  64. LONG EXPENTRY OS2Service(SGID sgid,ULONG ulCommand,ULONG ulCountIn,
  65.                          PVOID pvDataIn,ULONG ulCountout,
  66.                          PVOID pvDataOut);
  67. LONG OS2ReadQueue(SGID sgid,ULONG ulCountout,
  68.                     PVOID pvDataOut);
  69.  
  70. BOOL OS2TerminateService(VOID);
  71. BOOL OS2PostMessage(SGID SessionId,ULONG ulCountIn,PVOID pvDataIn);
  72. BOOL OS2InitSetup(VOID);
  73. BOOL OS2ReadMessageData(SGID sgid,ULONG ulCountout,PVOID pvDataOut);
  74. PMESSAGE GetNextLink(VOID);
  75.  
  76. #pragma alloc_text(CINIT_TEXT, Start_Here)
  77. //#pragma alloc_text(CINIT_TEXT, VDDInit)
  78.  
  79. /********************** START OF SPECIFICATIONS ***********************/
  80. /*                                                                    */
  81. /* SUBROUTINE NAME:  VDFTInit (Start_here)                            */
  82. /*                                                                    */
  83. /* DESCRIPTIVE NAME:  Virtual DFT Initialization entry point          */
  84. /*                                                                    */
  85. /* FUNCTION:  The function of this routine is to register the VDM     */
  86. /*            Creation, Termination, PDB Change, and PDB Destroy      */
  87. /*            handlers. This routine also sets up the VDFT Port       */
  88. /*            Address table in the global data area.                  */
  89. /*                                                                    */
  90. /* NOTES:                                                             */
  91. /*                                                                    */
  92. /* CONTEXT: System Initialization                                     */
  93. /*                                                                    */
  94. /* ENTRY POINT:  VDFTInit                                             */
  95. /*    LINKAGE:  CALL NEAR 32                                          */
  96. /*                                                                    */
  97. /* INPUT: psz - pointer to configuration strings                      */
  98. /*                                                                    */
  99. /* EXIT-NORMAL: returns !0                                            */
  100. /*                                                                    */
  101. /* EXIT-ERROR: returns 0                                              */
  102. /*                                                                    */
  103. /* EFFECTS: VDFT Port Address table in the global data area.          */
  104. /*                                                                    */
  105. /* INTERNAL REFERENCES:  NONE                                         */
  106. /*                                                                    */
  107. /* EXTERNAL REFERENCES:  VDHInstallUserHook - Install VDM event hook  */
  108. /*                       VDHOpenPDD         - Get PDD entry point     */
  109. /*                       VDHOpenVIRQ        - register an IRQ for VDM */
  110. /*                       VDHReservePages    - set up some linear      */
  111. /*                                 space for use as the adapter space */
  112. /************************ END OF SPECIFICATIONS ***********************/
  113. #pragma entry(Start_Here)
  114. BOOL EXPENTRY Start_Here(PSZ psz)
  115. {
  116.  
  117.      ULONG ErrorNumber;                 /* holds error number from getError*/
  118. #ifdef DEBUG
  119.      _interrupt(3);
  120. #endif
  121.      if(!VDHRegisterVDD(VDDNAME,OS2Service,NULL))
  122.      {
  123.        return(FALSE);
  124.      }
  125.      return TRUE;
  126. }
  127. /****************************************************************\
  128.  *
  129.  *--------------------------------------------------------------
  130.  *
  131.  *  Name:OS2Service
  132.  *
  133.  *  Purpose:
  134.  *
  135.  *
  136.  *
  137.  *  Usage:
  138.  *
  139.  *  Method:
  140.  *          -
  141.  *
  142.  *          -
  143.  *          -
  144.  *
  145.  *          -
  146.  *          -
  147.  *
  148.  *  Returns:
  149.  *          1 - if sucessful execution completed
  150.  *          0 - if error
  151. \****************************************************************/
  152. LONG EXPENTRY OS2Service(SGID sgid,ULONG ulCommand,ULONG ulCountIn,
  153.                          PVOID pvDataIn,ULONG ulCountout,
  154.                          PVOID pvDataOut)
  155.  
  156. {
  157.  
  158.      LONG apiRet;
  159.  
  160. #ifdef DEBUG
  161.      _interrupt(3);
  162. #endif
  163.      do
  164.      {
  165.           switch(ulCommand)
  166.           {
  167.           case INIT_COMMAND:
  168.                apiRet= OS2InitSetup();
  169.                break;
  170.           case POST_MESSAGE:
  171.                apiRet=OS2PostMessage(sgid,ulCountIn,pvDataIn);
  172.                break;
  173.  
  174.           case READ_QUEUE:
  175.                apiRet= OS2ReadQueue(sgid,ulCountout,pvDataOut);
  176.                break;
  177.           case TERMINATE_COMMAND:
  178.                OS2TerminateService();
  179.                break;
  180.           default:
  181.                apiRet = TRUE;
  182.                break;
  183.           }
  184.  
  185.  
  186.      }ONCE;
  187.      return(apiRet);
  188. }
  189. /****************************************************************\
  190.  *
  191.  *--------------------------------------------------------------
  192.  *
  193.  *  Name:
  194.  *
  195.  *  Purpose:
  196.  *
  197.  *
  198.  *
  199.  *  Usage:
  200.  *
  201.  *  Method:
  202.  *          -
  203.  *
  204.  *          -
  205.  *          -
  206.  *
  207.  *          -
  208.  *          -
  209.  *
  210.  *  Returns:
  211.  *          1 - if sucessful execution completed
  212.  *          0 - if error
  213. \****************************************************************/
  214. LONG OS2ReadQueue(SGID sgid,ULONG ulCountout,
  215.                     PVOID pvDataOut)
  216. {
  217.  
  218. #ifdef DEBUG
  219.      _interrupt(3);
  220. #endif
  221.      do
  222.      {
  223.  
  224.           /*
  225.            *wait until we get cleared
  226.            *and if so assume we have a new message waiting for us
  227.            */
  228.           if(!VDHWaitEventSem(SemOS2Handle,
  229.                           SEM_INDEFINITE_WAIT) )
  230.           {
  231.                return(TRUE);
  232.           }
  233.           /*
  234.            *reset the semaphore for the next time
  235.            *through
  236.            */
  237.           VDHResetEventSem(SemOS2Handle);
  238.           /*
  239.            *retrieve the message data
  240.            *and store it in pvDataOut
  241.            */
  242.           if(OS2ReadMessageData(sgid,ulCountout,pvDataOut) )
  243.           {
  244.                return(TRUE);
  245.           }
  246.  
  247.      }ONCE;
  248.      return(FALSE);
  249.  
  250. }
  251.  
  252. /****************************************************************\
  253.  *
  254.  *--------------------------------------------------------------
  255.  *
  256.  *  Name:
  257.  *
  258.  *  Purpose:
  259.  *
  260.  *
  261.  *
  262.  *  Usage:
  263.  *
  264.  *  Method:
  265.  *          -
  266.  *
  267.  *          -
  268.  *          -
  269.  *
  270.  *          -
  271.  *          -
  272.  *
  273.  *  Returns:
  274.  *          1 - if sucessful execution completed
  275.  *          0 - if error
  276. \****************************************************************/
  277. BOOL OS2InitSetup(VOID)
  278. {
  279.  
  280. #ifdef DEBUG
  281.      _interrupt(3);
  282. #endif
  283.  
  284.      /*
  285.       *if we have not created our structure yet
  286.       */
  287.      if(!pMessage)
  288.      {
  289.  
  290.          /*
  291.           *allocate our control
  292.           *structure for our messagelist
  293.           */
  294.           if( ( pMessage = VDHAllocMem(sizeof(MESSAGE),
  295.                                         VDHAM_SWAPPABLE) ) == NULL)
  296.           {
  297.                return(TRUE);
  298.           }
  299.      }
  300.      pMessage->SrcSessionIdentifier = -1;
  301.      pMessage->DstSessionIdentifier = -1;
  302.      pMessage->ulMessageType        = 0;
  303.      pMessage->ulMessageSubCommand  = 0;
  304.      pMessage->ulBufferSize         = 0;
  305.      pMessage->pvMessageData    = NULL;
  306.      pMessage->pvNextMessage    = NULL;
  307.      /*
  308.       *create our event semaphore for the
  309.       *read thread from the OS/2 process
  310.       */
  311.  
  312.      if(!VDHCreateSem(&SemOS2Handle,
  313.                      VDH_EVENTSEM) )
  314.      {
  315.           return(TRUE);
  316.      }
  317.  
  318.      VDHResetEventSem(SemOS2Handle);
  319.      return(FALSE);
  320.  
  321. }
  322.  
  323.  
  324. /****************************************************************\
  325.  *
  326.  *--------------------------------------------------------------
  327.  *
  328.  *  Name:
  329.  *
  330.  *  Purpose:
  331.  *
  332.  *
  333.  *
  334.  *  Usage:
  335.  *
  336.  *  Method:
  337.  *          -
  338.  *
  339.  *          -
  340.  *          -
  341.  *
  342.  *          -
  343.  *          -
  344.  *
  345.  *  Returns:
  346.  *          1 - if sucessful execution completed
  347.  *          0 - if error
  348. \****************************************************************/
  349. BOOL OS2TerminateService(VOID)
  350. {
  351.      BOOL fError = FALSE;
  352.  
  353. #ifdef DEBUG
  354.      _interrupt(3);
  355. #endif
  356.  
  357.      do
  358.      {
  359.  
  360.           /*
  361.            *post our semaphore
  362.            */
  363.           VDHPostEventSem(SemOS2Handle);
  364.           /*
  365.            *and get rid of it
  366.            */
  367.           VDHDestroySem(SemOS2Handle);
  368.  
  369.      }ONCE;
  370.      return(fError);
  371. }
  372.  
  373. /****************************************************************\
  374.  *
  375.  *--------------------------------------------------------------
  376.  *
  377.  *  Name:
  378.  *
  379.  *  Purpose:
  380.  *
  381.  *
  382.  *
  383.  *  Usage:
  384.  *
  385.  *  Method:
  386.  *          -
  387.  *
  388.  *          -
  389.  *          -
  390.  *
  391.  *          -
  392.  *          -
  393.  *
  394.  *  Returns:
  395.  *          1 - if sucessful execution completed
  396.  *          0 - if error
  397. \****************************************************************/
  398. BOOL OS2PostMessage(SGID SessionId,ULONG ulCountIn,PVOID pvDataIn)
  399. {
  400.      BOOL fError = FALSE;
  401.      PMESSAGE pMessage;
  402.  
  403.  
  404.      do
  405.      {
  406.           /*
  407.            *get the next link in our list
  408.            */
  409.            pMessage = GetNextLink();
  410.  
  411.  
  412.           /*
  413.            *allocate some local storage
  414.            */
  415.            if( (pMessage = VDHAllocMem(ulCountIn,VDHAM_SWAPPABLE))== NULL)
  416.            {
  417.                return(TRUE);
  418.            }
  419.  
  420.           /*
  421.            *copy our message data in
  422.            *and hold onto our session identifier
  423.            */
  424.            pMessage->SrcSessionIdentifier = SessionId;
  425.            pMessage->ulBufferSize         = ulCountIn;
  426.            memcpy(pMessage->pvMessageData,pvDataIn,
  427.                   ulCountIn);
  428.  
  429.  
  430.           /*
  431.            *clear the semaphore on the Windows side
  432.            */
  433.           VDHPostEventSem(SemWinHandle);
  434.  
  435.  
  436.      }ONCE;
  437.      return(fError);
  438. }
  439. /****************************************************************\
  440.  *
  441.  *--------------------------------------------------------------
  442.  *
  443.  *  Name:
  444.  *
  445.  *  Purpose:
  446.  *
  447.  *
  448.  *
  449.  *  Usage:
  450.  *
  451.  *  Method:
  452.  *          -
  453.  *
  454.  *          -
  455.  *          -
  456.  *
  457.  *          -
  458.  *          -
  459.  *
  460.  *  Returns:
  461.  *          1 - if sucessful execution completed
  462.  *          0 - if error
  463. \****************************************************************/
  464. BOOL OS2ReadMessageData(SGID sgid,ULONG ulCountout,PVOID pvDataOut)
  465. {
  466.  
  467.  
  468.  
  469.  
  470.  
  471.      return(FALSE);
  472. }
  473. /****************************************************************\
  474.  *
  475.  *--------------------------------------------------------------
  476.  *
  477.  *  Name:
  478.  *
  479.  *  Purpose:
  480.  *
  481.  *
  482.  *
  483.  *  Usage:
  484.  *
  485.  *  Method:
  486.  *          -
  487.  *
  488.  *          -
  489.  *          -
  490.  *
  491.  *          -
  492.  *          -
  493.  *
  494.  *  Returns:
  495.  *          1 - if sucessful execution completed
  496.  *          0 - if error
  497. \****************************************************************/
  498. PMESSAGE GetNextLink(VOID)
  499. {
  500.      PMESSAGE pMessageNext;
  501.  
  502.      do
  503.      {
  504.           /*
  505.            *spin through our list looking for the
  506.            *first null link
  507.            *in our list
  508.            */
  509.           for(pMessageNext = (PMESSAGE)pMessage->pvNextMessage;
  510.                          !pMessageNext;
  511.                          pMessageNext = pMessageNext->pvNextMessage)
  512.           {
  513.                ;
  514.           }
  515.  
  516.  
  517.      }ONCE;
  518.      return(pMessageNext);
  519. }
  520.  
  521.