Home | History | Annotate | Line # | Download | only in ic
z8530tty.c revision 1.112
      1 /*	$NetBSD: z8530tty.c,v 1.112 2006/10/01 20:31:50 elad 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) 1992, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * This software was developed by the Computer Systems Engineering group
     38  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     39  * contributed to Berkeley.
     40  *
     41  * All advertising materials mentioning features or use of this software
     42  * must display the following acknowledgement:
     43  *	This product includes software developed by the University of
     44  *	California, Lawrence Berkeley Laboratory.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)zs.c	8.1 (Berkeley) 7/19/93
     71  */
     72 
     73 /*
     74  * Copyright (c) 1994 Gordon W. Ross
     75  *
     76  * This software was developed by the Computer Systems Engineering group
     77  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     78  * contributed to Berkeley.
     79  *
     80  * All advertising materials mentioning features or use of this software
     81  * must display the following acknowledgement:
     82  *	This product includes software developed by the University of
     83  *	California, Lawrence Berkeley Laboratory.
     84  *
     85  * Redistribution and use in source and binary forms, with or without
     86  * modification, are permitted provided that the following conditions
     87  * are met:
     88  * 1. Redistributions of source code must retain the above copyright
     89  *    notice, this list of conditions and the following disclaimer.
     90  * 2. Redistributions in binary form must reproduce the above copyright
     91  *    notice, this list of conditions and the following disclaimer in the
     92  *    documentation and/or other materials provided with the distribution.
     93  * 3. All advertising materials mentioning features or use of this software
     94  *    must display the following acknowledgement:
     95  *	This product includes software developed by the University of
     96  *	California, Berkeley and its contributors.
     97  * 4. Neither the name of the University nor the names of its contributors
     98  *    may be used to endorse or promote products derived from this software
     99  *    without specific prior written permission.
    100  *
    101  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    102  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    103  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    104  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    105  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    106  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    107  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    108  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    109  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    110  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    111  * SUCH DAMAGE.
    112  *
    113  *	@(#)zs.c	8.1 (Berkeley) 7/19/93
    114  */
    115 
    116 /*
    117  * Zilog Z8530 Dual UART driver (tty interface)
    118  *
    119  * This is the "slave" driver that will be attached to
    120  * the "zsc" driver for plain "tty" async. serial lines.
    121  *
    122  * Credits, history:
    123  *
    124  * The original version of this code was the sparc/dev/zs.c driver
    125  * as distributed with the Berkeley 4.4 Lite release.  Since then,
    126  * Gordon Ross reorganized the code into the current parent/child
    127  * driver scheme, separating the Sun keyboard and mouse support
    128  * into independent child drivers.
    129  *
    130  * RTS/CTS flow-control support was a collaboration of:
    131  *	Gordon Ross <gwr (at) NetBSD.org>,
    132  *	Bill Studenmund <wrstuden (at) loki.stanford.edu>
    133  *	Ian Dall <Ian.Dall (at) dsto.defence.gov.au>
    134  *
    135  * The driver was massively overhauled in November 1997 by Charles Hannum,
    136  * fixing *many* bugs, and substantially improving performance.
    137  */
    138 
    139 #include <sys/cdefs.h>
    140 __KERNEL_RCSID(0, "$NetBSD: z8530tty.c,v 1.112 2006/10/01 20:31:50 elad Exp $");
    141 
    142 #include "opt_kgdb.h"
    143 #include "opt_ntp.h"
    144 
    145 #include <sys/param.h>
    146 #include <sys/systm.h>
    147 #include <sys/proc.h>
    148 #include <sys/device.h>
    149 #include <sys/conf.h>
    150 #include <sys/file.h>
    151 #include <sys/ioctl.h>
    152 #include <sys/malloc.h>
    153 #include <sys/timepps.h>
    154 #include <sys/tty.h>
    155 #include <sys/time.h>
    156 #include <sys/kernel.h>
    157 #include <sys/syslog.h>
    158 #include <sys/kauth.h>
    159 
    160 #include <dev/ic/z8530reg.h>
    161 #include <machine/z8530var.h>
    162 
    163 #include <dev/cons.h>
    164 
    165 #include "locators.h"
    166 
    167 /*
    168  * How many input characters we can buffer.
    169  * The port-specific var.h may override this.
    170  * Note: must be a power of two!
    171  */
    172 #ifndef	ZSTTY_RING_SIZE
    173 #define	ZSTTY_RING_SIZE	2048
    174 #endif
    175 
    176 static struct cnm_state zstty_cnm_state;
    177 /*
    178  * Make this an option variable one can patch.
    179  * But be warned:  this must be a power of 2!
    180  */
    181 u_int zstty_rbuf_size = ZSTTY_RING_SIZE;
    182 
    183 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    184 u_int zstty_rbuf_hiwat = (ZSTTY_RING_SIZE * 1) / 4;
    185 u_int zstty_rbuf_lowat = (ZSTTY_RING_SIZE * 3) / 4;
    186 
    187 #ifndef __HAVE_TIMECOUNTER
    188 static int zsppscap =
    189 	PPS_TSFMT_TSPEC |
    190 	PPS_CAPTUREASSERT |
    191 	PPS_CAPTURECLEAR |
    192 	PPS_OFFSETASSERT | PPS_OFFSETCLEAR;
    193 #endif /* __HAVE_TIMECOUNTER */
    194 
    195 struct zstty_softc {
    196 	struct	device zst_dev;		/* required first: base device */
    197 	struct  tty *zst_tty;
    198 	struct	zs_chanstate *zst_cs;
    199 
    200 	struct callout zst_diag_ch;
    201 
    202 	u_int zst_overflows,
    203 	      zst_floods,
    204 	      zst_errors;
    205 
    206 	int zst_hwflags,	/* see z8530var.h */
    207 	    zst_swflags;	/* TIOCFLAG_SOFTCAR, ... <ttycom.h> */
    208 
    209 	u_int zst_r_hiwat,
    210 	      zst_r_lowat;
    211 	u_char *volatile zst_rbget,
    212 	       *volatile zst_rbput;
    213 	volatile u_int zst_rbavail;
    214 	u_char *zst_rbuf,
    215 	       *zst_ebuf;
    216 
    217 	/*
    218 	 * The transmit byte count and address are used for pseudo-DMA
    219 	 * output in the hardware interrupt code.  PDMA can be suspended
    220 	 * to get pending changes done; heldtbc is used for this.  It can
    221 	 * also be stopped for ^S; this sets TS_TTSTOP in tp->t_state.
    222 	 */
    223 	u_char *zst_tba;		/* transmit buffer address */
    224 	u_int zst_tbc,			/* transmit byte count */
    225 	      zst_heldtbc;		/* held tbc while xmission stopped */
    226 
    227 	/* Flags to communicate with zstty_softint() */
    228 	volatile u_char zst_rx_flags,	/* receiver blocked */
    229 #define	RX_TTY_BLOCKED		0x01
    230 #define	RX_TTY_OVERFLOWED	0x02
    231 #define	RX_IBUF_BLOCKED		0x04
    232 #define	RX_IBUF_OVERFLOWED	0x08
    233 #define	RX_ANY_BLOCK		0x0f
    234 			zst_tx_busy,	/* working on an output chunk */
    235 			zst_tx_done,	/* done with one output chunk */
    236 			zst_tx_stopped,	/* H/W level stop (lost CTS) */
    237 			zst_st_check,	/* got a status interrupt */
    238 			zst_rx_ready;
    239 
    240 	/* PPS signal on DCD, with or without inkernel clock disciplining */
    241 	u_char  zst_ppsmask;			/* pps signal mask */
    242 #ifdef __HAVE_TIMECOUNTER
    243 	struct pps_state zst_pps_state;
    244 #else /* !__HAVE_TIMECOUNTER */
    245 	u_char  zst_ppsassert;			/* pps leading edge */
    246 	u_char  zst_ppsclear;			/* pps trailing edge */
    247 	pps_info_t ppsinfo;
    248 	pps_params_t ppsparam;
    249 #endif /* !__HAVE_TIMECOUNTER */
    250 };
    251 
    252 /* Definition of the driver for autoconfig. */
    253 static int	zstty_match(struct device *, struct cfdata *, void *);
    254 static void	zstty_attach(struct device *, struct device *, void *);
    255 
    256 CFATTACH_DECL(zstty, sizeof(struct zstty_softc),
    257     zstty_match, zstty_attach, NULL, NULL);
    258 
    259 extern struct cfdriver zstty_cd;
    260 
    261 dev_type_open(zsopen);
    262 dev_type_close(zsclose);
    263 dev_type_read(zsread);
    264 dev_type_write(zswrite);
    265 dev_type_ioctl(zsioctl);
    266 dev_type_stop(zsstop);
    267 dev_type_tty(zstty);
    268 dev_type_poll(zspoll);
    269 
    270 const struct cdevsw zstty_cdevsw = {
    271 	zsopen, zsclose, zsread, zswrite, zsioctl,
    272 	zsstop, zstty, zspoll, nommap, ttykqfilter, D_TTY
    273 };
    274 
    275 struct zsops zsops_tty;
    276 
    277 static void zs_shutdown(struct zstty_softc *);
    278 static void	zsstart(struct tty *);
    279 static int	zsparam(struct tty *, struct termios *);
    280 static void zs_modem(struct zstty_softc *, int);
    281 static void tiocm_to_zs(struct zstty_softc *, u_long, int);
    282 static int  zs_to_tiocm(struct zstty_softc *);
    283 static int    zshwiflow(struct tty *, int);
    284 static void  zs_hwiflow(struct zstty_softc *);
    285 static void zs_maskintr(struct zstty_softc *);
    286 
    287 /* Low-level routines. */
    288 static void zstty_rxint  (struct zs_chanstate *);
    289 static void zstty_stint  (struct zs_chanstate *, int);
    290 static void zstty_txint  (struct zs_chanstate *);
    291 static void zstty_softint(struct zs_chanstate *);
    292 
    293 #define	ZSUNIT(x)	(minor(x) & 0x7ffff)
    294 #define	ZSDIALOUT(x)	(minor(x) & 0x80000)
    295 
    296 struct tty *zstty_get_tty_from_dev(struct device *);
    297 
    298 /*
    299  * XXX get the (struct tty *) out of a (struct device *) we trust to be a
    300  * (struct zstty_softc *) - needed by sparc/dev/zs.c, sparc64/dev/zs.c,
    301  * sun3/dev/zs.c and sun2/dev/zs.c will probably need it at some point
    302  */
    303 
    304 struct tty *
    305 zstty_get_tty_from_dev(struct device *dev)
    306 {
    307 	struct zstty_softc *sc = (struct zstty_softc *)dev;
    308 
    309 	return sc->zst_tty;
    310 }
    311 
    312 /*
    313  * zstty_match: how is this zs channel configured?
    314  */
    315 int
    316 zstty_match(parent, cf, aux)
    317 	struct device *parent;
    318 	struct cfdata *cf;
    319 	void   *aux;
    320 {
    321 	struct zsc_attach_args *args = aux;
    322 
    323 	/* Exact match is better than wildcard. */
    324 	if (cf->zsccf_channel == args->channel)
    325 		return 2;
    326 
    327 	/* This driver accepts wildcard. */
    328 	if (cf->zsccf_channel == ZSCCF_CHANNEL_DEFAULT)
    329 		return 1;
    330 
    331 	return 0;
    332 }
    333 
    334 void
    335 zstty_attach(parent, self, aux)
    336 	struct device *parent, *self;
    337 	void   *aux;
    338 
    339 {
    340 	struct zsc_softc *zsc = (void *) parent;
    341 	struct zstty_softc *zst = (void *) self;
    342 	struct cfdata *cf = device_cfdata(self);
    343 	struct zsc_attach_args *args = aux;
    344 	struct zs_chanstate *cs;
    345 	struct tty *tp;
    346 	int channel, s, tty_unit;
    347 	dev_t dev;
    348 	const char *i, *o;
    349 	int dtr_on;
    350 	int resetbit;
    351 
    352 	callout_init(&zst->zst_diag_ch);
    353 	cn_init_magic(&zstty_cnm_state);
    354 
    355 	tty_unit = device_unit(&zst->zst_dev);
    356 	channel = args->channel;
    357 	cs = zsc->zsc_cs[channel];
    358 	cs->cs_private = zst;
    359 	cs->cs_ops = &zsops_tty;
    360 
    361 	zst->zst_cs = cs;
    362 	zst->zst_swflags = cf->cf_flags;	/* softcar, etc. */
    363 	zst->zst_hwflags = args->hwflags;
    364 	dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), tty_unit);
    365 
    366 	if (zst->zst_swflags)
    367 		printf(" flags 0x%x", zst->zst_swflags);
    368 
    369 	/*
    370 	 * Check whether we serve as a console device.
    371 	 * XXX - split console input/output channels aren't
    372 	 *	 supported yet on /dev/console
    373 	 */
    374 	i = o = NULL;
    375 	if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
    376 		i = "input";
    377 		if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
    378 			args->consdev->cn_dev = dev;
    379 			cn_tab->cn_pollc = args->consdev->cn_pollc;
    380 			cn_tab->cn_getc = args->consdev->cn_getc;
    381 		}
    382 		cn_tab->cn_dev = dev;
    383 		/* Set console magic to BREAK */
    384 		cn_set_magic("\047\001");
    385 	}
    386 	if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
    387 		o = "output";
    388 		if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
    389 			cn_tab->cn_putc = args->consdev->cn_putc;
    390 		}
    391 		cn_tab->cn_dev = dev;
    392 	}
    393 	if (i != NULL || o != NULL)
    394 		printf(" (console %s)", i ? (o ? "i/o" : i) : o);
    395 
    396 #ifdef KGDB
    397 	if (zs_check_kgdb(cs, dev)) {
    398 		/*
    399 		 * Allow kgdb to "take over" this port.  Returns true
    400 		 * if this serial port is in-use by kgdb.
    401 		 */
    402 		printf(" (kgdb)\n");
    403 		/*
    404 		 * This is the kgdb port (exclusive use)
    405 		 * so skip the normal attach code.
    406 		 */
    407 		return;
    408 	}
    409 #endif
    410 	printf("\n");
    411 
    412 	tp = ttymalloc();
    413 	tp->t_dev = dev;
    414 	tp->t_oproc = zsstart;
    415 	tp->t_param = zsparam;
    416 	tp->t_hwiflow = zshwiflow;
    417 	tty_attach(tp);
    418 
    419 	zst->zst_tty = tp;
    420 	zst->zst_rbuf = malloc(zstty_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
    421 	if (zst->zst_rbuf == NULL) {
    422 		aprint_error("%s: unable to allocate ring buffer\n",
    423 		    zst->zst_dev.dv_xname);
    424 		return;
    425 	}
    426 	zst->zst_ebuf = zst->zst_rbuf + (zstty_rbuf_size << 1);
    427 	/* Disable the high water mark. */
    428 	zst->zst_r_hiwat = 0;
    429 	zst->zst_r_lowat = 0;
    430 	zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
    431 	zst->zst_rbavail = zstty_rbuf_size;
    432 
    433 	/* if there are no enable/disable functions, assume the device
    434 	   is always enabled */
    435 	if (!cs->enable)
    436 		cs->enabled = 1;
    437 
    438 	/*
    439 	 * Hardware init
    440 	 */
    441 	dtr_on = 0;
    442 	resetbit = 0;
    443 	if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
    444 		/* Call zsparam similar to open. */
    445 		struct termios t;
    446 
    447 		/* Wait a while for previous console output to complete */
    448 		DELAY(10000);
    449 
    450 		/* Setup the "new" parameters in t. */
    451 		t.c_ispeed = 0;
    452 		t.c_ospeed = cs->cs_defspeed;
    453 		t.c_cflag = cs->cs_defcflag;
    454 
    455 		/*
    456 		 * Turn on receiver and status interrupts.
    457 		 * We defer the actual write of the register to zsparam(),
    458 		 * but we must make sure status interrupts are turned on by
    459 		 * the time zsparam() reads the initial rr0 state.
    460 		 */
    461 		SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_SIE);
    462 
    463 		/* Make sure zsparam will see changes. */
    464 		tp->t_ospeed = 0;
    465 		(void) zsparam(tp, &t);
    466 
    467 		/* Make sure DTR is on now. */
    468 		dtr_on = 1;
    469 
    470 	} else if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_NORESET)) {
    471 		/* Not the console; may need reset. */
    472 		resetbit = (channel == 0) ? ZSWR9_A_RESET : ZSWR9_B_RESET;
    473 	}
    474 
    475 	s = splzs();
    476 	simple_lock(&cs->cs_lock);
    477 	if (resetbit)
    478 		zs_write_reg(cs, 9, resetbit);
    479 	zs_modem(zst, dtr_on);
    480 	simple_unlock(&cs->cs_lock);
    481 	splx(s);
    482 }
    483 
    484 
    485 /*
    486  * Return pointer to our tty.
    487  */
    488 struct tty *
    489 zstty(dev)
    490 	dev_t dev;
    491 {
    492 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev));
    493 
    494 	return (zst->zst_tty);
    495 }
    496 
    497 
    498 void
    499 zs_shutdown(zst)
    500 	struct zstty_softc *zst;
    501 {
    502 	struct zs_chanstate *cs = zst->zst_cs;
    503 	struct tty *tp = zst->zst_tty;
    504 	int s;
    505 
    506 	s = splzs();
    507 	simple_lock(&cs->cs_lock);
    508 
    509 	/* If we were asserting flow control, then deassert it. */
    510 	SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
    511 	zs_hwiflow(zst);
    512 
    513 	/* Clear any break condition set with TIOCSBRK. */
    514 	zs_break(cs, 0);
    515 
    516 #ifndef __HAVE_TIMECOUNTER
    517 	/* Turn off PPS capture on last close. */
    518 	zst->zst_ppsmask = 0;
    519 	zst->ppsparam.mode = 0;
    520 #endif /* __HAVE_TIMECOUNTER */
    521 
    522 	/*
    523 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    524 	 * notice even if we immediately open the port again.
    525 	 */
    526 	if (ISSET(tp->t_cflag, HUPCL)) {
    527 		zs_modem(zst, 0);
    528 		simple_unlock(&cs->cs_lock);
    529 		splx(s);
    530 		/*
    531 		 * XXX -    another process is not prevented from opening
    532 		 *	    the device during our sleep.
    533 		 */
    534 		(void) tsleep(cs, TTIPRI, ttclos, hz);
    535 		/* Re-check state in case we were opened during our sleep */
    536 		if (ISSET(tp->t_state, TS_ISOPEN) || tp->t_wopen != 0)
    537 			return;
    538 
    539 		s = splzs();
    540 		simple_lock(&cs->cs_lock);
    541 	}
    542 
    543 	/* Turn off interrupts if not the console. */
    544 	if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
    545 		CLR(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_SIE);
    546 		cs->cs_creg[1] = cs->cs_preg[1];
    547 		zs_write_reg(cs, 1, cs->cs_creg[1]);
    548 	}
    549 
    550 	/* Call the power management hook. */
    551 	if (cs->disable) {
    552 #ifdef DIAGNOSTIC
    553 		if (!cs->enabled)
    554 			panic("zs_shutdown: not enabled?");
    555 #endif
    556 		(*cs->disable)(zst->zst_cs);
    557 	}
    558 
    559 	simple_unlock(&cs->cs_lock);
    560 	splx(s);
    561 }
    562 
    563 /*
    564  * Open a zs serial (tty) port.
    565  */
    566 int
    567 zsopen(dev, flags, mode, l)
    568 	dev_t dev;
    569 	int flags;
    570 	int mode;
    571 	struct lwp *l;
    572 {
    573 	struct zstty_softc *zst;
    574 	struct zs_chanstate *cs;
    575 	struct tty *tp;
    576 	int s, s2;
    577 	int error;
    578 
    579 	zst = device_lookup(&zstty_cd, ZSUNIT(dev));
    580 	if (zst == NULL)
    581 		return (ENXIO);
    582 
    583 	tp = zst->zst_tty;
    584 	cs = zst->zst_cs;
    585 
    586 	/* If KGDB took the line, then tp==NULL */
    587 	if (tp == NULL)
    588 		return (EBUSY);
    589 
    590 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    591 		return (EBUSY);
    592 
    593 	s = spltty();
    594 
    595 	/*
    596 	 * Do the following iff this is a first open.
    597 	 */
    598 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    599 		struct termios t;
    600 
    601 		tp->t_dev = dev;
    602 
    603 		/* Call the power management hook. */
    604 		if (cs->enable) {
    605 			if ((*cs->enable)(cs)) {
    606 				splx(s);
    607 				printf("%s: device enable failed\n",
    608 			       	zst->zst_dev.dv_xname);
    609 				return (EIO);
    610 			}
    611 		}
    612 
    613 		/*
    614 		 * Initialize the termios status to the defaults.  Add in the
    615 		 * sticky bits from TIOCSFLAGS.
    616 		 */
    617 		t.c_ispeed = 0;
    618 		t.c_ospeed = cs->cs_defspeed;
    619 		t.c_cflag = cs->cs_defcflag;
    620 		if (ISSET(zst->zst_swflags, TIOCFLAG_CLOCAL))
    621 			SET(t.c_cflag, CLOCAL);
    622 		if (ISSET(zst->zst_swflags, TIOCFLAG_CRTSCTS))
    623 			SET(t.c_cflag, CRTSCTS);
    624 		if (ISSET(zst->zst_swflags, TIOCFLAG_CDTRCTS))
    625 			SET(t.c_cflag, CDTRCTS);
    626 		if (ISSET(zst->zst_swflags, TIOCFLAG_MDMBUF))
    627 			SET(t.c_cflag, MDMBUF);
    628 
    629 		s2 = splzs();
    630 		simple_lock(&cs->cs_lock);
    631 
    632 		/*
    633 		 * Turn on receiver and status interrupts.
    634 		 * We defer the actual write of the register to zsparam(),
    635 		 * but we must make sure status interrupts are turned on by
    636 		 * the time zsparam() reads the initial rr0 state.
    637 		 */
    638 		SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_SIE);
    639 
    640 		/* Clear PPS capture state on first open. */
    641 		zst->zst_ppsmask = 0;
    642 #ifdef __HAVE_TIMECOUNTER
    643 		memset(&zst->zst_pps_state, 0, sizeof(zst->zst_pps_state));
    644 		zst->zst_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
    645 		pps_init(&zst->zst_pps_state);
    646 #else /* !__HAVE_TIMECOUNTER */
    647 		zst->ppsparam.mode = 0;
    648 #endif /* !__HAVE_TIMECOUNTER */
    649 
    650 		simple_unlock(&cs->cs_lock);
    651 		splx(s2);
    652 
    653 		/* Make sure zsparam will see changes. */
    654 		tp->t_ospeed = 0;
    655 		(void) zsparam(tp, &t);
    656 
    657 		/*
    658 		 * Note: zsparam has done: cflag, ispeed, ospeed
    659 		 * so we just need to do: iflag, oflag, lflag, cc
    660 		 * For "raw" mode, just leave all zeros.
    661 		 */
    662 		if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_RAW)) {
    663 			tp->t_iflag = TTYDEF_IFLAG;
    664 			tp->t_oflag = TTYDEF_OFLAG;
    665 			tp->t_lflag = TTYDEF_LFLAG;
    666 		} else {
    667 			tp->t_iflag = 0;
    668 			tp->t_oflag = 0;
    669 			tp->t_lflag = 0;
    670 		}
    671 		ttychars(tp);
    672 		ttsetwater(tp);
    673 
    674 		s2 = splzs();
    675 		simple_lock(&cs->cs_lock);
    676 
    677 		/*
    678 		 * Turn on DTR.  We must always do this, even if carrier is not
    679 		 * present, because otherwise we'd have to use TIOCSDTR
    680 		 * immediately after setting CLOCAL, which applications do not
    681 		 * expect.  We always assert DTR while the device is open
    682 		 * unless explicitly requested to deassert it.
    683 		 */
    684 		zs_modem(zst, 1);
    685 
    686 		/* Clear the input ring, and unblock. */
    687 		zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
    688 		zst->zst_rbavail = zstty_rbuf_size;
    689 		zs_iflush(cs);
    690 		CLR(zst->zst_rx_flags, RX_ANY_BLOCK);
    691 		zs_hwiflow(zst);
    692 
    693 		simple_unlock(&cs->cs_lock);
    694 		splx(s2);
    695 	}
    696 
    697 	splx(s);
    698 
    699 	error = ttyopen(tp, ZSDIALOUT(dev), ISSET(flags, O_NONBLOCK));
    700 	if (error)
    701 		goto bad;
    702 
    703 	error = (*tp->t_linesw->l_open)(dev, tp);
    704 	if (error)
    705 		goto bad;
    706 
    707 	return (0);
    708 
    709 bad:
    710 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    711 		/*
    712 		 * We failed to open the device, and nobody else had it opened.
    713 		 * Clean up the state as appropriate.
    714 		 */
    715 		zs_shutdown(zst);
    716 	}
    717 
    718 	return (error);
    719 }
    720 
    721 /*
    722  * Close a zs serial port.
    723  */
    724 int
    725 zsclose(dev, flags, mode, l)
    726 	dev_t dev;
    727 	int flags;
    728 	int mode;
    729 	struct lwp *l;
    730 {
    731 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev));
    732 	struct tty *tp = zst->zst_tty;
    733 
    734 	/* XXX This is for cons.c. */
    735 	if (!ISSET(tp->t_state, TS_ISOPEN))
    736 		return 0;
    737 
    738 	(*tp->t_linesw->l_close)(tp, flags);
    739 	ttyclose(tp);
    740 
    741 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    742 		/*
    743 		 * Although we got a last close, the device may still be in
    744 		 * use; e.g. if this was the dialout node, and there are still
    745 		 * processes waiting for carrier on the non-dialout node.
    746 		 */
    747 		zs_shutdown(zst);
    748 	}
    749 
    750 	return (0);
    751 }
    752 
    753 /*
    754  * Read/write zs serial port.
    755  */
    756 int
    757 zsread(dev, uio, flags)
    758 	dev_t dev;
    759 	struct uio *uio;
    760 	int flags;
    761 {
    762 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev));
    763 	struct tty *tp = zst->zst_tty;
    764 
    765 	return ((*tp->t_linesw->l_read)(tp, uio, flags));
    766 }
    767 
    768 int
    769 zswrite(dev, uio, flags)
    770 	dev_t dev;
    771 	struct uio *uio;
    772 	int flags;
    773 {
    774 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev));
    775 	struct tty *tp = zst->zst_tty;
    776 
    777 	return ((*tp->t_linesw->l_write)(tp, uio, flags));
    778 }
    779 
    780 int
    781 zspoll(dev, events, l)
    782 	dev_t dev;
    783 	int events;
    784 	struct lwp *l;
    785 {
    786 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev));
    787 	struct tty *tp = zst->zst_tty;
    788 
    789 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    790 }
    791 
    792 int
    793 zsioctl(dev, cmd, data, flag, l)
    794 	dev_t dev;
    795 	u_long cmd;
    796 	caddr_t data;
    797 	int flag;
    798 	struct lwp *l;
    799 {
    800 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev));
    801 	struct zs_chanstate *cs = zst->zst_cs;
    802 	struct tty *tp = zst->zst_tty;
    803 	int error;
    804 	int s;
    805 
    806 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    807 	if (error != EPASSTHROUGH)
    808 		return (error);
    809 
    810 	error = ttioctl(tp, cmd, data, flag, l);
    811 	if (error != EPASSTHROUGH)
    812 		return (error);
    813 
    814 #ifdef	ZS_MD_IOCTL
    815 	error = ZS_MD_IOCTL(cs, cmd, data);
    816 	if (error != EPASSTHROUGH)
    817 		return (error);
    818 #endif	/* ZS_MD_IOCTL */
    819 
    820 	error = 0;
    821 
    822 	s = splzs();
    823 	simple_lock(&cs->cs_lock);
    824 
    825 	switch (cmd) {
    826 	case TIOCSBRK:
    827 		zs_break(cs, 1);
    828 		break;
    829 
    830 	case TIOCCBRK:
    831 		zs_break(cs, 0);
    832 		break;
    833 
    834 	case TIOCGFLAGS:
    835 		*(int *)data = zst->zst_swflags;
    836 		break;
    837 
    838 	case TIOCSFLAGS:
    839 		error = kauth_authorize_device_tty(l->l_cred,
    840 			KAUTH_DEVICE_TTY_PRIVSET, tp);
    841 		if (error)
    842 			break;
    843 		zst->zst_swflags = *(int *)data;
    844 		break;
    845 
    846 	case TIOCSDTR:
    847 		zs_modem(zst, 1);
    848 		break;
    849 
    850 	case TIOCCDTR:
    851 		zs_modem(zst, 0);
    852 		break;
    853 
    854 	case TIOCMSET:
    855 	case TIOCMBIS:
    856 	case TIOCMBIC:
    857 		tiocm_to_zs(zst, cmd, *(int *)data);
    858 		break;
    859 
    860 	case TIOCMGET:
    861 		*(int *)data = zs_to_tiocm(zst);
    862 		break;
    863 
    864 #ifdef __HAVE_TIMECOUNTER
    865 	case PPS_IOC_CREATE:
    866 	case PPS_IOC_DESTROY:
    867 	case PPS_IOC_GETPARAMS:
    868 	case PPS_IOC_SETPARAMS:
    869 	case PPS_IOC_GETCAP:
    870 	case PPS_IOC_FETCH:
    871 #ifdef PPS_SYNC
    872 	case PPS_IOC_KCBIND:
    873 #endif
    874 		error = pps_ioctl(cmd, data, &zst->zst_pps_state);
    875 		if (zst->zst_pps_state.ppsparam.mode & PPS_CAPTUREBOTH)
    876 			zst->zst_ppsmask = ZSRR0_DCD;
    877 		else
    878 			zst->zst_ppsmask = 0;
    879 		break;
    880 #else /* !__HAVE_TIMECOUNTER */
    881 	case PPS_IOC_CREATE:
    882 		break;
    883 
    884 	case PPS_IOC_DESTROY:
    885 		break;
    886 
    887 	case PPS_IOC_GETPARAMS: {
    888 		pps_params_t *pp;
    889 		pp = (pps_params_t *)data;
    890 		*pp = zst->ppsparam;
    891 		break;
    892 	}
    893 
    894 	case PPS_IOC_SETPARAMS: {
    895 		pps_params_t *pp;
    896 		int mode;
    897 		if (cs->cs_rr0_pps == 0) {
    898 			error = EINVAL;
    899 			break;
    900 		}
    901 		pp = (pps_params_t *)data;
    902 		if (pp->mode & ~zsppscap) {
    903 			error = EINVAL;
    904 			break;
    905 		}
    906 		zst->ppsparam = *pp;
    907 		/*
    908 		 * compute masks from user-specified timestamp state.
    909 		 */
    910 		mode = zst->ppsparam.mode;
    911 		switch (mode & PPS_CAPTUREBOTH) {
    912 		case 0:
    913 			zst->zst_ppsmask = 0;
    914 			break;
    915 
    916 		case PPS_CAPTUREASSERT:
    917 			zst->zst_ppsmask = ZSRR0_DCD;
    918 			zst->zst_ppsassert = ZSRR0_DCD;
    919 			zst->zst_ppsclear = -1;
    920 			break;
    921 
    922 		case PPS_CAPTURECLEAR:
    923 			zst->zst_ppsmask = ZSRR0_DCD;
    924 			zst->zst_ppsassert = -1;
    925 			zst->zst_ppsclear = 0;
    926 			break;
    927 
    928 		case PPS_CAPTUREBOTH:
    929 			zst->zst_ppsmask = ZSRR0_DCD;
    930 			zst->zst_ppsassert = ZSRR0_DCD;
    931 			zst->zst_ppsclear = 0;
    932 			break;
    933 
    934 		default:
    935 			error = EINVAL;
    936 			break;
    937 		}
    938 
    939 		/*
    940 		 * Now update interrupts.
    941 		 */
    942 		zs_maskintr(zst);
    943 		/*
    944 		 * If nothing is being transmitted, set up new current values,
    945 		 * else mark them as pending.
    946 		 */
    947 		if (!cs->cs_heldchange) {
    948 			if (zst->zst_tx_busy) {
    949 				zst->zst_heldtbc = zst->zst_tbc;
    950 				zst->zst_tbc = 0;
    951 				cs->cs_heldchange = 1;
    952 			} else
    953 				zs_loadchannelregs(cs);
    954 		}
    955 
    956 		break;
    957 	}
    958 
    959 	case PPS_IOC_GETCAP:
    960 		*(int *)data = zsppscap;
    961 		break;
    962 
    963 	case PPS_IOC_FETCH: {
    964 		pps_info_t *pi;
    965 		pi = (pps_info_t *)data;
    966 		*pi = zst->ppsinfo;
    967 		break;
    968 	}
    969 
    970 #ifdef PPS_SYNC
    971 	case PPS_IOC_KCBIND: {
    972 		int edge = (*(int *)data) & PPS_CAPTUREBOTH;
    973 
    974 		if (edge == 0) {
    975 			/*
    976 			 * remove binding for this source; ignore
    977 			 * the request if this is not the current
    978 			 * hardpps source
    979 			 */
    980 			if (pps_kc_hardpps_source == zst) {
    981 				pps_kc_hardpps_source = NULL;
    982 				pps_kc_hardpps_mode = 0;
    983 			}
    984 		} else {
    985 			/*
    986 			 * bind hardpps to this source, replacing any
    987 			 * previously specified source or edges
    988 			 */
    989 			pps_kc_hardpps_source = zst;
    990 			pps_kc_hardpps_mode = edge;
    991 		}
    992 		break;
    993 	}
    994 #endif /* PPS_SYNC */
    995 #endif /* !__HAVE_TIMECOUNTER */
    996 
    997 	case TIOCDCDTIMESTAMP:	/* XXX old, overloaded  API used by xntpd v3 */
    998 		if (cs->cs_rr0_pps == 0) {
    999 			error = EINVAL;
   1000 			break;
   1001 		}
   1002 #ifdef __HAVE_TIMECOUNTER
   1003 #ifndef PPS_TRAILING_EDGE
   1004 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1005 		    &zst->zst_pps_state.ppsinfo.assert_timestamp);
   1006 #else
   1007 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1008 		    &zst->zst_pps_state.ppsinfo.clear_timestamp);
   1009 #endif
   1010 #else /* !__HAVE_TIMECOUNTER */
   1011 		zst->zst_ppsmask = ZSRR0_DCD;
   1012 #ifndef	PPS_TRAILING_EDGE
   1013 		zst->zst_ppsassert = ZSRR0_DCD;
   1014 		zst->zst_ppsclear = -1;
   1015 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1016 			&zst->ppsinfo.assert_timestamp);
   1017 #else
   1018 		zst->zst_ppsassert = -1;
   1019 		zst->zst_ppsclear = 01;
   1020 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1021 			&zst->ppsinfo.clear_timestamp);
   1022 #endif
   1023 #endif /* !__HAVE_TIMECOUNTER */
   1024 		/*
   1025 		 * Now update interrupts.
   1026 		 */
   1027 		zs_maskintr(zst);
   1028 		/*
   1029 		 * If nothing is being transmitted, set up new current values,
   1030 		 * else mark them as pending.
   1031 		 */
   1032 		if (!cs->cs_heldchange) {
   1033 			if (zst->zst_tx_busy) {
   1034 				zst->zst_heldtbc = zst->zst_tbc;
   1035 				zst->zst_tbc = 0;
   1036 				cs->cs_heldchange = 1;
   1037 			} else
   1038 				zs_loadchannelregs(cs);
   1039 		}
   1040 
   1041 		break;
   1042 
   1043 	default:
   1044 		error = EPASSTHROUGH;
   1045 		break;
   1046 	}
   1047 
   1048 	simple_unlock(&cs->cs_lock);
   1049 	splx(s);
   1050 
   1051 	return (error);
   1052 }
   1053 
   1054 /*
   1055  * Start or restart transmission.
   1056  */
   1057 static void
   1058 zsstart(tp)
   1059 	struct tty *tp;
   1060 {
   1061 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
   1062 	struct zs_chanstate *cs = zst->zst_cs;
   1063 	u_char *tba;
   1064 	int s, tbc;
   1065 
   1066 	s = spltty();
   1067 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1068 		goto out;
   1069 	if (zst->zst_tx_stopped)
   1070 		goto out;
   1071 
   1072 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1073 		if (ISSET(tp->t_state, TS_ASLEEP)) {
   1074 			CLR(tp->t_state, TS_ASLEEP);
   1075 			wakeup((caddr_t)&tp->t_outq);
   1076 		}
   1077 		selwakeup(&tp->t_wsel);
   1078 		if (tp->t_outq.c_cc == 0)
   1079 			goto out;
   1080 	}
   1081 
   1082 	/* Grab the first contiguous region of buffer space. */
   1083 	tba = tp->t_outq.c_cf;
   1084 	tbc = ndqb(&tp->t_outq, 0);
   1085 
   1086 	(void) splzs();
   1087 	simple_lock(&cs->cs_lock);
   1088 
   1089 	zst->zst_tba = tba;
   1090 	zst->zst_tbc = tbc;
   1091 	SET(tp->t_state, TS_BUSY);
   1092 	zst->zst_tx_busy = 1;
   1093 
   1094 #ifdef ZS_TXDMA
   1095 	if (zst->zst_tbc > 1) {
   1096 		zs_dma_setup(cs, zst->zst_tba, zst->zst_tbc);
   1097 		goto out;
   1098 	}
   1099 #endif
   1100 
   1101 	/* Enable transmit completion interrupts if necessary. */
   1102 	if (!ISSET(cs->cs_preg[1], ZSWR1_TIE)) {
   1103 		SET(cs->cs_preg[1], ZSWR1_TIE);
   1104 		cs->cs_creg[1] = cs->cs_preg[1];
   1105 		zs_write_reg(cs, 1, cs->cs_creg[1]);
   1106 	}
   1107 
   1108 	/* Output the first character of the contiguous buffer. */
   1109 	zs_write_data(cs, *zst->zst_tba);
   1110 	zst->zst_tbc--;
   1111 	zst->zst_tba++;
   1112 
   1113 	simple_unlock(&cs->cs_lock);
   1114 out:
   1115 	splx(s);
   1116 	return;
   1117 }
   1118 
   1119 /*
   1120  * Stop output, e.g., for ^S or output flush.
   1121  */
   1122 void
   1123 zsstop(tp, flag)
   1124 	struct tty *tp;
   1125 	int flag;
   1126 {
   1127 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
   1128 	int s;
   1129 
   1130 	s = splzs();
   1131 	if (ISSET(tp->t_state, TS_BUSY)) {
   1132 		/* Stop transmitting at the next chunk. */
   1133 		zst->zst_tbc = 0;
   1134 		zst->zst_heldtbc = 0;
   1135 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1136 			SET(tp->t_state, TS_FLUSH);
   1137 	}
   1138 	splx(s);
   1139 }
   1140 
   1141 /*
   1142  * Set ZS tty parameters from termios.
   1143  * XXX - Should just copy the whole termios after
   1144  * making sure all the changes could be done.
   1145  */
   1146 static int
   1147 zsparam(tp, t)
   1148 	struct tty *tp;
   1149 	struct termios *t;
   1150 {
   1151 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
   1152 	struct zs_chanstate *cs = zst->zst_cs;
   1153 	int ospeed;
   1154 	tcflag_t cflag;
   1155 	u_char tmp3, tmp4, tmp5;
   1156 	int s, error;
   1157 
   1158 	ospeed = t->c_ospeed;
   1159 	cflag = t->c_cflag;
   1160 
   1161 	/* Check requested parameters. */
   1162 	if (ospeed < 0)
   1163 		return (EINVAL);
   1164 	if (t->c_ispeed && t->c_ispeed != ospeed)
   1165 		return (EINVAL);
   1166 
   1167 	/*
   1168 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1169 	 * is always active.
   1170 	 */
   1171 	if (ISSET(zst->zst_swflags, TIOCFLAG_SOFTCAR) ||
   1172 	    ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
   1173 		SET(cflag, CLOCAL);
   1174 		CLR(cflag, HUPCL);
   1175 	}
   1176 
   1177 	/*
   1178 	 * Only whack the UART when params change.
   1179 	 * Some callers need to clear tp->t_ospeed
   1180 	 * to make sure initialization gets done.
   1181 	 */
   1182 	if (tp->t_ospeed == ospeed &&
   1183 	    tp->t_cflag == cflag)
   1184 		return (0);
   1185 
   1186 	/*
   1187 	 * Call MD functions to deal with changed
   1188 	 * clock modes or H/W flow control modes.
   1189 	 * The BRG divisor is set now. (reg 12,13)
   1190 	 */
   1191 	error = zs_set_speed(cs, ospeed);
   1192 	if (error)
   1193 		return (error);
   1194 	error = zs_set_modes(cs, cflag);
   1195 	if (error)
   1196 		return (error);
   1197 
   1198 	/*
   1199 	 * Block interrupts so that state will not
   1200 	 * be altered until we are done setting it up.
   1201 	 *
   1202 	 * Initial values in cs_preg are set before
   1203 	 * our attach routine is called.  The master
   1204 	 * interrupt enable is handled by zsc.c
   1205 	 *
   1206 	 */
   1207 	s = splzs();
   1208 	simple_lock(&cs->cs_lock);
   1209 
   1210 	/*
   1211 	 * Recalculate which status ints to enable.
   1212 	 */
   1213 	zs_maskintr(zst);
   1214 
   1215 	/* Recompute character size bits. */
   1216 	tmp3 = cs->cs_preg[3];
   1217 	tmp5 = cs->cs_preg[5];
   1218 	CLR(tmp3, ZSWR3_RXSIZE);
   1219 	CLR(tmp5, ZSWR5_TXSIZE);
   1220 	switch (ISSET(cflag, CSIZE)) {
   1221 	case CS5:
   1222 		SET(tmp3, ZSWR3_RX_5);
   1223 		SET(tmp5, ZSWR5_TX_5);
   1224 		break;
   1225 	case CS6:
   1226 		SET(tmp3, ZSWR3_RX_6);
   1227 		SET(tmp5, ZSWR5_TX_6);
   1228 		break;
   1229 	case CS7:
   1230 		SET(tmp3, ZSWR3_RX_7);
   1231 		SET(tmp5, ZSWR5_TX_7);
   1232 		break;
   1233 	case CS8:
   1234 		SET(tmp3, ZSWR3_RX_8);
   1235 		SET(tmp5, ZSWR5_TX_8);
   1236 		break;
   1237 	}
   1238 	cs->cs_preg[3] = tmp3;
   1239 	cs->cs_preg[5] = tmp5;
   1240 
   1241 	/*
   1242 	 * Recompute the stop bits and parity bits.  Note that
   1243 	 * zs_set_speed() may have set clock selection bits etc.
   1244 	 * in wr4, so those must preserved.
   1245 	 */
   1246 	tmp4 = cs->cs_preg[4];
   1247 	CLR(tmp4, ZSWR4_SBMASK | ZSWR4_PARMASK);
   1248 	if (ISSET(cflag, CSTOPB))
   1249 		SET(tmp4, ZSWR4_TWOSB);
   1250 	else
   1251 		SET(tmp4, ZSWR4_ONESB);
   1252 	if (!ISSET(cflag, PARODD))
   1253 		SET(tmp4, ZSWR4_EVENP);
   1254 	if (ISSET(cflag, PARENB))
   1255 		SET(tmp4, ZSWR4_PARENB);
   1256 	cs->cs_preg[4] = tmp4;
   1257 
   1258 	/* And copy to tty. */
   1259 	tp->t_ispeed = 0;
   1260 	tp->t_ospeed = ospeed;
   1261 	tp->t_cflag = cflag;
   1262 
   1263 	/*
   1264 	 * If nothing is being transmitted, set up new current values,
   1265 	 * else mark them as pending.
   1266 	 */
   1267 	if (!cs->cs_heldchange) {
   1268 		if (zst->zst_tx_busy) {
   1269 			zst->zst_heldtbc = zst->zst_tbc;
   1270 			zst->zst_tbc = 0;
   1271 			cs->cs_heldchange = 1;
   1272 		} else
   1273 			zs_loadchannelregs(cs);
   1274 	}
   1275 
   1276 	/*
   1277 	 * If hardware flow control is disabled, turn off the buffer water
   1278 	 * marks and unblock any soft flow control state.  Otherwise, enable
   1279 	 * the water marks.
   1280 	 */
   1281 	if (!ISSET(cflag, CHWFLOW)) {
   1282 		zst->zst_r_hiwat = 0;
   1283 		zst->zst_r_lowat = 0;
   1284 		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
   1285 			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
   1286 			zst->zst_rx_ready = 1;
   1287 			cs->cs_softreq = 1;
   1288 		}
   1289 		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
   1290 			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
   1291 			zs_hwiflow(zst);
   1292 		}
   1293 	} else {
   1294 		zst->zst_r_hiwat = zstty_rbuf_hiwat;
   1295 		zst->zst_r_lowat = zstty_rbuf_lowat;
   1296 	}
   1297 
   1298 	/*
   1299 	 * Force a recheck of the hardware carrier and flow control status,
   1300 	 * since we may have changed which bits we're looking at.
   1301 	 */
   1302 	zstty_stint(cs, 1);
   1303 
   1304 	simple_unlock(&cs->cs_lock);
   1305 	splx(s);
   1306 
   1307 	/*
   1308 	 * If hardware flow control is disabled, unblock any hard flow control
   1309 	 * state.
   1310 	 */
   1311 	if (!ISSET(cflag, CHWFLOW)) {
   1312 		if (zst->zst_tx_stopped) {
   1313 			zst->zst_tx_stopped = 0;
   1314 			zsstart(tp);
   1315 		}
   1316 	}
   1317 
   1318 	zstty_softint(cs);
   1319 
   1320 	return (0);
   1321 }
   1322 
   1323 /*
   1324  * Compute interrupt enable bits and set in the pending bits. Called both
   1325  * in zsparam() and when PPS (pulse per second timing) state changes.
   1326  * Must be called at splzs().
   1327  */
   1328 static void
   1329 zs_maskintr(zst)
   1330 	struct zstty_softc *zst;
   1331 {
   1332 	struct zs_chanstate *cs = zst->zst_cs;
   1333 	int tmp15;
   1334 
   1335 	cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
   1336 	if (zst->zst_ppsmask != 0)
   1337 		cs->cs_rr0_mask |= cs->cs_rr0_pps;
   1338 	tmp15 = cs->cs_preg[15];
   1339 	if (ISSET(cs->cs_rr0_mask, ZSRR0_DCD))
   1340 		SET(tmp15, ZSWR15_DCD_IE);
   1341 	else
   1342 		CLR(tmp15, ZSWR15_DCD_IE);
   1343 	if (ISSET(cs->cs_rr0_mask, ZSRR0_CTS))
   1344 		SET(tmp15, ZSWR15_CTS_IE);
   1345 	else
   1346 		CLR(tmp15, ZSWR15_CTS_IE);
   1347 	cs->cs_preg[15] = tmp15;
   1348 }
   1349 
   1350 
   1351 /*
   1352  * Raise or lower modem control (DTR/RTS) signals.  If a character is
   1353  * in transmission, the change is deferred.
   1354  * Called at splzs() and with the channel lock held.
   1355  */
   1356 static void
   1357 zs_modem(zst, onoff)
   1358 	struct zstty_softc *zst;
   1359 	int onoff;
   1360 {
   1361 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
   1362 
   1363 	if (cs->cs_wr5_dtr == 0)
   1364 		return;
   1365 
   1366 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
   1367 
   1368 	if (onoff)
   1369 		SET(ccs->cs_preg[5], cs->cs_wr5_dtr);
   1370 	else
   1371 		CLR(ccs->cs_preg[5], cs->cs_wr5_dtr);
   1372 
   1373 	if (!cs->cs_heldchange) {
   1374 		if (zst->zst_tx_busy) {
   1375 			zst->zst_heldtbc = zst->zst_tbc;
   1376 			zst->zst_tbc = 0;
   1377 			cs->cs_heldchange = 1;
   1378 		} else
   1379 			zs_loadchannelregs(cs);
   1380 	}
   1381 }
   1382 
   1383 /*
   1384  * Set modem bits.
   1385  * Called at splzs() and with the channel lock held.
   1386  */
   1387 static void
   1388 tiocm_to_zs(zst, how, ttybits)
   1389 	struct zstty_softc *zst;
   1390 	u_long how;
   1391 	int ttybits;
   1392 {
   1393 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
   1394 	u_char zsbits;
   1395 
   1396 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
   1397 
   1398 	zsbits = 0;
   1399 	if (ISSET(ttybits, TIOCM_DTR))
   1400 		SET(zsbits, ZSWR5_DTR);
   1401 	if (ISSET(ttybits, TIOCM_RTS))
   1402 		SET(zsbits, ZSWR5_RTS);
   1403 
   1404 	switch (how) {
   1405 	case TIOCMBIC:
   1406 		CLR(ccs->cs_preg[5], zsbits);
   1407 		break;
   1408 
   1409 	case TIOCMBIS:
   1410 		SET(ccs->cs_preg[5], zsbits);
   1411 		break;
   1412 
   1413 	case TIOCMSET:
   1414 		CLR(ccs->cs_preg[5], ZSWR5_RTS | ZSWR5_DTR);
   1415 		SET(ccs->cs_preg[5], zsbits);
   1416 		break;
   1417 	}
   1418 
   1419 	if (!cs->cs_heldchange) {
   1420 		if (zst->zst_tx_busy) {
   1421 			zst->zst_heldtbc = zst->zst_tbc;
   1422 			zst->zst_tbc = 0;
   1423 			cs->cs_heldchange = 1;
   1424 		} else
   1425 			zs_loadchannelregs(cs);
   1426 	}
   1427 }
   1428 
   1429 /*
   1430  * Get modem bits.
   1431  * Called at splzs() and with the channel lock held.
   1432  */
   1433 static int
   1434 zs_to_tiocm(zst)
   1435 	struct zstty_softc *zst;
   1436 {
   1437 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
   1438 	u_char zsbits;
   1439 	int ttybits = 0;
   1440 
   1441 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
   1442 
   1443 	zsbits = ccs->cs_preg[5];
   1444 	if (ISSET(zsbits, ZSWR5_DTR))
   1445 		SET(ttybits, TIOCM_DTR);
   1446 	if (ISSET(zsbits, ZSWR5_RTS))
   1447 		SET(ttybits, TIOCM_RTS);
   1448 
   1449 	zsbits = cs->cs_rr0;
   1450 	if (ISSET(zsbits, ZSRR0_DCD))
   1451 		SET(ttybits, TIOCM_CD);
   1452 	if (ISSET(zsbits, ZSRR0_CTS))
   1453 		SET(ttybits, TIOCM_CTS);
   1454 
   1455 	return (ttybits);
   1456 }
   1457 
   1458 /*
   1459  * Try to block or unblock input using hardware flow-control.
   1460  * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
   1461  * if this function returns non-zero, the TS_TBLOCK flag will
   1462  * be set or cleared according to the "block" arg passed.
   1463  */
   1464 int
   1465 zshwiflow(tp, block)
   1466 	struct tty *tp;
   1467 	int block;
   1468 {
   1469 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
   1470 	struct zs_chanstate *cs = zst->zst_cs;
   1471 	int s;
   1472 
   1473 	if (cs->cs_wr5_rts == 0)
   1474 		return (0);
   1475 
   1476 	s = splzs();
   1477 	simple_lock(&cs->cs_lock);
   1478 	if (block) {
   1479 		if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
   1480 			SET(zst->zst_rx_flags, RX_TTY_BLOCKED);
   1481 			zs_hwiflow(zst);
   1482 		}
   1483 	} else {
   1484 		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
   1485 			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
   1486 			zst->zst_rx_ready = 1;
   1487 			cs->cs_softreq = 1;
   1488 		}
   1489 		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
   1490 			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED);
   1491 			zs_hwiflow(zst);
   1492 		}
   1493 	}
   1494 	simple_unlock(&cs->cs_lock);
   1495 	splx(s);
   1496 	return (1);
   1497 }
   1498 
   1499 /*
   1500  * Internal version of zshwiflow
   1501  * Called at splzs() and with the channel lock held.
   1502  */
   1503 static void
   1504 zs_hwiflow(zst)
   1505 	struct zstty_softc *zst;
   1506 {
   1507 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
   1508 
   1509 	if (cs->cs_wr5_rts == 0)
   1510 		return;
   1511 
   1512 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
   1513 
   1514 	if (ISSET(zst->zst_rx_flags, RX_ANY_BLOCK)) {
   1515 		CLR(ccs->cs_preg[5], cs->cs_wr5_rts);
   1516 		CLR(ccs->cs_creg[5], cs->cs_wr5_rts);
   1517 	} else {
   1518 		SET(ccs->cs_preg[5], cs->cs_wr5_rts);
   1519 		SET(ccs->cs_creg[5], cs->cs_wr5_rts);
   1520 	}
   1521 	zs_write_reg(ccs, 5, ccs->cs_creg[5]);
   1522 }
   1523 
   1524 
   1525 /****************************************************************
   1526  * Interface to the lower layer (zscc)
   1527  ****************************************************************/
   1528 
   1529 #define	integrate	static inline
   1530 integrate void zstty_rxsoft(struct zstty_softc *, struct tty *);
   1531 integrate void zstty_txsoft(struct zstty_softc *, struct tty *);
   1532 integrate void zstty_stsoft(struct zstty_softc *, struct tty *);
   1533 static void zstty_diag(void *);
   1534 
   1535 /*
   1536  * Receiver Ready interrupt.
   1537  * Called at splzs() and with the channel lock held.
   1538  */
   1539 static void
   1540 zstty_rxint(cs)
   1541 	struct zs_chanstate *cs;
   1542 {
   1543 	struct zstty_softc *zst = cs->cs_private;
   1544 	u_char *put, *end;
   1545 	u_int cc;
   1546 	u_char rr0, rr1, c;
   1547 
   1548 	end = zst->zst_ebuf;
   1549 	put = zst->zst_rbput;
   1550 	cc = zst->zst_rbavail;
   1551 
   1552 	while (cc > 0) {
   1553 		/*
   1554 		 * First read the status, because reading the received char
   1555 		 * destroys the status of this char.
   1556 		 */
   1557 		rr1 = zs_read_reg(cs, 1);
   1558 		c = zs_read_data(cs);
   1559 
   1560 		if (ISSET(rr1, ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
   1561 			/* Clear the receive error. */
   1562 			zs_write_csr(cs, ZSWR0_RESET_ERRORS);
   1563 		}
   1564 
   1565 		cn_check_magic(zst->zst_tty->t_dev, c, zstty_cnm_state);
   1566 		put[0] = c;
   1567 		put[1] = rr1;
   1568 		put += 2;
   1569 		if (put >= end)
   1570 			put = zst->zst_rbuf;
   1571 		cc--;
   1572 
   1573 		rr0 = zs_read_csr(cs);
   1574 		if (!ISSET(rr0, ZSRR0_RX_READY))
   1575 			break;
   1576 	}
   1577 
   1578 	/*
   1579 	 * Current string of incoming characters ended because
   1580 	 * no more data was available or we ran out of space.
   1581 	 * Schedule a receive event if any data was received.
   1582 	 * If we're out of space, turn off receive interrupts.
   1583 	 */
   1584 	zst->zst_rbput = put;
   1585 	zst->zst_rbavail = cc;
   1586 	if (!ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
   1587 		zst->zst_rx_ready = 1;
   1588 		cs->cs_softreq = 1;
   1589 	}
   1590 
   1591 	/*
   1592 	 * See if we are in danger of overflowing a buffer. If
   1593 	 * so, use hardware flow control to ease the pressure.
   1594 	 */
   1595 	if (!ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED) &&
   1596 	    cc < zst->zst_r_hiwat) {
   1597 		SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
   1598 		zs_hwiflow(zst);
   1599 	}
   1600 
   1601 	/*
   1602 	 * If we're out of space, disable receive interrupts
   1603 	 * until the queue has drained a bit.
   1604 	 */
   1605 	if (!cc) {
   1606 		SET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
   1607 		CLR(cs->cs_preg[1], ZSWR1_RIE);
   1608 		cs->cs_creg[1] = cs->cs_preg[1];
   1609 		zs_write_reg(cs, 1, cs->cs_creg[1]);
   1610 	}
   1611 
   1612 #if 0
   1613 	printf("%xH%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
   1614 #endif
   1615 }
   1616 
   1617 /*
   1618  * Transmitter Ready interrupt.
   1619  * Called at splzs() and with the channel lock held.
   1620  */
   1621 static void
   1622 zstty_txint(cs)
   1623 	struct zs_chanstate *cs;
   1624 {
   1625 	struct zstty_softc *zst = cs->cs_private;
   1626 
   1627 	/*
   1628 	 * If we've delayed a parameter change, do it now, and restart
   1629 	 * output.
   1630 	 */
   1631 	if (cs->cs_heldchange) {
   1632 		zs_loadchannelregs(cs);
   1633 		cs->cs_heldchange = 0;
   1634 		zst->zst_tbc = zst->zst_heldtbc;
   1635 		zst->zst_heldtbc = 0;
   1636 	}
   1637 
   1638 	/* Output the next character in the buffer, if any. */
   1639 	if (zst->zst_tbc > 0) {
   1640 		zs_write_data(cs, *zst->zst_tba);
   1641 		zst->zst_tbc--;
   1642 		zst->zst_tba++;
   1643 	} else {
   1644 		/* Disable transmit completion interrupts if necessary. */
   1645 		if (ISSET(cs->cs_preg[1], ZSWR1_TIE)) {
   1646 			CLR(cs->cs_preg[1], ZSWR1_TIE);
   1647 			cs->cs_creg[1] = cs->cs_preg[1];
   1648 			zs_write_reg(cs, 1, cs->cs_creg[1]);
   1649 		}
   1650 		if (zst->zst_tx_busy) {
   1651 			zst->zst_tx_busy = 0;
   1652 			zst->zst_tx_done = 1;
   1653 			cs->cs_softreq = 1;
   1654 		}
   1655 	}
   1656 }
   1657 
   1658 /*
   1659  * Status Change interrupt.
   1660  * Called at splzs() and with the channel lock held.
   1661  */
   1662 static void
   1663 zstty_stint(cs, force)
   1664 	struct zs_chanstate *cs;
   1665 	int force;
   1666 {
   1667 	struct zstty_softc *zst = cs->cs_private;
   1668 	u_char rr0, delta;
   1669 
   1670 	rr0 = zs_read_csr(cs);
   1671 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
   1672 
   1673 	/*
   1674 	 * Check here for console break, so that we can abort
   1675 	 * even when interrupts are locking up the machine.
   1676 	 */
   1677 	if (ISSET(rr0, ZSRR0_BREAK))
   1678 		cn_check_magic(zst->zst_tty->t_dev, CNC_BREAK, zstty_cnm_state);
   1679 
   1680 	if (!force)
   1681 		delta = rr0 ^ cs->cs_rr0;
   1682 	else
   1683 		delta = cs->cs_rr0_mask;
   1684 	cs->cs_rr0 = rr0;
   1685 
   1686 	if (ISSET(delta, cs->cs_rr0_mask)) {
   1687 		SET(cs->cs_rr0_delta, delta);
   1688 
   1689 		/*
   1690 		 * Pulse-per-second clock signal on edge of DCD?
   1691 		 */
   1692 		if (ISSET(delta, zst->zst_ppsmask)) {
   1693 #ifdef __HAVE_TIMECOUNTER
   1694 			if (zst->zst_pps_state.ppsparam.mode & PPS_CAPTUREBOTH) {
   1695 				pps_capture(&zst->zst_pps_state);
   1696 				pps_event(&zst->zst_pps_state,
   1697 				    (ISSET(cs->cs_rr0, zst->zst_ppsmask))
   1698 				    ? PPS_CAPTUREASSERT
   1699 				    : PPS_CAPTURECLEAR);
   1700 			}
   1701 #else /* !__HAVE_TIMECOUNTER */
   1702 			struct timeval tv;
   1703 			if (ISSET(rr0, zst->zst_ppsmask) == zst->zst_ppsassert) {
   1704 				/* XXX nanotime() */
   1705 				microtime(&tv);
   1706 				TIMEVAL_TO_TIMESPEC(&tv,
   1707 					&zst->ppsinfo.assert_timestamp);
   1708 				if (zst->ppsparam.mode & PPS_OFFSETASSERT) {
   1709 					timespecadd(&zst->ppsinfo.assert_timestamp,
   1710 					    &zst->ppsparam.assert_offset,
   1711 					    &zst->ppsinfo.assert_timestamp);
   1712 				}
   1713 
   1714 #ifdef PPS_SYNC
   1715 				if (pps_kc_hardpps_source == zst &&
   1716 				    pps_kc_hardpps_mode & PPS_CAPTUREASSERT) {
   1717 					hardpps(&tv, tv.tv_usec);
   1718 				}
   1719 #endif
   1720 				zst->ppsinfo.assert_sequence++;
   1721 				zst->ppsinfo.current_mode = zst->ppsparam.mode;
   1722 			} else if (ISSET(rr0, zst->zst_ppsmask) ==
   1723 						zst->zst_ppsclear) {
   1724 				/* XXX nanotime() */
   1725 				microtime(&tv);
   1726 				TIMEVAL_TO_TIMESPEC(&tv,
   1727 					&zst->ppsinfo.clear_timestamp);
   1728 				if (zst->ppsparam.mode & PPS_OFFSETCLEAR) {
   1729 					timespecadd(&zst->ppsinfo.clear_timestamp,
   1730 						&zst->ppsparam.clear_offset,
   1731 						&zst->ppsinfo.clear_timestamp);
   1732 				}
   1733 
   1734 #ifdef PPS_SYNC
   1735 				if (pps_kc_hardpps_source == zst &&
   1736 				    pps_kc_hardpps_mode & PPS_CAPTURECLEAR) {
   1737 					hardpps(&tv, tv.tv_usec);
   1738 				}
   1739 #endif
   1740 				zst->ppsinfo.clear_sequence++;
   1741 				zst->ppsinfo.current_mode = zst->ppsparam.mode;
   1742 			}
   1743 #endif /* !__HAVE_TIMECOUNTER */
   1744 		}
   1745 
   1746 		/*
   1747 		 * Stop output immediately if we lose the output
   1748 		 * flow control signal or carrier detect.
   1749 		 */
   1750 		if (ISSET(~rr0, cs->cs_rr0_mask)) {
   1751 			zst->zst_tbc = 0;
   1752 			zst->zst_heldtbc = 0;
   1753 		}
   1754 
   1755 		zst->zst_st_check = 1;
   1756 		cs->cs_softreq = 1;
   1757 	}
   1758 }
   1759 
   1760 void
   1761 zstty_diag(arg)
   1762 	void *arg;
   1763 {
   1764 	struct zstty_softc *zst = arg;
   1765 	int overflows, floods;
   1766 	int s;
   1767 
   1768 	s = splzs();
   1769 	overflows = zst->zst_overflows;
   1770 	zst->zst_overflows = 0;
   1771 	floods = zst->zst_floods;
   1772 	zst->zst_floods = 0;
   1773 	zst->zst_errors = 0;
   1774 	splx(s);
   1775 
   1776 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1777 	    zst->zst_dev.dv_xname,
   1778 	    overflows, overflows == 1 ? "" : "s",
   1779 	    floods, floods == 1 ? "" : "s");
   1780 }
   1781 
   1782 integrate void
   1783 zstty_rxsoft(zst, tp)
   1784 	struct zstty_softc *zst;
   1785 	struct tty *tp;
   1786 {
   1787 	struct zs_chanstate *cs = zst->zst_cs;
   1788 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
   1789 	u_char *get, *end;
   1790 	u_int cc, scc;
   1791 	u_char rr1;
   1792 	int code;
   1793 	int s;
   1794 
   1795 	end = zst->zst_ebuf;
   1796 	get = zst->zst_rbget;
   1797 	scc = cc = zstty_rbuf_size - zst->zst_rbavail;
   1798 
   1799 	if (cc == zstty_rbuf_size) {
   1800 		zst->zst_floods++;
   1801 		if (zst->zst_errors++ == 0)
   1802 			callout_reset(&zst->zst_diag_ch, 60 * hz,
   1803 			    zstty_diag, zst);
   1804 	}
   1805 
   1806 	/* If not yet open, drop the entire buffer content here */
   1807 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
   1808 		get += cc << 1;
   1809 		if (get >= end)
   1810 			get -= zstty_rbuf_size << 1;
   1811 		cc = 0;
   1812 	}
   1813 	while (cc) {
   1814 		code = get[0];
   1815 		rr1 = get[1];
   1816 		if (ISSET(rr1, ZSRR1_DO | ZSRR1_FE | ZSRR1_PE)) {
   1817 			if (ISSET(rr1, ZSRR1_DO)) {
   1818 				zst->zst_overflows++;
   1819 				if (zst->zst_errors++ == 0)
   1820 					callout_reset(&zst->zst_diag_ch,
   1821 					    60 * hz, zstty_diag, zst);
   1822 			}
   1823 			if (ISSET(rr1, ZSRR1_FE))
   1824 				SET(code, TTY_FE);
   1825 			if (ISSET(rr1, ZSRR1_PE))
   1826 				SET(code, TTY_PE);
   1827 		}
   1828 		if ((*rint)(code, tp) == -1) {
   1829 			/*
   1830 			 * The line discipline's buffer is out of space.
   1831 			 */
   1832 			if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
   1833 				/*
   1834 				 * We're either not using flow control, or the
   1835 				 * line discipline didn't tell us to block for
   1836 				 * some reason.  Either way, we have no way to
   1837 				 * know when there's more space available, so
   1838 				 * just drop the rest of the data.
   1839 				 */
   1840 				get += cc << 1;
   1841 				if (get >= end)
   1842 					get -= zstty_rbuf_size << 1;
   1843 				cc = 0;
   1844 			} else {
   1845 				/*
   1846 				 * Don't schedule any more receive processing
   1847 				 * until the line discipline tells us there's
   1848 				 * space available (through comhwiflow()).
   1849 				 * Leave the rest of the data in the input
   1850 				 * buffer.
   1851 				 */
   1852 				SET(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
   1853 			}
   1854 			break;
   1855 		}
   1856 		get += 2;
   1857 		if (get >= end)
   1858 			get = zst->zst_rbuf;
   1859 		cc--;
   1860 	}
   1861 
   1862 	if (cc != scc) {
   1863 		zst->zst_rbget = get;
   1864 		s = splzs();
   1865 		simple_lock(&cs->cs_lock);
   1866 		cc = zst->zst_rbavail += scc - cc;
   1867 		/* Buffers should be ok again, release possible block. */
   1868 		if (cc >= zst->zst_r_lowat) {
   1869 			if (ISSET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED)) {
   1870 				CLR(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
   1871 				SET(cs->cs_preg[1], ZSWR1_RIE);
   1872 				cs->cs_creg[1] = cs->cs_preg[1];
   1873 				zs_write_reg(cs, 1, cs->cs_creg[1]);
   1874 			}
   1875 			if (ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED)) {
   1876 				CLR(zst->zst_rx_flags, RX_IBUF_BLOCKED);
   1877 				zs_hwiflow(zst);
   1878 			}
   1879 		}
   1880 		simple_unlock(&cs->cs_lock);
   1881 		splx(s);
   1882 	}
   1883 
   1884 #if 0
   1885 	printf("%xS%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
   1886 #endif
   1887 }
   1888 
   1889 integrate void
   1890 zstty_txsoft(zst, tp)
   1891 	struct zstty_softc *zst;
   1892 	struct tty *tp;
   1893 {
   1894 	struct zs_chanstate *cs = zst->zst_cs;
   1895 	int s;
   1896 
   1897 	s = splzs();
   1898 	simple_lock(&cs->cs_lock);
   1899 	CLR(tp->t_state, TS_BUSY);
   1900 	if (ISSET(tp->t_state, TS_FLUSH))
   1901 		CLR(tp->t_state, TS_FLUSH);
   1902 	else
   1903 		ndflush(&tp->t_outq, (int)(zst->zst_tba - tp->t_outq.c_cf));
   1904 	simple_unlock(&cs->cs_lock);
   1905 	splx(s);
   1906 	(*tp->t_linesw->l_start)(tp);
   1907 }
   1908 
   1909 integrate void
   1910 zstty_stsoft(zst, tp)
   1911 	struct zstty_softc *zst;
   1912 	struct tty *tp;
   1913 {
   1914 	struct zs_chanstate *cs = zst->zst_cs;
   1915 	u_char rr0, delta;
   1916 	int s;
   1917 
   1918 	s = splzs();
   1919 	simple_lock(&cs->cs_lock);
   1920 	rr0 = cs->cs_rr0;
   1921 	delta = cs->cs_rr0_delta;
   1922 	cs->cs_rr0_delta = 0;
   1923 	simple_unlock(&cs->cs_lock);
   1924 	splx(s);
   1925 
   1926 	if (ISSET(delta, cs->cs_rr0_dcd)) {
   1927 		/*
   1928 		 * Inform the tty layer that carrier detect changed.
   1929 		 */
   1930 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(rr0, ZSRR0_DCD));
   1931 	}
   1932 
   1933 	if (ISSET(delta, cs->cs_rr0_cts)) {
   1934 		/* Block or unblock output according to flow control. */
   1935 		if (ISSET(rr0, cs->cs_rr0_cts)) {
   1936 			zst->zst_tx_stopped = 0;
   1937 			(*tp->t_linesw->l_start)(tp);
   1938 		} else {
   1939 			zst->zst_tx_stopped = 1;
   1940 		}
   1941 	}
   1942 }
   1943 
   1944 /*
   1945  * Software interrupt.  Called at zssoft
   1946  *
   1947  * The main job to be done here is to empty the input ring
   1948  * by passing its contents up to the tty layer.  The ring is
   1949  * always emptied during this operation, therefore the ring
   1950  * must not be larger than the space after "high water" in
   1951  * the tty layer, or the tty layer might drop our input.
   1952  *
   1953  * Note: an "input blockage" condition is assumed to exist if
   1954  * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
   1955  */
   1956 static void
   1957 zstty_softint(cs)
   1958 	struct zs_chanstate *cs;
   1959 {
   1960 	struct zstty_softc *zst = cs->cs_private;
   1961 	struct tty *tp = zst->zst_tty;
   1962 	int s;
   1963 
   1964 	s = spltty();
   1965 
   1966 	if (zst->zst_rx_ready) {
   1967 		zst->zst_rx_ready = 0;
   1968 		zstty_rxsoft(zst, tp);
   1969 	}
   1970 
   1971 	if (zst->zst_st_check) {
   1972 		zst->zst_st_check = 0;
   1973 		zstty_stsoft(zst, tp);
   1974 	}
   1975 
   1976 	if (zst->zst_tx_done) {
   1977 		zst->zst_tx_done = 0;
   1978 		zstty_txsoft(zst, tp);
   1979 	}
   1980 
   1981 	splx(s);
   1982 }
   1983 
   1984 struct zsops zsops_tty = {
   1985 	zstty_rxint,	/* receive char available */
   1986 	zstty_stint,	/* external/status */
   1987 	zstty_txint,	/* xmit buffer empty */
   1988 	zstty_softint,	/* process software interrupt */
   1989 };
   1990 
   1991 #ifdef ZS_TXDMA
   1992 void
   1993 zstty_txdma_int(arg)
   1994 	void *arg;
   1995 {
   1996 	struct zs_chanstate *cs = arg;
   1997 	struct zstty_softc *zst = cs->cs_private;
   1998 
   1999 	zst->zst_tba += zst->zst_tbc;
   2000 	zst->zst_tbc = 0;
   2001 
   2002 	if (zst->zst_tx_busy) {
   2003 		zst->zst_tx_busy = 0;
   2004 		zst->zst_tx_done = 1;
   2005 		cs->cs_softreq = 1;
   2006 	}
   2007 }
   2008 #endif
   2009