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