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