home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / DRSI.C < prev    next >
C/C++ Source or Header  |  1993-07-16  |  32KB  |  1,104 lines

  1. /*
  2.  * Version with Stopwatches
  3.  *
  4.  * 0 - Not used
  5.  * 1 - rx_fsm run time
  6.  * 2 - drtx_active run time (per character tx time)
  7.  * 
  8.  * Interface driver for the DRSI board for KA9Q's TCP/IP on an IBM-PC ONLY!
  9.  *
  10.  * Derived from a driver written by Art Goldman, WA3CVG
  11.  * (c) Copyright 1987 All Rights Reserved
  12.  * Permission for non-commercial use is hereby granted provided this notice
  13.  * is retained.  For info call: (301) 997-3838.
  14.  *
  15.  * Heavily re-written from the original,  a driver for the EAGLE board into
  16.  * a driver for the DRSI PC* Packet adpator. Copyright as original, all
  17.  * amendments likewise providing credit given and notice retained.
  18.  * Stu Phillips - N6TTO, W6/G8HQA (yes Virginia,  really !).
  19.  * For info call: (408) 285-4142
  20.  *
  21.  * This driver supports 1 (one) DRSI board.
  22.  * 
  23.  * Reformatted and added ANSI-style declarations, integrated into NOS
  24.  * by KA9Q, 10/14/89
  25.  *
  26.  * Latest set of defect fixes added 1/2/90 by N6TTO
  27.  * 1. Made P-PERSIST work properly
  28.  * 2. Fixed UNDERRUN bug when in DEFER state
  29.  * 3. Tx now defers correctly when DCD is high (!)
  30.  *
  31.  * Changed 3/4/90 by N6TTO
  32.  * Changed method of enabling the IRQ to the 8259 to call maskon()
  33.  * instead of clrbit(); change made to allow interrupts > 8 to work
  34.  * on an AT.
  35.  *
  36.  * Changed 11/14/90 by N6TTO
  37.  * Fixed incompatiblity between current NOS memory allocation scheme
  38.  * and changes made to speed up drsi transmit state machine.
  39.  *
  40.  */
  41.  
  42. /****************************************************************************
  43. *    $Id: drsi.c 1.2 93/07/16 11:44:03 ROOT_DOS Exp $
  44. *    15 Jul 93    1.2        GT    Fix warnings.                                    *
  45. ****************************************************************************/
  46.  
  47. #include <stdio.h>
  48. #include <dos.h>
  49. #include <time.h>
  50. #include "global.h"
  51. #include "mbuf.h"
  52. #include "iface.h"
  53. #include "pktdrvr.h"
  54. #include "netuser.h"
  55. #include "drsi.h"
  56. #include "ax25.h"
  57. #include "trace.h"
  58. #include "pc.h"
  59. #include "8530.h"
  60. #include "devparam.h"
  61. #include "ip.h"
  62.  
  63. static int32 dr_ctl __ARGS((struct iface *iface,int cmd,int set,int32 val));
  64. static int dr_raw __ARGS((struct iface *iface,struct mbuf *bp));
  65. static int dr_stop __ARGS((struct iface *iface));
  66. static void dr_wake __ARGS((struct drchan *hp,int rx_or_tx,
  67.     void (*routine) __ARGS((struct drchan *)),int ticks));
  68. static int drchanparam __ARGS((struct drchan *hp));
  69. static void drexint __ARGS((struct drchan *hp));
  70. static void drinitctc __ARGS((unsigned port));
  71. static void drrx_active __ARGS((struct drchan *hp));
  72. static void drrx_enable __ARGS((struct drchan *hp));
  73. static void drtx_active __ARGS((struct drchan *hp));
  74. static void drtx_defer __ARGS((struct drchan *hp));
  75. static void drtx_downtx __ARGS((struct drchan *hp));
  76. static void drtx_flagout __ARGS((struct drchan *hp));
  77. static void drtx_idle __ARGS((struct drchan *hp));
  78. static void drtx_rrts __ARGS((struct drchan *hp));
  79. static void drtx_tfirst __ARGS((struct drchan *hp));
  80. static char read_ctc __ARGS((unsigned port,unsigned reg));
  81. static void rx_fsm __ARGS((struct drchan *hp));
  82. static void tx_fsm __ARGS((struct drchan *hp));
  83. static void write_ctc __ARGS((unsigned port,unsigned reg,unsigned val));
  84.  
  85. struct DRTAB Drsi[DRMAX];    /* Device table - one entry per card */
  86. INTERRUPT (*Drhandle[]) __ARGS((void)) = { dr0vec };  /* handler interrupt vector table */
  87. struct drchan Drchan[2*DRMAX];     /* channel table - 2 entries per card */
  88. int16 Drnbr;
  89.  
  90. /* Set specified routine to be 'woken' up after specified number
  91.  * of ticks (allows CPU to be freed up and reminders posted);
  92.  */
  93. static void
  94. dr_wake(hp, rx_or_tx, routine, ticks)
  95. struct drchan *hp;
  96. int rx_or_tx;
  97. void (*routine) __ARGS((struct drchan *));
  98. int ticks;
  99. {
  100.     hp->w[rx_or_tx].wcall = routine;
  101.     hp->w[rx_or_tx].wakecnt = ticks;
  102. }
  103.  
  104. /* Master interrupt handler.  One interrupt at a time is handled.
  105.  * here. Service routines are called from here.
  106.  */
  107. void
  108. drint(dev)
  109. int dev;
  110. {
  111.     register char st;
  112.     register int16 pcbase, i;
  113.     struct drchan *hpa,*hpb;
  114.  
  115.     Drsi[dev].ints++;
  116.     pcbase = Drsi[dev].addr;
  117.     hpa = &Drchan[2 * dev];
  118.     hpb = &Drchan[(2 * dev)+1];
  119.  
  120. yuk:
  121.     /* Check CTC for timer interrupt */
  122.     st = read_ctc(pcbase, Z8536_CSR3);
  123.     if(st & Z_IP){
  124.         /* Reset interrupt pending */
  125.         write_ctc(pcbase, Z8536_CSR3, Z_CIP|Z_GCB);
  126.         for(i=0;i<=1;i++){
  127.             if(hpa->w[i].wakecnt){
  128.                 if(--hpa->w[i].wakecnt == 0){
  129.                     (hpa->w[i].wcall)(hpa);
  130.                 }
  131.             }
  132.             if(hpb->w[i].wakecnt){
  133.                 if(--hpb->w[i].wakecnt == 0){
  134.                     (hpb->w[i].wcall)(hpb);
  135.                 }
  136.             }
  137.         }
  138.     }
  139.     /* Check the SIO for interrupts */
  140.  
  141.     /* Read interrupt status register from channel A */
  142.     while((st = read_scc(pcbase+CHANA+CTL,R3)) != 0){
  143.         /* Use IFs to process ALL interrupts pending
  144.          * because we need to check all interrupt conditions
  145.          */
  146.         if(st & CHARxIP){
  147.             /* Channel A Rcv Interrupt Pending */
  148.             rx_fsm(hpa);
  149.         }
  150.         if(st & CHBRxIP){
  151.             /* Channel B Rcv Interrupt Pending */
  152.             rx_fsm(hpb);
  153.         }
  154.         if(st & CHATxIP){
  155.             /* Channel A Transmit Int Pending */
  156.             tx_fsm(hpa);
  157.         }
  158.         if(st & CHBTxIP){
  159.             /* Channel B Transmit Int Pending */
  160.             tx_fsm(hpb);
  161.         }
  162.         if(st & CHAEXT){
  163.             /* Channel A External Status Int */
  164.             drexint(hpa);
  165.         }
  166.         if(st & CHBEXT){
  167.             /* Channel B External Status Int */
  168.             drexint(hpb);
  169.         }
  170.         /* Reset highest interrupt under service */
  171.         write_scc(hpa->base+CTL,R0,RES_H_IUS);
  172.  
  173.     } /* End of while loop on int processing */
  174.     if(read_ctc(pcbase, Z8536_CSR3) & Z_IP)
  175.         goto yuk;
  176. }
  177.  
  178.  
  179. /* DRSI SIO External/Status interrupts
  180.  * This can be caused by a receiver abort, or a Tx UNDERRUN/EOM.
  181.  * Receiver automatically goes to Hunt on an abort.
  182.  *
  183.  * If the Tx Underrun interrupt hits, change state and
  184.  * issue a reset command for it, and return.
  185.  */
  186. static void
  187. drexint(hp)
  188. register struct drchan *hp;
  189. {
  190.     register int base = hp->base;
  191.     char st, i_state;
  192.  
  193.     i_state = dirps();     /* disable interrupts */
  194.     hp->exints++;
  195.  
  196.     st = read_scc(base+CTL,R0);     /* Fetch status */
  197.  
  198.     /* Check for Tx UNDERRUN/EOM - only in Transmit Mode */
  199.         /* Note that the TxEOM bit remains set once we go    */
  200.     /* back to receive.  The following qualifications    */
  201.     /* are necessary to prevent an aborted frame causing */
  202.     /* a queued transmit frame to be tossed when in      */
  203.     /* DEFER state on transmit.                 */
  204.     if((hp->tstate != DEFER) && (hp->rstate==0) && (st & TxEOM)){
  205.         if(hp->tstate != UNDERRUN){
  206.             /* This is an unexpected underrun.  Discard the current
  207.              * frame (there's no way to rewind),  kill the transmitter
  208.              * and return to receive with a wakeup posted to get the
  209.              * next (if any) frame.  Any recovery will have to be done
  210.              * by higher level protocols (yuk).
  211.              */
  212.             write_scc(base, R5, Tx8|DTR);    /* Tx off now */
  213.             write_scc(base, R1, 0);        /* Prevent ext.status int */
  214.             write_scc(base, R0, RES_Tx_P);  /* Reset Tx int pending */
  215.             write_scc(base, R0, ERR_RES);
  216.             write_scc(base, R0, RES_EOM_L); /* Reset underrun latch */
  217.             free_p(hp->sndbuf);
  218.             hp->tstate = IDLE;
  219.             hp->tx_state = drtx_idle;
  220.             dr_wake(hp, TX, tx_fsm, hp->slotime);
  221.             hp->rstate = ENABLE;
  222.             hp->rx_state = drrx_enable;
  223.             drrx_enable(hp);
  224.         }
  225.     }
  226.     /* Receive Mode only
  227.      * This triggers when hunt mode is entered, & since an ABORT
  228.      * automatically enters hunt mode, we use that to clean up
  229.      * any waiting garbage
  230.      */
  231.     if((hp->rstate != IDLE) && (st & BRK_ABRT)){
  232.         if(hp->rcvbuf != NULLBUF){
  233.             hp->rcp = hp->rcvbuf->data;
  234.             hp->rcvbuf->cnt = 0;
  235.         }
  236.         while(read_scc(base,R0) & Rx_CH_AV)
  237.             (void) inportb(base+DATA);
  238.         hp->aborts++;
  239.         hp->rstate = ACTIVE;
  240.         write_scc(base, R0, ERR_RES);
  241.     }
  242.     /* reset external status latch */
  243.     write_scc(base,R0,RES_EXT_INT);
  244.     restore(i_state);
  245. }
  246.  
  247. /* Receive Finite State Machine - dispatcher */
  248. static void
  249. rx_fsm(hp)
  250. struct drchan *hp;
  251. {
  252.     char i_state = dirps();
  253.  
  254.     hp->rxints++;
  255.     (*hp->rx_state)(hp);
  256.     restore(i_state);
  257. }
  258.  
  259. /* drrx_enable
  260.  * Receive ENABLE state processor
  261.  */
  262. static void
  263. drrx_enable(hp)
  264. struct drchan *hp;
  265. {
  266.     register int16 base = hp->base;
  267.  
  268.     write_scc(base, R1, INT_ALL_Rx|EXT_INT_ENAB);
  269.     write_scc(base, R15, BRKIE);    /* Allow ABORT Int */
  270.     write_scc(base, R14, BRSRC|BRENABL|SEARCH);
  271.     /* Turn on rx and enter hunt mode */
  272.     write_scc(base, R3, ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  273.  
  274.     if(hp->rcvbuf != NULLBUF){
  275.         hp->rcvbuf->cnt = 0;
  276.         hp->rcp = hp->rcvbuf->data;
  277.     }
  278.     hp->rstate = ACTIVE;
  279.     hp->rx_state = drrx_active;
  280. }
  281.  
  282. /* drrx_active
  283.  * Receive ACTIVE state processor
  284.  */
  285. static void
  286. drrx_active(hp)
  287. struct drchan *hp;
  288. {
  289.     register int16 base = hp->base;
  290.     unsigned char rse,st;
  291.     struct phdr *phdr;
  292.     struct mbuf *bp;
  293.  
  294.     /* Allocate a receive buffer if not already present */
  295.     if(hp->rcvbuf == NULLBUF){
  296.         bp = hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  297.         if(bp == NULLBUF){
  298.             /* No buffer - abort the receiver */
  299.             write_scc(base, R3, ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  300.             /* Clear character from rx buffer in SIO */
  301.             (void) inportb(base+DATA);
  302.             return;
  303.         }
  304.         hp->rcvbuf->cnt = 0; 
  305.         hp->rcp = hp->rcvbuf->data;
  306.     }
  307.  
  308.     st = read_scc(base, R0); /* get interrupt status from R0 */
  309.     rse = read_scc(base,R1); /* get special status from R1 */
  310.  
  311.     if(st & Rx_CH_AV){
  312.         /* there is a char to be stored
  313.          * read special condition bits before reading the data char
  314.          * (already read above)
  315.          */
  316.         if(rse & Rx_OVR){
  317.             /* Rx overrun - toss buffer */
  318.             hp->rcp = hp->rcvbuf->data;    /* reset buffer pointers */
  319.             hp->rcvbuf->cnt = 0;
  320.             hp->rstate = RXERROR;    /* set error flag */
  321.             hp->rovers++;        /* count overruns */
  322.         } else if(hp->rcvbuf->cnt >= hp->bufsiz){
  323.             /* Too large -- toss buffer */
  324.             hp->toobig++;
  325.             hp->rcp = hp->rcvbuf->data;    /* reset buffer pointers */
  326.             hp->rcvbuf->cnt = 0;
  327.             hp->rstate = TOOBIG;    /* when set, chars are not stored */
  328.         }
  329.         /* ok, we can store the received character now */
  330.         if((hp->rstate == ACTIVE) && ((st & BRK_ABRT) == 0)){
  331.             *hp->rcp++ = inportb(base+DATA); /* char to rcv buff */
  332.             hp->rcvbuf->cnt++;         /* bump count */
  333.         } else {
  334.             /* got to empty FIFO */
  335.             (void) inportb(base+DATA);
  336.             hp->rcp = hp->rcvbuf->data;    /* reset buffer pointers */
  337.             hp->rcvbuf->cnt = 0;
  338.             hp->rstate = RXABORT;
  339.             write_scc(base,R0,ERR_RES);    /* reset err latch */
  340.         }
  341.     }
  342.     /* The End of Frame bit is ALWAYS associated with a character,
  343.      * usually, it is the last CRC char.  Only when EOF is true can
  344.      * we look at the CRC byte to see if we have a valid frame
  345.      */
  346.     if(rse & END_FR){
  347.         hp->rxframes++;
  348.         /* END OF FRAME -- Make sure Rx was active */
  349.         if(hp->rcvbuf->cnt > 0){    /* any data to store */
  350.             /* looks like a frame was received
  351.              * now is the only time we can check for CRC error
  352.              */
  353.             if((rse & CRC_ERR) || (hp->rstate > ACTIVE) ||
  354.              (hp->rcvbuf->cnt < 10) || (st & BRK_ABRT)){
  355.                 /* error occurred; toss frame */
  356.                 if(rse & CRC_ERR)
  357.                     hp->crcerr++;    /* count CRC errs */
  358.                 hp->rcp = hp->rcvbuf->data;
  359.                 hp->rcvbuf->cnt = 0;
  360.                 hp->rstate = ACTIVE;   /* Clear error state */
  361.             } else {
  362.                 /* Here we have a valid frame */
  363.                 bp = alloc_mbuf(sizeof(struct phdr));
  364.                 bp->cnt = sizeof(struct phdr);
  365.                 phdr = (struct phdr *)bp->data;
  366.                 phdr->type = CL_AX25;
  367.                 phdr->iface = hp->iface;
  368.                 bp->next = hp->rcvbuf;
  369.                 hp->rcvbuf->cnt -= 2;    /* chuck FCS bytes */
  370.                 enqueue(&Hopper, bp);    /* queue it in */
  371.                 hp->enqueued++;
  372.                 /* packet queued - reset buffer pointer */
  373.                 hp->rcvbuf = NULLBUF;
  374.             } /* end good frame queued */
  375.         }  /* end check for active receive upon EOF */
  376.     }
  377. }
  378.  
  379. /*
  380.  * TX finite state machine - dispatcher
  381.  */
  382. static void
  383. tx_fsm(hp)
  384. struct drchan *hp;
  385. {
  386.     char i_state = dirps();
  387.     if(hp->tstate != DEFER && hp->tstate)
  388.         hp->txints++;
  389.     (*hp->tx_state)(hp);
  390.     restore(i_state);
  391. }
  392.  
  393. /* drtx_idle
  394.  * Transmit IDLE transmit state processor
  395.  */
  396. static void
  397. drtx_idle(hp)
  398. struct drchan *hp;
  399. {
  400.     register int16 base;
  401.  
  402.     /* Tx idle - is there a frame to transmit ? */
  403.     if((hp->sndbuf = dequeue(&hp->sndq)) == NULLBUF){
  404.         /* Nothing to send - return to receive mode
  405.          * Turn Tx off - any trailing flag should have been sent
  406.          * by now
  407.          */
  408. #ifdef DRSIDEBUG
  409.         printf("Nothing to TX\n");
  410. #endif
  411.         base = hp->base;
  412.         write_scc(base, R5, Tx8|DTR);   /* Tx off now */
  413.         write_scc(base, R0, ERR_RES);    /* Reset error bits */
  414.  
  415.         /* Delay for squelch tail before enabling receiver */
  416.         hp->rstate = ENABLE;
  417.         hp->rx_state = drrx_enable;
  418.         dr_wake(hp, RX, rx_fsm, hp->squeldelay);
  419.     } else {
  420.         /* Frame to transmit */
  421.         hp->tstate = DEFER;
  422.         hp->tx_state = drtx_defer;
  423.         drtx_defer(hp);
  424.     }
  425. }
  426.  
  427. /* drtx_defer
  428.  * Transmit DEFER state processor
  429.  */
  430. static void
  431. drtx_defer(hp)
  432. struct drchan *hp;
  433. {
  434.     register int16 base = hp->base;
  435.  
  436.     /* We may have defered a previous tx attempt - in any event...
  437.      * Check DCD in case someone is already transmitting
  438.      * then check to see if we should defer due to P-PERSIST.
  439.      */
  440.  
  441. #ifdef DRSIDEBUG
  442.     printf("drtx_defer - checking for DCD\n");
  443. #endif
  444.     if((read_scc(base+CTL, R0) & DCD) > 0){
  445.         /* Carrier detected - defer */
  446.         hp->txdefers++;
  447.         dr_wake(hp, TX, tx_fsm, 10);    /* Defer for 100 mS */
  448. #ifdef DRSIDEBUG
  449.         printf("drtx_defer - TX deferred\n");
  450. #endif
  451.         return;
  452.     }
  453.  
  454. #ifdef DRSIDEBUG
  455.     printf("drtx_defer - checking for P-PERSIST backoff\n");
  456. #endif
  457.     /* P-PERSIST is checked against channel 3 of the 8536 which is
  458.      * the free running counter for the 10 mS tick; The counter
  459.      * goes through 0x6000 ticks per 10 mS or one tick every
  460.      * 407 nS - this is pretty random compared to the DOS time of
  461.      * day clock (0x40:0x6C) used by the other (EAGLE) drivers.
  462.      */
  463.         if (hp->persist <= read_ctc(base,Z8536_CC3LSB)) {
  464. #ifdef DRSIDEBUG
  465.         printf("drtx_defer - BACKOFF\n");
  466. #endif
  467.         hp->txppersist++;
  468.         dr_wake (hp, TX, tx_fsm, hp->slotime);
  469.         return;
  470.     }
  471.     /* No backoff - set RTS and start to transmit frame */
  472.     write_scc(base, R1, 0);        /* Prevent external status int */
  473.     write_scc(base, R3, Rx8);    /* Turn Rx off */
  474.     hp->rstate = IDLE;        /* Mark Rx as idle */
  475.     hp->tstate = RRTS;
  476.     hp->tx_state = drtx_rrts;
  477. #ifdef DRSIDEBUG
  478.     printf("drtx_defer - wake posted for drtx_rrts\n");
  479. #endif
  480.     write_scc(base, R9, 0);        /* Interrupts off */
  481.     write_scc(base,R5,RTS|Tx8|DTR);    /* Turn tx on */
  482.     dr_wake(hp, TX, tx_fsm, 10);
  483. }
  484.  
  485. /* drtx_rrts
  486.  * Transmit RRTS state processor
  487.  */
  488. static void
  489. drtx_rrts(hp)
  490. struct drchan *hp;
  491. {
  492.     register int16 base = hp->base;
  493.  
  494.     write_scc(base, R9, 0);    /* Interrupts off */
  495.     write_scc(base,R5,TxCRC_ENAB|RTS|TxENAB|Tx8|DTR);    /* Tx now on */
  496.     hp->tstate = TFIRST;
  497.     hp->tx_state = drtx_tfirst;
  498. #ifdef DRSIDEBUG
  499.     printf("8530 Int status %x\n", read_scc(base+CHANA,R3) & 0xff); 
  500.     printf("drtx_rrts - Wake posted for TXDELAY\n");
  501. #endif
  502.     dr_wake(hp, TX, tx_fsm, hp->txdelay);
  503. }
  504.     
  505. /* drtx_tfirst
  506.  * Transmit TFIRST state processor
  507.  */
  508. static void
  509. drtx_tfirst(hp)
  510. struct drchan *hp;
  511. {
  512.     register int16 base = hp->base;
  513.     char c;
  514.     
  515.     /* Copy data to a local buffer to save on mbuf overheads
  516.      * during transmit interrupt time.
  517.      */
  518.     hp->drtx_cnt = len_p(hp->sndbuf);
  519.     hp->drtx_tcp = hp->drtx_buffer;
  520.     
  521.     pullup(&hp->sndbuf, hp->drtx_tcp, hp->drtx_cnt);
  522.     
  523.     /* Transmit the first character in the buffer */
  524.     c = *hp->drtx_tcp++;
  525.     hp->drtx_cnt--;
  526.  
  527.     write_scc(base, R0, RES_Tx_CRC);    /* Reset CRC */
  528.     write_scc(base, R0, RES_EOM_L);        /* Reset underrun latch */
  529.     outportb(base+DATA, c);            /* Output first character */
  530.     write_scc(base, R15, TxUIE);        /* Allow underrun ints only */
  531.     write_scc(base, R1, TxINT_ENAB|EXT_INT_ENAB); /* Tx/Ext status ints on */
  532.     write_scc(base, R9, MIE|NV);        /* master enable */
  533.     hp->tstate = ACTIVE;
  534.     hp->tx_state = drtx_active;
  535. }
  536.  
  537. /* drtx_active
  538.  * Transmit ACTIVE state processor
  539.  */
  540. static void
  541. drtx_active(hp)
  542. struct drchan *hp;
  543. {
  544.     if(hp->drtx_cnt-- > 0){
  545.         /* Send next character */
  546.         outportb(hp->base+DATA, *hp->drtx_tcp++);
  547.     } else {
  548.         /* No more to send - wait for underrun to hit */
  549.         hp->tstate = UNDERRUN;
  550.         hp->tx_state = drtx_flagout;
  551.         free_p(hp->sndbuf);
  552.         write_scc(hp->base, R0, RES_EOM_L);  /* Send CRC on underrun */
  553.         write_scc(hp->base, R0, RES_Tx_P);   /* Reset Tx Int pending */
  554.     }
  555. }
  556.  
  557. /* drtx_flagout
  558.  * Transmit FLAGOUT state processor
  559.  */
  560. static void
  561. drtx_flagout(hp)
  562. struct drchan *hp;
  563. {
  564.     /* Arrive here after CRC sent and Tx interrupt fires.
  565.      * Post a wake for ENDDELAY
  566.      */
  567.  
  568.     hp->tstate = UNDERRUN;
  569.     hp->tx_state = drtx_downtx;
  570.     write_scc(hp->base, R9, 0);
  571.     write_scc(hp->base, R0,  RES_Tx_P);
  572.     dr_wake(hp, TX, tx_fsm, hp->enddelay);
  573. }
  574.  
  575. /* drtx_downtx
  576.  * Transmit DOWNTX state processor
  577.  */
  578. static void
  579. drtx_downtx(hp)
  580. struct drchan *hp;
  581. {
  582.     register int base = hp->base;
  583.  
  584.     /* See if theres anything left to send - if there is,  send it ! */
  585.     if((hp->sndbuf = dequeue(&hp->sndq)) == NULLBUF){
  586.         /* Nothing left to send - return to receive */
  587.         write_scc(base, R5, Tx8|DTR);   /* Tx off now */
  588.         write_scc(base, R0, ERR_RES);   /* Reset error bits */
  589.         hp->tstate = IDLE;
  590.         hp->tx_state = drtx_idle;
  591.         hp->rstate = ENABLE;
  592.         hp->rx_state = drrx_enable;
  593.         drrx_enable(hp);
  594.     } else
  595.         drtx_tfirst(hp);
  596.  
  597. }
  598.     
  599. /* Write CTC register */
  600. static void
  601. write_ctc(port, reg, val)
  602. unsigned port,reg,val;
  603. {
  604.     char i_state;
  605.     int16 base = port;
  606.     
  607.     i_state = dirps();
  608.     /* Select register */
  609.     outportb(base+Z8536_MASTER,(char)reg);
  610.     outportb(base+Z8536_MASTER,(char)val);
  611.     restore(i_state);
  612. }
  613.  
  614. /* Read CTC register */
  615. static char
  616. read_ctc(port, reg)
  617. unsigned port, reg;
  618. {
  619.     char c,i_state;
  620.     int16 i,base = port;
  621.     
  622.     i_state = dirps();
  623.     /* Select register */
  624.         outportb(base+Z8536_MASTER,(char)(reg&0xFF));
  625.     /* Delay for a short time to allow 8536 to settle */
  626.     for(i=0;i<100;i++);
  627.     c = inportb(base+Z8536_MASTER);
  628.     restore(i_state);
  629.     return(c&0xFF);
  630. }
  631.  
  632. /* Initialize dr controller parameters */
  633. static int
  634. drchanparam(hp)
  635. register struct drchan *hp;
  636. {
  637.     int16 tc;
  638.     long br;
  639.     char i_state;
  640.     register int16 base;
  641.  
  642.     /* Initialize 8530 channel for SDLC operation */
  643.     base = hp->base;
  644.     i_state = dirps();
  645.  
  646.     switch(base & 2){
  647.     case 2:
  648.         write_scc(base,R9,CHRA);    /* Reset channel A */
  649.         break;
  650.     case 0:
  651.         write_scc(base,R9,CHRB);    /* Reset channel B */
  652.         break;
  653.     }
  654.     /* Deselect all Rx and Tx interrupts */
  655.     write_scc(base,R1,0);
  656.  
  657.     /* Turn off external interrupts (like CTS/CD) */
  658.     write_scc(base,R15,0);
  659.  
  660.     /* X1 clock, SDLC mode */
  661.     write_scc(base,R4,SDLC|X1CLK);     /* SDLC mode and X1 clock */
  662.  
  663.     /* Now some misc Tx/Rx parameters */
  664.     /* CRC PRESET 1, NRZI Mode */
  665.     write_scc(base,R10,CRCPS|NRZI);
  666.  
  667.     /* Set up BRG and DPLL multiplexers */
  668.     /* Tx Clk from RTxC. Rcv Clk from DPLL, TRxC pin outputs BRG */
  669.     write_scc(base,R11,RCDPLL|TCRTxCP|TRxCOI|TRxCBR);
  670.  
  671.     /* Null out SDLC start address */
  672.     write_scc(base,R6,0);
  673.  
  674.     /* SDLC flag */
  675.     write_scc(base,R7,FLAG);
  676.  
  677.     /* Set up the Transmitter but don't enable it */
  678.     /*  DTR, 8 bit TX chars only - TX NOT ENABLED */
  679.     write_scc(base,R5,Tx8|DTR);
  680.  
  681.     /* Receiver - initial setup only - more later */
  682.     write_scc(base,R3,Rx8);         /* 8 bits/char */
  683.  
  684.     /* Setting up BRG now - turn it off first */
  685.     write_scc(base,R14,BRSRC);     /* BRG off, but keep Pclk source */
  686.  
  687.     /* set the 32x time constant for the BRG */
  688.  
  689.     br = hp->speed;            /* get desired speed */
  690.     tc = ((XTAL/32)/br)-2;        /* calc 32X BRG divisor */
  691.  
  692.     write_scc(base,R12,tc&0xFF);      /* lower byte */
  693.     write_scc(base,R13,(tc>>8)&0xFF); /* upper bite */
  694.  
  695.     /* Time to set up clock control register for RECEIVE mode
  696.      * DRSI has xtal osc going to pclk at 4.9152 Mhz
  697.      * The BRG is sourced from that, and set to 32x clock
  698.      * The DPLL is sourced from the BRG.  BRG is fed to the TRxC pin
  699.      * Transmit clock is provided by the BRG externally divided by
  700.      * 32 in the CTC counter 1 and 2.
  701.      * Receive clock is from the DPLL
  702.      */
  703.  
  704.     /* Following subroutine sets up and ENABLES the receiver */
  705.     drrx_enable(hp);
  706.     
  707.     /* DPLL from BRG, BRG source is PCLK */
  708.     write_scc(hp->base,R14,BRSRC|SSBR);
  709.     /* SEARCH mode, keep BRG source */
  710.     write_scc(hp->base,R14,BRSRC|SEARCH);
  711.     /* Enable the BRG */
  712.     write_scc(hp->base,R14,BRSRC|BRENABL);
  713.  
  714.     /* enable the receive interrupts */
  715.  
  716.     write_scc(hp->base,R1,(INT_ALL_Rx|EXT_INT_ENAB));
  717.     write_scc(hp->base,R15,BRKIE);    /* ABORT int */
  718.     write_scc(hp->base,R9,MIE|NV);    /* master enable */
  719.  
  720.  
  721.     /* Now, turn on the receiver and hunt for a flag */
  722.     write_scc(hp->base,R3,RxENABLE|RxCRC_ENAB|Rx8);
  723.  
  724.     restore(i_state);
  725.     return 0;
  726. }
  727.  
  728. /*
  729.  * Initialize the CTC (8536)
  730.  * Only the counter/timers are used - the IO ports are un-comitted.
  731.  * Channels 1 and 2 are linked to provide a /32 counter to convert
  732.  * the SIO BRG to a real clock for Transmit clocking.
  733.  * CTC 3 is left free running on a 10 mS period.  It is always polled
  734.  * and therefore all interrupts from the chip are disabled.
  735.  *
  736.  * Updated 02/16/89 by N6TTO
  737.  * Changed to support the use of the second channel on the 8530.
  738.  * Added so that the driver works on the DRSI type 2 PC Adaptor
  739.  * which has 2 1200 bps modems.
  740.  *
  741.  */
  742. static void
  743. drinitctc(port)
  744. unsigned port;
  745. {
  746.     long i;
  747.  
  748.     /* Initialize 8536 CTC */
  749.  
  750.     /* Initialize 8536 */
  751.     /* Start by forcing chip into known state */
  752.     (void) read_ctc(port, Z8536_MICR);
  753.  
  754.     write_ctc(port, Z8536_MICR, 0x01);    /* Reset the CTC */
  755.     for(i=0;i < 1000L; i++)        /* Loop to delay */
  756.         ;
  757.     write_ctc(port, Z8536_MICR, 0x00);    /* Clear reset and start init seq. */
  758.  
  759.     /* Wait for chip to come ready */
  760.     while((read_ctc(port, Z8536_MICR)) != (char) 0x02)
  761.         ;
  762.  
  763.     write_ctc(port, Z8536_MICR, 0xa6);    /* MIE|NV|CT_VIS|RJA */
  764.     write_ctc(port, Z8536_MCCR, 0xf4);    /* PBE|CT1E|CT2E|CT3E|PAE */
  765.  
  766.     write_ctc(port, Z8536_CTMS1, 0xe2);    /* Continuous,EOE,ECE, Pulse output */
  767.     write_ctc(port, Z8536_CTMS2, 0xe2);    /* Continuous,EOE,ECE, Pulse output */
  768.     write_ctc(port, Z8536_CTMS3, 0x80);    /* Continuous,Pulse output */
  769.     write_ctc(port, Z8536_CT1MSB, 0x00);    /* Load time constant CTC #1 */
  770.     write_ctc(port, Z8536_CT1LSB, 0x10);
  771.     write_ctc(port, Z8536_CT2MSB, 0x00);    /* Load time constant CTC #2 */
  772.     write_ctc(port, Z8536_CT2LSB, 0x10);
  773.     write_ctc(port, Z8536_CT3MSB, 0x60);    /* Load time constant CTC #3 */
  774.     write_ctc(port, Z8536_CT3LSB, 0x00);
  775.  
  776.     write_ctc(port, Z8536_IVR, 0x06);
  777.  
  778.     /* Set port direction bits in port A and B
  779.      * Data is input on bits d1 and d5, output on d0 and d4.
  780.      * The direction is set by 1 for input and 0 for output
  781.      */
  782.     write_ctc(port, Z8536_PDCA, 0x22);
  783.     write_ctc(port, Z8536_PDCB, 0x22);
  784.  
  785.     write_ctc(port, Z8536_CSR1, Z_GCB|Z_TCB);  /* Start CTC #1 running */
  786.     write_ctc(port, Z8536_CSR2, Z_GCB|Z_TCB);  /* Start CTC #2 running */
  787.     write_ctc(port, Z8536_CSR3, Z_IE|Z_GCB|Z_TCB); /* Start CTC #3 running */
  788. }
  789.  
  790. /* Attach a DRSI interface to the system
  791.  * argv[0]: hardware type, must be "drsi"
  792.  * argv[1]: I/O address, e.g., "0x300"
  793.  * argv[2]: vector, e.g., "2"
  794.  * argv[3]: mode, must be "ax25"
  795.  * argv[4]: iface label, e.g., "dr0"
  796.  * argv[5]: receiver packet buffer size in bytes
  797.  * argv[6]: maximum transmission unit, bytes
  798.  * argv[7]: iface speed for channel A
  799.  * argv[8]: iface speed for channel B (defaults to same as A if absent)
  800.  * argv[9]: First IP address, optional (defaults to Ip_addr)
  801.  * argv[10]: Second IP address, optional (defaults to Ip_addr)
  802.  */
  803. int
  804. dr_attach(argc,argv)
  805. int argc;
  806. char *argv[];
  807. {
  808.     register struct iface *if_pca,*if_pcb;
  809.     struct drchan *hp;
  810.     int dev;
  811.  
  812.     /* Quick check to make sure args are good and mycall is set */
  813.     if(strcmp(argv[3],"ax25") != 0){
  814.         printf("Mode %s unknown for interface %s\n",
  815.             argv[3],argv[4]);
  816.         return -1;
  817.     }
  818.     if(if_lookup(argv[4]) != NULLIF){
  819.         printf("Interface %s already exists\n", argv[4]);
  820.         return -1;
  821.     }    
  822.     if(Mycall[0] == '\0'){
  823.         printf("set mycall first\n");
  824.         return -1;
  825.     }
  826.     /* Note: More than one card can be supported if you give up a COM:
  827.      * port, thus freeing up an IRQ line and port address
  828.      */
  829.     if(Drnbr >= DRMAX){
  830.         printf("Only %d DRSI controller(s) supported right now!\n",DRMAX);
  831.         return -1;
  832.     }
  833.     dev = Drnbr++;
  834.  
  835.     /* Initialize hardware-level control structure */
  836.     Drsi[dev].addr = htoi(argv[1]);
  837.     Drsi[dev].vec = htoi(argv[2]);
  838.  
  839.     /* Save original interrupt vector */
  840.     Drsi[dev].oldvec = getirq(Drsi[dev].vec);
  841.  
  842.     /* Set new interrupt vector */
  843.     if(setirq(Drsi[dev].vec,Drhandle[dev]) == -1){
  844.         printf("IRQ %u out of range\n",Drsi[dev].vec);
  845.         Drnbr--;
  846.     }    
  847.     /* Initialize the CTC */
  848.     drinitctc(Drsi[dev].addr);
  849.     
  850.     /* Create iface structures and fill in details */
  851.     if_pca = (struct iface *)callocw(1,sizeof(struct iface));
  852.     if_pcb = (struct iface *)callocw(1,sizeof(struct iface));
  853.  
  854.     if_pca->addr = if_pcb->addr = Ip_addr;
  855.     if(argc > 9)
  856.         if_pca->addr = resolve(argv[9]);
  857.     if(argc > 10)
  858.         if_pcb->addr = resolve(argv[10]);
  859.     if(if_pca->addr == 0 || if_pcb->addr == 0){
  860.         tprintf(Noipaddr);
  861.         free((char *)if_pca);
  862.         free((char *)if_pcb);
  863.         return -1;
  864.     }
  865.     /* Append "a" to iface associated with A channel */
  866.     if_pca->name = mallocw((unsigned)strlen(argv[4])+2);
  867.     strcpy(if_pca->name,argv[4]);
  868.     strcat(if_pca->name,"a");
  869.  
  870.     /* Append "b" to iface associated with B channel */
  871.     if_pcb->name = mallocw((unsigned)strlen(argv[4])+2);
  872.     strcpy(if_pcb->name,argv[4]);
  873.     strcat(if_pcb->name,"b");
  874.  
  875.     if_pcb->mtu = if_pca->mtu = atoi(argv[6]);
  876.     if_pcb->type = if_pca->type = CL_AX25;
  877.     if_pcb->ioctl = if_pca->ioctl = dr_ctl;
  878.     if_pca->dev = 2*dev;            /* dr0a */
  879.     if_pcb->dev = 2*dev + 1;        /* dr0b */
  880.     if_pcb->stop = if_pca->stop = dr_stop;
  881.     if_pcb->output = if_pca->output = ax_output;
  882.     if_pcb->raw = if_pca->raw = dr_raw;
  883.  
  884.     if(strcmp(argv[3],"ax25") == 0){
  885.         /* Must be true, was checked at top */
  886.         if_pcb->send = if_pca->send = ax_send;
  887.         if(if_pcb->hwaddr == NULLCHAR)
  888.             if_pcb->hwaddr = mallocw(sizeof(Mycall));
  889.         memcpy(if_pcb->hwaddr,(char *)&Mycall,sizeof(Mycall));
  890.         if(if_pca->hwaddr == NULLCHAR)
  891.             if_pca->hwaddr = mallocw(sizeof(Mycall));
  892.         memcpy(if_pca->hwaddr,(char *)&Mycall,sizeof(Mycall));
  893.     }
  894.     /* Link em in to the iface chain */
  895.     if_pca->next = if_pcb;
  896.     if_pcb->next = Ifaces;
  897.     Ifaces = if_pca;
  898.  
  899.     /* set params in drchan table for CHANNEL B */
  900.  
  901.     hp = &Drchan[2*dev+1];                /* dr1 is offset 1 */
  902.     hp->iface = if_pcb;
  903.     hp->stata = Drsi[dev].addr + CHANA + CTL;    /* permanent status */
  904.     hp->statb = Drsi[dev].addr + CHANB + CTL;    /* addrs for CHANA/B*/
  905.     if(argc > 8){
  906.         /* Separate speed for channel B */
  907.         hp->speed = (int16)atoi(argv[8]);
  908.     } else {
  909.         /* Set speed to same as for channel A */
  910.         hp->speed = (int16)atoi(argv[7]);
  911.     }
  912.     hp->base = Drsi[dev].addr + CHANB;
  913.     hp->bufsiz = atoi(argv[5]);
  914.     hp->drtx_buffer = mallocw(if_pcb->mtu+100);
  915.     hp->tstate = IDLE;
  916.     hp->tx_state = drtx_idle;
  917.     hp->w[RX].wcall = NULL;
  918.     hp->w[RX].wakecnt = 0;
  919.     hp->w[TX].wcall = NULL;
  920.     hp->w[TX].wakecnt = 0;
  921.     /* default KISS Params */
  922.     hp->txdelay = 25;        /* 250 Ms */
  923.     hp->persist = 64;        /* 25% persistence */
  924.     hp->slotime = 10;        /* 100 Ms */
  925.     hp->squeldelay = 20;        /* 200 Ms */
  926.     hp->enddelay = 10;        /* 100 Ms */
  927.     
  928.     write_scc(hp->stata,R9,FHWRES);        /* Hardware reset */
  929.     
  930.     /* Disable interrupts with Master interrupt ctrl reg */
  931.     write_scc(hp->stata,R9,0);
  932.  
  933.     drchanparam(hp); 
  934.  
  935.     /* Initialize buffer pointers */
  936.     hp->rcvbuf = NULLBUF;
  937.     hp->rcvbuf->cnt = 0;
  938.     hp->sndq = NULLBUF;
  939.     
  940.     /* set params in drchan table for CHANNEL A */
  941.     hp = &Drchan[2*dev];            /* dr0a is offset 0 */
  942.     hp->iface = if_pca;
  943.     hp->speed = (int16)atoi(argv[7]);
  944.     hp->base = Drsi[dev].addr + CHANA;
  945.     hp->bufsiz = atoi(argv[5]);
  946.     hp->drtx_buffer = mallocw(if_pca->mtu+100);
  947.     hp->tstate = IDLE;
  948.     hp->tx_state = drtx_idle;
  949.     hp->w[RX].wcall = NULL;
  950.     hp->w[RX].wakecnt = 0;
  951.     hp->w[TX].wcall = NULL;
  952.     hp->w[TX].wakecnt = 0;
  953.     /* default KISS Params */
  954.     hp->txdelay = 30;        /* 300 Ms */
  955.     hp->persist = 64;        /* 25% persistence */
  956.     hp->slotime = 10;        /* 100 Ms */
  957.     hp->squeldelay = 20;        /* 200 Ms */
  958.     hp->enddelay = 10;        /* 100 Ms */
  959.  
  960.     drchanparam(hp);
  961.  
  962.     /* Initialize buffer pointers */
  963.     hp->rcvbuf = NULLBUF;
  964.     hp->rcvbuf->cnt = 0;
  965.     hp->sndq = NULLBUF;
  966.  
  967.     write_scc(hp->base,R9,MIE|NV);        /* master interrupt enable */
  968.  
  969.     /* Enable interrupt in 8259 interrupt controller */
  970.     maskon(Drsi[dev].vec);
  971.     
  972.     return 0;
  973. }
  974.  
  975. /* Shut down iface */
  976. static int
  977. dr_stop(iface)
  978. struct iface *iface;
  979. {
  980.     int16 dev;
  981.  
  982.     dev = iface->dev;
  983.     if(dev & 1)
  984.         return 0;
  985.     dev >>= 1;    /* Convert back into DRSI number */
  986.  
  987.     /* Set 8259 interrupt mask (turn off interrupts) */
  988.     maskoff(Drsi[dev].vec);
  989.  
  990.     /* Restore original interrupt vector */
  991.     setirq(Drsi[dev].vec, Drsi[dev].oldvec);
  992.     Drnbr--;
  993.     
  994.     /* Force hardware reset */
  995.     write_scc(Drsi[dev].addr + CHANA + CTL,R9,FHWRES);
  996.     /* Reset the CTC */
  997.     (void) read_ctc(Drsi[dev].addr, Z8536_MICR);
  998.     write_ctc(Drsi[dev].addr, Z8536_MICR, 0x01);
  999.     return 0;
  1000. }
  1001.  
  1002. /* Send raw packet on DRSI card */
  1003. static int
  1004. dr_raw(iface,bp)
  1005. struct iface *iface;
  1006. struct mbuf *bp;
  1007. {
  1008.     char kickflag;
  1009.     struct drchan *hp;
  1010.     char i_st = dirps();
  1011.     
  1012.     dump(iface,IF_TRACE_OUT,CL_AX25,bp);
  1013.     iface->rawsndcnt++;
  1014.     iface->lastsent = secclock();
  1015.     hp = &Drchan[iface->dev];
  1016.     kickflag = (hp->sndq == NULL) & (hp->sndbuf == NULLBUF);
  1017.     /* clever! flag=1 if something in queue */
  1018.     enqueue(&hp->sndq,bp);
  1019.  
  1020.     if(kickflag)            /* simulate interrupt to xmit */
  1021.         tx_fsm(hp);        /* process interrupt */
  1022.  
  1023.     restore(i_st);
  1024.     return 0;
  1025. }
  1026.  
  1027. /* display DRSI Channel stats */
  1028. int
  1029. dodrstat(argc,argv,p)
  1030. int argc;
  1031. char *argv[];
  1032. void *p;
  1033. {
  1034.     struct drchan *hp0, *hp1;
  1035.     int i;
  1036.  
  1037.     for(i=0; i<DRMAX; i++){
  1038.         hp0 = &Drchan[i];
  1039.         hp1 = &Drchan[i+1];
  1040.         i = Drchan[i].base;
  1041.         tprintf("DRSI Board Statistics - N6TTO 112790.0\n");
  1042.         tprintf("--------------------------------------\n");
  1043.         tprintf("Channel - %s\n", hp0->iface->name);
  1044.         tprintf("Rxints  - %8ld  Txints  - %8ld  Exints  - %8ld\n",
  1045.             hp0->rxints, hp0->txints, hp0->exints);
  1046.         tprintf("Enqued  - %8ld  Crcerr  - %8ld  Aborts  - %8ld\n",
  1047.             hp0->enqueued, hp0->crcerr, hp0->aborts);
  1048.         tprintf("RFrames - %8ld  Rxovers - %8ld  TooBig  - %8ld\n",
  1049.             hp0->rxframes, hp0->rovers, hp0->toobig);
  1050.         tprintf("Txdefer - %8ld  Txppers - %8ld  Nomem   - %8ld\n",
  1051.             hp0->txdefers, hp0->txppersist, hp0->nomem);
  1052.         tprintf("Tx state  %8d  Rx state  %8d\n\n",hp0->tstate,hp0->rstate);
  1053.         tprintf("Channel - %s\n", hp1->iface->name);
  1054.         tprintf("Rxints  - %8ld  Txints  - %8ld  Exints  - %8ld\n",
  1055.             hp1->rxints, hp1->txints, hp1->exints);
  1056.         tprintf("Enqued  - %8ld  Crcerr  - %8ld  Aborts  - %8ld\n",
  1057.             hp1->enqueued, hp1->crcerr, hp1->aborts);
  1058.         tprintf("RFrames - %8ld  Rxovers - %8ld  TooBig  - %8ld\n",
  1059.             hp1->rxframes, hp1->rovers, hp1->toobig);
  1060.         tprintf("Txdefer - %8ld  Txppers - %8ld  Nomem   - %8ld\n",
  1061.             hp1->txdefers, hp1->txppersist, hp1->nomem);
  1062.         tprintf("Tx state  %8d  Rx state  %8d\n",hp1->tstate,hp1->rstate);
  1063.     }
  1064.     return 0;
  1065. }
  1066.  
  1067. /* Subroutine to set kiss params in channel tables */
  1068. static int32
  1069. dr_ctl(iface,cmd,set,val)
  1070. struct iface *iface;
  1071. int cmd;
  1072. int set;
  1073. int32 val;
  1074. {
  1075.     struct drchan *hp;
  1076.     hp = &Drchan[iface->dev];
  1077.  
  1078.     switch(cmd){
  1079.     case PARAM_TXDELAY:
  1080.         if(set)
  1081.             hp->txdelay = (int) val;
  1082.         return hp->txdelay;
  1083.     case PARAM_PERSIST:
  1084.         if(set)
  1085.             hp->persist = (int) val;
  1086.         return hp->persist;
  1087.     case PARAM_SLOTTIME:
  1088.         if(set)
  1089.             hp->slotime = (int) val;
  1090.         return hp->slotime;
  1091.     case PARAM_TXTAIL:
  1092.         if(set)
  1093.             hp->squeldelay = (int) val;
  1094.         return hp->squeldelay;
  1095.     case PARAM_ENDDELAY:
  1096.         if(set)
  1097.             hp->enddelay = (int) val;
  1098.         return hp->enddelay;
  1099.     case PARAM_SPEED:
  1100.         return hp->speed;
  1101.     }
  1102.     return -1;
  1103. }
  1104.