Home | History | Annotate | Line # | Download | only in ic
z8530tty.c revision 1.50
      1 /*	$NetBSD: z8530tty.c,v 1.50 1998/08/09 18:36:45 wrstuden Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
      5  *	Charles M. Hannum.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Charles M. Hannum.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1994 Gordon W. Ross
     35  * Copyright (c) 1992, 1993
     36  *	The Regents of the University of California.  All rights reserved.
     37  *
     38  * This software was developed by the Computer Systems Engineering group
     39  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     40  * contributed to Berkeley.
     41  *
     42  * All advertising materials mentioning features or use of this software
     43  * must display the following acknowledgement:
     44  *	This product includes software developed by the University of
     45  *	California, Lawrence Berkeley Laboratory.
     46  *
     47  * Redistribution and use in source and binary forms, with or without
     48  * modification, are permitted provided that the following conditions
     49  * are met:
     50  * 1. Redistributions of source code must retain the above copyright
     51  *    notice, this list of conditions and the following disclaimer.
     52  * 2. Redistributions in binary form must reproduce the above copyright
     53  *    notice, this list of conditions and the following disclaimer in the
     54  *    documentation and/or other materials provided with the distribution.
     55  * 3. All advertising materials mentioning features or use of this software
     56  *    must display the following acknowledgement:
     57  *	This product includes software developed by the University of
     58  *	California, Berkeley and its contributors.
     59  * 4. Neither the name of the University nor the names of its contributors
     60  *    may be used to endorse or promote products derived from this software
     61  *    without specific prior written permission.
     62  *
     63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73  * SUCH DAMAGE.
     74  *
     75  *	@(#)zs.c	8.1 (Berkeley) 7/19/93
     76  */
     77 
     78 /*
     79  * Zilog Z8530 Dual UART driver (tty interface)
     80  *
     81  * This is the "slave" driver that will be attached to
     82  * the "zsc" driver for plain "tty" async. serial lines.
     83  *
     84  * Credits, history:
     85  *
     86  * The original version of this code was the sparc/dev/zs.c driver
     87  * as distributed with the Berkeley 4.4 Lite release.  Since then,
     88  * Gordon Ross reorganized the code into the current parent/child
     89  * driver scheme, separating the Sun keyboard and mouse support
     90  * into independent child drivers.
     91  *
     92  * RTS/CTS flow-control support was a collaboration of:
     93  *	Gordon Ross <gwr (at) netbsd.org>,
     94  *	Bill Studenmund <wrstuden (at) loki.stanford.edu>
     95  *	Ian Dall <Ian.Dall (at) dsto.defence.gov.au>
     96  */
     97 
     98 #include <sys/param.h>
     99 #include <sys/systm.h>
    100 #include <sys/proc.h>
    101 #include <sys/device.h>
    102 #include <sys/conf.h>
    103 #include <sys/file.h>
    104 #include <sys/ioctl.h>
    105 #include <sys/malloc.h>
    106 #include <sys/tty.h>
    107 #include <sys/time.h>
    108 #include <sys/kernel.h>
    109 #include <sys/syslog.h>
    110 
    111 #include <dev/ic/z8530reg.h>
    112 #include <machine/z8530var.h>
    113 
    114 #include "locators.h"
    115 
    116 /*
    117  * How many input characters we can buffer.
    118  * The port-specific var.h may override this.
    119  * Note: must be a power of two!
    120  */
    121 #ifndef	ZSTTY_RING_SIZE
    122 #define	ZSTTY_RING_SIZE	2048
    123 #endif
    124 
    125 /*
    126  * Make this an option variable one can patch.
    127  * But be warned:  this must be a power of 2!
    128  */
    129 u_int zstty_rbuf_size = ZSTTY_RING_SIZE;
    130 
    131 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    132 u_int zstty_rbuf_hiwat = (ZSTTY_RING_SIZE * 1) / 4;
    133 u_int zstty_rbuf_lowat = (ZSTTY_RING_SIZE * 3) / 4;
    134 
    135 struct zstty_softc {
    136 	struct	device zst_dev;		/* required first: base device */
    137 	struct  tty *zst_tty;
    138 	struct	zs_chanstate *zst_cs;
    139 
    140 	u_int zst_overflows,
    141 	      zst_floods,
    142 	      zst_errors;
    143 
    144 	int zst_hwflags,	/* see z8530var.h */
    145 	    zst_swflags;	/* TIOCFLAG_SOFTCAR, ... <ttycom.h> */
    146 
    147 	u_int zst_r_hiwat,
    148 	      zst_r_lowat;
    149 	u_char *volatile zst_rbget,
    150 	       *volatile zst_rbput;
    151 	volatile u_int zst_rbavail;
    152 	u_char *zst_rbuf,
    153 	       *zst_ebuf;
    154 
    155 	/*
    156 	 * The transmit byte count and address are used for pseudo-DMA
    157 	 * output in the hardware interrupt code.  PDMA can be suspended
    158 	 * to get pending changes done; heldtbc is used for this.  It can
    159 	 * also be stopped for ^S; this sets TS_TTSTOP in tp->t_state.
    160 	 */
    161 	u_char *zst_tba;		/* transmit buffer address */
    162 	u_int zst_tbc,			/* transmit byte count */
    163 	      zst_heldtbc;		/* held tbc while xmission stopped */
    164 
    165 	/* Flags to communicate with zstty_softint() */
    166 	volatile u_char zst_rx_flags,	/* receiver blocked */
    167 #define	RX_TTY_BLOCKED		0x01
    168 #define	RX_TTY_OVERFLOWED	0x02
    169 #define	RX_IBUF_BLOCKED		0x04
    170 #define	RX_IBUF_OVERFLOWED	0x08
    171 #define	RX_ANY_BLOCK		0x0f
    172 			zst_tx_busy,	/* working on an output chunk */
    173 			zst_tx_done,	/* done with one output chunk */
    174 			zst_tx_stopped,	/* H/W level stop (lost CTS) */
    175 			zst_st_check,	/* got a status interrupt */
    176 			zst_rx_ready;
    177 };
    178 
    179 /* Macros to clear/set/test flags. */
    180 #define SET(t, f)	(t) |= (f)
    181 #define CLR(t, f)	(t) &= ~(f)
    182 #define ISSET(t, f)	((t) & (f))
    183 
    184 /* Definition of the driver for autoconfig. */
    185 #ifdef	__BROKEN_INDIRECT_CONFIG
    186 static int	zstty_match(struct device *, void *, void *);
    187 #else
    188 static int	zstty_match(struct device *, struct cfdata *, void *);
    189 #endif
    190 static void	zstty_attach(struct device *, struct device *, void *);
    191 
    192 struct cfattach zstty_ca = {
    193 	sizeof(struct zstty_softc), zstty_match, zstty_attach
    194 };
    195 
    196 extern struct cfdriver zstty_cd;
    197 
    198 struct zsops zsops_tty;
    199 
    200 /* Routines called from other code. */
    201 cdev_decl(zs);	/* open, close, read, write, ioctl, stop, ... */
    202 
    203 static void zs_shutdown __P((struct zstty_softc *));
    204 static void	zsstart __P((struct tty *));
    205 static int	zsparam __P((struct tty *, struct termios *));
    206 static void zs_modem __P((struct zstty_softc *zst, int onoff));
    207 static int	zshwiflow __P((struct tty *, int));
    208 static void zs_hwiflow __P((struct zstty_softc *));
    209 
    210 #define	ZSUNIT(x)	(minor(x) & 0x7ffff)
    211 #define	ZSDIALOUT(x)	(minor(x) & 0x80000)
    212 
    213 /*
    214  * zstty_match: how is this zs channel configured?
    215  */
    216 #ifdef	__BROKEN_INDIRECT_CONFIG
    217 int
    218 zstty_match(parent, vcf, aux)
    219 	struct device *parent;
    220 	void   *vcf, *aux;
    221 {
    222 	struct cfdata *cf = vcf;
    223 	struct zsc_attach_args *args = aux;
    224 
    225 	/* Exact match is better than wildcard. */
    226 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
    227 		return 2;
    228 
    229 	/* This driver accepts wildcard. */
    230 	if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
    231 		return 1;
    232 
    233 	return 0;
    234 }
    235 #else	/* __BROKEN_INDIRECT_CONFIG */
    236 int
    237 zstty_match(parent, cf, aux)
    238 	struct device *parent;
    239 	struct cfdata *cf;
    240 	void   *aux;
    241 {
    242 	struct zsc_attach_args *args = aux;
    243 
    244 	/* Exact match is better than wildcard. */
    245 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
    246 		return 2;
    247 
    248 	/* This driver accepts wildcard. */
    249 	if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
    250 		return 1;
    251 
    252 	return 0;
    253 }
    254 #endif	/* __BROKEN_INDIRECT_CONFIG */
    255 
    256 void
    257 zstty_attach(parent, self, aux)
    258 	struct device *parent, *self;
    259 	void   *aux;
    260 
    261 {
    262 	struct zsc_softc *zsc = (void *) parent;
    263 	struct zstty_softc *zst = (void *) self;
    264 	struct cfdata *cf = self->dv_cfdata;
    265 	struct zsc_attach_args *args = aux;
    266 	struct zs_chanstate *cs;
    267 	struct tty *tp;
    268 	int channel, s, tty_unit;
    269 	dev_t dev;
    270 
    271 	tty_unit = zst->zst_dev.dv_unit;
    272 	channel = args->channel;
    273 	cs = zsc->zsc_cs[channel];
    274 	cs->cs_private = zst;
    275 	cs->cs_ops = &zsops_tty;
    276 
    277 	zst->zst_cs = cs;
    278 	zst->zst_swflags = cf->cf_flags;	/* softcar, etc. */
    279 	zst->zst_hwflags = args->hwflags;
    280 	dev = makedev(zs_major, tty_unit);
    281 
    282 	if (zst->zst_swflags)
    283 		printf(" flags 0x%x", zst->zst_swflags);
    284 
    285 	if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE))
    286 		printf(" (console)");
    287 	else {
    288 #ifdef KGDB
    289 		/*
    290 		 * Allow kgdb to "take over" this port.  Returns true
    291 		 * if this serial port is in-use by kgdb.
    292 		 */
    293 		if (zs_check_kgdb(cs, dev)) {
    294 			printf(" (kgdb)\n");
    295 			/*
    296 			 * This is the kgdb port (exclusive use)
    297 			 * so skip the normal attach code.
    298 			 */
    299 			return;
    300 		}
    301 #endif
    302 	}
    303 	printf("\n");
    304 
    305 	tp = ttymalloc();
    306 	tp->t_dev = dev;
    307 	tp->t_oproc = zsstart;
    308 	tp->t_param = zsparam;
    309 	tp->t_hwiflow = zshwiflow;
    310 	tty_attach(tp);
    311 
    312 	zst->zst_tty = tp;
    313 	zst->zst_rbuf = malloc(zstty_rbuf_size << 1, M_DEVBUF, M_WAITOK);
    314 	zst->zst_ebuf = zst->zst_rbuf + (zstty_rbuf_size << 1);
    315 	/* Disable the high water mark. */
    316 	zst->zst_r_hiwat = 0;
    317 	zst->zst_r_lowat = 0;
    318 	zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
    319 	zst->zst_rbavail = zstty_rbuf_size;
    320 
    321 	/* XXX - Do we need an MD hook here? */
    322 
    323 	/*
    324 	 * Hardware init
    325 	 */
    326 	if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
    327 		/* Call zsparam similar to open. */
    328 		struct termios t;
    329 
    330 		s = splzs();
    331 
    332 		/* Turn on interrupts. */
    333 		cs->cs_creg[1] = cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    334 		zs_write_reg(cs, 1, cs->cs_creg[1]);
    335 
    336 		/* Fetch the current modem control status, needed later. */
    337 		cs->cs_rr0 = zs_read_csr(cs);
    338 
    339 		splx(s);
    340 
    341 		/* Setup the "new" parameters in t. */
    342 		t.c_ispeed = 0;
    343 		t.c_ospeed = cs->cs_defspeed;
    344 		t.c_cflag = cs->cs_defcflag;
    345 		/* Make sure zsparam will see changes. */
    346 		tp->t_ospeed = 0;
    347 		(void) zsparam(tp, &t);
    348 
    349 		s = splzs();
    350 
    351 		/* Make sure DTR is on now. */
    352 		zs_modem(zst, 1);
    353 
    354 		splx(s);
    355 	} else {
    356 		/* Not the console; may need reset. */
    357 		int reset;
    358 
    359 		reset = (channel == 0) ? ZSWR9_A_RESET : ZSWR9_B_RESET;
    360 
    361 		s = splzs();
    362 
    363 		zs_write_reg(cs, 9, reset);
    364 
    365 		/* Will raise DTR in open. */
    366 		zs_modem(zst, 0);
    367 
    368 		splx(s);
    369 	}
    370 }
    371 
    372 
    373 /*
    374  * Return pointer to our tty.
    375  */
    376 struct tty *
    377 zstty(dev)
    378 	dev_t dev;
    379 {
    380 	struct zstty_softc *zst;
    381 	int unit = ZSUNIT(dev);
    382 
    383 #ifdef	DIAGNOSTIC
    384 	if (unit >= zstty_cd.cd_ndevs)
    385 		panic("zstty");
    386 #endif
    387 	zst = zstty_cd.cd_devs[unit];
    388 	return (zst->zst_tty);
    389 }
    390 
    391 
    392 void
    393 zs_shutdown(zst)
    394 	struct zstty_softc *zst;
    395 {
    396 	struct zs_chanstate *cs = zst->zst_cs;
    397 	struct tty *tp = zst->zst_tty;
    398 	int s;
    399 
    400 	s = splzs();
    401 
    402 	/* If we were asserting flow control, then deassert it. */
    403 	SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
    404 	zs_hwiflow(zst);
    405 
    406 	/* Clear any break condition set with TIOCSBRK. */
    407 	zs_break(cs, 0);
    408 
    409 	/*
    410 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    411 	 * notice even if we immediately open the port again.
    412 	 */
    413 	if (ISSET(tp->t_cflag, HUPCL)) {
    414 		zs_modem(zst, 0);
    415 		(void) tsleep(cs, TTIPRI, ttclos, hz);
    416 	}
    417 
    418 	/* Turn off interrupts if not the console. */
    419 	if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE))
    420 		cs->cs_creg[1] = cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    421 	else
    422 		cs->cs_creg[1] = cs->cs_preg[1] = 0;
    423 	zs_write_reg(cs, 1, cs->cs_creg[1]);
    424 
    425 	splx(s);
    426 }
    427 
    428 /*
    429  * Open a zs serial (tty) port.
    430  */
    431 int
    432 zsopen(dev, flags, mode, p)
    433 	dev_t dev;
    434 	int flags;
    435 	int mode;
    436 	struct proc *p;
    437 {
    438 	int unit = ZSUNIT(dev);
    439 	struct zstty_softc *zst;
    440 	struct zs_chanstate *cs;
    441 	struct tty *tp;
    442 	int s, s2;
    443 	int error;
    444 
    445 	if (unit >= zstty_cd.cd_ndevs)
    446 		return (ENXIO);
    447 	zst = zstty_cd.cd_devs[unit];
    448 	if (zst == 0)
    449 		return (ENXIO);
    450 	tp = zst->zst_tty;
    451 	cs = zst->zst_cs;
    452 
    453 	/* If KGDB took the line, then tp==NULL */
    454 	if (tp == NULL)
    455 		return (EBUSY);
    456 
    457 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    458 	    ISSET(tp->t_state, TS_XCLUDE) &&
    459 	    p->p_ucred->cr_uid != 0)
    460 		return (EBUSY);
    461 
    462 	s = spltty();
    463 
    464 	/*
    465 	 * Do the following iff this is a first open.
    466 	 */
    467 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    468 		struct termios t;
    469 
    470 		tp->t_dev = dev;
    471 
    472 		s2 = splzs();
    473 
    474 		/* Turn on interrupts. */
    475 		cs->cs_creg[1] = cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    476 		zs_write_reg(cs, 1, cs->cs_creg[1]);
    477 
    478 		/* Fetch the current modem control status, needed later. */
    479 		cs->cs_rr0 = zs_read_csr(cs);
    480 
    481 		splx(s2);
    482 
    483 		/*
    484 		 * Initialize the termios status to the defaults.  Add in the
    485 		 * sticky bits from TIOCSFLAGS.
    486 		 */
    487 		t.c_ispeed = 0;
    488 		t.c_ospeed = cs->cs_defspeed;
    489 		t.c_cflag = cs->cs_defcflag;
    490 		if (ISSET(zst->zst_swflags, TIOCFLAG_CLOCAL))
    491 			SET(t.c_cflag, CLOCAL);
    492 		if (ISSET(zst->zst_swflags, TIOCFLAG_CRTSCTS))
    493 			SET(t.c_cflag, CRTSCTS);
    494 		if (ISSET(zst->zst_swflags, TIOCFLAG_CDTRCTS))
    495 			SET(t.c_cflag, CDTRCTS);
    496 		if (ISSET(zst->zst_swflags, TIOCFLAG_MDMBUF))
    497 			SET(t.c_cflag, MDMBUF);
    498 		/* Make sure zsparam will see changes. */
    499 		tp->t_ospeed = 0;
    500 		(void) zsparam(tp, &t);
    501 		/*
    502 		 * Note: zsparam has done: cflag, ispeed, ospeed
    503 		 * so we just need to do: iflag, oflag, lflag, cc
    504 		 * For "raw" mode, just leave all zeros.
    505 		 */
    506 		if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_RAW)) {
    507 			tp->t_iflag = TTYDEF_IFLAG;
    508 			tp->t_oflag = TTYDEF_OFLAG;
    509 			tp->t_lflag = TTYDEF_LFLAG;
    510 		} else {
    511 			tp->t_iflag = 0;
    512 			tp->t_oflag = 0;
    513 			tp->t_lflag = 0;
    514 		}
    515 		ttychars(tp);
    516 		ttsetwater(tp);
    517 
    518 		s2 = splzs();
    519 
    520 		/*
    521 		 * Turn on DTR.  We must always do this, even if carrier is not
    522 		 * present, because otherwise we'd have to use TIOCSDTR
    523 		 * immediately after setting CLOCAL, which applications do not
    524 		 * expect.  We always assert DTR while the device is open
    525 		 * unless explicitly requested to deassert it.
    526 		 */
    527 		zs_modem(zst, 1);
    528 
    529 		/* Clear the input ring, and unblock. */
    530 		zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
    531 		zst->zst_rbavail = zstty_rbuf_size;
    532 		zs_iflush(cs);
    533 		CLR(zst->zst_rx_flags, RX_ANY_BLOCK);
    534 		zs_hwiflow(zst);
    535 
    536 		splx(s2);
    537 	}
    538 
    539 	splx(s);
    540 
    541 	error = ttyopen(tp, ZSDIALOUT(dev), ISSET(flags, O_NONBLOCK));
    542 	if (error)
    543 		goto bad;
    544 
    545 	error = (*linesw[tp->t_line].l_open)(dev, tp);
    546 	if (error)
    547 		goto bad;
    548 
    549 	return (0);
    550 
    551 bad:
    552 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    553 		/*
    554 		 * We failed to open the device, and nobody else had it opened.
    555 		 * Clean up the state as appropriate.
    556 		 */
    557 		zs_shutdown(zst);
    558 	}
    559 
    560 	return (error);
    561 }
    562 
    563 /*
    564  * Close a zs serial port.
    565  */
    566 int
    567 zsclose(dev, flags, mode, p)
    568 	dev_t dev;
    569 	int flags;
    570 	int mode;
    571 	struct proc *p;
    572 {
    573 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(dev)];
    574 	struct tty *tp = zst->zst_tty;
    575 
    576 	/* XXX This is for cons.c. */
    577 	if (!ISSET(tp->t_state, TS_ISOPEN))
    578 		return 0;
    579 
    580 	(*linesw[tp->t_line].l_close)(tp, flags);
    581 	ttyclose(tp);
    582 
    583 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    584 		/*
    585 		 * Although we got a last close, the device may still be in
    586 		 * use; e.g. if this was the dialout node, and there are still
    587 		 * processes waiting for carrier on the non-dialout node.
    588 		 */
    589 		zs_shutdown(zst);
    590 	}
    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[ZSUNIT(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[ZSUNIT(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[ZSUNIT(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[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 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[ZSUNIT(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 (zst->zst_tbc > 0) {
   1171 		zs_write_data(cs, *zst->zst_tba);
   1172 		zst->zst_tbc--;
   1173 		zst->zst_tba++;
   1174 	} else {
   1175 		/* Disable transmit completion interrupts if necessary. */
   1176 		if (ISSET(cs->cs_preg[1], ZSWR1_TIE)) {
   1177 			CLR(cs->cs_preg[1], ZSWR1_TIE);
   1178 			cs->cs_creg[1] = cs->cs_preg[1];
   1179 			zs_write_reg(cs, 1, cs->cs_creg[1]);
   1180 		}
   1181 		if (zst->zst_tx_busy) {
   1182 			zst->zst_tx_busy = 0;
   1183 			zst->zst_tx_done = 1;
   1184 			cs->cs_softreq = 1;
   1185 		}
   1186 	}
   1187 }
   1188 
   1189 /*
   1190  * status change interrupt.  (splzs)
   1191  */
   1192 static void
   1193 zstty_stint(cs)
   1194 	struct zs_chanstate *cs;
   1195 {
   1196 	struct zstty_softc *zst = cs->cs_private;
   1197 	u_char rr0, delta;
   1198 
   1199 	rr0 = zs_read_csr(cs);
   1200 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
   1201 
   1202 	/*
   1203 	 * Check here for console break, so that we can abort
   1204 	 * even when interrupts are locking up the machine.
   1205 	 */
   1206 	if (ISSET(rr0, ZSRR0_BREAK) &&
   1207 	    ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
   1208 		zs_abort(cs);
   1209 		return;
   1210 	}
   1211 
   1212 	delta = rr0 ^ cs->cs_rr0;
   1213 	cs->cs_rr0 = rr0;
   1214 	if (ISSET(delta, cs->cs_rr0_mask)) {
   1215 		SET(cs->cs_rr0_delta, delta);
   1216 
   1217 		/*
   1218 		 * Stop output immediately if we lose the output
   1219 		 * flow control signal or carrier detect.
   1220 		 */
   1221 		if (ISSET(~rr0, cs->cs_rr0_mask)) {
   1222 			zst->zst_tbc = 0;
   1223 			zst->zst_heldtbc = 0;
   1224 		}
   1225 
   1226 		zst->zst_st_check = 1;
   1227 		cs->cs_softreq = 1;
   1228 	}
   1229 }
   1230 
   1231 void
   1232 zstty_diag(arg)
   1233 	void *arg;
   1234 {
   1235 	struct zstty_softc *zst = arg;
   1236 	int overflows, floods;
   1237 	int s;
   1238 
   1239 	s = splzs();
   1240 	overflows = zst->zst_overflows;
   1241 	zst->zst_overflows = 0;
   1242 	floods = zst->zst_floods;
   1243 	zst->zst_floods = 0;
   1244 	zst->zst_errors = 0;
   1245 	splx(s);
   1246 
   1247 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1248 	    zst->zst_dev.dv_xname,
   1249 	    overflows, overflows == 1 ? "" : "s",
   1250 	    floods, floods == 1 ? "" : "s");
   1251 }
   1252 
   1253 integrate void
   1254 zstty_rxsoft(zst, tp)
   1255 	struct zstty_softc *zst;
   1256 	struct tty *tp;
   1257 {
   1258 	struct zs_chanstate *cs = zst->zst_cs;
   1259 	int (*rint) __P((int c, struct tty *tp)) = linesw[tp->t_line].l_rint;
   1260 	u_char *get, *end;
   1261 	u_int cc, scc;
   1262 	u_char rr1;
   1263 	int code;
   1264 	int s;
   1265 
   1266 	end = zst->zst_ebuf;
   1267 	get = zst->zst_rbget;
   1268 	scc = cc = zstty_rbuf_size - zst->zst_rbavail;
   1269 
   1270 	if (cc == zstty_rbuf_size) {
   1271 		zst->zst_floods++;
   1272 		if (zst->zst_errors++ == 0)
   1273 			timeout(zstty_diag, zst, 60 * hz);
   1274 	}
   1275 
   1276 	while (cc) {
   1277 		code = get[0];
   1278 		rr1 = get[1];
   1279 		if (ISSET(rr1, ZSRR1_DO | ZSRR1_FE | ZSRR1_PE)) {
   1280 			if (ISSET(rr1, ZSRR1_DO)) {
   1281 				zst->zst_overflows++;
   1282 				if (zst->zst_errors++ == 0)
   1283 					timeout(zstty_diag, zst, 60 * hz);
   1284 			}
   1285 			if (ISSET(rr1, ZSRR1_FE))
   1286 				SET(code, TTY_FE);
   1287 			if (ISSET(rr1, ZSRR1_PE))
   1288 				SET(code, TTY_PE);
   1289 		}
   1290 		if ((*rint)(code, tp) == -1) {
   1291 			/*
   1292 			 * The line discipline's buffer is out of space.
   1293 			 */
   1294 			if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
   1295 				/*
   1296 				 * We're either not using flow control, or the
   1297 				 * line discipline didn't tell us to block for
   1298 				 * some reason.  Either way, we have no way to
   1299 				 * know when there's more space available, so
   1300 				 * just drop the rest of the data.
   1301 				 */
   1302 				get += cc << 1;
   1303 				if (get >= end)
   1304 					get -= zstty_rbuf_size << 1;
   1305 				cc = 0;
   1306 			} else {
   1307 				/*
   1308 				 * Don't schedule any more receive processing
   1309 				 * until the line discipline tells us there's
   1310 				 * space available (through comhwiflow()).
   1311 				 * Leave the rest of the data in the input
   1312 				 * buffer.
   1313 				 */
   1314 				SET(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
   1315 			}
   1316 			break;
   1317 		}
   1318 		get += 2;
   1319 		if (get >= end)
   1320 			get = zst->zst_rbuf;
   1321 		cc--;
   1322 	}
   1323 
   1324 	if (cc != scc) {
   1325 		zst->zst_rbget = get;
   1326 		s = splzs();
   1327 		cc = zst->zst_rbavail += scc - cc;
   1328 		/* Buffers should be ok again, release possible block. */
   1329 		if (cc >= zst->zst_r_lowat) {
   1330 			if (ISSET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED)) {
   1331 				CLR(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
   1332 				SET(cs->cs_preg[1], ZSWR1_RIE);
   1333 				cs->cs_creg[1] = cs->cs_preg[1];
   1334 				zs_write_reg(cs, 1, cs->cs_creg[1]);
   1335 			}
   1336 			if (ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED)) {
   1337 				CLR(zst->zst_rx_flags, RX_IBUF_BLOCKED);
   1338 				zs_hwiflow(zst);
   1339 			}
   1340 		}
   1341 		splx(s);
   1342 	}
   1343 
   1344 #if 0
   1345 	printf("%xS%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
   1346 #endif
   1347 }
   1348 
   1349 integrate void
   1350 zstty_txsoft(zst, tp)
   1351 	struct zstty_softc *zst;
   1352 	struct tty *tp;
   1353 {
   1354 
   1355 	CLR(tp->t_state, TS_BUSY);
   1356 	if (ISSET(tp->t_state, TS_FLUSH))
   1357 		CLR(tp->t_state, TS_FLUSH);
   1358 	else
   1359 		ndflush(&tp->t_outq, (int)(zst->zst_tba - tp->t_outq.c_cf));
   1360 	(*linesw[tp->t_line].l_start)(tp);
   1361 }
   1362 
   1363 integrate void
   1364 zstty_stsoft(zst, tp)
   1365 	struct zstty_softc *zst;
   1366 	struct tty *tp;
   1367 {
   1368 	struct zs_chanstate *cs = zst->zst_cs;
   1369 	u_char rr0, delta;
   1370 	int s;
   1371 
   1372 	s = splzs();
   1373 	rr0 = cs->cs_rr0;
   1374 	delta = cs->cs_rr0_delta;
   1375 	cs->cs_rr0_delta = 0;
   1376 	splx(s);
   1377 
   1378 	if (ISSET(delta, cs->cs_rr0_dcd)) {
   1379 		/*
   1380 		 * Inform the tty layer that carrier detect changed.
   1381 		 */
   1382 		(void) (*linesw[tp->t_line].l_modem)(tp, ISSET(rr0, ZSRR0_DCD));
   1383 	}
   1384 
   1385 	if (ISSET(delta, cs->cs_rr0_cts)) {
   1386 		/* Block or unblock output according to flow control. */
   1387 		if (ISSET(rr0, cs->cs_rr0_cts)) {
   1388 			zst->zst_tx_stopped = 0;
   1389 			(*linesw[tp->t_line].l_start)(tp);
   1390 		} else {
   1391 			zst->zst_tx_stopped = 1;
   1392 		}
   1393 	}
   1394 }
   1395 
   1396 /*
   1397  * Software interrupt.  Called at zssoft
   1398  *
   1399  * The main job to be done here is to empty the input ring
   1400  * by passing its contents up to the tty layer.  The ring is
   1401  * always emptied during this operation, therefore the ring
   1402  * must not be larger than the space after "high water" in
   1403  * the tty layer, or the tty layer might drop our input.
   1404  *
   1405  * Note: an "input blockage" condition is assumed to exist if
   1406  * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
   1407  */
   1408 static void
   1409 zstty_softint(cs)
   1410 	struct zs_chanstate *cs;
   1411 {
   1412 	struct zstty_softc *zst = cs->cs_private;
   1413 	struct tty *tp = zst->zst_tty;
   1414 	int s;
   1415 
   1416 	s = spltty();
   1417 
   1418 	if (zst->zst_rx_ready) {
   1419 		zst->zst_rx_ready = 0;
   1420 		zstty_rxsoft(zst, tp);
   1421 	}
   1422 
   1423 	if (zst->zst_st_check) {
   1424 		zst->zst_st_check = 0;
   1425 		zstty_stsoft(zst, tp);
   1426 	}
   1427 
   1428 	if (zst->zst_tx_done) {
   1429 		zst->zst_tx_done = 0;
   1430 		zstty_txsoft(zst, tp);
   1431 	}
   1432 
   1433 	splx(s);
   1434 }
   1435 
   1436 struct zsops zsops_tty = {
   1437 	zstty_rxint,	/* receive char available */
   1438 	zstty_stint,	/* external/status */
   1439 	zstty_txint,	/* xmit buffer empty */
   1440 	zstty_softint,	/* process software interrupt */
   1441 };
   1442