xref: /csrg-svn/sys/vax/uba/dh.c (revision 7408)
1*7408Skre /*	dh.c	4.46	82/07/15	*/
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
180*7408Skre 	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*/
39313Sbill dhioctl(dev, cmd, addr, flag)
3942395Swnj 	caddr_t addr;
39513Sbill {
39613Sbill 	register struct tty *tp;
3972395Swnj 	register unit = minor(dev);
39813Sbill 
3992395Swnj 	tp = &dh11[unit];
400113Sbill 	cmd = (*linesw[tp->t_line].l_ioctl)(tp, cmd, addr);
4012468Swnj 	if (cmd == 0)
402113Sbill 		return;
4031895Swnj 	if (ttioctl(tp, cmd, addr, flag)) {
4042468Swnj 		if (cmd==TIOCSETP || cmd==TIOCSETN)
4052395Swnj 			dhparam(unit);
406168Sbill 	} else switch(cmd) {
407168Sbill 	case TIOCSBRK:
4082479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak |= 1<<(unit&017);
409168Sbill 		break;
410168Sbill 	case TIOCCBRK:
4112479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
412168Sbill 		break;
413168Sbill 	case TIOCSDTR:
4142479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIS);
415168Sbill 		break;
416168Sbill 	case TIOCCDTR:
4172479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIC);
418168Sbill 		break;
419168Sbill 	default:
42013Sbill 		u.u_error = ENOTTY;
421168Sbill 	}
42213Sbill }
42313Sbill 
42413Sbill /*
42513Sbill  * Set parameters from open or stty into the DH hardware
42613Sbill  * registers.
42713Sbill  */
4282395Swnj dhparam(unit)
4292395Swnj 	register int unit;
43013Sbill {
43113Sbill 	register struct tty *tp;
4322479Swnj 	register struct dhdevice *addr;
4332395Swnj 	register int lpar;
434300Sbill 	int s;
43513Sbill 
4362395Swnj 	tp = &dh11[unit];
4372479Swnj 	addr = (struct dhdevice *)tp->t_addr;
4382468Swnj 	/*
4392468Swnj 	 * Block interrupts so parameters will be set
4402468Swnj 	 * before the line interrupts.
4412468Swnj 	 */
442300Sbill 	s = spl5();
4432468Swnj 	addr->un.dhcsrl = (unit&0xf) | DH_IE;
44413Sbill 	if ((tp->t_ispeed)==0) {
4455406Swnj 		tp->t_state |= TS_HUPCLS;
4462479Swnj 		dmctl(unit, DML_OFF, DMSET);
44713Sbill 		return;
44813Sbill 	}
4492395Swnj 	lpar = ((tp->t_ospeed)<<10) | ((tp->t_ispeed)<<6);
4502468Swnj 	if ((tp->t_ispeed) == B134)
4512395Swnj 		lpar |= BITS6|PENABLE|HDUPLX;
4522312Skre 	else if ((tp->t_flags&RAW) || (tp->t_local&LLITOUT))
4532395Swnj 		lpar |= BITS8;
45413Sbill 	else
4552395Swnj 		lpar |= BITS7|PENABLE;
45613Sbill 	if ((tp->t_flags&EVENP) == 0)
4572395Swnj 		lpar |= OPAR;
4582468Swnj 	if ((tp->t_ospeed) == B110)
4592395Swnj 		lpar |= TWOSB;
4602395Swnj 	addr->dhlpr = lpar;
461300Sbill 	splx(s);
46213Sbill }
46313Sbill 
46413Sbill /*
46513Sbill  * DH11 transmitter interrupt.
46613Sbill  * Restart each line which used to be active but has
46713Sbill  * terminated transmission since the last interrupt.
46813Sbill  */
4692395Swnj dhxint(dh)
4702395Swnj 	int dh;
47113Sbill {
47213Sbill 	register struct tty *tp;
4732479Swnj 	register struct dhdevice *addr;
47413Sbill 	short ttybit, bar, *sbar;
4752974Swnj 	register struct uba_device *ui;
4762468Swnj 	register int unit;
4772605Swnj 	u_short cntr;
47813Sbill 
4792395Swnj 	ui = dhinfo[dh];
4802479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
4812456Swnj 	if (addr->un.dhcsr & DH_NXM) {
4822456Swnj 		addr->un.dhcsr |= DH_CNI;
4832924Swnj 		printf("dh%d: NXM\n", dh);
484105Sbill 	}
4852395Swnj 	sbar = &dhsar[dh];
48613Sbill 	bar = *sbar & ~addr->dhbar;
4872395Swnj 	unit = dh * 16; ttybit = 1;
4882468Swnj 	addr->un.dhcsr &= (short)~DH_TI;
4892468Swnj 	for (; bar; unit++, ttybit <<= 1) {
4902468Swnj 		if (bar & ttybit) {
49113Sbill 			*sbar &= ~ttybit;
49213Sbill 			bar &= ~ttybit;
4932395Swnj 			tp = &dh11[unit];
4945406Swnj 			tp->t_state &= ~TS_BUSY;
4955406Swnj 			if (tp->t_state&TS_FLUSH)
4965406Swnj 				tp->t_state &= ~TS_FLUSH;
497113Sbill 			else {
4982456Swnj 				addr->un.dhcsrl = (unit&017)|DH_IE;
4992468Swnj 				/*
5002468Swnj 				 * Do arithmetic in a short to make up
5012468Swnj 				 * for lost 16&17 bits.
5022468Swnj 				 */
5032605Swnj 				cntr = addr->dhcar -
5042468Swnj 				    UBACVT(tp->t_outq.c_cf, ui->ui_ubanum);
5053101Swnj 				ndflush(&tp->t_outq, (int)cntr);
506113Sbill 			}
507113Sbill 			if (tp->t_line)
50813Sbill 				(*linesw[tp->t_line].l_start)(tp);
509113Sbill 			else
51013Sbill 				dhstart(tp);
51113Sbill 		}
51213Sbill 	}
51313Sbill }
51413Sbill 
51513Sbill /*
51613Sbill  * Start (restart) transmission on the given DH11 line.
51713Sbill  */
51813Sbill dhstart(tp)
5192395Swnj 	register struct tty *tp;
52013Sbill {
5212479Swnj 	register struct dhdevice *addr;
5222468Swnj 	register int car, dh, unit, nch;
5232395Swnj 	int s;
52413Sbill 
5252468Swnj 	unit = minor(tp->t_dev);
5262468Swnj 	dh = unit >> 4;
5272468Swnj 	unit &= 0xf;
5282479Swnj 	addr = (struct dhdevice *)tp->t_addr;
5292468Swnj 
53013Sbill 	/*
5312468Swnj 	 * Must hold interrupts in following code to prevent
5322468Swnj 	 * state of the tp from changing.
53313Sbill 	 */
53413Sbill 	s = spl5();
5352468Swnj 	/*
5362468Swnj 	 * If it's currently active, or delaying, no need to do anything.
5372468Swnj 	 */
5385406Swnj 	if (tp->t_state&(TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
53913Sbill 		goto out;
5402468Swnj 	/*
5412468Swnj 	 * If there are sleepers, and output has drained below low
5422468Swnj 	 * water mark, wake up the sleepers.
5432468Swnj 	 */
5445406Swnj 	if (tp->t_outq.c_cc<=TTLOWAT(tp)) {
5455406Swnj 		if (tp->t_state&TS_ASLEEP) {
5465406Swnj 			tp->t_state &= ~TS_ASLEEP;
5475406Swnj 			wakeup((caddr_t)&tp->t_outq);
5485406Swnj 		}
5495406Swnj 		if (tp->t_wsel) {
5505406Swnj 			selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
5515406Swnj 			tp->t_wsel = 0;
5525406Swnj 			tp->t_state &= ~TS_WCOLL;
5535406Swnj 		}
55413Sbill 	}
5552468Swnj 	/*
5562468Swnj 	 * Now restart transmission unless the output queue is
5572468Swnj 	 * empty.
5582468Swnj 	 */
55913Sbill 	if (tp->t_outq.c_cc == 0)
56013Sbill 		goto out;
5613703Sroot 	if (tp->t_flags&RAW || tp->t_local&LLITOUT)
56213Sbill 		nch = ndqb(&tp->t_outq, 0);
5632395Swnj 	else {
56413Sbill 		nch = ndqb(&tp->t_outq, 0200);
5652468Swnj 		/*
5662468Swnj 		 * If first thing on queue is a delay process it.
5672468Swnj 		 */
56813Sbill 		if (nch == 0) {
56913Sbill 			nch = getc(&tp->t_outq);
5702468Swnj 			timeout(ttrstrt, (caddr_t)tp, (nch&0x7f)+6);
5715406Swnj 			tp->t_state |= TS_TIMEOUT;
57213Sbill 			goto out;
57313Sbill 		}
57413Sbill 	}
5752468Swnj 	/*
5762468Swnj 	 * If characters to transmit, restart transmission.
5772468Swnj 	 */
57813Sbill 	if (nch) {
5792468Swnj 		car = UBACVT(tp->t_outq.c_cf, dhinfo[dh]->ui_ubanum);
5802468Swnj 		addr->un.dhcsrl = unit|((car>>12)&0x30)|DH_IE;
5813586Sroot 		/*
5823586Sroot 		 * The following nonsense with short word
5833586Sroot 		 * is to make sure the dhbar |= word below
5843586Sroot 		 * is done with an interlocking bisw2 instruction.
5853586Sroot 		 */
5863586Sroot 		{ short word = 1 << unit;
5873586Sroot 		dhsar[dh] |= word;
5882468Swnj 		addr->dhcar = car;
58913Sbill 		addr->dhbcr = -nch;
5903586Sroot 		addr->dhbar |= word;
5913586Sroot 		}
5925406Swnj 		tp->t_state |= TS_BUSY;
59313Sbill 	}
5942395Swnj out:
59513Sbill 	splx(s);
59613Sbill }
59713Sbill 
59813Sbill /*
5992468Swnj  * Stop output on a line, e.g. for ^S/^Q or output flush.
60013Sbill  */
60113Sbill /*ARGSUSED*/
60213Sbill dhstop(tp, flag)
6032468Swnj 	register struct tty *tp;
60413Sbill {
6052479Swnj 	register struct dhdevice *addr;
6062395Swnj 	register int unit, s;
60713Sbill 
6082479Swnj 	addr = (struct dhdevice *)tp->t_addr;
6092468Swnj 	/*
6102468Swnj 	 * Block input/output interrupts while messing with state.
6112468Swnj 	 */
6122468Swnj 	s = spl5();
6135406Swnj 	if (tp->t_state & TS_BUSY) {
6142468Swnj 		/*
6152468Swnj 		 * Device is transmitting; stop output
6162468Swnj 		 * by selecting the line and setting the byte
6172468Swnj 		 * count to -1.  We will clean up later
6182468Swnj 		 * by examining the address where the dh stopped.
6192468Swnj 		 */
6202395Swnj 		unit = minor(tp->t_dev);
6212456Swnj 		addr->un.dhcsrl = (unit&017) | DH_IE;
6225406Swnj 		if ((tp->t_state&TS_TTSTOP)==0)
6235406Swnj 			tp->t_state |= TS_FLUSH;
624113Sbill 		addr->dhbcr = -1;
625113Sbill 	}
62613Sbill 	splx(s);
62713Sbill }
62813Sbill 
629168Sbill /*
630280Sbill  * Reset state of driver if UBA reset was necessary.
631280Sbill  * Reset the csrl and lpr registers on open lines, and
632280Sbill  * restart transmitters.
633280Sbill  */
6342395Swnj dhreset(uban)
6352468Swnj 	int uban;
636280Sbill {
6372395Swnj 	register int dh, unit;
638280Sbill 	register struct tty *tp;
6392974Swnj 	register struct uba_device *ui;
6402421Skre 	int i;
641280Sbill 
6422421Skre 	if (dh_ubinfo[uban] == 0)
6432421Skre 		return;
6442421Skre 	ubarelse(uban, &dh_ubinfo[uban]);
6452421Skre 	dh_ubinfo[uban] = uballoc(uban, (caddr_t)cfree,
6462770Swnj 	    512+nclist*sizeof (struct cblock), 0);
6472421Skre 	cbase[uban] = dh_ubinfo[uban]&0x3ffff;
6482395Swnj 	dh = 0;
6492643Swnj 	for (dh = 0; dh < NDH; dh++) {
6502421Skre 		ui = dhinfo[dh];
6512421Skre 		if (ui == 0 || ui->ui_alive == 0 || ui->ui_ubanum != uban)
6522421Skre 			continue;
6532924Swnj 		printf(" dh%d", dh);
6542479Swnj 		((struct dhdevice *)ui->ui_addr)->un.dhcsr |= DH_IE;
6552479Swnj 		((struct dhdevice *)ui->ui_addr)->dhsilo = 16;
6562421Skre 		unit = dh * 16;
6572421Skre 		for (i = 0; i < 16; i++) {
6582421Skre 			tp = &dh11[unit];
6595406Swnj 			if (tp->t_state & (TS_ISOPEN|TS_WOPEN)) {
6602421Skre 				dhparam(unit);
6612479Swnj 				dmctl(unit, DML_ON, DMSET);
6625406Swnj 				tp->t_state &= ~TS_BUSY;
6632421Skre 				dhstart(tp);
6642421Skre 			}
6652421Skre 			unit++;
666300Sbill 		}
667300Sbill 	}
668300Sbill 	dhtimer();
669280Sbill }
6702395Swnj 
6712468Swnj /*
6722468Swnj  * At software clock interrupt time or after a UNIBUS reset
6732468Swnj  * empty all the dh silos.
6742468Swnj  */
6752456Swnj dhtimer()
6762456Swnj {
6772456Swnj 	register int dh;
6782456Swnj 
6792643Swnj 	for (dh = 0; dh < NDH; dh++)
6802456Swnj 		dhrint(dh);
6812456Swnj }
6822456Swnj 
6832468Swnj /*
6842479Swnj  * Turn on the line associated with dh dev.
6852468Swnj  */
6862468Swnj dmopen(dev)
6872468Swnj 	dev_t dev;
6882468Swnj {
6892468Swnj 	register struct tty *tp;
6902468Swnj 	register struct dmdevice *addr;
6912974Swnj 	register struct uba_device *ui;
6922468Swnj 	register int unit;
6932468Swnj 	register int dm;
6943792Swnj 	int s;
6952468Swnj 
6962468Swnj 	unit = minor(dev);
6972479Swnj 	dm = unit >> 4;
6982468Swnj 	tp = &dh11[unit];
6992566Swnj 	unit &= 0xf;
7002643Swnj 	if (dm >= NDH || (ui = dminfo[dm]) == 0 || ui->ui_alive == 0 ||
7012566Swnj 	    (dhsoftCAR[dm]&(1<<unit))) {
7025406Swnj 		tp->t_state |= TS_CARR_ON;
7032468Swnj 		return;
7042468Swnj 	}
7052468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7063792Swnj 	s = spl5();
7072479Swnj 	addr->dmcsr &= ~DM_SE;
7082479Swnj 	while (addr->dmcsr & DM_BUSY)
7092468Swnj 		;
7102566Swnj 	addr->dmcsr = unit;
7112479Swnj 	addr->dmlstat = DML_ON;
7122479Swnj 	if (addr->dmlstat&DML_CAR)
7135406Swnj 		tp->t_state |= TS_CARR_ON;
7143792Swnj 	addr->dmcsr = DM_IE|DM_SE;
7155406Swnj 	while ((tp->t_state&TS_CARR_ON)==0)
7162468Swnj 		sleep((caddr_t)&tp->t_rawq, TTIPRI);
7173792Swnj 	splx(s);
7182468Swnj }
7192468Swnj 
7202468Swnj /*
7212468Swnj  * Dump control bits into the DM registers.
7222468Swnj  */
7232468Swnj dmctl(dev, bits, how)
7242468Swnj 	dev_t dev;
7252468Swnj 	int bits, how;
7262468Swnj {
7272974Swnj 	register struct uba_device *ui;
7282468Swnj 	register struct dmdevice *addr;
7292468Swnj 	register int unit, s;
7302468Swnj 	int dm;
7312468Swnj 
7322468Swnj 	unit = minor(dev);
7332468Swnj 	dm = unit >> 4;
7342468Swnj 	if ((ui = dminfo[dm]) == 0 || ui->ui_alive == 0)
7352468Swnj 		return;
7362468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7372468Swnj 	s = spl5();
7382479Swnj 	addr->dmcsr &= ~DM_SE;
7392479Swnj 	while (addr->dmcsr & DM_BUSY)
7402468Swnj 		;
7412468Swnj 	addr->dmcsr = unit & 0xf;
7422468Swnj 	switch(how) {
7432468Swnj 	case DMSET:
7442468Swnj 		addr->dmlstat = bits;
7452468Swnj 		break;
7462468Swnj 	case DMBIS:
7472468Swnj 		addr->dmlstat |= bits;
7482468Swnj 		break;
7492468Swnj 	case DMBIC:
7502468Swnj 		addr->dmlstat &= ~bits;
7512468Swnj 		break;
7522468Swnj 	}
7533792Swnj 	addr->dmcsr = DM_IE|DM_SE;
7542468Swnj 	splx(s);
7552468Swnj }
7562468Swnj 
7572468Swnj /*
7582468Swnj  * DM11 interrupt; deal with carrier transitions.
7592468Swnj  */
7602468Swnj dmintr(dm)
7612468Swnj 	register int dm;
7622468Swnj {
7632974Swnj 	register struct uba_device *ui;
7642468Swnj 	register struct tty *tp;
7652468Swnj 	register struct dmdevice *addr;
7662468Swnj 
7672468Swnj 	ui = dminfo[dm];
7682479Swnj 	if (ui == 0)
7692479Swnj 		return;
7702468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7713997Sroot 	if (addr->dmcsr&DM_DONE) {
7723997Sroot 		if (addr->dmcsr&DM_CF) {
7733997Sroot 			tp = &dh11[(dm<<4)+(addr->dmcsr&0xf)];
7743997Sroot 			wakeup((caddr_t)&tp->t_rawq);
7755406Swnj 			if ((tp->t_state&TS_WOPEN)==0 &&
7763997Sroot 			    (tp->t_local&LMDMBUF)) {
7773997Sroot 				if (addr->dmlstat & DML_CAR) {
7785406Swnj 					tp->t_state &= ~TS_TTSTOP;
7793997Sroot 					ttstart(tp);
7805406Swnj 				} else if ((tp->t_state&TS_TTSTOP) == 0) {
7815406Swnj 					tp->t_state |= TS_TTSTOP;
7823997Sroot 					dhstop(tp, 0);
7833997Sroot 				}
7843997Sroot 			} else if ((addr->dmlstat&DML_CAR)==0) {
7855406Swnj 				if ((tp->t_state&TS_WOPEN)==0 &&
7863997Sroot 				    (tp->t_local&LNOHANG)==0) {
7873997Sroot 					gsignal(tp->t_pgrp, SIGHUP);
7883997Sroot 					gsignal(tp->t_pgrp, SIGCONT);
7893997Sroot 					addr->dmlstat = 0;
7903997Sroot 					flushtty(tp, FREAD|FWRITE);
7913997Sroot 				}
7925406Swnj 				tp->t_state &= ~TS_CARR_ON;
7933997Sroot 			} else
7945406Swnj 				tp->t_state |= TS_CARR_ON;
7953997Sroot 		}
7963997Sroot 		addr->dmcsr = DM_IE|DM_SE;
7972468Swnj 	}
7982468Swnj }
7992625Swnj #endif
800