xref: /csrg-svn/sys/vax/uba/dh.c (revision 7629)
1*7629Ssam /*	dh.c	4.47	82/08/01	*/
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"
136185Ssam #include "../h/proc.h"
1413Sbill #include "../h/tty.h"
1513Sbill #include "../h/map.h"
1613Sbill #include "../h/pte.h"
172395Swnj #include "../h/buf.h"
182566Swnj #include "../h/vm.h"
192974Swnj #include "../h/ubareg.h"
202974Swnj #include "../h/ubavar.h"
21113Sbill #include "../h/bk.h"
221561Sbill #include "../h/clist.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 
566615Ssam #ifndef	PORTSELECTOR
576615Ssam #define	ISPEED	B300
586615Ssam #define	IFLAGS	(EVENP|ODDP|ECHO)
596615Ssam #else
606615Ssam #define	ISPEED	B4800
616615Ssam #define	IFLAGS	(EVENP|ODDP)
626615Ssam #endif
636615Ssam 
642456Swnj /* Bits in dhcsr */
652456Swnj #define	DH_TI	0100000		/* transmit interrupt */
662456Swnj #define	DH_SI	0040000		/* storage interrupt */
672456Swnj #define	DH_TIE	0020000		/* transmit interrupt enable */
682456Swnj #define	DH_SIE	0010000		/* storage interrupt enable */
692456Swnj #define	DH_MC	0004000		/* master clear */
702456Swnj #define	DH_NXM	0002000		/* non-existant memory */
712456Swnj #define	DH_MM	0001000		/* maintenance mode */
722456Swnj #define	DH_CNI	0000400		/* clear non-existant memory interrupt */
732456Swnj #define	DH_RI	0000200		/* receiver interrupt */
742456Swnj #define	DH_RIE	0000100		/* receiver interrupt enable */
7513Sbill 
762479Swnj /* Bits in dhlpr */
772479Swnj #define	BITS6	01
782479Swnj #define	BITS7	02
792479Swnj #define	BITS8	03
802479Swnj #define	TWOSB	04
812479Swnj #define	PENABLE	020
822479Swnj /* DEC manuals incorrectly say this bit causes generation of even parity. */
832479Swnj #define	OPAR	040
842479Swnj #define	HDUPLX	040000
852479Swnj 
862456Swnj #define	DH_IE	(DH_TIE|DH_SIE|DH_RIE)
872456Swnj 
882456Swnj /* Bits in dhrcr */
892479Swnj #define	DH_PE		0010000		/* parity error */
902479Swnj #define	DH_FE		0020000		/* framing error */
912479Swnj #define	DH_DO		0040000		/* data overrun */
922456Swnj 
932479Swnj struct dmdevice
942479Swnj {
952479Swnj 	short	dmcsr;		/* control status register */
962479Swnj 	short	dmlstat;	/* line status register */
972479Swnj 	short	dmpad1[2];
982479Swnj };
992479Swnj 
1002479Swnj /* bits in dm csr */
1012479Swnj #define	DM_RF		0100000		/* ring flag */
1022479Swnj #define	DM_CF		0040000		/* carrier flag */
1032479Swnj #define	DM_CTS		0020000		/* clear to send */
1042479Swnj #define	DM_SRF		0010000		/* secondary receive flag */
1052479Swnj #define	DM_CS		0004000		/* clear scan */
1062479Swnj #define	DM_CM		0002000		/* clear multiplexor */
1072479Swnj #define	DM_MM		0001000		/* maintenance mode */
1082479Swnj #define	DM_STP		0000400		/* step */
1092479Swnj #define	DM_DONE		0000200		/* scanner is done */
1102479Swnj #define	DM_IE		0000100		/* interrupt enable */
1112479Swnj #define	DM_SE		0000040		/* scan enable */
1122479Swnj #define	DM_BUSY		0000020		/* scan busy */
1132479Swnj 
1142479Swnj /* bits in dm lsr */
1152479Swnj #define	DML_RNG		0000200		/* ring */
1162479Swnj #define	DML_CAR		0000100		/* carrier detect */
1172479Swnj #define	DML_CTS		0000040		/* clear to send */
1182479Swnj #define	DML_SR		0000020		/* secondary receive */
1192479Swnj #define	DML_ST		0000010		/* secondary transmit */
1202479Swnj #define	DML_RTS		0000004		/* request to send */
1212479Swnj #define	DML_DTR		0000002		/* data terminal ready */
1222479Swnj #define	DML_LE		0000001		/* line enable */
1232479Swnj 
1243792Swnj #define	DML_ON		(DML_DTR|DML_RTS|DML_LE)
1252479Swnj #define	DML_OFF		(DML_LE)
1262479Swnj 
12713Sbill /*
1282479Swnj  * Local variables for the driver
12913Sbill  */
1302643Swnj short	dhsar[NDH];			/* software copy of last bar */
1312643Swnj short	dhsoftCAR[NDH];
13213Sbill 
1332643Swnj struct	tty dh11[NDH*16];
1342643Swnj int	ndh11	= NDH*16;
1352479Swnj int	dhact;				/* mask of active dh's */
1362479Swnj int	dhstart(), ttrstrt();
13713Sbill 
1382479Swnj /*
1392479Swnj  * The clist space is mapped by the driver onto each UNIBUS.
1402479Swnj  * The UBACVT macro converts a clist space address for unibus uban
1412479Swnj  * into an i/o space address for the DMA routine.
1422479Swnj  */
1432479Swnj int	dh_ubinfo[MAXNUBA];		/* info about allocated unibus map */
1442479Swnj int	cbase[MAXNUBA];			/* base address in unibus map */
1452479Swnj #define	UBACVT(x, uban)		(cbase[uban] + ((x)-(char *)cfree))
14613Sbill 
1472456Swnj /*
1482456Swnj  * Routine for configuration to force a dh to interrupt.
1492456Swnj  * Set to transmit at 9600 baud, and cause a transmitter interrupt.
1502456Swnj  */
1512468Swnj /*ARGSUSED*/
1522605Swnj dhprobe(reg)
1532395Swnj 	caddr_t reg;
1542395Swnj {
1552468Swnj 	register int br, cvec;		/* these are ``value-result'' */
1562479Swnj 	register struct dhdevice *dhaddr = (struct dhdevice *)reg;
1572395Swnj 
1582605Swnj #ifdef lint
1592605Swnj 	br = 0; cvec = br; br = cvec;
1607384Sroot 	if (ndh11 == 0) ndh11 = 1;
1614932Swnj 	dhrint(0); dhxint(0);
1622605Swnj #endif
1632696Swnj #ifndef notdef
1642566Swnj 	dhaddr->un.dhcsr = DH_RIE|DH_MM|DH_RI;
1656380Swnj 	DELAY(1000);
1667384Sroot 	dhaddr->un.dhcsr &= ~DH_RI;
1672566Swnj 	dhaddr->un.dhcsr = 0;
1682566Swnj #else
1692456Swnj 	dhaddr->un.dhcsr = DH_TIE;
1702456Swnj 	DELAY(5);
1712456Swnj 	dhaddr->dhlpr = (B9600 << 10) | (B9600 << 6) | BITS7|PENABLE;
1722421Skre 	dhaddr->dhbcr = -1;
1732456Swnj 	dhaddr->dhcar = 0;
1742421Skre 	dhaddr->dhbar = 1;
1752456Swnj 	DELAY(100000);		/* wait 1/10'th of a sec for interrupt */
1762421Skre 	dhaddr->un.dhcsr = 0;
1772456Swnj 	if (cvec && cvec != 0x200)
1782456Swnj 		cvec -= 4;		/* transmit -> receive */
1792482Swnj #endif
1807408Skre 	return (sizeof (struct dhdevice));
1812395Swnj }
1822395Swnj 
1832456Swnj /*
1842605Swnj  * Routine called to attach a dh.
1852456Swnj  */
1862605Swnj dhattach(ui)
1872974Swnj 	struct uba_device *ui;
1882395Swnj {
1892395Swnj 
1902566Swnj 	dhsoftCAR[ui->ui_unit] = ui->ui_flags;
1912395Swnj }
1922395Swnj 
19313Sbill /*
1942479Swnj  * Configuration routine to cause a dm to interrupt.
1952479Swnj  */
1962605Swnj dmprobe(reg)
1972605Swnj 	caddr_t reg;
1982479Swnj {
1992479Swnj 	register int br, vec;			/* value-result */
2002605Swnj 	register struct dmdevice *dmaddr = (struct dmdevice *)reg;
2012479Swnj 
2022605Swnj #ifdef lint
2033101Swnj 	br = 0; vec = br; br = vec;
2046185Ssam 	dmintr(0);
2052605Swnj #endif
2062479Swnj 	dmaddr->dmcsr = DM_DONE|DM_IE;
2072479Swnj 	DELAY(20);
2082479Swnj 	dmaddr->dmcsr = 0;
2092605Swnj 	return (1);
2102479Swnj }
2112479Swnj 
2122605Swnj /*ARGSUSED*/
2132605Swnj dmattach(ui)
2142974Swnj 	struct uba_device *ui;
2152479Swnj {
2162479Swnj 
2172479Swnj 	/* no local state to set up */
2182479Swnj }
2192479Swnj 
2202479Swnj /*
2212468Swnj  * Open a DH11 line, mapping the clist onto the uba if this
2222468Swnj  * is the first dh on this uba.  Turn on this dh if this is
2232468Swnj  * the first use of it.  Also do a dmopen to wait for carrier.
22413Sbill  */
22513Sbill /*ARGSUSED*/
22613Sbill dhopen(dev, flag)
2272395Swnj 	dev_t dev;
22813Sbill {
22913Sbill 	register struct tty *tp;
2302395Swnj 	register int unit, dh;
2312479Swnj 	register struct dhdevice *addr;
2322974Swnj 	register struct uba_device *ui;
23313Sbill 	int s;
23413Sbill 
2352395Swnj 	unit = minor(dev);
2362395Swnj 	dh = unit >> 4;
2372643Swnj 	if (unit >= NDH*16 || (ui = dhinfo[dh])== 0 || ui->ui_alive == 0) {
23813Sbill 		u.u_error = ENXIO;
23913Sbill 		return;
24013Sbill 	}
2412395Swnj 	tp = &dh11[unit];
2425406Swnj 	if (tp->t_state&TS_XCLUDE && u.u_uid!=0) {
2432468Swnj 		u.u_error = EBUSY;
2442468Swnj 		return;
2452468Swnj 	}
2462479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
24713Sbill 	tp->t_addr = (caddr_t)addr;
24813Sbill 	tp->t_oproc = dhstart;
2495406Swnj 	tp->t_state |= TS_WOPEN;
2502468Swnj 	/*
2512468Swnj 	 * While setting up state for this uba and this dh,
2522468Swnj 	 * block uba resets which can clear the state.
2532468Swnj 	 */
2542468Swnj 	s = spl5();
2552421Skre 	if (dh_ubinfo[ui->ui_ubanum] == 0) {
256717Sbill 		/* 512+ is a kludge to try to get around a hardware problem */
2572395Swnj 		dh_ubinfo[ui->ui_ubanum] =
2582421Skre 		    uballoc(ui->ui_ubanum, (caddr_t)cfree,
2592770Swnj 			512+nclist*sizeof(struct cblock), 0);
2602456Swnj 		cbase[ui->ui_ubanum] = dh_ubinfo[ui->ui_ubanum]&0x3ffff;
26113Sbill 	}
2622456Swnj 	if ((dhact&(1<<dh)) == 0) {
2632456Swnj 		addr->un.dhcsr |= DH_IE;
2642468Swnj 		dhact |= (1<<dh);
2652456Swnj 		addr->dhsilo = 16;
2662456Swnj 	}
26713Sbill 	splx(s);
2682468Swnj 	/*
2692468Swnj 	 * If this is first open, initialze tty state to default.
2702468Swnj 	 */
2715406Swnj 	if ((tp->t_state&TS_ISOPEN) == 0) {
27213Sbill 		ttychars(tp);
2736615Ssam #ifndef PORTSELECTOR
274168Sbill 		if (tp->t_ispeed == 0) {
2756615Ssam #endif
2766615Ssam 			tp->t_ispeed = ISPEED;
2776615Ssam 			tp->t_ospeed = ISPEED;
2786615Ssam 			tp->t_flags = IFLAGS;
2796615Ssam #ifndef PORTSELECTOR
280168Sbill 		}
2816615Ssam #endif
2822395Swnj 		dhparam(unit);
28313Sbill 	}
2842468Swnj 	/*
2852468Swnj 	 * Wait for carrier, then process line discipline specific open.
2862468Swnj 	 */
28713Sbill 	dmopen(dev);
2882395Swnj 	(*linesw[tp->t_line].l_open)(dev, tp);
28913Sbill }
29013Sbill 
29113Sbill /*
2922468Swnj  * Close a DH11 line, turning off the DM11.
29313Sbill  */
29413Sbill /*ARGSUSED*/
29513Sbill dhclose(dev, flag)
2962395Swnj 	dev_t dev;
2972395Swnj 	int flag;
29813Sbill {
29913Sbill 	register struct tty *tp;
3002395Swnj 	register unit;
30113Sbill 
3022395Swnj 	unit = minor(dev);
3032395Swnj 	tp = &dh11[unit];
30413Sbill 	(*linesw[tp->t_line].l_close)(tp);
3052479Swnj 	((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
3065406Swnj 	if (tp->t_state&TS_HUPCLS || (tp->t_state&TS_ISOPEN)==0)
3072479Swnj 		dmctl(unit, DML_OFF, DMSET);
30813Sbill 	ttyclose(tp);
30913Sbill }
31013Sbill 
31113Sbill dhread(dev)
3122395Swnj 	dev_t dev;
31313Sbill {
3142395Swnj 	register struct tty *tp;
31513Sbill 
3162395Swnj 	tp = &dh11[minor(dev)];
31713Sbill 	(*linesw[tp->t_line].l_read)(tp);
31813Sbill }
31913Sbill 
32013Sbill dhwrite(dev)
3212395Swnj 	dev_t dev;
32213Sbill {
3232395Swnj 	register struct tty *tp;
32413Sbill 
3252395Swnj 	tp = &dh11[minor(dev)];
32613Sbill 	(*linesw[tp->t_line].l_write)(tp);
32713Sbill }
32813Sbill 
32913Sbill /*
33013Sbill  * DH11 receiver interrupt.
33113Sbill  */
3322395Swnj dhrint(dh)
3332395Swnj 	int dh;
33413Sbill {
33513Sbill 	register struct tty *tp;
3362395Swnj 	register c;
3372479Swnj 	register struct dhdevice *addr;
338117Sbill 	register struct tty *tp0;
3392974Swnj 	register struct uba_device *ui;
3402924Swnj 	int overrun = 0;
34113Sbill 
3422395Swnj 	ui = dhinfo[dh];
3432479Swnj 	if (ui == 0 || ui->ui_alive == 0)
3442479Swnj 		return;
3452479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
3462468Swnj 	tp0 = &dh11[dh<<4];
3472468Swnj 	/*
3482468Swnj 	 * Loop fetching characters from the silo for this
3492468Swnj 	 * dh until there are no more in the silo.
3502468Swnj 	 */
3512468Swnj 	while ((c = addr->dhrcr) < 0) {
3522468Swnj 		tp = tp0 + ((c>>8)&0xf);
3536615Ssam #ifndef PORTSELECTOR
3545406Swnj 		if ((tp->t_state&TS_ISOPEN)==0) {
3556615Ssam #else
3566615Ssam 		if ((tp->t_state&(TS_ISOPEN|TS_WOPEN))==0) {
3576615Ssam #endif
35813Sbill 			wakeup((caddr_t)tp);
35913Sbill 			continue;
36013Sbill 		}
3612468Swnj 		if (c & DH_PE)
36213Sbill 			if ((tp->t_flags&(EVENP|ODDP))==EVENP
36313Sbill 			 || (tp->t_flags&(EVENP|ODDP))==ODDP )
36413Sbill 				continue;
3652924Swnj 		if ((c & DH_DO) && overrun == 0) {
3662924Swnj 			printf("dh%d: silo overflow\n", dh);
3672924Swnj 			overrun = 1;
3682924Swnj 		}
3692468Swnj 		if (c & DH_FE)
3702468Swnj 			/*
3712468Swnj 			 * At framing error (break) generate
3722468Swnj 			 * a null (in raw mode, for getty), or a
3732468Swnj 			 * interrupt (in cooked/cbreak mode).
3742468Swnj 			 */
37513Sbill 			if (tp->t_flags&RAW)
3762468Swnj 				c = 0;
37713Sbill 			else
378184Sbill 				c = tun.t_intrc;
3792730Swnj #if NBK > 0
380139Sbill 		if (tp->t_line == NETLDISC) {
381117Sbill 			c &= 0177;
382168Sbill 			BKINPUT(c, tp);
383117Sbill 		} else
3842730Swnj #endif
3852468Swnj 			(*linesw[tp->t_line].l_rint)(c, tp);
38613Sbill 	}
38713Sbill }
38813Sbill 
38913Sbill /*
3902468Swnj  * Ioctl for DH11.
39113Sbill  */
39213Sbill /*ARGSUSED*/
393*7629Ssam dhioctl(dev, cmd, data, flag)
394*7629Ssam 	caddr_t data;
39513Sbill {
39613Sbill 	register struct tty *tp;
3972395Swnj 	register unit = minor(dev);
39813Sbill 
3992395Swnj 	tp = &dh11[unit];
400*7629Ssam 	cmd = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
4012468Swnj 	if (cmd == 0)
402113Sbill 		return;
403*7629Ssam 	if (ttioctl(tp, cmd, data, flag)) {
404*7629Ssam 		if (cmd == TIOCSETP || cmd == TIOCSETN)
4052395Swnj 			dhparam(unit);
406168Sbill 	} else switch(cmd) {
407*7629Ssam 
408168Sbill 	case TIOCSBRK:
4092479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak |= 1<<(unit&017);
410168Sbill 		break;
411*7629Ssam 
412168Sbill 	case TIOCCBRK:
4132479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
414168Sbill 		break;
415*7629Ssam 
416168Sbill 	case TIOCSDTR:
4172479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIS);
418168Sbill 		break;
419*7629Ssam 
420168Sbill 	case TIOCCDTR:
4212479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIC);
422168Sbill 		break;
423*7629Ssam 
424168Sbill 	default:
42513Sbill 		u.u_error = ENOTTY;
426168Sbill 	}
42713Sbill }
42813Sbill 
42913Sbill /*
43013Sbill  * Set parameters from open or stty into the DH hardware
43113Sbill  * registers.
43213Sbill  */
4332395Swnj dhparam(unit)
4342395Swnj 	register int unit;
43513Sbill {
43613Sbill 	register struct tty *tp;
4372479Swnj 	register struct dhdevice *addr;
4382395Swnj 	register int lpar;
439300Sbill 	int s;
44013Sbill 
4412395Swnj 	tp = &dh11[unit];
4422479Swnj 	addr = (struct dhdevice *)tp->t_addr;
4432468Swnj 	/*
4442468Swnj 	 * Block interrupts so parameters will be set
4452468Swnj 	 * before the line interrupts.
4462468Swnj 	 */
447300Sbill 	s = spl5();
4482468Swnj 	addr->un.dhcsrl = (unit&0xf) | DH_IE;
44913Sbill 	if ((tp->t_ispeed)==0) {
4505406Swnj 		tp->t_state |= TS_HUPCLS;
4512479Swnj 		dmctl(unit, DML_OFF, DMSET);
45213Sbill 		return;
45313Sbill 	}
4542395Swnj 	lpar = ((tp->t_ospeed)<<10) | ((tp->t_ispeed)<<6);
4552468Swnj 	if ((tp->t_ispeed) == B134)
4562395Swnj 		lpar |= BITS6|PENABLE|HDUPLX;
4572312Skre 	else if ((tp->t_flags&RAW) || (tp->t_local&LLITOUT))
4582395Swnj 		lpar |= BITS8;
45913Sbill 	else
4602395Swnj 		lpar |= BITS7|PENABLE;
46113Sbill 	if ((tp->t_flags&EVENP) == 0)
4622395Swnj 		lpar |= OPAR;
4632468Swnj 	if ((tp->t_ospeed) == B110)
4642395Swnj 		lpar |= TWOSB;
4652395Swnj 	addr->dhlpr = lpar;
466300Sbill 	splx(s);
46713Sbill }
46813Sbill 
46913Sbill /*
47013Sbill  * DH11 transmitter interrupt.
47113Sbill  * Restart each line which used to be active but has
47213Sbill  * terminated transmission since the last interrupt.
47313Sbill  */
4742395Swnj dhxint(dh)
4752395Swnj 	int dh;
47613Sbill {
47713Sbill 	register struct tty *tp;
4782479Swnj 	register struct dhdevice *addr;
47913Sbill 	short ttybit, bar, *sbar;
4802974Swnj 	register struct uba_device *ui;
4812468Swnj 	register int unit;
4822605Swnj 	u_short cntr;
48313Sbill 
4842395Swnj 	ui = dhinfo[dh];
4852479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
4862456Swnj 	if (addr->un.dhcsr & DH_NXM) {
4872456Swnj 		addr->un.dhcsr |= DH_CNI;
4882924Swnj 		printf("dh%d: NXM\n", dh);
489105Sbill 	}
4902395Swnj 	sbar = &dhsar[dh];
49113Sbill 	bar = *sbar & ~addr->dhbar;
4922395Swnj 	unit = dh * 16; ttybit = 1;
4932468Swnj 	addr->un.dhcsr &= (short)~DH_TI;
4942468Swnj 	for (; bar; unit++, ttybit <<= 1) {
4952468Swnj 		if (bar & ttybit) {
49613Sbill 			*sbar &= ~ttybit;
49713Sbill 			bar &= ~ttybit;
4982395Swnj 			tp = &dh11[unit];
4995406Swnj 			tp->t_state &= ~TS_BUSY;
5005406Swnj 			if (tp->t_state&TS_FLUSH)
5015406Swnj 				tp->t_state &= ~TS_FLUSH;
502113Sbill 			else {
5032456Swnj 				addr->un.dhcsrl = (unit&017)|DH_IE;
5042468Swnj 				/*
5052468Swnj 				 * Do arithmetic in a short to make up
5062468Swnj 				 * for lost 16&17 bits.
5072468Swnj 				 */
5082605Swnj 				cntr = addr->dhcar -
5092468Swnj 				    UBACVT(tp->t_outq.c_cf, ui->ui_ubanum);
5103101Swnj 				ndflush(&tp->t_outq, (int)cntr);
511113Sbill 			}
512113Sbill 			if (tp->t_line)
51313Sbill 				(*linesw[tp->t_line].l_start)(tp);
514113Sbill 			else
51513Sbill 				dhstart(tp);
51613Sbill 		}
51713Sbill 	}
51813Sbill }
51913Sbill 
52013Sbill /*
52113Sbill  * Start (restart) transmission on the given DH11 line.
52213Sbill  */
52313Sbill dhstart(tp)
5242395Swnj 	register struct tty *tp;
52513Sbill {
5262479Swnj 	register struct dhdevice *addr;
5272468Swnj 	register int car, dh, unit, nch;
5282395Swnj 	int s;
52913Sbill 
5302468Swnj 	unit = minor(tp->t_dev);
5312468Swnj 	dh = unit >> 4;
5322468Swnj 	unit &= 0xf;
5332479Swnj 	addr = (struct dhdevice *)tp->t_addr;
5342468Swnj 
53513Sbill 	/*
5362468Swnj 	 * Must hold interrupts in following code to prevent
5372468Swnj 	 * state of the tp from changing.
53813Sbill 	 */
53913Sbill 	s = spl5();
5402468Swnj 	/*
5412468Swnj 	 * If it's currently active, or delaying, no need to do anything.
5422468Swnj 	 */
5435406Swnj 	if (tp->t_state&(TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
54413Sbill 		goto out;
5452468Swnj 	/*
5462468Swnj 	 * If there are sleepers, and output has drained below low
5472468Swnj 	 * water mark, wake up the sleepers.
5482468Swnj 	 */
5495406Swnj 	if (tp->t_outq.c_cc<=TTLOWAT(tp)) {
5505406Swnj 		if (tp->t_state&TS_ASLEEP) {
5515406Swnj 			tp->t_state &= ~TS_ASLEEP;
5525406Swnj 			wakeup((caddr_t)&tp->t_outq);
5535406Swnj 		}
5545406Swnj 		if (tp->t_wsel) {
5555406Swnj 			selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
5565406Swnj 			tp->t_wsel = 0;
5575406Swnj 			tp->t_state &= ~TS_WCOLL;
5585406Swnj 		}
55913Sbill 	}
5602468Swnj 	/*
5612468Swnj 	 * Now restart transmission unless the output queue is
5622468Swnj 	 * empty.
5632468Swnj 	 */
56413Sbill 	if (tp->t_outq.c_cc == 0)
56513Sbill 		goto out;
5663703Sroot 	if (tp->t_flags&RAW || tp->t_local&LLITOUT)
56713Sbill 		nch = ndqb(&tp->t_outq, 0);
5682395Swnj 	else {
56913Sbill 		nch = ndqb(&tp->t_outq, 0200);
5702468Swnj 		/*
5712468Swnj 		 * If first thing on queue is a delay process it.
5722468Swnj 		 */
57313Sbill 		if (nch == 0) {
57413Sbill 			nch = getc(&tp->t_outq);
5752468Swnj 			timeout(ttrstrt, (caddr_t)tp, (nch&0x7f)+6);
5765406Swnj 			tp->t_state |= TS_TIMEOUT;
57713Sbill 			goto out;
57813Sbill 		}
57913Sbill 	}
5802468Swnj 	/*
5812468Swnj 	 * If characters to transmit, restart transmission.
5822468Swnj 	 */
58313Sbill 	if (nch) {
5842468Swnj 		car = UBACVT(tp->t_outq.c_cf, dhinfo[dh]->ui_ubanum);
5852468Swnj 		addr->un.dhcsrl = unit|((car>>12)&0x30)|DH_IE;
5863586Sroot 		/*
5873586Sroot 		 * The following nonsense with short word
5883586Sroot 		 * is to make sure the dhbar |= word below
5893586Sroot 		 * is done with an interlocking bisw2 instruction.
5903586Sroot 		 */
5913586Sroot 		{ short word = 1 << unit;
5923586Sroot 		dhsar[dh] |= word;
5932468Swnj 		addr->dhcar = car;
59413Sbill 		addr->dhbcr = -nch;
5953586Sroot 		addr->dhbar |= word;
5963586Sroot 		}
5975406Swnj 		tp->t_state |= TS_BUSY;
59813Sbill 	}
5992395Swnj out:
60013Sbill 	splx(s);
60113Sbill }
60213Sbill 
60313Sbill /*
6042468Swnj  * Stop output on a line, e.g. for ^S/^Q or output flush.
60513Sbill  */
60613Sbill /*ARGSUSED*/
60713Sbill dhstop(tp, flag)
6082468Swnj 	register struct tty *tp;
60913Sbill {
6102479Swnj 	register struct dhdevice *addr;
6112395Swnj 	register int unit, s;
61213Sbill 
6132479Swnj 	addr = (struct dhdevice *)tp->t_addr;
6142468Swnj 	/*
6152468Swnj 	 * Block input/output interrupts while messing with state.
6162468Swnj 	 */
6172468Swnj 	s = spl5();
6185406Swnj 	if (tp->t_state & TS_BUSY) {
6192468Swnj 		/*
6202468Swnj 		 * Device is transmitting; stop output
6212468Swnj 		 * by selecting the line and setting the byte
6222468Swnj 		 * count to -1.  We will clean up later
6232468Swnj 		 * by examining the address where the dh stopped.
6242468Swnj 		 */
6252395Swnj 		unit = minor(tp->t_dev);
6262456Swnj 		addr->un.dhcsrl = (unit&017) | DH_IE;
6275406Swnj 		if ((tp->t_state&TS_TTSTOP)==0)
6285406Swnj 			tp->t_state |= TS_FLUSH;
629113Sbill 		addr->dhbcr = -1;
630113Sbill 	}
63113Sbill 	splx(s);
63213Sbill }
63313Sbill 
634168Sbill /*
635280Sbill  * Reset state of driver if UBA reset was necessary.
636280Sbill  * Reset the csrl and lpr registers on open lines, and
637280Sbill  * restart transmitters.
638280Sbill  */
6392395Swnj dhreset(uban)
6402468Swnj 	int uban;
641280Sbill {
6422395Swnj 	register int dh, unit;
643280Sbill 	register struct tty *tp;
6442974Swnj 	register struct uba_device *ui;
6452421Skre 	int i;
646280Sbill 
6472421Skre 	if (dh_ubinfo[uban] == 0)
6482421Skre 		return;
6492421Skre 	ubarelse(uban, &dh_ubinfo[uban]);
6502421Skre 	dh_ubinfo[uban] = uballoc(uban, (caddr_t)cfree,
6512770Swnj 	    512+nclist*sizeof (struct cblock), 0);
6522421Skre 	cbase[uban] = dh_ubinfo[uban]&0x3ffff;
6532395Swnj 	dh = 0;
6542643Swnj 	for (dh = 0; dh < NDH; dh++) {
6552421Skre 		ui = dhinfo[dh];
6562421Skre 		if (ui == 0 || ui->ui_alive == 0 || ui->ui_ubanum != uban)
6572421Skre 			continue;
6582924Swnj 		printf(" dh%d", dh);
6592479Swnj 		((struct dhdevice *)ui->ui_addr)->un.dhcsr |= DH_IE;
6602479Swnj 		((struct dhdevice *)ui->ui_addr)->dhsilo = 16;
6612421Skre 		unit = dh * 16;
6622421Skre 		for (i = 0; i < 16; i++) {
6632421Skre 			tp = &dh11[unit];
6645406Swnj 			if (tp->t_state & (TS_ISOPEN|TS_WOPEN)) {
6652421Skre 				dhparam(unit);
6662479Swnj 				dmctl(unit, DML_ON, DMSET);
6675406Swnj 				tp->t_state &= ~TS_BUSY;
6682421Skre 				dhstart(tp);
6692421Skre 			}
6702421Skre 			unit++;
671300Sbill 		}
672300Sbill 	}
673300Sbill 	dhtimer();
674280Sbill }
6752395Swnj 
6762468Swnj /*
6772468Swnj  * At software clock interrupt time or after a UNIBUS reset
6782468Swnj  * empty all the dh silos.
6792468Swnj  */
6802456Swnj dhtimer()
6812456Swnj {
6822456Swnj 	register int dh;
6832456Swnj 
6842643Swnj 	for (dh = 0; dh < NDH; dh++)
6852456Swnj 		dhrint(dh);
6862456Swnj }
6872456Swnj 
6882468Swnj /*
6892479Swnj  * Turn on the line associated with dh dev.
6902468Swnj  */
6912468Swnj dmopen(dev)
6922468Swnj 	dev_t dev;
6932468Swnj {
6942468Swnj 	register struct tty *tp;
6952468Swnj 	register struct dmdevice *addr;
6962974Swnj 	register struct uba_device *ui;
6972468Swnj 	register int unit;
6982468Swnj 	register int dm;
6993792Swnj 	int s;
7002468Swnj 
7012468Swnj 	unit = minor(dev);
7022479Swnj 	dm = unit >> 4;
7032468Swnj 	tp = &dh11[unit];
7042566Swnj 	unit &= 0xf;
7052643Swnj 	if (dm >= NDH || (ui = dminfo[dm]) == 0 || ui->ui_alive == 0 ||
7062566Swnj 	    (dhsoftCAR[dm]&(1<<unit))) {
7075406Swnj 		tp->t_state |= TS_CARR_ON;
7082468Swnj 		return;
7092468Swnj 	}
7102468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7113792Swnj 	s = spl5();
7122479Swnj 	addr->dmcsr &= ~DM_SE;
7132479Swnj 	while (addr->dmcsr & DM_BUSY)
7142468Swnj 		;
7152566Swnj 	addr->dmcsr = unit;
7162479Swnj 	addr->dmlstat = DML_ON;
7172479Swnj 	if (addr->dmlstat&DML_CAR)
7185406Swnj 		tp->t_state |= TS_CARR_ON;
7193792Swnj 	addr->dmcsr = DM_IE|DM_SE;
7205406Swnj 	while ((tp->t_state&TS_CARR_ON)==0)
7212468Swnj 		sleep((caddr_t)&tp->t_rawq, TTIPRI);
7223792Swnj 	splx(s);
7232468Swnj }
7242468Swnj 
7252468Swnj /*
7262468Swnj  * Dump control bits into the DM registers.
7272468Swnj  */
7282468Swnj dmctl(dev, bits, how)
7292468Swnj 	dev_t dev;
7302468Swnj 	int bits, how;
7312468Swnj {
7322974Swnj 	register struct uba_device *ui;
7332468Swnj 	register struct dmdevice *addr;
7342468Swnj 	register int unit, s;
7352468Swnj 	int dm;
7362468Swnj 
7372468Swnj 	unit = minor(dev);
7382468Swnj 	dm = unit >> 4;
7392468Swnj 	if ((ui = dminfo[dm]) == 0 || ui->ui_alive == 0)
7402468Swnj 		return;
7412468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7422468Swnj 	s = spl5();
7432479Swnj 	addr->dmcsr &= ~DM_SE;
7442479Swnj 	while (addr->dmcsr & DM_BUSY)
7452468Swnj 		;
7462468Swnj 	addr->dmcsr = unit & 0xf;
7472468Swnj 	switch(how) {
7482468Swnj 	case DMSET:
7492468Swnj 		addr->dmlstat = bits;
7502468Swnj 		break;
7512468Swnj 	case DMBIS:
7522468Swnj 		addr->dmlstat |= bits;
7532468Swnj 		break;
7542468Swnj 	case DMBIC:
7552468Swnj 		addr->dmlstat &= ~bits;
7562468Swnj 		break;
7572468Swnj 	}
7583792Swnj 	addr->dmcsr = DM_IE|DM_SE;
7592468Swnj 	splx(s);
7602468Swnj }
7612468Swnj 
7622468Swnj /*
7632468Swnj  * DM11 interrupt; deal with carrier transitions.
7642468Swnj  */
7652468Swnj dmintr(dm)
7662468Swnj 	register int dm;
7672468Swnj {
7682974Swnj 	register struct uba_device *ui;
7692468Swnj 	register struct tty *tp;
7702468Swnj 	register struct dmdevice *addr;
7712468Swnj 
7722468Swnj 	ui = dminfo[dm];
7732479Swnj 	if (ui == 0)
7742479Swnj 		return;
7752468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7763997Sroot 	if (addr->dmcsr&DM_DONE) {
7773997Sroot 		if (addr->dmcsr&DM_CF) {
7783997Sroot 			tp = &dh11[(dm<<4)+(addr->dmcsr&0xf)];
7793997Sroot 			wakeup((caddr_t)&tp->t_rawq);
7805406Swnj 			if ((tp->t_state&TS_WOPEN)==0 &&
7813997Sroot 			    (tp->t_local&LMDMBUF)) {
7823997Sroot 				if (addr->dmlstat & DML_CAR) {
7835406Swnj 					tp->t_state &= ~TS_TTSTOP;
7843997Sroot 					ttstart(tp);
7855406Swnj 				} else if ((tp->t_state&TS_TTSTOP) == 0) {
7865406Swnj 					tp->t_state |= TS_TTSTOP;
7873997Sroot 					dhstop(tp, 0);
7883997Sroot 				}
7893997Sroot 			} else if ((addr->dmlstat&DML_CAR)==0) {
7905406Swnj 				if ((tp->t_state&TS_WOPEN)==0 &&
7913997Sroot 				    (tp->t_local&LNOHANG)==0) {
7923997Sroot 					gsignal(tp->t_pgrp, SIGHUP);
7933997Sroot 					gsignal(tp->t_pgrp, SIGCONT);
7943997Sroot 					addr->dmlstat = 0;
7953997Sroot 					flushtty(tp, FREAD|FWRITE);
7963997Sroot 				}
7975406Swnj 				tp->t_state &= ~TS_CARR_ON;
7983997Sroot 			} else
7995406Swnj 				tp->t_state |= TS_CARR_ON;
8003997Sroot 		}
8013997Sroot 		addr->dmcsr = DM_IE|DM_SE;
8022468Swnj 	}
8032468Swnj }
8042625Swnj #endif
805