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