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