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