xref: /csrg-svn/sys/vax/uba/dh.c (revision 26219)
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*26219Skarels  *	@(#)dh.c	6.15 (Berkeley) 02/17/86
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;
137*26219Skarels 	cbase[ui->ui_ubanum] = -1;
1382395Swnj }
1392395Swnj 
14013Sbill /*
1412479Swnj  * Configuration routine to cause a dm to interrupt.
1422479Swnj  */
1432605Swnj dmprobe(reg)
1442605Swnj 	caddr_t reg;
1452479Swnj {
1462479Swnj 	register int br, vec;			/* value-result */
1472605Swnj 	register struct dmdevice *dmaddr = (struct dmdevice *)reg;
1482479Swnj 
1492605Swnj #ifdef lint
1503101Swnj 	br = 0; vec = br; br = vec;
1516185Ssam 	dmintr(0);
1522605Swnj #endif
1532479Swnj 	dmaddr->dmcsr = DM_DONE|DM_IE;
1542479Swnj 	DELAY(20);
1552479Swnj 	dmaddr->dmcsr = 0;
1562605Swnj 	return (1);
1572479Swnj }
1582479Swnj 
1592605Swnj /*ARGSUSED*/
1602605Swnj dmattach(ui)
1612974Swnj 	struct uba_device *ui;
1622479Swnj {
1632479Swnj 
1642479Swnj 	/* no local state to set up */
1652479Swnj }
1662479Swnj 
1672479Swnj /*
1682468Swnj  * Open a DH11 line, mapping the clist onto the uba if this
1692468Swnj  * is the first dh on this uba.  Turn on this dh if this is
1702468Swnj  * the first use of it.  Also do a dmopen to wait for carrier.
17113Sbill  */
17213Sbill /*ARGSUSED*/
17313Sbill dhopen(dev, flag)
1742395Swnj 	dev_t dev;
17513Sbill {
17613Sbill 	register struct tty *tp;
1772395Swnj 	register int unit, dh;
1782479Swnj 	register struct dhdevice *addr;
1792974Swnj 	register struct uba_device *ui;
18013Sbill 	int s;
18113Sbill 
1822395Swnj 	unit = minor(dev);
1832395Swnj 	dh = unit >> 4;
1848566Sroot 	if (unit >= NDH*16 || (ui = dhinfo[dh])== 0 || ui->ui_alive == 0)
1858566Sroot 		return (ENXIO);
1862395Swnj 	tp = &dh11[unit];
1878566Sroot 	if (tp->t_state&TS_XCLUDE && u.u_uid!=0)
1888566Sroot 		return (EBUSY);
1892479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
19013Sbill 	tp->t_addr = (caddr_t)addr;
19113Sbill 	tp->t_oproc = dhstart;
1925406Swnj 	tp->t_state |= TS_WOPEN;
1932468Swnj 	/*
1942468Swnj 	 * While setting up state for this uba and this dh,
1952468Swnj 	 * block uba resets which can clear the state.
1962468Swnj 	 */
1972468Swnj 	s = spl5();
198*26219Skarels 	if (cbase[ui->ui_ubanum] == -1) {
1992395Swnj 		dh_ubinfo[ui->ui_ubanum] =
2002421Skre 		    uballoc(ui->ui_ubanum, (caddr_t)cfree,
20125433Skarels 			nclist*sizeof(struct cblock), 0);
202*26219Skarels 		cbase[ui->ui_ubanum] = UBAI_ADDR(dh_ubinfo[ui->ui_ubanum]);
20313Sbill 	}
20416190Skarels 	if (timerstarted == 0) {
20516190Skarels 		timerstarted++;
20616190Skarels 		timeout(dhtimer, (caddr_t) 0, hz);
20716190Skarels 	}
2082456Swnj 	if ((dhact&(1<<dh)) == 0) {
2092456Swnj 		addr->un.dhcsr |= DH_IE;
2102468Swnj 		dhact |= (1<<dh);
21116190Skarels 		addr->dhsilo = 0;
2122456Swnj 	}
21313Sbill 	splx(s);
2142468Swnj 	/*
2152468Swnj 	 * If this is first open, initialze tty state to default.
2162468Swnj 	 */
2175406Swnj 	if ((tp->t_state&TS_ISOPEN) == 0) {
21813Sbill 		ttychars(tp);
21925394Skarels 		tp->t_ispeed = ISPEED;
22025394Skarels 		tp->t_ospeed = ISPEED;
22125394Skarels 		tp->t_flags = IFLAGS;
2222395Swnj 		dhparam(unit);
22313Sbill 	}
2242468Swnj 	/*
2252468Swnj 	 * Wait for carrier, then process line discipline specific open.
2262468Swnj 	 */
22713Sbill 	dmopen(dev);
2288566Sroot 	return ((*linesw[tp->t_line].l_open)(dev, tp));
22913Sbill }
23013Sbill 
23113Sbill /*
2322468Swnj  * Close a DH11 line, turning off the DM11.
23313Sbill  */
23413Sbill /*ARGSUSED*/
23513Sbill dhclose(dev, flag)
2362395Swnj 	dev_t dev;
2372395Swnj 	int flag;
23813Sbill {
23913Sbill 	register struct tty *tp;
2402395Swnj 	register unit;
24113Sbill 
2422395Swnj 	unit = minor(dev);
2432395Swnj 	tp = &dh11[unit];
24413Sbill 	(*linesw[tp->t_line].l_close)(tp);
2452479Swnj 	((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
2465406Swnj 	if (tp->t_state&TS_HUPCLS || (tp->t_state&TS_ISOPEN)==0)
2472479Swnj 		dmctl(unit, DML_OFF, DMSET);
24813Sbill 	ttyclose(tp);
24913Sbill }
25013Sbill 
2517725Sroot dhread(dev, uio)
2522395Swnj 	dev_t dev;
2537725Sroot 	struct uio *uio;
25413Sbill {
2558490Sroot 	register struct tty *tp = &dh11[minor(dev)];
25613Sbill 
2577725Sroot 	return ((*linesw[tp->t_line].l_read)(tp, uio));
25813Sbill }
25913Sbill 
2607831Sroot dhwrite(dev, uio)
2612395Swnj 	dev_t dev;
2627831Sroot 	struct uio *uio;
26313Sbill {
2648490Sroot 	register struct tty *tp = &dh11[minor(dev)];
26513Sbill 
2668490Sroot 	return ((*linesw[tp->t_line].l_write)(tp, uio));
26713Sbill }
26813Sbill 
26913Sbill /*
27013Sbill  * DH11 receiver interrupt.
27113Sbill  */
2722395Swnj dhrint(dh)
2732395Swnj 	int dh;
27413Sbill {
27513Sbill 	register struct tty *tp;
2762395Swnj 	register c;
2772479Swnj 	register struct dhdevice *addr;
278117Sbill 	register struct tty *tp0;
2792974Swnj 	register struct uba_device *ui;
2802924Swnj 	int overrun = 0;
28113Sbill 
2822395Swnj 	ui = dhinfo[dh];
2832479Swnj 	if (ui == 0 || ui->ui_alive == 0)
2842479Swnj 		return;
2852479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
2862468Swnj 	tp0 = &dh11[dh<<4];
2872468Swnj 	/*
2882468Swnj 	 * Loop fetching characters from the silo for this
2892468Swnj 	 * dh until there are no more in the silo.
2902468Swnj 	 */
2912468Swnj 	while ((c = addr->dhrcr) < 0) {
2922468Swnj 		tp = tp0 + ((c>>8)&0xf);
29316190Skarels 		dhchars[dh]++;
2945406Swnj 		if ((tp->t_state&TS_ISOPEN)==0) {
29525394Skarels 			wakeup((caddr_t)&tp->t_rawq);
29625394Skarels #ifdef PORTSELECTOR
29725394Skarels 			if ((tp->t_state&TS_WOPEN) == 0)
2986615Ssam #endif
29925394Skarels 				continue;
30013Sbill 		}
3012468Swnj 		if (c & DH_PE)
30213Sbill 			if ((tp->t_flags&(EVENP|ODDP))==EVENP
30313Sbill 			 || (tp->t_flags&(EVENP|ODDP))==ODDP )
30413Sbill 				continue;
3052924Swnj 		if ((c & DH_DO) && overrun == 0) {
30624840Seric 			log(LOG_WARNING, "dh%d: silo overflow\n", dh);
3072924Swnj 			overrun = 1;
3082924Swnj 		}
3092468Swnj 		if (c & DH_FE)
3102468Swnj 			/*
3112468Swnj 			 * At framing error (break) generate
3122468Swnj 			 * a null (in raw mode, for getty), or a
3132468Swnj 			 * interrupt (in cooked/cbreak mode).
3142468Swnj 			 */
31513Sbill 			if (tp->t_flags&RAW)
3162468Swnj 				c = 0;
31713Sbill 			else
3189549Ssam 				c = tp->t_intrc;
3192730Swnj #if NBK > 0
320139Sbill 		if (tp->t_line == NETLDISC) {
321117Sbill 			c &= 0177;
322168Sbill 			BKINPUT(c, tp);
323117Sbill 		} else
3242730Swnj #endif
3252468Swnj 			(*linesw[tp->t_line].l_rint)(c, tp);
32613Sbill 	}
32713Sbill }
32813Sbill 
32913Sbill /*
3302468Swnj  * Ioctl for DH11.
33113Sbill  */
33213Sbill /*ARGSUSED*/
3337629Ssam dhioctl(dev, cmd, data, flag)
3347629Ssam 	caddr_t data;
33513Sbill {
33613Sbill 	register struct tty *tp;
3378566Sroot 	register int unit = minor(dev);
3388566Sroot 	int error;
33913Sbill 
3402395Swnj 	tp = &dh11[unit];
3418566Sroot 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
3428566Sroot 	if (error >= 0)
3438566Sroot 		return (error);
3448566Sroot 	error = ttioctl(tp, cmd, data, flag);
3458566Sroot 	if (error >= 0) {
34617561Sbloom 		if (cmd == TIOCSETP || cmd == TIOCSETN || cmd == TIOCLBIS ||
34717561Sbloom 		    cmd == TIOCLBIC || cmd == TIOCLSET)
3482395Swnj 			dhparam(unit);
3498566Sroot 		return (error);
3508566Sroot 	}
3518566Sroot 	switch (cmd) {
3527629Ssam 
353168Sbill 	case TIOCSBRK:
3542479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak |= 1<<(unit&017);
355168Sbill 		break;
3567629Ssam 
357168Sbill 	case TIOCCBRK:
3582479Swnj 		((struct dhdevice *)(tp->t_addr))->dhbreak &= ~(1<<(unit&017));
359168Sbill 		break;
3607629Ssam 
361168Sbill 	case TIOCSDTR:
3622479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIS);
363168Sbill 		break;
3647629Ssam 
365168Sbill 	case TIOCCDTR:
3662479Swnj 		dmctl(unit, DML_DTR|DML_RTS, DMBIC);
367168Sbill 		break;
3687629Ssam 
369168Sbill 	default:
3708566Sroot 		return (ENOTTY);
371168Sbill 	}
3728566Sroot 	return (0);
37313Sbill }
37413Sbill 
37513Sbill /*
37613Sbill  * Set parameters from open or stty into the DH hardware
37713Sbill  * registers.
37813Sbill  */
3792395Swnj dhparam(unit)
3802395Swnj 	register int unit;
38113Sbill {
38213Sbill 	register struct tty *tp;
3832479Swnj 	register struct dhdevice *addr;
3842395Swnj 	register int lpar;
385300Sbill 	int s;
38613Sbill 
3872395Swnj 	tp = &dh11[unit];
3882479Swnj 	addr = (struct dhdevice *)tp->t_addr;
3892468Swnj 	/*
3902468Swnj 	 * Block interrupts so parameters will be set
3912468Swnj 	 * before the line interrupts.
3922468Swnj 	 */
393300Sbill 	s = spl5();
3942468Swnj 	addr->un.dhcsrl = (unit&0xf) | DH_IE;
39513Sbill 	if ((tp->t_ispeed)==0) {
3965406Swnj 		tp->t_state |= TS_HUPCLS;
3972479Swnj 		dmctl(unit, DML_OFF, DMSET);
39813Sbill 		return;
39913Sbill 	}
4002395Swnj 	lpar = ((tp->t_ospeed)<<10) | ((tp->t_ispeed)<<6);
4012468Swnj 	if ((tp->t_ispeed) == B134)
4022395Swnj 		lpar |= BITS6|PENABLE|HDUPLX;
40324269Slepreau 	else if (tp->t_flags & (RAW|LITOUT|PASS8))
4042395Swnj 		lpar |= BITS8;
40513Sbill 	else
4062395Swnj 		lpar |= BITS7|PENABLE;
40713Sbill 	if ((tp->t_flags&EVENP) == 0)
4082395Swnj 		lpar |= OPAR;
4092468Swnj 	if ((tp->t_ospeed) == B110)
4102395Swnj 		lpar |= TWOSB;
4112395Swnj 	addr->dhlpr = lpar;
412300Sbill 	splx(s);
41313Sbill }
41413Sbill 
41513Sbill /*
41613Sbill  * DH11 transmitter interrupt.
41713Sbill  * Restart each line which used to be active but has
41813Sbill  * terminated transmission since the last interrupt.
41913Sbill  */
4202395Swnj dhxint(dh)
4212395Swnj 	int dh;
42213Sbill {
42313Sbill 	register struct tty *tp;
4242479Swnj 	register struct dhdevice *addr;
42513Sbill 	short ttybit, bar, *sbar;
4262974Swnj 	register struct uba_device *ui;
4272468Swnj 	register int unit;
4282605Swnj 	u_short cntr;
42913Sbill 
4302395Swnj 	ui = dhinfo[dh];
4312479Swnj 	addr = (struct dhdevice *)ui->ui_addr;
4322456Swnj 	if (addr->un.dhcsr & DH_NXM) {
4332456Swnj 		addr->un.dhcsr |= DH_CNI;
4342924Swnj 		printf("dh%d: NXM\n", dh);
435105Sbill 	}
4362395Swnj 	sbar = &dhsar[dh];
43713Sbill 	bar = *sbar & ~addr->dhbar;
4382395Swnj 	unit = dh * 16; ttybit = 1;
4392468Swnj 	addr->un.dhcsr &= (short)~DH_TI;
4402468Swnj 	for (; bar; unit++, ttybit <<= 1) {
4412468Swnj 		if (bar & ttybit) {
44213Sbill 			*sbar &= ~ttybit;
44313Sbill 			bar &= ~ttybit;
4442395Swnj 			tp = &dh11[unit];
4455406Swnj 			tp->t_state &= ~TS_BUSY;
4465406Swnj 			if (tp->t_state&TS_FLUSH)
4475406Swnj 				tp->t_state &= ~TS_FLUSH;
448113Sbill 			else {
4492456Swnj 				addr->un.dhcsrl = (unit&017)|DH_IE;
4502468Swnj 				/*
4512468Swnj 				 * Do arithmetic in a short to make up
4522468Swnj 				 * for lost 16&17 bits.
4532468Swnj 				 */
4542605Swnj 				cntr = addr->dhcar -
4552468Swnj 				    UBACVT(tp->t_outq.c_cf, ui->ui_ubanum);
4563101Swnj 				ndflush(&tp->t_outq, (int)cntr);
457113Sbill 			}
458113Sbill 			if (tp->t_line)
45913Sbill 				(*linesw[tp->t_line].l_start)(tp);
460113Sbill 			else
46113Sbill 				dhstart(tp);
46213Sbill 		}
46313Sbill 	}
46413Sbill }
46513Sbill 
46613Sbill /*
46713Sbill  * Start (restart) transmission on the given DH11 line.
46813Sbill  */
46913Sbill dhstart(tp)
4702395Swnj 	register struct tty *tp;
47113Sbill {
4722479Swnj 	register struct dhdevice *addr;
4732468Swnj 	register int car, dh, unit, nch;
4742395Swnj 	int s;
47513Sbill 
4762468Swnj 	unit = minor(tp->t_dev);
4772468Swnj 	dh = unit >> 4;
4782468Swnj 	unit &= 0xf;
4792479Swnj 	addr = (struct dhdevice *)tp->t_addr;
4802468Swnj 
48113Sbill 	/*
4822468Swnj 	 * Must hold interrupts in following code to prevent
4832468Swnj 	 * state of the tp from changing.
48413Sbill 	 */
48513Sbill 	s = spl5();
4862468Swnj 	/*
4872468Swnj 	 * If it's currently active, or delaying, no need to do anything.
4882468Swnj 	 */
4895406Swnj 	if (tp->t_state&(TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
49013Sbill 		goto out;
4912468Swnj 	/*
4922468Swnj 	 * If there are sleepers, and output has drained below low
4932468Swnj 	 * water mark, wake up the sleepers.
4942468Swnj 	 */
4955406Swnj 	if (tp->t_outq.c_cc<=TTLOWAT(tp)) {
4965406Swnj 		if (tp->t_state&TS_ASLEEP) {
4975406Swnj 			tp->t_state &= ~TS_ASLEEP;
4985406Swnj 			wakeup((caddr_t)&tp->t_outq);
4995406Swnj 		}
5005406Swnj 		if (tp->t_wsel) {
5015406Swnj 			selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
5025406Swnj 			tp->t_wsel = 0;
5035406Swnj 			tp->t_state &= ~TS_WCOLL;
5045406Swnj 		}
50513Sbill 	}
5062468Swnj 	/*
5072468Swnj 	 * Now restart transmission unless the output queue is
5082468Swnj 	 * empty.
5092468Swnj 	 */
51013Sbill 	if (tp->t_outq.c_cc == 0)
51113Sbill 		goto out;
5129549Ssam 	if (tp->t_flags & (RAW|LITOUT))
51313Sbill 		nch = ndqb(&tp->t_outq, 0);
5142395Swnj 	else {
51513Sbill 		nch = ndqb(&tp->t_outq, 0200);
5162468Swnj 		/*
5172468Swnj 		 * If first thing on queue is a delay process it.
5182468Swnj 		 */
51913Sbill 		if (nch == 0) {
52013Sbill 			nch = getc(&tp->t_outq);
5212468Swnj 			timeout(ttrstrt, (caddr_t)tp, (nch&0x7f)+6);
5225406Swnj 			tp->t_state |= TS_TIMEOUT;
52313Sbill 			goto out;
52413Sbill 		}
52513Sbill 	}
5262468Swnj 	/*
5272468Swnj 	 * If characters to transmit, restart transmission.
5282468Swnj 	 */
52913Sbill 	if (nch) {
5302468Swnj 		car = UBACVT(tp->t_outq.c_cf, dhinfo[dh]->ui_ubanum);
5312468Swnj 		addr->un.dhcsrl = unit|((car>>12)&0x30)|DH_IE;
5323586Sroot 		/*
5333586Sroot 		 * The following nonsense with short word
5343586Sroot 		 * is to make sure the dhbar |= word below
5353586Sroot 		 * is done with an interlocking bisw2 instruction.
5363586Sroot 		 */
5373586Sroot 		{ short word = 1 << unit;
5383586Sroot 		dhsar[dh] |= word;
5392468Swnj 		addr->dhcar = car;
54013Sbill 		addr->dhbcr = -nch;
5413586Sroot 		addr->dhbar |= word;
5423586Sroot 		}
5435406Swnj 		tp->t_state |= TS_BUSY;
54413Sbill 	}
5452395Swnj out:
54613Sbill 	splx(s);
54713Sbill }
54813Sbill 
54913Sbill /*
5502468Swnj  * Stop output on a line, e.g. for ^S/^Q or output flush.
55113Sbill  */
55213Sbill /*ARGSUSED*/
55313Sbill dhstop(tp, flag)
5542468Swnj 	register struct tty *tp;
55513Sbill {
5562479Swnj 	register struct dhdevice *addr;
5572395Swnj 	register int unit, s;
55813Sbill 
5592479Swnj 	addr = (struct dhdevice *)tp->t_addr;
5602468Swnj 	/*
5612468Swnj 	 * Block input/output interrupts while messing with state.
5622468Swnj 	 */
5632468Swnj 	s = spl5();
5645406Swnj 	if (tp->t_state & TS_BUSY) {
5652468Swnj 		/*
5662468Swnj 		 * Device is transmitting; stop output
5672468Swnj 		 * by selecting the line and setting the byte
5682468Swnj 		 * count to -1.  We will clean up later
5692468Swnj 		 * by examining the address where the dh stopped.
5702468Swnj 		 */
5712395Swnj 		unit = minor(tp->t_dev);
5722456Swnj 		addr->un.dhcsrl = (unit&017) | DH_IE;
5735406Swnj 		if ((tp->t_state&TS_TTSTOP)==0)
5745406Swnj 			tp->t_state |= TS_FLUSH;
575113Sbill 		addr->dhbcr = -1;
576113Sbill 	}
57713Sbill 	splx(s);
57813Sbill }
57913Sbill 
580168Sbill /*
581280Sbill  * Reset state of driver if UBA reset was necessary.
582280Sbill  * Reset the csrl and lpr registers on open lines, and
583280Sbill  * restart transmitters.
584280Sbill  */
5852395Swnj dhreset(uban)
5862468Swnj 	int uban;
587280Sbill {
5882395Swnj 	register int dh, unit;
589280Sbill 	register struct tty *tp;
5902974Swnj 	register struct uba_device *ui;
5912421Skre 	int i;
592280Sbill 
5932395Swnj 	dh = 0;
5942643Swnj 	for (dh = 0; dh < NDH; dh++) {
5952421Skre 		ui = dhinfo[dh];
5962421Skre 		if (ui == 0 || ui->ui_alive == 0 || ui->ui_ubanum != uban)
5972421Skre 			continue;
5982924Swnj 		printf(" dh%d", dh);
599*26219Skarels 		if (dh_ubinfo[uban]) {
60025433Skarels 			dh_ubinfo[uban] = uballoc(uban, (caddr_t)cfree,
60125433Skarels 			    nclist*sizeof (struct cblock), 0);
602*26219Skarels 			cbase[uban] = UBAI_ADDR(dh_ubinfo[uban]);
60325433Skarels 		}
6042479Swnj 		((struct dhdevice *)ui->ui_addr)->un.dhcsr |= DH_IE;
60516190Skarels 		((struct dhdevice *)ui->ui_addr)->dhsilo = 0;
6062421Skre 		unit = dh * 16;
6072421Skre 		for (i = 0; i < 16; i++) {
6082421Skre 			tp = &dh11[unit];
6095406Swnj 			if (tp->t_state & (TS_ISOPEN|TS_WOPEN)) {
6102421Skre 				dhparam(unit);
6112479Swnj 				dmctl(unit, DML_ON, DMSET);
6125406Swnj 				tp->t_state &= ~TS_BUSY;
6132421Skre 				dhstart(tp);
6142421Skre 			}
6152421Skre 			unit++;
616300Sbill 		}
617300Sbill 	}
61816190Skarels 	dhsilos = 0;
619280Sbill }
6202395Swnj 
62116190Skarels int dhtransitions, dhslowtimers, dhfasttimers;		/*DEBUG*/
6222468Swnj /*
62316190Skarels  * At software clock interrupt time, check status.
62416190Skarels  * Empty all the dh silos that are in use, and decide whether
62516190Skarels  * to turn any silos off or on.
6262468Swnj  */
6272456Swnj dhtimer()
6282456Swnj {
62916190Skarels 	register int dh, s;
63016190Skarels 	static int timercalls;
6312456Swnj 
63216190Skarels 	if (dhsilos) {
63316190Skarels 		dhfasttimers++;		/*DEBUG*/
63416190Skarels 		timercalls++;
63516190Skarels 		s = spl5();
63616190Skarels 		for (dh = 0; dh < NDH; dh++)
63716190Skarels 			if (dhsilos & (1 << dh))
63816190Skarels 				dhrint(dh);
63916190Skarels 		splx(s);
64016190Skarels 	}
64116190Skarels 	if ((dhsilos == 0) || ((timercalls += FASTTIMER) >= hz)) {
64216190Skarels 		dhslowtimers++;		/*DEBUG*/
64316190Skarels 		timercalls = 0;
64416190Skarels 		for (dh = 0; dh < NDH; dh++) {
64516190Skarels 		    ave(dhrate[dh], dhchars[dh], 8);
64616190Skarels 		    if ((dhchars[dh] > dhhighrate) &&
64716190Skarels 		      ((dhsilos & (1 << dh)) == 0)) {
64816190Skarels 			((struct dhdevice *)(dhinfo[dh]->ui_addr))->dhsilo =
64916190Skarels 			    (dhchars[dh] > 500? 32 : 16);
65016190Skarels 			dhsilos |= (1 << dh);
65116190Skarels 			dhtransitions++;		/*DEBUG*/
65216190Skarels 		    } else if ((dhsilos & (1 << dh)) &&
65316190Skarels 		      (dhrate[dh] < dhlowrate)) {
65416190Skarels 			((struct dhdevice *)(dhinfo[dh]->ui_addr))->dhsilo = 0;
65516190Skarels 			dhsilos &= ~(1 << dh);
65616190Skarels 		    }
65716190Skarels 		    dhchars[dh] = 0;
65816190Skarels 		}
65916190Skarels 	}
66016190Skarels 	timeout(dhtimer, (caddr_t) 0, dhsilos? FASTTIMER: hz);
6612456Swnj }
6622456Swnj 
6632468Swnj /*
6642479Swnj  * Turn on the line associated with dh dev.
6652468Swnj  */
6662468Swnj dmopen(dev)
6672468Swnj 	dev_t dev;
6682468Swnj {
6692468Swnj 	register struct tty *tp;
6702468Swnj 	register struct dmdevice *addr;
6712974Swnj 	register struct uba_device *ui;
6722468Swnj 	register int unit;
6732468Swnj 	register int dm;
6743792Swnj 	int s;
6752468Swnj 
6762468Swnj 	unit = minor(dev);
6772479Swnj 	dm = unit >> 4;
6782468Swnj 	tp = &dh11[unit];
6792566Swnj 	unit &= 0xf;
68016942Skarels 	if (dm >= NDH || (ui = dminfo[dm]) == 0 || ui->ui_alive == 0) {
6815406Swnj 		tp->t_state |= TS_CARR_ON;
6822468Swnj 		return;
6832468Swnj 	}
6842468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
6853792Swnj 	s = spl5();
6862479Swnj 	addr->dmcsr &= ~DM_SE;
6872479Swnj 	while (addr->dmcsr & DM_BUSY)
6882468Swnj 		;
6892566Swnj 	addr->dmcsr = unit;
6902479Swnj 	addr->dmlstat = DML_ON;
69116942Skarels 	if ((addr->dmlstat&DML_CAR) || (dhsoftCAR[dm]&(1<<unit)))
6925406Swnj 		tp->t_state |= TS_CARR_ON;
6933792Swnj 	addr->dmcsr = DM_IE|DM_SE;
6945406Swnj 	while ((tp->t_state&TS_CARR_ON)==0)
6952468Swnj 		sleep((caddr_t)&tp->t_rawq, TTIPRI);
6963792Swnj 	splx(s);
6972468Swnj }
6982468Swnj 
6992468Swnj /*
7002468Swnj  * Dump control bits into the DM registers.
7012468Swnj  */
7022468Swnj dmctl(dev, bits, how)
7032468Swnj 	dev_t dev;
7042468Swnj 	int bits, how;
7052468Swnj {
7062974Swnj 	register struct uba_device *ui;
7072468Swnj 	register struct dmdevice *addr;
7082468Swnj 	register int unit, s;
7092468Swnj 	int dm;
7102468Swnj 
7112468Swnj 	unit = minor(dev);
7122468Swnj 	dm = unit >> 4;
7132468Swnj 	if ((ui = dminfo[dm]) == 0 || ui->ui_alive == 0)
7142468Swnj 		return;
7152468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7162468Swnj 	s = spl5();
7172479Swnj 	addr->dmcsr &= ~DM_SE;
7182479Swnj 	while (addr->dmcsr & DM_BUSY)
7192468Swnj 		;
7202468Swnj 	addr->dmcsr = unit & 0xf;
7212468Swnj 	switch(how) {
7222468Swnj 	case DMSET:
7232468Swnj 		addr->dmlstat = bits;
7242468Swnj 		break;
7252468Swnj 	case DMBIS:
7262468Swnj 		addr->dmlstat |= bits;
7272468Swnj 		break;
7282468Swnj 	case DMBIC:
7292468Swnj 		addr->dmlstat &= ~bits;
7302468Swnj 		break;
7312468Swnj 	}
7323792Swnj 	addr->dmcsr = DM_IE|DM_SE;
7332468Swnj 	splx(s);
7342468Swnj }
7352468Swnj 
7362468Swnj /*
7372468Swnj  * DM11 interrupt; deal with carrier transitions.
7382468Swnj  */
7392468Swnj dmintr(dm)
7402468Swnj 	register int dm;
7412468Swnj {
7422974Swnj 	register struct uba_device *ui;
7432468Swnj 	register struct tty *tp;
7442468Swnj 	register struct dmdevice *addr;
74516942Skarels 	int unit;
7462468Swnj 
7472468Swnj 	ui = dminfo[dm];
7482479Swnj 	if (ui == 0)
7492479Swnj 		return;
7502468Swnj 	addr = (struct dmdevice *)ui->ui_addr;
7513997Sroot 	if (addr->dmcsr&DM_DONE) {
7523997Sroot 		if (addr->dmcsr&DM_CF) {
75316942Skarels 			unit = addr->dmcsr & 0xf;
75416942Skarels 			tp = &dh11[(dm << 4) + unit];
75525394Skarels 			if (addr->dmlstat & DML_CAR)
75625394Skarels 				(void)(*linesw[tp->t_line].l_modem)(tp, 1);
75725394Skarels 			else if ((dhsoftCAR[dm] & (1<<unit)) == 0 &&
75825394Skarels 			    (*linesw[tp->t_line].l_modem)(tp, 0) == 0)
75925394Skarels 				addr->dmlstat = 0;
7603997Sroot 		}
7613997Sroot 		addr->dmcsr = DM_IE|DM_SE;
7622468Swnj 	}
7632468Swnj }
7642625Swnj #endif
765