home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / SCC.C < prev    next >
C/C++ Source or Header  |  1991-02-10  |  47KB  |  1,494 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. #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.  
  230.     /* first handle the special "init" mode, to initialize global stuff */
  231.  
  232.     if(!strcmp(argv[2],"init")){
  233.     if(argc < 11)            /* need at least argv[1]..argv[10] */
  234.         return -1;
  235.  
  236.     if(isupper(argv[10][0]))
  237.         argv[10][0] = tolower(argv[10][0]);
  238.  
  239.     if(argv[10][0] == 'p'){    /* wants to use PCLK as clock? */
  240.         pclk = 1;
  241.         argv[10]++;
  242.     } else {
  243.         if(argv[10][0] == 'r')    /* wants to use RTxC? */
  244.         argv[10]++;        /* that's the default */
  245.     }
  246.     if(argc > 11)            /* optional hardware type */
  247.         hwtype = htoi(argv[11]);    /* it is given in hex */
  248.  
  249.     if(argc > 12)            /* optional hardware param */
  250.         hwparam = htoi(argv[12]);    /* also in hex */
  251.  
  252.     return scc_init(atoi(argv[1]),(ioaddr) htol(argv[3]),atoi(argv[4]),
  253.         atoi(argv[5]),atoi(argv[6]),atoi(argv[7]),
  254.         (ioaddr) htol(argv[8]),atoi(argv[9]),
  255.         atol(argv[10]),pclk,hwtype,hwparam);
  256.     }
  257.     /* not "init", so it must be a valid mode to attach a channel */
  258.     if(strcmp(argv[2],"ax25") && strcmp(argv[2],"kiss") &&
  259.      strcmp(argv[2],"slip")){
  260.         printf("Mode %s unknown for SCC\n",argv[2]);
  261.         return -1;
  262.     }
  263.     if(strcmp(argv[2],"slip") == 0 || strcmp(argv[2],"kiss") == 0){
  264.         for(xdev = 0;xdev < SLIP_MAX;xdev++)
  265.             if(Slip[xdev].iface == NULLIF)
  266.                 break;
  267.         if(xdev >= SLIP_MAX){
  268.             tprintf("Too many slip devices\n");
  269.             return -1;
  270.         }
  271.     }
  272.     if(strcmp(argv[2],"nrs") == 0){
  273.         for(xdev = 0;xdev < NRS_MAX;xdev++)
  274.             if(Nrs[xdev].iface == NULLIF)
  275.                 break;
  276.  
  277.         if(xdev >= NRS_MAX){
  278.             tprintf("Too many nrs devices\n");
  279.             return -1;
  280.         }
  281.     }
  282.     if(!Sccinfo.init){
  283.         printf("First init SCC driver\n");
  284.         return -1;
  285.     }
  286.     if((chan = atoi(argv[1])) > Sccinfo.maxchan){
  287.         printf("SCC channel %d out of range\n",chan);
  288.         return -1;
  289.     }
  290.     if(Sccchan[chan] != NULLCHAN){
  291.         printf("SCC channel %d already attached\n",chan);
  292.         return -1;
  293.     }
  294.     /* create interface structure and fill in details */
  295.     ifp = (struct iface *) callocw(1,sizeof(struct iface));
  296.     ifp->name = mallocw(strlen(argv[3]) + 1);
  297.     strcpy(ifp->name,argv[3]);
  298.  
  299.     ifp->mtu = atoi(argv[4]);
  300.     ifp->dev = chan;
  301.     ifp->stop = scc_stop;
  302.  
  303.     scc = (struct sccchan *) callocw(1,sizeof(struct sccchan));
  304.     scc->ctrl = Sccinfo.iobase + (chan / 2) * Sccinfo.space + Sccinfo.off[chan % 2];
  305.     scc->data = scc->ctrl + Sccinfo.doff;
  306.     scc->iface = ifp;
  307.  
  308.     if(isupper(argv[5][0]))
  309.         argv[5][0] = tolower(argv[5][0]);
  310.  
  311.     switch (argv[5][0]) {
  312.     case 'd':                /* fulldup divider installed? */
  313.         scc->fulldup = 1;        /* set appropriate flag */
  314.         argv[5]++;            /* skip the 'd' */
  315.         break;
  316.  
  317.     case 'e':                /* external clocking? */
  318.         scc->extclock = 1;        /* set the flag */
  319.         break;
  320.     }
  321.  
  322.     scc->bufsiz = atoi(argv[6]);
  323.     ifp->addr = Ip_addr; 
  324.     Sccchan[chan] = scc;        /* put addr in table for interrupts */
  325.  
  326.     switch(argv[2][0]){             /* mode already checked above */
  327. #ifdef AX25
  328.     case 'a':    /* AX.25 */
  329.         scc_sdlc(scc);                /* init SCC in SDLC mode */
  330.  
  331.         if (!scc->extclock) {
  332.             brgrate = scc_speed(scc,32,atol(argv[5]));/* init SCC speed */
  333.             scc->speed = Sccinfo.clk / (64L * (brgrate + 2));/* calc real speed */
  334.         }
  335.  
  336.         brgrate = scc_speed(scc,32,atol(argv[5]));/* init SCC speed */
  337.         scc->speed = Sccinfo.clk / (64L * (brgrate + 2));/* calc real speed */
  338.         setencap(ifp,"AX25");
  339.         scc_call(ifp,argc > 7 ? argv[7] : (char *) 0);    /* set the callsign */
  340.             ifp->ioctl = scc_sioctl;
  341.         ifp->raw = scc_raw;
  342.  
  343.         /* default KISS Params */
  344.         scc->a.txdelay = 36*TPS/100;    /* 360 ms */
  345.         scc->a.persist = 25;        /* 10% persistence */
  346.         scc->a.slottime = 16*TPS/100; /* 160 ms */
  347. #if TPS > 67
  348.         scc->a.tailtime = 3*TPS/100;    /* 30 ms */
  349. #else
  350.         scc->a.tailtime = 2;        /* minimal reasonable value */
  351. #endif
  352.         scc->a.fulldup = 0;        /* CSMA */
  353.         scc->a.waittime = 50*TPS/100; /* 500 ms */
  354.         scc->a.maxkeyup = 7;        /* 7 s */
  355.         scc->a.mintime = 3;        /* 3 s */
  356.         scc->a.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 int32
  749. scc_aioctl(ifp,cmd,set,val)
  750. struct iface *ifp;
  751. int cmd;
  752. int set;
  753. int32 val;
  754. {
  755.     struct sccchan *scc;
  756.     unsigned int brgrate;
  757.  
  758.     scc = Sccchan[ifp->dev];
  759.  
  760.     switch(cmd){
  761.     case PARAM_SPEED:
  762.         if(set){
  763.             brgrate = scc_speed(scc,16,val);
  764.             scc->speed = Sccinfo.clk / (32L * (brgrate + 2));
  765.         }
  766.         return scc->speed;
  767.     }
  768.     return 0;
  769. }
  770.  
  771. /* perform ioctl on SCC (sdlc) channel
  772.  * this is used for AX.25 mode only, and will set the "kiss" parameters
  773.  */
  774. static int32
  775. scc_sioctl(ifp,cmd,set,val)
  776. struct iface *ifp;
  777. int cmd;
  778. int set;
  779. int32 val;
  780. {
  781.     struct sccchan *scc;
  782.     int i_state;
  783.     unsigned int brgrate;
  784.     
  785.     scc = Sccchan[ifp->dev];
  786.  
  787.     switch(cmd){
  788.     case PARAM_SPEED:
  789.         if(set){
  790.             if(val == 0)
  791.                 scc->extclock = 1;
  792.             else {
  793.                 brgrate = scc_speed(scc,32,val);/* init SCC speed */
  794.                 scc->speed = Sccinfo.clk / (64L * (brgrate + 2));/* calc real speed */
  795.             }
  796.         }
  797.         return scc->speed;
  798.     case PARAM_TXDELAY:
  799.         if(set)
  800.             scc->a.txdelay = val;
  801.         return scc->a.txdelay;
  802.     case PARAM_PERSIST:
  803.         if(set)
  804.             scc->a.persist = val;
  805.         return scc->a.persist;
  806.     case PARAM_SLOTTIME:
  807.         if(set)
  808.             scc->a.slottime = val;
  809.         return scc->a.slottime;
  810.     case PARAM_TXTAIL:
  811.         if(set)
  812.             scc->a.tailtime = val;
  813.         return scc->a.tailtime;
  814.     case PARAM_FULLDUP:
  815.         if(set)
  816.             scc->a.fulldup = val;
  817.         return scc->a.fulldup;
  818.     case PARAM_WAIT:
  819.         if(set)
  820.             scc->a.waittime = val;
  821.         return scc->a.waittime;
  822.     case PARAM_MAXKEY:
  823.         if(set)
  824.             scc->a.maxkeyup = val;
  825.         return scc->a.maxkeyup;
  826.     case PARAM_MIN:
  827.         if(set)
  828.             scc->a.mintime = val;
  829.         return scc->a.mintime;
  830.     case PARAM_IDLE:
  831.         if(set)
  832.             scc->a.idletime = val;
  833.         return scc->a.idletime;
  834.     case PARAM_DTR:
  835.         if(set){
  836.             if(val)
  837.                 scc->wreg[R5] |= DTR;
  838.             else
  839.                 scc->wreg[R5] &= ~DTR;
  840.  
  841.             i_state = dirps();
  842.             if(scc->a.tstate == IDLE && scc->timercount == 0)
  843.                 scc->timercount = 1;    /* force an update */
  844.             restore(i_state);
  845.         }
  846.         return (scc->wreg[R5] & DTR) ? 1 : 0;
  847.     case PARAM_GROUP:
  848.         if(set)
  849.             scc->group = val;
  850.         return scc->group;
  851.     }
  852.     return -1;
  853. }
  854.  
  855. /* start SCC transmitter when it is idle (SLIP/KISS mode only) */
  856. static void
  857. scc_sstart(scc)
  858. register struct sccchan *scc;
  859. {
  860.     if(scc->tbp != NULLBUF ||    /* busy */
  861.      scc->sndq == NULLBUF)    /* no work */
  862.         return;
  863.  
  864.     scc->tbp = dequeue(&scc->sndq);
  865.     WRREG(scc->data,FR_END);
  866. }
  867.  
  868. /* show SCC status */
  869. int
  870. dosccstat()
  871. {
  872.     register struct sccchan *scc;
  873.     int i;
  874.  
  875.     if(!Sccinfo.init){
  876.         printf("SCC driver not initialized\n");
  877.         return 0;
  878.     }
  879.     printf("Ch Iface    Sent   Rcvd   Error Space Overr   Rxints   Txints   Exints   Spints\n");
  880.  
  881.     for(i = 0; i <= Sccinfo.maxchan; i++){
  882.         if((scc = Sccchan[i]) == NULLCHAN)
  883.             continue;
  884.  
  885.         if(scc->int_receive == scc_asyrx)
  886.             printf("%2d %-6s  ** asynch ** %7lu %5u %5u %8lu %8lu %8lu %8lu\n",i,scc->iface->name,
  887.                 scc->rxerrs,scc->nospace,scc->rovers,
  888.                 scc->rxints,scc->txints,scc->exints,scc->spints);
  889.         else
  890.             printf("%2d %-6s %6lu %6lu %7lu %5u %5u %8lu %8lu %8lu %8lu\n",i,scc->iface->name,
  891.                 scc->enqueued,scc->rxframes,scc->rxerrs,scc->nospace,scc->rovers,
  892.                 scc->rxints,scc->txints,scc->exints,scc->spints);
  893.     }
  894.     return 0;
  895. }
  896.  
  897. /* send raw frame to SCC. used for AX.25 */
  898. static int
  899. scc_raw(ifp,bp)
  900. struct iface *ifp;
  901. struct mbuf *bp;
  902. {
  903.     struct sccchan *scc;
  904.     int i_state;
  905.  
  906.     dump(ifp,IF_TRACE_OUT,CL_AX25,bp);
  907.     ifp->rawsndcnt++;
  908.     ifp->lastsent = secclock();
  909.  
  910.     scc = Sccchan[ifp->dev];
  911.  
  912.     if (scc->tx_inhibit){        /* transmitter inhibit */
  913.         free_p(bp);
  914.         return -1;
  915.     }
  916.  
  917.     enqueue(&scc->sndq,bp);        /* enqueue packet */
  918.     scc->enqueued++;
  919.  
  920.     i_state = dirps();
  921.  
  922.     if(scc->a.tstate == IDLE){    /* when transmitter is idle */
  923.         scc->a.tstate = DEFER;    /* start the key-up sequence */
  924.         scc->a.maxdefer = TPS * scc->a.idletime /
  925.             scc->a.slottime;
  926.         scc->timercount = scc->a.waittime;
  927.     }
  928.     restore(i_state);
  929.     return 0;
  930. }
  931.  
  932. static int
  933. scc_send(dev,bp)
  934. int dev;
  935. struct mbuf *bp;
  936. {
  937.     struct sccchan *scc;
  938.  
  939.     scc = Sccchan[dev];
  940.     enqueue(&scc->sndq,bp);
  941.  
  942.     if(scc->tbp == NULLBUF)
  943.         scc_sstart(scc);
  944.     return(0);
  945. }
  946.  
  947. /* initialize interface for AX.25 use */
  948. static int
  949. scc_call(ifp,call)
  950. register struct iface *ifp;
  951. char *call;
  952. {
  953.     char out[AXALEN];
  954.  
  955.     ifp->hwaddr = mallocw(AXALEN);
  956.     if(setcall(out,call) == 0)
  957.         memcpy(ifp->hwaddr,out,AXALEN);
  958.     else
  959.         memcpy(ifp->hwaddr,Mycall,AXALEN);
  960.     return 0;
  961. }
  962.  
  963. /* Interrupt handlers for asynchronous modes (kiss, slip) */
  964.  
  965. /* Transmitter interrupt handler */
  966. /* This routine sends data from mbufs in SLIP format */
  967. static void
  968. scc_asytx(scc)
  969. register struct sccchan *scc;
  970. {
  971.     register struct mbuf *bp;
  972.  
  973.     scc->txints++;
  974.  
  975.     if(scc->txchar != 0){        /* a character pending for transmit? */
  976.         WRREG(scc->data,scc->txchar);    /* send it now */
  977.         scc->txchar = 0;        /* next time, ignore it */
  978.         return;
  979.     }
  980.  
  981.     if(scc->tbp == NULLBUF){    /* nothing to send? */
  982.         if((scc->tbp = scc->sndq) != NULLBUF){ /* dequeue next frame */
  983.             scc->sndq = scc->sndq->anext;
  984.             WRREG(scc->data,FR_END);    /* send FR_END to flush line garbage */
  985.         } else {
  986.             WRREG(scc->ctrl,RES_Tx_P);    /* else only reset pending int */
  987.         }
  988.         return;
  989.     }
  990.     while ((bp = scc->tbp)->cnt == 0){ /* nothing left in this mbuf? */
  991.         bp = bp->next;            /* save link to next */
  992.  
  993.         free_mbuf(scc->tbp);
  994.  
  995.         if((scc->tbp = bp) == NULLBUF){ /* see if more mbufs follow */
  996.             WRREG(scc->data,FR_END);    /* frame complete, send FR_END */
  997.             return;
  998.         }
  999.     }
  1000.     /* now bp = scc->tbp (either from while or from if stmt above) */
  1001.  
  1002.     WRREG(scc->data,*(bp->data));    /* just send the character */
  1003.     bp->cnt--;                /* decrease mbuf byte count */
  1004.     bp->data++;                /* and increment the data pointer */
  1005. }
  1006.  
  1007. /* External/Status interrupt handler */
  1008. static void
  1009. scc_asyex(scc)
  1010. register struct sccchan *scc;
  1011. {
  1012.     register unsigned char status,changes;
  1013.  
  1014.     scc->exints++;
  1015.     status = RDREG(scc->ctrl);
  1016.     changes = status ^ scc->status;
  1017.  
  1018.     if(changes & BRK_ABRT){        /* BREAK? */
  1019.         if((status & BRK_ABRT) == 0)    /* BREAK now over? */
  1020.             VOID(RDREG(scc->data));    /* read the NUL character */
  1021.     }
  1022.     scc->status = status;
  1023.     WRREG(scc->ctrl,RES_EXT_INT);
  1024. }
  1025.  
  1026. /* Receiver interrupt handler under NOS.
  1027.  * Since the higher serial protocol routines are all written to work
  1028.  * well with the routines in 8250.c, it makes sense to handle
  1029.  * asynch i/o with the 8530 in a similar manner. Therefore, these
  1030.  * routines are as close to their counterparts in 8250.c as possible.
  1031.  */
  1032.  
  1033. static void
  1034. scc_asyrx(scc)
  1035. register struct sccchan *scc;
  1036. {
  1037.     register struct fifo *fp;
  1038.     char c;
  1039.  
  1040.     scc->rxints++;
  1041.  
  1042.     fp = &(scc->fifo);
  1043.     do {
  1044.         c = RDREG(scc->data);
  1045.         if(fp->cnt != fp->bufsize){
  1046.             *fp->wp++ = c;
  1047.             if(fp->wp >= &fp->buf[fp->bufsize])
  1048.                 fp->wp = fp->buf;
  1049.             fp->cnt++;
  1050.         } else
  1051.             scc->nospace++;
  1052.     } while(RDREG(scc->ctrl) & Rx_CH_AV);
  1053.     psignal(fp,1);    /* eventually move this to timer routine */
  1054. }
  1055.  
  1056. /* Blocking read from asynch input.
  1057.  * Essentially the same as get_asy() in 8250.c
  1058.  * See comments in asy_rxint().
  1059.  */
  1060. static int
  1061. get_scc(dev)
  1062. int dev;
  1063. {
  1064.     char i_state;
  1065.     register struct fifo *fp;
  1066.     char c;
  1067.  
  1068.     fp = &(Sccchan[dev]->fifo);
  1069.  
  1070.     i_state = dirps();
  1071.     while(fp->cnt == 0)
  1072.         pwait(fp);
  1073.     fp->cnt--;
  1074.     restore(i_state);
  1075.  
  1076.     c = *fp->rp++;
  1077.     if(fp->rp >= &fp->buf[fp->bufsize])
  1078.         fp->rp = fp->buf;
  1079.  
  1080.     return uchar(c);
  1081. }
  1082.  
  1083. int
  1084. scc_frameup(dev)
  1085. int dev;
  1086. {
  1087.     Sccchan[dev]->rxframes++;
  1088.     return 0;
  1089. }
  1090.  
  1091. /* Receive Special Condition interrupt handler */
  1092. static void
  1093. scc_asysp(scc)
  1094. register struct sccchan *scc;
  1095. {
  1096.     register unsigned char status;
  1097.  
  1098.     scc->spints++;
  1099.  
  1100.     status = rd(scc,R1);        /* read receiver status */
  1101.     VOID(RDREG(scc->data));        /* flush offending character */
  1102.  
  1103.     if(status & (CRC_ERR | Rx_OVR))        /* did a framing error or overrun occur ? */
  1104.         scc->rovers++;            /* report as overrun */
  1105.  
  1106.     WRREG(scc->ctrl,ERR_RES);
  1107. }
  1108.  
  1109. /* Interrupt handlers for sdlc mode (AX.25) */
  1110.  
  1111. /* Transmitter interrupt handler */
  1112. static void
  1113. scc_sdlctx(scc)
  1114. register struct sccchan *scc;
  1115. {
  1116.     register struct mbuf *bp;
  1117.  
  1118.     scc->txints++;
  1119.  
  1120.     switch(scc->a.tstate){        /* look at transmitter state */
  1121.     case ACTIVE:            /* busy sending data bytes */
  1122.         while ((bp = scc->tbp)->cnt == 0){    /* nothing left in this mbuf? */
  1123.             bp = bp->next;            /* save link to next */
  1124.             free_mbuf(scc->tbp);    /*KM*/
  1125.             if((scc->tbp = bp) == NULLBUF){/* see if more mbufs follow */
  1126.                 if(RDREG(scc->ctrl) & TxEOM){    /* check tx underrun status */
  1127.                     scc->rovers++;        /* oops, an underrun! count them */
  1128.                     WRREG(scc->ctrl,SEND_ABORT);/* send an abort to be sure */
  1129.                     scc->a.tstate = TAIL;    /* key down tx after TAILTIME */
  1130.                     scc->timercount = scc->a.tailtime;
  1131.                     return;
  1132.                 }
  1133.                 cl(scc,R10,ABUNDER);        /* frame complete, allow CRC transmit */
  1134.                 scc->a.tstate = FLUSH;
  1135.                 WRREG(scc->ctrl,RES_Tx_P);    /* reset pending int */
  1136.                 return;
  1137.             }
  1138.         }
  1139.         /* now bp = scc->tbp (either from while or from if stmt above) */
  1140.         WRREG(scc->data,*(bp->data++)); /* send the character */
  1141.         bp->cnt--;            /* decrease mbuf byte count */
  1142.         return;
  1143.     case FLUSH:    /* CRC just went out, more to send? */
  1144.         or(scc,R10,ABUNDER);        /* re-install underrun protection */
  1145.         /* verify that we are not exeeding max tx time (if defined) */
  1146.         if((scc->timercount != 0 || scc->a.maxkeyup == 0) &&
  1147.          (scc->tbp = scc->sndq) != NULLBUF){ /* dequeue a frame */
  1148.             scc->sndq = scc->sndq->anext;
  1149.             WRREG(scc->ctrl,RES_Tx_CRC); /* reset the TX CRC generator */
  1150.             scc->a.tstate = ACTIVE;
  1151.             scc_sdlctx(scc);        /* write 1st byte */
  1152.             WRREG(scc->ctrl,RES_EOM_L); /* reset the EOM latch */
  1153.             return;
  1154.         }
  1155.         scc->a.tstate = TAIL;        /* no more, key down tx after TAILTIME */
  1156.         scc->timercount = scc->a.tailtime;
  1157.         WRREG(scc->ctrl,RES_Tx_P);
  1158.         return;
  1159.     default:                /* another state */
  1160.         WRREG(scc->ctrl,RES_Tx_P);    /* then don't send anything */
  1161.         return;
  1162.     }
  1163. }
  1164.  
  1165. /* External/Status interrupt handler */
  1166. static void
  1167. scc_sdlcex(scc)
  1168. register struct sccchan *scc;
  1169. {
  1170.     register unsigned char status,changes;
  1171.  
  1172.     scc->exints++;
  1173.     status = RDREG(scc->ctrl);
  1174.     changes = status ^ scc->status;
  1175.  
  1176.     if(changes & BRK_ABRT){        /* Received an ABORT */
  1177.         if(status & BRK_ABRT){        /* is this the beginning? */
  1178.             if(scc->rbp != NULLBUF){/* did we receive something? */
  1179.                 /* check if a significant amount of data came in */
  1180.                 /* this is because the drop of DCD tends to generate an ABORT */
  1181.                 if(scc->rbp->next != NULLBUF || scc->rbp->cnt > sizeof(struct phdr))
  1182.                 scc->rxerrs++;    /* then count it as an error */
  1183.                 scc_tossb(scc);        /* throw away buffer */
  1184.             }
  1185.             VOID(RDREG(scc->data));    /* flush the FIFO */
  1186.             VOID(RDREG(scc->data));
  1187.             VOID(RDREG(scc->data));
  1188.         }
  1189.     }
  1190.     if(changes & CTS){            /* CTS input changed state */
  1191.         if(status & CTS){        /* CTS is now ON */
  1192.             if(scc->a.tstate == KEYWT &&
  1193.                 scc->a.txdelay == 0) /* zero TXDELAY = wait for CTS */
  1194.             scc->timercount = 1;    /* it will start within 10 ms */
  1195.         }
  1196.     }
  1197.     if(changes & DCD){            /* DCD input changed state */
  1198.         if(status & DCD){        /* DCD is now ON */
  1199.             if (!scc->extclock)
  1200.                 WRSCC(scc->ctrl,R14,SEARCH|scc->wreg[R14]); /* DPLL: enter search mode */
  1201.             or(scc,R3,ENT_HM|RxENABLE); /* enable the receiver, hunt mode */
  1202.         } else {            /* DCD is now OFF */
  1203.             cl(scc,R3,ENT_HM|RxENABLE); /* disable the receiver */
  1204.             VOID(RDREG(scc->data));    /* flush the FIFO */
  1205.             VOID(RDREG(scc->data));
  1206.             VOID(RDREG(scc->data));
  1207.             if(scc->rbp != NULLBUF){/* did we receive something? */
  1208.                 /* check if a significant amount of data came in */
  1209.                 /* this is because some characters precede the drop of DCD */
  1210.                 if(scc->rbp->next != NULLBUF || scc->rbp->cnt > sizeof(struct phdr))
  1211.                 scc->rxerrs++;    /* then count it as an error */
  1212.                 scc_tossb(scc);        /* throw away buffer */
  1213.             }
  1214.         }
  1215.     }
  1216.     scc->status = status;
  1217.     WRREG(scc->ctrl,RES_EXT_INT);
  1218. }
  1219.  
  1220. /* Receiver interrupt handler */
  1221. static void
  1222. scc_sdlcrx(scc)
  1223. register struct sccchan *scc;
  1224. {
  1225.     register struct mbuf *bp;
  1226.  
  1227.     scc->rxints++;
  1228.  
  1229.     if((bp = scc->rbp1) == NULLBUF){ /* no buffer available now */
  1230.         if(scc->rbp == NULLBUF){
  1231.             if((bp = alloc_mbuf(scc->bufsiz+sizeof(struct phdr))) != NULLBUF){
  1232.                 scc->rbp = scc->rbp1 = bp;
  1233.                 bp->cnt = sizeof(struct phdr);    /* get past the header */
  1234.             }
  1235.         } else if((bp = alloc_mbuf(scc->bufsiz)) != NULLBUF){
  1236.             scc->rbp1 = bp;
  1237.             for(bp = scc->rbp; bp->next != NULLBUF; bp = bp->next);
  1238.             bp->next = scc->rbp1;
  1239.             bp = scc->rbp1;
  1240.         }
  1241.         if(bp == NULLBUF){
  1242.             VOID(RDREG(scc->data));    /* so we have to discard the char */
  1243.             or(scc,R3,ENT_HM);        /* enter hunt mode for next flag */
  1244.             scc_tossb(scc);        /* put buffers back on pool */
  1245.             scc->nospace++;        /* count these events */
  1246.             return;
  1247.         }
  1248.     }
  1249.  
  1250.     /* now, we have a buffer (at bp). read character and store it */
  1251.     bp->data[bp->cnt++] = RDREG(scc->data);
  1252.  
  1253.     if(bp->cnt == bp->size)        /* buffer full? */
  1254.         scc->rbp1 = NULLBUF;    /* acquire a new one next time */
  1255. }
  1256.  
  1257. /* Receive Special Condition interrupt handler */
  1258. static void
  1259. scc_sdlcsp(scc)
  1260. register struct sccchan *scc;
  1261. {
  1262.     register unsigned char status;
  1263.     register struct mbuf *bp;
  1264.     struct phdr phdr;
  1265.  
  1266.     scc->spints++;
  1267.  
  1268.     status = rd(scc,R1);        /* read receiver status */
  1269.     VOID(RDREG(scc->data));        /* flush offending character */
  1270.  
  1271.     if(status & Rx_OVR){        /* receiver overrun */
  1272.         scc->rovers++;            /* count them */
  1273.         or(scc,R3,ENT_HM);        /* enter hunt mode for next flag */
  1274.         scc_tossb(scc);            /* rewind the buffer and toss */
  1275.     }
  1276.     if(status & END_FR &&        /* end of frame */
  1277.     scc->rbp != NULLBUF){    /* at least received something */
  1278.         if((status & CRC_ERR) == 0 &&    /* no CRC error is indicated */
  1279.         (status & 0xe) == RES8 &&    /* 8 bits in last byte */
  1280.         scc->rbp->cnt > sizeof(phdr)){
  1281.  
  1282.             /* we seem to have a good frame. but the last byte received */
  1283.             /* from rx interrupt is in fact a CRC byte, so discard it */
  1284.             if(scc->rbp1 != NULLBUF){
  1285.                 scc->rbp1->cnt--;    /* current mbuf was not full */
  1286.             } else {
  1287.                 for(bp = scc->rbp; bp->next != NULLBUF; bp = bp->next);
  1288.                     /* find last mbuf */
  1289.  
  1290.                 bp->cnt--;        /* last byte is first CRC byte */
  1291.             }
  1292.  
  1293.             phdr.iface = scc->iface;
  1294.             phdr.type = CL_AX25;
  1295.             memcpy(&scc->rbp->data[0],(char *)&phdr,sizeof(phdr));
  1296.             enqueue(&Hopper,scc->rbp);
  1297.  
  1298.             scc->rbp = scc->rbp1 = NULLBUF;
  1299.             scc->rxframes++;
  1300.         } else {            /* a bad frame */
  1301.             scc_tossb(scc);        /* throw away frame */
  1302.             scc->rxerrs++;
  1303.         }
  1304.     }
  1305.     WRREG(scc->ctrl,ERR_RES);
  1306. }
  1307.  
  1308. /* Throw away receive mbuf(s) when an error occurred */
  1309. static void
  1310. scc_tossb (scc)
  1311. register struct sccchan *scc;
  1312. {
  1313.     register struct mbuf *bp;
  1314.     
  1315.     if((bp = scc->rbp) != NULLBUF){
  1316.         free_p(bp->next);
  1317.         free_p(bp->dup);    /* Should be NULLBUF */
  1318.         bp->next = NULLBUF;
  1319.         scc->rbp1 = bp;        /* Don't throw this one away */
  1320.         bp->cnt = sizeof(struct phdr);    /* Simply rewind it */
  1321.     }
  1322. }
  1323.  
  1324. /* Switch the SCC to "transmit" mode */
  1325. /* Only to be called from an interrupt handler, while in AX.25 mode */
  1326. static void
  1327. scc_txon(scc)
  1328. register struct sccchan *scc;
  1329. {
  1330.     if (!scc->fulldup && !scc->extclock){ /* no fulldup divider? */
  1331.         cl(scc,R3,RxENABLE);        /* then switch off receiver */
  1332.         cl(scc,R5,TxENAB);        /* transmitter off during switch */
  1333.         scc_speed(scc,1,scc->speed);    /* reprogram baudrate generator */
  1334.     }
  1335.     or(scc,R5,RTS|TxENAB);        /* set the RTS line and enable TX */
  1336.     if(Sccinfo.hwtype & HWPRIMUS)    /* PRIMUS has another PTT bit... */
  1337.         WRREG(scc->ctrl + 4,Sccinfo.hwparam | 0x80); /* set that bit! */
  1338. }
  1339.  
  1340. /* Switch the SCC to "receive" mode (or: switch off transmitter)
  1341.  * Only to be called from an interrupt handler, while in AX.25 mode
  1342.  */
  1343. static void
  1344. scc_txoff(scc)
  1345. register struct sccchan *scc;
  1346. {
  1347.     cl(scc,R5,RTS);            /* turn off RTS line */
  1348.     if(Sccinfo.hwtype & HWPRIMUS)    /* PRIMUS has another PTT bit... */
  1349.         WRREG(scc->ctrl + 4,Sccinfo.hwparam); /* clear that bit! */
  1350.  
  1351.     if (!scc->fulldup && !scc->extclock){ /* no fulldup divider? */
  1352.         cl(scc,R5,TxENAB);        /* then disable the transmitter */
  1353.         scc_speed(scc,32,scc->speed);    /* back to receiver baudrate */
  1354.     }
  1355. }
  1356.  
  1357. /* SCC timer interrupt handler. Will be called every 1/TPS s by the 
  1358.  * routine systick in pc.c
  1359.  */
  1360. void scctimer()
  1361. {
  1362.     register struct sccchan *scc;
  1363.     register struct sccchan **sccp;
  1364.     char i_state;
  1365.  
  1366.     i_state = dirps();
  1367.     for(sccp = Sccchan + Sccinfo.maxchan; sccp >= Sccchan; sccp--){
  1368.         if((scc = *sccp) != NULLCHAN &&
  1369.           scc->timercount != 0 &&
  1370.           --(scc->timercount) == 0){
  1371.             /* handle an SCC timer event for this SCC channel
  1372.              * this can only happen when the channel is AX.25 type
  1373.              * (the SLIP/KISS driver does not use timers)
  1374.              */
  1375.             switch(scc->a.tstate){
  1376.             case IDLE:            /* it was idle, this is FULLDUP2 timeout */
  1377.                 scc_txoff(scc);        /* switch-off the transmitter */
  1378.                 break;
  1379.             case DEFER:            /* trying to get the channel */
  1380.                 /* operation is as follows:
  1381.                  * CSMA: when channel clear AND persistence randomgenerator
  1382.                  *     wins, AND group restrictions allow it:
  1383.                  *        keyup the transmitter
  1384.                  *     if not, delay one SLOTTIME and try again
  1385.                  * FULL: always keyup the transmitter
  1386.                  */
  1387.                 if(scc->a.fulldup == 0){
  1388.                     Random = 21 * Random + 53;
  1389.                     if(scc->status & DCD || scc->a.persist < Random){
  1390.                         /* defer transmission again. check for limit */
  1391. defer_it:                    if(--(scc->a.maxdefer) == 0){
  1392.                             /* deferred too long. choice is to:
  1393.                              * - throw away pending frames, or
  1394.                              * - smash-on the transmitter and send them.
  1395.                              * the first would be the choice in a clean
  1396.                              * environment, but in the amateur radio world
  1397.                              * a distant faulty station could tie us up
  1398.                              * forever, so the second may be better...
  1399.                             */
  1400. #ifdef THROW_AWAY_AFTER_DEFER_TIMEOUT
  1401.                             struct mbuf *bp,*bp1;
  1402.  
  1403.                             while ((bp = scc->sndq) != NULLBUF){
  1404.                                 scc->sndq = scc->sndq->anext;
  1405.                                 free_p(bp);
  1406.                             }
  1407. #else
  1408.                             goto keyup; /* just keyup the transmitter... */
  1409. #endif
  1410.                         }
  1411.                         scc->timercount = scc->a.slottime;
  1412.                         break;
  1413.                     }
  1414.                     if(uchar(scc->group) != NOGROUP){
  1415.                         int i;
  1416.                         struct sccchan *scc2;
  1417.  
  1418.                         for(i = 0; i <= Sccinfo.maxchan; i++)
  1419.                             if((scc2 = Sccchan[i]) != NULLCHAN &&
  1420.                              scc2 != scc &&
  1421.                              uchar(scc2->group) & uchar(scc->group) &&
  1422.                              ((scc->group & TXGROUP && scc2->wreg[R5] & RTS) ||
  1423.                              (scc->group & RXGROUP && scc2->status & DCD))){
  1424.                                 goto defer_it;
  1425.                             }
  1426.                     }
  1427.                 }
  1428.             case KEYUP:            /* keyup transmitter (note fallthrough) */
  1429. keyup:                if((scc->wreg[R5] & RTS) == 0){ /* when not yet keyed */
  1430.                     scc->a.tstate = KEYWT;
  1431.                     scc->timercount = scc->a.txdelay; /* 0 if CTSwait */
  1432.                     scc_txon(scc);
  1433.                     break;
  1434.                 }
  1435.                 /* when already keyed, directly fall through */
  1436.             case KEYWT:            /* waited for CTS or TXDELAY */
  1437.                 /* when a frame is available (it should be...):
  1438.                  * - dequeue it from the send queue
  1439.                  * - reset the transmitter CRC generator
  1440.                  * - set a timeout on transmission length, if defined
  1441.                  * - send the first byte of the frame
  1442.                  * - reset the EOM latch
  1443.                  * when no frame available, proceed to TAIL handling
  1444.                  */
  1445.                 if((scc->tbp = scc->sndq) != NULLBUF){
  1446.                     scc->sndq = scc->sndq->anext;
  1447.                     WRREG(scc->ctrl,RES_Tx_CRC);
  1448.                     scc->a.tstate = ACTIVE;
  1449.                     scc->timercount = TPS * scc->a.maxkeyup;
  1450.                     scc_sdlctx(scc);
  1451.                     WRREG(scc->ctrl,RES_EOM_L);
  1452.                     break;
  1453.                 }
  1454.                 /* when no frame queued, fall through to TAIL case */
  1455.             case TAIL:            /* at end of frame */
  1456.                 /* when fulldup is 0 or 1, switch off the transmitter.
  1457.                  * when frames are still queued (because of transmit time limit),
  1458.                  * restart the procedure to get the channel after MINTIME.
  1459.                  * when fulldup is 2, the transmitter remains keyed and we
  1460.                  * continue sending.    IDLETIME is an idle timeout in this case.
  1461.                  */    
  1462.                 if(scc->a.fulldup < 2){
  1463.                     scc->a.tstate = IDLE;
  1464.                     scc_txoff(scc);
  1465.  
  1466.                     if(scc->sndq != NULLBUF){
  1467.                         scc->a.tstate = DEFER;
  1468.                         scc->a.maxdefer = TPS * scc->a.idletime /
  1469.                          scc->a.slottime;
  1470.                         scc->timercount = TPS * scc->a.mintime;
  1471.                     }
  1472.                     break;
  1473.                 }
  1474.                 if(scc->sndq != NULLBUF){ /* still frames on the queue? */
  1475.                     scc->a.tstate = KEYWT; /* continue sending */
  1476.                     scc->timercount = TPS * scc->a.mintime; /* after mintime */
  1477.                 } else {
  1478.                     scc->a.tstate = IDLE;
  1479.                     scc->timercount = TPS * scc->a.idletime;
  1480.                 }
  1481.                 break;
  1482.             case ACTIVE:    /* max keyup time expired */
  1483.             case FLUSH:    /* same while in flush mode */
  1484.                 break;    /* no action required yet */
  1485.             default:            /* unexpected state */
  1486.                 scc->a.tstate = IDLE; /* that should not happen, but... */
  1487.                 scc_txoff(scc);        /* at least stop the transmitter */
  1488.                 break;
  1489.             }
  1490.         }
  1491.     }
  1492.     restore(i_state);
  1493. }
  1494.