xref: /netbsd-src/sys/arch/amiga/dev/msc.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: msc.c,v 1.31 2005/12/11 12:16:28 christos 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.31 2005/12/11 12:16:28 christos 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 
114 #include <amiga/amiga/device.h>
115 #include <amiga/dev/zbusvar.h>
116 #include <amiga/dev/mscreg.h>
117 #include <machine/cpu.h>
118 
119 #include <amiga/amiga/custom.h>
120 #include <amiga/amiga/cia.h>
121 #include <amiga/amiga/cc.h>
122 
123 /* 6502 code for A2232 card */
124 #include "msc6502.h"
125 
126 /*
127  * Note: These are Zik's original comments:
128  * There is a bit of a "gotcha" concerning the numbering
129  * of msc devices and the specification of the number of serial
130  * lines in the kernel.
131  *
132  * Each board has seven lines, but for programming convenience
133  * and compatibility with Amiga UNIX the boards' minor device
134  * numbers are allocated in groups of sixteen:
135  *
136  *                     minor device numbers
137  * First board		0  2  4  6  8 10 12
138  * Second board        16 18 20 22 24 26 28
139  * Third board	       32 34 36 38 40 42 44
140  *
141  * The intermediate minor device numbers are dialin versions
142  * of the same devices. ie. 10 is dialout line 6 and 11 is
143  * the dialin version of line 6.
144  *
145  * On the other hand, I have made the NMSC config option refer
146  * to the total number of a2232 cards, not the maximum
147  * minor device number. So you might have NMSC=3, in which case
148  * you have three boards with minor device numbers from 0 to 45.
149  */
150 
151 int	mscparam(struct tty *, struct termios *);
152 void	mscstart(struct tty *);
153 int	mschwiflow(struct tty *, int);
154 int	mscinitcard(struct zbus_args *);
155 
156 int	mscdefaultrate = TTYDEF_SPEED;
157 
158 struct	mscdevice mscdev[MSCSLOTS];	/* device structs for all lines */
159 struct	tty *msc_tty[MSCTTYS];		/* ttys for all lines */
160 
161 struct	vbl_node msc_vbl_node[NMSC];	/* vbl interrupt node per board */
162 
163 const struct speedtab mscspeedtab_normal[] = {
164 	{ 0,		0		},
165 	{ 50,		MSCPARAM_B50	},
166 	{ 75,		MSCPARAM_B75	},
167 	{ 110,		MSCPARAM_B110	},
168 	{ 134,		MSCPARAM_B134	},
169 	{ 150,		MSCPARAM_B150	},
170 	{ 300,		MSCPARAM_B300	},
171 	{ 600,		MSCPARAM_B600	},
172 	{ 1200,		MSCPARAM_B1200	},
173 	{ 1800,		MSCPARAM_B1800	},
174 	{ 2400,		MSCPARAM_B2400	},
175 	{ 3600,		MSCPARAM_B3600	},
176 	{ 4800,		MSCPARAM_B4800	},
177 	{ 7200,		MSCPARAM_B7200	},
178 	{ 9600,		MSCPARAM_B9600	},
179 	{ 19200,	MSCPARAM_B19200	},
180 	{ 115200,	MSCPARAM_B115200 },
181 	{ -1,		-1		}
182 };
183 
184 const struct speedtab mscspeedtab_turbo[] = {
185 	{ 0,		0		},
186 	{ 100,		MSCPARAM_B50	},
187 	{ 150,		MSCPARAM_B75	},
188 	{ 220,		MSCPARAM_B110	},
189 	{ 269,		MSCPARAM_B134	},
190 	{ 300,		MSCPARAM_B150	},
191 	{ 600,		MSCPARAM_B300	},
192 	{ 1200,		MSCPARAM_B600	},
193 	{ 2400,		MSCPARAM_B1200	},
194 	{ 3600,		MSCPARAM_B1800	},
195 	{ 4800,		MSCPARAM_B2400	},
196 	{ 7200,		MSCPARAM_B3600	},
197 	{ 9600,		MSCPARAM_B4800	},
198 	{ 14400,	MSCPARAM_B7200	},
199 	{ 19200,	MSCPARAM_B9600	},
200 	{ 38400,	MSCPARAM_B19200	},
201 	{ 230400,	MSCPARAM_B115200 },
202 	{ -1,		-1		}
203 };
204 
205 const struct   speedtab *mscspeedtab;
206 
207 int mscmctl(dev_t dev, int bits, int howto);
208 void mscmint(register void *data);
209 
210 int mscmatch(struct device *, struct cfdata *, void *);
211 void mscattach(struct device *, struct device *, void *);
212 
213 #define	SWFLAGS(dev)	(msc->openflags | (MSCDIALIN(dev) ? 0 : TIOCFLAG_SOFTCAR))
214 #define	DEBUG_CD	0
215 
216 CFATTACH_DECL(msc, sizeof(struct device),
217     mscmatch, mscattach, NULL, NULL);
218 
219 dev_type_open(mscopen);
220 dev_type_close(mscclose);
221 dev_type_read(mscread);
222 dev_type_write(mscwrite);
223 dev_type_ioctl(mscioctl);
224 dev_type_stop(mscstop);
225 dev_type_tty(msctty);
226 dev_type_poll(mscpoll);
227 
228 const struct cdevsw msc_cdevsw = {
229 	mscopen, mscclose, mscread, mscwrite, mscioctl,
230 	mscstop, msctty, mscpoll, nommap, ttykqfilter, D_TTY
231 };
232 
233 int
234 mscmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
235 {
236 	struct zbus_args *zap;
237 
238 	zap = auxp;
239 	if (zap->manid == 514 && (zap->prodid == 70 || zap->prodid == 69))
240 		return(1);
241 
242 	return (0);
243 }
244 
245 void
246 mscattach(struct device *pdp, struct device *dp, void *auxp)
247 {
248 	volatile struct mscmemory *mscmem;
249 	struct mscdevice *msc;
250 	struct zbus_args *zap;
251 	int unit;
252 	int Count;
253 
254 	zap = (struct zbus_args *)auxp;
255 	unit = dp->dv_unit;
256 
257 	/*
258 	 * Make config msgs look nicer.
259 	 */
260 	printf("\n");
261 
262 	if (mscinitcard(zap) != 0) {
263 		printf("msc%d: Board initialize failed, bad download code.\n", unit);
264 		return;
265 	}
266 
267 	printf("msc%d: Board successfully initialized.\n", unit);
268 
269 	mscmem = (struct mscmemory *) zap->va;
270 
271 	if (mscmem->Common.Crystal == MSC_UNKNOWN) {
272 		printf("msc%d: Unable to detect crystal frequency.\n", unit);
273 		return;
274 	}
275 
276 	if (mscmem->Common.Crystal == MSC_TURBO) {
277 		printf("msc%d: Turbo version detected (%02x%02x:%d)\n", unit,
278 			mscmem->Common.TimerH, mscmem->Common.TimerL,
279 			mscmem->Common.Pad_a);
280 		mscspeedtab = mscspeedtab_turbo;
281 	} else {
282 		printf("msc%d: Normal version detected (%02x%02x:%d)\n", unit,
283 			mscmem->Common.TimerH, mscmem->Common.TimerL,
284 			mscmem->Common.Pad_a);
285 		mscspeedtab = mscspeedtab_normal;
286 	}
287 
288 	mscmem->Common.CDStatus = 0;	/* common status for all 7 ports */
289 
290 	/* XXX 8 is a constant */
291 	for (Count = 0; Count < 8 && MSCSLOTUL(unit, Count) < MSCSLOTS; Count++) {
292 		msc = &mscdev[MSCSLOTUL(unit, Count)];
293 		msc->board = mscmem;
294 		msc->port = Count;
295 		msc->flags = 0;
296 		msc->openflags = 0;
297 		msc->active = 1;
298 		msc->unit = unit;
299 		msc->closing = FALSE;
300 		msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, Count))] = NULL;
301 		msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, Count)) + 1] = NULL;
302 	}
303 
304 	/* disable the non-existent eighth port */
305 	if (MSCSLOTUL(unit, NUMLINES) < MSCSLOTS)
306 		mscdev[MSCSLOTUL(unit, NUMLINES)].active = 0;
307 
308 	msc_vbl_node[unit].function = (void (*) (void *)) mscmint;
309 	msc_vbl_node[unit].data = (void *) unit;
310 
311 	add_vbl_function (&msc_vbl_node[unit], MSC_VBL_PRIORITY, (void *)unit);
312 
313 	return;
314 }
315 
316 /* ARGSUSED */
317 int
318 mscopen(dev_t dev, int flag, int mode, struct lwp *l)
319 {
320 	register struct tty *tp;
321 	struct mscdevice *msc;
322 	volatile struct mscstatus *ms;
323 	int error = 0;
324 	int s, slot, ttyn;
325 
326 	/* get the device structure */
327 	slot = MSCSLOT(dev);
328 	ttyn = MSCTTY(dev);
329 
330 	if (slot >= MSCSLOTS)
331 		return ENXIO;
332 
333 	if (MSCLINE(dev) >= NUMLINES)
334 		return ENXIO;
335 
336 	msc = &mscdev[slot];
337 	ms = &msc->board->Status[msc->port];
338 
339 	if (!msc->active)
340 		return ENXIO;
341 
342 	/*
343 	 * RFH: WHY here? Put down by while like other serial drivers
344 	 *      But if we do that it makes things bomb.
345 	 */
346 	s = spltty();
347 
348 	if (!msc_tty[ttyn]) {
349 
350 		tp = ttymalloc();
351 		tty_attach(tp);
352 		msc_tty[ttyn] = tp;
353 		msc_tty[ttyn+1] = (struct tty *)NULL;
354 
355 #if 0
356 		/* default values are not optimal for this device, increase buffers. */
357 		clfree(&tp->t_rawq);
358 		clfree(&tp->t_canq);
359 		clfree(&tp->t_outq);
360 		clalloc(&tp->t_rawq, 8192, 1);
361 		clalloc(&tp->t_canq, 8192, 1);
362 		clalloc(&tp->t_outq, 8192, 0);
363 #endif
364 
365 	} else
366 		tp = msc_tty[ttyn];
367 
368 	tp->t_oproc = (void (*) (struct tty *)) mscstart;
369 	tp->t_param = mscparam;
370 	tp->t_dev = dev;
371 	tp->t_hwiflow = mschwiflow;
372 
373 	/* if port is still closing, just bitbucket remaining characters */
374 	if (msc->closing) {
375 		ms->OutFlush = TRUE;
376 		msc->closing = FALSE;
377 	}
378 
379 	/* initialize tty */
380 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
381 		ttychars(tp);
382 		if (tp->t_ispeed == 0) {
383 			tp->t_iflag = TTYDEF_IFLAG;
384 			tp->t_oflag = TTYDEF_OFLAG;
385 			tp->t_cflag = TTYDEF_CFLAG;
386 			tp->t_lflag = TTYDEF_LFLAG;
387 			tp->t_ispeed = tp->t_ospeed = mscdefaultrate;
388 		}
389 
390 		/* flags changed to be private to every unit by JM */
391 		if (msc->openflags & TIOCFLAG_CLOCAL)
392 			tp->t_cflag |= CLOCAL;
393 		if (msc->openflags & TIOCFLAG_CRTSCTS)
394 			tp->t_cflag |= CRTSCTS;
395 		if (msc->openflags & TIOCFLAG_MDMBUF)
396 			tp->t_cflag |= MDMBUF;
397 
398 		mscparam(tp, &tp->t_termios);
399 		ttsetwater(tp);
400 
401 		(void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMSET);
402 
403 		if ((SWFLAGS(dev) & TIOCFLAG_SOFTCAR) ||
404 		    (mscmctl(dev, 0, DMGET) & TIOCM_CD))
405 			tp->t_state |= TS_CARR_ON;
406 		else
407 			tp->t_state &= ~TS_CARR_ON;
408 
409 	} else {
410 		if (tp->t_state & TS_XCLUDE &&
411 		    suser(l->l_proc->p_ucred, &l->l_proc->p_acflag) != 0) {
412 			splx(s);
413 			return (EBUSY);
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, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH, ttopen, 0);
442 		tp->t_wopen--;
443 
444 		if (error) {
445 			splx(s);
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 	splx(s);
462 
463 	/*
464 	 * Reset the tty pointer, as there could have been a dialout
465 	 * use of the tty with a dialin open waiting.
466 	 */
467 	tp->t_dev = dev;
468 
469 	return tp->t_linesw->l_open(dev, tp);
470 }
471 
472 
473 int
474 mscclose(dev_t dev, int flag, int mode, struct lwp *l)
475 {
476 	register struct tty *tp;
477 	int slot;
478 	volatile struct mscstatus *ms;
479 	struct mscdevice *msc;
480 
481 	/* get the device structure */
482 	slot = MSCSLOT(dev);
483 
484 	if (slot >= MSCSLOTS)
485 		return ENXIO;
486 
487 	msc = &mscdev[slot];
488 
489 	if (!msc->active)
490 		return ENXIO;
491 
492 	ms = &msc->board->Status[msc->port];
493 
494 	tp = msc_tty[MSCTTY(dev)];
495 	tp->t_linesw->l_close(tp, flag);
496 
497 	(void) mscmctl(dev, 0, DMSET);
498 
499 	ttyclose(tp);
500 
501 	if (msc->flags & TIOCM_DTR)
502 		msc->closing = TRUE; /* flush remaining characters before dropping DTR */
503 	else
504 		ms->OutFlush = TRUE; /* just bitbucket remaining characters */
505 
506 	return (0);
507 }
508 
509 
510 int
511 mscread(dev_t dev, struct uio *uio, int flag)
512 {
513 	register struct tty *tp;
514 
515 	tp = msc_tty[MSCTTY(dev)];
516 
517 	if (! tp)
518 	 return ENXIO;
519 
520 	return tp->t_linesw->l_read(tp, uio, flag);
521 }
522 
523 
524 int
525 mscwrite(dev_t dev, struct uio *uio, int flag)
526 {
527 	register struct tty *tp;
528 
529 	tp = msc_tty[MSCTTY(dev)];
530 
531 	if (! tp)
532 		return ENXIO;
533 
534 	return tp->t_linesw->l_write(tp, uio, flag);
535 }
536 
537 int
538 mscpoll(dev_t dev, int events, struct lwp *l)
539 {
540 	register struct tty *tp;
541 
542 	tp = msc_tty[MSCTTY(dev)];
543 
544 	if (! tp)
545 		return ENXIO;
546 
547 	return ((*tp->t_linesw->l_poll)(tp, events, l));
548 }
549 
550 /*
551  * This interrupt is periodically invoked in the vertical blank
552  * interrupt. It's used to keep track of the modem control lines
553  * and (new with the fast_int code) to move accumulated data up in
554  * to the tty layer.
555  *
556  * NOTE: MSCCDHACK is an invention of mine for dubious purposes. If you
557  *	 want to activate it add
558  *	 options MSCCDHACK
559  *	 in the kernel conf file. Basically it forces CD->Low transitions
560  *	 to ALWAYS send a signal to the process, even if the device is in
561  *	 clocal mode or an outdial device. RFH
562  */
563 void
564 mscmint(register void *data)
565 {
566 	register struct tty *tp;
567 	struct mscdevice *msc;
568 	volatile struct mscstatus *ms;
569 	volatile u_char *ibuf, *cbuf;
570 	unsigned char newhead; /* was int */
571 	unsigned char bufpos;  /* was int */
572 	unsigned char ncd, ocd, ccd;
573 	int unit, slot, maxslot;
574 	int s, i;
575 
576 	unit = (int) data;
577 
578 	/* check each line on this board */
579 	maxslot = MSCSLOTUL(unit, NUMLINES);
580 	if (maxslot > MSCSLOTS)
581 		maxslot = MSCSLOTS;
582 
583 	msc = &mscdev[MSCSLOTUL(unit, 0)];
584 
585 	newhead = msc->board->Common.CDHead;
586 	bufpos  = msc->board->Common.CDTail;
587 	if (newhead != bufpos) {	/* CD events in queue	*/
588 	    /* set interrupt priority level */
589 	    s = spltty();
590 	    ocd = msc->board->Common.CDStatus;		/* get old status bits	*/
591 	    while (newhead != bufpos) {			/* read all events	*/
592 		ncd = msc->board->CDBuf[bufpos++];	/* get one event	*/
593 		ccd = ncd ^ ocd;			/* mask of changed lines*/
594 		ocd = ncd;				/* save new status bits	*/
595 #if DEBUG_CD
596 		printf("ocd %02x ncd %02x ccd %02x\n", ocd, ncd, ccd);
597 #endif
598 		for(i = 0; i < NUMLINES; i++) {		/* do for all lines	*/
599 		    if (ccd & 1) {			/* this one changed	*/
600 			msc = &mscdev[MSCSLOTUL(unit, i)];
601 			if (ncd & 1) {	/* CD is now OFF */
602 #if DEBUG_CD
603 			    printf("msc%d: CD OFF %d\n", unit, msc->port);
604 #endif
605 			    msc->flags &= ~TIOCM_CD;
606 			    if ((tp = msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, i))]) &&
607 				 (tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
608 
609 #ifndef MSCCDHACK
610 				if (MSCDIALIN(tp->t_dev))
611 #endif
612 				{
613 				    if (tp->t_linesw->l_modem(tp, 0) == 0) {
614 					/* clear RTS and DTR, bitbucket output */
615 					ms = &msc->board->Status[msc->port];
616 					ms->Command = (ms->Command & ~MSCCMD_CMask) |
617 						 MSCCMD_Close;
618 					ms->Setup = TRUE;
619 					msc->flags &= ~(TIOCM_DTR | TIOCM_RTS);
620 					ms->OutFlush = TRUE;
621 				    }
622 				}
623 			    }
624 			} else {	/* CD is now ON */
625 #if DEBUG_CD
626 			    printf("msc%d: CD ON %d\n", unit, msc->port);
627 #endif
628 			    msc->flags |= TIOCM_CD;
629 			    if ((tp = msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, i))]) &&
630 				(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
631 				    if (MSCDIALIN(tp->t_dev))
632 					tp->t_linesw->l_modem(tp, 1);
633 			    } /* if tp valid and port open */
634 			}		/* CD on/off */
635 		    } /* if CD changed for this line */
636 		    ccd >>= 1; ncd >>= 1;			/* bit for next line */
637 		} /* for every line */
638 	    } /* while events in queue */
639 	msc->board->Common.CDStatus = ocd;		/* save new status */
640 	msc->board->Common.CDTail   = bufpos;	/* remove events */
641 	splx(s);
642 	}	/* if events in CD queue */
643 
644 	for (slot = MSCSLOTUL(unit, 0); slot < maxslot; slot++) {
645 	    msc = &mscdev[slot];
646 
647 	    if (!msc->active)
648 		continue;
649 
650 	    tp = msc_tty[MSCTTYSLOT(slot)];
651 	    ms = &msc->board->Status[msc->port];
652 
653 	    newhead = ms->InHead;		/* 65c02 write pointer */
654 
655 	    /* yoohoo, is the port open? */
656 	    if (tp && (tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
657 		/* port is open, handle all type of events */
658 
659 		/* set interrupt priority level */
660 		s = spltty();
661 
662 		/* check for input for this port */
663 		if (newhead != (bufpos = ms->InTail)) {
664 		    /* buffer for input chars/events */
665 		    ibuf = &msc->board->InBuf[msc->port][0];
666 
667 		    /* data types of bytes in ibuf */
668 		    cbuf = &msc->board->InCtl[msc->port][0];
669 
670 		    /* do for all chars, if room */
671 		    while (bufpos != newhead) {
672 			/* which type of input data? */
673 			switch (cbuf[bufpos]) {
674 			    /* input event (CD, BREAK, etc.) */
675 			    case MSCINCTL_EVENT:
676 				switch (ibuf[bufpos++]) {
677 				    case MSCEVENT_Break:
678 					tp->t_linesw->l_rint(TTY_FE, tp);
679 					break;
680 
681 				    default:
682 					printf("msc%d: unknown event type %d\n",
683 					msc->unit, ibuf[(bufpos-1)&0xff]);
684 				} /* event type switch */
685 				break;
686 
687 			    case MSCINCTL_CHAR:
688 				if (tp->t_state & TS_TBLOCK) {
689 				    goto NoRoomForYa;
690 				}
691 				tp->t_linesw->l_rint((int)ibuf[bufpos++], tp);
692 				break;
693 
694 			    default:
695 				printf("msc%d: unknown data type %d\n",
696 				msc->unit, cbuf[bufpos]);
697 				bufpos++;
698 			} /* switch on input data type */
699 		    } /* while there's something in the buffer */
700 NoRoomForYa:
701 		ms->InTail = bufpos;		/* tell 65C02 what we've read */
702 	    } /* if there was something in the buffer */
703 
704 	    /* we get here only when the port is open */
705 	    /* send output */
706 	    if (tp->t_state & (TS_BUSY|TS_FLUSH)) {
707 
708 		bufpos = ms->OutHead - ms->OutTail;
709 
710 		/* busy and below low water mark? */
711 		if (tp->t_state & TS_BUSY) {
712 		    if (bufpos < IOBUFLOWWATER) {
713 			tp->t_state &= ~TS_BUSY;	/* not busy any more */
714 			if (tp->t_linesw)
715 			    tp->t_linesw->l_start(tp);
716 			else
717 			    mscstart(tp);
718 		    }
719 		}
720 
721 		/* waiting for flush and buffer empty? */
722 		if (tp->t_state & TS_FLUSH) {
723 		    if (bufpos == 0)
724 			tp->t_state &= ~TS_FLUSH;	/* finished flushing */
725 		}
726 	    } /* BUSY or FLUSH */
727 
728 	    splx(s);
729 
730 	    } else { /* End of port open */
731 		/* port is closed, don't pass on the chars from it */
732 
733 		/* check for input for this port */
734 		if (newhead != (bufpos = ms->InTail)) {
735 		    /* buffer for input chars/events */
736 		    ibuf = &msc->board->InBuf[msc->port][0];
737 
738 		    /* data types of bytes in ibuf */
739 		    cbuf = &msc->board->InCtl[msc->port][0];
740 
741 		    /* do for all chars, if room */
742 			while (bufpos != newhead) {
743 			    /* which type of input data? */
744 			    switch (cbuf[bufpos]) {
745 			    /* input event (BREAK, etc.) */
746 				case MSCINCTL_EVENT:
747 				    switch (ibuf[bufpos++]) {
748 					default:
749 					    printf("msc: unknown event type %d\n",
750 						ibuf[(bufpos-1)&0xff]);
751 				    } /* event type switch */
752 				    break;
753 
754 				default:
755 				    bufpos++;
756 			    } /* switch on input data type */
757 			} /* while there's something in the buffer */
758 
759 		    ms->InTail = bufpos;		/* tell 65C02 what we've read */
760 		} /* if there was something in the buffer */
761 	    } /* End of port open/close */
762 
763 	    /* is this port closing? */
764 	    if (msc->closing) {
765 		/* if DTR is off, just bitbucket remaining characters */
766 		if ( (msc->flags & TIOCM_DTR) == 0) {
767 		    ms->OutFlush = TRUE;
768 		    msc->closing = FALSE;
769 		}
770 		/* if output has drained, drop DTR */
771 		else if (ms->OutHead == ms->OutTail) {
772 		    (void) mscmctl(tp->t_dev, 0, DMSET);
773 		    msc->closing = FALSE;
774 		}
775 	    }
776 	}  /* For all ports */
777 }
778 
779 
780 int
781 mscioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
782 {
783 	register struct tty *tp;
784 	register int slot;
785 	register int error;
786 	struct mscdevice *msc;
787 	volatile struct mscstatus *ms;
788 	int s;
789 
790 	/* get the device structure */
791 	slot = MSCSLOT(dev);
792 
793 	if (slot >= MSCSLOTS)
794 		return ENXIO;
795 
796 	msc = &mscdev[slot];
797 
798 	if (!msc->active)
799 		return ENXIO;
800 
801 	ms = &msc->board->Status[msc->port];
802 	if (!(tp = msc_tty[MSCTTY(dev)]))
803 		return ENXIO;
804 
805 	error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l);
806 	if (error != EPASSTHROUGH)
807 		return (error);
808 
809 	error = ttioctl(tp, cmd, data, flag, l);
810 	if (error != EPASSTHROUGH)
811 		return (error);
812 
813 	switch (cmd) {
814 
815 		/* send break */
816 		case TIOCSBRK:
817 			s = spltty();
818 			ms->Command = (ms->Command & (~MSCCMD_RTSMask)) | MSCCMD_Break;
819 			ms->Setup = TRUE;
820 			splx(s);
821 			break;
822 
823 		/* clear break */
824 		case TIOCCBRK:
825 			s = spltty();
826 			ms->Command = (ms->Command & (~MSCCMD_RTSMask)) | MSCCMD_RTSOn;
827 			ms->Setup = TRUE;
828 			splx(s);
829 			break;
830 
831 		case TIOCSDTR:
832 			(void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIS);
833 			break;
834 
835 		case TIOCCDTR:
836 			if (!MSCDIALIN(dev))	/* don't let dialins drop DTR */
837 				(void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIC);
838 			break;
839 
840 		case TIOCMSET:
841 			(void) mscmctl(dev, *(int *)data, DMSET);
842 			break;
843 
844 		case TIOCMBIS:
845 			(void) mscmctl(dev, *(int *)data, DMBIS);
846 			break;
847 
848 		case TIOCMBIC:
849 			if (MSCDIALIN(dev))	/* don't let dialins drop DTR */
850 				(void) mscmctl(dev, *(int *)data & TIOCM_DTR, DMBIC);
851 			else
852 				(void) mscmctl(dev, *(int *)data, DMBIC);
853 			break;
854 
855 		case TIOCMGET:
856 			*(int *)data = mscmctl(dev, 0, DMGET);
857 			break;
858 
859 		case TIOCGFLAGS:
860 			*(int *)data = SWFLAGS(dev);
861 			break;
862 
863 		case TIOCSFLAGS:
864 			error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
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 
1005 	if (cc <= tp->t_lowat) {
1006 		if (tp->t_state & TS_ASLEEP) {
1007 			tp->t_state &= ~TS_ASLEEP;
1008 			wakeup((caddr_t)&tp->t_outq);
1009 		}
1010 		selwakeup(&tp->t_wsel);
1011 	}
1012 
1013 	/* don't bother if no characters or busy */
1014 	if (cc == 0 || (tp->t_state & TS_BUSY))
1015 		goto out;
1016 
1017 	/*
1018 	 * Limit the amount of output we do in one burst
1019 	 */
1020 	msc = &mscdev[slot];
1021 	ms = &msc->board->Status[msc->port];
1022 	mhead = ms->OutHead;
1023 	maxout = mhead - ms->OutTail;
1024 
1025 	if (maxout < 0)
1026 		maxout += IOBUFLEN;
1027 
1028 	maxout = IOBUFLEN - 1 - maxout;
1029 
1030 	if (cc >= maxout) {
1031 		hiwat++;
1032 		cc = maxout;
1033 	}
1034 
1035 	cc = q_to_b (&tp->t_outq, msc->tmpbuf, cc);
1036 
1037 	if (cc > 0) {
1038 		tp->t_state |= TS_BUSY;
1039 
1040 		mob = &msc->board->OutBuf[msc->port][0];
1041 		cp = &msc->tmpbuf[0];
1042 
1043 		/* enable output */
1044 		ms->OutDisable = FALSE;
1045 
1046 #if 0
1047 		msc->tmpbuf[cc] = 0;
1048 		printf("sending '%s'\n", msctmpbuf);
1049 #endif
1050 
1051 		/* send the first char across to reduce latency */
1052 		mob[mhead++] = *cp++;
1053 		mhead &= IOBUFLENMASK;
1054 		ms->OutHead = mhead;
1055 		cc--;
1056 
1057 		/* copy the rest of the chars across quickly */
1058 		while (cc > 0) {
1059 			mob[mhead++] = *cp++;
1060 			mhead &= IOBUFLENMASK;
1061 			cc--;
1062 		}
1063 		ms->OutHead = mhead;
1064 
1065 		/* leave the device busy if we've filled the buffer */
1066 		if (!hiwat)
1067 			tp->t_state &= ~TS_BUSY;
1068 	}
1069 
1070 out:
1071 	splx(s);
1072 }
1073 
1074 
1075 /* XXX */
1076 /*
1077  * Stop output on a line.
1078  */
1079 /*ARGSUSED*/
1080 void
1081 mscstop(register struct tty *tp, int flag)
1082 /* flag variable defaulted to int anyway */
1083 {
1084 	register int s;
1085 #if 0
1086 	struct mscdevice *msc;
1087 	volatile struct mscstatus *ms;
1088 #endif
1089 
1090 	s = spltty();
1091 	if (tp->t_state & TS_BUSY) {
1092 		if ((tp->t_state & TS_TTSTOP) == 0) {
1093 			tp->t_state |= TS_FLUSH;
1094 #if 0
1095 			msc = &mscdev[MSCSLOT(tp->t_dev)];
1096 			ms = &msc->board->Status[msc->port];
1097 			printf("stopped output on msc%d\n", MSCSLOT(tp->t_dev));
1098 			ms->OutDisable = TRUE;
1099 #endif
1100 		}
1101 	}
1102 	splx(s);
1103 }
1104 
1105 
1106 /*
1107  * bits can be: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, TIOCM_CD, TIOCM_RI, TIOCM_DSR
1108  */
1109 int
1110 mscmctl(dev_t dev, int bits, int how)
1111 {
1112 	struct mscdevice *msc;
1113 	volatile struct mscstatus *ms;
1114 	int slot;
1115 	int s;
1116 	u_char newcmd;
1117 	int OldFlags;
1118 
1119 	/* get the device structure */
1120 	slot = MSCSLOT(dev);
1121 
1122 	if (slot >= MSCSLOTS)
1123 		return ENXIO;
1124 
1125 	msc = &mscdev[slot];
1126 
1127 	if (!msc->active)
1128 		return ENXIO;
1129 
1130 	s = spltty();
1131 
1132 	if (how != DMGET) {
1133 		OldFlags = msc->flags;
1134 		bits &= TIOCM_DTR | TIOCM_RTS;	/* can only modify DTR and RTS */
1135 
1136 		switch (how) {
1137 		    case DMSET:
1138 			msc->flags = (bits | (msc->flags & ~(TIOCM_DTR | TIOCM_RTS)));
1139 			break;
1140 
1141 		    case DMBIC:
1142 			msc->flags &= ~bits;
1143 			break;
1144 
1145 		    case DMBIS:
1146 			msc->flags |= bits;
1147 			break;
1148 		}
1149 
1150 		/* modify modem control state */
1151 		ms = &msc->board->Status[msc->port];
1152 
1153 		if (msc->flags & TIOCM_RTS)	/* was bits & */
1154 			newcmd = MSCCMD_RTSOn;
1155 		else			/* this doesn't actually work now */
1156 			newcmd = MSCCMD_RTSOff;
1157 
1158 		if (msc->flags & TIOCM_DTR)	/* was bits & */
1159 			newcmd |= MSCCMD_Enable;
1160 
1161 		ms->Command = (ms->Command & (~MSCCMD_RTSMask & ~MSCCMD_Enable)) | newcmd;
1162 		ms->Setup = TRUE;
1163 
1164 		/* if we've dropped DTR, bitbucket any pending output */
1165 		if ( (OldFlags & TIOCM_DTR) && ((bits & TIOCM_DTR) == 0))
1166 			ms->OutFlush = TRUE;
1167 	}
1168 
1169 	bits = msc->flags;
1170 
1171 	(void) splx(s);
1172 
1173 	return(bits);
1174 }
1175 
1176 
1177 struct tty *
1178 msctty(dev_t dev)
1179 {
1180 	return(msc_tty[MSCTTY(dev)]);
1181 }
1182 
1183 
1184 /*
1185  * Load JM's freely redistributable A2232 6502c code. Let turbo detector
1186  * run for a while too.
1187  */
1188 
1189 int
1190 mscinitcard(struct zbus_args *zap)
1191 {
1192 	int bcount;
1193 	short start;
1194 	u_char *from;
1195 	volatile u_char *to;
1196 	volatile struct mscmemory *mlm;
1197 
1198 	mlm = (volatile struct mscmemory *)zap->va;
1199 	(void)mlm->Enable6502Reset;
1200 
1201 	/* copy the code across to the board */
1202 	to = (volatile u_char *)mlm;
1203 	from = msc6502code; bcount = sizeof(msc6502code) - 2;
1204 	start = *(short *)from; from += sizeof(start);
1205 	to += start;
1206 
1207 	while(bcount--) *to++ = *from++;
1208 
1209 	mlm->Common.Crystal = MSC_UNKNOWN;	/* use automatic speed check */
1210 
1211 	/* start 6502 running */
1212 	(void)mlm->ResetBoard;
1213 
1214 	/* wait until speed detector has finished */
1215 	for (bcount = 0; bcount < 200; bcount++) {
1216 		delay(10000);
1217 		if (mlm->Common.Crystal)
1218 			break;
1219 	}
1220 
1221 	return(0);
1222 }
1223 
1224 #endif  /* NMSC > 0 */
1225