summaryrefslogtreecommitdiff
path: root/sys/arch/amiga/dev/msc.c
blob: 742eecf29e878f3fc10e9d8160cfa089753e3d0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
/*	$OpenBSD: msc.c,v 1.7 2002/02/15 20:45:30 nordin Exp $ */
/*	$NetBSD: msc.c,v 1.13 1996/12/23 09:10:26 veego Exp $	*/

/*
 * Copyright (c) 1993 Zik.
 * Copyright (c) 1995 Jukka Marin <jmarin@jmp.fi>.
 * Copyright (c) 1995 Timo Rossi <trossi@jyu.fi>.
 * Copyright (c) 1995 Rob Healey <rhealey@kas.helios.mn.org>.
 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *   - converted from NetBSD Amiga serial driver to A2232 serial driver
 *     by zik 931207
 *   - added ttyflags hooks rfh 940419
 *   - added new style config support rfh 940601
 *   - added code to halt board during memory load so board doesn't flip
 *     out. /dev/reload works now. Also created mschwiflow function so BSD can
 *     attempt to use board RTS flow control now. rfh 950108
 *   - Integrated work from Jukka Marin <jmarin@jmp.fi> and
 *     Timo Rossi <trossi@jyu.fi> The mscmint() code is Jukka's. 950916
 *     Integrated more bug fixes by Jukka Marin <jmarin@jmp.fi> 950918
 *     Also added Jukka's turbo board code. 950918
 *   - Reformatted to NetBSD style format.
 *   - Rewritten the carrier detect system to prevent lock-ups (jm 951029)
 */

#include "msc.h"

#if NMSC > 0
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/proc.h>
#include <sys/file.h>
#include <sys/malloc.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/syslog.h>
#include <sys/device.h>

#include <amiga/amiga/device.h>
#include <amiga/dev/zbusvar.h>
#include <amiga/dev/mscreg.h>
#include <machine/cpu.h>

#include <amiga/amiga/custom.h>
#include <amiga/amiga/cia.h>
#include <amiga/amiga/cc.h>

#include <sys/conf.h>
#include <machine/conf.h>

/* 6502 code for A2232 card */
#include "msc6502.h"

/*
 * Note: These are Zik's original comments:
 * There is a bit of a "gotcha" concerning the numbering
 * of msc devices and the specification of the number of serial
 * lines in the kernel.
 *
 * Each board has seven lines, but for programming convenience
 * and compatibility with Amiga UNIX the boards' minor device
 * numbers are allocated in groups of sixteen:
 *
 *                     minor device numbers
 * First board		0  2  4  6  8 10 12
 * Second board        16 18 20 22 24 26 28
 * Third board	       32 34 36 38 40 42 44
 *
 * The intermediate minor device numbers are dialin versions
 * of the same devices. ie. 10 is dialout line 6 and 11 is
 * the dialin version of line 6.
 *
 * On the other hand, I have made the NMSC config option refer
 * to the total number of a2232 cards, not the maximum
 * minor device number. So you might have NMSC=3, in which case
 * you have three boards with minor device numbers from 0 to 45.
 */

int	mscparam __P((struct tty *, struct termios *));
void	mscstart __P((struct tty *));
int	mschwiflow __P((struct tty *, int));
int	mscinitcard __P((struct zbus_args *));

int	mscdefaultrate = TTYDEF_SPEED;

struct	mscdevice mscdev[MSCSLOTS];	/* device structs for all lines */
struct	tty *msc_tty[MSCTTYS];		/* ttys for all lines */

struct	vbl_node msc_vbl_node[NMSC];	/* vbl interrupt node per board */

struct speedtab mscspeedtab_normal[] = {
	{ 0,		0		},
	{ 50,		MSCPARAM_B50	},
	{ 75,		MSCPARAM_B75	},
	{ 110,		MSCPARAM_B110	},
	{ 134,		MSCPARAM_B134	},
	{ 150,		MSCPARAM_B150	},
	{ 300,		MSCPARAM_B300	},
	{ 600,		MSCPARAM_B600	},
	{ 1200,		MSCPARAM_B1200	},
	{ 1800,		MSCPARAM_B1800	},
	{ 2400,		MSCPARAM_B2400	},
	{ 3600,		MSCPARAM_B3600	},
	{ 4800,		MSCPARAM_B4800	},
	{ 7200,		MSCPARAM_B7200	},
	{ 9600,		MSCPARAM_B9600	},
	{ 19200,	MSCPARAM_B19200	},
	{ 115200,	MSCPARAM_B115200 },
	{ -1,		-1		}
};
  
struct speedtab mscspeedtab_turbo[] = {
	{ 0,		0		},
	{ 100,		MSCPARAM_B50	},
	{ 150,		MSCPARAM_B75	},
	{ 220,		MSCPARAM_B110	},
	{ 269,		MSCPARAM_B134	},
	{ 300,		MSCPARAM_B150	},
	{ 600,		MSCPARAM_B300	},
	{ 1200,		MSCPARAM_B600	},
	{ 2400,		MSCPARAM_B1200	},
	{ 3600,		MSCPARAM_B1800	},
	{ 4800,		MSCPARAM_B2400	},
	{ 7200,		MSCPARAM_B3600	},
	{ 9600,		MSCPARAM_B4800	},
	{ 14400,	MSCPARAM_B7200	},
	{ 19200,	MSCPARAM_B9600	},
	{ 38400,	MSCPARAM_B19200	},
	{ 230400,	MSCPARAM_B115200 },
	{ -1,		-1		}
};
  
struct   speedtab *mscspeedtab;

int mscmctl __P((dev_t dev, int bits, int howto));
void mscmint __P((register void *data));

int mscmatch __P((struct device *, void *, void *));
void mscattach __P((struct device *, struct device *, void *));

#define	SWFLAGS(dev)	(msc->openflags | (MSCDIALIN(dev) ? 0 : TIOCFLAG_SOFTCAR))
#define	DEBUG_CD	0

struct cfattach msc_ca = {
	sizeof(struct device), mscmatch, mscattach
};

struct cfdriver msc_cd = {
	NULL, "msc",DV_TTY, NULL, 0
};

int
mscmatch(pdp, match, auxp)
	struct device *pdp;
	void *match, *auxp;
{
	struct zbus_args *zap;

	zap = auxp;
	if (zap->manid == 514 && (zap->prodid == 70 || zap->prodid == 69))
		return(1);

	return (0);
}

void
mscattach(pdp, dp, auxp)
	struct device *pdp, *dp;
	void *auxp;
{
	volatile struct mscmemory *mscmem;
	struct mscdevice *msc;
	struct zbus_args *zap;
	int unit;
	int Count;

	zap = (struct zbus_args *)auxp;
	unit = dp->dv_unit;

	/*
	 * Make config msgs look nicer.
	 */
	printf("\n");

	if (mscinitcard(zap) != 0) {
		printf("msc%d: Board initialize failed, bad download code.\n", unit);
		return;
	}

	printf("msc%d: Board successfully initialized.\n", unit);

	mscmem = (struct mscmemory *) zap->va;

	if (mscmem->Common.Crystal == MSC_UNKNOWN) {
		printf("msc%d: Unable to detect crystal frequency.\n", unit);
		return;
	}

	if (mscmem->Common.Crystal == MSC_TURBO) {
		printf("msc%d: Turbo version detected (%02x%02x:%d)\n", unit,
			mscmem->Common.TimerH, mscmem->Common.TimerL,
			mscmem->Common.Pad_a);
		mscspeedtab = mscspeedtab_turbo;
	} else {
		printf("msc%d: Normal version detected (%02x%02x:%d)\n", unit,
			mscmem->Common.TimerH, mscmem->Common.TimerL,
			mscmem->Common.Pad_a);
		mscspeedtab = mscspeedtab_normal;
	}

	mscmem->Common.CDStatus = 0;	/* common status for all 7 ports */

	/* XXX 8 is a constant */
	for (Count = 0; Count < 8 && MSCSLOTUL(unit, Count) < MSCSLOTS; Count++) {
		msc = &mscdev[MSCSLOTUL(unit, Count)];
		msc->board = mscmem;
		msc->port = Count;
		msc->flags = 0;
		msc->openflags = 0;
		msc->active = 1;
		msc->unit = unit;
		msc->closing = FALSE;
		msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, Count))] = NULL;
		msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, Count)) + 1] = NULL;
	}

	/* disable the non-existant eighth port */
	if (MSCSLOTUL(unit, NUMLINES) < MSCSLOTS)
		mscdev[MSCSLOTUL(unit, NUMLINES)].active = 0;

	msc_vbl_node[unit].function = (void (*) (void *)) mscmint;
	msc_vbl_node[unit].data = (void *) unit;

	add_vbl_function (&msc_vbl_node[unit], MSC_VBL_PRIORITY, (void *)unit);

	return; 
}

/* ARGSUSED */
int
mscopen(dev, flag, mode, p)
	dev_t dev;
	int flag, mode;
	struct proc *p;
{
	register struct tty *tp;
	struct mscdevice *msc;
	volatile struct mscstatus *ms;
	int error = 0;
	int s, slot, ttyn;
  
	/* get the device structure */
	slot = MSCSLOT(dev);
	ttyn = MSCTTY(dev);

	if (slot >= MSCSLOTS)
		return ENXIO;

	if (MSCLINE(dev) >= NUMLINES)
		return ENXIO;

	msc = &mscdev[slot];
	ms = &msc->board->Status[msc->port];

	if (!msc->active)
		return ENXIO;

	/*
	 * RFH: WHY here? Put down by while like other serial drivers
	 *      But if we do that it makes things bomb.
	 */
	s = spltty();

	if (!msc_tty[ttyn]) {

		tp = ttymalloc();
		tty_attach(tp);
		msc_tty[ttyn] = tp;
		msc_tty[ttyn+1] = (struct tty *)NULL;

#if 0
		/* default values are not optimal for this device, increase buffers. */
		clfree(&tp->t_rawq);
		clfree(&tp->t_canq);
		clfree(&tp->t_outq);
		clalloc(&tp->t_rawq, 8192, 1);
		clalloc(&tp->t_canq, 8192, 1);
		clalloc(&tp->t_outq, 8192, 0);
#endif

	} else
		tp = msc_tty[ttyn];

	tp->t_oproc = (void (*) (struct tty *)) mscstart;
	tp->t_param = mscparam;
	tp->t_dev = dev;
	tp->t_hwiflow = mschwiflow;
 
	/* if port is still closing, just bitbucket remaining characters */
	if (msc->closing) {
		ms->OutFlush = TRUE;
		msc->closing = FALSE;
	}

	/* initialize tty */
	if ((tp->t_state & TS_ISOPEN) == 0) {
		tp->t_state |= TS_WOPEN;
		ttychars(tp);
		if (tp->t_ispeed == 0) {
			tp->t_iflag = TTYDEF_IFLAG;
			tp->t_oflag = TTYDEF_OFLAG;
			tp->t_cflag = TTYDEF_CFLAG;
			tp->t_lflag = TTYDEF_LFLAG;
			tp->t_ispeed = tp->t_ospeed = mscdefaultrate;
		}

		/* flags changed to be private to every unit by JM */
		if (msc->openflags & TIOCFLAG_CLOCAL)
			tp->t_cflag |= CLOCAL;
		if (msc->openflags & TIOCFLAG_CRTSCTS)
			tp->t_cflag |= CRTSCTS;
		if (msc->openflags & TIOCFLAG_MDMBUF)
			tp->t_cflag |= MDMBUF;

		mscparam(tp, &tp->t_termios);
		ttsetwater(tp);

		(void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMSET);

		if ((SWFLAGS(dev) & TIOCFLAG_SOFTCAR) ||
		    (mscmctl(dev, 0, DMGET) & TIOCM_CD))
			tp->t_state |= TS_CARR_ON;
		else
			tp->t_state &= ~TS_CARR_ON;

	} else {
		if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
			splx(s);
			return (EBUSY);
		}
	}

	/*
	 * if NONBLOCK requested, ignore carrier
	 */
	if (flag & O_NONBLOCK) {
#if DEBUG_CD
		printf("msc%d: %d open nonblock\n", msc->unit, MSCLINE(dev));
#endif
		goto done;
	}

	/* 
	 * s = spltty();
	 *
	 * This causes hangs when put here, like other TTY drivers do, rather than
	 * above, WHY? RFH
	 *
	 */

	while ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_cflag & CLOCAL) == 0) {
		tp->t_state |= TS_WOPEN;

#if DEBUG_CD
		printf("msc%d: %d waiting for CD\n", msc->unit, MSCLINE(dev));
#endif
		error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH, ttopen, 0);

		if (error) {
			splx(s);
			return(error);
		}
	}

#if DEBUG_CD
	printf("msc%d: %d got CD\n", msc->unit, MSCLINE(dev));
#endif

	done: 
		/* This is a way to handle lost XON characters */
		if ((flag & O_TRUNC) && (tp->t_state & TS_TTSTOP)) {
			tp->t_state &= ~TS_TTSTOP;
			ttstart (tp);
		}

	splx(s);

	/*
	 * Reset the tty pointer, as there could have been a dialout
	 * use of the tty with a dialin open waiting.
	 */
	tp->t_dev = dev;

	return((*linesw[tp->t_line].l_open)(dev, tp));
}

int
mscclose(dev, flag, mode, p)
	dev_t dev;
	int flag, mode;
	struct proc *p;
{
	register struct tty *tp;
	int slot;
	volatile struct mscstatus *ms;
	struct mscdevice *msc;
  
	/* get the device structure */
	slot = MSCSLOT(dev);

	if (slot >= MSCSLOTS)
		return ENXIO;

	msc = &mscdev[slot];

	if (!msc->active)
		return ENXIO;

	ms = &msc->board->Status[msc->port];
  
	tp = msc_tty[MSCTTY(dev)];
	(*linesw[tp->t_line].l_close)(tp, flag);

	(void) mscmctl(dev, 0, DMSET);

	ttyclose(tp);

	if (msc->flags & TIOCM_DTR)
		msc->closing = TRUE; /* flush remaining characters before dropping DTR */
	else
		ms->OutFlush = TRUE; /* just bitbucket remaining characters */

	return (0);
}
 
int
mscread(dev, uio, flag)
	dev_t dev;
	struct uio *uio;
	int flag;
{
	register struct tty *tp;
  
	tp = msc_tty[MSCTTY(dev)];

	if (! tp)
	 return ENXIO;

	return((*linesw[tp->t_line].l_read)(tp, uio, flag));
}
 
int
mscwrite(dev, uio, flag)
	dev_t dev;
	struct uio *uio;
	int flag;
{
	register struct tty *tp;
  
	tp = msc_tty[MSCTTY(dev)];

	if (! tp)
		return ENXIO;

	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
}

/*
 * This interrupt is periodically invoked in the vertical blank 
 * interrupt. It's used to keep track of the modem control lines
 * and (new with the fast_int code) to move accumulated data up in
 * to the tty layer.
 *
 * NOTE: MSCCDHACK is an invention of mine for dubious purposes. If you
 *	 want to activate it add
 *	 options MSCCDHACK
 *	 in the kernel conf file. Basically it forces CD->Low transitions
 *	 to ALWAYS send a signal to the process, even if the device is in
 *	 clocal mode or an outdial device. RFH
 */
void
mscmint (data)
	register void *data;
{
	register struct tty *tp;
	struct mscdevice *msc;
	volatile struct mscstatus *ms;
	volatile u_char *ibuf, *cbuf;
	unsigned char newhead; /* was int */
	unsigned char bufpos;  /* was int */
	unsigned char ncd, ocd, ccd;
	int unit, slot, maxslot;
	int s, i;

	unit = (int) data;

	/* check each line on this board */
	maxslot = MSCSLOTUL(unit, NUMLINES);
	if (maxslot > MSCSLOTS)
		maxslot = MSCSLOTS;

	msc = &mscdev[MSCSLOTUL(unit, 0)];

	newhead = msc->board->Common.CDHead;
	bufpos  = msc->board->Common.CDTail;
	if (newhead != bufpos) {	/* CD events in queue	*/
	    /* set interrupt priority level */
	    s = spltty();
	    ocd = msc->board->Common.CDStatus;		/* get old status bits	*/
	    while (newhead != bufpos) {			/* read all events	*/
		ncd = msc->board->CDBuf[bufpos++];	/* get one event	*/
		ccd = ncd ^ ocd;			/* mask of changed lines*/
		ocd = ncd;				/* save new status bits	*/
#if DEBUG_CD
		printf("ocd %02x ncd %02x ccd %02x\n", ocd, ncd, ccd);
#endif
		for(i = 0; i < NUMLINES; i++) {		/* do for all lines	*/
		    if (ccd & 1) {			/* this one changed	*/
			msc = &mscdev[MSCSLOTUL(unit, i)];
			if (ncd & 1) {	/* CD is now OFF */
#if DEBUG_CD
			    printf("msc%d: CD OFF %d\n", unit, msc->port);
#endif
			    msc->flags &= ~TIOCM_CD;
			    if ((tp = msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, i))]) &&
				 (tp->t_state & (TS_ISOPEN | TS_WOPEN))) {

#ifndef MSCCDHACK
				if (MSCDIALIN(tp->t_dev))
#endif
				{
				    if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0) {
					/* clear RTS and DTR, bitbucket output */
					ms = &msc->board->Status[msc->port];
					ms->Command = (ms->Command & ~MSCCMD_CMask) |
						 MSCCMD_Close;
					ms->Setup = TRUE;
					msc->flags &= ~(TIOCM_DTR | TIOCM_RTS);
					ms->OutFlush = TRUE;
				    }
				}
			    }
			} else {	/* CD is now ON */
#if DEBUG_CD
			    printf("msc%d: CD ON %d\n", unit, msc->port);
#endif
			    msc->flags |= TIOCM_CD;
			    if ((tp = msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, i))]) &&
				(tp->t_state & (TS_ISOPEN | TS_WOPEN))) {
				    if (MSCDIALIN(tp->t_dev))
					(*linesw[tp->t_line].l_modem)(tp, 1);
			    } /* if tp valid and port open */
			}		/* CD on/off */
		    } /* if CD changed for this line */
		    ccd >>= 1; ncd >>= 1;			/* bit for next line */
		} /* for every line */
	    } /* while events in queue */
	msc->board->Common.CDStatus = ocd;		/* save new status */
	msc->board->Common.CDTail   = bufpos;	/* remove events */
	splx(s);
	}	/* if events in CD queue */

	for (slot = MSCSLOTUL(unit, 0); slot < maxslot; slot++) {
	    msc = &mscdev[slot];

	    if (!msc->active)
		continue;

	    tp = msc_tty[MSCTTYSLOT(slot)];
	    ms = &msc->board->Status[msc->port];

	    newhead = ms->InHead;		/* 65c02 write pointer */

	    /* yoohoo, is the port open? */
	    if (tp && (tp->t_state & (TS_ISOPEN|TS_WOPEN))) {
		/* port is open, handle all type of events */

		/* set interrupt priority level */
		s = spltty();

		/* check for input for this port */
		if (newhead != (bufpos = ms->InTail)) {
		    /* buffer for input chars/events */
		    ibuf = &msc->board->InBuf[msc->port][0];

		    /* data types of bytes in ibuf */
		    cbuf = &msc->board->InCtl[msc->port][0];
    
		    /* do for all chars, if room */
		    while (bufpos != newhead) {
			/* which type of input data? */
			switch (cbuf[bufpos]) {
			    /* input event (CD, BREAK, etc.) */
			    case MSCINCTL_EVENT:
				switch (ibuf[bufpos++]) {
				    case MSCEVENT_Break:
					(*linesw[tp->t_line].l_rint)(TTY_FE, tp);
					break;

				    default:
					printf("msc%d: unknown event type %d\n",
					msc->unit, ibuf[(bufpos-1)&0xff]);
				} /* event type switch */
				break;

			    case MSCINCTL_CHAR:
				if (tp->t_state & TS_TBLOCK) {
				    goto NoRoomForYa;
				}
				(*linesw[tp->t_line].l_rint)((int)ibuf[bufpos++], tp);
				break;

			    default:
				printf("msc%d: unknown data type %d\n", 
				msc->unit, cbuf[bufpos]);
				bufpos++;
			} /* switch on input data type */
		    } /* while there's something in the buffer */
NoRoomForYa:
		ms->InTail = bufpos;		/* tell 65C02 what we've read */
	    } /* if there was something in the buffer */

	    /* we get here only when the port is open */
	    /* send output */
	    if (tp->t_state & (TS_BUSY|TS_FLUSH)) {

		bufpos = ms->OutHead - ms->OutTail;

		/* busy and below low water mark? */
		if (tp->t_state & TS_BUSY) {
		    if (bufpos < IOBUFLOWWATER) {
			tp->t_state &= ~TS_BUSY;	/* not busy any more */
			if (tp->t_line)
			    (*linesw[tp->t_line].l_start)(tp);
			else
			    mscstart(tp);
		    }
		}

		/* waiting for flush and buffer empty? */
		if (tp->t_state & TS_FLUSH) {
		    if (bufpos == 0)
			tp->t_state &= ~TS_FLUSH;	/* finished flushing */
		}
	    } /* BUSY or FLUSH */

	    splx(s);

	    } else { /* End of port open */
		/* port is closed, don't pass on the chars from it */

		/* check for input for this port */
		if (newhead != (bufpos = ms->InTail)) {
		    /* buffer for input chars/events */
		    ibuf = &msc->board->InBuf[msc->port][0];

		    /* data types of bytes in ibuf */
		    cbuf = &msc->board->InCtl[msc->port][0];
    
		    /* do for all chars, if room */
			while (bufpos != newhead) {
			    /* which type of input data? */
			    switch (cbuf[bufpos]) {
			    /* input event (BREAK, etc.) */
				case MSCINCTL_EVENT:
				    switch (ibuf[bufpos++]) {
					default:
					    printf("msc: unknown event type %d\n",
						ibuf[(bufpos-1)&0xff]);
				    } /* event type switch */
				    break;

				default:
				    bufpos++;
			    } /* switch on input data type */
			} /* while there's something in the buffer */

		    ms->InTail = bufpos;		/* tell 65C02 what we've read */
		} /* if there was something in the buffer */
	    } /* End of port open/close */

	    /* is this port closing? */
	    if (msc->closing) {
		/* if DTR is off, just bitbucket remaining characters */
		if ( (msc->flags & TIOCM_DTR) == 0) {
		    ms->OutFlush = TRUE;
		    msc->closing = FALSE;
		}
		/* if output has drained, drop DTR */
		else if (ms->OutHead == ms->OutTail) {
		    (void) mscmctl(tp->t_dev, 0, DMSET);
		    msc->closing = FALSE;
		}
	    }
	}  /* For all ports */
}

int
mscioctl(dev, cmd, data, flag, p)
	dev_t dev;
	u_long cmd;
	caddr_t data;
	int flag;
	struct proc *p;
{
	register struct tty *tp;
	register int slot;
	register int error;
	struct mscdevice *msc;
	volatile struct mscstatus *ms;
	int s;
  
	/* get the device structure */
	slot = MSCSLOT(dev);

	if (slot >= MSCSLOTS)
		return ENXIO;

	msc = &mscdev[slot];

	if (!msc->active)
		return ENXIO;

	ms = &msc->board->Status[msc->port];
	if (!(tp = msc_tty[MSCTTY(dev)]))
		return ENXIO;

	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);

	if (error >= 0)
		return (error);

	error = ttioctl(tp, cmd, data, flag, p);

	if (error >= 0)
		return (error);
  
	switch (cmd) {

		/* send break */
		case TIOCSBRK:
			s = spltty();
			ms->Command = (ms->Command & (~MSCCMD_RTSMask)) | MSCCMD_Break;
			ms->Setup = TRUE;
			splx(s);
			break;

		/* clear break */
		case TIOCCBRK:
			s = spltty();
			ms->Command = (ms->Command & (~MSCCMD_RTSMask)) | MSCCMD_RTSOn;
			ms->Setup = TRUE;
			splx(s);
			break;

		case TIOCSDTR:
			(void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIS);
			break;
      
		case TIOCCDTR:
			if (!MSCDIALIN(dev))	/* don't let dialins drop DTR */
				(void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIC);
			break;
      
		case TIOCMSET:
			(void) mscmctl(dev, *(int *)data, DMSET);
			break;
      
		case TIOCMBIS:
			(void) mscmctl(dev, *(int *)data, DMBIS);
			break;
      
		case TIOCMBIC:
			if (MSCDIALIN(dev))	/* don't let dialins drop DTR */
				(void) mscmctl(dev, *(int *)data & TIOCM_DTR, DMBIC);
			else
				(void) mscmctl(dev, *(int *)data, DMBIC);
			break;
      
		case TIOCMGET:
			*(int *)data = mscmctl(dev, 0, DMGET);
			break;
      
		case TIOCGFLAGS:
			*(int *)data = SWFLAGS(dev);
			break;

		case TIOCSFLAGS:
			error = suser(p->p_ucred, &p->p_acflag);
			if (error != 0)
				return(EPERM);
			msc->openflags = *(int *)data;
			/* only allow valid flags */
			msc->openflags &=
			     (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | TIOCFLAG_CRTSCTS);
			break;

		default:
			return (ENOTTY);
	}

	return (0);
}

int
mscparam(tp, t)
	register struct tty *tp;
	register struct termios *t;
{
	register int cflag = t->c_cflag;
	struct mscdevice *msc;
	volatile struct mscstatus *ms;
	int s, slot;
	int ospeed = ttspeedtab(t->c_ospeed, mscspeedtab);
  
	/* get the device structure */
	slot = MSCSLOT(tp->t_dev);

	if (slot >= MSCSLOTS)
		return ENXIO;

	msc = &mscdev[slot];

	if (!msc->active)
		return ENXIO;

	ms = &msc->board->Status[msc->port];

	/* check requested parameters */
	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
		return (EINVAL);

	/* and copy to tty */
	tp->t_ispeed = t->c_ispeed;
	tp->t_ospeed = t->c_ospeed;
	tp->t_cflag = cflag;
  
	/* hang up if baud is zero */
	if (t->c_ospeed == 0) {
		if (!MSCDIALIN(tp->t_dev))  /* don't let dialins drop DTR */
			(void) mscmctl(tp->t_dev, 0, DMSET);
	} else {
		/* set the baud rate */
		s = spltty();
		ms->Param = (ms->Param & ~MSCPARAM_BaudMask) | ospeed | MSCPARAM_RcvBaud;

		/*
		 * Make sure any previous hangup is undone, ie.  reenable DTR.
		 * also mscmctl will cause the speed to be set
		 */
		(void) mscmctl (tp->t_dev, TIOCM_DTR | TIOCM_RTS, DMSET);

		splx(s);
	}
  
	return(0);
}


/*
 *	Jukka's code initializes alot of stuff that other drivers don't
 *	I'm including it here so that this code is a common set of work
 *	done by both of us. rfh
 */
int
mschwiflow(tp, flag)
	struct tty *tp;
	int flag;
{

/* Rob's version */
#if 1
	if (flag)
		mscmctl( tp->t_dev, TIOCM_RTS, DMBIC);	/* Clear/Lower RTS */
	else
		mscmctl( tp->t_dev, TIOCM_RTS, DMBIS);	/* Set/Raise RTS */

#else	/* Jukka's version */

	int s, slot;
	struct mscdevice *msc;
	volatile struct mscstatus *ms;

	/* get the device structure */
	slot = MSCSLOT(tp->t_dev);
	if (slot >= MSCSLOTS)
		return ENXIO;
	msc = &mscdev[slot];
	if (!msc->active)
		return ENXIO;
	ms = &msc->board->Status[msc->port];

	/* Well, we should really _do_ something here, but the 65c02 code
	 * manages the RTS signal on its own now, so...  This will probably
	 * change in the future.
	 */
#endif
	return 1;
}

void
mscstart(tp)
	register struct tty *tp;
{
	register int cc;
	register char *cp;
	register int mhead;
	int s, slot;
	struct mscdevice *msc;
	volatile struct mscstatus *ms;
	volatile char *mob;
	int hiwat = 0;
	int maxout;

	if (! (tp->t_state & TS_ISOPEN))
		return;

	slot = MSCSLOT(tp->t_dev);

#if 0
	printf("starting msc%d\n", slot);
#endif

	s = spltty();

	/* don't start if explicitly stopped */
	if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP)) 
		goto out;

	/* wake up if below low water */
	cc = tp->t_outq.c_cc;

	if (cc <= tp->t_lowat) {
		if (tp->t_state & TS_ASLEEP) {
			tp->t_state &= ~TS_ASLEEP;
			wakeup((caddr_t)&tp->t_outq);
		}
		selwakeup(&tp->t_wsel);
	}

	/* don't bother if no characters or busy */
	if (cc == 0 || (tp->t_state & TS_BUSY))
		goto out;

	/*
	 * Limit the amount of output we do in one burst
	 */
	msc = &mscdev[slot];
	ms = &msc->board->Status[msc->port];
	mhead = ms->OutHead;
	maxout = mhead - ms->OutTail;

	if (maxout < 0)
		maxout += IOBUFLEN;

	maxout = IOBUFLEN - 1 - maxout;

	if (cc >= maxout) {
		hiwat++;
		cc = maxout;
	}

	cc = q_to_b (&tp->t_outq, msc->tmpbuf, cc);

	if (cc > 0) {
		tp->t_state |= TS_BUSY;

		mob = &msc->board->OutBuf[msc->port][0];
		cp = &msc->tmpbuf[0];
      
		/* enable output */
		ms->OutDisable = FALSE;

#if 0
		msc->tmpbuf[cc] = 0;
		printf("sending '%s'\n", msctmpbuf);
#endif

		/* send the first char across to reduce latency */
		mob[mhead++] = *cp++;
		mhead &= IOBUFLENMASK;
		ms->OutHead = mhead;
		cc--;

		/* copy the rest of the chars across quickly */
		while (cc > 0) {
			mob[mhead++] = *cp++;
			mhead &= IOBUFLENMASK;
			cc--;
		}
		ms->OutHead = mhead;

		/* leave the device busy if we've filled the buffer */
		if (!hiwat)
			tp->t_state &= ~TS_BUSY;
	}

out:  
	splx(s);
}
 
/* XXX */
/*
 * Stop output on a line.
 */
/*ARGSUSED*/
int
mscstop(tp, flag)
	register struct tty *tp;
	int flag;			/* defaulted to int anyway */
{
	register int s;
#if 0
	struct mscdevice *msc;
	volatile struct mscstatus *ms;
#endif

	s = spltty();
	if (tp->t_state & TS_BUSY) {
		if ((tp->t_state & TS_TTSTOP) == 0) {
			tp->t_state |= TS_FLUSH;
#if 0
			msc = &mscdev[MSCSLOT(tp->t_dev)];
			ms = &msc->board->Status[msc->port];
			printf("stopped output on msc%d\n", MSCSLOT(tp->t_dev));
			ms->OutDisable = TRUE;
#endif
		}
	}
	splx(s);
	return 0;
}
 
/*
 * bits can be: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, TIOCM_CD, TIOCM_RI, TIOCM_DSR
 */
int
mscmctl(dev, bits, how)
	dev_t dev;
	int bits, how;
{
	struct mscdevice *msc;
	volatile struct mscstatus *ms;
	int slot;
	int s;
	u_char newcmd;
	int OldFlags;

	/* get the device structure */
	slot = MSCSLOT(dev);

	if (slot >= MSCSLOTS)
		return ENXIO;

	msc = &mscdev[slot];

	if (!msc->active)
		return ENXIO;

	s = spltty();

	if (how != DMGET) {
		OldFlags = msc->flags;
		bits &= TIOCM_DTR | TIOCM_RTS;	/* can only modify DTR and RTS */

		switch (how) {
		    case DMSET:
			msc->flags = (bits | (msc->flags & ~(TIOCM_DTR | TIOCM_RTS)));
			break;
      
		    case DMBIC:
			msc->flags &= ~bits;
			break;
      
		    case DMBIS:
			msc->flags |= bits;
			break;
		}

		/* modify modem control state */
		ms = &msc->board->Status[msc->port];

		if (msc->flags & TIOCM_RTS)	/* was bits & */
			newcmd = MSCCMD_RTSOn;
		else			/* this doesn't actually work now */
			newcmd = MSCCMD_RTSOff;

		if (msc->flags & TIOCM_DTR)	/* was bits & */
			newcmd |= MSCCMD_Enable;

		ms->Command = (ms->Command & (~MSCCMD_RTSMask & ~MSCCMD_Enable)) | newcmd;
		ms->Setup = TRUE;

		/* if we've dropped DTR, bitbucket any pending output */
		if ( (OldFlags & TIOCM_DTR) && ((bits & TIOCM_DTR) == 0))
			ms->OutFlush = TRUE;
	}

	bits = msc->flags;

	splx(s);
  
	return(bits);
}

struct tty *
msctty(dev)
	dev_t dev;
{
	return(msc_tty[MSCTTY(dev)]);
}

/*
 * Load JM's freely redistributable A2232 6502c code. Let turbo detector
 * run for a while too.
 */

int
mscinitcard(zap)
	struct zbus_args *zap;
{
	int bcount;
	short start;
	u_char *from;
	volatile u_char *to;
	volatile struct mscmemory *mlm;

	mlm = (volatile struct mscmemory *)zap->va;	
	(void)mlm->Enable6502Reset;

	/* copy the code across to the board */
	to = (u_char *)mlm;
	from = msc6502code; bcount = sizeof(msc6502code) - 2;
	start = *(short *)from; from += sizeof(start);
	to += start;

	while(bcount--) *to++ = *from++;

	mlm->Common.Crystal = MSC_UNKNOWN;	/* use automatic speed check */

	/* start 6502 running */
	(void)mlm->ResetBoard;

	/* wait until speed detector has finished */
	for (bcount = 0; bcount < 200; bcount++) {
		delay(10000);
		if (mlm->Common.Crystal)
			break;
	}

	return(0);
}

#endif  /* NMSC > 0 */