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