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