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