home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / EAGLE.C < prev    next >
C/C++ Source or Header  |  1992-05-15  |  25KB  |  833 lines

  1. /*
  2.  * Interface driver for the EAGLE board for KA9Q's TCP/IP on an IBM-PC ONLY!
  3.  *
  4.  *  Written by Art Goldman, WA3CVG - (c) Copyright 1987 All Rights Reserved
  5.  *  Permission for non-commercial use is hereby granted provided this notice
  6.  *  is retained.  For info call: (301) 997-3838.
  7.  *
  8.  *  10 Jan 88    ng6q    - Corrected IDLE comparison in doegstat.
  9.  *   6 Apr 88    ng6q    - Changed eg_raw to prevent calling egtxint with a
  10.  *              packet in sndbuf.  Initialized sndq and rcvq in
  11.  *              eg_attach.  Added carrier detect check before
  12.  *              slot time delay in egtxint.  Should make major
  13.  *              changes to egtxint to avoid delay loops while
  14.  *              masked for receive interrupts.
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <dos.h>
  19. #include "global.h"
  20. #include "mbuf.h"
  21. #include "iface.h"
  22. #include "pktdrvr.h"
  23. #include "netuser.h"
  24. #include "eagle.h"
  25. #include "z8530.h"
  26. #include "ax25.h"
  27. #include "trace.h"
  28. #include "pc.h"
  29. #include "devparam.h"
  30. #include <time.h>
  31.  
  32. static int32 eg_ctl __ARGS((struct iface *iface,int cmd,int set,int32 val));
  33. static int eg_raw __ARGS((struct iface *iface,struct mbuf *bp));
  34. static int eg_stop __ARGS((struct iface *iface));
  35. static void egchanparam __ARGS((struct egchan *hp));
  36. static void egexint __ARGS((struct egchan *hp));
  37. static void egrxint __ARGS((struct egchan *hp));
  38. static void egtxint __ARGS((struct egchan *hp));
  39. static void rts __ARGS((struct egchan *hp,int16 x));
  40. static void waitmsec __ARGS((int n));
  41.  
  42. static struct EGTAB Eagle[EGMAX];    /* Device table - one entry per card */
  43. static INTERRUPT (*eghandle[])() = {    /* handler interrupt vector table */
  44.     eg0vec,    
  45. };
  46. static struct egchan Egchan[2*EGMAX];    /* channel table - 2 entries per card */
  47. static int16 Egnbr;
  48.  
  49. /* Master interrupt handler.  One interrupt at a time is handled.
  50.  * here. Service routines are called from here.
  51.  */
  52. INTERRUPT (far *(egint)(dev))()
  53. int dev;
  54. {
  55.     register char st;
  56.     register int16 pcbase;
  57.     struct egchan *hp;
  58.     struct EGTAB *ep;
  59.  
  60.     ep = &Eagle[dev];
  61.     ep->ints++;
  62.     pcbase = ep->addr;
  63.  
  64.     /* Read interrupt status register from channel A */
  65.     while((st = read_scc(pcbase+CHANA+CTL,R3)) != 0) {
  66.         /* Use IFs to process ALL interrupts pending
  67.          * because we need to check all interrupt conditions
  68.          */
  69.         if (st & CHARxIP) {
  70.             /* Channel A Rcv Interrupt Pending */
  71.             hp = &Egchan[2 * dev];
  72.             egrxint(hp);
  73.         } else if (st & CHATxIP) {
  74.             /* Channel A Transmit Int Pending */
  75.             hp = &Egchan[2 * dev];
  76.             egtxint(hp);
  77.         } else if (st & CHAEXT) {
  78.             /* Channel A External Status Int */
  79.             hp = &Egchan[2 * dev];
  80.             egexint(hp);
  81.         } else if (st & CHBRxIP) {
  82.             /* Channel B Rcv Interrupt Pending */
  83.             hp = &Egchan[(2 * dev)+1];
  84.             egrxint(hp);
  85.         } else if (st & CHBTxIP) {
  86.             /* Channel B Transmit Int Pending */
  87.             hp = &Egchan[(2 * dev)+1];
  88.             egtxint(hp);
  89.         } else if (st & CHBEXT) {
  90.             /* Channel B External Status Int */
  91.             hp = &Egchan[(2 * dev)+1];
  92.             egexint(hp);
  93.         }
  94.         /* Reset highest interrupt under service */
  95.         write_scc(hp->base+CTL,R0,RES_H_IUS);
  96.     } /* End of while loop on int processing */
  97.     return ep->chain ? ep->oldvec : NULL;
  98. }
  99.  
  100. /* Eagle SIO External/Status interrupts
  101.  * This can be caused by a receiver abort, or a Tx UNDERRUN/EOM.
  102.  * Receiver automatically goes to Hunt on an abort.
  103.  *
  104.  * If the Tx Underrun interrupt hits, change state and
  105.  * issue a reset command for it, and return.
  106.  */
  107. static void
  108. egexint(hp)
  109. register struct egchan *hp;
  110. {
  111.     char st;
  112.  
  113.     DISABLE();
  114.     hp->exints++;
  115.     st = read_scc(hp->base+CTL,R0);     /* Fetch status */
  116.  
  117.     /* Check for Tx UNDERRUN/EOM - only in Transmit Mode */
  118.     if((hp->rstate==0) && (st & TxEOM)) {
  119.         /* if in UNDERRUN, go to FLAGOUT state
  120.          * see explanation under egtxint()
  121.          * CRC & FLAG now going out, so
  122.          * wait for Tx BUffer Empty int
  123.          */
  124.  
  125.         /* If we are not in underrun, this is an unexpected
  126.          * underrun.  EOM bit should be set, so the SCC will
  127.          * now send an abort
  128.          */
  129.  
  130.         if(hp->tstate == UNDERRUN)
  131.             hp->tstate = FLAGOUT;
  132.  
  133.         /* Tx Buff EMPTY interrupt occurs after CRC is sent */
  134.     }
  135.  
  136.     /* Receive Mode only
  137.      * This triggers when hunt mode is entered, & since an ABORT
  138.      * automatically enters hunt mode, we use that to clean up
  139.      * any waiting garbage
  140.      */
  141.     if((hp->rstate == ACTIVE) && (st & BRK_ABRT)) {
  142.         hp->rcp = hp->rcvbuf->data;
  143.         hp->rcvbuf->cnt = 0;          /* rewind on DCD transition */
  144.         hp->aborts++;              /* nbr aborts * 2 */
  145.     }
  146.  
  147.     /* reset external status latch */
  148.     write_scc(CTL+hp->base,R0,RES_EXT_INT);
  149.  
  150.     RESTORE();
  151. }
  152.  
  153. /* EG receive interrupt handler. The first receive buffer is pre-allocated
  154.  * in the init routine.  Thereafter, it is filled here, queued out, and a
  155.  * new one acquired.  CRC, OVERRUN and TOOBIG errors merely 'rewind' the
  156.  * pointers and reuse the same buffer.
  157.  */
  158. static void
  159. egrxint(hp)
  160. register struct egchan *hp;
  161. {
  162.     register int16 base;
  163.     char rse;
  164.  
  165.     DISABLE();
  166.     hp->rxints++;
  167.     base = hp->base;
  168.  
  169.     if ((read_scc(base+CTL,R0)) & Rx_CH_AV) {
  170.         /* there is a char to be stored
  171.          * read special condition bits before reading the data char
  172.          */
  173.         rse = read_scc(hp->base+CTL,R1); /* get status byte from R1 */
  174.         if(rse & Rx_OVR) {
  175.             /* Rx overrun - toss buffer */
  176.             hp->rcp = hp->rcvbuf->data;    /* reset buffer pointers */
  177.             hp->rcvbuf->cnt = 0;
  178.             hp->rstate = RXERROR;    /* set error flag */
  179.             hp->rovers++;        /* count overruns */
  180.         } else if(hp->rcvbuf->cnt >= hp->bufsiz) {
  181.             /* Too large -- toss buffer */
  182.             hp->toobig++;
  183.             hp->rcp = hp->rcvbuf->data;    /* reset buffer pointers */
  184.             hp->rcvbuf->cnt = 0;
  185.             hp->rstate = TOOBIG;    /* when set, chars are not stored */
  186.         }
  187.         /* ok, we can store the received character now */
  188.         if(hp->rstate == ACTIVE) {        /* If no errors... */
  189.             *hp->rcp++ = inportb(base+DATA);    /* char to rcv buff */
  190.             hp->rcvbuf->cnt++;            /* bump count */
  191.         } else {
  192.             /* got to empty FIFO */
  193.             (void) inportb(base+DATA);
  194.             write_scc(hp->base+CTL,R0,ERR_RES);    /* reset err latch */
  195.             hp->rstate = ACTIVE;
  196.         }
  197.     }
  198.     /* char has been stored
  199.      * read special condition bits
  200.      */
  201.     rse = read_scc(hp->base+CTL,R1);     /* get status byte from R1 */
  202.  
  203.     /* The End of Frame bit is ALWAYS associated with a character,
  204.      * usually, it is the last CRC char.  Only when EOF is true can
  205.      * we look at the CRC byte to see if we have a valid frame
  206.      */
  207.     if(rse & END_FR) {
  208.         hp->rxframes++;
  209.         /* END OF FRAME -- Make sure Rx was active */
  210.         if(hp->rcvbuf->cnt > 0) {        /* any data to store */
  211.             /* looks like a frame was received
  212.              * now is the only time we can check for CRC error
  213.              */
  214.             if((rse & CRC_ERR) || (hp->rstate > ACTIVE) || (hp->rcvbuf->cnt < 10)) {
  215.                 /* error occurred; toss frame */
  216.                 if(rse & CRC_ERR)
  217.                     hp->crcerr++;    /* count CRC errs */
  218.                 if(hp->rstate == RXERROR)
  219.                     hp->rovers++;
  220.                 /* don't throw away buffer -
  221.                  * merely reset the pointers
  222.                  */
  223.                 hp->rcp = hp->rcvbuf->data;
  224.                 hp->rcvbuf->cnt = 0;
  225.             } else {
  226.                 /* Here we have a valid frame */
  227.                 hp->rcvbuf->cnt -= 2;           /* Toss 2 crc bytes */
  228.                 net_route(hp->iface,hp->rcvbuf);
  229.                 /* packet queued - get buffer for next frame */
  230.                 hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  231.                 hp->rcp = hp->rcvbuf->data;
  232.                 hp->rcvbuf->cnt = 0;
  233.                 if(hp->rcvbuf == NULLBUF) {
  234.                     /* No memory, abort receiver */
  235.                     RESTORE();
  236.                     printf("DISASTER! Out of Memory for Receive!\n");
  237.                     write_scc(CTL+base,R3,Rx8);
  238.                     return;
  239.                 }
  240.             } /* end good frame queued */
  241.         }  /* end check for active receive upon EOF */
  242.         hp->rstate = ACTIVE;    /* and clear error status */
  243.     } /* end EOF check */
  244.     RESTORE();
  245. }
  246.  
  247. /* egchan transmit interrupt service routine
  248.  *
  249.  * The state variable tstate, along with some static pointers,
  250.  * represents the state of the transmit "process".
  251.  */
  252. static void
  253. egtxint(hp)
  254. register struct egchan *hp;
  255. {
  256.     register int16 base;
  257.     int c;
  258.  
  259.     DISABLE();
  260.  
  261.     if(hp->tstate != DEFER && hp->tstate)
  262.         hp->txints++;
  263.     base = hp->base;
  264.  
  265.     switch(hp->tstate) {
  266.     case FLAGOUT:
  267.         /* Here after CRC sent and Tx interrupt fires.
  268.          * To give the SCC a chance to get the FLAG
  269.          * out, we delay 100 Ms
  270.          */
  271.         hp->tstate = IDLE;    /* fall thru to IDLE */
  272.         waitmsec(10);        /* 100 msec wait for flag Tx */
  273.         /* Note, it may be possible to stuff out a
  274.          * meaningless character, wait for the interrupt
  275.          * then go to idle.  A delay is absolutely necessary
  276.          * here else the packet gets truncated prematurely
  277.          * when no other packet is waiting to be sent.
  278.          * IDLE state indicates either we are starting a brand new packet
  279.          * as a result of its being queued for transmission (egtxint called
  280.          * from eg_raw), or after a frame has been transmitted (as a
  281.          * result of a Tx buffer empty interrupt after the CRC/FLAG
  282.          */
  283.     case IDLE:
  284.         /* Transmitter idle. Find a frame for transmission */
  285.         if((hp->sndbuf = dequeue(&hp->sndq)) == NULLBUF) {
  286.             /* Nothing to send - return to receive mode
  287.              * Tx OFF now - flag should have gone
  288.              */
  289.             rts(hp,OFF);
  290.             RESTORE();
  291.             return;
  292.         }
  293.         /* If a buffer to send, we drop thru here */
  294.     case DEFER:        /* we may have deferred prev xmit attempt */
  295.         /* PPERSIST CALCULATION: we use the lower byte of the
  296.          * 8253 timer 0 count, as a random number (0-255).
  297.          * If the persist value is higher, wait one slot time
  298.          */
  299.         if(hp->persist >= peekb(0x40,0x6c))
  300.             waitmsec(hp->slotime);
  301.  
  302.         /* Check DCD so we don't step on a frame being received */
  303.         /* DCD is ACTIVE LOW on the SCC DCD pin, but the bit in R0 */
  304.         /* is SET when DCD is ACTIVE!! */
  305.  
  306.         if((read_scc(base+CTL,R0) & DCD) > 0) { /* Carrier Detected? */
  307.             hp->tstate = DEFER;    /* defer xmit */
  308.             /* don't release dequeued buffer...*/
  309.             RESTORE();
  310.             return;
  311.         }
  312.  
  313.         rts(hp,ON);   /* Transmitter on */
  314.         /* ints not enabled yet */
  315.  
  316.         /* Get next char to send */
  317.         c = PULLCHAR(&hp->sndbuf);        /* one char at a time */
  318.         write_scc(CTL+base,R0,RES_Tx_CRC);    /* reset for next frame */
  319.         outportb(base+DATA,c);        /* First char out now */
  320.  
  321.         /* select transmit interrupts to enable */
  322.  
  323.         write_scc(CTL+base,R15,TxUIE);        /* allow Underrun int only */
  324.         write_scc(CTL+base,R1,TxINT_ENAB|EXT_INT_ENAB);  /* Tx/Extern ints on */
  325.         write_scc(CTL+base,R9,MIE|NV);        /* master enable */
  326.         /* enable interrupt latch on board */
  327.         outportb(hp->dmactrl,INTENABLE);
  328.  
  329.         hp->tstate = ACTIVE;    /* char going out now */
  330.         RESTORE();
  331.         return;
  332.  
  333.     case ACTIVE:
  334.         /* Here we are actively sending a frame */
  335.         if((c = PULLCHAR(&hp->sndbuf)) != -1){
  336.             outportb(hp->base+DATA,c);    /* next char is gone */
  337.             /* stuffing a char satisfies Interrupt condition */
  338.         } else {
  339.             /* No more to send - just stop till underrun int */
  340.             hp->tstate = UNDERRUN;
  341.             free_p(hp->sndbuf);
  342.             /* now we reset the EOM latch & enable underrun int */
  343.             write_scc(CTL+base,R0,RES_EOM_L);    /* send CRC at underrun */
  344.             write_scc(CTL+hp->base,R0,RES_Tx_P); /* reset Tx Int Pend */
  345.         }
  346.         RESTORE();
  347.         return;     /* back to wait for interrupt */
  348.  
  349.     case UNDERRUN:
  350.         /*
  351.          * This state is handled by an UNDERRUN interrupt, which
  352.          * is an External Status interrupt.  At UNDERRUN, the
  353.          * UNDERRUN/EOM latch in R0 will be 0, so the SCC will send
  354.          * CRC and ending flag.  After the CRC clears the Tx buffer,
  355.          * a TX BUFF EMPTY interrupt will fire.  At that time, we
  356.          * should be in FLAGOUT state, ready to send another frame
  357.          * if one is there to send.
  358.          */
  359.         break;
  360.     } /* end switch */
  361.     RESTORE();
  362. }
  363.  
  364. /* SET Transmit or Receive Mode
  365.  * Set RTS (request-to-send) to modem on Transmit
  366.  */
  367. static void
  368. rts(hp,x)
  369. register struct egchan *hp;
  370. int16 x;
  371. {
  372. int16 tc;
  373. long br;
  374.  
  375.     /* Reprogram BRG and turn on transmitter to send flags */
  376.     if(x == ON) {                /* Turn Tx ON and Receive OFF */
  377.         write_scc(CTL+hp->base,R3,Rx8);    /* Rx off */
  378.         waitmsec(50);            /* 500 msec delay before on */
  379.         hp->rstate = IDLE;
  380.         write_scc(CTL+hp->base,R9,0);    /* Interrupts off */
  381.         br = hp->speed;         /* get desired speed */
  382.         tc = (XTAL/br)-2;        /* calc 1X BRG divisor */
  383.         write_scc(CTL+hp->base,R12,tc&0xFF);      /* lower byte */
  384.         write_scc(CTL+hp->base,R13,(tc>>8)&0xFF); /* upper bite */
  385.  
  386.         write_scc(CTL+hp->base,R5,TxCRC_ENAB|RTS|TxENAB|Tx8|DTR);
  387.         /* Transmitter now on */
  388.         write_scc(CTL+hp->base,R0,RES_Tx_CRC);/* CRC reset */
  389.         waitmsec(hp->txdelay);      /* Delay after Tx on */
  390.     } else {    /* Tx OFF and Rx ON */
  391.         hp->tstate = IDLE;
  392.         write_scc(CTL+hp->base,R5,Tx8|DTR);     /* TX off now */
  393.         write_scc(CTL+hp->base,R0,ERR_RES);     /* reset error bits */
  394.  
  395.         write_scc(CTL+hp->base,R1,(INT_ALL_Rx|EXT_INT_ENAB));
  396.         write_scc(CTL+hp->base,R15,BRKIE);        /* allow ABORT int */
  397.  
  398.         /* delay for squelch tail before enable of Rx */
  399.         waitmsec(hp->squeldelay);    /* keep it up  */
  400.  
  401.         /* Reprogram BRG for 32x clock for receive DPLL */
  402.         write_scc(CTL+hp->base,R14,BRSRC);         /* BRG off, but keep Pclk source */
  403.         br = hp->speed;             /* get desired speed */
  404.         tc = ((XTAL/32)/br)-2;            /* calc 32X BRG divisor */
  405.         write_scc(CTL+hp->base,R12,tc&0xFF);    /* lower byte */
  406.         write_scc(CTL+hp->base,R13,(tc>>8)&0xFF);    /* upper bite */
  407.         write_scc(CTL+hp->base,R14,BRSRC|SEARCH);    /* SEARCH mode, keep BRG source */
  408.         write_scc(CTL+hp->base,R14,BRSRC|BRENABL);    /* Enable the BRG */
  409.  
  410.         /* Now, turn on the receiver and hunt for a flag */
  411.         write_scc(CTL+hp->base,R3,RxENABLE|RxCRC_ENAB|Rx8);
  412.         hp->rstate = ACTIVE;            /* Normal state */
  413.     }
  414. }
  415.  
  416. /* Initialize eg controller parameters */
  417. static void
  418. egchanparam(hp)
  419. register struct egchan *hp;
  420. {
  421.     int16 tc;
  422.     long br;
  423.     register int16 base;
  424.  
  425.     /* Initialize 8530 channel for SDLC operation */
  426.  
  427.     base = hp->base;
  428. #ifdef    notdef
  429.     printf("Initializing Channel %c - Base = %x\n",base&2?'A':'B',base);
  430. #endif
  431.     DISABLE();
  432.  
  433.     switch(base & 2){
  434.     case 2:
  435.         write_scc(CTL+base,R9,CHRA);    /* Reset channel A */
  436.         break;
  437.     case 0:
  438.         write_scc(CTL+base,R9,CHRB);    /* Reset channel B */
  439.         break;
  440.     }
  441.  
  442.     /* Deselect all Rx and Tx interrupts */
  443.     write_scc(CTL+base,R1,0);
  444.  
  445.     /* Turn off external interrupts (like CTS/CD) */
  446.     write_scc(CTL+base,R15,0);
  447.  
  448.     /* X1 clock, SDLC mode */
  449.     write_scc(CTL+base,R4,SDLC|X1CLK);           /* SDLC mode and X1 clock */
  450.  
  451.     /* Now some misc Tx/Rx parameters */
  452.     /* CRC PRESET 1, NRZI Mode */
  453.     write_scc(CTL+base,R10,CRCPS|NRZI);
  454.  
  455.     /* Set up BRG and DPLL multiplexers */
  456.     /* Tx Clk from BRG. Rcv Clk from DPLL, TRxC pin outputs DPLL */
  457.     write_scc(CTL+base,R11,TCBR|RCDPLL|TRxCDP|TRxCOI);
  458.  
  459.     /* Null out SDLC start address */
  460.     write_scc(CTL+base,R6,0);
  461.  
  462.     /* SDLC flag */
  463.     write_scc(CTL+base,R7,FLAG);
  464.  
  465.     /* Set up the Transmitter but don't enable it */
  466.     /*  DTR, 8 bit TX chars only - TX NOT ENABLED */
  467.     write_scc(CTL+base,R5,Tx8|DTR);
  468.  
  469.     /* Receiver - intial setup only - more later */
  470.     write_scc(CTL+base,R3,Rx8);            /* 8 bits/char */
  471.  
  472.     /* Setting up BRG now - turn it off first */
  473.     write_scc(CTL+hp->base,R14,BRSRC);         /* BRG off, but keep Pclk source */
  474.  
  475.     /* set the 32x time constant for the BRG in Receive mode */
  476.  
  477.     br = hp->speed;             /* get desired speed */
  478.     tc = ((XTAL/32)/br)-2;            /* calc 32X BRG divisor */
  479.  
  480.     write_scc(CTL+hp->base,R12,tc&0xFF);      /* lower byte */
  481.     write_scc(CTL+hp->base,R13,(tc>>8)&0xFF); /* upper bite */
  482.  
  483.     /* Time to set up clock control register for RECEIVE mode
  484.      * Eagle has xtal osc going to pclk at 3.6864 Mhz
  485.      * The BRG is sourced from that, and set to 32x clock
  486.      * The DPLL is sourced from the BRG, and feeds the TRxC pin
  487.      * Transmit clock & Receive clock come from DPLL
  488.      */
  489.  
  490.     /* Following subroutine sets up and ENABLES the receiver */
  491.     rts(hp,OFF);           /* TX OFF and RX ON */
  492.  
  493.     write_scc(CTL+hp->base,R14,BRSRC|SSBR);       /* DPLL from BRG, BRG source is PCLK */
  494.     write_scc(CTL+hp->base,R14,BRSRC|SEARCH);       /* SEARCH mode, keep BRG source */
  495.  
  496.     write_scc(CTL+hp->base,R14,BRSRC|BRENABL);    /* Enable the BRG */
  497.  
  498.     /* enable the receive interrupts */
  499.  
  500.     write_scc(CTL+hp->base,R1,(INT_ALL_Rx|EXT_INT_ENAB));
  501.     write_scc(CTL+hp->base,R15,BRKIE);        /* ABORT int */
  502.     write_scc(CTL+hp->base,R9,MIE|NV);    /* master enable */
  503.  
  504.     /* enable interrupt latch on board */
  505.     outportb(hp->dmactrl,INTENABLE);
  506.  
  507.     /* Now, turn on the receiver and hunt for a flag */
  508.     write_scc(CTL+hp->base,R3,RxENABLE|RxCRC_ENAB|Rx8);
  509.  
  510.     RESTORE();
  511. }
  512.  
  513. /* Attach a EAGLE interface to the system
  514.  * argv[0]: hardware type, must be "eagle"
  515.  * argv[1]: I/O address, e.g., "0x300"
  516.  * argv[2]: vector, e.g., "2"
  517.  * argv[3]: mode, must be:
  518.  *        "ax25" (AX.25 UI frame format)
  519.  * argv[4]: interface label, e.g., "eg0"
  520.  * argv[5]: receiver packet buffer size in bytes
  521.  * argv[6]: maximum transmission unit, bytes
  522.  * argv[7]: interface speed, e.g, "1200"
  523.  * argv[8]: First IP address, optional (defaults to Ip_addr);
  524.  * argv[9]: Second IP address, optional (defaults to Ip_addr);
  525.  */
  526. int
  527. eg_attach(argc,argv,p)
  528. int argc;
  529. char *argv[];
  530. void *p;
  531. {
  532.     register struct iface *if_pca,*if_pcb;
  533.     struct egchan *hp;
  534.     int dev;
  535.     char *cp;
  536.  
  537.     /* Quick check to make sure args are good and mycall is set */
  538.     if(strcmp(argv[3],"ax25") != 0){
  539.         printf("Mode %s unknown for interface %s\n",
  540.             argv[3],argv[4]);
  541.         return -1;
  542.     }
  543.     if(if_lookup(argv[4]) != NULLIF){
  544.         printf("Interface %s already exists\n",argv[4]);
  545.         return -1;
  546.     }
  547.     if(Mycall[0] == '\0'){
  548.         printf("set mycall first\n");
  549.         return -1;
  550.     }
  551.     /* Note: More than one card can be supported if you give up a COM:
  552.      * port, thus freeing up an IRQ line and port address
  553.      */
  554.  
  555.     if(Egnbr >= EGMAX) {
  556.         printf("Only 1 EAGLE controller supported right now!\n");
  557.         return -1;
  558.     }
  559.     dev = Egnbr++;
  560.  
  561.     /* Initialize hardware-level control structure */
  562.     Eagle[dev].addr = htoi(argv[1]);
  563.     Eagle[dev].vec = atoi(argv[2]);
  564.     if(strchr(argv[2],'c') != NULLCHAR)
  565.         Eagle[dev].chain = 1;
  566.     else
  567.         Eagle[dev].chain = 0;
  568.  
  569.  
  570.     /* Save original interrupt vector */
  571.     Eagle[dev].oldvec = getirq(Eagle[dev].vec);
  572.  
  573.     /* Set new interrupt vector */
  574.     if(setirq(Eagle[dev].vec,eghandle[dev]) == -1){
  575.         printf("IRQ %u out of range\n",Eagle[dev].vec);
  576.         Egnbr--;
  577.         return -1;
  578.     }
  579.     /* Create interface structures and fill in details */
  580.     if_pca = (struct iface *)callocw(1,sizeof(struct iface));
  581.     if_pcb = (struct iface *)callocw(1,sizeof(struct iface));
  582.  
  583.     if_pca->addr = if_pcb->addr = Ip_addr;
  584.     if(argc > 8)
  585.         if_pca->addr = resolve(argv[8]);
  586.     if(argc > 9)
  587.         if_pcb->addr = resolve(argv[9]);
  588.  
  589.     if(if_pca->addr == 0 || if_pcb->addr == 0){
  590.         printf(Noipaddr);
  591.         free((char *)if_pca);
  592.         free((char *)if_pcb);
  593.         return -1;
  594.     }
  595.     /* Append "a" to interface associated with A channel */
  596.     if_pca->name = mallocw((unsigned)strlen(argv[4])+2);
  597.     strcpy(if_pca->name,argv[4]);
  598.     strcat(if_pca->name,"a");
  599.     /* Append "b" to iface associated with B channel */
  600.     if_pcb->name = mallocw((unsigned)strlen(argv[4])+2);
  601.     strcpy(if_pcb->name,argv[4]);
  602.     strcat(if_pcb->name,"b");
  603.  
  604.     if_pcb->mtu = if_pca->mtu = atoi(argv[6]);
  605.     if_pcb->ioctl = if_pca->ioctl = eg_ctl;
  606.     if_pca->dev = 2*dev;            /* eg0a */
  607.     if_pcb->dev = 2*dev + 1;        /* eg0b */
  608.     if_pcb->stop = if_pca->stop = eg_stop;
  609.     if_pcb->raw = if_pca->raw = eg_raw;
  610.  
  611.     if(strcmp(argv[3],"ax25") == 0) {
  612.         /* Must be true, was checked at top */
  613.         setencap(if_pca,"AX25");
  614.         setencap(if_pcb,"AX25");
  615.         if(if_pcb->hwaddr == NULLCHAR)
  616.             if_pcb->hwaddr = mallocw(AXALEN);
  617.         memcpy(if_pcb->hwaddr,Mycall,AXALEN);
  618.         if(if_pca->hwaddr == NULLCHAR)
  619.             if_pca->hwaddr = mallocw(AXALEN);
  620.         memcpy(if_pca->hwaddr,Mycall,AXALEN);
  621.     }
  622.     /* Link em in to the interface chain */
  623.     if_pca->next = if_pcb;
  624.     if_pcb->next = Ifaces;
  625.     Ifaces = if_pca;
  626.  
  627.     /* set params in egchan table for CHANNEL B */
  628.  
  629.     hp = &Egchan[2*dev+1];                /* eg1 is offset 1 */
  630.     hp->iface = if_pcb;
  631.     hp->stata = Eagle[dev].addr + CHANA + CTL;    /* permanent status */
  632.     hp->statb = Eagle[dev].addr + CHANB + CTL;    /* addrs for CHANA/B*/
  633.     hp->dmactrl = Eagle[dev].addr + DMACTRL;    /* Eagle control reg */
  634.     hp->speed = (int16)atoi(argv[7]);
  635.     hp->base = Eagle[dev].addr + CHANB;
  636.     hp->bufsiz = atoi(argv[5]);
  637.     hp->tstate = IDLE;
  638.     /* default KISS Params */
  639.     hp->txdelay = 25;        /* 250 Ms */
  640.     hp->persist = 64;        /* 25% persistence */
  641.     hp->slotime = 10;        /* 100 Ms */
  642.     hp->squeldelay = 20;        /* 200 Ms */
  643.  
  644.     write_scc(CTL+hp->stata,R9,FHWRES);     /* Hardware reset */
  645.                         /* one time only */
  646.     /* Disable interrupts with Master interrupt ctrl reg */
  647.     write_scc(CTL+hp->stata,R9,0);
  648.  
  649.     egchanparam(hp);
  650.  
  651.     /* Pre-allocate a receive buffer */
  652.     hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  653.     if(hp->rcvbuf == NULLBUF) {
  654.         /* No memory, abort receiver */
  655.         printf("EGATTACH: No memory available for Receive buffers\n");
  656.         /* Restore original interrupt vector */
  657.         setirq(Eagle[dev].vec,Eagle[dev].oldvec);
  658.         Egnbr--;
  659.         return(-1);
  660.     }
  661.     hp->rcp = hp->rcvbuf->data;
  662.     hp->rcvbuf->cnt = 0;
  663.     hp->sndq = NULLBUF;
  664.  
  665.     /* set params in egchan table for CHANNEL A */
  666.     hp = &Egchan[2*dev];            /* eg0a is offset 0 */
  667.     hp->iface = if_pca;
  668.     hp->speed = (int16)atoi(argv[7]);
  669.     hp->base = Eagle[dev].addr + CHANA;
  670.     hp->bufsiz = atoi(argv[5]);
  671.     hp->tstate = IDLE;
  672.     /* default KISS Params */
  673.     hp->txdelay = 25;        /* 250 Ms */
  674.     hp->persist = 64;        /* 25% persistence */
  675.     hp->slotime = 10;        /* 100 Ms */
  676.     hp->squeldelay = 20;        /* 200 Ms */
  677.  
  678.     egchanparam(hp);
  679.  
  680.     /* Pre-allocate a receive buffer */
  681.     hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  682.     if(hp->rcvbuf == NULLBUF) {
  683.         /* No memory, abort receiver */
  684.         printf("EGATTACH: No memory available for Receive buffers\n");
  685.         /* Restore original interrupt vector */
  686.         setirq(Eagle[dev].vec,Eagle[dev].oldvec);
  687.         Egnbr--;
  688.         return -1;
  689.     }
  690.     hp->rcp = hp->rcvbuf->data;
  691.     hp->rcvbuf->cnt = 0;
  692.     hp->sndq = NULLBUF;
  693.  
  694.     write_scc(CTL+hp->base,R9,MIE|NV);        /* master interrupt enable */
  695.  
  696.     /* Enable interrupts on the EAGLE card itself */
  697.     outportb(hp->dmactrl,INTENABLE);
  698.  
  699.     /* Enable interrupt */
  700.     maskon(Eagle[dev].vec);
  701.  
  702.     cp = if_name(if_pca," tx");
  703.     if_pca->txproc = newproc(cp,512,if_tx,0,if_pca,NULL,0);
  704.     free(cp);
  705.     cp = if_name(if_pcb," tx");
  706.     if_pcb->txproc = newproc(cp,512,if_tx,0,if_pcb,NULL,0);
  707.     free(cp);
  708.  
  709.     return 0;
  710. }
  711.  
  712.  
  713. /* Shut down interface */
  714. static int
  715. eg_stop(iface)
  716. struct iface *iface;
  717. {
  718.     int dev;
  719.  
  720.     dev = iface->dev;
  721.     if(dev & 1)
  722.         return 0;
  723.     dev >>= 1;    /* Convert back into eagle number */
  724.  
  725.     /* Turn off interrupts */
  726.     maskoff(Eagle[dev].vec);
  727.  
  728.     /* Restore original interrupt vector */
  729.     setirq(Eagle[dev].vec,Eagle[dev].oldvec);
  730.  
  731.     /* Force hardware reset */
  732.     write_scc(CTL+Eagle[dev].addr + CHANA,R9,FHWRES);
  733.  
  734.     /* resets interrupt enable on eagle card itself */
  735.     outportb(Eagle[dev].addr+DMACTRL,0);
  736.     return 0;
  737. }
  738.  
  739. /* Send raw packet on eagle card */
  740. static int
  741. eg_raw(iface,bp)
  742. struct iface *iface;
  743. struct mbuf *bp;
  744. {
  745.     char kickflag;
  746.     struct egchan *hp;
  747.  
  748.     dump(iface,IF_TRACE_OUT,bp);
  749.     iface->rawsndcnt++;
  750.     iface->lastsent = secclock();
  751.     hp = &Egchan[iface->dev];
  752.     kickflag = (hp->sndq == NULLBUF) & (hp->sndbuf == NULLBUF);    /* clever! flag=1 if something in queue */
  753.     enqueue(&hp->sndq,bp);
  754.     if(kickflag)            /* simulate interrupt to xmit */
  755.         egtxint(hp);        /* process interrupt */
  756.     return 0;
  757. }
  758. /* routine to delay n increments of 10 milliseconds
  759.  * about right on a turbo XT - will be slow on 4.77
  760.  * Still looking for a way to use the 8253 timer...
  761.  */
  762. static void
  763. waitmsec(n)
  764. int n;
  765. {
  766.     long i;
  767.  
  768.     for(i=0L; i < (200L*n); i++)
  769.         ;  /* simple loop delay */
  770. }
  771.  
  772. /* display EAGLE Channel stats */
  773. int
  774. doegstat(argc,argv,p)
  775. int argc;
  776. char *argv[];
  777. void *p;
  778. {
  779.     struct egchan *hp0, *hp1;
  780.     int i;
  781.  
  782.     for(i=0; i<EGMAX; i++) {
  783.         hp0 = &Egchan[i];
  784.         hp1 = &Egchan[i+1];
  785.  
  786.         printf("EAGLE Board Statistics:\n\n");
  787.         printf("Base Addr\tRxints\tTxints\tExints\tEnqued\tCrcerr\tAborts\tRxOvers\tRFrames\n");
  788.         printf("---------\t------\t------\t------\t------\t------\t------\t-------\t-------\n");
  789.         printf("0x%x\t\t%ld\t%ld\t%ld\t%d\t%d\t%d\t%d\t%d\nRcv State=%s\n", hp0->base, hp0->rxints,
  790.             hp0->txints, hp0->exints, hp0->enqueued, hp0->crcerr, hp0->aborts,
  791.             hp0->rovers,hp0->rxframes,
  792.             hp0->rstate==0?"IDLE":hp0->rstate==1?"ACTIVE":hp0->rstate==2?"RXERROR":hp0->rstate==3?"RXABORT":"TOOBIG");
  793.  
  794.         printf("0x%x\t\t%ld\t%ld\t%ld\t%d\t%d\t%d\t%d\t%d\nRcv State=%s\n\n", hp1->base, hp1->rxints,
  795.             hp1->txints, hp1->exints, hp1->enqueued, hp1->crcerr, hp1->aborts,
  796.             hp1->rovers,hp1->rxframes,
  797.             hp1->rstate==0?"IDLE":hp1->rstate==1?"ACTIVE":hp1->rstate==2?"RXERROR":hp1->rstate==3?"RXABORT":"TOOBIG");
  798.     }
  799.     return 0;
  800. }
  801.  
  802. /* Subroutine to set kiss params in channel tables */
  803. static int32
  804. eg_ctl(iface,cmd,set,val)
  805. struct iface *iface;
  806. int cmd;
  807. int set;
  808. int32 val;
  809. {
  810.     struct egchan *hp;
  811.  
  812.     hp = &Egchan[iface->dev];        /* point to channel table */
  813.     switch(cmd){
  814.     case PARAM_TXDELAY:
  815.         if(set)
  816.             hp->txdelay = val;
  817.         return hp->txdelay;
  818.     case PARAM_PERSIST:
  819.         if(set)
  820.             hp->persist = val;
  821.         return hp->persist;
  822.     case PARAM_SLOTTIME:
  823.         if(set)
  824.             hp->slotime = val;
  825.         return hp->slotime;
  826.     case PARAM_TXTAIL:
  827.         if(set)
  828.             hp->squeldelay = val;
  829.         return hp->squeldelay;
  830.     }
  831.     return -1;
  832. }
  833.