xref: /csrg-svn/sys/pmax/dev/dc.c (revision 57234)
156819Sralph /*-
256819Sralph  * Copyright (c) 1992 The Regents of the University of California.
352130Smckusick  * All rights reserved.
452130Smckusick  *
552130Smckusick  * This code is derived from software contributed to Berkeley by
656819Sralph  * Ralph Campbell and Rick Macklem.
756819Sralph  *
856819Sralph  * %sccs.include.redist.c%
956819Sralph  *
10*57234Sralph  *	@(#)dc.c	7.12 (Berkeley) 12/20/92
1152693Sralph  */
1252693Sralph 
1352693Sralph /*
1452130Smckusick  * devDC7085.c --
1552130Smckusick  *
1652130Smckusick  *     	This file contains machine-dependent routines that handle the
1752130Smckusick  *	output queue for the serial lines.
1852130Smckusick  *
1952130Smckusick  *	Copyright (C) 1989 Digital Equipment Corporation.
2052130Smckusick  *	Permission to use, copy, modify, and distribute this software and
2152130Smckusick  *	its documentation for any purpose and without fee is hereby granted,
2252130Smckusick  *	provided that the above copyright notice appears in all copies.
2352130Smckusick  *	Digital Equipment Corporation makes no representations about the
2452130Smckusick  *	suitability of this software for any purpose.  It is provided "as is"
2552130Smckusick  *	without express or implied warranty.
2652130Smckusick  *
2752130Smckusick  * from: $Header: /sprite/src/kernel/dev/ds3100.md/RCS/devDC7085.c,
2852130Smckusick  *	v 1.4 89/08/29 11:55:30 nelson Exp $ SPRITE (DECWRL)";
2952130Smckusick  */
3052130Smckusick 
3156819Sralph #include <dc.h>
3252130Smckusick #if NDC > 0
3352130Smckusick /*
3452130Smckusick  * DC7085 (DZ-11 look alike) Driver
3552130Smckusick  */
3656522Sbostic #include <sys/param.h>
3756522Sbostic #include <sys/systm.h>
3856522Sbostic #include <sys/ioctl.h>
3956522Sbostic #include <sys/tty.h>
4056522Sbostic #include <sys/proc.h>
4156522Sbostic #include <sys/map.h>
4256522Sbostic #include <sys/buf.h>
4356522Sbostic #include <sys/conf.h>
4456522Sbostic #include <sys/file.h>
4556522Sbostic #include <sys/uio.h>
4656522Sbostic #include <sys/kernel.h>
4756522Sbostic #include <sys/syslog.h>
4852130Smckusick 
4956522Sbostic #include <machine/dc7085cons.h>
5056819Sralph #include <machine/pmioctl.h>
5152130Smckusick 
5256819Sralph #include <pmax/pmax/pmaxtype.h>
5356819Sralph #include <pmax/pmax/cons.h>
5456819Sralph 
5556525Sbostic #include <pmax/dev/device.h>
5656525Sbostic #include <pmax/dev/pdma.h>
5756819Sralph #include <pmax/dev/fbreg.h>
5852130Smckusick 
5956819Sralph extern int pmax_boardtype;
6056819Sralph extern struct consdev cn_tab;
6156819Sralph 
6252130Smckusick /*
6352130Smckusick  * Driver information for auto-configuration stuff.
6452130Smckusick  */
6552130Smckusick int	dcprobe();
6652693Sralph void	dcintr();
6752130Smckusick struct	driver dcdriver = {
6852693Sralph 	"dc", dcprobe, 0, 0, dcintr,
6952130Smckusick };
7052130Smckusick 
7152130Smckusick #define	NDCLINE 	(NDC*4)
7252130Smckusick 
7356819Sralph void dcstart	__P((struct tty *));
7456819Sralph void dcxint	__P((struct tty *));
7556819Sralph void dcPutc	__P((dev_t, int));
7656819Sralph void dcscan	__P((void *));
7756226Sralph extern void ttrstrt __P((void *));
7856819Sralph int dcGetc	__P((dev_t));
7956819Sralph int dcparam	__P((struct tty *, struct termios *));
8056819Sralph extern void KBDReset	__P((dev_t, void (*)()));
8156819Sralph extern void MouseInit	__P((dev_t, void (*)(), int (*)()));
8252130Smckusick 
8352130Smckusick struct	tty dc_tty[NDCLINE];
8452130Smckusick int	dc_cnt = NDCLINE;
8552863Sralph void	(*dcDivertXInput)();	/* X windows keyboard input routine */
8652863Sralph void	(*dcMouseEvent)();	/* X windows mouse motion event routine */
8752863Sralph void	(*dcMouseButtons)();	/* X windows mouse buttons event routine */
8852130Smckusick #ifdef DEBUG
8952130Smckusick int	debugChar;
9052130Smckusick #endif
9152130Smckusick 
9252130Smckusick /*
9352130Smckusick  * Software copy of brk register since it isn't readable
9452130Smckusick  */
9552130Smckusick int	dc_brk[NDC];
9652130Smckusick char	dcsoftCAR[NDC];		/* mask of dc's with carrier on (DSR) */
9752130Smckusick 
9852130Smckusick /*
9952130Smckusick  * The DC7085 doesn't interrupt on carrier transitions, so
10052130Smckusick  * we have to use a timer to watch it.
10152130Smckusick  */
10252130Smckusick int	dc_timer;		/* true if timer started */
10352130Smckusick 
10452130Smckusick /*
10552130Smckusick  * Pdma structures for fast output code
10652130Smckusick  */
10752130Smckusick struct	pdma dcpdma[NDCLINE];
10852130Smckusick 
10952130Smckusick struct speedtab dcspeedtab[] = {
11052130Smckusick 	0,	0,
11152130Smckusick 	50,	LPR_B50,
11252130Smckusick 	75,	LPR_B75,
11352130Smckusick 	110,	LPR_B110,
11452130Smckusick 	134,	LPR_B134,
11552130Smckusick 	150,	LPR_B150,
11652130Smckusick 	300,	LPR_B300,
11752130Smckusick 	600,	LPR_B600,
11852130Smckusick 	1200,	LPR_B1200,
11952130Smckusick 	1800,	LPR_B1800,
12052130Smckusick 	2400,	LPR_B2400,
12152130Smckusick 	4800,	LPR_B4800,
12252130Smckusick 	9600,	LPR_B9600,
12352693Sralph 	19200,	LPR_B19200,
12452130Smckusick 	-1,	-1
12552130Smckusick };
12652130Smckusick 
12752130Smckusick #ifndef	PORTSELECTOR
12852130Smckusick #define	ISPEED	TTYDEF_SPEED
12952130Smckusick #define	LFLAG	TTYDEF_LFLAG
13052130Smckusick #else
13152130Smckusick #define	ISPEED	B4800
13252130Smckusick #define	LFLAG	(TTYDEF_LFLAG & ~ECHO)
13352130Smckusick #endif
13452130Smckusick 
13552130Smckusick /*
13652130Smckusick  * Test to see if device is present.
13752130Smckusick  * Return true if found and initialized ok.
13852130Smckusick  */
13952130Smckusick dcprobe(cp)
14052130Smckusick 	register struct pmax_ctlr *cp;
14152130Smckusick {
14252130Smckusick 	register dcregs *dcaddr;
14352130Smckusick 	register struct pdma *pdp;
14452130Smckusick 	register struct tty *tp;
14552130Smckusick 	register int cntr;
14656819Sralph 	int s;
14752130Smckusick 
14852130Smckusick 	if (cp->pmax_unit >= NDC)
14952130Smckusick 		return (0);
15052130Smckusick 	if (badaddr(cp->pmax_addr, 2))
15152130Smckusick 		return (0);
15252130Smckusick 
153*57234Sralph 	/*
154*57234Sralph 	 * For a remote console, wait a while for previous output to
155*57234Sralph 	 * complete.
156*57234Sralph 	 */
157*57234Sralph 	if (major(cn_tab.cn_dev) == DCDEV && cp->pmax_unit == 0 &&
158*57234Sralph 		cn_tab.cn_screen == 0)
159*57234Sralph 		DELAY(10000);
160*57234Sralph 
16152130Smckusick 	/* reset chip */
16252130Smckusick 	dcaddr = (dcregs *)cp->pmax_addr;
16352130Smckusick 	dcaddr->dc_csr = CSR_CLR;
16452130Smckusick 	MachEmptyWriteBuffer();
16552130Smckusick 	while (dcaddr->dc_csr & CSR_CLR)
16652130Smckusick 		;
16752130Smckusick 	dcaddr->dc_csr = CSR_MSE | CSR_TIE | CSR_RIE;
16852130Smckusick 
16952130Smckusick 	/* init pseudo DMA structures */
17052130Smckusick 	pdp = &dcpdma[cp->pmax_unit * 4];
17152130Smckusick 	tp = &dc_tty[cp->pmax_unit * 4];
17252130Smckusick 	for (cntr = 0; cntr < 4; cntr++) {
17356819Sralph 		pdp->p_addr = (void *)dcaddr;
17452130Smckusick 		pdp->p_arg = (int)tp;
17552130Smckusick 		pdp->p_fcn = dcxint;
17652130Smckusick 		tp->t_addr = (caddr_t)pdp;
17752130Smckusick 		pdp++, tp++;
17852130Smckusick 	}
17952130Smckusick 	dcsoftCAR[cp->pmax_unit] = cp->pmax_flags | 0xB;
18052130Smckusick 
18152130Smckusick 	if (dc_timer == 0) {
18252130Smckusick 		dc_timer = 1;
18355744Sralph 		timeout(dcscan, (void *)0, hz);
18452130Smckusick 	}
18556819Sralph 
18656819Sralph 	/*
18756819Sralph 	 * Special handling for consoles.
18856819Sralph 	 */
18952130Smckusick 	if (cp->pmax_unit == 0) {
19056819Sralph 		if (cn_tab.cn_screen) {
19156819Sralph 			s = spltty();
19256819Sralph 			dcaddr->dc_lpr = LPR_RXENAB | LPR_8_BIT_CHAR |
19356819Sralph 				LPR_B4800 | DCKBD_PORT;
19456819Sralph 			dcaddr->dc_lpr = LPR_RXENAB | LPR_B4800 | LPR_OPAR |
19556819Sralph 				LPR_PARENB | LPR_8_BIT_CHAR | DCMOUSE_PORT;
19656819Sralph 			MachEmptyWriteBuffer();
197*57234Sralph 			DELAY(1000);
19856819Sralph 			KBDReset(makedev(DCDEV, DCKBD_PORT), dcPutc);
19956819Sralph 			MouseInit(makedev(DCDEV, DCMOUSE_PORT), dcPutc, dcGetc);
20056819Sralph 			splx(s);
20156819Sralph 		} else if (major(cn_tab.cn_dev) == DCDEV) {
20256819Sralph 			s = spltty();
20356819Sralph 			dcaddr->dc_lpr = LPR_RXENAB | LPR_8_BIT_CHAR |
20456819Sralph 				LPR_B9600 | minor(cn_tab.cn_dev);
20556819Sralph 			MachEmptyWriteBuffer();
206*57234Sralph 			DELAY(1000);
20756819Sralph 			cn_tab.cn_disabled = 0;
20856819Sralph 			splx(s);
20956819Sralph 		}
21052130Smckusick 	}
211*57234Sralph 	printf("dc%d at nexus0 csr 0x%x priority %d\n",
212*57234Sralph 		cp->pmax_unit, cp->pmax_addr, cp->pmax_pri);
21352130Smckusick 	return (1);
21452130Smckusick }
21552130Smckusick 
21654146Sralph dcopen(dev, flag, mode, p)
21752130Smckusick 	dev_t dev;
21854146Sralph 	int flag, mode;
21954146Sralph 	struct proc *p;
22052130Smckusick {
22152130Smckusick 	register struct tty *tp;
22252130Smckusick 	register int unit;
22352130Smckusick 	int s, error = 0;
22452130Smckusick 
22552130Smckusick 	unit = minor(dev);
22656819Sralph 	if (unit >= dc_cnt || dcpdma[unit].p_addr == (void *)0)
22752130Smckusick 		return (ENXIO);
22852130Smckusick 	tp = &dc_tty[unit];
22952130Smckusick 	tp->t_addr = (caddr_t)&dcpdma[unit];
23052130Smckusick 	tp->t_oproc = dcstart;
23152130Smckusick 	tp->t_param = dcparam;
23252130Smckusick 	tp->t_dev = dev;
23352130Smckusick 	if ((tp->t_state & TS_ISOPEN) == 0) {
23452130Smckusick 		tp->t_state |= TS_WOPEN;
23552130Smckusick 		ttychars(tp);
23652130Smckusick #ifndef PORTSELECTOR
23752130Smckusick 		if (tp->t_ispeed == 0) {
23852130Smckusick #endif
23952130Smckusick 			tp->t_iflag = TTYDEF_IFLAG;
24052130Smckusick 			tp->t_oflag = TTYDEF_OFLAG;
24152130Smckusick 			tp->t_cflag = TTYDEF_CFLAG;
24252130Smckusick 			tp->t_lflag = LFLAG;
24352130Smckusick 			tp->t_ispeed = tp->t_ospeed = ISPEED;
24452130Smckusick #ifdef PORTSELECTOR
24552130Smckusick 			tp->t_cflag |= HUPCL;
24652130Smckusick #else
24752130Smckusick 		}
24852130Smckusick #endif
24952130Smckusick 		(void) dcparam(tp, &tp->t_termios);
25052130Smckusick 		ttsetwater(tp);
25152130Smckusick 	} else if ((tp->t_state & TS_XCLUDE) && curproc->p_ucred->cr_uid != 0)
25252130Smckusick 		return (EBUSY);
25352130Smckusick 	(void) dcmctl(dev, DML_DTR, DMSET);
25452130Smckusick 	s = spltty();
25552130Smckusick 	while (!(flag & O_NONBLOCK) && !(tp->t_cflag & CLOCAL) &&
25652130Smckusick 	       !(tp->t_state & TS_CARR_ON)) {
25752130Smckusick 		tp->t_state |= TS_WOPEN;
25852130Smckusick 		if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
25952130Smckusick 		    ttopen, 0))
26052130Smckusick 			break;
26152130Smckusick 	}
26252130Smckusick 	splx(s);
26352130Smckusick 	if (error)
26452130Smckusick 		return (error);
26552130Smckusick 	return ((*linesw[tp->t_line].l_open)(dev, tp));
26652130Smckusick }
26752130Smckusick 
26852130Smckusick /*ARGSUSED*/
26954146Sralph dcclose(dev, flag, mode, p)
27052130Smckusick 	dev_t dev;
27154146Sralph 	int flag, mode;
27254146Sralph 	struct proc *p;
27352130Smckusick {
27452130Smckusick 	register struct tty *tp;
27552130Smckusick 	register int unit, bit;
27652130Smckusick 
27752130Smckusick 	unit = minor(dev);
27852130Smckusick 	tp = &dc_tty[unit];
27952130Smckusick 	bit = 1 << ((unit & 03) + 8);
28052130Smckusick 	if (dc_brk[unit >> 2] & bit) {
28152130Smckusick 		dc_brk[unit >> 2] &= ~bit;
28252130Smckusick 		ttyoutput(0, tp);
28352130Smckusick 	}
28454146Sralph 	(*linesw[tp->t_line].l_close)(tp, flag);
28552130Smckusick 	if ((tp->t_cflag & HUPCL) || (tp->t_state & TS_WOPEN) ||
28652130Smckusick 	    !(tp->t_state & TS_ISOPEN))
28752130Smckusick 		(void) dcmctl(dev, 0, DMSET);
28852130Smckusick 	return (ttyclose(tp));
28952130Smckusick }
29052130Smckusick 
29152130Smckusick dcread(dev, uio, flag)
29252130Smckusick 	dev_t dev;
29352130Smckusick 	struct uio *uio;
29452130Smckusick {
29552130Smckusick 	register struct tty *tp;
29652130Smckusick 
29752130Smckusick 	tp = &dc_tty[minor(dev)];
29852130Smckusick 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
29952130Smckusick }
30052130Smckusick 
30152130Smckusick dcwrite(dev, uio, flag)
30252130Smckusick 	dev_t dev;
30352130Smckusick 	struct uio *uio;
30452130Smckusick {
30552130Smckusick 	register struct tty *tp;
30652130Smckusick 
30752130Smckusick 	tp = &dc_tty[minor(dev)];
30852130Smckusick 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
30952130Smckusick }
31052130Smckusick 
31152130Smckusick /*ARGSUSED*/
31254146Sralph dcioctl(dev, cmd, data, flag, p)
31352130Smckusick 	dev_t dev;
31456819Sralph 	int cmd;
31552130Smckusick 	caddr_t data;
31654146Sralph 	int flag;
31754146Sralph 	struct proc *p;
31852130Smckusick {
31952130Smckusick 	register struct tty *tp;
32052130Smckusick 	register int unit = minor(dev);
32152130Smckusick 	register int dc = unit >> 2;
32252130Smckusick 	int error;
32352130Smckusick 
32452130Smckusick 	tp = &dc_tty[unit];
32554146Sralph 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
32652130Smckusick 	if (error >= 0)
32752130Smckusick 		return (error);
32852130Smckusick 	error = ttioctl(tp, cmd, data, flag);
32952130Smckusick 	if (error >= 0)
33052130Smckusick 		return (error);
33152130Smckusick 
33252130Smckusick 	switch (cmd) {
33352130Smckusick 
33452130Smckusick 	case TIOCSBRK:
33552130Smckusick 		dc_brk[dc] |= 1 << ((unit & 03) + 8);
33652130Smckusick 		ttyoutput(0, tp);
33752130Smckusick 		break;
33852130Smckusick 
33952130Smckusick 	case TIOCCBRK:
34052130Smckusick 		dc_brk[dc] &= ~(1 << ((unit & 03) + 8));
34152130Smckusick 		ttyoutput(0, tp);
34252130Smckusick 		break;
34352130Smckusick 
34452130Smckusick 	case TIOCSDTR:
34552130Smckusick 		(void) dcmctl(dev, DML_DTR|DML_RTS, DMBIS);
34652130Smckusick 		break;
34752130Smckusick 
34852130Smckusick 	case TIOCCDTR:
34952130Smckusick 		(void) dcmctl(dev, DML_DTR|DML_RTS, DMBIC);
35052130Smckusick 		break;
35152130Smckusick 
35252130Smckusick 	case TIOCMSET:
35352130Smckusick 		(void) dcmctl(dev, *(int *)data, DMSET);
35452130Smckusick 		break;
35552130Smckusick 
35652130Smckusick 	case TIOCMBIS:
35752130Smckusick 		(void) dcmctl(dev, *(int *)data, DMBIS);
35852130Smckusick 		break;
35952130Smckusick 
36052130Smckusick 	case TIOCMBIC:
36152130Smckusick 		(void) dcmctl(dev, *(int *)data, DMBIC);
36252130Smckusick 		break;
36352130Smckusick 
36452130Smckusick 	case TIOCMGET:
36552130Smckusick 		*(int *)data = dcmctl(dev, 0, DMGET);
36652130Smckusick 		break;
36752130Smckusick 
36852130Smckusick 	default:
36952130Smckusick 		return (ENOTTY);
37052130Smckusick 	}
37152130Smckusick 	return (0);
37252130Smckusick }
37352130Smckusick 
37452130Smckusick dcparam(tp, t)
37552130Smckusick 	register struct tty *tp;
37652130Smckusick 	register struct termios *t;
37752130Smckusick {
37852130Smckusick 	register dcregs *dcaddr;
37952130Smckusick 	register int lpr;
38052130Smckusick 	register int cflag = t->c_cflag;
38152130Smckusick 	int unit = minor(tp->t_dev);
38252130Smckusick 	int ospeed = ttspeedtab(t->c_ospeed, dcspeedtab);
38352130Smckusick 
38452130Smckusick 	/* check requested parameters */
38552130Smckusick         if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed) ||
38656819Sralph             (cflag & CSIZE) == CS5 || (cflag & CSIZE) == CS6 ||
38756819Sralph 	    (pmax_boardtype == DS_PMAX && t->c_ospeed == 19200))
38852130Smckusick                 return (EINVAL);
38952130Smckusick         /* and copy to tty */
39052130Smckusick         tp->t_ispeed = t->c_ispeed;
39152130Smckusick         tp->t_ospeed = t->c_ospeed;
39252130Smckusick         tp->t_cflag = cflag;
39352130Smckusick 
39456819Sralph 	dcaddr = (dcregs *)dcpdma[unit].p_addr;
39556819Sralph 
39656819Sralph 	/*
39756819Sralph 	 * Handle console cases specially.
39856819Sralph 	 */
39956819Sralph 	if (cn_tab.cn_screen) {
40056819Sralph 		if (unit == DCKBD_PORT) {
40156819Sralph 			dcaddr->dc_lpr = LPR_RXENAB | LPR_8_BIT_CHAR |
40256819Sralph 				LPR_B4800 | DCKBD_PORT;
40356819Sralph 			MachEmptyWriteBuffer();
40456819Sralph 			return (0);
40556819Sralph 		} else if (unit == DCMOUSE_PORT) {
40656819Sralph 			dcaddr->dc_lpr = LPR_RXENAB | LPR_B4800 | LPR_OPAR |
40756819Sralph 				LPR_PARENB | LPR_8_BIT_CHAR | DCMOUSE_PORT;
40856819Sralph 			MachEmptyWriteBuffer();
40956819Sralph 			return (0);
41056819Sralph 		}
41156819Sralph 	} else if (tp->t_dev == cn_tab.cn_dev) {
41256819Sralph 		dcaddr->dc_lpr = LPR_RXENAB | LPR_8_BIT_CHAR |
41356819Sralph 			LPR_B9600 | unit;
41452130Smckusick 		MachEmptyWriteBuffer();
41552130Smckusick 		return (0);
41652130Smckusick 	}
41752130Smckusick 	if (ospeed == 0) {
41852130Smckusick 		(void) dcmctl(unit, 0, DMSET);	/* hang up line */
41952130Smckusick 		return (0);
42052130Smckusick 	}
42152130Smckusick 	lpr = LPR_RXENAB | ospeed | (unit & 03);
42252130Smckusick 	if ((cflag & CSIZE) == CS7)
42352130Smckusick 		lpr |= LPR_7_BIT_CHAR;
42452130Smckusick 	else
42552130Smckusick 		lpr |= LPR_8_BIT_CHAR;
42652130Smckusick 	if (cflag & PARENB)
42752130Smckusick 		lpr |= LPR_PARENB;
42852130Smckusick 	if (cflag & PARODD)
42952130Smckusick 		lpr |= LPR_OPAR;
43052130Smckusick 	if (cflag & CSTOPB)
43152130Smckusick 		lpr |= LPR_2_STOP;
43252130Smckusick 	dcaddr->dc_lpr = lpr;
43352130Smckusick 	MachEmptyWriteBuffer();
43452130Smckusick 	return (0);
43552130Smckusick }
43652130Smckusick 
43752693Sralph /*
43852693Sralph  * Check for interrupts from all devices.
43952693Sralph  */
44052693Sralph void
44152693Sralph dcintr(unit)
44252693Sralph 	register int unit;
44352693Sralph {
44452693Sralph 	register dcregs *dcaddr;
44552693Sralph 	register unsigned csr;
44652693Sralph 
44752693Sralph 	unit <<= 2;
44856819Sralph 	dcaddr = (dcregs *)dcpdma[unit].p_addr;
44952693Sralph 	while ((csr = dcaddr->dc_csr) & (CSR_RDONE | CSR_TRDY)) {
45052693Sralph 		if (csr & CSR_RDONE)
45152693Sralph 			dcrint(unit);
45252693Sralph 		if (csr & CSR_TRDY)
45352693Sralph 			dcxint(&dc_tty[unit + ((csr >> 8) & 03)]);
45452693Sralph 	}
45552693Sralph }
45652693Sralph 
45752693Sralph dcrint(unit)
45852693Sralph 	register int unit;
45952693Sralph {
46052693Sralph 	register dcregs *dcaddr;
46152693Sralph 	register struct tty *tp;
46252693Sralph 	register int c, cc;
46352693Sralph 	register struct tty *tp0;
46452693Sralph 	int overrun = 0;
46552693Sralph 
46656819Sralph 	dcaddr = (dcregs *)dcpdma[unit].p_addr;
46752693Sralph 	tp0 = &dc_tty[unit];
46852693Sralph 	while ((c = dcaddr->dc_rbuf) < 0) {	/* char present */
46952693Sralph 		cc = c & 0xff;
47052693Sralph 		tp = tp0 + ((c >> 8) & 03);
47152693Sralph 		if ((c & RBUF_OERR) && overrun == 0) {
47252693Sralph 			log(LOG_WARNING, "dc%d,%d: silo overflow\n", unit >> 2,
47352693Sralph 				(c >> 8) & 03);
47452693Sralph 			overrun = 1;
47552693Sralph 		}
47652693Sralph 		/* the keyboard requires special translation */
47756819Sralph 		if (tp == &dc_tty[DCKBD_PORT] && cn_tab.cn_screen) {
47852693Sralph #ifdef KADB
47952693Sralph 			if (cc == LK_DO) {
48052693Sralph 				spl0();
48152693Sralph 				kdbpanic();
48252693Sralph 				return;
48352693Sralph 			}
48452693Sralph #endif
48552693Sralph #ifdef DEBUG
48652693Sralph 			debugChar = cc;
48752693Sralph #endif
48852693Sralph 			if (dcDivertXInput) {
48952863Sralph 				(*dcDivertXInput)(cc);
49052693Sralph 				return;
49152693Sralph 			}
49256819Sralph 			if ((cc = kbdMapChar(cc)) < 0)
49352693Sralph 				return;
49456819Sralph 		} else if (tp == &dc_tty[DCMOUSE_PORT] && dcMouseButtons) {
49552863Sralph 			register MouseReport *mrp;
49652693Sralph 			static MouseReport currentRep;
49752693Sralph 
49852863Sralph 			mrp = &currentRep;
49952863Sralph 			mrp->byteCount++;
50052693Sralph 			if (cc & MOUSE_START_FRAME) {
50152693Sralph 				/*
50252693Sralph 				 * The first mouse report byte (button state).
50352693Sralph 				 */
50452863Sralph 				mrp->state = cc;
50552863Sralph 				if (mrp->byteCount > 1)
50652863Sralph 					mrp->byteCount = 1;
50752863Sralph 			} else if (mrp->byteCount == 2) {
50852693Sralph 				/*
50952693Sralph 				 * The second mouse report byte (delta x).
51052693Sralph 				 */
51152863Sralph 				mrp->dx = cc;
51252863Sralph 			} else if (mrp->byteCount == 3) {
51352693Sralph 				/*
51452693Sralph 				 * The final mouse report byte (delta y).
51552693Sralph 				 */
51652863Sralph 				mrp->dy = cc;
51752863Sralph 				mrp->byteCount = 0;
51852863Sralph 				if (mrp->dx != 0 || mrp->dy != 0) {
51952693Sralph 					/*
52052693Sralph 					 * If the mouse moved,
52152693Sralph 					 * post a motion event.
52252693Sralph 					 */
52352863Sralph 					(*dcMouseEvent)(mrp);
52452693Sralph 				}
52552863Sralph 				(*dcMouseButtons)(mrp);
52652693Sralph 			}
52752693Sralph 			return;
52852693Sralph 		}
52952693Sralph 		if (!(tp->t_state & TS_ISOPEN)) {
53052693Sralph 			wakeup((caddr_t)&tp->t_rawq);
53152693Sralph #ifdef PORTSELECTOR
53252693Sralph 			if (!(tp->t_state & TS_WOPEN))
53352693Sralph #endif
53452693Sralph 				return;
53552693Sralph 		}
53652693Sralph 		if (c & RBUF_FERR)
53752693Sralph 			cc |= TTY_FE;
53852693Sralph 		if (c & RBUF_PERR)
53952693Sralph 			cc |= TTY_PE;
54052693Sralph 		(*linesw[tp->t_line].l_rint)(cc, tp);
54152693Sralph 	}
54252693Sralph 	DELAY(10);
54352693Sralph }
54452693Sralph 
54552755Sralph void
54652130Smckusick dcxint(tp)
54752130Smckusick 	register struct tty *tp;
54852130Smckusick {
54952130Smckusick 	register struct pdma *dp;
55052130Smckusick 	register dcregs *dcaddr;
55152130Smckusick 
55252130Smckusick 	dp = (struct pdma *)tp->t_addr;
55352130Smckusick 	if (dp->p_mem < dp->p_end) {
55456819Sralph 		dcaddr = (dcregs *)dp->p_addr;
55552130Smckusick 		dcaddr->dc_tdr = dc_brk[(tp - dc_tty) >> 2] | *dp->p_mem++;
55652130Smckusick 		MachEmptyWriteBuffer();
55752130Smckusick 		DELAY(10);
55852130Smckusick 		return;
55952130Smckusick 	}
56052130Smckusick 	tp->t_state &= ~TS_BUSY;
56152130Smckusick 	if (tp->t_state & TS_FLUSH)
56252130Smckusick 		tp->t_state &= ~TS_FLUSH;
56352130Smckusick 	else {
56452130Smckusick 		ndflush(&tp->t_outq, dp->p_mem-tp->t_outq.c_cf);
56552130Smckusick 		dp->p_end = dp->p_mem = tp->t_outq.c_cf;
56652130Smckusick 	}
56752130Smckusick 	if (tp->t_line)
56852130Smckusick 		(*linesw[tp->t_line].l_start)(tp);
56952130Smckusick 	else
57052130Smckusick 		dcstart(tp);
57152130Smckusick 	if (tp->t_outq.c_cc == 0 || !(tp->t_state & TS_BUSY)) {
57256819Sralph 		((dcregs *)dp->p_addr)->dc_tcr &= ~(1 << (minor(tp->t_dev) & 03));
57352130Smckusick 		MachEmptyWriteBuffer();
57452130Smckusick 		DELAY(10);
57552130Smckusick 	}
57652130Smckusick }
57752130Smckusick 
57852755Sralph void
57952130Smckusick dcstart(tp)
58052130Smckusick 	register struct tty *tp;
58152130Smckusick {
58252130Smckusick 	register struct pdma *dp;
58352130Smckusick 	register dcregs *dcaddr;
58452130Smckusick 	register int cc;
58552130Smckusick 	int s;
58652130Smckusick 
58752130Smckusick 	dp = (struct pdma *)tp->t_addr;
58856819Sralph 	dcaddr = (dcregs *)dp->p_addr;
58952130Smckusick 	s = spltty();
59052130Smckusick 	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
59152130Smckusick 		goto out;
59252130Smckusick 	if (tp->t_outq.c_cc <= tp->t_lowat) {
59352130Smckusick 		if (tp->t_state & TS_ASLEEP) {
59452130Smckusick 			tp->t_state &= ~TS_ASLEEP;
59552130Smckusick 			wakeup((caddr_t)&tp->t_outq);
59652130Smckusick 		}
59752674Smckusick 		selwakeup(&tp->t_wsel);
59852130Smckusick 	}
59952130Smckusick 	if (tp->t_outq.c_cc == 0)
60052130Smckusick 		goto out;
60152130Smckusick 	/* handle console specially */
60256819Sralph 	if (tp == &dc_tty[DCKBD_PORT] && cn_tab.cn_screen) {
60352130Smckusick 		while (tp->t_outq.c_cc > 0) {
60452130Smckusick 			cc = getc(&tp->t_outq) & 0x7f;
60552863Sralph 			cnputc(cc);
60652130Smckusick 		}
60752130Smckusick 		/*
60852130Smckusick 		 * After we flush the output queue we may need to wake
60952130Smckusick 		 * up the process that made the output.
61052130Smckusick 		 */
61152130Smckusick 		if (tp->t_outq.c_cc <= tp->t_lowat) {
61252130Smckusick 			if (tp->t_state & TS_ASLEEP) {
61352130Smckusick 				tp->t_state &= ~TS_ASLEEP;
61452130Smckusick 				wakeup((caddr_t)&tp->t_outq);
61552130Smckusick 			}
61652674Smckusick 			selwakeup(&tp->t_wsel);
61752130Smckusick 		}
61852130Smckusick 		goto out;
61952130Smckusick 	}
62052130Smckusick 	if (tp->t_flags & (RAW|LITOUT))
62152130Smckusick 		cc = ndqb(&tp->t_outq, 0);
62252130Smckusick 	else {
62352130Smckusick 		cc = ndqb(&tp->t_outq, 0200);
62452130Smckusick 		if (cc == 0) {
62552130Smckusick 			cc = getc(&tp->t_outq);
62655744Sralph 			timeout(ttrstrt, (void *)tp, (cc & 0x7f) + 6);
62752130Smckusick 			tp->t_state |= TS_TIMEOUT;
62852130Smckusick 			goto out;
62952130Smckusick 		}
63052130Smckusick 	}
63152130Smckusick 	tp->t_state |= TS_BUSY;
63252130Smckusick 	dp->p_end = dp->p_mem = tp->t_outq.c_cf;
63352130Smckusick 	dp->p_end += cc;
63452130Smckusick 	dcaddr->dc_tcr |= 1 << (minor(tp->t_dev) & 03);
63552130Smckusick 	MachEmptyWriteBuffer();
63652130Smckusick out:
63752130Smckusick 	splx(s);
63852130Smckusick }
63952130Smckusick 
64052130Smckusick /*
64152130Smckusick  * Stop output on a line.
64252130Smckusick  */
64352130Smckusick /*ARGSUSED*/
64452130Smckusick dcstop(tp, flag)
64552130Smckusick 	register struct tty *tp;
64652130Smckusick {
64752130Smckusick 	register struct pdma *dp;
64852130Smckusick 	register int s;
64952130Smckusick 
65052130Smckusick 	dp = (struct pdma *)tp->t_addr;
65152130Smckusick 	s = spltty();
65252130Smckusick 	if (tp->t_state & TS_BUSY) {
65352130Smckusick 		dp->p_end = dp->p_mem;
65452130Smckusick 		if (!(tp->t_state & TS_TTSTOP))
65552130Smckusick 			tp->t_state |= TS_FLUSH;
65652130Smckusick 	}
65752130Smckusick 	splx(s);
65852130Smckusick }
65952130Smckusick 
66052130Smckusick dcmctl(dev, bits, how)
66152130Smckusick 	dev_t dev;
66252130Smckusick 	int bits, how;
66352130Smckusick {
66452130Smckusick 	register dcregs *dcaddr;
66552130Smckusick 	register int unit, mbits;
66652130Smckusick 	int b, s;
66752693Sralph 	register int msr;
66852130Smckusick 
66952130Smckusick 	unit = minor(dev);
67052130Smckusick 	b = 1 << (unit & 03);
67156819Sralph 	dcaddr = (dcregs *)dcpdma[unit].p_addr;
67252130Smckusick 	s = spltty();
67352130Smckusick 	/* only channel 2 has modem control (what about line 3?) */
67456819Sralph 	mbits = DML_DTR | DML_DSR | DML_CAR;
67552693Sralph 	switch (unit & 03) {
67652693Sralph 	case 2:
67752130Smckusick 		mbits = 0;
67852130Smckusick 		if (dcaddr->dc_tcr & TCR_DTR2)
67952130Smckusick 			mbits |= DML_DTR;
68052693Sralph 		msr = dcaddr->dc_msr;
68152693Sralph 		if (msr & MSR_CD2)
68252693Sralph 			mbits |= DML_CAR;
68356819Sralph 		if (msr & MSR_DSR2) {
68456819Sralph 			if (pmax_boardtype == DS_PMAX)
68556819Sralph 				mbits |= DML_CAR | DML_DSR;
68656819Sralph 			else
68756819Sralph 				mbits |= DML_DSR;
68856819Sralph 		}
68952693Sralph 		break;
69052693Sralph 
69152693Sralph 	case 3:
69256819Sralph 		if (pmax_boardtype != DS_PMAX) {
69356819Sralph 			mbits = 0;
69456819Sralph 			if (dcaddr->dc_tcr & TCR_DTR3)
69556819Sralph 				mbits |= DML_DTR;
69656819Sralph 			msr = dcaddr->dc_msr;
69756819Sralph 			if (msr & MSR_CD3)
69856819Sralph 				mbits |= DML_CAR;
69956819Sralph 			if (msr & MSR_DSR3)
70056819Sralph 				mbits |= DML_DSR;
70156819Sralph 		}
70252693Sralph 	}
70352130Smckusick 	switch (how) {
70452130Smckusick 	case DMSET:
70552130Smckusick 		mbits = bits;
70652130Smckusick 		break;
70752130Smckusick 
70852130Smckusick 	case DMBIS:
70952130Smckusick 		mbits |= bits;
71052130Smckusick 		break;
71152130Smckusick 
71252130Smckusick 	case DMBIC:
71352130Smckusick 		mbits &= ~bits;
71452130Smckusick 		break;
71552130Smckusick 
71652130Smckusick 	case DMGET:
71752130Smckusick 		(void) splx(s);
71852130Smckusick 		return (mbits);
71952130Smckusick 	}
72052693Sralph 	switch (unit & 03) {
72152693Sralph 	case 2:
72252130Smckusick 		if (mbits & DML_DTR)
72352130Smckusick 			dcaddr->dc_tcr |= TCR_DTR2;
72452130Smckusick 		else
72552130Smckusick 			dcaddr->dc_tcr &= ~TCR_DTR2;
72652693Sralph 		break;
72752693Sralph 
72852693Sralph 	case 3:
72956819Sralph 		if (pmax_boardtype != DS_PMAX) {
73056819Sralph 			if (mbits & DML_DTR)
73156819Sralph 				dcaddr->dc_tcr |= TCR_DTR3;
73256819Sralph 			else
73356819Sralph 				dcaddr->dc_tcr &= ~TCR_DTR3;
73456819Sralph 		}
73552130Smckusick 	}
73652130Smckusick 	if ((mbits & DML_DTR) && (dcsoftCAR[unit >> 2] & b))
73752130Smckusick 		dc_tty[unit].t_state |= TS_CARR_ON;
73852130Smckusick 	(void) splx(s);
73952130Smckusick 	return (mbits);
74052130Smckusick }
74152130Smckusick 
74252130Smckusick /*
74352130Smckusick  * This is called by timeout() periodically.
74452130Smckusick  * Check to see if modem status bits have changed.
74552130Smckusick  */
74656819Sralph void
74755744Sralph dcscan(arg)
74855744Sralph 	void *arg;
74952130Smckusick {
75052130Smckusick 	register dcregs *dcaddr;
75152130Smckusick 	register struct tty *tp;
75252130Smckusick 	register int i, bit, car;
75352130Smckusick 	int s;
75452130Smckusick 
75552130Smckusick 	s = spltty();
75652130Smckusick 	/* only channel 2 has modem control (what about line 3?) */
75756819Sralph 	dcaddr = (dcregs *)dcpdma[i = 2].p_addr;
75852130Smckusick 	tp = &dc_tty[i];
75952130Smckusick 	bit = TCR_DTR2;
76052130Smckusick 	if (dcsoftCAR[i >> 2] & bit)
76152130Smckusick 		car = 1;
76252130Smckusick 	else
76352130Smckusick 		car = dcaddr->dc_msr & MSR_DSR2;
76452130Smckusick 	if (car) {
76552130Smckusick 		/* carrier present */
76652130Smckusick 		if (!(tp->t_state & TS_CARR_ON))
76752130Smckusick 			(void)(*linesw[tp->t_line].l_modem)(tp, 1);
76852130Smckusick 	} else if ((tp->t_state & TS_CARR_ON) &&
76952130Smckusick 	    (*linesw[tp->t_line].l_modem)(tp, 0) == 0)
77052130Smckusick 		dcaddr->dc_tcr &= ~bit;
77152130Smckusick 	splx(s);
77255744Sralph 	timeout(dcscan, (void *)0, hz);
77352130Smckusick }
77452130Smckusick 
77552130Smckusick /*
77652130Smckusick  * ----------------------------------------------------------------------------
77752130Smckusick  *
77856819Sralph  * dcGetc --
77952130Smckusick  *
78056819Sralph  *	Read a character from a serial line.
78152130Smckusick  *
78252130Smckusick  * Results:
78356819Sralph  *	A character read from the serial port.
78452130Smckusick  *
78552130Smckusick  * Side effects:
78652130Smckusick  *	None.
78752130Smckusick  *
78852130Smckusick  * ----------------------------------------------------------------------------
78952130Smckusick  */
79052130Smckusick int
79156819Sralph dcGetc(dev)
79256819Sralph 	dev_t dev;
79352130Smckusick {
79452130Smckusick 	register dcregs *dcaddr;
79552130Smckusick 	register int c;
79654146Sralph 	int s;
79752130Smckusick 
79856819Sralph 	dcaddr = (dcregs *)dcpdma[minor(dev)].p_addr;
79952130Smckusick 	if (!dcaddr)
80052130Smckusick 		return (0);
80154146Sralph 	s = spltty();
80252693Sralph 	for (;;) {
80352693Sralph 		if (!(dcaddr->dc_csr & CSR_RDONE))
80452693Sralph 			continue;
80552693Sralph 		c = dcaddr->dc_rbuf;
80652693Sralph 		DELAY(10);
80756819Sralph 		if (((c >> 8) & 03) == (minor(dev) & 03))
80852693Sralph 			break;
80952693Sralph 	}
81052693Sralph 	splx(s);
81156819Sralph 	return (c & 0xff);
81252693Sralph }
81352693Sralph 
81452693Sralph /*
81556819Sralph  * Send a char on a port, non interrupt driven.
81652693Sralph  */
81752130Smckusick void
81856819Sralph dcPutc(dev, c)
81956819Sralph 	dev_t dev;
82052130Smckusick 	int c;
82152130Smckusick {
82252130Smckusick 	register dcregs *dcaddr;
82352130Smckusick 	register u_short tcr;
82452130Smckusick 	register int timeout;
82556819Sralph 	int s, line;
82652130Smckusick 
82756819Sralph 	s = spltty();
82856819Sralph 
82956819Sralph 	dcaddr = (dcregs *)dcpdma[minor(dev)].p_addr;
83052130Smckusick 	tcr = dcaddr->dc_tcr;
83156819Sralph 	dcaddr->dc_tcr = tcr | (1 << minor(dev));
83252130Smckusick 	MachEmptyWriteBuffer();
83352130Smckusick 	DELAY(10);
83452130Smckusick 	while (1) {
83552130Smckusick 		/*
83652130Smckusick 		 * Wait for transmitter to be not busy.
83752130Smckusick 		 */
83852130Smckusick 		timeout = 1000000;
83952130Smckusick 		while (!(dcaddr->dc_csr & CSR_TRDY) && timeout > 0)
84052130Smckusick 			timeout--;
84152130Smckusick 		if (timeout == 0) {
84256819Sralph 			printf("dcPutc: timeout waiting for CSR_TRDY\n");
84352130Smckusick 			break;
84452130Smckusick 		}
84552130Smckusick 		line = (dcaddr->dc_csr >> 8) & 3;
84652130Smckusick 		/*
84752130Smckusick 		 * Check to be sure its the right port.
84852130Smckusick 		 */
84956819Sralph 		if (line != minor(dev)) {
85052130Smckusick 			tcr |= 1 << line;
85152130Smckusick 			dcaddr->dc_tcr &= ~(1 << line);
85252130Smckusick 			MachEmptyWriteBuffer();
85352130Smckusick 			DELAY(10);
85452130Smckusick 			continue;
85552130Smckusick 		}
85652130Smckusick 		/*
85752130Smckusick 		 * Start sending the character.
85852130Smckusick 		 */
85952130Smckusick 		dcaddr->dc_tdr = dc_brk[0] | (c & 0xff);
86052130Smckusick 		MachEmptyWriteBuffer();
86152130Smckusick 		DELAY(10);
86252130Smckusick 		/*
86352130Smckusick 		 * Wait for character to be sent.
86452130Smckusick 		 */
86552130Smckusick 		while (1) {
86652130Smckusick 			/*
86752130Smckusick 			 * cc -O bug: this code produces and infinite loop!
86852130Smckusick 			 * while (!(dcaddr->dc_csr & CSR_TRDY))
86952130Smckusick 			 *	;
87052130Smckusick 			 */
87152130Smckusick 			timeout = 1000000;
87252130Smckusick 			while (!(dcaddr->dc_csr & CSR_TRDY) && timeout > 0)
87352130Smckusick 				timeout--;
87452130Smckusick 			line = (dcaddr->dc_csr >> 8) & 3;
87556819Sralph 			if (line != minor(dev)) {
87652130Smckusick 				tcr |= 1 << line;
87752130Smckusick 				dcaddr->dc_tcr &= ~(1 << line);
87852130Smckusick 				MachEmptyWriteBuffer();
87952130Smckusick 				DELAY(10);
88052130Smckusick 				continue;
88152130Smckusick 			}
88256819Sralph 			dcaddr->dc_tcr &= ~(1 << minor(dev));
88352130Smckusick 			MachEmptyWriteBuffer();
88452130Smckusick 			DELAY(10);
88552130Smckusick 			break;
88652130Smckusick 		}
88752130Smckusick 		break;
88852130Smckusick 	}
88952130Smckusick 	/*
89052130Smckusick 	 * Enable interrupts for other lines which became ready.
89152130Smckusick 	 */
89252130Smckusick 	if (tcr & 0xF) {
89352130Smckusick 		dcaddr->dc_tcr = tcr;
89452130Smckusick 		MachEmptyWriteBuffer();
89552130Smckusick 		DELAY(10);
89652130Smckusick 	}
89752130Smckusick 
89856819Sralph 	splx(s);
89952130Smckusick }
90052130Smckusick #endif /* NDC */
901