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