Home | History | Annotate | Line # | Download | only in dev
dcm.c revision 1.75
      1  1.75      elad /*	$NetBSD: dcm.c,v 1.75 2006/09/30 20:05:57 elad Exp $	*/
      2  1.42   thorpej 
      3  1.42   thorpej /*-
      4  1.42   thorpej  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5  1.42   thorpej  * All rights reserved.
      6  1.42   thorpej  *
      7  1.42   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.42   thorpej  * by Jason R. Thorpe.
      9  1.42   thorpej  *
     10  1.42   thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.42   thorpej  * modification, are permitted provided that the following conditions
     12  1.42   thorpej  * are met:
     13  1.42   thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.42   thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.42   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.42   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.42   thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.42   thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.42   thorpej  *    must display the following acknowledgement:
     20  1.42   thorpej  *	This product includes software developed by the NetBSD
     21  1.42   thorpej  *	Foundation, Inc. and its contributors.
     22  1.42   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.42   thorpej  *    contributors may be used to endorse or promote products derived
     24  1.42   thorpej  *    from this software without specific prior written permission.
     25  1.42   thorpej  *
     26  1.42   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.42   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.42   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.42   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.42   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.42   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.42   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.42   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.42   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.42   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.42   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.42   thorpej  */
     38  1.15       cgd 
     39   1.1       cgd /*
     40  1.14   mycroft  * Copyright (c) 1982, 1986, 1990, 1993
     41  1.14   mycroft  *	The Regents of the University of California.  All rights reserved.
     42   1.1       cgd  *
     43   1.1       cgd  * This code is derived from software contributed to Berkeley by
     44   1.1       cgd  * the Systems Programming Group of the University of Utah Computer
     45   1.1       cgd  * Science Department.
     46   1.1       cgd  *
     47   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     48   1.1       cgd  * modification, are permitted provided that the following conditions
     49   1.1       cgd  * are met:
     50   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     51   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     52   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     53   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     54   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     55  1.62       agc  * 3. Neither the name of the University nor the names of its contributors
     56  1.62       agc  *    may be used to endorse or promote products derived from this software
     57  1.62       agc  *    without specific prior written permission.
     58  1.62       agc  *
     59  1.62       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  1.62       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  1.62       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  1.62       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  1.62       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  1.62       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  1.62       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  1.62       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  1.62       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  1.62       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  1.62       agc  * SUCH DAMAGE.
     70  1.62       agc  *
     71  1.62       agc  * from Utah: $Hdr: dcm.c 1.29 92/01/21$
     72  1.62       agc  *
     73  1.62       agc  *	@(#)dcm.c	8.4 (Berkeley) 1/12/94
     74  1.62       agc  */
     75  1.62       agc /*
     76  1.62       agc  * Copyright (c) 1988 University of Utah.
     77  1.62       agc  *
     78  1.62       agc  * This code is derived from software contributed to Berkeley by
     79  1.62       agc  * the Systems Programming Group of the University of Utah Computer
     80  1.62       agc  * Science Department.
     81  1.62       agc  *
     82  1.62       agc  * Redistribution and use in source and binary forms, with or without
     83  1.62       agc  * modification, are permitted provided that the following conditions
     84  1.62       agc  * are met:
     85  1.62       agc  * 1. Redistributions of source code must retain the above copyright
     86  1.62       agc  *    notice, this list of conditions and the following disclaimer.
     87  1.62       agc  * 2. Redistributions in binary form must reproduce the above copyright
     88  1.62       agc  *    notice, this list of conditions and the following disclaimer in the
     89  1.62       agc  *    documentation and/or other materials provided with the distribution.
     90   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     91   1.1       cgd  *    must display the following acknowledgement:
     92   1.1       cgd  *	This product includes software developed by the University of
     93   1.1       cgd  *	California, Berkeley and its contributors.
     94   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     95   1.1       cgd  *    may be used to endorse or promote products derived from this software
     96   1.1       cgd  *    without specific prior written permission.
     97   1.1       cgd  *
     98   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     99   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    100   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    101   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    102   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    103   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    104   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    105   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    106   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    107   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    108   1.1       cgd  * SUCH DAMAGE.
    109   1.1       cgd  *
    110  1.14   mycroft  * from Utah: $Hdr: dcm.c 1.29 92/01/21$
    111  1.14   mycroft  *
    112  1.15       cgd  *	@(#)dcm.c	8.4 (Berkeley) 1/12/94
    113   1.1       cgd  */
    114   1.1       cgd 
    115   1.1       cgd /*
    116   1.1       cgd  * TODO:
    117   1.1       cgd  *	Timeouts
    118   1.1       cgd  *	Test console support.
    119   1.1       cgd  */
    120   1.1       cgd 
    121   1.1       cgd /*
    122   1.1       cgd  *  98642/MUX
    123   1.1       cgd  */
    124  1.50  gmcgarry 
    125  1.50  gmcgarry #include <sys/cdefs.h>
    126  1.75      elad __KERNEL_RCSID(0, "$NetBSD: dcm.c,v 1.75 2006/09/30 20:05:57 elad Exp $");
    127  1.47     lukem 
    128  1.47     lukem #include "opt_kgdb.h"
    129  1.47     lukem 
    130  1.13   mycroft #include <sys/param.h>
    131  1.13   mycroft #include <sys/systm.h>
    132  1.13   mycroft #include <sys/ioctl.h>
    133  1.13   mycroft #include <sys/proc.h>
    134  1.13   mycroft #include <sys/tty.h>
    135  1.13   mycroft #include <sys/conf.h>
    136  1.13   mycroft #include <sys/file.h>
    137  1.13   mycroft #include <sys/uio.h>
    138  1.13   mycroft #include <sys/kernel.h>
    139  1.13   mycroft #include <sys/syslog.h>
    140  1.13   mycroft #include <sys/time.h>
    141  1.34   thorpej #include <sys/device.h>
    142  1.71      elad #include <sys/kauth.h>
    143  1.13   mycroft 
    144  1.59  gmcgarry #include <machine/bus.h>
    145  1.14   mycroft 
    146  1.22   thorpej #include <dev/cons.h>
    147  1.22   thorpej 
    148  1.34   thorpej #include <hp300/dev/diovar.h>
    149  1.34   thorpej #include <hp300/dev/diodevs.h>
    150  1.13   mycroft #include <hp300/dev/dcmreg.h>
    151   1.1       cgd 
    152  1.66   tsutsui #include "ioconf.h"
    153  1.66   tsutsui 
    154   1.1       cgd #ifndef DEFAULT_BAUD_RATE
    155   1.1       cgd #define DEFAULT_BAUD_RATE 9600
    156   1.1       cgd #endif
    157   1.1       cgd 
    158  1.65   thorpej static const struct speedtab dcmspeedtab[] = {
    159  1.36    scottr 	{	0,	BR_0		},
    160  1.36    scottr 	{	50,	BR_50		},
    161  1.36    scottr 	{	75,	BR_75		},
    162  1.36    scottr 	{	110,	BR_110		},
    163  1.36    scottr 	{	134,	BR_134		},
    164  1.36    scottr 	{	150,	BR_150		},
    165  1.36    scottr 	{	300,	BR_300		},
    166  1.36    scottr 	{	600,	BR_600		},
    167  1.36    scottr 	{	1200,	BR_1200		},
    168  1.36    scottr 	{	1800,	BR_1800		},
    169  1.36    scottr 	{	2400,	BR_2400		},
    170  1.36    scottr 	{	4800,	BR_4800		},
    171  1.36    scottr 	{	9600,	BR_9600		},
    172  1.36    scottr 	{	19200,	BR_19200	},
    173  1.36    scottr 	{	38400,	BR_38400	},
    174  1.36    scottr 	{	-1,	-1		},
    175   1.1       cgd };
    176   1.1       cgd 
    177   1.1       cgd /* u-sec per character based on baudrate (assumes 1 start/8 data/1 stop bit) */
    178   1.1       cgd #define	DCM_USPERCH(s)	(10000000 / (s))
    179   1.1       cgd 
    180   1.1       cgd /*
    181   1.1       cgd  * Per board interrupt scheme.  16.7ms is the polling interrupt rate
    182   1.1       cgd  * (16.7ms is about 550 baud, 38.4k is 72 chars in 16.7ms).
    183   1.1       cgd  */
    184   1.1       cgd #define DIS_TIMER	0
    185   1.1       cgd #define DIS_PERCHAR	1
    186   1.1       cgd #define DIS_RESET	2
    187   1.1       cgd 
    188  1.65   thorpej static int	dcmistype = -1; /* -1 == dynamic, 0 == timer, 1 == perchar */
    189  1.65   thorpej static int     dcminterval = 5;	/* interval (secs) between checks */
    190   1.1       cgd struct	dcmischeme {
    191   1.1       cgd 	int	dis_perchar;	/* non-zero if interrupting per char */
    192   1.1       cgd 	long	dis_time;	/* last time examined */
    193   1.1       cgd 	int	dis_intr;	/* recv interrupts during last interval */
    194   1.1       cgd 	int	dis_char;	/* characters read during last interval */
    195  1.20   thorpej };
    196   1.1       cgd 
    197   1.1       cgd #ifdef KGDB
    198   1.1       cgd /*
    199   1.1       cgd  * Kernel GDB support
    200   1.1       cgd  */
    201  1.14   mycroft #include <machine/remote-sl.h>
    202   1.1       cgd 
    203   1.1       cgd extern dev_t kgdb_dev;
    204   1.1       cgd extern int kgdb_rate;
    205   1.1       cgd extern int kgdb_debug_init;
    206   1.1       cgd #endif
    207   1.1       cgd 
    208  1.14   mycroft /* #define DCMSTATS */
    209   1.1       cgd 
    210   1.1       cgd #ifdef DEBUG
    211   1.1       cgd int	dcmdebug = 0x0;
    212   1.1       cgd #define DDB_SIOERR	0x01
    213   1.1       cgd #define DDB_PARAM	0x02
    214   1.1       cgd #define DDB_INPUT	0x04
    215   1.1       cgd #define DDB_OUTPUT	0x08
    216   1.1       cgd #define DDB_INTR	0x10
    217   1.1       cgd #define DDB_IOCTL	0x20
    218   1.1       cgd #define DDB_INTSCHM	0x40
    219   1.1       cgd #define DDB_MODEM	0x80
    220   1.1       cgd #define DDB_OPENCLOSE	0x100
    221   1.1       cgd #endif
    222   1.1       cgd 
    223  1.14   mycroft #ifdef DCMSTATS
    224   1.1       cgd #define	DCMRBSIZE	94
    225   1.1       cgd #define DCMXBSIZE	24
    226   1.1       cgd 
    227   1.1       cgd struct	dcmstats {
    228   1.1       cgd 	long	xints;		    /* # of xmit ints */
    229   1.1       cgd 	long	xchars;		    /* # of xmit chars */
    230   1.1       cgd 	long	xempty;		    /* times outq is empty in dcmstart */
    231   1.1       cgd 	long	xrestarts;	    /* times completed while xmitting */
    232   1.1       cgd 	long	rints;		    /* # of recv ints */
    233   1.1       cgd 	long	rchars;		    /* # of recv chars */
    234   1.1       cgd 	long	xsilo[DCMXBSIZE+2]; /* times this many chars xmit on one int */
    235   1.1       cgd 	long	rsilo[DCMRBSIZE+2]; /* times this many chars read on one int */
    236  1.20   thorpej };
    237   1.1       cgd #endif
    238   1.1       cgd 
    239  1.44   thorpej #define DCMUNIT(x)		(minor(x) & 0x7ffff)
    240  1.44   thorpej #define	DCMDIALOUT(x)		(minor(x) & 0x80000)
    241  1.20   thorpej #define	DCMBOARD(x)		(((x) >> 2) & 0x3f)
    242  1.20   thorpej #define DCMPORT(x)		((x) & 3)
    243   1.1       cgd 
    244   1.1       cgd /*
    245   1.1       cgd  * Conversion from "HP DCE" to almost-normal DCE: on the 638 8-port mux,
    246   1.1       cgd  * the distribution panel uses "HP DCE" conventions.  If requested via
    247   1.1       cgd  * the device flags, we swap the inputs to something closer to normal DCE,
    248   1.1       cgd  * allowing a straight-through cable to a DTE or a reversed cable
    249   1.1       cgd  * to a DCE (reversing 2-3, 4-5, 8-20 and leaving 6 unconnected;
    250   1.1       cgd  * this gets "DCD" on pin 20 and "CTS" on 4, but doesn't connect
    251   1.1       cgd  * DSR or make RTS work, though).  The following gives the full
    252   1.1       cgd  * details of a cable from this mux panel to a modem:
    253   1.1       cgd  *
    254   1.1       cgd  *		     HP		    modem
    255   1.1       cgd  *		name	pin	pin	name
    256   1.1       cgd  * HP inputs:
    257   1.1       cgd  *		"Rx"	 2	 3	Tx
    258   1.1       cgd  *		CTS	 4	 5	CTS	(only needed for CCTS_OFLOW)
    259   1.1       cgd  *		DCD	20	 8	DCD
    260   1.1       cgd  *		"DSR"	 9	 6	DSR	(unneeded)
    261   1.1       cgd  *		RI	22	22	RI	(unneeded)
    262   1.1       cgd  *
    263   1.1       cgd  * HP outputs:
    264   1.1       cgd  *		"Tx"	 3	 2	Rx
    265   1.1       cgd  *		"DTR"	 6	not connected
    266   1.1       cgd  *		"RTS"	 8	20	DTR
    267   1.1       cgd  *		"SR"	23	 4	RTS	(often not needed)
    268   1.1       cgd  */
    269   1.1       cgd #define hp2dce_in(ibits)	(iconv[(ibits) & 0xf])
    270  1.65   thorpej static const char iconv[16] = {
    271   1.1       cgd 	0,		MI_DM,		MI_CTS,		MI_CTS|MI_DM,
    272   1.1       cgd 	MI_CD,		MI_CD|MI_DM,	MI_CD|MI_CTS,	MI_CD|MI_CTS|MI_DM,
    273   1.1       cgd 	MI_RI,		MI_RI|MI_DM,	MI_RI|MI_CTS,	MI_RI|MI_CTS|MI_DM,
    274   1.1       cgd 	MI_RI|MI_CD,	MI_RI|MI_CD|MI_DM, MI_RI|MI_CD|MI_CTS,
    275   1.1       cgd 	MI_RI|MI_CD|MI_CTS|MI_DM
    276   1.1       cgd };
    277   1.1       cgd 
    278  1.21   thorpej /*
    279  1.21   thorpej  * Note that 8-port boards appear as 2 4-port boards at consecutive
    280  1.21   thorpej  * select codes.
    281  1.21   thorpej  */
    282  1.21   thorpej #define	NDCMPORT	4
    283  1.20   thorpej 
    284  1.20   thorpej struct	dcm_softc {
    285  1.34   thorpej 	struct	device sc_dev;		/* generic device glue */
    286  1.59  gmcgarry 
    287  1.59  gmcgarry 	bus_space_tag_t sc_bst;
    288  1.59  gmcgarry 	bus_space_handle_t sc_bsh;
    289  1.59  gmcgarry 
    290  1.20   thorpej 	struct	dcmdevice *sc_dcm;	/* pointer to hardware */
    291  1.20   thorpej 	struct	tty *sc_tty[NDCMPORT];	/* our tty instances */
    292  1.20   thorpej 	struct	modemreg *sc_modem[NDCMPORT]; /* modem control */
    293  1.20   thorpej 	char	sc_mcndlast[NDCMPORT];	/* XXX last modem status for port */
    294  1.20   thorpej 	short	sc_softCAR;		/* mask of ports with soft-carrier */
    295  1.20   thorpej 	struct	dcmischeme sc_scheme;	/* interrupt scheme for board */
    296  1.20   thorpej 
    297  1.20   thorpej 	/*
    298  1.20   thorpej 	 * Mask of soft-carrier bits in config flags.
    299  1.20   thorpej 	 */
    300  1.20   thorpej #define	DCM_SOFTCAR	0x0000000f
    301  1.20   thorpej 
    302  1.20   thorpej 	int	sc_flags;		/* misc. configuration info */
    303  1.20   thorpej 
    304  1.20   thorpej 	/*
    305  1.20   thorpej 	 * Bits for sc_flags
    306  1.20   thorpej 	 */
    307  1.20   thorpej #define	DCM_ACTIVE	0x00000001	/* indicates board is alive */
    308  1.24   thorpej #define	DCM_ISCONSOLE	0x00000002	/* indicates board is console */
    309  1.20   thorpej #define	DCM_STDDCE	0x00000010	/* re-map DCE to standard */
    310  1.20   thorpej #define	DCM_FLAGMASK	(DCM_STDDCE)	/* mask of valid bits in config flags */
    311  1.20   thorpej 
    312  1.20   thorpej #ifdef DCMSTATS
    313  1.20   thorpej 	struct	dcmstats sc_stats;	/* metrics gathering */
    314  1.20   thorpej #endif
    315  1.34   thorpej };
    316  1.34   thorpej 
    317  1.65   thorpej static int	dcmintr(void *);
    318  1.65   thorpej static void	dcmpint(struct dcm_softc *, int, int);
    319  1.65   thorpej static void	dcmrint(struct dcm_softc *);
    320  1.65   thorpej static void	dcmreadbuf(struct dcm_softc *, int);
    321  1.65   thorpej static void	dcmxint(struct dcm_softc *, int);
    322  1.65   thorpej static void	dcmmint(struct dcm_softc *, int, int);
    323  1.65   thorpej 
    324  1.65   thorpej static int	dcmparam(struct tty *, struct termios *);
    325  1.65   thorpej static void	dcmstart(struct tty *);
    326  1.65   thorpej static int	dcmmctl(dev_t, int, int);
    327  1.65   thorpej static void	dcmsetischeme(int, int);
    328  1.65   thorpej static void	dcminit(struct dcmdevice *, int, int);
    329  1.65   thorpej 
    330  1.65   thorpej static int	dcmselftest(struct dcm_softc *);
    331  1.36    scottr 
    332  1.65   thorpej static int	dcmcngetc(dev_t);
    333  1.65   thorpej static void	dcmcnputc(dev_t, int);
    334  1.65   thorpej 
    335  1.65   thorpej int	dcmcnattach(bus_space_tag_t, bus_addr_t, int);
    336  1.65   thorpej 
    337  1.65   thorpej static int	dcmmatch(struct device *, struct cfdata *, void *);
    338  1.65   thorpej static void	dcmattach(struct device *, struct device *, void *);
    339  1.38    scottr 
    340  1.54   thorpej CFATTACH_DECL(dcm, sizeof(struct dcm_softc),
    341  1.54   thorpej     dcmmatch, dcmattach, NULL, NULL);
    342  1.38    scottr 
    343  1.48  gmcgarry /*
    344  1.48  gmcgarry  * Stuff for DCM console support.  This could probably be done a little
    345  1.48  gmcgarry  * better.
    346  1.48  gmcgarry  */
    347  1.48  gmcgarry static	struct dcmdevice *dcm_cn = NULL;	/* pointer to hardware */
    348  1.48  gmcgarry static	int dcmconsinit;			/* has been initialized */
    349  1.48  gmcgarry /* static	int dcm_lastcnpri = CN_DEAD; */	/* XXX last priority */
    350  1.48  gmcgarry 
    351  1.48  gmcgarry static struct consdev dcm_cons = {
    352  1.63   tsutsui 	NULL,
    353  1.63   tsutsui 	NULL,
    354  1.63   tsutsui 	dcmcngetc,
    355  1.63   tsutsui 	dcmcnputc,
    356  1.63   tsutsui 	nullcnpollc,
    357  1.63   tsutsui 	NULL,
    358  1.63   tsutsui 	NULL,
    359  1.63   tsutsui 	NULL,
    360  1.63   tsutsui 	NODEV,
    361  1.63   tsutsui 	CN_REMOTE
    362  1.48  gmcgarry };
    363  1.48  gmcgarry int	dcmconscode;
    364  1.48  gmcgarry int	dcmdefaultrate = DEFAULT_BAUD_RATE;
    365  1.48  gmcgarry int	dcmconbrdbusy = 0;
    366  1.48  gmcgarry 
    367  1.65   thorpej static dev_type_open(dcmopen);
    368  1.65   thorpej static dev_type_close(dcmclose);
    369  1.65   thorpej static dev_type_read(dcmread);
    370  1.65   thorpej static dev_type_write(dcmwrite);
    371  1.65   thorpej static dev_type_ioctl(dcmioctl);
    372  1.65   thorpej static dev_type_stop(dcmstop);
    373  1.65   thorpej static dev_type_tty(dcmtty);
    374  1.65   thorpej static dev_type_poll(dcmpoll);
    375  1.52   gehenna 
    376  1.52   gehenna const struct cdevsw dcm_cdevsw = {
    377  1.52   gehenna 	dcmopen, dcmclose, dcmread, dcmwrite, dcmioctl,
    378  1.56  jdolecek 	dcmstop, dcmtty, dcmpoll, nommap, ttykqfilter, D_TTY
    379  1.52   gehenna };
    380  1.52   gehenna 
    381  1.65   thorpej static int
    382  1.65   thorpej dcmmatch(struct device *parent, struct cfdata *match, void *aux)
    383  1.34   thorpej {
    384  1.34   thorpej 	struct dio_attach_args *da = aux;
    385  1.34   thorpej 
    386  1.34   thorpej 	switch (da->da_id) {
    387  1.34   thorpej 	case DIO_DEVICE_ID_DCM:
    388  1.34   thorpej 	case DIO_DEVICE_ID_DCMREM:
    389  1.72   tsutsui 		return 1;
    390  1.34   thorpej 	}
    391  1.34   thorpej 
    392  1.72   tsutsui 	return 0;
    393  1.34   thorpej }
    394  1.20   thorpej 
    395  1.65   thorpej static void
    396  1.65   thorpej dcmattach(struct device *parent, struct device *self, void *aux)
    397  1.34   thorpej {
    398  1.34   thorpej 	struct dcm_softc *sc = (struct dcm_softc *)self;
    399  1.34   thorpej 	struct dio_attach_args *da = aux;
    400  1.34   thorpej 	struct dcmdevice *dcm;
    401  1.69   thorpej 	int brd = device_unit(self);
    402  1.34   thorpej 	int scode = da->da_scode;
    403  1.59  gmcgarry 	int i, mbits, code;
    404  1.20   thorpej 
    405  1.36    scottr 	sc->sc_flags = 0;
    406  1.36    scottr 
    407  1.48  gmcgarry 	if (scode == dcmconscode) {
    408  1.48  gmcgarry 		dcm = dcm_cn;
    409  1.24   thorpej 		sc->sc_flags |= DCM_ISCONSOLE;
    410  1.24   thorpej 
    411  1.24   thorpej 		/*
    412  1.24   thorpej 		 * We didn't know which unit this would be during
    413  1.24   thorpej 		 * the console probe, so we have to fixup cn_dev here.
    414  1.24   thorpej 		 * Note that we always assume port 1 on the board.
    415  1.24   thorpej 		 */
    416  1.52   gehenna 		cn_tab->cn_dev = makedev(cdevsw_lookup_major(&dcm_cdevsw),
    417  1.52   gehenna 					 (brd << 2) | DCMCONSPORT);
    418  1.34   thorpej 	} else {
    419  1.59  gmcgarry 		sc->sc_bst = da->da_bst;
    420  1.59  gmcgarry 		if (bus_space_map(sc->sc_bst, da->da_addr, da->da_size,
    421  1.59  gmcgarry 		    BUS_SPACE_MAP_LINEAR, &sc->sc_bsh)) {
    422  1.34   thorpej 			printf("\n%s: can't map registers\n",
    423  1.34   thorpej 			    sc->sc_dev.dv_xname);
    424  1.34   thorpej 			return;
    425  1.34   thorpej 		}
    426  1.59  gmcgarry 		dcm = (struct dcmdevice *)bus_space_vaddr(sc->sc_bst,
    427  1.59  gmcgarry 		    sc->sc_bsh);
    428  1.34   thorpej 	}
    429  1.34   thorpej 
    430  1.34   thorpej 	sc->sc_dcm = dcm;
    431  1.34   thorpej 
    432  1.38    scottr 	/*
    433  1.38    scottr 	 * XXX someone _should_ fix this; the self test screws
    434  1.38    scottr 	 * autoconfig messages.
    435  1.38    scottr 	 */
    436  1.38    scottr 	if ((sc->sc_flags & DCM_ISCONSOLE) && dcmselftest(sc)) {
    437  1.34   thorpej 		printf("\n%s: self-test failed\n", sc->sc_dev.dv_xname);
    438  1.34   thorpej 		return;
    439  1.24   thorpej 	}
    440  1.20   thorpej 
    441  1.20   thorpej 	/* Extract configuration info from flags. */
    442  1.70   thorpej 	sc->sc_softCAR = device_cfdata(self)->cf_flags & DCM_SOFTCAR;
    443  1.70   thorpej 	sc->sc_flags |= device_cfdata(self)->cf_flags & DCM_FLAGMASK;
    444  1.20   thorpej 
    445  1.20   thorpej 	/* Mark our unit as configured. */
    446  1.20   thorpej 	sc->sc_flags |= DCM_ACTIVE;
    447  1.20   thorpej 
    448  1.20   thorpej 	/* Establish the interrupt handler. */
    449  1.59  gmcgarry 	(void) dio_intr_establish(dcmintr, sc, da->da_ipl, IPL_TTY);
    450  1.20   thorpej 
    451   1.1       cgd 	if (dcmistype == DIS_TIMER)
    452   1.1       cgd 		dcmsetischeme(brd, DIS_RESET|DIS_TIMER);
    453   1.1       cgd 	else
    454   1.1       cgd 		dcmsetischeme(brd, DIS_RESET|DIS_PERCHAR);
    455   1.1       cgd 
    456   1.1       cgd 	/* load pointers to modem control */
    457  1.20   thorpej 	sc->sc_modem[0] = &dcm->dcm_modem0;
    458  1.20   thorpej 	sc->sc_modem[1] = &dcm->dcm_modem1;
    459  1.20   thorpej 	sc->sc_modem[2] = &dcm->dcm_modem2;
    460  1.20   thorpej 	sc->sc_modem[3] = &dcm->dcm_modem3;
    461  1.20   thorpej 
    462   1.1       cgd 	/* set DCD (modem) and CTS (flow control) on all ports */
    463  1.20   thorpej 	if (sc->sc_flags & DCM_STDDCE)
    464   1.1       cgd 		mbits = hp2dce_in(MI_CD|MI_CTS);
    465   1.1       cgd 	else
    466   1.1       cgd 		mbits = MI_CD|MI_CTS;
    467  1.20   thorpej 
    468  1.20   thorpej 	for (i = 0; i < NDCMPORT; i++)
    469  1.20   thorpej 		sc->sc_modem[i]->mdmmsk = mbits;
    470   1.1       cgd 
    471  1.38    scottr 	/*
    472  1.38    scottr 	 * Get current state of mdmin register on all ports, so that
    473  1.38    scottr 	 * deltas will work properly.
    474  1.38    scottr 	 */
    475  1.38    scottr 	for (i = 0; i < NDCMPORT; i++) {
    476  1.38    scottr 		code = sc->sc_modem[i]->mdmin;
    477  1.38    scottr 		if (sc->sc_flags & DCM_STDDCE)
    478  1.38    scottr 			code = hp2dce_in(code);
    479  1.38    scottr 		sc->sc_mcndlast[i] = code;
    480  1.38    scottr 	}
    481  1.38    scottr 
    482   1.1       cgd 	dcm->dcm_ic = IC_IE;		/* turn all interrupts on */
    483  1.20   thorpej 
    484   1.1       cgd 	/*
    485  1.24   thorpej 	 * Need to reset baud rate, etc. of next print so reset dcmconsinit.
    486   1.1       cgd 	 * Also make sure console is always "hardwired"
    487   1.1       cgd 	 */
    488  1.24   thorpej 	if (sc->sc_flags & DCM_ISCONSOLE) {
    489   1.1       cgd 		dcmconsinit = 0;
    490  1.24   thorpej 		sc->sc_softCAR |= (1 << DCMCONSPORT);
    491  1.31  christos 		printf(": console on port %d\n", DCMCONSPORT);
    492  1.20   thorpej 	} else
    493  1.31  christos 		printf("\n");
    494  1.20   thorpej 
    495  1.20   thorpej #ifdef KGDB
    496  1.52   gehenna 	if (cdevsw_lookup(kgdb_dev) == &dcm_cdevsw &&
    497  1.20   thorpej 	    DCMBOARD(DCMUNIT(kgdb_dev)) == brd) {
    498  1.24   thorpej 		if (dcmconsole == DCMUNIT(kgdb_dev))	/* XXX fixme */
    499  1.20   thorpej 			kgdb_dev = NODEV; /* can't debug over console port */
    500  1.20   thorpej #ifndef KGDB_CHEAT
    501  1.20   thorpej 		/*
    502  1.20   thorpej 		 * The following could potentially be replaced
    503  1.20   thorpej 		 * by the corresponding code in dcmcnprobe.
    504  1.20   thorpej 		 */
    505  1.20   thorpej 		else {
    506  1.22   thorpej 			dcminit(dcm, DCMPORT(DCMUNIT(kgdb_dev)),
    507  1.22   thorpej 			    kgdb_rate);
    508  1.20   thorpej 			if (kgdb_debug_init) {
    509  1.34   thorpej 				printf("%s port %d: ", sc->sc_dev.dv_xname,
    510  1.20   thorpej 				    DCMPORT(DCMUNIT(kgdb_dev)));
    511  1.20   thorpej 				kgdb_connect(1);
    512  1.20   thorpej 			} else
    513  1.31  christos 				printf("%s port %d: kgdb enabled\n",
    514  1.34   thorpej 				    sc->sc_dev.dv_xname,
    515  1.20   thorpej 				    DCMPORT(DCMUNIT(kgdb_dev)));
    516  1.20   thorpej 		}
    517  1.20   thorpej 		/* end could be replaced */
    518  1.34   thorpej #endif /* KGDB_CHEAT */
    519   1.1       cgd 	}
    520  1.34   thorpej #endif /* KGDB */
    521   1.1       cgd }
    522   1.1       cgd 
    523   1.1       cgd /* ARGSUSED */
    524  1.65   thorpej static int
    525  1.68  christos dcmopen(dev_t dev, int flag, int mode, struct lwp *l)
    526   1.1       cgd {
    527  1.20   thorpej 	struct dcm_softc *sc;
    528  1.20   thorpej 	struct tty *tp;
    529  1.20   thorpej 	int unit, brd, port;
    530  1.18   thorpej 	int error = 0, mbits, s;
    531   1.1       cgd 
    532  1.20   thorpej 	unit = DCMUNIT(dev);
    533  1.20   thorpej 	brd = DCMBOARD(unit);
    534  1.20   thorpej 	port = DCMPORT(unit);
    535  1.20   thorpej 
    536  1.34   thorpej 	if (brd >= dcm_cd.cd_ndevs || port >= NDCMPORT ||
    537  1.34   thorpej 	    (sc = dcm_cd.cd_devs[brd]) == NULL)
    538  1.72   tsutsui 		return ENXIO;
    539  1.20   thorpej 
    540  1.20   thorpej 	if ((sc->sc_flags & DCM_ACTIVE) == 0)
    541  1.72   tsutsui 		return ENXIO;
    542  1.20   thorpej 
    543  1.28   thorpej 	if (sc->sc_tty[port] == NULL) {
    544  1.20   thorpej 		tp = sc->sc_tty[port] = ttymalloc();
    545  1.28   thorpej 		tty_attach(tp);
    546  1.28   thorpej 	} else
    547  1.20   thorpej 		tp = sc->sc_tty[port];
    548  1.20   thorpej 
    549   1.1       cgd 	tp->t_oproc = dcmstart;
    550   1.1       cgd 	tp->t_param = dcmparam;
    551   1.1       cgd 	tp->t_dev = dev;
    552  1.18   thorpej 
    553  1.75      elad 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tty))
    554  1.75      elad 		return (EBUSY);
    555  1.44   thorpej 
    556  1.44   thorpej 	s = spltty();
    557  1.44   thorpej 
    558  1.44   thorpej 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
    559  1.18   thorpej 		/*
    560  1.18   thorpej 		 * Sanity clause: reset the card on first open.
    561  1.18   thorpej 		 * The card might be left in an inconsistent state
    562  1.18   thorpej 		 * if the card memory is read inadvertently.
    563  1.18   thorpej 		 */
    564  1.22   thorpej 		dcminit(sc->sc_dcm, port, dcmdefaultrate);
    565  1.18   thorpej 
    566   1.1       cgd 		ttychars(tp);
    567  1.18   thorpej 		tp->t_iflag = TTYDEF_IFLAG;
    568  1.18   thorpej 		tp->t_oflag = TTYDEF_OFLAG;
    569  1.18   thorpej 		tp->t_cflag = TTYDEF_CFLAG;
    570  1.18   thorpej 		tp->t_lflag = TTYDEF_LFLAG;
    571  1.18   thorpej 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    572  1.18   thorpej 
    573   1.1       cgd 		(void) dcmparam(tp, &tp->t_termios);
    574   1.1       cgd 		ttsetwater(tp);
    575  1.18   thorpej 
    576  1.44   thorpej 		/* Set modem control state. */
    577  1.44   thorpej 		mbits = MO_ON;
    578  1.44   thorpej 		if (sc->sc_flags & DCM_STDDCE)
    579  1.44   thorpej 			mbits |= MO_SR;	/* pin 23, could be used as RTS */
    580  1.44   thorpej 
    581  1.44   thorpej 		(void) dcmmctl(dev, mbits, DMSET);	/* enable port */
    582  1.20   thorpej 
    583  1.44   thorpej 		/* Set soft-carrier if so configured. */
    584  1.44   thorpej 		if ((sc->sc_softCAR & (1 << port)) ||
    585  1.44   thorpej 		    (dcmmctl(dev, MO_OFF, DMGET) & MI_CD))
    586  1.44   thorpej 			tp->t_state |= TS_CARR_ON;
    587  1.44   thorpej 	}
    588  1.18   thorpej 
    589  1.44   thorpej 	splx(s);
    590  1.18   thorpej 
    591   1.1       cgd #ifdef DEBUG
    592   1.1       cgd 	if (dcmdebug & DDB_MODEM)
    593  1.31  christos 		printf("%s: dcmopen port %d softcarr %c\n",
    594  1.34   thorpej 		       sc->sc_dev.dv_xname, port,
    595  1.20   thorpej 		       (tp->t_state & TS_CARR_ON) ? '1' : '0');
    596   1.1       cgd #endif
    597  1.18   thorpej 
    598  1.44   thorpej 	error = ttyopen(tp, DCMDIALOUT(dev), (flag & O_NONBLOCK));
    599  1.44   thorpej 	if (error)
    600  1.44   thorpej 		goto bad;
    601   1.1       cgd 
    602   1.1       cgd #ifdef DEBUG
    603   1.1       cgd 	if (dcmdebug & DDB_OPENCLOSE)
    604  1.31  christos 		printf("%s port %d: dcmopen: st %x fl %x\n",
    605  1.34   thorpej 			sc->sc_dev.dv_xname, port, tp->t_state, tp->t_flags);
    606   1.1       cgd #endif
    607  1.45       eeh 	error = (*tp->t_linesw->l_open)(dev, tp);
    608  1.18   thorpej 
    609  1.44   thorpej  bad:
    610  1.72   tsutsui 	return error;
    611   1.1       cgd }
    612  1.63   tsutsui 
    613   1.1       cgd /*ARGSUSED*/
    614  1.65   thorpej static int
    615  1.68  christos dcmclose(dev_t dev, int flag, int mode, struct lwp *l)
    616   1.1       cgd {
    617  1.20   thorpej 	int s, unit, board, port;
    618  1.20   thorpej 	struct dcm_softc *sc;
    619  1.20   thorpej 	struct tty *tp;
    620  1.63   tsutsui 
    621  1.20   thorpej 	unit = DCMUNIT(dev);
    622  1.20   thorpej 	board = DCMBOARD(unit);
    623  1.20   thorpej 	port = DCMPORT(unit);
    624  1.20   thorpej 
    625  1.34   thorpej 	sc = dcm_cd.cd_devs[board];
    626  1.20   thorpej 	tp = sc->sc_tty[port];
    627  1.20   thorpej 
    628  1.45       eeh 	(*tp->t_linesw->l_close)(tp, flag);
    629  1.18   thorpej 
    630  1.18   thorpej 	s = spltty();
    631  1.18   thorpej 
    632  1.44   thorpej 	if (tp->t_cflag & HUPCL || tp->t_wopen != 0 ||
    633  1.18   thorpej 	    (tp->t_state & TS_ISOPEN) == 0)
    634   1.1       cgd 		(void) dcmmctl(dev, MO_OFF, DMSET);
    635   1.1       cgd #ifdef DEBUG
    636   1.1       cgd 	if (dcmdebug & DDB_OPENCLOSE)
    637  1.31  christos 		printf("%s port %d: dcmclose: st %x fl %x\n",
    638  1.34   thorpej 			sc->sc_dev.dv_xname, port, tp->t_state, tp->t_flags);
    639   1.1       cgd #endif
    640  1.18   thorpej 	splx(s);
    641   1.1       cgd 	ttyclose(tp);
    642  1.12   mycroft #if 0
    643  1.28   thorpej 	tty_detach(tp);
    644   1.7   mycroft 	ttyfree(tp);
    645  1.20   thorpej 	sc->sc_tty[port] == NULL;
    646  1.12   mycroft #endif
    647  1.72   tsutsui 	return 0;
    648   1.1       cgd }
    649  1.63   tsutsui 
    650  1.65   thorpej static int
    651  1.65   thorpej dcmread(dev_t dev, struct uio *uio, int flag)
    652   1.1       cgd {
    653  1.20   thorpej 	int unit, board, port;
    654  1.20   thorpej 	struct dcm_softc *sc;
    655  1.36    scottr 	struct tty *tp;
    656  1.20   thorpej 
    657  1.20   thorpej 	unit = DCMUNIT(dev);
    658  1.20   thorpej 	board = DCMBOARD(unit);
    659  1.20   thorpej 	port = DCMPORT(unit);
    660  1.20   thorpej 
    661  1.34   thorpej 	sc = dcm_cd.cd_devs[board];
    662  1.20   thorpej 	tp = sc->sc_tty[port];
    663  1.14   mycroft 
    664  1.72   tsutsui 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    665   1.1       cgd }
    666  1.63   tsutsui 
    667  1.65   thorpej static int
    668  1.65   thorpej dcmwrite(dev_t dev, struct uio *uio, int flag)
    669   1.1       cgd {
    670  1.20   thorpej 	int unit, board, port;
    671  1.20   thorpej 	struct dcm_softc *sc;
    672  1.36    scottr 	struct tty *tp;
    673  1.20   thorpej 
    674  1.20   thorpej 	unit = DCMUNIT(dev);
    675  1.20   thorpej 	board = DCMBOARD(unit);
    676  1.20   thorpej 	port = DCMPORT(unit);
    677  1.20   thorpej 
    678  1.34   thorpej 	sc = dcm_cd.cd_devs[board];
    679  1.20   thorpej 	tp = sc->sc_tty[port];
    680  1.14   mycroft 
    681  1.72   tsutsui 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    682  1.46       scw }
    683  1.46       scw 
    684  1.65   thorpej static int
    685  1.68  christos dcmpoll(dev_t dev, int events, struct lwp *l)
    686  1.46       scw {
    687  1.46       scw 	int unit, board, port;
    688  1.46       scw 	struct dcm_softc *sc;
    689  1.46       scw 	struct tty *tp;
    690  1.46       scw 
    691  1.46       scw 	unit = DCMUNIT(dev);
    692  1.46       scw 	board = DCMBOARD(unit);
    693  1.46       scw 	port = DCMPORT(unit);
    694  1.46       scw 
    695  1.46       scw 	sc = dcm_cd.cd_devs[board];
    696  1.46       scw 	tp = sc->sc_tty[port];
    697  1.63   tsutsui 
    698  1.72   tsutsui 	return (*tp->t_linesw->l_poll)(tp, events, l);
    699   1.1       cgd }
    700  1.17   mycroft 
    701  1.65   thorpej static struct tty *
    702  1.65   thorpej dcmtty(dev_t dev)
    703  1.17   mycroft {
    704  1.20   thorpej 	int unit, board, port;
    705  1.20   thorpej 	struct dcm_softc *sc;
    706  1.17   mycroft 
    707  1.20   thorpej 	unit = DCMUNIT(dev);
    708  1.20   thorpej 	board = DCMBOARD(unit);
    709  1.20   thorpej 	port = DCMPORT(unit);
    710  1.20   thorpej 
    711  1.34   thorpej 	sc = dcm_cd.cd_devs[board];
    712  1.20   thorpej 
    713  1.72   tsutsui 	return sc->sc_tty[port];
    714  1.17   mycroft }
    715  1.63   tsutsui 
    716  1.65   thorpej static int
    717  1.65   thorpej dcmintr(void *arg)
    718   1.1       cgd {
    719  1.23   thorpej 	struct dcm_softc *sc = arg;
    720  1.20   thorpej 	struct dcmdevice *dcm = sc->sc_dcm;
    721  1.20   thorpej 	struct dcmischeme *dis = &sc->sc_scheme;
    722  1.69   thorpej 	int brd = device_unit(&sc->sc_dev);
    723  1.20   thorpej 	int code, i;
    724   1.1       cgd 	int pcnd[4], mcode, mcnd[4];
    725   1.1       cgd 
    726   1.1       cgd 	/*
    727  1.36    scottr 	 * Do all guarded accesses right off to minimize
    728   1.1       cgd 	 * block out of hardware.
    729   1.1       cgd 	 */
    730   1.1       cgd 	SEM_LOCK(dcm);
    731   1.1       cgd 	if ((dcm->dcm_ic & IC_IR) == 0) {
    732   1.1       cgd 		SEM_UNLOCK(dcm);
    733  1.72   tsutsui 		return 0;
    734   1.1       cgd 	}
    735   1.1       cgd 	for (i = 0; i < 4; i++) {
    736   1.1       cgd 		pcnd[i] = dcm->dcm_icrtab[i].dcm_data;
    737   1.1       cgd 		dcm->dcm_icrtab[i].dcm_data = 0;
    738  1.20   thorpej 		code = sc->sc_modem[i]->mdmin;
    739  1.20   thorpej 		if (sc->sc_flags & DCM_STDDCE)
    740   1.1       cgd 			code = hp2dce_in(code);
    741   1.1       cgd 		mcnd[i] = code;
    742   1.1       cgd 	}
    743   1.1       cgd 	code = dcm->dcm_iir & IIR_MASK;
    744   1.1       cgd 	dcm->dcm_iir = 0;	/* XXX doc claims read clears interrupt?! */
    745   1.1       cgd 	mcode = dcm->dcm_modemintr;
    746   1.1       cgd 	dcm->dcm_modemintr = 0;
    747   1.1       cgd 	SEM_UNLOCK(dcm);
    748   1.1       cgd 
    749   1.1       cgd #ifdef DEBUG
    750   1.1       cgd 	if (dcmdebug & DDB_INTR) {
    751  1.31  christos 		printf("%s: dcmintr: iir %x pc %x/%x/%x/%x ",
    752  1.34   thorpej 		       sc->sc_dev.dv_xname, code, pcnd[0], pcnd[1],
    753  1.63   tsutsui 		       pcnd[2], pcnd[3]);
    754  1.31  christos 		printf("miir %x mc %x/%x/%x/%x\n",
    755   1.1       cgd 		       mcode, mcnd[0], mcnd[1], mcnd[2], mcnd[3]);
    756   1.1       cgd 	}
    757   1.1       cgd #endif
    758   1.1       cgd 	if (code & IIR_TIMEO)
    759  1.20   thorpej 		dcmrint(sc);
    760   1.1       cgd 	if (code & IIR_PORT0)
    761  1.20   thorpej 		dcmpint(sc, 0, pcnd[0]);
    762   1.1       cgd 	if (code & IIR_PORT1)
    763  1.20   thorpej 		dcmpint(sc, 1, pcnd[1]);
    764   1.1       cgd 	if (code & IIR_PORT2)
    765  1.20   thorpej 		dcmpint(sc, 2, pcnd[2]);
    766   1.1       cgd 	if (code & IIR_PORT3)
    767  1.20   thorpej 		dcmpint(sc, 3, pcnd[3]);
    768   1.1       cgd 	if (code & IIR_MODM) {
    769   1.1       cgd 		if (mcode == 0 || mcode & 0x1)	/* mcode==0 -> 98642 board */
    770  1.20   thorpej 			dcmmint(sc, 0, mcnd[0]);
    771   1.1       cgd 		if (mcode & 0x2)
    772  1.20   thorpej 			dcmmint(sc, 1, mcnd[1]);
    773   1.1       cgd 		if (mcode & 0x4)
    774  1.20   thorpej 			dcmmint(sc, 2, mcnd[2]);
    775   1.1       cgd 		if (mcode & 0x8)
    776  1.20   thorpej 			dcmmint(sc, 3, mcnd[3]);
    777   1.1       cgd 	}
    778   1.1       cgd 
    779   1.1       cgd 	/*
    780   1.1       cgd 	 * Chalk up a receiver interrupt if the timer running or one of
    781   1.1       cgd 	 * the ports reports a special character interrupt.
    782   1.1       cgd 	 */
    783   1.1       cgd 	if ((code & IIR_TIMEO) ||
    784   1.1       cgd 	    ((pcnd[0]|pcnd[1]|pcnd[2]|pcnd[3]) & IT_SPEC))
    785   1.1       cgd 		dis->dis_intr++;
    786   1.1       cgd 	/*
    787   1.1       cgd 	 * See if it is time to check/change the interrupt rate.
    788   1.1       cgd 	 */
    789   1.1       cgd 	if (dcmistype < 0 &&
    790  1.74   tsutsui 	    (i = time_second - dis->dis_time) >= dcminterval) {
    791   1.1       cgd 		/*
    792   1.1       cgd 		 * If currently per-character and averaged over 70 interrupts
    793   1.1       cgd 		 * per-second (66 is threshold of 600 baud) in last interval,
    794   1.1       cgd 		 * switch to timer mode.
    795   1.1       cgd 		 *
    796   1.1       cgd 		 * XXX decay counts ala load average to avoid spikes?
    797   1.1       cgd 		 */
    798   1.1       cgd 		if (dis->dis_perchar && dis->dis_intr > 70 * i)
    799   1.1       cgd 			dcmsetischeme(brd, DIS_TIMER);
    800   1.1       cgd 		/*
    801   1.1       cgd 		 * If currently using timer and had more interrupts than
    802   1.1       cgd 		 * received characters in the last interval, switch back
    803   1.1       cgd 		 * to per-character.  Note that after changing to per-char
    804   1.1       cgd 		 * we must process any characters already in the queue
    805   1.1       cgd 		 * since they may have arrived before the bitmap was setup.
    806   1.1       cgd 		 *
    807   1.1       cgd 		 * XXX decay counts?
    808   1.1       cgd 		 */
    809   1.1       cgd 		else if (!dis->dis_perchar && dis->dis_intr > dis->dis_char) {
    810   1.1       cgd 			dcmsetischeme(brd, DIS_PERCHAR);
    811  1.20   thorpej 			dcmrint(sc);
    812   1.1       cgd 		}
    813   1.1       cgd 		dis->dis_intr = dis->dis_char = 0;
    814  1.74   tsutsui 		dis->dis_time = time_second;
    815   1.1       cgd 	}
    816  1.72   tsutsui 	return 1;
    817   1.1       cgd }
    818   1.1       cgd 
    819   1.1       cgd /*
    820   1.1       cgd  *  Port interrupt.  Can be two things:
    821   1.1       cgd  *	First, it might be a special character (exception interrupt);
    822   1.1       cgd  *	Second, it may be a buffer empty (transmit interrupt);
    823   1.1       cgd  */
    824  1.65   thorpej static void
    825  1.65   thorpej dcmpint(struct dcm_softc *sc, int port, int code)
    826   1.1       cgd {
    827   1.1       cgd 
    828   1.1       cgd 	if (code & IT_SPEC)
    829  1.20   thorpej 		dcmreadbuf(sc, port);
    830   1.1       cgd 	if (code & IT_TX)
    831  1.20   thorpej 		dcmxint(sc, port);
    832   1.1       cgd }
    833   1.1       cgd 
    834  1.65   thorpej static void
    835  1.65   thorpej dcmrint(struct dcm_softc *sc)
    836   1.1       cgd {
    837  1.20   thorpej 	int port;
    838   1.1       cgd 
    839  1.20   thorpej 	for (port = 0; port < NDCMPORT; port++)
    840  1.20   thorpej 		dcmreadbuf(sc, port);
    841   1.1       cgd }
    842   1.1       cgd 
    843  1.65   thorpej static void
    844  1.65   thorpej dcmreadbuf(struct dcm_softc *sc, int port)
    845   1.1       cgd {
    846  1.20   thorpej 	struct dcmdevice *dcm = sc->sc_dcm;
    847  1.20   thorpej 	struct dcmpreg *pp = dcm_preg(dcm, port);
    848  1.20   thorpej 	struct dcmrfifo *fifo;
    849  1.38    scottr 	struct tty *tp;
    850  1.20   thorpej 	int c, stat;
    851  1.20   thorpej 	u_int head;
    852   1.1       cgd 	int nch = 0;
    853  1.14   mycroft #ifdef DCMSTATS
    854  1.20   thorpej 	struct dcmstats *dsp = &sc->sc_stats;
    855   1.1       cgd 
    856   1.1       cgd 	dsp->rints++;
    857   1.1       cgd #endif
    858  1.38    scottr 	tp = sc->sc_tty[port];
    859  1.40    scottr 	if (tp == NULL)
    860  1.38    scottr 		return;
    861  1.38    scottr 
    862   1.1       cgd 	if ((tp->t_state & TS_ISOPEN) == 0) {
    863   1.1       cgd #ifdef KGDB
    864  1.52   gehenna 		int maj;
    865  1.52   gehenna 
    866  1.52   gehenna 		maj = cdevsw_lookup_major(&dcm_cdevsw);
    867  1.52   gehenna 
    868  1.52   gehenna 		if ((makedev(maj, minor(tp->t_dev)) == kgdb_dev) &&
    869   1.1       cgd 		    (head = pp->r_head & RX_MASK) != (pp->r_tail & RX_MASK) &&
    870  1.14   mycroft 		    dcm->dcm_rfifos[3-port][head>>1].data_char == FRAME_START) {
    871   1.1       cgd 			pp->r_head = (head + 2) & RX_MASK;
    872   1.1       cgd 			kgdb_connect(0);	/* trap into kgdb */
    873   1.1       cgd 			return;
    874   1.1       cgd 		}
    875   1.1       cgd #endif /* KGDB */
    876   1.1       cgd 		pp->r_head = pp->r_tail & RX_MASK;
    877   1.1       cgd 		return;
    878   1.1       cgd 	}
    879   1.1       cgd 
    880   1.1       cgd 	head = pp->r_head & RX_MASK;
    881   1.1       cgd 	fifo = &dcm->dcm_rfifos[3-port][head>>1];
    882   1.1       cgd 	/*
    883   1.1       cgd 	 * XXX upper bound on how many chars we will take in one swallow?
    884   1.1       cgd 	 */
    885   1.1       cgd 	while (head != (pp->r_tail & RX_MASK)) {
    886   1.1       cgd 		/*
    887   1.1       cgd 		 * Get character/status and update head pointer as fast
    888   1.1       cgd 		 * as possible to make room for more characters.
    889   1.1       cgd 		 */
    890   1.1       cgd 		c = fifo->data_char;
    891   1.1       cgd 		stat = fifo->data_stat;
    892   1.1       cgd 		head = (head + 2) & RX_MASK;
    893   1.1       cgd 		pp->r_head = head;
    894   1.1       cgd 		fifo = head ? fifo+1 : &dcm->dcm_rfifos[3-port][0];
    895   1.1       cgd 		nch++;
    896   1.1       cgd 
    897   1.1       cgd #ifdef DEBUG
    898   1.1       cgd 		if (dcmdebug & DDB_INPUT)
    899  1.31  christos 			printf("%s port %d: dcmreadbuf: c%x('%c') s%x f%x h%x t%x\n",
    900  1.34   thorpej 			       sc->sc_dev.dv_xname, port,
    901  1.20   thorpej 			       c&0xFF, c, stat&0xFF,
    902   1.1       cgd 			       tp->t_flags, head, pp->r_tail);
    903   1.1       cgd #endif
    904   1.1       cgd 		/*
    905   1.1       cgd 		 * Check for and handle errors
    906   1.1       cgd 		 */
    907   1.1       cgd 		if (stat & RD_MASK) {
    908   1.1       cgd #ifdef DEBUG
    909   1.1       cgd 			if (dcmdebug & (DDB_INPUT|DDB_SIOERR))
    910  1.31  christos 				printf("%s port %d: dcmreadbuf: err: c%x('%c') s%x\n",
    911  1.34   thorpej 				       sc->sc_dev.dv_xname, port,
    912  1.20   thorpej 				       stat, c&0xFF, c);
    913   1.1       cgd #endif
    914   1.1       cgd 			if (stat & (RD_BD | RD_FE))
    915   1.1       cgd 				c |= TTY_FE;
    916   1.1       cgd 			else if (stat & RD_PE)
    917   1.1       cgd 				c |= TTY_PE;
    918   1.1       cgd 			else if (stat & RD_OVF)
    919   1.1       cgd 				log(LOG_WARNING,
    920  1.20   thorpej 				    "%s port %d: silo overflow\n",
    921  1.34   thorpej 				    sc->sc_dev.dv_xname, port);
    922   1.1       cgd 			else if (stat & RD_OE)
    923   1.1       cgd 				log(LOG_WARNING,
    924  1.20   thorpej 				    "%s port %d: uart overflow\n",
    925  1.34   thorpej 				    sc->sc_dev.dv_xname, port);
    926   1.1       cgd 		}
    927  1.45       eeh 		(*tp->t_linesw->l_rint)(c, tp);
    928   1.1       cgd 	}
    929  1.20   thorpej 	sc->sc_scheme.dis_char += nch;
    930  1.20   thorpej 
    931  1.14   mycroft #ifdef DCMSTATS
    932   1.1       cgd 	dsp->rchars += nch;
    933   1.1       cgd 	if (nch <= DCMRBSIZE)
    934   1.1       cgd 		dsp->rsilo[nch]++;
    935   1.1       cgd 	else
    936   1.1       cgd 		dsp->rsilo[DCMRBSIZE+1]++;
    937   1.1       cgd #endif
    938   1.1       cgd }
    939   1.1       cgd 
    940  1.65   thorpej static void
    941  1.65   thorpej dcmxint(struct dcm_softc *sc, int port)
    942   1.1       cgd {
    943  1.38    scottr 	struct tty *tp;
    944  1.38    scottr 
    945  1.38    scottr 	tp = sc->sc_tty[port];
    946  1.38    scottr 	if (tp == NULL || (tp->t_state & TS_ISOPEN) == 0)
    947  1.38    scottr 		return;
    948  1.20   thorpej 
    949   1.1       cgd 	tp->t_state &= ~TS_BUSY;
    950   1.1       cgd 	if (tp->t_state & TS_FLUSH)
    951   1.1       cgd 		tp->t_state &= ~TS_FLUSH;
    952  1.45       eeh 	(*tp->t_linesw->l_start)(tp);
    953   1.1       cgd }
    954   1.1       cgd 
    955  1.65   thorpej static void
    956  1.65   thorpej dcmmint(struct dcm_softc *sc, int port, int mcnd)
    957   1.1       cgd {
    958   1.1       cgd 	int delta;
    959  1.20   thorpej 	struct tty *tp;
    960  1.20   thorpej 	struct dcmdevice *dcm = sc->sc_dcm;
    961  1.20   thorpej 
    962   1.1       cgd #ifdef DEBUG
    963   1.1       cgd 	if (dcmdebug & DDB_MODEM)
    964  1.31  christos 		printf("%s port %d: dcmmint: mcnd %x mcndlast %x\n",
    965  1.34   thorpej 		       sc->sc_dev.dv_xname, port, mcnd, sc->sc_mcndlast[port]);
    966   1.1       cgd #endif
    967  1.20   thorpej 	delta = mcnd ^ sc->sc_mcndlast[port];
    968  1.20   thorpej 	sc->sc_mcndlast[port] = mcnd;
    969  1.58  gmcgarry 	tp = sc->sc_tty[port];
    970  1.58  gmcgarry 	if (tp == NULL || (tp->t_state & TS_ISOPEN) == 0)
    971  1.58  gmcgarry 		return;
    972  1.58  gmcgarry 
    973   1.1       cgd 	if ((delta & MI_CTS) && (tp->t_state & TS_ISOPEN) &&
    974  1.55  gmcgarry 	    (tp->t_cflag & CCTS_OFLOW)) {
    975   1.1       cgd 		if (mcnd & MI_CTS) {
    976   1.1       cgd 			tp->t_state &= ~TS_TTSTOP;
    977   1.1       cgd 			ttstart(tp);
    978   1.1       cgd 		} else
    979   1.1       cgd 			tp->t_state |= TS_TTSTOP;	/* inline dcmstop */
    980   1.1       cgd 	}
    981   1.1       cgd 	if (delta & MI_CD) {
    982   1.1       cgd 		if (mcnd & MI_CD)
    983  1.45       eeh 			(void)(*tp->t_linesw->l_modem)(tp, 1);
    984  1.20   thorpej 		else if ((sc->sc_softCAR & (1 << port)) == 0 &&
    985  1.45       eeh 		    (*tp->t_linesw->l_modem)(tp, 0) == 0) {
    986  1.20   thorpej 			sc->sc_modem[port]->mdmout = MO_OFF;
    987   1.1       cgd 			SEM_LOCK(dcm);
    988  1.20   thorpej 			dcm->dcm_modemchng |= (1 << port);
    989   1.1       cgd 			dcm->dcm_cr |= CR_MODM;
    990   1.1       cgd 			SEM_UNLOCK(dcm);
    991   1.1       cgd 			DELAY(10); /* time to change lines */
    992   1.1       cgd 		}
    993   1.1       cgd 	}
    994   1.1       cgd }
    995   1.1       cgd 
    996  1.65   thorpej static int
    997  1.68  christos dcmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    998   1.1       cgd {
    999  1.20   thorpej 	struct dcm_softc *sc;
   1000  1.20   thorpej 	struct tty *tp;
   1001  1.20   thorpej 	struct dcmdevice *dcm;
   1002  1.20   thorpej 	int board, port, unit = DCMUNIT(dev);
   1003   1.1       cgd 	int error, s;
   1004  1.20   thorpej 
   1005  1.20   thorpej 	port = DCMPORT(unit);
   1006  1.20   thorpej 	board = DCMBOARD(unit);
   1007  1.20   thorpej 
   1008  1.34   thorpej 	sc = dcm_cd.cd_devs[board];
   1009  1.20   thorpej 	dcm = sc->sc_dcm;
   1010  1.20   thorpej 	tp = sc->sc_tty[port];
   1011  1.63   tsutsui 
   1012   1.1       cgd #ifdef DEBUG
   1013   1.1       cgd 	if (dcmdebug & DDB_IOCTL)
   1014  1.37    scottr 		printf("%s port %d: dcmioctl: cmd %lx data %x flag %x\n",
   1015  1.34   thorpej 		       sc->sc_dev.dv_xname, port, cmd, *data, flag);
   1016   1.1       cgd #endif
   1017  1.51    atatat 
   1018  1.68  christos 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
   1019  1.51    atatat 	if (error != EPASSTHROUGH)
   1020  1.72   tsutsui 		return error;
   1021  1.51    atatat 
   1022  1.68  christos 	error = ttioctl(tp, cmd, data, flag, l);
   1023  1.51    atatat 	if (error != EPASSTHROUGH)
   1024  1.72   tsutsui 		return error;
   1025   1.1       cgd 
   1026   1.1       cgd 	switch (cmd) {
   1027   1.1       cgd 	case TIOCSBRK:
   1028   1.1       cgd 		/*
   1029   1.1       cgd 		 * Wait for transmitter buffer to empty
   1030   1.1       cgd 		 */
   1031   1.1       cgd 		s = spltty();
   1032   1.1       cgd 		while (dcm->dcm_thead[port].ptr != dcm->dcm_ttail[port].ptr)
   1033   1.1       cgd 			DELAY(DCM_USPERCH(tp->t_ospeed));
   1034   1.1       cgd 		SEM_LOCK(dcm);
   1035   1.1       cgd 		dcm->dcm_cmdtab[port].dcm_data |= CT_BRK;
   1036   1.1       cgd 		dcm->dcm_cr |= (1 << port);	/* start break */
   1037   1.1       cgd 		SEM_UNLOCK(dcm);
   1038   1.1       cgd 		splx(s);
   1039   1.1       cgd 		break;
   1040   1.1       cgd 
   1041   1.1       cgd 	case TIOCCBRK:
   1042   1.1       cgd 		SEM_LOCK(dcm);
   1043   1.1       cgd 		dcm->dcm_cmdtab[port].dcm_data |= CT_BRK;
   1044   1.1       cgd 		dcm->dcm_cr |= (1 << port);	/* end break */
   1045   1.1       cgd 		SEM_UNLOCK(dcm);
   1046   1.1       cgd 		break;
   1047   1.1       cgd 
   1048   1.1       cgd 	case TIOCSDTR:
   1049   1.1       cgd 		(void) dcmmctl(dev, MO_ON, DMBIS);
   1050   1.1       cgd 		break;
   1051   1.1       cgd 
   1052   1.1       cgd 	case TIOCCDTR:
   1053   1.1       cgd 		(void) dcmmctl(dev, MO_ON, DMBIC);
   1054   1.1       cgd 		break;
   1055   1.1       cgd 
   1056   1.1       cgd 	case TIOCMSET:
   1057   1.1       cgd 		(void) dcmmctl(dev, *(int *)data, DMSET);
   1058   1.1       cgd 		break;
   1059   1.1       cgd 
   1060   1.1       cgd 	case TIOCMBIS:
   1061   1.1       cgd 		(void) dcmmctl(dev, *(int *)data, DMBIS);
   1062   1.1       cgd 		break;
   1063   1.1       cgd 
   1064   1.1       cgd 	case TIOCMBIC:
   1065   1.1       cgd 		(void) dcmmctl(dev, *(int *)data, DMBIC);
   1066   1.1       cgd 		break;
   1067   1.1       cgd 
   1068   1.1       cgd 	case TIOCMGET:
   1069   1.1       cgd 		*(int *)data = dcmmctl(dev, 0, DMGET);
   1070   1.1       cgd 		break;
   1071  1.18   thorpej 
   1072  1.18   thorpej 	case TIOCGFLAGS: {
   1073  1.18   thorpej 		int bits = 0;
   1074  1.18   thorpej 
   1075  1.20   thorpej 		if ((sc->sc_softCAR & (1 << port)))
   1076  1.18   thorpej 			bits |= TIOCFLAG_SOFTCAR;
   1077  1.18   thorpej 
   1078  1.18   thorpej 		if (tp->t_cflag & CLOCAL)
   1079  1.18   thorpej 			bits |= TIOCFLAG_CLOCAL;
   1080  1.18   thorpej 
   1081  1.18   thorpej 		*(int *)data = bits;
   1082  1.18   thorpej 		break;
   1083  1.18   thorpej 	}
   1084  1.18   thorpej 
   1085  1.18   thorpej 	case TIOCSFLAGS: {
   1086  1.18   thorpej 		int userbits;
   1087  1.18   thorpej 
   1088  1.75      elad 		if (kauth_authorize_device_tty(l->l_cred,
   1089  1.75      elad 		    KAUTH_DEVICE_TTY_PRIVSET, tp))
   1090  1.75      elad 			return (EPERM);
   1091  1.18   thorpej 
   1092  1.18   thorpej 		userbits = *(int *)data;
   1093  1.18   thorpej 
   1094  1.18   thorpej 		if ((userbits & TIOCFLAG_SOFTCAR) ||
   1095  1.24   thorpej 		    ((sc->sc_flags & DCM_ISCONSOLE) &&
   1096  1.24   thorpej 		    (port == DCMCONSPORT)))
   1097  1.20   thorpej 			sc->sc_softCAR |= (1 << port);
   1098  1.18   thorpej 
   1099  1.18   thorpej 		if (userbits & TIOCFLAG_CLOCAL)
   1100  1.18   thorpej 			tp->t_cflag |= CLOCAL;
   1101  1.18   thorpej 
   1102  1.18   thorpej 		break;
   1103  1.18   thorpej 	}
   1104   1.1       cgd 
   1105   1.1       cgd 	default:
   1106  1.72   tsutsui 		return EPASSTHROUGH;
   1107   1.1       cgd 	}
   1108  1.72   tsutsui 	return 0;
   1109   1.1       cgd }
   1110   1.1       cgd 
   1111  1.65   thorpej static int
   1112  1.65   thorpej dcmparam(struct tty *tp, struct termios *t)
   1113   1.1       cgd {
   1114  1.20   thorpej 	struct dcm_softc *sc;
   1115  1.20   thorpej 	struct dcmdevice *dcm;
   1116  1.20   thorpej 	int unit, board, port, mode, cflag = t->c_cflag;
   1117   1.1       cgd 	int ospeed = ttspeedtab(t->c_ospeed, dcmspeedtab);
   1118   1.1       cgd 
   1119  1.20   thorpej 	unit = DCMUNIT(tp->t_dev);
   1120  1.20   thorpej 	board = DCMBOARD(unit);
   1121  1.20   thorpej 	port = DCMPORT(unit);
   1122  1.20   thorpej 
   1123  1.34   thorpej 	sc = dcm_cd.cd_devs[board];
   1124  1.20   thorpej 	dcm = sc->sc_dcm;
   1125  1.20   thorpej 
   1126   1.1       cgd 	/* check requested parameters */
   1127  1.59  gmcgarry 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
   1128  1.72   tsutsui 		return EINVAL;
   1129  1.59  gmcgarry 	/* and copy to tty */
   1130  1.59  gmcgarry 	tp->t_ispeed = t->c_ispeed;
   1131  1.59  gmcgarry 	tp->t_ospeed = t->c_ospeed;
   1132  1.59  gmcgarry 	tp->t_cflag = cflag;
   1133   1.1       cgd 	if (ospeed == 0) {
   1134  1.72   tsutsui 		(void)dcmmctl(DCMUNIT(tp->t_dev), MO_OFF, DMSET);
   1135  1.72   tsutsui 		return 0;
   1136   1.1       cgd 	}
   1137   1.1       cgd 
   1138   1.1       cgd 	mode = 0;
   1139   1.1       cgd 	switch (cflag&CSIZE) {
   1140   1.1       cgd 	case CS5:
   1141   1.1       cgd 		mode = LC_5BITS; break;
   1142   1.1       cgd 	case CS6:
   1143   1.1       cgd 		mode = LC_6BITS; break;
   1144   1.1       cgd 	case CS7:
   1145   1.1       cgd 		mode = LC_7BITS; break;
   1146   1.1       cgd 	case CS8:
   1147   1.1       cgd 		mode = LC_8BITS; break;
   1148   1.1       cgd 	}
   1149   1.1       cgd 	if (cflag&PARENB) {
   1150   1.1       cgd 		if (cflag&PARODD)
   1151   1.1       cgd 			mode |= LC_PODD;
   1152   1.1       cgd 		else
   1153   1.1       cgd 			mode |= LC_PEVEN;
   1154   1.1       cgd 	}
   1155   1.1       cgd 	if (cflag&CSTOPB)
   1156   1.1       cgd 		mode |= LC_2STOP;
   1157   1.1       cgd 	else
   1158   1.1       cgd 		mode |= LC_1STOP;
   1159   1.1       cgd #ifdef DEBUG
   1160   1.1       cgd 	if (dcmdebug & DDB_PARAM)
   1161  1.31  christos 		printf("%s port %d: dcmparam: cflag %x mode %x speed %d uperch %d\n",
   1162  1.34   thorpej 		       sc->sc_dev.dv_xname, port, cflag, mode, tp->t_ospeed,
   1163   1.1       cgd 		       DCM_USPERCH(tp->t_ospeed));
   1164   1.1       cgd #endif
   1165   1.1       cgd 
   1166   1.1       cgd 	/*
   1167   1.1       cgd 	 * Wait for transmitter buffer to empty.
   1168   1.1       cgd 	 */
   1169   1.1       cgd 	while (dcm->dcm_thead[port].ptr != dcm->dcm_ttail[port].ptr)
   1170   1.1       cgd 		DELAY(DCM_USPERCH(tp->t_ospeed));
   1171   1.1       cgd 	/*
   1172   1.1       cgd 	 * Make changes known to hardware.
   1173   1.1       cgd 	 */
   1174   1.1       cgd 	dcm->dcm_data[port].dcm_baud = ospeed;
   1175   1.1       cgd 	dcm->dcm_data[port].dcm_conf = mode;
   1176   1.1       cgd 	SEM_LOCK(dcm);
   1177   1.1       cgd 	dcm->dcm_cmdtab[port].dcm_data |= CT_CON;
   1178   1.1       cgd 	dcm->dcm_cr |= (1 << port);
   1179   1.1       cgd 	SEM_UNLOCK(dcm);
   1180   1.1       cgd 	/*
   1181   1.1       cgd 	 * Delay for config change to take place. Weighted by baud.
   1182   1.1       cgd 	 * XXX why do we do this?
   1183   1.1       cgd 	 */
   1184   1.1       cgd 	DELAY(16 * DCM_USPERCH(tp->t_ospeed));
   1185  1.72   tsutsui 	return 0;
   1186   1.1       cgd }
   1187  1.63   tsutsui 
   1188  1.65   thorpej static void
   1189  1.65   thorpej dcmstart(struct tty *tp)
   1190   1.1       cgd {
   1191  1.20   thorpej 	struct dcm_softc *sc;
   1192  1.20   thorpej 	struct dcmdevice *dcm;
   1193  1.20   thorpej 	struct dcmpreg *pp;
   1194  1.20   thorpej 	struct dcmtfifo *fifo;
   1195  1.20   thorpej 	char *bp;
   1196  1.20   thorpej 	u_int head, tail, next;
   1197  1.20   thorpej 	int unit, board, port, nch;
   1198   1.1       cgd 	char buf[16];
   1199   1.1       cgd 	int s;
   1200  1.14   mycroft #ifdef DCMSTATS
   1201  1.20   thorpej 	struct dcmstats *dsp = &sc->sc_stats;
   1202   1.1       cgd 	int tch = 0;
   1203   1.1       cgd #endif
   1204   1.1       cgd 
   1205  1.20   thorpej 	unit = DCMUNIT(tp->t_dev);
   1206  1.20   thorpej 	board = DCMBOARD(unit);
   1207  1.20   thorpej 	port = DCMPORT(unit);
   1208  1.20   thorpej 
   1209  1.34   thorpej 	sc = dcm_cd.cd_devs[board];
   1210  1.20   thorpej 	dcm = sc->sc_dcm;
   1211  1.20   thorpej 
   1212   1.1       cgd 	s = spltty();
   1213  1.14   mycroft #ifdef DCMSTATS
   1214   1.1       cgd 	dsp->xints++;
   1215   1.1       cgd #endif
   1216   1.1       cgd #ifdef DEBUG
   1217   1.1       cgd 	if (dcmdebug & DDB_OUTPUT)
   1218  1.31  christos 		printf("%s port %d: dcmstart: state %x flags %x outcc %d\n",
   1219  1.34   thorpej 		       sc->sc_dev.dv_xname, port, tp->t_state, tp->t_flags,
   1220   1.7   mycroft 		       tp->t_outq.c_cc);
   1221   1.1       cgd #endif
   1222   1.1       cgd 	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
   1223   1.1       cgd 		goto out;
   1224   1.7   mycroft 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1225   1.1       cgd 		if (tp->t_state&TS_ASLEEP) {
   1226   1.1       cgd 			tp->t_state &= ~TS_ASLEEP;
   1227   1.7   mycroft 			wakeup((caddr_t)&tp->t_outq);
   1228   1.1       cgd 		}
   1229   1.2       cgd 		selwakeup(&tp->t_wsel);
   1230   1.1       cgd 	}
   1231   1.7   mycroft 	if (tp->t_outq.c_cc == 0) {
   1232  1.14   mycroft #ifdef DCMSTATS
   1233   1.1       cgd 		dsp->xempty++;
   1234   1.1       cgd #endif
   1235   1.1       cgd 		goto out;
   1236   1.1       cgd 	}
   1237   1.1       cgd 
   1238   1.1       cgd 	pp = dcm_preg(dcm, port);
   1239   1.1       cgd 	tail = pp->t_tail & TX_MASK;
   1240   1.1       cgd 	next = (tail + 1) & TX_MASK;
   1241   1.1       cgd 	head = pp->t_head & TX_MASK;
   1242   1.1       cgd 	if (head == next)
   1243   1.1       cgd 		goto out;
   1244   1.1       cgd 	fifo = &dcm->dcm_tfifos[3-port][tail];
   1245   1.1       cgd again:
   1246   1.1       cgd 	nch = q_to_b(&tp->t_outq, buf, (head - next) & TX_MASK);
   1247  1.14   mycroft #ifdef DCMSTATS
   1248   1.1       cgd 	tch += nch;
   1249   1.1       cgd #endif
   1250   1.1       cgd #ifdef DEBUG
   1251   1.1       cgd 	if (dcmdebug & DDB_OUTPUT)
   1252  1.31  christos 		printf("\thead %x tail %x nch %d\n", head, tail, nch);
   1253   1.1       cgd #endif
   1254   1.1       cgd 	/*
   1255   1.1       cgd 	 * Loop transmitting all the characters we can.
   1256   1.1       cgd 	 */
   1257   1.1       cgd 	for (bp = buf; --nch >= 0; bp++) {
   1258   1.1       cgd 		fifo->data_char = *bp;
   1259   1.1       cgd 		pp->t_tail = next;
   1260   1.1       cgd 		/*
   1261   1.1       cgd 		 * If this is the first character,
   1262   1.1       cgd 		 * get the hardware moving right now.
   1263   1.1       cgd 		 */
   1264   1.1       cgd 		if (bp == buf) {
   1265   1.1       cgd 			tp->t_state |= TS_BUSY;
   1266   1.1       cgd 			SEM_LOCK(dcm);
   1267   1.1       cgd 			dcm->dcm_cmdtab[port].dcm_data |= CT_TX;
   1268   1.1       cgd 			dcm->dcm_cr |= (1 << port);
   1269   1.1       cgd 			SEM_UNLOCK(dcm);
   1270   1.1       cgd 		}
   1271   1.1       cgd 		tail = next;
   1272   1.1       cgd 		fifo = tail ? fifo+1 : &dcm->dcm_tfifos[3-port][0];
   1273   1.1       cgd 		next = (next + 1) & TX_MASK;
   1274   1.1       cgd 	}
   1275   1.1       cgd 	/*
   1276   1.1       cgd 	 * Head changed while we were loading the buffer,
   1277   1.1       cgd 	 * go back and load some more if we can.
   1278   1.1       cgd 	 */
   1279   1.7   mycroft 	if (tp->t_outq.c_cc && head != (pp->t_head & TX_MASK)) {
   1280  1.14   mycroft #ifdef DCMSTATS
   1281   1.1       cgd 		dsp->xrestarts++;
   1282   1.1       cgd #endif
   1283   1.1       cgd 		head = pp->t_head & TX_MASK;
   1284   1.1       cgd 		goto again;
   1285   1.1       cgd 	}
   1286   1.1       cgd 
   1287   1.1       cgd 	/*
   1288   1.1       cgd 	 * Kick it one last time in case it finished while we were
   1289   1.1       cgd 	 * loading the last bunch.
   1290   1.1       cgd 	 */
   1291   1.1       cgd 	if (bp > &buf[1]) {
   1292   1.1       cgd 		tp->t_state |= TS_BUSY;
   1293   1.1       cgd 		SEM_LOCK(dcm);
   1294   1.1       cgd 		dcm->dcm_cmdtab[port].dcm_data |= CT_TX;
   1295   1.1       cgd 		dcm->dcm_cr |= (1 << port);
   1296   1.1       cgd 		SEM_UNLOCK(dcm);
   1297   1.1       cgd 	}
   1298   1.1       cgd #ifdef DEBUG
   1299   1.1       cgd 	if (dcmdebug & DDB_INTR)
   1300  1.37    scottr 		printf("%s port %d: dcmstart: head %x tail %x outqcc %d\n",
   1301  1.34   thorpej 		    sc->sc_dev.dv_xname, port, head, tail, tp->t_outq.c_cc);
   1302   1.1       cgd #endif
   1303   1.1       cgd out:
   1304  1.14   mycroft #ifdef DCMSTATS
   1305   1.1       cgd 	dsp->xchars += tch;
   1306   1.1       cgd 	if (tch <= DCMXBSIZE)
   1307   1.1       cgd 		dsp->xsilo[tch]++;
   1308   1.1       cgd 	else
   1309   1.1       cgd 		dsp->xsilo[DCMXBSIZE+1]++;
   1310   1.1       cgd #endif
   1311   1.1       cgd 	splx(s);
   1312   1.1       cgd }
   1313  1.63   tsutsui 
   1314   1.1       cgd /*
   1315   1.1       cgd  * Stop output on a line.
   1316   1.1       cgd  */
   1317  1.65   thorpej static void
   1318  1.65   thorpej dcmstop(struct tty *tp, int flag)
   1319   1.1       cgd {
   1320   1.1       cgd 	int s;
   1321   1.1       cgd 
   1322   1.1       cgd 	s = spltty();
   1323   1.1       cgd 	if (tp->t_state & TS_BUSY) {
   1324   1.1       cgd 		/* XXX is there some way to safely stop transmission? */
   1325   1.1       cgd 		if ((tp->t_state&TS_TTSTOP) == 0)
   1326   1.1       cgd 			tp->t_state |= TS_FLUSH;
   1327   1.1       cgd 	}
   1328   1.1       cgd 	splx(s);
   1329   1.1       cgd }
   1330  1.63   tsutsui 
   1331   1.1       cgd /*
   1332   1.1       cgd  * Modem control
   1333   1.1       cgd  */
   1334  1.36    scottr int
   1335  1.65   thorpej dcmmctl(dev_t dev, int bits, int how)
   1336   1.1       cgd {
   1337  1.20   thorpej 	struct dcm_softc *sc;
   1338  1.20   thorpej 	struct dcmdevice *dcm;
   1339  1.20   thorpej 	int s, unit, brd, port, hit = 0;
   1340  1.20   thorpej 
   1341  1.20   thorpej 	unit = DCMUNIT(dev);
   1342  1.20   thorpej 	brd = DCMBOARD(unit);
   1343  1.20   thorpej 	port = DCMPORT(unit);
   1344  1.34   thorpej 
   1345  1.34   thorpej 	sc = dcm_cd.cd_devs[brd];
   1346  1.20   thorpej 	dcm = sc->sc_dcm;
   1347   1.1       cgd 
   1348   1.1       cgd #ifdef DEBUG
   1349   1.1       cgd 	if (dcmdebug & DDB_MODEM)
   1350  1.31  christos 		printf("%s port %d: dcmmctl: bits 0x%x how %x\n",
   1351  1.34   thorpej 		       sc->sc_dev.dv_xname, port, bits, how);
   1352   1.1       cgd #endif
   1353   1.1       cgd 
   1354   1.1       cgd 	s = spltty();
   1355  1.20   thorpej 
   1356   1.1       cgd 	switch (how) {
   1357   1.1       cgd 	case DMSET:
   1358  1.20   thorpej 		sc->sc_modem[port]->mdmout = bits;
   1359   1.1       cgd 		hit++;
   1360   1.1       cgd 		break;
   1361   1.1       cgd 
   1362   1.1       cgd 	case DMBIS:
   1363  1.20   thorpej 		sc->sc_modem[port]->mdmout |= bits;
   1364   1.1       cgd 		hit++;
   1365   1.1       cgd 		break;
   1366   1.1       cgd 
   1367   1.1       cgd 	case DMBIC:
   1368  1.20   thorpej 		sc->sc_modem[port]->mdmout &= ~bits;
   1369   1.1       cgd 		hit++;
   1370   1.1       cgd 		break;
   1371   1.1       cgd 
   1372   1.1       cgd 	case DMGET:
   1373  1.20   thorpej 		bits = sc->sc_modem[port]->mdmin;
   1374  1.20   thorpej 		if (sc->sc_flags & DCM_STDDCE)
   1375   1.1       cgd 			bits = hp2dce_in(bits);
   1376   1.1       cgd 		break;
   1377   1.1       cgd 	}
   1378   1.1       cgd 	if (hit) {
   1379   1.1       cgd 		SEM_LOCK(dcm);
   1380   1.1       cgd 		dcm->dcm_modemchng |= 1<<(unit & 3);
   1381   1.1       cgd 		dcm->dcm_cr |= CR_MODM;
   1382   1.1       cgd 		SEM_UNLOCK(dcm);
   1383   1.1       cgd 		DELAY(10); /* delay until done */
   1384  1.49  gmcgarry 		splx(s);
   1385   1.1       cgd 	}
   1386  1.72   tsutsui 	return bits;
   1387   1.1       cgd }
   1388   1.1       cgd 
   1389   1.1       cgd /*
   1390   1.1       cgd  * Set board to either interrupt per-character or at a fixed interval.
   1391   1.1       cgd  */
   1392  1.65   thorpej static void
   1393  1.65   thorpej dcmsetischeme(int brd, int flags)
   1394   1.1       cgd {
   1395  1.34   thorpej 	struct dcm_softc *sc = dcm_cd.cd_devs[brd];
   1396  1.20   thorpej 	struct dcmdevice *dcm = sc->sc_dcm;
   1397  1.20   thorpej 	struct dcmischeme *dis = &sc->sc_scheme;
   1398  1.20   thorpej 	int i;
   1399   1.1       cgd 	u_char mask;
   1400   1.1       cgd 	int perchar = flags & DIS_PERCHAR;
   1401   1.1       cgd 
   1402   1.1       cgd #ifdef DEBUG
   1403   1.1       cgd 	if (dcmdebug & DDB_INTSCHM)
   1404  1.31  christos 		printf("%s: dcmsetischeme(%d): cur %d, ints %d, chars %d\n",
   1405  1.34   thorpej 		       sc->sc_dev.dv_xname, perchar, dis->dis_perchar,
   1406   1.1       cgd 		       dis->dis_intr, dis->dis_char);
   1407   1.1       cgd 	if ((flags & DIS_RESET) == 0 && perchar == dis->dis_perchar) {
   1408  1.31  christos 		printf("%s: dcmsetischeme: redundent request %d\n",
   1409  1.34   thorpej 		       sc->sc_dev.dv_xname, perchar);
   1410   1.1       cgd 		return;
   1411   1.1       cgd 	}
   1412   1.1       cgd #endif
   1413   1.1       cgd 	/*
   1414   1.1       cgd 	 * If perchar is non-zero, we enable interrupts on all characters
   1415   1.1       cgd 	 * otherwise we disable perchar interrupts and use periodic
   1416   1.1       cgd 	 * polling interrupts.
   1417   1.1       cgd 	 */
   1418   1.1       cgd 	dis->dis_perchar = perchar;
   1419   1.1       cgd 	mask = perchar ? 0xf : 0x0;
   1420   1.1       cgd 	for (i = 0; i < 256; i++)
   1421   1.1       cgd 		dcm->dcm_bmap[i].data_data = mask;
   1422   1.1       cgd 	/*
   1423   1.1       cgd 	 * Don't slow down tandem mode, interrupt on flow control
   1424   1.1       cgd 	 * chars for any port on the board.
   1425   1.1       cgd 	 */
   1426   1.1       cgd 	if (!perchar) {
   1427  1.36    scottr 		struct tty *tp;
   1428   1.1       cgd 		int c;
   1429   1.1       cgd 
   1430  1.20   thorpej 		for (i = 0; i < NDCMPORT; i++) {
   1431  1.20   thorpej 			tp = sc->sc_tty[i];
   1432  1.20   thorpej 
   1433   1.1       cgd 			if ((c = tp->t_cc[VSTART]) != _POSIX_VDISABLE)
   1434   1.1       cgd 				dcm->dcm_bmap[c].data_data |= (1 << i);
   1435   1.1       cgd 			if ((c = tp->t_cc[VSTOP]) != _POSIX_VDISABLE)
   1436   1.1       cgd 				dcm->dcm_bmap[c].data_data |= (1 << i);
   1437   1.1       cgd 		}
   1438   1.1       cgd 	}
   1439   1.1       cgd 	/*
   1440   1.1       cgd 	 * Board starts with timer disabled so if first call is to
   1441   1.1       cgd 	 * set perchar mode then we don't want to toggle the timer.
   1442   1.1       cgd 	 */
   1443   1.1       cgd 	if (flags == (DIS_RESET|DIS_PERCHAR))
   1444   1.1       cgd 		return;
   1445   1.1       cgd 	/*
   1446   1.1       cgd 	 * Toggle card 16.7ms interrupts (we first make sure that card
   1447   1.1       cgd 	 * has cleared the bit so it will see the toggle).
   1448   1.1       cgd 	 */
   1449   1.1       cgd 	while (dcm->dcm_cr & CR_TIMER)
   1450   1.1       cgd 		;
   1451   1.1       cgd 	SEM_LOCK(dcm);
   1452   1.1       cgd 	dcm->dcm_cr |= CR_TIMER;
   1453   1.1       cgd 	SEM_UNLOCK(dcm);
   1454   1.1       cgd }
   1455   1.1       cgd 
   1456  1.65   thorpej static void
   1457  1.65   thorpej dcminit(struct dcmdevice *dcm, int port, int rate)
   1458  1.22   thorpej {
   1459  1.22   thorpej 	int s, mode;
   1460  1.22   thorpej 
   1461  1.22   thorpej 	mode = LC_8BITS | LC_1STOP;
   1462  1.22   thorpej 
   1463  1.22   thorpej 	s = splhigh();
   1464  1.22   thorpej 
   1465  1.22   thorpej 	/*
   1466  1.22   thorpej 	 * Wait for transmitter buffer to empty.
   1467  1.22   thorpej 	 */
   1468  1.22   thorpej 	while (dcm->dcm_thead[port].ptr != dcm->dcm_ttail[port].ptr)
   1469  1.22   thorpej 		DELAY(DCM_USPERCH(rate));
   1470  1.22   thorpej 
   1471  1.22   thorpej 	/*
   1472  1.22   thorpej 	 * Make changes known to hardware.
   1473  1.22   thorpej 	 */
   1474  1.22   thorpej 	dcm->dcm_data[port].dcm_baud = ttspeedtab(rate, dcmspeedtab);
   1475  1.22   thorpej 	dcm->dcm_data[port].dcm_conf = mode;
   1476  1.22   thorpej 	SEM_LOCK(dcm);
   1477  1.22   thorpej 	dcm->dcm_cmdtab[port].dcm_data |= CT_CON;
   1478  1.22   thorpej 	dcm->dcm_cr |= (1 << port);
   1479  1.22   thorpej 	SEM_UNLOCK(dcm);
   1480  1.22   thorpej 
   1481  1.22   thorpej 	/*
   1482  1.22   thorpej 	 * Delay for config change to take place. Weighted by baud.
   1483  1.22   thorpej 	 * XXX why do we do this?
   1484  1.22   thorpej 	 */
   1485  1.22   thorpej 	DELAY(16 * DCM_USPERCH(rate));
   1486  1.22   thorpej 	splx(s);
   1487  1.22   thorpej }
   1488  1.22   thorpej 
   1489   1.1       cgd /*
   1490  1.34   thorpej  * Empirically derived self-test magic
   1491  1.34   thorpej  */
   1492  1.65   thorpej static int
   1493  1.65   thorpej dcmselftest(struct dcm_softc *sc)
   1494  1.34   thorpej {
   1495  1.34   thorpej 	struct dcmdevice *dcm = sc->sc_dcm;
   1496  1.36    scottr 	int timo = 0;
   1497  1.36    scottr 	int s, rv;
   1498  1.34   thorpej 
   1499  1.35   thorpej 	rv = 1;
   1500  1.35   thorpej 
   1501  1.35   thorpej 	s = splhigh();
   1502  1.34   thorpej 	dcm->dcm_rsid = DCMRS;
   1503  1.34   thorpej 	DELAY(50000);	/* 5000 is not long enough */
   1504  1.63   tsutsui 	dcm->dcm_rsid = 0;
   1505  1.34   thorpej 	dcm->dcm_ic = IC_IE;
   1506  1.34   thorpej 	dcm->dcm_cr = CR_SELFT;
   1507  1.35   thorpej 	while ((dcm->dcm_ic & IC_IR) == 0) {
   1508  1.34   thorpej 		if (++timo == 20000)
   1509  1.35   thorpej 			goto out;
   1510  1.35   thorpej 		DELAY(1);
   1511  1.35   thorpej 	}
   1512  1.34   thorpej 	DELAY(50000);	/* XXX why is this needed ???? */
   1513  1.35   thorpej 	while ((dcm->dcm_iir & IIR_SELFT) == 0) {
   1514  1.34   thorpej 		if (++timo == 400000)
   1515  1.35   thorpej 			goto out;
   1516  1.35   thorpej 		DELAY(1);
   1517  1.35   thorpej 	}
   1518  1.34   thorpej 	DELAY(50000);	/* XXX why is this needed ???? */
   1519  1.34   thorpej 	if (dcm->dcm_stcon != ST_OK) {
   1520  1.34   thorpej #if 0
   1521  1.34   thorpej 		if (hd->hp_args->hw_sc != conscode)
   1522  1.34   thorpej 			printf("dcm%d: self test failed: %x\n",
   1523  1.34   thorpej 			       brd, dcm->dcm_stcon);
   1524  1.34   thorpej #endif
   1525  1.35   thorpej 		goto out;
   1526  1.34   thorpej 	}
   1527  1.34   thorpej 	dcm->dcm_ic = IC_ID;
   1528  1.35   thorpej 	rv = 0;
   1529  1.35   thorpej 
   1530  1.35   thorpej  out:
   1531  1.34   thorpej 	splx(s);
   1532  1.72   tsutsui 	return rv;
   1533  1.34   thorpej }
   1534  1.34   thorpej 
   1535  1.34   thorpej /*
   1536   1.1       cgd  * Following are all routines needed for DCM to act as console
   1537   1.1       cgd  */
   1538   1.1       cgd 
   1539  1.24   thorpej int
   1540  1.48  gmcgarry dcmcnattach(bus_space_tag_t bst, bus_addr_t addr, int scode)
   1541   1.1       cgd {
   1542  1.59  gmcgarry 	bus_space_handle_t bsh;
   1543  1.59  gmcgarry 	caddr_t va;
   1544  1.59  gmcgarry 	struct dcmdevice *dcm;
   1545  1.52   gehenna 	int maj;
   1546  1.48  gmcgarry 
   1547  1.59  gmcgarry 	if (bus_space_map(bst, addr, DIOCSIZE, 0, &bsh))
   1548  1.72   tsutsui 		return 1;
   1549  1.48  gmcgarry 
   1550  1.59  gmcgarry 	va = bus_space_vaddr(bst, bsh);
   1551  1.48  gmcgarry 	dcm = (struct dcmdevice *)va;
   1552   1.1       cgd 
   1553  1.20   thorpej 	switch (dcm->dcm_rsid) {
   1554  1.48  gmcgarry #ifdef CONSCODE
   1555   1.1       cgd 	case DCMID:
   1556  1.48  gmcgarry #endif
   1557   1.1       cgd 	case DCMID|DCMCON:
   1558   1.1       cgd 		break;
   1559   1.1       cgd 	default:
   1560  1.48  gmcgarry 		goto error;
   1561  1.22   thorpej 	}
   1562  1.24   thorpej 
   1563  1.48  gmcgarry 	dcminit(dcm, DCMCONSPORT, dcmdefaultrate);
   1564  1.59  gmcgarry 	dcmconsinit = 1;
   1565  1.48  gmcgarry 	dcmconscode = scode;
   1566  1.59  gmcgarry 	dcm_cn = dcm;
   1567  1.48  gmcgarry 
   1568  1.59  gmcgarry 	/* locate the major number */
   1569  1.59  gmcgarry 	maj = cdevsw_lookup_major(&dcm_cdevsw);
   1570  1.48  gmcgarry 
   1571  1.59  gmcgarry 	/* initialize required fields */
   1572  1.59  gmcgarry 	cn_tab = &dcm_cons;
   1573  1.59  gmcgarry 	cn_tab->cn_dev = makedev(maj, 0);
   1574  1.22   thorpej 
   1575   1.1       cgd #ifdef KGDB_CHEAT
   1576  1.24   thorpej 	/* XXX this needs to be fixed. */
   1577   1.1       cgd 	/*
   1578   1.1       cgd 	 * This doesn't currently work, at least not with ite consoles;
   1579   1.1       cgd 	 * the console hasn't been initialized yet.
   1580   1.1       cgd 	 */
   1581  1.52   gehenna 	if (major(kgdb_dev) == maj &&
   1582  1.20   thorpej 	    DCMBOARD(DCMUNIT(kgdb_dev)) == DCMBOARD(unit)) {
   1583  1.22   thorpej 		dcminit(dcm_cn, DCMPORT(DCMUNIT(kgdb_dev)), kgdb_rate);
   1584   1.1       cgd 		if (kgdb_debug_init) {
   1585   1.1       cgd 			/*
   1586   1.1       cgd 			 * We assume that console is ready for us...
   1587   1.1       cgd 			 * this assumes that a dca or ite console
   1588   1.1       cgd 			 * has been selected already and will init
   1589   1.1       cgd 			 * on the first putc.
   1590   1.1       cgd 			 */
   1591  1.31  christos 			printf("dcm%d: ", DCMUNIT(kgdb_dev));
   1592   1.1       cgd 			kgdb_connect(1);
   1593   1.1       cgd 		}
   1594   1.1       cgd 	}
   1595   1.1       cgd #endif
   1596   1.1       cgd 
   1597  1.20   thorpej 
   1598  1.72   tsutsui 	return 0;
   1599  1.48  gmcgarry 
   1600  1.48  gmcgarry error:
   1601  1.59  gmcgarry 	bus_space_unmap(bst, bsh, DIOCSIZE);
   1602  1.72   tsutsui 	return 1;
   1603   1.1       cgd }
   1604   1.1       cgd 
   1605  1.24   thorpej /* ARGSUSED */
   1606  1.65   thorpej static int
   1607  1.65   thorpej dcmcngetc(dev_t dev)
   1608   1.1       cgd {
   1609  1.20   thorpej 	struct dcmrfifo *fifo;
   1610  1.20   thorpej 	struct dcmpreg *pp;
   1611  1.20   thorpej 	u_int head;
   1612  1.24   thorpej 	int s, c, stat;
   1613  1.22   thorpej 
   1614  1.24   thorpej 	pp = dcm_preg(dcm_cn, DCMCONSPORT);
   1615  1.20   thorpej 
   1616   1.1       cgd 	s = splhigh();
   1617   1.1       cgd 	head = pp->r_head & RX_MASK;
   1618  1.24   thorpej 	fifo = &dcm_cn->dcm_rfifos[3-DCMCONSPORT][head>>1];
   1619   1.1       cgd 	while (head == (pp->r_tail & RX_MASK))
   1620   1.1       cgd 		;
   1621   1.1       cgd 	/*
   1622   1.1       cgd 	 * If board interrupts are enabled, just let our received char
   1623   1.1       cgd 	 * interrupt through in case some other port on the board was
   1624   1.1       cgd 	 * busy.  Otherwise we must clear the interrupt.
   1625   1.1       cgd 	 */
   1626  1.22   thorpej 	SEM_LOCK(dcm_cn);
   1627  1.22   thorpej 	if ((dcm_cn->dcm_ic & IC_IE) == 0)
   1628  1.22   thorpej 		stat = dcm_cn->dcm_iir;
   1629  1.22   thorpej 	SEM_UNLOCK(dcm_cn);
   1630   1.1       cgd 	c = fifo->data_char;
   1631   1.1       cgd 	stat = fifo->data_stat;
   1632   1.1       cgd 	pp->r_head = (head + 2) & RX_MASK;
   1633   1.1       cgd 	splx(s);
   1634  1.72   tsutsui 	return c;
   1635   1.1       cgd }
   1636   1.1       cgd 
   1637   1.1       cgd /*
   1638   1.1       cgd  * Console kernel output character routine.
   1639   1.1       cgd  */
   1640  1.24   thorpej /* ARGSUSED */
   1641  1.65   thorpej static void
   1642  1.65   thorpej dcmcnputc(dev_t dev, int c)
   1643   1.1       cgd {
   1644  1.20   thorpej 	struct dcmpreg *pp;
   1645   1.1       cgd 	unsigned tail;
   1646  1.36    scottr 	int s, stat;
   1647  1.22   thorpej 
   1648  1.24   thorpej 	pp = dcm_preg(dcm_cn, DCMCONSPORT);
   1649  1.20   thorpej 
   1650   1.1       cgd 	s = splhigh();
   1651   1.1       cgd #ifdef KGDB
   1652   1.1       cgd 	if (dev != kgdb_dev)
   1653   1.1       cgd #endif
   1654   1.1       cgd 	if (dcmconsinit == 0) {
   1655  1.24   thorpej 		dcminit(dcm_cn, DCMCONSPORT, dcmdefaultrate);
   1656   1.1       cgd 		dcmconsinit = 1;
   1657   1.1       cgd 	}
   1658   1.1       cgd 	tail = pp->t_tail & TX_MASK;
   1659   1.1       cgd 	while (tail != (pp->t_head & TX_MASK))
   1660   1.1       cgd 		;
   1661  1.24   thorpej 	dcm_cn->dcm_tfifos[3-DCMCONSPORT][tail].data_char = c;
   1662   1.1       cgd 	pp->t_tail = tail = (tail + 1) & TX_MASK;
   1663  1.22   thorpej 	SEM_LOCK(dcm_cn);
   1664  1.24   thorpej 	dcm_cn->dcm_cmdtab[DCMCONSPORT].dcm_data |= CT_TX;
   1665  1.24   thorpej 	dcm_cn->dcm_cr |= (1 << DCMCONSPORT);
   1666  1.22   thorpej 	SEM_UNLOCK(dcm_cn);
   1667   1.1       cgd 	while (tail != (pp->t_head & TX_MASK))
   1668   1.1       cgd 		;
   1669   1.1       cgd 	/*
   1670   1.1       cgd 	 * If board interrupts are enabled, just let our completion
   1671   1.1       cgd 	 * interrupt through in case some other port on the board
   1672   1.1       cgd 	 * was busy.  Otherwise we must clear the interrupt.
   1673   1.1       cgd 	 */
   1674  1.22   thorpej 	if ((dcm_cn->dcm_ic & IC_IE) == 0) {
   1675  1.22   thorpej 		SEM_LOCK(dcm_cn);
   1676  1.22   thorpej 		stat = dcm_cn->dcm_iir;
   1677  1.22   thorpej 		SEM_UNLOCK(dcm_cn);
   1678   1.1       cgd 	}
   1679   1.1       cgd 	splx(s);
   1680   1.1       cgd }
   1681