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