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