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