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