Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.75
      1  1.75  dholland /*	$NetBSD: zs.c,v 1.75 2014/03/16 05:20:23 dholland Exp $	*/
      2   1.1       leo 
      3   1.1       leo /*
      4   1.1       leo  * Copyright (c) 1992, 1993
      5   1.1       leo  *	The Regents of the University of California.  All rights reserved.
      6   1.1       leo  *
      7   1.1       leo  * This software was developed by the Computer Systems Engineering group
      8   1.1       leo  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9   1.1       leo  * contributed to Berkeley.
     10   1.1       leo  *
     11   1.1       leo  *
     12   1.1       leo  * All advertising materials mentioning features or use of this software
     13   1.1       leo  * must display the following acknowledgement:
     14   1.1       leo  *	This product includes software developed by the University of
     15   1.1       leo  *	California, Lawrence Berkeley Laboratory.
     16   1.1       leo  *
     17   1.1       leo  * Redistribution and use in source and binary forms, with or without
     18   1.1       leo  * modification, are permitted provided that the following conditions
     19   1.1       leo  * are met:
     20   1.1       leo  * 1. Redistributions of source code must retain the above copyright
     21   1.1       leo  *    notice, this list of conditions and the following disclaimer.
     22   1.1       leo  * 2. Redistributions in binary form must reproduce the above copyright
     23   1.1       leo  *    notice, this list of conditions and the following disclaimer in the
     24   1.1       leo  *    documentation and/or other materials provided with the distribution.
     25  1.41       agc  * 3. Neither the name of the University nor the names of its contributors
     26  1.41       agc  *    may be used to endorse or promote products derived from this software
     27  1.41       agc  *    without specific prior written permission.
     28  1.41       agc  *
     29  1.41       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  1.41       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  1.41       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  1.41       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  1.41       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  1.41       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  1.41       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  1.41       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  1.41       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  1.41       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  1.41       agc  * SUCH DAMAGE.
     40  1.41       agc  *
     41  1.41       agc  *	@(#)zs.c	8.1 (Berkeley) 7/19/93
     42  1.41       agc  */
     43  1.41       agc 
     44  1.42       leo /*-
     45  1.42       leo  * Copyright (c) 1995 The NetBSD Foundation, Inc. (Atari modifications)
     46  1.42       leo  * All rights reserved.
     47  1.41       agc  *
     48  1.42       leo  * This code is derived from software contributed to The NetBSD Foundation
     49  1.42       leo  * by Leo Weppelman.
     50  1.41       agc  *
     51  1.41       agc  * Redistribution and use in source and binary forms, with or without
     52  1.41       agc  * modification, are permitted provided that the following conditions
     53  1.41       agc  * are met:
     54  1.41       agc  * 1. Redistributions of source code must retain the above copyright
     55  1.41       agc  *    notice, this list of conditions and the following disclaimer.
     56  1.41       agc  * 2. Redistributions in binary form must reproduce the above copyright
     57  1.41       agc  *    notice, this list of conditions and the following disclaimer in the
     58  1.41       agc  *    documentation and/or other materials provided with the distribution.
     59   1.1       leo  *
     60  1.42       leo  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     61  1.42       leo  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     62  1.42       leo  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     63  1.42       leo  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     64  1.42       leo  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     65  1.42       leo  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     66  1.42       leo  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     67  1.42       leo  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     68  1.42       leo  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     69  1.42       leo  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     70  1.42       leo  * POSSIBILITY OF SUCH DAMAGE.
     71   1.1       leo  */
     72   1.1       leo 
     73   1.1       leo /*
     74   1.1       leo  * Zilog Z8530 (ZSCC) driver.
     75   1.1       leo  *
     76   1.1       leo  * Runs two tty ports (modem2 and serial2) on zs0.
     77   1.1       leo  *
     78   1.1       leo  * This driver knows far too much about chip to usage mappings.
     79   1.1       leo  */
     80  1.40     lukem 
     81  1.40     lukem #include <sys/cdefs.h>
     82  1.75  dholland __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.75 2014/03/16 05:20:23 dholland Exp $");
     83  1.40     lukem 
     84   1.1       leo #include <sys/param.h>
     85   1.9       leo #include <sys/systm.h>
     86   1.1       leo #include <sys/proc.h>
     87   1.1       leo #include <sys/device.h>
     88   1.1       leo #include <sys/conf.h>
     89   1.1       leo #include <sys/file.h>
     90   1.1       leo #include <sys/ioctl.h>
     91  1.13       leo #include <sys/malloc.h>
     92   1.1       leo #include <sys/tty.h>
     93   1.1       leo #include <sys/time.h>
     94   1.1       leo #include <sys/kernel.h>
     95   1.1       leo #include <sys/syslog.h>
     96  1.47      elad #include <sys/kauth.h>
     97   1.1       leo 
     98   1.1       leo #include <machine/cpu.h>
     99   1.1       leo #include <machine/iomap.h>
    100   1.1       leo #include <machine/scu.h>
    101   1.1       leo #include <machine/mfp.h>
    102  1.18       leo #include <atari/dev/ym2149reg.h>
    103   1.1       leo 
    104   1.7       cgd #include <dev/ic/z8530reg.h>
    105   1.1       leo #include <atari/dev/zsvar.h>
    106  1.65   tsutsui 
    107  1.65   tsutsui #include "ioconf.h"
    108  1.65   tsutsui 
    109   1.1       leo #include "zs.h"
    110   1.1       leo #if NZS > 1
    111   1.1       leo #error "This driver supports only 1 85C30!"
    112   1.1       leo #endif
    113   1.1       leo 
    114   1.1       leo #if NZS > 0
    115   1.1       leo 
    116  1.12       leo #define PCLK	(8053976)	/* PCLK pin input clock rate */
    117  1.30   mycroft #define PCLK_HD	(9600 * 1536)	/* PCLK on Hades pin input clock rate */
    118   1.1       leo 
    119   1.1       leo #define splzs	spl5
    120   1.1       leo 
    121   1.1       leo /*
    122   1.1       leo  * Software state per found chip.
    123   1.1       leo  */
    124   1.1       leo struct zs_softc {
    125  1.69   tsutsui 	device_t sc_dev;		/* base device */
    126  1.72   tsutsui 	struct zs_chanstate *sc_cs[2];	/* chan A and B software state */
    127  1.72   tsutsui 
    128  1.72   tsutsui 	struct zs_chanstate sc_cs_store[2];
    129  1.72   tsutsui 	void *sc_sicookie;		/* for callback */
    130   1.1       leo };
    131   1.1       leo 
    132   1.1       leo /*
    133   1.1       leo  * Define the registers for a closed port
    134   1.1       leo  */
    135  1.66   tsutsui static uint8_t zs_init_regs[16] = {
    136   1.1       leo /*  0 */	0,
    137   1.1       leo /*  1 */	0,
    138   1.1       leo /*  2 */	0x60,
    139   1.1       leo /*  3 */	0,
    140   1.1       leo /*  4 */	0,
    141   1.1       leo /*  5 */	0,
    142   1.1       leo /*  6 */	0,
    143   1.1       leo /*  7 */	0,
    144   1.1       leo /*  8 */	0,
    145  1.13       leo /*  9 */	ZSWR9_MASTER_IE | ZSWR9_VECTOR_INCL_STAT,
    146   1.1       leo /* 10 */	ZSWR10_NRZ,
    147   1.1       leo /* 11 */	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    148   1.1       leo /* 12 */	0,
    149   1.1       leo /* 13 */	0,
    150   1.1       leo /* 14 */	ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
    151   1.1       leo /* 15 */	0
    152   1.1       leo };
    153   1.1       leo 
    154   1.6       leo /*
    155  1.74       wiz  * Define the machine dependent clock frequencies
    156   1.6       leo  * If BRgen feeds sender/receiver we always use a
    157   1.6       leo  * divisor 16, therefor the division by 16 can as
    158   1.6       leo  * well be done here.
    159   1.6       leo  */
    160  1.71   tsutsui static const u_long zs_freqs_tt[] = {
    161   1.6       leo 	/*
    162   1.6       leo 	 * Atari TT, RTxCB is generated by TT-MFP timer C,
    163  1.46     lukem 	 * which is set to 307.2 kHz during initialisation
    164   1.6       leo 	 * and never changed afterwards.
    165   1.6       leo 	 */
    166   1.6       leo 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    167   1.6       leo 	 229500,	/* BRgen, RTxCA, divisor 16	*/
    168   1.6       leo 	3672000,	/* RTxCA, from PCLK4		*/
    169   1.6       leo 	      0,	/* TRxCA, external		*/
    170   1.6       leo 
    171   1.6       leo 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    172   1.6       leo 	  19200,	/* BRgen, RTxCB, divisor 16	*/
    173   1.6       leo 	 307200,	/* RTxCB, from TT-MFP TCO	*/
    174   1.6       leo 	2457600		/* TRxCB, from BCLK		*/
    175   1.6       leo };
    176  1.24       leo 
    177  1.71   tsutsui static const u_long zs_freqs_falcon[] = {
    178   1.6       leo 	/*
    179   1.6       leo 	 * Atari Falcon, XXX no specs available, this might be wrong
    180   1.6       leo 	 */
    181   1.6       leo 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    182   1.6       leo 	 229500,	/* BRgen, RTxCA, divisor 16	*/
    183   1.6       leo 	3672000,	/* RTxCA, ???			*/
    184   1.6       leo 	      0,	/* TRxCA, external		*/
    185   1.6       leo 
    186   1.6       leo 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    187   1.6       leo 	 229500,	/* BRgen, RTxCB, divisor 16	*/
    188   1.6       leo 	3672000,	/* RTxCB, ???			*/
    189   1.6       leo 	2457600		/* TRxCB, ???			*/
    190   1.6       leo };
    191  1.24       leo 
    192  1.71   tsutsui static const u_long zs_freqs_hades[] = {
    193  1.24       leo 	/*
    194  1.24       leo 	 * XXX: Channel-A unchecked!!!!!
    195  1.24       leo 	 */
    196  1.24       leo      PCLK_HD/16,	/* BRgen, PCLK,  divisor 16	*/
    197  1.24       leo 	 229500,	/* BRgen, RTxCA, divisor 16	*/
    198  1.24       leo 	3672000,	/* RTxCA, from PCLK4		*/
    199  1.24       leo 	      0,	/* TRxCA, external		*/
    200  1.24       leo 
    201  1.24       leo      PCLK_HD/16,	/* BRgen, PCLK,  divisor 16	*/
    202  1.24       leo 	 235550,	/* BRgen, RTxCB, divisor 16	*/
    203  1.24       leo 	3768800,	/* RTxCB, 3.7688MHz		*/
    204  1.24       leo 	3768800		/* TRxCB, 3.7688MHz		*/
    205  1.24       leo };
    206  1.24       leo 
    207  1.71   tsutsui static const u_long zs_freqs_generic[] = {
    208   1.6       leo 	/*
    209   1.6       leo 	 * other machines, assume only PCLK is available
    210   1.6       leo 	 */
    211   1.6       leo 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    212   1.6       leo 	      0,	/* BRgen, RTxCA, divisor 16	*/
    213   1.6       leo 	      0,	/* RTxCA, unknown		*/
    214   1.6       leo 	      0,	/* TRxCA, unknown		*/
    215   1.6       leo 
    216   1.6       leo 	PCLK/16,	/* BRgen, PCLK,  divisor 16	*/
    217   1.6       leo 	      0,	/* BRgen, RTxCB, divisor 16	*/
    218   1.6       leo 	      0,	/* RTxCB, unknown		*/
    219   1.6       leo 	      0		/* TRxCB, unknown		*/
    220   1.6       leo };
    221  1.71   tsutsui static const u_long *zs_frequencies;
    222   1.6       leo 
    223   1.1       leo /* Definition of the driver for autoconfig. */
    224  1.69   tsutsui static int	zsmatch(device_t, cfdata_t, void *);
    225  1.69   tsutsui static void	zsattach(device_t, device_t, void *);
    226  1.17   thorpej 
    227  1.69   tsutsui CFATTACH_DECL_NEW(zs, sizeof(struct zs_softc),
    228  1.38   thorpej     zsmatch, zsattach, NULL, NULL);
    229  1.17   thorpej 
    230  1.15       leo /* {b,c}devsw[] function prototypes */
    231  1.15       leo dev_type_open(zsopen);
    232  1.15       leo dev_type_close(zsclose);
    233  1.15       leo dev_type_read(zsread);
    234  1.15       leo dev_type_write(zswrite);
    235  1.15       leo dev_type_ioctl(zsioctl);
    236  1.36   gehenna dev_type_stop(zsstop);
    237  1.16       leo dev_type_tty(zstty);
    238  1.36   gehenna dev_type_poll(zspoll);
    239  1.36   gehenna 
    240  1.36   gehenna const struct cdevsw zs_cdevsw = {
    241  1.75  dholland 	.d_open = zsopen,
    242  1.75  dholland 	.d_close = zsclose,
    243  1.75  dholland 	.d_read = zsread,
    244  1.75  dholland 	.d_write = zswrite,
    245  1.75  dholland 	.d_ioctl = zsioctl,
    246  1.75  dholland 	.d_stop = zsstop,
    247  1.75  dholland 	.d_tty = zstty,
    248  1.75  dholland 	.d_poll = zspoll,
    249  1.75  dholland 	.d_mmap = nommap,
    250  1.75  dholland 	.d_kqfilter = ttykqfilter,
    251  1.75  dholland 	.d_flag = D_TTY
    252  1.36   gehenna };
    253  1.15       leo 
    254   1.1       leo /* Interrupt handlers. */
    255  1.72   tsutsui static int	zshard(void *);
    256  1.72   tsutsui static int	zssoft(void *);
    257  1.67   tsutsui static int	zsrint(struct zs_chanstate *, struct zschan *);
    258  1.67   tsutsui static int	zsxint(struct zs_chanstate *, struct zschan *);
    259  1.67   tsutsui static int	zssint(struct zs_chanstate *, struct zschan *);
    260   1.1       leo 
    261   1.1       leo /* Routines called from other code. */
    262  1.59       dsl static void	zsstart(struct tty *);
    263  1.16       leo 
    264  1.16       leo /* Routines purely local to this driver. */
    265  1.59       dsl static void	zsoverrun(int, long *, const char *);
    266  1.59       dsl static int	zsparam(struct tty *, struct termios *);
    267  1.59       dsl static int	zsbaudrate(int, int, int *, int *, int *, int *);
    268  1.59       dsl static int	zs_modem(struct zs_chanstate *, int, int);
    269  1.67   tsutsui static void	zs_loadchannelregs(struct zschan *, uint8_t *);
    270  1.59       dsl static void	zs_shutdown(struct zs_chanstate *);
    271   1.1       leo 
    272   1.4       leo static int
    273  1.69   tsutsui zsmatch(device_t parent, cfdata_t cf, void *aux)
    274   1.1       leo {
    275  1.66   tsutsui 	static int zs_matched = 0;
    276  1.31       leo 
    277  1.69   tsutsui 	if (strcmp("zs", aux) || zs_matched)
    278  1.66   tsutsui 		return 0;
    279  1.31       leo 	zs_matched = 1;
    280  1.66   tsutsui 	return 1;
    281   1.1       leo }
    282   1.1       leo 
    283   1.1       leo /*
    284   1.1       leo  * Attach a found zs.
    285   1.1       leo  */
    286   1.1       leo static void
    287  1.69   tsutsui zsattach(device_t parent, device_t self, void *aux)
    288   1.1       leo {
    289  1.68   tsutsui 	struct zs_softc *sc;
    290  1.72   tsutsui 	struct zsdevice *zs;
    291  1.72   tsutsui 	struct zschan *zc;
    292  1.66   tsutsui 	struct zs_chanstate *cs;
    293  1.72   tsutsui 	int channel;
    294   1.1       leo 
    295  1.72   tsutsui 	sc = device_private(self);
    296  1.69   tsutsui 	sc->sc_dev = self;
    297   1.1       leo 
    298  1.72   tsutsui 	printf(": serial2 on channel a and modem2 on channel b\n");
    299  1.72   tsutsui 
    300  1.72   tsutsui 	zs = (struct zsdevice *)AD_SCC;
    301  1.72   tsutsui 
    302  1.72   tsutsui 	for (channel = 0; channel < 2; channel++) {
    303  1.72   tsutsui 		cs = &sc->sc_cs_store[channel];
    304  1.72   tsutsui 		sc->sc_cs[channel] = cs;
    305  1.72   tsutsui 
    306  1.72   tsutsui 		cs->cs_unit = channel;
    307  1.72   tsutsui 		cs->cs_zc = zc =
    308  1.72   tsutsui 		    (channel == 0) ?  &zs->zs_chan_a : &zs->zs_chan_b;
    309  1.72   tsutsui 		/*
    310  1.72   tsutsui 		 * Get the command register into a known state.
    311  1.72   tsutsui 		 */
    312  1.72   tsutsui 		(void)zc->zc_csr;
    313  1.72   tsutsui 		(void)zc->zc_csr;
    314   1.1       leo 
    315  1.72   tsutsui 		/*
    316  1.72   tsutsui 		 * Do a hardware reset.
    317  1.72   tsutsui 		 */
    318  1.72   tsutsui 		if (channel == 0) {
    319  1.72   tsutsui 			ZS_WRITE(zc, 9, ZSWR9_HARD_RESET);
    320  1.72   tsutsui 			delay(50000);	/* enough ? */
    321  1.72   tsutsui 			ZS_WRITE(zc, 9, 0);
    322  1.72   tsutsui 		}
    323   1.1       leo 
    324  1.72   tsutsui 		/*
    325  1.72   tsutsui 		 * Initialize channel
    326  1.72   tsutsui 		 */
    327  1.72   tsutsui 		zs_loadchannelregs(zc, zs_init_regs);
    328  1.72   tsutsui 	}
    329   1.1       leo 
    330  1.66   tsutsui 	if (machineid & ATARI_TT) {
    331   1.5       leo 		/*
    332   1.6       leo 		 * ininitialise TT-MFP timer C: 307200Hz
    333   1.6       leo 		 * timer C and D share one control register:
    334   1.6       leo 		 *	bits 0-2 control timer D
    335   1.6       leo 		 *	bits 4-6 control timer C
    336   1.6       leo 		 */
    337   1.6       leo 		int cr = MFP2->mf_tcdcr & 7;
    338   1.6       leo 		MFP2->mf_tcdcr = cr;		/* stop timer C  */
    339   1.6       leo 		MFP2->mf_tcdr  = 1;		/* counter 1     */
    340   1.6       leo 		cr |= T_Q004 << 4;		/* divisor 4     */
    341   1.6       leo 		MFP2->mf_tcdcr = cr;		/* start timer C */
    342   1.6       leo 		/*
    343   1.5       leo 		 * enable scc related interrupts
    344   1.5       leo 		 */
    345  1.27       leo 		SCU->vme_mask |= SCU_SCC;
    346   1.6       leo 
    347   1.6       leo 		zs_frequencies = zs_freqs_tt;
    348   1.6       leo 	} else if (machineid & ATARI_FALCON) {
    349   1.6       leo 		zs_frequencies = zs_freqs_falcon;
    350  1.24       leo 	} else if (machineid & ATARI_HADES) {
    351  1.24       leo 		zs_frequencies = zs_freqs_hades;
    352   1.6       leo 	} else {
    353   1.6       leo 		zs_frequencies = zs_freqs_generic;
    354   1.5       leo 	}
    355   1.1       leo 
    356  1.72   tsutsui 	if (intr_establish(36, USER_VEC, 0, (hw_ifun_t)zshard, sc) == NULL)
    357  1.72   tsutsui 		aprint_error_dev(self,
    358  1.72   tsutsui 		    "Can't establish interrupt (Rx chan B)\n");
    359  1.72   tsutsui 	if (intr_establish(32, USER_VEC, 0, (hw_ifun_t)zshard, sc) == NULL)
    360  1.72   tsutsui 		aprint_error_dev(self,
    361  1.72   tsutsui 		    "Can't establish interrupt (Tx empty chan B)\n");
    362  1.72   tsutsui 	if (intr_establish(34, USER_VEC, 0, (hw_ifun_t)zshard, sc) == NULL)
    363  1.72   tsutsui 		aprint_error_dev(self,
    364  1.72   tsutsui 		    "Can't establish interrupt (Ext./Status chan B)\n");
    365  1.72   tsutsui 	if (intr_establish(38, USER_VEC, 0, (hw_ifun_t)zshard, sc) == NULL)
    366  1.72   tsutsui 		aprint_error_dev(self,
    367  1.72   tsutsui 		    "Can't establish interrupt (Special Rx cond. chan B)\n");
    368  1.72   tsutsui 	if (intr_establish(44, USER_VEC, 0, (hw_ifun_t)zshard, sc) == NULL)
    369  1.72   tsutsui 		aprint_error_dev(self,
    370  1.72   tsutsui 		    "Can't establish interrupt (Rx chan A)\n");
    371  1.72   tsutsui 	if (intr_establish(40, USER_VEC, 0, (hw_ifun_t)zshard, sc) == NULL)
    372  1.72   tsutsui 		aprint_error_dev(self,
    373  1.72   tsutsui 		    "Can't establish interrupt (Tx empty chan A)\n");
    374  1.72   tsutsui 	if (intr_establish(42, USER_VEC, 0, (hw_ifun_t)zshard, sc) == NULL)
    375  1.72   tsutsui 		aprint_error_dev(self,
    376  1.72   tsutsui 		    "Can't establish interrupt (Ext./Status chan A)\n");
    377  1.72   tsutsui 	if (intr_establish(46, USER_VEC, 0, (hw_ifun_t)zshard, sc) == NULL)
    378  1.72   tsutsui 		aprint_error_dev(self,
    379  1.72   tsutsui 		    "Can't establish interrupt (Special Rx cond. chan A)\n");
    380  1.58      tjam 
    381  1.72   tsutsui 	sc->sc_sicookie = softint_establish(SOFTINT_SERIAL,
    382  1.72   tsutsui 	    (void (*)(void *))zssoft, sc);
    383   1.1       leo }
    384   1.1       leo 
    385   1.1       leo /*
    386   1.1       leo  * Open a zs serial port.
    387   1.1       leo  */
    388   1.1       leo int
    389  1.60       dsl zsopen(dev_t dev, int flags, int mode, struct lwp *l)
    390   1.1       leo {
    391  1.66   tsutsui 	struct tty *tp;
    392  1.66   tsutsui 	struct zs_chanstate *cs;
    393  1.68   tsutsui 	struct zs_softc *sc;
    394  1.66   tsutsui 	int unit = ZS_UNIT(dev);
    395  1.66   tsutsui 	int zs = unit >> 1;
    396  1.66   tsutsui 	int error, s;
    397   1.1       leo 
    398  1.68   tsutsui 	sc = device_lookup_private(&zs_cd, zs);
    399  1.68   tsutsui 	if (sc == NULL)
    400  1.66   tsutsui 		return ENXIO;
    401  1.72   tsutsui 	cs = sc->sc_cs[unit & 1];
    402  1.10       jtc 
    403  1.10       jtc 	/*
    404  1.10       jtc 	 * When port A (ser02) is selected on the TT, make sure
    405  1.10       jtc 	 * the port is enabled.
    406  1.10       jtc 	 */
    407  1.66   tsutsui 	if ((machineid & ATARI_TT) && !(unit & 1))
    408  1.26       leo 		ym2149_ser2(1);
    409  1.13       leo 
    410  1.13       leo 	if (cs->cs_rbuf == NULL) {
    411  1.13       leo 		cs->cs_rbuf = malloc(ZLRB_RING_SIZE * sizeof(int), M_DEVBUF,
    412  1.66   tsutsui 		    M_WAITOK);
    413  1.10       jtc 	}
    414  1.10       jtc 
    415   1.1       leo 	tp = cs->cs_ttyp;
    416  1.70   tsutsui 	if (tp == NULL) {
    417  1.73     rmind 		cs->cs_ttyp = tp = tty_alloc();
    418  1.21       leo 		tty_attach(tp);
    419  1.21       leo 		tp->t_dev   = dev;
    420  1.21       leo 		tp->t_oproc = zsstart;
    421  1.21       leo 		tp->t_param = zsparam;
    422   1.1       leo 	}
    423   1.1       leo 
    424  1.50      elad 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    425  1.66   tsutsui 		return EBUSY;
    426  1.29       leo 
    427   1.1       leo 	s  = spltty();
    428  1.29       leo 
    429  1.29       leo 	/*
    430  1.29       leo 	 * Do the following iff this is a first open.
    431  1.29       leo 	 */
    432  1.66   tsutsui 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
    433  1.70   tsutsui 		if (tp->t_ispeed == 0) {
    434   1.1       leo 			tp->t_iflag = TTYDEF_IFLAG;
    435   1.1       leo 			tp->t_oflag = TTYDEF_OFLAG;
    436   1.1       leo 			tp->t_cflag = TTYDEF_CFLAG;
    437   1.1       leo 			tp->t_lflag = TTYDEF_LFLAG;
    438   1.1       leo 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    439   1.1       leo 		}
    440  1.29       leo 		ttychars(tp);
    441  1.29       leo 		ttsetwater(tp);
    442  1.29       leo 
    443   1.1       leo 		(void)zsparam(tp, &tp->t_termios);
    444  1.29       leo 
    445  1.29       leo 		/*
    446  1.29       leo 		 * Turn on DTR.  We must always do this, even if carrier is not
    447  1.29       leo 		 * present, because otherwise we'd have to use TIOCSDTR
    448  1.29       leo 		 * immediately after setting CLOCAL, which applications do not
    449  1.29       leo 		 * expect.  We always assert DTR while the device is open
    450  1.29       leo 		 * unless explicitly requested to deassert it.
    451  1.29       leo 		 */
    452   1.1       leo 		zs_modem(cs, ZSWR5_RTS|ZSWR5_DTR, DMSET);
    453   1.8       leo 		/* May never get a status intr. if DCD already on. -gwr */
    454  1.66   tsutsui 		if (((cs->cs_rr0 = cs->cs_zc->zc_csr) & ZSRR0_DCD) != 0)
    455   1.8       leo 			tp->t_state |= TS_CARR_ON;
    456  1.70   tsutsui 		if (cs->cs_softcar)
    457   1.1       leo 			tp->t_state |= TS_CARR_ON;
    458   1.1       leo 	}
    459  1.29       leo 
    460   1.1       leo 	splx(s);
    461  1.29       leo 
    462  1.29       leo 	error = ttyopen(tp, ZS_DIALOUT(dev), (flags & O_NONBLOCK));
    463  1.29       leo 	if (error)
    464  1.29       leo 		goto bad;
    465  1.29       leo 
    466  1.32       eeh 	error = tp->t_linesw->l_open(dev, tp);
    467  1.66   tsutsui 	if (error)
    468  1.29       leo 		goto bad;
    469  1.66   tsutsui 	return 0;
    470  1.29       leo 
    471  1.29       leo bad:
    472  1.66   tsutsui 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
    473  1.29       leo 		/*
    474  1.29       leo 		 * We failed to open the device, and nobody else had it opened.
    475  1.29       leo 		 * Clean up the state as appropriate.
    476  1.29       leo 		 */
    477  1.29       leo 		zs_shutdown(cs);
    478  1.29       leo 	}
    479  1.66   tsutsui 	return error;
    480   1.1       leo }
    481   1.1       leo 
    482   1.1       leo /*
    483   1.1       leo  * Close a zs serial port.
    484   1.1       leo  */
    485   1.1       leo int
    486  1.60       dsl zsclose(dev_t dev, int flags, int mode, struct lwp *l)
    487   1.1       leo {
    488  1.66   tsutsui 	struct zs_chanstate *cs;
    489  1.66   tsutsui 	struct tty *tp;
    490  1.68   tsutsui 	struct zs_softc *sc;
    491  1.66   tsutsui 	int unit = ZS_UNIT(dev);
    492   1.1       leo 
    493  1.68   tsutsui 	sc = device_lookup_private(&zs_cd, unit >> 1);
    494  1.72   tsutsui 	cs = sc->sc_cs[unit & 1];
    495   1.1       leo 	tp = cs->cs_ttyp;
    496  1.29       leo 
    497  1.32       eeh 	tp->t_linesw->l_close(tp, flags);
    498   1.1       leo 	ttyclose(tp);
    499   1.1       leo 
    500  1.66   tsutsui 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
    501  1.29       leo 		/*
    502  1.29       leo 		 * Although we got a last close, the device may still be in
    503  1.29       leo 		 * use; e.g. if this was the dialout node, and there are still
    504  1.29       leo 		 * processes waiting for carrier on the non-dialout node.
    505  1.29       leo 		 */
    506  1.29       leo 		zs_shutdown(cs);
    507  1.29       leo 	}
    508  1.66   tsutsui 	return 0;
    509   1.1       leo }
    510   1.1       leo 
    511   1.1       leo /*
    512   1.1       leo  * Read/write zs serial port.
    513   1.1       leo  */
    514   1.1       leo int
    515  1.60       dsl zsread(dev_t dev, struct uio *uio, int flags)
    516   1.1       leo {
    517  1.66   tsutsui 	struct zs_chanstate *cs;
    518  1.68   tsutsui 	struct zs_softc *sc;
    519  1.66   tsutsui 	struct tty *tp;
    520  1.66   tsutsui 	int unit;
    521   1.4       leo 
    522   1.4       leo 	unit = ZS_UNIT(dev);
    523  1.68   tsutsui 	sc   = device_lookup_private(&zs_cd, unit >> 1);
    524  1.72   tsutsui 	cs   = sc->sc_cs[unit & 1];
    525   1.4       leo 	tp   = cs->cs_ttyp;
    526   1.1       leo 
    527  1.66   tsutsui 	return (*tp->t_linesw->l_read)(tp, uio, flags);
    528   1.1       leo }
    529   1.1       leo 
    530   1.4       leo int
    531  1.60       dsl zswrite(dev_t dev, struct uio *uio, int flags)
    532   1.1       leo {
    533  1.66   tsutsui 	struct zs_chanstate *cs;
    534  1.68   tsutsui 	struct zs_softc *sc;
    535  1.66   tsutsui 	struct tty *tp;
    536  1.66   tsutsui 	int unit;
    537   1.4       leo 
    538   1.4       leo 	unit = ZS_UNIT(dev);
    539  1.68   tsutsui 	sc   = device_lookup_private(&zs_cd, unit >> 1);
    540  1.72   tsutsui 	cs   = sc->sc_cs[unit & 1];
    541   1.4       leo 	tp   = cs->cs_ttyp;
    542   1.1       leo 
    543  1.66   tsutsui 	return (*tp->t_linesw->l_write)(tp, uio, flags);
    544  1.34       scw }
    545  1.34       scw 
    546  1.34       scw int
    547  1.60       dsl zspoll(dev_t dev, int events, struct lwp *l)
    548  1.34       scw {
    549  1.66   tsutsui 	struct zs_chanstate *cs;
    550  1.68   tsutsui 	struct zs_softc *sc;
    551  1.66   tsutsui 	struct tty *tp;
    552  1.66   tsutsui 	int unit;
    553  1.34       scw 
    554  1.34       scw 	unit = ZS_UNIT(dev);
    555  1.68   tsutsui 	sc   = device_lookup_private(&zs_cd, unit >> 1);
    556  1.72   tsutsui 	cs   = sc->sc_cs[unit & 1];
    557  1.34       scw 	tp   = cs->cs_ttyp;
    558  1.34       scw 
    559  1.66   tsutsui 	return (*tp->t_linesw->l_poll)(tp, events, l);
    560   1.4       leo }
    561   1.4       leo 
    562   1.4       leo struct tty *
    563  1.60       dsl zstty(dev_t dev)
    564   1.4       leo {
    565  1.66   tsutsui 	struct zs_chanstate *cs;
    566  1.68   tsutsui 	struct zs_softc *sc;
    567  1.66   tsutsui 	int unit;
    568   1.4       leo 
    569   1.4       leo 	unit = ZS_UNIT(dev);
    570  1.68   tsutsui 	sc   = device_lookup_private(&zs_cd, unit >> 1);
    571  1.72   tsutsui 	cs   = sc->sc_cs[unit & 1];
    572  1.66   tsutsui 	return cs->cs_ttyp;
    573   1.1       leo }
    574   1.1       leo 
    575   1.1       leo /*
    576   1.1       leo  * ZS hardware interrupt.  Scan all ZS channels.  NB: we know here that
    577   1.1       leo  * channels are kept in (A,B) pairs.
    578   1.1       leo  *
    579   1.1       leo  * Do just a little, then get out; set a software interrupt if more
    580   1.1       leo  * work is needed.
    581   1.1       leo  *
    582   1.1       leo  * We deliberately ignore the vectoring Zilog gives us, and match up
    583   1.1       leo  * only the number of `reset interrupt under service' operations, not
    584   1.1       leo  * the order.
    585   1.1       leo  */
    586   1.8       leo 
    587   1.1       leo int
    588  1.72   tsutsui zshard(void *arg)
    589   1.1       leo {
    590  1.72   tsutsui 	struct zs_softc *sc;
    591  1.72   tsutsui 	struct zs_chanstate *cs0, *cs1;
    592  1.67   tsutsui 	struct zschan *zc;
    593  1.72   tsutsui 	int intflags, v, i;
    594  1.72   tsutsui 	uint8_t rr3;
    595  1.72   tsutsui 
    596  1.72   tsutsui 	sc = arg;
    597  1.72   tsutsui 	intflags = 0;
    598  1.72   tsutsui 	cs0 = sc->sc_cs[0];
    599  1.72   tsutsui 	cs1 = sc->sc_cs[1];
    600   1.1       leo 
    601   1.8       leo 	do {
    602  1.66   tsutsui 		intflags &= ~4;
    603  1.72   tsutsui 		rr3 = ZS_READ(cs0->cs_zc, 3);
    604  1.72   tsutsui 		if (rr3 & (ZSRR3_IP_A_RX | ZSRR3_IP_A_TX | ZSRR3_IP_A_STAT)) {
    605  1.72   tsutsui 			intflags |= 4 | 2;
    606  1.72   tsutsui 			zc = cs0->cs_zc;
    607  1.72   tsutsui 			i  = cs0->cs_rbput;
    608  1.72   tsutsui 			if ((rr3 & ZSRR3_IP_A_RX) != 0 &&
    609  1.72   tsutsui 			    (v = zsrint(cs0, zc)) != 0) {
    610  1.72   tsutsui 				cs0->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    611  1.72   tsutsui 				intflags |= 1;
    612  1.72   tsutsui 			}
    613  1.72   tsutsui 			if ((rr3 & ZSRR3_IP_A_TX) != 0 &&
    614  1.72   tsutsui 			    (v = zsxint(cs0, zc)) != 0) {
    615  1.72   tsutsui 				cs0->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    616  1.72   tsutsui 				intflags |= 1;
    617  1.72   tsutsui 			}
    618  1.72   tsutsui 			if ((rr3 & ZSRR3_IP_A_STAT) != 0 &&
    619  1.72   tsutsui 			    (v = zssint(cs0, zc)) != 0) {
    620  1.72   tsutsui 				cs0->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    621  1.72   tsutsui 				intflags |= 1;
    622  1.72   tsutsui 			}
    623  1.72   tsutsui 			cs0->cs_rbput = i;
    624  1.72   tsutsui 		}
    625  1.72   tsutsui 		if (rr3 & (ZSRR3_IP_B_RX | ZSRR3_IP_B_TX | ZSRR3_IP_B_STAT)) {
    626  1.72   tsutsui 			intflags |= 4 | 2;
    627  1.72   tsutsui 			zc = cs1->cs_zc;
    628  1.72   tsutsui 			i  = cs1->cs_rbput;
    629  1.72   tsutsui 			if ((rr3 & ZSRR3_IP_B_RX) != 0 &&
    630  1.72   tsutsui 			    (v = zsrint(cs1, zc)) != 0) {
    631  1.72   tsutsui 				cs1->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    632  1.72   tsutsui 				intflags |= 1;
    633  1.72   tsutsui 			}
    634  1.72   tsutsui 			if ((rr3 & ZSRR3_IP_B_TX) != 0 &&
    635  1.72   tsutsui 			    (v = zsxint(cs1, zc)) != 0) {
    636  1.72   tsutsui 				cs1->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    637  1.72   tsutsui 				intflags |= 1;
    638   1.1       leo 			}
    639  1.72   tsutsui 			if ((rr3 & ZSRR3_IP_B_STAT) != 0 &&
    640  1.72   tsutsui 			    (v = zssint(cs1, zc)) != 0) {
    641  1.72   tsutsui 				cs1->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
    642  1.72   tsutsui 				intflags |= 1;
    643   1.1       leo 			}
    644  1.72   tsutsui 			cs1->cs_rbput = i;
    645   1.1       leo 		}
    646  1.66   tsutsui 	} while (intflags & 4);
    647   1.1       leo 
    648  1.66   tsutsui 	if (intflags & 1)
    649  1.72   tsutsui 		softint_schedule(sc->sc_sicookie);
    650  1.58      tjam 
    651  1.66   tsutsui 	return intflags & 2;
    652   1.1       leo }
    653   1.1       leo 
    654   1.1       leo static int
    655  1.67   tsutsui zsrint(struct zs_chanstate *cs, struct zschan *zc)
    656   1.1       leo {
    657  1.66   tsutsui 	int c;
    658   1.1       leo 
    659   1.8       leo 	/*
    660   1.8       leo 	 * First read the status, because read of the received char
    661   1.8       leo 	 * destroy the status of this char.
    662   1.8       leo 	 */
    663   1.8       leo 	c = ZS_READ(zc, 1);
    664   1.8       leo 	c |= (zc->zc_data << 8);
    665   1.1       leo 
    666   1.1       leo 	/* clear receive error & interrupt condition */
    667   1.1       leo 	zc->zc_csr = ZSWR0_RESET_ERRORS;
    668   1.1       leo 	zc->zc_csr = ZSWR0_CLR_INTR;
    669   1.1       leo 
    670  1.66   tsutsui 	return ZRING_MAKE(ZRING_RINT, c);
    671   1.1       leo }
    672   1.1       leo 
    673   1.1       leo static int
    674  1.67   tsutsui zsxint(struct zs_chanstate *cs, struct zschan *zc)
    675   1.1       leo {
    676  1.66   tsutsui 	int i = cs->cs_tbc;
    677   1.1       leo 
    678  1.66   tsutsui 	if (i == 0) {
    679   1.1       leo 		zc->zc_csr = ZSWR0_RESET_TXINT;
    680   1.1       leo 		zc->zc_csr = ZSWR0_CLR_INTR;
    681  1.66   tsutsui 		return ZRING_MAKE(ZRING_XINT, 0);
    682   1.1       leo 	}
    683   1.1       leo 	cs->cs_tbc = i - 1;
    684   1.1       leo 	zc->zc_data = *cs->cs_tba++;
    685   1.1       leo 	zc->zc_csr = ZSWR0_CLR_INTR;
    686  1.66   tsutsui 	return 0;
    687   1.1       leo }
    688   1.1       leo 
    689   1.1       leo static int
    690  1.67   tsutsui zssint(struct zs_chanstate *cs, struct zschan *zc)
    691   1.1       leo {
    692  1.66   tsutsui 	int rr0;
    693   1.1       leo 
    694   1.1       leo 	rr0 = zc->zc_csr;
    695   1.1       leo 	zc->zc_csr = ZSWR0_RESET_STATUS;
    696   1.1       leo 	zc->zc_csr = ZSWR0_CLR_INTR;
    697   1.1       leo 	/*
    698   1.1       leo 	 * The chip's hardware flow control is, as noted in zsreg.h,
    699   1.1       leo 	 * busted---if the DCD line goes low the chip shuts off the
    700   1.1       leo 	 * receiver (!).  If we want hardware CTS flow control but do
    701   1.1       leo 	 * not have it, and carrier is now on, turn HFC on; if we have
    702   1.1       leo 	 * HFC now but carrier has gone low, turn it off.
    703   1.1       leo 	 */
    704  1.66   tsutsui 	if (rr0 & ZSRR0_DCD) {
    705  1.66   tsutsui 		if (cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
    706   1.1       leo 		    (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
    707   1.1       leo 			cs->cs_creg[3] |= ZSWR3_HFC;
    708   1.1       leo 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    709   1.1       leo 		}
    710  1.66   tsutsui 	} else {
    711   1.1       leo 		if (cs->cs_creg[3] & ZSWR3_HFC) {
    712   1.1       leo 			cs->cs_creg[3] &= ~ZSWR3_HFC;
    713   1.1       leo 			ZS_WRITE(zc, 3, cs->cs_creg[3]);
    714   1.1       leo 		}
    715   1.1       leo 	}
    716  1.66   tsutsui 	return ZRING_MAKE(ZRING_SINT, rr0);
    717   1.1       leo }
    718   1.1       leo 
    719   1.1       leo /*
    720   1.1       leo  * Print out a ring or fifo overrun error message.
    721   1.1       leo  */
    722   1.1       leo static void
    723  1.60       dsl zsoverrun(int unit, long *ptime, const char *what)
    724   1.1       leo {
    725  1.55     joerg 	time_t cur_sec = time_second;
    726   1.1       leo 
    727  1.70   tsutsui 	if (*ptime != cur_sec) {
    728  1.55     joerg 		*ptime = cur_sec;
    729   1.1       leo 		log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
    730   1.1       leo 		    (unit & 1) + 'a', what);
    731   1.1       leo 	}
    732   1.1       leo }
    733   1.1       leo 
    734   1.1       leo /*
    735   1.1       leo  * ZS software interrupt.  Scan all channels for deferred interrupts.
    736   1.1       leo  */
    737   1.1       leo int
    738  1.72   tsutsui zssoft(void *arg)
    739   1.1       leo {
    740  1.72   tsutsui 	struct zs_softc *sc;
    741  1.66   tsutsui 	struct zs_chanstate *cs;
    742  1.67   tsutsui 	struct zschan *zc;
    743  1.66   tsutsui 	struct linesw *line;
    744  1.66   tsutsui 	struct tty *tp;
    745  1.72   tsutsui 	int chan, get, n, c, cc, s;
    746  1.66   tsutsui 	int retval = 0;
    747  1.66   tsutsui 
    748  1.72   tsutsui 	sc = arg;
    749  1.66   tsutsui 	s = spltty();
    750  1.72   tsutsui 	for (chan = 0; chan < 2; chan++) {
    751  1.72   tsutsui 		cs = sc->sc_cs[chan];
    752  1.66   tsutsui 		get = cs->cs_rbget;
    753   1.1       leo again:
    754  1.66   tsutsui 		n = cs->cs_rbput;	/* atomic			*/
    755  1.66   tsutsui 		if (get == n)		/* nothing more on this line	*/
    756  1.66   tsutsui 			continue;
    757  1.66   tsutsui 		retval = 1;
    758  1.66   tsutsui 		zc     = cs->cs_zc;
    759  1.66   tsutsui 		tp     = cs->cs_ttyp;
    760  1.66   tsutsui 		line   = tp->t_linesw;
    761  1.66   tsutsui 		/*
    762  1.66   tsutsui 		 * Compute the number of interrupts in the receive ring.
    763  1.66   tsutsui 		 * If the count is overlarge, we lost some events, and
    764  1.66   tsutsui 		 * must advance to the first valid one.  It may get
    765  1.66   tsutsui 		 * overwritten if more data are arriving, but this is
    766  1.66   tsutsui 		 * too expensive to check and gains nothing (we already
    767  1.66   tsutsui 		 * lost out; all we can do at this point is trade one
    768  1.66   tsutsui 		 * kind of loss for another).
    769  1.66   tsutsui 		 */
    770  1.66   tsutsui 		n -= get;
    771  1.66   tsutsui 		if (n > ZLRB_RING_SIZE) {
    772  1.72   tsutsui 			zsoverrun(chan, &cs->cs_rotime, "ring");
    773  1.66   tsutsui 			get += n - ZLRB_RING_SIZE;
    774  1.66   tsutsui 			n    = ZLRB_RING_SIZE;
    775  1.66   tsutsui 		}
    776  1.66   tsutsui 		while (--n >= 0) {
    777  1.66   tsutsui 			/* race to keep ahead of incoming interrupts */
    778  1.66   tsutsui 			c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
    779  1.66   tsutsui 			switch (ZRING_TYPE(c)) {
    780  1.66   tsutsui 
    781  1.66   tsutsui 			case ZRING_RINT:
    782  1.66   tsutsui 				c = ZRING_VALUE(c);
    783  1.66   tsutsui 				if ((c & ZSRR1_DO) != 0)
    784  1.72   tsutsui 					zsoverrun(chan, &cs->cs_fotime, "fifo");
    785  1.66   tsutsui 				cc = c >> 8;
    786  1.66   tsutsui 				if ((c & ZSRR1_FE) != 0)
    787  1.66   tsutsui 					cc |= TTY_FE;
    788  1.66   tsutsui 				if ((c & ZSRR1_PE) != 0)
    789  1.66   tsutsui 					cc |= TTY_PE;
    790  1.66   tsutsui 				line->l_rint(cc, tp);
    791  1.66   tsutsui 				break;
    792  1.66   tsutsui 
    793  1.66   tsutsui 			case ZRING_XINT:
    794  1.66   tsutsui 				/*
    795  1.66   tsutsui 				 * Transmit done: change registers and resume,
    796  1.66   tsutsui 				 * or clear BUSY.
    797  1.66   tsutsui 				 */
    798  1.66   tsutsui 				if (cs->cs_heldchange) {
    799  1.66   tsutsui 					int sps;
    800  1.66   tsutsui 
    801  1.66   tsutsui 					sps = splzs();
    802  1.66   tsutsui 					c = zc->zc_csr;
    803  1.66   tsutsui 					if ((c & ZSRR0_DCD) == 0)
    804  1.66   tsutsui 						cs->cs_preg[3] &= ~ZSWR3_HFC;
    805  1.66   tsutsui 					memcpy((void *)cs->cs_creg,
    806  1.66   tsutsui 					    (void *)cs->cs_preg, 16);
    807  1.66   tsutsui 					zs_loadchannelregs(zc, cs->cs_creg);
    808  1.66   tsutsui 					splx(sps);
    809  1.66   tsutsui 					cs->cs_heldchange = 0;
    810  1.66   tsutsui 					if (cs->cs_heldtbc &&
    811  1.66   tsutsui 					    (tp->t_state & TS_TTSTOP) == 0) {
    812  1.66   tsutsui 						cs->cs_tbc = cs->cs_heldtbc - 1;
    813  1.66   tsutsui 						zc->zc_data = *cs->cs_tba++;
    814  1.66   tsutsui 						goto again;
    815  1.66   tsutsui 					}
    816  1.66   tsutsui 				}
    817  1.66   tsutsui 				tp->t_state &= ~TS_BUSY;
    818  1.66   tsutsui 				if ((tp->t_state & TS_FLUSH) != 0)
    819  1.66   tsutsui 					tp->t_state &= ~TS_FLUSH;
    820  1.66   tsutsui 				else
    821  1.66   tsutsui 					ndflush(&tp->t_outq,
    822  1.66   tsutsui 					    cs->cs_tba - tp->t_outq.c_cf);
    823  1.66   tsutsui 				line->l_start(tp);
    824  1.66   tsutsui 				break;
    825   1.1       leo 
    826  1.66   tsutsui 			case ZRING_SINT:
    827  1.66   tsutsui 				/*
    828  1.66   tsutsui 				 * Status line change.  HFC bit is run in
    829  1.66   tsutsui 				 * hardware interrupt, to avoid locking
    830  1.66   tsutsui 				 * at splzs here.
    831  1.66   tsutsui 				 */
    832  1.66   tsutsui 				c = ZRING_VALUE(c);
    833  1.66   tsutsui 				if (((c ^ cs->cs_rr0) & ZSRR0_DCD) != 0) {
    834  1.66   tsutsui 					cc = (c & ZSRR0_DCD) != 0;
    835  1.66   tsutsui 					if (line->l_modem(tp, cc) == 0)
    836  1.66   tsutsui 						zs_modem(cs,
    837  1.66   tsutsui 						    ZSWR5_RTS | ZSWR5_DTR,
    838  1.66   tsutsui 						    cc ? DMBIS : DMBIC);
    839   1.1       leo 				}
    840  1.66   tsutsui 				cs->cs_rr0 = c;
    841  1.66   tsutsui 				break;
    842   1.1       leo 
    843  1.66   tsutsui 			default:
    844  1.66   tsutsui 				log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
    845  1.72   tsutsui 				    chan >> 1, (chan & 1) + 'a', c);
    846  1.66   tsutsui 				break;
    847   1.1       leo 			}
    848   1.1       leo 		}
    849  1.66   tsutsui 		cs->cs_rbget = get;
    850  1.66   tsutsui 		goto again;
    851   1.1       leo 	}
    852  1.66   tsutsui 	splx(s);
    853  1.66   tsutsui 	return retval;
    854   1.1       leo }
    855   1.1       leo 
    856   1.1       leo int
    857  1.60       dsl zsioctl(dev_t dev, u_long cmd, void * data, int flag, struct lwp *l)
    858   1.1       leo {
    859  1.66   tsutsui 	int unit = ZS_UNIT(dev);
    860  1.68   tsutsui 	struct zs_softc *sc = device_lookup_private(&zs_cd, unit >> 1);
    861  1.72   tsutsui 	struct zs_chanstate *cs = sc->sc_cs[unit & 1];
    862  1.72   tsutsui 	struct tty *tp = cs->cs_ttyp;
    863  1.66   tsutsui 	int error, s;
    864   1.1       leo 
    865  1.45  christos 	error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l);
    866  1.66   tsutsui 	if (error != EPASSTHROUGH)
    867  1.66   tsutsui 		return error;
    868  1.35    atatat 
    869  1.45  christos 	error = ttioctl(tp, cmd, data, flag, l);
    870  1.66   tsutsui 	if (error !=EPASSTHROUGH)
    871  1.66   tsutsui 		return error;
    872   1.1       leo 
    873   1.1       leo 	switch (cmd) {
    874   1.1       leo 	case TIOCSBRK:
    875   1.1       leo 		s = splzs();
    876   1.1       leo 		cs->cs_preg[5] |= ZSWR5_BREAK;
    877   1.1       leo 		cs->cs_creg[5] |= ZSWR5_BREAK;
    878   1.1       leo 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
    879   1.1       leo 		splx(s);
    880   1.1       leo 		break;
    881   1.1       leo 	case TIOCCBRK:
    882   1.1       leo 		s = splzs();
    883   1.1       leo 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
    884   1.1       leo 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
    885   1.1       leo 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
    886   1.1       leo 		splx(s);
    887   1.1       leo 		break;
    888   1.1       leo 	case TIOCGFLAGS: {
    889   1.1       leo 		int bits = 0;
    890   1.1       leo 
    891  1.66   tsutsui 		if (cs->cs_softcar)
    892   1.1       leo 			bits |= TIOCFLAG_SOFTCAR;
    893  1.66   tsutsui 		if ((cs->cs_creg[15] & ZSWR15_DCD_IE) != 0)
    894   1.1       leo 			bits |= TIOCFLAG_CLOCAL;
    895  1.66   tsutsui 		if ((cs->cs_creg[3] & ZSWR3_HFC) != 0)
    896   1.1       leo 			bits |= TIOCFLAG_CRTSCTS;
    897   1.1       leo 		*(int *)data = bits;
    898   1.1       leo 		break;
    899   1.1       leo 	}
    900   1.1       leo 	case TIOCSFLAGS: {
    901  1.15       leo 		int userbits = 0;
    902   1.1       leo 
    903  1.51      elad 		error = kauth_authorize_device_tty(l->l_cred,
    904  1.51      elad 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    905  1.66   tsutsui 		if (error != 0)
    906  1.66   tsutsui 			return EPERM;
    907   1.1       leo 
    908   1.1       leo 		userbits = *(int *)data;
    909   1.1       leo 
    910   1.1       leo 		/*
    911   1.1       leo 		 * can have `local' or `softcar', and `rtscts' or `mdmbuf'
    912   1.1       leo 		 # defaulting to software flow control.
    913   1.1       leo 		 */
    914  1.66   tsutsui 		if ((userbits & TIOCFLAG_SOFTCAR) != 0 &&
    915  1.66   tsutsui 		    (userbits & TIOCFLAG_CLOCAL) != 0)
    916  1.66   tsutsui 			return EINVAL;
    917  1.66   tsutsui 		if ((userbits & TIOCFLAG_MDMBUF) != 0)
    918  1.66   tsutsui 			/* don't support this (yet?) */
    919  1.66   tsutsui 			return ENODEV;
    920   1.1       leo 
    921   1.1       leo 		s = splzs();
    922  1.66   tsutsui 		if ((userbits & TIOCFLAG_SOFTCAR) != 0) {
    923   1.1       leo 			cs->cs_softcar = 1;	/* turn on softcar */
    924   1.1       leo 			cs->cs_preg[15] &= ~ZSWR15_DCD_IE; /* turn off dcd */
    925   1.1       leo 			cs->cs_creg[15] &= ~ZSWR15_DCD_IE;
    926   1.1       leo 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    927  1.66   tsutsui 		} else if ((userbits & TIOCFLAG_CLOCAL) != 0) {
    928   1.1       leo 			cs->cs_softcar = 0; 	/* turn off softcar */
    929   1.1       leo 			cs->cs_preg[15] |= ZSWR15_DCD_IE; /* turn on dcd */
    930   1.1       leo 			cs->cs_creg[15] |= ZSWR15_DCD_IE;
    931   1.1       leo 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    932   1.1       leo 			tp->t_termios.c_cflag |= CLOCAL;
    933   1.1       leo 		}
    934  1.66   tsutsui 		if ((userbits & TIOCFLAG_CRTSCTS) != 0) {
    935   1.1       leo 			cs->cs_preg[15] |= ZSWR15_CTS_IE;
    936   1.1       leo 			cs->cs_creg[15] |= ZSWR15_CTS_IE;
    937   1.1       leo 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    938   1.1       leo 			cs->cs_preg[3] |= ZSWR3_HFC;
    939   1.1       leo 			cs->cs_creg[3] |= ZSWR3_HFC;
    940   1.1       leo 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
    941   1.1       leo 			tp->t_termios.c_cflag |= CRTSCTS;
    942  1.66   tsutsui 		} else {
    943   1.1       leo 			/* no mdmbuf, so we must want software flow control */
    944   1.1       leo 			cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
    945   1.1       leo 			cs->cs_creg[15] &= ~ZSWR15_CTS_IE;
    946   1.1       leo 			ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
    947   1.1       leo 			cs->cs_preg[3] &= ~ZSWR3_HFC;
    948   1.1       leo 			cs->cs_creg[3] &= ~ZSWR3_HFC;
    949   1.1       leo 			ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
    950   1.1       leo 			tp->t_termios.c_cflag &= ~CRTSCTS;
    951   1.1       leo 		}
    952   1.1       leo 		splx(s);
    953   1.1       leo 		break;
    954   1.1       leo 	}
    955   1.1       leo 	case TIOCSDTR:
    956   1.1       leo 		zs_modem(cs, ZSWR5_DTR, DMBIS);
    957   1.1       leo 		break;
    958   1.1       leo 	case TIOCCDTR:
    959   1.1       leo 		zs_modem(cs, ZSWR5_DTR, DMBIC);
    960   1.1       leo 		break;
    961   1.1       leo 	case TIOCMGET:
    962   1.1       leo 		zs_modem(cs, 0, DMGET);
    963   1.1       leo 		break;
    964   1.1       leo 	case TIOCMSET:
    965   1.1       leo 	case TIOCMBIS:
    966   1.1       leo 	case TIOCMBIC:
    967   1.1       leo 	default:
    968  1.66   tsutsui 		return EPASSTHROUGH;
    969   1.1       leo 	}
    970  1.66   tsutsui 	return 0;
    971   1.1       leo }
    972   1.1       leo 
    973   1.1       leo /*
    974   1.1       leo  * Start or restart transmission.
    975   1.1       leo  */
    976   1.1       leo static void
    977  1.66   tsutsui zsstart(struct tty *tp)
    978   1.1       leo {
    979  1.66   tsutsui 	struct zs_chanstate *cs;
    980  1.66   tsutsui 	int s, nch;
    981  1.66   tsutsui 	int unit = ZS_UNIT(tp->t_dev);
    982  1.68   tsutsui 	struct zs_softc *sc = device_lookup_private(&zs_cd, unit >> 1);
    983   1.1       leo 
    984  1.72   tsutsui 	cs = sc->sc_cs[unit & 1];
    985   1.1       leo 	s  = spltty();
    986   1.1       leo 
    987   1.1       leo 	/*
    988   1.1       leo 	 * If currently active or delaying, no need to do anything.
    989   1.1       leo 	 */
    990  1.66   tsutsui 	if ((tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) != 0)
    991   1.1       leo 		goto out;
    992   1.1       leo 
    993   1.1       leo 	/*
    994   1.1       leo 	 * If there are sleepers, and output has drained below low
    995   1.1       leo 	 * water mark, awaken.
    996   1.1       leo 	 */
    997  1.54        ad 	ttypull(tp);
    998   1.1       leo 
    999   1.1       leo 	nch = ndqb(&tp->t_outq, 0);	/* XXX */
   1000  1.66   tsutsui 	if (nch) {
   1001  1.66   tsutsui 		char *p = tp->t_outq.c_cf;
   1002   1.1       leo 
   1003   1.1       leo 		/* mark busy, enable tx done interrupts, & send first byte */
   1004   1.1       leo 		tp->t_state |= TS_BUSY;
   1005  1.66   tsutsui 		(void)splzs();
   1006   1.1       leo 		cs->cs_preg[1] |= ZSWR1_TIE;
   1007   1.1       leo 		cs->cs_creg[1] |= ZSWR1_TIE;
   1008   1.1       leo 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1009   1.1       leo 		cs->cs_zc->zc_data = *p;
   1010   1.1       leo 		cs->cs_tba = p + 1;
   1011   1.1       leo 		cs->cs_tbc = nch - 1;
   1012   1.1       leo 	} else {
   1013   1.1       leo 		/*
   1014   1.1       leo 		 * Nothing to send, turn off transmit done interrupts.
   1015   1.1       leo 		 * This is useful if something is doing polled output.
   1016   1.1       leo 		 */
   1017  1.66   tsutsui 		(void)splzs();
   1018   1.1       leo 		cs->cs_preg[1] &= ~ZSWR1_TIE;
   1019   1.1       leo 		cs->cs_creg[1] &= ~ZSWR1_TIE;
   1020   1.1       leo 		ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
   1021   1.1       leo 	}
   1022   1.1       leo out:
   1023   1.1       leo 	splx(s);
   1024   1.1       leo }
   1025   1.1       leo 
   1026   1.1       leo /*
   1027   1.1       leo  * Stop output, e.g., for ^S or output flush.
   1028   1.1       leo  */
   1029   1.1       leo void
   1030  1.66   tsutsui zsstop(struct tty *tp, int flag)
   1031   1.1       leo {
   1032  1.66   tsutsui 	struct zs_chanstate *cs;
   1033  1.66   tsutsui 	int s, unit = ZS_UNIT(tp->t_dev);
   1034  1.68   tsutsui 	struct zs_softc *sc = device_lookup_private(&zs_cd, unit >> 1);
   1035   1.1       leo 
   1036  1.72   tsutsui 	cs = sc->sc_cs[unit & 1];
   1037   1.1       leo 	s  = splzs();
   1038  1.66   tsutsui 	if ((tp->t_state & TS_BUSY) != 0) {
   1039   1.1       leo 		/*
   1040   1.1       leo 		 * Device is transmitting; must stop it.
   1041   1.1       leo 		 */
   1042   1.1       leo 		cs->cs_tbc = 0;
   1043   1.1       leo 		if ((tp->t_state & TS_TTSTOP) == 0)
   1044   1.1       leo 			tp->t_state |= TS_FLUSH;
   1045   1.1       leo 	}
   1046  1.29       leo 	splx(s);
   1047  1.29       leo }
   1048  1.29       leo 
   1049  1.29       leo static void
   1050  1.60       dsl zs_shutdown(struct zs_chanstate *cs)
   1051  1.29       leo {
   1052  1.66   tsutsui 	struct tty *tp = cs->cs_ttyp;
   1053  1.66   tsutsui 	int s;
   1054  1.29       leo 
   1055  1.29       leo 	s = splzs();
   1056  1.29       leo 
   1057  1.29       leo 	/*
   1058  1.29       leo 	 * Hang up if necessary.  Wait a bit, so the other side has time to
   1059  1.29       leo 	 * notice even if we immediately open the port again.
   1060  1.29       leo 	 */
   1061  1.66   tsutsui 	if ((tp->t_cflag & HUPCL) != 0) {
   1062  1.29       leo 		zs_modem(cs, 0, DMSET);
   1063  1.52  christos 		(void)tsleep((void *)cs, TTIPRI, ttclos, hz);
   1064  1.29       leo 	}
   1065  1.29       leo 
   1066  1.29       leo 	/* Clear any break condition set with TIOCSBRK. */
   1067  1.66   tsutsui 	if ((cs->cs_creg[5] & ZSWR5_BREAK) != 0) {
   1068  1.29       leo 		cs->cs_preg[5] &= ~ZSWR5_BREAK;
   1069  1.29       leo 		cs->cs_creg[5] &= ~ZSWR5_BREAK;
   1070  1.29       leo 		ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1071  1.29       leo 	}
   1072  1.29       leo 
   1073  1.29       leo 	/*
   1074  1.29       leo 	 * Drop all lines and cancel interrupts
   1075  1.29       leo 	 */
   1076  1.29       leo 	zs_loadchannelregs(cs->cs_zc, zs_init_regs);
   1077   1.1       leo 	splx(s);
   1078   1.1       leo }
   1079   1.1       leo 
   1080   1.1       leo /*
   1081   1.1       leo  * Set ZS tty parameters from termios.
   1082   1.1       leo  *
   1083   1.1       leo  * This routine makes use of the fact that only registers
   1084   1.1       leo  * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
   1085   1.1       leo  */
   1086   1.1       leo static int
   1087  1.66   tsutsui zsparam(struct tty *tp, struct termios *t)
   1088   1.1       leo {
   1089  1.66   tsutsui 	int unit = ZS_UNIT(tp->t_dev);
   1090  1.68   tsutsui 	struct zs_softc *sc = device_lookup_private(&zs_cd, unit >> 1);
   1091  1.72   tsutsui 	struct zs_chanstate *cs = sc->sc_cs[unit & 1];
   1092  1.66   tsutsui 	int cdiv = 0;	/* XXX gcc4 -Wuninitialized */
   1093  1.66   tsutsui 	int clkm = 0;	/* XXX gcc4 -Wuninitialized */
   1094  1.66   tsutsui 	int brgm = 0;	/* XXX gcc4 -Wuninitialized */
   1095  1.66   tsutsui 	int tcon = 0;	/* XXX gcc4 -Wuninitialized */
   1096  1.66   tsutsui 	int tmp, tmp5, cflag, s;
   1097   1.1       leo 
   1098   1.6       leo 	tmp  = t->c_ospeed;
   1099   1.6       leo 	tmp5 = t->c_ispeed;
   1100  1.66   tsutsui 	if (tmp < 0 || (tmp5 && tmp5 != tmp))
   1101  1.66   tsutsui 		return EINVAL;
   1102  1.66   tsutsui 	if (tmp == 0) {
   1103   1.1       leo 		/* stty 0 => drop DTR and RTS */
   1104   1.1       leo 		zs_modem(cs, 0, DMSET);
   1105  1.66   tsutsui 		return 0;
   1106   1.1       leo 	}
   1107   1.6       leo 	tmp = zsbaudrate(unit, tmp, &cdiv, &clkm, &brgm, &tcon);
   1108   1.6       leo 	if (tmp < 0)
   1109  1.66   tsutsui 		return EINVAL;
   1110   1.6       leo 	tp->t_ispeed = tp->t_ospeed = tmp;
   1111   1.1       leo 
   1112   1.6       leo 	cflag = tp->t_cflag = t->c_cflag;
   1113  1.66   tsutsui 	if ((cflag & CSTOPB) != 0)
   1114   1.6       leo 		cdiv |= ZSWR4_TWOSB;
   1115   1.6       leo 	else
   1116   1.6       leo 		cdiv |= ZSWR4_ONESB;
   1117  1.66   tsutsui 	if ((cflag & PARODD) == 0)
   1118   1.6       leo 		cdiv |= ZSWR4_EVENP;
   1119  1.66   tsutsui 	if ((cflag & PARENB) != 0)
   1120   1.6       leo 		cdiv |= ZSWR4_PARENB;
   1121   1.1       leo 
   1122  1.66   tsutsui 	switch (cflag & CSIZE) {
   1123   1.1       leo 	case CS5:
   1124   1.1       leo 		tmp  = ZSWR3_RX_5;
   1125   1.1       leo 		tmp5 = ZSWR5_TX_5;
   1126   1.1       leo 		break;
   1127   1.1       leo 	case CS6:
   1128   1.1       leo 		tmp  = ZSWR3_RX_6;
   1129   1.1       leo 		tmp5 = ZSWR5_TX_6;
   1130   1.1       leo 		break;
   1131   1.1       leo 	case CS7:
   1132   1.1       leo 		tmp  = ZSWR3_RX_7;
   1133   1.1       leo 		tmp5 = ZSWR5_TX_7;
   1134   1.1       leo 		break;
   1135   1.1       leo 	case CS8:
   1136   1.1       leo 	default:
   1137   1.1       leo 		tmp  = ZSWR3_RX_8;
   1138   1.1       leo 		tmp5 = ZSWR5_TX_8;
   1139   1.1       leo 		break;
   1140   1.1       leo 	}
   1141   1.6       leo 	tmp  |= ZSWR3_RX_ENABLE;
   1142   1.6       leo 	tmp5 |= ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
   1143   1.6       leo 
   1144   1.6       leo 	/*
   1145   1.6       leo 	 * Block interrupts so that state will not
   1146   1.6       leo 	 * be altered until we are done setting it up.
   1147   1.6       leo 	 */
   1148   1.6       leo 	s = splzs();
   1149   1.6       leo 	cs->cs_preg[4]  = cdiv;
   1150   1.6       leo 	cs->cs_preg[11] = clkm;
   1151   1.6       leo 	cs->cs_preg[12] = tcon;
   1152   1.6       leo 	cs->cs_preg[13] = tcon >> 8;
   1153   1.6       leo 	cs->cs_preg[14] = brgm;
   1154   1.6       leo 	cs->cs_preg[1]  = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
   1155   1.6       leo 	cs->cs_preg[9]  = ZSWR9_MASTER_IE | ZSWR9_VECTOR_INCL_STAT;
   1156   1.6       leo 	cs->cs_preg[10] = ZSWR10_NRZ;
   1157   1.6       leo 	cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
   1158   1.1       leo 
   1159   1.1       leo 	/*
   1160   1.1       leo 	 * Output hardware flow control on the chip is horrendous: if
   1161   1.1       leo 	 * carrier detect drops, the receiver is disabled.  Hence we
   1162   1.1       leo 	 * can only do this when the carrier is on.
   1163   1.1       leo 	 */
   1164  1.66   tsutsui 	if ((cflag & CCTS_OFLOW) != 0 &&
   1165  1.66   tsutsui 	    (cs->cs_zc->zc_csr & ZSRR0_DCD) != 0)
   1166   1.6       leo 		tmp |= ZSWR3_HFC;
   1167   1.1       leo 	cs->cs_preg[3] = tmp;
   1168   1.6       leo 	cs->cs_preg[5] = tmp5;
   1169   1.1       leo 
   1170   1.1       leo 	/*
   1171   1.1       leo 	 * If nothing is being transmitted, set up new current values,
   1172   1.1       leo 	 * else mark them as pending.
   1173   1.1       leo 	 */
   1174  1.66   tsutsui 	if (cs->cs_heldchange == 0) {
   1175  1.66   tsutsui 		if ((cs->cs_ttyp->t_state & TS_BUSY) != 0) {
   1176   1.1       leo 			cs->cs_heldtbc = cs->cs_tbc;
   1177   1.1       leo 			cs->cs_tbc = 0;
   1178   1.1       leo 			cs->cs_heldchange = 1;
   1179   1.6       leo 		} else {
   1180  1.63   tsutsui 			memcpy((void *)cs->cs_creg, (void *)cs->cs_preg, 16);
   1181   1.1       leo 			zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
   1182   1.1       leo 		}
   1183   1.1       leo 	}
   1184   1.1       leo 	splx(s);
   1185  1.66   tsutsui 	return 0;
   1186   1.6       leo }
   1187   1.6       leo 
   1188   1.6       leo /*
   1189   1.6       leo  * search for the best matching baudrate
   1190   1.6       leo  */
   1191   1.6       leo static int
   1192  1.66   tsutsui zsbaudrate(int unit, int wanted, int *divisor, int *clockmode, int *brgenmode,
   1193  1.66   tsutsui     int *timeconst)
   1194   1.6       leo {
   1195  1.66   tsutsui 	int bestdiff, bestbps, source;
   1196   1.6       leo 
   1197  1.20       leo 	bestdiff = bestbps = 0;
   1198   1.6       leo 	unit = (unit & 1) << 2;
   1199   1.6       leo 	for (source = 0; source < 4; ++source) {
   1200  1.71   tsutsui 		u_long freq = zs_frequencies[unit + source];
   1201  1.66   tsutsui 		int diff, bps, div, clkm, brgm, tcon;
   1202  1.20       leo 
   1203  1.20       leo 		bps = div = clkm = brgm = tcon = 0;
   1204   1.6       leo 		switch (source) {
   1205  1.66   tsutsui 		case 0:	/* BRgen, PCLK */
   1206  1.66   tsutsui 			brgm = ZSWR14_BAUD_ENA|ZSWR14_BAUD_FROM_PCLK;
   1207  1.66   tsutsui 			break;
   1208  1.66   tsutsui 		case 1:	/* BRgen, RTxC */
   1209  1.66   tsutsui 			brgm = ZSWR14_BAUD_ENA;
   1210  1.66   tsutsui 			break;
   1211  1.66   tsutsui 		case 2: /* RTxC */
   1212  1.66   tsutsui 			clkm = ZSWR11_RXCLK_RTXC|ZSWR11_TXCLK_RTXC;
   1213  1.66   tsutsui 			break;
   1214  1.66   tsutsui 		case 3: /* TRxC */
   1215  1.66   tsutsui 			clkm = ZSWR11_RXCLK_TRXC|ZSWR11_TXCLK_TRXC;
   1216  1.66   tsutsui 			break;
   1217   1.6       leo 		}
   1218   1.6       leo 		switch (source) {
   1219  1.66   tsutsui 		case 0:
   1220  1.66   tsutsui 		case 1:
   1221  1.66   tsutsui 			div  = ZSWR4_CLK_X16;
   1222  1.66   tsutsui 			clkm = ZSWR11_RXCLK_BAUD|ZSWR11_TXCLK_BAUD;
   1223  1.66   tsutsui 			tcon = BPS_TO_TCONST(freq, wanted);
   1224  1.66   tsutsui 			if (tcon < 0)
   1225  1.66   tsutsui 				tcon = 0;
   1226  1.66   tsutsui 			bps  = TCONST_TO_BPS(freq, tcon);
   1227  1.66   tsutsui 			break;
   1228  1.66   tsutsui 		case 2:
   1229  1.66   tsutsui 		case 3:
   1230  1.66   tsutsui 		    {
   1231  1.66   tsutsui 			int b1 = freq / 16, d1 = abs(b1 - wanted);
   1232  1.66   tsutsui 			int b2 = freq / 32, d2 = abs(b2 - wanted);
   1233  1.66   tsutsui 			int b3 = freq / 64, d3 = abs(b3 - wanted);
   1234  1.66   tsutsui 
   1235  1.66   tsutsui 			if (d1 < d2 && d1 < d3) {
   1236  1.66   tsutsui 				div = ZSWR4_CLK_X16;
   1237  1.66   tsutsui 				bps = b1;
   1238  1.66   tsutsui 			} else if (d2 < d3 && d2 < d1) {
   1239  1.66   tsutsui 				div = ZSWR4_CLK_X32;
   1240  1.66   tsutsui 				bps = b2;
   1241  1.66   tsutsui 			} else {
   1242  1.66   tsutsui 				div = ZSWR4_CLK_X64;
   1243  1.66   tsutsui 				bps = b3;
   1244   1.6       leo 			}
   1245  1.66   tsutsui 			brgm = tcon = 0;
   1246  1.66   tsutsui 			break;
   1247  1.66   tsutsui 		    }
   1248   1.6       leo 		}
   1249   1.6       leo 		diff = abs(bps - wanted);
   1250   1.6       leo 		if (!source || diff < bestdiff) {
   1251   1.6       leo 			*divisor   = div;
   1252   1.6       leo 			*clockmode = clkm;
   1253   1.6       leo 			*brgenmode = brgm;
   1254   1.6       leo 			*timeconst = tcon;
   1255   1.6       leo 			bestbps    = bps;
   1256   1.6       leo 			bestdiff   = diff;
   1257   1.6       leo 			if (diff == 0)
   1258   1.6       leo 				break;
   1259   1.6       leo 		}
   1260   1.6       leo 	}
   1261   1.6       leo 	/* Allow deviations upto 5% */
   1262   1.6       leo 	if (20 * bestdiff > wanted)
   1263   1.6       leo 		return -1;
   1264   1.6       leo 	return bestbps;
   1265   1.1       leo }
   1266   1.1       leo 
   1267   1.1       leo /*
   1268   1.1       leo  * Raise or lower modem control (DTR/RTS) signals.  If a character is
   1269   1.1       leo  * in transmission, the change is deferred.
   1270   1.1       leo  */
   1271   1.1       leo static int
   1272  1.61       dsl zs_modem(struct zs_chanstate *cs, int bits, int how)
   1273   1.1       leo {
   1274   1.1       leo 	int s, mbits;
   1275   1.1       leo 
   1276   1.1       leo 	bits  &= ZSWR5_DTR | ZSWR5_RTS;
   1277   1.1       leo 
   1278   1.1       leo 	s = splzs();
   1279   1.1       leo 	mbits  = cs->cs_preg[5] &  (ZSWR5_DTR | ZSWR5_RTS);
   1280   1.1       leo 
   1281  1.66   tsutsui 	switch (how) {
   1282  1.66   tsutsui 	case DMSET:
   1283  1.66   tsutsui 		mbits  = bits;
   1284  1.66   tsutsui 		break;
   1285  1.66   tsutsui 	case DMBIS:
   1286  1.66   tsutsui 		mbits |= bits;
   1287  1.66   tsutsui 		break;
   1288  1.66   tsutsui 	case DMBIC:
   1289  1.66   tsutsui 		mbits &= ~bits;
   1290  1.66   tsutsui 		break;
   1291  1.66   tsutsui 	case DMGET:
   1292  1.66   tsutsui 		splx(s);
   1293  1.66   tsutsui 		return mbits;
   1294   1.1       leo 	}
   1295   1.1       leo 
   1296   1.1       leo 	cs->cs_preg[5] = (cs->cs_preg[5] & ~(ZSWR5_DTR | ZSWR5_RTS)) | mbits;
   1297  1.66   tsutsui 	if (cs->cs_heldchange == 0) {
   1298  1.66   tsutsui 		if ((cs->cs_ttyp->t_state & TS_BUSY) != 0) {
   1299   1.1       leo 			cs->cs_heldtbc = cs->cs_tbc;
   1300   1.1       leo 			cs->cs_tbc = 0;
   1301   1.1       leo 			cs->cs_heldchange = 1;
   1302  1.66   tsutsui 		} else {
   1303   1.1       leo 			ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
   1304   1.1       leo 		}
   1305   1.1       leo 	}
   1306   1.1       leo 	splx(s);
   1307  1.66   tsutsui 	return 0;
   1308   1.1       leo }
   1309   1.1       leo 
   1310   1.1       leo /*
   1311   1.1       leo  * Write the given register set to the given zs channel in the proper order.
   1312   1.1       leo  * The channel must not be transmitting at the time.  The receiver will
   1313   1.1       leo  * be disabled for the time it takes to write all the registers.
   1314   1.1       leo  */
   1315   1.1       leo static void
   1316  1.67   tsutsui zs_loadchannelregs(struct zschan *zc, uint8_t *reg)
   1317   1.1       leo {
   1318   1.1       leo 	int i;
   1319   1.1       leo 
   1320   1.1       leo 	zc->zc_csr = ZSM_RESET_ERR;	/* reset error condition */
   1321   1.1       leo 	i = zc->zc_data;		/* drain fifo */
   1322   1.1       leo 	i = zc->zc_data;
   1323   1.1       leo 	i = zc->zc_data;
   1324   1.1       leo 	ZS_WRITE(zc,  4, reg[4]);
   1325   1.1       leo 	ZS_WRITE(zc, 10, reg[10]);
   1326   1.1       leo 	ZS_WRITE(zc,  3, reg[3] & ~ZSWR3_RX_ENABLE);
   1327   1.1       leo 	ZS_WRITE(zc,  5, reg[5] & ~ZSWR5_TX_ENABLE);
   1328   1.1       leo 	ZS_WRITE(zc,  1, reg[1]);
   1329   1.1       leo 	ZS_WRITE(zc,  9, reg[9]);
   1330   1.1       leo 	ZS_WRITE(zc, 11, reg[11]);
   1331   1.1       leo 	ZS_WRITE(zc, 12, reg[12]);
   1332   1.1       leo 	ZS_WRITE(zc, 13, reg[13]);
   1333   1.1       leo 	ZS_WRITE(zc, 14, reg[14]);
   1334   1.1       leo 	ZS_WRITE(zc, 15, reg[15]);
   1335   1.1       leo 	ZS_WRITE(zc,  3, reg[3]);
   1336   1.1       leo 	ZS_WRITE(zc,  5, reg[5]);
   1337   1.1       leo }
   1338   1.1       leo #endif /* NZS > 1 */
   1339