home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / SCC.C < prev    next >
C/C++ Source or Header  |  1992-05-19  |  46KB  |  1,498 lines

  1. /* Generic driver for Z8530 boards, modified from the PE1CHL
  2.  * driver for use with NOS. This version also supports the NRS
  3.  * mode when used as an asynch port. Device setup is similar to
  4.  * that of the PE1CHL version, with the addition of user specification
  5.  * of buffer size (bufsize). See the file "scc.txt" for general
  6.  * information on the use of this driver and setup procedures.
  7.  *
  8.  * General differences between this driver and the original version:
  9.  *
  10.  * 1) Slip encoding and decoding is not done in the driver, but
  11.  *    using the routines in slip.c, and these routines are supported
  12.  *    in a manner similar to the asynch routines for the 8250. The
  13.  *    input is handled via fifo buffer, while output is direct. The
  14.  *    routines scc_send and get_scc are called via pointers in the
  15.  *    Slip and Nrs structs for the parcticular channel.
  16.  *
  17.  * 2) The timer routine, scctim, is not installed directly in the
  18.  *    timer interrupt chain, but is called through the systick routine
  19.  *    in pc.c.
  20.  *
  21.  * 3) Facilities of nos are used whenever possible in place of direct
  22.  *    structure or variable manipulation. Mbuf management is handled
  23.  *    this way, along with interface initialization.
  24.  *
  25.  * 4) Nrs mode support is added in a manner similar to that of the
  26.  *    Slip support. I have not had an opportunity to test this, but
  27.  *    it is essentially identical to the way the 8250 version works.
  28.  *
  29.  * 5) Callsign specification on radio modes (kiss,nrs,ax25) is an
  30.  *    option. If not supplied, the value of Mycall will be used.
  31.  *
  32.  * 6) Bufsize specification is now a parameter on setup of each channel.
  33.  *    This is the size of the fifo on asynch input, and the size of
  34.  *    mbuf buffers for sdlc mode. Since the fifo buffer can fill up,
  35.  *    this value should be reasonably large for asynch mode. Mbufs
  36.  *    are chained when they fill up, so having a small bufsize with
  37.  *    sdlc modes (ax25) does not result in loss of characters.
  38.  *
  39.  * 7) Because slip and nrs decoding is handled outside the driver,
  40.  *    sccstat cannot be used to report sent and receive packet counts
  41.  *    in asynch mode, and these fields are blanked on display in asynch
  42.  *    modes.
  43.  *
  44.  *
  45.  * I am interested in setting up some default initializations for
  46.  * the popular Z8530 boards, to minimize user problems in constructing
  47.  * the proper attach init entries. These would allow for shortened
  48.  * entries to use the defaults, such as "attach scc 1 init drsi" to
  49.  * attach a DRSI board in standard configuration at its default address.
  50.  * Since I do not have complete technical information on all such boards,
  51.  * I would very much appreciate any information that users can provide
  52.  * me regarding particular boards.
  53.  *
  54.  * 1/25/90
  55.  *
  56.  * Modifications:
  57.  *
  58.  * 2/17/90:
  59.  *
  60.  * 1) Added mods from PE1CHL which reflect relevent changes to the
  61.  *    scc driver in his version of net between 10/89 and 1/90. Changes
  62.  *    incorporated include additional delays in sccvec.asm, addition
  63.  *    of external clock mode, and initialization for the 8536 as a
  64.  *    clock divider on the DRSI board. "INLINE" is a slight delay
  65.  *    for register access incorporated for use with the inline i/o
  66.  *    code in MSC. This may not be useful or necessary with TURBO.
  67.  *    Changes making "TPS" a variable were not added, since the
  68.  *    scc timer does not install itself on the hardware interrupt
  69.  *    in this version.
  70.  * 
  71.  *
  72.  * Ken Mitchum, KY3B       km@cs.pitt.edu  km@dsl.pitt.edu
  73.  *                             or mail to the tcpip group
  74.  *
  75.  */
  76.  
  77. /* Added ANSI-style prototypes, reformatted source, minor delinting.
  78.  * Integrated into standard 900201 NOS by KA9Q.
  79.  */
  80.  
  81. /*
  82.  * Generic driver for Z8530 SCC chip in SLIP, KISS or AX.25 mode.
  83.  *
  84.  * Written by R.E. Janssen (PE1CHL) using material from earlier
  85.  * EAGLE and PC100 drivers in this package.
  86.  *
  87.  * The driver has initially been written for my own Atari SCC interface
  88.  * board, but it could eventually replace the other SCC drivers.
  89.  *
  90.  * Unfortunately, there is little consistency between the different interface
  91.  * boards, as to the use of a clock source, the solution for the fullduplex
  92.  * clocking problem, and most important of all: the generation of the INTACK
  93.  * signal.    Most designs do not even support the generation of an INTACK and
  94.  * the read of the interrupt vector provided by the chip.
  95.  * This results in lots of configuration parameters, and a fuzzy
  96.  * polltable to be able to support multiple chips connected at one interrupt
  97.  * line...
  98.  *
  99.  */
  100.  
  101.  
  102. #include <stdlib.h>
  103. #include <stdio.h>
  104. #include <ctype.h>
  105. #include <time.h>
  106. #include <dos.h>
  107. #include "global.h"
  108. #include "mbuf.h"
  109. #include "config.h"
  110. #include "netuser.h"
  111. #include "proc.h"
  112. #include "iface.h"
  113. #include "pktdrvr.h"
  114. #include "slip.h"
  115. #include "nrs.h"
  116. #include "n8250.h"
  117. #include "scc.h"
  118. #include "z8530.h"
  119. #include "z8536.h"
  120. #include "ax25.h"
  121. #include "trace.h"
  122. #include "pc.h"
  123. #include "kiss.h"
  124. #include "devparam.h"
  125.  
  126. /* interrupt handlers */
  127. extern INTERRUPT sccvec();
  128. extern INTERRUPT sccnovec();
  129.  
  130. /* variables used by the SCC interrupt handler in sccvec.asm */
  131. static INTERRUPT (*Orgivec)();    /* original interrupt vector */
  132.  
  133. struct sccinfo Sccinfo = {0};        /* global info about SCCs */
  134. struct sccchan *Sccchan[2 * MAXSCC] = {0}; /* information per channel */
  135. ioaddr Sccvecloc = {0};            /* location to access for SCC vector */
  136. unsigned char Sccmaxvec = {0};        /* maximum legal vector from SCC */
  137. ioaddr Sccpolltab[MAXSCC+1][2] = {0};    /* polling table when no vectoring */
  138.  
  139. #if defined(INLINE)
  140. static unsigned scc_delay __ARGS((unsigned v));
  141.  
  142. static unsigned scc_delay (v)        /* delay for about 5 PCLK cycles */
  143.    unsigned v;                /* pass-through used for input */
  144.  
  145. {
  146.    register int i,j;            /* it takes time to save them */
  147.  
  148.    return v;                /* return the passed parameter */
  149. }
  150. #endif
  151.  
  152. unsigned char Random = 0;        /* random number for p-persist */
  153.  
  154. static int scc_call __ARGS((struct iface *ifp,char *call));
  155. static int scc_init __ARGS((int nchips,ioaddr iobase,int space,int aoff,
  156.  int boff,int doff,ioaddr intack,int ivec,long clk,int pclk,int hwtype,
  157.  int hwparam));
  158. static int scc_raw __ARGS((struct iface *ifp,struct mbuf *bp));
  159. static int scc_stop __ARGS((struct iface *ifp));
  160. static int get_scc __ARGS((int dev));
  161. static int scc_send __ARGS((int dev,struct mbuf *bp));
  162. static int scc_async __ARGS((struct sccchan *scc));
  163.  
  164. static void scc_sdlc __ARGS((struct sccchan *scc));
  165. static void scc_tossb __ARGS((struct sccchan *scc));
  166. static void scc_txon __ARGS((struct sccchan *scc));
  167. static void scc_txoff __ARGS((struct sccchan *scc));
  168. static int32 scc_aioctl __ARGS((struct iface *ifp,int cmd,int set,int32 val));
  169. static int32 scc_sioctl __ARGS((struct iface *ifp,int cmd,int set,int32 val));
  170. static void scc_sstart __ARGS((struct sccchan *scc));
  171. static unsigned int scc_speed __ARGS((struct sccchan *scc,
  172.   unsigned int clkmode,long speed));
  173. static void scc_asytx __ARGS((struct sccchan *scc));
  174. static void scc_asyex __ARGS((struct sccchan *scc));
  175. static void scc_asyrx __ARGS((struct sccchan *scc));
  176. static void scc_asysp __ARGS((struct sccchan *scc));
  177. static void scc_sdlctx __ARGS((struct sccchan *scc));
  178. static void scc_sdlcex __ARGS((struct sccchan *scc));
  179. static void scc_sdlcrx __ARGS((struct sccchan *scc));
  180. static void scc_sdlcsp __ARGS((struct sccchan *scc));
  181.  
  182. /* Attach an SCC channel to the system, or initialize SCC driver.
  183.  * operation depends on argv[2]:
  184.  * when "init", the SCC driver is initialized, and global information about
  185.  * the hardware is set up.
  186.  * argv[0]: hardware type, must be "scc"
  187.  * argv[1]: number of SCC chips we will support
  188.  * argv[2]: mode, must be: "init" in this case
  189.  * argv[3]: base address of SCC chip #0 (hex)
  190.  * argv[4]: spacing between SCC chip base addresses
  191.  * argv[5]: offset from chip base address to channel A control register
  192.  * argv[6]: offset from chip base address to channel B control register
  193.  * argv[7]: offset from each channel's control register to data register
  194.  * argv[8]: address of INTACK/Read Vector port. 0 to read from RR3A/RR2B
  195.  * argv[9]: CPU interrupt vector number for all connected SCCs
  196.  * argv[10]: clock frequency (PCLK/RTxC) of all SCCs in cycles per second
  197.  *         prefix with "p" for PCLK, "r" for RTxC clock (for baudrate gen)
  198.  * argv[11]: optional hardware type (for special features)
  199.  * argv[12]: optional extra parameter for special hardware
  200.  *
  201.  * otherwise, a single channel is attached using the specified parameters:
  202.  * argv[0]: hardware type, must be "scc"
  203.  * argv[1]: SCC channel number to attach, 0/1 for first chip A/B, 2/3 for 2nd...
  204.  * argv[2]: mode, can be:
  205.  *        "slip", "kiss", "ax25"
  206.  * argv[3]: interface label, e.g., "sl0"
  207.  * argv[4]: maximum transmission unit, bytes
  208.  * argv[5]: interface speed, e.g, "1200". prefix with "d" when an external
  209.  *        divider is available to generate the TX clock.    When the clock
  210.  *        source is PCLK, this can be a /32 divider between TRxC and RTxC.
  211.  *        When the clock is at RTxC, the TX rate must be supplied at TRxC.
  212.  *        This is needed only for AX.25 fullduplex.
  213.  *        When this arg is given as "ext", the transmit and receive clock
  214.  *        are external, and the BRG and DPLL are not used.
  215.  * argv[6]: buffer size
  216.  * argv[7]: callsign used on the radio channels (optional)
  217.  */
  218.  
  219. int
  220. scc_attach(argc,argv)
  221. int argc;
  222. char *argv[];
  223. {
  224.     register struct iface *ifp;
  225.     struct sccchan *scc;
  226.     unsigned int chan,brgrate;
  227.     int pclk = 0,hwtype = 0,hwparam = 0;
  228.     int xdev;
  229.     char *cp;
  230.  
  231.     /* first handle the special "init" mode, to initialize global stuff */
  232.  
  233.     if(!strcmp(argv[2],"init")){
  234.     if(argc < 11)            /* need at least argv[1]..argv[10] */
  235.         return -1;
  236.  
  237.     if(isupper(argv[10][0]))
  238.         argv[10][0] = tolower(argv[10][0]);
  239.  
  240.     if(argv[10][0] == 'p'){    /* wants to use PCLK as clock? */
  241.         pclk = 1;
  242.         argv[10]++;
  243.     } else {
  244.         if(argv[10][0] == 'r')    /* wants to use RTxC? */
  245.         argv[10]++;        /* that's the default */
  246.     }
  247.     if(argc > 11)            /* optional hardware type */
  248.         hwtype = htoi(argv[11]);    /* it is given in hex */
  249.  
  250.     if(argc > 12)            /* optional hardware param */
  251.         hwparam = htoi(argv[12]);    /* also in hex */
  252.  
  253.     return scc_init(atoi(argv[1]),(ioaddr) htol(argv[3]),atoi(argv[4]),
  254.         atoi(argv[5]),atoi(argv[6]),atoi(argv[7]),
  255.         (ioaddr) htol(argv[8]),atoi(argv[9]),
  256.         atol(argv[10]),pclk,hwtype,hwparam);
  257.     }
  258.     /* not "init", so it must be a valid mode to attach a channel */
  259.     if(strcmp(argv[2],"ax25") && strcmp(argv[2],"kiss") &&
  260.      strcmp(argv[2],"slip")){
  261.         printf("Mode %s unknown for SCC\n",argv[2]);
  262.         return -1;
  263.     }
  264.     if(strcmp(argv[2],"slip") == 0 || strcmp(argv[2],"kiss") == 0){
  265.         for(xdev = 0;xdev < SLIP_MAX;xdev++)
  266.             if(Slip[xdev].iface == NULLIF)
  267.                 break;
  268.         if(xdev >= SLIP_MAX){
  269.             printf("Too many slip devices\n");
  270.             return -1;
  271.         }
  272.     }
  273.     if(strcmp(argv[2],"nrs") == 0){
  274.         for(xdev = 0;xdev < NRS_MAX;xdev++)
  275.             if(Nrs[xdev].iface == NULLIF)
  276.                 break;
  277.  
  278.         if(xdev >= NRS_MAX){
  279.             printf("Too many nrs devices\n");
  280.             return -1;
  281.         }
  282.     }
  283.     if(!Sccinfo.init){
  284.         printf("First init SCC driver\n");
  285.         return -1;
  286.     }
  287.     if((chan = atoi(argv[1])) > Sccinfo.maxchan){
  288.         printf("SCC channel %d out of range\n",chan);
  289.         return -1;
  290.     }
  291.     if(Sccchan[chan] != NULLCHAN){
  292.         printf("SCC channel %d already attached\n",chan);
  293.         return -1;
  294.     }
  295.     /* create interface structure and fill in details */
  296.     ifp = (struct iface *) callocw(1,sizeof(struct iface));
  297.     ifp->name = mallocw(strlen(argv[3]) + 1);
  298.     strcpy(ifp->name,argv[3]);
  299.  
  300.     ifp->mtu = atoi(argv[4]);
  301.     ifp->dev = chan;
  302.     ifp->stop = scc_stop;
  303.  
  304.     scc = (struct sccchan *) callocw(1,sizeof(struct sccchan));
  305.     scc->ctrl = Sccinfo.iobase + (chan / 2) * Sccinfo.space + Sccinfo.off[chan % 2];
  306.     scc->data = scc->ctrl + Sccinfo.doff;
  307.     scc->iface = ifp;
  308.  
  309.     if(isupper(argv[5][0]))
  310.         argv[5][0] = tolower(argv[5][0]);
  311.  
  312.     switch (argv[5][0]) {
  313.     case 'd':                /* fulldup divider installed? */
  314.         scc->fulldup = 1;        /* set appropriate flag */
  315.         argv[5]++;            /* skip the 'd' */
  316.         break;
  317.  
  318.     case 'e':                /* external clocking? */
  319.         scc->extclock = 1;        /* set the flag */
  320.         break;
  321.     }
  322.  
  323.     scc->bufsiz = atoi(argv[6]);
  324.     ifp->addr = Ip_addr; 
  325.     Sccchan[chan] = scc;        /* put addr in table for interrupts */
  326.  
  327.     switch(argv[2][0]){             /* mode already checked above */
  328. #ifdef AX25
  329.     case 'a':    /* AX.25 */
  330.         scc_sdlc(scc);                /* init SCC in SDLC mode */
  331.  
  332.         if (!scc->extclock) {
  333.             brgrate = scc_speed(scc,32,atol(argv[5]));/* init SCC speed */
  334.             scc->speed = Sccinfo.clk / (64L * (brgrate + 2));/* calc real speed */
  335.         }
  336.  
  337.         brgrate = scc_speed(scc,32,atol(argv[5]));/* init SCC speed */
  338.         scc->speed = Sccinfo.clk / (64L * (brgrate + 2));/* calc real speed */
  339.         setencap(ifp,"AX25");
  340.         scc_call(ifp,argc > 7 ? argv[7] : (char *) 0);    /* set the callsign */
  341.             ifp->ioctl = scc_sioctl;
  342.         ifp->raw = scc_raw;
  343.  
  344.         /* default KISS Params */
  345.         scc->a.txdelay = 36*TPS/100;    /* 360 ms */
  346.         scc->a.persist = 25;        /* 10% persistence */
  347.         scc->a.slottime = 16*TPS/100; /* 160 ms */
  348. #if TPS > 67
  349.         scc->a.tailtime = 3*TPS/100;    /* 30 ms */
  350. #else
  351.         scc->a.tailtime = 2;        /* minimal reasonable value */
  352. #endif
  353.         scc->a.fulldup = 0;        /* CSMA */
  354.         scc->a.waittime = 50*TPS/100; /* 500 ms */
  355.         scc->a.maxkeyup = 7;        /* 7 s */
  356.         scc->a.mintime = 3;        /* 3 s */
  357.         scc->a.idletime = 120;    /* 120 s */
  358.         break;
  359.     case 'k':    /* kiss */
  360.         scc_async(scc);                /* init SCC in async mode */
  361.         brgrate = scc_speed(scc,16,atol(argv[5]));
  362.         scc->speed = Sccinfo.clk / (32L * (brgrate + 2));
  363.  
  364.         setencap(ifp,"AX25");
  365.         scc_call(ifp,argc > 7 ? argv[7] : (char *) 0);    /* set the callsign */
  366.  
  367.         ifp->ioctl = kiss_ioctl;
  368.         ifp->raw = kiss_raw;
  369.  
  370.         for(xdev = 0;xdev < SLIP_MAX;xdev++){
  371.             if(Slip[xdev].iface == NULLIF)
  372.                 break;
  373.         }
  374.         ifp->xdev = xdev;
  375.         Slip[xdev].iface = ifp;
  376.         Slip[xdev].type = CL_KISS;
  377.         Slip[xdev].send = scc_send;
  378.         Slip[xdev].get = get_scc;
  379.         cp = if_name(ifp," rx");
  380.         ifp->rxproc = newproc(cp,256,slip_rx,xdev,NULL,NULL,0);
  381.         free(cp);
  382.         break;
  383. #endif
  384. #ifdef SLIP
  385.     case 's':    /* slip */
  386.         scc_async(scc);                /* init SCC in async mode */
  387.         brgrate = scc_speed(scc,16,atol(argv[5]));
  388.         scc->speed = Sccinfo.clk / (32L * (brgrate + 2));
  389.         setencap(ifp,"SLIP");
  390.         ifp->ioctl = scc_aioctl;
  391.         ifp->raw = slip_raw;
  392.         for(xdev = 0;xdev < SLIP_MAX;xdev++){
  393.             if(Slip[xdev].iface == NULLIF)
  394.                 break;
  395.         }
  396.         ifp->xdev = xdev;
  397.         Slip[xdev].iface = ifp;
  398.         Slip[xdev].type = CL_SERIAL_LINE;
  399.         Slip[xdev].send = scc_send;
  400.         Slip[xdev].get = get_scc;
  401.         cp = if_name(ifp," rx");
  402.         ifp->rxproc = newproc(cp,256,slip_rx,xdev,NULL,NULL,0);
  403.         free(cp);
  404.         break;
  405. #endif
  406. #ifdef NRS
  407.     case 'n':    /* nrs */
  408.         scc_async(scc);                /* init SCC in async mode */
  409.         brgrate = scc_speed(scc,16,atol(argv[5]));
  410.         scc->speed = Sccinfo.clk / (32L * (brgrate + 2));
  411.         setencap(ifp,"AX25");
  412.         scc_call(ifp,argc > 7 ? argv[7] : (char *) 0);    /* set the callsign */
  413.         ifp->ioctl = scc_aioctl;
  414.         ifp->raw = nrs_raw;
  415.     
  416.         for(xdev = 0;xdev < NRS_MAX;xdev++)
  417.             if(Nrs[xdev].iface == NULLIF)
  418.                 break;
  419.  
  420.         ifp->xdev = xdev;
  421.         Nrs[xdev].iface = ifp;
  422.         Nrs[xdev].send = scc_send;
  423.         Nrs[xdev].get = get_scc;
  424.         cp = if_name(ifp," rx");
  425.         ifp->rxproc = newproc(cp,256,nrs_recv,xdev,NULL,NULL,0);
  426.         free(cp);
  427.         break;
  428. #endif
  429.     }
  430.     ifp->next = Ifaces;            /* link interface in list */
  431.     Ifaces = ifp;
  432.     cp = if_name(ifp," tx");
  433.     ifp->txproc = newproc(cp,512,if_tx,0,ifp,NULL,0);
  434.     free(cp);
  435.     return 0;
  436. }
  437.  
  438. /* SCC driver initialisation. called on "attach scc <num> init ..." */
  439. static int
  440. scc_init(nchips,iobase,space,aoff,boff,doff,intack,ivec,clk,pclk,hwtype,hwparam)
  441. int nchips;            /* number of chips */
  442. ioaddr iobase;            /* base of first chip */
  443. int space,aoff,boff,doff;
  444. ioaddr intack;            /* INTACK ioport or 0 for no INTACK */
  445. int ivec;            /* interrupt vector number */
  446. long clk;            /* clock frequency */
  447. int pclk;            /* PCLK or RTxC for clock */
  448. int hwtype;            /* selection of special hardware types */
  449. int hwparam;            /* extra parameter for special hardware */
  450. {
  451.     int chip,chan;
  452.     ioaddr chipbase;
  453.     register ioaddr ctrl;
  454.     int d;
  455.     int dum = 1;
  456.  
  457. #define z 0
  458.  
  459.     if(Sccinfo.init){
  460.         printf("SCC driver already initialized - nothing done\n");
  461.         return 1;
  462.     }
  463.     Sccinfo.init = 1;
  464.     Sccinfo.nchips = nchips;
  465.     Sccinfo.maxchan = (2 * nchips) - 1;
  466.     Sccinfo.iobase = iobase;
  467.     Sccinfo.space = space;
  468.     Sccinfo.off[0] = aoff;
  469.     Sccinfo.off[1] = boff;
  470.     Sccinfo.doff = doff;
  471.     Sccinfo.ivec = ivec;
  472.     Sccinfo.clk = clk;
  473.     Sccinfo.pclk = pclk;
  474.     Sccinfo.hwtype = hwtype;
  475.     Sccinfo.hwparam = hwparam;
  476.  
  477.     /* reset and pre-init all chips in the system */
  478.     for(chip = 0; chip < nchips; chip++){
  479.         chipbase = iobase + chip * space;
  480.         ctrl = chipbase + Sccinfo.off[0];
  481.         DISABLE();        /* because of 2-step accesses */
  482.         VOID(RDREG(ctrl));        /* make sure pointer is written */
  483.         WRSCC(ctrl,R9,FHWRES);        /* force hardware reset */
  484.         for (d = 0; d < 1000; d++)    /* wait a while to be sure */
  485.             dum *= 10;
  486.         for(chan = 0; chan < 2; chan++){
  487.             ctrl = chipbase + Sccinfo.off[chan];
  488.  
  489.             /* initialize a single channel to no-op */
  490.             VOID(RDREG(ctrl));        /* make sure pointer is written */
  491.             WRSCC(ctrl,R4,z);        /* no mode selected yet */
  492.             WRSCC(ctrl,R1,z);        /* no W/REQ operation */
  493.             WRSCC(ctrl,R2,16 * chip);    /* chip# in upper 4 bits of vector */
  494.             WRSCC(ctrl,R3,z);        /* disable rx */
  495.             WRSCC(ctrl,R5,z);        /* disable tx */
  496.             WRSCC(ctrl,R9,VIS);        /* vector includes status, MIE off */
  497.             Sccpolltab[chip][chan] = ctrl; /* store ctrl addr for polling */
  498.         }
  499.         if(hwtype & HWEAGLE)        /* this is an EAGLE card */
  500.             WRREG(chipbase + 4,0x08);    /* enable interrupt on the board */
  501.  
  502.         if(hwtype & HWPC100)        /* this is a PC100 card */
  503.             WRREG(chipbase,hwparam);    /* set the MODEM mode (22H normally) */
  504.  
  505.         if(hwtype & HWPRIMUS)        /* this is a PRIMUS-PC */
  506.             WRREG(chipbase + 4,hwparam); /* set the MODEM mode (02H normally) */
  507.  
  508.         if (hwtype & HWDRSI) {        /* this is a DRSI PC*Packet card */
  509.             ioaddr z8536 = chipbase + 7; /* point to 8536 master ctrl reg */
  510.  
  511.             /* Initialize 8536 to perform its divide-by-32 function */
  512.             /* This part copied from N6TTO DRSI-driver */
  513.  
  514.             /* Start by forcing chip into known state */
  515.  
  516.             VOID(RDREG(z8536));        /* make sure pointer is written */
  517.             WRSCC(z8536,CIO_MICR,0x01); /* force hardware reset */
  518.  
  519.             for (d = 0; d < 1000; d++)    /* wait a while to be sure */
  520.                 dum *= 10;
  521.  
  522.             WRSCC(z8536,CIO_MICR,0x00); /* Clear reset and start */
  523.  
  524.             /* Wait for chip to come ready */
  525.  
  526.             while (RDSCC(z8536,CIO_MICR) != 0x02)
  527.                 dum *= 10;
  528.  
  529.             WRSCC(z8536,CIO_MICR,0x26); /* NV|CT_VIS|RJA */
  530.             WRSCC(z8536,CIO_MCCR,0xf4); /* PBE|CT1E|CT2E|CT3E|PAE */
  531.  
  532.             WRSCC(z8536,CIO_CTMS1,0xe2);/* Continuous, EOE, ECE, Pulse output */
  533.             WRSCC(z8536,CIO_CTMS2,0xe2);/* Continuous, EOE, ECE, Pulse output */
  534.             
  535.         WRSCC(z8536,CIO_CT1MSB,0x00); /* Load time constant CTC #1 */
  536.             WRSCC(z8536,CIO_CT1LSB,0x10);
  537.             WRSCC(z8536,CIO_CT2MSB,0x00); /* Load time constant CTC #2 */
  538.             WRSCC(z8536,CIO_CT2LSB,0x10);
  539.  
  540.             WRSCC(z8536,CIO_IVR,0x06);
  541.  
  542.             /* Set port direction bits in port A and B             */
  543.             /* Data is input on bits d1 and d5, output on d0 and d4. */
  544.             /* The direction is set by 1 for input and 0 for output  */
  545.  
  546.             WRSCC(z8536,CIO_PDCA,0x22);
  547.             WRSCC(z8536,CIO_PDCB,0x22);
  548.  
  549.             WRSCC(z8536,CIO_CSR1,CIO_GCB|CIO_TCB); /* Start CTC #1 running */
  550.             WRSCC(z8536,CIO_CSR2,CIO_GCB|CIO_TCB); /* Start CTC #2 running */
  551.         }
  552.  
  553.         RESTORE();
  554.     }
  555.     Sccpolltab[chip][0] = 0;    /* terminate the polling table */
  556.     Sccvecloc = intack;        /* location of INTACK/vector read */
  557.     Sccmaxvec = 16 * nchips;    /* upper limit on valid vector */
  558.     /* save original interrupt vector */
  559.     Orgivec = getirq(ivec);
  560.  
  561.     if(intack){    /* INTACK method selected? */
  562.         /* set interrupt vector to INTACK-generating routine  */
  563.         setirq(ivec,sccvec);
  564.     } else {
  565.         /* set interrupt vector to polling routine */
  566.         setirq(ivec,sccnovec);
  567.     }
  568.     /* enable the interrupt  */
  569.     maskon(ivec);
  570.     return 0;
  571. }
  572.  
  573. /* initialize an SCC channel in asynchronous mode */
  574. static int
  575. scc_async(scc)
  576. register struct sccchan *scc;
  577. {
  578.     register struct fifo *fp = &(scc->fifo);
  579.     
  580.     if((fp->buf = malloc(scc->bufsiz)) == NULLCHAR){
  581.         printf("scc%d: No space for rx buffer\n",scc->iface->dev);
  582.         return -1;
  583.     }
  584.     fp->bufsize = scc->bufsiz;
  585.     fp->wp = fp->rp = fp->buf;
  586.     fp->cnt = 0;
  587.  
  588.     scc->int_transmit = scc_asytx;    /* set interrupt handlers */
  589.     scc->int_extstat = scc_asyex;
  590.     scc->int_receive = scc_asyrx;
  591.     scc->int_special = scc_asysp;
  592.  
  593.     DISABLE();
  594.  
  595.     wr(scc,R4,X16CLK|SB1);        /* *16 clock, 1 stopbit, no parity */
  596.     wr(scc,R1,z);            /* no W/REQ operation */
  597.     wr(scc,R3,Rx8);            /* RX 8 bits/char, disabled */
  598.     wr(scc,R5,Tx8|DTR|RTS);        /* TX 8 bits/char, disabled, DTR RTS */
  599.     wr(scc,R9,VIS);            /* vector includes status */
  600.     wr(scc,R10,NRZ|z);            /* select NRZ */
  601.     wr(scc,R11,RCBR|TCBR);        /* clocks are BR generator */
  602.     wr(scc,R14,Sccinfo.pclk? BRSRC:z);    /* brg source = PCLK/RTxC */
  603.     wr(scc,R15,BRKIE);            /* enable BREAK ext/status int */
  604.  
  605.     or(scc,R3,RxENABLE);        /* enable receiver */
  606.     or(scc,R5,TxENAB);            /* enable transmitter */
  607.  
  608.     WRREG(scc->ctrl,RES_EXT_INT);    /* reset ext/status interrupts */
  609.     WRREG(scc->ctrl,RES_EXT_INT);    /* must be done twice */
  610.     scc->status = RDREG(scc->ctrl);    /* read initial status */
  611.  
  612.     or(scc,R1,INT_ALL_Rx|TxINT_ENAB|EXT_INT_ENAB); /* enable interrupts */
  613.     or(scc,R9,MIE);            /* master interrupt enable */
  614.  
  615.     RESTORE();
  616.     return 0;
  617. }
  618.  
  619. /* initialize an SCC channel in SDLC mode */
  620. static void
  621. scc_sdlc(scc)
  622. register struct sccchan *scc;
  623. {
  624.     scc->int_transmit = scc_sdlctx;    /* set interrupt handlers */
  625.     scc->int_extstat = scc_sdlcex;
  626.     scc->int_receive = scc_sdlcrx;
  627.     scc->int_special = scc_sdlcsp;
  628.  
  629.     DISABLE();
  630.  
  631.     wr(scc,R4,X1CLK|SDLC);        /* *1 clock, SDLC mode */
  632.     wr(scc,R1,z);            /* no W/REQ operation */
  633.     wr(scc,R3,Rx8|RxCRC_ENAB);    /* RX 8 bits/char, CRC, disabled */
  634.     wr(scc,R5,Tx8|DTR|TxCRC_ENAB);    /* TX 8 bits/char, disabled, DTR */
  635.     wr(scc,R6,z);            /* SDLC address zero (not used) */
  636.     wr(scc,R7,FLAG);        /* SDLC flag value */
  637.     wr(scc,R9,VIS);            /* vector includes status */
  638.     wr(scc,R10,CRCPS|NRZI|ABUNDER);    /* CRC preset 1, select NRZI, ABORT on underrun */
  639.  
  640.     if (scc->extclock){            /* when using external clocks */
  641.         /* RXclk RTxC, TXclk TRxC. */
  642.         wr(scc,R11,RCRTxCP|TCTRxCP);
  643.         wr(scc,R14,z);            /* No BRG options */
  644.         WRSCC(scc->ctrl,R14,DISDPLL|scc->wreg[R14]); /* No DPLL operation */
  645.     } else {
  646.         if(scc->fulldup){        /* when external clock divider */
  647.             if(Sccinfo.pclk){    /* when using PCLK as clock source */
  648.                 /* RXclk DPLL, TXclk RTxC, out=BRG.     external /32 TRxC->RTxC */
  649.                 wr(scc,R11,RCDPLL|TCRTxCP|TRxCOI|TRxCBR);
  650.                 } else {
  651.                     /* RXclk DPLL, TXclk TRxC.    external TX clock to TRxC */
  652.                     wr(scc,R11,RCDPLL|TCTRxCP);
  653.                     }
  654.             } else {            /* only half-duplex operation */
  655.                 /* RXclk DPLL, TXclk BRG. BRG reprogrammed at every TX/RX switch */
  656. #ifdef    notdef    /* KA9Q - for PSK modem */
  657.                 wr(scc,R11,RCDPLL|TCBR);
  658. #else
  659.                 /* DPLL -> Rx clk, DPLL -> Tx CLK, TxCLK -> TRxC pin */
  660.                 wr(scc,R11,RCDPLL|TCDPLL|TRxCOI|TRxCDP);
  661. #endif
  662.             }
  663.         wr(scc,R14,Sccinfo.pclk? BRSRC:z);    /* BRG source = PCLK/RTxC */
  664.         WRSCC(scc->ctrl,R14,SSBR|scc->wreg[R14]); /* DPLL source = BRG */
  665.         WRSCC(scc->ctrl,R14,SNRZI|scc->wreg[R14]); /* DPLL NRZI mode */
  666.     }
  667.     wr(scc,R15,BRKIE|CTSIE|DCDIE);    /* enable ABORT, CTS & DCD interrupts */
  668.  
  669.     if(RDREG(scc->ctrl) & DCD){    /* DCD is now ON */
  670.         if (!scc->extclock)
  671.             WRSCC(scc->ctrl,R14,SEARCH|scc->wreg[R14]); /* DPLL: enter search mode */
  672.         or(scc,R3,ENT_HM|RxENABLE);    /* enable the receiver, hunt mode */
  673.     }
  674.     WRREG(scc->ctrl,RES_EXT_INT);    /* reset ext/status interrupts */
  675.     WRREG(scc->ctrl,RES_EXT_INT);    /* must be done twice */
  676.     scc->status = RDREG(scc->ctrl);    /* read initial status */
  677.  
  678.     or(scc,R1,INT_ALL_Rx|TxINT_ENAB|EXT_INT_ENAB); /* enable interrupts */
  679.     or(scc,R9,MIE);            /* master interrupt enable */
  680.  
  681.     RESTORE();
  682. }
  683.  
  684. /* set SCC channel speed
  685.  * clkmode specifies the division rate (1,16,32) inside the SCC
  686.  * returns the selected brgrate for "real speed" calculation
  687.  */
  688. static unsigned int
  689. scc_speed(scc,clkmode,speed)
  690. register struct sccchan *scc;
  691. unsigned int clkmode;
  692. long speed;                /* the desired baudrate */
  693. {
  694.     unsigned int brgrate;
  695.     long spdclkm;
  696.  
  697.     /* calculate baudrate generator value */
  698.  
  699.     if ((spdclkm = speed * clkmode) == 0)
  700.     return 65000U;            /* avoid divide-by-zero */
  701.  
  702.     brgrate = (unsigned) ((Sccinfo.clk + spdclkm) / (spdclkm * 2)) - 2;
  703.  
  704.     DISABLE();        /* 2-step register accesses... */
  705.  
  706.     cl(scc,R14,BRENABL);        /* disable baudrate generator */
  707.     wr(scc,R12,brgrate);        /* brg rate LOW */
  708.     wr(scc,R13,brgrate >> 8);        /* brg rate HIGH */
  709.     or(scc,R14,BRENABL);        /* enable baudrate generator */
  710.  
  711.     RESTORE();
  712.     return brgrate;
  713. }
  714.  
  715. /* de-activate SCC channel */
  716. static int
  717. scc_stop(ifp)
  718. struct iface *ifp;
  719. {
  720.     struct sccchan *scc = Sccchan[ifp->dev];
  721.  
  722.     DISABLE();
  723.  
  724.     VOID(RDREG(scc->ctrl));        /* make sure pointer is written */
  725.     wr(scc,R9,(ifp->dev % 2)? CHRB : CHRA); /* reset the channel */
  726.  
  727.     switch(ifp->iftype->type){
  728.     case CL_SERIAL_LINE:
  729.     case CL_KISS:
  730.         free(scc->fifo.buf);
  731.     default:
  732.         break;
  733.     }
  734.     free(scc);
  735.     Sccchan[ifp->dev] = NULLCHAN;
  736.     RESTORE();
  737.     return 0;
  738. }
  739.  
  740. /* de-activate SCC driver on program exit */
  741. void
  742. sccstop()
  743. {
  744.     if(Sccinfo.init){            /* was it initialized? */
  745.         maskoff(Sccinfo.ivec);        /* disable the interrupt */
  746.         setirq(Sccinfo.ivec,Orgivec);    /* restore original interrupt vector */
  747.     }
  748. }
  749.  
  750. /* perform ioctl on SCC (async) channel
  751.  * this is used for SLIP mode only, and will read/set the line speed
  752.  */
  753. static int32
  754. scc_aioctl(ifp,cmd,set,val)
  755. struct iface *ifp;
  756. int cmd;
  757. int set;
  758. int32 val;
  759. {
  760.     struct sccchan *scc;
  761.     unsigned int brgrate;
  762.  
  763.     scc = Sccchan[ifp->dev];
  764.  
  765.     switch(cmd){
  766.     case PARAM_SPEED:
  767.         if(set){
  768.             brgrate = scc_speed(scc,16,val);
  769.             scc->speed = Sccinfo.clk / (32L * (brgrate + 2));
  770.         }
  771.         return scc->speed;
  772.     }
  773.     return 0;
  774. }
  775.  
  776. /* perform ioctl on SCC (sdlc) channel
  777.  * this is used for AX.25 mode only, and will set the "kiss" parameters
  778.  */
  779. static int32
  780. scc_sioctl(ifp,cmd,set,val)
  781. struct iface *ifp;
  782. int cmd;
  783. int set;
  784. int32 val;
  785. {
  786.     struct sccchan *scc;
  787.     unsigned int brgrate;
  788.     
  789.     scc = Sccchan[ifp->dev];
  790.  
  791.     switch(cmd){
  792.     case PARAM_SPEED:
  793.         if(set){
  794.             if(val == 0)
  795.                 scc->extclock = 1;
  796.             else {
  797.                 brgrate = scc_speed(scc,32,val);/* init SCC speed */
  798.                 scc->speed = Sccinfo.clk / (64L * (brgrate + 2));/* calc real speed */
  799.             }
  800.         }
  801.         return scc->speed;
  802.     case PARAM_TXDELAY:
  803.         if(set)
  804.             scc->a.txdelay = val;
  805.         return scc->a.txdelay;
  806.     case PARAM_PERSIST:
  807.         if(set)
  808.             scc->a.persist = val;
  809.         return scc->a.persist;
  810.     case PARAM_SLOTTIME:
  811.         if(set)
  812.             scc->a.slottime = val;
  813.         return scc->a.slottime;
  814.     case PARAM_TXTAIL:
  815.         if(set)
  816.             scc->a.tailtime = val;
  817.         return scc->a.tailtime;
  818.     case PARAM_FULLDUP:
  819.         if(set)
  820.             scc->a.fulldup = val;
  821.         return scc->a.fulldup;
  822.     case PARAM_WAIT:
  823.         if(set)
  824.             scc->a.waittime = val;
  825.         return scc->a.waittime;
  826.     case PARAM_MAXKEY:
  827.         if(set)
  828.             scc->a.maxkeyup = val;
  829.         return scc->a.maxkeyup;
  830.     case PARAM_MIN:
  831.         if(set)
  832.             scc->a.mintime = val;
  833.         return scc->a.mintime;
  834.     case PARAM_IDLE:
  835.         if(set)
  836.             scc->a.idletime = val;
  837.         return scc->a.idletime;
  838.     case PARAM_DTR:
  839.         if(set){
  840.             if(val)
  841.                 scc->wreg[R5] |= DTR;
  842.             else
  843.                 scc->wreg[R5] &= ~DTR;
  844.  
  845.             DISABLE();
  846.             if(scc->a.tstate == IDLE && scc->timercount == 0)
  847.                 scc->timercount = 1;    /* force an update */
  848.             RESTORE();
  849.         }
  850.         return (scc->wreg[R5] & DTR) ? 1 : 0;
  851.     case PARAM_GROUP:
  852.         if(set)
  853.             scc->group = val;
  854.         return scc->group;
  855.     }
  856.     return -1;
  857. }
  858.  
  859. /* start SCC transmitter when it is idle (SLIP/KISS mode only) */
  860. static void
  861. scc_sstart(scc)
  862. register struct sccchan *scc;
  863. {
  864.     if(scc->tbp != NULLBUF ||    /* busy */
  865.      scc->sndq == NULLBUF)    /* no work */
  866.         return;
  867.  
  868.     scc->tbp = dequeue(&scc->sndq);
  869.     WRREG(scc->data,FR_END);
  870. }
  871.  
  872. /* show SCC status */
  873. int
  874. dosccstat()
  875. {
  876.     register struct sccchan *scc;
  877.     int i;
  878.  
  879.     if(!Sccinfo.init){
  880.         printf("SCC driver not initialized\n");
  881.         return 0;
  882.     }
  883.     printf("Ch Iface    Sent   Rcvd   Error Space Overr   Rxints   Txints   Exints   Spints\n");
  884.  
  885.     for(i = 0; i <= Sccinfo.maxchan; i++){
  886.         if((scc = Sccchan[i]) == NULLCHAN)
  887.             continue;
  888.  
  889.         if(scc->int_receive == scc_asyrx)
  890.             printf("%2d %-6s  ** asynch ** %7lu %5u %5u %8lu %8lu %8lu %8lu\n",i,scc->iface->name,
  891.                 scc->rxerrs,scc->nospace,scc->rovers,
  892.                 scc->rxints,scc->txints,scc->exints,scc->spints);
  893.         else
  894.             printf("%2d %-6s %6lu %6lu %7lu %5u %5u %8lu %8lu %8lu %8lu\n",i,scc->iface->name,
  895.                 scc->enqueued,scc->rxframes,scc->rxerrs,scc->nospace,scc->rovers,
  896.                 scc->rxints,scc->txints,scc->exints,scc->spints);
  897.     }
  898.     return 0;
  899. }
  900.  
  901. /* send raw frame to SCC. used for AX.25 */
  902. static int
  903. scc_raw(ifp,bp)
  904. struct iface *ifp;
  905. struct mbuf *bp;
  906. {
  907.     struct sccchan *scc;
  908.  
  909.     dump(ifp,IF_TRACE_OUT,bp);
  910.     ifp->rawsndcnt++;
  911.     ifp->lastsent = secclock();
  912.  
  913.     scc = Sccchan[ifp->dev];
  914.  
  915.     if (scc->tx_inhibit){        /* transmitter inhibit */
  916.         free_p(bp);
  917.         return -1;
  918.     }
  919.  
  920.     enqueue(&scc->sndq,bp);        /* enqueue packet */
  921.     scc->enqueued++;
  922.  
  923.     DISABLE();
  924.  
  925.     if(scc->a.tstate == IDLE){    /* when transmitter is idle */
  926.         scc->a.tstate = DEFER;    /* start the key-up sequence */
  927.         scc->a.maxdefer = TPS * scc->a.idletime /
  928.             scc->a.slottime;
  929.         scc->timercount = scc->a.waittime;
  930.     }
  931.     RESTORE();
  932.     return 0;
  933. }
  934.  
  935. static int
  936. scc_send(dev,bp)
  937. int dev;
  938. struct mbuf *bp;
  939. {
  940.     struct sccchan *scc;
  941.  
  942.     scc = Sccchan[dev];
  943.     enqueue(&scc->sndq,bp);
  944.  
  945.     if(scc->tbp == NULLBUF)
  946.         scc_sstart(scc);
  947.     return(0);
  948. }
  949.  
  950. /* initialize interface for AX.25 use */
  951. static int
  952. scc_call(ifp,call)
  953. register struct iface *ifp;
  954. char *call;
  955. {
  956.     char out[AXALEN];
  957.  
  958.     ifp->hwaddr = mallocw(AXALEN);
  959.     if(setcall(out,call) == 0)
  960.         memcpy(ifp->hwaddr,out,AXALEN);
  961.     else
  962.         memcpy(ifp->hwaddr,Mycall,AXALEN);
  963.     return 0;
  964. }
  965.  
  966. /* Interrupt handlers for asynchronous modes (kiss, slip) */
  967.  
  968. /* Transmitter interrupt handler */
  969. /* This routine sends data from mbufs in SLIP format */
  970. static void
  971. scc_asytx(scc)
  972. register struct sccchan *scc;
  973. {
  974.     register struct mbuf *bp;
  975.  
  976.     scc->txints++;
  977.  
  978.     if(scc->txchar != 0){        /* a character pending for transmit? */
  979.         WRREG(scc->data,scc->txchar);    /* send it now */
  980.         scc->txchar = 0;        /* next time, ignore it */
  981.         return;
  982.     }
  983.  
  984.     if(scc->tbp == NULLBUF){    /* nothing to send? */
  985.         if((scc->tbp = scc->sndq) != NULLBUF){ /* dequeue next frame */
  986.             scc->sndq = scc->sndq->anext;
  987.             WRREG(scc->data,FR_END);    /* send FR_END to flush line garbage */
  988.         } else {
  989.             WRREG(scc->ctrl,RES_Tx_P);    /* else only reset pending int */
  990.         }
  991.         return;
  992.     }
  993.     while ((bp = scc->tbp)->cnt == 0){ /* nothing left in this mbuf? */
  994.         bp = bp->next;            /* save link to next */
  995.  
  996.         free_mbuf(scc->tbp);
  997.  
  998.         if((scc->tbp = bp) == NULLBUF){ /* see if more mbufs follow */
  999.             WRREG(scc->data,FR_END);    /* frame complete, send FR_END */
  1000.             return;
  1001.         }
  1002.     }
  1003.     /* now bp = scc->tbp (either from while or from if stmt above) */
  1004.  
  1005.     WRREG(scc->data,*(bp->data));    /* just send the character */
  1006.     bp->cnt--;                /* decrease mbuf byte count */
  1007.     bp->data++;                /* and increment the data pointer */
  1008. }
  1009.  
  1010. /* External/Status interrupt handler */
  1011. static void
  1012. scc_asyex(scc)
  1013. register struct sccchan *scc;
  1014. {
  1015.     register unsigned char status,changes;
  1016.  
  1017.     scc->exints++;
  1018.     status = RDREG(scc->ctrl);
  1019.     changes = status ^ scc->status;
  1020.  
  1021.     if(changes & BRK_ABRT){        /* BREAK? */
  1022.         if((status & BRK_ABRT) == 0)    /* BREAK now over? */
  1023.             VOID(RDREG(scc->data));    /* read the NUL character */
  1024.     }
  1025.     scc->status = status;
  1026.     WRREG(scc->ctrl,RES_EXT_INT);
  1027. }
  1028.  
  1029. /* Receiver interrupt handler under NOS.
  1030.  * Since the higher serial protocol routines are all written to work
  1031.  * well with the routines in 8250.c, it makes sense to handle
  1032.  * asynch i/o with the 8530 in a similar manner. Therefore, these
  1033.  * routines are as close to their counterparts in 8250.c as possible.
  1034.  */
  1035.  
  1036. static void
  1037. scc_asyrx(scc)
  1038. register struct sccchan *scc;
  1039. {
  1040.     register struct fifo *fp;
  1041.     char c;
  1042.  
  1043.     scc->rxints++;
  1044.  
  1045.     fp = &(scc->fifo);
  1046.     do {
  1047.         c = RDREG(scc->data);
  1048.         if(fp->cnt != fp->bufsize){
  1049.             *fp->wp++ = c;
  1050.             if(fp->wp >= &fp->buf[fp->bufsize])
  1051.                 fp->wp = fp->buf;
  1052.             fp->cnt++;
  1053.         } else
  1054.             scc->nospace++;
  1055.     } while(RDREG(scc->ctrl) & Rx_CH_AV);
  1056.     psignal(fp,1);    /* eventually move this to timer routine */
  1057. }
  1058.  
  1059. /* Blocking read from asynch input.
  1060.  * Essentially the same as get_asy() in 8250.c
  1061.  * See comments in asyrxint().
  1062.  */
  1063. static int
  1064. get_scc(dev)
  1065. int dev;
  1066. {
  1067.     register struct fifo *fp;
  1068.     char c;
  1069.     int tmp;
  1070.  
  1071.     fp = &(Sccchan[dev]->fifo);
  1072.  
  1073.     for(;;){
  1074.         DISABLE();
  1075.         tmp = fp->cnt;
  1076.         if(tmp != 0){
  1077.             fp->cnt--;
  1078.             RESTORE();
  1079.             break;
  1080.         }
  1081.         RESTORE();
  1082.         pwait(fp);
  1083.     }
  1084.     c = *fp->rp++;
  1085.     if(fp->rp >= &fp->buf[fp->bufsize])
  1086.         fp->rp = fp->buf;
  1087.  
  1088.     return uchar(c);
  1089. }
  1090.  
  1091. int
  1092. scc_frameup(dev)
  1093. int dev;
  1094. {
  1095.     Sccchan[dev]->rxframes++;
  1096.     return 0;
  1097. }
  1098.  
  1099. /* Receive Special Condition interrupt handler */
  1100. static void
  1101. scc_asysp(scc)
  1102. register struct sccchan *scc;
  1103. {
  1104.     register unsigned char status;
  1105.  
  1106.     scc->spints++;
  1107.  
  1108.     status = rd(scc,R1);        /* read receiver status */
  1109.     VOID(RDREG(scc->data));        /* flush offending character */
  1110.  
  1111.     if(status & (CRC_ERR | Rx_OVR))        /* did a framing error or overrun occur ? */
  1112.         scc->rovers++;            /* report as overrun */
  1113.  
  1114.     WRREG(scc->ctrl,ERR_RES);
  1115. }
  1116.  
  1117. /* Interrupt handlers for sdlc mode (AX.25) */
  1118.  
  1119. /* Transmitter interrupt handler */
  1120. static void
  1121. scc_sdlctx(scc)
  1122. register struct sccchan *scc;
  1123. {
  1124.     register struct mbuf *bp;
  1125.  
  1126.     scc->txints++;
  1127.  
  1128.     switch(scc->a.tstate){        /* look at transmitter state */
  1129.     case ACTIVE:            /* busy sending data bytes */
  1130.         while ((bp = scc->tbp)->cnt == 0){    /* nothing left in this mbuf? */
  1131.             bp = bp->next;            /* save link to next */
  1132.             free_mbuf(scc->tbp);    /*KM*/
  1133.             if((scc->tbp = bp) == NULLBUF){/* see if more mbufs follow */
  1134.                 if(RDREG(scc->ctrl) & TxEOM){    /* check tx underrun status */
  1135.                     scc->rovers++;        /* oops, an underrun! count them */
  1136.                     WRREG(scc->ctrl,SEND_ABORT);/* send an abort to be sure */
  1137.                     scc->a.tstate = TAIL;    /* key down tx after TAILTIME */
  1138.                     scc->timercount = scc->a.tailtime;
  1139.                     return;
  1140.                 }
  1141.                 cl(scc,R10,ABUNDER);        /* frame complete, allow CRC transmit */
  1142.                 scc->a.tstate = FLUSH;
  1143.                 WRREG(scc->ctrl,RES_Tx_P);    /* reset pending int */
  1144.                 return;
  1145.             }
  1146.         }
  1147.         /* now bp = scc->tbp (either from while or from if stmt above) */
  1148.         WRREG(scc->data,*(bp->data++)); /* send the character */
  1149.         bp->cnt--;            /* decrease mbuf byte count */
  1150.         return;
  1151.     case FLUSH:    /* CRC just went out, more to send? */
  1152.         or(scc,R10,ABUNDER);        /* re-install underrun protection */
  1153.         /* verify that we are not exeeding max tx time (if defined) */
  1154.         if((scc->timercount != 0 || scc->a.maxkeyup == 0) &&
  1155.          (scc->tbp = scc->sndq) != NULLBUF){ /* dequeue a frame */
  1156.             scc->sndq = scc->sndq->anext;
  1157.             WRREG(scc->ctrl,RES_Tx_CRC); /* reset the TX CRC generator */
  1158.             scc->a.tstate = ACTIVE;
  1159.             scc_sdlctx(scc);        /* write 1st byte */
  1160.             WRREG(scc->ctrl,RES_EOM_L); /* reset the EOM latch */
  1161.             return;
  1162.         }
  1163.         scc->a.tstate = TAIL;        /* no more, key down tx after TAILTIME */
  1164.         scc->timercount = scc->a.tailtime;
  1165.         WRREG(scc->ctrl,RES_Tx_P);
  1166.         return;
  1167.     default:                /* another state */
  1168.         WRREG(scc->ctrl,RES_Tx_P);    /* then don't send anything */
  1169.         return;
  1170.     }
  1171. }
  1172.  
  1173. /* External/Status interrupt handler */
  1174. static void
  1175. scc_sdlcex(scc)
  1176. register struct sccchan *scc;
  1177. {
  1178.     register unsigned char status,changes;
  1179.  
  1180.     scc->exints++;
  1181.     status = RDREG(scc->ctrl);
  1182.     changes = status ^ scc->status;
  1183.  
  1184.     if(changes & BRK_ABRT){        /* Received an ABORT */
  1185.         if(status & BRK_ABRT){        /* is this the beginning? */
  1186.             if(scc->rbp != NULLBUF){/* did we receive something? */
  1187.                 /* check if a significant amount of data came in */
  1188.                 /* this is because the drop of DCD tends to generate an ABORT */
  1189.                 if(scc->rbp->next != NULLBUF || scc->rbp->cnt > 0)
  1190.                 scc->rxerrs++;    /* then count it as an error */
  1191.                 scc_tossb(scc);        /* throw away buffer */
  1192.             }
  1193.             VOID(RDREG(scc->data));    /* flush the FIFO */
  1194.             VOID(RDREG(scc->data));
  1195.             VOID(RDREG(scc->data));
  1196.         }
  1197.     }
  1198.     if(changes & CTS){            /* CTS input changed state */
  1199.         if(status & CTS){        /* CTS is now ON */
  1200.             if(scc->a.tstate == KEYWT &&
  1201.                 scc->a.txdelay == 0) /* zero TXDELAY = wait for CTS */
  1202.             scc->timercount = 1;    /* it will start within 10 ms */
  1203.         }
  1204.     }
  1205.     if(changes & DCD){            /* DCD input changed state */
  1206.         if(status & DCD){        /* DCD is now ON */
  1207.             if (!scc->extclock)
  1208.                 WRSCC(scc->ctrl,R14,SEARCH|scc->wreg[R14]); /* DPLL: enter search mode */
  1209.             or(scc,R3,ENT_HM|RxENABLE); /* enable the receiver, hunt mode */
  1210.         } else {            /* DCD is now OFF */
  1211.             cl(scc,R3,ENT_HM|RxENABLE); /* disable the receiver */
  1212.             VOID(RDREG(scc->data));    /* flush the FIFO */
  1213.             VOID(RDREG(scc->data));
  1214.             VOID(RDREG(scc->data));
  1215.             if(scc->rbp != NULLBUF){/* did we receive something? */
  1216.                 /* check if a significant amount of data came in */
  1217.                 /* this is because some characters precede the drop of DCD */
  1218.                 if(scc->rbp->next != NULLBUF || scc->rbp->cnt > 0)
  1219.                 scc->rxerrs++;    /* then count it as an error */
  1220.                 scc_tossb(scc);        /* throw away buffer */
  1221.             }
  1222.         }
  1223.     }
  1224.     scc->status = status;
  1225.     WRREG(scc->ctrl,RES_EXT_INT);
  1226. }
  1227.  
  1228. /* Receiver interrupt handler */
  1229. static void
  1230. scc_sdlcrx(scc)
  1231. register struct sccchan *scc;
  1232. {
  1233.     register struct mbuf *bp;
  1234.  
  1235.     scc->rxints++;
  1236.  
  1237.     if((bp = scc->rbp1) == NULLBUF){ /* no buffer available now */
  1238.         if(scc->rbp == NULLBUF){
  1239.             if((bp = alloc_mbuf(scc->bufsiz+sizeof(struct iface *))) != NULLBUF){
  1240.                 scc->rbp = scc->rbp1 = bp;
  1241.                 bp->cnt = 0;
  1242.             }
  1243.         } else if((bp = alloc_mbuf(scc->bufsiz)) != NULLBUF){
  1244.             scc->rbp1 = bp;
  1245.             for(bp = scc->rbp; bp->next != NULLBUF; bp = bp->next)
  1246.                 ;
  1247.             bp->next = scc->rbp1;
  1248.             bp = scc->rbp1;
  1249.         }
  1250.         if(bp == NULLBUF){
  1251.             VOID(RDREG(scc->data));    /* so we have to discard the char */
  1252.             or(scc,R3,ENT_HM);        /* enter hunt mode for next flag */
  1253.             scc_tossb(scc);        /* put buffers back on pool */
  1254.             scc->nospace++;        /* count these events */
  1255.             return;
  1256.         }
  1257.     }
  1258.  
  1259.     /* now, we have a buffer (at bp). read character and store it */
  1260.     bp->data[bp->cnt++] = RDREG(scc->data);
  1261.  
  1262.     if(bp->cnt == bp->size)        /* buffer full? */
  1263.         scc->rbp1 = NULLBUF;    /* acquire a new one next time */
  1264. }
  1265.  
  1266. /* Receive Special Condition interrupt handler */
  1267. static void
  1268. scc_sdlcsp(scc)
  1269. register struct sccchan *scc;
  1270. {
  1271.     register unsigned char status;
  1272.     register struct mbuf *bp;
  1273.  
  1274.     scc->spints++;
  1275.  
  1276.     status = rd(scc,R1);        /* read receiver status */
  1277.     VOID(RDREG(scc->data));        /* flush offending character */
  1278.  
  1279.     if(status & Rx_OVR){        /* receiver overrun */
  1280.         scc->rovers++;            /* count them */
  1281.         or(scc,R3,ENT_HM);        /* enter hunt mode for next flag */
  1282.         scc_tossb(scc);            /* rewind the buffer and toss */
  1283.     }
  1284.     if(status & END_FR &&        /* end of frame */
  1285.     scc->rbp != NULLBUF){    /* at least received something */
  1286.         if((status & CRC_ERR) == 0 &&    /* no CRC error is indicated */
  1287.         (status & 0xe) == RES8 &&    /* 8 bits in last byte */
  1288.         scc->rbp->cnt > 0){
  1289.  
  1290.             /* we seem to have a good frame. but the last byte received */
  1291.             /* from rx interrupt is in fact a CRC byte, so discard it */
  1292.             if(scc->rbp1 != NULLBUF){
  1293.                 scc->rbp1->cnt--;    /* current mbuf was not full */
  1294.             } else {
  1295.                 for(bp = scc->rbp; bp->next != NULLBUF; bp = bp->next);
  1296.                     /* find last mbuf */
  1297.  
  1298.                 bp->cnt--;        /* last byte is first CRC byte */
  1299.             }
  1300.  
  1301.             net_route(scc->iface,scc->rbp);
  1302.  
  1303.             scc->rbp = scc->rbp1 = NULLBUF;
  1304.             scc->rxframes++;
  1305.         } else {            /* a bad frame */
  1306.             scc_tossb(scc);        /* throw away frame */
  1307.             scc->rxerrs++;
  1308.         }
  1309.     }
  1310.     WRREG(scc->ctrl,ERR_RES);
  1311. }
  1312.  
  1313. /* Throw away receive mbuf(s) when an error occurred */
  1314. static void
  1315. scc_tossb (scc)
  1316. register struct sccchan *scc;
  1317. {
  1318.     register struct mbuf *bp;
  1319.     
  1320.     if((bp = scc->rbp) != NULLBUF){
  1321.         free_p(bp->next);
  1322.         free_p(bp->dup);    /* Should be NULLBUF */
  1323.         bp->next = NULLBUF;
  1324.         scc->rbp1 = bp;        /* Don't throw this one away */
  1325.         bp->cnt = 0;    /* Simply rewind it */
  1326.     }
  1327. }
  1328.  
  1329. /* Switch the SCC to "transmit" mode */
  1330. /* Only to be called from an interrupt handler, while in AX.25 mode */
  1331. static void
  1332. scc_txon(scc)
  1333. register struct sccchan *scc;
  1334. {
  1335.     if (!scc->fulldup && !scc->extclock){ /* no fulldup divider? */
  1336.         cl(scc,R3,RxENABLE);        /* then switch off receiver */
  1337.         cl(scc,R5,TxENAB);        /* transmitter off during switch */
  1338.         scc_speed(scc,1,scc->speed);    /* reprogram baudrate generator */
  1339.     }
  1340.     or(scc,R5,RTS|TxENAB);        /* set the RTS line and enable TX */
  1341.     if(Sccinfo.hwtype & HWPRIMUS)    /* PRIMUS has another PTT bit... */
  1342.         WRREG(scc->ctrl + 4,Sccinfo.hwparam | 0x80); /* set that bit! */
  1343. }
  1344.  
  1345. /* Switch the SCC to "receive" mode (or: switch off transmitter)
  1346.  * Only to be called from an interrupt handler, while in AX.25 mode
  1347.  */
  1348. static void
  1349. scc_txoff(scc)
  1350. register struct sccchan *scc;
  1351. {
  1352.     cl(scc,R5,RTS);            /* turn off RTS line */
  1353.     if(Sccinfo.hwtype & HWPRIMUS)    /* PRIMUS has another PTT bit... */
  1354.         WRREG(scc->ctrl + 4,Sccinfo.hwparam); /* clear that bit! */
  1355.  
  1356.     if (!scc->fulldup && !scc->extclock){ /* no fulldup divider? */
  1357.         cl(scc,R5,TxENAB);        /* then disable the transmitter */
  1358.         scc_speed(scc,32,scc->speed);    /* back to receiver baudrate */
  1359.     }
  1360. }
  1361.  
  1362. /* SCC timer interrupt handler. Will be called every 1/TPS s by the 
  1363.  * routine systick in pc.c
  1364.  */
  1365. void scctimer()
  1366. {
  1367.     register struct sccchan *scc;
  1368.     register struct sccchan **sccp;
  1369.  
  1370.     DISABLE();
  1371.     for(sccp = Sccchan + Sccinfo.maxchan; sccp >= Sccchan; sccp--){
  1372.         if((scc = *sccp) != NULLCHAN &&
  1373.           scc->timercount != 0 &&
  1374.           --(scc->timercount) == 0){
  1375.             /* handle an SCC timer event for this SCC channel
  1376.              * this can only happen when the channel is AX.25 type
  1377.              * (the SLIP/KISS driver does not use timers)
  1378.              */
  1379.             switch(scc->a.tstate){
  1380.             case IDLE:            /* it was idle, this is FULLDUP2 timeout */
  1381.                 scc_txoff(scc);        /* switch-off the transmitter */
  1382.                 break;
  1383.             case DEFER:            /* trying to get the channel */
  1384.                 /* operation is as follows:
  1385.                  * CSMA: when channel clear AND persistence randomgenerator
  1386.                  *     wins, AND group restrictions allow it:
  1387.                  *        keyup the transmitter
  1388.                  *     if not, delay one SLOTTIME and try again
  1389.                  * FULL: always keyup the transmitter
  1390.                  */
  1391.                 if(scc->a.fulldup == 0){
  1392.                     Random = 21 * Random + 53;
  1393.                     if(scc->status & DCD || scc->a.persist < Random){
  1394.                         /* defer transmission again. check for limit */
  1395. defer_it:                    if(--(scc->a.maxdefer) == 0){
  1396.                             /* deferred too long. choice is to:
  1397.                              * - throw away pending frames, or
  1398.                              * - smash-on the transmitter and send them.
  1399.                              * the first would be the choice in a clean
  1400.                              * environment, but in the amateur radio world
  1401.                              * a distant faulty station could tie us up
  1402.                              * forever, so the second may be better...
  1403.                             */
  1404. #ifdef THROW_AWAY_AFTER_DEFER_TIMEOUT
  1405.                             struct mbuf *bp,*bp1;
  1406.  
  1407.                             while ((bp = scc->sndq) != NULLBUF){
  1408.                                 scc->sndq = scc->sndq->anext;
  1409.                                 free_p(bp);
  1410.                             }
  1411. #else
  1412.                             goto keyup; /* just keyup the transmitter... */
  1413. #endif
  1414.                         }
  1415.                         scc->timercount = scc->a.slottime;
  1416.                         break;
  1417.                     }
  1418.                     if(uchar(scc->group) != NOGROUP){
  1419.                         int i;
  1420.                         struct sccchan *scc2;
  1421.  
  1422.                         for(i = 0; i <= Sccinfo.maxchan; i++)
  1423.                             if((scc2 = Sccchan[i]) != NULLCHAN &&
  1424.                              scc2 != scc &&
  1425.                              uchar(scc2->group) & uchar(scc->group) &&
  1426.                              ((scc->group & TXGROUP && scc2->wreg[R5] & RTS) ||
  1427.                              (scc->group & RXGROUP && scc2->status & DCD))){
  1428.                                 goto defer_it;
  1429.                             }
  1430.                     }
  1431.                 }
  1432.             case KEYUP:            /* keyup transmitter (note fallthrough) */
  1433. keyup:                if((scc->wreg[R5] & RTS) == 0){ /* when not yet keyed */
  1434.                     scc->a.tstate = KEYWT;
  1435.                     scc->timercount = scc->a.txdelay; /* 0 if CTSwait */
  1436.                     scc_txon(scc);
  1437.                     break;
  1438.                 }
  1439.                 /* when already keyed, directly fall through */
  1440.             case KEYWT:            /* waited for CTS or TXDELAY */
  1441.                 /* when a frame is available (it should be...):
  1442.                  * - dequeue it from the send queue
  1443.                  * - reset the transmitter CRC generator
  1444.                  * - set a timeout on transmission length, if defined
  1445.                  * - send the first byte of the frame
  1446.                  * - reset the EOM latch
  1447.                  * when no frame available, proceed to TAIL handling
  1448.                  */
  1449.                 if((scc->tbp = scc->sndq) != NULLBUF){
  1450.                     scc->sndq = scc->sndq->anext;
  1451.                     WRREG(scc->ctrl,RES_Tx_CRC);
  1452.                     scc->a.tstate = ACTIVE;
  1453.                     scc->timercount = TPS * scc->a.maxkeyup;
  1454.                     scc_sdlctx(scc);
  1455.                     WRREG(scc->ctrl,RES_EOM_L);
  1456.                     break;
  1457.                 }
  1458.                 /* when no frame queued, fall through to TAIL case */
  1459.             case TAIL:            /* at end of frame */
  1460.                 /* when fulldup is 0 or 1, switch off the transmitter.
  1461.                  * when frames are still queued (because of transmit time limit),
  1462.                  * restart the procedure to get the channel after MINTIME.
  1463.                  * when fulldup is 2, the transmitter remains keyed and we
  1464.                  * continue sending.    IDLETIME is an idle timeout in this case.
  1465.                  */    
  1466.                 if(scc->a.fulldup < 2){
  1467.                     scc->a.tstate = IDLE;
  1468.                     scc_txoff(scc);
  1469.  
  1470.                     if(scc->sndq != NULLBUF){
  1471.                         scc->a.tstate = DEFER;
  1472.                         scc->a.maxdefer = TPS * scc->a.idletime /
  1473.                          scc->a.slottime;
  1474.                         scc->timercount = TPS * scc->a.mintime;
  1475.                     }
  1476.                     break;
  1477.                 }
  1478.                 if(scc->sndq != NULLBUF){ /* still frames on the queue? */
  1479.                     scc->a.tstate = KEYWT; /* continue sending */
  1480.                     scc->timercount = TPS * scc->a.mintime; /* after mintime */
  1481.                 } else {
  1482.                     scc->a.tstate = IDLE;
  1483.                     scc->timercount = TPS * scc->a.idletime;
  1484.                 }
  1485.                 break;
  1486.             case ACTIVE:    /* max keyup time expired */
  1487.             case FLUSH:    /* same while in flush mode */
  1488.                 break;    /* no action required yet */
  1489.             default:            /* unexpected state */
  1490.                 scc->a.tstate = IDLE; /* that should not happen, but... */
  1491.                 scc_txoff(scc);        /* at least stop the transmitter */
  1492.                 break;
  1493.             }
  1494.         }
  1495.     }
  1496.     RESTORE();
  1497. }
  1498.