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