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