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