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