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