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