Home | History | Annotate | Line # | Download | only in ic
z8530tty.c revision 1.108
      1 /*	$NetBSD: z8530tty.c,v 1.108 2006/06/07 22:33:36 kardel 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.108 2006/06/07 22:33:36 kardel 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 	struct proc *p;
    577 	int s, s2;
    578 	int error;
    579 
    580 	zst = device_lookup(&zstty_cd, ZSUNIT(dev));
    581 	if (zst == NULL)
    582 		return (ENXIO);
    583 
    584 	tp = zst->zst_tty;
    585 	cs = zst->zst_cs;
    586 	p = l->l_proc;
    587 
    588 	/* If KGDB took the line, then tp==NULL */
    589 	if (tp == NULL)
    590 		return (EBUSY);
    591 
    592 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    593 	    ISSET(tp->t_state, TS_XCLUDE) &&
    594 	    kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
    595 			      &p->p_acflag) != 0)
    596 		return (EBUSY);
    597 
    598 	s = spltty();
    599 
    600 	/*
    601 	 * Do the following iff this is a first open.
    602 	 */
    603 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    604 		struct termios t;
    605 
    606 		tp->t_dev = dev;
    607 
    608 		/* Call the power management hook. */
    609 		if (cs->enable) {
    610 			if ((*cs->enable)(cs)) {
    611 				splx(s);
    612 				printf("%s: device enable failed\n",
    613 			       	zst->zst_dev.dv_xname);
    614 				return (EIO);
    615 			}
    616 		}
    617 
    618 		/*
    619 		 * Initialize the termios status to the defaults.  Add in the
    620 		 * sticky bits from TIOCSFLAGS.
    621 		 */
    622 		t.c_ispeed = 0;
    623 		t.c_ospeed = cs->cs_defspeed;
    624 		t.c_cflag = cs->cs_defcflag;
    625 		if (ISSET(zst->zst_swflags, TIOCFLAG_CLOCAL))
    626 			SET(t.c_cflag, CLOCAL);
    627 		if (ISSET(zst->zst_swflags, TIOCFLAG_CRTSCTS))
    628 			SET(t.c_cflag, CRTSCTS);
    629 		if (ISSET(zst->zst_swflags, TIOCFLAG_CDTRCTS))
    630 			SET(t.c_cflag, CDTRCTS);
    631 		if (ISSET(zst->zst_swflags, TIOCFLAG_MDMBUF))
    632 			SET(t.c_cflag, MDMBUF);
    633 
    634 		s2 = splzs();
    635 		simple_lock(&cs->cs_lock);
    636 
    637 		/*
    638 		 * Turn on receiver and status interrupts.
    639 		 * We defer the actual write of the register to zsparam(),
    640 		 * but we must make sure status interrupts are turned on by
    641 		 * the time zsparam() reads the initial rr0 state.
    642 		 */
    643 		SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_SIE);
    644 
    645 		/* Clear PPS capture state on first open. */
    646 		zst->zst_ppsmask = 0;
    647 #ifdef __HAVE_TIMECOUNTER
    648 		memset(&zst->zst_pps_state, 0, sizeof(zst->zst_pps_state));
    649 		zst->zst_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
    650 		pps_init(&zst->zst_pps_state);
    651 #else /* !__HAVE_TIMECOUNTER */
    652 		zst->ppsparam.mode = 0;
    653 #endif /* !__HAVE_TIMECOUNTER */
    654 
    655 		simple_unlock(&cs->cs_lock);
    656 		splx(s2);
    657 
    658 		/* Make sure zsparam will see changes. */
    659 		tp->t_ospeed = 0;
    660 		(void) zsparam(tp, &t);
    661 
    662 		/*
    663 		 * Note: zsparam has done: cflag, ispeed, ospeed
    664 		 * so we just need to do: iflag, oflag, lflag, cc
    665 		 * For "raw" mode, just leave all zeros.
    666 		 */
    667 		if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_RAW)) {
    668 			tp->t_iflag = TTYDEF_IFLAG;
    669 			tp->t_oflag = TTYDEF_OFLAG;
    670 			tp->t_lflag = TTYDEF_LFLAG;
    671 		} else {
    672 			tp->t_iflag = 0;
    673 			tp->t_oflag = 0;
    674 			tp->t_lflag = 0;
    675 		}
    676 		ttychars(tp);
    677 		ttsetwater(tp);
    678 
    679 		s2 = splzs();
    680 		simple_lock(&cs->cs_lock);
    681 
    682 		/*
    683 		 * Turn on DTR.  We must always do this, even if carrier is not
    684 		 * present, because otherwise we'd have to use TIOCSDTR
    685 		 * immediately after setting CLOCAL, which applications do not
    686 		 * expect.  We always assert DTR while the device is open
    687 		 * unless explicitly requested to deassert it.
    688 		 */
    689 		zs_modem(zst, 1);
    690 
    691 		/* Clear the input ring, and unblock. */
    692 		zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
    693 		zst->zst_rbavail = zstty_rbuf_size;
    694 		zs_iflush(cs);
    695 		CLR(zst->zst_rx_flags, RX_ANY_BLOCK);
    696 		zs_hwiflow(zst);
    697 
    698 		simple_unlock(&cs->cs_lock);
    699 		splx(s2);
    700 	}
    701 
    702 	splx(s);
    703 
    704 	error = ttyopen(tp, ZSDIALOUT(dev), ISSET(flags, O_NONBLOCK));
    705 	if (error)
    706 		goto bad;
    707 
    708 	error = (*tp->t_linesw->l_open)(dev, tp);
    709 	if (error)
    710 		goto bad;
    711 
    712 	return (0);
    713 
    714 bad:
    715 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    716 		/*
    717 		 * We failed to open the device, and nobody else had it opened.
    718 		 * Clean up the state as appropriate.
    719 		 */
    720 		zs_shutdown(zst);
    721 	}
    722 
    723 	return (error);
    724 }
    725 
    726 /*
    727  * Close a zs serial port.
    728  */
    729 int
    730 zsclose(dev, flags, mode, l)
    731 	dev_t dev;
    732 	int flags;
    733 	int mode;
    734 	struct lwp *l;
    735 {
    736 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev));
    737 	struct tty *tp = zst->zst_tty;
    738 
    739 	/* XXX This is for cons.c. */
    740 	if (!ISSET(tp->t_state, TS_ISOPEN))
    741 		return 0;
    742 
    743 	(*tp->t_linesw->l_close)(tp, flags);
    744 	ttyclose(tp);
    745 
    746 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    747 		/*
    748 		 * Although we got a last close, the device may still be in
    749 		 * use; e.g. if this was the dialout node, and there are still
    750 		 * processes waiting for carrier on the non-dialout node.
    751 		 */
    752 		zs_shutdown(zst);
    753 	}
    754 
    755 	return (0);
    756 }
    757 
    758 /*
    759  * Read/write zs serial port.
    760  */
    761 int
    762 zsread(dev, uio, flags)
    763 	dev_t dev;
    764 	struct uio *uio;
    765 	int flags;
    766 {
    767 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev));
    768 	struct tty *tp = zst->zst_tty;
    769 
    770 	return ((*tp->t_linesw->l_read)(tp, uio, flags));
    771 }
    772 
    773 int
    774 zswrite(dev, uio, flags)
    775 	dev_t dev;
    776 	struct uio *uio;
    777 	int flags;
    778 {
    779 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev));
    780 	struct tty *tp = zst->zst_tty;
    781 
    782 	return ((*tp->t_linesw->l_write)(tp, uio, flags));
    783 }
    784 
    785 int
    786 zspoll(dev, events, l)
    787 	dev_t dev;
    788 	int events;
    789 	struct lwp *l;
    790 {
    791 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev));
    792 	struct tty *tp = zst->zst_tty;
    793 
    794 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    795 }
    796 
    797 int
    798 zsioctl(dev, cmd, data, flag, l)
    799 	dev_t dev;
    800 	u_long cmd;
    801 	caddr_t data;
    802 	int flag;
    803 	struct lwp *l;
    804 {
    805 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(dev));
    806 	struct zs_chanstate *cs = zst->zst_cs;
    807 	struct tty *tp = zst->zst_tty;
    808 	struct proc *p = l->l_proc;
    809 	int error;
    810 	int s;
    811 
    812 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    813 	if (error != EPASSTHROUGH)
    814 		return (error);
    815 
    816 	error = ttioctl(tp, cmd, data, flag, l);
    817 	if (error != EPASSTHROUGH)
    818 		return (error);
    819 
    820 #ifdef	ZS_MD_IOCTL
    821 	error = ZS_MD_IOCTL(cs, cmd, data);
    822 	if (error != EPASSTHROUGH)
    823 		return (error);
    824 #endif	/* ZS_MD_IOCTL */
    825 
    826 	error = 0;
    827 
    828 	s = splzs();
    829 	simple_lock(&cs->cs_lock);
    830 
    831 	switch (cmd) {
    832 	case TIOCSBRK:
    833 		zs_break(cs, 1);
    834 		break;
    835 
    836 	case TIOCCBRK:
    837 		zs_break(cs, 0);
    838 		break;
    839 
    840 	case TIOCGFLAGS:
    841 		*(int *)data = zst->zst_swflags;
    842 		break;
    843 
    844 	case TIOCSFLAGS:
    845 		error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
    846 					  &p->p_acflag);
    847 		if (error)
    848 			break;
    849 		zst->zst_swflags = *(int *)data;
    850 		break;
    851 
    852 	case TIOCSDTR:
    853 		zs_modem(zst, 1);
    854 		break;
    855 
    856 	case TIOCCDTR:
    857 		zs_modem(zst, 0);
    858 		break;
    859 
    860 	case TIOCMSET:
    861 	case TIOCMBIS:
    862 	case TIOCMBIC:
    863 		tiocm_to_zs(zst, cmd, *(int *)data);
    864 		break;
    865 
    866 	case TIOCMGET:
    867 		*(int *)data = zs_to_tiocm(zst);
    868 		break;
    869 
    870 #ifdef __HAVE_TIMECOUNTER
    871 	case PPS_IOC_CREATE:
    872 	case PPS_IOC_DESTROY:
    873 	case PPS_IOC_GETPARAMS:
    874 	case PPS_IOC_SETPARAMS:
    875 	case PPS_IOC_GETCAP:
    876 	case PPS_IOC_FETCH:
    877 #ifdef PPS_SYNC
    878 	case PPS_IOC_KCBIND:
    879 #endif
    880 		error = pps_ioctl(cmd, data, &zst->zst_pps_state);
    881 		if (zst->zst_pps_state.ppsparam.mode & PPS_CAPTUREBOTH)
    882 			zst->zst_ppsmask = ZSRR0_DCD;
    883 		else
    884 			zst->zst_ppsmask = 0;
    885 		break;
    886 #else /* !__HAVE_TIMECOUNTER */
    887 	case PPS_IOC_CREATE:
    888 		break;
    889 
    890 	case PPS_IOC_DESTROY:
    891 		break;
    892 
    893 	case PPS_IOC_GETPARAMS: {
    894 		pps_params_t *pp;
    895 		pp = (pps_params_t *)data;
    896 		*pp = zst->ppsparam;
    897 		break;
    898 	}
    899 
    900 	case PPS_IOC_SETPARAMS: {
    901 		pps_params_t *pp;
    902 		int mode;
    903 		if (cs->cs_rr0_pps == 0) {
    904 			error = EINVAL;
    905 			break;
    906 		}
    907 		pp = (pps_params_t *)data;
    908 		if (pp->mode & ~zsppscap) {
    909 			error = EINVAL;
    910 			break;
    911 		}
    912 		zst->ppsparam = *pp;
    913 		/*
    914 		 * compute masks from user-specified timestamp state.
    915 		 */
    916 		mode = zst->ppsparam.mode;
    917 		switch (mode & PPS_CAPTUREBOTH) {
    918 		case 0:
    919 			zst->zst_ppsmask = 0;
    920 			break;
    921 
    922 		case PPS_CAPTUREASSERT:
    923 			zst->zst_ppsmask = ZSRR0_DCD;
    924 			zst->zst_ppsassert = ZSRR0_DCD;
    925 			zst->zst_ppsclear = -1;
    926 			break;
    927 
    928 		case PPS_CAPTURECLEAR:
    929 			zst->zst_ppsmask = ZSRR0_DCD;
    930 			zst->zst_ppsassert = -1;
    931 			zst->zst_ppsclear = 0;
    932 			break;
    933 
    934 		case PPS_CAPTUREBOTH:
    935 			zst->zst_ppsmask = ZSRR0_DCD;
    936 			zst->zst_ppsassert = ZSRR0_DCD;
    937 			zst->zst_ppsclear = 0;
    938 			break;
    939 
    940 		default:
    941 			error = EINVAL;
    942 			break;
    943 		}
    944 
    945 		/*
    946 		 * Now update interrupts.
    947 		 */
    948 		zs_maskintr(zst);
    949 		/*
    950 		 * If nothing is being transmitted, set up new current values,
    951 		 * else mark them as pending.
    952 		 */
    953 		if (!cs->cs_heldchange) {
    954 			if (zst->zst_tx_busy) {
    955 				zst->zst_heldtbc = zst->zst_tbc;
    956 				zst->zst_tbc = 0;
    957 				cs->cs_heldchange = 1;
    958 			} else
    959 				zs_loadchannelregs(cs);
    960 		}
    961 
    962 		break;
    963 	}
    964 
    965 	case PPS_IOC_GETCAP:
    966 		*(int *)data = zsppscap;
    967 		break;
    968 
    969 	case PPS_IOC_FETCH: {
    970 		pps_info_t *pi;
    971 		pi = (pps_info_t *)data;
    972 		*pi = zst->ppsinfo;
    973 		break;
    974 	}
    975 
    976 #ifdef PPS_SYNC
    977 	case PPS_IOC_KCBIND: {
    978 		int edge = (*(int *)data) & PPS_CAPTUREBOTH;
    979 
    980 		if (edge == 0) {
    981 			/*
    982 			 * remove binding for this source; ignore
    983 			 * the request if this is not the current
    984 			 * hardpps source
    985 			 */
    986 			if (pps_kc_hardpps_source == zst) {
    987 				pps_kc_hardpps_source = NULL;
    988 				pps_kc_hardpps_mode = 0;
    989 			}
    990 		} else {
    991 			/*
    992 			 * bind hardpps to this source, replacing any
    993 			 * previously specified source or edges
    994 			 */
    995 			pps_kc_hardpps_source = zst;
    996 			pps_kc_hardpps_mode = edge;
    997 		}
    998 		break;
    999 	}
   1000 #endif /* PPS_SYNC */
   1001 #endif /* !__HAVE_TIMECOUNTER */
   1002 
   1003 	case TIOCDCDTIMESTAMP:	/* XXX old, overloaded  API used by xntpd v3 */
   1004 		if (cs->cs_rr0_pps == 0) {
   1005 			error = EINVAL;
   1006 			break;
   1007 		}
   1008 #ifdef __HAVE_TIMECOUNTER
   1009 #ifndef PPS_TRAILING_EDGE
   1010 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1011 		    &zst->zst_pps_state.ppsinfo.assert_timestamp);
   1012 #else
   1013 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1014 		    &zst->zst_pps_state.ppsinfo.clear_timestamp);
   1015 #endif
   1016 #else /* !__HAVE_TIMECOUNTER */
   1017 		zst->zst_ppsmask = ZSRR0_DCD;
   1018 #ifndef	PPS_TRAILING_EDGE
   1019 		zst->zst_ppsassert = ZSRR0_DCD;
   1020 		zst->zst_ppsclear = -1;
   1021 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1022 			&zst->ppsinfo.assert_timestamp);
   1023 #else
   1024 		zst->zst_ppsassert = -1;
   1025 		zst->zst_ppsclear = 01;
   1026 		TIMESPEC_TO_TIMEVAL((struct timeval *)data,
   1027 			&zst->ppsinfo.clear_timestamp);
   1028 #endif
   1029 #endif /* !__HAVE_TIMECOUNTER */
   1030 		/*
   1031 		 * Now update interrupts.
   1032 		 */
   1033 		zs_maskintr(zst);
   1034 		/*
   1035 		 * If nothing is being transmitted, set up new current values,
   1036 		 * else mark them as pending.
   1037 		 */
   1038 		if (!cs->cs_heldchange) {
   1039 			if (zst->zst_tx_busy) {
   1040 				zst->zst_heldtbc = zst->zst_tbc;
   1041 				zst->zst_tbc = 0;
   1042 				cs->cs_heldchange = 1;
   1043 			} else
   1044 				zs_loadchannelregs(cs);
   1045 		}
   1046 
   1047 		break;
   1048 
   1049 	default:
   1050 		error = EPASSTHROUGH;
   1051 		break;
   1052 	}
   1053 
   1054 	simple_unlock(&cs->cs_lock);
   1055 	splx(s);
   1056 
   1057 	return (error);
   1058 }
   1059 
   1060 /*
   1061  * Start or restart transmission.
   1062  */
   1063 static void
   1064 zsstart(tp)
   1065 	struct tty *tp;
   1066 {
   1067 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
   1068 	struct zs_chanstate *cs = zst->zst_cs;
   1069 	u_char *tba;
   1070 	int s, tbc;
   1071 
   1072 	s = spltty();
   1073 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1074 		goto out;
   1075 	if (zst->zst_tx_stopped)
   1076 		goto out;
   1077 
   1078 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1079 		if (ISSET(tp->t_state, TS_ASLEEP)) {
   1080 			CLR(tp->t_state, TS_ASLEEP);
   1081 			wakeup((caddr_t)&tp->t_outq);
   1082 		}
   1083 		selwakeup(&tp->t_wsel);
   1084 		if (tp->t_outq.c_cc == 0)
   1085 			goto out;
   1086 	}
   1087 
   1088 	/* Grab the first contiguous region of buffer space. */
   1089 	tba = tp->t_outq.c_cf;
   1090 	tbc = ndqb(&tp->t_outq, 0);
   1091 
   1092 	(void) splzs();
   1093 	simple_lock(&cs->cs_lock);
   1094 
   1095 	zst->zst_tba = tba;
   1096 	zst->zst_tbc = tbc;
   1097 	SET(tp->t_state, TS_BUSY);
   1098 	zst->zst_tx_busy = 1;
   1099 
   1100 #ifdef ZS_TXDMA
   1101 	if (zst->zst_tbc > 1) {
   1102 		zs_dma_setup(cs, zst->zst_tba, zst->zst_tbc);
   1103 		goto out;
   1104 	}
   1105 #endif
   1106 
   1107 	/* Enable transmit completion interrupts if necessary. */
   1108 	if (!ISSET(cs->cs_preg[1], ZSWR1_TIE)) {
   1109 		SET(cs->cs_preg[1], ZSWR1_TIE);
   1110 		cs->cs_creg[1] = cs->cs_preg[1];
   1111 		zs_write_reg(cs, 1, cs->cs_creg[1]);
   1112 	}
   1113 
   1114 	/* Output the first character of the contiguous buffer. */
   1115 	zs_write_data(cs, *zst->zst_tba);
   1116 	zst->zst_tbc--;
   1117 	zst->zst_tba++;
   1118 
   1119 	simple_unlock(&cs->cs_lock);
   1120 out:
   1121 	splx(s);
   1122 	return;
   1123 }
   1124 
   1125 /*
   1126  * Stop output, e.g., for ^S or output flush.
   1127  */
   1128 void
   1129 zsstop(tp, flag)
   1130 	struct tty *tp;
   1131 	int flag;
   1132 {
   1133 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
   1134 	int s;
   1135 
   1136 	s = splzs();
   1137 	if (ISSET(tp->t_state, TS_BUSY)) {
   1138 		/* Stop transmitting at the next chunk. */
   1139 		zst->zst_tbc = 0;
   1140 		zst->zst_heldtbc = 0;
   1141 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1142 			SET(tp->t_state, TS_FLUSH);
   1143 	}
   1144 	splx(s);
   1145 }
   1146 
   1147 /*
   1148  * Set ZS tty parameters from termios.
   1149  * XXX - Should just copy the whole termios after
   1150  * making sure all the changes could be done.
   1151  */
   1152 static int
   1153 zsparam(tp, t)
   1154 	struct tty *tp;
   1155 	struct termios *t;
   1156 {
   1157 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
   1158 	struct zs_chanstate *cs = zst->zst_cs;
   1159 	int ospeed;
   1160 	tcflag_t cflag;
   1161 	u_char tmp3, tmp4, tmp5;
   1162 	int s, error;
   1163 
   1164 	ospeed = t->c_ospeed;
   1165 	cflag = t->c_cflag;
   1166 
   1167 	/* Check requested parameters. */
   1168 	if (ospeed < 0)
   1169 		return (EINVAL);
   1170 	if (t->c_ispeed && t->c_ispeed != ospeed)
   1171 		return (EINVAL);
   1172 
   1173 	/*
   1174 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1175 	 * is always active.
   1176 	 */
   1177 	if (ISSET(zst->zst_swflags, TIOCFLAG_SOFTCAR) ||
   1178 	    ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
   1179 		SET(cflag, CLOCAL);
   1180 		CLR(cflag, HUPCL);
   1181 	}
   1182 
   1183 	/*
   1184 	 * Only whack the UART when params change.
   1185 	 * Some callers need to clear tp->t_ospeed
   1186 	 * to make sure initialization gets done.
   1187 	 */
   1188 	if (tp->t_ospeed == ospeed &&
   1189 	    tp->t_cflag == cflag)
   1190 		return (0);
   1191 
   1192 	/*
   1193 	 * Call MD functions to deal with changed
   1194 	 * clock modes or H/W flow control modes.
   1195 	 * The BRG divisor is set now. (reg 12,13)
   1196 	 */
   1197 	error = zs_set_speed(cs, ospeed);
   1198 	if (error)
   1199 		return (error);
   1200 	error = zs_set_modes(cs, cflag);
   1201 	if (error)
   1202 		return (error);
   1203 
   1204 	/*
   1205 	 * Block interrupts so that state will not
   1206 	 * be altered until we are done setting it up.
   1207 	 *
   1208 	 * Initial values in cs_preg are set before
   1209 	 * our attach routine is called.  The master
   1210 	 * interrupt enable is handled by zsc.c
   1211 	 *
   1212 	 */
   1213 	s = splzs();
   1214 	simple_lock(&cs->cs_lock);
   1215 
   1216 	/*
   1217 	 * Recalculate which status ints to enable.
   1218 	 */
   1219 	zs_maskintr(zst);
   1220 
   1221 	/* Recompute character size bits. */
   1222 	tmp3 = cs->cs_preg[3];
   1223 	tmp5 = cs->cs_preg[5];
   1224 	CLR(tmp3, ZSWR3_RXSIZE);
   1225 	CLR(tmp5, ZSWR5_TXSIZE);
   1226 	switch (ISSET(cflag, CSIZE)) {
   1227 	case CS5:
   1228 		SET(tmp3, ZSWR3_RX_5);
   1229 		SET(tmp5, ZSWR5_TX_5);
   1230 		break;
   1231 	case CS6:
   1232 		SET(tmp3, ZSWR3_RX_6);
   1233 		SET(tmp5, ZSWR5_TX_6);
   1234 		break;
   1235 	case CS7:
   1236 		SET(tmp3, ZSWR3_RX_7);
   1237 		SET(tmp5, ZSWR5_TX_7);
   1238 		break;
   1239 	case CS8:
   1240 		SET(tmp3, ZSWR3_RX_8);
   1241 		SET(tmp5, ZSWR5_TX_8);
   1242 		break;
   1243 	}
   1244 	cs->cs_preg[3] = tmp3;
   1245 	cs->cs_preg[5] = tmp5;
   1246 
   1247 	/*
   1248 	 * Recompute the stop bits and parity bits.  Note that
   1249 	 * zs_set_speed() may have set clock selection bits etc.
   1250 	 * in wr4, so those must preserved.
   1251 	 */
   1252 	tmp4 = cs->cs_preg[4];
   1253 	CLR(tmp4, ZSWR4_SBMASK | ZSWR4_PARMASK);
   1254 	if (ISSET(cflag, CSTOPB))
   1255 		SET(tmp4, ZSWR4_TWOSB);
   1256 	else
   1257 		SET(tmp4, ZSWR4_ONESB);
   1258 	if (!ISSET(cflag, PARODD))
   1259 		SET(tmp4, ZSWR4_EVENP);
   1260 	if (ISSET(cflag, PARENB))
   1261 		SET(tmp4, ZSWR4_PARENB);
   1262 	cs->cs_preg[4] = tmp4;
   1263 
   1264 	/* And copy to tty. */
   1265 	tp->t_ispeed = 0;
   1266 	tp->t_ospeed = ospeed;
   1267 	tp->t_cflag = cflag;
   1268 
   1269 	/*
   1270 	 * If nothing is being transmitted, set up new current values,
   1271 	 * else mark them as pending.
   1272 	 */
   1273 	if (!cs->cs_heldchange) {
   1274 		if (zst->zst_tx_busy) {
   1275 			zst->zst_heldtbc = zst->zst_tbc;
   1276 			zst->zst_tbc = 0;
   1277 			cs->cs_heldchange = 1;
   1278 		} else
   1279 			zs_loadchannelregs(cs);
   1280 	}
   1281 
   1282 	/*
   1283 	 * If hardware flow control is disabled, turn off the buffer water
   1284 	 * marks and unblock any soft flow control state.  Otherwise, enable
   1285 	 * the water marks.
   1286 	 */
   1287 	if (!ISSET(cflag, CHWFLOW)) {
   1288 		zst->zst_r_hiwat = 0;
   1289 		zst->zst_r_lowat = 0;
   1290 		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
   1291 			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
   1292 			zst->zst_rx_ready = 1;
   1293 			cs->cs_softreq = 1;
   1294 		}
   1295 		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
   1296 			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
   1297 			zs_hwiflow(zst);
   1298 		}
   1299 	} else {
   1300 		zst->zst_r_hiwat = zstty_rbuf_hiwat;
   1301 		zst->zst_r_lowat = zstty_rbuf_lowat;
   1302 	}
   1303 
   1304 	/*
   1305 	 * Force a recheck of the hardware carrier and flow control status,
   1306 	 * since we may have changed which bits we're looking at.
   1307 	 */
   1308 	zstty_stint(cs, 1);
   1309 
   1310 	simple_unlock(&cs->cs_lock);
   1311 	splx(s);
   1312 
   1313 	/*
   1314 	 * If hardware flow control is disabled, unblock any hard flow control
   1315 	 * state.
   1316 	 */
   1317 	if (!ISSET(cflag, CHWFLOW)) {
   1318 		if (zst->zst_tx_stopped) {
   1319 			zst->zst_tx_stopped = 0;
   1320 			zsstart(tp);
   1321 		}
   1322 	}
   1323 
   1324 	zstty_softint(cs);
   1325 
   1326 	return (0);
   1327 }
   1328 
   1329 /*
   1330  * Compute interrupt enable bits and set in the pending bits. Called both
   1331  * in zsparam() and when PPS (pulse per second timing) state changes.
   1332  * Must be called at splzs().
   1333  */
   1334 static void
   1335 zs_maskintr(zst)
   1336 	struct zstty_softc *zst;
   1337 {
   1338 	struct zs_chanstate *cs = zst->zst_cs;
   1339 	int tmp15;
   1340 
   1341 	cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
   1342 	if (zst->zst_ppsmask != 0)
   1343 		cs->cs_rr0_mask |= cs->cs_rr0_pps;
   1344 	tmp15 = cs->cs_preg[15];
   1345 	if (ISSET(cs->cs_rr0_mask, ZSRR0_DCD))
   1346 		SET(tmp15, ZSWR15_DCD_IE);
   1347 	else
   1348 		CLR(tmp15, ZSWR15_DCD_IE);
   1349 	if (ISSET(cs->cs_rr0_mask, ZSRR0_CTS))
   1350 		SET(tmp15, ZSWR15_CTS_IE);
   1351 	else
   1352 		CLR(tmp15, ZSWR15_CTS_IE);
   1353 	cs->cs_preg[15] = tmp15;
   1354 }
   1355 
   1356 
   1357 /*
   1358  * Raise or lower modem control (DTR/RTS) signals.  If a character is
   1359  * in transmission, the change is deferred.
   1360  * Called at splzs() and with the channel lock held.
   1361  */
   1362 static void
   1363 zs_modem(zst, onoff)
   1364 	struct zstty_softc *zst;
   1365 	int onoff;
   1366 {
   1367 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
   1368 
   1369 	if (cs->cs_wr5_dtr == 0)
   1370 		return;
   1371 
   1372 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
   1373 
   1374 	if (onoff)
   1375 		SET(ccs->cs_preg[5], cs->cs_wr5_dtr);
   1376 	else
   1377 		CLR(ccs->cs_preg[5], cs->cs_wr5_dtr);
   1378 
   1379 	if (!cs->cs_heldchange) {
   1380 		if (zst->zst_tx_busy) {
   1381 			zst->zst_heldtbc = zst->zst_tbc;
   1382 			zst->zst_tbc = 0;
   1383 			cs->cs_heldchange = 1;
   1384 		} else
   1385 			zs_loadchannelregs(cs);
   1386 	}
   1387 }
   1388 
   1389 /*
   1390  * Set modem bits.
   1391  * Called at splzs() and with the channel lock held.
   1392  */
   1393 static void
   1394 tiocm_to_zs(zst, how, ttybits)
   1395 	struct zstty_softc *zst;
   1396 	u_long how;
   1397 	int ttybits;
   1398 {
   1399 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
   1400 	u_char zsbits;
   1401 
   1402 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
   1403 
   1404 	zsbits = 0;
   1405 	if (ISSET(ttybits, TIOCM_DTR))
   1406 		SET(zsbits, ZSWR5_DTR);
   1407 	if (ISSET(ttybits, TIOCM_RTS))
   1408 		SET(zsbits, ZSWR5_RTS);
   1409 
   1410 	switch (how) {
   1411 	case TIOCMBIC:
   1412 		CLR(ccs->cs_preg[5], zsbits);
   1413 		break;
   1414 
   1415 	case TIOCMBIS:
   1416 		SET(ccs->cs_preg[5], zsbits);
   1417 		break;
   1418 
   1419 	case TIOCMSET:
   1420 		CLR(ccs->cs_preg[5], ZSWR5_RTS | ZSWR5_DTR);
   1421 		SET(ccs->cs_preg[5], zsbits);
   1422 		break;
   1423 	}
   1424 
   1425 	if (!cs->cs_heldchange) {
   1426 		if (zst->zst_tx_busy) {
   1427 			zst->zst_heldtbc = zst->zst_tbc;
   1428 			zst->zst_tbc = 0;
   1429 			cs->cs_heldchange = 1;
   1430 		} else
   1431 			zs_loadchannelregs(cs);
   1432 	}
   1433 }
   1434 
   1435 /*
   1436  * Get modem bits.
   1437  * Called at splzs() and with the channel lock held.
   1438  */
   1439 static int
   1440 zs_to_tiocm(zst)
   1441 	struct zstty_softc *zst;
   1442 {
   1443 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
   1444 	u_char zsbits;
   1445 	int ttybits = 0;
   1446 
   1447 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
   1448 
   1449 	zsbits = ccs->cs_preg[5];
   1450 	if (ISSET(zsbits, ZSWR5_DTR))
   1451 		SET(ttybits, TIOCM_DTR);
   1452 	if (ISSET(zsbits, ZSWR5_RTS))
   1453 		SET(ttybits, TIOCM_RTS);
   1454 
   1455 	zsbits = cs->cs_rr0;
   1456 	if (ISSET(zsbits, ZSRR0_DCD))
   1457 		SET(ttybits, TIOCM_CD);
   1458 	if (ISSET(zsbits, ZSRR0_CTS))
   1459 		SET(ttybits, TIOCM_CTS);
   1460 
   1461 	return (ttybits);
   1462 }
   1463 
   1464 /*
   1465  * Try to block or unblock input using hardware flow-control.
   1466  * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
   1467  * if this function returns non-zero, the TS_TBLOCK flag will
   1468  * be set or cleared according to the "block" arg passed.
   1469  */
   1470 int
   1471 zshwiflow(tp, block)
   1472 	struct tty *tp;
   1473 	int block;
   1474 {
   1475 	struct zstty_softc *zst = device_lookup(&zstty_cd, ZSUNIT(tp->t_dev));
   1476 	struct zs_chanstate *cs = zst->zst_cs;
   1477 	int s;
   1478 
   1479 	if (cs->cs_wr5_rts == 0)
   1480 		return (0);
   1481 
   1482 	s = splzs();
   1483 	simple_lock(&cs->cs_lock);
   1484 	if (block) {
   1485 		if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
   1486 			SET(zst->zst_rx_flags, RX_TTY_BLOCKED);
   1487 			zs_hwiflow(zst);
   1488 		}
   1489 	} else {
   1490 		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
   1491 			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
   1492 			zst->zst_rx_ready = 1;
   1493 			cs->cs_softreq = 1;
   1494 		}
   1495 		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
   1496 			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED);
   1497 			zs_hwiflow(zst);
   1498 		}
   1499 	}
   1500 	simple_unlock(&cs->cs_lock);
   1501 	splx(s);
   1502 	return (1);
   1503 }
   1504 
   1505 /*
   1506  * Internal version of zshwiflow
   1507  * Called at splzs() and with the channel lock held.
   1508  */
   1509 static void
   1510 zs_hwiflow(zst)
   1511 	struct zstty_softc *zst;
   1512 {
   1513 	struct zs_chanstate *cs = zst->zst_cs, *ccs;
   1514 
   1515 	if (cs->cs_wr5_rts == 0)
   1516 		return;
   1517 
   1518 	ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
   1519 
   1520 	if (ISSET(zst->zst_rx_flags, RX_ANY_BLOCK)) {
   1521 		CLR(ccs->cs_preg[5], cs->cs_wr5_rts);
   1522 		CLR(ccs->cs_creg[5], cs->cs_wr5_rts);
   1523 	} else {
   1524 		SET(ccs->cs_preg[5], cs->cs_wr5_rts);
   1525 		SET(ccs->cs_creg[5], cs->cs_wr5_rts);
   1526 	}
   1527 	zs_write_reg(ccs, 5, ccs->cs_creg[5]);
   1528 }
   1529 
   1530 
   1531 /****************************************************************
   1532  * Interface to the lower layer (zscc)
   1533  ****************************************************************/
   1534 
   1535 #define	integrate	static inline
   1536 integrate void zstty_rxsoft(struct zstty_softc *, struct tty *);
   1537 integrate void zstty_txsoft(struct zstty_softc *, struct tty *);
   1538 integrate void zstty_stsoft(struct zstty_softc *, struct tty *);
   1539 static void zstty_diag(void *);
   1540 
   1541 /*
   1542  * Receiver Ready interrupt.
   1543  * Called at splzs() and with the channel lock held.
   1544  */
   1545 static void
   1546 zstty_rxint(cs)
   1547 	struct zs_chanstate *cs;
   1548 {
   1549 	struct zstty_softc *zst = cs->cs_private;
   1550 	u_char *put, *end;
   1551 	u_int cc;
   1552 	u_char rr0, rr1, c;
   1553 
   1554 	end = zst->zst_ebuf;
   1555 	put = zst->zst_rbput;
   1556 	cc = zst->zst_rbavail;
   1557 
   1558 	while (cc > 0) {
   1559 		/*
   1560 		 * First read the status, because reading the received char
   1561 		 * destroys the status of this char.
   1562 		 */
   1563 		rr1 = zs_read_reg(cs, 1);
   1564 		c = zs_read_data(cs);
   1565 
   1566 		if (ISSET(rr1, ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
   1567 			/* Clear the receive error. */
   1568 			zs_write_csr(cs, ZSWR0_RESET_ERRORS);
   1569 		}
   1570 
   1571 		cn_check_magic(zst->zst_tty->t_dev, c, zstty_cnm_state);
   1572 		put[0] = c;
   1573 		put[1] = rr1;
   1574 		put += 2;
   1575 		if (put >= end)
   1576 			put = zst->zst_rbuf;
   1577 		cc--;
   1578 
   1579 		rr0 = zs_read_csr(cs);
   1580 		if (!ISSET(rr0, ZSRR0_RX_READY))
   1581 			break;
   1582 	}
   1583 
   1584 	/*
   1585 	 * Current string of incoming characters ended because
   1586 	 * no more data was available or we ran out of space.
   1587 	 * Schedule a receive event if any data was received.
   1588 	 * If we're out of space, turn off receive interrupts.
   1589 	 */
   1590 	zst->zst_rbput = put;
   1591 	zst->zst_rbavail = cc;
   1592 	if (!ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
   1593 		zst->zst_rx_ready = 1;
   1594 		cs->cs_softreq = 1;
   1595 	}
   1596 
   1597 	/*
   1598 	 * See if we are in danger of overflowing a buffer. If
   1599 	 * so, use hardware flow control to ease the pressure.
   1600 	 */
   1601 	if (!ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED) &&
   1602 	    cc < zst->zst_r_hiwat) {
   1603 		SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
   1604 		zs_hwiflow(zst);
   1605 	}
   1606 
   1607 	/*
   1608 	 * If we're out of space, disable receive interrupts
   1609 	 * until the queue has drained a bit.
   1610 	 */
   1611 	if (!cc) {
   1612 		SET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
   1613 		CLR(cs->cs_preg[1], ZSWR1_RIE);
   1614 		cs->cs_creg[1] = cs->cs_preg[1];
   1615 		zs_write_reg(cs, 1, cs->cs_creg[1]);
   1616 	}
   1617 
   1618 #if 0
   1619 	printf("%xH%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
   1620 #endif
   1621 }
   1622 
   1623 /*
   1624  * Transmitter Ready interrupt.
   1625  * Called at splzs() and with the channel lock held.
   1626  */
   1627 static void
   1628 zstty_txint(cs)
   1629 	struct zs_chanstate *cs;
   1630 {
   1631 	struct zstty_softc *zst = cs->cs_private;
   1632 
   1633 	/*
   1634 	 * If we've delayed a parameter change, do it now, and restart
   1635 	 * output.
   1636 	 */
   1637 	if (cs->cs_heldchange) {
   1638 		zs_loadchannelregs(cs);
   1639 		cs->cs_heldchange = 0;
   1640 		zst->zst_tbc = zst->zst_heldtbc;
   1641 		zst->zst_heldtbc = 0;
   1642 	}
   1643 
   1644 	/* Output the next character in the buffer, if any. */
   1645 	if (zst->zst_tbc > 0) {
   1646 		zs_write_data(cs, *zst->zst_tba);
   1647 		zst->zst_tbc--;
   1648 		zst->zst_tba++;
   1649 	} else {
   1650 		/* Disable transmit completion interrupts if necessary. */
   1651 		if (ISSET(cs->cs_preg[1], ZSWR1_TIE)) {
   1652 			CLR(cs->cs_preg[1], ZSWR1_TIE);
   1653 			cs->cs_creg[1] = cs->cs_preg[1];
   1654 			zs_write_reg(cs, 1, cs->cs_creg[1]);
   1655 		}
   1656 		if (zst->zst_tx_busy) {
   1657 			zst->zst_tx_busy = 0;
   1658 			zst->zst_tx_done = 1;
   1659 			cs->cs_softreq = 1;
   1660 		}
   1661 	}
   1662 }
   1663 
   1664 /*
   1665  * Status Change interrupt.
   1666  * Called at splzs() and with the channel lock held.
   1667  */
   1668 static void
   1669 zstty_stint(cs, force)
   1670 	struct zs_chanstate *cs;
   1671 	int force;
   1672 {
   1673 	struct zstty_softc *zst = cs->cs_private;
   1674 	u_char rr0, delta;
   1675 
   1676 	rr0 = zs_read_csr(cs);
   1677 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
   1678 
   1679 	/*
   1680 	 * Check here for console break, so that we can abort
   1681 	 * even when interrupts are locking up the machine.
   1682 	 */
   1683 	if (ISSET(rr0, ZSRR0_BREAK))
   1684 		cn_check_magic(zst->zst_tty->t_dev, CNC_BREAK, zstty_cnm_state);
   1685 
   1686 	if (!force)
   1687 		delta = rr0 ^ cs->cs_rr0;
   1688 	else
   1689 		delta = cs->cs_rr0_mask;
   1690 	cs->cs_rr0 = rr0;
   1691 
   1692 	if (ISSET(delta, cs->cs_rr0_mask)) {
   1693 		SET(cs->cs_rr0_delta, delta);
   1694 
   1695 		/*
   1696 		 * Pulse-per-second clock signal on edge of DCD?
   1697 		 */
   1698 		if (ISSET(delta, zst->zst_ppsmask)) {
   1699 #ifdef __HAVE_TIMECOUNTER
   1700 			if (zst->zst_pps_state.ppsparam.mode & PPS_CAPTUREBOTH) {
   1701 				pps_capture(&zst->zst_pps_state);
   1702 				pps_event(&zst->zst_pps_state,
   1703 				    (ISSET(cs->cs_rr0, zst->zst_ppsmask))
   1704 				    ? PPS_CAPTUREASSERT
   1705 				    : PPS_CAPTURECLEAR);
   1706 			}
   1707 #else /* !__HAVE_TIMECOUNTER */
   1708 			struct timeval tv;
   1709 			if (ISSET(rr0, zst->zst_ppsmask) == zst->zst_ppsassert) {
   1710 				/* XXX nanotime() */
   1711 				microtime(&tv);
   1712 				TIMEVAL_TO_TIMESPEC(&tv,
   1713 					&zst->ppsinfo.assert_timestamp);
   1714 				if (zst->ppsparam.mode & PPS_OFFSETASSERT) {
   1715 					timespecadd(&zst->ppsinfo.assert_timestamp,
   1716 					    &zst->ppsparam.assert_offset,
   1717 					    &zst->ppsinfo.assert_timestamp);
   1718 				}
   1719 
   1720 #ifdef PPS_SYNC
   1721 				if (pps_kc_hardpps_source == zst &&
   1722 				    pps_kc_hardpps_mode & PPS_CAPTUREASSERT) {
   1723 					hardpps(&tv, tv.tv_usec);
   1724 				}
   1725 #endif
   1726 				zst->ppsinfo.assert_sequence++;
   1727 				zst->ppsinfo.current_mode = zst->ppsparam.mode;
   1728 			} else if (ISSET(rr0, zst->zst_ppsmask) ==
   1729 						zst->zst_ppsclear) {
   1730 				/* XXX nanotime() */
   1731 				microtime(&tv);
   1732 				TIMEVAL_TO_TIMESPEC(&tv,
   1733 					&zst->ppsinfo.clear_timestamp);
   1734 				if (zst->ppsparam.mode & PPS_OFFSETCLEAR) {
   1735 					timespecadd(&zst->ppsinfo.clear_timestamp,
   1736 						&zst->ppsparam.clear_offset,
   1737 						&zst->ppsinfo.clear_timestamp);
   1738 				}
   1739 
   1740 #ifdef PPS_SYNC
   1741 				if (pps_kc_hardpps_source == zst &&
   1742 				    pps_kc_hardpps_mode & PPS_CAPTURECLEAR) {
   1743 					hardpps(&tv, tv.tv_usec);
   1744 				}
   1745 #endif
   1746 				zst->ppsinfo.clear_sequence++;
   1747 				zst->ppsinfo.current_mode = zst->ppsparam.mode;
   1748 			}
   1749 #endif /* !__HAVE_TIMECOUNTER */
   1750 		}
   1751 
   1752 		/*
   1753 		 * Stop output immediately if we lose the output
   1754 		 * flow control signal or carrier detect.
   1755 		 */
   1756 		if (ISSET(~rr0, cs->cs_rr0_mask)) {
   1757 			zst->zst_tbc = 0;
   1758 			zst->zst_heldtbc = 0;
   1759 		}
   1760 
   1761 		zst->zst_st_check = 1;
   1762 		cs->cs_softreq = 1;
   1763 	}
   1764 }
   1765 
   1766 void
   1767 zstty_diag(arg)
   1768 	void *arg;
   1769 {
   1770 	struct zstty_softc *zst = arg;
   1771 	int overflows, floods;
   1772 	int s;
   1773 
   1774 	s = splzs();
   1775 	overflows = zst->zst_overflows;
   1776 	zst->zst_overflows = 0;
   1777 	floods = zst->zst_floods;
   1778 	zst->zst_floods = 0;
   1779 	zst->zst_errors = 0;
   1780 	splx(s);
   1781 
   1782 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1783 	    zst->zst_dev.dv_xname,
   1784 	    overflows, overflows == 1 ? "" : "s",
   1785 	    floods, floods == 1 ? "" : "s");
   1786 }
   1787 
   1788 integrate void
   1789 zstty_rxsoft(zst, tp)
   1790 	struct zstty_softc *zst;
   1791 	struct tty *tp;
   1792 {
   1793 	struct zs_chanstate *cs = zst->zst_cs;
   1794 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
   1795 	u_char *get, *end;
   1796 	u_int cc, scc;
   1797 	u_char rr1;
   1798 	int code;
   1799 	int s;
   1800 
   1801 	end = zst->zst_ebuf;
   1802 	get = zst->zst_rbget;
   1803 	scc = cc = zstty_rbuf_size - zst->zst_rbavail;
   1804 
   1805 	if (cc == zstty_rbuf_size) {
   1806 		zst->zst_floods++;
   1807 		if (zst->zst_errors++ == 0)
   1808 			callout_reset(&zst->zst_diag_ch, 60 * hz,
   1809 			    zstty_diag, zst);
   1810 	}
   1811 
   1812 	/* If not yet open, drop the entire buffer content here */
   1813 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
   1814 		get += cc << 1;
   1815 		if (get >= end)
   1816 			get -= zstty_rbuf_size << 1;
   1817 		cc = 0;
   1818 	}
   1819 	while (cc) {
   1820 		code = get[0];
   1821 		rr1 = get[1];
   1822 		if (ISSET(rr1, ZSRR1_DO | ZSRR1_FE | ZSRR1_PE)) {
   1823 			if (ISSET(rr1, ZSRR1_DO)) {
   1824 				zst->zst_overflows++;
   1825 				if (zst->zst_errors++ == 0)
   1826 					callout_reset(&zst->zst_diag_ch,
   1827 					    60 * hz, zstty_diag, zst);
   1828 			}
   1829 			if (ISSET(rr1, ZSRR1_FE))
   1830 				SET(code, TTY_FE);
   1831 			if (ISSET(rr1, ZSRR1_PE))
   1832 				SET(code, TTY_PE);
   1833 		}
   1834 		if ((*rint)(code, tp) == -1) {
   1835 			/*
   1836 			 * The line discipline's buffer is out of space.
   1837 			 */
   1838 			if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
   1839 				/*
   1840 				 * We're either not using flow control, or the
   1841 				 * line discipline didn't tell us to block for
   1842 				 * some reason.  Either way, we have no way to
   1843 				 * know when there's more space available, so
   1844 				 * just drop the rest of the data.
   1845 				 */
   1846 				get += cc << 1;
   1847 				if (get >= end)
   1848 					get -= zstty_rbuf_size << 1;
   1849 				cc = 0;
   1850 			} else {
   1851 				/*
   1852 				 * Don't schedule any more receive processing
   1853 				 * until the line discipline tells us there's
   1854 				 * space available (through comhwiflow()).
   1855 				 * Leave the rest of the data in the input
   1856 				 * buffer.
   1857 				 */
   1858 				SET(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
   1859 			}
   1860 			break;
   1861 		}
   1862 		get += 2;
   1863 		if (get >= end)
   1864 			get = zst->zst_rbuf;
   1865 		cc--;
   1866 	}
   1867 
   1868 	if (cc != scc) {
   1869 		zst->zst_rbget = get;
   1870 		s = splzs();
   1871 		simple_lock(&cs->cs_lock);
   1872 		cc = zst->zst_rbavail += scc - cc;
   1873 		/* Buffers should be ok again, release possible block. */
   1874 		if (cc >= zst->zst_r_lowat) {
   1875 			if (ISSET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED)) {
   1876 				CLR(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
   1877 				SET(cs->cs_preg[1], ZSWR1_RIE);
   1878 				cs->cs_creg[1] = cs->cs_preg[1];
   1879 				zs_write_reg(cs, 1, cs->cs_creg[1]);
   1880 			}
   1881 			if (ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED)) {
   1882 				CLR(zst->zst_rx_flags, RX_IBUF_BLOCKED);
   1883 				zs_hwiflow(zst);
   1884 			}
   1885 		}
   1886 		simple_unlock(&cs->cs_lock);
   1887 		splx(s);
   1888 	}
   1889 
   1890 #if 0
   1891 	printf("%xS%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
   1892 #endif
   1893 }
   1894 
   1895 integrate void
   1896 zstty_txsoft(zst, tp)
   1897 	struct zstty_softc *zst;
   1898 	struct tty *tp;
   1899 {
   1900 	struct zs_chanstate *cs = zst->zst_cs;
   1901 	int s;
   1902 
   1903 	s = splzs();
   1904 	simple_lock(&cs->cs_lock);
   1905 	CLR(tp->t_state, TS_BUSY);
   1906 	if (ISSET(tp->t_state, TS_FLUSH))
   1907 		CLR(tp->t_state, TS_FLUSH);
   1908 	else
   1909 		ndflush(&tp->t_outq, (int)(zst->zst_tba - tp->t_outq.c_cf));
   1910 	simple_unlock(&cs->cs_lock);
   1911 	splx(s);
   1912 	(*tp->t_linesw->l_start)(tp);
   1913 }
   1914 
   1915 integrate void
   1916 zstty_stsoft(zst, tp)
   1917 	struct zstty_softc *zst;
   1918 	struct tty *tp;
   1919 {
   1920 	struct zs_chanstate *cs = zst->zst_cs;
   1921 	u_char rr0, delta;
   1922 	int s;
   1923 
   1924 	s = splzs();
   1925 	simple_lock(&cs->cs_lock);
   1926 	rr0 = cs->cs_rr0;
   1927 	delta = cs->cs_rr0_delta;
   1928 	cs->cs_rr0_delta = 0;
   1929 	simple_unlock(&cs->cs_lock);
   1930 	splx(s);
   1931 
   1932 	if (ISSET(delta, cs->cs_rr0_dcd)) {
   1933 		/*
   1934 		 * Inform the tty layer that carrier detect changed.
   1935 		 */
   1936 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(rr0, ZSRR0_DCD));
   1937 	}
   1938 
   1939 	if (ISSET(delta, cs->cs_rr0_cts)) {
   1940 		/* Block or unblock output according to flow control. */
   1941 		if (ISSET(rr0, cs->cs_rr0_cts)) {
   1942 			zst->zst_tx_stopped = 0;
   1943 			(*tp->t_linesw->l_start)(tp);
   1944 		} else {
   1945 			zst->zst_tx_stopped = 1;
   1946 		}
   1947 	}
   1948 }
   1949 
   1950 /*
   1951  * Software interrupt.  Called at zssoft
   1952  *
   1953  * The main job to be done here is to empty the input ring
   1954  * by passing its contents up to the tty layer.  The ring is
   1955  * always emptied during this operation, therefore the ring
   1956  * must not be larger than the space after "high water" in
   1957  * the tty layer, or the tty layer might drop our input.
   1958  *
   1959  * Note: an "input blockage" condition is assumed to exist if
   1960  * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
   1961  */
   1962 static void
   1963 zstty_softint(cs)
   1964 	struct zs_chanstate *cs;
   1965 {
   1966 	struct zstty_softc *zst = cs->cs_private;
   1967 	struct tty *tp = zst->zst_tty;
   1968 	int s;
   1969 
   1970 	s = spltty();
   1971 
   1972 	if (zst->zst_rx_ready) {
   1973 		zst->zst_rx_ready = 0;
   1974 		zstty_rxsoft(zst, tp);
   1975 	}
   1976 
   1977 	if (zst->zst_st_check) {
   1978 		zst->zst_st_check = 0;
   1979 		zstty_stsoft(zst, tp);
   1980 	}
   1981 
   1982 	if (zst->zst_tx_done) {
   1983 		zst->zst_tx_done = 0;
   1984 		zstty_txsoft(zst, tp);
   1985 	}
   1986 
   1987 	splx(s);
   1988 }
   1989 
   1990 struct zsops zsops_tty = {
   1991 	zstty_rxint,	/* receive char available */
   1992 	zstty_stint,	/* external/status */
   1993 	zstty_txint,	/* xmit buffer empty */
   1994 	zstty_softint,	/* process software interrupt */
   1995 };
   1996 
   1997 #ifdef ZS_TXDMA
   1998 void
   1999 zstty_txdma_int(arg)
   2000 	void *arg;
   2001 {
   2002 	struct zs_chanstate *cs = arg;
   2003 	struct zstty_softc *zst = cs->cs_private;
   2004 
   2005 	zst->zst_tba += zst->zst_tbc;
   2006 	zst->zst_tbc = 0;
   2007 
   2008 	if (zst->zst_tx_busy) {
   2009 		zst->zst_tx_busy = 0;
   2010 		zst->zst_tx_done = 1;
   2011 		cs->cs_softreq = 1;
   2012 	}
   2013 }
   2014 #endif
   2015