Home | History | Annotate | Line # | Download | only in dec
dz.c revision 1.3
      1 /*	$NetBSD: dz.c,v 1.3 2002/09/06 13:18:43 gehenna Exp $	*/
      2 /*
      3  * Copyright (c) 1996  Ken C. Wellsch.  All rights reserved.
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Ralph Campbell and Rick Macklem.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: dz.c,v 1.3 2002/09/06 13:18:43 gehenna Exp $");
     41 
     42 #include "opt_ddb.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/callout.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/tty.h>
     49 #include <sys/proc.h>
     50 #include <sys/map.h>
     51 #include <sys/buf.h>
     52 #include <sys/conf.h>
     53 #include <sys/file.h>
     54 #include <sys/uio.h>
     55 #include <sys/kernel.h>
     56 #include <sys/syslog.h>
     57 #include <sys/device.h>
     58 
     59 #include <machine/bus.h>
     60 
     61 #include <dev/dec/dzreg.h>
     62 #include <dev/dec/dzvar.h>
     63 
     64 #define	DZ_READ_BYTE(adr) \
     65 	bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->sc_dr.adr)
     66 #define	DZ_READ_WORD(adr) \
     67 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, sc->sc_dr.adr)
     68 #define	DZ_WRITE_BYTE(adr, val) \
     69 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->sc_dr.adr, val)
     70 #define	DZ_WRITE_WORD(adr, val) \
     71 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, sc->sc_dr.adr, val)
     72 
     73 #include "ioconf.h"
     74 
     75 /* A DZ-11 has 8 ports while a DZV/DZQ-11 has only 4. We use 8 by default */
     76 
     77 #define	NDZLINE 	8
     78 
     79 #define DZ_C2I(c)	((c)<<3)	/* convert controller # to index */
     80 #define DZ_I2C(c)	((c)>>3)	/* convert minor to controller # */
     81 #define DZ_PORT(u)	((u)&07)	/* extract the port # */
     82 
     83 /* Flags used to monitor modem bits, make them understood outside driver */
     84 
     85 #define DML_DTR		TIOCM_DTR
     86 #define DML_DCD		TIOCM_CD
     87 #define DML_RI		TIOCM_RI
     88 #define DML_BRK		0100000		/* no equivalent, we will mask */
     89 
     90 static struct speedtab dzspeedtab[] =
     91 {
     92   {       0,	0		},
     93   {      50,	DZ_LPR_B50	},
     94   {      75,	DZ_LPR_B75	},
     95   {     110,	DZ_LPR_B110	},
     96   {     134,	DZ_LPR_B134	},
     97   {     150,	DZ_LPR_B150	},
     98   {     300,	DZ_LPR_B300	},
     99   {     600,	DZ_LPR_B600	},
    100   {    1200,	DZ_LPR_B1200	},
    101   {    1800,	DZ_LPR_B1800	},
    102   {    2000,	DZ_LPR_B2000	},
    103   {    2400,	DZ_LPR_B2400	},
    104   {    3600,	DZ_LPR_B3600	},
    105   {    4800,	DZ_LPR_B4800	},
    106   {    7200,	DZ_LPR_B7200	},
    107   {    9600,	DZ_LPR_B9600	},
    108   {   19200,	DZ_LPR_B19200	},
    109   {      -1,	-1		}
    110 };
    111 
    112 static void	dzstart(struct tty *);
    113 static int	dzparam(struct tty *, struct termios *);
    114 static unsigned	dzmctl(struct dz_softc *, int, int, int);
    115 static void	dzscan(void *);
    116 
    117 dev_type_open(dzopen);
    118 dev_type_close(dzclose);
    119 dev_type_read(dzread);
    120 dev_type_write(dzwrite);
    121 dev_type_ioctl(dzioctl);
    122 dev_type_stop(dzstop);
    123 dev_type_tty(dztty);
    124 dev_type_poll(dzpoll);
    125 
    126 const struct cdevsw dz_cdevsw = {
    127 	dzopen, dzclose, dzread, dzwrite, dzioctl,
    128 	dzstop, dztty, dzpoll, nommap, D_TTY
    129 };
    130 
    131 /*
    132  * The DZ series doesn't interrupt on carrier transitions,
    133  * so we have to use a timer to watch it.
    134  */
    135 int	dz_timer;	/* true if timer started */
    136 struct callout dzscan_ch;
    137 
    138 #define DZ_DZ	8		/* Unibus DZ-11 board linecount */
    139 #define DZ_DZV	4		/* Q-bus DZV-11 or DZQ-11 */
    140 
    141 void
    142 dzattach(struct dz_softc *sc, struct evcnt *parent_evcnt)
    143 {
    144 	int n;
    145 
    146 	sc->sc_rxint = sc->sc_brk = 0;
    147 
    148 	sc->sc_dr.dr_tcrw = sc->sc_dr.dr_tcr;
    149 	DZ_WRITE_WORD(dr_csr, DZ_CSR_MSE | DZ_CSR_RXIE | DZ_CSR_TXIE);
    150 	DZ_WRITE_BYTE(dr_dtr, 0);
    151 	DZ_WRITE_BYTE(dr_break, 0);
    152 
    153 	/* Initialize our softc structure. Should be done in open? */
    154 
    155 	for (n = 0; n < sc->sc_type; n++)
    156 		sc->sc_dz[n].dz_tty = ttymalloc();
    157 
    158 	evcnt_attach_dynamic(&sc->sc_rintrcnt, EVCNT_TYPE_INTR, parent_evcnt,
    159 		sc->sc_dev.dv_xname, "rintr");
    160 	evcnt_attach_dynamic(&sc->sc_tintrcnt, EVCNT_TYPE_INTR, parent_evcnt,
    161 		sc->sc_dev.dv_xname, "tintr");
    162 
    163 	/* Alas no interrupt on modem bit changes, so we manually scan */
    164 
    165 	if (dz_timer == 0) {
    166 		dz_timer = 1;
    167 		callout_init(&dzscan_ch);
    168 		callout_reset(&dzscan_ch, hz, dzscan, NULL);
    169 	}
    170 	printf("\n");
    171 	return;
    172 }
    173 
    174 /* Receiver Interrupt */
    175 
    176 void
    177 dzrint(void *arg)
    178 {
    179 	struct dz_softc *sc = arg;
    180 	struct tty *tp;
    181 	int cc, line;
    182 	unsigned c;
    183 	int overrun = 0;
    184 
    185 	sc->sc_rxint++;
    186 
    187 	while ((c = DZ_READ_WORD(dr_rbuf)) & DZ_RBUF_DATA_VALID) {
    188 		cc = c & 0xFF;
    189 		line = DZ_PORT(c>>8);
    190 		tp = sc->sc_dz[line].dz_tty;
    191 
    192 		/* Must be caught early */
    193 		if (sc->sc_dz[line].dz_catch &&
    194 		    (*sc->sc_dz[line].dz_catch)(sc->sc_dz[line].dz_private, cc))
    195 			continue;
    196 
    197 		if (!(tp->t_state & TS_ISOPEN)) {
    198 			wakeup((caddr_t)&tp->t_rawq);
    199 			continue;
    200 		}
    201 
    202 		if ((c & DZ_RBUF_OVERRUN_ERR) && overrun == 0) {
    203 			log(LOG_WARNING, "%s: silo overflow, line %d\n",
    204 			    sc->sc_dev.dv_xname, line);
    205 			overrun = 1;
    206 		}
    207 
    208 		/* A BREAK key will appear as a NULL with a framing error */
    209 		if (c & DZ_RBUF_FRAMING_ERR)
    210 			cc |= TTY_FE;
    211 		if (c & DZ_RBUF_PARITY_ERR)
    212 			cc |= TTY_PE;
    213 
    214 		(*tp->t_linesw->l_rint)(cc, tp);
    215 	}
    216 }
    217 
    218 /* Transmitter Interrupt */
    219 
    220 void
    221 dzxint(void *arg)
    222 {
    223 	struct dz_softc *sc = arg;
    224 	struct tty *tp;
    225 	struct clist *cl;
    226 	int line, ch, csr;
    227 	u_char tcr;
    228 
    229 	/*
    230 	 * Switch to POLLED mode.
    231 	 *   Some simple measurements indicated that even on
    232 	 *  one port, by freeing the scanner in the controller
    233 	 *  by either providing a character or turning off
    234 	 *  the port when output is complete, the transmitter
    235 	 *  was ready to accept more output when polled again.
    236 	 *   With just two ports running the game "worms,"
    237 	 *  almost every interrupt serviced both transmitters!
    238 	 *   Each UART is double buffered, so if the scanner
    239 	 *  is quick enough and timing works out, we can even
    240 	 *  feed the same port twice.
    241 	 *
    242 	 * Ragge 980517:
    243 	 * Do not need to turn off interrupts, already at interrupt level.
    244 	 * Remove the pdma stuff; no great need of it right now.
    245 	 */
    246 
    247 	while (((csr = DZ_READ_WORD(dr_csr)) & DZ_CSR_TX_READY) != 0) {
    248 
    249 		line = DZ_PORT(csr>>8);
    250 
    251 		tp = sc->sc_dz[line].dz_tty;
    252 		cl = &tp->t_outq;
    253 		tp->t_state &= ~TS_BUSY;
    254 
    255 		/* Just send out a char if we have one */
    256 		/* As long as we can fill the chip buffer, we just loop here */
    257 		if (cl->c_cc) {
    258 			tp->t_state |= TS_BUSY;
    259 			ch = getc(cl);
    260 			DZ_WRITE_BYTE(dr_tbuf, ch);
    261 			continue;
    262 		}
    263 		/* Nothing to send; clear the scan bit */
    264 		/* Clear xmit scanner bit; dzstart may set it again */
    265 		tcr = DZ_READ_WORD(dr_tcrw);
    266 		tcr &= 255;
    267 		tcr &= ~(1 << line);
    268 		DZ_WRITE_BYTE(dr_tcr, tcr);
    269 		if (sc->sc_dz[line].dz_catch)
    270 			continue;
    271 
    272 		if (tp->t_state & TS_FLUSH)
    273 			tp->t_state &= ~TS_FLUSH;
    274 		else
    275 			ndflush (&tp->t_outq, cl->c_cc);
    276 
    277 		(*tp->t_linesw->l_start)(tp);
    278 	}
    279 }
    280 
    281 int
    282 dzopen(dev_t dev, int flag, int mode, struct proc *p)
    283 {
    284 	struct tty *tp;
    285 	int unit, line;
    286 	struct	dz_softc *sc;
    287 	int s, error = 0;
    288 
    289 	unit = DZ_I2C(minor(dev));
    290 	line = DZ_PORT(minor(dev));
    291 	if (unit >= dz_cd.cd_ndevs ||  dz_cd.cd_devs[unit] == NULL)
    292 		return (ENXIO);
    293 
    294 	sc = dz_cd.cd_devs[unit];
    295 
    296 	if (line >= sc->sc_type)
    297 		return ENXIO;
    298 
    299 	/* if some other device is using the line, it's busy */
    300 	if (sc->sc_dz[line].dz_catch)
    301 		return EBUSY;
    302 
    303 	tp = sc->sc_dz[line].dz_tty;
    304 	if (tp == NULL)
    305 		return (ENODEV);
    306 	tp->t_oproc   = dzstart;
    307 	tp->t_param   = dzparam;
    308 	tp->t_dev = dev;
    309 	if ((tp->t_state & TS_ISOPEN) == 0) {
    310 		ttychars(tp);
    311 		if (tp->t_ispeed == 0) {
    312 			tp->t_iflag = TTYDEF_IFLAG;
    313 			tp->t_oflag = TTYDEF_OFLAG;
    314 			tp->t_cflag = TTYDEF_CFLAG;
    315 			tp->t_lflag = TTYDEF_LFLAG;
    316 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    317 		}
    318 		(void) dzparam(tp, &tp->t_termios);
    319 		ttsetwater(tp);
    320 	} else if ((tp->t_state & TS_XCLUDE) && p->p_ucred->cr_uid != 0)
    321 		return (EBUSY);
    322 	/* Use DMBIS and *not* DMSET or else we clobber incoming bits */
    323 	if (dzmctl(sc, line, DML_DTR, DMBIS) & DML_DCD)
    324 		tp->t_state |= TS_CARR_ON;
    325 	s = spltty();
    326 	while (!(flag & O_NONBLOCK) && !(tp->t_cflag & CLOCAL) &&
    327 	       !(tp->t_state & TS_CARR_ON)) {
    328 		tp->t_wopen++;
    329 		error = ttysleep(tp, (caddr_t)&tp->t_rawq,
    330 				TTIPRI | PCATCH, ttopen, 0);
    331 		tp->t_wopen--;
    332 		if (error)
    333 			break;
    334 	}
    335 	(void) splx(s);
    336 	if (error)
    337 		return (error);
    338 	return ((*tp->t_linesw->l_open)(dev, tp));
    339 }
    340 
    341 /*ARGSUSED*/
    342 int
    343 dzclose(dev_t dev, int flag, int mode, struct proc *p)
    344 {
    345 	struct	dz_softc *sc;
    346 	struct tty *tp;
    347 	int unit, line;
    348 
    349 
    350 	unit = DZ_I2C(minor(dev));
    351 	line = DZ_PORT(minor(dev));
    352 	sc = dz_cd.cd_devs[unit];
    353 
    354 	tp = sc->sc_dz[line].dz_tty;
    355 
    356 	(*tp->t_linesw->l_close)(tp, flag);
    357 
    358 	/* Make sure a BREAK state is not left enabled. */
    359 	(void) dzmctl(sc, line, DML_BRK, DMBIC);
    360 
    361 	/* Do a hangup if so required. */
    362 	if ((tp->t_cflag & HUPCL) || tp->t_wopen || !(tp->t_state & TS_ISOPEN))
    363 		(void) dzmctl(sc, line, 0, DMSET);
    364 
    365 	return (ttyclose(tp));
    366 }
    367 
    368 int
    369 dzread(dev_t dev, struct uio *uio, int flag)
    370 {
    371 	struct tty *tp;
    372 	struct	dz_softc *sc;
    373 
    374 	sc = dz_cd.cd_devs[DZ_I2C(minor(dev))];
    375 
    376 	tp = sc->sc_dz[DZ_PORT(minor(dev))].dz_tty;
    377 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    378 }
    379 
    380 int
    381 dzwrite(dev_t dev, struct uio *uio, int flag)
    382 {
    383 	struct tty *tp;
    384 	struct	dz_softc *sc;
    385 
    386 	sc = dz_cd.cd_devs[DZ_I2C(minor(dev))];
    387 
    388 	tp = sc->sc_dz[DZ_PORT(minor(dev))].dz_tty;
    389 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    390 }
    391 
    392 int
    393 dzpoll(dev, events, p)
    394 	dev_t dev;
    395 	int events;
    396 	struct proc *p;
    397 {
    398 	struct tty *tp;
    399 	struct	dz_softc *sc;
    400 
    401 	sc = dz_cd.cd_devs[DZ_I2C(minor(dev))];
    402 
    403 	tp = sc->sc_dz[DZ_PORT(minor(dev))].dz_tty;
    404 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    405 }
    406 
    407 /*ARGSUSED*/
    408 int
    409 dzioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    410 {
    411 	struct	dz_softc *sc;
    412 	struct tty *tp;
    413 	int unit, line;
    414 	int error;
    415 
    416 	unit = DZ_I2C(minor(dev));
    417 	line = DZ_PORT(minor(dev));
    418 	sc = dz_cd.cd_devs[unit];
    419 	tp = sc->sc_dz[line].dz_tty;
    420 
    421 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    422 	if (error >= 0)
    423 		return (error);
    424 
    425 	error = ttioctl(tp, cmd, data, flag, p);
    426 	if (error >= 0)
    427 		return (error);
    428 
    429 	switch (cmd) {
    430 
    431 	case TIOCSBRK:
    432 		(void) dzmctl(sc, line, DML_BRK, DMBIS);
    433 		break;
    434 
    435 	case TIOCCBRK:
    436 		(void) dzmctl(sc, line, DML_BRK, DMBIC);
    437 		break;
    438 
    439 	case TIOCSDTR:
    440 		(void) dzmctl(sc, line, DML_DTR, DMBIS);
    441 		break;
    442 
    443 	case TIOCCDTR:
    444 		(void) dzmctl(sc, line, DML_DTR, DMBIC);
    445 		break;
    446 
    447 	case TIOCMSET:
    448 		(void) dzmctl(sc, line, *(int *)data, DMSET);
    449 		break;
    450 
    451 	case TIOCMBIS:
    452 		(void) dzmctl(sc, line, *(int *)data, DMBIS);
    453 		break;
    454 
    455 	case TIOCMBIC:
    456 		(void) dzmctl(sc, line, *(int *)data, DMBIC);
    457 		break;
    458 
    459 	case TIOCMGET:
    460 		*(int *)data = (dzmctl(sc, line, 0, DMGET) & ~DML_BRK);
    461 		break;
    462 
    463 	default:
    464 		return (EPASSTHROUGH);
    465 	}
    466 	return (0);
    467 }
    468 
    469 struct tty *
    470 dztty(dev_t dev)
    471 {
    472 	struct	dz_softc *sc = dz_cd.cd_devs[DZ_I2C(minor(dev))];
    473         struct tty *tp = sc->sc_dz[DZ_PORT(minor(dev))].dz_tty;
    474 
    475         return (tp);
    476 }
    477 
    478 /*ARGSUSED*/
    479 void
    480 dzstop(struct tty *tp, int flag)
    481 {
    482 	if (tp->t_state & TS_BUSY)
    483 		if (!(tp->t_state & TS_TTSTOP))
    484 			tp->t_state |= TS_FLUSH;
    485 }
    486 
    487 void
    488 dzstart(struct tty *tp)
    489 {
    490 	struct dz_softc *sc;
    491 	struct clist *cl;
    492 	int unit, line, s;
    493 	char state;
    494 
    495 	unit = DZ_I2C(minor(tp->t_dev));
    496 	line = DZ_PORT(minor(tp->t_dev));
    497 	sc = dz_cd.cd_devs[unit];
    498 
    499 	s = spltty();
    500 	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
    501 		return;
    502 	cl = &tp->t_outq;
    503 	if (cl->c_cc <= tp->t_lowat) {
    504 		if (tp->t_state & TS_ASLEEP) {
    505 			tp->t_state &= ~TS_ASLEEP;
    506 			wakeup((caddr_t)cl);
    507 		}
    508 		selwakeup(&tp->t_wsel);
    509 	}
    510 	if (cl->c_cc == 0)
    511 		return;
    512 
    513 	tp->t_state |= TS_BUSY;
    514 
    515 	state = DZ_READ_WORD(dr_tcrw) & 255;
    516 	if ((state & (1 << line)) == 0) {
    517 		DZ_WRITE_BYTE(dr_tcr, state | (1 << line));
    518 	}
    519 	dzxint(sc);
    520 	splx(s);
    521 }
    522 
    523 static int
    524 dzparam(struct tty *tp, struct termios *t)
    525 {
    526 	struct	dz_softc *sc;
    527 	int cflag = t->c_cflag;
    528 	int unit, line;
    529 	int ispeed = ttspeedtab(t->c_ispeed, dzspeedtab);
    530 	int ospeed = ttspeedtab(t->c_ospeed, dzspeedtab);
    531 	unsigned lpr;
    532 	int s;
    533 
    534 	unit = DZ_I2C(minor(tp->t_dev));
    535 	line = DZ_PORT(minor(tp->t_dev));
    536 	sc = dz_cd.cd_devs[unit];
    537 
    538 	/* check requested parameters */
    539         if (ospeed < 0 || ispeed < 0 || ispeed != ospeed)
    540                 return (EINVAL);
    541 
    542         tp->t_ispeed = t->c_ispeed;
    543         tp->t_ospeed = t->c_ospeed;
    544         tp->t_cflag = cflag;
    545 
    546 	if (ospeed == 0) {
    547 		(void) dzmctl(sc, line, 0, DMSET);	/* hang up line */
    548 		return (0);
    549 	}
    550 
    551 	s = spltty();
    552 
    553 	lpr = DZ_LPR_RX_ENABLE | ((ispeed&0xF)<<8) | line;
    554 
    555 	switch (cflag & CSIZE)
    556 	{
    557 	  case CS5:
    558 		lpr |= DZ_LPR_5_BIT_CHAR;
    559 		break;
    560 	  case CS6:
    561 		lpr |= DZ_LPR_6_BIT_CHAR;
    562 		break;
    563 	  case CS7:
    564 		lpr |= DZ_LPR_7_BIT_CHAR;
    565 		break;
    566 	  default:
    567 		lpr |= DZ_LPR_8_BIT_CHAR;
    568 		break;
    569 	}
    570 	if (cflag & PARENB)
    571 		lpr |= DZ_LPR_PARENB;
    572 	if (cflag & PARODD)
    573 		lpr |= DZ_LPR_OPAR;
    574 	if (cflag & CSTOPB)
    575 		lpr |= DZ_LPR_2_STOP;
    576 
    577 	DZ_WRITE_WORD(dr_lpr, lpr);
    578 
    579 	(void) splx(s);
    580 	return (0);
    581 }
    582 
    583 static unsigned
    584 dzmctl(struct dz_softc *sc, int line, int bits, int how)
    585 {
    586 	unsigned status;
    587 	unsigned mbits;
    588 	unsigned bit;
    589 	int s;
    590 
    591 	s = spltty();
    592 
    593 	mbits = 0;
    594 
    595 	bit = (1 << line);
    596 
    597 	/* external signals as seen from the port */
    598 
    599 	status = DZ_READ_BYTE(dr_dcd) | sc->sc_dsr;
    600 
    601 	if (status & bit)
    602 		mbits |= DML_DCD;
    603 
    604 	status = DZ_READ_BYTE(dr_ring);
    605 
    606 	if (status & bit)
    607 		mbits |= DML_RI;
    608 
    609 	/* internal signals/state delivered to port */
    610 
    611 	status = DZ_READ_BYTE(dr_dtr);
    612 
    613 	if (status & bit)
    614 		mbits |= DML_DTR;
    615 
    616 	if (sc->sc_brk & bit)
    617 		mbits |= DML_BRK;
    618 
    619 	switch (how)
    620 	{
    621 	  case DMSET:
    622 		mbits = bits;
    623 		break;
    624 
    625 	  case DMBIS:
    626 		mbits |= bits;
    627 		break;
    628 
    629 	  case DMBIC:
    630 		mbits &= ~bits;
    631 		break;
    632 
    633 	  case DMGET:
    634 		(void) splx(s);
    635 		return (mbits);
    636 	}
    637 
    638 	if (mbits & DML_DTR) {
    639 		DZ_WRITE_BYTE(dr_dtr, DZ_READ_BYTE(dr_dtr) | bit);
    640 	} else {
    641 		DZ_WRITE_BYTE(dr_dtr, DZ_READ_BYTE(dr_dtr) & ~bit);
    642 	}
    643 
    644 	if (mbits & DML_BRK) {
    645 		sc->sc_brk |= bit;
    646 		DZ_WRITE_BYTE(dr_break, sc->sc_brk);
    647 	} else {
    648 		sc->sc_brk &= ~bit;
    649 		DZ_WRITE_BYTE(dr_break, sc->sc_brk);
    650 	}
    651 
    652 	(void) splx(s);
    653 	return (mbits);
    654 }
    655 
    656 /*
    657  * This is called by timeout() periodically.
    658  * Check to see if modem status bits have changed.
    659  */
    660 static void
    661 dzscan(void *arg)
    662 {
    663 	struct dz_softc *sc;
    664 	struct tty *tp;
    665 	int n, bit, port;
    666 	unsigned csr;
    667 	int s;
    668 
    669 	s = spltty();
    670 
    671 	for (n = 0; n < dz_cd.cd_ndevs; n++) {
    672 
    673 		if (dz_cd.cd_devs[n] == NULL)
    674 			continue;
    675 
    676 		sc = dz_cd.cd_devs[n];
    677 
    678 		for (port = 0; port < sc->sc_type; port++) {
    679 
    680 			tp = sc->sc_dz[port].dz_tty;
    681 			bit = (1 << port);
    682 
    683 			if ((DZ_READ_BYTE(dr_dcd) | sc->sc_dsr) & bit) {
    684 				if (!(tp->t_state & TS_CARR_ON))
    685 					(*tp->t_linesw->l_modem) (tp, 1);
    686 			} else if ((tp->t_state & TS_CARR_ON) &&
    687 			    (*tp->t_linesw->l_modem)(tp, 0) == 0) {
    688 				DZ_WRITE_BYTE(dr_tcr,
    689 				    (DZ_READ_WORD(dr_tcrw) & 255) & ~bit);
    690 			}
    691 	    	}
    692 
    693 		/*
    694 		 *  If the RX interrupt rate is this high, switch
    695 		 *  the controller to Silo Alarm - which means don't
    696 	 	 *  interrupt until the RX silo has 16 characters in
    697 	 	 *  it (the silo is 64 characters in all).
    698 		 *  Avoid oscillating SA on and off by not turning
    699 		 *  if off unless the rate is appropriately low.
    700 		 */
    701 
    702 		csr = DZ_READ_WORD(dr_csr);
    703 
    704 		if (sc->sc_rxint > (16*10)) {
    705 			if ((csr & DZ_CSR_SAE) == 0)
    706 				DZ_WRITE_WORD(dr_csr, csr | DZ_CSR_SAE);
    707 	    	} else if ((csr & DZ_CSR_SAE) != 0)
    708 			if (sc->sc_rxint < 10)
    709 				DZ_WRITE_WORD(dr_csr, csr & ~(DZ_CSR_SAE));
    710 
    711 		sc->sc_rxint = 0;
    712 	}
    713 	(void) splx(s);
    714 	callout_reset(&dzscan_ch, hz, dzscan, NULL);
    715 	return;
    716 }
    717 
    718 /*
    719  * Called after an ubareset. The DZ card is reset, but the only thing
    720  * that must be done is to start the receiver and transmitter again.
    721  * No DMA setup to care about.
    722  */
    723 void
    724 dzreset(struct device *dev)
    725 {
    726 	struct dz_softc *sc = (void *)dev;
    727 	struct tty *tp;
    728 	int i;
    729 
    730 	for (i = 0; i < sc->sc_type; i++) {
    731 		tp = sc->sc_dz[i].dz_tty;
    732 
    733 		if (((tp->t_state & TS_ISOPEN) == 0) || (tp->t_wopen == 0))
    734 			continue;
    735 
    736 		dzparam(tp, &tp->t_termios);
    737 		dzmctl(sc, i, DML_DTR, DMSET);
    738 		tp->t_state &= ~TS_BUSY;
    739 		dzstart(tp);	/* Kick off transmitter again */
    740 	}
    741 }
    742