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