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