home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 4 Drivers / 04-Drivers.zip / scsiopt2.zip / ioctl.c < prev    next >
C/C++ Source or Header  |  1997-12-16  |  6KB  |  298 lines

  1. /*
  2.  * $Source: e:/source/driver/perf/RCS/ioctl.c,v $
  3.  * $Revision: 1.4 $
  4.  * $Date: 1997/12/16 02:24:16 $
  5.  * $Locker:  $
  6.  *
  7.  * General IOCtl interface
  8.  *
  9.  * $Log: ioctl.c,v $
  10.  * Revision 1.4  1997/12/16 02:24:16  vitus
  11.  * - renamed source files
  12.  *
  13.  * Revision 1.3  1997/12/11 01:52:47  vitus
  14.  * - implemented DSKPF_READ_MSGS, DSKPF_CLEAR_MSGS IOCtls
  15.  * - made DSKPF_QUERY_VERSION compatible to DSKSleep
  16.  *
  17.  * Revision 1.2  1997/12/06 01:01:16  vitus
  18.  * - enabled support for WatCom C
  19.  *
  20.  * Revision 1.1  1997/10/28 02:09:49  vitus
  21.  * Initial revision
  22.  * ----------------------------------------
  23.  * Author: Vitus Jensen, 2:2474/424, 1997
  24.  */
  25. #pragma off (unreferenced)
  26. static char vcid[]="$Id: ioctl.c,v 1.4 1997/12/16 02:24:16 vitus Exp $";
  27. #pragma on (unreferenced)
  28.  
  29. #include <string.h>
  30.  
  31. #define INCL_NOBASEAPI
  32. #define INCL_DOSERRORS
  33. #define INCL_NOPMAPI
  34. #include <os2.h>
  35. #include <bseerr.h>
  36.  
  37. #include <devcmd.h>
  38. #include <strat2.h>
  39. #include <reqpkt.h>
  40.  
  41. #include <iorb.h>
  42. #include <addcalls.h>
  43. #if defined(__WATCOMC__)
  44. # include <devhelp.h>
  45. typedef USHORT NEAR *    NPUSHORT;
  46. typedef VOID NEAR *    NPVOID;
  47. #else
  48. # include <dhcalls.h>
  49. #endif
  50.  
  51. #include "scsiopt.h"
  52. #include "proto.h"
  53. #include "extern.h"
  54.  
  55. #include "scsioptpub.h"
  56.  
  57.  
  58.  
  59.  
  60. /* Save messages here.  This buffer is located in a swapable
  61.  * data segment and may only access at task time!
  62.  * OBS: M$-C tends to allocate a const containing the
  63.  * segment of these variables.  To prevent this use strange code
  64.  * and no optimization at all. */
  65.  
  66. #define _inswap        _based(_segname("SWAPDATA"))
  67. PRIVATE USHORT _inswap    iNextString = 0;
  68. PRIVATE UCHAR _inswap    DisplayBuffer[DISPLAYBUFFER_SIZE];
  69.  
  70.  
  71.  
  72. /* Macro to verify that a certain memory range is
  73.  * readable/writable (depending on w). */
  74.  
  75. #define CheckPointer(p,s,w)    DevHelp_VerifyAccess(FP_SEL(p),(s),FP_OFF(p),(w))
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. /*#
  83.  * NAME
  84.  *    DriverVersion
  85.  * CALL
  86.  *    DriverVersion(rp)
  87.  * PARAMETER
  88.  *    request packet
  89.  * RETURNS
  90.  *    driver status word
  91.  * GLOBAL
  92.  *    none
  93.  * DESPRIPTION
  94.  *    Returns version of this driver.  Coded like the MS-DOS
  95.  *    version number.
  96.  * REMARKS
  97.  */
  98. PRIVATE USHORT NEAR
  99. DriverVersion(PRP_GENIOCTL rp)
  100. {
  101.     PUSHORT const    datasize = &rp->DataLen;
  102.     USHORT const    dsize = (*datasize == 0 ? 0xFFFF : *datasize);
  103.     PDSKPF_VER_DATA const data = (PDSKPF_VER_DATA)rp->DataPacket;
  104.  
  105.     if( dsize < sizeof(DSKPF_VER_DATA)
  106.     ||  CheckPointer(rp->DataPacket, sizeof(DSKPF_VER_DATA), 1) )
  107.     return STATUS_ERR_INVPARAM;
  108.  
  109.     data->version = MAKEUSHORT(SUBVERSION,VERSION);
  110.     data->flags = 0;
  111.     return STATUS_DONE;
  112. }
  113.  
  114.  
  115.  
  116.  
  117. /*#
  118.  * NAME
  119.  *    ReadMessages
  120.  * CALL
  121.  *    ReadMessages(rp)
  122.  * PARAMETER
  123.  *    rp        request packet
  124.  * RETURNS
  125.  *    driver status word
  126.  * GLOBAL
  127.  *    DisplayBuffer,iNextString
  128.  * DESPRIPTION
  129.  *    Copies message buffer to user buffer.
  130.  * REMARKS
  131.  */
  132. #pragma optimize("",off)            /* keep optimizer quiet */
  133. PRIVATE USHORT NEAR
  134. ReadMessages(PRP_GENIOCTL rp)
  135. {
  136.     PUSHORT const    datasize = &rp->DataLen;
  137.     USHORT        dsize = (*datasize == 0 ? 0xFFFF : *datasize);
  138.     PDSKPF_MSGS_DATA const data = (PDSKPF_MSGS_DATA)rp->DataPacket;
  139.     PUSHORT        pcbMsg = &iNextString;
  140.     PUCHAR        pMsg = DisplayBuffer;
  141.     USHORT        rc = STATUS_ERR_INVPARAM;
  142.  
  143.     *datasize = *pcbMsg + FIELDOFFSET(DSKPF_MSGS_DATA,msg);
  144.  
  145.     if( dsize < FIELDOFFSET(DSKPF_MSGS_DATA,msg)
  146.     ||  CheckPointer(data, FIELDOFFSET(DSKPF_MSGS_DATA,msg), 1) )
  147.     return rc;
  148.     data->cb = *datasize;            /* tell caller */
  149.  
  150.     if( dsize > *pcbMsg + FIELDOFFSET(DSKPF_MSGS_DATA,msg) )
  151.     dsize = *pcbMsg + FIELDOFFSET(DSKPF_MSGS_DATA,msg);
  152.     if( CheckPointer(data, dsize, 1) )
  153.     return rc;
  154.  
  155.     /* If some bytes available in caller buffer (more than
  156.      * just 'cb'), copy part/all of message buffer. */
  157.  
  158.     if( dsize - FIELDOFFSET(DSKPF_MSGS_DATA,msg) > 0 )
  159.     {
  160.     _fmemcpy(data->msg, pMsg, dsize - FIELDOFFSET(DSKPF_MSGS_DATA,msg));
  161.     }
  162.  
  163.     return STATUS_DONE;
  164. }
  165. #pragma optimize("",)
  166.  
  167.  
  168.  
  169.  
  170. /*#
  171.  * NAME
  172.  *    ClearMessages
  173.  * CALL
  174.  *    ClearMessages(rp)
  175.  * PARAMETER
  176.  *    rp        request packet
  177.  * RETURNS
  178.  *    driver status word
  179.  * GLOBAL
  180.  *    iNextString
  181.  * DESPRIPTION
  182.  *    Removes messages from DisplayBuffer.
  183.  * REMARKS
  184.  */
  185. #pragma optimize("",off)            /* keep optimizer quiet */
  186. PRIVATE USHORT NEAR
  187. ClearMessages(PRP_GENIOCTL rp)
  188. {
  189.     PUSHORT const    parmsize = &rp->ParmLen;
  190.     PUSHORT const    datasize = &rp->DataLen;
  191.  
  192.     PUSHORT    pi = &iNextString;
  193.     USHORT    rc = STATUS_DONE;
  194.  
  195.     *parmsize = 0;
  196.     *datasize = 0;
  197.  
  198.     *pi = 0;                    /* to beginning of buffer */
  199.     return rc;
  200. }
  201. #pragma optimize("",)
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. /* **********************************************************************
  209.  * **** Public Entries **************************************************
  210.  * ******************************************************************* */
  211.  
  212. /*#
  213.  * NAME
  214.  *    SaveMessage
  215.  * CALL
  216.  *    SaveMessage(void)
  217.  * PARAMETER
  218.  *    none
  219.  * RETURNS
  220.  *    nothing
  221.  * GLOBAL
  222.  *    szMsgBuffer,msgBuffer        message
  223.  *    DisplayBuffer,iNextString    swapdata buffer
  224.  * DESPRIPTION
  225.  *    Copies 'szMsgBuffer' to swapdata buffer and, if still
  226.  *    in init phase 1, to screen via DevHelp_Save_Messsage.
  227.  * REMARKS
  228.  */
  229. #pragma optimize("",off)
  230. PUBLIC void NEAR
  231. SaveMessage(void)
  232. {
  233.     PUSHORT    pi = &iNextString;
  234.  
  235.     DISABLE();
  236.     if( *pi+_fstrlen(szMsgBuffer) < DISPLAYBUFFER_SIZE )
  237.     {
  238.     _fstrcpy(&DisplayBuffer[*pi], szMsgBuffer);
  239.     *pi += _fstrlen(szMsgBuffer) + 1;
  240.     }
  241.     ENABLE();
  242.     DEBMSG1("\r\n%s",(PSZ)szMsgBuffer);
  243.     if( !(fDriverFlags & DF_INITDONE) )
  244.     DevHelp_Save_Message((NPBYTE)&msgBuffer);
  245.     return;
  246. }
  247. #pragma optimize("",)
  248.  
  249.  
  250.  
  251.  
  252. /*#
  253.  * NAME
  254.  *    GenIOCtl
  255.  * CALL
  256.  *    GenIOCtl(rp)
  257.  * PARAMETER
  258.  *    rp        request packet
  259.  * RETURNS
  260.  *    driver status word
  261.  * GLOBAL
  262.  *    none
  263.  * DESPRIPTION
  264.  *    Calls approbiate subfunction.
  265.  * REMARKS
  266.  */
  267. PUBLIC USHORT
  268. GenIOCtl(PRP_GENIOCTL rp)
  269. {
  270.     USHORT rc;
  271.  
  272.     if( rp->Category != IOCTL_DSKPERF_CATEGORY )
  273.     {
  274.     return STATUS_ERR_UNKCMD;
  275.     }
  276.  
  277.     switch( rp->Function )
  278.     {
  279.       case DSKPF_QUERY_VERSION:
  280.     rc = DriverVersion( rp );
  281.     break;
  282.  
  283.       case DSKPF_READ_MSGS:
  284.     rc = ReadMessages( rp );
  285.     break;
  286.  
  287.       case DSKPF_CLEAR_MSGS:
  288.     rc = ClearMessages( rp );
  289.     break;
  290.  
  291.       default:
  292.     rc = STATUS_ERR_UNKCMD;
  293.     break;
  294.     }
  295.  
  296.     return rc;
  297. }
  298.