Home | History | Annotate | Line # | Download | only in ic
z8530tty.c revision 1.57
      1  1.57   mycroft /*	$NetBSD: z8530tty.c,v 1.57 1999/02/03 20:15:51 mycroft Exp $	*/
      2  1.21   mycroft 
      3  1.21   mycroft /*-
      4  1.57   mycroft  * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998, 1999
      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.57   mycroft  *
     97  1.57   mycroft  * The driver was massively overhauled in November 1997 by Charles Hannum,
     98  1.57   mycroft  * fixing *many* bugs, and substantially improving performance.
     99   1.1       gwr  */
    100   1.1       gwr 
    101   1.1       gwr #include <sys/param.h>
    102   1.1       gwr #include <sys/systm.h>
    103   1.1       gwr #include <sys/proc.h>
    104   1.1       gwr #include <sys/device.h>
    105   1.1       gwr #include <sys/conf.h>
    106   1.1       gwr #include <sys/file.h>
    107   1.1       gwr #include <sys/ioctl.h>
    108   1.6       gwr #include <sys/malloc.h>
    109   1.1       gwr #include <sys/tty.h>
    110   1.1       gwr #include <sys/time.h>
    111   1.1       gwr #include <sys/kernel.h>
    112   1.1       gwr #include <sys/syslog.h>
    113   1.1       gwr 
    114   1.1       gwr #include <dev/ic/z8530reg.h>
    115   1.1       gwr #include <machine/z8530var.h>
    116   1.1       gwr 
    117  1.52  drochner #include <dev/cons.h>
    118  1.52  drochner 
    119  1.17       jtk #include "locators.h"
    120  1.17       jtk 
    121   1.1       gwr /*
    122   1.1       gwr  * How many input characters we can buffer.
    123   1.1       gwr  * The port-specific var.h may override this.
    124   1.1       gwr  * Note: must be a power of two!
    125   1.1       gwr  */
    126   1.1       gwr #ifndef	ZSTTY_RING_SIZE
    127  1.36   mycroft #define	ZSTTY_RING_SIZE	2048
    128   1.1       gwr #endif
    129   1.6       gwr 
    130   1.6       gwr /*
    131   1.6       gwr  * Make this an option variable one can patch.
    132   1.6       gwr  * But be warned:  this must be a power of 2!
    133   1.6       gwr  */
    134  1.35   mycroft u_int zstty_rbuf_size = ZSTTY_RING_SIZE;
    135   1.1       gwr 
    136  1.35   mycroft /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
    137  1.35   mycroft u_int zstty_rbuf_hiwat = (ZSTTY_RING_SIZE * 1) / 4;
    138  1.35   mycroft u_int zstty_rbuf_lowat = (ZSTTY_RING_SIZE * 3) / 4;
    139   1.8       gwr 
    140   1.1       gwr struct zstty_softc {
    141   1.1       gwr 	struct	device zst_dev;		/* required first: base device */
    142   1.1       gwr 	struct  tty *zst_tty;
    143   1.1       gwr 	struct	zs_chanstate *zst_cs;
    144   1.1       gwr 
    145  1.35   mycroft 	u_int zst_overflows,
    146  1.35   mycroft 	      zst_floods,
    147  1.35   mycroft 	      zst_errors;
    148  1.35   mycroft 
    149  1.35   mycroft 	int zst_hwflags,	/* see z8530var.h */
    150  1.35   mycroft 	    zst_swflags;	/* TIOCFLAG_SOFTCAR, ... <ttycom.h> */
    151  1.35   mycroft 
    152  1.35   mycroft 	u_int zst_r_hiwat,
    153  1.35   mycroft 	      zst_r_lowat;
    154  1.35   mycroft 	u_char *volatile zst_rbget,
    155  1.35   mycroft 	       *volatile zst_rbput;
    156  1.35   mycroft 	volatile u_int zst_rbavail;
    157  1.35   mycroft 	u_char *zst_rbuf,
    158  1.35   mycroft 	       *zst_ebuf;
    159   1.1       gwr 
    160   1.1       gwr 	/*
    161   1.1       gwr 	 * The transmit byte count and address are used for pseudo-DMA
    162   1.1       gwr 	 * output in the hardware interrupt code.  PDMA can be suspended
    163   1.1       gwr 	 * to get pending changes done; heldtbc is used for this.  It can
    164   1.1       gwr 	 * also be stopped for ^S; this sets TS_TTSTOP in tp->t_state.
    165   1.1       gwr 	 */
    166  1.35   mycroft 	u_char *zst_tba;		/* transmit buffer address */
    167  1.35   mycroft 	u_int zst_tbc,			/* transmit byte count */
    168  1.35   mycroft 	      zst_heldtbc;		/* held tbc while xmission stopped */
    169   1.1       gwr 
    170   1.8       gwr 	/* Flags to communicate with zstty_softint() */
    171  1.35   mycroft 	volatile u_char zst_rx_flags,	/* receiver blocked */
    172  1.35   mycroft #define	RX_TTY_BLOCKED		0x01
    173  1.35   mycroft #define	RX_TTY_OVERFLOWED	0x02
    174  1.35   mycroft #define	RX_IBUF_BLOCKED		0x04
    175  1.35   mycroft #define	RX_IBUF_OVERFLOWED	0x08
    176  1.35   mycroft #define	RX_ANY_BLOCK		0x0f
    177  1.35   mycroft 			zst_tx_busy,	/* working on an output chunk */
    178  1.35   mycroft 			zst_tx_done,	/* done with one output chunk */
    179  1.35   mycroft 			zst_tx_stopped,	/* H/W level stop (lost CTS) */
    180  1.35   mycroft 			zst_st_check,	/* got a status interrupt */
    181  1.35   mycroft 			zst_rx_ready;
    182   1.1       gwr };
    183   1.1       gwr 
    184  1.35   mycroft /* Macros to clear/set/test flags. */
    185  1.35   mycroft #define SET(t, f)	(t) |= (f)
    186  1.35   mycroft #define CLR(t, f)	(t) &= ~(f)
    187  1.35   mycroft #define ISSET(t, f)	((t) & (f))
    188   1.1       gwr 
    189   1.1       gwr /* Definition of the driver for autoconfig. */
    190  1.14       gwr static int	zstty_match(struct device *, struct cfdata *, void *);
    191   1.1       gwr static void	zstty_attach(struct device *, struct device *, void *);
    192   1.1       gwr 
    193   1.4   thorpej struct cfattach zstty_ca = {
    194   1.4   thorpej 	sizeof(struct zstty_softc), zstty_match, zstty_attach
    195   1.4   thorpej };
    196   1.4   thorpej 
    197  1.42   thorpej extern struct cfdriver zstty_cd;
    198   1.1       gwr 
    199   1.1       gwr struct zsops zsops_tty;
    200   1.1       gwr 
    201   1.1       gwr /* Routines called from other code. */
    202   1.1       gwr cdev_decl(zs);	/* open, close, read, write, ioctl, stop, ... */
    203   1.1       gwr 
    204  1.45   mycroft static void zs_shutdown __P((struct zstty_softc *));
    205  1.14       gwr static void	zsstart __P((struct tty *));
    206  1.14       gwr static int	zsparam __P((struct tty *, struct termios *));
    207  1.54  christos static void zs_modem __P((struct zstty_softc *, int));
    208  1.54  christos static void tiocm_to_zs __P((struct zstty_softc *, struct zs_chanstate *,
    209  1.54  christos     int, int));
    210  1.54  christos static int  zs_to_tiocm __P((struct zs_chanstate *));
    211  1.54  christos static int    zshwiflow __P((struct tty *, int));
    212  1.54  christos static void  zs_hwiflow __P((struct zstty_softc *));
    213   1.1       gwr 
    214  1.57   mycroft /* Low-level routines. */
    215  1.57   mycroft static void zstty_rxint   __P((struct zs_chanstate *));
    216  1.57   mycroft static void zstty_stint   __P((struct zs_chanstate *, int));
    217  1.57   mycroft static void zstty_txint   __P((struct zs_chanstate *));
    218  1.57   mycroft static void zstty_softint __P((struct zs_chanstate *));
    219  1.57   mycroft 
    220  1.47   mycroft #define	ZSUNIT(x)	(minor(x) & 0x7ffff)
    221  1.47   mycroft #define	ZSDIALOUT(x)	(minor(x) & 0x80000)
    222  1.47   mycroft 
    223   1.1       gwr /*
    224   1.1       gwr  * zstty_match: how is this zs channel configured?
    225   1.1       gwr  */
    226   1.1       gwr int
    227  1.14       gwr zstty_match(parent, cf, aux)
    228   1.1       gwr 	struct device *parent;
    229  1.14       gwr 	struct cfdata *cf;
    230  1.14       gwr 	void   *aux;
    231   1.1       gwr {
    232   1.1       gwr 	struct zsc_attach_args *args = aux;
    233   1.1       gwr 
    234   1.1       gwr 	/* Exact match is better than wildcard. */
    235  1.17       jtk 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
    236   1.1       gwr 		return 2;
    237   1.1       gwr 
    238   1.1       gwr 	/* This driver accepts wildcard. */
    239  1.17       jtk 	if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
    240   1.1       gwr 		return 1;
    241   1.1       gwr 
    242   1.1       gwr 	return 0;
    243   1.1       gwr }
    244   1.1       gwr 
    245   1.1       gwr void
    246   1.1       gwr zstty_attach(parent, self, aux)
    247   1.1       gwr 	struct device *parent, *self;
    248   1.1       gwr 	void   *aux;
    249   1.1       gwr 
    250   1.1       gwr {
    251   1.1       gwr 	struct zsc_softc *zsc = (void *) parent;
    252   1.1       gwr 	struct zstty_softc *zst = (void *) self;
    253  1.14       gwr 	struct cfdata *cf = self->dv_cfdata;
    254   1.1       gwr 	struct zsc_attach_args *args = aux;
    255   1.1       gwr 	struct zs_chanstate *cs;
    256   1.1       gwr 	struct tty *tp;
    257  1.34       gwr 	int channel, s, tty_unit;
    258   1.1       gwr 	dev_t dev;
    259   1.1       gwr 
    260   1.3       gwr 	tty_unit = zst->zst_dev.dv_unit;
    261   1.1       gwr 	channel = args->channel;
    262  1.14       gwr 	cs = zsc->zsc_cs[channel];
    263   1.1       gwr 	cs->cs_private = zst;
    264   1.1       gwr 	cs->cs_ops = &zsops_tty;
    265   1.1       gwr 
    266   1.1       gwr 	zst->zst_cs = cs;
    267   1.1       gwr 	zst->zst_swflags = cf->cf_flags;	/* softcar, etc. */
    268   1.1       gwr 	zst->zst_hwflags = args->hwflags;
    269  1.14       gwr 	dev = makedev(zs_major, tty_unit);
    270   1.1       gwr 
    271   1.1       gwr 	if (zst->zst_swflags)
    272  1.12  christos 		printf(" flags 0x%x", zst->zst_swflags);
    273   1.1       gwr 
    274  1.52  drochner 	if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
    275  1.57   mycroft 		printf(" (console)\n");
    276  1.57   mycroft 		DELAY(20000);
    277  1.52  drochner 		cn_tab->cn_dev = dev;
    278  1.57   mycroft 	} else
    279   1.1       gwr #ifdef KGDB
    280  1.57   mycroft 	if (zs_check_kgdb(cs, dev)) {
    281   1.1       gwr 		/*
    282  1.15       gwr 		 * Allow kgdb to "take over" this port.  Returns true
    283  1.15       gwr 		 * if this serial port is in-use by kgdb.
    284   1.1       gwr 		 */
    285  1.57   mycroft 		printf(" (kgdb)\n");
    286  1.57   mycroft 		/*
    287  1.57   mycroft 		 * This is the kgdb port (exclusive use)
    288  1.57   mycroft 		 * so skip the normal attach code.
    289  1.57   mycroft 		 */
    290  1.57   mycroft 		return;
    291  1.57   mycroft 	} else
    292   1.1       gwr #endif
    293  1.57   mycroft 		printf("\n");
    294   1.1       gwr 
    295   1.6       gwr 	tp = ttymalloc();
    296  1.49  wrstuden 	tp->t_dev = dev;
    297   1.1       gwr 	tp->t_oproc = zsstart;
    298   1.1       gwr 	tp->t_param = zsparam;
    299   1.8       gwr 	tp->t_hwiflow = zshwiflow;
    300   1.9       gwr 	tty_attach(tp);
    301   1.1       gwr 
    302   1.6       gwr 	zst->zst_tty = tp;
    303  1.35   mycroft 	zst->zst_rbuf = malloc(zstty_rbuf_size << 1, M_DEVBUF, M_WAITOK);
    304  1.35   mycroft 	zst->zst_ebuf = zst->zst_rbuf + (zstty_rbuf_size << 1);
    305  1.35   mycroft 	/* Disable the high water mark. */
    306  1.35   mycroft 	zst->zst_r_hiwat = 0;
    307  1.35   mycroft 	zst->zst_r_lowat = 0;
    308  1.35   mycroft 	zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
    309  1.35   mycroft 	zst->zst_rbavail = zstty_rbuf_size;
    310   1.6       gwr 
    311  1.14       gwr 	/* XXX - Do we need an MD hook here? */
    312  1.14       gwr 
    313   1.1       gwr 	/*
    314   1.1       gwr 	 * Hardware init
    315   1.1       gwr 	 */
    316  1.35   mycroft 	if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
    317  1.14       gwr 		/* Call zsparam similar to open. */
    318  1.14       gwr 		struct termios t;
    319  1.52  drochner 
    320  1.57   mycroft 		/* Setup the "new" parameters in t. */
    321  1.57   mycroft 		t.c_ispeed = 0;
    322  1.57   mycroft 		t.c_ospeed = cs->cs_defspeed;
    323  1.57   mycroft 		t.c_cflag = cs->cs_defcflag;
    324  1.14       gwr 
    325  1.34       gwr 		s = splzs();
    326  1.34       gwr 
    327  1.57   mycroft 		/*
    328  1.57   mycroft 		 * Turn on receiver and status interrupts.
    329  1.57   mycroft 		 * We defer the actual write of the register to zsparam(),
    330  1.57   mycroft 		 * but we must make sure status interrupts are turned on by
    331  1.57   mycroft 		 * the time zsparam() reads the initial rr0 state.
    332  1.57   mycroft 		 */
    333  1.57   mycroft 		SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_SIE);
    334  1.34       gwr 
    335  1.34       gwr 		splx(s);
    336  1.34       gwr 
    337  1.14       gwr 		/* Make sure zsparam will see changes. */
    338  1.14       gwr 		tp->t_ospeed = 0;
    339  1.34       gwr 		(void) zsparam(tp, &t);
    340  1.35   mycroft 
    341  1.43   mycroft 		s = splzs();
    342  1.43   mycroft 
    343  1.34       gwr 		/* Make sure DTR is on now. */
    344  1.34       gwr 		zs_modem(zst, 1);
    345  1.43   mycroft 
    346  1.43   mycroft 		splx(s);
    347   1.1       gwr 	} else {
    348   1.1       gwr 		/* Not the console; may need reset. */
    349  1.34       gwr 		int reset;
    350  1.43   mycroft 
    351  1.43   mycroft 		reset = (channel == 0) ? ZSWR9_A_RESET : ZSWR9_B_RESET;
    352  1.43   mycroft 
    353   1.1       gwr 		s = splzs();
    354  1.43   mycroft 
    355   1.2       gwr 		zs_write_reg(cs, 9, reset);
    356  1.35   mycroft 
    357  1.34       gwr 		/* Will raise DTR in open. */
    358  1.34       gwr 		zs_modem(zst, 0);
    359  1.43   mycroft 
    360  1.43   mycroft 		splx(s);
    361   1.1       gwr 	}
    362   1.1       gwr }
    363   1.1       gwr 
    364   1.1       gwr 
    365   1.1       gwr /*
    366   1.1       gwr  * Return pointer to our tty.
    367   1.1       gwr  */
    368   1.1       gwr struct tty *
    369   1.1       gwr zstty(dev)
    370   1.1       gwr 	dev_t dev;
    371   1.1       gwr {
    372   1.1       gwr 	struct zstty_softc *zst;
    373  1.47   mycroft 	int unit = ZSUNIT(dev);
    374   1.1       gwr 
    375   1.1       gwr #ifdef	DIAGNOSTIC
    376   1.4   thorpej 	if (unit >= zstty_cd.cd_ndevs)
    377   1.1       gwr 		panic("zstty");
    378   1.1       gwr #endif
    379   1.4   thorpej 	zst = zstty_cd.cd_devs[unit];
    380   1.1       gwr 	return (zst->zst_tty);
    381   1.1       gwr }
    382   1.1       gwr 
    383   1.1       gwr 
    384  1.45   mycroft void
    385  1.45   mycroft zs_shutdown(zst)
    386  1.45   mycroft 	struct zstty_softc *zst;
    387  1.45   mycroft {
    388  1.46   mycroft 	struct zs_chanstate *cs = zst->zst_cs;
    389  1.45   mycroft 	struct tty *tp = zst->zst_tty;
    390  1.45   mycroft 	int s;
    391  1.45   mycroft 
    392  1.45   mycroft 	s = splzs();
    393  1.45   mycroft 
    394  1.45   mycroft 	/* If we were asserting flow control, then deassert it. */
    395  1.45   mycroft 	SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
    396  1.45   mycroft 	zs_hwiflow(zst);
    397  1.45   mycroft 
    398  1.45   mycroft 	/* Clear any break condition set with TIOCSBRK. */
    399  1.45   mycroft 	zs_break(cs, 0);
    400  1.45   mycroft 
    401  1.45   mycroft 	/*
    402  1.45   mycroft 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    403  1.45   mycroft 	 * notice even if we immediately open the port again.
    404  1.45   mycroft 	 */
    405  1.45   mycroft 	if (ISSET(tp->t_cflag, HUPCL)) {
    406  1.45   mycroft 		zs_modem(zst, 0);
    407  1.45   mycroft 		(void) tsleep(cs, TTIPRI, ttclos, hz);
    408  1.45   mycroft 	}
    409  1.45   mycroft 
    410  1.45   mycroft 	/* Turn off interrupts if not the console. */
    411  1.57   mycroft 	if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
    412  1.57   mycroft 		CLR(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_SIE);
    413  1.57   mycroft 		cs->cs_creg[1] = cs->cs_preg[1];
    414  1.57   mycroft 		zs_write_reg(cs, 1, cs->cs_creg[1]);
    415  1.57   mycroft 	}
    416  1.45   mycroft 
    417  1.45   mycroft 	splx(s);
    418  1.45   mycroft }
    419  1.45   mycroft 
    420   1.1       gwr /*
    421   1.1       gwr  * Open a zs serial (tty) port.
    422   1.1       gwr  */
    423   1.1       gwr int
    424   1.1       gwr zsopen(dev, flags, mode, p)
    425   1.1       gwr 	dev_t dev;
    426   1.1       gwr 	int flags;
    427   1.1       gwr 	int mode;
    428   1.1       gwr 	struct proc *p;
    429   1.1       gwr {
    430  1.47   mycroft 	int unit = ZSUNIT(dev);
    431  1.45   mycroft 	struct zstty_softc *zst;
    432  1.45   mycroft 	struct zs_chanstate *cs;
    433  1.35   mycroft 	struct tty *tp;
    434  1.45   mycroft 	int s, s2;
    435  1.45   mycroft 	int error;
    436   1.1       gwr 
    437   1.4   thorpej 	if (unit >= zstty_cd.cd_ndevs)
    438   1.1       gwr 		return (ENXIO);
    439   1.4   thorpej 	zst = zstty_cd.cd_devs[unit];
    440  1.45   mycroft 	if (zst == 0)
    441   1.1       gwr 		return (ENXIO);
    442   1.1       gwr 	tp = zst->zst_tty;
    443   1.1       gwr 	cs = zst->zst_cs;
    444   1.1       gwr 
    445   1.1       gwr 	/* If KGDB took the line, then tp==NULL */
    446   1.1       gwr 	if (tp == NULL)
    447   1.1       gwr 		return (EBUSY);
    448   1.1       gwr 
    449  1.35   mycroft 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    450  1.35   mycroft 	    ISSET(tp->t_state, TS_XCLUDE) &&
    451  1.20   mycroft 	    p->p_ucred->cr_uid != 0)
    452   1.1       gwr 		return (EBUSY);
    453   1.1       gwr 
    454   1.1       gwr 	s = spltty();
    455   1.1       gwr 
    456  1.35   mycroft 	/*
    457  1.35   mycroft 	 * Do the following iff this is a first open.
    458  1.35   mycroft 	 */
    459  1.45   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    460  1.14       gwr 		struct termios t;
    461  1.50  wrstuden 
    462  1.50  wrstuden 		tp->t_dev = dev;
    463  1.35   mycroft 
    464  1.14       gwr 		/*
    465  1.35   mycroft 		 * Initialize the termios status to the defaults.  Add in the
    466  1.35   mycroft 		 * sticky bits from TIOCSFLAGS.
    467  1.14       gwr 		 */
    468  1.20   mycroft 		t.c_ispeed = 0;
    469  1.20   mycroft 		t.c_ospeed = cs->cs_defspeed;
    470  1.20   mycroft 		t.c_cflag = cs->cs_defcflag;
    471  1.35   mycroft 		if (ISSET(zst->zst_swflags, TIOCFLAG_CLOCAL))
    472  1.35   mycroft 			SET(t.c_cflag, CLOCAL);
    473  1.35   mycroft 		if (ISSET(zst->zst_swflags, TIOCFLAG_CRTSCTS))
    474  1.35   mycroft 			SET(t.c_cflag, CRTSCTS);
    475  1.40   mycroft 		if (ISSET(zst->zst_swflags, TIOCFLAG_CDTRCTS))
    476  1.40   mycroft 			SET(t.c_cflag, CDTRCTS);
    477  1.35   mycroft 		if (ISSET(zst->zst_swflags, TIOCFLAG_MDMBUF))
    478  1.35   mycroft 			SET(t.c_cflag, MDMBUF);
    479  1.57   mycroft 
    480  1.57   mycroft 		s2 = splzs();
    481  1.57   mycroft 
    482  1.57   mycroft 		/*
    483  1.57   mycroft 		 * Turn on receiver and status interrupts.
    484  1.57   mycroft 		 * We defer the actual write of the register to zsparam(),
    485  1.57   mycroft 		 * but we must make sure status interrupts are turned on by
    486  1.57   mycroft 		 * the time zsparam() reads the initial rr0 state.
    487  1.57   mycroft 		 */
    488  1.57   mycroft 		SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_SIE);
    489  1.57   mycroft 
    490  1.57   mycroft 		splx(s2);
    491  1.57   mycroft 
    492  1.14       gwr 		/* Make sure zsparam will see changes. */
    493  1.14       gwr 		tp->t_ospeed = 0;
    494  1.14       gwr 		(void) zsparam(tp, &t);
    495  1.57   mycroft 
    496  1.14       gwr 		/*
    497  1.14       gwr 		 * Note: zsparam has done: cflag, ispeed, ospeed
    498  1.14       gwr 		 * so we just need to do: iflag, oflag, lflag, cc
    499  1.14       gwr 		 * For "raw" mode, just leave all zeros.
    500  1.14       gwr 		 */
    501  1.35   mycroft 		if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_RAW)) {
    502  1.14       gwr 			tp->t_iflag = TTYDEF_IFLAG;
    503  1.14       gwr 			tp->t_oflag = TTYDEF_OFLAG;
    504  1.14       gwr 			tp->t_lflag = TTYDEF_LFLAG;
    505  1.35   mycroft 		} else {
    506  1.35   mycroft 			tp->t_iflag = 0;
    507  1.35   mycroft 			tp->t_oflag = 0;
    508  1.35   mycroft 			tp->t_lflag = 0;
    509  1.14       gwr 		}
    510  1.19       gwr 		ttychars(tp);
    511   1.1       gwr 		ttsetwater(tp);
    512  1.20   mycroft 
    513  1.43   mycroft 		s2 = splzs();
    514  1.43   mycroft 
    515  1.20   mycroft 		/*
    516  1.20   mycroft 		 * Turn on DTR.  We must always do this, even if carrier is not
    517  1.20   mycroft 		 * present, because otherwise we'd have to use TIOCSDTR
    518  1.28   mycroft 		 * immediately after setting CLOCAL, which applications do not
    519  1.28   mycroft 		 * expect.  We always assert DTR while the device is open
    520  1.28   mycroft 		 * unless explicitly requested to deassert it.
    521  1.20   mycroft 		 */
    522  1.20   mycroft 		zs_modem(zst, 1);
    523  1.20   mycroft 
    524  1.20   mycroft 		/* Clear the input ring, and unblock. */
    525  1.35   mycroft 		zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
    526  1.35   mycroft 		zst->zst_rbavail = zstty_rbuf_size;
    527  1.20   mycroft 		zs_iflush(cs);
    528  1.35   mycroft 		CLR(zst->zst_rx_flags, RX_ANY_BLOCK);
    529  1.24   mycroft 		zs_hwiflow(zst);
    530  1.26   mycroft 
    531  1.26   mycroft 		splx(s2);
    532   1.1       gwr 	}
    533  1.14       gwr 
    534  1.47   mycroft 	splx(s);
    535   1.1       gwr 
    536  1.47   mycroft 	error = ttyopen(tp, ZSDIALOUT(dev), ISSET(flags, O_NONBLOCK));
    537  1.47   mycroft 	if (error)
    538  1.47   mycroft 		goto bad;
    539  1.45   mycroft 
    540  1.45   mycroft 	error = (*linesw[tp->t_line].l_open)(dev, tp);
    541  1.45   mycroft 	if (error)
    542  1.45   mycroft 		goto bad;
    543  1.45   mycroft 
    544  1.45   mycroft 	return (0);
    545  1.45   mycroft 
    546  1.45   mycroft bad:
    547  1.45   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    548  1.45   mycroft 		/*
    549  1.45   mycroft 		 * We failed to open the device, and nobody else had it opened.
    550  1.45   mycroft 		 * Clean up the state as appropriate.
    551  1.45   mycroft 		 */
    552  1.45   mycroft 		zs_shutdown(zst);
    553  1.45   mycroft 	}
    554  1.45   mycroft 
    555   1.1       gwr 	return (error);
    556   1.1       gwr }
    557   1.1       gwr 
    558   1.1       gwr /*
    559   1.1       gwr  * Close a zs serial port.
    560   1.1       gwr  */
    561   1.1       gwr int
    562   1.1       gwr zsclose(dev, flags, mode, p)
    563   1.1       gwr 	dev_t dev;
    564   1.1       gwr 	int flags;
    565   1.1       gwr 	int mode;
    566   1.1       gwr 	struct proc *p;
    567   1.1       gwr {
    568  1.47   mycroft 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(dev)];
    569  1.35   mycroft 	struct tty *tp = zst->zst_tty;
    570   1.1       gwr 
    571   1.1       gwr 	/* XXX This is for cons.c. */
    572  1.35   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN))
    573   1.1       gwr 		return 0;
    574   1.1       gwr 
    575   1.1       gwr 	(*linesw[tp->t_line].l_close)(tp, flags);
    576  1.20   mycroft 	ttyclose(tp);
    577  1.20   mycroft 
    578  1.47   mycroft 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    579  1.47   mycroft 		/*
    580  1.47   mycroft 		 * Although we got a last close, the device may still be in
    581  1.47   mycroft 		 * use; e.g. if this was the dialout node, and there are still
    582  1.47   mycroft 		 * processes waiting for carrier on the non-dialout node.
    583  1.47   mycroft 		 */
    584  1.47   mycroft 		zs_shutdown(zst);
    585  1.47   mycroft 	}
    586  1.14       gwr 
    587   1.1       gwr 	return (0);
    588   1.1       gwr }
    589   1.1       gwr 
    590   1.1       gwr /*
    591   1.1       gwr  * Read/write zs serial port.
    592   1.1       gwr  */
    593   1.1       gwr int
    594   1.1       gwr zsread(dev, uio, flags)
    595   1.1       gwr 	dev_t dev;
    596   1.1       gwr 	struct uio *uio;
    597   1.1       gwr 	int flags;
    598   1.1       gwr {
    599  1.47   mycroft 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(dev)];
    600  1.35   mycroft 	struct tty *tp = zst->zst_tty;
    601   1.1       gwr 
    602  1.35   mycroft 	return ((*linesw[tp->t_line].l_read)(tp, uio, flags));
    603   1.1       gwr }
    604   1.1       gwr 
    605   1.1       gwr int
    606   1.1       gwr zswrite(dev, uio, flags)
    607   1.1       gwr 	dev_t dev;
    608   1.1       gwr 	struct uio *uio;
    609   1.1       gwr 	int flags;
    610   1.1       gwr {
    611  1.47   mycroft 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(dev)];
    612  1.35   mycroft 	struct tty *tp = zst->zst_tty;
    613   1.1       gwr 
    614  1.35   mycroft 	return ((*linesw[tp->t_line].l_write)(tp, uio, flags));
    615   1.1       gwr }
    616   1.1       gwr 
    617   1.1       gwr int
    618   1.1       gwr zsioctl(dev, cmd, data, flag, p)
    619   1.1       gwr 	dev_t dev;
    620   1.1       gwr 	u_long cmd;
    621   1.1       gwr 	caddr_t data;
    622   1.1       gwr 	int flag;
    623   1.1       gwr 	struct proc *p;
    624   1.1       gwr {
    625  1.47   mycroft 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(dev)];
    626  1.35   mycroft 	struct zs_chanstate *cs = zst->zst_cs;
    627  1.35   mycroft 	struct tty *tp = zst->zst_tty;
    628  1.35   mycroft 	int error;
    629  1.43   mycroft 	int s;
    630   1.1       gwr 
    631  1.35   mycroft 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    632   1.1       gwr 	if (error >= 0)
    633   1.1       gwr 		return (error);
    634  1.14       gwr 
    635   1.1       gwr 	error = ttioctl(tp, cmd, data, flag, p);
    636   1.1       gwr 	if (error >= 0)
    637   1.1       gwr 		return (error);
    638   1.1       gwr 
    639  1.14       gwr #ifdef	ZS_MD_IOCTL
    640  1.14       gwr 	error = ZS_MD_IOCTL;
    641  1.14       gwr 	if (error >= 0)
    642  1.14       gwr 		return (error);
    643  1.14       gwr #endif	/* ZS_MD_IOCTL */
    644  1.44   mycroft 
    645  1.44   mycroft 	error = 0;
    646  1.14       gwr 
    647  1.43   mycroft 	s = splzs();
    648  1.43   mycroft 
    649   1.1       gwr 	switch (cmd) {
    650   1.1       gwr 	case TIOCSBRK:
    651   1.1       gwr 		zs_break(cs, 1);
    652   1.1       gwr 		break;
    653   1.1       gwr 
    654   1.1       gwr 	case TIOCCBRK:
    655   1.1       gwr 		zs_break(cs, 0);
    656   1.1       gwr 		break;
    657   1.1       gwr 
    658   1.1       gwr 	case TIOCGFLAGS:
    659   1.1       gwr 		*(int *)data = zst->zst_swflags;
    660   1.1       gwr 		break;
    661   1.1       gwr 
    662   1.1       gwr 	case TIOCSFLAGS:
    663   1.1       gwr 		error = suser(p->p_ucred, &p->p_acflag);
    664  1.20   mycroft 		if (error)
    665  1.43   mycroft 			break;
    666  1.20   mycroft 		zst->zst_swflags = *(int *)data;
    667   1.1       gwr 		break;
    668   1.1       gwr 
    669   1.1       gwr 	case TIOCSDTR:
    670   1.1       gwr 		zs_modem(zst, 1);
    671   1.1       gwr 		break;
    672   1.1       gwr 
    673   1.1       gwr 	case TIOCCDTR:
    674   1.1       gwr 		zs_modem(zst, 0);
    675   1.1       gwr 		break;
    676   1.1       gwr 
    677   1.1       gwr 	case TIOCMSET:
    678   1.1       gwr 	case TIOCMBIS:
    679   1.1       gwr 	case TIOCMBIC:
    680  1.54  christos 		tiocm_to_zs(zst, cs, cmd, *(int *)data);
    681  1.54  christos 		break;
    682  1.54  christos 
    683   1.1       gwr 	case TIOCMGET:
    684  1.54  christos 		*(int *)data = zs_to_tiocm(cs);
    685  1.54  christos 		break;
    686  1.54  christos 
    687   1.1       gwr 	default:
    688  1.43   mycroft 		error = ENOTTY;
    689  1.43   mycroft 		break;
    690   1.1       gwr 	}
    691  1.43   mycroft 
    692  1.43   mycroft 	splx(s);
    693  1.43   mycroft 
    694  1.43   mycroft 	return (error);
    695   1.1       gwr }
    696   1.1       gwr 
    697   1.1       gwr /*
    698   1.1       gwr  * Start or restart transmission.
    699   1.1       gwr  */
    700   1.1       gwr static void
    701   1.1       gwr zsstart(tp)
    702  1.35   mycroft 	struct tty *tp;
    703   1.1       gwr {
    704  1.47   mycroft 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(tp->t_dev)];
    705  1.35   mycroft 	struct zs_chanstate *cs = zst->zst_cs;
    706  1.35   mycroft 	int s;
    707   1.1       gwr 
    708   1.1       gwr 	s = spltty();
    709  1.35   mycroft 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
    710   1.1       gwr 		goto out;
    711  1.14       gwr 	if (zst->zst_tx_stopped)
    712  1.35   mycroft 		goto out;
    713   1.8       gwr 
    714   1.1       gwr 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    715  1.35   mycroft 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    716  1.35   mycroft 			CLR(tp->t_state, TS_ASLEEP);
    717   1.1       gwr 			wakeup((caddr_t)&tp->t_outq);
    718   1.1       gwr 		}
    719   1.1       gwr 		selwakeup(&tp->t_wsel);
    720  1.20   mycroft 		if (tp->t_outq.c_cc == 0)
    721  1.35   mycroft 			goto out;
    722   1.1       gwr 	}
    723   1.1       gwr 
    724  1.20   mycroft 	/* Grab the first contiguous region of buffer space. */
    725  1.20   mycroft 	{
    726  1.20   mycroft 		u_char *tba;
    727  1.20   mycroft 		int tbc;
    728  1.20   mycroft 
    729  1.20   mycroft 		tba = tp->t_outq.c_cf;
    730  1.20   mycroft 		tbc = ndqb(&tp->t_outq, 0);
    731  1.20   mycroft 
    732  1.20   mycroft 		(void) splzs();
    733  1.20   mycroft 
    734  1.20   mycroft 		zst->zst_tba = tba;
    735  1.20   mycroft 		zst->zst_tbc = tbc;
    736  1.20   mycroft 	}
    737   1.8       gwr 
    738  1.35   mycroft 	SET(tp->t_state, TS_BUSY);
    739  1.20   mycroft 	zst->zst_tx_busy = 1;
    740   1.1       gwr 
    741  1.20   mycroft 	/* Enable transmit completion interrupts if necessary. */
    742  1.35   mycroft 	if (!ISSET(cs->cs_preg[1], ZSWR1_TIE)) {
    743  1.35   mycroft 		SET(cs->cs_preg[1], ZSWR1_TIE);
    744   1.8       gwr 		cs->cs_creg[1] = cs->cs_preg[1];
    745   1.2       gwr 		zs_write_reg(cs, 1, cs->cs_creg[1]);
    746  1.20   mycroft 	}
    747  1.20   mycroft 
    748  1.20   mycroft 	/* Output the first character of the contiguous buffer. */
    749  1.35   mycroft 	{
    750  1.35   mycroft 		zs_write_data(cs, *zst->zst_tba);
    751  1.35   mycroft 		zst->zst_tbc--;
    752  1.35   mycroft 		zst->zst_tba++;
    753   1.1       gwr 	}
    754   1.1       gwr out:
    755   1.1       gwr 	splx(s);
    756  1.20   mycroft 	return;
    757   1.1       gwr }
    758   1.1       gwr 
    759   1.1       gwr /*
    760   1.1       gwr  * Stop output, e.g., for ^S or output flush.
    761   1.1       gwr  */
    762  1.10   mycroft void
    763   1.1       gwr zsstop(tp, flag)
    764   1.1       gwr 	struct tty *tp;
    765   1.1       gwr 	int flag;
    766   1.1       gwr {
    767  1.47   mycroft 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(tp->t_dev)];
    768  1.35   mycroft 	int s;
    769   1.1       gwr 
    770   1.1       gwr 	s = splzs();
    771  1.35   mycroft 	if (ISSET(tp->t_state, TS_BUSY)) {
    772  1.35   mycroft 		/* Stop transmitting at the next chunk. */
    773   1.1       gwr 		zst->zst_tbc = 0;
    774   1.8       gwr 		zst->zst_heldtbc = 0;
    775  1.35   mycroft 		if (!ISSET(tp->t_state, TS_TTSTOP))
    776  1.35   mycroft 			SET(tp->t_state, TS_FLUSH);
    777   1.1       gwr 	}
    778   1.1       gwr 	splx(s);
    779   1.1       gwr }
    780   1.1       gwr 
    781   1.1       gwr /*
    782   1.1       gwr  * Set ZS tty parameters from termios.
    783   1.1       gwr  * XXX - Should just copy the whole termios after
    784   1.1       gwr  * making sure all the changes could be done.
    785   1.1       gwr  */
    786   1.1       gwr static int
    787   1.1       gwr zsparam(tp, t)
    788  1.35   mycroft 	struct tty *tp;
    789  1.35   mycroft 	struct termios *t;
    790   1.1       gwr {
    791  1.47   mycroft 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(tp->t_dev)];
    792  1.35   mycroft 	struct zs_chanstate *cs = zst->zst_cs;
    793  1.35   mycroft 	int ospeed, cflag;
    794  1.35   mycroft 	u_char tmp3, tmp4, tmp5, tmp15;
    795  1.35   mycroft 	int s, error;
    796   1.1       gwr 
    797  1.35   mycroft 	ospeed = t->c_ospeed;
    798  1.14       gwr 	cflag = t->c_cflag;
    799   1.1       gwr 
    800  1.35   mycroft 	/* Check requested parameters. */
    801  1.35   mycroft 	if (ospeed < 0)
    802  1.35   mycroft 		return (EINVAL);
    803  1.35   mycroft 	if (t->c_ispeed && t->c_ispeed != ospeed)
    804   1.1       gwr 		return (EINVAL);
    805  1.14       gwr 
    806  1.14       gwr 	/*
    807  1.20   mycroft 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    808  1.20   mycroft 	 * is always active.
    809  1.20   mycroft 	 */
    810  1.35   mycroft 	if (ISSET(zst->zst_swflags, TIOCFLAG_SOFTCAR) ||
    811  1.35   mycroft 	    ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
    812  1.35   mycroft 		SET(cflag, CLOCAL);
    813  1.35   mycroft 		CLR(cflag, HUPCL);
    814  1.20   mycroft 	}
    815  1.20   mycroft 
    816  1.20   mycroft 	/*
    817  1.14       gwr 	 * Only whack the UART when params change.
    818  1.14       gwr 	 * Some callers need to clear tp->t_ospeed
    819  1.14       gwr 	 * to make sure initialization gets done.
    820  1.14       gwr 	 */
    821  1.35   mycroft 	if (tp->t_ospeed == ospeed &&
    822  1.20   mycroft 	    tp->t_cflag == cflag)
    823   1.1       gwr 		return (0);
    824   1.1       gwr 
    825  1.14       gwr 	/*
    826  1.14       gwr 	 * Call MD functions to deal with changed
    827  1.14       gwr 	 * clock modes or H/W flow control modes.
    828  1.14       gwr 	 * The BRG divisor is set now. (reg 12,13)
    829  1.14       gwr 	 */
    830  1.35   mycroft 	error = zs_set_speed(cs, ospeed);
    831  1.14       gwr 	if (error)
    832  1.14       gwr 		return (error);
    833  1.14       gwr 	error = zs_set_modes(cs, cflag);
    834  1.14       gwr 	if (error)
    835  1.14       gwr 		return (error);
    836   1.1       gwr 
    837   1.1       gwr 	/*
    838   1.1       gwr 	 * Block interrupts so that state will not
    839   1.1       gwr 	 * be altered until we are done setting it up.
    840  1.14       gwr 	 *
    841   1.1       gwr 	 * Initial values in cs_preg are set before
    842   1.1       gwr 	 * our attach routine is called.  The master
    843   1.1       gwr 	 * interrupt enable is handled by zsc.c
    844  1.14       gwr 	 *
    845   1.1       gwr 	 */
    846  1.14       gwr 	s = splzs();
    847  1.29   mycroft 
    848  1.29   mycroft 	cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
    849  1.35   mycroft 	tmp15 = cs->cs_preg[15];
    850  1.35   mycroft 	if (ISSET(cs->cs_rr0_mask, ZSRR0_DCD))
    851  1.35   mycroft 		SET(tmp15, ZSWR15_DCD_IE);
    852  1.29   mycroft 	else
    853  1.35   mycroft 		CLR(tmp15, ZSWR15_DCD_IE);
    854  1.35   mycroft 	if (ISSET(cs->cs_rr0_mask, ZSRR0_CTS))
    855  1.35   mycroft 		SET(tmp15, ZSWR15_CTS_IE);
    856  1.29   mycroft 	else
    857  1.35   mycroft 		CLR(tmp15, ZSWR15_CTS_IE);
    858  1.35   mycroft 	cs->cs_preg[15] = tmp15;
    859   1.1       gwr 
    860  1.14       gwr 	/* Recompute character size bits. */
    861  1.35   mycroft 	tmp3 = cs->cs_preg[3];
    862  1.35   mycroft 	tmp5 = cs->cs_preg[5];
    863  1.35   mycroft 	CLR(tmp3, ZSWR3_RXSIZE);
    864  1.35   mycroft 	CLR(tmp5, ZSWR5_TXSIZE);
    865  1.35   mycroft 	switch (ISSET(cflag, CSIZE)) {
    866   1.1       gwr 	case CS5:
    867  1.35   mycroft 		SET(tmp3, ZSWR3_RX_5);
    868  1.35   mycroft 		SET(tmp5, ZSWR5_TX_5);
    869   1.1       gwr 		break;
    870   1.1       gwr 	case CS6:
    871  1.35   mycroft 		SET(tmp3, ZSWR3_RX_6);
    872  1.35   mycroft 		SET(tmp5, ZSWR5_TX_6);
    873   1.1       gwr 		break;
    874   1.1       gwr 	case CS7:
    875  1.35   mycroft 		SET(tmp3, ZSWR3_RX_7);
    876  1.35   mycroft 		SET(tmp5, ZSWR5_TX_7);
    877   1.1       gwr 		break;
    878   1.1       gwr 	case CS8:
    879  1.35   mycroft 		SET(tmp3, ZSWR3_RX_8);
    880  1.35   mycroft 		SET(tmp5, ZSWR5_TX_8);
    881   1.1       gwr 		break;
    882   1.1       gwr 	}
    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.35   mycroft 	CLR(tmp4, ZSWR4_SBMASK | ZSWR4_PARMASK);
    893  1.35   mycroft 	if (ISSET(cflag, CSTOPB))
    894  1.35   mycroft 		SET(tmp4, ZSWR4_TWOSB);
    895  1.35   mycroft 	else
    896  1.35   mycroft 		SET(tmp4, ZSWR4_ONESB);
    897  1.35   mycroft 	if (!ISSET(cflag, PARODD))
    898  1.35   mycroft 		SET(tmp4, ZSWR4_EVENP);
    899  1.35   mycroft 	if (ISSET(cflag, PARENB))
    900  1.35   mycroft 		SET(tmp4, ZSWR4_PARENB);
    901   1.1       gwr 	cs->cs_preg[4] = tmp4;
    902   1.1       gwr 
    903  1.35   mycroft 	/* And copy to tty. */
    904  1.35   mycroft 	tp->t_ispeed = 0;
    905  1.35   mycroft 	tp->t_ospeed = ospeed;
    906  1.35   mycroft 	tp->t_cflag = cflag;
    907   1.8       gwr 
    908   1.8       gwr 	/*
    909   1.1       gwr 	 * If nothing is being transmitted, set up new current values,
    910   1.1       gwr 	 * else mark them as pending.
    911   1.1       gwr 	 */
    912  1.25   mycroft 	if (!cs->cs_heldchange) {
    913   1.8       gwr 		if (zst->zst_tx_busy) {
    914   1.1       gwr 			zst->zst_heldtbc = zst->zst_tbc;
    915   1.1       gwr 			zst->zst_tbc = 0;
    916  1.25   mycroft 			cs->cs_heldchange = 1;
    917  1.25   mycroft 		} else
    918   1.1       gwr 			zs_loadchannelregs(cs);
    919   1.1       gwr 	}
    920  1.20   mycroft 
    921  1.57   mycroft 	/*
    922  1.57   mycroft 	 * If hardware flow control is disabled, turn off the buffer water
    923  1.57   mycroft 	 * marks and unblock any soft flow control state.  Otherwise, enable
    924  1.57   mycroft 	 * the water marks.
    925  1.57   mycroft 	 */
    926  1.35   mycroft 	if (!ISSET(cflag, CHWFLOW)) {
    927  1.35   mycroft 		zst->zst_r_hiwat = 0;
    928  1.35   mycroft 		zst->zst_r_lowat = 0;
    929  1.35   mycroft 		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
    930  1.35   mycroft 			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
    931  1.35   mycroft 			zst->zst_rx_ready = 1;
    932  1.35   mycroft 			cs->cs_softreq = 1;
    933  1.35   mycroft 		}
    934  1.35   mycroft 		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
    935  1.35   mycroft 			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
    936  1.33   mycroft 			zs_hwiflow(zst);
    937  1.33   mycroft 		}
    938  1.33   mycroft 	} else {
    939  1.35   mycroft 		zst->zst_r_hiwat = zstty_rbuf_hiwat;
    940  1.35   mycroft 		zst->zst_r_lowat = zstty_rbuf_lowat;
    941  1.33   mycroft 	}
    942  1.33   mycroft 
    943  1.56  wrstuden 	/*
    944  1.57   mycroft 	 * Force a recheck of the hardware carrier and flow control status,
    945  1.57   mycroft 	 * since we may have changed which bits we're looking at.
    946  1.56  wrstuden 	 */
    947  1.57   mycroft 	zstty_stint(cs, 1);
    948  1.56  wrstuden 
    949   1.1       gwr 	splx(s);
    950  1.15       gwr 
    951  1.20   mycroft 	/*
    952  1.57   mycroft 	 * If hardware flow control is disabled, unblock any hard flow control
    953  1.57   mycroft 	 * state.
    954  1.55  wrstuden 	 */
    955  1.35   mycroft 	if (!ISSET(cflag, CHWFLOW)) {
    956  1.14       gwr 		if (zst->zst_tx_stopped) {
    957  1.14       gwr 			zst->zst_tx_stopped = 0;
    958  1.14       gwr 			zsstart(tp);
    959  1.14       gwr 		}
    960  1.14       gwr 	}
    961  1.14       gwr 
    962  1.57   mycroft 	zstty_softint(cs);
    963  1.57   mycroft 
    964   1.1       gwr 	return (0);
    965   1.1       gwr }
    966   1.1       gwr 
    967   1.1       gwr /*
    968   1.1       gwr  * Raise or lower modem control (DTR/RTS) signals.  If a character is
    969   1.1       gwr  * in transmission, the change is deferred.
    970   1.1       gwr  */
    971   1.1       gwr static void
    972   1.1       gwr zs_modem(zst, onoff)
    973   1.1       gwr 	struct zstty_softc *zst;
    974   1.1       gwr 	int onoff;
    975   1.1       gwr {
    976  1.35   mycroft 	struct zs_chanstate *cs = zst->zst_cs;
    977   1.1       gwr 
    978  1.14       gwr 	if (cs->cs_wr5_dtr == 0)
    979  1.14       gwr 		return;
    980   1.1       gwr 
    981  1.24   mycroft 	if (onoff)
    982  1.35   mycroft 		SET(cs->cs_preg[5], cs->cs_wr5_dtr);
    983  1.24   mycroft 	else
    984  1.35   mycroft 		CLR(cs->cs_preg[5], cs->cs_wr5_dtr);
    985  1.14       gwr 
    986  1.25   mycroft 	if (!cs->cs_heldchange) {
    987   1.8       gwr 		if (zst->zst_tx_busy) {
    988   1.1       gwr 			zst->zst_heldtbc = zst->zst_tbc;
    989   1.1       gwr 			zst->zst_tbc = 0;
    990  1.25   mycroft 			cs->cs_heldchange = 1;
    991  1.25   mycroft 		} else
    992  1.25   mycroft 			zs_loadchannelregs(cs);
    993   1.1       gwr 	}
    994  1.54  christos }
    995  1.54  christos 
    996  1.54  christos static void
    997  1.57   mycroft tiocm_to_zs(zst, cs, how, ttybits)
    998  1.54  christos 	struct zstty_softc *zst;
    999  1.54  christos 	struct zs_chanstate *cs;
   1000  1.57   mycroft 	int how, ttybits;
   1001  1.54  christos {
   1002  1.57   mycroft 	int s;
   1003  1.57   mycroft 	u_char zsbits, tmp5;
   1004  1.54  christos 
   1005  1.57   mycroft 	zsbits = 0;
   1006  1.57   mycroft 	if (ISSET(ttybits, TIOCM_DTR))
   1007  1.57   mycroft 		SET(zsbits, ZSWR5_DTR);
   1008  1.57   mycroft 	if (ISSET(ttybits, TIOCM_RTS))
   1009  1.57   mycroft 		SET(zsbits, ZSWR5_RTS);
   1010  1.54  christos 
   1011  1.54  christos 	s = splzs();
   1012  1.54  christos 
   1013  1.57   mycroft 	tmp5 = cs->cs_preg[5];
   1014  1.54  christos 	switch (how) {
   1015  1.54  christos 	case TIOCMBIC:
   1016  1.57   mycroft 		CLR(tmp5, zsbits);
   1017  1.54  christos 		break;
   1018  1.54  christos 
   1019  1.54  christos 	case TIOCMBIS:
   1020  1.57   mycroft 		SET(tmp5, zsbits);
   1021  1.54  christos 		break;
   1022  1.54  christos 
   1023  1.54  christos 	case TIOCMSET:
   1024  1.57   mycroft 		CLR(tmp5, ZSWR5_RTS | ZSWR5_DTR);
   1025  1.57   mycroft 		SET(tmp5, zsbits);
   1026  1.54  christos 		break;
   1027  1.54  christos 	}
   1028  1.57   mycroft 	cs->cs_preg[5] = tmp5;
   1029  1.54  christos 
   1030  1.54  christos 	if (!cs->cs_heldchange) {
   1031  1.54  christos 		if (zst->zst_tx_busy) {
   1032  1.54  christos 			zst->zst_heldtbc = zst->zst_tbc;
   1033  1.54  christos 			zst->zst_tbc = 0;
   1034  1.54  christos 			cs->cs_heldchange = 1;
   1035  1.57   mycroft 		} else
   1036  1.57   mycroft 			zs_loadchannelregs(cs);
   1037  1.54  christos 	}
   1038  1.54  christos 
   1039  1.54  christos 	splx(s);
   1040  1.54  christos }
   1041  1.54  christos 
   1042  1.54  christos static int
   1043  1.54  christos zs_to_tiocm(cs)
   1044  1.54  christos 	struct zs_chanstate *cs;
   1045  1.54  christos {
   1046  1.57   mycroft 	u_char zsbits;
   1047  1.57   mycroft 	int ttybits = 0;
   1048  1.54  christos 
   1049  1.57   mycroft 	zsbits = cs->cs_preg[5];
   1050  1.57   mycroft 	if (ISSET(zsbits, ZSWR5_DTR))
   1051  1.57   mycroft 		SET(ttybits, TIOCM_DTR);
   1052  1.57   mycroft 	if (ISSET(zsbits, ZSWR5_RTS))
   1053  1.57   mycroft 		SET(ttybits, TIOCM_RTS);
   1054  1.57   mycroft 
   1055  1.57   mycroft 	zsbits = cs->cs_rr0;
   1056  1.57   mycroft 	if (ISSET(zsbits, ZSRR0_DCD))
   1057  1.57   mycroft 		SET(ttybits, TIOCM_CD);
   1058  1.57   mycroft 	if (ISSET(zsbits, ZSRR0_CTS))
   1059  1.57   mycroft 		SET(ttybits, TIOCM_CTS);
   1060  1.54  christos 
   1061  1.57   mycroft 	return (ttybits);
   1062   1.1       gwr }
   1063   1.1       gwr 
   1064   1.8       gwr /*
   1065   1.8       gwr  * Try to block or unblock input using hardware flow-control.
   1066   1.8       gwr  * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
   1067   1.8       gwr  * if this function returns non-zero, the TS_TBLOCK flag will
   1068  1.24   mycroft  * be set or cleared according to the "block" arg passed.
   1069   1.8       gwr  */
   1070   1.8       gwr int
   1071  1.24   mycroft zshwiflow(tp, block)
   1072   1.8       gwr 	struct tty *tp;
   1073  1.24   mycroft 	int block;
   1074   1.8       gwr {
   1075  1.47   mycroft 	struct zstty_softc *zst = zstty_cd.cd_devs[ZSUNIT(tp->t_dev)];
   1076  1.35   mycroft 	struct zs_chanstate *cs = zst->zst_cs;
   1077   1.8       gwr 	int s;
   1078   1.8       gwr 
   1079  1.14       gwr 	if (cs->cs_wr5_rts == 0)
   1080  1.14       gwr 		return (0);
   1081   1.8       gwr 
   1082   1.8       gwr 	s = splzs();
   1083  1.24   mycroft 	if (block) {
   1084  1.35   mycroft 		if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
   1085  1.35   mycroft 			SET(zst->zst_rx_flags, RX_TTY_BLOCKED);
   1086  1.24   mycroft 			zs_hwiflow(zst);
   1087  1.24   mycroft 		}
   1088   1.8       gwr 	} else {
   1089  1.35   mycroft 		if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
   1090  1.35   mycroft 			CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
   1091  1.35   mycroft 			zst->zst_rx_ready = 1;
   1092  1.35   mycroft 			cs->cs_softreq = 1;
   1093  1.35   mycroft 		}
   1094  1.35   mycroft 		if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
   1095  1.35   mycroft 			CLR(zst->zst_rx_flags, RX_TTY_BLOCKED);
   1096  1.24   mycroft 			zs_hwiflow(zst);
   1097  1.24   mycroft 		}
   1098   1.8       gwr 	}
   1099   1.8       gwr 	splx(s);
   1100  1.35   mycroft 	return (1);
   1101   1.8       gwr }
   1102   1.8       gwr 
   1103   1.8       gwr /*
   1104   1.8       gwr  * Internal version of zshwiflow
   1105   1.8       gwr  * called at splzs
   1106   1.8       gwr  */
   1107   1.8       gwr static void
   1108  1.24   mycroft zs_hwiflow(zst)
   1109  1.35   mycroft 	struct zstty_softc *zst;
   1110   1.8       gwr {
   1111  1.35   mycroft 	struct zs_chanstate *cs = zst->zst_cs;
   1112   1.8       gwr 
   1113  1.14       gwr 	if (cs->cs_wr5_rts == 0)
   1114  1.14       gwr 		return;
   1115   1.8       gwr 
   1116  1.35   mycroft 	if (ISSET(zst->zst_rx_flags, RX_ANY_BLOCK)) {
   1117  1.35   mycroft 		CLR(cs->cs_preg[5], cs->cs_wr5_rts);
   1118  1.35   mycroft 		CLR(cs->cs_creg[5], cs->cs_wr5_rts);
   1119   1.8       gwr 	} else {
   1120  1.35   mycroft 		SET(cs->cs_preg[5], cs->cs_wr5_rts);
   1121  1.35   mycroft 		SET(cs->cs_creg[5], cs->cs_wr5_rts);
   1122   1.8       gwr 	}
   1123  1.24   mycroft 	zs_write_reg(cs, 5, cs->cs_creg[5]);
   1124   1.8       gwr }
   1125   1.8       gwr 
   1126   1.1       gwr 
   1127   1.1       gwr /****************************************************************
   1128   1.1       gwr  * Interface to the lower layer (zscc)
   1129   1.1       gwr  ****************************************************************/
   1130   1.3       gwr 
   1131  1.35   mycroft #define	integrate	static inline
   1132  1.35   mycroft integrate void zstty_rxsoft __P((struct zstty_softc *, struct tty *));
   1133  1.35   mycroft integrate void zstty_txsoft __P((struct zstty_softc *, struct tty *));
   1134  1.35   mycroft integrate void zstty_stsoft __P((struct zstty_softc *, struct tty *));
   1135  1.35   mycroft static void zstty_diag __P((void *));
   1136   1.1       gwr 
   1137   1.6       gwr /*
   1138   1.8       gwr  * receiver ready interrupt.
   1139   1.8       gwr  * called at splzs
   1140   1.6       gwr  */
   1141   1.6       gwr static void
   1142   1.1       gwr zstty_rxint(cs)
   1143  1.35   mycroft 	struct zs_chanstate *cs;
   1144   1.1       gwr {
   1145  1.35   mycroft 	struct zstty_softc *zst = cs->cs_private;
   1146  1.35   mycroft 	u_char *put, *end;
   1147  1.35   mycroft 	u_int cc;
   1148  1.35   mycroft 	u_char rr0, rr1, c;
   1149   1.1       gwr 
   1150  1.35   mycroft 	end = zst->zst_ebuf;
   1151   1.1       gwr 	put = zst->zst_rbput;
   1152  1.35   mycroft 	cc = zst->zst_rbavail;
   1153   1.1       gwr 
   1154  1.35   mycroft 	while (cc > 0) {
   1155  1.35   mycroft 		/*
   1156  1.35   mycroft 		 * First read the status, because reading the received char
   1157  1.35   mycroft 		 * destroys the status of this char.
   1158  1.35   mycroft 		 */
   1159  1.35   mycroft 		rr1 = zs_read_reg(cs, 1);
   1160  1.35   mycroft 		c = zs_read_data(cs);
   1161  1.35   mycroft 
   1162  1.35   mycroft 		if (ISSET(rr1, ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
   1163  1.35   mycroft 			/* Clear the receive error. */
   1164  1.35   mycroft 			zs_write_csr(cs, ZSWR0_RESET_ERRORS);
   1165  1.35   mycroft 		}
   1166  1.35   mycroft 
   1167  1.35   mycroft 		put[0] = c;
   1168  1.35   mycroft 		put[1] = rr1;
   1169  1.35   mycroft 		put += 2;
   1170  1.35   mycroft 		if (put >= end)
   1171  1.35   mycroft 			put = zst->zst_rbuf;
   1172  1.35   mycroft 		cc--;
   1173  1.35   mycroft 
   1174  1.35   mycroft 		rr0 = zs_read_csr(cs);
   1175  1.35   mycroft 		if (!ISSET(rr0, ZSRR0_RX_READY))
   1176  1.35   mycroft 			break;
   1177  1.35   mycroft 	}
   1178   1.1       gwr 
   1179   1.5       gwr 	/*
   1180  1.35   mycroft 	 * Current string of incoming characters ended because
   1181  1.35   mycroft 	 * no more data was available or we ran out of space.
   1182  1.35   mycroft 	 * Schedule a receive event if any data was received.
   1183  1.35   mycroft 	 * If we're out of space, turn off receive interrupts.
   1184   1.5       gwr 	 */
   1185  1.35   mycroft 	zst->zst_rbput = put;
   1186  1.35   mycroft 	zst->zst_rbavail = cc;
   1187  1.35   mycroft 	if (!ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
   1188  1.35   mycroft 		zst->zst_rx_ready = 1;
   1189  1.35   mycroft 		cs->cs_softreq = 1;
   1190   1.1       gwr 	}
   1191   1.1       gwr 
   1192  1.35   mycroft 	/*
   1193  1.35   mycroft 	 * See if we are in danger of overflowing a buffer. If
   1194  1.35   mycroft 	 * so, use hardware flow control to ease the pressure.
   1195  1.35   mycroft 	 */
   1196  1.35   mycroft 	if (!ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED) &&
   1197  1.35   mycroft 	    cc < zst->zst_r_hiwat) {
   1198  1.35   mycroft 		SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
   1199  1.35   mycroft 		zs_hwiflow(zst);
   1200   1.1       gwr 	}
   1201   1.1       gwr 
   1202   1.8       gwr 	/*
   1203  1.35   mycroft 	 * If we're out of space, disable receive interrupts
   1204  1.35   mycroft 	 * until the queue has drained a bit.
   1205   1.8       gwr 	 */
   1206  1.35   mycroft 	if (!cc) {
   1207  1.35   mycroft 		SET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
   1208  1.35   mycroft 		CLR(cs->cs_preg[1], ZSWR1_RIE);
   1209  1.35   mycroft 		cs->cs_creg[1] = cs->cs_preg[1];
   1210  1.35   mycroft 		zs_write_reg(cs, 1, cs->cs_creg[1]);
   1211   1.8       gwr 	}
   1212   1.8       gwr 
   1213  1.35   mycroft #if 0
   1214  1.35   mycroft 	printf("%xH%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
   1215  1.35   mycroft #endif
   1216   1.1       gwr }
   1217   1.1       gwr 
   1218   1.6       gwr /*
   1219   1.6       gwr  * transmitter ready interrupt.  (splzs)
   1220   1.6       gwr  */
   1221   1.6       gwr static void
   1222   1.1       gwr zstty_txint(cs)
   1223  1.35   mycroft 	struct zs_chanstate *cs;
   1224   1.1       gwr {
   1225  1.35   mycroft 	struct zstty_softc *zst = cs->cs_private;
   1226   1.8       gwr 
   1227   1.8       gwr 	/*
   1228  1.35   mycroft 	 * If we've delayed a parameter change, do it now, and restart
   1229  1.35   mycroft 	 * output.
   1230   1.8       gwr 	 */
   1231   1.8       gwr 	if (cs->cs_heldchange) {
   1232  1.25   mycroft 		zs_loadchannelregs(cs);
   1233   1.8       gwr 		cs->cs_heldchange = 0;
   1234  1.35   mycroft 		zst->zst_tbc = zst->zst_heldtbc;
   1235  1.35   mycroft 		zst->zst_heldtbc = 0;
   1236  1.35   mycroft 	}
   1237   1.1       gwr 
   1238  1.35   mycroft 	/* Output the next character in the buffer, if any. */
   1239  1.48   mycroft 	if (zst->zst_tbc > 0) {
   1240   1.2       gwr 		zs_write_data(cs, *zst->zst_tba);
   1241  1.35   mycroft 		zst->zst_tbc--;
   1242   1.2       gwr 		zst->zst_tba++;
   1243  1.35   mycroft 	} else {
   1244  1.35   mycroft 		/* Disable transmit completion interrupts if necessary. */
   1245  1.35   mycroft 		if (ISSET(cs->cs_preg[1], ZSWR1_TIE)) {
   1246  1.35   mycroft 			CLR(cs->cs_preg[1], ZSWR1_TIE);
   1247  1.35   mycroft 			cs->cs_creg[1] = cs->cs_preg[1];
   1248  1.35   mycroft 			zs_write_reg(cs, 1, cs->cs_creg[1]);
   1249  1.35   mycroft 		}
   1250  1.35   mycroft 		if (zst->zst_tx_busy) {
   1251  1.35   mycroft 			zst->zst_tx_busy = 0;
   1252  1.35   mycroft 			zst->zst_tx_done = 1;
   1253  1.35   mycroft 			cs->cs_softreq = 1;
   1254  1.35   mycroft 		}
   1255   1.1       gwr 	}
   1256   1.1       gwr }
   1257   1.1       gwr 
   1258   1.6       gwr /*
   1259   1.6       gwr  * status change interrupt.  (splzs)
   1260   1.6       gwr  */
   1261   1.6       gwr static void
   1262  1.57   mycroft zstty_stint(cs, force)
   1263  1.35   mycroft 	struct zs_chanstate *cs;
   1264  1.57   mycroft 	int force;
   1265   1.1       gwr {
   1266  1.35   mycroft 	struct zstty_softc *zst = cs->cs_private;
   1267  1.35   mycroft 	u_char rr0, delta;
   1268   1.1       gwr 
   1269   1.2       gwr 	rr0 = zs_read_csr(cs);
   1270   1.2       gwr 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
   1271   1.1       gwr 
   1272   1.6       gwr 	/*
   1273   1.6       gwr 	 * Check here for console break, so that we can abort
   1274   1.6       gwr 	 * even when interrupts are locking up the machine.
   1275   1.6       gwr 	 */
   1276  1.35   mycroft 	if (ISSET(rr0, ZSRR0_BREAK) &&
   1277  1.35   mycroft 	    ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
   1278  1.14       gwr 		zs_abort(cs);
   1279   1.6       gwr 		return;
   1280   1.1       gwr 	}
   1281   1.1       gwr 
   1282  1.57   mycroft 	if (!force)
   1283  1.57   mycroft 		delta = rr0 ^ cs->cs_rr0;
   1284  1.57   mycroft 	else
   1285  1.57   mycroft 		delta = cs->cs_rr0_mask;
   1286  1.14       gwr 	cs->cs_rr0 = rr0;
   1287  1.57   mycroft 
   1288  1.35   mycroft 	if (ISSET(delta, cs->cs_rr0_mask)) {
   1289  1.35   mycroft 		SET(cs->cs_rr0_delta, delta);
   1290  1.14       gwr 
   1291  1.22   mycroft 		/*
   1292  1.22   mycroft 		 * Stop output immediately if we lose the output
   1293  1.22   mycroft 		 * flow control signal or carrier detect.
   1294  1.22   mycroft 		 */
   1295  1.35   mycroft 		if (ISSET(~rr0, cs->cs_rr0_mask)) {
   1296  1.22   mycroft 			zst->zst_tbc = 0;
   1297  1.22   mycroft 			zst->zst_heldtbc = 0;
   1298  1.22   mycroft 		}
   1299  1.22   mycroft 
   1300  1.22   mycroft 		zst->zst_st_check = 1;
   1301  1.35   mycroft 		cs->cs_softreq = 1;
   1302  1.35   mycroft 	}
   1303  1.35   mycroft }
   1304  1.35   mycroft 
   1305  1.35   mycroft void
   1306  1.35   mycroft zstty_diag(arg)
   1307  1.35   mycroft 	void *arg;
   1308  1.35   mycroft {
   1309  1.35   mycroft 	struct zstty_softc *zst = arg;
   1310  1.35   mycroft 	int overflows, floods;
   1311  1.35   mycroft 	int s;
   1312  1.35   mycroft 
   1313  1.35   mycroft 	s = splzs();
   1314  1.35   mycroft 	overflows = zst->zst_overflows;
   1315  1.35   mycroft 	zst->zst_overflows = 0;
   1316  1.35   mycroft 	floods = zst->zst_floods;
   1317  1.35   mycroft 	zst->zst_floods = 0;
   1318  1.35   mycroft 	zst->zst_errors = 0;
   1319  1.35   mycroft 	splx(s);
   1320  1.35   mycroft 
   1321  1.35   mycroft 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
   1322  1.35   mycroft 	    zst->zst_dev.dv_xname,
   1323  1.35   mycroft 	    overflows, overflows == 1 ? "" : "s",
   1324  1.35   mycroft 	    floods, floods == 1 ? "" : "s");
   1325  1.35   mycroft }
   1326  1.35   mycroft 
   1327  1.35   mycroft integrate void
   1328  1.35   mycroft zstty_rxsoft(zst, tp)
   1329  1.35   mycroft 	struct zstty_softc *zst;
   1330  1.35   mycroft 	struct tty *tp;
   1331  1.35   mycroft {
   1332  1.35   mycroft 	struct zs_chanstate *cs = zst->zst_cs;
   1333  1.35   mycroft 	int (*rint) __P((int c, struct tty *tp)) = linesw[tp->t_line].l_rint;
   1334  1.35   mycroft 	u_char *get, *end;
   1335  1.35   mycroft 	u_int cc, scc;
   1336  1.35   mycroft 	u_char rr1;
   1337  1.35   mycroft 	int code;
   1338  1.35   mycroft 	int s;
   1339  1.35   mycroft 
   1340  1.35   mycroft 	end = zst->zst_ebuf;
   1341  1.35   mycroft 	get = zst->zst_rbget;
   1342  1.35   mycroft 	scc = cc = zstty_rbuf_size - zst->zst_rbavail;
   1343  1.35   mycroft 
   1344  1.35   mycroft 	if (cc == zstty_rbuf_size) {
   1345  1.35   mycroft 		zst->zst_floods++;
   1346  1.35   mycroft 		if (zst->zst_errors++ == 0)
   1347  1.35   mycroft 			timeout(zstty_diag, zst, 60 * hz);
   1348  1.35   mycroft 	}
   1349  1.35   mycroft 
   1350  1.35   mycroft 	while (cc) {
   1351  1.37   mycroft 		code = get[0];
   1352  1.35   mycroft 		rr1 = get[1];
   1353  1.37   mycroft 		if (ISSET(rr1, ZSRR1_DO | ZSRR1_FE | ZSRR1_PE)) {
   1354  1.37   mycroft 			if (ISSET(rr1, ZSRR1_DO)) {
   1355  1.37   mycroft 				zst->zst_overflows++;
   1356  1.37   mycroft 				if (zst->zst_errors++ == 0)
   1357  1.37   mycroft 					timeout(zstty_diag, zst, 60 * hz);
   1358  1.37   mycroft 			}
   1359  1.35   mycroft 			if (ISSET(rr1, ZSRR1_FE))
   1360  1.35   mycroft 				SET(code, TTY_FE);
   1361  1.35   mycroft 			if (ISSET(rr1, ZSRR1_PE))
   1362  1.35   mycroft 				SET(code, TTY_PE);
   1363  1.35   mycroft 		}
   1364  1.35   mycroft 		if ((*rint)(code, tp) == -1) {
   1365  1.35   mycroft 			/*
   1366  1.35   mycroft 			 * The line discipline's buffer is out of space.
   1367  1.35   mycroft 			 */
   1368  1.35   mycroft 			if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
   1369  1.35   mycroft 				/*
   1370  1.35   mycroft 				 * We're either not using flow control, or the
   1371  1.35   mycroft 				 * line discipline didn't tell us to block for
   1372  1.35   mycroft 				 * some reason.  Either way, we have no way to
   1373  1.35   mycroft 				 * know when there's more space available, so
   1374  1.35   mycroft 				 * just drop the rest of the data.
   1375  1.35   mycroft 				 */
   1376  1.35   mycroft 				get += cc << 1;
   1377  1.35   mycroft 				if (get >= end)
   1378  1.35   mycroft 					get -= zstty_rbuf_size << 1;
   1379  1.35   mycroft 				cc = 0;
   1380  1.35   mycroft 			} else {
   1381  1.35   mycroft 				/*
   1382  1.35   mycroft 				 * Don't schedule any more receive processing
   1383  1.35   mycroft 				 * until the line discipline tells us there's
   1384  1.35   mycroft 				 * space available (through comhwiflow()).
   1385  1.35   mycroft 				 * Leave the rest of the data in the input
   1386  1.35   mycroft 				 * buffer.
   1387  1.35   mycroft 				 */
   1388  1.35   mycroft 				SET(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
   1389  1.35   mycroft 			}
   1390  1.35   mycroft 			break;
   1391  1.35   mycroft 		}
   1392  1.35   mycroft 		get += 2;
   1393  1.35   mycroft 		if (get >= end)
   1394  1.35   mycroft 			get = zst->zst_rbuf;
   1395  1.35   mycroft 		cc--;
   1396   1.8       gwr 	}
   1397   1.6       gwr 
   1398  1.35   mycroft 	if (cc != scc) {
   1399  1.35   mycroft 		zst->zst_rbget = get;
   1400  1.35   mycroft 		s = splzs();
   1401  1.35   mycroft 		cc = zst->zst_rbavail += scc - cc;
   1402  1.35   mycroft 		/* Buffers should be ok again, release possible block. */
   1403  1.35   mycroft 		if (cc >= zst->zst_r_lowat) {
   1404  1.35   mycroft 			if (ISSET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED)) {
   1405  1.35   mycroft 				CLR(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
   1406  1.35   mycroft 				SET(cs->cs_preg[1], ZSWR1_RIE);
   1407  1.35   mycroft 				cs->cs_creg[1] = cs->cs_preg[1];
   1408  1.35   mycroft 				zs_write_reg(cs, 1, cs->cs_creg[1]);
   1409  1.35   mycroft 			}
   1410  1.35   mycroft 			if (ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED)) {
   1411  1.35   mycroft 				CLR(zst->zst_rx_flags, RX_IBUF_BLOCKED);
   1412  1.35   mycroft 				zs_hwiflow(zst);
   1413  1.35   mycroft 			}
   1414  1.35   mycroft 		}
   1415  1.35   mycroft 		splx(s);
   1416  1.35   mycroft 	}
   1417  1.35   mycroft 
   1418  1.35   mycroft #if 0
   1419  1.35   mycroft 	printf("%xS%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
   1420  1.35   mycroft #endif
   1421   1.1       gwr }
   1422   1.1       gwr 
   1423  1.35   mycroft integrate void
   1424  1.35   mycroft zstty_txsoft(zst, tp)
   1425  1.35   mycroft 	struct zstty_softc *zst;
   1426  1.35   mycroft 	struct tty *tp;
   1427  1.35   mycroft {
   1428  1.35   mycroft 
   1429  1.35   mycroft 	CLR(tp->t_state, TS_BUSY);
   1430  1.35   mycroft 	if (ISSET(tp->t_state, TS_FLUSH))
   1431  1.35   mycroft 		CLR(tp->t_state, TS_FLUSH);
   1432  1.35   mycroft 	else
   1433  1.35   mycroft 		ndflush(&tp->t_outq, (int)(zst->zst_tba - tp->t_outq.c_cf));
   1434  1.35   mycroft 	(*linesw[tp->t_line].l_start)(tp);
   1435  1.35   mycroft }
   1436  1.35   mycroft 
   1437  1.35   mycroft integrate void
   1438  1.35   mycroft zstty_stsoft(zst, tp)
   1439   1.1       gwr 	struct zstty_softc *zst;
   1440  1.35   mycroft 	struct tty *tp;
   1441   1.1       gwr {
   1442  1.35   mycroft 	struct zs_chanstate *cs = zst->zst_cs;
   1443  1.35   mycroft 	u_char rr0, delta;
   1444  1.35   mycroft 	int s;
   1445  1.35   mycroft 
   1446  1.35   mycroft 	s = splzs();
   1447  1.35   mycroft 	rr0 = cs->cs_rr0;
   1448  1.35   mycroft 	delta = cs->cs_rr0_delta;
   1449  1.35   mycroft 	cs->cs_rr0_delta = 0;
   1450  1.35   mycroft 	splx(s);
   1451  1.35   mycroft 
   1452  1.35   mycroft 	if (ISSET(delta, cs->cs_rr0_dcd)) {
   1453  1.35   mycroft 		/*
   1454  1.35   mycroft 		 * Inform the tty layer that carrier detect changed.
   1455  1.35   mycroft 		 */
   1456  1.35   mycroft 		(void) (*linesw[tp->t_line].l_modem)(tp, ISSET(rr0, ZSRR0_DCD));
   1457  1.35   mycroft 	}
   1458   1.1       gwr 
   1459  1.35   mycroft 	if (ISSET(delta, cs->cs_rr0_cts)) {
   1460  1.35   mycroft 		/* Block or unblock output according to flow control. */
   1461  1.35   mycroft 		if (ISSET(rr0, cs->cs_rr0_cts)) {
   1462  1.35   mycroft 			zst->zst_tx_stopped = 0;
   1463  1.35   mycroft 			(*linesw[tp->t_line].l_start)(tp);
   1464  1.35   mycroft 		} else {
   1465  1.35   mycroft 			zst->zst_tx_stopped = 1;
   1466  1.35   mycroft 		}
   1467   1.1       gwr 	}
   1468   1.1       gwr }
   1469   1.1       gwr 
   1470   1.6       gwr /*
   1471   1.6       gwr  * Software interrupt.  Called at zssoft
   1472   1.8       gwr  *
   1473   1.8       gwr  * The main job to be done here is to empty the input ring
   1474   1.8       gwr  * by passing its contents up to the tty layer.  The ring is
   1475   1.8       gwr  * always emptied during this operation, therefore the ring
   1476   1.8       gwr  * must not be larger than the space after "high water" in
   1477   1.8       gwr  * the tty layer, or the tty layer might drop our input.
   1478   1.8       gwr  *
   1479   1.8       gwr  * Note: an "input blockage" condition is assumed to exist if
   1480   1.8       gwr  * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
   1481   1.6       gwr  */
   1482   1.6       gwr static void
   1483   1.1       gwr zstty_softint(cs)
   1484   1.1       gwr 	struct zs_chanstate *cs;
   1485   1.1       gwr {
   1486  1.35   mycroft 	struct zstty_softc *zst = cs->cs_private;
   1487  1.35   mycroft 	struct tty *tp = zst->zst_tty;
   1488  1.35   mycroft 	int s;
   1489   1.1       gwr 
   1490   1.8       gwr 	s = spltty();
   1491   1.1       gwr 
   1492  1.35   mycroft 	if (zst->zst_rx_ready) {
   1493  1.35   mycroft 		zst->zst_rx_ready = 0;
   1494  1.35   mycroft 		zstty_rxsoft(zst, tp);
   1495   1.1       gwr 	}
   1496   1.1       gwr 
   1497   1.8       gwr 	if (zst->zst_st_check) {
   1498   1.8       gwr 		zst->zst_st_check = 0;
   1499  1.35   mycroft 		zstty_stsoft(zst, tp);
   1500   1.8       gwr 	}
   1501   1.8       gwr 
   1502   1.8       gwr 	if (zst->zst_tx_done) {
   1503   1.8       gwr 		zst->zst_tx_done = 0;
   1504  1.35   mycroft 		zstty_txsoft(zst, tp);
   1505   1.1       gwr 	}
   1506   1.1       gwr 
   1507   1.6       gwr 	splx(s);
   1508   1.1       gwr }
   1509   1.1       gwr 
   1510   1.1       gwr struct zsops zsops_tty = {
   1511   1.1       gwr 	zstty_rxint,	/* receive char available */
   1512   1.1       gwr 	zstty_stint,	/* external/status */
   1513   1.1       gwr 	zstty_txint,	/* xmit buffer empty */
   1514   1.1       gwr 	zstty_softint,	/* process software interrupt */
   1515   1.1       gwr };
   1516