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