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