home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1 / HamRadio.cdr / misc / tcpipsrc / pi.c < prev    next >
C/C++ Source or Header  |  1991-01-30  |  40KB  |  1,452 lines

  1. /*
  2.  * Interface driver for the VE3IFB 8530 card (PI card)
  3.  * Copyright 1990 by Dave Perry, VE3IFB
  4.  *
  5.  * Portions of this driver were derived from the Eagle card
  6.  * driver by Art Goldman, WA3CVG. It has been very extensively
  7.  * modified from his work, due to the addition of DMA support
  8.  * and due to differences in the hardware.  The PI card is NOT
  9.  * an Eagle card clone. It is an original design by Dave Perry,
  10.  * VE3IFB.  Art's copyright notice follows:
  11.  *
  12.  *  Written by Art Goldman, WA3CVG - (c) Copyright 1987 All Rights Reserved
  13.  *  Permission for non-commercial use is hereby granted provided this notice
  14.  *  is retained.  For info call: (301) 997-3838.
  15.  * 
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <dos.h>
  20. #include <bios.h>
  21. #include "global.h"
  22. #include "mbuf.h"
  23. #include "iface.h"
  24. #include "pktdrvr.h"
  25. #include "netuser.h"
  26. #include "pi.h"
  27. #include "8530.h"
  28. #include "ax25.h"
  29. #include "trace.h"
  30. #include "pc.h"
  31. #include <time.h>
  32.  
  33. #include "session.h"
  34. #include "lapb.h"
  35. #include "proc.h"
  36. #include "ip.h"
  37.  
  38. #define FP_OFF(fp)    ((unsigned)(fp))
  39. #define FP_SEG(fp)    ((unsigned)((unsigned long)(fp) >> 16))
  40.  
  41. static void xwrite_scc __ARGS((struct pichan *hp,int16 ctl,int16 reg,
  42.     int16 val ));
  43. static char xread_scc __ARGS((struct pichan *hp, int16 ctl, char reg));
  44. static int pi_ctl __ARGS((struct iface *iface,int argc,char *argv[]));
  45. static int pi_raw __ARGS((struct iface *iface,struct mbuf *bp));
  46. static int pi_stop __ARGS((struct iface *iface));
  47. static void rts __ARGS((struct pichan *hp, int16 x));
  48. void setup_rx_dma __ARGS((struct pichan *hp));
  49. void setup_tx_dma __ARGS((struct pichan *hp, char *buffer, int length));
  50.  
  51. static struct PITAB Pi[PIMAX];    /* Device table - one entry per card */
  52. static INTERRUPT (*pihandle[])() = {    /* handler interrupt vector table */
  53.     pi0vec,
  54.     pi1vec,
  55.     pi2vec
  56. };
  57. static struct pichan Pichan[2*PIMAX];    /* channel table - 2 entries per card */
  58. static int16 pinbr;
  59.  
  60. extern int16 acc_delay;    /* Delay for the 8530 chip access recovery time */
  61.  
  62. /* Allocate a buffer which does not cross a dma page boundary */
  63. /* This really belongs in mbuf.c */
  64. struct mbuf *
  65. alloc_dmabuf(size)
  66. register int16 size;
  67. {
  68.     struct mbuf *bp[20],*retbuf;
  69.     unsigned buf_offset, buf_segment;
  70.     long longseg, dma_abs, dma_page;
  71.     int n;
  72.  
  73.     for(n = 0; n < 20 ;n++)
  74.     {
  75.         if((bp[n] = alloc_mbuf(size)) == NULLBUF)
  76.         {
  77.             while(--n >= 0) /* Free the buffers which failed the test */
  78.                 free_p(bp[n]);
  79.             return(NULLBUF);
  80.         }
  81.  
  82.         /* Calculate the DMA page */
  83.  
  84.         buf_offset = FP_OFF(bp[n]);
  85.         buf_segment= FP_SEG(bp[n]);
  86.         longseg = (long) buf_segment;
  87.         dma_abs = (longseg << 4) + (long) buf_offset;
  88.         dma_page = dma_abs >> 16;
  89.         if(((dma_abs+size) >> 16) == dma_page)
  90.         {
  91.             retbuf = bp[n]; /* Save the one that passed */
  92.             while(--n >= 0) /* Free the buffers which failed the test */
  93.                 free_p(bp[n]);
  94.             return(retbuf);
  95.         }
  96.     }
  97.     while(--n >= 0) /* Free the buffers which failed the test */
  98.         free_p(bp[n]);
  99.     return(NULLBUF);
  100. }
  101.  
  102. /* This calculates the constant to be used in the delay loops
  103.    which satify the SCC's access recovery time.  It needs to be timed and
  104.    calculated because a fixed value would not work in a 4.77mhz XT
  105.    to a 40mhz 486 (and beyond).
  106. */
  107. set_acc_delay()
  108. {    
  109.     long starttime, endtime;
  110.     int n;
  111.     int ticks;
  112.  
  113.     starttime = biostime(0,0l);
  114.     for(n = 0; n < 10; n++)
  115.         mloop();
  116.     endtime = biostime(0,0l);
  117.     ticks = (int) (endtime - starttime);
  118.     if(ticks == 0)
  119.         ticks = 1;
  120.     acc_delay = 61/ticks;
  121.     if(acc_delay == 0)
  122.         acc_delay = 1;
  123.     fflush(stdout);
  124. }
  125.  
  126. /* Write 8530 register */
  127. static void
  128. xwrite_scc(hp,ctl,reg,val)
  129. register struct pichan *hp;
  130. register int16 ctl;
  131. int16 reg,val;
  132. {
  133.     wrtscc(hp->cardbase,ctl,reg,val);
  134. }
  135.  
  136. /* Read 8530 register */
  137. static char
  138. xread_scc(hp,ctl,reg)
  139. register struct pichan *hp;
  140. register int16 ctl;
  141. char reg;
  142. {
  143.     return(rdscc(hp->cardbase,ctl,reg));
  144. }
  145.  
  146. /* Setup 8253 chip for time delay */
  147. static tdelay(hp,time)
  148. register struct pichan *hp;
  149. unsigned char time; /* Time to delay in milliseconds */
  150. {    int n;
  151.     int port, t1;
  152.     unsigned char sc;
  153.  
  154.     if(hp->base & 2) /* If A channel */
  155.     {
  156.         sc = SC1;
  157.         t1 = time;
  158.         port = hp->cardbase+TMR1;
  159.     }
  160.     else {
  161.         sc = SC2;
  162.         t1 = 10 * time; /* 10s of milliseconds for the B channel */
  163.         port = hp->cardbase+TMR2;
  164.     }
  165.  
  166.     outportb(hp->cardbase+TMRCMD, sc|LSB_MSB|MODE0); /* Setup timer sc */
  167.     for(n=0; n<5;n++) ; /* satisfy access time restriction */
  168.     outportb(port, (t1 << 1) & 0xFF);/* times 2 to make millisecs */
  169.     for(n=0; n<5;n++) ; /* satisfy access time restriction */
  170.     outportb(port, (t1 >> 7) & 0xFF);
  171.  
  172.     xwrite_scc(hp,hp->base+CTL,R15,CTSIE); /* Enable correct int for timeout */
  173.     xwrite_scc(hp,hp->base+CTL,R1,EXT_INT_ENAB);
  174.     xwrite_scc(hp,hp->base+CTL,R0,RES_EXT_INT);
  175. }
  176.  
  177. /* Master interrupt handler.  One interrupt at a time is handled.
  178.  * here. Service routines are called from here.
  179.  */
  180. void
  181. piint(dev)
  182. int dev;
  183. {
  184.     register char st;
  185.     register int16 pcbase;
  186.     struct pichan *hp;
  187.     void b_rxint(),b_txint(),b_exint();
  188.     void a_rxint(),a_txint(),a_exint();
  189.  
  190.     Pi[dev].ints++;
  191.     pcbase = Pi[dev].addr;
  192.  
  193.     /* Read interrupt status register (only valid from channel A)            */
  194.     /* Process all pending interrupts in while loop                            */
  195.     hp = &Pichan[2 * dev];    /* Assume channel A */
  196.     while((st = xread_scc(hp,pcbase+CHANA+CTL,R3)) != 0)
  197.     {
  198.         if (st & CHARxIP) {
  199.             /* Channel A Rcv Interrupt Pending */
  200.             hp = &Pichan[2 * dev];
  201.             a_rxint(hp);
  202.         } else if (st & CHATxIP) {
  203.             /* Channel A Transmit Int Pending */
  204.             hp = &Pichan[2 * dev];
  205.             a_txint(hp);
  206.         } else if (st & CHAEXT) {
  207.             /* Channel A External Status Int */
  208.             hp = &Pichan[2 * dev];
  209.             a_exint(hp);
  210.         } else if (st & CHBRxIP) {
  211.             /* Channel B Rcv Interrupt Pending */
  212.             hp = &Pichan[(2 * dev)+1];
  213.             b_rxint(hp);
  214.         } else if (st & CHBTxIP) {
  215.             /* Channel B Transmit Int Pending */
  216.             hp = &Pichan[(2 * dev)+1];
  217.             b_txint(hp);
  218.         } else if (st & CHBEXT) {
  219.             /* Channel B External Status Int */
  220.             hp = &Pichan[(2 * dev)+1];
  221.             b_exint(hp);
  222.         }
  223.         /* Reset highest interrupt under service */
  224.         xwrite_scc(hp,hp->base+CTL,R0,RES_H_IUS);
  225.     } /* End of while loop on int processing */
  226. }
  227.  
  228. static void
  229. a_exint(hp)
  230. register struct pichan *hp;
  231. {
  232.     register int16 cmd;
  233.     char st, i_state;
  234.     int i,length;
  235.  
  236.     i_state = dirps();        /* disable interrupts */
  237.  
  238.     st = xread_scc(hp,hp->base+CTL,R0);     /* Fetch status */
  239.  
  240.     /* reset external status latch */
  241.     xwrite_scc(hp,CTL+hp->base,R0,RES_EXT_INT);
  242.     cmd = hp->base+CTL;
  243.     hp->exints++;
  244.  
  245.     if((hp->rstate >= ACTIVE) && (st & BRK_ABRT))
  246.     {    
  247.         setup_rx_dma(hp);
  248.         hp->rstate = ACTIVE;
  249.     }
  250.  
  251.     switch(hp->tstate)
  252.     {
  253.         case ACTIVE:
  254.             free_p(hp->sndbuf);
  255.             hp->sndbuf = NULLBUF;
  256.             hp->tstate = FLAGOUT;
  257.             tdelay(hp,hp->params[SQUELDELAY]);
  258.             break;
  259.         case FLAGOUT:
  260.             if((hp->sndbuf = dequeue(&hp->sndq)) == NULLBUF)
  261.             {
  262.                 /* Nothing to send - return to receive mode */
  263.                 hp->tstate = IDLE;
  264.                 rts(hp,OFF);
  265.                 restore(i_state);
  266.                 return;
  267.             }
  268.             /* NOTE - fall through if more to send */
  269.         case ST_TXDELAY:
  270.             outportb(DMA_MASK, DMA_DISABLE|hp->dmachan);/* Disable DMA chan */
  271.             xwrite_scc(hp,cmd,R1,WT_FN_RDYFN|EXT_INT_ENAB);/*Set up for TX dma*/
  272.             length = pullup(&hp->sndbuf,hp->txdmabuf,hp->bufsiz); /* Get all chars */
  273.             setup_tx_dma(hp,hp->txdmabuf,length);/* Setup DMA controller for tx */
  274.             /* select transmit interrupts to enable */
  275.             outportb(DMA_MASK,DMA_ENABLE|hp->dmachan);/* Allow DMA on chan */
  276.             xwrite_scc(hp,cmd,R0,RES_Tx_CRC|RES_Tx_P);/* reset CRC, Txint pend*/
  277.             xwrite_scc(hp,cmd,R15,TxUIE);        /* allow Underrun int only */
  278.             /* Enable TX DMA */
  279.             xwrite_scc(hp,cmd,R1,WT_RDY_ENAB|WT_FN_RDYFN|EXT_INT_ENAB);
  280.             xwrite_scc(hp,cmd,R0,RES_EOM_L);    /* Send CRC on underrun */
  281.                                             /* Rest of frame sent by DMA */
  282.             hp->tstate = ACTIVE;            /* packet going out now */
  283.             break;
  284.         case DEFER:        /* we have deferred prev xmit attempt */
  285.  
  286.             /* See Intel Microcommunications Handbook, p2-308 */
  287.             xwrite_scc(hp,cmd,R0,RES_EXT_INT);
  288.             xwrite_scc(hp,cmd,R0,RES_EXT_INT);
  289.             if((xread_scc(hp,cmd,R0) & DCD) > 0)
  290.             {    hp->tstate = DEFER;
  291.                 tdelay(hp,100);
  292.                 /* Defer until dcd transition or 100mS timeout */
  293.                 xwrite_scc(hp,CTL+hp->base,R15,CTSIE|DCDIE);
  294.                 restore(i_state);
  295.                 return;
  296.             }
  297.  
  298.              if((rand() & 0xff) > uchar(hp->params[PERSIST]))
  299.              {
  300.                 hp->tstate = DEFER;
  301.                 tdelay(hp,hp->params[SLOTIME]);
  302.                 restore(i_state);
  303.                 return;
  304.             }
  305.  
  306.             xwrite_scc(hp,cmd,R5,TxCRC_ENAB|RTS|Tx8);/* Assert RTS early minimize */
  307.                                                 /* collision window */
  308.             rts(hp,ON);                        /* Transmitter on */
  309.             hp->tstate = ST_TXDELAY;
  310.             tdelay(hp,hp->params[TXDELAY]);
  311.             restore(i_state);
  312.             return;
  313.  
  314.     } /* switch(hp->tstate) */
  315.  
  316.     restore(i_state);
  317. } /* a_exint() */
  318.  
  319. /* Receive interrupt handler for the A channel 
  320.  */
  321. static void
  322. a_rxint(hp)
  323. register struct pichan *hp;
  324. {
  325.     register int16 cmd;
  326.     register int16 bytecount;
  327.     char rse, i_state;
  328.     struct mbuf *bp;
  329.     struct phdr *phdr;
  330.  
  331.     i_state = dirps();        /* disable interrupts */
  332.     hp->rxints++;
  333.     cmd = hp->base+CTL;
  334.  
  335.     rse = xread_scc(hp,cmd,R1); /* Get special condition bits from R1 */
  336.     if(rse & Rx_OVR) /* If receiver overrun */
  337.     {
  338.         hp->rovers++;
  339.         hp->rstate = RXERROR;
  340.     }
  341.  
  342.     if(rse & END_FR) /* If end of frame */
  343.     {
  344.         /* figure length of frame from 8237 */
  345.         outportb(DMA_RESETFF,0); /* reset firstlast ff */
  346.         bytecount = inportb(hp->dma_wcr);
  347.         bytecount += inportb(hp->dma_wcr) << 8;
  348.         bytecount = (hp->bufsiz - 1) - bytecount;
  349.  
  350.         if((rse & CRC_ERR)||(hp->rstate > ACTIVE)||(bytecount < 10))
  351.         {
  352.             if((bytecount >= 10) && (rse & CRC_ERR)) /* Ignore noise */
  353.                 hp->crcerr++;
  354.  
  355.             /* Reset buffer pointers */
  356.             hp->rstate = ACTIVE;
  357.             setup_rx_dma(hp);
  358.         }
  359.         else
  360.         {
  361.             /* Here we have a valid frame */
  362.             hp->rcvbuf->cnt = bytecount - 2;   /* Toss 2 crc bytes */
  363.             bp = alloc_mbuf(sizeof(struct phdr));
  364.             if(!bp)
  365.             {
  366.                 setup_rx_dma(hp);
  367.                 xwrite_scc(hp,hp->base+CTL,R0,ERR_RES);    /* error reset */
  368.                 restore(i_state);
  369.                 return;
  370.             };
  371.             bp->cnt = sizeof(struct phdr);            
  372.             phdr = (struct phdr *)bp->data;
  373.             phdr->type = CL_AX25;
  374.             phdr->iface = hp->iface;
  375.             bp->next = hp->rcvbuf;
  376.             hp->rcvbuf = NULLBUF;
  377.             enqueue(&Hopper,bp);       /* queue it in */
  378.             hp->rxframes++;
  379.  
  380.             /* packet queued - get buffer for next frame */
  381.             hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  382.             setup_rx_dma(hp);
  383.         } /* end good frame queued */
  384.     } /* end EOF check */
  385.  
  386.     xwrite_scc(hp,hp->base+CTL,R0,ERR_RES);    /* error reset */
  387.     restore(i_state);
  388. }
  389.  
  390. void
  391. a_txint(hp)
  392. register struct pichan *hp;
  393. {
  394.     register int16 cmd;
  395.     char i_state;
  396.  
  397.     i_state = dirps();
  398.     cmd = CTL+hp->base;
  399.  
  400.     switch(hp->tstate)
  401.     {
  402.         case IDLE:
  403.             /* Transmitter idle. Find a frame for transmission */
  404.             if((hp->sndbuf = dequeue(&hp->sndq)) == NULLBUF)
  405.             {
  406.                 rts(hp,OFF);
  407.                 restore(i_state);
  408.                 return;
  409.             }
  410.             /* If a buffer to send, we drop thru here */
  411.         case DEFER:        /* we may have deferred prev xmit attempt */
  412.  
  413.             /* Check DCD - debounce it */
  414.             /* See Intel Microcommunications Handbook, p2-308 */
  415.             xwrite_scc(hp,cmd,R0,RES_EXT_INT);
  416.             xwrite_scc(hp,cmd,R0,RES_EXT_INT);
  417.             if((xread_scc(hp,cmd,R0) & DCD) > 0)
  418.             {    hp->tstate = DEFER;
  419.                 tdelay(hp,100);
  420.                 /* defer until DCD transition or timeout */
  421.                 xwrite_scc(hp,cmd,R15,CTSIE|DCDIE);
  422.                 restore(i_state);
  423.                 return;
  424.             }
  425.  
  426.              if((rand() & 0xff) > uchar(hp->params[PERSIST]))
  427.              {
  428.                 hp->tstate = DEFER;
  429.                 tdelay(hp,hp->params[SLOTIME]);
  430.                 restore(i_state);
  431.                 return;
  432.             }
  433.  
  434.             xwrite_scc(hp,cmd,R5,TxCRC_ENAB|RTS|Tx8);/* Assert RTS early minimize */
  435.                                                 /* collision window */
  436.             rts(hp,ON);                        /* Transmitter on */
  437.             hp->tstate = ST_TXDELAY;
  438.             tdelay(hp,hp->params[TXDELAY]);
  439.             restore(i_state);
  440.             return;
  441.  
  442.         default:
  443.             break;
  444.     } /* end switch(hp->state) */
  445.  
  446.     restore(i_state);
  447. } /*a_txint */
  448.  
  449. static void
  450. b_rxint(hp)
  451. register struct pichan *hp;
  452. {
  453.     register int16 cmd;
  454.     char rse, i_state;
  455.     struct mbuf *bp;
  456.     struct phdr *phdr;
  457.  
  458.     i_state = dirps();        /* disable interrupts */
  459.     hp->rxints++;
  460.     cmd = CTL+hp->base;
  461.  
  462.     if ((xread_scc(hp,cmd,R0)) & Rx_CH_AV)
  463.     {
  464.         /* there is a char to be stored
  465.          * read special condition bits before reading the data char
  466.          */
  467.         rse = xread_scc(hp,cmd,R1); /* get status byte from R1 */
  468.         if(rse & Rx_OVR) {
  469.             /* Rx overrun - toss buffer */
  470.             hp->rcp = hp->rcvbuf->data;    /* reset buffer pointers */
  471.             hp->rcvbuf->cnt = 0;
  472.             hp->rstate = RXERROR;    /* set error flag */
  473.             hp->rovers++;
  474.         } else if(hp->rcvbuf->cnt >= hp->bufsiz) {
  475.             /* Too large -- toss buffer */
  476.             hp->rcp = hp->rcvbuf->data;    /* reset buffer pointers */
  477.             hp->rcvbuf->cnt = 0;
  478.             hp->rstate = TOOBIG;    /* when set, chars are not stored */
  479.         }
  480.         /* ok, we can store the received character now */
  481.         if(hp->rstate == ACTIVE) {            /* If no errors... */
  482.             *hp->rcp++ = xread_scc(hp,cmd,R8);    /* char to rcv buff */
  483.             hp->rcvbuf->cnt++;                   /* bump count */
  484.         } else {
  485.             /* got to empty FIFO */
  486.             (void) xread_scc(hp,cmd,R8);
  487.             xwrite_scc(hp,cmd,R0,ERR_RES);    /* reset err latch */
  488.             hp->rstate = ACTIVE;
  489.         }
  490.     }
  491.  
  492.     if(rse & END_FR)
  493.     {
  494.         /* END OF FRAME -- Make sure Rx was active */
  495.         if(hp->rcvbuf->cnt > 0)
  496.         {
  497.             if((rse & CRC_ERR)||(hp->rstate > ACTIVE)||(hp->rcvbuf->cnt < 10))
  498.             {
  499.                 if((hp->rcvbuf->cnt >= 10) && (rse & CRC_ERR))/* Ignore noise */
  500.                     hp->crcerr++;
  501.  
  502.                 hp->rcp = hp->rcvbuf->data;
  503.                 hp->rcvbuf->cnt = 0;
  504.             } else {
  505.                 /* Here we have a valid frame */
  506.                 hp->rcvbuf->cnt -= 2;           /* Toss 2 crc bytes */
  507.                 bp = alloc_mbuf(sizeof(struct phdr));
  508.                 if(!bp) /* we can't get memory - throw it away */
  509.                 {
  510.                     hp->rcp = hp->rcvbuf->data;
  511.                     hp->rcvbuf->cnt = 0;
  512.                     restore(i_state);
  513.                     return;
  514.                 }
  515.                 bp->cnt = sizeof(struct phdr);            
  516.                 phdr = (struct phdr *)bp->data;
  517.                 phdr->type = CL_AX25;
  518.                 phdr->iface = hp->iface;
  519.                 bp->next = hp->rcvbuf;
  520.                 enqueue(&Hopper,bp);       /* queue it in */
  521.                 hp->rxframes++;
  522.  
  523.                 /* packet queued - get buffer for next frame */
  524.                 hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  525.                 if(hp->rcvbuf == NULLBUF) {
  526.                     xwrite_scc(hp,cmd,R3,Rx8); /* No memory - abort rx */
  527.                     restore(i_state);
  528.                     return;
  529.                 }
  530.                 hp->rcp = hp->rcvbuf->data;
  531.                 hp->rcvbuf->cnt = 0;
  532.             } /* end good frame queued */
  533.         }  /* end check for active receive upon EOF */
  534.         hp->rstate = ACTIVE;    /* and clear error status */
  535.     } /* end EOF check */
  536.  
  537.     restore(i_state);
  538. }
  539.  
  540. static void
  541. b_txint(hp)
  542. register struct pichan *hp;
  543. {
  544.     register int16 cmd;
  545.     char i_state,c;
  546.  
  547.     i_state = dirps();
  548.     cmd = CTL+hp->base;
  549.  
  550.     if(hp->tstate != DEFER && hp->tstate)
  551.         hp->txints++;
  552.  
  553.     switch(hp->tstate) {
  554.     case CRCOUT:
  555.         hp->tstate = FLAGOUT;
  556.         tdelay(hp,hp->params[SQUELDELAY]);
  557.         restore(i_state);
  558.         return;
  559.     case IDLE:
  560.         /* Transmitter idle. Find a frame for transmission */
  561.         if((hp->sndbuf = dequeue(&hp->sndq)) == NULLBUF) {
  562.             /* Nothing to send - return to receive mode
  563.              * Tx OFF now - flag should have gone
  564.              */
  565.             rts(hp,OFF);
  566.             restore(i_state);
  567.             return;
  568.         }
  569.         /* If a buffer to send, we drop thru here */
  570.     case DEFER:        /* we may have deferred prev xmit attempt */
  571.         /* Check DCD - debounce it */
  572.         /* See Intel Microcommunications Handbook, p2-308 */
  573.         xwrite_scc(hp,cmd,R0,RES_EXT_INT);
  574.         xwrite_scc(hp,cmd,R0,RES_EXT_INT);
  575.         if((xread_scc(hp,cmd,R0) & DCD) > 0)
  576.         {    hp->tstate = DEFER;
  577.             tdelay(hp,100);
  578.             /* defer until DCD transition or timeout */
  579.             xwrite_scc(hp,cmd,R15,CTSIE|DCDIE);
  580.             restore(i_state);
  581.             return;
  582.         }
  583.  
  584.         /* p - persist calculation */
  585.         if(inportb(hp->cardbase+TMR0) > hp->params[PERSIST])
  586.         {    inportb(hp->cardbase+TMR0); /* Discard MSB */
  587.             hp->tstate = DEFER;
  588.             tdelay(hp,hp->params[SLOTIME]);
  589.             restore(i_state);
  590.             return;
  591.         }
  592.         inportb(hp->cardbase+TMR0); /* Discard MSB */
  593.  
  594.         rts(hp,ON);   /* Transmitter on */
  595.         hp->tstate = ST_TXDELAY;
  596.         tdelay(hp,hp->params[TXDELAY]);
  597.         restore(i_state);
  598.         return;
  599.  
  600.     case ACTIVE:
  601.         /* Here we are actively sending a frame */
  602.         if((c = PULLCHAR(&hp->sndbuf)) != -1){
  603.             xwrite_scc(hp,cmd,R8,c);        /* next char is gone */
  604.             /* stuffing a char satisfies Interrupt condition */
  605.         } else {
  606.             /* No more to send */
  607.             free_p(hp->sndbuf);
  608.             if((xread_scc(hp,cmd,R0) & 0x40)) /* Did we underrun? */
  609.             {    /* unexpected underrun */
  610.                 hp->tunders++;
  611.                 xwrite_scc(hp,cmd,R0,SEND_ABORT);
  612.                 hp->tstate = FLAGOUT;
  613.                 tdelay(hp,hp->params[SQUELDELAY]);
  614.                 restore(i_state);
  615.                 return;
  616.             }
  617.             hp->tstate = UNDERRUN; /* Now we expect to underrun */
  618.             /* Send flags on underrun */
  619.             if(hp->speed) { /* If externally clocked */
  620.                 xwrite_scc(hp,cmd,R10,CRCPS|NRZI);
  621.             } else {
  622.                 xwrite_scc(hp,cmd,R10,CRCPS);
  623.             }
  624.             xwrite_scc(hp,cmd,R0,RES_Tx_P); /* reset Tx Int Pend */
  625.         }
  626.         restore(i_state);
  627.         return;     /* back to wait for interrupt */
  628.     } /* end switch */
  629.     restore(i_state);
  630. }
  631.  
  632. /* Pi SIO External/Status interrupts (for the B channel)
  633.  * This can be caused by a receiver abort, or a Tx UNDERRUN/EOM.
  634.  * Receiver automatically goes to Hunt on an abort.
  635.  *
  636.  * If the Tx Underrun interrupt hits, change state and
  637.  * issue a reset command for it, and return.
  638.  */
  639. static void
  640. b_exint(hp)
  641. register struct pichan *hp;
  642. {
  643.     char st, i_state;
  644.     register int16 cmd;
  645.     char c;
  646.  
  647.     cmd = CTL+hp->base;
  648.     i_state = dirps();        /* disable interrupts */
  649.     hp->exints++;
  650.     st = xread_scc(hp,cmd,R0);     /* Fetch status */
  651.     /* reset external status latch */
  652.     xwrite_scc(hp,cmd,R0,RES_EXT_INT);
  653.  
  654.  
  655.     switch(hp->tstate) {
  656.     case ACTIVE:    /* Unexpected underrun */
  657.         free_p(hp->sndbuf);
  658.         xwrite_scc(hp,cmd,R0,SEND_ABORT);
  659.         hp->tstate = FLAGOUT;
  660.         hp->tunders++;
  661.         tdelay(hp,hp->params[SQUELDELAY]);
  662.         restore(i_state);
  663.         return;
  664.     case UNDERRUN:
  665.         hp->tstate = CRCOUT;
  666.         restore(i_state);
  667.         return;
  668.     case FLAGOUT: 
  669.         /* Find a frame for transmission */
  670.         if((hp->sndbuf = dequeue(&hp->sndq)) == NULLBUF) {
  671.             /* Nothing to send - return to receive mode
  672.              * Tx OFF now - flag should have gone
  673.              */
  674.             rts(hp,OFF);
  675.             hp->tstate = IDLE;
  676.             restore(i_state);
  677.             return;
  678.         }
  679.         /* Get next char to send */
  680.         pullup(&hp->sndbuf,&c,1);        /* one char at a time */
  681.         xwrite_scc(hp,cmd,R0,RES_Tx_CRC);    /* reset for next frame */
  682.  
  683.         /* Send abort on underrun */
  684.         if(hp->speed) { /* If externally clocked */
  685.             xwrite_scc(hp,cmd,R10,CRCPS|NRZI|ABUNDER);
  686.         } else {
  687.             xwrite_scc(hp,cmd,R10,CRCPS|ABUNDER);
  688.         }
  689.  
  690.         xwrite_scc(hp,cmd,R8,c);            /* First char out now */
  691.         xwrite_scc(hp,cmd,R0,RES_EOM_L);    /* Reset end of message latch */
  692.  
  693.         /* select transmit interrupts to enable */
  694.  
  695.         xwrite_scc(hp,cmd,R15,TxUIE);        /* allow Underrun int only */
  696.         xwrite_scc(hp,cmd,R0,RES_EXT_INT);
  697.         xwrite_scc(hp,cmd,R1,TxINT_ENAB|EXT_INT_ENAB);  /* Tx/Extern ints on */
  698.  
  699.         hp->tstate = ACTIVE;    /* char going out now */
  700.         restore(i_state);
  701.         return;
  702.  
  703.     case DEFER:
  704.         /* Check DCD - debounce it */
  705.         /* See Intel Microcommunications Handbook, p2-308 */
  706.         xwrite_scc(hp,cmd,R0,RES_EXT_INT);
  707.         xwrite_scc(hp,cmd,R0,RES_EXT_INT);
  708.         if((xread_scc(hp,cmd,R0) & DCD) > 0)
  709.         {    hp->tstate = DEFER;
  710.             tdelay(hp,100);
  711.             /* defer until DCD transition or timeout */
  712.             xwrite_scc(hp,cmd,R15,CTSIE|DCDIE);
  713.             restore(i_state);
  714.             return;
  715.         }
  716.  
  717.         /* p - persist calculation */
  718.         if(inportb(hp->cardbase+TMR0) > hp->params[PERSIST])
  719.         {    inportb(hp->cardbase+TMR0); /* Discard MSB */
  720.             hp->tstate = DEFER;
  721.             tdelay(hp,hp->params[SLOTIME]);
  722.             restore(i_state);
  723.             return;
  724.         }
  725.         inportb(hp->cardbase+TMR0); /* Discard MSB */
  726.  
  727.         rts(hp,ON);   /* Transmitter on */
  728.         hp->tstate = ST_TXDELAY;
  729.         tdelay(hp,hp->params[TXDELAY]);
  730.         restore(i_state);
  731.         return;
  732.  
  733.     case ST_TXDELAY:
  734.  
  735.         /* Get next char to send */
  736.         pullup(&hp->sndbuf,&c,1);        /* one char at a time */
  737.         xwrite_scc(hp,cmd,R0,RES_Tx_CRC);    /* reset for next frame */
  738.  
  739.         /* Send abort on underrun */
  740.         if(hp->speed) { /* If externally clocked */
  741.             xwrite_scc(hp,cmd,R10,CRCPS|NRZI|ABUNDER);
  742.         } else {
  743.             xwrite_scc(hp,cmd,R10,CRCPS|ABUNDER);
  744.         }
  745.  
  746.         xwrite_scc(hp,cmd,R8,c);            /* First char out now */
  747.         xwrite_scc(hp,cmd,R0,RES_EOM_L);    /* Reset end of message latch */
  748.  
  749.         /* select transmit interrupts to enable */
  750.  
  751.         xwrite_scc(hp,cmd,R15,TxUIE);        /* allow Underrun int only */
  752.         xwrite_scc(hp,cmd,R0,RES_EXT_INT);
  753.         xwrite_scc(hp,cmd,R1,TxINT_ENAB|EXT_INT_ENAB);  /* Tx/Extern ints on */
  754.  
  755.         hp->tstate = ACTIVE;    /* char going out now */
  756.         restore(i_state);
  757.         return;
  758.  
  759.     }
  760.  
  761.     /* Receive Mode only
  762.      * This triggers when hunt mode is entered, & since an ABORT
  763.      * automatically enters hunt mode, we use that to clean up
  764.      * any waiting garbage
  765.      */
  766.     if((hp->rstate == ACTIVE) && (st & BRK_ABRT)) {
  767.         (void) xread_scc(hp,cmd,R8);
  768.         (void) xread_scc(hp,cmd,R8);
  769.         (void) xread_scc(hp,cmd,R8);
  770.         hp->rcp = hp->rcvbuf->data;
  771.         hp->rcvbuf->cnt = 0;          /* rewind on DCD transition */
  772.     }
  773.  
  774.     restore(i_state);
  775. }
  776.  
  777. /* SET Transmit or Receive Mode
  778.  * Set RTS (request-to-send) to modem on Transmit
  779.  */
  780. static void
  781. rts(hp,x)
  782. register struct pichan *hp;
  783. int16 x;
  784. {
  785.     int16 tc;
  786.     long br;
  787.     int16 cmd;
  788.  
  789.     cmd = CTL+hp->base;
  790.  
  791.     /* Reprogram BRG and turn on transmitter to send flags */
  792.     if(x == ON) {                /* Turn Tx ON and Receive OFF */
  793.         xwrite_scc(hp,cmd,R15,0);    /* Exints off first to avoid abort int */
  794.         xwrite_scc(hp,cmd,R3,Rx8);    /* Rx off */
  795.         hp->rstate = IDLE;
  796.         if(cmd & 2) /* if channel a */
  797.             xwrite_scc(hp,cmd,R1,WT_FN_RDYFN|EXT_INT_ENAB);    /* Set up for TX dma */
  798.         else
  799.             xwrite_scc(hp,cmd,R1,0);    /* No interrupts */
  800.  
  801.         if(hp->speed) {                        /* if internally clocked */
  802.             br = hp->speed;                     /* get desired speed */
  803.             tc = (XTAL/br)-2;                /* calc 1X BRG divisor */
  804.             xwrite_scc(hp,cmd,R12,tc&0xFF);     /* lower byte */
  805.             xwrite_scc(hp,cmd,R13,(tc>>8)&0xFF);/* upper byte */
  806.         }
  807.  
  808.         xwrite_scc(hp,cmd,R5,TxCRC_ENAB|RTS|TxENAB|Tx8|DTR);
  809.         /* Transmitter now on */
  810.     } else {    /* Tx OFF and Rx ON */
  811.         hp->tstate = IDLE;
  812.         xwrite_scc(hp,cmd,R5,Tx8|DTR);     /*  TX off */
  813.  
  814.         if(hp->speed) {                            /* if internally clocked */
  815.             /* Reprogram BRG for 32x clock for receive DPLL */
  816.             xwrite_scc(hp,cmd,R14,BRSRC);            /* BRG off, keep Pclk source */
  817.             br = hp->speed;                         /* get desired speed */
  818.             tc = ((XTAL/32)/br)-2;                /* calc 32X BRG divisor */
  819.             xwrite_scc(hp,cmd,R12,tc&0xFF);            /* lower byte */
  820.             xwrite_scc(hp,cmd,R13,(tc>>8)&0xFF);    /* upper bite */
  821.             xwrite_scc(hp,cmd,R14,BRSRC|SEARCH);    /* SEARCH mode, BRG source */
  822.             xwrite_scc(hp,cmd,R14,BRSRC|BRENABL);    /* Enable the BRG */
  823.         }
  824.  
  825.         /* Now, turn on the receiver and hunt for a flag */
  826.         xwrite_scc(hp,cmd,R3,RxENABLE|RxCRC_ENAB|Rx8);
  827.         hp->rstate = ACTIVE;            /* Normal state */
  828.  
  829.         if(cmd & 2) {/* if channel a */
  830.             setup_rx_dma(hp);
  831.         } else {
  832.         /*    xwrite_scc(hp,cmd,R0,ERR_RES); */    /* reset error bits */
  833.             hp->rcp = hp->rcvbuf->data;    /* reset buffer pointers */
  834.             hp->rcvbuf->cnt = 0;
  835.             xwrite_scc(hp,cmd,R1,(INT_ALL_Rx|EXT_INT_ENAB));
  836.         }
  837.  
  838.         xwrite_scc(hp,cmd,R15,BRKIE);        /* allow ABORT int */
  839.     }
  840. }
  841.  
  842. void
  843. setup_rx_dma(hp)
  844. register struct pichan *hp;
  845. {
  846.     unsigned buf_offset, buf_segment;
  847.     int cmd;
  848.     char dummy;
  849.     long longseg, dma_abs, dma_page;
  850.     char i_state;
  851.  
  852.     i_state = dirps();        /* disable interrupts */
  853.  
  854.     cmd = hp->base+CTL;
  855.  
  856.     if(!hp->rcvbuf) /* No rx buffer available */
  857.     {    restore(i_state);
  858.         return;
  859.     }
  860.  
  861.     /* Calculate high order 4 bits of the buffer area and store
  862.         them in the DMA page register
  863.     */
  864.  
  865.     buf_offset = FP_OFF(hp->rcvbuf->data);
  866.     buf_segment= FP_SEG(hp->rcvbuf->data);
  867.     longseg = (long) buf_segment;
  868.     dma_abs = (longseg << 4) + (long) buf_offset;
  869.     dma_page = dma_abs >> 16;
  870.  
  871.     /* Get ready for RX DMA */
  872.     xwrite_scc(hp,cmd,R1,WT_FN_RDYFN|WT_RDY_RT|INT_ERR_Rx|EXT_INT_ENAB);
  873.     outportb(DMA_MASK, DMA_DISABLE|hp->dmachan); /* Disable DMA chan */
  874.     /* Set DMA mode register to single transfers, incrementing address,
  875.         auto init, writes
  876.     */
  877.     outportb(DMA_MODE,DMA_RX_MODE|hp->dmachan);
  878.  
  879.     outportb(hp->page_addr,dma_page); /* Store in  64K DMA page */
  880.     outportb(DMA_RESETFF,0); /* reset byte pointer flipflop */
  881.     outportb(hp->dma_dest,dma_abs);    /* Output buffer start (dest) address */
  882.     outportb(hp->dma_dest,dma_abs >> 8);
  883.     /* output DMA maximum byte count */
  884.     outportb(hp->dma_wcr,hp->bufsiz - 1);
  885.     outportb(hp->dma_wcr, (hp->bufsiz - 1) >> 8);
  886.     /* Unmask channel 1 (start DMA) */
  887.     outportb(DMA_MASK, DMA_ENABLE|hp->dmachan); /* Enable DMA chan */
  888.  
  889.     /* If a packet is already coming in, this line is supposed
  890.         to mess up the crc to avoid receiving a partial packet */
  891.     xwrite_scc(hp,cmd,R0,RES_Rx_CRC);
  892.  
  893.     /* Enable RX dma */
  894.     xwrite_scc(hp,cmd,R1,WT_RDY_ENAB|WT_FN_RDYFN|WT_RDY_RT|INT_ERR_Rx|EXT_INT_ENAB);
  895.  
  896.     restore(i_state);
  897. }
  898.  
  899. void
  900. setup_tx_dma(hp,buffer,length)
  901. struct pichan *hp;
  902. char *buffer;
  903. int length;
  904. {
  905.     unsigned buf_offset, buf_segment;
  906.     long longseg, dma_abs, dma_page;
  907.     char i_state;
  908.  
  909.     i_state = dirps();        /* disable interrupts */
  910.  
  911.     /* Calculate high order 4 bits of the buffer area and store
  912.         them in the DMA page register
  913.     */
  914.  
  915.     buf_offset = FP_OFF(buffer);
  916.     buf_segment= FP_SEG(buffer);
  917.     longseg = (long) buf_segment;
  918.     dma_abs = (longseg << 4) + (long) buf_offset;
  919.     dma_page = dma_abs >> 16;
  920.  
  921.     outportb(DMA_MASK, DMA_DISABLE|hp->dmachan); /* Disable DMA chan */
  922.     if(((dma_abs + length) >> 16) != dma_page)
  923.         tprintf("PI: ERROR - DMA page boundary violation\n");
  924.     --length; /* Adjust length for DMA chip */
  925.     /* Set DMA mode register to single transfers, incrementing address,
  926.         no auto init, reads
  927.     */
  928.     outportb(DMA_MODE,DMA_TX_MODE|hp->dmachan);
  929.  
  930.     outportb(hp->page_addr,dma_page); /* Store in 64K DMA page */
  931.     outportb(DMA_RESETFF,0); /* reset byte pointer flipflop */
  932.     outportb(hp->dma_dest,dma_abs);    /* Output buffer start (source) address */
  933.     outportb(hp->dma_dest,dma_abs >> 8);
  934.     /* output byte count */
  935.     outportb(hp->dma_wcr,length);
  936.     outportb(hp->dma_wcr, (length) >> 8);
  937.  
  938.     restore(i_state);
  939. }
  940.  
  941. /* Initialize pi controller parameters */
  942. static int
  943. scc_init(hp)
  944. register struct pichan *hp;
  945. {
  946.     int16 tc;
  947.     long br;
  948.     char i_state;
  949.     register int16 cmd;
  950.  
  951.     /* Initialize 8530 channel for SDLC operation */
  952.  
  953.     cmd = CTL+hp->base;
  954. #ifdef    notdef
  955.     tprintf("Pi: Initializing Channel %c - Base = %x\n",cmd&2?'A':'B',cmd&~CTL);
  956. #endif
  957.     i_state = dirps();
  958.  
  959.     switch(cmd & 2){
  960.     case 2:
  961.         xwrite_scc(hp,cmd,R9,CHRA);    /* Reset channel A */
  962.         xwrite_scc(hp,cmd,R2,0xff); /* Initialize interrupt vector */
  963.         break;
  964.     case 0:
  965.         xwrite_scc(hp,cmd,R9,CHRB);    /* Reset channel B */
  966.         break;
  967.     }
  968.  
  969.     /* Deselect all Rx and Tx interrupts */
  970.     xwrite_scc(hp,cmd,R1,0);
  971.  
  972.     /* Turn off external interrupts (like CTS/CD) */
  973.     xwrite_scc(hp,cmd,R15,0);
  974.  
  975.     /* X1 clock, SDLC mode */
  976.     xwrite_scc(hp,cmd,R4,SDLC|X1CLK);           /* SDLC mode and X1 clock */
  977.  
  978.     /* Now some misc Tx/Rx parameters */
  979.     /* CRC PRESET 1, NRZI Mode */
  980.     if(hp->speed) {
  981.         xwrite_scc(hp,cmd,R10,CRCPS|NRZI);
  982.         /* Tx Clk from BRG. Rcv Clk from DPLL, TRxC pin outputs DPLL */
  983.         xwrite_scc(hp,cmd,R11,TCBR|RCDPLL|TRxCDP|TRxCOI);
  984.     } else {
  985.         xwrite_scc(hp,cmd,R10,CRCPS);
  986.         /* Tx Clk from Trxcl. Rcv Clk from Rtxcl, TRxC pin is input */
  987.         xwrite_scc(hp,cmd,R11,TCTRxCP);
  988.     }
  989.  
  990.     /* Null out SDLC start address */
  991.     xwrite_scc(hp,cmd,R6,0);
  992.  
  993.     /* SDLC flag */
  994.     xwrite_scc(hp,cmd,R7,FLAG);
  995.  
  996.     /* Set up the Transmitter but don't enable it */
  997.     /*  DTR, 8 bit TX chars only - TX NOT ENABLED */
  998.     xwrite_scc(hp,cmd,R5,Tx8|DTR);
  999.  
  1000.     /* Receiver - intial setup only - more later */
  1001.     xwrite_scc(hp,cmd,R3,Rx8);            /* 8 bits/char */
  1002.  
  1003.     /* Setting up BRG now - turn it off first */
  1004.     xwrite_scc(hp,cmd,R14,BRSRC);         /* BRG off, but keep Pclk source */
  1005.  
  1006.     /* set the 32x time constant for the BRG in Receive mode */
  1007.  
  1008.     if(hp->speed) {
  1009.         br = hp->speed;             /* get desired speed */
  1010.         tc = ((XTAL/32)/br)-2;            /* calc 32X BRG divisor */
  1011.     } else {
  1012.         tc = 14;
  1013.     }
  1014.  
  1015.     xwrite_scc(hp,cmd,R12,tc&0xFF);      /* lower byte */
  1016.     xwrite_scc(hp,cmd,R13,(tc>>8)&0xFF); /* upper bite */
  1017.  
  1018.     /* Following subroutine sets up and ENABLES the receiver */
  1019.     rts(hp,OFF);           /* TX OFF and RX ON */
  1020.  
  1021.     if(hp->speed)
  1022.         xwrite_scc(hp,cmd,R14,BRSRC|SSBR);/*DPLL frm BRG, BRG src PCLK */
  1023.     else
  1024.         xwrite_scc(hp,cmd,R14,BRSRC|SSRTxC);/*DPLL frm rtxc,BRG src PCLK */
  1025.  
  1026.     xwrite_scc(hp,cmd,R14,BRSRC|SEARCH);       /* SEARCH mode, keep BRG source */
  1027.     xwrite_scc(hp,cmd,R14,BRSRC|BRENABL);    /* Enable the BRG */
  1028.  
  1029.     if(!(cmd & 2)) /* if channel b */
  1030.         xwrite_scc(hp,cmd,R1,(INT_ALL_Rx|EXT_INT_ENAB));
  1031.  
  1032.     xwrite_scc(hp,cmd,R15,BRKIE);        /* ABORT int */
  1033.  
  1034.     /* Now, turn on the receiver and hunt for a flag */
  1035.     xwrite_scc(hp,cmd,R3,RxENABLE|RxCRC_ENAB|Rx8);
  1036.  
  1037.     restore(i_state);
  1038.     return 0;
  1039. }
  1040.  
  1041. /* Process to recover from ibuffails.
  1042.    This could be done in the function network() in config.c,
  1043.    to save a context switch.  I put it here so the driver would
  1044.    be more self contained.
  1045. */
  1046. void
  1047. buf_recover(unused,b,a)
  1048. int unused;
  1049. void *b;    /* Unused */
  1050. void *a;    /* Unused */
  1051. {
  1052.     struct pichan *hp0, *hp1;
  1053.     char i_state;
  1054.     int i;
  1055.  
  1056.     for(;;)
  1057.     {    pwait(NULL);
  1058.  
  1059.         for(i=0; i<pinbr; i++) /* for each card */
  1060.         {
  1061.             hp0 = &Pichan[i];
  1062.             hp1 = &Pichan[i + 1];
  1063.             if(!hp0->rcvbuf) /* No rx buffer allocated */
  1064.             {
  1065.                 i_state = dirps();
  1066.                 hp0->rcvbuf = alloc_mbuf(hp0->bufsiz);
  1067.                 restore(i_state);
  1068.                 setup_rx_dma(hp0);
  1069.             }
  1070.             i_state = dirps();
  1071.             if(!hp1->rcvbuf && (hp1->rstate == ACTIVE))/* No rx buf allocated */
  1072.             {
  1073.                 if((hp1->rcvbuf = alloc_mbuf(hp1->bufsiz)) != NULL)
  1074.                 {    
  1075.                     hp1->rcp = hp1->rcvbuf->data;
  1076.                     hp1->rcvbuf->cnt = 0;
  1077.                     xwrite_scc(hp1,CTL+hp1->base,R3,RxENABLE|RxCRC_ENAB|Rx8);
  1078.                 }
  1079.             }
  1080.             restore(i_state);
  1081.         }
  1082.     }
  1083. }
  1084.  
  1085. /* Attach a PI interface to the system
  1086.  * argv[0]: hardware type, must be "pi"
  1087.  * argv[1]: I/O address, e.g., "0x300"
  1088.  * argv[2]: vector, e.g., "2"
  1089.  * argv[3]: dma channel (1..3)
  1090.  * argv[4]: mode, must be:
  1091.  *        "ax25" (AX.25 UI frame format)
  1092.  * argv[5]: interface label, e.g., "pi0"
  1093.  * argv[6]: receiver packet buffer size in bytes
  1094.  * argv[7]: maximum transmission unit, bytes
  1095.  * argv[8]: channel A interface speed, e.g, "1200", 0 = ext. clock
  1096.  * argv[9]: channel B interface speed
  1097.  * argv[10]: First IP address, optional (defaults to Ip_addr);
  1098.  * argv[11]: Second IP address, optional (defaults to Ip_addr);
  1099.  */
  1100. int
  1101. pi_attach(argc,argv)
  1102. int argc;
  1103. char *argv[];
  1104. {
  1105.     extern void refiq();
  1106.     register struct iface *if_pca,*if_pcb;
  1107.     struct pichan *hp;
  1108.     int dev;
  1109.     char i_state;
  1110.     int n;
  1111.  
  1112.     refiq(); /* replenish interrupt buffer pool (in mbuf.c) */
  1113.     if(acc_delay == 0)    /* Only do this once */
  1114.         set_acc_delay();/* Adapt recovery time delay to processor speed */
  1115.  
  1116.     /* Quick check to make sure args are good and mycall is set */
  1117.     if(strcmp(argv[4],"ax25") != 0){
  1118.         tprintf("PI: Mode %s unknown for interface %s\n",
  1119.             argv[4],argv[5]);
  1120.         return -1;
  1121.     }
  1122.     if(if_lookup(argv[5]) != NULLIF){
  1123.         tprintf("PI: Interface %s already exists\n",argv[5]);
  1124.         return -1;
  1125.     }
  1126.     if(Mycall[0] == '\0'){
  1127.         tprintf("PI: Set mycall first\n");
  1128.         return -1;
  1129.     }
  1130.     /* Note: each card must have a unique address, IRQ and DMA 
  1131.      */
  1132.  
  1133.     if(pinbr >= PIMAX) {
  1134.         tprintf("PI: Maximum of %d PI cards supported\n",PIMAX);
  1135.         return -1;
  1136.     }
  1137.     dev = pinbr++;
  1138.  
  1139.     /* Initialize hardware-level control structure */
  1140.     Pi[dev].addr = htoi(argv[1]);
  1141.     Pi[dev].vec = htoi(argv[2]);
  1142.  
  1143.     /* Set up counter chip */
  1144.     outportb(Pi[dev].addr+TMRCMD, SC0|LSB_MSB|MODE3); /* 500 uS square wave */
  1145.     for(n=0; n<5;n++) /* satisfy access time restriction */
  1146.         ;
  1147.     outportb(Pi[dev].addr+TMR0, 922 & 0xFF); 
  1148.     for(n=0; n<5;n++) /* satisfy access time restriction */
  1149.         ;
  1150.     outportb(Pi[dev].addr+TMR0, 922 >> 8);
  1151.     for(n=0; n<5;n++) /* satisfy access time restriction */
  1152.         ;
  1153.  
  1154.     /* Save original interrupt vector */
  1155.     Pi[dev].oldvec = getirq(Pi[dev].vec);
  1156.  
  1157.     /* Set new interrupt vector */
  1158.     if(setirq(Pi[dev].vec,pihandle[dev]) == -1){
  1159.         tprintf("PI: IRQ %u out of range\n",Pi[dev].vec);
  1160.         pinbr--;
  1161.         return -1;
  1162.     }
  1163.  
  1164.     if((atoi(argv[3]) < 1) || (atoi(argv[3]) > 3)){
  1165.         tprintf("PI: DMA %d out of range\n",atoi(argv[3]));
  1166.         pinbr--;
  1167.         return -1;
  1168.     }
  1169.  
  1170.     /* Create interface structures and fill in details */
  1171.     if_pca = (struct iface *)callocw(1,sizeof(struct iface));
  1172.     if_pcb = (struct iface *)callocw(1,sizeof(struct iface));
  1173.  
  1174.  
  1175.     if_pca->addr = if_pcb->addr = Ip_addr;
  1176.     if(argc > 10)
  1177.         if_pca->addr = resolve(argv[10]);
  1178.     if(argc > 11)
  1179.         if_pcb->addr = resolve(argv[11]);
  1180.  
  1181.     if(if_pca->addr == 0 || if_pcb->addr == 0){
  1182.         tprintf("PI: No IP address");
  1183.         free((char *)if_pca);
  1184.         free((char *)if_pcb);
  1185.         return -1;
  1186.     }
  1187.  
  1188.     /* Append "a" to interface associated with A channel */
  1189.     if_pca->name = malloc((unsigned)strlen(argv[5])+2);
  1190.     strcpy(if_pca->name,argv[5]);
  1191.     strcat(if_pca->name,"a");
  1192.     /* Append "b" to interface associated with B channel */
  1193.     if_pcb->name = malloc((unsigned)strlen(argv[5])+2);
  1194.     strcpy(if_pcb->name,argv[5]);
  1195.     strcat(if_pcb->name,"b");
  1196.  
  1197.     if_pcb->mtu = if_pca->mtu = atoi(argv[7]);
  1198.     if_pcb->type = if_pca->type = CL_AX25;
  1199.     if_pcb->ioctl = if_pca->ioctl = pi_ctl;
  1200.     if_pca->dev = 2*dev;            /* pi0a */
  1201.     if_pcb->dev = 2*dev + 1;        /* pi0b */
  1202.     if_pcb->stop = if_pca->stop = pi_stop;
  1203.     if_pcb->output = if_pca->output = ax_output;
  1204.     if_pcb->raw = if_pca->raw = pi_raw;
  1205.  
  1206.     if(strcmp(argv[4],"ax25") == 0) {
  1207.         /* Must be true, was checked at top */
  1208.         if_pcb->send = if_pca->send = ax_send;
  1209.         if(if_pcb->hwaddr == NULLCHAR)
  1210.             if_pcb->hwaddr = mallocw(AXALEN);
  1211.         memcpy(if_pcb->hwaddr,Mycall,AXALEN);
  1212.         if(if_pca->hwaddr == NULLCHAR)
  1213.             if_pca->hwaddr = mallocw(AXALEN);
  1214.         memcpy(if_pca->hwaddr,Mycall,AXALEN);
  1215.     }
  1216.     /* Link em in to the interface chain */
  1217.     if_pca->next = if_pcb;
  1218.     if_pcb->next = Ifaces;
  1219.     Ifaces = if_pca;
  1220.  
  1221.     /* set params in pichan table for CHANNEL B */
  1222.  
  1223.     hp = &Pichan[2*dev+1];                /* pi1 is offset 1 */
  1224.     hp->dmachan = 0; /* Channel B does not have dma */
  1225.     hp->cardbase = Pi[dev].addr;
  1226.     hp->iface = if_pcb;
  1227.     hp->stata = Pi[dev].addr + CHANA + CTL;    /* permanent status */
  1228.     hp->statb = Pi[dev].addr + CHANB + CTL;    /* addrs for CHANA/B*/
  1229.     hp->speed = (int16)atoi(argv[9]);
  1230.     hp->base = Pi[dev].addr + CHANB;
  1231.     hp->bufsiz = atoi(argv[6]);
  1232.     hp->tstate = IDLE;
  1233.     /* default KISS Params */
  1234.     hp->params[TXDELAY] = 30;        /* 300 Ms */
  1235.     hp->params[PERSIST] = 128;        /* 50% persistence */
  1236.     hp->params[SLOTIME] = 30;        /* 300 Ms */
  1237.     hp->params[SQUELDELAY] = 3;        /* 30 Ms */
  1238.  
  1239.     xwrite_scc(hp,CTL+hp->stata,R9,FHWRES);     /* Hardware reset */
  1240.                         /* one time only */
  1241.     /* Disable interrupts with Master interrupt ctrl reg */
  1242.     xwrite_scc(hp,CTL+hp->stata,R9,0);
  1243.  
  1244.     scc_init(hp);
  1245.  
  1246.     /* Pre-allocate a receive buffer */
  1247.     i_state = dirps();
  1248.     hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  1249.     restore(i_state);
  1250.     if(hp->rcvbuf == NULLBUF) {
  1251.         /* No memory, abort receiver */
  1252.         tprintf("PI: No memory available for receive buffers\n");
  1253.         /* Restore original interrupt vector */
  1254.         setirq(Pi[dev].vec,Pi[dev].oldvec);
  1255.         pinbr--;
  1256.         return(-1);
  1257.     }
  1258.     hp->rcp = hp->rcvbuf->data;
  1259.     hp->rcvbuf->cnt = 0;
  1260.     hp->sndq = NULLBUF;
  1261.  
  1262.     /* set params in pichan table for CHANNEL A */
  1263.     hp = &Pichan[2*dev];            /* pi0a is offset 0 */
  1264.     hp->dmachan = (unsigned char)atoi(argv[3]);
  1265.     /* Figure out where the dma page register is. */
  1266.     switch(hp->dmachan) {
  1267.         case 0:
  1268.         case 1:
  1269.             hp->page_addr = 0x83;
  1270.             break;
  1271.         case 2:
  1272.             hp->page_addr = 0x81;
  1273.             break;
  1274.         case 3:
  1275.             hp->page_addr = 0x82;
  1276.             break;
  1277.     }
  1278.     hp->dma_dest = hp->dmachan * 2;
  1279.     hp->dma_wcr = hp->dma_dest + 1;
  1280.  
  1281.     hp->cardbase = Pi[dev].addr;
  1282.     hp->iface = if_pca;
  1283.     hp->speed = (int16)atoi(argv[8]);
  1284.     hp->base = Pi[dev].addr + CHANA;
  1285.     hp->bufsiz = atoi(argv[6]);
  1286.     hp->tstate = IDLE;
  1287.     /* default KISS Params */
  1288.     hp->params[TXDELAY] = 15;        /* 15 mS */
  1289.     hp->params[PERSIST] = 128;        /* 50% persistence */
  1290.     hp->params[SLOTIME] = 15;        /* 15 mS */
  1291.     hp->params[SQUELDELAY] = 1;        /* 1 mS */
  1292.     newproc("buf_recover",256,buf_recover,0,hp,NULL,0);
  1293.  
  1294.  
  1295.     /* Pre-allocate a receive buffer */
  1296.     i_state = dirps();/* buffer is allocated with ints off */
  1297.     hp->rcvbuf = alloc_mbuf(hp->bufsiz);
  1298.     restore(i_state);
  1299.     if(hp->rcvbuf == NULLBUF) {
  1300.         /* No memory, abort receiver */
  1301.         tprintf("PI: No memory available for receive buffers\n");
  1302.         /* Restore original interrupt vector */
  1303.         setirq(Pi[dev].vec,Pi[dev].oldvec);
  1304.         pinbr--;
  1305.         return -1;
  1306.     }
  1307.     hp->rcvbuf->cnt = 0;
  1308.     hp->sndq = NULLBUF;
  1309.  
  1310.     /* Get a buffer for tx which does not cross a dma boundary */
  1311.     i_state = dirps();/* buffer is allocated with ints off */
  1312.     hp->txdmabuf = (char *)alloc_mbuf(hp->bufsiz);
  1313.     restore(i_state);
  1314.     if(!hp->txdmabuf)
  1315.         tprintf("PI: No memory available for transmit buffer");
  1316.  
  1317.     scc_init(hp);
  1318.     xwrite_scc(hp,CTL+hp->base,R9,MIE|NV);        /* master interrupt enable */
  1319.  
  1320.     /* Enable interrupt */
  1321.     maskon(Pi[dev].vec);
  1322.  
  1323.     return 0;
  1324. }
  1325.  
  1326. /* Shut down interface */
  1327. int
  1328. pi_stop(iface)
  1329. struct iface *iface;
  1330. {
  1331.     int16 dev;
  1332.     struct pichan *hp;
  1333.  
  1334.     dev = iface->dev;
  1335.     if(dev & 1) /* Because there are 2 devices per card */
  1336.         return 0;
  1337.     dev >>= 1;    /* Convert back into pi number */
  1338.     hp = &Pichan[2*dev];            /* pi0a is offset 0 */
  1339.  
  1340.     outportb(DMA_MASK, DMA_DISABLE|hp->dmachan); /* Disable DMA channel */
  1341.  
  1342.     /* Turn off interrupts */
  1343.     maskoff(Pi[dev].vec);
  1344.  
  1345.     /* Restore original interrupt vector */
  1346.     setirq(Pi[dev].vec,Pi[dev].oldvec);
  1347.  
  1348.     /* Force hardware reset */
  1349.     xwrite_scc(&Pichan[2*dev],CTL+Pi[dev].addr + CHANA,R9,FHWRES);
  1350.  
  1351.     return 0;
  1352. }
  1353.  
  1354. /* Send raw packet on pi card */
  1355. int
  1356. pi_raw(iface,bp)
  1357. struct iface *iface;
  1358. struct mbuf *bp;
  1359. {
  1360.     char i_state;
  1361.     char kickflag;
  1362.     struct pichan *hp;
  1363.  
  1364.     dump(iface,IF_TRACE_OUT,CL_AX25,bp);
  1365.     iface->rawsndcnt++;
  1366.     iface->lastsent = secclock();
  1367.  
  1368.     hp = &Pichan[iface->dev];
  1369.     kickflag = (hp->sndq == NULLBUF) & (hp->sndbuf == NULLBUF);
  1370.     enqueue(&hp->sndq,bp);
  1371.     hp->enqueued++;
  1372.     if(kickflag)            /* simulate interrupt to xmit */
  1373.     {
  1374.         switch(hp->base & 2)
  1375.         {
  1376.             case 2:
  1377.                 a_txint(hp);        /* process interrupt */
  1378.                 break;
  1379.             case 0:
  1380.                 i_state = dirps();
  1381.                 if(hp->tstate == IDLE)
  1382.                     b_txint(hp);
  1383.                 restore(i_state);
  1384.                 break;
  1385.         }
  1386.     }
  1387.     return 0;
  1388. }
  1389.  
  1390. /* display PI Channel stats */
  1391. int
  1392. dopistat()
  1393. {
  1394.     struct pichan *hp;
  1395.     int i;
  1396.  
  1397.     tprintf("PI Board Statistics:\n\n");
  1398.     tprintf("Base Addr  Rxints  Txints  Exints  TxFrms  RxFrms  Crcerr  RxOvrs  TxUndr \n");
  1399.     tprintf("---------  ------  ------  ------  ------  ------  ------  ------  ------ \n");
  1400.     for(i=0; i<pinbr*2; i++)
  1401.     {
  1402.         hp = &Pichan[i];
  1403.  
  1404.         tprintf("0x%03x    % 8lu% 8lu% 8lu% 8u% 8u% 8u% 8u% 8u\nRcv State=%s ",
  1405.             hp->base, hp->rxints, hp->txints, hp->exints, hp->enqueued,
  1406.             hp->rxframes, hp->crcerr, hp->rovers, hp->tunders,
  1407.             hp->rstate==0 ?
  1408.                 "IDLE" : hp->rstate==1 ?
  1409.                     "ACTIVE" : hp->rstate==2 ?
  1410.                         "RXERROR" : hp->rstate==3 ?
  1411.                             "RXABORT":"TOOBIG"
  1412.         );
  1413.  
  1414.         tprintf("Tstate = %s\n",
  1415.             hp->tstate == 0 ?
  1416.                 "IDLE" : hp->tstate == 1 ?
  1417.                     "ACTIVE" : hp->tstate == 2 ?
  1418.                         "UNDERRUN" : hp->tstate == 3 ?
  1419.                             "FLAGOUT" : hp->tstate == 4 ?
  1420.                                 "DEFER" : hp->tstate == 5 ?
  1421.                                     "TXDELAY" : "CRCOUT"
  1422.         );
  1423.     }
  1424.     return 0;
  1425. }
  1426.  
  1427. /* Subroutine to set kiss params in channel tables */
  1428. int
  1429. pi_ctl(iface,argc,argv)
  1430. struct iface *iface;
  1431. int argc;
  1432. char *argv[];
  1433. {
  1434.     struct pichan *hp;
  1435.     int p, v;
  1436.  
  1437.     if(argc < 2){
  1438.         tprintf("PI: Insufficient parameters\n");
  1439.         return 1;
  1440.     }
  1441.     hp = &Pichan[iface->dev]; /* point to channel table */
  1442.     p = atoi(argv[0]);          /* parameter in binary */
  1443.     if(p > 3){
  1444.         tprintf("PI: Parameter %d out of range\n",p);
  1445.         return 1;
  1446.     }
  1447.     v = atoi(argv[1]);          /* value to be loaded */
  1448.     hp->params[p] = v;          /* Stuff in Kiss array */
  1449.     return 0;
  1450. }
  1451.  
  1452.