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