Home | History | Annotate | Line # | Download | only in ic
z8530tty.c revision 1.47
      1 /*	$NetBSD: z8530tty.c,v 1.47 1998/03/22 00:55:37 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
      5  *	Charles M. Hannum.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Charles M. Hannum.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1994 Gordon W. Ross
     35  * Copyright (c) 1992, 1993
     36  *	The Regents of the University of California.  All rights reserved.
     37  *
     38  * This software was developed by the Computer Systems Engineering group
     39  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     40  * contributed to Berkeley.
     41  *
     42  * All advertising materials mentioning features or use of this software
     43  * must display the following acknowledgement:
     44  *	This product includes software developed by the University of
     45  *	California, Lawrence Berkeley Laboratory.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. All advertising materials mentioning features or use of this software
     56  *    must display the following acknowledgement:
     57  *	This product includes software developed by the University of
     58  *	California, Berkeley and its contributors.
     59  * 4. Neither the name of the University nor the names of its contributors
     60  *    may be used to endorse or promote products derived from this software
     61  *    without specific prior written permission.
     62  *
     63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73  * SUCH DAMAGE.
     74  *
     75  *	@(#)zs.c	8.1 (Berkeley) 7/19/93
     76  */
     77 
     78 /*
     79  * Zilog Z8530 Dual UART driver (tty interface)
     80  *
     81  * This is the "slave" driver that will be attached to
     82  * the "zsc" driver for plain "tty" async. serial lines.
     83  *
     84  * Credits, history:
     85  *
     86  * The original version of this code was the sparc/dev/zs.c driver
     87  * as distributed with the Berkeley 4.4 Lite release.  Since then,
     88  * Gordon Ross reorganized the code into the current parent/child
     89  * driver scheme, separating the Sun keyboard and mouse support
     90  * into independent child drivers.
     91  *
     92  * RTS/CTS flow-control support was a collaboration of:
     93  *	Gordon Ross <gwr (at) netbsd.org>,
     94  *	Bill Studenmund <wrstuden (at) loki.stanford.edu>
     95  *	Ian Dall <Ian.Dall (at) dsto.defence.gov.au>
     96  */
     97 
     98 #include <sys/param.h>
     99 #include <sys/systm.h>
    100 #include <sys/proc.h>
    101 #include <sys/device.h>
    102 #include <sys/conf.h>
    103 #include <sys/file.h>
    104 #include <sys/ioctl.h>
    105 #include <sys/malloc.h>
    106 #include <sys/tty.h>
    107 #include <sys/time.h>
    108 #include <sys/kernel.h>
    109 #include <sys/syslog.h>
    110 
    111 #include <dev/ic/z8530reg.h>
    112 #include <machine/z8530var.h>
    113 
    114 #include "locators.h"
    115 
    116 /*
    117  * How many input characters we can buffer.
    118  * The port-specific var.h may override this.
    119  * Note: must be a power of two!
    120  */
    121 #ifndef	ZSTTY_RING_SIZE
    122 #define	ZSTTY_RING_SIZE	2048
    123 #endif
    124 
    125 /*
    126  * Make this an option variable one can patch.
    127  * But be warned:  this must be a power of 2!
    128  */
    129 u_int zstty_rbuf_size = ZSTTY_RING_SIZE;
    130 
    131 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    132 u_int zstty_rbuf_hiwat = (ZSTTY_RING_SIZE * 1) / 4;
    133 u_int zstty_rbuf_lowat = (ZSTTY_RING_SIZE * 3) / 4;
    134 
    135 struct zstty_softc {
    136 	struct	device zst_dev;		/* required first: base device */
    137 	struct  tty *zst_tty;
    138 	struct	zs_chanstate *zst_cs;
    139 
    140 	u_int zst_overflows,
    141 	      zst_floods,
    142 	      zst_errors;
    143 
    144 	int zst_hwflags,	/* see z8530var.h */
    145 	    zst_swflags;	/* TIOCFLAG_SOFTCAR, ... <ttycom.h> */
    146 
    147 	u_int zst_r_hiwat,
    148 	      zst_r_lowat;
    149 	u_char *volatile zst_rbget,
    150 	       *volatile zst_rbput;
    151 	volatile u_int zst_rbavail;
    152 	u_char *zst_rbuf,
    153 	       *zst_ebuf;
    154 
    155 	/*
    156 	 * The transmit byte count and address are used for pseudo-DMA
    157 	 * output in the hardware interrupt code.  PDMA can be suspended
    158 	 * to get pending changes done; heldtbc is used for this.  It can
    159 	 * also be stopped for ^S; this sets TS_TTSTOP in tp->t_state.
    160 	 */
    161 	u_char *zst_tba;		/* transmit buffer address */
    162 	u_int zst_tbc,			/* transmit byte count */
    163 	      zst_heldtbc;		/* held tbc while xmission stopped */
    164 
    165 	/* Flags to communicate with zstty_softint() */
    166 	volatile u_char zst_rx_flags,	/* receiver blocked */
    167 #define	RX_TTY_BLOCKED		0x01
    168 #define	RX_TTY_OVERFLOWED	0x02
    169 #define	RX_IBUF_BLOCKED		0x04
    170 #define	RX_IBUF_OVERFLOWED	0x08
    171 #define	RX_ANY_BLOCK		0x0f
    172 			zst_tx_busy,	/* working on an output chunk */
    173 			zst_tx_done,	/* done with one output chunk */
    174 			zst_tx_stopped,	/* H/W level stop (lost CTS) */
    175 			zst_st_check,	/* got a status interrupt */
    176 			zst_rx_ready;
    177 };
    178 
    179 /* Macros to clear/set/test flags. */
    180 #define SET(t, f)	(t) |= (f)
    181 #define CLR(t, f)	(t) &= ~(f)
    182 #define ISSET(t, f)	((t) & (f))
    183 
    184 /* Definition of the driver for autoconfig. */
    185 #ifdef	__BROKEN_INDIRECT_CONFIG
    186 static int	zstty_match(struct device *, void *, void *);
    187 #else
    188 static int	zstty_match(struct device *, struct cfdata *, void *);
    189 #endif
    190 static void	zstty_attach(struct device *, struct device *, void *);
    191 
    192 struct cfattach zstty_ca = {
    193 	sizeof(struct zstty_softc), zstty_match, zstty_attach
    194 };
    195 
    196 extern struct cfdriver zstty_cd;
    197 
    198 struct zsops zsops_tty;
    199 
    200 /* Routines called from other code. */
    201 cdev_decl(zs);	/* open, close, read, write, ioctl, stop, ... */
    202 
    203 static void zs_shutdown __P((struct zstty_softc *));
    204 static void	zsstart __P((struct tty *));
    205 static int	zsparam __P((struct tty *, struct termios *));
    206 static void zs_modem __P((struct zstty_softc *zst, int onoff));
    207 static int	zshwiflow __P((struct tty *, int));
    208 static void zs_hwiflow __P((struct zstty_softc *));
    209 
    210 #define	ZSUNIT(x)	(minor(x) & 0x7ffff)
    211 #define	ZSDIALOUT(x)	(minor(x) & 0x80000)
    212 
    213 /*
    214  * zstty_match: how is this zs channel configured?
    215  */
    216 #ifdef	__BROKEN_INDIRECT_CONFIG
    217 int
    218 zstty_match(parent, vcf, aux)
    219 	struct device *parent;
    220 	void   *vcf, *aux;
    221 {
    222 	struct cfdata *cf = vcf;
    223 	struct zsc_attach_args *args = aux;
    224 
    225 	/* Exact match is better than wildcard. */
    226 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
    227 		return 2;
    228 
    229 	/* This driver accepts wildcard. */
    230 	if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
    231 		return 1;
    232 
    233 	return 0;
    234 }
    235 #else	/* __BROKEN_INDIRECT_CONFIG */
    236 int
    237 zstty_match(parent, cf, aux)
    238 	struct device *parent;
    239 	struct cfdata *cf;
    240 	void   *aux;
    241 {
    242 	struct zsc_attach_args *args = aux;
    243 
    244 	/* Exact match is better than wildcard. */
    245 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
    246 		return 2;
    247 
    248 	/* This driver accepts wildcard. */
    249 	if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
    250 		return 1;
    251 
    252 	return 0;
    253 }
    254 #endif	/* __BROKEN_INDIRECT_CONFIG */
    255 
    256 void
    257 zstty_attach(parent, self, aux)
    258 	struct device *parent, *self;
    259 	void   *aux;
    260 
    261 {
    262 	struct zsc_softc *zsc = (void *) parent;
    263 	struct zstty_softc *zst = (void *) self;
    264 	struct cfdata *cf = self->dv_cfdata;
    265 	struct zsc_attach_args *args = aux;
    266 	struct zs_chanstate *cs;
    267 	struct tty *tp;
    268 	int channel, s, tty_unit;
    269 	dev_t dev;
    270 
    271 	tty_unit = zst->zst_dev.dv_unit;
    272 	channel = args->channel;
    273 	cs = zsc->zsc_cs[channel];
    274 	cs->cs_private = zst;
    275 	cs->cs_ops = &zsops_tty;
    276 
    277 	zst->zst_cs = cs;
    278 	zst->zst_swflags = cf->cf_flags;	/* softcar, etc. */
    279 	zst->zst_hwflags = args->hwflags;
    280 	dev = makedev(zs_major, tty_unit);
    281 
    282 	if (zst->zst_swflags)
    283 		printf(" flags 0x%x", zst->zst_swflags);
    284 
    285 	if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE))
    286 		printf(" (console)");
    287 	else {
    288 #ifdef KGDB
    289 		/*
    290 		 * Allow kgdb to "take over" this port.  Returns true
    291 		 * if this serial port is in-use by kgdb.
    292 		 */
    293 		if (zs_check_kgdb(cs, dev)) {
    294 			printf(" (kgdb)\n");
    295 			/*
    296 			 * This is the kgdb port (exclusive use)
    297 			 * so skip the normal attach code.
    298 			 */
    299 			return;
    300 		}
    301 #endif
    302 	}
    303 	printf("\n");
    304 
    305 	tp = ttymalloc();
    306 	tp->t_oproc = zsstart;
    307 	tp->t_param = zsparam;
    308 	tp->t_hwiflow = zshwiflow;
    309 	tty_attach(tp);
    310 
    311 	zst->zst_tty = tp;
    312 	zst->zst_rbuf = malloc(zstty_rbuf_size << 1, M_DEVBUF, M_WAITOK);
    313 	zst->zst_ebuf = zst->zst_rbuf + (zstty_rbuf_size << 1);
    314 	/* Disable the high water mark. */
    315 	zst->zst_r_hiwat = 0;
    316 	zst->zst_r_lowat = 0;
    317 	zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
    318 	zst->zst_rbavail = zstty_rbuf_size;
    319 
    320 	/* XXX - Do we need an MD hook here? */
    321 
    322 	/*
    323 	 * Hardware init
    324 	 */
    325 	if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
    326 		/* Call zsparam similar to open. */
    327 		struct termios t;
    328 
    329 		s = splzs();
    330 
    331 		/* Turn on interrupts. */
    332 		cs->cs_creg[1] = cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    333 		zs_write_reg(cs, 1, cs->cs_creg[1]);
    334 
    335 		/* Fetch the current modem control status, needed later. */
    336 		cs->cs_rr0 = zs_read_csr(cs);
    337 
    338 		splx(s);
    339 
    340 		/* Setup the "new" parameters in t. */
    341 		t.c_ispeed = 0;
    342 		t.c_ospeed = cs->cs_defspeed;
    343 		t.c_cflag = cs->cs_defcflag;
    344 		/* Make sure zsparam will see changes. */
    345 		tp->t_ospeed = 0;
    346 		(void) zsparam(tp, &t);
    347 
    348 		s = splzs();
    349 
    350 		/* Make sure DTR is on now. */
    351 		zs_modem(zst, 1);
    352 
    353 		splx(s);
    354 	} else {
    355 		/* Not the console; may need reset. */
    356 		int reset;
    357 
    358 		reset = (channel == 0) ? ZSWR9_A_RESET : ZSWR9_B_RESET;
    359 
    360 		s = splzs();
    361 
    362 		zs_write_reg(cs, 9, reset);
    363 
    364 		/* Will raise DTR in open. */
    365 		zs_modem(zst, 0);
    366 
    367 		splx(s);
    368 	}
    369 }
    370 
    371 
    372 /*
    373  * Return pointer to our tty.
    374  */
    375 struct tty *
    376 zstty(dev)
    377 	dev_t dev;
    378 {
    379 	struct zstty_softc *zst;
    380 	int unit = ZSUNIT(dev);
    381 
    382 #ifdef	DIAGNOSTIC
    383 	if (unit >= zstty_cd.cd_ndevs)
    384 		panic("zstty");
    385 #endif
    386 	zst = zstty_cd.cd_devs[unit];
    387 	return (zst->zst_tty);
    388 }
    389 
    390 
    391 void
    392 zs_shutdown(zst)
    393 	struct zstty_softc *zst;
    394 {
    395 	struct zs_chanstate *cs = zst->zst_cs;
    396 	struct tty *tp = zst->zst_tty;
    397 	int s;
    398 
    399 	s = splzs();
    400 
    401 	/* If we were asserting flow control, then deassert it. */
    402 	SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
    403 	zs_hwiflow(zst);
    404 
    405 	/* Clear any break condition set with TIOCSBRK. */
    406 	zs_break(cs, 0);
    407 
    408 	/*
    409 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    410 	 * notice even if we immediately open the port again.
    411 	 */
    412 	if (ISSET(tp->t_cflag, HUPCL)) {
    413 		zs_modem(zst, 0);
    414 		(void) tsleep(cs, TTIPRI, ttclos, hz);
    415 	}
    416 
    417 	/* Turn off interrupts if not the console. */
    418 	if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE))
    419 		cs->cs_creg[1] = cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    420 	else
    421 		cs->cs_creg[1] = cs->cs_preg[1] = 0;
    422 	zs_write_reg(cs, 1, cs->cs_creg[1]);
    423 
    424 	splx(s);
    425 }
    426 
    427 /*
    428  * Open a zs serial (tty) port.
    429  */
    430 int
    431 zsopen(dev, flags, mode, p)
    432 	dev_t dev;
    433 	int flags;
    434 	int mode;
    435 	struct proc *p;
    436 {
    437 	int unit = ZSUNIT(dev);
    438 	struct zstty_softc *zst;
    439 	struct zs_chanstate *cs;
    440 	struct tty *tp;
    441 	int s, s2;
    442 	int error;
    443 
    444 	if (unit >= zstty_cd.cd_ndevs)
    445 		return (ENXIO);
    446 	zst = zstty_cd.cd_devs[unit];
    447 	if (zst == 0)
    448 		return (ENXIO);
    449 	tp = zst->zst_tty;
    450 	cs = zst->zst_cs;
    451 
    452 	/* If KGDB took the line, then tp==NULL */
    453 	if (tp == NULL)
    454 		return (EBUSY);
    455 
    456 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    457 	    ISSET(tp->t_state, TS_XCLUDE) &&
    458 	    p->p_ucred->cr_uid != 0)
    459 		return (EBUSY);
    460 
    461 	s = spltty();
    462 
    463 	/*
    464 	 * Do the following iff this is a first open.
    465 	 */
    466 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    467 		struct termios t;
    468 
    469 		tp->t_dev = dev;
    470 
    471 		s2 = splzs();
    472 
    473 		/* Turn on interrupts. */
    474 		cs->cs_creg[1] = cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    475 		zs_write_reg(cs, 1, cs->cs_creg[1]);
    476 
    477 		/* Fetch the current modem control status, needed later. */
    478 		cs->cs_rr0 = zs_read_csr(cs);
    479 
    480 		splx(s2);
    481 
    482 		/*
    483 		 * Initialize the termios status to the defaults.  Add in the
    484 		 * sticky bits from TIOCSFLAGS.
    485 		 */
    486 		t.c_ispeed = 0;
    487 		t.c_ospeed = cs->cs_defspeed;
    488 		t.c_cflag = cs->cs_defcflag;
    489 		if (ISSET(zst->zst_swflags, TIOCFLAG_CLOCAL))
    490 			SET(t.c_cflag, CLOCAL);
    491 		if (ISSET(zst->zst_swflags, TIOCFLAG_CRTSCTS))
    492 			SET(t.c_cflag, CRTSCTS);
    493 		if (ISSET(zst->zst_swflags, TIOCFLAG_CDTRCTS))
    494 			SET(t.c_cflag, CDTRCTS);
    495 		if (ISSET(zst->zst_swflags, TIOCFLAG_MDMBUF))
    496 			SET(t.c_cflag, MDMBUF);
    497 		/* Make sure zsparam will see changes. */
    498 		tp->t_ospeed = 0;
    499 		(void) zsparam(tp, &t);
    500 		/*
    501 		 * Note: zsparam has done: cflag, ispeed, ospeed
    502 		 * so we just need to do: iflag, oflag, lflag, cc
    503 		 * For "raw" mode, just leave all zeros.
    504 		 */
    505 		if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_RAW)) {
    506 			tp->t_iflag = TTYDEF_IFLAG;
    507 			tp->t_oflag = TTYDEF_OFLAG;
    508 			tp->t_lflag = TTYDEF_LFLAG;
    509 		} else {
    510 			tp->t_iflag = 0;
    511 			tp->t_oflag = 0;
    512 			tp->t_lflag = 0;
    513 		}
    514 		ttychars(tp);
    515 		ttsetwater(tp);
    516 
    517 		s2 = splzs();
    518 
    519 		/*
    520 		 * Turn on DTR.  We must always do this, even if carrier is not
    521 		 * present, because otherwise we'd have to use TIOCSDTR
    522 		 * immediately after setting CLOCAL, which applications do not
    523 		 * expect.  We always assert DTR while the device is open
    524 		 * unless explicitly requested to deassert it.
    525 		 */
    526 		zs_modem(zst, 1);
    527 
    528 		/* Clear the input ring, and unblock. */
    529 		zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
    530 		zst->zst_rbavail = zstty_rbuf_size;
    531 		zs_iflush(cs);
    532 		CLR(zst->zst_rx_flags, RX_ANY_BLOCK);
    533 		zs_hwiflow(zst);
    534 
    535 		splx(s2);
    536 	}
    537 
    538 	splx(s);
    539 
    540 	error = ttyopen(tp, ZSDIALOUT(dev), ISSET(flags, O_NONBLOCK));
    541 	if (error)
    542 		goto bad;
    543 
    544 	error = (*linesw[tp->t_line].l_open)(dev, tp);
    545 	if (error)
    546 		goto bad;
    547 
    548 	return (0);
    549 
    550 bad:
    551 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    552 		/*
    553 		 * We failed to open the device, and nobody else had it opened.
    554 		 * Clean up the state as appropriate.
    555 		 */
    556 		zs_shutdown(zst);
    557 	}
    558 
    559 	return (error);
    560 }
    561 
    562 /*
    563  * Close a zs serial port.
    564  */
    565 int
    566 zsclose(dev, flags, mode, p)
    567 	dev_t dev;
    568 	int flags;
    569 	int mode;
    570 	struct proc *p;
    571 {
    572 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(dev)];
    573 	struct tty *tp = zst->zst_tty;
    574 
    575 	/* XXX This is for cons.c. */
    576 	if (!ISSET(tp->t_state, TS_ISOPEN))
    577 		return 0;
    578 
    579 	(*linesw[tp->t_line].l_close)(tp, flags);
    580 	ttyclose(tp);
    581 
    582 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    583 		/*
    584 		 * Although we got a last close, the device may still be in
    585 		 * use; e.g. if this was the dialout node, and there are still
    586 		 * processes waiting for carrier on the non-dialout node.
    587 		 */
    588 		zs_shutdown(zst);
    589 	}
    590 
    591 	return (0);
    592 }
    593 
    594 /*
    595  * Read/write zs serial port.
    596  */
    597 int
    598 zsread(dev, uio, flags)
    599 	dev_t dev;
    600 	struct uio *uio;
    601 	int flags;
    602 {
    603 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(dev)];
    604 	struct tty *tp = zst->zst_tty;
    605 
    606 	return ((*linesw[tp->t_line].l_read)(tp, uio, flags));
    607 }
    608 
    609 int
    610 zswrite(dev, uio, flags)
    611 	dev_t dev;
    612 	struct uio *uio;
    613 	int flags;
    614 {
    615 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(dev)];
    616 	struct tty *tp = zst->zst_tty;
    617 
    618 	return ((*linesw[tp->t_line].l_write)(tp, uio, flags));
    619 }
    620 
    621 int
    622 zsioctl(dev, cmd, data, flag, p)
    623 	dev_t dev;
    624 	u_long cmd;
    625 	caddr_t data;
    626 	int flag;
    627 	struct proc *p;
    628 {
    629 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(dev)];
    630 	struct zs_chanstate *cs = zst->zst_cs;
    631 	struct tty *tp = zst->zst_tty;
    632 	int error;
    633 	int s;
    634 
    635 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    636 	if (error >= 0)
    637 		return (error);
    638 
    639 	error = ttioctl(tp, cmd, data, flag, p);
    640 	if (error >= 0)
    641 		return (error);
    642 
    643 #ifdef	ZS_MD_IOCTL
    644 	error = ZS_MD_IOCTL;
    645 	if (error >= 0)
    646 		return (error);
    647 #endif	/* ZS_MD_IOCTL */
    648 
    649 	error = 0;
    650 
    651 	s = splzs();
    652 
    653 	switch (cmd) {
    654 	case TIOCSBRK:
    655 		zs_break(cs, 1);
    656 		break;
    657 
    658 	case TIOCCBRK:
    659 		zs_break(cs, 0);
    660 		break;
    661 
    662 	case TIOCGFLAGS:
    663 		*(int *)data = zst->zst_swflags;
    664 		break;
    665 
    666 	case TIOCSFLAGS:
    667 		error = suser(p->p_ucred, &p->p_acflag);
    668 		if (error)
    669 			break;
    670 		zst->zst_swflags = *(int *)data;
    671 		break;
    672 
    673 	case TIOCSDTR:
    674 		zs_modem(zst, 1);
    675 		break;
    676 
    677 	case TIOCCDTR:
    678 		zs_modem(zst, 0);
    679 		break;
    680 
    681 	case TIOCMSET:
    682 	case TIOCMBIS:
    683 	case TIOCMBIC:
    684 	case TIOCMGET:
    685 	default:
    686 		error = ENOTTY;
    687 		break;
    688 	}
    689 
    690 	splx(s);
    691 
    692 	return (error);
    693 }
    694 
    695 /*
    696  * Start or restart transmission.
    697  */
    698 static void
    699 zsstart(tp)
    700 	struct tty *tp;
    701 {
    702 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(tp->t_dev)];
    703 	struct zs_chanstate *cs = zst->zst_cs;
    704 	int s;
    705 
    706 	s = spltty();
    707 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
    708 		goto out;
    709 	if (zst->zst_tx_stopped)
    710 		goto out;
    711 
    712 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    713 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    714 			CLR(tp->t_state, TS_ASLEEP);
    715 			wakeup((caddr_t)&tp->t_outq);
    716 		}
    717 		selwakeup(&tp->t_wsel);
    718 		if (tp->t_outq.c_cc == 0)
    719 			goto out;
    720 	}
    721 
    722 	/* Grab the first contiguous region of buffer space. */
    723 	{
    724 		u_char *tba;
    725 		int tbc;
    726 
    727 		tba = tp->t_outq.c_cf;
    728 		tbc = ndqb(&tp->t_outq, 0);
    729 
    730 		(void) splzs();
    731 
    732 		zst->zst_tba = tba;
    733 		zst->zst_tbc = tbc;
    734 	}
    735 
    736 	SET(tp->t_state, TS_BUSY);
    737 	zst->zst_tx_busy = 1;
    738 
    739 	/* Enable transmit completion interrupts if necessary. */
    740 	if (!ISSET(cs->cs_preg[1], ZSWR1_TIE)) {
    741 		SET(cs->cs_preg[1], ZSWR1_TIE);
    742 		cs->cs_creg[1] = cs->cs_preg[1];
    743 		zs_write_reg(cs, 1, cs->cs_creg[1]);
    744 	}
    745 
    746 	/* Output the first character of the contiguous buffer. */
    747 	{
    748 		zs_write_data(cs, *zst->zst_tba);
    749 		zst->zst_tbc--;
    750 		zst->zst_tba++;
    751 	}
    752 out:
    753 	splx(s);
    754 	return;
    755 }
    756 
    757 /*
    758  * Stop output, e.g., for ^S or output flush.
    759  */
    760 void
    761 zsstop(tp, flag)
    762 	struct tty *tp;
    763 	int flag;
    764 {
    765 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(tp->t_dev)];
    766 	int s;
    767 
    768 	s = splzs();
    769 	if (ISSET(tp->t_state, TS_BUSY)) {
    770 		/* Stop transmitting at the next chunk. */
    771 		zst->zst_tbc = 0;
    772 		zst->zst_heldtbc = 0;
    773 		if (!ISSET(tp->t_state, TS_TTSTOP))
    774 			SET(tp->t_state, TS_FLUSH);
    775 	}
    776 	splx(s);
    777 }
    778 
    779 /*
    780  * Set ZS tty parameters from termios.
    781  * XXX - Should just copy the whole termios after
    782  * making sure all the changes could be done.
    783  */
    784 static int
    785 zsparam(tp, t)
    786 	struct tty *tp;
    787 	struct termios *t;
    788 {
    789 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(tp->t_dev)];
    790 	struct zs_chanstate *cs = zst->zst_cs;
    791 	int ospeed, cflag;
    792 	u_char tmp3, tmp4, tmp5, tmp15;
    793 	int s, error;
    794 
    795 	ospeed = t->c_ospeed;
    796 	cflag = t->c_cflag;
    797 
    798 	/* Check requested parameters. */
    799 	if (ospeed < 0)
    800 		return (EINVAL);
    801 	if (t->c_ispeed && t->c_ispeed != ospeed)
    802 		return (EINVAL);
    803 
    804 	/*
    805 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    806 	 * is always active.
    807 	 */
    808 	if (ISSET(zst->zst_swflags, TIOCFLAG_SOFTCAR) ||
    809 	    ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
    810 		SET(cflag, CLOCAL);
    811 		CLR(cflag, HUPCL);
    812 	}
    813 
    814 	/*
    815 	 * Only whack the UART when params change.
    816 	 * Some callers need to clear tp->t_ospeed
    817 	 * to make sure initialization gets done.
    818 	 */
    819 	if (tp->t_ospeed == ospeed &&
    820 	    tp->t_cflag == cflag)
    821 		return (0);
    822 
    823 	/*
    824 	 * Call MD functions to deal with changed
    825 	 * clock modes or H/W flow control modes.
    826 	 * The BRG divisor is set now. (reg 12,13)
    827 	 */
    828 	error = zs_set_speed(cs, ospeed);
    829 	if (error)
    830 		return (error);
    831 	error = zs_set_modes(cs, cflag);
    832 	if (error)
    833 		return (error);
    834 
    835 	/*
    836 	 * Block interrupts so that state will not
    837 	 * be altered until we are done setting it up.
    838 	 *
    839 	 * Initial values in cs_preg are set before
    840 	 * our attach routine is called.  The master
    841 	 * interrupt enable is handled by zsc.c
    842 	 *
    843 	 */
    844 	s = splzs();
    845 
    846 	cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
    847 	tmp15 = cs->cs_preg[15];
    848 #if 0
    849 	if (ISSET(cs->cs_rr0_mask, ZSRR0_DCD))
    850 		SET(tmp15, ZSWR15_DCD_IE);
    851 	else
    852 		CLR(tmp15, ZSWR15_DCD_IE);
    853 	if (ISSET(cs->cs_rr0_mask, ZSRR0_CTS))
    854 		SET(tmp15, ZSWR15_CTS_IE);
    855 	else
    856 		CLR(tmp15, ZSWR15_CTS_IE);
    857 #else
    858 	SET(tmp15, ZSWR15_DCD_IE | ZSWR15_CTS_IE);
    859 #endif
    860 	cs->cs_preg[15] = tmp15;
    861 
    862 	/* Recompute character size bits. */
    863 	tmp3 = cs->cs_preg[3];
    864 	tmp5 = cs->cs_preg[5];
    865 	CLR(tmp3, ZSWR3_RXSIZE);
    866 	CLR(tmp5, ZSWR5_TXSIZE);
    867 	switch (ISSET(cflag, CSIZE)) {
    868 	case CS5:
    869 		SET(tmp3, ZSWR3_RX_5);
    870 		SET(tmp5, ZSWR5_TX_5);
    871 		break;
    872 	case CS6:
    873 		SET(tmp3, ZSWR3_RX_6);
    874 		SET(tmp5, ZSWR5_TX_6);
    875 		break;
    876 	case CS7:
    877 		SET(tmp3, ZSWR3_RX_7);
    878 		SET(tmp5, ZSWR5_TX_7);
    879 		break;
    880 	case CS8:
    881 		SET(tmp3, ZSWR3_RX_8);
    882 		SET(tmp5, ZSWR5_TX_8);
    883 		break;
    884 	}
    885 	cs->cs_preg[3] = tmp3;
    886 	cs->cs_preg[5] = tmp5;
    887 
    888 	/*
    889 	 * Recompute the stop bits and parity bits.  Note that
    890 	 * zs_set_speed() may have set clock selection bits etc.
    891 	 * in wr4, so those must preserved.
    892 	 */
    893 	tmp4 = cs->cs_preg[4];
    894 	CLR(tmp4, ZSWR4_SBMASK | ZSWR4_PARMASK);
    895 	if (ISSET(cflag, CSTOPB))
    896 		SET(tmp4, ZSWR4_TWOSB);
    897 	else
    898 		SET(tmp4, ZSWR4_ONESB);
    899 	if (!ISSET(cflag, PARODD))
    900 		SET(tmp4, ZSWR4_EVENP);
    901 	if (ISSET(cflag, PARENB))
    902 		SET(tmp4, ZSWR4_PARENB);
    903 	cs->cs_preg[4] = tmp4;
    904 
    905 	/* And copy to tty. */
    906 	tp->t_ispeed = 0;
    907 	tp->t_ospeed = ospeed;
    908 	tp->t_cflag = cflag;
    909 
    910 	/*
    911 	 * If nothing is being transmitted, set up new current values,
    912 	 * else mark them as pending.
    913 	 */
    914 	if (!cs->cs_heldchange) {
    915 		if (zst->zst_tx_busy) {
    916 			zst->zst_heldtbc = zst->zst_tbc;
    917 			zst->zst_tbc = 0;
    918 			cs->cs_heldchange = 1;
    919 		} else
    920 			zs_loadchannelregs(cs);
    921 	}
    922 
    923 	if (!ISSET(cflag, CHWFLOW)) {
    924 		/* Disable the high water mark. */
    925 		zst->zst_r_hiwat = 0;
    926 		zst->zst_r_lowat = 0;
    927 		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
    928 			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
    929 			zst->zst_rx_ready = 1;
    930 			cs->cs_softreq = 1;
    931 		}
    932 		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
    933 			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
    934 			zs_hwiflow(zst);
    935 		}
    936 	} else {
    937 		zst->zst_r_hiwat = zstty_rbuf_hiwat;
    938 		zst->zst_r_lowat = zstty_rbuf_lowat;
    939 	}
    940 
    941 	splx(s);
    942 
    943 	/*
    944 	 * Update the tty layer's idea of the carrier bit, in case we changed
    945 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
    946 	 * explicit request.
    947 	 */
    948 	(void) (*linesw[tp->t_line].l_modem)(tp, ISSET(cs->cs_rr0, ZSRR0_DCD));
    949 
    950 	if (!ISSET(cflag, CHWFLOW)) {
    951 		if (zst->zst_tx_stopped) {
    952 			zst->zst_tx_stopped = 0;
    953 			zsstart(tp);
    954 		}
    955 	}
    956 
    957 	return (0);
    958 }
    959 
    960 /*
    961  * Raise or lower modem control (DTR/RTS) signals.  If a character is
    962  * in transmission, the change is deferred.
    963  */
    964 static void
    965 zs_modem(zst, onoff)
    966 	struct zstty_softc *zst;
    967 	int onoff;
    968 {
    969 	struct zs_chanstate *cs = zst->zst_cs;
    970 
    971 	if (cs->cs_wr5_dtr == 0)
    972 		return;
    973 
    974 	if (onoff)
    975 		SET(cs->cs_preg[5], cs->cs_wr5_dtr);
    976 	else
    977 		CLR(cs->cs_preg[5], cs->cs_wr5_dtr);
    978 
    979 	if (!cs->cs_heldchange) {
    980 		if (zst->zst_tx_busy) {
    981 			zst->zst_heldtbc = zst->zst_tbc;
    982 			zst->zst_tbc = 0;
    983 			cs->cs_heldchange = 1;
    984 		} else
    985 			zs_loadchannelregs(cs);
    986 	}
    987 }
    988 
    989 /*
    990  * Try to block or unblock input using hardware flow-control.
    991  * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
    992  * if this function returns non-zero, the TS_TBLOCK flag will
    993  * be set or cleared according to the "block" arg passed.
    994  */
    995 int
    996 zshwiflow(tp, block)
    997 	struct tty *tp;
    998 	int block;
    999 {
   1000 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(tp->t_dev)];
   1001 	struct zs_chanstate *cs = zst->zst_cs;
   1002 	int s;
   1003 
   1004 	if (cs->cs_wr5_rts == 0)
   1005 		return (0);
   1006 
   1007 	s = splzs();
   1008 	if (block) {
   1009 		if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
   1010 			SET(zst->zst_rx_flags, RX_TTY_BLOCKED);
   1011 			zs_hwiflow(zst);
   1012 		}
   1013 	} else {
   1014 		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
   1015 			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
   1016 			zst->zst_rx_ready = 1;
   1017 			cs->cs_softreq = 1;
   1018 		}
   1019 		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
   1020 			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED);
   1021 			zs_hwiflow(zst);
   1022 		}
   1023 	}
   1024 	splx(s);
   1025 	return (1);
   1026 }
   1027 
   1028 /*
   1029  * Internal version of zshwiflow
   1030  * called at splzs
   1031  */
   1032 static void
   1033 zs_hwiflow(zst)
   1034 	struct zstty_softc *zst;
   1035 {
   1036 	struct zs_chanstate *cs = zst->zst_cs;
   1037 
   1038 	if (cs->cs_wr5_rts == 0)
   1039 		return;
   1040 
   1041 	if (ISSET(zst->zst_rx_flags, RX_ANY_BLOCK)) {
   1042 		CLR(cs->cs_preg[5], cs->cs_wr5_rts);
   1043 		CLR(cs->cs_creg[5], cs->cs_wr5_rts);
   1044 	} else {
   1045 		SET(cs->cs_preg[5], cs->cs_wr5_rts);
   1046 		SET(cs->cs_creg[5], cs->cs_wr5_rts);
   1047 	}
   1048 	zs_write_reg(cs, 5, cs->cs_creg[5]);
   1049 }
   1050 
   1051 
   1052 /****************************************************************
   1053  * Interface to the lower layer (zscc)
   1054  ****************************************************************/
   1055 
   1056 static void zstty_rxint __P((struct zs_chanstate *));
   1057 static void zstty_txint __P((struct zs_chanstate *));
   1058 static void zstty_stint __P((struct zs_chanstate *));
   1059 
   1060 #define	integrate	static inline
   1061 static void zstty_softint  __P((struct zs_chanstate *));
   1062 integrate void zstty_rxsoft __P((struct zstty_softc *, struct tty *));
   1063 integrate void zstty_txsoft __P((struct zstty_softc *, struct tty *));
   1064 integrate void zstty_stsoft __P((struct zstty_softc *, struct tty *));
   1065 static void zstty_diag __P((void *));
   1066 
   1067 /*
   1068  * receiver ready interrupt.
   1069  * called at splzs
   1070  */
   1071 static void
   1072 zstty_rxint(cs)
   1073 	struct zs_chanstate *cs;
   1074 {
   1075 	struct zstty_softc *zst = cs->cs_private;
   1076 	u_char *put, *end;
   1077 	u_int cc;
   1078 	u_char rr0, rr1, c;
   1079 
   1080 	end = zst->zst_ebuf;
   1081 	put = zst->zst_rbput;
   1082 	cc = zst->zst_rbavail;
   1083 
   1084 	while (cc > 0) {
   1085 		/*
   1086 		 * First read the status, because reading the received char
   1087 		 * destroys the status of this char.
   1088 		 */
   1089 		rr1 = zs_read_reg(cs, 1);
   1090 		c = zs_read_data(cs);
   1091 
   1092 		if (ISSET(rr1, ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
   1093 			/* Clear the receive error. */
   1094 			zs_write_csr(cs, ZSWR0_RESET_ERRORS);
   1095 		}
   1096 
   1097 		put[0] = c;
   1098 		put[1] = rr1;
   1099 		put += 2;
   1100 		if (put >= end)
   1101 			put = zst->zst_rbuf;
   1102 		cc--;
   1103 
   1104 		rr0 = zs_read_csr(cs);
   1105 		if (!ISSET(rr0, ZSRR0_RX_READY))
   1106 			break;
   1107 	}
   1108 
   1109 	/*
   1110 	 * Current string of incoming characters ended because
   1111 	 * no more data was available or we ran out of space.
   1112 	 * Schedule a receive event if any data was received.
   1113 	 * If we're out of space, turn off receive interrupts.
   1114 	 */
   1115 	zst->zst_rbput = put;
   1116 	zst->zst_rbavail = cc;
   1117 	if (!ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
   1118 		zst->zst_rx_ready = 1;
   1119 		cs->cs_softreq = 1;
   1120 	}
   1121 
   1122 	/*
   1123 	 * See if we are in danger of overflowing a buffer. If
   1124 	 * so, use hardware flow control to ease the pressure.
   1125 	 */
   1126 	if (!ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED) &&
   1127 	    cc < zst->zst_r_hiwat) {
   1128 		SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
   1129 		zs_hwiflow(zst);
   1130 	}
   1131 
   1132 	/*
   1133 	 * If we're out of space, disable receive interrupts
   1134 	 * until the queue has drained a bit.
   1135 	 */
   1136 	if (!cc) {
   1137 		SET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
   1138 		CLR(cs->cs_preg[1], ZSWR1_RIE);
   1139 		cs->cs_creg[1] = cs->cs_preg[1];
   1140 		zs_write_reg(cs, 1, cs->cs_creg[1]);
   1141 	}
   1142 
   1143 #if 0
   1144 	printf("%xH%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
   1145 #endif
   1146 }
   1147 
   1148 /*
   1149  * transmitter ready interrupt.  (splzs)
   1150  */
   1151 static void
   1152 zstty_txint(cs)
   1153 	struct zs_chanstate *cs;
   1154 {
   1155 	struct zstty_softc *zst = cs->cs_private;
   1156 
   1157 	/*
   1158 	 * If we've delayed a parameter change, do it now, and restart
   1159 	 * output.
   1160 	 */
   1161 	if (cs->cs_heldchange) {
   1162 		zs_loadchannelregs(cs);
   1163 		cs->cs_heldchange = 0;
   1164 		zst->zst_tbc = zst->zst_heldtbc;
   1165 		zst->zst_heldtbc = 0;
   1166 	}
   1167 
   1168 	/* Output the next character in the buffer, if any. */
   1169 	if (cs->cs_heldchar != 0) {
   1170 		/* An "out-of-band" character is waiting to be output */
   1171 		zs_write_data(cs, cs->cs_heldchar);
   1172 		cs->cs_heldchar = 0;
   1173 	} else if (zst->zst_tbc > 0) {
   1174 		zs_write_data(cs, *zst->zst_tba);
   1175 		zst->zst_tbc--;
   1176 		zst->zst_tba++;
   1177 	} else {
   1178 		/* Disable transmit completion interrupts if necessary. */
   1179 		if (ISSET(cs->cs_preg[1], ZSWR1_TIE)) {
   1180 			CLR(cs->cs_preg[1], ZSWR1_TIE);
   1181 			cs->cs_creg[1] = cs->cs_preg[1];
   1182 			zs_write_reg(cs, 1, cs->cs_creg[1]);
   1183 		}
   1184 		if (zst->zst_tx_busy) {
   1185 			zst->zst_tx_busy = 0;
   1186 			zst->zst_tx_done = 1;
   1187 			cs->cs_softreq = 1;
   1188 		}
   1189 	}
   1190 }
   1191 
   1192 /*
   1193  * status change interrupt.  (splzs)
   1194  */
   1195 static void
   1196 zstty_stint(cs)
   1197 	struct zs_chanstate *cs;
   1198 {
   1199 	struct zstty_softc *zst = cs->cs_private;
   1200 	u_char rr0, delta;
   1201 
   1202 	rr0 = zs_read_csr(cs);
   1203 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
   1204 
   1205 	/*
   1206 	 * Check here for console break, so that we can abort
   1207 	 * even when interrupts are locking up the machine.
   1208 	 */
   1209 	if (ISSET(rr0, ZSRR0_BREAK) &&
   1210 	    ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
   1211 		zs_abort(cs);
   1212 		return;
   1213 	}
   1214 
   1215 	delta = rr0 ^ cs->cs_rr0;
   1216 	cs->cs_rr0 = rr0;
   1217 	if (ISSET(delta, cs->cs_rr0_mask)) {
   1218 		SET(cs->cs_rr0_delta, delta);
   1219 
   1220 		/*
   1221 		 * Stop output immediately if we lose the output
   1222 		 * flow control signal or carrier detect.
   1223 		 */
   1224 		if (ISSET(~rr0, cs->cs_rr0_mask)) {
   1225 			zst->zst_tbc = 0;
   1226 			zst->zst_heldtbc = 0;
   1227 		}
   1228 
   1229 		zst->zst_st_check = 1;
   1230 		cs->cs_softreq = 1;
   1231 	}
   1232 }
   1233 
   1234 void
   1235 zstty_diag(arg)
   1236 	void *arg;
   1237 {
   1238 	struct zstty_softc *zst = arg;
   1239 	int overflows, floods;
   1240 	int s;
   1241 
   1242 	s = splzs();
   1243 	overflows = zst->zst_overflows;
   1244 	zst->zst_overflows = 0;
   1245 	floods = zst->zst_floods;
   1246 	zst->zst_floods = 0;
   1247 	zst->zst_errors = 0;
   1248 	splx(s);
   1249 
   1250 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1251 	    zst->zst_dev.dv_xname,
   1252 	    overflows, overflows == 1 ? "" : "s",
   1253 	    floods, floods == 1 ? "" : "s");
   1254 }
   1255 
   1256 integrate void
   1257 zstty_rxsoft(zst, tp)
   1258 	struct zstty_softc *zst;
   1259 	struct tty *tp;
   1260 {
   1261 	struct zs_chanstate *cs = zst->zst_cs;
   1262 	int (*rint) __P((int c, struct tty *tp)) = linesw[tp->t_line].l_rint;
   1263 	u_char *get, *end;
   1264 	u_int cc, scc;
   1265 	u_char rr1;
   1266 	int code;
   1267 	int s;
   1268 
   1269 	end = zst->zst_ebuf;
   1270 	get = zst->zst_rbget;
   1271 	scc = cc = zstty_rbuf_size - zst->zst_rbavail;
   1272 
   1273 	if (cc == zstty_rbuf_size) {
   1274 		zst->zst_floods++;
   1275 		if (zst->zst_errors++ == 0)
   1276 			timeout(zstty_diag, zst, 60 * hz);
   1277 	}
   1278 
   1279 	while (cc) {
   1280 		code = get[0];
   1281 		rr1 = get[1];
   1282 		if (ISSET(rr1, ZSRR1_DO | ZSRR1_FE | ZSRR1_PE)) {
   1283 			if (ISSET(rr1, ZSRR1_DO)) {
   1284 				zst->zst_overflows++;
   1285 				if (zst->zst_errors++ == 0)
   1286 					timeout(zstty_diag, zst, 60 * hz);
   1287 			}
   1288 			if (ISSET(rr1, ZSRR1_FE))
   1289 				SET(code, TTY_FE);
   1290 			if (ISSET(rr1, ZSRR1_PE))
   1291 				SET(code, TTY_PE);
   1292 		}
   1293 		if ((*rint)(code, tp) == -1) {
   1294 			/*
   1295 			 * The line discipline's buffer is out of space.
   1296 			 */
   1297 			if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
   1298 				/*
   1299 				 * We're either not using flow control, or the
   1300 				 * line discipline didn't tell us to block for
   1301 				 * some reason.  Either way, we have no way to
   1302 				 * know when there's more space available, so
   1303 				 * just drop the rest of the data.
   1304 				 */
   1305 				get += cc << 1;
   1306 				if (get >= end)
   1307 					get -= zstty_rbuf_size << 1;
   1308 				cc = 0;
   1309 			} else {
   1310 				/*
   1311 				 * Don't schedule any more receive processing
   1312 				 * until the line discipline tells us there's
   1313 				 * space available (through comhwiflow()).
   1314 				 * Leave the rest of the data in the input
   1315 				 * buffer.
   1316 				 */
   1317 				SET(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
   1318 			}
   1319 			break;
   1320 		}
   1321 		get += 2;
   1322 		if (get >= end)
   1323 			get = zst->zst_rbuf;
   1324 		cc--;
   1325 	}
   1326 
   1327 	if (cc != scc) {
   1328 		zst->zst_rbget = get;
   1329 		s = splzs();
   1330 		cc = zst->zst_rbavail += scc - cc;
   1331 		/* Buffers should be ok again, release possible block. */
   1332 		if (cc >= zst->zst_r_lowat) {
   1333 			if (ISSET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED)) {
   1334 				CLR(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
   1335 				SET(cs->cs_preg[1], ZSWR1_RIE);
   1336 				cs->cs_creg[1] = cs->cs_preg[1];
   1337 				zs_write_reg(cs, 1, cs->cs_creg[1]);
   1338 			}
   1339 			if (ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED)) {
   1340 				CLR(zst->zst_rx_flags, RX_IBUF_BLOCKED);
   1341 				zs_hwiflow(zst);
   1342 			}
   1343 		}
   1344 		splx(s);
   1345 	}
   1346 
   1347 #if 0
   1348 	printf("%xS%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
   1349 #endif
   1350 }
   1351 
   1352 integrate void
   1353 zstty_txsoft(zst, tp)
   1354 	struct zstty_softc *zst;
   1355 	struct tty *tp;
   1356 {
   1357 
   1358 	CLR(tp->t_state, TS_BUSY);
   1359 	if (ISSET(tp->t_state, TS_FLUSH))
   1360 		CLR(tp->t_state, TS_FLUSH);
   1361 	else
   1362 		ndflush(&tp->t_outq, (int)(zst->zst_tba - tp->t_outq.c_cf));
   1363 	(*linesw[tp->t_line].l_start)(tp);
   1364 }
   1365 
   1366 integrate void
   1367 zstty_stsoft(zst, tp)
   1368 	struct zstty_softc *zst;
   1369 	struct tty *tp;
   1370 {
   1371 	struct zs_chanstate *cs = zst->zst_cs;
   1372 	u_char rr0, delta;
   1373 	int s;
   1374 
   1375 	s = splzs();
   1376 	rr0 = cs->cs_rr0;
   1377 	delta = cs->cs_rr0_delta;
   1378 	cs->cs_rr0_delta = 0;
   1379 	splx(s);
   1380 
   1381 	if (ISSET(delta, cs->cs_rr0_dcd)) {
   1382 		/*
   1383 		 * Inform the tty layer that carrier detect changed.
   1384 		 */
   1385 		(void) (*linesw[tp->t_line].l_modem)(tp, ISSET(rr0, ZSRR0_DCD));
   1386 	}
   1387 
   1388 	if (ISSET(delta, cs->cs_rr0_cts)) {
   1389 		/* Block or unblock output according to flow control. */
   1390 		if (ISSET(rr0, cs->cs_rr0_cts)) {
   1391 			zst->zst_tx_stopped = 0;
   1392 			(*linesw[tp->t_line].l_start)(tp);
   1393 		} else {
   1394 			zst->zst_tx_stopped = 1;
   1395 		}
   1396 	}
   1397 }
   1398 
   1399 /*
   1400  * Software interrupt.  Called at zssoft
   1401  *
   1402  * The main job to be done here is to empty the input ring
   1403  * by passing its contents up to the tty layer.  The ring is
   1404  * always emptied during this operation, therefore the ring
   1405  * must not be larger than the space after "high water" in
   1406  * the tty layer, or the tty layer might drop our input.
   1407  *
   1408  * Note: an "input blockage" condition is assumed to exist if
   1409  * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
   1410  */
   1411 static void
   1412 zstty_softint(cs)
   1413 	struct zs_chanstate *cs;
   1414 {
   1415 	struct zstty_softc *zst = cs->cs_private;
   1416 	struct tty *tp = zst->zst_tty;
   1417 	int s;
   1418 
   1419 	s = spltty();
   1420 
   1421 	if (zst->zst_rx_ready) {
   1422 		zst->zst_rx_ready = 0;
   1423 		zstty_rxsoft(zst, tp);
   1424 	}
   1425 
   1426 	if (zst->zst_st_check) {
   1427 		zst->zst_st_check = 0;
   1428 		zstty_stsoft(zst, tp);
   1429 	}
   1430 
   1431 	if (zst->zst_tx_done) {
   1432 		zst->zst_tx_done = 0;
   1433 		zstty_txsoft(zst, tp);
   1434 	}
   1435 
   1436 	splx(s);
   1437 }
   1438 
   1439 struct zsops zsops_tty = {
   1440 	zstty_rxint,	/* receive char available */
   1441 	zstty_stint,	/* external/status */
   1442 	zstty_txint,	/* xmit buffer empty */
   1443 	zstty_softint,	/* process software interrupt */
   1444 };
   1445