xref: /csrg-svn/sys/vax/uba/dh.c (revision 4831)
1*4831Swnj /*	dh.c	4.39	81/11/08	*/
213Sbill 
31934Swnj #include "dh.h"
42643Swnj #if NDH > 0
513Sbill /*
62479Swnj  * DH-11/DM-11 driver
713Sbill  */
82730Swnj #include "bk.h"
913Sbill #include "../h/param.h"
1013Sbill #include "../h/conf.h"
1113Sbill #include "../h/dir.h"
1213Sbill #include "../h/user.h"
1313Sbill #include "../h/tty.h"
1413Sbill #include "../h/map.h"
1513Sbill #include "../h/pte.h"
162395Swnj #include "../h/buf.h"
172566Swnj #include "../h/vm.h"
182974Swnj #include "../h/ubareg.h"
192974Swnj #include "../h/ubavar.h"
20113Sbill #include "../h/bk.h"
211561Sbill #include "../h/clist.h"
222468Swnj #include "../h/file.h"
2313Sbill 
242468Swnj /*
252479Swnj  * Definition of the driver for the auto-configuration program.
262479Swnj  * There is one definition for the dh and one for the dm.
272468Swnj  */
282605Swnj int	dhprobe(), dhattach(), dhrint(), dhxint();
292974Swnj struct	uba_device *dhinfo[NDH];
302395Swnj u_short	dhstd[] = { 0 };
312395Swnj struct	uba_driver dhdriver =
322605Swnj 	{ dhprobe, 0, dhattach, 0, dhstd, "dh", dhinfo };
332395Swnj 
342605Swnj int	dmprobe(), dmattach(), dmintr();
352974Swnj struct	uba_device *dminfo[NDH];
362479Swnj u_short	dmstd[] = { 0 };
372479Swnj struct	uba_driver dmdriver =
382605Swnj 	{ dmprobe, 0, dmattach, 0, dmstd, "dm", dminfo };
3913Sbill 
402479Swnj struct dhdevice
412479Swnj {
422479Swnj 	union {
432479Swnj 		short	dhcsr;		/* control-status register */
442479Swnj 		char	dhcsrl;		/* low byte for line select */
452479Swnj 	} un;
462479Swnj 	short	dhrcr;			/* receive character register */
472479Swnj 	short	dhlpr;			/* line parameter register */
482479Swnj 	u_short dhcar;			/* current address register */
492479Swnj 	short	dhbcr;			/* byte count register */
502479Swnj 	u_short	dhbar;			/* buffer active register */
512479Swnj 	short	dhbreak;		/* break control register */
522479Swnj 	short	dhsilo;			/* silo status register */
532479Swnj };
5413Sbill 
552456Swnj /* Bits in dhcsr */
562456Swnj #define	DH_TI	0100000		/* transmit interrupt */
572456Swnj #define	DH_SI	0040000		/* storage interrupt */
582456Swnj #define	DH_TIE	0020000		/* transmit interrupt enable */
592456Swnj #define	DH_SIE	0010000		/* storage interrupt enable */
602456Swnj #define	DH_MC	0004000		/* master clear */
612456Swnj #define	DH_NXM	0002000		/* non-existant memory */
622456Swnj #define	DH_MM	0001000		/* maintenance mode */
632456Swnj #define	DH_CNI	0000400		/* clear non-existant memory interrupt */
642456Swnj #define	DH_RI	0000200		/* receiver interrupt */
652456Swnj #define	DH_RIE	0000100		/* receiver interrupt enable */
6613Sbill 
672479Swnj /* Bits in dhlpr */
682479Swnj #define	BITS6	01
692479Swnj #define	BITS7	02
702479Swnj #define	BITS8	03
712479Swnj #define	TWOSB	04
722479Swnj #define	PENABLE	020
732479Swnj /* DEC manuals incorrectly say this bit causes generation of even parity. */
742479Swnj #define	OPAR	040
752479Swnj #define	HDUPLX	040000
762479Swnj 
772456Swnj #define	DH_IE	(DH_TIE|DH_SIE|DH_RIE)
782456Swnj 
792456Swnj /* Bits in dhrcr */
802479Swnj #define	DH_PE		0010000		/* parity error */
812479Swnj #define	DH_FE		0020000		/* framing error */
822479Swnj #define	DH_DO		0040000		/* data overrun */
832456Swnj 
842479Swnj struct dmdevice
852479Swnj {
862479Swnj 	short	dmcsr;		/* control status register */
872479Swnj 	short	dmlstat;	/* line status register */
882479Swnj 	short	dmpad1[2];
892479Swnj };
902479Swnj 
912479Swnj /* bits in dm csr */
922479Swnj #define	DM_RF		0100000		/* ring flag */
932479Swnj #define	DM_CF		0040000		/* carrier flag */
942479Swnj #define	DM_CTS		0020000		/* clear to send */
952479Swnj #define	DM_SRF		0010000		/* secondary receive flag */
962479Swnj #define	DM_CS		0004000		/* clear scan */
972479Swnj #define	DM_CM		0002000		/* clear multiplexor */
982479Swnj #define	DM_MM		0001000		/* maintenance mode */
992479Swnj #define	DM_STP		0000400		/* step */
1002479Swnj #define	DM_DONE		0000200		/* scanner is done */
1012479Swnj #define	DM_IE		0000100		/* interrupt enable */
1022479Swnj #define	DM_SE		0000040		/* scan enable */
1032479Swnj #define	DM_BUSY		0000020		/* scan busy */
1042479Swnj 
1052479Swnj /* bits in dm lsr */
1062479Swnj #define	DML_RNG		0000200		/* ring */
1072479Swnj #define	DML_CAR		0000100		/* carrier detect */
1082479Swnj #define	DML_CTS		0000040		/* clear to send */
1092479Swnj #define	DML_SR		0000020		/* secondary receive */
1102479Swnj #define	DML_ST		0000010		/* secondary transmit */
1112479Swnj #define	DML_RTS		0000004		/* request to send */
1122479Swnj #define	DML_DTR		0000002		/* data terminal ready */
1132479Swnj #define	DML_LE		0000001		/* line enable */
1142479Swnj 
1153792Swnj #define	DML_ON		(DML_DTR|DML_RTS|DML_LE)
1162479Swnj #define	DML_OFF		(DML_LE)
1172479Swnj 
11813Sbill /*
1192479Swnj  * Local variables for the driver
12013Sbill  */
1212643Swnj short	dhsar[NDH];			/* software copy of last bar */
1222643Swnj short	dhsoftCAR[NDH];
12313Sbill 
1242643Swnj struct	tty dh11[NDH*16];
1252643Swnj int	ndh11	= NDH*16;
1262479Swnj int	dhact;				/* mask of active dh's */
1272479Swnj int	dhstart(), ttrstrt();
12813Sbill 
1292479Swnj /*
1302479Swnj  * The clist space is mapped by the driver onto each UNIBUS.
1312479Swnj  * The UBACVT macro converts a clist space address for unibus uban
1322479Swnj  * into an i/o space address for the DMA routine.
1332479Swnj  */
1342479Swnj int	dh_ubinfo[MAXNUBA];		/* info about allocated unibus map */
1352479Swnj int	cbase[MAXNUBA];			/* base address in unibus map */
1362479Swnj #define	UBACVT(x, uban)		(cbase[uban] + ((x)-(char *)cfree))
13713Sbill 
1382456Swnj /*
1392456Swnj  * Routine for configuration to force a dh to interrupt.
1402456Swnj  * Set to transmit at 9600 baud, and cause a transmitter interrupt.
1412456Swnj  */
1422468Swnj /*ARGSUSED*/
1432605Swnj dhprobe(reg)
1442395Swnj 	caddr_t reg;
1452395Swnj {
1462468Swnj 	register int br, cvec;		/* these are ``value-result'' */
1472479Swnj 	register struct dhdevice *dhaddr = (struct dhdevice *)reg;
1482395Swnj 
1492605Swnj #ifdef lint
1502605Swnj 	br = 0; cvec = br; br = cvec;
1512605Swnj #endif
1522696Swnj #ifndef notdef
1532566Swnj 	dhaddr->un.dhcsr = DH_RIE|DH_MM|DH_RI;
1543441Sroot 	DELAY(25);
1552566Swnj 	dhaddr->un.dhcsr = 0;
1562566Swnj #else
1572456Swnj 	dhaddr->un.dhcsr = DH_TIE;
1582456Swnj 	DELAY(5);
1592456Swnj 	dhaddr->dhlpr = (B9600 << 10) | (B9600 << 6) | BITS7|PENABLE;
1602421Skre 	dhaddr->dhbcr = -1;
1612456Swnj 	dhaddr->dhcar = 0;
1622421Skre 	dhaddr->dhbar = 1;
1632456Swnj 	DELAY(100000);		/* wait 1/10'th of a sec for interrupt */
1642421Skre 	dhaddr->un.dhcsr = 0;
1652456Swnj 	if (cvec && cvec != 0x200)
1662456Swnj 		cvec -= 4;		/* transmit -> receive */
1672482Swnj #endif
1682456Swnj 	return (1);
1692395Swnj }
1702395Swnj 
1712456Swnj /*
1722605Swnj  * Routine called to attach a dh.
1732456Swnj  */
1742605Swnj dhattach(ui)
1752974Swnj 	struct uba_device *ui;
1762395Swnj {
1772395Swnj 
1782566Swnj 	dhsoftCAR[ui->ui_unit] = ui->ui_flags;
1792395Swnj }
1802395Swnj 
18113Sbill /*
1822479Swnj  * Configuration routine to cause a dm to interrupt.
1832479Swnj  */
1842605Swnj dmprobe(reg)
1852605Swnj 	caddr_t reg;
1862479Swnj {
1872479Swnj 	register int br, vec;			/* value-result */
1882605Swnj 	register struct dmdevice *dmaddr = (struct dmdevice *)reg;
1892479Swnj 
1902605Swnj #ifdef lint
1913101Swnj 	br = 0; vec = br; br = vec;
1922605Swnj #endif
1932479Swnj 	dmaddr->dmcsr = DM_DONE|DM_IE;
1942479Swnj 	DELAY(20);
1952479Swnj 	dmaddr->dmcsr = 0;
1962605Swnj 	return (1);
1972479Swnj }
1982479Swnj 
1992605Swnj /*ARGSUSED*/
2002605Swnj dmattach(ui)
2012974Swnj 	struct uba_device *ui;
2022479Swnj {
2032479Swnj 
2042479Swnj 	/* no local state to set up */
2052479Swnj }
2062479Swnj 
2072479Swnj /*
2082468Swnj  * Open a DH11 line, mapping the clist onto the uba if this
2092468Swnj  * is the first dh on this uba.  Turn on this dh if this is
2102468Swnj  * the first use of it.  Also do a dmopen to wait for carrier.
21113Sbill  */
21213Sbill /*ARGSUSED*/
21313Sbill dhopen(dev, flag)
2142395Swnj 	dev_t dev;
21513Sbill {
21613Sbill 	register struct tty *tp;
2172395Swnj 	register int unit, dh;
2182479Swnj 	register struct dhdevice *addr;
2192974Swnj 	register struct uba_device *ui;
22013Sbill 	int s;
22113Sbill 
2222395Swnj 	unit = minor(dev);
2232395Swnj 	dh = unit >> 4;
2242643Swnj 	if (unit >= NDH*16 || (ui = dhinfo[dh])== 0 || ui->ui_alive == 0) {
22513Sbill 		u.u_error = ENXIO;
22613Sbill 		return;
22713Sbill 	}
2282395Swnj 	tp = &dh11[unit];
2292468Swnj 	if (tp->t_state&XCLUDE && u.u_uid!=0) {
2302468Swnj 		u.u_error = EBUSY;
2312468Swnj 		return;
2322468Swnj 	}
2332479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
23413Sbill 	tp->t_addr = (caddr_t)addr;
23513Sbill 	tp->t_oproc = dhstart;
23613Sbill 	tp->t_state |= WOPEN;
2372468Swnj 	/*
2382468Swnj 	 * While setting up state for this uba and this dh,
2392468Swnj 	 * block uba resets which can clear the state.
2402468Swnj 	 */
2412468Swnj 	s = spl5();
2422421Skre 	if (dh_ubinfo[ui->ui_ubanum] == 0) {
243717Sbill 		/* 512+ is a kludge to try to get around a hardware problem */
2442395Swnj 		dh_ubinfo[ui->ui_ubanum] =
2452421Skre 		    uballoc(ui->ui_ubanum, (caddr_t)cfree,
2462770Swnj 			512+nclist*sizeof(struct cblock), 0);
2472456Swnj 		cbase[ui->ui_ubanum] = dh_ubinfo[ui->ui_ubanum]&0x3ffff;
24813Sbill 	}
2492456Swnj 	if ((dhact&(1<<dh)) == 0) {
2502456Swnj 		addr->un.dhcsr |= DH_IE;
2512468Swnj 		dhact |= (1<<dh);
2522456Swnj 		addr->dhsilo = 16;
2532456Swnj 	}
25413Sbill 	splx(s);
2552468Swnj 	/*
2562468Swnj 	 * If this is first open, initialze tty state to default.
2572468Swnj 	 */
25813Sbill 	if ((tp->t_state&ISOPEN) == 0) {
25913Sbill 		ttychars(tp);
260168Sbill 		if (tp->t_ispeed == 0) {
2612456Swnj 			tp->t_ispeed = B300;
2622456Swnj 			tp->t_ospeed = B300;
263168Sbill 			tp->t_flags = ODDP|EVENP|ECHO;
264168Sbill 		}
2652395Swnj 		dhparam(unit);
26613Sbill 	}
2672468Swnj 	/*
2682468Swnj 	 * Wait for carrier, then process line discipline specific open.
2692468Swnj 	 */
27013Sbill 	dmopen(dev);
2712395Swnj 	(*linesw[tp->t_line].l_open)(dev, tp);
27213Sbill }
27313Sbill 
27413Sbill /*
2752468Swnj  * Close a DH11 line, turning off the DM11.
27613Sbill  */
27713Sbill /*ARGSUSED*/
27813Sbill dhclose(dev, flag)
2792395Swnj 	dev_t dev;
2802395Swnj 	int flag;
28113Sbill {
28213Sbill 	register struct tty *tp;
2832395Swnj 	register unit;
28413Sbill 
2852395Swnj 	unit = minor(dev);
2862395Swnj 	tp = &dh11[unit];
28713Sbill 	(*linesw[tp->t_line].l_close)(tp);
2882479Swnj 	((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
28913Sbill 	if (tp->t_state&HUPCLS || (tp->t_state&ISOPEN)==0)
2902479Swnj 		dmctl(unit, DML_OFF, DMSET);
29113Sbill 	ttyclose(tp);
29213Sbill }
29313Sbill 
29413Sbill dhread(dev)
2952395Swnj 	dev_t dev;
29613Sbill {
2972395Swnj 	register struct tty *tp;
29813Sbill 
2992395Swnj 	tp = &dh11[minor(dev)];
30013Sbill 	(*linesw[tp->t_line].l_read)(tp);
30113Sbill }
30213Sbill 
30313Sbill dhwrite(dev)
3042395Swnj 	dev_t dev;
30513Sbill {
3062395Swnj 	register struct tty *tp;
30713Sbill 
3082395Swnj 	tp = &dh11[minor(dev)];
30913Sbill 	(*linesw[tp->t_line].l_write)(tp);
31013Sbill }
31113Sbill 
31213Sbill /*
31313Sbill  * DH11 receiver interrupt.
31413Sbill  */
3152395Swnj dhrint(dh)
3162395Swnj 	int dh;
31713Sbill {
31813Sbill 	register struct tty *tp;
3192395Swnj 	register c;
3202479Swnj 	register struct dhdevice *addr;
321117Sbill 	register struct tty *tp0;
3222974Swnj 	register struct uba_device *ui;
3232924Swnj 	int overrun = 0;
32413Sbill 
3252395Swnj 	ui = dhinfo[dh];
3262479Swnj 	if (ui == 0 || ui->ui_alive == 0)
3272479Swnj 		return;
3282479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
3292468Swnj 	tp0 = &dh11[dh<<4];
3302468Swnj 	/*
3312468Swnj 	 * Loop fetching characters from the silo for this
3322468Swnj 	 * dh until there are no more in the silo.
3332468Swnj 	 */
3342468Swnj 	while ((c = addr->dhrcr) < 0) {
3352468Swnj 		tp = tp0 + ((c>>8)&0xf);
3362468Swnj 		if ((tp->t_state&ISOPEN)==0) {
33713Sbill 			wakeup((caddr_t)tp);
33813Sbill 			continue;
33913Sbill 		}
3402468Swnj 		if (c & DH_PE)
34113Sbill 			if ((tp->t_flags&(EVENP|ODDP))==EVENP
34213Sbill 			 || (tp->t_flags&(EVENP|ODDP))==ODDP )
34313Sbill 				continue;
3442924Swnj 		if ((c & DH_DO) && overrun == 0) {
3452924Swnj 			printf("dh%d: silo overflow\n", dh);
3462924Swnj 			overrun = 1;
3472924Swnj 		}
3482468Swnj 		if (c & DH_FE)
3492468Swnj 			/*
3502468Swnj 			 * At framing error (break) generate
3512468Swnj 			 * a null (in raw mode, for getty), or a
3522468Swnj 			 * interrupt (in cooked/cbreak mode).
3532468Swnj 			 */
35413Sbill 			if (tp->t_flags&RAW)
3552468Swnj 				c = 0;
35613Sbill 			else
357184Sbill 				c = tun.t_intrc;
3582730Swnj #if NBK > 0
359139Sbill 		if (tp->t_line == NETLDISC) {
360117Sbill 			c &= 0177;
361168Sbill 			BKINPUT(c, tp);
362117Sbill 		} else
3632730Swnj #endif
3642468Swnj 			(*linesw[tp->t_line].l_rint)(c, tp);
36513Sbill 	}
36613Sbill }
36713Sbill 
36813Sbill /*
3692468Swnj  * Ioctl for DH11.
37013Sbill  */
37113Sbill /*ARGSUSED*/
37213Sbill dhioctl(dev, cmd, addr, flag)
3732395Swnj 	caddr_t addr;
37413Sbill {
37513Sbill 	register struct tty *tp;
3762395Swnj 	register unit = minor(dev);
37713Sbill 
3782395Swnj 	tp = &dh11[unit];
379113Sbill 	cmd = (*linesw[tp->t_line].l_ioctl)(tp, cmd, addr);
3802468Swnj 	if (cmd == 0)
381113Sbill 		return;
3821895Swnj 	if (ttioctl(tp, cmd, addr, flag)) {
3832468Swnj 		if (cmd==TIOCSETP || cmd==TIOCSETN)
3842395Swnj 			dhparam(unit);
385168Sbill 	} else switch(cmd) {
386168Sbill 	case TIOCSBRK:
3872479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak |= 1<<(unit&017);
388168Sbill 		break;
389168Sbill 	case TIOCCBRK:
3902479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
391168Sbill 		break;
392168Sbill 	case TIOCSDTR:
3932479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIS);
394168Sbill 		break;
395168Sbill 	case TIOCCDTR:
3962479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIC);
397168Sbill 		break;
398168Sbill 	default:
39913Sbill 		u.u_error = ENOTTY;
400168Sbill 	}
40113Sbill }
40213Sbill 
40313Sbill /*
40413Sbill  * Set parameters from open or stty into the DH hardware
40513Sbill  * registers.
40613Sbill  */
4072395Swnj dhparam(unit)
4082395Swnj 	register int unit;
40913Sbill {
41013Sbill 	register struct tty *tp;
4112479Swnj 	register struct dhdevice *addr;
4122395Swnj 	register int lpar;
413300Sbill 	int s;
41413Sbill 
4152395Swnj 	tp = &dh11[unit];
4162479Swnj 	addr = (struct dhdevice *)tp->t_addr;
4172468Swnj 	/*
4182468Swnj 	 * Block interrupts so parameters will be set
4192468Swnj 	 * before the line interrupts.
4202468Swnj 	 */
421300Sbill 	s = spl5();
4222468Swnj 	addr->un.dhcsrl = (unit&0xf) | DH_IE;
42313Sbill 	if ((tp->t_ispeed)==0) {
42413Sbill 		tp->t_state |= HUPCLS;
4252479Swnj 		dmctl(unit, DML_OFF, DMSET);
42613Sbill 		return;
42713Sbill 	}
4282395Swnj 	lpar = ((tp->t_ospeed)<<10) | ((tp->t_ispeed)<<6);
4292468Swnj 	if ((tp->t_ispeed) == B134)
4302395Swnj 		lpar |= BITS6|PENABLE|HDUPLX;
4312312Skre 	else if ((tp->t_flags&RAW) || (tp->t_local&LLITOUT))
4322395Swnj 		lpar |= BITS8;
43313Sbill 	else
4342395Swnj 		lpar |= BITS7|PENABLE;
43513Sbill 	if ((tp->t_flags&EVENP) == 0)
4362395Swnj 		lpar |= OPAR;
4372468Swnj 	if ((tp->t_ospeed) == B110)
4382395Swnj 		lpar |= TWOSB;
4392395Swnj 	addr->dhlpr = lpar;
440300Sbill 	splx(s);
44113Sbill }
44213Sbill 
44313Sbill /*
44413Sbill  * DH11 transmitter interrupt.
44513Sbill  * Restart each line which used to be active but has
44613Sbill  * terminated transmission since the last interrupt.
44713Sbill  */
4482395Swnj dhxint(dh)
4492395Swnj 	int dh;
45013Sbill {
45113Sbill 	register struct tty *tp;
4522479Swnj 	register struct dhdevice *addr;
45313Sbill 	short ttybit, bar, *sbar;
4542974Swnj 	register struct uba_device *ui;
4552468Swnj 	register int unit;
4562605Swnj 	u_short cntr;
45713Sbill 
4582395Swnj 	ui = dhinfo[dh];
4592479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
4602456Swnj 	if (addr->un.dhcsr & DH_NXM) {
4612456Swnj 		addr->un.dhcsr |= DH_CNI;
4622924Swnj 		printf("dh%d: NXM\n", dh);
463105Sbill 	}
4642395Swnj 	sbar = &dhsar[dh];
46513Sbill 	bar = *sbar & ~addr->dhbar;
4662395Swnj 	unit = dh * 16; ttybit = 1;
4672468Swnj 	addr->un.dhcsr &= (short)~DH_TI;
4682468Swnj 	for (; bar; unit++, ttybit <<= 1) {
4692468Swnj 		if (bar & ttybit) {
47013Sbill 			*sbar &= ~ttybit;
47113Sbill 			bar &= ~ttybit;
4722395Swnj 			tp = &dh11[unit];
473113Sbill 			tp->t_state &= ~BUSY;
474113Sbill 			if (tp->t_state&FLUSH)
475113Sbill 				tp->t_state &= ~FLUSH;
476113Sbill 			else {
4772456Swnj 				addr->un.dhcsrl = (unit&017)|DH_IE;
4782468Swnj 				/*
4792468Swnj 				 * Do arithmetic in a short to make up
4802468Swnj 				 * for lost 16&17 bits.
4812468Swnj 				 */
4822605Swnj 				cntr = addr->dhcar -
4832468Swnj 				    UBACVT(tp->t_outq.c_cf, ui->ui_ubanum);
4843101Swnj 				ndflush(&tp->t_outq, (int)cntr);
485113Sbill 			}
486113Sbill 			if (tp->t_line)
48713Sbill 				(*linesw[tp->t_line].l_start)(tp);
488113Sbill 			else
48913Sbill 				dhstart(tp);
49013Sbill 		}
49113Sbill 	}
49213Sbill }
49313Sbill 
49413Sbill /*
49513Sbill  * Start (restart) transmission on the given DH11 line.
49613Sbill  */
49713Sbill dhstart(tp)
4982395Swnj 	register struct tty *tp;
49913Sbill {
5002479Swnj 	register struct dhdevice *addr;
5012468Swnj 	register int car, dh, unit, nch;
5022395Swnj 	int s;
50313Sbill 
5042468Swnj 	unit = minor(tp->t_dev);
5052468Swnj 	dh = unit >> 4;
5062468Swnj 	unit &= 0xf;
5072479Swnj 	addr = (struct dhdevice *)tp->t_addr;
5082468Swnj 
50913Sbill 	/*
5102468Swnj 	 * Must hold interrupts in following code to prevent
5112468Swnj 	 * state of the tp from changing.
51213Sbill 	 */
51313Sbill 	s = spl5();
5142468Swnj 	/*
5152468Swnj 	 * If it's currently active, or delaying, no need to do anything.
5162468Swnj 	 */
51713Sbill 	if (tp->t_state&(TIMEOUT|BUSY|TTSTOP))
51813Sbill 		goto out;
5192468Swnj 	/*
5202468Swnj 	 * If there are sleepers, and output has drained below low
5212468Swnj 	 * water mark, wake up the sleepers.
5222468Swnj 	 */
5232395Swnj 	if ((tp->t_state&ASLEEP) && tp->t_outq.c_cc<=TTLOWAT(tp)) {
52413Sbill 		tp->t_state &= ~ASLEEP;
525*4831Swnj 		wakeup((caddr_t)&tp->t_outq);
52613Sbill 	}
5272468Swnj 	/*
5282468Swnj 	 * Now restart transmission unless the output queue is
5292468Swnj 	 * empty.
5302468Swnj 	 */
53113Sbill 	if (tp->t_outq.c_cc == 0)
53213Sbill 		goto out;
5333703Sroot 	if (tp->t_flags&RAW || tp->t_local&LLITOUT)
53413Sbill 		nch = ndqb(&tp->t_outq, 0);
5352395Swnj 	else {
53613Sbill 		nch = ndqb(&tp->t_outq, 0200);
5372468Swnj 		/*
5382468Swnj 		 * If first thing on queue is a delay process it.
5392468Swnj 		 */
54013Sbill 		if (nch == 0) {
54113Sbill 			nch = getc(&tp->t_outq);
5422468Swnj 			timeout(ttrstrt, (caddr_t)tp, (nch&0x7f)+6);
54313Sbill 			tp->t_state |= TIMEOUT;
54413Sbill 			goto out;
54513Sbill 		}
54613Sbill 	}
5472468Swnj 	/*
5482468Swnj 	 * If characters to transmit, restart transmission.
5492468Swnj 	 */
55013Sbill 	if (nch) {
5512468Swnj 		car = UBACVT(tp->t_outq.c_cf, dhinfo[dh]->ui_ubanum);
5522468Swnj 		addr->un.dhcsrl = unit|((car>>12)&0x30)|DH_IE;
5533586Sroot 		/*
5543586Sroot 		 * The following nonsense with short word
5553586Sroot 		 * is to make sure the dhbar |= word below
5563586Sroot 		 * is done with an interlocking bisw2 instruction.
5573586Sroot 		 */
5583586Sroot 		{ short word = 1 << unit;
5593586Sroot 		dhsar[dh] |= word;
5602468Swnj 		addr->dhcar = car;
56113Sbill 		addr->dhbcr = -nch;
5623586Sroot 		addr->dhbar |= word;
5633586Sroot 		}
56413Sbill 		tp->t_state |= BUSY;
56513Sbill 	}
5662395Swnj out:
56713Sbill 	splx(s);
56813Sbill }
56913Sbill 
57013Sbill /*
5712468Swnj  * Stop output on a line, e.g. for ^S/^Q or output flush.
57213Sbill  */
57313Sbill /*ARGSUSED*/
57413Sbill dhstop(tp, flag)
5752468Swnj 	register struct tty *tp;
57613Sbill {
5772479Swnj 	register struct dhdevice *addr;
5782395Swnj 	register int unit, s;
57913Sbill 
5802479Swnj 	addr = (struct dhdevice *)tp->t_addr;
5812468Swnj 	/*
5822468Swnj 	 * Block input/output interrupts while messing with state.
5832468Swnj 	 */
5842468Swnj 	s = spl5();
585113Sbill 	if (tp->t_state & BUSY) {
5862468Swnj 		/*
5872468Swnj 		 * Device is transmitting; stop output
5882468Swnj 		 * by selecting the line and setting the byte
5892468Swnj 		 * count to -1.  We will clean up later
5902468Swnj 		 * by examining the address where the dh stopped.
5912468Swnj 		 */
5922395Swnj 		unit = minor(tp->t_dev);
5932456Swnj 		addr->un.dhcsrl = (unit&017) | DH_IE;
59413Sbill 		if ((tp->t_state&TTSTOP)==0)
59513Sbill 			tp->t_state |= FLUSH;
596113Sbill 		addr->dhbcr = -1;
597113Sbill 	}
59813Sbill 	splx(s);
59913Sbill }
60013Sbill 
601168Sbill /*
602280Sbill  * Reset state of driver if UBA reset was necessary.
603280Sbill  * Reset the csrl and lpr registers on open lines, and
604280Sbill  * restart transmitters.
605280Sbill  */
6062395Swnj dhreset(uban)
6072468Swnj 	int uban;
608280Sbill {
6092395Swnj 	register int dh, unit;
610280Sbill 	register struct tty *tp;
6112974Swnj 	register struct uba_device *ui;
6122421Skre 	int i;
613280Sbill 
6142421Skre 	if (dh_ubinfo[uban] == 0)
6152421Skre 		return;
6162421Skre 	ubarelse(uban, &dh_ubinfo[uban]);
6172421Skre 	dh_ubinfo[uban] = uballoc(uban, (caddr_t)cfree,
6182770Swnj 	    512+nclist*sizeof (struct cblock), 0);
6192421Skre 	cbase[uban] = dh_ubinfo[uban]&0x3ffff;
6202395Swnj 	dh = 0;
6212643Swnj 	for (dh = 0; dh < NDH; dh++) {
6222421Skre 		ui = dhinfo[dh];
6232421Skre 		if (ui == 0 || ui->ui_alive == 0 || ui->ui_ubanum != uban)
6242421Skre 			continue;
6252924Swnj 		printf(" dh%d", dh);
6262479Swnj 		((struct dhdevice *)ui->ui_addr)->un.dhcsr |= DH_IE;
6272479Swnj 		((struct dhdevice *)ui->ui_addr)->dhsilo = 16;
6282421Skre 		unit = dh * 16;
6292421Skre 		for (i = 0; i < 16; i++) {
6302421Skre 			tp = &dh11[unit];
6312421Skre 			if (tp->t_state & (ISOPEN|WOPEN)) {
6322421Skre 				dhparam(unit);
6332479Swnj 				dmctl(unit, DML_ON, DMSET);
6342421Skre 				tp->t_state &= ~BUSY;
6352421Skre 				dhstart(tp);
6362421Skre 			}
6372421Skre 			unit++;
638300Sbill 		}
639300Sbill 	}
640300Sbill 	dhtimer();
641280Sbill }
6422395Swnj 
6432468Swnj /*
6442468Swnj  * At software clock interrupt time or after a UNIBUS reset
6452468Swnj  * empty all the dh silos.
6462468Swnj  */
6472456Swnj dhtimer()
6482456Swnj {
6492456Swnj 	register int dh;
6502456Swnj 
6512643Swnj 	for (dh = 0; dh < NDH; dh++)
6522456Swnj 		dhrint(dh);
6532456Swnj }
6542456Swnj 
6552468Swnj /*
6562479Swnj  * Turn on the line associated with dh dev.
6572468Swnj  */
6582468Swnj dmopen(dev)
6592468Swnj 	dev_t dev;
6602468Swnj {
6612468Swnj 	register struct tty *tp;
6622468Swnj 	register struct dmdevice *addr;
6632974Swnj 	register struct uba_device *ui;
6642468Swnj 	register int unit;
6652468Swnj 	register int dm;
6663792Swnj 	int s;
6672468Swnj 
6682468Swnj 	unit = minor(dev);
6692479Swnj 	dm = unit >> 4;
6702468Swnj 	tp = &dh11[unit];
6712566Swnj 	unit &= 0xf;
6722643Swnj 	if (dm >= NDH || (ui = dminfo[dm]) == 0 || ui->ui_alive == 0 ||
6732566Swnj 	    (dhsoftCAR[dm]&(1<<unit))) {
6742468Swnj 		tp->t_state |= CARR_ON;
6752468Swnj 		return;
6762468Swnj 	}
6772468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
6783792Swnj 	s = spl5();
6792479Swnj 	addr->dmcsr &= ~DM_SE;
6802479Swnj 	while (addr->dmcsr & DM_BUSY)
6812468Swnj 		;
6822566Swnj 	addr->dmcsr = unit;
6832479Swnj 	addr->dmlstat = DML_ON;
6842479Swnj 	if (addr->dmlstat&DML_CAR)
6852468Swnj 		tp->t_state |= CARR_ON;
6863792Swnj 	addr->dmcsr = DM_IE|DM_SE;
6872468Swnj 	while ((tp->t_state&CARR_ON)==0)
6882468Swnj 		sleep((caddr_t)&tp->t_rawq, TTIPRI);
6893792Swnj 	splx(s);
6902468Swnj }
6912468Swnj 
6922468Swnj /*
6932468Swnj  * Dump control bits into the DM registers.
6942468Swnj  */
6952468Swnj dmctl(dev, bits, how)
6962468Swnj 	dev_t dev;
6972468Swnj 	int bits, how;
6982468Swnj {
6992974Swnj 	register struct uba_device *ui;
7002468Swnj 	register struct dmdevice *addr;
7012468Swnj 	register int unit, s;
7022468Swnj 	int dm;
7032468Swnj 
7042468Swnj 	unit = minor(dev);
7052468Swnj 	dm = unit >> 4;
7062468Swnj 	if ((ui = dminfo[dm]) == 0 || ui->ui_alive == 0)
7072468Swnj 		return;
7082468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7092468Swnj 	s = spl5();
7102479Swnj 	addr->dmcsr &= ~DM_SE;
7112479Swnj 	while (addr->dmcsr & DM_BUSY)
7122468Swnj 		;
7132468Swnj 	addr->dmcsr = unit & 0xf;
7142468Swnj 	switch(how) {
7152468Swnj 	case DMSET:
7162468Swnj 		addr->dmlstat = bits;
7172468Swnj 		break;
7182468Swnj 	case DMBIS:
7192468Swnj 		addr->dmlstat |= bits;
7202468Swnj 		break;
7212468Swnj 	case DMBIC:
7222468Swnj 		addr->dmlstat &= ~bits;
7232468Swnj 		break;
7242468Swnj 	}
7253792Swnj 	addr->dmcsr = DM_IE|DM_SE;
7262468Swnj 	splx(s);
7272468Swnj }
7282468Swnj 
7292468Swnj /*
7302468Swnj  * DM11 interrupt; deal with carrier transitions.
7312468Swnj  */
7322468Swnj dmintr(dm)
7332468Swnj 	register int dm;
7342468Swnj {
7352974Swnj 	register struct uba_device *ui;
7362468Swnj 	register struct tty *tp;
7372468Swnj 	register struct dmdevice *addr;
7382468Swnj 
7392468Swnj 	ui = dminfo[dm];
7402479Swnj 	if (ui == 0)
7412479Swnj 		return;
7422468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7433997Sroot 	if (addr->dmcsr&DM_DONE) {
7443997Sroot 		if (addr->dmcsr&DM_CF) {
7453997Sroot 			tp = &dh11[(dm<<4)+(addr->dmcsr&0xf)];
7463997Sroot 			wakeup((caddr_t)&tp->t_rawq);
7472468Swnj 			if ((tp->t_state&WOPEN)==0 &&
7483997Sroot 			    (tp->t_local&LMDMBUF)) {
7493997Sroot 				if (addr->dmlstat & DML_CAR) {
7503997Sroot 					tp->t_state &= ~TTSTOP;
7513997Sroot 					ttstart(tp);
7523997Sroot 				} else if ((tp->t_state&TTSTOP) == 0) {
7533997Sroot 					tp->t_state |= TTSTOP;
7543997Sroot 					dhstop(tp, 0);
7553997Sroot 				}
7563997Sroot 			} else if ((addr->dmlstat&DML_CAR)==0) {
7573997Sroot 				if ((tp->t_state&WOPEN)==0 &&
7583997Sroot 				    (tp->t_local&LNOHANG)==0) {
7593997Sroot 					gsignal(tp->t_pgrp, SIGHUP);
7603997Sroot 					gsignal(tp->t_pgrp, SIGCONT);
7613997Sroot 					addr->dmlstat = 0;
7623997Sroot 					flushtty(tp, FREAD|FWRITE);
7633997Sroot 				}
7643997Sroot 				tp->t_state &= ~CARR_ON;
7653997Sroot 			} else
7663997Sroot 				tp->t_state |= CARR_ON;
7673997Sroot 		}
7683997Sroot 		addr->dmcsr = DM_IE|DM_SE;
7692468Swnj 	}
7702468Swnj }
7712625Swnj #endif
772