xref: /csrg-svn/sys/vax/uba/dh.c (revision 25433)
123320Smckusick /*
223320Smckusick  * Copyright (c) 1982 Regents of the University of California.
323320Smckusick  * All rights reserved.  The Berkeley software License Agreement
423320Smckusick  * specifies the terms and conditions for redistribution.
523320Smckusick  *
6*25433Skarels  *	@(#)dh.c	6.13 (Berkeley) 11/08/85
723320Smckusick  */
813Sbill 
91934Swnj #include "dh.h"
102643Swnj #if NDH > 0
1113Sbill /*
122479Swnj  * DH-11/DM-11 driver
1313Sbill  */
149771Ssam #include "../machine/pte.h"
159771Ssam 
162730Swnj #include "bk.h"
1716062Skarels #include "uba.h"
1817122Sbloom #include "param.h"
1917122Sbloom #include "conf.h"
2017122Sbloom #include "dir.h"
2117122Sbloom #include "user.h"
2217122Sbloom #include "proc.h"
2317122Sbloom #include "ioctl.h"
2417122Sbloom #include "tty.h"
2517122Sbloom #include "map.h"
2617122Sbloom #include "buf.h"
2717122Sbloom #include "vm.h"
2817122Sbloom #include "kernel.h"
2918311Sralph #include "syslog.h"
308472Sroot 
3117122Sbloom #include "ubareg.h"
3217122Sbloom #include "ubavar.h"
3317122Sbloom #include "dhreg.h"
3417122Sbloom #include "dmreg.h"
358472Sroot 
3617122Sbloom #include "bkmac.h"
3717122Sbloom #include "clist.h"
3817122Sbloom #include "file.h"
3917122Sbloom #include "uio.h"
4013Sbill 
412468Swnj /*
422479Swnj  * Definition of the driver for the auto-configuration program.
432479Swnj  * There is one definition for the dh and one for the dm.
442468Swnj  */
4516190Skarels int	dhprobe(), dhattach(), dhrint(), dhxint(), dhtimer();
462974Swnj struct	uba_device *dhinfo[NDH];
472395Swnj u_short	dhstd[] = { 0 };
482395Swnj struct	uba_driver dhdriver =
492605Swnj 	{ dhprobe, 0, dhattach, 0, dhstd, "dh", dhinfo };
502395Swnj 
512605Swnj int	dmprobe(), dmattach(), dmintr();
522974Swnj struct	uba_device *dminfo[NDH];
532479Swnj u_short	dmstd[] = { 0 };
542479Swnj struct	uba_driver dmdriver =
552605Swnj 	{ dmprobe, 0, dmattach, 0, dmstd, "dm", dminfo };
5613Sbill 
576615Ssam #ifndef	PORTSELECTOR
5825394Skarels #define	ISPEED	B9600
596615Ssam #define	IFLAGS	(EVENP|ODDP|ECHO)
606615Ssam #else
616615Ssam #define	ISPEED	B4800
626615Ssam #define	IFLAGS	(EVENP|ODDP)
636615Ssam #endif
646615Ssam 
6516190Skarels #define	FASTTIMER	(hz/30)		/* scan rate with silos on */
6616190Skarels 
6713Sbill /*
682479Swnj  * Local variables for the driver
6913Sbill  */
702643Swnj short	dhsar[NDH];			/* software copy of last bar */
712643Swnj short	dhsoftCAR[NDH];
7213Sbill 
732643Swnj struct	tty dh11[NDH*16];
742643Swnj int	ndh11	= NDH*16;
752479Swnj int	dhact;				/* mask of active dh's */
7616190Skarels int	dhsilos;			/* mask of dh's with silo in use */
7716190Skarels int	dhchars[NDH];			/* recent input count */
7816190Skarels int	dhrate[NDH];			/* smoothed input count */
7916190Skarels int	dhhighrate = 100;		/* silo on if dhchars > dhhighrate */
8016190Skarels int	dhlowrate = 75;			/* silo off if dhrate < dhlowrate */
8116190Skarels static short timerstarted;
822479Swnj int	dhstart(), ttrstrt();
8313Sbill 
842479Swnj /*
852479Swnj  * The clist space is mapped by the driver onto each UNIBUS.
862479Swnj  * The UBACVT macro converts a clist space address for unibus uban
872479Swnj  * into an i/o space address for the DMA routine.
882479Swnj  */
8916062Skarels int	dh_ubinfo[NUBA];		/* info about allocated unibus map */
9016062Skarels int	cbase[NUBA];			/* base address in unibus map */
912479Swnj #define	UBACVT(x, uban)		(cbase[uban] + ((x)-(char *)cfree))
9213Sbill 
932456Swnj /*
942456Swnj  * Routine for configuration to force a dh to interrupt.
952456Swnj  * Set to transmit at 9600 baud, and cause a transmitter interrupt.
962456Swnj  */
972468Swnj /*ARGSUSED*/
982605Swnj dhprobe(reg)
992395Swnj 	caddr_t reg;
1002395Swnj {
1012468Swnj 	register int br, cvec;		/* these are ``value-result'' */
1022479Swnj 	register struct dhdevice *dhaddr = (struct dhdevice *)reg;
1032395Swnj 
1042605Swnj #ifdef lint
1052605Swnj 	br = 0; cvec = br; br = cvec;
1067384Sroot 	if (ndh11 == 0) ndh11 = 1;
1074932Swnj 	dhrint(0); dhxint(0);
1082605Swnj #endif
1092696Swnj #ifndef notdef
1102566Swnj 	dhaddr->un.dhcsr = DH_RIE|DH_MM|DH_RI;
1116380Swnj 	DELAY(1000);
1127384Sroot 	dhaddr->un.dhcsr &= ~DH_RI;
1132566Swnj 	dhaddr->un.dhcsr = 0;
1142566Swnj #else
1152456Swnj 	dhaddr->un.dhcsr = DH_TIE;
1162456Swnj 	DELAY(5);
1172456Swnj 	dhaddr->dhlpr = (B9600 << 10) | (B9600 << 6) | BITS7|PENABLE;
1182421Skre 	dhaddr->dhbcr = -1;
1192456Swnj 	dhaddr->dhcar = 0;
1202421Skre 	dhaddr->dhbar = 1;
1212456Swnj 	DELAY(100000);		/* wait 1/10'th of a sec for interrupt */
1222421Skre 	dhaddr->un.dhcsr = 0;
1232456Swnj 	if (cvec && cvec != 0x200)
1242456Swnj 		cvec -= 4;		/* transmit -> receive */
1252482Swnj #endif
1267408Skre 	return (sizeof (struct dhdevice));
1272395Swnj }
1282395Swnj 
1292456Swnj /*
1302605Swnj  * Routine called to attach a dh.
1312456Swnj  */
1322605Swnj dhattach(ui)
1332974Swnj 	struct uba_device *ui;
1342395Swnj {
1352395Swnj 
1362566Swnj 	dhsoftCAR[ui->ui_unit] = ui->ui_flags;
1372395Swnj }
1382395Swnj 
13913Sbill /*
1402479Swnj  * Configuration routine to cause a dm to interrupt.
1412479Swnj  */
1422605Swnj dmprobe(reg)
1432605Swnj 	caddr_t reg;
1442479Swnj {
1452479Swnj 	register int br, vec;			/* value-result */
1462605Swnj 	register struct dmdevice *dmaddr = (struct dmdevice *)reg;
1472479Swnj 
1482605Swnj #ifdef lint
1493101Swnj 	br = 0; vec = br; br = vec;
1506185Ssam 	dmintr(0);
1512605Swnj #endif
1522479Swnj 	dmaddr->dmcsr = DM_DONE|DM_IE;
1532479Swnj 	DELAY(20);
1542479Swnj 	dmaddr->dmcsr = 0;
1552605Swnj 	return (1);
1562479Swnj }
1572479Swnj 
1582605Swnj /*ARGSUSED*/
1592605Swnj dmattach(ui)
1602974Swnj 	struct uba_device *ui;
1612479Swnj {
1622479Swnj 
1632479Swnj 	/* no local state to set up */
1642479Swnj }
1652479Swnj 
1662479Swnj /*
1672468Swnj  * Open a DH11 line, mapping the clist onto the uba if this
1682468Swnj  * is the first dh on this uba.  Turn on this dh if this is
1692468Swnj  * the first use of it.  Also do a dmopen to wait for carrier.
17013Sbill  */
17113Sbill /*ARGSUSED*/
17213Sbill dhopen(dev, flag)
1732395Swnj 	dev_t dev;
17413Sbill {
17513Sbill 	register struct tty *tp;
1762395Swnj 	register int unit, dh;
1772479Swnj 	register struct dhdevice *addr;
1782974Swnj 	register struct uba_device *ui;
17913Sbill 	int s;
18013Sbill 
1812395Swnj 	unit = minor(dev);
1822395Swnj 	dh = unit >> 4;
1838566Sroot 	if (unit >= NDH*16 || (ui = dhinfo[dh])== 0 || ui->ui_alive == 0)
1848566Sroot 		return (ENXIO);
1852395Swnj 	tp = &dh11[unit];
1868566Sroot 	if (tp->t_state&TS_XCLUDE && u.u_uid!=0)
1878566Sroot 		return (EBUSY);
1882479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
18913Sbill 	tp->t_addr = (caddr_t)addr;
19013Sbill 	tp->t_oproc = dhstart;
1915406Swnj 	tp->t_state |= TS_WOPEN;
1922468Swnj 	/*
1932468Swnj 	 * While setting up state for this uba and this dh,
1942468Swnj 	 * block uba resets which can clear the state.
1952468Swnj 	 */
1962468Swnj 	s = spl5();
197*25433Skarels 	if (cbase[ui->ui_ubanum] == 0) {
1982395Swnj 		dh_ubinfo[ui->ui_ubanum] =
1992421Skre 		    uballoc(ui->ui_ubanum, (caddr_t)cfree,
200*25433Skarels 			nclist*sizeof(struct cblock), 0);
2012456Swnj 		cbase[ui->ui_ubanum] = dh_ubinfo[ui->ui_ubanum]&0x3ffff;
20213Sbill 	}
20316190Skarels 	if (timerstarted == 0) {
20416190Skarels 		timerstarted++;
20516190Skarels 		timeout(dhtimer, (caddr_t) 0, hz);
20616190Skarels 	}
2072456Swnj 	if ((dhact&(1<<dh)) == 0) {
2082456Swnj 		addr->un.dhcsr |= DH_IE;
2092468Swnj 		dhact |= (1<<dh);
21016190Skarels 		addr->dhsilo = 0;
2112456Swnj 	}
21213Sbill 	splx(s);
2132468Swnj 	/*
2142468Swnj 	 * If this is first open, initialze tty state to default.
2152468Swnj 	 */
2165406Swnj 	if ((tp->t_state&TS_ISOPEN) == 0) {
21713Sbill 		ttychars(tp);
21825394Skarels 		tp->t_ispeed = ISPEED;
21925394Skarels 		tp->t_ospeed = ISPEED;
22025394Skarels 		tp->t_flags = IFLAGS;
2212395Swnj 		dhparam(unit);
22213Sbill 	}
2232468Swnj 	/*
2242468Swnj 	 * Wait for carrier, then process line discipline specific open.
2252468Swnj 	 */
22613Sbill 	dmopen(dev);
2278566Sroot 	return ((*linesw[tp->t_line].l_open)(dev, tp));
22813Sbill }
22913Sbill 
23013Sbill /*
2312468Swnj  * Close a DH11 line, turning off the DM11.
23213Sbill  */
23313Sbill /*ARGSUSED*/
23413Sbill dhclose(dev, flag)
2352395Swnj 	dev_t dev;
2362395Swnj 	int flag;
23713Sbill {
23813Sbill 	register struct tty *tp;
2392395Swnj 	register unit;
24013Sbill 
2412395Swnj 	unit = minor(dev);
2422395Swnj 	tp = &dh11[unit];
24313Sbill 	(*linesw[tp->t_line].l_close)(tp);
2442479Swnj 	((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
2455406Swnj 	if (tp->t_state&TS_HUPCLS || (tp->t_state&TS_ISOPEN)==0)
2462479Swnj 		dmctl(unit, DML_OFF, DMSET);
24713Sbill 	ttyclose(tp);
24813Sbill }
24913Sbill 
2507725Sroot dhread(dev, uio)
2512395Swnj 	dev_t dev;
2527725Sroot 	struct uio *uio;
25313Sbill {
2548490Sroot 	register struct tty *tp = &dh11[minor(dev)];
25513Sbill 
2567725Sroot 	return ((*linesw[tp->t_line].l_read)(tp, uio));
25713Sbill }
25813Sbill 
2597831Sroot dhwrite(dev, uio)
2602395Swnj 	dev_t dev;
2617831Sroot 	struct uio *uio;
26213Sbill {
2638490Sroot 	register struct tty *tp = &dh11[minor(dev)];
26413Sbill 
2658490Sroot 	return ((*linesw[tp->t_line].l_write)(tp, uio));
26613Sbill }
26713Sbill 
26813Sbill /*
26913Sbill  * DH11 receiver interrupt.
27013Sbill  */
2712395Swnj dhrint(dh)
2722395Swnj 	int dh;
27313Sbill {
27413Sbill 	register struct tty *tp;
2752395Swnj 	register c;
2762479Swnj 	register struct dhdevice *addr;
277117Sbill 	register struct tty *tp0;
2782974Swnj 	register struct uba_device *ui;
2792924Swnj 	int overrun = 0;
28013Sbill 
2812395Swnj 	ui = dhinfo[dh];
2822479Swnj 	if (ui == 0 || ui->ui_alive == 0)
2832479Swnj 		return;
2842479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
2852468Swnj 	tp0 = &dh11[dh<<4];
2862468Swnj 	/*
2872468Swnj 	 * Loop fetching characters from the silo for this
2882468Swnj 	 * dh until there are no more in the silo.
2892468Swnj 	 */
2902468Swnj 	while ((c = addr->dhrcr) < 0) {
2912468Swnj 		tp = tp0 + ((c>>8)&0xf);
29216190Skarels 		dhchars[dh]++;
2935406Swnj 		if ((tp->t_state&TS_ISOPEN)==0) {
29425394Skarels 			wakeup((caddr_t)&tp->t_rawq);
29525394Skarels #ifdef PORTSELECTOR
29625394Skarels 			if ((tp->t_state&TS_WOPEN) == 0)
2976615Ssam #endif
29825394Skarels 				continue;
29913Sbill 		}
3002468Swnj 		if (c & DH_PE)
30113Sbill 			if ((tp->t_flags&(EVENP|ODDP))==EVENP
30213Sbill 			 || (tp->t_flags&(EVENP|ODDP))==ODDP )
30313Sbill 				continue;
3042924Swnj 		if ((c & DH_DO) && overrun == 0) {
30524840Seric 			log(LOG_WARNING, "dh%d: silo overflow\n", dh);
3062924Swnj 			overrun = 1;
3072924Swnj 		}
3082468Swnj 		if (c & DH_FE)
3092468Swnj 			/*
3102468Swnj 			 * At framing error (break) generate
3112468Swnj 			 * a null (in raw mode, for getty), or a
3122468Swnj 			 * interrupt (in cooked/cbreak mode).
3132468Swnj 			 */
31413Sbill 			if (tp->t_flags&RAW)
3152468Swnj 				c = 0;
31613Sbill 			else
3179549Ssam 				c = tp->t_intrc;
3182730Swnj #if NBK > 0
319139Sbill 		if (tp->t_line == NETLDISC) {
320117Sbill 			c &= 0177;
321168Sbill 			BKINPUT(c, tp);
322117Sbill 		} else
3232730Swnj #endif
3242468Swnj 			(*linesw[tp->t_line].l_rint)(c, tp);
32513Sbill 	}
32613Sbill }
32713Sbill 
32813Sbill /*
3292468Swnj  * Ioctl for DH11.
33013Sbill  */
33113Sbill /*ARGSUSED*/
3327629Ssam dhioctl(dev, cmd, data, flag)
3337629Ssam 	caddr_t data;
33413Sbill {
33513Sbill 	register struct tty *tp;
3368566Sroot 	register int unit = minor(dev);
3378566Sroot 	int error;
33813Sbill 
3392395Swnj 	tp = &dh11[unit];
3408566Sroot 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
3418566Sroot 	if (error >= 0)
3428566Sroot 		return (error);
3438566Sroot 	error = ttioctl(tp, cmd, data, flag);
3448566Sroot 	if (error >= 0) {
34517561Sbloom 		if (cmd == TIOCSETP || cmd == TIOCSETN || cmd == TIOCLBIS ||
34617561Sbloom 		    cmd == TIOCLBIC || cmd == TIOCLSET)
3472395Swnj 			dhparam(unit);
3488566Sroot 		return (error);
3498566Sroot 	}
3508566Sroot 	switch (cmd) {
3517629Ssam 
352168Sbill 	case TIOCSBRK:
3532479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak |= 1<<(unit&017);
354168Sbill 		break;
3557629Ssam 
356168Sbill 	case TIOCCBRK:
3572479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
358168Sbill 		break;
3597629Ssam 
360168Sbill 	case TIOCSDTR:
3612479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIS);
362168Sbill 		break;
3637629Ssam 
364168Sbill 	case TIOCCDTR:
3652479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIC);
366168Sbill 		break;
3677629Ssam 
368168Sbill 	default:
3698566Sroot 		return (ENOTTY);
370168Sbill 	}
3718566Sroot 	return (0);
37213Sbill }
37313Sbill 
37413Sbill /*
37513Sbill  * Set parameters from open or stty into the DH hardware
37613Sbill  * registers.
37713Sbill  */
3782395Swnj dhparam(unit)
3792395Swnj 	register int unit;
38013Sbill {
38113Sbill 	register struct tty *tp;
3822479Swnj 	register struct dhdevice *addr;
3832395Swnj 	register int lpar;
384300Sbill 	int s;
38513Sbill 
3862395Swnj 	tp = &dh11[unit];
3872479Swnj 	addr = (struct dhdevice *)tp->t_addr;
3882468Swnj 	/*
3892468Swnj 	 * Block interrupts so parameters will be set
3902468Swnj 	 * before the line interrupts.
3912468Swnj 	 */
392300Sbill 	s = spl5();
3932468Swnj 	addr->un.dhcsrl = (unit&0xf) | DH_IE;
39413Sbill 	if ((tp->t_ispeed)==0) {
3955406Swnj 		tp->t_state |= TS_HUPCLS;
3962479Swnj 		dmctl(unit, DML_OFF, DMSET);
39713Sbill 		return;
39813Sbill 	}
3992395Swnj 	lpar = ((tp->t_ospeed)<<10) | ((tp->t_ispeed)<<6);
4002468Swnj 	if ((tp->t_ispeed) == B134)
4012395Swnj 		lpar |= BITS6|PENABLE|HDUPLX;
40224269Slepreau 	else if (tp->t_flags & (RAW|LITOUT|PASS8))
4032395Swnj 		lpar |= BITS8;
40413Sbill 	else
4052395Swnj 		lpar |= BITS7|PENABLE;
40613Sbill 	if ((tp->t_flags&EVENP) == 0)
4072395Swnj 		lpar |= OPAR;
4082468Swnj 	if ((tp->t_ospeed) == B110)
4092395Swnj 		lpar |= TWOSB;
4102395Swnj 	addr->dhlpr = lpar;
411300Sbill 	splx(s);
41213Sbill }
41313Sbill 
41413Sbill /*
41513Sbill  * DH11 transmitter interrupt.
41613Sbill  * Restart each line which used to be active but has
41713Sbill  * terminated transmission since the last interrupt.
41813Sbill  */
4192395Swnj dhxint(dh)
4202395Swnj 	int dh;
42113Sbill {
42213Sbill 	register struct tty *tp;
4232479Swnj 	register struct dhdevice *addr;
42413Sbill 	short ttybit, bar, *sbar;
4252974Swnj 	register struct uba_device *ui;
4262468Swnj 	register int unit;
4272605Swnj 	u_short cntr;
42813Sbill 
4292395Swnj 	ui = dhinfo[dh];
4302479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
4312456Swnj 	if (addr->un.dhcsr & DH_NXM) {
4322456Swnj 		addr->un.dhcsr |= DH_CNI;
4332924Swnj 		printf("dh%d: NXM\n", dh);
434105Sbill 	}
4352395Swnj 	sbar = &dhsar[dh];
43613Sbill 	bar = *sbar & ~addr->dhbar;
4372395Swnj 	unit = dh * 16; ttybit = 1;
4382468Swnj 	addr->un.dhcsr &= (short)~DH_TI;
4392468Swnj 	for (; bar; unit++, ttybit <<= 1) {
4402468Swnj 		if (bar & ttybit) {
44113Sbill 			*sbar &= ~ttybit;
44213Sbill 			bar &= ~ttybit;
4432395Swnj 			tp = &dh11[unit];
4445406Swnj 			tp->t_state &= ~TS_BUSY;
4455406Swnj 			if (tp->t_state&TS_FLUSH)
4465406Swnj 				tp->t_state &= ~TS_FLUSH;
447113Sbill 			else {
4482456Swnj 				addr->un.dhcsrl = (unit&017)|DH_IE;
4492468Swnj 				/*
4502468Swnj 				 * Do arithmetic in a short to make up
4512468Swnj 				 * for lost 16&17 bits.
4522468Swnj 				 */
4532605Swnj 				cntr = addr->dhcar -
4542468Swnj 				    UBACVT(tp->t_outq.c_cf, ui->ui_ubanum);
4553101Swnj 				ndflush(&tp->t_outq, (int)cntr);
456113Sbill 			}
457113Sbill 			if (tp->t_line)
45813Sbill 				(*linesw[tp->t_line].l_start)(tp);
459113Sbill 			else
46013Sbill 				dhstart(tp);
46113Sbill 		}
46213Sbill 	}
46313Sbill }
46413Sbill 
46513Sbill /*
46613Sbill  * Start (restart) transmission on the given DH11 line.
46713Sbill  */
46813Sbill dhstart(tp)
4692395Swnj 	register struct tty *tp;
47013Sbill {
4712479Swnj 	register struct dhdevice *addr;
4722468Swnj 	register int car, dh, unit, nch;
4732395Swnj 	int s;
47413Sbill 
4752468Swnj 	unit = minor(tp->t_dev);
4762468Swnj 	dh = unit >> 4;
4772468Swnj 	unit &= 0xf;
4782479Swnj 	addr = (struct dhdevice *)tp->t_addr;
4792468Swnj 
48013Sbill 	/*
4812468Swnj 	 * Must hold interrupts in following code to prevent
4822468Swnj 	 * state of the tp from changing.
48313Sbill 	 */
48413Sbill 	s = spl5();
4852468Swnj 	/*
4862468Swnj 	 * If it's currently active, or delaying, no need to do anything.
4872468Swnj 	 */
4885406Swnj 	if (tp->t_state&(TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
48913Sbill 		goto out;
4902468Swnj 	/*
4912468Swnj 	 * If there are sleepers, and output has drained below low
4922468Swnj 	 * water mark, wake up the sleepers.
4932468Swnj 	 */
4945406Swnj 	if (tp->t_outq.c_cc<=TTLOWAT(tp)) {
4955406Swnj 		if (tp->t_state&TS_ASLEEP) {
4965406Swnj 			tp->t_state &= ~TS_ASLEEP;
4975406Swnj 			wakeup((caddr_t)&tp->t_outq);
4985406Swnj 		}
4995406Swnj 		if (tp->t_wsel) {
5005406Swnj 			selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
5015406Swnj 			tp->t_wsel = 0;
5025406Swnj 			tp->t_state &= ~TS_WCOLL;
5035406Swnj 		}
50413Sbill 	}
5052468Swnj 	/*
5062468Swnj 	 * Now restart transmission unless the output queue is
5072468Swnj 	 * empty.
5082468Swnj 	 */
50913Sbill 	if (tp->t_outq.c_cc == 0)
51013Sbill 		goto out;
5119549Ssam 	if (tp->t_flags & (RAW|LITOUT))
51213Sbill 		nch = ndqb(&tp->t_outq, 0);
5132395Swnj 	else {
51413Sbill 		nch = ndqb(&tp->t_outq, 0200);
5152468Swnj 		/*
5162468Swnj 		 * If first thing on queue is a delay process it.
5172468Swnj 		 */
51813Sbill 		if (nch == 0) {
51913Sbill 			nch = getc(&tp->t_outq);
5202468Swnj 			timeout(ttrstrt, (caddr_t)tp, (nch&0x7f)+6);
5215406Swnj 			tp->t_state |= TS_TIMEOUT;
52213Sbill 			goto out;
52313Sbill 		}
52413Sbill 	}
5252468Swnj 	/*
5262468Swnj 	 * If characters to transmit, restart transmission.
5272468Swnj 	 */
52813Sbill 	if (nch) {
5292468Swnj 		car = UBACVT(tp->t_outq.c_cf, dhinfo[dh]->ui_ubanum);
5302468Swnj 		addr->un.dhcsrl = unit|((car>>12)&0x30)|DH_IE;
5313586Sroot 		/*
5323586Sroot 		 * The following nonsense with short word
5333586Sroot 		 * is to make sure the dhbar |= word below
5343586Sroot 		 * is done with an interlocking bisw2 instruction.
5353586Sroot 		 */
5363586Sroot 		{ short word = 1 << unit;
5373586Sroot 		dhsar[dh] |= word;
5382468Swnj 		addr->dhcar = car;
53913Sbill 		addr->dhbcr = -nch;
5403586Sroot 		addr->dhbar |= word;
5413586Sroot 		}
5425406Swnj 		tp->t_state |= TS_BUSY;
54313Sbill 	}
5442395Swnj out:
54513Sbill 	splx(s);
54613Sbill }
54713Sbill 
54813Sbill /*
5492468Swnj  * Stop output on a line, e.g. for ^S/^Q or output flush.
55013Sbill  */
55113Sbill /*ARGSUSED*/
55213Sbill dhstop(tp, flag)
5532468Swnj 	register struct tty *tp;
55413Sbill {
5552479Swnj 	register struct dhdevice *addr;
5562395Swnj 	register int unit, s;
55713Sbill 
5582479Swnj 	addr = (struct dhdevice *)tp->t_addr;
5592468Swnj 	/*
5602468Swnj 	 * Block input/output interrupts while messing with state.
5612468Swnj 	 */
5622468Swnj 	s = spl5();
5635406Swnj 	if (tp->t_state & TS_BUSY) {
5642468Swnj 		/*
5652468Swnj 		 * Device is transmitting; stop output
5662468Swnj 		 * by selecting the line and setting the byte
5672468Swnj 		 * count to -1.  We will clean up later
5682468Swnj 		 * by examining the address where the dh stopped.
5692468Swnj 		 */
5702395Swnj 		unit = minor(tp->t_dev);
5712456Swnj 		addr->un.dhcsrl = (unit&017) | DH_IE;
5725406Swnj 		if ((tp->t_state&TS_TTSTOP)==0)
5735406Swnj 			tp->t_state |= TS_FLUSH;
574113Sbill 		addr->dhbcr = -1;
575113Sbill 	}
57613Sbill 	splx(s);
57713Sbill }
57813Sbill 
579168Sbill /*
580280Sbill  * Reset state of driver if UBA reset was necessary.
581280Sbill  * Reset the csrl and lpr registers on open lines, and
582280Sbill  * restart transmitters.
583280Sbill  */
5842395Swnj dhreset(uban)
5852468Swnj 	int uban;
586280Sbill {
5872395Swnj 	register int dh, unit;
588280Sbill 	register struct tty *tp;
5892974Swnj 	register struct uba_device *ui;
5902421Skre 	int i;
591280Sbill 
5922395Swnj 	dh = 0;
5932643Swnj 	for (dh = 0; dh < NDH; dh++) {
5942421Skre 		ui = dhinfo[dh];
5952421Skre 		if (ui == 0 || ui->ui_alive == 0 || ui->ui_ubanum != uban)
5962421Skre 			continue;
5972924Swnj 		printf(" dh%d", dh);
598*25433Skarels 		if (cbase[uban] == 0) {
599*25433Skarels 			dh_ubinfo[uban] = uballoc(uban, (caddr_t)cfree,
600*25433Skarels 			    nclist*sizeof (struct cblock), 0);
601*25433Skarels 			cbase[uban] = dh_ubinfo[uban]&0x3ffff;
602*25433Skarels 		}
6032479Swnj 		((struct dhdevice *)ui->ui_addr)->un.dhcsr |= DH_IE;
60416190Skarels 		((struct dhdevice *)ui->ui_addr)->dhsilo = 0;
6052421Skre 		unit = dh * 16;
6062421Skre 		for (i = 0; i < 16; i++) {
6072421Skre 			tp = &dh11[unit];
6085406Swnj 			if (tp->t_state & (TS_ISOPEN|TS_WOPEN)) {
6092421Skre 				dhparam(unit);
6102479Swnj 				dmctl(unit, DML_ON, DMSET);
6115406Swnj 				tp->t_state &= ~TS_BUSY;
6122421Skre 				dhstart(tp);
6132421Skre 			}
6142421Skre 			unit++;
615300Sbill 		}
616300Sbill 	}
61716190Skarels 	dhsilos = 0;
618280Sbill }
6192395Swnj 
62016190Skarels int dhtransitions, dhslowtimers, dhfasttimers;		/*DEBUG*/
6212468Swnj /*
62216190Skarels  * At software clock interrupt time, check status.
62316190Skarels  * Empty all the dh silos that are in use, and decide whether
62416190Skarels  * to turn any silos off or on.
6252468Swnj  */
6262456Swnj dhtimer()
6272456Swnj {
62816190Skarels 	register int dh, s;
62916190Skarels 	static int timercalls;
6302456Swnj 
63116190Skarels 	if (dhsilos) {
63216190Skarels 		dhfasttimers++;		/*DEBUG*/
63316190Skarels 		timercalls++;
63416190Skarels 		s = spl5();
63516190Skarels 		for (dh = 0; dh < NDH; dh++)
63616190Skarels 			if (dhsilos & (1 << dh))
63716190Skarels 				dhrint(dh);
63816190Skarels 		splx(s);
63916190Skarels 	}
64016190Skarels 	if ((dhsilos == 0) || ((timercalls += FASTTIMER) >= hz)) {
64116190Skarels 		dhslowtimers++;		/*DEBUG*/
64216190Skarels 		timercalls = 0;
64316190Skarels 		for (dh = 0; dh < NDH; dh++) {
64416190Skarels 		    ave(dhrate[dh], dhchars[dh], 8);
64516190Skarels 		    if ((dhchars[dh] > dhhighrate) &&
64616190Skarels 		      ((dhsilos & (1 << dh)) == 0)) {
64716190Skarels 			((struct dhdevice *)(dhinfo[dh]->ui_addr))->dhsilo =
64816190Skarels 			    (dhchars[dh] > 500? 32 : 16);
64916190Skarels 			dhsilos |= (1 << dh);
65016190Skarels 			dhtransitions++;		/*DEBUG*/
65116190Skarels 		    } else if ((dhsilos & (1 << dh)) &&
65216190Skarels 		      (dhrate[dh] < dhlowrate)) {
65316190Skarels 			((struct dhdevice *)(dhinfo[dh]->ui_addr))->dhsilo = 0;
65416190Skarels 			dhsilos &= ~(1 << dh);
65516190Skarels 		    }
65616190Skarels 		    dhchars[dh] = 0;
65716190Skarels 		}
65816190Skarels 	}
65916190Skarels 	timeout(dhtimer, (caddr_t) 0, dhsilos? FASTTIMER: hz);
6602456Swnj }
6612456Swnj 
6622468Swnj /*
6632479Swnj  * Turn on the line associated with dh dev.
6642468Swnj  */
6652468Swnj dmopen(dev)
6662468Swnj 	dev_t dev;
6672468Swnj {
6682468Swnj 	register struct tty *tp;
6692468Swnj 	register struct dmdevice *addr;
6702974Swnj 	register struct uba_device *ui;
6712468Swnj 	register int unit;
6722468Swnj 	register int dm;
6733792Swnj 	int s;
6742468Swnj 
6752468Swnj 	unit = minor(dev);
6762479Swnj 	dm = unit >> 4;
6772468Swnj 	tp = &dh11[unit];
6782566Swnj 	unit &= 0xf;
67916942Skarels 	if (dm >= NDH || (ui = dminfo[dm]) == 0 || ui->ui_alive == 0) {
6805406Swnj 		tp->t_state |= TS_CARR_ON;
6812468Swnj 		return;
6822468Swnj 	}
6832468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
6843792Swnj 	s = spl5();
6852479Swnj 	addr->dmcsr &= ~DM_SE;
6862479Swnj 	while (addr->dmcsr & DM_BUSY)
6872468Swnj 		;
6882566Swnj 	addr->dmcsr = unit;
6892479Swnj 	addr->dmlstat = DML_ON;
69016942Skarels 	if ((addr->dmlstat&DML_CAR) || (dhsoftCAR[dm]&(1<<unit)))
6915406Swnj 		tp->t_state |= TS_CARR_ON;
6923792Swnj 	addr->dmcsr = DM_IE|DM_SE;
6935406Swnj 	while ((tp->t_state&TS_CARR_ON)==0)
6942468Swnj 		sleep((caddr_t)&tp->t_rawq, TTIPRI);
6953792Swnj 	splx(s);
6962468Swnj }
6972468Swnj 
6982468Swnj /*
6992468Swnj  * Dump control bits into the DM registers.
7002468Swnj  */
7012468Swnj dmctl(dev, bits, how)
7022468Swnj 	dev_t dev;
7032468Swnj 	int bits, how;
7042468Swnj {
7052974Swnj 	register struct uba_device *ui;
7062468Swnj 	register struct dmdevice *addr;
7072468Swnj 	register int unit, s;
7082468Swnj 	int dm;
7092468Swnj 
7102468Swnj 	unit = minor(dev);
7112468Swnj 	dm = unit >> 4;
7122468Swnj 	if ((ui = dminfo[dm]) == 0 || ui->ui_alive == 0)
7132468Swnj 		return;
7142468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7152468Swnj 	s = spl5();
7162479Swnj 	addr->dmcsr &= ~DM_SE;
7172479Swnj 	while (addr->dmcsr & DM_BUSY)
7182468Swnj 		;
7192468Swnj 	addr->dmcsr = unit & 0xf;
7202468Swnj 	switch(how) {
7212468Swnj 	case DMSET:
7222468Swnj 		addr->dmlstat = bits;
7232468Swnj 		break;
7242468Swnj 	case DMBIS:
7252468Swnj 		addr->dmlstat |= bits;
7262468Swnj 		break;
7272468Swnj 	case DMBIC:
7282468Swnj 		addr->dmlstat &= ~bits;
7292468Swnj 		break;
7302468Swnj 	}
7313792Swnj 	addr->dmcsr = DM_IE|DM_SE;
7322468Swnj 	splx(s);
7332468Swnj }
7342468Swnj 
7352468Swnj /*
7362468Swnj  * DM11 interrupt; deal with carrier transitions.
7372468Swnj  */
7382468Swnj dmintr(dm)
7392468Swnj 	register int dm;
7402468Swnj {
7412974Swnj 	register struct uba_device *ui;
7422468Swnj 	register struct tty *tp;
7432468Swnj 	register struct dmdevice *addr;
74416942Skarels 	int unit;
7452468Swnj 
7462468Swnj 	ui = dminfo[dm];
7472479Swnj 	if (ui == 0)
7482479Swnj 		return;
7492468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7503997Sroot 	if (addr->dmcsr&DM_DONE) {
7513997Sroot 		if (addr->dmcsr&DM_CF) {
75216942Skarels 			unit = addr->dmcsr & 0xf;
75316942Skarels 			tp = &dh11[(dm << 4) + unit];
75425394Skarels 			if (addr->dmlstat & DML_CAR)
75525394Skarels 				(void)(*linesw[tp->t_line].l_modem)(tp, 1);
75625394Skarels 			else if ((dhsoftCAR[dm] & (1<<unit)) == 0 &&
75725394Skarels 			    (*linesw[tp->t_line].l_modem)(tp, 0) == 0)
75825394Skarels 				addr->dmlstat = 0;
7593997Sroot 		}
7603997Sroot 		addr->dmcsr = DM_IE|DM_SE;
7612468Swnj 	}
7622468Swnj }
7632625Swnj #endif
764