home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CTASK22.ZIP / TSKSIO.C < prev    next >
C/C++ Source or Header  |  1990-10-12  |  26KB  |  967 lines

  1. /*
  2.    --- Version 2.2 90-10-12 10:33 ---
  3.  
  4.    TSKSIO.C - CTask - Serial I/O interface routines.
  5.  
  6.    Public Domain Software written by
  7.       Thomas Wagner
  8.       Ferrari electronic Gmbh
  9.       Beusselstrasse 27
  10.       D-1000 Berlin 21
  11.       Germany
  12.  
  13.    With version 1.1, support for shared IRQ lines and dynamically
  14.    defined ports was added. The tables used are
  15.  
  16.       port_list   The pointer to the list of defined ports. Newly
  17.                   defined ports are added to the end of the list.
  18.                   Ports can never be deleted. Each port descriptor
  19.                   contains the hardware info for the port, plus a
  20.                   pointer to the associated sio-control-block if the
  21.                   port was initialized.
  22.                   
  23.       port_last   The pointer to the last element in port_list.
  24.  
  25.       irq_array   Is an array of pointers to sio-control-blocks. For
  26.                   each possible IRQ-line the entry in this table points
  27.                   to the first block active for this line. If the IRQ
  28.                   is shared, sio-blocks are chained into this list via 
  29.                   their "next" pointer.
  30.  
  31.       irq_procs   Contains the pointer to the interrupt handler function
  32.                   for the corresponding IRQ-line.
  33.  
  34.       sio_data    Contains the statically defined sio control blocks.
  35.       port_descr  Contains the statically defined port descriptor blocks.
  36.  
  37.    NOTE:    You can not dynamically define ports for IRQ-lines that
  38.             have no interrupt handler function defined. To be completely
  39.             flexible, you would have to define sioint-handlers for all
  40.             possible IRQs and enter the addresses into the irq_procs array.
  41.  
  42.    CAUTION: Please restrict the installation and removal of v24-
  43.             ports to *one* task. The manipulation of the lists is
  44.             not protected, so simultaneous install/remove calls
  45.             may cause trouble. Since ports are normally installed and
  46.             removed in the main task, the protection of critical regions
  47.             seemed unnecessary.
  48.  
  49.    CAUTION: Shared interrupt logic and IRQ-lines above 4 were not
  50.             tested due to the lack of suitable hardware. Changes may
  51.             be necessary in the handling of the modem control register
  52.             OUT2 line that normally controls interrupt enable, depending
  53.             on the multiport-hardware installed in the target system.
  54.  
  55.    With Version 1.2, special interrupt handler functions have been added
  56.    in a separate assembler file, TSKSIOI.ASM. These functions switch to
  57.    a local stack, and then call the sioint-functions defined here, which
  58.    no longer have the interrupt attribute.
  59.  
  60. */
  61.  
  62. #include "tsk.h"
  63. #include "tsklocal.h"
  64. #include "sio.h"
  65.  
  66. #if (DOS)
  67.  
  68. #define MAX_IRQ   16    /* Maximum number of interrupt lines, 
  69.                            16 for AT, 8 for XT. Can be left at 16 */
  70.  
  71. #define CHAIN_IRQBIT    0x04  /* Chained int controller IRQ bit */
  72.  
  73. #define RESTORE_DEFAULT 1     /* Restore parameter for remove_all */
  74.  
  75. #define RTS       0x02
  76. #define DTR       0x01
  77. #define OUT2      0x08
  78.  
  79. #define ERR_MASK  0x1e
  80.  
  81. #define inta00    0x20   /* 8259 interrupt controller control-port */
  82. #define inta01    0x21   /* 8259 interrupt controller mask-port */
  83.  
  84. #define inta10    0xa0   /* secondary 8259 control-port for IRQ 8-15 */
  85. #define inta11    0xa1   /* secondary 8259 mask-port for IRQ 8-15 */
  86.  
  87. #define rdserv    0x0b   /* read service register control value */
  88. #define eoi       0x20   /* end of interrupt signal for 8259 */
  89.  
  90. #define intdata   0x0b   /* Enable Interrupts except Line status */
  91.  
  92. #define rxreadybit 0x01
  93. #define txemptybit 0x40
  94. #define txreadybit 0x20
  95. #define framingbit 0x08
  96. #define breakbit   0x10
  97.  
  98. /* Note: In version 1.1, port offsets start at 0, not 8 */
  99.  
  100. #define linecontrol  0x03
  101. #define linestatus   0x05
  102. #define intid        0x02
  103. #define intenable    0x01
  104. #define modemcontrol 0x04
  105. #define modemstatus  0x06
  106. #define receivedata  0x00
  107. #define transmitdata 0x00
  108.  
  109. #define baudreg_dll  0x00     /* baud rate least significant byte */
  110. #define baudreg_dlm  0x01     /* baud rate most significant byte */
  111.  
  112. /*
  113.    Default values for initialising the ports.
  114.    Change to your liking (but remember that OUT2 must be set in the
  115.    modem control register to allow interrupts to come through with 
  116.    normal controllers).
  117. */
  118.  
  119. #define dflt_modcon 0x0b   /* Modem Control: Activate DTR, RTS, OUT2 */
  120. #define dflt_baud   96     /* Baud Rate Divisor: 1200 Baud */
  121. #define dflt_lcon   0x03   /* Line Control: No Parity, 1 Stop, 8 Data */
  122.  
  123. /*
  124.    Defined baud rates. You may expand this table with non-standard
  125.    rates if desired.
  126. */
  127.  
  128. local long baud_table [] = {
  129.                                50L, 2304L,
  130.                                75L, 1536L,
  131.                               110L, 1047L,
  132.                               134L,  857L,
  133.                               150L,  768L,
  134.                               300L,  384L,
  135.                               600L,  192L,
  136.                              1200L,   96L,
  137.                              1800L,   64L,
  138.                              2000L,   58L,
  139.                              2400L,   48L,
  140.                              3600L,   32L,
  141.                              4800L,   24L,
  142.                              7200L,   16L,
  143.                              9600L,   12L,
  144.                             19200L,    6L,
  145.                             38400L,    3L,
  146.                                 0L,    0L };
  147.  
  148. local byte force_transmit_ready;       /* flag to indicate
  149.                                           transmitter needs service */
  150.  
  151. /*-------------------------------------------------------------------------*/
  152.  
  153. /*
  154.    To add static support for other COM-Ports, define
  155.       - Port base
  156.       - IRQ-Line
  157.       - Interrupt vector
  158.    here, and add the necessary data to the port_descr array.
  159.    If the port does *not* share an IRQ with the predefined ports,
  160.    define the corresponding interrupt function by duplicating both siointx
  161.    here and _tsksio_intx in the TSKSIOI.ASM file, and place the
  162.    entry for the _tsksio_intx function into the irq_procs array.
  163.  
  164.    Note that ports may also be defined on-line if TSK_DYNAMIC is enabled.
  165. */
  166.  
  167. #define STATIC_PORTS  2       /* Number of statically defined ports */
  168.  
  169. /* Note: In version 1.1, port offsets start at 0, not 8 */
  170.  
  171. #define com1_base    0x3f8    /* COM1 port base */
  172. #define com2_base    0x2f8    /* COM2 port base */
  173.  
  174. #define com1_irq     4        /* IRQ-Line for COM1 */
  175. #define com2_irq     3        /* IRQ-Line for COM2 */
  176.  
  177. #define com1_vect    0x0c     /* Interrupt vector for COM1 */
  178. #define com2_vect    0x0b     /* Interrupt vector for COM2 */
  179.  
  180. /*-------------------------------------------------------------------------*/
  181.  
  182.  
  183. extern void interrupt far tsksio_int2 (void);
  184. extern void interrupt far tsksio_int3 (void);
  185. extern void interrupt far tsksio_int4 (void);
  186. extern void interrupt far tsksio_int5 (void);
  187. extern void interrupt far tsksio_int7 (void);
  188. extern void interrupt far tsksio_int10 (void);
  189. extern void interrupt far tsksio_int11 (void);
  190. extern void interrupt far tsksio_int12 (void);
  191. extern void interrupt far tsksio_int15 (void);
  192.  
  193. /* 
  194.    Table of Interrupt handler functions for each IRQ line.
  195. */
  196.  
  197. local intprocptr irq_procs [MAX_IRQ] = {  LNULL,         /* IRQ 0 */
  198.                                           LNULL,         /* IRQ 1 */
  199.                                           tsksio_int2,   /* IRQ 2 */
  200.                                           tsksio_int3,   /* IRQ 3 */
  201.                                           tsksio_int4,   /* IRQ 4 */
  202.                                           tsksio_int5,   /* IRQ 5 */
  203.                                           LNULL,         /* IRQ 6 */
  204.                                           tsksio_int7,   /* IRQ 7 */
  205.                                           LNULL,         /* IRQ 8 */
  206.                                           LNULL,         /* IRQ 9 */
  207.                                           tsksio_int10,  /* IRQ 10 */
  208.                                           tsksio_int11,  /* IRQ 11 */
  209.                                           tsksio_int12,  /* IRQ 12 */
  210.                                           LNULL,         /* IRQ 13 */
  211.                                           LNULL,         /* IRQ 14 */
  212.                                           tsksio_int15   /* IRQ 15 */
  213.                                           };
  214.  
  215.  
  216. local sio_datarec sio_data [STATIC_PORTS];
  217.  
  218. /* When adding entries to port_descr, be sure to chain the 
  219.    elements in ascending order via the first field, and to
  220.    increase the internal port number in the second field. */
  221.  
  222. local port_data port_descr [STATIC_PORTS] = {
  223.      { &port_descr[1], 0, LNULL, com1_base, com1_irq, com1_vect },
  224.      { LNULL,          1, LNULL, com2_base, com2_irq, com2_vect }
  225.                                             };
  226.  
  227. local sioptr  irq_array [MAX_IRQ] = { LNULL };
  228.  
  229. local portptr port_list = &port_descr [0];
  230. local portptr port_last = &port_descr [1];
  231.  
  232. local int ports = STATIC_PORTS;
  233. local callchain remove_chain = { LNULL, LNULL };
  234.  
  235. /*-------------------------------------------------------------------------*/
  236.  
  237.  
  238. local void Staticfunc change_rts (sioptr data, int on)
  239. {
  240.    data->rtsoff = (byte)(!on);
  241.    data->cmodcontrol = (byte)((data->cmodcontrol & ~RTS) | ((on) ? RTS : 0));
  242.    tsk_outp (data->port_base + modemcontrol, data->cmodcontrol);
  243. }
  244.  
  245.  
  246. local void Staticfunc transmit_ready (sioptr data)
  247. {
  248.    int i;
  249. #if (TSK_MSC)
  250.    int temp;
  251. #endif
  252.  
  253.    force_transmit_ready = 0;
  254.  
  255.    if ((i = data->r_xoff) < 0)
  256.       {
  257. #if (TSK_MSC)
  258.       temp = (i == -1) ? XOFF : XON;
  259.       tsk_outp (data->port_base + transmitdata, temp);
  260. #else
  261.       /* NOTE: Microsoft C 5.0 generates an "Internal Compiler Error"
  262.                when compiling the following statement.
  263.       */
  264.       tsk_outp (data->port_base + transmitdata, (i == -1) ? XOFF : XON);
  265. #endif
  266.       data->r_xoff = (i == -1) ? 1 : 0;
  267.       data->xmit_pending = 1;
  268.       return;
  269.       }
  270.  
  271.    data->xmit_pending = 0;
  272.  
  273.    if ((data->wait_xmit = (byte)(check_pipe (&data->xmit_pipe) != -1)) == 0)
  274.       return;
  275.  
  276.    if ((data->modem_flags & data->modstat) ^ data->modem_flags)
  277.       return;
  278.  
  279.    if (data->flags & XONXOFF && data->t_xoff)
  280.       return;
  281.  
  282.    data->wait_xmit = 0;
  283.  
  284.    if ((i = c_read_pipe (&data->xmit_pipe)) < 0)
  285.       return;
  286.  
  287.    tsk_outp (data->port_base + transmitdata, (byte)i);
  288.    data->xmit_pending = 1;
  289. }
  290.  
  291.  
  292. local void Staticfunc modem_status_int (sioptr data)
  293. {
  294.    data->modstat = tsk_inp (data->port_base + modemstatus);
  295.  
  296.    if (data->wait_xmit)
  297.       transmit_ready (data);
  298. }
  299.  
  300.  
  301. local void Staticfunc receive_ready (sioptr data)
  302. {
  303.    word status;
  304.    word ch;
  305.  
  306.    while ((status = tsk_inp (data->port_base + linestatus)) & rxreadybit)
  307.       {
  308.  
  309.       /* Correct for possible loss of transmit interrupt from IIR register */   
  310.       if (status & txreadybit)
  311.           force_transmit_ready = 1;
  312.  
  313.       tsk_nop ();
  314.       ch = tsk_inp (data->port_base + receivedata);
  315.  
  316.       if (data->flags & XONXOFF)
  317.          {
  318.          if (ch == XON)
  319.             {
  320.             data->t_xoff = 0;
  321.             if (data->wait_xmit)
  322.                transmit_ready (data);
  323.             continue;
  324.             }
  325.          else if (ch == XOFF)
  326.             {
  327.             data->t_xoff = 1;
  328.             continue;
  329.             }
  330.          if (!data->r_xoff && 
  331.              wpipe_free (&data->rcv_pipe) < data->xoff_threshold)
  332.             {
  333.             data->r_xoff = -1;
  334.             if (!data->xmit_pending)
  335.                transmit_ready (data);
  336.             }
  337.          }
  338.  
  339.       if (data->flags & RTSCTS && !data->rtsoff)
  340.          if (wpipe_free (&data->rcv_pipe) < data->xoff_threshold)
  341.             change_rts (data, 0);
  342.  
  343.       status = (status & ERR_MASK) << 8;
  344.       if (c_write_wpipe (&data->rcv_pipe, ch | status) < 0)
  345.          data->overrun = 1;
  346.       }
  347. }
  348.  
  349.  
  350. /*-------------------------------------------------------------------------*/
  351.  
  352.  
  353. local void Staticfunc sioint (sioptr data)
  354. {
  355.    int id;
  356.  
  357.    force_transmit_ready = 0;
  358.  
  359.    while (!((id = tsk_inp (data->port_base + intid)) & 1))
  360.       switch (id & 0x07)
  361.          {
  362.          case 0x00:  modem_status_int (data);
  363.                      break;
  364.  
  365.          case 0x02:  transmit_ready (data);
  366.                      break;
  367.  
  368.          case 0x04:  receive_ready (data);
  369.                      break;
  370.  
  371. /*       case 0x06:  line_status_int (data); (currently not used)
  372.                      break;
  373. */
  374.          }
  375.       if (force_transmit_ready)
  376.          transmit_ready (data);
  377. }
  378.  
  379.  
  380. void Localfunc tsk_sio_int (int irq)
  381. {
  382.    sioptr curr;
  383.  
  384.    for (curr = irq_array [irq]; curr != LNULL; curr = curr->next)
  385.       sioint (curr);
  386. }
  387.  
  388.  
  389. /*-------------------------------------------------------------------------*/
  390.  
  391. local void Taskfunc v24_chain_remove (callchainptr chain)
  392. {
  393.    /* 
  394.       We just assume here that all local data is in the same segment.
  395.       This should be true for all compilers and memory models
  396.       unless you define an extremely low data threshold.
  397.    */
  398.    v24_remove_all ();
  399. }
  400.  
  401.  
  402. int Globalfunc v24_define_port (int base, byte irq, byte vector)
  403. {
  404. #if (TSK_DYNAMIC)
  405.    portptr portp;
  406.  
  407.    if (irq >= MAX_IRQ)
  408.       return -1; 
  409.    if (irq_procs [irq] == LNULL)
  410.       return -1; 
  411.  
  412.    if ((portp = tsk_palloc (sizeof (port_data))) == LNULL)
  413.       return -1;
  414.    portp->pnum = ports;
  415.    portp->base = base;
  416.    portp->irq = irq;
  417.    portp->vector = vector;
  418.    portp->next = LNULL;
  419.    portp->sio = LNULL;
  420.  
  421.    if (port_list == LNULL)
  422.       port_list = portp;
  423.    else
  424.       port_last->next = portp;
  425.  
  426.    port_last = portp;
  427.    ports++;
  428.  
  429.    return portp->pnum;
  430.  
  431. #else
  432.    return -1;
  433. #endif
  434. }
  435.  
  436.  
  437. local sioptr Staticfunc ret_error (sioptr sio)
  438. {
  439.    sio->port->sio = LNULL;
  440. #if (TSK_DYNAMIC)
  441.    if (sio->port->pnum >= STATIC_PORTS)
  442.       tsk_pfree (sio);
  443. #endif
  444.    return LNULL; 
  445. }
  446.  
  447.  
  448. sioptr Globalfunc v24_install (int port, int init,
  449.                         farptr rcvbuf, word rcvsize,
  450.                         farptr xmitbuf, word xmitsize)
  451. {
  452.    sioptr sio;
  453.    portptr portp;
  454.    int pbase;
  455.    intprocptr far *intptr;
  456.    int i, inta;
  457.  
  458. #if (TSK_NAMEPAR)
  459.    static char xname [] = "SIOnXMIT", rname [] = "SIOnRCV";
  460.  
  461.    xname [3] = rname [3] = (char)((port & 0x7f) + '0');
  462. #endif
  463.  
  464.    if (port < 0 || !rcvsize || !xmitsize)
  465.       return LNULL;
  466.  
  467.    portp = port_list;
  468.  
  469.    if (port & 0x80)     /* Relative port number */
  470.       {
  471.       port &= 0x7f;
  472.       if (port > 4)
  473.          return LNULL; 
  474.       pbase = *((wordptr)(TMK_FP (0x40, port * 2)));
  475.       if (!pbase)
  476.          return LNULL;
  477.  
  478.       for (port = 0; port < ports; port++, portp = portp->next)
  479.          if (portp->base == pbase)
  480.             break;
  481.  
  482.       if (port >= ports)
  483.          return LNULL;
  484.       }
  485.    else 
  486.       {
  487.       if (port > ports)
  488.          return LNULL;
  489.       for (i = 0; i < port; i++)
  490.          portp = portp->next;
  491.       }
  492.  
  493.    if (portp->sio != LNULL) /* Port already in use ? */
  494.       return LNULL;
  495.  
  496.    if (port < STATIC_PORTS)
  497.       portp->sio = sio = &sio_data [port];
  498.    else
  499. #if (TSK_DYNAMIC)
  500.       if ((portp->sio = sio = tsk_palloc (sizeof (sio_datarec))) == LNULL)
  501.          return LNULL;
  502. #else
  503.       return LNULL;
  504. #endif
  505.  
  506.    pbase = sio->port_base = portp->base;
  507.    sio->port = portp;
  508.  
  509.    /* Check if port accessible by modifying the modem control register */
  510.  
  511.    i = sio->cmodcontrol = sio->save_mcon = tsk_inp (pbase + modemcontrol);
  512.    if (i & 0xe0)
  513.       return ret_error (sio);
  514.    tsk_nop ();
  515.    tsk_outp (pbase + modemcontrol, 0xe0 | i);
  516.    tsk_nop ();
  517.    if (tsk_inp (pbase + modemcontrol) != (byte) i)
  518.       return ret_error (sio);
  519.  
  520.    /* Port seems OK */
  521.  
  522.    if (create_pipe (&sio->xmit_pipe, xmitbuf, xmitsize TN(xname)) == LNULL)
  523.       return ret_error (sio);
  524.  
  525.    if (create_wpipe (&sio->rcv_pipe, rcvbuf, rcvsize TN(rname)) == LNULL)
  526.       {
  527.       delete_pipe (&sio->xmit_pipe);
  528.       return ret_error (sio);
  529.       }
  530.  
  531.    sio->civect = portp->vector;
  532.    sio->irqbit = (byte)(1 << (portp->irq & 0x07));
  533.  
  534.    sio->wait_xmit = sio->xmit_pending = 0;
  535.    sio->overrun = 0;
  536.    sio->flags = 0;
  537.    sio->modem_flags = 0;
  538.    sio->r_xoff = sio->t_xoff = 0;
  539.    sio->rtsoff = 0;
  540.  
  541.    sio->clcontrol = sio->save_lcon = tsk_inp (pbase + linecontrol);
  542.    tsk_nop ();
  543.    sio->save_inten = tsk_inp (pbase + intenable);
  544.    tsk_nop ();
  545.  
  546.    if (init)
  547.       {
  548.       sio->clcontrol = dflt_lcon;
  549.       sio->cmodcontrol = dflt_modcon;
  550.       }
  551.  
  552.    tsk_outp (pbase + linecontrol, sio->clcontrol | 0x80);
  553.    tsk_nop ();
  554.    sio->save_bd1 = tsk_inp (pbase + baudreg_dll);
  555.    tsk_nop ();
  556.    sio->save_bd2 = tsk_inp (pbase + baudreg_dlm);
  557.    tsk_nop ();
  558.    tsk_outp (pbase + linecontrol, sio->clcontrol);
  559.    tsk_nop ();
  560.  
  561.    tsk_outp (pbase + intenable, 0);
  562.  
  563.    if (irq_array [portp->irq] == LNULL)
  564.       {
  565.       intptr = (intprocptr far *)TMK_FP (0, sio->civect * 4);
  566.       tsk_cli ();
  567.       sio->savvect = *intptr;
  568.       *intptr = irq_procs [portp->irq];
  569.       tsk_sti ();
  570.       }
  571.  
  572.    if (init)
  573.       {
  574.       tsk_outp (pbase + linecontrol, dflt_lcon | 0x80);
  575.       tsk_nop ();
  576.       tsk_outp (pbase + baudreg_dll, dflt_baud);
  577.       tsk_nop ();
  578.       tsk_outp (pbase + baudreg_dlm, dflt_baud >> 8);
  579.       tsk_nop ();
  580.       tsk_outp (pbase + linecontrol, dflt_lcon);
  581.       tsk_nop ();
  582.       tsk_outp (pbase + modemcontrol, dflt_modcon);
  583.       tsk_nop ();
  584.       }
  585.    else
  586.       {
  587.       i = tsk_inp (pbase + modemcontrol) | OUT2;
  588.       tsk_nop ();
  589.       tsk_outp (pbase + modemcontrol, i);
  590.       tsk_nop ();
  591.       }
  592.  
  593.    while (tsk_inp (pbase + linestatus) & rxreadybit)
  594.       {
  595.       tsk_nop ();
  596.       tsk_inp (pbase + receivedata);
  597.       tsk_nop ();
  598.       }
  599.    tsk_nop ();
  600.  
  601.    tsk_inp (pbase + linestatus);
  602.    tsk_nop ();
  603.    sio->modstat = tsk_inp (pbase + modemstatus);
  604.    tsk_nop ();
  605.    tsk_inp (pbase + intid);
  606.    tsk_nop ();
  607.  
  608.    inta = (portp->irq > 7) ? inta11 : inta01;
  609.  
  610.    if (irq_array [portp->irq] == LNULL)
  611.       {
  612.       if (portp->irq > 7)
  613.          {
  614.          i = tsk_inp (inta01) & ~CHAIN_IRQBIT;
  615.          tsk_nop ();
  616.          tsk_outp (inta01, i);
  617.          }
  618.  
  619.       sio->save_irq = (byte)((i = tsk_inp (inta)) & sio->irqbit);
  620.       tsk_nop ();
  621.       tsk_outp (inta, i & ~sio->irqbit);
  622.       }
  623.    else
  624.       sio->save_irq = (irq_array [portp->irq])->save_irq;
  625.  
  626.    tsk_cli ();
  627.    sio->next = irq_array [portp->irq];
  628.    irq_array [portp->irq] = sio;
  629.    tsk_sti ();
  630.  
  631.    if (remove_chain.func == LNULL)
  632.       chain_removefunc (v24_chain_remove, &remove_chain, NULL);
  633.  
  634.    /* Enable interrupts with correction for possible loss of the 
  635.       first tranmit interrupt on INS8250 and INS8250-B chips */
  636.    for (;;)
  637.        {
  638.        if (tsk_inp (pbase + linestatus) & txreadybit)
  639.            {
  640.            break;
  641.            }
  642.        tsk_nop ();
  643.        }
  644.    tsk_nop ();
  645.    tsk_cli ();
  646.    tsk_outp (pbase + intenable, intdata);
  647.    tsk_nop ();
  648.    tsk_outp (pbase + intenable, intdata);
  649.    tsk_sti ();
  650.  
  651.    return sio;
  652.    }
  653.  
  654.  
  655. void Globalfunc v24_remove (sioptr sio, int restore)
  656.    {
  657.    intprocptr far *intptr;
  658.    int pbase, i, inta;
  659.    portptr portp;
  660.    sioptr curr, last;
  661.  
  662.    pbase = sio->port_base;
  663.    portp = sio->port;
  664.  
  665.    last = LNULL;
  666.    curr = irq_array [portp->irq];
  667.    while (curr != sio && curr != LNULL)
  668.       {
  669.       last = curr;
  670.       curr = curr->next;
  671.       }
  672.    if (curr == LNULL)
  673.       return;
  674.  
  675.    tsk_outp (pbase + intenable, 0);
  676.    tsk_cli ();
  677.    if (last == LNULL)
  678.       irq_array [portp->irq] = sio->next;
  679.    else
  680.       last->next = sio->next;
  681.    tsk_sti ();
  682.  
  683.    inta = (portp->irq > 7) ? inta11 : inta01;
  684.  
  685.    if (restore)
  686.       {
  687.       tsk_outp (pbase + modemcontrol, sio->save_mcon);
  688.       tsk_nop ();
  689.       tsk_outp (pbase + linecontrol, sio->save_lcon | 0x80);
  690.       tsk_nop ();
  691.       tsk_outp (pbase + baudreg_dll, sio->save_bd1);
  692.       tsk_nop ();
  693.       tsk_outp (pbase + baudreg_dlm, sio->save_bd2);
  694.       tsk_nop ();
  695.       tsk_outp (pbase + linecontrol, sio->save_lcon);
  696.       tsk_nop ();
  697.       if (irq_array [portp->irq] == LNULL)
  698.          {
  699.          tsk_cli ();
  700.          tsk_outp (pbase + intenable, sio->save_inten);
  701.          i = tsk_inp (inta) & ~sio->irqbit;
  702.          tsk_nop ();
  703.          tsk_outp (inta, i | sio->save_irq);
  704.          }
  705.       }
  706.    else if (irq_array [portp->irq] == LNULL)
  707.       {
  708.       tsk_cli ();
  709.       i = tsk_inp (inta) | sio->irqbit;
  710.       tsk_nop ();
  711.       tsk_outp (inta, i);
  712.       }
  713.  
  714.    if (irq_array [portp->irq] == LNULL)
  715.       {
  716.       tsk_cli ();
  717.       intptr = (intprocptr far *)TMK_FP (0, sio->civect * 4);
  718.       *intptr = sio->savvect;
  719.       }
  720.    tsk_sti ();
  721.  
  722.    portp->sio = LNULL;
  723.    delete_pipe (&sio->xmit_pipe);
  724.    delete_wpipe (&sio->rcv_pipe);
  725.  
  726. #if (TSK_DYNAMIC)
  727.    if (portp->pnum >= STATIC_PORTS)
  728.       tsk_pfree (sio);
  729. #endif
  730.    }
  731.  
  732.  
  733. void Taskfunc v24_remove_all (void)
  734. {
  735.    int i;
  736.    sioptr sio;
  737.  
  738.    unchain_removefunc (&remove_chain);
  739.  
  740.    for (i = 0; i < MAX_IRQ; i++)
  741.       {
  742.       while ((sio = irq_array [i]) != LNULL)
  743.          v24_remove (sio, RESTORE_DEFAULT);
  744.       }
  745. }
  746.  
  747.  
  748. /*-------------------------------------------------------------------------*/
  749.  
  750. /*
  751.    void v24_change_rts (sioptr sio, int on)
  752. */
  753.  
  754. void Globalfunc v24_change_rts (sioptr sio, int on)
  755. {
  756.    sio->cmodcontrol = (byte)((sio->cmodcontrol & ~RTS) | ((on) ? RTS : 0));
  757.    tsk_outp (sio->port_base + modemcontrol, sio->cmodcontrol);
  758. }
  759.  
  760. /*
  761.    void v24_change_dtr (sioptr sio, int on)
  762. */
  763.  
  764. void Globalfunc v24_change_dtr (sioptr sio, int on)
  765. {
  766.    sio->cmodcontrol = (byte)((sio->cmodcontrol & ~DTR) | ((on) ? DTR : 0));
  767.    tsk_outp (sio->port_base + modemcontrol, sio->cmodcontrol);
  768. }
  769.  
  770.  
  771. /*
  772.    void Globalfunc v24_change_baud (sioptr sio, int rate)
  773. */
  774.  
  775. void Globalfunc v24_change_baud (sioptr sio, long rate)
  776. {
  777.    int i;
  778.  
  779.    for (i = 0; baud_table [i]; i += 2)
  780.       if (baud_table [i] == rate)
  781.          break;
  782.    if ((i = (int)baud_table [i + 1]) == 0)
  783.       return;
  784.  
  785.    tsk_outp (sio->port_base + linecontrol, sio->clcontrol | (byte)0x80);
  786.    tsk_nop ();
  787.    tsk_outp (sio->port_base + baudreg_dll, (byte)i);
  788.    tsk_nop ();
  789.    tsk_outp (sio->port_base + baudreg_dlm, (byte)(i >> 8));
  790.    tsk_nop ();
  791.    tsk_outp (sio->port_base + linecontrol, sio->clcontrol);
  792. }
  793.  
  794.  
  795. void Globalfunc v24_change_parity (sioptr sio, int par)
  796. {
  797.    sio->clcontrol = (byte)((sio->clcontrol & 0xc7) | par);
  798.    tsk_outp (sio->port_base + linecontrol, sio->clcontrol);
  799. }
  800.  
  801.  
  802. void Globalfunc v24_change_wordlength (sioptr sio, int len)
  803. {
  804.    int i;
  805.  
  806.    switch (len)
  807.       {
  808.       case 5:  i = 0x00; break;
  809.       case 6:  i = 0x01; break;
  810.       case 7:  i = 0x02; break;
  811.       case 8:  i = 0x03; break;
  812.       default: return;
  813.       }
  814.    sio->clcontrol = (byte)((sio->clcontrol & 0xfc) | i);
  815.    tsk_outp (sio->port_base + linecontrol, sio->clcontrol);
  816. }
  817.  
  818.  
  819. void Globalfunc v24_change_stopbits (sioptr sio, int n)
  820. {
  821.    int i;
  822.  
  823.    switch (n)
  824.       {
  825.       case 1:  i = 0x00; break;
  826.       case 2:  i = 0x04; break;
  827.       default: return;
  828.       }
  829.    sio->clcontrol = (byte)((sio->clcontrol & 0xfb) | i);
  830.    tsk_outp (sio->port_base + linecontrol, sio->clcontrol);
  831. }
  832.  
  833.  
  834. void Globalfunc v24_watch_modem (sioptr sio, byte flags)
  835. {
  836.    sio->modem_flags = (byte)(flags & (CTS | DSR | RI | CD));
  837. }
  838.  
  839.  
  840. void Globalfunc v24_protocol (sioptr sio, int prot, word offthresh, word onthresh)
  841. {
  842.    byte old;
  843.    
  844.    old = sio->flags;
  845.    sio->flags = (byte)prot;
  846.    if (prot)
  847.       {
  848.       if (!offthresh)
  849.          offthresh = 10;
  850.       sio->xoff_threshold = offthresh;
  851.       if (onthresh <= offthresh)
  852.          onthresh = offthresh + 10;
  853.       sio->xon_threshold = onthresh;
  854.       }
  855.  
  856.    if ((old & RTSCTS) != ((byte)prot & RTSCTS))
  857.       {
  858.       change_rts (sio, 1);
  859.       sio->modem_flags = (byte)((sio->modem_flags & ~CTS) |
  860.                          ((prot & RTSCTS) ? CTS : 0));
  861.       }
  862.  
  863.    if (!(prot & XONXOFF))
  864.       {
  865.       if (sio->r_xoff)
  866.          sio->r_xoff = -2;
  867.       sio->t_xoff = 0;
  868.       }
  869.  
  870.    if (!sio->xmit_pending)
  871.       transmit_ready (sio);
  872. }
  873.  
  874.  
  875. /*-------------------------------------------------------------------------*/
  876.  
  877.  
  878. int Globalfunc v24_send (sioptr sio, byte ch, dword timeout)
  879. {
  880.    int res;
  881.  
  882.    if ((res = write_pipe (&sio->xmit_pipe, ch, timeout)) < 0)
  883.       return res;
  884.    tsk_cli ();
  885.    if (!sio->xmit_pending)
  886.       transmit_ready (sio);
  887.    tsk_sti ();
  888.    return 0;
  889. }
  890.  
  891.  
  892. int Globalfunc v24_receive (sioptr sio, dword timeout)
  893. {
  894.    int res;
  895.    
  896.    if ((res = (int)read_wpipe (&sio->rcv_pipe, timeout)) < 0)
  897.       return res;
  898.  
  899.    if (!sio->flags)
  900.       return res;
  901.  
  902.    if (wpipe_free (&sio->rcv_pipe) > sio->xon_threshold)
  903.       {
  904.       tsk_cli ();
  905.       if (sio->r_xoff)
  906.          {
  907.          sio->r_xoff = -2;
  908.          if (!sio->xmit_pending)
  909.             transmit_ready (sio);
  910.          }
  911.       tsk_sti ();
  912.  
  913.       if (sio->rtsoff)
  914.          change_rts (sio, 1);
  915.       }
  916.    return res;
  917. }
  918.  
  919.  
  920. int Globalfunc v24_overrun (sioptr sio)
  921. {
  922.    int res;
  923.  
  924.    res = sio->overrun;
  925.    sio->overrun = 0;
  926.    return res;
  927. }
  928.  
  929.  
  930. int Globalfunc v24_check (sioptr sio)
  931. {
  932.    return check_wpipe (&sio->rcv_pipe);
  933. }
  934.  
  935.  
  936. int Globalfunc v24_modem_status (sioptr sio)
  937. {
  938.    return sio->modstat;
  939. }
  940.  
  941.  
  942. int Globalfunc v24_complete (sioptr sio)
  943. {
  944.    return (check_pipe (&sio->xmit_pipe) == -1);
  945. }
  946.  
  947.  
  948. int Globalfunc v24_wait_complete (sioptr sio, dword timeout)
  949. {
  950.    return wait_pipe_empty (&sio->xmit_pipe, timeout);
  951. }
  952.  
  953.  
  954. void Globalfunc v24_flush_receive (sioptr sio)
  955. {
  956.     flush_wpipe (&sio->rcv_pipe);
  957. }
  958.  
  959.  
  960. void Globalfunc v24_flush_transmit (sioptr sio)
  961. {
  962.     flush_pipe (&sio->xmit_pipe);
  963. }
  964.  
  965. #endif
  966.  
  967.