Home | History | Annotate | Line # | Download | only in ic
z8530tty.c revision 1.31
      1  1.31   mycroft /*	$NetBSD: z8530tty.c,v 1.31 1997/11/02 08:50:31 mycroft Exp $	*/
      2  1.21   mycroft 
      3  1.21   mycroft /*-
      4  1.21   mycroft  * Copyright (c) 1993, 1994, 1995, 1996, 1997
      5  1.21   mycroft  *	Charles M. Hannum.  All rights reserved.
      6  1.21   mycroft  *
      7  1.21   mycroft  * Redistribution and use in source and binary forms, with or without
      8  1.21   mycroft  * modification, are permitted provided that the following conditions
      9  1.21   mycroft  * are met:
     10  1.21   mycroft  * 1. Redistributions of source code must retain the above copyright
     11  1.21   mycroft  *    notice, this list of conditions and the following disclaimer.
     12  1.21   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.21   mycroft  *    notice, this list of conditions and the following disclaimer in the
     14  1.21   mycroft  *    documentation and/or other materials provided with the distribution.
     15  1.21   mycroft  * 3. All advertising materials mentioning features or use of this software
     16  1.21   mycroft  *    must display the following acknowledgement:
     17  1.21   mycroft  *	This product includes software developed by Charles M. Hannum.
     18  1.21   mycroft  * 4. The name of the author may not be used to endorse or promote products
     19  1.21   mycroft  *    derived from this software without specific prior written permission.
     20  1.21   mycroft  *
     21  1.21   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.21   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.21   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.21   mycroft  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.21   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.21   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.21   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.21   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.21   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.21   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.21   mycroft  */
     32   1.1       gwr 
     33   1.1       gwr /*
     34   1.1       gwr  * Copyright (c) 1994 Gordon W. Ross
     35   1.1       gwr  * Copyright (c) 1992, 1993
     36   1.1       gwr  *	The Regents of the University of California.  All rights reserved.
     37   1.1       gwr  *
     38   1.1       gwr  * This software was developed by the Computer Systems Engineering group
     39   1.1       gwr  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     40   1.1       gwr  * contributed to Berkeley.
     41   1.1       gwr  *
     42   1.1       gwr  * All advertising materials mentioning features or use of this software
     43   1.1       gwr  * must display the following acknowledgement:
     44   1.1       gwr  *	This product includes software developed by the University of
     45   1.1       gwr  *	California, Lawrence Berkeley Laboratory.
     46   1.1       gwr  *
     47   1.1       gwr  * Redistribution and use in source and binary forms, with or without
     48   1.1       gwr  * modification, are permitted provided that the following conditions
     49   1.1       gwr  * are met:
     50   1.1       gwr  * 1. Redistributions of source code must retain the above copyright
     51   1.1       gwr  *    notice, this list of conditions and the following disclaimer.
     52   1.1       gwr  * 2. Redistributions in binary form must reproduce the above copyright
     53   1.1       gwr  *    notice, this list of conditions and the following disclaimer in the
     54   1.1       gwr  *    documentation and/or other materials provided with the distribution.
     55   1.1       gwr  * 3. All advertising materials mentioning features or use of this software
     56   1.1       gwr  *    must display the following acknowledgement:
     57   1.1       gwr  *	This product includes software developed by the University of
     58   1.1       gwr  *	California, Berkeley and its contributors.
     59   1.1       gwr  * 4. Neither the name of the University nor the names of its contributors
     60   1.1       gwr  *    may be used to endorse or promote products derived from this software
     61   1.1       gwr  *    without specific prior written permission.
     62   1.1       gwr  *
     63   1.1       gwr  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64   1.1       gwr  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65   1.1       gwr  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66   1.1       gwr  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67   1.1       gwr  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68   1.1       gwr  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69   1.1       gwr  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70   1.1       gwr  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71   1.1       gwr  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72   1.1       gwr  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73   1.1       gwr  * SUCH DAMAGE.
     74   1.1       gwr  *
     75   1.1       gwr  *	@(#)zs.c	8.1 (Berkeley) 7/19/93
     76   1.1       gwr  */
     77   1.1       gwr 
     78   1.1       gwr /*
     79   1.1       gwr  * Zilog Z8530 Dual UART driver (tty interface)
     80   1.1       gwr  *
     81   1.1       gwr  * This is the "slave" driver that will be attached to
     82   1.1       gwr  * the "zsc" driver for plain "tty" async. serial lines.
     83   1.8       gwr  *
     84   1.8       gwr  * Credits, history:
     85   1.8       gwr  *
     86   1.8       gwr  * The original version of this code was the sparc/dev/zs.c driver
     87   1.8       gwr  * as distributed with the Berkeley 4.4 Lite release.  Since then,
     88   1.8       gwr  * Gordon Ross reorganized the code into the current parent/child
     89   1.8       gwr  * driver scheme, separating the Sun keyboard and mouse support
     90   1.8       gwr  * into independent child drivers.
     91   1.8       gwr  *
     92   1.8       gwr  * RTS/CTS flow-control support was a collaboration of:
     93   1.8       gwr  *	Gordon Ross <gwr (at) netbsd.org>,
     94   1.8       gwr  *	Bill Studenmund <wrstuden (at) loki.stanford.edu>
     95   1.8       gwr  *	Ian Dall <Ian.Dall (at) dsto.defence.gov.au>
     96   1.1       gwr  */
     97   1.1       gwr 
     98   1.1       gwr #include <sys/param.h>
     99   1.1       gwr #include <sys/systm.h>
    100   1.1       gwr #include <sys/proc.h>
    101   1.1       gwr #include <sys/device.h>
    102   1.1       gwr #include <sys/conf.h>
    103   1.1       gwr #include <sys/file.h>
    104   1.1       gwr #include <sys/ioctl.h>
    105   1.6       gwr #include <sys/malloc.h>
    106   1.1       gwr #include <sys/tty.h>
    107   1.1       gwr #include <sys/time.h>
    108   1.1       gwr #include <sys/kernel.h>
    109   1.1       gwr #include <sys/syslog.h>
    110   1.1       gwr 
    111   1.1       gwr #include <dev/ic/z8530reg.h>
    112   1.1       gwr #include <machine/z8530var.h>
    113   1.1       gwr 
    114  1.17       jtk #include "locators.h"
    115  1.17       jtk 
    116   1.1       gwr /*
    117   1.1       gwr  * How many input characters we can buffer.
    118   1.1       gwr  * The port-specific var.h may override this.
    119   1.1       gwr  * Note: must be a power of two!
    120   1.1       gwr  */
    121   1.1       gwr #ifndef	ZSTTY_RING_SIZE
    122   1.6       gwr #define	ZSTTY_RING_SIZE	2048
    123   1.1       gwr #endif
    124   1.6       gwr 
    125   1.6       gwr /*
    126   1.6       gwr  * Make this an option variable one can patch.
    127   1.6       gwr  * But be warned:  this must be a power of 2!
    128   1.6       gwr  */
    129   1.6       gwr int zstty_rbuf_size = ZSTTY_RING_SIZE;
    130   1.1       gwr 
    131   1.8       gwr /* This should usually be 3/4 of ZSTTY_RING_SIZE */
    132   1.8       gwr int zstty_rbuf_hiwat = (ZSTTY_RING_SIZE - (ZSTTY_RING_SIZE >> 2));
    133   1.8       gwr 
    134   1.1       gwr struct zstty_softc {
    135   1.1       gwr 	struct	device zst_dev;		/* required first: base device */
    136   1.1       gwr 	struct  tty *zst_tty;
    137   1.1       gwr 	struct	zs_chanstate *zst_cs;
    138   1.1       gwr 
    139   1.1       gwr 	int zst_hwflags;	/* see z8530var.h */
    140   1.1       gwr 	int zst_swflags;	/* TIOCFLAG_SOFTCAR, ... <ttycom.h> */
    141   1.1       gwr 
    142   1.8       gwr 	/*
    143   1.8       gwr 	 * Printing an overrun error message often takes long enough to
    144   1.8       gwr 	 * cause another overrun, so we only print one per second.
    145   1.8       gwr 	 */
    146   1.8       gwr 	long	zst_rotime;		/* time of last ring overrun */
    147   1.8       gwr 	long	zst_fotime;		/* time of last fifo overrun */
    148   1.8       gwr 
    149   1.8       gwr 	/*
    150   1.8       gwr 	 * The receive ring buffer.
    151   1.8       gwr 	 */
    152   1.8       gwr 	int	zst_rbget;	/* ring buffer `get' index */
    153   1.8       gwr 	volatile int	zst_rbput;	/* ring buffer `put' index */
    154   1.8       gwr 	int	zst_ringmask;
    155   1.8       gwr 	int	zst_rbhiwat;
    156   1.8       gwr 
    157   1.8       gwr 	u_short	*zst_rbuf; /* rr1, data pairs */
    158   1.1       gwr 
    159   1.1       gwr 	/*
    160   1.1       gwr 	 * The transmit byte count and address are used for pseudo-DMA
    161   1.1       gwr 	 * output in the hardware interrupt code.  PDMA can be suspended
    162   1.1       gwr 	 * to get pending changes done; heldtbc is used for this.  It can
    163   1.1       gwr 	 * also be stopped for ^S; this sets TS_TTSTOP in tp->t_state.
    164   1.1       gwr 	 */
    165   1.1       gwr 	int 	zst_tbc;			/* transmit byte count */
    166  1.23   mycroft 	u_char *zst_tba;			/* transmit buffer address */
    167   1.1       gwr 	int 	zst_heldtbc;		/* held tbc while xmission stopped */
    168   1.1       gwr 
    169   1.8       gwr 	/* Flags to communicate with zstty_softint() */
    170   1.8       gwr 	volatile char zst_rx_blocked;	/* input block at ring */
    171   1.8       gwr 	volatile char zst_rx_overrun;	/* ring overrun */
    172   1.8       gwr 	volatile char zst_tx_busy;	/* working on an output chunk */
    173   1.8       gwr 	volatile char zst_tx_done;	/* done with one output chunk */
    174   1.8       gwr 	volatile char zst_tx_stopped;	/* H/W level stop (lost CTS) */
    175   1.8       gwr 	volatile char zst_st_check;	/* got a status interrupt */
    176   1.8       gwr 	char pad[2];
    177   1.1       gwr };
    178   1.1       gwr 
    179   1.1       gwr 
    180   1.1       gwr /* Definition of the driver for autoconfig. */
    181  1.14       gwr #ifdef	__BROKEN_INDIRECT_CONFIG
    182   1.1       gwr static int	zstty_match(struct device *, void *, void *);
    183  1.14       gwr #else
    184  1.14       gwr static int	zstty_match(struct device *, struct cfdata *, void *);
    185  1.14       gwr #endif
    186   1.1       gwr static void	zstty_attach(struct device *, struct device *, void *);
    187   1.1       gwr 
    188   1.4   thorpej struct cfattach zstty_ca = {
    189   1.4   thorpej 	sizeof(struct zstty_softc), zstty_match, zstty_attach
    190   1.4   thorpej };
    191   1.4   thorpej 
    192   1.4   thorpej struct cfdriver zstty_cd = {
    193   1.4   thorpej 	NULL, "zstty", DV_TTY
    194   1.1       gwr };
    195   1.1       gwr 
    196   1.1       gwr struct zsops zsops_tty;
    197   1.1       gwr 
    198   1.1       gwr /* Routines called from other code. */
    199   1.1       gwr cdev_decl(zs);	/* open, close, read, write, ioctl, stop, ... */
    200   1.1       gwr 
    201  1.14       gwr static void	zsstart __P((struct tty *));
    202  1.14       gwr static int	zsparam __P((struct tty *, struct termios *));
    203  1.14       gwr static void zs_modem __P((struct zstty_softc *zst, int onoff));
    204  1.14       gwr static int	zshwiflow __P((struct tty *, int));
    205  1.24   mycroft static void zs_hwiflow __P((struct zstty_softc *));
    206   1.1       gwr 
    207   1.1       gwr /*
    208   1.1       gwr  * zstty_match: how is this zs channel configured?
    209   1.1       gwr  */
    210  1.14       gwr #ifdef	__BROKEN_INDIRECT_CONFIG
    211  1.14       gwr int
    212  1.14       gwr zstty_match(parent, vcf, aux)
    213  1.14       gwr 	struct device *parent;
    214  1.14       gwr 	void   *vcf, *aux;
    215  1.14       gwr {
    216  1.14       gwr 	struct cfdata *cf = vcf;
    217  1.14       gwr 	struct zsc_attach_args *args = aux;
    218  1.14       gwr 
    219  1.14       gwr 	/* Exact match is better than wildcard. */
    220  1.17       jtk 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
    221  1.14       gwr 		return 2;
    222  1.14       gwr 
    223  1.14       gwr 	/* This driver accepts wildcard. */
    224  1.17       jtk 	if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
    225  1.14       gwr 		return 1;
    226  1.14       gwr 
    227  1.14       gwr 	return 0;
    228  1.14       gwr }
    229  1.14       gwr #else	/* __BROKEN_INDIRECT_CONFIG */
    230   1.1       gwr int
    231  1.14       gwr zstty_match(parent, cf, aux)
    232   1.1       gwr 	struct device *parent;
    233  1.14       gwr 	struct cfdata *cf;
    234  1.14       gwr 	void   *aux;
    235   1.1       gwr {
    236   1.1       gwr 	struct zsc_attach_args *args = aux;
    237   1.1       gwr 
    238   1.1       gwr 	/* Exact match is better than wildcard. */
    239  1.17       jtk 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
    240   1.1       gwr 		return 2;
    241   1.1       gwr 
    242   1.1       gwr 	/* This driver accepts wildcard. */
    243  1.17       jtk 	if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
    244   1.1       gwr 		return 1;
    245   1.1       gwr 
    246   1.1       gwr 	return 0;
    247   1.1       gwr }
    248  1.14       gwr #endif	/* __BROKEN_INDIRECT_CONFIG */
    249   1.1       gwr 
    250   1.1       gwr void
    251   1.1       gwr zstty_attach(parent, self, aux)
    252   1.1       gwr 	struct device *parent, *self;
    253   1.1       gwr 	void   *aux;
    254   1.1       gwr 
    255   1.1       gwr {
    256   1.1       gwr 	struct zsc_softc *zsc = (void *) parent;
    257   1.1       gwr 	struct zstty_softc *zst = (void *) self;
    258  1.14       gwr 	struct cfdata *cf = self->dv_cfdata;
    259   1.1       gwr 	struct zsc_attach_args *args = aux;
    260   1.1       gwr 	struct zs_chanstate *cs;
    261   1.1       gwr 	struct tty *tp;
    262   1.1       gwr 	int channel, tty_unit;
    263   1.1       gwr 	dev_t dev;
    264   1.1       gwr 
    265   1.3       gwr 	tty_unit = zst->zst_dev.dv_unit;
    266   1.1       gwr 	channel = args->channel;
    267  1.14       gwr 	cs = zsc->zsc_cs[channel];
    268   1.1       gwr 	cs->cs_private = zst;
    269   1.1       gwr 	cs->cs_ops = &zsops_tty;
    270   1.1       gwr 
    271   1.1       gwr 	zst->zst_cs = cs;
    272   1.1       gwr 	zst->zst_swflags = cf->cf_flags;	/* softcar, etc. */
    273   1.1       gwr 	zst->zst_hwflags = args->hwflags;
    274  1.14       gwr 	dev = makedev(zs_major, tty_unit);
    275   1.1       gwr 
    276   1.1       gwr 	if (zst->zst_swflags)
    277  1.12  christos 		printf(" flags 0x%x", zst->zst_swflags);
    278   1.1       gwr 
    279   1.1       gwr 	if (zst->zst_hwflags & ZS_HWFLAG_CONSOLE)
    280  1.12  christos 		printf(" (console)");
    281   1.1       gwr 	else {
    282   1.1       gwr #ifdef KGDB
    283   1.1       gwr 		/*
    284  1.15       gwr 		 * Allow kgdb to "take over" this port.  Returns true
    285  1.15       gwr 		 * if this serial port is in-use by kgdb.
    286   1.1       gwr 		 */
    287   1.1       gwr 		if (zs_check_kgdb(cs, dev)) {
    288  1.15       gwr 			printf(" (kgdb)\n");
    289   1.1       gwr 			/*
    290   1.1       gwr 			 * This is the kgdb port (exclusive use)
    291   1.1       gwr 			 * so skip the normal attach code.
    292   1.1       gwr 			 */
    293   1.1       gwr 			return;
    294   1.1       gwr 		}
    295   1.1       gwr #endif
    296   1.1       gwr 	}
    297  1.12  christos 	printf("\n");
    298   1.1       gwr 
    299   1.6       gwr 	tp = ttymalloc();
    300   1.1       gwr 	tp->t_dev = dev;
    301   1.1       gwr 	tp->t_oproc = zsstart;
    302   1.1       gwr 	tp->t_param = zsparam;
    303   1.8       gwr 	tp->t_hwiflow = zshwiflow;
    304   1.9       gwr 	tty_attach(tp);
    305   1.1       gwr 
    306   1.6       gwr 	zst->zst_tty = tp;
    307   1.8       gwr 	zst->zst_rbhiwat =  zstty_rbuf_size;	/* impossible value */
    308   1.6       gwr 	zst->zst_ringmask = zstty_rbuf_size - 1;
    309   1.6       gwr 	zst->zst_rbuf = malloc(zstty_rbuf_size * sizeof(zst->zst_rbuf[0]),
    310   1.6       gwr 			      M_DEVBUF, M_WAITOK);
    311   1.6       gwr 
    312  1.14       gwr 	/* XXX - Do we need an MD hook here? */
    313  1.14       gwr 
    314   1.1       gwr 	/*
    315   1.1       gwr 	 * Hardware init
    316   1.1       gwr 	 */
    317   1.1       gwr 	if (zst->zst_hwflags & ZS_HWFLAG_CONSOLE) {
    318  1.14       gwr 		/* Call zsparam similar to open. */
    319  1.14       gwr 		struct termios t;
    320  1.14       gwr 
    321  1.14       gwr 		/* Make console output work while closed. */
    322   1.1       gwr 		zst->zst_swflags |= TIOCFLAG_SOFTCAR;
    323  1.14       gwr 		/* Setup the "new" parameters in t. */
    324  1.14       gwr 		bzero((void*)&t, sizeof(t));
    325  1.14       gwr 		t.c_cflag  = cs->cs_defcflag;
    326  1.14       gwr 		t.c_ospeed = cs->cs_defspeed;
    327  1.14       gwr 		/* Enable interrupts. */
    328  1.14       gwr 		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    329  1.14       gwr 		/* Make sure zsparam will see changes. */
    330  1.14       gwr 		tp->t_ospeed = 0;
    331  1.16       mrg 		(void)zsparam(tp, &t);
    332   1.1       gwr 	} else {
    333   1.1       gwr 		/* Not the console; may need reset. */
    334   1.1       gwr 		int reset, s;
    335   1.1       gwr 		reset = (channel == 0) ?
    336   1.1       gwr 			ZSWR9_A_RESET : ZSWR9_B_RESET;
    337   1.1       gwr 		s = splzs();
    338   1.2       gwr 		zs_write_reg(cs, 9, reset);
    339   1.1       gwr 		splx(s);
    340   1.1       gwr 	}
    341   1.1       gwr 
    342   1.1       gwr 	/*
    343   1.1       gwr 	 * Initialize state of modem control lines (DTR).
    344   1.1       gwr 	 * If softcar is set, turn on DTR now and leave it.
    345   1.1       gwr 	 * otherwise, turn off DTR now, and raise in open.
    346   1.1       gwr 	 * (Keeps modem from answering too early.)
    347   1.1       gwr 	 */
    348   1.1       gwr 	zs_modem(zst, (zst->zst_swflags & TIOCFLAG_SOFTCAR) ? 1 : 0);
    349   1.1       gwr }
    350   1.1       gwr 
    351   1.1       gwr 
    352   1.1       gwr /*
    353   1.1       gwr  * Return pointer to our tty.
    354   1.1       gwr  */
    355   1.1       gwr struct tty *
    356   1.1       gwr zstty(dev)
    357   1.1       gwr 	dev_t dev;
    358   1.1       gwr {
    359   1.1       gwr 	struct zstty_softc *zst;
    360   1.1       gwr 	int unit = minor(dev);
    361   1.1       gwr 
    362   1.1       gwr #ifdef	DIAGNOSTIC
    363   1.4   thorpej 	if (unit >= zstty_cd.cd_ndevs)
    364   1.1       gwr 		panic("zstty");
    365   1.1       gwr #endif
    366   1.4   thorpej 	zst = zstty_cd.cd_devs[unit];
    367   1.1       gwr 	return (zst->zst_tty);
    368   1.1       gwr }
    369   1.1       gwr 
    370   1.1       gwr 
    371   1.1       gwr /*
    372   1.1       gwr  * Open a zs serial (tty) port.
    373   1.1       gwr  */
    374   1.1       gwr int
    375   1.1       gwr zsopen(dev, flags, mode, p)
    376   1.1       gwr 	dev_t dev;
    377   1.1       gwr 	int flags;
    378   1.1       gwr 	int mode;
    379   1.1       gwr 	struct proc *p;
    380   1.1       gwr {
    381   1.1       gwr 	register struct tty *tp;
    382   1.1       gwr 	register struct zs_chanstate *cs;
    383   1.1       gwr 	struct zstty_softc *zst;
    384  1.26   mycroft 	int error, s, s2, unit;
    385   1.1       gwr 
    386   1.1       gwr 	unit = minor(dev);
    387   1.4   thorpej 	if (unit >= zstty_cd.cd_ndevs)
    388   1.1       gwr 		return (ENXIO);
    389   1.4   thorpej 	zst = zstty_cd.cd_devs[unit];
    390   1.1       gwr 	if (zst == NULL)
    391   1.1       gwr 		return (ENXIO);
    392   1.1       gwr 	tp = zst->zst_tty;
    393   1.1       gwr 	cs = zst->zst_cs;
    394   1.1       gwr 
    395   1.1       gwr 	/* If KGDB took the line, then tp==NULL */
    396   1.1       gwr 	if (tp == NULL)
    397   1.1       gwr 		return (EBUSY);
    398   1.1       gwr 
    399  1.20   mycroft 	if ((tp->t_state & TS_ISOPEN) != 0 &&
    400  1.20   mycroft 	    (tp->t_state & TS_XCLUDE) != 0 &&
    401  1.20   mycroft 	    p->p_ucred->cr_uid != 0)
    402   1.1       gwr 		return (EBUSY);
    403   1.1       gwr 
    404   1.1       gwr 	s = spltty();
    405   1.1       gwr 
    406   1.1       gwr 	if ((tp->t_state & TS_ISOPEN) == 0) {
    407   1.1       gwr 		/* First open. */
    408  1.14       gwr 		struct termios t;
    409  1.14       gwr 
    410  1.26   mycroft 		s2 = splzs();
    411  1.26   mycroft 
    412  1.20   mycroft 		/* Turn on interrupts. */
    413  1.27   mycroft 		cs->cs_creg[1] = cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    414  1.27   mycroft 		zs_write_reg(cs, 1, cs->cs_creg[1]);
    415  1.20   mycroft 
    416  1.20   mycroft 		/* Fetch the current modem control status, needed later. */
    417  1.20   mycroft 		cs->cs_rr0 = zs_read_csr(cs);
    418  1.20   mycroft 
    419  1.26   mycroft 		splx(s2);
    420  1.26   mycroft 
    421  1.14       gwr 		/*
    422  1.14       gwr 		 * Setup the "new" parameters in t.
    423  1.14       gwr 		 * Can not use tp->t because zsparam
    424  1.14       gwr 		 * deals only with what has changed.
    425  1.14       gwr 		 */
    426  1.20   mycroft 		t.c_ispeed = 0;
    427  1.20   mycroft 		t.c_ospeed = cs->cs_defspeed;
    428  1.20   mycroft 		t.c_cflag = cs->cs_defcflag;
    429   1.1       gwr 		if (zst->zst_swflags & TIOCFLAG_CLOCAL)
    430  1.14       gwr 			t.c_cflag |= CLOCAL;
    431   1.1       gwr 		if (zst->zst_swflags & TIOCFLAG_CRTSCTS)
    432  1.14       gwr 			t.c_cflag |= CRTSCTS;
    433   1.1       gwr 		if (zst->zst_swflags & TIOCFLAG_MDMBUF)
    434  1.14       gwr 			t.c_cflag |= MDMBUF;
    435  1.14       gwr 		/* Make sure zsparam will see changes. */
    436  1.14       gwr 		tp->t_ospeed = 0;
    437  1.14       gwr 		(void) zsparam(tp, &t);
    438  1.14       gwr 		/*
    439  1.14       gwr 		 * Note: zsparam has done: cflag, ispeed, ospeed
    440  1.14       gwr 		 * so we just need to do: iflag, oflag, lflag, cc
    441  1.14       gwr 		 * For "raw" mode, just leave all zeros.
    442  1.14       gwr 		 */
    443  1.14       gwr 		if ((zst->zst_hwflags & ZS_HWFLAG_RAW) == 0) {
    444  1.14       gwr 			tp->t_iflag = TTYDEF_IFLAG;
    445  1.14       gwr 			tp->t_oflag = TTYDEF_OFLAG;
    446  1.14       gwr 			tp->t_lflag = TTYDEF_LFLAG;
    447  1.14       gwr 		}
    448  1.19       gwr 		ttychars(tp);
    449   1.1       gwr 		ttsetwater(tp);
    450  1.20   mycroft 
    451  1.20   mycroft 		/*
    452  1.20   mycroft 		 * Turn on DTR.  We must always do this, even if carrier is not
    453  1.20   mycroft 		 * present, because otherwise we'd have to use TIOCSDTR
    454  1.28   mycroft 		 * immediately after setting CLOCAL, which applications do not
    455  1.28   mycroft 		 * expect.  We always assert DTR while the device is open
    456  1.28   mycroft 		 * unless explicitly requested to deassert it.
    457  1.20   mycroft 		 */
    458  1.20   mycroft 		zs_modem(zst, 1);
    459  1.20   mycroft 
    460  1.31   mycroft 		s2 = splzs();
    461  1.31   mycroft 
    462  1.20   mycroft 		/* Clear the input ring, and unblock. */
    463   1.1       gwr 		zst->zst_rbget = zst->zst_rbput;
    464  1.20   mycroft 		zs_iflush(cs);
    465  1.24   mycroft 		zst->zst_rx_blocked = 0;
    466  1.24   mycroft 		zs_hwiflow(zst);
    467  1.26   mycroft 
    468  1.26   mycroft 		splx(s2);
    469   1.1       gwr 	}
    470   1.1       gwr 	error = 0;
    471  1.14       gwr 
    472  1.20   mycroft 	/* If we're doing a blocking open... */
    473  1.20   mycroft 	if ((flags & O_NONBLOCK) == 0)
    474  1.20   mycroft 		/* ...then wait for carrier. */
    475  1.20   mycroft 		while ((tp->t_state & TS_CARR_ON) == 0 &&
    476  1.20   mycroft 		    (tp->t_cflag & (CLOCAL | MDMBUF)) == 0) {
    477  1.20   mycroft 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
    478  1.20   mycroft 			    ttopen, 0);
    479  1.20   mycroft 			if (error) {
    480  1.20   mycroft 				/*
    481  1.20   mycroft 				 * If the open was interrupted and nobody
    482  1.20   mycroft 				 * else has the device open, then hang up.
    483  1.20   mycroft 				 */
    484  1.20   mycroft 				if ((tp->t_state & TS_ISOPEN) == 0) {
    485  1.20   mycroft 					zs_modem(zst, 0);
    486  1.20   mycroft 					tp->t_state &= ~TS_WOPEN;
    487  1.20   mycroft 					ttwakeup(tp);
    488  1.20   mycroft 				}
    489  1.20   mycroft 				break;
    490   1.1       gwr 			}
    491  1.20   mycroft 			tp->t_state |= TS_WOPEN;
    492   1.1       gwr 		}
    493   1.1       gwr 
    494   1.1       gwr 	splx(s);
    495   1.1       gwr 	if (error == 0)
    496  1.20   mycroft 		error = (*linesw[tp->t_line].l_open)(dev, tp);
    497   1.1       gwr 	return (error);
    498   1.1       gwr }
    499   1.1       gwr 
    500   1.1       gwr /*
    501   1.1       gwr  * Close a zs serial port.
    502   1.1       gwr  */
    503   1.1       gwr int
    504   1.1       gwr zsclose(dev, flags, mode, p)
    505   1.1       gwr 	dev_t dev;
    506   1.1       gwr 	int flags;
    507   1.1       gwr 	int mode;
    508   1.1       gwr 	struct proc *p;
    509   1.1       gwr {
    510   1.1       gwr 	struct zstty_softc *zst;
    511   1.1       gwr 	register struct zs_chanstate *cs;
    512   1.1       gwr 	register struct tty *tp;
    513  1.20   mycroft 	int s;
    514   1.1       gwr 
    515   1.4   thorpej 	zst = zstty_cd.cd_devs[minor(dev)];
    516   1.1       gwr 	cs = zst->zst_cs;
    517   1.1       gwr 	tp = zst->zst_tty;
    518   1.1       gwr 
    519   1.1       gwr 	/* XXX This is for cons.c. */
    520   1.1       gwr 	if ((tp->t_state & TS_ISOPEN) == 0)
    521   1.1       gwr 		return 0;
    522   1.1       gwr 
    523   1.1       gwr 	(*linesw[tp->t_line].l_close)(tp, flags);
    524  1.20   mycroft 	ttyclose(tp);
    525  1.20   mycroft 
    526  1.27   mycroft 	s = splzs();
    527  1.27   mycroft 
    528  1.20   mycroft 	/* If we were asserting flow control, then deassert it. */
    529  1.24   mycroft 	zst->zst_rx_blocked = 1;
    530  1.24   mycroft 	zs_hwiflow(zst);
    531  1.31   mycroft 
    532  1.31   mycroft 	splx(s);
    533  1.31   mycroft 
    534  1.20   mycroft 	/* Clear any break condition set with TIOCSBRK. */
    535  1.20   mycroft 	zs_break(cs, 0);
    536  1.27   mycroft 
    537  1.20   mycroft 	/*
    538  1.20   mycroft 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    539  1.20   mycroft 	 * notice even if we immediately open the port again.
    540  1.20   mycroft 	 */
    541  1.20   mycroft 	if ((tp->t_cflag & HUPCL) != 0) {
    542  1.20   mycroft 		zs_modem(zst, 0);
    543  1.20   mycroft 		(void) tsleep(cs, TTIPRI, ttclos, hz);
    544  1.20   mycroft 	}
    545  1.14       gwr 
    546  1.14       gwr 	s = splzs();
    547  1.27   mycroft 
    548  1.20   mycroft 	/* Turn off interrupts. */
    549  1.14       gwr 	cs->cs_creg[1] = cs->cs_preg[1] = 0;
    550  1.14       gwr 	zs_write_reg(cs, 1, cs->cs_creg[1]);
    551  1.27   mycroft 
    552  1.14       gwr 	splx(s);
    553  1.14       gwr 
    554   1.1       gwr 	return (0);
    555   1.1       gwr }
    556   1.1       gwr 
    557   1.1       gwr /*
    558   1.1       gwr  * Read/write zs serial port.
    559   1.1       gwr  */
    560   1.1       gwr int
    561   1.1       gwr zsread(dev, uio, flags)
    562   1.1       gwr 	dev_t dev;
    563   1.1       gwr 	struct uio *uio;
    564   1.1       gwr 	int flags;
    565   1.1       gwr {
    566   1.1       gwr 	register struct zstty_softc *zst;
    567   1.1       gwr 	register struct tty *tp;
    568   1.1       gwr 
    569   1.4   thorpej 	zst = zstty_cd.cd_devs[minor(dev)];
    570   1.1       gwr 	tp = zst->zst_tty;
    571   1.1       gwr 	return (linesw[tp->t_line].l_read(tp, uio, flags));
    572   1.1       gwr }
    573   1.1       gwr 
    574   1.1       gwr int
    575   1.1       gwr zswrite(dev, uio, flags)
    576   1.1       gwr 	dev_t dev;
    577   1.1       gwr 	struct uio *uio;
    578   1.1       gwr 	int flags;
    579   1.1       gwr {
    580   1.1       gwr 	register struct zstty_softc *zst;
    581   1.1       gwr 	register struct tty *tp;
    582   1.1       gwr 
    583   1.4   thorpej 	zst = zstty_cd.cd_devs[minor(dev)];
    584   1.1       gwr 	tp = zst->zst_tty;
    585   1.1       gwr 	return (linesw[tp->t_line].l_write(tp, uio, flags));
    586   1.1       gwr }
    587   1.1       gwr 
    588   1.1       gwr int
    589   1.1       gwr zsioctl(dev, cmd, data, flag, p)
    590   1.1       gwr 	dev_t dev;
    591   1.1       gwr 	u_long cmd;
    592   1.1       gwr 	caddr_t data;
    593   1.1       gwr 	int flag;
    594   1.1       gwr 	struct proc *p;
    595   1.1       gwr {
    596   1.1       gwr 	register struct zstty_softc *zst;
    597   1.1       gwr 	register struct zs_chanstate *cs;
    598   1.1       gwr 	register struct tty *tp;
    599  1.22   mycroft 	register struct linesw *line;
    600  1.20   mycroft 	register int error;
    601   1.1       gwr 
    602   1.4   thorpej 	zst = zstty_cd.cd_devs[minor(dev)];
    603   1.1       gwr 	cs = zst->zst_cs;
    604   1.1       gwr 	tp = zst->zst_tty;
    605  1.22   mycroft 	line = &linesw[tp->t_line];
    606   1.1       gwr 
    607  1.22   mycroft 	error = (*line->l_ioctl)(tp, cmd, data, flag, p);
    608   1.1       gwr 	if (error >= 0)
    609   1.1       gwr 		return (error);
    610  1.14       gwr 
    611   1.1       gwr 	error = ttioctl(tp, cmd, data, flag, p);
    612   1.1       gwr 	if (error >= 0)
    613   1.1       gwr 		return (error);
    614   1.1       gwr 
    615  1.14       gwr #ifdef	ZS_MD_IOCTL
    616  1.14       gwr 	error = ZS_MD_IOCTL;
    617  1.14       gwr 	if (error >= 0)
    618  1.14       gwr 		return (error);
    619  1.14       gwr #endif	/* ZS_MD_IOCTL */
    620  1.14       gwr 
    621   1.1       gwr 	switch (cmd) {
    622   1.1       gwr 	case TIOCSBRK:
    623   1.1       gwr 		zs_break(cs, 1);
    624   1.1       gwr 		break;
    625   1.1       gwr 
    626   1.1       gwr 	case TIOCCBRK:
    627   1.1       gwr 		zs_break(cs, 0);
    628   1.1       gwr 		break;
    629   1.1       gwr 
    630   1.1       gwr 	case TIOCGFLAGS:
    631   1.1       gwr 		*(int *)data = zst->zst_swflags;
    632   1.1       gwr 		break;
    633   1.1       gwr 
    634   1.1       gwr 	case TIOCSFLAGS:
    635   1.1       gwr 		error = suser(p->p_ucred, &p->p_acflag);
    636  1.20   mycroft 		if (error)
    637  1.20   mycroft 			return (error);
    638  1.20   mycroft 		zst->zst_swflags = *(int *)data;
    639   1.1       gwr 		break;
    640   1.1       gwr 
    641   1.1       gwr 	case TIOCSDTR:
    642   1.1       gwr 		zs_modem(zst, 1);
    643   1.1       gwr 		break;
    644   1.1       gwr 
    645   1.1       gwr 	case TIOCCDTR:
    646   1.1       gwr 		zs_modem(zst, 0);
    647   1.1       gwr 		break;
    648   1.1       gwr 
    649   1.1       gwr 	case TIOCMSET:
    650   1.1       gwr 	case TIOCMBIS:
    651   1.1       gwr 	case TIOCMBIC:
    652   1.1       gwr 	case TIOCMGET:
    653   1.1       gwr 	default:
    654   1.1       gwr 		return (ENOTTY);
    655   1.1       gwr 	}
    656   1.1       gwr 	return (0);
    657   1.1       gwr }
    658   1.1       gwr 
    659   1.1       gwr /*
    660   1.1       gwr  * Start or restart transmission.
    661   1.1       gwr  */
    662   1.1       gwr static void
    663   1.1       gwr zsstart(tp)
    664   1.1       gwr 	register struct tty *tp;
    665   1.1       gwr {
    666   1.1       gwr 	register struct zstty_softc *zst;
    667   1.1       gwr 	register struct zs_chanstate *cs;
    668  1.20   mycroft 	register int s;
    669   1.1       gwr 
    670   1.4   thorpej 	zst = zstty_cd.cd_devs[minor(tp->t_dev)];
    671   1.1       gwr 	cs = zst->zst_cs;
    672   1.1       gwr 
    673   1.1       gwr 	s = spltty();
    674  1.20   mycroft 	if ((tp->t_state & TS_BUSY) != 0)
    675   1.1       gwr 		goto out;
    676  1.20   mycroft 	if ((tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) != 0)
    677  1.20   mycroft 		goto stopped;
    678   1.1       gwr 
    679  1.14       gwr 	if (zst->zst_tx_stopped)
    680  1.20   mycroft 		goto stopped;
    681   1.8       gwr 
    682   1.1       gwr 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    683  1.20   mycroft 		if ((tp->t_state & TS_ASLEEP) != 0) {
    684   1.1       gwr 			tp->t_state &= ~TS_ASLEEP;
    685   1.1       gwr 			wakeup((caddr_t)&tp->t_outq);
    686   1.1       gwr 		}
    687   1.1       gwr 		selwakeup(&tp->t_wsel);
    688  1.20   mycroft 		if (tp->t_outq.c_cc == 0)
    689  1.20   mycroft 			goto stopped;
    690   1.1       gwr 	}
    691   1.1       gwr 
    692  1.20   mycroft 	/* Grab the first contiguous region of buffer space. */
    693  1.20   mycroft 	{
    694  1.20   mycroft 		u_char *tba;
    695  1.20   mycroft 		int tbc;
    696  1.20   mycroft 
    697  1.20   mycroft 		tba = tp->t_outq.c_cf;
    698  1.20   mycroft 		tbc = ndqb(&tp->t_outq, 0);
    699  1.20   mycroft 
    700  1.20   mycroft 		(void) splzs();
    701  1.20   mycroft 
    702  1.20   mycroft 		zst->zst_tba = tba;
    703  1.20   mycroft 		zst->zst_tbc = tbc;
    704  1.20   mycroft 	}
    705   1.8       gwr 
    706  1.20   mycroft 	tp->t_state |= TS_BUSY;
    707  1.20   mycroft 	zst->zst_tx_busy = 1;
    708   1.1       gwr 
    709  1.20   mycroft 	/* Enable transmit completion interrupts if necessary. */
    710  1.20   mycroft 	if ((cs->cs_preg[1] & ZSWR1_TIE) == 0) {
    711   1.1       gwr 		cs->cs_preg[1] |= ZSWR1_TIE;
    712   1.8       gwr 		cs->cs_creg[1] = cs->cs_preg[1];
    713   1.2       gwr 		zs_write_reg(cs, 1, cs->cs_creg[1]);
    714  1.20   mycroft 	}
    715  1.20   mycroft 
    716  1.20   mycroft 	/* Output the first character of the contiguous buffer. */
    717  1.20   mycroft 	zs_write_data(cs, *zst->zst_tba);
    718  1.20   mycroft 	zst->zst_tbc--;
    719  1.20   mycroft 	zst->zst_tba++;
    720  1.20   mycroft 	splx(s);
    721  1.20   mycroft 	return;
    722  1.20   mycroft 
    723  1.20   mycroft stopped:
    724  1.20   mycroft 	/* Disable transmit completion interrupts if necessary. */
    725  1.20   mycroft 	if ((cs->cs_preg[1] & ZSWR1_TIE) != 0) {
    726   1.1       gwr 		cs->cs_preg[1] &= ~ZSWR1_TIE;
    727   1.8       gwr 		cs->cs_creg[1] = cs->cs_preg[1];
    728   1.2       gwr 		zs_write_reg(cs, 1, cs->cs_creg[1]);
    729   1.1       gwr 	}
    730   1.1       gwr out:
    731   1.1       gwr 	splx(s);
    732  1.20   mycroft 	return;
    733   1.1       gwr }
    734   1.1       gwr 
    735   1.1       gwr /*
    736   1.1       gwr  * Stop output, e.g., for ^S or output flush.
    737   1.1       gwr  */
    738  1.10   mycroft void
    739   1.1       gwr zsstop(tp, flag)
    740   1.1       gwr 	struct tty *tp;
    741   1.1       gwr 	int flag;
    742   1.1       gwr {
    743   1.1       gwr 	register struct zstty_softc *zst;
    744   1.1       gwr 	register struct zs_chanstate *cs;
    745   1.1       gwr 	register int s;
    746   1.1       gwr 
    747   1.4   thorpej 	zst = zstty_cd.cd_devs[minor(tp->t_dev)];
    748   1.1       gwr 	cs = zst->zst_cs;
    749   1.1       gwr 
    750   1.1       gwr 	s = splzs();
    751   1.1       gwr 	if (tp->t_state & TS_BUSY) {
    752   1.1       gwr 		/*
    753   1.1       gwr 		 * Device is transmitting; must stop it.
    754   1.8       gwr 		 * Also clear _heldtbc to prevent any
    755   1.8       gwr 		 * flow-control event from resuming.
    756   1.1       gwr 		 */
    757   1.1       gwr 		zst->zst_tbc = 0;
    758   1.8       gwr 		zst->zst_heldtbc = 0;
    759   1.1       gwr 		if ((tp->t_state & TS_TTSTOP) == 0)
    760   1.1       gwr 			tp->t_state |= TS_FLUSH;
    761   1.1       gwr 	}
    762   1.1       gwr 	splx(s);
    763   1.1       gwr }
    764   1.1       gwr 
    765   1.1       gwr /*
    766   1.1       gwr  * Set ZS tty parameters from termios.
    767   1.1       gwr  * XXX - Should just copy the whole termios after
    768   1.1       gwr  * making sure all the changes could be done.
    769   1.1       gwr  */
    770   1.1       gwr static int
    771   1.1       gwr zsparam(tp, t)
    772   1.1       gwr 	register struct tty *tp;
    773   1.1       gwr 	register struct termios *t;
    774   1.1       gwr {
    775  1.14       gwr 	struct zstty_softc *zst;
    776  1.14       gwr 	struct zs_chanstate *cs;
    777  1.22   mycroft 	register struct linesw *line;
    778  1.14       gwr 	int s, bps, cflag, error;
    779  1.14       gwr 	u_char tmp3, tmp4, tmp5;
    780   1.1       gwr 
    781   1.4   thorpej 	zst = zstty_cd.cd_devs[minor(tp->t_dev)];
    782   1.1       gwr 	cs = zst->zst_cs;
    783  1.22   mycroft 	line = &linesw[tp->t_line];
    784  1.14       gwr 	bps = t->c_ospeed;
    785  1.14       gwr 	cflag = t->c_cflag;
    786   1.1       gwr 
    787   1.1       gwr 	if (bps < 0 || (t->c_ispeed && t->c_ispeed != bps))
    788   1.1       gwr 		return (EINVAL);
    789  1.14       gwr 
    790  1.14       gwr 	/*
    791  1.20   mycroft 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    792  1.20   mycroft 	 * is always active.
    793  1.20   mycroft 	 */
    794  1.20   mycroft 	if ((zst->zst_swflags & TIOCFLAG_SOFTCAR) != 0 ||
    795  1.20   mycroft 	    (zst->zst_hwflags & (ZS_HWFLAG_NO_DCD | ZS_HWFLAG_CONSOLE)) != 0) {
    796  1.20   mycroft 		t->c_cflag |= CLOCAL;
    797  1.20   mycroft 		t->c_cflag &= ~HUPCL;
    798  1.20   mycroft 	}
    799  1.20   mycroft 
    800  1.20   mycroft 	/*
    801  1.14       gwr 	 * Only whack the UART when params change.
    802  1.14       gwr 	 * Some callers need to clear tp->t_ospeed
    803  1.14       gwr 	 * to make sure initialization gets done.
    804  1.14       gwr 	 */
    805  1.20   mycroft 	if (tp->t_ospeed == bps &&
    806  1.20   mycroft 	    tp->t_cflag == cflag)
    807   1.1       gwr 		return (0);
    808   1.1       gwr 
    809  1.14       gwr 	/*
    810  1.14       gwr 	 * Call MD functions to deal with changed
    811  1.14       gwr 	 * clock modes or H/W flow control modes.
    812  1.14       gwr 	 * The BRG divisor is set now. (reg 12,13)
    813  1.14       gwr 	 */
    814  1.14       gwr 	error = zs_set_speed(cs, bps);
    815  1.14       gwr 	if (error)
    816  1.14       gwr 		return (error);
    817  1.14       gwr 	error = zs_set_modes(cs, cflag);
    818  1.14       gwr 	if (error)
    819  1.14       gwr 		return (error);
    820   1.1       gwr 
    821  1.14       gwr 	/* OK, we are now committed to do it. */
    822   1.1       gwr 	tp->t_cflag = cflag;
    823  1.14       gwr 	tp->t_ospeed = bps;
    824  1.14       gwr 	tp->t_ispeed = bps;
    825   1.1       gwr 
    826   1.1       gwr 	/*
    827   1.1       gwr 	 * Block interrupts so that state will not
    828   1.1       gwr 	 * be altered until we are done setting it up.
    829  1.14       gwr 	 *
    830   1.1       gwr 	 * Initial values in cs_preg are set before
    831   1.1       gwr 	 * our attach routine is called.  The master
    832   1.1       gwr 	 * interrupt enable is handled by zsc.c
    833  1.14       gwr 	 *
    834   1.1       gwr 	 */
    835  1.14       gwr 	s = splzs();
    836  1.29   mycroft 
    837  1.29   mycroft 	cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
    838  1.29   mycroft 	if ((cs->cs_rr0_mask & ZSRR0_DCD) != 0)
    839  1.29   mycroft 		cs->cs_preg[15] |= ZSWR15_DCD_IE;
    840  1.29   mycroft 	else
    841  1.29   mycroft 		cs->cs_preg[15] &= ~ZSWR15_DCD_IE;
    842  1.29   mycroft 	if ((cs->cs_rr0_mask & ZSRR0_CTS) != 0)
    843  1.29   mycroft 		cs->cs_preg[15] |= ZSWR15_CTS_IE;
    844  1.29   mycroft 	else
    845  1.29   mycroft 		cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
    846   1.1       gwr 
    847  1.14       gwr 	/* Recompute character size bits. */
    848  1.14       gwr 	tmp3 = cs->cs_preg[3] & ~ZSWR3_RXSIZE;
    849  1.14       gwr 	tmp5 = cs->cs_preg[5] & ~ZSWR5_TXSIZE;
    850   1.1       gwr 	switch (cflag & CSIZE) {
    851   1.1       gwr 	case CS5:
    852  1.14       gwr 		/* These are |= 0 but let the optimizer deal with it. */
    853  1.14       gwr 		tmp3 |= ZSWR3_RX_5;
    854  1.14       gwr 		tmp5 |= ZSWR5_TX_5;
    855   1.1       gwr 		break;
    856   1.1       gwr 	case CS6:
    857  1.14       gwr 		tmp3 |= ZSWR3_RX_6;
    858  1.14       gwr 		tmp5 |= ZSWR5_TX_6;
    859   1.1       gwr 		break;
    860   1.1       gwr 	case CS7:
    861  1.14       gwr 		tmp3 |= ZSWR3_RX_7;
    862  1.14       gwr 		tmp5 |= ZSWR5_TX_7;
    863   1.1       gwr 		break;
    864   1.1       gwr 	case CS8:
    865   1.1       gwr 	default:
    866  1.14       gwr 		tmp3 |= ZSWR3_RX_8;
    867  1.14       gwr 		tmp5 |= ZSWR5_TX_8;
    868   1.1       gwr 		break;
    869   1.1       gwr 	}
    870  1.20   mycroft 
    871  1.20   mycroft #if 0
    872  1.14       gwr 	/* Raise or lower DTR and RTS as appropriate. */
    873  1.14       gwr 	if (bps) {
    874  1.14       gwr 		/* Raise DTR and RTS */
    875  1.14       gwr 		tmp5 |= cs->cs_wr5_dtr;
    876  1.14       gwr 	} else {
    877  1.14       gwr 		/* Drop DTR and RTS */
    878  1.14       gwr 		/* XXX: Should SOFTCAR prevent this? */
    879  1.22   mycroft 		tmp5 &= ~cs->cs_wr5_dtr;
    880  1.14       gwr 	}
    881  1.20   mycroft #endif
    882  1.20   mycroft 
    883  1.14       gwr 	cs->cs_preg[3] = tmp3;
    884  1.14       gwr 	cs->cs_preg[5] = tmp5;
    885  1.14       gwr 
    886  1.14       gwr 	/*
    887  1.14       gwr 	 * Recompute the stop bits and parity bits.  Note that
    888  1.14       gwr 	 * zs_set_speed() may have set clock selection bits etc.
    889  1.14       gwr 	 * in wr4, so those must preserved.
    890  1.14       gwr 	 */
    891  1.14       gwr 	tmp4 = cs->cs_preg[4];
    892  1.14       gwr 	/* Recompute stop bits. */
    893  1.14       gwr 	tmp4 &= ~ZSWR4_SBMASK;
    894  1.14       gwr 	tmp4 |= (cflag & CSTOPB) ?
    895  1.14       gwr 		ZSWR4_TWOSB : ZSWR4_ONESB;
    896  1.14       gwr 	/* Recompute parity bits. */
    897  1.14       gwr 	tmp4 &= ~ZSWR4_PARMASK;
    898   1.1       gwr 	if ((cflag & PARODD) == 0)
    899   1.1       gwr 		tmp4 |= ZSWR4_EVENP;
    900   1.1       gwr 	if (cflag & PARENB)
    901   1.1       gwr 		tmp4 |= ZSWR4_PARENB;
    902   1.1       gwr 	cs->cs_preg[4] = tmp4;
    903   1.1       gwr 
    904  1.14       gwr 	/* The MD function zs_set_modes handled CRTSCTS, etc. */
    905   1.8       gwr 
    906   1.8       gwr 	/*
    907   1.1       gwr 	 * If nothing is being transmitted, set up new current values,
    908   1.1       gwr 	 * else mark them as pending.
    909   1.1       gwr 	 */
    910  1.25   mycroft 	if (!cs->cs_heldchange) {
    911   1.8       gwr 		if (zst->zst_tx_busy) {
    912   1.1       gwr 			zst->zst_heldtbc = zst->zst_tbc;
    913   1.1       gwr 			zst->zst_tbc = 0;
    914  1.25   mycroft 			cs->cs_heldchange = 1;
    915  1.25   mycroft 		} else
    916   1.1       gwr 			zs_loadchannelregs(cs);
    917   1.1       gwr 	}
    918  1.20   mycroft 
    919   1.1       gwr 	splx(s);
    920  1.15       gwr 
    921  1.20   mycroft 	/*
    922  1.20   mycroft 	 * Update the tty layer's idea of the carrier bit, in case we changed
    923  1.20   mycroft 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that if we
    924  1.20   mycroft 	 * lose carrier while carrier detection is on.
    925  1.20   mycroft 	 */
    926  1.22   mycroft 	(void) (*line->l_modem)(tp, (cs->cs_rr0 & cs->cs_rr0_dcd) != 0);
    927  1.14       gwr 
    928  1.14       gwr 	/* If we can throttle input, enable "high water" detection. */
    929  1.14       gwr 	if (cflag & CHWFLOW) {
    930  1.14       gwr 		zst->zst_rbhiwat = zstty_rbuf_hiwat;
    931  1.14       gwr 	} else {
    932  1.14       gwr 		/* This impossible value prevents a "high water" trigger. */
    933  1.14       gwr 		zst->zst_rbhiwat = zstty_rbuf_size;
    934  1.30   mycroft 		if (zst->zst_rx_blocked) {
    935  1.30   mycroft 			zst->zst_rx_blocked = 0;
    936  1.30   mycroft 			zs_hwiflow(zst);
    937  1.30   mycroft 		}
    938  1.14       gwr 		if (zst->zst_tx_stopped) {
    939  1.14       gwr 			zst->zst_tx_stopped = 0;
    940  1.14       gwr 			zsstart(tp);
    941  1.14       gwr 		}
    942  1.14       gwr 	}
    943  1.14       gwr 
    944   1.1       gwr 	return (0);
    945   1.1       gwr }
    946   1.1       gwr 
    947   1.1       gwr /*
    948   1.1       gwr  * Raise or lower modem control (DTR/RTS) signals.  If a character is
    949   1.1       gwr  * in transmission, the change is deferred.
    950   1.1       gwr  */
    951   1.1       gwr static void
    952   1.1       gwr zs_modem(zst, onoff)
    953   1.1       gwr 	struct zstty_softc *zst;
    954   1.1       gwr 	int onoff;
    955   1.1       gwr {
    956   1.1       gwr 	struct zs_chanstate *cs;
    957  1.24   mycroft 	int s;
    958   1.1       gwr 
    959   1.1       gwr 	cs = zst->zst_cs;
    960  1.14       gwr 	if (cs->cs_wr5_dtr == 0)
    961  1.14       gwr 		return;
    962   1.1       gwr 
    963  1.24   mycroft 	s = splzs();
    964  1.24   mycroft 	if (onoff)
    965  1.24   mycroft 		cs->cs_preg[5] |= cs->cs_wr5_dtr;
    966  1.24   mycroft 	else
    967  1.24   mycroft 		cs->cs_preg[5] &= ~cs->cs_wr5_dtr;
    968  1.14       gwr 
    969  1.25   mycroft 	if (!cs->cs_heldchange) {
    970   1.8       gwr 		if (zst->zst_tx_busy) {
    971   1.1       gwr 			zst->zst_heldtbc = zst->zst_tbc;
    972   1.1       gwr 			zst->zst_tbc = 0;
    973  1.25   mycroft 			cs->cs_heldchange = 1;
    974  1.25   mycroft 		} else
    975  1.25   mycroft 			zs_loadchannelregs(cs);
    976   1.1       gwr 	}
    977   1.1       gwr 	splx(s);
    978   1.1       gwr }
    979   1.1       gwr 
    980   1.8       gwr /*
    981   1.8       gwr  * Try to block or unblock input using hardware flow-control.
    982   1.8       gwr  * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
    983   1.8       gwr  * if this function returns non-zero, the TS_TBLOCK flag will
    984  1.24   mycroft  * be set or cleared according to the "block" arg passed.
    985   1.8       gwr  */
    986   1.8       gwr int
    987  1.24   mycroft zshwiflow(tp, block)
    988   1.8       gwr 	struct tty *tp;
    989  1.24   mycroft 	int block;
    990   1.8       gwr {
    991   1.8       gwr 	register struct zstty_softc *zst;
    992  1.14       gwr 	register struct zs_chanstate *cs;
    993   1.8       gwr 	int s;
    994   1.8       gwr 
    995   1.8       gwr 	zst = zstty_cd.cd_devs[minor(tp->t_dev)];
    996  1.14       gwr 	cs = zst->zst_cs;
    997  1.14       gwr 	if (cs->cs_wr5_rts == 0)
    998  1.14       gwr 		return (0);
    999   1.8       gwr 
   1000   1.8       gwr 	s = splzs();
   1001  1.24   mycroft 	if (block) {
   1002  1.24   mycroft 		if (!zst->zst_rx_blocked) {
   1003  1.24   mycroft 			zst->zst_rx_blocked = 1;
   1004  1.24   mycroft 			zs_hwiflow(zst);
   1005  1.24   mycroft 		}
   1006   1.8       gwr 	} else {
   1007  1.24   mycroft 		if (zst->zst_rx_blocked) {
   1008  1.24   mycroft 			zst->zst_rx_blocked = 0;
   1009  1.24   mycroft 			zs_hwiflow(zst);
   1010  1.24   mycroft 		}
   1011   1.8       gwr 	}
   1012   1.8       gwr 	splx(s);
   1013   1.8       gwr 	return 1;
   1014   1.8       gwr }
   1015   1.8       gwr 
   1016   1.8       gwr /*
   1017   1.8       gwr  * Internal version of zshwiflow
   1018   1.8       gwr  * called at splzs
   1019   1.8       gwr  */
   1020   1.8       gwr static void
   1021  1.24   mycroft zs_hwiflow(zst)
   1022   1.8       gwr 	register struct zstty_softc *zst;
   1023   1.8       gwr {
   1024   1.8       gwr 	register struct zs_chanstate *cs;
   1025   1.8       gwr 
   1026   1.8       gwr 	cs = zst->zst_cs;
   1027  1.14       gwr 	if (cs->cs_wr5_rts == 0)
   1028  1.14       gwr 		return;
   1029   1.8       gwr 
   1030  1.24   mycroft 	if (zst->zst_rx_blocked) {
   1031  1.24   mycroft 		cs->cs_preg[5] &= ~cs->cs_wr5_rts;
   1032  1.24   mycroft 		cs->cs_creg[5] &= ~cs->cs_wr5_rts;
   1033   1.8       gwr 	} else {
   1034  1.24   mycroft 		cs->cs_preg[5] |= cs->cs_wr5_rts;
   1035  1.24   mycroft 		cs->cs_creg[5] |= cs->cs_wr5_rts;
   1036   1.8       gwr 	}
   1037  1.24   mycroft 	zs_write_reg(cs, 5, cs->cs_creg[5]);
   1038   1.8       gwr }
   1039   1.8       gwr 
   1040   1.1       gwr 
   1041   1.1       gwr /****************************************************************
   1042   1.1       gwr  * Interface to the lower layer (zscc)
   1043   1.1       gwr  ****************************************************************/
   1044   1.3       gwr 
   1045  1.14       gwr static void zstty_rxint __P((struct zs_chanstate *));
   1046  1.14       gwr static void zstty_txint __P((struct zs_chanstate *));
   1047  1.14       gwr static void zstty_stint __P((struct zs_chanstate *));
   1048  1.14       gwr static void zstty_softint  __P((struct zs_chanstate *));
   1049  1.14       gwr 
   1050  1.14       gwr static void zsoverrun __P((struct zstty_softc *, long *, char *));
   1051   1.1       gwr 
   1052   1.6       gwr /*
   1053   1.8       gwr  * receiver ready interrupt.
   1054   1.8       gwr  * called at splzs
   1055   1.6       gwr  */
   1056   1.6       gwr static void
   1057   1.1       gwr zstty_rxint(cs)
   1058   1.1       gwr 	register struct zs_chanstate *cs;
   1059   1.1       gwr {
   1060   1.1       gwr 	register struct zstty_softc *zst;
   1061   1.8       gwr 	register int cc, put, put_next, ringmask;
   1062   1.1       gwr 	register u_char c, rr0, rr1;
   1063   1.8       gwr 	register u_short ch_rr1;
   1064   1.1       gwr 
   1065   1.1       gwr 	zst = cs->cs_private;
   1066   1.1       gwr 	put = zst->zst_rbput;
   1067   1.6       gwr 	ringmask = zst->zst_ringmask;
   1068   1.1       gwr 
   1069   1.1       gwr nextchar:
   1070   1.1       gwr 
   1071   1.5       gwr 	/*
   1072   1.5       gwr 	 * First read the status, because reading the received char
   1073   1.5       gwr 	 * destroys the status of this char.
   1074   1.5       gwr 	 */
   1075   1.2       gwr 	rr1 = zs_read_reg(cs, 1);
   1076   1.5       gwr 	c = zs_read_data(cs);
   1077   1.8       gwr 	ch_rr1 = (c << 8) | rr1;
   1078   1.1       gwr 
   1079   1.8       gwr 	if (ch_rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
   1080   1.1       gwr 		/* Clear the receive error. */
   1081   1.2       gwr 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
   1082   1.1       gwr 	}
   1083   1.1       gwr 
   1084   1.8       gwr 	/* XXX: Check for the stop character? */
   1085   1.8       gwr 
   1086   1.8       gwr 	zst->zst_rbuf[put] = ch_rr1;
   1087   1.6       gwr 	put_next = (put + 1) & ringmask;
   1088   1.1       gwr 
   1089   1.1       gwr 	/* Would overrun if increment makes (put==get). */
   1090   1.1       gwr 	if (put_next == zst->zst_rbget) {
   1091   1.8       gwr 		zst->zst_rx_overrun = 1;
   1092   1.1       gwr 	} else {
   1093   1.1       gwr 		/* OK, really increment. */
   1094   1.1       gwr 		put = put_next;
   1095   1.1       gwr 	}
   1096   1.1       gwr 
   1097   1.1       gwr 	/* Keep reading until the FIFO is empty. */
   1098   1.2       gwr 	rr0 = zs_read_csr(cs);
   1099   1.1       gwr 	if (rr0 & ZSRR0_RX_READY)
   1100   1.1       gwr 		goto nextchar;
   1101   1.1       gwr 
   1102   1.1       gwr 	/* Done reading. */
   1103   1.1       gwr 	zst->zst_rbput = put;
   1104   1.1       gwr 
   1105   1.8       gwr 	/*
   1106   1.8       gwr 	 * If ring is getting too full, try to block input.
   1107   1.8       gwr 	 */
   1108   1.8       gwr 	cc = put - zst->zst_rbget;
   1109   1.8       gwr 	if (cc < 0)
   1110   1.8       gwr 		cc += zstty_rbuf_size;
   1111   1.8       gwr 	if ((cc > zst->zst_rbhiwat) && (zst->zst_rx_blocked == 0)) {
   1112   1.8       gwr 		zst->zst_rx_blocked = 1;
   1113  1.24   mycroft 		zs_hwiflow(zst);
   1114   1.8       gwr 	}
   1115   1.8       gwr 
   1116   1.1       gwr 	/* Ask for softint() call. */
   1117   1.1       gwr 	cs->cs_softreq = 1;
   1118   1.1       gwr }
   1119   1.1       gwr 
   1120   1.6       gwr /*
   1121   1.6       gwr  * transmitter ready interrupt.  (splzs)
   1122   1.6       gwr  */
   1123   1.6       gwr static void
   1124   1.1       gwr zstty_txint(cs)
   1125   1.1       gwr 	register struct zs_chanstate *cs;
   1126   1.1       gwr {
   1127   1.1       gwr 	register struct zstty_softc *zst;
   1128   1.6       gwr 	register int count;
   1129   1.1       gwr 
   1130   1.1       gwr 	zst = cs->cs_private;
   1131   1.8       gwr 
   1132   1.8       gwr 	/*
   1133   1.8       gwr 	 * If we suspended output for a "held" change,
   1134   1.8       gwr 	 * then handle that now and resume.
   1135   1.8       gwr 	 * Do flow-control changes ASAP.
   1136   1.8       gwr 	 * When the only change is for flow control,
   1137   1.8       gwr 	 * avoid hitting other registers, because that
   1138   1.8       gwr 	 * often makes the stupid zs drop input...
   1139   1.8       gwr 	 */
   1140   1.8       gwr 	if (cs->cs_heldchange) {
   1141  1.25   mycroft 		zs_loadchannelregs(cs);
   1142   1.8       gwr 		cs->cs_heldchange = 0;
   1143   1.8       gwr 		count = zst->zst_heldtbc;
   1144   1.8       gwr 	} else
   1145   1.8       gwr 		count = zst->zst_tbc;
   1146   1.1       gwr 
   1147   1.6       gwr 	/*
   1148   1.6       gwr 	 * If our transmit buffer still has data,
   1149   1.6       gwr 	 * just send the next character.
   1150   1.6       gwr 	 */
   1151   1.1       gwr 	if (count > 0) {
   1152   1.1       gwr 		/* Send the next char. */
   1153   1.6       gwr 		zst->zst_tbc = --count;
   1154   1.2       gwr 		zs_write_data(cs, *zst->zst_tba);
   1155   1.2       gwr 		zst->zst_tba++;
   1156   1.6       gwr 		return;
   1157   1.1       gwr 	}
   1158   1.1       gwr 
   1159   1.6       gwr 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
   1160   1.6       gwr 
   1161   1.6       gwr 	/* Ask the softint routine for more output. */
   1162   1.8       gwr 	zst->zst_tx_busy = 0;
   1163   1.8       gwr 	zst->zst_tx_done = 1;
   1164   1.6       gwr 	cs->cs_softreq = 1;
   1165   1.1       gwr }
   1166   1.1       gwr 
   1167   1.6       gwr /*
   1168   1.6       gwr  * status change interrupt.  (splzs)
   1169   1.6       gwr  */
   1170   1.6       gwr static void
   1171   1.1       gwr zstty_stint(cs)
   1172   1.1       gwr 	register struct zs_chanstate *cs;
   1173   1.1       gwr {
   1174   1.1       gwr 	register struct zstty_softc *zst;
   1175  1.14       gwr 	register u_char rr0, delta;
   1176   1.1       gwr 
   1177   1.1       gwr 	zst = cs->cs_private;
   1178   1.1       gwr 
   1179   1.2       gwr 	rr0 = zs_read_csr(cs);
   1180   1.2       gwr 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
   1181   1.1       gwr 
   1182   1.6       gwr 	/*
   1183   1.6       gwr 	 * Check here for console break, so that we can abort
   1184   1.6       gwr 	 * even when interrupts are locking up the machine.
   1185   1.6       gwr 	 */
   1186   1.6       gwr 	if ((rr0 & ZSRR0_BREAK) &&
   1187   1.1       gwr 		(zst->zst_hwflags & ZS_HWFLAG_CONSOLE))
   1188   1.1       gwr 	{
   1189  1.14       gwr 		zs_abort(cs);
   1190   1.6       gwr 		return;
   1191   1.1       gwr 	}
   1192   1.1       gwr 
   1193  1.22   mycroft 	delta = rr0 ^ cs->cs_rr0;
   1194  1.14       gwr 	cs->cs_rr0 = rr0;
   1195  1.22   mycroft 	if ((delta & cs->cs_rr0_mask) != 0) {
   1196  1.22   mycroft 		cs->cs_rr0_delta |= delta;
   1197  1.14       gwr 
   1198  1.22   mycroft 		/*
   1199  1.22   mycroft 		 * Stop output immediately if we lose the output
   1200  1.22   mycroft 		 * flow control signal or carrier detect.
   1201  1.22   mycroft 		 */
   1202  1.22   mycroft 		if ((~rr0 & cs->cs_rr0_mask) != 0) {
   1203  1.22   mycroft 			zst->zst_tbc = 0;
   1204  1.22   mycroft 			zst->zst_heldtbc = 0;
   1205  1.22   mycroft 		}
   1206  1.22   mycroft 
   1207  1.22   mycroft 		zst->zst_st_check = 1;
   1208   1.8       gwr 	}
   1209   1.6       gwr 
   1210   1.1       gwr 	/* Ask for softint() call. */
   1211   1.1       gwr 	cs->cs_softreq = 1;
   1212   1.1       gwr }
   1213   1.1       gwr 
   1214   1.1       gwr /*
   1215   1.1       gwr  * Print out a ring or fifo overrun error message.
   1216   1.1       gwr  */
   1217   1.1       gwr static void
   1218   1.1       gwr zsoverrun(zst, ptime, what)
   1219   1.1       gwr 	struct zstty_softc *zst;
   1220   1.1       gwr 	long *ptime;
   1221   1.1       gwr 	char *what;
   1222   1.1       gwr {
   1223   1.1       gwr 
   1224   1.1       gwr 	if (*ptime != time.tv_sec) {
   1225   1.1       gwr 		*ptime = time.tv_sec;
   1226   1.1       gwr 		log(LOG_WARNING, "%s: %s overrun\n",
   1227   1.1       gwr 			zst->zst_dev.dv_xname, what);
   1228   1.1       gwr 	}
   1229   1.1       gwr }
   1230   1.1       gwr 
   1231   1.6       gwr /*
   1232   1.6       gwr  * Software interrupt.  Called at zssoft
   1233   1.8       gwr  *
   1234   1.8       gwr  * The main job to be done here is to empty the input ring
   1235   1.8       gwr  * by passing its contents up to the tty layer.  The ring is
   1236   1.8       gwr  * always emptied during this operation, therefore the ring
   1237   1.8       gwr  * must not be larger than the space after "high water" in
   1238   1.8       gwr  * the tty layer, or the tty layer might drop our input.
   1239   1.8       gwr  *
   1240   1.8       gwr  * Note: an "input blockage" condition is assumed to exist if
   1241   1.8       gwr  * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
   1242   1.6       gwr  */
   1243   1.6       gwr static void
   1244   1.1       gwr zstty_softint(cs)
   1245   1.1       gwr 	struct zs_chanstate *cs;
   1246   1.1       gwr {
   1247   1.1       gwr 	register struct zstty_softc *zst;
   1248  1.22   mycroft 	register struct tty *tp;
   1249   1.1       gwr 	register struct linesw *line;
   1250  1.18    scottr 	register int get, c, s, t;
   1251   1.8       gwr 	int ringmask, overrun;
   1252   1.1       gwr 	register u_short ring_data;
   1253  1.14       gwr 	register u_char rr0, delta;
   1254   1.1       gwr 
   1255  1.22   mycroft 	zst = cs->cs_private;
   1256  1.22   mycroft 	tp = zst->zst_tty;
   1257   1.1       gwr 	line = &linesw[tp->t_line];
   1258   1.6       gwr 	ringmask = zst->zst_ringmask;
   1259   1.8       gwr 	overrun = 0;
   1260   1.6       gwr 
   1261   1.6       gwr 	/*
   1262   1.8       gwr 	 * Raise to tty priority while servicing the ring.
   1263   1.6       gwr 	 */
   1264   1.8       gwr 	s = spltty();
   1265   1.1       gwr 
   1266   1.8       gwr 	if (zst->zst_rx_overrun) {
   1267   1.8       gwr 		zst->zst_rx_overrun = 0;
   1268   1.6       gwr 		zsoverrun(zst, &zst->zst_rotime, "ring");
   1269   1.1       gwr 	}
   1270   1.1       gwr 
   1271   1.1       gwr 	/*
   1272   1.1       gwr 	 * Copy data from the receive ring into the tty layer.
   1273   1.1       gwr 	 */
   1274   1.1       gwr 	get = zst->zst_rbget;
   1275   1.1       gwr 	while (get != zst->zst_rbput) {
   1276   1.1       gwr 		ring_data = zst->zst_rbuf[get];
   1277   1.6       gwr 		get = (get + 1) & ringmask;
   1278   1.1       gwr 
   1279   1.1       gwr 		if (ring_data & ZSRR1_DO)
   1280   1.8       gwr 			overrun++;
   1281   1.1       gwr 		/* low byte of ring_data is rr1 */
   1282   1.1       gwr 		c = (ring_data >> 8) & 0xff;
   1283   1.1       gwr 		if (ring_data & ZSRR1_FE)
   1284   1.1       gwr 			c |= TTY_FE;
   1285   1.1       gwr 		if (ring_data & ZSRR1_PE)
   1286   1.1       gwr 			c |= TTY_PE;
   1287   1.1       gwr 
   1288  1.22   mycroft 		(*line->l_rint)(c, tp);
   1289   1.1       gwr 	}
   1290   1.1       gwr 	zst->zst_rbget = get;
   1291   1.1       gwr 
   1292   1.6       gwr 	/*
   1293   1.6       gwr 	 * If the overrun flag is set now, it was set while
   1294   1.6       gwr 	 * copying char/status pairs from the ring, which
   1295   1.6       gwr 	 * means this was a hardware (fifo) overrun.
   1296   1.6       gwr 	 */
   1297   1.8       gwr 	if (overrun) {
   1298   1.6       gwr 		zsoverrun(zst, &zst->zst_fotime, "fifo");
   1299   1.1       gwr 	}
   1300   1.1       gwr 
   1301   1.8       gwr 	/*
   1302   1.8       gwr 	 * We have emptied the input ring.  Maybe unblock input.
   1303   1.8       gwr 	 * Note: an "input blockage" condition is assumed to exist
   1304   1.8       gwr 	 * when EITHER zst_rx_blocked or the TS_TBLOCK flag is set,
   1305   1.8       gwr 	 * so unblock here ONLY if TS_TBLOCK has not been set.
   1306   1.8       gwr 	 */
   1307   1.8       gwr 	if (zst->zst_rx_blocked && ((tp->t_state & TS_TBLOCK) == 0)) {
   1308  1.18    scottr 		t = splzs();
   1309   1.8       gwr 		zst->zst_rx_blocked = 0;
   1310  1.24   mycroft 		zs_hwiflow(zst);
   1311  1.18    scottr 		splx(t);
   1312   1.8       gwr 	}
   1313   1.8       gwr 
   1314   1.8       gwr 	/*
   1315   1.8       gwr 	 * Do any deferred work for status interrupts.
   1316   1.8       gwr 	 * The rr0 was saved in the h/w interrupt to
   1317   1.8       gwr 	 * avoid another splzs in here.
   1318   1.8       gwr 	 */
   1319   1.8       gwr 	if (zst->zst_st_check) {
   1320   1.8       gwr 		zst->zst_st_check = 0;
   1321   1.8       gwr 
   1322  1.18    scottr 		t = splzs();
   1323  1.13       gwr 		rr0 = cs->cs_rr0;
   1324  1.13       gwr 		delta = cs->cs_rr0_delta;
   1325  1.13       gwr 		cs->cs_rr0_delta = 0;
   1326  1.18    scottr 		splx(t);
   1327  1.14       gwr 
   1328  1.22   mycroft 		if ((delta & cs->cs_rr0_dcd) != 0) {
   1329  1.22   mycroft 			/*
   1330  1.22   mycroft 			 * Inform the tty layer that carrier detect changed.
   1331  1.22   mycroft 			 */
   1332  1.22   mycroft 			(void) (*line->l_modem)(tp, (rr0 & cs->cs_rr0_dcd) != 0);
   1333   1.8       gwr 		}
   1334  1.14       gwr 
   1335  1.22   mycroft 		if ((delta & cs->cs_rr0_cts) != 0) {
   1336  1.22   mycroft 			/* Block or unblock output according to flow control. */
   1337  1.22   mycroft 			if ((rr0 & cs->cs_rr0_cts) != 0) {
   1338   1.8       gwr 				zst->zst_tx_stopped = 0;
   1339   1.8       gwr 				(*line->l_start)(tp);
   1340  1.22   mycroft 			} else {
   1341  1.22   mycroft 				zst->zst_tx_stopped = 1;
   1342   1.1       gwr 			}
   1343   1.1       gwr 		}
   1344   1.8       gwr 	}
   1345   1.8       gwr 
   1346   1.8       gwr 	if (zst->zst_tx_done) {
   1347   1.8       gwr 		zst->zst_tx_done = 0;
   1348  1.22   mycroft 
   1349   1.1       gwr 		tp->t_state &= ~TS_BUSY;
   1350   1.1       gwr 		if (tp->t_state & TS_FLUSH)
   1351   1.1       gwr 			tp->t_state &= ~TS_FLUSH;
   1352   1.1       gwr 		else
   1353  1.22   mycroft 			ndflush(&tp->t_outq,
   1354  1.22   mycroft 			    (int)(zst->zst_tba - tp->t_outq.c_cf));
   1355  1.22   mycroft 		(*line->l_start)(tp);
   1356   1.1       gwr 	}
   1357   1.1       gwr 
   1358   1.6       gwr 	splx(s);
   1359   1.1       gwr }
   1360   1.1       gwr 
   1361   1.1       gwr struct zsops zsops_tty = {
   1362   1.1       gwr 	zstty_rxint,	/* receive char available */
   1363   1.1       gwr 	zstty_stint,	/* external/status */
   1364   1.1       gwr 	zstty_txint,	/* xmit buffer empty */
   1365   1.1       gwr 	zstty_softint,	/* process software interrupt */
   1366   1.1       gwr };
   1367   1.1       gwr 
   1368