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