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