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