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