xref: /csrg-svn/sys/vax/uba/dh.c (revision 3586)
1*3586Sroot /*	dh.c	4.32	81/04/22	*/
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"
221786Sbill #include "../h/mx.h"
232468Swnj #include "../h/file.h"
2413Sbill 
252468Swnj /*
262479Swnj  * Definition of the driver for the auto-configuration program.
272479Swnj  * There is one definition for the dh and one for the dm.
282468Swnj  */
292605Swnj int	dhprobe(), dhattach(), dhrint(), dhxint();
302974Swnj struct	uba_device *dhinfo[NDH];
312395Swnj u_short	dhstd[] = { 0 };
322395Swnj struct	uba_driver dhdriver =
332605Swnj 	{ dhprobe, 0, dhattach, 0, dhstd, "dh", dhinfo };
342395Swnj 
352605Swnj int	dmprobe(), dmattach(), dmintr();
362974Swnj struct	uba_device *dminfo[NDH];
372479Swnj u_short	dmstd[] = { 0 };
382479Swnj struct	uba_driver dmdriver =
392605Swnj 	{ dmprobe, 0, dmattach, 0, dmstd, "dm", dminfo };
4013Sbill 
412479Swnj struct dhdevice
422479Swnj {
432479Swnj 	union {
442479Swnj 		short	dhcsr;		/* control-status register */
452479Swnj 		char	dhcsrl;		/* low byte for line select */
462479Swnj 	} un;
472479Swnj 	short	dhrcr;			/* receive character register */
482479Swnj 	short	dhlpr;			/* line parameter register */
492479Swnj 	u_short dhcar;			/* current address register */
502479Swnj 	short	dhbcr;			/* byte count register */
512479Swnj 	u_short	dhbar;			/* buffer active register */
522479Swnj 	short	dhbreak;		/* break control register */
532479Swnj 	short	dhsilo;			/* silo status register */
542479Swnj };
5513Sbill 
562456Swnj /* Bits in dhcsr */
572456Swnj #define	DH_TI	0100000		/* transmit interrupt */
582456Swnj #define	DH_SI	0040000		/* storage interrupt */
592456Swnj #define	DH_TIE	0020000		/* transmit interrupt enable */
602456Swnj #define	DH_SIE	0010000		/* storage interrupt enable */
612456Swnj #define	DH_MC	0004000		/* master clear */
622456Swnj #define	DH_NXM	0002000		/* non-existant memory */
632456Swnj #define	DH_MM	0001000		/* maintenance mode */
642456Swnj #define	DH_CNI	0000400		/* clear non-existant memory interrupt */
652456Swnj #define	DH_RI	0000200		/* receiver interrupt */
662456Swnj #define	DH_RIE	0000100		/* receiver interrupt enable */
6713Sbill 
682479Swnj /* Bits in dhlpr */
692479Swnj #define	BITS6	01
702479Swnj #define	BITS7	02
712479Swnj #define	BITS8	03
722479Swnj #define	TWOSB	04
732479Swnj #define	PENABLE	020
742479Swnj /* DEC manuals incorrectly say this bit causes generation of even parity. */
752479Swnj #define	OPAR	040
762479Swnj #define	HDUPLX	040000
772479Swnj 
783441Sroot #if NBK == 0
793441Sroot #define	DH_IE	(DH_TIE|DH_RIE)
803441Sroot #else
812456Swnj #define	DH_IE	(DH_TIE|DH_SIE|DH_RIE)
823441Sroot #endif
832456Swnj 
842456Swnj /* Bits in dhrcr */
852479Swnj #define	DH_PE		0010000		/* parity error */
862479Swnj #define	DH_FE		0020000		/* framing error */
872479Swnj #define	DH_DO		0040000		/* data overrun */
882456Swnj 
892479Swnj struct dmdevice
902479Swnj {
912479Swnj 	short	dmcsr;		/* control status register */
922479Swnj 	short	dmlstat;	/* line status register */
932479Swnj 	short	dmpad1[2];
942479Swnj };
952479Swnj 
962479Swnj /* bits in dm csr */
972479Swnj #define	DM_RF		0100000		/* ring flag */
982479Swnj #define	DM_CF		0040000		/* carrier flag */
992479Swnj #define	DM_CTS		0020000		/* clear to send */
1002479Swnj #define	DM_SRF		0010000		/* secondary receive flag */
1012479Swnj #define	DM_CS		0004000		/* clear scan */
1022479Swnj #define	DM_CM		0002000		/* clear multiplexor */
1032479Swnj #define	DM_MM		0001000		/* maintenance mode */
1042479Swnj #define	DM_STP		0000400		/* step */
1052479Swnj #define	DM_DONE		0000200		/* scanner is done */
1062479Swnj #define	DM_IE		0000100		/* interrupt enable */
1072479Swnj #define	DM_SE		0000040		/* scan enable */
1082479Swnj #define	DM_BUSY		0000020		/* scan busy */
1092479Swnj 
1102479Swnj /* bits in dm lsr */
1112479Swnj #define	DML_RNG		0000200		/* ring */
1122479Swnj #define	DML_CAR		0000100		/* carrier detect */
1132479Swnj #define	DML_CTS		0000040		/* clear to send */
1142479Swnj #define	DML_SR		0000020		/* secondary receive */
1152479Swnj #define	DML_ST		0000010		/* secondary transmit */
1162479Swnj #define	DML_RTS		0000004		/* request to send */
1172479Swnj #define	DML_DTR		0000002		/* data terminal ready */
1182479Swnj #define	DML_LE		0000001		/* line enable */
1192479Swnj 
1202479Swnj #define	DML_ON		(DML_DTR|DML_LE)
1212479Swnj #define	DML_OFF		(DML_LE)
1222479Swnj 
12313Sbill /*
1242479Swnj  * Local variables for the driver
12513Sbill  */
1262643Swnj short	dhsar[NDH];			/* software copy of last bar */
1272643Swnj short	dhsoftCAR[NDH];
12813Sbill 
1292643Swnj struct	tty dh11[NDH*16];
1302643Swnj int	ndh11	= NDH*16;
1312479Swnj int	dhact;				/* mask of active dh's */
1322479Swnj int	dhstart(), ttrstrt();
13313Sbill 
1342479Swnj /*
1352479Swnj  * The clist space is mapped by the driver onto each UNIBUS.
1362479Swnj  * The UBACVT macro converts a clist space address for unibus uban
1372479Swnj  * into an i/o space address for the DMA routine.
1382479Swnj  */
1392479Swnj int	dh_ubinfo[MAXNUBA];		/* info about allocated unibus map */
1402479Swnj int	cbase[MAXNUBA];			/* base address in unibus map */
1412479Swnj #define	UBACVT(x, uban)		(cbase[uban] + ((x)-(char *)cfree))
14213Sbill 
1432456Swnj /*
1442456Swnj  * Routine for configuration to force a dh to interrupt.
1452456Swnj  * Set to transmit at 9600 baud, and cause a transmitter interrupt.
1462456Swnj  */
1472468Swnj /*ARGSUSED*/
1482605Swnj dhprobe(reg)
1492395Swnj 	caddr_t reg;
1502395Swnj {
1512468Swnj 	register int br, cvec;		/* these are ``value-result'' */
1522479Swnj 	register struct dhdevice *dhaddr = (struct dhdevice *)reg;
1532395Swnj 
1542605Swnj #ifdef lint
1552605Swnj 	br = 0; cvec = br; br = cvec;
1562605Swnj #endif
1572696Swnj #ifndef notdef
1582566Swnj 	dhaddr->un.dhcsr = DH_RIE|DH_MM|DH_RI;
1593441Sroot 	DELAY(25);
1602566Swnj 	dhaddr->un.dhcsr = 0;
1612566Swnj #else
1622456Swnj 	dhaddr->un.dhcsr = DH_TIE;
1632456Swnj 	DELAY(5);
1642456Swnj 	dhaddr->dhlpr = (B9600 << 10) | (B9600 << 6) | BITS7|PENABLE;
1652421Skre 	dhaddr->dhbcr = -1;
1662456Swnj 	dhaddr->dhcar = 0;
1672421Skre 	dhaddr->dhbar = 1;
1682456Swnj 	DELAY(100000);		/* wait 1/10'th of a sec for interrupt */
1692421Skre 	dhaddr->un.dhcsr = 0;
1702456Swnj 	if (cvec && cvec != 0x200)
1712456Swnj 		cvec -= 4;		/* transmit -> receive */
1722482Swnj #endif
1732456Swnj 	return (1);
1742395Swnj }
1752395Swnj 
1762456Swnj /*
1772605Swnj  * Routine called to attach a dh.
1782456Swnj  */
1792605Swnj dhattach(ui)
1802974Swnj 	struct uba_device *ui;
1812395Swnj {
1822395Swnj 
1832566Swnj 	dhsoftCAR[ui->ui_unit] = ui->ui_flags;
1842395Swnj }
1852395Swnj 
18613Sbill /*
1872479Swnj  * Configuration routine to cause a dm to interrupt.
1882479Swnj  */
1892605Swnj dmprobe(reg)
1902605Swnj 	caddr_t reg;
1912479Swnj {
1922479Swnj 	register int br, vec;			/* value-result */
1932605Swnj 	register struct dmdevice *dmaddr = (struct dmdevice *)reg;
1942479Swnj 
1952605Swnj #ifdef lint
1963101Swnj 	br = 0; vec = br; br = vec;
1972605Swnj #endif
1982479Swnj 	dmaddr->dmcsr = DM_DONE|DM_IE;
1992479Swnj 	DELAY(20);
2002479Swnj 	dmaddr->dmcsr = 0;
2012605Swnj 	return (1);
2022479Swnj }
2032479Swnj 
2042605Swnj /*ARGSUSED*/
2052605Swnj dmattach(ui)
2062974Swnj 	struct uba_device *ui;
2072479Swnj {
2082479Swnj 
2092479Swnj 	/* no local state to set up */
2102479Swnj }
2112479Swnj 
2122479Swnj /*
2132468Swnj  * Open a DH11 line, mapping the clist onto the uba if this
2142468Swnj  * is the first dh on this uba.  Turn on this dh if this is
2152468Swnj  * the first use of it.  Also do a dmopen to wait for carrier.
21613Sbill  */
21713Sbill /*ARGSUSED*/
21813Sbill dhopen(dev, flag)
2192395Swnj 	dev_t dev;
22013Sbill {
22113Sbill 	register struct tty *tp;
2222395Swnj 	register int unit, dh;
2232479Swnj 	register struct dhdevice *addr;
2242974Swnj 	register struct uba_device *ui;
22513Sbill 	int s;
22613Sbill 
2272395Swnj 	unit = minor(dev);
2282395Swnj 	dh = unit >> 4;
2292643Swnj 	if (unit >= NDH*16 || (ui = dhinfo[dh])== 0 || ui->ui_alive == 0) {
23013Sbill 		u.u_error = ENXIO;
23113Sbill 		return;
23213Sbill 	}
2332395Swnj 	tp = &dh11[unit];
2342468Swnj 	if (tp->t_state&XCLUDE && u.u_uid!=0) {
2352468Swnj 		u.u_error = EBUSY;
2362468Swnj 		return;
2372468Swnj 	}
2382479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
23913Sbill 	tp->t_addr = (caddr_t)addr;
24013Sbill 	tp->t_oproc = dhstart;
24113Sbill 	tp->t_iproc = NULL;
24213Sbill 	tp->t_state |= WOPEN;
2432468Swnj 	/*
2442468Swnj 	 * While setting up state for this uba and this dh,
2452468Swnj 	 * block uba resets which can clear the state.
2462468Swnj 	 */
2472468Swnj 	s = spl5();
2482421Skre 	if (dh_ubinfo[ui->ui_ubanum] == 0) {
249717Sbill 		/* 512+ is a kludge to try to get around a hardware problem */
2502395Swnj 		dh_ubinfo[ui->ui_ubanum] =
2512421Skre 		    uballoc(ui->ui_ubanum, (caddr_t)cfree,
2522770Swnj 			512+nclist*sizeof(struct cblock), 0);
2532456Swnj 		cbase[ui->ui_ubanum] = dh_ubinfo[ui->ui_ubanum]&0x3ffff;
25413Sbill 	}
2552456Swnj 	if ((dhact&(1<<dh)) == 0) {
2562456Swnj 		addr->un.dhcsr |= DH_IE;
2572468Swnj 		dhact |= (1<<dh);
2582456Swnj 		addr->dhsilo = 16;
2592456Swnj 	}
26013Sbill 	splx(s);
2612468Swnj 	/*
2622468Swnj 	 * If this is first open, initialze tty state to default.
2632468Swnj 	 */
26413Sbill 	if ((tp->t_state&ISOPEN) == 0) {
26513Sbill 		ttychars(tp);
266168Sbill 		if (tp->t_ispeed == 0) {
2672456Swnj 			tp->t_ispeed = B300;
2682456Swnj 			tp->t_ospeed = B300;
269168Sbill 			tp->t_flags = ODDP|EVENP|ECHO;
270168Sbill 		}
2712395Swnj 		dhparam(unit);
27213Sbill 	}
2732468Swnj 	/*
2742468Swnj 	 * Wait for carrier, then process line discipline specific open.
2752468Swnj 	 */
27613Sbill 	dmopen(dev);
2772395Swnj 	(*linesw[tp->t_line].l_open)(dev, tp);
27813Sbill }
27913Sbill 
28013Sbill /*
2812468Swnj  * Close a DH11 line, turning off the DM11.
28213Sbill  */
28313Sbill /*ARGSUSED*/
28413Sbill dhclose(dev, flag)
2852395Swnj 	dev_t dev;
2862395Swnj 	int flag;
28713Sbill {
28813Sbill 	register struct tty *tp;
2892395Swnj 	register unit;
29013Sbill 
2912395Swnj 	unit = minor(dev);
2922395Swnj 	tp = &dh11[unit];
29313Sbill 	(*linesw[tp->t_line].l_close)(tp);
2942479Swnj 	((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
29513Sbill 	if (tp->t_state&HUPCLS || (tp->t_state&ISOPEN)==0)
2962479Swnj 		dmctl(unit, DML_OFF, DMSET);
29713Sbill 	ttyclose(tp);
29813Sbill }
29913Sbill 
30013Sbill dhread(dev)
3012395Swnj 	dev_t dev;
30213Sbill {
3032395Swnj 	register struct tty *tp;
30413Sbill 
3052395Swnj 	tp = &dh11[minor(dev)];
30613Sbill 	(*linesw[tp->t_line].l_read)(tp);
30713Sbill }
30813Sbill 
30913Sbill dhwrite(dev)
3102395Swnj 	dev_t dev;
31113Sbill {
3122395Swnj 	register struct tty *tp;
31313Sbill 
3142395Swnj 	tp = &dh11[minor(dev)];
31513Sbill 	(*linesw[tp->t_line].l_write)(tp);
31613Sbill }
31713Sbill 
31813Sbill /*
31913Sbill  * DH11 receiver interrupt.
32013Sbill  */
3212395Swnj dhrint(dh)
3222395Swnj 	int dh;
32313Sbill {
32413Sbill 	register struct tty *tp;
3252395Swnj 	register c;
3262479Swnj 	register struct dhdevice *addr;
327117Sbill 	register struct tty *tp0;
3282974Swnj 	register struct uba_device *ui;
3292924Swnj 	int overrun = 0;
33013Sbill 
3312395Swnj 	ui = dhinfo[dh];
3322479Swnj 	if (ui == 0 || ui->ui_alive == 0)
3332479Swnj 		return;
3342479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
3352468Swnj 	tp0 = &dh11[dh<<4];
3362468Swnj 	/*
3372468Swnj 	 * Loop fetching characters from the silo for this
3382468Swnj 	 * dh until there are no more in the silo.
3392468Swnj 	 */
3402468Swnj 	while ((c = addr->dhrcr) < 0) {
3412468Swnj 		tp = tp0 + ((c>>8)&0xf);
3422468Swnj 		if ((tp->t_state&ISOPEN)==0) {
34313Sbill 			wakeup((caddr_t)tp);
34413Sbill 			continue;
34513Sbill 		}
3462468Swnj 		if (c & DH_PE)
34713Sbill 			if ((tp->t_flags&(EVENP|ODDP))==EVENP
34813Sbill 			 || (tp->t_flags&(EVENP|ODDP))==ODDP )
34913Sbill 				continue;
3502924Swnj 		if ((c & DH_DO) && overrun == 0) {
3512924Swnj 			printf("dh%d: silo overflow\n", dh);
3522924Swnj 			overrun = 1;
3532924Swnj 		}
3542468Swnj 		if (c & DH_FE)
3552468Swnj 			/*
3562468Swnj 			 * At framing error (break) generate
3572468Swnj 			 * a null (in raw mode, for getty), or a
3582468Swnj 			 * interrupt (in cooked/cbreak mode).
3592468Swnj 			 */
36013Sbill 			if (tp->t_flags&RAW)
3612468Swnj 				c = 0;
36213Sbill 			else
363184Sbill 				c = tun.t_intrc;
3642730Swnj #if NBK > 0
365139Sbill 		if (tp->t_line == NETLDISC) {
366117Sbill 			c &= 0177;
367168Sbill 			BKINPUT(c, tp);
368117Sbill 		} else
3692730Swnj #endif
3702468Swnj 			(*linesw[tp->t_line].l_rint)(c, tp);
37113Sbill 	}
37213Sbill }
37313Sbill 
37413Sbill /*
3752468Swnj  * Ioctl for DH11.
37613Sbill  */
37713Sbill /*ARGSUSED*/
37813Sbill dhioctl(dev, cmd, addr, flag)
3792395Swnj 	caddr_t addr;
38013Sbill {
38113Sbill 	register struct tty *tp;
3822395Swnj 	register unit = minor(dev);
38313Sbill 
3842395Swnj 	tp = &dh11[unit];
385113Sbill 	cmd = (*linesw[tp->t_line].l_ioctl)(tp, cmd, addr);
3862468Swnj 	if (cmd == 0)
387113Sbill 		return;
3881895Swnj 	if (ttioctl(tp, cmd, addr, flag)) {
3892468Swnj 		if (cmd==TIOCSETP || cmd==TIOCSETN)
3902395Swnj 			dhparam(unit);
391168Sbill 	} else switch(cmd) {
392168Sbill 	case TIOCSBRK:
3932479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak |= 1<<(unit&017);
394168Sbill 		break;
395168Sbill 	case TIOCCBRK:
3962479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
397168Sbill 		break;
398168Sbill 	case TIOCSDTR:
3992479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIS);
400168Sbill 		break;
401168Sbill 	case TIOCCDTR:
4022479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIC);
403168Sbill 		break;
404168Sbill 	default:
40513Sbill 		u.u_error = ENOTTY;
406168Sbill 	}
40713Sbill }
40813Sbill 
40913Sbill /*
41013Sbill  * Set parameters from open or stty into the DH hardware
41113Sbill  * registers.
41213Sbill  */
4132395Swnj dhparam(unit)
4142395Swnj 	register int unit;
41513Sbill {
41613Sbill 	register struct tty *tp;
4172479Swnj 	register struct dhdevice *addr;
4182395Swnj 	register int lpar;
419300Sbill 	int s;
42013Sbill 
4212395Swnj 	tp = &dh11[unit];
4222479Swnj 	addr = (struct dhdevice *)tp->t_addr;
4232468Swnj 	/*
4242468Swnj 	 * Block interrupts so parameters will be set
4252468Swnj 	 * before the line interrupts.
4262468Swnj 	 */
427300Sbill 	s = spl5();
4282468Swnj 	addr->un.dhcsrl = (unit&0xf) | DH_IE;
42913Sbill 	if ((tp->t_ispeed)==0) {
43013Sbill 		tp->t_state |= HUPCLS;
4312479Swnj 		dmctl(unit, DML_OFF, DMSET);
43213Sbill 		return;
43313Sbill 	}
4342395Swnj 	lpar = ((tp->t_ospeed)<<10) | ((tp->t_ispeed)<<6);
4352468Swnj 	if ((tp->t_ispeed) == B134)
4362395Swnj 		lpar |= BITS6|PENABLE|HDUPLX;
4372312Skre 	else if ((tp->t_flags&RAW) || (tp->t_local&LLITOUT))
4382395Swnj 		lpar |= BITS8;
43913Sbill 	else
4402395Swnj 		lpar |= BITS7|PENABLE;
44113Sbill 	if ((tp->t_flags&EVENP) == 0)
4422395Swnj 		lpar |= OPAR;
4432468Swnj 	if ((tp->t_ospeed) == B110)
4442395Swnj 		lpar |= TWOSB;
4452395Swnj 	addr->dhlpr = lpar;
446300Sbill 	splx(s);
44713Sbill }
44813Sbill 
44913Sbill /*
45013Sbill  * DH11 transmitter interrupt.
45113Sbill  * Restart each line which used to be active but has
45213Sbill  * terminated transmission since the last interrupt.
45313Sbill  */
4542395Swnj dhxint(dh)
4552395Swnj 	int dh;
45613Sbill {
45713Sbill 	register struct tty *tp;
4582479Swnj 	register struct dhdevice *addr;
45913Sbill 	short ttybit, bar, *sbar;
4602974Swnj 	register struct uba_device *ui;
4612468Swnj 	register int unit;
4622605Swnj 	u_short cntr;
46313Sbill 
4642395Swnj 	ui = dhinfo[dh];
4652479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
4662456Swnj 	if (addr->un.dhcsr & DH_NXM) {
4672456Swnj 		addr->un.dhcsr |= DH_CNI;
4682924Swnj 		printf("dh%d: NXM\n", dh);
469105Sbill 	}
4702395Swnj 	sbar = &dhsar[dh];
47113Sbill 	bar = *sbar & ~addr->dhbar;
4722395Swnj 	unit = dh * 16; ttybit = 1;
4732468Swnj 	addr->un.dhcsr &= (short)~DH_TI;
4742468Swnj 	for (; bar; unit++, ttybit <<= 1) {
4752468Swnj 		if (bar & ttybit) {
47613Sbill 			*sbar &= ~ttybit;
47713Sbill 			bar &= ~ttybit;
4782395Swnj 			tp = &dh11[unit];
479113Sbill 			tp->t_state &= ~BUSY;
480113Sbill 			if (tp->t_state&FLUSH)
481113Sbill 				tp->t_state &= ~FLUSH;
482113Sbill 			else {
4832456Swnj 				addr->un.dhcsrl = (unit&017)|DH_IE;
4842468Swnj 				/*
4852468Swnj 				 * Do arithmetic in a short to make up
4862468Swnj 				 * for lost 16&17 bits.
4872468Swnj 				 */
4882605Swnj 				cntr = addr->dhcar -
4892468Swnj 				    UBACVT(tp->t_outq.c_cf, ui->ui_ubanum);
4903101Swnj 				ndflush(&tp->t_outq, (int)cntr);
491113Sbill 			}
492113Sbill 			if (tp->t_line)
49313Sbill 				(*linesw[tp->t_line].l_start)(tp);
494113Sbill 			else
49513Sbill 				dhstart(tp);
49613Sbill 		}
49713Sbill 	}
49813Sbill }
49913Sbill 
50013Sbill /*
50113Sbill  * Start (restart) transmission on the given DH11 line.
50213Sbill  */
50313Sbill dhstart(tp)
5042395Swnj 	register struct tty *tp;
50513Sbill {
5062479Swnj 	register struct dhdevice *addr;
5072468Swnj 	register int car, dh, unit, nch;
5082395Swnj 	int s;
50913Sbill 
5102468Swnj 	unit = minor(tp->t_dev);
5112468Swnj 	dh = unit >> 4;
5122468Swnj 	unit &= 0xf;
5132479Swnj 	addr = (struct dhdevice *)tp->t_addr;
5142468Swnj 
51513Sbill 	/*
5162468Swnj 	 * Must hold interrupts in following code to prevent
5172468Swnj 	 * state of the tp from changing.
51813Sbill 	 */
51913Sbill 	s = spl5();
5202468Swnj 	/*
5212468Swnj 	 * If it's currently active, or delaying, no need to do anything.
5222468Swnj 	 */
52313Sbill 	if (tp->t_state&(TIMEOUT|BUSY|TTSTOP))
52413Sbill 		goto out;
5252468Swnj 	/*
5262468Swnj 	 * If there are sleepers, and output has drained below low
5272468Swnj 	 * water mark, wake up the sleepers.
5282468Swnj 	 */
5292395Swnj 	if ((tp->t_state&ASLEEP) && tp->t_outq.c_cc<=TTLOWAT(tp)) {
53013Sbill 		tp->t_state &= ~ASLEEP;
53113Sbill 		if (tp->t_chan)
532168Sbill 			mcstart(tp->t_chan, (caddr_t)&tp->t_outq);
533168Sbill 		else
53413Sbill 			wakeup((caddr_t)&tp->t_outq);
53513Sbill 	}
5362468Swnj 	/*
5372468Swnj 	 * Now restart transmission unless the output queue is
5382468Swnj 	 * empty.
5392468Swnj 	 */
54013Sbill 	if (tp->t_outq.c_cc == 0)
54113Sbill 		goto out;
5422395Swnj 	if (tp->t_flags & RAW)
54313Sbill 		nch = ndqb(&tp->t_outq, 0);
5442395Swnj 	else {
54513Sbill 		nch = ndqb(&tp->t_outq, 0200);
5462468Swnj 		/*
5472468Swnj 		 * If first thing on queue is a delay process it.
5482468Swnj 		 */
54913Sbill 		if (nch == 0) {
55013Sbill 			nch = getc(&tp->t_outq);
5512468Swnj 			timeout(ttrstrt, (caddr_t)tp, (nch&0x7f)+6);
55213Sbill 			tp->t_state |= TIMEOUT;
55313Sbill 			goto out;
55413Sbill 		}
55513Sbill 	}
5562468Swnj 	/*
5572468Swnj 	 * If characters to transmit, restart transmission.
5582468Swnj 	 */
55913Sbill 	if (nch) {
5602468Swnj 		car = UBACVT(tp->t_outq.c_cf, dhinfo[dh]->ui_ubanum);
5612468Swnj 		addr->un.dhcsrl = unit|((car>>12)&0x30)|DH_IE;
562*3586Sroot 		/*
563*3586Sroot 		 * The following nonsense with short word
564*3586Sroot 		 * is to make sure the dhbar |= word below
565*3586Sroot 		 * is done with an interlocking bisw2 instruction.
566*3586Sroot 		 */
567*3586Sroot 		{ short word = 1 << unit;
568*3586Sroot 		dhsar[dh] |= word;
5692468Swnj 		addr->dhcar = car;
57013Sbill 		addr->dhbcr = -nch;
571*3586Sroot 		addr->dhbar |= word;
572*3586Sroot 		}
57313Sbill 		tp->t_state |= BUSY;
57413Sbill 	}
5752395Swnj out:
57613Sbill 	splx(s);
57713Sbill }
57813Sbill 
57913Sbill /*
5802468Swnj  * Stop output on a line, e.g. for ^S/^Q or output flush.
58113Sbill  */
58213Sbill /*ARGSUSED*/
58313Sbill dhstop(tp, flag)
5842468Swnj 	register struct tty *tp;
58513Sbill {
5862479Swnj 	register struct dhdevice *addr;
5872395Swnj 	register int unit, s;
58813Sbill 
5892479Swnj 	addr = (struct dhdevice *)tp->t_addr;
5902468Swnj 	/*
5912468Swnj 	 * Block input/output interrupts while messing with state.
5922468Swnj 	 */
5932468Swnj 	s = spl5();
594113Sbill 	if (tp->t_state & BUSY) {
5952468Swnj 		/*
5962468Swnj 		 * Device is transmitting; stop output
5972468Swnj 		 * by selecting the line and setting the byte
5982468Swnj 		 * count to -1.  We will clean up later
5992468Swnj 		 * by examining the address where the dh stopped.
6002468Swnj 		 */
6012395Swnj 		unit = minor(tp->t_dev);
6022456Swnj 		addr->un.dhcsrl = (unit&017) | DH_IE;
60313Sbill 		if ((tp->t_state&TTSTOP)==0)
60413Sbill 			tp->t_state |= FLUSH;
605113Sbill 		addr->dhbcr = -1;
606113Sbill 	}
60713Sbill 	splx(s);
60813Sbill }
60913Sbill 
610168Sbill /*
611280Sbill  * Reset state of driver if UBA reset was necessary.
612280Sbill  * Reset the csrl and lpr registers on open lines, and
613280Sbill  * restart transmitters.
614280Sbill  */
6152395Swnj dhreset(uban)
6162468Swnj 	int uban;
617280Sbill {
6182395Swnj 	register int dh, unit;
619280Sbill 	register struct tty *tp;
6202974Swnj 	register struct uba_device *ui;
6212421Skre 	int i;
622280Sbill 
6232421Skre 	if (dh_ubinfo[uban] == 0)
6242421Skre 		return;
6252421Skre 	ubarelse(uban, &dh_ubinfo[uban]);
6262421Skre 	dh_ubinfo[uban] = uballoc(uban, (caddr_t)cfree,
6272770Swnj 	    512+nclist*sizeof (struct cblock), 0);
6282421Skre 	cbase[uban] = dh_ubinfo[uban]&0x3ffff;
6292395Swnj 	dh = 0;
6302643Swnj 	for (dh = 0; dh < NDH; dh++) {
6312421Skre 		ui = dhinfo[dh];
6322421Skre 		if (ui == 0 || ui->ui_alive == 0 || ui->ui_ubanum != uban)
6332421Skre 			continue;
6342924Swnj 		printf(" dh%d", dh);
6352479Swnj 		((struct dhdevice *)ui->ui_addr)->un.dhcsr |= DH_IE;
6362479Swnj 		((struct dhdevice *)ui->ui_addr)->dhsilo = 16;
6372421Skre 		unit = dh * 16;
6382421Skre 		for (i = 0; i < 16; i++) {
6392421Skre 			tp = &dh11[unit];
6402421Skre 			if (tp->t_state & (ISOPEN|WOPEN)) {
6412421Skre 				dhparam(unit);
6422479Swnj 				dmctl(unit, DML_ON, DMSET);
6432421Skre 				tp->t_state &= ~BUSY;
6442421Skre 				dhstart(tp);
6452421Skre 			}
6462421Skre 			unit++;
647300Sbill 		}
648300Sbill 	}
649300Sbill 	dhtimer();
650280Sbill }
6512395Swnj 
6522468Swnj /*
6532468Swnj  * At software clock interrupt time or after a UNIBUS reset
6542468Swnj  * empty all the dh silos.
6552468Swnj  */
6562456Swnj dhtimer()
6572456Swnj {
6582456Swnj 	register int dh;
6592456Swnj 
6602643Swnj 	for (dh = 0; dh < NDH; dh++)
6612456Swnj 		dhrint(dh);
6622456Swnj }
6632456Swnj 
6642468Swnj /*
6652479Swnj  * Turn on the line associated with dh dev.
6662468Swnj  */
6672468Swnj dmopen(dev)
6682468Swnj 	dev_t dev;
6692468Swnj {
6702468Swnj 	register struct tty *tp;
6712468Swnj 	register struct dmdevice *addr;
6722974Swnj 	register struct uba_device *ui;
6732468Swnj 	register int unit;
6742468Swnj 	register int dm;
6752468Swnj 
6762468Swnj 	unit = minor(dev);
6772479Swnj 	dm = unit >> 4;
6782468Swnj 	tp = &dh11[unit];
6792566Swnj 	unit &= 0xf;
6802643Swnj 	if (dm >= NDH || (ui = dminfo[dm]) == 0 || ui->ui_alive == 0 ||
6812566Swnj 	    (dhsoftCAR[dm]&(1<<unit))) {
6822468Swnj 		tp->t_state |= CARR_ON;
6832468Swnj 		return;
6842468Swnj 	}
6852468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
6863101Swnj 	(void) spl5();
6872479Swnj 	addr->dmcsr &= ~DM_SE;
6882479Swnj 	while (addr->dmcsr & DM_BUSY)
6892468Swnj 		;
6902566Swnj 	addr->dmcsr = unit;
6912479Swnj 	addr->dmlstat = DML_ON;
6922479Swnj 	if (addr->dmlstat&DML_CAR)
6932468Swnj 		tp->t_state |= CARR_ON;
6942479Swnj 	addr->dmcsr = DH_IE|DM_SE;
6952468Swnj 	while ((tp->t_state&CARR_ON)==0)
6962468Swnj 		sleep((caddr_t)&tp->t_rawq, TTIPRI);
6973101Swnj 	(void) spl0();
6982468Swnj }
6992468Swnj 
7002468Swnj /*
7012468Swnj  * Dump control bits into the DM registers.
7022468Swnj  */
7032468Swnj dmctl(dev, bits, how)
7042468Swnj 	dev_t dev;
7052468Swnj 	int bits, how;
7062468Swnj {
7072974Swnj 	register struct uba_device *ui;
7082468Swnj 	register struct dmdevice *addr;
7092468Swnj 	register int unit, s;
7102468Swnj 	int dm;
7112468Swnj 
7122468Swnj 	unit = minor(dev);
7132468Swnj 	dm = unit >> 4;
7142468Swnj 	if ((ui = dminfo[dm]) == 0 || ui->ui_alive == 0)
7152468Swnj 		return;
7162468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7172468Swnj 	s = spl5();
7182479Swnj 	addr->dmcsr &= ~DM_SE;
7192479Swnj 	while (addr->dmcsr & DM_BUSY)
7202468Swnj 		;
7212468Swnj 	addr->dmcsr = unit & 0xf;
7222468Swnj 	switch(how) {
7232468Swnj 	case DMSET:
7242468Swnj 		addr->dmlstat = bits;
7252468Swnj 		break;
7262468Swnj 	case DMBIS:
7272468Swnj 		addr->dmlstat |= bits;
7282468Swnj 		break;
7292468Swnj 	case DMBIC:
7302468Swnj 		addr->dmlstat &= ~bits;
7312468Swnj 		break;
7322468Swnj 	}
7332479Swnj 	addr->dmcsr = DH_IE|DM_SE;
7342468Swnj 	splx(s);
7352468Swnj }
7362468Swnj 
7372468Swnj /*
7382468Swnj  * DM11 interrupt; deal with carrier transitions.
7392468Swnj  */
7402468Swnj dmintr(dm)
7412468Swnj 	register int dm;
7422468Swnj {
7432974Swnj 	register struct uba_device *ui;
7442468Swnj 	register struct tty *tp;
7452468Swnj 	register struct dmdevice *addr;
7462468Swnj 
7472468Swnj 	ui = dminfo[dm];
7482479Swnj 	if (ui == 0)
7492479Swnj 		return;
7502468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7512479Swnj 	if (addr->dmcsr&DM_DONE && addr->dmcsr&DM_CF) {
7522468Swnj 		tp = &dh11[(dm<<4)+(addr->dmcsr&0xf)];
7532468Swnj 		wakeup((caddr_t)&tp->t_rawq);
7542468Swnj 		if ((tp->t_state&WOPEN)==0 &&
7552468Swnj 		    (tp->t_local&LMDMBUF)) {
7562479Swnj 			if (addr->dmlstat & DML_CAR) {
7572468Swnj 				tp->t_state &= ~TTSTOP;
7582468Swnj 				ttstart(tp);
7592468Swnj 			} else if ((tp->t_state&TTSTOP) == 0) {
7602468Swnj 				tp->t_state |= TTSTOP;
7612468Swnj 				dhstop(tp, 0);
7622468Swnj 			}
7632479Swnj 		} else if ((addr->dmlstat&DML_CAR)==0) {
7642468Swnj 			if ((tp->t_state&WOPEN)==0 &&
7652468Swnj 			    (tp->t_local&LNOHANG)==0) {
7662468Swnj 				gsignal(tp->t_pgrp, SIGHUP);
7672468Swnj 				gsignal(tp->t_pgrp, SIGCONT);
7682468Swnj 				addr->dmlstat = 0;
7692468Swnj 				flushtty(tp, FREAD|FWRITE);
7702468Swnj 			}
7712468Swnj 			tp->t_state &= ~CARR_ON;
7722468Swnj 		} else
7732468Swnj 			tp->t_state |= CARR_ON;
7742479Swnj 		addr->dmcsr = DH_IE|DM_SE;
7752468Swnj 	}
7762468Swnj }
7772625Swnj #endif
778