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