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