xref: /csrg-svn/sys/vax/uba/dh.c (revision 8472)
1*8472Sroot /*	dh.c	4.51	82/10/10	*/
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"
19*8472Sroot 
20*8472Sroot #include "../vaxuba/ubareg.h"
21*8472Sroot #include "../vaxuba/ubavar.h"
22*8472Sroot 
23113Sbill #include "../h/bk.h"
241561Sbill #include "../h/clist.h"
252468Swnj #include "../h/file.h"
267725Sroot #include "../h/uio.h"
2713Sbill 
282468Swnj /*
292479Swnj  * Definition of the driver for the auto-configuration program.
302479Swnj  * There is one definition for the dh and one for the dm.
312468Swnj  */
322605Swnj int	dhprobe(), dhattach(), dhrint(), dhxint();
332974Swnj struct	uba_device *dhinfo[NDH];
342395Swnj u_short	dhstd[] = { 0 };
352395Swnj struct	uba_driver dhdriver =
362605Swnj 	{ dhprobe, 0, dhattach, 0, dhstd, "dh", dhinfo };
372395Swnj 
382605Swnj int	dmprobe(), dmattach(), dmintr();
392974Swnj struct	uba_device *dminfo[NDH];
402479Swnj u_short	dmstd[] = { 0 };
412479Swnj struct	uba_driver dmdriver =
422605Swnj 	{ dmprobe, 0, dmattach, 0, dmstd, "dm", dminfo };
4313Sbill 
442479Swnj struct dhdevice
452479Swnj {
462479Swnj 	union {
472479Swnj 		short	dhcsr;		/* control-status register */
482479Swnj 		char	dhcsrl;		/* low byte for line select */
492479Swnj 	} un;
502479Swnj 	short	dhrcr;			/* receive character register */
512479Swnj 	short	dhlpr;			/* line parameter register */
522479Swnj 	u_short dhcar;			/* current address register */
532479Swnj 	short	dhbcr;			/* byte count register */
542479Swnj 	u_short	dhbar;			/* buffer active register */
552479Swnj 	short	dhbreak;		/* break control register */
562479Swnj 	short	dhsilo;			/* silo status register */
572479Swnj };
5813Sbill 
596615Ssam #ifndef	PORTSELECTOR
606615Ssam #define	ISPEED	B300
616615Ssam #define	IFLAGS	(EVENP|ODDP|ECHO)
626615Ssam #else
636615Ssam #define	ISPEED	B4800
646615Ssam #define	IFLAGS	(EVENP|ODDP)
656615Ssam #endif
666615Ssam 
672456Swnj /* Bits in dhcsr */
682456Swnj #define	DH_TI	0100000		/* transmit interrupt */
692456Swnj #define	DH_SI	0040000		/* storage interrupt */
702456Swnj #define	DH_TIE	0020000		/* transmit interrupt enable */
712456Swnj #define	DH_SIE	0010000		/* storage interrupt enable */
722456Swnj #define	DH_MC	0004000		/* master clear */
732456Swnj #define	DH_NXM	0002000		/* non-existant memory */
742456Swnj #define	DH_MM	0001000		/* maintenance mode */
752456Swnj #define	DH_CNI	0000400		/* clear non-existant memory interrupt */
762456Swnj #define	DH_RI	0000200		/* receiver interrupt */
772456Swnj #define	DH_RIE	0000100		/* receiver interrupt enable */
7813Sbill 
792479Swnj /* Bits in dhlpr */
802479Swnj #define	BITS6	01
812479Swnj #define	BITS7	02
822479Swnj #define	BITS8	03
832479Swnj #define	TWOSB	04
842479Swnj #define	PENABLE	020
852479Swnj /* DEC manuals incorrectly say this bit causes generation of even parity. */
862479Swnj #define	OPAR	040
872479Swnj #define	HDUPLX	040000
882479Swnj 
892456Swnj #define	DH_IE	(DH_TIE|DH_SIE|DH_RIE)
902456Swnj 
912456Swnj /* Bits in dhrcr */
922479Swnj #define	DH_PE		0010000		/* parity error */
932479Swnj #define	DH_FE		0020000		/* framing error */
942479Swnj #define	DH_DO		0040000		/* data overrun */
952456Swnj 
962479Swnj struct dmdevice
972479Swnj {
982479Swnj 	short	dmcsr;		/* control status register */
992479Swnj 	short	dmlstat;	/* line status register */
1002479Swnj 	short	dmpad1[2];
1012479Swnj };
1022479Swnj 
1032479Swnj /* bits in dm csr */
1042479Swnj #define	DM_RF		0100000		/* ring flag */
1052479Swnj #define	DM_CF		0040000		/* carrier flag */
1062479Swnj #define	DM_CTS		0020000		/* clear to send */
1072479Swnj #define	DM_SRF		0010000		/* secondary receive flag */
1082479Swnj #define	DM_CS		0004000		/* clear scan */
1092479Swnj #define	DM_CM		0002000		/* clear multiplexor */
1102479Swnj #define	DM_MM		0001000		/* maintenance mode */
1112479Swnj #define	DM_STP		0000400		/* step */
1122479Swnj #define	DM_DONE		0000200		/* scanner is done */
1132479Swnj #define	DM_IE		0000100		/* interrupt enable */
1142479Swnj #define	DM_SE		0000040		/* scan enable */
1152479Swnj #define	DM_BUSY		0000020		/* scan busy */
1162479Swnj 
1172479Swnj /* bits in dm lsr */
1182479Swnj #define	DML_RNG		0000200		/* ring */
1192479Swnj #define	DML_CAR		0000100		/* carrier detect */
1202479Swnj #define	DML_CTS		0000040		/* clear to send */
1212479Swnj #define	DML_SR		0000020		/* secondary receive */
1222479Swnj #define	DML_ST		0000010		/* secondary transmit */
1232479Swnj #define	DML_RTS		0000004		/* request to send */
1242479Swnj #define	DML_DTR		0000002		/* data terminal ready */
1252479Swnj #define	DML_LE		0000001		/* line enable */
1262479Swnj 
1273792Swnj #define	DML_ON		(DML_DTR|DML_RTS|DML_LE)
1282479Swnj #define	DML_OFF		(DML_LE)
1292479Swnj 
13013Sbill /*
1312479Swnj  * Local variables for the driver
13213Sbill  */
1332643Swnj short	dhsar[NDH];			/* software copy of last bar */
1342643Swnj short	dhsoftCAR[NDH];
13513Sbill 
1362643Swnj struct	tty dh11[NDH*16];
1372643Swnj int	ndh11	= NDH*16;
1382479Swnj int	dhact;				/* mask of active dh's */
1392479Swnj int	dhstart(), ttrstrt();
14013Sbill 
1412479Swnj /*
1422479Swnj  * The clist space is mapped by the driver onto each UNIBUS.
1432479Swnj  * The UBACVT macro converts a clist space address for unibus uban
1442479Swnj  * into an i/o space address for the DMA routine.
1452479Swnj  */
1462479Swnj int	dh_ubinfo[MAXNUBA];		/* info about allocated unibus map */
1472479Swnj int	cbase[MAXNUBA];			/* base address in unibus map */
1482479Swnj #define	UBACVT(x, uban)		(cbase[uban] + ((x)-(char *)cfree))
14913Sbill 
1502456Swnj /*
1512456Swnj  * Routine for configuration to force a dh to interrupt.
1522456Swnj  * Set to transmit at 9600 baud, and cause a transmitter interrupt.
1532456Swnj  */
1542468Swnj /*ARGSUSED*/
1552605Swnj dhprobe(reg)
1562395Swnj 	caddr_t reg;
1572395Swnj {
1582468Swnj 	register int br, cvec;		/* these are ``value-result'' */
1592479Swnj 	register struct dhdevice *dhaddr = (struct dhdevice *)reg;
1602395Swnj 
1612605Swnj #ifdef lint
1622605Swnj 	br = 0; cvec = br; br = cvec;
1637384Sroot 	if (ndh11 == 0) ndh11 = 1;
1644932Swnj 	dhrint(0); dhxint(0);
1652605Swnj #endif
1662696Swnj #ifndef notdef
1672566Swnj 	dhaddr->un.dhcsr = DH_RIE|DH_MM|DH_RI;
1686380Swnj 	DELAY(1000);
1697384Sroot 	dhaddr->un.dhcsr &= ~DH_RI;
1702566Swnj 	dhaddr->un.dhcsr = 0;
1712566Swnj #else
1722456Swnj 	dhaddr->un.dhcsr = DH_TIE;
1732456Swnj 	DELAY(5);
1742456Swnj 	dhaddr->dhlpr = (B9600 << 10) | (B9600 << 6) | BITS7|PENABLE;
1752421Skre 	dhaddr->dhbcr = -1;
1762456Swnj 	dhaddr->dhcar = 0;
1772421Skre 	dhaddr->dhbar = 1;
1782456Swnj 	DELAY(100000);		/* wait 1/10'th of a sec for interrupt */
1792421Skre 	dhaddr->un.dhcsr = 0;
1802456Swnj 	if (cvec && cvec != 0x200)
1812456Swnj 		cvec -= 4;		/* transmit -> receive */
1822482Swnj #endif
1837408Skre 	return (sizeof (struct dhdevice));
1842395Swnj }
1852395Swnj 
1862456Swnj /*
1872605Swnj  * Routine called to attach a dh.
1882456Swnj  */
1892605Swnj dhattach(ui)
1902974Swnj 	struct uba_device *ui;
1912395Swnj {
1922395Swnj 
1932566Swnj 	dhsoftCAR[ui->ui_unit] = ui->ui_flags;
1942395Swnj }
1952395Swnj 
19613Sbill /*
1972479Swnj  * Configuration routine to cause a dm to interrupt.
1982479Swnj  */
1992605Swnj dmprobe(reg)
2002605Swnj 	caddr_t reg;
2012479Swnj {
2022479Swnj 	register int br, vec;			/* value-result */
2032605Swnj 	register struct dmdevice *dmaddr = (struct dmdevice *)reg;
2042479Swnj 
2052605Swnj #ifdef lint
2063101Swnj 	br = 0; vec = br; br = vec;
2076185Ssam 	dmintr(0);
2082605Swnj #endif
2092479Swnj 	dmaddr->dmcsr = DM_DONE|DM_IE;
2102479Swnj 	DELAY(20);
2112479Swnj 	dmaddr->dmcsr = 0;
2122605Swnj 	return (1);
2132479Swnj }
2142479Swnj 
2152605Swnj /*ARGSUSED*/
2162605Swnj dmattach(ui)
2172974Swnj 	struct uba_device *ui;
2182479Swnj {
2192479Swnj 
2202479Swnj 	/* no local state to set up */
2212479Swnj }
2222479Swnj 
2232479Swnj /*
2242468Swnj  * Open a DH11 line, mapping the clist onto the uba if this
2252468Swnj  * is the first dh on this uba.  Turn on this dh if this is
2262468Swnj  * the first use of it.  Also do a dmopen to wait for carrier.
22713Sbill  */
22813Sbill /*ARGSUSED*/
22913Sbill dhopen(dev, flag)
2302395Swnj 	dev_t dev;
23113Sbill {
23213Sbill 	register struct tty *tp;
2332395Swnj 	register int unit, dh;
2342479Swnj 	register struct dhdevice *addr;
2352974Swnj 	register struct uba_device *ui;
23613Sbill 	int s;
23713Sbill 
2382395Swnj 	unit = minor(dev);
2392395Swnj 	dh = unit >> 4;
2402643Swnj 	if (unit >= NDH*16 || (ui = dhinfo[dh])== 0 || ui->ui_alive == 0) {
24113Sbill 		u.u_error = ENXIO;
24213Sbill 		return;
24313Sbill 	}
2442395Swnj 	tp = &dh11[unit];
2455406Swnj 	if (tp->t_state&TS_XCLUDE && u.u_uid!=0) {
2462468Swnj 		u.u_error = EBUSY;
2472468Swnj 		return;
2482468Swnj 	}
2492479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
25013Sbill 	tp->t_addr = (caddr_t)addr;
25113Sbill 	tp->t_oproc = dhstart;
2525406Swnj 	tp->t_state |= TS_WOPEN;
2532468Swnj 	/*
2542468Swnj 	 * While setting up state for this uba and this dh,
2552468Swnj 	 * block uba resets which can clear the state.
2562468Swnj 	 */
2572468Swnj 	s = spl5();
2582421Skre 	if (dh_ubinfo[ui->ui_ubanum] == 0) {
259717Sbill 		/* 512+ is a kludge to try to get around a hardware problem */
2602395Swnj 		dh_ubinfo[ui->ui_ubanum] =
2612421Skre 		    uballoc(ui->ui_ubanum, (caddr_t)cfree,
2622770Swnj 			512+nclist*sizeof(struct cblock), 0);
2632456Swnj 		cbase[ui->ui_ubanum] = dh_ubinfo[ui->ui_ubanum]&0x3ffff;
26413Sbill 	}
2652456Swnj 	if ((dhact&(1<<dh)) == 0) {
2662456Swnj 		addr->un.dhcsr |= DH_IE;
2672468Swnj 		dhact |= (1<<dh);
2682456Swnj 		addr->dhsilo = 16;
2692456Swnj 	}
27013Sbill 	splx(s);
2712468Swnj 	/*
2722468Swnj 	 * If this is first open, initialze tty state to default.
2732468Swnj 	 */
2745406Swnj 	if ((tp->t_state&TS_ISOPEN) == 0) {
27513Sbill 		ttychars(tp);
2766615Ssam #ifndef PORTSELECTOR
277168Sbill 		if (tp->t_ispeed == 0) {
2786615Ssam #endif
2796615Ssam 			tp->t_ispeed = ISPEED;
2806615Ssam 			tp->t_ospeed = ISPEED;
2816615Ssam 			tp->t_flags = IFLAGS;
2826615Ssam #ifndef PORTSELECTOR
283168Sbill 		}
2846615Ssam #endif
2852395Swnj 		dhparam(unit);
28613Sbill 	}
2872468Swnj 	/*
2882468Swnj 	 * Wait for carrier, then process line discipline specific open.
2892468Swnj 	 */
29013Sbill 	dmopen(dev);
2912395Swnj 	(*linesw[tp->t_line].l_open)(dev, tp);
29213Sbill }
29313Sbill 
29413Sbill /*
2952468Swnj  * Close a DH11 line, turning off the DM11.
29613Sbill  */
29713Sbill /*ARGSUSED*/
29813Sbill dhclose(dev, flag)
2992395Swnj 	dev_t dev;
3002395Swnj 	int flag;
30113Sbill {
30213Sbill 	register struct tty *tp;
3032395Swnj 	register unit;
30413Sbill 
3052395Swnj 	unit = minor(dev);
3062395Swnj 	tp = &dh11[unit];
30713Sbill 	(*linesw[tp->t_line].l_close)(tp);
3082479Swnj 	((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
3095406Swnj 	if (tp->t_state&TS_HUPCLS || (tp->t_state&TS_ISOPEN)==0)
3102479Swnj 		dmctl(unit, DML_OFF, DMSET);
31113Sbill 	ttyclose(tp);
31213Sbill }
31313Sbill 
3147725Sroot dhread(dev, uio)
3152395Swnj 	dev_t dev;
3167725Sroot 	struct uio *uio;
31713Sbill {
3182395Swnj 	register struct tty *tp;
31913Sbill 
3202395Swnj 	tp = &dh11[minor(dev)];
3217725Sroot 	return ((*linesw[tp->t_line].l_read)(tp, uio));
32213Sbill }
32313Sbill 
3247831Sroot dhwrite(dev, uio)
3252395Swnj 	dev_t dev;
3267831Sroot 	struct uio *uio;
32713Sbill {
3282395Swnj 	register struct tty *tp;
32913Sbill 
3302395Swnj 	tp = &dh11[minor(dev)];
3317831Sroot 	(*linesw[tp->t_line].l_write)(tp, uio);
33213Sbill }
33313Sbill 
33413Sbill /*
33513Sbill  * DH11 receiver interrupt.
33613Sbill  */
3372395Swnj dhrint(dh)
3382395Swnj 	int dh;
33913Sbill {
34013Sbill 	register struct tty *tp;
3412395Swnj 	register c;
3422479Swnj 	register struct dhdevice *addr;
343117Sbill 	register struct tty *tp0;
3442974Swnj 	register struct uba_device *ui;
3452924Swnj 	int overrun = 0;
34613Sbill 
3472395Swnj 	ui = dhinfo[dh];
3482479Swnj 	if (ui == 0 || ui->ui_alive == 0)
3492479Swnj 		return;
3502479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
3512468Swnj 	tp0 = &dh11[dh<<4];
3522468Swnj 	/*
3532468Swnj 	 * Loop fetching characters from the silo for this
3542468Swnj 	 * dh until there are no more in the silo.
3552468Swnj 	 */
3562468Swnj 	while ((c = addr->dhrcr) < 0) {
3572468Swnj 		tp = tp0 + ((c>>8)&0xf);
3586615Ssam #ifndef PORTSELECTOR
3595406Swnj 		if ((tp->t_state&TS_ISOPEN)==0) {
3606615Ssam #else
3616615Ssam 		if ((tp->t_state&(TS_ISOPEN|TS_WOPEN))==0) {
3626615Ssam #endif
36313Sbill 			wakeup((caddr_t)tp);
36413Sbill 			continue;
36513Sbill 		}
3662468Swnj 		if (c & DH_PE)
36713Sbill 			if ((tp->t_flags&(EVENP|ODDP))==EVENP
36813Sbill 			 || (tp->t_flags&(EVENP|ODDP))==ODDP )
36913Sbill 				continue;
3702924Swnj 		if ((c & DH_DO) && overrun == 0) {
3712924Swnj 			printf("dh%d: silo overflow\n", dh);
3722924Swnj 			overrun = 1;
3732924Swnj 		}
3742468Swnj 		if (c & DH_FE)
3752468Swnj 			/*
3762468Swnj 			 * At framing error (break) generate
3772468Swnj 			 * a null (in raw mode, for getty), or a
3782468Swnj 			 * interrupt (in cooked/cbreak mode).
3792468Swnj 			 */
38013Sbill 			if (tp->t_flags&RAW)
3812468Swnj 				c = 0;
38213Sbill 			else
383184Sbill 				c = tun.t_intrc;
3842730Swnj #if NBK > 0
385139Sbill 		if (tp->t_line == NETLDISC) {
386117Sbill 			c &= 0177;
387168Sbill 			BKINPUT(c, tp);
388117Sbill 		} else
3892730Swnj #endif
3902468Swnj 			(*linesw[tp->t_line].l_rint)(c, tp);
39113Sbill 	}
39213Sbill }
39313Sbill 
39413Sbill /*
3952468Swnj  * Ioctl for DH11.
39613Sbill  */
39713Sbill /*ARGSUSED*/
3987629Ssam dhioctl(dev, cmd, data, flag)
3997629Ssam 	caddr_t data;
40013Sbill {
40113Sbill 	register struct tty *tp;
4022395Swnj 	register unit = minor(dev);
40313Sbill 
4042395Swnj 	tp = &dh11[unit];
4057629Ssam 	cmd = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
4062468Swnj 	if (cmd == 0)
407113Sbill 		return;
4087629Ssam 	if (ttioctl(tp, cmd, data, flag)) {
4097629Ssam 		if (cmd == TIOCSETP || cmd == TIOCSETN)
4102395Swnj 			dhparam(unit);
411168Sbill 	} else switch(cmd) {
4127629Ssam 
413168Sbill 	case TIOCSBRK:
4142479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak |= 1<<(unit&017);
415168Sbill 		break;
4167629Ssam 
417168Sbill 	case TIOCCBRK:
4182479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
419168Sbill 		break;
4207629Ssam 
421168Sbill 	case TIOCSDTR:
4222479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIS);
423168Sbill 		break;
4247629Ssam 
425168Sbill 	case TIOCCDTR:
4262479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIC);
427168Sbill 		break;
4287629Ssam 
429168Sbill 	default:
43013Sbill 		u.u_error = ENOTTY;
431168Sbill 	}
43213Sbill }
43313Sbill 
43413Sbill /*
43513Sbill  * Set parameters from open or stty into the DH hardware
43613Sbill  * registers.
43713Sbill  */
4382395Swnj dhparam(unit)
4392395Swnj 	register int unit;
44013Sbill {
44113Sbill 	register struct tty *tp;
4422479Swnj 	register struct dhdevice *addr;
4432395Swnj 	register int lpar;
444300Sbill 	int s;
44513Sbill 
4462395Swnj 	tp = &dh11[unit];
4472479Swnj 	addr = (struct dhdevice *)tp->t_addr;
4482468Swnj 	/*
4492468Swnj 	 * Block interrupts so parameters will be set
4502468Swnj 	 * before the line interrupts.
4512468Swnj 	 */
452300Sbill 	s = spl5();
4532468Swnj 	addr->un.dhcsrl = (unit&0xf) | DH_IE;
45413Sbill 	if ((tp->t_ispeed)==0) {
4555406Swnj 		tp->t_state |= TS_HUPCLS;
4562479Swnj 		dmctl(unit, DML_OFF, DMSET);
45713Sbill 		return;
45813Sbill 	}
4592395Swnj 	lpar = ((tp->t_ospeed)<<10) | ((tp->t_ispeed)<<6);
4602468Swnj 	if ((tp->t_ispeed) == B134)
4612395Swnj 		lpar |= BITS6|PENABLE|HDUPLX;
4622312Skre 	else if ((tp->t_flags&RAW) || (tp->t_local&LLITOUT))
4632395Swnj 		lpar |= BITS8;
46413Sbill 	else
4652395Swnj 		lpar |= BITS7|PENABLE;
46613Sbill 	if ((tp->t_flags&EVENP) == 0)
4672395Swnj 		lpar |= OPAR;
4682468Swnj 	if ((tp->t_ospeed) == B110)
4692395Swnj 		lpar |= TWOSB;
4702395Swnj 	addr->dhlpr = lpar;
471300Sbill 	splx(s);
47213Sbill }
47313Sbill 
47413Sbill /*
47513Sbill  * DH11 transmitter interrupt.
47613Sbill  * Restart each line which used to be active but has
47713Sbill  * terminated transmission since the last interrupt.
47813Sbill  */
4792395Swnj dhxint(dh)
4802395Swnj 	int dh;
48113Sbill {
48213Sbill 	register struct tty *tp;
4832479Swnj 	register struct dhdevice *addr;
48413Sbill 	short ttybit, bar, *sbar;
4852974Swnj 	register struct uba_device *ui;
4862468Swnj 	register int unit;
4872605Swnj 	u_short cntr;
48813Sbill 
4892395Swnj 	ui = dhinfo[dh];
4902479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
4912456Swnj 	if (addr->un.dhcsr & DH_NXM) {
4922456Swnj 		addr->un.dhcsr |= DH_CNI;
4932924Swnj 		printf("dh%d: NXM\n", dh);
494105Sbill 	}
4952395Swnj 	sbar = &dhsar[dh];
49613Sbill 	bar = *sbar & ~addr->dhbar;
4972395Swnj 	unit = dh * 16; ttybit = 1;
4982468Swnj 	addr->un.dhcsr &= (short)~DH_TI;
4992468Swnj 	for (; bar; unit++, ttybit <<= 1) {
5002468Swnj 		if (bar & ttybit) {
50113Sbill 			*sbar &= ~ttybit;
50213Sbill 			bar &= ~ttybit;
5032395Swnj 			tp = &dh11[unit];
5045406Swnj 			tp->t_state &= ~TS_BUSY;
5055406Swnj 			if (tp->t_state&TS_FLUSH)
5065406Swnj 				tp->t_state &= ~TS_FLUSH;
507113Sbill 			else {
5082456Swnj 				addr->un.dhcsrl = (unit&017)|DH_IE;
5092468Swnj 				/*
5102468Swnj 				 * Do arithmetic in a short to make up
5112468Swnj 				 * for lost 16&17 bits.
5122468Swnj 				 */
5132605Swnj 				cntr = addr->dhcar -
5142468Swnj 				    UBACVT(tp->t_outq.c_cf, ui->ui_ubanum);
5153101Swnj 				ndflush(&tp->t_outq, (int)cntr);
516113Sbill 			}
517113Sbill 			if (tp->t_line)
51813Sbill 				(*linesw[tp->t_line].l_start)(tp);
519113Sbill 			else
52013Sbill 				dhstart(tp);
52113Sbill 		}
52213Sbill 	}
52313Sbill }
52413Sbill 
52513Sbill /*
52613Sbill  * Start (restart) transmission on the given DH11 line.
52713Sbill  */
52813Sbill dhstart(tp)
5292395Swnj 	register struct tty *tp;
53013Sbill {
5312479Swnj 	register struct dhdevice *addr;
5322468Swnj 	register int car, dh, unit, nch;
5332395Swnj 	int s;
53413Sbill 
5352468Swnj 	unit = minor(tp->t_dev);
5362468Swnj 	dh = unit >> 4;
5372468Swnj 	unit &= 0xf;
5382479Swnj 	addr = (struct dhdevice *)tp->t_addr;
5392468Swnj 
54013Sbill 	/*
5412468Swnj 	 * Must hold interrupts in following code to prevent
5422468Swnj 	 * state of the tp from changing.
54313Sbill 	 */
54413Sbill 	s = spl5();
5452468Swnj 	/*
5462468Swnj 	 * If it's currently active, or delaying, no need to do anything.
5472468Swnj 	 */
5485406Swnj 	if (tp->t_state&(TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
54913Sbill 		goto out;
5502468Swnj 	/*
5512468Swnj 	 * If there are sleepers, and output has drained below low
5522468Swnj 	 * water mark, wake up the sleepers.
5532468Swnj 	 */
5545406Swnj 	if (tp->t_outq.c_cc<=TTLOWAT(tp)) {
5555406Swnj 		if (tp->t_state&TS_ASLEEP) {
5565406Swnj 			tp->t_state &= ~TS_ASLEEP;
5575406Swnj 			wakeup((caddr_t)&tp->t_outq);
5585406Swnj 		}
5595406Swnj 		if (tp->t_wsel) {
5605406Swnj 			selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
5615406Swnj 			tp->t_wsel = 0;
5625406Swnj 			tp->t_state &= ~TS_WCOLL;
5635406Swnj 		}
56413Sbill 	}
5652468Swnj 	/*
5662468Swnj 	 * Now restart transmission unless the output queue is
5672468Swnj 	 * empty.
5682468Swnj 	 */
56913Sbill 	if (tp->t_outq.c_cc == 0)
57013Sbill 		goto out;
5713703Sroot 	if (tp->t_flags&RAW || tp->t_local&LLITOUT)
57213Sbill 		nch = ndqb(&tp->t_outq, 0);
5732395Swnj 	else {
57413Sbill 		nch = ndqb(&tp->t_outq, 0200);
5752468Swnj 		/*
5762468Swnj 		 * If first thing on queue is a delay process it.
5772468Swnj 		 */
57813Sbill 		if (nch == 0) {
57913Sbill 			nch = getc(&tp->t_outq);
5802468Swnj 			timeout(ttrstrt, (caddr_t)tp, (nch&0x7f)+6);
5815406Swnj 			tp->t_state |= TS_TIMEOUT;
58213Sbill 			goto out;
58313Sbill 		}
58413Sbill 	}
5852468Swnj 	/*
5862468Swnj 	 * If characters to transmit, restart transmission.
5872468Swnj 	 */
58813Sbill 	if (nch) {
5892468Swnj 		car = UBACVT(tp->t_outq.c_cf, dhinfo[dh]->ui_ubanum);
5902468Swnj 		addr->un.dhcsrl = unit|((car>>12)&0x30)|DH_IE;
5913586Sroot 		/*
5923586Sroot 		 * The following nonsense with short word
5933586Sroot 		 * is to make sure the dhbar |= word below
5943586Sroot 		 * is done with an interlocking bisw2 instruction.
5953586Sroot 		 */
5963586Sroot 		{ short word = 1 << unit;
5973586Sroot 		dhsar[dh] |= word;
5982468Swnj 		addr->dhcar = car;
59913Sbill 		addr->dhbcr = -nch;
6003586Sroot 		addr->dhbar |= word;
6013586Sroot 		}
6025406Swnj 		tp->t_state |= TS_BUSY;
60313Sbill 	}
6042395Swnj out:
60513Sbill 	splx(s);
60613Sbill }
60713Sbill 
60813Sbill /*
6092468Swnj  * Stop output on a line, e.g. for ^S/^Q or output flush.
61013Sbill  */
61113Sbill /*ARGSUSED*/
61213Sbill dhstop(tp, flag)
6132468Swnj 	register struct tty *tp;
61413Sbill {
6152479Swnj 	register struct dhdevice *addr;
6162395Swnj 	register int unit, s;
61713Sbill 
6182479Swnj 	addr = (struct dhdevice *)tp->t_addr;
6192468Swnj 	/*
6202468Swnj 	 * Block input/output interrupts while messing with state.
6212468Swnj 	 */
6222468Swnj 	s = spl5();
6235406Swnj 	if (tp->t_state & TS_BUSY) {
6242468Swnj 		/*
6252468Swnj 		 * Device is transmitting; stop output
6262468Swnj 		 * by selecting the line and setting the byte
6272468Swnj 		 * count to -1.  We will clean up later
6282468Swnj 		 * by examining the address where the dh stopped.
6292468Swnj 		 */
6302395Swnj 		unit = minor(tp->t_dev);
6312456Swnj 		addr->un.dhcsrl = (unit&017) | DH_IE;
6325406Swnj 		if ((tp->t_state&TS_TTSTOP)==0)
6335406Swnj 			tp->t_state |= TS_FLUSH;
634113Sbill 		addr->dhbcr = -1;
635113Sbill 	}
63613Sbill 	splx(s);
63713Sbill }
63813Sbill 
639168Sbill /*
640280Sbill  * Reset state of driver if UBA reset was necessary.
641280Sbill  * Reset the csrl and lpr registers on open lines, and
642280Sbill  * restart transmitters.
643280Sbill  */
6442395Swnj dhreset(uban)
6452468Swnj 	int uban;
646280Sbill {
6472395Swnj 	register int dh, unit;
648280Sbill 	register struct tty *tp;
6492974Swnj 	register struct uba_device *ui;
6502421Skre 	int i;
651280Sbill 
6522421Skre 	if (dh_ubinfo[uban] == 0)
6532421Skre 		return;
6542421Skre 	ubarelse(uban, &dh_ubinfo[uban]);
6552421Skre 	dh_ubinfo[uban] = uballoc(uban, (caddr_t)cfree,
6562770Swnj 	    512+nclist*sizeof (struct cblock), 0);
6572421Skre 	cbase[uban] = dh_ubinfo[uban]&0x3ffff;
6582395Swnj 	dh = 0;
6592643Swnj 	for (dh = 0; dh < NDH; dh++) {
6602421Skre 		ui = dhinfo[dh];
6612421Skre 		if (ui == 0 || ui->ui_alive == 0 || ui->ui_ubanum != uban)
6622421Skre 			continue;
6632924Swnj 		printf(" dh%d", dh);
6642479Swnj 		((struct dhdevice *)ui->ui_addr)->un.dhcsr |= DH_IE;
6652479Swnj 		((struct dhdevice *)ui->ui_addr)->dhsilo = 16;
6662421Skre 		unit = dh * 16;
6672421Skre 		for (i = 0; i < 16; i++) {
6682421Skre 			tp = &dh11[unit];
6695406Swnj 			if (tp->t_state & (TS_ISOPEN|TS_WOPEN)) {
6702421Skre 				dhparam(unit);
6712479Swnj 				dmctl(unit, DML_ON, DMSET);
6725406Swnj 				tp->t_state &= ~TS_BUSY;
6732421Skre 				dhstart(tp);
6742421Skre 			}
6752421Skre 			unit++;
676300Sbill 		}
677300Sbill 	}
678300Sbill 	dhtimer();
679280Sbill }
6802395Swnj 
6812468Swnj /*
6822468Swnj  * At software clock interrupt time or after a UNIBUS reset
6832468Swnj  * empty all the dh silos.
6842468Swnj  */
6852456Swnj dhtimer()
6862456Swnj {
6872456Swnj 	register int dh;
6888159Sroot 	register int s = spl5();
6892456Swnj 
6902643Swnj 	for (dh = 0; dh < NDH; dh++)
6912456Swnj 		dhrint(dh);
6928159Sroot 	splx(s);
6932456Swnj }
6942456Swnj 
6952468Swnj /*
6962479Swnj  * Turn on the line associated with dh dev.
6972468Swnj  */
6982468Swnj dmopen(dev)
6992468Swnj 	dev_t dev;
7002468Swnj {
7012468Swnj 	register struct tty *tp;
7022468Swnj 	register struct dmdevice *addr;
7032974Swnj 	register struct uba_device *ui;
7042468Swnj 	register int unit;
7052468Swnj 	register int dm;
7063792Swnj 	int s;
7072468Swnj 
7082468Swnj 	unit = minor(dev);
7092479Swnj 	dm = unit >> 4;
7102468Swnj 	tp = &dh11[unit];
7112566Swnj 	unit &= 0xf;
7122643Swnj 	if (dm >= NDH || (ui = dminfo[dm]) == 0 || ui->ui_alive == 0 ||
7132566Swnj 	    (dhsoftCAR[dm]&(1<<unit))) {
7145406Swnj 		tp->t_state |= TS_CARR_ON;
7152468Swnj 		return;
7162468Swnj 	}
7172468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7183792Swnj 	s = spl5();
7192479Swnj 	addr->dmcsr &= ~DM_SE;
7202479Swnj 	while (addr->dmcsr & DM_BUSY)
7212468Swnj 		;
7222566Swnj 	addr->dmcsr = unit;
7232479Swnj 	addr->dmlstat = DML_ON;
7242479Swnj 	if (addr->dmlstat&DML_CAR)
7255406Swnj 		tp->t_state |= TS_CARR_ON;
7263792Swnj 	addr->dmcsr = DM_IE|DM_SE;
7275406Swnj 	while ((tp->t_state&TS_CARR_ON)==0)
7282468Swnj 		sleep((caddr_t)&tp->t_rawq, TTIPRI);
7293792Swnj 	splx(s);
7302468Swnj }
7312468Swnj 
7322468Swnj /*
7332468Swnj  * Dump control bits into the DM registers.
7342468Swnj  */
7352468Swnj dmctl(dev, bits, how)
7362468Swnj 	dev_t dev;
7372468Swnj 	int bits, how;
7382468Swnj {
7392974Swnj 	register struct uba_device *ui;
7402468Swnj 	register struct dmdevice *addr;
7412468Swnj 	register int unit, s;
7422468Swnj 	int dm;
7432468Swnj 
7442468Swnj 	unit = minor(dev);
7452468Swnj 	dm = unit >> 4;
7462468Swnj 	if ((ui = dminfo[dm]) == 0 || ui->ui_alive == 0)
7472468Swnj 		return;
7482468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7492468Swnj 	s = spl5();
7502479Swnj 	addr->dmcsr &= ~DM_SE;
7512479Swnj 	while (addr->dmcsr & DM_BUSY)
7522468Swnj 		;
7532468Swnj 	addr->dmcsr = unit & 0xf;
7542468Swnj 	switch(how) {
7552468Swnj 	case DMSET:
7562468Swnj 		addr->dmlstat = bits;
7572468Swnj 		break;
7582468Swnj 	case DMBIS:
7592468Swnj 		addr->dmlstat |= bits;
7602468Swnj 		break;
7612468Swnj 	case DMBIC:
7622468Swnj 		addr->dmlstat &= ~bits;
7632468Swnj 		break;
7642468Swnj 	}
7653792Swnj 	addr->dmcsr = DM_IE|DM_SE;
7662468Swnj 	splx(s);
7672468Swnj }
7682468Swnj 
7692468Swnj /*
7702468Swnj  * DM11 interrupt; deal with carrier transitions.
7712468Swnj  */
7722468Swnj dmintr(dm)
7732468Swnj 	register int dm;
7742468Swnj {
7752974Swnj 	register struct uba_device *ui;
7762468Swnj 	register struct tty *tp;
7772468Swnj 	register struct dmdevice *addr;
7782468Swnj 
7792468Swnj 	ui = dminfo[dm];
7802479Swnj 	if (ui == 0)
7812479Swnj 		return;
7822468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7833997Sroot 	if (addr->dmcsr&DM_DONE) {
7843997Sroot 		if (addr->dmcsr&DM_CF) {
7853997Sroot 			tp = &dh11[(dm<<4)+(addr->dmcsr&0xf)];
7863997Sroot 			wakeup((caddr_t)&tp->t_rawq);
7875406Swnj 			if ((tp->t_state&TS_WOPEN)==0 &&
7883997Sroot 			    (tp->t_local&LMDMBUF)) {
7893997Sroot 				if (addr->dmlstat & DML_CAR) {
7905406Swnj 					tp->t_state &= ~TS_TTSTOP;
7913997Sroot 					ttstart(tp);
7925406Swnj 				} else if ((tp->t_state&TS_TTSTOP) == 0) {
7935406Swnj 					tp->t_state |= TS_TTSTOP;
7943997Sroot 					dhstop(tp, 0);
7953997Sroot 				}
7963997Sroot 			} else if ((addr->dmlstat&DML_CAR)==0) {
7975406Swnj 				if ((tp->t_state&TS_WOPEN)==0 &&
7983997Sroot 				    (tp->t_local&LNOHANG)==0) {
7993997Sroot 					gsignal(tp->t_pgrp, SIGHUP);
8003997Sroot 					gsignal(tp->t_pgrp, SIGCONT);
8013997Sroot 					addr->dmlstat = 0;
8023997Sroot 					flushtty(tp, FREAD|FWRITE);
8033997Sroot 				}
8045406Swnj 				tp->t_state &= ~TS_CARR_ON;
8053997Sroot 			} else
8065406Swnj 				tp->t_state |= TS_CARR_ON;
8073997Sroot 		}
8083997Sroot 		addr->dmcsr = DM_IE|DM_SE;
8092468Swnj 	}
8102468Swnj }
8112625Swnj #endif
812