home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ddkx86v5.zip / DDKX86 / SRC / DEV / PCMCIA / CLDFM / CS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-14  |  13.0 KB  |  435 lines

  1. /*DDK*************************************************************************/
  2. /*                                                                           */
  3. /* COPYRIGHT    Copyright (C) 1995 IBM Corporation                           */
  4. /*                                                                           */
  5. /*    The following IBM OS/2 WARP source code is provided to you solely for  */
  6. /*    the purpose of assisting you in your development of OS/2 WARP device   */
  7. /*    drivers. You may use this code in accordance with the IBM License      */
  8. /*    Agreement provided in the IBM Device Driver Source Kit for OS/2. This  */
  9. /*    Copyright statement may not be removed.                                */
  10. /*                                                                           */
  11. /*****************************************************************************/
  12. /**************************************************************************
  13.  *
  14.  * SOURCE FILE NAME = CS.C
  15.  *
  16.  * DESCRIPTIVE NAME =
  17.  *
  18.  *
  19.  * VERSION =
  20.  *
  21.  * DATE
  22.  *
  23.  * DESCRIPTION
  24.  *
  25.  *
  26.  * FUNCTIONS
  27.  *
  28.  * ENTRY POINTS:
  29.  *
  30.  * DEPENDENCIES:
  31.  *
  32.  * NOTES
  33.  *
  34.  *
  35.  * STRUCTURES
  36.  *
  37.  * EXTERNAL REFERENCES
  38.  *
  39.  * EXTERNAL FUNCTIONS
  40.  *
  41. */
  42.  
  43. #include "types.h"
  44. #include "cs.h"
  45. #include "cs_util.h"
  46. #include "cs_help.h"
  47. #include "config.h"
  48. #include "cs_idc.h"
  49. #include "comport.h"
  50. #include "com_idc.h"
  51.  
  52.    /*
  53.    ** constant definitions
  54.    */
  55. #define CARD_INSERTION 0x40            /* card insertion call back code  */
  56. #define CARD_REMOVAL 0x05              /* card removal call back code    */
  57. #define VENDOR_STR_LEN 25              /* len of string buffer           */
  58.  
  59.    /*
  60.    ** type definitions
  61.    */
  62. typedef void (far *FPFV_V)(void);
  63.  
  64. #if defined (CS_2_0)
  65. typedef struct {
  66.     USHORT info_len;                   /* length of data returned by cs  */
  67.     char signature[2];                 /* ASCII signature                */
  68.     USHORT count;                      /* number of sockets              */
  69.     USHORT revision;                   /* BCD val of CS rev              */
  70.     USHORT release;                    /* BCD val of CS rel              */
  71.     USHORT vendor_str_max_len;         /* length of buffer               */
  72.     USHORT vendor_str_len;             /* Vendor string length           */
  73.     UCHAR vendor_str[VENDOR_STR_LEN];  /* ASCIIZ vendor string           */
  74. } GET_CARD_SERV_INFO;
  75. #endif
  76.  
  77. #if defined (CS_1_7)
  78. typedef struct {                       /* function 0BH                   */
  79.     char signature[2];                 /* ASCII signature                */
  80.     USHORT count;                      /* number of sockets              */
  81.     USHORT revision;                   /* BCD val of CS rev              */
  82.      USHORT release;                   /* BCD val of CS rel              */
  83.     USHORT vendor_str_max_len;         /* length of buffer               */
  84.     USHORT vendor_str_len;             /* Vendor string length           */
  85.     UCHAR vendor_str[VENDOR_STR_LEN];  /* ASCIIZ vendor string           */
  86. } GET_CARD_SERV_INFO;
  87. #endif
  88.  
  89. typedef struct {
  90.     USHORT socket;                     /* logical status                 */
  91.     USHORT card_status;                /* card status  data              */
  92.     USHORT socket_status;              /* socket  status data            */
  93. } GET_STATUS;
  94.  
  95. #if defined (CS_2_0)
  96. typedef struct {
  97.     USHORT attributes;                 /* bit-mapped attribute           */
  98.     USHORT event_mask;                 /* events to notify client        */
  99.     UCHAR client_data[8];
  100.     USHORT version;
  101. } REGISTER_CLIENT;
  102. #endif
  103.  
  104. #if defined (CS_1_7)
  105. typedef struct {
  106.     USHORT attributes;                 /* bit-mapped attribute           */
  107.     USHORT event_mask;                 /* events to notify client        */
  108.     USHORT client_data;                /* data for the client            */
  109.     USHORT client_data_segment;        /* data for the client            */
  110.     USHORT client_data_offset;         /* data for the client            */
  111.     USHORT reserved;                   /* reserved                       */
  112. } REGISTER_CLIENT;
  113. #endif
  114.  
  115.     /*
  116.     ** external global variables
  117.     */
  118. extern CONFIG_INFO Config_info;        /* configuration information      */
  119.  
  120.     /*
  121.     ** private global variables
  122.     */
  123. static USHORT Num_of_sockets;
  124.  
  125.     /*
  126.     ** public global variables
  127.     */
  128. USHORT Client_handle, Resources_acquired[MAX_NUM_IO_ADDRESSES];
  129.  
  130.  
  131. /****************************************************************************
  132.  *
  133.  * FUNCTION NAME = cs_services_present
  134.  *
  135.  * DESCRIPTION   = This function determines if card services is present on the
  136.  *                 system.
  137.  *                 The function returns the value TRUE if card services is
  138.  *                 present.  Otherwise, the value FALSE is returned.
  139.  *
  140.  * INPUT         =
  141.  *
  142.  * OUTPUT        =
  143.  *
  144.  * RETURN-NORMAL =
  145.  *
  146.  * RETURN-ERROR  =
  147.  *
  148.  ****************************************************************************/
  149.  
  150.     GET_CARD_SERV_INFO data1;
  151.     IDC_PACKET         idc_pack1;
  152.  
  153. BOOL cs_services_present(void)
  154. {
  155.         /* initialize signature field - set maximum length for vendor
  156.         ** string - get card services info
  157.         */
  158.     data1.signature[0] = 0;
  159.     data1.signature[1] = 0;
  160.     data1.vendor_str_max_len = VENDOR_STR_LEN;
  161.  
  162.         /* set up codes for get card services information
  163.         */
  164.     idc_pack1.function = 0x0b;
  165.  
  166.     idc_pack1.pointer = (ULONG) 0;
  167.  
  168.         /* set up size of packet
  169.         */
  170.     idc_pack1.arglength = sizeof(GET_CARD_SERV_INFO);
  171.  
  172.         /* set up segment and offset of packet
  173.         */
  174.     idc_pack1.argpointer = (ULONG) (GET_CARD_SERV_INFO far *) &data1;
  175.  
  176.  
  177.     if (do_card_services(&idc_pack1) != SUCCESS)
  178.          return(FALSE);
  179.  
  180.         /* store number of sockets
  181.         */
  182.     Num_of_sockets = data1.count;
  183.  
  184.         /* if card services are present then return true
  185.         */
  186.     if ((data1.signature[0] == 'C') && (data1.signature[1] == 'S'))
  187.           return(TRUE);
  188.  
  189.     return(FALSE);
  190. }
  191.  
  192. /****************************************************************************
  193.  *
  194.  * FUNCTION NAME = cs_card_present
  195.  *
  196.  * DESCRIPTION   = This function determines if a card is present in a
  197.  *                 particular socket.
  198.  *                 The parameter 'socket_num' contains the socket to
  199.  *                 check for a card.
  200.  *                 The function returns the value TRUE if a card is present.
  201.  *                 Otherwise, the function returns the value FALSE.
  202.  *
  203.  * INPUT         =
  204.  *
  205.  * OUTPUT        =
  206.  *
  207.  * RETURN-NORMAL =
  208.  *
  209.  * RETURN-ERROR  =
  210.  *
  211.  ****************************************************************************/
  212.  
  213.     GET_STATUS data2;
  214.     IDC_PACKET idc_pack2;
  215.  
  216. BOOL cs_card_present(USHORT socket_num)
  217. {
  218.         /* set up socket number
  219.         */
  220.     data2.socket = socket_num;
  221.  
  222.         /* set up codes for get status
  223.         */
  224.     idc_pack2.function = 0x0c;
  225.  
  226.     idc_pack2.pointer = (ULONG) 0;
  227.  
  228.         /* set up size of packet
  229.         */
  230.     idc_pack2.arglength = sizeof(GET_STATUS);
  231.  
  232.         /* set up segment and offset for packet
  233.         */
  234.     idc_pack2.argpointer = (ULONG) (GET_STATUS far *) &data2;
  235.  
  236.  
  237.     if (do_card_services(&idc_pack2) != SUCCESS)
  238.        return(FALSE);
  239.  
  240.         /* return true if card present bit is set or false if clear
  241.         */
  242.     if ((data2.card_status & 0x0080)  == 0x0080)
  243.        return(TRUE);
  244.  
  245.     return(FALSE);
  246. }
  247.  
  248. /****************************************************************************
  249.  *
  250.  * FUNCTION NAME = cs_register_client
  251.  *
  252.  * DESCRIPTION   = This function registers as a client with card services.
  253.  *                 The function returns an error code in parameter 'statusP'
  254.  *                 if an error  occurs.
  255.  *
  256.  * INPUT         =
  257.  *
  258.  * OUTPUT        =
  259.  *
  260.  * RETURN-NORMAL = 0
  261.  *
  262.  * RETURN-ERROR  = -1
  263.  *
  264.  ****************************************************************************/
  265.  
  266.     REGISTER_CLIENT data3;
  267.     IDC_PACKET      idc_pack3;
  268.  
  269. USHORT cs_register_client(void)
  270.  
  271. {
  272.     USHORT ret_code;
  273.  
  274.         /* set up attributes - memory client device driver - generate
  275.         ** artificial insertion events
  276.         */
  277.     data3.attributes = 0x001c;
  278.  
  279.         /* set up event mask - global notification of insertion and
  280.         ** ejection events
  281.         */
  282.     data3.event_mask = 0x0081;
  283.  
  284.         /* set up codes for register client
  285.         */
  286.     idc_pack3.function = 0x10;
  287.  
  288.         /* set up segment and offset of call back handler routine
  289.         */
  290.     idc_pack3.pointer = (ULONG) (FPFV_V) callback_handler;
  291.  
  292.         /* set up size of packet
  293.         */
  294.     idc_pack3.arglength = sizeof(REGISTER_CLIENT);
  295.  
  296.         /* set up segment and offset of packet
  297.         */
  298.     idc_pack3.argpointer = (ULONG) (REGISTER_CLIENT far *) &data3;
  299.  
  300.  
  301.     if ((ret_code = do_card_services(&idc_pack3)) != SUCCESS)
  302.           return(ret_code);
  303.  
  304.         /* store client handle
  305.         */
  306.     Client_handle = idc_pack3.handle;
  307.  
  308.         /* return success
  309.         */
  310.     return(SUCCESS);
  311. }
  312.  
  313. /****************************************************************************
  314.  *
  315.  * FUNCTION NAME = service_callback
  316.  *
  317.  * DESCRIPTION   = This function services card insertion and card removal
  318.  *                 callback events from card services
  319.  *
  320.  * INPUT         =
  321.  *
  322.  * OUTPUT        =
  323.  *
  324.  * RETURN-NORMAL =
  325.  *
  326.  * RETURN-ERROR  =
  327.  *
  328.  ****************************************************************************/
  329.  
  330. void service_callback(USHORT socket_num, USHORT event_num)
  331. {
  332.         /*
  333.         ** release resources if a card removal event occurs
  334.         ** acquire resources if card insertion event occurs
  335.         **                    - check for modem card - release
  336.         ** resources if not modem card
  337.         ** acquire resources if card insertion event occurs -  display
  338.         ** critical message if unsuccessful - release resources an display
  339.         ** critical message if it's not a modem card - display informational
  340.         ** message if everythings ok
  341.         */
  342.     switch (event_num)
  343.     {
  344.         case CARD_REMOVAL:
  345.             if (Resources_acquired[socket_num - 1] == FALSE)
  346.                 return;
  347.             if (cs_release_resource(socket_num,
  348.                              Config_info.io_address[socket_num - 1],
  349.                              Config_info.irq_num[socket_num - 1]) != SUCCESS)
  350.             {
  351.                 return;
  352.             }
  353.  
  354.             set_com_port(0, Config_info.com_port_num[socket_num - 1],
  355.                          Config_info.io_address[socket_num - 1],
  356.                          Config_info.irq_num[socket_num - 1]);
  357.  
  358.             release_com_port(Config_info.io_address[socket_num - 1]);
  359.  
  360.             Resources_acquired[socket_num - 1] = FALSE;
  361.             return;
  362.  
  363.         case CARD_INSERTION:
  364.             if (Config_info.io_address[socket_num - 1] == 0)
  365.                 return;
  366.  
  367.             if (!cs_is_modem_card(socket_num))
  368.             {
  369.                 return;
  370.             }
  371.  
  372.             if (cs_acquire_resource(socket_num,
  373.                               Config_info.io_address[socket_num - 1],
  374.                               Config_info.irq_num[socket_num - 1]) != SUCCESS)
  375.             {
  376.                 return;
  377.             }
  378.  
  379.             if (set_com_port(1, Config_info.com_port_num[socket_num - 1],
  380.                          Config_info.io_address[socket_num - 1],
  381.                          Config_info.irq_num[socket_num - 1]) != SUCCESS)
  382.             {
  383.                 cs_release_resource(socket_num,
  384.                               Config_info.io_address[socket_num - 1],
  385.                               Config_info.irq_num[socket_num - 1]);
  386.                 return;
  387.             }
  388.  
  389.             if (acquire_com_port(Config_info.io_address[socket_num - 1])
  390.                                                                 != SUCCESS)
  391.             {
  392.                 cs_release_resource(socket_num,
  393.                               Config_info.io_address[socket_num - 1],
  394.                               Config_info.irq_num[socket_num - 1]);
  395.  
  396.  
  397.                 set_com_port(0, Config_info.com_port_num[socket_num - 1],
  398.                              Config_info.io_address[socket_num - 1],
  399.                              Config_info.irq_num[socket_num - 1]);
  400.                 return;
  401.  
  402.             }
  403.  
  404.             Resources_acquired[socket_num - 1] = TRUE;
  405.  
  406.             return;
  407.      }
  408. }
  409.  
  410.  
  411. /****************************************************************************
  412.  *
  413.  * FUNCTION NAME = cs_get_num_of_sockets
  414.  *
  415.  * DESCRIPTION   = This function reports the number of sockets present in the
  416.  *                 system.
  417.  *                 The function returns the number of sockets.
  418.  *
  419.  * INPUT         =
  420.  *
  421.  * OUTPUT        =
  422.  *
  423.  * RETURN-NORMAL =
  424.  *
  425.  * RETURN-ERROR  =
  426.  *
  427.  ****************************************************************************/
  428.  
  429. USHORT cs_get_num_of_sockets(void)
  430. {
  431.         /* return the number of sockets
  432.         */
  433.     return(Num_of_sockets);
  434. }
  435.