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