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