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