Home | History | Annotate | Line # | Download | only in dev
msc.c revision 1.1
      1 /*	$NetBSD: msc.c,v 1.1 1995/09/30 01:50:50 chopps Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1993 Zik.
      5  * Copyright (c) 1995 Jukka Marin <jmarin (at) teeri.jmp.fi>.
      6  * Copyright (c) 1995 Timo Rossi <trossi (at) jyu.fi>.
      7  * Copyright (c) 1995 Rob Healey <rhealey (at) kas.helios.mn.org>.
      8  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  *
     39  *   - converted from NetBSD Amiga serial driver to A2232 serial driver
     40  *     by zik 931207
     41  *   - added ttyflags hooks rfh 940419
     42  *   - added new style config support rfh 940601
     43  *   - added code to halt board during memory load so board doesn't flip
     44  *     out. /dev/reload works now. Also created mschwiflow function so BSD can
     45  *     attempt to use board RTS flow control now. rfh 950108
     46  *   - Integrated work from Jukka Marin <jmarin (at) jmp.fi> and
     47  *     Timo Rossi <trossi (at) jyu.fi> The mscmint() code is Jukka's. 950916
     48  *     Integrated more bug fixes by Jukka Marin <jmarin (at) jmp.fi> 950918
     49  *     Also added Jukka's turbo board code. 950918
     50  *   - Reformatted to NetBSD style format.
     51  */
     52 
     53 #include "msc.h"
     54 
     55 #if NMSC > 0
     56 #include <sys/param.h>
     57 #include <sys/systm.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/tty.h>
     60 #include <sys/proc.h>
     61 #include <sys/conf.h>
     62 #include <sys/file.h>
     63 #include <sys/malloc.h>
     64 #include <sys/uio.h>
     65 #include <sys/kernel.h>
     66 #include <sys/syslog.h>
     67 #include <sys/device.h>
     68 
     69 #include <amiga/amiga/device.h>
     70 #include <amiga/dev/zbusvar.h>
     71 #include <amiga/dev/mscreg.h>
     72 #include "machine/cpu.h"
     73 
     74 #include <amiga/amiga/custom.h>
     75 #include <amiga/amiga/cia.h>
     76 #include <amiga/amiga/cc.h>
     77 
     78 /* 6502 code for A2232 card */
     79 #include "msc6502.h"
     80 
     81 /*
     82  * Note: These are Zik's original comments:
     83  * There is a bit of a "gotcha" concerning the numbering
     84  * of msc devices and the specification of the number of serial
     85  * lines in the kernel.
     86  *
     87  * Each board has seven lines, but for programming convenience
     88  * and compatibility with Amiga UNIX the boards' minor device
     89  * numbers are allocated in groups of sixteen:
     90  *
     91  *                     minor device numbers
     92  * First board		0  2  4  6  8 10 12
     93  * Second board        16 18 20 22 24 26 28
     94  * Third board	       32 34 36 38 40 42 44
     95  *
     96  * The intermediate minor device numbers are dialin versions
     97  * of the same devices. ie. 10 is dialout line 6 and 11 is
     98  * the dialin version of line 6.
     99  *
    100  * On the other hand, I have made the NMSC config option refer
    101  * to the total number of a2232 cards, not the maximum
    102  * minor device number. So you might have NMSC=3, in which case
    103  * you have three boards with minor device numbers from 0 to 45.
    104  */
    105 
    106 int	mscparam();
    107 int	mscstart __P((struct tty *));
    108 int	mschwiflow __P((struct tty *, int));
    109 int	mscinitcard __P((struct zbus_args *));
    110 
    111 int	mscdefaultrate = TTYDEF_SPEED;
    112 
    113 struct	mscdevice mscdev[MSCSLOTS];	/* device structs for all lines */
    114 struct	tty *msc_tty[MSCTTYS];		/* ttys for all lines */
    115 
    116 struct	vbl_node msc_vbl_node[NMSC];	/* vbl interrupt node per board */
    117 
    118 struct speedtab mscspeedtab_normal[] = {
    119 	0,	0,
    120 	50,	MSCPARAM_B50,
    121 	75,	MSCPARAM_B75,
    122 	110,	MSCPARAM_B110,
    123 	134,	MSCPARAM_B134,
    124 	150,	MSCPARAM_B150,
    125 	300,	MSCPARAM_B300,
    126 	600,	MSCPARAM_B600,
    127 	1200,	MSCPARAM_B1200,
    128 	1800,	MSCPARAM_B1800,
    129 	2400,	MSCPARAM_B2400,
    130 	3600,	MSCPARAM_B3600,
    131 	4800,	MSCPARAM_B4800,
    132 	7200,	MSCPARAM_B7200,
    133 	9600,	MSCPARAM_B9600,
    134 	19200,	MSCPARAM_B19200,
    135 	115200,	MSCPARAM_B115200,
    136 	-1,	-1
    137 };
    138 
    139 struct speedtab mscspeedtab_turbo[] = {
    140 	0,	0,
    141 	100,	MSCPARAM_B50,
    142 	150,	MSCPARAM_B75,
    143 	220,	MSCPARAM_B110,
    144 	269,	MSCPARAM_B134,
    145 	300,	MSCPARAM_B150,
    146 	600,	MSCPARAM_B300,
    147 	1200,	MSCPARAM_B600,
    148 	2400,	MSCPARAM_B1200,
    149 	3600,	MSCPARAM_B1800,
    150 	4800,	MSCPARAM_B2400,
    151 	7200,	MSCPARAM_B3600,
    152 	9600,	MSCPARAM_B4800,
    153 	14400,	MSCPARAM_B7200,
    154 	19200,	MSCPARAM_B9600,
    155 	38400,	MSCPARAM_B19200,
    156 	230400,	MSCPARAM_B115200,
    157 	-1,	-1
    158 };
    159 
    160 struct   speedtab *mscspeedtab;
    161 
    162 int mscmctl __P((dev_t dev, int bits, int howto));
    163 void mscmint __P((register void *data));
    164 
    165 int mscmatch __P((struct device *, struct cfdata *, void *));
    166 void mscattach __P((struct device *, struct device *, void *));
    167 
    168 #define	SWFLAGS(dev)	(msc->openflags | (MSCDIALIN(dev) ? 0 : TIOCFLAG_SOFTCAR))
    169 #define	DEBUG_MSC	0
    170 #define	DEBUG_CD	0
    171 
    172 struct cfdriver msccd = {
    173 	NULL, "msc", (cfmatch_t) mscmatch, mscattach, DV_TTY,
    174 	sizeof(struct device), NULL, 0
    175 };
    176 
    177 int
    178 mscmatch(pdp, cdp, auxp)
    179 	struct device *pdp;
    180 	struct cfdata *cdp;
    181 	void *auxp;
    182 {
    183 	struct zbus_args *zap;
    184 
    185 	zap = auxp;
    186 	if (zap->manid == 514 && (zap->prodid == 70 || zap->prodid == 69))
    187 		return(1);
    188 
    189 	return (0);
    190 }
    191 
    192 void
    193 mscattach(pdp, dp, auxp)
    194 	struct device *pdp, *dp;
    195 	void *auxp;
    196 {
    197   volatile struct mscmemory *mscmem;
    198   struct mscdevice *msc;
    199   struct zbus_args *zap;
    200   int unit;
    201   int Count;
    202   unsigned short ir = custom.intenar;	/* XXX */
    203 
    204   zap = (struct zbus_args *)auxp;
    205   unit = dp->dv_unit;
    206 
    207   if (mscinitcard(zap) != 0) {
    208     printf("\nmsc%d: Board initialize failed, bad download code.\n", unit);
    209     return;
    210   }
    211 
    212   printf("\nmsc%d: Board successfully initialized.\n", unit);
    213 
    214   mscmem = (struct mscmemory *) zap->va;
    215 
    216   if (mscmem->Common.Crystal == MSC_UNKNOWN) {
    217 	printf("msc%d: Unable to detect crystal frequency.\n", unit);
    218 	return;
    219   }
    220 
    221   if (mscmem->Common.Crystal == MSC_TURBO) {
    222 	printf("msc%d: Turbo version detected (%02x%02x:%d)\n", unit,
    223 		mscmem->Common.TimerH, mscmem->Common.TimerL,
    224 		mscmem->Common.Pad_a);
    225 	mscspeedtab = mscspeedtab_turbo;
    226   } else {
    227 	printf("msc%d: Normal version detected (%02x%02x:%d)\n", unit,
    228 		mscmem->Common.TimerH, mscmem->Common.TimerL,
    229 		mscmem->Common.Pad_a);
    230 	mscspeedtab = mscspeedtab_normal;
    231   }
    232 
    233   /* XXX 8 is a constant */
    234   for (Count = 0; Count < 8 && MSCSLOTUL(unit, Count) < MSCSLOTS; Count++) {
    235     msc = &mscdev[MSCSLOTUL(unit, Count)];
    236     msc->board = mscmem;
    237     msc->port = Count;
    238     msc->flags = 0;
    239     msc->openflags = 0;
    240     msc->active = 1;
    241     msc->closing = FALSE;
    242     msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, Count))] = NULL;
    243     msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, Count))+1] = NULL;
    244 
    245   }
    246 
    247   /* disable the non-existant eighth port */
    248   if (MSCSLOTUL(unit, NUMLINES) < MSCSLOTS)
    249     mscdev[MSCSLOTUL(unit, NUMLINES)].active = 0;
    250 
    251   msc_vbl_node[unit].function = (void (*) (void *)) mscmint;
    252   msc_vbl_node[unit].data = (void *) unit;
    253 
    254   add_vbl_function (&msc_vbl_node[unit], MSC_VBL_PRIORITY, (void *)unit);
    255 
    256   return;
    257 }
    258 
    259 /* ARGSUSED */
    260 int
    261 mscopen(dev, flag, mode, p)
    262 	dev_t dev;
    263 	int flag, mode;
    264 	struct proc *p;
    265 {
    266   register struct tty *tp;
    267   int error = 0;
    268   int s;
    269   int slot;
    270   int ttyn;
    271   struct mscdevice *msc;
    272   volatile struct mscstatus *ms;
    273 
    274   /* get the device structure */
    275   slot = MSCSLOT(dev);
    276   ttyn = MSCTTY(dev);
    277 
    278   if (slot >= MSCSLOTS)
    279     return ENXIO;
    280 
    281   if (MSCLINE(dev) >= NUMLINES)
    282     return ENXIO;
    283 
    284   msc = &mscdev[slot];
    285   ms = &msc->board->Status[msc->port];
    286 
    287   if (!msc->active)
    288     return ENXIO;
    289 
    290   /*
    291    * RFH: WHY here? Put down by while like other serial drivers
    292    *      But if we do that it makes things bomb.
    293    */
    294   s = spltty();
    295 
    296   if (!msc_tty[ttyn]) {
    297 
    298       tp = ttymalloc();
    299       msc_tty[ttyn] = tp;
    300       msc_tty[ttyn+1] = (struct tty *)NULL;
    301 
    302       /* default values are not optimal for this device, increase buffers. */
    303       clfree(&tp->t_rawq);
    304       clfree(&tp->t_canq);
    305       clfree(&tp->t_outq);
    306       clalloc(&tp->t_rawq, 8192, 1);
    307       clalloc(&tp->t_canq, 8192, 1);
    308       clalloc(&tp->t_outq, 8192, 0);
    309 
    310   }
    311   else
    312     tp = msc_tty[ttyn];
    313 
    314   tp->t_oproc = (void (*) (struct tty *)) mscstart;
    315   tp->t_param = mscparam;
    316   tp->t_dev = dev;
    317   tp->t_hwiflow = mschwiflow;
    318 
    319   /* if port is still closing, just bitbucket remaining characters */
    320   if (msc->closing) {
    321 
    322       ms->OutFlush = TRUE;
    323       msc->closing = FALSE;
    324   }
    325 
    326   /* initialize tty */
    327   if ((tp->t_state & TS_ISOPEN) == 0) {
    328 
    329       tp->t_state |= TS_WOPEN;
    330       ttychars(tp);
    331       if (tp->t_ispeed == 0) {
    332 
    333 	  tp->t_iflag = TTYDEF_IFLAG;
    334 	  tp->t_oflag = TTYDEF_OFLAG;
    335 	  tp->t_cflag = TTYDEF_CFLAG;
    336 	  tp->t_lflag = TTYDEF_LFLAG;
    337 	  tp->t_ispeed = tp->t_ospeed = mscdefaultrate;
    338       }
    339 
    340       /* flags changed to be private to every unit by JM */
    341       if (msc->openflags & TIOCFLAG_CLOCAL)
    342 		tp->t_cflag |= CLOCAL;
    343       if (msc->openflags & TIOCFLAG_CRTSCTS)
    344 		tp->t_cflag |= CRTSCTS;
    345       if (msc->openflags & TIOCFLAG_MDMBUF)
    346 		tp->t_cflag |= MDMBUF;
    347 
    348       mscparam(tp, &tp->t_termios);
    349       ttsetwater(tp);
    350 
    351       (void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMSET);
    352 
    353       if ((SWFLAGS(dev) & TIOCFLAG_SOFTCAR) ||
    354 	  (mscmctl(dev, 0, DMGET) & TIOCM_CD))
    355             tp->t_state |= TS_CARR_ON;
    356       else
    357             tp->t_state &= ~TS_CARR_ON;
    358 
    359   }
    360   else {
    361         if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
    362 	    splx(s);
    363 	    return (EBUSY);
    364        }
    365   }
    366 
    367   /*
    368    * if NONBLOCK requested, ignore carrier
    369    */
    370   if (flag & O_NONBLOCK)
    371     goto done;
    372 
    373   /*
    374    * s = spltty();
    375    *
    376    * This causes hangs when put here, like other TTY drivers do, rather than
    377    * above, WHY? RFH
    378    *
    379    */
    380 
    381   while ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_cflag & CLOCAL) == 0) {
    382 
    383       tp->t_state |= TS_WOPEN;
    384 
    385       error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH, ttopen, 0);
    386 
    387       if (error) {
    388                splx(s);
    389                return(error);
    390       }
    391   }
    392 
    393 #if DEBUG_CD
    394   if (tp->t_state & TS_CARR_ON) printf(" CARR_ON ");
    395   if (tp->t_cflag & CLOCAL) printf(" CLOCAL ");
    396 #endif
    397 
    398 done:
    399   /* This is a way to handle lost XON characters */
    400   if ((flag & O_TRUNC) && (tp->t_state & TS_TTSTOP)) {
    401           tp->t_state &= ~TS_TTSTOP;
    402           ttstart (tp);
    403   }
    404 
    405   splx(s);
    406 
    407   /*
    408    * Reset the tty pointer, as there could have been a dialout
    409    * use of the tty with a dialin open waiting.
    410    */
    411   tp->t_dev = dev;
    412 
    413   return((*linesw[tp->t_line].l_open)(dev, tp));
    414 
    415 }
    416 
    417 int
    418 mscclose(dev, flag, mode, p)
    419 	dev_t dev;
    420 	int flag, mode;
    421 	struct proc *p;
    422 {
    423   register struct tty *tp;
    424   int slot;
    425   volatile struct mscstatus *ms;
    426   struct mscdevice *msc;
    427 
    428   /* get the device structure */
    429   slot = MSCSLOT(dev);
    430 
    431   if (slot >= MSCSLOTS)
    432     return ENXIO;
    433 
    434   msc = &mscdev[slot];
    435 
    436   if (!msc->active)
    437     return ENXIO;
    438 
    439   ms = &msc->board->Status[msc->port];
    440 
    441   tp = msc_tty[MSCTTY(dev)];
    442   (*linesw[tp->t_line].l_close)(tp, flag);
    443 
    444   (void) mscmctl(dev, 0, DMSET);
    445 
    446   ttyclose(tp);
    447 
    448   if (msc->flags & TIOCM_DTR)
    449     msc->closing = TRUE; /* flush remaining characters before dropping DTR */
    450   else
    451     ms->OutFlush = TRUE; /* just bitbucket remaining characters */
    452 
    453   return (0);
    454 
    455 }
    456 
    457 int
    458 mscread(dev, uio, flag)
    459 	dev_t dev;
    460 	struct uio *uio;
    461 	int flag;
    462 {
    463 	register struct tty *tp;
    464 
    465 	tp = msc_tty[MSCTTY(dev)];
    466 
    467 	if (! tp)
    468 	 return ENXIO;
    469 
    470 	return((*linesw[tp->t_line].l_read)(tp, uio, flag));
    471 }
    472 
    473 int
    474 mscwrite(dev, uio, flag)
    475 	dev_t dev;
    476 	struct uio *uio;
    477 	int flag;
    478 {
    479   register struct tty *tp;
    480 
    481   tp = msc_tty[MSCTTY(dev)];
    482 
    483   if (! tp)
    484     return ENXIO;
    485 
    486   return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    487 }
    488 
    489 /*
    490  * This interrupt is periodically invoked in the vertical blank
    491  * interrupt. It's used to keep track of the modem control lines
    492  * and (new with the fast_int code) to move accumulated data up in
    493  * to the tty layer.
    494  *
    495  * NOTE: MSCCDHACK is an invention of mine for dubious purposes. If you
    496  *	 want to activate it add
    497  *	 options MSCCDHACK
    498  *	 in the kernel conf file. Basically it forces CD->Low transitions
    499  *	 to ALWAYS send a signal to the process, even if the device is in
    500  *	 clocal mode or an outdial device. RFH
    501  */
    502 void
    503 mscmint (data)
    504 	register void *data;
    505 {
    506   int unit;
    507   register struct tty *tp;
    508   int slot;
    509   int maxslot;
    510   struct mscdevice *msc;
    511   volatile struct mscstatus *ms;
    512   volatile u_char *ibuf, *cbuf;
    513   unsigned char newhead; /* was int */
    514   unsigned char bufpos;  /* was int */
    515   int s;
    516 
    517   unit = (int) data;
    518 
    519   /* check each line on this board */
    520   maxslot = MSCSLOTUL(unit, NUMLINES);
    521   if (maxslot > MSCSLOTS)
    522     maxslot = MSCSLOTS;
    523 
    524   for (slot = MSCSLOTUL(unit, 0); slot < maxslot; slot++)
    525     {
    526       msc = &mscdev[slot];
    527 
    528       if (!msc->active)
    529         continue;
    530 
    531       tp = msc_tty[MSCTTYSLOT(slot)];
    532       ms = &msc->board->Status[msc->port];
    533 
    534       newhead = ms->InHead;		/* 65c02 write pointer */
    535 
    536       /* yoohoo, is the port open? */
    537       if (tp && (tp->t_state & (TS_ISOPEN|TS_WOPEN))) {
    538 	/* port is open, handle all type of events */
    539 
    540 	/* set interrupt priority level */
    541         s = spltty();
    542 
    543       /* check for input for this port */
    544       if (newhead != (bufpos = ms->InTail))
    545       {
    546 #if DEBUG_MSC
    547 	      printf("iop%d\n",slot);
    548 #endif
    549 	      /* buffer for input chars/events */
    550 	      ibuf = &msc->board->InBuf[msc->port][0];
    551 
    552 	      /* data types of bytes in ibuf */
    553 	      cbuf = &msc->board->InCtl[msc->port][0];
    554 
    555 	      /* do for all chars, if room */
    556 	      while (bufpos != newhead)
    557 	      {
    558 		  /* which type of input data? */
    559 		  switch (cbuf[bufpos])
    560 		  {
    561 		      /* input event (CD, BREAK, etc.) */
    562 		      case MSCINCTL_EVENT:
    563 			switch (ibuf[bufpos++])
    564 			{
    565 			    /* carrier detect change OFF -> ON */
    566 			    case MSCEVENT_CarrierOn:
    567 #if DEBUG_CD
    568 			      printf("Carrier detected on msc%d\n", slot);
    569 #endif
    570 			      msc->flags |= TIOCM_CD;
    571 			      if (tp && (tp->t_state & (TS_ISOPEN|TS_WOPEN))
    572 				     && MSCDIALIN(tp->t_dev))
    573 				(*linesw[tp->t_line].l_modem)(tp, 1);
    574 			      break;
    575 
    576 			    /*  carrier detect change ON -> OFF */
    577 			    case MSCEVENT_CarrierOff:
    578 #if DEBUG_CD
    579 			      printf("Carrier lost on msc%d\n", slot);
    580 #endif
    581 			      msc->flags &= ~TIOCM_CD;
    582 			      /* if (tp && (tp->t_state & (TS_ISOPEN|TS_WOPEN)) && MSCDIALIN(tp->t_dev)) */
    583 #ifndef MSCCDHACK
    584 			      if (tp && MSCDIALIN(tp->t_dev))
    585 #else
    586 			      if (tp )
    587 #endif
    588 			      {
    589 				  if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
    590 				  {
    591 				      /* clear RTS and DTR, bitbucket output */
    592 				      ms->Command = (ms->Command & ~MSCCMD_CMask) | MSCCMD_Close;
    593 				      ms->Setup = TRUE;
    594 				      msc->flags &= ~(TIOCM_DTR | TIOCM_RTS);
    595 				      ms->OutFlush = TRUE;
    596 				  }
    597 			      }
    598 			      break;
    599 
    600 			    case MSCEVENT_Break:
    601 #if DEBUG_MSC
    602 			      printf("Break received on msc%d\n", slot);
    603 #endif
    604 			      (*linesw[tp->t_line].l_rint)(TTY_FE, tp);
    605 			      break;
    606 
    607 			    default:
    608 			      printf("msc: unknown event type %d\n",
    609 				      ibuf[(bufpos-1)&0xff]);
    610 
    611 			} /* event type switch */
    612 			break;
    613 
    614 		      case MSCINCTL_CHAR:
    615 			 if (tp->t_state & TS_TBLOCK)
    616 			   goto NoRoomForYa;
    617 #if DEBUG_MSC
    618 			 printf("'%c' ",ibuf[bufpos]);
    619 #endif
    620 			 (*linesw[tp->t_line].l_rint)((int)ibuf[bufpos++], tp);
    621 			break;
    622 
    623 		      default:
    624 			printf("msc: unknown data type %d\n", cbuf[bufpos]);
    625 			bufpos++;
    626 
    627 		   } /* switch on input data type */
    628 
    629 		} /* while there's something in the buffer */
    630 NoRoomForYa:
    631 	      ms->InTail = bufpos;		/* tell 65C02 what we've read */
    632 
    633 	    } /* if there was something in the buffer */
    634 
    635 	  /* we get here only when the port is open */
    636 	  /* send output */
    637 	   if (tp->t_state & (TS_BUSY|TS_FLUSH))
    638 	    {
    639 
    640 	      bufpos = ms->OutHead - ms->OutTail;
    641 
    642 	      /* busy and below low water mark? */
    643 	      if (tp->t_state & TS_BUSY)
    644 	        {
    645 	          if (bufpos < IOBUFLOWWATER)
    646 		    {
    647 		      tp->t_state &= ~TS_BUSY;	/* not busy any more */
    648 		      if (tp->t_line)
    649 		        (*linesw[tp->t_line].l_start)(tp);
    650 		      else
    651 		        mscstart(tp);
    652 		    }
    653 		}
    654 
    655 	      /* waiting for flush and buffer empty? */
    656 	      if (tp->t_state & TS_FLUSH)
    657 	        {
    658 	          if (bufpos == 0)
    659 	            tp->t_state &= ~TS_FLUSH;	/* finished flushing */
    660 	        }
    661 	    } /* BUSY or FLUSH */
    662 
    663 	     splx(s);
    664 
    665       } else { /* End of port open */
    666 	/* port is closed, don't pass on the chars from it */
    667 
    668       /* check for input for this port */
    669       if (newhead != (bufpos = ms->InTail))
    670       {
    671 #if DEBUG_MSC
    672 	      printf("icp%d\n",slot);
    673 #endif
    674 	      /* buffer for input chars/events */
    675 	      ibuf = &msc->board->InBuf[msc->port][0];
    676 
    677 	      /* data types of bytes in ibuf */
    678 	      cbuf = &msc->board->InCtl[msc->port][0];
    679 
    680 	      /* do for all chars, if room */
    681 	      while (bufpos != newhead)
    682 	      {
    683 		  /* which type of input data? */
    684 		  switch (cbuf[bufpos])
    685 		  {
    686 		      /* input event (CD, BREAK, etc.) */
    687 		      case MSCINCTL_EVENT:
    688 			switch (ibuf[bufpos++])
    689 			{
    690 			    /* carrier detect change OFF -> ON */
    691 			    case MSCEVENT_CarrierOn:
    692 #if DEBUG_CD
    693 			      printf("Carrier detected on msc%d\n", slot);
    694 #endif
    695 			      msc->flags |= TIOCM_CD;
    696 			      if (tp && (tp->t_state & (TS_ISOPEN|TS_WOPEN))
    697 				     && MSCDIALIN(tp->t_dev))
    698 				(*linesw[tp->t_line].l_modem)(tp, 1);
    699 			      break;
    700 
    701 			    /*  carrier detect change ON -> OFF */
    702 			    case MSCEVENT_CarrierOff:
    703 #if DEBUG_CD
    704 			      printf("Carrier lost on msc%d\n", slot);
    705 #endif
    706 			      msc->flags &= ~TIOCM_CD;
    707 			      /* if (tp && (tp->t_state & (TS_ISOPEN|TS_WOPEN)) && MSCDIALIN(tp->t_dev)) */
    708 #ifndef MSCCDHACK
    709 			      if (tp && MSCDIALIN(tp->t_dev))
    710 #else
    711 			      if (tp )
    712 #endif
    713 			      {
    714 				  if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
    715 				  {
    716 				      /* clear RTS and DTR, bitbucket output */
    717 				      ms->Command = (ms->Command & ~MSCCMD_CMask) | MSCCMD_Close;
    718 				      ms->Setup = TRUE;
    719 				      msc->flags &= ~(TIOCM_DTR | TIOCM_RTS);
    720 				      ms->OutFlush = TRUE;
    721 				  }
    722 			      }
    723 			      break;
    724 
    725 			    case MSCEVENT_Break:
    726 #if DEBUG_MSC
    727 			      printf("Break received on msc%d\n", slot);
    728 #endif
    729 			      (*linesw[tp->t_line].l_rint)(TTY_FE, tp);
    730 			      break;
    731 
    732 			    default:
    733 			      printf("msc: unknown event type %d\n",
    734 				     ibuf[(bufpos-1)&0xff]);
    735 
    736 			} /* event type switch */
    737 			break;
    738 
    739 		      default:
    740 			;
    741 
    742 		   } /* switch on input data type */
    743 
    744 		} /* while there's something in the buffer */
    745 
    746 	        ms->InTail = bufpos;		/* tell 65C02 what we've read */
    747 
    748 	    } /* if there was something in the buffer */
    749       } /* End of port open/close */
    750 
    751       /* is this port closing? */
    752       if (msc->closing)
    753         {
    754 	  /* if DTR is off, just bitbucket remaining characters */
    755 	  if ( (msc->flags & TIOCM_DTR) == 0)
    756 	    {
    757 	      ms->OutFlush = TRUE;
    758 	      msc->closing = FALSE;
    759 	    }
    760 	  /* if output has drained, drop DTR */
    761           else if (ms->OutHead == ms->OutTail)
    762 	    {
    763 	      (void) mscmctl(tp->t_dev, 0, DMSET);
    764 	      msc->closing = FALSE;
    765             }
    766         }
    767     }  /* For all ports */
    768 
    769 }
    770 
    771 int
    772 mscioctl(dev, cmd, data, flag, p)
    773 	dev_t dev;
    774 	int cmd;
    775 	caddr_t data;
    776 	int flag;
    777 	struct proc *p;
    778 {
    779   register struct tty *tp;
    780   register int slot;
    781   register int error;
    782   struct mscdevice *msc;
    783   volatile struct mscstatus *ms;
    784   int s;
    785 
    786   /* get the device structure */
    787   slot = MSCSLOT(dev);
    788 
    789   if (slot >= MSCSLOTS)
    790     return ENXIO;
    791 
    792   msc = &mscdev[slot];
    793 
    794   if (!msc->active)
    795     return ENXIO;
    796 
    797   ms = &msc->board->Status[msc->port];
    798   tp = msc_tty[MSCTTY(dev)];
    799 
    800   error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    801 
    802   if (error >= 0)
    803     return (error);
    804 
    805   error = ttioctl(tp, cmd, data, flag, p);
    806 
    807   if (error >= 0)
    808     return (error);
    809 
    810   switch (cmd) {
    811 
    812       /* send break */
    813     case TIOCSBRK:
    814       s = spltty();
    815       ms->Command = (ms->Command & (~MSCCMD_RTSMask)) | MSCCMD_Break;
    816       ms->Setup = TRUE;
    817       splx(s);
    818       break;
    819 
    820       /* clear break */
    821     case TIOCCBRK:
    822       s = spltty();
    823       ms->Command = (ms->Command & (~MSCCMD_RTSMask)) | MSCCMD_RTSOn;
    824       ms->Setup = TRUE;
    825       splx(s);
    826       break;
    827 
    828     case TIOCSDTR:
    829       (void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIS);
    830       break;
    831 
    832     case TIOCCDTR:
    833       if (!MSCDIALIN(dev))	/* don't let dialins drop DTR */
    834         (void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIC);
    835       break;
    836 
    837     case TIOCMSET:
    838       (void) mscmctl(dev, *(int *)data, DMSET);
    839       break;
    840 
    841     case TIOCMBIS:
    842       (void) mscmctl(dev, *(int *)data, DMBIS);
    843       break;
    844 
    845     case TIOCMBIC:
    846       if (MSCDIALIN(dev))	/* don't let dialins drop DTR */
    847         (void) mscmctl(dev, *(int *)data & TIOCM_DTR, DMBIC);
    848       else
    849         (void) mscmctl(dev, *(int *)data, DMBIC);
    850       break;
    851 
    852     case TIOCMGET:
    853       *(int *)data = mscmctl(dev, 0, DMGET);
    854       break;
    855 
    856     case TIOCGFLAGS:
    857       *(int *)data = SWFLAGS(dev);
    858       break;
    859 
    860     case TIOCSFLAGS:
    861       error = suser(p->p_ucred, &p->p_acflag);
    862       if (error != 0)
    863               return(EPERM);
    864 
    865       msc->openflags = *(int *)data;
    866 
    867       /* only allow valid flags */
    868       msc->openflags &= (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | TIOCFLAG_CRTSCTS);
    869 
    870       break;
    871 
    872     default:
    873       return (ENOTTY);
    874     }
    875 
    876   return (0);
    877 }
    878 
    879 
    880 int
    881 mscparam(tp, t)
    882 	register struct tty *tp;
    883 	register struct termios *t;
    884 {
    885   register int cfcr, cflag = t->c_cflag;
    886   int slot;
    887   struct mscdevice *msc;
    888   volatile struct mscstatus *ms;
    889   int s;
    890   int ospeed = ttspeedtab(t->c_ospeed, mscspeedtab);
    891 
    892   /* get the device structure */
    893   slot = MSCSLOT(tp->t_dev);
    894 
    895   if (slot >= MSCSLOTS)
    896     return ENXIO;
    897 
    898   msc = &mscdev[slot];
    899 
    900   if (!msc->active)
    901     return ENXIO;
    902 
    903   ms = &msc->board->Status[msc->port];
    904 
    905   /* check requested parameters */
    906   if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
    907     return (EINVAL);
    908 
    909   /* and copy to tty */
    910   tp->t_ispeed = t->c_ispeed;
    911   tp->t_ospeed = t->c_ospeed;
    912   tp->t_cflag = cflag;
    913 
    914   /* hang up if baud is zero */
    915   if (t->c_ospeed == 0) {
    916 
    917       if (!MSCDIALIN(tp->t_dev))  /* don't let dialins drop DTR */
    918         (void) mscmctl(tp->t_dev, 0, DMSET);
    919   }
    920   else {
    921 
    922       /* set the baud rate */
    923       s = spltty();
    924       ms->Param = (ms->Param & ~MSCPARAM_BaudMask) | ospeed | MSCPARAM_RcvBaud;
    925 
    926       /* make sure any previous hangup is undone, ie.  reenable DTR.
    927        * also mscmctl will cause the speed to be set
    928        */
    929       (void) mscmctl (tp->t_dev, TIOCM_DTR | TIOCM_RTS, DMSET);
    930       splx(s);
    931   }
    932 
    933   return (0);
    934 
    935 }
    936 
    937 
    938 /*
    939  *	Jukka's code initializes alot of stuff that other drivers don't
    940  *	I'm including it here so that this code is a common set of work
    941  *	done by both of us. rfh
    942  */
    943 int
    944 mschwiflow(tp, flag)
    945 	struct tty *tp;
    946 	int flag;
    947 {
    948 
    949 /* Rob's version */
    950 #if 1
    951 #if DEBUG_MSC
    952 	printf("mschwiflow %d\n", flag);
    953 #endif
    954 
    955 	if (flag)
    956 	   mscmctl( tp->t_dev, TIOCM_RTS, DMBIC); /* Clear/Lower RTS */
    957 	else
    958 	   mscmctl( tp->t_dev, TIOCM_RTS, DMBIS); /* Set/Raise RTS */
    959 
    960 #endif
    961 
    962 /* Jukka's version */
    963 #if 0
    964   int slot;
    965   struct mscdevice *msc;
    966   volatile struct mscstatus *ms;
    967   int s;
    968 
    969   /* get the device structure */
    970   slot = MSCSLOT(tp->t_dev);
    971   if (slot >= MSCSLOTS)
    972     return ENXIO;
    973   msc = &mscdev[slot];
    974   if (!msc->active)
    975     return ENXIO;
    976   ms = &msc->board->Status[msc->port];
    977 
    978   /* Well, we should really _do_ something here, but the 65c02 code
    979    * manages the RTS signal on its own now, so...  This will probably
    980    * change in the future.
    981    */
    982 
    983 #endif
    984 	return 1;
    985 
    986 }
    987 
    988 int
    989 mscstart(tp)
    990 	register struct tty *tp;
    991 {
    992   register int cc;
    993   register char *cp;
    994   register int mhead;
    995   int s;
    996   int slot;
    997   struct mscdevice *msc;
    998   volatile struct mscstatus *ms;
    999   volatile char *mob;
   1000   int hiwat = 0;
   1001   int maxout;
   1002 
   1003   if (! (tp->t_state & TS_ISOPEN))
   1004     return;
   1005 
   1006   slot = MSCSLOT(tp->t_dev);
   1007 
   1008 #if 0
   1009   printf("starting msc%d\n", slot);
   1010 #endif
   1011 
   1012   s = spltty();
   1013 
   1014 #if 0
   1015   /* don't start if explicitly stopped */
   1016   if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP))
   1017     goto out;
   1018 #endif
   1019 
   1020   /* wake up if below low water */
   1021   cc = tp->t_outq.c_cc;
   1022 
   1023   if (cc <= tp->t_lowat) {
   1024       if (tp->t_state & TS_ASLEEP) {
   1025 
   1026 	  tp->t_state &= ~TS_ASLEEP;
   1027 	  wakeup((caddr_t)&tp->t_outq);
   1028 	}
   1029 
   1030       selwakeup(&tp->t_wsel);
   1031   }
   1032 
   1033   /* don't bother if no characters or busy */
   1034   if (! cc || (tp->t_state & TS_BUSY))
   1035     goto out;
   1036 
   1037   /*
   1038    * Limit the amount of output we do in one burst
   1039    */
   1040   msc = &mscdev[slot];
   1041   ms = &msc->board->Status[msc->port];
   1042   mhead = ms->OutHead;
   1043   maxout = mhead - ms->OutTail;
   1044 
   1045   if (maxout < 0)
   1046       maxout += IOBUFLEN;
   1047 
   1048   maxout = IOBUFLEN - 1 - maxout;
   1049 
   1050   if (cc >= maxout) {
   1051       hiwat++;
   1052       cc = maxout;
   1053   }
   1054 
   1055   cc = q_to_b (&tp->t_outq, msc->tmpbuf, cc);
   1056 
   1057   if (cc > 0) {
   1058       tp->t_state |= TS_BUSY;
   1059 
   1060       mob = &msc->board->OutBuf[msc->port][0];
   1061       cp = &msc->tmpbuf[0];
   1062 
   1063       /* enable output */
   1064       ms->OutDisable = FALSE;
   1065 
   1066 #if 0
   1067       msc->tmpbuf[cc] = 0;
   1068       printf("sending '%s'\n", msctmpbuf);
   1069 #endif
   1070 
   1071       /* send the first char across to reduce latency */
   1072       mob[mhead++] = *cp++;
   1073       mhead &= IOBUFLENMASK;
   1074       ms->OutHead = mhead;
   1075       cc--;
   1076 
   1077       /* copy the rest of the chars across quickly */
   1078       while (cc > 0) {
   1079 	  mob[mhead++] = *cp++;
   1080 	  mhead &= IOBUFLENMASK;
   1081 	  cc--;
   1082       }
   1083       ms->OutHead = mhead;
   1084 
   1085       /* leave the device busy if we've filled the buffer */
   1086       if (!hiwat)
   1087         tp->t_state &= ~TS_BUSY;
   1088     }
   1089 
   1090 out:
   1091   splx(s);
   1092 
   1093 }
   1094 
   1095 /* XXX */
   1096 /*
   1097  * Stop output on a line.
   1098  */
   1099 /*ARGSUSED*/
   1100 int
   1101 mscstop(tp, flag)
   1102 	register struct tty *tp;
   1103 	int flag;			/* defaulted to int anyway */
   1104 {
   1105 	register int s;
   1106 	struct mscdevice *msc;
   1107 	volatile struct mscstatus *ms;
   1108 
   1109 	s = spltty();
   1110 	if (tp->t_state & TS_BUSY) {
   1111 		if (tp->t_state & TS_TTSTOP == 0) {
   1112 			tp->t_state |= TS_FLUSH;
   1113 #if 0
   1114 			msc = &mscdev[MSCSLOT(tp->t_dev)];
   1115 			ms = &msc->board->Status[msc->port];
   1116 			printf("stopped output on msc%d\n", MSCSLOT(tp->t_dev));
   1117 			ms->OutDisable = TRUE;
   1118 #endif
   1119 		}
   1120 	}
   1121 	splx(s);
   1122 }
   1123 
   1124 /*
   1125  * bits can be: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, TIOCM_CD, TIOCM_RI, TIOCM_DSR
   1126  */
   1127 int
   1128 mscmctl(dev, bits, how)
   1129 	dev_t dev;
   1130 	int bits, how;
   1131 {
   1132   struct mscdevice *msc;
   1133   volatile struct mscstatus *ms;
   1134   int slot;
   1135   int s;
   1136   u_char newcmd;
   1137   int OldFlags;
   1138 
   1139   /* get the device structure */
   1140   slot = MSCSLOT(dev);
   1141 
   1142   if (slot >= MSCSLOTS)
   1143     return ENXIO;
   1144 
   1145   msc = &mscdev[slot];
   1146 
   1147   if (!msc->active)
   1148     return ENXIO;
   1149 
   1150   s = spltty();
   1151 
   1152   if (how != DMGET) {
   1153       OldFlags = msc->flags;
   1154       bits &= TIOCM_DTR | TIOCM_RTS; /* can only modify DTR and RTS */
   1155 
   1156       switch (how) {
   1157         case DMSET:
   1158 	  msc->flags = (bits | (msc->flags & ~(TIOCM_DTR | TIOCM_RTS)));
   1159           break;
   1160 
   1161         case DMBIC:
   1162           msc->flags &= ~bits;
   1163           break;
   1164 
   1165         case DMBIS:
   1166           msc->flags |= bits;
   1167           break;
   1168       }
   1169 
   1170       /* modify modem control state */
   1171       ms = &msc->board->Status[msc->port];
   1172 
   1173       if (msc->flags & TIOCM_RTS)	/* was bits & */
   1174 	newcmd = MSCCMD_RTSOn;
   1175       else			/* this doesn't actually work now */
   1176 	newcmd = MSCCMD_RTSOff;
   1177 
   1178       if (msc->flags & TIOCM_DTR)	/* was bits & */
   1179 	newcmd |= MSCCMD_Enable;
   1180 
   1181       ms->Command = (ms->Command & (~MSCCMD_RTSMask & ~MSCCMD_Enable)) | newcmd;
   1182       ms->Setup = TRUE;
   1183 
   1184       /* if we've dropped DTR, bitbucket any pending output */
   1185       if ( (OldFlags & TIOCM_DTR) && ((bits & TIOCM_DTR) == 0))
   1186         ms->OutFlush = TRUE;
   1187   }
   1188 
   1189   bits = msc->flags;
   1190 
   1191   (void) splx(s);
   1192 
   1193   return(bits);
   1194 
   1195 }
   1196 
   1197 struct tty *
   1198 msctty(dev)
   1199 	dev_t dev;
   1200 {
   1201 	return(msc_tty[MSCTTY(dev)]);
   1202 }
   1203 
   1204 /*
   1205  * Load JM's freely redistributable A2232 6502c code. Let turbo detector
   1206  * run for a while too.
   1207  */
   1208 
   1209 int
   1210 mscinitcard(zap)
   1211 	struct zbus_args *zap;
   1212 {
   1213   int bcount;
   1214   short start;
   1215   u_char *from;
   1216   volatile u_char *to;
   1217   volatile struct mscmemory *mlm;
   1218 
   1219   mlm = (volatile struct mscmemory *)zap->va;
   1220   (void)mlm->Enable6502Reset;
   1221 
   1222   /* copy the code across to the board */
   1223   to = (u_char *)mlm;
   1224   from = msc6502code; bcount = sizeof(msc6502code) - 2;
   1225   start = *(short *)from; from += sizeof(start);
   1226   to += start;
   1227 
   1228 #if DEBUG_MSC
   1229   printf("\n** copying %ld bytes from %08lx to %08lx (start=%04lx)\n",
   1230 	  (unsigned long)bcount, (unsigned long)from, to, start);
   1231   printf("First byte to copy is %02lx\n", *from);
   1232 #endif
   1233 
   1234   while(bcount--) *to++ = *from++;
   1235 
   1236   mlm->Common.Crystal = MSC_UNKNOWN;	/* use automatic speed check */
   1237 
   1238   /* start it running */
   1239   (void)mlm->ResetBoard;
   1240 
   1241   /* wait until speed detector has finished */
   1242   for (bcount = 0; bcount < 200; bcount++) {
   1243 	delay(10000);
   1244 	if (mlm->Common.Crystal) break;
   1245   }
   1246 
   1247   return(0);
   1248 
   1249 }
   1250 
   1251 #endif  /* NMSC > 0 */
   1252