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